ibv_open_device()函数

struct ibv_context *ibv_open_device(struct ibv_device *device);

描述

函数会创建一个RDMA设备相关的context;可以通过ibv_close_device()函数来关闭设备。

context的作用

  • 查询RDMA设备的资源
  • 创建资源

注意:

  • 函数的功能与函数名所代表的意思不同,它事实上并不是打开设备;
  • 设备实际是被内核底层的驱动打开;
  • 设备可能被其他的用户/内核层的代码所使用;
  • 这个verb仅仅打开一个context,供用户层应用来使用它;

 

参数(struct ibv_device *device)

参数为函数ibv_get_device_list()返回RDMA可用RDMA设备数组的某一项。

 

返回值(struct ibv_context *)

struct ibv_context包含下面区域

  • async_fd:async_fd是一个文件描述符,为了用来读取异步事件。如果他想在一个non-blocking模式下读取一个异步事件,可以使用这个描述符
  • num_comp_vectors:可用于此RDMA设备的完成向量(completion vectors)的数量。

 

例子

#include <stdio.h>
#include <infiniband/verbs.h>

int main(void)
{
    struct ibv_device **device_list;
    int num_devices;
    int i;
    int rc;

    device_list = ibv_get_device_list(&num_devices);
    if (!device_list) {
        fprintf(stderr, "Error, ibv_get_device_list() failed\n");
        return -1;
    }

    printf("%d RDMA device(s) found:\n\n", num_devices);

    for (i = 0; i < num_devices; ++ i) {
        struct ibv_context *ctx;

        ctx = ibv_open_device(device_list[i]);
        if (!ctx) {
            fprintf(stderr, "Error, failed to open the device '%s'\n",
                ibv_get_device_name(device_list[i]));
            rc = -1;
            goto out;
        }

        printf("The device '%s' was opened\n", ibv_get_device_name(ctx->device));

        rc = ibv_close_device(ctx);
        if (rc) {
            fprintf(stderr, "Error, failed to close the device '%s'\n",
                ibv_get_device_name(ctx->device));
            rc = -1;
            goto out;
        }
    }
        
    ibv_free_device_list(device_list);

    return 0;

out:
    ibv_free_device_list(device_list);
    return rc;
}

 

posted @ 2017-12-13 18:20  huststephen  阅读(1300)  评论(0编辑  收藏  举报