Skip to content

Commit 2bd4147

Browse files
committed
daemon: fold command-line options into the runtime context
Remove the standalone opts module (opts.h, opts.c) in the daemon, which maintained a separate static global options object. Options are moved to bf_ctx, and so are accessors.
1 parent bb16adf commit 2bd4147

File tree

9 files changed

+228
-293
lines changed

9 files changed

+228
-293
lines changed

src/bpfilter/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ configure_file(
1111

1212
add_executable(bpfilter
1313
${CMAKE_CURRENT_SOURCE_DIR}/main.c
14-
${CMAKE_CURRENT_SOURCE_DIR}/opts.h ${CMAKE_CURRENT_SOURCE_DIR}/opts.c
1514
${CMAKE_CURRENT_SOURCE_DIR}/cgen/cgen.h ${CMAKE_CURRENT_SOURCE_DIR}/cgen/cgen.c
1615
${CMAKE_CURRENT_SOURCE_DIR}/cgen/cgroup_skb.h ${CMAKE_CURRENT_SOURCE_DIR}/cgen/cgroup_skb.c
1716
${CMAKE_CURRENT_SOURCE_DIR}/cgen/dump.h ${CMAKE_CURRENT_SOURCE_DIR}/cgen/dump.c

src/bpfilter/cgen/cgen.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include "cgen/prog/map.h"
3333
#include "cgen/program.h"
3434
#include "ctx.h"
35-
#include "opts.h"
3635

3736
#define _BF_PROG_NAME "bf_prog"
3837
#define _BF_CTX_PIN_NAME "bf_ctx"
@@ -236,7 +235,7 @@ void bf_cgen_free(struct bf_cgen **cgen)
236235
* the chain hasn't been pinned (e.g. due to a failure), the pin directory
237236
* will be empty and will be removed. If the chain is valid and pinned, then
238237
* the removal of the pin directory will fail, but that's alright. */
239-
if (bf_opts_persist() && (pin_fd = bf_ctx_get_pindir_fd()) >= 0)
238+
if (!bf_ctx_is_transient() && (pin_fd = bf_ctx_get_pindir_fd()) >= 0)
240239
bf_rmdir_at(pin_fd, (*cgen)->chain->name, false);
241240

242241
bf_handle_free(&(*cgen)->handle);
@@ -311,7 +310,7 @@ int bf_cgen_set(struct bf_cgen *cgen, const struct bf_ns *ns,
311310
{
312311
_free_bf_program_ struct bf_program *prog = NULL;
313312
_cleanup_close_ int pindir_fd = -1;
314-
bool persist = bf_opts_persist();
313+
bool persist = !bf_ctx_is_transient();
315314
int r;
316315

317316
assert(cgen);
@@ -367,7 +366,7 @@ int bf_cgen_load(struct bf_cgen *cgen)
367366
{
368367
_free_bf_program_ struct bf_program *prog = NULL;
369368
_cleanup_close_ int pindir_fd = -1;
370-
bool persist = bf_opts_persist();
369+
bool persist = !bf_ctx_is_transient();
371370
int r;
372371

373372
assert(cgen);
@@ -413,7 +412,7 @@ int bf_cgen_attach(struct bf_cgen *cgen, const struct bf_ns *ns,
413412
struct bf_hookopts **hookopts)
414413
{
415414
_cleanup_close_ int pindir_fd = -1;
416-
bool persist = bf_opts_persist();
415+
bool persist = !bf_ctx_is_transient();
417416
int r;
418417

419418
assert(cgen);
@@ -509,7 +508,7 @@ int bf_cgen_update(struct bf_cgen *cgen, struct bf_chain **new_chain,
509508
_free_bf_program_ struct bf_program *new_prog = NULL;
510509
_free_bf_handle_ struct bf_handle *new_handle = NULL;
511510
_cleanup_close_ int pindir_fd = -1;
512-
bool persist = bf_opts_persist();
511+
bool persist = !bf_ctx_is_transient();
513512
struct bf_handle *old_handle;
514513
int r;
515514

src/bpfilter/cgen/program.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
#include "cgen/xdp.h"
5858
#include "ctx.h"
5959
#include "filter.h"
60-
#include "opts.h"
6160

6261
#define _BF_LOG_BUF_SIZE \
6362
(UINT32_MAX >> 8) /* verifier maximum in kernels <= 5.1 */
@@ -834,15 +833,15 @@ int bf_program_load(struct bf_program *prog)
834833
if (r)
835834
return bf_err_r(r, "failed to load the log map");
836835

837-
if (bf_opts_is_verbose(BF_VERBOSE_DEBUG)) {
836+
if (bf_ctx_is_verbose(BF_VERBOSE_DEBUG)) {
838837
log_buf = malloc(_BF_LOG_BUF_SIZE);
839838
if (!log_buf) {
840839
return bf_err_r(-ENOMEM,
841840
"failed to allocate BPF_PROG_LOAD logs buffer");
842841
}
843842
}
844843

845-
if (bf_opts_is_verbose(BF_VERBOSE_BYTECODE))
844+
if (bf_ctx_is_verbose(BF_VERBOSE_BYTECODE))
846845
bf_program_dump_bytecode(prog);
847846

848847
r = bf_bpf_prog_load(prog->handle->prog_name,

src/bpfilter/cgen/stub.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
#include "cgen/printer.h"
3131
#include "cgen/program.h"
3232
#include "cgen/swich.h"
33+
#include "ctx.h"
3334
#include "filter.h"
34-
#include "opts.h"
3535

3636
#define _BF_LOW_EH_BITMASK 0x1801800000000801ULL
3737

@@ -73,7 +73,7 @@ static int _bf_stub_make_ctx_dynptr(struct bf_program *program, int arg_reg,
7373
BPF_MOV32_IMM(BPF_REG_3, bf_program_error_counter_idx(program)));
7474
EMIT_FIXUP_ELFSTUB(program, BF_ELFSTUB_UPDATE_COUNTERS);
7575

76-
if (bf_opts_is_verbose(BF_VERBOSE_BPF))
76+
if (bf_ctx_is_verbose(BF_VERBOSE_BPF))
7777
EMIT_PRINT(program, "failed to create a new dynamic pointer");
7878

7979
r = program->runtime.ops->get_verdict(BF_VERDICT_ACCEPT);
@@ -132,7 +132,7 @@ int bf_stub_parse_l2_ethhdr(struct bf_program *program)
132132
BPF_MOV32_IMM(BPF_REG_3, bf_program_error_counter_idx(program)));
133133
EMIT_FIXUP_ELFSTUB(program, BF_ELFSTUB_UPDATE_COUNTERS);
134134

135-
if (bf_opts_is_verbose(BF_VERBOSE_BPF))
135+
if (bf_ctx_is_verbose(BF_VERBOSE_BPF))
136136
EMIT_PRINT(program, "failed to create L2 dynamic pointer slice");
137137

138138
r = program->runtime.ops->get_verdict(BF_VERDICT_ACCEPT);
@@ -207,7 +207,7 @@ int bf_stub_parse_l3_hdr(struct bf_program *program)
207207
BPF_MOV32_IMM(BPF_REG_3, bf_program_error_counter_idx(program)));
208208
EMIT_FIXUP_ELFSTUB(program, BF_ELFSTUB_UPDATE_COUNTERS);
209209

210-
if (bf_opts_is_verbose(BF_VERBOSE_BPF))
210+
if (bf_ctx_is_verbose(BF_VERBOSE_BPF))
211211
EMIT_PRINT(program, "failed to create L3 dynamic pointer slice");
212212

213213
r = program->runtime.ops->get_verdict(BF_VERDICT_ACCEPT);
@@ -366,7 +366,7 @@ int bf_stub_parse_l4_hdr(struct bf_program *program)
366366
BPF_MOV32_IMM(BPF_REG_3, bf_program_error_counter_idx(program)));
367367
EMIT_FIXUP_ELFSTUB(program, BF_ELFSTUB_UPDATE_COUNTERS);
368368

369-
if (bf_opts_is_verbose(BF_VERBOSE_BPF))
369+
if (bf_ctx_is_verbose(BF_VERBOSE_BPF))
370370
EMIT_PRINT(program, "failed to create L4 dynamic pointer slice");
371371

372372
r = program->runtime.ops->get_verdict(BF_VERDICT_ACCEPT);

src/bpfilter/ctx.c

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
#include "cgen/cgen.h"
2828
#include "cgen/elfstub.h"
29-
#include "opts.h"
3029

3130
#define _free_bf_ctx_ __attribute__((cleanup(_bf_ctx_free)))
3231

@@ -47,32 +46,43 @@ struct bf_ctx
4746
bf_list cgens;
4847

4948
struct bf_elfstub *stubs[_BF_ELFSTUB_MAX];
49+
50+
/// If true, don't persist state and unload programs on exit.
51+
bool transient;
52+
53+
/// Pass a token to BPF system calls, obtained from bpffs.
54+
bool with_bpf_token;
55+
56+
/// Path to the bpffs to pin the BPF objects into.
57+
const char *bpffs_path;
58+
59+
/// Verbose flags.
60+
uint16_t verbose;
5061
};
5162

5263
static void _bf_ctx_free(struct bf_ctx **ctx);
5364

5465
/// Global daemon context. Hidden in this translation unit.
5566
static struct bf_ctx *_bf_global_ctx = NULL;
5667

57-
static int _bf_ctx_gen_token(void)
68+
static int _bf_ctx_gen_token(const char *bpffs_path)
5869
{
5970
_cleanup_close_ int mnt_fd = -1;
6071
_cleanup_close_ int bpffs_fd = -1;
6172
_cleanup_close_ int token_fd = -1;
6273

63-
mnt_fd = open(bf_opts_bpffs_path(), O_DIRECTORY);
74+
mnt_fd = open(bpffs_path, O_DIRECTORY);
6475
if (mnt_fd < 0)
65-
return bf_err_r(errno, "failed to open '%s'", bf_opts_bpffs_path());
76+
return bf_err_r(errno, "failed to open '%s'", bpffs_path);
6677

6778
bpffs_fd = openat(mnt_fd, ".", 0, O_RDWR);
6879
if (bpffs_fd < 0)
69-
return bf_err_r(errno, "failed to get bpffs FD from '%s'",
70-
bf_opts_bpffs_path());
80+
return bf_err_r(errno, "failed to get bpffs FD from '%s'", bpffs_path);
7181

7282
token_fd = bf_bpf_token_create(bpffs_fd);
7383
if (token_fd < 0) {
7484
return bf_err_r(token_fd, "failed to create BPF token for '%s'",
75-
bf_opts_bpffs_path());
85+
bpffs_path);
7686
}
7787

7888
return TAKE_FD(token_fd);
@@ -84,25 +94,36 @@ static int _bf_ctx_gen_token(void)
8494
* On failure, @p ctx is left unchanged.
8595
*
8696
* @param ctx New context to create. Can't be NULL.
97+
* @param transient If true, don't persist state and unload programs on exit.
98+
* @param with_bpf_token If true, create a BPF token from bpffs.
99+
* @param bpffs_path Path to the bpffs mountpoint. Can't be NULL.
100+
* @param verbose Bitmask of verbose flags.
87101
* @return 0 on success, negative errno value on failure.
88102
*/
89-
static int _bf_ctx_new(struct bf_ctx **ctx)
103+
static int _bf_ctx_new(struct bf_ctx **ctx, bool transient, bool with_bpf_token,
104+
const char *bpffs_path, uint16_t verbose)
90105
{
91106
_free_bf_ctx_ struct bf_ctx *_ctx = NULL;
92107
int r;
93108

94109
assert(ctx);
110+
assert(bpffs_path);
95111

96112
_ctx = calloc(1, sizeof(*_ctx));
97113
if (!_ctx)
98114
return -ENOMEM;
99115

116+
_ctx->transient = transient;
117+
_ctx->with_bpf_token = with_bpf_token;
118+
_ctx->bpffs_path = bpffs_path;
119+
_ctx->verbose = verbose;
120+
100121
r = bf_ns_init(&_ctx->ns, getpid());
101122
if (r)
102123
return bf_err_r(r, "failed to initialise current bf_ns");
103124

104125
_ctx->token_fd = -1;
105-
if (bf_opts_with_bpf_token()) {
126+
if (_ctx->with_bpf_token) {
106127
_cleanup_close_ int token_fd = -1;
107128

108129
r = bf_btf_kernel_has_token();
@@ -114,7 +135,7 @@ static int _bf_ctx_new(struct bf_ctx **ctx)
114135
if (r)
115136
return bf_err_r(r, "failed to check for BPF token support");
116137

117-
token_fd = _bf_ctx_gen_token();
138+
token_fd = _bf_ctx_gen_token(_ctx->bpffs_path);
118139
if (token_fd < 0)
119140
return bf_err_r(token_fd, "failed to generate a BPF token");
120141

@@ -310,10 +331,10 @@ static int _bf_ctx_discover(void)
310331
int iter_fd;
311332
int r;
312333

313-
bpffs_fd = bf_opendir(bf_opts_bpffs_path());
334+
bpffs_fd = bf_opendir(_bf_global_ctx->bpffs_path);
314335
if (bpffs_fd < 0) {
315336
return bf_err_r(bpffs_fd, "failed to open bpffs at %s",
316-
bf_opts_bpffs_path());
337+
_bf_global_ctx->bpffs_path);
317338
}
318339

319340
pindir_fd = bf_opendir_at(bpffs_fd, "bpfilter", false);
@@ -381,18 +402,19 @@ static int _bf_ctx_discover(void)
381402
return 0;
382403
}
383404

384-
int bf_ctx_setup(void)
405+
int bf_ctx_setup(bool transient, bool with_bpf_token, const char *bpffs_path,
406+
uint16_t verbose)
385407
{
386408
_free_bf_ctx_ struct bf_ctx *_ctx = NULL;
387409
int r;
388410

389-
r = _bf_ctx_new(&_ctx);
411+
r = _bf_ctx_new(&_ctx, transient, with_bpf_token, bpffs_path, verbose);
390412
if (r)
391413
return bf_err_r(r, "failed to create new context");
392414

393415
_bf_global_ctx = TAKE_PTR(_ctx);
394416

395-
if (!bf_opts_transient()) {
417+
if (!bf_ctx_is_transient()) {
396418
r = _bf_ctx_discover();
397419
if (r) {
398420
_bf_ctx_free(&_bf_global_ctx);
@@ -403,13 +425,8 @@ int bf_ctx_setup(void)
403425
return 0;
404426
}
405427

406-
void bf_ctx_teardown(bool clear)
428+
void bf_ctx_teardown(void)
407429
{
408-
if (clear) {
409-
bf_list_foreach (&_bf_global_ctx->cgens, cgen_node)
410-
bf_cgen_unload(bf_list_node_get_data(cgen_node));
411-
}
412-
413430
_bf_ctx_free(&_bf_global_ctx);
414431
}
415432

@@ -470,16 +487,16 @@ int bf_ctx_get_pindir_fd(void)
470487
_cleanup_close_ int bpffs_fd = -1;
471488
_cleanup_close_ int pindir_fd = -1;
472489

473-
bpffs_fd = bf_opendir(bf_opts_bpffs_path());
490+
bpffs_fd = bf_opendir(_bf_global_ctx->bpffs_path);
474491
if (bpffs_fd < 0) {
475492
return bf_err_r(bpffs_fd, "failed to open bpffs at %s",
476-
bf_opts_bpffs_path());
493+
_bf_global_ctx->bpffs_path);
477494
}
478495

479496
pindir_fd = bf_opendir_at(bpffs_fd, "bpfilter", true);
480497
if (pindir_fd < 0) {
481498
return bf_err_r(pindir_fd, "failed to open pin directory %s/bpfilter",
482-
bf_opts_bpffs_path());
499+
_bf_global_ctx->bpffs_path);
483500
}
484501

485502
return TAKE_FD(pindir_fd);
@@ -490,10 +507,10 @@ int bf_ctx_rm_pindir(void)
490507
_cleanup_close_ int bpffs_fd = -1;
491508
int r;
492509

493-
bpffs_fd = bf_opendir(bf_opts_bpffs_path());
510+
bpffs_fd = bf_opendir(_bf_global_ctx->bpffs_path);
494511
if (bpffs_fd < 0) {
495512
return bf_err_r(bpffs_fd, "failed to open bpffs at %s",
496-
bf_opts_bpffs_path());
513+
_bf_global_ctx->bpffs_path);
497514
}
498515

499516
r = bf_rmdir_at(bpffs_fd, "bpfilter", false);
@@ -507,3 +524,13 @@ const struct bf_elfstub *bf_ctx_get_elfstub(enum bf_elfstub_id id)
507524
{
508525
return _bf_global_ctx->stubs[id];
509526
}
527+
528+
bool bf_ctx_is_transient(void)
529+
{
530+
return _bf_global_ctx->transient;
531+
}
532+
533+
bool bf_ctx_is_verbose(enum bf_verbose opt)
534+
{
535+
return _bf_global_ctx->verbose & BF_FLAG(opt);
536+
}

src/bpfilter/ctx.h

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#pragma once
77

88
#include <stdbool.h>
9+
#include <stdint.h>
910

1011
#include <bpfilter/dump.h>
1112
#include <bpfilter/list.h>
@@ -28,20 +29,30 @@
2829
struct bf_cgen;
2930
struct bf_ns;
3031

32+
enum bf_verbose
33+
{
34+
BF_VERBOSE_DEBUG,
35+
BF_VERBOSE_BPF,
36+
BF_VERBOSE_BYTECODE,
37+
_BF_VERBOSE_MAX,
38+
};
39+
3140
/**
3241
* Initialise the global context.
3342
*
43+
* @param transient If true, don't persist state and unload programs on exit.
44+
* @param with_bpf_token If true, create a BPF token from bpffs.
45+
* @param bpffs_path Path to the bpffs mountpoint. Can't be NULL.
46+
* @param verbose Bitmask of verbose flags.
3447
* @return 0 on success, or a negative errno value on failure.
3548
*/
36-
int bf_ctx_setup(void);
49+
int bf_ctx_setup(bool transient, bool with_bpf_token, const char *bpffs_path,
50+
uint16_t verbose);
3751

3852
/**
3953
* Teardown the global context.
40-
*
41-
* @param clear If true, all the BPF programs will be unloaded before clearing
42-
* the context.
4354
*/
44-
void bf_ctx_teardown(bool clear);
55+
void bf_ctx_teardown(void);
4556

4657
/**
4758
* Dump the global context.
@@ -141,3 +152,13 @@ int bf_ctx_rm_pindir(void);
141152
* @return The requested ELF stub.
142153
*/
143154
const struct bf_elfstub *bf_ctx_get_elfstub(enum bf_elfstub_id id);
155+
156+
/**
157+
* @return true if transient mode is enabled.
158+
*/
159+
bool bf_ctx_is_transient(void);
160+
161+
/**
162+
* @return true if the given verbose flag is set.
163+
*/
164+
bool bf_ctx_is_verbose(enum bf_verbose opt);

0 commit comments

Comments
 (0)