Debian Codegraph @

stapbpf/libbpf.c

raw
1 /* 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>
10 #include <net/ethernet.h>
11 #include <net/if.h>
12 #include <linux/if_packet.h>
13 #include <linux/perf_event.h>
14 #include <arpa/inet.h>
15 #include "libbpf.h"
16
17 /* Older headers might not have this defined yet. */
18 #ifndef __NR_bpf
19 # if defined(__i386__)
20 # define __NR_bpf 357
21 # elif defined(__x86_64__)
22 # define __NR_bpf 321
23 # elif defined(__aarch64__)
24 # define __NR_bpf 280
25 # else
26 # error __NR_bpf not defined.
27 # endif
28 #endif
29
30 static __u64 ptr_to_u64(void *ptr)
31 {
32 return (__u64) (unsigned long) ptr;
33 }
34
35 int bpf_create_map(enum bpf_map_type map_type, unsigned key_size,
36 unsigned value_size, unsigned max_entries,
37 unsigned flags __attribute__((unused)))
38 {
39 union bpf_attr attr;
40 memset(&attr, 0, sizeof(union bpf_attr));
41 attr.map_type = map_type;
42 attr.key_size = key_size;
43 attr.value_size = value_size;
44 attr.max_entries = max_entries;
45
46 return syscall(__NR_bpf, BPF_MAP_CREATE, &attr, sizeof(attr));
47 }
48
49 int bpf_update_elem(int fd, void *key, void *value, unsigned long long flags)
50 {
51 union bpf_attr attr;
52 memset(&attr, 0, sizeof(union bpf_attr));
53 attr.map_fd = fd;
54 attr.key = ptr_to_u64(key);
55 attr.value = ptr_to_u64(value);
56 attr.flags = flags;
57
58 return syscall(__NR_bpf, BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
59 }
60
61 int bpf_lookup_elem(int fd, void *key, void *value)
62 {
63 union bpf_attr attr;
64 memset(&attr, 0, sizeof(union bpf_attr));
65 attr.map_fd = fd;
66 attr.key = ptr_to_u64(key);
67 attr.value = ptr_to_u64(value);
68
69 return syscall(__NR_bpf, BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
70 }
71
72 int bpf_delete_elem(int fd, void *key)
73 {
74 union bpf_attr attr;
75 memset(&attr, 0, sizeof(union bpf_attr));
76 attr.map_fd = fd;
77 attr.key = ptr_to_u64(key);
78
79 return syscall(__NR_bpf, BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
80 }
81
82 int bpf_get_next_key(int fd, void *key, void *next_key)
83 {
84 union bpf_attr attr;
85 memset(&attr, 0, sizeof(union bpf_attr));
86 attr.map_fd = fd;
87 attr.key = ptr_to_u64(key);
88 attr.next_key = ptr_to_u64(next_key);
89
90 return syscall(__NR_bpf, BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
91 }
92
93 #define ROUND_UP(x, n) (((x) + (n) - 1u) & ~((n) - 1u))
94
95 char bpf_log_buf[LOG_BUF_SIZE];
96 extern int verbose; // set from stapbpf command line
97
98 int bpf_prog_load(enum bpf_prog_type prog_type,
99 const struct bpf_insn *insns, int prog_len,
100 const char *license, int kern_version)
101 {
102 union bpf_attr attr;
103 memset (&attr, 0, sizeof(attr)); // kernel asserts 0 pad values
104 attr.prog_type = prog_type;
105 attr.insns = ptr_to_u64((void *) insns);
106 attr.insn_cnt = prog_len / sizeof(struct bpf_insn);
107 attr.license = ptr_to_u64((void *) license);
108
109 /* If the syscall fails, retry with higher verbosity to get
110 the eBPF verifier output */
111 int retry = 0;
112 do_retry:
113 if (verbose || retry)
114 {
115 attr.log_buf = ptr_to_u64(bpf_log_buf);
116 attr.log_size = LOG_BUF_SIZE;
117 attr.log_level = retry ? verbose + 1 : verbose;
118 /* they hang together, or they hang separately with -EINVAL */
119 }
120
121 /* assign one field outside of struct init to make sure any
122 * padding is zero initialized
123 */
124 attr.kern_version = kern_version;
125
126 bpf_log_buf[0] = 0;
127
128 if (verbose > 1)
129 fprintf(stderr, "Loading probe type %d, size %d\n", prog_type, prog_len);
130
131 int rc = syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
132 if (rc < 0 && verbose == 0 && !retry)
133 {
134 retry = 1; goto do_retry;
135 }
136 return rc;
137 }
138
139 int bpf_obj_pin(int fd, const char *pathname)
140 {
141 union bpf_attr attr;
142 memset(&attr, 0, sizeof(union bpf_attr));
143 attr.pathname = ptr_to_u64((void *)pathname);
144 attr.bpf_fd = fd;
145
146 return syscall(__NR_bpf, BPF_OBJ_PIN, &attr, sizeof(attr));
147 }
148
149 // XXX experimental, imitates tools/include/linux/ring_buffer.h and bcc
150 static inline __u64
151 ring_buffer_read_head(volatile struct perf_event_mmap_page *base)
152 {
153 __u64 head = base->data_head;
154 asm volatile ("" ::: "memory"); // memory fence
155 return head;
156 }
157
158 // XXX experimental, imitates tools/include/linux/ring_buffer.h and bcc
159 static inline void
160 ring_buffer_write_tail(volatile struct perf_event_mmap_page *base,
161 __u64 tail)
162 {
163 asm volatile("" ::: "memory"); // memory fence
164 base->data_tail = tail;
165 }
166
167
168 enum bpf_perf_event_ret
169 bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
170 void **copy_mem, size_t *copy_size,
171 bpf_perf_event_print_t fn, void *private_data)
172 {
173 struct perf_event_mmap_page *header = mmap_mem;
174 __u64 data_head = ring_buffer_read_head(header);
175 __u64 data_tail = header->data_tail;
176 void *base = ((__u8 *)header) + page_size;
177 int ret = LIBBPF_PERF_EVENT_CONT;
178 struct perf_event_header *ehdr;
179 size_t ehdr_size;
180
181 while (data_head != data_tail) {
182 ehdr = base + (data_tail & (mmap_size - 1));
183 ehdr_size = ehdr->size;
184
185 if (((void *)ehdr) + ehdr_size > base + mmap_size) {
186 void *copy_start = ehdr;
187 size_t len_first = base + mmap_size - copy_start;
188 size_t len_secnd = ehdr_size - len_first;
189
190 if (*copy_size < ehdr_size) {
191 free(*copy_mem);
192 *copy_mem = malloc(ehdr_size);
193 if (!*copy_mem) {
194 *copy_size = 0;
195 ret = LIBBPF_PERF_EVENT_ERROR;
196 break;
197 }
198 *copy_size = ehdr_size;
199 }
200
201 memcpy(*copy_mem, copy_start, len_first);
202 memcpy(*copy_mem + len_first, base, len_secnd);
203 ehdr = *copy_mem;
204 }
205
206 ret = fn(ehdr, private_data);
207 data_tail += ehdr_size;
208 if (ret != LIBBPF_PERF_EVENT_CONT)
209 break;
210 }
211
212 ring_buffer_write_tail(header, data_tail);
213 return ret;
214 }
215
216 int bpf_obj_get(const char *pathname)
217 {
218 union bpf_attr attr;
219 memset(&attr, 0, sizeof(union bpf_attr));
220 attr.pathname = ptr_to_u64((void *)pathname);
221
222 return syscall(__NR_bpf, BPF_OBJ_GET, &attr, sizeof(attr));
223 }
224
225 int open_raw_sock(const char *name)
226 {
227 struct sockaddr_ll sll;
228 int sock;
229
230 sock = socket(PF_PACKET, SOCK_RAW | SOCK_NONBLOCK | SOCK_CLOEXEC, htons(ETH_P_ALL));
231 if (sock < 0) {
232 printf("cannot create raw socket\n");
233 return -1;
234 }
235
236 memset(&sll, 0, sizeof(sll));
237 sll.sll_family = AF_PACKET;
238 sll.sll_ifindex = if_nametoindex(name);
239 sll.sll_protocol = htons(ETH_P_ALL);
240 if (bind(sock, (struct sockaddr *)&sll, sizeof(sll)) < 0) {
241 printf("bind to %s: %s\n", name, strerror(errno));
242 close(sock);
243 return -1;
244 }
245
246 return sock;
247 }
248
249 int perf_event_open(struct perf_event_attr *attr, int pid, int cpu,
250 int group_fd, unsigned long flags)
251 {
252 return syscall(__NR_perf_event_open, attr, pid, cpu,
253 group_fd, flags);
254 }