1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-10 05:18:17 +03:00

Merge pull request #22619 from yuwata/network-netdev-cleanups

network: several netdev cleanups
This commit is contained in:
Yu Watanabe 2022-02-26 08:13:38 +09:00 committed by GitHub
commit a23e4f60d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 94 additions and 113 deletions

View File

@ -163,7 +163,7 @@ static int netdev_batadv_post_create_message(NetDev *netdev, sd_netlink_message
return 0;
}
static int netdev_batadv_post_create(NetDev *netdev, Link *link, sd_netlink_message *m) {
static int netdev_batadv_post_create(NetDev *netdev, Link *link) {
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *message = NULL;
int r;

View File

@ -149,7 +149,7 @@ static int netdev_bridge_post_create_message(NetDev *netdev, sd_netlink_message
return 0;
}
static int netdev_bridge_post_create(NetDev *netdev, Link *link, sd_netlink_message *m) {
static int netdev_bridge_post_create(NetDev *netdev, Link *link) {
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
int r;

View File

@ -80,16 +80,13 @@ const NetDevVTable ipvtap_vtable = {
};
IPVlanMode link_get_ipvlan_mode(Link *link) {
NetDev *netdev;
IPVlan *ipvlan;
if (!streq_ptr(link->kind, "ipvlan"))
assert(link);
ipvlan = IPVLAN(link->netdev);
if (!ipvlan)
return _NETDEV_IPVLAN_MODE_INVALID;
if (netdev_get(link->manager, link->ifname, &netdev) < 0)
return _NETDEV_IPVLAN_MODE_INVALID;
if (netdev->kind != NETDEV_KIND_IPVLAN)
return _NETDEV_IPVLAN_MODE_INVALID;
return IPVLAN(netdev)->mode;
return ipvlan->mode;
}

View File

@ -474,7 +474,7 @@ static int netdev_macsec_configure_transmit_association(NetDev *netdev, Transmit
return 0;
}
static int netdev_macsec_configure(NetDev *netdev, Link *link, sd_netlink_message *m) {
static int netdev_macsec_configure(NetDev *netdev, Link *link) {
TransmitAssociation *a;
ReceiveChannel *c;
MACsec *s;

View File

@ -253,10 +253,8 @@ int netdev_get(Manager *manager, const char *name, NetDev **ret) {
assert(ret);
netdev = hashmap_get(manager->netdevs, name);
if (!netdev) {
*ret = NULL;
if (!netdev)
return -ENOENT;
}
*ret = netdev;
@ -279,7 +277,7 @@ static int netdev_enter_ready(NetDev *netdev) {
log_netdev_info(netdev, "netdev ready");
if (NETDEV_VTABLE(netdev)->post_create)
NETDEV_VTABLE(netdev)->post_create(netdev, NULL, NULL);
NETDEV_VTABLE(netdev)->post_create(netdev, NULL);
return 0;
}
@ -544,17 +542,14 @@ static int netdev_create_message(NetDev *netdev, Link *link, sd_netlink_message
return 0;
}
static int netdev_create(NetDev *netdev, Link *link, link_netlink_message_handler_t callback) {
static int independent_netdev_create(NetDev *netdev) {
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
int r;
assert(netdev);
assert(!link || callback);
/* create netdev */
if (NETDEV_VTABLE(netdev)->create) {
assert(!link);
r = NETDEV_VTABLE(netdev)->create(netdev);
if (r < 0)
return r;
@ -565,65 +560,50 @@ static int netdev_create(NetDev *netdev, Link *link, link_netlink_message_handle
r = sd_rtnl_message_new_link(netdev->manager->rtnl, &m, RTM_NEWLINK, 0);
if (r < 0)
return log_netdev_error_errno(netdev, r, "Could not allocate netlink message: %m");
return r;
r = netdev_create_message(netdev, link, m);
r = netdev_create_message(netdev, NULL, m);
if (r < 0)
return log_netdev_error_errno(netdev, r, "Could not create netlink message: %m");
return r;
if (link) {
r = netlink_call_async(netdev->manager->rtnl, NULL, m, callback,
link_netlink_destroy_callback, link);
if (r < 0)
return log_netdev_error_errno(netdev, r, "Could not send netlink message: %m");
r = netlink_call_async(netdev->manager->rtnl, NULL, m, netdev_create_handler,
netdev_destroy_callback, netdev);
if (r < 0)
return r;
link_ref(link);
} else {
r = netlink_call_async(netdev->manager->rtnl, NULL, m, netdev_create_handler,
netdev_destroy_callback, netdev);
if (r < 0)
return log_netdev_error_errno(netdev, r, "Could not send netlink message: %m");
netdev_ref(netdev);
}
netdev_ref(netdev);
netdev->state = NETDEV_STATE_CREATING;
log_netdev_debug(netdev, "Creating");
return 0;
}
static int netdev_create_after_configured(NetDev *netdev, Link *link) {
assert(netdev);
assert(link);
assert(NETDEV_VTABLE(netdev)->create_after_configured);
return NETDEV_VTABLE(netdev)->create_after_configured(netdev, link);
}
int netdev_join(NetDev *netdev, Link *link, link_netlink_message_handler_t callback) {
static int stacked_netdev_create(NetDev *netdev, Link *link, link_netlink_message_handler_t callback) {
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
int r;
assert(netdev);
assert(netdev->manager);
assert(netdev->manager->rtnl);
assert(link);
assert(callback);
switch (netdev_get_create_type(netdev)) {
case NETDEV_CREATE_STACKED:
r = netdev_create(netdev, link, callback);
if (r < 0)
return r;
r = sd_rtnl_message_new_link(netdev->manager->rtnl, &m, RTM_NEWLINK, 0);
if (r < 0)
return r;
break;
case NETDEV_CREATE_AFTER_CONFIGURED:
r = netdev_create_after_configured(netdev, link);
if (r < 0)
return r;
break;
default:
assert_not_reached();
}
r = netdev_create_message(netdev, link, m);
if (r < 0)
return r;
r = netlink_call_async(netdev->manager->rtnl, NULL, m, callback,
link_netlink_destroy_callback, link);
if (r < 0)
return r;
link_ref(link);
netdev->state = NETDEV_STATE_CREATING;
log_netdev_debug(netdev, "Creating");
return 0;
}
@ -657,21 +637,34 @@ static int netdev_is_ready_to_create(NetDev *netdev, Link *link) {
}
int request_process_stacked_netdev(Request *req) {
NetDev *netdev;
Link *link;
int r;
assert(req);
assert(req->link);
assert(req->type == REQUEST_TYPE_NETDEV_STACKED);
assert(req->netdev);
assert(req->netlink_handler);
r = netdev_is_ready_to_create(req->netdev, req->link);
netdev = ASSERT_PTR(req->netdev);
link = ASSERT_PTR(req->link);
r = netdev_is_ready_to_create(netdev, link);
if (r <= 0)
return r;
r = netdev_join(req->netdev, req->link, req->netlink_handler);
switch (netdev_get_create_type(netdev)) {
case NETDEV_CREATE_STACKED:
r = stacked_netdev_create(netdev, link, req->netlink_handler);
break;
case NETDEV_CREATE_AFTER_CONFIGURED:
assert(NETDEV_VTABLE(netdev)->create_after_configured);
r = NETDEV_VTABLE(netdev)->create_after_configured(netdev, link);
break;
default:
assert_not_reached();
}
if (r < 0)
return log_link_error_errno(req->link, r, "Failed to create stacked netdev '%s': %m", req->netdev->ifname);
return log_netdev_warning_errno(netdev, r, "Failed to create netdev: %m");
return 1;
}
@ -714,19 +707,6 @@ static int link_create_stacked_netdev_handler(sd_netlink *rtnl, sd_netlink_messa
}
static int link_create_stacked_netdev_after_configured_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
assert(link);
assert(link->create_stacked_netdev_after_configured_messages > 0);
link->create_stacked_netdev_after_configured_messages--;
if (link_create_stacked_netdev_handler_internal(rtnl, m, link) <= 0)
return 0;
if (link->create_stacked_netdev_after_configured_messages == 0) {
link->stacked_netdevs_after_configured_created = true;
log_link_debug(link, "Stacked netdevs created.");
}
return 0;
}
@ -748,13 +728,9 @@ int link_request_stacked_netdev(Link *link, NetDev *netdev) {
&link->create_stacked_netdev_messages,
link_create_stacked_netdev_handler,
NULL);
} else {
link->stacked_netdevs_after_configured_created = false;
} else
r = link_queue_request(link, REQUEST_TYPE_NETDEV_STACKED, netdev_ref(netdev), true,
&link->create_stacked_netdev_after_configured_messages,
link_create_stacked_netdev_after_configured_handler,
NULL);
}
NULL, link_create_stacked_netdev_after_configured_handler, NULL);
if (r < 0)
return log_link_error_errno(link, r, "Failed to request stacked netdev '%s': %m",
netdev->ifname);
@ -764,24 +740,26 @@ int link_request_stacked_netdev(Link *link, NetDev *netdev) {
}
int request_process_independent_netdev(Request *req) {
NetDev *netdev;
int r;
assert(req);
assert(req->type == REQUEST_TYPE_NETDEV_INDEPENDENT);
assert(req->netdev);
r = netdev_is_ready_to_create(req->netdev, NULL);
netdev = ASSERT_PTR(req->netdev);
r = netdev_is_ready_to_create(netdev, NULL);
if (r <= 0)
return r;
r = netdev_create(req->netdev, NULL, NULL);
r = independent_netdev_create(netdev);
if (r < 0)
return r;
return log_netdev_warning_errno(netdev, r, "Failed to create netdev: %m");
return 1;
}
static int netdev_request(NetDev *netdev) {
static int netdev_request_to_create(NetDev *netdev) {
int r;
assert(netdev);
@ -793,12 +771,20 @@ static int netdev_request(NetDev *netdev) {
r = netdev_is_ready_to_create(netdev, NULL);
if (r < 0)
return r;
if (r > 0)
if (r > 0) {
/* If the netdev has no dependency, then create it now. */
return netdev_create(netdev, NULL, NULL);
r = independent_netdev_create(netdev);
if (r < 0)
return log_netdev_warning_errno(netdev, r, "Failed to create netdev: %m");
/* Otherwise, wait for the dependencies being resolved. */
return netdev_queue_request(netdev, NULL);
} else {
/* Otherwise, wait for the dependencies being resolved. */
r = netdev_queue_request(netdev, NULL);
if (r < 0)
return log_netdev_warning_errno(netdev, r, "Failed to request to create: %m");
}
return 0;
}
int netdev_load_one(Manager *manager, const char *filename) {
@ -911,9 +897,9 @@ int netdev_load_one(Manager *manager, const char *filename) {
log_netdev_debug(netdev, "loaded %s", netdev_kind_to_string(netdev->kind));
r = netdev_request(netdev);
r = netdev_request_to_create(netdev);
if (r < 0)
return log_netdev_warning_errno(netdev, r, "Failed to request to create: %m");
return r;
TAKE_PTR(netdev);
return 0;

View File

@ -165,7 +165,7 @@ typedef struct NetDevVTable {
int (*create_after_configured)(NetDev *netdev, Link *link);
/* perform additional configuration after netdev has been createad */
int (*post_create)(NetDev *netdev, Link *link, sd_netlink_message *message);
int (*post_create)(NetDev *netdev, Link *link);
/* verify that compulsory configuration options were specified */
int (*config_verify)(NetDev *netdev, const char *filename);
@ -213,7 +213,6 @@ int netdev_get(Manager *manager, const char *name, NetDev **ret);
int netdev_set_ifindex(NetDev *netdev, sd_netlink_message *newlink);
int netdev_generate_hw_addr(NetDev *netdev, Link *link, const char *name,
const struct hw_addr_data *hw_addr, struct hw_addr_data *ret);
int netdev_join(NetDev *netdev, Link *link, link_netlink_message_handler_t cb);
int request_process_independent_netdev(Request *req);
int request_process_stacked_netdev(Request *req);

View File

@ -437,7 +437,7 @@ static void wireguard_resolve_endpoints(NetDev *netdev) {
break;
}
static int netdev_wireguard_post_create(NetDev *netdev, Link *link, sd_netlink_message *m) {
static int netdev_wireguard_post_create(NetDev *netdev, Link *link) {
assert(netdev);
assert(WIREGUARD(netdev));

View File

@ -228,6 +228,7 @@ static Link *link_free(Link *link) {
unlink_and_free(link->state_file);
sd_device_unref(link->sd_device);
netdev_unref(link->netdev);
hashmap_free(link->bound_to_links);
hashmap_free(link->bound_by_links);
@ -561,7 +562,6 @@ static int link_request_stacked_netdevs(Link *link) {
assert(link);
link->stacked_netdevs_created = false;
link->stacked_netdevs_after_configured_created = false;
HASHMAP_FOREACH(netdev, link->network->stacked_netdevs) {
r = link_request_stacked_netdev(link, netdev);
@ -573,8 +573,6 @@ static int link_request_stacked_netdevs(Link *link) {
link->stacked_netdevs_created = true;
link_check_ready(link);
}
if (link->create_stacked_netdev_after_configured_messages == 0)
link->stacked_netdevs_after_configured_created = true;
return 0;
}
@ -1206,6 +1204,7 @@ static int link_get_network(Link *link, Network **ret) {
static int link_reconfigure_impl(Link *link, bool force) {
Network *network = NULL;
NetDev *netdev = NULL;
int r;
assert(link);
@ -1213,6 +1212,10 @@ static int link_reconfigure_impl(Link *link, bool force) {
if (!IN_SET(link->state, LINK_STATE_INITIALIZED, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED, LINK_STATE_UNMANAGED))
return 0;
r = netdev_get(link->manager, link->ifname, &netdev);
if (r < 0 && r != -ENOENT)
return r;
r = link_get_network(link, &network);
if (r < 0 && r != -ENOENT)
return r;
@ -1265,6 +1268,9 @@ static int link_reconfigure_impl(Link *link, bool force) {
link_free_engines(link);
link->network = network_unref(link->network);
netdev_unref(link->netdev);
link->netdev = netdev_ref(netdev);
if (!network) {
link_set_state(link, LINK_STATE_UNMANAGED);
return 0;

View File

@ -41,6 +41,7 @@ typedef enum LinkState {
typedef struct Manager Manager;
typedef struct Network Network;
typedef struct NetDev NetDev;
typedef struct DUID DUID;
typedef struct Link {
@ -83,6 +84,7 @@ typedef struct Link {
sd_event_source *carrier_lost_timer;
Network *network;
NetDev *netdev;
LinkState state;
LinkOperationalState operstate;
@ -106,7 +108,6 @@ typedef struct Link {
unsigned set_link_messages;
unsigned set_flags_messages;
unsigned create_stacked_netdev_messages;
unsigned create_stacked_netdev_after_configured_messages;
Set *addresses;
Set *neighbors;
@ -140,7 +141,6 @@ typedef struct Link {
bool activated:1;
bool master_set:1;
bool stacked_netdevs_created:1;
bool stacked_netdevs_after_configured_created:1;
sd_dhcp_server *dhcp_server;

View File

@ -871,18 +871,11 @@ static bool route_by_kernel(const Route *route) {
static void link_unmark_wireguard_routes(Link *link) {
Route *route, *existing;
NetDev *netdev;
Wireguard *w;
assert(link);
if (!streq_ptr(link->kind, "wireguard"))
return;
if (netdev_get(link->manager, link->ifname, &netdev) < 0)
return;
w = WIREGUARD(netdev);
w = WIREGUARD(link->netdev);
if (!w)
return;