Skip to content

Commit

Permalink
Plug a packet buffer memory leak
Browse files Browse the repository at this point in the history
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 #107

Signed-off-by: stevenmhood <[email protected]>
  • Loading branch information
Steven Hood authored and cfergeau committed Nov 3, 2022
1 parent 0dcf004 commit 690f945
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions pkg/tap/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down

0 comments on commit 690f945

Please sign in to comment.