xref: /f-stack/dpdk/drivers/net/mlx5/mlx5_rxtx_vec.c (revision 031be553)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2017 6WIND S.A.
5  *   Copyright 2017 Mellanox.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of 6WIND S.A. nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <assert.h>
35 #include <stdint.h>
36 #include <string.h>
37 #include <stdlib.h>
38 
39 /* Verbs header. */
40 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
41 #ifdef PEDANTIC
42 #pragma GCC diagnostic ignored "-Wpedantic"
43 #endif
44 #include <infiniband/verbs.h>
45 #include <infiniband/mlx5dv.h>
46 #ifdef PEDANTIC
47 #pragma GCC diagnostic error "-Wpedantic"
48 #endif
49 
50 #include <rte_mbuf.h>
51 #include <rte_mempool.h>
52 #include <rte_prefetch.h>
53 
54 #include "mlx5.h"
55 #include "mlx5_utils.h"
56 #include "mlx5_rxtx.h"
57 #include "mlx5_rxtx_vec.h"
58 #include "mlx5_autoconf.h"
59 #include "mlx5_defs.h"
60 #include "mlx5_prm.h"
61 
62 #if defined RTE_ARCH_X86_64
63 #include "mlx5_rxtx_vec_sse.h"
64 #elif defined RTE_ARCH_ARM64
65 #include "mlx5_rxtx_vec_neon.h"
66 #else
67 #error "This should not be compiled if SIMD instructions are not supported."
68 #endif
69 
70 /**
71  * Count the number of continuous single segment packets.
72  *
73  * @param pkts
74  *   Pointer to array of packets.
75  * @param pkts_n
76  *   Number of packets.
77  *
78  * @return
79  *   Number of continuous single segment packets.
80  */
81 static inline unsigned int
82 txq_check_multiseg(struct rte_mbuf **pkts, uint16_t pkts_n)
83 {
84 	unsigned int pos;
85 
86 	if (!pkts_n)
87 		return 0;
88 	/* Count the number of continuous single segment packets. */
89 	for (pos = 0; pos < pkts_n; ++pos)
90 		if (NB_SEGS(pkts[pos]) > 1)
91 			break;
92 	return pos;
93 }
94 
95 /**
96  * Count the number of packets having same ol_flags and calculate cs_flags.
97  *
98  * @param txq
99  *   Pointer to TX queue structure.
100  * @param pkts
101  *   Pointer to array of packets.
102  * @param pkts_n
103  *   Number of packets.
104  * @param cs_flags
105  *   Pointer of flags to be returned.
106  *
107  * @return
108  *   Number of packets having same ol_flags.
109  */
110 static inline unsigned int
111 txq_calc_offload(struct mlx5_txq_data *txq, struct rte_mbuf **pkts,
112 		 uint16_t pkts_n, uint8_t *cs_flags)
113 {
114 	unsigned int pos;
115 	const uint64_t ol_mask =
116 		PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM |
117 		PKT_TX_UDP_CKSUM | PKT_TX_TUNNEL_GRE |
118 		PKT_TX_TUNNEL_VXLAN | PKT_TX_OUTER_IP_CKSUM;
119 
120 	if (!pkts_n)
121 		return 0;
122 	/* Count the number of packets having same ol_flags. */
123 	for (pos = 1; pos < pkts_n; ++pos)
124 		if ((pkts[pos]->ol_flags ^ pkts[0]->ol_flags) & ol_mask)
125 			break;
126 	*cs_flags = txq_ol_cksum_to_cs(txq, pkts[0]);
127 	return pos;
128 }
129 
130 /**
131  * DPDK callback for vectorized TX.
132  *
133  * @param dpdk_txq
134  *   Generic pointer to TX queue structure.
135  * @param[in] pkts
136  *   Packets to transmit.
137  * @param pkts_n
138  *   Number of packets in array.
139  *
140  * @return
141  *   Number of packets successfully transmitted (<= pkts_n).
142  */
143 uint16_t
144 mlx5_tx_burst_raw_vec(void *dpdk_txq, struct rte_mbuf **pkts,
145 		      uint16_t pkts_n)
146 {
147 	struct mlx5_txq_data *txq = (struct mlx5_txq_data *)dpdk_txq;
148 	uint16_t nb_tx = 0;
149 
150 	while (pkts_n > nb_tx) {
151 		uint16_t n;
152 		uint16_t ret;
153 
154 		n = RTE_MIN((uint16_t)(pkts_n - nb_tx), MLX5_VPMD_TX_MAX_BURST);
155 		ret = txq_burst_v(txq, &pkts[nb_tx], n, 0);
156 		nb_tx += ret;
157 		if (!ret)
158 			break;
159 	}
160 	return nb_tx;
161 }
162 
163 /**
164  * DPDK callback for vectorized TX with multi-seg packets and offload.
165  *
166  * @param dpdk_txq
167  *   Generic pointer to TX queue structure.
168  * @param[in] pkts
169  *   Packets to transmit.
170  * @param pkts_n
171  *   Number of packets in array.
172  *
173  * @return
174  *   Number of packets successfully transmitted (<= pkts_n).
175  */
176 uint16_t
177 mlx5_tx_burst_vec(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
178 {
179 	struct mlx5_txq_data *txq = (struct mlx5_txq_data *)dpdk_txq;
180 	uint16_t nb_tx = 0;
181 
182 	while (pkts_n > nb_tx) {
183 		uint8_t cs_flags = 0;
184 		uint16_t n;
185 		uint16_t ret;
186 
187 		/* Transmit multi-seg packets in the head of pkts list. */
188 		if (!(txq->flags & ETH_TXQ_FLAGS_NOMULTSEGS) &&
189 		    NB_SEGS(pkts[nb_tx]) > 1)
190 			nb_tx += txq_scatter_v(txq,
191 					       &pkts[nb_tx],
192 					       pkts_n - nb_tx);
193 		n = RTE_MIN((uint16_t)(pkts_n - nb_tx), MLX5_VPMD_TX_MAX_BURST);
194 		if (!(txq->flags & ETH_TXQ_FLAGS_NOMULTSEGS))
195 			n = txq_check_multiseg(&pkts[nb_tx], n);
196 		if (!(txq->flags & ETH_TXQ_FLAGS_NOOFFLOADS))
197 			n = txq_calc_offload(txq, &pkts[nb_tx], n, &cs_flags);
198 		ret = txq_burst_v(txq, &pkts[nb_tx], n, cs_flags);
199 		nb_tx += ret;
200 		if (!ret)
201 			break;
202 	}
203 	return nb_tx;
204 }
205 
206 /**
207  * Skip error packets.
208  *
209  * @param rxq
210  *   Pointer to RX queue structure.
211  * @param[out] pkts
212  *   Array to store received packets.
213  * @param pkts_n
214  *   Maximum number of packets in array.
215  *
216  * @return
217  *   Number of packets successfully received (<= pkts_n).
218  */
219 static uint16_t
220 rxq_handle_pending_error(struct mlx5_rxq_data *rxq, struct rte_mbuf **pkts,
221 			 uint16_t pkts_n)
222 {
223 	uint16_t n = 0;
224 	unsigned int i;
225 #ifdef MLX5_PMD_SOFT_COUNTERS
226 	uint32_t err_bytes = 0;
227 #endif
228 
229 	for (i = 0; i < pkts_n; ++i) {
230 		struct rte_mbuf *pkt = pkts[i];
231 
232 		if (pkt->packet_type == RTE_PTYPE_ALL_MASK) {
233 #ifdef MLX5_PMD_SOFT_COUNTERS
234 			err_bytes += PKT_LEN(pkt);
235 #endif
236 			rte_pktmbuf_free_seg(pkt);
237 		} else {
238 			pkts[n++] = pkt;
239 		}
240 	}
241 	rxq->stats.idropped += (pkts_n - n);
242 #ifdef MLX5_PMD_SOFT_COUNTERS
243 	/* Correct counters of errored completions. */
244 	rxq->stats.ipackets -= (pkts_n - n);
245 	rxq->stats.ibytes -= err_bytes;
246 #endif
247 	return n;
248 }
249 
250 /**
251  * DPDK callback for vectorized RX.
252  *
253  * @param dpdk_rxq
254  *   Generic pointer to RX queue structure.
255  * @param[out] pkts
256  *   Array to store received packets.
257  * @param pkts_n
258  *   Maximum number of packets in array.
259  *
260  * @return
261  *   Number of packets successfully received (<= pkts_n).
262  */
263 uint16_t
264 mlx5_rx_burst_vec(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
265 {
266 	struct mlx5_rxq_data *rxq = dpdk_rxq;
267 	uint16_t nb_rx;
268 	uint64_t err = 0;
269 
270 	nb_rx = rxq_burst_v(rxq, pkts, pkts_n, &err);
271 	if (unlikely(err))
272 		nb_rx = rxq_handle_pending_error(rxq, pkts, nb_rx);
273 	return nb_rx;
274 }
275 
276 /**
277  * Check Tx queue flags are set for raw vectorized Tx.
278  *
279  * @param dev
280  *   Pointer to Ethernet device.
281  *
282  * @return
283  *   1 if supported, negative errno value if not.
284  */
285 int __attribute__((cold))
286 mlx5_check_raw_vec_tx_support(struct rte_eth_dev *dev)
287 {
288 	struct priv *priv = dev->data->dev_private;
289 	uint16_t i;
290 
291 	/* All the configured queues should support. */
292 	for (i = 0; i < priv->txqs_n; ++i) {
293 		struct mlx5_txq_data *txq = (*priv->txqs)[i];
294 
295 		if (!(txq->flags & ETH_TXQ_FLAGS_NOMULTSEGS) ||
296 		    !(txq->flags & ETH_TXQ_FLAGS_NOOFFLOADS))
297 			break;
298 	}
299 	if (i != priv->txqs_n)
300 		return -ENOTSUP;
301 	return 1;
302 }
303 
304 /**
305  * Check a device can support vectorized TX.
306  *
307  * @param dev
308  *   Pointer to Ethernet device.
309  *
310  * @return
311  *   1 if supported, negative errno value if not.
312  */
313 int __attribute__((cold))
314 mlx5_check_vec_tx_support(struct rte_eth_dev *dev)
315 {
316 	struct priv *priv = dev->data->dev_private;
317 
318 	if (!priv->tx_vec_en ||
319 	    priv->txqs_n > MLX5_VPMD_MIN_TXQS ||
320 	    priv->mps != MLX5_MPW_ENHANCED ||
321 	    priv->tso)
322 		return -ENOTSUP;
323 	return 1;
324 }
325 
326 /**
327  * Check a RX queue can support vectorized RX.
328  *
329  * @param rxq
330  *   Pointer to RX queue.
331  *
332  * @return
333  *   1 if supported, negative errno value if not.
334  */
335 int __attribute__((cold))
336 mlx5_rxq_check_vec_support(struct mlx5_rxq_data *rxq)
337 {
338 	struct mlx5_rxq_ctrl *ctrl =
339 		container_of(rxq, struct mlx5_rxq_ctrl, rxq);
340 
341 	if (!ctrl->priv->rx_vec_en || rxq->sges_n != 0)
342 		return -ENOTSUP;
343 	return 1;
344 }
345 
346 /**
347  * Check a device can support vectorized RX.
348  *
349  * @param dev
350  *   Pointer to Ethernet device.
351  *
352  * @return
353  *   1 if supported, negative errno value if not.
354  */
355 int __attribute__((cold))
356 mlx5_check_vec_rx_support(struct rte_eth_dev *dev)
357 {
358 	struct priv *priv = dev->data->dev_private;
359 	uint16_t i;
360 
361 	if (!priv->rx_vec_en)
362 		return -ENOTSUP;
363 	/* All the configured queues should support. */
364 	for (i = 0; i < priv->rxqs_n; ++i) {
365 		struct mlx5_rxq_data *rxq = (*priv->rxqs)[i];
366 
367 		if (!rxq)
368 			continue;
369 		if (mlx5_rxq_check_vec_support(rxq) < 0)
370 			break;
371 	}
372 	if (i != priv->rxqs_n)
373 		return -ENOTSUP;
374 	return 1;
375 }
376