chiark / gitweb /
networkd: link - reduce default verbosity a bit
[elogind.git] / src / network / networkd-link.c
index aad72debeb84d336e43f580fc01c0afc7ec56c55..fb873a9089416f21fae4a7250d29346465bac414 100644 (file)
@@ -141,13 +141,61 @@ static int link_enter_configured(Link *link) {
         return 0;
 }
 
+static void link_enter_unmanaged(Link *link) {
+        assert(link);
+
+        log_debug_link(link, "unmanaged");
+
+        link->state = LINK_STATE_UNMANAGED;
+
+        link_save(link);
+}
+
+static int link_stop_clients(Link *link) {
+        int r = 0, k;
+
+        assert(link);
+        assert(link->manager);
+        assert(link->manager->event);
+
+        if (!link->network)
+                return 0;
+
+        if (link->network->dhcp) {
+                assert(link->dhcp_client);
+
+                k = sd_dhcp_client_stop(link->dhcp_client);
+                if (k < 0) {
+                        log_warning_link(link, "Could not stop DHCPv4 client: %s", strerror(-r));
+                        r = k;
+                }
+        }
+
+        if (link->network->ipv4ll) {
+                assert(link->ipv4ll);
+
+                k = sd_ipv4ll_stop(link->ipv4ll);
+                if (k < 0) {
+                        log_warning_link(link, "Could not stop IPv4 link-local: %s", strerror(-r));
+                        r = k;
+                }
+        }
+
+        return r;
+}
+
 static void link_enter_failed(Link *link) {
         assert(link);
 
+        if (link->state == LINK_STATE_FAILED)
+                return;
+
         log_warning_link(link, "failed");
 
         link->state = LINK_STATE_FAILED;
 
+        link_stop_clients(link);
+
         link_save(link);
 }
 
@@ -1027,8 +1075,11 @@ static int link_acquire_conf(Link *link) {
                 log_debug_link(link, "acquiring IPv4 link-local address");
 
                 r = sd_ipv4ll_start(link->ipv4ll);
-                if (r < 0)
+                if (r < 0) {
+                        log_warning_link(link, "could not acquire IPv4 "
+                                         "link-local address");
                         return r;
+                }
         }
 
         if (link->network->dhcp) {
@@ -1037,16 +1088,34 @@ static int link_acquire_conf(Link *link) {
                 log_debug_link(link, "acquiring DHCPv4 lease");
 
                 r = sd_dhcp_client_start(link->dhcp_client);
-                if (r < 0)
+                if (r < 0) {
+                        log_warning_link(link, "could not acquire DHCPv4 "
+                                         "lease");
                         return r;
+                }
         }
 
         return 0;
 }
 
-static int link_update_flags(Link *link, unsigned flags) {
-        unsigned flags_added, flags_removed, generic_flags;
-        bool carrier_gained, carrier_lost;
+static bool link_has_carrier(unsigned flags, uint8_t operstate) {
+        /* see Documentation/networking/operstates.txt in the kernel sources */
+
+        if (operstate == IF_OPER_UP)
+                return true;
+
+        if (operstate == IF_OPER_UNKNOWN)
+                /* operstate may not be implemented, so fall back to flags */
+                if ((flags & IFF_LOWER_UP) && !(flags & IFF_DORMANT))
+                        return true;
+
+        return false;
+}
+
+static int link_update_flags(Link *link, sd_rtnl_message *m) {
+        unsigned flags, flags_added, flags_removed, generic_flags;
+        uint8_t operstate;
+        bool carrier_gained = false, carrier_lost = false;
         int r;
 
         assert(link);
@@ -1054,62 +1123,100 @@ static int link_update_flags(Link *link, unsigned flags) {
         if (link->state == LINK_STATE_FAILED)
                 return 0;
 
-        if (link->flags == flags)
+        r = sd_rtnl_message_link_get_flags(m, &flags);
+        if (r < 0) {
+                log_warning_link(link, "Could not get link flags");
+                return r;
+        }
+
+        r = sd_rtnl_message_read_u8(m, IFLA_OPERSTATE, &operstate);
+        if (r < 0)
+                /* if we got a message without operstate, take it to mean
+                   the state was unchanged */
+                operstate = link->operstate;
+
+        if ((link->flags == flags) && (link->operstate == operstate))
                 return 0;
 
         flags_added = (link->flags ^ flags) & flags;
         flags_removed = (link->flags ^ flags) & link->flags;
-        generic_flags = ~(IFF_UP | IFF_LOWER_UP | IFF_RUNNING);
-
-        /* consider link to have carrier when both RUNNING and LOWER_UP, as RUNNING
-           may mean that the oper state is unknown, in which case we should fall back to
-           simply trust LOWER_UP, even thought that is less reliable
-         */
-        carrier_gained = ((flags_added & IFF_LOWER_UP) && (flags & IFF_RUNNING)) ||
-                         ((flags_added & IFF_RUNNING) && (flags & IFF_LOWER_UP));
-        carrier_lost = ((link->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
-                        (IFF_RUNNING | IFF_LOWER_UP)) &&
-                       (flags_removed & (IFF_LOWER_UP | IFF_RUNNING));
-
-        link->flags = flags;
-
-        if (!link->network)
-                /* not currently managing this link
-                   we track state changes, but don't log them
-                   they will be logged if and when a network is
-                   applied */
-                return 0;
+        generic_flags = ~(IFF_UP | IFF_LOWER_UP | IFF_DORMANT | IFF_DEBUG |
+                          IFF_MULTICAST | IFF_BROADCAST | IFF_PROMISC |
+                          IFF_NOARP | IFF_MASTER | IFF_SLAVE);
 
         if (flags_added & IFF_UP)
-                log_info_link(link, "link is up");
+                log_debug_link(link, "link is up");
         else if (flags_removed & IFF_UP)
-                log_info_link(link, "link is down");
+                log_debug_link(link, "link is down");
 
         if (flags_added & IFF_LOWER_UP)
                 log_debug_link(link, "link is lower up");
         else if (flags_removed & IFF_LOWER_UP)
                 log_debug_link(link, "link is lower down");
 
-        if (flags_added & IFF_RUNNING)
-                log_debug_link(link, "link is running");
-        else if (flags_removed & IFF_RUNNING)
-                log_debug_link(link, "link is not running");
-
+        if (flags_added & IFF_DORMANT)
+                log_debug_link(link, "link is dormant");
+        else if (flags_removed & IFF_DORMANT)
+                log_debug_link(link, "link is not dormant");
+
+        if (flags_added & IFF_DEBUG)
+                log_debug_link(link, "debugging enabled in the kernel");
+        else if (flags_removed & IFF_DEBUG)
+                log_debug_link(link, "debugging disabled in the kernel");
+
+        if (flags_added & IFF_MULTICAST)
+                log_debug_link(link, "multicast enabled");
+        else if (flags_removed & IFF_MULTICAST)
+                log_debug_link(link, "multicast disabled");
+
+        if (flags_added & IFF_BROADCAST)
+                log_debug_link(link, "broadcast enabled");
+        else if (flags_removed & IFF_BROADCAST)
+                log_debug_link(link, "broadcast disabled");
+
+        if (flags_added & IFF_PROMISC)
+                log_debug_link(link, "promiscuous mode enabled");
+        else if (flags_removed & IFF_PROMISC)
+                log_debug_link(link, "promiscuous mode disabled");
+
+        if (flags_added & IFF_NOARP)
+                log_debug_link(link, "ARP protocol disabled");
+        else if (flags_removed & IFF_NOARP)
+                log_debug_link(link, "ARP protocol enabled");
+
+        if (flags_added & IFF_MASTER)
+                log_debug_link(link, "link is master");
+        else if (flags_removed & IFF_MASTER)
+                log_debug_link(link, "link is no longer master");
+
+        if (flags_added & IFF_SLAVE)
+                log_debug_link(link, "link is slave");
+        else if (flags_removed & IFF_SLAVE)
+                log_debug_link(link, "link is no longer slave");
+
+        /* link flags are currently at most 18 bits, let's default to printing 20 */
         if (flags_added & generic_flags)
-                log_debug_link(link, "ignored link flags gained: %#.8x",
+                log_debug_link(link, "unknown link flags gained: %#.5x (ignoring)",
                                flags_added & generic_flags);
 
         if (flags_removed & generic_flags)
-                log_debug_link(link, "ignored link flags lost: %#.8x",
-                                flags_removed & generic_flags);
+                log_debug_link(link, "unknown link flags lost: %#.5x (ignoring)",
+                               flags_removed & generic_flags);
+
+        carrier_gained = !link_has_carrier(link->flags, link->operstate) &&
+                       link_has_carrier(flags, operstate);
+        carrier_lost = link_has_carrier(link->flags, link->operstate) &&
+                         !link_has_carrier(flags, operstate);
+
+        link->flags = flags;
+        link->operstate = operstate;
 
         if (carrier_gained) {
                 log_info_link(link, "gained carrier");
 
-                if (link->network->dhcp || link->network->ipv4ll) {
+                if (link->network) {
                         r = link_acquire_conf(link);
                         if (r < 0) {
-                                log_warning_link(link, "Could not acquire configuration: %s", strerror(-r));
                                 link_enter_failed(link);
                                 return r;
                         }
@@ -1117,22 +1224,10 @@ static int link_update_flags(Link *link, unsigned flags) {
         } else if (carrier_lost) {
                 log_info_link(link, "lost carrier");
 
-                if (link->network->dhcp) {
-                        r = sd_dhcp_client_stop(link->dhcp_client);
-                        if (r < 0) {
-                                log_warning_link(link, "Could not stop DHCPv4 client: %s", strerror(-r));
-                                link_enter_failed(link);
-                                return r;
-                        }
-                }
-
-                if (link->network->ipv4ll) {
-                        r = sd_ipv4ll_stop(link->ipv4ll);
-                        if (r < 0) {
-                                log_warning_link(link, "Could not stop IPv4 link-local: %s", strerror(-r));
-                                link_enter_failed(link);
-                                return r;
-                        }
+                r = link_stop_clients(link);
+                if (r < 0) {
+                        link_enter_failed(link);
+                        return r;
                 }
         }
 
@@ -1149,14 +1244,15 @@ static int link_up_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
                 return 1;
 
         r = sd_rtnl_message_get_errno(m);
-        if (r >= 0)
-                link_update_flags(link, link->flags | IFF_UP);
-        else
+        if (r < 0) {
                 log_struct_link(LOG_WARNING, link,
                                 "MESSAGE=%s: could not bring up interface: %s",
                                 link->ifname, strerror(-r),
                                 "ERRNO=%d", -r,
                                 NULL);
+                link_enter_failed(link);
+        }
+
         return 1;
 }
 
@@ -1353,6 +1449,7 @@ static int link_configure(Link *link) {
 
         if (link->network->ipv4ll) {
                 uint8_t seed[8];
+
                 r = sd_ipv4ll_new(&link->ipv4ll);
                 if (r < 0)
                         return r;
@@ -1370,6 +1467,10 @@ static int link_configure(Link *link) {
                 if (r < 0)
                         return r;
 
+                r = sd_ipv4ll_set_mac(link->ipv4ll, &link->mac);
+                if (r < 0)
+                        return r;
+
                 r = sd_ipv4ll_set_index(link->ipv4ll, link->ifindex);
                 if (r < 0)
                         return r;
@@ -1388,6 +1489,10 @@ static int link_configure(Link *link) {
                 if (r < 0)
                         return r;
 
+                r = sd_dhcp_client_set_mac(link->dhcp_client, &link->mac);
+                if (r < 0)
+                        return r;
+
                 r = sd_dhcp_client_set_index(link->dhcp_client, link->ifindex);
                 if (r < 0)
                         return r;
@@ -1403,12 +1508,16 @@ static int link_configure(Link *link) {
                 }
         }
 
+        if (link_has_carrier(link->flags, link->operstate))
+                r = link_acquire_conf(link);
+                if (r < 0)
+                        return r;
+
         return link_enter_enslave(link);
 }
 
 int link_initialized(Link *link, struct udev_device *device) {
         Network *network;
-        unsigned flags;
         int r;
 
         assert(link);
@@ -1424,8 +1533,11 @@ int link_initialized(Link *link, struct udev_device *device) {
         log_debug_link(link, "link initialized");
 
         r = network_get(link->manager, device, link->ifname, &link->mac, &network);
-        if (r < 0)
-                return r == -ENOENT ? 0 : r;
+        if (r == -ENOENT) {
+                link_enter_unmanaged(link);
+                return 0;
+        } else if (r < 0)
+                return r;
 
         r = network_apply(link->manager, network, link);
         if (r < 0)
@@ -1435,13 +1547,6 @@ int link_initialized(Link *link, struct udev_device *device) {
         if (r < 0)
                 return r;
 
-        /* re-trigger all state updates */
-        flags = link->flags;
-        link->flags = 0;
-        r = link_update_flags(link, flags);
-        if (r < 0)
-                return r;
-
         return 0;
 }
 
@@ -1461,7 +1566,7 @@ int link_add(Manager *m, sd_rtnl_message *message, Link **ret) {
 
         link = *ret;
 
-        log_info_link(link, "link added");
+        log_debug_link(link, "link added");
 
         if (detect_container(NULL) <= 0) {
                 /* not in a container, udev will be around */
@@ -1485,7 +1590,6 @@ int link_add(Manager *m, sd_rtnl_message *message, Link **ret) {
 }
 
 int link_update(Link *link, sd_rtnl_message *m) {
-        unsigned flags;
         struct ether_addr mac;
         char *ifname;
         int r;
@@ -1494,7 +1598,7 @@ int link_update(Link *link, sd_rtnl_message *m) {
         assert(link->ifname);
         assert(m);
 
-        if (link->state == LINK_STATE_FAILED)
+        if (link->state == LINK_STATE_FAILED || link->state == LINK_STATE_UNMANAGED)
                 return 0;
 
         r = sd_rtnl_message_read_string(m, IFLA_IFNAME, &ifname);
@@ -1553,13 +1657,7 @@ int link_update(Link *link, sd_rtnl_message *m) {
                 }
         }
 
-        r = sd_rtnl_message_link_get_flags(m, &flags);
-        if (r < 0) {
-                log_warning_link(link, "Could not get link flags");
-                return r;
-        }
-
-        return link_update_flags(link, flags);
+        return link_update_flags(link, m);
 }
 
 int link_save(Link *link) {
@@ -1620,6 +1718,7 @@ static const char* const link_state_table[_LINK_STATE_MAX] = {
         [LINK_STATE_SETTING_ADDRESSES] = "configuring",
         [LINK_STATE_SETTING_ROUTES] = "configuring",
         [LINK_STATE_CONFIGURED] = "configured",
+        [LINK_STATE_UNMANAGED] = "unmanaged",
         [LINK_STATE_FAILED] = "failed",
 };