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__NLPARSE, /* netlink parsing error */ 50 __LIBBPF_ERRNO__END, 51 }; 52 53 int libbpf_strerror(int err, char *buf, size_t size); 54 55 /* 56 * __printf is defined in include/linux/compiler-gcc.h. However, 57 * it would be better if libbpf.h didn't depend on Linux header files. 58 * So instead of __printf, here we use gcc attribute directly. 59 */ 60 typedef int (*libbpf_print_fn_t)(const char *, ...) 61 __attribute__((format(printf, 1, 2))); 62 63 void libbpf_set_print(libbpf_print_fn_t warn, 64 libbpf_print_fn_t info, 65 libbpf_print_fn_t debug); 66 67 /* Hide internal to user */ 68 struct bpf_object; 69 70 struct bpf_object_open_attr { 71 const char *file; 72 enum bpf_prog_type prog_type; 73 }; 74 75 struct bpf_object *bpf_object__open(const char *path); 76 struct bpf_object *bpf_object__open_xattr(struct bpf_object_open_attr *attr); 77 struct bpf_object *bpf_object__open_buffer(void *obj_buf, 78 size_t obj_buf_sz, 79 const char *name); 80 int bpf_object__pin(struct bpf_object *object, const char *path); 81 void bpf_object__close(struct bpf_object *object); 82 83 /* Load/unload object into/from kernel */ 84 int bpf_object__load(struct bpf_object *obj); 85 int bpf_object__unload(struct bpf_object *obj); 86 const char *bpf_object__name(struct bpf_object *obj); 87 unsigned int bpf_object__kversion(struct bpf_object *obj); 88 int bpf_object__btf_fd(const struct bpf_object *obj); 89 90 struct bpf_program * 91 bpf_object__find_program_by_title(struct bpf_object *obj, const char *title); 92 93 struct bpf_object *bpf_object__next(struct bpf_object *prev); 94 #define bpf_object__for_each_safe(pos, tmp) \ 95 for ((pos) = bpf_object__next(NULL), \ 96 (tmp) = bpf_object__next(pos); \ 97 (pos) != NULL; \ 98 (pos) = (tmp), (tmp) = bpf_object__next(tmp)) 99 100 typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *); 101 int bpf_object__set_priv(struct bpf_object *obj, void *priv, 102 bpf_object_clear_priv_t clear_priv); 103 void *bpf_object__priv(struct bpf_object *prog); 104 105 int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type, 106 enum bpf_attach_type *expected_attach_type); 107 108 /* Accessors of bpf_program */ 109 struct bpf_program; 110 struct bpf_program *bpf_program__next(struct bpf_program *prog, 111 struct bpf_object *obj); 112 113 #define bpf_object__for_each_program(pos, obj) \ 114 for ((pos) = bpf_program__next(NULL, (obj)); \ 115 (pos) != NULL; \ 116 (pos) = bpf_program__next((pos), (obj))) 117 118 typedef void (*bpf_program_clear_priv_t)(struct bpf_program *, 119 void *); 120 121 int bpf_program__set_priv(struct bpf_program *prog, void *priv, 122 bpf_program_clear_priv_t clear_priv); 123 124 void *bpf_program__priv(struct bpf_program *prog); 125 void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex); 126 127 const char *bpf_program__title(struct bpf_program *prog, bool needs_copy); 128 129 int bpf_program__fd(struct bpf_program *prog); 130 int bpf_program__pin_instance(struct bpf_program *prog, const char *path, 131 int instance); 132 int bpf_program__pin(struct bpf_program *prog, const char *path); 133 134 struct bpf_insn; 135 136 /* 137 * Libbpf allows callers to adjust BPF programs before being loaded 138 * into kernel. One program in an object file can be transformed into 139 * multiple variants to be attached to different hooks. 140 * 141 * bpf_program_prep_t, bpf_program__set_prep and bpf_program__nth_fd 142 * form an API for this purpose. 143 * 144 * - bpf_program_prep_t: 145 * Defines a 'preprocessor', which is a caller defined function 146 * passed to libbpf through bpf_program__set_prep(), and will be 147 * called before program is loaded. The processor should adjust 148 * the program one time for each instance according to the instance id 149 * passed to it. 150 * 151 * - bpf_program__set_prep: 152 * Attaches a preprocessor to a BPF program. The number of instances 153 * that should be created is also passed through this function. 154 * 155 * - bpf_program__nth_fd: 156 * After the program is loaded, get resulting FD of a given instance 157 * of the BPF program. 158 * 159 * If bpf_program__set_prep() is not used, the program would be loaded 160 * without adjustment during bpf_object__load(). The program has only 161 * one instance. In this case bpf_program__fd(prog) is equal to 162 * bpf_program__nth_fd(prog, 0). 163 */ 164 165 struct bpf_prog_prep_result { 166 /* 167 * If not NULL, load new instruction array. 168 * If set to NULL, don't load this instance. 169 */ 170 struct bpf_insn *new_insn_ptr; 171 int new_insn_cnt; 172 173 /* If not NULL, result FD is written to it. */ 174 int *pfd; 175 }; 176 177 /* 178 * Parameters of bpf_program_prep_t: 179 * - prog: The bpf_program being loaded. 180 * - n: Index of instance being generated. 181 * - insns: BPF instructions array. 182 * - insns_cnt:Number of instructions in insns. 183 * - res: Output parameter, result of transformation. 184 * 185 * Return value: 186 * - Zero: pre-processing success. 187 * - Non-zero: pre-processing error, stop loading. 188 */ 189 typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n, 190 struct bpf_insn *insns, int insns_cnt, 191 struct bpf_prog_prep_result *res); 192 193 int bpf_program__set_prep(struct bpf_program *prog, int nr_instance, 194 bpf_program_prep_t prep); 195 196 int bpf_program__nth_fd(struct bpf_program *prog, int n); 197 198 /* 199 * Adjust type of BPF program. Default is kprobe. 200 */ 201 int bpf_program__set_socket_filter(struct bpf_program *prog); 202 int bpf_program__set_tracepoint(struct bpf_program *prog); 203 int bpf_program__set_raw_tracepoint(struct bpf_program *prog); 204 int bpf_program__set_kprobe(struct bpf_program *prog); 205 int bpf_program__set_sched_cls(struct bpf_program *prog); 206 int bpf_program__set_sched_act(struct bpf_program *prog); 207 int bpf_program__set_xdp(struct bpf_program *prog); 208 int bpf_program__set_perf_event(struct bpf_program *prog); 209 void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type); 210 void bpf_program__set_expected_attach_type(struct bpf_program *prog, 211 enum bpf_attach_type type); 212 213 bool bpf_program__is_socket_filter(struct bpf_program *prog); 214 bool bpf_program__is_tracepoint(struct bpf_program *prog); 215 bool bpf_program__is_raw_tracepoint(struct bpf_program *prog); 216 bool bpf_program__is_kprobe(struct bpf_program *prog); 217 bool bpf_program__is_sched_cls(struct bpf_program *prog); 218 bool bpf_program__is_sched_act(struct bpf_program *prog); 219 bool bpf_program__is_xdp(struct bpf_program *prog); 220 bool bpf_program__is_perf_event(struct bpf_program *prog); 221 222 /* 223 * No need for __attribute__((packed)), all members of 'bpf_map_def' 224 * are all aligned. In addition, using __attribute__((packed)) 225 * would trigger a -Wpacked warning message, and lead to an error 226 * if -Werror is set. 227 */ 228 struct bpf_map_def { 229 unsigned int type; 230 unsigned int key_size; 231 unsigned int value_size; 232 unsigned int max_entries; 233 unsigned int map_flags; 234 }; 235 236 /* 237 * The 'struct bpf_map' in include/linux/bpf.h is internal to the kernel, 238 * so no need to worry about a name clash. 239 */ 240 struct bpf_map; 241 struct bpf_map * 242 bpf_object__find_map_by_name(struct bpf_object *obj, const char *name); 243 244 /* 245 * Get bpf_map through the offset of corresponding struct bpf_map_def 246 * in the BPF object file. 247 */ 248 struct bpf_map * 249 bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset); 250 251 struct bpf_map * 252 bpf_map__next(struct bpf_map *map, struct bpf_object *obj); 253 #define bpf_map__for_each(pos, obj) \ 254 for ((pos) = bpf_map__next(NULL, (obj)); \ 255 (pos) != NULL; \ 256 (pos) = bpf_map__next((pos), (obj))) 257 258 int bpf_map__fd(struct bpf_map *map); 259 const struct bpf_map_def *bpf_map__def(struct bpf_map *map); 260 const char *bpf_map__name(struct bpf_map *map); 261 __u32 bpf_map__btf_key_type_id(const struct bpf_map *map); 262 __u32 bpf_map__btf_value_type_id(const struct bpf_map *map); 263 264 typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *); 265 int bpf_map__set_priv(struct bpf_map *map, void *priv, 266 bpf_map_clear_priv_t clear_priv); 267 void *bpf_map__priv(struct bpf_map *map); 268 int bpf_map__reuse_fd(struct bpf_map *map, int fd); 269 bool bpf_map__is_offload_neutral(struct bpf_map *map); 270 void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex); 271 int bpf_map__pin(struct bpf_map *map, const char *path); 272 273 long libbpf_get_error(const void *ptr); 274 275 struct bpf_prog_load_attr { 276 const char *file; 277 enum bpf_prog_type prog_type; 278 enum bpf_attach_type expected_attach_type; 279 int ifindex; 280 }; 281 282 int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr, 283 struct bpf_object **pobj, int *prog_fd); 284 int bpf_prog_load(const char *file, enum bpf_prog_type type, 285 struct bpf_object **pobj, int *prog_fd); 286 287 int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags); 288 289 enum bpf_perf_event_ret { 290 LIBBPF_PERF_EVENT_DONE = 0, 291 LIBBPF_PERF_EVENT_ERROR = -1, 292 LIBBPF_PERF_EVENT_CONT = -2, 293 }; 294 295 typedef enum bpf_perf_event_ret (*bpf_perf_event_print_t)(void *event, 296 void *priv); 297 int bpf_perf_event_read_simple(void *mem, unsigned long size, 298 unsigned long page_size, 299 void **buf, size_t *buf_len, 300 bpf_perf_event_print_t fn, void *priv); 301 302 struct nlmsghdr; 303 struct nlattr; 304 typedef int (*dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb); 305 typedef int (*__dump_nlmsg_t)(struct nlmsghdr *nlmsg, dump_nlmsg_t, 306 void *cookie); 307 int bpf_netlink_open(unsigned int *nl_pid); 308 int nl_get_link(int sock, unsigned int nl_pid, dump_nlmsg_t dump_link_nlmsg, 309 void *cookie); 310 int nl_get_class(int sock, unsigned int nl_pid, int ifindex, 311 dump_nlmsg_t dump_class_nlmsg, void *cookie); 312 int nl_get_qdisc(int sock, unsigned int nl_pid, int ifindex, 313 dump_nlmsg_t dump_qdisc_nlmsg, void *cookie); 314 int nl_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle, 315 dump_nlmsg_t dump_filter_nlmsg, void *cookie); 316 #endif 317