net/mlx5: select driver by class device argument
[dpdk.git] / drivers / common / mlx5 / mlx5_common.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2019 Mellanox Technologies, Ltd
3  */
4
5 #include <dlfcn.h>
6 #include <unistd.h>
7 #include <string.h>
8 #include <stdio.h>
9
10 #include <rte_errno.h>
11
12 #include "mlx5_common.h"
13 #include "mlx5_common_utils.h"
14 #include "mlx5_glue.h"
15
16
17 int mlx5_common_logtype;
18
19
20 /**
21  * Get PCI information by sysfs device path.
22  *
23  * @param dev_path
24  *   Pointer to device sysfs folder name.
25  * @param[out] pci_addr
26  *   PCI bus address output buffer.
27  *
28  * @return
29  *   0 on success, a negative errno value otherwise and rte_errno is set.
30  */
31 int
32 mlx5_dev_to_pci_addr(const char *dev_path,
33                      struct rte_pci_addr *pci_addr)
34 {
35         FILE *file;
36         char line[32];
37         MKSTR(path, "%s/device/uevent", dev_path);
38
39         file = fopen(path, "rb");
40         if (file == NULL) {
41                 rte_errno = errno;
42                 return -rte_errno;
43         }
44         while (fgets(line, sizeof(line), file) == line) {
45                 size_t len = strlen(line);
46                 int ret;
47
48                 /* Truncate long lines. */
49                 if (len == (sizeof(line) - 1))
50                         while (line[(len - 1)] != '\n') {
51                                 ret = fgetc(file);
52                                 if (ret == EOF)
53                                         break;
54                                 line[(len - 1)] = ret;
55                         }
56                 /* Extract information. */
57                 if (sscanf(line,
58                            "PCI_SLOT_NAME="
59                            "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
60                            &pci_addr->domain,
61                            &pci_addr->bus,
62                            &pci_addr->devid,
63                            &pci_addr->function) == 4) {
64                         ret = 0;
65                         break;
66                 }
67         }
68         fclose(file);
69         return 0;
70 }
71
72 static int
73 mlx5_class_check_handler(__rte_unused const char *key, const char *value,
74                          void *opaque)
75 {
76         enum mlx5_class *ret = opaque;
77
78         if (strcmp(value, "vdpa") == 0) {
79                 *ret = MLX5_CLASS_VDPA;
80         } else if (strcmp(value, "net") == 0) {
81                 *ret = MLX5_CLASS_NET;
82         } else {
83                 DRV_LOG(ERR, "Invalid mlx5 class %s. Maybe typo in device"
84                         " class argument setting?", value);
85                 *ret = MLX5_CLASS_INVALID;
86         }
87         return 0;
88 }
89
90 enum mlx5_class
91 mlx5_class_get(struct rte_devargs *devargs)
92 {
93         struct rte_kvargs *kvlist;
94         const char *key = MLX5_CLASS_ARG_NAME;
95         enum mlx5_class ret = MLX5_CLASS_NET;
96
97         if (devargs == NULL)
98                 return ret;
99         kvlist = rte_kvargs_parse(devargs->args, NULL);
100         if (kvlist == NULL)
101                 return ret;
102         if (rte_kvargs_count(kvlist, key))
103                 rte_kvargs_process(kvlist, key, mlx5_class_check_handler, &ret);
104         rte_kvargs_free(kvlist);
105         return ret;
106 }
107
108 #ifdef RTE_IBVERBS_LINK_DLOPEN
109
110 /**
111  * Suffix RTE_EAL_PMD_PATH with "-glue".
112  *
113  * This function performs a sanity check on RTE_EAL_PMD_PATH before
114  * suffixing its last component.
115  *
116  * @param buf[out]
117  *   Output buffer, should be large enough otherwise NULL is returned.
118  * @param size
119  *   Size of @p out.
120  *
121  * @return
122  *   Pointer to @p buf or @p NULL in case suffix cannot be appended.
123  */
124 static char *
125 mlx5_glue_path(char *buf, size_t size)
126 {
127         static const char *const bad[] = { "/", ".", "..", NULL };
128         const char *path = RTE_EAL_PMD_PATH;
129         size_t len = strlen(path);
130         size_t off;
131         int i;
132
133         while (len && path[len - 1] == '/')
134                 --len;
135         for (off = len; off && path[off - 1] != '/'; --off)
136                 ;
137         for (i = 0; bad[i]; ++i)
138                 if (!strncmp(path + off, bad[i], (int)(len - off)))
139                         goto error;
140         i = snprintf(buf, size, "%.*s-glue", (int)len, path);
141         if (i == -1 || (size_t)i >= size)
142                 goto error;
143         return buf;
144 error:
145         RTE_LOG(ERR, PMD, "unable to append \"-glue\" to last component of"
146                 " RTE_EAL_PMD_PATH (\"" RTE_EAL_PMD_PATH "\"), please"
147                 " re-configure DPDK");
148         return NULL;
149 }
150 #endif
151
152 /**
153  * Initialization routine for run-time dependency on rdma-core.
154  */
155 RTE_INIT_PRIO(mlx5_glue_init, CLASS)
156 {
157         void *handle = NULL;
158
159         /* Initialize common log type. */
160         mlx5_common_logtype = rte_log_register("pmd.common.mlx5");
161         if (mlx5_common_logtype >= 0)
162                 rte_log_set_level(mlx5_common_logtype, RTE_LOG_NOTICE);
163         /*
164          * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
165          * huge pages. Calling ibv_fork_init() during init allows
166          * applications to use fork() safely for purposes other than
167          * using this PMD, which is not supported in forked processes.
168          */
169         setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
170         /* Match the size of Rx completion entry to the size of a cacheline. */
171         if (RTE_CACHE_LINE_SIZE == 128)
172                 setenv("MLX5_CQE_SIZE", "128", 0);
173         /*
174          * MLX5_DEVICE_FATAL_CLEANUP tells ibv_destroy functions to
175          * cleanup all the Verbs resources even when the device was removed.
176          */
177         setenv("MLX5_DEVICE_FATAL_CLEANUP", "1", 1);
178         /* The glue initialization was done earlier by mlx5 common library. */
179 #ifdef RTE_IBVERBS_LINK_DLOPEN
180         char glue_path[sizeof(RTE_EAL_PMD_PATH) - 1 + sizeof("-glue")];
181         const char *path[] = {
182                 /*
183                  * A basic security check is necessary before trusting
184                  * MLX5_GLUE_PATH, which may override RTE_EAL_PMD_PATH.
185                  */
186                 (geteuid() == getuid() && getegid() == getgid() ?
187                  getenv("MLX5_GLUE_PATH") : NULL),
188                 /*
189                  * When RTE_EAL_PMD_PATH is set, use its glue-suffixed
190                  * variant, otherwise let dlopen() look up libraries on its
191                  * own.
192                  */
193                 (*RTE_EAL_PMD_PATH ?
194                  mlx5_glue_path(glue_path, sizeof(glue_path)) : ""),
195         };
196         unsigned int i = 0;
197         void **sym;
198         const char *dlmsg;
199
200         while (!handle && i != RTE_DIM(path)) {
201                 const char *end;
202                 size_t len;
203                 int ret;
204
205                 if (!path[i]) {
206                         ++i;
207                         continue;
208                 }
209                 end = strpbrk(path[i], ":;");
210                 if (!end)
211                         end = path[i] + strlen(path[i]);
212                 len = end - path[i];
213                 ret = 0;
214                 do {
215                         char name[ret + 1];
216
217                         ret = snprintf(name, sizeof(name), "%.*s%s" MLX5_GLUE,
218                                        (int)len, path[i],
219                                        (!len || *(end - 1) == '/') ? "" : "/");
220                         if (ret == -1)
221                                 break;
222                         if (sizeof(name) != (size_t)ret + 1)
223                                 continue;
224                         DRV_LOG(DEBUG, "Looking for rdma-core glue as "
225                                 "\"%s\"", name);
226                         handle = dlopen(name, RTLD_LAZY);
227                         break;
228                 } while (1);
229                 path[i] = end + 1;
230                 if (!*end)
231                         ++i;
232         }
233         if (!handle) {
234                 rte_errno = EINVAL;
235                 dlmsg = dlerror();
236                 if (dlmsg)
237                         DRV_LOG(WARNING, "Cannot load glue library: %s", dlmsg);
238                 goto glue_error;
239         }
240         sym = dlsym(handle, "mlx5_glue");
241         if (!sym || !*sym) {
242                 rte_errno = EINVAL;
243                 dlmsg = dlerror();
244                 if (dlmsg)
245                         DRV_LOG(ERR, "Cannot resolve glue symbol: %s", dlmsg);
246                 goto glue_error;
247         }
248         mlx5_glue = *sym;
249 #endif /* RTE_IBVERBS_LINK_DLOPEN */
250 #ifndef NDEBUG
251         /* Glue structure must not contain any NULL pointers. */
252         {
253                 unsigned int i;
254
255                 for (i = 0; i != sizeof(*mlx5_glue) / sizeof(void *); ++i)
256                         assert(((const void *const *)mlx5_glue)[i]);
257         }
258 #endif
259         if (strcmp(mlx5_glue->version, MLX5_GLUE_VERSION)) {
260                 rte_errno = EINVAL;
261                 DRV_LOG(ERR, "rdma-core glue \"%s\" mismatch: \"%s\" is "
262                         "required", mlx5_glue->version, MLX5_GLUE_VERSION);
263                 goto glue_error;
264         }
265         mlx5_glue->fork_init();
266         return;
267 glue_error:
268         if (handle)
269                 dlclose(handle);
270         DRV_LOG(WARNING, "Cannot initialize MLX5 common due to missing"
271                 " run-time dependency on rdma-core libraries (libibverbs,"
272                 " libmlx5)");
273         mlx5_glue = NULL;
274         return;
275 }