main.h 8.2 KB

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