Skip to content

Commit 5f6eab6

Browse files
committed
build: add USE_HASHSET option to switch to hashset implementation
When cmake -DUSE_HASHSET=1 is passed, set.h redirects all bf_set_* symbols to their bf_hashset_* counterparts via preprocessor macros. The CI workflow is extended to test both paths. USE_HASHSET defaults to off for now.
1 parent b23d217 commit 5f6eab6

File tree

14 files changed

+90
-10
lines changed

14 files changed

+90
-10
lines changed

.github/workflows/ci.yaml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,24 +94,25 @@ jobs:
9494
host:
9595
- { name: 8-core-ubuntu, arch: x64 }
9696
- { name: 4-core-ubuntu-arm, arch: arm64 }
97+
use_hashset: [ 0, 1 ]
9798
runs-on: [ "${{ matrix.host.name }}" ]
9899
container:
99100
image: ghcr.io/facebook/bpfilter:fedora-43-${{ matrix.host.arch }}
100101
options: --privileged
101-
name: "Test: ${{ matrix.host.arch }}"
102+
name: "Test: ${{ matrix.host.arch }}${{ matrix.use_hashset == 1 && ' (hashset)' || '' }}"
102103
steps:
103104
- name: Checkout bpfilter
104105
uses: actions/checkout@v2
105106
- name: Restore the cached test results
106107
uses: actions/cache@v4
107-
if: matrix.host.arch == 'x64'
108+
if: matrix.host.arch == 'x64' && matrix.use_hashset == 0
108109
with:
109110
path: build/coverage
110111
key: tests-results-${{ github.run_id }}
111112
- name: Mount bpffs
112113
run: mount bpffs /sys/fs/bpf -t bpf
113114
- name: Configure the build
114-
run: cmake -S $GITHUB_WORKSPACE -B $GITHUB_WORKSPACE/build -DWITH_COVERAGE=1
115+
run: cmake -S $GITHUB_WORKSPACE -B $GITHUB_WORKSPACE/build -DWITH_COVERAGE=1 -DUSE_HASHSET=${{ matrix.use_hashset }}
115116
- name: Build tests
116117
run: make -C $GITHUB_WORKSPACE/build -j `nproc` test_bin
117118

@@ -125,7 +126,7 @@ jobs:
125126
run: ctest --test-dir $GITHUB_WORKSPACE/build -L fuzzing --verbose
126127
- name: Upload fuzzer findings
127128
uses: actions/upload-artifact@v4
128-
if: always()
129+
if: always() && matrix.use_hashset == 0
129130
with:
130131
name: fuzzer-findings-${{ matrix.host.arch }}
131132
path: ${{ github.workspace }}/build/findings
@@ -136,6 +137,7 @@ jobs:
136137
- name: Run checks
137138
run: ctest --test-dir $GITHUB_WORKSPACE/build -L check --verbose
138139
- name: Generate the coverage report
140+
if: matrix.use_hashset == 0
139141
run: make -C $GITHUB_WORKSPACE/build coverage
140142

141143
benchmark:

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ option(NO_TESTS "Disable unit, end-to-end, and integration tests" 0)
3434
option(NO_CHECKS "Disable the check target (clang-tidy and clang-format" 0)
3535
option(NO_BENCHMARKS "Disable the benchmark" 0)
3636
option(WITH_COVERAGE "Build with code coverage support. Disabled by default" 0)
37+
option(USE_HASHSET "Use bf_hashset instead of bf_set for set implementation. Disabled by default" 0)
3738

3839
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
3940

src/libbpfilter/CMakeLists.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ set(libbpfilter_srcs
5555
${CMAKE_CURRENT_SOURCE_DIR}/request.c
5656
${CMAKE_CURRENT_SOURCE_DIR}/response.c
5757
${CMAKE_CURRENT_SOURCE_DIR}/rule.c
58-
${CMAKE_CURRENT_SOURCE_DIR}/set.c
5958
${CMAKE_CURRENT_SOURCE_DIR}/vector.c
6059
${CMAKE_CURRENT_SOURCE_DIR}/verdict.c
6160
${CMAKE_CURRENT_SOURCE_DIR}/version.c
@@ -65,6 +64,15 @@ set(libbpfilter_srcs
6564
${CMAKE_SOURCE_DIR}/src/external/mpack.c
6665
)
6766

67+
if (NOT USE_HASHSET)
68+
list(APPEND libbpfilter_srcs ${CMAKE_CURRENT_SOURCE_DIR}/set.c)
69+
endif ()
70+
71+
set(BF_PC_EXTRA_CFLAGS "")
72+
if (USE_HASHSET)
73+
string(APPEND BF_PC_EXTRA_CFLAGS " -DBF_USE_HASHSET")
74+
endif ()
75+
6876
configure_file(
6977
${CMAKE_CURRENT_SOURCE_DIR}/bpfilter.pc.in
7078
${CMAKE_BINARY_DIR}/output/lib/pkgconfig/bpfilter.pc
@@ -86,6 +94,8 @@ target_compile_definitions(libbpfilter
8694
PRIVATE
8795
# MPack should use the C standard library API
8896
MPACK_STDLIB
97+
PUBLIC
98+
$<$<BOOL:${USE_HASHSET}>:BF_USE_HASHSET>
8999
)
90100

91101
target_include_directories(libbpfilter

src/libbpfilter/bpfilter.pc.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ Name: bpfilter
66
Description: BPF-based packet filtering framework
77
URL: https://github.com/facebook/bpfilter
88
Version: @PROJECT_VERSION@@PROJECT_VERSION_SUFFIX@
9-
Cflags: -I${includedir}
9+
Cflags: -I${includedir}@BF_PC_EXTRA_CFLAGS@
1010
Libs: -L${libdir} -lbpfilter

src/libbpfilter/include/bpfilter/bpfilter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
#include <stddef.h>
1010

1111
#include <bpfilter/list.h>
12+
#include <bpfilter/set.h>
1213

1314
struct bf_response;
1415
struct bf_chain;
15-
struct bf_set;
1616
struct bf_hookopts;
1717

1818
/**

src/libbpfilter/include/bpfilter/chain.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
#include <bpfilter/hook.h>
1010
#include <bpfilter/list.h>
1111
#include <bpfilter/pack.h>
12+
#include <bpfilter/set.h>
1213
#include <bpfilter/verdict.h>
1314

1415
struct bf_hookopts;
1516
struct bf_matcher;
1617
struct bf_rule;
17-
struct bf_set;
1818

1919
#define _free_bf_chain_ __attribute__((cleanup(bf_chain_free)))
2020

src/libbpfilter/include/bpfilter/set.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,30 @@
55

66
#pragma once
77

8+
#ifdef BF_USE_HASHSET
9+
10+
#include <bpfilter/hashset.h>
11+
12+
#define bf_set bf_hashset
13+
#define _free_bf_set_ _free_bf_hashset_
14+
#define BF_SET_MAX_N_COMPS BF_HASHSET_MAX_N_COMPS
15+
#define bf_set_new bf_hashset_new
16+
#define bf_set_new_from_raw bf_hashset_new_from_raw
17+
#define bf_set_new_from_pack bf_hashset_new_from_pack
18+
#define bf_set_free bf_hashset_free
19+
#define bf_set_pack bf_hashset_pack
20+
#define bf_set_dump bf_hashset_dump
21+
#define bf_set_is_empty bf_hashset_is_empty
22+
#define bf_set_add_elem bf_hashset_add_elem
23+
#define bf_set_add_elem_raw bf_hashset_add_elem_raw
24+
#define bf_set_add_many bf_hashset_add_many
25+
#define bf_set_remove_many bf_hashset_remove_many
26+
#define bf_set_foreach bf_hashset_foreach
27+
#define bf_set_size bf_hashset_size
28+
#define bf_set_contains bf_hashset_contains
29+
30+
#else
31+
832
#include <stdbool.h>
933
#include <stddef.h>
1034

@@ -193,3 +217,14 @@ int bf_set_add_many(struct bf_set *dest, struct bf_set **to_add);
193217
* - `-EINVAL`: set key format doesn't match between dest and to_remove.
194218
*/
195219
int bf_set_remove_many(struct bf_set *dest, struct bf_set **to_remove);
220+
221+
/**
222+
* @brief Check whether an element exists in a set.
223+
*
224+
* @param set Initialised set. Can't be NULL.
225+
* @param elem Element to look up. Can't be NULL.
226+
* @return True if @p elem is present.
227+
*/
228+
bool bf_set_contains(const struct bf_set *set, const void *elem);
229+
230+
#endif

src/libbpfilter/set.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,3 +523,16 @@ int bf_set_remove_many(struct bf_set *dest, struct bf_set **to_remove)
523523

524524
return 0;
525525
}
526+
527+
bool bf_set_contains(const struct bf_set *set, const void *elem)
528+
{
529+
assert(set);
530+
assert(elem);
531+
532+
bf_list_foreach (&set->elems, node) {
533+
if (memcmp(bf_list_node_get_data(node), elem, set->elem_size) == 0)
534+
return true;
535+
}
536+
537+
return false;
538+
}

tests/harness/fake.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ struct bf_matcher *bft_matcher_dummy(const void *data, size_t data_len)
221221
return TAKE_PTR(matcher);
222222
}
223223

224+
#ifndef BF_USE_HASHSET
224225
struct bf_set *bft_set_dummy(size_t n_elems)
225226
{
226227
_free_bf_set_ struct bf_set *set = NULL;
@@ -245,6 +246,7 @@ struct bf_set *bft_set_dummy(size_t n_elems)
245246

246247
return TAKE_PTR(set);
247248
}
249+
#endif
248250

249251
struct bf_hashset *bft_hashset_dummy(size_t n_elems)
250252
{

tests/harness/fake.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,9 @@ const void *bft_get_randomly_filled_buffer(size_t len);
5353
struct bf_chain *bft_chain_dummy(bool with_rules);
5454
struct bf_rule *bft_rule_dummy(size_t n_matchers);
5555
struct bf_matcher *bft_matcher_dummy(const void *data, size_t data_len);
56+
#ifdef BF_USE_HASHSET
57+
#define bft_set_dummy bft_hashset_dummy
58+
#else
5659
struct bf_set *bft_set_dummy(size_t n_elems);
60+
#endif
5761
struct bf_hashset *bft_hashset_dummy(size_t n_elems);

0 commit comments

Comments
 (0)