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
- Variables
- func IsDirIn(flag uint8) bool
- func IsListener(flag uint8) bool
- func NameHash(s string) uint16
- type Connection
- type Deps
- type Engine
- type NameIndex
- type NullEngine
- func (o *NullEngine) Close() error
- func (o *NullEngine) Flows() (live, limit int)
- func (o *NullEngine) Name() string
- func (o *NullEngine) Packets(_, _ Connection) (uint64, bool)
- func (o *NullEngine) Remove(_, _ Connection) error
- func (o *NullEngine) Start(_ string, _ []string) error
- func (o *NullEngine) Stats() (StatMap, error)
- func (o *NullEngine) Upsert(_, _ Connection, _, _ string) error
- type PuntRule
- type StatInfo
- type StatKey
- type StatMap
- type StatsReporter
Constants ¶
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.
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.
const MaxPuntRules = 2
MaxPuntRules is the number of punt rules a side carries at most.
const NullName = "null"
NullName is the name of the null engine, the one that runs when no kernel engine does.
const OffloadStatsUpdateInterval = 2 * time.Second
OffloadStatsUpdateInterval is how often the reporter samples the engine and updates telemetry.
Variables ¶
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 IsListener ¶
IsListener reports whether the stat flags mark a listener.
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.
type NameIndex ¶
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 (*NullEngine) Flows ¶
func (o *NullEngine) Flows() (live, limit int)
Flows reports no flows and no known limit: the null engine offloads nothing.
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 ¶
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.
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.