]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - tools/bpf/bpftool/main.h
bpftool: make libbfd optional
[mirror_ubuntu-bionic-kernel.git] / tools / bpf / bpftool / main.h
CommitLineData
71bb428f
JK
1/*
2 * Copyright (C) 2017 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
34/* Author: Jakub Kicinski <kubakici@wp.pl> */
35
36#ifndef __BPF_TOOL_H
37#define __BPF_TOOL_H
38
c9c35995
JK
39/* BFD and kernel.h both define GCC_VERSION, differently */
40#undef GCC_VERSION
71bb428f
JK
41#include <stdbool.h>
42#include <stdio.h>
43#include <linux/bpf.h>
7868620a 44#include <linux/compiler.h>
c9c35995 45#include <linux/kernel.h>
4990f1f4 46#include <linux/hashtable.h>
71bb428f 47
d35efba9
QM
48#include "json_writer.h"
49
71bb428f
JK
50#define ptr_to_u64(ptr) ((__u64)(unsigned long)(ptr))
51
71bb428f
JK
52#define NEXT_ARG() ({ argc--; argv++; if (argc < 0) usage(); })
53#define NEXT_ARGP() ({ (*argc)--; (*argv)++; if (*argc < 0) usage(); })
0d954eeb 54#define BAD_ARG() ({ p_err("what is '%s'?", *argv); -1; })
71bb428f 55
3fc27b71
QM
56#define ERR_MAX_LEN 1024
57
2dc7c1fe 58#define BPF_TAG_FMT "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
71bb428f
JK
59
60#define HELP_SPEC_PROGRAM \
61 "PROG := { id PROG_ID | pinned FILE | tag PROG_TAG }"
0641c3c8 62#define HELP_SPEC_OPTIONS \
c541b734 63 "OPTIONS := { {-j|--json} [{-p|--pretty}] | {-f|--bpffs} }"
71bb428f
JK
64
65enum bpf_obj_type {
66 BPF_OBJ_UNKNOWN,
67 BPF_OBJ_PROG,
68 BPF_OBJ_MAP,
69};
70
71extern const char *bin_name;
72
d35efba9
QM
73extern json_writer_t *json_wtr;
74extern bool json_output;
c541b734 75extern bool show_pinned;
4990f1f4
PB
76extern struct pinned_obj_table prog_table;
77extern struct pinned_obj_table map_table;
d35efba9 78
0b1c27db
QM
79void p_err(const char *fmt, ...);
80void p_info(const char *fmt, ...);
81
71bb428f 82bool is_prefix(const char *pfx, const char *str);
9cbe1f58 83void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep);
7868620a 84void usage(void) __noreturn;
71bb428f 85
4990f1f4
PB
86struct pinned_obj_table {
87 DECLARE_HASHTABLE(table, 16);
88};
89
90struct pinned_obj {
91 __u32 id;
92 char *path;
93 struct hlist_node hash;
94};
95
96int build_pinned_obj_table(struct pinned_obj_table *table,
97 enum bpf_obj_type type);
98void delete_pinned_obj_table(struct pinned_obj_table *tab);
99
71bb428f
JK
100struct cmd {
101 const char *cmd;
102 int (*func)(int argc, char **argv);
103};
104
105int cmd_select(const struct cmd *cmds, int argc, char **argv,
106 int (*help)(int argc, char **argv));
107
108int get_fd_type(int fd);
109const char *get_fd_type_name(enum bpf_obj_type type);
110char *get_fdinfo(int fd, const char *key);
e7cfe3a2 111int open_obj_pinned(char *path, bool quiet);
71bb428f
JK
112int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type);
113int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32));
114
115int do_prog(int argc, char **arg);
116int do_map(int argc, char **arg);
117
118int prog_parse_fd(int *argc, char ***argv);
119
66155515 120#ifdef HAVE_LIBBFD_SUPPORT
71bb428f 121void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes);
66155515
SF
122int disasm_init(void);
123#else
124static inline
125void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes)
126{
127}
128static inline int disasm_init(void)
129{
130 p_err("No libbfd support");
131 return -1;
132}
133#endif
f05e2c32 134void print_hex_data_json(uint8_t *data, size_t len);
71bb428f
JK
135
136#endif