Revert BPF token-related functionality
[linux-2.6-block.git] / tools / lib / bpf / bpf.c
CommitLineData
1bc38b8f 1// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
6061a3d6 2
e3ed2fef
WN
3/*
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.
203d1cac
WN
9 *
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>
e3ed2fef
WN
22 */
23
24#include <stdlib.h>
1ad9cbb8 25#include <string.h>
e3ed2fef
WN
26#include <memory.h>
27#include <unistd.h>
28#include <asm/unistd.h>
d7fe74f9 29#include <errno.h>
e3ed2fef 30#include <linux/bpf.h>
e542f2c4 31#include <linux/filter.h>
04b6de64 32#include <linux/kernel.h>
d10ef2b8 33#include <limits.h>
e542f2c4 34#include <sys/resource.h>
e3ed2fef 35#include "bpf.h"
949abbe8 36#include "libbpf.h"
d7fe74f9 37#include "libbpf_internal.h"
e3ed2fef
WN
38
39/*
03671057 40 * When building perf, unistd.h is overridden. __NR_bpf is
8f9e05fb 41 * required to be defined explicitly.
e3ed2fef
WN
42 */
43#ifndef __NR_bpf
44# if defined(__i386__)
45# define __NR_bpf 357
46# elif defined(__x86_64__)
47# define __NR_bpf 321
48# elif defined(__aarch64__)
49# define __NR_bpf 280
b0c47807
DM
50# elif defined(__sparc__)
51# define __NR_bpf 349
bad1926d
DB
52# elif defined(__s390__)
53# define __NR_bpf 351
ca31ca82
VG
54# elif defined(__arc__)
55# define __NR_bpf 280
e32cb12f
TY
56# elif defined(__mips__) && defined(_ABIO32)
57# define __NR_bpf 4355
58# elif defined(__mips__) && defined(_ABIN32)
59# define __NR_bpf 6319
60# elif defined(__mips__) && defined(_ABI64)
61# define __NR_bpf 5315
e3ed2fef
WN
62# else
63# error __NR_bpf not defined. libbpf does not support your arch.
64# endif
65#endif
66
cdc6a4ba 67static inline __u64 ptr_to_u64(const void *ptr)
7bf98369
WN
68{
69 return (__u64) (unsigned long) ptr;
70}
71
cdc6a4ba
MS
72static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
73 unsigned int size)
e3ed2fef
WN
74{
75 return syscall(__NR_bpf, cmd, attr, size);
76}
77
549a6323
KKD
78static inline int sys_bpf_fd(enum bpf_cmd cmd, union bpf_attr *attr,
79 unsigned int size)
80{
81 int fd;
82
83 fd = sys_bpf(cmd, attr, size);
84 return ensure_good_fd(fd);
85}
86
1f235777 87int sys_bpf_prog_load(union bpf_attr *attr, unsigned int size, int attempts)
86edaed3
LB
88{
89 int fd;
90
91 do {
549a6323 92 fd = sys_bpf_fd(BPF_PROG_LOAD, attr, size);
45493cba 93 } while (fd < 0 && errno == EAGAIN && --attempts > 0);
86edaed3
LB
94
95 return fd;
96}
97
e542f2c4
AN
98/* Probe whether kernel switched from memlock-based (RLIMIT_MEMLOCK) to
99 * memcg-based memory accounting for BPF maps and progs. This was done in [0].
100 * We use the support for bpf_ktime_get_coarse_ns() helper, which was added in
101 * the same 5.11 Linux release ([1]), to detect memcg-based accounting for BPF.
102 *
103 * [0] https://lore.kernel.org/bpf/20201201215900.3569844-1-guro@fb.com/
104 * [1] d05512618056 ("bpf: Add bpf_ktime_get_coarse_ns helper")
105 */
d17aff80 106int probe_memcg_account(void)
e542f2c4 107{
813847a3 108 const size_t attr_sz = offsetofend(union bpf_attr, attach_btf_obj_fd);
e542f2c4
AN
109 struct bpf_insn insns[] = {
110 BPF_EMIT_CALL(BPF_FUNC_ktime_get_coarse_ns),
111 BPF_EXIT_INSN(),
112 };
04b6de64 113 size_t insn_cnt = ARRAY_SIZE(insns);
e542f2c4
AN
114 union bpf_attr attr;
115 int prog_fd;
116
117 /* attempt loading freplace trying to use custom BTF */
813847a3 118 memset(&attr, 0, attr_sz);
e542f2c4
AN
119 attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
120 attr.insns = ptr_to_u64(insns);
121 attr.insn_cnt = insn_cnt;
122 attr.license = ptr_to_u64("GPL");
123
813847a3 124 prog_fd = sys_bpf_fd(BPF_PROG_LOAD, &attr, attr_sz);
e542f2c4
AN
125 if (prog_fd >= 0) {
126 close(prog_fd);
127 return 1;
128 }
129 return 0;
130}
131
132static bool memlock_bumped;
133static rlim_t memlock_rlim = RLIM_INFINITY;
134
135int libbpf_set_memlock_rlim(size_t memlock_bytes)
136{
137 if (memlock_bumped)
138 return libbpf_err(-EBUSY);
139
140 memlock_rlim = memlock_bytes;
141 return 0;
142}
143
144int bump_rlimit_memlock(void)
145{
146 struct rlimit rlim;
147
e542f2c4 148 /* if kernel supports memcg-based accounting, skip bumping RLIMIT_MEMLOCK */
d17aff80 149 if (memlock_bumped || kernel_supports(NULL, FEAT_MEMCG_ACCOUNT))
e542f2c4
AN
150 return 0;
151
152 memlock_bumped = true;
153
154 /* zero memlock_rlim_max disables auto-bumping RLIMIT_MEMLOCK */
155 if (memlock_rlim == 0)
156 return 0;
157
158 rlim.rlim_cur = rlim.rlim_max = memlock_rlim;
159 if (setrlimit(RLIMIT_MEMLOCK, &rlim))
160 return -errno;
161
162 return 0;
163}
164
992c4225
AN
165int bpf_map_create(enum bpf_map_type map_type,
166 const char *map_name,
167 __u32 key_size,
168 __u32 value_size,
169 __u32 max_entries,
170 const struct bpf_map_create_opts *opts)
e3ed2fef 171{
d17aff80 172 const size_t attr_sz = offsetofend(union bpf_attr, map_extra);
e3ed2fef 173 union bpf_attr attr;
f12b6543 174 int fd;
e3ed2fef 175
e542f2c4
AN
176 bump_rlimit_memlock();
177
992c4225
AN
178 memset(&attr, 0, attr_sz);
179
180 if (!OPTS_VALID(opts, bpf_map_create_opts))
181 return libbpf_err(-EINVAL);
182
183 attr.map_type = map_type;
d17aff80 184 if (map_name && kernel_supports(NULL, FEAT_PROG_NAME))
9fc205b4 185 libbpf_strlcpy(attr.map_name, map_name, sizeof(attr.map_name));
992c4225
AN
186 attr.key_size = key_size;
187 attr.value_size = value_size;
188 attr.max_entries = max_entries;
189
190 attr.btf_fd = OPTS_GET(opts, btf_fd, 0);
191 attr.btf_key_type_id = OPTS_GET(opts, btf_key_type_id, 0);
192 attr.btf_value_type_id = OPTS_GET(opts, btf_value_type_id, 0);
193 attr.btf_vmlinux_value_type_id = OPTS_GET(opts, btf_vmlinux_value_type_id, 0);
194
195 attr.inner_map_fd = OPTS_GET(opts, inner_map_fd, 0);
196 attr.map_flags = OPTS_GET(opts, map_flags, 0);
197 attr.map_extra = OPTS_GET(opts, map_extra, 0);
198 attr.numa_node = OPTS_GET(opts, numa_node, 0);
199 attr.map_ifindex = OPTS_GET(opts, map_ifindex, 0);
8a138aed 200
992c4225 201 fd = sys_bpf_fd(BPF_MAP_CREATE, &attr, attr_sz);
f12b6543 202 return libbpf_err_errno(fd);
8a138aed 203}
88cda1c9 204
3d650141
MKL
205static void *
206alloc_zero_tailing_info(const void *orecord, __u32 cnt,
207 __u32 actual_rec_size, __u32 expected_rec_size)
208{
4ee11356 209 __u64 info_len = (__u64)actual_rec_size * cnt;
3d650141
MKL
210 void *info, *nrecord;
211 int i;
212
213 info = malloc(info_len);
214 if (!info)
215 return NULL;
216
217 /* zero out bytes kernel does not understand */
218 nrecord = info;
219 for (i = 0; i < cnt; i++) {
220 memcpy(nrecord, orecord, expected_rec_size);
221 memset(nrecord + expected_rec_size, 0,
222 actual_rec_size - expected_rec_size);
223 orecord += actual_rec_size;
224 nrecord += actual_rec_size;
225 }
226
227 return info;
228}
229
765a3413
AN
230int bpf_prog_load(enum bpf_prog_type prog_type,
231 const char *prog_name, const char *license,
232 const struct bpf_insn *insns, size_t insn_cnt,
94e55c0f 233 struct bpf_prog_load_opts *opts)
7bf98369 234{
d17aff80 235 const size_t attr_sz = offsetofend(union bpf_attr, log_true_size);
3d650141 236 void *finfo = NULL, *linfo = NULL;
d10ef2b8
AN
237 const char *func_info, *line_info;
238 __u32 log_size, log_level, attach_prog_fd, attach_btf_obj_fd;
239 __u32 func_info_rec_size, line_info_rec_size;
240 int fd, attempts;
7bf98369 241 union bpf_attr attr;
d10ef2b8 242 char *log_buf;
d7be143b 243
e542f2c4
AN
244 bump_rlimit_memlock();
245
d10ef2b8 246 if (!OPTS_VALID(opts, bpf_prog_load_opts))
f12b6543 247 return libbpf_err(-EINVAL);
a4021a35 248
d10ef2b8
AN
249 attempts = OPTS_GET(opts, attempts, 0);
250 if (attempts < 0)
f12b6543 251 return libbpf_err(-EINVAL);
d10ef2b8
AN
252 if (attempts == 0)
253 attempts = PROG_LOAD_ATTEMPTS;
d7be143b 254
813847a3 255 memset(&attr, 0, attr_sz);
6aef10a4 256
d10ef2b8
AN
257 attr.prog_type = prog_type;
258 attr.expected_attach_type = OPTS_GET(opts, expected_attach_type, 0);
6aef10a4 259
d10ef2b8
AN
260 attr.prog_btf_fd = OPTS_GET(opts, prog_btf_fd, 0);
261 attr.prog_flags = OPTS_GET(opts, prog_flags, 0);
262 attr.prog_ifindex = OPTS_GET(opts, prog_ifindex, 0);
263 attr.kern_version = OPTS_GET(opts, kern_version, 0);
6aef10a4 264
d17aff80 265 if (prog_name && kernel_supports(NULL, FEAT_PROG_NAME))
9fc205b4 266 libbpf_strlcpy(attr.prog_name, prog_name, sizeof(attr.prog_name));
d10ef2b8 267 attr.license = ptr_to_u64(license);
a4021a35 268
d10ef2b8
AN
269 if (insn_cnt > UINT_MAX)
270 return libbpf_err(-E2BIG);
271
272 attr.insns = ptr_to_u64(insns);
273 attr.insn_cnt = (__u32)insn_cnt;
274
275 attach_prog_fd = OPTS_GET(opts, attach_prog_fd, 0);
276 attach_btf_obj_fd = OPTS_GET(opts, attach_btf_obj_fd, 0);
277
278 if (attach_prog_fd && attach_btf_obj_fd)
279 return libbpf_err(-EINVAL);
280
281 attr.attach_btf_id = OPTS_GET(opts, attach_btf_id, 0);
282 if (attach_prog_fd)
283 attr.attach_prog_fd = attach_prog_fd;
284 else
285 attr.attach_btf_obj_fd = attach_btf_obj_fd;
a4021a35 286
d10ef2b8
AN
287 log_buf = OPTS_GET(opts, log_buf, NULL);
288 log_size = OPTS_GET(opts, log_size, 0);
289 log_level = OPTS_GET(opts, log_level, 0);
6aef10a4 290
d10ef2b8
AN
291 if (!!log_buf != !!log_size)
292 return libbpf_err(-EINVAL);
6aef10a4 293
d10ef2b8
AN
294 func_info_rec_size = OPTS_GET(opts, func_info_rec_size, 0);
295 func_info = OPTS_GET(opts, func_info, NULL);
296 attr.func_info_rec_size = func_info_rec_size;
297 attr.func_info = ptr_to_u64(func_info);
298 attr.func_info_cnt = OPTS_GET(opts, func_info_cnt, 0);
7bf98369 299
d10ef2b8
AN
300 line_info_rec_size = OPTS_GET(opts, line_info_rec_size, 0);
301 line_info = OPTS_GET(opts, line_info, NULL);
302 attr.line_info_rec_size = line_info_rec_size;
303 attr.line_info = ptr_to_u64(line_info);
304 attr.line_info_cnt = OPTS_GET(opts, line_info_cnt, 0);
6aef10a4 305
d10ef2b8 306 attr.fd_array = ptr_to_u64(OPTS_GET(opts, fd_array, NULL));
7bf98369 307
4cf23a3c
AN
308 if (log_level) {
309 attr.log_buf = ptr_to_u64(log_buf);
310 attr.log_size = log_size;
311 attr.log_level = log_level;
312 }
313
813847a3 314 fd = sys_bpf_prog_load(&attr, attr_sz, attempts);
94e55c0f 315 OPTS_SET(opts, log_true_size, attr.log_true_size);
f0187f0b 316 if (fd >= 0)
7bf98369
WN
317 return fd;
318
2993e051
YS
319 /* After bpf_prog_load, the kernel may modify certain attributes
320 * to give user space a hint how to deal with loading failure.
321 * Check to see whether we can make some changes and load again.
322 */
3d650141
MKL
323 while (errno == E2BIG && (!finfo || !linfo)) {
324 if (!finfo && attr.func_info_cnt &&
d10ef2b8 325 attr.func_info_rec_size < func_info_rec_size) {
3d650141 326 /* try with corrected func info records */
d10ef2b8
AN
327 finfo = alloc_zero_tailing_info(func_info,
328 attr.func_info_cnt,
329 func_info_rec_size,
3d650141 330 attr.func_info_rec_size);
f12b6543
AN
331 if (!finfo) {
332 errno = E2BIG;
3d650141 333 goto done;
f12b6543 334 }
3d650141
MKL
335
336 attr.func_info = ptr_to_u64(finfo);
d10ef2b8 337 attr.func_info_rec_size = func_info_rec_size;
3d650141 338 } else if (!linfo && attr.line_info_cnt &&
d10ef2b8
AN
339 attr.line_info_rec_size < line_info_rec_size) {
340 linfo = alloc_zero_tailing_info(line_info,
341 attr.line_info_cnt,
342 line_info_rec_size,
3d650141 343 attr.line_info_rec_size);
f12b6543
AN
344 if (!linfo) {
345 errno = E2BIG;
3d650141 346 goto done;
f12b6543 347 }
3d650141
MKL
348
349 attr.line_info = ptr_to_u64(linfo);
d10ef2b8 350 attr.line_info_rec_size = line_info_rec_size;
3d650141
MKL
351 } else {
352 break;
2993e051
YS
353 }
354
813847a3 355 fd = sys_bpf_prog_load(&attr, attr_sz, attempts);
94e55c0f 356 OPTS_SET(opts, log_true_size, attr.log_true_size);
f0187f0b 357 if (fd >= 0)
2993e051
YS
358 goto done;
359 }
360
4cf23a3c
AN
361 if (log_level == 0 && log_buf) {
362 /* log_level == 0 with non-NULL log_buf requires retrying on error
363 * with log_level == 1 and log_buf/log_buf_size set, to get details of
364 * failure
365 */
366 attr.log_buf = ptr_to_u64(log_buf);
367 attr.log_size = log_size;
368 attr.log_level = 1;
6aef10a4 369
813847a3 370 fd = sys_bpf_prog_load(&attr, attr_sz, attempts);
94e55c0f 371 OPTS_SET(opts, log_true_size, attr.log_true_size);
4cf23a3c 372 }
2993e051 373done:
f12b6543 374 /* free() doesn't affect errno, so we don't need to restore it */
2993e051 375 free(finfo);
3d650141 376 free(linfo);
f12b6543 377 return libbpf_err_errno(fd);
7bf98369 378}
43798bf3 379
10ecc728 380int bpf_map_update_elem(int fd, const void *key, const void *value,
83d994d0 381 __u64 flags)
43798bf3 382{
813847a3 383 const size_t attr_sz = offsetofend(union bpf_attr, flags);
43798bf3 384 union bpf_attr attr;
f12b6543 385 int ret;
43798bf3 386
813847a3 387 memset(&attr, 0, attr_sz);
43798bf3
HK
388 attr.map_fd = fd;
389 attr.key = ptr_to_u64(key);
390 attr.value = ptr_to_u64(value);
391 attr.flags = flags;
392
813847a3 393 ret = sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, attr_sz);
f12b6543 394 return libbpf_err_errno(ret);
43798bf3 395}
9742da01 396
e5ff7c40 397int bpf_map_lookup_elem(int fd, const void *key, void *value)
9742da01 398{
813847a3 399 const size_t attr_sz = offsetofend(union bpf_attr, flags);
9742da01 400 union bpf_attr attr;
f12b6543 401 int ret;
9742da01 402
813847a3 403 memset(&attr, 0, attr_sz);
9742da01
WN
404 attr.map_fd = fd;
405 attr.key = ptr_to_u64(key);
406 attr.value = ptr_to_u64(value);
407
813847a3 408 ret = sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, attr_sz);
f12b6543 409 return libbpf_err_errno(ret);
9742da01
WN
410}
411
df5d22fa
AS
412int bpf_map_lookup_elem_flags(int fd, const void *key, void *value, __u64 flags)
413{
813847a3 414 const size_t attr_sz = offsetofend(union bpf_attr, flags);
df5d22fa 415 union bpf_attr attr;
f12b6543 416 int ret;
df5d22fa 417
813847a3 418 memset(&attr, 0, attr_sz);
df5d22fa
AS
419 attr.map_fd = fd;
420 attr.key = ptr_to_u64(key);
421 attr.value = ptr_to_u64(value);
422 attr.flags = flags;
423
813847a3 424 ret = sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, attr_sz);
f12b6543 425 return libbpf_err_errno(ret);
df5d22fa
AS
426}
427
43b987d2
MV
428int bpf_map_lookup_and_delete_elem(int fd, const void *key, void *value)
429{
813847a3 430 const size_t attr_sz = offsetofend(union bpf_attr, flags);
43b987d2 431 union bpf_attr attr;
f12b6543 432 int ret;
43b987d2 433
813847a3 434 memset(&attr, 0, attr_sz);
43b987d2
MV
435 attr.map_fd = fd;
436 attr.key = ptr_to_u64(key);
437 attr.value = ptr_to_u64(value);
438
813847a3 439 ret = sys_bpf(BPF_MAP_LOOKUP_AND_DELETE_ELEM, &attr, attr_sz);
f12b6543 440 return libbpf_err_errno(ret);
43b987d2
MV
441}
442
d59b9f2d
DS
443int bpf_map_lookup_and_delete_elem_flags(int fd, const void *key, void *value, __u64 flags)
444{
813847a3 445 const size_t attr_sz = offsetofend(union bpf_attr, flags);
d59b9f2d 446 union bpf_attr attr;
64165ddf 447 int ret;
d59b9f2d 448
813847a3 449 memset(&attr, 0, attr_sz);
d59b9f2d
DS
450 attr.map_fd = fd;
451 attr.key = ptr_to_u64(key);
452 attr.value = ptr_to_u64(value);
453 attr.flags = flags;
454
813847a3 455 ret = sys_bpf(BPF_MAP_LOOKUP_AND_DELETE_ELEM, &attr, attr_sz);
64165ddf 456 return libbpf_err_errno(ret);
d59b9f2d
DS
457}
458
e58383b8 459int bpf_map_delete_elem(int fd, const void *key)
9742da01 460{
813847a3 461 const size_t attr_sz = offsetofend(union bpf_attr, flags);
9742da01 462 union bpf_attr attr;
f12b6543 463 int ret;
9742da01 464
813847a3 465 memset(&attr, 0, attr_sz);
9742da01
WN
466 attr.map_fd = fd;
467 attr.key = ptr_to_u64(key);
468
813847a3 469 ret = sys_bpf(BPF_MAP_DELETE_ELEM, &attr, attr_sz);
f12b6543 470 return libbpf_err_errno(ret);
9742da01
WN
471}
472
737d0646
AN
473int bpf_map_delete_elem_flags(int fd, const void *key, __u64 flags)
474{
813847a3 475 const size_t attr_sz = offsetofend(union bpf_attr, flags);
737d0646
AN
476 union bpf_attr attr;
477 int ret;
478
813847a3 479 memset(&attr, 0, attr_sz);
737d0646
AN
480 attr.map_fd = fd;
481 attr.key = ptr_to_u64(key);
482 attr.flags = flags;
483
813847a3 484 ret = sys_bpf(BPF_MAP_DELETE_ELEM, &attr, attr_sz);
737d0646
AN
485 return libbpf_err_errno(ret);
486}
487
5f155c25 488int bpf_map_get_next_key(int fd, const void *key, void *next_key)
9742da01 489{
813847a3 490 const size_t attr_sz = offsetofend(union bpf_attr, next_key);
9742da01 491 union bpf_attr attr;
f12b6543 492 int ret;
9742da01 493
813847a3 494 memset(&attr, 0, attr_sz);
9742da01
WN
495 attr.map_fd = fd;
496 attr.key = ptr_to_u64(key);
497 attr.next_key = ptr_to_u64(next_key);
498
813847a3 499 ret = sys_bpf(BPF_MAP_GET_NEXT_KEY, &attr, attr_sz);
f12b6543 500 return libbpf_err_errno(ret);
9742da01
WN
501}
502
d859900c
DB
503int bpf_map_freeze(int fd)
504{
813847a3 505 const size_t attr_sz = offsetofend(union bpf_attr, map_fd);
d859900c 506 union bpf_attr attr;
f12b6543 507 int ret;
d859900c 508
813847a3 509 memset(&attr, 0, attr_sz);
d859900c
DB
510 attr.map_fd = fd;
511
813847a3 512 ret = sys_bpf(BPF_MAP_FREEZE, &attr, attr_sz);
f12b6543 513 return libbpf_err_errno(ret);
d859900c
DB
514}
515
2ab3d86e
YS
516static int bpf_map_batch_common(int cmd, int fd, void *in_batch,
517 void *out_batch, void *keys, void *values,
518 __u32 *count,
519 const struct bpf_map_batch_opts *opts)
520{
813847a3 521 const size_t attr_sz = offsetofend(union bpf_attr, batch);
858e284f 522 union bpf_attr attr;
2ab3d86e
YS
523 int ret;
524
525 if (!OPTS_VALID(opts, bpf_map_batch_opts))
f12b6543 526 return libbpf_err(-EINVAL);
2ab3d86e 527
813847a3 528 memset(&attr, 0, attr_sz);
2ab3d86e
YS
529 attr.batch.map_fd = fd;
530 attr.batch.in_batch = ptr_to_u64(in_batch);
531 attr.batch.out_batch = ptr_to_u64(out_batch);
532 attr.batch.keys = ptr_to_u64(keys);
533 attr.batch.values = ptr_to_u64(values);
534 attr.batch.count = *count;
535 attr.batch.elem_flags = OPTS_GET(opts, elem_flags, 0);
536 attr.batch.flags = OPTS_GET(opts, flags, 0);
537
813847a3 538 ret = sys_bpf(cmd, &attr, attr_sz);
2ab3d86e
YS
539 *count = attr.batch.count;
540
f12b6543 541 return libbpf_err_errno(ret);
2ab3d86e
YS
542}
543
e59618f0 544int bpf_map_delete_batch(int fd, const void *keys, __u32 *count,
2ab3d86e
YS
545 const struct bpf_map_batch_opts *opts)
546{
547 return bpf_map_batch_common(BPF_MAP_DELETE_BATCH, fd, NULL,
e59618f0 548 NULL, (void *)keys, NULL, count, opts);
2ab3d86e
YS
549}
550
551int bpf_map_lookup_batch(int fd, void *in_batch, void *out_batch, void *keys,
552 void *values, __u32 *count,
553 const struct bpf_map_batch_opts *opts)
554{
555 return bpf_map_batch_common(BPF_MAP_LOOKUP_BATCH, fd, in_batch,
556 out_batch, keys, values, count, opts);
557}
558
559int bpf_map_lookup_and_delete_batch(int fd, void *in_batch, void *out_batch,
560 void *keys, void *values, __u32 *count,
561 const struct bpf_map_batch_opts *opts)
562{
563 return bpf_map_batch_common(BPF_MAP_LOOKUP_AND_DELETE_BATCH,
564 fd, in_batch, out_batch, keys, values,
565 count, opts);
566}
567
e59618f0 568int bpf_map_update_batch(int fd, const void *keys, const void *values, __u32 *count,
2ab3d86e
YS
569 const struct bpf_map_batch_opts *opts)
570{
571 return bpf_map_batch_common(BPF_MAP_UPDATE_BATCH, fd, NULL, NULL,
e59618f0 572 (void *)keys, (void *)values, count, opts);
2ab3d86e
YS
573}
574
f1674dc7 575int bpf_obj_pin_opts(int fd, const char *pathname, const struct bpf_obj_pin_opts *opts)
9742da01 576{
f1674dc7 577 const size_t attr_sz = offsetofend(union bpf_attr, path_fd);
9742da01 578 union bpf_attr attr;
f12b6543 579 int ret;
9742da01 580
f1674dc7
AN
581 if (!OPTS_VALID(opts, bpf_obj_pin_opts))
582 return libbpf_err(-EINVAL);
583
813847a3 584 memset(&attr, 0, attr_sz);
f1674dc7 585 attr.path_fd = OPTS_GET(opts, path_fd, 0);
9742da01 586 attr.pathname = ptr_to_u64((void *)pathname);
f1674dc7 587 attr.file_flags = OPTS_GET(opts, file_flags, 0);
9742da01
WN
588 attr.bpf_fd = fd;
589
813847a3 590 ret = sys_bpf(BPF_OBJ_PIN, &attr, attr_sz);
f12b6543 591 return libbpf_err_errno(ret);
9742da01
WN
592}
593
f1674dc7
AN
594int bpf_obj_pin(int fd, const char *pathname)
595{
596 return bpf_obj_pin_opts(fd, pathname, NULL);
597}
598
9742da01 599int bpf_obj_get(const char *pathname)
395fc4fa
JB
600{
601 return bpf_obj_get_opts(pathname, NULL);
602}
603
604int bpf_obj_get_opts(const char *pathname, const struct bpf_obj_get_opts *opts)
9742da01 605{
f1674dc7 606 const size_t attr_sz = offsetofend(union bpf_attr, path_fd);
9742da01 607 union bpf_attr attr;
f12b6543 608 int fd;
9742da01 609
395fc4fa
JB
610 if (!OPTS_VALID(opts, bpf_obj_get_opts))
611 return libbpf_err(-EINVAL);
612
813847a3 613 memset(&attr, 0, attr_sz);
f1674dc7 614 attr.path_fd = OPTS_GET(opts, path_fd, 0);
9742da01 615 attr.pathname = ptr_to_u64((void *)pathname);
395fc4fa 616 attr.file_flags = OPTS_GET(opts, file_flags, 0);
9742da01 617
813847a3 618 fd = sys_bpf_fd(BPF_OBJ_GET, &attr, attr_sz);
f12b6543 619 return libbpf_err_errno(fd);
9742da01 620}
5dc880de 621
464bc0fd
JF
622int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type,
623 unsigned int flags)
cdbee383
AI
624{
625 DECLARE_LIBBPF_OPTS(bpf_prog_attach_opts, opts,
626 .flags = flags,
627 );
628
d6c9c24e 629 return bpf_prog_attach_opts(prog_fd, target_fd, type, &opts);
cdbee383
AI
630}
631
fe20ce3a
DB
632int bpf_prog_attach_opts(int prog_fd, int target, enum bpf_attach_type type,
633 const struct bpf_prog_attach_opts *opts)
5dc880de 634{
fe20ce3a
DB
635 const size_t attr_sz = offsetofend(union bpf_attr, expected_revision);
636 __u32 relative_id, flags;
637 int ret, relative_fd;
5dc880de
JS
638 union bpf_attr attr;
639
cdbee383 640 if (!OPTS_VALID(opts, bpf_prog_attach_opts))
f12b6543 641 return libbpf_err(-EINVAL);
cdbee383 642
fe20ce3a
DB
643 relative_id = OPTS_GET(opts, relative_id, 0);
644 relative_fd = OPTS_GET(opts, relative_fd, 0);
645 flags = OPTS_GET(opts, flags, 0);
646
647 /* validate we don't have unexpected combinations of non-zero fields */
648 if (relative_fd && relative_id)
649 return libbpf_err(-EINVAL);
650
813847a3 651 memset(&attr, 0, attr_sz);
fe20ce3a
DB
652 attr.target_fd = target;
653 attr.attach_bpf_fd = prog_fd;
654 attr.attach_type = type;
655 attr.replace_bpf_fd = OPTS_GET(opts, replace_fd, 0);
656 attr.expected_revision = OPTS_GET(opts, expected_revision, 0);
657
658 if (relative_id) {
659 attr.attach_flags = flags | BPF_F_ID;
660 attr.relative_id = relative_id;
661 } else {
662 attr.attach_flags = flags;
663 attr.relative_fd = relative_fd;
664 }
5dc880de 665
813847a3 666 ret = sys_bpf(BPF_PROG_ATTACH, &attr, attr_sz);
f12b6543 667 return libbpf_err_errno(ret);
5dc880de
JS
668}
669
fe20ce3a
DB
670int bpf_prog_detach_opts(int prog_fd, int target, enum bpf_attach_type type,
671 const struct bpf_prog_detach_opts *opts)
5dc880de 672{
fe20ce3a
DB
673 const size_t attr_sz = offsetofend(union bpf_attr, expected_revision);
674 __u32 relative_id, flags;
675 int ret, relative_fd;
5dc880de 676 union bpf_attr attr;
fe20ce3a
DB
677
678 if (!OPTS_VALID(opts, bpf_prog_detach_opts))
679 return libbpf_err(-EINVAL);
680
681 relative_id = OPTS_GET(opts, relative_id, 0);
682 relative_fd = OPTS_GET(opts, relative_fd, 0);
683 flags = OPTS_GET(opts, flags, 0);
684
685 /* validate we don't have unexpected combinations of non-zero fields */
686 if (relative_fd && relative_id)
687 return libbpf_err(-EINVAL);
5dc880de 688
813847a3 689 memset(&attr, 0, attr_sz);
fe20ce3a
DB
690 attr.target_fd = target;
691 attr.attach_bpf_fd = prog_fd;
692 attr.attach_type = type;
693 attr.expected_revision = OPTS_GET(opts, expected_revision, 0);
694
695 if (relative_id) {
696 attr.attach_flags = flags | BPF_F_ID;
697 attr.relative_id = relative_id;
698 } else {
699 attr.attach_flags = flags;
700 attr.relative_fd = relative_fd;
701 }
5dc880de 702
813847a3 703 ret = sys_bpf(BPF_PROG_DETACH, &attr, attr_sz);
f12b6543 704 return libbpf_err_errno(ret);
5dc880de 705}
30848873 706
fe20ce3a 707int bpf_prog_detach(int target_fd, enum bpf_attach_type type)
244d20ef 708{
fe20ce3a
DB
709 return bpf_prog_detach_opts(0, target_fd, type, NULL);
710}
244d20ef 711
fe20ce3a
DB
712int bpf_prog_detach2(int prog_fd, int target_fd, enum bpf_attach_type type)
713{
714 return bpf_prog_detach_opts(prog_fd, target_fd, type, NULL);
244d20ef
AS
715}
716
cc4f864b
AN
717int bpf_link_create(int prog_fd, int target_fd,
718 enum bpf_attach_type attach_type,
719 const struct bpf_link_create_opts *opts)
720{
813847a3 721 const size_t attr_sz = offsetofend(union bpf_attr, link_create);
55cc3768
DB
722 __u32 target_btf_id, iter_info_len, relative_id;
723 int fd, err, relative_fd;
cc4f864b
AN
724 union bpf_attr attr;
725
726 if (!OPTS_VALID(opts, bpf_link_create_opts))
f12b6543 727 return libbpf_err(-EINVAL);
cc4f864b 728
a5359091
THJ
729 iter_info_len = OPTS_GET(opts, iter_info_len, 0);
730 target_btf_id = OPTS_GET(opts, target_btf_id, 0);
731
3ec84f4b
AN
732 /* validate we don't have unexpected combinations of non-zero fields */
733 if (iter_info_len || target_btf_id) {
734 if (iter_info_len && target_btf_id)
735 return libbpf_err(-EINVAL);
736 if (!OPTS_ZEROED(opts, target_btf_id))
737 return libbpf_err(-EINVAL);
738 }
a5359091 739
813847a3 740 memset(&attr, 0, attr_sz);
cc4f864b
AN
741 attr.link_create.prog_fd = prog_fd;
742 attr.link_create.target_fd = target_fd;
743 attr.link_create.attach_type = attach_type;
cd31039a 744 attr.link_create.flags = OPTS_GET(opts, flags, 0);
a5359091 745
3ec84f4b 746 if (target_btf_id) {
a5359091 747 attr.link_create.target_btf_id = target_btf_id;
3ec84f4b 748 goto proceed;
a5359091 749 }
cc4f864b 750
3ec84f4b
AN
751 switch (attach_type) {
752 case BPF_TRACE_ITER:
753 attr.link_create.iter_info = ptr_to_u64(OPTS_GET(opts, iter_info, (void *)0));
754 attr.link_create.iter_info_len = iter_info_len;
755 break;
756 case BPF_PERF_EVENT:
757 attr.link_create.perf_event.bpf_cookie = OPTS_GET(opts, perf_event.bpf_cookie, 0);
758 if (!OPTS_ZEROED(opts, perf_event))
759 return libbpf_err(-EINVAL);
5117c26e
JO
760 break;
761 case BPF_TRACE_KPROBE_MULTI:
762 attr.link_create.kprobe_multi.flags = OPTS_GET(opts, kprobe_multi.flags, 0);
763 attr.link_create.kprobe_multi.cnt = OPTS_GET(opts, kprobe_multi.cnt, 0);
764 attr.link_create.kprobe_multi.syms = ptr_to_u64(OPTS_GET(opts, kprobe_multi.syms, 0));
765 attr.link_create.kprobe_multi.addrs = ptr_to_u64(OPTS_GET(opts, kprobe_multi.addrs, 0));
766 attr.link_create.kprobe_multi.cookies = ptr_to_u64(OPTS_GET(opts, kprobe_multi.cookies, 0));
767 if (!OPTS_ZEROED(opts, kprobe_multi))
768 return libbpf_err(-EINVAL);
3ec84f4b 769 break;
5054a303
JO
770 case BPF_TRACE_UPROBE_MULTI:
771 attr.link_create.uprobe_multi.flags = OPTS_GET(opts, uprobe_multi.flags, 0);
772 attr.link_create.uprobe_multi.cnt = OPTS_GET(opts, uprobe_multi.cnt, 0);
773 attr.link_create.uprobe_multi.path = ptr_to_u64(OPTS_GET(opts, uprobe_multi.path, 0));
774 attr.link_create.uprobe_multi.offsets = ptr_to_u64(OPTS_GET(opts, uprobe_multi.offsets, 0));
775 attr.link_create.uprobe_multi.ref_ctr_offsets = ptr_to_u64(OPTS_GET(opts, uprobe_multi.ref_ctr_offsets, 0));
776 attr.link_create.uprobe_multi.cookies = ptr_to_u64(OPTS_GET(opts, uprobe_multi.cookies, 0));
777 attr.link_create.uprobe_multi.pid = OPTS_GET(opts, uprobe_multi.pid, 0);
778 if (!OPTS_ZEROED(opts, uprobe_multi))
779 return libbpf_err(-EINVAL);
780 break;
129b9c5e
KFL
781 case BPF_TRACE_FENTRY:
782 case BPF_TRACE_FEXIT:
783 case BPF_MODIFY_RETURN:
784 case BPF_LSM_MAC:
785 attr.link_create.tracing.cookie = OPTS_GET(opts, tracing.cookie, 0);
786 if (!OPTS_ZEROED(opts, tracing))
787 return libbpf_err(-EINVAL);
788 break;
52364abb
FW
789 case BPF_NETFILTER:
790 attr.link_create.netfilter.pf = OPTS_GET(opts, netfilter.pf, 0);
791 attr.link_create.netfilter.hooknum = OPTS_GET(opts, netfilter.hooknum, 0);
792 attr.link_create.netfilter.priority = OPTS_GET(opts, netfilter.priority, 0);
793 attr.link_create.netfilter.flags = OPTS_GET(opts, netfilter.flags, 0);
794 if (!OPTS_ZEROED(opts, netfilter))
795 return libbpf_err(-EINVAL);
796 break;
55cc3768
DB
797 case BPF_TCX_INGRESS:
798 case BPF_TCX_EGRESS:
799 relative_fd = OPTS_GET(opts, tcx.relative_fd, 0);
800 relative_id = OPTS_GET(opts, tcx.relative_id, 0);
801 if (relative_fd && relative_id)
802 return libbpf_err(-EINVAL);
803 if (relative_id) {
804 attr.link_create.tcx.relative_id = relative_id;
805 attr.link_create.flags |= BPF_F_ID;
806 } else {
807 attr.link_create.tcx.relative_fd = relative_fd;
808 }
809 attr.link_create.tcx.expected_revision = OPTS_GET(opts, tcx.expected_revision, 0);
810 if (!OPTS_ZEROED(opts, tcx))
811 return libbpf_err(-EINVAL);
812 break;
05c31b4a
DB
813 case BPF_NETKIT_PRIMARY:
814 case BPF_NETKIT_PEER:
815 relative_fd = OPTS_GET(opts, netkit.relative_fd, 0);
816 relative_id = OPTS_GET(opts, netkit.relative_id, 0);
817 if (relative_fd && relative_id)
818 return libbpf_err(-EINVAL);
819 if (relative_id) {
820 attr.link_create.netkit.relative_id = relative_id;
821 attr.link_create.flags |= BPF_F_ID;
822 } else {
823 attr.link_create.netkit.relative_fd = relative_fd;
824 }
825 attr.link_create.netkit.expected_revision = OPTS_GET(opts, netkit.expected_revision, 0);
826 if (!OPTS_ZEROED(opts, netkit))
827 return libbpf_err(-EINVAL);
828 break;
3ec84f4b
AN
829 default:
830 if (!OPTS_ZEROED(opts, flags))
831 return libbpf_err(-EINVAL);
832 break;
833 }
834proceed:
813847a3 835 fd = sys_bpf_fd(BPF_LINK_CREATE, &attr, attr_sz);
8462e0b4
AN
836 if (fd >= 0)
837 return fd;
838 /* we'll get EINVAL if LINK_CREATE doesn't support attaching fentry
839 * and other similar programs
840 */
841 err = -errno;
842 if (err != -EINVAL)
843 return libbpf_err(err);
844
845 /* if user used features not supported by
846 * BPF_RAW_TRACEPOINT_OPEN command, then just give up immediately
847 */
848 if (attr.link_create.target_fd || attr.link_create.target_btf_id)
849 return libbpf_err(err);
850 if (!OPTS_ZEROED(opts, sz))
851 return libbpf_err(err);
852
853 /* otherwise, for few select kinds of programs that can be
854 * attached using BPF_RAW_TRACEPOINT_OPEN command, try that as
855 * a fallback for older kernels
856 */
857 switch (attach_type) {
858 case BPF_TRACE_RAW_TP:
859 case BPF_LSM_MAC:
860 case BPF_TRACE_FENTRY:
861 case BPF_TRACE_FEXIT:
862 case BPF_MODIFY_RETURN:
863 return bpf_raw_tracepoint_open(NULL, prog_fd);
864 default:
865 return libbpf_err(err);
866 }
cc4f864b
AN
867}
868
2e49527e
AN
869int bpf_link_detach(int link_fd)
870{
813847a3 871 const size_t attr_sz = offsetofend(union bpf_attr, link_detach);
2e49527e 872 union bpf_attr attr;
f12b6543 873 int ret;
2e49527e 874
813847a3 875 memset(&attr, 0, attr_sz);
2e49527e
AN
876 attr.link_detach.link_fd = link_fd;
877
813847a3 878 ret = sys_bpf(BPF_LINK_DETACH, &attr, attr_sz);
f12b6543 879 return libbpf_err_errno(ret);
2e49527e
AN
880}
881
cc4f864b
AN
882int bpf_link_update(int link_fd, int new_prog_fd,
883 const struct bpf_link_update_opts *opts)
884{
813847a3 885 const size_t attr_sz = offsetofend(union bpf_attr, link_update);
cc4f864b 886 union bpf_attr attr;
f12b6543 887 int ret;
cc4f864b
AN
888
889 if (!OPTS_VALID(opts, bpf_link_update_opts))
f12b6543 890 return libbpf_err(-EINVAL);
cc4f864b 891
912dd4b0
KFL
892 if (OPTS_GET(opts, old_prog_fd, 0) && OPTS_GET(opts, old_map_fd, 0))
893 return libbpf_err(-EINVAL);
894
813847a3 895 memset(&attr, 0, attr_sz);
cc4f864b
AN
896 attr.link_update.link_fd = link_fd;
897 attr.link_update.new_prog_fd = new_prog_fd;
898 attr.link_update.flags = OPTS_GET(opts, flags, 0);
912dd4b0
KFL
899 if (OPTS_GET(opts, old_prog_fd, 0))
900 attr.link_update.old_prog_fd = OPTS_GET(opts, old_prog_fd, 0);
901 else if (OPTS_GET(opts, old_map_fd, 0))
902 attr.link_update.old_map_fd = OPTS_GET(opts, old_map_fd, 0);
cc4f864b 903
813847a3 904 ret = sys_bpf(BPF_LINK_UPDATE, &attr, attr_sz);
f12b6543 905 return libbpf_err_errno(ret);
cc4f864b
AN
906}
907
c09add2f
YS
908int bpf_iter_create(int link_fd)
909{
813847a3 910 const size_t attr_sz = offsetofend(union bpf_attr, iter_create);
c09add2f 911 union bpf_attr attr;
f12b6543 912 int fd;
c09add2f 913
813847a3 914 memset(&attr, 0, attr_sz);
c09add2f
YS
915 attr.iter_create.link_fd = link_fd;
916
813847a3 917 fd = sys_bpf_fd(BPF_ITER_CREATE, &attr, attr_sz);
f12b6543 918 return libbpf_err_errno(fd);
c09add2f
YS
919}
920
fe20ce3a 921int bpf_prog_query_opts(int target, enum bpf_attach_type type,
a4b2f3cf 922 struct bpf_prog_query_opts *opts)
5d0cbf9b 923{
813847a3 924 const size_t attr_sz = offsetofend(union bpf_attr, query);
5d0cbf9b
AS
925 union bpf_attr attr;
926 int ret;
927
a4b2f3cf
SF
928 if (!OPTS_VALID(opts, bpf_prog_query_opts))
929 return libbpf_err(-EINVAL);
930
813847a3 931 memset(&attr, 0, attr_sz);
fe20ce3a
DB
932 attr.query.target_fd = target;
933 attr.query.attach_type = type;
934 attr.query.query_flags = OPTS_GET(opts, query_flags, 0);
935 attr.query.count = OPTS_GET(opts, count, 0);
936 attr.query.prog_ids = ptr_to_u64(OPTS_GET(opts, prog_ids, NULL));
937 attr.query.link_ids = ptr_to_u64(OPTS_GET(opts, link_ids, NULL));
938 attr.query.prog_attach_flags = ptr_to_u64(OPTS_GET(opts, prog_attach_flags, NULL));
939 attr.query.link_attach_flags = ptr_to_u64(OPTS_GET(opts, link_attach_flags, NULL));
5d0cbf9b 940
813847a3 941 ret = sys_bpf(BPF_PROG_QUERY, &attr, attr_sz);
f12b6543 942
a4b2f3cf 943 OPTS_SET(opts, attach_flags, attr.query.attach_flags);
fe20ce3a
DB
944 OPTS_SET(opts, revision, attr.query.revision);
945 OPTS_SET(opts, count, attr.query.count);
a4b2f3cf
SF
946
947 return libbpf_err_errno(ret);
948}
949
950int bpf_prog_query(int target_fd, enum bpf_attach_type type, __u32 query_flags,
951 __u32 *attach_flags, __u32 *prog_ids, __u32 *prog_cnt)
952{
953 LIBBPF_OPTS(bpf_prog_query_opts, opts);
954 int ret;
955
956 opts.query_flags = query_flags;
957 opts.prog_ids = prog_ids;
958 opts.prog_cnt = *prog_cnt;
959
960 ret = bpf_prog_query_opts(target_fd, type, &opts);
961
5d0cbf9b 962 if (attach_flags)
a4b2f3cf
SF
963 *attach_flags = opts.attach_flags;
964 *prog_cnt = opts.prog_cnt;
f12b6543
AN
965
966 return libbpf_err_errno(ret);
5d0cbf9b
AS
967}
968
88f7fe72
SL
969int bpf_prog_test_run_opts(int prog_fd, struct bpf_test_run_opts *opts)
970{
813847a3 971 const size_t attr_sz = offsetofend(union bpf_attr, test);
88f7fe72
SL
972 union bpf_attr attr;
973 int ret;
974
975 if (!OPTS_VALID(opts, bpf_test_run_opts))
f12b6543 976 return libbpf_err(-EINVAL);
88f7fe72 977
813847a3 978 memset(&attr, 0, attr_sz);
88f7fe72 979 attr.test.prog_fd = prog_fd;
24592ad1 980 attr.test.batch_size = OPTS_GET(opts, batch_size, 0);
88f7fe72
SL
981 attr.test.cpu = OPTS_GET(opts, cpu, 0);
982 attr.test.flags = OPTS_GET(opts, flags, 0);
983 attr.test.repeat = OPTS_GET(opts, repeat, 0);
984 attr.test.duration = OPTS_GET(opts, duration, 0);
985 attr.test.ctx_size_in = OPTS_GET(opts, ctx_size_in, 0);
986 attr.test.ctx_size_out = OPTS_GET(opts, ctx_size_out, 0);
987 attr.test.data_size_in = OPTS_GET(opts, data_size_in, 0);
988 attr.test.data_size_out = OPTS_GET(opts, data_size_out, 0);
989 attr.test.ctx_in = ptr_to_u64(OPTS_GET(opts, ctx_in, NULL));
990 attr.test.ctx_out = ptr_to_u64(OPTS_GET(opts, ctx_out, NULL));
991 attr.test.data_in = ptr_to_u64(OPTS_GET(opts, data_in, NULL));
992 attr.test.data_out = ptr_to_u64(OPTS_GET(opts, data_out, NULL));
993
813847a3 994 ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, attr_sz);
f12b6543 995
88f7fe72
SL
996 OPTS_SET(opts, data_size_out, attr.test.data_size_out);
997 OPTS_SET(opts, ctx_size_out, attr.test.ctx_size_out);
998 OPTS_SET(opts, duration, attr.test.duration);
999 OPTS_SET(opts, retval, attr.test.retval);
f12b6543
AN
1000
1001 return libbpf_err_errno(ret);
88f7fe72
SL
1002}
1003
a6e130c4 1004static int bpf_obj_get_next_id(__u32 start_id, __u32 *next_id, int cmd)
95b9afd3 1005{
813847a3 1006 const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
95b9afd3
MKL
1007 union bpf_attr attr;
1008 int err;
1009
813847a3 1010 memset(&attr, 0, attr_sz);
95b9afd3
MKL
1011 attr.start_id = start_id;
1012
813847a3 1013 err = sys_bpf(cmd, &attr, attr_sz);
95b9afd3
MKL
1014 if (!err)
1015 *next_id = attr.next_id;
1016
f12b6543 1017 return libbpf_err_errno(err);
95b9afd3
MKL
1018}
1019
a6e130c4 1020int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id)
95b9afd3 1021{
a6e130c4
QM
1022 return bpf_obj_get_next_id(start_id, next_id, BPF_PROG_GET_NEXT_ID);
1023}
95b9afd3 1024
a6e130c4
QM
1025int bpf_map_get_next_id(__u32 start_id, __u32 *next_id)
1026{
1027 return bpf_obj_get_next_id(start_id, next_id, BPF_MAP_GET_NEXT_ID);
95b9afd3
MKL
1028}
1029
09d7c2e3
QM
1030int bpf_btf_get_next_id(__u32 start_id, __u32 *next_id)
1031{
1032 return bpf_obj_get_next_id(start_id, next_id, BPF_BTF_GET_NEXT_ID);
1033}
1034
0dbc8668
AN
1035int bpf_link_get_next_id(__u32 start_id, __u32 *next_id)
1036{
1037 return bpf_obj_get_next_id(start_id, next_id, BPF_LINK_GET_NEXT_ID);
1038}
1039
8f13f168
RS
1040int bpf_prog_get_fd_by_id_opts(__u32 id,
1041 const struct bpf_get_fd_by_id_opts *opts)
95b9afd3 1042{
813847a3 1043 const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
95b9afd3 1044 union bpf_attr attr;
f12b6543 1045 int fd;
95b9afd3 1046
8f13f168
RS
1047 if (!OPTS_VALID(opts, bpf_get_fd_by_id_opts))
1048 return libbpf_err(-EINVAL);
1049
813847a3 1050 memset(&attr, 0, attr_sz);
95b9afd3 1051 attr.prog_id = id;
8f13f168 1052 attr.open_flags = OPTS_GET(opts, open_flags, 0);
95b9afd3 1053
813847a3 1054 fd = sys_bpf_fd(BPF_PROG_GET_FD_BY_ID, &attr, attr_sz);
f12b6543 1055 return libbpf_err_errno(fd);
95b9afd3
MKL
1056}
1057
8f13f168
RS
1058int bpf_prog_get_fd_by_id(__u32 id)
1059{
1060 return bpf_prog_get_fd_by_id_opts(id, NULL);
1061}
1062
243e3005
RS
1063int bpf_map_get_fd_by_id_opts(__u32 id,
1064 const struct bpf_get_fd_by_id_opts *opts)
95b9afd3 1065{
813847a3 1066 const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
95b9afd3 1067 union bpf_attr attr;
f12b6543 1068 int fd;
95b9afd3 1069
243e3005
RS
1070 if (!OPTS_VALID(opts, bpf_get_fd_by_id_opts))
1071 return libbpf_err(-EINVAL);
1072
813847a3 1073 memset(&attr, 0, attr_sz);
95b9afd3 1074 attr.map_id = id;
243e3005 1075 attr.open_flags = OPTS_GET(opts, open_flags, 0);
95b9afd3 1076
813847a3 1077 fd = sys_bpf_fd(BPF_MAP_GET_FD_BY_ID, &attr, attr_sz);
f12b6543 1078 return libbpf_err_errno(fd);
95b9afd3
MKL
1079}
1080
243e3005
RS
1081int bpf_map_get_fd_by_id(__u32 id)
1082{
1083 return bpf_map_get_fd_by_id_opts(id, NULL);
1084}
1085
2ce7cbf2
RS
1086int bpf_btf_get_fd_by_id_opts(__u32 id,
1087 const struct bpf_get_fd_by_id_opts *opts)
cd8b8928 1088{
813847a3 1089 const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
cd8b8928 1090 union bpf_attr attr;
f12b6543 1091 int fd;
cd8b8928 1092
2ce7cbf2
RS
1093 if (!OPTS_VALID(opts, bpf_get_fd_by_id_opts))
1094 return libbpf_err(-EINVAL);
1095
813847a3 1096 memset(&attr, 0, attr_sz);
cd8b8928 1097 attr.btf_id = id;
2ce7cbf2 1098 attr.open_flags = OPTS_GET(opts, open_flags, 0);
cd8b8928 1099
813847a3 1100 fd = sys_bpf_fd(BPF_BTF_GET_FD_BY_ID, &attr, attr_sz);
f12b6543 1101 return libbpf_err_errno(fd);
cd8b8928
MKL
1102}
1103
2ce7cbf2
RS
1104int bpf_btf_get_fd_by_id(__u32 id)
1105{
1106 return bpf_btf_get_fd_by_id_opts(id, NULL);
1107}
1108
97c8f9dd
RS
1109int bpf_link_get_fd_by_id_opts(__u32 id,
1110 const struct bpf_get_fd_by_id_opts *opts)
0dbc8668 1111{
813847a3 1112 const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
0dbc8668 1113 union bpf_attr attr;
f12b6543 1114 int fd;
0dbc8668 1115
97c8f9dd
RS
1116 if (!OPTS_VALID(opts, bpf_get_fd_by_id_opts))
1117 return libbpf_err(-EINVAL);
1118
813847a3 1119 memset(&attr, 0, attr_sz);
0dbc8668 1120 attr.link_id = id;
97c8f9dd 1121 attr.open_flags = OPTS_GET(opts, open_flags, 0);
0dbc8668 1122
813847a3 1123 fd = sys_bpf_fd(BPF_LINK_GET_FD_BY_ID, &attr, attr_sz);
f12b6543 1124 return libbpf_err_errno(fd);
0dbc8668
AN
1125}
1126
97c8f9dd
RS
1127int bpf_link_get_fd_by_id(__u32 id)
1128{
1129 return bpf_link_get_fd_by_id_opts(id, NULL);
1130}
1131
0dbc8668 1132int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len)
95b9afd3 1133{
813847a3 1134 const size_t attr_sz = offsetofend(union bpf_attr, info);
95b9afd3
MKL
1135 union bpf_attr attr;
1136 int err;
1137
813847a3 1138 memset(&attr, 0, attr_sz);
0dbc8668 1139 attr.info.bpf_fd = bpf_fd;
95b9afd3
MKL
1140 attr.info.info_len = *info_len;
1141 attr.info.info = ptr_to_u64(info);
1142
813847a3 1143 err = sys_bpf(BPF_OBJ_GET_INFO_BY_FD, &attr, attr_sz);
95b9afd3
MKL
1144 if (!err)
1145 *info_len = attr.info.info_len;
f12b6543 1146 return libbpf_err_errno(err);
95b9afd3 1147}
949abbe8 1148
55a9ed0e
IL
1149int bpf_prog_get_info_by_fd(int prog_fd, struct bpf_prog_info *info, __u32 *info_len)
1150{
1151 return bpf_obj_get_info_by_fd(prog_fd, info, info_len);
1152}
1153
1154int bpf_map_get_info_by_fd(int map_fd, struct bpf_map_info *info, __u32 *info_len)
1155{
1156 return bpf_obj_get_info_by_fd(map_fd, info, info_len);
1157}
1158
1159int bpf_btf_get_info_by_fd(int btf_fd, struct bpf_btf_info *info, __u32 *info_len)
1160{
1161 return bpf_obj_get_info_by_fd(btf_fd, info, info_len);
1162}
1163
1164int bpf_link_get_info_by_fd(int link_fd, struct bpf_link_info *info, __u32 *info_len)
1165{
1166 return bpf_obj_get_info_by_fd(link_fd, info, info_len);
1167}
1168
a0fe3e57
AS
1169int bpf_raw_tracepoint_open(const char *name, int prog_fd)
1170{
813847a3 1171 const size_t attr_sz = offsetofend(union bpf_attr, raw_tracepoint);
a0fe3e57 1172 union bpf_attr attr;
f12b6543 1173 int fd;
a0fe3e57 1174
813847a3 1175 memset(&attr, 0, attr_sz);
a0fe3e57
AS
1176 attr.raw_tracepoint.name = ptr_to_u64(name);
1177 attr.raw_tracepoint.prog_fd = prog_fd;
1178
813847a3 1179 fd = sys_bpf_fd(BPF_RAW_TRACEPOINT_OPEN, &attr, attr_sz);
f12b6543 1180 return libbpf_err_errno(fd);
a0fe3e57
AS
1181}
1182
097d8002 1183int bpf_btf_load(const void *btf_data, size_t btf_size, struct bpf_btf_load_opts *opts)
8a138aed 1184{
d17aff80 1185 const size_t attr_sz = offsetofend(union bpf_attr, btf_log_true_size);
0ed08d67
AN
1186 union bpf_attr attr;
1187 char *log_buf;
1188 size_t log_size;
1189 __u32 log_level;
8a138aed
MKL
1190 int fd;
1191
e542f2c4
AN
1192 bump_rlimit_memlock();
1193
0ed08d67
AN
1194 memset(&attr, 0, attr_sz);
1195
1196 if (!OPTS_VALID(opts, bpf_btf_load_opts))
1197 return libbpf_err(-EINVAL);
1198
1199 log_buf = OPTS_GET(opts, log_buf, NULL);
1200 log_size = OPTS_GET(opts, log_size, 0);
1201 log_level = OPTS_GET(opts, log_level, 0);
1202
1203 if (log_size > UINT_MAX)
1204 return libbpf_err(-EINVAL);
1205 if (log_size && !log_buf)
1206 return libbpf_err(-EINVAL);
1207
1208 attr.btf = ptr_to_u64(btf_data);
8a138aed 1209 attr.btf_size = btf_size;
0ed08d67
AN
1210 /* log_level == 0 and log_buf != NULL means "try loading without
1211 * log_buf, but retry with log_buf and log_level=1 on error", which is
1212 * consistent across low-level and high-level BTF and program loading
1213 * APIs within libbpf and provides a sensible behavior in practice
1214 */
1215 if (log_level) {
1216 attr.btf_log_buf = ptr_to_u64(log_buf);
1217 attr.btf_log_size = (__u32)log_size;
1218 attr.btf_log_level = log_level;
1219 }
8a138aed 1220
0ed08d67
AN
1221 fd = sys_bpf_fd(BPF_BTF_LOAD, &attr, attr_sz);
1222 if (fd < 0 && log_buf && log_level == 0) {
8a138aed 1223 attr.btf_log_buf = ptr_to_u64(log_buf);
0ed08d67
AN
1224 attr.btf_log_size = (__u32)log_size;
1225 attr.btf_log_level = 1;
1226 fd = sys_bpf_fd(BPF_BTF_LOAD, &attr, attr_sz);
8a138aed 1227 }
097d8002
AN
1228
1229 OPTS_SET(opts, log_true_size, attr.btf_log_true_size);
0ed08d67
AN
1230 return libbpf_err_errno(fd);
1231}
1232
30687ad9
YS
1233int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, __u32 *buf_len,
1234 __u32 *prog_id, __u32 *fd_type, __u64 *probe_offset,
1235 __u64 *probe_addr)
1236{
813847a3
AN
1237 const size_t attr_sz = offsetofend(union bpf_attr, task_fd_query);
1238 union bpf_attr attr;
30687ad9
YS
1239 int err;
1240
813847a3 1241 memset(&attr, 0, attr_sz);
30687ad9
YS
1242 attr.task_fd_query.pid = pid;
1243 attr.task_fd_query.fd = fd;
1244 attr.task_fd_query.flags = flags;
1245 attr.task_fd_query.buf = ptr_to_u64(buf);
1246 attr.task_fd_query.buf_len = *buf_len;
1247
813847a3 1248 err = sys_bpf(BPF_TASK_FD_QUERY, &attr, attr_sz);
f12b6543 1249
30687ad9
YS
1250 *buf_len = attr.task_fd_query.buf_len;
1251 *prog_id = attr.task_fd_query.prog_id;
1252 *fd_type = attr.task_fd_query.fd_type;
1253 *probe_offset = attr.task_fd_query.probe_offset;
1254 *probe_addr = attr.task_fd_query.probe_addr;
1255
f12b6543 1256 return libbpf_err_errno(err);
30687ad9 1257}
0bee1067
SL
1258
1259int bpf_enable_stats(enum bpf_stats_type type)
1260{
813847a3 1261 const size_t attr_sz = offsetofend(union bpf_attr, enable_stats);
0bee1067 1262 union bpf_attr attr;
f12b6543 1263 int fd;
0bee1067 1264
813847a3 1265 memset(&attr, 0, attr_sz);
0bee1067
SL
1266 attr.enable_stats.type = type;
1267
813847a3 1268 fd = sys_bpf_fd(BPF_ENABLE_STATS, &attr, attr_sz);
f12b6543 1269 return libbpf_err_errno(fd);
0bee1067 1270}
5d23328d
YZ
1271
1272int bpf_prog_bind_map(int prog_fd, int map_fd,
1273 const struct bpf_prog_bind_opts *opts)
1274{
813847a3 1275 const size_t attr_sz = offsetofend(union bpf_attr, prog_bind_map);
5d23328d 1276 union bpf_attr attr;
f12b6543 1277 int ret;
5d23328d
YZ
1278
1279 if (!OPTS_VALID(opts, bpf_prog_bind_opts))
f12b6543 1280 return libbpf_err(-EINVAL);
5d23328d 1281
813847a3 1282 memset(&attr, 0, attr_sz);
5d23328d
YZ
1283 attr.prog_bind_map.prog_fd = prog_fd;
1284 attr.prog_bind_map.map_fd = map_fd;
1285 attr.prog_bind_map.flags = OPTS_GET(opts, flags, 0);
1286
813847a3 1287 ret = sys_bpf(BPF_PROG_BIND_MAP, &attr, attr_sz);
f12b6543 1288 return libbpf_err_errno(ret);
5d23328d 1289}