[pve-devel] [PATCH pve-manager v2 1/2] pvestatd: pull metric: use ip link to detect physical interfaces

Stefan Hanreich s.hanreich at proxmox.com
Tue Jul 29 19:16:44 CEST 2025


pve-common now allows arbitrary names for physical interfaces, without
being restricted by PHYSICAL_NIC_RE. In order to detect physical
interfaces, pvestatd now needs to query 'ip link' for the type of an
interface instead of relying on the regular expression.

On the receiving end, PullMetric cannot consult 'ip link' for
determining which interface is physical or not. To work around that,
introduce a new type key, that carries information about the type of
an interface. When aggregating the metrics, PullMetric can now read
this additional parameter, to infer the type of the interface (either
physical or virtual).

To avoid spawning a process in every update loop of pvestatd, cache
the output once and then use it throughout the lifecycle of pvestatd.
Physical interfaces rarely get added / removed without a reboot, so we
can cache this indefinitely. Users can always restart the service to
refresh the information about physical interfaces.

Signed-off-by: Stefan Hanreich <s.hanreich at proxmox.com>
---
 PVE/PullMetric.pm         | 15 ++++++++++++---
 PVE/Service/pvestatd.pm   | 15 ++++++++++++++-
 services/pvestatd.service |  2 +-
 3 files changed, 27 insertions(+), 5 deletions(-)

diff --git a/PVE/PullMetric.pm b/PVE/PullMetric.pm
index f55653505..24310c30e 100644
--- a/PVE/PullMetric.pm
+++ b/PVE/PullMetric.pm
@@ -91,9 +91,18 @@ my sub get_node_metrics {
     push @$metrics, gauge($id, $timestamp, "uptime", $data->{uptime});
 
     my ($netin, $netout) = (0, 0);
-    for my $dev (grep { /^$PVE::Network::PHYSICAL_NIC_RE$/ } keys $data->{nics}->%*) {
-        $netin += $data->{nics}->{$dev}->{receive};
-        $netout += $data->{nics}->{$dev}->{transmit};
+
+    for my $dev (keys $data->{nics}->%*) {
+        my $nic_data = $data->{nics}->{$dev};
+
+        if ($nic_data->{type}) {
+            next if $nic_data->{type} ne 'physical';
+        } else {
+            next if $dev !~ /^$PVE::Network::PHYSICAL_NIC_RE$/;
+        }
+
+        $netin += $nic_data->{receive};
+        $netout += $nic_data->{transmit};
     }
     push @$metrics, derive($id, $timestamp, "net_in", $netin);
     push @$metrics, derive($id, $timestamp, "net_out", $netout);
diff --git a/PVE/Service/pvestatd.pm b/PVE/Service/pvestatd.pm
index e645eec3c..d662e3070 100755
--- a/PVE/Service/pvestatd.pm
+++ b/PVE/Service/pvestatd.pm
@@ -155,6 +155,8 @@ my sub broadcast_static_node_info {
     }
 }
 
+my $cached_ip_links = undef;
+
 sub update_node_status {
     my ($status_cfg, $pull_txn) = @_;
 
@@ -171,9 +173,20 @@ sub update_node_status {
     my $sublevel = $subinfo->{level} || '';
 
     my $netdev = PVE::ProcFSTools::read_proc_net_dev();
+    $cached_ip_links = PVE::Network::ip_link_details() if !$cached_ip_links;
+
     # traffic from/to physical interface cards
     my ($netin, $netout) = (0, 0);
-    for my $dev (grep { /^$PVE::Network::PHYSICAL_NIC_RE$/ } keys %$netdev) {
+    for my $dev (keys %$netdev) {
+        my $ip_link = $cached_ip_links->{$dev};
+
+        if (PVE::Network::ip_link_is_physical($ip_link)) {
+            $netdev->{$dev}->{type} = 'physical';
+        } else {
+            $netdev->{$dev}->{type} = 'virtual';
+            next;
+        }
+
         $netin += $netdev->{$dev}->{receive};
         $netout += $netdev->{$dev}->{transmit};
     }
diff --git a/services/pvestatd.service b/services/pvestatd.service
index d7db50f6d..68a05305f 100644
--- a/services/pvestatd.service
+++ b/services/pvestatd.service
@@ -2,7 +2,7 @@
 Description=PVE Status Daemon
 ConditionPathExists=/usr/bin/pvestatd
 Wants=pve-cluster.service
-After=pve-cluster.service
+After=pve-cluster.service pvenetcommit.service
 
 [Service]
 ExecStart=/usr/bin/pvestatd start
-- 
2.47.2




More information about the pve-devel mailing list