]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
src: Use device alias when ifname is unset in virDomainInterfaceAddresses()
authorMichal Privoznik <mprivozn@redhat.com>
Tue, 20 Jan 2026 15:19:00 +0000 (16:19 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Wed, 21 Jan 2026 16:29:44 +0000 (17:29 +0100)
The virDomainInterfaceAddresses() API returns an array of
_virDomainInterface structs which then describe IP addresses
associated with given domain. The struct contains 'name' member
which is documented deliberately vaguely: "interface name". This
is because depending on the source of truth used (controlled by
'source' argument) the name can be wildly different from the one
in domain XML. Now, in case of source =
VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP, the host's ARP table is
parsed and matching interfaces are found by comparing MAC
addresses. If it's a match then the 'name' is set to net->ifname
(corresponds to /interface/target/@dev). But that is not always
set and sometimes may be NULL (e.g. for hostdevs, usernet). We
can't change the API (like we did for hwaddr in v1.2.14-rc1~105)
because this is already released. So the next best thing to do is
to put the interface alias in there.

To be on a safe side, do the same change to the
VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LEASE case.

Resolves: https://issues.redhat.com/browse/RHEL-141496
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/conf/domain_conf.c
src/libvirt-domain.c

index d00a43e969a8ca798e335d12e9ad88be4adc33ac..8b26de674e5679ac145f99a53709c215a8def672 100644 (file)
@@ -15598,6 +15598,12 @@ virDomainNetDHCPInterfaces(virDomainDef *def,
             goto error;
 
         if (n_leases) {
+            const char *ifname = def->nets[i]->ifname;
+
+            if (!ifname) {
+                ifname = def->nets[i]->info.alias;
+            }
+
             ifaces_ret = g_renew(virDomainInterfacePtr, ifaces_ret, ifaces_count + 1);
             ifaces_ret[ifaces_count] = g_new0(virDomainInterface, 1);
             iface = ifaces_ret[ifaces_count];
@@ -15606,7 +15612,7 @@ virDomainNetDHCPInterfaces(virDomainDef *def,
             /* Assuming each lease corresponds to a separate IP */
             iface->naddrs = n_leases;
             iface->addrs = g_new0(virDomainIPAddress, iface->naddrs);
-            iface->name = g_strdup(def->nets[i]->ifname);
+            iface->name = g_strdup(ifname);
             iface->hwaddr = g_strdup(macaddr);
         }
 
@@ -15660,9 +15666,15 @@ virDomainNetARPInterfaces(virDomainDef *def,
             virArpTableEntry entry = table->t[j];
 
             if (STREQ(entry.mac, macaddr)) {
+                const char *ifname = def->nets[i]->ifname;
+
+                if (!ifname) {
+                    ifname = def->nets[i]->info.alias;
+                }
+
                 iface = g_new0(virDomainInterface, 1);
 
-                iface->name = g_strdup(def->nets[i]->ifname);
+                iface->name = g_strdup(ifname);
 
                 iface->hwaddr = g_strdup(macaddr);
 
index a278c4679d91d9e8c62c5fac4e14a97c196db5d1..a1f6efde20713513cef541b11dda0041b90d7ca3 100644 (file)
@@ -12880,6 +12880,8 @@ virDomainFSInfoFree(virDomainFSInfoPtr info)
  * Note that for some @source values some pieces of returned @ifaces
  * might be unset (e.g. VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP does not
  * set IP address prefix as ARP table does not have any notion of that).
+ * Moreover, it may happen that the interface doesn't have a name. In
+ * that case, @ifaces->name is set to the interface's device alias.
  *
  * @ifaces->name is never NULL, and @ifaces->hwaddr may be NULL.
  *