Skip to content

Refactor sources to support C++ integration#325

Merged
qdeslandes merged 21 commits intofacebook:mainfrom
qdeslandes:pedantic
Sep 14, 2025
Merged

Refactor sources to support C++ integration#325
qdeslandes merged 21 commits intofacebook:mainfrom
qdeslandes:pedantic

Conversation

@qdeslandes
Copy link
Contributor

This is a massive change, which does not affect bpfilter's features. The core and lib modules both use C-specific features and compiler extensions. To allow C++ client to use bpfilter, those constructs must be dealt with, this is the purpose of this PR.

The biggest changes in this PR are:

  • Refactor the serialization logic to use MessagePack: serialization is now faster and supports ABI changes. The main purpose was to get it of the variable length arrays in struct bf_marsh, but the improvement is welcome.
  • Merge libbpfilter and core together: core headers were needed externally to communicate with the daemon, merging lib and core modules makes it easier to manage the public interface. Further refinements will be required before 1.0.0 as additional structures should be hidden.
  • Create a pedantic test target to ensure bpfilter can be used in a client library without any compiler extension of C-only feature.

@meta-cla meta-cla bot added the cla signed label Sep 14, 2025
Build tests will ensure the public headers stick to standard C code,
without any compiler extension or non-standard feature.
autosectionlabel is only used once and regularly triggers warnings with
Breathe do to sections naming conflicts. Instead, create sections
manually.
covreport should not depend on lcov.out: it should generate a coverage report
if it exists, or skip the report generation if it doesn't.
Do not nest the flexible-array member into a union, use a single status
code to carry the success or error type.
Core headers could be included in external project which might not
support compiler extension. As part of the effort to allow public
headers to be included in sources built with -pedantic, the kernel UAPI
headers must be hidden (to prevent compilation errors).
This commit is part of a multi-commits change to refactor the
serialization logic. The current logic has a few caveats:
- It's slow for large ruleset, due to all the allocations required
- It doesn't support ABI update (if the serialized structures change)
- It uses a flexible array member, which break C++ builds

Instead, the MessagePack format will be used. Further explanation about
this choice are available in the documentation.

This commit introduce pack.{c,h} which defines the objects used to
serialize and deserialize data.
See previous commit for more context.

This change update the serialization logic of core/bpfilter objects with
the message pack format.
See previous commits for more context.

Marsh has been replaced with pack, it can be removed from the sources.
This new API will be used for testing purposes: it allows end-to-end
tests to request a program's file descriptor and run BPF_PROG_TEST_RUN
to validate a chain.

It should not be used by end users, as described in the comment.
Instead of using a custom structure (bf_test_prog), perform direct calls
to bpfilter using the public API.
nft support has been disabled in bpfilter for some time, as major
changes broke it. This change also remove part of nft support unit
tests for now.
bf_request, bf_response, and bf_matcher definitions contain a
variable-length array, which is not allowed in C++. In order to allow
users to integrate with bpfilter in C++, those definition must be
hidden.
@qdeslandes qdeslandes force-pushed the pedantic branch 2 times, most recently from ee2e0b7 to 7eedf36 Compare September 14, 2025 14:33
The structures defined in core module are part of the public interface,
are they are needed to send and receive ruleset from the daemon. The
change merges lib and core together as a single shared library used for
both bfcli and bpfilter.

libbpfilter will require some improvements before 1.0.0 stable is
tagged, as some structures like bf_request should not need to be
exposed, but this commit is a first step.
`make test` relies on a target depending on unit, e2e, and pedantic
targets. This means CMake will run all 3 of these in parallel if -j is
anything but 1, which will make the output of the tests unreadable.

Modify the test target to use the cmake command and call unit, e2e, and
pedantic sequentially.
@qdeslandes qdeslandes merged commit 4f64a77 into facebook:main Sep 14, 2025
27 checks passed
@qdeslandes qdeslandes deleted the pedantic branch September 14, 2025 15:39
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

Comments