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