Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Common eBPF ELF object loading operations. |
| 3 | * |
| 4 | * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org> |
| 5 | * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com> |
| 6 | * Copyright (C) 2015 Huawei Inc. |
| 7 | */ |
| 8 | #ifndef __BPF_LIBBPF_H |
| 9 | #define __BPF_LIBBPF_H |
| 10 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 11 | #include <stdio.h> |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 12 | #include <stdbool.h> |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 13 | |
Wang Nan | b3f59d6 | 2015-07-01 02:13:52 +0000 | [diff] [blame] | 14 | /* |
| 15 | * In include/linux/compiler-gcc.h, __printf is defined. However |
| 16 | * it should be better if libbpf.h doesn't depend on Linux header file. |
| 17 | * So instead of __printf, here we use gcc attribute directly. |
| 18 | */ |
| 19 | typedef int (*libbpf_print_fn_t)(const char *, ...) |
| 20 | __attribute__((format(printf, 1, 2))); |
| 21 | |
| 22 | void libbpf_set_print(libbpf_print_fn_t warn, |
| 23 | libbpf_print_fn_t info, |
| 24 | libbpf_print_fn_t debug); |
| 25 | |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 26 | /* Hide internal to user */ |
| 27 | struct bpf_object; |
| 28 | |
| 29 | struct bpf_object *bpf_object__open(const char *path); |
Wang Nan | 6c95639 | 2015-07-01 02:13:54 +0000 | [diff] [blame] | 30 | struct bpf_object *bpf_object__open_buffer(void *obj_buf, |
| 31 | size_t obj_buf_sz); |
Wang Nan | 1a5e3fb | 2015-07-01 02:13:53 +0000 | [diff] [blame] | 32 | void bpf_object__close(struct bpf_object *object); |
| 33 | |
Wang Nan | 52d3352 | 2015-07-01 02:14:04 +0000 | [diff] [blame] | 34 | /* Load/unload object into/from kernel */ |
| 35 | int bpf_object__load(struct bpf_object *obj); |
| 36 | int bpf_object__unload(struct bpf_object *obj); |
| 37 | |
Wang Nan | 9a208ef | 2015-07-01 02:14:10 +0000 | [diff] [blame^] | 38 | struct bpf_object *bpf_object__next(struct bpf_object *prev); |
| 39 | #define bpf_object__for_each_safe(pos, tmp) \ |
| 40 | for ((pos) = bpf_object__next(NULL), \ |
| 41 | (tmp) = bpf_object__next(pos); \ |
| 42 | (pos) != NULL; \ |
| 43 | (pos) = (tmp), (tmp) = bpf_object__next(tmp)) |
| 44 | |
Wang Nan | aa9b1ac | 2015-07-01 02:14:08 +0000 | [diff] [blame] | 45 | /* Accessors of bpf_program. */ |
| 46 | struct bpf_program; |
| 47 | struct bpf_program *bpf_program__next(struct bpf_program *prog, |
| 48 | struct bpf_object *obj); |
| 49 | |
| 50 | #define bpf_object__for_each_program(pos, obj) \ |
| 51 | for ((pos) = bpf_program__next(NULL, (obj)); \ |
| 52 | (pos) != NULL; \ |
| 53 | (pos) = bpf_program__next((pos), (obj))) |
| 54 | |
| 55 | typedef void (*bpf_program_clear_priv_t)(struct bpf_program *, |
| 56 | void *); |
| 57 | |
| 58 | int bpf_program__set_private(struct bpf_program *prog, void *priv, |
| 59 | bpf_program_clear_priv_t clear_priv); |
| 60 | |
| 61 | int bpf_program__get_private(struct bpf_program *prog, |
| 62 | void **ppriv); |
| 63 | |
| 64 | const char *bpf_program__title(struct bpf_program *prog, bool dup); |
| 65 | |
| 66 | int bpf_program__fd(struct bpf_program *prog); |
| 67 | |
Wang Nan | 3409091 | 2015-07-01 02:14:02 +0000 | [diff] [blame] | 68 | /* |
| 69 | * We don't need __attribute__((packed)) now since it is |
| 70 | * unnecessary for 'bpf_map_def' because they are all aligned. |
| 71 | * In addition, using it will trigger -Wpacked warning message, |
| 72 | * and will be treated as an error due to -Werror. |
| 73 | */ |
| 74 | struct bpf_map_def { |
| 75 | unsigned int type; |
| 76 | unsigned int key_size; |
| 77 | unsigned int value_size; |
| 78 | unsigned int max_entries; |
| 79 | }; |
| 80 | |
Wang Nan | 1b76c13 | 2015-07-01 02:13:51 +0000 | [diff] [blame] | 81 | #endif |