blob: 9cd015574e83828d20ace576fe7190f68a924829 [file] [log] [blame]
Alexei Starovoitov1bc38b82018-10-05 16:40:00 -07001// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
Eric Leblond6061a3d2018-01-30 21:55:03 +01002
Wang Nane3ed2fe2015-07-01 02:14:03 +00003/*
4 * common eBPF ELF operations.
5 *
6 * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
7 * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
8 * Copyright (C) 2015 Huawei Inc.
Wang Nan203d1ca2016-07-04 11:02:42 +00009 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation;
13 * version 2.1 of the License (not later!)
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this program; if not, see <http://www.gnu.org/licenses>
Wang Nane3ed2fe2015-07-01 02:14:03 +000022 */
23
24#include <stdlib.h>
Andrii Nakryiko1ad9cbb2019-02-13 10:25:53 -080025#include <string.h>
Wang Nane3ed2fe2015-07-01 02:14:03 +000026#include <memory.h>
27#include <unistd.h>
28#include <asm/unistd.h>
29#include <linux/bpf.h>
30#include "bpf.h"
Eric Leblond949abbe2018-01-30 21:55:01 +010031#include "libbpf.h"
Eric Leblond949abbe2018-01-30 21:55:01 +010032#include <errno.h>
Wang Nane3ed2fe2015-07-01 02:14:03 +000033
34/*
Masahiro Yamada03671052017-02-27 14:29:28 -080035 * When building perf, unistd.h is overridden. __NR_bpf is
Wang Nan8f9e05f2016-01-11 13:47:57 +000036 * required to be defined explicitly.
Wang Nane3ed2fe2015-07-01 02:14:03 +000037 */
38#ifndef __NR_bpf
39# if defined(__i386__)
40# define __NR_bpf 357
41# elif defined(__x86_64__)
42# define __NR_bpf 321
43# elif defined(__aarch64__)
44# define __NR_bpf 280
David S. Millerb0c47802017-04-22 12:31:05 -070045# elif defined(__sparc__)
46# define __NR_bpf 349
Daniel Borkmannbad19262017-08-04 14:20:55 +020047# elif defined(__s390__)
48# define __NR_bpf 351
Wang Nane3ed2fe2015-07-01 02:14:03 +000049# else
50# error __NR_bpf not defined. libbpf does not support your arch.
51# endif
52#endif
53
Eric Leblond949abbe2018-01-30 21:55:01 +010054#ifndef min
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -070055#define min(x, y) ((x) < (y) ? (x) : (y))
Eric Leblond949abbe2018-01-30 21:55:01 +010056#endif
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -070057
Mickaël Salaüncdc6a4b2017-02-11 20:37:08 +010058static inline __u64 ptr_to_u64(const void *ptr)
Wang Nan7bf98362015-07-01 02:14:06 +000059{
60 return (__u64) (unsigned long) ptr;
61}
62
Mickaël Salaüncdc6a4b2017-02-11 20:37:08 +010063static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
64 unsigned int size)
Wang Nane3ed2fe2015-07-01 02:14:03 +000065{
66 return syscall(__NR_bpf, cmd, attr, size);
67}
68
Lorenz Bauer86edaed2019-01-08 13:58:00 +000069static inline int sys_bpf_prog_load(union bpf_attr *attr, unsigned int size)
70{
71 int fd;
72
73 do {
74 fd = sys_bpf(BPF_PROG_LOAD, attr, size);
75 } while (fd < 0 && errno == EAGAIN);
76
77 return fd;
78}
79
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -070080int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr)
Wang Nane3ed2fe2015-07-01 02:14:03 +000081{
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -070082 __u32 name_len = create_attr->name ? strlen(create_attr->name) : 0;
Wang Nane3ed2fe2015-07-01 02:14:03 +000083 union bpf_attr attr;
84
85 memset(&attr, '\0', sizeof(attr));
86
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -070087 attr.map_type = create_attr->map_type;
88 attr.key_size = create_attr->key_size;
89 attr.value_size = create_attr->value_size;
90 attr.max_entries = create_attr->max_entries;
91 attr.map_flags = create_attr->map_flags;
92 memcpy(attr.map_name, create_attr->name,
93 min(name_len, BPF_OBJ_NAME_LEN - 1));
94 attr.numa_node = create_attr->numa_node;
95 attr.btf_fd = create_attr->btf_fd;
Martin KaFai Lau61746dbe2018-05-22 15:04:24 -070096 attr.btf_key_type_id = create_attr->btf_key_type_id;
97 attr.btf_value_type_id = create_attr->btf_value_type_id;
David Beckettf0307a72018-05-16 14:02:49 -070098 attr.map_ifindex = create_attr->map_ifindex;
Martin KaFai Lau91134d82018-08-08 01:01:31 -070099 attr.inner_map_fd = create_attr->inner_map_fd;
Wang Nane3ed2fe2015-07-01 02:14:03 +0000100
Stanislav Fomichev94cb3102018-11-20 17:11:20 -0800101 return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
Wang Nane3ed2fe2015-07-01 02:14:03 +0000102}
Wang Nan7bf98362015-07-01 02:14:06 +0000103
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700104int bpf_create_map_node(enum bpf_map_type map_type, const char *name,
105 int key_size, int value_size, int max_entries,
106 __u32 map_flags, int node)
107{
108 struct bpf_create_map_attr map_attr = {};
109
110 map_attr.name = name;
111 map_attr.map_type = map_type;
112 map_attr.map_flags = map_flags;
113 map_attr.key_size = key_size;
114 map_attr.value_size = value_size;
115 map_attr.max_entries = max_entries;
116 if (node >= 0) {
117 map_attr.numa_node = node;
118 map_attr.map_flags |= BPF_F_NUMA_NODE;
119 }
120
121 return bpf_create_map_xattr(&map_attr);
122}
123
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -0700124int bpf_create_map(enum bpf_map_type map_type, int key_size,
125 int value_size, int max_entries, __u32 map_flags)
126{
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700127 struct bpf_create_map_attr map_attr = {};
128
129 map_attr.map_type = map_type;
130 map_attr.map_flags = map_flags;
131 map_attr.key_size = key_size;
132 map_attr.value_size = value_size;
133 map_attr.max_entries = max_entries;
134
135 return bpf_create_map_xattr(&map_attr);
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -0700136}
137
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700138int bpf_create_map_name(enum bpf_map_type map_type, const char *name,
139 int key_size, int value_size, int max_entries,
140 __u32 map_flags)
141{
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700142 struct bpf_create_map_attr map_attr = {};
143
144 map_attr.name = name;
145 map_attr.map_type = map_type;
146 map_attr.map_flags = map_flags;
147 map_attr.key_size = key_size;
148 map_attr.value_size = value_size;
149 map_attr.max_entries = max_entries;
150
151 return bpf_create_map_xattr(&map_attr);
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700152}
153
154int bpf_create_map_in_map_node(enum bpf_map_type map_type, const char *name,
155 int key_size, int inner_map_fd, int max_entries,
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -0700156 __u32 map_flags, int node)
Martin KaFai Laufb30d4b2017-03-22 10:00:35 -0700157{
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700158 __u32 name_len = name ? strlen(name) : 0;
Martin KaFai Laufb30d4b2017-03-22 10:00:35 -0700159 union bpf_attr attr;
160
161 memset(&attr, '\0', sizeof(attr));
162
163 attr.map_type = map_type;
164 attr.key_size = key_size;
165 attr.value_size = 4;
166 attr.inner_map_fd = inner_map_fd;
167 attr.max_entries = max_entries;
168 attr.map_flags = map_flags;
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700169 memcpy(attr.map_name, name, min(name_len, BPF_OBJ_NAME_LEN - 1));
170
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -0700171 if (node >= 0) {
172 attr.map_flags |= BPF_F_NUMA_NODE;
173 attr.numa_node = node;
174 }
Martin KaFai Laufb30d4b2017-03-22 10:00:35 -0700175
176 return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
177}
178
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700179int bpf_create_map_in_map(enum bpf_map_type map_type, const char *name,
180 int key_size, int inner_map_fd, int max_entries,
181 __u32 map_flags)
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -0700182{
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700183 return bpf_create_map_in_map_node(map_type, name, key_size,
184 inner_map_fd, max_entries, map_flags,
185 -1);
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -0700186}
187
Martin KaFai Lau3d650142018-12-07 16:42:31 -0800188static void *
189alloc_zero_tailing_info(const void *orecord, __u32 cnt,
190 __u32 actual_rec_size, __u32 expected_rec_size)
191{
192 __u64 info_len = actual_rec_size * cnt;
193 void *info, *nrecord;
194 int i;
195
196 info = malloc(info_len);
197 if (!info)
198 return NULL;
199
200 /* zero out bytes kernel does not understand */
201 nrecord = info;
202 for (i = 0; i < cnt; i++) {
203 memcpy(nrecord, orecord, expected_rec_size);
204 memset(nrecord + expected_rec_size, 0,
205 actual_rec_size - expected_rec_size);
206 orecord += actual_rec_size;
207 nrecord += actual_rec_size;
208 }
209
210 return info;
211}
212
Andrey Ignatovd7be1432018-03-30 15:08:01 -0700213int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
214 char *log_buf, size_t log_buf_sz)
Wang Nan7bf98362015-07-01 02:14:06 +0000215{
Martin KaFai Lau3d650142018-12-07 16:42:31 -0800216 void *finfo = NULL, *linfo = NULL;
Wang Nan7bf98362015-07-01 02:14:06 +0000217 union bpf_attr attr;
Yonghong Songa4021a32019-02-07 09:34:51 -0800218 __u32 log_level;
Andrey Ignatovd7be1432018-03-30 15:08:01 -0700219 __u32 name_len;
220 int fd;
221
Yonghong Songa4021a32019-02-07 09:34:51 -0800222 if (!load_attr || !log_buf != !log_buf_sz)
223 return -EINVAL;
224
225 log_level = load_attr->log_level;
226 if (log_level > 2 || (log_level && !log_buf))
Andrey Ignatovd7be1432018-03-30 15:08:01 -0700227 return -EINVAL;
228
229 name_len = load_attr->name ? strlen(load_attr->name) : 0;
Wang Nan7bf98362015-07-01 02:14:06 +0000230
Andrii Nakryiko1ad9cbb2019-02-13 10:25:53 -0800231 memset(&attr, 0, sizeof(attr));
Andrey Ignatovd7be1432018-03-30 15:08:01 -0700232 attr.prog_type = load_attr->prog_type;
233 attr.expected_attach_type = load_attr->expected_attach_type;
234 attr.insn_cnt = (__u32)load_attr->insns_cnt;
235 attr.insns = ptr_to_u64(load_attr->insns);
236 attr.license = ptr_to_u64(load_attr->license);
Yonghong Songa4021a32019-02-07 09:34:51 -0800237
238 attr.log_level = log_level;
239 if (log_level) {
240 attr.log_buf = ptr_to_u64(log_buf);
241 attr.log_size = log_buf_sz;
242 } else {
243 attr.log_buf = ptr_to_u64(NULL);
244 attr.log_size = 0;
245 }
246
Andrey Ignatovd7be1432018-03-30 15:08:01 -0700247 attr.kern_version = load_attr->kern_version;
David Beckettf0307a72018-05-16 14:02:49 -0700248 attr.prog_ifindex = load_attr->prog_ifindex;
Yonghong Song7e0d0fb2018-11-19 15:29:14 -0800249 attr.prog_btf_fd = load_attr->prog_btf_fd;
250 attr.func_info_rec_size = load_attr->func_info_rec_size;
251 attr.func_info_cnt = load_attr->func_info_cnt;
252 attr.func_info = ptr_to_u64(load_attr->func_info);
Martin KaFai Lau3d650142018-12-07 16:42:31 -0800253 attr.line_info_rec_size = load_attr->line_info_rec_size;
254 attr.line_info_cnt = load_attr->line_info_cnt;
255 attr.line_info = ptr_to_u64(load_attr->line_info);
Andrey Ignatovd7be1432018-03-30 15:08:01 -0700256 memcpy(attr.prog_name, load_attr->name,
257 min(name_len, BPF_OBJ_NAME_LEN - 1));
Wang Nan7bf98362015-07-01 02:14:06 +0000258
Lorenz Bauer86edaed2019-01-08 13:58:00 +0000259 fd = sys_bpf_prog_load(&attr, sizeof(attr));
Martin KaFai Lauf0187f02018-12-07 16:42:29 -0800260 if (fd >= 0)
Wang Nan7bf98362015-07-01 02:14:06 +0000261 return fd;
262
Yonghong Song2993e052018-11-19 15:29:16 -0800263 /* After bpf_prog_load, the kernel may modify certain attributes
264 * to give user space a hint how to deal with loading failure.
265 * Check to see whether we can make some changes and load again.
266 */
Martin KaFai Lau3d650142018-12-07 16:42:31 -0800267 while (errno == E2BIG && (!finfo || !linfo)) {
268 if (!finfo && attr.func_info_cnt &&
269 attr.func_info_rec_size < load_attr->func_info_rec_size) {
270 /* try with corrected func info records */
271 finfo = alloc_zero_tailing_info(load_attr->func_info,
272 load_attr->func_info_cnt,
273 load_attr->func_info_rec_size,
274 attr.func_info_rec_size);
275 if (!finfo)
276 goto done;
Yonghong Song2993e052018-11-19 15:29:16 -0800277
Martin KaFai Lau3d650142018-12-07 16:42:31 -0800278 attr.func_info = ptr_to_u64(finfo);
279 attr.func_info_rec_size = load_attr->func_info_rec_size;
280 } else if (!linfo && attr.line_info_cnt &&
281 attr.line_info_rec_size <
282 load_attr->line_info_rec_size) {
283 linfo = alloc_zero_tailing_info(load_attr->line_info,
284 load_attr->line_info_cnt,
285 load_attr->line_info_rec_size,
286 attr.line_info_rec_size);
287 if (!linfo)
288 goto done;
Yonghong Song2993e052018-11-19 15:29:16 -0800289
Martin KaFai Lau3d650142018-12-07 16:42:31 -0800290 attr.line_info = ptr_to_u64(linfo);
291 attr.line_info_rec_size = load_attr->line_info_rec_size;
292 } else {
293 break;
Yonghong Song2993e052018-11-19 15:29:16 -0800294 }
295
Lorenz Bauer86edaed2019-01-08 13:58:00 +0000296 fd = sys_bpf_prog_load(&attr, sizeof(attr));
Yonghong Song2993e052018-11-19 15:29:16 -0800297
Martin KaFai Lauf0187f02018-12-07 16:42:29 -0800298 if (fd >= 0)
Yonghong Song2993e052018-11-19 15:29:16 -0800299 goto done;
300 }
301
Yonghong Songa4021a32019-02-07 09:34:51 -0800302 if (log_level || !log_buf)
Martin KaFai Lauf0187f02018-12-07 16:42:29 -0800303 goto done;
304
Wang Nan7bf98362015-07-01 02:14:06 +0000305 /* Try again with log */
306 attr.log_buf = ptr_to_u64(log_buf);
307 attr.log_size = log_buf_sz;
308 attr.log_level = 1;
309 log_buf[0] = 0;
Lorenz Bauer86edaed2019-01-08 13:58:00 +0000310 fd = sys_bpf_prog_load(&attr, sizeof(attr));
Yonghong Song2993e052018-11-19 15:29:16 -0800311done:
312 free(finfo);
Martin KaFai Lau3d650142018-12-07 16:42:31 -0800313 free(linfo);
Yonghong Song2993e052018-11-19 15:29:16 -0800314 return fd;
Wang Nan7bf98362015-07-01 02:14:06 +0000315}
He Kuang43798bf2015-11-24 13:36:08 +0000316
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700317int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
318 size_t insns_cnt, const char *license,
319 __u32 kern_version, char *log_buf,
320 size_t log_buf_sz)
321{
Andrey Ignatovd7be1432018-03-30 15:08:01 -0700322 struct bpf_load_program_attr load_attr;
323
324 memset(&load_attr, 0, sizeof(struct bpf_load_program_attr));
325 load_attr.prog_type = type;
326 load_attr.expected_attach_type = 0;
327 load_attr.name = NULL;
328 load_attr.insns = insns;
329 load_attr.insns_cnt = insns_cnt;
330 load_attr.license = license;
331 load_attr.kern_version = kern_version;
332
333 return bpf_load_program_xattr(&load_attr, log_buf, log_buf_sz);
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700334}
335
David S. Miller91045f52017-05-10 11:42:48 -0700336int bpf_verify_program(enum bpf_prog_type type, const struct bpf_insn *insns,
David Millere9ee9ef2018-11-30 21:08:14 -0800337 size_t insns_cnt, __u32 prog_flags, const char *license,
338 __u32 kern_version, char *log_buf, size_t log_buf_sz,
339 int log_level)
David S. Miller91045f52017-05-10 11:42:48 -0700340{
341 union bpf_attr attr;
342
Andrii Nakryiko1ad9cbb2019-02-13 10:25:53 -0800343 memset(&attr, 0, sizeof(attr));
David S. Miller91045f52017-05-10 11:42:48 -0700344 attr.prog_type = type;
345 attr.insn_cnt = (__u32)insns_cnt;
346 attr.insns = ptr_to_u64(insns);
347 attr.license = ptr_to_u64(license);
348 attr.log_buf = ptr_to_u64(log_buf);
349 attr.log_size = log_buf_sz;
Daniel Borkmannd6554902017-07-21 00:00:22 +0200350 attr.log_level = log_level;
David S. Miller91045f52017-05-10 11:42:48 -0700351 log_buf[0] = 0;
352 attr.kern_version = kern_version;
David Millere9ee9ef2018-11-30 21:08:14 -0800353 attr.prog_flags = prog_flags;
David S. Miller91045f52017-05-10 11:42:48 -0700354
Lorenz Bauer86edaed2019-01-08 13:58:00 +0000355 return sys_bpf_prog_load(&attr, sizeof(attr));
David S. Miller91045f52017-05-10 11:42:48 -0700356}
357
Mickaël Salaün10ecc722017-02-10 00:21:39 +0100358int bpf_map_update_elem(int fd, const void *key, const void *value,
Joe Stringer83d994d2016-12-08 18:46:15 -0800359 __u64 flags)
He Kuang43798bf2015-11-24 13:36:08 +0000360{
361 union bpf_attr attr;
362
Andrii Nakryiko1ad9cbb2019-02-13 10:25:53 -0800363 memset(&attr, 0, sizeof(attr));
He Kuang43798bf2015-11-24 13:36:08 +0000364 attr.map_fd = fd;
365 attr.key = ptr_to_u64(key);
366 attr.value = ptr_to_u64(value);
367 attr.flags = flags;
368
369 return sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
370}
Wang Nan9742da02016-11-26 07:03:25 +0000371
Mickaël Salaüne5ff7c42017-02-10 00:21:40 +0100372int bpf_map_lookup_elem(int fd, const void *key, void *value)
Wang Nan9742da02016-11-26 07:03:25 +0000373{
374 union bpf_attr attr;
375
Andrii Nakryiko1ad9cbb2019-02-13 10:25:53 -0800376 memset(&attr, 0, sizeof(attr));
Wang Nan9742da02016-11-26 07:03:25 +0000377 attr.map_fd = fd;
378 attr.key = ptr_to_u64(key);
379 attr.value = ptr_to_u64(value);
380
381 return sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
382}
383
Alexei Starovoitovdf5d22f2019-01-31 15:40:11 -0800384int bpf_map_lookup_elem_flags(int fd, const void *key, void *value, __u64 flags)
385{
386 union bpf_attr attr;
387
Andrii Nakryiko1ad9cbb2019-02-13 10:25:53 -0800388 memset(&attr, 0, sizeof(attr));
Alexei Starovoitovdf5d22f2019-01-31 15:40:11 -0800389 attr.map_fd = fd;
390 attr.key = ptr_to_u64(key);
391 attr.value = ptr_to_u64(value);
392 attr.flags = flags;
393
394 return sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
395}
396
Mauricio Vasquez B43b987d2018-10-18 15:16:41 +0200397int bpf_map_lookup_and_delete_elem(int fd, const void *key, void *value)
398{
399 union bpf_attr attr;
400
Andrii Nakryiko1ad9cbb2019-02-13 10:25:53 -0800401 memset(&attr, 0, sizeof(attr));
Mauricio Vasquez B43b987d2018-10-18 15:16:41 +0200402 attr.map_fd = fd;
403 attr.key = ptr_to_u64(key);
404 attr.value = ptr_to_u64(value);
405
406 return sys_bpf(BPF_MAP_LOOKUP_AND_DELETE_ELEM, &attr, sizeof(attr));
407}
408
Mickaël Salaüne58383b2017-02-10 00:21:41 +0100409int bpf_map_delete_elem(int fd, const void *key)
Wang Nan9742da02016-11-26 07:03:25 +0000410{
411 union bpf_attr attr;
412
Andrii Nakryiko1ad9cbb2019-02-13 10:25:53 -0800413 memset(&attr, 0, sizeof(attr));
Wang Nan9742da02016-11-26 07:03:25 +0000414 attr.map_fd = fd;
415 attr.key = ptr_to_u64(key);
416
417 return sys_bpf(BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
418}
419
Mickaël Salaün5f155c22017-02-10 00:21:42 +0100420int bpf_map_get_next_key(int fd, const void *key, void *next_key)
Wang Nan9742da02016-11-26 07:03:25 +0000421{
422 union bpf_attr attr;
423
Andrii Nakryiko1ad9cbb2019-02-13 10:25:53 -0800424 memset(&attr, 0, sizeof(attr));
Wang Nan9742da02016-11-26 07:03:25 +0000425 attr.map_fd = fd;
426 attr.key = ptr_to_u64(key);
427 attr.next_key = ptr_to_u64(next_key);
428
429 return sys_bpf(BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
430}
431
432int bpf_obj_pin(int fd, const char *pathname)
433{
434 union bpf_attr attr;
435
Andrii Nakryiko1ad9cbb2019-02-13 10:25:53 -0800436 memset(&attr, 0, sizeof(attr));
Wang Nan9742da02016-11-26 07:03:25 +0000437 attr.pathname = ptr_to_u64((void *)pathname);
438 attr.bpf_fd = fd;
439
440 return sys_bpf(BPF_OBJ_PIN, &attr, sizeof(attr));
441}
442
443int bpf_obj_get(const char *pathname)
444{
445 union bpf_attr attr;
446
Andrii Nakryiko1ad9cbb2019-02-13 10:25:53 -0800447 memset(&attr, 0, sizeof(attr));
Wang Nan9742da02016-11-26 07:03:25 +0000448 attr.pathname = ptr_to_u64((void *)pathname);
449
450 return sys_bpf(BPF_OBJ_GET, &attr, sizeof(attr));
451}
Joe Stringer5dc880d2016-12-14 14:05:26 -0800452
John Fastabend464bc0f2017-08-28 07:10:04 -0700453int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type,
454 unsigned int flags)
Joe Stringer5dc880d2016-12-14 14:05:26 -0800455{
456 union bpf_attr attr;
457
Andrii Nakryiko1ad9cbb2019-02-13 10:25:53 -0800458 memset(&attr, 0, sizeof(attr));
Joe Stringer5dc880d2016-12-14 14:05:26 -0800459 attr.target_fd = target_fd;
John Fastabend464bc0f2017-08-28 07:10:04 -0700460 attr.attach_bpf_fd = prog_fd;
Joe Stringer5dc880d2016-12-14 14:05:26 -0800461 attr.attach_type = type;
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800462 attr.attach_flags = flags;
Joe Stringer5dc880d2016-12-14 14:05:26 -0800463
464 return sys_bpf(BPF_PROG_ATTACH, &attr, sizeof(attr));
465}
466
467int bpf_prog_detach(int target_fd, enum bpf_attach_type type)
468{
469 union bpf_attr attr;
470
Andrii Nakryiko1ad9cbb2019-02-13 10:25:53 -0800471 memset(&attr, 0, sizeof(attr));
Joe Stringer5dc880d2016-12-14 14:05:26 -0800472 attr.target_fd = target_fd;
473 attr.attach_type = type;
474
475 return sys_bpf(BPF_PROG_DETACH, &attr, sizeof(attr));
476}
Alexei Starovoitov30848872017-03-30 21:45:39 -0700477
Alexei Starovoitov244d20e2017-10-02 22:50:24 -0700478int bpf_prog_detach2(int prog_fd, int target_fd, enum bpf_attach_type type)
479{
480 union bpf_attr attr;
481
Andrii Nakryiko1ad9cbb2019-02-13 10:25:53 -0800482 memset(&attr, 0, sizeof(attr));
Alexei Starovoitov244d20e2017-10-02 22:50:24 -0700483 attr.target_fd = target_fd;
484 attr.attach_bpf_fd = prog_fd;
485 attr.attach_type = type;
486
487 return sys_bpf(BPF_PROG_DETACH, &attr, sizeof(attr));
488}
489
Alexei Starovoitov5d0cbf92017-10-02 22:50:27 -0700490int bpf_prog_query(int target_fd, enum bpf_attach_type type, __u32 query_flags,
491 __u32 *attach_flags, __u32 *prog_ids, __u32 *prog_cnt)
492{
493 union bpf_attr attr;
494 int ret;
495
Andrii Nakryiko1ad9cbb2019-02-13 10:25:53 -0800496 memset(&attr, 0, sizeof(attr));
Alexei Starovoitov5d0cbf92017-10-02 22:50:27 -0700497 attr.query.target_fd = target_fd;
498 attr.query.attach_type = type;
499 attr.query.query_flags = query_flags;
500 attr.query.prog_cnt = *prog_cnt;
501 attr.query.prog_ids = ptr_to_u64(prog_ids);
502
503 ret = sys_bpf(BPF_PROG_QUERY, &attr, sizeof(attr));
504 if (attach_flags)
505 *attach_flags = attr.query.attach_flags;
506 *prog_cnt = attr.query.prog_cnt;
507 return ret;
508}
509
Alexei Starovoitov30848872017-03-30 21:45:39 -0700510int bpf_prog_test_run(int prog_fd, int repeat, void *data, __u32 size,
511 void *data_out, __u32 *size_out, __u32 *retval,
512 __u32 *duration)
513{
514 union bpf_attr attr;
515 int ret;
516
Andrii Nakryiko1ad9cbb2019-02-13 10:25:53 -0800517 memset(&attr, 0, sizeof(attr));
Alexei Starovoitov30848872017-03-30 21:45:39 -0700518 attr.test.prog_fd = prog_fd;
519 attr.test.data_in = ptr_to_u64(data);
520 attr.test.data_out = ptr_to_u64(data_out);
521 attr.test.data_size_in = size;
522 attr.test.repeat = repeat;
523
524 ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, sizeof(attr));
525 if (size_out)
526 *size_out = attr.test.data_size_out;
527 if (retval)
528 *retval = attr.test.retval;
529 if (duration)
530 *duration = attr.test.duration;
531 return ret;
532}
Martin KaFai Lau95b9afd2017-06-05 12:15:53 -0700533
Lorenz Bauer64a97592018-12-03 11:31:25 +0000534int bpf_prog_test_run_xattr(struct bpf_prog_test_run_attr *test_attr)
535{
536 union bpf_attr attr;
537 int ret;
538
539 if (!test_attr->data_out && test_attr->data_size_out > 0)
540 return -EINVAL;
541
Andrii Nakryiko1ad9cbb2019-02-13 10:25:53 -0800542 memset(&attr, 0, sizeof(attr));
Lorenz Bauer64a97592018-12-03 11:31:25 +0000543 attr.test.prog_fd = test_attr->prog_fd;
544 attr.test.data_in = ptr_to_u64(test_attr->data_in);
545 attr.test.data_out = ptr_to_u64(test_attr->data_out);
546 attr.test.data_size_in = test_attr->data_size_in;
547 attr.test.data_size_out = test_attr->data_size_out;
548 attr.test.repeat = test_attr->repeat;
549
550 ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, sizeof(attr));
551 test_attr->data_size_out = attr.test.data_size_out;
552 test_attr->retval = attr.test.retval;
553 test_attr->duration = attr.test.duration;
554 return ret;
555}
556
Martin KaFai Lau95b9afd2017-06-05 12:15:53 -0700557int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id)
558{
559 union bpf_attr attr;
560 int err;
561
Andrii Nakryiko1ad9cbb2019-02-13 10:25:53 -0800562 memset(&attr, 0, sizeof(attr));
Martin KaFai Lau95b9afd2017-06-05 12:15:53 -0700563 attr.start_id = start_id;
564
565 err = sys_bpf(BPF_PROG_GET_NEXT_ID, &attr, sizeof(attr));
566 if (!err)
567 *next_id = attr.next_id;
568
569 return err;
570}
571
572int bpf_map_get_next_id(__u32 start_id, __u32 *next_id)
573{
574 union bpf_attr attr;
575 int err;
576
Andrii Nakryiko1ad9cbb2019-02-13 10:25:53 -0800577 memset(&attr, 0, sizeof(attr));
Martin KaFai Lau95b9afd2017-06-05 12:15:53 -0700578 attr.start_id = start_id;
579
580 err = sys_bpf(BPF_MAP_GET_NEXT_ID, &attr, sizeof(attr));
581 if (!err)
582 *next_id = attr.next_id;
583
584 return err;
585}
586
587int bpf_prog_get_fd_by_id(__u32 id)
588{
589 union bpf_attr attr;
590
Andrii Nakryiko1ad9cbb2019-02-13 10:25:53 -0800591 memset(&attr, 0, sizeof(attr));
Martin KaFai Lau95b9afd2017-06-05 12:15:53 -0700592 attr.prog_id = id;
593
594 return sys_bpf(BPF_PROG_GET_FD_BY_ID, &attr, sizeof(attr));
595}
596
597int bpf_map_get_fd_by_id(__u32 id)
598{
599 union bpf_attr attr;
600
Andrii Nakryiko1ad9cbb2019-02-13 10:25:53 -0800601 memset(&attr, 0, sizeof(attr));
Martin KaFai Lau95b9afd2017-06-05 12:15:53 -0700602 attr.map_id = id;
603
604 return sys_bpf(BPF_MAP_GET_FD_BY_ID, &attr, sizeof(attr));
605}
606
Martin KaFai Laucd8b8922018-05-04 14:49:55 -0700607int bpf_btf_get_fd_by_id(__u32 id)
608{
609 union bpf_attr attr;
610
Andrii Nakryiko1ad9cbb2019-02-13 10:25:53 -0800611 memset(&attr, 0, sizeof(attr));
Martin KaFai Laucd8b8922018-05-04 14:49:55 -0700612 attr.btf_id = id;
613
614 return sys_bpf(BPF_BTF_GET_FD_BY_ID, &attr, sizeof(attr));
615}
616
Martin KaFai Lau95b9afd2017-06-05 12:15:53 -0700617int bpf_obj_get_info_by_fd(int prog_fd, void *info, __u32 *info_len)
618{
619 union bpf_attr attr;
620 int err;
621
Andrii Nakryiko1ad9cbb2019-02-13 10:25:53 -0800622 memset(&attr, 0, sizeof(attr));
Martin KaFai Lau95b9afd2017-06-05 12:15:53 -0700623 attr.info.bpf_fd = prog_fd;
624 attr.info.info_len = *info_len;
625 attr.info.info = ptr_to_u64(info);
626
627 err = sys_bpf(BPF_OBJ_GET_INFO_BY_FD, &attr, sizeof(attr));
628 if (!err)
629 *info_len = attr.info.info_len;
630
631 return err;
632}
Eric Leblond949abbe2018-01-30 21:55:01 +0100633
Alexei Starovoitova0fe3e52018-03-28 12:05:38 -0700634int bpf_raw_tracepoint_open(const char *name, int prog_fd)
635{
636 union bpf_attr attr;
637
Andrii Nakryiko1ad9cbb2019-02-13 10:25:53 -0800638 memset(&attr, 0, sizeof(attr));
Alexei Starovoitova0fe3e52018-03-28 12:05:38 -0700639 attr.raw_tracepoint.name = ptr_to_u64(name);
640 attr.raw_tracepoint.prog_fd = prog_fd;
641
642 return sys_bpf(BPF_RAW_TRACEPOINT_OPEN, &attr, sizeof(attr));
643}
644
Martin KaFai Lau8a138ae2018-04-18 15:56:05 -0700645int bpf_load_btf(void *btf, __u32 btf_size, char *log_buf, __u32 log_buf_size,
646 bool do_log)
647{
648 union bpf_attr attr = {};
649 int fd;
650
651 attr.btf = ptr_to_u64(btf);
652 attr.btf_size = btf_size;
653
654retry:
655 if (do_log && log_buf && log_buf_size) {
656 attr.btf_log_level = 1;
657 attr.btf_log_size = log_buf_size;
658 attr.btf_log_buf = ptr_to_u64(log_buf);
659 }
660
661 fd = sys_bpf(BPF_BTF_LOAD, &attr, sizeof(attr));
662 if (fd == -1 && !do_log && log_buf && log_buf_size) {
663 do_log = true;
664 goto retry;
665 }
666
667 return fd;
668}
Yonghong Song30687ad2018-05-24 11:21:10 -0700669
670int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, __u32 *buf_len,
671 __u32 *prog_id, __u32 *fd_type, __u64 *probe_offset,
672 __u64 *probe_addr)
673{
674 union bpf_attr attr = {};
675 int err;
676
677 attr.task_fd_query.pid = pid;
678 attr.task_fd_query.fd = fd;
679 attr.task_fd_query.flags = flags;
680 attr.task_fd_query.buf = ptr_to_u64(buf);
681 attr.task_fd_query.buf_len = *buf_len;
682
683 err = sys_bpf(BPF_TASK_FD_QUERY, &attr, sizeof(attr));
684 *buf_len = attr.task_fd_query.buf_len;
685 *prog_id = attr.task_fd_query.prog_id;
686 *fd_type = attr.task_fd_query.fd_type;
687 *probe_offset = attr.task_fd_query.probe_offset;
688 *probe_addr = attr.task_fd_query.probe_addr;
689
690 return err;
691}