diff options
-rw-r--r-- | src/network/networkd-manager.c | 413 | ||||
-rw-r--r-- | src/network/networkd.c | 18 | ||||
-rw-r--r-- | src/network/networkd.h | 4 | ||||
-rw-r--r-- | src/network/test-network.c | 2 |
4 files changed, 203 insertions, 234 deletions
diff --git a/src/network/networkd-manager.c b/src/network/networkd-manager.c index 09958c40da..378633061e 100644 --- a/src/network/networkd-manager.c +++ b/src/network/networkd-manager.c @@ -169,169 +169,84 @@ int manager_connect_bus(Manager *m) { return 0; } -static int systemd_netlink_fd(void) { - int n, fd, rtnl_fd = -EINVAL; +static int manager_udev_process_link(Manager *m, struct udev_device *device) { + Link *link = NULL; + int r, ifindex; - n = sd_listen_fds(true); - if (n <= 0) - return -EINVAL; + assert(m); + assert(device); - for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) { - if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1) > 0) { - if (rtnl_fd >= 0) - return -EINVAL; + if (!streq_ptr(udev_device_get_action(device), "add")) + return 0; - rtnl_fd = fd; - } + ifindex = udev_device_get_ifindex(device); + if (ifindex <= 0) { + log_debug("ignoring udev ADD event for device with invalid ifindex"); + return 0; } - return rtnl_fd; -} - -int manager_new(Manager **ret) { - _cleanup_manager_free_ Manager *m = NULL; - int r, fd; - - m = new0(Manager, 1); - if (!m) - return -ENOMEM; - - m->state_file = strdup("/run/systemd/netif/state"); - if (!m->state_file) - return -ENOMEM; - - r = sd_event_default(&m->event); - if (r < 0) + r = link_get(m, ifindex, &link); + if (r == -ENODEV) + return 0; + else if (r < 0) return r; - sd_event_set_watchdog(m->event, true); - - sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL); - sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL); - - fd = systemd_netlink_fd(); - if (fd < 0) - r = sd_rtnl_open(&m->rtnl, 3, RTNLGRP_LINK, RTNLGRP_IPV4_IFADDR, RTNLGRP_IPV6_IFADDR); - else - r = sd_rtnl_new_from_netlink(&m->rtnl, fd); + r = link_initialized(link, device); if (r < 0) return r; - r = manager_connect_bus(m); - if (r < 0) - return r; + return 0; +} - /* udev does not initialize devices inside containers, - * so we rely on them being already initialized before - * entering the container */ - if (detect_container(NULL) <= 0) { - m->udev = udev_new(); - if (!m->udev) - return -ENOMEM; - - m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev"); - if (!m->udev_monitor) - return -ENOMEM; - } +static int manager_dispatch_link_udev(sd_event_source *source, int fd, uint32_t revents, void *userdata) { + Manager *m = userdata; + struct udev_monitor *monitor = m->udev_monitor; + _cleanup_udev_device_unref_ struct udev_device *device = NULL; - m->netdevs = hashmap_new(&string_hash_ops); - if (!m->netdevs) + device = udev_monitor_receive_device(monitor); + if (!device) return -ENOMEM; - LIST_HEAD_INIT(m->networks); - - r = setup_default_address_pool(m); - if (r < 0) - return r; - - *ret = m; - m = NULL; - + manager_udev_process_link(m, device); return 0; } -void manager_free(Manager *m) { - Network *network; - NetDev *netdev; - Link *link; - AddressPool *pool; - - if (!m) - return; - - free(m->state_file); - - udev_monitor_unref(m->udev_monitor); - udev_unref(m->udev); - sd_bus_unref(m->bus); - sd_bus_slot_unref(m->prepare_for_sleep_slot); - sd_event_source_unref(m->udev_event_source); - sd_event_source_unref(m->bus_retry_event_source); - sd_event_unref(m->event); - - while ((link = hashmap_first(m->links))) - link_unref(link); - hashmap_free(m->links); - - while ((network = m->networks)) - network_free(network); - - while ((netdev = hashmap_first(m->netdevs))) - netdev_unref(netdev); - hashmap_free(m->netdevs); - - while ((pool = m->address_pools)) - address_pool_free(pool); - - sd_rtnl_unref(m->rtnl); +static int manager_connect_udev(Manager *m) { + int r; - free(m); -} + /* udev does not initialize devices inside containers, + * so we rely on them being already initialized before + * entering the container */ + if (detect_container(NULL) > 0) + return 0; -int manager_load_config(Manager *m) { - int r; + m->udev = udev_new(); + if (!m->udev) + return -ENOMEM; - /* update timestamp */ - paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, true); + m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev"); + if (!m->udev_monitor) + return -ENOMEM; - r = netdev_load(m); + r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_monitor, "net", NULL); if (r < 0) - return r; + return log_error_errno(r, "Could not add udev monitor filter: %m"); - r = network_load(m); - if (r < 0) + r = udev_monitor_enable_receiving(m->udev_monitor); + if (r < 0) { + log_error("Could not enable udev monitor"); return r; - - return 0; -} - -bool manager_should_reload(Manager *m) { - return paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, false); -} - -static int manager_udev_process_link(Manager *m, struct udev_device *device) { - Link *link = NULL; - int r, ifindex; - - assert(m); - assert(device); - - if (!streq_ptr(udev_device_get_action(device), "add")) - return 0; - - ifindex = udev_device_get_ifindex(device); - if (ifindex <= 0) { - log_debug("ignoring udev ADD event for device with invalid ifindex"); - return 0; } - r = link_get(m, ifindex, &link); - if (r == -ENODEV) - return 0; - else if (r < 0) + r = sd_event_add_io(m->event, + &m->udev_event_source, + udev_monitor_get_fd(m->udev_monitor), + EPOLLIN, manager_dispatch_link_udev, + m); + if (r < 0) return r; - r = link_initialized(link, device); + r = sd_event_source_set_description(m->udev_event_source, "networkd-udev"); if (r < 0) return r; @@ -420,155 +335,233 @@ static int manager_rtnl_process_link(sd_rtnl *rtnl, sd_rtnl_message *message, vo return 1; } -int manager_rtnl_enumerate_links(Manager *m) { - _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL; - sd_rtnl_message *link; - int r; +static int systemd_netlink_fd(void) { + int n, fd, rtnl_fd = -EINVAL; + + n = sd_listen_fds(true); + if (n <= 0) + return -EINVAL; + + for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) { + if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1) > 0) { + if (rtnl_fd >= 0) + return -EINVAL; + + rtnl_fd = fd; + } + } + + return rtnl_fd; +} + +static int manager_connect_rtnl(Manager *m) { + int fd, r; assert(m); - assert(m->rtnl); - r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0); + fd = systemd_netlink_fd(); + if (fd < 0) + r = sd_rtnl_open(&m->rtnl, 3, RTNLGRP_LINK, RTNLGRP_IPV4_IFADDR, RTNLGRP_IPV6_IFADDR); + else + r = sd_rtnl_open_fd(&m->rtnl, fd, 0); if (r < 0) return r; - r = sd_rtnl_message_request_dump(req, true); + r = sd_rtnl_inc_rcvbuf(m->rtnl, RCVBUF_SIZE); if (r < 0) return r; - r = sd_rtnl_call(m->rtnl, req, 0, &reply); + r = sd_rtnl_attach_event(m->rtnl, m->event, 0); if (r < 0) return r; - for (link = reply; link; link = sd_rtnl_message_next(link)) { - int k; + r = sd_rtnl_add_match(m->rtnl, RTM_NEWLINK, &manager_rtnl_process_link, m); + if (r < 0) + return r; - k = manager_rtnl_process_link(m->rtnl, link, m); - if (k < 0) - r = k; - } + r = sd_rtnl_add_match(m->rtnl, RTM_DELLINK, &manager_rtnl_process_link, m); + if (r < 0) + return r; - return r; + r = sd_rtnl_add_match(m->rtnl, RTM_NEWADDR, &link_rtnl_process_address, m); + if (r < 0) + return r; + + r = sd_rtnl_add_match(m->rtnl, RTM_DELADDR, &link_rtnl_process_address, m); + if (r < 0) + return r; + + return 0; } -int manager_rtnl_enumerate_addresses(Manager *m) { - _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL; - sd_rtnl_message *addr; +int manager_new(Manager **ret) { + _cleanup_manager_free_ Manager *m = NULL; int r; - assert(m); - assert(m->rtnl); + m = new0(Manager, 1); + if (!m) + return -ENOMEM; - r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDR, 0, 0); + m->state_file = strdup("/run/systemd/netif/state"); + if (!m->state_file) + return -ENOMEM; + + r = sd_event_default(&m->event); if (r < 0) return r; - r = sd_rtnl_message_request_dump(req, true); + sd_event_set_watchdog(m->event, true); + + sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL); + sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL); + + r = manager_connect_rtnl(m); if (r < 0) return r; - r = sd_rtnl_call(m->rtnl, req, 0, &reply); + r = manager_connect_bus(m); if (r < 0) return r; - for (addr = reply; addr; addr = sd_rtnl_message_next(addr)) { - int k; + r = manager_connect_udev(m); + if (r < 0) + return r; - k = link_rtnl_process_address(m->rtnl, addr, m); - if (k < 0) - r = k; - } + m->netdevs = hashmap_new(&string_hash_ops); + if (!m->netdevs) + return -ENOMEM; - return r; -} + LIST_HEAD_INIT(m->networks); -static int manager_dispatch_link_udev(sd_event_source *source, int fd, uint32_t revents, void *userdata) { - Manager *m = userdata; - struct udev_monitor *monitor = m->udev_monitor; - _cleanup_udev_device_unref_ struct udev_device *device = NULL; + r = setup_default_address_pool(m); + if (r < 0) + return r; - device = udev_monitor_receive_device(monitor); - if (!device) - return -ENOMEM; + *ret = m; + m = NULL; - manager_udev_process_link(m, device); return 0; } -int manager_udev_listen(Manager *m) { - int r; +void manager_free(Manager *m) { + Network *network; + NetDev *netdev; + Link *link; + AddressPool *pool; - if (detect_container(NULL) > 0) - return 0; + if (!m) + return; - assert(m->udev_monitor); + free(m->state_file); - r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_monitor, "net", NULL); - if (r < 0) - return log_error_errno(r, "Could not add udev monitor filter: %m"); + udev_monitor_unref(m->udev_monitor); + udev_unref(m->udev); + sd_bus_unref(m->bus); + sd_bus_slot_unref(m->prepare_for_sleep_slot); + sd_event_source_unref(m->udev_event_source); + sd_event_source_unref(m->bus_retry_event_source); + sd_event_unref(m->event); - r = udev_monitor_enable_receiving(m->udev_monitor); - if (r < 0) { - log_error("Could not enable udev monitor"); - return r; - } + while ((link = hashmap_first(m->links))) + link_unref(link); + hashmap_free(m->links); - r = sd_event_add_io(m->event, - &m->udev_event_source, - udev_monitor_get_fd(m->udev_monitor), - EPOLLIN, manager_dispatch_link_udev, - m); + while ((network = m->networks)) + network_free(network); + + while ((netdev = hashmap_first(m->netdevs))) + netdev_unref(netdev); + hashmap_free(m->netdevs); + + while ((pool = m->address_pools)) + address_pool_free(pool); + + sd_rtnl_unref(m->rtnl); + + free(m); +} + +int manager_load_config(Manager *m) { + int r; + + /* update timestamp */ + paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, true); + + r = netdev_load(m); if (r < 0) return r; - r = sd_event_source_set_description(m->udev_event_source, "networkd-udev"); + r = network_load(m); if (r < 0) return r; return 0; } -int manager_rtnl_listen(Manager *m) { +bool manager_should_reload(Manager *m) { + return paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, false); +} + +int manager_rtnl_enumerate_links(Manager *m) { + _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL; + sd_rtnl_message *link; int r; assert(m); + assert(m->rtnl); - r = sd_rtnl_attach_event(m->rtnl, m->event, 0); + r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0); if (r < 0) return r; - r = sd_rtnl_add_match(m->rtnl, RTM_NEWLINK, &manager_rtnl_process_link, m); + r = sd_rtnl_message_request_dump(req, true); if (r < 0) return r; - r = sd_rtnl_add_match(m->rtnl, RTM_DELLINK, &manager_rtnl_process_link, m); + r = sd_rtnl_call(m->rtnl, req, 0, &reply); if (r < 0) return r; - r = sd_rtnl_add_match(m->rtnl, RTM_NEWADDR, &link_rtnl_process_address, m); - if (r < 0) - return r; + for (link = reply; link; link = sd_rtnl_message_next(link)) { + int k; - r = sd_rtnl_add_match(m->rtnl, RTM_DELADDR, &link_rtnl_process_address, m); - if (r < 0) - return r; + k = manager_rtnl_process_link(m->rtnl, link, m); + if (k < 0) + r = k; + } - return 0; + return r; } -int manager_bus_listen(Manager *m) { +int manager_rtnl_enumerate_addresses(Manager *m) { + _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL; + sd_rtnl_message *addr; int r; - assert(m->event); + assert(m); + assert(m->rtnl); - if (!m->bus) /* TODO: drop when we can rely on kdbus */ - return 0; + r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDR, 0, 0); + if (r < 0) + return r; - r = sd_bus_attach_event(m->bus, m->event, 0); + r = sd_rtnl_message_request_dump(req, true); if (r < 0) return r; - return 0; + r = sd_rtnl_call(m->rtnl, req, 0, &reply); + if (r < 0) + return r; + + for (addr = reply; addr; addr = sd_rtnl_message_next(addr)) { + int k; + + k = link_rtnl_process_address(m->rtnl, addr, m); + if (k < 0) + r = k; + } + + return r; } static int set_put_in_addr(Set *s, const struct in_addr *address) { diff --git a/src/network/networkd.c b/src/network/networkd.c index ced319de1d..0b4af90677 100644 --- a/src/network/networkd.c +++ b/src/network/networkd.c @@ -86,24 +86,6 @@ int main(int argc, char *argv[]) { goto out; } - r = manager_udev_listen(m); - if (r < 0) { - log_error_errno(r, "Could not connect to udev: %m"); - goto out; - } - - r = manager_rtnl_listen(m); - if (r < 0) { - log_error_errno(r, "Could not connect to rtnl: %m"); - goto out; - } - - r = manager_bus_listen(m); - if (r < 0) { - log_error_errno(r, "Could not connect to system bus: %m"); - goto out; - } - r = manager_load_config(m); if (r < 0) { log_error_errno(r, "Could not load configuration files: %m"); diff --git a/src/network/networkd.h b/src/network/networkd.h index a4ac8e3503..075aefec71 100644 --- a/src/network/networkd.h +++ b/src/network/networkd.h @@ -229,10 +229,6 @@ bool manager_should_reload(Manager *m); int manager_rtnl_enumerate_links(Manager *m); int manager_rtnl_enumerate_addresses(Manager *m); -int manager_rtnl_listen(Manager *m); -int manager_udev_listen(Manager *m); -int manager_bus_listen(Manager *m); - int manager_save(Manager *m); int manager_address_pool_acquire(Manager *m, int family, unsigned prefixlen, union in_addr_union *found); diff --git a/src/network/test-network.c b/src/network/test-network.c index b4a7be00f5..5909cc790e 100644 --- a/src/network/test-network.c +++ b/src/network/test-network.c @@ -209,8 +209,6 @@ int main(void) { test_network_get(manager, loopback); - assert_se(manager_udev_listen(manager) >= 0); - assert_se(manager_rtnl_listen(manager) >= 0); assert_se(manager_rtnl_enumerate_links(manager) >= 0); udev_device_unref(loopback); |