mirror of
https://github.com/netbirdio/gvisor.git
synced 2026-05-22 17:12:49 -07:00
Add PacketBufferPtr.ID() method to get a unique ID
Fuchsia uses the pointer value of the buffer for logging purposes[1]. The recent refactor to hide the pointer behind the value type PacketBufferPtr broke this. Add a method to expose an ID without exposing the raw pointer value. [1]: https://cs.opensource.google/fuchsia/fuchsia/+/main:src/connectivity/network/netstack/link/netdevice/client.go;l=405;drc=c1ef7fc5a62946351e696705017366815014fc09 PiperOrigin-RevId: 485940941
This commit is contained in:
@@ -544,6 +544,35 @@ func TestPacketBufferData(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPacketBufferId(t *testing.T) {
|
||||
pk := NewPacketBuffer(PacketBufferOptions{
|
||||
ReserveHeaderBytes: 12,
|
||||
})
|
||||
|
||||
id := pk.ID()
|
||||
// The ID should be stable
|
||||
if idAgain := pk.ID(); idAgain != id {
|
||||
t.Errorf("pk.ID() = %d, want %d", idAgain, id)
|
||||
}
|
||||
|
||||
// Shallow copies have the same ID.
|
||||
pkShallowCopy := pk
|
||||
if shallowCopyID := pkShallowCopy.ID(); shallowCopyID != id {
|
||||
t.Errorf("pkShallowCopy.ID() = %d, want %d", shallowCopyID, id)
|
||||
}
|
||||
|
||||
// Clones have different IDs.
|
||||
pkClone := pk.Clone()
|
||||
if cloneID := pkClone.ID(); cloneID == id {
|
||||
t.Errorf("pkClone.ID() = %d = pk.ID(), but pk = %#v, pkClone = %#v", cloneID, pk, pkClone)
|
||||
}
|
||||
|
||||
pk2 := NewPacketBuffer(PacketBufferOptions{ReserveHeaderBytes: 12})
|
||||
if id2 := pk2.ID(); id2 == id {
|
||||
t.Errorf("pk2.ID() = %d = pk.ID(), but pk = %#v, pk2 = %#v", id2, pk, pk2)
|
||||
}
|
||||
}
|
||||
|
||||
type packetContents struct {
|
||||
link []byte
|
||||
network []byte
|
||||
|
||||
@@ -18,3 +18,11 @@ import "unsafe"
|
||||
|
||||
// PacketBufferStructSize is the minimal size of the packet buffer overhead.
|
||||
const PacketBufferStructSize = int(unsafe.Sizeof(packetBuffer{}))
|
||||
|
||||
// ID returns a unique ID for the underlying storage of the packet.
|
||||
//
|
||||
// Two PacketBufferPtrs have the same IDs if and only if they point to the same
|
||||
// location in memory.
|
||||
func (pk PacketBufferPtr) ID() uintptr {
|
||||
return uintptr(unsafe.Pointer(pk.packetBuffer))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user