]> Git Repo - linux.git/blame - tools/bpf/bpftool/main.h
Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux.git] / tools / bpf / bpftool / main.h
CommitLineData
71bb428f 1/*
e64d5256 2 * Copyright (C) 2017-2018 Netronome Systems, Inc.
71bb428f
JK
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
71bb428f
JK
34#ifndef __BPF_TOOL_H
35#define __BPF_TOOL_H
36
c9c35995
JK
37/* BFD and kernel.h both define GCC_VERSION, differently */
38#undef GCC_VERSION
71bb428f
JK
39#include <stdbool.h>
40#include <stdio.h>
41#include <linux/bpf.h>
7868620a 42#include <linux/compiler.h>
c9c35995 43#include <linux/kernel.h>
4990f1f4 44#include <linux/hashtable.h>
531b014e 45#include <tools/libc_compat.h>
71bb428f 46
d35efba9
QM
47#include "json_writer.h"
48
71bb428f
JK
49#define ptr_to_u64(ptr) ((__u64)(unsigned long)(ptr))
50
71bb428f
JK
51#define NEXT_ARG() ({ argc--; argv++; if (argc < 0) usage(); })
52#define NEXT_ARGP() ({ (*argc)--; (*argv)++; if (*argc < 0) usage(); })
0d954eeb 53#define BAD_ARG() ({ p_err("what is '%s'?", *argv); -1; })
8d1fc3de
JK
54#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 })
71bb428f 69
3fc27b71
QM
70#define ERR_MAX_LEN 1024
71
2dc7c1fe 72#define BPF_TAG_FMT "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
71bb428f
JK
73
74#define HELP_SPEC_PROGRAM \
75 "PROG := { id PROG_ID | pinned FILE | tag PROG_TAG }"
0641c3c8 76#define HELP_SPEC_OPTIONS \
c034a177 77 "OPTIONS := { {-j|--json} [{-p|--pretty}] | {-f|--bpffs} | {-m|--mapcompat}"
3ff5a4dc
JK
78#define HELP_SPEC_MAP \
79 "MAP := { id MAP_ID | pinned FILE }"
71bb428f
JK
80
81enum bpf_obj_type {
82 BPF_OBJ_UNKNOWN,
83 BPF_OBJ_PROG,
84 BPF_OBJ_MAP,
85};
86
87extern const char *bin_name;
88
d35efba9
QM
89extern json_writer_t *json_wtr;
90extern bool json_output;
c541b734 91extern bool show_pinned;
c034a177 92extern int bpf_flags;
4990f1f4
PB
93extern struct pinned_obj_table prog_table;
94extern struct pinned_obj_table map_table;
d35efba9 95
0b1c27db
QM
96void p_err(const char *fmt, ...);
97void p_info(const char *fmt, ...);
98
71bb428f 99bool is_prefix(const char *pfx, const char *str);
9cbe1f58 100void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep);
7868620a 101void usage(void) __noreturn;
71bb428f 102
4990f1f4
PB
103struct pinned_obj_table {
104 DECLARE_HASHTABLE(table, 16);
105};
106
107struct pinned_obj {
108 __u32 id;
109 char *path;
110 struct hlist_node hash;
111};
112
113int build_pinned_obj_table(struct pinned_obj_table *table,
114 enum bpf_obj_type type);
115void delete_pinned_obj_table(struct pinned_obj_table *tab);
52262210
JK
116void print_dev_plain(__u32 ifindex, __u64 ns_dev, __u64 ns_inode);
117void print_dev_json(__u32 ifindex, __u64 ns_dev, __u64 ns_inode);
4990f1f4 118
71bb428f
JK
119struct cmd {
120 const char *cmd;
121 int (*func)(int argc, char **argv);
122};
123
124int cmd_select(const struct cmd *cmds, int argc, char **argv,
125 int (*help)(int argc, char **argv));
126
127int get_fd_type(int fd);
128const char *get_fd_type_name(enum bpf_obj_type type);
129char *get_fdinfo(int fd, const char *key);
f120919f 130int open_obj_pinned(char *path, bool quiet);
71bb428f
JK
131int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type);
132int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32));
49a086c2 133int do_pin_fd(int fd, const char *name);
71bb428f
JK
134
135int do_prog(int argc, char **arg);
136int do_map(int argc, char **arg);
f412eed9 137int do_event_pipe(int argc, char **argv);
5ccda64d 138int do_cgroup(int argc, char **arg);
b04df400 139int do_perf(int argc, char **arg);
f6f3bac0 140int do_net(int argc, char **arg);
71bb428f 141
0b592b5a 142int parse_u32_arg(int *argc, char ***argv, __u32 *val, const char *what);
71bb428f 143int prog_parse_fd(int *argc, char ***argv);
3ff5a4dc 144int map_parse_fd(int *argc, char ***argv);
f412eed9 145int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len);
71bb428f 146
e6593596 147void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
3ddeac67 148 const char *arch, const char *disassembler_options);
f412eed9 149void print_data_json(uint8_t *data, size_t len);
f05e2c32 150void print_hex_data_json(uint8_t *data, size_t len);
71bb428f 151
f412eed9 152unsigned int get_page_size(void);
e64d5256 153unsigned int get_possible_cpus(void);
3ddeac67
JK
154const char *
155ifindex_to_bfd_params(__u32 ifindex, __u64 ns_dev, __u64 ns_ino,
156 const char **opt);
e6593596 157
b12d6ec0
OK
158struct btf_dumper {
159 const struct btf *btf;
160 json_writer_t *jw;
161 bool is_plain_text;
162};
163
164/* btf_dumper_type - print data along with type information
165 * @d: an instance containing context for dumping types
166 * @type_id: index in btf->types array. this points to the type to be dumped
167 * @data: pointer the actual data, i.e. the values to be printed
168 *
169 * Returns zero on success and negative error code otherwise
170 */
171int btf_dumper_type(const struct btf_dumper *d, __u32 type_id,
172 const void *data);
f6f3bac0
YS
173
174struct nlattr;
175struct ifinfomsg;
176struct tcmsg;
177int do_xdp_dump(struct ifinfomsg *ifinfo, struct nlattr **tb);
7900efc1
YS
178int do_filter_dump(struct tcmsg *ifinfo, struct nlattr **tb, const char *kind,
179 const char *devname, int ifindex);
71bb428f 180#endif
This page took 0.165101 seconds and 4 git commands to generate.