This patch aims to:
 1. Add flexibility by supporting IP & UDP src/dst fields
 2. Improve multi-core performance by using per-core vars

Signed-off-by: Zhihong Wang <wangzhihong....@bytedance.com>
---
 app/test-pmd/flowgen.c | 137 +++++++++++++++++++++++++++++++------------------
 1 file changed, 86 insertions(+), 51 deletions(-)

diff --git a/app/test-pmd/flowgen.c b/app/test-pmd/flowgen.c
index 3bf6e1ce97..5b389165bc 100644
--- a/app/test-pmd/flowgen.c
+++ b/app/test-pmd/flowgen.c
@@ -40,41 +40,37 @@
 
 #include "testpmd.h"
 
-/* hardcoded configuration (for now) */
-static unsigned cfg_n_flows    = 1024;
-static uint32_t cfg_ip_src     = RTE_IPV4(10, 254, 0, 0);
-static uint32_t cfg_ip_dst     = RTE_IPV4(10, 253, 0, 0);
-static uint16_t cfg_udp_src    = 1000;
-static uint16_t cfg_udp_dst    = 1001;
+/*
+ * Hardcoded range for flow generation.
+ *
+ * Total number of flows =
+ *     cfg_n_ip_src * cfg_n_ip_dst * cfg_n_udp_src * cfg_n_udp_dst
+ */
+static uint32_t cfg_n_ip_src = 100;
+static uint32_t cfg_n_ip_dst = 100;
+static uint32_t cfg_n_udp_src = 10;
+static uint32_t cfg_n_udp_dst = 10;
+
+/* Base ip and port for flow generation. */
+static uint32_t cfg_ip_src_base = RTE_IPV4(10, 254, 0, 0);
+static uint32_t cfg_ip_dst_base = RTE_IPV4(10, 253, 0, 0);
+static uint16_t cfg_udp_src_base = 1000;
+static uint16_t cfg_udp_dst_base = 1001;
 static struct rte_ether_addr cfg_ether_src =
        {{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x00 }};
 static struct rte_ether_addr cfg_ether_dst =
        {{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x01 }};
 
+RTE_DEFINE_PER_LCORE(uint32_t, _next_ip_src);
+RTE_DEFINE_PER_LCORE(uint32_t, _next_ip_dst);
+RTE_DEFINE_PER_LCORE(uint32_t, _next_udp_src);
+RTE_DEFINE_PER_LCORE(uint32_t, _next_udp_dst);
+
 #define IP_DEFTTL  64   /* from RFC 1340. */
 
 /* Use this type to inform GCC that ip_sum violates aliasing rules. */
 typedef unaligned_uint16_t alias_int16_t __attribute__((__may_alias__));
 
-static inline uint16_t
-ip_sum(const alias_int16_t *hdr, int hdr_len)
-{
-       uint32_t sum = 0;
-
-       while (hdr_len > 1)
-       {
-               sum += *hdr++;
-               if (sum & 0x80000000)
-                       sum = (sum & 0xFFFF) + (sum >> 16);
-               hdr_len -= 2;
-       }
-
-       while (sum >> 16)
-               sum = (sum & 0xFFFF) + (sum >> 16);
-
-       return ~sum;
-}
-
 /*
  * Multi-flow generation mode.
  *
@@ -85,7 +81,7 @@ ip_sum(const alias_int16_t *hdr, int hdr_len)
 static void
 pkt_burst_flow_gen(struct fwd_stream *fs)
 {
-       unsigned pkt_size = tx_pkt_length - 4;  /* Adjust FCS */
+       uint32_t pkt_size = tx_pkt_length - 4; /* Adjust FCS */
        struct rte_mbuf  *pkts_burst[MAX_PKT_BURST];
        struct rte_mempool *mbp;
        struct rte_mbuf  *pkt = NULL;
@@ -102,15 +98,18 @@ pkt_burst_flow_gen(struct fwd_stream *fs)
        uint32_t retry;
        uint64_t tx_offloads;
        uint64_t start_tsc = 0;
-       static int next_flow = 0;
+       uint32_t next_ip_src = RTE_PER_LCORE(_next_ip_src);
+       uint32_t next_ip_dst = RTE_PER_LCORE(_next_ip_dst);
+       uint32_t next_udp_src = RTE_PER_LCORE(_next_udp_src);
+       uint32_t next_udp_dst = RTE_PER_LCORE(_next_udp_dst);
 
        get_start_cycles(&start_tsc);
 
        /* Receive a burst of packets and discard them. */
        nb_rx = rte_eth_rx_burst(fs->rx_port, fs->rx_queue, pkts_burst,
                                 nb_pkt_per_burst);
+       inc_rx_burst_stats(fs, nb_rx);
        fs->rx_packets += nb_rx;
-
        for (i = 0; i < nb_rx; i++)
                rte_pktmbuf_free(pkts_burst[i]);
 
@@ -144,7 +143,8 @@ pkt_burst_flow_gen(struct fwd_stream *fs)
                        eth_hdr = rte_pktmbuf_mtod(pkt, struct rte_ether_hdr *);
                        rte_ether_addr_copy(&cfg_ether_dst, &eth_hdr->d_addr);
                        rte_ether_addr_copy(&cfg_ether_src, &eth_hdr->s_addr);
-                       eth_hdr->ether_type = 
rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
+                       eth_hdr->ether_type =
+                               rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
 
                        /* Initialize IP header. */
                        ip_hdr = (struct rte_ipv4_hdr *)(eth_hdr + 1);
@@ -155,22 +155,30 @@ pkt_burst_flow_gen(struct fwd_stream *fs)
                        ip_hdr->time_to_live    = IP_DEFTTL;
                        ip_hdr->next_proto_id   = IPPROTO_UDP;
                        ip_hdr->packet_id       = 0;
-                       ip_hdr->src_addr        = rte_cpu_to_be_32(cfg_ip_src);
-                       ip_hdr->dst_addr        = rte_cpu_to_be_32(cfg_ip_dst +
-                                                                  next_flow);
-                       ip_hdr->total_length    = RTE_CPU_TO_BE_16(pkt_size -
-                                                                  
sizeof(*eth_hdr));
-                       ip_hdr->hdr_checksum    = ip_sum((const alias_int16_t 
*)ip_hdr,
-                                                        sizeof(*ip_hdr));
+                       ip_hdr->src_addr        =
+                               rte_cpu_to_be_32(cfg_ip_src_base
+                                               + next_ip_src);
+                       ip_hdr->dst_addr        =
+                               rte_cpu_to_be_32(cfg_ip_dst_base
+                                               + next_ip_dst);
+                       ip_hdr->total_length    =
+                               RTE_CPU_TO_BE_16(pkt_size
+                                               - sizeof(*eth_hdr));
+                       rte_ipv4_cksum(ip_hdr);
 
                        /* Initialize UDP header. */
                        udp_hdr = (struct rte_udp_hdr *)(ip_hdr + 1);
-                       udp_hdr->src_port       = rte_cpu_to_be_16(cfg_udp_src);
-                       udp_hdr->dst_port       = rte_cpu_to_be_16(cfg_udp_dst);
+                       udp_hdr->src_port       =
+                               rte_cpu_to_be_16(cfg_udp_src_base
+                                               + next_udp_src);
+                       udp_hdr->dst_port       =
+                               rte_cpu_to_be_16(cfg_udp_dst_base
+                                               + next_udp_dst);
+                       udp_hdr->dgram_len      =
+                               RTE_CPU_TO_BE_16(pkt_size
+                                               - sizeof(*eth_hdr)
+                                               - sizeof(*ip_hdr));
                        udp_hdr->dgram_cksum    = 0; /* No UDP checksum. */
-                       udp_hdr->dgram_len      = RTE_CPU_TO_BE_16(pkt_size -
-                                                                  
sizeof(*eth_hdr) -
-                                                                  
sizeof(*ip_hdr));
                        pkt->nb_segs            = 1;
                        pkt->pkt_len            = pkt_size;
                        pkt->ol_flags           &= EXT_ATTACHED_MBUF;
@@ -185,30 +193,57 @@ pkt_burst_flow_gen(struct fwd_stream *fs)
                }
                pkts_burst[nb_pkt] = pkt;
 
-               next_flow = (next_flow + 1) % cfg_n_flows;
+               if (++next_udp_dst < cfg_n_udp_dst)
+                       continue;
+               next_udp_dst = 0;
+               if (++next_udp_src < cfg_n_udp_src)
+                       continue;
+               next_udp_src = 0;
+               if (++next_ip_dst < cfg_n_ip_dst)
+                       continue;
+               next_ip_dst = 0;
+               if (++next_ip_src < cfg_n_ip_src)
+                       continue;
+               next_ip_src = 0;
        }
 
        nb_tx = rte_eth_tx_burst(fs->tx_port, fs->tx_queue, pkts_burst, nb_pkt);
        /*
         * Retry if necessary
         */
-       if (unlikely(nb_tx < nb_rx) && fs->retry_enabled) {
+       if (unlikely(nb_tx < nb_pkt) && fs->retry_enabled) {
                retry = 0;
-               while (nb_tx < nb_rx && retry++ < burst_tx_retry_num) {
+               while (nb_tx < nb_pkt && retry++ < burst_tx_retry_num) {
                        rte_delay_us(burst_tx_delay_time);
                        nb_tx += rte_eth_tx_burst(fs->tx_port, fs->tx_queue,
-                                       &pkts_burst[nb_tx], nb_rx - nb_tx);
+                                       &pkts_burst[nb_tx], nb_pkt - nb_tx);
                }
        }
-       fs->tx_packets += nb_tx;
 
        inc_tx_burst_stats(fs, nb_tx);
-       if (unlikely(nb_tx < nb_pkt)) {
-               /* Back out the flow counter. */
-               next_flow -= (nb_pkt - nb_tx);
-               while (next_flow < 0)
-                       next_flow += cfg_n_flows;
+       fs->tx_packets += nb_tx;
+       /* Catch up flow idx by actual sent. */
+       for (i = 0; i < nb_tx; ++i) {
+               RTE_PER_LCORE(_next_udp_dst) = RTE_PER_LCORE(_next_udp_dst) + 1;
+               if (RTE_PER_LCORE(_next_udp_dst) < cfg_n_udp_dst)
+                       continue;
+               RTE_PER_LCORE(_next_udp_dst) = 0;
+               RTE_PER_LCORE(_next_udp_src) = RTE_PER_LCORE(_next_udp_src) + 1;
+               if (RTE_PER_LCORE(_next_udp_src) < cfg_n_udp_src)
+                       continue;
+               RTE_PER_LCORE(_next_udp_src) = 0;
+               RTE_PER_LCORE(_next_ip_dst) = RTE_PER_LCORE(_next_ip_dst) + 1;
+               if (RTE_PER_LCORE(_next_ip_dst) < cfg_n_ip_dst)
+                       continue;
+               RTE_PER_LCORE(_next_ip_dst) = 0;
+               RTE_PER_LCORE(_next_ip_src) = RTE_PER_LCORE(_next_ip_src) + 1;
+               if (RTE_PER_LCORE(_next_ip_src) < cfg_n_ip_src)
+                       continue;
+               RTE_PER_LCORE(_next_ip_src) = 0;
+       }
 
+       if (unlikely(nb_tx < nb_pkt)) {
+               fs->fwd_dropped += nb_pkt - nb_tx;
                do {
                        rte_pktmbuf_free(pkts_burst[nb_tx]);
                } while (++nb_tx < nb_pkt);
-- 
2.11.0

Reply via email to