blob: 5128677e41179e89ca0a7ad1cddbfd7d8d69cce6 [file] [log] [blame]
Wang Nane3ed2fe2015-07-01 02:14:03 +00001/*
2 * common eBPF ELF operations.
3 *
4 * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
5 * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
6 * Copyright (C) 2015 Huawei Inc.
Wang Nan203d1ca2016-07-04 11:02:42 +00007 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation;
11 * version 2.1 of the License (not later!)
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this program; if not, see <http://www.gnu.org/licenses>
Wang Nane3ed2fe2015-07-01 02:14:03 +000020 */
21
22#include <stdlib.h>
23#include <memory.h>
24#include <unistd.h>
25#include <asm/unistd.h>
26#include <linux/bpf.h>
27#include "bpf.h"
28
29/*
Masahiro Yamada03671052017-02-27 14:29:28 -080030 * When building perf, unistd.h is overridden. __NR_bpf is
Wang Nan8f9e05f2016-01-11 13:47:57 +000031 * required to be defined explicitly.
Wang Nane3ed2fe2015-07-01 02:14:03 +000032 */
33#ifndef __NR_bpf
34# if defined(__i386__)
35# define __NR_bpf 357
36# elif defined(__x86_64__)
37# define __NR_bpf 321
38# elif defined(__aarch64__)
39# define __NR_bpf 280
David S. Millerb0c47802017-04-22 12:31:05 -070040# elif defined(__sparc__)
41# define __NR_bpf 349
Daniel Borkmannbad19262017-08-04 14:20:55 +020042# elif defined(__s390__)
43# define __NR_bpf 351
Wang Nane3ed2fe2015-07-01 02:14:03 +000044# else
45# error __NR_bpf not defined. libbpf does not support your arch.
46# endif
47#endif
48
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -070049#define min(x, y) ((x) < (y) ? (x) : (y))
50
Mickaël Salaüncdc6a4b2017-02-11 20:37:08 +010051static inline __u64 ptr_to_u64(const void *ptr)
Wang Nan7bf98362015-07-01 02:14:06 +000052{
53 return (__u64) (unsigned long) ptr;
54}
55
Mickaël Salaüncdc6a4b2017-02-11 20:37:08 +010056static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
57 unsigned int size)
Wang Nane3ed2fe2015-07-01 02:14:03 +000058{
59 return syscall(__NR_bpf, cmd, attr, size);
60}
61
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -070062int bpf_create_map_node(enum bpf_map_type map_type, const char *name,
63 int key_size, int value_size, int max_entries,
64 __u32 map_flags, int node)
Wang Nane3ed2fe2015-07-01 02:14:03 +000065{
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -070066 __u32 name_len = name ? strlen(name) : 0;
Wang Nane3ed2fe2015-07-01 02:14:03 +000067 union bpf_attr attr;
68
69 memset(&attr, '\0', sizeof(attr));
70
71 attr.map_type = map_type;
72 attr.key_size = key_size;
73 attr.value_size = value_size;
74 attr.max_entries = max_entries;
Joe Stringera5580c72016-12-08 18:46:16 -080075 attr.map_flags = map_flags;
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -070076 memcpy(attr.map_name, name, min(name_len, BPF_OBJ_NAME_LEN - 1));
77
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -070078 if (node >= 0) {
79 attr.map_flags |= BPF_F_NUMA_NODE;
80 attr.numa_node = node;
81 }
Wang Nane3ed2fe2015-07-01 02:14:03 +000082
83 return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
84}
Wang Nan7bf98362015-07-01 02:14:06 +000085
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -070086int bpf_create_map(enum bpf_map_type map_type, int key_size,
87 int value_size, int max_entries, __u32 map_flags)
88{
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -070089 return bpf_create_map_node(map_type, NULL, key_size, value_size,
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -070090 max_entries, map_flags, -1);
91}
92
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -070093int bpf_create_map_name(enum bpf_map_type map_type, const char *name,
94 int key_size, int value_size, int max_entries,
95 __u32 map_flags)
96{
97 return bpf_create_map_node(map_type, name, key_size, value_size,
98 max_entries, map_flags, -1);
99}
100
101int bpf_create_map_in_map_node(enum bpf_map_type map_type, const char *name,
102 int key_size, int inner_map_fd, int max_entries,
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -0700103 __u32 map_flags, int node)
Martin KaFai Laufb30d4b2017-03-22 10:00:35 -0700104{
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700105 __u32 name_len = name ? strlen(name) : 0;
Martin KaFai Laufb30d4b2017-03-22 10:00:35 -0700106 union bpf_attr attr;
107
108 memset(&attr, '\0', sizeof(attr));
109
110 attr.map_type = map_type;
111 attr.key_size = key_size;
112 attr.value_size = 4;
113 attr.inner_map_fd = inner_map_fd;
114 attr.max_entries = max_entries;
115 attr.map_flags = map_flags;
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700116 memcpy(attr.map_name, name, min(name_len, BPF_OBJ_NAME_LEN - 1));
117
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -0700118 if (node >= 0) {
119 attr.map_flags |= BPF_F_NUMA_NODE;
120 attr.numa_node = node;
121 }
Martin KaFai Laufb30d4b2017-03-22 10:00:35 -0700122
123 return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
124}
125
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700126int bpf_create_map_in_map(enum bpf_map_type map_type, const char *name,
127 int key_size, int inner_map_fd, int max_entries,
128 __u32 map_flags)
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -0700129{
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700130 return bpf_create_map_in_map_node(map_type, name, key_size,
131 inner_map_fd, max_entries, map_flags,
132 -1);
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -0700133}
134
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700135int bpf_load_program_name(enum bpf_prog_type type, const char *name,
136 const struct bpf_insn *insns,
137 size_t insns_cnt, const char *license,
138 __u32 kern_version, char *log_buf,
139 size_t log_buf_sz)
Wang Nan7bf98362015-07-01 02:14:06 +0000140{
141 int fd;
142 union bpf_attr attr;
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700143 __u32 name_len = name ? strlen(name) : 0;
Wang Nan7bf98362015-07-01 02:14:06 +0000144
145 bzero(&attr, sizeof(attr));
146 attr.prog_type = type;
147 attr.insn_cnt = (__u32)insns_cnt;
148 attr.insns = ptr_to_u64(insns);
149 attr.license = ptr_to_u64(license);
150 attr.log_buf = ptr_to_u64(NULL);
151 attr.log_size = 0;
152 attr.log_level = 0;
153 attr.kern_version = kern_version;
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700154 memcpy(attr.prog_name, name, min(name_len, BPF_OBJ_NAME_LEN - 1));
Wang Nan7bf98362015-07-01 02:14:06 +0000155
156 fd = sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
157 if (fd >= 0 || !log_buf || !log_buf_sz)
158 return fd;
159
160 /* Try again with log */
161 attr.log_buf = ptr_to_u64(log_buf);
162 attr.log_size = log_buf_sz;
163 attr.log_level = 1;
164 log_buf[0] = 0;
165 return sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
166}
He Kuang43798bf2015-11-24 13:36:08 +0000167
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700168int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
169 size_t insns_cnt, const char *license,
170 __u32 kern_version, char *log_buf,
171 size_t log_buf_sz)
172{
173 return bpf_load_program_name(type, NULL, insns, insns_cnt, license,
174 kern_version, log_buf, log_buf_sz);
175}
176
David S. Miller91045f52017-05-10 11:42:48 -0700177int bpf_verify_program(enum bpf_prog_type type, const struct bpf_insn *insns,
178 size_t insns_cnt, int strict_alignment,
179 const char *license, __u32 kern_version,
Daniel Borkmannd6554902017-07-21 00:00:22 +0200180 char *log_buf, size_t log_buf_sz, int log_level)
David S. Miller91045f52017-05-10 11:42:48 -0700181{
182 union bpf_attr attr;
183
184 bzero(&attr, sizeof(attr));
185 attr.prog_type = type;
186 attr.insn_cnt = (__u32)insns_cnt;
187 attr.insns = ptr_to_u64(insns);
188 attr.license = ptr_to_u64(license);
189 attr.log_buf = ptr_to_u64(log_buf);
190 attr.log_size = log_buf_sz;
Daniel Borkmannd6554902017-07-21 00:00:22 +0200191 attr.log_level = log_level;
David S. Miller91045f52017-05-10 11:42:48 -0700192 log_buf[0] = 0;
193 attr.kern_version = kern_version;
194 attr.prog_flags = strict_alignment ? BPF_F_STRICT_ALIGNMENT : 0;
195
196 return sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
197}
198
Mickaël Salaün10ecc722017-02-10 00:21:39 +0100199int bpf_map_update_elem(int fd, const void *key, const void *value,
Joe Stringer83d994d2016-12-08 18:46:15 -0800200 __u64 flags)
He Kuang43798bf2015-11-24 13:36:08 +0000201{
202 union bpf_attr attr;
203
204 bzero(&attr, sizeof(attr));
205 attr.map_fd = fd;
206 attr.key = ptr_to_u64(key);
207 attr.value = ptr_to_u64(value);
208 attr.flags = flags;
209
210 return sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
211}
Wang Nan9742da02016-11-26 07:03:25 +0000212
Mickaël Salaüne5ff7c42017-02-10 00:21:40 +0100213int bpf_map_lookup_elem(int fd, const void *key, void *value)
Wang Nan9742da02016-11-26 07:03:25 +0000214{
215 union bpf_attr attr;
216
217 bzero(&attr, sizeof(attr));
218 attr.map_fd = fd;
219 attr.key = ptr_to_u64(key);
220 attr.value = ptr_to_u64(value);
221
222 return sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
223}
224
Mickaël Salaüne58383b2017-02-10 00:21:41 +0100225int bpf_map_delete_elem(int fd, const void *key)
Wang Nan9742da02016-11-26 07:03:25 +0000226{
227 union bpf_attr attr;
228
229 bzero(&attr, sizeof(attr));
230 attr.map_fd = fd;
231 attr.key = ptr_to_u64(key);
232
233 return sys_bpf(BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
234}
235
Mickaël Salaün5f155c22017-02-10 00:21:42 +0100236int bpf_map_get_next_key(int fd, const void *key, void *next_key)
Wang Nan9742da02016-11-26 07:03:25 +0000237{
238 union bpf_attr attr;
239
240 bzero(&attr, sizeof(attr));
241 attr.map_fd = fd;
242 attr.key = ptr_to_u64(key);
243 attr.next_key = ptr_to_u64(next_key);
244
245 return sys_bpf(BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
246}
247
248int bpf_obj_pin(int fd, const char *pathname)
249{
250 union bpf_attr attr;
251
252 bzero(&attr, sizeof(attr));
253 attr.pathname = ptr_to_u64((void *)pathname);
254 attr.bpf_fd = fd;
255
256 return sys_bpf(BPF_OBJ_PIN, &attr, sizeof(attr));
257}
258
259int bpf_obj_get(const char *pathname)
260{
261 union bpf_attr attr;
262
263 bzero(&attr, sizeof(attr));
264 attr.pathname = ptr_to_u64((void *)pathname);
265
266 return sys_bpf(BPF_OBJ_GET, &attr, sizeof(attr));
267}
Joe Stringer5dc880d2016-12-14 14:05:26 -0800268
John Fastabend464bc0f2017-08-28 07:10:04 -0700269int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type,
270 unsigned int flags)
Joe Stringer5dc880d2016-12-14 14:05:26 -0800271{
272 union bpf_attr attr;
273
274 bzero(&attr, sizeof(attr));
275 attr.target_fd = target_fd;
John Fastabend464bc0f2017-08-28 07:10:04 -0700276 attr.attach_bpf_fd = prog_fd;
Joe Stringer5dc880d2016-12-14 14:05:26 -0800277 attr.attach_type = type;
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800278 attr.attach_flags = flags;
Joe Stringer5dc880d2016-12-14 14:05:26 -0800279
280 return sys_bpf(BPF_PROG_ATTACH, &attr, sizeof(attr));
281}
282
283int bpf_prog_detach(int target_fd, enum bpf_attach_type type)
284{
285 union bpf_attr attr;
286
287 bzero(&attr, sizeof(attr));
288 attr.target_fd = target_fd;
289 attr.attach_type = type;
290
291 return sys_bpf(BPF_PROG_DETACH, &attr, sizeof(attr));
292}
Alexei Starovoitov30848872017-03-30 21:45:39 -0700293
Alexei Starovoitov244d20e2017-10-02 22:50:24 -0700294int bpf_prog_detach2(int prog_fd, int target_fd, enum bpf_attach_type type)
295{
296 union bpf_attr attr;
297
298 bzero(&attr, sizeof(attr));
299 attr.target_fd = target_fd;
300 attr.attach_bpf_fd = prog_fd;
301 attr.attach_type = type;
302
303 return sys_bpf(BPF_PROG_DETACH, &attr, sizeof(attr));
304}
305
Alexei Starovoitov5d0cbf92017-10-02 22:50:27 -0700306int bpf_prog_query(int target_fd, enum bpf_attach_type type, __u32 query_flags,
307 __u32 *attach_flags, __u32 *prog_ids, __u32 *prog_cnt)
308{
309 union bpf_attr attr;
310 int ret;
311
312 bzero(&attr, sizeof(attr));
313 attr.query.target_fd = target_fd;
314 attr.query.attach_type = type;
315 attr.query.query_flags = query_flags;
316 attr.query.prog_cnt = *prog_cnt;
317 attr.query.prog_ids = ptr_to_u64(prog_ids);
318
319 ret = sys_bpf(BPF_PROG_QUERY, &attr, sizeof(attr));
320 if (attach_flags)
321 *attach_flags = attr.query.attach_flags;
322 *prog_cnt = attr.query.prog_cnt;
323 return ret;
324}
325
Alexei Starovoitov30848872017-03-30 21:45:39 -0700326int bpf_prog_test_run(int prog_fd, int repeat, void *data, __u32 size,
327 void *data_out, __u32 *size_out, __u32 *retval,
328 __u32 *duration)
329{
330 union bpf_attr attr;
331 int ret;
332
333 bzero(&attr, sizeof(attr));
334 attr.test.prog_fd = prog_fd;
335 attr.test.data_in = ptr_to_u64(data);
336 attr.test.data_out = ptr_to_u64(data_out);
337 attr.test.data_size_in = size;
338 attr.test.repeat = repeat;
339
340 ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, sizeof(attr));
341 if (size_out)
342 *size_out = attr.test.data_size_out;
343 if (retval)
344 *retval = attr.test.retval;
345 if (duration)
346 *duration = attr.test.duration;
347 return ret;
348}
Martin KaFai Lau95b9afd2017-06-05 12:15:53 -0700349
350int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id)
351{
352 union bpf_attr attr;
353 int err;
354
355 bzero(&attr, sizeof(attr));
356 attr.start_id = start_id;
357
358 err = sys_bpf(BPF_PROG_GET_NEXT_ID, &attr, sizeof(attr));
359 if (!err)
360 *next_id = attr.next_id;
361
362 return err;
363}
364
365int bpf_map_get_next_id(__u32 start_id, __u32 *next_id)
366{
367 union bpf_attr attr;
368 int err;
369
370 bzero(&attr, sizeof(attr));
371 attr.start_id = start_id;
372
373 err = sys_bpf(BPF_MAP_GET_NEXT_ID, &attr, sizeof(attr));
374 if (!err)
375 *next_id = attr.next_id;
376
377 return err;
378}
379
380int bpf_prog_get_fd_by_id(__u32 id)
381{
382 union bpf_attr attr;
383
384 bzero(&attr, sizeof(attr));
385 attr.prog_id = id;
386
387 return sys_bpf(BPF_PROG_GET_FD_BY_ID, &attr, sizeof(attr));
388}
389
390int bpf_map_get_fd_by_id(__u32 id)
391{
392 union bpf_attr attr;
393
394 bzero(&attr, sizeof(attr));
395 attr.map_id = id;
396
397 return sys_bpf(BPF_MAP_GET_FD_BY_ID, &attr, sizeof(attr));
398}
399
400int bpf_obj_get_info_by_fd(int prog_fd, void *info, __u32 *info_len)
401{
402 union bpf_attr attr;
403 int err;
404
405 bzero(&attr, sizeof(attr));
Martin KaFai Lau95b9afd2017-06-05 12:15:53 -0700406 attr.info.bpf_fd = prog_fd;
407 attr.info.info_len = *info_len;
408 attr.info.info = ptr_to_u64(info);
409
410 err = sys_bpf(BPF_OBJ_GET_INFO_BY_FD, &attr, sizeof(attr));
411 if (!err)
412 *info_len = attr.info.info_len;
413
414 return err;
415}