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_os.h"
20 #include "mlx5_glue.h"
23 const struct mlx5_glue *mlx5_glue;
27 * Get PCI information by sysfs device path.
30 * Pointer to device sysfs folder name.
31 * @param[out] pci_addr
32 * PCI bus address output buffer.
35 * 0 on success, a negative errno value otherwise and rte_errno is set.
38 mlx5_dev_to_pci_addr(const char *dev_path,
39 struct rte_pci_addr *pci_addr)
44 MKSTR(path, "%s/device/uevent", dev_path);
46 file = fopen(path, "rb");
51 while (fgets(line, sizeof(line), file) == line) {
52 size_t len = strlen(line);
54 /* Truncate long lines. */
55 if (len == (sizeof(line) - 1)) {
56 while (line[(len - 1)] != '\n') {
57 int ret = fgetc(file);
61 line[(len - 1)] = ret;
63 /* No match for long lines. */
66 /* Extract information. */
69 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
73 &pci_addr->function) == 4) {
86 * Extract port name, as a number, from sysfs or netlink information.
88 * @param[in] port_name_in
89 * String representing the port name.
90 * @param[out] port_info_out
91 * Port information, including port name as a number and port name
95 * port_name field set according to recognized name format.
98 mlx5_translate_port_name(const char *port_name_in,
99 struct mlx5_switch_info *port_info_out)
101 char ctrl = 0, pf_c1, pf_c2, vf_c1, vf_c2, eol;
105 sc_items = sscanf(port_name_in, "%c%d",
106 &ctrl, &port_info_out->ctrl_num);
107 if (sc_items == 2 && ctrl == 'c') {
108 port_name_in++; /* 'c' */
109 port_name_in += snprintf(NULL, 0, "%d",
110 port_info_out->ctrl_num);
112 /* Check for port-name as a string of the form pf0vf0 or pf0sf0 */
113 sc_items = sscanf(port_name_in, "%c%c%d%c%c%d%c",
114 &pf_c1, &pf_c2, &port_info_out->pf_num,
115 &vf_c1, &vf_c2, &port_info_out->port_name, &eol);
116 if (sc_items == 6 && pf_c1 == 'p' && pf_c2 == 'f') {
117 if (vf_c1 == 'v' && vf_c2 == 'f') {
118 /* Kernel ver >= 5.0 or OFED ver >= 4.6 */
119 port_info_out->name_type =
120 MLX5_PHYS_PORT_NAME_TYPE_PFVF;
123 if (vf_c1 == 's' && vf_c2 == 'f') {
124 /* Kernel ver >= 5.11 or OFED ver >= 5.1 */
125 port_info_out->name_type =
126 MLX5_PHYS_PORT_NAME_TYPE_PFSF;
131 * Check for port-name as a string of the form p0
132 * (support kernel ver >= 5.0, or OFED ver >= 4.6).
134 sc_items = sscanf(port_name_in, "%c%d%c",
135 &pf_c1, &port_info_out->port_name, &eol);
136 if (sc_items == 2 && pf_c1 == 'p') {
137 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UPLINK;
141 * Check for port-name as a string of the form pf0
142 * (support kernel ver >= 5.7 for HPF representor on BF).
144 sc_items = sscanf(port_name_in, "%c%c%d%c",
145 &pf_c1, &pf_c2, &port_info_out->pf_num, &eol);
146 if (sc_items == 3 && pf_c1 == 'p' && pf_c2 == 'f') {
147 port_info_out->port_name = -1;
148 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_PFHPF;
151 /* Check for port-name as a number (support kernel ver < 5.0 */
153 port_info_out->port_name = strtol(port_name_in, &end, 0);
155 (size_t)(end - port_name_in) == strlen(port_name_in)) {
156 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_LEGACY;
159 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN;
163 * Get kernel interface name from IB device path.
165 * @param[in] ibdev_path
166 * Pointer to IB device path.
168 * Interface name output buffer.
171 * 0 on success, a negative errno value otherwise and rte_errno is set.
174 mlx5_get_ifname_sysfs(const char *ibdev_path, char *ifname)
178 unsigned int dev_type = 0;
179 unsigned int dev_port_prev = ~0u;
180 char match[IF_NAMESIZE] = "";
182 MLX5_ASSERT(ibdev_path);
184 MKSTR(path, "%s/device/net", ibdev_path);
192 while ((dent = readdir(dir)) != NULL) {
193 char *name = dent->d_name;
195 unsigned int dev_port;
198 if ((name[0] == '.') &&
199 ((name[1] == '\0') ||
200 ((name[1] == '.') && (name[2] == '\0'))))
203 MKSTR(path, "%s/device/net/%s/%s",
205 (dev_type ? "dev_id" : "dev_port"));
207 file = fopen(path, "rb");
212 * Switch to dev_id when dev_port does not exist as
213 * is the case with Linux kernel versions < 3.15.
224 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
229 * Switch to dev_id when dev_port returns the same value for
230 * all ports. May happen when using a MOFED release older than
231 * 3.0 with a Linux kernel >= 3.15.
233 if (dev_port == dev_port_prev)
235 dev_port_prev = dev_port;
237 strlcpy(match, name, IF_NAMESIZE);
240 if (match[0] == '\0') {
244 strncpy(ifname, match, IF_NAMESIZE);
251 * Suffix RTE_EAL_PMD_PATH with "-glue".
253 * This function performs a sanity check on RTE_EAL_PMD_PATH before
254 * suffixing its last component.
257 * Output buffer, should be large enough otherwise NULL is returned.
262 * Pointer to @p buf or @p NULL in case suffix cannot be appended.
265 mlx5_glue_path(char *buf, size_t size)
267 static const char *const bad[] = { "/", ".", "..", NULL };
268 const char *path = RTE_EAL_PMD_PATH;
269 size_t len = strlen(path);
273 while (len && path[len - 1] == '/')
275 for (off = len; off && path[off - 1] != '/'; --off)
277 for (i = 0; bad[i]; ++i)
278 if (!strncmp(path + off, bad[i], (int)(len - off)))
280 i = snprintf(buf, size, "%.*s-glue", (int)len, path);
281 if (i == -1 || (size_t)i >= size)
285 RTE_LOG(ERR, PMD, "unable to append \"-glue\" to last component of"
286 " RTE_EAL_PMD_PATH (\"" RTE_EAL_PMD_PATH "\"), please"
287 " re-configure DPDK");
292 mlx5_glue_dlopen(void)
294 char glue_path[sizeof(RTE_EAL_PMD_PATH) - 1 + sizeof("-glue")];
297 char const *path[] = {
299 * A basic security check is necessary before trusting
300 * MLX5_GLUE_PATH, which may override RTE_EAL_PMD_PATH.
302 (geteuid() == getuid() && getegid() == getgid() ?
303 getenv("MLX5_GLUE_PATH") : NULL),
305 * When RTE_EAL_PMD_PATH is set, use its glue-suffixed
306 * variant, otherwise let dlopen() look up libraries on its
310 mlx5_glue_path(glue_path, sizeof(glue_path)) : ""),
316 while (!handle && i != RTE_DIM(path)) {
325 end = strpbrk(path[i], ":;");
327 end = path[i] + strlen(path[i]);
333 ret = snprintf(name, sizeof(name), "%.*s%s" MLX5_GLUE,
335 (!len || *(end - 1) == '/') ? "" : "/");
338 if (sizeof(name) != (size_t)ret + 1)
340 DRV_LOG(DEBUG, "Looking for rdma-core glue as "
342 handle = dlopen(name, RTLD_LAZY);
353 DRV_LOG(WARNING, "Cannot load glue library: %s", dlmsg);
356 sym = dlsym(handle, "mlx5_glue");
361 DRV_LOG(ERR, "Cannot resolve glue symbol: %s", dlmsg);
376 * Initialization routine for run-time dependency on rdma-core.
379 mlx5_glue_constructor(void)
382 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
383 * huge pages. Calling ibv_fork_init() during init allows
384 * applications to use fork() safely for purposes other than
385 * using this PMD, which is not supported in forked processes.
387 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
388 /* Match the size of Rx completion entry to the size of a cacheline. */
389 if (RTE_CACHE_LINE_SIZE == 128)
390 setenv("MLX5_CQE_SIZE", "128", 0);
392 * MLX5_DEVICE_FATAL_CLEANUP tells ibv_destroy functions to
393 * cleanup all the Verbs resources even when the device was removed.
395 setenv("MLX5_DEVICE_FATAL_CLEANUP", "1", 1);
398 if (mlx5_glue_dlopen() != 0)
402 #ifdef RTE_LIBRTE_MLX5_DEBUG
403 /* Glue structure must not contain any NULL pointers. */
407 for (i = 0; i != sizeof(*mlx5_glue) / sizeof(void *); ++i)
408 MLX5_ASSERT(((const void *const *)mlx5_glue)[i]);
411 if (strcmp(mlx5_glue->version, MLX5_GLUE_VERSION)) {
413 DRV_LOG(ERR, "rdma-core glue \"%s\" mismatch: \"%s\" is "
414 "required", mlx5_glue->version, MLX5_GLUE_VERSION);
417 mlx5_glue->fork_init();
421 DRV_LOG(WARNING, "Cannot initialize MLX5 common due to missing"
422 " run-time dependency on rdma-core libraries (libibverbs,"
428 mlx5_os_get_ibv_device(struct rte_pci_addr *addr)
431 struct ibv_device **ibv_list = mlx5_glue->get_device_list(&n);
432 struct ibv_device *ibv_match = NULL;
434 if (ibv_list == NULL) {
439 struct rte_pci_addr paddr;
441 DRV_LOG(DEBUG, "Checking device \"%s\"..", ibv_list[n]->name);
442 if (mlx5_dev_to_pci_addr(ibv_list[n]->ibdev_path, &paddr) != 0)
444 if (rte_pci_addr_cmp(addr, &paddr) != 0)
446 ibv_match = ibv_list[n];
449 if (ibv_match == NULL)
451 mlx5_glue->free_device_list(ibv_list);