New upstream version 17.11-rc3
[deb_dpdk.git] / app / test-pmd / rxonly.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_launch.h>
53 #include <rte_eal.h>
54 #include <rte_per_lcore.h>
55 #include <rte_lcore.h>
56 #include <rte_atomic.h>
57 #include <rte_branch_prediction.h>
58 #include <rte_mempool.h>
59 #include <rte_mbuf.h>
60 #include <rte_interrupts.h>
61 #include <rte_pci.h>
62 #include <rte_ether.h>
63 #include <rte_ethdev.h>
64 #include <rte_string_fns.h>
65 #include <rte_ip.h>
66 #include <rte_udp.h>
67 #include <rte_net.h>
68 #include <rte_flow.h>
69
70 #include "testpmd.h"
71
72 static inline void
73 print_ether_addr(const char *what, struct ether_addr *eth_addr)
74 {
75         char buf[ETHER_ADDR_FMT_SIZE];
76         ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
77         printf("%s%s", what, buf);
78 }
79
80 /*
81  * Received a burst of packets.
82  */
83 static void
84 pkt_burst_receive(struct fwd_stream *fs)
85 {
86         struct rte_mbuf  *pkts_burst[MAX_PKT_BURST];
87         struct rte_mbuf  *mb;
88         struct ether_hdr *eth_hdr;
89         uint16_t eth_type;
90         uint64_t ol_flags;
91         uint16_t nb_rx;
92         uint16_t i, packet_type;
93         uint16_t is_encapsulation;
94         char buf[256];
95         struct rte_net_hdr_lens hdr_lens;
96         uint32_t sw_packet_type;
97
98 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
99         uint64_t start_tsc;
100         uint64_t end_tsc;
101         uint64_t core_cycles;
102
103         start_tsc = rte_rdtsc();
104 #endif
105
106         /*
107          * Receive a burst of packets.
108          */
109         nb_rx = rte_eth_rx_burst(fs->rx_port, fs->rx_queue, pkts_burst,
110                                  nb_pkt_per_burst);
111         if (unlikely(nb_rx == 0))
112                 return;
113
114 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
115         fs->rx_burst_stats.pkt_burst_spread[nb_rx]++;
116 #endif
117         fs->rx_packets += nb_rx;
118
119         /*
120          * Dump each received packet if verbose_level > 0.
121          */
122         if (verbose_level > 0)
123                 printf("port %u/queue %u: received %u packets\n",
124                        fs->rx_port,
125                        (unsigned) fs->rx_queue,
126                        (unsigned) nb_rx);
127         for (i = 0; i < nb_rx; i++) {
128                 mb = pkts_burst[i];
129                 if (verbose_level == 0) {
130                         rte_pktmbuf_free(mb);
131                         continue;
132                 }
133                 eth_hdr = rte_pktmbuf_mtod(mb, struct ether_hdr *);
134                 eth_type = RTE_BE_TO_CPU_16(eth_hdr->ether_type);
135                 ol_flags = mb->ol_flags;
136                 packet_type = mb->packet_type;
137                 is_encapsulation = RTE_ETH_IS_TUNNEL_PKT(packet_type);
138
139                 print_ether_addr("  src=", &eth_hdr->s_addr);
140                 print_ether_addr(" - dst=", &eth_hdr->d_addr);
141                 printf(" - type=0x%04x - length=%u - nb_segs=%d",
142                        eth_type, (unsigned) mb->pkt_len,
143                        (int)mb->nb_segs);
144                 if (ol_flags & PKT_RX_RSS_HASH) {
145                         printf(" - RSS hash=0x%x", (unsigned) mb->hash.rss);
146                         printf(" - RSS queue=0x%x",(unsigned) fs->rx_queue);
147                 }
148                 if (ol_flags & PKT_RX_FDIR) {
149                         printf(" - FDIR matched ");
150                         if (ol_flags & PKT_RX_FDIR_ID)
151                                 printf("ID=0x%x",
152                                        mb->hash.fdir.hi);
153                         else if (ol_flags & PKT_RX_FDIR_FLX)
154                                 printf("flex bytes=0x%08x %08x",
155                                        mb->hash.fdir.hi, mb->hash.fdir.lo);
156                         else
157                                 printf("hash=0x%x ID=0x%x ",
158                                        mb->hash.fdir.hash, mb->hash.fdir.id);
159                 }
160                 if (ol_flags & PKT_RX_TIMESTAMP)
161                         printf(" - timestamp %"PRIu64" ", mb->timestamp);
162                 if (ol_flags & PKT_RX_VLAN_STRIPPED)
163                         printf(" - VLAN tci=0x%x", mb->vlan_tci);
164                 if (ol_flags & PKT_RX_QINQ_STRIPPED)
165                         printf(" - QinQ VLAN tci=0x%x, VLAN tci outer=0x%x",
166                                         mb->vlan_tci, mb->vlan_tci_outer);
167                 if (mb->packet_type) {
168                         rte_get_ptype_name(mb->packet_type, buf, sizeof(buf));
169                         printf(" - hw ptype: %s", buf);
170                 }
171                 sw_packet_type = rte_net_get_ptype(mb, &hdr_lens,
172                         RTE_PTYPE_ALL_MASK);
173                 rte_get_ptype_name(sw_packet_type, buf, sizeof(buf));
174                 printf(" - sw ptype: %s", buf);
175                 if (sw_packet_type & RTE_PTYPE_L2_MASK)
176                         printf(" - l2_len=%d", hdr_lens.l2_len);
177                 if (sw_packet_type & RTE_PTYPE_L3_MASK)
178                         printf(" - l3_len=%d", hdr_lens.l3_len);
179                 if (sw_packet_type & RTE_PTYPE_L4_MASK)
180                         printf(" - l4_len=%d", hdr_lens.l4_len);
181                 if (sw_packet_type & RTE_PTYPE_TUNNEL_MASK)
182                         printf(" - tunnel_len=%d", hdr_lens.tunnel_len);
183                 if (sw_packet_type & RTE_PTYPE_INNER_L2_MASK)
184                         printf(" - inner_l2_len=%d", hdr_lens.inner_l2_len);
185                 if (sw_packet_type & RTE_PTYPE_INNER_L3_MASK)
186                         printf(" - inner_l3_len=%d", hdr_lens.inner_l3_len);
187                 if (sw_packet_type & RTE_PTYPE_INNER_L4_MASK)
188                         printf(" - inner_l4_len=%d", hdr_lens.inner_l4_len);
189                 if (is_encapsulation) {
190                         struct ipv4_hdr *ipv4_hdr;
191                         struct ipv6_hdr *ipv6_hdr;
192                         struct udp_hdr *udp_hdr;
193                         uint8_t l2_len;
194                         uint8_t l3_len;
195                         uint8_t l4_len;
196                         uint8_t l4_proto;
197                         struct  vxlan_hdr *vxlan_hdr;
198
199                         l2_len  = sizeof(struct ether_hdr);
200
201                          /* Do not support ipv4 option field */
202                         if (RTE_ETH_IS_IPV4_HDR(packet_type)) {
203                                 l3_len = sizeof(struct ipv4_hdr);
204                                 ipv4_hdr = rte_pktmbuf_mtod_offset(mb,
205                                                                    struct ipv4_hdr *,
206                                                                    l2_len);
207                                 l4_proto = ipv4_hdr->next_proto_id;
208                         } else {
209                                 l3_len = sizeof(struct ipv6_hdr);
210                                 ipv6_hdr = rte_pktmbuf_mtod_offset(mb,
211                                                                    struct ipv6_hdr *,
212                                                                    l2_len);
213                                 l4_proto = ipv6_hdr->proto;
214                         }
215                         if (l4_proto == IPPROTO_UDP) {
216                                 udp_hdr = rte_pktmbuf_mtod_offset(mb,
217                                                                   struct udp_hdr *,
218                                                                   l2_len + l3_len);
219                                 l4_len = sizeof(struct udp_hdr);
220                                 vxlan_hdr = rte_pktmbuf_mtod_offset(mb,
221                                                                     struct vxlan_hdr *,
222                                                                     l2_len + l3_len + l4_len);
223
224                                 printf(" - VXLAN packet: packet type =%d, "
225                                         "Destination UDP port =%d, VNI = %d",
226                                         packet_type, RTE_BE_TO_CPU_16(udp_hdr->dst_port),
227                                         rte_be_to_cpu_32(vxlan_hdr->vx_vni) >> 8);
228                         }
229                 }
230                 printf(" - Receive queue=0x%x", (unsigned) fs->rx_queue);
231                 printf("\n");
232                 rte_get_rx_ol_flag_list(mb->ol_flags, buf, sizeof(buf));
233                 printf("  ol_flags: %s\n", buf);
234                 rte_pktmbuf_free(mb);
235         }
236
237 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
238         end_tsc = rte_rdtsc();
239         core_cycles = (end_tsc - start_tsc);
240         fs->core_cycles = (uint64_t) (fs->core_cycles + core_cycles);
241 #endif
242 }
243
244 struct fwd_engine rx_only_engine = {
245         .fwd_mode_name  = "rxonly",
246         .port_fwd_begin = NULL,
247         .port_fwd_end   = NULL,
248         .packet_fwd     = pkt_burst_receive,
249 };