chiark / gitweb /
networkd: add address pool support
[elogind.git] / src / network / networkd-link.c
index 81872f7cc67adeeb7d77c91f4957c1ab9d79d1aa..e7753dc98cea309cd08610332ec1ba03808e8f47 100644 (file)
@@ -112,6 +112,11 @@ static void link_free(Link *link) {
                 address_free(address);
         }
 
+        while ((address = link->pool_addresses)) {
+                LIST_REMOVE(addresses, link->pool_addresses, address);
+                address_free(address);
+        }
+
         sd_dhcp_client_unref(link->dhcp_client);
         sd_dhcp_lease_unref(link->dhcp_lease);
 
@@ -1492,7 +1497,7 @@ static int enslave_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
 }
 
 static int link_enter_enslave(Link *link) {
-        NetDev *vlan, *macvlan;
+        NetDev *vlan, *macvlan, *vxlan;
         Iterator i;
         int r;
 
@@ -1508,7 +1513,8 @@ static int link_enter_enslave(Link *link) {
             !link->network->bond &&
             !link->network->tunnel &&
             hashmap_isempty(link->network->vlans) &&
-            hashmap_isempty(link->network->macvlans))
+            hashmap_isempty(link->network->macvlans) &&
+            hashmap_isempty(link->network->vxlans))
                 return link_enslaved(link);
 
         if (link->network->bond) {
@@ -1625,6 +1631,27 @@ static int link_enter_enslave(Link *link) {
                 link->enslaving ++;
         }
 
+        HASHMAP_FOREACH(vxlan, link->network->vxlans, i) {
+                log_struct_link(LOG_DEBUG, link,
+                                "MESSAGE=%*s: enslaving by '%s'",
+                                IFNAMSIZ,
+                                link->ifname, vxlan->ifname, NETDEV(vxlan), NULL);
+
+                r = netdev_enslave(vxlan, link, &enslave_handler);
+                if (r < 0) {
+                        log_struct_link(LOG_WARNING, link,
+                                        "MESSAGE=%*s: could not enslave by '%s': %s",
+                                        IFNAMSIZ,
+                                        link->ifname, vxlan->ifname, strerror(-r),
+                                        NETDEV(vxlan), NULL);
+                        link_enter_failed(link);
+                        return r;
+                }
+
+                link_ref(link);
+                link->enslaving ++;
+        }
+
         return 0;
 }
 
@@ -1723,6 +1750,8 @@ static int link_configure(Link *link) {
                         pool_start.s_addr = htobe32(be32toh(address->in_addr.in.s_addr) + 1);
                         r = sd_dhcp_server_set_lease_pool(link->dhcp_server,
                                                           &pool_start, 32);
+                        if (r < 0)
+                                return r;
 
                         break;
                 }
@@ -1740,7 +1769,7 @@ static int link_configure(Link *link) {
                 */
         }
 
-        if (link_has_carrier(link->flags, link->operstate)) {
+        if (link_has_carrier(link->flags, link->kernel_operstate)) {
                 r = link_acquire_conf(link);
                 if (r < 0)
                         return r;
@@ -1749,7 +1778,8 @@ static int link_configure(Link *link) {
         return link_enter_enslave(link);
 }
 
-int link_initialized(Link *link, struct udev_device *device) {
+static int link_initialized_and_synced(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
+        Link *link = userdata;
         Network *network;
         int r;
 
@@ -1760,12 +1790,9 @@ int link_initialized(Link *link, struct udev_device *device) {
         if (link->state != LINK_STATE_INITIALIZING)
                 return 0;
 
-        if (device)
-                link->udev_device = udev_device_ref(device);
-
-        log_debug_link(link, "udev initialized link");
+        log_debug_link(link, "link state is up-to-date");
 
-        r = network_get(link->manager, device, link->ifname, &link->mac, &network);
+        r = network_get(link->manager, link->udev_device, link->ifname, &link->mac, &network);
         if (r == -ENOENT) {
                 link_enter_unmanaged(link);
                 return 0;
@@ -1783,6 +1810,38 @@ int link_initialized(Link *link, struct udev_device *device) {
         return 0;
 }
 
+int link_initialized(Link *link, struct udev_device *device) {
+        _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
+        int r;
+
+        assert(link);
+        assert(link->manager);
+        assert(link->manager->rtnl);
+        assert(device);
+
+        if (link->state != LINK_STATE_INITIALIZING)
+                return 0;
+
+        log_debug_link(link, "udev initialized link");
+
+        link->udev_device = udev_device_ref(device);
+
+        /* udev has initialized the link, but we don't know if we have yet processed
+           the NEWLINK messages with the latest state. Do a GETLINK, when it returns
+           we know that the pending NEWLINKs have already been processed and that we
+           are up-to-date */
+
+        r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, link->ifindex);
+        if (r < 0)
+                return r;
+
+        r = sd_rtnl_call_async(link->manager->rtnl, req, link_initialized_and_synced, link, 0, NULL);
+        if (r < 0)
+                return r;
+
+        return 0;
+}
+
 int link_rtnl_process_address(sd_rtnl *rtnl, sd_rtnl_message *message, void *userdata) {
         Manager *m = userdata;
         Link *link = NULL;
@@ -1970,11 +2029,15 @@ int link_add(Manager *m, sd_rtnl_message *message, Link **ret) {
                         log_debug_link(link, "udev initializing link...");
                         return 0;
                 }
-        }
 
-        r = link_initialized(link, device);
-        if (r < 0)
-                return r;
+                r = link_initialized(link, device);
+                if (r < 0)
+                        return r;
+        } else {
+                r = link_initialized_and_synced(m->rtnl, NULL, link);
+                if (r < 0)
+                        return r;
+        }
 
         return 0;
 }
@@ -2150,6 +2213,8 @@ int link_save(Link *link) {
         }
 
         if (link->dhcp_lease) {
+                assert(link->network);
+
                 r = dhcp_lease_save(link->dhcp_lease, link->lease_file);
                 if (r < 0)
                         goto finish;