Documentation
¶
Overview ¶
Package gnet implements TCP/IP connectivity through a generic NetworkDevice interface and TCP/IP Stack.
The package provides GVisorStack as pure Go Stack implementation using gVisor.
This package is designed for, but not limited to, use with `GOOS=tamago` as supported by the TamaGo framework for bare metal Go, see https://github.com/usbarmory/tamago.
Index ¶
- Constants
- Variables
- type GVisorStack
- func (g *GVisorStack) Configure(mac net.HardwareAddr, ip netip.Prefix, gw netip.Addr) (err error)
- func (g *GVisorStack) EnableICMP() error
- func (g *GVisorStack) HardwareAddress() (net.HardwareAddr, error)
- func (g *GVisorStack) ListenerTCP4(port uint16) (net.Listener, error)
- func (g *GVisorStack) RecvInboundPacket(buf []byte) error
- func (g *GVisorStack) SetWriteNotify(notifier func(buf []byte))
- func (g *GVisorStack) Socket(ctx context.Context, network string, family, sotype int, laddr, raddr net.Addr) (c interface{}, err error)
- func (g *GVisorStack) WriteOutboundPacket(buf []byte) (int, error)
- type Interface
- type LnetoConfig
- type LnetoStack
- func (ls *LnetoStack) Configure(mac net.HardwareAddr, ip netip.Prefix, gw netip.Addr) error
- func (ls *LnetoStack) EnableICMP() error
- func (ls *LnetoStack) HardwareAddress() (net.HardwareAddr, error)
- func (ls *LnetoStack) RecvInboundPacket(buf []byte) error
- func (ls *LnetoStack) SeedNeighbor(addr netip.Addr, mac [6]byte) error
- func (ls *LnetoStack) SetWriteNotify(cb func(buf []byte))
- func (ls *LnetoStack) Socket(ctx context.Context, network string, family, sotype int, laddr, raddr net.Addr) (c interface{}, err error)
- func (ls *LnetoStack) WriteOutboundPacket(buf []byte) (int, error)
- type NetworkDevice
- type Stack
Constants ¶
const ( // MTU represents the Maximum Transmission Unit. MTU = 1500 // EthernetMinimumSize is the minimum size of a valid ethernet frame. EthernetMinimumSize = 14 // EthernetMaximumSize is the maximum size of a valid ethernet frame. EthernetMaximumSize = 18 )
Variables ¶
var ( // NICID represents the default gVisor NIC identifier NICID = tcpip.NICID(1) // DefaultStackOptions represents the default gVisor Stack configuration DefaultStackOptions = stack.Options{ NetworkProtocols: []stack.NetworkProtocolFactory{ ipv4.NewProtocol, arp.NewProtocol}, TransportProtocols: []stack.TransportProtocolFactory{ tcp.NewProtocol, icmp.NewProtocol4, udp.NewProtocol}, } )
Functions ¶
This section is empty.
Types ¶
type GVisorStack ¶
type GVisorStack struct {
Stack *stack.Stack
Link *channel.Endpoint
NICID tcpip.NICID
// contains filtered or unexported fields
}
GVisorStack implements Stack using the gvisor.dev/gvisor package.
func NewGVisorStack ¶
func NewGVisorStack(nicid tcpip.NICID) *GVisorStack
NewGVisorStack returns a gvisor stack ready to configure with the given tcpip.NICID.
func (*GVisorStack) Configure ¶
func (g *GVisorStack) Configure(mac net.HardwareAddr, ip netip.Prefix, gw netip.Addr) (err error)
Configure implements Stack.Configure.
func (*GVisorStack) EnableICMP ¶
func (g *GVisorStack) EnableICMP() error
EnableICMP implements Stack.
func (*GVisorStack) HardwareAddress ¶
func (g *GVisorStack) HardwareAddress() (net.HardwareAddr, error)
HardwareAddress implements Stack.HardwareAddress.
func (*GVisorStack) ListenerTCP4 ¶
func (g *GVisorStack) ListenerTCP4(port uint16) (net.Listener, error)
ListenerTCP4 returns a net.Listener capable of accepting IPv4 TCP connections for the argument port on this stack.
func (*GVisorStack) RecvInboundPacket ¶
func (g *GVisorStack) RecvInboundPacket(buf []byte) error
RecvInboundPacket implements Stack.RecvInboundPacket.
func (*GVisorStack) SetWriteNotify ¶
func (g *GVisorStack) SetWriteNotify(notifier func(buf []byte))
SetWriteNotify implements Stack.SetWriteNotify.
func (*GVisorStack) Socket ¶
func (g *GVisorStack) Socket(ctx context.Context, network string, family, sotype int, laddr, raddr net.Addr) (c interface{}, err error)
Socket implements Stack.Socket.
func (*GVisorStack) WriteOutboundPacket ¶
func (g *GVisorStack) WriteOutboundPacket(buf []byte) (int, error)
WriteOutboundPacket implements Stack.WriteOutboundPacket.
type Interface ¶
type Interface struct {
// Stack represents a [Stack] instance.
Stack Stack
// HandleStackErr defines an optional function to handle [Stack]
// errors.
HandleStackErr func(err error, tx bool)
// NetworkDevice represents a [NetworkDevice] instance.
NetworkDevice NetworkDevice
}
Interface bridges a Stack and a NetworkDevice, driving packet I/O between them.
func (*Interface) Init ¶
Init initializes an Interface bridging its Stack and NetworkDevice.
If Stack is not set a default implementation is initialized, the stack is configured with the CIDR address and hardware (MAC) address.
If NetworkDevice is not set the Stack is left without a transmission callback and Interface.Start has no effect.
The gateway may or may not be provided, an empty MAC will result in a random Stack.HardwareAddress.
func (*Interface) Start ¶
Start begins processing of incoming packets, the function receives packets through NetworkDevice.Receive and handles them through Stack.RecvInboundPacket, it should never return.
type LnetoConfig ¶
type LnetoConfig struct {
// Hostname specifies the hostname to use for DHCP. Optional.
Hostname string
// MaxActiveTCPPorts is a heap-memory guardrail to limit number of simultaneous open TCP ports.
MaxActiveTCPPorts uint16
// MaxActiveUDPPorts is the same guardrail for UDP. It must be non-zero for
// anything UDP-based to work at all: with no slots every socket fails with
// [lneto.ErrExhausted]. That silently takes out DNS resolution and SNTP,
// and a node whose clock stays at the epoch cannot verify any certificate —
// so plain-looking HTTPS failures ("certificate is not yet valid") trace
// back to this number. A port costs on the order of 64 bytes.
MaxActiveUDPPorts uint16
// MaxListenerConns limits the amount of open [net.Listener] connections that can be established
// in simultaneous. Each newly allocated listener conn consumes 2*TCPBufferSize*MaxListenerConns so
// this can have drastic memory consumption impact.
MaxListenerConns uint16
// determine size of each TCP rx/tx ring buffers.
TCPBufferSize int
// BackoffStack sets the time between protocol checks for completion like DHCP, NTP, DNS etc. via the blocking APIs.
// BackoffStack can use a channel driven approach behind the scenes
// and return [lneto.BackoffFlagNop] to signal backoff yield is
// implemented by the callback and that no sleep should be performed.
BackoffStack lneto.BackoffStrategy
// NewBackoffTCP returns a per-connection backoff strategy for TCP read/write retries.
// If nil, a short-exponential default is used (or [lneto.BackoffFlagGosched] on GOMAXPROCS=1).
NewBackoffTCP func() lneto.BackoffStrategy
// TCPQueueSize sets the number of packets that can be sent out and not be acknowledged before halting new packet tx.
TCPQueueSize int
// GatewayHardwareAddr statically sets the gateway MAC. When zero the
// gateway is resolved over ARP at Configure time. Networks with a
// deterministic address plan know it up front; setting it skips the
// resolve entirely.
GatewayHardwareAddr [6]byte
}
LnetoConfig provides configuration options to better optimize the LnetoStack.
func DefaultLnetoStackConfig ¶
func DefaultLnetoStackConfig() *LnetoConfig
DefaultLnetoStackConfig returns an LnetoConfig ready for use with NewLnetoStack with sane configuration parameters.
type LnetoStack ¶
type LnetoStack struct {
// contains filtered or unexported fields
}
LnetoStack implements Stack with the lneto networking package.
func NewLnetoStack ¶
func NewLnetoStack(cfg *LnetoConfig) *LnetoStack
NewLnetoStack returns a stack using the Lneto userspace networking library.
func (*LnetoStack) Configure ¶
func (ls *LnetoStack) Configure(mac net.HardwareAddr, ip netip.Prefix, gw netip.Addr) error
Configure sets the MAC address, IP prefix and gateway. Gateway may be invalid.
func (*LnetoStack) EnableICMP ¶
func (ls *LnetoStack) EnableICMP() error
EnableICMP registers an ICMP handler on the stack.
func (*LnetoStack) HardwareAddress ¶
func (ls *LnetoStack) HardwareAddress() (net.HardwareAddr, error)
HardwareAddress returns the MAC address of the NIC.
func (*LnetoStack) RecvInboundPacket ¶
func (ls *LnetoStack) RecvInboundPacket(buf []byte) error
RecvInboundPacket delivers an inbound packet to the stack.
func (*LnetoStack) SeedNeighbor ¶
func (ls *LnetoStack) SeedNeighbor(addr netip.Addr, mac [6]byte) error
SeedNeighbor pre-populates the neighbor table with a static IP→MAC mapping; see xnet.StackAsync.SeedNeighbor. Requires a Configure'd stack.
func (*LnetoStack) SetWriteNotify ¶
func (ls *LnetoStack) SetWriteNotify(cb func(buf []byte))
SetWriteNotify registers a callback invoked when outbound data is ready.
func (*LnetoStack) Socket ¶
func (ls *LnetoStack) Socket(ctx context.Context, network string, family, sotype int, laddr, raddr net.Addr) (c interface{}, err error)
Socket creates a network socket bound to laddr and connected to raddr.
func (*LnetoStack) WriteOutboundPacket ¶
func (ls *LnetoStack) WriteOutboundPacket(buf []byte) (int, error)
WriteOutboundPacket dequeues one outbound packet into buf, returning bytes written.
type NetworkDevice ¶
type NetworkDevice interface {
// Receive receives a single Ethernet frame from a network adapter.
Receive(buf []byte) (n int, err error)
// Transmit transmits a single Ethernet frame to a network adapter.
Transmit(buf []byte) (err error)
}
NetworkDevice represents a generic network device interface capable of receiving and transmitting raw Ethernet frames.
type Stack ¶
type Stack interface {
// Configure sets the NIC ID, MAC address, IP prefix and gateway.
// Gateway may be invalid.
Configure(mac net.HardwareAddr, ip netip.Prefix, gw netip.Addr) error
// HardwareAddress returns the MAC address of the NIC.
HardwareAddress() (net.HardwareAddr, error)
// EnableICMP registers an ICMP handler on the stack.
EnableICMP() error
// Socket creates a network socket bound to laddr and connected to raddr.
Socket(ctx context.Context, network string, family, sotype int, laddr, raddr net.Addr) (c interface{}, err error)
// SetWriteNotify registers a callback invoked when outbound data is ready.
// The argument buffer if present can be used by the callback to fulfill data exchange.
SetWriteNotify(cb func(auxbuf []byte))
// WriteOutboundPacket dequeues one outbound packet into buf, returning bytes written.
WriteOutboundPacket(buf []byte) (int, error)
// RecvInboundPacket delivers an inbound packet to the stack.
RecvInboundPacket(buf []byte) error
}
Stack is the interface for a network stack implementation. It manages a single NIC and provides socket-level networking.