f20531dc917bc21bbda31fb18d90632ef56d4482
[dpdk.git] / app / test-pmd / util.c
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_vxlan.h>
12 #include <rte_ethdev.h>
13 #include <rte_flow.h>
14
15 #include "testpmd.h"
16
17 static inline void
18 print_ether_addr(const char *what, const struct rte_ether_addr *eth_addr)
19 {
20         char buf[RTE_ETHER_ADDR_FMT_SIZE];
21         rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
22         printf("%s%s", what, buf);
23 }
24
25 static inline void
26 dump_pkt_burst(uint16_t port_id, uint16_t queue, struct rte_mbuf *pkts[],
27               uint16_t nb_pkts, int is_rx)
28 {
29         struct rte_mbuf  *mb;
30         const struct rte_ether_hdr *eth_hdr;
31         struct rte_ether_hdr _eth_hdr;
32         uint16_t eth_type;
33         uint64_t ol_flags;
34         uint16_t i, packet_type;
35         uint16_t is_encapsulation;
36         char buf[256];
37         struct rte_net_hdr_lens hdr_lens;
38         uint32_t sw_packet_type;
39         uint16_t udp_port;
40         uint32_t vx_vni;
41         const char *reason;
42
43         if (!nb_pkts)
44                 return;
45         printf("port %u/queue %u: %s %u packets\n",
46                 port_id, queue,
47                is_rx ? "received" : "sent",
48                (unsigned int) nb_pkts);
49         for (i = 0; i < nb_pkts; i++) {
50                 mb = pkts[i];
51                 eth_hdr = rte_pktmbuf_read(mb, 0, sizeof(_eth_hdr), &_eth_hdr);
52                 eth_type = RTE_BE_TO_CPU_16(eth_hdr->ether_type);
53                 ol_flags = mb->ol_flags;
54                 packet_type = mb->packet_type;
55                 is_encapsulation = RTE_ETH_IS_TUNNEL_PKT(packet_type);
56
57                 print_ether_addr("  src=", &eth_hdr->s_addr);
58                 print_ether_addr(" - dst=", &eth_hdr->d_addr);
59                 printf(" - type=0x%04x - length=%u - nb_segs=%d",
60                        eth_type, (unsigned int) mb->pkt_len,
61                        (int)mb->nb_segs);
62                 if (ol_flags & PKT_RX_RSS_HASH) {
63                         printf(" - RSS hash=0x%x", (unsigned int) mb->hash.rss);
64                         printf(" - RSS queue=0x%x", (unsigned int) queue);
65                 }
66                 if (ol_flags & PKT_RX_FDIR) {
67                         printf(" - FDIR matched ");
68                         if (ol_flags & PKT_RX_FDIR_ID)
69                                 printf("ID=0x%x",
70                                        mb->hash.fdir.hi);
71                         else if (ol_flags & PKT_RX_FDIR_FLX)
72                                 printf("flex bytes=0x%08x %08x",
73                                        mb->hash.fdir.hi, mb->hash.fdir.lo);
74                         else
75                                 printf("hash=0x%x ID=0x%x ",
76                                        mb->hash.fdir.hash, mb->hash.fdir.id);
77                 }
78                 if (ol_flags & PKT_RX_TIMESTAMP)
79                         printf(" - timestamp %"PRIu64" ", mb->timestamp);
80                 if (ol_flags & PKT_RX_QINQ)
81                         printf(" - QinQ VLAN tci=0x%x, VLAN tci outer=0x%x",
82                                mb->vlan_tci, mb->vlan_tci_outer);
83                 else if (ol_flags & PKT_RX_VLAN)
84                         printf(" - VLAN tci=0x%x", mb->vlan_tci);
85                 if (mb->packet_type) {
86                         rte_get_ptype_name(mb->packet_type, buf, sizeof(buf));
87                         printf(" - hw ptype: %s", buf);
88                 }
89                 sw_packet_type = rte_net_get_ptype(mb, &hdr_lens,
90                                         RTE_PTYPE_ALL_MASK);
91                 rte_get_ptype_name(sw_packet_type, buf, sizeof(buf));
92                 printf(" - sw ptype: %s", buf);
93                 if (sw_packet_type & RTE_PTYPE_L2_MASK)
94                         printf(" - l2_len=%d", hdr_lens.l2_len);
95                 if (sw_packet_type & RTE_PTYPE_L3_MASK)
96                         printf(" - l3_len=%d", hdr_lens.l3_len);
97                 if (sw_packet_type & RTE_PTYPE_L4_MASK)
98                         printf(" - l4_len=%d", hdr_lens.l4_len);
99                 if (sw_packet_type & RTE_PTYPE_TUNNEL_MASK)
100                         printf(" - tunnel_len=%d", hdr_lens.tunnel_len);
101                 if (sw_packet_type & RTE_PTYPE_INNER_L2_MASK)
102                         printf(" - inner_l2_len=%d", hdr_lens.inner_l2_len);
103                 if (sw_packet_type & RTE_PTYPE_INNER_L3_MASK)
104                         printf(" - inner_l3_len=%d", hdr_lens.inner_l3_len);
105                 if (sw_packet_type & RTE_PTYPE_INNER_L4_MASK)
106                         printf(" - inner_l4_len=%d", hdr_lens.inner_l4_len);
107                 if (is_encapsulation) {
108                         struct rte_ipv4_hdr *ipv4_hdr;
109                         struct rte_ipv6_hdr *ipv6_hdr;
110                         struct rte_udp_hdr *udp_hdr;
111                         uint8_t l2_len;
112                         uint8_t l3_len;
113                         uint8_t l4_len;
114                         uint8_t l4_proto;
115                         struct  rte_vxlan_hdr *vxlan_hdr;
116
117                         l2_len  = sizeof(struct rte_ether_hdr);
118
119                         /* Do not support ipv4 option field */
120                         if (RTE_ETH_IS_IPV4_HDR(packet_type)) {
121                                 l3_len = sizeof(struct rte_ipv4_hdr);
122                                 ipv4_hdr = rte_pktmbuf_mtod_offset(mb,
123                                 struct rte_ipv4_hdr *,
124                                 l2_len);
125                                 l4_proto = ipv4_hdr->next_proto_id;
126                         } else {
127                                 l3_len = sizeof(struct rte_ipv6_hdr);
128                                 ipv6_hdr = rte_pktmbuf_mtod_offset(mb,
129                                 struct rte_ipv6_hdr *,
130                                 l2_len);
131                                 l4_proto = ipv6_hdr->proto;
132                         }
133                         if (l4_proto == IPPROTO_UDP) {
134                                 udp_hdr = rte_pktmbuf_mtod_offset(mb,
135                                 struct rte_udp_hdr *,
136                                 l2_len + l3_len);
137                                 l4_len = sizeof(struct rte_udp_hdr);
138                                 vxlan_hdr = rte_pktmbuf_mtod_offset(mb,
139                                 struct rte_vxlan_hdr *,
140                                 l2_len + l3_len + l4_len);
141                                 udp_port = RTE_BE_TO_CPU_16(udp_hdr->dst_port);
142                                 vx_vni = rte_be_to_cpu_32(vxlan_hdr->vx_vni);
143                                 printf(" - VXLAN packet: packet type =%d, "
144                                        "Destination UDP port =%d, VNI = %d",
145                                        packet_type, udp_port, vx_vni >> 8);
146                         }
147                 }
148                 printf(" - %s queue=0x%x", is_rx ? "Receive" : "Send",
149                         (unsigned int) queue);
150                 printf("\n");
151                 rte_get_rx_ol_flag_list(mb->ol_flags, buf, sizeof(buf));
152                 printf("  ol_flags: %s\n", buf);
153                 if (rte_mbuf_check(mb, 1, &reason) < 0)
154                         printf("INVALID mbuf: %s\n", reason);
155         }
156 }
157
158 uint16_t
159 dump_rx_pkts(uint16_t port_id, uint16_t queue, struct rte_mbuf *pkts[],
160              uint16_t nb_pkts, __rte_unused uint16_t max_pkts,
161              __rte_unused void *user_param)
162 {
163         dump_pkt_burst(port_id, queue, pkts, nb_pkts, 1);
164         return nb_pkts;
165 }
166
167 uint16_t
168 dump_tx_pkts(uint16_t port_id, uint16_t queue, struct rte_mbuf *pkts[],
169              uint16_t nb_pkts, __rte_unused void *user_param)
170 {
171         dump_pkt_burst(port_id, queue, pkts, nb_pkts, 0);
172         return nb_pkts;
173 }
174
175 uint16_t
176 tx_pkt_set_md(uint16_t port_id, __rte_unused uint16_t queue,
177               struct rte_mbuf *pkts[], uint16_t nb_pkts,
178               __rte_unused void *user_param)
179 {
180         uint16_t i = 0;
181
182         /*
183          * Add metadata value to every Tx packet,
184          * and set ol_flags accordingly.
185          */
186         for (i = 0; i < nb_pkts; i++) {
187                 pkts[i]->tx_metadata = ports[port_id].tx_metadata;
188                 pkts[i]->ol_flags |= PKT_TX_METADATA;
189         }
190         return nb_pkts;
191 }
192
193 void
194 add_tx_md_callback(portid_t portid)
195 {
196         struct rte_eth_dev_info dev_info;
197         uint16_t queue;
198         int ret;
199
200         if (port_id_is_invalid(portid, ENABLED_WARN))
201                 return;
202
203         ret = eth_dev_info_get_print_err(portid, &dev_info);
204         if (ret != 0)
205                 return;
206
207         for (queue = 0; queue < dev_info.nb_tx_queues; queue++)
208                 if (!ports[portid].tx_set_md_cb[queue])
209                         ports[portid].tx_set_md_cb[queue] =
210                                 rte_eth_add_tx_callback(portid, queue,
211                                                         tx_pkt_set_md, NULL);
212 }
213
214 void
215 remove_tx_md_callback(portid_t portid)
216 {
217         struct rte_eth_dev_info dev_info;
218         uint16_t queue;
219         int ret;
220
221         if (port_id_is_invalid(portid, ENABLED_WARN))
222                 return;
223
224         ret = eth_dev_info_get_print_err(portid, &dev_info);
225         if (ret != 0)
226                 return;
227
228         for (queue = 0; queue < dev_info.nb_tx_queues; queue++)
229                 if (ports[portid].tx_set_md_cb[queue]) {
230                         rte_eth_remove_tx_callback(portid, queue,
231                                 ports[portid].tx_set_md_cb[queue]);
232                         ports[portid].tx_set_md_cb[queue] = NULL;
233                 }
234 }
235
236 int
237 eth_dev_info_get_print_err(uint16_t port_id,
238                                         struct rte_eth_dev_info *dev_info)
239 {
240         int ret;
241
242         ret = rte_eth_dev_info_get(port_id, dev_info);
243         if (ret != 0)
244                 printf("Error during getting device (port %u) info: %s\n",
245                                 port_id, strerror(-ret));
246
247         return ret;
248 }
249
250 void
251 eth_set_promisc_mode(uint16_t port, int enable)
252 {
253         int ret;
254
255         if (enable)
256                 ret = rte_eth_promiscuous_enable(port);
257         else
258                 ret = rte_eth_promiscuous_disable(port);
259
260         if (ret != 0)
261                 printf("Error during %s promiscuous mode for port %u: %s\n",
262                         enable ? "enabling" : "disabling",
263                         port, rte_strerror(-ret));
264 }
265
266 void
267 eth_set_allmulticast_mode(uint16_t port, int enable)
268 {
269         int ret;
270
271         if (enable)
272                 ret = rte_eth_allmulticast_enable(port);
273         else
274                 ret = rte_eth_allmulticast_disable(port);
275
276         if (ret != 0)
277                 printf("Error during %s all-multicast mode for port %u: %s\n",
278                         enable ? "enabling" : "disabling",
279                         port, rte_strerror(-ret));
280 }
281
282 int
283 eth_link_get_nowait_print_err(uint16_t port_id, struct rte_eth_link *link)
284 {
285         int ret;
286
287         ret = rte_eth_link_get_nowait(port_id, link);
288         if (ret < 0)
289                 printf("Device (port %u) link get (without wait) failed: %s\n",
290                         port_id, rte_strerror(-ret));
291
292         return ret;
293 }
294
295 int
296 eth_macaddr_get_print_err(uint16_t port_id, struct rte_ether_addr *mac_addr)
297 {
298         int ret;
299
300         ret = rte_eth_macaddr_get(port_id, mac_addr);
301         if (ret != 0)
302                 printf("Error getting device (port %u) mac address: %s\n",
303                                 port_id, rte_strerror(-ret));
304
305         return ret;
306 }