X-Git-Url: https://git.proxmox.com/?p=pve-common.git;a=blobdiff_plain;f=data%2FPVE%2FNetwork.pm;h=dffa764f552a7b527dcbec8e08dfd0629174d4f1;hp=73984c2ad9c1b142be604cd13355fd3036a3f993;hb=47c710a624f7904efbb1ab04c57f3a3196d4421a;hpb=70d89745ca6da381df4b99dedaf909476b16eec7 diff --git a/data/PVE/Network.pm b/data/PVE/Network.pm index 73984c2..dffa764 100644 --- a/data/PVE/Network.pm +++ b/data/PVE/Network.pm @@ -53,13 +53,86 @@ sub tap_rate_limit { setup_tc_rate_limit($iface, $rate, $burst, $debug); } +my $read_bridge_mtu = sub { + my ($bridge) = @_; + + my $mtu = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/mtu"); + die "bridge '$bridge' does not exist\n" if !$mtu; + # avoid insecure dependency; + die "unable to parse mtu value" if $mtu !~ /^(\d+)$/; + $mtu = int($1); + + return $mtu; +}; + +my $parse_tap_devive_name = sub { + my ($iface) = @_; + + my ($vmid, $devid); + + if ($iface =~ m/^tap(\d+)i(\d+)$/) { + $vmid = $1; + $devid = $2; + } elsif ($iface =~ m/^veth(\d+)\.(\d+)$/) { + $vmid = $1; + $devid = $2; + } else { + die "wrong interface name $iface"; + } + + return ($vmid, $devid); +}; + +my $compute_fwbr_names = sub { + my ($vmid, $devid) = @_; + + my $fwbr = "fwbr${vmid}i${devid}"; + my $vethfw = "link${vmid}i${devid}"; + my $vethfwpeer = "link${vmid}p${devid}"; + my $ovsintport = "link${vmid}o${devid}"; + + return ($fwbr, $vethfw, $vethfwpeer, $ovsintport); +}; + +my $cond_create_bridge = sub { + my ($bridge) = @_; + + if (! -d "/sys/class/net/$bridge") { + system("/sbin/brctl addbr $bridge") == 0 || + die "can't add bridge '$bridge'\n"; + } +}; + +my $bridge_add_interface = sub { + my ($bridge, $iface) = @_; + + system("/sbin/brctl addif $bridge $iface") == 0 || + die "can't add interface 'iface' to bridge '$bridge'\n"; +}; + +my $ovs_bridge_add_port = sub { + my ($bridge, $iface, $tag, $internal) = @_; + + my $cmd = "/usr/bin/ovs-vsctl add-port $bridge $iface"; + $cmd .= " tag=$tag" if $tag; + $cmd .= " -- set Interface $iface type=internal" if $internal; + system($cmd) == 0 || + die "can't add ovs port '$iface'\n"; +}; + +my $activate_interface = sub { + my ($iface) = @_; + + system("/sbin/ip link set $iface up") == 0 || + die "can't activate interface '$iface'\n"; +}; + sub tap_create { my ($iface, $bridge) = @_; die "unable to get bridge setting\n" if !$bridge; - my $bridgemtu = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/mtu"); - die "bridge '$bridge' does not exist\n" if !$bridgemtu; + my $bridgemtu = &$read_bridge_mtu($bridge); eval { PVE::Tools::run_command("/sbin/ifconfig $iface 0.0.0.0 promisc up mtu $bridgemtu"); @@ -67,38 +140,119 @@ sub tap_create { die "interface activation failed\n" if $@; } -sub tap_plug { +my $create_firewall_bridge_linux = sub { + my ($iface, $bridge) = @_; + + my ($vmid, $devid) = &$parse_tap_devive_name($iface); + my ($fwbr, $vethfw, $vethfwpeer) = &$compute_fwbr_names($vmid, $devid); + + my $bridgemtu = &$read_bridge_mtu($bridge); + + &$cond_create_bridge($fwbr); + &$activate_interface($fwbr); + + copy_bridge_config($bridge, $fwbr); + # create veth pair + if (! -d "/sys/class/net/$vethfw") { + system("/sbin/ip link add name $vethfw type veth peer name $vethfwpeer mtu $bridgemtu") == 0 || + die "can't create interface $vethfw\n"; + } + + # up vethpair + &$activate_interface($vethfw); + &$activate_interface($vethfwpeer); + + &$bridge_add_interface($bridge, $vethfw); + &$bridge_add_interface($fwbr, $vethfwpeer); + + return $fwbr; +}; + +my $create_firewall_bridge_ovs = sub { my ($iface, $bridge, $tag) = @_; + my ($vmid, $devid) = &$parse_tap_devive_name($iface); + my ($fwbr, undef, undef, $ovsintport) = &$compute_fwbr_names($vmid, $devid); + + my $bridgemtu = &$read_bridge_mtu($bridge); + + &$cond_create_bridge($fwbr); + &$activate_interface($fwbr); + + &$bridge_add_interface($fwbr, $iface); + + &$ovs_bridge_add_port($bridge, $ovsintport, $tag, 1); + + # set the same mtu for ovs int port + PVE::Tools::run_command("/sbin/ifconfig $ovsintport mtu $bridgemtu"); + + &$bridge_add_interface($fwbr, $ovsintport); +}; + +my $cleanup_firewall_bridge = sub { + my ($iface) = @_; + + my ($vmid, $devid) = &$parse_tap_devive_name($iface); + my ($fwbr, $vethfw, $vethfwpeer, $ovsintport) = &$compute_fwbr_names($vmid, $devid); + + # cleanup old port config from any openvswitch bridge + if (-d "/sys/class/net/$ovsintport") { + run_command("/usr/bin/ovs-vsctl del-port $ovsintport", outfunc => sub {}, errfunc => sub {}); + } + + # delete old vethfw interface + if (-d "/sys/class/net/$vethfw") { + run_command("/sbin/ip link delete dev $vethfw", outfunc => sub {}, errfunc => sub {}); + } + + # cleanup fwbr bridge + if (-d "/sys/class/net/$fwbr") { + run_command("/sbin/ip link set dev $fwbr down", outfunc => sub {}, errfunc => sub {}); + run_command("/sbin/brctl delbr $fwbr", outfunc => sub {}, errfunc => sub {}); + } +}; + +sub tap_plug { + my ($iface, $bridge, $tag, $firewall) = @_; + #cleanup old port config from any openvswitch bridge eval {run_command("/usr/bin/ovs-vsctl del-port $iface", outfunc => sub {}, errfunc => sub {}) }; 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; - system("/sbin/brctl addif $newbridge $iface") == 0 || - die "can't add interface to bridge\n"; + $newbridge = &$create_firewall_bridge_linux($iface, $newbridge) if $firewall; + + &$bridge_add_interface($newbridge, $iface); } 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"; + &$cleanup_firewall_bridge($iface); # remove stale devices + + if ($firewall) { + &$create_firewall_bridge_ovs($iface, $bridge, $tag); + } else { + &$ovs_bridge_add_port($bridge, $iface, $tag); + } } } sub tap_unplug { - my ($iface, $bridge, $tag) = @_; + my ($iface) = @_; - if (-d "/sys/class/net/$bridge/bridge") { - $bridge .= "v$tag" if $tag; + my $path= "/sys/class/net/$iface/brport/bridge"; + if (-l $path) { + my $bridge = basename(readlink($path)); + #avoid insecure dependency; + ($bridge) = $bridge =~ /(\S+)/; 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"; + die "can't del interface '$iface' from bridge '$bridge'\n"; + } + + &$cleanup_firewall_bridge($iface); } sub copy_bridge_config { @@ -132,8 +286,7 @@ sub activate_bridge_vlan_slave { } # be sure to have the $ifacevlan up - system("/sbin/ip link set $ifacevlan up") == 0 || - die "can't up interface $ifacevlan\n"; + &$activate_interface($ifacevlan); # test if $vlaniface is already enslaved in another bridge my $path= "/sys/class/net/$ifacevlan/brport/bridge"; @@ -141,12 +294,14 @@ sub activate_bridge_vlan_slave { my $tbridge = basename(readlink($path)); 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"; + &$bridge_add_interface($bridgevlan, $ifacevlan); } sub activate_bridge_vlan { @@ -162,6 +317,14 @@ sub activate_bridge_vlan { 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 || @@ -169,15 +332,9 @@ sub activate_bridge_vlan { } # for each physical interface (eth or bridge) bind them to bridge vlan - my $ifcount = 0; - my $dir = "/sys/class/net/$bridge/brif"; - PVE::Tools::dir_glob_foreach($dir, '((eth|bond)\d+)', sub { - my ($slave) = @_; - activate_bridge_vlan_slave($bridgevlan, $slave, $tag); - $ifcount++; - }); - - die "no physical interface on bridge '$bridge'\n" if $ifcount == 0; + foreach my $iface (@ifaces) { + activate_bridge_vlan_slave($bridgevlan, $iface, $tag); + } #fixme: set other bridge flags