blob: f373f2baef5a16ee289f60822a256eabef441162 [file] [log] [blame]
Jakub Kicinski71bb4282017-10-04 20:10:04 -07001/*
2 * Copyright (C) 2017 Netronome Systems, Inc.
3 *
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.
8 *
9 * The BSD 2-Clause License:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
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.
23 *
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
31 * SOFTWARE.
32 */
33
34/* Author: Jakub Kicinski <kubakici@wp.pl> */
35
36#include <errno.h>
37#include <fcntl.h>
Jakub Kicinskic9c35992017-10-09 10:30:13 -070038#include <stdarg.h>
Jakub Kicinski71bb4282017-10-04 20:10:04 -070039#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include <time.h>
43#include <unistd.h>
44#include <sys/types.h>
45#include <sys/stat.h>
46
47#include <bpf.h>
48
49#include "main.h"
Jakub Kicinskic9c35992017-10-09 10:30:13 -070050#include "disasm.h"
Jakub Kicinski71bb4282017-10-04 20:10:04 -070051
52static 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",
68};
69
70static void print_boot_time(__u64 nsecs, char *buf, unsigned int size)
71{
72 struct timespec real_time_ts, boot_time_ts;
73 time_t wallclock_secs;
74 struct tm load_tm;
75
76 buf[--size] = '\0';
77
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);
82 return;
83 }
84
85 wallclock_secs = (real_time_ts.tv_sec - boot_time_ts.tv_sec) +
86 nsecs / 1000000000;
87
88 if (!localtime_r(&wallclock_secs, &load_tm)) {
89 snprintf(buf, size, "%llu", nsecs / 1000000000);
90 return;
91 }
92
93 strftime(buf, size, "%b %d/%H:%M", &load_tm);
94}
95
96static int prog_fd_by_tag(unsigned char *tag)
97{
98 struct bpf_prog_info info = {};
99 __u32 len = sizeof(info);
100 unsigned int id = 0;
101 int err;
102 int fd;
103
104 while (true) {
105 err = bpf_prog_get_next_id(id, &id);
106 if (err) {
107 err("%s\n", strerror(errno));
108 return -1;
109 }
110
111 fd = bpf_prog_get_fd_by_id(id);
112 if (fd < 0) {
113 err("can't get prog by id (%u): %s\n",
114 id, strerror(errno));
115 return -1;
116 }
117
118 err = bpf_obj_get_info_by_fd(fd, &info, &len);
119 if (err) {
120 err("can't get prog info (%u): %s\n",
121 id, strerror(errno));
122 close(fd);
123 return -1;
124 }
125
126 if (!memcmp(tag, info.tag, BPF_TAG_SIZE))
127 return fd;
128
129 close(fd);
130 }
131}
132
133int prog_parse_fd(int *argc, char ***argv)
134{
135 int fd;
136
137 if (is_prefix(**argv, "id")) {
138 unsigned int id;
139 char *endptr;
140
141 NEXT_ARGP();
142
143 id = strtoul(**argv, &endptr, 0);
144 if (*endptr) {
145 err("can't parse %s as ID\n", **argv);
146 return -1;
147 }
148 NEXT_ARGP();
149
150 fd = bpf_prog_get_fd_by_id(id);
151 if (fd < 0)
152 err("get by id (%u): %s\n", id, strerror(errno));
153 return fd;
154 } else if (is_prefix(**argv, "tag")) {
155 unsigned char tag[BPF_TAG_SIZE];
156
157 NEXT_ARGP();
158
159 if (sscanf(**argv, BPF_TAG_FMT, tag, tag + 1, tag + 2,
160 tag + 3, tag + 4, tag + 5, tag + 6, tag + 7)
161 != BPF_TAG_SIZE) {
162 err("can't parse tag\n");
163 return -1;
164 }
165 NEXT_ARGP();
166
167 return prog_fd_by_tag(tag);
168 } else if (is_prefix(**argv, "pinned")) {
169 char *path;
170
171 NEXT_ARGP();
172
173 path = **argv;
174 NEXT_ARGP();
175
176 return open_obj_pinned_any(path, BPF_OBJ_PROG);
177 }
178
179 err("expected 'id', 'tag' or 'pinned', got: '%s'?\n", **argv);
180 return -1;
181}
182
183static void show_prog_maps(int fd, u32 num_maps)
184{
185 struct bpf_prog_info info = {};
186 __u32 len = sizeof(info);
187 __u32 map_ids[num_maps];
188 unsigned int i;
189 int err;
190
191 info.nr_map_ids = num_maps;
192 info.map_ids = ptr_to_u64(map_ids);
193
194 err = bpf_obj_get_info_by_fd(fd, &info, &len);
195 if (err || !info.nr_map_ids)
196 return;
197
Quentin Monnet743cc662017-10-23 09:24:08 -0700198 if (json_output) {
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);
204 } else {
205 printf(" map_ids ");
206 for (i = 0; i < info.nr_map_ids; i++)
207 printf("%u%s", map_ids[i],
208 i == info.nr_map_ids - 1 ? "" : ",");
209 }
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700210}
211
Quentin Monnet743cc662017-10-23 09:24:08 -0700212static void print_prog_json(struct bpf_prog_info *info, int fd)
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700213{
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700214 char *memlock;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700215
Quentin Monnet743cc662017-10-23 09:24:08 -0700216 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]);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700221 else
Quentin Monnet743cc662017-10-23 09:24:08 -0700222 jsonw_uint_field(json_wtr, "type", info->type);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700223
Quentin Monnet743cc662017-10-23 09:24:08 -0700224 if (*info->name)
225 jsonw_string_field(json_wtr, "name", info->name);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700226
Quentin Monnet743cc662017-10-23 09:24:08 -0700227 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]);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700231
Quentin Monnet743cc662017-10-23 09:24:08 -0700232 if (info->load_time) {
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700233 char buf[32];
234
Quentin Monnet743cc662017-10-23 09:24:08 -0700235 print_boot_time(info->load_time, buf, sizeof(buf));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700236
237 /* Piggy back on load_time, since 0 uid is a valid one */
Quentin Monnet743cc662017-10-23 09:24:08 -0700238 jsonw_string_field(json_wtr, "loaded_at", buf);
239 jsonw_uint_field(json_wtr, "uid", info->created_by_uid);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700240 }
241
Quentin Monnet743cc662017-10-23 09:24:08 -0700242 jsonw_uint_field(json_wtr, "bytes_xlated", info->xlated_prog_len);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700243
Quentin Monnet743cc662017-10-23 09:24:08 -0700244 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);
247 } else {
248 jsonw_bool_field(json_wtr, "jited", false);
249 }
250
251 memlock = get_fdinfo(fd, "memlock");
252 if (memlock)
253 jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock));
254 free(memlock);
255
256 if (info->nr_map_ids)
257 show_prog_maps(fd, info->nr_map_ids);
258
259 jsonw_end_object(json_wtr);
260}
261
262static void print_prog_plain(struct bpf_prog_info *info, int fd)
263{
264 char *memlock;
265
266 printf("%u: ", info->id);
267 if (info->type < ARRAY_SIZE(prog_type_name))
268 printf("%s ", prog_type_name[info->type]);
269 else
270 printf("type %u ", info->type);
271
272 if (*info->name)
273 printf("name %s ", info->name);
274
275 printf("tag ");
276 fprint_hex(stdout, info->tag, BPF_TAG_SIZE, "");
277 printf("\n");
278
279 if (info->load_time) {
280 char buf[32];
281
282 print_boot_time(info->load_time, buf, sizeof(buf));
283
284 /* Piggy back on load_time, since 0 uid is a valid one */
285 printf("\tloaded_at %s uid %u\n", buf, info->created_by_uid);
286 }
287
288 printf("\txlated %uB", info->xlated_prog_len);
289
290 if (info->jited_prog_len)
291 printf(" jited %uB", info->jited_prog_len);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700292 else
293 printf(" not jited");
294
295 memlock = get_fdinfo(fd, "memlock");
296 if (memlock)
297 printf(" memlock %sB", memlock);
298 free(memlock);
299
Quentin Monnet743cc662017-10-23 09:24:08 -0700300 if (info->nr_map_ids)
301 show_prog_maps(fd, info->nr_map_ids);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700302
303 printf("\n");
Quentin Monnet743cc662017-10-23 09:24:08 -0700304}
305
306static int show_prog(int fd)
307{
308 struct bpf_prog_info info = {};
309 __u32 len = sizeof(info);
310 int err;
311
312 err = bpf_obj_get_info_by_fd(fd, &info, &len);
313 if (err) {
314 err("can't get prog info: %s\n", strerror(errno));
315 return -1;
316 }
317
318 if (json_output)
319 print_prog_json(&info, fd);
320 else
321 print_prog_plain(&info, fd);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700322
323 return 0;
324}
325
326static int do_show(int argc, char **argv)
Quentin Monnet743cc662017-10-23 09:24:08 -0700327{
328 __u32 id = 0;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700329 int err;
330 int fd;
331
332 if (argc == 2) {
333 fd = prog_parse_fd(&argc, &argv);
334 if (fd < 0)
335 return -1;
336
337 return show_prog(fd);
338 }
339
340 if (argc)
341 return BAD_ARG();
342
Quentin Monnet743cc662017-10-23 09:24:08 -0700343 if (json_output)
344 jsonw_start_array(json_wtr);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700345 while (true) {
346 err = bpf_prog_get_next_id(id, &id);
347 if (err) {
Quentin Monnet1739c262017-10-19 15:46:20 -0700348 if (errno == ENOENT) {
349 err = 0;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700350 break;
Quentin Monnet1739c262017-10-19 15:46:20 -0700351 }
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700352 err("can't get next program: %s\n", strerror(errno));
353 if (errno == EINVAL)
354 err("kernel too old?\n");
Quentin Monnet743cc662017-10-23 09:24:08 -0700355 err = -1;
356 break;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700357 }
358
359 fd = bpf_prog_get_fd_by_id(id);
360 if (fd < 0) {
361 err("can't get prog by id (%u): %s\n",
362 id, strerror(errno));
Quentin Monnet743cc662017-10-23 09:24:08 -0700363 err = -1;
364 break;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700365 }
366
367 err = show_prog(fd);
368 close(fd);
369 if (err)
Quentin Monnet743cc662017-10-23 09:24:08 -0700370 break;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700371 }
372
Quentin Monnet743cc662017-10-23 09:24:08 -0700373 if (json_output)
374 jsonw_end_array(json_wtr);
375
376 return err;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700377}
378
Jakub Kicinskic9c35992017-10-09 10:30:13 -0700379static void print_insn(struct bpf_verifier_env *env, const char *fmt, ...)
380{
381 va_list args;
382
383 va_start(args, fmt);
384 vprintf(fmt, args);
385 va_end(args);
386}
387
388static void dump_xlated(void *buf, unsigned int len, bool opcodes)
389{
390 struct bpf_insn *insn = buf;
Quentin Monnet9e2308c2017-10-19 15:46:24 -0700391 bool double_insn = false;
Jakub Kicinskic9c35992017-10-09 10:30:13 -0700392 unsigned int i;
393
394 for (i = 0; i < len / sizeof(*insn); i++) {
Quentin Monnet9e2308c2017-10-19 15:46:24 -0700395 if (double_insn) {
396 double_insn = false;
397 continue;
398 }
399
400 double_insn = insn[i].code == (BPF_LD | BPF_IMM | BPF_DW);
401
Jakub Kicinskic9c35992017-10-09 10:30:13 -0700402 printf("% 4d: ", i);
403 print_bpf_insn(print_insn, NULL, insn + i, true);
404
405 if (opcodes) {
406 printf(" ");
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700407 fprint_hex(stdout, insn + i, 8, " ");
Quentin Monnet9e2308c2017-10-19 15:46:24 -0700408 if (double_insn && i < len - 1) {
409 printf(" ");
410 fprint_hex(stdout, insn + i + 1, 8, " ");
411 }
Jakub Kicinskic9c35992017-10-09 10:30:13 -0700412 printf("\n");
413 }
Jakub Kicinskic9c35992017-10-09 10:30:13 -0700414 }
415}
416
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700417static int do_dump(int argc, char **argv)
418{
419 struct bpf_prog_info info = {};
420 __u32 len = sizeof(info);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700421 unsigned int buf_size;
422 char *filepath = NULL;
423 bool opcodes = false;
424 unsigned char *buf;
425 __u32 *member_len;
426 __u64 *member_ptr;
427 ssize_t n;
428 int err;
429 int fd;
430
431 if (is_prefix(*argv, "jited")) {
432 member_len = &info.jited_prog_len;
433 member_ptr = &info.jited_prog_insns;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700434 } else if (is_prefix(*argv, "xlated")) {
435 member_len = &info.xlated_prog_len;
436 member_ptr = &info.xlated_prog_insns;
437 } else {
438 err("expected 'xlated' or 'jited', got: %s\n", *argv);
439 return -1;
440 }
441 NEXT_ARG();
442
443 if (argc < 2)
444 usage();
445
446 fd = prog_parse_fd(&argc, &argv);
447 if (fd < 0)
448 return -1;
449
450 if (is_prefix(*argv, "file")) {
451 NEXT_ARG();
452 if (!argc) {
453 err("expected file path\n");
454 return -1;
455 }
456
457 filepath = *argv;
458 NEXT_ARG();
459 } else if (is_prefix(*argv, "opcodes")) {
460 opcodes = true;
461 NEXT_ARG();
462 }
463
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700464 if (argc) {
465 usage();
466 return -1;
467 }
468
469 err = bpf_obj_get_info_by_fd(fd, &info, &len);
470 if (err) {
471 err("can't get prog info: %s\n", strerror(errno));
472 return -1;
473 }
474
475 if (!*member_len) {
476 info("no instructions returned\n");
477 close(fd);
478 return 0;
479 }
480
481 buf_size = *member_len;
482
483 buf = malloc(buf_size);
484 if (!buf) {
485 err("mem alloc failed\n");
486 close(fd);
487 return -1;
488 }
489
490 memset(&info, 0, sizeof(info));
491
492 *member_ptr = ptr_to_u64(buf);
493 *member_len = buf_size;
494
495 err = bpf_obj_get_info_by_fd(fd, &info, &len);
496 close(fd);
497 if (err) {
498 err("can't get prog info: %s\n", strerror(errno));
499 goto err_free;
500 }
501
502 if (*member_len > buf_size) {
Quentin Monnet1d844872017-10-19 15:46:21 -0700503 err("too many instructions returned\n");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700504 goto err_free;
505 }
506
507 if (filepath) {
508 fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
509 if (fd < 0) {
510 err("can't open file %s: %s\n", filepath,
511 strerror(errno));
512 goto err_free;
513 }
514
515 n = write(fd, buf, *member_len);
516 close(fd);
517 if (n != *member_len) {
518 err("error writing output file: %s\n",
519 n < 0 ? strerror(errno) : "short write");
520 goto err_free;
521 }
522 } else {
Jakub Kicinskic9c35992017-10-09 10:30:13 -0700523 if (member_len == &info.jited_prog_len)
524 disasm_print_insn(buf, *member_len, opcodes);
525 else
526 dump_xlated(buf, *member_len, opcodes);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700527 }
528
529 free(buf);
530
531 return 0;
532
533err_free:
534 free(buf);
535 return -1;
536}
537
538static int do_pin(int argc, char **argv)
539{
540 return do_pin_any(argc, argv, bpf_prog_get_fd_by_id);
541}
542
543static int do_help(int argc, char **argv)
544{
545 fprintf(stderr,
546 "Usage: %s %s show [PROG]\n"
Quentin Monnet8dfbc6d2017-10-19 15:46:25 -0700547 " %s %s dump xlated PROG [{ file FILE | opcodes }]\n"
548 " %s %s dump jited PROG [{ file FILE | opcodes }]\n"
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700549 " %s %s pin PROG FILE\n"
550 " %s %s help\n"
551 "\n"
552 " " HELP_SPEC_PROGRAM "\n"
553 "",
554 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
555 bin_name, argv[-2], bin_name, argv[-2]);
556
557 return 0;
558}
559
560static const struct cmd cmds[] = {
561 { "show", do_show },
Quentin Monnet9f606172017-10-19 15:46:22 -0700562 { "help", do_help },
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700563 { "dump", do_dump },
564 { "pin", do_pin },
565 { 0 }
566};
567
568int do_prog(int argc, char **argv)
569{
570 return cmd_select(cmds, argc, argv, do_help);
571}