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 39 /* BFD and kernel.h both define GCC_VERSION, differently */ 40 #undef GCC_VERSION 41 #include <stdbool.h> 42 #include <stdio.h> 43 #include <linux/bpf.h> 44 #include <linux/kernel.h> 45 46 #include "json_writer.h" 47 48 #define ptr_to_u64(ptr) ((__u64)(unsigned long)(ptr)) 49 50 #define NEXT_ARG() ({ argc--; argv++; if (argc < 0) usage(); }) 51 #define NEXT_ARGP() ({ (*argc)--; (*argv)++; if (*argc < 0) usage(); }) 52 #define BAD_ARG() ({ p_err("what is '%s'?\n", *argv); -1; }) 53 54 #define ERR_MAX_LEN 1024 55 56 #define BPF_TAG_FMT "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx" 57 58 #define HELP_SPEC_PROGRAM \ 59 "PROG := { id PROG_ID | pinned FILE | tag PROG_TAG }" 60 #define HELP_SPEC_OPTIONS \ 61 "OPTIONS := { {-j|--json} [{-p|--pretty}] }" 62 63 enum bpf_obj_type { 64 BPF_OBJ_UNKNOWN, 65 BPF_OBJ_PROG, 66 BPF_OBJ_MAP, 67 }; 68 69 extern const char *bin_name; 70 71 extern json_writer_t *json_wtr; 72 extern bool json_output; 73 74 bool is_prefix(const char *pfx, const char *str); 75 void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep); 76 void usage(void) __attribute__((noreturn)); 77 78 struct cmd { 79 const char *cmd; 80 int (*func)(int argc, char **argv); 81 }; 82 83 int cmd_select(const struct cmd *cmds, int argc, char **argv, 84 int (*help)(int argc, char **argv)); 85 86 int get_fd_type(int fd); 87 const char *get_fd_type_name(enum bpf_obj_type type); 88 char *get_fdinfo(int fd, const char *key); 89 int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type); 90 int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32)); 91 92 int do_prog(int argc, char **arg); 93 int do_map(int argc, char **arg); 94 95 int prog_parse_fd(int *argc, char ***argv); 96 97 void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes); 98 void print_hex_data_json(uint8_t *data, size_t len); 99 100 static inline void p_err(const char *fmt, ...) 101 { 102 va_list ap; 103 104 va_start(ap, fmt); 105 if (json_output) { 106 jsonw_start_object(json_wtr); 107 jsonw_name(json_wtr, "error"); 108 jsonw_vprintf_enquote(json_wtr, fmt, ap); 109 jsonw_end_object(json_wtr); 110 } else { 111 fprintf(stderr, "Error: "); 112 vfprintf(stderr, fmt, ap); 113 fprintf(stderr, "\n"); 114 } 115 va_end(ap); 116 } 117 118 static inline void p_info(const char *fmt, ...) 119 { 120 va_list ap; 121 122 if (json_output) 123 return; 124 125 va_start(ap, fmt); 126 vfprintf(stderr, fmt, ap); 127 fprintf(stderr, "\n"); 128 va_end(ap); 129 } 130 131 #endif 132