Skip to content

Latest commit

 

History

History
146 lines (122 loc) · 8.92 KB

develop.md

File metadata and controls

146 lines (122 loc) · 8.92 KB

Developing iohr-capture

How the capture crates are built and tested. Installing and running it: install.md. Why it is shaped this way: ADR 0002.

The crates

Crate What Toolchain
crates/iohr-capture-ebpf the eBPF programs (no_std, aya-ebpf), MIT OR GPL-2.0, not a workspace member, its own Cargo.lock and deny.toml the pinned nightly in its rust-toolchain.toml, bpf-linker
crates/iohr-capture-common no_std types shared by kernel and user space; unsafe impl aya::Pod behind the user feature stable
crates/iohr-capture the program: run, stats, lookup, pcap, dissect, doctor, cleanup; layers 1, 2, 4, 5 (design-phase1.md), 3 and 7 (design-phase2.md) stable; its build.rs builds the eBPF crate

crates/iohr-capture/build.rs calls aya-build, which runs cargo build --target bpfel-unknown-none -Z build-std=core for the eBPF crate with the nightly named in crates/iohr-capture-ebpf/rust-toolchain.toml and embeds the object in the binary. Nothing else in the workspace uses nightly.

Setup

mise install          # stable and the pinned nightly (rust-src, clippy, rustfmt), bpf-linker
mise run ci           # includes capture:ebpf-check (fmt, clippy for the BPF target, cargo-deny)
cargo build -p iohr-capture

mise.toml pins the nightly next to stable and bpf-linker (whose LLVM must be at least as new as the nightly's). mise run capture:toolchain-check fails when mise.toml and the eBPF crate's rust-toolchain.toml name different nightlies. To move to a newer nightly, change both, run mise install and mise run ci, and check the bpf-linker release notes for the LLVM version.

cargo build without mise needs rustup, the nightly with rust-src, and bpf-linker on PATH.

Tests

  • cargo nextest run -p iohr-capture -p iohr-capture-common: unit tests, including every doctor probe against fixtures. No privileges, no eBPF.
  • mise run capture:e2e: the real thing, in throwaway VMs.

Never load the programs on your own machine or a server to try them out. Loading and attaching need root, change the interface's TC hooks and, on a shared or production host, affect everyone. The VM test exists so nobody has to.

The VM test (mise run capture:e2e)

tools/capture-e2e.sh builds a static iohr-capture and boots one VM per kernel with virtme-ng (vng) on QEMU with KVM. The VM sees this machine's files read-only; inside it, tools/capture_e2e_guest.py runs as root and refuses to run anywhere but a virtme-ng guest. For each attach mode the kernel has (netlink always, TCX from 6.6) it:

  1. makes a veth pair, one end in a network namespace, IPv6 off and static neighbour entries so there is no background traffic;
  2. starts servers on the root namespace's end: UDP and TCP counters, an HTTP/1 server in its own cgroup (/sys/fs/cgroup/iohr-e2e-web), openssl s_server, a DNS responder, a minimal HTTP/2 cleartext server, a listener that never accepts and one that holds a connection;
  3. starts iohr-capture run on that end, its sockets in a scratch directory;
  4. sends from the namespace: 1000 UDP datagrams and 100 TCP connections (64 bytes each), 20 HTTP/1 requests (curl, with a Host), 10 TLS handshakes (openssl s_client with a known SNI), 15 DNS queries (dig, one known name), 5 gRPC calls and 5 h2c requests (curl --http2-prior-knowledge), 5 connections to a closed port, 8 into the full listen queue;
  5. asks the aggregates socket (stats --tables --json), checks its access rules (a group member may read, another user with the group only as a supplementary group is refused by SO_PEERCRED, the agent's user gets counts but never the tables), the socket modes, the control socket and the parser process's capabilities (CapEff and CapPrm zero) and user (not root, though run was started as root), then stops capture and asserts.

What is asserted: capture's ingress and egress packet counts equal the interface's own rx and tx packet counters over the same window, exactly. Both count socket buffers, and with small payloads on veth (no GRO without XDP, nothing for TSO to merge) one socket buffer is one packet; on real NICs GRO can merge packets, which is why the report says packet_unit: "skb". Bytes may differ from the interface counters only by the Ethernet header (0 or 14 bytes per packet). Per layer: resets on the closed port (1); exact HTTP/1 request and response counts, Host and path template, TLS hellos and SNI, DNS queries and name, h2c connections, gRPC calls and method, and that the query strings sent never appear (2); the HTTP/1 flows owned by the cgroup and python3 (4); an RTT sample from the held connection and the listen overflow (5); no copy drops at this rate; the capabilities left after attaching are the expected ones for the kernel and mode; nothing is left on the interface and the sockets are gone afterwards; cleanup and the next start remove the filters of a SIGKILLed netlink run; doctor exits 0 as root and 1 without capabilities.

Then three floods of 40000 new UDP flows: with a 4 KiB ring buffer and a 512-flow table (ring buffer full and flows evicted must be counted), with a 100/s copy rate (rate limited must be counted), and with a 16 MiB ring and no rate limit (the reader must keep up: every copied record read, and 5 DNS queries sent after the flood counted). In all the totals stay exact and both processes stay under 64 MiB.

Phase 2, once per kernel: an HTTP/1.1 server with a fixed delay per route (0, 35, 150 and 700 ms; one keep-alive connection per route) and an h2c/gRPC server (150 ms; 120 ms with grpc-status: 0 in the trailers; 700 ms with a trailers-only grpc-status: 5), each in its own cgroup. The version 2 lookup, asked as the agent's user, must put every request in the histogram bucket its delay belongs to, with the right status classes and gRPC codes, find nothing for an unknown route or owner, and carry no name. With --packets, root asks for a last pcap with a filter and a copy for another user (0600, owned by that user), that user runs iohr-capture dissect (the host's tshark, which must be installed: it is reached through the read-only host filesystem) and must see the HTTP requests, dissect as root is refused, a next pcap must be readable by tshark, the companion's own user and another user are refused by the control socket, and the companion's file must be gone after its 6 s retention while the copy stays. A fourth flood with packets on checks the packet buffer stays at its 4 MiB.

Last, the shipped unit: packaging/systemd/iohr-capture.service is started by a real systemd running as PID 1 of a new PID namespace inside the VM (the guest's /etc and /usr are throwaway overlays, so users, groups, the binary and the unit are installed there), on lo. It checks the unit starts without failures or restarts under its sandbox, the runtime directory is 2750 iohr-capture-read, the socket 0660 iohr-capture-read, a member reads the tables, the agent's user reads counts only, anyone else is kept out, the parser has no capabilities and the privileged process kept exactly what the kernel needs. Then it stops the service the way systemd does (SIGTERM to the main process): the unit must end with "Deactivated successfully" (exit 0, its ExecStopPost cleanup ran under the sandbox, no restart) and leave no filter on lo. (On lo every packet passes egress and ingress, so 3 requests count 6.)

Results: dist/capture-e2e/<kernel>.json and .log.

What it needs

  • vng (virtme-ng 1.32 or newer) and qemu-system-x86_64: sudo apt install virtme-ng qemu-system-x86 (Ubuntu 24.04+), or pipx install virtme-ng plus your distribution's QEMU.
  • Read and write access to /dev/kvm. Without it the VMs are too slow to be useful and the script stops. Join the kvm group (sudo usermod -aG kvm "$USER", then log in again), or for the current login only: sudo setfacl -m u:"$USER":rw /dev/kvm.
  • tshark on this machine (sudo apt install tshark, and answer "No" to letting non-root users capture): the guest uses this machine's files, and the phase 2 checks dissect with it. It only ever reads files there.
  • Network access the first time: vng --run v<version> downloads Ubuntu mainline kernel builds (image and modules) from kernel.ubuntu.com and caches them in ~/.cache/virtme-ng.

The kernel matrix

KERNELS in tools/capture-e2e.sh (default v5.15.222 v6.6.158 v6.12.112): 5.15 is the oldest LTS that most fleets still run and exercises netlink attach and the kept CAP_BPF; 6.6 is the first with TCX; 6.12 is a current LTS. To add one, pick a version from https://kernel.ubuntu.com/mainline/, check it boots with KERNELS=v6.x.y mise run capture:e2e, and add it to the default list. One run:

KERNELS=v6.6.158 mise run capture:e2e