[pve-devel] [PATCH pve-common 1/1] INotify : network_interfaces: detect physical nic name from sysfs

Alexandre Derumier aderumier at odiso.com
Mon Jan 20 10:38:21 CET 2020


User can rename interfaces with udev/system, we can't rely on simple regex
to known if it's a physical inteface.

keep proc_net_dev && PHYSICAL_NIC_RE for tests compatibility.

Signed-off-by: Alexandre Derumier <aderumier at odiso.com>
---
 src/PVE/INotify.pm     | 22 ++++++++++++++--------
 src/PVE/ProcFSTools.pm | 12 ++++++++++++
 2 files changed, 26 insertions(+), 8 deletions(-)

diff --git a/src/PVE/INotify.pm b/src/PVE/INotify.pm
index d1731fc..9c1aa84 100644
--- a/src/PVE/INotify.pm
+++ b/src/PVE/INotify.pm
@@ -859,13 +859,13 @@ my $check_mtu = sub {
 # }
 sub read_etc_network_interfaces {
     my ($filename, $fh) = @_;
-    my $proc_net_dev = IO::File->new('/proc/net/dev', 'r');
+    my $phys_net_dev = PVE::ProcFSTools::get_physical_network_interfaces();
     my $active = PVE::ProcFSTools::get_active_network_interfaces();
-    return __read_etc_network_interfaces($fh, $proc_net_dev, $active);
+    return __read_etc_network_interfaces($fh, undef, $active, $phys_net_dev);
 }
 
 sub __read_etc_network_interfaces {
-    my ($fh, $proc_net_dev, $active_ifaces) = @_;
+    my ($fh, $proc_net_dev, $active_ifaces, $phys_net_dev) = @_;
 
     my $config = {};
     my $ifaces = $config->{ifaces} = {};
@@ -893,7 +893,12 @@ sub __read_etc_network_interfaces {
 		$ifaces->{$1}->{exists} = 1;
 	    }
 	}
-	close($proc_net_dev);
+    }
+
+    if($phys_net_dev) {
+	while (my ($ifname) = each %{$phys_net_dev}) {
+	    $ifaces->{$ifname}->{exists} = 1;
+	}
     }
 
     # we try to keep order inside the file
@@ -1084,7 +1089,7 @@ sub __read_etc_network_interfaces {
 		$ifaces->{$1}->{exists} = 0;
 		$d->{exists} = 0;
 	    }
-	} elsif ($iface =~ m/^$PVE::Network::PHYSICAL_NIC_RE$/) {
+	} elsif ($iface =~ m/^$PVE::Network::PHYSICAL_NIC_RE$/ || $phys_net_dev->{$iface}) {
 	    if (!$d->{ovs_type}) {
 		$d->{type} = 'eth';
 	    } elsif ($d->{ovs_type} eq 'OVSPort') {
@@ -1354,11 +1359,12 @@ sub __interface_to_string {
 sub write_etc_network_interfaces {
     my ($filename, $fh, $config) = @_;
     my $ifupdown2 = -e '/usr/share/ifupdown2';
-    my $raw = __write_etc_network_interfaces($config, $ifupdown2);
+    my $phys_net_dev = PVE::ProcFSTools::get_physical_network_interfaces();
+    my $raw = __write_etc_network_interfaces($config, $phys_net_dev, $ifupdown2);
     PVE::Tools::safe_print($filename, $fh, encode('UTF-8', $raw));
 }
 sub __write_etc_network_interfaces {
-    my ($config, $ifupdown2) = @_;
+    my ($config, $phys_net_dev, $ifupdown2) = @_;
 
     my $ifaces = $config->{ifaces};
     my @options = @{$config->{options}};
@@ -1390,7 +1396,7 @@ sub __write_etc_network_interfaces {
 	    $d->{type} eq 'OVSBond') {
 	    my $brname = $used_ports->{$iface};
 	    if (!$brname || !$ifaces->{$brname}) {
-		if ($iface =~ /^$PVE::Network::PHYSICAL_NIC_RE/) {
+		if ($iface =~ /^$PVE::Network::PHYSICAL_NIC_RE/ || $phys_net_dev->{$iface}) {
 		    $ifaces->{$iface} = { type => 'eth',
 					  exists => 1,
 					  method => 'manual',
diff --git a/src/PVE/ProcFSTools.pm b/src/PVE/ProcFSTools.pm
index 94a2d54..eda9a96 100644
--- a/src/PVE/ProcFSTools.pm
+++ b/src/PVE/ProcFSTools.pm
@@ -434,4 +434,16 @@ sub get_active_network_interfaces {
     return $ifaces;
 }
 
+sub get_physical_network_interfaces {
+
+    my $ifaces = {};
+    my @devices = glob('/sys/devices/pci*/*/*/net/*');
+   
+    foreach my $device (@devices) {
+        if ($device =~ /\/sys\/devices\/(\S+)\/(\S+)\/(\S+)\/net\/(\S+)/) {
+	    $ifaces->{$4} = 1;
+        }
+    }
+    return $ifaces;
+}
 1;
-- 
2.20.1




More information about the pve-devel mailing list