app/testpmd: VXLAN packet identification
[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_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_interrupts.h>
65 #include <rte_pci.h>
66 #include <rte_ether.h>
67 #include <rte_ethdev.h>
68 #include <rte_string_fns.h>
69 #include <rte_ip.h>
70 #include <rte_udp.h>
71
72 #include "testpmd.h"
73
74 #define MAX_PKT_RX_FLAGS 13
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         "TUNNEL_IPV4_HDR",
91         "TUNNEL_IPV6_HDR",
92 };
93
94 static inline void
95 print_ether_addr(const char *what, struct ether_addr *eth_addr)
96 {
97         printf("%s%02X:%02X:%02X:%02X:%02X:%02X",
98                what,
99                eth_addr->addr_bytes[0],
100                eth_addr->addr_bytes[1],
101                eth_addr->addr_bytes[2],
102                eth_addr->addr_bytes[3],
103                eth_addr->addr_bytes[4],
104                eth_addr->addr_bytes[5]);
105 }
106
107 /*
108  * Received a burst of packets.
109  */
110 static void
111 pkt_burst_receive(struct fwd_stream *fs)
112 {
113         struct rte_mbuf  *pkts_burst[MAX_PKT_BURST];
114         struct rte_mbuf  *mb;
115         struct ether_hdr *eth_hdr;
116         uint16_t eth_type;
117         uint64_t ol_flags;
118         uint16_t nb_rx;
119         uint16_t i, packet_type;
120         uint64_t is_encapsulation;
121
122 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
123         uint64_t start_tsc;
124         uint64_t end_tsc;
125         uint64_t core_cycles;
126 #endif
127
128 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
129         start_tsc = rte_rdtsc();
130 #endif
131
132         /*
133          * Receive a burst of packets.
134          */
135         nb_rx = rte_eth_rx_burst(fs->rx_port, fs->rx_queue, pkts_burst,
136                                  nb_pkt_per_burst);
137         if (unlikely(nb_rx == 0))
138                 return;
139
140 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
141         fs->rx_burst_stats.pkt_burst_spread[nb_rx]++;
142 #endif
143         fs->rx_packets += nb_rx;
144
145         /*
146          * Dump each received packet if verbose_level > 0.
147          */
148         if (verbose_level > 0)
149                 printf("port %u/queue %u: received %u packets\n",
150                        (unsigned) fs->rx_port,
151                        (unsigned) fs->rx_queue,
152                        (unsigned) nb_rx);
153         for (i = 0; i < nb_rx; i++) {
154                 mb = pkts_burst[i];
155                 if (verbose_level == 0) {
156                         rte_pktmbuf_free(mb);
157                         continue;
158                 }
159                 eth_hdr = rte_pktmbuf_mtod(mb, struct ether_hdr *);
160                 eth_type = RTE_BE_TO_CPU_16(eth_hdr->ether_type);
161                 ol_flags = mb->ol_flags;
162                 packet_type = mb->packet_type;
163
164                 is_encapsulation = ol_flags & (PKT_RX_TUNNEL_IPV4_HDR |
165                                 PKT_RX_TUNNEL_IPV6_HDR);
166
167                 print_ether_addr("  src=", &eth_hdr->s_addr);
168                 print_ether_addr(" - dst=", &eth_hdr->d_addr);
169                 printf(" - type=0x%04x - length=%u - nb_segs=%d",
170                        eth_type, (unsigned) mb->pkt_len,
171                        (int)mb->nb_segs);
172                 if (ol_flags & PKT_RX_RSS_HASH) {
173                         printf(" - RSS hash=0x%x", (unsigned) mb->hash.rss);
174                         printf(" - RSS queue=0x%x",(unsigned) fs->rx_queue);
175                 }
176                 else if (ol_flags & PKT_RX_FDIR)
177                         printf(" - FDIR hash=0x%x - FDIR id=0x%x ",
178                                mb->hash.fdir.hash, mb->hash.fdir.id);
179                 if (ol_flags & PKT_RX_VLAN_PKT)
180                         printf(" - VLAN tci=0x%x", mb->vlan_tci);
181                 if (is_encapsulation) {
182                         struct ipv4_hdr *ipv4_hdr;
183                         struct ipv6_hdr *ipv6_hdr;
184                         struct udp_hdr *udp_hdr;
185                         uint8_t l2_len;
186                         uint8_t l3_len;
187                         uint8_t l4_len;
188                         uint8_t l4_proto;
189                         struct  vxlan_hdr *vxlan_hdr;
190
191                         l2_len  = sizeof(struct ether_hdr);
192
193                          /* Do not support ipv4 option field */
194                         if (ol_flags & PKT_RX_TUNNEL_IPV4_HDR) {
195                                 l3_len = sizeof(struct ipv4_hdr);
196                                 ipv4_hdr = (struct ipv4_hdr *) (rte_pktmbuf_mtod(mb,
197                                                 unsigned char *) + l2_len);
198                                 l4_proto = ipv4_hdr->next_proto_id;
199                         } else {
200                                 l3_len = sizeof(struct ipv6_hdr);
201                                 ipv6_hdr = (struct ipv6_hdr *) (rte_pktmbuf_mtod(mb,
202                                                 unsigned char *) + l2_len);
203                                 l4_proto = ipv6_hdr->proto;
204                         }
205                         if (l4_proto == IPPROTO_UDP) {
206                                 udp_hdr = (struct udp_hdr *) (rte_pktmbuf_mtod(mb,
207                                                 unsigned char *) + l2_len + l3_len);
208                                 l4_len = sizeof(struct udp_hdr);
209                                 vxlan_hdr = (struct vxlan_hdr *) (rte_pktmbuf_mtod(mb,
210                                                 unsigned char *) + l2_len + l3_len
211                                                  + l4_len);
212
213                                 printf(" - VxLAN packet: packet type =%d, "
214                                         "Destination UDP port =%d, VNI = %d",
215                                         packet_type, RTE_BE_TO_CPU_16(udp_hdr->dst_port),
216                                         rte_be_to_cpu_32(vxlan_hdr->vx_vni) >> 8);
217                         }
218                 }
219                 printf(" - Receive queue=0x%x", (unsigned) fs->rx_queue);
220                 printf("\n");
221                 if (ol_flags != 0) {
222                         int rxf;
223
224                         for (rxf = 0; rxf < MAX_PKT_RX_FLAGS; rxf++) {
225                                 if (ol_flags & (1 << rxf))
226                                         printf("  PKT_RX_%s\n",
227                                                pkt_rx_flag_names[rxf]);
228                         }
229                 }
230                 rte_pktmbuf_free(mb);
231         }
232
233 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
234         end_tsc = rte_rdtsc();
235         core_cycles = (end_tsc - start_tsc);
236         fs->core_cycles = (uint64_t) (fs->core_cycles + core_cycles);
237 #endif
238 }
239
240 struct fwd_engine rx_only_engine = {
241         .fwd_mode_name  = "rxonly",
242         .port_fwd_begin = NULL,
243         .port_fwd_end   = NULL,
244         .packet_fwd     = pkt_burst_receive,
245 };