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