From a89d610a3d731657a47c7331621c94d376456323 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Fri, 24 Jul 2020 08:29:36 -0700 Subject: [PATCH] wgengine/tstun: move sync.Pool to package global sync.Pools should almost always be packate globals, even though in this case we only have exactly 1 TUN device anyway, so it matters less. Still, it's unusual to see a Pool that's not a package global, so move it. --- wgengine/tstun/tun.go | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/wgengine/tstun/tun.go b/wgengine/tstun/tun.go index 951124cf3..48d0e6c43 100644 --- a/wgengine/tstun/tun.go +++ b/wgengine/tstun/tun.go @@ -45,6 +45,11 @@ var ( errOffsetTooSmall = errors.New("offset smaller than PacketStartOffset") ) +// parsedPacketPool holds a pool of ParsedPacket structs for use in filtering. +// This is needed because escape analysis cannot see that parsed packets +// do not escape through {Pre,Post}Filter{In,Out}. +var parsedPacketPool = sync.Pool{New: func() interface{} { return new(packet.ParsedPacket) }} + // FilterFunc is a packet-filtering function with access to the TUN device. // It must not hold onto the packet struct, as its backing storage will be reused. type FilterFunc func(*packet.ParsedPacket, *TUN) filter.Response @@ -66,10 +71,6 @@ type TUN struct { buffer [maxBufferSize]byte // bufferConsumed synchronizes access to buffer (shared by Read and poll). bufferConsumed chan struct{} - // parsedPacketPool holds a pool of ParsedPacket structs for use in filtering. - // This is needed because escape analysis cannot see that parsed packets - // do not escape through {Pre,Post}Filter{In,Out}. - parsedPacketPool sync.Pool // of *packet.ParsedPacket // closed signals poll (by closing) when the device is closed. closed chan struct{} @@ -121,10 +122,6 @@ func WrapTUN(logf logger.Logf, tdev tun.Device) *TUN { filterFlags: filter.LogAccepts | filter.LogDrops, } - tun.parsedPacketPool.New = func() interface{} { - return new(packet.ParsedPacket) - } - go tun.poll() // The buffer starts out consumed. tun.bufferConsumed <- struct{}{} @@ -208,8 +205,8 @@ func (t *TUN) poll() { } func (t *TUN) filterOut(buf []byte) filter.Response { - p := t.parsedPacketPool.Get().(*packet.ParsedPacket) - defer t.parsedPacketPool.Put(p) + p := parsedPacketPool.Get().(*packet.ParsedPacket) + defer parsedPacketPool.Put(p) p.Decode(buf) if t.PreFilterOut != nil { @@ -287,8 +284,8 @@ func (t *TUN) Read(buf []byte, offset int) (int, error) { } func (t *TUN) filterIn(buf []byte) filter.Response { - p := t.parsedPacketPool.Get().(*packet.ParsedPacket) - defer t.parsedPacketPool.Put(p) + p := parsedPacketPool.Get().(*packet.ParsedPacket) + defer parsedPacketPool.Put(p) p.Decode(buf) if t.PreFilterIn != nil {