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 mlx5_get_pci_addr(const char *dev_path, struct rte_pci_addr *pci_addr)
32 MKSTR(path, "%s/device/uevent", dev_path);
34 file = fopen(path, "rb");
39 while (fgets(line, sizeof(line), file) == line) {
40 size_t len = strlen(line);
42 /* Truncate long lines. */
43 if (len == (sizeof(line) - 1)) {
44 while (line[(len - 1)] != '\n') {
45 int ret = fgetc(file);
49 line[(len - 1)] = ret;
51 /* No match for long lines. */
54 /* Extract information. */
57 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
61 &pci_addr->function) == 4) {
74 * Extract port name, as a number, from sysfs or netlink information.
76 * @param[in] port_name_in
77 * String representing the port name.
78 * @param[out] port_info_out
79 * Port information, including port name as a number and port name
83 * port_name field set according to recognized name format.
86 mlx5_translate_port_name(const char *port_name_in,
87 struct mlx5_switch_info *port_info_out)
89 char ctrl = 0, pf_c1, pf_c2, vf_c1, vf_c2, eol;
93 sc_items = sscanf(port_name_in, "%c%d",
94 &ctrl, &port_info_out->ctrl_num);
95 if (sc_items == 2 && ctrl == 'c') {
96 port_name_in++; /* 'c' */
97 port_name_in += snprintf(NULL, 0, "%d",
98 port_info_out->ctrl_num);
100 /* Check for port-name as a string of the form pf0vf0 or pf0sf0 */
101 sc_items = sscanf(port_name_in, "%c%c%d%c%c%d%c",
102 &pf_c1, &pf_c2, &port_info_out->pf_num,
103 &vf_c1, &vf_c2, &port_info_out->port_name, &eol);
104 if (sc_items == 6 && pf_c1 == 'p' && pf_c2 == 'f') {
105 if (vf_c1 == 'v' && vf_c2 == 'f') {
106 /* Kernel ver >= 5.0 or OFED ver >= 4.6 */
107 port_info_out->name_type =
108 MLX5_PHYS_PORT_NAME_TYPE_PFVF;
111 if (vf_c1 == 's' && vf_c2 == 'f') {
112 /* Kernel ver >= 5.11 or OFED ver >= 5.1 */
113 port_info_out->name_type =
114 MLX5_PHYS_PORT_NAME_TYPE_PFSF;
119 * Check for port-name as a string of the form p0
120 * (support kernel ver >= 5.0, or OFED ver >= 4.6).
122 sc_items = sscanf(port_name_in, "%c%d%c",
123 &pf_c1, &port_info_out->port_name, &eol);
124 if (sc_items == 2 && pf_c1 == 'p') {
125 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UPLINK;
129 * Check for port-name as a string of the form pf0
130 * (support kernel ver >= 5.7 for HPF representor on BF).
132 sc_items = sscanf(port_name_in, "%c%c%d%c",
133 &pf_c1, &pf_c2, &port_info_out->pf_num, &eol);
134 if (sc_items == 3 && pf_c1 == 'p' && pf_c2 == 'f') {
135 port_info_out->port_name = -1;
136 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_PFHPF;
139 /* Check for port-name as a number (support kernel ver < 5.0 */
141 port_info_out->port_name = strtol(port_name_in, &end, 0);
143 (size_t)(end - port_name_in) == strlen(port_name_in)) {
144 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_LEGACY;
147 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN;
151 mlx5_get_ifname_sysfs(const char *ibdev_path, char *ifname)
155 unsigned int dev_type = 0;
156 unsigned int dev_port_prev = ~0u;
157 char match[IF_NAMESIZE] = "";
159 MLX5_ASSERT(ibdev_path);
161 MKSTR(path, "%s/device/net", ibdev_path);
169 while ((dent = readdir(dir)) != NULL) {
170 char *name = dent->d_name;
172 unsigned int dev_port;
175 if ((name[0] == '.') &&
176 ((name[1] == '\0') ||
177 ((name[1] == '.') && (name[2] == '\0'))))
180 MKSTR(path, "%s/device/net/%s/%s",
182 (dev_type ? "dev_id" : "dev_port"));
184 file = fopen(path, "rb");
189 * Switch to dev_id when dev_port does not exist as
190 * is the case with Linux kernel versions < 3.15.
201 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
206 * Switch to dev_id when dev_port returns the same value for
207 * all ports. May happen when using a MOFED release older than
208 * 3.0 with a Linux kernel >= 3.15.
210 if (dev_port == dev_port_prev)
212 dev_port_prev = dev_port;
214 strlcpy(match, name, IF_NAMESIZE);
217 if (match[0] == '\0') {
221 strncpy(ifname, match, IF_NAMESIZE);
228 * Suffix RTE_EAL_PMD_PATH with "-glue".
230 * This function performs a sanity check on RTE_EAL_PMD_PATH before
231 * suffixing its last component.
234 * Output buffer, should be large enough otherwise NULL is returned.
239 * Pointer to @p buf or @p NULL in case suffix cannot be appended.
242 mlx5_glue_path(char *buf, size_t size)
244 static const char *const bad[] = { "/", ".", "..", NULL };
245 const char *path = RTE_EAL_PMD_PATH;
246 size_t len = strlen(path);
250 while (len && path[len - 1] == '/')
252 for (off = len; off && path[off - 1] != '/'; --off)
254 for (i = 0; bad[i]; ++i)
255 if (!strncmp(path + off, bad[i], (int)(len - off)))
257 i = snprintf(buf, size, "%.*s-glue", (int)len, path);
258 if (i == -1 || (size_t)i >= size)
262 RTE_LOG(ERR, PMD, "unable to append \"-glue\" to last component of"
263 " RTE_EAL_PMD_PATH (\"" RTE_EAL_PMD_PATH "\"), please"
264 " re-configure DPDK");
269 mlx5_glue_dlopen(void)
271 char glue_path[sizeof(RTE_EAL_PMD_PATH) - 1 + sizeof("-glue")];
274 char const *path[] = {
276 * A basic security check is necessary before trusting
277 * MLX5_GLUE_PATH, which may override RTE_EAL_PMD_PATH.
279 (geteuid() == getuid() && getegid() == getgid() ?
280 getenv("MLX5_GLUE_PATH") : NULL),
282 * When RTE_EAL_PMD_PATH is set, use its glue-suffixed
283 * variant, otherwise let dlopen() look up libraries on its
287 mlx5_glue_path(glue_path, sizeof(glue_path)) : ""),
293 while (!handle && i != RTE_DIM(path)) {
302 end = strpbrk(path[i], ":;");
304 end = path[i] + strlen(path[i]);
310 ret = snprintf(name, sizeof(name), "%.*s%s" MLX5_GLUE,
312 (!len || *(end - 1) == '/') ? "" : "/");
315 if (sizeof(name) != (size_t)ret + 1)
317 DRV_LOG(DEBUG, "Looking for rdma-core glue as "
319 handle = dlopen(name, RTLD_LAZY);
330 DRV_LOG(WARNING, "Cannot load glue library: %s", dlmsg);
333 sym = dlsym(handle, "mlx5_glue");
338 DRV_LOG(ERR, "Cannot resolve glue symbol: %s", dlmsg);
353 * Initialization routine for run-time dependency on rdma-core.
356 mlx5_glue_constructor(void)
359 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
360 * huge pages. Calling ibv_fork_init() during init allows
361 * applications to use fork() safely for purposes other than
362 * using this PMD, which is not supported in forked processes.
364 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
365 /* Match the size of Rx completion entry to the size of a cacheline. */
366 if (RTE_CACHE_LINE_SIZE == 128)
367 setenv("MLX5_CQE_SIZE", "128", 0);
369 * MLX5_DEVICE_FATAL_CLEANUP tells ibv_destroy functions to
370 * cleanup all the Verbs resources even when the device was removed.
372 setenv("MLX5_DEVICE_FATAL_CLEANUP", "1", 1);
375 if (mlx5_glue_dlopen() != 0)
379 #ifdef RTE_LIBRTE_MLX5_DEBUG
380 /* Glue structure must not contain any NULL pointers. */
384 for (i = 0; i != sizeof(*mlx5_glue) / sizeof(void *); ++i)
385 MLX5_ASSERT(((const void *const *)mlx5_glue)[i]);
388 if (strcmp(mlx5_glue->version, MLX5_GLUE_VERSION)) {
390 DRV_LOG(ERR, "rdma-core glue \"%s\" mismatch: \"%s\" is "
391 "required", mlx5_glue->version, MLX5_GLUE_VERSION);
394 mlx5_glue->fork_init();
398 DRV_LOG(WARNING, "Cannot initialize MLX5 common due to missing"
399 " run-time dependency on rdma-core libraries (libibverbs,"
405 mlx5_os_get_ibv_device(const struct rte_pci_addr *addr)
408 struct ibv_device **ibv_list = mlx5_glue->get_device_list(&n);
409 struct ibv_device *ibv_match = NULL;
411 if (ibv_list == NULL) {
416 struct rte_pci_addr paddr;
418 DRV_LOG(DEBUG, "Checking device \"%s\"..", ibv_list[n]->name);
419 if (mlx5_get_pci_addr(ibv_list[n]->ibdev_path, &paddr) != 0)
421 if (rte_pci_addr_cmp(addr, &paddr) != 0)
423 ibv_match = ibv_list[n];
426 if (ibv_match == NULL)
428 mlx5_glue->free_device_list(ibv_list);