1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) 2 /* Copyright (C) 2020 Facebook */ 3 4 #include <errno.h> 5 #include <stdio.h> 6 #include <unistd.h> 7 8 #include <linux/err.h> 9 10 #include <bpf/bpf.h> 11 #include <bpf/btf.h> 12 #include <bpf/libbpf.h> 13 14 #include "json_writer.h" 15 #include "main.h" 16 17 #define STRUCT_OPS_VALUE_PREFIX "bpf_struct_ops_" 18 19 static const struct btf_type *map_info_type; 20 static __u32 map_info_alloc_len; 21 static struct btf *btf_vmlinux; 22 static __s32 map_info_type_id; 23 24 struct res { 25 unsigned int nr_maps; 26 unsigned int nr_errs; 27 }; 28 29 static const struct btf *get_btf_vmlinux(void) 30 { 31 if (btf_vmlinux) 32 return btf_vmlinux; 33 34 btf_vmlinux = libbpf_find_kernel_btf(); 35 if (!btf_vmlinux) 36 p_err("struct_ops requires kernel CONFIG_DEBUG_INFO_BTF=y"); 37 38 return btf_vmlinux; 39 } 40 41 static const char *get_kern_struct_ops_name(const struct bpf_map_info *info) 42 { 43 const struct btf *kern_btf; 44 const struct btf_type *t; 45 const char *st_ops_name; 46 47 kern_btf = get_btf_vmlinux(); 48 if (!kern_btf) 49 return "<btf_vmlinux_not_found>"; 50 51 t = btf__type_by_id(kern_btf, info->btf_vmlinux_value_type_id); 52 st_ops_name = btf__name_by_offset(kern_btf, t->name_off); 53 st_ops_name += strlen(STRUCT_OPS_VALUE_PREFIX); 54 55 return st_ops_name; 56 } 57 58 static __s32 get_map_info_type_id(void) 59 { 60 const struct btf *kern_btf; 61 62 if (map_info_type_id) 63 return map_info_type_id; 64 65 kern_btf = get_btf_vmlinux(); 66 if (!kern_btf) 67 return 0; 68 69 map_info_type_id = btf__find_by_name_kind(kern_btf, "bpf_map_info", 70 BTF_KIND_STRUCT); 71 if (map_info_type_id < 0) { 72 p_err("can't find bpf_map_info from btf_vmlinux"); 73 return map_info_type_id; 74 } 75 map_info_type = btf__type_by_id(kern_btf, map_info_type_id); 76 77 /* Ensure map_info_alloc() has at least what the bpftool needs */ 78 map_info_alloc_len = map_info_type->size; 79 if (map_info_alloc_len < sizeof(struct bpf_map_info)) 80 map_info_alloc_len = sizeof(struct bpf_map_info); 81 82 return map_info_type_id; 83 } 84 85 /* If the subcmd needs to print out the bpf_map_info, 86 * it should always call map_info_alloc to allocate 87 * a bpf_map_info object instead of allocating it 88 * on the stack. 89 * 90 * map_info_alloc() will take the running kernel's btf 91 * into account. i.e. it will consider the 92 * sizeof(struct bpf_map_info) of the running kernel. 93 * 94 * It will enable the "struct_ops" cmd to print the latest 95 * "struct bpf_map_info". 96 * 97 * [ Recall that "struct_ops" requires the kernel's btf to 98 * be available ] 99 */ 100 static struct bpf_map_info *map_info_alloc(__u32 *alloc_len) 101 { 102 struct bpf_map_info *info; 103 104 if (get_map_info_type_id() < 0) 105 return NULL; 106 107 info = calloc(1, map_info_alloc_len); 108 if (!info) 109 p_err("mem alloc failed"); 110 else 111 *alloc_len = map_info_alloc_len; 112 113 return info; 114 } 115 116 /* It iterates all struct_ops maps of the system. 117 * It returns the fd in "*res_fd" and map_info in "*info". 118 * In the very first iteration, info->id should be 0. 119 * An optional map "*name" filter can be specified. 120 * The filter can be made more flexible in the future. 121 * e.g. filter by kernel-struct-ops-name, regex-name, glob-name, ...etc. 122 * 123 * Return value: 124 * 1: A struct_ops map found. It is returned in "*res_fd" and "*info". 125 * The caller can continue to call get_next in the future. 126 * 0: No struct_ops map is returned. 127 * All struct_ops map has been found. 128 * -1: Error and the caller should abort the iteration. 129 */ 130 static int get_next_struct_ops_map(const char *name, int *res_fd, 131 struct bpf_map_info *info, __u32 info_len) 132 { 133 __u32 id = info->id; 134 int err, fd; 135 136 while (true) { 137 err = bpf_map_get_next_id(id, &id); 138 if (err) { 139 if (errno == ENOENT) 140 return 0; 141 p_err("can't get next map: %s", strerror(errno)); 142 return -1; 143 } 144 145 fd = bpf_map_get_fd_by_id(id); 146 if (fd < 0) { 147 if (errno == ENOENT) 148 continue; 149 p_err("can't get map by id (%u): %s", 150 id, strerror(errno)); 151 return -1; 152 } 153 154 err = bpf_obj_get_info_by_fd(fd, info, &info_len); 155 if (err) { 156 p_err("can't get map info: %s", strerror(errno)); 157 close(fd); 158 return -1; 159 } 160 161 if (info->type == BPF_MAP_TYPE_STRUCT_OPS && 162 (!name || !strcmp(name, info->name))) { 163 *res_fd = fd; 164 return 1; 165 } 166 close(fd); 167 } 168 } 169 170 static int cmd_retval(const struct res *res, bool must_have_one_map) 171 { 172 if (res->nr_errs || (!res->nr_maps && must_have_one_map)) 173 return -1; 174 175 return 0; 176 } 177 178 /* "data" is the work_func private storage */ 179 typedef int (*work_func)(int fd, const struct bpf_map_info *info, void *data, 180 struct json_writer *wtr); 181 182 /* Find all struct_ops map in the system. 183 * Filter out by "name" (if specified). 184 * Then call "func(fd, info, data, wtr)" on each struct_ops map found. 185 */ 186 static struct res do_search(const char *name, work_func func, void *data, 187 struct json_writer *wtr) 188 { 189 struct bpf_map_info *info; 190 struct res res = {}; 191 __u32 info_len; 192 int fd, err; 193 194 info = map_info_alloc(&info_len); 195 if (!info) { 196 res.nr_errs++; 197 return res; 198 } 199 200 if (wtr) 201 jsonw_start_array(wtr); 202 while ((err = get_next_struct_ops_map(name, &fd, info, info_len)) == 1) { 203 res.nr_maps++; 204 err = func(fd, info, data, wtr); 205 if (err) 206 res.nr_errs++; 207 close(fd); 208 } 209 if (wtr) 210 jsonw_end_array(wtr); 211 212 if (err) 213 res.nr_errs++; 214 215 if (!wtr && name && !res.nr_errs && !res.nr_maps) 216 /* It is not printing empty []. 217 * Thus, needs to specifically say nothing found 218 * for "name" here. 219 */ 220 p_err("no struct_ops found for %s", name); 221 else if (!wtr && json_output && !res.nr_errs) 222 /* The "func()" above is not writing any json (i.e. !wtr 223 * test here). 224 * 225 * However, "-j" is enabled and there is no errs here, 226 * so call json_null() as the current convention of 227 * other cmds. 228 */ 229 jsonw_null(json_wtr); 230 231 free(info); 232 return res; 233 } 234 235 static struct res do_one_id(const char *id_str, work_func func, void *data, 236 struct json_writer *wtr) 237 { 238 struct bpf_map_info *info; 239 struct res res = {}; 240 unsigned long id; 241 __u32 info_len; 242 char *endptr; 243 int fd; 244 245 id = strtoul(id_str, &endptr, 0); 246 if (*endptr || !id || id > UINT32_MAX) { 247 p_err("invalid id %s", id_str); 248 res.nr_errs++; 249 return res; 250 } 251 252 fd = bpf_map_get_fd_by_id(id); 253 if (fd < 0) { 254 p_err("can't get map by id (%lu): %s", id, strerror(errno)); 255 res.nr_errs++; 256 return res; 257 } 258 259 info = map_info_alloc(&info_len); 260 if (!info) { 261 res.nr_errs++; 262 goto done; 263 } 264 265 if (bpf_obj_get_info_by_fd(fd, info, &info_len)) { 266 p_err("can't get map info: %s", strerror(errno)); 267 res.nr_errs++; 268 goto done; 269 } 270 271 if (info->type != BPF_MAP_TYPE_STRUCT_OPS) { 272 p_err("%s id %u is not a struct_ops map", info->name, info->id); 273 res.nr_errs++; 274 goto done; 275 } 276 277 res.nr_maps++; 278 279 if (func(fd, info, data, wtr)) 280 res.nr_errs++; 281 else if (!wtr && json_output) 282 /* The "func()" above is not writing any json (i.e. !wtr 283 * test here). 284 * 285 * However, "-j" is enabled and there is no errs here, 286 * so call json_null() as the current convention of 287 * other cmds. 288 */ 289 jsonw_null(json_wtr); 290 291 done: 292 free(info); 293 close(fd); 294 295 return res; 296 } 297 298 static struct res do_work_on_struct_ops(const char *search_type, 299 const char *search_term, 300 work_func func, void *data, 301 struct json_writer *wtr) 302 { 303 if (search_type) { 304 if (is_prefix(search_type, "id")) 305 return do_one_id(search_term, func, data, wtr); 306 else if (!is_prefix(search_type, "name")) 307 usage(); 308 } 309 310 return do_search(search_term, func, data, wtr); 311 } 312 313 static int __do_show(int fd, const struct bpf_map_info *info, void *data, 314 struct json_writer *wtr) 315 { 316 if (wtr) { 317 jsonw_start_object(wtr); 318 jsonw_uint_field(wtr, "id", info->id); 319 jsonw_string_field(wtr, "name", info->name); 320 jsonw_string_field(wtr, "kernel_struct_ops", 321 get_kern_struct_ops_name(info)); 322 jsonw_end_object(wtr); 323 } else { 324 printf("%u: %-15s %-32s\n", info->id, info->name, 325 get_kern_struct_ops_name(info)); 326 } 327 328 return 0; 329 } 330 331 static int do_show(int argc, char **argv) 332 { 333 const char *search_type = NULL, *search_term = NULL; 334 struct res res; 335 336 if (argc && argc != 2) 337 usage(); 338 339 if (argc == 2) { 340 search_type = GET_ARG(); 341 search_term = GET_ARG(); 342 } 343 344 res = do_work_on_struct_ops(search_type, search_term, __do_show, 345 NULL, json_wtr); 346 347 return cmd_retval(&res, !!search_term); 348 } 349 350 static int __do_dump(int fd, const struct bpf_map_info *info, void *data, 351 struct json_writer *wtr) 352 { 353 struct btf_dumper *d = (struct btf_dumper *)data; 354 const struct btf_type *struct_ops_type; 355 const struct btf *kern_btf = d->btf; 356 const char *struct_ops_name; 357 int zero = 0; 358 void *value; 359 360 /* note: d->jw == wtr */ 361 362 kern_btf = d->btf; 363 364 /* The kernel supporting BPF_MAP_TYPE_STRUCT_OPS must have 365 * btf_vmlinux_value_type_id. 366 */ 367 struct_ops_type = btf__type_by_id(kern_btf, 368 info->btf_vmlinux_value_type_id); 369 struct_ops_name = btf__name_by_offset(kern_btf, 370 struct_ops_type->name_off); 371 value = calloc(1, info->value_size); 372 if (!value) { 373 p_err("mem alloc failed"); 374 return -1; 375 } 376 377 if (bpf_map_lookup_elem(fd, &zero, value)) { 378 p_err("can't lookup struct_ops map %s id %u", 379 info->name, info->id); 380 free(value); 381 return -1; 382 } 383 384 jsonw_start_object(wtr); 385 jsonw_name(wtr, "bpf_map_info"); 386 btf_dumper_type(d, map_info_type_id, (void *)info); 387 jsonw_end_object(wtr); 388 389 jsonw_start_object(wtr); 390 jsonw_name(wtr, struct_ops_name); 391 btf_dumper_type(d, info->btf_vmlinux_value_type_id, value); 392 jsonw_end_object(wtr); 393 394 free(value); 395 396 return 0; 397 } 398 399 static int do_dump(int argc, char **argv) 400 { 401 const char *search_type = NULL, *search_term = NULL; 402 json_writer_t *wtr = json_wtr; 403 const struct btf *kern_btf; 404 struct btf_dumper d = {}; 405 struct res res; 406 407 if (argc && argc != 2) 408 usage(); 409 410 if (argc == 2) { 411 search_type = GET_ARG(); 412 search_term = GET_ARG(); 413 } 414 415 kern_btf = get_btf_vmlinux(); 416 if (!kern_btf) 417 return -1; 418 419 if (!json_output) { 420 wtr = jsonw_new(stdout); 421 if (!wtr) { 422 p_err("can't create json writer"); 423 return -1; 424 } 425 jsonw_pretty(wtr, true); 426 } 427 428 d.btf = kern_btf; 429 d.jw = wtr; 430 d.is_plain_text = !json_output; 431 d.prog_id_as_func_ptr = true; 432 433 res = do_work_on_struct_ops(search_type, search_term, __do_dump, &d, 434 wtr); 435 436 if (!json_output) 437 jsonw_destroy(&wtr); 438 439 return cmd_retval(&res, !!search_term); 440 } 441 442 static int __do_unregister(int fd, const struct bpf_map_info *info, void *data, 443 struct json_writer *wtr) 444 { 445 int zero = 0; 446 447 if (bpf_map_delete_elem(fd, &zero)) { 448 p_err("can't unload %s %s id %u: %s", 449 get_kern_struct_ops_name(info), info->name, 450 info->id, strerror(errno)); 451 return -1; 452 } 453 454 p_info("Unregistered %s %s id %u", 455 get_kern_struct_ops_name(info), info->name, 456 info->id); 457 458 return 0; 459 } 460 461 static int do_unregister(int argc, char **argv) 462 { 463 const char *search_type, *search_term; 464 struct res res; 465 466 if (argc != 2) 467 usage(); 468 469 search_type = GET_ARG(); 470 search_term = GET_ARG(); 471 472 res = do_work_on_struct_ops(search_type, search_term, 473 __do_unregister, NULL, NULL); 474 475 return cmd_retval(&res, true); 476 } 477 478 static int do_register(int argc, char **argv) 479 { 480 LIBBPF_OPTS(bpf_object_open_opts, open_opts); 481 struct bpf_map_info info = {}; 482 __u32 info_len = sizeof(info); 483 int nr_errs = 0, nr_maps = 0; 484 struct bpf_object *obj; 485 struct bpf_link *link; 486 struct bpf_map *map; 487 const char *file; 488 489 if (argc != 1) 490 usage(); 491 492 file = GET_ARG(); 493 494 if (verifier_logs) 495 /* log_level1 + log_level2 + stats, but not stable UAPI */ 496 open_opts.kernel_log_level = 1 + 2 + 4; 497 498 obj = bpf_object__open_file(file, &open_opts); 499 if (!obj) 500 return -1; 501 502 set_max_rlimit(); 503 504 if (bpf_object__load(obj)) { 505 bpf_object__close(obj); 506 return -1; 507 } 508 509 bpf_object__for_each_map(map, obj) { 510 if (bpf_map__type(map) != BPF_MAP_TYPE_STRUCT_OPS) 511 continue; 512 513 link = bpf_map__attach_struct_ops(map); 514 if (!link) { 515 p_err("can't register struct_ops %s: %s", 516 bpf_map__name(map), strerror(errno)); 517 nr_errs++; 518 continue; 519 } 520 nr_maps++; 521 522 bpf_link__disconnect(link); 523 bpf_link__destroy(link); 524 525 if (!bpf_obj_get_info_by_fd(bpf_map__fd(map), &info, 526 &info_len)) 527 p_info("Registered %s %s id %u", 528 get_kern_struct_ops_name(&info), 529 bpf_map__name(map), 530 info.id); 531 else 532 /* Not p_err. The struct_ops was attached 533 * successfully. 534 */ 535 p_info("Registered %s but can't find id: %s", 536 bpf_map__name(map), strerror(errno)); 537 } 538 539 bpf_object__close(obj); 540 541 if (nr_errs) 542 return -1; 543 544 if (!nr_maps) { 545 p_err("no struct_ops found in %s", file); 546 return -1; 547 } 548 549 if (json_output) 550 jsonw_null(json_wtr); 551 552 return 0; 553 } 554 555 static int do_help(int argc, char **argv) 556 { 557 if (json_output) { 558 jsonw_null(json_wtr); 559 return 0; 560 } 561 562 fprintf(stderr, 563 "Usage: %1$s %2$s { show | list } [STRUCT_OPS_MAP]\n" 564 " %1$s %2$s dump [STRUCT_OPS_MAP]\n" 565 " %1$s %2$s register OBJ\n" 566 " %1$s %2$s unregister STRUCT_OPS_MAP\n" 567 " %1$s %2$s help\n" 568 "\n" 569 " STRUCT_OPS_MAP := [ id STRUCT_OPS_MAP_ID | name STRUCT_OPS_MAP_NAME ]\n" 570 " " HELP_SPEC_OPTIONS " }\n" 571 "", 572 bin_name, argv[-2]); 573 574 return 0; 575 } 576 577 static const struct cmd cmds[] = { 578 { "show", do_show }, 579 { "list", do_show }, 580 { "register", do_register }, 581 { "unregister", do_unregister }, 582 { "dump", do_dump }, 583 { "help", do_help }, 584 { 0 } 585 }; 586 587 int do_struct_ops(int argc, char **argv) 588 { 589 int err; 590 591 err = cmd_select(cmds, argc, argv, do_help); 592 593 btf__free(btf_vmlinux); 594 595 return err; 596 } 597