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
5263static void _bf_ctx_free (struct bf_ctx * * ctx );
5364
5465/// Global daemon context. Hidden in this translation unit.
5566static 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+ }
0 commit comments