2 * Copyright (C) 2017 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
34 /* Author: Jakub Kicinski <kubakici@wp.pl> */
44 #include <sys/types.h>
52 #include "xlated_dumper.h"
54 static const char * const prog_type_name[] = {
55 [BPF_PROG_TYPE_UNSPEC] = "unspec",
56 [BPF_PROG_TYPE_SOCKET_FILTER] = "socket_filter",
57 [BPF_PROG_TYPE_KPROBE] = "kprobe",
58 [BPF_PROG_TYPE_SCHED_CLS] = "sched_cls",
59 [BPF_PROG_TYPE_SCHED_ACT] = "sched_act",
60 [BPF_PROG_TYPE_TRACEPOINT] = "tracepoint",
61 [BPF_PROG_TYPE_XDP] = "xdp",
62 [BPF_PROG_TYPE_PERF_EVENT] = "perf_event",
63 [BPF_PROG_TYPE_CGROUP_SKB] = "cgroup_skb",
64 [BPF_PROG_TYPE_CGROUP_SOCK] = "cgroup_sock",
65 [BPF_PROG_TYPE_LWT_IN] = "lwt_in",
66 [BPF_PROG_TYPE_LWT_OUT] = "lwt_out",
67 [BPF_PROG_TYPE_LWT_XMIT] = "lwt_xmit",
68 [BPF_PROG_TYPE_SOCK_OPS] = "sock_ops",
69 [BPF_PROG_TYPE_SK_SKB] = "sk_skb",
70 [BPF_PROG_TYPE_CGROUP_DEVICE] = "cgroup_device",
73 static void print_boot_time(__u64 nsecs, char *buf, unsigned int size)
75 struct timespec real_time_ts, boot_time_ts;
76 time_t wallclock_secs;
81 if (clock_gettime(CLOCK_REALTIME, &real_time_ts) ||
82 clock_gettime(CLOCK_BOOTTIME, &boot_time_ts)) {
83 perror("Can't read clocks");
84 snprintf(buf, size, "%llu", nsecs / 1000000000);
88 wallclock_secs = (real_time_ts.tv_sec - boot_time_ts.tv_sec) +
91 if (!localtime_r(&wallclock_secs, &load_tm)) {
92 snprintf(buf, size, "%llu", nsecs / 1000000000);
96 strftime(buf, size, "%b %d/%H:%M", &load_tm);
99 static int prog_fd_by_tag(unsigned char *tag)
101 struct bpf_prog_info info = {};
102 __u32 len = sizeof(info);
108 err = bpf_prog_get_next_id(id, &id);
110 p_err("%s", strerror(errno));
114 fd = bpf_prog_get_fd_by_id(id);
116 p_err("can't get prog by id (%u): %s",
117 id, strerror(errno));
121 err = bpf_obj_get_info_by_fd(fd, &info, &len);
123 p_err("can't get prog info (%u): %s",
124 id, strerror(errno));
129 if (!memcmp(tag, info.tag, BPF_TAG_SIZE))
136 int prog_parse_fd(int *argc, char ***argv)
140 if (is_prefix(**argv, "id")) {
146 id = strtoul(**argv, &endptr, 0);
148 p_err("can't parse %s as ID", **argv);
153 fd = bpf_prog_get_fd_by_id(id);
155 p_err("get by id (%u): %s", id, strerror(errno));
157 } else if (is_prefix(**argv, "tag")) {
158 unsigned char tag[BPF_TAG_SIZE];
162 if (sscanf(**argv, BPF_TAG_FMT, tag, tag + 1, tag + 2,
163 tag + 3, tag + 4, tag + 5, tag + 6, tag + 7)
165 p_err("can't parse tag");
170 return prog_fd_by_tag(tag);
171 } else if (is_prefix(**argv, "pinned")) {
179 return open_obj_pinned_any(path, BPF_OBJ_PROG);
182 p_err("expected 'id', 'tag' or 'pinned', got: '%s'?", **argv);
186 static void show_prog_maps(int fd, u32 num_maps)
188 struct bpf_prog_info info = {};
189 __u32 len = sizeof(info);
190 __u32 map_ids[num_maps];
194 info.nr_map_ids = num_maps;
195 info.map_ids = ptr_to_u64(map_ids);
197 err = bpf_obj_get_info_by_fd(fd, &info, &len);
198 if (err || !info.nr_map_ids)
202 jsonw_name(json_wtr, "map_ids");
203 jsonw_start_array(json_wtr);
204 for (i = 0; i < info.nr_map_ids; i++)
205 jsonw_uint(json_wtr, map_ids[i]);
206 jsonw_end_array(json_wtr);
209 for (i = 0; i < info.nr_map_ids; i++)
210 printf("%u%s", map_ids[i],
211 i == info.nr_map_ids - 1 ? "" : ",");
215 static void print_prog_json(struct bpf_prog_info *info, int fd)
219 jsonw_start_object(json_wtr);
220 jsonw_uint_field(json_wtr, "id", info->id);
221 if (info->type < ARRAY_SIZE(prog_type_name))
222 jsonw_string_field(json_wtr, "type",
223 prog_type_name[info->type]);
225 jsonw_uint_field(json_wtr, "type", info->type);
228 jsonw_string_field(json_wtr, "name", info->name);
230 jsonw_name(json_wtr, "tag");
231 jsonw_printf(json_wtr, "\"" BPF_TAG_FMT "\"",
232 info->tag[0], info->tag[1], info->tag[2], info->tag[3],
233 info->tag[4], info->tag[5], info->tag[6], info->tag[7]);
235 print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
237 if (info->load_time) {
240 print_boot_time(info->load_time, buf, sizeof(buf));
242 /* Piggy back on load_time, since 0 uid is a valid one */
243 jsonw_string_field(json_wtr, "loaded_at", buf);
244 jsonw_uint_field(json_wtr, "uid", info->created_by_uid);
247 jsonw_uint_field(json_wtr, "bytes_xlated", info->xlated_prog_len);
249 if (info->jited_prog_len) {
250 jsonw_bool_field(json_wtr, "jited", true);
251 jsonw_uint_field(json_wtr, "bytes_jited", info->jited_prog_len);
253 jsonw_bool_field(json_wtr, "jited", false);
256 memlock = get_fdinfo(fd, "memlock");
258 jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock));
261 if (info->nr_map_ids)
262 show_prog_maps(fd, info->nr_map_ids);
264 if (!hash_empty(prog_table.table)) {
265 struct pinned_obj *obj;
267 jsonw_name(json_wtr, "pinned");
268 jsonw_start_array(json_wtr);
269 hash_for_each_possible(prog_table.table, obj, hash, info->id) {
270 if (obj->id == info->id)
271 jsonw_string(json_wtr, obj->path);
273 jsonw_end_array(json_wtr);
276 jsonw_end_object(json_wtr);
279 static void print_prog_plain(struct bpf_prog_info *info, int fd)
283 printf("%u: ", info->id);
284 if (info->type < ARRAY_SIZE(prog_type_name))
285 printf("%s ", prog_type_name[info->type]);
287 printf("type %u ", info->type);
290 printf("name %s ", info->name);
293 fprint_hex(stdout, info->tag, BPF_TAG_SIZE, "");
294 print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
297 if (info->load_time) {
300 print_boot_time(info->load_time, buf, sizeof(buf));
302 /* Piggy back on load_time, since 0 uid is a valid one */
303 printf("\tloaded_at %s uid %u\n", buf, info->created_by_uid);
306 printf("\txlated %uB", info->xlated_prog_len);
308 if (info->jited_prog_len)
309 printf(" jited %uB", info->jited_prog_len);
311 printf(" not jited");
313 memlock = get_fdinfo(fd, "memlock");
315 printf(" memlock %sB", memlock);
318 if (info->nr_map_ids)
319 show_prog_maps(fd, info->nr_map_ids);
321 if (!hash_empty(prog_table.table)) {
322 struct pinned_obj *obj;
325 hash_for_each_possible(prog_table.table, obj, hash, info->id) {
326 if (obj->id == info->id)
327 printf("\tpinned %s\n", obj->path);
334 static int show_prog(int fd)
336 struct bpf_prog_info info = {};
337 __u32 len = sizeof(info);
340 err = bpf_obj_get_info_by_fd(fd, &info, &len);
342 p_err("can't get prog info: %s", strerror(errno));
347 print_prog_json(&info, fd);
349 print_prog_plain(&info, fd);
354 static int do_show(int argc, char **argv)
361 build_pinned_obj_table(&prog_table, BPF_OBJ_PROG);
364 fd = prog_parse_fd(&argc, &argv);
368 return show_prog(fd);
375 jsonw_start_array(json_wtr);
377 err = bpf_prog_get_next_id(id, &id);
379 if (errno == ENOENT) {
383 p_err("can't get next program: %s%s", strerror(errno),
384 errno == EINVAL ? " -- kernel too old?" : "");
389 fd = bpf_prog_get_fd_by_id(id);
393 p_err("can't get prog by id (%u): %s",
394 id, strerror(errno));
406 jsonw_end_array(json_wtr);
411 static int do_dump(int argc, char **argv)
413 struct bpf_prog_info info = {};
414 struct dump_data dd = {};
415 __u32 len = sizeof(info);
416 unsigned int buf_size;
417 char *filepath = NULL;
418 bool opcodes = false;
427 if (is_prefix(*argv, "jited")) {
428 member_len = &info.jited_prog_len;
429 member_ptr = &info.jited_prog_insns;
430 } else if (is_prefix(*argv, "xlated")) {
431 member_len = &info.xlated_prog_len;
432 member_ptr = &info.xlated_prog_insns;
434 p_err("expected 'xlated' or 'jited', got: %s", *argv);
442 fd = prog_parse_fd(&argc, &argv);
446 if (is_prefix(*argv, "file")) {
449 p_err("expected file path");
455 } else if (is_prefix(*argv, "opcodes")) {
458 } else if (is_prefix(*argv, "visual")) {
468 err = bpf_obj_get_info_by_fd(fd, &info, &len);
470 p_err("can't get prog info: %s", strerror(errno));
475 p_info("no instructions returned");
480 buf_size = *member_len;
482 buf = malloc(buf_size);
484 p_err("mem alloc failed");
489 memset(&info, 0, sizeof(info));
491 *member_ptr = ptr_to_u64(buf);
492 *member_len = buf_size;
494 err = bpf_obj_get_info_by_fd(fd, &info, &len);
497 p_err("can't get prog info: %s", strerror(errno));
501 if (*member_len > buf_size) {
502 p_err("too many instructions returned");
506 if ((member_len == &info.jited_prog_len &&
507 info.jited_prog_insns == 0) ||
508 (member_len == &info.xlated_prog_len &&
509 info.xlated_prog_insns == 0)) {
510 p_err("error retrieving insn dump: kernel.kptr_restrict set?");
515 fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
517 p_err("can't open file %s: %s", filepath,
522 n = write(fd, buf, *member_len);
524 if (n != *member_len) {
525 p_err("error writing output file: %s",
526 n < 0 ? strerror(errno) : "short write");
531 jsonw_null(json_wtr);
532 } else if (member_len == &info.jited_prog_len) {
533 const char *name = NULL;
536 name = ifindex_to_bfd_name_ns(info.ifindex,
543 disasm_print_insn(buf, *member_len, opcodes, name);
546 jsonw_null(json_wtr);
548 dump_xlated_cfg(buf, *member_len);
550 kernel_syms_load(&dd);
552 dump_xlated_json(&dd, buf, *member_len, opcodes);
554 dump_xlated_plain(&dd, buf, *member_len, opcodes);
555 kernel_syms_destroy(&dd);
566 static int do_pin(int argc, char **argv)
570 err = do_pin_any(argc, argv, bpf_prog_get_fd_by_id);
571 if (!err && json_output)
572 jsonw_null(json_wtr);
576 static int do_load(int argc, char **argv)
578 struct bpf_object *obj;
584 if (bpf_prog_load(argv[0], BPF_PROG_TYPE_UNSPEC, &obj, &prog_fd)) {
585 p_err("failed to load program");
589 if (do_pin_fd(prog_fd, argv[1])) {
590 p_err("failed to pin program");
595 jsonw_null(json_wtr);
600 static int do_help(int argc, char **argv)
603 jsonw_null(json_wtr);
608 "Usage: %s %s { show | list } [PROG]\n"
609 " %s %s dump xlated PROG [{ file FILE | opcodes | visual }]\n"
610 " %s %s dump jited PROG [{ file FILE | opcodes }]\n"
611 " %s %s pin PROG FILE\n"
612 " %s %s load OBJ FILE\n"
615 " " HELP_SPEC_PROGRAM "\n"
616 " " HELP_SPEC_OPTIONS "\n"
618 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
619 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2]);
624 static const struct cmd cmds[] = {
634 int do_prog(int argc, char **argv)
636 return cmd_select(cmds, argc, argv, do_help);