1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2020 Facebook */
12 #include <bpf/libbpf.h>
14 #include "json_writer.h"
17 #define STRUCT_OPS_VALUE_PREFIX "bpf_struct_ops_"
19 static const struct btf_type *map_info_type;
20 static __u32 map_info_alloc_len;
21 static struct btf *btf_vmlinux;
22 static __s32 map_info_type_id;
29 static const struct btf *get_btf_vmlinux(void)
34 btf_vmlinux = libbpf_find_kernel_btf();
36 p_err("struct_ops requires kernel CONFIG_DEBUG_INFO_BTF=y");
41 static const char *get_kern_struct_ops_name(const struct bpf_map_info *info)
43 const struct btf *kern_btf;
44 const struct btf_type *t;
45 const char *st_ops_name;
47 kern_btf = get_btf_vmlinux();
49 return "<btf_vmlinux_not_found>";
51 t = btf__type_by_id(kern_btf, info->btf_vmlinux_value_type_id);
52 st_ops_name = btf__name_by_offset(kern_btf, t->name_off);
53 st_ops_name += strlen(STRUCT_OPS_VALUE_PREFIX);
58 static __s32 get_map_info_type_id(void)
60 const struct btf *kern_btf;
63 return map_info_type_id;
65 kern_btf = get_btf_vmlinux();
69 map_info_type_id = btf__find_by_name_kind(kern_btf, "bpf_map_info",
71 if (map_info_type_id < 0) {
72 p_err("can't find bpf_map_info from btf_vmlinux");
73 return map_info_type_id;
75 map_info_type = btf__type_by_id(kern_btf, map_info_type_id);
77 /* Ensure map_info_alloc() has at least what the bpftool needs */
78 map_info_alloc_len = map_info_type->size;
79 if (map_info_alloc_len < sizeof(struct bpf_map_info))
80 map_info_alloc_len = sizeof(struct bpf_map_info);
82 return map_info_type_id;
85 /* If the subcmd needs to print out the bpf_map_info,
86 * it should always call map_info_alloc to allocate
87 * a bpf_map_info object instead of allocating it
90 * map_info_alloc() will take the running kernel's btf
91 * into account. i.e. it will consider the
92 * sizeof(struct bpf_map_info) of the running kernel.
94 * It will enable the "struct_ops" cmd to print the latest
95 * "struct bpf_map_info".
97 * [ Recall that "struct_ops" requires the kernel's btf to
100 static struct bpf_map_info *map_info_alloc(__u32 *alloc_len)
102 struct bpf_map_info *info;
104 if (get_map_info_type_id() < 0)
107 info = calloc(1, map_info_alloc_len);
109 p_err("mem alloc failed");
111 *alloc_len = map_info_alloc_len;
116 /* It iterates all struct_ops maps of the system.
117 * It returns the fd in "*res_fd" and map_info in "*info".
118 * In the very first iteration, info->id should be 0.
119 * An optional map "*name" filter can be specified.
120 * The filter can be made more flexible in the future.
121 * e.g. filter by kernel-struct-ops-name, regex-name, glob-name, ...etc.
124 * 1: A struct_ops map found. It is returned in "*res_fd" and "*info".
125 * The caller can continue to call get_next in the future.
126 * 0: No struct_ops map is returned.
127 * All struct_ops map has been found.
128 * -1: Error and the caller should abort the iteration.
130 static int get_next_struct_ops_map(const char *name, int *res_fd,
131 struct bpf_map_info *info, __u32 info_len)
137 err = bpf_map_get_next_id(id, &id);
141 p_err("can't get next map: %s", strerror(errno));
145 fd = bpf_map_get_fd_by_id(id);
149 p_err("can't get map by id (%u): %s",
150 id, strerror(errno));
154 err = bpf_map_get_info_by_fd(fd, info, &info_len);
156 p_err("can't get map info: %s", strerror(errno));
161 if (info->type == BPF_MAP_TYPE_STRUCT_OPS &&
162 (!name || !strcmp(name, info->name))) {
170 static int cmd_retval(const struct res *res, bool must_have_one_map)
172 if (res->nr_errs || (!res->nr_maps && must_have_one_map))
178 /* "data" is the work_func private storage */
179 typedef int (*work_func)(int fd, const struct bpf_map_info *info, void *data,
180 struct json_writer *wtr);
182 /* Find all struct_ops map in the system.
183 * Filter out by "name" (if specified).
184 * Then call "func(fd, info, data, wtr)" on each struct_ops map found.
186 static struct res do_search(const char *name, work_func func, void *data,
187 struct json_writer *wtr)
189 struct bpf_map_info *info;
194 info = map_info_alloc(&info_len);
201 jsonw_start_array(wtr);
202 while ((err = get_next_struct_ops_map(name, &fd, info, info_len)) == 1) {
204 err = func(fd, info, data, wtr);
210 jsonw_end_array(wtr);
215 if (!wtr && name && !res.nr_errs && !res.nr_maps)
216 /* It is not printing empty [].
217 * Thus, needs to specifically say nothing found
220 p_err("no struct_ops found for %s", name);
221 else if (!wtr && json_output && !res.nr_errs)
222 /* The "func()" above is not writing any json (i.e. !wtr
225 * However, "-j" is enabled and there is no errs here,
226 * so call json_null() as the current convention of
229 jsonw_null(json_wtr);
235 static struct res do_one_id(const char *id_str, work_func func, void *data,
236 struct json_writer *wtr)
238 struct bpf_map_info *info;
245 id = strtoul(id_str, &endptr, 0);
246 if (*endptr || !id || id > UINT32_MAX) {
247 p_err("invalid id %s", id_str);
252 fd = bpf_map_get_fd_by_id(id);
254 p_err("can't get map by id (%lu): %s", id, strerror(errno));
259 info = map_info_alloc(&info_len);
265 if (bpf_map_get_info_by_fd(fd, info, &info_len)) {
266 p_err("can't get map info: %s", strerror(errno));
271 if (info->type != BPF_MAP_TYPE_STRUCT_OPS) {
272 p_err("%s id %u is not a struct_ops map", info->name, info->id);
279 if (func(fd, info, data, wtr))
281 else if (!wtr && json_output)
282 /* The "func()" above is not writing any json (i.e. !wtr
285 * However, "-j" is enabled and there is no errs here,
286 * so call json_null() as the current convention of
289 jsonw_null(json_wtr);
298 static struct res do_work_on_struct_ops(const char *search_type,
299 const char *search_term,
300 work_func func, void *data,
301 struct json_writer *wtr)
304 if (is_prefix(search_type, "id"))
305 return do_one_id(search_term, func, data, wtr);
306 else if (!is_prefix(search_type, "name"))
310 return do_search(search_term, func, data, wtr);
313 static int __do_show(int fd, const struct bpf_map_info *info, void *data,
314 struct json_writer *wtr)
317 jsonw_start_object(wtr);
318 jsonw_uint_field(wtr, "id", info->id);
319 jsonw_string_field(wtr, "name", info->name);
320 jsonw_string_field(wtr, "kernel_struct_ops",
321 get_kern_struct_ops_name(info));
322 jsonw_end_object(wtr);
324 printf("%u: %-15s %-32s\n", info->id, info->name,
325 get_kern_struct_ops_name(info));
331 static int do_show(int argc, char **argv)
333 const char *search_type = NULL, *search_term = NULL;
336 if (argc && argc != 2)
340 search_type = GET_ARG();
341 search_term = GET_ARG();
344 res = do_work_on_struct_ops(search_type, search_term, __do_show,
347 return cmd_retval(&res, !!search_term);
350 static int __do_dump(int fd, const struct bpf_map_info *info, void *data,
351 struct json_writer *wtr)
353 struct btf_dumper *d = (struct btf_dumper *)data;
354 const struct btf_type *struct_ops_type;
355 const struct btf *kern_btf = d->btf;
356 const char *struct_ops_name;
360 /* note: d->jw == wtr */
364 /* The kernel supporting BPF_MAP_TYPE_STRUCT_OPS must have
365 * btf_vmlinux_value_type_id.
367 struct_ops_type = btf__type_by_id(kern_btf,
368 info->btf_vmlinux_value_type_id);
369 struct_ops_name = btf__name_by_offset(kern_btf,
370 struct_ops_type->name_off);
371 value = calloc(1, info->value_size);
373 p_err("mem alloc failed");
377 if (bpf_map_lookup_elem(fd, &zero, value)) {
378 p_err("can't lookup struct_ops map %s id %u",
379 info->name, info->id);
384 jsonw_start_object(wtr);
385 jsonw_name(wtr, "bpf_map_info");
386 btf_dumper_type(d, map_info_type_id, (void *)info);
387 jsonw_end_object(wtr);
389 jsonw_start_object(wtr);
390 jsonw_name(wtr, struct_ops_name);
391 btf_dumper_type(d, info->btf_vmlinux_value_type_id, value);
392 jsonw_end_object(wtr);
399 static int do_dump(int argc, char **argv)
401 const char *search_type = NULL, *search_term = NULL;
402 json_writer_t *wtr = json_wtr;
403 const struct btf *kern_btf;
404 struct btf_dumper d = {};
407 if (argc && argc != 2)
411 search_type = GET_ARG();
412 search_term = GET_ARG();
415 kern_btf = get_btf_vmlinux();
420 wtr = jsonw_new(stdout);
422 p_err("can't create json writer");
425 jsonw_pretty(wtr, true);
430 d.is_plain_text = !json_output;
431 d.prog_id_as_func_ptr = true;
433 res = do_work_on_struct_ops(search_type, search_term, __do_dump, &d,
439 return cmd_retval(&res, !!search_term);
442 static int __do_unregister(int fd, const struct bpf_map_info *info, void *data,
443 struct json_writer *wtr)
447 if (bpf_map_delete_elem(fd, &zero)) {
448 p_err("can't unload %s %s id %u: %s",
449 get_kern_struct_ops_name(info), info->name,
450 info->id, strerror(errno));
454 p_info("Unregistered %s %s id %u",
455 get_kern_struct_ops_name(info), info->name,
461 static int do_unregister(int argc, char **argv)
463 const char *search_type, *search_term;
469 search_type = GET_ARG();
470 search_term = GET_ARG();
472 res = do_work_on_struct_ops(search_type, search_term,
473 __do_unregister, NULL, NULL);
475 return cmd_retval(&res, true);
478 static int pin_link(struct bpf_link *link, const char *pindir,
481 char pinfile[PATH_MAX];
484 err = pathname_concat(pinfile, sizeof(pinfile), pindir, name);
488 return bpf_link__pin(link, pinfile);
491 static int do_register(int argc, char **argv)
493 LIBBPF_OPTS(bpf_object_open_opts, open_opts);
494 __u32 link_info_len = sizeof(struct bpf_link_info);
495 struct bpf_link_info link_info = {};
496 struct bpf_map_info info = {};
497 __u32 info_len = sizeof(info);
498 int nr_errs = 0, nr_maps = 0;
499 const char *linkdir = NULL;
500 struct bpf_object *obj;
501 struct bpf_link *link;
505 if (argc != 1 && argc != 2)
512 if (linkdir && mount_bpffs_for_pin(linkdir, true)) {
513 p_err("can't mount bpffs for pinning");
518 /* log_level1 + log_level2 + stats, but not stable UAPI */
519 open_opts.kernel_log_level = 1 + 2 + 4;
521 obj = bpf_object__open_file(file, &open_opts);
527 if (bpf_object__load(obj)) {
528 bpf_object__close(obj);
532 bpf_object__for_each_map(map, obj) {
533 if (bpf_map__type(map) != BPF_MAP_TYPE_STRUCT_OPS)
536 link = bpf_map__attach_struct_ops(map);
538 p_err("can't register struct_ops %s: %s",
539 bpf_map__name(map), strerror(errno));
545 if (bpf_map_get_info_by_fd(bpf_map__fd(map), &info,
547 /* Not p_err. The struct_ops was attached
550 p_info("Registered %s but can't find id: %s",
551 bpf_map__name(map), strerror(errno));
554 if (!(bpf_map__map_flags(map) & BPF_F_LINK)) {
555 p_info("Registered %s %s id %u",
556 get_kern_struct_ops_name(&info),
561 if (bpf_link_get_info_by_fd(bpf_link__fd(link),
564 p_err("Registered %s but can't find link id: %s",
565 bpf_map__name(map), strerror(errno));
569 if (linkdir && pin_link(link, linkdir, info.name)) {
570 p_err("can't pin link %u for %s: %s",
571 link_info.id, info.name,
576 p_info("Registered %s %s map id %u link id %u",
577 get_kern_struct_ops_name(&info),
578 info.name, info.id, link_info.id);
581 bpf_link__disconnect(link);
582 bpf_link__destroy(link);
585 bpf_object__close(obj);
591 p_err("no struct_ops found in %s", file);
596 jsonw_null(json_wtr);
601 static int do_help(int argc, char **argv)
604 jsonw_null(json_wtr);
609 "Usage: %1$s %2$s { show | list } [STRUCT_OPS_MAP]\n"
610 " %1$s %2$s dump [STRUCT_OPS_MAP]\n"
611 " %1$s %2$s register OBJ [LINK_DIR]\n"
612 " %1$s %2$s unregister STRUCT_OPS_MAP\n"
615 " STRUCT_OPS_MAP := [ id STRUCT_OPS_MAP_ID | name STRUCT_OPS_MAP_NAME ]\n"
616 " " HELP_SPEC_OPTIONS " }\n"
623 static const struct cmd cmds[] = {
626 { "register", do_register },
627 { "unregister", do_unregister },
633 int do_struct_ops(int argc, char **argv)
637 err = cmd_select(cmds, argc, argv, do_help);
639 btf__free(btf_vmlinux);