prog.c (986c6f7c3fc855032f3457a5a1b7fbcc09c375bb) prog.c (a19df7139440258e02126f1c795ba64932a8e949)
1// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2/* Copyright (C) 2017-2018 Netronome Systems, Inc. */
3
4#define _GNU_SOURCE
5#include <errno.h>
6#include <fcntl.h>
7#include <signal.h>
8#include <stdarg.h>

--- 12 unchanged lines hidden (view full) ---

21#include <linux/err.h>
22#include <linux/perf_event.h>
23#include <linux/sizes.h>
24
25#include <bpf/bpf.h>
26#include <bpf/btf.h>
27#include <bpf/hashmap.h>
28#include <bpf/libbpf.h>
1// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2/* Copyright (C) 2017-2018 Netronome Systems, Inc. */
3
4#define _GNU_SOURCE
5#include <errno.h>
6#include <fcntl.h>
7#include <signal.h>
8#include <stdarg.h>

--- 12 unchanged lines hidden (view full) ---

21#include <linux/err.h>
22#include <linux/perf_event.h>
23#include <linux/sizes.h>
24
25#include <bpf/bpf.h>
26#include <bpf/btf.h>
27#include <bpf/hashmap.h>
28#include <bpf/libbpf.h>
29#include <bpf/libbpf_internal.h>
29#include <bpf/skel_internal.h>
30
31#include "cfg.h"
32#include "main.h"
33#include "xlated_dumper.h"
34
35#define BPF_METADATA_PREFIX "bpf_metadata_"
36#define BPF_METADATA_PREFIX_LEN (sizeof(BPF_METADATA_PREFIX) - 1)

--- 382 unchanged lines hidden (view full) ---

419 jsonw_destroy(&btf_wtr);
420 }
421
422out_free:
423 btf__free(btf);
424 free(value);
425}
426
30#include <bpf/skel_internal.h>
31
32#include "cfg.h"
33#include "main.h"
34#include "xlated_dumper.h"
35
36#define BPF_METADATA_PREFIX "bpf_metadata_"
37#define BPF_METADATA_PREFIX_LEN (sizeof(BPF_METADATA_PREFIX) - 1)

--- 382 unchanged lines hidden (view full) ---

420 jsonw_destroy(&btf_wtr);
421 }
422
423out_free:
424 btf__free(btf);
425 free(value);
426}
427
427static void print_prog_header_json(struct bpf_prog_info *info)
428static void print_prog_header_json(struct bpf_prog_info *info, int fd)
428{
429{
430 char prog_name[MAX_PROG_FULL_NAME];
431
429 jsonw_uint_field(json_wtr, "id", info->id);
430 if (info->type < ARRAY_SIZE(prog_type_name))
431 jsonw_string_field(json_wtr, "type",
432 prog_type_name[info->type]);
433 else
434 jsonw_uint_field(json_wtr, "type", info->type);
435
432 jsonw_uint_field(json_wtr, "id", info->id);
433 if (info->type < ARRAY_SIZE(prog_type_name))
434 jsonw_string_field(json_wtr, "type",
435 prog_type_name[info->type]);
436 else
437 jsonw_uint_field(json_wtr, "type", info->type);
438
436 if (*info->name)
437 jsonw_string_field(json_wtr, "name", info->name);
439 if (*info->name) {
440 get_prog_full_name(info, fd, prog_name, sizeof(prog_name));
441 jsonw_string_field(json_wtr, "name", prog_name);
442 }
438
439 jsonw_name(json_wtr, "tag");
440 jsonw_printf(json_wtr, "\"" BPF_TAG_FMT "\"",
441 info->tag[0], info->tag[1], info->tag[2], info->tag[3],
442 info->tag[4], info->tag[5], info->tag[6], info->tag[7]);
443
444 jsonw_bool_field(json_wtr, "gpl_compatible", info->gpl_compatible);
445 if (info->run_time_ns) {

--- 4 unchanged lines hidden (view full) ---

450 jsonw_uint_field(json_wtr, "recursion_misses", info->recursion_misses);
451}
452
453static void print_prog_json(struct bpf_prog_info *info, int fd)
454{
455 char *memlock;
456
457 jsonw_start_object(json_wtr);
443
444 jsonw_name(json_wtr, "tag");
445 jsonw_printf(json_wtr, "\"" BPF_TAG_FMT "\"",
446 info->tag[0], info->tag[1], info->tag[2], info->tag[3],
447 info->tag[4], info->tag[5], info->tag[6], info->tag[7]);
448
449 jsonw_bool_field(json_wtr, "gpl_compatible", info->gpl_compatible);
450 if (info->run_time_ns) {

--- 4 unchanged lines hidden (view full) ---

455 jsonw_uint_field(json_wtr, "recursion_misses", info->recursion_misses);
456}
457
458static void print_prog_json(struct bpf_prog_info *info, int fd)
459{
460 char *memlock;
461
462 jsonw_start_object(json_wtr);
458 print_prog_header_json(info);
463 print_prog_header_json(info, fd);
459 print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
460
461 if (info->load_time) {
462 char buf[32];
463
464 print_boot_time(info->load_time, buf, sizeof(buf));
465
466 /* Piggy back on load_time, since 0 uid is a valid one */

--- 35 unchanged lines hidden (view full) ---

502
503 emit_obj_refs_json(refs_table, info->id, json_wtr);
504
505 show_prog_metadata(fd, info->nr_map_ids);
506
507 jsonw_end_object(json_wtr);
508}
509
464 print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
465
466 if (info->load_time) {
467 char buf[32];
468
469 print_boot_time(info->load_time, buf, sizeof(buf));
470
471 /* Piggy back on load_time, since 0 uid is a valid one */

--- 35 unchanged lines hidden (view full) ---

507
508 emit_obj_refs_json(refs_table, info->id, json_wtr);
509
510 show_prog_metadata(fd, info->nr_map_ids);
511
512 jsonw_end_object(json_wtr);
513}
514
510static void print_prog_header_plain(struct bpf_prog_info *info)
515static void print_prog_header_plain(struct bpf_prog_info *info, int fd)
511{
516{
517 char prog_name[MAX_PROG_FULL_NAME];
518
512 printf("%u: ", info->id);
513 if (info->type < ARRAY_SIZE(prog_type_name))
514 printf("%s ", prog_type_name[info->type]);
515 else
516 printf("type %u ", info->type);
517
519 printf("%u: ", info->id);
520 if (info->type < ARRAY_SIZE(prog_type_name))
521 printf("%s ", prog_type_name[info->type]);
522 else
523 printf("type %u ", info->type);
524
518 if (*info->name)
519 printf("name %s ", info->name);
525 if (*info->name) {
526 get_prog_full_name(info, fd, prog_name, sizeof(prog_name));
527 printf("name %s ", prog_name);
528 }
520
521 printf("tag ");
522 fprint_hex(stdout, info->tag, BPF_TAG_SIZE, "");
523 print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
524 printf("%s", info->gpl_compatible ? " gpl" : "");
525 if (info->run_time_ns)
526 printf(" run_time_ns %lld run_cnt %lld",
527 info->run_time_ns, info->run_cnt);
528 if (info->recursion_misses)
529 printf(" recursion_misses %lld", info->recursion_misses);
530 printf("\n");
531}
532
533static void print_prog_plain(struct bpf_prog_info *info, int fd)
534{
535 char *memlock;
536
529
530 printf("tag ");
531 fprint_hex(stdout, info->tag, BPF_TAG_SIZE, "");
532 print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
533 printf("%s", info->gpl_compatible ? " gpl" : "");
534 if (info->run_time_ns)
535 printf(" run_time_ns %lld run_cnt %lld",
536 info->run_time_ns, info->run_cnt);
537 if (info->recursion_misses)
538 printf(" recursion_misses %lld", info->recursion_misses);
539 printf("\n");
540}
541
542static void print_prog_plain(struct bpf_prog_info *info, int fd)
543{
544 char *memlock;
545
537 print_prog_header_plain(info);
546 print_prog_header_plain(info, fd);
538
539 if (info->load_time) {
540 char buf[32];
541
542 print_boot_time(info->load_time, buf, sizeof(buf));
543
544 /* Piggy back on load_time, since 0 uid is a valid one */
545 printf("\tloaded_at %s uid %u\n", buf, info->created_by_uid);

--- 90 unchanged lines hidden (view full) ---

636{
637 __u32 id = 0;
638 int err;
639 int fd;
640
641 if (show_pinned) {
642 prog_table = hashmap__new(hash_fn_for_key_as_id,
643 equal_fn_for_key_as_id, NULL);
547
548 if (info->load_time) {
549 char buf[32];
550
551 print_boot_time(info->load_time, buf, sizeof(buf));
552
553 /* Piggy back on load_time, since 0 uid is a valid one */
554 printf("\tloaded_at %s uid %u\n", buf, info->created_by_uid);

--- 90 unchanged lines hidden (view full) ---

645{
646 __u32 id = 0;
647 int err;
648 int fd;
649
650 if (show_pinned) {
651 prog_table = hashmap__new(hash_fn_for_key_as_id,
652 equal_fn_for_key_as_id, NULL);
644 if (!prog_table) {
653 if (IS_ERR(prog_table)) {
645 p_err("failed to create hashmap for pinned paths");
646 return -1;
647 }
648 build_pinned_obj_table(prog_table, BPF_OBJ_PROG);
649 }
650 build_obj_refs_table(&refs_table, BPF_OBJ_PROG);
651
652 if (argc == 2)

--- 314 unchanged lines hidden (view full) ---

967 err = bpf_obj_get_info_by_fd(fds[i], &info, &info_len);
968 if (err) {
969 p_err("can't get prog info: %s", strerror(errno));
970 break;
971 }
972
973 if (json_output && nb_fds > 1) {
974 jsonw_start_object(json_wtr); /* prog object */
654 p_err("failed to create hashmap for pinned paths");
655 return -1;
656 }
657 build_pinned_obj_table(prog_table, BPF_OBJ_PROG);
658 }
659 build_obj_refs_table(&refs_table, BPF_OBJ_PROG);
660
661 if (argc == 2)

--- 314 unchanged lines hidden (view full) ---

976 err = bpf_obj_get_info_by_fd(fds[i], &info, &info_len);
977 if (err) {
978 p_err("can't get prog info: %s", strerror(errno));
979 break;
980 }
981
982 if (json_output && nb_fds > 1) {
983 jsonw_start_object(json_wtr); /* prog object */
975 print_prog_header_json(&info);
984 print_prog_header_json(&info, fds[i]);
976 jsonw_name(json_wtr, "insns");
977 } else if (nb_fds > 1) {
985 jsonw_name(json_wtr, "insns");
986 } else if (nb_fds > 1) {
978 print_prog_header_plain(&info);
987 print_prog_header_plain(&info, fds[i]);
979 }
980
981 err = prog_dump(&info, mode, filepath, opcodes, visual, linum);
982
983 if (json_output && nb_fds > 1)
984 jsonw_end_object(json_wtr); /* prog object */
985 else if (i != nb_fds - 1 && nb_fds > 1)
986 printf("\n");

--- 272 unchanged lines hidden (view full) ---

1259
1260 return 0;
1261}
1262
1263static int do_run(int argc, char **argv)
1264{
1265 char *data_fname_in = NULL, *data_fname_out = NULL;
1266 char *ctx_fname_in = NULL, *ctx_fname_out = NULL;
988 }
989
990 err = prog_dump(&info, mode, filepath, opcodes, visual, linum);
991
992 if (json_output && nb_fds > 1)
993 jsonw_end_object(json_wtr); /* prog object */
994 else if (i != nb_fds - 1 && nb_fds > 1)
995 printf("\n");

--- 272 unchanged lines hidden (view full) ---

1268
1269 return 0;
1270}
1271
1272static int do_run(int argc, char **argv)
1273{
1274 char *data_fname_in = NULL, *data_fname_out = NULL;
1275 char *ctx_fname_in = NULL, *ctx_fname_out = NULL;
1267 struct bpf_prog_test_run_attr test_attr = {0};
1268 const unsigned int default_size = SZ_32K;
1269 void *data_in = NULL, *data_out = NULL;
1270 void *ctx_in = NULL, *ctx_out = NULL;
1271 unsigned int repeat = 1;
1272 int fd, err;
1276 const unsigned int default_size = SZ_32K;
1277 void *data_in = NULL, *data_out = NULL;
1278 void *ctx_in = NULL, *ctx_out = NULL;
1279 unsigned int repeat = 1;
1280 int fd, err;
1281 LIBBPF_OPTS(bpf_test_run_opts, test_attr);
1273
1274 if (!REQ_ARGS(4))
1275 return -1;
1276
1277 fd = prog_parse_fd(&argc, &argv);
1278 if (fd < 0)
1279 return -1;
1280

--- 101 unchanged lines hidden (view full) ---

1382 if (ctx_in) {
1383 if (!test_attr.ctx_size_out)
1384 test_attr.ctx_size_out = default_size;
1385 err = alloc_run_data(&ctx_out, test_attr.ctx_size_out);
1386 if (err)
1387 goto free_ctx_in;
1388 }
1389
1282
1283 if (!REQ_ARGS(4))
1284 return -1;
1285
1286 fd = prog_parse_fd(&argc, &argv);
1287 if (fd < 0)
1288 return -1;
1289

--- 101 unchanged lines hidden (view full) ---

1391 if (ctx_in) {
1392 if (!test_attr.ctx_size_out)
1393 test_attr.ctx_size_out = default_size;
1394 err = alloc_run_data(&ctx_out, test_attr.ctx_size_out);
1395 if (err)
1396 goto free_ctx_in;
1397 }
1398
1390 test_attr.prog_fd = fd;
1391 test_attr.repeat = repeat;
1392 test_attr.data_in = data_in;
1393 test_attr.data_out = data_out;
1394 test_attr.ctx_in = ctx_in;
1395 test_attr.ctx_out = ctx_out;
1396
1399 test_attr.repeat = repeat;
1400 test_attr.data_in = data_in;
1401 test_attr.data_out = data_out;
1402 test_attr.ctx_in = ctx_in;
1403 test_attr.ctx_out = ctx_out;
1404
1397 err = bpf_prog_test_run_xattr(&test_attr);
1405 err = bpf_prog_test_run_opts(fd, &test_attr);
1398 if (err) {
1399 p_err("failed to run program: %s", strerror(errno));
1400 goto free_ctx_out;
1401 }
1402
1403 err = 0;
1404
1405 if (json_output)

--- 140 unchanged lines hidden (view full) ---

1546 goto err_free_reuse_maps;
1547 }
1548 NEXT_ARG();
1549
1550 fd = map_parse_fd(&argc, &argv);
1551 if (fd < 0)
1552 goto err_free_reuse_maps;
1553
1406 if (err) {
1407 p_err("failed to run program: %s", strerror(errno));
1408 goto free_ctx_out;
1409 }
1410
1411 err = 0;
1412
1413 if (json_output)

--- 140 unchanged lines hidden (view full) ---

1554 goto err_free_reuse_maps;
1555 }
1556 NEXT_ARG();
1557
1558 fd = map_parse_fd(&argc, &argv);
1559 if (fd < 0)
1560 goto err_free_reuse_maps;
1561
1554 new_map_replace = reallocarray(map_replace,
1555 old_map_fds + 1,
1556 sizeof(*map_replace));
1562 new_map_replace = libbpf_reallocarray(map_replace,
1563 old_map_fds + 1,
1564 sizeof(*map_replace));
1557 if (!new_map_replace) {
1558 p_err("mem alloc failed");
1559 goto err_free_reuse_maps;
1560 }
1561 map_replace = new_map_replace;
1562
1563 map_replace[old_map_fds].idx = idx;
1564 map_replace[old_map_fds].name = name;

--- 705 unchanged lines hidden (view full) ---

2270 p_err("failed to open and/or load BPF object");
2271 goto out;
2272 }
2273
2274 profile_obj->rodata->num_cpu = num_cpu;
2275 profile_obj->rodata->num_metric = num_metric;
2276
2277 /* adjust map sizes */
1565 if (!new_map_replace) {
1566 p_err("mem alloc failed");
1567 goto err_free_reuse_maps;
1568 }
1569 map_replace = new_map_replace;
1570
1571 map_replace[old_map_fds].idx = idx;
1572 map_replace[old_map_fds].name = name;

--- 705 unchanged lines hidden (view full) ---

2278 p_err("failed to open and/or load BPF object");
2279 goto out;
2280 }
2281
2282 profile_obj->rodata->num_cpu = num_cpu;
2283 profile_obj->rodata->num_metric = num_metric;
2284
2285 /* adjust map sizes */
2278 bpf_map__resize(profile_obj->maps.events, num_metric * num_cpu);
2279 bpf_map__resize(profile_obj->maps.fentry_readings, num_metric);
2280 bpf_map__resize(profile_obj->maps.accum_readings, num_metric);
2281 bpf_map__resize(profile_obj->maps.counts, 1);
2286 bpf_map__set_max_entries(profile_obj->maps.events, num_metric * num_cpu);
2287 bpf_map__set_max_entries(profile_obj->maps.fentry_readings, num_metric);
2288 bpf_map__set_max_entries(profile_obj->maps.accum_readings, num_metric);
2289 bpf_map__set_max_entries(profile_obj->maps.counts, 1);
2282
2283 /* change target name */
2284 profile_tgt_name = profile_target_name(profile_tgt_fd);
2285 if (!profile_tgt_name)
2286 goto out;
2287
2288 bpf_object__for_each_program(prog, profile_obj->obj) {
2289 err = bpf_program__set_attach_target(prog, profile_tgt_fd,

--- 113 unchanged lines hidden ---
2290
2291 /* change target name */
2292 profile_tgt_name = profile_target_name(profile_tgt_fd);
2293 if (!profile_tgt_name)
2294 goto out;
2295
2296 bpf_object__for_each_program(prog, profile_obj->obj) {
2297 err = bpf_program__set_attach_target(prog, profile_tgt_fd,

--- 113 unchanged lines hidden ---