Skip to content

Allow users to name and reuse sets#314

Merged
qdeslandes merged 2 commits into
facebook:mainfrom
qdeslandes:named_sets
Aug 19, 2025
Merged

Allow users to name and reuse sets#314
qdeslandes merged 2 commits into
facebook:mainfrom
qdeslandes:named_sets

Conversation

@qdeslandes

@qdeslandes qdeslandes commented Aug 19, 2025

Copy link
Copy Markdown
Contributor

Currently, users can use sets to efficiently filter packets against a large pool of similarly formatted data. Sets are defined as part of a rule, and can't be reused for other rules. If a user wants to filter on the same 10k elements sets in 2 different rules, bpfilter will create the set twice.

This change adds a new syntax to allow users to create named sets at the beginning of a chain, then filter on it in a rule using its name:

chain mychain BF_HOOK_XDP{ifindex=2} ACCEPT
    set myset (ip4.saddr, ip4.proto) in {
        192.168.1.1, udp;
        192.168.1.1, tcp
    }

    rule
        (ip4.saddr, ip4.proto) in myset
        tcp.flags any syn
        counter
        ACCEPT
      
    rule
        (ip4.saddr, ip4.proto) in myset
        counter
        ACCEPT

Add a name field to bf_set, so users can define named sets. Subsequent
commits will use this feature to reuse pre-defined set more than once in
a ruleset.
Add grammar and logic to allow users to name his sets and refer to them
later on in a rule. This change allows sets to be reused in multiple
rules.
@qdeslandes qdeslandes requested a review from Copilot August 19, 2025 17:04
@meta-cla meta-cla Bot added the cla signed label Aug 19, 2025

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds support for named sets in bpfilter, allowing users to define reusable sets at the chain level and reference them in multiple rules. Previously, sets were anonymous and defined inline within individual rules, leading to duplication when the same set was needed across multiple rules.

Key changes include:

  • Adding a name field to the bf_set structure to support named sets
  • Updating set creation functions to accept an optional name parameter
  • Extending the parser to handle named set declarations and references
  • Adding comprehensive test coverage for the new named set functionality

Reviewed Changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/core/set.h Adds name field to bf_set structure and updates function signatures
src/core/set.c Implements named set functionality with memory management for set names
src/bfcli/parser.y Adds grammar rules for named set declarations and references
src/bfcli/lexer.l Updates lexer to handle 'set' keyword and improved whitespace handling
src/bfcli/print.c Adds printing support for named sets in ruleset output
tests/unit/core/set.c Updates all test calls to use new function signatures
tests/harness/filters.c Updates test helper to use new bf_set_new signature
tests/e2e/cli.sh Adds comprehensive end-to-end tests for named set functionality
tests/rules.bpfilter Adds example usage of named sets in test ruleset
doc/usage/bfcli.rst Documents the new named set syntax and usage

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread src/core/set.c
@qdeslandes qdeslandes merged commit cefce8f into facebook:main Aug 19, 2025
27 checks passed
@qdeslandes qdeslandes deleted the named_sets branch August 19, 2025 17:35
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