chiark / gitweb /
udev: net_id - handle virtio buses
[elogind.git] / src / udev / udev-builtin-net_id.c
index b859a6b9d5c17751be16cedf3afa4a11ac1169bc..c3220565ed7232328a0e55aa1c0bd00aac6438f7 100644 (file)
@@ -101,6 +101,7 @@ enum netname_type{
         NET_PCI,
         NET_USB,
         NET_BCMA,
+        NET_VIRTIO,
 };
 
 struct netnames {
@@ -118,6 +119,8 @@ struct netnames {
         char usb_ports[IFNAMSIZ];
 
         char bcma_core[IFNAMSIZ];
+
+        char virtio_core[IFNAMSIZ];
 };
 
 /* retrieve on-board index number and label from firmware */
@@ -166,23 +169,17 @@ out:
 
 static int dev_pci_slot(struct udev_device *dev, struct netnames *names) {
         struct udev *udev = udev_device_get_udev(names->pcidev);
-        unsigned int domain;
-        unsigned int bus;
-        unsigned int slot;
-        unsigned int func;
-        unsigned int dev_id = 0;
+        unsigned domain, bus, slot, func, dev_id = 0;
         size_t l;
         char *s;
         const char *attr;
         struct udev_device *pci = NULL;
-        char slots[256];
-        DIR *dir;
+        char slots[256], str[256];
+        _cleanup_closedir_ DIR *dir = NULL;
         struct dirent *dent;
-        char str[256];
-        int hotplug_slot = 0;
-        int err = 0;
+        int hotplug_slot = 0, err = 0;
 
-        if (sscanf(udev_device_get_sysname(names->pcidev), "%x:%x:%x.%d", &domain, &bus, &slot, &func) != 4)
+        if (sscanf(udev_device_get_sysname(names->pcidev), "%x:%x:%x.%u", &domain, &bus, &slot, &func) != 4)
                 return -ENOENT;
 
         /* kernel provided multi-device index */
@@ -239,7 +236,6 @@ static int dev_pci_slot(struct udev_device *dev, struct netnames *names) {
                 if (hotplug_slot > 0)
                         break;
         }
-        closedir(dir);
 
         if (hotplug_slot > 0) {
                 s = names->pci_slot;
@@ -341,16 +337,35 @@ static int names_bcma(struct udev_device *dev, struct netnames *names) {
                 return -ENOENT;
 
         /* bus num:core num */
-        if (sscanf(udev_device_get_sysname(bcmadev), "bcma%*d:%d", &core) != 1)
+        if (sscanf(udev_device_get_sysname(bcmadev), "bcma%*u:%u", &core) != 1)
                 return -EINVAL;
         /* suppress the common core == 0 */
         if (core > 0)
-                snprintf(names->bcma_core, sizeof(names->bcma_core), "b%d", core);
+                snprintf(names->bcma_core, sizeof(names->bcma_core), "b%u", core);
 
         names->type = NET_BCMA;
         return 0;
 }
 
+static int names_virtio(struct udev_device *dev, struct netnames *names) {
+        struct udev_device *virtdev;
+        unsigned int core;
+
+        virtdev = udev_device_get_parent_with_subsystem_devtype(dev, "virtio", NULL);
+        if (!virtdev)
+                return -ENOENT;
+
+        /* core num */
+        if (sscanf(udev_device_get_sysname(virtdev), "virtio%u", &core) != 1)
+                return -EINVAL;
+        /* suppress the common core == 0 */
+        if (core > 0)
+                snprintf(names->virtio_core, sizeof(names->virtio_core), "v%u", core);
+
+        names->type = NET_VIRTIO;
+        return 0;
+}
+
 static int names_mac(struct udev_device *dev, struct netnames *names) {
         const char *s;
         unsigned int i;
@@ -504,6 +519,21 @@ static int builtin_net_id(struct udev_device *dev, int argc, char *argv[], bool
                 goto out;
         }
 
+        /* virtio bus */
+        err = names_virtio(dev, &names);
+        if (err >= 0 && names.type == NET_VIRTIO) {
+                char str[IFNAMSIZ];
+
+                if (names.pci_path[0])
+                        if (snprintf(str, sizeof(str), "%s%s%s", prefix, names.pci_path, names.virtio_core) < (int)sizeof(str))
+                                udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
+
+                if (names.pci_slot[0])
+                        if (snprintf(str, sizeof(str), "%s%s%s", prefix, names.pci_slot, names.virtio_core) < (int)sizeof(str))
+                                udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
+                goto out;
+        }
+
 out:
         return EXIT_SUCCESS;
 }