Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 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 | |
Jakub Kicinski | c9c3599 | 2017-10-09 10:30:13 -0700 | [diff] [blame] | 39 | /* BFD and kernel.h both define GCC_VERSION, differently */ |
| 40 | #undef GCC_VERSION |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 41 | #include <stdbool.h> |
| 42 | #include <stdio.h> |
| 43 | #include <linux/bpf.h> |
Jakub Kicinski | c9c3599 | 2017-10-09 10:30:13 -0700 | [diff] [blame] | 44 | #include <linux/kernel.h> |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 45 | |
| 46 | #define err(msg...) fprintf(stderr, "Error: " msg) |
| 47 | #define warn(msg...) fprintf(stderr, "Warning: " msg) |
| 48 | #define info(msg...) fprintf(stderr, msg) |
| 49 | |
| 50 | #define ptr_to_u64(ptr) ((__u64)(unsigned long)(ptr)) |
| 51 | |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 52 | #define NEXT_ARG() ({ argc--; argv++; if (argc < 0) usage(); }) |
| 53 | #define NEXT_ARGP() ({ (*argc)--; (*argv)++; if (*argc < 0) usage(); }) |
| 54 | #define BAD_ARG() ({ err("what is '%s'?\n", *argv); -1; }) |
| 55 | |
Jakub Kicinski | 2dc7c1f | 2017-10-16 10:12:54 -0700 | [diff] [blame^] | 56 | #define BPF_TAG_FMT "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx" |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 57 | |
| 58 | #define HELP_SPEC_PROGRAM \ |
| 59 | "PROG := { id PROG_ID | pinned FILE | tag PROG_TAG }" |
| 60 | |
| 61 | enum bpf_obj_type { |
| 62 | BPF_OBJ_UNKNOWN, |
| 63 | BPF_OBJ_PROG, |
| 64 | BPF_OBJ_MAP, |
| 65 | }; |
| 66 | |
| 67 | extern const char *bin_name; |
| 68 | |
| 69 | bool is_prefix(const char *pfx, const char *str); |
| 70 | void print_hex(void *arg, unsigned int n, const char *sep); |
| 71 | void usage(void) __attribute__((noreturn)); |
| 72 | |
| 73 | struct cmd { |
| 74 | const char *cmd; |
| 75 | int (*func)(int argc, char **argv); |
| 76 | }; |
| 77 | |
| 78 | int cmd_select(const struct cmd *cmds, int argc, char **argv, |
| 79 | int (*help)(int argc, char **argv)); |
| 80 | |
| 81 | int get_fd_type(int fd); |
| 82 | const char *get_fd_type_name(enum bpf_obj_type type); |
| 83 | char *get_fdinfo(int fd, const char *key); |
| 84 | int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type); |
| 85 | int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32)); |
| 86 | |
| 87 | int do_prog(int argc, char **arg); |
| 88 | int do_map(int argc, char **arg); |
| 89 | |
| 90 | int prog_parse_fd(int *argc, char ***argv); |
| 91 | |
| 92 | void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes); |
| 93 | |
| 94 | #endif |