Skip to content

Commit

Permalink
Merge pull request #1667 from aka7/ncm_network_nmstate_vlan
Browse files Browse the repository at this point in the history
ncm-network: provide backward compatibility for vlan interface config for nmstate.
  • Loading branch information
stdweird committed Mar 18, 2024
2 parents a186b58 + fbad4e0 commit a626bba
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
34 changes: 30 additions & 4 deletions ncm-network/src/main/perl/nmstate.pm
Expand Up @@ -296,6 +296,31 @@ sub generate_vip_config {
return $iface_cfg;
}

# private sub to extract vlan id from name
# i.e given eth0.123 return 123
sub _get_vlan_id {
my ($self, $name) = @_;
return $1 if ($name =~ m/\.(\d+)$/);
}

# find vlan id from either the name or device.
# i.e eth0.123 will return 123. by checking iface name and device
# returns vlan id.
sub find_vlan_id {
my ($self, $iface, $device) = @_;
# a vlan interface can defined in two ways
# interface/name.vlanid/
# or interface/name/device=device.vlanid
# replace everything up-to and including . to get vlan id of the interface.
# favors ifacename.vlanid, then device.vlanid
my $vlanid = $self->_get_vlan_id($iface);
# if vlanid is empty here, lets check if device has vlan id.
if ((! $vlanid) && ($device)) {
$vlanid = $self->_get_vlan_id($device);
}
return $vlanid;
}

# generates the hashrefs for interface in yaml file format needed by nmstate.
# bulk of the config settings needed by the nmstate yml is done here.
# to add additional options, it should be constructed here.
Expand Down Expand Up @@ -330,10 +355,11 @@ sub generate_nmstate_config
$ifaceconfig->{state} = "up";
}
} elsif ($is_vlan_eth) {
my $vlan_id = $name;
# replace everything up-to and including . to get vlan id of the interface.
# TODO: instead of this, should perhaps add valid-id in schema? but may not be backward compatible for existing host entreis, aqdb will need updating?
$vlan_id =~ s/^[^.]*.//;;
my $vlan_id = $self->find_vlan_id($name, $iface->{device});
# if vlan_id is empty, error
if (! $vlan_id) {
$self->error("Could not find vlan id for vlan device $name");
}
$ifaceconfig->{type} = "vlan";
$ifaceconfig->{vlan}->{'base-iface'} = $iface->{physdev};
$ifaceconfig->{vlan}->{'id'} = $vlan_id;
Expand Down
28 changes: 28 additions & 0 deletions ncm-network/src/test/perl/nmstate_advance.t
Expand Up @@ -86,6 +86,32 @@ routes:
next-hop-interface: eth0.123
EOF

Readonly my $VLAN0_YML => <<EOF;
# File generated by NCM::Component::nmstate. Do not edit
---
interfaces:
- ipv4:
address:
- ip: 4.3.2.1
prefix-length: 24
dhcp: false
enabled: true
name: vlan0
profile-name: vlan0
state: up
type: vlan
vlan:
base-iface: eth0
id: '123'
routes:
config:
- next-hop-interface: vlan0
state: absent
- destination: 1.2.3.4/32
next-hop-interface: vlan0
EOF


Readonly my $DHCP_YML => <<EOF;
# File generated by NCM::Component::nmstate. Do not edit
---
Expand Down Expand Up @@ -178,4 +204,6 @@ is($bondyml, $BOND_YML, "Exact bond0 yml config");
my $vlanyml = get_file_contents("/etc/nmstate/eth0.123.yml");
is($vlanyml, $VLAN_YML, "Exact eth0.123 vlan yml config");

my $vlanyml2 = get_file_contents("/etc/nmstate/vlan0.yml");
is($vlanyml2, $VLAN0_YML, "Exact vlan0 yml config");
done_testing();
7 changes: 7 additions & 0 deletions ncm-network/src/test/resources/nmstate_advance.pan
Expand Up @@ -32,3 +32,10 @@ prefix "/system/network/interfaces/eth0";
prefix "/system/network/interfaces/eth0.123";
"physdev" = "eth0";
"route/0" = dict("address", "1.2.3.4");

# test vlan interface route on vlan for backward compatibily with network.pm
"/system/network/interfaces/vlan0" = create("defaultinterface");
prefix "/system/network/interfaces/vlan0";
"device" = "eth0.123";
"physdev" = "eth0";
"route/0" = dict("address", "1.2.3.4");

0 comments on commit a626bba

Please sign in to comment.