1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2020 Mellanox Technologies, Ltd
8 #ifdef RTE_IBVERBS_LINK_DLOPEN
14 #include <rte_errno.h>
15 #include <rte_string_fns.h>
17 #include "mlx5_common.h"
18 #include "mlx5_common_log.h"
19 #include "mlx5_common_defs.h"
20 #include "mlx5_common_os.h"
21 #include "mlx5_glue.h"
24 const struct mlx5_glue *mlx5_glue;
28 mlx5_get_pci_addr(const char *dev_path, struct rte_pci_addr *pci_addr)
33 MKSTR(path, "%s/device/uevent", dev_path);
35 file = fopen(path, "rb");
40 while (fgets(line, sizeof(line), file) == line) {
41 size_t len = strlen(line);
43 /* Truncate long lines. */
44 if (len == (sizeof(line) - 1)) {
45 while (line[(len - 1)] != '\n') {
46 int ret = fgetc(file);
50 line[(len - 1)] = ret;
52 /* No match for long lines. */
55 /* Extract information. */
58 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
62 &pci_addr->function) == 4) {
75 * Extract port name, as a number, from sysfs or netlink information.
77 * @param[in] port_name_in
78 * String representing the port name.
79 * @param[out] port_info_out
80 * Port information, including port name as a number and port name
84 * port_name field set according to recognized name format.
87 mlx5_translate_port_name(const char *port_name_in,
88 struct mlx5_switch_info *port_info_out)
90 char ctrl = 0, pf_c1, pf_c2, vf_c1, vf_c2, eol;
94 sc_items = sscanf(port_name_in, "%c%d",
95 &ctrl, &port_info_out->ctrl_num);
96 if (sc_items == 2 && ctrl == 'c') {
97 port_name_in++; /* 'c' */
98 port_name_in += snprintf(NULL, 0, "%d",
99 port_info_out->ctrl_num);
101 /* Check for port-name as a string of the form pf0vf0 or pf0sf0 */
102 sc_items = sscanf(port_name_in, "%c%c%d%c%c%d%c",
103 &pf_c1, &pf_c2, &port_info_out->pf_num,
104 &vf_c1, &vf_c2, &port_info_out->port_name, &eol);
105 if (sc_items == 6 && pf_c1 == 'p' && pf_c2 == 'f') {
106 if (vf_c1 == 'v' && vf_c2 == 'f') {
107 /* Kernel ver >= 5.0 or OFED ver >= 4.6 */
108 port_info_out->name_type =
109 MLX5_PHYS_PORT_NAME_TYPE_PFVF;
112 if (vf_c1 == 's' && vf_c2 == 'f') {
113 /* Kernel ver >= 5.11 or OFED ver >= 5.1 */
114 port_info_out->name_type =
115 MLX5_PHYS_PORT_NAME_TYPE_PFSF;
120 * Check for port-name as a string of the form p0
121 * (support kernel ver >= 5.0, or OFED ver >= 4.6).
123 sc_items = sscanf(port_name_in, "%c%d%c",
124 &pf_c1, &port_info_out->port_name, &eol);
125 if (sc_items == 2 && pf_c1 == 'p') {
126 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UPLINK;
130 * Check for port-name as a string of the form pf0
131 * (support kernel ver >= 5.7 for HPF representor on BF).
133 sc_items = sscanf(port_name_in, "%c%c%d%c",
134 &pf_c1, &pf_c2, &port_info_out->pf_num, &eol);
135 if (sc_items == 3 && pf_c1 == 'p' && pf_c2 == 'f') {
136 port_info_out->port_name = -1;
137 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_PFHPF;
140 /* Check for port-name as a number (support kernel ver < 5.0 */
142 port_info_out->port_name = strtol(port_name_in, &end, 0);
144 (size_t)(end - port_name_in) == strlen(port_name_in)) {
145 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_LEGACY;
148 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN;
152 mlx5_get_ifname_sysfs(const char *ibdev_path, char *ifname)
156 unsigned int dev_type = 0;
157 unsigned int dev_port_prev = ~0u;
158 char match[IF_NAMESIZE] = "";
160 MLX5_ASSERT(ibdev_path);
162 MKSTR(path, "%s/device/net", ibdev_path);
170 while ((dent = readdir(dir)) != NULL) {
171 char *name = dent->d_name;
173 unsigned int dev_port;
176 if ((name[0] == '.') &&
177 ((name[1] == '\0') ||
178 ((name[1] == '.') && (name[2] == '\0'))))
181 MKSTR(path, "%s/device/net/%s/%s",
183 (dev_type ? "dev_id" : "dev_port"));
185 file = fopen(path, "rb");
190 * Switch to dev_id when dev_port does not exist as
191 * is the case with Linux kernel versions < 3.15.
202 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
207 * Switch to dev_id when dev_port returns the same value for
208 * all ports. May happen when using a MOFED release older than
209 * 3.0 with a Linux kernel >= 3.15.
211 if (dev_port == dev_port_prev)
213 dev_port_prev = dev_port;
215 strlcpy(match, name, IF_NAMESIZE);
218 if (match[0] == '\0') {
222 strncpy(ifname, match, IF_NAMESIZE);
229 * Suffix RTE_EAL_PMD_PATH with "-glue".
231 * This function performs a sanity check on RTE_EAL_PMD_PATH before
232 * suffixing its last component.
235 * Output buffer, should be large enough otherwise NULL is returned.
240 * Pointer to @p buf or @p NULL in case suffix cannot be appended.
243 mlx5_glue_path(char *buf, size_t size)
245 static const char *const bad[] = { "/", ".", "..", NULL };
246 const char *path = RTE_EAL_PMD_PATH;
247 size_t len = strlen(path);
251 while (len && path[len - 1] == '/')
253 for (off = len; off && path[off - 1] != '/'; --off)
255 for (i = 0; bad[i]; ++i)
256 if (!strncmp(path + off, bad[i], (int)(len - off)))
258 i = snprintf(buf, size, "%.*s-glue", (int)len, path);
259 if (i == -1 || (size_t)i >= size)
263 RTE_LOG(ERR, PMD, "unable to append \"-glue\" to last component of"
264 " RTE_EAL_PMD_PATH (\"" RTE_EAL_PMD_PATH "\"), please"
265 " re-configure DPDK");
270 mlx5_glue_dlopen(void)
272 char glue_path[sizeof(RTE_EAL_PMD_PATH) - 1 + sizeof("-glue")];
275 char const *path[] = {
277 * A basic security check is necessary before trusting
278 * MLX5_GLUE_PATH, which may override RTE_EAL_PMD_PATH.
280 (geteuid() == getuid() && getegid() == getgid() ?
281 getenv("MLX5_GLUE_PATH") : NULL),
283 * When RTE_EAL_PMD_PATH is set, use its glue-suffixed
284 * variant, otherwise let dlopen() look up libraries on its
288 mlx5_glue_path(glue_path, sizeof(glue_path)) : ""),
294 while (!handle && i != RTE_DIM(path)) {
303 end = strpbrk(path[i], ":;");
305 end = path[i] + strlen(path[i]);
311 ret = snprintf(name, sizeof(name), "%.*s%s" MLX5_GLUE,
313 (!len || *(end - 1) == '/') ? "" : "/");
316 if (sizeof(name) != (size_t)ret + 1)
318 DRV_LOG(DEBUG, "Looking for rdma-core glue as "
320 handle = dlopen(name, RTLD_LAZY);
331 DRV_LOG(WARNING, "Cannot load glue library: %s", dlmsg);
334 sym = dlsym(handle, "mlx5_glue");
339 DRV_LOG(ERR, "Cannot resolve glue symbol: %s", dlmsg);
354 * Initialization routine for run-time dependency on rdma-core.
357 mlx5_glue_constructor(void)
360 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
361 * huge pages. Calling ibv_fork_init() during init allows
362 * applications to use fork() safely for purposes other than
363 * using this PMD, which is not supported in forked processes.
365 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
366 /* Match the size of Rx completion entry to the size of a cacheline. */
367 if (RTE_CACHE_LINE_SIZE == 128)
368 setenv("MLX5_CQE_SIZE", "128", 0);
370 * MLX5_DEVICE_FATAL_CLEANUP tells ibv_destroy functions to
371 * cleanup all the Verbs resources even when the device was removed.
373 setenv("MLX5_DEVICE_FATAL_CLEANUP", "1", 1);
376 if (mlx5_glue_dlopen() != 0)
380 #ifdef RTE_LIBRTE_MLX5_DEBUG
381 /* Glue structure must not contain any NULL pointers. */
385 for (i = 0; i != sizeof(*mlx5_glue) / sizeof(void *); ++i)
386 MLX5_ASSERT(((const void *const *)mlx5_glue)[i]);
389 if (strcmp(mlx5_glue->version, MLX5_GLUE_VERSION)) {
391 DRV_LOG(ERR, "rdma-core glue \"%s\" mismatch: \"%s\" is "
392 "required", mlx5_glue->version, MLX5_GLUE_VERSION);
395 mlx5_glue->fork_init();
399 DRV_LOG(WARNING, "Cannot initialize MLX5 common due to missing"
400 " run-time dependency on rdma-core libraries (libibverbs,"
406 mlx5_os_get_ibv_device(const struct rte_pci_addr *addr)
409 struct ibv_device **ibv_list = mlx5_glue->get_device_list(&n);
410 struct ibv_device *ibv_match = NULL;
412 if (ibv_list == NULL) {
417 struct rte_pci_addr paddr;
419 DRV_LOG(DEBUG, "Checking device \"%s\"..", ibv_list[n]->name);
420 if (mlx5_get_pci_addr(ibv_list[n]->ibdev_path, &paddr) != 0)
422 if (rte_pci_addr_cmp(addr, &paddr) != 0)
424 ibv_match = ibv_list[n];
427 if (ibv_match == NULL) {
429 "No Verbs device matches PCI device " PCI_PRI_FMT ","
430 " are kernel drivers loaded?",
431 addr->domain, addr->bus, addr->devid, addr->function);
434 mlx5_glue->free_device_list(ibv_list);
439 mlx5_config_doorbell_mapping_env(int dbnc)
444 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
445 /* Get environment variable to store. */
446 env = getenv(MLX5_SHUT_UP_BF);
447 value = env ? !!strcmp(env, "0") : MLX5_ARG_UNSET;
448 if (dbnc == MLX5_ARG_UNSET)
449 setenv(MLX5_SHUT_UP_BF, MLX5_SHUT_UP_BF_DEFAULT, 1);
451 setenv(MLX5_SHUT_UP_BF,
452 dbnc == MLX5_TXDB_NCACHED ? "1" : "0", 1);
457 mlx5_restore_doorbell_mapping_env(int value)
459 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
460 /* Restore the original environment variable state. */
461 if (value == MLX5_ARG_UNSET)
462 unsetenv(MLX5_SHUT_UP_BF);
464 setenv(MLX5_SHUT_UP_BF, value ? "1" : "0", 1);
468 * Function API to open IB device.
472 * Pointer to the mlx5 device.
474 * Chosen classes come from device arguments.
477 * 0 on success, a negative errno value otherwise and rte_errno is set.
480 mlx5_os_open_device(struct mlx5_common_device *cdev, uint32_t classes)
482 struct ibv_device *ibv;
483 struct ibv_context *ctx = NULL;
486 ibv = mlx5_os_get_ibv_dev(cdev->dev);
489 DRV_LOG(INFO, "Dev information matches for device \"%s\".", ibv->name);
491 * Configure environment variable "MLX5_BF_SHUT_UP" before the device
492 * creation. The rdma_core library checks the variable at device
493 * creation and stores the result internally.
495 dbmap_env = mlx5_config_doorbell_mapping_env(cdev->config.dbnc);
496 /* Try to open IB device with DV first, then usual Verbs. */
498 ctx = mlx5_glue->dv_open_device(ibv);
500 cdev->config.devx = 1;
501 DRV_LOG(DEBUG, "DevX is supported.");
502 } else if (classes == MLX5_CLASS_ETH) {
503 /* The environment variable is still configured. */
504 ctx = mlx5_glue->open_device(ibv);
507 DRV_LOG(DEBUG, "DevX is NOT supported.");
511 /* The device is created, no need for environment. */
512 mlx5_restore_doorbell_mapping_env(dbmap_env);
513 /* Hint libmlx5 to use PMD allocator for data plane resources */
514 mlx5_set_context_attr(cdev->dev, ctx);
518 rte_errno = errno ? errno : ENODEV;
519 /* The device creation is failed, no need for environment. */
520 mlx5_restore_doorbell_mapping_env(dbmap_env);
521 DRV_LOG(ERR, "Failed to open IB device \"%s\".", ibv->name);