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 | |
| 9 | #include <stdlib.h> |
Wang Nan | b3f59d6 | 2015-07-01 02:13:52 +0000 | [diff] [blame] | 10 | #include <stdio.h> |
| 11 | #include <stdarg.h> |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 12 | #include <inttypes.h> |
Wang Nan | b3f59d6 | 2015-07-01 02:13:52 +0000 | [diff] [blame] | 13 | #include <string.h> |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 14 | #include <unistd.h> |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 15 | #include <fcntl.h> |
| 16 | #include <errno.h> |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 17 | #include <asm/unistd.h> |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 18 | #include <linux/kernel.h> |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 19 | #include <linux/bpf.h> |
Wang Nan | 9a208ef | 2015-07-01 02:14:10 +0000 | [diff] [blame] | 20 | #include <linux/list.h> |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 21 | #include <libelf.h> |
| 22 | #include <gelf.h> |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 23 | |
| 24 | #include "libbpf.h" |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 25 | #include "bpf.h" |
Wang Nan | b3f59d6 | 2015-07-01 02:13:52 +0000 | [diff] [blame] | 26 | |
| 27 | #define __printf(a, b) __attribute__((format(printf, a, b))) |
| 28 | |
| 29 | __printf(1, 2) |
| 30 | static int __base_pr(const char *format, ...) |
| 31 | { |
| 32 | va_list args; |
| 33 | int err; |
| 34 | |
| 35 | va_start(args, format); |
| 36 | err = vfprintf(stderr, format, args); |
| 37 | va_end(args); |
| 38 | return err; |
| 39 | } |
| 40 | |
| 41 | static __printf(1, 2) libbpf_print_fn_t __pr_warning = __base_pr; |
| 42 | static __printf(1, 2) libbpf_print_fn_t __pr_info = __base_pr; |
| 43 | static __printf(1, 2) libbpf_print_fn_t __pr_debug; |
| 44 | |
| 45 | #define __pr(func, fmt, ...) \ |
| 46 | do { \ |
| 47 | if ((func)) \ |
| 48 | (func)("libbpf: " fmt, ##__VA_ARGS__); \ |
| 49 | } while (0) |
| 50 | |
| 51 | #define pr_warning(fmt, ...) __pr(__pr_warning, fmt, ##__VA_ARGS__) |
| 52 | #define pr_info(fmt, ...) __pr(__pr_info, fmt, ##__VA_ARGS__) |
| 53 | #define pr_debug(fmt, ...) __pr(__pr_debug, fmt, ##__VA_ARGS__) |
| 54 | |
| 55 | void libbpf_set_print(libbpf_print_fn_t warn, |
| 56 | libbpf_print_fn_t info, |
| 57 | libbpf_print_fn_t debug) |
| 58 | { |
| 59 | __pr_warning = warn; |
| 60 | __pr_info = info; |
| 61 | __pr_debug = debug; |
| 62 | } |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 63 | |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 64 | #define STRERR_BUFSIZE 128 |
| 65 | |
| 66 | #define ERRNO_OFFSET(e) ((e) - __LIBBPF_ERRNO__START) |
| 67 | #define ERRCODE_OFFSET(c) ERRNO_OFFSET(LIBBPF_ERRNO__##c) |
| 68 | #define NR_ERRNO (__LIBBPF_ERRNO__END - __LIBBPF_ERRNO__START) |
| 69 | |
| 70 | static const char *libbpf_strerror_table[NR_ERRNO] = { |
| 71 | [ERRCODE_OFFSET(LIBELF)] = "Something wrong in libelf", |
| 72 | [ERRCODE_OFFSET(FORMAT)] = "BPF object format invalid", |
| 73 | [ERRCODE_OFFSET(KVERSION)] = "'version' section incorrect or lost", |
| 74 | [ERRCODE_OFFSET(ENDIAN)] = "Endian missmatch", |
| 75 | [ERRCODE_OFFSET(INTERNAL)] = "Internal error in libbpf", |
| 76 | [ERRCODE_OFFSET(RELOC)] = "Relocation failed", |
| 77 | [ERRCODE_OFFSET(VERIFY)] = "Kernel verifier blocks program loading", |
| 78 | [ERRCODE_OFFSET(PROG2BIG)] = "Program too big", |
| 79 | [ERRCODE_OFFSET(KVER)] = "Incorrect kernel version", |
| 80 | }; |
| 81 | |
| 82 | int libbpf_strerror(int err, char *buf, size_t size) |
| 83 | { |
| 84 | if (!buf || !size) |
| 85 | return -1; |
| 86 | |
| 87 | err = err > 0 ? err : -err; |
| 88 | |
| 89 | if (err < __LIBBPF_ERRNO__START) { |
| 90 | int ret; |
| 91 | |
| 92 | ret = strerror_r(err, buf, size); |
| 93 | buf[size - 1] = '\0'; |
| 94 | return ret; |
| 95 | } |
| 96 | |
| 97 | if (err < __LIBBPF_ERRNO__END) { |
| 98 | const char *msg; |
| 99 | |
| 100 | msg = libbpf_strerror_table[ERRNO_OFFSET(err)]; |
| 101 | snprintf(buf, size, "%s", msg); |
| 102 | buf[size - 1] = '\0'; |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | snprintf(buf, size, "Unknown libbpf error %d", err); |
| 107 | buf[size - 1] = '\0'; |
| 108 | return -1; |
| 109 | } |
| 110 | |
| 111 | #define CHECK_ERR(action, err, out) do { \ |
| 112 | err = action; \ |
| 113 | if (err) \ |
| 114 | goto out; \ |
| 115 | } while(0) |
| 116 | |
| 117 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 118 | /* Copied from tools/perf/util/util.h */ |
| 119 | #ifndef zfree |
| 120 | # define zfree(ptr) ({ free(*ptr); *ptr = NULL; }) |
| 121 | #endif |
| 122 | |
| 123 | #ifndef zclose |
| 124 | # define zclose(fd) ({ \ |
| 125 | int ___err = 0; \ |
| 126 | if ((fd) >= 0) \ |
| 127 | ___err = close((fd)); \ |
| 128 | fd = -1; \ |
| 129 | ___err; }) |
| 130 | #endif |
| 131 | |
| 132 | #ifdef HAVE_LIBELF_MMAP_SUPPORT |
| 133 | # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ_MMAP |
| 134 | #else |
| 135 | # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ |
| 136 | #endif |
| 137 | |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 138 | /* |
| 139 | * bpf_prog should be a better name but it has been used in |
| 140 | * linux/filter.h. |
| 141 | */ |
| 142 | struct bpf_program { |
| 143 | /* Index in elf obj file, for relocation use. */ |
| 144 | int idx; |
| 145 | char *section_name; |
| 146 | struct bpf_insn *insns; |
| 147 | size_t insns_cnt; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 148 | |
| 149 | struct { |
| 150 | int insn_idx; |
| 151 | int map_idx; |
| 152 | } *reloc_desc; |
| 153 | int nr_reloc; |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 154 | |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 155 | struct { |
| 156 | int nr; |
| 157 | int *fds; |
| 158 | } instances; |
| 159 | bpf_program_prep_t preprocessor; |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 160 | |
| 161 | struct bpf_object *obj; |
| 162 | void *priv; |
| 163 | bpf_program_clear_priv_t clear_priv; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 164 | }; |
| 165 | |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame^] | 166 | struct bpf_map { |
| 167 | int fd; |
| 168 | struct bpf_map_def def; |
| 169 | void *priv; |
| 170 | bpf_map_clear_priv_t clear_priv; |
| 171 | }; |
| 172 | |
Wang Nan | 9a208ef | 2015-07-01 02:14:10 +0000 | [diff] [blame] | 173 | static LIST_HEAD(bpf_objects_list); |
| 174 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 175 | struct bpf_object { |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 176 | char license[64]; |
| 177 | u32 kern_version; |
Wang Nan | 0b3d1ef | 2015-07-01 02:13:58 +0000 | [diff] [blame] | 178 | |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 179 | struct bpf_program *programs; |
| 180 | size_t nr_programs; |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame^] | 181 | struct bpf_map *maps; |
| 182 | size_t nr_maps; |
| 183 | |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 184 | bool loaded; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 185 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 186 | /* |
| 187 | * Information when doing elf related work. Only valid if fd |
| 188 | * is valid. |
| 189 | */ |
| 190 | struct { |
| 191 | int fd; |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 192 | void *obj_buf; |
| 193 | size_t obj_buf_sz; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 194 | Elf *elf; |
| 195 | GElf_Ehdr ehdr; |
Wang Nan | bec7d68 | 2015-07-01 02:13:59 +0000 | [diff] [blame] | 196 | Elf_Data *symbols; |
Wang Nan | b62f06e | 2015-07-01 02:14:01 +0000 | [diff] [blame] | 197 | struct { |
| 198 | GElf_Shdr shdr; |
| 199 | Elf_Data *data; |
| 200 | } *reloc; |
| 201 | int nr_reloc; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 202 | } efile; |
Wang Nan | 9a208ef | 2015-07-01 02:14:10 +0000 | [diff] [blame] | 203 | /* |
| 204 | * All loaded bpf_object is linked in a list, which is |
| 205 | * hidden to caller. bpf_objects__<func> handlers deal with |
| 206 | * all objects. |
| 207 | */ |
| 208 | struct list_head list; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 209 | char path[]; |
| 210 | }; |
| 211 | #define obj_elf_valid(o) ((o)->efile.elf) |
| 212 | |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 213 | static void bpf_program__unload(struct bpf_program *prog) |
| 214 | { |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 215 | int i; |
| 216 | |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 217 | if (!prog) |
| 218 | return; |
| 219 | |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 220 | /* |
| 221 | * If the object is opened but the program was never loaded, |
| 222 | * it is possible that prog->instances.nr == -1. |
| 223 | */ |
| 224 | if (prog->instances.nr > 0) { |
| 225 | for (i = 0; i < prog->instances.nr; i++) |
| 226 | zclose(prog->instances.fds[i]); |
| 227 | } else if (prog->instances.nr != -1) { |
| 228 | pr_warning("Internal error: instances.nr is %d\n", |
| 229 | prog->instances.nr); |
| 230 | } |
| 231 | |
| 232 | prog->instances.nr = -1; |
| 233 | zfree(&prog->instances.fds); |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 236 | static void bpf_program__exit(struct bpf_program *prog) |
| 237 | { |
| 238 | if (!prog) |
| 239 | return; |
| 240 | |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 241 | if (prog->clear_priv) |
| 242 | prog->clear_priv(prog, prog->priv); |
| 243 | |
| 244 | prog->priv = NULL; |
| 245 | prog->clear_priv = NULL; |
| 246 | |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 247 | bpf_program__unload(prog); |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 248 | zfree(&prog->section_name); |
| 249 | zfree(&prog->insns); |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 250 | zfree(&prog->reloc_desc); |
| 251 | |
| 252 | prog->nr_reloc = 0; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 253 | prog->insns_cnt = 0; |
| 254 | prog->idx = -1; |
| 255 | } |
| 256 | |
| 257 | static int |
| 258 | bpf_program__init(void *data, size_t size, char *name, int idx, |
| 259 | struct bpf_program *prog) |
| 260 | { |
| 261 | if (size < sizeof(struct bpf_insn)) { |
| 262 | pr_warning("corrupted section '%s'\n", name); |
| 263 | return -EINVAL; |
| 264 | } |
| 265 | |
| 266 | bzero(prog, sizeof(*prog)); |
| 267 | |
| 268 | prog->section_name = strdup(name); |
| 269 | if (!prog->section_name) { |
| 270 | pr_warning("failed to alloc name for prog %s\n", |
| 271 | name); |
| 272 | goto errout; |
| 273 | } |
| 274 | |
| 275 | prog->insns = malloc(size); |
| 276 | if (!prog->insns) { |
| 277 | pr_warning("failed to alloc insns for %s\n", name); |
| 278 | goto errout; |
| 279 | } |
| 280 | prog->insns_cnt = size / sizeof(struct bpf_insn); |
| 281 | memcpy(prog->insns, data, |
| 282 | prog->insns_cnt * sizeof(struct bpf_insn)); |
| 283 | prog->idx = idx; |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 284 | prog->instances.fds = NULL; |
| 285 | prog->instances.nr = -1; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 286 | |
| 287 | return 0; |
| 288 | errout: |
| 289 | bpf_program__exit(prog); |
| 290 | return -ENOMEM; |
| 291 | } |
| 292 | |
| 293 | static int |
| 294 | bpf_object__add_program(struct bpf_object *obj, void *data, size_t size, |
| 295 | char *name, int idx) |
| 296 | { |
| 297 | struct bpf_program prog, *progs; |
| 298 | int nr_progs, err; |
| 299 | |
| 300 | err = bpf_program__init(data, size, name, idx, &prog); |
| 301 | if (err) |
| 302 | return err; |
| 303 | |
| 304 | progs = obj->programs; |
| 305 | nr_progs = obj->nr_programs; |
| 306 | |
| 307 | progs = realloc(progs, sizeof(progs[0]) * (nr_progs + 1)); |
| 308 | if (!progs) { |
| 309 | /* |
| 310 | * In this case the original obj->programs |
| 311 | * is still valid, so don't need special treat for |
| 312 | * bpf_close_object(). |
| 313 | */ |
| 314 | pr_warning("failed to alloc a new program '%s'\n", |
| 315 | name); |
| 316 | bpf_program__exit(&prog); |
| 317 | return -ENOMEM; |
| 318 | } |
| 319 | |
| 320 | pr_debug("found program %s\n", prog.section_name); |
| 321 | obj->programs = progs; |
| 322 | obj->nr_programs = nr_progs + 1; |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 323 | prog.obj = obj; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 324 | progs[nr_progs] = prog; |
| 325 | return 0; |
| 326 | } |
| 327 | |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 328 | static struct bpf_object *bpf_object__new(const char *path, |
| 329 | void *obj_buf, |
| 330 | size_t obj_buf_sz) |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 331 | { |
| 332 | struct bpf_object *obj; |
| 333 | |
| 334 | obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1); |
| 335 | if (!obj) { |
| 336 | pr_warning("alloc memory failed for %s\n", path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 337 | return ERR_PTR(-ENOMEM); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | strcpy(obj->path, path); |
| 341 | obj->efile.fd = -1; |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 342 | |
| 343 | /* |
| 344 | * Caller of this function should also calls |
| 345 | * bpf_object__elf_finish() after data collection to return |
| 346 | * obj_buf to user. If not, we should duplicate the buffer to |
| 347 | * avoid user freeing them before elf finish. |
| 348 | */ |
| 349 | obj->efile.obj_buf = obj_buf; |
| 350 | obj->efile.obj_buf_sz = obj_buf_sz; |
| 351 | |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 352 | obj->loaded = false; |
Wang Nan | 9a208ef | 2015-07-01 02:14:10 +0000 | [diff] [blame] | 353 | |
| 354 | INIT_LIST_HEAD(&obj->list); |
| 355 | list_add(&obj->list, &bpf_objects_list); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 356 | return obj; |
| 357 | } |
| 358 | |
| 359 | static void bpf_object__elf_finish(struct bpf_object *obj) |
| 360 | { |
| 361 | if (!obj_elf_valid(obj)) |
| 362 | return; |
| 363 | |
| 364 | if (obj->efile.elf) { |
| 365 | elf_end(obj->efile.elf); |
| 366 | obj->efile.elf = NULL; |
| 367 | } |
Wang Nan | bec7d68 | 2015-07-01 02:13:59 +0000 | [diff] [blame] | 368 | obj->efile.symbols = NULL; |
Wang Nan | b62f06e | 2015-07-01 02:14:01 +0000 | [diff] [blame] | 369 | |
| 370 | zfree(&obj->efile.reloc); |
| 371 | obj->efile.nr_reloc = 0; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 372 | zclose(obj->efile.fd); |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 373 | obj->efile.obj_buf = NULL; |
| 374 | obj->efile.obj_buf_sz = 0; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | static int bpf_object__elf_init(struct bpf_object *obj) |
| 378 | { |
| 379 | int err = 0; |
| 380 | GElf_Ehdr *ep; |
| 381 | |
| 382 | if (obj_elf_valid(obj)) { |
| 383 | pr_warning("elf init: internal error\n"); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 384 | return -LIBBPF_ERRNO__LIBELF; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 387 | if (obj->efile.obj_buf_sz > 0) { |
| 388 | /* |
| 389 | * obj_buf should have been validated by |
| 390 | * bpf_object__open_buffer(). |
| 391 | */ |
| 392 | obj->efile.elf = elf_memory(obj->efile.obj_buf, |
| 393 | obj->efile.obj_buf_sz); |
| 394 | } else { |
| 395 | obj->efile.fd = open(obj->path, O_RDONLY); |
| 396 | if (obj->efile.fd < 0) { |
| 397 | pr_warning("failed to open %s: %s\n", obj->path, |
| 398 | strerror(errno)); |
| 399 | return -errno; |
| 400 | } |
| 401 | |
| 402 | obj->efile.elf = elf_begin(obj->efile.fd, |
| 403 | LIBBPF_ELF_C_READ_MMAP, |
| 404 | NULL); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 407 | if (!obj->efile.elf) { |
| 408 | pr_warning("failed to open %s as ELF file\n", |
| 409 | obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 410 | err = -LIBBPF_ERRNO__LIBELF; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 411 | goto errout; |
| 412 | } |
| 413 | |
| 414 | if (!gelf_getehdr(obj->efile.elf, &obj->efile.ehdr)) { |
| 415 | pr_warning("failed to get EHDR from %s\n", |
| 416 | obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 417 | err = -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 418 | goto errout; |
| 419 | } |
| 420 | ep = &obj->efile.ehdr; |
| 421 | |
| 422 | if ((ep->e_type != ET_REL) || (ep->e_machine != 0)) { |
| 423 | pr_warning("%s is not an eBPF object file\n", |
| 424 | obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 425 | err = -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 426 | goto errout; |
| 427 | } |
| 428 | |
| 429 | return 0; |
| 430 | errout: |
| 431 | bpf_object__elf_finish(obj); |
| 432 | return err; |
| 433 | } |
| 434 | |
Wang Nan | cc4228d | 2015-07-01 02:13:55 +0000 | [diff] [blame] | 435 | static int |
| 436 | bpf_object__check_endianness(struct bpf_object *obj) |
| 437 | { |
| 438 | static unsigned int const endian = 1; |
| 439 | |
| 440 | switch (obj->efile.ehdr.e_ident[EI_DATA]) { |
| 441 | case ELFDATA2LSB: |
| 442 | /* We are big endian, BPF obj is little endian. */ |
| 443 | if (*(unsigned char const *)&endian != 1) |
| 444 | goto mismatch; |
| 445 | break; |
| 446 | |
| 447 | case ELFDATA2MSB: |
| 448 | /* We are little endian, BPF obj is big endian. */ |
| 449 | if (*(unsigned char const *)&endian != 0) |
| 450 | goto mismatch; |
| 451 | break; |
| 452 | default: |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 453 | return -LIBBPF_ERRNO__ENDIAN; |
Wang Nan | cc4228d | 2015-07-01 02:13:55 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | return 0; |
| 457 | |
| 458 | mismatch: |
| 459 | pr_warning("Error: endianness mismatch.\n"); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 460 | return -LIBBPF_ERRNO__ENDIAN; |
Wang Nan | cc4228d | 2015-07-01 02:13:55 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 463 | static int |
| 464 | bpf_object__init_license(struct bpf_object *obj, |
| 465 | void *data, size_t size) |
| 466 | { |
| 467 | memcpy(obj->license, data, |
| 468 | min(size, sizeof(obj->license) - 1)); |
| 469 | pr_debug("license of %s is %s\n", obj->path, obj->license); |
| 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | static int |
| 474 | bpf_object__init_kversion(struct bpf_object *obj, |
| 475 | void *data, size_t size) |
| 476 | { |
| 477 | u32 kver; |
| 478 | |
| 479 | if (size != sizeof(kver)) { |
| 480 | pr_warning("invalid kver section in %s\n", obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 481 | return -LIBBPF_ERRNO__FORMAT; |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 482 | } |
| 483 | memcpy(&kver, data, sizeof(kver)); |
| 484 | obj->kern_version = kver; |
| 485 | pr_debug("kernel version of %s is %x\n", obj->path, |
| 486 | obj->kern_version); |
| 487 | return 0; |
| 488 | } |
| 489 | |
Wang Nan | 0b3d1ef | 2015-07-01 02:13:58 +0000 | [diff] [blame] | 490 | static int |
| 491 | bpf_object__init_maps(struct bpf_object *obj, void *data, |
| 492 | size_t size) |
| 493 | { |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame^] | 494 | size_t nr_maps; |
| 495 | int i; |
| 496 | |
| 497 | nr_maps = size / sizeof(struct bpf_map_def); |
| 498 | if (!data || !nr_maps) { |
Wang Nan | 0b3d1ef | 2015-07-01 02:13:58 +0000 | [diff] [blame] | 499 | pr_debug("%s doesn't need map definition\n", |
| 500 | obj->path); |
| 501 | return 0; |
| 502 | } |
| 503 | |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame^] | 504 | pr_debug("maps in %s: %zd bytes\n", obj->path, size); |
| 505 | |
| 506 | obj->maps = calloc(nr_maps, sizeof(obj->maps[0])); |
| 507 | if (!obj->maps) { |
| 508 | pr_warning("alloc maps for object failed\n"); |
Wang Nan | 0b3d1ef | 2015-07-01 02:13:58 +0000 | [diff] [blame] | 509 | return -ENOMEM; |
| 510 | } |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame^] | 511 | obj->nr_maps = nr_maps; |
Wang Nan | 0b3d1ef | 2015-07-01 02:13:58 +0000 | [diff] [blame] | 512 | |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame^] | 513 | for (i = 0; i < nr_maps; i++) { |
| 514 | struct bpf_map_def *def = &obj->maps[i].def; |
| 515 | |
| 516 | /* |
| 517 | * fill all fd with -1 so won't close incorrect |
| 518 | * fd (fd=0 is stdin) when failure (zclose won't close |
| 519 | * negative fd)). |
| 520 | */ |
| 521 | obj->maps[i].fd = -1; |
| 522 | |
| 523 | /* Save map definition into obj->maps */ |
| 524 | *def = ((struct bpf_map_def *)data)[i]; |
| 525 | } |
Wang Nan | 0b3d1ef | 2015-07-01 02:13:58 +0000 | [diff] [blame] | 526 | return 0; |
| 527 | } |
| 528 | |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 529 | static int bpf_object__elf_collect(struct bpf_object *obj) |
| 530 | { |
| 531 | Elf *elf = obj->efile.elf; |
| 532 | GElf_Ehdr *ep = &obj->efile.ehdr; |
| 533 | Elf_Scn *scn = NULL; |
| 534 | int idx = 0, err = 0; |
| 535 | |
| 536 | /* Elf is corrupted/truncated, avoid calling elf_strptr. */ |
| 537 | if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) { |
| 538 | pr_warning("failed to get e_shstrndx from %s\n", |
| 539 | obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 540 | return -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | while ((scn = elf_nextscn(elf, scn)) != NULL) { |
| 544 | char *name; |
| 545 | GElf_Shdr sh; |
| 546 | Elf_Data *data; |
| 547 | |
| 548 | idx++; |
| 549 | if (gelf_getshdr(scn, &sh) != &sh) { |
| 550 | pr_warning("failed to get section header from %s\n", |
| 551 | obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 552 | err = -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 553 | goto out; |
| 554 | } |
| 555 | |
| 556 | name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name); |
| 557 | if (!name) { |
| 558 | pr_warning("failed to get section name from %s\n", |
| 559 | obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 560 | err = -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 561 | goto out; |
| 562 | } |
| 563 | |
| 564 | data = elf_getdata(scn, 0); |
| 565 | if (!data) { |
| 566 | pr_warning("failed to get section data from %s(%s)\n", |
| 567 | name, obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 568 | err = -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 569 | goto out; |
| 570 | } |
| 571 | pr_debug("section %s, size %ld, link %d, flags %lx, type=%d\n", |
| 572 | name, (unsigned long)data->d_size, |
| 573 | (int)sh.sh_link, (unsigned long)sh.sh_flags, |
| 574 | (int)sh.sh_type); |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 575 | |
| 576 | if (strcmp(name, "license") == 0) |
| 577 | err = bpf_object__init_license(obj, |
| 578 | data->d_buf, |
| 579 | data->d_size); |
| 580 | else if (strcmp(name, "version") == 0) |
| 581 | err = bpf_object__init_kversion(obj, |
| 582 | data->d_buf, |
| 583 | data->d_size); |
Wang Nan | 0b3d1ef | 2015-07-01 02:13:58 +0000 | [diff] [blame] | 584 | else if (strcmp(name, "maps") == 0) |
| 585 | err = bpf_object__init_maps(obj, data->d_buf, |
| 586 | data->d_size); |
Wang Nan | bec7d68 | 2015-07-01 02:13:59 +0000 | [diff] [blame] | 587 | else if (sh.sh_type == SHT_SYMTAB) { |
| 588 | if (obj->efile.symbols) { |
| 589 | pr_warning("bpf: multiple SYMTAB in %s\n", |
| 590 | obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 591 | err = -LIBBPF_ERRNO__FORMAT; |
Wang Nan | bec7d68 | 2015-07-01 02:13:59 +0000 | [diff] [blame] | 592 | } else |
| 593 | obj->efile.symbols = data; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 594 | } else if ((sh.sh_type == SHT_PROGBITS) && |
| 595 | (sh.sh_flags & SHF_EXECINSTR) && |
| 596 | (data->d_size > 0)) { |
| 597 | err = bpf_object__add_program(obj, data->d_buf, |
| 598 | data->d_size, name, idx); |
| 599 | if (err) { |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 600 | char errmsg[STRERR_BUFSIZE]; |
| 601 | |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 602 | strerror_r(-err, errmsg, sizeof(errmsg)); |
| 603 | pr_warning("failed to alloc program %s (%s): %s", |
| 604 | name, obj->path, errmsg); |
| 605 | } |
Wang Nan | b62f06e | 2015-07-01 02:14:01 +0000 | [diff] [blame] | 606 | } else if (sh.sh_type == SHT_REL) { |
| 607 | void *reloc = obj->efile.reloc; |
| 608 | int nr_reloc = obj->efile.nr_reloc + 1; |
| 609 | |
| 610 | reloc = realloc(reloc, |
| 611 | sizeof(*obj->efile.reloc) * nr_reloc); |
| 612 | if (!reloc) { |
| 613 | pr_warning("realloc failed\n"); |
| 614 | err = -ENOMEM; |
| 615 | } else { |
| 616 | int n = nr_reloc - 1; |
| 617 | |
| 618 | obj->efile.reloc = reloc; |
| 619 | obj->efile.nr_reloc = nr_reloc; |
| 620 | |
| 621 | obj->efile.reloc[n].shdr = sh; |
| 622 | obj->efile.reloc[n].data = data; |
| 623 | } |
Wang Nan | bec7d68 | 2015-07-01 02:13:59 +0000 | [diff] [blame] | 624 | } |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 625 | if (err) |
| 626 | goto out; |
Wang Nan | 2960366 | 2015-07-01 02:13:56 +0000 | [diff] [blame] | 627 | } |
| 628 | out: |
| 629 | return err; |
| 630 | } |
| 631 | |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 632 | static struct bpf_program * |
| 633 | bpf_object__find_prog_by_idx(struct bpf_object *obj, int idx) |
| 634 | { |
| 635 | struct bpf_program *prog; |
| 636 | size_t i; |
| 637 | |
| 638 | for (i = 0; i < obj->nr_programs; i++) { |
| 639 | prog = &obj->programs[i]; |
| 640 | if (prog->idx == idx) |
| 641 | return prog; |
| 642 | } |
| 643 | return NULL; |
| 644 | } |
| 645 | |
| 646 | static int |
| 647 | bpf_program__collect_reloc(struct bpf_program *prog, |
| 648 | size_t nr_maps, GElf_Shdr *shdr, |
| 649 | Elf_Data *data, Elf_Data *symbols) |
| 650 | { |
| 651 | int i, nrels; |
| 652 | |
| 653 | pr_debug("collecting relocating info for: '%s'\n", |
| 654 | prog->section_name); |
| 655 | nrels = shdr->sh_size / shdr->sh_entsize; |
| 656 | |
| 657 | prog->reloc_desc = malloc(sizeof(*prog->reloc_desc) * nrels); |
| 658 | if (!prog->reloc_desc) { |
| 659 | pr_warning("failed to alloc memory in relocation\n"); |
| 660 | return -ENOMEM; |
| 661 | } |
| 662 | prog->nr_reloc = nrels; |
| 663 | |
| 664 | for (i = 0; i < nrels; i++) { |
| 665 | GElf_Sym sym; |
| 666 | GElf_Rel rel; |
| 667 | unsigned int insn_idx; |
| 668 | struct bpf_insn *insns = prog->insns; |
| 669 | size_t map_idx; |
| 670 | |
| 671 | if (!gelf_getrel(data, i, &rel)) { |
| 672 | pr_warning("relocation: failed to get %d reloc\n", i); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 673 | return -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | insn_idx = rel.r_offset / sizeof(struct bpf_insn); |
| 677 | pr_debug("relocation: insn_idx=%u\n", insn_idx); |
| 678 | |
| 679 | if (!gelf_getsym(symbols, |
| 680 | GELF_R_SYM(rel.r_info), |
| 681 | &sym)) { |
| 682 | pr_warning("relocation: symbol %"PRIx64" not found\n", |
| 683 | GELF_R_SYM(rel.r_info)); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 684 | return -LIBBPF_ERRNO__FORMAT; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | if (insns[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) { |
| 688 | pr_warning("bpf: relocation: invalid relo for insns[%d].code 0x%x\n", |
| 689 | insn_idx, insns[insn_idx].code); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 690 | return -LIBBPF_ERRNO__RELOC; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | map_idx = sym.st_value / sizeof(struct bpf_map_def); |
| 694 | if (map_idx >= nr_maps) { |
| 695 | pr_warning("bpf relocation: map_idx %d large than %d\n", |
| 696 | (int)map_idx, (int)nr_maps - 1); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 697 | return -LIBBPF_ERRNO__RELOC; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | prog->reloc_desc[i].insn_idx = insn_idx; |
| 701 | prog->reloc_desc[i].map_idx = map_idx; |
| 702 | } |
| 703 | return 0; |
| 704 | } |
| 705 | |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 706 | static int |
| 707 | bpf_object__create_maps(struct bpf_object *obj) |
| 708 | { |
| 709 | unsigned int i; |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 710 | |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame^] | 711 | for (i = 0; i < obj->nr_maps; i++) { |
| 712 | struct bpf_map_def *def = &obj->maps[i].def; |
| 713 | int *pfd = &obj->maps[i].fd; |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 714 | |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame^] | 715 | *pfd = bpf_create_map(def->type, |
| 716 | def->key_size, |
| 717 | def->value_size, |
| 718 | def->max_entries); |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 719 | if (*pfd < 0) { |
| 720 | size_t j; |
| 721 | int err = *pfd; |
| 722 | |
| 723 | pr_warning("failed to create map: %s\n", |
| 724 | strerror(errno)); |
| 725 | for (j = 0; j < i; j++) |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame^] | 726 | zclose(obj->maps[j].fd); |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 727 | return err; |
| 728 | } |
| 729 | pr_debug("create map: fd=%d\n", *pfd); |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 730 | } |
| 731 | |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 732 | return 0; |
| 733 | } |
| 734 | |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 735 | static int |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame^] | 736 | bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj) |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 737 | { |
| 738 | int i; |
| 739 | |
| 740 | if (!prog || !prog->reloc_desc) |
| 741 | return 0; |
| 742 | |
| 743 | for (i = 0; i < prog->nr_reloc; i++) { |
| 744 | int insn_idx, map_idx; |
| 745 | struct bpf_insn *insns = prog->insns; |
| 746 | |
| 747 | insn_idx = prog->reloc_desc[i].insn_idx; |
| 748 | map_idx = prog->reloc_desc[i].map_idx; |
| 749 | |
| 750 | if (insn_idx >= (int)prog->insns_cnt) { |
| 751 | pr_warning("relocation out of range: '%s'\n", |
| 752 | prog->section_name); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 753 | return -LIBBPF_ERRNO__RELOC; |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 754 | } |
| 755 | insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD; |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame^] | 756 | insns[insn_idx].imm = obj->maps[map_idx].fd; |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | zfree(&prog->reloc_desc); |
| 760 | prog->nr_reloc = 0; |
| 761 | return 0; |
| 762 | } |
| 763 | |
| 764 | |
| 765 | static int |
| 766 | bpf_object__relocate(struct bpf_object *obj) |
| 767 | { |
| 768 | struct bpf_program *prog; |
| 769 | size_t i; |
| 770 | int err; |
| 771 | |
| 772 | for (i = 0; i < obj->nr_programs; i++) { |
| 773 | prog = &obj->programs[i]; |
| 774 | |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame^] | 775 | err = bpf_program__relocate(prog, obj); |
Wang Nan | 8a47a6c | 2015-07-01 02:14:05 +0000 | [diff] [blame] | 776 | if (err) { |
| 777 | pr_warning("failed to relocate '%s'\n", |
| 778 | prog->section_name); |
| 779 | return err; |
| 780 | } |
| 781 | } |
| 782 | return 0; |
| 783 | } |
| 784 | |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 785 | static int bpf_object__collect_reloc(struct bpf_object *obj) |
| 786 | { |
| 787 | int i, err; |
| 788 | |
| 789 | if (!obj_elf_valid(obj)) { |
| 790 | pr_warning("Internal error: elf object is closed\n"); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 791 | return -LIBBPF_ERRNO__INTERNAL; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | for (i = 0; i < obj->efile.nr_reloc; i++) { |
| 795 | GElf_Shdr *shdr = &obj->efile.reloc[i].shdr; |
| 796 | Elf_Data *data = obj->efile.reloc[i].data; |
| 797 | int idx = shdr->sh_info; |
| 798 | struct bpf_program *prog; |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame^] | 799 | size_t nr_maps = obj->nr_maps; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 800 | |
| 801 | if (shdr->sh_type != SHT_REL) { |
| 802 | pr_warning("internal error at %d\n", __LINE__); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 803 | return -LIBBPF_ERRNO__INTERNAL; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | prog = bpf_object__find_prog_by_idx(obj, idx); |
| 807 | if (!prog) { |
| 808 | pr_warning("relocation failed: no %d section\n", |
| 809 | idx); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 810 | return -LIBBPF_ERRNO__RELOC; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | err = bpf_program__collect_reloc(prog, nr_maps, |
| 814 | shdr, data, |
| 815 | obj->efile.symbols); |
| 816 | if (err) |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 817 | return err; |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 818 | } |
| 819 | return 0; |
| 820 | } |
| 821 | |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 822 | static int |
| 823 | load_program(struct bpf_insn *insns, int insns_cnt, |
| 824 | char *license, u32 kern_version, int *pfd) |
| 825 | { |
| 826 | int ret; |
| 827 | char *log_buf; |
| 828 | |
| 829 | if (!insns || !insns_cnt) |
| 830 | return -EINVAL; |
| 831 | |
| 832 | log_buf = malloc(BPF_LOG_BUF_SIZE); |
| 833 | if (!log_buf) |
| 834 | pr_warning("Alloc log buffer for bpf loader error, continue without log\n"); |
| 835 | |
| 836 | ret = bpf_load_program(BPF_PROG_TYPE_KPROBE, insns, |
| 837 | insns_cnt, license, kern_version, |
| 838 | log_buf, BPF_LOG_BUF_SIZE); |
| 839 | |
| 840 | if (ret >= 0) { |
| 841 | *pfd = ret; |
| 842 | ret = 0; |
| 843 | goto out; |
| 844 | } |
| 845 | |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 846 | ret = -LIBBPF_ERRNO__LOAD; |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 847 | pr_warning("load bpf program failed: %s\n", strerror(errno)); |
| 848 | |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 849 | if (log_buf && log_buf[0] != '\0') { |
| 850 | ret = -LIBBPF_ERRNO__VERIFY; |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 851 | pr_warning("-- BEGIN DUMP LOG ---\n"); |
| 852 | pr_warning("\n%s\n", log_buf); |
| 853 | pr_warning("-- END LOG --\n"); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 854 | } else { |
| 855 | if (insns_cnt >= BPF_MAXINSNS) { |
| 856 | pr_warning("Program too large (%d insns), at most %d insns\n", |
| 857 | insns_cnt, BPF_MAXINSNS); |
| 858 | ret = -LIBBPF_ERRNO__PROG2BIG; |
| 859 | } else if (log_buf) { |
| 860 | pr_warning("log buffer is empty\n"); |
| 861 | ret = -LIBBPF_ERRNO__KVER; |
| 862 | } |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | out: |
| 866 | free(log_buf); |
| 867 | return ret; |
| 868 | } |
| 869 | |
| 870 | static int |
| 871 | bpf_program__load(struct bpf_program *prog, |
| 872 | char *license, u32 kern_version) |
| 873 | { |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 874 | int err = 0, fd, i; |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 875 | |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 876 | if (prog->instances.nr < 0 || !prog->instances.fds) { |
| 877 | if (prog->preprocessor) { |
| 878 | pr_warning("Internal error: can't load program '%s'\n", |
| 879 | prog->section_name); |
| 880 | return -LIBBPF_ERRNO__INTERNAL; |
| 881 | } |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 882 | |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 883 | prog->instances.fds = malloc(sizeof(int)); |
| 884 | if (!prog->instances.fds) { |
| 885 | pr_warning("Not enough memory for BPF fds\n"); |
| 886 | return -ENOMEM; |
| 887 | } |
| 888 | prog->instances.nr = 1; |
| 889 | prog->instances.fds[0] = -1; |
| 890 | } |
| 891 | |
| 892 | if (!prog->preprocessor) { |
| 893 | if (prog->instances.nr != 1) { |
| 894 | pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n", |
| 895 | prog->section_name, prog->instances.nr); |
| 896 | } |
| 897 | err = load_program(prog->insns, prog->insns_cnt, |
| 898 | license, kern_version, &fd); |
| 899 | if (!err) |
| 900 | prog->instances.fds[0] = fd; |
| 901 | goto out; |
| 902 | } |
| 903 | |
| 904 | for (i = 0; i < prog->instances.nr; i++) { |
| 905 | struct bpf_prog_prep_result result; |
| 906 | bpf_program_prep_t preprocessor = prog->preprocessor; |
| 907 | |
| 908 | bzero(&result, sizeof(result)); |
| 909 | err = preprocessor(prog, i, prog->insns, |
| 910 | prog->insns_cnt, &result); |
| 911 | if (err) { |
| 912 | pr_warning("Preprocessing the %dth instance of program '%s' failed\n", |
| 913 | i, prog->section_name); |
| 914 | goto out; |
| 915 | } |
| 916 | |
| 917 | if (!result.new_insn_ptr || !result.new_insn_cnt) { |
| 918 | pr_debug("Skip loading the %dth instance of program '%s'\n", |
| 919 | i, prog->section_name); |
| 920 | prog->instances.fds[i] = -1; |
| 921 | if (result.pfd) |
| 922 | *result.pfd = -1; |
| 923 | continue; |
| 924 | } |
| 925 | |
| 926 | err = load_program(result.new_insn_ptr, |
| 927 | result.new_insn_cnt, |
| 928 | license, kern_version, &fd); |
| 929 | |
| 930 | if (err) { |
| 931 | pr_warning("Loading the %dth instance of program '%s' failed\n", |
| 932 | i, prog->section_name); |
| 933 | goto out; |
| 934 | } |
| 935 | |
| 936 | if (result.pfd) |
| 937 | *result.pfd = fd; |
| 938 | prog->instances.fds[i] = fd; |
| 939 | } |
| 940 | out: |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 941 | if (err) |
| 942 | pr_warning("failed to load program '%s'\n", |
| 943 | prog->section_name); |
| 944 | zfree(&prog->insns); |
| 945 | prog->insns_cnt = 0; |
| 946 | return err; |
| 947 | } |
| 948 | |
| 949 | static int |
| 950 | bpf_object__load_progs(struct bpf_object *obj) |
| 951 | { |
| 952 | size_t i; |
| 953 | int err; |
| 954 | |
| 955 | for (i = 0; i < obj->nr_programs; i++) { |
| 956 | err = bpf_program__load(&obj->programs[i], |
| 957 | obj->license, |
| 958 | obj->kern_version); |
| 959 | if (err) |
| 960 | return err; |
| 961 | } |
| 962 | return 0; |
| 963 | } |
| 964 | |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 965 | static int bpf_object__validate(struct bpf_object *obj) |
| 966 | { |
| 967 | if (obj->kern_version == 0) { |
| 968 | pr_warning("%s doesn't provide kernel version\n", |
| 969 | obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 970 | return -LIBBPF_ERRNO__KVERSION; |
Wang Nan | cb1e5e9 | 2015-07-01 02:13:57 +0000 | [diff] [blame] | 971 | } |
| 972 | return 0; |
| 973 | } |
| 974 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 975 | static struct bpf_object * |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 976 | __bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz) |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 977 | { |
| 978 | struct bpf_object *obj; |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 979 | int err; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 980 | |
| 981 | if (elf_version(EV_CURRENT) == EV_NONE) { |
| 982 | pr_warning("failed to init libelf for %s\n", path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 983 | return ERR_PTR(-LIBBPF_ERRNO__LIBELF); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 984 | } |
| 985 | |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 986 | obj = bpf_object__new(path, obj_buf, obj_buf_sz); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 987 | if (IS_ERR(obj)) |
| 988 | return obj; |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 989 | |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 990 | CHECK_ERR(bpf_object__elf_init(obj), err, out); |
| 991 | CHECK_ERR(bpf_object__check_endianness(obj), err, out); |
| 992 | CHECK_ERR(bpf_object__elf_collect(obj), err, out); |
| 993 | CHECK_ERR(bpf_object__collect_reloc(obj), err, out); |
| 994 | CHECK_ERR(bpf_object__validate(obj), err, out); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 995 | |
| 996 | bpf_object__elf_finish(obj); |
| 997 | return obj; |
| 998 | out: |
| 999 | bpf_object__close(obj); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1000 | return ERR_PTR(err); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | struct bpf_object *bpf_object__open(const char *path) |
| 1004 | { |
| 1005 | /* param validation */ |
| 1006 | if (!path) |
| 1007 | return NULL; |
| 1008 | |
| 1009 | pr_debug("loading %s\n", path); |
| 1010 | |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 1011 | return __bpf_object__open(path, NULL, 0); |
| 1012 | } |
| 1013 | |
| 1014 | struct bpf_object *bpf_object__open_buffer(void *obj_buf, |
Wang Nan | acf860a | 2015-08-27 02:30:55 +0000 | [diff] [blame] | 1015 | size_t obj_buf_sz, |
| 1016 | const char *name) |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 1017 | { |
Wang Nan | acf860a | 2015-08-27 02:30:55 +0000 | [diff] [blame] | 1018 | char tmp_name[64]; |
| 1019 | |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 1020 | /* param validation */ |
| 1021 | if (!obj_buf || obj_buf_sz <= 0) |
| 1022 | return NULL; |
| 1023 | |
Wang Nan | acf860a | 2015-08-27 02:30:55 +0000 | [diff] [blame] | 1024 | if (!name) { |
| 1025 | snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx", |
| 1026 | (unsigned long)obj_buf, |
| 1027 | (unsigned long)obj_buf_sz); |
| 1028 | tmp_name[sizeof(tmp_name) - 1] = '\0'; |
| 1029 | name = tmp_name; |
| 1030 | } |
| 1031 | pr_debug("loading object '%s' from buffer\n", |
| 1032 | name); |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 1033 | |
Wang Nan | acf860a | 2015-08-27 02:30:55 +0000 | [diff] [blame] | 1034 | return __bpf_object__open(name, obj_buf, obj_buf_sz); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 1037 | int bpf_object__unload(struct bpf_object *obj) |
| 1038 | { |
| 1039 | size_t i; |
| 1040 | |
| 1041 | if (!obj) |
| 1042 | return -EINVAL; |
| 1043 | |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame^] | 1044 | for (i = 0; i < obj->nr_maps; i++) |
| 1045 | zclose(obj->maps[i].fd); |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 1046 | |
Wang Nan | 55cffde | 2015-07-01 02:14:07 +0000 | [diff] [blame] | 1047 | for (i = 0; i < obj->nr_programs; i++) |
| 1048 | bpf_program__unload(&obj->programs[i]); |
| 1049 | |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 1050 | return 0; |
| 1051 | } |
| 1052 | |
| 1053 | int bpf_object__load(struct bpf_object *obj) |
| 1054 | { |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1055 | int err; |
| 1056 | |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 1057 | if (!obj) |
| 1058 | return -EINVAL; |
| 1059 | |
| 1060 | if (obj->loaded) { |
| 1061 | pr_warning("object should not be loaded twice\n"); |
| 1062 | return -EINVAL; |
| 1063 | } |
| 1064 | |
| 1065 | obj->loaded = true; |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1066 | |
| 1067 | CHECK_ERR(bpf_object__create_maps(obj), err, out); |
| 1068 | CHECK_ERR(bpf_object__relocate(obj), err, out); |
| 1069 | CHECK_ERR(bpf_object__load_progs(obj), err, out); |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 1070 | |
| 1071 | return 0; |
| 1072 | out: |
| 1073 | bpf_object__unload(obj); |
| 1074 | pr_warning("failed to load object '%s'\n", obj->path); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1075 | return err; |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1078 | void bpf_object__close(struct bpf_object *obj) |
| 1079 | { |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 1080 | size_t i; |
| 1081 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1082 | if (!obj) |
| 1083 | return; |
| 1084 | |
| 1085 | bpf_object__elf_finish(obj); |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 1086 | bpf_object__unload(obj); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1087 | |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame^] | 1088 | for (i = 0; i < obj->nr_maps; i++) { |
| 1089 | if (obj->maps[i].clear_priv) |
| 1090 | obj->maps[i].clear_priv(&obj->maps[i], |
| 1091 | obj->maps[i].priv); |
| 1092 | obj->maps[i].priv = NULL; |
| 1093 | obj->maps[i].clear_priv = NULL; |
| 1094 | } |
| 1095 | zfree(&obj->maps); |
| 1096 | obj->nr_maps = 0; |
Wang Nan | a5b8bd4 | 2015-07-01 02:14:00 +0000 | [diff] [blame] | 1097 | |
| 1098 | if (obj->programs && obj->nr_programs) { |
| 1099 | for (i = 0; i < obj->nr_programs; i++) |
| 1100 | bpf_program__exit(&obj->programs[i]); |
| 1101 | } |
| 1102 | zfree(&obj->programs); |
| 1103 | |
Wang Nan | 9a208ef | 2015-07-01 02:14:10 +0000 | [diff] [blame] | 1104 | list_del(&obj->list); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 1105 | free(obj); |
| 1106 | } |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 1107 | |
Wang Nan | 9a208ef | 2015-07-01 02:14:10 +0000 | [diff] [blame] | 1108 | struct bpf_object * |
| 1109 | bpf_object__next(struct bpf_object *prev) |
| 1110 | { |
| 1111 | struct bpf_object *next; |
| 1112 | |
| 1113 | if (!prev) |
| 1114 | next = list_first_entry(&bpf_objects_list, |
| 1115 | struct bpf_object, |
| 1116 | list); |
| 1117 | else |
| 1118 | next = list_next_entry(prev, list); |
| 1119 | |
| 1120 | /* Empty list is noticed here so don't need checking on entry. */ |
| 1121 | if (&next->list == &bpf_objects_list) |
| 1122 | return NULL; |
| 1123 | |
| 1124 | return next; |
| 1125 | } |
| 1126 | |
Wang Nan | acf860a | 2015-08-27 02:30:55 +0000 | [diff] [blame] | 1127 | const char * |
| 1128 | bpf_object__get_name(struct bpf_object *obj) |
| 1129 | { |
| 1130 | if (!obj) |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1131 | return ERR_PTR(-EINVAL); |
Wang Nan | acf860a | 2015-08-27 02:30:55 +0000 | [diff] [blame] | 1132 | return obj->path; |
| 1133 | } |
| 1134 | |
Wang Nan | 45825d8 | 2015-11-06 13:49:38 +0000 | [diff] [blame] | 1135 | unsigned int |
| 1136 | bpf_object__get_kversion(struct bpf_object *obj) |
| 1137 | { |
| 1138 | if (!obj) |
| 1139 | return 0; |
| 1140 | return obj->kern_version; |
| 1141 | } |
| 1142 | |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 1143 | struct bpf_program * |
| 1144 | bpf_program__next(struct bpf_program *prev, struct bpf_object *obj) |
| 1145 | { |
| 1146 | size_t idx; |
| 1147 | |
| 1148 | if (!obj->programs) |
| 1149 | return NULL; |
| 1150 | /* First handler */ |
| 1151 | if (prev == NULL) |
| 1152 | return &obj->programs[0]; |
| 1153 | |
| 1154 | if (prev->obj != obj) { |
| 1155 | pr_warning("error: program handler doesn't match object\n"); |
| 1156 | return NULL; |
| 1157 | } |
| 1158 | |
| 1159 | idx = (prev - obj->programs) + 1; |
| 1160 | if (idx >= obj->nr_programs) |
| 1161 | return NULL; |
| 1162 | return &obj->programs[idx]; |
| 1163 | } |
| 1164 | |
| 1165 | int bpf_program__set_private(struct bpf_program *prog, |
| 1166 | void *priv, |
| 1167 | bpf_program_clear_priv_t clear_priv) |
| 1168 | { |
| 1169 | if (prog->priv && prog->clear_priv) |
| 1170 | prog->clear_priv(prog, prog->priv); |
| 1171 | |
| 1172 | prog->priv = priv; |
| 1173 | prog->clear_priv = clear_priv; |
| 1174 | return 0; |
| 1175 | } |
| 1176 | |
| 1177 | int bpf_program__get_private(struct bpf_program *prog, void **ppriv) |
| 1178 | { |
| 1179 | *ppriv = prog->priv; |
| 1180 | return 0; |
| 1181 | } |
| 1182 | |
Namhyung Kim | 715f8db | 2015-11-03 20:21:05 +0900 | [diff] [blame] | 1183 | const char *bpf_program__title(struct bpf_program *prog, bool needs_copy) |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 1184 | { |
| 1185 | const char *title; |
| 1186 | |
| 1187 | title = prog->section_name; |
Namhyung Kim | 715f8db | 2015-11-03 20:21:05 +0900 | [diff] [blame] | 1188 | if (needs_copy) { |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 1189 | title = strdup(title); |
| 1190 | if (!title) { |
| 1191 | pr_warning("failed to strdup program title\n"); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1192 | return ERR_PTR(-ENOMEM); |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | return title; |
| 1197 | } |
| 1198 | |
| 1199 | int bpf_program__fd(struct bpf_program *prog) |
| 1200 | { |
Wang Nan | b580563 | 2015-11-16 12:10:09 +0000 | [diff] [blame] | 1201 | return bpf_program__nth_fd(prog, 0); |
| 1202 | } |
| 1203 | |
| 1204 | int bpf_program__set_prep(struct bpf_program *prog, int nr_instances, |
| 1205 | bpf_program_prep_t prep) |
| 1206 | { |
| 1207 | int *instances_fds; |
| 1208 | |
| 1209 | if (nr_instances <= 0 || !prep) |
| 1210 | return -EINVAL; |
| 1211 | |
| 1212 | if (prog->instances.nr > 0 || prog->instances.fds) { |
| 1213 | pr_warning("Can't set pre-processor after loading\n"); |
| 1214 | return -EINVAL; |
| 1215 | } |
| 1216 | |
| 1217 | instances_fds = malloc(sizeof(int) * nr_instances); |
| 1218 | if (!instances_fds) { |
| 1219 | pr_warning("alloc memory failed for fds\n"); |
| 1220 | return -ENOMEM; |
| 1221 | } |
| 1222 | |
| 1223 | /* fill all fd with -1 */ |
| 1224 | memset(instances_fds, -1, sizeof(int) * nr_instances); |
| 1225 | |
| 1226 | prog->instances.nr = nr_instances; |
| 1227 | prog->instances.fds = instances_fds; |
| 1228 | prog->preprocessor = prep; |
| 1229 | return 0; |
| 1230 | } |
| 1231 | |
| 1232 | int bpf_program__nth_fd(struct bpf_program *prog, int n) |
| 1233 | { |
| 1234 | int fd; |
| 1235 | |
| 1236 | if (n >= prog->instances.nr || n < 0) { |
| 1237 | pr_warning("Can't get the %dth fd from program %s: only %d instances\n", |
| 1238 | n, prog->section_name, prog->instances.nr); |
| 1239 | return -EINVAL; |
| 1240 | } |
| 1241 | |
| 1242 | fd = prog->instances.fds[n]; |
| 1243 | if (fd < 0) { |
| 1244 | pr_warning("%dth instance of program '%s' is invalid\n", |
| 1245 | n, prog->section_name); |
| 1246 | return -ENOENT; |
| 1247 | } |
| 1248 | |
| 1249 | return fd; |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 1250 | } |
Wang Nan | 9d759a9 | 2015-11-27 08:47:35 +0000 | [diff] [blame^] | 1251 | |
| 1252 | int bpf_map__get_fd(struct bpf_map *map) |
| 1253 | { |
| 1254 | if (!map) |
| 1255 | return -EINVAL; |
| 1256 | |
| 1257 | return map->fd; |
| 1258 | } |
| 1259 | |
| 1260 | int bpf_map__get_def(struct bpf_map *map, struct bpf_map_def *pdef) |
| 1261 | { |
| 1262 | if (!map || !pdef) |
| 1263 | return -EINVAL; |
| 1264 | |
| 1265 | *pdef = map->def; |
| 1266 | return 0; |
| 1267 | } |
| 1268 | |
| 1269 | int bpf_map__set_private(struct bpf_map *map, void *priv, |
| 1270 | bpf_map_clear_priv_t clear_priv) |
| 1271 | { |
| 1272 | if (!map) |
| 1273 | return -EINVAL; |
| 1274 | |
| 1275 | if (map->priv) { |
| 1276 | if (map->clear_priv) |
| 1277 | map->clear_priv(map, map->priv); |
| 1278 | } |
| 1279 | |
| 1280 | map->priv = priv; |
| 1281 | map->clear_priv = clear_priv; |
| 1282 | return 0; |
| 1283 | } |
| 1284 | |
| 1285 | int bpf_map__get_private(struct bpf_map *map, void **ppriv) |
| 1286 | { |
| 1287 | if (!map) |
| 1288 | return -EINVAL; |
| 1289 | |
| 1290 | if (ppriv) |
| 1291 | *ppriv = map->priv; |
| 1292 | return 0; |
| 1293 | } |
| 1294 | |
| 1295 | struct bpf_map * |
| 1296 | bpf_map__next(struct bpf_map *prev, struct bpf_object *obj) |
| 1297 | { |
| 1298 | size_t idx; |
| 1299 | struct bpf_map *s, *e; |
| 1300 | |
| 1301 | if (!obj || !obj->maps) |
| 1302 | return NULL; |
| 1303 | |
| 1304 | s = obj->maps; |
| 1305 | e = obj->maps + obj->nr_maps; |
| 1306 | |
| 1307 | if (prev == NULL) |
| 1308 | return s; |
| 1309 | |
| 1310 | if ((prev < s) || (prev >= e)) { |
| 1311 | pr_warning("error in %s: map handler doesn't belong to object\n", |
| 1312 | __func__); |
| 1313 | return NULL; |
| 1314 | } |
| 1315 | |
| 1316 | idx = (prev - obj->maps) + 1; |
| 1317 | if (idx >= obj->nr_maps) |
| 1318 | return NULL; |
| 1319 | return &obj->maps[idx]; |
| 1320 | } |