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