chiark / gitweb /
networkd: minor simplification
[elogind.git] / src / network / networkd-manager.c
index 2c8a330d75c83c20cb9d0c90e696dd785faaf96b..ac207851cbb5b46ac5d2414fffefc79a3baec4fc 100644 (file)
 #include "virt.h"
 
 #include "sd-rtnl.h"
+#include "sd-daemon.h"
+
+/* use 8 MB for receive socket kernel queue. */
+#define RCVBUF_SIZE    (8*1024*1024)
 
 const char* const network_dirs[] = {
         "/etc/systemd/network",
@@ -72,9 +76,28 @@ static int setup_default_address_pool(Manager *m) {
         return 0;
 }
 
+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;
+}
+
 int manager_new(Manager **ret) {
         _cleanup_manager_free_ Manager *m = NULL;
-        int r;
+        int r, fd;
 
         m = new0(Manager, 1);
         if (!m)
@@ -93,8 +116,15 @@ int manager_new(Manager **ret) {
         sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL);
         sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL);
 
-        r = sd_rtnl_open(&m->rtnl, 3, RTNLGRP_LINK, RTNLGRP_IPV4_IFADDR,
-                         RTNLGRP_IPV6_IFADDR);
+        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);
+        if (r < 0)
+                return r;
+
+        r = sd_rtnl_inc_rcvbuf(m->rtnl, RCVBUF_SIZE);
         if (r < 0)
                 return r;
 
@@ -229,22 +259,33 @@ static int manager_rtnl_process_link(sd_rtnl *rtnl, sd_rtnl_message *message, vo
         assert(message);
         assert(m);
 
+        if (sd_rtnl_message_is_error(message)) {
+                r = sd_rtnl_message_get_errno(message);
+                if (r < 0)
+                        log_warning_errno(r, "rtnl: could not receive link: %m");
+
+                return 0;
+        }
+
         r = sd_rtnl_message_get_type(message, &type);
         if (r < 0) {
-                log_warning("rtnl: could not get message type");
+                log_warning_errno(r, "rtnl: could not get message type: %m");
                 return 0;
         }
 
         r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
-        if (r < 0 || ifindex <= 0) {
-                log_warning("rtnl: received link message without valid ifindex");
+        if (r < 0) {
+                log_warning_errno(r, "rtnl: could not get ifindex: %m");
+                return 0;
+        } else if (ifindex <= 0) {
+                log_warning("rtnl: received link message with invalid ifindex: %d", ifindex);
                 return 0;
         } else
                 link_get(m, ifindex, &link);
 
         r = sd_rtnl_message_read_string(message, IFLA_IFNAME, &name);
-        if (r < 0 || !name) {
-                log_warning("rtnl: received link message without valid ifname");
+        if (r < 0) {
+                log_warning_errno(r, "rtnl: received link message without ifname: %m");
                 return 0;
         } else
                 netdev_get(m, name, &netdev);
@@ -255,8 +296,7 @@ static int manager_rtnl_process_link(sd_rtnl *rtnl, sd_rtnl_message *message, vo
                         /* link is new, so add it */
                         r = link_add(m, message, &link);
                         if (r < 0) {
-                                log_debug("could not add new link: %s",
-                                           strerror(-r));
+                                log_warning_errno(r, "could not add new link: %m");
                                 return 0;
                         }
                 }
@@ -265,7 +305,7 @@ static int manager_rtnl_process_link(sd_rtnl *rtnl, sd_rtnl_message *message, vo
                         /* netdev exists, so make sure the ifindex matches */
                         r = netdev_set_ifindex(netdev, message);
                         if (r < 0) {
-                                log_debug("could not set ifindex on netdev");
+                                log_warning_errno(r, "could not set ifindex on netdev: %m");
                                 return 0;
                         }
                 }
@@ -292,7 +332,7 @@ static int manager_rtnl_process_link(sd_rtnl *rtnl, sd_rtnl_message *message, vo
 int manager_rtnl_enumerate_links(Manager *m) {
         _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL;
         sd_rtnl_message *link;
-        int r, k;
+        int r;
 
         assert(m);
         assert(m->rtnl);
@@ -310,16 +350,40 @@ int manager_rtnl_enumerate_links(Manager *m) {
                 return r;
 
         for (link = reply; link; link = sd_rtnl_message_next(link)) {
-                uint16_t type;
+                int k;
 
-                k = sd_rtnl_message_get_type(link, &type);
+                k = manager_rtnl_process_link(m->rtnl, link, m);
                 if (k < 0)
-                        return k;
+                        r = k;
+        }
 
-                if (type != RTM_NEWLINK)
-                        continue;
+        return r;
+}
 
-                k = manager_rtnl_process_link(m->rtnl, link, 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);
+        assert(m->rtnl);
+
+        r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDR, 0, 0);
+        if (r < 0)
+                return r;
+
+        r = sd_rtnl_message_request_dump(req, true);
+        if (r < 0)
+                return r;
+
+        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;
         }
@@ -349,10 +413,8 @@ int manager_udev_listen(Manager *m) {
         assert(m->udev_monitor);
 
         r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_monitor, "net", NULL);
-        if (r < 0) {
-                log_error("Could not add udev monitor filter: %s", strerror(-r));
-                return r;
-        }
+        if (r < 0)
+                return log_error_errno(r, "Could not add udev monitor filter: %m");
 
         r = udev_monitor_enable_receiving(m->udev_monitor);
         if (r < 0) {
@@ -590,7 +652,7 @@ int manager_save(Manager *m) {
         return 0;
 
 fail:
-        log_error("Failed to save network state to %s: %s", m->state_file, strerror(-r));
+        log_error_errno(r, "Failed to save network state to %s: %m", m->state_file);
         unlink(m->state_file);
         unlink(temp_path);
         return r;
@@ -615,3 +677,37 @@ int manager_address_pool_acquire(Manager *m, int family, unsigned prefixlen, uni
 
         return 0;
 }
+
+const char *address_family_boolean_to_string(AddressFamilyBoolean b) {
+        if (b == ADDRESS_FAMILY_YES ||
+            b == ADDRESS_FAMILY_NO)
+                return yes_no(b == ADDRESS_FAMILY_YES);
+
+        if (b == ADDRESS_FAMILY_IPV4)
+                return "ipv4";
+        if (b == ADDRESS_FAMILY_IPV6)
+                return "ipv6";
+
+        return NULL;
+}
+
+AddressFamilyBoolean address_family_boolean_from_string(const char *s) {
+        int r;
+
+        /* Make this a true superset of a boolean */
+
+        r = parse_boolean(s);
+        if (r > 0)
+                return ADDRESS_FAMILY_YES;
+        if (r == 0)
+                return ADDRESS_FAMILY_NO;
+
+        if (streq(s, "ipv4"))
+                return ADDRESS_FAMILY_IPV4;
+        if (streq(s, "ipv6"))
+                return ADDRESS_FAMILY_IPV6;
+
+        return _ADDRESS_FAMILY_BOOLEAN_INVALID;
+}
+
+DEFINE_CONFIG_PARSE_ENUM(config_parse_address_family_boolean, address_family_boolean, AddressFamilyBoolean, "Failed to parse option");