remove extra parentheses in return statement
[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_memzone.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_ring.h>
60 #include <rte_memory.h>
61 #include <rte_mempool.h>
62 #include <rte_mbuf.h>
63 #include <rte_memcpy.h>
64 #include <rte_interrupts.h>
65 #include <rte_pci.h>
66 #include <rte_ether.h>
67 #include <rte_ethdev.h>
68 #include <rte_ip.h>
69 #include <rte_tcp.h>
70 #include <rte_udp.h>
71 #include <rte_string_fns.h>
72
73 #include "testpmd.h"
74
75 #define UDP_SRC_PORT 1024
76 #define UDP_DST_PORT 1024
77
78 #define IP_SRC_ADDR ((192U << 24) | (168 << 16) | (0 << 8) | 1)
79 #define IP_DST_ADDR ((192U << 24) | (168 << 16) | (0 << 8) | 2)
80
81 #define IP_DEFTTL  64   /* from RFC 1340. */
82 #define IP_VERSION 0x40
83 #define IP_HDRLEN  0x05 /* default IP header length == five 32-bits words. */
84 #define IP_VHL_DEF (IP_VERSION | IP_HDRLEN)
85
86 static struct ipv4_hdr  pkt_ip_hdr;  /**< IP header of transmitted packets. */
87 static struct udp_hdr pkt_udp_hdr; /**< UDP header of transmitted packets. */
88
89 static inline struct rte_mbuf *
90 tx_mbuf_alloc(struct rte_mempool *mp)
91 {
92         struct rte_mbuf *m;
93
94         m = __rte_mbuf_raw_alloc(mp);
95         __rte_mbuf_sanity_check_raw(m, 0);
96         return m;
97 }
98
99 static void
100 copy_buf_to_pkt_segs(void* buf, unsigned len, struct rte_mbuf *pkt,
101                      unsigned offset)
102 {
103         struct rte_mbuf *seg;
104         void *seg_buf;
105         unsigned copy_len;
106
107         seg = pkt;
108         while (offset >= seg->data_len) {
109                 offset -= seg->data_len;
110                 seg = seg->next;
111         }
112         copy_len = seg->data_len - offset;
113         seg_buf = rte_pktmbuf_mtod_offset(seg, char *, offset);
114         while (len > copy_len) {
115                 rte_memcpy(seg_buf, buf, (size_t) copy_len);
116                 len -= copy_len;
117                 buf = ((char*) buf + copy_len);
118                 seg = seg->next;
119                 seg_buf = rte_pktmbuf_mtod(seg, char *);
120         }
121         rte_memcpy(seg_buf, buf, (size_t) len);
122 }
123
124 static inline void
125 copy_buf_to_pkt(void* buf, unsigned len, struct rte_mbuf *pkt, unsigned offset)
126 {
127         if (offset + len <= pkt->data_len) {
128                 rte_memcpy(rte_pktmbuf_mtod_offset(pkt, char *, offset),
129                         buf, (size_t) len);
130                 return;
131         }
132         copy_buf_to_pkt_segs(buf, len, pkt, offset);
133 }
134
135 static void
136 setup_pkt_udp_ip_headers(struct ipv4_hdr *ip_hdr,
137                          struct udp_hdr *udp_hdr,
138                          uint16_t pkt_data_len)
139 {
140         uint16_t *ptr16;
141         uint32_t ip_cksum;
142         uint16_t pkt_len;
143
144         /*
145          * Initialize UDP header.
146          */
147         pkt_len = (uint16_t) (pkt_data_len + sizeof(struct udp_hdr));
148         udp_hdr->src_port = rte_cpu_to_be_16(UDP_SRC_PORT);
149         udp_hdr->dst_port = rte_cpu_to_be_16(UDP_DST_PORT);
150         udp_hdr->dgram_len      = RTE_CPU_TO_BE_16(pkt_len);
151         udp_hdr->dgram_cksum    = 0; /* No UDP checksum. */
152
153         /*
154          * Initialize IP header.
155          */
156         pkt_len = (uint16_t) (pkt_len + sizeof(struct ipv4_hdr));
157         ip_hdr->version_ihl   = IP_VHL_DEF;
158         ip_hdr->type_of_service   = 0;
159         ip_hdr->fragment_offset = 0;
160         ip_hdr->time_to_live   = IP_DEFTTL;
161         ip_hdr->next_proto_id = IPPROTO_UDP;
162         ip_hdr->packet_id = 0;
163         ip_hdr->total_length   = RTE_CPU_TO_BE_16(pkt_len);
164         ip_hdr->src_addr = rte_cpu_to_be_32(IP_SRC_ADDR);
165         ip_hdr->dst_addr = rte_cpu_to_be_32(IP_DST_ADDR);
166
167         /*
168          * Compute IP header checksum.
169          */
170         ptr16 = (unaligned_uint16_t*) ip_hdr;
171         ip_cksum = 0;
172         ip_cksum += ptr16[0]; ip_cksum += ptr16[1];
173         ip_cksum += ptr16[2]; ip_cksum += ptr16[3];
174         ip_cksum += ptr16[4];
175         ip_cksum += ptr16[6]; ip_cksum += ptr16[7];
176         ip_cksum += ptr16[8]; ip_cksum += ptr16[9];
177
178         /*
179          * Reduce 32 bit checksum to 16 bits and complement it.
180          */
181         ip_cksum = ((ip_cksum & 0xFFFF0000) >> 16) +
182                 (ip_cksum & 0x0000FFFF);
183         if (ip_cksum > 65535)
184                 ip_cksum -= 65535;
185         ip_cksum = (~ip_cksum) & 0x0000FFFF;
186         if (ip_cksum == 0)
187                 ip_cksum = 0xFFFF;
188         ip_hdr->hdr_checksum = (uint16_t) ip_cksum;
189 }
190
191 /*
192  * Transmit a burst of multi-segments packets.
193  */
194 static void
195 pkt_burst_transmit(struct fwd_stream *fs)
196 {
197         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
198         struct rte_port *txp;
199         struct rte_mbuf *pkt;
200         struct rte_mbuf *pkt_seg;
201         struct rte_mempool *mbp;
202         struct ether_hdr eth_hdr;
203         uint16_t nb_tx;
204         uint16_t nb_pkt;
205         uint16_t vlan_tci, vlan_tci_outer;
206         uint64_t ol_flags = 0;
207         uint8_t  i;
208 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
209         uint64_t start_tsc;
210         uint64_t end_tsc;
211         uint64_t core_cycles;
212 #endif
213         uint32_t nb_segs, pkt_len;
214
215 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
216         start_tsc = rte_rdtsc();
217 #endif
218
219         mbp = current_fwd_lcore()->mbp;
220         txp = &ports[fs->tx_port];
221         vlan_tci = txp->tx_vlan_id;
222         vlan_tci_outer = txp->tx_vlan_id_outer;
223         if (txp->tx_ol_flags & TESTPMD_TX_OFFLOAD_INSERT_VLAN)
224                 ol_flags = PKT_TX_VLAN_PKT;
225         if (txp->tx_ol_flags & TESTPMD_TX_OFFLOAD_INSERT_QINQ)
226                 ol_flags |= PKT_TX_QINQ_PKT;
227         for (nb_pkt = 0; nb_pkt < nb_pkt_per_burst; nb_pkt++) {
228                 pkt = tx_mbuf_alloc(mbp);
229                 if (pkt == NULL) {
230                 nomore_mbuf:
231                         if (nb_pkt == 0)
232                                 return;
233                         break;
234                 }
235                 pkt->data_len = tx_pkt_seg_lengths[0];
236                 pkt_seg = pkt;
237                 if (tx_pkt_split == TX_PKT_SPLIT_RND)
238                         nb_segs = random() % tx_pkt_nb_segs + 1;
239                 else
240                         nb_segs = tx_pkt_nb_segs;
241                 pkt_len = pkt->data_len;
242                 for (i = 1; i < nb_segs; i++) {
243                         pkt_seg->next = tx_mbuf_alloc(mbp);
244                         if (pkt_seg->next == NULL) {
245                                 pkt->nb_segs = i;
246                                 rte_pktmbuf_free(pkt);
247                                 goto nomore_mbuf;
248                         }
249                         pkt_seg = pkt_seg->next;
250                         pkt_seg->data_len = tx_pkt_seg_lengths[i];
251                         pkt_len += pkt_seg->data_len;
252                 }
253                 pkt_seg->next = NULL; /* Last segment of packet. */
254
255                 /*
256                  * Initialize Ethernet header.
257                  */
258                 ether_addr_copy(&peer_eth_addrs[fs->peer_addr],&eth_hdr.d_addr);
259                 ether_addr_copy(&ports[fs->tx_port].eth_addr, &eth_hdr.s_addr);
260                 eth_hdr.ether_type = rte_cpu_to_be_16(ETHER_TYPE_IPv4);
261
262                 /*
263                  * Copy headers in first packet segment(s).
264                  */
265                 copy_buf_to_pkt(&eth_hdr, sizeof(eth_hdr), pkt, 0);
266                 copy_buf_to_pkt(&pkt_ip_hdr, sizeof(pkt_ip_hdr), pkt,
267                                 sizeof(struct ether_hdr));
268                 copy_buf_to_pkt(&pkt_udp_hdr, sizeof(pkt_udp_hdr), pkt,
269                                 sizeof(struct ether_hdr) +
270                                 sizeof(struct ipv4_hdr));
271
272                 /*
273                  * Complete first mbuf of packet and append it to the
274                  * burst of packets to be transmitted.
275                  */
276                 pkt->nb_segs = nb_segs;
277                 pkt->pkt_len = pkt_len;
278                 pkt->ol_flags = ol_flags;
279                 pkt->vlan_tci = vlan_tci;
280                 pkt->vlan_tci_outer = vlan_tci_outer;
281                 pkt->l2_len = sizeof(struct ether_hdr);
282                 pkt->l3_len = sizeof(struct ipv4_hdr);
283                 pkts_burst[nb_pkt] = pkt;
284         }
285         nb_tx = rte_eth_tx_burst(fs->tx_port, fs->tx_queue, pkts_burst, nb_pkt);
286         fs->tx_packets += nb_tx;
287
288 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
289         fs->tx_burst_stats.pkt_burst_spread[nb_tx]++;
290 #endif
291         if (unlikely(nb_tx < nb_pkt)) {
292                 if (verbose_level > 0 && fs->fwd_dropped == 0)
293                         printf("port %d tx_queue %d - drop "
294                                "(nb_pkt:%u - nb_tx:%u)=%u packets\n",
295                                fs->tx_port, fs->tx_queue,
296                                (unsigned) nb_pkt, (unsigned) nb_tx,
297                                (unsigned) (nb_pkt - nb_tx));
298                 fs->fwd_dropped += (nb_pkt - nb_tx);
299                 do {
300                         rte_pktmbuf_free(pkts_burst[nb_tx]);
301                 } while (++nb_tx < nb_pkt);
302         }
303
304 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
305         end_tsc = rte_rdtsc();
306         core_cycles = (end_tsc - start_tsc);
307         fs->core_cycles = (uint64_t) (fs->core_cycles + core_cycles);
308 #endif
309 }
310
311 static void
312 tx_only_begin(__attribute__((unused)) portid_t pi)
313 {
314         uint16_t pkt_data_len;
315
316         pkt_data_len = (uint16_t) (tx_pkt_length - (sizeof(struct ether_hdr) +
317                                                     sizeof(struct ipv4_hdr) +
318                                                     sizeof(struct udp_hdr)));
319         setup_pkt_udp_ip_headers(&pkt_ip_hdr, &pkt_udp_hdr, pkt_data_len);
320 }
321
322 struct fwd_engine tx_only_engine = {
323         .fwd_mode_name  = "txonly",
324         .port_fwd_begin = tx_only_begin,
325         .port_fwd_end   = NULL,
326         .packet_fwd     = pkt_burst_transmit,
327 };