获取每个 DMA 设备的名称

// 获取 DMA 设备数量
    int dma_count = rte_dma_count();
    if (dma_count <= 0) {
        printf("No DMA devices found\n");
    } else {
        printf("Found %d DMA device(s)\n", dma_count);
        // 获取每个 DMA 设备的名称
        for (int i = 0; i < dma_count; i++) {
            const char *dma_name = rte_dma_get_name_by_index(i);
            if (dma_name) {
                printf("DMA device %d: %s\n", i, dma_name);
            } else {
                printf("Failed to get name for DMA device %d\n", i);
            }
        }
    }

配置 DMA 设备

static int configure_dma_device(void) {
    uint16_t dma_dev_id;
    struct rte_dma_conf dma_conf = {
        .nb_vchans = 1,
        .socket_id = rte_socket_id(),
    };

    dma_dev_id = rte_dma_get_dev_id("dma0");
    if (dma_dev_id == -1)
        return -1;

    if (rte_dma_configure(dma_dev_id, &dma_conf) < 0)
        return -1;

    if (rte_dma_start(dma_dev_id) < 0)
        return -1;

    return dma_dev_id;
}

执行 DMA 传输

static int perform_dma_transfer(uint16_t dma_dev_id, struct rte_mbuf *src_mbuf, struct rte_mbuf *dst_mbuf) {
    struct rte_dma_xfer_desc xfer_desc = {
        .src = rte_pktmbuf_mtod(src_mbuf, void *),
        .dst = rte_pktmbuf_mtod(dst_mbuf, void *),
        .len = src_mbuf->data_len,
    };

    if (rte_dma_submit(dma_dev_id, &xfer_desc, 1) < 0)
        return -1;

    if (rte_dma_completed(dma_dev_id, 1, NULL) < 0)
        return -1;

    return 0;
}

main

int main(int argc, char **argv) {
    // 初始化 DPDK 环境
    int ret = rte_eal_init(argc, argv);
    if (ret < 0)
        rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");

    force_quit = false;
    // 注册信号处理函数
    signal(SIGINT, signal_handler);
    signal(SIGTERM, signal_handler);

    // 创建内存池,用于存放数据包
    mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS, MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
    if (mbuf_pool == NULL)
        rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");

    // 配置 DMA 设备
    int dma_dev_id = configure_dma_device();
    if (dma_dev_id < 0)
        rte_exit(EXIT_FAILURE, "Cannot configure DMA device\n");

    // 分配源和目标 mbuf
    struct rte_mbuf *src_mbuf = rte_pktmbuf_alloc(mbuf_pool);
    struct rte_mbuf *dst_mbuf = rte_pktmbuf_alloc(mbuf_pool);
    if (!src_mbuf || !dst_mbuf)
        rte_exit(EXIT_FAILURE, "Cannot allocate mbuf\n");

    // 填充源 mbuf 数据
    rte_memcpy(rte_pktmbuf_mtod(src_mbuf, void *), "Example data", 12);
    src_mbuf->data_len = 12;

    // 执行 DMA 传输
    if (perform_dma_transfer(dma_dev_id, src_mbuf, dst_mbuf) < 0)
        rte_exit(EXIT_FAILURE, "DMA transfer failed\n");

    // 验证传输结果
    if (memcmp(rte_pktmbuf_mtod(dst_mbuf, void *), "Example data", 12) != 0)
        rte_exit(EXIT_FAILURE, "Data mismatch after DMA transfer\n");

    printf("DMA transfer successful\n");

    // 释放 mbuf
    rte_pktmbuf_free(src_mbuf);
    rte_pktmbuf_free(dst_mbuf);

    // 停止并关闭 DMA 设备
    rte_dma_stop(dma_dev_id);
    rte_dma_close(dma_dev_id);
    // 释放内存池
    rte_mempool_free(mbuf_pool);

    return 0;
}

putao
8 声望1 粉丝

推动世界向前发展,改善民生。