diff options
Diffstat (limited to 'tools/lib/bpf/libbpf.h')
-rw-r--r-- | tools/lib/bpf/libbpf.h | 186 |
1 files changed, 99 insertions, 87 deletions
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index 7e9c801a9fdd..1354cc9f8cba 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h | |||
@@ -16,6 +16,10 @@ | |||
16 | #include <sys/types.h> // for size_t | 16 | #include <sys/types.h> // for size_t |
17 | #include <linux/bpf.h> | 17 | #include <linux/bpf.h> |
18 | 18 | ||
19 | #ifndef LIBBPF_API | ||
20 | #define LIBBPF_API __attribute__((visibility("default"))) | ||
21 | #endif | ||
22 | |||
19 | enum libbpf_errno { | 23 | enum libbpf_errno { |
20 | __LIBBPF_ERRNO__START = 4000, | 24 | __LIBBPF_ERRNO__START = 4000, |
21 | 25 | ||
@@ -37,7 +41,7 @@ enum libbpf_errno { | |||
37 | __LIBBPF_ERRNO__END, | 41 | __LIBBPF_ERRNO__END, |
38 | }; | 42 | }; |
39 | 43 | ||
40 | int libbpf_strerror(int err, char *buf, size_t size); | 44 | LIBBPF_API int libbpf_strerror(int err, char *buf, size_t size); |
41 | 45 | ||
42 | /* | 46 | /* |
43 | * __printf is defined in include/linux/compiler-gcc.h. However, | 47 | * __printf is defined in include/linux/compiler-gcc.h. However, |
@@ -47,9 +51,9 @@ int libbpf_strerror(int err, char *buf, size_t size); | |||
47 | typedef int (*libbpf_print_fn_t)(const char *, ...) | 51 | typedef int (*libbpf_print_fn_t)(const char *, ...) |
48 | __attribute__((format(printf, 1, 2))); | 52 | __attribute__((format(printf, 1, 2))); |
49 | 53 | ||
50 | void libbpf_set_print(libbpf_print_fn_t warn, | 54 | LIBBPF_API void libbpf_set_print(libbpf_print_fn_t warn, |
51 | libbpf_print_fn_t info, | 55 | libbpf_print_fn_t info, |
52 | libbpf_print_fn_t debug); | 56 | libbpf_print_fn_t debug); |
53 | 57 | ||
54 | /* Hide internal to user */ | 58 | /* Hide internal to user */ |
55 | struct bpf_object; | 59 | struct bpf_object; |
@@ -59,27 +63,28 @@ struct bpf_object_open_attr { | |||
59 | enum bpf_prog_type prog_type; | 63 | enum bpf_prog_type prog_type; |
60 | }; | 64 | }; |
61 | 65 | ||
62 | struct bpf_object *bpf_object__open(const char *path); | 66 | LIBBPF_API struct bpf_object *bpf_object__open(const char *path); |
63 | struct bpf_object *bpf_object__open_xattr(struct bpf_object_open_attr *attr); | 67 | LIBBPF_API struct bpf_object * |
68 | bpf_object__open_xattr(struct bpf_object_open_attr *attr); | ||
64 | struct bpf_object *__bpf_object__open_xattr(struct bpf_object_open_attr *attr, | 69 | struct bpf_object *__bpf_object__open_xattr(struct bpf_object_open_attr *attr, |
65 | int flags); | 70 | int flags); |
66 | struct bpf_object *bpf_object__open_buffer(void *obj_buf, | 71 | LIBBPF_API struct bpf_object *bpf_object__open_buffer(void *obj_buf, |
67 | size_t obj_buf_sz, | 72 | size_t obj_buf_sz, |
68 | const char *name); | 73 | const char *name); |
69 | int bpf_object__pin(struct bpf_object *object, const char *path); | 74 | LIBBPF_API int bpf_object__pin(struct bpf_object *object, const char *path); |
70 | void bpf_object__close(struct bpf_object *object); | 75 | LIBBPF_API void bpf_object__close(struct bpf_object *object); |
71 | 76 | ||
72 | /* Load/unload object into/from kernel */ | 77 | /* Load/unload object into/from kernel */ |
73 | int bpf_object__load(struct bpf_object *obj); | 78 | LIBBPF_API int bpf_object__load(struct bpf_object *obj); |
74 | int bpf_object__unload(struct bpf_object *obj); | 79 | LIBBPF_API int bpf_object__unload(struct bpf_object *obj); |
75 | const char *bpf_object__name(struct bpf_object *obj); | 80 | LIBBPF_API const char *bpf_object__name(struct bpf_object *obj); |
76 | unsigned int bpf_object__kversion(struct bpf_object *obj); | 81 | LIBBPF_API unsigned int bpf_object__kversion(struct bpf_object *obj); |
77 | int bpf_object__btf_fd(const struct bpf_object *obj); | 82 | LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj); |
78 | 83 | ||
79 | struct bpf_program * | 84 | LIBBPF_API struct bpf_program * |
80 | bpf_object__find_program_by_title(struct bpf_object *obj, const char *title); | 85 | bpf_object__find_program_by_title(struct bpf_object *obj, const char *title); |
81 | 86 | ||
82 | struct bpf_object *bpf_object__next(struct bpf_object *prev); | 87 | LIBBPF_API struct bpf_object *bpf_object__next(struct bpf_object *prev); |
83 | #define bpf_object__for_each_safe(pos, tmp) \ | 88 | #define bpf_object__for_each_safe(pos, tmp) \ |
84 | for ((pos) = bpf_object__next(NULL), \ | 89 | for ((pos) = bpf_object__next(NULL), \ |
85 | (tmp) = bpf_object__next(pos); \ | 90 | (tmp) = bpf_object__next(pos); \ |
@@ -87,19 +92,20 @@ struct bpf_object *bpf_object__next(struct bpf_object *prev); | |||
87 | (pos) = (tmp), (tmp) = bpf_object__next(tmp)) | 92 | (pos) = (tmp), (tmp) = bpf_object__next(tmp)) |
88 | 93 | ||
89 | typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *); | 94 | typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *); |
90 | int bpf_object__set_priv(struct bpf_object *obj, void *priv, | 95 | LIBBPF_API int bpf_object__set_priv(struct bpf_object *obj, void *priv, |
91 | bpf_object_clear_priv_t clear_priv); | 96 | bpf_object_clear_priv_t clear_priv); |
92 | void *bpf_object__priv(struct bpf_object *prog); | 97 | LIBBPF_API void *bpf_object__priv(struct bpf_object *prog); |
93 | 98 | ||
94 | int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type, | 99 | LIBBPF_API int |
95 | enum bpf_attach_type *expected_attach_type); | 100 | libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type, |
96 | int libbpf_attach_type_by_name(const char *name, | 101 | enum bpf_attach_type *expected_attach_type); |
97 | enum bpf_attach_type *attach_type); | 102 | LIBBPF_API int libbpf_attach_type_by_name(const char *name, |
103 | enum bpf_attach_type *attach_type); | ||
98 | 104 | ||
99 | /* Accessors of bpf_program */ | 105 | /* Accessors of bpf_program */ |
100 | struct bpf_program; | 106 | struct bpf_program; |
101 | struct bpf_program *bpf_program__next(struct bpf_program *prog, | 107 | LIBBPF_API struct bpf_program *bpf_program__next(struct bpf_program *prog, |
102 | struct bpf_object *obj); | 108 | struct bpf_object *obj); |
103 | 109 | ||
104 | #define bpf_object__for_each_program(pos, obj) \ | 110 | #define bpf_object__for_each_program(pos, obj) \ |
105 | for ((pos) = bpf_program__next(NULL, (obj)); \ | 111 | for ((pos) = bpf_program__next(NULL, (obj)); \ |
@@ -109,21 +115,24 @@ struct bpf_program *bpf_program__next(struct bpf_program *prog, | |||
109 | typedef void (*bpf_program_clear_priv_t)(struct bpf_program *, | 115 | typedef void (*bpf_program_clear_priv_t)(struct bpf_program *, |
110 | void *); | 116 | void *); |
111 | 117 | ||
112 | int bpf_program__set_priv(struct bpf_program *prog, void *priv, | 118 | LIBBPF_API int bpf_program__set_priv(struct bpf_program *prog, void *priv, |
113 | bpf_program_clear_priv_t clear_priv); | 119 | bpf_program_clear_priv_t clear_priv); |
114 | 120 | ||
115 | void *bpf_program__priv(struct bpf_program *prog); | 121 | LIBBPF_API void *bpf_program__priv(struct bpf_program *prog); |
116 | void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex); | 122 | LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog, |
123 | __u32 ifindex); | ||
117 | 124 | ||
118 | const char *bpf_program__title(struct bpf_program *prog, bool needs_copy); | 125 | LIBBPF_API const char *bpf_program__title(struct bpf_program *prog, |
126 | bool needs_copy); | ||
119 | 127 | ||
120 | int bpf_program__load(struct bpf_program *prog, char *license, | 128 | LIBBPF_API int bpf_program__load(struct bpf_program *prog, char *license, |
121 | __u32 kern_version); | 129 | __u32 kern_version); |
122 | int bpf_program__fd(struct bpf_program *prog); | 130 | LIBBPF_API int bpf_program__fd(struct bpf_program *prog); |
123 | int bpf_program__pin_instance(struct bpf_program *prog, const char *path, | 131 | LIBBPF_API int bpf_program__pin_instance(struct bpf_program *prog, |
124 | int instance); | 132 | const char *path, |
125 | int bpf_program__pin(struct bpf_program *prog, const char *path); | 133 | int instance); |
126 | void bpf_program__unload(struct bpf_program *prog); | 134 | LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path); |
135 | LIBBPF_API void bpf_program__unload(struct bpf_program *prog); | ||
127 | 136 | ||
128 | struct bpf_insn; | 137 | struct bpf_insn; |
129 | 138 | ||
@@ -184,34 +193,36 @@ typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n, | |||
184 | struct bpf_insn *insns, int insns_cnt, | 193 | struct bpf_insn *insns, int insns_cnt, |
185 | struct bpf_prog_prep_result *res); | 194 | struct bpf_prog_prep_result *res); |
186 | 195 | ||
187 | int bpf_program__set_prep(struct bpf_program *prog, int nr_instance, | 196 | LIBBPF_API int bpf_program__set_prep(struct bpf_program *prog, int nr_instance, |
188 | bpf_program_prep_t prep); | 197 | bpf_program_prep_t prep); |
189 | 198 | ||
190 | int bpf_program__nth_fd(struct bpf_program *prog, int n); | 199 | LIBBPF_API int bpf_program__nth_fd(struct bpf_program *prog, int n); |
191 | 200 | ||
192 | /* | 201 | /* |
193 | * Adjust type of BPF program. Default is kprobe. | 202 | * Adjust type of BPF program. Default is kprobe. |
194 | */ | 203 | */ |
195 | int bpf_program__set_socket_filter(struct bpf_program *prog); | 204 | LIBBPF_API int bpf_program__set_socket_filter(struct bpf_program *prog); |
196 | int bpf_program__set_tracepoint(struct bpf_program *prog); | 205 | LIBBPF_API int bpf_program__set_tracepoint(struct bpf_program *prog); |
197 | int bpf_program__set_raw_tracepoint(struct bpf_program *prog); | 206 | LIBBPF_API int bpf_program__set_raw_tracepoint(struct bpf_program *prog); |
198 | int bpf_program__set_kprobe(struct bpf_program *prog); | 207 | LIBBPF_API int bpf_program__set_kprobe(struct bpf_program *prog); |
199 | int bpf_program__set_sched_cls(struct bpf_program *prog); | 208 | LIBBPF_API int bpf_program__set_sched_cls(struct bpf_program *prog); |
200 | int bpf_program__set_sched_act(struct bpf_program *prog); | 209 | LIBBPF_API int bpf_program__set_sched_act(struct bpf_program *prog); |
201 | int bpf_program__set_xdp(struct bpf_program *prog); | 210 | LIBBPF_API int bpf_program__set_xdp(struct bpf_program *prog); |
202 | int bpf_program__set_perf_event(struct bpf_program *prog); | 211 | LIBBPF_API 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); | 212 | LIBBPF_API void bpf_program__set_type(struct bpf_program *prog, |
204 | void bpf_program__set_expected_attach_type(struct bpf_program *prog, | 213 | enum bpf_prog_type type); |
205 | enum bpf_attach_type type); | 214 | LIBBPF_API void |
206 | 215 | bpf_program__set_expected_attach_type(struct bpf_program *prog, | |
207 | bool bpf_program__is_socket_filter(struct bpf_program *prog); | 216 | enum bpf_attach_type type); |
208 | bool bpf_program__is_tracepoint(struct bpf_program *prog); | 217 | |
209 | bool bpf_program__is_raw_tracepoint(struct bpf_program *prog); | 218 | LIBBPF_API bool bpf_program__is_socket_filter(struct bpf_program *prog); |
210 | bool bpf_program__is_kprobe(struct bpf_program *prog); | 219 | LIBBPF_API bool bpf_program__is_tracepoint(struct bpf_program *prog); |
211 | bool bpf_program__is_sched_cls(struct bpf_program *prog); | 220 | LIBBPF_API bool bpf_program__is_raw_tracepoint(struct bpf_program *prog); |
212 | bool bpf_program__is_sched_act(struct bpf_program *prog); | 221 | LIBBPF_API bool bpf_program__is_kprobe(struct bpf_program *prog); |
213 | bool bpf_program__is_xdp(struct bpf_program *prog); | 222 | LIBBPF_API bool bpf_program__is_sched_cls(struct bpf_program *prog); |
214 | bool bpf_program__is_perf_event(struct bpf_program *prog); | 223 | LIBBPF_API bool bpf_program__is_sched_act(struct bpf_program *prog); |
224 | LIBBPF_API bool bpf_program__is_xdp(struct bpf_program *prog); | ||
225 | LIBBPF_API bool bpf_program__is_perf_event(struct bpf_program *prog); | ||
215 | 226 | ||
216 | /* | 227 | /* |
217 | * No need for __attribute__((packed)), all members of 'bpf_map_def' | 228 | * No need for __attribute__((packed)), all members of 'bpf_map_def' |
@@ -232,39 +243,39 @@ struct bpf_map_def { | |||
232 | * so no need to worry about a name clash. | 243 | * so no need to worry about a name clash. |
233 | */ | 244 | */ |
234 | struct bpf_map; | 245 | struct bpf_map; |
235 | struct bpf_map * | 246 | LIBBPF_API struct bpf_map * |
236 | bpf_object__find_map_by_name(struct bpf_object *obj, const char *name); | 247 | bpf_object__find_map_by_name(struct bpf_object *obj, const char *name); |
237 | 248 | ||
238 | /* | 249 | /* |
239 | * Get bpf_map through the offset of corresponding struct bpf_map_def | 250 | * Get bpf_map through the offset of corresponding struct bpf_map_def |
240 | * in the BPF object file. | 251 | * in the BPF object file. |
241 | */ | 252 | */ |
242 | struct bpf_map * | 253 | LIBBPF_API struct bpf_map * |
243 | bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset); | 254 | bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset); |
244 | 255 | ||
245 | struct bpf_map * | 256 | LIBBPF_API struct bpf_map * |
246 | bpf_map__next(struct bpf_map *map, struct bpf_object *obj); | 257 | bpf_map__next(struct bpf_map *map, struct bpf_object *obj); |
247 | #define bpf_map__for_each(pos, obj) \ | 258 | #define bpf_map__for_each(pos, obj) \ |
248 | for ((pos) = bpf_map__next(NULL, (obj)); \ | 259 | for ((pos) = bpf_map__next(NULL, (obj)); \ |
249 | (pos) != NULL; \ | 260 | (pos) != NULL; \ |
250 | (pos) = bpf_map__next((pos), (obj))) | 261 | (pos) = bpf_map__next((pos), (obj))) |
251 | 262 | ||
252 | int bpf_map__fd(struct bpf_map *map); | 263 | LIBBPF_API int bpf_map__fd(struct bpf_map *map); |
253 | const struct bpf_map_def *bpf_map__def(struct bpf_map *map); | 264 | LIBBPF_API const struct bpf_map_def *bpf_map__def(struct bpf_map *map); |
254 | const char *bpf_map__name(struct bpf_map *map); | 265 | LIBBPF_API const char *bpf_map__name(struct bpf_map *map); |
255 | __u32 bpf_map__btf_key_type_id(const struct bpf_map *map); | 266 | LIBBPF_API __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); | 267 | LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map); |
257 | 268 | ||
258 | typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *); | 269 | typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *); |
259 | int bpf_map__set_priv(struct bpf_map *map, void *priv, | 270 | LIBBPF_API int bpf_map__set_priv(struct bpf_map *map, void *priv, |
260 | bpf_map_clear_priv_t clear_priv); | 271 | bpf_map_clear_priv_t clear_priv); |
261 | void *bpf_map__priv(struct bpf_map *map); | 272 | LIBBPF_API void *bpf_map__priv(struct bpf_map *map); |
262 | int bpf_map__reuse_fd(struct bpf_map *map, int fd); | 273 | LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd); |
263 | bool bpf_map__is_offload_neutral(struct bpf_map *map); | 274 | LIBBPF_API bool bpf_map__is_offload_neutral(struct bpf_map *map); |
264 | void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex); | 275 | LIBBPF_API void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex); |
265 | int bpf_map__pin(struct bpf_map *map, const char *path); | 276 | LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path); |
266 | 277 | ||
267 | long libbpf_get_error(const void *ptr); | 278 | LIBBPF_API long libbpf_get_error(const void *ptr); |
268 | 279 | ||
269 | struct bpf_prog_load_attr { | 280 | struct bpf_prog_load_attr { |
270 | const char *file; | 281 | const char *file; |
@@ -273,12 +284,12 @@ struct bpf_prog_load_attr { | |||
273 | int ifindex; | 284 | int ifindex; |
274 | }; | 285 | }; |
275 | 286 | ||
276 | int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr, | 287 | LIBBPF_API int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr, |
277 | struct bpf_object **pobj, int *prog_fd); | 288 | struct bpf_object **pobj, int *prog_fd); |
278 | int bpf_prog_load(const char *file, enum bpf_prog_type type, | 289 | LIBBPF_API int bpf_prog_load(const char *file, enum bpf_prog_type type, |
279 | struct bpf_object **pobj, int *prog_fd); | 290 | struct bpf_object **pobj, int *prog_fd); |
280 | 291 | ||
281 | int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags); | 292 | LIBBPF_API int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags); |
282 | 293 | ||
283 | enum bpf_perf_event_ret { | 294 | enum bpf_perf_event_ret { |
284 | LIBBPF_PERF_EVENT_DONE = 0, | 295 | LIBBPF_PERF_EVENT_DONE = 0, |
@@ -288,10 +299,11 @@ enum bpf_perf_event_ret { | |||
288 | 299 | ||
289 | typedef enum bpf_perf_event_ret (*bpf_perf_event_print_t)(void *event, | 300 | typedef enum bpf_perf_event_ret (*bpf_perf_event_print_t)(void *event, |
290 | void *priv); | 301 | void *priv); |
291 | int bpf_perf_event_read_simple(void *mem, unsigned long size, | 302 | LIBBPF_API int bpf_perf_event_read_simple(void *mem, unsigned long size, |
292 | unsigned long page_size, | 303 | unsigned long page_size, |
293 | void **buf, size_t *buf_len, | 304 | void **buf, size_t *buf_len, |
294 | bpf_perf_event_print_t fn, void *priv); | 305 | bpf_perf_event_print_t fn, |
306 | void *priv); | ||
295 | 307 | ||
296 | struct nlattr; | 308 | struct nlattr; |
297 | typedef int (*libbpf_dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb); | 309 | typedef int (*libbpf_dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb); |