diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2021-05-21 06:57:24 +0200 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2021-06-07 23:33:27 +0200 |
commit | a8e5e27c9fb5ad8cab8e83edba8cdbdcb76a88d7 (patch) | |
tree | 9fd566c190e0e1f821794faedd92faeb7a3e20e1 | |
parent | network: use request queue to set link flags (diff) | |
download | systemd-a8e5e27c9fb5ad8cab8e83edba8cdbdcb76a88d7.tar.xz systemd-a8e5e27c9fb5ad8cab8e83edba8cdbdcb76a88d7.zip |
network: use request queue to set MAC address
-rw-r--r-- | src/network/networkd-link.c | 51 | ||||
-rw-r--r-- | src/network/networkd-setlink.c | 20 | ||||
-rw-r--r-- | src/network/networkd-setlink.h | 2 |
3 files changed, 23 insertions, 50 deletions
diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c index 261e7384cd..44d62fe857 100644 --- a/src/network/networkd-link.c +++ b/src/network/networkd-link.c @@ -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; diff --git a/src/network/networkd-setlink.c b/src/network/networkd-setlink.c index 9ea78fa818..a030eebd0d 100644 --- a/src/network/networkd-setlink.c +++ b/src/network/networkd-setlink.c @@ -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; diff --git a/src/network/networkd-setlink.h b/src/network/networkd-setlink.h index 0e9e0d61c4..0d6315a76f 100644 --- a/src/network/networkd-setlink.h +++ b/src/network/networkd-setlink.h @@ -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); |