xref: /dpdk/drivers/common/mlx5/linux/mlx5_common_os.c (revision 420bbdae89f29744847a166b831c675ad5affd2a)
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 			ret = 0;
70 			break;
71 		}
72 	}
73 	fclose(file);
74 	return 0;
75 }
76 
77 /**
78  * Extract port name, as a number, from sysfs or netlink information.
79  *
80  * @param[in] port_name_in
81  *   String representing the port name.
82  * @param[out] port_info_out
83  *   Port information, including port name as a number and port name
84  *   type if recognized
85  *
86  * @return
87  *   port_name field set according to recognized name format.
88  */
89 void
90 mlx5_translate_port_name(const char *port_name_in,
91 			 struct mlx5_switch_info *port_info_out)
92 {
93 	char pf_c1, pf_c2, vf_c1, vf_c2;
94 	char *end;
95 	int sc_items;
96 
97 	/*
98 	 * Check for port-name as a string of the form pf0vf0
99 	 * (support kernel ver >= 5.0 or OFED ver >= 4.6).
100 	 */
101 	sc_items = sscanf(port_name_in, "%c%c%d%c%c%d",
102 			  &pf_c1, &pf_c2, &port_info_out->pf_num,
103 			  &vf_c1, &vf_c2, &port_info_out->port_name);
104 	if (sc_items == 6 &&
105 	    pf_c1 == 'p' && pf_c2 == 'f' &&
106 	    vf_c1 == 'v' && vf_c2 == 'f') {
107 		port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_PFVF;
108 		return;
109 	}
110 	/*
111 	 * Check for port-name as a string of the form p0
112 	 * (support kernel ver >= 5.0, or OFED ver >= 4.6).
113 	 */
114 	sc_items = sscanf(port_name_in, "%c%d",
115 			  &pf_c1, &port_info_out->port_name);
116 	if (sc_items == 2 && pf_c1 == 'p') {
117 		port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UPLINK;
118 		return;
119 	}
120 	/*
121 	 * Check for port-name as a string of the form pf0
122 	 * (support kernel ver >= 5.7 for HPF representor on BF).
123 	 */
124 	sc_items = sscanf(port_name_in, "%c%c%d",
125 			  &pf_c1, &pf_c2, &port_info_out->pf_num);
126 	if (sc_items == 3 && pf_c1 == 'p' && pf_c2 == 'f') {
127 		port_info_out->port_name = -1;
128 		port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_PFHPF;
129 		return;
130 	}
131 	/* Check for port-name as a number (support kernel ver < 5.0 */
132 	errno = 0;
133 	port_info_out->port_name = strtol(port_name_in, &end, 0);
134 	if (!errno &&
135 	    (size_t)(end - port_name_in) == strlen(port_name_in)) {
136 		port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_LEGACY;
137 		return;
138 	}
139 	port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN;
140 }
141 
142 /**
143  * Get kernel interface name from IB device path.
144  *
145  * @param[in] ibdev_path
146  *   Pointer to IB device path.
147  * @param[out] ifname
148  *   Interface name output buffer.
149  *
150  * @return
151  *   0 on success, a negative errno value otherwise and rte_errno is set.
152  */
153 int
154 mlx5_get_ifname_sysfs(const char *ibdev_path, char *ifname)
155 {
156 	DIR *dir;
157 	struct dirent *dent;
158 	unsigned int dev_type = 0;
159 	unsigned int dev_port_prev = ~0u;
160 	char match[IF_NAMESIZE] = "";
161 
162 	MLX5_ASSERT(ibdev_path);
163 	{
164 		MKSTR(path, "%s/device/net", ibdev_path);
165 
166 		dir = opendir(path);
167 		if (dir == NULL) {
168 			rte_errno = errno;
169 			return -rte_errno;
170 		}
171 	}
172 	while ((dent = readdir(dir)) != NULL) {
173 		char *name = dent->d_name;
174 		FILE *file;
175 		unsigned int dev_port;
176 		int r;
177 
178 		if ((name[0] == '.') &&
179 		    ((name[1] == '\0') ||
180 		     ((name[1] == '.') && (name[2] == '\0'))))
181 			continue;
182 
183 		MKSTR(path, "%s/device/net/%s/%s",
184 		      ibdev_path, name,
185 		      (dev_type ? "dev_id" : "dev_port"));
186 
187 		file = fopen(path, "rb");
188 		if (file == NULL) {
189 			if (errno != ENOENT)
190 				continue;
191 			/*
192 			 * Switch to dev_id when dev_port does not exist as
193 			 * is the case with Linux kernel versions < 3.15.
194 			 */
195 try_dev_id:
196 			match[0] = '\0';
197 			if (dev_type)
198 				break;
199 			dev_type = 1;
200 			dev_port_prev = ~0u;
201 			rewinddir(dir);
202 			continue;
203 		}
204 		r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
205 		fclose(file);
206 		if (r != 1)
207 			continue;
208 		/*
209 		 * Switch to dev_id when dev_port returns the same value for
210 		 * all ports. May happen when using a MOFED release older than
211 		 * 3.0 with a Linux kernel >= 3.15.
212 		 */
213 		if (dev_port == dev_port_prev)
214 			goto try_dev_id;
215 		dev_port_prev = dev_port;
216 		if (dev_port == 0)
217 			strlcpy(match, name, IF_NAMESIZE);
218 	}
219 	closedir(dir);
220 	if (match[0] == '\0') {
221 		rte_errno = ENOENT;
222 		return -rte_errno;
223 	}
224 	strncpy(ifname, match, IF_NAMESIZE);
225 	return 0;
226 }
227 
228 #ifdef MLX5_GLUE
229 
230 /**
231  * Suffix RTE_EAL_PMD_PATH with "-glue".
232  *
233  * This function performs a sanity check on RTE_EAL_PMD_PATH before
234  * suffixing its last component.
235  *
236  * @param buf[out]
237  *   Output buffer, should be large enough otherwise NULL is returned.
238  * @param size
239  *   Size of @p out.
240  *
241  * @return
242  *   Pointer to @p buf or @p NULL in case suffix cannot be appended.
243  */
244 static char *
245 mlx5_glue_path(char *buf, size_t size)
246 {
247 	static const char *const bad[] = { "/", ".", "..", NULL };
248 	const char *path = RTE_EAL_PMD_PATH;
249 	size_t len = strlen(path);
250 	size_t off;
251 	int i;
252 
253 	while (len && path[len - 1] == '/')
254 		--len;
255 	for (off = len; off && path[off - 1] != '/'; --off)
256 		;
257 	for (i = 0; bad[i]; ++i)
258 		if (!strncmp(path + off, bad[i], (int)(len - off)))
259 			goto error;
260 	i = snprintf(buf, size, "%.*s-glue", (int)len, path);
261 	if (i == -1 || (size_t)i >= size)
262 		goto error;
263 	return buf;
264 error:
265 	RTE_LOG(ERR, PMD, "unable to append \"-glue\" to last component of"
266 		" RTE_EAL_PMD_PATH (\"" RTE_EAL_PMD_PATH "\"), please"
267 		" re-configure DPDK");
268 	return NULL;
269 }
270 
271 static int
272 mlx5_glue_dlopen(void)
273 {
274 	char glue_path[sizeof(RTE_EAL_PMD_PATH) - 1 + sizeof("-glue")];
275 	void *handle = NULL;
276 
277 	char const *path[] = {
278 		/*
279 		 * A basic security check is necessary before trusting
280 		 * MLX5_GLUE_PATH, which may override RTE_EAL_PMD_PATH.
281 		 */
282 		(geteuid() == getuid() && getegid() == getgid() ?
283 		 getenv("MLX5_GLUE_PATH") : NULL),
284 		/*
285 		 * When RTE_EAL_PMD_PATH is set, use its glue-suffixed
286 		 * variant, otherwise let dlopen() look up libraries on its
287 		 * own.
288 		 */
289 		(*RTE_EAL_PMD_PATH ?
290 		 mlx5_glue_path(glue_path, sizeof(glue_path)) : ""),
291 	};
292 	unsigned int i = 0;
293 	void **sym;
294 	const char *dlmsg;
295 
296 	while (!handle && i != RTE_DIM(path)) {
297 		const char *end;
298 		size_t len;
299 		int ret;
300 
301 		if (!path[i]) {
302 			++i;
303 			continue;
304 		}
305 		end = strpbrk(path[i], ":;");
306 		if (!end)
307 			end = path[i] + strlen(path[i]);
308 		len = end - path[i];
309 		ret = 0;
310 		do {
311 			char name[ret + 1];
312 
313 			ret = snprintf(name, sizeof(name), "%.*s%s" MLX5_GLUE,
314 				       (int)len, path[i],
315 				       (!len || *(end - 1) == '/') ? "" : "/");
316 			if (ret == -1)
317 				break;
318 			if (sizeof(name) != (size_t)ret + 1)
319 				continue;
320 			DRV_LOG(DEBUG, "Looking for rdma-core glue as "
321 				"\"%s\"", name);
322 			handle = dlopen(name, RTLD_LAZY);
323 			break;
324 		} while (1);
325 		path[i] = end + 1;
326 		if (!*end)
327 			++i;
328 	}
329 	if (!handle) {
330 		rte_errno = EINVAL;
331 		dlmsg = dlerror();
332 		if (dlmsg)
333 			DRV_LOG(WARNING, "Cannot load glue library: %s", dlmsg);
334 		goto glue_error;
335 	}
336 	sym = dlsym(handle, "mlx5_glue");
337 	if (!sym || !*sym) {
338 		rte_errno = EINVAL;
339 		dlmsg = dlerror();
340 		if (dlmsg)
341 			DRV_LOG(ERR, "Cannot resolve glue symbol: %s", dlmsg);
342 		goto glue_error;
343 	}
344 	mlx5_glue = *sym;
345 	return 0;
346 
347 glue_error:
348 	if (handle)
349 		dlclose(handle);
350 	return -1;
351 }
352 
353 #endif
354 
355 /**
356  * Initialization routine for run-time dependency on rdma-core.
357  */
358 void
359 mlx5_glue_constructor(void)
360 {
361 	/*
362 	 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
363 	 * huge pages. Calling ibv_fork_init() during init allows
364 	 * applications to use fork() safely for purposes other than
365 	 * using this PMD, which is not supported in forked processes.
366 	 */
367 	setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
368 	/* Match the size of Rx completion entry to the size of a cacheline. */
369 	if (RTE_CACHE_LINE_SIZE == 128)
370 		setenv("MLX5_CQE_SIZE", "128", 0);
371 	/*
372 	 * MLX5_DEVICE_FATAL_CLEANUP tells ibv_destroy functions to
373 	 * cleanup all the Verbs resources even when the device was removed.
374 	 */
375 	setenv("MLX5_DEVICE_FATAL_CLEANUP", "1", 1);
376 
377 #ifdef MLX5_GLUE
378 	if (mlx5_glue_dlopen() != 0)
379 		goto glue_error;
380 #endif
381 
382 #ifdef RTE_LIBRTE_MLX5_DEBUG
383 	/* Glue structure must not contain any NULL pointers. */
384 	{
385 		unsigned int i;
386 
387 		for (i = 0; i != sizeof(*mlx5_glue) / sizeof(void *); ++i)
388 			MLX5_ASSERT(((const void *const *)mlx5_glue)[i]);
389 	}
390 #endif
391 	if (strcmp(mlx5_glue->version, MLX5_GLUE_VERSION)) {
392 		rte_errno = EINVAL;
393 		DRV_LOG(ERR, "rdma-core glue \"%s\" mismatch: \"%s\" is "
394 			"required", mlx5_glue->version, MLX5_GLUE_VERSION);
395 		goto glue_error;
396 	}
397 	mlx5_glue->fork_init();
398 	return;
399 
400 glue_error:
401 	DRV_LOG(WARNING, "Cannot initialize MLX5 common due to missing"
402 		" run-time dependency on rdma-core libraries (libibverbs,"
403 		" libmlx5)");
404 	mlx5_glue = NULL;
405 }
406 
407