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 case BPF_PROG_TYPE_CGROUP_SYSCTL: 101 default: 102 break; 103 } 104 105 xattr.prog_type = prog_type; 106 xattr.insns = insns; 107 xattr.insns_cnt = insns_cnt; 108 xattr.license = "GPL"; 109 xattr.prog_ifindex = ifindex; 110 111 fd = bpf_load_program_xattr(&xattr, buf, buf_len); 112 if (fd >= 0) 113 close(fd); 114 } 115 116 bool bpf_probe_prog_type(enum bpf_prog_type prog_type, __u32 ifindex) 117 { 118 struct bpf_insn insns[2] = { 119 BPF_MOV64_IMM(BPF_REG_0, 0), 120 BPF_EXIT_INSN() 121 }; 122 123 if (ifindex && prog_type == BPF_PROG_TYPE_SCHED_CLS) 124 /* nfp returns -EINVAL on exit(0) with TC offload */ 125 insns[0].imm = 2; 126 127 errno = 0; 128 probe_load(prog_type, insns, ARRAY_SIZE(insns), NULL, 0, ifindex); 129 130 return errno != EINVAL && errno != EOPNOTSUPP; 131 } 132 133 bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex) 134 { 135 int key_size, value_size, max_entries, map_flags; 136 struct bpf_create_map_attr attr = {}; 137 int fd = -1, fd_inner; 138 139 key_size = sizeof(__u32); 140 value_size = sizeof(__u32); 141 max_entries = 1; 142 map_flags = 0; 143 144 switch (map_type) { 145 case BPF_MAP_TYPE_STACK_TRACE: 146 value_size = sizeof(__u64); 147 break; 148 case BPF_MAP_TYPE_LPM_TRIE: 149 key_size = sizeof(__u64); 150 value_size = sizeof(__u64); 151 map_flags = BPF_F_NO_PREALLOC; 152 break; 153 case BPF_MAP_TYPE_CGROUP_STORAGE: 154 case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: 155 key_size = sizeof(struct bpf_cgroup_storage_key); 156 value_size = sizeof(__u64); 157 max_entries = 0; 158 break; 159 case BPF_MAP_TYPE_QUEUE: 160 case BPF_MAP_TYPE_STACK: 161 key_size = 0; 162 break; 163 case BPF_MAP_TYPE_UNSPEC: 164 case BPF_MAP_TYPE_HASH: 165 case BPF_MAP_TYPE_ARRAY: 166 case BPF_MAP_TYPE_PROG_ARRAY: 167 case BPF_MAP_TYPE_PERF_EVENT_ARRAY: 168 case BPF_MAP_TYPE_PERCPU_HASH: 169 case BPF_MAP_TYPE_PERCPU_ARRAY: 170 case BPF_MAP_TYPE_CGROUP_ARRAY: 171 case BPF_MAP_TYPE_LRU_HASH: 172 case BPF_MAP_TYPE_LRU_PERCPU_HASH: 173 case BPF_MAP_TYPE_ARRAY_OF_MAPS: 174 case BPF_MAP_TYPE_HASH_OF_MAPS: 175 case BPF_MAP_TYPE_DEVMAP: 176 case BPF_MAP_TYPE_SOCKMAP: 177 case BPF_MAP_TYPE_CPUMAP: 178 case BPF_MAP_TYPE_XSKMAP: 179 case BPF_MAP_TYPE_SOCKHASH: 180 case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY: 181 default: 182 break; 183 } 184 185 if (map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS || 186 map_type == BPF_MAP_TYPE_HASH_OF_MAPS) { 187 /* TODO: probe for device, once libbpf has a function to create 188 * map-in-map for offload 189 */ 190 if (ifindex) 191 return false; 192 193 fd_inner = bpf_create_map(BPF_MAP_TYPE_HASH, 194 sizeof(__u32), sizeof(__u32), 1, 0); 195 if (fd_inner < 0) 196 return false; 197 fd = bpf_create_map_in_map(map_type, NULL, sizeof(__u32), 198 fd_inner, 1, 0); 199 close(fd_inner); 200 } else { 201 /* Note: No other restriction on map type probes for offload */ 202 attr.map_type = map_type; 203 attr.key_size = key_size; 204 attr.value_size = value_size; 205 attr.max_entries = max_entries; 206 attr.map_flags = map_flags; 207 attr.map_ifindex = ifindex; 208 209 fd = bpf_create_map_xattr(&attr); 210 } 211 if (fd >= 0) 212 close(fd); 213 214 return fd >= 0; 215 } 216 217 bool bpf_probe_helper(enum bpf_func_id id, enum bpf_prog_type prog_type, 218 __u32 ifindex) 219 { 220 struct bpf_insn insns[2] = { 221 BPF_EMIT_CALL(id), 222 BPF_EXIT_INSN() 223 }; 224 char buf[4096] = {}; 225 bool res; 226 227 probe_load(prog_type, insns, ARRAY_SIZE(insns), buf, sizeof(buf), 228 ifindex); 229 res = !grep(buf, "invalid func ") && !grep(buf, "unknown func "); 230 231 if (ifindex) { 232 switch (get_vendor_id(ifindex)) { 233 case 0x19ee: /* Netronome specific */ 234 res = res && !grep(buf, "not supported by FW") && 235 !grep(buf, "unsupported function id"); 236 break; 237 default: 238 break; 239 } 240 } 241 242 return res; 243 } 244