Skip to content

Embed counter snapshots in bf_rule and bf_chain#527

Merged
qdeslandes merged 2 commits intofacebook:mainfrom
qdeslandes:counters_in_rule
May 9, 2026
Merged

Embed counter snapshots in bf_rule and bf_chain#527
qdeslandes merged 2 commits intofacebook:mainfrom
qdeslandes:counters_in_rule

Conversation

@qdeslandes
Copy link
Copy Markdown
Contributor

Counter values were previously delivered alongside chains as a parallel bf_list parameter on bf_chain_get / bf_ruleset_get, requiring every consumer to walk two lists in lockstep and to re-derive which slot belonged to which rule. This series moves the snapshots into the data model and refreshes them on the read path.

@meta-cla meta-cla Bot added the cla signed label May 8, 2026
@qdeslandes
Copy link
Copy Markdown
Contributor Author

@claude review

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 8, 2026

Claude review of PR #527 (d6f88ab)

Must fix

  • Typo "still" → "steal"src/libbpfilter/cli.c:301 — Comment reads "we can still the chain" but should say "steal"

Suggestions

  • Misleading error key in legacy fallbacksrc/libbpfilter/rule.c:96 — Error reports "bf_rule.has_counters" but the key that failed is the old "counters"
  • Dead code: bf_cgen_get_counters()src/libbpfilter/cgen/cgen.c:574 — No callers remain after this series; consider removing it along with its declaration at cgen.h:264
  • Unrelated bugfix mixed into PRsrc/libbpfilter/chain.c:300 — Adding _clean_bf_list_ to rules/sets in bf_chain_new_from_pack fixes a pre-existing leak but is orthogonal to counter embedding; consider a separate commit

Nits

  • %lu vs PRIu64 inconsistencysrc/libbpfilter/rule.c:203 — Uses %lu for uint64_t counters while line 190 uses PRIx64 for the uint64_t mark field
  • Missing Doxygen on has_counterssrc/libbpfilter/include/bpfilter/rule.h:67 — Bitfield lacks documentation while surrounding fields have it

Workflow run

Comment thread src/libbpfilter/cli.c Outdated
Comment thread src/libbpfilter/rule.c Outdated
Comment thread src/libbpfilter/cgen/cgen.c
@qdeslandes
Copy link
Copy Markdown
Contributor Author

@claude review

@qdeslandes qdeslandes changed the title Embed counter snapshots in bf_rule and `bf_chain Embed counter snapshots in bf_rule and bf_chain May 9, 2026
Comment thread src/libbpfilter/chain.c
Comment thread src/libbpfilter/rule.c
Comment thread src/libbpfilter/include/bpfilter/rule.h
qdeslandes added 2 commits May 9, 2026 11:39
bf_rule doesn't need a full boolean to flag whether counters are defined
or not, use a 1 bit field instead. Follow up changes will apply this
pattern to bf_rule.mark and bf_rule.disabled.
Counter values were previously delivered alongside chains as a parallel
bf_list parameter on bf_chain_get/bf_ruleset_get, requiring consumers to
walk two lists in lockstep. Embed the snapshots directly:

- struct bf_counter counters in bf_rule (valid when has_counters == 1)
- struct bf_counter policy_counters and error_counters in bf_chain

Add bf_cgen_load_counters() to refresh those fields from the BPF
counters map, and call it from bf_chain_get and bf_ruleset_get on the
read path. The pinned BPF map remains the source of truth: snapshots
are populated per-invocation, never cached, and intentionally not
serialized.

Drop the counters parameter from bf_chain_get and bf_ruleset_get;
update bfcli, the fuzz stub, the test harness, and unit tests
accordingly.

bf_rule_new_from_pack accepts both the new "has_counters" key and the
legacy "counters" key, so chains pinned by an earlier build remain
readable.
@qdeslandes qdeslandes enabled auto-merge (rebase) May 9, 2026 09:42
@qdeslandes qdeslandes merged commit 29d1a72 into facebook:main May 9, 2026
34 checks passed
@qdeslandes qdeslandes deleted the counters_in_rule branch May 9, 2026 09:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant