Running Loading on an interface
Loading on an interface attach, swap and remove programs safely.
These commands need Linux and root. Run sudo moat doctor --iface eth0
first: it says whether the machine and the interface's driver are ready.
See Install.
$ moat load rules.moat --iface eth0 [--mode native|generic] [--audit] [--dry-run]
$ moat reload rules.moat --iface eth0 [--dry-run]
$ moat unload --iface eth0 [--keep-sets]
$ moat status [--iface eth0] [--json]
$ moat audit on|off --iface eth0
$ moat enable rules.moat --iface eth0
$ moat disable --iface eth0
loadcompiles the rules, attaches them through a pinned XDP link and fills sets that come from files.--dry-runonly runs the kernel verifier.--auditstarts in audit mode.reloadswaps the program in place: there is no moment without a program. Sets whose definition is unchanged keep their contents, and sets loaded from a file are made equal to the file again. A rule keeps its counters when its name and what it does stay the same; a rule that changed (its condition, its actions, or a constant it uses) starts again from zero, and so does a new one. Set entries are not part of a rule, soset syncand set files never reset a counter. The default keeps its counters while its verdict stays.- After the interface was removed and came back, which is what the unit
moat enableinstalls runs into,loadandreloadattach afresh and keep the sets: "the previous program had come off eth0 (the interface was removed); attached afresh, keeping sets". unloaddetaches the program and removes its state. With--keep-setsthe sets stay for the nextload.audit onmakes rules that drop count the packet and let it pass;statsthen shows them as "would drop".enablecopies the rules and their set files to/etc/moat/IFACE/and enables[email protected], which loads them whenever the interface appears, at boot or later. A load that fails is retried every 3 seconds, unless the rules have errors (exit 1). systemd starts the unit at most 10 times a minute; past that it stays failed untilsystemctl reset-failed. The unit runs the moat binaryenablewas run with, as root, soenablerefuses a binary that a user other than root could replace: install moat under a root-owned path such as/usr/bin.- With flows,
loadalso attaches the egress program that learns connections,reloadreplaces it and keeps the table (unless its size changed), andunloadremoves it.statusshows how many connections the table holds, confirmed and opening, and whether it is still warming up. statusnames any pinned map that has gone missing, for example "pinned map moat_counters in /sys/fs/bpf/moat/eth0 is missing; run moat unload to clean up, then moat load".
status --json and stats --json print lowercase keys (name,
packets, set_sizes...) that stay stable. status also gives the
sha256 of each set's live entries (set_sha256) and of the rules file a
boot loads (boot_sha256), so a control plane can check that a node
holds what it pushed.
stats, status, var list, set list and set show wait, up to 10
seconds, while a load, a reload or a set change runs, so they never mix
one program's rule names with another program's counters.
Sharing the interface with other XDP programs
$ sudo moat load rules.moat --iface eth0 --shared [--priority N] [--chain pass,drop,...] [--dispatcher-version 2|3]
An interface has one XDP hook. Without --shared moat takes it alone.
With --shared, moat runs as one program of a libxdp multiprog
dispatcher, the way xdp-loader and other libxdp users share the hook:
moat joins the dispatcher on the interface, or creates one, and the
programs run in order of their priority (--priority, default 50; lower
runs first). After a program, the next one runs only if it returned one
of its chain actions (--chain, default pass).
- moat follows libxdp's own protocol, so
xdp-loader statuslists moat, andxdp-loader loadandunloadwork on a dispatcher moat built, and moat on one they built. reloadkeeps the priority and chain,unloadtakes out only moat (and the dispatcher, when nothing else is left in it), andenable --sharedrecords them for boot, taking the running rules' own when not given.- moat keeps the version and mode of a dispatcher already on the
interface. One moat creates is version 2, what libxdp up to 1.5 uses;
give
--dispatcher-version 3where libxdp 1.6 or newer manages the other programs. LIBXDP_BPFFS, if set for one tool, must be set the same for the others.statuslists the dispatcher's programs in run order, andstatus --jsongivesattach(linkordispatcher) anddispatcher.- A dispatcher runs at most 10 programs, cannot carry programs bound to a device (so a shared moat does not ask the driver for VLAN tags), and is not loaded for multi-buffer (frags) packets.
Another XDP program on the interface
An interface runs one XDP program at a time. When another tool already
attached one, moat load stops and says so:
$ sudo moat load rules.moat --iface eth0
eth0 already runs an XDP program that moat did not attach; remove it with the tool that attached it, or with ip link set dev eth0 xdp off (moat doctor --iface eth0 shows it)
moat doctor --iface eth0 shows the same before loading, with the
program's name and id.
Where state lives
Pinned link and maps are in /sys/fs/bpf/moat/IFACE/, the manifest in
/var/lib/moat/IFACE/manifest.json. MOAT_BPFFS, MOAT_STATE,
MOAT_ETC and MOAT_SYSTEMD move these directories, MOAT_SYSTEMCTL
and MOAT_CLANG pick the programs run as systemctl and clang, and
LIBXDP_BPFFS is where shared mode looks for libxdp's pins. moat trusts
them because it runs as root: sudo's default env_reset drops them, so
do not add them to env_keep.
In another network namespace, enter it with nsenter --net=... rather than ip netns exec, which hides the BPF filesystem behind a fresh /sys.
Exit codes: 0 success, 1 errors in the rules (or a failed doctor
check), 2 wrong usage or state, including not running as root, 3 the
kernel verifier or clang refused the program. The verifier is the
usual cause of 3.