moat docs ยท language 1

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
  • load compiles the rules, attaches them through a pinned XDP link and fills sets that come from files. --dry-run only runs the kernel verifier. --audit starts in audit mode.
  • reload swaps 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, so set sync and 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 enable installs runs into, load and reload attach afresh and keep the sets: "the previous program had come off eth0 (the interface was removed); attached afresh, keeping sets".
  • unload detaches the program and removes its state. With --keep-sets the sets stay for the next load.
  • audit on makes rules that drop count the packet and let it pass; stats then shows them as "would drop".
  • enable copies 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 until systemctl reset-failed. The unit runs the moat binary enable was run with, as root, so enable refuses a binary that a user other than root could replace: install moat under a root-owned path such as /usr/bin.
  • With flows, load also attaches the egress program that learns connections, reload replaces it and keeps the table (unless its size changed), and unload removes it. status shows how many connections the table holds, confirmed and opening, and whether it is still warming up.
  • status names 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 status lists moat, and xdp-loader load and unload work on a dispatcher moat built, and moat on one they built.
  • reload keeps the priority and chain, unload takes out only moat (and the dispatcher, when nothing else is left in it), and enable --shared records 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 3 where libxdp 1.6 or newer manages the other programs.
  • LIBXDP_BPFFS, if set for one tool, must be set the same for the others.
  • status lists the dispatcher's programs in run order, and status --json gives attach (link or dispatcher) and dispatcher.
  • 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.