1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2019 Mellanox Technologies, Ltd
11 #include "mlx5_common.h"
12 #include "mlx5_common_utils.h"
13 #include "mlx5_glue.h"
16 int mlx5_common_logtype;
19 #ifdef RTE_IBVERBS_LINK_DLOPEN
22 * Suffix RTE_EAL_PMD_PATH with "-glue".
24 * This function performs a sanity check on RTE_EAL_PMD_PATH before
25 * suffixing its last component.
28 * Output buffer, should be large enough otherwise NULL is returned.
33 * Pointer to @p buf or @p NULL in case suffix cannot be appended.
36 mlx5_glue_path(char *buf, size_t size)
38 static const char *const bad[] = { "/", ".", "..", NULL };
39 const char *path = RTE_EAL_PMD_PATH;
40 size_t len = strlen(path);
44 while (len && path[len - 1] == '/')
46 for (off = len; off && path[off - 1] != '/'; --off)
48 for (i = 0; bad[i]; ++i)
49 if (!strncmp(path + off, bad[i], (int)(len - off)))
51 i = snprintf(buf, size, "%.*s-glue", (int)len, path);
52 if (i == -1 || (size_t)i >= size)
56 RTE_LOG(ERR, PMD, "unable to append \"-glue\" to last component of"
57 " RTE_EAL_PMD_PATH (\"" RTE_EAL_PMD_PATH "\"), please"
58 " re-configure DPDK");
64 * Initialization routine for run-time dependency on rdma-core.
66 RTE_INIT_PRIO(mlx5_glue_init, CLASS)
70 /* Initialize common log type. */
71 mlx5_common_logtype = rte_log_register("pmd.common.mlx5");
72 if (mlx5_common_logtype >= 0)
73 rte_log_set_level(mlx5_common_logtype, RTE_LOG_NOTICE);
75 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
76 * huge pages. Calling ibv_fork_init() during init allows
77 * applications to use fork() safely for purposes other than
78 * using this PMD, which is not supported in forked processes.
80 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
81 /* Match the size of Rx completion entry to the size of a cacheline. */
82 if (RTE_CACHE_LINE_SIZE == 128)
83 setenv("MLX5_CQE_SIZE", "128", 0);
85 * MLX5_DEVICE_FATAL_CLEANUP tells ibv_destroy functions to
86 * cleanup all the Verbs resources even when the device was removed.
88 setenv("MLX5_DEVICE_FATAL_CLEANUP", "1", 1);
89 /* The glue initialization was done earlier by mlx5 common library. */
90 #ifdef RTE_IBVERBS_LINK_DLOPEN
91 char glue_path[sizeof(RTE_EAL_PMD_PATH) - 1 + sizeof("-glue")];
92 const char *path[] = {
94 * A basic security check is necessary before trusting
95 * MLX5_GLUE_PATH, which may override RTE_EAL_PMD_PATH.
97 (geteuid() == getuid() && getegid() == getgid() ?
98 getenv("MLX5_GLUE_PATH") : NULL),
100 * When RTE_EAL_PMD_PATH is set, use its glue-suffixed
101 * variant, otherwise let dlopen() look up libraries on its
105 mlx5_glue_path(glue_path, sizeof(glue_path)) : ""),
111 while (!handle && i != RTE_DIM(path)) {
120 end = strpbrk(path[i], ":;");
122 end = path[i] + strlen(path[i]);
128 ret = snprintf(name, sizeof(name), "%.*s%s" MLX5_GLUE,
130 (!len || *(end - 1) == '/') ? "" : "/");
133 if (sizeof(name) != (size_t)ret + 1)
135 DRV_LOG(DEBUG, "Looking for rdma-core glue as "
137 handle = dlopen(name, RTLD_LAZY);
148 DRV_LOG(WARNING, "Cannot load glue library: %s", dlmsg);
151 sym = dlsym(handle, "mlx5_glue");
156 DRV_LOG(ERR, "Cannot resolve glue symbol: %s", dlmsg);
160 #endif /* RTE_IBVERBS_LINK_DLOPEN */
162 /* Glue structure must not contain any NULL pointers. */
166 for (i = 0; i != sizeof(*mlx5_glue) / sizeof(void *); ++i)
167 assert(((const void *const *)mlx5_glue)[i]);
170 if (strcmp(mlx5_glue->version, MLX5_GLUE_VERSION)) {
172 DRV_LOG(ERR, "rdma-core glue \"%s\" mismatch: \"%s\" is "
173 "required", mlx5_glue->version, MLX5_GLUE_VERSION);
176 mlx5_glue->fork_init();
181 DRV_LOG(WARNING, "Cannot initialize MLX5 common due to missing"
182 " run-time dependency on rdma-core libraries (libibverbs,"