1 /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
3
4 #ifndef __BPF_TOOL_H
5 #define __BPF_TOOL_H
6
7 /* BFD and kernel.h both define GCC_VERSION, differently */
8 #undef GCC_VERSION
9 #ifndef _GNU_SOURCE
10 #define _GNU_SOURCE
11 #endif
12 #include <stdbool.h>
13 #include <stdio.h>
14 #include <errno.h>
15 #include <stdlib.h>
16 #include <bpf/skel_internal.h>
17 #include <linux/bpf.h>
18 #include <linux/compiler.h>
19 #include <linux/kernel.h>
20
21 #include <bpf/hashmap.h>
22 #include <bpf/libbpf.h>
23 #include <bpf/bpf.h>
24
25 #include "json_writer.h"
26
27 /* Make sure we do not use kernel-only integer typedefs */
28 #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
29
ptr_to_u64(const void * ptr)30 static inline __u64 ptr_to_u64(const void *ptr)
31 {
32 return (__u64)(unsigned long)ptr;
33 }
34
u64_to_ptr(__u64 ptr)35 static inline void *u64_to_ptr(__u64 ptr)
36 {
37 return (void *)(unsigned long)ptr;
38 }
39
40 #define NEXT_ARG() ({ argc--; argv++; if (argc < 0) usage(); })
41 #define NEXT_ARGP() ({ (*argc)--; (*argv)++; if (*argc < 0) usage(); })
42 #define BAD_ARG() ({ p_err("what is '%s'?", *argv); -1; })
43 #define GET_ARG() ({ argc--; *argv++; })
44 #define REQ_ARGS(cnt) \
45 ({ \
46 int _cnt = (cnt); \
47 bool _res; \
48 \
49 if (argc < _cnt) { \
50 p_err("'%s' needs at least %d arguments, %d found", \
51 argv[-1], _cnt, argc); \
52 _res = false; \
53 } else { \
54 _res = true; \
55 } \
56 _res; \
57 })
58
59 #define ERR_MAX_LEN 1024
60 #define MAX_SIG_SIZE 4096
61
62 #define BPF_TAG_FMT "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
63
64 #define HELP_SPEC_PROGRAM \
65 "PROG := { id PROG_ID | pinned FILE | tag PROG_TAG | name PROG_NAME }"
66 #define HELP_SPEC_OPTIONS \
67 "OPTIONS := { {-j|--json} [{-p|--pretty}] | {-d|--debug}"
68 #define HELP_SPEC_MAP \
69 "MAP := { id MAP_ID | pinned FILE | name MAP_NAME }"
70 #define HELP_SPEC_LINK \
71 "LINK := { id LINK_ID | pinned FILE }"
72
73 /* keep in sync with the definition in skeleton/pid_iter.bpf.c */
74 enum bpf_obj_type {
75 BPF_OBJ_UNKNOWN,
76 BPF_OBJ_PROG,
77 BPF_OBJ_MAP,
78 BPF_OBJ_LINK,
79 BPF_OBJ_BTF,
80 };
81
82 extern const char *bin_name;
83
84 extern json_writer_t *json_wtr;
85 extern bool json_output;
86 extern bool show_pinned;
87 extern bool show_pids;
88 extern bool block_mount;
89 extern bool verifier_logs;
90 extern bool relaxed_maps;
91 extern bool use_loader;
92 extern struct btf *base_btf;
93 extern struct hashmap *refs_table;
94 extern bool sign_progs;
95 extern const char *private_key_path;
96 extern const char *cert_path;
97
98 void __printf(1, 2) p_err(const char *fmt, ...);
99 void __printf(1, 2) p_info(const char *fmt, ...);
100
101 bool is_prefix(const char *pfx, const char *str);
102 int detect_common_prefix(const char *arg, ...);
103 void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep);
104 void usage(void) __noreturn;
105
106 void set_max_rlimit(void);
107
108 int mount_tracefs(const char *target);
109
110 struct obj_ref {
111 int pid;
112 char comm[16];
113 };
114
115 struct obj_refs {
116 int ref_cnt;
117 bool has_bpf_cookie;
118 struct obj_ref *refs;
119 __u64 bpf_cookie;
120 };
121
122 struct btf;
123 struct bpf_line_info;
124
125 int build_pinned_obj_table(struct hashmap *table,
126 enum bpf_obj_type type);
127 void delete_pinned_obj_table(struct hashmap *table);
128 __weak int build_obj_refs_table(struct hashmap **table,
129 enum bpf_obj_type type);
130 __weak void delete_obj_refs_table(struct hashmap *table);
131 __weak void emit_obj_refs_json(struct hashmap *table, __u32 id,
132 json_writer_t *json_wtr);
133 __weak void emit_obj_refs_plain(struct hashmap *table, __u32 id,
134 const char *prefix);
135 void print_dev_plain(__u32 ifindex, __u64 ns_dev, __u64 ns_inode);
136 void print_dev_json(__u32 ifindex, __u64 ns_dev, __u64 ns_inode);
137
138 struct cmd {
139 const char *cmd;
140 int (*func)(int argc, char **argv);
141 };
142
143 int cmd_select(const struct cmd *cmds, int argc, char **argv,
144 int (*help)(int argc, char **argv));
145
146 #define MAX_PROG_FULL_NAME 128
147 void get_prog_full_name(const struct bpf_prog_info *prog_info, int prog_fd,
148 char *name_buff, size_t buff_len);
149
150 int get_fd_type(int fd);
151 const char *get_fd_type_name(enum bpf_obj_type type);
152 char *get_fdinfo(int fd, const char *key);
153 int open_obj_pinned(const char *path, bool quiet,
154 const struct bpf_obj_get_opts *opts);
155 int open_obj_pinned_any(const char *path, enum bpf_obj_type exp_type,
156 const struct bpf_obj_get_opts *opts);
157 int mount_bpffs_for_file(const char *file_name);
158 int create_and_mount_bpffs_dir(const char *dir_name);
159 int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(int *, char ***));
160 int do_pin_fd(int fd, const char *name);
161
162 /* commands available in bootstrap mode */
163 int do_gen(int argc, char **argv);
164 int do_btf(int argc, char **argv);
165
166 /* non-bootstrap only commands */
167 int do_prog(int argc, char **arg) __weak;
168 int do_map(int argc, char **arg) __weak;
169 int do_link(int argc, char **arg) __weak;
170 int do_event_pipe(int argc, char **argv) __weak;
171 int do_cgroup(int argc, char **arg) __weak;
172 int do_perf(int argc, char **arg) __weak;
173 int do_net(int argc, char **arg) __weak;
174 int do_tracelog(int argc, char **arg) __weak;
175 int do_feature(int argc, char **argv) __weak;
176 int do_struct_ops(int argc, char **argv) __weak;
177 int do_iter(int argc, char **argv) __weak;
178 int do_token(int argc, char **argv) __weak;
179
180 int parse_u32_arg(int *argc, char ***argv, __u32 *val, const char *what);
181 int prog_parse_fd(int *argc, char ***argv);
182 int prog_parse_fds(int *argc, char ***argv, int **fds);
183 int map_parse_fd(int *argc, char ***argv, __u32 open_flags);
184 int map_parse_fds(int *argc, char ***argv, int **fds, __u32 open_flags);
185 int map_parse_fd_and_info(int *argc, char ***argv, struct bpf_map_info *info,
186 __u32 *info_len, __u32 open_flags);
187
188 struct bpf_prog_linfo;
189 #if defined(HAVE_LLVM_SUPPORT) || defined(HAVE_LIBBFD_SUPPORT)
190 int disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
191 const char *arch, const char *disassembler_options,
192 const struct btf *btf,
193 const struct bpf_prog_linfo *prog_linfo,
194 __u64 func_ksym, unsigned int func_idx,
195 bool linum);
196 int disasm_init(void);
197 #else
198 static inline
disasm_print_insn(unsigned char * image,ssize_t len,int opcodes,const char * arch,const char * disassembler_options,const struct btf * btf,const struct bpf_prog_linfo * prog_linfo,__u64 func_ksym,unsigned int func_idx,bool linum)199 int disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
200 const char *arch, const char *disassembler_options,
201 const struct btf *btf,
202 const struct bpf_prog_linfo *prog_linfo,
203 __u64 func_ksym, unsigned int func_idx,
204 bool linum)
205 {
206 return 0;
207 }
disasm_init(void)208 static inline int disasm_init(void)
209 {
210 p_err("No JIT disassembly support");
211 return -1;
212 }
213 #endif
214 void print_data_json(uint8_t *data, size_t len);
215 void print_hex_data_json(uint8_t *data, size_t len);
216
217 unsigned int get_page_size(void);
218 unsigned int get_possible_cpus(void);
219 const char *
220 ifindex_to_arch(__u32 ifindex, __u64 ns_dev, __u64 ns_ino, const char **opt);
221
222 struct btf_dumper {
223 const struct btf *btf;
224 json_writer_t *jw;
225 bool is_plain_text;
226 bool prog_id_as_func_ptr;
227 };
228
229 /* btf_dumper_type - print data along with type information
230 * @d: an instance containing context for dumping types
231 * @type_id: index in btf->types array. this points to the type to be dumped
232 * @data: pointer the actual data, i.e. the values to be printed
233 *
234 * Returns zero on success and negative error code otherwise
235 */
236 int btf_dumper_type(const struct btf_dumper *d, __u32 type_id,
237 const void *data);
238 void btf_dumper_type_only(const struct btf *btf, __u32 func_type_id,
239 char *func_only, int size);
240
241 void btf_dump_linfo_plain(const struct btf *btf,
242 const struct bpf_line_info *linfo,
243 const char *prefix, bool linum);
244 void btf_dump_linfo_json(const struct btf *btf,
245 const struct bpf_line_info *linfo, bool linum);
246 void btf_dump_linfo_dotlabel(const struct btf *btf,
247 const struct bpf_line_info *linfo, bool linum);
248
249 struct nlattr;
250 struct ifinfomsg;
251 struct tcmsg;
252 int do_xdp_dump(struct ifinfomsg *ifinfo, struct nlattr **tb);
253 int do_filter_dump(struct tcmsg *ifinfo, struct nlattr **tb, const char *kind,
254 const char *devname, int ifindex);
255
256 int print_all_levels(__maybe_unused enum libbpf_print_level level,
257 const char *format, va_list args);
258
259 size_t hash_fn_for_key_as_id(long key, void *ctx);
260 bool equal_fn_for_key_as_id(long k1, long k2, void *ctx);
261
262 /* bpf_attach_type_input_str - convert the provided attach type value into a
263 * textual representation that we accept for input purposes.
264 *
265 * This function is similar in nature to libbpf_bpf_attach_type_str, but
266 * recognizes some attach type names that have been used by the program in the
267 * past and which do not follow the string inference scheme that libbpf uses.
268 * These textual representations should only be used for user input.
269 *
270 * @t: The attach type
271 * Returns a pointer to a static string identifying the attach type. NULL is
272 * returned for unknown bpf_attach_type values.
273 */
274 const char *bpf_attach_type_input_str(enum bpf_attach_type t);
275
hashmap__empty(struct hashmap * map)276 static inline bool hashmap__empty(struct hashmap *map)
277 {
278 return map ? hashmap__size(map) == 0 : true;
279 }
280
281 int pathname_concat(char *buf, int buf_sz, const char *path,
282 const char *name);
283
284 /* print netfilter bpf_link info */
285 void netfilter_dump_plain(const struct bpf_link_info *info);
286 void netfilter_dump_json(const struct bpf_link_info *info, json_writer_t *wtr);
287
288 struct kernel_config_option {
289 const char *name;
290 bool macro_dump;
291 };
292
293 int read_kernel_config(const struct kernel_config_option *requested_options,
294 size_t num_options, char **out_values,
295 const char *define_prefix);
296 int bpftool_prog_sign(struct bpf_load_and_run_opts *opts);
297 __u32 register_session_key(const char *key_der_path);
298 #endif
299