Skip to content

Commit

Permalink
mxrouterctl: Factor out common code
Browse files Browse the repository at this point in the history
Factor out common code of `mxrouterctl start` and `mxrouterctl restart`.
  • Loading branch information
donald committed Feb 15, 2024
1 parent b571970 commit 688008a
Showing 1 changed file with 19 additions and 23 deletions.
42 changes: 19 additions & 23 deletions mxrouter/mxrouterctl
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,23 @@ sub start_process_if {

##########################################

sub prepare_netns_interfaces {
# create veth pair if not yet available
for my $dev (sort keys %$want_veth) {
-d "/sys/class/net/$dev" and next;
sys('ip', 'link', 'add', $dev, 'type', 'veth', 'peer', 'name', $dev, 'netns', $NETNS);
sys('ip', 'link', 'set', $dev, 'up');
}
# move interfaces which are still in the init namespace
# ignore veth devices and lo, which exists in both namespaces
for my $dev (sort keys %$want_if) {
$dev eq 'lo' || $want_veth->{$dev} and next;
-d "/sys/class/net/$dev" and sys('ip', 'link', 'set', $dev, 'netns', $NETNS);
}
}

##########################################

my @SAVED_ARGV=@ARGV;

GetOptions(OPTIONS) or die USAGE;
Expand Down Expand Up @@ -808,36 +825,15 @@ if (!$opt_this_ns) {

have_netns($NETNS) and die "already running (network namespace $NETNS already exists)\n";
create_netns($NETNS);
for my $dev (sort keys %$want_veth) {
-d "/sys/class/net/$dev" and next;
sys('ip', 'link', 'add', $dev, 'type', 'veth', 'peer', 'name', $dev, 'netns', $NETNS);
sys('ip', 'link', 'set', $dev, 'up');
}
# move interfaces which are still in the init namespace
# ignore veth devices and lo, which exists in both namespaces
for my $dev (sort keys %$want_if) {
$dev eq 'lo' || $want_veth->{$dev} and next;
-d "/sys/class/net/$dev" and sys('ip', 'link', 'set', $dev, 'netns', $NETNS);
}
prepare_netns_interfaces();
system('ip','netns','exec',$NETNS,$0,'--this-ns',@SAVED_ARGV) and exit 1;
} elsif ($cmd eq 'stop') {
have_netns($NETNS) or die "not running (network namespace $NETNS does not exist)\n";
system('ip','netns','exec',$NETNS,$0,'--this-ns',@SAVED_ARGV) and exit 1;
delete_netns($NETNS);
} elsif ($cmd eq 'reload') {
have_netns($NETNS) or die "not running (network namespace $NETNS does not exist)\n";
# create veth pair if not yet available
for my $dev (sort keys %$want_veth) {
-d "/sys/class/net/$dev" and next;
sys('ip', 'link', 'add', $dev, 'type', 'veth', 'peer', 'name', $dev, 'netns', $NETNS);
sys('ip', 'link', 'set', $dev, 'up');
}
# move interfaces which are still in the init namespace
# ignore veth devices and lo, which exists in both namespaces
for my $dev (sort keys %$want_if) {
$dev eq 'lo' || $want_veth->{$dev} and next;
-d "/sys/class/net/$dev" and sys('ip', 'link', 'set', $dev, 'netns', $NETNS);
}
prepare_netns_interfaces();
system('ip', 'netns', 'exec', $NETNS,$0,'--this-ns', @SAVED_ARGV) and exit 1;
} else {
have_netns($NETNS) or die "not running (network namespace $NETNS does not exist)\n";
Expand Down

0 comments on commit 688008a

Please sign in to comment.