Skip to content

Commit

Permalink
ncm-network: provide backward compatibility for vlan interface config…
Browse files Browse the repository at this point in the history
… for nmstate

improvement made to nmstate to provide backward compatibility with network.pm for configuring vlan
interface.
  • Loading branch information
Abdul Karim authored and Abdul Karim committed Mar 6, 2024
1 parent 125da86 commit 73e3e22
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
29 changes: 25 additions & 4 deletions ncm-network/src/main/perl/nmstate.pm
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,26 @@ sub generate_vip_config {
return $iface_cfg;
}

# 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 get_vlanid {
my ($self, $iface, $device) = @_;
my $vlanid = $iface;
# 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
$vlanid =~ s/^[^.]*.//;
# if vlanid is empty here, lets check if device has vlan id.
if ((! $vlanid) && ($device)) {
$vlanid = $device;
$vlanid =~ s/^[^.]*.//;
}
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 +350,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->get_vlanid($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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 73e3e22

Please sign in to comment.