1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 /* Copyright (c) 2019 Netronome Systems, Inc. */
10 #include <sys/utsname.h>
12 #include <linux/btf.h>
13 #include <linux/filter.h>
14 #include <linux/kernel.h>
18 #include "libbpf_internal.h"
20 static bool grep(const char *buffer, const char *pattern)
22 return !!strstr(buffer, pattern);
25 static int get_vendor_id(int ifindex)
27 char ifname[IF_NAMESIZE], path[64], buf[8];
31 if (!if_indextoname(ifindex, ifname))
34 snprintf(path, sizeof(path), "/sys/class/net/%s/device/vendor", ifname);
36 fd = open(path, O_RDONLY);
40 len = read(fd, buf, sizeof(buf));
44 if (len >= (ssize_t)sizeof(buf))
48 return strtol(buf, NULL, 0);
51 static int get_kernel_version(void)
53 int version, subversion, patchlevel;
56 /* Return 0 on failure, and attempt to probe with empty kversion */
60 if (sscanf(utsn.release, "%d.%d.%d",
61 &version, &subversion, &patchlevel) != 3)
64 return (version << 16) + (subversion << 8) + patchlevel;
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)
71 struct bpf_load_program_attr xattr = {};
75 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
76 xattr.expected_attach_type = BPF_CGROUP_INET4_CONNECT;
78 case BPF_PROG_TYPE_KPROBE:
79 xattr.kern_version = get_kernel_version();
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 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
105 case BPF_PROG_TYPE_TRACING:
110 xattr.prog_type = prog_type;
112 xattr.insns_cnt = insns_cnt;
113 xattr.license = "GPL";
114 xattr.prog_ifindex = ifindex;
116 fd = bpf_load_program_xattr(&xattr, buf, buf_len);
121 bool bpf_probe_prog_type(enum bpf_prog_type prog_type, __u32 ifindex)
123 struct bpf_insn insns[2] = {
124 BPF_MOV64_IMM(BPF_REG_0, 0),
128 if (ifindex && prog_type == BPF_PROG_TYPE_SCHED_CLS)
129 /* nfp returns -EINVAL on exit(0) with TC offload */
133 probe_load(prog_type, insns, ARRAY_SIZE(insns), NULL, 0, ifindex);
135 return errno != EINVAL && errno != EOPNOTSUPP;
138 int libbpf__load_raw_btf(const char *raw_types, size_t types_len,
139 const char *str_sec, size_t str_len)
141 struct btf_header hdr = {
143 .version = BTF_VERSION,
144 .hdr_len = sizeof(struct btf_header),
145 .type_len = types_len,
146 .str_off = types_len,
152 btf_len = hdr.hdr_len + hdr.type_len + hdr.str_len;
153 raw_btf = malloc(btf_len);
157 memcpy(raw_btf, &hdr, sizeof(hdr));
158 memcpy(raw_btf + hdr.hdr_len, raw_types, hdr.type_len);
159 memcpy(raw_btf + hdr.hdr_len + hdr.type_len, str_sec, hdr.str_len);
161 btf_fd = bpf_load_btf(raw_btf, btf_len, NULL, 0, false);
167 static int load_sk_storage_btf(void)
169 const char strs[] = "\0bpf_spin_lock\0val\0cnt\0l";
170 /* struct bpf_spin_lock {
175 * struct bpf_spin_lock l;
180 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
181 /* struct bpf_spin_lock */ /* [2] */
182 BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 4),
183 BTF_MEMBER_ENC(15, 1, 0), /* int val; */
184 /* struct val */ /* [3] */
185 BTF_TYPE_ENC(15, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
186 BTF_MEMBER_ENC(19, 1, 0), /* int cnt; */
187 BTF_MEMBER_ENC(23, 2, 32),/* struct bpf_spin_lock l; */
190 return libbpf__load_raw_btf((char *)types, sizeof(types),
194 bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex)
196 int key_size, value_size, max_entries, map_flags;
197 __u32 btf_key_type_id = 0, btf_value_type_id = 0;
198 struct bpf_create_map_attr attr = {};
199 int fd = -1, btf_fd = -1, fd_inner;
201 key_size = sizeof(__u32);
202 value_size = sizeof(__u32);
207 case BPF_MAP_TYPE_STACK_TRACE:
208 value_size = sizeof(__u64);
210 case BPF_MAP_TYPE_LPM_TRIE:
211 key_size = sizeof(__u64);
212 value_size = sizeof(__u64);
213 map_flags = BPF_F_NO_PREALLOC;
215 case BPF_MAP_TYPE_CGROUP_STORAGE:
216 case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE:
217 key_size = sizeof(struct bpf_cgroup_storage_key);
218 value_size = sizeof(__u64);
221 case BPF_MAP_TYPE_QUEUE:
222 case BPF_MAP_TYPE_STACK:
225 case BPF_MAP_TYPE_SK_STORAGE:
227 btf_value_type_id = 3;
230 map_flags = BPF_F_NO_PREALLOC;
231 btf_fd = load_sk_storage_btf();
235 case BPF_MAP_TYPE_UNSPEC:
236 case BPF_MAP_TYPE_HASH:
237 case BPF_MAP_TYPE_ARRAY:
238 case BPF_MAP_TYPE_PROG_ARRAY:
239 case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
240 case BPF_MAP_TYPE_PERCPU_HASH:
241 case BPF_MAP_TYPE_PERCPU_ARRAY:
242 case BPF_MAP_TYPE_CGROUP_ARRAY:
243 case BPF_MAP_TYPE_LRU_HASH:
244 case BPF_MAP_TYPE_LRU_PERCPU_HASH:
245 case BPF_MAP_TYPE_ARRAY_OF_MAPS:
246 case BPF_MAP_TYPE_HASH_OF_MAPS:
247 case BPF_MAP_TYPE_DEVMAP:
248 case BPF_MAP_TYPE_DEVMAP_HASH:
249 case BPF_MAP_TYPE_SOCKMAP:
250 case BPF_MAP_TYPE_CPUMAP:
251 case BPF_MAP_TYPE_XSKMAP:
252 case BPF_MAP_TYPE_SOCKHASH:
253 case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY:
258 if (map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
259 map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
260 /* TODO: probe for device, once libbpf has a function to create
261 * map-in-map for offload
266 fd_inner = bpf_create_map(BPF_MAP_TYPE_HASH,
267 sizeof(__u32), sizeof(__u32), 1, 0);
270 fd = bpf_create_map_in_map(map_type, NULL, sizeof(__u32),
274 /* Note: No other restriction on map type probes for offload */
275 attr.map_type = map_type;
276 attr.key_size = key_size;
277 attr.value_size = value_size;
278 attr.max_entries = max_entries;
279 attr.map_flags = map_flags;
280 attr.map_ifindex = ifindex;
282 attr.btf_fd = btf_fd;
283 attr.btf_key_type_id = btf_key_type_id;
284 attr.btf_value_type_id = btf_value_type_id;
287 fd = bpf_create_map_xattr(&attr);
297 bool bpf_probe_helper(enum bpf_func_id id, enum bpf_prog_type prog_type,
300 struct bpf_insn insns[2] = {
307 probe_load(prog_type, insns, ARRAY_SIZE(insns), buf, sizeof(buf),
309 res = !grep(buf, "invalid func ") && !grep(buf, "unknown func ");
312 switch (get_vendor_id(ifindex)) {
313 case 0x19ee: /* Netronome specific */
314 res = res && !grep(buf, "not supported by FW") &&
315 !grep(buf, "unsupported function id");