1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
14 #include <sys/types.h>
17 #include <linux/err.h>
18 #include <linux/sizes.h>
26 #include "xlated_dumper.h"
28 static const char * const attach_type_strings[] = {
29 [BPF_SK_SKB_STREAM_PARSER] = "stream_parser",
30 [BPF_SK_SKB_STREAM_VERDICT] = "stream_verdict",
31 [BPF_SK_MSG_VERDICT] = "msg_verdict",
32 [BPF_FLOW_DISSECTOR] = "flow_dissector",
33 [__MAX_BPF_ATTACH_TYPE] = NULL,
36 static enum bpf_attach_type parse_attach_type(const char *str)
38 enum bpf_attach_type type;
40 for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) {
41 if (attach_type_strings[type] &&
42 is_prefix(str, attach_type_strings[type]))
46 return __MAX_BPF_ATTACH_TYPE;
49 static void print_boot_time(__u64 nsecs, char *buf, unsigned int size)
51 struct timespec real_time_ts, boot_time_ts;
52 time_t wallclock_secs;
57 if (clock_gettime(CLOCK_REALTIME, &real_time_ts) ||
58 clock_gettime(CLOCK_BOOTTIME, &boot_time_ts)) {
59 perror("Can't read clocks");
60 snprintf(buf, size, "%llu", nsecs / 1000000000);
64 wallclock_secs = (real_time_ts.tv_sec - boot_time_ts.tv_sec) +
65 (real_time_ts.tv_nsec - boot_time_ts.tv_nsec + nsecs) /
69 if (!localtime_r(&wallclock_secs, &load_tm)) {
70 snprintf(buf, size, "%llu", nsecs / 1000000000);
75 strftime(buf, size, "%s", &load_tm);
77 strftime(buf, size, "%FT%T%z", &load_tm);
80 static int prog_fd_by_tag(unsigned char *tag)
87 struct bpf_prog_info info = {};
88 __u32 len = sizeof(info);
90 err = bpf_prog_get_next_id(id, &id);
92 p_err("%s", strerror(errno));
96 fd = bpf_prog_get_fd_by_id(id);
98 p_err("can't get prog by id (%u): %s",
103 err = bpf_obj_get_info_by_fd(fd, &info, &len);
105 p_err("can't get prog info (%u): %s",
106 id, strerror(errno));
111 if (!memcmp(tag, info.tag, BPF_TAG_SIZE))
118 int prog_parse_fd(int *argc, char ***argv)
122 if (is_prefix(**argv, "id")) {
128 id = strtoul(**argv, &endptr, 0);
130 p_err("can't parse %s as ID", **argv);
135 fd = bpf_prog_get_fd_by_id(id);
137 p_err("get by id (%u): %s", id, strerror(errno));
139 } else if (is_prefix(**argv, "tag")) {
140 unsigned char tag[BPF_TAG_SIZE];
144 if (sscanf(**argv, BPF_TAG_FMT, tag, tag + 1, tag + 2,
145 tag + 3, tag + 4, tag + 5, tag + 6, tag + 7)
147 p_err("can't parse tag");
152 return prog_fd_by_tag(tag);
153 } else if (is_prefix(**argv, "pinned")) {
161 return open_obj_pinned_any(path, BPF_OBJ_PROG);
164 p_err("expected 'id', 'tag' or 'pinned', got: '%s'?", **argv);
168 static void show_prog_maps(int fd, u32 num_maps)
170 struct bpf_prog_info info = {};
171 __u32 len = sizeof(info);
172 __u32 map_ids[num_maps];
176 info.nr_map_ids = num_maps;
177 info.map_ids = ptr_to_u64(map_ids);
179 err = bpf_obj_get_info_by_fd(fd, &info, &len);
180 if (err || !info.nr_map_ids)
184 jsonw_name(json_wtr, "map_ids");
185 jsonw_start_array(json_wtr);
186 for (i = 0; i < info.nr_map_ids; i++)
187 jsonw_uint(json_wtr, map_ids[i]);
188 jsonw_end_array(json_wtr);
191 for (i = 0; i < info.nr_map_ids; i++)
192 printf("%u%s", map_ids[i],
193 i == info.nr_map_ids - 1 ? "" : ",");
197 static void print_prog_json(struct bpf_prog_info *info, int fd)
201 jsonw_start_object(json_wtr);
202 jsonw_uint_field(json_wtr, "id", info->id);
203 if (info->type < ARRAY_SIZE(prog_type_name))
204 jsonw_string_field(json_wtr, "type",
205 prog_type_name[info->type]);
207 jsonw_uint_field(json_wtr, "type", info->type);
210 jsonw_string_field(json_wtr, "name", info->name);
212 jsonw_name(json_wtr, "tag");
213 jsonw_printf(json_wtr, "\"" BPF_TAG_FMT "\"",
214 info->tag[0], info->tag[1], info->tag[2], info->tag[3],
215 info->tag[4], info->tag[5], info->tag[6], info->tag[7]);
217 jsonw_bool_field(json_wtr, "gpl_compatible", info->gpl_compatible);
218 if (info->run_time_ns) {
219 jsonw_uint_field(json_wtr, "run_time_ns", info->run_time_ns);
220 jsonw_uint_field(json_wtr, "run_cnt", info->run_cnt);
223 print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
225 if (info->load_time) {
228 print_boot_time(info->load_time, buf, sizeof(buf));
230 /* Piggy back on load_time, since 0 uid is a valid one */
231 jsonw_name(json_wtr, "loaded_at");
232 jsonw_printf(json_wtr, "%s", buf);
233 jsonw_uint_field(json_wtr, "uid", info->created_by_uid);
236 jsonw_uint_field(json_wtr, "bytes_xlated", info->xlated_prog_len);
238 if (info->jited_prog_len) {
239 jsonw_bool_field(json_wtr, "jited", true);
240 jsonw_uint_field(json_wtr, "bytes_jited", info->jited_prog_len);
242 jsonw_bool_field(json_wtr, "jited", false);
245 memlock = get_fdinfo(fd, "memlock");
247 jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock));
250 if (info->nr_map_ids)
251 show_prog_maps(fd, info->nr_map_ids);
254 jsonw_int_field(json_wtr, "btf_id", info->btf_id);
256 if (!hash_empty(prog_table.table)) {
257 struct pinned_obj *obj;
259 jsonw_name(json_wtr, "pinned");
260 jsonw_start_array(json_wtr);
261 hash_for_each_possible(prog_table.table, obj, hash, info->id) {
262 if (obj->id == info->id)
263 jsonw_string(json_wtr, obj->path);
265 jsonw_end_array(json_wtr);
268 jsonw_end_object(json_wtr);
271 static void print_prog_plain(struct bpf_prog_info *info, int fd)
275 printf("%u: ", info->id);
276 if (info->type < ARRAY_SIZE(prog_type_name))
277 printf("%s ", prog_type_name[info->type]);
279 printf("type %u ", info->type);
282 printf("name %s ", info->name);
285 fprint_hex(stdout, info->tag, BPF_TAG_SIZE, "");
286 print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
287 printf("%s", info->gpl_compatible ? " gpl" : "");
288 if (info->run_time_ns)
289 printf(" run_time_ns %lld run_cnt %lld",
290 info->run_time_ns, info->run_cnt);
293 if (info->load_time) {
296 print_boot_time(info->load_time, buf, sizeof(buf));
298 /* Piggy back on load_time, since 0 uid is a valid one */
299 printf("\tloaded_at %s uid %u\n", buf, info->created_by_uid);
302 printf("\txlated %uB", info->xlated_prog_len);
304 if (info->jited_prog_len)
305 printf(" jited %uB", info->jited_prog_len);
307 printf(" not jited");
309 memlock = get_fdinfo(fd, "memlock");
311 printf(" memlock %sB", memlock);
314 if (info->nr_map_ids)
315 show_prog_maps(fd, info->nr_map_ids);
317 if (!hash_empty(prog_table.table)) {
318 struct pinned_obj *obj;
320 hash_for_each_possible(prog_table.table, obj, hash, info->id) {
321 if (obj->id == info->id)
322 printf("\n\tpinned %s", obj->path);
327 printf("\n\tbtf_id %d", info->btf_id);
332 static int show_prog(int fd)
334 struct bpf_prog_info info = {};
335 __u32 len = sizeof(info);
338 err = bpf_obj_get_info_by_fd(fd, &info, &len);
340 p_err("can't get prog info: %s", strerror(errno));
345 print_prog_json(&info, fd);
347 print_prog_plain(&info, fd);
352 static int do_show(int argc, char **argv)
359 build_pinned_obj_table(&prog_table, BPF_OBJ_PROG);
362 fd = prog_parse_fd(&argc, &argv);
366 return show_prog(fd);
373 jsonw_start_array(json_wtr);
375 err = bpf_prog_get_next_id(id, &id);
377 if (errno == ENOENT) {
381 p_err("can't get next program: %s%s", strerror(errno),
382 errno == EINVAL ? " -- kernel too old?" : "");
387 fd = bpf_prog_get_fd_by_id(id);
391 p_err("can't get prog by id (%u): %s",
392 id, strerror(errno));
404 jsonw_end_array(json_wtr);
409 static int do_dump(int argc, char **argv)
411 struct bpf_prog_info_linear *info_linear;
412 struct bpf_prog_linfo *prog_linfo = NULL;
413 enum {DUMP_JITED, DUMP_XLATED} mode;
414 const char *disasm_opt = NULL;
415 struct bpf_prog_info *info;
416 struct dump_data dd = {};
417 void *func_info = NULL;
418 struct btf *btf = NULL;
419 char *filepath = NULL;
420 bool opcodes = false;
430 if (is_prefix(*argv, "jited")) {
434 } else if (is_prefix(*argv, "xlated")) {
437 p_err("expected 'xlated' or 'jited', got: %s", *argv);
445 fd = prog_parse_fd(&argc, &argv);
449 if (is_prefix(*argv, "file")) {
452 p_err("expected file path");
458 } else if (is_prefix(*argv, "opcodes")) {
461 } else if (is_prefix(*argv, "visual")) {
464 } else if (is_prefix(*argv, "linum")) {
474 if (mode == DUMP_JITED)
475 arrays = 1UL << BPF_PROG_INFO_JITED_INSNS;
477 arrays = 1UL << BPF_PROG_INFO_XLATED_INSNS;
479 arrays |= 1UL << BPF_PROG_INFO_JITED_KSYMS;
480 arrays |= 1UL << BPF_PROG_INFO_JITED_FUNC_LENS;
481 arrays |= 1UL << BPF_PROG_INFO_FUNC_INFO;
482 arrays |= 1UL << BPF_PROG_INFO_LINE_INFO;
483 arrays |= 1UL << BPF_PROG_INFO_JITED_LINE_INFO;
485 info_linear = bpf_program__get_prog_info_linear(fd, arrays);
487 if (IS_ERR_OR_NULL(info_linear)) {
488 p_err("can't get prog info: %s", strerror(errno));
492 info = &info_linear->info;
493 if (mode == DUMP_JITED) {
494 if (info->jited_prog_len == 0) {
495 p_info("no instructions returned");
498 buf = (unsigned char *)(info->jited_prog_insns);
499 member_len = info->jited_prog_len;
500 } else { /* DUMP_XLATED */
501 if (info->xlated_prog_len == 0) {
502 p_err("error retrieving insn dump: kernel.kptr_restrict set?");
505 buf = (unsigned char *)info->xlated_prog_insns;
506 member_len = info->xlated_prog_len;
509 if (info->btf_id && btf__get_from_id(info->btf_id, &btf)) {
510 p_err("failed to get btf");
514 func_info = (void *)info->func_info;
516 if (info->nr_line_info) {
517 prog_linfo = bpf_prog_linfo__new(info);
519 p_info("error in processing bpf_line_info. continue without it.");
523 fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
525 p_err("can't open file %s: %s", filepath,
530 n = write(fd, buf, member_len);
532 if (n != member_len) {
533 p_err("error writing output file: %s",
534 n < 0 ? strerror(errno) : "short write");
539 jsonw_null(json_wtr);
540 } else if (mode == DUMP_JITED) {
541 const char *name = NULL;
544 name = ifindex_to_bfd_params(info->ifindex,
552 if (info->nr_jited_func_lens && info->jited_func_lens) {
553 struct kernel_sym *sym = NULL;
554 struct bpf_func_info *record;
555 char sym_name[SYM_MAX_NAME];
556 unsigned char *img = buf;
560 if (info->nr_jited_ksyms) {
561 kernel_syms_load(&dd);
562 ksyms = (__u64 *) info->jited_ksyms;
566 jsonw_start_array(json_wtr);
568 lens = (__u32 *) info->jited_func_lens;
569 for (i = 0; i < info->nr_jited_func_lens; i++) {
571 sym = kernel_syms_search(&dd, ksyms[i]);
573 sprintf(sym_name, "%s", sym->name);
575 sprintf(sym_name, "0x%016llx", ksyms[i]);
577 strcpy(sym_name, "unknown");
581 record = func_info + i * info->func_info_rec_size;
582 btf_dumper_type_only(btf, record->type_id,
588 jsonw_start_object(json_wtr);
589 if (func_info && func_sig[0] != '\0') {
590 jsonw_name(json_wtr, "proto");
591 jsonw_string(json_wtr, func_sig);
593 jsonw_name(json_wtr, "name");
594 jsonw_string(json_wtr, sym_name);
595 jsonw_name(json_wtr, "insns");
597 if (func_info && func_sig[0] != '\0')
598 printf("%s:\n", func_sig);
599 printf("%s:\n", sym_name);
602 disasm_print_insn(img, lens[i], opcodes,
603 name, disasm_opt, btf,
604 prog_linfo, ksyms[i], i,
610 jsonw_end_object(json_wtr);
616 jsonw_end_array(json_wtr);
618 disasm_print_insn(buf, member_len, opcodes, name,
619 disasm_opt, btf, NULL, 0, 0, false);
623 jsonw_null(json_wtr);
625 dump_xlated_cfg(buf, member_len);
627 kernel_syms_load(&dd);
628 dd.nr_jited_ksyms = info->nr_jited_ksyms;
629 dd.jited_ksyms = (__u64 *) info->jited_ksyms;
631 dd.func_info = func_info;
632 dd.finfo_rec_size = info->func_info_rec_size;
633 dd.prog_linfo = prog_linfo;
636 dump_xlated_json(&dd, buf, member_len, opcodes,
639 dump_xlated_plain(&dd, buf, member_len, opcodes,
641 kernel_syms_destroy(&dd);
652 static int do_pin(int argc, char **argv)
656 err = do_pin_any(argc, argv, bpf_prog_get_fd_by_id);
657 if (!err && json_output)
658 jsonw_null(json_wtr);
668 static int map_replace_compar(const void *p1, const void *p2)
670 const struct map_replace *a = p1, *b = p2;
672 return a->idx - b->idx;
675 static int parse_attach_detach_args(int argc, char **argv, int *progfd,
676 enum bpf_attach_type *attach_type,
682 *progfd = prog_parse_fd(&argc, &argv);
686 *attach_type = parse_attach_type(*argv);
687 if (*attach_type == __MAX_BPF_ATTACH_TYPE) {
688 p_err("invalid attach/detach type");
692 if (*attach_type == BPF_FLOW_DISSECTOR) {
701 *mapfd = map_parse_fd(&argc, &argv);
708 static int do_attach(int argc, char **argv)
710 enum bpf_attach_type attach_type;
714 err = parse_attach_detach_args(argc, argv,
715 &progfd, &attach_type, &mapfd);
719 err = bpf_prog_attach(progfd, mapfd, attach_type, 0);
721 p_err("failed prog attach to map");
726 jsonw_null(json_wtr);
730 static int do_detach(int argc, char **argv)
732 enum bpf_attach_type attach_type;
736 err = parse_attach_detach_args(argc, argv,
737 &progfd, &attach_type, &mapfd);
741 err = bpf_prog_detach2(progfd, mapfd, attach_type);
743 p_err("failed prog detach from map");
748 jsonw_null(json_wtr);
752 static int check_single_stdin(char *file_data_in, char *file_ctx_in)
754 if (file_data_in && file_ctx_in &&
755 !strcmp(file_data_in, "-") && !strcmp(file_ctx_in, "-")) {
756 p_err("cannot use standard input for both data_in and ctx_in");
763 static int get_run_data(const char *fname, void **data_ptr, unsigned int *size)
765 size_t block_size = 256;
766 size_t buf_size = block_size;
777 if (!strcmp(fname, "-"))
780 f = fopen(fname, "r");
782 p_err("failed to open %s: %s", fname, strerror(errno));
786 *data_ptr = malloc(block_size);
788 p_err("failed to allocate memory for data_in/ctx_in: %s",
793 while ((nb_read += fread(*data_ptr + nb_read, 1, block_size, f))) {
797 p_err("failed to read data_in/ctx_in from %s: %s",
798 fname, strerror(errno));
801 if (nb_read > buf_size - block_size) {
802 if (buf_size == UINT32_MAX) {
803 p_err("data_in/ctx_in is too long (max: %d)",
807 /* No space for fread()-ing next chunk; realloc() */
809 tmp = realloc(*data_ptr, buf_size);
811 p_err("failed to reallocate data_in/ctx_in: %s",
833 static void hex_print(void *data, unsigned int size, FILE *f)
838 for (i = 0; i < size; i += 16) {
840 fprintf(f, "%07zx\t", i);
842 /* Hexadecimal values */
843 for (j = i; j < i + 16 && j < size; j++)
844 fprintf(f, "%02x%s", *(uint8_t *)(data + j),
846 for (; j < i + 16; j++)
847 fprintf(f, " %s", j % 2 ? " " : "");
849 /* ASCII values (if relevant), '.' otherwise */
851 for (j = i; j < i + 16 && j < size; j++) {
852 c = *(char *)(data + j);
853 if (c < ' ' || c > '~')
855 fprintf(f, "%c%s", c, j == i + 7 ? " " : "");
863 print_run_output(void *data, unsigned int size, const char *fname,
864 const char *json_key)
872 if (!strcmp(fname, "-")) {
875 jsonw_name(json_wtr, json_key);
876 print_data_json(data, size);
878 hex_print(data, size, f);
883 f = fopen(fname, "w");
885 p_err("failed to open %s: %s", fname, strerror(errno));
889 nb_written = fwrite(data, 1, size, f);
891 if (nb_written != size) {
892 p_err("failed to write output data/ctx: %s", strerror(errno));
899 static int alloc_run_data(void **data_ptr, unsigned int size_out)
901 *data_ptr = calloc(size_out, 1);
903 p_err("failed to allocate memory for output data/ctx: %s",
911 static int do_run(int argc, char **argv)
913 char *data_fname_in = NULL, *data_fname_out = NULL;
914 char *ctx_fname_in = NULL, *ctx_fname_out = NULL;
915 struct bpf_prog_test_run_attr test_attr = {0};
916 const unsigned int default_size = SZ_32K;
917 void *data_in = NULL, *data_out = NULL;
918 void *ctx_in = NULL, *ctx_out = NULL;
919 unsigned int repeat = 1;
925 fd = prog_parse_fd(&argc, &argv);
930 if (detect_common_prefix(*argv, "data_in", "data_out",
931 "data_size_out", NULL))
933 if (detect_common_prefix(*argv, "ctx_in", "ctx_out",
934 "ctx_size_out", NULL))
937 if (is_prefix(*argv, "data_in")) {
942 data_fname_in = GET_ARG();
943 if (check_single_stdin(data_fname_in, ctx_fname_in))
945 } else if (is_prefix(*argv, "data_out")) {
950 data_fname_out = GET_ARG();
951 } else if (is_prefix(*argv, "data_size_out")) {
958 test_attr.data_size_out = strtoul(*argv, &endptr, 0);
960 p_err("can't parse %s as output data size",
965 } else if (is_prefix(*argv, "ctx_in")) {
970 ctx_fname_in = GET_ARG();
971 if (check_single_stdin(data_fname_in, ctx_fname_in))
973 } else if (is_prefix(*argv, "ctx_out")) {
978 ctx_fname_out = GET_ARG();
979 } else if (is_prefix(*argv, "ctx_size_out")) {
986 test_attr.ctx_size_out = strtoul(*argv, &endptr, 0);
988 p_err("can't parse %s as output context size",
993 } else if (is_prefix(*argv, "repeat")) {
1000 repeat = strtoul(*argv, &endptr, 0);
1002 p_err("can't parse %s as repeat number",
1008 p_err("expected no more arguments, 'data_in', 'data_out', 'data_size_out', 'ctx_in', 'ctx_out', 'ctx_size_out' or 'repeat', got: '%s'?",
1014 err = get_run_data(data_fname_in, &data_in, &test_attr.data_size_in);
1019 if (!test_attr.data_size_out)
1020 test_attr.data_size_out = default_size;
1021 err = alloc_run_data(&data_out, test_attr.data_size_out);
1026 err = get_run_data(ctx_fname_in, &ctx_in, &test_attr.ctx_size_in);
1031 if (!test_attr.ctx_size_out)
1032 test_attr.ctx_size_out = default_size;
1033 err = alloc_run_data(&ctx_out, test_attr.ctx_size_out);
1038 test_attr.prog_fd = fd;
1039 test_attr.repeat = repeat;
1040 test_attr.data_in = data_in;
1041 test_attr.data_out = data_out;
1042 test_attr.ctx_in = ctx_in;
1043 test_attr.ctx_out = ctx_out;
1045 err = bpf_prog_test_run_xattr(&test_attr);
1047 p_err("failed to run program: %s", strerror(errno));
1054 jsonw_start_object(json_wtr); /* root */
1056 /* Do not exit on errors occurring when printing output data/context,
1057 * we still want to print return value and duration for program run.
1059 if (test_attr.data_size_out)
1060 err += print_run_output(test_attr.data_out,
1061 test_attr.data_size_out,
1062 data_fname_out, "data_out");
1063 if (test_attr.ctx_size_out)
1064 err += print_run_output(test_attr.ctx_out,
1065 test_attr.ctx_size_out,
1066 ctx_fname_out, "ctx_out");
1069 jsonw_uint_field(json_wtr, "retval", test_attr.retval);
1070 jsonw_uint_field(json_wtr, "duration", test_attr.duration);
1071 jsonw_end_object(json_wtr); /* root */
1073 fprintf(stdout, "Return value: %u, duration%s: %uns\n",
1075 repeat > 1 ? " (average)" : "", test_attr.duration);
1090 static int load_with_options(int argc, char **argv, bool first_prog_only)
1092 struct bpf_object_load_attr load_attr = { 0 };
1093 struct bpf_object_open_attr open_attr = {
1094 .prog_type = BPF_PROG_TYPE_UNSPEC,
1096 enum bpf_attach_type expected_attach_type;
1097 struct map_replace *map_replace = NULL;
1098 struct bpf_program *prog = NULL, *pos;
1099 unsigned int old_map_fds = 0;
1100 const char *pinmaps = NULL;
1101 struct bpf_object *obj;
1102 struct bpf_map *map;
1103 const char *pinfile;
1110 open_attr.file = GET_ARG();
1111 pinfile = GET_ARG();
1114 if (is_prefix(*argv, "type")) {
1119 if (open_attr.prog_type != BPF_PROG_TYPE_UNSPEC) {
1120 p_err("program type already specified");
1121 goto err_free_reuse_maps;
1124 goto err_free_reuse_maps;
1126 /* Put a '/' at the end of type to appease libbpf */
1127 type = malloc(strlen(*argv) + 2);
1129 p_err("mem alloc failed");
1130 goto err_free_reuse_maps;
1133 strcat(type, *argv);
1136 err = libbpf_prog_type_by_name(type,
1137 &open_attr.prog_type,
1138 &expected_attach_type);
1141 goto err_free_reuse_maps;
1144 } else if (is_prefix(*argv, "map")) {
1145 void *new_map_replace;
1146 char *endptr, *name;
1152 goto err_free_reuse_maps;
1154 if (is_prefix(*argv, "idx")) {
1157 idx = strtoul(*argv, &endptr, 0);
1159 p_err("can't parse %s as IDX", *argv);
1160 goto err_free_reuse_maps;
1163 } else if (is_prefix(*argv, "name")) {
1169 p_err("expected 'idx' or 'name', got: '%s'?",
1171 goto err_free_reuse_maps;
1175 fd = map_parse_fd(&argc, &argv);
1177 goto err_free_reuse_maps;
1179 new_map_replace = reallocarray(map_replace,
1181 sizeof(*map_replace));
1182 if (!new_map_replace) {
1183 p_err("mem alloc failed");
1184 goto err_free_reuse_maps;
1186 map_replace = new_map_replace;
1188 map_replace[old_map_fds].idx = idx;
1189 map_replace[old_map_fds].name = name;
1190 map_replace[old_map_fds].fd = fd;
1192 } else if (is_prefix(*argv, "dev")) {
1196 p_err("offload device already specified");
1197 goto err_free_reuse_maps;
1200 goto err_free_reuse_maps;
1202 ifindex = if_nametoindex(*argv);
1204 p_err("unrecognized netdevice '%s': %s",
1205 *argv, strerror(errno));
1206 goto err_free_reuse_maps;
1209 } else if (is_prefix(*argv, "pinmaps")) {
1213 goto err_free_reuse_maps;
1215 pinmaps = GET_ARG();
1217 p_err("expected no more arguments, 'type', 'map' or 'dev', got: '%s'?",
1219 goto err_free_reuse_maps;
1225 obj = __bpf_object__open_xattr(&open_attr, bpf_flags);
1226 if (IS_ERR_OR_NULL(obj)) {
1227 p_err("failed to open object file");
1228 goto err_free_reuse_maps;
1231 bpf_object__for_each_program(pos, obj) {
1232 enum bpf_prog_type prog_type = open_attr.prog_type;
1234 if (open_attr.prog_type == BPF_PROG_TYPE_UNSPEC) {
1235 const char *sec_name = bpf_program__title(pos, false);
1237 err = libbpf_prog_type_by_name(sec_name, &prog_type,
1238 &expected_attach_type);
1243 bpf_program__set_ifindex(pos, ifindex);
1244 bpf_program__set_type(pos, prog_type);
1245 bpf_program__set_expected_attach_type(pos, expected_attach_type);
1248 qsort(map_replace, old_map_fds, sizeof(*map_replace),
1249 map_replace_compar);
1251 /* After the sort maps by name will be first on the list, because they
1252 * have idx == -1. Resolve them.
1255 while (j < old_map_fds && map_replace[j].name) {
1257 bpf_object__for_each_map(map, obj) {
1258 if (!strcmp(bpf_map__name(map), map_replace[j].name)) {
1259 map_replace[j].idx = i;
1264 if (map_replace[j].idx == -1) {
1265 p_err("unable to find map '%s'", map_replace[j].name);
1270 /* Resort if any names were resolved */
1272 qsort(map_replace, old_map_fds, sizeof(*map_replace),
1273 map_replace_compar);
1275 /* Set ifindex and name reuse */
1278 bpf_object__for_each_map(map, obj) {
1279 if (!bpf_map__is_offload_neutral(map))
1280 bpf_map__set_ifindex(map, ifindex);
1282 if (j < old_map_fds && idx == map_replace[j].idx) {
1283 err = bpf_map__reuse_fd(map, map_replace[j++].fd);
1285 p_err("unable to set up map reuse: %d", err);
1289 /* Next reuse wants to apply to the same map */
1290 if (j < old_map_fds && map_replace[j].idx == idx) {
1291 p_err("replacement for map idx %d specified more than once",
1299 if (j < old_map_fds) {
1300 p_err("map idx '%d' not used", map_replace[j].idx);
1304 load_attr.obj = obj;
1306 /* log_level1 + log_level2 + stats, but not stable UAPI */
1307 load_attr.log_level = 1 + 2 + 4;
1309 err = bpf_object__load_xattr(&load_attr);
1311 p_err("failed to load object file");
1315 err = mount_bpffs_for_pin(pinfile);
1319 if (first_prog_only) {
1320 prog = bpf_program__next(NULL, obj);
1322 p_err("object file doesn't contain any bpf program");
1326 err = bpf_obj_pin(bpf_program__fd(prog), pinfile);
1328 p_err("failed to pin program %s",
1329 bpf_program__title(prog, false));
1333 err = bpf_object__pin_programs(obj, pinfile);
1335 p_err("failed to pin all programs");
1341 err = bpf_object__pin_maps(obj, pinmaps);
1343 p_err("failed to pin all maps");
1349 jsonw_null(json_wtr);
1351 bpf_object__close(obj);
1352 for (i = 0; i < old_map_fds; i++)
1353 close(map_replace[i].fd);
1359 if (first_prog_only)
1362 bpf_object__unpin_programs(obj, pinfile);
1364 bpf_object__close(obj);
1365 err_free_reuse_maps:
1366 for (i = 0; i < old_map_fds; i++)
1367 close(map_replace[i].fd);
1372 static int do_load(int argc, char **argv)
1374 return load_with_options(argc, argv, true);
1377 static int do_loadall(int argc, char **argv)
1379 return load_with_options(argc, argv, false);
1382 static int do_help(int argc, char **argv)
1385 jsonw_null(json_wtr);
1390 "Usage: %s %s { show | list } [PROG]\n"
1391 " %s %s dump xlated PROG [{ file FILE | opcodes | visual | linum }]\n"
1392 " %s %s dump jited PROG [{ file FILE | opcodes | linum }]\n"
1393 " %s %s pin PROG FILE\n"
1394 " %s %s { load | loadall } OBJ PATH \\\n"
1395 " [type TYPE] [dev NAME] \\\n"
1396 " [map { idx IDX | name NAME } MAP]\\\n"
1397 " [pinmaps MAP_DIR]\n"
1398 " %s %s attach PROG ATTACH_TYPE [MAP]\n"
1399 " %s %s detach PROG ATTACH_TYPE [MAP]\n"
1400 " %s %s run PROG \\\n"
1401 " data_in FILE \\\n"
1402 " [data_out FILE [data_size_out L]] \\\n"
1403 " [ctx_in FILE [ctx_out FILE [ctx_size_out M]]] \\\n"
1408 " " HELP_SPEC_MAP "\n"
1409 " " HELP_SPEC_PROGRAM "\n"
1410 " TYPE := { socket | kprobe | kretprobe | classifier | action |\n"
1411 " tracepoint | raw_tracepoint | xdp | perf_event | cgroup/skb |\n"
1412 " cgroup/sock | cgroup/dev | lwt_in | lwt_out | lwt_xmit |\n"
1413 " lwt_seg6local | sockops | sk_skb | sk_msg | lirc_mode2 |\n"
1414 " sk_reuseport | flow_dissector | cgroup/sysctl |\n"
1415 " cgroup/bind4 | cgroup/bind6 | cgroup/post_bind4 |\n"
1416 " cgroup/post_bind6 | cgroup/connect4 | cgroup/connect6 |\n"
1417 " cgroup/sendmsg4 | cgroup/sendmsg6 | cgroup/recvmsg4 |\n"
1418 " cgroup/recvmsg6 | cgroup/getsockopt |\n"
1419 " cgroup/setsockopt }\n"
1420 " ATTACH_TYPE := { msg_verdict | stream_verdict | stream_parser |\n"
1421 " flow_dissector }\n"
1422 " " HELP_SPEC_OPTIONS "\n"
1424 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
1425 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
1426 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
1427 bin_name, argv[-2]);
1432 static const struct cmd cmds[] = {
1433 { "show", do_show },
1434 { "list", do_show },
1435 { "help", do_help },
1436 { "dump", do_dump },
1438 { "load", do_load },
1439 { "loadall", do_loadall },
1440 { "attach", do_attach },
1441 { "detach", do_detach },
1442 { "tracelog", do_tracelog },
1447 int do_prog(int argc, char **argv)
1449 return cmd_select(cmds, argc, argv, do_help);