main.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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} }"
  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 struct pinned_obj_table prog_table;
  82. extern struct pinned_obj_table map_table;
  83. void p_err(const char *fmt, ...);
  84. void p_info(const char *fmt, ...);
  85. bool is_prefix(const char *pfx, const char *str);
  86. void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep);
  87. void usage(void) __noreturn;
  88. struct pinned_obj_table {
  89. DECLARE_HASHTABLE(table, 16);
  90. };
  91. struct pinned_obj {
  92. __u32 id;
  93. char *path;
  94. struct hlist_node hash;
  95. };
  96. int build_pinned_obj_table(struct pinned_obj_table *table,
  97. enum bpf_obj_type type);
  98. void delete_pinned_obj_table(struct pinned_obj_table *tab);
  99. void print_dev_plain(__u32 ifindex, __u64 ns_dev, __u64 ns_inode);
  100. void print_dev_json(__u32 ifindex, __u64 ns_dev, __u64 ns_inode);
  101. struct cmd {
  102. const char *cmd;
  103. int (*func)(int argc, char **argv);
  104. };
  105. int cmd_select(const struct cmd *cmds, int argc, char **argv,
  106. int (*help)(int argc, char **argv));
  107. int get_fd_type(int fd);
  108. const char *get_fd_type_name(enum bpf_obj_type type);
  109. char *get_fdinfo(int fd, const char *key);
  110. int open_obj_pinned(char *path, bool quiet);
  111. int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type);
  112. int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32));
  113. int do_pin_fd(int fd, const char *name);
  114. int do_prog(int argc, char **arg);
  115. int do_map(int argc, char **arg);
  116. int do_event_pipe(int argc, char **argv);
  117. int do_cgroup(int argc, char **arg);
  118. int do_perf(int argc, char **arg);
  119. int prog_parse_fd(int *argc, char ***argv);
  120. int map_parse_fd(int *argc, char ***argv);
  121. int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len);
  122. void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
  123. const char *arch);
  124. void print_data_json(uint8_t *data, size_t len);
  125. void print_hex_data_json(uint8_t *data, size_t len);
  126. unsigned int get_page_size(void);
  127. unsigned int get_possible_cpus(void);
  128. const char *ifindex_to_bfd_name_ns(__u32 ifindex, __u64 ns_dev, __u64 ns_ino);
  129. struct btf_dumper {
  130. const struct btf *btf;
  131. json_writer_t *jw;
  132. bool is_plain_text;
  133. };
  134. /* btf_dumper_type - print data along with type information
  135. * @d: an instance containing context for dumping types
  136. * @type_id: index in btf->types array. this points to the type to be dumped
  137. * @data: pointer the actual data, i.e. the values to be printed
  138. *
  139. * Returns zero on success and negative error code otherwise
  140. */
  141. int btf_dumper_type(const struct btf_dumper *d, __u32 type_id,
  142. const void *data);
  143. #endif