Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ncm-network: nmstate dgw fix #1656

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions ncm-network/src/main/perl/nmstate.pm
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,13 @@ sub find_vlan_id {
}
return $vlanid;
}
# Check if given ip belongs to a network
sub ip_in_network {
my ($self, $check_ip, $ip, $netmask) = @_;
# is the given ip in his ip/netmask.
my $subnet = NetAddr::IP->new("$ip", "$netmask");
return NetAddr::IP->new("$check_ip")->within($subnet);
}

# 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.
Expand Down Expand Up @@ -414,12 +421,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
# 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;
# next-hop-interface is mandatory in nmstate therefore we need interface to create default route entry.
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
12 changes: 12 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 All @@ -107,6 +113,9 @@ routes:
config:
- next-hop-interface: vlan0
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: vlan0
EOF
Expand Down Expand Up @@ -152,6 +161,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
Loading