xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017-2018 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 | #define _GNU_SOURCE |
| 35 | #include <errno.h> |
| 36 | #include <fcntl.h> |
| 37 | #include <stdarg.h> |
| 38 | #include <stdio.h> |
| 39 | #include <stdlib.h> |
| 40 | #include <string.h> |
| 41 | #include <time.h> |
| 42 | #include <unistd.h> |
| 43 | #include <net/if.h> |
| 44 | #include <sys/types.h> |
| 45 | #include <sys/stat.h> |
| 46 | |
| 47 | #include <linux/err.h> |
| 48 | |
| 49 | #include <bpf.h> |
| 50 | #include <libbpf.h> |
| 51 | |
| 52 | #include "cfg.h" |
| 53 | #include "main.h" |
| 54 | #include "xlated_dumper.h" |
| 55 | |
| 56 | static const char * const prog_type_name[] = { |
| 57 | [BPF_PROG_TYPE_UNSPEC] = "unspec", |
| 58 | [BPF_PROG_TYPE_SOCKET_FILTER] = "socket_filter", |
| 59 | [BPF_PROG_TYPE_KPROBE] = "kprobe", |
| 60 | [BPF_PROG_TYPE_SCHED_CLS] = "sched_cls", |
| 61 | [BPF_PROG_TYPE_SCHED_ACT] = "sched_act", |
| 62 | [BPF_PROG_TYPE_TRACEPOINT] = "tracepoint", |
| 63 | [BPF_PROG_TYPE_XDP] = "xdp", |
| 64 | [BPF_PROG_TYPE_PERF_EVENT] = "perf_event", |
| 65 | [BPF_PROG_TYPE_CGROUP_SKB] = "cgroup_skb", |
| 66 | [BPF_PROG_TYPE_CGROUP_SOCK] = "cgroup_sock", |
| 67 | [BPF_PROG_TYPE_LWT_IN] = "lwt_in", |
| 68 | [BPF_PROG_TYPE_LWT_OUT] = "lwt_out", |
| 69 | [BPF_PROG_TYPE_LWT_XMIT] = "lwt_xmit", |
| 70 | [BPF_PROG_TYPE_SOCK_OPS] = "sock_ops", |
| 71 | [BPF_PROG_TYPE_SK_SKB] = "sk_skb", |
| 72 | [BPF_PROG_TYPE_CGROUP_DEVICE] = "cgroup_device", |
| 73 | [BPF_PROG_TYPE_SK_MSG] = "sk_msg", |
| 74 | [BPF_PROG_TYPE_RAW_TRACEPOINT] = "raw_tracepoint", |
| 75 | [BPF_PROG_TYPE_CGROUP_SOCK_ADDR] = "cgroup_sock_addr", |
| 76 | [BPF_PROG_TYPE_LIRC_MODE2] = "lirc_mode2", |
| 77 | }; |
| 78 | |
| 79 | static void print_boot_time(__u64 nsecs, char *buf, unsigned int size) |
| 80 | { |
| 81 | struct timespec real_time_ts, boot_time_ts; |
| 82 | time_t wallclock_secs; |
| 83 | struct tm load_tm; |
| 84 | |
| 85 | buf[--size] = '\0'; |
| 86 | |
| 87 | if (clock_gettime(CLOCK_REALTIME, &real_time_ts) || |
| 88 | clock_gettime(CLOCK_BOOTTIME, &boot_time_ts)) { |
| 89 | perror("Can't read clocks"); |
| 90 | snprintf(buf, size, "%llu", nsecs / 1000000000); |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | wallclock_secs = (real_time_ts.tv_sec - boot_time_ts.tv_sec) + |
| 95 | (real_time_ts.tv_nsec - boot_time_ts.tv_nsec + nsecs) / |
| 96 | 1000000000; |
| 97 | |
| 98 | |
| 99 | if (!localtime_r(&wallclock_secs, &load_tm)) { |
| 100 | snprintf(buf, size, "%llu", nsecs / 1000000000); |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | if (json_output) |
| 105 | strftime(buf, size, "%s", &load_tm); |
| 106 | else |
| 107 | strftime(buf, size, "%FT%T%z", &load_tm); |
| 108 | } |
| 109 | |
| 110 | static int prog_fd_by_tag(unsigned char *tag) |
| 111 | { |
| 112 | unsigned int id = 0; |
| 113 | int err; |
| 114 | int fd; |
| 115 | |
| 116 | while (true) { |
| 117 | struct bpf_prog_info info = {}; |
| 118 | __u32 len = sizeof(info); |
| 119 | |
| 120 | err = bpf_prog_get_next_id(id, &id); |
| 121 | if (err) { |
| 122 | p_err("%s", strerror(errno)); |
| 123 | return -1; |
| 124 | } |
| 125 | |
| 126 | fd = bpf_prog_get_fd_by_id(id); |
| 127 | if (fd < 0) { |
| 128 | p_err("can't get prog by id (%u): %s", |
| 129 | id, strerror(errno)); |
| 130 | return -1; |
| 131 | } |
| 132 | |
| 133 | err = bpf_obj_get_info_by_fd(fd, &info, &len); |
| 134 | if (err) { |
| 135 | p_err("can't get prog info (%u): %s", |
| 136 | id, strerror(errno)); |
| 137 | close(fd); |
| 138 | return -1; |
| 139 | } |
| 140 | |
| 141 | if (!memcmp(tag, info.tag, BPF_TAG_SIZE)) |
| 142 | return fd; |
| 143 | |
| 144 | close(fd); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | int prog_parse_fd(int *argc, char ***argv) |
| 149 | { |
| 150 | int fd; |
| 151 | |
| 152 | if (is_prefix(**argv, "id")) { |
| 153 | unsigned int id; |
| 154 | char *endptr; |
| 155 | |
| 156 | NEXT_ARGP(); |
| 157 | |
| 158 | id = strtoul(**argv, &endptr, 0); |
| 159 | if (*endptr) { |
| 160 | p_err("can't parse %s as ID", **argv); |
| 161 | return -1; |
| 162 | } |
| 163 | NEXT_ARGP(); |
| 164 | |
| 165 | fd = bpf_prog_get_fd_by_id(id); |
| 166 | if (fd < 0) |
| 167 | p_err("get by id (%u): %s", id, strerror(errno)); |
| 168 | return fd; |
| 169 | } else if (is_prefix(**argv, "tag")) { |
| 170 | unsigned char tag[BPF_TAG_SIZE]; |
| 171 | |
| 172 | NEXT_ARGP(); |
| 173 | |
| 174 | if (sscanf(**argv, BPF_TAG_FMT, tag, tag + 1, tag + 2, |
| 175 | tag + 3, tag + 4, tag + 5, tag + 6, tag + 7) |
| 176 | != BPF_TAG_SIZE) { |
| 177 | p_err("can't parse tag"); |
| 178 | return -1; |
| 179 | } |
| 180 | NEXT_ARGP(); |
| 181 | |
| 182 | return prog_fd_by_tag(tag); |
| 183 | } else if (is_prefix(**argv, "pinned")) { |
| 184 | char *path; |
| 185 | |
| 186 | NEXT_ARGP(); |
| 187 | |
| 188 | path = **argv; |
| 189 | NEXT_ARGP(); |
| 190 | |
| 191 | return open_obj_pinned_any(path, BPF_OBJ_PROG); |
| 192 | } |
| 193 | |
| 194 | p_err("expected 'id', 'tag' or 'pinned', got: '%s'?", **argv); |
| 195 | return -1; |
| 196 | } |
| 197 | |
| 198 | static void show_prog_maps(int fd, u32 num_maps) |
| 199 | { |
| 200 | struct bpf_prog_info info = {}; |
| 201 | __u32 len = sizeof(info); |
| 202 | __u32 map_ids[num_maps]; |
| 203 | unsigned int i; |
| 204 | int err; |
| 205 | |
| 206 | info.nr_map_ids = num_maps; |
| 207 | info.map_ids = ptr_to_u64(map_ids); |
| 208 | |
| 209 | err = bpf_obj_get_info_by_fd(fd, &info, &len); |
| 210 | if (err || !info.nr_map_ids) |
| 211 | return; |
| 212 | |
| 213 | if (json_output) { |
| 214 | jsonw_name(json_wtr, "map_ids"); |
| 215 | jsonw_start_array(json_wtr); |
| 216 | for (i = 0; i < info.nr_map_ids; i++) |
| 217 | jsonw_uint(json_wtr, map_ids[i]); |
| 218 | jsonw_end_array(json_wtr); |
| 219 | } else { |
| 220 | printf(" map_ids "); |
| 221 | for (i = 0; i < info.nr_map_ids; i++) |
| 222 | printf("%u%s", map_ids[i], |
| 223 | i == info.nr_map_ids - 1 ? "" : ","); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | static void print_prog_json(struct bpf_prog_info *info, int fd) |
| 228 | { |
| 229 | char *memlock; |
| 230 | |
| 231 | jsonw_start_object(json_wtr); |
| 232 | jsonw_uint_field(json_wtr, "id", info->id); |
| 233 | if (info->type < ARRAY_SIZE(prog_type_name)) |
| 234 | jsonw_string_field(json_wtr, "type", |
| 235 | prog_type_name[info->type]); |
| 236 | else |
| 237 | jsonw_uint_field(json_wtr, "type", info->type); |
| 238 | |
| 239 | if (*info->name) |
| 240 | jsonw_string_field(json_wtr, "name", info->name); |
| 241 | |
| 242 | jsonw_name(json_wtr, "tag"); |
| 243 | jsonw_printf(json_wtr, "\"" BPF_TAG_FMT "\"", |
| 244 | info->tag[0], info->tag[1], info->tag[2], info->tag[3], |
| 245 | info->tag[4], info->tag[5], info->tag[6], info->tag[7]); |
| 246 | |
| 247 | jsonw_bool_field(json_wtr, "gpl_compatible", info->gpl_compatible); |
| 248 | |
| 249 | print_dev_json(info->ifindex, info->netns_dev, info->netns_ino); |
| 250 | |
| 251 | if (info->load_time) { |
| 252 | char buf[32]; |
| 253 | |
| 254 | print_boot_time(info->load_time, buf, sizeof(buf)); |
| 255 | |
| 256 | /* Piggy back on load_time, since 0 uid is a valid one */ |
| 257 | jsonw_name(json_wtr, "loaded_at"); |
| 258 | jsonw_printf(json_wtr, "%s", buf); |
| 259 | jsonw_uint_field(json_wtr, "uid", info->created_by_uid); |
| 260 | } |
| 261 | |
| 262 | jsonw_uint_field(json_wtr, "bytes_xlated", info->xlated_prog_len); |
| 263 | |
| 264 | if (info->jited_prog_len) { |
| 265 | jsonw_bool_field(json_wtr, "jited", true); |
| 266 | jsonw_uint_field(json_wtr, "bytes_jited", info->jited_prog_len); |
| 267 | } else { |
| 268 | jsonw_bool_field(json_wtr, "jited", false); |
| 269 | } |
| 270 | |
| 271 | memlock = get_fdinfo(fd, "memlock"); |
| 272 | if (memlock) |
| 273 | jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock)); |
| 274 | free(memlock); |
| 275 | |
| 276 | if (info->nr_map_ids) |
| 277 | show_prog_maps(fd, info->nr_map_ids); |
| 278 | |
| 279 | if (!hash_empty(prog_table.table)) { |
| 280 | struct pinned_obj *obj; |
| 281 | |
| 282 | jsonw_name(json_wtr, "pinned"); |
| 283 | jsonw_start_array(json_wtr); |
| 284 | hash_for_each_possible(prog_table.table, obj, hash, info->id) { |
| 285 | if (obj->id == info->id) |
| 286 | jsonw_string(json_wtr, obj->path); |
| 287 | } |
| 288 | jsonw_end_array(json_wtr); |
| 289 | } |
| 290 | |
| 291 | jsonw_end_object(json_wtr); |
| 292 | } |
| 293 | |
| 294 | static void print_prog_plain(struct bpf_prog_info *info, int fd) |
| 295 | { |
| 296 | char *memlock; |
| 297 | |
| 298 | printf("%u: ", info->id); |
| 299 | if (info->type < ARRAY_SIZE(prog_type_name)) |
| 300 | printf("%s ", prog_type_name[info->type]); |
| 301 | else |
| 302 | printf("type %u ", info->type); |
| 303 | |
| 304 | if (*info->name) |
| 305 | printf("name %s ", info->name); |
| 306 | |
| 307 | printf("tag "); |
| 308 | fprint_hex(stdout, info->tag, BPF_TAG_SIZE, ""); |
| 309 | print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino); |
| 310 | printf("%s", info->gpl_compatible ? " gpl" : ""); |
| 311 | printf("\n"); |
| 312 | |
| 313 | if (info->load_time) { |
| 314 | char buf[32]; |
| 315 | |
| 316 | print_boot_time(info->load_time, buf, sizeof(buf)); |
| 317 | |
| 318 | /* Piggy back on load_time, since 0 uid is a valid one */ |
| 319 | printf("\tloaded_at %s uid %u\n", buf, info->created_by_uid); |
| 320 | } |
| 321 | |
| 322 | printf("\txlated %uB", info->xlated_prog_len); |
| 323 | |
| 324 | if (info->jited_prog_len) |
| 325 | printf(" jited %uB", info->jited_prog_len); |
| 326 | else |
| 327 | printf(" not jited"); |
| 328 | |
| 329 | memlock = get_fdinfo(fd, "memlock"); |
| 330 | if (memlock) |
| 331 | printf(" memlock %sB", memlock); |
| 332 | free(memlock); |
| 333 | |
| 334 | if (info->nr_map_ids) |
| 335 | show_prog_maps(fd, info->nr_map_ids); |
| 336 | |
| 337 | if (!hash_empty(prog_table.table)) { |
| 338 | struct pinned_obj *obj; |
| 339 | |
| 340 | printf("\n"); |
| 341 | hash_for_each_possible(prog_table.table, obj, hash, info->id) { |
| 342 | if (obj->id == info->id) |
| 343 | printf("\tpinned %s\n", obj->path); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | printf("\n"); |
| 348 | } |
| 349 | |
| 350 | static int show_prog(int fd) |
| 351 | { |
| 352 | struct bpf_prog_info info = {}; |
| 353 | __u32 len = sizeof(info); |
| 354 | int err; |
| 355 | |
| 356 | err = bpf_obj_get_info_by_fd(fd, &info, &len); |
| 357 | if (err) { |
| 358 | p_err("can't get prog info: %s", strerror(errno)); |
| 359 | return -1; |
| 360 | } |
| 361 | |
| 362 | if (json_output) |
| 363 | print_prog_json(&info, fd); |
| 364 | else |
| 365 | print_prog_plain(&info, fd); |
| 366 | |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | static int do_show(int argc, char **argv) |
| 371 | { |
| 372 | __u32 id = 0; |
| 373 | int err; |
| 374 | int fd; |
| 375 | |
| 376 | if (show_pinned) |
| 377 | build_pinned_obj_table(&prog_table, BPF_OBJ_PROG); |
| 378 | |
| 379 | if (argc == 2) { |
| 380 | fd = prog_parse_fd(&argc, &argv); |
| 381 | if (fd < 0) |
| 382 | return -1; |
| 383 | |
| 384 | err = show_prog(fd); |
| 385 | close(fd); |
| 386 | return err; |
| 387 | } |
| 388 | |
| 389 | if (argc) |
| 390 | return BAD_ARG(); |
| 391 | |
| 392 | if (json_output) |
| 393 | jsonw_start_array(json_wtr); |
| 394 | while (true) { |
| 395 | err = bpf_prog_get_next_id(id, &id); |
| 396 | if (err) { |
| 397 | if (errno == ENOENT) { |
| 398 | err = 0; |
| 399 | break; |
| 400 | } |
| 401 | p_err("can't get next program: %s%s", strerror(errno), |
| 402 | errno == EINVAL ? " -- kernel too old?" : ""); |
| 403 | err = -1; |
| 404 | break; |
| 405 | } |
| 406 | |
| 407 | fd = bpf_prog_get_fd_by_id(id); |
| 408 | if (fd < 0) { |
| 409 | if (errno == ENOENT) |
| 410 | continue; |
| 411 | p_err("can't get prog by id (%u): %s", |
| 412 | id, strerror(errno)); |
| 413 | err = -1; |
| 414 | break; |
| 415 | } |
| 416 | |
| 417 | err = show_prog(fd); |
| 418 | close(fd); |
| 419 | if (err) |
| 420 | break; |
| 421 | } |
| 422 | |
| 423 | if (json_output) |
| 424 | jsonw_end_array(json_wtr); |
| 425 | |
| 426 | return err; |
| 427 | } |
| 428 | |
| 429 | static int do_dump(int argc, char **argv) |
| 430 | { |
| 431 | unsigned long *func_ksyms = NULL; |
| 432 | struct bpf_prog_info info = {}; |
| 433 | unsigned int *func_lens = NULL; |
| 434 | unsigned int nr_func_ksyms; |
| 435 | unsigned int nr_func_lens; |
| 436 | struct dump_data dd = {}; |
| 437 | __u32 len = sizeof(info); |
| 438 | unsigned int buf_size; |
| 439 | char *filepath = NULL; |
| 440 | bool opcodes = false; |
| 441 | bool visual = false; |
| 442 | unsigned char *buf; |
| 443 | __u32 *member_len; |
| 444 | __u64 *member_ptr; |
| 445 | ssize_t n; |
| 446 | int err; |
| 447 | int fd; |
| 448 | |
| 449 | if (is_prefix(*argv, "jited")) { |
| 450 | member_len = &info.jited_prog_len; |
| 451 | member_ptr = &info.jited_prog_insns; |
| 452 | } else if (is_prefix(*argv, "xlated")) { |
| 453 | member_len = &info.xlated_prog_len; |
| 454 | member_ptr = &info.xlated_prog_insns; |
| 455 | } else { |
| 456 | p_err("expected 'xlated' or 'jited', got: %s", *argv); |
| 457 | return -1; |
| 458 | } |
| 459 | NEXT_ARG(); |
| 460 | |
| 461 | if (argc < 2) |
| 462 | usage(); |
| 463 | |
| 464 | fd = prog_parse_fd(&argc, &argv); |
| 465 | if (fd < 0) |
| 466 | return -1; |
| 467 | |
| 468 | if (is_prefix(*argv, "file")) { |
| 469 | NEXT_ARG(); |
| 470 | if (!argc) { |
| 471 | p_err("expected file path"); |
| 472 | return -1; |
| 473 | } |
| 474 | |
| 475 | filepath = *argv; |
| 476 | NEXT_ARG(); |
| 477 | } else if (is_prefix(*argv, "opcodes")) { |
| 478 | opcodes = true; |
| 479 | NEXT_ARG(); |
| 480 | } else if (is_prefix(*argv, "visual")) { |
| 481 | visual = true; |
| 482 | NEXT_ARG(); |
| 483 | } |
| 484 | |
| 485 | if (argc) { |
| 486 | usage(); |
| 487 | return -1; |
| 488 | } |
| 489 | |
| 490 | err = bpf_obj_get_info_by_fd(fd, &info, &len); |
| 491 | if (err) { |
| 492 | p_err("can't get prog info: %s", strerror(errno)); |
| 493 | return -1; |
| 494 | } |
| 495 | |
| 496 | if (!*member_len) { |
| 497 | p_info("no instructions returned"); |
| 498 | close(fd); |
| 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | buf_size = *member_len; |
| 503 | |
| 504 | buf = malloc(buf_size); |
| 505 | if (!buf) { |
| 506 | p_err("mem alloc failed"); |
| 507 | close(fd); |
| 508 | return -1; |
| 509 | } |
| 510 | |
| 511 | nr_func_ksyms = info.nr_jited_ksyms; |
| 512 | if (nr_func_ksyms) { |
| 513 | func_ksyms = malloc(nr_func_ksyms * sizeof(__u64)); |
| 514 | if (!func_ksyms) { |
| 515 | p_err("mem alloc failed"); |
| 516 | close(fd); |
| 517 | goto err_free; |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | nr_func_lens = info.nr_jited_func_lens; |
| 522 | if (nr_func_lens) { |
| 523 | func_lens = malloc(nr_func_lens * sizeof(__u32)); |
| 524 | if (!func_lens) { |
| 525 | p_err("mem alloc failed"); |
| 526 | close(fd); |
| 527 | goto err_free; |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | memset(&info, 0, sizeof(info)); |
| 532 | |
| 533 | *member_ptr = ptr_to_u64(buf); |
| 534 | *member_len = buf_size; |
| 535 | info.jited_ksyms = ptr_to_u64(func_ksyms); |
| 536 | info.nr_jited_ksyms = nr_func_ksyms; |
| 537 | info.jited_func_lens = ptr_to_u64(func_lens); |
| 538 | info.nr_jited_func_lens = nr_func_lens; |
| 539 | |
| 540 | err = bpf_obj_get_info_by_fd(fd, &info, &len); |
| 541 | close(fd); |
| 542 | if (err) { |
| 543 | p_err("can't get prog info: %s", strerror(errno)); |
| 544 | goto err_free; |
| 545 | } |
| 546 | |
| 547 | if (*member_len > buf_size) { |
| 548 | p_err("too many instructions returned"); |
| 549 | goto err_free; |
| 550 | } |
| 551 | |
| 552 | if (info.nr_jited_ksyms > nr_func_ksyms) { |
| 553 | p_err("too many addresses returned"); |
| 554 | goto err_free; |
| 555 | } |
| 556 | |
| 557 | if (info.nr_jited_func_lens > nr_func_lens) { |
| 558 | p_err("too many values returned"); |
| 559 | goto err_free; |
| 560 | } |
| 561 | |
| 562 | if ((member_len == &info.jited_prog_len && |
| 563 | info.jited_prog_insns == 0) || |
| 564 | (member_len == &info.xlated_prog_len && |
| 565 | info.xlated_prog_insns == 0)) { |
| 566 | p_err("error retrieving insn dump: kernel.kptr_restrict set?"); |
| 567 | goto err_free; |
| 568 | } |
| 569 | |
| 570 | if (filepath) { |
| 571 | fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600); |
| 572 | if (fd < 0) { |
| 573 | p_err("can't open file %s: %s", filepath, |
| 574 | strerror(errno)); |
| 575 | goto err_free; |
| 576 | } |
| 577 | |
| 578 | n = write(fd, buf, *member_len); |
| 579 | close(fd); |
| 580 | if (n != *member_len) { |
| 581 | p_err("error writing output file: %s", |
| 582 | n < 0 ? strerror(errno) : "short write"); |
| 583 | goto err_free; |
| 584 | } |
| 585 | |
| 586 | if (json_output) |
| 587 | jsonw_null(json_wtr); |
| 588 | } else if (member_len == &info.jited_prog_len) { |
| 589 | const char *name = NULL; |
| 590 | |
| 591 | if (info.ifindex) { |
| 592 | name = ifindex_to_bfd_name_ns(info.ifindex, |
| 593 | info.netns_dev, |
| 594 | info.netns_ino); |
| 595 | if (!name) |
| 596 | goto err_free; |
| 597 | } |
| 598 | |
| 599 | if (info.nr_jited_func_lens && info.jited_func_lens) { |
| 600 | struct kernel_sym *sym = NULL; |
| 601 | char sym_name[SYM_MAX_NAME]; |
| 602 | unsigned char *img = buf; |
| 603 | __u64 *ksyms = NULL; |
| 604 | __u32 *lens; |
| 605 | __u32 i; |
| 606 | |
| 607 | if (info.nr_jited_ksyms) { |
| 608 | kernel_syms_load(&dd); |
| 609 | ksyms = (__u64 *) info.jited_ksyms; |
| 610 | } |
| 611 | |
| 612 | if (json_output) |
| 613 | jsonw_start_array(json_wtr); |
| 614 | |
| 615 | lens = (__u32 *) info.jited_func_lens; |
| 616 | for (i = 0; i < info.nr_jited_func_lens; i++) { |
| 617 | if (ksyms) { |
| 618 | sym = kernel_syms_search(&dd, ksyms[i]); |
| 619 | if (sym) |
| 620 | sprintf(sym_name, "%s", sym->name); |
| 621 | else |
| 622 | sprintf(sym_name, "0x%016llx", ksyms[i]); |
| 623 | } else { |
| 624 | strcpy(sym_name, "unknown"); |
| 625 | } |
| 626 | |
| 627 | if (json_output) { |
| 628 | jsonw_start_object(json_wtr); |
| 629 | jsonw_name(json_wtr, "name"); |
| 630 | jsonw_string(json_wtr, sym_name); |
| 631 | jsonw_name(json_wtr, "insns"); |
| 632 | } else { |
| 633 | printf("%s:\n", sym_name); |
| 634 | } |
| 635 | |
| 636 | disasm_print_insn(img, lens[i], opcodes, name); |
| 637 | img += lens[i]; |
| 638 | |
| 639 | if (json_output) |
| 640 | jsonw_end_object(json_wtr); |
| 641 | else |
| 642 | printf("\n"); |
| 643 | } |
| 644 | |
| 645 | if (json_output) |
| 646 | jsonw_end_array(json_wtr); |
| 647 | } else { |
| 648 | disasm_print_insn(buf, *member_len, opcodes, name); |
| 649 | } |
| 650 | } else if (visual) { |
| 651 | if (json_output) |
| 652 | jsonw_null(json_wtr); |
| 653 | else |
| 654 | dump_xlated_cfg(buf, *member_len); |
| 655 | } else { |
| 656 | kernel_syms_load(&dd); |
| 657 | dd.nr_jited_ksyms = info.nr_jited_ksyms; |
| 658 | dd.jited_ksyms = (__u64 *) info.jited_ksyms; |
| 659 | |
| 660 | if (json_output) |
| 661 | dump_xlated_json(&dd, buf, *member_len, opcodes); |
| 662 | else |
| 663 | dump_xlated_plain(&dd, buf, *member_len, opcodes); |
| 664 | kernel_syms_destroy(&dd); |
| 665 | } |
| 666 | |
| 667 | free(buf); |
| 668 | free(func_ksyms); |
| 669 | free(func_lens); |
| 670 | return 0; |
| 671 | |
| 672 | err_free: |
| 673 | free(buf); |
| 674 | free(func_ksyms); |
| 675 | free(func_lens); |
| 676 | return -1; |
| 677 | } |
| 678 | |
| 679 | static int do_pin(int argc, char **argv) |
| 680 | { |
| 681 | int err; |
| 682 | |
| 683 | err = do_pin_any(argc, argv, bpf_prog_get_fd_by_id); |
| 684 | if (!err && json_output) |
| 685 | jsonw_null(json_wtr); |
| 686 | return err; |
| 687 | } |
| 688 | |
| 689 | struct map_replace { |
| 690 | int idx; |
| 691 | int fd; |
| 692 | char *name; |
| 693 | }; |
| 694 | |
| 695 | int map_replace_compar(const void *p1, const void *p2) |
| 696 | { |
| 697 | const struct map_replace *a = p1, *b = p2; |
| 698 | |
| 699 | return a->idx - b->idx; |
| 700 | } |
| 701 | |
| 702 | static int do_load(int argc, char **argv) |
| 703 | { |
| 704 | enum bpf_attach_type expected_attach_type; |
| 705 | struct bpf_object_open_attr attr = { |
| 706 | .prog_type = BPF_PROG_TYPE_UNSPEC, |
| 707 | }; |
| 708 | struct map_replace *map_replace = NULL; |
| 709 | unsigned int old_map_fds = 0; |
| 710 | struct bpf_program *prog; |
| 711 | struct bpf_object *obj; |
| 712 | struct bpf_map *map; |
| 713 | const char *pinfile; |
| 714 | unsigned int i, j; |
| 715 | __u32 ifindex = 0; |
| 716 | int idx, err; |
| 717 | |
| 718 | if (!REQ_ARGS(2)) |
| 719 | return -1; |
| 720 | attr.file = GET_ARG(); |
| 721 | pinfile = GET_ARG(); |
| 722 | |
| 723 | while (argc) { |
| 724 | if (is_prefix(*argv, "type")) { |
| 725 | char *type; |
| 726 | |
| 727 | NEXT_ARG(); |
| 728 | |
| 729 | if (attr.prog_type != BPF_PROG_TYPE_UNSPEC) { |
| 730 | p_err("program type already specified"); |
| 731 | goto err_free_reuse_maps; |
| 732 | } |
| 733 | if (!REQ_ARGS(1)) |
| 734 | goto err_free_reuse_maps; |
| 735 | |
| 736 | /* Put a '/' at the end of type to appease libbpf */ |
| 737 | type = malloc(strlen(*argv) + 2); |
| 738 | if (!type) { |
| 739 | p_err("mem alloc failed"); |
| 740 | goto err_free_reuse_maps; |
| 741 | } |
| 742 | *type = 0; |
| 743 | strcat(type, *argv); |
| 744 | strcat(type, "/"); |
| 745 | |
| 746 | err = libbpf_prog_type_by_name(type, &attr.prog_type, |
| 747 | &expected_attach_type); |
| 748 | free(type); |
| 749 | if (err < 0) { |
| 750 | p_err("unknown program type '%s'", *argv); |
| 751 | goto err_free_reuse_maps; |
| 752 | } |
| 753 | NEXT_ARG(); |
| 754 | } else if (is_prefix(*argv, "map")) { |
| 755 | void *new_map_replace; |
| 756 | char *endptr, *name; |
| 757 | int fd; |
| 758 | |
| 759 | NEXT_ARG(); |
| 760 | |
| 761 | if (!REQ_ARGS(4)) |
| 762 | goto err_free_reuse_maps; |
| 763 | |
| 764 | if (is_prefix(*argv, "idx")) { |
| 765 | NEXT_ARG(); |
| 766 | |
| 767 | idx = strtoul(*argv, &endptr, 0); |
| 768 | if (*endptr) { |
| 769 | p_err("can't parse %s as IDX", *argv); |
| 770 | goto err_free_reuse_maps; |
| 771 | } |
| 772 | name = NULL; |
| 773 | } else if (is_prefix(*argv, "name")) { |
| 774 | NEXT_ARG(); |
| 775 | |
| 776 | name = *argv; |
| 777 | idx = -1; |
| 778 | } else { |
| 779 | p_err("expected 'idx' or 'name', got: '%s'?", |
| 780 | *argv); |
| 781 | goto err_free_reuse_maps; |
| 782 | } |
| 783 | NEXT_ARG(); |
| 784 | |
| 785 | fd = map_parse_fd(&argc, &argv); |
| 786 | if (fd < 0) |
| 787 | goto err_free_reuse_maps; |
| 788 | |
| 789 | new_map_replace = reallocarray(map_replace, |
| 790 | old_map_fds + 1, |
| 791 | sizeof(*map_replace)); |
| 792 | if (!new_map_replace) { |
| 793 | p_err("mem alloc failed"); |
| 794 | goto err_free_reuse_maps; |
| 795 | } |
| 796 | map_replace = new_map_replace; |
| 797 | |
| 798 | map_replace[old_map_fds].idx = idx; |
| 799 | map_replace[old_map_fds].name = name; |
| 800 | map_replace[old_map_fds].fd = fd; |
| 801 | old_map_fds++; |
| 802 | } else if (is_prefix(*argv, "dev")) { |
| 803 | NEXT_ARG(); |
| 804 | |
| 805 | if (ifindex) { |
| 806 | p_err("offload device already specified"); |
| 807 | goto err_free_reuse_maps; |
| 808 | } |
| 809 | if (!REQ_ARGS(1)) |
| 810 | goto err_free_reuse_maps; |
| 811 | |
| 812 | ifindex = if_nametoindex(*argv); |
| 813 | if (!ifindex) { |
| 814 | p_err("unrecognized netdevice '%s': %s", |
| 815 | *argv, strerror(errno)); |
| 816 | goto err_free_reuse_maps; |
| 817 | } |
| 818 | NEXT_ARG(); |
| 819 | } else { |
| 820 | p_err("expected no more arguments, 'type', 'map' or 'dev', got: '%s'?", |
| 821 | *argv); |
| 822 | goto err_free_reuse_maps; |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | obj = bpf_object__open_xattr(&attr); |
| 827 | if (IS_ERR_OR_NULL(obj)) { |
| 828 | p_err("failed to open object file"); |
| 829 | goto err_free_reuse_maps; |
| 830 | } |
| 831 | |
| 832 | prog = bpf_program__next(NULL, obj); |
| 833 | if (!prog) { |
| 834 | p_err("object file doesn't contain any bpf program"); |
| 835 | goto err_close_obj; |
| 836 | } |
| 837 | |
| 838 | bpf_program__set_ifindex(prog, ifindex); |
| 839 | if (attr.prog_type == BPF_PROG_TYPE_UNSPEC) { |
| 840 | const char *sec_name = bpf_program__title(prog, false); |
| 841 | |
| 842 | err = libbpf_prog_type_by_name(sec_name, &attr.prog_type, |
| 843 | &expected_attach_type); |
| 844 | if (err < 0) { |
| 845 | p_err("failed to guess program type based on section name %s\n", |
| 846 | sec_name); |
| 847 | goto err_close_obj; |
| 848 | } |
| 849 | } |
| 850 | bpf_program__set_type(prog, attr.prog_type); |
| 851 | bpf_program__set_expected_attach_type(prog, expected_attach_type); |
| 852 | |
| 853 | qsort(map_replace, old_map_fds, sizeof(*map_replace), |
| 854 | map_replace_compar); |
| 855 | |
| 856 | /* After the sort maps by name will be first on the list, because they |
| 857 | * have idx == -1. Resolve them. |
| 858 | */ |
| 859 | j = 0; |
| 860 | while (j < old_map_fds && map_replace[j].name) { |
| 861 | i = 0; |
| 862 | bpf_map__for_each(map, obj) { |
| 863 | if (!strcmp(bpf_map__name(map), map_replace[j].name)) { |
| 864 | map_replace[j].idx = i; |
| 865 | break; |
| 866 | } |
| 867 | i++; |
| 868 | } |
| 869 | if (map_replace[j].idx == -1) { |
| 870 | p_err("unable to find map '%s'", map_replace[j].name); |
| 871 | goto err_close_obj; |
| 872 | } |
| 873 | j++; |
| 874 | } |
| 875 | /* Resort if any names were resolved */ |
| 876 | if (j) |
| 877 | qsort(map_replace, old_map_fds, sizeof(*map_replace), |
| 878 | map_replace_compar); |
| 879 | |
| 880 | /* Set ifindex and name reuse */ |
| 881 | j = 0; |
| 882 | idx = 0; |
| 883 | bpf_map__for_each(map, obj) { |
| 884 | if (!bpf_map__is_offload_neutral(map)) |
| 885 | bpf_map__set_ifindex(map, ifindex); |
| 886 | |
| 887 | if (j < old_map_fds && idx == map_replace[j].idx) { |
| 888 | err = bpf_map__reuse_fd(map, map_replace[j++].fd); |
| 889 | if (err) { |
| 890 | p_err("unable to set up map reuse: %d", err); |
| 891 | goto err_close_obj; |
| 892 | } |
| 893 | |
| 894 | /* Next reuse wants to apply to the same map */ |
| 895 | if (j < old_map_fds && map_replace[j].idx == idx) { |
| 896 | p_err("replacement for map idx %d specified more than once", |
| 897 | idx); |
| 898 | goto err_close_obj; |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | idx++; |
| 903 | } |
| 904 | if (j < old_map_fds) { |
| 905 | p_err("map idx '%d' not used", map_replace[j].idx); |
| 906 | goto err_close_obj; |
| 907 | } |
| 908 | |
| 909 | err = bpf_object__load(obj); |
| 910 | if (err) { |
| 911 | p_err("failed to load object file"); |
| 912 | goto err_close_obj; |
| 913 | } |
| 914 | |
| 915 | if (do_pin_fd(bpf_program__fd(prog), pinfile)) |
| 916 | goto err_close_obj; |
| 917 | |
| 918 | if (json_output) |
| 919 | jsonw_null(json_wtr); |
| 920 | |
| 921 | bpf_object__close(obj); |
| 922 | for (i = 0; i < old_map_fds; i++) |
| 923 | close(map_replace[i].fd); |
| 924 | free(map_replace); |
| 925 | |
| 926 | return 0; |
| 927 | |
| 928 | err_close_obj: |
| 929 | bpf_object__close(obj); |
| 930 | err_free_reuse_maps: |
| 931 | for (i = 0; i < old_map_fds; i++) |
| 932 | close(map_replace[i].fd); |
| 933 | free(map_replace); |
| 934 | return -1; |
| 935 | } |
| 936 | |
| 937 | static int do_help(int argc, char **argv) |
| 938 | { |
| 939 | if (json_output) { |
| 940 | jsonw_null(json_wtr); |
| 941 | return 0; |
| 942 | } |
| 943 | |
| 944 | fprintf(stderr, |
| 945 | "Usage: %s %s { show | list } [PROG]\n" |
| 946 | " %s %s dump xlated PROG [{ file FILE | opcodes | visual }]\n" |
| 947 | " %s %s dump jited PROG [{ file FILE | opcodes }]\n" |
| 948 | " %s %s pin PROG FILE\n" |
| 949 | " %s %s load OBJ FILE [type TYPE] [dev NAME] \\\n" |
| 950 | " [map { idx IDX | name NAME } MAP]\n" |
| 951 | " %s %s help\n" |
| 952 | "\n" |
| 953 | " " HELP_SPEC_MAP "\n" |
| 954 | " " HELP_SPEC_PROGRAM "\n" |
| 955 | " TYPE := { socket | kprobe | kretprobe | classifier | action |\n" |
| 956 | " tracepoint | raw_tracepoint | xdp | perf_event | cgroup/skb |\n" |
| 957 | " cgroup/sock | cgroup/dev | lwt_in | lwt_out | lwt_xmit |\n" |
| 958 | " lwt_seg6local | sockops | sk_skb | sk_msg | lirc_mode2 |\n" |
| 959 | " cgroup/bind4 | cgroup/bind6 | cgroup/post_bind4 |\n" |
| 960 | " cgroup/post_bind6 | cgroup/connect4 | cgroup/connect6 |\n" |
| 961 | " cgroup/sendmsg4 | cgroup/sendmsg6 }\n" |
| 962 | " " HELP_SPEC_OPTIONS "\n" |
| 963 | "", |
| 964 | bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2], |
| 965 | bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2]); |
| 966 | |
| 967 | return 0; |
| 968 | } |
| 969 | |
| 970 | static const struct cmd cmds[] = { |
| 971 | { "show", do_show }, |
| 972 | { "list", do_show }, |
| 973 | { "help", do_help }, |
| 974 | { "dump", do_dump }, |
| 975 | { "pin", do_pin }, |
| 976 | { "load", do_load }, |
| 977 | { 0 } |
| 978 | }; |
| 979 | |
| 980 | int do_prog(int argc, char **argv) |
| 981 | { |
| 982 | return cmd_select(cmds, argc, argv, do_help); |
| 983 | } |