mbuf: fix tunnel flags check
[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 #ifdef RTE_NEXT_ABI
95         uint16_t is_encapsulation;
96 #else
97         uint64_t is_encapsulation;
98 #endif
99
100 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
101         uint64_t start_tsc;
102         uint64_t end_tsc;
103         uint64_t core_cycles;
104 #endif
105
106 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
107         start_tsc = rte_rdtsc();
108 #endif
109
110         /*
111          * Receive a burst of packets.
112          */
113         nb_rx = rte_eth_rx_burst(fs->rx_port, fs->rx_queue, pkts_burst,
114                                  nb_pkt_per_burst);
115         if (unlikely(nb_rx == 0))
116                 return;
117
118 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
119         fs->rx_burst_stats.pkt_burst_spread[nb_rx]++;
120 #endif
121         fs->rx_packets += nb_rx;
122
123         /*
124          * Dump each received packet if verbose_level > 0.
125          */
126         if (verbose_level > 0)
127                 printf("port %u/queue %u: received %u packets\n",
128                        (unsigned) fs->rx_port,
129                        (unsigned) fs->rx_queue,
130                        (unsigned) nb_rx);
131         for (i = 0; i < nb_rx; i++) {
132                 mb = pkts_burst[i];
133                 if (verbose_level == 0) {
134                         rte_pktmbuf_free(mb);
135                         continue;
136                 }
137                 eth_hdr = rte_pktmbuf_mtod(mb, struct ether_hdr *);
138                 eth_type = RTE_BE_TO_CPU_16(eth_hdr->ether_type);
139                 ol_flags = mb->ol_flags;
140                 packet_type = mb->packet_type;
141
142 #ifdef RTE_NEXT_ABI
143                 is_encapsulation = RTE_ETH_IS_TUNNEL_PKT(packet_type);
144 #else
145                 is_encapsulation = ol_flags & (PKT_RX_TUNNEL_IPV4_HDR |
146                                 PKT_RX_TUNNEL_IPV6_HDR);
147 #endif
148
149                 print_ether_addr("  src=", &eth_hdr->s_addr);
150                 print_ether_addr(" - dst=", &eth_hdr->d_addr);
151                 printf(" - type=0x%04x - length=%u - nb_segs=%d",
152                        eth_type, (unsigned) mb->pkt_len,
153                        (int)mb->nb_segs);
154                 if (ol_flags & PKT_RX_RSS_HASH) {
155                         printf(" - RSS hash=0x%x", (unsigned) mb->hash.rss);
156                         printf(" - RSS queue=0x%x",(unsigned) fs->rx_queue);
157                 } else if (ol_flags & PKT_RX_FDIR) {
158                         printf(" - FDIR matched ");
159                         if (ol_flags & PKT_RX_FDIR_ID)
160                                 printf("ID=0x%x",
161                                        mb->hash.fdir.hi);
162                         else if (ol_flags & PKT_RX_FDIR_FLX)
163                                 printf("flex bytes=0x%08x %08x",
164                                        mb->hash.fdir.hi, mb->hash.fdir.lo);
165                         else
166                                 printf("hash=0x%x ID=0x%x ",
167                                        mb->hash.fdir.hash, mb->hash.fdir.id);
168                 }
169                 if (ol_flags & PKT_RX_VLAN_PKT)
170                         printf(" - VLAN tci=0x%x", mb->vlan_tci);
171                 if (ol_flags & PKT_RX_QINQ_PKT)
172                         printf(" - QinQ VLAN tci=0x%x, VLAN tci outer=0x%x",
173                                         mb->vlan_tci, mb->vlan_tci_outer);
174 #ifdef RTE_NEXT_ABI
175                 if (mb->packet_type) {
176                         uint32_t ptype;
177
178                         /* (outer) L2 packet type */
179                         ptype = mb->packet_type & RTE_PTYPE_L2_MASK;
180                         switch (ptype) {
181                         case RTE_PTYPE_L2_ETHER:
182                                 printf(" - (outer) L2 type: ETHER");
183                                 break;
184                         case RTE_PTYPE_L2_ETHER_TIMESYNC:
185                                 printf(" - (outer) L2 type: ETHER_Timesync");
186                                 break;
187                         case RTE_PTYPE_L2_ETHER_ARP:
188                                 printf(" - (outer) L2 type: ETHER_ARP");
189                                 break;
190                         case RTE_PTYPE_L2_ETHER_LLDP:
191                                 printf(" - (outer) L2 type: ETHER_LLDP");
192                                 break;
193                         default:
194                                 printf(" - (outer) L2 type: Unknown");
195                                 break;
196                         }
197
198                         /* (outer) L3 packet type */
199                         ptype = mb->packet_type & RTE_PTYPE_L3_MASK;
200                         switch (ptype) {
201                         case RTE_PTYPE_L3_IPV4:
202                                 printf(" - (outer) L3 type: IPV4");
203                                 break;
204                         case RTE_PTYPE_L3_IPV4_EXT:
205                                 printf(" - (outer) L3 type: IPV4_EXT");
206                                 break;
207                         case RTE_PTYPE_L3_IPV6:
208                                 printf(" - (outer) L3 type: IPV6");
209                                 break;
210                         case RTE_PTYPE_L3_IPV4_EXT_UNKNOWN:
211                                 printf(" - (outer) L3 type: IPV4_EXT_UNKNOWN");
212                                 break;
213                         case RTE_PTYPE_L3_IPV6_EXT:
214                                 printf(" - (outer) L3 type: IPV6_EXT");
215                                 break;
216                         case RTE_PTYPE_L3_IPV6_EXT_UNKNOWN:
217                                 printf(" - (outer) L3 type: IPV6_EXT_UNKNOWN");
218                                 break;
219                         default:
220                                 printf(" - (outer) L3 type: Unknown");
221                                 break;
222                         }
223
224                         /* (outer) L4 packet type */
225                         ptype = mb->packet_type & RTE_PTYPE_L4_MASK;
226                         switch (ptype) {
227                         case RTE_PTYPE_L4_TCP:
228                                 printf(" - (outer) L4 type: TCP");
229                                 break;
230                         case RTE_PTYPE_L4_UDP:
231                                 printf(" - (outer) L4 type: UDP");
232                                 break;
233                         case RTE_PTYPE_L4_FRAG:
234                                 printf(" - (outer) L4 type: L4_FRAG");
235                                 break;
236                         case RTE_PTYPE_L4_SCTP:
237                                 printf(" - (outer) L4 type: SCTP");
238                                 break;
239                         case RTE_PTYPE_L4_ICMP:
240                                 printf(" - (outer) L4 type: ICMP");
241                                 break;
242                         case RTE_PTYPE_L4_NONFRAG:
243                                 printf(" - (outer) L4 type: L4_NONFRAG");
244                                 break;
245                         default:
246                                 printf(" - (outer) L4 type: Unknown");
247                                 break;
248                         }
249
250                         /* packet tunnel type */
251                         ptype = mb->packet_type & RTE_PTYPE_TUNNEL_MASK;
252                         switch (ptype) {
253                         case RTE_PTYPE_TUNNEL_IP:
254                                 printf(" - Tunnel type: IP");
255                                 break;
256                         case RTE_PTYPE_TUNNEL_GRE:
257                                 printf(" - Tunnel type: GRE");
258                                 break;
259                         case RTE_PTYPE_TUNNEL_VXLAN:
260                                 printf(" - Tunnel type: VXLAN");
261                                 break;
262                         case RTE_PTYPE_TUNNEL_NVGRE:
263                                 printf(" - Tunnel type: NVGRE");
264                                 break;
265                         case RTE_PTYPE_TUNNEL_GENEVE:
266                                 printf(" - Tunnel type: GENEVE");
267                                 break;
268                         case RTE_PTYPE_TUNNEL_GRENAT:
269                                 printf(" - Tunnel type: GRENAT");
270                                 break;
271                         default:
272                                 printf(" - Tunnel type: Unknown");
273                                 break;
274                         }
275
276                         /* inner L2 packet type */
277                         ptype = mb->packet_type & RTE_PTYPE_INNER_L2_MASK;
278                         switch (ptype) {
279                         case RTE_PTYPE_INNER_L2_ETHER:
280                                 printf(" - Inner L2 type: ETHER");
281                                 break;
282                         case RTE_PTYPE_INNER_L2_ETHER_VLAN:
283                                 printf(" - Inner L2 type: ETHER_VLAN");
284                                 break;
285                         default:
286                                 printf(" - Inner L2 type: Unknown");
287                                 break;
288                         }
289
290                         /* inner L3 packet type */
291                         ptype = mb->packet_type & RTE_PTYPE_INNER_L3_MASK;
292                         switch (ptype) {
293                         case RTE_PTYPE_INNER_L3_IPV4:
294                                 printf(" - Inner L3 type: IPV4");
295                                 break;
296                         case RTE_PTYPE_INNER_L3_IPV4_EXT:
297                                 printf(" - Inner L3 type: IPV4_EXT");
298                                 break;
299                         case RTE_PTYPE_INNER_L3_IPV6:
300                                 printf(" - Inner L3 type: IPV6");
301                                 break;
302                         case RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN:
303                                 printf(" - Inner L3 type: IPV4_EXT_UNKNOWN");
304                                 break;
305                         case RTE_PTYPE_INNER_L3_IPV6_EXT:
306                                 printf(" - Inner L3 type: IPV6_EXT");
307                                 break;
308                         case RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN:
309                                 printf(" - Inner L3 type: IPV6_EXT_UNKNOWN");
310                                 break;
311                         default:
312                                 printf(" - Inner L3 type: Unknown");
313                                 break;
314                         }
315
316                         /* inner L4 packet type */
317                         ptype = mb->packet_type & RTE_PTYPE_INNER_L4_MASK;
318                         switch (ptype) {
319                         case RTE_PTYPE_INNER_L4_TCP:
320                                 printf(" - Inner L4 type: TCP");
321                                 break;
322                         case RTE_PTYPE_INNER_L4_UDP:
323                                 printf(" - Inner L4 type: UDP");
324                                 break;
325                         case RTE_PTYPE_INNER_L4_FRAG:
326                                 printf(" - Inner L4 type: L4_FRAG");
327                                 break;
328                         case RTE_PTYPE_INNER_L4_SCTP:
329                                 printf(" - Inner L4 type: SCTP");
330                                 break;
331                         case RTE_PTYPE_INNER_L4_ICMP:
332                                 printf(" - Inner L4 type: ICMP");
333                                 break;
334                         case RTE_PTYPE_INNER_L4_NONFRAG:
335                                 printf(" - Inner L4 type: L4_NONFRAG");
336                                 break;
337                         default:
338                                 printf(" - Inner L4 type: Unknown");
339                                 break;
340                         }
341                         printf("\n");
342                 } else
343                         printf("Unknown packet type\n");
344 #endif /* RTE_NEXT_ABI */
345                 if (is_encapsulation) {
346                         struct ipv4_hdr *ipv4_hdr;
347                         struct ipv6_hdr *ipv6_hdr;
348                         struct udp_hdr *udp_hdr;
349                         uint8_t l2_len;
350                         uint8_t l3_len;
351                         uint8_t l4_len;
352                         uint8_t l4_proto;
353                         struct  vxlan_hdr *vxlan_hdr;
354
355                         l2_len  = sizeof(struct ether_hdr);
356
357                          /* Do not support ipv4 option field */
358 #ifdef RTE_NEXT_ABI
359                         if (RTE_ETH_IS_IPV4_HDR(packet_type)) {
360 #else
361                         if (ol_flags & PKT_RX_TUNNEL_IPV4_HDR) {
362 #endif
363                                 l3_len = sizeof(struct ipv4_hdr);
364                                 ipv4_hdr = rte_pktmbuf_mtod_offset(mb,
365                                                                    struct ipv4_hdr *,
366                                                                    l2_len);
367                                 l4_proto = ipv4_hdr->next_proto_id;
368                         } else {
369                                 l3_len = sizeof(struct ipv6_hdr);
370                                 ipv6_hdr = rte_pktmbuf_mtod_offset(mb,
371                                                                    struct ipv6_hdr *,
372                                                                    l2_len);
373                                 l4_proto = ipv6_hdr->proto;
374                         }
375                         if (l4_proto == IPPROTO_UDP) {
376                                 udp_hdr = rte_pktmbuf_mtod_offset(mb,
377                                                                   struct udp_hdr *,
378                                                                   l2_len + l3_len);
379                                 l4_len = sizeof(struct udp_hdr);
380                                 vxlan_hdr = rte_pktmbuf_mtod_offset(mb,
381                                                                     struct vxlan_hdr *,
382                                                                     l2_len + l3_len + l4_len);
383
384                                 printf(" - VXLAN packet: packet type =%d, "
385                                         "Destination UDP port =%d, VNI = %d",
386                                         packet_type, RTE_BE_TO_CPU_16(udp_hdr->dst_port),
387                                         rte_be_to_cpu_32(vxlan_hdr->vx_vni) >> 8);
388                         }
389                 }
390                 printf(" - Receive queue=0x%x", (unsigned) fs->rx_queue);
391                 printf("\n");
392                 if (ol_flags != 0) {
393                         unsigned rxf;
394                         const char *name;
395
396                         for (rxf = 0; rxf < sizeof(mb->ol_flags) * 8; rxf++) {
397                                 if ((ol_flags & (1ULL << rxf)) == 0)
398                                         continue;
399                                 name = rte_get_rx_ol_flag_name(1ULL << rxf);
400                                 if (name == NULL)
401                                         continue;
402                                 printf("  %s\n", name);
403                         }
404                 }
405                 rte_pktmbuf_free(mb);
406         }
407
408 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
409         end_tsc = rte_rdtsc();
410         core_cycles = (end_tsc - start_tsc);
411         fs->core_cycles = (uint64_t) (fs->core_cycles + core_cycles);
412 #endif
413 }
414
415 struct fwd_engine rx_only_engine = {
416         .fwd_mode_name  = "rxonly",
417         .port_fwd_begin = NULL,
418         .port_fwd_end   = NULL,
419         .packet_fwd     = pkt_burst_receive,
420 };