• RDMA技术详解(三):理解RDMA Scatter Gather List


    https://blog.csdn.net/qq_21125183/article/details/86527199

    1. 前言

    在使用RDMA操作之前,我们需要了解一些RDMA API中的一些需要的值。其中在ibv_send_wr我们需要一个sg_list的数组,sg_list是用来存放ibv_sge元素,那么什么是SGL以及什么是sge呢?对于一个使用RDMA进行开发的程序员来说,我们需要了解这一系列细节。
    2. SGE简介

    在NVMe over PCIe中,I/O命令支持SGL(Scatter Gather List 分散聚合表)和PRP(Physical Region Page 物理(内存)区域页), 而管理命令只支持PRP;而在NVMe over Fabrics中,无论是管理命令还是I/O命令都只支持SGL。

    RDMA编程中,SGL(Scatter/Gather List)是最基本的数据组织形式。 SGL是一个数组,该数组中的元素被称之为SGE(Scatter/Gather Element),每一个SGE就是一个Data Segment(数据段)。RDMA支持Scatter/Gather操作,具体来讲就是RDMA可以支持一个连续的Buffer空间,进行Scatter分散到多个目的主机的不连续的Buffer空间。Gather指的就是多个不连续的Buffer空间,可以Gather到目的主机的一段连续的Buffer空间。

    下面我们就来看一下ibv_sge的定义:

    struct ibv_sge {
            uint64_t        addr;
            uint32_t        length;
            uint32_t        lkey;
    };

        1
        2
        3
        4
        5

            addr: 数据段所在的虚拟内存的起始地址 (Virtual Address of the Data Segment (i.e. Buffer))
            length: 数据段长度(Length of the Data Segment)
            lkey: 该数据段对应的L_Key (Key of the local Memory Region)

    2. ivc_post_send接口

    而在数据传输中,发送/接收使用的Verbs API为:

            ibv_post_send() - post a list of work requests (WRs) to a send queue 将一个WR列表放置到发送队列中
            ibv_post_recv() - post a list of work requests (WRs) to a receive queue 将一个WR列表放置到接收队列中

    下面以ibv_post_send()为例,说明SGL是如何被放置到RDMA硬件的线缆(Wire)上的。

    ibv_post_send()的函数原型

    #include <infiniband/verbs.h>

    int ibv_post_send(struct ibv_qp *qp,
                      struct ibv_send_wr *wr,
                      struct ibv_send_wr **bad_wr);

        1
        2
        3
        4
        5

    ibv_post_send()将以send_wr开头的工作请求(WR)的列表发布到Queue Pair的Send Queue。 它会在第一次失败时停止处理此列表中的WR(可以在发布请求时立即检测到),并通过bad_wr返回此失败的WR。

    参数wr是一个ibv_send_wr结构,如<infiniband / verbs.h>中所定义。
    3. ibv_send_wr结构

    struct ibv_send_wr {
            uint64_t                wr_id;                  /* User defined WR ID */
            struct ibv_send_wr     *next;                   /* Pointer to next WR in list, NULL if last WR */
            struct ibv_sge         *sg_list;                /* Pointer to the s/g array */
            int                     num_sge;                /* Size of the s/g array */
            enum ibv_wr_opcode      opcode;                 /* Operation type */
            int                     send_flags;             /* Flags of the WR properties */
            uint32_t                imm_data;               /* Immediate data (in network byte order) */
            union {
                    struct {
                            uint64_t        remote_addr;    /* Start address of remote memory buffer */
                            uint32_t        rkey;           /* Key of the remote Memory Region */
                    } rdma;
                    struct {
                            uint64_t        remote_addr;    /* Start address of remote memory buffer */
                            uint64_t        compare_add;    /* Compare operand */
                            uint64_t        swap;           /* Swap operand */
                            uint32_t        rkey;           /* Key of the remote Memory Region */
                    } atomic;
                    struct {
                            struct ibv_ah  *ah;             /* Address handle (AH) for the remote node address */
                            uint32_t        remote_qpn;     /* QP number of the destination QP */
                            uint32_t        remote_qkey;    /* Q_Key number of the destination QP */
                    } ud;
            } wr;
    };

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26

    在调用ibv_post_send()之前,必须填充好数据结构wr。 wr是一个链表,每一个结点包含了一个sg_list(i.e. SGL: 由一个或多个SGE构成的数组), sg_list的长度为num_sge。
    4. RDMA 提交WR流程

    下面图解一下SGL和WR链表的对应关系,并说明一个SGL (struct ibv_sge *sg_list)里包含的多个数据段是如何被RDMA硬件聚合成一个连续的数据段的。
    4.1 第一步:创建SGL

     
    从上图中,我们可以看到wr链表中的每一个结点都包含了一个SGL,SGL是一个数组,包含一个或多个SGE。通过ibv_post_send提交一个RDMA SEND 请求。这个WR请求中,包括一个sg_list的元素。它是一个SGE链表,SGE指向具体需要发送数据的Buffer。
    4.2 第二步:使用PD进行内存保护

     
    我们在发送一段内存地址的时候,我们需要将这段内存地址通过Memory Registration注册到RDMA中。也就是说注册到PD内存保护域当中。一个SGL至少被一个MR保护, 多个MR存在同一个PD中。如图所示一段内存MR可以保护多个SGE元素。
    4.3 调用ibv_post_send()将SGL发送到wire上去

     
    在上图中,一个SGL数组包含了3个SGE, 长度分别为N1, N2, N3字节。我们可以看到,这3个buffer并不连续,它们Scatter(分散)在内存中的各个地方。RDMA硬件读取到SGL后,进行Gather(聚合)操作,于是在RDMA硬件的Wire上看到的就是N3+N2+N1个连续的字节。换句话说,通过使用SGL, 我们可以把分散(Scatter)在内存中的多个数据段(不连续)交给RDMA硬件去聚合(Gather)成连续的数据段。
    附录一: OFED Verbs

  • 相关阅读:
    403
    311
    401
    310
    308
    309
    307
    304
    3-1
    2-11
  • 原文地址:https://www.cnblogs.com/ztguang/p/14770898.html
Copyright © 2020-2023  润新知