mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-24 02:31:18 -08:00
* 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
78 lines
1.9 KiB
Go
78 lines
1.9 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
//go:build linux && !android
|
|
|
|
package magicsock
|
|
|
|
import (
|
|
"net"
|
|
"slices"
|
|
"testing"
|
|
|
|
"tailscale.com/tstest"
|
|
"tailscale.com/types/nettype"
|
|
)
|
|
|
|
func TestUDPSockets(t *testing.T) {
|
|
if !hasUDPTransport {
|
|
t.Skip("no UDP transport")
|
|
}
|
|
const sockets = 4
|
|
wantSharedPort := func(t *testing.T, ruc *RebindingUDPConn) {
|
|
t.Helper()
|
|
|
|
if got := ruc.group.bound; got != sockets {
|
|
t.Fatalf("bound %d sockets, want %d", got, sockets)
|
|
}
|
|
for i, pconn := range ruc.group.conns[:ruc.group.bound] {
|
|
if got := uint16(pconn.LocalAddr().(*net.UDPAddr).Port); got != ruc.Port() {
|
|
t.Errorf("socket %d bound to port %d, want %d", i, got, ruc.Port())
|
|
}
|
|
}
|
|
}
|
|
c, _ := newTestConnAndRegistry(t, func(o *Options) { o.UDPSockets = sockets })
|
|
|
|
wantSharedPort(t, &c.pconn4)
|
|
c.Rebind()
|
|
wantSharedPort(t, &c.pconn4)
|
|
}
|
|
|
|
func TestUDPSocketsOne(t *testing.T) {
|
|
if !hasUDPTransport {
|
|
t.Skip("no UDP transport")
|
|
}
|
|
for _, tt := range []struct {
|
|
name string
|
|
sockets int
|
|
listener nettype.PacketListener
|
|
noReuse bool // clear canShareUDPPort
|
|
}{
|
|
{name: "unset", sockets: 0},
|
|
{name: "negative", sockets: -3},
|
|
{name: "test-only-listener", sockets: 4, listener: localhostListener{}},
|
|
{name: "no-reuseport", sockets: 4, noReuse: true},
|
|
} {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if tt.noReuse {
|
|
tstest.Replace(t, &canShareUDPPort, false)
|
|
}
|
|
c, _ := newTestConnAndRegistry(t, func(o *Options) {
|
|
o.UDPSockets = tt.sockets
|
|
o.TestOnlyPacketListener = tt.listener
|
|
})
|
|
|
|
if got := c.pconn4.group.bound; got != 1 {
|
|
t.Errorf("bound %d sockets, want 1", got)
|
|
}
|
|
if _, _, err := c.bind.Open(0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := []string{"ReceiveIPv4", "ReceiveIPv6", "ReceiveDERP"}
|
|
if got := c.bind.ReceiveNames(); !slices.Equal(got, want) {
|
|
t.Errorf("ReceiveNames = %q, want %q", got, want)
|
|
}
|
|
})
|
|
}
|
|
}
|