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