From 2107c091f2cbe3168a00b964c52e06205300ec1c Mon Sep 17 00:00:00 2001 From: Nathan Duddles Date: Fri, 18 Mar 2022 08:10:29 -0700 Subject: [PATCH] lib: Fix hash map init call; cmp and hash func returns Signed-off-by: Nathan Duddles --- lib/vrf.c | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/lib/vrf.c b/lib/vrf.c index 1a7de9ab7b85..090418f118e6 100644 --- a/lib/vrf.c +++ b/lib/vrf.c @@ -36,6 +36,7 @@ #include "lib_errors.h" #include "northbound.h" #include "northbound_cli.h" +#include "jhash.h" /* default VRF name value used when VRF backend is not NETNS */ #define VRF_DEFAULT_NAME_INTERNAL "default" @@ -67,38 +68,52 @@ struct vrf_ids_by_table_proxy { vrf_id_t vrf_id; }; -static struct vrf_ids_by_table_head vrf_ids_by_table_head; static int vrf_ids_by_table_cmp(const struct vrf_ids_by_table_proxy *item_a, const struct vrf_ids_by_table_proxy *item_b) { - return (item_a->table_id == item_b->table_id); + if (item_a->table_id > item_b->table_id) return 1; + if (item_a->table_id < item_b->table_id) return -1; + return 0; } static uint32_t vrf_ids_by_table_hash(const struct vrf_ids_by_table_proxy *item) { - return item->table_id; + return jhash_1word(item->table_id, 0xa010bf92); } DECLARE_HASH(vrf_ids_by_table, struct vrf_ids_by_table_proxy, vrf_ids_by_table_hash_item, vrf_ids_by_table_cmp, vrf_ids_by_table_hash); +static struct vrf_ids_by_table_head vrf_ids_by_table_head; + +static int vrf_ids_by_table_hash_is_initialized = 0; + + /* Helper functions for vrf_ids_by_table hashmap */ static void vrf_ids_by_table_delete(uint32_t table_id, vrf_id_t vrf_id) { - struct vrf_ids_by_table_proxy ref = {.table_id = table_id}; - struct vrf_ids_by_table_proxy *item = - vrf_ids_by_table_find(&vrf_ids_by_table_head, &ref); + if (vrf_ids_by_table_hash_is_initialized) { + struct vrf_ids_by_table_proxy ref = {.table_id = table_id}; + struct vrf_ids_by_table_proxy *item = + vrf_ids_by_table_find(&vrf_ids_by_table_head, &ref); - /* If entry contains both table_id and vrf_id, remove from hashmap */ - if (item != NULL && item->vrf_id == vrf_id) { - vrf_ids_by_table_del(&vrf_ids_by_table_head, &ref); - } + /* If entry contains both table_id and vrf_id, remove from hashmap */ + if (item != NULL && item->vrf_id == vrf_id) { + vrf_ids_by_table_del(&vrf_ids_by_table_head, &ref); + } + } } static void vrf_ids_by_table_update(uint32_t table_id, vrf_id_t new_vrf_id) { + + if (!vrf_ids_by_table_hash_is_initialized) { + vrf_ids_by_table_init(&vrf_ids_by_table_head); + vrf_ids_by_table_hash_is_initialized = 1; + } + struct vrf_ids_by_table_proxy ref = {.table_id = table_id, .vrf_id = new_vrf_id};