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
print_ether_addr(const char * what,const struct rte_ether_addr * eth_addr,char print_buf[],size_t buf_size,size_t * cur_len)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
is_timestamp_enabled(const struct rte_mbuf * mbuf)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
get_timestamp(const struct rte_mbuf * mbuf)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
dump_pkt_burst(uint16_t port_id,uint16_t queue,struct rte_mbuf * pkts[],uint16_t nb_pkts,int is_rx)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
102 mb = pkts[i];
103 if (rxq_share > 0)
104 MKDUMPSTR(print_buf, buf_size, cur_len, "port %u, ",
105 mb->port);
106 eth_hdr = rte_pktmbuf_read(mb, 0, sizeof(_eth_hdr), &_eth_hdr);
107 eth_type = RTE_BE_TO_CPU_16(eth_hdr->ether_type);
108 packet_type = mb->packet_type;
109 is_encapsulation = RTE_ETH_IS_TUNNEL_PKT(packet_type);
110 ret = rte_flow_get_restore_info(port_id, mb, &info, &error);
111 if (!ret) {
112 MKDUMPSTR(print_buf, buf_size, cur_len,
113 "restore info:");
114 if (info.flags & RTE_FLOW_RESTORE_INFO_TUNNEL) {
115 struct port_flow_tunnel *port_tunnel;
116
117 port_tunnel = port_flow_locate_tunnel
118 (port_id, &info.tunnel);
119 MKDUMPSTR(print_buf, buf_size, cur_len,
120 " - tunnel");
121 if (port_tunnel)
122 MKDUMPSTR(print_buf, buf_size, cur_len,
123 " #%u", port_tunnel->id);
124 else
125 MKDUMPSTR(print_buf, buf_size, cur_len,
126 " %s", "-none-");
127 MKDUMPSTR(print_buf, buf_size, cur_len,
128 " type %s", port_flow_tunnel_type
129 (&info.tunnel));
130 } else {
131 MKDUMPSTR(print_buf, buf_size, cur_len,
132 " - no tunnel info");
133 }
134 if (info.flags & RTE_FLOW_RESTORE_INFO_ENCAPSULATED)
135 MKDUMPSTR(print_buf, buf_size, cur_len,
136 " - outer header present");
137 else
138 MKDUMPSTR(print_buf, buf_size, cur_len,
139 " - no outer header");
140 if (info.flags & RTE_FLOW_RESTORE_INFO_GROUP_ID)
141 MKDUMPSTR(print_buf, buf_size, cur_len,
142 " - miss group %u", info.group_id);
143 else
144 MKDUMPSTR(print_buf, buf_size, cur_len,
145 " - no miss group");
146 MKDUMPSTR(print_buf, buf_size, cur_len, "\n");
147 }
148 print_ether_addr(" src=", ð_hdr->src_addr,
149 print_buf, buf_size, &cur_len);
150 print_ether_addr(" - dst=", ð_hdr->dst_addr,
151 print_buf, buf_size, &cur_len);
152 MKDUMPSTR(print_buf, buf_size, cur_len,
153 " - type=0x%04x - length=%u - nb_segs=%d",
154 eth_type, (unsigned int) mb->pkt_len,
155 (int)mb->nb_segs);
156 ol_flags = mb->ol_flags;
157 if (ol_flags & RTE_MBUF_F_RX_RSS_HASH) {
158 MKDUMPSTR(print_buf, buf_size, cur_len,
159 " - RSS hash=0x%x",
160 (unsigned int) mb->hash.rss);
161 MKDUMPSTR(print_buf, buf_size, cur_len,
162 " - RSS queue=0x%x", (unsigned int) queue);
163 }
164 if (ol_flags & RTE_MBUF_F_RX_FDIR) {
165 MKDUMPSTR(print_buf, buf_size, cur_len,
166 " - FDIR matched ");
167 if (ol_flags & RTE_MBUF_F_RX_FDIR_ID)
168 MKDUMPSTR(print_buf, buf_size, cur_len,
169 "ID=0x%x", mb->hash.fdir.hi);
170 else if (ol_flags & RTE_MBUF_F_RX_FDIR_FLX)
171 MKDUMPSTR(print_buf, buf_size, cur_len,
172 "flex bytes=0x%08x %08x",
173 mb->hash.fdir.hi, mb->hash.fdir.lo);
174 else
175 MKDUMPSTR(print_buf, buf_size, cur_len,
176 "hash=0x%x ID=0x%x ",
177 mb->hash.fdir.hash, mb->hash.fdir.id);
178 }
179 if (is_timestamp_enabled(mb))
180 MKDUMPSTR(print_buf, buf_size, cur_len,
181 " - timestamp %"PRIu64" ", get_timestamp(mb));
182 if (ol_flags & RTE_MBUF_F_RX_QINQ)
183 MKDUMPSTR(print_buf, buf_size, cur_len,
184 " - QinQ VLAN tci=0x%x, VLAN tci outer=0x%x",
185 mb->vlan_tci, mb->vlan_tci_outer);
186 else if (ol_flags & RTE_MBUF_F_RX_VLAN)
187 MKDUMPSTR(print_buf, buf_size, cur_len,
188 " - VLAN tci=0x%x", mb->vlan_tci);
189 if (!is_rx && (ol_flags & RTE_MBUF_DYNFLAG_TX_METADATA))
190 MKDUMPSTR(print_buf, buf_size, cur_len,
191 " - Tx metadata: 0x%x",
192 *RTE_FLOW_DYNF_METADATA(mb));
193 if (is_rx && (ol_flags & RTE_MBUF_DYNFLAG_RX_METADATA))
194 MKDUMPSTR(print_buf, buf_size, cur_len,
195 " - Rx metadata: 0x%x",
196 *RTE_FLOW_DYNF_METADATA(mb));
197 for (dynf_index = 0; dynf_index < 64; dynf_index++) {
198 if (dynf_names[dynf_index][0] != '\0')
199 MKDUMPSTR(print_buf, buf_size, cur_len,
200 " - dynf %s: %d",
201 dynf_names[dynf_index],
202 !!(ol_flags & (1UL << dynf_index)));
203 }
204 if (mb->packet_type) {
205 rte_get_ptype_name(mb->packet_type, buf, sizeof(buf));
206 MKDUMPSTR(print_buf, buf_size, cur_len,
207 " - hw ptype: %s", buf);
208 }
209 sw_packet_type = rte_net_get_ptype(mb, &hdr_lens,
210 RTE_PTYPE_ALL_MASK);
211 rte_get_ptype_name(sw_packet_type, buf, sizeof(buf));
212 MKDUMPSTR(print_buf, buf_size, cur_len, " - sw ptype: %s", buf);
213 if (sw_packet_type & RTE_PTYPE_L2_MASK)
214 MKDUMPSTR(print_buf, buf_size, cur_len, " - l2_len=%d",
215 hdr_lens.l2_len);
216 if (sw_packet_type & RTE_PTYPE_L3_MASK)
217 MKDUMPSTR(print_buf, buf_size, cur_len, " - l3_len=%d",
218 hdr_lens.l3_len);
219 if (sw_packet_type & RTE_PTYPE_L4_MASK)
220 MKDUMPSTR(print_buf, buf_size, cur_len, " - l4_len=%d",
221 hdr_lens.l4_len);
222 if (sw_packet_type & RTE_PTYPE_TUNNEL_MASK)
223 MKDUMPSTR(print_buf, buf_size, cur_len,
224 " - tunnel_len=%d", hdr_lens.tunnel_len);
225 if (sw_packet_type & RTE_PTYPE_INNER_L2_MASK)
226 MKDUMPSTR(print_buf, buf_size, cur_len,
227 " - inner_l2_len=%d", hdr_lens.inner_l2_len);
228 if (sw_packet_type & RTE_PTYPE_INNER_L3_MASK)
229 MKDUMPSTR(print_buf, buf_size, cur_len,
230 " - inner_l3_len=%d", hdr_lens.inner_l3_len);
231 if (sw_packet_type & RTE_PTYPE_INNER_L4_MASK)
232 MKDUMPSTR(print_buf, buf_size, cur_len,
233 " - inner_l4_len=%d", hdr_lens.inner_l4_len);
234 if (is_encapsulation) {
235 struct rte_ipv4_hdr *ipv4_hdr;
236 struct rte_ipv6_hdr *ipv6_hdr;
237 struct rte_udp_hdr *udp_hdr;
238 uint8_t l2_len;
239 uint8_t l3_len;
240 uint8_t l4_len;
241 uint8_t l4_proto;
242 struct rte_vxlan_hdr *vxlan_hdr;
243
244 l2_len = sizeof(struct rte_ether_hdr);
245
246 /* Do not support ipv4 option field */
247 if (RTE_ETH_IS_IPV4_HDR(packet_type)) {
248 l3_len = sizeof(struct rte_ipv4_hdr);
249 ipv4_hdr = rte_pktmbuf_mtod_offset(mb,
250 struct rte_ipv4_hdr *,
251 l2_len);
252 l4_proto = ipv4_hdr->next_proto_id;
253 } else {
254 l3_len = sizeof(struct rte_ipv6_hdr);
255 ipv6_hdr = rte_pktmbuf_mtod_offset(mb,
256 struct rte_ipv6_hdr *,
257 l2_len);
258 l4_proto = ipv6_hdr->proto;
259 }
260 if (l4_proto == IPPROTO_UDP) {
261 udp_hdr = rte_pktmbuf_mtod_offset(mb,
262 struct rte_udp_hdr *,
263 l2_len + l3_len);
264 l4_len = sizeof(struct rte_udp_hdr);
265 vxlan_hdr = rte_pktmbuf_mtod_offset(mb,
266 struct rte_vxlan_hdr *,
267 l2_len + l3_len + l4_len);
268 udp_port = RTE_BE_TO_CPU_16(udp_hdr->dst_port);
269 vx_vni = rte_be_to_cpu_32(vxlan_hdr->vx_vni);
270 MKDUMPSTR(print_buf, buf_size, cur_len,
271 " - VXLAN packet: packet type =%d, "
272 "Destination UDP port =%d, VNI = %d, "
273 "last_rsvd = %d", packet_type,
274 udp_port, vx_vni >> 8, vx_vni & 0xff);
275 }
276 }
277 MKDUMPSTR(print_buf, buf_size, cur_len,
278 " - %s queue=0x%x", is_rx ? "Receive" : "Send",
279 (unsigned int) queue);
280 MKDUMPSTR(print_buf, buf_size, cur_len, "\n");
281 if (is_rx)
282 rte_get_rx_ol_flag_list(mb->ol_flags, buf, sizeof(buf));
283 else
284 rte_get_tx_ol_flag_list(mb->ol_flags, buf, sizeof(buf));
285
286 MKDUMPSTR(print_buf, buf_size, cur_len,
287 " ol_flags: %s\n", buf);
288 if (rte_mbuf_check(mb, 1, &reason) < 0)
289 MKDUMPSTR(print_buf, buf_size, cur_len,
290 "INVALID mbuf: %s\n", reason);
291 if (cur_len >= buf_size)
292 printf("%s ...\n", print_buf);
293 else
294 printf("%s", print_buf);
295 cur_len = 0;
296 }
297 }
298
299 uint16_t
dump_rx_pkts(uint16_t port_id,uint16_t queue,struct rte_mbuf * pkts[],uint16_t nb_pkts,__rte_unused uint16_t max_pkts,__rte_unused void * user_param)300 dump_rx_pkts(uint16_t port_id, uint16_t queue, struct rte_mbuf *pkts[],
301 uint16_t nb_pkts, __rte_unused uint16_t max_pkts,
302 __rte_unused void *user_param)
303 {
304 dump_pkt_burst(port_id, queue, pkts, nb_pkts, 1);
305 return nb_pkts;
306 }
307
308 uint16_t
dump_tx_pkts(uint16_t port_id,uint16_t queue,struct rte_mbuf * pkts[],uint16_t nb_pkts,__rte_unused void * user_param)309 dump_tx_pkts(uint16_t port_id, uint16_t queue, struct rte_mbuf *pkts[],
310 uint16_t nb_pkts, __rte_unused void *user_param)
311 {
312 dump_pkt_burst(port_id, queue, pkts, nb_pkts, 0);
313 return nb_pkts;
314 }
315
316 uint16_t
tx_pkt_set_md(uint16_t port_id,__rte_unused uint16_t queue,struct rte_mbuf * pkts[],uint16_t nb_pkts,__rte_unused void * user_param)317 tx_pkt_set_md(uint16_t port_id, __rte_unused uint16_t queue,
318 struct rte_mbuf *pkts[], uint16_t nb_pkts,
319 __rte_unused void *user_param)
320 {
321 uint16_t i = 0;
322
323 /*
324 * Add metadata value to every Tx packet,
325 * and set ol_flags accordingly.
326 */
327 if (rte_flow_dynf_metadata_avail())
328 for (i = 0; i < nb_pkts; i++) {
329 *RTE_FLOW_DYNF_METADATA(pkts[i]) =
330 ports[port_id].tx_metadata;
331 pkts[i]->ol_flags |= RTE_MBUF_DYNFLAG_TX_METADATA;
332 }
333 return nb_pkts;
334 }
335
336 void
add_tx_md_callback(portid_t portid)337 add_tx_md_callback(portid_t portid)
338 {
339 struct rte_eth_dev_info dev_info;
340 uint16_t queue;
341 int ret;
342
343 if (port_id_is_invalid(portid, ENABLED_WARN))
344 return;
345
346 ret = eth_dev_info_get_print_err(portid, &dev_info);
347 if (ret != 0)
348 return;
349
350 for (queue = 0; queue < dev_info.nb_tx_queues; queue++)
351 if (!ports[portid].tx_set_md_cb[queue])
352 ports[portid].tx_set_md_cb[queue] =
353 rte_eth_add_tx_callback(portid, queue,
354 tx_pkt_set_md, NULL);
355 }
356
357 void
remove_tx_md_callback(portid_t portid)358 remove_tx_md_callback(portid_t portid)
359 {
360 struct rte_eth_dev_info dev_info;
361 uint16_t queue;
362 int ret;
363
364 if (port_id_is_invalid(portid, ENABLED_WARN))
365 return;
366
367 ret = eth_dev_info_get_print_err(portid, &dev_info);
368 if (ret != 0)
369 return;
370
371 for (queue = 0; queue < dev_info.nb_tx_queues; queue++)
372 if (ports[portid].tx_set_md_cb[queue]) {
373 rte_eth_remove_tx_callback(portid, queue,
374 ports[portid].tx_set_md_cb[queue]);
375 ports[portid].tx_set_md_cb[queue] = NULL;
376 }
377 }
378
379 uint16_t
tx_pkt_set_dynf(uint16_t port_id,__rte_unused uint16_t queue,struct rte_mbuf * pkts[],uint16_t nb_pkts,__rte_unused void * user_param)380 tx_pkt_set_dynf(uint16_t port_id, __rte_unused uint16_t queue,
381 struct rte_mbuf *pkts[], uint16_t nb_pkts,
382 __rte_unused void *user_param)
383 {
384 uint16_t i = 0;
385
386 if (ports[port_id].mbuf_dynf)
387 for (i = 0; i < nb_pkts; i++)
388 pkts[i]->ol_flags |= ports[port_id].mbuf_dynf;
389 return nb_pkts;
390 }
391
392 void
add_tx_dynf_callback(portid_t portid)393 add_tx_dynf_callback(portid_t portid)
394 {
395 struct rte_eth_dev_info dev_info;
396 uint16_t queue;
397 int ret;
398
399 if (port_id_is_invalid(portid, ENABLED_WARN))
400 return;
401
402 ret = eth_dev_info_get_print_err(portid, &dev_info);
403 if (ret != 0)
404 return;
405
406 for (queue = 0; queue < dev_info.nb_tx_queues; queue++)
407 if (!ports[portid].tx_set_dynf_cb[queue])
408 ports[portid].tx_set_dynf_cb[queue] =
409 rte_eth_add_tx_callback(portid, queue,
410 tx_pkt_set_dynf, NULL);
411 }
412
413 void
remove_tx_dynf_callback(portid_t portid)414 remove_tx_dynf_callback(portid_t portid)
415 {
416 struct rte_eth_dev_info dev_info;
417 uint16_t queue;
418 int ret;
419
420 if (port_id_is_invalid(portid, ENABLED_WARN))
421 return;
422
423 ret = eth_dev_info_get_print_err(portid, &dev_info);
424 if (ret != 0)
425 return;
426
427 for (queue = 0; queue < dev_info.nb_tx_queues; queue++)
428 if (ports[portid].tx_set_dynf_cb[queue]) {
429 rte_eth_remove_tx_callback(portid, queue,
430 ports[portid].tx_set_dynf_cb[queue]);
431 ports[portid].tx_set_dynf_cb[queue] = NULL;
432 }
433 }
434
435 int
eth_dev_info_get_print_err(uint16_t port_id,struct rte_eth_dev_info * dev_info)436 eth_dev_info_get_print_err(uint16_t port_id,
437 struct rte_eth_dev_info *dev_info)
438 {
439 int ret;
440
441 ret = rte_eth_dev_info_get(port_id, dev_info);
442 if (ret != 0)
443 fprintf(stderr,
444 "Error during getting device (port %u) info: %s\n",
445 port_id, strerror(-ret));
446
447 return ret;
448 }
449
450 int
eth_dev_conf_get_print_err(uint16_t port_id,struct rte_eth_conf * dev_conf)451 eth_dev_conf_get_print_err(uint16_t port_id, struct rte_eth_conf *dev_conf)
452 {
453 int ret;
454
455 ret = rte_eth_dev_conf_get(port_id, dev_conf);
456 if (ret != 0)
457 fprintf(stderr,
458 "Error during getting device configuration (port %u): %s\n",
459 port_id, strerror(-ret));
460
461 return ret;
462 }
463
464 void
eth_set_promisc_mode(uint16_t port,int enable)465 eth_set_promisc_mode(uint16_t port, int enable)
466 {
467 int ret;
468
469 if (enable)
470 ret = rte_eth_promiscuous_enable(port);
471 else
472 ret = rte_eth_promiscuous_disable(port);
473
474 if (ret != 0)
475 fprintf(stderr,
476 "Error during %s promiscuous mode for port %u: %s\n",
477 enable ? "enabling" : "disabling",
478 port, rte_strerror(-ret));
479 }
480
481 void
eth_set_allmulticast_mode(uint16_t port,int enable)482 eth_set_allmulticast_mode(uint16_t port, int enable)
483 {
484 int ret;
485
486 if (enable)
487 ret = rte_eth_allmulticast_enable(port);
488 else
489 ret = rte_eth_allmulticast_disable(port);
490
491 if (ret != 0)
492 fprintf(stderr,
493 "Error during %s all-multicast mode for port %u: %s\n",
494 enable ? "enabling" : "disabling",
495 port, rte_strerror(-ret));
496 }
497
498 int
eth_link_get_nowait_print_err(uint16_t port_id,struct rte_eth_link * link)499 eth_link_get_nowait_print_err(uint16_t port_id, struct rte_eth_link *link)
500 {
501 int ret;
502
503 ret = rte_eth_link_get_nowait(port_id, link);
504 if (ret < 0)
505 fprintf(stderr,
506 "Device (port %u) link get (without wait) failed: %s\n",
507 port_id, rte_strerror(-ret));
508
509 return ret;
510 }
511
512 int
eth_macaddr_get_print_err(uint16_t port_id,struct rte_ether_addr * mac_addr)513 eth_macaddr_get_print_err(uint16_t port_id, struct rte_ether_addr *mac_addr)
514 {
515 int ret;
516
517 ret = rte_eth_macaddr_get(port_id, mac_addr);
518 if (ret != 0)
519 fprintf(stderr,
520 "Error getting device (port %u) mac address: %s\n",
521 port_id, rte_strerror(-ret));
522
523 return ret;
524 }
525