DPDK-實戰之skeleton(basicfwd)

原文鏈接:https://blog.csdn.net/pangyemeng/article/details/78226434

0x01 緣由

     繼續學習DPDK示例,主要熟悉每個環節和設計理念,今天要學習的是一個相對簡答的服務,僅僅從一個網口抓取數據包轉發到另外一個網口,這樣做雙向轉發,相當於橋的功能。其他基礎業務都不做。

0x02 直接上源碼分析

#include <stdint.h>
#include <inttypes.h>
#include <rte_eal.h>
#include <rte_ethdev.h>
#include <rte_cycles.h>
#include <rte_lcore.h>
#include <rte_mbuf.h>
 
#define RX_RING_SIZE 128
#define TX_RING_SIZE 512
 
#define NUM_MBUFS 8191
#define MBUF_CACHE_SIZE 250
#define BURST_SIZE 32
 
static const struct rte_eth_conf port_conf_default = {
    .rxmode = { .max_rx_pkt_len = ETHER_MAX_LEN }
};
 
/* basicfwd.c: Basic DPDK skeleton forwarding example. */
 
/*
 * 用全局默認的配置來初始化網口。在內存池中分配接收隊列和發送隊列。
 */
static inline int
port_init(uint8_t port, struct rte_mempool *mbuf_pool)
{
    struct rte_eth_conf port_conf = port_conf_default; //全部配置模式
    const uint16_t rx_rings = 1, tx_rings = 1;   //每個網口隊列多少,這裏都爲1
    int retval;
    uint16_t q;
 
    if (port >= rte_eth_dev_count())  //檢查網口編號
        return -1;
 
    /* 配置以太網設備 */
    retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
    if (retval != 0)
        return retval;
 
    /* 爲每個以太網網口分配和設置1個接收隊列 */
    for (q = 0; q < rx_rings; q++) {
        retval = rte_eth_rx_queue_setup(port, q, RX_RING_SIZE,
                rte_eth_dev_socket_id(port), NULL, mbuf_pool);
        if (retval < 0)
            return retval;
    }
 
    /* 爲每個以太網網口分配和設置1個發送隊列 */
    for (q = 0; q < tx_rings; q++) {
        retval = rte_eth_tx_queue_setup(port, q, TX_RING_SIZE,
                rte_eth_dev_socket_id(port), NULL);
        if (retval < 0)
            return retval;
    }
 
    /* 開啓以太網網口 */
    retval = rte_eth_dev_start(port);
    if (retval < 0)
        return retval;
 
    /* 打印網卡地址 MAC address. */
    struct ether_addr addr;
    rte_eth_macaddr_get(port, &addr);
    printf("Port %u MAC: %02" PRIx8 " %02" PRIx8 " %02" PRIx8
               " %02" PRIx8 " %02" PRIx8 " %02" PRIx8 "\n",
            (unsigned)port,
            addr.addr_bytes[0], addr.addr_bytes[1],
            addr.addr_bytes[2], addr.addr_bytes[3],
            addr.addr_bytes[4], addr.addr_bytes[5]);
 
    /* 網口開啓混雜模式 */
    rte_eth_promiscuous_enable(port);
 
    return 0;
}
 
/*
 * 一個線程處理一對網口,數據包一進一出。
 */
static __attribute__((noreturn)) void
lcore_main(void)
{
    const uint8_t nb_ports = rte_eth_dev_count(); //網口數
    uint8_t port;
 
    /*
     * 當有NUMA結構時,檢查網口是否在同一個NUMA node節點上,只有在一個NUMA node上時輪詢線程效率最好。
     */
    for (port = 0; port < nb_ports; port++)
        if (rte_eth_dev_socket_id(port) > 0 &&
                rte_eth_dev_socket_id(port) !=
                        (int)rte_socket_id())
            printf("WARNING, port %u is on remote NUMA node to "
                    "polling thread.\n\tPerformance will "
                    "not be optimal.\n", port);
 
    printf("\nCore %u forwarding packets. [Ctrl+C to quit]\n",
            rte_lcore_id());
 
    /* 死循環處理數據包的搬運 */
    for (;;) {
        /*
         * 一個端口接收包,然後直接轉發到這對數據包上。
         * 如 0 -> 1, 1 -> 0, 2 -> 3, 3 -> 2 等。
         */
        for (port = 0; port < nb_ports; port++) {
 
            /* 從一個端口接收數據包 */
            struct rte_mbuf *bufs[BURST_SIZE];
            const uint16_t nb_rx = rte_eth_rx_burst(port, 0,
                    bufs, BURST_SIZE);
 
            if (unlikely(nb_rx == 0))
                continue;
 
            /* Send burst of TX packets, to second port of pair. */
            const uint16_t nb_tx = rte_eth_tx_burst(port ^ 1, 0,
                    bufs, nb_rx);
 
            /* 釋放沒有發送的數據包。 */
            if (unlikely(nb_tx < nb_rx)) {
                uint16_t buf;
                for (buf = nb_tx; buf < nb_rx; buf++)
                    rte_pktmbuf_free(bufs[buf]);
            }
        }
    }
}
 
/*
 * The main function, which does initialization and calls the per-lcore
 * functions.
 */
int
main(int argc, char *argv[])
{
    struct rte_mempool *mbuf_pool; //使用內存池空間來容納ring隊列,接收和發送數據包
    unsigned nb_ports;             //網卡數
    uint8_t portid;                //網卡編號
 
    /* Initialize the Environment Abstraction Layer (EAL). */
    int ret = rte_eal_init(argc, argv);
    if (ret < 0)
        rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
 
    argc -= ret;
    argv += ret;
 
    /* 檢查網卡是否爲偶數 send/receive */
    nb_ports = rte_eth_dev_count();
    if (nb_ports < 2 || (nb_ports & 1))
        rte_exit(EXIT_FAILURE, "Error: number of ports must be even\n");
 
    /*  創建一個內存池來容納mbufs。大小按網口多少來分配。相當於每個網口都要有一個接受和發送隊列。*/
    mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS * nb_ports,
        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");
 
    /* 初始化所有網口配置 */
    for (portid = 0; portid < nb_ports; portid++)
        if (port_init(portid, mbuf_pool) != 0)
            rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu8 "\n",
                    portid);
 
    if (rte_lcore_count() > 1)
        printf("\nWARNING: Too many lcores enabled. Only 1 used.\n");
 
    /* 僅僅用一個主線程調用,相當與一個線程處理一對網口的數據包。*/
    lcore_main();
 
    return 0;
}

0x03 運行實例

[root@Huawei x86_64-native-linuxapp-gcc]# ./basicfwd -l 1 -n 4
EAL: Detected lcore 0 as core 0 on socket 0
EAL: Detected lcore 1 as core 1 on socket 0
EAL: Detected lcore 2 as core 2 on socket 0
EAL: Detected lcore 3 as core 3 on socket 0
EAL: Detected lcore 4 as core 4 on socket 0
EAL: Detected lcore 5 as core 5 on socket 0
EAL: Detected lcore 6 as core 6 on socket 0
EAL: Detected lcore 7 as core 7 on socket 0
EAL: Detected lcore 8 as core 0 on socket 1
EAL: Detected lcore 9 as core 1 on socket 1
EAL: Detected lcore 10 as core 2 on socket 1
EAL: Detected lcore 11 as core 3 on socket 1
EAL: Detected lcore 12 as core 4 on socket 1
EAL: Detected lcore 13 as core 5 on socket 1
EAL: Detected lcore 14 as core 6 on socket 1
EAL: Detected lcore 15 as core 7 on socket 1
EAL: Detected lcore 16 as core 0 on socket 0
EAL: Detected lcore 17 as core 1 on socket 0
EAL: Detected lcore 18 as core 2 on socket 0
EAL: Detected lcore 19 as core 3 on socket 0
EAL: Detected lcore 20 as core 4 on socket 0
EAL: Detected lcore 21 as core 5 on socket 0
EAL: Detected lcore 22 as core 6 on socket 0
EAL: Detected lcore 23 as core 7 on socket 0
EAL: Detected lcore 24 as core 0 on socket 1
EAL: Detected lcore 25 as core 1 on socket 1
EAL: Detected lcore 26 as core 2 on socket 1
EAL: Detected lcore 27 as core 3 on socket 1
EAL: Detected lcore 28 as core 4 on socket 1
EAL: Detected lcore 29 as core 5 on socket 1
EAL: Detected lcore 30 as core 6 on socket 1
EAL: Detected lcore 31 as core 7 on socket 1
EAL: Support maximum 128 logical core(s) by configuration.
EAL: Detected 32 lcore(s)
EAL: Setting up physically contiguous memory...
EAL: Ask a virtual area of 0x200000 bytes
EAL: Virtual area found at 0x7f45f8000000 (size = 0x200000)
EAL: Ask a virtual area of 0x7c00000 bytes
EAL: Virtual area found at 0x7f45f0200000 (size = 0x7c00000)
EAL: Ask a virtual area of 0x200000 bytes
EAL: Virtual area found at 0x7f45efe00000 (size = 0x200000)
EAL: Ask a virtual area of 0x7c00000 bytes
EAL: Virtual area found at 0x7f45e8000000 (size = 0x7c00000)
EAL: Ask a virtual area of 0x400000 bytes
EAL: Virtual area found at 0x7f45e7a00000 (size = 0x400000)
EAL: Requesting 64 pages of size 2MB from socket 0
EAL: Requesting 64 pages of size 2MB from socket 1
EAL: TSC frequency is ~2593994 KHz
EAL: Master lcore 1 is ready (tid=f83f2880;cpuset=[1])
EAL: PCI device 0000:04:00.0 on NUMA socket 0
EAL:   probe driver: 8086:1521 rte_igb_pmd
EAL:   Not managed by a supported kernel driver, skipped
EAL: PCI device 0000:04:00.1 on NUMA socket 0
EAL:   probe driver: 8086:1521 rte_igb_pmd
EAL:   Not managed by a supported kernel driver, skipped
EAL: PCI device 0000:04:00.2 on NUMA socket 0
EAL:   probe driver: 8086:1521 rte_igb_pmd
EAL:   PCI memory mapped at 0x7f45f7f00000
EAL:   PCI memory mapped at 0x7f45f7efc000
PMD: eth_igb_dev_init(): port_id 0 vendorID=0x8086 deviceID=0x1521
EAL: PCI device 0000:04:00.3 on NUMA socket 0
EAL:   probe driver: 8086:1521 rte_igb_pmd
EAL:   PCI memory mapped at 0x7f45f0100000
EAL:   PCI memory mapped at 0x7f45f8408000
PMD: eth_igb_dev_init(): port_id 1 vendorID=0x8086 deviceID=0x1521
EAL: PCI device 0000:05:00.0 on NUMA socket 0
EAL:   probe driver: 8086:1522 rte_igb_pmd
EAL:   Not managed by a supported kernel driver, skipped
EAL: PCI device 0000:05:00.1 on NUMA socket 0
EAL:   probe driver: 8086:1522 rte_igb_pmd
EAL:   Not managed by a supported kernel driver, skipped
EAL: PCI device 0000:05:00.2 on NUMA socket 0
EAL:   probe driver: 8086:1522 rte_igb_pmd
EAL:   Not managed by a supported kernel driver, skipped
EAL: PCI device 0000:05:00.3 on NUMA socket 0
EAL:   probe driver: 8086:1522 rte_igb_pmd
EAL:   Not managed by a supported kernel driver, skipped
PMD: eth_igb_rx_queue_setup(): sw_ring=0x7f45eff90fc0 hw_ring=0x7f45eff91400 dma_addr=0x67391400
PMD: eth_igb_tx_queue_setup(): sw_ring=0x7f45eff7ee40 hw_ring=0x7f45eff80e80 dma_addr=0x67380e80
PMD: eth_igb_start(): <<
Port 0 MAC: a0 36 9f 03 a8 ca
PMD: eth_igb_rx_queue_setup(): sw_ring=0x7f45eff6e7c0 hw_ring=0x7f45eff6ec00 dma_addr=0x6736ec00
PMD: eth_igb_tx_queue_setup(): sw_ring=0x7f45eff5c640 hw_ring=0x7f45eff5e680 dma_addr=0x6735e680
PMD: eth_igb_start(): <<
Port 1 MAC: a0 36 9f 03 a8 cb
 
Core 1 forwarding packets. [Ctrl+C to quit]

注:這篇文章轉載,我在筆記本電腦上用虛擬機會導致卡死,並報錯:例如kernel:NMI watchdog: BUG: soft lockup - CPU#0 stuck for 30s,根據查找結果是筆記本電腦的性能之類的問題。

0x04 總結

     思路比較簡單,一個網口用兩個緩存來緩存數據包,然後將數據包轉發出去。此處用到了內存池結構。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章