xref: /openbmc/linux/tools/lib/bpf/libbpf_probes.c (revision 5b628549)
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/filter.h>
13 #include <linux/kernel.h>
14 
15 #include "bpf.h"
16 #include "libbpf.h"
17 
18 static bool grep(const char *buffer, const char *pattern)
19 {
20 	return !!strstr(buffer, pattern);
21 }
22 
23 static int get_vendor_id(int ifindex)
24 {
25 	char ifname[IF_NAMESIZE], path[64], buf[8];
26 	ssize_t len;
27 	int fd;
28 
29 	if (!if_indextoname(ifindex, ifname))
30 		return -1;
31 
32 	snprintf(path, sizeof(path), "/sys/class/net/%s/device/vendor", ifname);
33 
34 	fd = open(path, O_RDONLY);
35 	if (fd < 0)
36 		return -1;
37 
38 	len = read(fd, buf, sizeof(buf));
39 	close(fd);
40 	if (len < 0)
41 		return -1;
42 	if (len >= (ssize_t)sizeof(buf))
43 		return -1;
44 	buf[len] = '\0';
45 
46 	return strtol(buf, NULL, 0);
47 }
48 
49 static int get_kernel_version(void)
50 {
51 	int version, subversion, patchlevel;
52 	struct utsname utsn;
53 
54 	/* Return 0 on failure, and attempt to probe with empty kversion */
55 	if (uname(&utsn))
56 		return 0;
57 
58 	if (sscanf(utsn.release, "%d.%d.%d",
59 		   &version, &subversion, &patchlevel) != 3)
60 		return 0;
61 
62 	return (version << 16) + (subversion << 8) + patchlevel;
63 }
64 
65 static void
66 probe_load(enum bpf_prog_type prog_type, const struct bpf_insn *insns,
67 	   size_t insns_cnt, char *buf, size_t buf_len, __u32 ifindex)
68 {
69 	struct bpf_load_program_attr xattr = {};
70 	int fd;
71 
72 	switch (prog_type) {
73 	case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
74 		xattr.expected_attach_type = BPF_CGROUP_INET4_CONNECT;
75 		break;
76 	case BPF_PROG_TYPE_KPROBE:
77 		xattr.kern_version = get_kernel_version();
78 		break;
79 	case BPF_PROG_TYPE_UNSPEC:
80 	case BPF_PROG_TYPE_SOCKET_FILTER:
81 	case BPF_PROG_TYPE_SCHED_CLS:
82 	case BPF_PROG_TYPE_SCHED_ACT:
83 	case BPF_PROG_TYPE_TRACEPOINT:
84 	case BPF_PROG_TYPE_XDP:
85 	case BPF_PROG_TYPE_PERF_EVENT:
86 	case BPF_PROG_TYPE_CGROUP_SKB:
87 	case BPF_PROG_TYPE_CGROUP_SOCK:
88 	case BPF_PROG_TYPE_LWT_IN:
89 	case BPF_PROG_TYPE_LWT_OUT:
90 	case BPF_PROG_TYPE_LWT_XMIT:
91 	case BPF_PROG_TYPE_SOCK_OPS:
92 	case BPF_PROG_TYPE_SK_SKB:
93 	case BPF_PROG_TYPE_CGROUP_DEVICE:
94 	case BPF_PROG_TYPE_SK_MSG:
95 	case BPF_PROG_TYPE_RAW_TRACEPOINT:
96 	case BPF_PROG_TYPE_LWT_SEG6LOCAL:
97 	case BPF_PROG_TYPE_LIRC_MODE2:
98 	case BPF_PROG_TYPE_SK_REUSEPORT:
99 	case BPF_PROG_TYPE_FLOW_DISSECTOR:
100 	default:
101 		break;
102 	}
103 
104 	xattr.prog_type = prog_type;
105 	xattr.insns = insns;
106 	xattr.insns_cnt = insns_cnt;
107 	xattr.license = "GPL";
108 	xattr.prog_ifindex = ifindex;
109 
110 	fd = bpf_load_program_xattr(&xattr, buf, buf_len);
111 	if (fd >= 0)
112 		close(fd);
113 }
114 
115 bool bpf_probe_prog_type(enum bpf_prog_type prog_type, __u32 ifindex)
116 {
117 	struct bpf_insn insns[2] = {
118 		BPF_MOV64_IMM(BPF_REG_0, 0),
119 		BPF_EXIT_INSN()
120 	};
121 
122 	if (ifindex && prog_type == BPF_PROG_TYPE_SCHED_CLS)
123 		/* nfp returns -EINVAL on exit(0) with TC offload */
124 		insns[0].imm = 2;
125 
126 	errno = 0;
127 	probe_load(prog_type, insns, ARRAY_SIZE(insns), NULL, 0, ifindex);
128 
129 	return errno != EINVAL && errno != EOPNOTSUPP;
130 }
131 
132 bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex)
133 {
134 	int key_size, value_size, max_entries, map_flags;
135 	struct bpf_create_map_attr attr = {};
136 	int fd = -1, fd_inner;
137 
138 	key_size	= sizeof(__u32);
139 	value_size	= sizeof(__u32);
140 	max_entries	= 1;
141 	map_flags	= 0;
142 
143 	switch (map_type) {
144 	case BPF_MAP_TYPE_STACK_TRACE:
145 		value_size	= sizeof(__u64);
146 		break;
147 	case BPF_MAP_TYPE_LPM_TRIE:
148 		key_size	= sizeof(__u64);
149 		value_size	= sizeof(__u64);
150 		map_flags	= BPF_F_NO_PREALLOC;
151 		break;
152 	case BPF_MAP_TYPE_CGROUP_STORAGE:
153 	case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE:
154 		key_size	= sizeof(struct bpf_cgroup_storage_key);
155 		value_size	= sizeof(__u64);
156 		max_entries	= 0;
157 		break;
158 	case BPF_MAP_TYPE_QUEUE:
159 	case BPF_MAP_TYPE_STACK:
160 		key_size	= 0;
161 		break;
162 	case BPF_MAP_TYPE_UNSPEC:
163 	case BPF_MAP_TYPE_HASH:
164 	case BPF_MAP_TYPE_ARRAY:
165 	case BPF_MAP_TYPE_PROG_ARRAY:
166 	case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
167 	case BPF_MAP_TYPE_PERCPU_HASH:
168 	case BPF_MAP_TYPE_PERCPU_ARRAY:
169 	case BPF_MAP_TYPE_CGROUP_ARRAY:
170 	case BPF_MAP_TYPE_LRU_HASH:
171 	case BPF_MAP_TYPE_LRU_PERCPU_HASH:
172 	case BPF_MAP_TYPE_ARRAY_OF_MAPS:
173 	case BPF_MAP_TYPE_HASH_OF_MAPS:
174 	case BPF_MAP_TYPE_DEVMAP:
175 	case BPF_MAP_TYPE_SOCKMAP:
176 	case BPF_MAP_TYPE_CPUMAP:
177 	case BPF_MAP_TYPE_XSKMAP:
178 	case BPF_MAP_TYPE_SOCKHASH:
179 	case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY:
180 	default:
181 		break;
182 	}
183 
184 	if (map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
185 	    map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
186 		/* TODO: probe for device, once libbpf has a function to create
187 		 * map-in-map for offload
188 		 */
189 		if (ifindex)
190 			return false;
191 
192 		fd_inner = bpf_create_map(BPF_MAP_TYPE_HASH,
193 					  sizeof(__u32), sizeof(__u32), 1, 0);
194 		if (fd_inner < 0)
195 			return false;
196 		fd = bpf_create_map_in_map(map_type, NULL, sizeof(__u32),
197 					   fd_inner, 1, 0);
198 		close(fd_inner);
199 	} else {
200 		/* Note: No other restriction on map type probes for offload */
201 		attr.map_type = map_type;
202 		attr.key_size = key_size;
203 		attr.value_size = value_size;
204 		attr.max_entries = max_entries;
205 		attr.map_flags = map_flags;
206 		attr.map_ifindex = ifindex;
207 
208 		fd = bpf_create_map_xattr(&attr);
209 	}
210 	if (fd >= 0)
211 		close(fd);
212 
213 	return fd >= 0;
214 }
215 
216 bool bpf_probe_helper(enum bpf_func_id id, enum bpf_prog_type prog_type,
217 		      __u32 ifindex)
218 {
219 	struct bpf_insn insns[2] = {
220 		BPF_EMIT_CALL(id),
221 		BPF_EXIT_INSN()
222 	};
223 	char buf[4096] = {};
224 	bool res;
225 
226 	probe_load(prog_type, insns, ARRAY_SIZE(insns), buf, sizeof(buf),
227 		   ifindex);
228 	res = !grep(buf, "invalid func ") && !grep(buf, "unknown func ");
229 
230 	if (ifindex) {
231 		switch (get_vendor_id(ifindex)) {
232 		case 0x19ee: /* Netronome specific */
233 			res = res && !grep(buf, "not supported by FW") &&
234 				!grep(buf, "unsupported function id");
235 			break;
236 		default:
237 			break;
238 		}
239 	}
240 
241 	return res;
242 }
243