1 /* 2 * Common eBPF ELF object loading operations. 3 * 4 * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org> 5 * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com> 6 * Copyright (C) 2015 Huawei Inc. 7 * 8 * This program is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU Lesser General Public 10 * License as published by the Free Software Foundation; 11 * version 2.1 of the License (not later!) 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU Lesser General Public License for more details. 17 * 18 * You should have received a copy of the GNU Lesser General Public 19 * License along with this program; if not, see <http://www.gnu.org/licenses> 20 */ 21 #ifndef __BPF_LIBBPF_H 22 #define __BPF_LIBBPF_H 23 24 #include <stdio.h> 25 #include <stdbool.h> 26 #include <linux/err.h> 27 28 enum libbpf_errno { 29 __LIBBPF_ERRNO__START = 4000, 30 31 /* Something wrong in libelf */ 32 LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START, 33 LIBBPF_ERRNO__FORMAT, /* BPF object format invalid */ 34 LIBBPF_ERRNO__KVERSION, /* Incorrect or no 'version' section */ 35 LIBBPF_ERRNO__ENDIAN, /* Endian mismatch */ 36 LIBBPF_ERRNO__INTERNAL, /* Internal error in libbpf */ 37 LIBBPF_ERRNO__RELOC, /* Relocation failed */ 38 LIBBPF_ERRNO__LOAD, /* Load program failure for unknown reason */ 39 LIBBPF_ERRNO__VERIFY, /* Kernel verifier blocks program loading */ 40 LIBBPF_ERRNO__PROG2BIG, /* Program too big */ 41 LIBBPF_ERRNO__KVER, /* Incorrect kernel version */ 42 __LIBBPF_ERRNO__END, 43 }; 44 45 int libbpf_strerror(int err, char *buf, size_t size); 46 47 /* 48 * In include/linux/compiler-gcc.h, __printf is defined. However 49 * it should be better if libbpf.h doesn't depend on Linux header file. 50 * So instead of __printf, here we use gcc attribute directly. 51 */ 52 typedef int (*libbpf_print_fn_t)(const char *, ...) 53 __attribute__((format(printf, 1, 2))); 54 55 void libbpf_set_print(libbpf_print_fn_t warn, 56 libbpf_print_fn_t info, 57 libbpf_print_fn_t debug); 58 59 /* Hide internal to user */ 60 struct bpf_object; 61 62 struct bpf_object *bpf_object__open(const char *path); 63 struct bpf_object *bpf_object__open_buffer(void *obj_buf, 64 size_t obj_buf_sz, 65 const char *name); 66 void bpf_object__close(struct bpf_object *object); 67 68 /* Load/unload object into/from kernel */ 69 int bpf_object__load(struct bpf_object *obj); 70 int bpf_object__unload(struct bpf_object *obj); 71 const char *bpf_object__name(struct bpf_object *obj); 72 unsigned int bpf_object__kversion(struct bpf_object *obj); 73 74 struct bpf_object *bpf_object__next(struct bpf_object *prev); 75 #define bpf_object__for_each_safe(pos, tmp) \ 76 for ((pos) = bpf_object__next(NULL), \ 77 (tmp) = bpf_object__next(pos); \ 78 (pos) != NULL; \ 79 (pos) = (tmp), (tmp) = bpf_object__next(tmp)) 80 81 /* Accessors of bpf_program. */ 82 struct bpf_program; 83 struct bpf_program *bpf_program__next(struct bpf_program *prog, 84 struct bpf_object *obj); 85 86 #define bpf_object__for_each_program(pos, obj) \ 87 for ((pos) = bpf_program__next(NULL, (obj)); \ 88 (pos) != NULL; \ 89 (pos) = bpf_program__next((pos), (obj))) 90 91 typedef void (*bpf_program_clear_priv_t)(struct bpf_program *, 92 void *); 93 94 int bpf_program__set_priv(struct bpf_program *prog, void *priv, 95 bpf_program_clear_priv_t clear_priv); 96 97 void *bpf_program__priv(struct bpf_program *prog); 98 99 const char *bpf_program__title(struct bpf_program *prog, bool needs_copy); 100 101 int bpf_program__fd(struct bpf_program *prog); 102 103 struct bpf_insn; 104 105 /* 106 * Libbpf allows callers to adjust BPF programs before being loaded 107 * into kernel. One program in an object file can be transform into 108 * multiple variants to be attached to different code. 109 * 110 * bpf_program_prep_t, bpf_program__set_prep and bpf_program__nth_fd 111 * are APIs for this propose. 112 * 113 * - bpf_program_prep_t: 114 * It defines 'preprocessor', which is a caller defined function 115 * passed to libbpf through bpf_program__set_prep(), and will be 116 * called before program is loaded. The processor should adjust 117 * the program one time for each instances according to the number 118 * passed to it. 119 * 120 * - bpf_program__set_prep: 121 * Attachs a preprocessor to a BPF program. The number of instances 122 * whould be created is also passed through this function. 123 * 124 * - bpf_program__nth_fd: 125 * After the program is loaded, get resuling fds from bpf program for 126 * each instances. 127 * 128 * If bpf_program__set_prep() is not used, the program whould be loaded 129 * without adjustment during bpf_object__load(). The program has only 130 * one instance. In this case bpf_program__fd(prog) is equal to 131 * bpf_program__nth_fd(prog, 0). 132 */ 133 134 struct bpf_prog_prep_result { 135 /* 136 * If not NULL, load new instruction array. 137 * If set to NULL, don't load this instance. 138 */ 139 struct bpf_insn *new_insn_ptr; 140 int new_insn_cnt; 141 142 /* If not NULL, result fd is set to it */ 143 int *pfd; 144 }; 145 146 /* 147 * Parameters of bpf_program_prep_t: 148 * - prog: The bpf_program being loaded. 149 * - n: Index of instance being generated. 150 * - insns: BPF instructions array. 151 * - insns_cnt:Number of instructions in insns. 152 * - res: Output parameter, result of transformation. 153 * 154 * Return value: 155 * - Zero: pre-processing success. 156 * - Non-zero: pre-processing, stop loading. 157 */ 158 typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n, 159 struct bpf_insn *insns, int insns_cnt, 160 struct bpf_prog_prep_result *res); 161 162 int bpf_program__set_prep(struct bpf_program *prog, int nr_instance, 163 bpf_program_prep_t prep); 164 165 int bpf_program__nth_fd(struct bpf_program *prog, int n); 166 167 /* 168 * We don't need __attribute__((packed)) now since it is 169 * unnecessary for 'bpf_map_def' because they are all aligned. 170 * In addition, using it will trigger -Wpacked warning message, 171 * and will be treated as an error due to -Werror. 172 */ 173 struct bpf_map_def { 174 unsigned int type; 175 unsigned int key_size; 176 unsigned int value_size; 177 unsigned int max_entries; 178 }; 179 180 /* 181 * There is another 'struct bpf_map' in include/linux/map.h. However, 182 * it is not a uapi header so no need to consider name clash. 183 */ 184 struct bpf_map; 185 struct bpf_map * 186 bpf_object__find_map_by_name(struct bpf_object *obj, const char *name); 187 188 struct bpf_map * 189 bpf_map__next(struct bpf_map *map, struct bpf_object *obj); 190 #define bpf_map__for_each(pos, obj) \ 191 for ((pos) = bpf_map__next(NULL, (obj)); \ 192 (pos) != NULL; \ 193 (pos) = bpf_map__next((pos), (obj))) 194 195 int bpf_map__fd(struct bpf_map *map); 196 const struct bpf_map_def *bpf_map__def(struct bpf_map *map); 197 const char *bpf_map__name(struct bpf_map *map); 198 199 typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *); 200 int bpf_map__set_priv(struct bpf_map *map, void *priv, 201 bpf_map_clear_priv_t clear_priv); 202 void *bpf_map__priv(struct bpf_map *map); 203 204 #endif 205