Skip to content

Commit

Permalink
lib: Fix hash map init call; cmp and hash func returns
Browse files Browse the repository at this point in the history
Signed-off-by: Nathan Duddles <[email protected]>
  • Loading branch information
nathan-duddles committed Mar 18, 2022
1 parent 399a33c commit de68f39
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions lib/vrf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -67,38 +68,55 @@ 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};

Expand Down

0 comments on commit de68f39

Please sign in to comment.