1 /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
2 #ifndef FLOW_DISSECTOR_LOAD
3 #define FLOW_DISSECTOR_LOAD
4 
5 #include <bpf/bpf.h>
6 #include <bpf/libbpf.h>
7 
8 static inline int bpf_flow_load(struct bpf_object **obj,
9 				const char *path,
10 				const char *section_name,
11 				const char *map_name,
12 				const char *keys_map_name,
13 				int *prog_fd,
14 				int *keys_fd)
15 {
16 	struct bpf_program *prog, *main_prog;
17 	struct bpf_map *prog_array, *keys;
18 	int prog_array_fd;
19 	int ret, fd, i;
20 
21 	ret = bpf_prog_load(path, BPF_PROG_TYPE_FLOW_DISSECTOR, obj,
22 			    prog_fd);
23 	if (ret)
24 		return ret;
25 
26 	main_prog = NULL;
27 	bpf_object__for_each_program(prog, *obj) {
28 		if (strcmp(section_name, bpf_program__section_name(prog)) == 0) {
29 			main_prog = prog;
30 			break;
31 		}
32 	}
33 	if (!main_prog)
34 		return -1;
35 
36 	*prog_fd = bpf_program__fd(main_prog);
37 	if (*prog_fd < 0)
38 		return -1;
39 
40 	prog_array = bpf_object__find_map_by_name(*obj, map_name);
41 	if (!prog_array)
42 		return -1;
43 
44 	prog_array_fd = bpf_map__fd(prog_array);
45 	if (prog_array_fd < 0)
46 		return -1;
47 
48 	if (keys_map_name && keys_fd) {
49 		keys = bpf_object__find_map_by_name(*obj, keys_map_name);
50 		if (!keys)
51 			return -1;
52 
53 		*keys_fd = bpf_map__fd(keys);
54 		if (*keys_fd < 0)
55 			return -1;
56 	}
57 
58 	i = 0;
59 	bpf_object__for_each_program(prog, *obj) {
60 		fd = bpf_program__fd(prog);
61 		if (fd < 0)
62 			return fd;
63 
64 		if (fd != *prog_fd) {
65 			bpf_map_update_elem(prog_array_fd, &i, &fd, BPF_ANY);
66 			++i;
67 		}
68 	}
69 
70 	return 0;
71 }
72 
73 #endif /* FLOW_DISSECTOR_LOAD */
74