Documentation
¶
Overview ¶
Package pktkit provides zero-copy packet handling primitives, virtual network devices, and hubs.
Index ¶
- Constants
- func AllocBuffer(n int) ([]byte, *[]byte)
- func Checksum(data []byte) uint16
- func CombineChecksums(a, b uint16) uint16
- func ConnectL2(a, b L2Device)
- func ConnectL3(a, b L3Device)
- func FreeBuffer(bufp *[]byte)
- func Noescape(p unsafe.Pointer) unsafe.Pointer
- func NoescapeBytes(p unsafe.Pointer, n int) []byte
- func PseudoHeaderChecksum(proto Protocol, src, dst netip.Addr, length uint16) uint16
- func Serve(acceptor L2Acceptor, connector L2Connector) error
- type DHCPServer
- type DHCPServerConfig
- type EtherType
- type Frame
- func (f Frame) DstMAC() net.HardwareAddr
- func (f Frame) EtherType() EtherType
- func (f Frame) HasVLAN() bool
- func (f Frame) HeaderLen() int
- func (f Frame) IsBroadcast() bool
- func (f Frame) IsMulticast() bool
- func (f Frame) IsValid() bool
- func (f Frame) Payload() []byte
- func (f Frame) SetDstMAC(mac net.HardwareAddr)
- func (f Frame) SetSrcMAC(mac net.HardwareAddr)
- func (f Frame) SrcMAC() net.HardwareAddr
- func (f Frame) VLANID() uint16
- type L2Acceptor
- type L2Adapter
- type L2Connector
- type L2Device
- type L2Hub
- type L2HubHandle
- type L3Connector
- type L3Device
- type L3Hub
- type L3HubHandle
- type Packet
- func (p Packet) DstAddr() netip.Addr
- func (p Packet) IPProtocol() Protocol
- func (p Packet) IPv4DstAddr() netip.Addr
- func (p Packet) IPv4HeaderLen() int
- func (p Packet) IPv4Payload() []byte
- func (p Packet) IPv4Protocol() Protocol
- func (p Packet) IPv4SrcAddr() netip.Addr
- func (p Packet) IPv4TTL() uint8
- func (p Packet) IPv4TotalLen() uint16
- func (p Packet) IPv6DstAddr() netip.Addr
- func (p Packet) IPv6HopLimit() uint8
- func (p Packet) IPv6NextHeader() Protocol
- func (p Packet) IPv6Payload() []byte
- func (p Packet) IPv6PayloadLen() uint16
- func (p Packet) IPv6SrcAddr() netip.Addr
- func (p Packet) IsBroadcast() bool
- func (p Packet) IsMulticast() bool
- func (p Packet) IsValid() bool
- func (p Packet) Payload() []byte
- func (p Packet) SetIPv4DstAddr(addr netip.Addr)
- func (p Packet) SetIPv4SrcAddr(addr netip.Addr)
- func (p Packet) SrcAddr() netip.Addr
- func (p Packet) Version() int
- type PipeL2
- type PipeL3
- type Protocol
Constants ¶
const DefaultMTU = 1536
DefaultMTU is the default buffer size used by the packet pool.
Variables ¶
This section is empty.
Functions ¶
func AllocBuffer ¶ added in v0.1.1
AllocBuffer returns a byte slice of length n from the global packet pool. The returned *[]byte handle must be passed to FreeBuffer when the caller is done with the buffer. The slice may have capacity > n.
func CombineChecksums ¶
CombineChecksums folds two partial checksums into one.
func ConnectL2 ¶
func ConnectL2(a, b L2Device)
ConnectL2 wires two L2Devices in a point-to-point link: frames produced by one are delivered to the other.
func ConnectL3 ¶
func ConnectL3(a, b L3Device)
ConnectL3 wires two L3Devices in a point-to-point link: packets produced by one are delivered to the other.
func FreeBuffer ¶ added in v0.1.1
func FreeBuffer(bufp *[]byte)
FreeBuffer returns a buffer obtained from AllocBuffer to the pool.
func Noescape ¶ added in v0.1.1
Noescape hides a pointer from escape analysis. Use this to prevent stack-allocated arrays (e.g. nonces, IVs) from escaping to the heap when passed as slices to crypto interfaces.
func NoescapeBytes ¶ added in v0.1.1
NoescapeBytes returns a []byte of length n pointing to p without causing escape analysis to move *p to the heap.
func PseudoHeaderChecksum ¶
PseudoHeaderChecksum returns the checksum contribution of the TCP/UDP pseudo-header for the given protocol, addresses, and upper-layer length.
func Serve ¶
func Serve(acceptor L2Acceptor, connector L2Connector) error
Serve runs an accept loop, connecting each accepted L2Device to the connector. If the accepted device implements a Done() <-chan struct{} method (e.g. [qemu.Conn]), cleanup is called automatically when the device's connection closes. Blocks until the acceptor returns an error.
Types ¶
type DHCPServer ¶
type DHCPServer struct {
// contains filtered or unexported fields
}
DHCPServer is an L2 device that serves DHCP leases on an Ethernet network.
func NewDHCPServer ¶
func NewDHCPServer(cfg DHCPServerConfig) *DHCPServer
NewDHCPServer creates a new DHCP server with the given configuration.
func (*DHCPServer) Close ¶
func (s *DHCPServer) Close() error
func (*DHCPServer) HWAddr ¶
func (s *DHCPServer) HWAddr() net.HardwareAddr
func (*DHCPServer) Send ¶
func (s *DHCPServer) Send(f Frame) error
func (*DHCPServer) SetHandler ¶
func (s *DHCPServer) SetHandler(h func(Frame) error)
type DHCPServerConfig ¶
type DHCPServerConfig struct {
// ServerIP is the server's own IP address.
ServerIP netip.Addr
// SubnetMask is the subnet mask to advertise.
SubnetMask net.IPMask
// RangeStart is the first IP in the leasable pool.
RangeStart netip.Addr
// RangeEnd is the last IP in the leasable pool (inclusive).
RangeEnd netip.Addr
// Router is the default gateway to advertise.
Router netip.Addr
// DNS is the list of DNS servers to advertise.
DNS []netip.Addr
// LeaseTime is the lease duration. Defaults to 1 hour if zero.
LeaseTime time.Duration
// MAC is the server's hardware address. Generated if nil.
MAC net.HardwareAddr
}
DHCPServerConfig configures a DHCP server.
type EtherType ¶
type EtherType uint16
EtherType identifies the protocol encapsulated in an Ethernet frame payload.
type Frame ¶
type Frame []byte
Frame is a raw Ethernet frame. It is a []byte type alias providing zero-copy typed access to header fields. The underlying buffer is only valid during the callback that receives it; callers must copy if they need to retain the data.
func NewFrame ¶
func NewFrame(dst, src net.HardwareAddr, etherType EtherType, payload []byte) Frame
NewFrame constructs an Ethernet frame from the given header fields and payload.
func (Frame) DstMAC ¶
func (f Frame) DstMAC() net.HardwareAddr
DstMAC returns the destination MAC address. Returns nil if the frame is invalid.
func (Frame) EtherType ¶
EtherType returns the protocol type of the frame payload. Handles 802.1Q tagged frames transparently.
func (Frame) HeaderLen ¶
HeaderLen returns the Ethernet header length in bytes (14 normally, 18 with VLAN tag).
func (Frame) IsBroadcast ¶
IsBroadcast returns true if the destination MAC is the broadcast address.
func (Frame) IsMulticast ¶
IsMulticast returns true if the destination MAC has the multicast bit set.
func (Frame) IsValid ¶
IsValid returns true if the frame is large enough to contain an Ethernet header.
func (Frame) SetDstMAC ¶
func (f Frame) SetDstMAC(mac net.HardwareAddr)
SetDstMAC writes the destination MAC address in place.
func (Frame) SetSrcMAC ¶
func (f Frame) SetSrcMAC(mac net.HardwareAddr)
SetSrcMAC writes the source MAC address in place.
func (Frame) SrcMAC ¶
func (f Frame) SrcMAC() net.HardwareAddr
SrcMAC returns the source MAC address. Returns nil if the frame is invalid.
type L2Acceptor ¶
L2Acceptor produces L2Devices, typically from incoming network connections. [qemu.Listener] implements this interface.
type L2Adapter ¶
type L2Adapter struct {
// contains filtered or unexported fields
}
L2Adapter wraps an L3Device and presents it as an L2Device. It handles Ethernet framing, ARP resolution, and optionally DHCP for IP acquisition on behalf of the underlying L3 device.
Usage:
l3dev := NewPipeL3(netip.MustParsePrefix("10.0.0.2/24"))
adapter := NewL2Adapter(l3dev, nil) // random MAC
hub.Connect(adapter) // adapter implements L2Device
func NewL2Adapter ¶
func NewL2Adapter(dev L3Device, mac net.HardwareAddr) *L2Adapter
NewL2Adapter creates an adapter that wraps the given L3Device. If mac is nil, a random locally-administered unicast MAC address is generated. It wires itself as the L3Device's handler so that outgoing packets are automatically framed and sent on the L2 network.
func (*L2Adapter) HWAddr ¶
func (a *L2Adapter) HWAddr() net.HardwareAddr
HWAddr returns the adapter's MAC address.
func (*L2Adapter) Send ¶
Send delivers a frame from the L2 network to this adapter. The adapter filters by destination MAC, handles ARP, intercepts DHCP, and forwards IP payloads to the wrapped L3 device.
func (*L2Adapter) SetGateway ¶
SetGateway sets the default gateway for off-subnet routing. When sending to an IP not covered by the L3 device's prefix, the adapter will ARP for the gateway MAC instead of the destination IP directly.
func (*L2Adapter) SetHandler ¶
SetHandler is called by the L2 network (e.g. an L2Hub) to receive frames produced by this adapter.
type L2Connector ¶
L2Connector receives L2Devices and manages their attachment lifecycle. When the returned cleanup function is called, the device is detached.
Implementations:
- *L2Hub: all devices join the shared hub (cleanup disconnects)
type L2Device ¶
type L2Device interface {
SetHandler(func(Frame) error)
Send(Frame) error
HWAddr() net.HardwareAddr
Close() error
}
L2Device represents a Layer 2 (Ethernet) network device.
SetHandler must be called before the device starts producing frames. Send may be called from any goroutine. The Frame passed to the handler is only valid for the duration of the callback.
type L2Hub ¶
type L2Hub struct {
// contains filtered or unexported fields
}
L2Hub is a learning switch that forwards Ethernet frames between connected devices. It learns source MAC addresses and forwards unicast frames only to the port associated with the destination MAC. Unknown unicast, broadcast, and multicast frames are flooded to all ports except the source.
It uses a copy-on-write port list for lock-free reads on the hot path and a sync.Map for the MAC address table.
func (*L2Hub) Connect ¶
func (h *L2Hub) Connect(dev L2Device) *L2HubHandle
Connect adds a device to the switch. The device's handler is set to forward received frames through the switch's learning/forwarding logic. Returns a handle whose Close method disconnects the device.
type L2HubHandle ¶
type L2HubHandle struct {
// contains filtered or unexported fields
}
L2HubHandle is returned by L2Hub.Connect and allows disconnecting a device from the switch.
func (*L2HubHandle) Close ¶
func (hh *L2HubHandle) Close() error
Close disconnects the device from the switch. Safe to call multiple times.
type L3Connector ¶
L3Connector receives L3Devices and manages their attachment lifecycle. This is the natural interface for protocols that operate at the IP layer (e.g. WireGuard), avoiding unnecessary L2 framing overhead.
Implementations:
- [slirp.Stack]: each device gets a namespace-isolated NAT (cleanup removes it)
- [nat.NAT]: each device gets a namespace-isolated NAT (cleanup removes it)
type L3Device ¶
type L3Device interface {
SetHandler(func(Packet) error)
Send(Packet) error
Addr() netip.Prefix
SetAddr(netip.Prefix) error
Close() error
}
L3Device represents a Layer 3 (IP) network device.
SetHandler must be called before the device starts producing packets. Send may be called from any goroutine. The Packet passed to the handler is only valid for the duration of the callback.
Addr returns the device's current IP prefix. SetAddr updates it (e.g. from DHCP). Implementations should store the prefix atomically.
type L3Hub ¶
type L3Hub struct {
// contains filtered or unexported fields
}
L3Hub is a routing hub that forwards IP packets to the appropriate connected device based on destination address prefix matching. Multicast and broadcast packets are sent to all ports except the source. A default route can be set for packets that don't match any connected prefix.
func (*L3Hub) Connect ¶
func (h *L3Hub) Connect(dev L3Device) *L3HubHandle
Connect adds a device to the hub. The device's handler is set to route received packets to the appropriate hub port. Returns a handle whose Close method disconnects the device.
func (*L3Hub) SetDefaultRoute ¶
SetDefaultRoute configures dev as the default route for packets that don't match any connected prefix.
type L3HubHandle ¶
type L3HubHandle struct {
// contains filtered or unexported fields
}
L3HubHandle is returned by L3Hub.Connect and allows disconnecting a device from the hub.
func (*L3HubHandle) Close ¶
func (hh *L3HubHandle) Close() error
Close disconnects the device from the hub. Safe to call multiple times.
type Packet ¶
type Packet []byte
Packet is a raw IP packet (no Ethernet header). It is a []byte type alias providing zero-copy typed access to IPv4 and IPv6 header fields. The underlying buffer is only valid during the callback that receives it.
func (Packet) IPProtocol ¶
IPProtocol returns the IP protocol number, dispatching on Version.
func (Packet) IPv4DstAddr ¶
IPv4DstAddr returns the destination IPv4 address.
func (Packet) IPv4HeaderLen ¶
IPv4HeaderLen returns the IPv4 header length in bytes (IHL field * 4).
func (Packet) IPv4Payload ¶
IPv4Payload returns the IPv4 payload (data after the header).
func (Packet) IPv4Protocol ¶
IPv4Protocol returns the Protocol field.
func (Packet) IPv4SrcAddr ¶
IPv4SrcAddr returns the source IPv4 address.
func (Packet) IPv4TotalLen ¶
IPv4TotalLen returns the Total Length field of the IPv4 header.
func (Packet) IPv6DstAddr ¶
IPv6DstAddr returns the destination IPv6 address.
func (Packet) IPv6HopLimit ¶
IPv6HopLimit returns the Hop Limit field.
func (Packet) IPv6NextHeader ¶
IPv6NextHeader returns the Next Header field (equivalent to IPv4 Protocol).
func (Packet) IPv6Payload ¶
IPv6Payload returns the IPv6 payload (data after the fixed 40-byte header).
func (Packet) IPv6PayloadLen ¶
IPv6PayloadLen returns the Payload Length field of the IPv6 header.
func (Packet) IPv6SrcAddr ¶
IPv6SrcAddr returns the source IPv6 address.
func (Packet) IsBroadcast ¶
IsBroadcast returns true if the destination is the IPv4 limited broadcast address (255.255.255.255). IPv6 has no broadcast; use IsMulticast instead.
func (Packet) IsMulticast ¶
IsMulticast returns true if the destination is a multicast address (IPv4 224.0.0.0/4 or IPv6 ff00::/8).
func (Packet) IsValid ¶
IsValid returns true if the packet is large enough to determine the IP version and contains at least the minimum header for that version.
func (Packet) SetIPv4DstAddr ¶
SetIPv4DstAddr writes the destination IPv4 address in place.
func (Packet) SetIPv4SrcAddr ¶
SetIPv4SrcAddr writes the source IPv4 address in place.
type PipeL2 ¶
type PipeL2 struct {
// contains filtered or unexported fields
}
PipeL2 is a simple in-memory L2Device useful for testing and for wiring devices in subpackages. Frames sent to it are forwarded to the handler.
func NewPipeL2 ¶
func NewPipeL2(mac net.HardwareAddr) *PipeL2
NewPipeL2 creates a PipeL2 with the given MAC address.
func (*PipeL2) HWAddr ¶
func (p *PipeL2) HWAddr() net.HardwareAddr
func (*PipeL2) Inject ¶
Inject pushes a frame into the pipe as if it were received from the network, triggering the handler.
func (*PipeL2) SetHandler ¶
type PipeL3 ¶
type PipeL3 struct {
// contains filtered or unexported fields
}
PipeL3 is a simple in-memory L3Device useful for testing.
func (*PipeL3) Inject ¶
Inject pushes a packet into the pipe as if it were received from the network, triggering the handler.
func (*PipeL3) SetHandler ¶
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package afxdp implements pktkit.L2Device over Linux AF_XDP sockets, providing kernel-bypass Ethernet frame I/O via shared memory ring buffers.
|
Package afxdp implements pktkit.L2Device over Linux AF_XDP sockets, providing kernel-bypass Ethernet frame I/O via shared memory ring buffers. |
|
Package nat implements IPv4 network address translation between two L3 networks.
|
Package nat implements IPv4 network address translation between two L3 networks. |
|
Package qemu implements QEMU's userspace network socket protocol.
|
Package qemu implements QEMU's userspace network socket protocol. |
|
Package tuntap provides OS-level TUN and TAP devices implementing pktkit's pktkit.L3Device and pktkit.L2Device interfaces.
|
Package tuntap provides OS-level TUN and TAP devices implementing pktkit's pktkit.L3Device and pktkit.L2Device interfaces. |
|
Package vclient implements a virtual network client with a user-space TCP/IP stack.
|
Package vclient implements a virtual network client with a user-space TCP/IP stack. |
|
Package vtcp implements a full, RFC-compliant TCP protocol engine operating on raw TCP segments.
|
Package vtcp implements a full, RFC-compliant TCP protocol engine operating on raw TCP segments. |
|
Package wgnet implements a WireGuard point-to-point endpoint library.
|
Package wgnet implements a WireGuard point-to-point endpoint library. |