diff options
author | David S. Miller <davem@davemloft.net> | 2018-10-22 00:11:46 -0400 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2018-10-22 00:11:46 -0400 |
commit | a19c59cc10a5ebc6b5a542e56bfd9f427ce01d74 (patch) | |
tree | cd04c1af4e800eef175cbc51ffb6e78040d7ee27 /tools/lib | |
parent | 92303c86b7e9b7d3895ccafb441a0354143e2a18 (diff) | |
parent | fe8ecccc10b3adc071de05ca7af728ca1a4ac9aa (diff) |
Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next
Daniel Borkmann says:
====================
pull-request: bpf-next 2018-10-21
The following pull-request contains BPF updates for your *net-next* tree.
The main changes are:
1) Implement two new kind of BPF maps, that is, queue and stack
map along with new peek, push and pop operations, from Mauricio.
2) Add support for MSG_PEEK flag when redirecting into an ingress
psock sk_msg queue, and add a new helper bpf_msg_push_data() for
insert data into the message, from John.
3) Allow for BPF programs of type BPF_PROG_TYPE_CGROUP_SKB to use
direct packet access for __skb_buff, from Song.
4) Use more lightweight barriers for walking perf ring buffer for
libbpf and perf tool as well. Also, various fixes and improvements
from verifier side, from Daniel.
5) Add per-symbol visibility for DSO in libbpf and hide by default
global symbols such as netlink related functions, from Andrey.
6) Two improvements to nfp's BPF offload to check vNIC capabilities
in case prog is shared with multiple vNICs and to protect against
mis-initializing atomic counters, from Jakub.
7) Fix for bpftool to use 4 context mode for the nfp disassembler,
also from Jakub.
8) Fix a return value comparison in test_libbpf.sh and add several
bpftool improvements in bash completion, documentation of bpf fs
restrictions and batch mode summary print, from Quentin.
9) Fix a file resource leak in BPF selftest's load_kallsyms()
helper, from Peng.
10) Fix an unused variable warning in map_lookup_and_delete_elem(),
from Alexei.
11) Fix bpf_skb_adjust_room() signature in BPF UAPI helper doc,
from Nicolas.
12) Add missing executables to .gitignore in BPF selftests, from Anders.
====================
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'tools/lib')
-rw-r--r-- | tools/lib/bpf/Makefile | 1 | ||||
-rw-r--r-- | tools/lib/bpf/bpf.c | 12 | ||||
-rw-r--r-- | tools/lib/bpf/bpf.h | 120 | ||||
-rw-r--r-- | tools/lib/bpf/btf.h | 22 | ||||
-rw-r--r-- | tools/lib/bpf/libbpf.c | 75 | ||||
-rw-r--r-- | tools/lib/bpf/libbpf.h | 191 |
6 files changed, 228 insertions, 193 deletions
diff --git a/tools/lib/bpf/Makefile b/tools/lib/bpf/Makefile index 79d84413ddf2..425b480bda75 100644 --- a/tools/lib/bpf/Makefile +++ b/tools/lib/bpf/Makefile | |||
@@ -125,6 +125,7 @@ override CFLAGS += $(EXTRA_WARNINGS) | |||
125 | override CFLAGS += -Werror -Wall | 125 | override CFLAGS += -Werror -Wall |
126 | override CFLAGS += -fPIC | 126 | override CFLAGS += -fPIC |
127 | override CFLAGS += $(INCLUDES) | 127 | override CFLAGS += $(INCLUDES) |
128 | override CFLAGS += -fvisibility=hidden | ||
128 | 129 | ||
129 | ifeq ($(VERBOSE),1) | 130 | ifeq ($(VERBOSE),1) |
130 | Q = | 131 | Q = |
diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c index d70a255cb05e..03f9bcc4ef50 100644 --- a/tools/lib/bpf/bpf.c +++ b/tools/lib/bpf/bpf.c | |||
@@ -278,6 +278,18 @@ int bpf_map_lookup_elem(int fd, const void *key, void *value) | |||
278 | return sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr)); | 278 | return sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr)); |
279 | } | 279 | } |
280 | 280 | ||
281 | int bpf_map_lookup_and_delete_elem(int fd, const void *key, void *value) | ||
282 | { | ||
283 | union bpf_attr attr; | ||
284 | |||
285 | bzero(&attr, sizeof(attr)); | ||
286 | attr.map_fd = fd; | ||
287 | attr.key = ptr_to_u64(key); | ||
288 | attr.value = ptr_to_u64(value); | ||
289 | |||
290 | return sys_bpf(BPF_MAP_LOOKUP_AND_DELETE_ELEM, &attr, sizeof(attr)); | ||
291 | } | ||
292 | |||
281 | int bpf_map_delete_elem(int fd, const void *key) | 293 | int bpf_map_delete_elem(int fd, const void *key) |
282 | { | 294 | { |
283 | union bpf_attr attr; | 295 | union bpf_attr attr; |
diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h index 69a4d40c4227..26a51538213c 100644 --- a/tools/lib/bpf/bpf.h +++ b/tools/lib/bpf/bpf.h | |||
@@ -27,6 +27,10 @@ | |||
27 | #include <stdbool.h> | 27 | #include <stdbool.h> |
28 | #include <stddef.h> | 28 | #include <stddef.h> |
29 | 29 | ||
30 | #ifndef LIBBPF_API | ||
31 | #define LIBBPF_API __attribute__((visibility("default"))) | ||
32 | #endif | ||
33 | |||
30 | struct bpf_create_map_attr { | 34 | struct bpf_create_map_attr { |
31 | const char *name; | 35 | const char *name; |
32 | enum bpf_map_type map_type; | 36 | enum bpf_map_type map_type; |
@@ -42,21 +46,24 @@ struct bpf_create_map_attr { | |||
42 | __u32 inner_map_fd; | 46 | __u32 inner_map_fd; |
43 | }; | 47 | }; |
44 | 48 | ||
45 | int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr); | 49 | LIBBPF_API int |
46 | int bpf_create_map_node(enum bpf_map_type map_type, const char *name, | 50 | bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr); |
47 | int key_size, int value_size, int max_entries, | 51 | LIBBPF_API int bpf_create_map_node(enum bpf_map_type map_type, const char *name, |
48 | __u32 map_flags, int node); | 52 | int key_size, int value_size, |
49 | int bpf_create_map_name(enum bpf_map_type map_type, const char *name, | 53 | int max_entries, __u32 map_flags, int node); |
50 | int key_size, int value_size, int max_entries, | 54 | LIBBPF_API int bpf_create_map_name(enum bpf_map_type map_type, const char *name, |
51 | __u32 map_flags); | 55 | int key_size, int value_size, |
52 | int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size, | 56 | int max_entries, __u32 map_flags); |
53 | int max_entries, __u32 map_flags); | 57 | LIBBPF_API int bpf_create_map(enum bpf_map_type map_type, int key_size, |
54 | int bpf_create_map_in_map_node(enum bpf_map_type map_type, const char *name, | 58 | int value_size, int max_entries, __u32 map_flags); |
55 | int key_size, int inner_map_fd, int max_entries, | 59 | LIBBPF_API int bpf_create_map_in_map_node(enum bpf_map_type map_type, |
56 | __u32 map_flags, int node); | 60 | const char *name, int key_size, |
57 | int bpf_create_map_in_map(enum bpf_map_type map_type, const char *name, | 61 | int inner_map_fd, int max_entries, |
58 | int key_size, int inner_map_fd, int max_entries, | 62 | __u32 map_flags, int node); |
59 | __u32 map_flags); | 63 | LIBBPF_API int bpf_create_map_in_map(enum bpf_map_type map_type, |
64 | const char *name, int key_size, | ||
65 | int inner_map_fd, int max_entries, | ||
66 | __u32 map_flags); | ||
60 | 67 | ||
61 | struct bpf_load_program_attr { | 68 | struct bpf_load_program_attr { |
62 | enum bpf_prog_type prog_type; | 69 | enum bpf_prog_type prog_type; |
@@ -74,44 +81,51 @@ struct bpf_load_program_attr { | |||
74 | 81 | ||
75 | /* Recommend log buffer size */ | 82 | /* Recommend log buffer size */ |
76 | #define BPF_LOG_BUF_SIZE (256 * 1024) | 83 | #define BPF_LOG_BUF_SIZE (256 * 1024) |
77 | int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr, | 84 | LIBBPF_API int |
78 | char *log_buf, size_t log_buf_sz); | 85 | bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr, |
79 | int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns, | 86 | char *log_buf, size_t log_buf_sz); |
80 | size_t insns_cnt, const char *license, | 87 | LIBBPF_API int bpf_load_program(enum bpf_prog_type type, |
81 | __u32 kern_version, char *log_buf, | 88 | const struct bpf_insn *insns, size_t insns_cnt, |
82 | size_t log_buf_sz); | 89 | const char *license, __u32 kern_version, |
83 | int bpf_verify_program(enum bpf_prog_type type, const struct bpf_insn *insns, | 90 | char *log_buf, size_t log_buf_sz); |
84 | size_t insns_cnt, int strict_alignment, | 91 | LIBBPF_API int bpf_verify_program(enum bpf_prog_type type, |
85 | const char *license, __u32 kern_version, | 92 | const struct bpf_insn *insns, |
86 | char *log_buf, size_t log_buf_sz, int log_level); | 93 | size_t insns_cnt, int strict_alignment, |
94 | const char *license, __u32 kern_version, | ||
95 | char *log_buf, size_t log_buf_sz, | ||
96 | int log_level); | ||
87 | 97 | ||
88 | int bpf_map_update_elem(int fd, const void *key, const void *value, | 98 | LIBBPF_API int bpf_map_update_elem(int fd, const void *key, const void *value, |
89 | __u64 flags); | 99 | __u64 flags); |
90 | 100 | ||
91 | int bpf_map_lookup_elem(int fd, const void *key, void *value); | 101 | LIBBPF_API int bpf_map_lookup_elem(int fd, const void *key, void *value); |
92 | int bpf_map_delete_elem(int fd, const void *key); | 102 | LIBBPF_API int bpf_map_lookup_and_delete_elem(int fd, const void *key, |
93 | int bpf_map_get_next_key(int fd, const void *key, void *next_key); | 103 | void *value); |
94 | int bpf_obj_pin(int fd, const char *pathname); | 104 | LIBBPF_API int bpf_map_delete_elem(int fd, const void *key); |
95 | int bpf_obj_get(const char *pathname); | 105 | LIBBPF_API int bpf_map_get_next_key(int fd, const void *key, void *next_key); |
96 | int bpf_prog_attach(int prog_fd, int attachable_fd, enum bpf_attach_type type, | 106 | LIBBPF_API int bpf_obj_pin(int fd, const char *pathname); |
97 | unsigned int flags); | 107 | LIBBPF_API int bpf_obj_get(const char *pathname); |
98 | int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type); | 108 | LIBBPF_API int bpf_prog_attach(int prog_fd, int attachable_fd, |
99 | int bpf_prog_detach2(int prog_fd, int attachable_fd, enum bpf_attach_type type); | 109 | enum bpf_attach_type type, unsigned int flags); |
100 | int bpf_prog_test_run(int prog_fd, int repeat, void *data, __u32 size, | 110 | LIBBPF_API int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type); |
101 | void *data_out, __u32 *size_out, __u32 *retval, | 111 | LIBBPF_API int bpf_prog_detach2(int prog_fd, int attachable_fd, |
102 | __u32 *duration); | 112 | enum bpf_attach_type type); |
103 | int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id); | 113 | LIBBPF_API int bpf_prog_test_run(int prog_fd, int repeat, void *data, |
104 | int bpf_map_get_next_id(__u32 start_id, __u32 *next_id); | 114 | __u32 size, void *data_out, __u32 *size_out, |
105 | int bpf_prog_get_fd_by_id(__u32 id); | 115 | __u32 *retval, __u32 *duration); |
106 | int bpf_map_get_fd_by_id(__u32 id); | 116 | LIBBPF_API int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id); |
107 | int bpf_btf_get_fd_by_id(__u32 id); | 117 | LIBBPF_API int bpf_map_get_next_id(__u32 start_id, __u32 *next_id); |
108 | int bpf_obj_get_info_by_fd(int prog_fd, void *info, __u32 *info_len); | 118 | LIBBPF_API int bpf_prog_get_fd_by_id(__u32 id); |
109 | int bpf_prog_query(int target_fd, enum bpf_attach_type type, __u32 query_flags, | 119 | LIBBPF_API int bpf_map_get_fd_by_id(__u32 id); |
110 | __u32 *attach_flags, __u32 *prog_ids, __u32 *prog_cnt); | 120 | LIBBPF_API int bpf_btf_get_fd_by_id(__u32 id); |
111 | int bpf_raw_tracepoint_open(const char *name, int prog_fd); | 121 | LIBBPF_API int bpf_obj_get_info_by_fd(int prog_fd, void *info, __u32 *info_len); |
112 | int bpf_load_btf(void *btf, __u32 btf_size, char *log_buf, __u32 log_buf_size, | 122 | LIBBPF_API int bpf_prog_query(int target_fd, enum bpf_attach_type type, |
113 | bool do_log); | 123 | __u32 query_flags, __u32 *attach_flags, |
114 | int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, __u32 *buf_len, | 124 | __u32 *prog_ids, __u32 *prog_cnt); |
115 | __u32 *prog_id, __u32 *fd_type, __u64 *probe_offset, | 125 | LIBBPF_API int bpf_raw_tracepoint_open(const char *name, int prog_fd); |
116 | __u64 *probe_addr); | 126 | LIBBPF_API int bpf_load_btf(void *btf, __u32 btf_size, char *log_buf, |
127 | __u32 log_buf_size, bool do_log); | ||
128 | LIBBPF_API int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, | ||
129 | __u32 *buf_len, __u32 *prog_id, __u32 *fd_type, | ||
130 | __u64 *probe_offset, __u64 *probe_addr); | ||
117 | #endif /* __LIBBPF_BPF_H */ | 131 | #endif /* __LIBBPF_BPF_H */ |
diff --git a/tools/lib/bpf/btf.h b/tools/lib/bpf/btf.h index 6db5462bb2ef..b77e7080f7e7 100644 --- a/tools/lib/bpf/btf.h +++ b/tools/lib/bpf/btf.h | |||
@@ -6,6 +6,10 @@ | |||
6 | 6 | ||
7 | #include <linux/types.h> | 7 | #include <linux/types.h> |
8 | 8 | ||
9 | #ifndef LIBBPF_API | ||
10 | #define LIBBPF_API __attribute__((visibility("default"))) | ||
11 | #endif | ||
12 | |||
9 | #define BTF_ELF_SEC ".BTF" | 13 | #define BTF_ELF_SEC ".BTF" |
10 | 14 | ||
11 | struct btf; | 15 | struct btf; |
@@ -14,13 +18,15 @@ struct btf_type; | |||
14 | typedef int (*btf_print_fn_t)(const char *, ...) | 18 | typedef int (*btf_print_fn_t)(const char *, ...) |
15 | __attribute__((format(printf, 1, 2))); | 19 | __attribute__((format(printf, 1, 2))); |
16 | 20 | ||
17 | void btf__free(struct btf *btf); | 21 | LIBBPF_API void btf__free(struct btf *btf); |
18 | struct btf *btf__new(__u8 *data, __u32 size, btf_print_fn_t err_log); | 22 | LIBBPF_API struct btf *btf__new(__u8 *data, __u32 size, btf_print_fn_t err_log); |
19 | __s32 btf__find_by_name(const struct btf *btf, const char *type_name); | 23 | LIBBPF_API __s32 btf__find_by_name(const struct btf *btf, |
20 | const struct btf_type *btf__type_by_id(const struct btf *btf, __u32 id); | 24 | const char *type_name); |
21 | __s64 btf__resolve_size(const struct btf *btf, __u32 type_id); | 25 | LIBBPF_API const struct btf_type *btf__type_by_id(const struct btf *btf, |
22 | int btf__resolve_type(const struct btf *btf, __u32 type_id); | 26 | __u32 id); |
23 | int btf__fd(const struct btf *btf); | 27 | LIBBPF_API __s64 btf__resolve_size(const struct btf *btf, __u32 type_id); |
24 | const char *btf__name_by_offset(const struct btf *btf, __u32 offset); | 28 | LIBBPF_API int btf__resolve_type(const struct btf *btf, __u32 type_id); |
29 | LIBBPF_API int btf__fd(const struct btf *btf); | ||
30 | LIBBPF_API const char *btf__name_by_offset(const struct btf *btf, __u32 offset); | ||
25 | 31 | ||
26 | #endif /* __LIBBPF_BTF_H */ | 32 | #endif /* __LIBBPF_BTF_H */ |
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index bd71efcc53be..b607be7236d3 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c | |||
@@ -27,6 +27,7 @@ | |||
27 | #include <linux/list.h> | 27 | #include <linux/list.h> |
28 | #include <linux/limits.h> | 28 | #include <linux/limits.h> |
29 | #include <linux/perf_event.h> | 29 | #include <linux/perf_event.h> |
30 | #include <linux/ring_buffer.h> | ||
30 | #include <sys/stat.h> | 31 | #include <sys/stat.h> |
31 | #include <sys/types.h> | 32 | #include <sys/types.h> |
32 | #include <sys/vfs.h> | 33 | #include <sys/vfs.h> |
@@ -2414,61 +2415,49 @@ int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr, | |||
2414 | } | 2415 | } |
2415 | 2416 | ||
2416 | enum bpf_perf_event_ret | 2417 | enum bpf_perf_event_ret |
2417 | bpf_perf_event_read_simple(void *mem, unsigned long size, | 2418 | bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size, |
2418 | unsigned long page_size, void **buf, size_t *buf_len, | 2419 | void **copy_mem, size_t *copy_size, |
2419 | bpf_perf_event_print_t fn, void *priv) | 2420 | bpf_perf_event_print_t fn, void *private_data) |
2420 | { | 2421 | { |
2421 | volatile struct perf_event_mmap_page *header = mem; | 2422 | struct perf_event_mmap_page *header = mmap_mem; |
2423 | __u64 data_head = ring_buffer_read_head(header); | ||
2422 | __u64 data_tail = header->data_tail; | 2424 | __u64 data_tail = header->data_tail; |
2423 | __u64 data_head = header->data_head; | 2425 | void *base = ((__u8 *)header) + page_size; |
2424 | int ret = LIBBPF_PERF_EVENT_ERROR; | 2426 | int ret = LIBBPF_PERF_EVENT_CONT; |
2425 | void *base, *begin, *end; | 2427 | struct perf_event_header *ehdr; |
2426 | 2428 | size_t ehdr_size; | |
2427 | asm volatile("" ::: "memory"); /* in real code it should be smp_rmb() */ | 2429 | |
2428 | if (data_head == data_tail) | 2430 | while (data_head != data_tail) { |
2429 | return LIBBPF_PERF_EVENT_CONT; | 2431 | ehdr = base + (data_tail & (mmap_size - 1)); |
2430 | 2432 | ehdr_size = ehdr->size; | |
2431 | base = ((char *)header) + page_size; | 2433 | |
2432 | 2434 | if (((void *)ehdr) + ehdr_size > base + mmap_size) { | |
2433 | begin = base + data_tail % size; | 2435 | void *copy_start = ehdr; |
2434 | end = base + data_head % size; | 2436 | size_t len_first = base + mmap_size - copy_start; |
2435 | 2437 | size_t len_secnd = ehdr_size - len_first; | |
2436 | while (begin != end) { | 2438 | |
2437 | struct perf_event_header *ehdr; | 2439 | if (*copy_size < ehdr_size) { |
2438 | 2440 | free(*copy_mem); | |
2439 | ehdr = begin; | 2441 | *copy_mem = malloc(ehdr_size); |
2440 | if (begin + ehdr->size > base + size) { | 2442 | if (!*copy_mem) { |
2441 | long len = base + size - begin; | 2443 | *copy_size = 0; |
2442 | |||
2443 | if (*buf_len < ehdr->size) { | ||
2444 | free(*buf); | ||
2445 | *buf = malloc(ehdr->size); | ||
2446 | if (!*buf) { | ||
2447 | ret = LIBBPF_PERF_EVENT_ERROR; | 2444 | ret = LIBBPF_PERF_EVENT_ERROR; |
2448 | break; | 2445 | break; |
2449 | } | 2446 | } |
2450 | *buf_len = ehdr->size; | 2447 | *copy_size = ehdr_size; |
2451 | } | 2448 | } |
2452 | 2449 | ||
2453 | memcpy(*buf, begin, len); | 2450 | memcpy(*copy_mem, copy_start, len_first); |
2454 | memcpy(*buf + len, base, ehdr->size - len); | 2451 | memcpy(*copy_mem + len_first, base, len_secnd); |
2455 | ehdr = (void *)*buf; | 2452 | ehdr = *copy_mem; |
2456 | begin = base + ehdr->size - len; | ||
2457 | } else if (begin + ehdr->size == base + size) { | ||
2458 | begin = base; | ||
2459 | } else { | ||
2460 | begin += ehdr->size; | ||
2461 | } | 2453 | } |
2462 | 2454 | ||
2463 | ret = fn(ehdr, priv); | 2455 | ret = fn(ehdr, private_data); |
2456 | data_tail += ehdr_size; | ||
2464 | if (ret != LIBBPF_PERF_EVENT_CONT) | 2457 | if (ret != LIBBPF_PERF_EVENT_CONT) |
2465 | break; | 2458 | break; |
2466 | |||
2467 | data_tail += ehdr->size; | ||
2468 | } | 2459 | } |
2469 | 2460 | ||
2470 | __sync_synchronize(); /* smp_mb() */ | 2461 | ring_buffer_write_tail(header, data_tail); |
2471 | header->data_tail = data_tail; | ||
2472 | |||
2473 | return ret; | 2462 | return ret; |
2474 | } | 2463 | } |
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index 7e9c801a9fdd..1f3468dad8b2 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, |
@@ -286,12 +297,14 @@ enum bpf_perf_event_ret { | |||
286 | LIBBPF_PERF_EVENT_CONT = -2, | 297 | LIBBPF_PERF_EVENT_CONT = -2, |
287 | }; | 298 | }; |
288 | 299 | ||
289 | typedef enum bpf_perf_event_ret (*bpf_perf_event_print_t)(void *event, | 300 | struct perf_event_header; |
290 | void *priv); | 301 | typedef enum bpf_perf_event_ret |
291 | int bpf_perf_event_read_simple(void *mem, unsigned long size, | 302 | (*bpf_perf_event_print_t)(struct perf_event_header *hdr, |
292 | unsigned long page_size, | 303 | void *private_data); |
293 | void **buf, size_t *buf_len, | 304 | LIBBPF_API enum bpf_perf_event_ret |
294 | bpf_perf_event_print_t fn, void *priv); | 305 | bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size, |
306 | void **copy_mem, size_t *copy_size, | ||
307 | bpf_perf_event_print_t fn, void *private_data); | ||
295 | 308 | ||
296 | struct nlattr; | 309 | struct nlattr; |
297 | typedef int (*libbpf_dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb); | 310 | typedef int (*libbpf_dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb); |