1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
3 * Copyright(c) 2018 Mellanox Technology
10 #include <rte_ether.h>
11 #include <rte_ethdev.h>
17 print_ether_addr(const char *what, const struct rte_ether_addr *eth_addr)
19 char buf[RTE_ETHER_ADDR_FMT_SIZE];
20 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
21 printf("%s%s", what, buf);
25 dump_pkt_burst(uint16_t port_id, uint16_t queue, struct rte_mbuf *pkts[],
26 uint16_t nb_pkts, int is_rx)
29 const struct rte_ether_hdr *eth_hdr;
30 struct rte_ether_hdr _eth_hdr;
33 uint16_t i, packet_type;
34 uint16_t is_encapsulation;
36 struct rte_net_hdr_lens hdr_lens;
37 uint32_t sw_packet_type;
44 printf("port %u/queue %u: %s %u packets\n",
46 is_rx ? "received" : "sent",
47 (unsigned int) nb_pkts);
48 for (i = 0; i < nb_pkts; i++) {
50 eth_hdr = rte_pktmbuf_read(mb, 0, sizeof(_eth_hdr), &_eth_hdr);
51 eth_type = RTE_BE_TO_CPU_16(eth_hdr->ether_type);
52 ol_flags = mb->ol_flags;
53 packet_type = mb->packet_type;
54 is_encapsulation = RTE_ETH_IS_TUNNEL_PKT(packet_type);
56 print_ether_addr(" src=", ð_hdr->s_addr);
57 print_ether_addr(" - dst=", ð_hdr->d_addr);
58 printf(" - type=0x%04x - length=%u - nb_segs=%d",
59 eth_type, (unsigned int) mb->pkt_len,
61 if (ol_flags & PKT_RX_RSS_HASH) {
62 printf(" - RSS hash=0x%x", (unsigned int) mb->hash.rss);
63 printf(" - RSS queue=0x%x", (unsigned int) queue);
65 if (ol_flags & PKT_RX_FDIR) {
66 printf(" - FDIR matched ");
67 if (ol_flags & PKT_RX_FDIR_ID)
70 else if (ol_flags & PKT_RX_FDIR_FLX)
71 printf("flex bytes=0x%08x %08x",
72 mb->hash.fdir.hi, mb->hash.fdir.lo);
74 printf("hash=0x%x ID=0x%x ",
75 mb->hash.fdir.hash, mb->hash.fdir.id);
77 if (ol_flags & PKT_RX_TIMESTAMP)
78 printf(" - timestamp %"PRIu64" ", mb->timestamp);
79 if (ol_flags & PKT_RX_QINQ)
80 printf(" - QinQ VLAN tci=0x%x, VLAN tci outer=0x%x",
81 mb->vlan_tci, mb->vlan_tci_outer);
82 else if (ol_flags & PKT_RX_VLAN)
83 printf(" - VLAN tci=0x%x", mb->vlan_tci);
84 if (mb->packet_type) {
85 rte_get_ptype_name(mb->packet_type, buf, sizeof(buf));
86 printf(" - hw ptype: %s", buf);
88 sw_packet_type = rte_net_get_ptype(mb, &hdr_lens,
90 rte_get_ptype_name(sw_packet_type, buf, sizeof(buf));
91 printf(" - sw ptype: %s", buf);
92 if (sw_packet_type & RTE_PTYPE_L2_MASK)
93 printf(" - l2_len=%d", hdr_lens.l2_len);
94 if (sw_packet_type & RTE_PTYPE_L3_MASK)
95 printf(" - l3_len=%d", hdr_lens.l3_len);
96 if (sw_packet_type & RTE_PTYPE_L4_MASK)
97 printf(" - l4_len=%d", hdr_lens.l4_len);
98 if (sw_packet_type & RTE_PTYPE_TUNNEL_MASK)
99 printf(" - tunnel_len=%d", hdr_lens.tunnel_len);
100 if (sw_packet_type & RTE_PTYPE_INNER_L2_MASK)
101 printf(" - inner_l2_len=%d", hdr_lens.inner_l2_len);
102 if (sw_packet_type & RTE_PTYPE_INNER_L3_MASK)
103 printf(" - inner_l3_len=%d", hdr_lens.inner_l3_len);
104 if (sw_packet_type & RTE_PTYPE_INNER_L4_MASK)
105 printf(" - inner_l4_len=%d", hdr_lens.inner_l4_len);
106 if (is_encapsulation) {
107 struct rte_ipv4_hdr *ipv4_hdr;
108 struct rte_ipv6_hdr *ipv6_hdr;
109 struct rte_udp_hdr *udp_hdr;
114 struct rte_vxlan_hdr *vxlan_hdr;
116 l2_len = sizeof(struct rte_ether_hdr);
118 /* Do not support ipv4 option field */
119 if (RTE_ETH_IS_IPV4_HDR(packet_type)) {
120 l3_len = sizeof(struct rte_ipv4_hdr);
121 ipv4_hdr = rte_pktmbuf_mtod_offset(mb,
122 struct rte_ipv4_hdr *,
124 l4_proto = ipv4_hdr->next_proto_id;
126 l3_len = sizeof(struct rte_ipv6_hdr);
127 ipv6_hdr = rte_pktmbuf_mtod_offset(mb,
128 struct rte_ipv6_hdr *,
130 l4_proto = ipv6_hdr->proto;
132 if (l4_proto == IPPROTO_UDP) {
133 udp_hdr = rte_pktmbuf_mtod_offset(mb,
134 struct rte_udp_hdr *,
136 l4_len = sizeof(struct rte_udp_hdr);
137 vxlan_hdr = rte_pktmbuf_mtod_offset(mb,
138 struct rte_vxlan_hdr *,
139 l2_len + l3_len + l4_len);
140 udp_port = RTE_BE_TO_CPU_16(udp_hdr->dst_port);
141 vx_vni = rte_be_to_cpu_32(vxlan_hdr->vx_vni);
142 printf(" - VXLAN packet: packet type =%d, "
143 "Destination UDP port =%d, VNI = %d",
144 packet_type, udp_port, vx_vni >> 8);
147 printf(" - %s queue=0x%x", is_rx ? "Receive" : "Send",
148 (unsigned int) queue);
150 rte_get_rx_ol_flag_list(mb->ol_flags, buf, sizeof(buf));
151 printf(" ol_flags: %s\n", buf);
152 if (rte_mbuf_check(mb, 1, &reason) < 0)
153 printf("INVALID mbuf: %s\n", reason);
158 dump_rx_pkts(uint16_t port_id, uint16_t queue, struct rte_mbuf *pkts[],
159 uint16_t nb_pkts, __rte_unused uint16_t max_pkts,
160 __rte_unused void *user_param)
162 dump_pkt_burst(port_id, queue, pkts, nb_pkts, 1);
167 dump_tx_pkts(uint16_t port_id, uint16_t queue, struct rte_mbuf *pkts[],
168 uint16_t nb_pkts, __rte_unused void *user_param)
170 dump_pkt_burst(port_id, queue, pkts, nb_pkts, 0);
175 tx_pkt_set_md(uint16_t port_id, __rte_unused uint16_t queue,
176 struct rte_mbuf *pkts[], uint16_t nb_pkts,
177 __rte_unused void *user_param)
182 * Add metadata value to every Tx packet,
183 * and set ol_flags accordingly.
185 for (i = 0; i < nb_pkts; i++) {
186 pkts[i]->tx_metadata = ports[port_id].tx_metadata;
187 pkts[i]->ol_flags |= PKT_TX_METADATA;
193 add_tx_md_callback(portid_t portid)
195 struct rte_eth_dev_info dev_info;
199 if (port_id_is_invalid(portid, ENABLED_WARN))
202 ret = eth_dev_info_get_print_err(portid, &dev_info);
206 for (queue = 0; queue < dev_info.nb_tx_queues; queue++)
207 if (!ports[portid].tx_set_md_cb[queue])
208 ports[portid].tx_set_md_cb[queue] =
209 rte_eth_add_tx_callback(portid, queue,
210 tx_pkt_set_md, NULL);
214 remove_tx_md_callback(portid_t portid)
216 struct rte_eth_dev_info dev_info;
220 if (port_id_is_invalid(portid, ENABLED_WARN))
223 ret = eth_dev_info_get_print_err(portid, &dev_info);
227 for (queue = 0; queue < dev_info.nb_tx_queues; queue++)
228 if (ports[portid].tx_set_md_cb[queue]) {
229 rte_eth_remove_tx_callback(portid, queue,
230 ports[portid].tx_set_md_cb[queue]);
231 ports[portid].tx_set_md_cb[queue] = NULL;
236 eth_dev_info_get_print_err(uint16_t port_id,
237 struct rte_eth_dev_info *dev_info)
241 ret = rte_eth_dev_info_get(port_id, dev_info);
243 printf("Error during getting device (port %u) info: %s\n",
244 port_id, strerror(-ret));