1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
3 * Copyright 2018 Mellanox Technologies, Ltd
8 #include <rte_bitops.h>
11 #include <rte_ether.h>
12 #include <rte_vxlan.h>
13 #include <rte_ethdev.h>
18 #define MAX_STRING_LEN 8192
20 #define MKDUMPSTR(buf, buf_size, cur_len, ...) \
22 if (cur_len >= buf_size) \
24 cur_len += snprintf(buf + cur_len, buf_size - cur_len, __VA_ARGS__); \
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)
31 char buf[RTE_ETHER_ADDR_FMT_SIZE];
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);
38 is_timestamp_enabled(const struct rte_mbuf *mbuf)
40 static uint64_t timestamp_rx_dynflag;
41 int timestamp_rx_dynflag_offset;
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)
48 timestamp_rx_dynflag = RTE_BIT64(timestamp_rx_dynflag_offset);
51 return (mbuf->ol_flags & timestamp_rx_dynflag) != 0;
54 static inline rte_mbuf_timestamp_t
55 get_timestamp(const struct rte_mbuf *mbuf)
57 static int timestamp_dynfield_offset = -1;
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)
66 return *RTE_MBUF_DYNFIELD(mbuf,
67 timestamp_dynfield_offset, rte_mbuf_timestamp_t *);
71 dump_pkt_burst(uint16_t port_id, uint16_t queue, struct rte_mbuf *pkts[],
72 uint16_t nb_pkts, int is_rx)
75 const struct rte_ether_hdr *eth_hdr;
76 struct rte_ether_hdr _eth_hdr;
79 uint16_t i, packet_type;
80 uint16_t is_encapsulation;
82 struct rte_net_hdr_lens hdr_lens;
83 uint32_t sw_packet_type;
88 char print_buf[MAX_STRING_LEN];
89 size_t buf_size = MAX_STRING_LEN;
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++) {
99 struct rte_flow_error error;
100 struct rte_flow_restore_info info = { 0, };
101 struct rte_port *port = &ports[port_id];
105 MKDUMPSTR(print_buf, buf_size, cur_len, "port %u, ",
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);
112 ret = rte_flow_get_restore_info(port->flow_transfer_proxy,
115 MKDUMPSTR(print_buf, buf_size, cur_len,
117 if (info.flags & RTE_FLOW_RESTORE_INFO_TUNNEL) {
118 struct port_flow_tunnel *port_tunnel;
120 port_tunnel = port_flow_locate_tunnel
121 (port_id, &info.tunnel);
122 MKDUMPSTR(print_buf, buf_size, cur_len,
125 MKDUMPSTR(print_buf, buf_size, cur_len,
126 " #%u", port_tunnel->id);
128 MKDUMPSTR(print_buf, buf_size, cur_len,
130 MKDUMPSTR(print_buf, buf_size, cur_len,
131 " type %s", port_flow_tunnel_type
134 MKDUMPSTR(print_buf, buf_size, cur_len,
135 " - no tunnel info");
137 if (info.flags & RTE_FLOW_RESTORE_INFO_ENCAPSULATED)
138 MKDUMPSTR(print_buf, buf_size, cur_len,
139 " - outer header present");
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);
147 MKDUMPSTR(print_buf, buf_size, cur_len,
149 MKDUMPSTR(print_buf, buf_size, cur_len, "\n");
151 print_ether_addr(" src=", ð_hdr->src_addr,
152 print_buf, buf_size, &cur_len);
153 print_ether_addr(" - dst=", ð_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,
159 ol_flags = mb->ol_flags;
160 if (ol_flags & PKT_RX_RSS_HASH) {
161 MKDUMPSTR(print_buf, buf_size, cur_len,
163 (unsigned int) mb->hash.rss);
164 MKDUMPSTR(print_buf, buf_size, cur_len,
165 " - RSS queue=0x%x", (unsigned int) queue);
167 if (ol_flags & PKT_RX_FDIR) {
168 MKDUMPSTR(print_buf, buf_size, cur_len,
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);
178 MKDUMPSTR(print_buf, buf_size, cur_len,
179 "hash=0x%x ID=0x%x ",
180 mb->hash.fdir.hash, mb->hash.fdir.id);
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,
204 dynf_names[dynf_index],
205 !!(ol_flags & (1UL << dynf_index)));
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);
212 sw_packet_type = rte_net_get_ptype(mb, &hdr_lens,
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",
219 if (sw_packet_type & RTE_PTYPE_L3_MASK)
220 MKDUMPSTR(print_buf, buf_size, cur_len, " - l3_len=%d",
222 if (sw_packet_type & RTE_PTYPE_L4_MASK)
223 MKDUMPSTR(print_buf, buf_size, cur_len, " - l4_len=%d",
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;
245 struct rte_vxlan_hdr *vxlan_hdr;
247 l2_len = sizeof(struct rte_ether_hdr);
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 *,
255 l4_proto = ipv4_hdr->next_proto_id;
257 l3_len = sizeof(struct rte_ipv6_hdr);
258 ipv6_hdr = rte_pktmbuf_mtod_offset(mb,
259 struct rte_ipv6_hdr *,
261 l4_proto = ipv6_hdr->proto;
263 if (l4_proto == IPPROTO_UDP) {
264 udp_hdr = rte_pktmbuf_mtod_offset(mb,
265 struct rte_udp_hdr *,
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);
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");
285 rte_get_rx_ol_flag_list(mb->ol_flags, buf, sizeof(buf));
287 rte_get_tx_ol_flag_list(mb->ol_flags, buf, sizeof(buf));
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);
297 printf("%s", print_buf);
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)
307 dump_pkt_burst(port_id, queue, pkts, nb_pkts, 1);
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)
315 dump_pkt_burst(port_id, queue, pkts, nb_pkts, 0);
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)
327 * Add metadata value to every Tx packet,
328 * and set ol_flags accordingly.
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;
340 add_tx_md_callback(portid_t portid)
342 struct rte_eth_dev_info dev_info;
346 if (port_id_is_invalid(portid, ENABLED_WARN))
349 ret = eth_dev_info_get_print_err(portid, &dev_info);
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);
361 remove_tx_md_callback(portid_t portid)
363 struct rte_eth_dev_info dev_info;
367 if (port_id_is_invalid(portid, ENABLED_WARN))
370 ret = eth_dev_info_get_print_err(portid, &dev_info);
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;
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)
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;
396 add_tx_dynf_callback(portid_t portid)
398 struct rte_eth_dev_info dev_info;
402 if (port_id_is_invalid(portid, ENABLED_WARN))
405 ret = eth_dev_info_get_print_err(portid, &dev_info);
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);
417 remove_tx_dynf_callback(portid_t portid)
419 struct rte_eth_dev_info dev_info;
423 if (port_id_is_invalid(portid, ENABLED_WARN))
426 ret = eth_dev_info_get_print_err(portid, &dev_info);
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;
439 eth_dev_info_get_print_err(uint16_t port_id,
440 struct rte_eth_dev_info *dev_info)
444 ret = rte_eth_dev_info_get(port_id, dev_info);
447 "Error during getting device (port %u) info: %s\n",
448 port_id, strerror(-ret));
454 eth_dev_conf_get_print_err(uint16_t port_id, struct rte_eth_conf *dev_conf)
458 ret = rte_eth_dev_conf_get(port_id, dev_conf);
461 "Error during getting device configuration (port %u): %s\n",
462 port_id, strerror(-ret));
468 eth_set_promisc_mode(uint16_t port, int enable)
473 ret = rte_eth_promiscuous_enable(port);
475 ret = rte_eth_promiscuous_disable(port);
479 "Error during %s promiscuous mode for port %u: %s\n",
480 enable ? "enabling" : "disabling",
481 port, rte_strerror(-ret));
485 eth_set_allmulticast_mode(uint16_t port, int enable)
490 ret = rte_eth_allmulticast_enable(port);
492 ret = rte_eth_allmulticast_disable(port);
496 "Error during %s all-multicast mode for port %u: %s\n",
497 enable ? "enabling" : "disabling",
498 port, rte_strerror(-ret));
502 eth_link_get_nowait_print_err(uint16_t port_id, struct rte_eth_link *link)
506 ret = rte_eth_link_get_nowait(port_id, link);
509 "Device (port %u) link get (without wait) failed: %s\n",
510 port_id, rte_strerror(-ret));
516 eth_macaddr_get_print_err(uint16_t port_id, struct rte_ether_addr *mac_addr)
520 ret = rte_eth_macaddr_get(port_id, mac_addr);
523 "Error getting device (port %u) mac address: %s\n",
524 port_id, rte_strerror(-ret));