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();
35 if (IS_ERR(btf_vmlinux))
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();
66 if (IS_ERR(kern_btf)) {
67 map_info_type_id = PTR_ERR(kern_btf);
68 return map_info_type_id;
71 map_info_type_id = btf__find_by_name_kind(kern_btf, "bpf_map_info",
73 if (map_info_type_id < 0) {
74 p_err("can't find bpf_map_info from btf_vmlinux");
75 return map_info_type_id;
77 map_info_type = btf__type_by_id(kern_btf, map_info_type_id);
79 /* Ensure map_info_alloc() has at least what the bpftool needs */
80 map_info_alloc_len = map_info_type->size;
81 if (map_info_alloc_len < sizeof(struct bpf_map_info))
82 map_info_alloc_len = sizeof(struct bpf_map_info);
84 return map_info_type_id;
87 /* If the subcmd needs to print out the bpf_map_info,
88 * it should always call map_info_alloc to allocate
89 * a bpf_map_info object instead of allocating it
92 * map_info_alloc() will take the running kernel's btf
93 * into account. i.e. it will consider the
94 * sizeof(struct bpf_map_info) of the running kernel.
96 * It will enable the "struct_ops" cmd to print the latest
97 * "struct bpf_map_info".
99 * [ Recall that "struct_ops" requires the kernel's btf to
102 static struct bpf_map_info *map_info_alloc(__u32 *alloc_len)
104 struct bpf_map_info *info;
106 if (get_map_info_type_id() < 0)
109 info = calloc(1, map_info_alloc_len);
111 p_err("mem alloc failed");
113 *alloc_len = map_info_alloc_len;
118 /* It iterates all struct_ops maps of the system.
119 * It returns the fd in "*res_fd" and map_info in "*info".
120 * In the very first iteration, info->id should be 0.
121 * An optional map "*name" filter can be specified.
122 * The filter can be made more flexible in the future.
123 * e.g. filter by kernel-struct-ops-name, regex-name, glob-name, ...etc.
126 * 1: A struct_ops map found. It is returned in "*res_fd" and "*info".
127 * The caller can continue to call get_next in the future.
128 * 0: No struct_ops map is returned.
129 * All struct_ops map has been found.
130 * -1: Error and the caller should abort the iteration.
132 static int get_next_struct_ops_map(const char *name, int *res_fd,
133 struct bpf_map_info *info, __u32 info_len)
139 err = bpf_map_get_next_id(id, &id);
143 p_err("can't get next map: %s", strerror(errno));
147 fd = bpf_map_get_fd_by_id(id);
151 p_err("can't get map by id (%u): %s",
152 id, strerror(errno));
156 err = bpf_obj_get_info_by_fd(fd, info, &info_len);
158 p_err("can't get map info: %s", strerror(errno));
163 if (info->type == BPF_MAP_TYPE_STRUCT_OPS &&
164 (!name || !strcmp(name, info->name))) {
172 static int cmd_retval(const struct res *res, bool must_have_one_map)
174 if (res->nr_errs || (!res->nr_maps && must_have_one_map))
180 /* "data" is the work_func private storage */
181 typedef int (*work_func)(int fd, const struct bpf_map_info *info, void *data,
182 struct json_writer *wtr);
184 /* Find all struct_ops map in the system.
185 * Filter out by "name" (if specified).
186 * Then call "func(fd, info, data, wtr)" on each struct_ops map found.
188 static struct res do_search(const char *name, work_func func, void *data,
189 struct json_writer *wtr)
191 struct bpf_map_info *info;
196 info = map_info_alloc(&info_len);
203 jsonw_start_array(wtr);
204 while ((err = get_next_struct_ops_map(name, &fd, info, info_len)) == 1) {
206 err = func(fd, info, data, wtr);
212 jsonw_end_array(wtr);
217 if (!wtr && name && !res.nr_errs && !res.nr_maps)
218 /* It is not printing empty [].
219 * Thus, needs to specifically say nothing found
222 p_err("no struct_ops found for %s", name);
223 else if (!wtr && json_output && !res.nr_errs)
224 /* The "func()" above is not writing any json (i.e. !wtr
227 * However, "-j" is enabled and there is no errs here,
228 * so call json_null() as the current convention of
231 jsonw_null(json_wtr);
237 static struct res do_one_id(const char *id_str, work_func func, void *data,
238 struct json_writer *wtr)
240 struct bpf_map_info *info;
247 id = strtoul(id_str, &endptr, 0);
248 if (*endptr || !id || id > UINT32_MAX) {
249 p_err("invalid id %s", id_str);
254 fd = bpf_map_get_fd_by_id(id);
256 p_err("can't get map by id (%lu): %s", id, strerror(errno));
261 info = map_info_alloc(&info_len);
267 if (bpf_obj_get_info_by_fd(fd, info, &info_len)) {
268 p_err("can't get map info: %s", strerror(errno));
273 if (info->type != BPF_MAP_TYPE_STRUCT_OPS) {
274 p_err("%s id %u is not a struct_ops map", info->name, info->id);
281 if (func(fd, info, data, wtr))
283 else if (!wtr && json_output)
284 /* The "func()" above is not writing any json (i.e. !wtr
287 * However, "-j" is enabled and there is no errs here,
288 * so call json_null() as the current convention of
291 jsonw_null(json_wtr);
300 static struct res do_work_on_struct_ops(const char *search_type,
301 const char *search_term,
302 work_func func, void *data,
303 struct json_writer *wtr)
306 if (is_prefix(search_type, "id"))
307 return do_one_id(search_term, func, data, wtr);
308 else if (!is_prefix(search_type, "name"))
312 return do_search(search_term, func, data, wtr);
315 static int __do_show(int fd, const struct bpf_map_info *info, void *data,
316 struct json_writer *wtr)
319 jsonw_start_object(wtr);
320 jsonw_uint_field(wtr, "id", info->id);
321 jsonw_string_field(wtr, "name", info->name);
322 jsonw_string_field(wtr, "kernel_struct_ops",
323 get_kern_struct_ops_name(info));
324 jsonw_end_object(wtr);
326 printf("%u: %-15s %-32s\n", info->id, info->name,
327 get_kern_struct_ops_name(info));
333 static int do_show(int argc, char **argv)
335 const char *search_type = NULL, *search_term = NULL;
338 if (argc && argc != 2)
342 search_type = GET_ARG();
343 search_term = GET_ARG();
346 res = do_work_on_struct_ops(search_type, search_term, __do_show,
349 return cmd_retval(&res, !!search_term);
352 static int __do_dump(int fd, const struct bpf_map_info *info, void *data,
353 struct json_writer *wtr)
355 struct btf_dumper *d = (struct btf_dumper *)data;
356 const struct btf_type *struct_ops_type;
357 const struct btf *kern_btf = d->btf;
358 const char *struct_ops_name;
362 /* note: d->jw == wtr */
366 /* The kernel supporting BPF_MAP_TYPE_STRUCT_OPS must have
367 * btf_vmlinux_value_type_id.
369 struct_ops_type = btf__type_by_id(kern_btf,
370 info->btf_vmlinux_value_type_id);
371 struct_ops_name = btf__name_by_offset(kern_btf,
372 struct_ops_type->name_off);
373 value = calloc(1, info->value_size);
375 p_err("mem alloc failed");
379 if (bpf_map_lookup_elem(fd, &zero, value)) {
380 p_err("can't lookup struct_ops map %s id %u",
381 info->name, info->id);
386 jsonw_start_object(wtr);
387 jsonw_name(wtr, "bpf_map_info");
388 btf_dumper_type(d, map_info_type_id, (void *)info);
389 jsonw_end_object(wtr);
391 jsonw_start_object(wtr);
392 jsonw_name(wtr, struct_ops_name);
393 btf_dumper_type(d, info->btf_vmlinux_value_type_id, value);
394 jsonw_end_object(wtr);
401 static int do_dump(int argc, char **argv)
403 const char *search_type = NULL, *search_term = NULL;
404 json_writer_t *wtr = json_wtr;
405 const struct btf *kern_btf;
406 struct btf_dumper d = {};
409 if (argc && argc != 2)
413 search_type = GET_ARG();
414 search_term = GET_ARG();
417 kern_btf = get_btf_vmlinux();
418 if (IS_ERR(kern_btf))
422 wtr = jsonw_new(stdout);
424 p_err("can't create json writer");
427 jsonw_pretty(wtr, true);
432 d.is_plain_text = !json_output;
433 d.prog_id_as_func_ptr = true;
435 res = do_work_on_struct_ops(search_type, search_term, __do_dump, &d,
441 return cmd_retval(&res, !!search_term);
444 static int __do_unregister(int fd, const struct bpf_map_info *info, void *data,
445 struct json_writer *wtr)
449 if (bpf_map_delete_elem(fd, &zero)) {
450 p_err("can't unload %s %s id %u: %s",
451 get_kern_struct_ops_name(info), info->name,
452 info->id, strerror(errno));
456 p_info("Unregistered %s %s id %u",
457 get_kern_struct_ops_name(info), info->name,
463 static int do_unregister(int argc, char **argv)
465 const char *search_type, *search_term;
471 search_type = GET_ARG();
472 search_term = GET_ARG();
474 res = do_work_on_struct_ops(search_type, search_term,
475 __do_unregister, NULL, NULL);
477 return cmd_retval(&res, true);
480 static int do_register(int argc, char **argv)
482 struct bpf_object_load_attr load_attr = {};
483 const struct bpf_map_def *def;
484 struct bpf_map_info info = {};
485 __u32 info_len = sizeof(info);
486 int nr_errs = 0, nr_maps = 0;
487 struct bpf_object *obj;
488 struct bpf_link *link;
497 obj = bpf_object__open(file);
498 if (IS_ERR_OR_NULL(obj))
505 /* log_level1 + log_level2 + stats, but not stable UAPI */
506 load_attr.log_level = 1 + 2 + 4;
508 if (bpf_object__load_xattr(&load_attr)) {
509 bpf_object__close(obj);
513 bpf_object__for_each_map(map, obj) {
514 def = bpf_map__def(map);
515 if (def->type != BPF_MAP_TYPE_STRUCT_OPS)
518 link = bpf_map__attach_struct_ops(map);
520 p_err("can't register struct_ops %s: %s",
522 strerror(-PTR_ERR(link)));
528 bpf_link__disconnect(link);
529 bpf_link__destroy(link);
531 if (!bpf_obj_get_info_by_fd(bpf_map__fd(map), &info,
533 p_info("Registered %s %s id %u",
534 get_kern_struct_ops_name(&info),
538 /* Not p_err. The struct_ops was attached
541 p_info("Registered %s but can't find id: %s",
542 bpf_map__name(map), strerror(errno));
545 bpf_object__close(obj);
551 p_err("no struct_ops found in %s", file);
556 jsonw_null(json_wtr);
561 static int do_help(int argc, char **argv)
564 jsonw_null(json_wtr);
569 "Usage: %1$s %2$s { show | list } [STRUCT_OPS_MAP]\n"
570 " %1$s %2$s dump [STRUCT_OPS_MAP]\n"
571 " %1$s %2$s register OBJ\n"
572 " %1$s %2$s unregister STRUCT_OPS_MAP\n"
575 " STRUCT_OPS_MAP := [ id STRUCT_OPS_MAP_ID | name STRUCT_OPS_MAP_NAME ]\n"
576 " " HELP_SPEC_OPTIONS " }\n"
583 static const struct cmd cmds[] = {
586 { "register", do_register },
587 { "unregister", do_unregister },
593 int do_struct_ops(int argc, char **argv)
597 err = cmd_select(cmds, argc, argv, do_help);
599 if (!IS_ERR(btf_vmlinux))
600 btf__free(btf_vmlinux);