offload

package
v2.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package offload implements the kernel offload of relayed UDP flows. The open-source build ships only the null engine; New dispatches through the engineConstructor seam.

An offloaded flow is a pair of sides, each a 4-tuple and a channel: a packet arriving on one side leaves on the other, its addresses and ports rewritten and the ChannelData framing changed from the channel of its side to the channel of the other, 0 meaning raw UDP. So one shape covers the TURN channels (ChannelData towards the client, raw towards the peer), the L4 flows (raw both ways, or ChannelData towards an upstream TURN server), the SRT streams and the RTP media (raw both ways). A side's punt rules, and a STUN header on any side, send a packet to user space instead, so the server keeps seeing what it must (a handshake, a shutdown, a BYE). TLS and DTLS encrypt the payload and streams carry no datagrams: those flows report no wire and stay in user space.

Index

Constants

View Source
const (
	// FlagListener marks a stat as belonging to a listener, otherwise a cluster.
	FlagListener uint8 = 1 << 0
	// FlagDirIn marks a stat as inbound (DIR_IN), otherwise outbound (DIR_OUT).
	FlagDirIn uint8 = 1 << 1
)

stats flags.

View Source
const (
	ProtocolUDP = "UDP"
	ProtocolTCP = "TCP"
)

Protocol values of a Connection: the transport as the TURN five-tuple names it, which is what the engines match on.

View Source
const MaxPuntRules = 2

MaxPuntRules is the number of punt rules a side carries at most.

View Source
const NullName = "null"

NullName is the name of the null engine, the one that runs when no kernel engine does.

View Source
const OffloadStatsUpdateInterval = 2 * time.Second

OffloadStatsUpdateInterval is how often the reporter samples the engine and updates telemetry.

Variables

View Source
var ErrOffloadLimit = errors.New("offload limit reached")

ErrOffloadLimit is the error of an Upsert beyond the offload limit of the license: the flow stays in user space.

Functions

func IsDirIn

func IsDirIn(flag uint8) bool

IsDirIn reports whether the stat flags mark inbound traffic.

func IsListener

func IsListener(flag uint8) bool

IsListener reports whether the stat flags mark a listener.

func NameHash

func NameHash(s string) uint16

NameHash hashes an object name into the 16-bit identifier used by the offload datapath.

Types

type Connection

type Connection struct {
	RemoteAddr net.Addr
	LocalAddr  net.Addr
	// Protocol is the transport, one of the Protocol constants above. The engines compare it
	// verbatim, so the spelling is part of the contract.
	Protocol  string
	ChannelID uint32
	Punt      []PuntRule
}

Connection is one side of an offloaded flow: a packet arriving on it comes from RemoteAddr to LocalAddr, framed as ChannelData of ChannelID, 0 meaning raw UDP; a packet leaving on it goes from LocalAddr to RemoteAddr, framed the same way. A packet arriving that matches one of the side's punt rules goes to user space instead.

func (*Connection) String

func (c *Connection) String() string

type Deps

type Deps struct {
	Telemetry *telemetry.Telemetry
	License   licensecfg.ConfigManager
	Log       logging.LeveledLogger
}

Deps are the process-wide dependencies of the offload engine, set once at construction.

type Engine

type Engine interface {
	// Name returns the offload engine type.
	Name() string
	// Start pins the eBPF maps and attaches the program to the given interfaces, selecting the
	// mechanism from mode ("none"/"tc"/"xdp"/"auto"). Re-callable: a config change closes then
	// restarts the engine.
	Start(mode string, interfaces []string) error
	// Close detaches and unpins the eBPF maps.
	Close() error
	// Upsert offloads the flow between two sides, both directions, or rewrites it: a packet
	// arriving on a is sent out on b and the other way round. The listener and the cluster name
	// the stats of the two sides. Since eBPF maps are LRU, an offload can lose part of itself;
	// upsert repairs it but resets the Packets counter. An error leaves the flow in user space.
	Upsert(a, b Connection, listenerName, clusterName string) error
	// Remove removes an offloaded flow. Removing a flow that is not offloaded is not an error.
	Remove(a, b Connection) error
	// Stats returns the last cached offload statistics, keyed by object name-hash and direction.
	Stats() (StatMap, error)
	// Packets returns the packet counter of a flow, both directions, and whether the flow is
	// offloaded. The counter is monotone between upserts; Upsert resets it.
	Packets(a, b Connection) (uint64, bool)
	// Flows returns how many flows the engine holds and the offload limit it admits them up to:
	// 0 is unlimited, a negative value unknown.
	Flows() (live, limit int)
}

Engine provides a general interface for offloading techniques (e.g., XDP). The engine instance is a process-wide singleton (rt.OffloadEngine) with the server's lifetime; Start pins the eBPF maps and attaches to interfaces, Close unpins them. An offload-config change is applied by the Offload object as a Close()+Start() (re-pin), since the engine has no lighter in-place path.

func New

func New(deps Deps) Engine

New builds the offload engine. The engine is a process-wide singleton with the lifetime of the server: it is created once at startup, Start pins the eBPF maps, and Close unpins them on shutdown.

type NameIndex

type NameIndex func() (listeners, clusters map[uint16]string)

NameIndex returns the current offload name-hash to object-name maps for listeners and clusters. It is injected so the reporter need not depend on the runtime registry.

type NullEngine

type NullEngine struct{}

NullEngine is a no-op offload engine.

func NewNullEngine

func NewNullEngine() *NullEngine

NewNullEngine creates a null offload engine.

func (*NullEngine) Close

func (o *NullEngine) Close() error

Close stops the offload engine.

func (*NullEngine) Flows

func (o *NullEngine) Flows() (live, limit int)

Flows reports no flows and no known limit: the null engine offloads nothing.

func (*NullEngine) Name

func (o *NullEngine) Name() string

Name returns the offload engine type.

func (*NullEngine) Packets

func (o *NullEngine) Packets(_, _ Connection) (uint64, bool)

Packets reports that the null engine knows no connection: it offloads nothing, so every flow stays visible to its own pumps and needs no kernel-side liveness signal.

func (*NullEngine) Remove

func (o *NullEngine) Remove(_, _ Connection) error

Remove removes an offloaded connection.

func (*NullEngine) Start

func (o *NullEngine) Start(_ string, _ []string) error

Start initializes the offload engine.

func (*NullEngine) Stats

func (o *NullEngine) Stats() (StatMap, error)

Stats returns the last cached offload statistics.

func (*NullEngine) Upsert

func (o *NullEngine) Upsert(_, _ Connection, _, _ string) error

Upsert establishes a new offloaded connection on the engine or modifies an existing one.

type PuntRule

type PuntRule struct {
	Offset      uint16
	Mask, Value uint32
}

PuntRule matches a packet by a 32-bit big-endian word of its UDP payload at Offset: the packet matches when the word masked with Mask equals Value.

func (PuntRule) Match

func (r PuntRule) Match(payload []byte) bool

Match reports whether the rule matches a UDP payload; a payload too short for the word never matches. The engines implement the same match in the kernel.

type StatInfo

type StatInfo struct {
	Pkts          uint64
	Bytes         uint64
	TimestampLast uint64
}

StatInfo holds a single offload statistics sample.

type StatKey

type StatKey struct {
	NameHash uint16
	Flags    uint8
}

StatKey identifies an offload statistic by object name-hash and flags.

type StatMap

type StatMap = map[StatKey]StatInfo

StatMap maps stat keys to their last cached samples.

type StatsReporter

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

StatsReporter periodically samples an offload Engine and backfills the offloaded packet and byte totals into the shared listener/cluster telemetry counters. Offloaded traffic bypasses the userspace dataplane, so without this the packet/byte counters would undercount it.

func NewStatsReporter

func NewStatsReporter(engine Engine, tel *telemetry.Telemetry, nameIndex NameIndex, log logging.LeveledLogger) *StatsReporter

NewStatsReporter creates an offload telemetry reporter for the given engine.

func (*StatsReporter) Close

func (r *StatsReporter) Close()

Close stops the reporter loop and waits for it to finish.

func (*StatsReporter) Start

func (r *StatsReporter) Start()

Start launches the reporter loop in its own goroutine. It runs until Close is called.

Jump to

Keyboard shortcuts

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