chiark / gitweb /
networkd: link - make state changing logging a bit less verbose
[elogind.git] / src / network / networkd-link.c
index 684e1e5d3d9ca9fc01d464c32cec9960ab7abb39..f179af332ca286fa16af25e8c6874931ecfcbed9 100644 (file)
@@ -1031,6 +1031,8 @@ static int link_acquire_conf(Link *link) {
 }
 
 static int link_update_flags(Link *link, unsigned flags) {
+        unsigned flags_added, flags_removed, generic_flags;
+        bool carrier_gained, carrier_lost;
         int r;
 
         assert(link);
@@ -1042,50 +1044,78 @@ static int link_update_flags(Link *link, unsigned flags) {
         if (link->flags == flags)
                 return 0;
 
-        log_debug_link(link, "link status updated: %#.8x -> %#.8x",
-                       link->flags, flags);
+        flags_added = (link->flags ^ flags) & flags;
+        flags_removed = (link->flags ^ flags) & link->flags;
+        generic_flags = ~(IFF_UP | IFF_LOWER_UP | IFF_RUNNING);
 
-        if ((link->flags & IFF_UP) != (flags & IFF_UP))
-                log_info_link(link,
-                              "link is %s", flags & IFF_UP ? "up": "down");
+        /* 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));
 
-        if ((link->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
-                if (flags & IFF_LOWER_UP) {
-                        log_info_link(link, "carrier on");
+        link->flags = flags;
 
-                        if (link->network->dhcp || link->network->ipv4ll) {
-                                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;
-                                }
+        if (flags_added & generic_flags)
+                log_debug_link(link, "link flags gained: %#.8x",
+                               flags_added & generic_flags);
+
+        if (flags_removed & generic_flags)
+                log_debug_link(link, "link flags lost: %#.8x",
+                                flags_removed & generic_flags);
+
+        if (flags_added & IFF_UP)
+                log_info_link(link, "link is up");
+        else if (flags_removed & IFF_UP)
+                log_info_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 (carrier_gained) {
+                log_info_link(link, "gained carrier");
+
+                if (link->network->dhcp || link->network->ipv4ll) {
+                        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;
                         }
-                } else {
-                        log_info_link(link, "carrier off");
+                }
+        } 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->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;
-                                }
+                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;
                         }
                 }
         }
 
-        link->flags = flags;
-
         return 0;
 }
 
@@ -1460,10 +1490,10 @@ int link_update(Link *link, sd_rtnl_message *m) {
                                        PRIu16, link->original_mtu);
         }
 
+        /* The kernel may broadcast NEWLINK messages without the MAC address
+           set, simply ignore them. */
         r = sd_rtnl_message_read_ether_addr(m, IFLA_ADDRESS, &mac);
-        if (r < 0)
-                log_debug_link(link, "Could not get MAC address: %s", strerror(-r));
-        else {
+        if (r >= 0) {
                 if (memcmp(link->mac.ether_addr_octet, mac.ether_addr_octet, ETH_ALEN)) {
 
                         memcpy(link->mac.ether_addr_octet, mac.ether_addr_octet, ETH_ALEN);
@@ -1518,8 +1548,7 @@ int link_save(Link *link) {
         assert(link->state_file);
 
         state = link_state_to_string(link->state);
-        if (!state)
-                goto finish;
+        assert(state);
 
         r = fopen_temporary(link->state_file, &f, &temp_path);
         if (r < 0)