From de5a2e894e7f4b971fd54f3aa444981cfc808e56 Mon Sep 17 00:00:00 2001 From: Lucas Manning Date: Mon, 13 Jan 2025 10:18:45 -0800 Subject: [PATCH] Create the interfaces for PACKET_MMAP endpoints to implement. PiperOrigin-RevId: 715019134 --- pkg/tcpip/stack/registration.go | 63 ++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/pkg/tcpip/stack/registration.go b/pkg/tcpip/stack/registration.go index a5938e439b..eef4ef8693 100644 --- a/pkg/tcpip/stack/registration.go +++ b/pkg/tcpip/stack/registration.go @@ -162,7 +162,7 @@ type PacketEndpoint interface { // match the endpoint. // // Implementers should treat packet as immutable and should copy it - // before before modification. + // before modification. // // linkHeader may have a length of 0, in which case the PacketEndpoint // should construct its own ethernet header for applications. @@ -171,6 +171,67 @@ type PacketEndpoint interface { HandlePacket(nicID tcpip.NICID, netProto tcpip.NetworkProtocolNumber, pkt *PacketBuffer) } +// MappablePacketEndpoint is a packet endpoint that supports forwarding its +// packets to a PacketMMapEndpoint. +type MappablePacketEndpoint interface { + PacketEndpoint + + // GetPacketMMapOpts returns the options for initializing a PacketMMapEndpoint + // for this endpoint. + GetPacketMMapOpts(req *tcpip.TpacketReq, isRx bool) PacketMMapOpts + + // SetPacketMMapEndpoint sets the PacketMMapEndpoint for this endpoint. All + // packets received by this endpoint will be forwarded to the provided + // PacketMMapEndpoint. + SetPacketMMapEndpoint(ep PacketMMapEndpoint) + + // GetPacketMMapEndpoint returns the PacketMMapEndpoint for this endpoint or + // nil if there is none. + GetPacketMMapEndpoint() PacketMMapEndpoint + + // HandlePacketMMapCopy is a function that is called when a packet received is + // too large for the buffer size specified for the memory mapped endpoint. In + // this case, the packet is copied and passed to the original packet endpoint. + HandlePacketMMapCopy(nicID tcpip.NICID, netProto tcpip.NetworkProtocolNumber, pkt *PacketBuffer) +} + +// PacketMMapOpts are the options for initializing a PacketMMapEndpoint. +// +// +stateify savable +type PacketMMapOpts struct { + Req *tcpip.TpacketReq + IsRx bool + Cooked bool + Stack *Stack + Stats *tcpip.TransportEndpointStats + Wq *waiter.Queue + NICID tcpip.NICID + NetProto tcpip.NetworkProtocolNumber + PacketEndpoint MappablePacketEndpoint +} + +// PacketMMapEndpoint is the interface implemented by endpoints to handle memory +// mapped packets over the packet transport protocol (PACKET_MMAP). +type PacketMMapEndpoint interface { + // HandlePacket is called by the stack when new packets arrive that + // match the endpoint. + // + // Implementers should treat packet as immutable and should copy it + // before modification. + // + // linkHeader may have a length of 0, in which case the PacketEndpoint + // should construct its own ethernet header for applications. + // + // HandlePacket may modify pkt. + HandlePacket(nicID tcpip.NICID, netProto tcpip.NetworkProtocolNumber, pkt *PacketBuffer) + + // Close releases any resources associated with the endpoint. + Close() + + // Readiness returns the events that the endpoint is ready for. + Readiness(mask waiter.EventMask) waiter.EventMask +} + // UnknownDestinationPacketDisposition enumerates the possible return values from // HandleUnknownDestinationPacket(). type UnknownDestinationPacketDisposition int