Commit 9f6550e359 for frr
commit 9f6550e359cfeddfc4061cd6dcf01139d6e3c77a
Author: Philippe Guibert <philippe.guibert@6wind.com>
Date: Mon Oct 12 17:47:00 2026 +0200
bgpd: add aigp of the recursive nexthop route
In an inter-AS option C setup, an ASBR receives a VPN route with the
AIGP attribute and with the address of the remote PE as nexthop. This
nexthop is resolved by a BGP labeled-unicast route, which also carries
the AIGP attribute. When the ASBR advertises the VPN route to the next
AS with itself as nexthop, the AIGP metric of the labeled-unicast route
is not added: the next AS only receives the AIGP metric of the VPN route
plus the zebra metric of the labeled-unicast route, which is its MED.
RFC 7311 section 3.4.3 defines how to increase the AIGP metric when the
nexthop is resolved recursively (steps 4 to 8): the AIGP metric of each
BGP route used to resolve the nexthop is added, then the IGP distance to
the last nexthop. If one of these BGP routes does not have the AIGP
attribute, the AIGP attribute must not be passed on (step 6).
bgp_aigp_metric_total() only adds the IGP metric of the nexthop given by
zebra.
Fix this by storing the type of the route which resolves the nexthop in
the nexthop cache. When it is a BGP route, the AIGP metric of its best
path is added, then the distance to its own nexthop, recursively, with
a depth limit of 8. When one of these BGP routes does not have the AIGP
attribute, or when more than 8 BGP routes resolve the nexthop, the AIGP
attribute is not advertised, and the best path selection keeps the
previous computation. While one of these BGP routes has no selected
path, the previous computation is used, so that the AIGP attribute is
not withdrawn then advertised again. Step 6 only applies to a received
AIGP attribute: when the path is received without it, the AIGP
attribute is set by the outbound route-map, the BGP speaker is its
originator (section 3.4.1), and it is advertised.
Signed-off-by: Philippe Guibert <philippe.guibert@6wind.com>
diff --git a/bgpd/bgp_attr.h b/bgpd/bgp_attr.h
index 1fe4656055..ba5abac276 100644
--- a/bgpd/bgp_attr.h
+++ b/bgpd/bgp_attr.h
@@ -802,17 +802,6 @@ static inline uint64_t bgp_aigp_metric_add(uint64_t aigp, uint64_t value)
return aigp > UINT64_MAX - value ? UINT64_MAX : aigp + value;
}
-static inline uint64_t bgp_aigp_metric_total(struct bgp_path_info *bpi, const struct attr *attr)
-{
- uint64_t aigp = bgp_attr_get_aigp_metric(attr);
-
- /* Don't increment if it's locally sourced */
- if (bpi->peer == bpi->peer->bgp->peer_self)
- return aigp;
-
- return bpi->extra ? bgp_aigp_metric_add(aigp, bpi->extra->igpmetric) : aigp;
-}
-
static inline void bgp_attr_set_med(struct attr *attr, uint32_t med)
{
attr->med = med;
diff --git a/bgpd/bgp_nexthop.h b/bgpd/bgp_nexthop.h
index 0cc4980d86..fc3b1c2141 100644
--- a/bgpd/bgp_nexthop.h
+++ b/bgpd/bgp_nexthop.h
@@ -22,6 +22,18 @@
#define BGP_MP_NEXTHOP_FAMILY NEXTHOP_FAMILY
+/* AIGP distance to a nexthop resolved by BGP routes, RFC 7311 section 3.4.3 */
+enum bgp_aigp_nexthop {
+ /* A BGP route resolving the nexthop has no selected path yet */
+ BGP_AIGP_NEXTHOP_UNKNOWN = 0,
+ BGP_AIGP_NEXTHOP_DISTANCE,
+ /*
+ * A BGP route resolving the nexthop has no AIGP attribute (step 6), or
+ * too many BGP routes resolve it: the AIGP attribute is not passed on
+ */
+ BGP_AIGP_NEXTHOP_NOT_PASSED_ON,
+};
+
PREDECL_RBTREE_UNIQ(bgp_nexthop_cache);
/* BGP nexthop cache value structure. */
@@ -91,6 +103,7 @@ struct bgp_nexthop_cache {
struct prefix prefix;
struct prefix resolved_prefix;
+ uint8_t resolved_type;
void *nht_info; /* In BGP, peer session */
LIST_HEAD(path_list, bgp_path_info) paths;
unsigned int path_count;
diff --git a/bgpd/bgp_nht.c b/bgpd/bgp_nht.c
index fde8cd321c..e611601c3d 100644
--- a/bgpd/bgp_nht.c
+++ b/bgpd/bgp_nht.c
@@ -715,6 +715,7 @@ static void bgp_process_nexthop_update(struct bgp_nexthop_cache *bnc,
struct peer *peer = bnc->nht_info;
prefix_copy(&bnc->resolved_prefix, &nhr->prefix);
+ bnc->resolved_type = nhr->type;
/* notify bgp fsm if nbr ip goes from invalid->valid */
if (!bnc->nexthop_num)
@@ -826,6 +827,7 @@ static void bgp_process_nexthop_update(struct bgp_nexthop_cache *bnc,
}
} else {
memset(&bnc->resolved_prefix, 0, sizeof(bnc->resolved_prefix));
+ bnc->resolved_type = ZEBRA_ROUTE_SYSTEM;
UNSET_FLAG(bnc->flags, BGP_NEXTHOP_EVPN_INCOMPLETE);
UNSET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
UNSET_FLAG(bnc->flags, BGP_NEXTHOP_LABELED_VALID);
@@ -1444,6 +1446,92 @@ static bool bgp_nht_handle_gr_stale_nh_unreach(struct bgp_path_info *path,
return true;
}
+#define BGP_AIGP_RECURSION_DEPTH_MAX 8
+
+/* Distance to the nexthop of a path, RFC 7311 section 3.4.3 steps 4 to 8 */
+static enum bgp_aigp_nexthop bgp_aigp_nexthop_distance(struct bgp_path_info *bpi,
+ uint64_t *distance, unsigned int depth)
+{
+ struct bgp_nexthop_cache *bnc = bpi->nexthop;
+ struct bgp_dest *dest BGP_DEST_AUTOUNLOCK = NULL;
+ struct bgp_path_info *resolving;
+ struct bgp_table *table;
+ enum bgp_aigp_nexthop ret;
+ uint64_t next;
+
+ if (!bnc || bnc->resolved_type != ZEBRA_ROUTE_BGP) {
+ *distance = bpi->extra ? bpi->extra->igpmetric : 0;
+ return BGP_AIGP_NEXTHOP_DISTANCE;
+ }
+
+ if (depth >= BGP_AIGP_RECURSION_DEPTH_MAX) {
+ if (BGP_DEBUG(nht, NHT))
+ zlog_debug("%s: nexthop %pFX resolved by more than %u BGP routes, AIGP not passed on",
+ __func__, &bnc->prefix, BGP_AIGP_RECURSION_DEPTH_MAX);
+ return BGP_AIGP_NEXTHOP_NOT_PASSED_ON;
+ }
+
+ table = bnc->bgp->rib[family2afi(bnc->resolved_prefix.family)][SAFI_UNICAST];
+ if (!table)
+ return BGP_AIGP_NEXTHOP_UNKNOWN;
+
+ dest = bgp_node_lookup(table, &bnc->resolved_prefix);
+ if (!dest)
+ return BGP_AIGP_NEXTHOP_UNKNOWN;
+
+ for (resolving = bgp_dest_get_bgp_path_info(dest); resolving; resolving = resolving->next)
+ if (CHECK_FLAG(resolving->flags, BGP_PATH_SELECTED))
+ break;
+
+ if (!resolving)
+ return BGP_AIGP_NEXTHOP_UNKNOWN;
+
+ if (!bgp_attr_exists(resolving->attr, BGP_ATTR_AIGP))
+ return BGP_AIGP_NEXTHOP_NOT_PASSED_ON;
+
+ ret = bgp_aigp_nexthop_distance(resolving, &next, depth + 1);
+ if (ret != BGP_AIGP_NEXTHOP_DISTANCE)
+ return ret;
+
+ *distance = bgp_aigp_metric_add(bgp_attr_get_aigp_metric(resolving->attr), next);
+ return BGP_AIGP_NEXTHOP_DISTANCE;
+}
+
+/*
+ * Returns false if the nexthop is resolved by a BGP route without AIGP
+ * attribute, or by more than BGP_AIGP_RECURSION_DEPTH_MAX BGP routes: the
+ * received AIGP attribute must not be advertised. A path received without
+ * AIGP attribute gets it from the outbound route-map: this AIGP attribute is
+ * originated (RFC 7311 section 3.4.1), and is kept. While a BGP route
+ * resolving the nexthop has no selected path, the IGP metric of the nexthop
+ * is added, and the AIGP attribute is kept rather than withdrawn.
+ */
+bool bgp_aigp_metric_total(struct bgp_path_info *bpi, const struct attr *attr, uint64_t *aigp)
+{
+ uint64_t distance;
+
+ *aigp = bgp_attr_get_aigp_metric(attr);
+
+ /* Don't increment if it's locally sourced */
+ if (bpi->peer == bpi->peer->bgp->peer_self)
+ return true;
+
+ switch (bgp_aigp_nexthop_distance(bpi, &distance, 0)) {
+ case BGP_AIGP_NEXTHOP_DISTANCE:
+ break;
+ case BGP_AIGP_NEXTHOP_UNKNOWN:
+ distance = bpi->extra ? bpi->extra->igpmetric : 0;
+ break;
+ case BGP_AIGP_NEXTHOP_NOT_PASSED_ON:
+ distance = bpi->extra ? bpi->extra->igpmetric : 0;
+ *aigp = bgp_aigp_metric_add(*aigp, distance);
+ return !bgp_attr_exists(bpi->attr, BGP_ATTR_AIGP);
+ }
+
+ *aigp = bgp_aigp_metric_add(*aigp, distance);
+ return true;
+}
+
/**
* evaluate_paths - Evaluate the paths/nets associated with a nexthop.
* ARGUMENTS:
diff --git a/bgpd/bgp_nht.h b/bgpd/bgp_nht.h
index 8f3f9de323..c837990ce3 100644
--- a/bgpd/bgp_nht.h
+++ b/bgpd/bgp_nht.h
@@ -6,6 +6,8 @@
#ifndef _BGP_NHT_H
#define _BGP_NHT_H
+struct attr;
+
/**
* bgp_nexthop_update() - process a nexthop update message from Zebra.
*/
@@ -78,6 +80,9 @@ extern void bgp_nht_reg_enhe_cap_intfs(struct peer *peer);
extern void bgp_nht_dereg_enhe_cap_intfs(struct peer *peer);
extern void evaluate_paths(struct bgp_nexthop_cache *bnc);
+extern bool bgp_aigp_metric_total(struct bgp_path_info *bpi, const struct attr *attr,
+ uint64_t *aigp);
+
extern void bgp_nht_ifp_up(struct interface *ifp);
extern void bgp_nht_ifp_down(struct interface *ifp);
diff --git a/bgpd/bgp_route.c b/bgpd/bgp_route.c
index 66bf122d3f..1bcec71a9b 100644
--- a/bgpd/bgp_route.c
+++ b/bgpd/bgp_route.c
@@ -1419,8 +1419,11 @@ int bgp_path_info_cmp(struct bgp *bgp, struct bgp_path_info *new,
/* 4. Tie-breaker - AIGP (Metric TLV) attribute */
if (bgp_attr_exists(newattr, BGP_ATTR_AIGP) && bgp_attr_exists(existattr, BGP_ATTR_AIGP) &&
CHECK_FLAG(bgp->flags, BGP_FLAG_COMPARE_AIGP)) {
- uint64_t new_aigp = bgp_aigp_metric_total(new, new->attr);
- uint64_t exist_aigp = bgp_aigp_metric_total(exist, exist->attr);
+ uint64_t new_aigp;
+ uint64_t exist_aigp;
+
+ bgp_aigp_metric_total(new, new->attr, &new_aigp);
+ bgp_aigp_metric_total(exist, exist->attr, &exist_aigp);
if (new_aigp < exist_aigp) {
*reason = bgp_path_selection_aigp;
@@ -3373,9 +3376,12 @@ bool subgroup_announce_check(struct bgp_dest *dest, struct bgp_path_info *pi,
if (AIGP_TRANSMIT_ALLOWED(peer)) {
if (nh_reset ||
CHECK_FLAG(attr->rmap_change_flags, BATTR_RMAP_NEXTHOP_PEER_ADDRESS)) {
- uint64_t aigp = bgp_aigp_metric_total(pi, attr);
+ uint64_t aigp;
- bgp_attr_set_aigp_metric(attr, aigp);
+ if (bgp_aigp_metric_total(pi, attr, &aigp))
+ bgp_attr_set_aigp_metric(attr, aigp);
+ else
+ bgp_attr_unset_aigp_metric(attr);
}
} else {
bgp_attr_unset_aigp_metric(attr);