Skip to content

Commit c1806ae

Browse files
LucaGuerrapoiana
authored andcommitted
cleanup(modern_bpf): use a regular map for shared ebpf settings
Signed-off-by: Luca Guerra <luca@guerra.sh>
1 parent c14db10 commit c1806ae

File tree

4 files changed

+188
-34
lines changed

4 files changed

+188
-34
lines changed

driver/modern_bpf/helpers/base/maps_getters.h

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,44 +17,99 @@
1717

1818
/*=============================== SETTINGS ===========================*/
1919

20+
static __always_inline struct capture_settings *maps__get_capture_settings() {
21+
uint32_t key = 0;
22+
return bpf_map_lookup_elem(&capture_settings, &key);
23+
}
24+
2025
static __always_inline uint64_t maps__get_boot_time() {
21-
return g_settings.boot_time;
26+
struct capture_settings *settings = maps__get_capture_settings();
27+
if(settings == NULL) {
28+
return 0;
29+
}
30+
31+
return settings->boot_time;
2232
}
2333

2434
static __always_inline uint32_t maps__get_snaplen() {
25-
return g_settings.snaplen;
35+
struct capture_settings *settings = maps__get_capture_settings();
36+
if(settings == NULL) {
37+
return 0;
38+
}
39+
40+
return settings->snaplen;
2641
}
2742

2843
static __always_inline bool maps__get_dropping_mode() {
29-
return g_settings.dropping_mode;
44+
struct capture_settings *settings = maps__get_capture_settings();
45+
if(settings == NULL) {
46+
return 0;
47+
}
48+
49+
return settings->dropping_mode;
3050
}
3151

3252
static __always_inline uint32_t maps__get_sampling_ratio() {
33-
return g_settings.sampling_ratio;
53+
struct capture_settings *settings = maps__get_capture_settings();
54+
if(settings == NULL) {
55+
return 0;
56+
}
57+
58+
return settings->sampling_ratio;
3459
}
3560

3661
static __always_inline bool maps__get_drop_failed() {
37-
return g_settings.drop_failed;
62+
struct capture_settings *settings = maps__get_capture_settings();
63+
if(settings == NULL) {
64+
return 0;
65+
}
66+
67+
return settings->drop_failed;
3868
}
3969

4070
static __always_inline bool maps__get_do_dynamic_snaplen() {
41-
return g_settings.do_dynamic_snaplen;
71+
struct capture_settings *settings = maps__get_capture_settings();
72+
if(settings == NULL) {
73+
return 0;
74+
}
75+
76+
return settings->do_dynamic_snaplen;
4277
}
4378

4479
static __always_inline uint16_t maps__get_fullcapture_port_range_start() {
45-
return g_settings.fullcapture_port_range_start;
80+
struct capture_settings *settings = maps__get_capture_settings();
81+
if(settings == NULL) {
82+
return 0;
83+
}
84+
85+
return settings->fullcapture_port_range_start;
4686
}
4787

4888
static __always_inline uint16_t maps__get_fullcapture_port_range_end() {
49-
return g_settings.fullcapture_port_range_end;
89+
struct capture_settings *settings = maps__get_capture_settings();
90+
if(settings == NULL) {
91+
return 0;
92+
}
93+
94+
return settings->fullcapture_port_range_end;
5095
}
5196

5297
static __always_inline uint16_t maps__get_statsd_port() {
53-
return g_settings.statsd_port;
98+
struct capture_settings *settings = maps__get_capture_settings();
99+
if(settings == NULL) {
100+
return 0;
101+
}
102+
103+
return settings->statsd_port;
54104
}
55105

56106
static __always_inline int32_t maps__get_scap_tid() {
57-
return g_settings.scap_tid;
107+
struct capture_settings *settings = maps__get_capture_settings();
108+
if(settings == NULL) {
109+
return 0;
110+
}
111+
112+
return settings->scap_tid;
58113
}
59114

60115
/*=============================== SETTINGS ===========================*/

driver/modern_bpf/maps/maps.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,6 @@ __weak const volatile uint32_t g_ia32_to_64_table[SYSCALL_TABLE_SIZE];
6565

6666
/*=============================== BPF GLOBAL VARIABLES ===============================*/
6767

68-
/**
69-
* @brief Global capture settings shared between userspace and
70-
* bpf programs.
71-
*/
72-
__weak struct capture_settings g_settings;
73-
7468
/**
7569
* @brief Variable used only kernel side to understand when we need to send
7670
* `DROP_E` and `DROP_X` events
@@ -138,6 +132,17 @@ struct {
138132
__type(value, bool);
139133
} interesting_syscalls_table_64bit __weak SEC(".maps");
140134

135+
/**
136+
* @brief Global capture settings shared between userspace and
137+
* bpf programs.
138+
*/
139+
struct {
140+
__uint(type, BPF_MAP_TYPE_ARRAY);
141+
__uint(max_entries, 1);
142+
__type(key, uint32_t);
143+
__type(value, struct capture_settings);
144+
} capture_settings __weak SEC(".maps");
145+
141146
/* These maps have one entry for each CPU.
142147
*
143148
* PLEASE NOTE:

driver/modern_bpf/programs/tail_called/events/syscall_dispatched_events/socket.bpf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ int BPF_PROG(socket_x, struct pt_regs *regs, long ret) {
7676
/* Just called once by our scap process */
7777
if(ret >= 0 && maps__get_socket_file_ops() == NULL) {
7878
struct task_struct *task = get_current_task();
79-
/* Please note that in `g_settings.scap_tid` scap will put its virtual tid
79+
/* Please note that in `settings.scap_tid` scap will put its virtual tid
8080
* if it is running inside a container. If we want to extract the same information
8181
* in the kernel we need to extract the virtual tid of the task.
8282
*/

userspace/libpman/src/maps.c

Lines changed: 111 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,41 +75,106 @@ uint64_t pman_get_probe_schema_ver() {
7575

7676
/*=============================== BPF GLOBAL VARIABLES ===============================*/
7777

78+
int pman_get_capture_settings(struct capture_settings* settings) {
79+
char error_message[MAX_ERROR_MESSAGE_LEN];
80+
int ret;
81+
uint32_t key = 0;
82+
int fd = bpf_map__fd(g_state.skel->maps.capture_settings);
83+
if(fd <= 0) {
84+
snprintf(error_message, MAX_ERROR_MESSAGE_LEN, "unable to get capture_settings map fd!");
85+
pman_print_error((const char*)error_message);
86+
return errno;
87+
}
88+
if((ret = bpf_map_lookup_elem(fd, &key, settings)) != 0) {
89+
snprintf(error_message, MAX_ERROR_MESSAGE_LEN, "unable to get capture_settings!");
90+
pman_print_error((const char*)error_message);
91+
}
92+
93+
return ret;
94+
}
95+
96+
int pman_update_capture_settings(struct capture_settings* settings) {
97+
char error_message[MAX_ERROR_MESSAGE_LEN];
98+
int ret;
99+
int fd = bpf_map__fd(g_state.skel->maps.capture_settings);
100+
if(fd <= 0) {
101+
snprintf(error_message, MAX_ERROR_MESSAGE_LEN, "unable to get capture_settings map fd!");
102+
pman_print_error((const char*)error_message);
103+
return errno;
104+
}
105+
uint32_t key = 0;
106+
if((ret = bpf_map_update_elem(fd, &key, settings, BPF_ANY)) != 0) {
107+
snprintf(error_message,
108+
MAX_ERROR_MESSAGE_LEN,
109+
"unable to initialize capture_settings map!");
110+
pman_print_error((const char*)error_message);
111+
}
112+
113+
return ret;
114+
}
115+
78116
void pman_set_snaplen(uint32_t desired_snaplen) {
79-
g_state.skel->bss->g_settings.snaplen = desired_snaplen;
117+
struct capture_settings settings;
118+
pman_get_capture_settings(&settings);
119+
settings.snaplen = desired_snaplen;
120+
pman_update_capture_settings(&settings);
80121
}
81122

82123
void pman_set_boot_time(uint64_t boot_time) {
83-
g_state.skel->bss->g_settings.boot_time = boot_time;
124+
struct capture_settings settings;
125+
pman_get_capture_settings(&settings);
126+
settings.boot_time = boot_time;
127+
pman_update_capture_settings(&settings);
84128
}
85129

86130
void pman_set_dropping_mode(bool value) {
87-
g_state.skel->bss->g_settings.dropping_mode = value;
131+
struct capture_settings settings;
132+
pman_get_capture_settings(&settings);
133+
settings.dropping_mode = value;
134+
pman_update_capture_settings(&settings);
88135
}
89136

90137
void pman_set_sampling_ratio(uint32_t value) {
91-
g_state.skel->bss->g_settings.sampling_ratio = value;
138+
struct capture_settings settings;
139+
pman_get_capture_settings(&settings);
140+
settings.sampling_ratio = value;
141+
pman_update_capture_settings(&settings);
92142
}
93143

94144
void pman_set_drop_failed(bool drop_failed) {
95-
g_state.skel->bss->g_settings.drop_failed = drop_failed;
145+
struct capture_settings settings;
146+
pman_get_capture_settings(&settings);
147+
settings.drop_failed = drop_failed;
148+
pman_update_capture_settings(&settings);
96149
}
97150

98151
void pman_set_do_dynamic_snaplen(bool do_dynamic_snaplen) {
99-
g_state.skel->bss->g_settings.do_dynamic_snaplen = do_dynamic_snaplen;
152+
struct capture_settings settings;
153+
pman_get_capture_settings(&settings);
154+
settings.do_dynamic_snaplen = do_dynamic_snaplen;
155+
pman_update_capture_settings(&settings);
100156
}
101157

102158
void pman_set_fullcapture_port_range(uint16_t range_start, uint16_t range_end) {
103-
g_state.skel->bss->g_settings.fullcapture_port_range_start = range_start;
104-
g_state.skel->bss->g_settings.fullcapture_port_range_end = range_end;
159+
struct capture_settings settings;
160+
pman_get_capture_settings(&settings);
161+
settings.fullcapture_port_range_start = range_start;
162+
settings.fullcapture_port_range_end = range_end;
163+
pman_update_capture_settings(&settings);
105164
}
106165

107166
void pman_set_statsd_port(uint16_t statsd_port) {
108-
g_state.skel->bss->g_settings.statsd_port = statsd_port;
167+
struct capture_settings settings;
168+
pman_get_capture_settings(&settings);
169+
settings.statsd_port = statsd_port;
170+
pman_update_capture_settings(&settings);
109171
}
110172

111173
void pman_set_scap_tid(int32_t scap_tid) {
112-
g_state.skel->bss->g_settings.scap_tid = scap_tid;
174+
struct capture_settings settings;
175+
pman_get_capture_settings(&settings);
176+
settings.scap_tid = scap_tid;
177+
pman_update_capture_settings(&settings);
113178
}
114179

115180
void pman_fill_syscall_sampling_table() {
@@ -133,6 +198,28 @@ void pman_fill_syscall_sampling_table() {
133198
}
134199
}
135200

201+
int pman_init_settings_map() {
202+
char error_message[MAX_ERROR_MESSAGE_LEN];
203+
struct capture_settings settings = {};
204+
uint32_t key = 0;
205+
int fd = bpf_map__fd(g_state.skel->maps.capture_settings);
206+
if(fd <= 0) {
207+
snprintf(error_message, MAX_ERROR_MESSAGE_LEN, "unable to get capture_settings map!");
208+
pman_print_error((const char*)error_message);
209+
return errno;
210+
}
211+
212+
if(bpf_map_update_elem(fd, &key, &settings, BPF_ANY)) {
213+
snprintf(error_message,
214+
MAX_ERROR_MESSAGE_LEN,
215+
"unable to initialize capture_settings map!");
216+
pman_print_error((const char*)error_message);
217+
return errno;
218+
}
219+
220+
return 0;
221+
}
222+
136223
void pman_fill_ia32_to_64_table() {
137224
for(int syscall_id = 0; syscall_id < SYSCALL_TABLE_SIZE; syscall_id++) {
138225
// Note: we will map all syscalls from the upper limit of the ia32 table
@@ -294,13 +381,14 @@ int pman_fill_syscall_exit_extra_tail_table() {
294381
int pman_fill_interesting_syscalls_table_64bit() {
295382
char error_message[MAX_ERROR_MESSAGE_LEN];
296383
int fd = bpf_map__fd(g_state.skel->maps.interesting_syscalls_table_64bit);
297-
for (uint32_t i = 0; i < SYSCALL_TABLE_SIZE; i++) {
384+
for(uint32_t i = 0; i < SYSCALL_TABLE_SIZE; i++) {
298385
const bool interesting = false;
299386
if(bpf_map_update_elem(fd, &i, &interesting, BPF_ANY) < 0) {
300387
snprintf(error_message,
301-
MAX_ERROR_MESSAGE_LEN,
302-
"unable to initialize interesting syscall table at index %d!", i);
303-
pman_print_error((const char *)error_message);
388+
MAX_ERROR_MESSAGE_LEN,
389+
"unable to initialize interesting syscall table at index %d!",
390+
i);
391+
pman_print_error((const char*)error_message);
304392
return errno;
305393
}
306394
}
@@ -312,9 +400,11 @@ int pman_mark_single_64bit_syscall(int syscall_id, bool interesting) {
312400
int fd = bpf_map__fd(g_state.skel->maps.interesting_syscalls_table_64bit);
313401
if(bpf_map_update_elem(fd, &syscall_id, &interesting, BPF_ANY) < 0) {
314402
snprintf(error_message,
315-
MAX_ERROR_MESSAGE_LEN,
316-
"unable to set interesting syscall at index %d as %d!", syscall_id, interesting);
317-
pman_print_error((const char *)error_message);
403+
MAX_ERROR_MESSAGE_LEN,
404+
"unable to set interesting syscall at index %d as %d!",
405+
syscall_id,
406+
interesting);
407+
pman_print_error((const char*)error_message);
318408
return errno;
319409
}
320410
return 0;
@@ -362,6 +452,10 @@ int pman_prepare_maps_before_loading() {
362452

363453
int pman_finalize_maps_after_loading() {
364454
int err;
455+
err = pman_init_settings_map();
456+
if(err != 0) {
457+
return err;
458+
}
365459

366460
/* set bpf global variables. */
367461
pman_set_snaplen(80);

0 commit comments

Comments
 (0)