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