xref: /dpdk/app/test-pmd/util.c (revision a137d012a0dd7be982367687ea6cd10de6f12c69)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  * Copyright(c) 2018 Mellanox Technology
4  */
5 
6 #include <stdio.h>
7 
8 #include <rte_net.h>
9 #include <rte_mbuf.h>
10 #include <rte_ether.h>
11 #include <rte_ethdev.h>
12 #include <rte_flow.h>
13 
14 #include "testpmd.h"
15 
16 static inline void
17 print_ether_addr(const char *what, struct ether_addr *eth_addr)
18 {
19 	char buf[ETHER_ADDR_FMT_SIZE];
20 	ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
21 	printf("%s%s", what, buf);
22 }
23 
24 static inline void
25 dump_pkt_burst(uint16_t port_id, uint16_t queue, struct rte_mbuf *pkts[],
26 	      uint16_t nb_pkts, int is_rx)
27 {
28 	struct rte_mbuf  *mb;
29 	struct ether_hdr *eth_hdr;
30 	uint16_t eth_type;
31 	uint64_t ol_flags;
32 	uint16_t i, packet_type;
33 	uint16_t is_encapsulation;
34 	char buf[256];
35 	struct rte_net_hdr_lens hdr_lens;
36 	uint32_t sw_packet_type;
37 	uint16_t udp_port;
38 	uint32_t vx_vni;
39 
40 	if (!nb_pkts)
41 		return;
42 	printf("port %u/queue %u: %s %u packets\n",
43 		port_id, queue,
44 	       is_rx ? "received" : "sent",
45 	       (unsigned int) nb_pkts);
46 	for (i = 0; i < nb_pkts; i++) {
47 		mb = pkts[i];
48 		eth_hdr = rte_pktmbuf_mtod(mb, struct ether_hdr *);
49 		eth_type = RTE_BE_TO_CPU_16(eth_hdr->ether_type);
50 		ol_flags = mb->ol_flags;
51 		packet_type = mb->packet_type;
52 		is_encapsulation = RTE_ETH_IS_TUNNEL_PKT(packet_type);
53 
54 		print_ether_addr("  src=", &eth_hdr->s_addr);
55 		print_ether_addr(" - dst=", &eth_hdr->d_addr);
56 		printf(" - type=0x%04x - length=%u - nb_segs=%d",
57 		       eth_type, (unsigned int) mb->pkt_len,
58 		       (int)mb->nb_segs);
59 		if (ol_flags & PKT_RX_RSS_HASH) {
60 			printf(" - RSS hash=0x%x", (unsigned int) mb->hash.rss);
61 			printf(" - RSS queue=0x%x", (unsigned int) queue);
62 		}
63 		if (ol_flags & PKT_RX_FDIR) {
64 			printf(" - FDIR matched ");
65 			if (ol_flags & PKT_RX_FDIR_ID)
66 				printf("ID=0x%x",
67 				       mb->hash.fdir.hi);
68 			else if (ol_flags & PKT_RX_FDIR_FLX)
69 				printf("flex bytes=0x%08x %08x",
70 				       mb->hash.fdir.hi, mb->hash.fdir.lo);
71 			else
72 				printf("hash=0x%x ID=0x%x ",
73 				       mb->hash.fdir.hash, mb->hash.fdir.id);
74 		}
75 		if (ol_flags & PKT_RX_TIMESTAMP)
76 			printf(" - timestamp %"PRIu64" ", mb->timestamp);
77 		if (ol_flags & PKT_RX_QINQ)
78 			printf(" - QinQ VLAN tci=0x%x, VLAN tci outer=0x%x",
79 			       mb->vlan_tci, mb->vlan_tci_outer);
80 		else if (ol_flags & PKT_RX_VLAN)
81 			printf(" - VLAN tci=0x%x", mb->vlan_tci);
82 		if (mb->packet_type) {
83 			rte_get_ptype_name(mb->packet_type, buf, sizeof(buf));
84 			printf(" - hw ptype: %s", buf);
85 		}
86 		sw_packet_type = rte_net_get_ptype(mb, &hdr_lens,
87 					RTE_PTYPE_ALL_MASK);
88 		rte_get_ptype_name(sw_packet_type, buf, sizeof(buf));
89 		printf(" - sw ptype: %s", buf);
90 		if (sw_packet_type & RTE_PTYPE_L2_MASK)
91 			printf(" - l2_len=%d", hdr_lens.l2_len);
92 		if (sw_packet_type & RTE_PTYPE_L3_MASK)
93 			printf(" - l3_len=%d", hdr_lens.l3_len);
94 		if (sw_packet_type & RTE_PTYPE_L4_MASK)
95 			printf(" - l4_len=%d", hdr_lens.l4_len);
96 		if (sw_packet_type & RTE_PTYPE_TUNNEL_MASK)
97 			printf(" - tunnel_len=%d", hdr_lens.tunnel_len);
98 		if (sw_packet_type & RTE_PTYPE_INNER_L2_MASK)
99 			printf(" - inner_l2_len=%d", hdr_lens.inner_l2_len);
100 		if (sw_packet_type & RTE_PTYPE_INNER_L3_MASK)
101 			printf(" - inner_l3_len=%d", hdr_lens.inner_l3_len);
102 		if (sw_packet_type & RTE_PTYPE_INNER_L4_MASK)
103 			printf(" - inner_l4_len=%d", hdr_lens.inner_l4_len);
104 		if (is_encapsulation) {
105 			struct ipv4_hdr *ipv4_hdr;
106 			struct ipv6_hdr *ipv6_hdr;
107 			struct udp_hdr *udp_hdr;
108 			uint8_t l2_len;
109 			uint8_t l3_len;
110 			uint8_t l4_len;
111 			uint8_t l4_proto;
112 			struct  vxlan_hdr *vxlan_hdr;
113 
114 			l2_len  = sizeof(struct ether_hdr);
115 
116 			/* Do not support ipv4 option field */
117 			if (RTE_ETH_IS_IPV4_HDR(packet_type)) {
118 				l3_len = sizeof(struct ipv4_hdr);
119 				ipv4_hdr = rte_pktmbuf_mtod_offset(mb,
120 				struct ipv4_hdr *,
121 				l2_len);
122 				l4_proto = ipv4_hdr->next_proto_id;
123 			} else {
124 				l3_len = sizeof(struct ipv6_hdr);
125 				ipv6_hdr = rte_pktmbuf_mtod_offset(mb,
126 				struct ipv6_hdr *,
127 				l2_len);
128 				l4_proto = ipv6_hdr->proto;
129 			}
130 			if (l4_proto == IPPROTO_UDP) {
131 				udp_hdr = rte_pktmbuf_mtod_offset(mb,
132 				struct udp_hdr *,
133 				l2_len + l3_len);
134 				l4_len = sizeof(struct udp_hdr);
135 				vxlan_hdr = rte_pktmbuf_mtod_offset(mb,
136 				struct vxlan_hdr *,
137 				l2_len + l3_len + l4_len);
138 				udp_port = RTE_BE_TO_CPU_16(udp_hdr->dst_port);
139 				vx_vni = rte_be_to_cpu_32(vxlan_hdr->vx_vni);
140 				printf(" - VXLAN packet: packet type =%d, "
141 				       "Destination UDP port =%d, VNI = %d",
142 				       packet_type, udp_port, vx_vni >> 8);
143 			}
144 		}
145 		printf(" - %s queue=0x%x", is_rx ? "Receive" : "Send",
146 			(unsigned int) queue);
147 		printf("\n");
148 		rte_get_rx_ol_flag_list(mb->ol_flags, buf, sizeof(buf));
149 		printf("  ol_flags: %s\n", buf);
150 	}
151 }
152 
153 uint16_t
154 dump_rx_pkts(uint16_t port_id, uint16_t queue, struct rte_mbuf *pkts[],
155 	     uint16_t nb_pkts, __rte_unused uint16_t max_pkts,
156 	     __rte_unused void *user_param)
157 {
158 	dump_pkt_burst(port_id, queue, pkts, nb_pkts, 1);
159 	return nb_pkts;
160 }
161 
162 uint16_t
163 dump_tx_pkts(uint16_t port_id, uint16_t queue, struct rte_mbuf *pkts[],
164 	     uint16_t nb_pkts, __rte_unused void *user_param)
165 {
166 	dump_pkt_burst(port_id, queue, pkts, nb_pkts, 0);
167 	return nb_pkts;
168 }
169