app/testpmd: fix IP checksum calculation
[dpdk.git] / app / test-pmd / flowgen.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2014-2020 Mellanox Technologies, Ltd
3  */
4
5 #include <stdarg.h>
6 #include <string.h>
7 #include <stdio.h>
8 #include <errno.h>
9 #include <stdint.h>
10 #include <unistd.h>
11 #include <inttypes.h>
12
13 #include <sys/queue.h>
14 #include <sys/stat.h>
15
16 #include <rte_common.h>
17 #include <rte_byteorder.h>
18 #include <rte_log.h>
19 #include <rte_debug.h>
20 #include <rte_cycles.h>
21 #include <rte_memory.h>
22 #include <rte_memcpy.h>
23 #include <rte_launch.h>
24 #include <rte_eal.h>
25 #include <rte_per_lcore.h>
26 #include <rte_lcore.h>
27 #include <rte_atomic.h>
28 #include <rte_branch_prediction.h>
29 #include <rte_mempool.h>
30 #include <rte_mbuf.h>
31 #include <rte_interrupts.h>
32 #include <rte_pci.h>
33 #include <rte_ether.h>
34 #include <rte_ethdev.h>
35 #include <rte_ip.h>
36 #include <rte_tcp.h>
37 #include <rte_udp.h>
38 #include <rte_string_fns.h>
39 #include <rte_flow.h>
40
41 #include "testpmd.h"
42
43 /* hardcoded configuration (for now) */
44 static unsigned cfg_n_flows     = 1024;
45 static uint32_t cfg_ip_src      = RTE_IPV4(10, 254, 0, 0);
46 static uint32_t cfg_ip_dst      = RTE_IPV4(10, 253, 0, 0);
47 static uint16_t cfg_udp_src     = 1000;
48 static uint16_t cfg_udp_dst     = 1001;
49 static struct rte_ether_addr cfg_ether_src =
50         {{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x00 }};
51 static struct rte_ether_addr cfg_ether_dst =
52         {{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x01 }};
53
54 #define IP_DEFTTL  64   /* from RFC 1340. */
55
56 /* Use this type to inform GCC that ip_sum violates aliasing rules. */
57 typedef unaligned_uint16_t alias_int16_t __attribute__((__may_alias__));
58
59 static inline uint16_t
60 ip_sum(const alias_int16_t *hdr, int hdr_len)
61 {
62         uint32_t sum = 0;
63
64         while (hdr_len > 1)
65         {
66                 sum += *hdr++;
67                 if (sum & 0x80000000)
68                         sum = (sum & 0xFFFF) + (sum >> 16);
69                 hdr_len -= 2;
70         }
71
72         while (sum >> 16)
73                 sum = (sum & 0xFFFF) + (sum >> 16);
74
75         return ~sum;
76 }
77
78 /*
79  * Multi-flow generation mode.
80  *
81  * We originate a bunch of flows (varying destination IP addresses), and
82  * terminate receive traffic.  Received traffic is simply discarded, but we
83  * still do so in order to maintain traffic statistics.
84  */
85 static void
86 pkt_burst_flow_gen(struct fwd_stream *fs)
87 {
88         unsigned pkt_size = tx_pkt_length - 4;  /* Adjust FCS */
89         struct rte_mbuf  *pkts_burst[MAX_PKT_BURST];
90         struct rte_mempool *mbp;
91         struct rte_mbuf  *pkt;
92         struct rte_ether_hdr *eth_hdr;
93         struct rte_ipv4_hdr *ip_hdr;
94         struct rte_udp_hdr *udp_hdr;
95         uint16_t vlan_tci, vlan_tci_outer;
96         uint64_t ol_flags = 0;
97         uint16_t nb_rx;
98         uint16_t nb_tx;
99         uint16_t nb_pkt;
100         uint16_t i;
101         uint32_t retry;
102         uint64_t tx_offloads;
103         uint64_t start_tsc = 0;
104         static int next_flow = 0;
105
106         get_start_cycles(&start_tsc);
107
108         /* Receive a burst of packets and discard them. */
109         nb_rx = rte_eth_rx_burst(fs->rx_port, fs->rx_queue, pkts_burst,
110                                  nb_pkt_per_burst);
111         fs->rx_packets += nb_rx;
112
113         for (i = 0; i < nb_rx; i++)
114                 rte_pktmbuf_free(pkts_burst[i]);
115
116         mbp = current_fwd_lcore()->mbp;
117         vlan_tci = ports[fs->tx_port].tx_vlan_id;
118         vlan_tci_outer = ports[fs->tx_port].tx_vlan_id_outer;
119
120         tx_offloads = ports[fs->tx_port].dev_conf.txmode.offloads;
121         if (tx_offloads & DEV_TX_OFFLOAD_VLAN_INSERT)
122                 ol_flags |= PKT_TX_VLAN_PKT;
123         if (tx_offloads & DEV_TX_OFFLOAD_QINQ_INSERT)
124                 ol_flags |= PKT_TX_QINQ_PKT;
125         if (tx_offloads & DEV_TX_OFFLOAD_MACSEC_INSERT)
126                 ol_flags |= PKT_TX_MACSEC;
127
128         for (nb_pkt = 0; nb_pkt < nb_pkt_per_burst; nb_pkt++) {
129                 pkt = rte_mbuf_raw_alloc(mbp);
130                 if (!pkt)
131                         break;
132
133                 pkt->data_len = pkt_size;
134                 pkt->next = NULL;
135
136                 /* Initialize Ethernet header. */
137                 eth_hdr = rte_pktmbuf_mtod(pkt, struct rte_ether_hdr *);
138                 rte_ether_addr_copy(&cfg_ether_dst, &eth_hdr->d_addr);
139                 rte_ether_addr_copy(&cfg_ether_src, &eth_hdr->s_addr);
140                 eth_hdr->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
141
142                 /* Initialize IP header. */
143                 ip_hdr = (struct rte_ipv4_hdr *)(eth_hdr + 1);
144                 memset(ip_hdr, 0, sizeof(*ip_hdr));
145                 ip_hdr->version_ihl     = RTE_IPV4_VHL_DEF;
146                 ip_hdr->type_of_service = 0;
147                 ip_hdr->fragment_offset = 0;
148                 ip_hdr->time_to_live    = IP_DEFTTL;
149                 ip_hdr->next_proto_id   = IPPROTO_UDP;
150                 ip_hdr->packet_id       = 0;
151                 ip_hdr->src_addr        = rte_cpu_to_be_32(cfg_ip_src);
152                 ip_hdr->dst_addr        = rte_cpu_to_be_32(cfg_ip_dst +
153                                                            next_flow);
154                 ip_hdr->total_length    = RTE_CPU_TO_BE_16(pkt_size -
155                                                            sizeof(*eth_hdr));
156                 ip_hdr->hdr_checksum    = ip_sum((const alias_int16_t *)ip_hdr,
157                                                  sizeof(*ip_hdr));
158
159                 /* Initialize UDP header. */
160                 udp_hdr = (struct rte_udp_hdr *)(ip_hdr + 1);
161                 udp_hdr->src_port       = rte_cpu_to_be_16(cfg_udp_src);
162                 udp_hdr->dst_port       = rte_cpu_to_be_16(cfg_udp_dst);
163                 udp_hdr->dgram_cksum    = 0; /* No UDP checksum. */
164                 udp_hdr->dgram_len      = RTE_CPU_TO_BE_16(pkt_size -
165                                                            sizeof(*eth_hdr) -
166                                                            sizeof(*ip_hdr));
167                 pkt->nb_segs            = 1;
168                 pkt->pkt_len            = pkt_size;
169                 pkt->ol_flags           &= EXT_ATTACHED_MBUF;
170                 pkt->ol_flags           |= ol_flags;
171                 pkt->vlan_tci           = vlan_tci;
172                 pkt->vlan_tci_outer     = vlan_tci_outer;
173                 pkt->l2_len             = sizeof(struct rte_ether_hdr);
174                 pkt->l3_len             = sizeof(struct rte_ipv4_hdr);
175                 pkts_burst[nb_pkt]      = pkt;
176
177                 next_flow = (next_flow + 1) % cfg_n_flows;
178         }
179
180         nb_tx = rte_eth_tx_burst(fs->tx_port, fs->tx_queue, pkts_burst, nb_pkt);
181         /*
182          * Retry if necessary
183          */
184         if (unlikely(nb_tx < nb_rx) && fs->retry_enabled) {
185                 retry = 0;
186                 while (nb_tx < nb_rx && retry++ < burst_tx_retry_num) {
187                         rte_delay_us(burst_tx_delay_time);
188                         nb_tx += rte_eth_tx_burst(fs->tx_port, fs->tx_queue,
189                                         &pkts_burst[nb_tx], nb_rx - nb_tx);
190                 }
191         }
192         fs->tx_packets += nb_tx;
193
194         inc_tx_burst_stats(fs, nb_tx);
195         if (unlikely(nb_tx < nb_pkt)) {
196                 /* Back out the flow counter. */
197                 next_flow -= (nb_pkt - nb_tx);
198                 while (next_flow < 0)
199                         next_flow += cfg_n_flows;
200
201                 do {
202                         rte_pktmbuf_free(pkts_burst[nb_tx]);
203                 } while (++nb_tx < nb_pkt);
204         }
205
206         get_end_cycles(fs, start_tsc);
207 }
208
209 struct fwd_engine flow_gen_engine = {
210         .fwd_mode_name  = "flowgen",
211         .port_fwd_begin = NULL,
212         .port_fwd_end   = NULL,
213         .packet_fwd     = pkt_burst_flow_gen,
214 };