Skip to content

lib: chain: disable rules with incompatible same-layer matchers#471

Open
qdeslandes wants to merge 1 commit intofacebook:mainfrom
qdeslandes:disable_rules_incompatible_matchers
Open

lib: chain: disable rules with incompatible same-layer matchers#471
qdeslandes wants to merge 1 commit intofacebook:mainfrom
qdeslandes:disable_rules_incompatible_matchers

Conversation

@qdeslandes
Copy link
Contributor

A rule combining matchers on the same network layer but targeting different protocols (e.g. ip4.saddr + ip6.daddr, or tcp.sport + udp.dport) can never match a packet. Detect these during chain validation and disable the rule instead of generating dead BPF code.


ping -c 1 -W 0.1 ${NS_IP_ADDR}

stop_bpfilter

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude: Explicit stop_bpfilter is unnecessary here — the EXIT trap in e2e_test_util.sh already calls cleanup() which invokes stop_bpfilter(). None of the other rules/ e2e tests (action_order.sh, icmp_tc.sh, icmp_xdp.sh, log.sh, mark.sh, redirect.sh) call it explicitly. Consider removing for consistency.

Suggested change
stop_bpfilter


static void get_set_by_name(void **state)
{
_free_bf_chain_ struct bf_chain *chain = bft_chain_dummy(true);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude: The test covers L3/L4 conflicts and non-conflict cases well. Consider adding sub-cases for a couple of boundary inputs to _bf_rule_has_incompatible_matchers:

  1. Single matcher — a rule with only one matcher (e.g., just BF_MATCHER_IP4_SADDR) can never conflict; verifying it isn't disabled guards against loop logic regressions.
  2. Meta-layer matcher — matchers with BF_MATCHER_NO_LAYER (e.g., BF_MATCHER_META_IFACE) should be excluded from the conflict check. A rule mixing a meta matcher with an IPv4 matcher should not be flagged.

@github-actions
Copy link

github-actions bot commented Mar 12, 2026

Claude: review of facebook/bpfilter #471 (564fce9)

Suggestions

  • Add boundary edge-case teststests/unit/libbpfilter/chain.c — The unit test covers L3/L4 conflicts and non-conflict cases well, but is missing boundary inputs: (1) a rule with a single matcher (can never conflict), (2) a rule mixing a BF_MATCHER_NO_LAYER meta matcher with a protocol matcher (should not be flagged). These guard against loop logic regressions and verify the BF_MATCHER_NO_LAYER exclusion path.

Nits

  • Remove redundant stop_bpfiltertests/e2e/rules/incompatible_matchers.sh:26 — The EXIT trap in e2e_test_util.sh already handles teardown. No other rules/ e2e test calls stop_bpfilter explicitly.

@qdeslandes qdeslandes force-pushed the disable_rules_incompatible_matchers branch from 4abcead to 5bac247 Compare March 12, 2026 15:22
A rule combining matchers on the same network layer but targeting
different protocols (e.g. ip4.saddr + ip6.daddr, or tcp.sport +
udp.dport) can never match a packet. Detect these during chain
validation and disable the rule instead of generating dead BPF code.
@qdeslandes qdeslandes force-pushed the disable_rules_incompatible_matchers branch from 5bac247 to 564fce9 Compare March 12, 2026 16:43
Comment on lines 118 to +121
rule->disabled = r;

if (!rule->disabled)
rule->disabled = _bf_rule_has_incompatible_matchers(rule);
Copy link
Contributor

@yaakov-stein yaakov-stein Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rule->disabled = r | _bf_rule_has_incompatible_matchers(rule);? Or probably || so we don't run _bf_rule_has_incompatible_matchers(rule) unless we need to

@@ -0,0 +1,26 @@
#!/usr/bin/env bash
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the e2e tests valuable here? IIUC, the prior behavior would be the same as the behavior after the change, so what are we really testing? The only difference would be the codegen should be omitted and the rule should be disabled, which the e2e tests can't really cover, right?

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.

2 participants