1 /* eBPF mini library */
4 #include <linux/unistd.h>
7 #include <linux/netlink.h>
12 static __u64 ptr_to_u64(void *ptr)
14 return (__u64) (unsigned long) ptr;
17 int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size,
20 union bpf_attr attr = {
23 .value_size = value_size,
24 .max_entries = max_entries
27 return syscall(__NR_bpf, BPF_MAP_CREATE, &attr, sizeof(attr));
30 int bpf_update_elem(int fd, void *key, void *value)
32 union bpf_attr attr = {
34 .key = ptr_to_u64(key),
35 .value = ptr_to_u64(value),
38 return syscall(__NR_bpf, BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
41 int bpf_lookup_elem(int fd, void *key, void *value)
43 union bpf_attr attr = {
45 .key = ptr_to_u64(key),
46 .value = ptr_to_u64(value),
49 return syscall(__NR_bpf, BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
52 int bpf_delete_elem(int fd, void *key)
54 union bpf_attr attr = {
56 .key = ptr_to_u64(key),
59 return syscall(__NR_bpf, BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
62 int bpf_get_next_key(int fd, void *key, void *next_key)
64 union bpf_attr attr = {
66 .key = ptr_to_u64(key),
67 .next_key = ptr_to_u64(next_key),
70 return syscall(__NR_bpf, BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
73 #define ROUND_UP(x, n) (((x) + (n) - 1u) & ~((n) - 1u))
75 char bpf_log_buf[LOG_BUF_SIZE];
77 int bpf_prog_load(enum bpf_prog_type prog_type,
78 const struct bpf_insn *insns, int prog_len,
81 union bpf_attr attr = {
82 .prog_type = prog_type,
83 .insns = ptr_to_u64((void *) insns),
84 .insn_cnt = prog_len / sizeof(struct bpf_insn),
85 .license = ptr_to_u64((void *) license),
86 .log_buf = ptr_to_u64(bpf_log_buf),
87 .log_size = LOG_BUF_SIZE,
93 return syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));