summaryrefslogtreecommitdiff
path: root/lib/nexthop.c
diff options
context:
space:
mode:
authorStephen Worley <sworley@cumulusnetworks.com>2019-05-22 15:17:21 -0400
committerStephen Worley <sworley@cumulusnetworks.com>2019-05-23 12:21:15 -0400
commit24cfec84188bc8f69eef873302824dc6f49b5339 (patch)
tree17d48f7a41f2b150be29b2d52001b77276cb6fdc /lib/nexthop.c
parent78fba41bd810ce6e8976077c37644e9d23b9f69d (diff)
lib: Explicitly cmp gateway and source based on AF
Refactor the gatway and source nexthop comparision into a common code path that compares them explicitly based on their address family. Signed-off-by: Stephen Worley <sworley@cumulusnetworks.com>
Diffstat (limited to 'lib/nexthop.c')
-rw-r--r--lib/nexthop.c51
1 files changed, 39 insertions, 12 deletions
diff --git a/lib/nexthop.c b/lib/nexthop.c
index 123684b512..28a1170444 100644
--- a/lib/nexthop.c
+++ b/lib/nexthop.c
@@ -64,11 +64,45 @@ static int nexthop_labels_cmp(const struct nexthop *nh1,
return memcmp(nhl1->label, nhl2->label, nhl1->num_labels);
}
+static int nexthop_g_addr_cmp(enum nexthop_types_t type,
+ const union g_addr *addr1,
+ const union g_addr *addr2)
+{
+ int ret = 0;
+
+ switch (type) {
+ case NEXTHOP_TYPE_IPV4:
+ case NEXTHOP_TYPE_IPV4_IFINDEX:
+ ret = IPV4_ADDR_CMP(&addr1->ipv4, &addr2->ipv4);
+ break;
+ case NEXTHOP_TYPE_IPV6:
+ case NEXTHOP_TYPE_IPV6_IFINDEX:
+ ret = IPV6_ADDR_CMP(&addr1->ipv6, &addr2->ipv6);
+ break;
+ case NEXTHOP_TYPE_IFINDEX:
+ case NEXTHOP_TYPE_BLACKHOLE:
+ /* No addr here */
+ break;
+ }
+
+ return ret;
+}
+
+static int nexthop_gateway_cmp(const struct nexthop *nh1,
+ const struct nexthop *nh2)
+{
+ return nexthop_g_addr_cmp(nh1->type, &nh1->gate, &nh2->gate);
+}
+
+static int nexthop_source_cmp(const struct nexthop *nh1,
+ const struct nexthop *nh2)
+{
+ return nexthop_g_addr_cmp(nh1->type, &nh1->src, &nh2->src);
+}
+
int nexthop_cmp(const struct nexthop *next1, const struct nexthop *next2)
{
int ret = 0;
- uint32_t n1 = 0;
- uint32_t n2 = 0;
if (next1->vrf_id < next2->vrf_id)
return -1;
@@ -84,21 +118,14 @@ int nexthop_cmp(const struct nexthop *next1, const struct nexthop *next2)
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));
+ ret = nexthop_gateway_cmp(next1, next2);
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));
+ ret = nexthop_gateway_cmp(next1, next2);
if (ret)
return ret;
/* Intentional Fall-Through */
@@ -118,7 +145,7 @@ int nexthop_cmp(const struct nexthop *next1, const struct nexthop *next2)
break;
}
- ret = memcmp(&next1->src, &next2->src, sizeof(union g_addr));
+ ret = nexthop_source_cmp(next1, next2);
if (ret)
return ret;