From ac5d1091dcb62484bf432e80023fa55239fc0d84 Mon Sep 17 00:00:00 2001 From: Stephen Worley Date: Wed, 13 May 2020 17:42:55 -0400 Subject: [PATCH] zebra: make NHG ID allocation smarter Make NHG ID allocation smarter so it wraps once it hits the lower bound for protos and performs a lookup to make sure we don't already have that ID in use. Its pretty unlikely we would wrap since the ID space is somewhere around 24million for Zebra at this point in time. Signed-off-by: Stephen Worley --- zebra/zebra_nhg.c | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/zebra/zebra_nhg.c b/zebra/zebra_nhg.c index c078b25ab1..6d4a915673 100644 --- a/zebra/zebra_nhg.c +++ b/zebra/zebra_nhg.c @@ -69,6 +69,35 @@ static void depends_decrement_free(struct nhg_connected_tree_head *head); static struct nhg_backup_info * nhg_backup_copy(const struct nhg_backup_info *orig); +/* Helper function for getting the next allocatable ID */ +static uint32_t nhg_get_next_id() +{ + while (1) { + id_counter++; + + if (IS_ZEBRA_DEBUG_NHG_DETAIL) + zlog_debug("%s: ID %u checking", __func__, id_counter); + + if (id_counter == ZEBRA_NHG_PROTO_LOWER) { + if (IS_ZEBRA_DEBUG_NHG_DETAIL) + zlog_debug("%s: ID counter wrapped", __func__); + + id_counter = 0; + continue; + } + + if (zebra_nhg_lookup_id(id_counter)) { + if (IS_ZEBRA_DEBUG_NHG_DETAIL) + zlog_debug("%s: ID already exists", __func__); + + continue; + } + + break; + } + + return id_counter; +} static void nhg_connected_free(struct nhg_connected *dep) { @@ -674,7 +703,7 @@ static bool zebra_nhe_find(struct nhg_hash_entry **nhe, /* return value */ * assign the next global id value if necessary. */ if (lookup->id == 0) - lookup->id = ++id_counter; + lookup->id = nhg_get_next_id(); if (lookup->id < ZEBRA_NHG_PROTO_LOWER) { /* -- 2.39.5