Skip to content

Remove the use of the iptrie crate #693

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

Merged
merged 37 commits into from
Jul 14, 2025
Merged

Remove the use of the iptrie crate #693

merged 37 commits into from
Jul 14, 2025

Conversation

mvachhar
Copy link
Contributor

@mvachhar mvachhar commented Jul 12, 2025

iptrie is poorly tested and very buggy. This series of commits replaces it with PrefixMap. It does so by adding an lpm package that contains the Prefix implementation and a TrieMap trait that the implementation can hide behind so we can swap to different implementations later.

In the interest of time, there aren't many additional tests for the new stuff, but instead we rely on PrefixMap's tests which appear far superior to iptrie (with 90+% coverage) and the routing and nat tests. It is a better use of time to add fuzzing to routing which will uncover any trie issues and cover the routing crate.

Fixes #546

Copilot

This comment was marked as outdated.

@mvachhar mvachhar force-pushed the pr/mvachhar/remove-iptrie branch 4 times, most recently from 16c3b32 to 38382ae Compare July 12, 2025 03:16
@mvachhar mvachhar marked this pull request as ready for review July 12, 2025 03:16
@mvachhar mvachhar requested a review from a team as a code owner July 12, 2025 03:16
@mvachhar mvachhar requested a review from Copilot July 12, 2025 03:19
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR removes the use of the iptrie crate and replaces it with a new lpm package providing Prefix and a TrieMap trait. Key changes include:

  • Introducing the lpm crate with prefix and trie implementations.
  • Swapping all iptrie::map::RTrieMap usages for PrefixMapTrieWithDefault and updating lookup and length calls.
  • Updating imports across routing, NAT, and management modules to use lpm::prefix instead of the old prefix or iptrie paths.

Reviewed Changes

Copilot reviewed 38 out of 39 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
routing/src/rpc_adapt.rs Swapped crate::prefix::Prefix import for lpm::prefix::Prefix
routing/src/rib/vrf.rs Replaced RTrieMap with PrefixMapTrieWithDefault and updated initialization and LPM calls
routing/src/lib.rs Removed the now-obsolete prefix module export
routing/src/fib/fibtype.rs Updated FIB trie fields and lookups to use the new lpm trie API
nat/src/stateless/... Updated NAT prefix trie to use lpm::prefix and lpm::trie types
mgmt/src/... Updated management and GRPC converter modules to import from lpm
lpm/src/... Added full implementation of prefix and trie logic in the lpm crate
Cargo.toml files Added lpm as a workspace dependency and removed iptrie
Comments suppressed due to low confidence (2)

routing/src/rib/vrf.rs:184

  • Consider adding a unit test that verifies the default routes are properly initialized for both IPv4 and IPv6, and that lookups on an empty VRF do not panic.
            unsafe { PrefixMapTrieWithDefault::with_capacity(Vrf::DEFAULT_IPV6_CAPACITY) };

mgmt/src/models/internal/natconfig/collapse.rs:64

  • The original break in this exclusion case was correct: once an exclusion prefix removes a matching allowed prefix, you should stop processing further allowed prefixes for this exclusion and move to the next exclusion. Changing it to continue alters that logic and may lead to incorrect results.
                continue;

@mvachhar mvachhar force-pushed the pr/mvachhar/remove-iptrie branch from 38382ae to 25abe59 Compare July 12, 2025 03:29
@mvachhar mvachhar added this to the GW R1 milestone Jul 12, 2025
@mvachhar mvachhar force-pushed the pr/mvachhar/remove-iptrie branch from 25abe59 to 323447b Compare July 12, 2025 13:32
Copy link
Member

@qmonnet qmonnet left a comment

Choose a reason for hiding this comment

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

Looks good, thanks a lot for this work! Nice abstraction work for the trie map.

I've got some comments (see inline), but it's only minor things, the changes look good overall.

Copy link
Contributor

@Fredi-raspall Fredi-raspall left a comment

Choose a reason for hiding this comment

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

Overall LGTM @mvachhar .
I would, for simplicity and clarity:

  • have a single trait by removing TrieMapNew and adding a new() method to Triemap, removing all the with_capacity and with_root, etc..
  • let the users of the crate decide and handle if there has to be a root or not, including the handling of the removal of objects for 0.0.0.0/0 or ::0/0. IMO, this is not something that this crate should decide and this avoids "dangerous" situations, which are better handled by the users of the crate.
  • rename TrieMapWithDefault and the module as just Trie.

@mvachhar mvachhar force-pushed the pr/mvachhar/remove-iptrie branch 6 times, most recently from 4c16b70 to 42aad6a Compare July 14, 2025 17:09
Copy link
Collaborator

@daniel-noland daniel-noland left a comment

Choose a reason for hiding this comment

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

I made comments but I think they are all outdated at this point.

Regardless, I did a careful review and I'm baiscally happy with where this is at


/// This function gets the prefix, with lognest prefix match
fn lookup<Q>(&self, addr: &Q) -> Option<(&Self::Prefix, &Self::Value)>
where
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ideally, lookup, get, and get_mut, and remove would accept a Q: Borrow<Self::Prefix> in the same way that std's HashMap et al do.

@daniel-noland daniel-noland force-pushed the pr/mvachhar/remove-iptrie branch 2 times, most recently from b332e40 to 9efcc66 Compare July 14, 2025 21:13
@daniel-noland daniel-noland force-pushed the pr/mvachhar/remove-iptrie branch from 9efcc66 to 7b6fd62 Compare July 14, 2025 21:14
mvachhar and others added 22 commits July 14, 2025 15:53
Adding a ROOT const will allow us to reduce our use of unsafe code in a future commit.

This is a logical requirement of a trie in any case.

Signed-off-by: Daniel Noland <[email protected]>
Signed-off-by: Manish Vachharajani <[email protected]>
If we use the combo of

* a const ROOT
* and scope the new method to only apply in the case of a defaulted value type

then this method becomes safe.

Signed-off-by: Daniel Noland <[email protected]>
Signed-off-by: Manish Vachharajani <[email protected]>
These methods can all be evaluated at compile time and should be scoped as such.

Signed-off-by: Daniel Noland <[email protected]>
Signed-off-by: Manish Vachharajani <[email protected]>
Signed-off-by: Daniel Noland <[email protected]>
Signed-off-by: Manish Vachharajani <[email protected]>
Signed-off-by: Daniel Noland <[email protected]>
Signed-off-by: Manish Vachharajani <[email protected]>
Signed-off-by: Daniel Noland <[email protected]>
Signed-off-by: Manish Vachharajani <[email protected]>
This is a contract we can and should hold up.

Signed-off-by: Daniel Noland <[email protected]>
Signed-off-by: Manish Vachharajani <[email protected]>
The comment no longer obtains as the described trait is implemented.

Signed-off-by: Daniel Noland <[email protected]>
Signed-off-by: Manish Vachharajani <[email protected]>
Signed-off-by: Daniel Noland <[email protected]>
Signed-off-by: Manish Vachharajani <[email protected]>
Signed-off-by: Daniel Noland <[email protected]>
Signed-off-by: Manish Vachharajani <[email protected]>
Signed-off-by: Daniel Noland <[email protected]>
Signed-off-by: Manish Vachharajani <[email protected]>
Signed-off-by: Daniel Noland <[email protected]>
Signed-off-by: Manish Vachharajani <[email protected]>
Signed-off-by: Daniel Noland <[email protected]>
Signed-off-by: Manish Vachharajani <[email protected]>
Signed-off-by: Daniel Noland <[email protected]>
Signed-off-by: Manish Vachharajani <[email protected]>
When explicitly requested, the conversion of Ip{4,6}Net to Ip{4,6}Prefix should be done by zeroing any set host bits.

This logic does not apply to constructing Ip{4,6}Prefixes explicitly using new et al.

Signed-off-by: Daniel Noland <[email protected]>
Signed-off-by: Manish Vachharajani <[email protected]>
The signature of TrieMapNew didn't allow it to function as a factory before.

Signed-off-by: Daniel Noland <[email protected]>
Signed-off-by: Manish Vachharajani <[email protected]>
This name makes it clearer that the trait is intended to be implemented on factory structures.

Also, as a general rule, new is reserved as a constructor for the type itself, so rename to `create`.

Signed-off-by: Daniel Noland <[email protected]>
Signed-off-by: Manish Vachharajani <[email protected]>
@mvachhar mvachhar force-pushed the pr/mvachhar/remove-iptrie branch from 1bae6c0 to a08ef5f Compare July 14, 2025 21:53
@mvachhar mvachhar enabled auto-merge July 14, 2025 21:55
@mvachhar mvachhar dismissed stale reviews from Fredi-raspall and qmonnet July 14, 2025 21:55

Addressed the comments, but dismissing due to timezone so we can merge today.

@mvachhar mvachhar added this pull request to the merge queue Jul 14, 2025
Merged via the queue into main with commit ca42079 Jul 14, 2025
16 checks passed
@mvachhar mvachhar deleted the pr/mvachhar/remove-iptrie branch July 14, 2025 22:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Investigate / fix iptrie assertion/panic
4 participants