1 /* 2 * Copyright (C) 2017-2018 Netronome Systems, Inc. 3 * 4 * This software is dual licensed under the GNU General License Version 2, 5 * June 1991 as shown in the file COPYING in the top-level directory of this 6 * source tree or the BSD 2-Clause License provided below. You have the 7 * option to license this software under the complete terms of either license. 8 * 9 * The BSD 2-Clause License: 10 * 11 * Redistribution and use in source and binary forms, with or 12 * without modification, are permitted provided that the following 13 * conditions are met: 14 * 15 * 1. Redistributions of source code must retain the above 16 * copyright notice, this list of conditions and the following 17 * disclaimer. 18 * 19 * 2. Redistributions in binary form must reproduce the above 20 * copyright notice, this list of conditions and the following 21 * disclaimer in the documentation and/or other materials 22 * provided with the distribution. 23 * 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 31 * SOFTWARE. 32 */ 33 34 #ifndef __BPF_TOOL_H 35 #define __BPF_TOOL_H 36 37 /* BFD and kernel.h both define GCC_VERSION, differently */ 38 #undef GCC_VERSION 39 #include <stdbool.h> 40 #include <stdio.h> 41 #include <linux/bpf.h> 42 #include <linux/compiler.h> 43 #include <linux/kernel.h> 44 #include <linux/hashtable.h> 45 #include <tools/libc_compat.h> 46 47 #include "json_writer.h" 48 49 #define ptr_to_u64(ptr) ((__u64)(unsigned long)(ptr)) 50 51 #define NEXT_ARG() ({ argc--; argv++; if (argc < 0) usage(); }) 52 #define NEXT_ARGP() ({ (*argc)--; (*argv)++; if (*argc < 0) usage(); }) 53 #define BAD_ARG() ({ p_err("what is '%s'?", *argv); -1; }) 54 #define GET_ARG() ({ argc--; *argv++; }) 55 #define REQ_ARGS(cnt) \ 56 ({ \ 57 int _cnt = (cnt); \ 58 bool _res; \ 59 \ 60 if (argc < _cnt) { \ 61 p_err("'%s' needs at least %d arguments, %d found", \ 62 argv[-1], _cnt, argc); \ 63 _res = false; \ 64 } else { \ 65 _res = true; \ 66 } \ 67 _res; \ 68 }) 69 70 #define ERR_MAX_LEN 1024 71 72 #define BPF_TAG_FMT "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx" 73 74 #define HELP_SPEC_PROGRAM \ 75 "PROG := { id PROG_ID | pinned FILE | tag PROG_TAG }" 76 #define HELP_SPEC_OPTIONS \ 77 "OPTIONS := { {-j|--json} [{-p|--pretty}] | {-f|--bpffs} | {-m|--mapcompat}" 78 #define HELP_SPEC_MAP \ 79 "MAP := { id MAP_ID | pinned FILE }" 80 81 static const char * const prog_type_name[] = { 82 [BPF_PROG_TYPE_UNSPEC] = "unspec", 83 [BPF_PROG_TYPE_SOCKET_FILTER] = "socket_filter", 84 [BPF_PROG_TYPE_KPROBE] = "kprobe", 85 [BPF_PROG_TYPE_SCHED_CLS] = "sched_cls", 86 [BPF_PROG_TYPE_SCHED_ACT] = "sched_act", 87 [BPF_PROG_TYPE_TRACEPOINT] = "tracepoint", 88 [BPF_PROG_TYPE_XDP] = "xdp", 89 [BPF_PROG_TYPE_PERF_EVENT] = "perf_event", 90 [BPF_PROG_TYPE_CGROUP_SKB] = "cgroup_skb", 91 [BPF_PROG_TYPE_CGROUP_SOCK] = "cgroup_sock", 92 [BPF_PROG_TYPE_LWT_IN] = "lwt_in", 93 [BPF_PROG_TYPE_LWT_OUT] = "lwt_out", 94 [BPF_PROG_TYPE_LWT_XMIT] = "lwt_xmit", 95 [BPF_PROG_TYPE_SOCK_OPS] = "sock_ops", 96 [BPF_PROG_TYPE_SK_SKB] = "sk_skb", 97 [BPF_PROG_TYPE_CGROUP_DEVICE] = "cgroup_device", 98 [BPF_PROG_TYPE_SK_MSG] = "sk_msg", 99 [BPF_PROG_TYPE_RAW_TRACEPOINT] = "raw_tracepoint", 100 [BPF_PROG_TYPE_CGROUP_SOCK_ADDR] = "cgroup_sock_addr", 101 [BPF_PROG_TYPE_LWT_SEG6LOCAL] = "lwt_seg6local", 102 [BPF_PROG_TYPE_LIRC_MODE2] = "lirc_mode2", 103 [BPF_PROG_TYPE_SK_REUSEPORT] = "sk_reuseport", 104 [BPF_PROG_TYPE_FLOW_DISSECTOR] = "flow_dissector", 105 }; 106 107 enum bpf_obj_type { 108 BPF_OBJ_UNKNOWN, 109 BPF_OBJ_PROG, 110 BPF_OBJ_MAP, 111 }; 112 113 extern const char *bin_name; 114 115 extern json_writer_t *json_wtr; 116 extern bool json_output; 117 extern bool show_pinned; 118 extern int bpf_flags; 119 extern struct pinned_obj_table prog_table; 120 extern struct pinned_obj_table map_table; 121 122 void p_err(const char *fmt, ...); 123 void p_info(const char *fmt, ...); 124 125 bool is_prefix(const char *pfx, const char *str); 126 void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep); 127 void usage(void) __noreturn; 128 129 void set_max_rlimit(void); 130 131 struct pinned_obj_table { 132 DECLARE_HASHTABLE(table, 16); 133 }; 134 135 struct pinned_obj { 136 __u32 id; 137 char *path; 138 struct hlist_node hash; 139 }; 140 141 struct btf; 142 struct bpf_line_info; 143 144 int build_pinned_obj_table(struct pinned_obj_table *table, 145 enum bpf_obj_type type); 146 void delete_pinned_obj_table(struct pinned_obj_table *tab); 147 void print_dev_plain(__u32 ifindex, __u64 ns_dev, __u64 ns_inode); 148 void print_dev_json(__u32 ifindex, __u64 ns_dev, __u64 ns_inode); 149 150 struct cmd { 151 const char *cmd; 152 int (*func)(int argc, char **argv); 153 }; 154 155 int cmd_select(const struct cmd *cmds, int argc, char **argv, 156 int (*help)(int argc, char **argv)); 157 158 int get_fd_type(int fd); 159 const char *get_fd_type_name(enum bpf_obj_type type); 160 char *get_fdinfo(int fd, const char *key); 161 int open_obj_pinned(char *path, bool quiet); 162 int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type); 163 int mount_bpffs_for_pin(const char *name); 164 int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32)); 165 int do_pin_fd(int fd, const char *name); 166 167 int do_prog(int argc, char **arg); 168 int do_map(int argc, char **arg); 169 int do_event_pipe(int argc, char **argv); 170 int do_cgroup(int argc, char **arg); 171 int do_perf(int argc, char **arg); 172 int do_net(int argc, char **arg); 173 int do_tracelog(int argc, char **arg); 174 175 int parse_u32_arg(int *argc, char ***argv, __u32 *val, const char *what); 176 int prog_parse_fd(int *argc, char ***argv); 177 int map_parse_fd(int *argc, char ***argv); 178 int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len); 179 180 #ifdef HAVE_LIBBFD_SUPPORT 181 struct bpf_prog_linfo; 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 int disasm_init(void); 189 #else 190 static inline 191 void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, 192 const char *arch, const char *disassembler_options, 193 const struct btf *btf, 194 const struct bpf_prog_linfo *prog_linfo, 195 __u64 func_ksym, unsigned int func_idx, 196 bool linum) 197 { 198 } 199 static inline int disasm_init(void) 200 { 201 p_err("No libbfd support"); 202 return -1; 203 } 204 #endif 205 void print_data_json(uint8_t *data, size_t len); 206 void print_hex_data_json(uint8_t *data, size_t len); 207 208 unsigned int get_page_size(void); 209 unsigned int get_possible_cpus(void); 210 const char * 211 ifindex_to_bfd_params(__u32 ifindex, __u64 ns_dev, __u64 ns_ino, 212 const char **opt); 213 214 struct btf_dumper { 215 const struct btf *btf; 216 json_writer_t *jw; 217 bool is_plain_text; 218 }; 219 220 /* btf_dumper_type - print data along with type information 221 * @d: an instance containing context for dumping types 222 * @type_id: index in btf->types array. this points to the type to be dumped 223 * @data: pointer the actual data, i.e. the values to be printed 224 * 225 * Returns zero on success and negative error code otherwise 226 */ 227 int btf_dumper_type(const struct btf_dumper *d, __u32 type_id, 228 const void *data); 229 void btf_dumper_type_only(const struct btf *btf, __u32 func_type_id, 230 char *func_only, int size); 231 232 void btf_dump_linfo_plain(const struct btf *btf, 233 const struct bpf_line_info *linfo, 234 const char *prefix, bool linum); 235 void btf_dump_linfo_json(const struct btf *btf, 236 const struct bpf_line_info *linfo, bool linum); 237 238 struct nlattr; 239 struct ifinfomsg; 240 struct tcmsg; 241 int do_xdp_dump(struct ifinfomsg *ifinfo, struct nlattr **tb); 242 int do_filter_dump(struct tcmsg *ifinfo, struct nlattr **tb, const char *kind, 243 const char *devname, int ifindex); 244 #endif 245