New upstream version 17.11.1
[deb_dpdk.git] / app / test-pmd / flowgen.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2013 Tilera Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Tilera Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34
35 #include <stdarg.h>
36 #include <string.h>
37 #include <stdio.h>
38 #include <errno.h>
39 #include <stdint.h>
40 #include <unistd.h>
41 #include <inttypes.h>
42
43 #include <sys/queue.h>
44 #include <sys/stat.h>
45
46 #include <rte_common.h>
47 #include <rte_byteorder.h>
48 #include <rte_log.h>
49 #include <rte_debug.h>
50 #include <rte_cycles.h>
51 #include <rte_memory.h>
52 #include <rte_memcpy.h>
53 #include <rte_launch.h>
54 #include <rte_eal.h>
55 #include <rte_per_lcore.h>
56 #include <rte_lcore.h>
57 #include <rte_atomic.h>
58 #include <rte_branch_prediction.h>
59 #include <rte_mempool.h>
60 #include <rte_mbuf.h>
61 #include <rte_interrupts.h>
62 #include <rte_pci.h>
63 #include <rte_ether.h>
64 #include <rte_ethdev.h>
65 #include <rte_ip.h>
66 #include <rte_tcp.h>
67 #include <rte_udp.h>
68 #include <rte_string_fns.h>
69 #include <rte_flow.h>
70
71 #include "testpmd.h"
72
73 /* hardcoded configuration (for now) */
74 static unsigned cfg_n_flows     = 1024;
75 static uint32_t cfg_ip_src      = IPv4(10, 254, 0, 0);
76 static uint32_t cfg_ip_dst      = IPv4(10, 253, 0, 0);
77 static uint16_t cfg_udp_src     = 1000;
78 static uint16_t cfg_udp_dst     = 1001;
79 static struct ether_addr cfg_ether_src  =
80         {{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x00 }};
81 static struct ether_addr cfg_ether_dst  =
82         {{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x01 }};
83
84 #define IP_DEFTTL  64   /* from RFC 1340. */
85 #define IP_VERSION 0x40
86 #define IP_HDRLEN  0x05 /* default IP header length == five 32-bits words. */
87 #define IP_VHL_DEF (IP_VERSION | IP_HDRLEN)
88
89 static inline uint16_t
90 ip_sum(const unaligned_uint16_t *hdr, int hdr_len)
91 {
92         uint32_t sum = 0;
93
94         while (hdr_len > 1)
95         {
96                 sum += *hdr++;
97                 if (sum & 0x80000000)
98                         sum = (sum & 0xFFFF) + (sum >> 16);
99                 hdr_len -= 2;
100         }
101
102         while (sum >> 16)
103                 sum = (sum & 0xFFFF) + (sum >> 16);
104
105         return ~sum;
106 }
107
108 /*
109  * Multi-flow generation mode.
110  *
111  * We originate a bunch of flows (varying destination IP addresses), and
112  * terminate receive traffic.  Received traffic is simply discarded, but we
113  * still do so in order to maintain traffic statistics.
114  */
115 static void
116 pkt_burst_flow_gen(struct fwd_stream *fs)
117 {
118         unsigned pkt_size = tx_pkt_length - 4;  /* Adjust FCS */
119         struct rte_mbuf  *pkts_burst[MAX_PKT_BURST];
120         struct rte_mempool *mbp;
121         struct rte_mbuf  *pkt;
122         struct ether_hdr *eth_hdr;
123         struct ipv4_hdr *ip_hdr;
124         struct udp_hdr *udp_hdr;
125         uint16_t vlan_tci, vlan_tci_outer;
126         uint64_t ol_flags;
127         uint16_t nb_rx;
128         uint16_t nb_tx;
129         uint16_t nb_pkt;
130         uint16_t i;
131         uint32_t retry;
132 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
133         uint64_t start_tsc;
134         uint64_t end_tsc;
135         uint64_t core_cycles;
136 #endif
137         static int next_flow = 0;
138
139 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
140         start_tsc = rte_rdtsc();
141 #endif
142
143         /* Receive a burst of packets and discard them. */
144         nb_rx = rte_eth_rx_burst(fs->rx_port, fs->rx_queue, pkts_burst,
145                                  nb_pkt_per_burst);
146         fs->rx_packets += nb_rx;
147
148         for (i = 0; i < nb_rx; i++)
149                 rte_pktmbuf_free(pkts_burst[i]);
150
151         mbp = current_fwd_lcore()->mbp;
152         vlan_tci = ports[fs->tx_port].tx_vlan_id;
153         vlan_tci_outer = ports[fs->tx_port].tx_vlan_id_outer;
154
155         if (ports[fs->tx_port].tx_ol_flags & TESTPMD_TX_OFFLOAD_INSERT_VLAN)
156                 ol_flags = PKT_TX_VLAN_PKT;
157         if (ports[fs->tx_port].tx_ol_flags & TESTPMD_TX_OFFLOAD_INSERT_QINQ)
158                 ol_flags |= PKT_TX_QINQ_PKT;
159         if (ports[fs->tx_port].tx_ol_flags & TESTPMD_TX_OFFLOAD_MACSEC)
160                 ol_flags |= PKT_TX_MACSEC;
161
162         for (nb_pkt = 0; nb_pkt < nb_pkt_per_burst; nb_pkt++) {
163                 pkt = rte_mbuf_raw_alloc(mbp);
164                 if (!pkt)
165                         break;
166
167                 pkt->data_len = pkt_size;
168                 pkt->next = NULL;
169
170                 /* Initialize Ethernet header. */
171                 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
172                 ether_addr_copy(&cfg_ether_dst, &eth_hdr->d_addr);
173                 ether_addr_copy(&cfg_ether_src, &eth_hdr->s_addr);
174                 eth_hdr->ether_type = rte_cpu_to_be_16(ETHER_TYPE_IPv4);
175
176                 /* Initialize IP header. */
177                 ip_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
178                 memset(ip_hdr, 0, sizeof(*ip_hdr));
179                 ip_hdr->version_ihl     = IP_VHL_DEF;
180                 ip_hdr->type_of_service = 0;
181                 ip_hdr->fragment_offset = 0;
182                 ip_hdr->time_to_live    = IP_DEFTTL;
183                 ip_hdr->next_proto_id   = IPPROTO_UDP;
184                 ip_hdr->packet_id       = 0;
185                 ip_hdr->src_addr        = rte_cpu_to_be_32(cfg_ip_src);
186                 ip_hdr->dst_addr        = rte_cpu_to_be_32(cfg_ip_dst +
187                                                            next_flow);
188                 ip_hdr->total_length    = RTE_CPU_TO_BE_16(pkt_size -
189                                                            sizeof(*eth_hdr));
190                 ip_hdr->hdr_checksum    = ip_sum((unaligned_uint16_t *)ip_hdr,
191                                                  sizeof(*ip_hdr));
192
193                 /* Initialize UDP header. */
194                 udp_hdr = (struct udp_hdr *)(ip_hdr + 1);
195                 udp_hdr->src_port       = rte_cpu_to_be_16(cfg_udp_src);
196                 udp_hdr->dst_port       = rte_cpu_to_be_16(cfg_udp_dst);
197                 udp_hdr->dgram_cksum    = 0; /* No UDP checksum. */
198                 udp_hdr->dgram_len      = RTE_CPU_TO_BE_16(pkt_size -
199                                                            sizeof(*eth_hdr) -
200                                                            sizeof(*ip_hdr));
201                 pkt->nb_segs            = 1;
202                 pkt->pkt_len            = pkt_size;
203                 pkt->ol_flags           = ol_flags;
204                 pkt->vlan_tci           = vlan_tci;
205                 pkt->vlan_tci_outer     = vlan_tci_outer;
206                 pkt->l2_len             = sizeof(struct ether_hdr);
207                 pkt->l3_len             = sizeof(struct ipv4_hdr);
208                 pkts_burst[nb_pkt]      = pkt;
209
210                 next_flow = (next_flow + 1) % cfg_n_flows;
211         }
212
213         nb_tx = rte_eth_tx_burst(fs->tx_port, fs->tx_queue, pkts_burst, nb_pkt);
214         /*
215          * Retry if necessary
216          */
217         if (unlikely(nb_tx < nb_rx) && fs->retry_enabled) {
218                 retry = 0;
219                 while (nb_tx < nb_rx && retry++ < burst_tx_retry_num) {
220                         rte_delay_us(burst_tx_delay_time);
221                         nb_tx += rte_eth_tx_burst(fs->tx_port, fs->tx_queue,
222                                         &pkts_burst[nb_tx], nb_rx - nb_tx);
223                 }
224         }
225         fs->tx_packets += nb_tx;
226
227 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
228         fs->tx_burst_stats.pkt_burst_spread[nb_tx]++;
229 #endif
230         if (unlikely(nb_tx < nb_pkt)) {
231                 /* Back out the flow counter. */
232                 next_flow -= (nb_pkt - nb_tx);
233                 while (next_flow < 0)
234                         next_flow += cfg_n_flows;
235
236                 do {
237                         rte_pktmbuf_free(pkts_burst[nb_tx]);
238                 } while (++nb_tx < nb_pkt);
239         }
240 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
241         end_tsc = rte_rdtsc();
242         core_cycles = (end_tsc - start_tsc);
243         fs->core_cycles = (uint64_t) (fs->core_cycles + core_cycles);
244 #endif
245 }
246
247 struct fwd_engine flow_gen_engine = {
248         .fwd_mode_name  = "flowgen",
249         .port_fwd_begin = NULL,
250         .port_fwd_end   = NULL,
251         .packet_fwd     = pkt_burst_flow_gen,
252 };