mirror of
https://github.com/slackhq/nebula.git
synced 2026-11-16 00:06:52 +01:00
internal/msgx is the one copy of xnu's private batched socket calls: the msghdr_x layout, Send/Recv through the raw syscall (EINTR retried) and Unsupported for the errnos a kernel or sandbox refuses them with. Every socket the listener owns reads with recvmsg_x: the listener and each connected socket run their own reader goroutine, and hand whole batches to ListenOut over a channel. ListenOut runs the EncReader over a batch and flushes once, so the reader stays single-threaded without a lock that readers contend on, and the next recvmsg_x overlaps the decrypt of the last batch. Batch buffers (64 x MTU) come from a per-listener free list that keeps up to 11; a reader takes one only while its socket is readable, so idle connected sockets hold none. A batch with a header intact msghdr_x entries can't produce, or ENOSYS/EPERM/EOPNOTSUPP, switches every reader to one datagram per read for good. Connected sockets send runs with sendmsg_x, which xnu batches through the stack on a connected socket. EAGAIN/ENOBUFS keep #1901's single-datagram stall handling; a refused sendmsg_x retries the datagram with write and turns sendmsg_x off once that goes through. The connected-socket knobs lose their NEBULA_CONNSOCK* environment overrides and keep #1901's defaults. Based on Caleb Jasik's #1895 (recvmsg_x) and #1897 (sendmsg_x), and Jack Doan's internal/msgx shape from #1898. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
107 lines
3.2 KiB
Go
107 lines
3.2 KiB
Go
//go:build !e2e_testing
|
|
|
|
package udp
|
|
|
|
import (
|
|
"log/slog"
|
|
"net"
|
|
"net/netip"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// benchConnSocks opens a wildcard listener with ListenOut running and cfg applied, and a loopback peer that never
|
|
// reads, so its sends cost only the sender.
|
|
func benchConnSocks(b *testing.B, s Settings, cfg func(*connSockConfig)) (*StdConn, netip.AddrPort) {
|
|
b.Helper()
|
|
c, err := NewListener(slog.New(slog.DiscardHandler), s)
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
b.Cleanup(func() { _ = c.Close() })
|
|
u := c.(*StdConn)
|
|
if cfg != nil {
|
|
cfg(&u.socks.cfg)
|
|
}
|
|
go func() { _ = u.ListenOut(func(netip.AddrPort, []byte) {}, func() {}) }()
|
|
for !u.listening.Load() {
|
|
time.Sleep(time.Millisecond)
|
|
}
|
|
pc, err := net.ListenPacket("udp4", "127.0.0.1:0")
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
b.Cleanup(func() { _ = pc.Close() })
|
|
return u, pc.LocalAddr().(*net.UDPAddr).AddrPort()
|
|
}
|
|
|
|
// BenchmarkConnSockWriteTo measures one WriteTo of a full-size datagram down each path: the listener with
|
|
// connected sockets off, to a destination that is no tunnel's remote while another is, to an established peer
|
|
// that never gets busy, and on a connected socket.
|
|
func BenchmarkConnSockWriteTo(b *testing.B) {
|
|
pkt := make([]byte, 1400)
|
|
cases := []struct {
|
|
name string
|
|
cfg func(*connSockConfig)
|
|
setup func(b *testing.B, u *StdConn, dst netip.AddrPort)
|
|
}{
|
|
{name: "listener/off", cfg: func(c *connSockConfig) { c.max = 0 }},
|
|
{name: "listener/ineligible", setup: func(b *testing.B, u *StdConn, dst netip.AddrPort) {
|
|
u.SetEstablishedPeer(netip.AddrPortFrom(dst.Addr(), dst.Port()+1), true)
|
|
}},
|
|
{name: "listener/eligible-not-busy", cfg: func(c *connSockConfig) { c.run = 1 << 30 },
|
|
setup: func(b *testing.B, u *StdConn, dst netip.AddrPort) { u.SetEstablishedPeer(dst, true) }},
|
|
{name: "connected", setup: func(b *testing.B, u *StdConn, dst netip.AddrPort) {
|
|
u.SetEstablishedPeer(dst, true)
|
|
for range u.socks.cfg.run {
|
|
_ = u.WriteTo(pkt, dst)
|
|
}
|
|
if u.connSockFor(dst) == nil {
|
|
b.Fatal("no connected socket")
|
|
}
|
|
}},
|
|
}
|
|
for _, c := range cases {
|
|
b.Run(c.name, func(b *testing.B) {
|
|
u, dst := benchConnSocks(b, Settings{Listen: netip.MustParseAddrPort("0.0.0.0:0")}, c.cfg)
|
|
if c.setup != nil {
|
|
c.setup(b, u, dst)
|
|
}
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for range b.N {
|
|
_ = u.WriteTo(pkt, dst)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// BenchmarkConnSockDispatch measures what WriteTo adds before the listener's sendto, without the syscall: a v4
|
|
// listener refuses a v6 destination before sending, with connected sockets off or with another peer established.
|
|
func BenchmarkConnSockDispatch(b *testing.B) {
|
|
pkt := make([]byte, 1400)
|
|
dst := netip.MustParseAddrPort("[::1]:4242")
|
|
for _, c := range []struct {
|
|
name string
|
|
cfg func(*connSockConfig)
|
|
establish bool
|
|
}{
|
|
{name: "off", cfg: func(c *connSockConfig) { c.max = 0 }},
|
|
{name: "ineligible", establish: true},
|
|
} {
|
|
b.Run(c.name, func(b *testing.B) {
|
|
u, peer := benchConnSocks(b, Settings{Listen: netip.MustParseAddrPort("127.0.0.1:0"), Multi: true}, c.cfg)
|
|
if c.establish {
|
|
u.SetEstablishedPeer(peer, true)
|
|
}
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for range b.N {
|
|
if u.WriteTo(pkt, dst) == nil {
|
|
b.Fatal("sent a v6 datagram on a v4 listener")
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|