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