Commit 091b4b7a72 for frr
commit 091b4b7a72858d5f797bc0a3376fe1d0798fe1a7
Author: Philippe Guibert <philippe.guibert@6wind.com>
Date: Mon Oct 12 17:47:57 2026 +0200
bgpd: recompute aigp when the aigp of a nexthop route changes
In an inter-AS option C setup, the AIGP metric that an ASBR advertises
for a VPN route includes the AIGP metric of the labeled-unicast route
which resolves the nexthop of the VPN route. When the AIGP metric of
this labeled-unicast route changes, the VPN route is not advertised
again, and the next AS keeps the previous AIGP metric. The same happens
when the nexthop of the labeled-unicast route is itself resolved by a
BGP route whose AIGP metric changes, or when the IGP distance to the
nexthop of the labeled-unicast route changes.
RFC 7311 section 3.4.3 requires a new AIGP computation when the AIGP
metric of a route used to resolve the nexthop changes. Zebra does not
know the AIGP metric, so no nexthop update is received in that case.
Fix this by looking up the nexthops resolved by a route when its best
path changes, or when its attributes or its IGP metric change. The
nexthops resolved by a BGP route are indexed by the prefix of this
route, so that the lookup does not depend on the size of the nexthop
cache. The BGP_NODE_NHT_RESOLVED_NODE flag of the route can not be used
to trigger the lookup: it is cleared when one nexthop stops using the
route, even if other nexthops still use it. The AIGP distance to each
nexthop resolved by this route is stored in the nexthop cache; when it
changes, the paths that use this nexthop are processed again, flagged
with BGP_PATH_IGP_CHANGED so that they are advertised again. The paths
without the AIGP attribute are processed too, since an outbound
route-map may add it with "set aigp-metric". When such a
path itself resolves other nexthops, the change is passed on to the
next level. Since the propagation stops at nexthops whose distance did
not change, a resolution loop does not process routes forever.
The lookup is done once the selected flag is moved to the new best path,
since the AIGP distance is read from the selected path.
Also process the paths again when a nexthop update from zebra changes
the route resolving the nexthop from or to a BGP route, or changes the
BGP route resolving it, even if the metric and the nexthops are the
same.
Signed-off-by: Philippe Guibert <philippe.guibert@6wind.com>
diff --git a/bgpd/bgp_nexthop.c b/bgpd/bgp_nexthop.c
index 4bed396b33..3172b8b729 100644
--- a/bgpd/bgp_nexthop.c
+++ b/bgpd/bgp_nexthop.c
@@ -89,8 +89,41 @@ bool bnc_existing_for_prefix(struct bgp_nexthop_cache *bnc)
return false;
}
+int bgp_nexthop_resolved_compare(const struct bgp_nexthop_cache *a,
+ const struct bgp_nexthop_cache *b)
+{
+ int ret = prefix_cmp(&a->resolved_prefix, &b->resolved_prefix);
+
+ if (ret)
+ return ret;
+
+ if (a->afi != b->afi)
+ return numcmp(a->afi, b->afi);
+
+ return bgp_nexthop_cache_compare(a, b);
+}
+
+void bnc_set_resolved(struct bgp_nexthop_cache *bnc, uint8_t type, const struct prefix *prefix)
+{
+ struct bgp *bgp = bnc->bgp;
+ bool indexed = bnc->tree == &bgp->nexthop_cache_table[bnc->afi];
+
+ if (indexed && bnc->resolved_type == ZEBRA_ROUTE_BGP)
+ bgp_nexthop_resolved_del(&bgp->nexthop_cache_resolved, bnc);
+
+ bnc->resolved_type = type;
+ if (prefix)
+ prefix_copy(&bnc->resolved_prefix, prefix);
+ else
+ memset(&bnc->resolved_prefix, 0, sizeof(bnc->resolved_prefix));
+
+ if (indexed && type == ZEBRA_ROUTE_BGP)
+ bgp_nexthop_resolved_add(&bgp->nexthop_cache_resolved, bnc);
+}
+
void bnc_free(struct bgp_nexthop_cache *bnc)
{
+ bnc_set_resolved(bnc, ZEBRA_ROUTE_SYSTEM, NULL);
bnc_nexthop_free(bnc);
bgp_nexthop_cache_del(bnc->tree, bnc);
XFREE(MTYPE_BGP_NEXTHOP_CACHE, bnc);
@@ -1397,6 +1430,7 @@ void bgp_scan_init(struct bgp *bgp)
bgp->connected_table[afi] = bgp_table_init(bgp, afi,
SAFI_UNICAST);
}
+ bgp_nexthop_resolved_init(&bgp->nexthop_cache_resolved);
}
void bgp_scan_vty_init(void)
diff --git a/bgpd/bgp_nexthop.h b/bgpd/bgp_nexthop.h
index fc3b1c2141..7244a45076 100644
--- a/bgpd/bgp_nexthop.h
+++ b/bgpd/bgp_nexthop.h
@@ -35,6 +35,7 @@ enum bgp_aigp_nexthop {
};
PREDECL_RBTREE_UNIQ(bgp_nexthop_cache);
+PREDECL_RBTREE_UNIQ(bgp_nexthop_resolved);
/* BGP nexthop cache value structure. */
struct bgp_nexthop_cache {
@@ -104,6 +105,11 @@ struct bgp_nexthop_cache {
struct prefix prefix;
struct prefix resolved_prefix;
uint8_t resolved_type;
+ /* Entry in the nexthops resolved by a BGP route */
+ struct bgp_nexthop_resolved_item resolved_entry;
+ /* AIGP distance through the BGP route resolving the nexthop */
+ uint64_t aigp_distance;
+ enum bgp_aigp_nexthop aigp_state;
void *nht_info; /* In BGP, peer session */
LIST_HEAD(path_list, bgp_path_info) paths;
unsigned int path_count;
@@ -114,6 +120,10 @@ extern int bgp_nexthop_cache_compare(const struct bgp_nexthop_cache *a,
const struct bgp_nexthop_cache *b);
DECLARE_RBTREE_UNIQ(bgp_nexthop_cache, struct bgp_nexthop_cache, entry,
bgp_nexthop_cache_compare);
+extern int bgp_nexthop_resolved_compare(const struct bgp_nexthop_cache *a,
+ const struct bgp_nexthop_cache *b);
+DECLARE_RBTREE_UNIQ(bgp_nexthop_resolved, struct bgp_nexthop_cache, resolved_entry,
+ bgp_nexthop_resolved_compare);
/* Own tunnel-ip address structure */
struct tip_addr {
@@ -154,6 +164,8 @@ extern struct bgp_nexthop_cache *bnc_new(struct bgp_nexthop_cache_head *tree,
ifindex_t ifindex);
extern bool bnc_existing_for_prefix(struct bgp_nexthop_cache *bnc);
extern void bnc_free(struct bgp_nexthop_cache *bnc);
+extern void bnc_set_resolved(struct bgp_nexthop_cache *bnc, uint8_t type,
+ const struct prefix *prefix);
extern struct bgp_nexthop_cache *bnc_find(struct bgp_nexthop_cache_head *tree,
struct prefix *prefix,
uint32_t srte_color,
diff --git a/bgpd/bgp_nht.c b/bgpd/bgp_nht.c
index e611601c3d..738de05bd6 100644
--- a/bgpd/bgp_nht.c
+++ b/bgpd/bgp_nht.c
@@ -663,6 +663,8 @@ static void bgp_bnc_mark_nht_important(struct bgp_nexthop_cache *bnc, struct zap
bgp_dest_unlock_node(dest);
}
+static bool bgp_aigp_nexthop_cache_changed(struct bgp_nexthop_cache *bnc);
+
static void bgp_process_nexthop_update(struct bgp_nexthop_cache *bnc,
struct zapi_route *nhr,
bool import_check)
@@ -714,8 +716,12 @@ static void bgp_process_nexthop_update(struct bgp_nexthop_cache *bnc,
} else if (nhr->nexthop_num) {
struct peer *peer = bnc->nht_info;
- prefix_copy(&bnc->resolved_prefix, &nhr->prefix);
- bnc->resolved_type = nhr->type;
+ if ((bnc->resolved_type == ZEBRA_ROUTE_BGP || nhr->type == ZEBRA_ROUTE_BGP) &&
+ (bnc->resolved_type != nhr->type ||
+ !prefix_same(&bnc->resolved_prefix, &nhr->prefix)))
+ SET_FLAG(bnc->change_flags, BGP_NEXTHOP_METRIC_CHANGED);
+
+ bnc_set_resolved(bnc, nhr->type, &nhr->prefix);
/* notify bgp fsm if nbr ip goes from invalid->valid */
if (!bnc->nexthop_num)
@@ -826,8 +832,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;
+ bnc_set_resolved(bnc, ZEBRA_ROUTE_SYSTEM, NULL);
UNSET_FLAG(bnc->flags, BGP_NEXTHOP_EVPN_INCOMPLETE);
UNSET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
UNSET_FLAG(bnc->flags, BGP_NEXTHOP_LABELED_VALID);
@@ -840,6 +845,9 @@ static void bgp_process_nexthop_update(struct bgp_nexthop_cache *bnc,
bnc->nexthop = NULL;
}
+ if (bgp_aigp_nexthop_cache_changed(bnc))
+ SET_FLAG(bnc->change_flags, BGP_NEXTHOP_METRIC_CHANGED);
+
evaluate_paths(bnc);
}
@@ -1532,6 +1540,57 @@ bool bgp_aigp_metric_total(struct bgp_path_info *bpi, const struct attr *attr, u
return true;
}
+/*
+ * Returns true if the AIGP distance to the nexthop changed since the last
+ * call. The paths using this nexthop must then be advertised again.
+ */
+static bool bgp_aigp_nexthop_cache_changed(struct bgp_nexthop_cache *bnc)
+{
+ struct bgp_path_info *path = LIST_FIRST(&bnc->paths);
+ enum bgp_aigp_nexthop state = BGP_AIGP_NEXTHOP_UNKNOWN;
+ uint64_t distance = 0;
+
+ if (path && bnc->resolved_type == ZEBRA_ROUTE_BGP)
+ state = bgp_aigp_nexthop_distance(path, &distance, 0);
+ if (state != BGP_AIGP_NEXTHOP_DISTANCE)
+ distance = 0;
+
+ if (state == bnc->aigp_state && distance == bnc->aigp_distance)
+ return false;
+
+ bnc->aigp_state = state;
+ bnc->aigp_distance = distance;
+ return true;
+}
+
+/* The AIGP metric of the paths resolved by this route depends on its AIGP */
+void bgp_aigp_nexthop_route_update(struct bgp *bgp, struct bgp_dest *dest)
+{
+ struct bgp_nexthop_cache ref = {};
+ struct bgp_nexthop_cache *bnc;
+ struct bgp_path_info *path;
+ struct bgp_table *table;
+
+ prefix_copy(&ref.resolved_prefix, bgp_dest_get_prefix(dest));
+
+ for (bnc = bgp_nexthop_resolved_find_gteq(&bgp->nexthop_cache_resolved, &ref);
+ bnc && !prefix_cmp(&bnc->resolved_prefix, &ref.resolved_prefix);
+ bnc = bgp_nexthop_resolved_next(&bgp->nexthop_cache_resolved, bnc)) {
+ if (!bgp_aigp_nexthop_cache_changed(bnc))
+ continue;
+
+ LIST_FOREACH (path, &bnc->paths, nh_thread) {
+ if (CHECK_FLAG(path->flags, BGP_PATH_REMOVED) ||
+ CHECK_FLAG(path->flags, BGP_PATH_HISTORY))
+ continue;
+
+ table = bgp_dest_table(path->net);
+ SET_FLAG(path->flags, BGP_PATH_IGP_CHANGED);
+ bgp_process(table->bgp, path->net, path, table->afi, table->safi);
+ }
+ }
+}
+
/**
* evaluate_paths - Evaluate the paths/nets associated with a nexthop.
* ARGUMENTS:
diff --git a/bgpd/bgp_nht.h b/bgpd/bgp_nht.h
index c837990ce3..2e37befc52 100644
--- a/bgpd/bgp_nht.h
+++ b/bgpd/bgp_nht.h
@@ -7,6 +7,7 @@
#define _BGP_NHT_H
struct attr;
+struct bgp_dest;
/**
* bgp_nexthop_update() - process a nexthop update message from Zebra.
@@ -80,6 +81,7 @@ 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 void bgp_aigp_nexthop_route_update(struct bgp *bgp, struct bgp_dest *dest);
extern bool bgp_aigp_metric_total(struct bgp_path_info *bpi, const struct attr *attr,
uint64_t *aigp);
diff --git a/bgpd/bgp_route.c b/bgpd/bgp_route.c
index 1bcec71a9b..f18c508f3a 100644
--- a/bgpd/bgp_route.c
+++ b/bgpd/bgp_route.c
@@ -4448,6 +4448,7 @@ void bgp_process_main_one(struct bgp *bgp, struct bgp_dest *dest, afi_t afi, saf
struct bgp_path_info *new_select;
struct bgp_path_info *old_select;
struct bgp_path_info_pair old_and_new;
+ bool aigp_nexthop_update;
int debug = 0;
struct bgp_debug_vrf dv = {
.name = "Unknown",
@@ -4535,6 +4536,12 @@ void bgp_process_main_one(struct bgp *bgp, struct bgp_dest *dest, afi_t afi, saf
old_select = old_and_new.old;
new_select = old_and_new.new;
+ aigp_nexthop_update =
+ safi == SAFI_UNICAST &&
+ (old_select != new_select ||
+ (new_select && (CHECK_FLAG(new_select->flags, BGP_PATH_ATTR_CHANGED) ||
+ CHECK_FLAG(new_select->flags, BGP_PATH_IGP_CHANGED))));
+
if (safi == SAFI_UNICAST && is_srv6_unicast_enabled(bgp, afi))
bgp_srv6_unicast_register_route(bgp, afi, dest, new_select);
@@ -4611,6 +4618,8 @@ void bgp_process_main_one(struct bgp *bgp, struct bgp_dest *dest, afi_t afi, saf
UNSET_FLAG(old_select->flags, BGP_PATH_MULTIPATH_CHG);
UNSET_FLAG(old_select->flags, BGP_PATH_LINK_BW_CHG);
+ if (aigp_nexthop_update)
+ bgp_aigp_nexthop_route_update(bgp, dest);
bgp_zebra_clear_route_change_flags(dest);
UNSET_FLAG(dest->flags, BGP_NODE_ZEBRA_ANNOUNCE_EARLY);
UNSET_FLAG(dest->flags, BGP_NODE_PROCESS_SCHEDULED);
@@ -4659,6 +4668,10 @@ void bgp_process_main_one(struct bgp *bgp, struct bgp_dest *dest, afi_t afi, saf
UNSET_FLAG(dest->flags, BGP_NODE_FIB_INSTALL_PENDING);
}
+ /* The AIGP distance is read from the path flagged as selected */
+ if (aigp_nexthop_update)
+ bgp_aigp_nexthop_route_update(bgp, dest);
+
/* call bmp hook for loc-rib route update / withdraw after flags were
* set
*/
diff --git a/bgpd/bgpd.h b/bgpd/bgpd.h
index 045718a1f0..96a0b628c6 100644
--- a/bgpd/bgpd.h
+++ b/bgpd/bgpd.h
@@ -860,6 +860,8 @@ struct bgp {
/* Tree for next-hop lookup cache. */
struct bgp_nexthop_cache_head nexthop_cache_table[AFI_MAX];
+ /* Nexthops of nexthop_cache_table resolved by a BGP route */
+ struct bgp_nexthop_resolved_head nexthop_cache_resolved;
/* Tree for import-check */
struct bgp_nexthop_cache_head import_check_table[AFI_MAX];