xref: /aosp_15_r20/external/libbpf/src/libbpf_probes.c (revision f7c14bbac8cf49633f2740db462ea43457973ec4)
1*f7c14bbaSAndroid Build Coastguard Worker // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2*f7c14bbaSAndroid Build Coastguard Worker /* Copyright (c) 2019 Netronome Systems, Inc. */
3*f7c14bbaSAndroid Build Coastguard Worker 
4*f7c14bbaSAndroid Build Coastguard Worker #include <errno.h>
5*f7c14bbaSAndroid Build Coastguard Worker #include <fcntl.h>
6*f7c14bbaSAndroid Build Coastguard Worker #include <string.h>
7*f7c14bbaSAndroid Build Coastguard Worker #include <stdlib.h>
8*f7c14bbaSAndroid Build Coastguard Worker #include <unistd.h>
9*f7c14bbaSAndroid Build Coastguard Worker #include <net/if.h>
10*f7c14bbaSAndroid Build Coastguard Worker #include <sys/utsname.h>
11*f7c14bbaSAndroid Build Coastguard Worker 
12*f7c14bbaSAndroid Build Coastguard Worker #include <linux/btf.h>
13*f7c14bbaSAndroid Build Coastguard Worker #include <linux/filter.h>
14*f7c14bbaSAndroid Build Coastguard Worker #include <linux/kernel.h>
15*f7c14bbaSAndroid Build Coastguard Worker #include <linux/version.h>
16*f7c14bbaSAndroid Build Coastguard Worker 
17*f7c14bbaSAndroid Build Coastguard Worker #include "bpf.h"
18*f7c14bbaSAndroid Build Coastguard Worker #include "libbpf.h"
19*f7c14bbaSAndroid Build Coastguard Worker #include "libbpf_internal.h"
20*f7c14bbaSAndroid Build Coastguard Worker 
21*f7c14bbaSAndroid Build Coastguard Worker /* On Ubuntu LINUX_VERSION_CODE doesn't correspond to info.release,
22*f7c14bbaSAndroid Build Coastguard Worker  * but Ubuntu provides /proc/version_signature file, as described at
23*f7c14bbaSAndroid Build Coastguard Worker  * https://ubuntu.com/kernel, with an example contents below, which we
24*f7c14bbaSAndroid Build Coastguard Worker  * can use to get a proper LINUX_VERSION_CODE.
25*f7c14bbaSAndroid Build Coastguard Worker  *
26*f7c14bbaSAndroid Build Coastguard Worker  *   Ubuntu 5.4.0-12.15-generic 5.4.8
27*f7c14bbaSAndroid Build Coastguard Worker  *
28*f7c14bbaSAndroid Build Coastguard Worker  * In the above, 5.4.8 is what kernel is actually expecting, while
29*f7c14bbaSAndroid Build Coastguard Worker  * uname() call will return 5.4.0 in info.release.
30*f7c14bbaSAndroid Build Coastguard Worker  */
get_ubuntu_kernel_version(void)31*f7c14bbaSAndroid Build Coastguard Worker static __u32 get_ubuntu_kernel_version(void)
32*f7c14bbaSAndroid Build Coastguard Worker {
33*f7c14bbaSAndroid Build Coastguard Worker 	const char *ubuntu_kver_file = "/proc/version_signature";
34*f7c14bbaSAndroid Build Coastguard Worker 	__u32 major, minor, patch;
35*f7c14bbaSAndroid Build Coastguard Worker 	int ret;
36*f7c14bbaSAndroid Build Coastguard Worker 	FILE *f;
37*f7c14bbaSAndroid Build Coastguard Worker 
38*f7c14bbaSAndroid Build Coastguard Worker 	if (faccessat(AT_FDCWD, ubuntu_kver_file, R_OK, AT_EACCESS) != 0)
39*f7c14bbaSAndroid Build Coastguard Worker 		return 0;
40*f7c14bbaSAndroid Build Coastguard Worker 
41*f7c14bbaSAndroid Build Coastguard Worker 	f = fopen(ubuntu_kver_file, "re");
42*f7c14bbaSAndroid Build Coastguard Worker 	if (!f)
43*f7c14bbaSAndroid Build Coastguard Worker 		return 0;
44*f7c14bbaSAndroid Build Coastguard Worker 
45*f7c14bbaSAndroid Build Coastguard Worker 	ret = fscanf(f, "%*s %*s %u.%u.%u\n", &major, &minor, &patch);
46*f7c14bbaSAndroid Build Coastguard Worker 	fclose(f);
47*f7c14bbaSAndroid Build Coastguard Worker 	if (ret != 3)
48*f7c14bbaSAndroid Build Coastguard Worker 		return 0;
49*f7c14bbaSAndroid Build Coastguard Worker 
50*f7c14bbaSAndroid Build Coastguard Worker 	return KERNEL_VERSION(major, minor, patch);
51*f7c14bbaSAndroid Build Coastguard Worker }
52*f7c14bbaSAndroid Build Coastguard Worker 
53*f7c14bbaSAndroid Build Coastguard Worker /* On Debian LINUX_VERSION_CODE doesn't correspond to info.release.
54*f7c14bbaSAndroid Build Coastguard Worker  * Instead, it is provided in info.version. An example content of
55*f7c14bbaSAndroid Build Coastguard Worker  * Debian 10 looks like the below.
56*f7c14bbaSAndroid Build Coastguard Worker  *
57*f7c14bbaSAndroid Build Coastguard Worker  *   utsname::release   4.19.0-22-amd64
58*f7c14bbaSAndroid Build Coastguard Worker  *   utsname::version   #1 SMP Debian 4.19.260-1 (2022-09-29)
59*f7c14bbaSAndroid Build Coastguard Worker  *
60*f7c14bbaSAndroid Build Coastguard Worker  * In the above, 4.19.260 is what kernel is actually expecting, while
61*f7c14bbaSAndroid Build Coastguard Worker  * uname() call will return 4.19.0 in info.release.
62*f7c14bbaSAndroid Build Coastguard Worker  */
get_debian_kernel_version(struct utsname * info)63*f7c14bbaSAndroid Build Coastguard Worker static __u32 get_debian_kernel_version(struct utsname *info)
64*f7c14bbaSAndroid Build Coastguard Worker {
65*f7c14bbaSAndroid Build Coastguard Worker 	__u32 major, minor, patch;
66*f7c14bbaSAndroid Build Coastguard Worker 	char *p;
67*f7c14bbaSAndroid Build Coastguard Worker 
68*f7c14bbaSAndroid Build Coastguard Worker 	p = strstr(info->version, "Debian ");
69*f7c14bbaSAndroid Build Coastguard Worker 	if (!p) {
70*f7c14bbaSAndroid Build Coastguard Worker 		/* This is not a Debian kernel. */
71*f7c14bbaSAndroid Build Coastguard Worker 		return 0;
72*f7c14bbaSAndroid Build Coastguard Worker 	}
73*f7c14bbaSAndroid Build Coastguard Worker 
74*f7c14bbaSAndroid Build Coastguard Worker 	if (sscanf(p, "Debian %u.%u.%u", &major, &minor, &patch) != 3)
75*f7c14bbaSAndroid Build Coastguard Worker 		return 0;
76*f7c14bbaSAndroid Build Coastguard Worker 
77*f7c14bbaSAndroid Build Coastguard Worker 	return KERNEL_VERSION(major, minor, patch);
78*f7c14bbaSAndroid Build Coastguard Worker }
79*f7c14bbaSAndroid Build Coastguard Worker 
get_kernel_version(void)80*f7c14bbaSAndroid Build Coastguard Worker __u32 get_kernel_version(void)
81*f7c14bbaSAndroid Build Coastguard Worker {
82*f7c14bbaSAndroid Build Coastguard Worker 	__u32 major, minor, patch, version;
83*f7c14bbaSAndroid Build Coastguard Worker 	struct utsname info;
84*f7c14bbaSAndroid Build Coastguard Worker 
85*f7c14bbaSAndroid Build Coastguard Worker 	/* Check if this is an Ubuntu kernel. */
86*f7c14bbaSAndroid Build Coastguard Worker 	version = get_ubuntu_kernel_version();
87*f7c14bbaSAndroid Build Coastguard Worker 	if (version != 0)
88*f7c14bbaSAndroid Build Coastguard Worker 		return version;
89*f7c14bbaSAndroid Build Coastguard Worker 
90*f7c14bbaSAndroid Build Coastguard Worker 	uname(&info);
91*f7c14bbaSAndroid Build Coastguard Worker 
92*f7c14bbaSAndroid Build Coastguard Worker 	/* Check if this is a Debian kernel. */
93*f7c14bbaSAndroid Build Coastguard Worker 	version = get_debian_kernel_version(&info);
94*f7c14bbaSAndroid Build Coastguard Worker 	if (version != 0)
95*f7c14bbaSAndroid Build Coastguard Worker 		return version;
96*f7c14bbaSAndroid Build Coastguard Worker 
97*f7c14bbaSAndroid Build Coastguard Worker 	if (sscanf(info.release, "%u.%u.%u", &major, &minor, &patch) != 3)
98*f7c14bbaSAndroid Build Coastguard Worker 		return 0;
99*f7c14bbaSAndroid Build Coastguard Worker 
100*f7c14bbaSAndroid Build Coastguard Worker 	return KERNEL_VERSION(major, minor, patch);
101*f7c14bbaSAndroid Build Coastguard Worker }
102*f7c14bbaSAndroid Build Coastguard Worker 
probe_prog_load(enum bpf_prog_type prog_type,const struct bpf_insn * insns,size_t insns_cnt,char * log_buf,size_t log_buf_sz)103*f7c14bbaSAndroid Build Coastguard Worker static int probe_prog_load(enum bpf_prog_type prog_type,
104*f7c14bbaSAndroid Build Coastguard Worker 			   const struct bpf_insn *insns, size_t insns_cnt,
105*f7c14bbaSAndroid Build Coastguard Worker 			   char *log_buf, size_t log_buf_sz)
106*f7c14bbaSAndroid Build Coastguard Worker {
107*f7c14bbaSAndroid Build Coastguard Worker 	LIBBPF_OPTS(bpf_prog_load_opts, opts,
108*f7c14bbaSAndroid Build Coastguard Worker 		.log_buf = log_buf,
109*f7c14bbaSAndroid Build Coastguard Worker 		.log_size = log_buf_sz,
110*f7c14bbaSAndroid Build Coastguard Worker 		.log_level = log_buf ? 1 : 0,
111*f7c14bbaSAndroid Build Coastguard Worker 	);
112*f7c14bbaSAndroid Build Coastguard Worker 	int fd, err, exp_err = 0;
113*f7c14bbaSAndroid Build Coastguard Worker 	const char *exp_msg = NULL;
114*f7c14bbaSAndroid Build Coastguard Worker 	char buf[4096];
115*f7c14bbaSAndroid Build Coastguard Worker 
116*f7c14bbaSAndroid Build Coastguard Worker 	switch (prog_type) {
117*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
118*f7c14bbaSAndroid Build Coastguard Worker 		opts.expected_attach_type = BPF_CGROUP_INET4_CONNECT;
119*f7c14bbaSAndroid Build Coastguard Worker 		break;
120*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_CGROUP_SOCKOPT:
121*f7c14bbaSAndroid Build Coastguard Worker 		opts.expected_attach_type = BPF_CGROUP_GETSOCKOPT;
122*f7c14bbaSAndroid Build Coastguard Worker 		break;
123*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_SK_LOOKUP:
124*f7c14bbaSAndroid Build Coastguard Worker 		opts.expected_attach_type = BPF_SK_LOOKUP;
125*f7c14bbaSAndroid Build Coastguard Worker 		break;
126*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_KPROBE:
127*f7c14bbaSAndroid Build Coastguard Worker 		opts.kern_version = get_kernel_version();
128*f7c14bbaSAndroid Build Coastguard Worker 		break;
129*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_LIRC_MODE2:
130*f7c14bbaSAndroid Build Coastguard Worker 		opts.expected_attach_type = BPF_LIRC_MODE2;
131*f7c14bbaSAndroid Build Coastguard Worker 		break;
132*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_TRACING:
133*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_LSM:
134*f7c14bbaSAndroid Build Coastguard Worker 		opts.log_buf = buf;
135*f7c14bbaSAndroid Build Coastguard Worker 		opts.log_size = sizeof(buf);
136*f7c14bbaSAndroid Build Coastguard Worker 		opts.log_level = 1;
137*f7c14bbaSAndroid Build Coastguard Worker 		if (prog_type == BPF_PROG_TYPE_TRACING)
138*f7c14bbaSAndroid Build Coastguard Worker 			opts.expected_attach_type = BPF_TRACE_FENTRY;
139*f7c14bbaSAndroid Build Coastguard Worker 		else
140*f7c14bbaSAndroid Build Coastguard Worker 			opts.expected_attach_type = BPF_MODIFY_RETURN;
141*f7c14bbaSAndroid Build Coastguard Worker 		opts.attach_btf_id = 1;
142*f7c14bbaSAndroid Build Coastguard Worker 
143*f7c14bbaSAndroid Build Coastguard Worker 		exp_err = -EINVAL;
144*f7c14bbaSAndroid Build Coastguard Worker 		exp_msg = "attach_btf_id 1 is not a function";
145*f7c14bbaSAndroid Build Coastguard Worker 		break;
146*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_EXT:
147*f7c14bbaSAndroid Build Coastguard Worker 		opts.log_buf = buf;
148*f7c14bbaSAndroid Build Coastguard Worker 		opts.log_size = sizeof(buf);
149*f7c14bbaSAndroid Build Coastguard Worker 		opts.log_level = 1;
150*f7c14bbaSAndroid Build Coastguard Worker 		opts.attach_btf_id = 1;
151*f7c14bbaSAndroid Build Coastguard Worker 
152*f7c14bbaSAndroid Build Coastguard Worker 		exp_err = -EINVAL;
153*f7c14bbaSAndroid Build Coastguard Worker 		exp_msg = "Cannot replace kernel functions";
154*f7c14bbaSAndroid Build Coastguard Worker 		break;
155*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_SYSCALL:
156*f7c14bbaSAndroid Build Coastguard Worker 		opts.prog_flags = BPF_F_SLEEPABLE;
157*f7c14bbaSAndroid Build Coastguard Worker 		break;
158*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_STRUCT_OPS:
159*f7c14bbaSAndroid Build Coastguard Worker 		exp_err = -524; /* -ENOTSUPP */
160*f7c14bbaSAndroid Build Coastguard Worker 		break;
161*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_UNSPEC:
162*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_SOCKET_FILTER:
163*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_SCHED_CLS:
164*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_SCHED_ACT:
165*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_TRACEPOINT:
166*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_XDP:
167*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_PERF_EVENT:
168*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_CGROUP_SKB:
169*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_CGROUP_SOCK:
170*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_LWT_IN:
171*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_LWT_OUT:
172*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_LWT_XMIT:
173*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_SOCK_OPS:
174*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_SK_SKB:
175*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_CGROUP_DEVICE:
176*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_SK_MSG:
177*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_RAW_TRACEPOINT:
178*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
179*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_LWT_SEG6LOCAL:
180*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_SK_REUSEPORT:
181*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_FLOW_DISSECTOR:
182*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_CGROUP_SYSCTL:
183*f7c14bbaSAndroid Build Coastguard Worker 		break;
184*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_NETFILTER:
185*f7c14bbaSAndroid Build Coastguard Worker 		opts.expected_attach_type = BPF_NETFILTER;
186*f7c14bbaSAndroid Build Coastguard Worker 		break;
187*f7c14bbaSAndroid Build Coastguard Worker 	default:
188*f7c14bbaSAndroid Build Coastguard Worker 		return -EOPNOTSUPP;
189*f7c14bbaSAndroid Build Coastguard Worker 	}
190*f7c14bbaSAndroid Build Coastguard Worker 
191*f7c14bbaSAndroid Build Coastguard Worker 	fd = bpf_prog_load(prog_type, NULL, "GPL", insns, insns_cnt, &opts);
192*f7c14bbaSAndroid Build Coastguard Worker 	err = -errno;
193*f7c14bbaSAndroid Build Coastguard Worker 	if (fd >= 0)
194*f7c14bbaSAndroid Build Coastguard Worker 		close(fd);
195*f7c14bbaSAndroid Build Coastguard Worker 	if (exp_err) {
196*f7c14bbaSAndroid Build Coastguard Worker 		if (fd >= 0 || err != exp_err)
197*f7c14bbaSAndroid Build Coastguard Worker 			return 0;
198*f7c14bbaSAndroid Build Coastguard Worker 		if (exp_msg && !strstr(buf, exp_msg))
199*f7c14bbaSAndroid Build Coastguard Worker 			return 0;
200*f7c14bbaSAndroid Build Coastguard Worker 		return 1;
201*f7c14bbaSAndroid Build Coastguard Worker 	}
202*f7c14bbaSAndroid Build Coastguard Worker 	return fd >= 0 ? 1 : 0;
203*f7c14bbaSAndroid Build Coastguard Worker }
204*f7c14bbaSAndroid Build Coastguard Worker 
libbpf_probe_bpf_prog_type(enum bpf_prog_type prog_type,const void * opts)205*f7c14bbaSAndroid Build Coastguard Worker int libbpf_probe_bpf_prog_type(enum bpf_prog_type prog_type, const void *opts)
206*f7c14bbaSAndroid Build Coastguard Worker {
207*f7c14bbaSAndroid Build Coastguard Worker 	struct bpf_insn insns[] = {
208*f7c14bbaSAndroid Build Coastguard Worker 		BPF_MOV64_IMM(BPF_REG_0, 0),
209*f7c14bbaSAndroid Build Coastguard Worker 		BPF_EXIT_INSN()
210*f7c14bbaSAndroid Build Coastguard Worker 	};
211*f7c14bbaSAndroid Build Coastguard Worker 	const size_t insn_cnt = ARRAY_SIZE(insns);
212*f7c14bbaSAndroid Build Coastguard Worker 	int ret;
213*f7c14bbaSAndroid Build Coastguard Worker 
214*f7c14bbaSAndroid Build Coastguard Worker 	if (opts)
215*f7c14bbaSAndroid Build Coastguard Worker 		return libbpf_err(-EINVAL);
216*f7c14bbaSAndroid Build Coastguard Worker 
217*f7c14bbaSAndroid Build Coastguard Worker 	ret = probe_prog_load(prog_type, insns, insn_cnt, NULL, 0);
218*f7c14bbaSAndroid Build Coastguard Worker 	return libbpf_err(ret);
219*f7c14bbaSAndroid Build Coastguard Worker }
220*f7c14bbaSAndroid Build Coastguard Worker 
libbpf__load_raw_btf(const char * raw_types,size_t types_len,const char * str_sec,size_t str_len,int token_fd)221*f7c14bbaSAndroid Build Coastguard Worker int libbpf__load_raw_btf(const char *raw_types, size_t types_len,
222*f7c14bbaSAndroid Build Coastguard Worker 			 const char *str_sec, size_t str_len,
223*f7c14bbaSAndroid Build Coastguard Worker 			 int token_fd)
224*f7c14bbaSAndroid Build Coastguard Worker {
225*f7c14bbaSAndroid Build Coastguard Worker 	struct btf_header hdr = {
226*f7c14bbaSAndroid Build Coastguard Worker 		.magic = BTF_MAGIC,
227*f7c14bbaSAndroid Build Coastguard Worker 		.version = BTF_VERSION,
228*f7c14bbaSAndroid Build Coastguard Worker 		.hdr_len = sizeof(struct btf_header),
229*f7c14bbaSAndroid Build Coastguard Worker 		.type_len = types_len,
230*f7c14bbaSAndroid Build Coastguard Worker 		.str_off = types_len,
231*f7c14bbaSAndroid Build Coastguard Worker 		.str_len = str_len,
232*f7c14bbaSAndroid Build Coastguard Worker 	};
233*f7c14bbaSAndroid Build Coastguard Worker 	LIBBPF_OPTS(bpf_btf_load_opts, opts,
234*f7c14bbaSAndroid Build Coastguard Worker 		.token_fd = token_fd,
235*f7c14bbaSAndroid Build Coastguard Worker 		.btf_flags = token_fd ? BPF_F_TOKEN_FD : 0,
236*f7c14bbaSAndroid Build Coastguard Worker 	);
237*f7c14bbaSAndroid Build Coastguard Worker 	int btf_fd, btf_len;
238*f7c14bbaSAndroid Build Coastguard Worker 	__u8 *raw_btf;
239*f7c14bbaSAndroid Build Coastguard Worker 
240*f7c14bbaSAndroid Build Coastguard Worker 	btf_len = hdr.hdr_len + hdr.type_len + hdr.str_len;
241*f7c14bbaSAndroid Build Coastguard Worker 	raw_btf = malloc(btf_len);
242*f7c14bbaSAndroid Build Coastguard Worker 	if (!raw_btf)
243*f7c14bbaSAndroid Build Coastguard Worker 		return -ENOMEM;
244*f7c14bbaSAndroid Build Coastguard Worker 
245*f7c14bbaSAndroid Build Coastguard Worker 	memcpy(raw_btf, &hdr, sizeof(hdr));
246*f7c14bbaSAndroid Build Coastguard Worker 	memcpy(raw_btf + hdr.hdr_len, raw_types, hdr.type_len);
247*f7c14bbaSAndroid Build Coastguard Worker 	memcpy(raw_btf + hdr.hdr_len + hdr.type_len, str_sec, hdr.str_len);
248*f7c14bbaSAndroid Build Coastguard Worker 
249*f7c14bbaSAndroid Build Coastguard Worker 	btf_fd = bpf_btf_load(raw_btf, btf_len, &opts);
250*f7c14bbaSAndroid Build Coastguard Worker 
251*f7c14bbaSAndroid Build Coastguard Worker 	free(raw_btf);
252*f7c14bbaSAndroid Build Coastguard Worker 	return btf_fd;
253*f7c14bbaSAndroid Build Coastguard Worker }
254*f7c14bbaSAndroid Build Coastguard Worker 
load_local_storage_btf(void)255*f7c14bbaSAndroid Build Coastguard Worker static int load_local_storage_btf(void)
256*f7c14bbaSAndroid Build Coastguard Worker {
257*f7c14bbaSAndroid Build Coastguard Worker 	const char strs[] = "\0bpf_spin_lock\0val\0cnt\0l";
258*f7c14bbaSAndroid Build Coastguard Worker 	/* struct bpf_spin_lock {
259*f7c14bbaSAndroid Build Coastguard Worker 	 *   int val;
260*f7c14bbaSAndroid Build Coastguard Worker 	 * };
261*f7c14bbaSAndroid Build Coastguard Worker 	 * struct val {
262*f7c14bbaSAndroid Build Coastguard Worker 	 *   int cnt;
263*f7c14bbaSAndroid Build Coastguard Worker 	 *   struct bpf_spin_lock l;
264*f7c14bbaSAndroid Build Coastguard Worker 	 * };
265*f7c14bbaSAndroid Build Coastguard Worker 	 */
266*f7c14bbaSAndroid Build Coastguard Worker 	__u32 types[] = {
267*f7c14bbaSAndroid Build Coastguard Worker 		/* int */
268*f7c14bbaSAndroid Build Coastguard Worker 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
269*f7c14bbaSAndroid Build Coastguard Worker 		/* struct bpf_spin_lock */                      /* [2] */
270*f7c14bbaSAndroid Build Coastguard Worker 		BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 4),
271*f7c14bbaSAndroid Build Coastguard Worker 		BTF_MEMBER_ENC(15, 1, 0), /* int val; */
272*f7c14bbaSAndroid Build Coastguard Worker 		/* struct val */                                /* [3] */
273*f7c14bbaSAndroid Build Coastguard Worker 		BTF_TYPE_ENC(15, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
274*f7c14bbaSAndroid Build Coastguard Worker 		BTF_MEMBER_ENC(19, 1, 0), /* int cnt; */
275*f7c14bbaSAndroid Build Coastguard Worker 		BTF_MEMBER_ENC(23, 2, 32),/* struct bpf_spin_lock l; */
276*f7c14bbaSAndroid Build Coastguard Worker 	};
277*f7c14bbaSAndroid Build Coastguard Worker 
278*f7c14bbaSAndroid Build Coastguard Worker 	return libbpf__load_raw_btf((char *)types, sizeof(types),
279*f7c14bbaSAndroid Build Coastguard Worker 				     strs, sizeof(strs), 0);
280*f7c14bbaSAndroid Build Coastguard Worker }
281*f7c14bbaSAndroid Build Coastguard Worker 
probe_map_create(enum bpf_map_type map_type)282*f7c14bbaSAndroid Build Coastguard Worker static int probe_map_create(enum bpf_map_type map_type)
283*f7c14bbaSAndroid Build Coastguard Worker {
284*f7c14bbaSAndroid Build Coastguard Worker 	LIBBPF_OPTS(bpf_map_create_opts, opts);
285*f7c14bbaSAndroid Build Coastguard Worker 	int key_size, value_size, max_entries;
286*f7c14bbaSAndroid Build Coastguard Worker 	__u32 btf_key_type_id = 0, btf_value_type_id = 0;
287*f7c14bbaSAndroid Build Coastguard Worker 	int fd = -1, btf_fd = -1, fd_inner = -1, exp_err = 0, err = 0;
288*f7c14bbaSAndroid Build Coastguard Worker 
289*f7c14bbaSAndroid Build Coastguard Worker 	key_size	= sizeof(__u32);
290*f7c14bbaSAndroid Build Coastguard Worker 	value_size	= sizeof(__u32);
291*f7c14bbaSAndroid Build Coastguard Worker 	max_entries	= 1;
292*f7c14bbaSAndroid Build Coastguard Worker 
293*f7c14bbaSAndroid Build Coastguard Worker 	switch (map_type) {
294*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_MAP_TYPE_STACK_TRACE:
295*f7c14bbaSAndroid Build Coastguard Worker 		value_size	= sizeof(__u64);
296*f7c14bbaSAndroid Build Coastguard Worker 		break;
297*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_MAP_TYPE_LPM_TRIE:
298*f7c14bbaSAndroid Build Coastguard Worker 		key_size	= sizeof(__u64);
299*f7c14bbaSAndroid Build Coastguard Worker 		value_size	= sizeof(__u64);
300*f7c14bbaSAndroid Build Coastguard Worker 		opts.map_flags	= BPF_F_NO_PREALLOC;
301*f7c14bbaSAndroid Build Coastguard Worker 		break;
302*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_MAP_TYPE_CGROUP_STORAGE:
303*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE:
304*f7c14bbaSAndroid Build Coastguard Worker 		key_size	= sizeof(struct bpf_cgroup_storage_key);
305*f7c14bbaSAndroid Build Coastguard Worker 		value_size	= sizeof(__u64);
306*f7c14bbaSAndroid Build Coastguard Worker 		max_entries	= 0;
307*f7c14bbaSAndroid Build Coastguard Worker 		break;
308*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_MAP_TYPE_QUEUE:
309*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_MAP_TYPE_STACK:
310*f7c14bbaSAndroid Build Coastguard Worker 		key_size	= 0;
311*f7c14bbaSAndroid Build Coastguard Worker 		break;
312*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_MAP_TYPE_SK_STORAGE:
313*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_MAP_TYPE_INODE_STORAGE:
314*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_MAP_TYPE_TASK_STORAGE:
315*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_MAP_TYPE_CGRP_STORAGE:
316*f7c14bbaSAndroid Build Coastguard Worker 		btf_key_type_id = 1;
317*f7c14bbaSAndroid Build Coastguard Worker 		btf_value_type_id = 3;
318*f7c14bbaSAndroid Build Coastguard Worker 		value_size = 8;
319*f7c14bbaSAndroid Build Coastguard Worker 		max_entries = 0;
320*f7c14bbaSAndroid Build Coastguard Worker 		opts.map_flags = BPF_F_NO_PREALLOC;
321*f7c14bbaSAndroid Build Coastguard Worker 		btf_fd = load_local_storage_btf();
322*f7c14bbaSAndroid Build Coastguard Worker 		if (btf_fd < 0)
323*f7c14bbaSAndroid Build Coastguard Worker 			return btf_fd;
324*f7c14bbaSAndroid Build Coastguard Worker 		break;
325*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_MAP_TYPE_RINGBUF:
326*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_MAP_TYPE_USER_RINGBUF:
327*f7c14bbaSAndroid Build Coastguard Worker 		key_size = 0;
328*f7c14bbaSAndroid Build Coastguard Worker 		value_size = 0;
329*f7c14bbaSAndroid Build Coastguard Worker 		max_entries = sysconf(_SC_PAGE_SIZE);
330*f7c14bbaSAndroid Build Coastguard Worker 		break;
331*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_MAP_TYPE_STRUCT_OPS:
332*f7c14bbaSAndroid Build Coastguard Worker 		/* we'll get -ENOTSUPP for invalid BTF type ID for struct_ops */
333*f7c14bbaSAndroid Build Coastguard Worker 		opts.btf_vmlinux_value_type_id = 1;
334*f7c14bbaSAndroid Build Coastguard Worker 		opts.value_type_btf_obj_fd = -1;
335*f7c14bbaSAndroid Build Coastguard Worker 		exp_err = -524; /* -ENOTSUPP */
336*f7c14bbaSAndroid Build Coastguard Worker 		break;
337*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_MAP_TYPE_BLOOM_FILTER:
338*f7c14bbaSAndroid Build Coastguard Worker 		key_size = 0;
339*f7c14bbaSAndroid Build Coastguard Worker 		max_entries = 1;
340*f7c14bbaSAndroid Build Coastguard Worker 		break;
341*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_MAP_TYPE_ARENA:
342*f7c14bbaSAndroid Build Coastguard Worker 		key_size	= 0;
343*f7c14bbaSAndroid Build Coastguard Worker 		value_size	= 0;
344*f7c14bbaSAndroid Build Coastguard Worker 		max_entries	= 1; /* one page */
345*f7c14bbaSAndroid Build Coastguard Worker 		opts.map_extra	= 0; /* can mmap() at any address */
346*f7c14bbaSAndroid Build Coastguard Worker 		opts.map_flags	= BPF_F_MMAPABLE;
347*f7c14bbaSAndroid Build Coastguard Worker 		break;
348*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_MAP_TYPE_HASH:
349*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_MAP_TYPE_ARRAY:
350*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_MAP_TYPE_PROG_ARRAY:
351*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
352*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_MAP_TYPE_PERCPU_HASH:
353*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_MAP_TYPE_PERCPU_ARRAY:
354*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_MAP_TYPE_CGROUP_ARRAY:
355*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_MAP_TYPE_LRU_HASH:
356*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_MAP_TYPE_LRU_PERCPU_HASH:
357*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_MAP_TYPE_ARRAY_OF_MAPS:
358*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_MAP_TYPE_HASH_OF_MAPS:
359*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_MAP_TYPE_DEVMAP:
360*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_MAP_TYPE_DEVMAP_HASH:
361*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_MAP_TYPE_SOCKMAP:
362*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_MAP_TYPE_CPUMAP:
363*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_MAP_TYPE_XSKMAP:
364*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_MAP_TYPE_SOCKHASH:
365*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY:
366*f7c14bbaSAndroid Build Coastguard Worker 		break;
367*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_MAP_TYPE_UNSPEC:
368*f7c14bbaSAndroid Build Coastguard Worker 	default:
369*f7c14bbaSAndroid Build Coastguard Worker 		return -EOPNOTSUPP;
370*f7c14bbaSAndroid Build Coastguard Worker 	}
371*f7c14bbaSAndroid Build Coastguard Worker 
372*f7c14bbaSAndroid Build Coastguard Worker 	if (map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
373*f7c14bbaSAndroid Build Coastguard Worker 	    map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
374*f7c14bbaSAndroid Build Coastguard Worker 		fd_inner = bpf_map_create(BPF_MAP_TYPE_HASH, NULL,
375*f7c14bbaSAndroid Build Coastguard Worker 					  sizeof(__u32), sizeof(__u32), 1, NULL);
376*f7c14bbaSAndroid Build Coastguard Worker 		if (fd_inner < 0)
377*f7c14bbaSAndroid Build Coastguard Worker 			goto cleanup;
378*f7c14bbaSAndroid Build Coastguard Worker 
379*f7c14bbaSAndroid Build Coastguard Worker 		opts.inner_map_fd = fd_inner;
380*f7c14bbaSAndroid Build Coastguard Worker 	}
381*f7c14bbaSAndroid Build Coastguard Worker 
382*f7c14bbaSAndroid Build Coastguard Worker 	if (btf_fd >= 0) {
383*f7c14bbaSAndroid Build Coastguard Worker 		opts.btf_fd = btf_fd;
384*f7c14bbaSAndroid Build Coastguard Worker 		opts.btf_key_type_id = btf_key_type_id;
385*f7c14bbaSAndroid Build Coastguard Worker 		opts.btf_value_type_id = btf_value_type_id;
386*f7c14bbaSAndroid Build Coastguard Worker 	}
387*f7c14bbaSAndroid Build Coastguard Worker 
388*f7c14bbaSAndroid Build Coastguard Worker 	fd = bpf_map_create(map_type, NULL, key_size, value_size, max_entries, &opts);
389*f7c14bbaSAndroid Build Coastguard Worker 	err = -errno;
390*f7c14bbaSAndroid Build Coastguard Worker 
391*f7c14bbaSAndroid Build Coastguard Worker cleanup:
392*f7c14bbaSAndroid Build Coastguard Worker 	if (fd >= 0)
393*f7c14bbaSAndroid Build Coastguard Worker 		close(fd);
394*f7c14bbaSAndroid Build Coastguard Worker 	if (fd_inner >= 0)
395*f7c14bbaSAndroid Build Coastguard Worker 		close(fd_inner);
396*f7c14bbaSAndroid Build Coastguard Worker 	if (btf_fd >= 0)
397*f7c14bbaSAndroid Build Coastguard Worker 		close(btf_fd);
398*f7c14bbaSAndroid Build Coastguard Worker 
399*f7c14bbaSAndroid Build Coastguard Worker 	if (exp_err)
400*f7c14bbaSAndroid Build Coastguard Worker 		return fd < 0 && err == exp_err ? 1 : 0;
401*f7c14bbaSAndroid Build Coastguard Worker 	else
402*f7c14bbaSAndroid Build Coastguard Worker 		return fd >= 0 ? 1 : 0;
403*f7c14bbaSAndroid Build Coastguard Worker }
404*f7c14bbaSAndroid Build Coastguard Worker 
libbpf_probe_bpf_map_type(enum bpf_map_type map_type,const void * opts)405*f7c14bbaSAndroid Build Coastguard Worker int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts)
406*f7c14bbaSAndroid Build Coastguard Worker {
407*f7c14bbaSAndroid Build Coastguard Worker 	int ret;
408*f7c14bbaSAndroid Build Coastguard Worker 
409*f7c14bbaSAndroid Build Coastguard Worker 	if (opts)
410*f7c14bbaSAndroid Build Coastguard Worker 		return libbpf_err(-EINVAL);
411*f7c14bbaSAndroid Build Coastguard Worker 
412*f7c14bbaSAndroid Build Coastguard Worker 	ret = probe_map_create(map_type);
413*f7c14bbaSAndroid Build Coastguard Worker 	return libbpf_err(ret);
414*f7c14bbaSAndroid Build Coastguard Worker }
415*f7c14bbaSAndroid Build Coastguard Worker 
libbpf_probe_bpf_helper(enum bpf_prog_type prog_type,enum bpf_func_id helper_id,const void * opts)416*f7c14bbaSAndroid Build Coastguard Worker int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type, enum bpf_func_id helper_id,
417*f7c14bbaSAndroid Build Coastguard Worker 			    const void *opts)
418*f7c14bbaSAndroid Build Coastguard Worker {
419*f7c14bbaSAndroid Build Coastguard Worker 	struct bpf_insn insns[] = {
420*f7c14bbaSAndroid Build Coastguard Worker 		BPF_EMIT_CALL((__u32)helper_id),
421*f7c14bbaSAndroid Build Coastguard Worker 		BPF_EXIT_INSN(),
422*f7c14bbaSAndroid Build Coastguard Worker 	};
423*f7c14bbaSAndroid Build Coastguard Worker 	const size_t insn_cnt = ARRAY_SIZE(insns);
424*f7c14bbaSAndroid Build Coastguard Worker 	char buf[4096];
425*f7c14bbaSAndroid Build Coastguard Worker 	int ret;
426*f7c14bbaSAndroid Build Coastguard Worker 
427*f7c14bbaSAndroid Build Coastguard Worker 	if (opts)
428*f7c14bbaSAndroid Build Coastguard Worker 		return libbpf_err(-EINVAL);
429*f7c14bbaSAndroid Build Coastguard Worker 
430*f7c14bbaSAndroid Build Coastguard Worker 	/* we can't successfully load all prog types to check for BPF helper
431*f7c14bbaSAndroid Build Coastguard Worker 	 * support, so bail out with -EOPNOTSUPP error
432*f7c14bbaSAndroid Build Coastguard Worker 	 */
433*f7c14bbaSAndroid Build Coastguard Worker 	switch (prog_type) {
434*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_TRACING:
435*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_EXT:
436*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_LSM:
437*f7c14bbaSAndroid Build Coastguard Worker 	case BPF_PROG_TYPE_STRUCT_OPS:
438*f7c14bbaSAndroid Build Coastguard Worker 		return -EOPNOTSUPP;
439*f7c14bbaSAndroid Build Coastguard Worker 	default:
440*f7c14bbaSAndroid Build Coastguard Worker 		break;
441*f7c14bbaSAndroid Build Coastguard Worker 	}
442*f7c14bbaSAndroid Build Coastguard Worker 
443*f7c14bbaSAndroid Build Coastguard Worker 	buf[0] = '\0';
444*f7c14bbaSAndroid Build Coastguard Worker 	ret = probe_prog_load(prog_type, insns, insn_cnt, buf, sizeof(buf));
445*f7c14bbaSAndroid Build Coastguard Worker 	if (ret < 0)
446*f7c14bbaSAndroid Build Coastguard Worker 		return libbpf_err(ret);
447*f7c14bbaSAndroid Build Coastguard Worker 
448*f7c14bbaSAndroid Build Coastguard Worker 	/* If BPF verifier doesn't recognize BPF helper ID (enum bpf_func_id)
449*f7c14bbaSAndroid Build Coastguard Worker 	 * at all, it will emit something like "invalid func unknown#181".
450*f7c14bbaSAndroid Build Coastguard Worker 	 * If BPF verifier recognizes BPF helper but it's not supported for
451*f7c14bbaSAndroid Build Coastguard Worker 	 * given BPF program type, it will emit "unknown func bpf_sys_bpf#166".
452*f7c14bbaSAndroid Build Coastguard Worker 	 * In both cases, provided combination of BPF program type and BPF
453*f7c14bbaSAndroid Build Coastguard Worker 	 * helper is not supported by the kernel.
454*f7c14bbaSAndroid Build Coastguard Worker 	 * In all other cases, probe_prog_load() above will either succeed (e.g.,
455*f7c14bbaSAndroid Build Coastguard Worker 	 * because BPF helper happens to accept no input arguments or it
456*f7c14bbaSAndroid Build Coastguard Worker 	 * accepts one input argument and initial PTR_TO_CTX is fine for
457*f7c14bbaSAndroid Build Coastguard Worker 	 * that), or we'll get some more specific BPF verifier error about
458*f7c14bbaSAndroid Build Coastguard Worker 	 * some unsatisfied conditions.
459*f7c14bbaSAndroid Build Coastguard Worker 	 */
460*f7c14bbaSAndroid Build Coastguard Worker 	if (ret == 0 && (strstr(buf, "invalid func ") || strstr(buf, "unknown func ")))
461*f7c14bbaSAndroid Build Coastguard Worker 		return 0;
462*f7c14bbaSAndroid Build Coastguard Worker 	return 1; /* assume supported */
463*f7c14bbaSAndroid Build Coastguard Worker }
464