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