Skip to content

Commit

Permalink
Merge pull request #1693 from stdweird/bonding_ib
Browse files Browse the repository at this point in the history
ncm-network: nmstate: simple interfaces reorder on apply/delete
  • Loading branch information
jrha authored Sep 6, 2024
2 parents 13c3773 + 6cc99a1 commit f9414a7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
27 changes: 26 additions & 1 deletion ncm-network/src/main/perl/network.pm
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ our @EXPORT = qw($FAILED_SUFFIX

# list of constants to allow inheritance via $self->CONSTANTNAME
use constant IFCFG_DIR => "/etc/sysconfig/network-scripts";
use constant BOND_MASTER_STARTS_SLAVES => 1;


sub backup_dir
{
Expand Down Expand Up @@ -891,6 +893,19 @@ sub process_network
}
$iface->{my_inner_ipaddr} .= "/$iface->{my_inner_prefix}";
}

# insert master->slaves data
if ($iface->{master}) {
my $bond = $iface->{master};
my $bondiface = $nwtree->{interfaces}->{$bond};
if ($bondiface) {
$bondiface->{slaves} = [] if !exists($bondiface->{slaves});
push(@{$bondiface->{slaves}}, $ifname);
} else {
$self->warn("Interface $ifname has master $bond configured, but corresponding iface entry not found");
}
}

}

return $nwtree;
Expand Down Expand Up @@ -1445,7 +1460,17 @@ sub make_ifup
$exifiles->{"$cfg_filename"} == $REMOVE) {
$self->verbose("Not starting $iface scheduled for removal");
} else {
if ($ifaces->{$iface}->{master}) {
if (!$self->BOND_MASTER_STARTS_SLAVES && $ifaces->{$iface}->{slaves}) {
my @slaves = @{$ifaces->{$iface}->{slaves}};
# master won't bring up slaves, so add them here
$self->verbose("Found MASTER interface $iface in ifdown map, ",
"adding slaves @slaves to ifup.");
foreach my $slname (@slaves) {
$ifup{$slname} = 1;
}
}

if ($self->BOND_MASTER_STARTS_SLAVES && $ifaces->{$iface}->{master}) {
# bonding devices: don't bring the slaves up, only the master
$self->verbose("Found SLAVE interface $iface in ifdown map, ",
"not starting it with ifup; is left for master $ifaces->{$iface}->{master}.");
Expand Down
11 changes: 10 additions & 1 deletion ncm-network/src/main/perl/nmstate.pm
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ Readonly my $YTRUE => $EDG::WP4::CCM::TextRender::ELEMENT_CONVERT{yaml_boolean}-
Readonly my $YFALSE => $EDG::WP4::CCM::TextRender::ELEMENT_CONVERT{yaml_boolean}->(0);

use constant IFCFG_DIR => "/etc/nmstate";
use constant BOND_MASTER_STARTS_SLAVES => 0;


sub iface_filename
{
Expand Down Expand Up @@ -610,8 +612,15 @@ sub nmstate_apply
{
my ($self, $exifiles, $ifup, $ifdown, $nwsrv) = @_;


my @ifaces = sort keys %$ifup;
my @ifaces_down = sort keys %$ifdown;

# primitive re-ordering to make sure eg bond are apply'ed last, and removed first
my $order_pattern = '^bond';
@ifaces = ((grep {$_ !~ m/$order_pattern/} @ifaces), (grep {$_ =~ m/$order_pattern/} @ifaces));
@ifaces_down = ((grep {$_ =~ m/$order_pattern/} @ifaces_down), (grep {$_ !~ m/$order_pattern/} @ifaces_down));

my $action;

if (@ifaces) {
Expand All @@ -622,7 +631,7 @@ sub nmstate_apply
my $ymlfile = $self->iface_filename($iface);
if ($self->any_exists($ymlfile)){
push(@cmds, [$NMSTATECTL, "apply", $ymlfile]);
push(@cmds, [qw(sleep 10)]) if ($iface =~ m/bond/);
push(@cmds, [qw(/usr/bin/sleep 10)]) if ($iface =~ m/^bond/);
} else {
# TODO: perhaps try down the interface? it's done later anyway
$self->verbose("$ymlfile does not exist for $iface, not applying");
Expand Down
18 changes: 18 additions & 0 deletions ncm-network/src/test/perl/nmstate_advance.t
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,22 @@ is($dummy_yml, $DUMMY_YML, "Exact dummy interface yml config");
my $alias_yml = get_file_contents("/etc/nmstate/eth4.yml");
is($alias_yml, $ALIAS_YML, "Exact alias interface yml config");

diag "all history commands ", explain \@Test::Quattor::command_history;

# apply commands are sorted alphabetically, bond last
ok(command_history_ok([
'/usr/bin/nmstatectl apply /etc/nmstate/dummy_myvip.yml',
'/usr/bin/nmstatectl apply /etc/nmstate/eth0.yml',
'/usr/bin/nmstatectl apply /etc/nmstate/eth0.123.yml',
'/usr/bin/nmstatectl apply /etc/nmstate/eth1.yml',
'/usr/bin/nmstatectl apply /etc/nmstate/eth2.yml',
'/usr/bin/nmstatectl apply /etc/nmstate/eth3.yml',
'/usr/bin/nmstatectl apply /etc/nmstate/eth4.yml',
'/usr/bin/nmstatectl apply /etc/nmstate/ib0.yml',
'/usr/bin/nmstatectl apply /etc/nmstate/ib1.12345.yml',
'/usr/bin/nmstatectl apply /etc/nmstate/vlan0.yml',
'/usr/bin/nmstatectl apply /etc/nmstate/bond0.yml',
], []));


done_testing();

0 comments on commit f9414a7

Please sign in to comment.