google / google/gopacket

ZeroCopy read packet data causes heap allocations in pcapNextPacketEx

Open
#1,142 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
6.8k
Forks
1.2k
PR merge metrics
No merged PRs in 30d

Description

```
gopacket@v1.1.19

go version go1.21.1 linux/amd64

Fedora 39, amd64
```

ZeroCopy receiving using pcap on Unix causes 1-allocation per receive, which is very unfortunate:

![image](https://github.com/google/gopacket/assets/110322/74cf4e3f-4b8c-4c24-b0a4-6d3e31877b43)

There is a mysterious comment in this function suggesting some weirdness with GC:

```go
func (p *Handle) pcapNextPacketEx() NextError {
// This horrible magic allows us to pass a ptr-to-ptr to pcap_next_ex
// without causing that ptr-to-ptr to itself be allocated on the heap.
// Since Handle itself survives through the duration of the pcap_next_ex
// call, this should be perfectly safe for GC stuff, etc.

return NextError(C.pcap_next_ex_escaping(p.cptr, C.uintptr_t(uintptr(unsafe.Pointer(&p.pkthdr))), C.uintptr_t(uintptr(unsafe.Pointer(&p.bufptr)))))
}
```

No idea why this allocates anything.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.