Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 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. |
Wang Nan | 203d1ca | 2016-07-04 11:02:42 +0000 | [diff] [blame] | 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> |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 20 | */ |
| 21 | #ifndef __BPF_LIBBPF_H |
| 22 | #define __BPF_LIBBPF_H |
| 23 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 24 | #include <stdio.h> |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 25 | #include <stdbool.h> |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 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 */ |
Colin Ian King | de8a63b | 2016-06-28 13:23:37 +0100 | [diff] [blame] | 35 | LIBBPF_ERRNO__ENDIAN, /* Endian mismatch */ |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 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 */ |
Wang Nan | 705fa21 | 2016-07-13 10:44:02 +0000 | [diff] [blame] | 42 | LIBBPF_ERRNO__PROGTYPE, /* Kernel doesn't support this program type */ |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 43 | __LIBBPF_ERRNO__END, |
| 44 | }; |
| 45 | |
| 46 | int libbpf_strerror(int err, char *buf, size_t size); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 47 | |
Wang Nan | b3f59d6 | 2015-07-01 02:13:52 +0000 | [diff] [blame] | 48 | /* |
| 49 | * In include/linux/compiler-gcc.h, __printf is defined. However |
| 50 | * it should be better if libbpf.h doesn't depend on Linux header file. |
| 51 | * So instead of __printf, here we use gcc attribute directly. |
| 52 | */ |
| 53 | typedef int (*libbpf_print_fn_t)(const char *, ...) |
| 54 | __attribute__((format(printf, 1, 2))); |
| 55 | |
| 56 | void libbpf_set_print(libbpf_print_fn_t warn, |
| 57 | libbpf_print_fn_t info, |
| 58 | libbpf_print_fn_t debug); |
| 59 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 60 | /* Hide internal to user */ |
| 61 | struct bpf_object; |
| 62 | |
| 63 | struct bpf_object *bpf_object__open(const char *path); |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 64 | struct bpf_object *bpf_object__open_buffer(void *obj_buf, |
Wang Nan | acf860a | 2015-08-27 02:30:55 +0000 | [diff] [blame] | 65 | size_t obj_buf_sz, |
| 66 | const char *name); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 67 | void bpf_object__close(struct bpf_object *object); |
| 68 | |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 69 | /* Load/unload object into/from kernel */ |
| 70 | int bpf_object__load(struct bpf_object *obj); |
| 71 | int bpf_object__unload(struct bpf_object *obj); |
Arnaldo Carvalho de Melo | a7fe045 | 2016-06-03 12:22:51 -0300 | [diff] [blame] | 72 | const char *bpf_object__name(struct bpf_object *obj); |
| 73 | unsigned int bpf_object__kversion(struct bpf_object *obj); |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 74 | |
Wang Nan | 9a208ef | 2015-07-01 02:14:10 +0000 | [diff] [blame] | 75 | struct bpf_object *bpf_object__next(struct bpf_object *prev); |
| 76 | #define bpf_object__for_each_safe(pos, tmp) \ |
| 77 | for ((pos) = bpf_object__next(NULL), \ |
| 78 | (tmp) = bpf_object__next(pos); \ |
| 79 | (pos) != NULL; \ |
| 80 | (pos) = (tmp), (tmp) = bpf_object__next(tmp)) |
| 81 | |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 82 | /* Accessors of bpf_program. */ |
| 83 | struct bpf_program; |
| 84 | struct bpf_program *bpf_program__next(struct bpf_program *prog, |
| 85 | struct bpf_object *obj); |
| 86 | |
| 87 | #define bpf_object__for_each_program(pos, obj) \ |
| 88 | for ((pos) = bpf_program__next(NULL, (obj)); \ |
| 89 | (pos) != NULL; \ |
| 90 | (pos) = bpf_program__next((pos), (obj))) |
| 91 | |
| 92 | typedef void (*bpf_program_clear_priv_t)(struct bpf_program *, |
| 93 | void *); |
| 94 | |
Arnaldo Carvalho de Melo | edb13ed | 2016-06-03 12:38:21 -0300 | [diff] [blame] | 95 | int bpf_program__set_priv(struct bpf_program *prog, void *priv, |
| 96 | bpf_program_clear_priv_t clear_priv); |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 97 | |
Arnaldo Carvalho de Melo | be834ff | 2016-06-03 12:36:39 -0300 | [diff] [blame] | 98 | void *bpf_program__priv(struct bpf_program *prog); |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 99 | |
Namhyung Kim | 715f8db | 2015-11-03 20:21:05 +0900 | [diff] [blame] | 100 | const char *bpf_program__title(struct bpf_program *prog, bool needs_copy); |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 101 | |
| 102 | int bpf_program__fd(struct bpf_program *prog); |
| 103 | |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 104 | struct bpf_insn; |
| 105 | |
| 106 | /* |
| 107 | * Libbpf allows callers to adjust BPF programs before being loaded |
| 108 | * into kernel. One program in an object file can be transform into |
| 109 | * multiple variants to be attached to different code. |
| 110 | * |
| 111 | * bpf_program_prep_t, bpf_program__set_prep and bpf_program__nth_fd |
| 112 | * are APIs for this propose. |
| 113 | * |
| 114 | * - bpf_program_prep_t: |
| 115 | * It defines 'preprocessor', which is a caller defined function |
| 116 | * passed to libbpf through bpf_program__set_prep(), and will be |
| 117 | * called before program is loaded. The processor should adjust |
| 118 | * the program one time for each instances according to the number |
| 119 | * passed to it. |
| 120 | * |
| 121 | * - bpf_program__set_prep: |
| 122 | * Attachs a preprocessor to a BPF program. The number of instances |
| 123 | * whould be created is also passed through this function. |
| 124 | * |
| 125 | * - bpf_program__nth_fd: |
| 126 | * After the program is loaded, get resuling fds from bpf program for |
| 127 | * each instances. |
| 128 | * |
| 129 | * If bpf_program__set_prep() is not used, the program whould be loaded |
| 130 | * without adjustment during bpf_object__load(). The program has only |
| 131 | * one instance. In this case bpf_program__fd(prog) is equal to |
| 132 | * bpf_program__nth_fd(prog, 0). |
| 133 | */ |
| 134 | |
| 135 | struct bpf_prog_prep_result { |
| 136 | /* |
| 137 | * If not NULL, load new instruction array. |
| 138 | * If set to NULL, don't load this instance. |
| 139 | */ |
| 140 | struct bpf_insn *new_insn_ptr; |
| 141 | int new_insn_cnt; |
| 142 | |
| 143 | /* If not NULL, result fd is set to it */ |
| 144 | int *pfd; |
| 145 | }; |
| 146 | |
| 147 | /* |
| 148 | * Parameters of bpf_program_prep_t: |
| 149 | * - prog: The bpf_program being loaded. |
| 150 | * - n: Index of instance being generated. |
| 151 | * - insns: BPF instructions array. |
| 152 | * - insns_cnt:Number of instructions in insns. |
| 153 | * - res: Output parameter, result of transformation. |
| 154 | * |
| 155 | * Return value: |
| 156 | * - Zero: pre-processing success. |
| 157 | * - Non-zero: pre-processing, stop loading. |
| 158 | */ |
| 159 | typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n, |
| 160 | struct bpf_insn *insns, int insns_cnt, |
| 161 | struct bpf_prog_prep_result *res); |
| 162 | |
| 163 | int bpf_program__set_prep(struct bpf_program *prog, int nr_instance, |
| 164 | bpf_program_prep_t prep); |
| 165 | |
| 166 | int bpf_program__nth_fd(struct bpf_program *prog, int n); |
| 167 | |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 168 | /* |
Wang Nan | 5f44e4c8 | 2016-07-13 10:44:01 +0000 | [diff] [blame] | 169 | * Adjust type of bpf program. Default is kprobe. |
| 170 | */ |
| 171 | int bpf_program__set_tracepoint(struct bpf_program *prog); |
| 172 | int bpf_program__set_kprobe(struct bpf_program *prog); |
| 173 | |
| 174 | bool bpf_program__is_tracepoint(struct bpf_program *prog); |
| 175 | bool bpf_program__is_kprobe(struct bpf_program *prog); |
| 176 | |
| 177 | /* |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 178 | * We don't need __attribute__((packed)) now since it is |
| 179 | * unnecessary for 'bpf_map_def' because they are all aligned. |
| 180 | * In addition, using it will trigger -Wpacked warning message, |
| 181 | * and will be treated as an error due to -Werror. |
| 182 | */ |
| 183 | struct bpf_map_def { |
| 184 | unsigned int type; |
| 185 | unsigned int key_size; |
| 186 | unsigned int value_size; |
| 187 | unsigned int max_entries; |
| 188 | }; |
| 189 | |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 190 | /* |
| 191 | * There is another 'struct bpf_map' in include/linux/map.h. However, |
| 192 | * it is not a uapi header so no need to consider name clash. |
| 193 | */ |
| 194 | struct bpf_map; |
Wang Nan | 561bbcc | 2015-11-27 08:47:36 +0000 | [diff] [blame] | 195 | struct bpf_map * |
Arnaldo Carvalho de Melo | a7fe045 | 2016-06-03 12:22:51 -0300 | [diff] [blame] | 196 | bpf_object__find_map_by_name(struct bpf_object *obj, const char *name); |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 197 | |
| 198 | struct bpf_map * |
| 199 | bpf_map__next(struct bpf_map *map, struct bpf_object *obj); |
| 200 | #define bpf_map__for_each(pos, obj) \ |
| 201 | for ((pos) = bpf_map__next(NULL, (obj)); \ |
| 202 | (pos) != NULL; \ |
| 203 | (pos) = bpf_map__next((pos), (obj))) |
| 204 | |
Arnaldo Carvalho de Melo | 6e009e6 | 2016-06-03 12:15:52 -0300 | [diff] [blame] | 205 | int bpf_map__fd(struct bpf_map *map); |
Arnaldo Carvalho de Melo | 53897a7 | 2016-06-02 14:21:06 -0300 | [diff] [blame] | 206 | const struct bpf_map_def *bpf_map__def(struct bpf_map *map); |
Arnaldo Carvalho de Melo | 009ad5d | 2016-06-02 11:02:05 -0300 | [diff] [blame] | 207 | const char *bpf_map__name(struct bpf_map *map); |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 208 | |
| 209 | typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *); |
Arnaldo Carvalho de Melo | edb13ed | 2016-06-03 12:38:21 -0300 | [diff] [blame] | 210 | int bpf_map__set_priv(struct bpf_map *map, void *priv, |
| 211 | bpf_map_clear_priv_t clear_priv); |
Arnaldo Carvalho de Melo | b4cbfa5 | 2016-06-02 10:51:59 -0300 | [diff] [blame] | 212 | void *bpf_map__priv(struct bpf_map *map); |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame] | 213 | |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 214 | #endif |