大量练习:

#include <rte_mbuf.h>
#include <rte_eal.h>
#include <rte_ethdev.h>
#include <stdio.h>

#define MBUF_POOL_NAME "MBUF_POOL"
#define NUM_MBUFS 8192

int main(int argc, char **argv) {
    if (rte_eal_init(argc, argv) < 0)
        rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");

    struct rte_mempool *mbuf_pool = rte_pktmbuf_pool_create(MBUF_POOL_NAME, NUM_MBUFS,
                                                            250, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
                                                            rte_socket_id());
    if (mbuf_pool == NULL)
        rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");

    struct rte_mbuf *mbuf = rte_pktmbuf_alloc(mbuf_pool);
    if (mbuf == NULL) {
        printf("Failed to allocate mbuf\n");
        return -1;
    }

    // 填充数据包内容
    char *data = rte_pktmbuf_mtod(mbuf, char *);
    snprintf(data, rte_pktmbuf_tailroom(mbuf), "Hello, DPDK!");

    // 设置数据包长度
    rte_pktmbuf_pkt_len(mbuf) = strlen(data);
    rte_pktmbuf_data_len(mbuf) = strlen(data);

    // 发送数据包(假设已经配置好端口和队列)
    uint16_t port_id = 0;
    uint16_t nb_tx = rte_eth_tx_burst(port_id, 0, &mbuf, 1);
    if (nb_tx < 1) {
        rte_pktmbuf_free(mbuf);
        printf("Failed to send packet\n");
    }

    return 0;
}


putao
8 声望1 粉丝

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