Coverage Report

Created: 2025-11-24 06:08

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