teknoraver / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone

Blame SOURCES/0476-udev-introduce-udev-net_id-naming-schemes.patch

bd1529
From 08ac9f7f55c138678c6415139e7510a05a75b81d Mon Sep 17 00:00:00 2001
bd1529
From: =?UTF-8?q?Michal=20Sekleta=CC=81r?= <msekleta@redhat.com>
bd1529
Date: Wed, 14 Oct 2020 16:57:44 +0200
bd1529
Subject: [PATCH] udev: introduce udev net_id "naming schemes"
bd1529
MIME-Version: 1.0
bd1529
Content-Type: text/plain; charset=UTF-8
bd1529
Content-Transfer-Encoding: 8bit
bd1529
bd1529
With this we can stabilize how naming works for network interfaces. A
bd1529
user can request through a kernel cmdline option or an env var which
bd1529
scheme to follow. The idea is that installers use this to set into stone
bd1529
(a very soft stone though) the scheme used during installation so that
bd1529
interface naming doesn't change afterwards anymore.
bd1529
bd1529
Why use env vars and kernel cmdline options, and not a config file of
bd1529
its own?
bd1529
bd1529
Well, first of all there's no obvious existing one to use. But more
bd1529
importantly: I have the feeling that this logic is kind of an incomplete
bd1529
hack, and I simply don't want to do advertise this as a perfectly
bd1529
working solution. So far we used env vars for the non-so-official
bd1529
options and proper config files for the official stuff. Given how
bd1529
incomplete this logic is (i.e. the big variable for naming remains the
bd1529
kernel, which might expose sysfs attributes in newer versions that we
bd1529
check for and didn't exist in older versions — and other problems like
bd1529
this), I am simply not confident in giving this first-class exposure in
bd1529
a primary configuration file.
bd1529
bd1529
Fixes: #10448
bd1529
bd1529
(cherry-picked from commit f7e81fd96fdfe0ac6dcdb72de43f7cb4720e363a)
bd1529
bd1529
Related: #1827462
bd1529
bd1529
[msekleta: note that we are introducing our own naming schemes based on
bd1529
RHEL-8 minor versions. Also we are not backporting all naming scheme
bd1529
features that appeared in the original commit. We are backporting only
bd1529
features relevant for v239 while original commit also converted
bd1529
changes introduced in v240 into naming scheme flags.]
bd1529
---
bd1529
 doc/ENVIRONMENT.md             |   9 +++
bd1529
 man/kernel-command-line.xml    |   1 +
bd1529
 man/systemd-udevd.service.xml  |  16 +++++
bd1529
 src/udev/udev-builtin-net_id.c | 106 ++++++++++++++++++++++++++++++++-
bd1529
 4 files changed, 130 insertions(+), 2 deletions(-)
bd1529
bd1529
diff --git a/doc/ENVIRONMENT.md b/doc/ENVIRONMENT.md
bd1529
index 39a36a52cc..1a4aa01ef4 100644
bd1529
--- a/doc/ENVIRONMENT.md
bd1529
+++ b/doc/ENVIRONMENT.md
bd1529
@@ -76,6 +76,15 @@ systemd-logind:
bd1529
   hibernation is available even if the swap devices do not provide enough room
bd1529
   for it.
bd1529
 
bd1529
+* `$NET_NAMING_SCHEME=` – if set, takes a network naming scheme (i.e. one of
bd1529
+  v238, v239, v240 …) as parameter. If specified udev's net_id builtin will
bd1529
+  follow the specified naming scheme when determining stable network interface
bd1529
+  names. This may be used to revert to naming schemes of older udev versions,
bd1529
+  in order to provide more stable naming across updates. This environment
bd1529
+  variable takes precedence over the kernel command line option
bd1529
+  `net.naming-scheme=`, except if the value is prefixed with `:` in which case
bd1529
+  the kernel command line option takes precedence, if it is specified as well.
bd1529
+
bd1529
 installed systemd tests:
bd1529
 
bd1529
 * `$SYSTEMD_TEST_DATA` — override the location of test data. This is useful if
bd1529
diff --git a/man/kernel-command-line.xml b/man/kernel-command-line.xml
bd1529
index 4d8cb4e50e..b753d0592c 100644
bd1529
--- a/man/kernel-command-line.xml
bd1529
+++ b/man/kernel-command-line.xml
bd1529
@@ -246,6 +246,7 @@
bd1529
         <term><varname>udev.event_timeout=</varname></term>
bd1529
         <term><varname>rd.udev.event_timeout=</varname></term>
bd1529
         <term><varname>net.ifnames=</varname></term>
bd1529
+        <term><varname>net.naming-scheme=</varname></term>
bd1529
 
bd1529
         <listitem>
bd1529
           <para>Parameters understood by the device event managing
bd1529
diff --git a/man/systemd-udevd.service.xml b/man/systemd-udevd.service.xml
bd1529
index 73c77ea690..6449103441 100644
bd1529
--- a/man/systemd-udevd.service.xml
bd1529
+++ b/man/systemd-udevd.service.xml
bd1529
@@ -170,6 +170,22 @@
bd1529
           when possible. It is enabled by default; specifying 0 disables it.</para>
bd1529
         </listitem>
bd1529
       </varlistentry>
bd1529
+      <varlistentry>
bd1529
+        <term><varname>net.naming-scheme=</varname></term>
bd1529
+        <listitem>
bd1529
+          <para>Network interfaces are renamed to give them predictable names when possible (unless
bd1529
+          <varname>net.ifnames=0</varname> is specified, see above). The names are derived from various device metadata
bd1529
+          fields. Newer versions of <filename>systemd-udevd.service</filename> take more of these fields into account,
bd1529
+          improving (and thus possibly changing) the names used for the same devices. With this kernel command line
bd1529
+          option it is possible to pick a specific version of this algorithm. It expects a naming scheme identifier as
bd1529
+          argument. Currently the following identifiers are known: <literal>v238</literal>, <literal>v239</literal>,
bd1529
+          <literal>v240</literal> which each implement the naming scheme that was the default in the indicated systemd
bd1529
+          version. Note that selecting a specific scheme is not sufficient to fully stabilize interface naming: the
bd1529
+          naming is generally derived from driver attributes exposed by the kernel. As the kernel is updated,
bd1529
+          previously missing attributes <filename>systemd-udevd.service</filename> is checking might appear, which
bd1529
+          affects older name derivation algorithms, too.</para>
bd1529
+        </listitem>
bd1529
+      </varlistentry>
bd1529
     </variablelist>
bd1529
     
bd1529
          in kernel-command-line.xml -->
bd1529
diff --git a/src/udev/udev-builtin-net_id.c b/src/udev/udev-builtin-net_id.c
bd1529
index 147e04ab8c..148696183e 100644
bd1529
--- a/src/udev/udev-builtin-net_id.c
bd1529
+++ b/src/udev/udev-builtin-net_id.c
bd1529
@@ -96,6 +96,7 @@
bd1529
 #include "fileio.h"
bd1529
 #include "fs-util.h"
bd1529
 #include "parse-util.h"
bd1529
+#include "proc-cmdline.h"
bd1529
 #include "stdio-util.h"
bd1529
 #include "string-util.h"
bd1529
 #include "udev.h"
bd1529
@@ -103,6 +104,52 @@
bd1529
 
bd1529
 #define ONBOARD_INDEX_MAX (16*1024-1)
bd1529
 
bd1529
+/* So here's the deal: net_id is supposed to be an excercise in providing stable names for network devices. However, we
bd1529
+ * also want to keep updating the naming scheme used in future versions of net_id. These two goals of course are
bd1529
+ * contradictory: on one hand we want things to not change and on the other hand we want them to improve. Our way out
bd1529
+ * of this dilemma is to introduce the "naming scheme" concept: each time we improve the naming logic we define a new
bd1529
+ * flag for it. Then, we keep a list of schemes, each identified by a name associated with the flags it implements. Via
bd1529
+ * a kernel command line and environment variable we then allow the user to pick the scheme they want us to follow:
bd1529
+ * installers could "freeze" the used scheme at the moment of installation this way.
bd1529
+ *
bd1529
+ * Developers: each time you tweak the naming logic here, define a new flag below, and condition the tweak with
bd1529
+ * it. Each time we do a release we'll then add a new scheme entry and include all newly defined flags.
bd1529
+ *
bd1529
+ * Note that this is only half a solution to the problem though: not only udev/net_id gets updated all the time, the
bd1529
+ * kernel gets too. And thus a kernel that previously didn't expose some sysfs attribute we look for might eventually
bd1529
+ * do, and thus affect our naming scheme too. Thus, enforcing a naming scheme will make interfacing more stable across
bd1529
+ * OS versions, but not fully stabilize them. */
bd1529
+typedef enum NamingSchemeFlags {
bd1529
+        /* First, the individual features */
bd1529
+        NAMING_SR_IOV_V        = 1 << 0, /* Use "v" suffix for SR-IOV, see 609948c7043a40008b8299529c978ed8e11de8f6*/
bd1529
+        NAMING_NPAR_ARI        = 1 << 1, /* Use NPAR "ARI", see 6bc04997b6eab35d1cb9fa73889892702c27be09 */
bd1529
+
bd1529
+        /* And now the masks that combine the features above */
bd1529
+        NAMING_V238 = 0,
bd1529
+        NAMING_V239 = NAMING_V238|NAMING_SR_IOV_V|NAMING_NPAR_ARI,
bd1529
+        NAMING_RHEL_8_0 = NAMING_V239,
bd1529
+        NAMING_RHEL_8_1 = NAMING_V239,
bd1529
+        NAMING_RHEL_8_2 = NAMING_V239,
bd1529
+        NAMING_RHEL_8_3 = NAMING_V239,
bd1529
+
bd1529
+        _NAMING_SCHEME_FLAGS_INVALID = -1,
bd1529
+} NamingSchemeFlags;
bd1529
+
bd1529
+typedef struct NamingScheme {
bd1529
+        const char *name;
bd1529
+        NamingSchemeFlags flags;
bd1529
+} NamingScheme;
bd1529
+
bd1529
+static const NamingScheme naming_schemes[] = {
bd1529
+        { "v238", NAMING_V238 },
bd1529
+        { "v239", NAMING_V239 },
bd1529
+        { "rhel-8.0", NAMING_RHEL_8_0 },
bd1529
+        { "rhel-8.1", NAMING_RHEL_8_1 },
bd1529
+        { "rhel-8.2", NAMING_RHEL_8_2 },
bd1529
+        { "rhel-8.3", NAMING_RHEL_8_3 },
bd1529
+        /* … add more schemes here, as the logic to name devices is updated … */
bd1529
+};
bd1529
+
bd1529
 enum netname_type{
bd1529
         NET_UNDEF,
bd1529
         NET_PCI,
bd1529
@@ -138,6 +185,56 @@ struct virtfn_info {
bd1529
         char suffix[IFNAMSIZ];
bd1529
 };
bd1529
 
bd1529
+static const NamingScheme* naming_scheme(void) {
bd1529
+        static const NamingScheme *cache = NULL;
bd1529
+        _cleanup_free_ char *buffer = NULL;
bd1529
+        const char *e, *k;
bd1529
+
bd1529
+        if (cache)
bd1529
+                return cache;
bd1529
+
bd1529
+        /* Acquire setting from the kernel command line */
bd1529
+        (void) proc_cmdline_get_key("net.naming-scheme", 0, &buffer);
bd1529
+
bd1529
+        /* Also acquire it from an env var */
bd1529
+        e = getenv("NET_NAMING_SCHEME");
bd1529
+        if (e) {
bd1529
+                if (*e == ':') {
bd1529
+                        /* If prefixed with ':' the kernel cmdline takes precedence */
bd1529
+                        k = buffer ?: e + 1;
bd1529
+                } else
bd1529
+                        k = e; /* Otherwise the env var takes precedence */
bd1529
+        } else
bd1529
+                k = buffer;
bd1529
+
bd1529
+        if (k) {
bd1529
+                size_t i;
bd1529
+
bd1529
+                for (i = 0; i < ELEMENTSOF(naming_schemes); i++)
bd1529
+                        if (streq(naming_schemes[i].name, k)) {
bd1529
+                                cache = naming_schemes + i;
bd1529
+                                break;
bd1529
+                        }
bd1529
+
bd1529
+                if (!cache)
bd1529
+                        log_warning("Unknown interface naming scheme '%s' requested, ignoring.", k);
bd1529
+        }
bd1529
+
bd1529
+        if (cache)
bd1529
+                log_info("Using interface naming scheme '%s'.", cache->name);
bd1529
+        else {
bd1529
+                /* RHEL-only: here we differ from the upstream and if no naming scheme was selected we default to naming from systemd-239 */
bd1529
+                cache = &naming_schemes[2];
bd1529
+                log_info("Using default interface naming scheme '%s'.", cache->name);
bd1529
+        }
bd1529
+
bd1529
+        return cache;
bd1529
+}
bd1529
+
bd1529
+static bool naming_scheme_has(NamingSchemeFlags flags) {
bd1529
+        return FLAGS_SET(naming_scheme()->flags, flags);
bd1529
+}
bd1529
+
bd1529
 /* skip intermediate virtio devices */
bd1529
 static struct udev_device *skip_virtio(struct udev_device *dev) {
bd1529
         struct udev_device *parent = dev;
bd1529
@@ -299,7 +396,9 @@ static int dev_pci_slot(struct udev_device *dev, struct netnames *names) {
bd1529
 
bd1529
         if (sscanf(udev_device_get_sysname(names->pcidev), "%x:%x:%x.%u", &domain, &bus, &slot, &func) != 4)
bd1529
                 return -ENOENT;
bd1529
-        if (is_pci_ari_enabled(names->pcidev))
bd1529
+
bd1529
+        if (naming_scheme_has(NAMING_NPAR_ARI) &&
bd1529
+            is_pci_ari_enabled(names->pcidev))
bd1529
                 /* ARI devices support up to 256 functions on a single device ("slot"), and interpret the
bd1529
                  * traditional 5-bit slot and 3-bit function number as a single 8-bit function number,
bd1529
                  * where the slot makes up the upper 5 bits. */
bd1529
@@ -494,7 +593,8 @@ static int names_pci(struct udev_device *dev, struct netnames *names) {
bd1529
                         return -ENOENT;
bd1529
         }
bd1529
 
bd1529
-        if (get_virtfn_info(dev, names, &vf_info) >= 0) {
bd1529
+        if (naming_scheme_has(NAMING_SR_IOV_V) &&
bd1529
+            get_virtfn_info(dev, names, &vf_info) >= 0) {
bd1529
                 /* If this is an SR-IOV virtual device, get base name using physical device and add virtfn suffix. */
bd1529
                 vf_names.pcidev = vf_info.physfn_pcidev;
bd1529
                 dev_pci_onboard(dev, &vf_names);
bd1529
@@ -741,6 +841,8 @@ static int builtin_net_id(struct udev_device *dev, int argc, char *argv[], bool
bd1529
                         prefix = "ww";
bd1529
         }
bd1529
 
bd1529
+        udev_builtin_add_property(dev, test, "ID_NET_NAMING_SCHEME", naming_scheme()->name);
bd1529
+
bd1529
         err = names_mac(dev, &names);
bd1529
         if (err >= 0 && names.mac_valid) {
bd1529
                 char str[IFNAMSIZ];