Skip to content
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

Bind to correct interface by specifying zone index #1

Merged
merged 1 commit into from
Nov 7, 2024

Conversation

xliuxu
Copy link

@xliuxu xliuxu commented Nov 5, 2024

Mirror of upstream change: mdlayher#32

Copy link

@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)

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
}

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?

addr.go Outdated
@@ -18,14 +19,16 @@ const (
)

// 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) {

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
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.

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.

conn.go Outdated
@@ -41,7 +41,7 @@ func Listen(ifi *net.Interface, addr Addr) (*Conn, netip.Addr, error) {
return nil, netip.Addr{}, err
}

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

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
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
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)?

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
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 changed the title Allow binding on address with arbitrary zone Bind to correct interface by specifying zone index Nov 7, 2024
@antoninbas
Copy link

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
4 checks passed
@xliuxu
Copy link
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