Skip to content

Commit

Permalink
ncm-network: nmstate - default gateway fix.
Browse files Browse the repository at this point in the history
nmstate requires next-hop-interface for every route to be added.
Create default route entry only on the interface if default_gateway falls within subnet boundary.

fixes quattor#1655
  • Loading branch information
Abdul Karim authored and Abdul Karim committed Feb 20, 2024
1 parent 125da86 commit da1e033
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
25 changes: 20 additions & 5 deletions ncm-network/src/main/perl/nmstate.pm
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,17 @@ sub generate_vip_config {
return $iface_cfg;
}

# Check if given ip belongs to a network
sub ip_in_network {
my ($self, $check_ip, $ip, $netmask) = @_;
# is the given gw ip in his ip/netmask.
my $thisip = NetAddr::IP->new("$check_ip");
if ($thisip->within(NetAddr::IP->new("$ip", "$netmask"))) {
return 1;
}
return 0;
}

# 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 @@ -388,12 +399,16 @@ sub generate_nmstate_config
# create default route entry.
my %default_rt;
if ($default_gw) {
# create only default gw entry if gw entry match interface gateway defined
# nmstate configure requires next-hop-interface.
# create default gw entry on this interface only if it falls within the subnet boundary.
# otherwise this interface is not the default gw interface.
if ((defined($iface->{gateway})) and ($iface->{gateway} eq $default_gw)) {
$default_rt{destination} = '0.0.0.0/0';
$default_rt{'next-hop-address'} = $default_gw;
$default_rt{'next-hop-interface'} = $device;
if ((defined($iface->{ip})) and (defined($iface->{netmask}))) {
my $is_dgw_iface = $self->ip_in_network($default_gw, $iface->{ip}, $iface->{netmask});
if ($is_dgw_iface) {
$default_rt{destination} = '0.0.0.0/0';
$default_rt{'next-hop-address'} = $default_gw;
$default_rt{'next-hop-interface'} = $device;
}
}
}
# combined default route with any policy routing/rule, if any
Expand Down
9 changes: 9 additions & 0 deletions ncm-network/src/test/perl/nmstate_advance.t
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ routes:
config:
- next-hop-interface: eth0
state: absent
- destination: 0.0.0.0/0
next-hop-address: 4.3.2.254
next-hop-interface: eth0
- destination: 1.2.3.4/32
next-hop-interface: eth0
- destination: 1.2.3.5/24
Expand Down Expand Up @@ -82,6 +85,9 @@ routes:
config:
- next-hop-interface: eth0.123
state: absent
- destination: 0.0.0.0/0
next-hop-address: 4.3.2.254
next-hop-interface: eth0.123
- destination: 1.2.3.4/32
next-hop-interface: eth0.123
EOF
Expand Down Expand Up @@ -126,6 +132,9 @@ routes:
config:
- next-hop-interface: bond0
state: absent
- destination: 0.0.0.0/0
next-hop-address: 4.3.2.254
next-hop-interface: bond0
EOF


Expand Down
3 changes: 3 additions & 0 deletions ncm-network/src/test/perl/nmstate_simple.t
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ routes:
config:
- next-hop-interface: eth0
state: absent
- destination: 0.0.0.0/0
next-hop-address: 4.3.2.254
next-hop-interface: eth0
EOF

Readonly my $NOTTOREMOVE => <<EOF;
Expand Down
1 change: 1 addition & 0 deletions ncm-network/src/test/resources/nmstate_advance.pan
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ object template nmstate_advance;
include 'simple_base_profile';
include 'components/network/config-nmstate';

"/system/network/default_gateway" = "4.3.2.254";
# additional interface testing for nmstate.
"/system/network/interfaces/eth1" = create("dhcpinterface");
"/hardware/cards/nic/eth1/hwaddr" = "6e:a5:1b:55:77:0b";
Expand Down

0 comments on commit da1e033

Please sign in to comment.