first public release
[dpdk.git] / app / test-pmd / rxonly.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_interrupts.h>
67 #include <rte_pci.h>
68 #include <rte_ether.h>
69 #include <rte_ethdev.h>
70 #include <rte_string_fns.h>
71
72 #include "testpmd.h"
73
74 #define MAX_PKT_RX_FLAGS 11
75 static const char *pkt_rx_flag_names[MAX_PKT_RX_FLAGS] = {
76         "VLAN_PKT",
77         "RSS_HASH",
78         "PKT_RX_FDIR",
79         "IP_CKSUM",
80         "IP_CKSUM_BAD",
81
82         "IPV4_HDR",
83         "IPV4_HDR_EXT",
84         "IPV6_HDR",
85         "IPV6_HDR_EXT",
86
87         "IEEE1588_PTP",
88         "IEEE1588_TMST",
89 };
90
91 static inline void
92 print_ether_addr(const char *what, struct ether_addr *eth_addr)
93 {
94         printf("%s%02X:%02X:%02X:%02X:%02X:%02X",
95                what,
96                eth_addr->addr_bytes[0],
97                eth_addr->addr_bytes[1],
98                eth_addr->addr_bytes[2],
99                eth_addr->addr_bytes[3],
100                eth_addr->addr_bytes[4],
101                eth_addr->addr_bytes[5]);
102 }
103
104 /*
105  * Received a burst of packets.
106  */
107 static void
108 pkt_burst_receive(struct fwd_stream *fs)
109 {
110         struct rte_mbuf  *pkts_burst[MAX_PKT_BURST];
111         struct rte_mbuf  *mb;
112         struct ether_hdr *eth_hdr;
113         uint16_t eth_type;
114         uint16_t ol_flags;
115         uint16_t nb_rx;
116         uint16_t i;
117 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
118         uint64_t start_tsc;
119         uint64_t end_tsc;
120         uint64_t core_cycles;
121 #endif
122
123 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
124         start_tsc = rte_rdtsc();
125 #endif
126
127         /*
128          * Receive a burst of packets.
129          */
130         nb_rx = rte_eth_rx_burst(fs->rx_port, fs->rx_queue, pkts_burst,
131                                  nb_pkt_per_burst);
132         if (unlikely(nb_rx == 0))
133                 return;
134
135 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
136         fs->rx_burst_stats.pkt_burst_spread[nb_rx]++;
137 #endif
138         fs->rx_packets += nb_rx;
139
140         /*
141          * Dump each received packet if verbose_level > 0.
142          */
143         if (verbose_level > 0)
144                 printf("port %u/queue %u: received %u packets\n",
145                        (unsigned) fs->rx_port,
146                        (unsigned) fs->rx_queue,
147                        (unsigned) nb_rx);
148         for (i = 0; i < nb_rx; i++) {
149                 mb = pkts_burst[i];
150                 if (verbose_level == 0) {
151                         rte_pktmbuf_free(mb);
152                         continue;
153                 }
154                 eth_hdr = (struct ether_hdr *) mb->pkt.data;
155                 eth_type = RTE_BE_TO_CPU_16(eth_hdr->ether_type);
156                 ol_flags = mb->ol_flags;
157                 print_ether_addr("  src=", &eth_hdr->s_addr);
158                 print_ether_addr(" - dst=", &eth_hdr->d_addr);
159                 printf(" - type=0x%04x - length=%u - nb_segs=%d",
160                        eth_type, (unsigned) mb->pkt.pkt_len,
161                        (int)mb->pkt.nb_segs);
162                 if (ol_flags & PKT_RX_RSS_HASH)
163                         printf(" - RSS hash=0x%x", (unsigned) mb->pkt.hash.rss);
164                 else if (ol_flags & PKT_RX_FDIR)
165                         printf(" - FDIR hash=0x%x - FDIR id=0x%x ",
166                                mb->pkt.hash.fdir.hash, mb->pkt.hash.fdir.id);
167                 if (ol_flags & PKT_RX_VLAN_PKT)
168                         printf(" - VLAN tci=0x%x", mb->pkt.vlan_tci);
169                 printf("\n");
170                 if (ol_flags != 0) {
171                         int rxf;
172
173                         for (rxf = 0; rxf < MAX_PKT_RX_FLAGS; rxf++) {
174                                 if (ol_flags & (1 << rxf))
175                                         printf("  PKT_RX_%s\n",
176                                                pkt_rx_flag_names[rxf]);
177                         }
178                 }
179                 rte_pktmbuf_free(mb);
180         }
181
182 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
183         end_tsc = rte_rdtsc();
184         core_cycles = (end_tsc - start_tsc);
185         fs->core_cycles = (uint64_t) (fs->core_cycles + core_cycles);
186 #endif
187 }
188
189 struct fwd_engine rx_only_engine = {
190         .fwd_mode_name  = "rxonly",
191         .port_fwd_begin = NULL,
192         .port_fwd_end   = NULL,
193         .packet_fwd     = pkt_burst_receive,
194 };