xref: /dpdk/app/test-pmd/util.c (revision 5208d68d30cbc36dc453703e58084f33af0320ef)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  * Copyright 2018 Mellanox Technologies, Ltd
4  */
5 
6 #include <stdio.h>
7 
8 #include <rte_bitops.h>
9 #include <rte_net.h>
10 #include <rte_mbuf.h>
11 #include <rte_ether.h>
12 #include <rte_vxlan.h>
13 #include <rte_ethdev.h>
14 #include <rte_flow.h>
15 
16 #include "testpmd.h"
17 
18 #define MAX_STRING_LEN 8192
19 
20 #define MKDUMPSTR(buf, buf_size, cur_len, ...) \
21 do { \
22 	if (cur_len >= buf_size) \
23 		break; \
24 	cur_len += snprintf(buf + cur_len, buf_size - cur_len, __VA_ARGS__); \
25 } while (0)
26 
27 static inline void
28 print_ether_addr(const char *what, const struct rte_ether_addr *eth_addr,
29 		 char print_buf[], size_t buf_size, size_t *cur_len)
30 {
31 	char buf[RTE_ETHER_ADDR_FMT_SIZE];
32 
33 	rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
34 	MKDUMPSTR(print_buf, buf_size, *cur_len, "%s%s", what, buf);
35 }
36 
37 static inline bool
38 is_timestamp_enabled(const struct rte_mbuf *mbuf)
39 {
40 	static uint64_t timestamp_rx_dynflag;
41 	int timestamp_rx_dynflag_offset;
42 
43 	if (timestamp_rx_dynflag == 0) {
44 		timestamp_rx_dynflag_offset = rte_mbuf_dynflag_lookup(
45 				RTE_MBUF_DYNFLAG_RX_TIMESTAMP_NAME, NULL);
46 		if (timestamp_rx_dynflag_offset < 0)
47 			return false;
48 		timestamp_rx_dynflag = RTE_BIT64(timestamp_rx_dynflag_offset);
49 	}
50 
51 	return (mbuf->ol_flags & timestamp_rx_dynflag) != 0;
52 }
53 
54 static inline rte_mbuf_timestamp_t
55 get_timestamp(const struct rte_mbuf *mbuf)
56 {
57 	static int timestamp_dynfield_offset = -1;
58 
59 	if (timestamp_dynfield_offset < 0) {
60 		timestamp_dynfield_offset = rte_mbuf_dynfield_lookup(
61 				RTE_MBUF_DYNFIELD_TIMESTAMP_NAME, NULL);
62 		if (timestamp_dynfield_offset < 0)
63 			return 0;
64 	}
65 
66 	return *RTE_MBUF_DYNFIELD(mbuf,
67 			timestamp_dynfield_offset, rte_mbuf_timestamp_t *);
68 }
69 
70 static inline void
71 dump_pkt_burst(uint16_t port_id, uint16_t queue, struct rte_mbuf *pkts[],
72 	      uint16_t nb_pkts, int is_rx)
73 {
74 	struct rte_mbuf  *mb;
75 	const struct rte_ether_hdr *eth_hdr;
76 	struct rte_ether_hdr _eth_hdr;
77 	uint16_t eth_type;
78 	uint64_t ol_flags;
79 	uint16_t i, packet_type;
80 	uint16_t is_encapsulation;
81 	char buf[256];
82 	struct rte_net_hdr_lens hdr_lens;
83 	uint32_t sw_packet_type;
84 	uint16_t udp_port;
85 	uint32_t vx_vni;
86 	const char *reason;
87 	int dynf_index;
88 	char print_buf[MAX_STRING_LEN];
89 	size_t buf_size = MAX_STRING_LEN;
90 	size_t cur_len = 0;
91 
92 	if (!nb_pkts)
93 		return;
94 	MKDUMPSTR(print_buf, buf_size, cur_len,
95 		  "port %u/queue %u: %s %u packets\n", port_id, queue,
96 		  is_rx ? "received" : "sent", (unsigned int) nb_pkts);
97 	for (i = 0; i < nb_pkts; i++) {
98 		int ret;
99 		struct rte_flow_error error;
100 		struct rte_flow_restore_info info = { 0, };
101 		struct rte_port *port = &ports[port_id];
102 
103 		mb = pkts[i];
104 		if (rxq_share > 0)
105 			MKDUMPSTR(print_buf, buf_size, cur_len, "port %u, ",
106 				  mb->port);
107 		eth_hdr = rte_pktmbuf_read(mb, 0, sizeof(_eth_hdr), &_eth_hdr);
108 		eth_type = RTE_BE_TO_CPU_16(eth_hdr->ether_type);
109 		packet_type = mb->packet_type;
110 		is_encapsulation = RTE_ETH_IS_TUNNEL_PKT(packet_type);
111 
112 		ret = rte_flow_get_restore_info(port->flow_transfer_proxy,
113 						mb, &info, &error);
114 		if (!ret) {
115 			MKDUMPSTR(print_buf, buf_size, cur_len,
116 				  "restore info:");
117 			if (info.flags & RTE_FLOW_RESTORE_INFO_TUNNEL) {
118 				struct port_flow_tunnel *port_tunnel;
119 
120 				port_tunnel = port_flow_locate_tunnel
121 					      (port_id, &info.tunnel);
122 				MKDUMPSTR(print_buf, buf_size, cur_len,
123 					  " - tunnel");
124 				if (port_tunnel)
125 					MKDUMPSTR(print_buf, buf_size, cur_len,
126 						  " #%u", port_tunnel->id);
127 				else
128 					MKDUMPSTR(print_buf, buf_size, cur_len,
129 						  " %s", "-none-");
130 				MKDUMPSTR(print_buf, buf_size, cur_len,
131 					  " type %s", port_flow_tunnel_type
132 					  (&info.tunnel));
133 			} else {
134 				MKDUMPSTR(print_buf, buf_size, cur_len,
135 					  " - no tunnel info");
136 			}
137 			if (info.flags & RTE_FLOW_RESTORE_INFO_ENCAPSULATED)
138 				MKDUMPSTR(print_buf, buf_size, cur_len,
139 					  " - outer header present");
140 			else
141 				MKDUMPSTR(print_buf, buf_size, cur_len,
142 					  " - no outer header");
143 			if (info.flags & RTE_FLOW_RESTORE_INFO_GROUP_ID)
144 				MKDUMPSTR(print_buf, buf_size, cur_len,
145 					  " - miss group %u", info.group_id);
146 			else
147 				MKDUMPSTR(print_buf, buf_size, cur_len,
148 					  " - no miss group");
149 			MKDUMPSTR(print_buf, buf_size, cur_len, "\n");
150 		}
151 		print_ether_addr("  src=", &eth_hdr->src_addr,
152 				 print_buf, buf_size, &cur_len);
153 		print_ether_addr(" - dst=", &eth_hdr->dst_addr,
154 				 print_buf, buf_size, &cur_len);
155 		MKDUMPSTR(print_buf, buf_size, cur_len,
156 			  " - type=0x%04x - length=%u - nb_segs=%d",
157 			  eth_type, (unsigned int) mb->pkt_len,
158 			  (int)mb->nb_segs);
159 		ol_flags = mb->ol_flags;
160 		if (ol_flags & PKT_RX_RSS_HASH) {
161 			MKDUMPSTR(print_buf, buf_size, cur_len,
162 				  " - RSS hash=0x%x",
163 				  (unsigned int) mb->hash.rss);
164 			MKDUMPSTR(print_buf, buf_size, cur_len,
165 				  " - RSS queue=0x%x", (unsigned int) queue);
166 		}
167 		if (ol_flags & PKT_RX_FDIR) {
168 			MKDUMPSTR(print_buf, buf_size, cur_len,
169 				  " - FDIR matched ");
170 			if (ol_flags & PKT_RX_FDIR_ID)
171 				MKDUMPSTR(print_buf, buf_size, cur_len,
172 					  "ID=0x%x", mb->hash.fdir.hi);
173 			else if (ol_flags & PKT_RX_FDIR_FLX)
174 				MKDUMPSTR(print_buf, buf_size, cur_len,
175 					  "flex bytes=0x%08x %08x",
176 					  mb->hash.fdir.hi, mb->hash.fdir.lo);
177 			else
178 				MKDUMPSTR(print_buf, buf_size, cur_len,
179 					  "hash=0x%x ID=0x%x ",
180 					  mb->hash.fdir.hash, mb->hash.fdir.id);
181 		}
182 		if (is_timestamp_enabled(mb))
183 			MKDUMPSTR(print_buf, buf_size, cur_len,
184 				  " - timestamp %"PRIu64" ", get_timestamp(mb));
185 		if (ol_flags & PKT_RX_QINQ)
186 			MKDUMPSTR(print_buf, buf_size, cur_len,
187 				  " - QinQ VLAN tci=0x%x, VLAN tci outer=0x%x",
188 				  mb->vlan_tci, mb->vlan_tci_outer);
189 		else if (ol_flags & PKT_RX_VLAN)
190 			MKDUMPSTR(print_buf, buf_size, cur_len,
191 				  " - VLAN tci=0x%x", mb->vlan_tci);
192 		if (!is_rx && (ol_flags & PKT_TX_DYNF_METADATA))
193 			MKDUMPSTR(print_buf, buf_size, cur_len,
194 				  " - Tx metadata: 0x%x",
195 				  *RTE_FLOW_DYNF_METADATA(mb));
196 		if (is_rx && (ol_flags & PKT_RX_DYNF_METADATA))
197 			MKDUMPSTR(print_buf, buf_size, cur_len,
198 				  " - Rx metadata: 0x%x",
199 				  *RTE_FLOW_DYNF_METADATA(mb));
200 		for (dynf_index = 0; dynf_index < 64; dynf_index++) {
201 			if (dynf_names[dynf_index][0] != '\0')
202 				MKDUMPSTR(print_buf, buf_size, cur_len,
203 					  " - dynf %s: %d",
204 					  dynf_names[dynf_index],
205 					  !!(ol_flags & (1UL << dynf_index)));
206 		}
207 		if (mb->packet_type) {
208 			rte_get_ptype_name(mb->packet_type, buf, sizeof(buf));
209 			MKDUMPSTR(print_buf, buf_size, cur_len,
210 				  " - hw ptype: %s", buf);
211 		}
212 		sw_packet_type = rte_net_get_ptype(mb, &hdr_lens,
213 					RTE_PTYPE_ALL_MASK);
214 		rte_get_ptype_name(sw_packet_type, buf, sizeof(buf));
215 		MKDUMPSTR(print_buf, buf_size, cur_len, " - sw ptype: %s", buf);
216 		if (sw_packet_type & RTE_PTYPE_L2_MASK)
217 			MKDUMPSTR(print_buf, buf_size, cur_len, " - l2_len=%d",
218 				  hdr_lens.l2_len);
219 		if (sw_packet_type & RTE_PTYPE_L3_MASK)
220 			MKDUMPSTR(print_buf, buf_size, cur_len, " - l3_len=%d",
221 				  hdr_lens.l3_len);
222 		if (sw_packet_type & RTE_PTYPE_L4_MASK)
223 			MKDUMPSTR(print_buf, buf_size, cur_len, " - l4_len=%d",
224 				  hdr_lens.l4_len);
225 		if (sw_packet_type & RTE_PTYPE_TUNNEL_MASK)
226 			MKDUMPSTR(print_buf, buf_size, cur_len,
227 				  " - tunnel_len=%d", hdr_lens.tunnel_len);
228 		if (sw_packet_type & RTE_PTYPE_INNER_L2_MASK)
229 			MKDUMPSTR(print_buf, buf_size, cur_len,
230 				  " - inner_l2_len=%d", hdr_lens.inner_l2_len);
231 		if (sw_packet_type & RTE_PTYPE_INNER_L3_MASK)
232 			MKDUMPSTR(print_buf, buf_size, cur_len,
233 				  " - inner_l3_len=%d", hdr_lens.inner_l3_len);
234 		if (sw_packet_type & RTE_PTYPE_INNER_L4_MASK)
235 			MKDUMPSTR(print_buf, buf_size, cur_len,
236 				  " - inner_l4_len=%d", hdr_lens.inner_l4_len);
237 		if (is_encapsulation) {
238 			struct rte_ipv4_hdr *ipv4_hdr;
239 			struct rte_ipv6_hdr *ipv6_hdr;
240 			struct rte_udp_hdr *udp_hdr;
241 			uint8_t l2_len;
242 			uint8_t l3_len;
243 			uint8_t l4_len;
244 			uint8_t l4_proto;
245 			struct  rte_vxlan_hdr *vxlan_hdr;
246 
247 			l2_len  = sizeof(struct rte_ether_hdr);
248 
249 			/* Do not support ipv4 option field */
250 			if (RTE_ETH_IS_IPV4_HDR(packet_type)) {
251 				l3_len = sizeof(struct rte_ipv4_hdr);
252 				ipv4_hdr = rte_pktmbuf_mtod_offset(mb,
253 				struct rte_ipv4_hdr *,
254 				l2_len);
255 				l4_proto = ipv4_hdr->next_proto_id;
256 			} else {
257 				l3_len = sizeof(struct rte_ipv6_hdr);
258 				ipv6_hdr = rte_pktmbuf_mtod_offset(mb,
259 				struct rte_ipv6_hdr *,
260 				l2_len);
261 				l4_proto = ipv6_hdr->proto;
262 			}
263 			if (l4_proto == IPPROTO_UDP) {
264 				udp_hdr = rte_pktmbuf_mtod_offset(mb,
265 				struct rte_udp_hdr *,
266 				l2_len + l3_len);
267 				l4_len = sizeof(struct rte_udp_hdr);
268 				vxlan_hdr = rte_pktmbuf_mtod_offset(mb,
269 				struct rte_vxlan_hdr *,
270 				l2_len + l3_len + l4_len);
271 				udp_port = RTE_BE_TO_CPU_16(udp_hdr->dst_port);
272 				vx_vni = rte_be_to_cpu_32(vxlan_hdr->vx_vni);
273 				MKDUMPSTR(print_buf, buf_size, cur_len,
274 					  " - VXLAN packet: packet type =%d, "
275 					  "Destination UDP port =%d, VNI = %d, "
276 					  "last_rsvd = %d", packet_type,
277 					  udp_port, vx_vni >> 8, vx_vni & 0xff);
278 			}
279 		}
280 		MKDUMPSTR(print_buf, buf_size, cur_len,
281 			  " - %s queue=0x%x", is_rx ? "Receive" : "Send",
282 			  (unsigned int) queue);
283 		MKDUMPSTR(print_buf, buf_size, cur_len, "\n");
284 		if (is_rx)
285 			rte_get_rx_ol_flag_list(mb->ol_flags, buf, sizeof(buf));
286 		else
287 			rte_get_tx_ol_flag_list(mb->ol_flags, buf, sizeof(buf));
288 
289 		MKDUMPSTR(print_buf, buf_size, cur_len,
290 			  "  ol_flags: %s\n", buf);
291 		if (rte_mbuf_check(mb, 1, &reason) < 0)
292 			MKDUMPSTR(print_buf, buf_size, cur_len,
293 				  "INVALID mbuf: %s\n", reason);
294 		if (cur_len >= buf_size)
295 			printf("%s ...\n", print_buf);
296 		else
297 			printf("%s", print_buf);
298 		cur_len = 0;
299 	}
300 }
301 
302 uint16_t
303 dump_rx_pkts(uint16_t port_id, uint16_t queue, struct rte_mbuf *pkts[],
304 	     uint16_t nb_pkts, __rte_unused uint16_t max_pkts,
305 	     __rte_unused void *user_param)
306 {
307 	dump_pkt_burst(port_id, queue, pkts, nb_pkts, 1);
308 	return nb_pkts;
309 }
310 
311 uint16_t
312 dump_tx_pkts(uint16_t port_id, uint16_t queue, struct rte_mbuf *pkts[],
313 	     uint16_t nb_pkts, __rte_unused void *user_param)
314 {
315 	dump_pkt_burst(port_id, queue, pkts, nb_pkts, 0);
316 	return nb_pkts;
317 }
318 
319 uint16_t
320 tx_pkt_set_md(uint16_t port_id, __rte_unused uint16_t queue,
321 	      struct rte_mbuf *pkts[], uint16_t nb_pkts,
322 	      __rte_unused void *user_param)
323 {
324 	uint16_t i = 0;
325 
326 	/*
327 	 * Add metadata value to every Tx packet,
328 	 * and set ol_flags accordingly.
329 	 */
330 	if (rte_flow_dynf_metadata_avail())
331 		for (i = 0; i < nb_pkts; i++) {
332 			*RTE_FLOW_DYNF_METADATA(pkts[i]) =
333 						ports[port_id].tx_metadata;
334 			pkts[i]->ol_flags |= PKT_TX_DYNF_METADATA;
335 		}
336 	return nb_pkts;
337 }
338 
339 void
340 add_tx_md_callback(portid_t portid)
341 {
342 	struct rte_eth_dev_info dev_info;
343 	uint16_t queue;
344 	int ret;
345 
346 	if (port_id_is_invalid(portid, ENABLED_WARN))
347 		return;
348 
349 	ret = eth_dev_info_get_print_err(portid, &dev_info);
350 	if (ret != 0)
351 		return;
352 
353 	for (queue = 0; queue < dev_info.nb_tx_queues; queue++)
354 		if (!ports[portid].tx_set_md_cb[queue])
355 			ports[portid].tx_set_md_cb[queue] =
356 				rte_eth_add_tx_callback(portid, queue,
357 							tx_pkt_set_md, NULL);
358 }
359 
360 void
361 remove_tx_md_callback(portid_t portid)
362 {
363 	struct rte_eth_dev_info dev_info;
364 	uint16_t queue;
365 	int ret;
366 
367 	if (port_id_is_invalid(portid, ENABLED_WARN))
368 		return;
369 
370 	ret = eth_dev_info_get_print_err(portid, &dev_info);
371 	if (ret != 0)
372 		return;
373 
374 	for (queue = 0; queue < dev_info.nb_tx_queues; queue++)
375 		if (ports[portid].tx_set_md_cb[queue]) {
376 			rte_eth_remove_tx_callback(portid, queue,
377 				ports[portid].tx_set_md_cb[queue]);
378 			ports[portid].tx_set_md_cb[queue] = NULL;
379 		}
380 }
381 
382 uint16_t
383 tx_pkt_set_dynf(uint16_t port_id, __rte_unused uint16_t queue,
384 		struct rte_mbuf *pkts[], uint16_t nb_pkts,
385 		__rte_unused void *user_param)
386 {
387 	uint16_t i = 0;
388 
389 	if (ports[port_id].mbuf_dynf)
390 		for (i = 0; i < nb_pkts; i++)
391 			pkts[i]->ol_flags |= ports[port_id].mbuf_dynf;
392 	return nb_pkts;
393 }
394 
395 void
396 add_tx_dynf_callback(portid_t portid)
397 {
398 	struct rte_eth_dev_info dev_info;
399 	uint16_t queue;
400 	int ret;
401 
402 	if (port_id_is_invalid(portid, ENABLED_WARN))
403 		return;
404 
405 	ret = eth_dev_info_get_print_err(portid, &dev_info);
406 	if (ret != 0)
407 		return;
408 
409 	for (queue = 0; queue < dev_info.nb_tx_queues; queue++)
410 		if (!ports[portid].tx_set_dynf_cb[queue])
411 			ports[portid].tx_set_dynf_cb[queue] =
412 				rte_eth_add_tx_callback(portid, queue,
413 							tx_pkt_set_dynf, NULL);
414 }
415 
416 void
417 remove_tx_dynf_callback(portid_t portid)
418 {
419 	struct rte_eth_dev_info dev_info;
420 	uint16_t queue;
421 	int ret;
422 
423 	if (port_id_is_invalid(portid, ENABLED_WARN))
424 		return;
425 
426 	ret = eth_dev_info_get_print_err(portid, &dev_info);
427 	if (ret != 0)
428 		return;
429 
430 	for (queue = 0; queue < dev_info.nb_tx_queues; queue++)
431 		if (ports[portid].tx_set_dynf_cb[queue]) {
432 			rte_eth_remove_tx_callback(portid, queue,
433 				ports[portid].tx_set_dynf_cb[queue]);
434 			ports[portid].tx_set_dynf_cb[queue] = NULL;
435 		}
436 }
437 
438 int
439 eth_dev_info_get_print_err(uint16_t port_id,
440 					struct rte_eth_dev_info *dev_info)
441 {
442 	int ret;
443 
444 	ret = rte_eth_dev_info_get(port_id, dev_info);
445 	if (ret != 0)
446 		fprintf(stderr,
447 			"Error during getting device (port %u) info: %s\n",
448 			port_id, strerror(-ret));
449 
450 	return ret;
451 }
452 
453 int
454 eth_dev_conf_get_print_err(uint16_t port_id, struct rte_eth_conf *dev_conf)
455 {
456 	int ret;
457 
458 	ret = rte_eth_dev_conf_get(port_id, dev_conf);
459 	if (ret != 0)
460 		fprintf(stderr,
461 			"Error during getting device configuration (port %u): %s\n",
462 			port_id, strerror(-ret));
463 
464 	return ret;
465 }
466 
467 void
468 eth_set_promisc_mode(uint16_t port, int enable)
469 {
470 	int ret;
471 
472 	if (enable)
473 		ret = rte_eth_promiscuous_enable(port);
474 	else
475 		ret = rte_eth_promiscuous_disable(port);
476 
477 	if (ret != 0)
478 		fprintf(stderr,
479 			"Error during %s promiscuous mode for port %u: %s\n",
480 			enable ? "enabling" : "disabling",
481 			port, rte_strerror(-ret));
482 }
483 
484 void
485 eth_set_allmulticast_mode(uint16_t port, int enable)
486 {
487 	int ret;
488 
489 	if (enable)
490 		ret = rte_eth_allmulticast_enable(port);
491 	else
492 		ret = rte_eth_allmulticast_disable(port);
493 
494 	if (ret != 0)
495 		fprintf(stderr,
496 			"Error during %s all-multicast mode for port %u: %s\n",
497 			enable ? "enabling" : "disabling",
498 			port, rte_strerror(-ret));
499 }
500 
501 int
502 eth_link_get_nowait_print_err(uint16_t port_id, struct rte_eth_link *link)
503 {
504 	int ret;
505 
506 	ret = rte_eth_link_get_nowait(port_id, link);
507 	if (ret < 0)
508 		fprintf(stderr,
509 			"Device (port %u) link get (without wait) failed: %s\n",
510 			port_id, rte_strerror(-ret));
511 
512 	return ret;
513 }
514 
515 int
516 eth_macaddr_get_print_err(uint16_t port_id, struct rte_ether_addr *mac_addr)
517 {
518 	int ret;
519 
520 	ret = rte_eth_macaddr_get(port_id, mac_addr);
521 	if (ret != 0)
522 		fprintf(stderr,
523 			"Error getting device (port %u) mac address: %s\n",
524 			port_id, rte_strerror(-ret));
525 
526 	return ret;
527 }
528