]> git.puffer.fish Git - matthieu/frr.git/commitdiff
lib: Add nexthop_cmp
authorDonald Sharp <sharpd@cumulusnetworks.com>
Fri, 1 Jun 2018 23:26:53 +0000 (19:26 -0400)
committerStephen Worley <sworley@cumulusnetworks.com>
Thu, 23 May 2019 16:21:15 +0000 (12:21 -0400)
Add function to allow us to have a sorted order
of nexthops.

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
lib/nexthop.c
lib/nexthop.h

index 8e16e705901b8a951b81bf108cba5b5e5dc5f7cb..2a65c4d54625f5327f4e29334c4675c540408421 100644 (file)
 DEFINE_MTYPE_STATIC(LIB, NEXTHOP, "Nexthop")
 DEFINE_MTYPE_STATIC(LIB, NH_LABEL, "Nexthop label")
 
+int nexthop_cmp(const struct nexthop *next1, const struct nexthop *next2)
+{
+       int ret;
+       uint32_t n1, n2;
+
+       if (next1->vrf_id < next2->vrf_id)
+               return -1;
+
+       if (next1->vrf_id > next2->vrf_id)
+               return 1;
+
+       if (next1->type < next2->type)
+               return -1;
+
+       if (next1->type > next2->type)
+               return 1;
+
+       switch(next1->type) {
+       case NEXTHOP_TYPE_IPV4:
+               n1 = ntohl(next1->gate.ipv4.s_addr);
+               n2 = ntohl(next2->gate.ipv4.s_addr);
+               if (n1 < n2)
+                       return -1;
+               if (n1 > n2)
+                       return 1;
+               break;
+       case NEXTHOP_TYPE_IPV6:
+               ret = memcmp(&next1->gate, &next2->gate, sizeof(union g_addr));
+               if (!ret)
+                       return ret;
+               break;
+       case NEXTHOP_TYPE_IPV4_IFINDEX:
+       case NEXTHOP_TYPE_IPV6_IFINDEX:
+               ret = memcmp(&next1->gate, &next2->gate, sizeof(union g_addr));
+               if (!ret)
+                       return ret;
+               /* Intentional Fall-Through */
+       case NEXTHOP_TYPE_IFINDEX:
+               if (next1->ifindex < next2->ifindex)
+                       return -1;
+
+               if (next1->ifindex > next2->ifindex)
+                       return 1;
+               break;
+       case NEXTHOP_TYPE_BLACKHOLE:
+               if (next1->bh_type < next2->bh_type)
+                       return -1;
+
+               if (next1->bh_type > next2->bh_type)
+                       return 1;
+               break;
+       }
+
+       ret = memcmp(&next1->src, &next2->src, sizeof(union g_addr));
+       return ret;
+}
+
 /* check if nexthops are same, non-recursive */
 int nexthop_same_no_recurse(const struct nexthop *next1,
                            const struct nexthop *next2)
index 663acaeb69513eb5e64ff5c3c87e817e815ae115..58e8e0ebb60fa862b281060f9dbc99a7703557dd 100644 (file)
@@ -139,6 +139,7 @@ void nexthop_del_labels(struct nexthop *);
 uint32_t nexthop_hash(const struct nexthop *nexthop);
 
 extern bool nexthop_same(const struct nexthop *nh1, const struct nexthop *nh2);
+extern int nexthop_cmp(const struct nexthop *nh1, const struct nexthop *nh2);
 
 extern const char *nexthop_type_to_str(enum nexthop_types_t nh_type);
 extern int nexthop_same_no_recurse(const struct nexthop *next1,