Files
tailscale/wgengine/magicsock/peermtu_unix.go
Alex Valiushko ea3b9f2472 wgengine/magicsock: implement conn.MultiSocketBind (#21971)
* wgengine/magicsock: implement conn.MultiSocketBind

Bind a group of UDP sockets sharing one local port per address family. A
flow index is plumbed through the send path to pick which socket a batch
leaves from.

Group size is fixed at Options.UDPSockets for the life of the Conn. In
case rebind comes short, the group is padded out with conns that block
until closed.

No caller sets Options.UDPSockets yet, every group holds a single socket
and this is inert.

Additionally, add test coverage for the now exercised net/sockopts paths.

Updates tailscale/corp#48168

Signed-off-by: Alex Valiushko <[email protected]>
Change-Id: I253646c753fa46e2efd0ad2158d24c416a6a6964
2026-10-28 11:09:56 -07:00

61 lines
1.7 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
//go:build (darwin && !ios) || (linux && !android)
package magicsock
import (
"syscall"
)
// getIPProto returns the value of the get/setsockopt proto argument necessary
// to set an IP sockopt that corresponds with the string network, which must be
// "udp4" or "udp6".
func getIPProto(network string) int {
if network == "udp4" {
return syscall.IPPROTO_IP
}
return syscall.IPPROTO_IPV6
}
// connControl runs the system call fn on the fds of the sockets underlying the
// Conn specified by the string network, which must be "udp4" or "udp6".
// See [sockopts.ControlEach] for what it returns.
func (c *Conn) connControl(network string, fn func(fd uintptr) error) error {
pconn := &c.pconn4
if network == "udp6" {
pconn = &c.pconn6
}
return pconn.controlEach(fn)
}
// setDontFragment sets or clears the don't fragment bit on the network sockets.
// Network must be "udp4" or "udp6".
func (c *Conn) setDontFragment(network string, enable bool) error {
optArg := dontFragEnabled
if !enable {
optArg = dontFragDisabled
}
return c.connControl(network, func(fd uintptr) error {
return syscall.SetsockoptInt(int(fd), getIPProto(network), getDontFragOpt(network), optArg)
})
}
// getDontFragment reports whether the network sockets have the don't fragment bit set.
// It reports false if any socket could not be read.
func (c *Conn) getDontFragment(network string) (bool, error) {
allSet := true
err := c.connControl(network, func(fd uintptr) error {
v, err := syscall.GetsockoptInt(int(fd), getIPProto(network), getDontFragOpt(network))
if err != nil {
return err
}
if v != dontFragEnabled {
allSet = false
}
return nil
})
return err == nil && allSet, err
}