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 <stdint.h> 26 #include <stdbool.h> 27 #include <sys/types.h> // for size_t 28 #include <linux/bpf.h> 29 30 enum libbpf_errno { 31 __LIBBPF_ERRNO__START = 4000, 32 33 /* Something wrong in libelf */ 34 LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START, 35 LIBBPF_ERRNO__FORMAT, /* BPF object format invalid */ 36 LIBBPF_ERRNO__KVERSION, /* Incorrect or no 'version' section */ 37 LIBBPF_ERRNO__ENDIAN, /* Endian mismatch */ 38 LIBBPF_ERRNO__INTERNAL, /* Internal error in libbpf */ 39 LIBBPF_ERRNO__RELOC, /* Relocation failed */ 40 LIBBPF_ERRNO__LOAD, /* Load program failure for unknown reason */ 41 LIBBPF_ERRNO__VERIFY, /* Kernel verifier blocks program loading */ 42 LIBBPF_ERRNO__PROG2BIG, /* Program too big */ 43 LIBBPF_ERRNO__KVER, /* Incorrect kernel version */ 44 LIBBPF_ERRNO__PROGTYPE, /* Kernel doesn't support this program type */ 45 __LIBBPF_ERRNO__END, 46 }; 47 48 int libbpf_strerror(int err, char *buf, size_t size); 49 50 /* 51 * In include/linux/compiler-gcc.h, __printf is defined. However 52 * it should be better if libbpf.h doesn't depend on Linux header file. 53 * So instead of __printf, here we use gcc attribute directly. 54 */ 55 typedef int (*libbpf_print_fn_t)(const char *, ...) 56 __attribute__((format(printf, 1, 2))); 57 58 void libbpf_set_print(libbpf_print_fn_t warn, 59 libbpf_print_fn_t info, 60 libbpf_print_fn_t debug); 61 62 /* Hide internal to user */ 63 struct bpf_object; 64 65 struct bpf_object *bpf_object__open(const char *path); 66 struct bpf_object *bpf_object__open_buffer(void *obj_buf, 67 size_t obj_buf_sz, 68 const char *name); 69 int bpf_object__pin(struct bpf_object *object, const char *path); 70 void bpf_object__close(struct bpf_object *object); 71 72 /* Load/unload object into/from kernel */ 73 int bpf_object__load(struct bpf_object *obj); 74 int bpf_object__unload(struct bpf_object *obj); 75 const char *bpf_object__name(struct bpf_object *obj); 76 unsigned int bpf_object__kversion(struct bpf_object *obj); 77 78 struct bpf_object *bpf_object__next(struct bpf_object *prev); 79 #define bpf_object__for_each_safe(pos, tmp) \ 80 for ((pos) = bpf_object__next(NULL), \ 81 (tmp) = bpf_object__next(pos); \ 82 (pos) != NULL; \ 83 (pos) = (tmp), (tmp) = bpf_object__next(tmp)) 84 85 typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *); 86 int bpf_object__set_priv(struct bpf_object *obj, void *priv, 87 bpf_object_clear_priv_t clear_priv); 88 void *bpf_object__priv(struct bpf_object *prog); 89 90 /* Accessors of bpf_program. */ 91 struct bpf_program; 92 struct bpf_program *bpf_program__next(struct bpf_program *prog, 93 struct bpf_object *obj); 94 95 #define bpf_object__for_each_program(pos, obj) \ 96 for ((pos) = bpf_program__next(NULL, (obj)); \ 97 (pos) != NULL; \ 98 (pos) = bpf_program__next((pos), (obj))) 99 100 typedef void (*bpf_program_clear_priv_t)(struct bpf_program *, 101 void *); 102 103 int bpf_program__set_priv(struct bpf_program *prog, void *priv, 104 bpf_program_clear_priv_t clear_priv); 105 106 void *bpf_program__priv(struct bpf_program *prog); 107 108 const char *bpf_program__title(struct bpf_program *prog, bool needs_copy); 109 110 int bpf_program__fd(struct bpf_program *prog); 111 int bpf_program__pin_instance(struct bpf_program *prog, const char *path, 112 int instance); 113 int bpf_program__pin(struct bpf_program *prog, const char *path); 114 115 struct bpf_insn; 116 117 /* 118 * Libbpf allows callers to adjust BPF programs before being loaded 119 * into kernel. One program in an object file can be transform into 120 * multiple variants to be attached to different code. 121 * 122 * bpf_program_prep_t, bpf_program__set_prep and bpf_program__nth_fd 123 * are APIs for this propose. 124 * 125 * - bpf_program_prep_t: 126 * It defines 'preprocessor', which is a caller defined function 127 * passed to libbpf through bpf_program__set_prep(), and will be 128 * called before program is loaded. The processor should adjust 129 * the program one time for each instances according to the number 130 * passed to it. 131 * 132 * - bpf_program__set_prep: 133 * Attachs a preprocessor to a BPF program. The number of instances 134 * whould be created is also passed through this function. 135 * 136 * - bpf_program__nth_fd: 137 * After the program is loaded, get resuling fds from bpf program for 138 * each instances. 139 * 140 * If bpf_program__set_prep() is not used, the program whould be loaded 141 * without adjustment during bpf_object__load(). The program has only 142 * one instance. In this case bpf_program__fd(prog) is equal to 143 * bpf_program__nth_fd(prog, 0). 144 */ 145 146 struct bpf_prog_prep_result { 147 /* 148 * If not NULL, load new instruction array. 149 * If set to NULL, don't load this instance. 150 */ 151 struct bpf_insn *new_insn_ptr; 152 int new_insn_cnt; 153 154 /* If not NULL, result fd is set to it */ 155 int *pfd; 156 }; 157 158 /* 159 * Parameters of bpf_program_prep_t: 160 * - prog: The bpf_program being loaded. 161 * - n: Index of instance being generated. 162 * - insns: BPF instructions array. 163 * - insns_cnt:Number of instructions in insns. 164 * - res: Output parameter, result of transformation. 165 * 166 * Return value: 167 * - Zero: pre-processing success. 168 * - Non-zero: pre-processing, stop loading. 169 */ 170 typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n, 171 struct bpf_insn *insns, int insns_cnt, 172 struct bpf_prog_prep_result *res); 173 174 int bpf_program__set_prep(struct bpf_program *prog, int nr_instance, 175 bpf_program_prep_t prep); 176 177 int bpf_program__nth_fd(struct bpf_program *prog, int n); 178 179 /* 180 * Adjust type of bpf program. Default is kprobe. 181 */ 182 int bpf_program__set_socket_filter(struct bpf_program *prog); 183 int bpf_program__set_tracepoint(struct bpf_program *prog); 184 int bpf_program__set_kprobe(struct bpf_program *prog); 185 int bpf_program__set_sched_cls(struct bpf_program *prog); 186 int bpf_program__set_sched_act(struct bpf_program *prog); 187 int bpf_program__set_xdp(struct bpf_program *prog); 188 int bpf_program__set_perf_event(struct bpf_program *prog); 189 void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type); 190 191 bool bpf_program__is_socket_filter(struct bpf_program *prog); 192 bool bpf_program__is_tracepoint(struct bpf_program *prog); 193 bool bpf_program__is_kprobe(struct bpf_program *prog); 194 bool bpf_program__is_sched_cls(struct bpf_program *prog); 195 bool bpf_program__is_sched_act(struct bpf_program *prog); 196 bool bpf_program__is_xdp(struct bpf_program *prog); 197 bool bpf_program__is_perf_event(struct bpf_program *prog); 198 199 /* 200 * We don't need __attribute__((packed)) now since it is 201 * unnecessary for 'bpf_map_def' because they are all aligned. 202 * In addition, using it will trigger -Wpacked warning message, 203 * and will be treated as an error due to -Werror. 204 */ 205 struct bpf_map_def { 206 unsigned int type; 207 unsigned int key_size; 208 unsigned int value_size; 209 unsigned int max_entries; 210 }; 211 212 /* 213 * There is another 'struct bpf_map' in include/linux/map.h. However, 214 * it is not a uapi header so no need to consider name clash. 215 */ 216 struct bpf_map; 217 struct bpf_map * 218 bpf_object__find_map_by_name(struct bpf_object *obj, const char *name); 219 220 /* 221 * Get bpf_map through the offset of corresponding struct bpf_map_def 222 * in the bpf object file. 223 */ 224 struct bpf_map * 225 bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset); 226 227 struct bpf_map * 228 bpf_map__next(struct bpf_map *map, struct bpf_object *obj); 229 #define bpf_map__for_each(pos, obj) \ 230 for ((pos) = bpf_map__next(NULL, (obj)); \ 231 (pos) != NULL; \ 232 (pos) = bpf_map__next((pos), (obj))) 233 234 int bpf_map__fd(struct bpf_map *map); 235 const struct bpf_map_def *bpf_map__def(struct bpf_map *map); 236 const char *bpf_map__name(struct bpf_map *map); 237 238 typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *); 239 int bpf_map__set_priv(struct bpf_map *map, void *priv, 240 bpf_map_clear_priv_t clear_priv); 241 void *bpf_map__priv(struct bpf_map *map); 242 int bpf_map__pin(struct bpf_map *map, const char *path); 243 244 long libbpf_get_error(const void *ptr); 245 246 int bpf_prog_load(const char *file, enum bpf_prog_type type, 247 struct bpf_object **pobj, int *prog_fd); 248 #endif 249