Skip to content

[2/3] [nat] introduce NatAddress trait - #335

Open
nicolaskagami wants to merge 1 commit into
nsk/nat-1-portrangefrom
nsk/nat-2-nat-family
Open

[2/3] [nat] introduce NatAddress trait#335
nicolaskagami wants to merge 1 commit into
nsk/nat-1-portrangefrom
nsk/nat-2-nat-family

Conversation

@nicolaskagami

@nicolaskagami nicolaskagami commented Aug 6, 2026

Copy link
Copy Markdown

This PR:

  • Introduces a NatAddress trait, tying each IP address family to its p4 table, match key, and action types. Replaces duplicated per-family entry points.

This is the second of 3 PRs simplifying and de-duplicating some of the nat.rs code.

Obs: Changes are almost entirely equivalent, except for the ordering of some things and the log message nat tables -> nat table.

Tie each IP address family to its p4 table, match key, and action types
via a trait, with the table operations provided as default methods.
Replaces the duplicated per-family entry points.
@nicolaskagami nicolaskagami changed the title [2/3] [nat] introduce NatFamily trait [2/3] [nat] introduce NatAddress trait Aug 6, 2026
@nicolaskagami
nicolaskagami marked this pull request as ready for review August 6, 2026 18:00
Comment thread dpd/src/nat.rs
};

match nat::add_ipv6_entry(switch, nat_ip, low, high, tgt) {
match nat_ip.add_entry(switch, low, high, tgt) {

@internet-diglett internet-diglett Aug 6, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🤔

  • We're validating the port range with a newtype but not using it here
  • We're also passing in the NatTarget
  • Both the PortRange and the NatTarget were used to construct the Ipv6NatEntry type earlier

Does it make sense to pass the Ipv6NatEntry here as well? It wouldn't surprise me if there were additional validations we could perform with that type as well.

Same sentiment with other similar calls.

@nicolaskagami nicolaskagami Aug 7, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

  • Some of what we're doing here is in service of [3/3] [nat] unify per-family NAT mapping #336. This is one place where it could hold this type but it wouldn't be of much use since we would immediately de-structure it, effectively adding another type to the trait just so we could de-structure it one level below.
  • This PR was as semantic-preserving as possible and avoided bigger refactors. I actually think PortRange could be separated off and we could have a more bespoke struct / search algorithms for it (as mentioned on the description of [1/3] [nat] move PortRange to struct #334).

Comment thread dpd/src/table/nat.rs
}
}

impl NatAddress for Ipv4Addr {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Instead of implementing NatAddress for a raw V4 or V6 addr, would it make sense to create newtypes around them that provides additional validation (i.e. do we want to allow creation of a NAT entry for any Ipv4 / Ipv6 address?), and implement this trait for the newtype?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

We could, but I didn't want to add new functionality on these PRs.

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.

2 participants