lib: set: support port ranges in set elements#570
Conversation
bf_set_add_elem_raw() now detects range syntax (START-END) in a set element's components and expands it into individual elements, reusing the existing BF_MATCHER_RANGE parsing already used for standalone range matchers. Only one ranged component per element is supported; sets with ranges in multiple components of the same element return an error. Printing behavior for expanded ranges is intentionally left unchanged (elements print individually, not re-factored into a range) pending maintainer input on the open question raised in the issue. Fixes facebook#562
|
Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
|
Hi, could you please disclose if/how much AI has been used for this work? Thank you. |
Hey there, only used AI for two things: drafting the unit tests in tests/unit/libbpfilter/set.c, and writing up the PR description. I reviewed both, and ran the test suite locally (unit.libbpfilter.set, 28/28) to confirm the cases and checked the tests actually cover what they say they do. |
Summary
Implements port ranges in set elements (#562).
bf_set_add_elem_raw()now recognises range syntax (START-END) in a setelement's components and expands it into individual set elements. For example:
produces a set containing
{ 11, 22, 33, 34, …, 44 }(14 elements).Range detection reuses the
BF_MATCHER_RANGEparsing that already backs thestandalone
rangematcher operator — a component is treated as a range onlywhen the token contains a
-and the component's matcher type registersBF_MATCHER_RANGEops. This scopes the feature to the matcher types thatalready support ranges (ports today) without hardcoding anything port-specific,
and non-range / multi-component parsing is unchanged.
Behaviour
[start, end]is expanded into its own element and insertedinto the set's hashset, deduplicated like any other element.
(
BF_MATCHER_IN) path, so their in-memory representation is byte-identical toan element written as a plain value.
_bf_parse_l4_port_range():both bounds must be valid decimal ports
<= 65535, andstart <= end.44-33,33-, and-44are rejected.Scope / limitations
A couple of things are intentionally left out to keep this a minimal, focused
change — flagging them for maintainer input:
not re-factored back into a
START-ENDrange. This is the open questionraised in Port range in sets #562; happy to add range factorization on
ruleset getin afollow-up if that's the preferred direction.
component (e.g.
(tcp.sport, tcp.dport) in { 1-2, 3-4 }) returns-EINVALrather than performing a Cartesian expansion. This avoids surprising blow-up
in element count; can revisit if Cartesian expansion is wanted.
A note on element separators
The issue's example is written as
{ 11, 22, 33-44 }, but bpfilter separatesset elements with
;and components within an element with,(e.g.
{ 80; 443 }vs(ip4.daddr, tcp.sport) in { 1.2.3.4, 80 }). So therange form is
{ 11; 22; 33-44 }. This PR does not change the grammar, so thecomma-separated form from the issue's example still parses as multiple
components. If treating
,as an element separator for single-component setsis desired, that's a separate grammar change and I'm glad to take it on
separately.
Tests
Added unit tests in
tests/unit/libbpfilter/set.c:{11; 22; 33-44}→ 14 elements)0-1, degenerate65535-65535)44-33,33-,-44){1.2.3.4, 80-82}→ 3)All
unit.libbpfilter.settests pass (28/28).