New upstream version 17.11.1
[deb_dpdk.git] / app / test-pmd / txonly.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel 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 Intel 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 #include <stdarg.h>
35 #include <string.h>
36 #include <stdio.h>
37 #include <errno.h>
38 #include <stdint.h>
39 #include <unistd.h>
40 #include <inttypes.h>
41
42 #include <sys/queue.h>
43 #include <sys/stat.h>
44
45 #include <rte_common.h>
46 #include <rte_byteorder.h>
47 #include <rte_log.h>
48 #include <rte_debug.h>
49 #include <rte_cycles.h>
50 #include <rte_memory.h>
51 #include <rte_memcpy.h>
52 #include <rte_launch.h>
53 #include <rte_eal.h>
54 #include <rte_per_lcore.h>
55 #include <rte_lcore.h>
56 #include <rte_atomic.h>
57 #include <rte_branch_prediction.h>
58 #include <rte_mempool.h>
59 #include <rte_mbuf.h>
60 #include <rte_interrupts.h>
61 #include <rte_pci.h>
62 #include <rte_ether.h>
63 #include <rte_ethdev.h>
64 #include <rte_ip.h>
65 #include <rte_tcp.h>
66 #include <rte_udp.h>
67 #include <rte_string_fns.h>
68 #include <rte_flow.h>
69
70 #include "testpmd.h"
71
72 #define UDP_SRC_PORT 1024
73 #define UDP_DST_PORT 1024
74
75 #define IP_SRC_ADDR ((192U << 24) | (168 << 16) | (0 << 8) | 1)
76 #define IP_DST_ADDR ((192U << 24) | (168 << 16) | (0 << 8) | 2)
77
78 #define IP_DEFTTL  64   /* from RFC 1340. */
79 #define IP_VERSION 0x40
80 #define IP_HDRLEN  0x05 /* default IP header length == five 32-bits words. */
81 #define IP_VHL_DEF (IP_VERSION | IP_HDRLEN)
82
83 static struct ipv4_hdr  pkt_ip_hdr;  /**< IP header of transmitted packets. */
84 static struct udp_hdr pkt_udp_hdr; /**< UDP header of transmitted packets. */
85
86 static void
87 copy_buf_to_pkt_segs(void* buf, unsigned len, struct rte_mbuf *pkt,
88                      unsigned offset)
89 {
90         struct rte_mbuf *seg;
91         void *seg_buf;
92         unsigned copy_len;
93
94         seg = pkt;
95         while (offset >= seg->data_len) {
96                 offset -= seg->data_len;
97                 seg = seg->next;
98         }
99         copy_len = seg->data_len - offset;
100         seg_buf = rte_pktmbuf_mtod_offset(seg, char *, offset);
101         while (len > copy_len) {
102                 rte_memcpy(seg_buf, buf, (size_t) copy_len);
103                 len -= copy_len;
104                 buf = ((char*) buf + copy_len);
105                 seg = seg->next;
106                 seg_buf = rte_pktmbuf_mtod(seg, char *);
107                 copy_len = seg->data_len;
108         }
109         rte_memcpy(seg_buf, buf, (size_t) len);
110 }
111
112 static inline void
113 copy_buf_to_pkt(void* buf, unsigned len, struct rte_mbuf *pkt, unsigned offset)
114 {
115         if (offset + len <= pkt->data_len) {
116                 rte_memcpy(rte_pktmbuf_mtod_offset(pkt, char *, offset),
117                         buf, (size_t) len);
118                 return;
119         }
120         copy_buf_to_pkt_segs(buf, len, pkt, offset);
121 }
122
123 static void
124 setup_pkt_udp_ip_headers(struct ipv4_hdr *ip_hdr,
125                          struct udp_hdr *udp_hdr,
126                          uint16_t pkt_data_len)
127 {
128         uint16_t *ptr16;
129         uint32_t ip_cksum;
130         uint16_t pkt_len;
131
132         /*
133          * Initialize UDP header.
134          */
135         pkt_len = (uint16_t) (pkt_data_len + sizeof(struct udp_hdr));
136         udp_hdr->src_port = rte_cpu_to_be_16(UDP_SRC_PORT);
137         udp_hdr->dst_port = rte_cpu_to_be_16(UDP_DST_PORT);
138         udp_hdr->dgram_len      = RTE_CPU_TO_BE_16(pkt_len);
139         udp_hdr->dgram_cksum    = 0; /* No UDP checksum. */
140
141         /*
142          * Initialize IP header.
143          */
144         pkt_len = (uint16_t) (pkt_len + sizeof(struct ipv4_hdr));
145         ip_hdr->version_ihl   = IP_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->total_length   = RTE_CPU_TO_BE_16(pkt_len);
152         ip_hdr->src_addr = rte_cpu_to_be_32(IP_SRC_ADDR);
153         ip_hdr->dst_addr = rte_cpu_to_be_32(IP_DST_ADDR);
154
155         /*
156          * Compute IP header checksum.
157          */
158         ptr16 = (unaligned_uint16_t*) ip_hdr;
159         ip_cksum = 0;
160         ip_cksum += ptr16[0]; ip_cksum += ptr16[1];
161         ip_cksum += ptr16[2]; ip_cksum += ptr16[3];
162         ip_cksum += ptr16[4];
163         ip_cksum += ptr16[6]; ip_cksum += ptr16[7];
164         ip_cksum += ptr16[8]; ip_cksum += ptr16[9];
165
166         /*
167          * Reduce 32 bit checksum to 16 bits and complement it.
168          */
169         ip_cksum = ((ip_cksum & 0xFFFF0000) >> 16) +
170                 (ip_cksum & 0x0000FFFF);
171         if (ip_cksum > 65535)
172                 ip_cksum -= 65535;
173         ip_cksum = (~ip_cksum) & 0x0000FFFF;
174         if (ip_cksum == 0)
175                 ip_cksum = 0xFFFF;
176         ip_hdr->hdr_checksum = (uint16_t) ip_cksum;
177 }
178
179 /*
180  * Transmit a burst of multi-segments packets.
181  */
182 static void
183 pkt_burst_transmit(struct fwd_stream *fs)
184 {
185         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
186         struct rte_port *txp;
187         struct rte_mbuf *pkt;
188         struct rte_mbuf *pkt_seg;
189         struct rte_mempool *mbp;
190         struct ether_hdr eth_hdr;
191         uint16_t nb_tx;
192         uint16_t nb_pkt;
193         uint16_t vlan_tci, vlan_tci_outer;
194         uint32_t retry;
195         uint64_t ol_flags = 0;
196         uint8_t  i;
197 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
198         uint64_t start_tsc;
199         uint64_t end_tsc;
200         uint64_t core_cycles;
201 #endif
202         uint32_t nb_segs, pkt_len;
203
204 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
205         start_tsc = rte_rdtsc();
206 #endif
207
208         mbp = current_fwd_lcore()->mbp;
209         txp = &ports[fs->tx_port];
210         vlan_tci = txp->tx_vlan_id;
211         vlan_tci_outer = txp->tx_vlan_id_outer;
212         if (txp->tx_ol_flags & TESTPMD_TX_OFFLOAD_INSERT_VLAN)
213                 ol_flags = PKT_TX_VLAN_PKT;
214         if (txp->tx_ol_flags & TESTPMD_TX_OFFLOAD_INSERT_QINQ)
215                 ol_flags |= PKT_TX_QINQ_PKT;
216         if (txp->tx_ol_flags & TESTPMD_TX_OFFLOAD_MACSEC)
217                 ol_flags |= PKT_TX_MACSEC;
218         for (nb_pkt = 0; nb_pkt < nb_pkt_per_burst; nb_pkt++) {
219                 pkt = rte_mbuf_raw_alloc(mbp);
220                 if (pkt == NULL) {
221                 nomore_mbuf:
222                         if (nb_pkt == 0)
223                                 return;
224                         break;
225                 }
226
227                 /*
228                  * Using raw alloc is good to improve performance,
229                  * but some consumers may use the headroom and so
230                  * decrement data_off. We need to make sure it is
231                  * reset to default value.
232                  */
233                 rte_pktmbuf_reset_headroom(pkt);
234                 pkt->data_len = tx_pkt_seg_lengths[0];
235                 pkt_seg = pkt;
236                 if (tx_pkt_split == TX_PKT_SPLIT_RND)
237                         nb_segs = random() % tx_pkt_nb_segs + 1;
238                 else
239                         nb_segs = tx_pkt_nb_segs;
240                 pkt_len = pkt->data_len;
241                 for (i = 1; i < nb_segs; i++) {
242                         pkt_seg->next = rte_mbuf_raw_alloc(mbp);
243                         if (pkt_seg->next == NULL) {
244                                 pkt->nb_segs = i;
245                                 rte_pktmbuf_free(pkt);
246                                 goto nomore_mbuf;
247                         }
248                         pkt_seg = pkt_seg->next;
249                         pkt_seg->data_len = tx_pkt_seg_lengths[i];
250                         pkt_len += pkt_seg->data_len;
251                 }
252                 pkt_seg->next = NULL; /* Last segment of packet. */
253
254                 /*
255                  * Initialize Ethernet header.
256                  */
257                 ether_addr_copy(&peer_eth_addrs[fs->peer_addr],&eth_hdr.d_addr);
258                 ether_addr_copy(&ports[fs->tx_port].eth_addr, &eth_hdr.s_addr);
259                 eth_hdr.ether_type = rte_cpu_to_be_16(ETHER_TYPE_IPv4);
260
261                 /*
262                  * Copy headers in first packet segment(s).
263                  */
264                 copy_buf_to_pkt(&eth_hdr, sizeof(eth_hdr), pkt, 0);
265                 copy_buf_to_pkt(&pkt_ip_hdr, sizeof(pkt_ip_hdr), pkt,
266                                 sizeof(struct ether_hdr));
267                 copy_buf_to_pkt(&pkt_udp_hdr, sizeof(pkt_udp_hdr), pkt,
268                                 sizeof(struct ether_hdr) +
269                                 sizeof(struct ipv4_hdr));
270
271                 /*
272                  * Complete first mbuf of packet and append it to the
273                  * burst of packets to be transmitted.
274                  */
275                 pkt->nb_segs = nb_segs;
276                 pkt->pkt_len = pkt_len;
277                 pkt->ol_flags = ol_flags;
278                 pkt->vlan_tci = vlan_tci;
279                 pkt->vlan_tci_outer = vlan_tci_outer;
280                 pkt->l2_len = sizeof(struct ether_hdr);
281                 pkt->l3_len = sizeof(struct ipv4_hdr);
282                 pkts_burst[nb_pkt] = pkt;
283         }
284         nb_tx = rte_eth_tx_burst(fs->tx_port, fs->tx_queue, pkts_burst, nb_pkt);
285         /*
286          * Retry if necessary
287          */
288         if (unlikely(nb_tx < nb_pkt) && fs->retry_enabled) {
289                 retry = 0;
290                 while (nb_tx < nb_pkt && retry++ < burst_tx_retry_num) {
291                         rte_delay_us(burst_tx_delay_time);
292                         nb_tx += rte_eth_tx_burst(fs->tx_port, fs->tx_queue,
293                                         &pkts_burst[nb_tx], nb_pkt - nb_tx);
294                 }
295         }
296         fs->tx_packets += nb_tx;
297
298 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
299         fs->tx_burst_stats.pkt_burst_spread[nb_tx]++;
300 #endif
301         if (unlikely(nb_tx < nb_pkt)) {
302                 if (verbose_level > 0 && fs->fwd_dropped == 0)
303                         printf("port %d tx_queue %d - drop "
304                                "(nb_pkt:%u - nb_tx:%u)=%u packets\n",
305                                fs->tx_port, fs->tx_queue,
306                                (unsigned) nb_pkt, (unsigned) nb_tx,
307                                (unsigned) (nb_pkt - nb_tx));
308                 fs->fwd_dropped += (nb_pkt - nb_tx);
309                 do {
310                         rte_pktmbuf_free(pkts_burst[nb_tx]);
311                 } while (++nb_tx < nb_pkt);
312         }
313
314 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
315         end_tsc = rte_rdtsc();
316         core_cycles = (end_tsc - start_tsc);
317         fs->core_cycles = (uint64_t) (fs->core_cycles + core_cycles);
318 #endif
319 }
320
321 static void
322 tx_only_begin(__attribute__((unused)) portid_t pi)
323 {
324         uint16_t pkt_data_len;
325
326         pkt_data_len = (uint16_t) (tx_pkt_length - (sizeof(struct ether_hdr) +
327                                                     sizeof(struct ipv4_hdr) +
328                                                     sizeof(struct udp_hdr)));
329         setup_pkt_udp_ip_headers(&pkt_ip_hdr, &pkt_udp_hdr, pkt_data_len);
330 }
331
332 struct fwd_engine tx_only_engine = {
333         .fwd_mode_name  = "txonly",
334         .port_fwd_begin = tx_only_begin,
335         .port_fwd_end   = NULL,
336         .packet_fwd     = pkt_burst_transmit,
337 };