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> */
45 #include <sys/types.h>
53 static const char * const prog_type_name
[] = {
54 [BPF_PROG_TYPE_UNSPEC
] = "unspec",
55 [BPF_PROG_TYPE_SOCKET_FILTER
] = "socket_filter",
56 [BPF_PROG_TYPE_KPROBE
] = "kprobe",
57 [BPF_PROG_TYPE_SCHED_CLS
] = "sched_cls",
58 [BPF_PROG_TYPE_SCHED_ACT
] = "sched_act",
59 [BPF_PROG_TYPE_TRACEPOINT
] = "tracepoint",
60 [BPF_PROG_TYPE_XDP
] = "xdp",
61 [BPF_PROG_TYPE_PERF_EVENT
] = "perf_event",
62 [BPF_PROG_TYPE_CGROUP_SKB
] = "cgroup_skb",
63 [BPF_PROG_TYPE_CGROUP_SOCK
] = "cgroup_sock",
64 [BPF_PROG_TYPE_LWT_IN
] = "lwt_in",
65 [BPF_PROG_TYPE_LWT_OUT
] = "lwt_out",
66 [BPF_PROG_TYPE_LWT_XMIT
] = "lwt_xmit",
67 [BPF_PROG_TYPE_SOCK_OPS
] = "sock_ops",
68 [BPF_PROG_TYPE_SK_SKB
] = "sk_skb",
71 static void print_boot_time(__u64 nsecs
, char *buf
, unsigned int size
)
73 struct timespec real_time_ts
, boot_time_ts
;
74 time_t wallclock_secs
;
79 if (clock_gettime(CLOCK_REALTIME
, &real_time_ts
) ||
80 clock_gettime(CLOCK_BOOTTIME
, &boot_time_ts
)) {
81 perror("Can't read clocks");
82 snprintf(buf
, size
, "%llu", nsecs
/ 1000000000);
86 wallclock_secs
= (real_time_ts
.tv_sec
- boot_time_ts
.tv_sec
) +
89 if (!localtime_r(&wallclock_secs
, &load_tm
)) {
90 snprintf(buf
, size
, "%llu", nsecs
/ 1000000000);
94 strftime(buf
, size
, "%b %d/%H:%M", &load_tm
);
97 static int prog_fd_by_tag(unsigned char *tag
)
99 struct bpf_prog_info info
= {};
100 __u32 len
= sizeof(info
);
106 err
= bpf_prog_get_next_id(id
, &id
);
108 p_err("%s", strerror(errno
));
112 fd
= bpf_prog_get_fd_by_id(id
);
114 p_err("can't get prog by id (%u): %s",
115 id
, strerror(errno
));
119 err
= bpf_obj_get_info_by_fd(fd
, &info
, &len
);
121 p_err("can't get prog info (%u): %s",
122 id
, strerror(errno
));
127 if (!memcmp(tag
, info
.tag
, BPF_TAG_SIZE
))
134 int prog_parse_fd(int *argc
, char ***argv
)
138 if (is_prefix(**argv
, "id")) {
144 id
= strtoul(**argv
, &endptr
, 0);
146 p_err("can't parse %s as ID", **argv
);
151 fd
= bpf_prog_get_fd_by_id(id
);
153 p_err("get by id (%u): %s", id
, strerror(errno
));
155 } else if (is_prefix(**argv
, "tag")) {
156 unsigned char tag
[BPF_TAG_SIZE
];
160 if (sscanf(**argv
, BPF_TAG_FMT
, tag
, tag
+ 1, tag
+ 2,
161 tag
+ 3, tag
+ 4, tag
+ 5, tag
+ 6, tag
+ 7)
163 p_err("can't parse tag");
168 return prog_fd_by_tag(tag
);
169 } else if (is_prefix(**argv
, "pinned")) {
177 return open_obj_pinned_any(path
, BPF_OBJ_PROG
);
180 p_err("expected 'id', 'tag' or 'pinned', got: '%s'?", **argv
);
184 static void show_prog_maps(int fd
, u32 num_maps
)
186 struct bpf_prog_info info
= {};
187 __u32 len
= sizeof(info
);
188 __u32 map_ids
[num_maps
];
192 info
.nr_map_ids
= num_maps
;
193 info
.map_ids
= ptr_to_u64(map_ids
);
195 err
= bpf_obj_get_info_by_fd(fd
, &info
, &len
);
196 if (err
|| !info
.nr_map_ids
)
200 jsonw_name(json_wtr
, "map_ids");
201 jsonw_start_array(json_wtr
);
202 for (i
= 0; i
< info
.nr_map_ids
; i
++)
203 jsonw_uint(json_wtr
, map_ids
[i
]);
204 jsonw_end_array(json_wtr
);
207 for (i
= 0; i
< info
.nr_map_ids
; i
++)
208 printf("%u%s", map_ids
[i
],
209 i
== info
.nr_map_ids
- 1 ? "" : ",");
213 static void print_prog_json(struct bpf_prog_info
*info
, int fd
)
217 jsonw_start_object(json_wtr
);
218 jsonw_uint_field(json_wtr
, "id", info
->id
);
219 if (info
->type
< ARRAY_SIZE(prog_type_name
))
220 jsonw_string_field(json_wtr
, "type",
221 prog_type_name
[info
->type
]);
223 jsonw_uint_field(json_wtr
, "type", info
->type
);
226 jsonw_string_field(json_wtr
, "name", info
->name
);
228 jsonw_name(json_wtr
, "tag");
229 jsonw_printf(json_wtr
, "\"" BPF_TAG_FMT
"\"",
230 info
->tag
[0], info
->tag
[1], info
->tag
[2], info
->tag
[3],
231 info
->tag
[4], info
->tag
[5], info
->tag
[6], info
->tag
[7]);
233 if (info
->status
& BPF_PROG_STATUS_DEV_BOUND
) {
234 jsonw_name(json_wtr
, "dev");
236 char name
[IF_NAMESIZE
];
238 if (!if_indextoname(info
->ifindex
, name
))
239 jsonw_printf(json_wtr
, "\"ifindex:%d\"",
242 jsonw_printf(json_wtr
, "\"%s\"", name
);
244 jsonw_printf(json_wtr
, "\"unknown\"");
248 if (info
->load_time
) {
251 print_boot_time(info
->load_time
, buf
, sizeof(buf
));
253 /* Piggy back on load_time, since 0 uid is a valid one */
254 jsonw_string_field(json_wtr
, "loaded_at", buf
);
255 jsonw_uint_field(json_wtr
, "uid", info
->created_by_uid
);
258 jsonw_uint_field(json_wtr
, "bytes_xlated", info
->xlated_prog_len
);
260 if (info
->jited_prog_len
) {
261 jsonw_bool_field(json_wtr
, "jited", true);
262 jsonw_uint_field(json_wtr
, "bytes_jited", info
->jited_prog_len
);
264 jsonw_bool_field(json_wtr
, "jited", false);
267 memlock
= get_fdinfo(fd
, "memlock");
269 jsonw_int_field(json_wtr
, "bytes_memlock", atoi(memlock
));
272 if (info
->nr_map_ids
)
273 show_prog_maps(fd
, info
->nr_map_ids
);
275 jsonw_end_object(json_wtr
);
278 static void print_prog_plain(struct bpf_prog_info
*info
, int fd
)
282 printf("%u: ", info
->id
);
283 if (info
->type
< ARRAY_SIZE(prog_type_name
))
284 printf("%s ", prog_type_name
[info
->type
]);
286 printf("type %u ", info
->type
);
289 printf("name %s ", info
->name
);
292 fprint_hex(stdout
, info
->tag
, BPF_TAG_SIZE
, "");
295 if (info
->status
& BPF_PROG_STATUS_DEV_BOUND
) {
298 char name
[IF_NAMESIZE
];
300 if (!if_indextoname(info
->ifindex
, name
))
301 printf("ifindex:%d ", info
->ifindex
);
310 if (info
->load_time
) {
313 print_boot_time(info
->load_time
, buf
, sizeof(buf
));
315 /* Piggy back on load_time, since 0 uid is a valid one */
316 printf("\tloaded_at %s uid %u\n", buf
, info
->created_by_uid
);
319 printf("\txlated %uB", info
->xlated_prog_len
);
321 if (info
->jited_prog_len
)
322 printf(" jited %uB", info
->jited_prog_len
);
324 printf(" not jited");
326 memlock
= get_fdinfo(fd
, "memlock");
328 printf(" memlock %sB", memlock
);
331 if (info
->nr_map_ids
)
332 show_prog_maps(fd
, info
->nr_map_ids
);
337 static int show_prog(int fd
)
339 struct bpf_prog_info info
= {};
340 __u32 len
= sizeof(info
);
343 err
= bpf_obj_get_info_by_fd(fd
, &info
, &len
);
345 p_err("can't get prog info: %s", strerror(errno
));
350 print_prog_json(&info
, fd
);
352 print_prog_plain(&info
, fd
);
357 static int do_show(int argc
, char **argv
)
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
);
391 p_err("can't get prog by id (%u): %s",
392 id
, strerror(errno
));
404 jsonw_end_array(json_wtr
);
409 static void print_insn(struct bpf_verifier_env
*env
, const char *fmt
, ...)
418 static void dump_xlated_plain(void *buf
, unsigned int len
, bool opcodes
)
420 struct bpf_insn
*insn
= buf
;
421 bool double_insn
= false;
424 for (i
= 0; i
< len
/ sizeof(*insn
); i
++) {
430 double_insn
= insn
[i
].code
== (BPF_LD
| BPF_IMM
| BPF_DW
);
433 print_bpf_insn(print_insn
, NULL
, insn
+ i
, true);
437 fprint_hex(stdout
, insn
+ i
, 8, " ");
438 if (double_insn
&& i
< len
- 1) {
440 fprint_hex(stdout
, insn
+ i
+ 1, 8, " ");
447 static void print_insn_json(struct bpf_verifier_env
*env
, const char *fmt
, ...)
449 unsigned int l
= strlen(fmt
);
455 strncpy(chomped_fmt
, fmt
, l
- 1);
456 chomped_fmt
[l
- 1] = '\0';
458 jsonw_vprintf_enquote(json_wtr
, chomped_fmt
, args
);
462 static void dump_xlated_json(void *buf
, unsigned int len
, bool opcodes
)
464 struct bpf_insn
*insn
= buf
;
465 bool double_insn
= false;
468 jsonw_start_array(json_wtr
);
469 for (i
= 0; i
< len
/ sizeof(*insn
); i
++) {
474 double_insn
= insn
[i
].code
== (BPF_LD
| BPF_IMM
| BPF_DW
);
476 jsonw_start_object(json_wtr
);
477 jsonw_name(json_wtr
, "disasm");
478 print_bpf_insn(print_insn_json
, NULL
, insn
+ i
, true);
481 jsonw_name(json_wtr
, "opcodes");
482 jsonw_start_object(json_wtr
);
484 jsonw_name(json_wtr
, "code");
485 jsonw_printf(json_wtr
, "\"0x%02hhx\"", insn
[i
].code
);
487 jsonw_name(json_wtr
, "src_reg");
488 jsonw_printf(json_wtr
, "\"0x%hhx\"", insn
[i
].src_reg
);
490 jsonw_name(json_wtr
, "dst_reg");
491 jsonw_printf(json_wtr
, "\"0x%hhx\"", insn
[i
].dst_reg
);
493 jsonw_name(json_wtr
, "off");
494 print_hex_data_json((uint8_t *)(&insn
[i
].off
), 2);
496 jsonw_name(json_wtr
, "imm");
497 if (double_insn
&& i
< len
- 1)
498 print_hex_data_json((uint8_t *)(&insn
[i
].imm
),
501 print_hex_data_json((uint8_t *)(&insn
[i
].imm
),
503 jsonw_end_object(json_wtr
);
505 jsonw_end_object(json_wtr
);
507 jsonw_end_array(json_wtr
);
510 static int do_dump(int argc
, char **argv
)
512 struct bpf_prog_info info
= {};
513 __u32 len
= sizeof(info
);
514 unsigned int buf_size
;
515 char *filepath
= NULL
;
516 bool opcodes
= false;
524 if (is_prefix(*argv
, "jited")) {
525 member_len
= &info
.jited_prog_len
;
526 member_ptr
= &info
.jited_prog_insns
;
527 } else if (is_prefix(*argv
, "xlated")) {
528 member_len
= &info
.xlated_prog_len
;
529 member_ptr
= &info
.xlated_prog_insns
;
531 p_err("expected 'xlated' or 'jited', got: %s", *argv
);
539 fd
= prog_parse_fd(&argc
, &argv
);
543 if (is_prefix(*argv
, "file")) {
546 p_err("expected file path");
552 } else if (is_prefix(*argv
, "opcodes")) {
562 err
= bpf_obj_get_info_by_fd(fd
, &info
, &len
);
564 p_err("can't get prog info: %s", strerror(errno
));
569 p_info("no instructions returned");
574 buf_size
= *member_len
;
576 buf
= malloc(buf_size
);
578 p_err("mem alloc failed");
583 memset(&info
, 0, sizeof(info
));
585 *member_ptr
= ptr_to_u64(buf
);
586 *member_len
= buf_size
;
588 err
= bpf_obj_get_info_by_fd(fd
, &info
, &len
);
591 p_err("can't get prog info: %s", strerror(errno
));
595 if (*member_len
> buf_size
) {
596 p_err("too many instructions returned");
601 fd
= open(filepath
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
603 p_err("can't open file %s: %s", filepath
,
608 n
= write(fd
, buf
, *member_len
);
610 if (n
!= *member_len
) {
611 p_err("error writing output file: %s",
612 n
< 0 ? strerror(errno
) : "short write");
616 if (member_len
== &info
.jited_prog_len
)
617 disasm_print_insn(buf
, *member_len
, opcodes
);
620 dump_xlated_json(buf
, *member_len
, opcodes
);
622 dump_xlated_plain(buf
, *member_len
, opcodes
);
634 static int do_pin(int argc
, char **argv
)
638 err
= do_pin_any(argc
, argv
, bpf_prog_get_fd_by_id
);
639 if (!err
&& json_output
)
640 jsonw_null(json_wtr
);
644 static int do_help(int argc
, char **argv
)
647 jsonw_null(json_wtr
);
652 "Usage: %s %s show [PROG]\n"
653 " %s %s dump xlated PROG [{ file FILE | opcodes }]\n"
654 " %s %s dump jited PROG [{ file FILE | opcodes }]\n"
655 " %s %s pin PROG FILE\n"
658 " " HELP_SPEC_PROGRAM
"\n"
659 " " HELP_SPEC_OPTIONS
"\n"
661 bin_name
, argv
[-2], bin_name
, argv
[-2], bin_name
, argv
[-2],
662 bin_name
, argv
[-2], bin_name
, argv
[-2]);
667 static const struct cmd cmds
[] = {
675 int do_prog(int argc
, char **argv
)
677 return cmd_select(cmds
, argc
, argv
, do_help
);