1 // SPDX-License-Identifier: GPL-2.0
12 #include <bpf/libbpf.h>
14 #include "bpf_rlimit.h"
15 #include "flow_dissector_load.h"
17 const char *cfg_pin_path
= "/sys/fs/bpf/flow_dissector";
18 const char *cfg_map_name
= "jmp_table";
19 bool cfg_attach
= true;
20 char *cfg_section_name
;
23 static void load_and_attach_program(void)
26 struct bpf_object
*obj
;
28 ret
= bpf_flow_load(&obj
, cfg_path_name
, cfg_section_name
,
29 cfg_map_name
, NULL
, &prog_fd
, NULL
);
31 error(1, 0, "bpf_flow_load %s", cfg_path_name
);
33 ret
= bpf_prog_attach(prog_fd
, 0 /* Ignore */, BPF_FLOW_DISSECTOR
, 0);
35 error(1, 0, "bpf_prog_attach %s", cfg_path_name
);
37 ret
= bpf_object__pin(obj
, cfg_pin_path
);
39 error(1, 0, "bpf_object__pin %s", cfg_pin_path
);
42 static void detach_program(void)
47 ret
= bpf_prog_detach(0, BPF_FLOW_DISSECTOR
);
49 error(1, 0, "bpf_prog_detach");
51 /* To unpin, it is necessary and sufficient to just remove this dir */
52 sprintf(command
, "rm -r %s", cfg_pin_path
);
53 ret
= system(command
);
55 error(1, errno
, "%s", command
);
58 static void parse_opts(int argc
, char **argv
)
64 while ((c
= getopt(argc
, argv
, "adp:s:")) != -1) {
68 error(1, 0, "attach/detach are exclusive");
73 error(1, 0, "attach/detach are exclusive");
78 error(1, 0, "only one prog name can be given");
80 cfg_path_name
= optarg
;
84 error(1, 0, "only one section can be given");
86 cfg_section_name
= optarg
;
94 if (cfg_attach
&& !cfg_path_name
)
95 error(1, 0, "must provide a path to the BPF program");
97 if (cfg_attach
&& !cfg_section_name
)
98 error(1, 0, "must provide a section name");
101 int main(int argc
, char **argv
)
103 parse_opts(argc
, argv
);
105 load_and_attach_program();