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