Skip to content

Conversation

@corhere
Copy link
Contributor

@corhere corhere commented Nov 1, 2023

I am finally convinced that, given two netip.Prefix values a and b, the expression

a.Contains(b.Addr()) || b.Contains(a.Addr())

is functionally equivalent to

a.Overlaps(b)

The (netip.Prefix).Contains method works by masking the address with the prefix's mask and testing whether the remaining most-significant bits are equal to the same bits in the prefix. The (netip.Prefix).Overlaps method works by masking the longer prefix to the length of the shorter prefix and testing whether the remaining most-significant bits are equal. This is equivalent to shorterPrefix.Contains(longerPrefix.Addr()), therefore applying Contains symmetrically to two prefixes will always yield the same result as applying Overlaps to the two prefixes in either order.

- What I did

- How I did it

- How to verify it

- Description for the changelog

- A picture of a cute animal (not mandatory but encouraged)

I am finally convinced that, given two netip.Prefix values a and b, the
expression

    a.Contains(b.Addr()) || b.Contains(a.Addr())

is functionally equivalent to

    a.Overlaps(b)

The (netip.Prefix).Contains method works by masking the address with the
prefix's mask and testing whether the remaining most-significant bits
are equal to the same bits in the prefix. The (netip.Prefix).Overlaps
method works by masking the longer prefix to the length of the shorter
prefix and testing whether the remaining most-significant bits are
equal. This is equivalent to
shorterPrefix.Contains(longerPrefix.Addr()), therefore applying Contains
symmetrically to two prefixes will always yield the same result as
applying Overlaps to the two prefixes in either order.

Signed-off-by: Cory Snider <csnider@mirantis.com>
@corhere corhere added area/networking Networking kind/refactor PR's that refactor, or clean-up code area/networking/ipam Networking labels Nov 1, 2023
@corhere corhere added this to the 25.0.0 milestone Nov 1, 2023
Copy link
Member

@akerouanton akerouanton left a comment

Choose a reason for hiding this comment

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/networking/ipam Networking area/networking Networking kind/refactor PR's that refactor, or clean-up code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants