blob: 9cbc786e48e5a8b51bc3c874b30fc91081727e46 [file] [log] [blame]
Alexei Starovoitov3c731eb2014-09-26 00:17:07 -07001/* eBPF mini library */
2#include <stdlib.h>
3#include <stdio.h>
4#include <linux/unistd.h>
5#include <unistd.h>
6#include <string.h>
7#include <linux/netlink.h>
8#include <linux/bpf.h>
9#include <errno.h>
Alexei Starovoitov03f47232014-12-01 15:06:36 -080010#include <net/ethernet.h>
11#include <net/if.h>
12#include <linux/if_packet.h>
13#include <arpa/inet.h>
Alexei Starovoitov3c731eb2014-09-26 00:17:07 -070014#include "libbpf.h"
15
16static __u64 ptr_to_u64(void *ptr)
17{
18 return (__u64) (unsigned long) ptr;
19}
20
21int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size,
Alexei Starovoitov89b97602016-03-07 21:57:20 -080022 int max_entries, int map_flags)
Alexei Starovoitov3c731eb2014-09-26 00:17:07 -070023{
24 union bpf_attr attr = {
25 .map_type = map_type,
26 .key_size = key_size,
27 .value_size = value_size,
Alexei Starovoitov89b97602016-03-07 21:57:20 -080028 .max_entries = max_entries,
29 .map_flags = map_flags,
Alexei Starovoitov3c731eb2014-09-26 00:17:07 -070030 };
31
32 return syscall(__NR_bpf, BPF_MAP_CREATE, &attr, sizeof(attr));
33}
34
Alexei Starovoitovffb65f22014-11-13 17:36:48 -080035int bpf_update_elem(int fd, void *key, void *value, unsigned long long flags)
Alexei Starovoitov3c731eb2014-09-26 00:17:07 -070036{
37 union bpf_attr attr = {
38 .map_fd = fd,
39 .key = ptr_to_u64(key),
40 .value = ptr_to_u64(value),
Alexei Starovoitovffb65f22014-11-13 17:36:48 -080041 .flags = flags,
Alexei Starovoitov3c731eb2014-09-26 00:17:07 -070042 };
43
44 return syscall(__NR_bpf, BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
45}
46
47int bpf_lookup_elem(int fd, void *key, void *value)
48{
49 union bpf_attr attr = {
50 .map_fd = fd,
51 .key = ptr_to_u64(key),
52 .value = ptr_to_u64(value),
53 };
54
55 return syscall(__NR_bpf, BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
56}
57
58int bpf_delete_elem(int fd, void *key)
59{
60 union bpf_attr attr = {
61 .map_fd = fd,
62 .key = ptr_to_u64(key),
63 };
64
65 return syscall(__NR_bpf, BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
66}
67
68int bpf_get_next_key(int fd, void *key, void *next_key)
69{
70 union bpf_attr attr = {
71 .map_fd = fd,
72 .key = ptr_to_u64(key),
73 .next_key = ptr_to_u64(next_key),
74 };
75
76 return syscall(__NR_bpf, BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
77}
78
79#define ROUND_UP(x, n) (((x) + (n) - 1u) & ~((n) - 1u))
80
81char bpf_log_buf[LOG_BUF_SIZE];
82
83int bpf_prog_load(enum bpf_prog_type prog_type,
84 const struct bpf_insn *insns, int prog_len,
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070085 const char *license, int kern_version)
Alexei Starovoitov3c731eb2014-09-26 00:17:07 -070086{
87 union bpf_attr attr = {
88 .prog_type = prog_type,
89 .insns = ptr_to_u64((void *) insns),
90 .insn_cnt = prog_len / sizeof(struct bpf_insn),
91 .license = ptr_to_u64((void *) license),
92 .log_buf = ptr_to_u64(bpf_log_buf),
93 .log_size = LOG_BUF_SIZE,
94 .log_level = 1,
95 };
96
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070097 /* assign one field outside of struct init to make sure any
98 * padding is zero initialized
99 */
100 attr.kern_version = kern_version;
101
Alexei Starovoitov3c731eb2014-09-26 00:17:07 -0700102 bpf_log_buf[0] = 0;
103
104 return syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
105}
Alexei Starovoitov03f47232014-12-01 15:06:36 -0800106
Alexei Starovoitov1ee2b4b2017-02-10 20:28:24 -0800107int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type,
108 unsigned int flags)
Daniel Mackc00662a2016-11-23 16:52:30 +0100109{
110 union bpf_attr attr = {
111 .target_fd = target_fd,
112 .attach_bpf_fd = prog_fd,
113 .attach_type = type,
Alexei Starovoitov1ee2b4b2017-02-10 20:28:24 -0800114 .attach_flags = flags;
Daniel Mackc00662a2016-11-23 16:52:30 +0100115 };
116
117 return syscall(__NR_bpf, BPF_PROG_ATTACH, &attr, sizeof(attr));
118}
119
120int bpf_prog_detach(int target_fd, enum bpf_attach_type type)
121{
122 union bpf_attr attr = {
123 .target_fd = target_fd,
124 .attach_type = type,
125 };
126
127 return syscall(__NR_bpf, BPF_PROG_DETACH, &attr, sizeof(attr));
128}
129
Daniel Borkmann42984d72015-10-29 14:58:10 +0100130int bpf_obj_pin(int fd, const char *pathname)
131{
132 union bpf_attr attr = {
133 .pathname = ptr_to_u64((void *)pathname),
134 .bpf_fd = fd,
135 };
136
137 return syscall(__NR_bpf, BPF_OBJ_PIN, &attr, sizeof(attr));
138}
139
140int bpf_obj_get(const char *pathname)
141{
142 union bpf_attr attr = {
143 .pathname = ptr_to_u64((void *)pathname),
144 };
145
146 return syscall(__NR_bpf, BPF_OBJ_GET, &attr, sizeof(attr));
147}
148
Alexei Starovoitov03f47232014-12-01 15:06:36 -0800149int open_raw_sock(const char *name)
150{
151 struct sockaddr_ll sll;
152 int sock;
153
154 sock = socket(PF_PACKET, SOCK_RAW | SOCK_NONBLOCK | SOCK_CLOEXEC, htons(ETH_P_ALL));
155 if (sock < 0) {
156 printf("cannot create raw socket\n");
157 return -1;
158 }
159
160 memset(&sll, 0, sizeof(sll));
161 sll.sll_family = AF_PACKET;
162 sll.sll_ifindex = if_nametoindex(name);
163 sll.sll_protocol = htons(ETH_P_ALL);
164 if (bind(sock, (struct sockaddr *)&sll, sizeof(sll)) < 0) {
165 printf("bind to %s: %s\n", name, strerror(errno));
166 close(sock);
167 return -1;
168 }
169
170 return sock;
171}
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700172
173int perf_event_open(struct perf_event_attr *attr, int pid, int cpu,
174 int group_fd, unsigned long flags)
175{
176 return syscall(__NR_perf_event_open, attr, pid, cpu,
177 group_fd, flags);
178}