Imported Upstream version 16.07-rc1
[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_memzone.h>
53 #include <rte_launch.h>
54 #include <rte_eal.h>
55 #include <rte_per_lcore.h>
56 #include <rte_lcore.h>
57 #include <rte_atomic.h>
58 #include <rte_branch_prediction.h>
59 #include <rte_ring.h>
60 #include <rte_memory.h>
61 #include <rte_mempool.h>
62 #include <rte_mbuf.h>
63 #include <rte_interrupts.h>
64 #include <rte_pci.h>
65 #include <rte_ether.h>
66 #include <rte_ethdev.h>
67 #include <rte_string_fns.h>
68 #include <rte_ip.h>
69 #include <rte_udp.h>
70
71 #include "testpmd.h"
72
73 static inline void
74 print_ether_addr(const char *what, struct ether_addr *eth_addr)
75 {
76         char buf[ETHER_ADDR_FMT_SIZE];
77         ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
78         printf("%s%s", what, buf);
79 }
80
81 /*
82  * Received a burst of packets.
83  */
84 static void
85 pkt_burst_receive(struct fwd_stream *fs)
86 {
87         struct rte_mbuf  *pkts_burst[MAX_PKT_BURST];
88         struct rte_mbuf  *mb;
89         struct ether_hdr *eth_hdr;
90         uint16_t eth_type;
91         uint64_t ol_flags;
92         uint16_t nb_rx;
93         uint16_t i, packet_type;
94         uint16_t is_encapsulation;
95
96 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
97         uint64_t start_tsc;
98         uint64_t end_tsc;
99         uint64_t core_cycles;
100 #endif
101
102 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
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                        (unsigned) 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                 } else if (ol_flags & PKT_RX_FDIR) {
148                         printf(" - FDIR matched ");
149                         if (ol_flags & PKT_RX_FDIR_ID)
150                                 printf("ID=0x%x",
151                                        mb->hash.fdir.hi);
152                         else if (ol_flags & PKT_RX_FDIR_FLX)
153                                 printf("flex bytes=0x%08x %08x",
154                                        mb->hash.fdir.hi, mb->hash.fdir.lo);
155                         else
156                                 printf("hash=0x%x ID=0x%x ",
157                                        mb->hash.fdir.hash, mb->hash.fdir.id);
158                 }
159                 if (ol_flags & PKT_RX_VLAN_STRIPPED)
160                         printf(" - VLAN tci=0x%x", mb->vlan_tci);
161                 if (ol_flags & PKT_RX_QINQ_STRIPPED)
162                         printf(" - QinQ VLAN tci=0x%x, VLAN tci outer=0x%x",
163                                         mb->vlan_tci, mb->vlan_tci_outer);
164                 if (mb->packet_type) {
165                         uint32_t ptype;
166
167                         /* (outer) L2 packet type */
168                         ptype = mb->packet_type & RTE_PTYPE_L2_MASK;
169                         switch (ptype) {
170                         case RTE_PTYPE_L2_ETHER:
171                                 printf(" - (outer) L2 type: ETHER");
172                                 break;
173                         case RTE_PTYPE_L2_ETHER_TIMESYNC:
174                                 printf(" - (outer) L2 type: ETHER_Timesync");
175                                 break;
176                         case RTE_PTYPE_L2_ETHER_ARP:
177                                 printf(" - (outer) L2 type: ETHER_ARP");
178                                 break;
179                         case RTE_PTYPE_L2_ETHER_LLDP:
180                                 printf(" - (outer) L2 type: ETHER_LLDP");
181                                 break;
182                         case RTE_PTYPE_L2_ETHER_NSH:
183                                 printf(" - (outer) L2 type: ETHER_NSH");
184                                 break;
185                         default:
186                                 printf(" - (outer) L2 type: Unknown");
187                                 break;
188                         }
189
190                         /* (outer) L3 packet type */
191                         ptype = mb->packet_type & RTE_PTYPE_L3_MASK;
192                         switch (ptype) {
193                         case RTE_PTYPE_L3_IPV4:
194                                 printf(" - (outer) L3 type: IPV4");
195                                 break;
196                         case RTE_PTYPE_L3_IPV4_EXT:
197                                 printf(" - (outer) L3 type: IPV4_EXT");
198                                 break;
199                         case RTE_PTYPE_L3_IPV6:
200                                 printf(" - (outer) L3 type: IPV6");
201                                 break;
202                         case RTE_PTYPE_L3_IPV4_EXT_UNKNOWN:
203                                 printf(" - (outer) L3 type: IPV4_EXT_UNKNOWN");
204                                 break;
205                         case RTE_PTYPE_L3_IPV6_EXT:
206                                 printf(" - (outer) L3 type: IPV6_EXT");
207                                 break;
208                         case RTE_PTYPE_L3_IPV6_EXT_UNKNOWN:
209                                 printf(" - (outer) L3 type: IPV6_EXT_UNKNOWN");
210                                 break;
211                         default:
212                                 printf(" - (outer) L3 type: Unknown");
213                                 break;
214                         }
215
216                         /* (outer) L4 packet type */
217                         ptype = mb->packet_type & RTE_PTYPE_L4_MASK;
218                         switch (ptype) {
219                         case RTE_PTYPE_L4_TCP:
220                                 printf(" - (outer) L4 type: TCP");
221                                 break;
222                         case RTE_PTYPE_L4_UDP:
223                                 printf(" - (outer) L4 type: UDP");
224                                 break;
225                         case RTE_PTYPE_L4_FRAG:
226                                 printf(" - (outer) L4 type: L4_FRAG");
227                                 break;
228                         case RTE_PTYPE_L4_SCTP:
229                                 printf(" - (outer) L4 type: SCTP");
230                                 break;
231                         case RTE_PTYPE_L4_ICMP:
232                                 printf(" - (outer) L4 type: ICMP");
233                                 break;
234                         case RTE_PTYPE_L4_NONFRAG:
235                                 printf(" - (outer) L4 type: L4_NONFRAG");
236                                 break;
237                         default:
238                                 printf(" - (outer) L4 type: Unknown");
239                                 break;
240                         }
241
242                         /* packet tunnel type */
243                         ptype = mb->packet_type & RTE_PTYPE_TUNNEL_MASK;
244                         switch (ptype) {
245                         case RTE_PTYPE_TUNNEL_IP:
246                                 printf(" - Tunnel type: IP");
247                                 break;
248                         case RTE_PTYPE_TUNNEL_GRE:
249                                 printf(" - Tunnel type: GRE");
250                                 break;
251                         case RTE_PTYPE_TUNNEL_VXLAN:
252                                 printf(" - Tunnel type: VXLAN");
253                                 break;
254                         case RTE_PTYPE_TUNNEL_NVGRE:
255                                 printf(" - Tunnel type: NVGRE");
256                                 break;
257                         case RTE_PTYPE_TUNNEL_GENEVE:
258                                 printf(" - Tunnel type: GENEVE");
259                                 break;
260                         case RTE_PTYPE_TUNNEL_GRENAT:
261                                 printf(" - Tunnel type: GRENAT");
262                                 break;
263                         default:
264                                 printf(" - Tunnel type: Unknown");
265                                 break;
266                         }
267
268                         /* inner L2 packet type */
269                         ptype = mb->packet_type & RTE_PTYPE_INNER_L2_MASK;
270                         switch (ptype) {
271                         case RTE_PTYPE_INNER_L2_ETHER:
272                                 printf(" - Inner L2 type: ETHER");
273                                 break;
274                         case RTE_PTYPE_INNER_L2_ETHER_VLAN:
275                                 printf(" - Inner L2 type: ETHER_VLAN");
276                                 break;
277                         default:
278                                 printf(" - Inner L2 type: Unknown");
279                                 break;
280                         }
281
282                         /* inner L3 packet type */
283                         ptype = mb->packet_type & RTE_PTYPE_INNER_L3_MASK;
284                         switch (ptype) {
285                         case RTE_PTYPE_INNER_L3_IPV4:
286                                 printf(" - Inner L3 type: IPV4");
287                                 break;
288                         case RTE_PTYPE_INNER_L3_IPV4_EXT:
289                                 printf(" - Inner L3 type: IPV4_EXT");
290                                 break;
291                         case RTE_PTYPE_INNER_L3_IPV6:
292                                 printf(" - Inner L3 type: IPV6");
293                                 break;
294                         case RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN:
295                                 printf(" - Inner L3 type: IPV4_EXT_UNKNOWN");
296                                 break;
297                         case RTE_PTYPE_INNER_L3_IPV6_EXT:
298                                 printf(" - Inner L3 type: IPV6_EXT");
299                                 break;
300                         case RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN:
301                                 printf(" - Inner L3 type: IPV6_EXT_UNKNOWN");
302                                 break;
303                         default:
304                                 printf(" - Inner L3 type: Unknown");
305                                 break;
306                         }
307
308                         /* inner L4 packet type */
309                         ptype = mb->packet_type & RTE_PTYPE_INNER_L4_MASK;
310                         switch (ptype) {
311                         case RTE_PTYPE_INNER_L4_TCP:
312                                 printf(" - Inner L4 type: TCP");
313                                 break;
314                         case RTE_PTYPE_INNER_L4_UDP:
315                                 printf(" - Inner L4 type: UDP");
316                                 break;
317                         case RTE_PTYPE_INNER_L4_FRAG:
318                                 printf(" - Inner L4 type: L4_FRAG");
319                                 break;
320                         case RTE_PTYPE_INNER_L4_SCTP:
321                                 printf(" - Inner L4 type: SCTP");
322                                 break;
323                         case RTE_PTYPE_INNER_L4_ICMP:
324                                 printf(" - Inner L4 type: ICMP");
325                                 break;
326                         case RTE_PTYPE_INNER_L4_NONFRAG:
327                                 printf(" - Inner L4 type: L4_NONFRAG");
328                                 break;
329                         default:
330                                 printf(" - Inner L4 type: Unknown");
331                                 break;
332                         }
333                         printf("\n");
334                 } else
335                         printf("Unknown packet type\n");
336                 if (is_encapsulation) {
337                         struct ipv4_hdr *ipv4_hdr;
338                         struct ipv6_hdr *ipv6_hdr;
339                         struct udp_hdr *udp_hdr;
340                         uint8_t l2_len;
341                         uint8_t l3_len;
342                         uint8_t l4_len;
343                         uint8_t l4_proto;
344                         struct  vxlan_hdr *vxlan_hdr;
345
346                         l2_len  = sizeof(struct ether_hdr);
347
348                          /* Do not support ipv4 option field */
349                         if (RTE_ETH_IS_IPV4_HDR(packet_type)) {
350                                 l3_len = sizeof(struct ipv4_hdr);
351                                 ipv4_hdr = rte_pktmbuf_mtod_offset(mb,
352                                                                    struct ipv4_hdr *,
353                                                                    l2_len);
354                                 l4_proto = ipv4_hdr->next_proto_id;
355                         } else {
356                                 l3_len = sizeof(struct ipv6_hdr);
357                                 ipv6_hdr = rte_pktmbuf_mtod_offset(mb,
358                                                                    struct ipv6_hdr *,
359                                                                    l2_len);
360                                 l4_proto = ipv6_hdr->proto;
361                         }
362                         if (l4_proto == IPPROTO_UDP) {
363                                 udp_hdr = rte_pktmbuf_mtod_offset(mb,
364                                                                   struct udp_hdr *,
365                                                                   l2_len + l3_len);
366                                 l4_len = sizeof(struct udp_hdr);
367                                 vxlan_hdr = rte_pktmbuf_mtod_offset(mb,
368                                                                     struct vxlan_hdr *,
369                                                                     l2_len + l3_len + l4_len);
370
371                                 printf(" - VXLAN packet: packet type =%d, "
372                                         "Destination UDP port =%d, VNI = %d",
373                                         packet_type, RTE_BE_TO_CPU_16(udp_hdr->dst_port),
374                                         rte_be_to_cpu_32(vxlan_hdr->vx_vni) >> 8);
375                         }
376                 }
377                 printf(" - Receive queue=0x%x", (unsigned) fs->rx_queue);
378                 printf("\n");
379                 if (ol_flags != 0) {
380                         unsigned rxf;
381                         const char *name;
382
383                         for (rxf = 0; rxf < sizeof(mb->ol_flags) * 8; rxf++) {
384                                 if ((ol_flags & (1ULL << rxf)) == 0)
385                                         continue;
386                                 name = rte_get_rx_ol_flag_name(1ULL << rxf);
387                                 if (name == NULL)
388                                         continue;
389                                 printf("  %s\n", name);
390                         }
391                 }
392                 rte_pktmbuf_free(mb);
393         }
394
395 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
396         end_tsc = rte_rdtsc();
397         core_cycles = (end_tsc - start_tsc);
398         fs->core_cycles = (uint64_t) (fs->core_cycles + core_cycles);
399 #endif
400 }
401
402 struct fwd_engine rx_only_engine = {
403         .fwd_mode_name  = "rxonly",
404         .port_fwd_begin = NULL,
405         .port_fwd_end   = NULL,
406         .packet_fwd     = pkt_burst_receive,
407 };