]>
Commit | Line | Data |
---|---|---|
02ff58dc JK |
1 | /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */ |
2 | /* Copyright (C) 2017-2018 Netronome Systems, Inc. */ | |
71bb428f | 3 | |
71bb428f JK |
4 | #ifndef __BPF_TOOL_H |
5 | #define __BPF_TOOL_H | |
6 | ||
c9c35995 JK |
7 | /* BFD and kernel.h both define GCC_VERSION, differently */ |
8 | #undef GCC_VERSION | |
71bb428f JK |
9 | #include <stdbool.h> |
10 | #include <stdio.h> | |
11 | #include <linux/bpf.h> | |
7868620a | 12 | #include <linux/compiler.h> |
c9c35995 | 13 | #include <linux/kernel.h> |
4990f1f4 | 14 | #include <linux/hashtable.h> |
531b014e | 15 | #include <tools/libc_compat.h> |
71bb428f | 16 | |
d35efba9 QM |
17 | #include "json_writer.h" |
18 | ||
71bb428f JK |
19 | #define ptr_to_u64(ptr) ((__u64)(unsigned long)(ptr)) |
20 | ||
71bb428f JK |
21 | #define NEXT_ARG() ({ argc--; argv++; if (argc < 0) usage(); }) |
22 | #define NEXT_ARGP() ({ (*argc)--; (*argv)++; if (*argc < 0) usage(); }) | |
0d954eeb | 23 | #define BAD_ARG() ({ p_err("what is '%s'?", *argv); -1; }) |
8d1fc3de JK |
24 | #define GET_ARG() ({ argc--; *argv++; }) |
25 | #define REQ_ARGS(cnt) \ | |
26 | ({ \ | |
27 | int _cnt = (cnt); \ | |
28 | bool _res; \ | |
29 | \ | |
30 | if (argc < _cnt) { \ | |
31 | p_err("'%s' needs at least %d arguments, %d found", \ | |
32 | argv[-1], _cnt, argc); \ | |
33 | _res = false; \ | |
34 | } else { \ | |
35 | _res = true; \ | |
36 | } \ | |
37 | _res; \ | |
38 | }) | |
71bb428f | 39 | |
3fc27b71 QM |
40 | #define ERR_MAX_LEN 1024 |
41 | ||
2dc7c1fe | 42 | #define BPF_TAG_FMT "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx" |
71bb428f JK |
43 | |
44 | #define HELP_SPEC_PROGRAM \ | |
45 | "PROG := { id PROG_ID | pinned FILE | tag PROG_TAG }" | |
0641c3c8 | 46 | #define HELP_SPEC_OPTIONS \ |
33221307 QM |
47 | "OPTIONS := { {-j|--json} [{-p|--pretty}] | {-f|--bpffs} |\n" \ |
48 | "\t {-m|--mapcompat} | {-n|--nomount} }" | |
3ff5a4dc JK |
49 | #define HELP_SPEC_MAP \ |
50 | "MAP := { id MAP_ID | pinned FILE }" | |
71bb428f | 51 | |
99a44bef QM |
52 | static const char * const prog_type_name[] = { |
53 | [BPF_PROG_TYPE_UNSPEC] = "unspec", | |
54 | [BPF_PROG_TYPE_SOCKET_FILTER] = "socket_filter", | |
55 | [BPF_PROG_TYPE_KPROBE] = "kprobe", | |
56 | [BPF_PROG_TYPE_SCHED_CLS] = "sched_cls", | |
57 | [BPF_PROG_TYPE_SCHED_ACT] = "sched_act", | |
58 | [BPF_PROG_TYPE_TRACEPOINT] = "tracepoint", | |
59 | [BPF_PROG_TYPE_XDP] = "xdp", | |
60 | [BPF_PROG_TYPE_PERF_EVENT] = "perf_event", | |
61 | [BPF_PROG_TYPE_CGROUP_SKB] = "cgroup_skb", | |
62 | [BPF_PROG_TYPE_CGROUP_SOCK] = "cgroup_sock", | |
63 | [BPF_PROG_TYPE_LWT_IN] = "lwt_in", | |
64 | [BPF_PROG_TYPE_LWT_OUT] = "lwt_out", | |
65 | [BPF_PROG_TYPE_LWT_XMIT] = "lwt_xmit", | |
66 | [BPF_PROG_TYPE_SOCK_OPS] = "sock_ops", | |
67 | [BPF_PROG_TYPE_SK_SKB] = "sk_skb", | |
68 | [BPF_PROG_TYPE_CGROUP_DEVICE] = "cgroup_device", | |
69 | [BPF_PROG_TYPE_SK_MSG] = "sk_msg", | |
70 | [BPF_PROG_TYPE_RAW_TRACEPOINT] = "raw_tracepoint", | |
71 | [BPF_PROG_TYPE_CGROUP_SOCK_ADDR] = "cgroup_sock_addr", | |
72 | [BPF_PROG_TYPE_LWT_SEG6LOCAL] = "lwt_seg6local", | |
73 | [BPF_PROG_TYPE_LIRC_MODE2] = "lirc_mode2", | |
74 | [BPF_PROG_TYPE_SK_REUSEPORT] = "sk_reuseport", | |
75 | [BPF_PROG_TYPE_FLOW_DISSECTOR] = "flow_dissector", | |
f25377ee | 76 | [BPF_PROG_TYPE_CGROUP_SYSCTL] = "cgroup_sysctl", |
216b65fb | 77 | [BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE] = "raw_tracepoint_writable", |
f6d08d9d | 78 | [BPF_PROG_TYPE_CGROUP_SOCKOPT] = "cgroup_sockopt", |
99a44bef QM |
79 | }; |
80 | ||
f99e1663 QM |
81 | extern const char * const map_type_name[]; |
82 | extern const size_t map_type_name_size; | |
83 | ||
71bb428f JK |
84 | enum bpf_obj_type { |
85 | BPF_OBJ_UNKNOWN, | |
86 | BPF_OBJ_PROG, | |
87 | BPF_OBJ_MAP, | |
88 | }; | |
89 | ||
90 | extern const char *bin_name; | |
91 | ||
d35efba9 QM |
92 | extern json_writer_t *json_wtr; |
93 | extern bool json_output; | |
c541b734 | 94 | extern bool show_pinned; |
33221307 | 95 | extern bool block_mount; |
55d77807 | 96 | extern bool verifier_logs; |
32e3e58e | 97 | extern bool relaxed_maps; |
4990f1f4 PB |
98 | extern struct pinned_obj_table prog_table; |
99 | extern struct pinned_obj_table map_table; | |
d35efba9 | 100 | |
8918dc42 QM |
101 | void __printf(1, 2) p_err(const char *fmt, ...); |
102 | void __printf(1, 2) p_info(const char *fmt, ...); | |
0b1c27db | 103 | |
71bb428f | 104 | bool is_prefix(const char *pfx, const char *str); |
ba95c745 | 105 | int detect_common_prefix(const char *arg, ...); |
9cbe1f58 | 106 | void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep); |
7868620a | 107 | void usage(void) __noreturn; |
71bb428f | 108 | |
8302b9bd QM |
109 | void set_max_rlimit(void); |
110 | ||
be3245e2 QM |
111 | int mount_tracefs(const char *target); |
112 | ||
4990f1f4 PB |
113 | struct pinned_obj_table { |
114 | DECLARE_HASHTABLE(table, 16); | |
115 | }; | |
116 | ||
117 | struct pinned_obj { | |
118 | __u32 id; | |
119 | char *path; | |
120 | struct hlist_node hash; | |
121 | }; | |
122 | ||
b053b439 MKL |
123 | struct btf; |
124 | struct bpf_line_info; | |
125 | ||
4990f1f4 PB |
126 | int build_pinned_obj_table(struct pinned_obj_table *table, |
127 | enum bpf_obj_type type); | |
128 | void delete_pinned_obj_table(struct pinned_obj_table *tab); | |
52262210 JK |
129 | void print_dev_plain(__u32 ifindex, __u64 ns_dev, __u64 ns_inode); |
130 | void print_dev_json(__u32 ifindex, __u64 ns_dev, __u64 ns_inode); | |
4990f1f4 | 131 | |
71bb428f JK |
132 | struct cmd { |
133 | const char *cmd; | |
134 | int (*func)(int argc, char **argv); | |
135 | }; | |
136 | ||
137 | int cmd_select(const struct cmd *cmds, int argc, char **argv, | |
138 | int (*help)(int argc, char **argv)); | |
139 | ||
140 | int get_fd_type(int fd); | |
141 | const char *get_fd_type_name(enum bpf_obj_type type); | |
142 | char *get_fdinfo(int fd, const char *key); | |
f120919f | 143 | int open_obj_pinned(char *path, bool quiet); |
71bb428f | 144 | int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type); |
77380998 | 145 | int mount_bpffs_for_pin(const char *name); |
71bb428f | 146 | int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32)); |
49a086c2 | 147 | int do_pin_fd(int fd, const char *name); |
71bb428f JK |
148 | |
149 | int do_prog(int argc, char **arg); | |
150 | int do_map(int argc, char **arg); | |
f412eed9 | 151 | int do_event_pipe(int argc, char **argv); |
5ccda64d | 152 | int do_cgroup(int argc, char **arg); |
b04df400 | 153 | int do_perf(int argc, char **arg); |
f6f3bac0 | 154 | int do_net(int argc, char **arg); |
30da46b5 | 155 | int do_tracelog(int argc, char **arg); |
49eb7ab3 | 156 | int do_feature(int argc, char **argv); |
c93cc690 | 157 | int do_btf(int argc, char **argv); |
71bb428f | 158 | |
0b592b5a | 159 | int parse_u32_arg(int *argc, char ***argv, __u32 *val, const char *what); |
71bb428f | 160 | int prog_parse_fd(int *argc, char ***argv); |
3ff5a4dc | 161 | int map_parse_fd(int *argc, char ***argv); |
f412eed9 | 162 | int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len); |
71bb428f | 163 | |
b053b439 | 164 | struct bpf_prog_linfo; |
8c03ecf7 | 165 | #ifdef HAVE_LIBBFD_SUPPORT |
e6593596 | 166 | void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, |
b053b439 MKL |
167 | const char *arch, const char *disassembler_options, |
168 | const struct btf *btf, | |
169 | const struct bpf_prog_linfo *prog_linfo, | |
170 | __u64 func_ksym, unsigned int func_idx, | |
171 | bool linum); | |
29a9c10e SF |
172 | int disasm_init(void); |
173 | #else | |
174 | static inline | |
175 | void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, | |
b053b439 MKL |
176 | const char *arch, const char *disassembler_options, |
177 | const struct btf *btf, | |
178 | const struct bpf_prog_linfo *prog_linfo, | |
179 | __u64 func_ksym, unsigned int func_idx, | |
180 | bool linum) | |
29a9c10e SF |
181 | { |
182 | } | |
183 | static inline int disasm_init(void) | |
184 | { | |
185 | p_err("No libbfd support"); | |
186 | return -1; | |
187 | } | |
188 | #endif | |
f412eed9 | 189 | void print_data_json(uint8_t *data, size_t len); |
f05e2c32 | 190 | void print_hex_data_json(uint8_t *data, size_t len); |
71bb428f | 191 | |
f412eed9 | 192 | unsigned int get_page_size(void); |
e64d5256 | 193 | unsigned int get_possible_cpus(void); |
3ddeac67 JK |
194 | const char * |
195 | ifindex_to_bfd_params(__u32 ifindex, __u64 ns_dev, __u64 ns_ino, | |
196 | const char **opt); | |
e6593596 | 197 | |
b12d6ec0 OK |
198 | struct btf_dumper { |
199 | const struct btf *btf; | |
200 | json_writer_t *jw; | |
201 | bool is_plain_text; | |
202 | }; | |
203 | ||
204 | /* btf_dumper_type - print data along with type information | |
205 | * @d: an instance containing context for dumping types | |
206 | * @type_id: index in btf->types array. this points to the type to be dumped | |
207 | * @data: pointer the actual data, i.e. the values to be printed | |
208 | * | |
209 | * Returns zero on success and negative error code otherwise | |
210 | */ | |
211 | int btf_dumper_type(const struct btf_dumper *d, __u32 type_id, | |
212 | const void *data); | |
254471e5 YS |
213 | void btf_dumper_type_only(const struct btf *btf, __u32 func_type_id, |
214 | char *func_only, int size); | |
f6f3bac0 | 215 | |
b053b439 MKL |
216 | void btf_dump_linfo_plain(const struct btf *btf, |
217 | const struct bpf_line_info *linfo, | |
218 | const char *prefix, bool linum); | |
219 | void btf_dump_linfo_json(const struct btf *btf, | |
220 | const struct bpf_line_info *linfo, bool linum); | |
221 | ||
f6f3bac0 YS |
222 | struct nlattr; |
223 | struct ifinfomsg; | |
224 | struct tcmsg; | |
225 | int do_xdp_dump(struct ifinfomsg *ifinfo, struct nlattr **tb); | |
7900efc1 YS |
226 | int do_filter_dump(struct tcmsg *ifinfo, struct nlattr **tb, const char *kind, |
227 | const char *devname, int ifindex); | |
71bb428f | 228 | #endif |