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