]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/Network.pm
network: bridge add IF: do not add all VLANs if $trunks are passed
[pve-common.git] / src / PVE / Network.pm
index 69051b9de048e5a6023902b9898af9cd5ab655b5..04a964a8c4c8e99d8ffb5add7c7d3402f39fbafe 100644 (file)
@@ -2,17 +2,21 @@ package PVE::Network;
 
 use strict;
 use warnings;
-use PVE::Tools qw(run_command lock_file);
-use PVE::ProcFSTools;
+
 use PVE::INotify;
+use PVE::ProcFSTools;
+use PVE::Tools qw(run_command lock_file);
+
 use File::Basename;
 use IO::Socket::IP;
-use POSIX qw(ECONNREFUSED);
-
 use Net::IP;
+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 $ipv4_reverse_mask = [
     '0.0.0.0',
     '128.0.0.0',
@@ -78,7 +82,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"); };
@@ -99,24 +103,16 @@ sub setup_tc_rate_limit {
     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");
-       system("/sbin/tc class ls dev $iface");
-       system("/sbin/tc filter ls dev $iface parent ffff:");
-    }
+               "drop");
 }
 
 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 {
@@ -162,12 +158,42 @@ my $compute_fwbr_names = sub {
     return ($fwbr, $vethfw, $vethfwpeer, $ovsintport);
 };
 
+sub iface_delete($) {
+    my ($iface) = @_;
+    run_command(['/sbin/ip', 'link', 'delete', 'dev', $iface], noerr => 1)
+       == 0 or die "failed to delete interface '$iface'\n";
+}
+
+sub iface_create($$@) {
+    my ($iface, $type, @args) = @_;
+    run_command(['/sbin/ip', 'link', 'add', $iface, 'type', $type, @args], noerr => 1)
+       == 0 or die "failed to create interface '$iface'\n";
+}
+
+sub iface_set($@) {
+    my ($iface, @opts) = @_;
+    run_command(['/sbin/ip', 'link', 'set', $iface, @opts], noerr => 1)
+       == 0 or die "failed to set interface options for '$iface' (".join(' ', @opts).")\n";
+}
+
+# helper for nicer error messages:
+sub iface_set_master($$) {
+    my ($iface, $master) = @_;
+    if (defined($master)) {
+       eval { iface_set($iface, 'master', $master) };
+       die "can't enslave '$iface' to '$master'\n" if $@;
+    } else {
+       eval { iface_set($iface, 'nomaster') };
+       die "can't unenslave '$iface'\n" if $@;
+    }
+}
+
 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";
+       iface_create($bridge, 'bridge');
+       disable_ipv6($bridge);
     }
 };
 
@@ -185,29 +211,29 @@ my $bridge_add_interface = sub {
 
     # drop link local address (it can't be used when on a bridge anyway)
     disable_ipv6($iface);
-    system("/sbin/brctl addif $bridge $iface") == 0 ||
-       die "can't add interface 'iface' to bridge '$bridge'\n";
+    iface_set_master($iface, $bridge);
 
    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";
+           eval { run_command(['/sbin/bridge', 'vlan', 'del', 'dev', $iface, 'vid', '1-4094']) };
+           die "failed to remove default vlan tags of $iface - $@\n" if $@;
+
+           eval { run_command(['/sbin/bridge', 'vlan', 'add', 'dev', $iface, 'vid', $tag, 'pvid', 'untagged']) };
+           die "unable to add vlan $tag to interface $iface - $@\n" if $@;
 
            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;
-       } 
+       } elsif (!$trunks) {
+           eval { run_command(['/sbin/bridge', 'vlan', 'add', 'dev', $iface, 'vid', '2-4094']) };
+           die "unable to add default vlan tags to interface $iface - $@\n" if $@;
+       }
 
        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";
+           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 $@;
            }
        }
    }
@@ -218,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 {
@@ -243,7 +276,7 @@ sub tap_create {
 
     my $bridgemtu = &$read_bridge_mtu($bridge);
 
-    eval { 
+    eval {
        disable_ipv6($iface);
        PVE::Tools::run_command(['/sbin/ip', 'link', 'set', $iface, 'up', 'promisc', 'on', 'mtu', $bridgemtu]);
     };
@@ -259,9 +292,18 @@ sub veth_create {
 
     # create veth pair
     if (! -d "/sys/class/net/$veth") {
-       my $cmd = "/sbin/ip link add name $veth 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
@@ -275,7 +317,7 @@ sub veth_delete {
     my ($veth) = @_;
 
     if (-d "/sys/class/net/$veth") {
-       run_command("/sbin/ip link delete dev $veth", outfunc => sub {}, errfunc => sub {});
+       iface_delete($veth);
     }
     eval { tap_unplug($veth) };
 }
@@ -287,7 +329,6 @@ my $create_firewall_bridge_linux = sub {
     my ($fwbr, $vethfw, $vethfwpeer) = &$compute_fwbr_names($vmid, $devid);
 
     &$cond_create_bridge($fwbr);
-    disable_ipv6($fwbr);
     &$activate_interface($fwbr);
 
     copy_bridge_config($bridge, $fwbr);
@@ -308,7 +349,6 @@ my $create_firewall_bridge_ovs = sub {
     my $bridgemtu = &$read_bridge_mtu($bridge);
 
     &$cond_create_bridge($fwbr);
-    disable_ipv6($fwbr);
     &$activate_interface($fwbr);
 
     &$bridge_add_interface($fwbr, $iface);
@@ -318,7 +358,7 @@ my $create_firewall_bridge_ovs = sub {
 
     # set the same mtu for ovs int port
     PVE::Tools::run_command(['/sbin/ip', 'link', 'set', $ovsintport, 'mtu', $bridgemtu]);
-    
+
     &$bridge_add_interface($fwbr, $ovsintport);
 };
 
@@ -326,7 +366,7 @@ my $cleanup_firewall_bridge = sub {
     my ($iface) = @_;
 
     my ($vmid, $devid) = &$parse_tap_device_name($iface, 1);
-    return if !defined($vmid);  
+    return if !defined($vmid);
     my ($fwbr, $vethfw, $vethfwpeer, $ovsintport) = &$compute_fwbr_names($vmid, $devid);
 
     # cleanup old port config from any openvswitch bridge
@@ -339,8 +379,7 @@ my $cleanup_firewall_bridge = 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 {});
+       iface_delete($fwbr);
     }
 };
 
@@ -391,11 +430,9 @@ sub tap_unplug {
        #avoid insecure dependency;
        ($bridge) = $bridge =~ /(\S+)/;
 
-       system("/sbin/brctl delif $bridge $iface") == 0 ||
-           die "can't del interface '$iface' from bridge '$bridge'\n";
-
+       iface_set_master($iface, undef);
     }
-    
+
     &$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 {}) };
@@ -406,7 +443,7 @@ sub copy_bridge_config {
 
     return if $br0 eq $br1;
 
-    my $br_configs = [ 'ageing_time', 'stp_state', 'priority', 'forward_delay', 
+    my $br_configs = [ 'ageing_time', 'stp_state', 'priority', 'forward_delay',
                       'hello_time', 'max_age', 'multicast_snooping', 'multicast_querier'];
 
     foreach my $sysname (@$br_configs) {
@@ -424,15 +461,21 @@ sub copy_bridge_config {
 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/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);
+       # remove ipv6 link-local address before activation
+       disable_ipv6($ifacevlan);
+    }
 
     # be sure to have the $ifacevlan up
     &$activate_interface($ifacevlan);
@@ -477,8 +520,7 @@ sub activate_bridge_vlan {
     lock_network(sub {
        # 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";
+           iface_create($bridgevlan, 'bridge');
        }
 
        # for each physical interface (eth or bridge) bind them to bridge vlan
@@ -559,23 +601,50 @@ sub is_ip_in_cidr {
 sub get_local_ip_from_cidr {
     my ($cidr) = @_;
 
-    my $cmd = ['/sbin/ip', 'address', 'show', 'to', $cidr, 'up'];
-
     my $IPs = [];
-
-    my $code = sub {
-       my $line = shift;
-
-       if ($line =~ m!^\s*inet(?:6)?\s+($PVE::Tools::IPRE)/\d+!) {
+    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;
        }
-    };
-
-    PVE::Tools::run_command($cmd, outfunc => $code);
+    });
 
     return $IPs;
 }
 
+sub addr_to_ip {
+    my ($addr) = @_;
+    my ($err, $host, $port) = Socket::getnameinfo($addr, NI_NUMERICHOST | NI_NUMERICSERV);
+    die "failed to get numerical host address: $err\n" if $err;
+    return ($host, $port) if wantarray;
+    return $host;
+}
+
+sub get_ip_from_hostname {
+    my ($hostname, $noerr) = @_;
+
+    my @res = eval { PVE::Tools::getaddrinfo_all($hostname) };
+    if ($@) {
+       die "hostname lookup '$hostname' failed - $@" if !$noerr;
+       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;
+       }
+    }
+    if (!defined($ip) ) {
+       die "hostname lookup '$hostname' failed - got local IP address '$ip'\n" if !$noerr;
+       return undef;
+    }
+
+    return wantarray ? ($ip, $family) : $ip;
+}
+
 sub lock_network {
     my ($code, @param) = @_;
     my $res = lock_file('/var/lock/pve-network.lck', 10, $code, @param);