X-Git-Url: https://git.proxmox.com/?p=pve-common.git;a=blobdiff_plain;f=src%2FPVE%2FNetwork.pm;h=ab073505710150a5a24bea052e52070117fe99a6;hp=b9d2d37527736f1e4613071a4d436da05a9c0f75;hb=e43faad9ff27f4d252d5604439c7aab1ff1c6251;hpb=32cb7d272f3a86f9e33c75f2096f6595505a831d diff --git a/src/PVE/Network.pm b/src/PVE/Network.pm index b9d2d37..ab07350 100644 --- a/src/PVE/Network.pm +++ b/src/PVE/Network.pm @@ -6,6 +6,16 @@ use PVE::Tools qw(run_command); use PVE::ProcFSTools; use PVE::INotify; use File::Basename; +use IO::Socket::IP; +use POSIX qw(ECONNREFUSED); + +use Net::IP; + +use Socket qw(IPPROTO_IP); + +use constant IFF_UP => 1; +use constant IFNAMSIZ => 16; +use constant SIOCGIFFLAGS => 0x8913; # host network related utility functions @@ -61,27 +71,21 @@ our $ipv4_mask_hash_localnet = { '255.255.255.240' => 28, '255.255.255.248' => 29, '255.255.255.252' => 30, + '255.255.255.254' => 31, + '255.255.255.255' => 32, }; sub setup_tc_rate_limit { my ($iface, $rate, $burst, $debug) = @_; - system("/sbin/tc class del dev $iface parent 1: classid 1:1 >/dev/null 2>&1"); - system("/sbin/tc filter del dev $iface parent ffff: protocol ip prio 50 estimator 1sec 8sec >/dev/null 2>&1"); - system("/sbin/tc qdisc del dev $iface ingress >/dev/null 2>&1"); - system("/sbin/tc qdisc del dev $iface root >/dev/null 2>&1"); + # these are allowed / expected to fail, e.g. when there is no previous rate limit to remove + eval { run_command("/sbin/tc class del dev $iface parent 1: classid 1:1 >/dev/null 2>&1"); }; + eval { run_command("/sbin/tc filter del dev $iface parent ffff: protocol all pref 50 u32 >/dev/null 2>&1"); }; + eval { run_command("/sbin/tc qdisc del dev $iface ingress >/dev/null 2>&1"); }; + eval { run_command("/sbin/tc qdisc del dev $iface root >/dev/null 2>&1"); }; return if !$rate; - run_command("/sbin/tc qdisc add dev $iface handle ffff: ingress"); - - # this does not work wit virtio - don't know why (setting "mtu 64kb" does not help) - #run_command("/sbin/tc filter add dev $iface parent ffff: protocol ip prio 50 u32 match ip src 0.0.0.0/0 police rate ${rate}bps burst ${burst}b drop flowid :1"); - # so we use avrate instead - run_command("/sbin/tc filter add dev $iface parent ffff: " . - "protocol ip prio 50 estimator 1sec 8sec " . - "u32 match ip src 0.0.0.0/0 police avrate ${rate}bps drop flowid :1"); - # tbf does not work for unknown reason #$TC qdisc add dev $DEV root tbf rate $RATE latency 100ms burst $BURST # so we use htb instead @@ -89,6 +93,12 @@ sub setup_tc_rate_limit { run_command("/sbin/tc class add dev $iface parent 1: classid 1:1 " . "htb rate ${rate}bps burst ${burst}b"); + run_command("/sbin/tc qdisc add dev $iface handle ffff: ingress"); + run_command("/sbin/tc filter add dev $iface parent ffff: " . + "prio 50 basic " . + "police rate ${rate}bps burst ${burst}b mtu 64kb " . + "drop flowid :1"); + if ($debug) { print "DEBUG tc settings\n"; system("/sbin/tc qdisc ls dev $iface"); @@ -101,7 +111,7 @@ sub tap_rate_limit { my ($iface, $rate) = @_; my $debug = 0; - $rate = int($rate*1024*1024); + $rate = int($rate*1024*1024) if $rate; my $burst = 1024*1024; setup_tc_rate_limit($iface, $rate, $burst, $debug); @@ -160,7 +170,7 @@ my $cond_create_bridge = sub { }; my $bridge_add_interface = sub { - my ($bridge, $iface, $tag) = @_; + my ($bridge, $iface, $tag, $trunks) = @_; system("/sbin/brctl addif $bridge $iface") == 0 || die "can't add interface 'iface' to bridge '$bridge'\n"; @@ -171,18 +181,33 @@ my $bridge_add_interface = sub { if ($tag) { system("/sbin/bridge vlan add dev $iface vid $tag pvid untagged") == 0 || die "unable to add vlan $tag to interface $iface\n"; + + warn "Caution: Setting VLAN ID 1 on a VLAN aware bridge may be dangerous\n" if $tag == 1; } else { system("/sbin/bridge vlan add dev $iface vid 2-4094") == 0 || - die "unable to add vlan $tag to interface $iface\n"; + die "unable to add default vlan tags to interface $iface\n" if !$trunks; } + + if ($trunks) { + my @trunks_array = split /;/, $trunks; + foreach my $trunk (@trunks_array) { + system("/sbin/bridge vlan add dev $iface vid $trunk") == 0 || + die "unable to add vlan $trunk to interface $iface\n"; + } + } } }; my $ovs_bridge_add_port = sub { - my ($bridge, $iface, $tag, $internal) = @_; + my ($bridge, $iface, $tag, $internal, $trunks) = @_; + + $trunks =~ s/;/,/g if $trunks; my $cmd = "/usr/bin/ovs-vsctl add-port $bridge $iface"; $cmd .= " tag=$tag" if $tag; + $cmd .= " trunks=". join(',', $trunks) if $trunks; + $cmd .= " vlan_mode=native-untagged" if $tag && $trunks; + $cmd .= " -- set Interface $iface type=internal" if $internal; system($cmd) == 0 || die "can't add ovs port '$iface'\n"; @@ -237,7 +262,7 @@ sub veth_delete { } my $create_firewall_bridge_linux = sub { - my ($iface, $bridge, $tag) = @_; + my ($iface, $bridge, $tag, $trunks) = @_; my ($vmid, $devid) = &$parse_tap_device_name($iface); my ($fwbr, $vethfw, $vethfwpeer) = &$compute_fwbr_names($vmid, $devid); @@ -249,13 +274,13 @@ my $create_firewall_bridge_linux = sub { veth_create($vethfw, $vethfwpeer, $bridge); &$bridge_add_interface($fwbr, $vethfw); - &$bridge_add_interface($bridge, $vethfwpeer, $tag); + &$bridge_add_interface($bridge, $vethfwpeer, $tag, $trunks); &$bridge_add_interface($fwbr, $iface); }; my $create_firewall_bridge_ovs = sub { - my ($iface, $bridge, $tag) = @_; + my ($iface, $bridge, $tag, $trunks) = @_; my ($vmid, $devid) = &$parse_tap_device_name($iface); my ($fwbr, undef, undef, $ovsintport) = &$compute_fwbr_names($vmid, $devid); @@ -267,7 +292,7 @@ my $create_firewall_bridge_ovs = sub { &$bridge_add_interface($fwbr, $iface); - &$ovs_bridge_add_port($bridge, $ovsintport, $tag, 1); + &$ovs_bridge_add_port($bridge, $ovsintport, $tag, 1, $trunks); &$activate_interface($ovsintport); # set the same mtu for ovs int port @@ -299,7 +324,7 @@ my $cleanup_firewall_bridge = sub { }; sub tap_plug { - my ($iface, $bridge, $tag, $firewall) = @_; + my ($iface, $bridge, $tag, $firewall, $trunks, $rate) = @_; #cleanup old port config from any openvswitch bridge eval {run_command("/usr/bin/ovs-vsctl del-port $iface", outfunc => sub {}, errfunc => sub {}) }; @@ -310,6 +335,7 @@ sub tap_plug { my $vlan_aware = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/bridge/vlan_filtering"); if (!$vlan_aware) { + die "vlan aware feature need to be enabled to use trunks" if $trunks; my $newbridge = activate_bridge_vlan($bridge, $tag); copy_bridge_config($bridge, $newbridge) if $bridge ne $newbridge; $bridge = $newbridge; @@ -317,20 +343,22 @@ sub tap_plug { } if ($firewall) { - &$create_firewall_bridge_linux($iface, $bridge, $tag); + &$create_firewall_bridge_linux($iface, $bridge, $tag, $trunks); } else { - &$bridge_add_interface($bridge, $iface, $tag); + &$bridge_add_interface($bridge, $iface, $tag, $trunks); } } else { &$cleanup_firewall_bridge($iface); # remove stale devices if ($firewall) { - &$create_firewall_bridge_ovs($iface, $bridge, $tag); + &$create_firewall_bridge_ovs($iface, $bridge, $tag, $trunks); } else { - &$ovs_bridge_add_port($bridge, $iface, $tag); + &$ovs_bridge_add_port($bridge, $iface, $tag, undef, $trunks); } } + + tap_rate_limit($iface, $rate); } sub tap_unplug { @@ -348,6 +376,8 @@ sub tap_unplug { } &$cleanup_firewall_bridge($iface); + #cleanup old port config from any openvswitch bridge + eval {run_command("/usr/bin/ovs-vsctl del-port $iface", outfunc => sub {}, errfunc => sub {}) }; } sub copy_bridge_config { @@ -414,7 +444,7 @@ sub activate_bridge_vlan { my @ifaces = (); my $dir = "/sys/class/net/$bridge/brif"; - PVE::Tools::dir_glob_foreach($dir, '((eth|bond)\d+)', sub { + PVE::Tools::dir_glob_foreach($dir, '(((eth|bond)\d+|en[^.]+)(\.\d+)?)', sub { push @ifaces, $_[0]; }); @@ -440,4 +470,95 @@ sub activate_bridge_vlan { return $bridgevlan; } +sub tcp_ping { + my ($host, $port, $timeout) = @_; + + my $refused = 1; + + $timeout = 3 if !$timeout; # sane default + if (!$port) { + # Net::Ping defaults to the echo port + $port = 7; + } else { + # Net::Ping's port_number() implies service_check(1) + $refused = 0; + } + + my ($sock, $result); + eval { + $result = PVE::Tools::run_with_timeout($timeout, sub { + $sock = IO::Socket::IP->new(PeerHost => $host, PeerPort => $port, Type => SOCK_STREAM); + $result = $refused if $! == ECONNREFUSED; + }); + }; + if ($sock) { + $sock->close(); + $result = 1; + } + return $result; +} + +sub IP_from_cidr { + my ($cidr, $version) = @_; + + return if $cidr !~ m!^(\S+?)/(\S+)$!; + my ($ip, $prefix) = ($1, $2); + + my $ipobj = Net::IP->new($ip, $version); + return if !$ipobj; + + $version = $ipobj->version(); + + my $binmask = Net::IP::ip_get_mask($prefix, $version); + return if !$binmask; + + my $masked_binip = $ipobj->binip() & $binmask; + my $masked_ip = Net::IP::ip_bintoip($masked_binip, $version); + return Net::IP->new("$masked_ip/$prefix"); +} + +sub is_ip_in_cidr { + my ($ip, $cidr, $version) = @_; + + my $cidr_obj = IP_from_cidr($cidr, $version); + return undef if !$cidr_obj; + + my $ip_obj = Net::IP->new($ip, $version); + return undef if !$ip_obj; + + return $cidr_obj->overlaps($ip_obj) == $Net::IP::IP_B_IN_A_OVERLAP; +} + +# struct ifreq { // FOR SIOCGIFFLAGS: +# char ifrn_name[IFNAMSIZ] +# short ifru_flags +# }; +my $STRUCT_IFREQ_SIOCGIFFLAGS = 'Z' . IFNAMSIZ . 's1'; +sub get_active_interfaces { + # Use the interface name list from /proc/net/dev + open my $fh, '<', '/proc/net/dev' + or die "failed to open /proc/net/dev: $!\n"; + # And filter by IFF_UP flag fetched via a PF_INET6 socket ioctl: + my $sock; + socket($sock, PF_INET6, SOCK_DGRAM, &IPPROTO_IP) + or socket($sock, PF_INET, SOCK_DGRAM, &IPPROTO_IP) + or return []; + + my $ifaces = []; + while(defined(my $line = <$fh>)) { + next if $line !~ /^\s*([^:\s]+):/; + my $ifname = $1; + my $ifreq = pack($STRUCT_IFREQ_SIOCGIFFLAGS, $ifname, 0); + if (!defined(ioctl($sock, SIOCGIFFLAGS, $ifreq))) { + warn "failed to get interface flags for: $ifname\n"; + next; + } + my ($name, $flags) = unpack($STRUCT_IFREQ_SIOCGIFFLAGS, $ifreq); + push @$ifaces, $ifname if ($flags & IFF_UP); + } + close $fh; + close $sock; + return $ifaces; +} + 1;