]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
udev: net_id: introduce predictable names for xen-netfront
authorBenjamin Herrenschmidt <benh@amazon.com>
Wed, 20 Oct 2021 04:18:59 +0000 (15:18 +1100)
committerLennart Poettering <lennart@poettering.net>
Thu, 28 Oct 2021 11:09:00 +0000 (13:09 +0200)
Those devices show up as /sys/devices/vif-N, let's use that number
to name them enXN.

Without this, all schemes fail and they keep the kernel names, which can
be racy.

Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
man/systemd.net-naming-scheme.xml
src/shared/netif-naming-scheme.c
src/shared/netif-naming-scheme.h
src/udev/udev-builtin-net_id.c

index 6c14810a3e21cc897af498bf9ad627941110472d..b9badee1000cc6a06eb3a78c2c33e11750d62cfd 100644 (file)
         <varlistentry>
           <term><varname>ID_NET_NAME_SLOT=<replaceable>prefix</replaceable>[<constant>P</constant><replaceable>domain</replaceable>]<constant>s</constant><replaceable>slot</replaceable>[<constant>f</constant><replaceable>function</replaceable>][<constant>n</constant><replaceable>port_name</replaceable>|<constant>d</constant><replaceable>dev_port</replaceable>]</varname></term>
           <term><varname>ID_NET_NAME_SLOT=<replaceable>prefix</replaceable><constant>v</constant><replaceable>slot</replaceable></varname></term>
+          <term><varname>ID_NET_NAME_SLOT=<replaceable>prefix</replaceable><constant>x</constant><replaceable>slot</replaceable></varname></term>
           <term><varname>ID_NET_NAME_SLOT=<replaceable>prefix</replaceable>[<constant>P</constant><replaceable>domain</replaceable>]<constant>s</constant><replaceable>slot</replaceable>[<constant>f</constant><replaceable>function</replaceable>][<constant>n</constant><replaceable>port_name</replaceable>|<constant>d</constant><replaceable>dev_port</replaceable>]<constant>b</constant><replaceable>number</replaceable></varname></term>
           <term><varname>ID_NET_NAME_SLOT=<replaceable>prefix</replaceable>[<constant>P</constant><replaceable>domain</replaceable>]<constant>s</constant><replaceable>slot</replaceable>[<constant>f</constant><replaceable>function</replaceable>][<constant>n</constant><replaceable>port_name</replaceable>|<constant>d</constant><replaceable>dev_port</replaceable>]<constant>u</constant><replaceable>port</replaceable>…[<constant>c</constant><replaceable>config</replaceable>][<constant>i</constant><replaceable>interface</replaceable>]</varname></term>
           <term><varname>ID_NET_NAME_SLOT=<replaceable>prefix</replaceable>[<constant>P</constant><replaceable>domain</replaceable>]<constant>s</constant><replaceable>slot</replaceable>[<constant>f</constant><replaceable>function</replaceable>][<constant>n</constant><replaceable>port_name</replaceable>|<constant>d</constant><replaceable>dev_port</replaceable>]<constant>v</constant><replaceable>slot</replaceable></varname></term>
                   <entry>VIO slot number (IBM PowerVM)</entry>
                 </row>
 
+                <row>
+                  <entry><replaceable>prefix</replaceable> <constant>X</constant><replaceable>number</replaceable></entry>
+                  <entry>VIF interface number (Xen)</entry>
+                </row>
+
                 <row>
                   <entry>… <constant>b</constant><replaceable>number</replaceable></entry>
                   <entry>Broadcom bus (BCMA) core number</entry>
           </listitem>
         </varlistentry>
 
+        <varlistentry>
+          <term><constant>v250</constant></term>
+
+          <listitem><para>Added naming scheme for Xen netfront "vif" interfaces based on the guest side
+          VIF number set from the Xen config (or the interface index in AWS EC2).</para>
+          </listitem>
+        </varlistentry>
+
       </variablelist>
 
     <para>Note that <constant>latest</constant> may be used to denote the latest scheme known (to this
index d949f7381a8526a2f9a7ffb5335ccf82cb6469f9..8d03e0e5e759e3d4f157657671ca7eaf591510cc 100644 (file)
@@ -21,6 +21,7 @@ static const NamingScheme naming_schemes[] = {
         { "v245", NAMING_V245 },
         { "v247", NAMING_V247 },
         { "v249", NAMING_V249 },
+        { "v250", NAMING_V250 },
         /* … add more schemes here, as the logic to name devices is updated … */
 
         EXTRA_NET_NAMING_MAP
index 0141a4fb90af9b938027adf40ddf8452d8f44ed2..11ce6e62ee3362ee8465517bf0ad9b10eea6784b 100644 (file)
@@ -35,6 +35,7 @@ typedef enum NamingSchemeFlags {
         NAMING_SLOT_FUNCTION_ID    = 1 << 10, /* Use function_id if present to identify PCI hotplug slots */
         NAMING_16BIT_INDEX         = 1 << 11, /* Allow full 16-bit for the onboard index */
         NAMING_REPLACE_STRICTLY    = 1 << 12, /* Use udev_replace_ifname() for NAME= rule */
+        NAMING_XEN_VIF             = 1 << 13, /* GEnerate names for Xen netfront devices */
 
         /* And now the masks that combine the features above */
         NAMING_V238 = 0,
@@ -45,6 +46,7 @@ typedef enum NamingSchemeFlags {
         NAMING_V245 = NAMING_V243 | NAMING_NSPAWN_LONG_HASH,
         NAMING_V247 = NAMING_V245 | NAMING_BRIDGE_NO_SLOT,
         NAMING_V249 = NAMING_V247 | NAMING_SLOT_FUNCTION_ID | NAMING_16BIT_INDEX | NAMING_REPLACE_STRICTLY,
+        NAMING_V250 = NAMING_V249 | NAMING_XEN_VIF,
 
         EXTRA_NET_NAMING_SCHEMES
 
index 7c404d783c5a99df981a64dd20fbeae08e345d49..c52f796361c0775e15aeedfa9af549f35668a514 100644 (file)
@@ -49,6 +49,7 @@ typedef enum NetNameType {
         NET_VIRTIO,
         NET_CCW,
         NET_VIO,
+        NET_XENVIF,
         NET_PLATFORM,
         NET_NETDEVSIM,
 } NetNameType;
@@ -66,6 +67,7 @@ typedef struct NetNames {
         char bcma_core[ALTIFNAMSIZ];
         char ccw_busid[ALTIFNAMSIZ];
         char vio_slot[ALTIFNAMSIZ];
+        char xen_slot[ALTIFNAMSIZ];
         char platform_path[ALTIFNAMSIZ];
         char netdevsim_path[ALTIFNAMSIZ];
 } NetNames;
@@ -813,6 +815,59 @@ static int names_netdevsim(sd_device *dev, const LinkInfo *info, NetNames *names
         return 0;
 }
 
+static int names_xen(sd_device *dev, NetNames *names) {
+        sd_device *parent;
+        unsigned id;
+        const char *syspath, *subsystem, *p, *p2;
+        int r;
+
+        assert(dev);
+        assert(names);
+
+        if (!naming_scheme_has(NAMING_XEN_VIF))
+                return 0;
+
+        /* check if our direct parent is a Xen VIF device with no other bus in-between */
+        r = sd_device_get_parent(dev, &parent);
+        if (r < 0)
+                return r;
+
+        /* Do an exact-match on subsystem "xen". This will miss on "xen-backend" on
+         * purpose as the VIFs on the backend (dom0) have their own naming scheme
+         * which we don't want to affect
+         */
+        r = sd_device_get_subsystem(parent, &subsystem);
+        if (r < 0)
+                return r;
+        if (!streq("xen", subsystem))
+                return -ENOENT;
+
+        /* Use the vif-n name to extract "n" */
+        r = sd_device_get_syspath(dev, &syspath);
+        if (r < 0)
+                return r;
+
+        p = path_startswith(syspath, "/sys/devices/");
+        if (!p)
+                return -ENOENT;
+        p = startswith(p, "vif-");
+        if (!p)
+                return -ENOENT;
+        p2 = strchr(p, '/');
+        if (!p2)
+                return -ENOENT;
+        p = strndupa_safe(p, p2 - p);
+        if (!p)
+                return -ENOENT;
+        r = safe_atou_full(p, SAFE_ATO_REFUSE_PLUS_MINUS | SAFE_ATO_REFUSE_LEADING_ZERO |
+                           SAFE_ATO_REFUSE_LEADING_WHITESPACE | 10, &id);
+        if (r < 0)
+                return r;
+        xsprintf(names->xen_slot, "X%u", id);
+        names->type = NET_XENVIF;
+        return 0;
+}
+
 /* IEEE Organizationally Unique Identifier vendor string */
 static int ieee_oui(sd_device *dev, const LinkInfo *info, bool test) {
         char str[32];
@@ -945,6 +1000,15 @@ static int builtin_net_id(sd_device *dev, sd_netlink **rtnl, int argc, char *arg
                 return 0;
         }
 
+        /* get xen vif "slot" based names. */
+        if (names_xen(dev, &names) >= 0 && names.type == NET_XENVIF) {
+                char str[ALTIFNAMSIZ];
+
+                if (snprintf_ok(str, sizeof str, "%s%s", prefix, names.xen_slot))
+                        udev_builtin_add_property(dev, test, "ID_NET_NAME_SLOT", str);
+                return 0;
+        }
+
         /* get PCI based path names, we compose only PCI based paths */
         if (names_pci(dev, &info, &names) < 0)
                 return 0;