main.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #ifndef __BPF_TOOL_H
  34. #define __BPF_TOOL_H
  35. /* BFD and kernel.h both define GCC_VERSION, differently */
  36. #undef GCC_VERSION
  37. #include <stdbool.h>
  38. #include <stdio.h>
  39. #include <linux/bpf.h>
  40. #include <linux/compiler.h>
  41. #include <linux/kernel.h>
  42. #include <linux/hashtable.h>
  43. #include <tools/libc_compat.h>
  44. #include "json_writer.h"
  45. #define ptr_to_u64(ptr) ((__u64)(unsigned long)(ptr))
  46. #define NEXT_ARG() ({ argc--; argv++; if (argc < 0) usage(); })
  47. #define NEXT_ARGP() ({ (*argc)--; (*argv)++; if (*argc < 0) usage(); })
  48. #define BAD_ARG() ({ p_err("what is '%s'?", *argv); -1; })
  49. #define GET_ARG() ({ argc--; *argv++; })
  50. #define REQ_ARGS(cnt) \
  51. ({ \
  52. int _cnt = (cnt); \
  53. bool _res; \
  54. \
  55. if (argc < _cnt) { \
  56. p_err("'%s' needs at least %d arguments, %d found", \
  57. argv[-1], _cnt, argc); \
  58. _res = false; \
  59. } else { \
  60. _res = true; \
  61. } \
  62. _res; \
  63. })
  64. #define ERR_MAX_LEN 1024
  65. #define BPF_TAG_FMT "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
  66. #define HELP_SPEC_PROGRAM \
  67. "PROG := { id PROG_ID | pinned FILE | tag PROG_TAG }"
  68. #define HELP_SPEC_OPTIONS \
  69. "OPTIONS := { {-j|--json} [{-p|--pretty}] | {-f|--bpffs} | {-m|--mapcompat}"
  70. #define HELP_SPEC_MAP \
  71. "MAP := { id MAP_ID | pinned FILE }"
  72. enum bpf_obj_type {
  73. BPF_OBJ_UNKNOWN,
  74. BPF_OBJ_PROG,
  75. BPF_OBJ_MAP,
  76. };
  77. extern const char *bin_name;
  78. extern json_writer_t *json_wtr;
  79. extern bool json_output;
  80. extern bool show_pinned;
  81. extern int bpf_flags;
  82. extern struct pinned_obj_table prog_table;
  83. extern struct pinned_obj_table map_table;
  84. void p_err(const char *fmt, ...);
  85. void p_info(const char *fmt, ...);
  86. bool is_prefix(const char *pfx, const char *str);
  87. void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep);
  88. void usage(void) __noreturn;
  89. struct pinned_obj_table {
  90. DECLARE_HASHTABLE(table, 16);
  91. };
  92. struct pinned_obj {
  93. __u32 id;
  94. char *path;
  95. struct hlist_node hash;
  96. };
  97. int build_pinned_obj_table(struct pinned_obj_table *table,
  98. enum bpf_obj_type type);
  99. void delete_pinned_obj_table(struct pinned_obj_table *tab);
  100. void print_dev_plain(__u32 ifindex, __u64 ns_dev, __u64 ns_inode);
  101. void print_dev_json(__u32 ifindex, __u64 ns_dev, __u64 ns_inode);
  102. struct cmd {
  103. const char *cmd;
  104. int (*func)(int argc, char **argv);
  105. };
  106. int cmd_select(const struct cmd *cmds, int argc, char **argv,
  107. int (*help)(int argc, char **argv));
  108. int get_fd_type(int fd);
  109. const char *get_fd_type_name(enum bpf_obj_type type);
  110. char *get_fdinfo(int fd, const char *key);
  111. int open_obj_pinned(char *path, bool quiet);
  112. int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type);
  113. int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32));
  114. int do_pin_fd(int fd, const char *name);
  115. int do_prog(int argc, char **arg);
  116. int do_map(int argc, char **arg);
  117. int do_event_pipe(int argc, char **argv);
  118. int do_cgroup(int argc, char **arg);
  119. int do_perf(int argc, char **arg);
  120. int do_net(int argc, char **arg);
  121. int parse_u32_arg(int *argc, char ***argv, __u32 *val, const char *what);
  122. int prog_parse_fd(int *argc, char ***argv);
  123. int map_parse_fd(int *argc, char ***argv);
  124. int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len);
  125. void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
  126. const char *arch, const char *disassembler_options);
  127. void print_data_json(uint8_t *data, size_t len);
  128. void print_hex_data_json(uint8_t *data, size_t len);
  129. unsigned int get_page_size(void);
  130. unsigned int get_possible_cpus(void);
  131. const char *
  132. ifindex_to_bfd_params(__u32 ifindex, __u64 ns_dev, __u64 ns_ino,
  133. const char **opt);
  134. struct btf_dumper {
  135. const struct btf *btf;
  136. json_writer_t *jw;
  137. bool is_plain_text;
  138. };
  139. /* btf_dumper_type - print data along with type information
  140. * @d: an instance containing context for dumping types
  141. * @type_id: index in btf->types array. this points to the type to be dumped
  142. * @data: pointer the actual data, i.e. the values to be printed
  143. *
  144. * Returns zero on success and negative error code otherwise
  145. */
  146. int btf_dumper_type(const struct btf_dumper *d, __u32 type_id,
  147. const void *data);
  148. struct nlattr;
  149. struct ifinfomsg;
  150. struct tcmsg;
  151. int do_xdp_dump(struct ifinfomsg *ifinfo, struct nlattr **tb);
  152. int do_filter_dump(struct tcmsg *ifinfo, struct nlattr **tb, const char *kind,
  153. const char *devname, int ifindex);
  154. #endif