Skip to content

Commit fb2f3e4

Browse files
valscalebradfitz
andcommitted
wgengine/netstack: use buffer pools for UDP packet forwarding
Use buffer pools for UDP packet forwarding to prepare for increasing the forwarded UDP packet size for peer path MTU discovery. Updates tailscale#311 Co-authored-by: Brad Fitzpatrick <[email protected]> Signed-off-by: Val <[email protected]>
1 parent 81e8335 commit fb2f3e4

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

wgengine/netstack/netstack.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,18 +1059,26 @@ func (ns *Impl) acceptUDP(r *udp.ForwarderRequest) {
10591059
go ns.forwardUDP(c, srcAddr, dstAddr)
10601060
}
10611061

1062+
// Buffer pool for forwarding UDP packets.
1063+
var udpBufPool = &sync.Pool{
1064+
New: func() any {
1065+
b := make([]byte, maxUDPPacketSize)
1066+
return &b
1067+
},
1068+
}
1069+
10621070
func (ns *Impl) handleMagicDNSUDP(srcAddr netip.AddrPort, c *gonet.UDPConn) {
1063-
// In practice, implementations are advised not to exceed 512 bytes
1064-
// due to fragmenting. Just to be sure, we bump all the way to the MTU.
1065-
var maxUDPReqSize = tstun.DefaultMTU()
10661071
// Packets are being generated by the local host, so there should be
10671072
// very, very little latency. 150ms was chosen as something of an upper
10681073
// bound on resource usage, while hopefully still being long enough for
10691074
// a heavily loaded system.
10701075
const readDeadline = 150 * time.Millisecond
10711076

10721077
defer c.Close()
1073-
q := make([]byte, maxUDPReqSize)
1078+
1079+
bufp := udpBufPool.Get().(*[]byte)
1080+
defer udpBufPool.Put(bufp)
1081+
q := *bufp
10741082

10751083
// libresolv from glibc is quite adamant that transmitting multiple DNS
10761084
// requests down the same UDP socket is valid. To support this, we read
@@ -1183,7 +1191,11 @@ func startPacketCopy(ctx context.Context, cancel context.CancelFunc, dst net.Pac
11831191
}
11841192
go func() {
11851193
defer cancel() // tear down the other direction's copy
1186-
pkt := make([]byte, maxUDPPacketSize)
1194+
1195+
bufp := udpBufPool.Get().(*[]byte)
1196+
defer udpBufPool.Put(bufp)
1197+
pkt := *bufp
1198+
11871199
for {
11881200
select {
11891201
case <-ctx.Done():

0 commit comments

Comments
 (0)