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_utils.h"
19 #include "mlx5_glue.h"
22 const struct mlx5_glue *mlx5_glue;
26 * Get PCI information by sysfs device path.
29 * Pointer to device sysfs folder name.
30 * @param[out] pci_addr
31 * PCI bus address output buffer.
34 * 0 on success, a negative errno value otherwise and rte_errno is set.
37 mlx5_dev_to_pci_addr(const char *dev_path,
38 struct rte_pci_addr *pci_addr)
42 MKSTR(path, "%s/device/uevent", dev_path);
44 file = fopen(path, "rb");
49 while (fgets(line, sizeof(line), file) == line) {
50 size_t len = strlen(line);
53 /* Truncate long lines. */
54 if (len == (sizeof(line) - 1))
55 while (line[(len - 1)] != '\n') {
59 line[(len - 1)] = ret;
61 /* Extract information. */
64 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
68 &pci_addr->function) == 4) {
77 * Extract port name, as a number, from sysfs or netlink information.
79 * @param[in] port_name_in
80 * String representing the port name.
81 * @param[out] port_info_out
82 * Port information, including port name as a number and port name
86 * port_name field set according to recognized name format.
89 mlx5_translate_port_name(const char *port_name_in,
90 struct mlx5_switch_info *port_info_out)
92 char pf_c1, pf_c2, vf_c1, vf_c2, eol;
97 * Check for port-name as a string of the form pf0vf0
98 * (support kernel ver >= 5.0 or OFED ver >= 4.6).
100 sc_items = sscanf(port_name_in, "%c%c%d%c%c%d%c",
101 &pf_c1, &pf_c2, &port_info_out->pf_num,
102 &vf_c1, &vf_c2, &port_info_out->port_name, &eol);
104 pf_c1 == 'p' && pf_c2 == 'f' &&
105 vf_c1 == 'v' && vf_c2 == 'f') {
106 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_PFVF;
110 * Check for port-name as a string of the form p0
111 * (support kernel ver >= 5.0, or OFED ver >= 4.6).
113 sc_items = sscanf(port_name_in, "%c%d%c",
114 &pf_c1, &port_info_out->port_name, &eol);
115 if (sc_items == 2 && pf_c1 == 'p') {
116 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UPLINK;
120 * Check for port-name as a string of the form pf0
121 * (support kernel ver >= 5.7 for HPF representor on BF).
123 sc_items = sscanf(port_name_in, "%c%c%d%c",
124 &pf_c1, &pf_c2, &port_info_out->pf_num, &eol);
125 if (sc_items == 3 && pf_c1 == 'p' && pf_c2 == 'f') {
126 port_info_out->port_name = -1;
127 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_PFHPF;
130 /* Check for port-name as a number (support kernel ver < 5.0 */
132 port_info_out->port_name = strtol(port_name_in, &end, 0);
134 (size_t)(end - port_name_in) == strlen(port_name_in)) {
135 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_LEGACY;
138 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN;
142 * Get kernel interface name from IB device path.
144 * @param[in] ibdev_path
145 * Pointer to IB device path.
147 * Interface name output buffer.
150 * 0 on success, a negative errno value otherwise and rte_errno is set.
153 mlx5_get_ifname_sysfs(const char *ibdev_path, char *ifname)
157 unsigned int dev_type = 0;
158 unsigned int dev_port_prev = ~0u;
159 char match[IF_NAMESIZE] = "";
161 MLX5_ASSERT(ibdev_path);
163 MKSTR(path, "%s/device/net", ibdev_path);
171 while ((dent = readdir(dir)) != NULL) {
172 char *name = dent->d_name;
174 unsigned int dev_port;
177 if ((name[0] == '.') &&
178 ((name[1] == '\0') ||
179 ((name[1] == '.') && (name[2] == '\0'))))
182 MKSTR(path, "%s/device/net/%s/%s",
184 (dev_type ? "dev_id" : "dev_port"));
186 file = fopen(path, "rb");
191 * Switch to dev_id when dev_port does not exist as
192 * is the case with Linux kernel versions < 3.15.
203 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
208 * Switch to dev_id when dev_port returns the same value for
209 * all ports. May happen when using a MOFED release older than
210 * 3.0 with a Linux kernel >= 3.15.
212 if (dev_port == dev_port_prev)
214 dev_port_prev = dev_port;
216 strlcpy(match, name, IF_NAMESIZE);
219 if (match[0] == '\0') {
223 strncpy(ifname, match, IF_NAMESIZE);
230 * Suffix RTE_EAL_PMD_PATH with "-glue".
232 * This function performs a sanity check on RTE_EAL_PMD_PATH before
233 * suffixing its last component.
236 * Output buffer, should be large enough otherwise NULL is returned.
241 * Pointer to @p buf or @p NULL in case suffix cannot be appended.
244 mlx5_glue_path(char *buf, size_t size)
246 static const char *const bad[] = { "/", ".", "..", NULL };
247 const char *path = RTE_EAL_PMD_PATH;
248 size_t len = strlen(path);
252 while (len && path[len - 1] == '/')
254 for (off = len; off && path[off - 1] != '/'; --off)
256 for (i = 0; bad[i]; ++i)
257 if (!strncmp(path + off, bad[i], (int)(len - off)))
259 i = snprintf(buf, size, "%.*s-glue", (int)len, path);
260 if (i == -1 || (size_t)i >= size)
264 RTE_LOG(ERR, PMD, "unable to append \"-glue\" to last component of"
265 " RTE_EAL_PMD_PATH (\"" RTE_EAL_PMD_PATH "\"), please"
266 " re-configure DPDK");
271 mlx5_glue_dlopen(void)
273 char glue_path[sizeof(RTE_EAL_PMD_PATH) - 1 + sizeof("-glue")];
276 char const *path[] = {
278 * A basic security check is necessary before trusting
279 * MLX5_GLUE_PATH, which may override RTE_EAL_PMD_PATH.
281 (geteuid() == getuid() && getegid() == getgid() ?
282 getenv("MLX5_GLUE_PATH") : NULL),
284 * When RTE_EAL_PMD_PATH is set, use its glue-suffixed
285 * variant, otherwise let dlopen() look up libraries on its
289 mlx5_glue_path(glue_path, sizeof(glue_path)) : ""),
295 while (!handle && i != RTE_DIM(path)) {
304 end = strpbrk(path[i], ":;");
306 end = path[i] + strlen(path[i]);
312 ret = snprintf(name, sizeof(name), "%.*s%s" MLX5_GLUE,
314 (!len || *(end - 1) == '/') ? "" : "/");
317 if (sizeof(name) != (size_t)ret + 1)
319 DRV_LOG(DEBUG, "Looking for rdma-core glue as "
321 handle = dlopen(name, RTLD_LAZY);
332 DRV_LOG(WARNING, "Cannot load glue library: %s", dlmsg);
335 sym = dlsym(handle, "mlx5_glue");
340 DRV_LOG(ERR, "Cannot resolve glue symbol: %s", dlmsg);
355 * Initialization routine for run-time dependency on rdma-core.
358 mlx5_glue_constructor(void)
361 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
362 * huge pages. Calling ibv_fork_init() during init allows
363 * applications to use fork() safely for purposes other than
364 * using this PMD, which is not supported in forked processes.
366 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
367 /* Match the size of Rx completion entry to the size of a cacheline. */
368 if (RTE_CACHE_LINE_SIZE == 128)
369 setenv("MLX5_CQE_SIZE", "128", 0);
371 * MLX5_DEVICE_FATAL_CLEANUP tells ibv_destroy functions to
372 * cleanup all the Verbs resources even when the device was removed.
374 setenv("MLX5_DEVICE_FATAL_CLEANUP", "1", 1);
377 if (mlx5_glue_dlopen() != 0)
381 #ifdef RTE_LIBRTE_MLX5_DEBUG
382 /* Glue structure must not contain any NULL pointers. */
386 for (i = 0; i != sizeof(*mlx5_glue) / sizeof(void *); ++i)
387 MLX5_ASSERT(((const void *const *)mlx5_glue)[i]);
390 if (strcmp(mlx5_glue->version, MLX5_GLUE_VERSION)) {
392 DRV_LOG(ERR, "rdma-core glue \"%s\" mismatch: \"%s\" is "
393 "required", mlx5_glue->version, MLX5_GLUE_VERSION);
396 mlx5_glue->fork_init();
400 DRV_LOG(WARNING, "Cannot initialize MLX5 common due to missing"
401 " run-time dependency on rdma-core libraries (libibverbs,"