Skip to content

Commit fc0c9bd

Browse files
committed
n-acd: runtime eBPF support detection
Currently, eBPF support is toggled by a compile-time flag based on whether we expect the system to support it or not. Make it so that n-acd always attempts to use eBPF, and gracefully handles failure if it isn't supported (e.g. due to lacking capabilities).
1 parent a600afc commit fc0c9bd

9 files changed

+55
-59
lines changed

meson.build

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,4 @@ dep_crbtree = sub_crbtree.get_variable('libcrbtree_dep')
2222
dep_csiphash = sub_csiphash.get_variable('libcsiphash_dep')
2323
dep_cstdaux = sub_cstdaux.get_variable('libcstdaux_dep')
2424

25-
use_ebpf = get_option('ebpf')
26-
2725
subdir('src')

src/libnacd.sym

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ global:
1515
n_acd_ref;
1616
n_acd_unref;
1717
n_acd_get_fd;
18+
n_acd_has_bpf;
1819
n_acd_dispatch;
1920
n_acd_pop_event;
2021
n_acd_probe;

src/meson.build

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,9 @@ libnacd_sources = [
1515
'n-acd.c',
1616
'n-acd-probe.c',
1717
'util/timer.c',
18+
'n-acd-bpf.c',
1819
]
1920

20-
if use_ebpf
21-
libnacd_sources += [
22-
'n-acd-bpf.c',
23-
]
24-
else
25-
libnacd_sources += [
26-
'n-acd-bpf-fallback.c',
27-
]
28-
endif
29-
3021
libnacd_private = static_library(
3122
'nacd-private',
3223
libnacd_sources,
@@ -77,10 +68,8 @@ endif
7768
test_api = executable('test-api', ['test-api.c'], link_with: libnacd_shared)
7869
test('API Symbol Visibility', test_api)
7970

80-
if use_ebpf
81-
test_bpf = executable('test-bpf', ['test-bpf.c'], dependencies: libnacd_dep)
82-
test('eBPF socket filtering', test_bpf)
83-
endif
71+
test_bpf = executable('test-bpf', ['test-bpf.c'], dependencies: libnacd_dep)
72+
test('eBPF socket filtering', test_bpf)
8473

8574
test_loopback = executable('test-loopback', ['test-loopback.c'], dependencies: libnacd_dep)
8675
test('Echo Suppression via Loopback', test_loopback)

src/n-acd-bpf-fallback.c

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/n-acd-probe.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,9 @@ static int n_acd_probe_link(NAcdProbe *probe) {
236236
}
237237

238238
/*
239-
* Add the ip address to the map, if it is not already there.
239+
* Add the ip address to the map, it is not already there.
240240
*/
241-
if (n_acd_probe_is_unique(probe)) {
241+
if (probe->acd->fd_bpf_map != -1 && n_acd_probe_is_unique(probe)) {
242242
r = n_acd_bpf_map_add(probe->acd->fd_bpf_map, &probe->ip);
243243
if (r) {
244244
/*
@@ -261,7 +261,7 @@ static void n_acd_probe_unlink(NAcdProbe *probe) {
261261
* If this is the only probe for a given IP, remove the IP from the
262262
* kernel BPF map.
263263
*/
264-
if (n_acd_probe_is_unique(probe)) {
264+
if (probe->acd->fd_bpf_map != -1 && n_acd_probe_is_unique(probe)) {
265265
r = n_acd_bpf_map_remove(probe->acd->fd_bpf_map, &probe->ip);
266266
c_assert(r >= 0);
267267
--probe->acd->n_bpf_map;

src/n-acd.c

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -360,12 +360,13 @@ _c_public_ int n_acd_new(NAcd **acdp, NAcdConfig *config) {
360360
acd->max_bpf_map = 8;
361361

362362
r = n_acd_bpf_map_create(&acd->fd_bpf_map, acd->max_bpf_map);
363-
if (r)
364-
return r;
365-
366-
r = n_acd_bpf_compile(&fd_bpf_prog, acd->fd_bpf_map, (struct ether_addr*) acd->mac);
367-
if (r)
368-
return r;
363+
if (!r) {
364+
r = n_acd_bpf_compile(&fd_bpf_prog, acd->fd_bpf_map, (struct ether_addr*) acd->mac);
365+
if (r) {
366+
close(acd->fd_bpf_map);
367+
acd->fd_bpf_map = -1;
368+
}
369+
}
369370

370371
r = n_acd_socket_new(&acd->fd_socket, fd_bpf_prog, config);
371372
if (r)
@@ -572,6 +573,21 @@ _c_public_ void n_acd_get_fd(NAcd *acd, int *fdp) {
572573
*fdp = acd->fd_epoll;
573574
}
574575

576+
/**
577+
* n_acd_has_bpf() - query the usage of eBPF
578+
* @acd: context object to operate on
579+
*
580+
* Checks whether the ACD context has been configured, and if so, checks
581+
* whether the probe is using eBPF or not.
582+
*
583+
* Return: true if the probe is using eBPF, or
584+
* false if the probe failed to configure eBPF
585+
* (e.g. due to missing capabilities)
586+
*/
587+
_c_public_ bool n_acd_has_bpf(NAcd *acd) {
588+
return acd->fd_bpf_map != -1;
589+
}
590+
575591
static int n_acd_handle_timeout(NAcd *acd) {
576592
NAcdProbe *probe;
577593
uint64_t now;

src/n-acd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ NAcd *n_acd_ref(NAcd *acd);
9393
NAcd *n_acd_unref(NAcd *acd);
9494

9595
void n_acd_get_fd(NAcd *acd, int *fdp);
96+
bool n_acd_has_bpf(NAcd *acd);
9697
int n_acd_dispatch(NAcd *acd);
9798
int n_acd_pop_event(NAcd *acd, NAcdEvent **eventp);
9899

src/test-api.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ static void test_api_functions(void) {
5656
(void *)n_acd_ref,
5757
(void *)n_acd_unref,
5858
(void *)n_acd_get_fd,
59+
(void *)n_acd_has_bpf,
5960
(void *)n_acd_dispatch,
6061
(void *)n_acd_pop_event,
6162
(void *)n_acd_probe,

src/test-bpf.c

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
#include "n-acd-private.h"
2020
#include "test.h"
2121

22+
// https://mesonbuild.com/Unit-tests.html#skipped-tests-and-hard-errors
23+
#define RETURN_TEST_SKIPPED 77
24+
2225
#define ETHER_ARP_PACKET_INIT(_op, _mac, _sip, _tip) { \
2326
.ea_hdr = { \
2427
.ar_hrd = htobe16(ARPHRD_ETHER), \
@@ -43,11 +46,15 @@
4346
.arp_tpa[3] = be32toh((_tip)->s_addr) & 0xff, \
4447
}
4548

46-
static void test_map(void) {
49+
static int test_map(void) {
4750
int r, mapfd = -1;
4851
struct in_addr addr = { 1 };
4952

5053
r = n_acd_bpf_map_create(&mapfd, 8);
54+
if (r == -EPERM) {
55+
return RETURN_TEST_SKIPPED;
56+
}
57+
5158
c_assert(r >= 0);
5259
c_assert(mapfd >= 0);
5360

@@ -67,6 +74,7 @@ static void test_map(void) {
6774
c_assert(r == -ENOENT);
6875

6976
close(mapfd);
77+
return 0;
7078
}
7179

7280
static void verify_success(struct ether_arp *packet, int out_fd, int in_fd) {
@@ -92,7 +100,7 @@ static void verify_failure(struct ether_arp *packet, int out_fd, int in_fd) {
92100
c_assert(errno == EAGAIN);
93101
}
94102

95-
static void test_filter(void) {
103+
static int test_filter(void) {
96104
uint8_t buf[sizeof(struct ether_arp) + 1] = {};
97105
struct ether_addr mac1 = { { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 } };
98106
struct ether_addr mac2 = { { 0x01, 0x02, 0x03, 0x04, 0x05, 0x07 } };
@@ -103,6 +111,10 @@ static void test_filter(void) {
103111
int r, mapfd = -1, progfd = -1, pair[2];
104112

105113
r = n_acd_bpf_map_create(&mapfd, 1);
114+
if (r == -EPERM) {
115+
return RETURN_TEST_SKIPPED;
116+
}
117+
106118
c_assert(r >= 0);
107119

108120
r = n_acd_bpf_compile(&progfd, mapfd, &mac1);
@@ -214,13 +226,21 @@ static void test_filter(void) {
214226
close(pair[1]);
215227
close(progfd);
216228
close(mapfd);
229+
230+
return 0;
217231
}
218232

219233
int main(int argc, char **argv) {
234+
int r;
220235
test_setup();
221236

222-
test_map();
223-
test_filter();
237+
if ((r = test_map())) {
238+
return r;
239+
}
240+
241+
if ((r = test_filter())) {
242+
return r;
243+
}
224244

225245
return 0;
226246
}

0 commit comments

Comments
 (0)