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 static const char * const prog_type_name
[] = {
53 [BPF_PROG_TYPE_UNSPEC
] = "unspec",
54 [BPF_PROG_TYPE_SOCKET_FILTER
] = "socket_filter",
55 [BPF_PROG_TYPE_KPROBE
] = "kprobe",
56 [BPF_PROG_TYPE_SCHED_CLS
] = "sched_cls",
57 [BPF_PROG_TYPE_SCHED_ACT
] = "sched_act",
58 [BPF_PROG_TYPE_TRACEPOINT
] = "tracepoint",
59 [BPF_PROG_TYPE_XDP
] = "xdp",
60 [BPF_PROG_TYPE_PERF_EVENT
] = "perf_event",
61 [BPF_PROG_TYPE_CGROUP_SKB
] = "cgroup_skb",
62 [BPF_PROG_TYPE_CGROUP_SOCK
] = "cgroup_sock",
63 [BPF_PROG_TYPE_LWT_IN
] = "lwt_in",
64 [BPF_PROG_TYPE_LWT_OUT
] = "lwt_out",
65 [BPF_PROG_TYPE_LWT_XMIT
] = "lwt_xmit",
66 [BPF_PROG_TYPE_SOCK_OPS
] = "sock_ops",
67 [BPF_PROG_TYPE_SK_SKB
] = "sk_skb",
70 static void print_boot_time(__u64 nsecs
, char *buf
, unsigned int size
)
72 struct timespec real_time_ts
, boot_time_ts
;
73 time_t wallclock_secs
;
78 if (clock_gettime(CLOCK_REALTIME
, &real_time_ts
) ||
79 clock_gettime(CLOCK_BOOTTIME
, &boot_time_ts
)) {
80 perror("Can't read clocks");
81 snprintf(buf
, size
, "%llu", nsecs
/ 1000000000);
85 wallclock_secs
= (real_time_ts
.tv_sec
- boot_time_ts
.tv_sec
) +
88 if (!localtime_r(&wallclock_secs
, &load_tm
)) {
89 snprintf(buf
, size
, "%llu", nsecs
/ 1000000000);
93 strftime(buf
, size
, "%b %d/%H:%M", &load_tm
);
96 static int prog_fd_by_tag(unsigned char *tag
)
98 struct bpf_prog_info info
= {};
99 __u32 len
= sizeof(info
);
105 err
= bpf_prog_get_next_id(id
, &id
);
107 p_err("%s", strerror(errno
));
111 fd
= bpf_prog_get_fd_by_id(id
);
113 p_err("can't get prog by id (%u): %s",
114 id
, strerror(errno
));
118 err
= bpf_obj_get_info_by_fd(fd
, &info
, &len
);
120 p_err("can't get prog info (%u): %s",
121 id
, strerror(errno
));
126 if (!memcmp(tag
, info
.tag
, BPF_TAG_SIZE
))
133 int prog_parse_fd(int *argc
, char ***argv
)
137 if (is_prefix(**argv
, "id")) {
143 id
= strtoul(**argv
, &endptr
, 0);
145 p_err("can't parse %s as ID", **argv
);
150 fd
= bpf_prog_get_fd_by_id(id
);
152 p_err("get by id (%u): %s", id
, strerror(errno
));
154 } else if (is_prefix(**argv
, "tag")) {
155 unsigned char tag
[BPF_TAG_SIZE
];
159 if (sscanf(**argv
, BPF_TAG_FMT
, tag
, tag
+ 1, tag
+ 2,
160 tag
+ 3, tag
+ 4, tag
+ 5, tag
+ 6, tag
+ 7)
162 p_err("can't parse tag");
167 return prog_fd_by_tag(tag
);
168 } else if (is_prefix(**argv
, "pinned")) {
176 return open_obj_pinned_any(path
, BPF_OBJ_PROG
);
179 p_err("expected 'id', 'tag' or 'pinned', got: '%s'?", **argv
);
183 static void show_prog_maps(int fd
, u32 num_maps
)
185 struct bpf_prog_info info
= {};
186 __u32 len
= sizeof(info
);
187 __u32 map_ids
[num_maps
];
191 info
.nr_map_ids
= num_maps
;
192 info
.map_ids
= ptr_to_u64(map_ids
);
194 err
= bpf_obj_get_info_by_fd(fd
, &info
, &len
);
195 if (err
|| !info
.nr_map_ids
)
199 jsonw_name(json_wtr
, "map_ids");
200 jsonw_start_array(json_wtr
);
201 for (i
= 0; i
< info
.nr_map_ids
; i
++)
202 jsonw_uint(json_wtr
, map_ids
[i
]);
203 jsonw_end_array(json_wtr
);
206 for (i
= 0; i
< info
.nr_map_ids
; i
++)
207 printf("%u%s", map_ids
[i
],
208 i
== info
.nr_map_ids
- 1 ? "" : ",");
212 static void print_prog_json(struct bpf_prog_info
*info
, int fd
)
216 jsonw_start_object(json_wtr
);
217 jsonw_uint_field(json_wtr
, "id", info
->id
);
218 if (info
->type
< ARRAY_SIZE(prog_type_name
))
219 jsonw_string_field(json_wtr
, "type",
220 prog_type_name
[info
->type
]);
222 jsonw_uint_field(json_wtr
, "type", info
->type
);
225 jsonw_string_field(json_wtr
, "name", info
->name
);
227 jsonw_name(json_wtr
, "tag");
228 jsonw_printf(json_wtr
, "\"" BPF_TAG_FMT
"\"",
229 info
->tag
[0], info
->tag
[1], info
->tag
[2], info
->tag
[3],
230 info
->tag
[4], info
->tag
[5], info
->tag
[6], info
->tag
[7]);
232 if (info
->load_time
) {
235 print_boot_time(info
->load_time
, buf
, sizeof(buf
));
237 /* Piggy back on load_time, since 0 uid is a valid one */
238 jsonw_string_field(json_wtr
, "loaded_at", buf
);
239 jsonw_uint_field(json_wtr
, "uid", info
->created_by_uid
);
242 jsonw_uint_field(json_wtr
, "bytes_xlated", info
->xlated_prog_len
);
244 if (info
->jited_prog_len
) {
245 jsonw_bool_field(json_wtr
, "jited", true);
246 jsonw_uint_field(json_wtr
, "bytes_jited", info
->jited_prog_len
);
248 jsonw_bool_field(json_wtr
, "jited", false);
251 memlock
= get_fdinfo(fd
, "memlock");
253 jsonw_int_field(json_wtr
, "bytes_memlock", atoi(memlock
));
256 if (info
->nr_map_ids
)
257 show_prog_maps(fd
, info
->nr_map_ids
);
259 if (!hash_empty(prog_table
.table
)) {
260 struct pinned_obj
*obj
;
262 jsonw_name(json_wtr
, "pinned");
263 jsonw_start_array(json_wtr
);
264 hash_for_each_possible(prog_table
.table
, obj
, hash
, info
->id
) {
265 if (obj
->id
== info
->id
)
266 jsonw_string(json_wtr
, obj
->path
);
268 jsonw_end_array(json_wtr
);
271 jsonw_end_object(json_wtr
);
274 static void print_prog_plain(struct bpf_prog_info
*info
, int fd
)
278 printf("%u: ", info
->id
);
279 if (info
->type
< ARRAY_SIZE(prog_type_name
))
280 printf("%s ", prog_type_name
[info
->type
]);
282 printf("type %u ", info
->type
);
285 printf("name %s ", info
->name
);
288 fprint_hex(stdout
, info
->tag
, BPF_TAG_SIZE
, "");
291 if (info
->load_time
) {
294 print_boot_time(info
->load_time
, buf
, sizeof(buf
));
296 /* Piggy back on load_time, since 0 uid is a valid one */
297 printf("\tloaded_at %s uid %u\n", buf
, info
->created_by_uid
);
300 printf("\txlated %uB", info
->xlated_prog_len
);
302 if (info
->jited_prog_len
)
303 printf(" jited %uB", info
->jited_prog_len
);
305 printf(" not jited");
307 memlock
= get_fdinfo(fd
, "memlock");
309 printf(" memlock %sB", memlock
);
312 if (info
->nr_map_ids
)
313 show_prog_maps(fd
, info
->nr_map_ids
);
315 if (!hash_empty(prog_table
.table
)) {
316 struct pinned_obj
*obj
;
319 hash_for_each_possible(prog_table
.table
, obj
, hash
, info
->id
) {
320 if (obj
->id
== info
->id
)
321 printf("\tpinned %s\n", obj
->path
);
328 static int show_prog(int fd
)
330 struct bpf_prog_info info
= {};
331 __u32 len
= sizeof(info
);
334 err
= bpf_obj_get_info_by_fd(fd
, &info
, &len
);
336 p_err("can't get prog info: %s", strerror(errno
));
341 print_prog_json(&info
, fd
);
343 print_prog_plain(&info
, fd
);
348 static int do_show(int argc
, char **argv
)
355 build_pinned_obj_table(&prog_table
, BPF_OBJ_PROG
);
358 fd
= prog_parse_fd(&argc
, &argv
);
362 return show_prog(fd
);
369 jsonw_start_array(json_wtr
);
371 err
= bpf_prog_get_next_id(id
, &id
);
373 if (errno
== ENOENT
) {
377 p_err("can't get next program: %s%s", strerror(errno
),
378 errno
== EINVAL
? " -- kernel too old?" : "");
383 fd
= bpf_prog_get_fd_by_id(id
);
387 p_err("can't get prog by id (%u): %s",
388 id
, strerror(errno
));
400 jsonw_end_array(json_wtr
);
405 static void print_insn(struct bpf_verifier_env
*env
, const char *fmt
, ...)
414 static void dump_xlated_plain(void *buf
, unsigned int len
, bool opcodes
)
416 struct bpf_insn
*insn
= buf
;
417 bool double_insn
= false;
420 for (i
= 0; i
< len
/ sizeof(*insn
); i
++) {
426 double_insn
= insn
[i
].code
== (BPF_LD
| BPF_IMM
| BPF_DW
);
429 print_bpf_insn(print_insn
, NULL
, insn
+ i
, true);
433 fprint_hex(stdout
, insn
+ i
, 8, " ");
434 if (double_insn
&& i
< len
- 1) {
436 fprint_hex(stdout
, insn
+ i
+ 1, 8, " ");
443 static void print_insn_json(struct bpf_verifier_env
*env
, const char *fmt
, ...)
445 unsigned int l
= strlen(fmt
);
451 strncpy(chomped_fmt
, fmt
, l
- 1);
452 chomped_fmt
[l
- 1] = '\0';
454 jsonw_vprintf_enquote(json_wtr
, chomped_fmt
, args
);
458 static void dump_xlated_json(void *buf
, unsigned int len
, bool opcodes
)
460 struct bpf_insn
*insn
= buf
;
461 bool double_insn
= false;
464 jsonw_start_array(json_wtr
);
465 for (i
= 0; i
< len
/ sizeof(*insn
); i
++) {
470 double_insn
= insn
[i
].code
== (BPF_LD
| BPF_IMM
| BPF_DW
);
472 jsonw_start_object(json_wtr
);
473 jsonw_name(json_wtr
, "disasm");
474 print_bpf_insn(print_insn_json
, NULL
, insn
+ i
, true);
477 jsonw_name(json_wtr
, "opcodes");
478 jsonw_start_object(json_wtr
);
480 jsonw_name(json_wtr
, "code");
481 jsonw_printf(json_wtr
, "\"0x%02hhx\"", insn
[i
].code
);
483 jsonw_name(json_wtr
, "src_reg");
484 jsonw_printf(json_wtr
, "\"0x%hhx\"", insn
[i
].src_reg
);
486 jsonw_name(json_wtr
, "dst_reg");
487 jsonw_printf(json_wtr
, "\"0x%hhx\"", insn
[i
].dst_reg
);
489 jsonw_name(json_wtr
, "off");
490 print_hex_data_json((uint8_t *)(&insn
[i
].off
), 2);
492 jsonw_name(json_wtr
, "imm");
493 if (double_insn
&& i
< len
- 1)
494 print_hex_data_json((uint8_t *)(&insn
[i
].imm
),
497 print_hex_data_json((uint8_t *)(&insn
[i
].imm
),
499 jsonw_end_object(json_wtr
);
501 jsonw_end_object(json_wtr
);
503 jsonw_end_array(json_wtr
);
506 static int do_dump(int argc
, char **argv
)
508 struct bpf_prog_info info
= {};
509 __u32 len
= sizeof(info
);
510 unsigned int buf_size
;
511 char *filepath
= NULL
;
512 bool opcodes
= false;
520 if (is_prefix(*argv
, "jited")) {
521 member_len
= &info
.jited_prog_len
;
522 member_ptr
= &info
.jited_prog_insns
;
523 } else if (is_prefix(*argv
, "xlated")) {
524 member_len
= &info
.xlated_prog_len
;
525 member_ptr
= &info
.xlated_prog_insns
;
527 p_err("expected 'xlated' or 'jited', got: %s", *argv
);
535 fd
= prog_parse_fd(&argc
, &argv
);
539 if (is_prefix(*argv
, "file")) {
542 p_err("expected file path");
548 } else if (is_prefix(*argv
, "opcodes")) {
558 err
= bpf_obj_get_info_by_fd(fd
, &info
, &len
);
560 p_err("can't get prog info: %s", strerror(errno
));
565 p_info("no instructions returned");
570 buf_size
= *member_len
;
572 buf
= malloc(buf_size
);
574 p_err("mem alloc failed");
579 memset(&info
, 0, sizeof(info
));
581 *member_ptr
= ptr_to_u64(buf
);
582 *member_len
= buf_size
;
584 err
= bpf_obj_get_info_by_fd(fd
, &info
, &len
);
587 p_err("can't get prog info: %s", strerror(errno
));
591 if (*member_len
> buf_size
) {
592 p_err("too many instructions returned");
597 fd
= open(filepath
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
599 p_err("can't open file %s: %s", filepath
,
604 n
= write(fd
, buf
, *member_len
);
606 if (n
!= *member_len
) {
607 p_err("error writing output file: %s",
608 n
< 0 ? strerror(errno
) : "short write");
612 if (member_len
== &info
.jited_prog_len
)
613 disasm_print_insn(buf
, *member_len
, opcodes
);
616 dump_xlated_json(buf
, *member_len
, opcodes
);
618 dump_xlated_plain(buf
, *member_len
, opcodes
);
630 static int do_pin(int argc
, char **argv
)
634 err
= do_pin_any(argc
, argv
, bpf_prog_get_fd_by_id
);
635 if (!err
&& json_output
)
636 jsonw_null(json_wtr
);
640 static int do_help(int argc
, char **argv
)
643 jsonw_null(json_wtr
);
648 "Usage: %s %s show [PROG]\n"
649 " %s %s dump xlated PROG [{ file FILE | opcodes }]\n"
650 " %s %s dump jited PROG [{ file FILE | opcodes }]\n"
651 " %s %s pin PROG FILE\n"
654 " " HELP_SPEC_PROGRAM
"\n"
655 " " HELP_SPEC_OPTIONS
"\n"
657 bin_name
, argv
[-2], bin_name
, argv
[-2], bin_name
, argv
[-2],
658 bin_name
, argv
[-2], bin_name
, argv
[-2]);
663 static const struct cmd cmds
[] = {
671 int do_prog(int argc
, char **argv
)
673 return cmd_select(cmds
, argc
, argv
, do_help
);