main.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
  2. /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
  3. #ifndef __BPF_TOOL_H
  4. #define __BPF_TOOL_H
  5. /* BFD and kernel.h both define GCC_VERSION, differently */
  6. #undef GCC_VERSION
  7. #ifndef _GNU_SOURCE
  8. #define _GNU_SOURCE
  9. #endif
  10. #include <stdbool.h>
  11. #include <stdio.h>
  12. #include <errno.h>
  13. #include <stdlib.h>
  14. #include <bpf/skel_internal.h>
  15. #include <linux/bpf.h>
  16. #include <linux/compiler.h>
  17. #include <linux/kernel.h>
  18. #include <bpf/hashmap.h>
  19. #include <bpf/libbpf.h>
  20. #include <bpf/bpf.h>
  21. #include "json_writer.h"
  22. /* Make sure we do not use kernel-only integer typedefs */
  23. #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
  24. static inline __u64 ptr_to_u64(const void *ptr)
  25. {
  26. return (__u64)(unsigned long)ptr;
  27. }
  28. static inline void *u64_to_ptr(__u64 ptr)
  29. {
  30. return (void *)(unsigned long)ptr;
  31. }
  32. #define NEXT_ARG() ({ argc--; argv++; if (argc < 0) usage(); })
  33. #define NEXT_ARGP() ({ (*argc)--; (*argv)++; if (*argc < 0) usage(); })
  34. #define BAD_ARG() ({ p_err("what is '%s'?", *argv); -1; })
  35. #define GET_ARG() ({ argc--; *argv++; })
  36. #define REQ_ARGS(cnt) \
  37. ({ \
  38. int _cnt = (cnt); \
  39. bool _res; \
  40. \
  41. if (argc < _cnt) { \
  42. p_err("'%s' needs at least %d arguments, %d found", \
  43. argv[-1], _cnt, argc); \
  44. _res = false; \
  45. } else { \
  46. _res = true; \
  47. } \
  48. _res; \
  49. })
  50. #define ERR_MAX_LEN 1024
  51. #define MAX_SIG_SIZE 4096
  52. #define BPF_TAG_FMT "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
  53. #define HELP_SPEC_PROGRAM \
  54. "PROG := { id PROG_ID | pinned FILE | tag PROG_TAG | name PROG_NAME }"
  55. #define HELP_SPEC_OPTIONS \
  56. "OPTIONS := { {-j|--json} [{-p|--pretty}] | {-d|--debug}"
  57. #define HELP_SPEC_MAP \
  58. "MAP := { id MAP_ID | pinned FILE | name MAP_NAME }"
  59. #define HELP_SPEC_LINK \
  60. "LINK := { id LINK_ID | pinned FILE }"
  61. /* keep in sync with the definition in skeleton/pid_iter.bpf.c */
  62. enum bpf_obj_type {
  63. BPF_OBJ_UNKNOWN,
  64. BPF_OBJ_PROG,
  65. BPF_OBJ_MAP,
  66. BPF_OBJ_LINK,
  67. BPF_OBJ_BTF,
  68. };
  69. extern const char *bin_name;
  70. extern json_writer_t *json_wtr;
  71. extern bool json_output;
  72. extern bool show_pinned;
  73. extern bool show_pids;
  74. extern bool block_mount;
  75. extern bool verifier_logs;
  76. extern bool relaxed_maps;
  77. extern bool use_loader;
  78. extern struct btf *base_btf;
  79. extern struct hashmap *refs_table;
  80. extern bool sign_progs;
  81. extern const char *private_key_path;
  82. extern const char *cert_path;
  83. void __printf(1, 2) p_err(const char *fmt, ...);
  84. void __printf(1, 2) p_info(const char *fmt, ...);
  85. bool is_prefix(const char *pfx, const char *str);
  86. int detect_common_prefix(const char *arg, ...);
  87. void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep);
  88. void usage(void) __noreturn;
  89. void set_max_rlimit(void);
  90. int mount_tracefs(const char *target);
  91. struct obj_ref {
  92. int pid;
  93. char comm[16];
  94. };
  95. struct obj_refs {
  96. int ref_cnt;
  97. bool has_bpf_cookie;
  98. struct obj_ref *refs;
  99. __u64 bpf_cookie;
  100. };
  101. struct btf;
  102. struct bpf_line_info;
  103. int build_pinned_obj_table(struct hashmap *table,
  104. enum bpf_obj_type type);
  105. void delete_pinned_obj_table(struct hashmap *table);
  106. __weak int build_obj_refs_table(struct hashmap **table,
  107. enum bpf_obj_type type);
  108. __weak void delete_obj_refs_table(struct hashmap *table);
  109. __weak void emit_obj_refs_json(struct hashmap *table, __u32 id,
  110. json_writer_t *json_wtr);
  111. __weak void emit_obj_refs_plain(struct hashmap *table, __u32 id,
  112. const char *prefix);
  113. void print_dev_plain(__u32 ifindex, __u64 ns_dev, __u64 ns_inode);
  114. void print_dev_json(__u32 ifindex, __u64 ns_dev, __u64 ns_inode);
  115. struct cmd {
  116. const char *cmd;
  117. int (*func)(int argc, char **argv);
  118. };
  119. int cmd_select(const struct cmd *cmds, int argc, char **argv,
  120. int (*help)(int argc, char **argv));
  121. #define MAX_PROG_FULL_NAME 128
  122. void get_prog_full_name(const struct bpf_prog_info *prog_info, int prog_fd,
  123. char *name_buff, size_t buff_len);
  124. int get_fd_type(int fd);
  125. const char *get_fd_type_name(enum bpf_obj_type type);
  126. char *get_fdinfo(int fd, const char *key);
  127. int open_obj_pinned(const char *path, bool quiet,
  128. const struct bpf_obj_get_opts *opts);
  129. int open_obj_pinned_any(const char *path, enum bpf_obj_type exp_type,
  130. const struct bpf_obj_get_opts *opts);
  131. int mount_bpffs_for_file(const char *file_name);
  132. int create_and_mount_bpffs_dir(const char *dir_name);
  133. int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(int *, char ***));
  134. int do_pin_fd(int fd, const char *name);
  135. /* commands available in bootstrap mode */
  136. int do_gen(int argc, char **argv);
  137. int do_btf(int argc, char **argv);
  138. /* non-bootstrap only commands */
  139. int do_prog(int argc, char **arg) __weak;
  140. int do_map(int argc, char **arg) __weak;
  141. int do_link(int argc, char **arg) __weak;
  142. int do_event_pipe(int argc, char **argv) __weak;
  143. int do_cgroup(int argc, char **arg) __weak;
  144. int do_perf(int argc, char **arg) __weak;
  145. int do_net(int argc, char **arg) __weak;
  146. int do_tracelog(int argc, char **arg) __weak;
  147. int do_feature(int argc, char **argv) __weak;
  148. int do_struct_ops(int argc, char **argv) __weak;
  149. int do_iter(int argc, char **argv) __weak;
  150. int do_token(int argc, char **argv) __weak;
  151. int parse_u32_arg(int *argc, char ***argv, __u32 *val, const char *what);
  152. int prog_parse_fd(int *argc, char ***argv);
  153. int prog_parse_fds(int *argc, char ***argv, int **fds);
  154. int map_parse_fd(int *argc, char ***argv, __u32 open_flags);
  155. int map_parse_fds(int *argc, char ***argv, int **fds, __u32 open_flags);
  156. int map_parse_fd_and_info(int *argc, char ***argv, struct bpf_map_info *info,
  157. __u32 *info_len, __u32 open_flags);
  158. struct bpf_prog_linfo;
  159. #if defined(HAVE_LLVM_SUPPORT) || defined(HAVE_LIBBFD_SUPPORT)
  160. int 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);
  166. int disasm_init(void);
  167. #else
  168. static inline
  169. int 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. return 0;
  177. }
  178. static inline int disasm_init(void)
  179. {
  180. p_err("No JIT disassembly support");
  181. return -1;
  182. }
  183. #endif
  184. void print_data_json(uint8_t *data, size_t len);
  185. void print_hex_data_json(uint8_t *data, size_t len);
  186. unsigned int get_page_size(void);
  187. unsigned int get_possible_cpus(void);
  188. const char *
  189. ifindex_to_arch(__u32 ifindex, __u64 ns_dev, __u64 ns_ino, const char **opt);
  190. struct btf_dumper {
  191. const struct btf *btf;
  192. json_writer_t *jw;
  193. bool is_plain_text;
  194. bool prog_id_as_func_ptr;
  195. };
  196. /* btf_dumper_type - print data along with type information
  197. * @d: an instance containing context for dumping types
  198. * @type_id: index in btf->types array. this points to the type to be dumped
  199. * @data: pointer the actual data, i.e. the values to be printed
  200. *
  201. * Returns zero on success and negative error code otherwise
  202. */
  203. int btf_dumper_type(const struct btf_dumper *d, __u32 type_id,
  204. const void *data);
  205. void btf_dumper_type_only(const struct btf *btf, __u32 func_type_id,
  206. char *func_only, int size);
  207. void btf_dump_linfo_plain(const struct btf *btf,
  208. const struct bpf_line_info *linfo,
  209. const char *prefix, bool linum);
  210. void btf_dump_linfo_json(const struct btf *btf,
  211. const struct bpf_line_info *linfo, bool linum);
  212. void btf_dump_linfo_dotlabel(const struct btf *btf,
  213. const struct bpf_line_info *linfo, bool linum);
  214. struct nlattr;
  215. struct ifinfomsg;
  216. struct tcmsg;
  217. int do_xdp_dump(struct ifinfomsg *ifinfo, struct nlattr **tb);
  218. int do_filter_dump(struct tcmsg *ifinfo, struct nlattr **tb, const char *kind,
  219. const char *devname, int ifindex);
  220. int print_all_levels(__maybe_unused enum libbpf_print_level level,
  221. const char *format, va_list args);
  222. size_t hash_fn_for_key_as_id(long key, void *ctx);
  223. bool equal_fn_for_key_as_id(long k1, long k2, void *ctx);
  224. /* bpf_attach_type_input_str - convert the provided attach type value into a
  225. * textual representation that we accept for input purposes.
  226. *
  227. * This function is similar in nature to libbpf_bpf_attach_type_str, but
  228. * recognizes some attach type names that have been used by the program in the
  229. * past and which do not follow the string inference scheme that libbpf uses.
  230. * These textual representations should only be used for user input.
  231. *
  232. * @t: The attach type
  233. * Returns a pointer to a static string identifying the attach type. NULL is
  234. * returned for unknown bpf_attach_type values.
  235. */
  236. const char *bpf_attach_type_input_str(enum bpf_attach_type t);
  237. static inline bool hashmap__empty(struct hashmap *map)
  238. {
  239. return map ? hashmap__size(map) == 0 : true;
  240. }
  241. int pathname_concat(char *buf, int buf_sz, const char *path,
  242. const char *name);
  243. /* print netfilter bpf_link info */
  244. void netfilter_dump_plain(const struct bpf_link_info *info);
  245. void netfilter_dump_json(const struct bpf_link_info *info, json_writer_t *wtr);
  246. struct kernel_config_option {
  247. const char *name;
  248. bool macro_dump;
  249. };
  250. int read_kernel_config(const struct kernel_config_option *requested_options,
  251. size_t num_options, char **out_values,
  252. const char *define_prefix);
  253. int bpftool_prog_sign(struct bpf_load_and_run_opts *opts);
  254. __u32 register_session_key(const char *key_der_path);
  255. #endif