Skip to content

Commit 3d54132

Browse files
committed
cli/run: allow passing flags when loading XDP program
CLI and config file options are to be added.
1 parent 52ffc16 commit 3d54132

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/run.c

+13-8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <fcntl.h>
77
#include <ffi.h>
88
#include <linux/bpf.h>
9+
#include <linux/if_link.h>
910
#include <linux/tcp.h>
1011
#include <linux/types.h>
1112
#include <net/if.h>
@@ -112,8 +113,8 @@ static inline int tc_hook_cleanup(struct bpf_tc_hook* hook, struct bpf_tc_opts*
112113
return ret ?: bpf_tc_hook_destroy(hook);
113114
}
114115

115-
static inline int tc_hook_create_bind(struct bpf_tc_hook* hook, struct bpf_tc_opts* opts,
116-
const struct bpf_program* prog) {
116+
static inline int tc_hook_create_attach(struct bpf_tc_hook* hook, struct bpf_tc_opts* opts,
117+
const struct bpf_program* prog) {
117118
// EEXIST causes libbpf_print_fn to log harmless 'libbpf: Kernel error message: Exclusivity flag
118119
// on, cannot modify'
119120
int retcode = bpf_tc_hook_create(hook);
@@ -463,9 +464,9 @@ static inline int run_bpf(struct run_args* args, int lock_fd, const char* ifname
463464
int mimic_whitelist_fd = -1, mimic_conns_fd = -1, mimic_rb_fd = -1;
464465

465466
bool tc_hook_created = false;
467+
bool xdp_attached = false;
466468
struct bpf_tc_hook tc_hook_egress;
467469
struct bpf_tc_opts tc_opts_egress;
468-
struct bpf_link* xdp_ingress = NULL;
469470
struct ring_buffer* rb = NULL;
470471
ffi_closure* closure = NULL;
471472
ffi_cif cif;
@@ -526,15 +527,19 @@ static inline int run_bpf(struct run_args* args, int lock_fd, const char* ifname
526527
rb = try2_p(ring_buffer__new(mimic_rb_fd, handle_rb_event(&ctx, &cif, &closure), NULL, NULL),
527528
_("failed to attach BPF ring buffer '%s': %s"), "mimic_rb", strret);
528529

529-
// TC and XDP
530+
// TC
530531
tc_hook_egress = (typeof(tc_hook_egress)){
531532
.sz = sizeof(tc_hook_egress), .ifindex = ifindex, .attach_point = BPF_TC_EGRESS};
532533
tc_opts_egress =
533534
(typeof(tc_opts_egress)){.sz = sizeof(tc_opts_egress), .handle = 1, .priority = 1};
534535
tc_hook_created = true;
535-
try2(tc_hook_create_bind(&tc_hook_egress, &tc_opts_egress, skel->progs.egress_handler));
536-
xdp_ingress = try2_p(bpf_program__attach_xdp(skel->progs.ingress_handler, ifindex),
537-
_("failed to attach XDP program: %s"), strret);
536+
try2(tc_hook_create_attach(&tc_hook_egress, &tc_opts_egress, skel->progs.egress_handler));
537+
538+
// XDP
539+
// TODO: pass flags
540+
try2(bpf_xdp_attach(ifindex, bpf_program__fd(skel->progs.ingress_handler), 0 , NULL),
541+
_("failed to attach XDP program: %s"), strret);
542+
xdp_attached = true;
538543

539544
retcode = notify_ready();
540545
if (retcode < 0)
@@ -613,7 +618,7 @@ static inline int run_bpf(struct run_args* args, int lock_fd, const char* ifname
613618
terminate_all_conns(mimic_conns_fd, ifname);
614619
sigprocmask(SIG_SETMASK, NULL, NULL);
615620
if (tc_hook_created) tc_hook_cleanup(&tc_hook_egress, &tc_opts_egress);
616-
if (xdp_ingress) bpf_link__destroy(xdp_ingress);
621+
if (xdp_attached) bpf_xdp_detach(ifindex, 0, NULL);
617622
if (rb) ring_buffer__free(rb);
618623
if (closure) ffi_closure_free(closure);
619624
if (skel) mimic_bpf__destroy(skel);

0 commit comments

Comments
 (0)