[pve-devel] [PATCH 2/3] add firewall bridge support for openvswitch

Alexandre Derumier aderumier at odiso.com
Tue May 6 10:17:29 CEST 2014


eth0-->vmbr0--vlan--ovsintXiY-->fwbr---->tapXiY (firewalled tap)

            --vlan---->tapXiY (non firewall tap)

Signed-off-by: Alexandre Derumier <aderumier at odiso.com>
---
 data/PVE/Network.pm |  109 +++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 102 insertions(+), 7 deletions(-)

diff --git a/data/PVE/Network.pm b/data/PVE/Network.pm
index fa56c8b..64be5d2 100644
--- a/data/PVE/Network.pm
+++ b/data/PVE/Network.pm
@@ -6,6 +6,8 @@ use PVE::Tools qw(run_command);
 use PVE::ProcFSTools;
 use PVE::INotify;
 use File::Basename;
+use PVE::Firewall;
+use JSON;
 
 # host network related utility functions
 
@@ -68,10 +70,29 @@ sub tap_create {
 }
 
 sub tap_plug {
-    my ($iface, $bridge, $tag) = @_;
+    my ($iface, $bridge, $tag, $tapfirewall) = @_;
 
-    #cleanup old port config from any openvswitch bridge
-    eval {run_command("/usr/bin/ovs-vsctl del-port $iface", outfunc => sub {}, errfunc => sub {}) };
+    my $iface_suffix = undef;
+    my $vmid = undef;
+
+    if ($iface =~ m/^tap((\d+)i(\d+))$/){
+	$iface_suffix = $1;
+	$vmid = $2;
+    }elsif ($iface =~ m/^veth((\d+)\.(\d+))$/){
+	$iface_suffix = $1;
+	$vmid = $2;
+    }else{
+	die "wrong interface name $iface";
+    }
+
+    my $vmfw_conf = PVE::Firewall::load_vmfw_conf($vmid);
+    my $fwenable = $vmfw_conf->{options}->{enable};
+
+    $fwenable = $tapfirewall if $fwenable;
+
+    my $ovsintport = "ovsint$iface_suffix";
+
+    bridge_cleanup($iface);
 
     if (-d "/sys/class/net/$bridge/bridge") {
 	my $newbridge = activate_bridge_vlan($bridge, $tag);
@@ -80,10 +101,20 @@ sub tap_plug {
 	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";
-	$cmd .= " tag=$tag" if $tag;
-	system($cmd) == 0 ||
-	    die "can't add interface to bridge\n";
+    
+	my $bridge_hash = {};
+
+	eval { $bridge_hash = read_openvswitch_config() };
+
+	die "$bridge is not an linux bridge or openvswitch switch" if !$bridge_hash->{$bridge};
+	if(!$fwenable){	
+	    my $cmd = "/usr/bin/ovs-vsctl add-port $bridge $iface";
+	    $cmd .= " tag=$tag" if $tag;
+	    system($cmd) == 0 ||
+		die "can't add interface $iface to bridge $bridge\n";
+	}else{
+	    ovs_firewall_tap_plug($iface, $bridge, $tag); 		
+        }
     }
 }
 
@@ -234,4 +265,68 @@ sub bridge_cleanup {
 	run_command("/sbin/brctl delbr $bridgetap", outfunc => sub {}, errfunc => sub {});
     }
 }
+
+
+sub read_openvswitch_config {
+
+    my $filename = '/etc/openvswitch/conf.db';
+    my $config = file_get_contents($filename, 5*1024*1024);
+    my @lines = split('\n', $config);
+    my $bridge_hash = {};
+    foreach my $line (@lines) {
+	my $json = {};
+	eval { $json = decode_json($line)};
+	foreach my $bridgekey (keys %{$json->{Bridge}}) {
+	    my $bridgename = $json->{Bridge}->{$bridgekey}->{name};
+	    $bridge_hash->{$bridgename} = 1 if $bridgename;
+        }
+    }
+
+    return $bridge_hash;
+
+}
+
+sub ovs_firewall_tap_plug {
+    my ($iface, $bridge, $tag) = @_;
+
+    my $iface_suffix = $1 if $iface =~ m/^tap((\d+)i(\d+))$/;
+    die "wrong interface name $iface" if !$iface_suffix;
+
+    my $ovsintport = "ovsint$iface_suffix";
+    my $bridgetap = "fwbr$iface_suffix";
+
+    my $cmd = "/usr/bin/ovs-vsctl add-port $bridge $ovsintport";
+    $cmd .= " tag=$tag" if $tag;
+    $cmd .= " -- set Interface $ovsintport type=internal";
+    system($cmd) == 0 ||
+	die "$cmd : can't create ovs intport $ovsintport\n";
+
+    # set the same mtu for ovs int port
+    my $bridgemtu = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/mtu");
+	die "bridge '$bridge' does not exist\n" if !$bridgemtu;
+
+    eval { 
+	PVE::Tools::run_command("/sbin/ifconfig $ovsintport mtu $bridgemtu");
+    };
+
+    # add bridgetap if it doesn't already exist
+    if (! -d "/sys/class/net/$bridgetap") {
+	system("/sbin/brctl addbr $bridgetap") == 0 ||
+	    die "can't add bridge $bridgetap\n";
+    }
+
+    # be sure to have the bridgetap up
+    system("/sbin/ip link set $bridgetap up") == 0 ||
+	die "can't up bridge $bridgetap\n";
+
+    # add ovsintport to bridgetap
+    system("/sbin/brctl addif $bridgetap $ovsintport") == 0 ||
+	die "can't add interface $ovsintport to bridge $bridgetap\n";
+
+    # add vm tap interface to bridgetap
+    system("/sbin/brctl addif $bridgetap $iface") == 0 ||
+	die "can't add interface $iface to bridge $bridgetap\n";
+
+}
+
 1;
-- 
1.7.10.4




More information about the pve-devel mailing list