e74aa89cc538b256642860933e7acabf5cb36b13
[dpdk.git] / drivers / common / mlx5 / linux / mlx5_common_os.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2020 Mellanox Technologies, Ltd
3  */
4
5 #include <unistd.h>
6 #include <string.h>
7 #include <stdio.h>
8 #ifdef RTE_IBVERBS_LINK_DLOPEN
9 #include <dlfcn.h>
10 #endif
11 #include <dirent.h>
12 #include <net/if.h>
13
14 #include <rte_errno.h>
15 #include <rte_string_fns.h>
16
17 #include "mlx5_common.h"
18 #include "mlx5_common_utils.h"
19 #include "mlx5_glue.h"
20
21 #ifdef MLX5_GLUE
22 const struct mlx5_glue *mlx5_glue;
23 #endif
24
25 /**
26  * Get PCI information by sysfs device path.
27  *
28  * @param dev_path
29  *   Pointer to device sysfs folder name.
30  * @param[out] pci_addr
31  *   PCI bus address output buffer.
32  *
33  * @return
34  *   0 on success, a negative errno value otherwise and rte_errno is set.
35  */
36 int
37 mlx5_dev_to_pci_addr(const char *dev_path,
38                      struct rte_pci_addr *pci_addr)
39 {
40         FILE *file;
41         char line[32];
42         MKSTR(path, "%s/device/uevent", dev_path);
43
44         file = fopen(path, "rb");
45         if (file == NULL) {
46                 rte_errno = errno;
47                 return -rte_errno;
48         }
49         while (fgets(line, sizeof(line), file) == line) {
50                 size_t len = strlen(line);
51                 int ret;
52
53                 /* Truncate long lines. */
54                 if (len == (sizeof(line) - 1))
55                         while (line[(len - 1)] != '\n') {
56                                 ret = fgetc(file);
57                                 if (ret == EOF)
58                                         break;
59                                 line[(len - 1)] = ret;
60                         }
61                 /* Extract information. */
62                 if (sscanf(line,
63                            "PCI_SLOT_NAME="
64                            "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
65                            &pci_addr->domain,
66                            &pci_addr->bus,
67                            &pci_addr->devid,
68                            &pci_addr->function) == 4) {
69                         break;
70                 }
71         }
72         fclose(file);
73         return 0;
74 }
75
76 /**
77  * Extract port name, as a number, from sysfs or netlink information.
78  *
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
83  *   type if recognized
84  *
85  * @return
86  *   port_name field set according to recognized name format.
87  */
88 void
89 mlx5_translate_port_name(const char *port_name_in,
90                          struct mlx5_switch_info *port_info_out)
91 {
92         char pf_c1, pf_c2, vf_c1, vf_c2;
93         char *end;
94         int sc_items;
95
96         /*
97          * Check for port-name as a string of the form pf0vf0
98          * (support kernel ver >= 5.0 or OFED ver >= 4.6).
99          */
100         sc_items = sscanf(port_name_in, "%c%c%d%c%c%d",
101                           &pf_c1, &pf_c2, &port_info_out->pf_num,
102                           &vf_c1, &vf_c2, &port_info_out->port_name);
103         if (sc_items == 6 &&
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;
107                 return;
108         }
109         /*
110          * Check for port-name as a string of the form p0
111          * (support kernel ver >= 5.0, or OFED ver >= 4.6).
112          */
113         sc_items = sscanf(port_name_in, "%c%d",
114                           &pf_c1, &port_info_out->port_name);
115         if (sc_items == 2 && pf_c1 == 'p') {
116                 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UPLINK;
117                 return;
118         }
119         /*
120          * Check for port-name as a string of the form pf0
121          * (support kernel ver >= 5.7 for HPF representor on BF).
122          */
123         sc_items = sscanf(port_name_in, "%c%c%d",
124                           &pf_c1, &pf_c2, &port_info_out->pf_num);
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;
128                 return;
129         }
130         /* Check for port-name as a number (support kernel ver < 5.0 */
131         errno = 0;
132         port_info_out->port_name = strtol(port_name_in, &end, 0);
133         if (!errno &&
134             (size_t)(end - port_name_in) == strlen(port_name_in)) {
135                 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_LEGACY;
136                 return;
137         }
138         port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN;
139 }
140
141 /**
142  * Get kernel interface name from IB device path.
143  *
144  * @param[in] ibdev_path
145  *   Pointer to IB device path.
146  * @param[out] ifname
147  *   Interface name output buffer.
148  *
149  * @return
150  *   0 on success, a negative errno value otherwise and rte_errno is set.
151  */
152 int
153 mlx5_get_ifname_sysfs(const char *ibdev_path, char *ifname)
154 {
155         DIR *dir;
156         struct dirent *dent;
157         unsigned int dev_type = 0;
158         unsigned int dev_port_prev = ~0u;
159         char match[IF_NAMESIZE] = "";
160
161         MLX5_ASSERT(ibdev_path);
162         {
163                 MKSTR(path, "%s/device/net", ibdev_path);
164
165                 dir = opendir(path);
166                 if (dir == NULL) {
167                         rte_errno = errno;
168                         return -rte_errno;
169                 }
170         }
171         while ((dent = readdir(dir)) != NULL) {
172                 char *name = dent->d_name;
173                 FILE *file;
174                 unsigned int dev_port;
175                 int r;
176
177                 if ((name[0] == '.') &&
178                     ((name[1] == '\0') ||
179                      ((name[1] == '.') && (name[2] == '\0'))))
180                         continue;
181
182                 MKSTR(path, "%s/device/net/%s/%s",
183                       ibdev_path, name,
184                       (dev_type ? "dev_id" : "dev_port"));
185
186                 file = fopen(path, "rb");
187                 if (file == NULL) {
188                         if (errno != ENOENT)
189                                 continue;
190                         /*
191                          * Switch to dev_id when dev_port does not exist as
192                          * is the case with Linux kernel versions < 3.15.
193                          */
194 try_dev_id:
195                         match[0] = '\0';
196                         if (dev_type)
197                                 break;
198                         dev_type = 1;
199                         dev_port_prev = ~0u;
200                         rewinddir(dir);
201                         continue;
202                 }
203                 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
204                 fclose(file);
205                 if (r != 1)
206                         continue;
207                 /*
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.
211                  */
212                 if (dev_port == dev_port_prev)
213                         goto try_dev_id;
214                 dev_port_prev = dev_port;
215                 if (dev_port == 0)
216                         strlcpy(match, name, IF_NAMESIZE);
217         }
218         closedir(dir);
219         if (match[0] == '\0') {
220                 rte_errno = ENOENT;
221                 return -rte_errno;
222         }
223         strncpy(ifname, match, IF_NAMESIZE);
224         return 0;
225 }
226
227 #ifdef MLX5_GLUE
228
229 /**
230  * Suffix RTE_EAL_PMD_PATH with "-glue".
231  *
232  * This function performs a sanity check on RTE_EAL_PMD_PATH before
233  * suffixing its last component.
234  *
235  * @param buf[out]
236  *   Output buffer, should be large enough otherwise NULL is returned.
237  * @param size
238  *   Size of @p out.
239  *
240  * @return
241  *   Pointer to @p buf or @p NULL in case suffix cannot be appended.
242  */
243 static char *
244 mlx5_glue_path(char *buf, size_t size)
245 {
246         static const char *const bad[] = { "/", ".", "..", NULL };
247         const char *path = RTE_EAL_PMD_PATH;
248         size_t len = strlen(path);
249         size_t off;
250         int i;
251
252         while (len && path[len - 1] == '/')
253                 --len;
254         for (off = len; off && path[off - 1] != '/'; --off)
255                 ;
256         for (i = 0; bad[i]; ++i)
257                 if (!strncmp(path + off, bad[i], (int)(len - off)))
258                         goto error;
259         i = snprintf(buf, size, "%.*s-glue", (int)len, path);
260         if (i == -1 || (size_t)i >= size)
261                 goto error;
262         return buf;
263 error:
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");
267         return NULL;
268 }
269
270 static int
271 mlx5_glue_dlopen(void)
272 {
273         char glue_path[sizeof(RTE_EAL_PMD_PATH) - 1 + sizeof("-glue")];
274         void *handle = NULL;
275
276         char const *path[] = {
277                 /*
278                  * A basic security check is necessary before trusting
279                  * MLX5_GLUE_PATH, which may override RTE_EAL_PMD_PATH.
280                  */
281                 (geteuid() == getuid() && getegid() == getgid() ?
282                  getenv("MLX5_GLUE_PATH") : NULL),
283                 /*
284                  * When RTE_EAL_PMD_PATH is set, use its glue-suffixed
285                  * variant, otherwise let dlopen() look up libraries on its
286                  * own.
287                  */
288                 (*RTE_EAL_PMD_PATH ?
289                  mlx5_glue_path(glue_path, sizeof(glue_path)) : ""),
290         };
291         unsigned int i = 0;
292         void **sym;
293         const char *dlmsg;
294
295         while (!handle && i != RTE_DIM(path)) {
296                 const char *end;
297                 size_t len;
298                 int ret;
299
300                 if (!path[i]) {
301                         ++i;
302                         continue;
303                 }
304                 end = strpbrk(path[i], ":;");
305                 if (!end)
306                         end = path[i] + strlen(path[i]);
307                 len = end - path[i];
308                 ret = 0;
309                 do {
310                         char name[ret + 1];
311
312                         ret = snprintf(name, sizeof(name), "%.*s%s" MLX5_GLUE,
313                                        (int)len, path[i],
314                                        (!len || *(end - 1) == '/') ? "" : "/");
315                         if (ret == -1)
316                                 break;
317                         if (sizeof(name) != (size_t)ret + 1)
318                                 continue;
319                         DRV_LOG(DEBUG, "Looking for rdma-core glue as "
320                                 "\"%s\"", name);
321                         handle = dlopen(name, RTLD_LAZY);
322                         break;
323                 } while (1);
324                 path[i] = end + 1;
325                 if (!*end)
326                         ++i;
327         }
328         if (!handle) {
329                 rte_errno = EINVAL;
330                 dlmsg = dlerror();
331                 if (dlmsg)
332                         DRV_LOG(WARNING, "Cannot load glue library: %s", dlmsg);
333                 goto glue_error;
334         }
335         sym = dlsym(handle, "mlx5_glue");
336         if (!sym || !*sym) {
337                 rte_errno = EINVAL;
338                 dlmsg = dlerror();
339                 if (dlmsg)
340                         DRV_LOG(ERR, "Cannot resolve glue symbol: %s", dlmsg);
341                 goto glue_error;
342         }
343         mlx5_glue = *sym;
344         return 0;
345
346 glue_error:
347         if (handle)
348                 dlclose(handle);
349         return -1;
350 }
351
352 #endif
353
354 /**
355  * Initialization routine for run-time dependency on rdma-core.
356  */
357 void
358 mlx5_glue_constructor(void)
359 {
360         /*
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.
365          */
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);
370         /*
371          * MLX5_DEVICE_FATAL_CLEANUP tells ibv_destroy functions to
372          * cleanup all the Verbs resources even when the device was removed.
373          */
374         setenv("MLX5_DEVICE_FATAL_CLEANUP", "1", 1);
375
376 #ifdef MLX5_GLUE
377         if (mlx5_glue_dlopen() != 0)
378                 goto glue_error;
379 #endif
380
381 #ifdef RTE_LIBRTE_MLX5_DEBUG
382         /* Glue structure must not contain any NULL pointers. */
383         {
384                 unsigned int i;
385
386                 for (i = 0; i != sizeof(*mlx5_glue) / sizeof(void *); ++i)
387                         MLX5_ASSERT(((const void *const *)mlx5_glue)[i]);
388         }
389 #endif
390         if (strcmp(mlx5_glue->version, MLX5_GLUE_VERSION)) {
391                 rte_errno = EINVAL;
392                 DRV_LOG(ERR, "rdma-core glue \"%s\" mismatch: \"%s\" is "
393                         "required", mlx5_glue->version, MLX5_GLUE_VERSION);
394                 goto glue_error;
395         }
396         mlx5_glue->fork_init();
397         return;
398
399 glue_error:
400         DRV_LOG(WARNING, "Cannot initialize MLX5 common due to missing"
401                 " run-time dependency on rdma-core libraries (libibverbs,"
402                 " libmlx5)");
403         mlx5_glue = NULL;
404 }
405