]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/Network.pm
tap device name parse fix
[pve-common.git] / src / PVE / Network.pm
index 23206c1d831226e83d63779d39bff970b732d133..b9d2d37527736f1e4613071a4d436da05a9c0f75 100644 (file)
@@ -9,6 +9,60 @@ use File::Basename;
 
 # host network related utility functions
 
+our $ipv4_reverse_mask = [
+    '0.0.0.0',
+    '128.0.0.0',
+    '192.0.0.0',
+    '224.0.0.0',
+    '240.0.0.0',
+    '248.0.0.0',
+    '252.0.0.0',
+    '254.0.0.0',
+    '255.0.0.0',
+    '255.128.0.0',
+    '255.192.0.0',
+    '255.224.0.0',
+    '255.240.0.0',
+    '255.248.0.0',
+    '255.252.0.0',
+    '255.254.0.0',
+    '255.255.0.0',
+    '255.255.128.0',
+    '255.255.192.0',
+    '255.255.224.0',
+    '255.255.240.0',
+    '255.255.248.0',
+    '255.255.252.0',
+    '255.255.254.0',
+    '255.255.255.0',
+    '255.255.255.128',
+    '255.255.255.192',
+    '255.255.255.224',
+    '255.255.255.240',
+    '255.255.255.248',
+    '255.255.255.252',
+    '255.255.255.254',
+    '255.255.255.255',
+];
+
+our $ipv4_mask_hash_localnet = {
+    '255.255.0.0' => 16,
+    '255.255.128.0' => 17,
+    '255.255.192.0' => 18,
+    '255.255.224.0' => 19,
+    '255.255.240.0' => 20,
+    '255.255.248.0' => 21,
+    '255.255.252.0' => 22,
+    '255.255.254.0' => 23,
+    '255.255.255.0' => 24,
+    '255.255.255.128' => 25,
+    '255.255.255.192' => 26,
+    '255.255.255.224' => 27,
+    '255.255.255.240' => 28,
+    '255.255.255.248' => 29,
+    '255.255.255.252' => 30,
+};
+
 sub setup_tc_rate_limit {
     my ($iface, $rate, $burst, $debug) = @_;
 
@@ -65,7 +119,7 @@ my $read_bridge_mtu = sub {
     return $mtu;
 };
 
-my $parse_tap_devive_name = sub {
+my $parse_tap_device_name = sub {
     my ($iface, $noerr) = @_;
 
     my ($vmid, $devid);
@@ -73,7 +127,7 @@ my $parse_tap_devive_name = sub {
     if ($iface =~ m/^tap(\d+)i(\d+)$/) {
        $vmid = $1;
        $devid = $2;
-    } elsif ($iface =~ m/^veth(\d+)\.(\d+)$/) {
+    } elsif ($iface =~ m/^veth(\d+)i(\d+)$/) {
        $vmid = $1;
        $devid = $2;
     } else {
@@ -106,10 +160,22 @@ my $cond_create_bridge = sub {
 };
 
 my $bridge_add_interface = sub {
-    my ($bridge, $iface) = @_;
+    my ($bridge, $iface, $tag) = @_;
 
     system("/sbin/brctl addif $bridge $iface") == 0 ||
        die "can't add interface 'iface' to bridge '$bridge'\n";
+
+   my $vlan_aware = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/bridge/vlan_filtering");
+
+   if ($vlan_aware) {
+       if ($tag) {
+           system("/sbin/bridge vlan add dev $iface vid $tag pvid untagged") == 0 ||
+           die "unable to add vlan $tag to interface $iface\n";
+       } else {
+           system("/sbin/bridge vlan add dev $iface vid 2-4094") == 0 ||
+           die "unable to add vlan $tag to interface $iface\n";
+       } 
+   }
 };
 
 my $ovs_bridge_add_port = sub {
@@ -171,9 +237,9 @@ sub veth_delete {
 }
 
 my $create_firewall_bridge_linux = sub {
-    my ($iface, $bridge) = @_;
+    my ($iface, $bridge, $tag) = @_;
 
-    my ($vmid, $devid) = &$parse_tap_devive_name($iface);
+    my ($vmid, $devid) = &$parse_tap_device_name($iface);
     my ($fwbr, $vethfw, $vethfwpeer) = &$compute_fwbr_names($vmid, $devid);
 
     &$cond_create_bridge($fwbr);
@@ -183,15 +249,15 @@ my $create_firewall_bridge_linux = sub {
     veth_create($vethfw, $vethfwpeer, $bridge);
 
     &$bridge_add_interface($fwbr, $vethfw);
-    &$bridge_add_interface($bridge, $vethfwpeer);
+    &$bridge_add_interface($bridge, $vethfwpeer, $tag);
 
-    return $fwbr;
+    &$bridge_add_interface($fwbr, $iface);
 };
 
 my $create_firewall_bridge_ovs = sub {
     my ($iface, $bridge, $tag) = @_;
 
-    my ($vmid, $devid) = &$parse_tap_devive_name($iface);
+    my ($vmid, $devid) = &$parse_tap_device_name($iface);
     my ($fwbr, undef, undef, $ovsintport) = &$compute_fwbr_names($vmid, $devid);
 
     my $bridgemtu = &$read_bridge_mtu($bridge);
@@ -213,7 +279,7 @@ my $create_firewall_bridge_ovs = sub {
 my $cleanup_firewall_bridge = sub {
     my ($iface) = @_;
 
-    my ($vmid, $devid) = &$parse_tap_devive_name($iface, 1);
+    my ($vmid, $devid) = &$parse_tap_device_name($iface, 1);
     return if !defined($vmid);  
     my ($fwbr, $vethfw, $vethfwpeer, $ovsintport) = &$compute_fwbr_names($vmid, $devid);
 
@@ -241,12 +307,21 @@ sub tap_plug {
     if (-d "/sys/class/net/$bridge/bridge") {
        &$cleanup_firewall_bridge($iface); # remove stale devices
 
-       my $newbridge = activate_bridge_vlan($bridge, $tag);
-       copy_bridge_config($bridge, $newbridge) if $bridge ne $newbridge;
+       my $vlan_aware = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/bridge/vlan_filtering");
+
+       if (!$vlan_aware) {
+           my $newbridge = activate_bridge_vlan($bridge, $tag);
+           copy_bridge_config($bridge, $newbridge) if $bridge ne $newbridge;
+           $bridge = $newbridge;
+           $tag = undef;
+       }
 
-       $newbridge = &$create_firewall_bridge_linux($iface, $newbridge) if $firewall;
+       if ($firewall) {
+           &$create_firewall_bridge_linux($iface, $bridge, $tag);
+       } else {
+           &$bridge_add_interface($bridge, $iface, $tag);
+       }
 
-       &$bridge_add_interface($newbridge, $iface);
     } else {
        &$cleanup_firewall_bridge($iface); # remove stale devices
 
@@ -301,7 +376,7 @@ sub activate_bridge_vlan_slave {
        
     # create vlan on $iface is not already exist
     if (! -d "/sys/class/net/$ifacevlan") {
-       system("/sbin/vconfig add $iface $tag") == 0 ||
+       system("/sbin/ip link add link $iface name ${iface}.${tag} type vlan id $tag") == 0 ||
            die "can't add vlan tag $tag to interface $iface\n";
     }