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
, unsigned long long flags
)
32 union bpf_attr attr
= {
34 .key
= ptr_to_u64(key
),
35 .value
= ptr_to_u64(value
),
39 return syscall(__NR_bpf
, BPF_MAP_UPDATE_ELEM
, &attr
, sizeof(attr
));
42 int bpf_lookup_elem(int fd
, void *key
, void *value
)
44 union bpf_attr attr
= {
46 .key
= ptr_to_u64(key
),
47 .value
= ptr_to_u64(value
),
50 return syscall(__NR_bpf
, BPF_MAP_LOOKUP_ELEM
, &attr
, sizeof(attr
));
53 int bpf_delete_elem(int fd
, void *key
)
55 union bpf_attr attr
= {
57 .key
= ptr_to_u64(key
),
60 return syscall(__NR_bpf
, BPF_MAP_DELETE_ELEM
, &attr
, sizeof(attr
));
63 int bpf_get_next_key(int fd
, void *key
, void *next_key
)
65 union bpf_attr attr
= {
67 .key
= ptr_to_u64(key
),
68 .next_key
= ptr_to_u64(next_key
),
71 return syscall(__NR_bpf
, BPF_MAP_GET_NEXT_KEY
, &attr
, sizeof(attr
));
74 #define ROUND_UP(x, n) (((x) + (n) - 1u) & ~((n) - 1u))
76 char bpf_log_buf
[LOG_BUF_SIZE
];
78 int bpf_prog_load(enum bpf_prog_type prog_type
,
79 const struct bpf_insn
*insns
, int prog_len
,
82 union bpf_attr attr
= {
83 .prog_type
= prog_type
,
84 .insns
= ptr_to_u64((void *) insns
),
85 .insn_cnt
= prog_len
/ sizeof(struct bpf_insn
),
86 .license
= ptr_to_u64((void *) license
),
87 .log_buf
= ptr_to_u64(bpf_log_buf
),
88 .log_size
= LOG_BUF_SIZE
,
94 return syscall(__NR_bpf
, BPF_PROG_LOAD
, &attr
, sizeof(attr
));