2 * Copyright (C) 2017-2018 Netronome Systems, Inc.
4 * This software is dual licensed under the GNU General License Version 2,
5 * June 1991 as shown in the file COPYING in the top-level directory of this
6 * source tree or the BSD 2-Clause License provided below. You have the
7 * option to license this software under the complete terms of either license.
9 * The BSD 2-Clause License:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * 2. Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
44 #include <sys/types.h>
47 #include <linux/err.h>
54 #include "xlated_dumper.h"
56 static const char * const prog_type_name[] = {
57 [BPF_PROG_TYPE_UNSPEC] = "unspec",
58 [BPF_PROG_TYPE_SOCKET_FILTER] = "socket_filter",
59 [BPF_PROG_TYPE_KPROBE] = "kprobe",
60 [BPF_PROG_TYPE_SCHED_CLS] = "sched_cls",
61 [BPF_PROG_TYPE_SCHED_ACT] = "sched_act",
62 [BPF_PROG_TYPE_TRACEPOINT] = "tracepoint",
63 [BPF_PROG_TYPE_XDP] = "xdp",
64 [BPF_PROG_TYPE_PERF_EVENT] = "perf_event",
65 [BPF_PROG_TYPE_CGROUP_SKB] = "cgroup_skb",
66 [BPF_PROG_TYPE_CGROUP_SOCK] = "cgroup_sock",
67 [BPF_PROG_TYPE_LWT_IN] = "lwt_in",
68 [BPF_PROG_TYPE_LWT_OUT] = "lwt_out",
69 [BPF_PROG_TYPE_LWT_XMIT] = "lwt_xmit",
70 [BPF_PROG_TYPE_SOCK_OPS] = "sock_ops",
71 [BPF_PROG_TYPE_SK_SKB] = "sk_skb",
72 [BPF_PROG_TYPE_CGROUP_DEVICE] = "cgroup_device",
73 [BPF_PROG_TYPE_SK_MSG] = "sk_msg",
74 [BPF_PROG_TYPE_RAW_TRACEPOINT] = "raw_tracepoint",
75 [BPF_PROG_TYPE_CGROUP_SOCK_ADDR] = "cgroup_sock_addr",
76 [BPF_PROG_TYPE_LIRC_MODE2] = "lirc_mode2",
79 static void print_boot_time(__u64 nsecs, char *buf, unsigned int size)
81 struct timespec real_time_ts, boot_time_ts;
82 time_t wallclock_secs;
87 if (clock_gettime(CLOCK_REALTIME, &real_time_ts) ||
88 clock_gettime(CLOCK_BOOTTIME, &boot_time_ts)) {
89 perror("Can't read clocks");
90 snprintf(buf, size, "%llu", nsecs / 1000000000);
94 wallclock_secs = (real_time_ts.tv_sec - boot_time_ts.tv_sec) +
95 (real_time_ts.tv_nsec - boot_time_ts.tv_nsec + nsecs) /
99 if (!localtime_r(&wallclock_secs, &load_tm)) {
100 snprintf(buf, size, "%llu", nsecs / 1000000000);
105 strftime(buf, size, "%s", &load_tm);
107 strftime(buf, size, "%FT%T%z", &load_tm);
110 static int prog_fd_by_tag(unsigned char *tag)
117 struct bpf_prog_info info = {};
118 __u32 len = sizeof(info);
120 err = bpf_prog_get_next_id(id, &id);
122 p_err("%s", strerror(errno));
126 fd = bpf_prog_get_fd_by_id(id);
128 p_err("can't get prog by id (%u): %s",
129 id, strerror(errno));
133 err = bpf_obj_get_info_by_fd(fd, &info, &len);
135 p_err("can't get prog info (%u): %s",
136 id, strerror(errno));
141 if (!memcmp(tag, info.tag, BPF_TAG_SIZE))
148 int prog_parse_fd(int *argc, char ***argv)
152 if (is_prefix(**argv, "id")) {
158 id = strtoul(**argv, &endptr, 0);
160 p_err("can't parse %s as ID", **argv);
165 fd = bpf_prog_get_fd_by_id(id);
167 p_err("get by id (%u): %s", id, strerror(errno));
169 } else if (is_prefix(**argv, "tag")) {
170 unsigned char tag[BPF_TAG_SIZE];
174 if (sscanf(**argv, BPF_TAG_FMT, tag, tag + 1, tag + 2,
175 tag + 3, tag + 4, tag + 5, tag + 6, tag + 7)
177 p_err("can't parse tag");
182 return prog_fd_by_tag(tag);
183 } else if (is_prefix(**argv, "pinned")) {
191 return open_obj_pinned_any(path, BPF_OBJ_PROG);
194 p_err("expected 'id', 'tag' or 'pinned', got: '%s'?", **argv);
198 static void show_prog_maps(int fd, u32 num_maps)
200 struct bpf_prog_info info = {};
201 __u32 len = sizeof(info);
202 __u32 map_ids[num_maps];
206 info.nr_map_ids = num_maps;
207 info.map_ids = ptr_to_u64(map_ids);
209 err = bpf_obj_get_info_by_fd(fd, &info, &len);
210 if (err || !info.nr_map_ids)
214 jsonw_name(json_wtr, "map_ids");
215 jsonw_start_array(json_wtr);
216 for (i = 0; i < info.nr_map_ids; i++)
217 jsonw_uint(json_wtr, map_ids[i]);
218 jsonw_end_array(json_wtr);
221 for (i = 0; i < info.nr_map_ids; i++)
222 printf("%u%s", map_ids[i],
223 i == info.nr_map_ids - 1 ? "" : ",");
227 static void print_prog_json(struct bpf_prog_info *info, int fd)
231 jsonw_start_object(json_wtr);
232 jsonw_uint_field(json_wtr, "id", info->id);
233 if (info->type < ARRAY_SIZE(prog_type_name))
234 jsonw_string_field(json_wtr, "type",
235 prog_type_name[info->type]);
237 jsonw_uint_field(json_wtr, "type", info->type);
240 jsonw_string_field(json_wtr, "name", info->name);
242 jsonw_name(json_wtr, "tag");
243 jsonw_printf(json_wtr, "\"" BPF_TAG_FMT "\"",
244 info->tag[0], info->tag[1], info->tag[2], info->tag[3],
245 info->tag[4], info->tag[5], info->tag[6], info->tag[7]);
247 jsonw_bool_field(json_wtr, "gpl_compatible", info->gpl_compatible);
249 print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
251 if (info->load_time) {
254 print_boot_time(info->load_time, buf, sizeof(buf));
256 /* Piggy back on load_time, since 0 uid is a valid one */
257 jsonw_name(json_wtr, "loaded_at");
258 jsonw_printf(json_wtr, "%s", buf);
259 jsonw_uint_field(json_wtr, "uid", info->created_by_uid);
262 jsonw_uint_field(json_wtr, "bytes_xlated", info->xlated_prog_len);
264 if (info->jited_prog_len) {
265 jsonw_bool_field(json_wtr, "jited", true);
266 jsonw_uint_field(json_wtr, "bytes_jited", info->jited_prog_len);
268 jsonw_bool_field(json_wtr, "jited", false);
271 memlock = get_fdinfo(fd, "memlock");
273 jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock));
276 if (info->nr_map_ids)
277 show_prog_maps(fd, info->nr_map_ids);
279 if (!hash_empty(prog_table.table)) {
280 struct pinned_obj *obj;
282 jsonw_name(json_wtr, "pinned");
283 jsonw_start_array(json_wtr);
284 hash_for_each_possible(prog_table.table, obj, hash, info->id) {
285 if (obj->id == info->id)
286 jsonw_string(json_wtr, obj->path);
288 jsonw_end_array(json_wtr);
291 jsonw_end_object(json_wtr);
294 static void print_prog_plain(struct bpf_prog_info *info, int fd)
298 printf("%u: ", info->id);
299 if (info->type < ARRAY_SIZE(prog_type_name))
300 printf("%s ", prog_type_name[info->type]);
302 printf("type %u ", info->type);
305 printf("name %s ", info->name);
308 fprint_hex(stdout, info->tag, BPF_TAG_SIZE, "");
309 print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
310 printf("%s", info->gpl_compatible ? " gpl" : "");
313 if (info->load_time) {
316 print_boot_time(info->load_time, buf, sizeof(buf));
318 /* Piggy back on load_time, since 0 uid is a valid one */
319 printf("\tloaded_at %s uid %u\n", buf, info->created_by_uid);
322 printf("\txlated %uB", info->xlated_prog_len);
324 if (info->jited_prog_len)
325 printf(" jited %uB", info->jited_prog_len);
327 printf(" not jited");
329 memlock = get_fdinfo(fd, "memlock");
331 printf(" memlock %sB", memlock);
334 if (info->nr_map_ids)
335 show_prog_maps(fd, info->nr_map_ids);
337 if (!hash_empty(prog_table.table)) {
338 struct pinned_obj *obj;
341 hash_for_each_possible(prog_table.table, obj, hash, info->id) {
342 if (obj->id == info->id)
343 printf("\tpinned %s\n", obj->path);
350 static int show_prog(int fd)
352 struct bpf_prog_info info = {};
353 __u32 len = sizeof(info);
356 err = bpf_obj_get_info_by_fd(fd, &info, &len);
358 p_err("can't get prog info: %s", strerror(errno));
363 print_prog_json(&info, fd);
365 print_prog_plain(&info, fd);
370 static int do_show(int argc, char **argv)
377 build_pinned_obj_table(&prog_table, BPF_OBJ_PROG);
380 fd = prog_parse_fd(&argc, &argv);
393 jsonw_start_array(json_wtr);
395 err = bpf_prog_get_next_id(id, &id);
397 if (errno == ENOENT) {
401 p_err("can't get next program: %s%s", strerror(errno),
402 errno == EINVAL ? " -- kernel too old?" : "");
407 fd = bpf_prog_get_fd_by_id(id);
411 p_err("can't get prog by id (%u): %s",
412 id, strerror(errno));
424 jsonw_end_array(json_wtr);
429 static int do_dump(int argc, char **argv)
431 unsigned long *func_ksyms = NULL;
432 struct bpf_prog_info info = {};
433 unsigned int *func_lens = NULL;
434 unsigned int nr_func_ksyms;
435 unsigned int nr_func_lens;
436 struct dump_data dd = {};
437 __u32 len = sizeof(info);
438 unsigned int buf_size;
439 char *filepath = NULL;
440 bool opcodes = false;
449 if (is_prefix(*argv, "jited")) {
450 member_len = &info.jited_prog_len;
451 member_ptr = &info.jited_prog_insns;
452 } else if (is_prefix(*argv, "xlated")) {
453 member_len = &info.xlated_prog_len;
454 member_ptr = &info.xlated_prog_insns;
456 p_err("expected 'xlated' or 'jited', got: %s", *argv);
464 fd = prog_parse_fd(&argc, &argv);
468 if (is_prefix(*argv, "file")) {
471 p_err("expected file path");
477 } else if (is_prefix(*argv, "opcodes")) {
480 } else if (is_prefix(*argv, "visual")) {
490 err = bpf_obj_get_info_by_fd(fd, &info, &len);
492 p_err("can't get prog info: %s", strerror(errno));
497 p_info("no instructions returned");
502 buf_size = *member_len;
504 buf = malloc(buf_size);
506 p_err("mem alloc failed");
511 nr_func_ksyms = info.nr_jited_ksyms;
513 func_ksyms = malloc(nr_func_ksyms * sizeof(__u64));
515 p_err("mem alloc failed");
521 nr_func_lens = info.nr_jited_func_lens;
523 func_lens = malloc(nr_func_lens * sizeof(__u32));
525 p_err("mem alloc failed");
531 memset(&info, 0, sizeof(info));
533 *member_ptr = ptr_to_u64(buf);
534 *member_len = buf_size;
535 info.jited_ksyms = ptr_to_u64(func_ksyms);
536 info.nr_jited_ksyms = nr_func_ksyms;
537 info.jited_func_lens = ptr_to_u64(func_lens);
538 info.nr_jited_func_lens = nr_func_lens;
540 err = bpf_obj_get_info_by_fd(fd, &info, &len);
543 p_err("can't get prog info: %s", strerror(errno));
547 if (*member_len > buf_size) {
548 p_err("too many instructions returned");
552 if (info.nr_jited_ksyms > nr_func_ksyms) {
553 p_err("too many addresses returned");
557 if (info.nr_jited_func_lens > nr_func_lens) {
558 p_err("too many values returned");
562 if ((member_len == &info.jited_prog_len &&
563 info.jited_prog_insns == 0) ||
564 (member_len == &info.xlated_prog_len &&
565 info.xlated_prog_insns == 0)) {
566 p_err("error retrieving insn dump: kernel.kptr_restrict set?");
571 fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
573 p_err("can't open file %s: %s", filepath,
578 n = write(fd, buf, *member_len);
580 if (n != *member_len) {
581 p_err("error writing output file: %s",
582 n < 0 ? strerror(errno) : "short write");
587 jsonw_null(json_wtr);
588 } else if (member_len == &info.jited_prog_len) {
589 const char *name = NULL;
592 name = ifindex_to_bfd_name_ns(info.ifindex,
599 if (info.nr_jited_func_lens && info.jited_func_lens) {
600 struct kernel_sym *sym = NULL;
601 char sym_name[SYM_MAX_NAME];
602 unsigned char *img = buf;
607 if (info.nr_jited_ksyms) {
608 kernel_syms_load(&dd);
609 ksyms = (__u64 *) info.jited_ksyms;
613 jsonw_start_array(json_wtr);
615 lens = (__u32 *) info.jited_func_lens;
616 for (i = 0; i < info.nr_jited_func_lens; i++) {
618 sym = kernel_syms_search(&dd, ksyms[i]);
620 sprintf(sym_name, "%s", sym->name);
622 sprintf(sym_name, "0x%016llx", ksyms[i]);
624 strcpy(sym_name, "unknown");
628 jsonw_start_object(json_wtr);
629 jsonw_name(json_wtr, "name");
630 jsonw_string(json_wtr, sym_name);
631 jsonw_name(json_wtr, "insns");
633 printf("%s:\n", sym_name);
636 disasm_print_insn(img, lens[i], opcodes, name);
640 jsonw_end_object(json_wtr);
646 jsonw_end_array(json_wtr);
648 disasm_print_insn(buf, *member_len, opcodes, name);
652 jsonw_null(json_wtr);
654 dump_xlated_cfg(buf, *member_len);
656 kernel_syms_load(&dd);
657 dd.nr_jited_ksyms = info.nr_jited_ksyms;
658 dd.jited_ksyms = (__u64 *) info.jited_ksyms;
661 dump_xlated_json(&dd, buf, *member_len, opcodes);
663 dump_xlated_plain(&dd, buf, *member_len, opcodes);
664 kernel_syms_destroy(&dd);
679 static int do_pin(int argc, char **argv)
683 err = do_pin_any(argc, argv, bpf_prog_get_fd_by_id);
684 if (!err && json_output)
685 jsonw_null(json_wtr);
695 int map_replace_compar(const void *p1, const void *p2)
697 const struct map_replace *a = p1, *b = p2;
699 return a->idx - b->idx;
702 static int do_load(int argc, char **argv)
704 enum bpf_attach_type expected_attach_type;
705 struct bpf_object_open_attr attr = {
706 .prog_type = BPF_PROG_TYPE_UNSPEC,
708 struct map_replace *map_replace = NULL;
709 unsigned int old_map_fds = 0;
710 struct bpf_program *prog;
711 struct bpf_object *obj;
720 attr.file = GET_ARG();
724 if (is_prefix(*argv, "type")) {
729 if (attr.prog_type != BPF_PROG_TYPE_UNSPEC) {
730 p_err("program type already specified");
731 goto err_free_reuse_maps;
734 goto err_free_reuse_maps;
736 /* Put a '/' at the end of type to appease libbpf */
737 type = malloc(strlen(*argv) + 2);
739 p_err("mem alloc failed");
740 goto err_free_reuse_maps;
746 err = libbpf_prog_type_by_name(type, &attr.prog_type,
747 &expected_attach_type);
750 p_err("unknown program type '%s'", *argv);
751 goto err_free_reuse_maps;
754 } else if (is_prefix(*argv, "map")) {
755 void *new_map_replace;
762 goto err_free_reuse_maps;
764 if (is_prefix(*argv, "idx")) {
767 idx = strtoul(*argv, &endptr, 0);
769 p_err("can't parse %s as IDX", *argv);
770 goto err_free_reuse_maps;
773 } else if (is_prefix(*argv, "name")) {
779 p_err("expected 'idx' or 'name', got: '%s'?",
781 goto err_free_reuse_maps;
785 fd = map_parse_fd(&argc, &argv);
787 goto err_free_reuse_maps;
789 new_map_replace = reallocarray(map_replace,
791 sizeof(*map_replace));
792 if (!new_map_replace) {
793 p_err("mem alloc failed");
794 goto err_free_reuse_maps;
796 map_replace = new_map_replace;
798 map_replace[old_map_fds].idx = idx;
799 map_replace[old_map_fds].name = name;
800 map_replace[old_map_fds].fd = fd;
802 } else if (is_prefix(*argv, "dev")) {
806 p_err("offload device already specified");
807 goto err_free_reuse_maps;
810 goto err_free_reuse_maps;
812 ifindex = if_nametoindex(*argv);
814 p_err("unrecognized netdevice '%s': %s",
815 *argv, strerror(errno));
816 goto err_free_reuse_maps;
820 p_err("expected no more arguments, 'type', 'map' or 'dev', got: '%s'?",
822 goto err_free_reuse_maps;
826 obj = bpf_object__open_xattr(&attr);
827 if (IS_ERR_OR_NULL(obj)) {
828 p_err("failed to open object file");
829 goto err_free_reuse_maps;
832 prog = bpf_program__next(NULL, obj);
834 p_err("object file doesn't contain any bpf program");
838 bpf_program__set_ifindex(prog, ifindex);
839 if (attr.prog_type == BPF_PROG_TYPE_UNSPEC) {
840 const char *sec_name = bpf_program__title(prog, false);
842 err = libbpf_prog_type_by_name(sec_name, &attr.prog_type,
843 &expected_attach_type);
845 p_err("failed to guess program type based on section name %s\n",
850 bpf_program__set_type(prog, attr.prog_type);
851 bpf_program__set_expected_attach_type(prog, expected_attach_type);
853 qsort(map_replace, old_map_fds, sizeof(*map_replace),
856 /* After the sort maps by name will be first on the list, because they
857 * have idx == -1. Resolve them.
860 while (j < old_map_fds && map_replace[j].name) {
862 bpf_map__for_each(map, obj) {
863 if (!strcmp(bpf_map__name(map), map_replace[j].name)) {
864 map_replace[j].idx = i;
869 if (map_replace[j].idx == -1) {
870 p_err("unable to find map '%s'", map_replace[j].name);
875 /* Resort if any names were resolved */
877 qsort(map_replace, old_map_fds, sizeof(*map_replace),
880 /* Set ifindex and name reuse */
883 bpf_map__for_each(map, obj) {
884 if (!bpf_map__is_offload_neutral(map))
885 bpf_map__set_ifindex(map, ifindex);
887 if (j < old_map_fds && idx == map_replace[j].idx) {
888 err = bpf_map__reuse_fd(map, map_replace[j++].fd);
890 p_err("unable to set up map reuse: %d", err);
894 /* Next reuse wants to apply to the same map */
895 if (j < old_map_fds && map_replace[j].idx == idx) {
896 p_err("replacement for map idx %d specified more than once",
904 if (j < old_map_fds) {
905 p_err("map idx '%d' not used", map_replace[j].idx);
909 err = bpf_object__load(obj);
911 p_err("failed to load object file");
915 if (do_pin_fd(bpf_program__fd(prog), pinfile))
919 jsonw_null(json_wtr);
921 bpf_object__close(obj);
922 for (i = 0; i < old_map_fds; i++)
923 close(map_replace[i].fd);
929 bpf_object__close(obj);
931 for (i = 0; i < old_map_fds; i++)
932 close(map_replace[i].fd);
937 static int do_help(int argc, char **argv)
940 jsonw_null(json_wtr);
945 "Usage: %s %s { show | list } [PROG]\n"
946 " %s %s dump xlated PROG [{ file FILE | opcodes | visual }]\n"
947 " %s %s dump jited PROG [{ file FILE | opcodes }]\n"
948 " %s %s pin PROG FILE\n"
949 " %s %s load OBJ FILE [type TYPE] [dev NAME] \\\n"
950 " [map { idx IDX | name NAME } MAP]\n"
953 " " HELP_SPEC_MAP "\n"
954 " " HELP_SPEC_PROGRAM "\n"
955 " TYPE := { socket | kprobe | kretprobe | classifier | action |\n"
956 " tracepoint | raw_tracepoint | xdp | perf_event | cgroup/skb |\n"
957 " cgroup/sock | cgroup/dev | lwt_in | lwt_out | lwt_xmit |\n"
958 " lwt_seg6local | sockops | sk_skb | sk_msg | lirc_mode2 |\n"
959 " cgroup/bind4 | cgroup/bind6 | cgroup/post_bind4 |\n"
960 " cgroup/post_bind6 | cgroup/connect4 | cgroup/connect6 |\n"
961 " cgroup/sendmsg4 | cgroup/sendmsg6 }\n"
962 " " HELP_SPEC_OPTIONS "\n"
964 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
965 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2]);
970 static const struct cmd cmds[] = {
980 int do_prog(int argc, char **argv)
982 return cmd_select(cmds, argc, argv, do_help);