7e9c801a9fddbfbb51b9a1fa5989b6d974ab8d45
[linux-2.6-microblaze.git] / tools / lib / bpf / libbpf.h
1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2
3 /*
4  * Common eBPF ELF object loading operations.
5  *
6  * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
7  * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
8  * Copyright (C) 2015 Huawei Inc.
9  */
10 #ifndef __LIBBPF_LIBBPF_H
11 #define __LIBBPF_LIBBPF_H
12
13 #include <stdio.h>
14 #include <stdint.h>
15 #include <stdbool.h>
16 #include <sys/types.h>  // for size_t
17 #include <linux/bpf.h>
18
19 enum libbpf_errno {
20         __LIBBPF_ERRNO__START = 4000,
21
22         /* Something wrong in libelf */
23         LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START,
24         LIBBPF_ERRNO__FORMAT,   /* BPF object format invalid */
25         LIBBPF_ERRNO__KVERSION, /* Incorrect or no 'version' section */
26         LIBBPF_ERRNO__ENDIAN,   /* Endian mismatch */
27         LIBBPF_ERRNO__INTERNAL, /* Internal error in libbpf */
28         LIBBPF_ERRNO__RELOC,    /* Relocation failed */
29         LIBBPF_ERRNO__LOAD,     /* Load program failure for unknown reason */
30         LIBBPF_ERRNO__VERIFY,   /* Kernel verifier blocks program loading */
31         LIBBPF_ERRNO__PROG2BIG, /* Program too big */
32         LIBBPF_ERRNO__KVER,     /* Incorrect kernel version */
33         LIBBPF_ERRNO__PROGTYPE, /* Kernel doesn't support this program type */
34         LIBBPF_ERRNO__WRNGPID,  /* Wrong pid in netlink message */
35         LIBBPF_ERRNO__INVSEQ,   /* Invalid netlink sequence */
36         LIBBPF_ERRNO__NLPARSE,  /* netlink parsing error */
37         __LIBBPF_ERRNO__END,
38 };
39
40 int libbpf_strerror(int err, char *buf, size_t size);
41
42 /*
43  * __printf is defined in include/linux/compiler-gcc.h. However,
44  * it would be better if libbpf.h didn't depend on Linux header files.
45  * So instead of __printf, here we use gcc attribute directly.
46  */
47 typedef int (*libbpf_print_fn_t)(const char *, ...)
48         __attribute__((format(printf, 1, 2)));
49
50 void libbpf_set_print(libbpf_print_fn_t warn,
51                       libbpf_print_fn_t info,
52                       libbpf_print_fn_t debug);
53
54 /* Hide internal to user */
55 struct bpf_object;
56
57 struct bpf_object_open_attr {
58         const char *file;
59         enum bpf_prog_type prog_type;
60 };
61
62 struct bpf_object *bpf_object__open(const char *path);
63 struct bpf_object *bpf_object__open_xattr(struct bpf_object_open_attr *attr);
64 struct bpf_object *__bpf_object__open_xattr(struct bpf_object_open_attr *attr,
65                                             int flags);
66 struct bpf_object *bpf_object__open_buffer(void *obj_buf,
67                                            size_t obj_buf_sz,
68                                            const char *name);
69 int bpf_object__pin(struct bpf_object *object, const char *path);
70 void bpf_object__close(struct bpf_object *object);
71
72 /* Load/unload object into/from kernel */
73 int bpf_object__load(struct bpf_object *obj);
74 int bpf_object__unload(struct bpf_object *obj);
75 const char *bpf_object__name(struct bpf_object *obj);
76 unsigned int bpf_object__kversion(struct bpf_object *obj);
77 int bpf_object__btf_fd(const struct bpf_object *obj);
78
79 struct bpf_program *
80 bpf_object__find_program_by_title(struct bpf_object *obj, const char *title);
81
82 struct bpf_object *bpf_object__next(struct bpf_object *prev);
83 #define bpf_object__for_each_safe(pos, tmp)                     \
84         for ((pos) = bpf_object__next(NULL),            \
85                 (tmp) = bpf_object__next(pos);          \
86              (pos) != NULL;                             \
87              (pos) = (tmp), (tmp) = bpf_object__next(tmp))
88
89 typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *);
90 int bpf_object__set_priv(struct bpf_object *obj, void *priv,
91                          bpf_object_clear_priv_t clear_priv);
92 void *bpf_object__priv(struct bpf_object *prog);
93
94 int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
95                              enum bpf_attach_type *expected_attach_type);
96 int libbpf_attach_type_by_name(const char *name,
97                                enum bpf_attach_type *attach_type);
98
99 /* Accessors of bpf_program */
100 struct bpf_program;
101 struct bpf_program *bpf_program__next(struct bpf_program *prog,
102                                       struct bpf_object *obj);
103
104 #define bpf_object__for_each_program(pos, obj)          \
105         for ((pos) = bpf_program__next(NULL, (obj));    \
106              (pos) != NULL;                             \
107              (pos) = bpf_program__next((pos), (obj)))
108
109 typedef void (*bpf_program_clear_priv_t)(struct bpf_program *,
110                                          void *);
111
112 int bpf_program__set_priv(struct bpf_program *prog, void *priv,
113                           bpf_program_clear_priv_t clear_priv);
114
115 void *bpf_program__priv(struct bpf_program *prog);
116 void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex);
117
118 const char *bpf_program__title(struct bpf_program *prog, bool needs_copy);
119
120 int bpf_program__load(struct bpf_program *prog, char *license,
121                       __u32 kern_version);
122 int bpf_program__fd(struct bpf_program *prog);
123 int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
124                               int instance);
125 int bpf_program__pin(struct bpf_program *prog, const char *path);
126 void bpf_program__unload(struct bpf_program *prog);
127
128 struct bpf_insn;
129
130 /*
131  * Libbpf allows callers to adjust BPF programs before being loaded
132  * into kernel. One program in an object file can be transformed into
133  * multiple variants to be attached to different hooks.
134  *
135  * bpf_program_prep_t, bpf_program__set_prep and bpf_program__nth_fd
136  * form an API for this purpose.
137  *
138  * - bpf_program_prep_t:
139  *   Defines a 'preprocessor', which is a caller defined function
140  *   passed to libbpf through bpf_program__set_prep(), and will be
141  *   called before program is loaded. The processor should adjust
142  *   the program one time for each instance according to the instance id
143  *   passed to it.
144  *
145  * - bpf_program__set_prep:
146  *   Attaches a preprocessor to a BPF program. The number of instances
147  *   that should be created is also passed through this function.
148  *
149  * - bpf_program__nth_fd:
150  *   After the program is loaded, get resulting FD of a given instance
151  *   of the BPF program.
152  *
153  * If bpf_program__set_prep() is not used, the program would be loaded
154  * without adjustment during bpf_object__load(). The program has only
155  * one instance. In this case bpf_program__fd(prog) is equal to
156  * bpf_program__nth_fd(prog, 0).
157  */
158
159 struct bpf_prog_prep_result {
160         /*
161          * If not NULL, load new instruction array.
162          * If set to NULL, don't load this instance.
163          */
164         struct bpf_insn *new_insn_ptr;
165         int new_insn_cnt;
166
167         /* If not NULL, result FD is written to it. */
168         int *pfd;
169 };
170
171 /*
172  * Parameters of bpf_program_prep_t:
173  *  - prog:     The bpf_program being loaded.
174  *  - n:        Index of instance being generated.
175  *  - insns:    BPF instructions array.
176  *  - insns_cnt:Number of instructions in insns.
177  *  - res:      Output parameter, result of transformation.
178  *
179  * Return value:
180  *  - Zero:     pre-processing success.
181  *  - Non-zero: pre-processing error, stop loading.
182  */
183 typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n,
184                                   struct bpf_insn *insns, int insns_cnt,
185                                   struct bpf_prog_prep_result *res);
186
187 int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
188                           bpf_program_prep_t prep);
189
190 int bpf_program__nth_fd(struct bpf_program *prog, int n);
191
192 /*
193  * Adjust type of BPF program. Default is kprobe.
194  */
195 int bpf_program__set_socket_filter(struct bpf_program *prog);
196 int bpf_program__set_tracepoint(struct bpf_program *prog);
197 int bpf_program__set_raw_tracepoint(struct bpf_program *prog);
198 int bpf_program__set_kprobe(struct bpf_program *prog);
199 int bpf_program__set_sched_cls(struct bpf_program *prog);
200 int bpf_program__set_sched_act(struct bpf_program *prog);
201 int bpf_program__set_xdp(struct bpf_program *prog);
202 int bpf_program__set_perf_event(struct bpf_program *prog);
203 void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type);
204 void bpf_program__set_expected_attach_type(struct bpf_program *prog,
205                                            enum bpf_attach_type type);
206
207 bool bpf_program__is_socket_filter(struct bpf_program *prog);
208 bool bpf_program__is_tracepoint(struct bpf_program *prog);
209 bool bpf_program__is_raw_tracepoint(struct bpf_program *prog);
210 bool bpf_program__is_kprobe(struct bpf_program *prog);
211 bool bpf_program__is_sched_cls(struct bpf_program *prog);
212 bool bpf_program__is_sched_act(struct bpf_program *prog);
213 bool bpf_program__is_xdp(struct bpf_program *prog);
214 bool bpf_program__is_perf_event(struct bpf_program *prog);
215
216 /*
217  * No need for __attribute__((packed)), all members of 'bpf_map_def'
218  * are all aligned.  In addition, using __attribute__((packed))
219  * would trigger a -Wpacked warning message, and lead to an error
220  * if -Werror is set.
221  */
222 struct bpf_map_def {
223         unsigned int type;
224         unsigned int key_size;
225         unsigned int value_size;
226         unsigned int max_entries;
227         unsigned int map_flags;
228 };
229
230 /*
231  * The 'struct bpf_map' in include/linux/bpf.h is internal to the kernel,
232  * so no need to worry about a name clash.
233  */
234 struct bpf_map;
235 struct bpf_map *
236 bpf_object__find_map_by_name(struct bpf_object *obj, const char *name);
237
238 /*
239  * Get bpf_map through the offset of corresponding struct bpf_map_def
240  * in the BPF object file.
241  */
242 struct bpf_map *
243 bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset);
244
245 struct bpf_map *
246 bpf_map__next(struct bpf_map *map, struct bpf_object *obj);
247 #define bpf_map__for_each(pos, obj)             \
248         for ((pos) = bpf_map__next(NULL, (obj));        \
249              (pos) != NULL;                             \
250              (pos) = bpf_map__next((pos), (obj)))
251
252 int bpf_map__fd(struct bpf_map *map);
253 const struct bpf_map_def *bpf_map__def(struct bpf_map *map);
254 const char *bpf_map__name(struct bpf_map *map);
255 __u32 bpf_map__btf_key_type_id(const struct bpf_map *map);
256 __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);
257
258 typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
259 int bpf_map__set_priv(struct bpf_map *map, void *priv,
260                       bpf_map_clear_priv_t clear_priv);
261 void *bpf_map__priv(struct bpf_map *map);
262 int bpf_map__reuse_fd(struct bpf_map *map, int fd);
263 bool bpf_map__is_offload_neutral(struct bpf_map *map);
264 void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
265 int bpf_map__pin(struct bpf_map *map, const char *path);
266
267 long libbpf_get_error(const void *ptr);
268
269 struct bpf_prog_load_attr {
270         const char *file;
271         enum bpf_prog_type prog_type;
272         enum bpf_attach_type expected_attach_type;
273         int ifindex;
274 };
275
276 int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
277                         struct bpf_object **pobj, int *prog_fd);
278 int bpf_prog_load(const char *file, enum bpf_prog_type type,
279                   struct bpf_object **pobj, int *prog_fd);
280
281 int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags);
282
283 enum bpf_perf_event_ret {
284         LIBBPF_PERF_EVENT_DONE  = 0,
285         LIBBPF_PERF_EVENT_ERROR = -1,
286         LIBBPF_PERF_EVENT_CONT  = -2,
287 };
288
289 typedef enum bpf_perf_event_ret (*bpf_perf_event_print_t)(void *event,
290                                                           void *priv);
291 int bpf_perf_event_read_simple(void *mem, unsigned long size,
292                                unsigned long page_size,
293                                void **buf, size_t *buf_len,
294                                bpf_perf_event_print_t fn, void *priv);
295
296 struct nlattr;
297 typedef int (*libbpf_dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb);
298 int libbpf_netlink_open(unsigned int *nl_pid);
299 int libbpf_nl_get_link(int sock, unsigned int nl_pid,
300                        libbpf_dump_nlmsg_t dump_link_nlmsg, void *cookie);
301 int libbpf_nl_get_class(int sock, unsigned int nl_pid, int ifindex,
302                         libbpf_dump_nlmsg_t dump_class_nlmsg, void *cookie);
303 int libbpf_nl_get_qdisc(int sock, unsigned int nl_pid, int ifindex,
304                         libbpf_dump_nlmsg_t dump_qdisc_nlmsg, void *cookie);
305 int libbpf_nl_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle,
306                          libbpf_dump_nlmsg_t dump_filter_nlmsg, void *cookie);
307 #endif /* __LIBBPF_LIBBPF_H */