Skip to content

Commit

Permalink
lib, zebra: Modify nexthop_cmp to allow you to use weight or not
Browse files Browse the repository at this point in the history
Currently nexthop weight is a discriminator on whether or not
a nexthop matches.  There is a need to no use the weight as
part of this comparison function so let's add a boolean to
allow us to say use this or not.

Signed-off-by: Donald Sharp <[email protected]>
  • Loading branch information
donaldsharp committed Aug 22, 2024
1 parent 05c17ef commit b8e24a0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
30 changes: 22 additions & 8 deletions lib/nexthop.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ static int _nexthop_source_cmp(const struct nexthop *nh1,
}

static int _nexthop_cmp_no_labels(const struct nexthop *next1,
const struct nexthop *next2)
const struct nexthop *next2, bool use_weight)
{
int ret = 0;

Expand All @@ -155,11 +155,13 @@ static int _nexthop_cmp_no_labels(const struct nexthop *next1,
if (next1->type > next2->type)
return 1;

if (next1->weight < next2->weight)
return -1;
if (use_weight) {
if (next1->weight < next2->weight)
return -1;

if (next1->weight > next2->weight)
return 1;
if (next1->weight > next2->weight)
return 1;
}

switch (next1->type) {
case NEXTHOP_TYPE_IPV4:
Expand Down Expand Up @@ -227,11 +229,12 @@ static int _nexthop_cmp_no_labels(const struct nexthop *next1,
return ret;
}

int nexthop_cmp(const struct nexthop *next1, const struct nexthop *next2)
static int nexthop_cmp_internal(const struct nexthop *next1,
const struct nexthop *next2, bool use_weight)
{
int ret = 0;

ret = _nexthop_cmp_no_labels(next1, next2);
ret = _nexthop_cmp_no_labels(next1, next2, use_weight);
if (ret != 0)
return ret;

Expand All @@ -244,6 +247,17 @@ int nexthop_cmp(const struct nexthop *next1, const struct nexthop *next2)
return ret;
}

int nexthop_cmp(const struct nexthop *next1, const struct nexthop *next2)
{
return nexthop_cmp_internal(next1, next2, true);
}

int nexthop_cmp_no_weight(const struct nexthop *next1,
const struct nexthop *next2)
{
return nexthop_cmp_internal(next1, next2, false);
}

/*
* More-limited comparison function used to detect duplicate
* nexthops. This is used in places where we don't need the full
Expand Down Expand Up @@ -441,7 +455,7 @@ bool nexthop_same_no_labels(const struct nexthop *nh1,
if (nh1 == nh2)
return true;

if (_nexthop_cmp_no_labels(nh1, nh2) != 0)
if (_nexthop_cmp_no_labels(nh1, nh2, true) != 0)
return false;

return true;
Expand Down
2 changes: 2 additions & 0 deletions lib/nexthop.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ extern bool nexthop_same(const struct nexthop *nh1, const struct nexthop *nh2);
extern bool nexthop_same_no_labels(const struct nexthop *nh1,
const struct nexthop *nh2);
extern int nexthop_cmp(const struct nexthop *nh1, const struct nexthop *nh2);
extern int nexthop_cmp_no_weight(const struct nexthop *nh1,
const struct nexthop *nh2);
extern int nexthop_g_addr_cmp(enum nexthop_types_t type,
const union g_addr *addr1,
const union g_addr *addr2);
Expand Down

0 comments on commit b8e24a0

Please sign in to comment.