1
0
mirror of https://github.com/systemd/systemd.git synced 2024-10-31 16:21:26 +03:00

network: use request queue to set link flags

This commit is contained in:
Yu Watanabe 2021-05-21 13:50:39 +09:00
parent 0fa8ee6c77
commit 93fabc10fa
3 changed files with 53 additions and 76 deletions

View File

@ -974,81 +974,6 @@ static int link_set_nomaster(Link *link) {
return 0;
}
static int set_flags_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
int r;
assert(m);
assert(link);
assert(link->ifname);
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 link flags, ignoring");
return 1;
}
static int link_set_flags(Link *link) {
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
unsigned ifi_change = 0;
unsigned ifi_flags = 0;
int r;
assert(link);
assert(link->manager);
assert(link->manager->rtnl);
if (link->flags & IFF_LOOPBACK)
return 0;
if (!link->network)
return 0;
if (link->network->arp < 0 && link->network->multicast < 0 && link->network->allmulticast < 0 &&
link->network->promiscuous < 0)
return 0;
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");
if (link->network->arp >= 0) {
ifi_change |= IFF_NOARP;
SET_FLAG(ifi_flags, IFF_NOARP, link->network->arp == 0);
}
if (link->network->multicast >= 0) {
ifi_change |= IFF_MULTICAST;
SET_FLAG(ifi_flags, IFF_MULTICAST, link->network->multicast);
}
if (link->network->allmulticast >= 0) {
ifi_change |= IFF_ALLMULTI;
SET_FLAG(ifi_flags, IFF_ALLMULTI, link->network->allmulticast);
}
if (link->network->promiscuous >= 0) {
ifi_change |= IFF_PROMISC;
SET_FLAG(ifi_flags, IFF_PROMISC, link->network->promiscuous);
}
r = sd_rtnl_message_link_set_flags(req, ifi_flags, ifi_change);
if (r < 0)
return log_link_error_errno(link, r, "Could not set link flags: %m");
r = netlink_call_async(link->manager->rtnl, NULL, req, set_flags_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_acquire_dynamic_ipv6_conf(Link *link) {
int r;
@ -2010,7 +1935,7 @@ static int link_configure(Link *link) {
if (r < 0)
return r;
r = link_set_flags(link);
r = link_request_to_set_flags(link);
if (r < 0)
return r;

View File

@ -1,5 +1,8 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <netinet/in.h>
#include <linux/if.h>
#include "missing_network.h"
#include "netlink-util.h"
#include "networkd-link.h"
@ -8,6 +11,7 @@
#include "string-table.h"
static const char *const set_link_operation_table[_SET_LINK_OPERATION_MAX] = {
[SET_LINK_FLAGS] = "link flags",
[SET_LINK_MTU] = "MTU",
};
@ -42,6 +46,10 @@ static int set_link_handler_internal(sd_netlink *rtnl, sd_netlink_message *m, Li
return 1;
}
static int link_set_flags_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
return set_link_handler_internal(rtnl, m, link, SET_LINK_FLAGS, true);
}
static int link_set_mtu_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
int r;
@ -87,6 +95,35 @@ static int link_configure(
return log_link_debug_errno(link, r, "Could not allocate RTM_SETLINK message: %m");
switch (op) {
case SET_LINK_FLAGS: {
unsigned ifi_change = 0, ifi_flags = 0;
if (link->network->arp >= 0) {
ifi_change |= IFF_NOARP;
SET_FLAG(ifi_flags, IFF_NOARP, link->network->arp == 0);
}
if (link->network->multicast >= 0) {
ifi_change |= IFF_MULTICAST;
SET_FLAG(ifi_flags, IFF_MULTICAST, link->network->multicast);
}
if (link->network->allmulticast >= 0) {
ifi_change |= IFF_ALLMULTI;
SET_FLAG(ifi_flags, IFF_ALLMULTI, link->network->allmulticast);
}
if (link->network->promiscuous >= 0) {
ifi_change |= IFF_PROMISC;
SET_FLAG(ifi_flags, IFF_PROMISC, link->network->promiscuous);
}
r = sd_rtnl_message_link_set_flags(req, ifi_flags, ifi_change);
if (r < 0)
return log_link_debug_errno(link, r, "Could not set link flags: %m");
break;
}
case SET_LINK_MTU:
r = sd_netlink_message_append_u32(req, IFLA_MTU, PTR_TO_UINT32(userdata));
if (r < 0)
@ -164,6 +201,19 @@ static int link_request_set_link(
return 0;
}
int link_request_to_set_flags(Link *link) {
assert(link);
assert(link->network);
if (link->network->arp < 0 &&
link->network->multicast < 0 &&
link->network->allmulticast < 0 &&
link->network->promiscuous < 0)
return 0;
return link_request_set_link(link, SET_LINK_FLAGS, link_set_flags_handler, NULL);
}
int link_request_to_set_mtu(Link *link, uint32_t mtu) {
Request *req = NULL; /* avoid false maybe-uninitialized warning */
int r;

View File

@ -7,11 +7,13 @@ typedef struct Link Link;
typedef struct Request Request;
typedef enum SetLinkOperation {
SET_LINK_FLAGS, /* Setting IFF_NOARP or friends. */
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_mtu(Link *link, uint32_t mtu);
int link_configure_mtu(Link *link);