xref: /openbmc/linux/tools/lib/bpf/libbpf_probes.c (revision 2874c5fd)
1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 /* Copyright (c) 2019 Netronome Systems, Inc. */
3 
4 #include <errno.h>
5 #include <fcntl.h>
6 #include <string.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <net/if.h>
10 #include <sys/utsname.h>
11 
12 #include <linux/btf.h>
13 #include <linux/filter.h>
14 #include <linux/kernel.h>
15 
16 #include "bpf.h"
17 #include "libbpf.h"
18 #include "libbpf_internal.h"
19 
20 static bool grep(const char *buffer, const char *pattern)
21 {
22 	return !!strstr(buffer, pattern);
23 }
24 
25 static int get_vendor_id(int ifindex)
26 {
27 	char ifname[IF_NAMESIZE], path[64], buf[8];
28 	ssize_t len;
29 	int fd;
30 
31 	if (!if_indextoname(ifindex, ifname))
32 		return -1;
33 
34 	snprintf(path, sizeof(path), "/sys/class/net/%s/device/vendor", ifname);
35 
36 	fd = open(path, O_RDONLY);
37 	if (fd < 0)
38 		return -1;
39 
40 	len = read(fd, buf, sizeof(buf));
41 	close(fd);
42 	if (len < 0)
43 		return -1;
44 	if (len >= (ssize_t)sizeof(buf))
45 		return -1;
46 	buf[len] = '\0';
47 
48 	return strtol(buf, NULL, 0);
49 }
50 
51 static int get_kernel_version(void)
52 {
53 	int version, subversion, patchlevel;
54 	struct utsname utsn;
55 
56 	/* Return 0 on failure, and attempt to probe with empty kversion */
57 	if (uname(&utsn))
58 		return 0;
59 
60 	if (sscanf(utsn.release, "%d.%d.%d",
61 		   &version, &subversion, &patchlevel) != 3)
62 		return 0;
63 
64 	return (version << 16) + (subversion << 8) + patchlevel;
65 }
66 
67 static void
68 probe_load(enum bpf_prog_type prog_type, const struct bpf_insn *insns,
69 	   size_t insns_cnt, char *buf, size_t buf_len, __u32 ifindex)
70 {
71 	struct bpf_load_program_attr xattr = {};
72 	int fd;
73 
74 	switch (prog_type) {
75 	case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
76 		xattr.expected_attach_type = BPF_CGROUP_INET4_CONNECT;
77 		break;
78 	case BPF_PROG_TYPE_KPROBE:
79 		xattr.kern_version = get_kernel_version();
80 		break;
81 	case BPF_PROG_TYPE_UNSPEC:
82 	case BPF_PROG_TYPE_SOCKET_FILTER:
83 	case BPF_PROG_TYPE_SCHED_CLS:
84 	case BPF_PROG_TYPE_SCHED_ACT:
85 	case BPF_PROG_TYPE_TRACEPOINT:
86 	case BPF_PROG_TYPE_XDP:
87 	case BPF_PROG_TYPE_PERF_EVENT:
88 	case BPF_PROG_TYPE_CGROUP_SKB:
89 	case BPF_PROG_TYPE_CGROUP_SOCK:
90 	case BPF_PROG_TYPE_LWT_IN:
91 	case BPF_PROG_TYPE_LWT_OUT:
92 	case BPF_PROG_TYPE_LWT_XMIT:
93 	case BPF_PROG_TYPE_SOCK_OPS:
94 	case BPF_PROG_TYPE_SK_SKB:
95 	case BPF_PROG_TYPE_CGROUP_DEVICE:
96 	case BPF_PROG_TYPE_SK_MSG:
97 	case BPF_PROG_TYPE_RAW_TRACEPOINT:
98 	case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
99 	case BPF_PROG_TYPE_LWT_SEG6LOCAL:
100 	case BPF_PROG_TYPE_LIRC_MODE2:
101 	case BPF_PROG_TYPE_SK_REUSEPORT:
102 	case BPF_PROG_TYPE_FLOW_DISSECTOR:
103 	case BPF_PROG_TYPE_CGROUP_SYSCTL:
104 	default:
105 		break;
106 	}
107 
108 	xattr.prog_type = prog_type;
109 	xattr.insns = insns;
110 	xattr.insns_cnt = insns_cnt;
111 	xattr.license = "GPL";
112 	xattr.prog_ifindex = ifindex;
113 
114 	fd = bpf_load_program_xattr(&xattr, buf, buf_len);
115 	if (fd >= 0)
116 		close(fd);
117 }
118 
119 bool bpf_probe_prog_type(enum bpf_prog_type prog_type, __u32 ifindex)
120 {
121 	struct bpf_insn insns[2] = {
122 		BPF_MOV64_IMM(BPF_REG_0, 0),
123 		BPF_EXIT_INSN()
124 	};
125 
126 	if (ifindex && prog_type == BPF_PROG_TYPE_SCHED_CLS)
127 		/* nfp returns -EINVAL on exit(0) with TC offload */
128 		insns[0].imm = 2;
129 
130 	errno = 0;
131 	probe_load(prog_type, insns, ARRAY_SIZE(insns), NULL, 0, ifindex);
132 
133 	return errno != EINVAL && errno != EOPNOTSUPP;
134 }
135 
136 int libbpf__probe_raw_btf(const char *raw_types, size_t types_len,
137 			  const char *str_sec, size_t str_len)
138 {
139 	struct btf_header hdr = {
140 		.magic = BTF_MAGIC,
141 		.version = BTF_VERSION,
142 		.hdr_len = sizeof(struct btf_header),
143 		.type_len = types_len,
144 		.str_off = types_len,
145 		.str_len = str_len,
146 	};
147 	int btf_fd, btf_len;
148 	__u8 *raw_btf;
149 
150 	btf_len = hdr.hdr_len + hdr.type_len + hdr.str_len;
151 	raw_btf = malloc(btf_len);
152 	if (!raw_btf)
153 		return -ENOMEM;
154 
155 	memcpy(raw_btf, &hdr, sizeof(hdr));
156 	memcpy(raw_btf + hdr.hdr_len, raw_types, hdr.type_len);
157 	memcpy(raw_btf + hdr.hdr_len + hdr.type_len, str_sec, hdr.str_len);
158 
159 	btf_fd = bpf_load_btf(raw_btf, btf_len, NULL, 0, false);
160 	if (btf_fd < 0) {
161 		free(raw_btf);
162 		return 0;
163 	}
164 
165 	close(btf_fd);
166 	free(raw_btf);
167 	return 1;
168 }
169 
170 static int load_sk_storage_btf(void)
171 {
172 	const char strs[] = "\0bpf_spin_lock\0val\0cnt\0l";
173 	/* struct bpf_spin_lock {
174 	 *   int val;
175 	 * };
176 	 * struct val {
177 	 *   int cnt;
178 	 *   struct bpf_spin_lock l;
179 	 * };
180 	 */
181 	__u32 types[] = {
182 		/* int */
183 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
184 		/* struct bpf_spin_lock */                      /* [2] */
185 		BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 4),
186 		BTF_MEMBER_ENC(15, 1, 0), /* int val; */
187 		/* struct val */                                /* [3] */
188 		BTF_TYPE_ENC(15, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
189 		BTF_MEMBER_ENC(19, 1, 0), /* int cnt; */
190 		BTF_MEMBER_ENC(23, 2, 32),/* struct bpf_spin_lock l; */
191 	};
192 
193 	return libbpf__probe_raw_btf((char *)types, sizeof(types),
194 				     strs, sizeof(strs));
195 }
196 
197 bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex)
198 {
199 	int key_size, value_size, max_entries, map_flags;
200 	__u32 btf_key_type_id = 0, btf_value_type_id = 0;
201 	struct bpf_create_map_attr attr = {};
202 	int fd = -1, btf_fd = -1, fd_inner;
203 
204 	key_size	= sizeof(__u32);
205 	value_size	= sizeof(__u32);
206 	max_entries	= 1;
207 	map_flags	= 0;
208 
209 	switch (map_type) {
210 	case BPF_MAP_TYPE_STACK_TRACE:
211 		value_size	= sizeof(__u64);
212 		break;
213 	case BPF_MAP_TYPE_LPM_TRIE:
214 		key_size	= sizeof(__u64);
215 		value_size	= sizeof(__u64);
216 		map_flags	= BPF_F_NO_PREALLOC;
217 		break;
218 	case BPF_MAP_TYPE_CGROUP_STORAGE:
219 	case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE:
220 		key_size	= sizeof(struct bpf_cgroup_storage_key);
221 		value_size	= sizeof(__u64);
222 		max_entries	= 0;
223 		break;
224 	case BPF_MAP_TYPE_QUEUE:
225 	case BPF_MAP_TYPE_STACK:
226 		key_size	= 0;
227 		break;
228 	case BPF_MAP_TYPE_SK_STORAGE:
229 		btf_key_type_id = 1;
230 		btf_value_type_id = 3;
231 		value_size = 8;
232 		max_entries = 0;
233 		map_flags = BPF_F_NO_PREALLOC;
234 		btf_fd = load_sk_storage_btf();
235 		if (btf_fd < 0)
236 			return false;
237 		break;
238 	case BPF_MAP_TYPE_UNSPEC:
239 	case BPF_MAP_TYPE_HASH:
240 	case BPF_MAP_TYPE_ARRAY:
241 	case BPF_MAP_TYPE_PROG_ARRAY:
242 	case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
243 	case BPF_MAP_TYPE_PERCPU_HASH:
244 	case BPF_MAP_TYPE_PERCPU_ARRAY:
245 	case BPF_MAP_TYPE_CGROUP_ARRAY:
246 	case BPF_MAP_TYPE_LRU_HASH:
247 	case BPF_MAP_TYPE_LRU_PERCPU_HASH:
248 	case BPF_MAP_TYPE_ARRAY_OF_MAPS:
249 	case BPF_MAP_TYPE_HASH_OF_MAPS:
250 	case BPF_MAP_TYPE_DEVMAP:
251 	case BPF_MAP_TYPE_SOCKMAP:
252 	case BPF_MAP_TYPE_CPUMAP:
253 	case BPF_MAP_TYPE_XSKMAP:
254 	case BPF_MAP_TYPE_SOCKHASH:
255 	case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY:
256 	default:
257 		break;
258 	}
259 
260 	if (map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
261 	    map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
262 		/* TODO: probe for device, once libbpf has a function to create
263 		 * map-in-map for offload
264 		 */
265 		if (ifindex)
266 			return false;
267 
268 		fd_inner = bpf_create_map(BPF_MAP_TYPE_HASH,
269 					  sizeof(__u32), sizeof(__u32), 1, 0);
270 		if (fd_inner < 0)
271 			return false;
272 		fd = bpf_create_map_in_map(map_type, NULL, sizeof(__u32),
273 					   fd_inner, 1, 0);
274 		close(fd_inner);
275 	} else {
276 		/* Note: No other restriction on map type probes for offload */
277 		attr.map_type = map_type;
278 		attr.key_size = key_size;
279 		attr.value_size = value_size;
280 		attr.max_entries = max_entries;
281 		attr.map_flags = map_flags;
282 		attr.map_ifindex = ifindex;
283 		if (btf_fd >= 0) {
284 			attr.btf_fd = btf_fd;
285 			attr.btf_key_type_id = btf_key_type_id;
286 			attr.btf_value_type_id = btf_value_type_id;
287 		}
288 
289 		fd = bpf_create_map_xattr(&attr);
290 	}
291 	if (fd >= 0)
292 		close(fd);
293 	if (btf_fd >= 0)
294 		close(btf_fd);
295 
296 	return fd >= 0;
297 }
298 
299 bool bpf_probe_helper(enum bpf_func_id id, enum bpf_prog_type prog_type,
300 		      __u32 ifindex)
301 {
302 	struct bpf_insn insns[2] = {
303 		BPF_EMIT_CALL(id),
304 		BPF_EXIT_INSN()
305 	};
306 	char buf[4096] = {};
307 	bool res;
308 
309 	probe_load(prog_type, insns, ARRAY_SIZE(insns), buf, sizeof(buf),
310 		   ifindex);
311 	res = !grep(buf, "invalid func ") && !grep(buf, "unknown func ");
312 
313 	if (ifindex) {
314 		switch (get_vendor_id(ifindex)) {
315 		case 0x19ee: /* Netronome specific */
316 			res = res && !grep(buf, "not supported by FW") &&
317 				!grep(buf, "unsupported function id");
318 			break;
319 		default:
320 			break;
321 		}
322 	}
323 
324 	return res;
325 }
326