main.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. /* Author: Jakub Kicinski <kubakici@wp.pl> */
  34. #ifndef __BPF_TOOL_H
  35. #define __BPF_TOOL_H
  36. /* BFD and kernel.h both define GCC_VERSION, differently */
  37. #undef GCC_VERSION
  38. #include <stdbool.h>
  39. #include <stdio.h>
  40. #include <linux/bpf.h>
  41. #include <linux/compiler.h>
  42. #include <linux/kernel.h>
  43. #include <linux/hashtable.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 ERR_MAX_LEN 1024
  50. #define BPF_TAG_FMT "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
  51. #define HELP_SPEC_PROGRAM \
  52. "PROG := { id PROG_ID | pinned FILE | tag PROG_TAG }"
  53. #define HELP_SPEC_OPTIONS \
  54. "OPTIONS := { {-j|--json} [{-p|--pretty}] | {-f|--bpffs} }"
  55. enum bpf_obj_type {
  56. BPF_OBJ_UNKNOWN,
  57. BPF_OBJ_PROG,
  58. BPF_OBJ_MAP,
  59. };
  60. extern const char *bin_name;
  61. extern json_writer_t *json_wtr;
  62. extern bool json_output;
  63. extern bool show_pinned;
  64. extern struct pinned_obj_table prog_table;
  65. extern struct pinned_obj_table map_table;
  66. void p_err(const char *fmt, ...);
  67. void p_info(const char *fmt, ...);
  68. bool is_prefix(const char *pfx, const char *str);
  69. void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep);
  70. void usage(void) __noreturn;
  71. struct pinned_obj_table {
  72. DECLARE_HASHTABLE(table, 16);
  73. };
  74. struct pinned_obj {
  75. __u32 id;
  76. char *path;
  77. struct hlist_node hash;
  78. };
  79. int build_pinned_obj_table(struct pinned_obj_table *table,
  80. enum bpf_obj_type type);
  81. void delete_pinned_obj_table(struct pinned_obj_table *tab);
  82. void print_dev_plain(__u32 ifindex, __u64 ns_dev, __u64 ns_inode);
  83. void print_dev_json(__u32 ifindex, __u64 ns_dev, __u64 ns_inode);
  84. struct cmd {
  85. const char *cmd;
  86. int (*func)(int argc, char **argv);
  87. };
  88. int cmd_select(const struct cmd *cmds, int argc, char **argv,
  89. int (*help)(int argc, char **argv));
  90. int get_fd_type(int fd);
  91. const char *get_fd_type_name(enum bpf_obj_type type);
  92. char *get_fdinfo(int fd, const char *key);
  93. int open_obj_pinned(char *path);
  94. int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type);
  95. int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32));
  96. int do_pin_fd(int fd, const char *name);
  97. int do_prog(int argc, char **arg);
  98. int do_map(int argc, char **arg);
  99. int do_event_pipe(int argc, char **argv);
  100. int do_cgroup(int argc, char **arg);
  101. int do_perf(int argc, char **arg);
  102. int prog_parse_fd(int *argc, char ***argv);
  103. int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len);
  104. void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
  105. const char *arch);
  106. void print_data_json(uint8_t *data, size_t len);
  107. void print_hex_data_json(uint8_t *data, size_t len);
  108. unsigned int get_page_size(void);
  109. unsigned int get_possible_cpus(void);
  110. const char *ifindex_to_bfd_name_ns(__u32 ifindex, __u64 ns_dev, __u64 ns_ino);
  111. #endif