Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 1 | /* |
Jakub Kicinski | e64d525 | 2018-05-03 18:37:15 -0700 | [diff] [blame] | 2 | * Copyright (C) 2017-2018 Netronome Systems, Inc. |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 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> |
Quentin Monnet | 7868620 | 2017-11-28 17:44:29 -0800 | [diff] [blame] | 44 | #include <linux/compiler.h> |
Jakub Kicinski | c9c3599 | 2017-10-09 10:30:13 -0700 | [diff] [blame] | 45 | #include <linux/kernel.h> |
Prashant Bhole | 4990f1f | 2017-11-08 13:55:48 +0900 | [diff] [blame] | 46 | #include <linux/hashtable.h> |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 47 | |
Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 48 | #include "json_writer.h" |
| 49 | |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 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(); }) |
Quentin Monnet | 0d954ee | 2017-11-28 17:44:31 -0800 | [diff] [blame] | 54 | #define BAD_ARG() ({ p_err("what is '%s'?", *argv); -1; }) |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 55 | |
Quentin Monnet | 3fc27b7 | 2017-10-24 20:11:28 -0700 | [diff] [blame] | 56 | #define ERR_MAX_LEN 1024 |
| 57 | |
Jakub Kicinski | 2dc7c1f | 2017-10-16 10:12:54 -0700 | [diff] [blame] | 58 | #define BPF_TAG_FMT "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx" |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 59 | |
| 60 | #define HELP_SPEC_PROGRAM \ |
| 61 | "PROG := { id PROG_ID | pinned FILE | tag PROG_TAG }" |
Quentin Monnet | 0641c3c | 2017-10-23 09:24:16 -0700 | [diff] [blame] | 62 | #define HELP_SPEC_OPTIONS \ |
Prashant Bhole | c541b73 | 2017-11-08 13:55:49 +0900 | [diff] [blame] | 63 | "OPTIONS := { {-j|--json} [{-p|--pretty}] | {-f|--bpffs} }" |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 64 | |
| 65 | enum bpf_obj_type { |
| 66 | BPF_OBJ_UNKNOWN, |
| 67 | BPF_OBJ_PROG, |
| 68 | BPF_OBJ_MAP, |
| 69 | }; |
| 70 | |
| 71 | extern const char *bin_name; |
| 72 | |
Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 73 | extern json_writer_t *json_wtr; |
| 74 | extern bool json_output; |
Prashant Bhole | c541b73 | 2017-11-08 13:55:49 +0900 | [diff] [blame] | 75 | extern bool show_pinned; |
Prashant Bhole | 4990f1f | 2017-11-08 13:55:48 +0900 | [diff] [blame] | 76 | extern struct pinned_obj_table prog_table; |
| 77 | extern struct pinned_obj_table map_table; |
Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 78 | |
Quentin Monnet | 0b1c27db | 2017-11-03 13:59:07 -0700 | [diff] [blame] | 79 | void p_err(const char *fmt, ...); |
| 80 | void p_info(const char *fmt, ...); |
| 81 | |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 82 | bool is_prefix(const char *pfx, const char *str); |
Quentin Monnet | 9cbe1f58 | 2017-10-19 15:46:19 -0700 | [diff] [blame] | 83 | void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep); |
Quentin Monnet | 7868620 | 2017-11-28 17:44:29 -0800 | [diff] [blame] | 84 | void usage(void) __noreturn; |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 85 | |
Prashant Bhole | 4990f1f | 2017-11-08 13:55:48 +0900 | [diff] [blame] | 86 | struct pinned_obj_table { |
| 87 | DECLARE_HASHTABLE(table, 16); |
| 88 | }; |
| 89 | |
| 90 | struct pinned_obj { |
| 91 | __u32 id; |
| 92 | char *path; |
| 93 | struct hlist_node hash; |
| 94 | }; |
| 95 | |
| 96 | int build_pinned_obj_table(struct pinned_obj_table *table, |
| 97 | enum bpf_obj_type type); |
| 98 | void delete_pinned_obj_table(struct pinned_obj_table *tab); |
Jakub Kicinski | 5226221 | 2017-12-27 18:39:10 -0800 | [diff] [blame] | 99 | void print_dev_plain(__u32 ifindex, __u64 ns_dev, __u64 ns_inode); |
| 100 | void print_dev_json(__u32 ifindex, __u64 ns_dev, __u64 ns_inode); |
Prashant Bhole | 4990f1f | 2017-11-08 13:55:48 +0900 | [diff] [blame] | 101 | |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 102 | struct cmd { |
| 103 | const char *cmd; |
| 104 | int (*func)(int argc, char **argv); |
| 105 | }; |
| 106 | |
| 107 | int cmd_select(const struct cmd *cmds, int argc, char **argv, |
| 108 | int (*help)(int argc, char **argv)); |
| 109 | |
| 110 | int get_fd_type(int fd); |
| 111 | const char *get_fd_type_name(enum bpf_obj_type type); |
| 112 | char *get_fdinfo(int fd, const char *key); |
Prashant Bhole | 1852719 | 2017-11-08 13:55:47 +0900 | [diff] [blame] | 113 | int open_obj_pinned(char *path); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 114 | int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type); |
| 115 | int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32)); |
Roman Gushchin | 49a086c | 2017-12-13 15:18:53 +0000 | [diff] [blame] | 116 | int do_pin_fd(int fd, const char *name); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 117 | |
| 118 | int do_prog(int argc, char **arg); |
| 119 | int do_map(int argc, char **arg); |
Jakub Kicinski | f412eed | 2018-05-03 18:37:16 -0700 | [diff] [blame] | 120 | int do_event_pipe(int argc, char **argv); |
Roman Gushchin | 5ccda64 | 2017-12-13 15:18:54 +0000 | [diff] [blame] | 121 | int do_cgroup(int argc, char **arg); |
Yonghong Song | b04df40 | 2018-05-24 11:21:58 -0700 | [diff] [blame] | 122 | int do_perf(int argc, char **arg); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 123 | |
| 124 | int prog_parse_fd(int *argc, char ***argv); |
Jakub Kicinski | f412eed | 2018-05-03 18:37:16 -0700 | [diff] [blame] | 125 | int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 126 | |
Jiong Wang | e659359 | 2018-01-16 16:05:21 -0800 | [diff] [blame] | 127 | void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, |
| 128 | const char *arch); |
Jakub Kicinski | f412eed | 2018-05-03 18:37:16 -0700 | [diff] [blame] | 129 | void print_data_json(uint8_t *data, size_t len); |
Quentin Monnet | f05e2c3 | 2017-10-23 09:24:10 -0700 | [diff] [blame] | 130 | void print_hex_data_json(uint8_t *data, size_t len); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 131 | |
Jakub Kicinski | f412eed | 2018-05-03 18:37:16 -0700 | [diff] [blame] | 132 | unsigned int get_page_size(void); |
Jakub Kicinski | e64d525 | 2018-05-03 18:37:15 -0700 | [diff] [blame] | 133 | unsigned int get_possible_cpus(void); |
Jiong Wang | e659359 | 2018-01-16 16:05:21 -0800 | [diff] [blame] | 134 | const char *ifindex_to_bfd_name_ns(__u32 ifindex, __u64 ns_dev, __u64 ns_ino); |
| 135 | |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 136 | #endif |