1 // SPDX-License-Identifier: GPL-2.0
12 #include <bpf/libbpf.h>
14 #include "flow_dissector_load.h"
16 const char *cfg_pin_path
= "/sys/fs/bpf/flow_dissector";
17 const char *cfg_map_name
= "jmp_table";
18 bool cfg_attach
= true;
22 static void load_and_attach_program(void)
25 struct bpf_object
*obj
;
27 /* Use libbpf 1.0 API mode */
28 libbpf_set_strict_mode(LIBBPF_STRICT_ALL
);
30 ret
= bpf_flow_load(&obj
, cfg_path_name
, cfg_prog_name
,
31 cfg_map_name
, NULL
, &prog_fd
, NULL
);
33 error(1, 0, "bpf_flow_load %s", cfg_path_name
);
35 ret
= bpf_prog_attach(prog_fd
, 0 /* Ignore */, BPF_FLOW_DISSECTOR
, 0);
37 error(1, 0, "bpf_prog_attach %s", cfg_path_name
);
39 ret
= bpf_object__pin(obj
, cfg_pin_path
);
41 error(1, 0, "bpf_object__pin %s", cfg_pin_path
);
44 static void detach_program(void)
49 ret
= bpf_prog_detach(0, BPF_FLOW_DISSECTOR
);
51 error(1, 0, "bpf_prog_detach");
53 /* To unpin, it is necessary and sufficient to just remove this dir */
54 sprintf(command
, "rm -r %s", cfg_pin_path
);
55 ret
= system(command
);
57 error(1, errno
, "%s", command
);
60 static void parse_opts(int argc
, char **argv
)
66 while ((c
= getopt(argc
, argv
, "adp:s:")) != -1) {
70 error(1, 0, "attach/detach are exclusive");
75 error(1, 0, "attach/detach are exclusive");
80 error(1, 0, "only one path can be given");
82 cfg_path_name
= optarg
;
86 error(1, 0, "only one prog can be given");
88 cfg_prog_name
= optarg
;
96 if (cfg_attach
&& !cfg_path_name
)
97 error(1, 0, "must provide a path to the BPF program");
99 if (cfg_attach
&& !cfg_prog_name
)
100 error(1, 0, "must provide a section name");
103 int main(int argc
, char **argv
)
105 parse_opts(argc
, argv
);
107 load_and_attach_program();