c5c8ff288a0566b615d6ca773d93824556ce23cf
[dpdk.git] / app / test-pmd / csumonly.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 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 <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_tailq.h>
55 #include <rte_eal.h>
56 #include <rte_per_lcore.h>
57 #include <rte_lcore.h>
58 #include <rte_atomic.h>
59 #include <rte_branch_prediction.h>
60 #include <rte_ring.h>
61 #include <rte_memory.h>
62 #include <rte_mempool.h>
63 #include <rte_mbuf.h>
64 #include <rte_memcpy.h>
65 #include <rte_interrupts.h>
66 #include <rte_pci.h>
67 #include <rte_ether.h>
68 #include <rte_ethdev.h>
69 #include <rte_ip.h>
70 #include <rte_tcp.h>
71 #include <rte_udp.h>
72 #include <rte_sctp.h>
73 #include <rte_prefetch.h>
74 #include <rte_string_fns.h>
75 #include "testpmd.h"
76
77
78
79 #define IP_DEFTTL  64   /* from RFC 1340. */
80 #define IP_VERSION 0x40
81 #define IP_HDRLEN  0x05 /* default IP header length == five 32-bits words. */
82 #define IP_VHL_DEF (IP_VERSION | IP_HDRLEN)
83
84 static inline uint16_t
85 get_16b_sum(uint16_t *ptr16, uint32_t nr)
86 {
87         uint32_t sum = 0;
88         while (nr > 1)
89         {
90                 sum +=*ptr16;
91                 nr -= sizeof(uint16_t);
92                 ptr16++;
93                 if (sum > UINT16_MAX)
94                         sum -= UINT16_MAX;
95         }
96
97         /* If length is in odd bytes */
98         if (nr)
99                 sum += *((uint8_t*)ptr16);
100
101         sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
102         sum &= 0x0ffff;
103         return (uint16_t)sum;
104 }
105
106 static inline uint16_t
107 get_ipv4_cksum(struct ipv4_hdr *ipv4_hdr)
108 {
109         uint16_t cksum;
110         cksum = get_16b_sum((uint16_t*)ipv4_hdr, sizeof(struct ipv4_hdr));
111         return (uint16_t)((cksum == 0xffff)?cksum:~cksum);
112 }
113
114
115 static inline uint16_t
116 get_ipv4_psd_sum (struct ipv4_hdr * ip_hdr)
117 {
118         /* Pseudo Header for IPv4/UDP/TCP checksum */
119         union ipv4_psd_header {
120                 struct {
121                         uint32_t src_addr; /* IP address of source host. */
122                         uint32_t dst_addr; /* IP address of destination host(s). */
123                         uint8_t  zero;     /* zero. */
124                         uint8_t  proto;    /* L4 protocol type. */
125                         uint16_t len;      /* L4 length. */
126                 } __attribute__((__packed__));
127                 uint16_t u16_arr[0];
128         } psd_hdr;
129
130         psd_hdr.src_addr = ip_hdr->src_addr;
131         psd_hdr.dst_addr = ip_hdr->dst_addr;
132         psd_hdr.zero     = 0;
133         psd_hdr.proto    = ip_hdr->next_proto_id;
134         psd_hdr.len      = rte_cpu_to_be_16((uint16_t)(rte_be_to_cpu_16(ip_hdr->total_length)
135                                 - sizeof(struct ipv4_hdr)));
136         return get_16b_sum(psd_hdr.u16_arr, sizeof(psd_hdr));
137 }
138
139 static inline uint16_t
140 get_ipv6_psd_sum (struct ipv6_hdr * ip_hdr)
141 {
142         /* Pseudo Header for IPv6/UDP/TCP checksum */
143         union ipv6_psd_header {
144                 struct {
145                         uint8_t src_addr[16]; /* IP address of source host. */
146                         uint8_t dst_addr[16]; /* IP address of destination host(s). */
147                         uint32_t len;         /* L4 length. */
148                         uint32_t proto;       /* L4 protocol - top 3 bytes must be zero */
149                 } __attribute__((__packed__));
150
151                 uint16_t u16_arr[0]; /* allow use as 16-bit values with safe aliasing */
152         } psd_hdr;
153
154         rte_memcpy(&psd_hdr.src_addr, ip_hdr->src_addr,
155                         sizeof(ip_hdr->src_addr) + sizeof(ip_hdr->dst_addr));
156         psd_hdr.len       = ip_hdr->payload_len;
157         psd_hdr.proto     = (ip_hdr->proto << 24);
158
159         return get_16b_sum(psd_hdr.u16_arr, sizeof(psd_hdr));
160 }
161
162 static inline uint16_t
163 get_ipv4_udptcp_checksum(struct ipv4_hdr *ipv4_hdr, uint16_t *l4_hdr)
164 {
165         uint32_t cksum;
166         uint32_t l4_len;
167
168         l4_len = rte_be_to_cpu_16(ipv4_hdr->total_length) - sizeof(struct ipv4_hdr);
169
170         cksum = get_16b_sum(l4_hdr, l4_len);
171         cksum += get_ipv4_psd_sum(ipv4_hdr);
172
173         cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
174         cksum = (~cksum) & 0xffff;
175         if (cksum == 0)
176                 cksum = 0xffff;
177         return (uint16_t)cksum;
178
179 }
180
181 static inline uint16_t
182 get_ipv6_udptcp_checksum(struct ipv6_hdr *ipv6_hdr, uint16_t *l4_hdr)
183 {
184         uint32_t cksum;
185         uint32_t l4_len;
186
187         l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
188
189         cksum = get_16b_sum(l4_hdr, l4_len);
190         cksum += get_ipv6_psd_sum(ipv6_hdr);
191
192         cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
193         cksum = (~cksum) & 0xffff;
194         if (cksum == 0)
195                 cksum = 0xffff;
196
197         return (uint16_t)cksum;
198 }
199
200
201 /*
202  * Forwarding of packets. Change the checksum field with HW or SW methods
203  * The HW/SW method selection depends on the ol_flags on every packet
204  */
205 static void
206 pkt_burst_checksum_forward(struct fwd_stream *fs)
207 {
208         struct rte_mbuf  *pkts_burst[MAX_PKT_BURST];
209         struct rte_port  *txp;
210         struct rte_mbuf  *mb;
211         struct ether_hdr *eth_hdr;
212         struct ipv4_hdr  *ipv4_hdr;
213         struct ipv6_hdr  *ipv6_hdr;
214         struct udp_hdr   *udp_hdr;
215         struct tcp_hdr   *tcp_hdr;
216         struct sctp_hdr  *sctp_hdr;
217
218         uint16_t nb_rx;
219         uint16_t nb_tx;
220         uint16_t i;
221         uint16_t ol_flags;
222         uint16_t pkt_ol_flags;
223         uint16_t tx_ol_flags;
224         uint16_t l4_proto;
225         uint16_t eth_type;
226         uint8_t  l2_len;
227         uint8_t  l3_len;
228
229         uint32_t rx_bad_ip_csum;
230         uint32_t rx_bad_l4_csum;
231
232 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
233         uint64_t start_tsc;
234         uint64_t end_tsc;
235         uint64_t core_cycles;
236 #endif
237
238 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
239         start_tsc = rte_rdtsc();
240 #endif
241
242         /*
243          * Receive a burst of packets and forward them.
244          */
245         nb_rx = rte_eth_rx_burst(fs->rx_port, fs->rx_queue, pkts_burst,
246                                  nb_pkt_per_burst);
247         if (unlikely(nb_rx == 0))
248                 return;
249
250 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
251         fs->rx_burst_stats.pkt_burst_spread[nb_rx]++;
252 #endif
253         fs->rx_packets += nb_rx;
254         rx_bad_ip_csum = 0;
255         rx_bad_l4_csum = 0;
256
257         txp = &ports[fs->tx_port];
258         tx_ol_flags = txp->tx_ol_flags;
259
260         for (i = 0; i < nb_rx; i++) {
261
262                 mb = pkts_burst[i];
263                 l2_len  = sizeof(struct ether_hdr);
264                 pkt_ol_flags = mb->ol_flags;
265                 ol_flags = (uint16_t) (pkt_ol_flags & (~PKT_TX_L4_MASK));
266
267                 eth_hdr = (struct ether_hdr *) mb->pkt.data;
268                 eth_type = rte_be_to_cpu_16(eth_hdr->ether_type);
269                 if (eth_type == ETHER_TYPE_VLAN) {
270                         /* Only allow single VLAN label here */
271                         l2_len  += sizeof(struct vlan_hdr);
272                          eth_type = rte_be_to_cpu_16(*(uint16_t *)
273                                 ((uintptr_t)&eth_hdr->ether_type +
274                                 sizeof(struct vlan_hdr)));
275                 }
276
277                 /* Update the L3/L4 checksum error packet count  */
278                 rx_bad_ip_csum += (uint16_t) ((pkt_ol_flags & PKT_RX_IP_CKSUM_BAD) != 0);
279                 rx_bad_l4_csum += (uint16_t) ((pkt_ol_flags & PKT_RX_L4_CKSUM_BAD) != 0);
280
281                 /*
282                  * Try to figure out L3 packet type by SW.
283                  */
284                 if ((pkt_ol_flags & (PKT_RX_IPV4_HDR | PKT_RX_IPV4_HDR_EXT |
285                                 PKT_RX_IPV6_HDR | PKT_RX_IPV6_HDR_EXT)) == 0) {
286                         if (eth_type == ETHER_TYPE_IPv4)
287                                 pkt_ol_flags |= PKT_RX_IPV4_HDR;
288                         else if (eth_type == ETHER_TYPE_IPv6)
289                                 pkt_ol_flags |= PKT_RX_IPV6_HDR;
290                 }
291
292                 /*
293                  * Simplify the protocol parsing
294                  * Assuming the incoming packets format as
295                  *      Ethernet2 + optional single VLAN
296                  *      + ipv4 or ipv6
297                  *      + udp or tcp or sctp or others
298                  */
299                 if (pkt_ol_flags & PKT_RX_IPV4_HDR) {
300
301                         /* Do not support ipv4 option field */
302                         l3_len = sizeof(struct ipv4_hdr) ;
303
304                         ipv4_hdr = (struct ipv4_hdr *) (rte_pktmbuf_mtod(mb,
305                                         unsigned char *) + l2_len);
306
307                         l4_proto = ipv4_hdr->next_proto_id;
308
309                         /* Do not delete, this is required by HW*/
310                         ipv4_hdr->hdr_checksum = 0;
311
312                         if (tx_ol_flags & 0x1) {
313                                 /* HW checksum */
314                                 ol_flags |= PKT_TX_IP_CKSUM;
315                         }
316                         else {
317                                 /* SW checksum calculation */
318                                 ipv4_hdr->src_addr++;
319                                 ipv4_hdr->hdr_checksum = get_ipv4_cksum(ipv4_hdr);
320                         }
321
322                         if (l4_proto == IPPROTO_UDP) {
323                                 udp_hdr = (struct udp_hdr*) (rte_pktmbuf_mtod(mb,
324                                                 unsigned char *) + l2_len + l3_len);
325                                 if (tx_ol_flags & 0x2) {
326                                         /* HW Offload */
327                                         ol_flags |= PKT_TX_UDP_CKSUM;
328                                         /* Pseudo header sum need be set properly */
329                                         udp_hdr->dgram_cksum = get_ipv4_psd_sum(ipv4_hdr);
330                                 }
331                                 else {
332                                         /* SW Implementation, clear checksum field first */
333                                         udp_hdr->dgram_cksum = 0;
334                                         udp_hdr->dgram_cksum = get_ipv4_udptcp_checksum(ipv4_hdr,
335                                                         (uint16_t*)udp_hdr);
336                                 }
337                         }
338                         else if (l4_proto == IPPROTO_TCP){
339                                 tcp_hdr = (struct tcp_hdr*) (rte_pktmbuf_mtod(mb,
340                                                 unsigned char *) + l2_len + l3_len);
341                                 if (tx_ol_flags & 0x4) {
342                                         ol_flags |= PKT_TX_TCP_CKSUM;
343                                         tcp_hdr->cksum = get_ipv4_psd_sum(ipv4_hdr);
344                                 }
345                                 else {
346                                         tcp_hdr->cksum = 0;
347                                         tcp_hdr->cksum = get_ipv4_udptcp_checksum(ipv4_hdr,
348                                                         (uint16_t*)tcp_hdr);
349                                 }
350                         }
351                         else if (l4_proto == IPPROTO_SCTP) {
352                                 sctp_hdr = (struct sctp_hdr*) (rte_pktmbuf_mtod(mb,
353                                                 unsigned char *) + l2_len + l3_len);
354
355                                 if (tx_ol_flags & 0x8) {
356                                         ol_flags |= PKT_TX_SCTP_CKSUM;
357                                         sctp_hdr->cksum = 0;
358
359                                         /* Sanity check, only number of 4 bytes supported */
360                                         if ((rte_be_to_cpu_16(ipv4_hdr->total_length) % 4) != 0)
361                                                 printf("sctp payload must be a multiple "
362                                                         "of 4 bytes for checksum offload");
363                                 }
364                                 else {
365                                         sctp_hdr->cksum = 0;
366                                         /* CRC32c sample code available in RFC3309 */
367                                 }
368                         }
369                         /* End of L4 Handling*/
370                 }
371                 else if (pkt_ol_flags & PKT_RX_IPV6_HDR) {
372
373                         ipv6_hdr = (struct ipv6_hdr *) (rte_pktmbuf_mtod(mb,
374                                         unsigned char *) + l2_len);
375                         l3_len = sizeof(struct ipv6_hdr) ;
376                         l4_proto = ipv6_hdr->proto;
377
378                         if (l4_proto == IPPROTO_UDP) {
379                                 udp_hdr = (struct udp_hdr*) (rte_pktmbuf_mtod(mb,
380                                                 unsigned char *) + l2_len + l3_len);
381                                 if (tx_ol_flags & 0x2) {
382                                         /* HW Offload */
383                                         ol_flags |= PKT_TX_UDP_CKSUM;
384                                         udp_hdr->dgram_cksum = get_ipv6_psd_sum(ipv6_hdr);
385                                 }
386                                 else {
387                                         /* SW Implementation */
388                                         /* checksum field need be clear first */
389                                         udp_hdr->dgram_cksum = 0;
390                                         udp_hdr->dgram_cksum = get_ipv6_udptcp_checksum(ipv6_hdr,
391                                                         (uint16_t*)udp_hdr);
392                                 }
393                         }
394                         else if (l4_proto == IPPROTO_TCP) {
395                                 tcp_hdr = (struct tcp_hdr*) (rte_pktmbuf_mtod(mb,
396                                                 unsigned char *) + l2_len + l3_len);
397                                 if (tx_ol_flags & 0x4) {
398                                         ol_flags |= PKT_TX_TCP_CKSUM;
399                                         tcp_hdr->cksum = get_ipv6_psd_sum(ipv6_hdr);
400                                 }
401                                 else {
402                                         tcp_hdr->cksum = 0;
403                                         tcp_hdr->cksum = get_ipv6_udptcp_checksum(ipv6_hdr,
404                                                         (uint16_t*)tcp_hdr);
405                                 }
406                         }
407                         else if (l4_proto == IPPROTO_SCTP) {
408                                 sctp_hdr = (struct sctp_hdr*) (rte_pktmbuf_mtod(mb,
409                                                 unsigned char *) + l2_len + l3_len);
410
411                                 if (tx_ol_flags & 0x8) {
412                                         ol_flags |= PKT_TX_SCTP_CKSUM;
413                                         sctp_hdr->cksum = 0;
414                                         /* Sanity check, only number of 4 bytes supported by HW */
415                                         if ((rte_be_to_cpu_16(ipv6_hdr->payload_len) % 4) != 0)
416                                                 printf("sctp payload must be a multiple "
417                                                         "of 4 bytes for checksum offload");
418                                 }
419                                 else {
420                                         /* CRC32c sample code available in RFC3309 */
421                                         sctp_hdr->cksum = 0;
422                                 }
423                         } else {
424                                 printf("Test flow control for 1G PMD \n");
425                         }
426                         /* End of L6 Handling*/
427                 }
428                 else {
429                         l3_len = 0;
430                         printf("Unhandled packet type: %#hx\n", eth_type);
431                 }
432
433                 /* Combine the packet header write. VLAN is not consider here */
434                 mb->pkt.vlan_macip.f.l2_len = l2_len;
435                 mb->pkt.vlan_macip.f.l3_len = l3_len;
436                 mb->ol_flags = ol_flags;
437         }
438         nb_tx = rte_eth_tx_burst(fs->tx_port, fs->tx_queue, pkts_burst, nb_rx);
439         fs->tx_packets += nb_tx;
440         fs->rx_bad_ip_csum += rx_bad_ip_csum;
441         fs->rx_bad_l4_csum += rx_bad_l4_csum;
442
443 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
444         fs->tx_burst_stats.pkt_burst_spread[nb_tx]++;
445 #endif
446         if (unlikely(nb_tx < nb_rx)) {
447                 fs->fwd_dropped += (nb_rx - nb_tx);
448                 do {
449                         rte_pktmbuf_free(pkts_burst[nb_tx]);
450                 } while (++nb_tx < nb_rx);
451         }
452 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
453         end_tsc = rte_rdtsc();
454         core_cycles = (end_tsc - start_tsc);
455         fs->core_cycles = (uint64_t) (fs->core_cycles + core_cycles);
456 #endif
457 }
458
459
460 struct fwd_engine csum_fwd_engine = {
461         .fwd_mode_name  = "csum",
462         .port_fwd_begin = NULL,
463         .port_fwd_end   = NULL,
464         .packet_fwd     = pkt_burst_checksum_forward,
465 };
466