Warning: That file was not part of the compilation database. It may have many parsing errors.

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} |\n" \
48 "\t {-m|--mapcompat} | {-n|--nomount} }"
49#define HELP_SPEC_MAP \
50 "MAP := { id MAP_ID | pinned FILE }"
51
52static 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",
76};
77
78extern const char * const map_type_name[];
79extern const size_t map_type_name_size;
80
81enum bpf_obj_type {
82 BPF_OBJ_UNKNOWN,
83 BPF_OBJ_PROG,
84 BPF_OBJ_MAP,
85};
86
87extern const char *bin_name;
88
89extern json_writer_t *json_wtr;
90extern bool json_output;
91extern bool show_pinned;
92extern bool block_mount;
93extern int bpf_flags;
94extern struct pinned_obj_table prog_table;
95extern struct pinned_obj_table map_table;
96
97void p_err(const char *fmt, ...);
98void p_info(const char *fmt, ...);
99
100bool is_prefix(const char *pfx, const char *str);
101void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep);
102void usage(void) __noreturn;
103
104void set_max_rlimit(void);
105
106int mount_tracefs(const char *target);
107
108struct pinned_obj_table {
109 DECLARE_HASHTABLE(table, 16);
110};
111
112struct pinned_obj {
113 __u32 id;
114 char *path;
115 struct hlist_node hash;
116};
117
118struct btf;
119struct bpf_line_info;
120
121int build_pinned_obj_table(struct pinned_obj_table *table,
122 enum bpf_obj_type type);
123void delete_pinned_obj_table(struct pinned_obj_table *tab);
124void print_dev_plain(__u32 ifindex, __u64 ns_dev, __u64 ns_inode);
125void print_dev_json(__u32 ifindex, __u64 ns_dev, __u64 ns_inode);
126
127struct cmd {
128 const char *cmd;
129 int (*func)(int argc, char **argv);
130};
131
132int cmd_select(const struct cmd *cmds, int argc, char **argv,
133 int (*help)(int argc, char **argv));
134
135int get_fd_type(int fd);
136const char *get_fd_type_name(enum bpf_obj_type type);
137char *get_fdinfo(int fd, const char *key);
138int open_obj_pinned(char *path, bool quiet);
139int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type);
140int mount_bpffs_for_pin(const char *name);
141int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32));
142int do_pin_fd(int fd, const char *name);
143
144int do_prog(int argc, char **arg);
145int do_map(int argc, char **arg);
146int do_event_pipe(int argc, char **argv);
147int do_cgroup(int argc, char **arg);
148int do_perf(int argc, char **arg);
149int do_net(int argc, char **arg);
150int do_tracelog(int argc, char **arg);
151int do_feature(int argc, char **argv);
152
153int parse_u32_arg(int *argc, char ***argv, __u32 *val, const char *what);
154int prog_parse_fd(int *argc, char ***argv);
155int map_parse_fd(int *argc, char ***argv);
156int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len);
157
158struct bpf_prog_linfo;
159#ifdef HAVE_LIBBFD_SUPPORT
160void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
161 const char *arch, const char *disassembler_options,
162 const struct btf *btf,
163 const struct bpf_prog_linfo *prog_linfo,
164 __u64 func_ksym, unsigned int func_idx,
165 bool linum);
166int disasm_init(void);
167#else
168static inline
169void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
170 const char *arch, const char *disassembler_options,
171 const struct btf *btf,
172 const struct bpf_prog_linfo *prog_linfo,
173 __u64 func_ksym, unsigned int func_idx,
174 bool linum)
175{
176}
177static inline int disasm_init(void)
178{
179 p_err("No libbfd support");
180 return -1;
181}
182#endif
183void print_data_json(uint8_t *data, size_t len);
184void print_hex_data_json(uint8_t *data, size_t len);
185
186unsigned int get_page_size(void);
187unsigned int get_possible_cpus(void);
188const char *
189ifindex_to_bfd_params(__u32 ifindex, __u64 ns_dev, __u64 ns_ino,
190 const char **opt);
191
192struct btf_dumper {
193 const struct btf *btf;
194 json_writer_t *jw;
195 bool is_plain_text;
196};
197
198/* btf_dumper_type - print data along with type information
199 * @d: an instance containing context for dumping types
200 * @type_id: index in btf->types array. this points to the type to be dumped
201 * @data: pointer the actual data, i.e. the values to be printed
202 *
203 * Returns zero on success and negative error code otherwise
204 */
205int btf_dumper_type(const struct btf_dumper *d, __u32 type_id,
206 const void *data);
207void btf_dumper_type_only(const struct btf *btf, __u32 func_type_id,
208 char *func_only, int size);
209
210void btf_dump_linfo_plain(const struct btf *btf,
211 const struct bpf_line_info *linfo,
212 const char *prefix, bool linum);
213void btf_dump_linfo_json(const struct btf *btf,
214 const struct bpf_line_info *linfo, bool linum);
215
216struct nlattr;
217struct ifinfomsg;
218struct tcmsg;
219int do_xdp_dump(struct ifinfomsg *ifinfo, struct nlattr **tb);
220int do_filter_dump(struct tcmsg *ifinfo, struct nlattr **tb, const char *kind,
221 const char *devname, int ifindex);
222#endif
223

Warning: That file was not part of the compilation database. It may have many parsing errors.