Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
0002 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
0003 
0004 #ifndef __BPF_TOOL_H
0005 #define __BPF_TOOL_H
0006 
0007 /* BFD and kernel.h both define GCC_VERSION, differently */
0008 #undef GCC_VERSION
0009 #include <stdbool.h>
0010 #include <stdio.h>
0011 #include <stdlib.h>
0012 #include <linux/bpf.h>
0013 #include <linux/compiler.h>
0014 #include <linux/kernel.h>
0015 
0016 #include <bpf/hashmap.h>
0017 #include <bpf/libbpf.h>
0018 
0019 #include "json_writer.h"
0020 
0021 /* Make sure we do not use kernel-only integer typedefs */
0022 #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
0023 
0024 static inline __u64 ptr_to_u64(const void *ptr)
0025 {
0026     return (__u64)(unsigned long)ptr;
0027 }
0028 
0029 static inline void *u64_to_ptr(__u64 ptr)
0030 {
0031     return (void *)(unsigned long)ptr;
0032 }
0033 
0034 #define NEXT_ARG()  ({ argc--; argv++; if (argc < 0) usage(); })
0035 #define NEXT_ARGP() ({ (*argc)--; (*argv)++; if (*argc < 0) usage(); })
0036 #define BAD_ARG()   ({ p_err("what is '%s'?", *argv); -1; })
0037 #define GET_ARG()   ({ argc--; *argv++; })
0038 #define REQ_ARGS(cnt)                           \
0039     ({                              \
0040         int _cnt = (cnt);                   \
0041         bool _res;                      \
0042                                     \
0043         if (argc < _cnt) {                  \
0044             p_err("'%s' needs at least %d arguments, %d found", \
0045                   argv[-1], _cnt, argc);            \
0046             _res = false;                   \
0047         } else {                        \
0048             _res = true;                    \
0049         }                           \
0050         _res;                           \
0051     })
0052 
0053 #define ERR_MAX_LEN 1024
0054 
0055 #define BPF_TAG_FMT "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
0056 
0057 #define HELP_SPEC_PROGRAM                       \
0058     "PROG := { id PROG_ID | pinned FILE | tag PROG_TAG | name PROG_NAME }"
0059 #define HELP_SPEC_OPTIONS                       \
0060     "OPTIONS := { {-j|--json} [{-p|--pretty}] | {-d|--debug} | {-l|--legacy}"
0061 #define HELP_SPEC_MAP                           \
0062     "MAP := { id MAP_ID | pinned FILE | name MAP_NAME }"
0063 #define HELP_SPEC_LINK                          \
0064     "LINK := { id LINK_ID | pinned FILE }"
0065 
0066 /* keep in sync with the definition in skeleton/pid_iter.bpf.c */
0067 enum bpf_obj_type {
0068     BPF_OBJ_UNKNOWN,
0069     BPF_OBJ_PROG,
0070     BPF_OBJ_MAP,
0071     BPF_OBJ_LINK,
0072     BPF_OBJ_BTF,
0073 };
0074 
0075 extern const char *bin_name;
0076 
0077 extern json_writer_t *json_wtr;
0078 extern bool json_output;
0079 extern bool show_pinned;
0080 extern bool show_pids;
0081 extern bool block_mount;
0082 extern bool verifier_logs;
0083 extern bool relaxed_maps;
0084 extern bool use_loader;
0085 extern bool legacy_libbpf;
0086 extern struct btf *base_btf;
0087 extern struct hashmap *refs_table;
0088 
0089 void __printf(1, 2) p_err(const char *fmt, ...);
0090 void __printf(1, 2) p_info(const char *fmt, ...);
0091 
0092 bool is_prefix(const char *pfx, const char *str);
0093 int detect_common_prefix(const char *arg, ...);
0094 void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep);
0095 void usage(void) __noreturn;
0096 
0097 void set_max_rlimit(void);
0098 
0099 int mount_tracefs(const char *target);
0100 
0101 struct obj_ref {
0102     int pid;
0103     char comm[16];
0104 };
0105 
0106 struct obj_refs {
0107     int ref_cnt;
0108     bool has_bpf_cookie;
0109     struct obj_ref *refs;
0110     __u64 bpf_cookie;
0111 };
0112 
0113 struct btf;
0114 struct bpf_line_info;
0115 
0116 int build_pinned_obj_table(struct hashmap *table,
0117                enum bpf_obj_type type);
0118 void delete_pinned_obj_table(struct hashmap *table);
0119 __weak int build_obj_refs_table(struct hashmap **table,
0120                 enum bpf_obj_type type);
0121 __weak void delete_obj_refs_table(struct hashmap *table);
0122 __weak void emit_obj_refs_json(struct hashmap *table, __u32 id,
0123                    json_writer_t *json_wtr);
0124 __weak void emit_obj_refs_plain(struct hashmap *table, __u32 id,
0125                 const char *prefix);
0126 void print_dev_plain(__u32 ifindex, __u64 ns_dev, __u64 ns_inode);
0127 void print_dev_json(__u32 ifindex, __u64 ns_dev, __u64 ns_inode);
0128 
0129 struct cmd {
0130     const char *cmd;
0131     int (*func)(int argc, char **argv);
0132 };
0133 
0134 int cmd_select(const struct cmd *cmds, int argc, char **argv,
0135            int (*help)(int argc, char **argv));
0136 
0137 #define MAX_PROG_FULL_NAME 128
0138 void get_prog_full_name(const struct bpf_prog_info *prog_info, int prog_fd,
0139             char *name_buff, size_t buff_len);
0140 
0141 int get_fd_type(int fd);
0142 const char *get_fd_type_name(enum bpf_obj_type type);
0143 char *get_fdinfo(int fd, const char *key);
0144 int open_obj_pinned(const char *path, bool quiet);
0145 int open_obj_pinned_any(const char *path, enum bpf_obj_type exp_type);
0146 int mount_bpffs_for_pin(const char *name);
0147 int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(int *, char ***));
0148 int do_pin_fd(int fd, const char *name);
0149 
0150 /* commands available in bootstrap mode */
0151 int do_gen(int argc, char **argv);
0152 int do_btf(int argc, char **argv);
0153 
0154 /* non-bootstrap only commands */
0155 int do_prog(int argc, char **arg) __weak;
0156 int do_map(int argc, char **arg) __weak;
0157 int do_link(int argc, char **arg) __weak;
0158 int do_event_pipe(int argc, char **argv) __weak;
0159 int do_cgroup(int argc, char **arg) __weak;
0160 int do_perf(int argc, char **arg) __weak;
0161 int do_net(int argc, char **arg) __weak;
0162 int do_tracelog(int argc, char **arg) __weak;
0163 int do_feature(int argc, char **argv) __weak;
0164 int do_struct_ops(int argc, char **argv) __weak;
0165 int do_iter(int argc, char **argv) __weak;
0166 
0167 int parse_u32_arg(int *argc, char ***argv, __u32 *val, const char *what);
0168 int prog_parse_fd(int *argc, char ***argv);
0169 int prog_parse_fds(int *argc, char ***argv, int **fds);
0170 int map_parse_fd(int *argc, char ***argv);
0171 int map_parse_fds(int *argc, char ***argv, int **fds);
0172 int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len);
0173 
0174 struct bpf_prog_linfo;
0175 #ifdef HAVE_LIBBFD_SUPPORT
0176 void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
0177                const char *arch, const char *disassembler_options,
0178                const struct btf *btf,
0179                const struct bpf_prog_linfo *prog_linfo,
0180                __u64 func_ksym, unsigned int func_idx,
0181                bool linum);
0182 int disasm_init(void);
0183 #else
0184 static inline
0185 void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
0186                const char *arch, const char *disassembler_options,
0187                const struct btf *btf,
0188                const struct bpf_prog_linfo *prog_linfo,
0189                __u64 func_ksym, unsigned int func_idx,
0190                bool linum)
0191 {
0192 }
0193 static inline int disasm_init(void)
0194 {
0195     p_err("No libbfd support");
0196     return -1;
0197 }
0198 #endif
0199 void print_data_json(uint8_t *data, size_t len);
0200 void print_hex_data_json(uint8_t *data, size_t len);
0201 
0202 unsigned int get_page_size(void);
0203 unsigned int get_possible_cpus(void);
0204 const char *
0205 ifindex_to_bfd_params(__u32 ifindex, __u64 ns_dev, __u64 ns_ino,
0206               const char **opt);
0207 
0208 struct btf_dumper {
0209     const struct btf *btf;
0210     json_writer_t *jw;
0211     bool is_plain_text;
0212     bool prog_id_as_func_ptr;
0213 };
0214 
0215 /* btf_dumper_type - print data along with type information
0216  * @d: an instance containing context for dumping types
0217  * @type_id: index in btf->types array. this points to the type to be dumped
0218  * @data: pointer the actual data, i.e. the values to be printed
0219  *
0220  * Returns zero on success and negative error code otherwise
0221  */
0222 int btf_dumper_type(const struct btf_dumper *d, __u32 type_id,
0223             const void *data);
0224 void btf_dumper_type_only(const struct btf *btf, __u32 func_type_id,
0225               char *func_only, int size);
0226 
0227 void btf_dump_linfo_plain(const struct btf *btf,
0228               const struct bpf_line_info *linfo,
0229               const char *prefix, bool linum);
0230 void btf_dump_linfo_json(const struct btf *btf,
0231              const struct bpf_line_info *linfo, bool linum);
0232 
0233 struct nlattr;
0234 struct ifinfomsg;
0235 struct tcmsg;
0236 int do_xdp_dump(struct ifinfomsg *ifinfo, struct nlattr **tb);
0237 int do_filter_dump(struct tcmsg *ifinfo, struct nlattr **tb, const char *kind,
0238            const char *devname, int ifindex);
0239 
0240 int print_all_levels(__maybe_unused enum libbpf_print_level level,
0241              const char *format, va_list args);
0242 
0243 size_t hash_fn_for_key_as_id(const void *key, void *ctx);
0244 bool equal_fn_for_key_as_id(const void *k1, const void *k2, void *ctx);
0245 
0246 /* bpf_attach_type_input_str - convert the provided attach type value into a
0247  * textual representation that we accept for input purposes.
0248  *
0249  * This function is similar in nature to libbpf_bpf_attach_type_str, but
0250  * recognizes some attach type names that have been used by the program in the
0251  * past and which do not follow the string inference scheme that libbpf uses.
0252  * These textual representations should only be used for user input.
0253  *
0254  * @t: The attach type
0255  * Returns a pointer to a static string identifying the attach type. NULL is
0256  * returned for unknown bpf_attach_type values.
0257  */
0258 const char *bpf_attach_type_input_str(enum bpf_attach_type t);
0259 
0260 static inline void *u32_as_hash_field(__u32 x)
0261 {
0262     return (void *)(uintptr_t)x;
0263 }
0264 
0265 static inline __u32 hash_field_as_u32(const void *x)
0266 {
0267     return (__u32)(uintptr_t)x;
0268 }
0269 
0270 static inline bool hashmap__empty(struct hashmap *map)
0271 {
0272     return map ? hashmap__size(map) == 0 : true;
0273 }
0274 
0275 #endif