]> asedeno.scripts.mit.edu Git - linux.git/blob - tools/lib/bpf/libbpf.h
Merge branches 'pm-core', 'pm-qos', 'pm-domains' and 'pm-opp'
[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  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation;
11  * version 2.1 of the License (not later!)
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this program; if not,  see <http://www.gnu.org/licenses>
20  */
21 #ifndef __BPF_LIBBPF_H
22 #define __BPF_LIBBPF_H
23
24 #include <stdio.h>
25 #include <stdint.h>
26 #include <stdbool.h>
27 #include <sys/types.h>  // for size_t
28
29 enum libbpf_errno {
30         __LIBBPF_ERRNO__START = 4000,
31
32         /* Something wrong in libelf */
33         LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START,
34         LIBBPF_ERRNO__FORMAT,   /* BPF object format invalid */
35         LIBBPF_ERRNO__KVERSION, /* Incorrect or no 'version' section */
36         LIBBPF_ERRNO__ENDIAN,   /* Endian mismatch */
37         LIBBPF_ERRNO__INTERNAL, /* Internal error in libbpf */
38         LIBBPF_ERRNO__RELOC,    /* Relocation failed */
39         LIBBPF_ERRNO__LOAD,     /* Load program failure for unknown reason */
40         LIBBPF_ERRNO__VERIFY,   /* Kernel verifier blocks program loading */
41         LIBBPF_ERRNO__PROG2BIG, /* Program too big */
42         LIBBPF_ERRNO__KVER,     /* Incorrect kernel version */
43         LIBBPF_ERRNO__PROGTYPE, /* Kernel doesn't support this program type */
44         __LIBBPF_ERRNO__END,
45 };
46
47 int libbpf_strerror(int err, char *buf, size_t size);
48
49 /*
50  * In include/linux/compiler-gcc.h, __printf is defined. However
51  * it should be better if libbpf.h doesn't depend on Linux header file.
52  * So instead of __printf, here we use gcc attribute directly.
53  */
54 typedef int (*libbpf_print_fn_t)(const char *, ...)
55         __attribute__((format(printf, 1, 2)));
56
57 void libbpf_set_print(libbpf_print_fn_t warn,
58                       libbpf_print_fn_t info,
59                       libbpf_print_fn_t debug);
60
61 /* Hide internal to user */
62 struct bpf_object;
63
64 struct bpf_object *bpf_object__open(const char *path);
65 struct bpf_object *bpf_object__open_buffer(void *obj_buf,
66                                            size_t obj_buf_sz,
67                                            const char *name);
68 int bpf_object__pin(struct bpf_object *object, const char *path);
69 void bpf_object__close(struct bpf_object *object);
70
71 /* Load/unload object into/from kernel */
72 int bpf_object__load(struct bpf_object *obj);
73 int bpf_object__unload(struct bpf_object *obj);
74 const char *bpf_object__name(struct bpf_object *obj);
75 unsigned int bpf_object__kversion(struct bpf_object *obj);
76
77 struct bpf_object *bpf_object__next(struct bpf_object *prev);
78 #define bpf_object__for_each_safe(pos, tmp)                     \
79         for ((pos) = bpf_object__next(NULL),            \
80                 (tmp) = bpf_object__next(pos);          \
81              (pos) != NULL;                             \
82              (pos) = (tmp), (tmp) = bpf_object__next(tmp))
83
84 typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *);
85 int bpf_object__set_priv(struct bpf_object *obj, void *priv,
86                          bpf_object_clear_priv_t clear_priv);
87 void *bpf_object__priv(struct bpf_object *prog);
88
89 /* Accessors of bpf_program. */
90 struct bpf_program;
91 struct bpf_program *bpf_program__next(struct bpf_program *prog,
92                                       struct bpf_object *obj);
93
94 #define bpf_object__for_each_program(pos, obj)          \
95         for ((pos) = bpf_program__next(NULL, (obj));    \
96              (pos) != NULL;                             \
97              (pos) = bpf_program__next((pos), (obj)))
98
99 typedef void (*bpf_program_clear_priv_t)(struct bpf_program *,
100                                          void *);
101
102 int bpf_program__set_priv(struct bpf_program *prog, void *priv,
103                           bpf_program_clear_priv_t clear_priv);
104
105 void *bpf_program__priv(struct bpf_program *prog);
106
107 const char *bpf_program__title(struct bpf_program *prog, bool needs_copy);
108
109 int bpf_program__fd(struct bpf_program *prog);
110 int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
111                               int instance);
112 int bpf_program__pin(struct bpf_program *prog, const char *path);
113
114 struct bpf_insn;
115
116 /*
117  * Libbpf allows callers to adjust BPF programs before being loaded
118  * into kernel. One program in an object file can be transform into
119  * multiple variants to be attached to different code.
120  *
121  * bpf_program_prep_t, bpf_program__set_prep and bpf_program__nth_fd
122  * are APIs for this propose.
123  *
124  * - bpf_program_prep_t:
125  *   It defines 'preprocessor', which is a caller defined function
126  *   passed to libbpf through bpf_program__set_prep(), and will be
127  *   called before program is loaded. The processor should adjust
128  *   the program one time for each instances according to the number
129  *   passed to it.
130  *
131  * - bpf_program__set_prep:
132  *   Attachs a preprocessor to a BPF program. The number of instances
133  *   whould be created is also passed through this function.
134  *
135  * - bpf_program__nth_fd:
136  *   After the program is loaded, get resuling fds from bpf program for
137  *   each instances.
138  *
139  * If bpf_program__set_prep() is not used, the program whould be loaded
140  * without adjustment during bpf_object__load(). The program has only
141  * one instance. In this case bpf_program__fd(prog) is equal to
142  * bpf_program__nth_fd(prog, 0).
143  */
144
145 struct bpf_prog_prep_result {
146         /*
147          * If not NULL, load new instruction array.
148          * If set to NULL, don't load this instance.
149          */
150         struct bpf_insn *new_insn_ptr;
151         int new_insn_cnt;
152
153         /* If not NULL, result fd is set to it */
154         int *pfd;
155 };
156
157 /*
158  * Parameters of bpf_program_prep_t:
159  *  - prog:     The bpf_program being loaded.
160  *  - n:        Index of instance being generated.
161  *  - insns:    BPF instructions array.
162  *  - insns_cnt:Number of instructions in insns.
163  *  - res:      Output parameter, result of transformation.
164  *
165  * Return value:
166  *  - Zero: pre-processing success.
167  *  - Non-zero: pre-processing, stop loading.
168  */
169 typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n,
170                                   struct bpf_insn *insns, int insns_cnt,
171                                   struct bpf_prog_prep_result *res);
172
173 int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
174                           bpf_program_prep_t prep);
175
176 int bpf_program__nth_fd(struct bpf_program *prog, int n);
177
178 /*
179  * Adjust type of bpf program. Default is kprobe.
180  */
181 int bpf_program__set_socket_filter(struct bpf_program *prog);
182 int bpf_program__set_tracepoint(struct bpf_program *prog);
183 int bpf_program__set_kprobe(struct bpf_program *prog);
184 int bpf_program__set_sched_cls(struct bpf_program *prog);
185 int bpf_program__set_sched_act(struct bpf_program *prog);
186 int bpf_program__set_xdp(struct bpf_program *prog);
187 int bpf_program__set_perf_event(struct bpf_program *prog);
188
189 bool bpf_program__is_socket_filter(struct bpf_program *prog);
190 bool bpf_program__is_tracepoint(struct bpf_program *prog);
191 bool bpf_program__is_kprobe(struct bpf_program *prog);
192 bool bpf_program__is_sched_cls(struct bpf_program *prog);
193 bool bpf_program__is_sched_act(struct bpf_program *prog);
194 bool bpf_program__is_xdp(struct bpf_program *prog);
195 bool bpf_program__is_perf_event(struct bpf_program *prog);
196
197 /*
198  * We don't need __attribute__((packed)) now since it is
199  * unnecessary for 'bpf_map_def' because they are all aligned.
200  * In addition, using it will trigger -Wpacked warning message,
201  * and will be treated as an error due to -Werror.
202  */
203 struct bpf_map_def {
204         unsigned int type;
205         unsigned int key_size;
206         unsigned int value_size;
207         unsigned int max_entries;
208 };
209
210 /*
211  * There is another 'struct bpf_map' in include/linux/map.h. However,
212  * it is not a uapi header so no need to consider name clash.
213  */
214 struct bpf_map;
215 struct bpf_map *
216 bpf_object__find_map_by_name(struct bpf_object *obj, const char *name);
217
218 /*
219  * Get bpf_map through the offset of corresponding struct bpf_map_def
220  * in the bpf object file.
221  */
222 struct bpf_map *
223 bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset);
224
225 struct bpf_map *
226 bpf_map__next(struct bpf_map *map, struct bpf_object *obj);
227 #define bpf_map__for_each(pos, obj)             \
228         for ((pos) = bpf_map__next(NULL, (obj));        \
229              (pos) != NULL;                             \
230              (pos) = bpf_map__next((pos), (obj)))
231
232 int bpf_map__fd(struct bpf_map *map);
233 const struct bpf_map_def *bpf_map__def(struct bpf_map *map);
234 const char *bpf_map__name(struct bpf_map *map);
235
236 typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
237 int bpf_map__set_priv(struct bpf_map *map, void *priv,
238                       bpf_map_clear_priv_t clear_priv);
239 void *bpf_map__priv(struct bpf_map *map);
240 int bpf_map__pin(struct bpf_map *map, const char *path);
241
242 long libbpf_get_error(const void *ptr);
243
244 #endif