chiark / gitweb /
udev: link-config - let udevd set the ifname
authorTom Gundersen <teg@jklm.no>
Wed, 30 Oct 2013 03:10:19 +0000 (04:10 +0100)
committerTom Gundersen <teg@jklm.no>
Wed, 30 Oct 2013 14:36:04 +0000 (15:36 +0100)
Makefile.am
rules/80-net-setup-link.rules [moved from rules/85-net-configure-link.rules with 68% similarity]
src/libsystemd-rtnl/rtnl-util.c
src/libsystemd-rtnl/rtnl-util.h
src/udev/net/link-config.c
src/udev/net/link-config.h
src/udev/udev-builtin-net_setup_link.c
src/udev/udev-event.c

index 66bbf203a736ddc6660d74b95adc7333137406dd..bd7f577d4001a1dea48e00c1c85c46ddcc893a4e 100644 (file)
@@ -2234,7 +2234,7 @@ dist_udevrules_DATA += \
        rules/75-net-description.rules \
        rules/75-tty-description.rules \
        rules/78-sound-card.rules \
        rules/75-net-description.rules \
        rules/75-tty-description.rules \
        rules/78-sound-card.rules \
-       rules/85-net-configure-link.rules \
+       rules/80-net-setup-link.rules \
        rules/95-udev-late.rules
 
 dist_udevhwdb_DATA = \
        rules/95-udev-late.rules
 
 dist_udevhwdb_DATA = \
similarity index 68%
rename from rules/85-net-configure-link.rules
rename to rules/80-net-setup-link.rules
index d0ecb93058d88715cf46bb4f79a94b9793cfa32c..6e411a91f0ec9766c8351348825ca331863b3b5f 100644 (file)
@@ -6,6 +6,8 @@ IMPORT{builtin}="path_id"
 
 ACTION!="add", GOTO="net_setup_link_end"
 
 
 ACTION!="add", GOTO="net_setup_link_end"
 
-RUN{builtin}="net_setup_link"
+IMPORT{builtin}="net_setup_link"
+
+NAME=="", ENV{ID_NET_NAME}!="", NAME="$env{ID_NET_NAME}"
 
 LABEL="net_setup_link_end"
 
 LABEL="net_setup_link_end"
index bf6bf27bb45e036d781b4a385ddef9359f4ad762..93804bb74429ec6af002ba2f9521e45af97473c1 100644 (file)
 
 #include "rtnl-util.h"
 
 
 #include "rtnl-util.h"
 
-int rtnl_set_link_properties(sd_rtnl *rtnl, int ifindex, const char *name, const struct ether_addr *mac, unsigned mtu) {
-        _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *message;
-        bool need_update = false;
+int rtnl_set_link_name(sd_rtnl *rtnl, int ifindex, const char *name) {
+        _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *message = NULL;
         int r;
 
         assert(rtnl);
         assert(ifindex > 0);
         int r;
 
         assert(rtnl);
         assert(ifindex > 0);
+        assert(name);
 
         r = sd_rtnl_message_link_new(RTM_NEWLINK, ifindex, 0, 0, &message);
         if (r < 0)
                 return r;
 
 
         r = sd_rtnl_message_link_new(RTM_NEWLINK, ifindex, 0, 0, &message);
         if (r < 0)
                 return r;
 
-        if (name) {
-                r = sd_rtnl_message_append(message, IFLA_IFNAME, name);
-                if (r < 0)
-                        return r;
+        r = sd_rtnl_message_append(message, IFLA_IFNAME, name);
+        if (r < 0)
+                return r;
 
 
-                need_update = true;
-        }
+        r = sd_rtnl_send_with_reply_and_block(rtnl, message, 0, NULL);
+        if (r < 0)
+                return r;
+
+        return 0;
+}
+
+int rtnl_set_link_properties(sd_rtnl *rtnl, int ifindex, const struct ether_addr *mac, unsigned mtu) {
+        _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *message = NULL;
+        bool need_update = false;
+        int r;
+
+        assert(rtnl);
+        assert(ifindex > 0);
+
+        if (!mac && mtu == 0)
+                return 0;
+
+        r = sd_rtnl_message_link_new(RTM_NEWLINK, ifindex, 0, 0, &message);
+        if (r < 0)
+                return r;
 
         if (mac) {
                 r = sd_rtnl_message_append(message, IFLA_ADDRESS, mac);
 
         if (mac) {
                 r = sd_rtnl_message_append(message, IFLA_ADDRESS, mac);
index dd8300b8275a543ff4196bedaa2eca4fe3514c81..a6ac4b62c9f46cbb6f833704f39767bce073fa6b 100644 (file)
@@ -23,4 +23,5 @@
 
 #include "sd-rtnl.h"
 
 
 #include "sd-rtnl.h"
 
-int rtnl_set_link_properties(sd_rtnl *rtnl, int ifindex, const char *name, const struct ether_addr *mac, unsigned mtu);
+int rtnl_set_link_name(sd_rtnl *rtnl, int ifindex, const char *name);
+int rtnl_set_link_properties(sd_rtnl *rtnl, int ifindex, const struct ether_addr *mac, unsigned mtu);
index cf966bfe372f3d587adb69835ffeff992cff88dd..80f8b16f54b6c99cb8ddf0ccf4bf12fb7bca6a31 100644 (file)
@@ -280,8 +280,10 @@ static bool enable_name_policy(void) {
         }
 
         FOREACH_WORD_QUOTED(w, l, line, state)
         }
 
         FOREACH_WORD_QUOTED(w, l, line, state)
-                if (strneq(w, "net.ifnames=0", l))
+                if (strneq(w, "net.ifnames=0", l)) {
+                        log_info("Link name policy disabled on kernel commandline, ignoring.");
                         return false;
                         return false;
+                }
 
         return true;
 }
 
         return true;
 }
@@ -365,40 +367,45 @@ static int get_mac(struct udev_device *device, bool want_random, struct ether_ad
         return 0;
 }
 
         return 0;
 }
 
-int link_config_apply(link_config_ctx *ctx, link_config *config, struct udev_device *device) {
-        const char *name;
+int link_config_apply(link_config_ctx *ctx, link_config *config, struct udev_device *device, const char **name) {
+        const char *old_name;
         const char *new_name = NULL;
         struct ether_addr generated_mac;
         struct ether_addr *mac = NULL;
         int r, ifindex;
 
         const char *new_name = NULL;
         struct ether_addr generated_mac;
         struct ether_addr *mac = NULL;
         int r, ifindex;
 
+        assert(ctx);
+        assert(config);
+        assert(device);
+        assert(name);
+
         r = link_config_ctx_connect(ctx);
         if (r < 0)
                 return r;
 
         r = link_config_ctx_connect(ctx);
         if (r < 0)
                 return r;
 
-        name = udev_device_get_sysname(device);
-        if (!name)
+        old_name = udev_device_get_sysname(device);
+        if (!old_name)
                 return -EINVAL;
 
                 return -EINVAL;
 
-        log_info("Configuring %s", name);
+        log_info("Configuring %s", old_name);
 
         if (config->description) {
                 r = udev_device_set_sysattr_value(device, "ifalias",
                                                   config->description);
                 if (r < 0)
                         log_warning("Could not set description of %s to '%s': %s",
 
         if (config->description) {
                 r = udev_device_set_sysattr_value(device, "ifalias",
                                                   config->description);
                 if (r < 0)
                         log_warning("Could not set description of %s to '%s': %s",
-                                    name, config->description, strerror(-r));
+                                    old_name, config->description, strerror(-r));
         }
 
         }
 
-        r = ethtool_set_speed(ctx->ethtool_fd, name, config->speed, config->duplex);
+        r = ethtool_set_speed(ctx->ethtool_fd, old_name, config->speed, config->duplex);
         if (r < 0)
                 log_warning("Could not set speed or duplex of %s to %u Mbytes (%s): %s",
         if (r < 0)
                 log_warning("Could not set speed or duplex of %s to %u Mbytes (%s): %s",
-                             name, config->speed, duplex_to_string(config->duplex), strerror(-r));
+                             old_name, config->speed, duplex_to_string(config->duplex), strerror(-r));
 
 
-        r = ethtool_set_wol(ctx->ethtool_fd, name, config->wol);
+        r = ethtool_set_wol(ctx->ethtool_fd, old_name, config->wol);
         if (r < 0)
                 log_warning("Could not set WakeOnLan of %s to %s: %s",
         if (r < 0)
                 log_warning("Could not set WakeOnLan of %s to %s: %s",
-                            name, wol_to_string(config->wol), strerror(-r));
+                            old_name, wol_to_string(config->wol), strerror(-r));
 
         ifindex = udev_device_get_ifindex(device);
         if (ifindex <= 0) {
 
         ifindex = udev_device_get_ifindex(device);
         if (ifindex <= 0) {
@@ -429,9 +436,12 @@ int link_config_apply(link_config_ctx *ctx, link_config *config, struct udev_dev
                 }
         }
 
                 }
         }
 
-        if (!new_name && config->name) {
-                new_name = config->name;
-        }
+        if (new_name)
+                *name = new_name; /* a name was set by a policy */
+        else if (config->name)
+                *name = config->name; /* a name was set manually in the config */
+        else
+                *name = NULL;
 
         switch (config->mac_policy) {
                 case MACPOLICY_PERSISTENT:
 
         switch (config->mac_policy) {
                 case MACPOLICY_PERSISTENT:
@@ -454,9 +464,9 @@ int link_config_apply(link_config_ctx *ctx, link_config *config, struct udev_dev
                         mac = config->mac;
         }
 
                         mac = config->mac;
         }
 
-        r = rtnl_set_link_properties(ctx->rtnl, ifindex, new_name, mac, config->mtu);
+        r = rtnl_set_link_properties(ctx->rtnl, ifindex, mac, config->mtu);
         if (r < 0) {
         if (r < 0) {
-                log_warning("Could not set Name, MACAddress or MTU on %s: %s", name, strerror(-r));
+                log_warning("Could not set MACAddress or MTU on %s: %s", old_name, strerror(-r));
                 return r;
         }
 
                 return r;
         }
 
index f5dee5a274e782a4e6a85ec54676793ae9ba5f3f..b3ee81fdaaff9410df1b4f14dcc3e38506d17e4c 100644 (file)
@@ -74,7 +74,7 @@ int link_config_load(link_config_ctx *ctx);
 bool link_config_should_reload(link_config_ctx *ctx);
 
 int link_config_get(link_config_ctx *ctx, struct udev_device *device, struct link_config **ret);
 bool link_config_should_reload(link_config_ctx *ctx);
 
 int link_config_get(link_config_ctx *ctx, struct udev_device *device, struct link_config **ret);
-int link_config_apply(link_config_ctx *ctx, struct link_config *config, struct udev_device *device);
+int link_config_apply(link_config_ctx *ctx, struct link_config *config, struct udev_device *device, const char **name);
 
 const char *name_policy_to_string(NamePolicy p) _const_;
 NamePolicy name_policy_from_string(const char *p) _pure_;
 
 const char *name_policy_to_string(NamePolicy p) _const_;
 NamePolicy name_policy_from_string(const char *p) _pure_;
index 6a98b802b2faf7cce52e30d8705d7baa58cf093e..e6ffa53400c6952c41d11a67d74bc3936444e9f6 100644 (file)
@@ -26,6 +26,7 @@
 link_config_ctx *ctx;
 
 static int builtin_net_setup_link(struct udev_device *dev, int argc, char **argv, bool test) {
 link_config_ctx *ctx;
 
 static int builtin_net_setup_link(struct udev_device *dev, int argc, char **argv, bool test) {
+        const char *name;
         link_config *link;
         int r;
 
         link_config *link;
         int r;
 
@@ -45,12 +46,15 @@ static int builtin_net_setup_link(struct udev_device *dev, int argc, char **argv
                 }
         }
 
                 }
         }
 
-        r = link_config_apply(ctx, link, dev);
+        r = link_config_apply(ctx, link, dev, &name);
         if (r < 0) {
                 log_error("Could not apply link config to %s", udev_device_get_sysname(dev));
                 return EXIT_FAILURE;
         }
 
         if (r < 0) {
                 log_error("Could not apply link config to %s", udev_device_get_sysname(dev));
                 return EXIT_FAILURE;
         }
 
+        if (name)
+                udev_builtin_add_property(dev, test, "ID_NET_NAME", name);
+
         return EXIT_SUCCESS;
 }
 
         return EXIT_SUCCESS;
 }
 
index bdf3a386c93158c5f1d2a0d6f4a65528f9b84792..1242db9ef3b9a3f39a615b1c2edd085c2ea78591 100644 (file)
@@ -764,7 +764,7 @@ static int rename_netif(struct udev_event *event)
         if (r < 0)
                 return r;
 
         if (r < 0)
                 return r;
 
-        r = rtnl_set_link_properties(rtnl, udev_device_get_ifindex(dev), name, NULL, 0);
+        r = rtnl_set_link_name(rtnl, udev_device_get_ifindex(dev), name);
         if (r < 0)
                 log_error("error changing net interface name %s to %s: %s",
                           oldname, name, strerror(-r));
         if (r < 0)
                 log_error("error changing net interface name %s to %s: %s",
                           oldname, name, strerror(-r));