X-Git-Url: https://git.proxmox.com/?p=pve-common.git;a=blobdiff_plain;f=data%2FPVE%2FNetwork.pm;h=4677bf94009d1fe79eaba999eaf5d30ce4f61b6f;hp=1fa1dc799dafce37c70173400175d3959d36fa12;hb=5ffa7628c5c099b555a337d679c6894c5c7afefd;hpb=9e14b1b78aaf7bd79daa0ef13d736753483708ec diff --git a/data/PVE/Network.pm b/data/PVE/Network.pm index 1fa1dc7..4677bf9 100644 --- a/data/PVE/Network.pm +++ b/data/PVE/Network.pm @@ -1,6 +1,7 @@ package PVE::Network; use strict; +use warnings; use PVE::Tools qw(run_command); use PVE::ProcFSTools; use PVE::INotify; @@ -60,27 +61,44 @@ sub tap_create { 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 $iface 0.0.0.0 promisc up mtu $bridgemtu");}; - die "interface activation failed\n" if $@; + eval { + PVE::Tools::run_command("/sbin/ifconfig $iface 0.0.0.0 promisc up mtu $bridgemtu"); + }; + die "interface activation failed\n" if $@; } sub tap_plug { my ($iface, $bridge, $tag) = @_; - my $newbridge = activate_bridge_vlan($bridge, $tag); - copy_bridge_config($bridge, $newbridge) if $bridge ne $newbridge; + #cleanup old port config from any openvswitch bridge + eval {run_command("/usr/bin/ovs-vsctl del-port $iface", outfunc => sub {}, errfunc => sub {}) }; - system ("/sbin/brctl addif $newbridge $iface") == 0 || - die "can't add interface to bridge\n"; + 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 || + 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"; + } } sub tap_unplug { my ($iface, $bridge, $tag) = @_; - $bridge .= "v$tag" if $tag; + if (-d "/sys/class/net/$bridge/bridge") { + $bridge .= "v$tag" if $tag; - system ("/sbin/brctl delif $bridge $iface") == 0 || - die "can't del interface from bridge\n"; + system("/sbin/brctl delif $bridge $iface") == 0 || + die "can't del interface from bridge\n"; + } else { + system ("/usr/bin/ovs-vsctl del-port $iface") == 0 || + die "can't del interface from bridge\n"; + } } sub copy_bridge_config { @@ -96,44 +114,17 @@ sub copy_bridge_config { my $v0 = PVE::Tools::file_read_firstline("/sys/class/net/$br0/bridge/$sysname"); my $v1 = PVE::Tools::file_read_firstline("/sys/class/net/$br1/bridge/$sysname"); if ($v0 ne $v1) { - system("echo \"$v0\" > /sys/class/net/$br1/bridge/$sysname") == 0 || - warn "unable to set bridge config '$sysname'\n"; + PVE::ProcFSTools::write_proc_entry("/sys/class/net/$br1/bridge/$sysname", $v0); } }; warn $@ if $@; } } -sub activate_bridge_vlan { - 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"; - - #check if we have an only one ethX or bondX interface in the bridge - - my $iface; - PVE::Tools::dir_glob_foreach($dir, '((eth|bond)\d+)', sub { - my ($slave) = @_; - - die "more then one physical interfaces on bridge '$bridge'\n" if $iface; - $iface = $slave; - - }); - - die "no physical interface on bridge '$bridge'\n" if !$iface; - +sub activate_bridge_vlan_slave { + my ($bridgevlan, $iface, $tag) = @_; 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 || @@ -148,30 +139,57 @@ sub activate_bridge_vlan { 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 { + if ($tbridge ne $bridgevlan) { die "interface $ifacevlan already exist in bridge $tbridge\n"; + } else { + # Port already attached to bridge: do nothing. + return; } } + # add $ifacevlan to the bridge + system("/sbin/brctl addif $bridgevlan $ifacevlan") == 0 || + die "can't add interface $ifacevlan to bridge $bridgevlan\n"; +} + +sub activate_bridge_vlan { + 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 @ifaces = (); + my $dir = "/sys/class/net/$bridge/brif"; + PVE::Tools::dir_glob_foreach($dir, '((eth|bond)\d+)', sub { + push @ifaces, $_[0]; + }); + + die "no physical interface on bridge '$bridge'\n" if scalar(@ifaces) == 0; + # 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"; } + # for each physical interface (eth or bridge) bind them to bridge vlan + foreach my $iface (@ifaces) { + activate_bridge_vlan_slave($bridgevlan, $iface, $tag); + } + #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; }