Commit 35803dddf0 for frr

commit 35803dddf03b0775dc2827d3dcdfac2f8843b809
Author: Donatas Abraitis <donatas@opensourcerouting.org>
Date:   Mon Nov 30 16:33:57 2026 +0200

    bgpd: Add helpers for runtime attr flag bits and partial attrs

    ATTR_FLAG_BIT() only accepts compile-time constants, so places that
    deal with an attribute type known only at runtime open-coded
    `1ULL << (type - 1)`, each with its own (or no) 1..64 range check.
    The partial_attrs bitmask was likewise updated with an open-coded
    get/modify/set sequence.

    No functional change, just for maintainability.

    Signed-off-by: Donatas Abraitis <donatas@opensourcerouting.org>

diff --git a/bgpd/bgp_attr.c b/bgpd/bgp_attr.c
index a5d88153f8..b794aaf784 100644
--- a/bgpd/bgp_attr.c
+++ b/bgpd/bgp_attr.c
@@ -1523,7 +1523,7 @@ static void attr_show_all_iterator(struct hash_bucket *bucket, void *args[])

 	/* Only attribute types 1-64 have a bit in attr->flag */
 	for (i = 1; i <= 64; i++) {
-		if (CHECK_FLAG(attr->flag, 1ULL << (i - 1)))
+		if (bgp_attr_exists_type(attr, i))
 			counters[i]++;
 	}

@@ -3007,8 +3007,7 @@ bgp_attr_munge_as4_attrs(struct peer *const peer, struct attr *const attr,
 		 * it must carry its Partial bit too (RFC 4271 section 5).
 		 */
 		if (!ignore_as4_path && bgp_attr_partial(attr, BGP_ATTR_AS4_AGGREGATOR))
-			bgp_attr_set_partial_attrs(attr, bgp_attr_get_partial_attrs(attr) |
-								 ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR));
+			bgp_attr_partial_set(attr, BGP_ATTR_AGGREGATOR);
 	}

 	/* need to reconcile NEW_AS_PATH and AS_PATH */
@@ -5146,10 +5145,8 @@ enum bgp_attr_parse_ret bgp_attr_parse(struct peer_connection *connection, struc
 		 * to 1 by some previous AS, it MUST NOT be set back to 0 by the
 		 * current AS.
 		 */
-		if (CHECK_FLAG(flag, BGP_ATTR_FLAG_PARTIAL) && type >= 1 && type <= 64 &&
-		    CHECK_FLAG(attr->flag, 1ULL << (type - 1)))
-			bgp_attr_set_partial_attrs(attr, bgp_attr_get_partial_attrs(attr) |
-								 (1ULL << (type - 1)));
+		if (CHECK_FLAG(flag, BGP_ATTR_FLAG_PARTIAL) && bgp_attr_exists_type(attr, type))
+			bgp_attr_partial_set(attr, type);
 	}

 	/*
diff --git a/bgpd/bgp_attr.h b/bgpd/bgp_attr.h
index a7157d5347..1fe4656055 100644
--- a/bgpd/bgp_attr.h
+++ b/bgpd/bgp_attr.h
@@ -402,6 +402,16 @@ struct transit {
 #define bgp_attr_set(attr, id) SET_FLAG((attr)->flag, ATTR_FLAG_BIT(id))
 #define bgp_attr_unset(attr, id) UNSET_FLAG((attr)->flag, ATTR_FLAG_BIT(id))

+static inline uint64_t attr_flag_bit(unsigned int type)
+{
+	return (type >= 1 && type <= 64) ? 1ULL << (type - 1) : 0;
+}
+
+static inline bool bgp_attr_exists_type(const struct attr *attr, unsigned int type)
+{
+	return CHECK_FLAG(attr->flag, attr_flag_bit(type));
+}
+
 #define BGP_CLUSTER_LIST_LENGTH(attr)                                                             \
 	(bgp_attr_exists(attr, BGP_ATTR_CLUSTER_LIST) ? bgp_attr_get_cluster((attr))->length : 0)

@@ -618,6 +628,16 @@ static inline void bgp_attr_set_partial_attrs(struct attr *attr, uint64_t partia
 	}
 }

+static inline void bgp_attr_partial_set(struct attr *attr, unsigned int type)
+{
+	bgp_attr_set_partial_attrs(attr, bgp_attr_get_partial_attrs(attr) | attr_flag_bit(type));
+}
+
+static inline void bgp_attr_partial_unset(struct attr *attr, unsigned int type)
+{
+	bgp_attr_set_partial_attrs(attr, bgp_attr_get_partial_attrs(attr) & ~attr_flag_bit(type));
+}
+
 static inline struct ecommunity *
 bgp_attr_get_ecommunity(const struct attr *attr)
 {
@@ -633,8 +653,7 @@ static inline void bgp_attr_set_ecommunity(struct attr *attr,
 		bgp_attr_set(attr, BGP_ATTR_EXT_COMMUNITIES);
 	else {
 		bgp_attr_unset(attr, BGP_ATTR_EXT_COMMUNITIES);
-		bgp_attr_set_partial_attrs(attr, bgp_attr_get_partial_attrs(attr) &
-							 ~(1ULL << (BGP_ATTR_EXT_COMMUNITIES - 1)));
+		bgp_attr_partial_unset(attr, BGP_ATTR_EXT_COMMUNITIES);
 	}
 }

@@ -653,9 +672,7 @@ static inline void bgp_attr_set_lcommunity(struct attr *attr,
 		bgp_attr_set(attr, BGP_ATTR_LARGE_COMMUNITIES);
 	else {
 		bgp_attr_unset(attr, BGP_ATTR_LARGE_COMMUNITIES);
-		bgp_attr_set_partial_attrs(attr,
-					   bgp_attr_get_partial_attrs(attr) &
-						   ~(1ULL << (BGP_ATTR_LARGE_COMMUNITIES - 1)));
+		bgp_attr_partial_unset(attr, BGP_ATTR_LARGE_COMMUNITIES);
 	}
 }

@@ -673,8 +690,7 @@ static inline void bgp_attr_set_community(struct attr *attr,
 		bgp_attr_set(attr, BGP_ATTR_COMMUNITIES);
 	else {
 		bgp_attr_unset(attr, BGP_ATTR_COMMUNITIES);
-		bgp_attr_set_partial_attrs(attr, bgp_attr_get_partial_attrs(attr) &
-							 ~(1ULL << (BGP_ATTR_COMMUNITIES - 1)));
+		bgp_attr_partial_unset(attr, BGP_ATTR_COMMUNITIES);
 	}
 }

@@ -702,9 +718,7 @@ static inline void bgp_attr_set_ipv6_ecommunity(struct attr *attr,
 		bgp_attr_set(attr, BGP_ATTR_IPV6_EXT_COMMUNITIES);
 	else {
 		bgp_attr_unset(attr, BGP_ATTR_IPV6_EXT_COMMUNITIES);
-		bgp_attr_set_partial_attrs(attr,
-					   bgp_attr_get_partial_attrs(attr) &
-						   ~(1ULL << (BGP_ATTR_IPV6_EXT_COMMUNITIES - 1)));
+		bgp_attr_partial_unset(attr, BGP_ATTR_IPV6_EXT_COMMUNITIES);
 	}
 }

@@ -1064,10 +1078,7 @@ static inline void bgp_attr_set_otc(struct attr *attr, uint32_t otc)

 static inline uint8_t bgp_attr_partial(const struct attr *attr, uint8_t type)
 {
-	if (type < 1 || type > 64)
-		return 0;
-
-	return CHECK_FLAG(bgp_attr_get_partial_attrs(attr), 1ULL << (type - 1))
+	return CHECK_FLAG(bgp_attr_get_partial_attrs(attr), attr_flag_bit(type))
 		       ? BGP_ATTR_FLAG_PARTIAL
 		       : 0;
 }