DPDK RX/TX Callbacksソース読み

4862 ワード

このsampleはbasicfwに基づいています.basicfwは1つのポートで受け取ったパケットがすぐに別のポートから転送され、非常に簡潔で明瞭なプログラムであり、basicfw学習ベースのDPDKを通じてAPIを委託することができる.RX/TX Callbacksは、受信および送信されたパケット上でユーザ定義のコールバック関数を使用することを示す.
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2010-2015 Intel Corporation
 */

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define RX_RING_SIZE 1024
#define TX_RING_SIZE 1024

#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,
        .ignore_offload_bitfield = 1,
    },
};

static struct {
    uint64_t total_cycles;
    uint64_t total_pkts;
} latency_numbers;

/*               :
 、rx
typedef uint16_t(* rte_rx_callback_fn)(uint16_t port_id, uint16_t queue, struct rte_mbuf *pkts[], uint16_t nb_pkts, uint16_t max_pkts, void *user_param)
      :
    1. port id 
    2. queue index
    3.     burst pkt      rte_mbuf    
    4. burst        
    5.      pkts          。
    6.                     
    
 、tx
typedef uint16_t(* rte_tx_callback_fn)(uint16_t port_id, uint16_t queue, struct rte_mbuf *pkts[], uint16_t nb_pkts, void *user_param)
      :
    1. port id 
    2. queue index 
    3. rte_mbuf   ,   pkt     
    4. burst        
    5.                     
*/

// rx        ,          
static uint16_t
add_timestamps(uint16_t port __rte_unused, uint16_t qidx __rte_unused,
        struct rte_mbuf **pkts, uint16_t nb_pkts,
        uint16_t max_pkts __rte_unused, void *_ __rte_unused)
{
    unsigned i;
    uint64_t now = rte_rdtsc(); //           。

    for (i = 0; i < nb_pkts; i++)
        pkts[i]->udata64 = now; // 8      
    return nb_pkts;
}

// tx        ,       
static uint16_t
calc_latency(uint16_t port __rte_unused, uint16_t qidx __rte_unused,
        struct rte_mbuf **pkts, uint16_t nb_pkts, void *_ __rte_unused)
{
    uint64_t cycles = 0;
    uint64_t now = rte_rdtsc();
    unsigned i;

    for (i = 0; i < nb_pkts; i++)
        cycles += now - pkts[i]->udata64; //  main.c    struct             
    latency_numbers.total_cycles += cycles;
    latency_numbers.total_pkts += nb_pkts;

    if (latency_numbers.total_pkts > (100 * 1000 * 1000ULL)) { //            
        printf("Latency = %"PRIu64" cycles
", latency_numbers.total_cycles / latency_numbers.total_pkts); latency_numbers.total_cycles = latency_numbers.total_pkts = 0; } return nb_pkts; } /* * Initialises a given port using global settings and with the rx buffers * coming from the mbuf_pool passed as parameter */ static inline int port_init(uint16_t port, struct rte_mempool *mbuf_pool) { /*......*/ /* basicfw */ /* rte_eth_add_rx_callback() API , port id rx queue index pkt(burst) 。 : 1. port id 2. queue index 3. 4. , API rte_eth_add_tx_callback() tx queue 。 */ // typedef rte_eth_add_rx_callback(port, 0, add_timestamps, NULL); rte_eth_add_tx_callback(port, 0, calc_latency, NULL); return 0; } /* * Main thread that does the work, reading from INPUT_PORT * and writing to OUTPUT_PORT */ // lcore_main basicfw 。 static __attribute__((noreturn)) void lcore_main(void) { /*......*/ } /* Main function, does initialisation and calls the per-lcore functions */ // main basicfw 。 int main(int argc, char *argv[]) { /*......*/ }

実行状況
開いてからしばらく運転して、出力をもらいました.外部の発注器を開けたほうがいいです.
root@ubuntu:/home/chang/dpdk/examples/rxtx_callbacks/build# ./rxtx_callbacks -l 1 -n 4 
EAL: Detected 8 lcore(s)
EAL: No free hugepages reported in hugepages-1048576kB
EAL: Multi-process socket /var/run/.rte_unix
EAL: Probing VFIO support...
EAL: PCI device 0000:02:01.0 on NUMA socket -1
EAL:   Invalid NUMA socket, default to 0
EAL:   probe driver: 8086:100f net_e1000_em
EAL: PCI device 0000:02:02.0 on NUMA socket -1
EAL:   Invalid NUMA socket, default to 0
EAL:   probe driver: 8086:100f net_e1000_em
EAL: PCI device 0000:02:03.0 on NUMA socket -1
EAL:   Invalid NUMA socket, default to 0
EAL:   probe driver: 8086:100f net_e1000_em
EAL: PCI device 0000:02:04.0 on NUMA socket -1
EAL:   Invalid NUMA socket, default to 0
EAL:   probe driver: 8086:100f net_e1000_em
Port 0 MAC: 00 0c 29 f7 4d 25
Port 1 MAC: 00 0c 29 f7 4d 2f

Core 1 forwarding packets. [Ctrl+C to quit]
Latency = 629 cycles
Latency = 787 cycles
^C

リファレンス
sample guide:https://doc.dpdk.org/guides/sample_app_ug/rxtx_callbacks.html API:ethdev, mbuf