blob: 2ff2a361af0dfdc7d812ce9307a8045a8716f080 [file] [log] [blame]
Jakub Kicinski71bb4282017-10-04 20:10:04 -07001/*
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
Jakub Kicinskic9c35992017-10-09 10:30:13 -070039/* BFD and kernel.h both define GCC_VERSION, differently */
40#undef GCC_VERSION
Jakub Kicinski71bb4282017-10-04 20:10:04 -070041#include <stdbool.h>
42#include <stdio.h>
43#include <linux/bpf.h>
Jakub Kicinskic9c35992017-10-09 10:30:13 -070044#include <linux/kernel.h>
Jakub Kicinski71bb4282017-10-04 20:10:04 -070045
Quentin Monnetd35efba2017-10-23 09:24:07 -070046#include "json_writer.h"
47
Jakub Kicinski71bb4282017-10-04 20:10:04 -070048#define ptr_to_u64(ptr) ((__u64)(unsigned long)(ptr))
49
Jakub Kicinski71bb4282017-10-04 20:10:04 -070050#define NEXT_ARG() ({ argc--; argv++; if (argc < 0) usage(); })
51#define NEXT_ARGP() ({ (*argc)--; (*argv)++; if (*argc < 0) usage(); })
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -070052#define BAD_ARG() ({ p_err("what is '%s'?\n", *argv); -1; })
Jakub Kicinski71bb4282017-10-04 20:10:04 -070053
Quentin Monnet3fc27b72017-10-24 20:11:28 -070054#define ERR_MAX_LEN 1024
55
Jakub Kicinski2dc7c1f2017-10-16 10:12:54 -070056#define BPF_TAG_FMT "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
Jakub Kicinski71bb4282017-10-04 20:10:04 -070057
58#define HELP_SPEC_PROGRAM \
59 "PROG := { id PROG_ID | pinned FILE | tag PROG_TAG }"
Quentin Monnet0641c3c2017-10-23 09:24:16 -070060#define HELP_SPEC_OPTIONS \
61 "OPTIONS := { {-j|--json} [{-p|--pretty}] }"
Jakub Kicinski71bb4282017-10-04 20:10:04 -070062
63enum bpf_obj_type {
64 BPF_OBJ_UNKNOWN,
65 BPF_OBJ_PROG,
66 BPF_OBJ_MAP,
67};
68
69extern const char *bin_name;
70
Quentin Monnetd35efba2017-10-23 09:24:07 -070071extern json_writer_t *json_wtr;
72extern bool json_output;
73
Quentin Monnet0b1c27db2017-11-03 13:59:07 -070074void p_err(const char *fmt, ...);
75void p_info(const char *fmt, ...);
76
Jakub Kicinski71bb4282017-10-04 20:10:04 -070077bool is_prefix(const char *pfx, const char *str);
Quentin Monnet9cbe1f582017-10-19 15:46:19 -070078void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep);
Jakub Kicinski71bb4282017-10-04 20:10:04 -070079void usage(void) __attribute__((noreturn));
80
81struct cmd {
82 const char *cmd;
83 int (*func)(int argc, char **argv);
84};
85
86int cmd_select(const struct cmd *cmds, int argc, char **argv,
87 int (*help)(int argc, char **argv));
88
89int get_fd_type(int fd);
90const char *get_fd_type_name(enum bpf_obj_type type);
91char *get_fdinfo(int fd, const char *key);
Prashant Bhole18527192017-11-08 13:55:47 +090092int open_obj_pinned(char *path);
Jakub Kicinski71bb4282017-10-04 20:10:04 -070093int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type);
94int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32));
95
96int do_prog(int argc, char **arg);
97int do_map(int argc, char **arg);
98
99int prog_parse_fd(int *argc, char ***argv);
100
101void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes);
Quentin Monnetf05e2c32017-10-23 09:24:10 -0700102void print_hex_data_json(uint8_t *data, size_t len);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700103
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700104#endif