2 * Copyright (C) 2017-2018 Netronome Systems, Inc.
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.
9 * The BSD 2-Clause License:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
37 /* BFD and kernel.h both define GCC_VERSION, differently */
41 #include <linux/bpf.h>
42 #include <linux/compiler.h>
43 #include <linux/kernel.h>
44 #include <linux/hashtable.h>
45 #include <tools/libc_compat.h>
47 #include "json_writer.h"
49 #define ptr_to_u64(ptr) ((__u64)(unsigned long)(ptr))
51 #define NEXT_ARG() ({ argc--; argv++; if (argc < 0) usage(); })
52 #define NEXT_ARGP() ({ (*argc)--; (*argv)++; if (*argc < 0) usage(); })
53 #define BAD_ARG() ({ p_err("what is '%s'?", *argv); -1; })
54 #define GET_ARG() ({ argc--; *argv++; })
55 #define REQ_ARGS(cnt) \
61 p_err("'%s' needs at least %d arguments, %d found", \
62 argv[-1], _cnt, argc); \
70 #define ERR_MAX_LEN 1024
72 #define BPF_TAG_FMT "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
74 #define HELP_SPEC_PROGRAM \
75 "PROG := { id PROG_ID | pinned FILE | tag PROG_TAG }"
76 #define HELP_SPEC_OPTIONS \
77 "OPTIONS := { {-j|--json} [{-p|--pretty}] | {-f|--bpffs} }"
78 #define HELP_SPEC_MAP \
79 "MAP := { id MAP_ID | pinned FILE }"
87 extern const char *bin_name;
89 extern json_writer_t *json_wtr;
90 extern bool json_output;
91 extern bool show_pinned;
92 extern struct pinned_obj_table prog_table;
93 extern struct pinned_obj_table map_table;
95 void p_err(const char *fmt, ...);
96 void p_info(const char *fmt, ...);
98 bool is_prefix(const char *pfx, const char *str);
99 void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep);
100 void usage(void) __noreturn;
102 struct pinned_obj_table {
103 DECLARE_HASHTABLE(table, 16);
109 struct hlist_node hash;
112 int build_pinned_obj_table(struct pinned_obj_table *table,
113 enum bpf_obj_type type);
114 void delete_pinned_obj_table(struct pinned_obj_table *tab);
115 void print_dev_plain(__u32 ifindex, __u64 ns_dev, __u64 ns_inode);
116 void print_dev_json(__u32 ifindex, __u64 ns_dev, __u64 ns_inode);
120 int (*func)(int argc, char **argv);
123 int cmd_select(const struct cmd *cmds, int argc, char **argv,
124 int (*help)(int argc, char **argv));
126 int get_fd_type(int fd);
127 const char *get_fd_type_name(enum bpf_obj_type type);
128 char *get_fdinfo(int fd, const char *key);
129 int open_obj_pinned(char *path, bool quiet);
130 int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type);
131 int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32));
132 int do_pin_fd(int fd, const char *name);
134 int do_prog(int argc, char **arg);
135 int do_map(int argc, char **arg);
136 int do_event_pipe(int argc, char **argv);
137 int do_cgroup(int argc, char **arg);
138 int do_perf(int argc, char **arg);
140 int prog_parse_fd(int *argc, char ***argv);
141 int map_parse_fd(int *argc, char ***argv);
142 int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len);
144 void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
146 void print_data_json(uint8_t *data, size_t len);
147 void print_hex_data_json(uint8_t *data, size_t len);
149 unsigned int get_page_size(void);
150 unsigned int get_possible_cpus(void);
151 const char *ifindex_to_bfd_name_ns(__u32 ifindex, __u64 ns_dev, __u64 ns_ino);
154 const struct btf *btf;
159 /* btf_dumper_type - print data along with type information
160 * @d: an instance containing context for dumping types
161 * @type_id: index in btf->types array. this points to the type to be dumped
162 * @data: pointer the actual data, i.e. the values to be printed
164 * Returns zero on success and negative error code otherwise
166 int btf_dumper_type(const struct btf_dumper *d, __u32 type_id,