aboutsummaryrefslogtreecommitdiffstats
path: root/tools/lib
diff options
context:
space:
mode:
authorDavid S. Miller <davem@davemloft.net>2018-10-22 00:11:46 -0400
committerDavid S. Miller <davem@davemloft.net>2018-10-22 00:11:46 -0400
commita19c59cc10a5ebc6b5a542e56bfd9f427ce01d74 (patch)
treecd04c1af4e800eef175cbc51ffb6e78040d7ee27 /tools/lib
parent92303c86b7e9b7d3895ccafb441a0354143e2a18 (diff)
parentfe8ecccc10b3adc071de05ca7af728ca1a4ac9aa (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/Makefile1
-rw-r--r--tools/lib/bpf/bpf.c12
-rw-r--r--tools/lib/bpf/bpf.h120
-rw-r--r--tools/lib/bpf/btf.h22
-rw-r--r--tools/lib/bpf/libbpf.c75
-rw-r--r--tools/lib/bpf/libbpf.h191
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)
125override CFLAGS += -Werror -Wall 125override CFLAGS += -Werror -Wall
126override CFLAGS += -fPIC 126override CFLAGS += -fPIC
127override CFLAGS += $(INCLUDES) 127override CFLAGS += $(INCLUDES)
128override CFLAGS += -fvisibility=hidden
128 129
129ifeq ($(VERBOSE),1) 130ifeq ($(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
281int 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
281int bpf_map_delete_elem(int fd, const void *key) 293int 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
30struct bpf_create_map_attr { 34struct 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
45int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr); 49LIBBPF_API int
46int bpf_create_map_node(enum bpf_map_type map_type, const char *name, 50bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr);
47 int key_size, int value_size, int max_entries, 51LIBBPF_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,
49int 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, 54LIBBPF_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,
52int 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); 57LIBBPF_API int bpf_create_map(enum bpf_map_type map_type, int key_size,
54int 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, 59LIBBPF_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,
57int 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); 63LIBBPF_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
61struct bpf_load_program_attr { 68struct 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)
77int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr, 84LIBBPF_API int
78 char *log_buf, size_t log_buf_sz); 85bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
79int 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, 87LIBBPF_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,
83int 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, 91LIBBPF_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
88int bpf_map_update_elem(int fd, const void *key, const void *value, 98LIBBPF_API int bpf_map_update_elem(int fd, const void *key, const void *value,
89 __u64 flags); 99 __u64 flags);
90 100
91int bpf_map_lookup_elem(int fd, const void *key, void *value); 101LIBBPF_API int bpf_map_lookup_elem(int fd, const void *key, void *value);
92int bpf_map_delete_elem(int fd, const void *key); 102LIBBPF_API int bpf_map_lookup_and_delete_elem(int fd, const void *key,
93int bpf_map_get_next_key(int fd, const void *key, void *next_key); 103 void *value);
94int bpf_obj_pin(int fd, const char *pathname); 104LIBBPF_API int bpf_map_delete_elem(int fd, const void *key);
95int bpf_obj_get(const char *pathname); 105LIBBPF_API int bpf_map_get_next_key(int fd, const void *key, void *next_key);
96int bpf_prog_attach(int prog_fd, int attachable_fd, enum bpf_attach_type type, 106LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
97 unsigned int flags); 107LIBBPF_API int bpf_obj_get(const char *pathname);
98int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type); 108LIBBPF_API int bpf_prog_attach(int prog_fd, int attachable_fd,
99int bpf_prog_detach2(int prog_fd, int attachable_fd, enum bpf_attach_type type); 109 enum bpf_attach_type type, unsigned int flags);
100int bpf_prog_test_run(int prog_fd, int repeat, void *data, __u32 size, 110LIBBPF_API int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);
101 void *data_out, __u32 *size_out, __u32 *retval, 111LIBBPF_API int bpf_prog_detach2(int prog_fd, int attachable_fd,
102 __u32 *duration); 112 enum bpf_attach_type type);
103int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id); 113LIBBPF_API int bpf_prog_test_run(int prog_fd, int repeat, void *data,
104int bpf_map_get_next_id(__u32 start_id, __u32 *next_id); 114 __u32 size, void *data_out, __u32 *size_out,
105int bpf_prog_get_fd_by_id(__u32 id); 115 __u32 *retval, __u32 *duration);
106int bpf_map_get_fd_by_id(__u32 id); 116LIBBPF_API int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id);
107int bpf_btf_get_fd_by_id(__u32 id); 117LIBBPF_API int bpf_map_get_next_id(__u32 start_id, __u32 *next_id);
108int bpf_obj_get_info_by_fd(int prog_fd, void *info, __u32 *info_len); 118LIBBPF_API int bpf_prog_get_fd_by_id(__u32 id);
109int bpf_prog_query(int target_fd, enum bpf_attach_type type, __u32 query_flags, 119LIBBPF_API int bpf_map_get_fd_by_id(__u32 id);
110 __u32 *attach_flags, __u32 *prog_ids, __u32 *prog_cnt); 120LIBBPF_API int bpf_btf_get_fd_by_id(__u32 id);
111int bpf_raw_tracepoint_open(const char *name, int prog_fd); 121LIBBPF_API int bpf_obj_get_info_by_fd(int prog_fd, void *info, __u32 *info_len);
112int bpf_load_btf(void *btf, __u32 btf_size, char *log_buf, __u32 log_buf_size, 122LIBBPF_API int bpf_prog_query(int target_fd, enum bpf_attach_type type,
113 bool do_log); 123 __u32 query_flags, __u32 *attach_flags,
114int 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, 125LIBBPF_API int bpf_raw_tracepoint_open(const char *name, int prog_fd);
116 __u64 *probe_addr); 126LIBBPF_API int bpf_load_btf(void *btf, __u32 btf_size, char *log_buf,
127 __u32 log_buf_size, bool do_log);
128LIBBPF_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
11struct btf; 15struct btf;
@@ -14,13 +18,15 @@ struct btf_type;
14typedef int (*btf_print_fn_t)(const char *, ...) 18typedef int (*btf_print_fn_t)(const char *, ...)
15 __attribute__((format(printf, 1, 2))); 19 __attribute__((format(printf, 1, 2)));
16 20
17void btf__free(struct btf *btf); 21LIBBPF_API void btf__free(struct btf *btf);
18struct btf *btf__new(__u8 *data, __u32 size, btf_print_fn_t err_log); 22LIBBPF_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); 23LIBBPF_API __s32 btf__find_by_name(const struct btf *btf,
20const 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); 25LIBBPF_API const struct btf_type *btf__type_by_id(const struct btf *btf,
22int btf__resolve_type(const struct btf *btf, __u32 type_id); 26 __u32 id);
23int btf__fd(const struct btf *btf); 27LIBBPF_API __s64 btf__resolve_size(const struct btf *btf, __u32 type_id);
24const char *btf__name_by_offset(const struct btf *btf, __u32 offset); 28LIBBPF_API int btf__resolve_type(const struct btf *btf, __u32 type_id);
29LIBBPF_API int btf__fd(const struct btf *btf);
30LIBBPF_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
2416enum bpf_perf_event_ret 2417enum bpf_perf_event_ret
2417bpf_perf_event_read_simple(void *mem, unsigned long size, 2418bpf_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
19enum libbpf_errno { 23enum 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
40int libbpf_strerror(int err, char *buf, size_t size); 44LIBBPF_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);
47typedef int (*libbpf_print_fn_t)(const char *, ...) 51typedef int (*libbpf_print_fn_t)(const char *, ...)
48 __attribute__((format(printf, 1, 2))); 52 __attribute__((format(printf, 1, 2)));
49 53
50void libbpf_set_print(libbpf_print_fn_t warn, 54LIBBPF_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 */
55struct bpf_object; 59struct 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
62struct bpf_object *bpf_object__open(const char *path); 66LIBBPF_API struct bpf_object *bpf_object__open(const char *path);
63struct bpf_object *bpf_object__open_xattr(struct bpf_object_open_attr *attr); 67LIBBPF_API struct bpf_object *
68bpf_object__open_xattr(struct bpf_object_open_attr *attr);
64struct bpf_object *__bpf_object__open_xattr(struct bpf_object_open_attr *attr, 69struct bpf_object *__bpf_object__open_xattr(struct bpf_object_open_attr *attr,
65 int flags); 70 int flags);
66struct bpf_object *bpf_object__open_buffer(void *obj_buf, 71LIBBPF_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);
69int bpf_object__pin(struct bpf_object *object, const char *path); 74LIBBPF_API int bpf_object__pin(struct bpf_object *object, const char *path);
70void bpf_object__close(struct bpf_object *object); 75LIBBPF_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 */
73int bpf_object__load(struct bpf_object *obj); 78LIBBPF_API int bpf_object__load(struct bpf_object *obj);
74int bpf_object__unload(struct bpf_object *obj); 79LIBBPF_API int bpf_object__unload(struct bpf_object *obj);
75const char *bpf_object__name(struct bpf_object *obj); 80LIBBPF_API const char *bpf_object__name(struct bpf_object *obj);
76unsigned int bpf_object__kversion(struct bpf_object *obj); 81LIBBPF_API unsigned int bpf_object__kversion(struct bpf_object *obj);
77int bpf_object__btf_fd(const struct bpf_object *obj); 82LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj);
78 83
79struct bpf_program * 84LIBBPF_API struct bpf_program *
80bpf_object__find_program_by_title(struct bpf_object *obj, const char *title); 85bpf_object__find_program_by_title(struct bpf_object *obj, const char *title);
81 86
82struct bpf_object *bpf_object__next(struct bpf_object *prev); 87LIBBPF_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
89typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *); 94typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *);
90int bpf_object__set_priv(struct bpf_object *obj, void *priv, 95LIBBPF_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);
92void *bpf_object__priv(struct bpf_object *prog); 97LIBBPF_API void *bpf_object__priv(struct bpf_object *prog);
93 98
94int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type, 99LIBBPF_API int
95 enum bpf_attach_type *expected_attach_type); 100libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
96int libbpf_attach_type_by_name(const char *name, 101 enum bpf_attach_type *expected_attach_type);
97 enum bpf_attach_type *attach_type); 102LIBBPF_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 */
100struct bpf_program; 106struct bpf_program;
101struct bpf_program *bpf_program__next(struct bpf_program *prog, 107LIBBPF_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,
109typedef void (*bpf_program_clear_priv_t)(struct bpf_program *, 115typedef void (*bpf_program_clear_priv_t)(struct bpf_program *,
110 void *); 116 void *);
111 117
112int bpf_program__set_priv(struct bpf_program *prog, void *priv, 118LIBBPF_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
115void *bpf_program__priv(struct bpf_program *prog); 121LIBBPF_API void *bpf_program__priv(struct bpf_program *prog);
116void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex); 122LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,
123 __u32 ifindex);
117 124
118const char *bpf_program__title(struct bpf_program *prog, bool needs_copy); 125LIBBPF_API const char *bpf_program__title(struct bpf_program *prog,
126 bool needs_copy);
119 127
120int bpf_program__load(struct bpf_program *prog, char *license, 128LIBBPF_API int bpf_program__load(struct bpf_program *prog, char *license,
121 __u32 kern_version); 129 __u32 kern_version);
122int bpf_program__fd(struct bpf_program *prog); 130LIBBPF_API int bpf_program__fd(struct bpf_program *prog);
123int bpf_program__pin_instance(struct bpf_program *prog, const char *path, 131LIBBPF_API int bpf_program__pin_instance(struct bpf_program *prog,
124 int instance); 132 const char *path,
125int bpf_program__pin(struct bpf_program *prog, const char *path); 133 int instance);
126void bpf_program__unload(struct bpf_program *prog); 134LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path);
135LIBBPF_API void bpf_program__unload(struct bpf_program *prog);
127 136
128struct bpf_insn; 137struct 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
187int bpf_program__set_prep(struct bpf_program *prog, int nr_instance, 196LIBBPF_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
190int bpf_program__nth_fd(struct bpf_program *prog, int n); 199LIBBPF_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 */
195int bpf_program__set_socket_filter(struct bpf_program *prog); 204LIBBPF_API int bpf_program__set_socket_filter(struct bpf_program *prog);
196int bpf_program__set_tracepoint(struct bpf_program *prog); 205LIBBPF_API int bpf_program__set_tracepoint(struct bpf_program *prog);
197int bpf_program__set_raw_tracepoint(struct bpf_program *prog); 206LIBBPF_API int bpf_program__set_raw_tracepoint(struct bpf_program *prog);
198int bpf_program__set_kprobe(struct bpf_program *prog); 207LIBBPF_API int bpf_program__set_kprobe(struct bpf_program *prog);
199int bpf_program__set_sched_cls(struct bpf_program *prog); 208LIBBPF_API int bpf_program__set_sched_cls(struct bpf_program *prog);
200int bpf_program__set_sched_act(struct bpf_program *prog); 209LIBBPF_API int bpf_program__set_sched_act(struct bpf_program *prog);
201int bpf_program__set_xdp(struct bpf_program *prog); 210LIBBPF_API int bpf_program__set_xdp(struct bpf_program *prog);
202int bpf_program__set_perf_event(struct bpf_program *prog); 211LIBBPF_API int bpf_program__set_perf_event(struct bpf_program *prog);
203void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type); 212LIBBPF_API void bpf_program__set_type(struct bpf_program *prog,
204void bpf_program__set_expected_attach_type(struct bpf_program *prog, 213 enum bpf_prog_type type);
205 enum bpf_attach_type type); 214LIBBPF_API void
206 215bpf_program__set_expected_attach_type(struct bpf_program *prog,
207bool bpf_program__is_socket_filter(struct bpf_program *prog); 216 enum bpf_attach_type type);
208bool bpf_program__is_tracepoint(struct bpf_program *prog); 217
209bool bpf_program__is_raw_tracepoint(struct bpf_program *prog); 218LIBBPF_API bool bpf_program__is_socket_filter(struct bpf_program *prog);
210bool bpf_program__is_kprobe(struct bpf_program *prog); 219LIBBPF_API bool bpf_program__is_tracepoint(struct bpf_program *prog);
211bool bpf_program__is_sched_cls(struct bpf_program *prog); 220LIBBPF_API bool bpf_program__is_raw_tracepoint(struct bpf_program *prog);
212bool bpf_program__is_sched_act(struct bpf_program *prog); 221LIBBPF_API bool bpf_program__is_kprobe(struct bpf_program *prog);
213bool bpf_program__is_xdp(struct bpf_program *prog); 222LIBBPF_API bool bpf_program__is_sched_cls(struct bpf_program *prog);
214bool bpf_program__is_perf_event(struct bpf_program *prog); 223LIBBPF_API bool bpf_program__is_sched_act(struct bpf_program *prog);
224LIBBPF_API bool bpf_program__is_xdp(struct bpf_program *prog);
225LIBBPF_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 */
234struct bpf_map; 245struct bpf_map;
235struct bpf_map * 246LIBBPF_API struct bpf_map *
236bpf_object__find_map_by_name(struct bpf_object *obj, const char *name); 247bpf_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 */
242struct bpf_map * 253LIBBPF_API struct bpf_map *
243bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset); 254bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset);
244 255
245struct bpf_map * 256LIBBPF_API struct bpf_map *
246bpf_map__next(struct bpf_map *map, struct bpf_object *obj); 257bpf_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
252int bpf_map__fd(struct bpf_map *map); 263LIBBPF_API int bpf_map__fd(struct bpf_map *map);
253const struct bpf_map_def *bpf_map__def(struct bpf_map *map); 264LIBBPF_API const struct bpf_map_def *bpf_map__def(struct bpf_map *map);
254const char *bpf_map__name(struct bpf_map *map); 265LIBBPF_API const char *bpf_map__name(struct bpf_map *map);
255__u32 bpf_map__btf_key_type_id(const struct bpf_map *map); 266LIBBPF_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); 267LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);
257 268
258typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *); 269typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
259int bpf_map__set_priv(struct bpf_map *map, void *priv, 270LIBBPF_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);
261void *bpf_map__priv(struct bpf_map *map); 272LIBBPF_API void *bpf_map__priv(struct bpf_map *map);
262int bpf_map__reuse_fd(struct bpf_map *map, int fd); 273LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
263bool bpf_map__is_offload_neutral(struct bpf_map *map); 274LIBBPF_API bool bpf_map__is_offload_neutral(struct bpf_map *map);
264void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex); 275LIBBPF_API void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
265int bpf_map__pin(struct bpf_map *map, const char *path); 276LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);
266 277
267long libbpf_get_error(const void *ptr); 278LIBBPF_API long libbpf_get_error(const void *ptr);
268 279
269struct bpf_prog_load_attr { 280struct 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
276int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr, 287LIBBPF_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);
278int bpf_prog_load(const char *file, enum bpf_prog_type type, 289LIBBPF_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
281int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags); 292LIBBPF_API int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags);
282 293
283enum bpf_perf_event_ret { 294enum 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
289typedef enum bpf_perf_event_ret (*bpf_perf_event_print_t)(void *event, 300struct perf_event_header;
290 void *priv); 301typedef enum bpf_perf_event_ret
291int 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, 304LIBBPF_API enum bpf_perf_event_ret
294 bpf_perf_event_print_t fn, void *priv); 305bpf_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
296struct nlattr; 309struct nlattr;
297typedef int (*libbpf_dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb); 310typedef int (*libbpf_dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb);