Skip to content

fix(iroh): preserve ordering of IPs for nat traversal#4090

Open
dignifiedquire wants to merge 2 commits intomainfrom
fix-order-ips
Open

fix(iroh): preserve ordering of IPs for nat traversal#4090
dignifiedquire wants to merge 2 commits intomainfrom
fix-order-ips

Conversation

@dignifiedquire
Copy link
Copy Markdown
Contributor

Depends on n0-computer/noq#571

@dignifiedquire dignifiedquire requested review from Frando and flub April 8, 2026 15:17
@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 8, 2026

Documentation for this PR has been generated and is available at: https://n0-computer.github.io/iroh/pr/4090/docs/iroh/

Last updated: 2026-04-08T15:44:51Z

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 8, 2026

Netsim report & logs for this PR have been generated and is available at: LOGS
This report will remain available for 3 days.

Last updated for commit: add34f8

@n0bot n0bot bot added this to iroh Apr 8, 2026
@github-project-automation github-project-automation bot moved this to 🚑 Needs Triage in iroh Apr 8, 2026
Copy link
Copy Markdown
Member

@matheus23 matheus23 left a comment

Choose a reason for hiding this comment

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

Let's dogfood this!

Comment on lines +1955 to +1967
impl DirectAddrType {
/// Priority for NAT traversal probing. Lower = probed first.
pub(crate) fn priority(self) -> u8 {
match self {
Self::Portmapped => 0,
Self::Qad => 1,
Self::Qad4LocalPort => 2,
Self::Local => 3,
Self::Unknown => 4,
}
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looking at https://github.com/n0-computer/iroh/blob/v0.35.0/iroh/src/magicsock.rs#L2690-L2824 it doesn't seem like we used to have an order to our DirectAddrs: Although we were careful to first put Portmapper, then Stun, then Stun4LocalPort, then Local addrs into our collection, this collection was a BTreeMap<SocketAddr, DirectAddrType>, and later in send_queued_call_me_maybes we stripped the DirectAddrType from the addrs before sending them over in the order they're in the BTreeMap.


The reason I'm saying all this is because I initially wanted to cross-reference the priorities we have here with what we used to have in iroh 0.35, but it turns out although it looks like we had some order to them, we actually threw away the order when we put it all in a BTreeMap, and it worked fine, too. So the apparent order we had in iroh 0.35 should be taken with a good grain of salt.


Anyways - given all of the above, the ordering seems fine.

for i in 0..count {
let veth = format!("veth{i}");
let peer = format!("vpeer{i}");
let ip = format!("10.{}.{}.1/24", 200 + i, i);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This doesn't match the comment for this function.
The addresses this generates are

  • 10.200.0.1
  • 10.201.1.1
  • 10.202.2.1

This is a weird pattern IMO. Shouldn't this just be

Suggested change
let ip = format!("10.{}.{}.1/24", 200 + i, i);
let ip = format!("10.200.{i}.1/24");

?

(And then the function's comment needs to be adjusted to say 10.200.x.1, too)

Alternatively, this can take a u16 and you split it up, so it's 10.200.x.y, but I've heard x.x.x.0 addrs are special, so perhaps the .1 at the end is a good idea.

Comment on lines +460 to +477
let mut cmd = Command::new("ip");
cmd.args(["link", "add", &veth, "type", "veth", "peer", "name", &peer]);
device
.spawn_command_sync(cmd)?
.wait()
.context("ip link add veth")?;
let mut cmd = Command::new("ip");
cmd.args(["addr", "add", &ip, "dev", &veth]);
device
.spawn_command_sync(cmd)?
.wait()
.context("ip addr add")?;
let mut cmd = Command::new("ip");
cmd.args(["link", "set", &veth, "up"]);
device
.spawn_command_sync(cmd)?
.wait()
.context("ip link set up")?;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This scares me.

Can we do the same thing by spawning a bunch of routers in patchbay and connecting to them via interfaces? Or even better, add a bunch of interfaces to the same router or sth like that?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

no idea, cc @Arqu @Frando for better ways to simulate this

@matheus23
Copy link
Copy Markdown
Member

Dogfooding indicates this is doing pretty well (I found other issues w.r.t. my mobile phone hotspot, but that's likely unrelated)

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

Labels

None yet

Projects

Status: 🚑 Needs Triage

Development

Successfully merging this pull request may close these issues.

2 participants