pktkit

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 5, 2026 License: MIT Imports: 10 Imported by: 0

README

pktkit

Go Reference Test Coverage Status Go Report Card

Zero-copy L2/L3 packet handling library for Go.

pktkit provides primitives for building virtual network topologies: devices, hubs, adapters, and tunnels that move Ethernet frames and IP packets without copying buffers on the hot path.

Features

Core
  • Zero-copy types: Frame and Packet are []byte aliases with typed header accessors — no wrapper allocation
  • Callback-based forwarding: synchronous func(T) error handler callbacks matching Send signatures, no channels
  • L2Device / L3Device interfaces: uniform API for all network devices
  • IPv4 and IPv6 support throughout the stack
Connectivity
  • L2Hub: MAC-learning switch with 5-minute aging — forwards unicast to learned ports, floods unknown/broadcast/multicast
  • L3Hub: routing hub with prefix-based unicast forwarding and broadcast/multicast delivery
  • ConnectL2 / ConnectL3: point-to-point wiring helpers (a.SetHandler(b.Send))
  • L2Connector / L3Connector: lifecycle-managed device attachment with cleanup callbacks
  • Serve: accept loop connecting incoming L2Devices to a connector, with automatic cleanup on disconnect
Bridging
  • L2Adapter: bridges an L3 device onto an L2 network with ARP (IPv4), NDP (IPv6), DHCP client, and gateway routing. Random MAC generated by default.
  • DHCPServer: L2 device serving DHCP leases with configurable IP pool, router, DNS, and lease time. Handles DISCOVER, OFFER, REQUEST, ACK, RELEASE, DECLINE, and INFORM.
Subpackages
  • slirp: userspace NAT stack implementing L3Device — routes virtual traffic to the real network via net.Dial, with namespace-isolated connection tracking for multi-tenant use (ConnectL3). Supports IPv4/IPv6 TCP, UDP, and ICMPv6.
  • vclient: virtual network client implementing L3Device — provides Dial, Listen, net.Conn, DNS resolution, and http.Client
  • vtcp: pure RFC-compliant TCP protocol engine (congestion control, SACK, timestamps, window scaling, SYN cookies)
  • nat: packet-level IPv4 NAT between two virtual L3 networks with ALGs (FTP, SIP, H.323, PPTP, TFTP, IRC), NAT64, defragmentation, namespace isolation, and UPnP
  • wg: WireGuard tunnel implementation with Noise IK handshake, per-peer L3 isolation, and adapter bridging peers to pktkit networks
  • ovpn: OpenVPN server with TLS key exchange, AES-CBC/GCM encryption, per-peer L3/L2 isolation, and username/password authentication
  • tuntap: OS-level TUN/TAP devices with IP address and route configuration (Linux and macOS)
  • qemu: QEMU userspace network socket protocol (both client and server/listener)

Usage

Point-to-point L3
import "github.com/KarpelesLab/pktkit"

tun1 := pktkit.NewPipeL3(netip.MustParsePrefix("10.0.0.1/24"))
tun2 := pktkit.NewPipeL3(netip.MustParsePrefix("10.0.0.2/24"))
pktkit.ConnectL3(tun1, tun2)
Virtual LAN with DHCP and NAT
hub := pktkit.NewL2Hub()

// DHCP server
dhcpSrv := pktkit.NewDHCPServer(pktkit.DHCPServerConfig{
    ServerIP:   netip.MustParseAddr("192.168.0.1"),
    SubnetMask: net.CIDRMask(24, 32),
    RangeStart: netip.MustParseAddr("192.168.0.10"),
    RangeEnd:   netip.MustParseAddr("192.168.0.100"),
    Router:     netip.MustParseAddr("192.168.0.1"),
    DNS:        []netip.Addr{netip.MustParseAddr("1.1.1.1")},
})
hub.Connect(dhcpSrv)

// NAT gateway (slirp routes to real internet)
stack := slirp.New()
stack.SetAddr(netip.MustParsePrefix("192.168.0.1/24"))
hub.Connect(pktkit.NewL2Adapter(stack, nil))

// Virtual client — gets IP via DHCP, can dial out
client := vclient.New()
adapter := pktkit.NewL2Adapter(client, nil)
hub.Connect(adapter)
adapter.StartDHCP()

// Use standard Go HTTP client over the virtual network
resp, _ := client.HTTPClient().Get("https://example.com")
WireGuard server with per-peer isolation

Each WireGuard peer gets a namespace-isolated NAT connection on a single slirp stack. Peers can share the same IP address without conflict.

stack := slirp.New()
stack.SetAddr(netip.MustParsePrefix("10.0.0.1/24"))

adapter, _ := wg.NewAdapter(wg.AdapterConfig{
    Connector: stack, // each peer gets isolated NAT via ConnectL3
})

udp, _ := net.ListenPacket("udp4", ":51820")
go adapter.Serve(udp)

// Add authorized peers
adapter.AddPeer(clientPublicKey)
QEMU VM networking

Connect QEMU VMs via the socket protocol. Each accepted connection can join a shared hub or get an isolated namespace.

ln, _ := qemu.Listen("unix", "/tmp/qemu.sock")

hub := pktkit.NewL2Hub()
// hub.Connect(pktkit.NewL2Adapter(stack, nil)) // add a NAT gateway

pktkit.Serve(ln, hub) // accept loop: each VM joins the hub

Performance

All data-plane hot paths are zero-allocation. Benchmarks on an i9-14900K (32 threads):

Core
Path ns/op Throughput Allocs
Frame accessors 0.63 2.4 TB/s 0
Packet IPv4 accessors 0.92 1.6 TB/s 0
Packet IPv6 accessors 0.67 2.2 TB/s 0
L2Hub unicast forward 50 30 GB/s 0
L2Hub forward (parallel) 2.3 656 GB/s 0
L3Hub route 9.2 163 GB/s 0
L2Adapter incoming (frame to packet) 33 46 GB/s 0
L2Adapter outgoing (packet to frame, pooled) 61 24 GB/s 0
Checksum (1500 B) 273 5.5 GB/s 0
SendBuf PeekUnsent 0.24 6.1 TB/s 0
ARP/NDP table lookup 37 - 0
NAT (1440 B payload)
Path ns/op Throughput Allocs
NAT outbound TCP 40 36 GB/s 0
NAT inbound TCP 41 35 GB/s 0
NAT outbound UDP 34 3.7 GB/s 0
NAT mapping lookup 12 - 0
Defrag (non-fragment fast path) 1.4 1.0 TB/s 0
WireGuard (1420 B payload)
Path ns/op Throughput Allocs
Encrypt (EncryptTo, zero-alloc) 555 2.6 GB/s 0
Encrypt + Decrypt (zero-alloc) 1131 1.3 GB/s 0
Encrypt (Encrypt, allocating) 2417 588 MB/s 1
Decrypt (ProcessPacket) 4135 343 MB/s 1
Handshake (full initiation + response) 578k - 481
Key generation 57 - 0
Replay filter 9.1 - 0
Peer lookup 13 - 0
OpenVPN GCM (1420 B payload)
Path ns/op Throughput Allocs
GCM encrypt 227 6.3 GB/s 1
GCM decrypt 202 7.0 GB/s 0
CBC encrypt 8307 171 MB/s 15
Replay window 9.2 - 0
PRF 1.2 (256 B) 3497 73 MB/s 25
Options parse 721 - 4
End-to-end VPN over UDP loopback (1400 B payload)

Both benchmarks use real UDP sockets on 127.0.0.1. Throughput is measured with a saturated pipeline (sender blasts, receiver counts); latency is per-packet synchronous send-wait.

Metric WireGuard (ChaCha20-Poly1305) OpenVPN (AES-256-GCM)
Throughput 299 MB/s (2.4 Gbps) 678 MB/s (5.4 Gbps)
Latency 23.9 us 20.0 us

OpenVPN GCM is faster end-to-end on x86 because AES-GCM uses AES-NI hardware acceleration, while ChaCha20-Poly1305 is a software cipher. On platforms without AES-NI (e.g. ARM without crypto extensions), WireGuard's ChaCha20 is typically faster.

Run benchmarks with go test -bench=. -benchmem ./....

License

MIT — see LICENSE.

Documentation

Overview

Package pktkit provides zero-copy packet handling primitives, virtual network devices, and hubs.

Index

Constants

View Source
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

func AllocBuffer(n int) ([]byte, *[]byte)

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 Checksum

func Checksum(data []byte) uint16

Checksum computes the Internet checksum (RFC 1071) over data.

func CombineChecksums

func CombineChecksums(a, b uint16) uint16

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

func Noescape(p unsafe.Pointer) unsafe.Pointer

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

func NoescapeBytes(p unsafe.Pointer, n int) []byte

NoescapeBytes returns a []byte of length n pointing to p without causing escape analysis to move *p to the heap.

func PseudoHeaderChecksum

func PseudoHeaderChecksum(proto Protocol, src, dst netip.Addr, length uint16) uint16

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.

const (
	EtherTypeIPv4 EtherType = 0x0800
	EtherTypeARP  EtherType = 0x0806
	EtherTypeVLAN EtherType = 0x8100
	EtherTypeIPv6 EtherType = 0x86DD
)

func (EtherType) String

func (e EtherType) String() string

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

func (f Frame) EtherType() EtherType

EtherType returns the protocol type of the frame payload. Handles 802.1Q tagged frames transparently.

func (Frame) HasVLAN

func (f Frame) HasVLAN() bool

HasVLAN returns true if the frame has an 802.1Q VLAN tag.

func (Frame) HeaderLen

func (f Frame) HeaderLen() int

HeaderLen returns the Ethernet header length in bytes (14 normally, 18 with VLAN tag).

func (Frame) IsBroadcast

func (f Frame) IsBroadcast() bool

IsBroadcast returns true if the destination MAC is the broadcast address.

func (Frame) IsMulticast

func (f Frame) IsMulticast() bool

IsMulticast returns true if the destination MAC has the multicast bit set.

func (Frame) IsValid

func (f Frame) IsValid() bool

IsValid returns true if the frame is large enough to contain an Ethernet header.

func (Frame) Payload

func (f Frame) Payload() []byte

Payload returns the frame payload (everything after the 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.

func (Frame) VLANID

func (f Frame) VLANID() uint16

VLANID returns the VLAN identifier. Only valid if HasVLAN returns true.

type L2Acceptor

type L2Acceptor interface {
	AcceptL2() (L2Device, error)
}

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) Close

func (a *L2Adapter) Close() error

Close stops DHCP, pending queue cleanup, and releases resources.

func (*L2Adapter) HWAddr

func (a *L2Adapter) HWAddr() net.HardwareAddr

HWAddr returns the adapter's MAC address.

func (*L2Adapter) Send

func (a *L2Adapter) Send(f Frame) error

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

func (a *L2Adapter) SetGateway(gw netip.Addr)

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

func (a *L2Adapter) SetHandler(h func(Frame) error)

SetHandler is called by the L2 network (e.g. an L2Hub) to receive frames produced by this adapter.

func (*L2Adapter) StartDHCP

func (a *L2Adapter) StartDHCP()

StartDHCP begins DHCP discovery to obtain an IP address for the underlying L3 device.

func (*L2Adapter) StopDHCP

func (a *L2Adapter) StopDHCP()

StopDHCP cancels any running DHCP operations.

type L2Connector

type L2Connector interface {
	ConnectL2(dev L2Device) (cleanup func() error, err error)
}

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 NewL2Hub

func NewL2Hub() *L2Hub

NewL2Hub creates a new L2 learning switch.

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.

func (*L2Hub) ConnectL2

func (h *L2Hub) ConnectL2(dev L2Device) (func() error, error)

ConnectL2 implements L2Connector. It connects the device to the hub and returns a cleanup function that disconnects it.

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

type L3Connector interface {
	ConnectL3(dev L3Device) (cleanup func() error, err error)
}

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 NewL3Hub

func NewL3Hub() *L3Hub

NewL3Hub creates a new L3 routing hub.

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

func (h *L3Hub) SetDefaultRoute(dev L3Device)

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) DstAddr

func (p Packet) DstAddr() netip.Addr

DstAddr returns the destination IP address, dispatching on Version.

func (Packet) IPProtocol

func (p Packet) IPProtocol() Protocol

IPProtocol returns the IP protocol number, dispatching on Version.

func (Packet) IPv4DstAddr

func (p Packet) IPv4DstAddr() netip.Addr

IPv4DstAddr returns the destination IPv4 address.

func (Packet) IPv4HeaderLen

func (p Packet) IPv4HeaderLen() int

IPv4HeaderLen returns the IPv4 header length in bytes (IHL field * 4).

func (Packet) IPv4Payload

func (p Packet) IPv4Payload() []byte

IPv4Payload returns the IPv4 payload (data after the header).

func (Packet) IPv4Protocol

func (p Packet) IPv4Protocol() Protocol

IPv4Protocol returns the Protocol field.

func (Packet) IPv4SrcAddr

func (p Packet) IPv4SrcAddr() netip.Addr

IPv4SrcAddr returns the source IPv4 address.

func (Packet) IPv4TTL

func (p Packet) IPv4TTL() uint8

IPv4TTL returns the Time To Live field.

func (Packet) IPv4TotalLen

func (p Packet) IPv4TotalLen() uint16

IPv4TotalLen returns the Total Length field of the IPv4 header.

func (Packet) IPv6DstAddr

func (p Packet) IPv6DstAddr() netip.Addr

IPv6DstAddr returns the destination IPv6 address.

func (Packet) IPv6HopLimit

func (p Packet) IPv6HopLimit() uint8

IPv6HopLimit returns the Hop Limit field.

func (Packet) IPv6NextHeader

func (p Packet) IPv6NextHeader() Protocol

IPv6NextHeader returns the Next Header field (equivalent to IPv4 Protocol).

func (Packet) IPv6Payload

func (p Packet) IPv6Payload() []byte

IPv6Payload returns the IPv6 payload (data after the fixed 40-byte header).

func (Packet) IPv6PayloadLen

func (p Packet) IPv6PayloadLen() uint16

IPv6PayloadLen returns the Payload Length field of the IPv6 header.

func (Packet) IPv6SrcAddr

func (p Packet) IPv6SrcAddr() netip.Addr

IPv6SrcAddr returns the source IPv6 address.

func (Packet) IsBroadcast

func (p Packet) IsBroadcast() bool

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

func (p Packet) IsMulticast() bool

IsMulticast returns true if the destination is a multicast address (IPv4 224.0.0.0/4 or IPv6 ff00::/8).

func (Packet) IsValid

func (p Packet) IsValid() bool

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) Payload

func (p Packet) Payload() []byte

Payload returns the IP payload, dispatching on Version.

func (Packet) SetIPv4DstAddr

func (p Packet) SetIPv4DstAddr(addr netip.Addr)

SetIPv4DstAddr writes the destination IPv4 address in place.

func (Packet) SetIPv4SrcAddr

func (p Packet) SetIPv4SrcAddr(addr netip.Addr)

SetIPv4SrcAddr writes the source IPv4 address in place.

func (Packet) SrcAddr

func (p Packet) SrcAddr() netip.Addr

SrcAddr returns the source IP address, dispatching on Version.

func (Packet) Version

func (p Packet) Version() int

Version returns the IP version (4 or 6). Returns 0 if the packet is empty.

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) Close

func (p *PipeL2) Close() error

func (*PipeL2) HWAddr

func (p *PipeL2) HWAddr() net.HardwareAddr

func (*PipeL2) Inject

func (p *PipeL2) Inject(f Frame) error

Inject pushes a frame into the pipe as if it were received from the network, triggering the handler.

func (*PipeL2) Send

func (p *PipeL2) Send(f Frame) error

func (*PipeL2) SetHandler

func (p *PipeL2) SetHandler(h func(Frame) error)

type PipeL3

type PipeL3 struct {
	// contains filtered or unexported fields
}

PipeL3 is a simple in-memory L3Device useful for testing.

func NewPipeL3

func NewPipeL3(addr netip.Prefix) *PipeL3

NewPipeL3 creates a PipeL3 with the given IP prefix.

func (*PipeL3) Addr

func (p *PipeL3) Addr() netip.Prefix

func (*PipeL3) Close

func (p *PipeL3) Close() error

func (*PipeL3) Inject

func (p *PipeL3) Inject(pkt Packet) error

Inject pushes a packet into the pipe as if it were received from the network, triggering the handler.

func (*PipeL3) Send

func (p *PipeL3) Send(pkt Packet) error

func (*PipeL3) SetAddr

func (p *PipeL3) SetAddr(prefix netip.Prefix) error

func (*PipeL3) SetHandler

func (p *PipeL3) SetHandler(h func(Packet) error)

type Protocol

type Protocol uint8

Protocol identifies the IP protocol number carried in an IP packet.

const (
	ProtocolICMP   Protocol = 1
	ProtocolTCP    Protocol = 6
	ProtocolUDP    Protocol = 17
	ProtocolICMPv6 Protocol = 58
)

func (Protocol) String

func (p Protocol) String() string

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL