network: introduce request_call_netlink_async()

In most netlink handlers, we do the following,
1. decrease the message counter,
2. check the link state,
3. error handling,
4. update link state via e.g. link_check_ready().

The first two steps are mostly common, hence let's extract it.

Moreover, this is not only extracting the common logic, but provide a
strong advantage; `request_call_netlink_async()` assigns the relevant
Request object to the userdata of the netlink slot, and the request object
has full information about the message we sent. Hence, in the future,
netlink handler can print more detailed error message. E.g. when
an address is failed to configure, then currently we only show an
address is failed to configure, but with this commit, potentially we can
show which address is failed explicitly.

This does not change such error handling yet. But let's do that later.
This commit is contained in:
Yu Watanabe
2022-03-11 14:20:31 +09:00
parent e26d3d407c
commit 80d62d4f1a
22 changed files with 237 additions and 405 deletions
+3 -12
View File
@@ -595,13 +595,10 @@ static int stacked_netdev_create(NetDev *netdev, Link *link, Request *req) {
if (r < 0)
return r;
r = netlink_call_async(netdev->manager->rtnl, NULL, m, req->netlink_handler,
link_netlink_destroy_callback, link);
r = request_call_netlink_async(netdev->manager->rtnl, m, req);
if (r < 0)
return r;
link_ref(link);
netdev->state = NETDEV_STATE_CREATING;
log_netdev_debug(netdev, "Creating");
return 0;
@@ -650,17 +647,11 @@ int stacked_netdev_process_request(Request *req, Link *link, void *userdata) {
return 1;
}
static int link_create_stacked_netdev_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
static int create_stacked_netdev_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, void *userdata) {
int r;
assert(m);
assert(link);
assert(link->create_stacked_netdev_messages > 0);
link->create_stacked_netdev_messages--;
if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
return 0;
r = sd_netlink_message_get_errno(m);
if (r < 0 && r != -EEXIST) {
@@ -693,7 +684,7 @@ int link_request_stacked_netdev(Link *link, NetDev *netdev) {
link->stacked_netdevs_created = false;
r = link_queue_request(link, REQUEST_TYPE_NETDEV_STACKED, netdev_ref(netdev), true,
&link->create_stacked_netdev_messages,
link_create_stacked_netdev_handler,
create_stacked_netdev_handler,
NULL);
if (r < 0)
return log_link_error_errno(link, r, "Failed to request stacked netdev '%s': %m",