xref: /linux/tools/bpf/bpftool/main.h (revision a8471eb0747f083ae5bde949e667d3d799d65cf7)
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 
30 static inline __u64 ptr_to_u64(const void *ptr)
31 {
32 	return (__u64)(unsigned long)ptr;
33 }
34 
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	16384
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 bool wait_output;
93 extern struct btf *base_btf;
94 extern struct hashmap *refs_table;
95 extern bool sign_progs;
96 extern const char *private_key_path;
97 extern const char *cert_path;
98 
99 void __printf(1, 2) p_err(const char *fmt, ...);
100 void __printf(1, 2) p_info(const char *fmt, ...);
101 const char *bpf_strerror(int err);
102 
103 bool is_prefix(const char *pfx, const char *str);
104 int detect_common_prefix(const char *arg, ...);
105 void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep);
106 void usage(void) __noreturn;
107 
108 void set_max_rlimit(void);
109 
110 int mount_tracefs(const char *target);
111 
112 struct obj_ref {
113 	int pid;
114 	char comm[16];
115 };
116 
117 struct obj_refs {
118 	int ref_cnt;
119 	bool has_bpf_cookie;
120 	struct obj_ref *refs;
121 	__u64 bpf_cookie;
122 };
123 
124 struct btf;
125 struct btf_var_secinfo;
126 struct bpf_line_info;
127 
128 int build_pinned_obj_table(struct hashmap *table,
129 			   enum bpf_obj_type type);
130 void delete_pinned_obj_table(struct hashmap *table);
131 __weak int build_obj_refs_table(struct hashmap **table,
132 				enum bpf_obj_type type);
133 __weak void delete_obj_refs_table(struct hashmap *table);
134 __weak void emit_obj_refs_json(struct hashmap *table, __u32 id,
135 			       json_writer_t *json_wtr);
136 __weak void emit_obj_refs_plain(struct hashmap *table, __u32 id,
137 				const char *prefix);
138 void print_dev_plain(__u32 ifindex, __u64 ns_dev, __u64 ns_inode);
139 void print_dev_json(__u32 ifindex, __u64 ns_dev, __u64 ns_inode);
140 
141 struct cmd {
142 	const char *cmd;
143 	int (*func)(int argc, char **argv);
144 };
145 
146 int cmd_select(const struct cmd *cmds, int argc, char **argv,
147 	       int (*help)(int argc, char **argv));
148 
149 #define MAX_PROG_FULL_NAME 128
150 void get_prog_full_name(const struct bpf_prog_info *prog_info, int prog_fd,
151 			char *name_buff, size_t buff_len);
152 
153 int get_fd_type(int fd);
154 const char *get_fd_type_name(enum bpf_obj_type type);
155 char *get_fdinfo(int fd, const char *key);
156 int open_obj_pinned(const char *path, bool quiet,
157 		    const struct bpf_obj_get_opts *opts);
158 int open_obj_pinned_any(const char *path, enum bpf_obj_type exp_type,
159 			const struct bpf_obj_get_opts *opts);
160 int mount_bpffs_for_file(const char *file_name);
161 int create_and_mount_bpffs_dir(const char *dir_name);
162 int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(int *, char ***));
163 int do_pin_fd(int fd, const char *name);
164 
165 /* commands available in bootstrap mode */
166 int do_gen(int argc, char **argv);
167 int do_btf(int argc, char **argv);
168 
169 /* non-bootstrap only commands */
170 int do_prog(int argc, char **arg) __weak;
171 int do_map(int argc, char **arg) __weak;
172 int do_link(int argc, char **arg) __weak;
173 int do_event_pipe(int argc, char **argv) __weak;
174 int do_cgroup(int argc, char **arg) __weak;
175 int do_perf(int argc, char **arg) __weak;
176 int do_net(int argc, char **arg) __weak;
177 int do_tracelog(int argc, char **arg) __weak;
178 int do_feature(int argc, char **argv) __weak;
179 int do_struct_ops(int argc, char **argv) __weak;
180 int do_iter(int argc, char **argv) __weak;
181 int do_token(int argc, char **argv) __weak;
182 
183 int parse_u32_arg(int *argc, char ***argv, __u32 *val, const char *what);
184 int prog_parse_fd(int *argc, char ***argv);
185 int prog_parse_fds(int *argc, char ***argv, int **fds);
186 int map_parse_fd(int *argc, char ***argv, __u32 open_flags);
187 int map_parse_fds(int *argc, char ***argv, int **fds, __u32 open_flags);
188 int map_parse_fd_and_info(int *argc, char ***argv, struct bpf_map_info *info,
189 			  __u32 *info_len, __u32 open_flags);
190 
191 struct bpf_prog_linfo;
192 #if defined(HAVE_LLVM_SUPPORT) || defined(HAVE_LIBBFD_SUPPORT)
193 int disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
194 		      const char *arch, const char *disassembler_options,
195 		      const struct btf *btf,
196 		      const struct bpf_prog_linfo *prog_linfo,
197 		      __u64 func_ksym, unsigned int func_idx,
198 		      bool linum);
199 int disasm_init(void);
200 #else
201 static inline
202 int disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
203 		      const char *arch, const char *disassembler_options,
204 		      const struct btf *btf,
205 		      const struct bpf_prog_linfo *prog_linfo,
206 		      __u64 func_ksym, unsigned int func_idx,
207 		      bool linum)
208 {
209 	return 0;
210 }
211 static inline int disasm_init(void)
212 {
213 	p_err("No JIT disassembly support");
214 	return -1;
215 }
216 #endif
217 void print_data_json(uint8_t *data, size_t len);
218 void print_hex_data_json(uint8_t *data, size_t len);
219 
220 unsigned int get_page_size(void);
221 unsigned int get_possible_cpus(void);
222 int get_possible_cpu_ids(int **cpu_ids);
223 const char *
224 ifindex_to_arch(__u32 ifindex, __u64 ns_dev, __u64 ns_ino, const char **opt);
225 
226 struct btf_dumper {
227 	const struct btf *btf;
228 	json_writer_t *jw;
229 	bool is_plain_text;
230 	bool prog_id_as_func_ptr;
231 };
232 
233 /* btf_dumper_type - print data along with type information
234  * @d: an instance containing context for dumping types
235  * @type_id: index in btf->types array. this points to the type to be dumped
236  * @data: pointer the actual data, i.e. the values to be printed
237  *
238  * Returns zero on success and negative error code otherwise
239  */
240 int btf_dumper_type(const struct btf_dumper *d, __u32 type_id,
241 		    const void *data);
242 void btf_dumper_type_only(const struct btf *btf, __u32 func_type_id,
243 			  char *func_only, int size);
244 bool btf_var_is_piece(const struct btf *btf, const struct btf_var_secinfo *vsi);
245 
246 void btf_dump_linfo_plain(const struct btf *btf,
247 			  const struct bpf_line_info *linfo,
248 			  const char *prefix, bool linum);
249 void btf_dump_linfo_json(const struct btf *btf,
250 			 const struct bpf_line_info *linfo, bool linum);
251 void btf_dump_linfo_dotlabel(const struct btf *btf,
252 			     const struct bpf_line_info *linfo, bool linum);
253 
254 struct nlattr;
255 struct ifinfomsg;
256 struct tcmsg;
257 int do_xdp_dump(struct ifinfomsg *ifinfo, struct nlattr **tb);
258 int do_filter_dump(struct tcmsg *ifinfo, struct nlattr **tb, const char *kind,
259 		   const char *devname, int ifindex);
260 
261 int print_all_levels(__maybe_unused enum libbpf_print_level level,
262 		     const char *format, va_list args);
263 
264 size_t hash_fn_for_key_as_id(long key, void *ctx);
265 bool equal_fn_for_key_as_id(long k1, long k2, void *ctx);
266 
267 /* bpf_attach_type_input_str - convert the provided attach type value into a
268  * textual representation that we accept for input purposes.
269  *
270  * This function is similar in nature to libbpf_bpf_attach_type_str, but
271  * recognizes some attach type names that have been used by the program in the
272  * past and which do not follow the string inference scheme that libbpf uses.
273  * These textual representations should only be used for user input.
274  *
275  * @t: The attach type
276  * Returns a pointer to a static string identifying the attach type. NULL is
277  * returned for unknown bpf_attach_type values.
278  */
279 const char *bpf_attach_type_input_str(enum bpf_attach_type t);
280 
281 static inline bool hashmap__empty(struct hashmap *map)
282 {
283 	return map ? hashmap__size(map) == 0 : true;
284 }
285 
286 int pathname_concat(char *buf, int buf_sz, const char *path,
287 		    const char *name);
288 
289 /* print netfilter bpf_link info */
290 void netfilter_dump_plain(const struct bpf_link_info *info);
291 void netfilter_dump_json(const struct bpf_link_info *info, json_writer_t *wtr);
292 
293 struct kernel_config_option {
294 	const char *name;
295 	bool macro_dump;
296 };
297 
298 int read_kernel_config(const struct kernel_config_option *requested_options,
299 		       size_t num_options, char **out_values,
300 		       const char *define_prefix);
301 #ifndef BPFTOOL_WITHOUT_CRYPTO
302 int bpftool_prog_sign(struct bpf_load_and_run_opts *opts);
303 __u32 register_session_key(const char *key_der_path);
304 #else
305 static inline int bpftool_prog_sign(struct bpf_load_and_run_opts *opts)
306 {
307 	p_err("bpftool was built without signing support");
308 	return -ENOTSUP;
309 }
310 
311 static inline __u32 register_session_key(const char *key_der_path)
312 {
313 	p_err("bpftool was built without signing support");
314 	return -1;
315 }
316 #endif
317 #endif
318