[systemd-commits] 2 commits - links/99-default.link Makefile.am rules/80-net-name-slot.rules src/shared src/udev

Tom Gundersen tomegun at kemper.freedesktop.org
Sun Oct 27 17:20:34 PDT 2013


 Makefile.am                          |    6 ++-
 links/99-default.link                |    2 +
 rules/80-net-name-slot.rules         |   14 -------
 src/shared/util.c                    |    2 -
 src/udev/net/link-config-gperf.gperf |    1 
 src/udev/net/link-config.c           |   69 ++++++++++++++++++++++++++++++++---
 src/udev/net/link-config.h           |    1 
 7 files changed, 74 insertions(+), 21 deletions(-)

New commits:
commit daeb71a36a98834664e4d95773a3629b746f4db8
Author: Tom Gundersen <teg at jklm.no>
Date:   Mon Oct 28 00:19:59 2013 +0100

    udev: link-config - move naming policy from udev rules
    
    This introduces a new key NamePolicy, which takes an ordered list of naming
    policies. The first successful one is applide. If all fail the value of Name
    (if any) is used.
    
    The possible policies are 'onboard', 'slot', 'path' and 'mac'.
    
    This patch introduces a default link file, which replaces the equivalent udev
    rule.

diff --git a/Makefile.am b/Makefile.am
index e5ed302..4a96a50 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -83,7 +83,7 @@ userunitdir=$(prefix)/lib/systemd/user
 userpresetdir=$(prefix)/lib/systemd/user-preset
 tmpfilesdir=$(prefix)/lib/tmpfiles.d
 sysctldir=$(prefix)/lib/sysctl.d
-linkdir=$(prefix)/lib/net/links
+linksdir=$(prefix)/lib/net/links
 pkgincludedir=$(includedir)/systemd
 systemgeneratordir=$(rootlibexecdir)/system-generators
 usergeneratordir=$(prefix)/lib/systemd/user-generators
@@ -2214,6 +2214,9 @@ INSTALL_DIRS += \
 	$(sysconfdir)/udev/rules.d \
 	$(sysconfdir)/udev/hwdb.d
 
+dist_links_DATA = \
+	links/99-default.link
+
 dist_udevrules_DATA += \
 	rules/99-systemd.rules \
 	rules/42-usb-hid-pm.rules \
@@ -2228,7 +2231,6 @@ dist_udevrules_DATA += \
 	rules/75-net-description.rules \
 	rules/75-tty-description.rules \
 	rules/78-sound-card.rules \
-	rules/80-net-name-slot.rules \
 	rules/85-net-configure-link.rules \
 	rules/95-udev-late.rules
 
diff --git a/links/99-default.link b/links/99-default.link
new file mode 100644
index 0000000..c5043b8
--- /dev/null
+++ b/links/99-default.link
@@ -0,0 +1,2 @@
+[Link]
+NamePolicy=onboard slot path
diff --git a/rules/80-net-name-slot.rules b/rules/80-net-name-slot.rules
deleted file mode 100644
index c5f1b38..0000000
--- a/rules/80-net-name-slot.rules
+++ /dev/null
@@ -1,14 +0,0 @@
-# do not edit this file, it will be overwritten on update
-
-ACTION!="add", GOTO="net_name_slot_end"
-SUBSYSTEM!="net", GOTO="net_name_slot_end"
-NAME!="", GOTO="net_name_slot_end"
-
-IMPORT{cmdline}="net.ifnames"
-ENV{net.ifnames}=="0", GOTO="net_name_slot_end"
-
-NAME=="", ENV{ID_NET_NAME_ONBOARD}!="", NAME="$env{ID_NET_NAME_ONBOARD}"
-NAME=="", ENV{ID_NET_NAME_SLOT}!="", NAME="$env{ID_NET_NAME_SLOT}"
-NAME=="", ENV{ID_NET_NAME_PATH}!="", NAME="$env{ID_NET_NAME_PATH}"
-
-LABEL="net_name_slot_end"
diff --git a/src/shared/util.c b/src/shared/util.c
index 2419a76..ef3b67b 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -6004,7 +6004,7 @@ bool restore_state(void) {
         r = read_one_line_file("/proc/cmdline", &line);
         if (r < 0) {
                 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
-                return 0;
+                return true; /* something is very wrong, let's not make it worse */
         }
 
         FOREACH_WORD_QUOTED(w, l, line, state)
diff --git a/src/udev/net/link-config-gperf.gperf b/src/udev/net/link-config-gperf.gperf
index 182a664..b38dbe8 100644
--- a/src/udev/net/link-config-gperf.gperf
+++ b/src/udev/net/link-config-gperf.gperf
@@ -20,6 +20,7 @@ Match.Driver,                       config_parse_string,        0, offsetof(link
 Match.Type,                         config_parse_string,        0, offsetof(link_config, match_type)
 Link.Description,                   config_parse_string,        0, offsetof(link_config, description)
 Link.MACAddress,                    config_parse_string,        0, offsetof(link_config, mac)
+Link.NamePolicy,                    config_parse_strv,          0, offsetof(link_config, name_policy)
 Link.Name,                          config_parse_string,        0, offsetof(link_config, name)
 Link.MTU,                           config_parse_unsigned,      0, offsetof(link_config, mtu)
 Link.SpeedMBytes,                   config_parse_unsigned,      0, offsetof(link_config, speed)
diff --git a/src/udev/net/link-config.c b/src/udev/net/link-config.c
index bb06405..9c75b62 100644
--- a/src/udev/net/link-config.c
+++ b/src/udev/net/link-config.c
@@ -34,6 +34,7 @@
 #include "path-util.h"
 #include "conf-parser.h"
 #include "conf-files.h"
+#include "fileio.h"
 
 struct link_config_ctx {
         LIST_HEAD(link_config, links);
@@ -308,8 +309,27 @@ static int rtnl_set_properties(sd_rtnl *rtnl, int ifindex, const char *name, con
         return 0;
 }
 
+static bool enable_name_policy(void) {
+        _cleanup_free_ char *line;
+        char *w, *state;
+        int r;
+        size_t l;
+
+        r = read_one_line_file("/proc/cmdline", &line);
+        if (r < 0) {
+                log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
+                return true; /* something is very wrong, let's not make it worse */
+        }
+
+        FOREACH_WORD_QUOTED(w, l, line, state)
+                if (strneq(w, "net.ifnames=0", l))
+                        return false;
+
+        return true;
+}
+
 int link_config_apply(link_config_ctx *ctx, link_config *config, struct udev_device *device) {
-        const char *name;
+        const char *name, *new_name = NULL;
         int r, ifindex;
 
         name = udev_device_get_sysname(device);
@@ -355,7 +375,35 @@ int link_config_apply(link_config_ctx *ctx, link_config *config, struct udev_dev
                 return -ENODEV;
         }
 
-        r = rtnl_set_properties(ctx->rtnl, ifindex, config->name, config->mac, config->mtu);
+        if (config->name_policy && enable_name_policy()) {
+                char **policy;
+
+                STRV_FOREACH(policy, config->name_policy) {
+                        if (streq(*policy, "onboard")) {
+                                new_name = udev_device_get_property_value(device, "ID_NET_NAME_ONBOARD");
+                                if (new_name)
+                                        break;
+                        } else if (streq(*policy, "slot")) {
+                                new_name = udev_device_get_property_value(device, "ID_NET_NAME_SLOT");
+                                if (new_name)
+                                        break;
+                        } else if (streq(*policy, "path")) {
+                                new_name = udev_device_get_property_value(device, "ID_NET_NAME_PATH");
+                                if (new_name)
+                                        break;
+                        } else if (streq(*policy, "mac")) {
+                                new_name = udev_device_get_property_value(device, "ID_NET_NAME_MAC");
+                                if (new_name)
+                                        break;
+                        } else
+                                log_warning("Invalid link naming policy '%s', ignoring.", *policy);
+                }
+        }
+
+        if (!new_name && config->name)
+                new_name = config->name;
+
+        r = rtnl_set_properties(ctx->rtnl, ifindex, new_name, config->mac, config->mtu);
         if (r < 0) {
                 log_warning("Could not set Name, MACAddress or MTU on %s", name);
                 return r;
diff --git a/src/udev/net/link-config.h b/src/udev/net/link-config.h
index 849e481..0f57dc9 100644
--- a/src/udev/net/link-config.h
+++ b/src/udev/net/link-config.h
@@ -39,6 +39,7 @@ struct link_config {
 
         char *description;
         char *mac;
+        char **name_policy;
         char *name;
         unsigned int mtu;
         unsigned int speed;

commit 2a73e0d39a9bec82c3800071e375d27164727e71
Author: Tom Gundersen <teg at jklm.no>
Date:   Sun Oct 27 23:09:05 2013 +0100

    udev: link-config - sanity check the ifname and mac address

diff --git a/src/udev/net/link-config.c b/src/udev/net/link-config.c
index 902ed09..bb06405 100644
--- a/src/udev/net/link-config.c
+++ b/src/udev/net/link-config.c
@@ -20,6 +20,7 @@
 ***/
 
 #include <netinet/ether.h>
+#include <net/if.h>
 
 #include "link-config.h"
 
@@ -252,6 +253,8 @@ int link_config_get(link_config_ctx *ctx, struct udev_device *device, link_confi
 
 static int rtnl_set_properties(sd_rtnl *rtnl, int ifindex, const char *name, const char *mac, unsigned int mtu) {
         _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *message;
+        char new_name[IFNAMSIZ];
+        struct ether_addr new_mac;
         bool need_update;
         int r;
 
@@ -263,7 +266,8 @@ static int rtnl_set_properties(sd_rtnl *rtnl, int ifindex, const char *name, con
                 return r;
 
         if (name) {
-                r = sd_rtnl_message_append(message, IFLA_IFNAME, name);
+                strscpy(new_name, IFNAMSIZ, name);
+                r = sd_rtnl_message_append(message, IFLA_IFNAME, new_name);
                 if (r < 0)
                         return r;
 
@@ -271,7 +275,16 @@ static int rtnl_set_properties(sd_rtnl *rtnl, int ifindex, const char *name, con
         }
 
         if (mac) {
-                r = sd_rtnl_message_append(message, IFLA_ADDRESS, ether_aton(mac));
+                r = sscanf(mac, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
+                           &new_mac.ether_addr_octet[0],
+                           &new_mac.ether_addr_octet[1],
+                           &new_mac.ether_addr_octet[2],
+                           &new_mac.ether_addr_octet[3],
+                           &new_mac.ether_addr_octet[4],
+                           &new_mac.ether_addr_octet[5]);
+                if (r != 6)
+                        return -EINVAL;
+                r = sd_rtnl_message_append(message, IFLA_ADDRESS, &new_mac);
                 if (r < 0)
                         return r;
 



More information about the systemd-commits mailing list