Skip to content

Bind to correct interface by specifying zone index#1

Merged
antoninbas merged 1 commit intoantrea-io:mainfrom
xliuxu:xliu2/allow-addr-with-zone
Nov 7, 2024
Merged

Bind to correct interface by specifying zone index#1
antoninbas merged 1 commit intoantrea-io:mainfrom
xliuxu:xliu2/allow-addr-with-zone

Conversation

@xliuxu
Copy link
Copy Markdown

@xliuxu xliuxu commented Nov 5, 2024

Mirror of upstream change: mdlayher#32

Copy link
Copy Markdown

@antoninbas antoninbas left a comment

Choose a reason for hiding this comment

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

IMO, a simple alternative would be to add a ListenAddress function that skips address resolution (see comment)

Comment thread addr.go Outdated
Comment on lines 87 to 80
// If a preferred address is set, use it directly.
if preferred.IsValid() {
if preferred.Zone() == "" {
return preferred.WithZone(zone)
}
return preferred
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

this makes the function quite confusing. Why even bother to call match if we are going to do this? Wouldn't the "default" case (return ip.WithZone(zone)) just work in this case?

Comment thread addr.go Outdated

// chooseAddr selects an Addr from the interface based on the specified Addr type.
func chooseAddr(addrs []net.Addr, zone string, addr Addr) (netip.Addr, error) {
func chooseAddr(addrs []net.Addr, zone string, zoneIndex int, addr Addr) (netip.Addr, error) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I feel like the code is a bit more complicated than it needs to be. Could we do the following:

  • assume that addr does not include a zone information (or ignore it?) as when Listen is called, we are already working with a specific net.Interface
  • remove zone and only keep zoneIndex
  • when returning the final address (return value of type netip.Addr), always set the zone to zoneIndex

The current code feels like it's a bit too complex and was tweaked for a specific use case

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.

The motivation for this change is to adjust the library to support our specific use case while preserving the original behavior.

I am not very sure whether setting the zone to 'index' regardless of other factors could have side effects or not.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There is a point to be made that the rest of the library is consistently using the interface name as the zone, so you're right that may not be the best approach.
But the current solution is a bit too hacky IMO.

Comment thread conn.go Outdated
}

ip, err := chooseAddr(addrs, ifi.Name, addr)
ip, err := chooseAddr(addrs, ifi.Name, ifi.Index, addr)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

honestly, an alternative to this patch would be to just have a new function ListenAddr(address string) that directly calls icmp.ListenPacket("ip6:ipv6-icmp", address)

it seems that in our case (by which I mean your PR antrea-io/antrea#6700) we already know the address and zone, and we don't want to rely on this library to do the address resolution

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.

This is definitely better. I will update the PR.

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.

revisited this. The ndp.Conn still depends on net.Interface. Should we retrieve the interface from the address in ListenAddr(address string)?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

After thinking about it more, the big issue IMO comes from the fact that we are trying to use the interface index as zone, when the library uses the interface name consistently. This is unfortunate since we require interface index in our case and interface index may be more portable.

I see 2 options:

  1. update the library to use zone index everywhere, which is not such a big change
  2. only use the zone index when calling icmp.ListenPacket

For option 2, it is a one-line change:

ic, err := icmp.ListenPacket("ip6:ipv6-icmp", ip.WithZone(ifi.Index).String())

Unless I am misunderstanding the issue, this should take care of it ^

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.

changed to option 2.

Signed-off-by: Xu Liu <xu.liu@broadcom.com>
@xliuxu xliuxu force-pushed the xliu2/allow-addr-with-zone branch from 1013127 to 4b1e72c Compare November 7, 2024 02:27
@xliuxu xliuxu changed the title Allow binding on address with arbitrary zone Bind to correct interface by specifying zone index Nov 7, 2024
@antoninbas
Copy link
Copy Markdown

Closing and reopening to trigger workflows

@antoninbas antoninbas closed this Nov 7, 2024
@antoninbas antoninbas reopened this Nov 7, 2024
@antoninbas antoninbas merged commit 6f35f2e into antrea-io:main Nov 7, 2024
@xliuxu
Copy link
Copy Markdown
Author

xliuxu commented Nov 7, 2024

Thanks for the review! I will update antrea-io/antrea#6700

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