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;
}

 

转载于:https://www.cnblogs.com/coderex2522/p/8034317.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值