Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove in6addr cmp #17312

Merged
merged 3 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 4 additions & 22 deletions lib/sockunion.c
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,7 @@ int sockunion_same(const union sockunion *su1, const union sockunion *su2)
sizeof(struct in_addr));
break;
case AF_INET6:
ret = memcmp(&su1->sin6.sin6_addr, &su2->sin6.sin6_addr,
sizeof(struct in6_addr));
ret = IPV6_ADDR_CMP(&su1->sin6.sin6_addr, &su2->sin6.sin6_addr);
if ((ret == 0) && IN6_IS_ADDR_LINKLOCAL(&su1->sin6.sin6_addr)) {
/* compare interface indices */
if (su1->sin6.sin6_scope_id && su2->sin6.sin6_scope_id)
Expand Down Expand Up @@ -588,23 +587,6 @@ static void __attribute__((unused)) sockunion_print(const union sockunion *su)
}
}

int in6addr_cmp(const struct in6_addr *addr1, const struct in6_addr *addr2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couldn't we just change the implementation of this so that it's not so ... awkward?
it seems nice to have the v6 address-specific signature, for instance.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

except of course we have IPV6_ADDR_CMP and a couple other places where we just use memcmp for v6 addresses in sockunion.c. I've just converted them over to use that version. All in all looks like a bunch of places could be fixed up.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure- it's not the use of memcmp() at the bottom level that I was asking about, it's about having a v6-specific signature that seems ... useful and helpful. Just a fan of apis that help folks use our data types in a natural sort of way.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see why IPV6_ADDR_CMP is not sufficient. the custom function was incredibly slow, when I removed it in a 256 peer X 100k routes I dropped from ~3:30 convergence to 2:30 convergence.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that the byte-by-byte implementation was not good - fine, let's do it better.
the macro is just a macro, so no type-checking of its arguments - I like the idea of a strongly-typed utility (that has a good implementation). but it's ok - if you and Donatas are ok with the macro, I'm not lying on the tracks

{
unsigned int i;
const uint8_t *p1, *p2;

p1 = (const uint8_t *)addr1;
p2 = (const uint8_t *)addr2;

for (i = 0; i < sizeof(struct in6_addr); i++) {
if (p1[i] > p2[i])
return 1;
else if (p1[i] < p2[i])
return -1;
}
return 0;
}

int sockunion_cmp(const union sockunion *su1, const union sockunion *su2)
{
if (su1->sa.sa_family > su2->sa.sa_family)
Expand All @@ -621,7 +603,8 @@ int sockunion_cmp(const union sockunion *su1, const union sockunion *su2)
return -1;
}
if (su1->sa.sa_family == AF_INET6)
return in6addr_cmp(&su1->sin6.sin6_addr, &su2->sin6.sin6_addr);
return IPV6_ADDR_CMP(&su1->sin6.sin6_addr, &su2->sin6.sin6_addr);

return 0;
}

Expand Down Expand Up @@ -727,8 +710,7 @@ int sockunion_is_null(const union sockunion *su)
case AF_INET:
return (su->sin.sin_addr.s_addr == 0);
case AF_INET6:
return !memcmp(su->sin6.sin6_addr.s6_addr, null_s6_addr,
sizeof(null_s6_addr));
return !IPV6_ADDR_CMP(su->sin6.sin6_addr.s6_addr, null_s6_addr);
default:
return 0;
}
Expand Down
1 change: 0 additions & 1 deletion lib/sockunion.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ enum connect_result { connect_error, connect_success, connect_in_progress };
/* Prototypes. */
extern int str2sockunion(const char *, union sockunion *);
extern const char *sockunion2str(const union sockunion *, char *, size_t);
int in6addr_cmp(const struct in6_addr *addr1, const struct in6_addr *addr2);
extern int sockunion_cmp(const union sockunion *, const union sockunion *);
extern int sockunion_same(const union sockunion *, const union sockunion *);
extern unsigned int sockunion_hash(const union sockunion *);
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/test_frrlua.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ static void test_encode_decode(void)

lua_pushin6addr(L, &in6addr_a);
lua_decode_in6addr(L, -1, &in6addr_a);
assert(in6addr_cmp(&in6addr_a, &in6addr_b) == 0);
assert(memcmp(&in6addr_a, &in6addr_b, sizeof(struct in6_addr)) == 0);
assert(lua_gettop(L) == 0);

union sockunion su_a, su_b;
Expand Down
Loading