]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
network: use request queue to set MAC address
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 21 May 2021 04:57:24 +0000 (13:57 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 7 Jun 2021 21:33:27 +0000 (06:33 +0900)
src/network/networkd-link.c
src/network/networkd-setlink.c
src/network/networkd-setlink.h

index 261e7384cdeb950f5425946162499a427c6355f9..44d62fe857226de955b76cab374ad57a1836b7de 100644 (file)
@@ -875,55 +875,6 @@ static int link_request_static_configs(Link *link) {
         return 0;
 }
 
-static int link_mac_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
-        int r;
-
-        assert(link);
-
-        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
-                return 1;
-
-        r = sd_netlink_message_get_errno(m);
-        if (r < 0)
-                log_link_message_warning_errno(link, m, r, "Could not set MAC address, ignoring");
-        else
-                log_link_debug(link, "Setting MAC address done.");
-
-        return 1;
-}
-
-static int link_set_mac(Link *link) {
-        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
-        int r;
-
-        assert(link);
-        assert(link->network);
-        assert(link->manager);
-        assert(link->manager->rtnl);
-
-        if (!link->network->mac)
-                return 0;
-
-        log_link_debug(link, "Setting MAC address");
-
-        r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex);
-        if (r < 0)
-                return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m");
-
-        r = sd_netlink_message_append_ether_addr(req, IFLA_ADDRESS, link->network->mac);
-        if (r < 0)
-                return log_link_error_errno(link, r, "Could not set MAC address: %m");
-
-        r = netlink_call_async(link->manager->rtnl, NULL, req, link_mac_handler,
-                               link_netlink_destroy_callback, link);
-        if (r < 0)
-                return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
-
-        link_ref(link);
-
-        return 0;
-}
-
 static int link_nomaster_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
         int r;
 
@@ -1927,7 +1878,7 @@ static int link_configure(Link *link) {
         if (r < 0)
                 return r;
 
-        r = link_set_mac(link);
+        r = link_request_to_set_mac(link);
         if (r < 0)
                 return r;
 
index 9ea78fa81872ff5b024b5b2a70f5f5704411f85d..a030eebd0d05f218c1ccdf788eb21f5a3a47d677 100644 (file)
@@ -12,6 +12,7 @@
 
 static const char *const set_link_operation_table[_SET_LINK_OPERATION_MAX] = {
         [SET_LINK_FLAGS]                   = "link flags",
+        [SET_LINK_MAC]                     = "MAC address",
         [SET_LINK_MTU]                     = "MTU",
 };
 
@@ -50,6 +51,10 @@ static int link_set_flags_handler(sd_netlink *rtnl, sd_netlink_message *m, Link
         return set_link_handler_internal(rtnl, m, link, SET_LINK_FLAGS, true);
 }
 
+static int link_set_mac_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
+        return set_link_handler_internal(rtnl, m, link, SET_LINK_MAC, true);
+}
+
 static int link_set_mtu_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
         int r;
 
@@ -124,6 +129,11 @@ static int link_configure(
 
                 break;
         }
+        case SET_LINK_MAC:
+                r = sd_netlink_message_append_ether_addr(req, IFLA_ADDRESS, link->network->mac);
+                if (r < 0)
+                        return log_link_debug_errno(link, r, "Could not append IFLA_ADDRESS attribute: %m");
+                break;
         case SET_LINK_MTU:
                 r = sd_netlink_message_append_u32(req, IFLA_MTU, PTR_TO_UINT32(userdata));
                 if (r < 0)
@@ -214,6 +224,16 @@ int link_request_to_set_flags(Link *link) {
         return link_request_set_link(link, SET_LINK_FLAGS, link_set_flags_handler, NULL);
 }
 
+int link_request_to_set_mac(Link *link) {
+        assert(link);
+        assert(link->network);
+
+        if (!link->network->mac)
+                return 0;
+
+        return link_request_set_link(link, SET_LINK_MAC, link_set_mac_handler, NULL);
+}
+
 int link_request_to_set_mtu(Link *link, uint32_t mtu) {
         Request *req = NULL;  /* avoid false maybe-uninitialized warning */
         int r;
index 0e9e0d61c4ff851a84f254d99fc2aeedf2778810..0d6315a76f5eb6d1245dbd6a6050a7ae7b7e66ba 100644 (file)
@@ -8,12 +8,14 @@ typedef struct Request Request;
 
 typedef enum SetLinkOperation {
         SET_LINK_FLAGS,                   /* Setting IFF_NOARP or friends. */
+        SET_LINK_MAC,                     /* Setting MAC address. */
         SET_LINK_MTU,                     /* Setting MTU. */
         _SET_LINK_OPERATION_MAX,
         _SET_LINK_OPERATION_INVALID = -EINVAL,
 } SetLinkOperation;
 
 int link_request_to_set_flags(Link *link);
+int link_request_to_set_mac(Link *link);
 int link_request_to_set_mtu(Link *link, uint32_t mtu);
 
 int link_configure_mtu(Link *link);