572a44
From 08f8572347ca597147ab761d425e4480885e1995 Mon Sep 17 00:00:00 2001
572a44
From: Hendrik Brueckner <brueckner@redhat.com>
572a44
Date: Thu, 9 Jan 2014 11:28:12 +0100
572a44
Subject: [PATCH] udev/net_id: Introduce predictable network names for Linux on
572a44
 System z
572a44
572a44
Use the bus-ID to create predicatable devices names for network interfaces
572a44
on Linux on System z instances.  The bus-ID identifies a device in the s390
572a44
channel subsystem.
572a44
572a44
Network interfaces of device type Ethernet are named as:
572a44
    enccw0.0.1234    (13 characters)
572a44
up to
572a44
    enccwff.7.ffff   (14 characters)
572a44
572a44
CTC network devices of device type SLIP, use a different prefix as follows:
572a44
    slccw0.0.1234    (13 characters)
572a44
572a44
See also Red Hat Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=870859
572a44
[tomegun: typical problem of netdevs switching names between reboots.]
572a44
572a44
Conflicts:
572a44
	src/udev/udev-builtin-net_id.c
572a44
---
572a44
 src/udev/udev-builtin-net_id.c | 64 ++++++++++++++++++++++++++++++++++++++++--
572a44
 1 file changed, 62 insertions(+), 2 deletions(-)
572a44
572a44
diff --git a/src/udev/udev-builtin-net_id.c b/src/udev/udev-builtin-net_id.c
572a44
index 9ae8f08..d9de46d 100644
572a44
--- a/src/udev/udev-builtin-net_id.c
572a44
+++ b/src/udev/udev-builtin-net_id.c
572a44
@@ -28,6 +28,7 @@
572a44
  *
572a44
  * Two character prefixes based on the type of interface:
572a44
  *   en -- ethernet
572a44
+ *   sl -- serial line IP (slip)
572a44
  *   wl -- wlan
572a44
  *   ww -- wwan
572a44
  *
572a44
@@ -101,6 +102,7 @@ enum netname_type{
572a44
         NET_PCI,
572a44
         NET_USB,
572a44
         NET_BCMA,
572a44
+        NET_CCWGROUP,
572a44
 };
572a44
 
572a44
 struct netnames {
572a44
@@ -118,6 +120,8 @@ struct netnames {
572a44
         char usb_ports[IFNAMSIZ];
572a44
 
572a44
         char bcma_core[IFNAMSIZ];
572a44
+
572a44
+        char ccw_core[IFNAMSIZ];
572a44
 };
572a44
 
572a44
 /* retrieve on-board index number and label from firmware */
572a44
@@ -351,6 +355,44 @@ static int names_bcma(struct udev_device *dev, struct netnames *names) {
572a44
         return 0;
572a44
 }
572a44
 
572a44
+static int names_ccw(struct  udev_device *dev, struct netnames *names) {
572a44
+        struct udev_device *cdev;
572a44
+        const char *bus_id;
572a44
+        size_t bus_id_len;
572a44
+        int rc;
572a44
+
572a44
+        /* Retrieve the associated CCW device */
572a44
+        cdev = udev_device_get_parent(dev);
572a44
+        if (!cdev)
572a44
+                return -ENOENT;
572a44
+
572a44
+        /* Network devices are always grouped CCW devices */
572a44
+        if (!streq_ptr("ccwgroup", udev_device_get_subsystem(cdev)))
572a44
+                return -ENOENT;
572a44
+
572a44
+        /* Retrieve bus-ID of the grouped CCW device.  The bus-ID uniquely
572a44
+         * identifies the network device on the Linux on System z channel
572a44
+         * subsystem.  Note that the bus-ID contains lowercase characters.
572a44
+         */
572a44
+        bus_id = udev_device_get_sysname(cdev);
572a44
+        if (!bus_id)
572a44
+                return -ENOENT;
572a44
+
572a44
+        /* Check the length of the bus-ID.  Rely on that the kernel provides
572a44
+         * a correct bus-ID; alternatively, improve this check and parse and
572a44
+         * verify each bus-ID part...
572a44
+         */
572a44
+        bus_id_len = strlen(bus_id);
572a44
+        if (!bus_id_len || bus_id_len < 8 || bus_id_len > 9)
572a44
+                return -EINVAL;
572a44
+
572a44
+        /* Store the CCW bus-ID for use as network device name */
572a44
+        rc = snprintf(names->ccw_core, sizeof(names->ccw_core), "ccw%s", bus_id);
572a44
+        if (rc >= 0 && rc < (int)sizeof(names->ccw_core))
572a44
+                names->type = NET_CCWGROUP;
572a44
+        return 0;
572a44
+}
572a44
+
572a44
 static int names_mac(struct udev_device *dev, struct netnames *names) {
572a44
         const char *s;
572a44
         unsigned int i;
572a44
@@ -409,13 +451,21 @@ static int builtin_net_id(struct udev_device *dev, int argc, char *argv[], bool
572a44
         struct netnames names = {};
572a44
         int err;
572a44
 
572a44
-        /* handle only ARPHRD_ETHER devices */
572a44
+        /* handle only ARPHRD_ETHER and ARPHRD_SLIP devices */
572a44
         s = udev_device_get_sysattr_value(dev, "type");
572a44
         if (!s)
572a44
                 return EXIT_FAILURE;
572a44
         i = strtoul(s, NULL, 0);
572a44
-        if (i != 1)
572a44
+        switch (i) {
572a44
+        case 1: /* ARPHRD_ETHER */
572a44
+                prefix = "en";
572a44
+                break;
572a44
+        case 256: /* ARPHRD_SLIP */
572a44
+                prefix = "sl";
572a44
+                break;
572a44
+        default:
572a44
                 return 0;
572a44
+        }
572a44
 
572a44
         /* skip stacked devices, like VLANs, ... */
572a44
         s = udev_device_get_sysattr_value(dev, "ifindex");
572a44
@@ -447,6 +497,16 @@ static int builtin_net_id(struct udev_device *dev, int argc, char *argv[], bool
572a44
                 ieee_oui(dev, &names, test);
572a44
         }
572a44
 
572a44
+        /* get path names for Linux on System z network devices */
572a44
+        err = names_ccw(dev, &names);
572a44
+        if (err >= 0 && names.type == NET_CCWGROUP) {
572a44
+                char str[IFNAMSIZ];
572a44
+
572a44
+                if (snprintf(str, sizeof(str), "%s%s", prefix, names.ccw_core) < (int)sizeof(str))
572a44
+                        udev_builtin_add_property(dev, test, "ID_NET_NAME_PATH", str);
572a44
+                goto out;
572a44
+        }
572a44
+
572a44
         /* get PCI based path names, we compose only PCI based paths */
572a44
         err = names_pci(dev, &names);
572a44
         if (err < 0)