[pve-devel] [PATCH] add vlan for testing

Derumier Alexandre aderumier at odiso.com
Mon Mar 19 09:39:21 CET 2012


Signed-off-by: Derumier Alexandre <aderumier at odiso.com>
---
 PVE/QemuServer.pm |    5 ++++-
 pve-bridge        |   53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+), 1 deletions(-)

diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 3991606..b5103e7 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -404,7 +404,7 @@ my $nic_model_list_txt = join(' ', sort @$nic_model_list);
 my $netdesc = {
     optional => 1,
     type => 'string', format => 'pve-qm-net',
-    typetext => "MODEL=XX:XX:XX:XX:XX:XX [,bridge=<dev>][,rate=<mbps>]",
+    typetext => "MODEL=XX:XX:XX:XX:XX:XX [,bridge=<dev>][,rate=<mbps>][,vlan=<vlanid>]",
     description => <<EODESCR,
 Specify network devices.
 
@@ -1052,6 +1052,8 @@ sub parse_net {
 	    $res->{bridge} = $1;
 	} elsif ($kvp =~ m/^rate=(\d+(\.\d+)?)$/) {
 	    $res->{rate} = $1;
+        } elsif ($kvp =~ m/^vlan=(\d+)$/) {
+            $res->{vlan} = $1;
 	} else {
 	    return undef;
 	}
@@ -1070,6 +1072,7 @@ sub print_net {
     $res .= "=$net->{macaddr}" if $net->{macaddr};
     $res .= ",bridge=$net->{bridge}" if $net->{bridge};
     $res .= ",rate=$net->{rate}" if $net->{rate};
+    $res .= ",vlan=$net->{vlan}" if $net->{vlan};
 
     return $res;
 }
diff --git a/pve-bridge b/pve-bridge
index 2d826cf..faf50fa 100755
--- a/pve-bridge
+++ b/pve-bridge
@@ -3,6 +3,7 @@
 use strict;
 use PVE::QemuServer;
 use PVE::Tools qw(run_command);
+use PVE::Storage;
 
 my $iface = shift;
 
@@ -62,6 +63,58 @@ if ($net->{rate}) {
 
 }
 
+if ($net->{vlan}) {
+
+    my $vlan = $net->{vlan};
+    my $bridgevlan = $bridge."v".$vlan;
+
+    #check if we have an only one ethX or bondX interface in the bridge
+    my (undef, $interface) = PVE::Storage::dir_glob_regex("/sys/class/net/$bridge/brif/", '((eth|bond)\d+)');
+    die "No interface in bridge" if !$interface;
+    die "You can't have 2 physical interfaces on the same bridge" if(ref($interface) eq 'ARRAY');
+
+    my $interfacevlan = $interface.".".$vlan;
+
+    #create vlan on interface is not already exist
+    unless (-d "/sys/class/net/$interfacevlan") {
+    system ("/sbin/vconfig add $interface $vlan") == 0 ||
+        die "can't add vlan $vlan to interface $interface\n";
+    }
+
+    #be sure to have the interfacevlan up
+    system ("/sbin/ip link set $interfacevlan up") == 0 ||
+        die "can't up interface $interfacevlan\n";
+
+    # test if vlaninterface is already enslave in another bridge
+    my $path="/sys/class/net/$interfacevlan/brport/bridge";
+    if(-e $path) {
+        my $targetbridge = readlink($path);
+        if($targetbridge =~ m/([0-9A-Za-z]+)$/) {
+                die "Interface $interfacevlan already exist in bridge $targetbridge" if $1 ne $bridgevlan;
+        }
+    }
+
+    #add bridgevlan if it doesn't already exist
+    unless (-d "/sys/class/net/$bridgevlan") {
+        system ("/usr/sbin/brctl addbr $bridgevlan") == 0 ||
+            die "can't add bridge $bridgevlan\n";
+    }
+
+    #be sure to have the bridge up
+    system ("/sbin/ip link set $bridgevlan up") == 0 ||
+        die "can't up bridge $bridgevlan\n";
+
+    #add interfacevlan to the bridge
+    my (undef, $interfacevlanfound) = PVE::Storage::dir_glob_regex("/sys/class/net/$bridgevlan/brif/", "($interface\.$vlan)");
+    if (!defined($interfacevlanfound)) {
+     system ("/usr/sbin/brctl addif $bridgevlan $interfacevlan") == 0 ||
+     die "can't add interface $interfacevlan to bridge $bridgevlan\n";
+    }
+
+    $bridge=$bridgevlan;
+}
+
+
 system ("/usr/sbin/brctl addif $bridge $iface") == 0 ||
     die "can't add interface to bridge\n";
 
-- 
1.7.2.5




More information about the pve-devel mailing list