1 /* eBPF mini library */
4 #include <linux/unistd.h>
7 #include <linux/netlink.h>
10 #include <net/ethernet.h>
12 #include <linux/if_packet.h>
13 #include <arpa/inet.h>
16 static __u64 ptr_to_u64(void *ptr)
18 return (__u64) (unsigned long) ptr;
21 int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size,
22 int max_entries, int map_flags)
24 union bpf_attr attr = {
27 .value_size = value_size,
28 .max_entries = max_entries,
29 .map_flags = map_flags,
32 return syscall(__NR_bpf, BPF_MAP_CREATE, &attr, sizeof(attr));
35 int bpf_map_update_elem(int fd, void *key, void *value, unsigned long long flags)
37 union bpf_attr attr = {
39 .key = ptr_to_u64(key),
40 .value = ptr_to_u64(value),
44 return syscall(__NR_bpf, BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
47 int bpf_map_lookup_elem(int fd, void *key, void *value)
49 union bpf_attr attr = {
51 .key = ptr_to_u64(key),
52 .value = ptr_to_u64(value),
55 return syscall(__NR_bpf, BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
58 int bpf_map_delete_elem(int fd, void *key)
60 union bpf_attr attr = {
62 .key = ptr_to_u64(key),
65 return syscall(__NR_bpf, BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
68 int bpf_map_get_next_key(int fd, void *key, void *next_key)
70 union bpf_attr attr = {
72 .key = ptr_to_u64(key),
73 .next_key = ptr_to_u64(next_key),
76 return syscall(__NR_bpf, BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
79 #define ROUND_UP(x, n) (((x) + (n) - 1u) & ~((n) - 1u))
81 int bpf_load_program(enum bpf_prog_type prog_type,
82 const struct bpf_insn *insns, int prog_len,
83 const char *license, int kern_version,
84 char *log_buf, size_t log_buf_sz)
86 union bpf_attr attr = {
87 .prog_type = prog_type,
88 .insns = ptr_to_u64((void *) insns),
89 .insn_cnt = prog_len / sizeof(struct bpf_insn),
90 .license = ptr_to_u64((void *) license),
91 .log_buf = ptr_to_u64(log_buf),
92 .log_size = log_buf_sz,
96 /* assign one field outside of struct init to make sure any
97 * padding is zero initialized
99 attr.kern_version = kern_version;
103 return syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
106 int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type)
108 union bpf_attr attr = {
109 .target_fd = target_fd,
110 .attach_bpf_fd = prog_fd,
114 return syscall(__NR_bpf, BPF_PROG_ATTACH, &attr, sizeof(attr));
117 int bpf_prog_detach(int target_fd, enum bpf_attach_type type)
119 union bpf_attr attr = {
120 .target_fd = target_fd,
124 return syscall(__NR_bpf, BPF_PROG_DETACH, &attr, sizeof(attr));
127 int bpf_obj_pin(int fd, const char *pathname)
129 union bpf_attr attr = {
130 .pathname = ptr_to_u64((void *)pathname),
134 return syscall(__NR_bpf, BPF_OBJ_PIN, &attr, sizeof(attr));
137 int bpf_obj_get(const char *pathname)
139 union bpf_attr attr = {
140 .pathname = ptr_to_u64((void *)pathname),
143 return syscall(__NR_bpf, BPF_OBJ_GET, &attr, sizeof(attr));
146 int open_raw_sock(const char *name)
148 struct sockaddr_ll sll;
151 sock = socket(PF_PACKET, SOCK_RAW | SOCK_NONBLOCK | SOCK_CLOEXEC, htons(ETH_P_ALL));
153 printf("cannot create raw socket\n");
157 memset(&sll, 0, sizeof(sll));
158 sll.sll_family = AF_PACKET;
159 sll.sll_ifindex = if_nametoindex(name);
160 sll.sll_protocol = htons(ETH_P_ALL);
161 if (bind(sock, (struct sockaddr *)&sll, sizeof(sll)) < 0) {
162 printf("bind to %s: %s\n", name, strerror(errno));
170 int perf_event_open(struct perf_event_attr *attr, int pid, int cpu,
171 int group_fd, unsigned long flags)
173 return syscall(__NR_perf_event_open, attr, pid, cpu,