summaryrefslogtreecommitdiffhomepage
path: root/pkg/tcpip/link/channel
diff options
context:
space:
mode:
authorLucas Manning <lucasmanning@google.com>2021-11-08 13:26:02 -0800
committergVisor bot <gvisor-bot@google.com>2021-11-08 13:28:38 -0800
commit84b38f4c6e065d3f9314a8abbb3f5857ed4fa44e (patch)
tree53eb76fa6d0612696f93ec6919185ea5a37ff3f9 /pkg/tcpip/link/channel
parent49d23beb283d0306c9ccf5300e73517153ddd3c2 (diff)
Add reference counting to packet buffers.
PiperOrigin-RevId: 408426639
Diffstat (limited to 'pkg/tcpip/link/channel')
-rw-r--r--pkg/tcpip/link/channel/channel.go12
1 files changed, 12 insertions, 0 deletions
diff --git a/pkg/tcpip/link/channel/channel.go b/pkg/tcpip/link/channel/channel.go
index 658557d62..270fa8c79 100644
--- a/pkg/tcpip/link/channel/channel.go
+++ b/pkg/tcpip/link/channel/channel.go
@@ -81,6 +81,18 @@ func (q *queue) ReadContext(ctx context.Context) (PacketInfo, bool) {
}
func (q *queue) Write(p PacketInfo) bool {
+ // q holds the PacketBuffer.
+
+ // Ideally, Write() should take a reference here, since it is adding
+ // the underlying PacketBuffer to the channel. However, in practice,
+ // calls to Read() are not necessarily symetric with calls
+ // to Write() (e.g writing to this endpoint and then exiting). This
+ // causes tests and analyzers to detect erroneous "leaks" for expected
+ // behavior. To prevent this, we allow the refcount to go to zero, and
+ // make a call to PreserveObject(), which prevents the PacketBuffer
+ // pooling implementation from reclaiming this instance, even when
+ // the refcount goes to zero.
+ p.Pkt.PreserveObject()
wrote := false
select {
case q.c <- p: