]> git.proxmox.com Git - pve-container.git/commitdiff
lxc: Avoid open-coding normal vs SDN-specific tap_plug()
authorChristoph Heiss <c.heiss@proxmox.com>
Tue, 21 Feb 2023 08:05:48 +0000 (09:05 +0100)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Tue, 21 Feb 2023 16:30:26 +0000 (17:30 +0100)
This pattern is used in multiple places, thus just extract it into a sub
on its own.

No functional changes.

Signed-off-by: Christoph Heiss <c.heiss@proxmox.com>
src/PVE/LXC.pm
src/lxcnetaddbr

index cbbb82d9c81eaeb3147dd9f665693c3241812d11..d419124027d5afec973e629397ba6f2cda3ce78c 100644 (file)
@@ -918,6 +918,18 @@ sub vm_stop_cleanup {
     warn $@ if $@; # avoid errors - just warn
 }
 
+sub net_tap_plug : prototype($$$$$$;$) {
+    my ($iface, $bridge, $tag, $firewall, $trunks, $rate, $opts) = @_;
+
+    if ($have_sdn) {
+       PVE::Network::SDN::Zones::tap_plug($iface, $bridge, $tag, $firewall, $trunks, $rate);
+       PVE::Network::SDN::Zones::add_bridge_fdb($iface, $opts->{mac}, $bridge, $firewall)
+           if defined($opts->{mac});
+    } else {
+       PVE::Network::tap_plug($iface, $bridge, $tag, $firewall, $trunks, $rate, $opts);
+    }
+}
+
 sub update_net {
     my ($vmid, $conf, $opt, $newnet, $netid, $rootdir) = @_;
 
@@ -957,14 +969,7 @@ sub update_net {
                }
 
                my ($bridge, $mac, $firewall, $rate) = $newnet->@{'bridge', 'hwaddr', 'firewall', 'rate'};
-               if ($have_sdn) {
-                   PVE::Network::SDN::Zones::tap_plug(
-                       $veth, $bridge, $newnet->{tag}, $firewall, $newnet->{trunks}, $rate);
-                   PVE::Network::SDN::Zones::add_bridge_fdb($veth, $mac, $bridge, $firewall);
-               } else {
-                   PVE::Network::tap_plug(
-                       $veth, $bridge, $newnet->{tag}, $firewall, $newnet->{trunks}, $rate, { mac => $mac });
-               }
+               PVE::LXC::net_tap_plug($veth, $bridge, $newnet->{tag}, $firewall, $newnet->{trunks}, $rate, { mac => $mac });
 
                # This includes the rate:
                foreach (qw(bridge tag firewall rate)) {
@@ -995,13 +1000,12 @@ sub hotplug_net {
 
     if ($have_sdn) {
        PVE::Network::SDN::Zones::veth_create($veth, $vethpeer, $newnet->{bridge}, $newnet->{hwaddr});
-       PVE::Network::SDN::Zones::tap_plug($veth, $newnet->{bridge}, $newnet->{tag}, $newnet->{firewall}, $newnet->{trunks}, $newnet->{rate});
-       PVE::Network::SDN::Zones::add_bridge_fdb($veth, $newnet->{hwaddr}, $newnet->{bridge}, $newnet->{firewall});
     } else {
        PVE::Network::veth_create($veth, $vethpeer, $newnet->{bridge}, $newnet->{hwaddr});
-       PVE::Network::tap_plug($veth, $newnet->{bridge}, $newnet->{tag}, $newnet->{firewall}, $newnet->{trunks}, $newnet->{rate});
-       PVE::Network::add_bridge_fdb($veth, $newnet->{hwaddr}, $newnet->{firewall}); # early returns if brport has learning on
     }
+    PVE::LXC::net_tap_plug(
+       $veth, $newnet->{bridge}, $newnet->{tag}, $newnet->{firewall}, $newnet->{trunks},
+       $newnet->{rate}, { mac => $newnet->{hwaddr} });
 
     # attach peer in container
     my $cmd = ['lxc-device', '-n', $vmid, 'add', $vethpeer, "$eth" ];
index 83052e19f0be4d6116470f85e5c647d195cc7d7f..ebd6baa82c732754320290ba9253ef479f372ea3 100755 (executable)
@@ -7,15 +7,8 @@ exit 0 if $ENV{LXC_NAME} && $ENV{LXC_NAME} !~ /^\d+$/;
 
 use PVE::LXC;
 use PVE::Tools qw(run_command);
-use PVE::Network;
 use PVE::ProcFSTools;
 
-my $have_sdn;
-eval {
-    require PVE::Network::SDN::Zones;
-    $have_sdn = 1;
-};
-
 die "got unexpected argument count\n" if scalar(@ARGV) != 5;
 
 my ($vmid, $arg2, $arg3, $type, $iface) = @ARGV;
@@ -48,6 +41,7 @@ my $firewall = $net->{firewall};
 my $bridge = $net->{bridge};
 my $trunks = $net->{trunks};
 my $rate = $net->{rate};
+my $hwaddr = $net->{hwaddr};
 
 die "missing bridge configuration" if !$bridge;
 
@@ -61,12 +55,7 @@ if (-d "/sys/class/net/$iface") {
     PVE::Tools::run_command("/sbin/ip link set dev $iface up mtu $bridgemtu");
     PVE::Tools::run_command("/sbin/ip addr add 0.0.0.0/0 dev $iface");
 
-    if ($have_sdn) {
-       PVE::Network::SDN::Zones::tap_plug($iface, $bridge, $tag, $firewall, $trunks, $rate);
-       PVE::Network::SDN::Zones::add_bridge_fdb($iface, $net->{hwaddr}, $bridge, $firewall);
-    } else {
-       PVE::Network::tap_plug($iface, $bridge, $tag, $firewall, $trunks, $rate, { mac => $net->{hwaddr}});
-    }
+    PVE::LXC::net_tap_plug($iface, $bridge, $tag, $firewall, $trunks, $rate, { mac => $hwaddr });
 }
 
 exit 0;