[netcf-devel] [PATCH] Only list devices that have an ifcfg file in ncf_lookup_by_mac_string.

Laine Stump laine at laine.org
Thu Apr 8 19:36:36 UTC 2010


drv_lookup_by_mac_string would previously find all interfaces that had
a directory in /sys/class/net with an address file containing the
correct MAC address. This would include transient interfaces like
libvirt's virbr*, tun*, etc. These transient interfaces can't be
configured with netcf anyway, so it doesn't make sense to list them
here. This patch checks each interface name before adding it to the
list, to make sure an ifcfg file exists for that interface. If not,
it's not added to the list.

This came up because there was a BZ filed against libvirt's iface-name
command (which calls ncf_lookup_by_mac_string()) - it gave an error
when trying to lookup an interface with a mac address of
00:00:00:00:00:00:

  https://bugzilla.redhat.com/show_bug.cgi?id=580348

iface-name will log an error if there is more than one interface
matching the given mac address; in this case, lo (which is configured
with an ifcfg file) shows up in the list of all interfaces with a mac
of 00:00:00:00:00:00. However, libvirt's virbr0 (which *isn't* in the
list of all interfaces, because it has no ifcfg file) also matches
this MAC. That led libvirt to give an error when it apparently
shouldn't. Although that doesn't seem to be an important bug to fix,
it did reveal this inconsistency in listing of interfaces (if an
interface shows up when looking in one way, it should show up when
looking in other ways as well.)

An alternate fix to this would be to enhance libvirt to report
multiple interfaces in the iface-name command, and then add support
for transient interfaces to all the rest of the netcf API for
consistency's sake. That seems a bit drastic, though.
---
 src/drv_initscripts.c |   14 ++++++++++++++
 1 files changed, 14 insertions(+), 0 deletions(-)

diff --git a/src/drv_initscripts.c b/src/drv_initscripts.c
index 6cfbbd4..ceba52c 100644
--- a/src/drv_initscripts.c
+++ b/src/drv_initscripts.c
@@ -121,6 +121,18 @@ static int is_slave(struct netcf *ncf, const char *intf) {
     return 0;
 }
 
+static bool has_ifcfg_file(struct netcf *ncf, const char *name) {
+    int nmatches;
+
+    nmatches = aug_fmt_match(ncf, NULL,
+                             "%s[ DEVICE = '%s'"
+                             "    or BRIDGE = '%s'"
+                             "    or MASTER = '%s'"
+                             "    or MASTER = ../*[BRIDGE = '%s']/DEVICE ]/DEVICE",
+                             ifcfg_path, name, name, name, name);
+    return nmatches > 0;
+}
+
 static int cmpstrp(const void *p1, const void *p2) {
     const char *s1 = * (const char **)p1;
     const char *s2 = * (const char **)p2;
@@ -1087,6 +1099,8 @@ int drv_lookup_by_mac_string(struct netcf *ncf, const char *mac,
 
     int cnt = 0;
     for (int i = 0; i < nmatches; i++) {
+        if (!has_ifcfg_file(ncf, matches[i]))
+            continue;
         r = xasprintf(&ifcfg, "%s[DEVICE = '%s']", ifcfg_path, matches[i]);
         ERR_NOMEM(r < 0, ncf);
 
-- 
1.6.6.1



More information about the netcf-devel mailing list