2 * Copyright (C) 2017-2018 Netronome Systems, Inc.
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.
9 * The BSD 2-Clause License:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
44 #include <sys/types.h>
47 #include <linux/err.h>
54 #include "xlated_dumper.h"
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 [BPF_PROG_TYPE_FLOW_DISSECTOR
] = "flow_dissector",
80 static const char * const attach_type_strings
[] = {
81 [BPF_SK_SKB_STREAM_PARSER
] = "stream_parser",
82 [BPF_SK_SKB_STREAM_VERDICT
] = "stream_verdict",
83 [BPF_SK_MSG_VERDICT
] = "msg_verdict",
84 [__MAX_BPF_ATTACH_TYPE
] = NULL
,
87 enum bpf_attach_type
parse_attach_type(const char *str
)
89 enum bpf_attach_type type
;
91 for (type
= 0; type
< __MAX_BPF_ATTACH_TYPE
; type
++) {
92 if (attach_type_strings
[type
] &&
93 is_prefix(str
, attach_type_strings
[type
]))
97 return __MAX_BPF_ATTACH_TYPE
;
100 static void print_boot_time(__u64 nsecs
, char *buf
, unsigned int size
)
102 struct timespec real_time_ts
, boot_time_ts
;
103 time_t wallclock_secs
;
108 if (clock_gettime(CLOCK_REALTIME
, &real_time_ts
) ||
109 clock_gettime(CLOCK_BOOTTIME
, &boot_time_ts
)) {
110 perror("Can't read clocks");
111 snprintf(buf
, size
, "%llu", nsecs
/ 1000000000);
115 wallclock_secs
= (real_time_ts
.tv_sec
- boot_time_ts
.tv_sec
) +
116 (real_time_ts
.tv_nsec
- boot_time_ts
.tv_nsec
+ nsecs
) /
120 if (!localtime_r(&wallclock_secs
, &load_tm
)) {
121 snprintf(buf
, size
, "%llu", nsecs
/ 1000000000);
126 strftime(buf
, size
, "%s", &load_tm
);
128 strftime(buf
, size
, "%FT%T%z", &load_tm
);
131 static int prog_fd_by_tag(unsigned char *tag
)
133 struct bpf_prog_info info
= {};
134 __u32 len
= sizeof(info
);
140 err
= bpf_prog_get_next_id(id
, &id
);
142 p_err("%s", strerror(errno
));
146 fd
= bpf_prog_get_fd_by_id(id
);
148 p_err("can't get prog by id (%u): %s",
149 id
, strerror(errno
));
153 err
= bpf_obj_get_info_by_fd(fd
, &info
, &len
);
155 p_err("can't get prog info (%u): %s",
156 id
, strerror(errno
));
161 if (!memcmp(tag
, info
.tag
, BPF_TAG_SIZE
))
168 int prog_parse_fd(int *argc
, char ***argv
)
172 if (is_prefix(**argv
, "id")) {
178 id
= strtoul(**argv
, &endptr
, 0);
180 p_err("can't parse %s as ID", **argv
);
185 fd
= bpf_prog_get_fd_by_id(id
);
187 p_err("get by id (%u): %s", id
, strerror(errno
));
189 } else if (is_prefix(**argv
, "tag")) {
190 unsigned char tag
[BPF_TAG_SIZE
];
194 if (sscanf(**argv
, BPF_TAG_FMT
, tag
, tag
+ 1, tag
+ 2,
195 tag
+ 3, tag
+ 4, tag
+ 5, tag
+ 6, tag
+ 7)
197 p_err("can't parse tag");
202 return prog_fd_by_tag(tag
);
203 } else if (is_prefix(**argv
, "pinned")) {
211 return open_obj_pinned_any(path
, BPF_OBJ_PROG
);
214 p_err("expected 'id', 'tag' or 'pinned', got: '%s'?", **argv
);
218 static void show_prog_maps(int fd
, u32 num_maps
)
220 struct bpf_prog_info info
= {};
221 __u32 len
= sizeof(info
);
222 __u32 map_ids
[num_maps
];
226 info
.nr_map_ids
= num_maps
;
227 info
.map_ids
= ptr_to_u64(map_ids
);
229 err
= bpf_obj_get_info_by_fd(fd
, &info
, &len
);
230 if (err
|| !info
.nr_map_ids
)
234 jsonw_name(json_wtr
, "map_ids");
235 jsonw_start_array(json_wtr
);
236 for (i
= 0; i
< info
.nr_map_ids
; i
++)
237 jsonw_uint(json_wtr
, map_ids
[i
]);
238 jsonw_end_array(json_wtr
);
241 for (i
= 0; i
< info
.nr_map_ids
; i
++)
242 printf("%u%s", map_ids
[i
],
243 i
== info
.nr_map_ids
- 1 ? "" : ",");
247 static void print_prog_json(struct bpf_prog_info
*info
, int fd
)
251 jsonw_start_object(json_wtr
);
252 jsonw_uint_field(json_wtr
, "id", info
->id
);
253 if (info
->type
< ARRAY_SIZE(prog_type_name
))
254 jsonw_string_field(json_wtr
, "type",
255 prog_type_name
[info
->type
]);
257 jsonw_uint_field(json_wtr
, "type", info
->type
);
260 jsonw_string_field(json_wtr
, "name", info
->name
);
262 jsonw_name(json_wtr
, "tag");
263 jsonw_printf(json_wtr
, "\"" BPF_TAG_FMT
"\"",
264 info
->tag
[0], info
->tag
[1], info
->tag
[2], info
->tag
[3],
265 info
->tag
[4], info
->tag
[5], info
->tag
[6], info
->tag
[7]);
267 jsonw_bool_field(json_wtr
, "gpl_compatible", info
->gpl_compatible
);
269 print_dev_json(info
->ifindex
, info
->netns_dev
, info
->netns_ino
);
271 if (info
->load_time
) {
274 print_boot_time(info
->load_time
, buf
, sizeof(buf
));
276 /* Piggy back on load_time, since 0 uid is a valid one */
277 jsonw_name(json_wtr
, "loaded_at");
278 jsonw_printf(json_wtr
, "%s", buf
);
279 jsonw_uint_field(json_wtr
, "uid", info
->created_by_uid
);
282 jsonw_uint_field(json_wtr
, "bytes_xlated", info
->xlated_prog_len
);
284 if (info
->jited_prog_len
) {
285 jsonw_bool_field(json_wtr
, "jited", true);
286 jsonw_uint_field(json_wtr
, "bytes_jited", info
->jited_prog_len
);
288 jsonw_bool_field(json_wtr
, "jited", false);
291 memlock
= get_fdinfo(fd
, "memlock");
293 jsonw_int_field(json_wtr
, "bytes_memlock", atoi(memlock
));
296 if (info
->nr_map_ids
)
297 show_prog_maps(fd
, info
->nr_map_ids
);
299 if (!hash_empty(prog_table
.table
)) {
300 struct pinned_obj
*obj
;
302 jsonw_name(json_wtr
, "pinned");
303 jsonw_start_array(json_wtr
);
304 hash_for_each_possible(prog_table
.table
, obj
, hash
, info
->id
) {
305 if (obj
->id
== info
->id
)
306 jsonw_string(json_wtr
, obj
->path
);
308 jsonw_end_array(json_wtr
);
311 jsonw_end_object(json_wtr
);
314 static void print_prog_plain(struct bpf_prog_info
*info
, int fd
)
318 printf("%u: ", info
->id
);
319 if (info
->type
< ARRAY_SIZE(prog_type_name
))
320 printf("%s ", prog_type_name
[info
->type
]);
322 printf("type %u ", info
->type
);
325 printf("name %s ", info
->name
);
328 fprint_hex(stdout
, info
->tag
, BPF_TAG_SIZE
, "");
329 print_dev_plain(info
->ifindex
, info
->netns_dev
, info
->netns_ino
);
330 printf("%s", info
->gpl_compatible
? " gpl" : "");
333 if (info
->load_time
) {
336 print_boot_time(info
->load_time
, buf
, sizeof(buf
));
338 /* Piggy back on load_time, since 0 uid is a valid one */
339 printf("\tloaded_at %s uid %u\n", buf
, info
->created_by_uid
);
342 printf("\txlated %uB", info
->xlated_prog_len
);
344 if (info
->jited_prog_len
)
345 printf(" jited %uB", info
->jited_prog_len
);
347 printf(" not jited");
349 memlock
= get_fdinfo(fd
, "memlock");
351 printf(" memlock %sB", memlock
);
354 if (info
->nr_map_ids
)
355 show_prog_maps(fd
, info
->nr_map_ids
);
357 if (!hash_empty(prog_table
.table
)) {
358 struct pinned_obj
*obj
;
360 hash_for_each_possible(prog_table
.table
, obj
, hash
, info
->id
) {
361 if (obj
->id
== info
->id
)
362 printf("\n\tpinned %s", obj
->path
);
369 static int show_prog(int fd
)
371 struct bpf_prog_info info
= {};
372 __u32 len
= sizeof(info
);
375 err
= bpf_obj_get_info_by_fd(fd
, &info
, &len
);
377 p_err("can't get prog info: %s", strerror(errno
));
382 print_prog_json(&info
, fd
);
384 print_prog_plain(&info
, fd
);
389 static int do_show(int argc
, char **argv
)
396 build_pinned_obj_table(&prog_table
, BPF_OBJ_PROG
);
399 fd
= prog_parse_fd(&argc
, &argv
);
403 return show_prog(fd
);
410 jsonw_start_array(json_wtr
);
412 err
= bpf_prog_get_next_id(id
, &id
);
414 if (errno
== ENOENT
) {
418 p_err("can't get next program: %s%s", strerror(errno
),
419 errno
== EINVAL
? " -- kernel too old?" : "");
424 fd
= bpf_prog_get_fd_by_id(id
);
428 p_err("can't get prog by id (%u): %s",
429 id
, strerror(errno
));
441 jsonw_end_array(json_wtr
);
446 static int do_dump(int argc
, char **argv
)
448 unsigned long *func_ksyms
= NULL
;
449 struct bpf_prog_info info
= {};
450 unsigned int *func_lens
= NULL
;
451 const char *disasm_opt
= NULL
;
452 unsigned int nr_func_ksyms
;
453 unsigned int nr_func_lens
;
454 struct dump_data dd
= {};
455 __u32 len
= sizeof(info
);
456 unsigned int buf_size
;
457 char *filepath
= NULL
;
458 bool opcodes
= false;
467 if (is_prefix(*argv
, "jited")) {
468 member_len
= &info
.jited_prog_len
;
469 member_ptr
= &info
.jited_prog_insns
;
470 } else if (is_prefix(*argv
, "xlated")) {
471 member_len
= &info
.xlated_prog_len
;
472 member_ptr
= &info
.xlated_prog_insns
;
474 p_err("expected 'xlated' or 'jited', got: %s", *argv
);
482 fd
= prog_parse_fd(&argc
, &argv
);
486 if (is_prefix(*argv
, "file")) {
489 p_err("expected file path");
495 } else if (is_prefix(*argv
, "opcodes")) {
498 } else if (is_prefix(*argv
, "visual")) {
508 err
= bpf_obj_get_info_by_fd(fd
, &info
, &len
);
510 p_err("can't get prog info: %s", strerror(errno
));
515 p_info("no instructions returned");
520 buf_size
= *member_len
;
522 buf
= malloc(buf_size
);
524 p_err("mem alloc failed");
529 nr_func_ksyms
= info
.nr_jited_ksyms
;
531 func_ksyms
= malloc(nr_func_ksyms
* sizeof(__u64
));
533 p_err("mem alloc failed");
539 nr_func_lens
= info
.nr_jited_func_lens
;
541 func_lens
= malloc(nr_func_lens
* sizeof(__u32
));
543 p_err("mem alloc failed");
549 memset(&info
, 0, sizeof(info
));
551 *member_ptr
= ptr_to_u64(buf
);
552 *member_len
= buf_size
;
553 info
.jited_ksyms
= ptr_to_u64(func_ksyms
);
554 info
.nr_jited_ksyms
= nr_func_ksyms
;
555 info
.jited_func_lens
= ptr_to_u64(func_lens
);
556 info
.nr_jited_func_lens
= nr_func_lens
;
558 err
= bpf_obj_get_info_by_fd(fd
, &info
, &len
);
561 p_err("can't get prog info: %s", strerror(errno
));
565 if (*member_len
> buf_size
) {
566 p_err("too many instructions returned");
570 if (info
.nr_jited_ksyms
> nr_func_ksyms
) {
571 p_err("too many addresses returned");
575 if (info
.nr_jited_func_lens
> nr_func_lens
) {
576 p_err("too many values returned");
580 if ((member_len
== &info
.jited_prog_len
&&
581 info
.jited_prog_insns
== 0) ||
582 (member_len
== &info
.xlated_prog_len
&&
583 info
.xlated_prog_insns
== 0)) {
584 p_err("error retrieving insn dump: kernel.kptr_restrict set?");
589 fd
= open(filepath
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
591 p_err("can't open file %s: %s", filepath
,
596 n
= write(fd
, buf
, *member_len
);
598 if (n
!= *member_len
) {
599 p_err("error writing output file: %s",
600 n
< 0 ? strerror(errno
) : "short write");
605 jsonw_null(json_wtr
);
606 } else if (member_len
== &info
.jited_prog_len
) {
607 const char *name
= NULL
;
610 name
= ifindex_to_bfd_params(info
.ifindex
,
618 if (info
.nr_jited_func_lens
&& info
.jited_func_lens
) {
619 struct kernel_sym
*sym
= NULL
;
620 char sym_name
[SYM_MAX_NAME
];
621 unsigned char *img
= buf
;
626 if (info
.nr_jited_ksyms
) {
627 kernel_syms_load(&dd
);
628 ksyms
= (__u64
*) info
.jited_ksyms
;
632 jsonw_start_array(json_wtr
);
634 lens
= (__u32
*) info
.jited_func_lens
;
635 for (i
= 0; i
< info
.nr_jited_func_lens
; i
++) {
637 sym
= kernel_syms_search(&dd
, ksyms
[i
]);
639 sprintf(sym_name
, "%s", sym
->name
);
641 sprintf(sym_name
, "0x%016llx", ksyms
[i
]);
643 strcpy(sym_name
, "unknown");
647 jsonw_start_object(json_wtr
);
648 jsonw_name(json_wtr
, "name");
649 jsonw_string(json_wtr
, sym_name
);
650 jsonw_name(json_wtr
, "insns");
652 printf("%s:\n", sym_name
);
655 disasm_print_insn(img
, lens
[i
], opcodes
, name
,
660 jsonw_end_object(json_wtr
);
666 jsonw_end_array(json_wtr
);
668 disasm_print_insn(buf
, *member_len
, opcodes
, name
,
673 jsonw_null(json_wtr
);
675 dump_xlated_cfg(buf
, *member_len
);
677 kernel_syms_load(&dd
);
678 dd
.nr_jited_ksyms
= info
.nr_jited_ksyms
;
679 dd
.jited_ksyms
= (__u64
*) info
.jited_ksyms
;
682 dump_xlated_json(&dd
, buf
, *member_len
, opcodes
);
684 dump_xlated_plain(&dd
, buf
, *member_len
, opcodes
);
685 kernel_syms_destroy(&dd
);
700 static int do_pin(int argc
, char **argv
)
704 err
= do_pin_any(argc
, argv
, bpf_prog_get_fd_by_id
);
705 if (!err
&& json_output
)
706 jsonw_null(json_wtr
);
716 int map_replace_compar(const void *p1
, const void *p2
)
718 const struct map_replace
*a
= p1
, *b
= p2
;
720 return a
->idx
- b
->idx
;
723 static int do_attach(int argc
, char **argv
)
725 enum bpf_attach_type attach_type
;
726 int err
, mapfd
, progfd
;
729 p_err("too few parameters for map attach");
733 progfd
= prog_parse_fd(&argc
, &argv
);
737 attach_type
= parse_attach_type(*argv
);
738 if (attach_type
== __MAX_BPF_ATTACH_TYPE
) {
739 p_err("invalid attach type");
744 mapfd
= map_parse_fd(&argc
, &argv
);
748 err
= bpf_prog_attach(progfd
, mapfd
, attach_type
, 0);
750 p_err("failed prog attach to map");
755 jsonw_null(json_wtr
);
759 static int do_detach(int argc
, char **argv
)
761 enum bpf_attach_type attach_type
;
762 int err
, mapfd
, progfd
;
765 p_err("too few parameters for map detach");
769 progfd
= prog_parse_fd(&argc
, &argv
);
773 attach_type
= parse_attach_type(*argv
);
774 if (attach_type
== __MAX_BPF_ATTACH_TYPE
) {
775 p_err("invalid attach type");
780 mapfd
= map_parse_fd(&argc
, &argv
);
784 err
= bpf_prog_detach2(progfd
, mapfd
, attach_type
);
786 p_err("failed prog detach from map");
791 jsonw_null(json_wtr
);
794 static int do_load(int argc
, char **argv
)
796 enum bpf_attach_type expected_attach_type
;
797 struct bpf_object_open_attr attr
= {
798 .prog_type
= BPF_PROG_TYPE_UNSPEC
,
800 struct map_replace
*map_replace
= NULL
;
801 unsigned int old_map_fds
= 0;
802 struct bpf_program
*prog
;
803 struct bpf_object
*obj
;
812 attr
.file
= GET_ARG();
816 if (is_prefix(*argv
, "type")) {
821 if (attr
.prog_type
!= BPF_PROG_TYPE_UNSPEC
) {
822 p_err("program type already specified");
823 goto err_free_reuse_maps
;
826 goto err_free_reuse_maps
;
828 /* Put a '/' at the end of type to appease libbpf */
829 type
= malloc(strlen(*argv
) + 2);
831 p_err("mem alloc failed");
832 goto err_free_reuse_maps
;
838 err
= libbpf_prog_type_by_name(type
, &attr
.prog_type
,
839 &expected_attach_type
);
842 p_err("unknown program type '%s'", *argv
);
843 goto err_free_reuse_maps
;
846 } else if (is_prefix(*argv
, "map")) {
847 void *new_map_replace
;
854 goto err_free_reuse_maps
;
856 if (is_prefix(*argv
, "idx")) {
859 idx
= strtoul(*argv
, &endptr
, 0);
861 p_err("can't parse %s as IDX", *argv
);
862 goto err_free_reuse_maps
;
865 } else if (is_prefix(*argv
, "name")) {
871 p_err("expected 'idx' or 'name', got: '%s'?",
873 goto err_free_reuse_maps
;
877 fd
= map_parse_fd(&argc
, &argv
);
879 goto err_free_reuse_maps
;
881 new_map_replace
= reallocarray(map_replace
,
883 sizeof(*map_replace
));
884 if (!new_map_replace
) {
885 p_err("mem alloc failed");
886 goto err_free_reuse_maps
;
888 map_replace
= new_map_replace
;
890 map_replace
[old_map_fds
].idx
= idx
;
891 map_replace
[old_map_fds
].name
= name
;
892 map_replace
[old_map_fds
].fd
= fd
;
894 } else if (is_prefix(*argv
, "dev")) {
898 p_err("offload device already specified");
899 goto err_free_reuse_maps
;
902 goto err_free_reuse_maps
;
904 ifindex
= if_nametoindex(*argv
);
906 p_err("unrecognized netdevice '%s': %s",
907 *argv
, strerror(errno
));
908 goto err_free_reuse_maps
;
912 p_err("expected no more arguments, 'type', 'map' or 'dev', got: '%s'?",
914 goto err_free_reuse_maps
;
918 obj
= __bpf_object__open_xattr(&attr
, bpf_flags
);
919 if (IS_ERR_OR_NULL(obj
)) {
920 p_err("failed to open object file");
921 goto err_free_reuse_maps
;
924 prog
= bpf_program__next(NULL
, obj
);
926 p_err("object file doesn't contain any bpf program");
930 bpf_program__set_ifindex(prog
, ifindex
);
931 if (attr
.prog_type
== BPF_PROG_TYPE_UNSPEC
) {
932 const char *sec_name
= bpf_program__title(prog
, false);
934 err
= libbpf_prog_type_by_name(sec_name
, &attr
.prog_type
,
935 &expected_attach_type
);
937 p_err("failed to guess program type based on section name %s\n",
942 bpf_program__set_type(prog
, attr
.prog_type
);
943 bpf_program__set_expected_attach_type(prog
, expected_attach_type
);
945 qsort(map_replace
, old_map_fds
, sizeof(*map_replace
),
948 /* After the sort maps by name will be first on the list, because they
949 * have idx == -1. Resolve them.
952 while (j
< old_map_fds
&& map_replace
[j
].name
) {
954 bpf_map__for_each(map
, obj
) {
955 if (!strcmp(bpf_map__name(map
), map_replace
[j
].name
)) {
956 map_replace
[j
].idx
= i
;
961 if (map_replace
[j
].idx
== -1) {
962 p_err("unable to find map '%s'", map_replace
[j
].name
);
967 /* Resort if any names were resolved */
969 qsort(map_replace
, old_map_fds
, sizeof(*map_replace
),
972 /* Set ifindex and name reuse */
975 bpf_map__for_each(map
, obj
) {
976 if (!bpf_map__is_offload_neutral(map
))
977 bpf_map__set_ifindex(map
, ifindex
);
979 if (j
< old_map_fds
&& idx
== map_replace
[j
].idx
) {
980 err
= bpf_map__reuse_fd(map
, map_replace
[j
++].fd
);
982 p_err("unable to set up map reuse: %d", err
);
986 /* Next reuse wants to apply to the same map */
987 if (j
< old_map_fds
&& map_replace
[j
].idx
== idx
) {
988 p_err("replacement for map idx %d specified more than once",
996 if (j
< old_map_fds
) {
997 p_err("map idx '%d' not used", map_replace
[j
].idx
);
1001 err
= bpf_object__load(obj
);
1003 p_err("failed to load object file");
1007 if (do_pin_fd(bpf_program__fd(prog
), pinfile
))
1011 jsonw_null(json_wtr
);
1013 bpf_object__close(obj
);
1014 for (i
= 0; i
< old_map_fds
; i
++)
1015 close(map_replace
[i
].fd
);
1021 bpf_object__close(obj
);
1022 err_free_reuse_maps
:
1023 for (i
= 0; i
< old_map_fds
; i
++)
1024 close(map_replace
[i
].fd
);
1029 static int do_help(int argc
, char **argv
)
1032 jsonw_null(json_wtr
);
1037 "Usage: %s %s { show | list } [PROG]\n"
1038 " %s %s dump xlated PROG [{ file FILE | opcodes | visual }]\n"
1039 " %s %s dump jited PROG [{ file FILE | opcodes }]\n"
1040 " %s %s pin PROG FILE\n"
1041 " %s %s load OBJ FILE [type TYPE] [dev NAME] \\\n"
1042 " [map { idx IDX | name NAME } MAP]\n"
1043 " %s %s attach PROG ATTACH_TYPE MAP\n"
1044 " %s %s detach PROG ATTACH_TYPE MAP\n"
1047 " " HELP_SPEC_MAP
"\n"
1048 " " HELP_SPEC_PROGRAM
"\n"
1049 " TYPE := { socket | kprobe | kretprobe | classifier | action |\n"
1050 " tracepoint | raw_tracepoint | xdp | perf_event | cgroup/skb |\n"
1051 " cgroup/sock | cgroup/dev | lwt_in | lwt_out | lwt_xmit |\n"
1052 " lwt_seg6local | sockops | sk_skb | sk_msg | lirc_mode2 |\n"
1053 " cgroup/bind4 | cgroup/bind6 | cgroup/post_bind4 |\n"
1054 " cgroup/post_bind6 | cgroup/connect4 | cgroup/connect6 |\n"
1055 " cgroup/sendmsg4 | cgroup/sendmsg6 }\n"
1056 " ATTACH_TYPE := { msg_verdict | skb_verdict | skb_parse }\n"
1057 " " HELP_SPEC_OPTIONS
"\n"
1059 bin_name
, argv
[-2], bin_name
, argv
[-2], bin_name
, argv
[-2],
1060 bin_name
, argv
[-2], bin_name
, argv
[-2], bin_name
, argv
[-2],
1061 bin_name
, argv
[-2], bin_name
, argv
[-2]);
1066 static const struct cmd cmds
[] = {
1067 { "show", do_show
},
1068 { "list", do_show
},
1069 { "help", do_help
},
1070 { "dump", do_dump
},
1072 { "load", do_load
},
1073 { "attach", do_attach
},
1074 { "detach", do_detach
},
1078 int do_prog(int argc
, char **argv
)
1080 return cmd_select(cmds
, argc
, argv
, do_help
);