Debian Codegraph @

lib/bpf_libbpf.c

raw
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * bpf_libbpf.c BPF code relay on libbpf
4 * Authors: Hangbin Liu <[email protected]>
5 *
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <stdbool.h>
13 #include <stdint.h>
14 #include <errno.h>
15 #include <fcntl.h>
16 #include <limits.h>
17
18 #include <libelf.h>
19 #include <gelf.h>
20
21 #include <bpf/libbpf.h>
22 #include <bpf/bpf.h>
23
24 #include "bpf_util.h"
25
26 static int __attribute__((format(printf, 2, 0)))
27 verbose_print(enum libbpf_print_level level, const char *format, va_list args)
28 {
29 return vfprintf(stderr, format, args);
30 }
31
32 static int __attribute__((format(printf, 2, 0)))
33 silent_print(enum libbpf_print_level level, const char *format, va_list args)
34 {
35 if (level > LIBBPF_WARN)
36 return 0;
37
38 /* Skip warning from bpf_object__init_user_maps() for legacy maps */
39 if (strstr(format, "has unrecognized, non-zero options"))
40 return 0;
41
42 return vfprintf(stderr, format, args);
43 }
44
45 static const char *get_bpf_program__section_name(const struct bpf_program *prog)
46 {
47 #ifdef HAVE_LIBBPF_SECTION_NAME
48 return bpf_program__section_name(prog);
49 #else
50 return bpf_program__title(prog, false);
51 #endif
52 }
53
54 static int create_map(const char *name, struct bpf_elf_map *map,
55 __u32 ifindex, int inner_fd)
56 {
57 union bpf_attr attr = {};
58
59 attr.map_type = map->type;
60 strlcpy(attr.map_name, name, sizeof(attr.map_name));
61 attr.map_flags = map->flags;
62 attr.key_size = map->size_key;
63 attr.value_size = map->size_value;
64 attr.max_entries = map->max_elem;
65 attr.map_ifindex = ifindex;
66 attr.inner_map_fd = inner_fd;
67
68 return bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
69 }
70
71 static int create_map_in_map(struct bpf_object *obj, struct bpf_map *map,
72 struct bpf_elf_map *elf_map, int inner_fd,
73 bool *reuse_pin_map)
74 {
75 char pathname[PATH_MAX];
76 const char *map_name;
77 bool pin_map = false;
78 int map_fd, ret = 0;
79
80 map_name = bpf_map__name(map);
81
82 if (iproute2_is_pin_map(map_name, pathname)) {
83 pin_map = true;
84
85 /* Check if there already has a pinned map */
86 map_fd = bpf_obj_get(pathname);
87 if (map_fd > 0) {
88 if (reuse_pin_map)
89 *reuse_pin_map = true;
90 close(map_fd);
91 return bpf_map__set_pin_path(map, pathname);
92 }
93 }
94
95 map_fd = create_map(map_name, elf_map, bpf_map__ifindex(map), inner_fd);
96 if (map_fd < 0) {
97 fprintf(stderr, "create map %s failed\n", map_name);
98 return map_fd;
99 }
100
101 ret = bpf_map__reuse_fd(map, map_fd);
102 if (ret < 0) {
103 fprintf(stderr, "map %s reuse fd failed\n", map_name);
104 goto err_out;
105 }
106
107 if (pin_map) {
108 ret = bpf_map__set_pin_path(map, pathname);
109 if (ret < 0)
110 goto err_out;
111 }
112
113 return 0;
114 err_out:
115 close(map_fd);
116 return ret;
117 }
118
119 static int
120 handle_legacy_map_in_map(struct bpf_object *obj, struct bpf_map *inner_map,
121 const char *inner_map_name)
122 {
123 int inner_fd, outer_fd, inner_idx, ret = 0;
124 struct bpf_elf_map imap, omap;
125 struct bpf_map *outer_map;
126 /* What's the size limit of map name? */
127 char outer_map_name[128];
128 bool reuse_pin_map = false;
129
130 /* Deal with map-in-map */
131 if (iproute2_is_map_in_map(inner_map_name, &imap, &omap, outer_map_name)) {
132 ret = create_map_in_map(obj, inner_map, &imap, -1, NULL);
133 if (ret < 0)
134 return ret;
135
136 inner_fd = bpf_map__fd(inner_map);
137 outer_map = bpf_object__find_map_by_name(obj, outer_map_name);
138 ret = create_map_in_map(obj, outer_map, &omap, inner_fd, &reuse_pin_map);
139 if (ret < 0)
140 return ret;
141
142 if (!reuse_pin_map) {
143 inner_idx = imap.inner_idx;
144 outer_fd = bpf_map__fd(outer_map);
145 ret = bpf_map_update_elem(outer_fd, &inner_idx, &inner_fd, 0);
146 if (ret < 0)
147 fprintf(stderr, "Cannot update inner_idx into outer_map\n");
148 }
149 }
150
151 return ret;
152 }
153
154 static int find_legacy_tail_calls(struct bpf_program *prog, struct bpf_object *obj,
155 struct bpf_map **pmap)
156 {
157 unsigned int map_id, key_id;
158 const char *sec_name;
159 struct bpf_map *map;
160 char map_name[128];
161 int ret;
162
163 /* Handle iproute2 tail call */
164 sec_name = get_bpf_program__section_name(prog);
165 ret = sscanf(sec_name, "%i/%i", &map_id, &key_id);
166 if (ret != 2)
167 return -1;
168
169 ret = iproute2_find_map_name_by_id(map_id, map_name);
170 if (ret < 0) {
171 fprintf(stderr, "unable to find map id %u for tail call\n", map_id);
172 return ret;
173 }
174
175 map = bpf_object__find_map_by_name(obj, map_name);
176 if (!map)
177 return -1;
178
179 if (pmap)
180 *pmap = map;
181
182 return 0;
183 }
184
185 static int update_legacy_tail_call_maps(struct bpf_object *obj)
186 {
187 int prog_fd, map_fd, ret = 0;
188 unsigned int map_id, key_id;
189 struct bpf_program *prog;
190 const char *sec_name;
191 struct bpf_map *map;
192
193 bpf_object__for_each_program(prog, obj) {
194 /* load_bpf_object has already verified find_legacy_tail_calls
195 * succeeds when it should
196 */
197 if (find_legacy_tail_calls(prog, obj, &map) < 0)
198 continue;
199
200 prog_fd = bpf_program__fd(prog);
201 if (prog_fd < 0)
202 continue;
203
204 sec_name = get_bpf_program__section_name(prog);
205 ret = sscanf(sec_name, "%i/%i", &map_id, &key_id);
206 if (ret != 2)
207 continue;
208
209 map_fd = bpf_map__fd(map);
210 ret = bpf_map_update_elem(map_fd, &key_id, &prog_fd, 0);
211 if (ret < 0) {
212 fprintf(stderr, "Cannot update map key for tail call!\n");
213 return ret;
214 }
215 }
216
217 return 0;
218 }
219
220 static int handle_legacy_maps(struct bpf_object *obj)
221 {
222 char pathname[PATH_MAX];
223 struct bpf_map *map;
224 const char *map_name;
225 int map_fd, ret = 0;
226
227 bpf_object__for_each_map(map, obj) {
228 map_name = bpf_map__name(map);
229
230 ret = handle_legacy_map_in_map(obj, map, map_name);
231 if (ret)
232 return ret;
233
234 /* If it is a iproute2 legacy pin maps, just set pin path
235 * and let bpf_object__load() to deal with the map creation.
236 * We need to ignore map-in-maps which have pinned maps manually
237 */
238 map_fd = bpf_map__fd(map);
239 if (map_fd < 0 && iproute2_is_pin_map(map_name, pathname)) {
240 ret = bpf_map__set_pin_path(map, pathname);
241 if (ret) {
242 fprintf(stderr, "map '%s': couldn't set pin path.\n", map_name);
243 break;
244 }
245 }
246
247 }
248
249 return ret;
250 }
251
252 static bool bpf_map_is_offload_neutral(const struct bpf_map *map)
253 {
254 return bpf_map__type(map) == BPF_MAP_TYPE_PERF_EVENT_ARRAY;
255 }
256
257 static bool find_prog_to_attach(struct bpf_program *prog,
258 struct bpf_program *exist_prog,
259 const char *section, const char *prog_name)
260 {
261 if (exist_prog)
262 return false;
263
264 /* We have default section name 'prog'. So do not check
265 * section name if there already has program name.
266 */
267 if (prog_name)
268 return !strcmp(bpf_program__name(prog), prog_name);
269 else
270 return !strcmp(get_bpf_program__section_name(prog), section);
271 }
272
273 static int load_bpf_object(struct bpf_cfg_in *cfg)
274 {
275 struct bpf_program *p, *prog = NULL;
276 struct bpf_object *obj;
277 char root_path[PATH_MAX];
278 struct bpf_map *map;
279 int prog_fd, ret = 0;
280
281 ret = iproute2_get_root_path(root_path, PATH_MAX);
282 if (ret)
283 return ret;
284
285 DECLARE_LIBBPF_OPTS(bpf_object_open_opts, open_opts,
286 .relaxed_maps = true,
287 .pin_root_path = root_path,
288 );
289
290 #if (LIBBPF_MAJOR_VERSION > 0) || (LIBBPF_MINOR_VERSION >= 7)
291 open_opts.kernel_log_level = 1;
292 if (cfg->verbose)
293 open_opts.kernel_log_level |= 2;
294 #endif
295
296 obj = bpf_object__open_file(cfg->object, &open_opts);
297 if (libbpf_get_error(obj)) {
298 fprintf(stderr, "ERROR: opening BPF object file failed\n");
299 return -ENOENT;
300 }
301
302 bpf_object__for_each_program(p, obj) {
303 bool prog_to_attach = find_prog_to_attach(p, prog,
304 cfg->section,
305 cfg->prog_name);
306
307 /* Only load the programs that will either be subsequently
308 * attached or inserted into a tail call map */
309 if (find_legacy_tail_calls(p, obj, NULL) < 0 &&
310 !prog_to_attach) {
311 ret = bpf_program__set_autoload(p, false);
312 if (ret)
313 return -EINVAL;
314 continue;
315 }
316
317 bpf_program__set_type(p, cfg->type);
318 bpf_program__set_ifindex(p, cfg->ifindex);
319
320 if (prog_to_attach)
321 prog = p;
322 }
323
324 bpf_object__for_each_map(map, obj) {
325 if (!bpf_map_is_offload_neutral(map))
326 bpf_map__set_ifindex(map, cfg->ifindex);
327 }
328
329 if (!prog) {
330 if (cfg->prog_name)
331 fprintf(stderr, "object file doesn't contain prog %s\n", cfg->prog_name);
332 else
333 fprintf(stderr, "object file doesn't contain sec %s\n", cfg->section);
334 return -ENOENT;
335 }
336
337 /* Handle iproute2 legacy pin maps and map-in-maps */
338 ret = handle_legacy_maps(obj);
339 if (ret)
340 goto unload_obj;
341
342 ret = bpf_object__load(obj);
343 if (ret)
344 goto unload_obj;
345
346 ret = update_legacy_tail_call_maps(obj);
347 if (ret)
348 goto unload_obj;
349
350 prog_fd = fcntl(bpf_program__fd(prog), F_DUPFD_CLOEXEC, 1);
351 if (prog_fd < 0)
352 ret = -errno;
353 else
354 cfg->prog_fd = prog_fd;
355
356 unload_obj:
357 /* Close obj as we don't need it */
358 bpf_object__close(obj);
359 return ret;
360 }
361
362 /* Load ebpf and return prog fd */
363 int iproute2_load_libbpf(struct bpf_cfg_in *cfg)
364 {
365 int ret = 0;
366
367 if (cfg->verbose)
368 libbpf_set_print(verbose_print);
369 else
370 libbpf_set_print(silent_print);
371
372 ret = iproute2_bpf_elf_ctx_init(cfg);
373 if (ret < 0) {
374 fprintf(stderr, "Cannot initialize ELF context!\n");
375 return ret;
376 }
377
378 ret = iproute2_bpf_fetch_ancillary();
379 if (ret < 0) {
380 fprintf(stderr, "Error fetching ELF ancillary data!\n");
381 return ret;
382 }
383
384 ret = load_bpf_object(cfg);
385 if (ret)
386 return ret;
387
388 return cfg->prog_fd;
389 }