bpf tools: Introduce accessors for struct bpf_program
[cascardo/linux.git] / tools / lib / bpf / libbpf.h
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
11 #include <stdio.h>
12 #include <stdbool.h>
13
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
26 /* Hide internal to user */
27 struct bpf_object;
28
29 struct bpf_object *bpf_object__open(const char *path);
30 struct bpf_object *bpf_object__open_buffer(void *obj_buf,
31                                            size_t obj_buf_sz);
32 void bpf_object__close(struct bpf_object *object);
33
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
38 /* Accessors of bpf_program. */
39 struct bpf_program;
40 struct bpf_program *bpf_program__next(struct bpf_program *prog,
41                                       struct bpf_object *obj);
42
43 #define bpf_object__for_each_program(pos, obj)          \
44         for ((pos) = bpf_program__next(NULL, (obj));    \
45              (pos) != NULL;                             \
46              (pos) = bpf_program__next((pos), (obj)))
47
48 typedef void (*bpf_program_clear_priv_t)(struct bpf_program *,
49                                          void *);
50
51 int bpf_program__set_private(struct bpf_program *prog, void *priv,
52                              bpf_program_clear_priv_t clear_priv);
53
54 int bpf_program__get_private(struct bpf_program *prog,
55                              void **ppriv);
56
57 const char *bpf_program__title(struct bpf_program *prog, bool dup);
58
59 int bpf_program__fd(struct bpf_program *prog);
60
61 /*
62  * We don't need __attribute__((packed)) now since it is
63  * unnecessary for 'bpf_map_def' because they are all aligned.
64  * In addition, using it will trigger -Wpacked warning message,
65  * and will be treated as an error due to -Werror.
66  */
67 struct bpf_map_def {
68         unsigned int type;
69         unsigned int key_size;
70         unsigned int value_size;
71         unsigned int max_entries;
72 };
73
74 #endif