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 Socket qw(NI_NUMERICHOST NI_NUMERICSERV);
-use POSIX qw(ECONNREFUSED);
-
use Net::IP;
+use POSIX qw(ECONNREFUSED);
+use Socket qw(NI_NUMERICHOST NI_NUMERICSERV);
# host network related utility functions
};
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"); };
"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");
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 $@;
}
};
$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 {
die "unable to get bridge setting\n" if !$bridge;
- my $bridgemtu = &$read_bridge_mtu($bridge);
+ 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]);
};
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
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);
# set the same mtu for ovs int port
PVE::Tools::run_command(['/sbin/ip', 'link', 'set', $ovsintport, 'mtu', $bridgemtu]);
-
+
&$bridge_add_interface($fwbr, $ovsintport);
};
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
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 {}) };
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) {
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);
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+|\s+peer\s+)!) {
+ 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 get_ip_from_hostname {
my ($hostname, $noerr) = @_;
- my ($family, $ip);
-
- eval {
- my @res = PVE::Tools::getaddrinfo_all($hostname);
- $family = $res[0]->{family};
- $ip = addr_to_ip($res[0]->{addr})
- };
+ my @res = eval { PVE::Tools::getaddrinfo_all($hostname) };
if ($@) {
die "hostname lookup '$hostname' failed - $@" if !$noerr;
return undef;
}
- if ($ip =~ m/^127\.|^::1$/) {
+ 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;
}