net/udprelay: use batching.Conn (#16866)
This significantly improves throughput of a peer relay server on Linux. Server.packetReadLoop no longer passes sockets down the stack. Instead, packet handling methods return a netip.AddrPort and []byte, which packetReadLoop gathers together for eventual batched writes on the appropriate socket(s). Updates tailscale/corp#31164 Signed-off-by: Jordan Whited <jordan@tailscale.com>
This commit is contained in:
@@ -32,7 +32,6 @@ type Conn interface {
|
||||
// message may fall on either side of a nonzero.
|
||||
//
|
||||
// Each [ipv6.Message.OOB] must be sized to at least MinControlMessageSize().
|
||||
// len(msgs) must be at least MinReadBatchMsgsLen().
|
||||
ReadBatch(msgs []ipv6.Message, flags int) (n int, err error)
|
||||
// WriteBatchTo writes buffs to addr.
|
||||
//
|
||||
|
||||
@@ -19,3 +19,5 @@ var controlMessageSize = 0
|
||||
func MinControlMessageSize() int {
|
||||
return controlMessageSize
|
||||
}
|
||||
|
||||
const IdealBatchSize = 1
|
||||
|
||||
@@ -384,7 +384,7 @@ func setGSOSizeInControl(control *[]byte, gsoSize uint16) {
|
||||
}
|
||||
|
||||
// TryUpgradeToConn probes the capabilities of the OS and pconn, and upgrades
|
||||
// pconn to a [Conn] if appropriate. A batch size of MinReadBatchMsgsLen() is
|
||||
// pconn to a [Conn] if appropriate. A batch size of [IdealBatchSize] is
|
||||
// suggested for the best performance.
|
||||
func TryUpgradeToConn(pconn nettype.PacketConn, network string, batchSize int) nettype.PacketConn {
|
||||
if runtime.GOOS != "linux" {
|
||||
@@ -457,6 +457,4 @@ func MinControlMessageSize() int {
|
||||
return controlMessageSize
|
||||
}
|
||||
|
||||
func MinReadBatchMsgsLen() int {
|
||||
return 128
|
||||
}
|
||||
const IdealBatchSize = 128
|
||||
|
||||
@@ -310,7 +310,7 @@ func TestMinReadBatchMsgsLen(t *testing.T) {
|
||||
// So long as magicsock uses [Conn], and [wireguard-go/conn.Bind] API is
|
||||
// shaped for wireguard-go to control packet memory, these values should be
|
||||
// aligned.
|
||||
if MinReadBatchMsgsLen() != conn.IdealBatchSize {
|
||||
t.Fatalf("MinReadBatchMsgsLen():%d != conn.IdealBatchSize(): %d", MinReadBatchMsgsLen(), conn.IdealBatchSize)
|
||||
if IdealBatchSize != conn.IdealBatchSize {
|
||||
t.Fatalf("IdealBatchSize: %d != conn.IdealBatchSize(): %d", IdealBatchSize, conn.IdealBatchSize)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user