From 8f8d9845c6bc283e5a9f7339f9d3f2884774589b Mon Sep 17 00:00:00 2001 From: Stephen Worley Date: Thu, 11 Apr 2019 13:36:41 -0400 Subject: [PATCH] lib: Add equals function for nexthop groups Add a function to check whether nexthop groups are equivalent. It does not care about ordering. Also, set any functions that it relies on to take const vars as well. Signed-off-by: Stephen Worley --- lib/nexthop_group.c | 25 ++++++++++++++++++++++++- lib/nexthop_group.h | 6 ++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/lib/nexthop_group.c b/lib/nexthop_group.c index 527039ac63..463ab0b881 100644 --- a/lib/nexthop_group.c +++ b/lib/nexthop_group.c @@ -94,7 +94,8 @@ uint8_t nexthop_group_active_nexthop_num(const struct nexthop_group *nhg) return num; } -struct nexthop *nexthop_exists(struct nexthop_group *nhg, struct nexthop *nh) +struct nexthop *nexthop_exists(const struct nexthop_group *nhg, + const struct nexthop *nh) { struct nexthop *nexthop; @@ -106,6 +107,28 @@ struct nexthop *nexthop_exists(struct nexthop_group *nhg, struct nexthop *nh) return NULL; } +bool nexthop_group_equal(const struct nexthop_group *nhg1, + const struct nexthop_group *nhg2) +{ + struct nexthop *nh = NULL; + + if (nhg1 && !nhg2) + return false; + + if (!nhg1 && !nhg2) + return false; + + if (nexthop_group_nexthop_num(nhg1) != nexthop_group_nexthop_num(nhg2)) + return false; + + for (ALL_NEXTHOPS_PTR(nhg1, nh)) { + if (!nexthop_exists(nhg2, nh)) + return false; + } + + return true; +} + struct nexthop_group *nexthop_group_new(void) { return XCALLOC(MTYPE_NEXTHOP_GROUP, sizeof(struct nexthop_group)); diff --git a/lib/nexthop_group.h b/lib/nexthop_group.h index 5193c943c4..be6f50d8a0 100644 --- a/lib/nexthop_group.h +++ b/lib/nexthop_group.h @@ -111,8 +111,10 @@ void nexthop_group_disable_vrf(struct vrf *vrf); void nexthop_group_interface_state_change(struct interface *ifp, ifindex_t oldifindex); -extern struct nexthop *nexthop_exists(struct nexthop_group *nhg, - struct nexthop *nh); +extern struct nexthop *nexthop_exists(const struct nexthop_group *nhg, + const struct nexthop *nh); +extern bool nexthop_group_equal(const struct nexthop_group *nhg1, + const struct nexthop_group *nhg2); extern struct nexthop_group_cmd *nhgc_find(const char *name); -- 2.39.5