The Verifier Is the API
iptables on the nodes had turned into a long list. Every new pod appended rules, kube-proxy walked them, and the CPU spike lined up with deploys rather than with traffic. Cilium stopped sounding like a conference talk and started sounding like the graph we already had. A packet should hit a map, not a list that grows with the number of pods.
What safe means here
The verifier is not a linter you silence with a flag. If it rejects the program, the program does not load. Three rules covered every rejection we hit:
- The program ends. A loop has to be bounded. On kernels that still refuse loops, unroll a fixed number of times.
- You do not chase arbitrary pointers. You look up a map, you read the packet only inside the length the context gives you, you call a helper.
- The map has a type and a ceiling. An LRU hash of 10,240 entries is a decision you can explain at review. An unbounded scratch pad is not a program.
This is the counter we load when we want to know which destinations a node is actually forwarding. It does not drop anything. Counting is not a firewall. The object also carries a GPL license line. Leave that line out and the loader refuses the object. That is policy, and it fails closed.
struct key {
__u32 daddr;
};
struct {
__uint(type, BPF_MAP_TYPE_LRU_HASH);
__uint(max_entries, 10240);
__type(key, struct key);
__type(value, __u64);
} pass_counts SEC(".maps");
SEC("xdp")
int count_passes(struct xdp_md *ctx) {
void *data = (void *)(long)ctx->data;
void *end = (void *)(long)ctx->data_end;
struct ethhdr *eth = data;
if ((void *)(eth + 1) > end)
return XDP_PASS;
if (eth->h_proto != htons(ETH_P_IP))
return XDP_PASS;
struct iphdr *ip = (void *)(eth + 1);
if ((void *)(ip + 1) > end)
return XDP_PASS;
struct key k = {};
k.daddr = ip->daddr;
__u64 zero = 0;
__u64 *v = bpf_map_lookup_elem(&pass_counts, &k);
if (!v) {
bpf_map_update_elem(&pass_counts, &k, &zero, BPF_NOEXIST);
v = bpf_map_lookup_elem(&pass_counts, &k);
}
if (v)
__sync_fetch_and_add(v, 1);
return XDP_PASS;
}
The two length checks are the program. Delete them and the verifier says no, and it is right. A frame shorter than a header is a normal afternoon on a busy node, not a textbook case. Headers for ethhdr, iphdr, and the map helpers sit above this fragment. The fragment is the part we kept getting wrong.
How it runs without becoming a second network
Build with CO-RE and BTF, so the object is not welded to the headers of the laptop that compiled it. Nodes drift. The object should load on the node you actually have.
Read the map from userspace every 10 seconds. bpf_printk on XDP is how you spend the CPU you were trying to get back. Ten seconds is coarse, and coarse is what a counter of forwards needs. If you need a per-packet trace, this is the wrong hook and the wrong tool.
Custom programs are for the one number Cilium does not already export. Policy and the CNI stay in Cilium. A second networking stack "just for this metric" is how you grow two places that can drop a packet and no agreement on which one did.
When the verifier rejects you, delete code until the object loads, then add one check back at a time. Arguing with the error string is slower than shortening the program. The list-to-map change is the performance story: a rule list grows with the pods, a map lookup does not. The verifier's stubbornness is the price of being allowed to run in the kernel at all.