]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/Network.pm
cgroup: cpu quota: fix resetting period length for v1
[pve-common.git] / src / PVE / Network.pm
index 98a58fa98ee683ff2148a50cd24f0e9be44cbd25..d4d72d41380a6227c78af4813124fb8f61a46bfc 100644 (file)
@@ -9,13 +9,15 @@ use PVE::Tools qw(run_command lock_file);
 
 use File::Basename;
 use IO::Socket::IP;
+use JSON;
 use Net::IP;
+use NetAddr::IP qw(:lower);
 use POSIX qw(ECONNREFUSED);
 use Socket qw(NI_NUMERICHOST NI_NUMERICSERV);
 
 # host network related utility functions
 
-our $PHYSICAL_NIC_RE = qr/(?:eth\d+|en[^:.]+|ib\d+)/;
+our $PHYSICAL_NIC_RE = qr/(?:eth\d+|en[^:.]+|ib[^:.]+)/;
 
 our $ipv4_reverse_mask = [
     '0.0.0.0',
@@ -82,7 +84,7 @@ our $ipv4_mask_hash_localnet = {
 };
 
 sub setup_tc_rate_limit {
-    my ($iface, $rate, $burst, $debug) = @_;
+    my ($iface, $rate, $burst) = @_;
 
     # 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"); };
@@ -104,26 +106,18 @@ sub setup_tc_rate_limit {
                "prio 50 basic " .
                "police rate ${rate}bps burst ${burst}b mtu 64kb " .
                "drop");
-
-    if ($debug) {
-       print "DEBUG tc settings\n";
-       system("/sbin/tc qdisc ls dev $iface");
-       system("/sbin/tc class ls dev $iface");
-       system("/sbin/tc filter ls dev $iface parent ffff:");
-    }
 }
 
 sub tap_rate_limit {
     my ($iface, $rate) = @_;
 
-    my $debug = 0;
     $rate = int($rate*1024*1024) if $rate;
     my $burst = 1024*1024;
 
-    setup_tc_rate_limit($iface, $rate, $burst, $debug);
+    setup_tc_rate_limit($iface, $rate, $burst);
 }
 
-my $read_bridge_mtu = sub {
+sub read_bridge_mtu {
     my ($bridge) = @_;
 
     my $mtu = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/mtu");
@@ -224,25 +218,24 @@ my $bridge_add_interface = sub {
    my $vlan_aware = PVE::Tools::file_read_firstline("/sys/class/net/$bridge/bridge/vlan_filtering");
 
    if ($vlan_aware) {
-       if ($tag) {
-           system({'/sbin/bridge'} 'bridge', 'vlan', 'del', 'dev', $iface, 'vid', '1-4094') == 0
-               or die "failed to remove default vlan tags of $iface\n";
-           system({'/sbin/bridge'} 'bridge', 'vlan', 'add', 'dev', $iface, 'vid', $tag, 'pvid', 'untagged') == 0
-               or 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 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";
-           }
-       }
+        eval { run_command(['/sbin/bridge', 'vlan', 'del', 'dev', $iface, 'vid', '1-4094']) };
+        die "failed to remove default vlan tags of $iface - $@\n" if $@;
+
+        if ($trunks) {
+            my @trunks_array = split /;/, $trunks;
+            foreach my $trunk (@trunks_array) {
+                eval { run_command(['/sbin/bridge', 'vlan', 'add', 'dev', $iface, 'vid', $trunk]) };
+                die "unable to add vlan $trunk to interface $iface - $@\n" if $@;
+            }
+        } elsif (!$tag) {
+            eval { run_command(['/sbin/bridge', 'vlan', 'add', 'dev', $iface, 'vid', '2-4094']) };
+            die "unable to add default vlan tags to interface $iface - $@\n" if $@;
+        }
+
+        $tag = 1 if !$tag;
+        eval { run_command(['/sbin/bridge', 'vlan', 'add', 'dev', $iface, 'vid', $tag, 'pvid', 'untagged']) };
+        die "unable to add vlan $tag to interface $iface - $@\n" if $@;
    }
 };
 
@@ -251,22 +244,29 @@ my $ovs_bridge_add_port = sub {
 
     $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;
+    my $cmd = ['/usr/bin/ovs-vsctl'];
+    # first command
+    push @$cmd, '--', 'add-port', $bridge, $iface;
+    push @$cmd, "tag=$tag" if $tag;
+    push @$cmd, "trunks=". join(',', $trunks) if $trunks;
+    push @$cmd, "vlan_mode=native-untagged" if $tag && $trunks;
+
+    if ($internal) {
+       # second command
+       push @$cmd, '--', 'set', 'Interface', $iface, 'type=internal';
+    }
+
+    eval { run_command($cmd) };
+    die "can't add ovs port '$iface' - $@\n" if $@;
 
-    $cmd .= " -- set Interface $iface type=internal" if $internal;
-    system($cmd) == 0 ||
-       die "can't add ovs port '$iface'\n";
     disable_ipv6($iface);
 };
 
 my $activate_interface = sub {
     my ($iface) = @_;
 
-    system("/sbin/ip link set $iface up") == 0 ||
-       die "can't activate interface '$iface'\n";
+    eval { run_command(['/sbin/ip', 'link', 'set', $iface, 'up']) };
+    die "can't activate interface '$iface' - $@\n" if $@;
 };
 
 sub tap_create {
@@ -274,7 +274,7 @@ sub tap_create {
 
     die "unable to get bridge setting\n" if !$bridge;
 
-    my $bridgemtu = &$read_bridge_mtu($bridge);
+    my $bridgemtu = read_bridge_mtu($bridge);
 
     eval {
        disable_ipv6($iface);
@@ -288,13 +288,22 @@ sub veth_create {
 
     die "unable to get bridge setting\n" if !$bridge;
 
-    my $bridgemtu = &$read_bridge_mtu($bridge);
+    my $bridgemtu = read_bridge_mtu($bridge);
 
     # create veth pair
     if (! -d "/sys/class/net/$veth") {
-       my $cmd = "/sbin/ip link add name $veth mtu $bridgemtu type veth peer name $vethpeer mtu $bridgemtu";
-       $cmd .= " addr $mac" if $mac;
-       system($cmd) == 0 || die "can't create interface $veth\n";
+       my $cmd = ['/sbin/ip', 'link', 'add'];
+       # veth device + MTU
+       push @$cmd, 'name', $veth;
+       push @$cmd, 'mtu', $bridgemtu;
+       push @$cmd, 'type', 'veth';
+       # peer device + MTU
+       push @$cmd, 'peer', 'name', $vethpeer, 'mtu', $bridgemtu;
+
+       push @$cmd, 'addr', $mac if $mac;
+
+       eval { run_command($cmd) };
+       die "can't create interface $veth - $@\n" if $@;
     }
 
     # up vethpair
@@ -337,7 +346,7 @@ my $create_firewall_bridge_ovs = sub {
     my ($vmid, $devid) = &$parse_tap_device_name($iface);
     my ($fwbr, undef, undef, $ovsintport) = &$compute_fwbr_names($vmid, $devid);
 
-    my $bridgemtu = &$read_bridge_mtu($bridge);
+    my $bridgemtu = read_bridge_mtu($bridge);
 
     &$cond_create_bridge($fwbr);
     &$activate_interface($fwbr);
@@ -455,8 +464,14 @@ sub activate_bridge_vlan_slave {
 
     # create vlan on $iface is not already exist
     if (! -d "/sys/class/net/$ifacevlan") {
-       system("/sbin/ip link add link $iface name $ifacevlan type vlan id $tag") == 0 ||
-           die "can't add vlan tag $tag to interface $iface\n";
+       eval {
+           my $cmd = ['/sbin/ip', 'link', 'add'];
+           push @$cmd, 'link', $iface;
+           push @$cmd, 'name', $ifacevlan;
+           push @$cmd, 'type', 'vlan', 'id', $tag;
+           run_command($cmd);
+       };
+       die "can't add vlan tag $tag to interface $iface - $@\n" if $@;
 
        # remove ipv6 link-local address before activation
        disable_ipv6($ifacevlan);
@@ -579,21 +594,104 @@ sub is_ip_in_cidr {
     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;
+    my $overlap = $cidr_obj->overlaps($ip_obj);
+
+    return if !defined($overlap);
+
+    return $overlap == $Net::IP::IP_B_IN_A_OVERLAP || $overlap == $Net::IP::IP_IDENTICAL;
 }
 
+# get all currently configured addresses that have a global scope, i.e., are reachable from the
+# outside of the host and thus are neither loopback nor link-local ones
+# returns an array ref of: { addr => "IP", cidr => "IP/PREFIXLEN", family => "inet|inet6" }
+sub get_reachable_networks {
+    my $raw = '';
+    run_command([qw(ip -j addr show up scope global)], outfunc => sub { $raw .= shift });
+    my $decoded = decode_json($raw);
+
+    my $addrs = []; # filter/transform first so that we can sort correctly more easily below
+    for my $e ($decoded->@*) {
+       next if !$e->{addr_info} || grep { $_ eq 'LOOPBACK' } $e->{flags}->@*;
+       push $addrs->@*, grep { scalar(keys $_->%*) } $e->{addr_info}->@*
+    }
+    my $res = [];
+    for my $info (sort { $a->{family} cmp $b->{family} || $a->{local} cmp $b->{local} } $addrs->@*) {
+       push $res->@*, {
+           addr => $info->{local},
+           cidr => "$info->{local}/$info->{prefixlen}",
+           family => $info->{family},
+       };
+    }
+
+    return $res;
+}
+
+# get one or all local IPs that are not loopback ones, able to pick up the following ones (in order)
+# - the hostname primary resolves too, follows gai.conf (admin controlled) and will be prioritised
+# - all configured in the interfaces configuration
+# - all currently networks known to the kernel in the current (root) namespace
+# returns a single address if no parameter is passed, and all found, grouped by type, if `all => 1`
+# is passed.
+sub get_local_ip {
+    my (%param) = @_;
+
+    my $nodename = PVE::INotify::nodename();
+    my $resolved_host = eval { get_ip_from_hostname($nodename) };
+
+    return $resolved_host if defined($resolved_host) && !$param{all};
+
+    my $all = { v4 => {}, v6 => {} }; # hash to avoid duplicates and group by type
+
+    my $ifaces = PVE::INotify::read_file('interfaces', 1)->{data}->{ifaces};
+    for my $if (values $ifaces->%*) {
+       next if $if->{type} eq 'loopback' || (!defined($if->{address}) && !defined($if->{address6}));
+       my ($v4, $v6) = ($if->{address}, $if->{address6});
+
+       return ($v4 // $v6) if !$param{all}; # prefer v4, admin can override $resolved_host via hosts/gai.conf
+
+       $all->{v4}->{$v4} = 1 if defined($v4);
+       $all->{v6}->{$v6} = 1 if defined($v6);
+    }
+
+    my $live = eval { get_reachable_networks() } // [];
+    for my $info ($live->@*) {
+       my $addr = $info->{addr};
+
+       return $addr if !$param{all};
+
+       if ($info->{family} eq 'inet') {
+           $all->{v4}->{$addr} = 1;
+       } else {
+           $all->{v6}->{$addr} = 1;
+       }
+    }
+
+    return undef if !$param{all}; # getting here means no early return above triggered -> no IPs
+
+    my $res = []; # order gai.conf controlled first, then group v4 and v6, simply lexically sorted
+    if ($resolved_host) {
+       push $res->@*, $resolved_host;
+       delete $all->{v4}->{$resolved_host};
+       delete $all->{v6}->{$resolved_host};
+    }
+    push $res->@*, sort { $a cmp $b } keys $all->{v4}->%*;
+    push $res->@*, sort { $a cmp $b } keys $all->{v6}->%*;
+
+    return $res;
+}
 
 sub get_local_ip_from_cidr {
     my ($cidr) = @_;
 
-    my $IPs = [];
+    my $IPs = {};
+    my $i = 1;
     run_command(['/sbin/ip', 'address', 'show', 'to', $cidr, 'up'], outfunc => sub {
        if ($_[0] =~ m!^\s*inet(?:6)?\s+($PVE::Tools::IPRE)(?:/\d+|\s+peer\s+)!) {
-           push @$IPs, $1;
+           $IPs->{$1} = $i++ if !exists($IPs->{$1});
        }
     });
 
-    return $IPs;
+    return [ sort { $IPs->{$a} <=> $IPs->{$b} } keys %{$IPs} ];
 }
 
 sub addr_to_ip {
@@ -613,21 +711,15 @@ sub get_ip_from_hostname {
        return undef;
     }
 
-    my ($ip, $family);
     for my $ai (@res) {
-       $family = $ai->{family};
-       my $tmpip = addr_to_ip($ai->{addr});
-       if ($tmpip !~ m/^127\.|^::1$/) {
-           $ip = $tmpip;
-           last;
+       my $ip = addr_to_ip($ai->{addr});
+       if ($ip !~ m/^127\.|^::1$/) {
+           return wantarray ? ($ip, $ai->{family}) : $ip;
        }
     }
-    if (!defined($ip) ) {
-       die "hostname lookup '$hostname' failed - got local IP address '$ip'\n" if !$noerr;
-       return undef;
-    }
-
-    return wantarray ? ($ip, $family) : $ip;
+    # NOTE: we only get here if no WAN/LAN IP was found, so this is now the error path!
+    die "address lookup for '$hostname' did not find any IP address\n" if !$noerr;
+    return undef;
 }
 
 sub lock_network {
@@ -637,4 +729,33 @@ sub lock_network {
     return $res;
 }
 
+# the canonical form of the given IP, i.e. dotted quad for IPv4 and RFC 5952 for IPv6
+sub canonical_ip {
+    my ($ip) = @_;
+
+    my $ip_obj = NetAddr::IP->new($ip) or die "invalid IP string '$ip'\n";
+
+    return $ip_obj->canon();
+}
+
+# List of unique, canonical IPs in the provided list.
+# Keeps the original order, filtering later duplicates.
+sub unique_ips {
+    my ($ips) = @_;
+
+    my $res = [];
+    my $seen = {};
+
+    for my $ip (@{$ips}) {
+       $ip = canonical_ip($ip);
+
+       next if $seen->{$ip};
+
+       $seen->{$ip} = 1;
+       push @{$res}, $ip;
+    }
+
+    return $res;
+}
+
 1;