|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Build System |
| 6 | + |
| 7 | +This project uses [go-task](https://taskfile.dev) (`Taskfile.yml`) as the build orchestrator. |
| 8 | + |
| 9 | +```sh |
| 10 | +task build # fmt + compile (CGO_ENABLED=0, PGO, stripped) |
| 11 | +task build-debug # fmt + compile with race detector (CGO_ENABLED=1) |
| 12 | +task lint # fmt + golangci-lint |
| 13 | +task fmt # gci + gofumpt + betteralign (Go sources) |
| 14 | +task fmt-bpf # clang-format on bpf/*.c / bpf/*.h |
| 15 | +task generate # go generate (recompile eBPF C → Go; requires clang) |
| 16 | +task update # go get -u && go mod tidy |
| 17 | +``` |
| 18 | + |
| 19 | +Plain `go build` also works for the Go userland (pre-compiled eBPF objects are committed). |
| 20 | + |
| 21 | +## Architecture Overview |
| 22 | + |
| 23 | +pktstat-bpf is a Linux eBPF packet statistics tool. It has two layers: |
| 24 | + |
| 25 | +**eBPF layer (C, in `bpf/`):** |
| 26 | +- `bpf/counter.bpf.c` — packet/byte counting programs for TC (TCX ingress+egress), XDP (ingress only), KProbes (tcp/udp/icmp), and CGroup SKB hooks. |
| 27 | +- `bpf/cgroup.bpf.c` — raw tracepoint on `cgroup_mkdir` to push new CGroup path events to userspace via perf buffer. |
| 28 | + |
| 29 | +**Userland layer (Go, root package `main`):** |
| 30 | +- `gen.go` — `//go:generate` directives that invoke `bpf2go` to compile each `.bpf.c` for `amd64` and `arm64`, producing `counter_{amd64,arm64}_bpfel.{go,o}` and `cgroup_{amd64,arm64}_bpfel.{go,o}`. |
| 31 | +- `main.go` — program entry: loads both eBPF objects, resolves interface, selects capture mode (TC / XDP / KProbes / CGroup), runs TUI or CLI loop. |
| 32 | +- `probe.go` — `startTC`, `startXDP`, `startKProbes`, `startCgroup`, `startCGroupTrace`: attach eBPF programs to hooks. |
| 33 | +- `map.go` — reads `PktCount` eBPF LRU hash map; prefers `BatchLookup` (kernel ≥5.6), falls back to iterator. |
| 34 | +- `cgroup.go` — builds and maintains a `cgroup-id → path` cache by walking `/sys/fs/cgroup` and consuming perf events from `cgroup.bpf.c`. |
| 35 | +- `output.go` — `processMap`, sort functions (`bitrateSort`, etc.), `outputPlain`/`outputJSON`, bitrate formatting. |
| 36 | +- `tui.go` — rivo/tview TUI, refreshed on `--refresh` interval. |
| 37 | +- `flags.go` — flag parsing via `peterbourgon/ff/v4`; sets package-level pointers used throughout. |
| 38 | +- `types.go` — `statEntry` (per-flow stats struct) and `kprobeHook`. |
| 39 | +- `helpers.go` — `bytesToAddr`, `protoToString`, `findFirstEtherIface`. |
| 40 | +- `init.go` — version variables (`GitTag`, `GitCommit`, …) injected at link time. |
| 41 | + |
| 42 | +## Capture Modes |
| 43 | + |
| 44 | +Four mutually-exclusive modes selected at runtime: |
| 45 | + |
| 46 | +| Flag | Mode | Kernel req | |
| 47 | +|------|------|-----------| |
| 48 | +| *(default)* | TC (TCX ingress + egress) | ≥6.6 | |
| 49 | +| `--xdp` | XDP (ingress only, no egress stats) | ≥5.9 | |
| 50 | +| `--kprobes` | KProbes + PID/CGroup tracking | ≥4.10 + BTF | |
| 51 | +| `--cgroup <path>` | CGroup SKB + PID tracking | ≥4.10 + BTF | |
| 52 | + |
| 53 | +## Code Style & Tooling |
| 54 | + |
| 55 | +- **Formatter chain**: `gci` (import grouping) → `gofumpt` (strict gofmt) → `betteralign` (struct field alignment). Run `task fmt` before committing. |
| 56 | +- **Linter**: `golangci-lint` v2 with `default: all` minus a handful of disabled linters listed in `.golangci.yml`. Run `task lint`. |
| 57 | +- **No CGO** in production builds (`CGO_ENABLED=0`). CGO is only re-enabled for the race-detector build. |
| 58 | +- **eBPF code generation**: only needed when changing `bpf/*.c` or `bpf/*.h`. Requires `clang` and the arch-specific BTF headers under `contrib/`. Run `task generate` then commit the updated `*_bpfel.{go,o}` files. |
| 59 | +- The `contrib/` directory contains vendored kernel BTF/vmlinux headers split by architecture (`amd64/`, `arm64/`). |
0 commit comments