blob: 2761981669c8284062ef7c7b429c143d0a9ae3d6 [file] [log] [blame]
Jakub Kicinski71bb4282017-10-04 20:10:04 -07001/*
Jakub Kicinskie64d5252018-05-03 18:37:15 -07002 * Copyright (C) 2017-2018 Netronome Systems, Inc.
Jakub Kicinski71bb4282017-10-04 20:10:04 -07003 *
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
Jakub Kicinski71bb4282017-10-04 20:10:04 -070034#ifndef __BPF_TOOL_H
35#define __BPF_TOOL_H
36
Jakub Kicinskic9c35992017-10-09 10:30:13 -070037/* BFD and kernel.h both define GCC_VERSION, differently */
38#undef GCC_VERSION
Jakub Kicinski71bb4282017-10-04 20:10:04 -070039#include <stdbool.h>
40#include <stdio.h>
41#include <linux/bpf.h>
Quentin Monnet78686202017-11-28 17:44:29 -080042#include <linux/compiler.h>
Jakub Kicinskic9c35992017-10-09 10:30:13 -070043#include <linux/kernel.h>
Prashant Bhole4990f1f2017-11-08 13:55:48 +090044#include <linux/hashtable.h>
Jakub Kicinski531b0142018-07-10 14:43:05 -070045#include <tools/libc_compat.h>
Jakub Kicinski71bb4282017-10-04 20:10:04 -070046
Quentin Monnetd35efba2017-10-23 09:24:07 -070047#include "json_writer.h"
48
Jakub Kicinski71bb4282017-10-04 20:10:04 -070049#define ptr_to_u64(ptr) ((__u64)(unsigned long)(ptr))
50
Jakub Kicinski71bb4282017-10-04 20:10:04 -070051#define NEXT_ARG() ({ argc--; argv++; if (argc < 0) usage(); })
52#define NEXT_ARGP() ({ (*argc)--; (*argv)++; if (*argc < 0) usage(); })
Quentin Monnet0d954ee2017-11-28 17:44:31 -080053#define BAD_ARG() ({ p_err("what is '%s'?", *argv); -1; })
Jakub Kicinski8d1fc3d2018-07-10 14:42:57 -070054#define GET_ARG() ({ argc--; *argv++; })
55#define REQ_ARGS(cnt) \
56 ({ \
57 int _cnt = (cnt); \
58 bool _res; \
59 \
60 if (argc < _cnt) { \
61 p_err("'%s' needs at least %d arguments, %d found", \
62 argv[-1], _cnt, argc); \
63 _res = false; \
64 } else { \
65 _res = true; \
66 } \
67 _res; \
68 })
Jakub Kicinski71bb4282017-10-04 20:10:04 -070069
Quentin Monnet3fc27b72017-10-24 20:11:28 -070070#define ERR_MAX_LEN 1024
71
Jakub Kicinski2dc7c1f2017-10-16 10:12:54 -070072#define BPF_TAG_FMT "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
Jakub Kicinski71bb4282017-10-04 20:10:04 -070073
74#define HELP_SPEC_PROGRAM \
75 "PROG := { id PROG_ID | pinned FILE | tag PROG_TAG }"
Quentin Monnet0641c3c2017-10-23 09:24:16 -070076#define HELP_SPEC_OPTIONS \
John Fastabendc034a172018-10-15 11:19:55 -070077 "OPTIONS := { {-j|--json} [{-p|--pretty}] | {-f|--bpffs} | {-m|--mapcompat}"
Jakub Kicinski3ff5a4d2018-07-10 14:43:07 -070078#define HELP_SPEC_MAP \
79 "MAP := { id MAP_ID | pinned FILE }"
Jakub Kicinski71bb4282017-10-04 20:10:04 -070080
Quentin Monnet99a44bef2018-11-30 16:25:48 +000081static const char * const prog_type_name[] = {
82 [BPF_PROG_TYPE_UNSPEC] = "unspec",
83 [BPF_PROG_TYPE_SOCKET_FILTER] = "socket_filter",
84 [BPF_PROG_TYPE_KPROBE] = "kprobe",
85 [BPF_PROG_TYPE_SCHED_CLS] = "sched_cls",
86 [BPF_PROG_TYPE_SCHED_ACT] = "sched_act",
87 [BPF_PROG_TYPE_TRACEPOINT] = "tracepoint",
88 [BPF_PROG_TYPE_XDP] = "xdp",
89 [BPF_PROG_TYPE_PERF_EVENT] = "perf_event",
90 [BPF_PROG_TYPE_CGROUP_SKB] = "cgroup_skb",
91 [BPF_PROG_TYPE_CGROUP_SOCK] = "cgroup_sock",
92 [BPF_PROG_TYPE_LWT_IN] = "lwt_in",
93 [BPF_PROG_TYPE_LWT_OUT] = "lwt_out",
94 [BPF_PROG_TYPE_LWT_XMIT] = "lwt_xmit",
95 [BPF_PROG_TYPE_SOCK_OPS] = "sock_ops",
96 [BPF_PROG_TYPE_SK_SKB] = "sk_skb",
97 [BPF_PROG_TYPE_CGROUP_DEVICE] = "cgroup_device",
98 [BPF_PROG_TYPE_SK_MSG] = "sk_msg",
99 [BPF_PROG_TYPE_RAW_TRACEPOINT] = "raw_tracepoint",
100 [BPF_PROG_TYPE_CGROUP_SOCK_ADDR] = "cgroup_sock_addr",
101 [BPF_PROG_TYPE_LWT_SEG6LOCAL] = "lwt_seg6local",
102 [BPF_PROG_TYPE_LIRC_MODE2] = "lirc_mode2",
103 [BPF_PROG_TYPE_SK_REUSEPORT] = "sk_reuseport",
104 [BPF_PROG_TYPE_FLOW_DISSECTOR] = "flow_dissector",
105};
106
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700107enum bpf_obj_type {
108 BPF_OBJ_UNKNOWN,
109 BPF_OBJ_PROG,
110 BPF_OBJ_MAP,
111};
112
113extern const char *bin_name;
114
Quentin Monnetd35efba2017-10-23 09:24:07 -0700115extern json_writer_t *json_wtr;
116extern bool json_output;
Prashant Bholec541b732017-11-08 13:55:49 +0900117extern bool show_pinned;
John Fastabendc034a172018-10-15 11:19:55 -0700118extern int bpf_flags;
Prashant Bhole4990f1f2017-11-08 13:55:48 +0900119extern struct pinned_obj_table prog_table;
120extern struct pinned_obj_table map_table;
Quentin Monnetd35efba2017-10-23 09:24:07 -0700121
Quentin Monnet0b1c27d2017-11-03 13:59:07 -0700122void p_err(const char *fmt, ...);
123void p_info(const char *fmt, ...);
124
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700125bool is_prefix(const char *pfx, const char *str);
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700126void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep);
Quentin Monnet78686202017-11-28 17:44:29 -0800127void usage(void) __noreturn;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700128
Quentin Monnet8302b9b2018-11-07 12:29:30 +0000129void set_max_rlimit(void);
130
Prashant Bhole4990f1f2017-11-08 13:55:48 +0900131struct pinned_obj_table {
132 DECLARE_HASHTABLE(table, 16);
133};
134
135struct pinned_obj {
136 __u32 id;
137 char *path;
138 struct hlist_node hash;
139};
140
141int build_pinned_obj_table(struct pinned_obj_table *table,
142 enum bpf_obj_type type);
143void delete_pinned_obj_table(struct pinned_obj_table *tab);
Jakub Kicinski52262212017-12-27 18:39:10 -0800144void print_dev_plain(__u32 ifindex, __u64 ns_dev, __u64 ns_inode);
145void print_dev_json(__u32 ifindex, __u64 ns_dev, __u64 ns_inode);
Prashant Bhole4990f1f2017-11-08 13:55:48 +0900146
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700147struct cmd {
148 const char *cmd;
149 int (*func)(int argc, char **argv);
150};
151
152int cmd_select(const struct cmd *cmds, int argc, char **argv,
153 int (*help)(int argc, char **argv));
154
155int get_fd_type(int fd);
156const char *get_fd_type_name(enum bpf_obj_type type);
157char *get_fdinfo(int fd, const char *key);
Quentin Monnetf1209192018-11-08 11:52:27 +0000158int open_obj_pinned(char *path, bool quiet);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700159int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type);
Stanislav Fomichev77380992018-11-09 08:21:44 -0800160int mount_bpffs_for_pin(const char *name);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700161int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32));
Roman Gushchin49a086c2017-12-13 15:18:53 +0000162int do_pin_fd(int fd, const char *name);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700163
164int do_prog(int argc, char **arg);
165int do_map(int argc, char **arg);
Jakub Kicinskif412eed2018-05-03 18:37:16 -0700166int do_event_pipe(int argc, char **argv);
Roman Gushchin5ccda642017-12-13 15:18:54 +0000167int do_cgroup(int argc, char **arg);
Yonghong Songb04df402018-05-24 11:21:58 -0700168int do_perf(int argc, char **arg);
Yonghong Songf6f3bac2018-09-05 16:58:06 -0700169int do_net(int argc, char **arg);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700170
Jakub Kicinski0b592b5a2018-10-15 16:30:36 -0700171int parse_u32_arg(int *argc, char ***argv, __u32 *val, const char *what);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700172int prog_parse_fd(int *argc, char ***argv);
Jakub Kicinski3ff5a4d2018-07-10 14:43:07 -0700173int map_parse_fd(int *argc, char ***argv);
Jakub Kicinskif412eed2018-05-03 18:37:16 -0700174int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700175
Stanislav Fomichev29a9c102018-11-12 13:44:10 -0800176#ifdef HAVE_LIBBFD_SUPPORT
Jiong Wange6593592018-01-16 16:05:21 -0800177void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
Jakub Kicinski3ddeac62018-10-18 11:34:55 -0700178 const char *arch, const char *disassembler_options);
Stanislav Fomichev29a9c102018-11-12 13:44:10 -0800179int disasm_init(void);
180#else
181static inline
182void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
183 const char *arch, const char *disassembler_options)
184{
185}
186static inline int disasm_init(void)
187{
188 p_err("No libbfd support");
189 return -1;
190}
191#endif
Jakub Kicinskif412eed2018-05-03 18:37:16 -0700192void print_data_json(uint8_t *data, size_t len);
Quentin Monnetf05e2c32017-10-23 09:24:10 -0700193void print_hex_data_json(uint8_t *data, size_t len);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700194
Jakub Kicinskif412eed2018-05-03 18:37:16 -0700195unsigned int get_page_size(void);
Jakub Kicinskie64d5252018-05-03 18:37:15 -0700196unsigned int get_possible_cpus(void);
Jakub Kicinski3ddeac62018-10-18 11:34:55 -0700197const char *
198ifindex_to_bfd_params(__u32 ifindex, __u64 ns_dev, __u64 ns_ino,
199 const char **opt);
Jiong Wange6593592018-01-16 16:05:21 -0800200
Okash Khawajab12d6ec2018-07-13 21:57:03 -0700201struct btf_dumper {
202 const struct btf *btf;
203 json_writer_t *jw;
204 bool is_plain_text;
205};
206
207/* btf_dumper_type - print data along with type information
208 * @d: an instance containing context for dumping types
209 * @type_id: index in btf->types array. this points to the type to be dumped
210 * @data: pointer the actual data, i.e. the values to be printed
211 *
212 * Returns zero on success and negative error code otherwise
213 */
214int btf_dumper_type(const struct btf_dumper *d, __u32 type_id,
215 const void *data);
Yonghong Song254471e2018-11-19 15:29:21 -0800216void btf_dumper_type_only(const struct btf *btf, __u32 func_type_id,
217 char *func_only, int size);
Yonghong Songf6f3bac2018-09-05 16:58:06 -0700218
219struct nlattr;
220struct ifinfomsg;
221struct tcmsg;
222int do_xdp_dump(struct ifinfomsg *ifinfo, struct nlattr **tb);
Yonghong Song7900efc2018-09-17 16:13:00 -0700223int do_filter_dump(struct tcmsg *ifinfo, struct nlattr **tb, const char *kind,
224 const char *devname, int ifindex);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700225#endif