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);
375 jsonw_start_array(json_wtr);
377 err = bpf_prog_get_next_id(id, &id);
379 if (errno == ENOENT) {
383 p_err("can't get next program: %s%s", strerror(errno),
384 errno == EINVAL ? " -- kernel too old?" : "");
389 fd = bpf_prog_get_fd_by_id(id);
393 p_err("can't get prog by id (%u): %s",
394 id, strerror(errno));
406 jsonw_end_array(json_wtr);
411 static int do_dump(int argc, char **argv)
413 struct bpf_prog_info_linear *info_linear;
414 struct bpf_prog_linfo *prog_linfo = NULL;
415 enum {DUMP_JITED, DUMP_XLATED} mode;
416 const char *disasm_opt = NULL;
417 struct bpf_prog_info *info;
418 struct dump_data dd = {};
419 void *func_info = NULL;
420 struct btf *btf = NULL;
421 char *filepath = NULL;
422 bool opcodes = false;
432 if (is_prefix(*argv, "jited")) {
436 } else if (is_prefix(*argv, "xlated")) {
439 p_err("expected 'xlated' or 'jited', got: %s", *argv);
447 fd = prog_parse_fd(&argc, &argv);
451 if (is_prefix(*argv, "file")) {
454 p_err("expected file path");
460 } else if (is_prefix(*argv, "opcodes")) {
463 } else if (is_prefix(*argv, "visual")) {
466 } else if (is_prefix(*argv, "linum")) {
476 if (mode == DUMP_JITED)
477 arrays = 1UL << BPF_PROG_INFO_JITED_INSNS;
479 arrays = 1UL << BPF_PROG_INFO_XLATED_INSNS;
481 arrays |= 1UL << BPF_PROG_INFO_JITED_KSYMS;
482 arrays |= 1UL << BPF_PROG_INFO_JITED_FUNC_LENS;
483 arrays |= 1UL << BPF_PROG_INFO_FUNC_INFO;
484 arrays |= 1UL << BPF_PROG_INFO_LINE_INFO;
485 arrays |= 1UL << BPF_PROG_INFO_JITED_LINE_INFO;
487 info_linear = bpf_program__get_prog_info_linear(fd, arrays);
489 if (IS_ERR_OR_NULL(info_linear)) {
490 p_err("can't get prog info: %s", strerror(errno));
494 info = &info_linear->info;
495 if (mode == DUMP_JITED) {
496 if (info->jited_prog_len == 0) {
497 p_info("no instructions returned");
500 buf = (unsigned char *)(info->jited_prog_insns);
501 member_len = info->jited_prog_len;
502 } else { /* DUMP_XLATED */
503 if (info->xlated_prog_len == 0) {
504 p_err("error retrieving insn dump: kernel.kptr_restrict set?");
507 buf = (unsigned char *)info->xlated_prog_insns;
508 member_len = info->xlated_prog_len;
511 if (info->btf_id && btf__get_from_id(info->btf_id, &btf)) {
512 p_err("failed to get btf");
516 func_info = (void *)info->func_info;
518 if (info->nr_line_info) {
519 prog_linfo = bpf_prog_linfo__new(info);
521 p_info("error in processing bpf_line_info. continue without it.");
525 fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
527 p_err("can't open file %s: %s", filepath,
532 n = write(fd, buf, member_len);
534 if (n != member_len) {
535 p_err("error writing output file: %s",
536 n < 0 ? strerror(errno) : "short write");
541 jsonw_null(json_wtr);
542 } else if (mode == DUMP_JITED) {
543 const char *name = NULL;
546 name = ifindex_to_bfd_params(info->ifindex,
554 if (info->nr_jited_func_lens && info->jited_func_lens) {
555 struct kernel_sym *sym = NULL;
556 struct bpf_func_info *record;
557 char sym_name[SYM_MAX_NAME];
558 unsigned char *img = buf;
562 if (info->nr_jited_ksyms) {
563 kernel_syms_load(&dd);
564 ksyms = (__u64 *) info->jited_ksyms;
568 jsonw_start_array(json_wtr);
570 lens = (__u32 *) info->jited_func_lens;
571 for (i = 0; i < info->nr_jited_func_lens; i++) {
573 sym = kernel_syms_search(&dd, ksyms[i]);
575 sprintf(sym_name, "%s", sym->name);
577 sprintf(sym_name, "0x%016llx", ksyms[i]);
579 strcpy(sym_name, "unknown");
583 record = func_info + i * info->func_info_rec_size;
584 btf_dumper_type_only(btf, record->type_id,
590 jsonw_start_object(json_wtr);
591 if (func_info && func_sig[0] != '\0') {
592 jsonw_name(json_wtr, "proto");
593 jsonw_string(json_wtr, func_sig);
595 jsonw_name(json_wtr, "name");
596 jsonw_string(json_wtr, sym_name);
597 jsonw_name(json_wtr, "insns");
599 if (func_info && func_sig[0] != '\0')
600 printf("%s:\n", func_sig);
601 printf("%s:\n", sym_name);
604 disasm_print_insn(img, lens[i], opcodes,
605 name, disasm_opt, btf,
606 prog_linfo, ksyms[i], i,
612 jsonw_end_object(json_wtr);
618 jsonw_end_array(json_wtr);
620 disasm_print_insn(buf, member_len, opcodes, name,
621 disasm_opt, btf, NULL, 0, 0, false);
625 jsonw_null(json_wtr);
627 dump_xlated_cfg(buf, member_len);
629 kernel_syms_load(&dd);
630 dd.nr_jited_ksyms = info->nr_jited_ksyms;
631 dd.jited_ksyms = (__u64 *) info->jited_ksyms;
633 dd.func_info = func_info;
634 dd.finfo_rec_size = info->func_info_rec_size;
635 dd.prog_linfo = prog_linfo;
638 dump_xlated_json(&dd, buf, member_len, opcodes,
641 dump_xlated_plain(&dd, buf, member_len, opcodes,
643 kernel_syms_destroy(&dd);
654 static int do_pin(int argc, char **argv)
658 err = do_pin_any(argc, argv, bpf_prog_get_fd_by_id);
659 if (!err && json_output)
660 jsonw_null(json_wtr);
670 static int map_replace_compar(const void *p1, const void *p2)
672 const struct map_replace *a = p1, *b = p2;
674 return a->idx - b->idx;
677 static int parse_attach_detach_args(int argc, char **argv, int *progfd,
678 enum bpf_attach_type *attach_type,
684 *progfd = prog_parse_fd(&argc, &argv);
688 *attach_type = parse_attach_type(*argv);
689 if (*attach_type == __MAX_BPF_ATTACH_TYPE) {
690 p_err("invalid attach/detach type");
694 if (*attach_type == BPF_FLOW_DISSECTOR) {
703 *mapfd = map_parse_fd(&argc, &argv);
710 static int do_attach(int argc, char **argv)
712 enum bpf_attach_type attach_type;
716 err = parse_attach_detach_args(argc, argv,
717 &progfd, &attach_type, &mapfd);
721 err = bpf_prog_attach(progfd, mapfd, attach_type, 0);
723 p_err("failed prog attach to map");
728 jsonw_null(json_wtr);
732 static int do_detach(int argc, char **argv)
734 enum bpf_attach_type attach_type;
738 err = parse_attach_detach_args(argc, argv,
739 &progfd, &attach_type, &mapfd);
743 err = bpf_prog_detach2(progfd, mapfd, attach_type);
745 p_err("failed prog detach from map");
750 jsonw_null(json_wtr);
754 static int check_single_stdin(char *file_data_in, char *file_ctx_in)
756 if (file_data_in && file_ctx_in &&
757 !strcmp(file_data_in, "-") && !strcmp(file_ctx_in, "-")) {
758 p_err("cannot use standard input for both data_in and ctx_in");
765 static int get_run_data(const char *fname, void **data_ptr, unsigned int *size)
767 size_t block_size = 256;
768 size_t buf_size = block_size;
779 if (!strcmp(fname, "-"))
782 f = fopen(fname, "r");
784 p_err("failed to open %s: %s", fname, strerror(errno));
788 *data_ptr = malloc(block_size);
790 p_err("failed to allocate memory for data_in/ctx_in: %s",
795 while ((nb_read += fread(*data_ptr + nb_read, 1, block_size, f))) {
799 p_err("failed to read data_in/ctx_in from %s: %s",
800 fname, strerror(errno));
803 if (nb_read > buf_size - block_size) {
804 if (buf_size == UINT32_MAX) {
805 p_err("data_in/ctx_in is too long (max: %d)",
809 /* No space for fread()-ing next chunk; realloc() */
811 tmp = realloc(*data_ptr, buf_size);
813 p_err("failed to reallocate data_in/ctx_in: %s",
835 static void hex_print(void *data, unsigned int size, FILE *f)
840 for (i = 0; i < size; i += 16) {
842 fprintf(f, "%07zx\t", i);
844 /* Hexadecimal values */
845 for (j = i; j < i + 16 && j < size; j++)
846 fprintf(f, "%02x%s", *(uint8_t *)(data + j),
848 for (; j < i + 16; j++)
849 fprintf(f, " %s", j % 2 ? " " : "");
851 /* ASCII values (if relevant), '.' otherwise */
853 for (j = i; j < i + 16 && j < size; j++) {
854 c = *(char *)(data + j);
855 if (c < ' ' || c > '~')
857 fprintf(f, "%c%s", c, j == i + 7 ? " " : "");
865 print_run_output(void *data, unsigned int size, const char *fname,
866 const char *json_key)
874 if (!strcmp(fname, "-")) {
877 jsonw_name(json_wtr, json_key);
878 print_data_json(data, size);
880 hex_print(data, size, f);
885 f = fopen(fname, "w");
887 p_err("failed to open %s: %s", fname, strerror(errno));
891 nb_written = fwrite(data, 1, size, f);
893 if (nb_written != size) {
894 p_err("failed to write output data/ctx: %s", strerror(errno));
901 static int alloc_run_data(void **data_ptr, unsigned int size_out)
903 *data_ptr = calloc(size_out, 1);
905 p_err("failed to allocate memory for output data/ctx: %s",
913 static int do_run(int argc, char **argv)
915 char *data_fname_in = NULL, *data_fname_out = NULL;
916 char *ctx_fname_in = NULL, *ctx_fname_out = NULL;
917 struct bpf_prog_test_run_attr test_attr = {0};
918 const unsigned int default_size = SZ_32K;
919 void *data_in = NULL, *data_out = NULL;
920 void *ctx_in = NULL, *ctx_out = NULL;
921 unsigned int repeat = 1;
927 fd = prog_parse_fd(&argc, &argv);
932 if (detect_common_prefix(*argv, "data_in", "data_out",
933 "data_size_out", NULL))
935 if (detect_common_prefix(*argv, "ctx_in", "ctx_out",
936 "ctx_size_out", NULL))
939 if (is_prefix(*argv, "data_in")) {
944 data_fname_in = GET_ARG();
945 if (check_single_stdin(data_fname_in, ctx_fname_in))
947 } else if (is_prefix(*argv, "data_out")) {
952 data_fname_out = GET_ARG();
953 } else if (is_prefix(*argv, "data_size_out")) {
960 test_attr.data_size_out = strtoul(*argv, &endptr, 0);
962 p_err("can't parse %s as output data size",
967 } else if (is_prefix(*argv, "ctx_in")) {
972 ctx_fname_in = GET_ARG();
973 if (check_single_stdin(data_fname_in, ctx_fname_in))
975 } else if (is_prefix(*argv, "ctx_out")) {
980 ctx_fname_out = GET_ARG();
981 } else if (is_prefix(*argv, "ctx_size_out")) {
988 test_attr.ctx_size_out = strtoul(*argv, &endptr, 0);
990 p_err("can't parse %s as output context size",
995 } else if (is_prefix(*argv, "repeat")) {
1002 repeat = strtoul(*argv, &endptr, 0);
1004 p_err("can't parse %s as repeat number",
1010 p_err("expected no more arguments, 'data_in', 'data_out', 'data_size_out', 'ctx_in', 'ctx_out', 'ctx_size_out' or 'repeat', got: '%s'?",
1016 err = get_run_data(data_fname_in, &data_in, &test_attr.data_size_in);
1021 if (!test_attr.data_size_out)
1022 test_attr.data_size_out = default_size;
1023 err = alloc_run_data(&data_out, test_attr.data_size_out);
1028 err = get_run_data(ctx_fname_in, &ctx_in, &test_attr.ctx_size_in);
1033 if (!test_attr.ctx_size_out)
1034 test_attr.ctx_size_out = default_size;
1035 err = alloc_run_data(&ctx_out, test_attr.ctx_size_out);
1040 test_attr.prog_fd = fd;
1041 test_attr.repeat = repeat;
1042 test_attr.data_in = data_in;
1043 test_attr.data_out = data_out;
1044 test_attr.ctx_in = ctx_in;
1045 test_attr.ctx_out = ctx_out;
1047 err = bpf_prog_test_run_xattr(&test_attr);
1049 p_err("failed to run program: %s", strerror(errno));
1056 jsonw_start_object(json_wtr); /* root */
1058 /* Do not exit on errors occurring when printing output data/context,
1059 * we still want to print return value and duration for program run.
1061 if (test_attr.data_size_out)
1062 err += print_run_output(test_attr.data_out,
1063 test_attr.data_size_out,
1064 data_fname_out, "data_out");
1065 if (test_attr.ctx_size_out)
1066 err += print_run_output(test_attr.ctx_out,
1067 test_attr.ctx_size_out,
1068 ctx_fname_out, "ctx_out");
1071 jsonw_uint_field(json_wtr, "retval", test_attr.retval);
1072 jsonw_uint_field(json_wtr, "duration", test_attr.duration);
1073 jsonw_end_object(json_wtr); /* root */
1075 fprintf(stdout, "Return value: %u, duration%s: %uns\n",
1077 repeat > 1 ? " (average)" : "", test_attr.duration);
1092 static int load_with_options(int argc, char **argv, bool first_prog_only)
1094 struct bpf_object_load_attr load_attr = { 0 };
1095 struct bpf_object_open_attr open_attr = {
1096 .prog_type = BPF_PROG_TYPE_UNSPEC,
1098 enum bpf_attach_type expected_attach_type;
1099 struct map_replace *map_replace = NULL;
1100 struct bpf_program *prog = NULL, *pos;
1101 unsigned int old_map_fds = 0;
1102 const char *pinmaps = NULL;
1103 struct bpf_object *obj;
1104 struct bpf_map *map;
1105 const char *pinfile;
1112 open_attr.file = GET_ARG();
1113 pinfile = GET_ARG();
1116 if (is_prefix(*argv, "type")) {
1121 if (open_attr.prog_type != BPF_PROG_TYPE_UNSPEC) {
1122 p_err("program type already specified");
1123 goto err_free_reuse_maps;
1126 goto err_free_reuse_maps;
1128 /* Put a '/' at the end of type to appease libbpf */
1129 type = malloc(strlen(*argv) + 2);
1131 p_err("mem alloc failed");
1132 goto err_free_reuse_maps;
1135 strcat(type, *argv);
1138 err = libbpf_prog_type_by_name(type,
1139 &open_attr.prog_type,
1140 &expected_attach_type);
1143 goto err_free_reuse_maps;
1146 } else if (is_prefix(*argv, "map")) {
1147 void *new_map_replace;
1148 char *endptr, *name;
1154 goto err_free_reuse_maps;
1156 if (is_prefix(*argv, "idx")) {
1159 idx = strtoul(*argv, &endptr, 0);
1161 p_err("can't parse %s as IDX", *argv);
1162 goto err_free_reuse_maps;
1165 } else if (is_prefix(*argv, "name")) {
1171 p_err("expected 'idx' or 'name', got: '%s'?",
1173 goto err_free_reuse_maps;
1177 fd = map_parse_fd(&argc, &argv);
1179 goto err_free_reuse_maps;
1181 new_map_replace = reallocarray(map_replace,
1183 sizeof(*map_replace));
1184 if (!new_map_replace) {
1185 p_err("mem alloc failed");
1186 goto err_free_reuse_maps;
1188 map_replace = new_map_replace;
1190 map_replace[old_map_fds].idx = idx;
1191 map_replace[old_map_fds].name = name;
1192 map_replace[old_map_fds].fd = fd;
1194 } else if (is_prefix(*argv, "dev")) {
1198 p_err("offload device already specified");
1199 goto err_free_reuse_maps;
1202 goto err_free_reuse_maps;
1204 ifindex = if_nametoindex(*argv);
1206 p_err("unrecognized netdevice '%s': %s",
1207 *argv, strerror(errno));
1208 goto err_free_reuse_maps;
1211 } else if (is_prefix(*argv, "pinmaps")) {
1215 goto err_free_reuse_maps;
1217 pinmaps = GET_ARG();
1219 p_err("expected no more arguments, 'type', 'map' or 'dev', got: '%s'?",
1221 goto err_free_reuse_maps;
1227 obj = __bpf_object__open_xattr(&open_attr, bpf_flags);
1228 if (IS_ERR_OR_NULL(obj)) {
1229 p_err("failed to open object file");
1230 goto err_free_reuse_maps;
1233 bpf_object__for_each_program(pos, obj) {
1234 enum bpf_prog_type prog_type = open_attr.prog_type;
1236 if (open_attr.prog_type == BPF_PROG_TYPE_UNSPEC) {
1237 const char *sec_name = bpf_program__title(pos, false);
1239 err = libbpf_prog_type_by_name(sec_name, &prog_type,
1240 &expected_attach_type);
1245 bpf_program__set_ifindex(pos, ifindex);
1246 bpf_program__set_type(pos, prog_type);
1247 bpf_program__set_expected_attach_type(pos, expected_attach_type);
1250 qsort(map_replace, old_map_fds, sizeof(*map_replace),
1251 map_replace_compar);
1253 /* After the sort maps by name will be first on the list, because they
1254 * have idx == -1. Resolve them.
1257 while (j < old_map_fds && map_replace[j].name) {
1259 bpf_object__for_each_map(map, obj) {
1260 if (!strcmp(bpf_map__name(map), map_replace[j].name)) {
1261 map_replace[j].idx = i;
1266 if (map_replace[j].idx == -1) {
1267 p_err("unable to find map '%s'", map_replace[j].name);
1272 /* Resort if any names were resolved */
1274 qsort(map_replace, old_map_fds, sizeof(*map_replace),
1275 map_replace_compar);
1277 /* Set ifindex and name reuse */
1280 bpf_object__for_each_map(map, obj) {
1281 if (!bpf_map__is_offload_neutral(map))
1282 bpf_map__set_ifindex(map, ifindex);
1284 if (j < old_map_fds && idx == map_replace[j].idx) {
1285 err = bpf_map__reuse_fd(map, map_replace[j++].fd);
1287 p_err("unable to set up map reuse: %d", err);
1291 /* Next reuse wants to apply to the same map */
1292 if (j < old_map_fds && map_replace[j].idx == idx) {
1293 p_err("replacement for map idx %d specified more than once",
1301 if (j < old_map_fds) {
1302 p_err("map idx '%d' not used", map_replace[j].idx);
1306 load_attr.obj = obj;
1308 /* log_level1 + log_level2 + stats, but not stable UAPI */
1309 load_attr.log_level = 1 + 2 + 4;
1311 err = bpf_object__load_xattr(&load_attr);
1313 p_err("failed to load object file");
1317 err = mount_bpffs_for_pin(pinfile);
1321 if (first_prog_only) {
1322 prog = bpf_program__next(NULL, obj);
1324 p_err("object file doesn't contain any bpf program");
1328 err = bpf_obj_pin(bpf_program__fd(prog), pinfile);
1330 p_err("failed to pin program %s",
1331 bpf_program__title(prog, false));
1335 err = bpf_object__pin_programs(obj, pinfile);
1337 p_err("failed to pin all programs");
1343 err = bpf_object__pin_maps(obj, pinmaps);
1345 p_err("failed to pin all maps");
1351 jsonw_null(json_wtr);
1353 bpf_object__close(obj);
1354 for (i = 0; i < old_map_fds; i++)
1355 close(map_replace[i].fd);
1361 if (first_prog_only)
1364 bpf_object__unpin_programs(obj, pinfile);
1366 bpf_object__close(obj);
1367 err_free_reuse_maps:
1368 for (i = 0; i < old_map_fds; i++)
1369 close(map_replace[i].fd);
1374 static int do_load(int argc, char **argv)
1376 return load_with_options(argc, argv, true);
1379 static int do_loadall(int argc, char **argv)
1381 return load_with_options(argc, argv, false);
1384 static int do_help(int argc, char **argv)
1387 jsonw_null(json_wtr);
1392 "Usage: %s %s { show | list } [PROG]\n"
1393 " %s %s dump xlated PROG [{ file FILE | opcodes | visual | linum }]\n"
1394 " %s %s dump jited PROG [{ file FILE | opcodes | linum }]\n"
1395 " %s %s pin PROG FILE\n"
1396 " %s %s { load | loadall } OBJ PATH \\\n"
1397 " [type TYPE] [dev NAME] \\\n"
1398 " [map { idx IDX | name NAME } MAP]\\\n"
1399 " [pinmaps MAP_DIR]\n"
1400 " %s %s attach PROG ATTACH_TYPE [MAP]\n"
1401 " %s %s detach PROG ATTACH_TYPE [MAP]\n"
1402 " %s %s run PROG \\\n"
1403 " data_in FILE \\\n"
1404 " [data_out FILE [data_size_out L]] \\\n"
1405 " [ctx_in FILE [ctx_out FILE [ctx_size_out M]]] \\\n"
1410 " " HELP_SPEC_MAP "\n"
1411 " " HELP_SPEC_PROGRAM "\n"
1412 " TYPE := { socket | kprobe | kretprobe | classifier | action |\n"
1413 " tracepoint | raw_tracepoint | xdp | perf_event | cgroup/skb |\n"
1414 " cgroup/sock | cgroup/dev | lwt_in | lwt_out | lwt_xmit |\n"
1415 " lwt_seg6local | sockops | sk_skb | sk_msg | lirc_mode2 |\n"
1416 " sk_reuseport | flow_dissector | cgroup/sysctl |\n"
1417 " cgroup/bind4 | cgroup/bind6 | cgroup/post_bind4 |\n"
1418 " cgroup/post_bind6 | cgroup/connect4 | cgroup/connect6 |\n"
1419 " cgroup/sendmsg4 | cgroup/sendmsg6 | cgroup/recvmsg4 |\n"
1420 " cgroup/recvmsg6 | cgroup/getsockopt |\n"
1421 " cgroup/setsockopt }\n"
1422 " ATTACH_TYPE := { msg_verdict | stream_verdict | stream_parser |\n"
1423 " flow_dissector }\n"
1424 " " HELP_SPEC_OPTIONS "\n"
1426 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
1427 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
1428 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
1429 bin_name, argv[-2]);
1434 static const struct cmd cmds[] = {
1435 { "show", do_show },
1436 { "list", do_show },
1437 { "help", do_help },
1438 { "dump", do_dump },
1440 { "load", do_load },
1441 { "loadall", do_loadall },
1442 { "attach", do_attach },
1443 { "detach", do_detach },
1444 { "tracelog", do_tracelog },
1449 int do_prog(int argc, char **argv)
1451 return cmd_select(cmds, argc, argv, do_help);