]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
network: introduce link_is_up() helper function
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 9 Mar 2026 01:57:06 +0000 (10:57 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 9 Mar 2026 01:57:06 +0000 (10:57 +0900)
src/network/netdev/bond.c
src/network/networkd-link.c
src/network/networkd-link.h
src/network/networkd-nexthop.c
src/network/networkd-route.c
src/network/networkd-setlink.c

index 0ae5fbdc43004594906437928fd99d4d512f776a..38f06d43b7fbdcc0d705365a3cbd3a8d818fed10 100644 (file)
@@ -73,7 +73,7 @@ static int netdev_bond_fill_message_create(NetDev *netdev, Link *link, sd_netlin
                         return r;
         }
 
-        bool up = link && FLAGS_SET(link->flags, IFF_UP);
+        bool up = link && link_is_up(link);
         bool has_slaves = link && !set_isempty(link->slaves);
 
         if (b->mode != _NETDEV_BOND_MODE_INVALID && !up && !has_slaves) {
index e49d5946aa67d61f6135c8ca9470170b17514e3c..c77b02b8e0192704f67b4a1b729330abb3b5c0fe 100644 (file)
@@ -815,7 +815,6 @@ int link_ipv6ll_gained(Link *link) {
 
 int link_handle_bound_to_list(Link *link) {
         bool required_up = false;
-        bool link_is_up = false;
         Link *l;
 
         assert(link);
@@ -826,18 +825,15 @@ int link_handle_bound_to_list(Link *link) {
         if (hashmap_isempty(link->bound_to_links))
                 return 0;
 
-        if (link->flags & IFF_UP)
-                link_is_up = true;
-
         HASHMAP_FOREACH(l, link->bound_to_links)
                 if (link_has_carrier(l)) {
                         required_up = true;
                         break;
                 }
 
-        if (!required_up && link_is_up)
+        if (!required_up && link_is_up(link))
                 return link_request_to_bring_up_or_down(link, /* up= */ false);
-        if (required_up && !link_is_up)
+        if (required_up && !link_is_up(link))
                 return link_request_to_bring_up_or_down(link, /* up= */ true);
 
         return 0;
@@ -2012,7 +2008,7 @@ void link_update_operstate(Link *link, bool also_update_master) {
                         carrier_state = LINK_CARRIER_STATE_ENSLAVED;
                 else
                         carrier_state = LINK_CARRIER_STATE_CARRIER;
-        } else if (link->flags & IFF_UP)
+        } else if (link_is_up(link))
                 carrier_state = LINK_CARRIER_STATE_NO_CARRIER;
         else
                 carrier_state = LINK_CARRIER_STATE_OFF;
@@ -2147,6 +2143,11 @@ bool link_has_carrier(Link *link) {
         return netif_has_carrier(link->kernel_operstate, link->flags);
 }
 
+bool link_is_up(Link *link) {
+        assert(link);
+        return FLAGS_SET(link->flags, IFF_UP);
+}
+
 bool link_multicast_enabled(Link *link) {
         assert(link);
 
@@ -2226,7 +2227,7 @@ static int link_update_flags(Link *link, sd_netlink_message *message) {
                         log_link_debug(link, "Unknown link flags lost, ignoring: %#.5x", unknown_flags_removed);
         }
 
-        link_was_admin_up = link->flags & IFF_UP;
+        link_was_admin_up = link_is_up(link);
         had_carrier = link_has_carrier(link);
 
         link->flags = flags;
@@ -2236,9 +2237,9 @@ static int link_update_flags(Link *link, sd_netlink_message *message) {
 
         r = 0;
 
-        if (!link_was_admin_up && (link->flags & IFF_UP))
+        if (!link_was_admin_up && link_is_up(link))
                 r = link_admin_state_up(link);
-        else if (link_was_admin_up && !(link->flags & IFF_UP))
+        else if (link_was_admin_up && !link_is_up(link))
                 r = link_admin_state_down(link);
         if (r < 0)
                 return r;
index c5b9421bc0b2b396b5c1adbb6496943555bb9efa..9c641fad39bdb827b269410a2698a5b0d217950e 100644 (file)
@@ -229,6 +229,7 @@ void link_check_ready(Link *link);
 void link_update_operstate(Link *link, bool also_update_master);
 
 bool link_has_carrier(Link *link);
+bool link_is_up(Link *link);
 bool link_multicast_enabled(Link *link);
 
 bool link_ipv6_enabled(Link *link);
index 5e5f0d0e6858be618be4e15e76e3ea4613ef83a7..23941527cfdcfab128e0ee43948d841c6a2dd17a 100644 (file)
@@ -780,7 +780,7 @@ static bool nexthop_is_ready_to_configure(Link *link, const NextHop *nexthop) {
                  * kernel. */
                 if (link->set_flags_messages > 0)
                         return false;
-                if (!FLAGS_SET(link->flags, IFF_UP))
+                if (!link_is_up(link))
                         return false;
         }
 
@@ -995,7 +995,7 @@ void link_forget_nexthops(Link *link) {
         assert(link);
         assert(link->manager);
         assert(link->ifindex > 0);
-        assert(!FLAGS_SET(link->flags, IFF_UP));
+        assert(!link_is_up(link));
 
         /* See comments in link_forget_routes(). */
 
index 2d84ba1db071e9c0d969011d9cc61c61e0d4dada..593832df51aa87d5149fa1ce7a2fead76f7379a6 100644 (file)
@@ -1641,7 +1641,7 @@ int link_drop_routes(Link *link, bool only_static) {
 void link_forget_routes(Link *link) {
         assert(link);
         assert(link->ifindex > 0);
-        assert(!FLAGS_SET(link->flags, IFF_UP));
+        assert(!link_is_up(link));
 
         /* When an interface went down, IPv4 non-local routes bound to the interface are silently removed by
          * the kernel, without any notifications. Let's forget them in that case. Otherwise, when the link
index 7069b101f9f5548a7eb25903cd03a3e5737408e6..f5a43788ef79275a426b4d12a661e23d1906569d 100644 (file)
@@ -562,7 +562,7 @@ static int link_is_ready_to_set_link(Link *link, Request *req) {
         case REQUEST_TYPE_SET_LINK_CAN:
                 /* Do not check link->set_flags_messages here, as it is ok even if link->flags
                  * is outdated, and checking the counter causes a deadlock. */
-                if (FLAGS_SET(link->flags, IFF_UP)) {
+                if (link_is_up(link)) {
                         /* The CAN interface must be down to configure bitrate, etc... */
                         r = link_down_now(link);
                         if (r < 0)
@@ -626,14 +626,14 @@ static int link_is_ready_to_set_link(Link *link, Request *req) {
 
                 /* Do not check link->set_flags_messages here, as it is ok even if link->flags is outdated,
                  * and checking the counter causes a deadlock. */
-                if (link->network->bond && FLAGS_SET(link->flags, IFF_UP)) {
+                if (link->network->bond && link_is_up(link)) {
                         /* link must be down when joining to bond master. */
                         r = link_down_now(link);
                         if (r < 0)
                                 return r;
                 }
 
-                if (link->network->bridge && !FLAGS_SET(link->flags, IFF_UP) && link->dev) {
+                if (link->network->bridge && !link_is_up(link) && link->dev) {
                         /* Some devices require the port to be up before joining the bridge.
                          *
                          * E.g. Texas Instruments SoC Ethernet running in switch mode:
@@ -755,8 +755,7 @@ int link_request_to_set_addrgen_mode(Link *link) {
          * link goes down. Hence, we need to reset the interface. However, setting the mode by sysctl
          * does not need that. Let's use the sysctl interface when the link is already up.
          * See also issue #22424. */
-        if (mode != IPV6_LINK_LOCAL_ADDRESSS_GEN_MODE_NONE &&
-            FLAGS_SET(link->flags, IFF_UP)) {
+        if (mode != IPV6_LINK_LOCAL_ADDRESSS_GEN_MODE_NONE && link_is_up(link)) {
                 r = link_set_ipv6ll_addrgen_mode(link, mode);
                 if (r < 0)
                         log_link_warning_errno(link, r, "Cannot set IPv6 address generation mode, ignoring: %m");
@@ -1223,7 +1222,7 @@ static bool link_is_ready_to_bring_up_or_down(Link *link, bool up) {
                 if (link_get_by_index(link->manager, link->dsa_master_ifindex, &master) < 0)
                         return false;
 
-                if (!FLAGS_SET(master->flags, IFF_UP))
+                if (!link_is_up(master))
                         return false;
         }