From 690f9456bbf140d3b4488988080d22ef747558c0 Mon Sep 17 00:00:00 2001 From: Steven Hood Date: Mon, 24 Oct 2022 14:16:06 -0700 Subject: [PATCH] Plug a packet buffer memory leak Decrement reference count after queueing the PacketBuffer. Since PacketBuffer objects come from a pool, this allows them to be returned to the pool instead of staying in memory and forcing new allocations. Fixes https://github.com/containers/gvisor-tap-vsock/issues/107 Signed-off-by: stevenmhood --- pkg/tap/switch.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/pkg/tap/switch.go b/pkg/tap/switch.go index f9883f558..30766b734 100644 --- a/pkg/tap/switch.go +++ b/pkg/tap/switch.go @@ -267,21 +267,22 @@ func (e *Switch) rxBuf(ctx context.Context, id int, buf []byte) { e.camLock.Unlock() if eth.DestinationAddress() != e.gateway.LinkAddress() { - if err := e.tx(stack.NewPacketBuffer(stack.PacketBufferOptions{ + pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ Payload: bufferv2.MakeWithData(buf), - })); err != nil { + }) + if err := e.tx(pkt); err != nil { log.Error(err) } + pkt.DecRef() } if eth.DestinationAddress() == e.gateway.LinkAddress() || eth.DestinationAddress() == header.EthernetBroadcastAddress { data := bufferv2.MakeWithData(buf) data.TrimFront(header.EthernetMinimumSize) - e.gateway.DeliverNetworkPacket( - eth.Type(), - stack.NewPacketBuffer(stack.PacketBufferOptions{ - Payload: data, - }), - ) + pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ + Payload: data, + }) + e.gateway.DeliverNetworkPacket(eth.Type(), pkt) + pkt.DecRef() } atomic.AddUint64(&e.Received, uint64(len(buf)))