chiark / gitweb /
networkd: manager - enumerate addresses globally, rather than per-link
[elogind.git] / src / network / networkd-link.c
index a4f8c59e09d1e3e49516c406731bf7fb83a0747d..b9f1b992d31e71e5ce35a1387d8a8a83d2f701b6 100644 (file)
@@ -576,32 +576,6 @@ int link_route_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
         return 1;
 }
 
-int link_get_address_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
-        _cleanup_link_unref_ Link *link = userdata;
-        int r;
-
-        assert(rtnl);
-        assert(m);
-        assert(link);
-        assert(link->manager);
-
-        for (; m; m = sd_rtnl_message_next(m)) {
-                r = sd_rtnl_message_get_errno(m);
-                if (r < 0) {
-                        log_link_debug(link, "getting address failed: %s",
-                                       strerror(-r));
-                        continue;
-                }
-
-                r = link_rtnl_process_address(rtnl, m, link->manager);
-                if (r < 0)
-                        log_link_warning(link, "could not process address: %s",
-                                         strerror(-r));
-        }
-
-        return 1;
-}
-
 static int address_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
         _cleanup_link_unref_ Link *link = userdata;
         int r;
@@ -622,11 +596,8 @@ static int address_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
         r = sd_rtnl_message_get_errno(m);
         if (r < 0 && r != -EEXIST)
                 log_link_warning_errno(link, -r, "%-*s: could not set address: %m", IFNAMSIZ, link->ifname);
-        else if (r >= 0) {
-                /* calling handler directly so take a ref */
-                link_ref(link);
-                link_get_address_handler(rtnl, m, link);
-        }
+        else if (r >= 0)
+                link_rtnl_process_address(rtnl, m, link->manager);
 
         if (link->link_messages == 0) {
                 log_link_debug(link, "addresses set");
@@ -689,11 +660,11 @@ static int link_set_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
         _cleanup_link_unref_ Link *link = userdata;
         int r;
 
-        log_debug_link(link, "set link");
+        log_link_debug(link, "set link");
 
         r = sd_rtnl_message_get_errno(m);
         if (r < 0 && r != -EEXIST) {
-                log_struct_link(LOG_ERR, link,
+                log_link_struct(link, LOG_ERR,
                                 "MESSAGE=%-*s: could not join netdev: %s",
                                 IFNAMSIZ,
                                 link->ifname, strerror(-r),
@@ -836,20 +807,20 @@ static int link_set_bridge(Link *link) {
         r = sd_rtnl_message_new_link(link->manager->rtnl, &req,
                                      RTM_SETLINK, link->ifindex);
         if (r < 0) {
-                log_error_link(link, "Could not allocate RTM_SETLINK message");
+                log_link_error(link, "Could not allocate RTM_SETLINK message");
                 return r;
         }
 
         r = sd_rtnl_message_link_set_family(req, PF_BRIDGE);
         if (r < 0) {
-                log_error_link(link,
+                log_link_error(link,
                                "Could not set message family %s", strerror(-r));
                 return r;
         }
 
         r = sd_rtnl_message_open_container(req, IFLA_PROTINFO);
         if (r < 0) {
-                log_error_link(link,
+                log_link_error(link,
                                "Could not append IFLA_PROTINFO attribute: %s",
                                strerror(-r));
                 return r;
@@ -858,7 +829,7 @@ static int link_set_bridge(Link *link) {
         if(link->network->cost != 0) {
                 r = sd_rtnl_message_append_u32(req, IFLA_BRPORT_COST, link->network->cost);
                 if (r < 0) {
-                        log_error_link(link,
+                        log_link_error(link,
                                        "Could not append IFLA_BRPORT_COST attribute: %s",
                                        strerror(-r));
                         return r;
@@ -867,7 +838,7 @@ static int link_set_bridge(Link *link) {
 
         r = sd_rtnl_message_close_container(req);
         if (r < 0) {
-                log_error_link(link,
+                log_link_error(link,
                                "Could not append IFLA_LINKINFO attribute: %s",
                                strerror(-r));
                 return r;
@@ -875,7 +846,7 @@ static int link_set_bridge(Link *link) {
 
         r = sd_rtnl_call_async(link->manager->rtnl, req, link_set_handler, link, 0, NULL);
         if (r < 0) {
-                log_error_link(link,
+                log_link_error(link,
                                "Could not send rtnetlink message: %s",
                                strerror(-r));
                 return r;
@@ -1074,6 +1045,7 @@ static int link_up(Link *link) {
         int r;
 
         assert(link);
+        assert(link->network);
         assert(link->manager);
         assert(link->manager->rtnl);
 
@@ -1093,6 +1065,22 @@ static int link_up(Link *link) {
                 return r;
         }
 
+        if (link->network->mac) {
+                r = sd_rtnl_message_append_ether_addr(req, IFLA_ADDRESS, link->network->mac);
+                if (r < 0) {
+                        log_link_error(link, "Could not set MAC address: %s", strerror(-r));
+                        return r;
+                }
+        }
+
+        if (link->network->mtu) {
+                r = sd_rtnl_message_append_u32(req, IFLA_MTU, link->network->mtu);
+                if (r < 0) {
+                        log_link_error(link, "Could not set MTU: %s", strerror(-r));
+                        return r;
+                }
+        }
+
         r = sd_rtnl_call_async(link->manager->rtnl, req, link_up_handler, link,
                                0, NULL);
         if (r < 0) {
@@ -1124,7 +1112,7 @@ static int link_joined(Link *link) {
         if(link->network->bridge) {
                 r = link_set_bridge(link);
                 if (r < 0) {
-                        log_error_link(link,
+                        log_link_error(link,
                                        "Could not set bridge message: %s",
                                        strerror(-r));
                 }
@@ -1400,8 +1388,7 @@ int link_initialized(Link *link, struct udev_device *device) {
         return 0;
 }
 
-int link_rtnl_process_address(sd_rtnl *rtnl, sd_rtnl_message *message,
-                              void *userdata) {
+int link_rtnl_process_address(sd_rtnl *rtnl, sd_rtnl_message *message, void *userdata) {
         Manager *m = userdata;
         Link *link = NULL;
         uint16_t type;
@@ -1417,6 +1404,14 @@ int link_rtnl_process_address(sd_rtnl *rtnl, sd_rtnl_message *message,
         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: failed to receive address: %m");
+
+                return 0;
+        }
+
         r = sd_rtnl_message_get_type(message, &type);
         if (r < 0) {
                 log_warning("rtnl: could not get message type");
@@ -1424,13 +1419,16 @@ int link_rtnl_process_address(sd_rtnl *rtnl, sd_rtnl_message *message,
         }
 
         r = sd_rtnl_message_addr_get_ifindex(message, &ifindex);
-        if (r < 0 || ifindex <= 0) {
-                log_warning("rtnl: received address message without valid ifindex, ignoring");
+        if (r < 0) {
+                log_warning_errno(r, "rtnl: could not get ifindex: %m");
+                return 0;
+        } else if (ifindex <= 0) {
+                log_warning("rtnl: received address message with invalid ifindex: %d", ifindex);
                 return 0;
         } else {
                 r = link_get(m, ifindex, &link);
                 if (r < 0 || !link) {
-                        log_warning("rtnl: received address for nonexistent link (%d), ignoring", ifindex);
+                        log_warning("rtnl: received address for nonexistent link (%d), ignoring", ifindex);
                         return 0;
                 }
         }
@@ -1577,18 +1575,6 @@ int link_add(Manager *m, sd_rtnl_message *message, Link **ret) {
 
         log_link_debug(link, "link %d added", link->ifindex);
 
-        r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDR, link->ifindex,
-                                     0);
-        if (r < 0)
-                return r;
-
-        r = sd_rtnl_call_async(m->rtnl, req, link_get_address_handler, link, 0,
-                               NULL);
-        if (r < 0)
-                return r;
-
-        link_ref(link);
-
         if (detect_container(NULL) <= 0) {
                 /* not in a container, udev will be around */
                 sprintf(ifindex_str, "n%d", link->ifindex);