bgpd: add the bgp_label_per_nexthop_cache struct and apis

This commit introduces the necessary structs and apis to
create the cache entries that store the label information
associated to a given nexthop.

A hash table is created in each BGP instance for all the
AFIs: IPv4 and IPv6. That hash table is initialised.
An API to look and/or create an entry based on a given
nexthop.

Signed-off-by: Philippe Guibert <philippe.guibert@6wind.com>
This commit is contained in:
Philippe Guibert 2023-02-28 14:17:17 +01:00
parent 855dcbf158
commit 546d58702e
4 changed files with 107 additions and 0 deletions

View File

@ -23,6 +23,7 @@
#include "bgpd/bgp_debug.h"
#include "bgpd/bgp_errors.h"
#include "bgpd/bgp_route.h"
#include "bgpd/bgp_zebra.h"
#define BGP_LABELPOOL_ENABLE_TESTS 0
@ -1558,3 +1559,57 @@ void bgp_lp_vty_init(void)
install_element(ENABLE_NODE, &clear_labelpool_perf_test_cmd);
#endif /* BGP_LABELPOOL_ENABLE_TESTS */
}
DEFINE_MTYPE_STATIC(BGPD, LABEL_PER_NEXTHOP_CACHE,
"BGP Label Per Nexthop entry");
/* The nexthops values are compared to
* find in the tree the appropriate cache entry
*/
int bgp_label_per_nexthop_cache_cmp(const struct bgp_label_per_nexthop_cache *a,
const struct bgp_label_per_nexthop_cache *b)
{
return prefix_cmp(&a->nexthop, &b->nexthop);
}
struct bgp_label_per_nexthop_cache *
bgp_label_per_nexthop_new(struct bgp_label_per_nexthop_cache_head *tree,
struct prefix *nexthop)
{
struct bgp_label_per_nexthop_cache *blnc;
blnc = XCALLOC(MTYPE_LABEL_PER_NEXTHOP_CACHE,
sizeof(struct bgp_label_per_nexthop_cache));
blnc->tree = tree;
blnc->label = MPLS_INVALID_LABEL;
prefix_copy(&blnc->nexthop, nexthop);
LIST_INIT(&(blnc->paths));
bgp_label_per_nexthop_cache_add(tree, blnc);
return blnc;
}
struct bgp_label_per_nexthop_cache *
bgp_label_per_nexthop_find(struct bgp_label_per_nexthop_cache_head *tree,
struct prefix *nexthop)
{
struct bgp_label_per_nexthop_cache blnc = {};
if (!tree)
return NULL;
memcpy(&blnc.nexthop, nexthop, sizeof(struct prefix));
return bgp_label_per_nexthop_cache_find(tree, &blnc);
}
void bgp_label_per_nexthop_free(struct bgp_label_per_nexthop_cache *blnc)
{
if (blnc->label != MPLS_INVALID_LABEL) {
bgp_zebra_send_nexthop_label(ZEBRA_MPLS_LABELS_DELETE,
blnc->label, ZEBRA_LSP_BGP,
&blnc->nexthop);
bgp_lp_release(LP_TYPE_NEXTHOP, blnc, blnc->label);
}
bgp_label_per_nexthop_cache_del(blnc->tree, blnc);
XFREE(MTYPE_LABEL_PER_NEXTHOP_CACHE, blnc);
}

View File

@ -42,4 +42,47 @@ extern void bgp_lp_event_zebra_down(void);
extern void bgp_lp_event_zebra_up(void);
extern void bgp_lp_vty_init(void);
struct bgp_label_per_nexthop_cache;
PREDECL_RBTREE_UNIQ(bgp_label_per_nexthop_cache);
extern int
bgp_label_per_nexthop_cache_cmp(const struct bgp_label_per_nexthop_cache *a,
const struct bgp_label_per_nexthop_cache *b);
struct bgp_label_per_nexthop_cache {
/* RB-tree entry. */
struct bgp_label_per_nexthop_cache_item entry;
/* the nexthop is the key of the list */
struct prefix nexthop;
/* calculated label */
mpls_label_t label;
/* number of path_vrfs */
unsigned int path_count;
/* back pointer to bgp instance */
struct bgp *to_bgp;
/* list of path_vrfs using it */
LIST_HEAD(path_lists, bgp_path_info) paths;
/* Back pointer to the cache tree this entry belongs to. */
struct bgp_label_per_nexthop_cache_head *tree;
};
DECLARE_RBTREE_UNIQ(bgp_label_per_nexthop_cache,
struct bgp_label_per_nexthop_cache, entry,
bgp_label_per_nexthop_cache_cmp);
void bgp_label_per_nexthop_free(struct bgp_label_per_nexthop_cache *blnc);
struct bgp_label_per_nexthop_cache *
bgp_label_per_nexthop_new(struct bgp_label_per_nexthop_cache_head *tree,
struct prefix *nexthop);
struct bgp_label_per_nexthop_cache *
bgp_label_per_nexthop_find(struct bgp_label_per_nexthop_cache_head *tree,
struct prefix *nexthop);
#endif /* _FRR_BGP_LABELPOOL_H */

View File

@ -3355,6 +3355,11 @@ static struct bgp *bgp_create(as_t *as, const char *name,
SET_FLAG(bgp->af_flags[afi][SAFI_MPLS_VPN],
BGP_VPNVX_RETAIN_ROUTE_TARGET_ALL);
}
for (afi = AFI_IP; afi < AFI_MAX; afi++)
bgp_label_per_nexthop_cache_init(
&bgp->mpls_labels_per_nexthop[afi]);
if (name)
bgp->name = XSTRDUP(MTYPE_BGP, name);

View File

@ -574,6 +574,10 @@ struct bgp {
/* Allocate MPLS labels */
uint8_t allocate_mpls_labels[AFI_MAX][SAFI_MAX];
/* Tree for next-hop lookup cache. */
struct bgp_label_per_nexthop_cache_head
mpls_labels_per_nexthop[AFI_MAX];
/* Allocate hash entries to store policy routing information
* The hash are used to host pbr rules somewhere.
* Actually, pbr will only be used by flowspec