1bf0f0fd9SStanislav Fomichev /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
2bf0f0fd9SStanislav Fomichev #ifndef FLOW_DISSECTOR_LOAD
3bf0f0fd9SStanislav Fomichev #define FLOW_DISSECTOR_LOAD
4bf0f0fd9SStanislav Fomichev
5bf0f0fd9SStanislav Fomichev #include <bpf/bpf.h>
6bf0f0fd9SStanislav Fomichev #include <bpf/libbpf.h>
7*cbdb1461SAndrii Nakryiko #include "testing_helpers.h"
8bf0f0fd9SStanislav Fomichev
bpf_flow_load(struct bpf_object ** obj,const char * path,const char * prog_name,const char * map_name,const char * keys_map_name,int * prog_fd,int * keys_fd)9bf0f0fd9SStanislav Fomichev static inline int bpf_flow_load(struct bpf_object **obj,
10bf0f0fd9SStanislav Fomichev const char *path,
11d1321207SStanislav Fomichev const char *prog_name,
12bf0f0fd9SStanislav Fomichev const char *map_name,
130905beecSStanislav Fomichev const char *keys_map_name,
140905beecSStanislav Fomichev int *prog_fd,
150905beecSStanislav Fomichev int *keys_fd)
16bf0f0fd9SStanislav Fomichev {
17bf0f0fd9SStanislav Fomichev struct bpf_program *prog, *main_prog;
180905beecSStanislav Fomichev struct bpf_map *prog_array, *keys;
19bf0f0fd9SStanislav Fomichev int prog_array_fd;
20bf0f0fd9SStanislav Fomichev int ret, fd, i;
21bf0f0fd9SStanislav Fomichev
22*cbdb1461SAndrii Nakryiko ret = bpf_prog_test_load(path, BPF_PROG_TYPE_FLOW_DISSECTOR, obj,
23bf0f0fd9SStanislav Fomichev prog_fd);
24bf0f0fd9SStanislav Fomichev if (ret)
25bf0f0fd9SStanislav Fomichev return ret;
26bf0f0fd9SStanislav Fomichev
27d1321207SStanislav Fomichev main_prog = bpf_object__find_program_by_name(*obj, prog_name);
28bf0f0fd9SStanislav Fomichev if (!main_prog)
29fe993c64SStanislav Fomichev return -1;
30bf0f0fd9SStanislav Fomichev
31bf0f0fd9SStanislav Fomichev *prog_fd = bpf_program__fd(main_prog);
32bf0f0fd9SStanislav Fomichev if (*prog_fd < 0)
33fe993c64SStanislav Fomichev return -1;
34bf0f0fd9SStanislav Fomichev
35bf0f0fd9SStanislav Fomichev prog_array = bpf_object__find_map_by_name(*obj, map_name);
36bf0f0fd9SStanislav Fomichev if (!prog_array)
37fe993c64SStanislav Fomichev return -1;
38bf0f0fd9SStanislav Fomichev
39bf0f0fd9SStanislav Fomichev prog_array_fd = bpf_map__fd(prog_array);
40bf0f0fd9SStanislav Fomichev if (prog_array_fd < 0)
41fe993c64SStanislav Fomichev return -1;
42bf0f0fd9SStanislav Fomichev
430905beecSStanislav Fomichev if (keys_map_name && keys_fd) {
440905beecSStanislav Fomichev keys = bpf_object__find_map_by_name(*obj, keys_map_name);
450905beecSStanislav Fomichev if (!keys)
460905beecSStanislav Fomichev return -1;
470905beecSStanislav Fomichev
480905beecSStanislav Fomichev *keys_fd = bpf_map__fd(keys);
490905beecSStanislav Fomichev if (*keys_fd < 0)
500905beecSStanislav Fomichev return -1;
510905beecSStanislav Fomichev }
520905beecSStanislav Fomichev
53bf0f0fd9SStanislav Fomichev i = 0;
54bf0f0fd9SStanislav Fomichev bpf_object__for_each_program(prog, *obj) {
55bf0f0fd9SStanislav Fomichev fd = bpf_program__fd(prog);
56bf0f0fd9SStanislav Fomichev if (fd < 0)
57bf0f0fd9SStanislav Fomichev return fd;
58bf0f0fd9SStanislav Fomichev
59bf0f0fd9SStanislav Fomichev if (fd != *prog_fd) {
60bf0f0fd9SStanislav Fomichev bpf_map_update_elem(prog_array_fd, &i, &fd, BPF_ANY);
61bf0f0fd9SStanislav Fomichev ++i;
62bf0f0fd9SStanislav Fomichev }
63bf0f0fd9SStanislav Fomichev }
64bf0f0fd9SStanislav Fomichev
65bf0f0fd9SStanislav Fomichev return 0;
66bf0f0fd9SStanislav Fomichev }
67bf0f0fd9SStanislav Fomichev
68bf0f0fd9SStanislav Fomichev #endif /* FLOW_DISSECTOR_LOAD */
69