2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
3 * Copyright (c) 2006 Cisco Systems, Inc. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
36 #endif /* HAVE_CONFIG_H */
45 #include <sys/types.h>
47 #include <sys/resource.h>
55 struct ibv_sysfs_dev {
56 char sysfs_name[IBV_SYSFS_NAME_MAX];
57 char ibdev_name[IBV_SYSFS_NAME_MAX];
58 char sysfs_path[IBV_SYSFS_PATH_MAX];
59 char ibdev_path[IBV_SYSFS_PATH_MAX];
60 struct ibv_sysfs_dev *next;
65 struct ibv_driver_name {
67 struct ibv_driver_name *next;
72 ibv_driver_init_func init_func;
73 struct ibv_driver *next;
76 static struct ibv_sysfs_dev *sysfs_dev_list;
77 static struct ibv_driver_name *driver_name_list;
78 static struct ibv_driver *head_driver, *tail_driver;
80 static int find_sysfs_devs(void)
83 char class_path[IBV_SYSFS_PATH_MAX];
86 struct ibv_sysfs_dev *sysfs_dev = NULL;
90 snprintf(class_path, sizeof class_path, "%s/class/infiniband_verbs",
91 ibv_get_sysfs_path());
93 class_dir = opendir(class_path);
97 while ((dent = readdir(class_dir))) {
100 if (dent->d_name[0] == '.')
104 sysfs_dev = malloc(sizeof *sysfs_dev);
110 snprintf(sysfs_dev->sysfs_path, sizeof sysfs_dev->sysfs_path,
111 "%s/%s", class_path, dent->d_name);
113 if (stat(sysfs_dev->sysfs_path, &buf)) {
114 fprintf(stderr, PFX "Warning: couldn't stat '%s'.\n",
115 sysfs_dev->sysfs_path);
119 if (!S_ISDIR(buf.st_mode))
122 snprintf(sysfs_dev->sysfs_name, sizeof sysfs_dev->sysfs_name,
125 if (ibv_read_sysfs_file(sysfs_dev->sysfs_path, "ibdev",
126 sysfs_dev->ibdev_name,
127 sizeof sysfs_dev->ibdev_name) < 0) {
128 fprintf(stderr, PFX "Warning: no ibdev class attr for '%s'.\n",
133 snprintf(sysfs_dev->ibdev_path, sizeof sysfs_dev->ibdev_path,
134 "%s/class/infiniband/%s", ibv_get_sysfs_path(),
135 sysfs_dev->ibdev_name);
137 sysfs_dev->next = sysfs_dev_list;
138 sysfs_dev->have_driver = 0;
139 if (ibv_read_sysfs_file(sysfs_dev->sysfs_path, "abi_version",
140 value, sizeof value) > 0)
141 sysfs_dev->abi_ver = strtol(value, NULL, 10);
143 sysfs_dev->abi_ver = 0;
145 sysfs_dev_list = sysfs_dev;
156 char class_path[IBV_SYSFS_PATH_MAX];
157 struct ibv_sysfs_dev *sysfs_dev = NULL;
162 snprintf(class_path, sizeof class_path, "%s/class/infiniband_verbs",
163 ibv_get_sysfs_path());
165 for (i = 0; i < 256; i++) {
167 sysfs_dev = malloc(sizeof *sysfs_dev);
173 snprintf(sysfs_dev->sysfs_path, sizeof sysfs_dev->sysfs_path,
174 "%s/uverbs%d", class_path, i);
176 snprintf(sysfs_dev->sysfs_name, sizeof sysfs_dev->sysfs_name,
179 if (ibv_read_sysfs_file(sysfs_dev->sysfs_path, "ibdev",
180 sysfs_dev->ibdev_name,
181 sizeof sysfs_dev->ibdev_name) < 0)
184 snprintf(sysfs_dev->ibdev_path, sizeof sysfs_dev->ibdev_path,
185 "%s/class/infiniband/%s", ibv_get_sysfs_path(),
186 sysfs_dev->ibdev_name);
188 sysfs_dev->next = sysfs_dev_list;
189 sysfs_dev->have_driver = 0;
190 if (ibv_read_sysfs_file(sysfs_dev->sysfs_path, "abi_version",
191 value, sizeof value) > 0)
192 sysfs_dev->abi_ver = strtol(value, NULL, 10);
194 sysfs_dev->abi_ver = 0;
196 sysfs_dev_list = sysfs_dev;
209 void ibv_register_driver(const char *name, ibv_driver_init_func init_func)
211 struct ibv_driver *driver;
213 driver = malloc(sizeof *driver);
215 fprintf(stderr, PFX "Warning: couldn't allocate driver for %s\n", name);
220 driver->init_func = init_func;
224 tail_driver->next = driver;
226 head_driver = driver;
227 tail_driver = driver;
230 static void load_driver(const char *name)
235 #define __IBV_QUOTE(x) #x
236 #define IBV_QUOTE(x) __IBV_QUOTE(x)
238 if (asprintf(&so_name,
240 "%s-" IBV_QUOTE(IBV_DEVICE_LIBRARY_EXTENSION) ".so" :
241 "lib%s-" IBV_QUOTE(IBV_DEVICE_LIBRARY_EXTENSION) ".so",
243 fprintf(stderr, PFX "Warning: couldn't load driver '%s'.\n",
248 dlhandle = dlopen(so_name, RTLD_NOW);
250 fprintf(stderr, PFX "Warning: couldn't load driver '%s': %s\n",
259 static void load_drivers(void)
261 struct ibv_driver_name *name, *next_name;
263 char *list, *env_name;
266 * Only use drivers passed in through the calling user's
267 * environment if we're not running setuid.
269 if (getuid() == geteuid()) {
270 if ((env = getenv("RDMAV_DRIVERS"))) {
272 while ((env_name = strsep(&list, ":;")))
273 load_driver(env_name);
274 } else if ((env = getenv("IBV_DRIVERS"))) {
276 while ((env_name = strsep(&list, ":;")))
277 load_driver(env_name);
281 for (name = driver_name_list, next_name = name ? name->next : NULL;
283 name = next_name, next_name = name ? name->next : NULL) {
284 load_driver(name->name);
290 static void read_config_file(const char *path)
299 conf = fopen(path, "r");
301 fprintf(stderr, PFX "Warning: couldn't read config file %s.\n",
306 while ((len = getline(&line, &buflen, conf)) != -1) {
307 config = line + strspn(line, "\t ");
308 if (config[0] == '\n' || config[0] == '#')
311 field = strsep(&config, "\n\t ");
313 if (strcmp(field, "driver") == 0) {
314 struct ibv_driver_name *driver_name;
316 config += strspn(config, "\t ");
317 field = strsep(&config, "\n\t ");
319 driver_name = malloc(sizeof *driver_name);
321 fprintf(stderr, PFX "Warning: couldn't allocate "
322 "driver name '%s'.\n", field);
326 driver_name->name = strdup(field);
327 if (!driver_name->name) {
328 fprintf(stderr, PFX "Warning: couldn't allocate "
329 "driver name '%s'.\n", field);
334 driver_name->next = driver_name_list;
335 driver_name_list = driver_name;
337 fprintf(stderr, PFX "Warning: ignoring bad config directive "
338 "'%s' in file '%s'.\n", field, path);
346 static void read_config(void)
352 conf_dir = opendir(IBV_CONFIG_DIR);
354 fprintf(stderr, PFX "Warning: couldn't open config directory '%s'.\n",
359 while ((dent = readdir(conf_dir))) {
362 if (asprintf(&path, "%s/%s", IBV_CONFIG_DIR, dent->d_name) < 0) {
363 fprintf(stderr, PFX "Warning: couldn't read config file %s/%s.\n",
364 IBV_CONFIG_DIR, dent->d_name);
368 if (stat(path, &buf)) {
369 fprintf(stderr, PFX "Warning: couldn't stat config file '%s'.\n",
374 if (!S_ISREG(buf.st_mode))
377 read_config_file(path);
385 static struct ibv_device *try_driver(struct ibv_driver *driver,
386 struct ibv_sysfs_dev *sysfs_dev)
388 struct ibv_device *dev;
391 dev = driver->init_func(sysfs_dev->sysfs_path, sysfs_dev->abi_ver);
395 if (ibv_read_sysfs_file(sysfs_dev->ibdev_path, "node_type", value, sizeof value) < 0) {
396 fprintf(stderr, PFX "Warning: no node_type attr under %s.\n",
397 sysfs_dev->ibdev_path);
398 dev->node_type = IBV_NODE_UNKNOWN;
400 dev->node_type = strtol(value, NULL, 10);
401 if (dev->node_type < IBV_NODE_CA || dev->node_type > IBV_NODE_RNIC)
402 dev->node_type = IBV_NODE_UNKNOWN;
406 switch (dev->node_type) {
408 case IBV_NODE_SWITCH:
409 case IBV_NODE_ROUTER:
410 dev->transport_type = IBV_TRANSPORT_IB;
413 dev->transport_type = IBV_TRANSPORT_IWARP;
416 dev->transport_type = IBV_TRANSPORT_UNKNOWN;
420 strcpy(dev->dev_name, sysfs_dev->sysfs_name);
421 strcpy(dev->dev_path, sysfs_dev->sysfs_path);
422 strcpy(dev->name, sysfs_dev->ibdev_name);
423 strcpy(dev->ibdev_path, sysfs_dev->ibdev_path);
428 static struct ibv_device *try_drivers(struct ibv_sysfs_dev *sysfs_dev)
430 struct ibv_driver *driver;
431 struct ibv_device *dev;
433 for (driver = head_driver; driver; driver = driver->next) {
434 dev = try_driver(driver, sysfs_dev);
442 static int check_abi_version(const char *path)
446 if (ibv_read_sysfs_file(path, "class/infiniband_verbs/abi_version",
447 value, sizeof value) < 0) {
451 abi_ver = strtol(value, NULL, 10);
453 if (abi_ver < IB_USER_VERBS_MIN_ABI_VERSION ||
454 abi_ver > IB_USER_VERBS_MAX_ABI_VERSION) {
455 fprintf(stderr, PFX "Fatal: kernel ABI version %d "
456 "doesn't match library version %d.\n",
457 abi_ver, IB_USER_VERBS_MAX_ABI_VERSION);
464 static void check_memlock_limit(void)
471 if (getrlimit(RLIMIT_MEMLOCK, &rlim)) {
472 fprintf(stderr, PFX "Warning: getrlimit(RLIMIT_MEMLOCK) failed.");
476 if (rlim.rlim_cur <= 32768)
477 fprintf(stderr, PFX "Warning: RLIMIT_MEMLOCK is %lu bytes.\n"
478 " This will severely limit memory registrations.\n",
482 static void add_device(struct ibv_device *dev,
483 struct ibv_device ***dev_list,
487 struct ibv_device **new_list;
489 if (*list_size <= *num_devices) {
490 *list_size = *list_size ? *list_size * 2 : 1;
491 new_list = realloc(*dev_list, *list_size * sizeof (struct ibv_device *));
494 *dev_list = new_list;
497 (*dev_list)[(*num_devices)++] = dev;
500 HIDDEN int ibverbs_init(struct ibv_device ***list)
502 const char *sysfs_path;
503 struct ibv_sysfs_dev *sysfs_dev, *next_dev;
504 struct ibv_device *device;
507 int statically_linked = 0;
513 if (getenv("RDMAV_FORK_SAFE") || getenv("IBV_FORK_SAFE"))
515 fprintf(stderr, PFX "Warning: fork()-safety requested "
516 "but init failed\n");
518 sysfs_path = ibv_get_sysfs_path();
522 ret = check_abi_version(sysfs_path);
526 check_memlock_limit();
530 ret = find_sysfs_devs();
534 for (sysfs_dev = sysfs_dev_list; sysfs_dev; sysfs_dev = sysfs_dev->next) {
535 device = try_drivers(sysfs_dev);
537 add_device(device, list, &num_devices, &list_size);
538 sysfs_dev->have_driver = 1;
547 * Check if we can dlopen() ourselves. If this fails,
548 * libibverbs is probably statically linked into the
549 * executable, and we should just give up, since trying to
550 * dlopen() a driver module will fail spectacularly (loading a
551 * driver .so will bring in dynamic copies of libibverbs and
552 * libdl to go along with the static copies the executable
553 * has, which quickly leads to a crash.
556 void *hand = dlopen(NULL, RTLD_NOW);
558 fprintf(stderr, PFX "Warning: dlopen(NULL) failed, "
559 "assuming static linking.\n");
560 statically_linked = 1;
568 for (sysfs_dev = sysfs_dev_list; sysfs_dev; sysfs_dev = sysfs_dev->next) {
569 if (sysfs_dev->have_driver)
572 device = try_drivers(sysfs_dev);
574 add_device(device, list, &num_devices, &list_size);
575 sysfs_dev->have_driver = 1;
580 for (sysfs_dev = sysfs_dev_list,
581 next_dev = sysfs_dev ? sysfs_dev->next : NULL;
583 sysfs_dev = next_dev, next_dev = sysfs_dev ? sysfs_dev->next : NULL) {
584 if (!sysfs_dev->have_driver) {
585 fprintf(stderr, PFX "Warning: no userspace device-specific "
586 "driver found for %s\n", sysfs_dev->sysfs_path);
587 if (statically_linked)
588 fprintf(stderr, " When linking libibverbs statically, "
589 "driver must be statically linked too.\n");