Documentation
¶
Overview ¶
Package shaper is agentlab's bandwidth shaper (docs/bandwidth.md): it lets the registry mirror's downloads take what the person's uplink leaves idle and give it back within seconds. One CAKE qdisc on the host side of the mirror's veth caps everything the mirror downloads from gsoci, and a controller sets its bandwidth every tick from the uplink's latency: while the latency to a few public reflectors stays at its baseline the mirror may take more, when it rises (the link's queue is filling, whoever fills it) the mirror yields.
Index ¶
- Constants
- Variables
- func Cleanup(pid int, logger *log.Logger) error
- func FetchBulk(ctx context.Context, w io.Writer, from string, streams int, start time.Time, ...) error
- func FormatRate(bps float64) string
- func ParseRate(s string) (float64, error)
- func Proof(ctx context.Context, opts ProofOptions) error
- func ProofInNamespace(ctx context.Context, opts ProofOptions) error
- func RateArg(bps float64) string
- func Run(ctx context.Context, cfg Config, logger *log.Logger) error
- func ServeBulk(ctx context.Context, listen string) error
- func WriteMode(dir string, m Mode) error
- type Config
- type Mode
- type ProofOptions
- type ReflectorState
- type Sample
- type State
Constants ¶
const ( ModeAuto = "auto" ModePause = "pause" ModeOff = "off" )
The modes a person sets for the machine (`agentlab shaper auto|max|pause| off`): one shaper serves every lab, so the override lives with it, in the mode file of its state directory, not in a lab's agentlab.yaml.
const ( StateFile = "state.json" ModeFile = "mode" )
The shaper's state directory: the shaper publishes what it does there (StateFile), and `agentlab shaper auto|max|pause|off` leaves the person's mode there (ModeFile). On the host it is agentlab's state folder's `shaper/`, mounted into the shaper's container.
const StaleAfter = 10 * time.Second
StaleAfter is how old the published state may be before a reader calls the shaper gone: it publishes every second.
Variables ¶
var DefaultReflectors = []netip.Addr{ netip.MustParseAddr("1.1.1.1"), netip.MustParseAddr("9.9.9.9"), netip.MustParseAddr("8.8.8.8"), netip.MustParseAddr("208.67.222.222"), }
DefaultReflectors are four public resolvers on separate networks.
var ErrProofFailed = errors.New("the namespace proof failed")
ErrProofFailed is a proof that ran and measured a criterion missed: the run said which.
Functions ¶
func Cleanup ¶
Cleanup takes CAKE off the veth of the mirror whose process is pid: what a shaper that was killed left behind.
func FetchBulk ¶
func FetchBulk(ctx context.Context, w io.Writer, from string, streams int, start time.Time, duration time.Duration) error
FetchBulk opens streams connections to from at start, reads from them for the given duration and writes one Sample per second as a JSON line.
func FormatRate ¶
FormatRate prints a rate for people; 0 means unlimited.
func ParseRate ¶
ParseRate reads a rate in bits per second: a number with an optional unit bit, kbit, mbit or gbit (k, m and g for short), case-insensitive. Byte units are refused on purpose: tc's "mbps" means megabytes.
func Proof ¶
func Proof(ctx context.Context, opts ProofOptions) error
Proof runs the namespace proof: it starts this agentlab again as root of a new user namespace with network and mount namespaces of its own, which builds the topology and runs it there.
func ProofInNamespace ¶
func ProofInNamespace(ctx context.Context, opts ProofOptions) error
ProofInNamespace is the proof inside its namespaces.
func RateArg ¶
RateArg prints a rate the way ParseRate reads it, in the largest unit that keeps it whole: 5mbit, 1500kbit.
func Run ¶
Run is the shaper: it finds the mirror's veth, puts CAKE on it and sets its bandwidth every tick until ctx ends, then takes CAKE off again — the veth back to what docker made it, the mirror unlimited.
Types ¶
type Config ¶
type Config struct {
// MirrorPID is a process in the mirror's network namespace: PID 1 of
// the mirror's PID namespace, which the shaper's container joins.
MirrorPID int
// Reflectors are the IPv4 hosts the latency is measured against.
Reflectors []netip.Addr
// StateDir is where the shaper publishes its state and reads the
// person's mode; "" publishes nothing.
StateDir string
// Interval is the control tick and the probe interval; Target the
// queue above the baseline at which the mirror backs off.
Interval, Target time.Duration
// Min and Max bound auto's cap, Start is where it starts on a network,
// Pause what `agentlab shaper pause` holds the mirror at; bits per
// second.
Min, Max, Start, Pause float64
}
Config is the shaper's configuration, from `agentlab shaper run`'s flags.
type Mode ¶
type Mode struct {
// Kind is ModeAuto, ModePause or ModeOff.
Kind string
// Max is auto's cap below the shaper's own ceiling; 0 is none.
Max float64
}
Mode is what the person asked for.
type ProofOptions ¶
type ProofOptions struct {
// Binary is this agentlab: the shaper and the traffic run it.
Binary string
// Baseline also runs the topology without the shaper, for comparison.
Baseline bool
// Out is where the run reports.
Out io.Writer
}
ProofOptions is one run of the proof.
type ReflectorState ¶
type ReflectorState struct {
Addr string `json:"addr"`
BaseMs float64 `json:"baseMs"` // 0 = no baseline on this network yet
LastMs float64 `json:"lastMs"` // 0 = no answer yet
Lost int `json:"lost"` // probes lost in a row
}
ReflectorState is one latency reflector.
type Sample ¶
Sample is one second of a fetch: the second since the proof's start and the bits per second received in it.
type State ¶
type State struct {
// Mode is the person's mode (auto, auto <max>, pause, off).
Mode string `json:"mode"`
// Device is the mirror's host-side veth the shaper works on; "" while it
// finds none.
Device string `json:"device,omitempty"`
// Shaping is whether CAKE is in place on Device.
Shaping bool `json:"shaping"`
// CapBps is the mirror's cap in bits per second, 0 unlimited.
CapBps float64 `json:"capBps"`
// MirrorBps is what the mirror downloaded over the last second.
MirrorBps float64 `json:"mirrorBps"`
// DelayMs is the link's queue as the latency probes see it, over the
// lowest baseline; DelayKnown false while no probe answered.
DelayMs float64 `json:"delayMs"`
DelayKnown bool `json:"delayKnown"`
// TargetMs is the queue at which the mirror backs off.
TargetMs float64 `json:"targetMs"`
// Reason is why the cap is where it is.
Reason string `json:"reason"`
// Network is the host's network (the default route), whose change
// starts the baselines over.
Network string `json:"network"`
Reflectors []ReflectorState `json:"reflectors"`
// Problem is what keeps the shaper from shaping, "" when nothing does.
Problem string `json:"problem,omitempty"`
Updated time.Time `json:"updated"`
}
State is what the shaper publishes.