1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright 2020 Mellanox Technologies, Ltd 3 */ 4 5 #include <unistd.h> 6 #include <string.h> 7 #include <stdio.h> 8 #ifdef RTE_IBVERBS_LINK_DLOPEN 9 #include <dlfcn.h> 10 #endif 11 #include <dirent.h> 12 #include <net/if.h> 13 14 #include <rte_errno.h> 15 #include <rte_string_fns.h> 16 17 #include "mlx5_common.h" 18 #include "mlx5_common_utils.h" 19 #include "mlx5_glue.h" 20 21 #ifdef MLX5_GLUE 22 const struct mlx5_glue *mlx5_glue; 23 #endif 24 25 /** 26 * Get PCI information by sysfs device path. 27 * 28 * @param dev_path 29 * Pointer to device sysfs folder name. 30 * @param[out] pci_addr 31 * PCI bus address output buffer. 32 * 33 * @return 34 * 0 on success, a negative errno value otherwise and rte_errno is set. 35 */ 36 int 37 mlx5_dev_to_pci_addr(const char *dev_path, 38 struct rte_pci_addr *pci_addr) 39 { 40 FILE *file; 41 char line[32]; 42 int rc = -ENOENT; 43 MKSTR(path, "%s/device/uevent", dev_path); 44 45 file = fopen(path, "rb"); 46 if (file == NULL) { 47 rte_errno = errno; 48 return -rte_errno; 49 } 50 while (fgets(line, sizeof(line), file) == line) { 51 size_t len = strlen(line); 52 53 /* Truncate long lines. */ 54 if (len == (sizeof(line) - 1)) { 55 while (line[(len - 1)] != '\n') { 56 int ret = fgetc(file); 57 58 if (ret == EOF) 59 goto exit; 60 line[(len - 1)] = ret; 61 } 62 /* No match for long lines. */ 63 continue; 64 } 65 /* Extract information. */ 66 if (sscanf(line, 67 "PCI_SLOT_NAME=" 68 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n", 69 &pci_addr->domain, 70 &pci_addr->bus, 71 &pci_addr->devid, 72 &pci_addr->function) == 4) { 73 rc = 0; 74 break; 75 } 76 } 77 exit: 78 fclose(file); 79 if (rc) 80 rte_errno = -rc; 81 return rc; 82 } 83 84 /** 85 * Extract port name, as a number, from sysfs or netlink information. 86 * 87 * @param[in] port_name_in 88 * String representing the port name. 89 * @param[out] port_info_out 90 * Port information, including port name as a number and port name 91 * type if recognized 92 * 93 * @return 94 * port_name field set according to recognized name format. 95 */ 96 void 97 mlx5_translate_port_name(const char *port_name_in, 98 struct mlx5_switch_info *port_info_out) 99 { 100 char ctrl = 0, pf_c1, pf_c2, vf_c1, vf_c2, eol; 101 char *end; 102 int sc_items; 103 104 sc_items = sscanf(port_name_in, "%c%d", 105 &ctrl, &port_info_out->ctrl_num); 106 if (sc_items == 2 && ctrl == 'c') { 107 port_name_in++; /* 'c' */ 108 port_name_in += snprintf(NULL, 0, "%d", 109 port_info_out->ctrl_num); 110 } 111 /* Check for port-name as a string of the form pf0vf0 or pf0sf0 */ 112 sc_items = sscanf(port_name_in, "%c%c%d%c%c%d%c", 113 &pf_c1, &pf_c2, &port_info_out->pf_num, 114 &vf_c1, &vf_c2, &port_info_out->port_name, &eol); 115 if (sc_items == 6 && pf_c1 == 'p' && pf_c2 == 'f') { 116 if (vf_c1 == 'v' && vf_c2 == 'f') { 117 /* Kernel ver >= 5.0 or OFED ver >= 4.6 */ 118 port_info_out->name_type = 119 MLX5_PHYS_PORT_NAME_TYPE_PFVF; 120 return; 121 } 122 if (vf_c1 == 's' && vf_c2 == 'f') { 123 /* Kernel ver >= 5.11 or OFED ver >= 5.1 */ 124 port_info_out->name_type = 125 MLX5_PHYS_PORT_NAME_TYPE_PFSF; 126 return; 127 } 128 } 129 /* 130 * Check for port-name as a string of the form p0 131 * (support kernel ver >= 5.0, or OFED ver >= 4.6). 132 */ 133 sc_items = sscanf(port_name_in, "%c%d%c", 134 &pf_c1, &port_info_out->port_name, &eol); 135 if (sc_items == 2 && pf_c1 == 'p') { 136 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UPLINK; 137 return; 138 } 139 /* 140 * Check for port-name as a string of the form pf0 141 * (support kernel ver >= 5.7 for HPF representor on BF). 142 */ 143 sc_items = sscanf(port_name_in, "%c%c%d%c", 144 &pf_c1, &pf_c2, &port_info_out->pf_num, &eol); 145 if (sc_items == 3 && pf_c1 == 'p' && pf_c2 == 'f') { 146 port_info_out->port_name = -1; 147 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_PFHPF; 148 return; 149 } 150 /* Check for port-name as a number (support kernel ver < 5.0 */ 151 errno = 0; 152 port_info_out->port_name = strtol(port_name_in, &end, 0); 153 if (!errno && 154 (size_t)(end - port_name_in) == strlen(port_name_in)) { 155 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_LEGACY; 156 return; 157 } 158 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN; 159 } 160 161 /** 162 * Get kernel interface name from IB device path. 163 * 164 * @param[in] ibdev_path 165 * Pointer to IB device path. 166 * @param[out] ifname 167 * Interface name output buffer. 168 * 169 * @return 170 * 0 on success, a negative errno value otherwise and rte_errno is set. 171 */ 172 int 173 mlx5_get_ifname_sysfs(const char *ibdev_path, char *ifname) 174 { 175 DIR *dir; 176 struct dirent *dent; 177 unsigned int dev_type = 0; 178 unsigned int dev_port_prev = ~0u; 179 char match[IF_NAMESIZE] = ""; 180 181 MLX5_ASSERT(ibdev_path); 182 { 183 MKSTR(path, "%s/device/net", ibdev_path); 184 185 dir = opendir(path); 186 if (dir == NULL) { 187 rte_errno = errno; 188 return -rte_errno; 189 } 190 } 191 while ((dent = readdir(dir)) != NULL) { 192 char *name = dent->d_name; 193 FILE *file; 194 unsigned int dev_port; 195 int r; 196 197 if ((name[0] == '.') && 198 ((name[1] == '\0') || 199 ((name[1] == '.') && (name[2] == '\0')))) 200 continue; 201 202 MKSTR(path, "%s/device/net/%s/%s", 203 ibdev_path, name, 204 (dev_type ? "dev_id" : "dev_port")); 205 206 file = fopen(path, "rb"); 207 if (file == NULL) { 208 if (errno != ENOENT) 209 continue; 210 /* 211 * Switch to dev_id when dev_port does not exist as 212 * is the case with Linux kernel versions < 3.15. 213 */ 214 try_dev_id: 215 match[0] = '\0'; 216 if (dev_type) 217 break; 218 dev_type = 1; 219 dev_port_prev = ~0u; 220 rewinddir(dir); 221 continue; 222 } 223 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port); 224 fclose(file); 225 if (r != 1) 226 continue; 227 /* 228 * Switch to dev_id when dev_port returns the same value for 229 * all ports. May happen when using a MOFED release older than 230 * 3.0 with a Linux kernel >= 3.15. 231 */ 232 if (dev_port == dev_port_prev) 233 goto try_dev_id; 234 dev_port_prev = dev_port; 235 if (dev_port == 0) 236 strlcpy(match, name, IF_NAMESIZE); 237 } 238 closedir(dir); 239 if (match[0] == '\0') { 240 rte_errno = ENOENT; 241 return -rte_errno; 242 } 243 strncpy(ifname, match, IF_NAMESIZE); 244 return 0; 245 } 246 247 #ifdef MLX5_GLUE 248 249 /** 250 * Suffix RTE_EAL_PMD_PATH with "-glue". 251 * 252 * This function performs a sanity check on RTE_EAL_PMD_PATH before 253 * suffixing its last component. 254 * 255 * @param buf[out] 256 * Output buffer, should be large enough otherwise NULL is returned. 257 * @param size 258 * Size of @p out. 259 * 260 * @return 261 * Pointer to @p buf or @p NULL in case suffix cannot be appended. 262 */ 263 static char * 264 mlx5_glue_path(char *buf, size_t size) 265 { 266 static const char *const bad[] = { "/", ".", "..", NULL }; 267 const char *path = RTE_EAL_PMD_PATH; 268 size_t len = strlen(path); 269 size_t off; 270 int i; 271 272 while (len && path[len - 1] == '/') 273 --len; 274 for (off = len; off && path[off - 1] != '/'; --off) 275 ; 276 for (i = 0; bad[i]; ++i) 277 if (!strncmp(path + off, bad[i], (int)(len - off))) 278 goto error; 279 i = snprintf(buf, size, "%.*s-glue", (int)len, path); 280 if (i == -1 || (size_t)i >= size) 281 goto error; 282 return buf; 283 error: 284 RTE_LOG(ERR, PMD, "unable to append \"-glue\" to last component of" 285 " RTE_EAL_PMD_PATH (\"" RTE_EAL_PMD_PATH "\"), please" 286 " re-configure DPDK"); 287 return NULL; 288 } 289 290 static int 291 mlx5_glue_dlopen(void) 292 { 293 char glue_path[sizeof(RTE_EAL_PMD_PATH) - 1 + sizeof("-glue")]; 294 void *handle = NULL; 295 296 char const *path[] = { 297 /* 298 * A basic security check is necessary before trusting 299 * MLX5_GLUE_PATH, which may override RTE_EAL_PMD_PATH. 300 */ 301 (geteuid() == getuid() && getegid() == getgid() ? 302 getenv("MLX5_GLUE_PATH") : NULL), 303 /* 304 * When RTE_EAL_PMD_PATH is set, use its glue-suffixed 305 * variant, otherwise let dlopen() look up libraries on its 306 * own. 307 */ 308 (*RTE_EAL_PMD_PATH ? 309 mlx5_glue_path(glue_path, sizeof(glue_path)) : ""), 310 }; 311 unsigned int i = 0; 312 void **sym; 313 const char *dlmsg; 314 315 while (!handle && i != RTE_DIM(path)) { 316 const char *end; 317 size_t len; 318 int ret; 319 320 if (!path[i]) { 321 ++i; 322 continue; 323 } 324 end = strpbrk(path[i], ":;"); 325 if (!end) 326 end = path[i] + strlen(path[i]); 327 len = end - path[i]; 328 ret = 0; 329 do { 330 char name[ret + 1]; 331 332 ret = snprintf(name, sizeof(name), "%.*s%s" MLX5_GLUE, 333 (int)len, path[i], 334 (!len || *(end - 1) == '/') ? "" : "/"); 335 if (ret == -1) 336 break; 337 if (sizeof(name) != (size_t)ret + 1) 338 continue; 339 DRV_LOG(DEBUG, "Looking for rdma-core glue as " 340 "\"%s\"", name); 341 handle = dlopen(name, RTLD_LAZY); 342 break; 343 } while (1); 344 path[i] = end + 1; 345 if (!*end) 346 ++i; 347 } 348 if (!handle) { 349 rte_errno = EINVAL; 350 dlmsg = dlerror(); 351 if (dlmsg) 352 DRV_LOG(WARNING, "Cannot load glue library: %s", dlmsg); 353 goto glue_error; 354 } 355 sym = dlsym(handle, "mlx5_glue"); 356 if (!sym || !*sym) { 357 rte_errno = EINVAL; 358 dlmsg = dlerror(); 359 if (dlmsg) 360 DRV_LOG(ERR, "Cannot resolve glue symbol: %s", dlmsg); 361 goto glue_error; 362 } 363 mlx5_glue = *sym; 364 return 0; 365 366 glue_error: 367 if (handle) 368 dlclose(handle); 369 return -1; 370 } 371 372 #endif 373 374 /** 375 * Initialization routine for run-time dependency on rdma-core. 376 */ 377 void 378 mlx5_glue_constructor(void) 379 { 380 /* 381 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use 382 * huge pages. Calling ibv_fork_init() during init allows 383 * applications to use fork() safely for purposes other than 384 * using this PMD, which is not supported in forked processes. 385 */ 386 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1); 387 /* Match the size of Rx completion entry to the size of a cacheline. */ 388 if (RTE_CACHE_LINE_SIZE == 128) 389 setenv("MLX5_CQE_SIZE", "128", 0); 390 /* 391 * MLX5_DEVICE_FATAL_CLEANUP tells ibv_destroy functions to 392 * cleanup all the Verbs resources even when the device was removed. 393 */ 394 setenv("MLX5_DEVICE_FATAL_CLEANUP", "1", 1); 395 396 #ifdef MLX5_GLUE 397 if (mlx5_glue_dlopen() != 0) 398 goto glue_error; 399 #endif 400 401 #ifdef RTE_LIBRTE_MLX5_DEBUG 402 /* Glue structure must not contain any NULL pointers. */ 403 { 404 unsigned int i; 405 406 for (i = 0; i != sizeof(*mlx5_glue) / sizeof(void *); ++i) 407 MLX5_ASSERT(((const void *const *)mlx5_glue)[i]); 408 } 409 #endif 410 if (strcmp(mlx5_glue->version, MLX5_GLUE_VERSION)) { 411 rte_errno = EINVAL; 412 DRV_LOG(ERR, "rdma-core glue \"%s\" mismatch: \"%s\" is " 413 "required", mlx5_glue->version, MLX5_GLUE_VERSION); 414 goto glue_error; 415 } 416 mlx5_glue->fork_init(); 417 return; 418 419 glue_error: 420 DRV_LOG(WARNING, "Cannot initialize MLX5 common due to missing" 421 " run-time dependency on rdma-core libraries (libibverbs," 422 " libmlx5)"); 423 mlx5_glue = NULL; 424 } 425 426