]>
Commit | Line | Data |
---|---|---|
5aa5bd14 DB |
1 | #ifndef __BPF_SYS__ |
2 | #define __BPF_SYS__ | |
3 | ||
4 | #include <stdint.h> | |
5 | #include <stdlib.h> | |
6 | ||
7 | #include <sys/syscall.h> | |
8 | ||
9 | #include <linux/bpf.h> | |
10 | ||
11 | static inline __u64 bpf_ptr_to_u64(const void *ptr) | |
12 | { | |
13 | return (__u64)(unsigned long) ptr; | |
14 | } | |
15 | ||
16 | static inline int bpf(int cmd, union bpf_attr *attr, unsigned int size) | |
17 | { | |
18 | #ifdef __NR_bpf | |
19 | return syscall(__NR_bpf, cmd, attr, size); | |
20 | #else | |
21 | fprintf(stderr, "No bpf syscall, kernel headers too old?\n"); | |
22 | errno = ENOSYS; | |
23 | return -1; | |
24 | #endif | |
25 | } | |
26 | ||
5aa5bd14 DB |
27 | static inline int bpf_map_next_key(int fd, const void *key, void *next_key) |
28 | { | |
29 | union bpf_attr attr = {}; | |
30 | ||
31 | attr.map_fd = fd; | |
32 | attr.key = bpf_ptr_to_u64(key); | |
33 | attr.next_key = bpf_ptr_to_u64(next_key); | |
34 | ||
35 | return bpf(BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr)); | |
36 | } | |
37 | ||
38 | static inline int bpf_map_create(enum bpf_map_type type, uint32_t size_key, | |
39 | uint32_t size_value, uint32_t max_elem, | |
40 | uint32_t flags) | |
41 | { | |
42 | union bpf_attr attr = {}; | |
43 | ||
44 | attr.map_type = type; | |
45 | attr.key_size = size_key; | |
46 | attr.value_size = size_value; | |
47 | attr.max_entries = max_elem; | |
48 | attr.map_flags = flags; | |
49 | ||
50 | return bpf(BPF_MAP_CREATE, &attr, sizeof(attr)); | |
51 | } | |
52 | ||
5aa5bd14 | 53 | #endif /* __BPF_SYS__ */ |