[pve-devel] [PATCH] Virtual vlan tagging to bridge interface

Johannes Ernst info at filemedia.de
Wed Jan 8 11:19:41 CET 2014


Signed-off-by: Johannes Ernst <info at filemedia.de>
---
 data/PVE/Network.pm | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 78 insertions(+), 4 deletions(-)

diff --git a/data/PVE/Network.pm b/data/PVE/Network.pm
index 9ad34f1..7eeb0d2 100644
--- a/data/PVE/Network.pm
+++ b/data/PVE/Network.pm
@@ -6,6 +6,7 @@ use PVE::Tools qw(run_command);
 use PVE::ProcFSTools;
 use PVE::INotify;
 use File::Basename;
+use PVE::Cluster;
 
 # host network related utility functions
 
@@ -74,10 +75,20 @@ sub tap_plug {
     eval {run_command("/usr/bin/ovs-vsctl del-port $iface", outfunc => sub {}, errfunc => sub {}) };
 
     if (-d "/sys/class/net/$bridge/bridge") {
-	my $newbridge = activate_bridge_vlan($bridge, $tag);
-	copy_bridge_config($bridge, $newbridge) if $bridge ne $newbridge;
-
-	system("/sbin/brctl addif $newbridge $iface") == 0 ||
+		
+		# get config for virtual vmbr tag
+		my $datacenterconf = PVE::Cluster::cfs_read_file('datacenter.cfg');
+
+		#check if virtual vlan tag
+		if ($datacenterconf->{virtual_tagging}) {
+	    	my $newbridge = activate_bridge_vlan_virtual($bridge, $tag);
+		} else {
+			my $newbridge = activate_bridge_vlan($bridge, $tag);
+		}
+		
+		copy_bridge_config($bridge, $newbridge) if $bridge ne $newbridge;
+
+		system("/sbin/brctl addif $newbridge $iface") == 0 ||
 	    die "can't add interface to bridge\n";
     } else {
 	my $cmd = "/usr/bin/ovs-vsctl add-port $bridge $iface";
@@ -193,4 +204,67 @@ sub activate_bridge_vlan {
     return $bridgevlan;
 }
 
+sub activate_bridge_vlan_virtual {
+    my ($bridge, $tag_param) = @_;
+
+    die "bridge '$bridge' is not active\n" if ! -d "/sys/class/net/$bridge";
+
+    return $bridge if !defined($tag_param); # no vlan, simply return
+
+    my $tag = int($tag_param);
+
+    die "got strange vlan tag '$tag_param'\n" if $tag < 1 || $tag > 4094;
+
+    my $bridgevlan = "${bridge}v$tag";
+
+    my $dir = "/sys/class/net/$bridge/brif";
+	
+	#set the bridge as interface
+	my $iface = "$bridge";
+	
+	#vlan construct: bridge -> bridge.tag -> bridgevtag -> VM
+	
+    my $ifacevlan = "${iface}.$tag";
+
+    # create vlan on $iface is not already exist
+    if (! -d "/sys/class/net/$ifacevlan") {
+	system("/sbin/vconfig add $iface $tag") == 0 ||
+	    die "can't add vlan tag $tag to interface $iface\n";
+    }
+
+    # be sure to have the $ifacevlan up
+    system("/sbin/ip link set $ifacevlan up") == 0 ||
+        die "can't up interface $ifacevlan\n";
+
+    # test if $vlaniface is already enslaved in another bridge
+    my $path= "/sys/class/net/$ifacevlan/brport/bridge";
+    if (-l $path) {
+        my $tbridge = basename(readlink($path));
+	if ($tbridge eq $bridgevlan) {
+	    # already member of bridge - assume setup is already done
+	    return $bridgevlan;
+	} else {
+	    die "interface $ifacevlan already exist in bridge $tbridge\n";
+	}
+    }
+
+    # add bridgevlan if it doesn't already exist
+    if (! -d "/sys/class/net/$bridgevlan") {
+        system("/sbin/brctl addbr $bridgevlan") == 0 ||
+            die "can't add bridge $bridgevlan\n";
+    }
+
+    #fixme: set other bridge flags
+
+    # be sure to have the bridge up
+    system("/sbin/ip link set $bridgevlan up") == 0 ||
+        die "can't up bridge $bridgevlan\n";
+
+    # add $ifacevlan to the bridge
+    system("/sbin/brctl addif $bridgevlan $ifacevlan") == 0 ||
+	die "can't add interface $ifacevlan to bridge $bridgevlan\n";
+    
+    return $bridgevlan;
+}
+
 1;
-- 
1.8.3.4 (Apple Git-47)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.proxmox.com/pipermail/pve-devel/attachments/20140108/33bc52f0/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 495 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <http://lists.proxmox.com/pipermail/pve-devel/attachments/20140108/33bc52f0/attachment.sig>


More information about the pve-devel mailing list