From 79c6e114562c94188a65606ce707d36e17d9d16e Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 26 May 2021 13:44:02 +0900 Subject: [PATCH] network: introduce link_call_getlink() --- src/network/networkd-link.c | 59 +++++++++++++++++++++++++++++++++++++ src/network/networkd-link.h | 2 ++ 2 files changed, 61 insertions(+) diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c index 67c6b5519f7..998c4c4c8ec 100644 --- a/src/network/networkd-link.c +++ b/src/network/networkd-link.c @@ -2764,6 +2764,65 @@ int manager_rtnl_process_link(sd_netlink *rtnl, sd_netlink_message *message, Man return 1; } +int link_getlink_handler_internal(sd_netlink *rtnl, sd_netlink_message *m, Link *link, const char *error_msg) { + uint16_t message_type; + int r; + + assert(m); + assert(link); + assert(error_msg); + + if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) + return 0; + + r = sd_netlink_message_get_errno(m); + if (r < 0) { + log_link_message_warning_errno(link, m, r, error_msg); + link_enter_failed(link); + return 0; + } + + r = sd_netlink_message_get_type(m, &message_type); + if (r < 0) { + log_link_debug_errno(link, r, "rtnl: failed to read link message type, ignoring: %m"); + return 0; + } + if (message_type != RTM_NEWLINK) { + log_link_debug(link, "rtnl: received invalid link message type, ignoring."); + return 0; + } + + r = link_update(link, m); + if (r < 0) { + link_enter_failed(link); + return 0; + } + + return 1; +} + +int link_call_getlink(Link *link, link_netlink_message_handler_t callback) { + _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; + int r; + + assert(link); + assert(link->manager); + assert(link->manager->rtnl); + assert(callback); + + r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, link->ifindex); + if (r < 0) + return r; + + r = netlink_call_async(link->manager->rtnl, NULL, req, callback, + link_netlink_destroy_callback, link); + if (r < 0) + return r; + + link_ref(link); + return 0; +} + static const char* const link_state_table[_LINK_STATE_MAX] = { [LINK_STATE_PENDING] = "pending", [LINK_STATE_INITIALIZED] = "initialized", diff --git a/src/network/networkd-link.h b/src/network/networkd-link.h index 2305bf8d42d..bd6c1b2774e 100644 --- a/src/network/networkd-link.h +++ b/src/network/networkd-link.h @@ -221,6 +221,8 @@ int link_get(Manager *m, int ifindex, Link **ret); int link_get_by_name(Manager *m, const char *ifname, Link **ret); int link_get_master(Link *link, Link **ret); +int link_getlink_handler_internal(sd_netlink *rtnl, sd_netlink_message *m, Link *link, const char *error_msg); +int link_call_getlink(Link *link, link_netlink_message_handler_t callback); int link_up(Link *link); int link_down(Link *link, link_netlink_message_handler_t callback); int link_activate(Link *link); -- 2.47.3