xref: /linux/tools/testing/selftests/bpf/flow_dissector_load.h (revision d003d772e64df08af04ee63609d47169ee82ae0e)
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 				int *prog_fd)
13 {
14 	struct bpf_program *prog, *main_prog;
15 	struct bpf_map *prog_array;
16 	int prog_array_fd;
17 	int ret, fd, i;
18 
19 	ret = bpf_prog_load(path, BPF_PROG_TYPE_FLOW_DISSECTOR, obj,
20 			    prog_fd);
21 	if (ret)
22 		return ret;
23 
24 	main_prog = bpf_object__find_program_by_title(*obj, section_name);
25 	if (!main_prog)
26 		return ret;
27 
28 	*prog_fd = bpf_program__fd(main_prog);
29 	if (*prog_fd < 0)
30 		return ret;
31 
32 	prog_array = bpf_object__find_map_by_name(*obj, map_name);
33 	if (!prog_array)
34 		return ret;
35 
36 	prog_array_fd = bpf_map__fd(prog_array);
37 	if (prog_array_fd < 0)
38 		return ret;
39 
40 	i = 0;
41 	bpf_object__for_each_program(prog, *obj) {
42 		fd = bpf_program__fd(prog);
43 		if (fd < 0)
44 			return fd;
45 
46 		if (fd != *prog_fd) {
47 			bpf_map_update_elem(prog_array_fd, &i, &fd, BPF_ANY);
48 			++i;
49 		}
50 	}
51 
52 	return 0;
53 }
54 
55 #endif /* FLOW_DISSECTOR_LOAD */
56