blob: 57edbea2fbe8293e474bac7909fccd76302bfe4a [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
198 printf(" map_ids ");
199 for (i = 0; i < info.nr_map_ids; i++)
200 printf("%u%s", map_ids[i],
201 i == info.nr_map_ids - 1 ? "" : ",");
202}
203
204static int show_prog(int fd)
205{
206 struct bpf_prog_info info = {};
207 __u32 len = sizeof(info);
208 char *memlock;
209 int err;
210
211 err = bpf_obj_get_info_by_fd(fd, &info, &len);
212 if (err) {
213 err("can't get prog info: %s\n", strerror(errno));
214 return -1;
215 }
216
217 printf("%u: ", info.id);
218 if (info.type < ARRAY_SIZE(prog_type_name))
219 printf("%s ", prog_type_name[info.type]);
220 else
221 printf("type %u ", info.type);
222
223 if (*info.name)
224 printf("name %s ", info.name);
225
226 printf("tag ");
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700227 fprint_hex(stdout, info.tag, BPF_TAG_SIZE, "");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700228 printf("\n");
229
230 if (info.load_time) {
231 char buf[32];
232
233 print_boot_time(info.load_time, buf, sizeof(buf));
234
235 /* Piggy back on load_time, since 0 uid is a valid one */
236 printf("\tloaded_at %s uid %u\n", buf, info.created_by_uid);
237 }
238
239 printf("\txlated %uB", info.xlated_prog_len);
240
241 if (info.jited_prog_len)
242 printf(" jited %uB", info.jited_prog_len);
243 else
244 printf(" not jited");
245
246 memlock = get_fdinfo(fd, "memlock");
247 if (memlock)
248 printf(" memlock %sB", memlock);
249 free(memlock);
250
251 if (info.nr_map_ids)
252 show_prog_maps(fd, info.nr_map_ids);
253
254 printf("\n");
255
256 return 0;
257}
258
259static int do_show(int argc, char **argv)
260{ __u32 id = 0;
261 int err;
262 int fd;
263
264 if (argc == 2) {
265 fd = prog_parse_fd(&argc, &argv);
266 if (fd < 0)
267 return -1;
268
269 return show_prog(fd);
270 }
271
272 if (argc)
273 return BAD_ARG();
274
275 while (true) {
276 err = bpf_prog_get_next_id(id, &id);
277 if (err) {
Quentin Monnet1739c262017-10-19 15:46:20 -0700278 if (errno == ENOENT) {
279 err = 0;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700280 break;
Quentin Monnet1739c262017-10-19 15:46:20 -0700281 }
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700282 err("can't get next program: %s\n", strerror(errno));
283 if (errno == EINVAL)
284 err("kernel too old?\n");
285 return -1;
286 }
287
288 fd = bpf_prog_get_fd_by_id(id);
289 if (fd < 0) {
290 err("can't get prog by id (%u): %s\n",
291 id, strerror(errno));
292 return -1;
293 }
294
295 err = show_prog(fd);
296 close(fd);
297 if (err)
298 return err;
299 }
300
301 return 0;
302}
303
Jakub Kicinskic9c35992017-10-09 10:30:13 -0700304static void print_insn(struct bpf_verifier_env *env, const char *fmt, ...)
305{
306 va_list args;
307
308 va_start(args, fmt);
309 vprintf(fmt, args);
310 va_end(args);
311}
312
313static void dump_xlated(void *buf, unsigned int len, bool opcodes)
314{
315 struct bpf_insn *insn = buf;
Quentin Monnet9e2308c2017-10-19 15:46:24 -0700316 bool double_insn = false;
Jakub Kicinskic9c35992017-10-09 10:30:13 -0700317 unsigned int i;
318
319 for (i = 0; i < len / sizeof(*insn); i++) {
Quentin Monnet9e2308c2017-10-19 15:46:24 -0700320 if (double_insn) {
321 double_insn = false;
322 continue;
323 }
324
325 double_insn = insn[i].code == (BPF_LD | BPF_IMM | BPF_DW);
326
Jakub Kicinskic9c35992017-10-09 10:30:13 -0700327 printf("% 4d: ", i);
328 print_bpf_insn(print_insn, NULL, insn + i, true);
329
330 if (opcodes) {
331 printf(" ");
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700332 fprint_hex(stdout, insn + i, 8, " ");
Quentin Monnet9e2308c2017-10-19 15:46:24 -0700333 if (double_insn && i < len - 1) {
334 printf(" ");
335 fprint_hex(stdout, insn + i + 1, 8, " ");
336 }
Jakub Kicinskic9c35992017-10-09 10:30:13 -0700337 printf("\n");
338 }
Jakub Kicinskic9c35992017-10-09 10:30:13 -0700339 }
340}
341
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700342static int do_dump(int argc, char **argv)
343{
344 struct bpf_prog_info info = {};
345 __u32 len = sizeof(info);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700346 unsigned int buf_size;
347 char *filepath = NULL;
348 bool opcodes = false;
349 unsigned char *buf;
350 __u32 *member_len;
351 __u64 *member_ptr;
352 ssize_t n;
353 int err;
354 int fd;
355
356 if (is_prefix(*argv, "jited")) {
357 member_len = &info.jited_prog_len;
358 member_ptr = &info.jited_prog_insns;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700359 } else if (is_prefix(*argv, "xlated")) {
360 member_len = &info.xlated_prog_len;
361 member_ptr = &info.xlated_prog_insns;
362 } else {
363 err("expected 'xlated' or 'jited', got: %s\n", *argv);
364 return -1;
365 }
366 NEXT_ARG();
367
368 if (argc < 2)
369 usage();
370
371 fd = prog_parse_fd(&argc, &argv);
372 if (fd < 0)
373 return -1;
374
375 if (is_prefix(*argv, "file")) {
376 NEXT_ARG();
377 if (!argc) {
378 err("expected file path\n");
379 return -1;
380 }
381
382 filepath = *argv;
383 NEXT_ARG();
384 } else if (is_prefix(*argv, "opcodes")) {
385 opcodes = true;
386 NEXT_ARG();
387 }
388
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700389 if (argc) {
390 usage();
391 return -1;
392 }
393
394 err = bpf_obj_get_info_by_fd(fd, &info, &len);
395 if (err) {
396 err("can't get prog info: %s\n", strerror(errno));
397 return -1;
398 }
399
400 if (!*member_len) {
401 info("no instructions returned\n");
402 close(fd);
403 return 0;
404 }
405
406 buf_size = *member_len;
407
408 buf = malloc(buf_size);
409 if (!buf) {
410 err("mem alloc failed\n");
411 close(fd);
412 return -1;
413 }
414
415 memset(&info, 0, sizeof(info));
416
417 *member_ptr = ptr_to_u64(buf);
418 *member_len = buf_size;
419
420 err = bpf_obj_get_info_by_fd(fd, &info, &len);
421 close(fd);
422 if (err) {
423 err("can't get prog info: %s\n", strerror(errno));
424 goto err_free;
425 }
426
427 if (*member_len > buf_size) {
Quentin Monnet1d844872017-10-19 15:46:21 -0700428 err("too many instructions returned\n");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700429 goto err_free;
430 }
431
432 if (filepath) {
433 fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
434 if (fd < 0) {
435 err("can't open file %s: %s\n", filepath,
436 strerror(errno));
437 goto err_free;
438 }
439
440 n = write(fd, buf, *member_len);
441 close(fd);
442 if (n != *member_len) {
443 err("error writing output file: %s\n",
444 n < 0 ? strerror(errno) : "short write");
445 goto err_free;
446 }
447 } else {
Jakub Kicinskic9c35992017-10-09 10:30:13 -0700448 if (member_len == &info.jited_prog_len)
449 disasm_print_insn(buf, *member_len, opcodes);
450 else
451 dump_xlated(buf, *member_len, opcodes);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700452 }
453
454 free(buf);
455
456 return 0;
457
458err_free:
459 free(buf);
460 return -1;
461}
462
463static int do_pin(int argc, char **argv)
464{
465 return do_pin_any(argc, argv, bpf_prog_get_fd_by_id);
466}
467
468static int do_help(int argc, char **argv)
469{
470 fprintf(stderr,
471 "Usage: %s %s show [PROG]\n"
Jakub Kicinskic9c35992017-10-09 10:30:13 -0700472 " %s %s dump xlated PROG [file FILE] [opcodes]\n"
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700473 " %s %s dump jited PROG [file FILE] [opcodes]\n"
474 " %s %s pin PROG FILE\n"
475 " %s %s help\n"
476 "\n"
477 " " HELP_SPEC_PROGRAM "\n"
478 "",
479 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
480 bin_name, argv[-2], bin_name, argv[-2]);
481
482 return 0;
483}
484
485static const struct cmd cmds[] = {
486 { "show", do_show },
Quentin Monnet9f606172017-10-19 15:46:22 -0700487 { "help", do_help },
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700488 { "dump", do_dump },
489 { "pin", do_pin },
490 { 0 }
491};
492
493int do_prog(int argc, char **argv)
494{
495 return cmd_select(cmds, argc, argv, do_help);
496}