Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 1 | /* |
| 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 Kicinski | c9c3599 | 2017-10-09 10:30:13 -0700 | [diff] [blame] | 38 | #include <stdarg.h> |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 39 | #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 Kicinski | c9c3599 | 2017-10-09 10:30:13 -0700 | [diff] [blame] | 50 | #include "disasm.h" |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 51 | |
| 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", |
| 68 | }; |
| 69 | |
| 70 | static 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 | |
| 96 | static 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 | |
| 133 | int 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 | |
| 183 | static 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 | |
| 204 | static 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 Monnet | 9cbe1f58 | 2017-10-19 15:46:19 -0700 | [diff] [blame] | 227 | fprint_hex(stdout, info.tag, BPF_TAG_SIZE, ""); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 228 | 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 | |
| 259 | static 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 Monnet | 1739c26 | 2017-10-19 15:46:20 -0700 | [diff] [blame] | 278 | if (errno == ENOENT) { |
| 279 | err = 0; |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 280 | break; |
Quentin Monnet | 1739c26 | 2017-10-19 15:46:20 -0700 | [diff] [blame] | 281 | } |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 282 | 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 Kicinski | c9c3599 | 2017-10-09 10:30:13 -0700 | [diff] [blame] | 304 | static 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 | |
| 313 | static void dump_xlated(void *buf, unsigned int len, bool opcodes) |
| 314 | { |
| 315 | struct bpf_insn *insn = buf; |
| 316 | unsigned int i; |
| 317 | |
| 318 | for (i = 0; i < len / sizeof(*insn); i++) { |
| 319 | printf("% 4d: ", i); |
| 320 | print_bpf_insn(print_insn, NULL, insn + i, true); |
| 321 | |
| 322 | if (opcodes) { |
| 323 | printf(" "); |
Quentin Monnet | 9cbe1f58 | 2017-10-19 15:46:19 -0700 | [diff] [blame] | 324 | fprint_hex(stdout, insn + i, 8, " "); |
Jakub Kicinski | c9c3599 | 2017-10-09 10:30:13 -0700 | [diff] [blame] | 325 | printf("\n"); |
| 326 | } |
| 327 | |
| 328 | if (insn[i].code == (BPF_LD | BPF_IMM | BPF_DW)) |
| 329 | i++; |
| 330 | } |
| 331 | } |
| 332 | |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 333 | static int do_dump(int argc, char **argv) |
| 334 | { |
| 335 | struct bpf_prog_info info = {}; |
| 336 | __u32 len = sizeof(info); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 337 | unsigned int buf_size; |
| 338 | char *filepath = NULL; |
| 339 | bool opcodes = false; |
| 340 | unsigned char *buf; |
| 341 | __u32 *member_len; |
| 342 | __u64 *member_ptr; |
| 343 | ssize_t n; |
| 344 | int err; |
| 345 | int fd; |
| 346 | |
| 347 | if (is_prefix(*argv, "jited")) { |
| 348 | member_len = &info.jited_prog_len; |
| 349 | member_ptr = &info.jited_prog_insns; |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 350 | } else if (is_prefix(*argv, "xlated")) { |
| 351 | member_len = &info.xlated_prog_len; |
| 352 | member_ptr = &info.xlated_prog_insns; |
| 353 | } else { |
| 354 | err("expected 'xlated' or 'jited', got: %s\n", *argv); |
| 355 | return -1; |
| 356 | } |
| 357 | NEXT_ARG(); |
| 358 | |
| 359 | if (argc < 2) |
| 360 | usage(); |
| 361 | |
| 362 | fd = prog_parse_fd(&argc, &argv); |
| 363 | if (fd < 0) |
| 364 | return -1; |
| 365 | |
| 366 | if (is_prefix(*argv, "file")) { |
| 367 | NEXT_ARG(); |
| 368 | if (!argc) { |
| 369 | err("expected file path\n"); |
| 370 | return -1; |
| 371 | } |
| 372 | |
| 373 | filepath = *argv; |
| 374 | NEXT_ARG(); |
| 375 | } else if (is_prefix(*argv, "opcodes")) { |
| 376 | opcodes = true; |
| 377 | NEXT_ARG(); |
| 378 | } |
| 379 | |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 380 | if (argc) { |
| 381 | usage(); |
| 382 | return -1; |
| 383 | } |
| 384 | |
| 385 | err = bpf_obj_get_info_by_fd(fd, &info, &len); |
| 386 | if (err) { |
| 387 | err("can't get prog info: %s\n", strerror(errno)); |
| 388 | return -1; |
| 389 | } |
| 390 | |
| 391 | if (!*member_len) { |
| 392 | info("no instructions returned\n"); |
| 393 | close(fd); |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | buf_size = *member_len; |
| 398 | |
| 399 | buf = malloc(buf_size); |
| 400 | if (!buf) { |
| 401 | err("mem alloc failed\n"); |
| 402 | close(fd); |
| 403 | return -1; |
| 404 | } |
| 405 | |
| 406 | memset(&info, 0, sizeof(info)); |
| 407 | |
| 408 | *member_ptr = ptr_to_u64(buf); |
| 409 | *member_len = buf_size; |
| 410 | |
| 411 | err = bpf_obj_get_info_by_fd(fd, &info, &len); |
| 412 | close(fd); |
| 413 | if (err) { |
| 414 | err("can't get prog info: %s\n", strerror(errno)); |
| 415 | goto err_free; |
| 416 | } |
| 417 | |
| 418 | if (*member_len > buf_size) { |
Quentin Monnet | 1d84487 | 2017-10-19 15:46:21 -0700 | [diff] [blame] | 419 | err("too many instructions returned\n"); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 420 | goto err_free; |
| 421 | } |
| 422 | |
| 423 | if (filepath) { |
| 424 | fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600); |
| 425 | if (fd < 0) { |
| 426 | err("can't open file %s: %s\n", filepath, |
| 427 | strerror(errno)); |
| 428 | goto err_free; |
| 429 | } |
| 430 | |
| 431 | n = write(fd, buf, *member_len); |
| 432 | close(fd); |
| 433 | if (n != *member_len) { |
| 434 | err("error writing output file: %s\n", |
| 435 | n < 0 ? strerror(errno) : "short write"); |
| 436 | goto err_free; |
| 437 | } |
| 438 | } else { |
Jakub Kicinski | c9c3599 | 2017-10-09 10:30:13 -0700 | [diff] [blame] | 439 | if (member_len == &info.jited_prog_len) |
| 440 | disasm_print_insn(buf, *member_len, opcodes); |
| 441 | else |
| 442 | dump_xlated(buf, *member_len, opcodes); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | free(buf); |
| 446 | |
| 447 | return 0; |
| 448 | |
| 449 | err_free: |
| 450 | free(buf); |
| 451 | return -1; |
| 452 | } |
| 453 | |
| 454 | static int do_pin(int argc, char **argv) |
| 455 | { |
| 456 | return do_pin_any(argc, argv, bpf_prog_get_fd_by_id); |
| 457 | } |
| 458 | |
| 459 | static int do_help(int argc, char **argv) |
| 460 | { |
| 461 | fprintf(stderr, |
| 462 | "Usage: %s %s show [PROG]\n" |
Jakub Kicinski | c9c3599 | 2017-10-09 10:30:13 -0700 | [diff] [blame] | 463 | " %s %s dump xlated PROG [file FILE] [opcodes]\n" |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 464 | " %s %s dump jited PROG [file FILE] [opcodes]\n" |
| 465 | " %s %s pin PROG FILE\n" |
| 466 | " %s %s help\n" |
| 467 | "\n" |
| 468 | " " HELP_SPEC_PROGRAM "\n" |
| 469 | "", |
| 470 | bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2], |
| 471 | bin_name, argv[-2], bin_name, argv[-2]); |
| 472 | |
| 473 | return 0; |
| 474 | } |
| 475 | |
| 476 | static const struct cmd cmds[] = { |
| 477 | { "show", do_show }, |
Quentin Monnet | 9f60617 | 2017-10-19 15:46:22 -0700 | [diff] [blame^] | 478 | { "help", do_help }, |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 479 | { "dump", do_dump }, |
| 480 | { "pin", do_pin }, |
| 481 | { 0 } |
| 482 | }; |
| 483 | |
| 484 | int do_prog(int argc, char **argv) |
| 485 | { |
| 486 | return cmd_select(cmds, argc, argv, do_help); |
| 487 | } |