xref: /freebsd/contrib/ofed/libibverbs/init.c (revision a25896ca1270e25b657ceaa8d47d5699515f5c25)
1 /*
2  * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
3  * Copyright (c) 2006 Cisco Systems, Inc.  All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
19  *      - Redistributions in binary form must reproduce the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer in the documentation and/or other materials
22  *        provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33 #define _GNU_SOURCE
34 #include <config.h>
35 
36 #include <stdlib.h>
37 #include <string.h>
38 #include <glob.h>
39 #include <stdio.h>
40 #include <dlfcn.h>
41 #include <unistd.h>
42 #include <sys/stat.h>
43 #include <sys/types.h>
44 #include <sys/time.h>
45 #include <sys/resource.h>
46 #include <dirent.h>
47 #include <errno.h>
48 #include <assert.h>
49 
50 #include "ibverbs.h"
51 
52 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
53 
54 int abi_ver;
55 
56 struct ibv_sysfs_dev {
57 	char		        sysfs_name[IBV_SYSFS_NAME_MAX];
58 	char		        ibdev_name[IBV_SYSFS_NAME_MAX];
59 	char		        sysfs_path[IBV_SYSFS_PATH_MAX];
60 	char		        ibdev_path[IBV_SYSFS_PATH_MAX];
61 	struct ibv_sysfs_dev   *next;
62 	int			abi_ver;
63 	int			have_driver;
64 };
65 
66 struct ibv_driver_name {
67 	char		       *name;
68 	struct ibv_driver_name *next;
69 };
70 
71 struct ibv_driver {
72 	const char	       *name;
73 	verbs_driver_init_func	verbs_init_func;
74 	struct ibv_driver      *next;
75 };
76 
77 static struct ibv_sysfs_dev *sysfs_dev_list;
78 static struct ibv_driver_name *driver_name_list;
79 static struct ibv_driver *head_driver, *tail_driver;
80 
81 static int find_sysfs_devs(void)
82 {
83 	char class_path[IBV_SYSFS_PATH_MAX];
84 	struct ibv_sysfs_dev *sysfs_dev = NULL;
85 	char value[8];
86 	int ret = 0;
87 	int i;
88 
89 	snprintf(class_path, sizeof class_path, "%s/class/infiniband_verbs",
90 		 ibv_get_sysfs_path());
91 
92 	for (i = 0; i < 256; i++) {
93 		if (!sysfs_dev)
94 			sysfs_dev = malloc(sizeof *sysfs_dev);
95 		if (!sysfs_dev) {
96 			ret = ENOMEM;
97 			goto out;
98 		}
99 
100 		snprintf(sysfs_dev->sysfs_path, sizeof sysfs_dev->sysfs_path,
101 			 "%s/uverbs%d", class_path, i);
102 
103 		snprintf(sysfs_dev->sysfs_name, sizeof sysfs_dev->sysfs_name,
104 			"uverbs%d", i);
105 
106 		if (ibv_read_sysfs_file(sysfs_dev->sysfs_path, "ibdev",
107 					sysfs_dev->ibdev_name,
108 					sizeof sysfs_dev->ibdev_name) < 0)
109 			continue;
110 
111 		snprintf(sysfs_dev->ibdev_path, sizeof sysfs_dev->ibdev_path,
112 			 "%s/class/infiniband/%s", ibv_get_sysfs_path(),
113 			 sysfs_dev->ibdev_name);
114 
115 		sysfs_dev->next        = sysfs_dev_list;
116 		sysfs_dev->have_driver = 0;
117 		if (ibv_read_sysfs_file(sysfs_dev->sysfs_path, "abi_version",
118 					value, sizeof value) > 0)
119 			sysfs_dev->abi_ver = strtol(value, NULL, 10);
120 		else
121 			sysfs_dev->abi_ver = 0;
122 
123 		sysfs_dev_list = sysfs_dev;
124 		sysfs_dev      = NULL;
125 	}
126 
127  out:
128 	if (sysfs_dev)
129 		free(sysfs_dev);
130 
131 	return ret;
132 }
133 
134 void verbs_register_driver(const char *name,
135 			   verbs_driver_init_func verbs_init_func)
136 {
137 	struct ibv_driver *driver;
138 
139 	driver = malloc(sizeof *driver);
140 	if (!driver) {
141 		fprintf(stderr, PFX "Warning: couldn't allocate driver for %s\n", name);
142 		return;
143 	}
144 
145 	driver->name            = name;
146 	driver->verbs_init_func = verbs_init_func;
147 	driver->next            = NULL;
148 
149 	if (tail_driver)
150 		tail_driver->next = driver;
151 	else
152 		head_driver = driver;
153 	tail_driver = driver;
154 }
155 
156 static struct ibv_device *try_driver(struct ibv_driver *driver,
157 				     struct ibv_sysfs_dev *sysfs_dev)
158 {
159 	struct verbs_device *vdev;
160 	struct ibv_device *dev;
161 	char value[16];
162 
163 	vdev = driver->verbs_init_func(sysfs_dev->sysfs_path, sysfs_dev->abi_ver);
164 	if (!vdev)
165 		return NULL;
166 
167 	dev = &vdev->device;
168 	assert(dev->_ops._dummy1 == NULL);
169 	assert(dev->_ops._dummy2 == NULL);
170 
171 	if (ibv_read_sysfs_file(sysfs_dev->ibdev_path, "node_type", value, sizeof value) < 0) {
172 		fprintf(stderr, PFX "Warning: no node_type attr under %s.\n",
173 			sysfs_dev->ibdev_path);
174 			dev->node_type = IBV_NODE_UNKNOWN;
175 	} else {
176 		dev->node_type = strtol(value, NULL, 10);
177 		if (dev->node_type < IBV_NODE_CA || dev->node_type > IBV_NODE_USNIC_UDP)
178 			dev->node_type = IBV_NODE_UNKNOWN;
179 	}
180 
181 	switch (dev->node_type) {
182 	case IBV_NODE_CA:
183 	case IBV_NODE_SWITCH:
184 	case IBV_NODE_ROUTER:
185 		dev->transport_type = IBV_TRANSPORT_IB;
186 		break;
187 	case IBV_NODE_RNIC:
188 		dev->transport_type = IBV_TRANSPORT_IWARP;
189 		break;
190 	case IBV_NODE_USNIC:
191 		dev->transport_type = IBV_TRANSPORT_USNIC;
192 		break;
193 	case IBV_NODE_USNIC_UDP:
194 		dev->transport_type = IBV_TRANSPORT_USNIC_UDP;
195 		break;
196 	default:
197 		dev->transport_type = IBV_TRANSPORT_UNKNOWN;
198 		break;
199 	}
200 
201 	strcpy(dev->dev_name,   sysfs_dev->sysfs_name);
202 	strcpy(dev->dev_path,   sysfs_dev->sysfs_path);
203 	strcpy(dev->name,       sysfs_dev->ibdev_name);
204 	strcpy(dev->ibdev_path, sysfs_dev->ibdev_path);
205 
206 	return dev;
207 }
208 
209 static struct ibv_device *try_drivers(struct ibv_sysfs_dev *sysfs_dev)
210 {
211 	struct ibv_driver *driver;
212 	struct ibv_device *dev;
213 
214 	for (driver = head_driver; driver; driver = driver->next) {
215 		dev = try_driver(driver, sysfs_dev);
216 		if (dev)
217 			return dev;
218 	}
219 
220 	return NULL;
221 }
222 
223 static int check_abi_version(const char *path)
224 {
225 	char value[8];
226 
227 	if (ibv_read_sysfs_file(path, "class/infiniband_verbs/abi_version",
228 				value, sizeof value) < 0) {
229 		return ENOSYS;
230 	}
231 
232 	abi_ver = strtol(value, NULL, 10);
233 
234 	if (abi_ver < IB_USER_VERBS_MIN_ABI_VERSION ||
235 	    abi_ver > IB_USER_VERBS_MAX_ABI_VERSION) {
236 		fprintf(stderr, PFX "Fatal: kernel ABI version %d "
237 			"doesn't match library version %d.\n",
238 			abi_ver, IB_USER_VERBS_MAX_ABI_VERSION);
239 		return ENOSYS;
240 	}
241 
242 	return 0;
243 }
244 
245 static void check_memlock_limit(void)
246 {
247 	struct rlimit rlim;
248 
249 	if (!geteuid())
250 		return;
251 
252 	if (getrlimit(RLIMIT_MEMLOCK, &rlim)) {
253 		fprintf(stderr, PFX "Warning: getrlimit(RLIMIT_MEMLOCK) failed.");
254 		return;
255 	}
256 
257 	if (rlim.rlim_cur <= 32768)
258 		fprintf(stderr, PFX "Warning: RLIMIT_MEMLOCK is %lu bytes.\n"
259 			"    This will severely limit memory registrations.\n",
260 			rlim.rlim_cur);
261 }
262 
263 static void add_device(struct ibv_device *dev,
264 		       struct ibv_device ***dev_list,
265 		       int *num_devices,
266 		       int *list_size)
267 {
268 	struct ibv_device **new_list;
269 
270 	if (*list_size <= *num_devices) {
271 		*list_size = *list_size ? *list_size * 2 : 1;
272 		new_list = realloc(*dev_list, *list_size * sizeof (struct ibv_device *));
273 		if (!new_list)
274 			return;
275 		*dev_list = new_list;
276 	}
277 
278 	(*dev_list)[(*num_devices)++] = dev;
279 }
280 
281 int ibverbs_init(struct ibv_device ***list)
282 {
283 	const char *sysfs_path;
284 	struct ibv_sysfs_dev *sysfs_dev, *next_dev;
285 	struct ibv_device *device;
286 	int num_devices = 0;
287 	int list_size = 0;
288 	int statically_linked = 0;
289 	int no_driver = 0;
290 	int ret;
291 
292 	*list = NULL;
293 
294 	if (getenv("RDMAV_FORK_SAFE") || getenv("IBV_FORK_SAFE"))
295 		if (ibv_fork_init())
296 			fprintf(stderr, PFX "Warning: fork()-safety requested "
297 				"but init failed\n");
298 
299 	sysfs_path = ibv_get_sysfs_path();
300 	if (!sysfs_path)
301 		return -ENOSYS;
302 
303 	ret = check_abi_version(sysfs_path);
304 	if (ret)
305 		return -ret;
306 
307 	check_memlock_limit();
308 
309 	ret = find_sysfs_devs();
310 	if (ret)
311 		return -ret;
312 
313 	for (sysfs_dev = sysfs_dev_list; sysfs_dev; sysfs_dev = sysfs_dev->next) {
314 		device = try_drivers(sysfs_dev);
315 		if (device) {
316 			add_device(device, list, &num_devices, &list_size);
317 			sysfs_dev->have_driver = 1;
318 		} else
319 			no_driver = 1;
320 	}
321 
322 	if (!no_driver)
323 		goto out;
324 
325 	/*
326 	 * Check if we can dlopen() ourselves.  If this fails,
327 	 * libibverbs is probably statically linked into the
328 	 * executable, and we should just give up, since trying to
329 	 * dlopen() a driver module will fail spectacularly (loading a
330 	 * driver .so will bring in dynamic copies of libibverbs and
331 	 * libdl to go along with the static copies the executable
332 	 * has, which quickly leads to a crash.
333 	 */
334 	{
335 		void *hand = dlopen(NULL, RTLD_NOW);
336 		if (!hand) {
337 			fprintf(stderr, PFX "Warning: dlopen(NULL) failed, "
338 				"assuming static linking.\n");
339 			statically_linked = 1;
340 			goto out;
341 		}
342 		dlclose(hand);
343 	}
344 
345 	for (sysfs_dev = sysfs_dev_list; sysfs_dev; sysfs_dev = sysfs_dev->next) {
346 		if (sysfs_dev->have_driver)
347 			continue;
348 
349 		device = try_drivers(sysfs_dev);
350 		if (device) {
351 			add_device(device, list, &num_devices, &list_size);
352 			sysfs_dev->have_driver = 1;
353 		}
354 	}
355 
356 out:
357 	for (sysfs_dev = sysfs_dev_list,
358 		     next_dev = sysfs_dev ? sysfs_dev->next : NULL;
359 	     sysfs_dev;
360 	     sysfs_dev = next_dev, next_dev = sysfs_dev ? sysfs_dev->next : NULL) {
361 		if (!sysfs_dev->have_driver && getenv("IBV_SHOW_WARNINGS")) {
362 			fprintf(stderr, PFX "Warning: no userspace device-specific "
363 				"driver found for %s\n", sysfs_dev->sysfs_path);
364 			if (statically_linked)
365 				fprintf(stderr, "	When linking libibverbs statically, "
366 					"driver must be statically linked too.\n");
367 		}
368 		free(sysfs_dev);
369 	}
370 
371 	return num_devices;
372 }
373