]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/INotify.pm
interfaces: improve bridge_fd handling
[pve-common.git] / src / PVE / INotify.pm
index 1eaec2191f365fc901793cd411678ec23ee08ef0..ad45cd2cba2fe40b4fc1038cb06897ef0ca98f95 100644 (file)
@@ -723,14 +723,15 @@ register_file('active', "/var/log/pve/tasks/active",
              \&write_active_workers);
 
 
-our $bond_modes = { 'balance-rr' => 0,
-                  'active-backup' => 1,
-                  'balance-xor' => 2,
-                  'broadcast' => 3,
-                  '802.3ad' => 4,
-                  'balance-tlb' => 5,
-                  'balance-alb' => 6,
-              };
+our $bond_modes = {
+    'balance-rr' => 0,
+    'active-backup' => 1,
+    'balance-xor' => 2,
+    'broadcast' => 3,
+    '802.3ad' => 4,
+    'balance-tlb' => 5,
+    'balance-alb' => 6,
+};
 
 my $ovs_bond_modes = {
     'active-backup' => 1,
@@ -960,6 +961,7 @@ sub __read_etc_network_interfaces {
                        'bond_miimon' => 1,
                        'bond_xmit_hash_policy' => 1,
                        'bond-primary' => 1,
+                       'link-type'   => 1,
                        'uplink-id' => 1,
                        'vlan-protocol' => 1,
                        'vlan-raw-device' => 1,
@@ -1059,13 +1061,12 @@ sub __read_etc_network_interfaces {
        } elsif ($iface =~ m/^vmbr\d+$/) {
            if (!$d->{ovs_type}) {
                $d->{type} = 'bridge';
-
-               if (!defined ($d->{bridge_fd})) {
-                   $d->{bridge_fd} = 0;
-               }
                if (!defined ($d->{bridge_stp})) {
                    $d->{bridge_stp} = 'off';
                }
+               if (!defined($d->{bridge_fd}) && $d->{bridge_stp} eq 'off') {
+                   $d->{bridge_fd} = 0;
+               }
            } elsif ($d->{ovs_type} eq 'OVSBridge') {
                $d->{type} = $d->{ovs_type};
            }
@@ -1077,10 +1078,19 @@ sub __read_etc_network_interfaces {
                $ifaces->{$1}->{exists} = 0;
                $d->{exists} = 0;
            }
-       } elsif ($iface =~ m/^(\S+)\.\d+$/ || $d->{'vlan-raw-device'}) {
+       } elsif ($iface =~ m/^(\S+)\.(\d+)$/ || $d->{'vlan-raw-device'}) {
            $d->{type} = 'vlan';
 
-           my $raw_iface = $d->{'vlan-raw-device'} ? $d->{'vlan-raw-device'} : $1;
+           my ($dev, $id) = ($1, $2);
+           $d->{'vlan-raw-device'} = $dev if defined($dev) && !$d->{'vlan-raw-device'};
+
+           if (!$id && $iface =~ m/^vlan(\d+)$/) { # VLAN id 0 is not valid, so truthy check it is
+               $id = $1;
+           }
+           $d->{'vlan-id'} = $id if $id;
+
+           my $raw_iface = $d->{'vlan-raw-device'};
+
            if (defined ($ifaces->{$raw_iface})) {
                $d->{exists} = $ifaces->{$raw_iface}->{exists};
            } else {
@@ -1106,6 +1116,8 @@ sub __read_etc_network_interfaces {
                    my $tag = &$extract_ovs_option($d, 'tag');
                    $d->{ovs_tag} = $tag if defined($tag);
                }
+           } elsif (defined($d->{'link-type'})) {
+               $d->{type} = $d->{'link-type'} if $d->{'link-type'} eq 'dummy';
            }
        }
 
@@ -1143,6 +1155,10 @@ sub __read_etc_network_interfaces {
        $d->{method} = 'manual' if !$d->{method};
        $d->{method6} = 'manual' if !$d->{method6};
 
+       if (my $comments6 = delete $d->{comments6}) {
+           $d->{comments} = ($d->{comments} // '') . $comments6;
+       }
+
        $d->{families} ||= ['inet'];
     }
 
@@ -1229,24 +1245,31 @@ sub __interface_to_string {
     my $done = { type => 1, priority => 1, method => 1, active => 1, exists => 1,
                 comments => 1, autostart => 1, options => 1,
                 address => 1, netmask => 1, gateway => 1, broadcast => 1,
-                method6 => 1, families => 1, options6 => 1,
+                method6 => 1, families => 1, options6 => 1, comments6 => 1,
                 address6 => 1, netmask6 => 1, gateway6 => 1, broadcast6 => 1, 'uplink-id' => 1 };
 
     if (!$first_block) {
        # not printing out options
     } elsif ($d->{type} eq 'bridge') {
 
-       $d->{bridge_ports} =~ s/[;,\s]+/ /g;
        my $ports = $d->{bridge_ports} || 'none';
+       $ports =~ s/[;,\s]+/ /g;
        $raw .= "\tbridge-ports $ports\n";
        $done->{bridge_ports} = 1;
 
        my $v = defined($d->{bridge_stp}) ? $d->{bridge_stp} : 'off';
+       my $no_stp = $v eq 'off';
+
        $raw .= "\tbridge-stp $v\n";
        $done->{bridge_stp} = 1;
 
        $v = defined($d->{bridge_fd}) ? $d->{bridge_fd} : 0;
-       $raw .= "\tbridge-fd $v\n";
+       # 0 is only allowed when STP is disabled
+       if ($no_stp || ($v >= 2 && $v <= 30)) {
+           $raw .= "\tbridge-fd $v\n";
+       } else {
+           warn "'$iface': not setting 'bridge_fd' to value '$v' outside of allowed range 2-30\n";
+       }
        $done->{bridge_fd} = 1;
 
        if( defined($d->{bridge_vlan_aware})) {
@@ -1487,6 +1510,7 @@ sub __write_etc_network_interfaces {
        if ($d->{type} eq 'OVSBond' && $d->{ovs_bonds}) {
            foreach my $p (split (/\s+/, $d->{ovs_bonds})) {
                my $n = $ifaces->{$p};
+               $n->{autostart} = 1;
                die "OVS bond '$iface' - unable to find slave '$p'\n"
                    if !$n;
                die "OVS bond '$iface' - wrong interface type on slave '$p' " .
@@ -1499,20 +1523,21 @@ sub __write_etc_network_interfaces {
     # check bond
     foreach my $iface (keys %$ifaces) {
        my $d = $ifaces->{$iface};
-       if ($d->{type} eq 'bond' && $d->{slaves}) {
-           my $bond_primary_is_slave = undef;
-           foreach my $p (split (/\s+/, $d->{slaves})) {
-               my $n = $ifaces->{$p};
+       next if !($d->{type} eq 'bond' && $d->{slaves});
 
-               die "bond '$iface' - unable to find slave '$p'\n"
-                   if !$n;
-               die "bond '$iface' - wrong interface type on slave '$p' " .
-                   "('$n->{type}' != 'eth')\n" if $n->{type} ne 'eth';
-               &$check_mtu($ifaces, $iface, $p);
-               $bond_primary_is_slave = 1 if $d->{'bond-primary'} && $d->{'bond-primary'} eq $p;
-           }
-           die "bond '$iface' - bond-primary interface is not a slave" if $d->{'bond-primary'} && !$bond_primary_is_slave;
+       my $bond_primary_is_slave = undef;
+       foreach my $p (split (/\s+/, $d->{slaves})) {
+           my $n = $ifaces->{$p};
+           $n->{autostart} = 1;
+
+           die "bond '$iface' - unable to find slave '$p'\n" if !$n;
+           die "bond '$iface' - wrong interface type on slave '$p' ('$n->{type}' != 'eth or bond')\n"
+               if ($n->{type} ne 'eth' && $n->{type} ne 'bond');
+
+           $check_mtu->($ifaces, $iface, $p);
+           $bond_primary_is_slave = 1 if $d->{'bond-primary'} && $d->{'bond-primary'} eq $p;
        }
+       die "bond '$iface' - bond-primary interface is not a slave" if $d->{'bond-primary'} && !$bond_primary_is_slave;
     }
 
     # check vxlan
@@ -1552,6 +1577,8 @@ sub __write_etc_network_interfaces {
                $p = $1;
                $vlanid = $2;
                delete $d->{'vlan-raw-device'} if $d->{'vlan-raw-device'};
+               delete $d->{'vlan-id'} if $d->{'vlan-id'};
+
            } else {
                die "missing vlan-raw-device option" if !$d->{'vlan-raw-device'};
                $p = $d->{'vlan-raw-device'};
@@ -1603,7 +1630,7 @@ sub __write_etc_network_interfaces {
     foreach my $iface (keys %$ifaces_copy) {
        my $d = $ifaces_copy->{$iface};
        if ($d->{type} eq 'bridge') {
-           foreach my $p (split (/\s+/, $d->{bridge_ports})) {
+           foreach my $p (split (/\s+/, $d->{bridge_ports} // '')) {
                if($p =~ m/(\S+)\.(\d+)$/) {
                    my $vlanparent = $1;
                    if (!defined($ifaces_copy->{$p})) {
@@ -1617,7 +1644,7 @@ sub __write_etc_network_interfaces {
                die "bridge '$iface' - unable to find bridge port '$p'\n" if !$n;
                die "iface $p - ip address can't be set on interface if bridged in $iface\n"
                    if ($n->{method} && $n->{method} eq 'static' && $n->{address} ne '0.0.0.0') ||
-                      ($n->{method6} && $n->{method6} eq 'static' && $n->{address} ne '::');
+                      ($n->{method6} && $n->{method6} eq 'static' && $n->{address6} ne '::');
                &$check_mtu($ifaces_copy, $p, $iface);
                $bridgeports->{$p} = $iface;
            }
@@ -1656,6 +1683,7 @@ NETWORKDOC
 
     my $if_type_hash = {
        loopback => 100000,
+       dummy => 100000,
        eth => 200000,
        OVSPort => 200000,
        OVSIntPort => 300000,
@@ -1672,12 +1700,10 @@ NETWORKDOC
 
        my ($rootiface, @rest) = split(/[.:]/, $iface);
        my $childlevel = scalar(@rest);
-       my $n = $ifaces->{$rootiface};
-
-       my $pri = $if_type_hash->{$n->{type}} + $childlevel
-           if $n->{type} && $n->{type} ne 'unknown';
+       my $type = $ifaces->{$rootiface}->{type};
+       return if !$type || $type eq 'unknown';
 
-       return $pri;
+       return $if_type_hash->{$type} + $childlevel
     };
 
     foreach my $iface (sort {
@@ -1717,6 +1743,11 @@ NETWORKDOC
            }
        }
 
+       # if 'inet6' is the only family
+       if (scalar($d->{families}->@*) == 1 && $d->{families}[0] eq 'inet6') {
+           $d->{comments6} = delete $d->{comments};
+       }
+
        my $i = 0; # some options should be printed only once
        $raw .= __interface_to_string($iface, $d, $_, !$i++, $ifupdown2) foreach @{$d->{families}};
     }
@@ -1778,8 +1809,11 @@ my $format_apt_auth_data = sub {
 
     my $raw = '';
 
-    foreach my $machine (sort keys %$data) {
+    # sort longer entries first, so machine definitions with higher granularity are preferred
+    for my $machine (sort { length($b) <=> length($a) || $a cmp $b} keys %$data) {
        my $d = $data->{$machine};
+       next if !defined($d); # allow "deleting" set entries
+
        $raw .= "machine $machine\n";
        $raw .= " login $d->{login}\n" if $d->{login};
        $raw .= " password $d->{password}\n" if $d->{password};
@@ -1792,7 +1826,7 @@ my $format_apt_auth_data = sub {
 sub write_apt_auth {
     my ($filename, $fh, $data) = @_;
 
-    my $raw = &$format_apt_auth_data($data);
+    my $raw = $format_apt_auth_data->($data);
 
     die "write failed: $!" unless print $fh "$raw\n";
 
@@ -1808,11 +1842,16 @@ sub update_apt_auth {
        $orig->{$machine} = $data->{$machine};
     }
 
-    return &$format_apt_auth_data($orig);
+    return $format_apt_auth_data->($orig);
 }
 
-register_file('apt-auth', "/etc/apt/auth.conf",
-             \&read_apt_auth, \&write_apt_auth,
-             \&update_apt_auth, perm => 0640);
+register_file(
+    'apt-auth',
+    "/etc/apt/auth.conf",
+    \&read_apt_auth,
+    \&write_apt_auth,
+    \&update_apt_auth,
+    perm => 0640,
+);
 
 1;