Skip to content

Commit

Permalink
Merge pull request #1635 from aka7/ncm_network_fix
Browse files Browse the repository at this point in the history
ncm-network: provide a better way to cleanup inactive connections
  • Loading branch information
jrha committed Dec 20, 2023
2 parents 756e746 + 6f5e7f0 commit 00d516b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
2 changes: 2 additions & 0 deletions ncm-network/src/main/pan/components/network/core-schema.pan
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,8 @@ type structure_network = {
"allow_nm" ? boolean
@{let NetworkManager manage the dns (only for nmstate)}
"nm_manage_dns" : boolean = false
@{let ncm-network cleanup inactive connections (only for nmstate)}
"nm_clean_inactive_conn" : boolean = true
"primary_ip" ? string
"routers" ? structure_router{}
"ipv6" ? structure_ipv6
Expand Down
34 changes: 19 additions & 15 deletions ncm-network/src/main/perl/nmstate.pm
Original file line number Diff line number Diff line change
Expand Up @@ -480,21 +480,19 @@ sub is_active_interface
return $found;
}

# check for existing connections, will clear the default connections created by 'NM with Wired connecton x'
# check for existing connections, any connections which are not active.
# good to have.
sub clear_default_nm_connections
sub clear_inactive_nm_connections
{
my ($self) = @_;
# NM creates auto connections with Wired connection x
# Delete all connections with name 'Wired connection', everything ncm-network creates will have connection name set to interface name.
my $output = $self->runrun([$NMCLI_CMD, "-t", "-f", "name", "conn"]);
my @existing_conn = split('\n', $output);
my %current_conn;
foreach my $conn_name (@existing_conn) {
$conn_name =~ s/\s+$//;
if ($conn_name =~ /Wired connection/){
$self->verbose("Clearing default connections created automatically by NetworkManager [ $conn_name ]");
$output = $self->runrun([$NMCLI_CMD,"conn", "delete", $conn_name]);
# clean any inactive connections
my $output = $self->runrun([$NMCLI_CMD, "-t", "-f", "uuid,device,name,state,active", "conn"]);
my @all_conn = split('\n', $output);
foreach my $conn (@all_conn) {
my ($uuid,$device,$name,$state,$active) = split(':', $conn);
if ($active eq 'no') {
$self->verbose("Clearing inactive connection for [ uuid=$uuid, name=$name, state=$state, active=$active ]");
$output = $self->runrun([$NMCLI_CMD,"conn", "delete", $uuid]);
$self->verbose($output);
}
}
Expand All @@ -510,9 +508,7 @@ sub nmstate_apply

if (@ifaces) {
$self->info("Applying changes using $NMSTATECTL ", join(', ', @ifaces));
my @cmds;
# clear any connections created by NM with 'Wired connection x' to start fresh.
$self->clear_default_nm_connections();
my @cmds;
foreach my $iface (@ifaces) {
# apply config using nmstatectl
my $ymlfile = $self->iface_filename($iface);
Expand Down Expand Up @@ -738,6 +734,14 @@ sub Configure
}
};

# cleanup dangling inactive connections after ncm network changes are applied.
# defaults to cleanup
my $clean_inactive_conn = $net->{nm_clean_inactive_conn};
if ($clean_inactive_conn and $stopstart) {
# look to cleanup connections only when something is changed.
$self->clear_inactive_nm_connections;
}

# test network
my $ccm_tree = $config->getTree("/software/components/ccm");
my $profile = $ccm_tree && $ccm_tree->{profile};
Expand Down
2 changes: 1 addition & 1 deletion ncm-network/src/test/perl/nmstate_simple.t
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ ok(command_history_ok([
'/usr/bin/nmcli connection',
'service NetworkManager reload',
'systemctl disable nmstate',
'/usr/bin/nmcli -t -f name conn',
'/usr/bin/nmstatectl apply /etc/nmstate/eth0.yml',
'/usr/bin/nmstatectl apply /etc/nmstate/resolv.yml',
'service NetworkManager reload',
Expand All @@ -115,6 +114,7 @@ ok(command_history_ok([
'/usr/bin/nmstatectl show',
'/usr/bin/nmcli dev status',
'/usr/bin/nmcli connection',
'/usr/bin/nmcli -t -f uuid,device,name,state,active conn',
'ccm-fetch'
], []));

Expand Down

0 comments on commit 00d516b

Please sign in to comment.