1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2017 6WIND S.A.
3 * Copyright 2017 Mellanox Technologies, Ltd
11 #include <rte_mempool.h>
12 #include <rte_prefetch.h>
15 #include <mlx5_glue.h>
18 #include "mlx5_defs.h"
20 #include "mlx5_utils.h"
21 #include "mlx5_rxtx.h"
23 #include "mlx5_rxtx_vec.h"
24 #include "mlx5_autoconf.h"
26 #if defined RTE_ARCH_X86_64
27 #include "mlx5_rxtx_vec_sse.h"
28 #elif defined RTE_ARCH_ARM64
29 #include "mlx5_rxtx_vec_neon.h"
30 #elif defined RTE_ARCH_PPC_64
31 #include "mlx5_rxtx_vec_altivec.h"
33 #error "This should not be compiled if SIMD instructions are not supported."
40 * Pointer to RX queue structure.
42 * Array to store received packets.
44 * Maximum number of packets in array.
47 * Number of packets successfully received (<= pkts_n).
50 rxq_handle_pending_error(struct mlx5_rxq_data *rxq, struct rte_mbuf **pkts,
55 #ifdef MLX5_PMD_SOFT_COUNTERS
56 uint32_t err_bytes = 0;
59 for (i = 0; i < pkts_n; ++i) {
60 struct rte_mbuf *pkt = pkts[i];
62 if (pkt->packet_type == RTE_PTYPE_ALL_MASK || rxq->err_state) {
63 #ifdef MLX5_PMD_SOFT_COUNTERS
64 err_bytes += PKT_LEN(pkt);
66 rte_pktmbuf_free_seg(pkt);
71 rxq->stats.idropped += (pkts_n - n);
72 #ifdef MLX5_PMD_SOFT_COUNTERS
73 /* Correct counters of errored completions. */
74 rxq->stats.ipackets -= (pkts_n - n);
75 rxq->stats.ibytes -= err_bytes;
77 mlx5_rx_err_handle(rxq, 1);
82 * Replenish buffers for RX in bulk.
85 * Pointer to RX queue structure.
88 mlx5_rx_replenish_bulk_mbuf(struct mlx5_rxq_data *rxq)
90 const uint16_t q_n = 1 << rxq->elts_n;
91 const uint16_t q_mask = q_n - 1;
92 uint16_t n = q_n - (rxq->rq_ci - rxq->rq_pi);
93 uint16_t elts_idx = rxq->rq_ci & q_mask;
94 struct rte_mbuf **elts = &(*rxq->elts)[elts_idx];
95 volatile struct mlx5_wqe_data_seg *wq =
96 &((volatile struct mlx5_wqe_data_seg *)rxq->wqes)[elts_idx];
99 if (n >= rxq->rq_repl_thresh) {
100 MLX5_ASSERT(n >= MLX5_VPMD_RXQ_RPLNSH_THRESH(q_n));
101 MLX5_ASSERT(MLX5_VPMD_RXQ_RPLNSH_THRESH(q_n) >
102 MLX5_VPMD_DESCS_PER_LOOP);
103 /* Not to cross queue end. */
104 n = RTE_MIN(n - MLX5_VPMD_DESCS_PER_LOOP, q_n - elts_idx);
105 if (rte_mempool_get_bulk(rxq->mp, (void *)elts, n) < 0) {
106 rxq->stats.rx_nombuf += n;
109 if (unlikely(mlx5_mr_btree_len(&rxq->mr_ctrl.cache_bh) > 1)) {
110 for (i = 0; i < n; ++i) {
112 * In order to support the mbufs with external attached
113 * data buffer we should use the buf_addr pointer
114 * instead of rte_mbuf_buf_addr(). It touches the mbuf
115 * itself and may impact the performance.
117 void *buf_addr = elts[i]->buf_addr;
119 wq[i].addr = rte_cpu_to_be_64((uintptr_t)buf_addr +
120 RTE_PKTMBUF_HEADROOM);
121 wq[i].lkey = mlx5_rx_mb2mr(rxq, elts[i]);
124 for (i = 0; i < n; ++i) {
125 void *buf_addr = elts[i]->buf_addr;
127 wq[i].addr = rte_cpu_to_be_64((uintptr_t)buf_addr +
128 RTE_PKTMBUF_HEADROOM);
132 /* Prevent overflowing into consumed mbufs. */
133 elts_idx = rxq->rq_ci & q_mask;
134 for (i = 0; i < MLX5_VPMD_DESCS_PER_LOOP; ++i)
135 (*rxq->elts)[elts_idx + i] = &rxq->fake_mbuf;
137 *rxq->rq_db = rte_cpu_to_be_32(rxq->rq_ci);
142 * Replenish buffers for MPRQ RX in bulk.
145 * Pointer to RX queue structure.
148 mlx5_rx_mprq_replenish_bulk_mbuf(struct mlx5_rxq_data *rxq)
150 const uint16_t wqe_n = 1 << rxq->elts_n;
151 const uint32_t strd_n = 1 << rxq->strd_num_n;
152 const uint32_t elts_n = wqe_n * strd_n;
153 const uint32_t wqe_mask = elts_n - 1;
154 uint32_t n = elts_n - (rxq->elts_ci - rxq->rq_pi);
155 uint32_t elts_idx = rxq->elts_ci & wqe_mask;
156 struct rte_mbuf **elts = &(*rxq->elts)[elts_idx];
159 if (n >= rxq->rq_repl_thresh &&
160 rxq->elts_ci - rxq->rq_pi <= rxq->rq_repl_thresh) {
161 MLX5_ASSERT(n >= MLX5_VPMD_RXQ_RPLNSH_THRESH(elts_n));
162 MLX5_ASSERT(MLX5_VPMD_RXQ_RPLNSH_THRESH(elts_n) >
163 MLX5_VPMD_DESCS_PER_LOOP);
164 /* Not to cross queue end. */
165 n = RTE_MIN(n - MLX5_VPMD_DESCS_PER_LOOP, elts_n - elts_idx);
166 /* Limit replenish number to threshold value. */
167 n = RTE_MIN(n, rxq->rq_repl_thresh);
168 if (rte_mempool_get_bulk(rxq->mp, (void *)elts, n) < 0) {
169 rxq->stats.rx_nombuf += n;
173 /* Prevent overflowing into consumed mbufs. */
174 elts_idx = rxq->elts_ci & wqe_mask;
175 for (i = 0; i < MLX5_VPMD_DESCS_PER_LOOP; ++i)
176 (*rxq->elts)[elts_idx + i] = &rxq->fake_mbuf;
181 * Copy or attach MPRQ buffers to RX SW ring.
184 * Pointer to RX queue structure.
186 * Pointer to array of packets to be stored.
188 * Number of packets to be stored.
191 * Number of packets successfully copied/attached (<= pkts_n).
193 static inline uint16_t
194 rxq_copy_mprq_mbuf_v(struct mlx5_rxq_data *rxq,
195 struct rte_mbuf **pkts, uint16_t pkts_n)
197 const uint16_t wqe_n = 1 << rxq->elts_n;
198 const uint16_t wqe_mask = wqe_n - 1;
199 const uint16_t strd_sz = 1 << rxq->strd_sz_n;
200 const uint32_t strd_n = 1 << rxq->strd_num_n;
201 const uint32_t elts_n = wqe_n * strd_n;
202 const uint32_t elts_mask = elts_n - 1;
203 uint32_t elts_idx = rxq->rq_pi & elts_mask;
204 struct rte_mbuf **elts = &(*rxq->elts)[elts_idx];
205 uint32_t rq_ci = rxq->rq_ci;
206 struct mlx5_mprq_buf *buf = (*rxq->mprq_bufs)[rq_ci & wqe_mask];
210 for (i = 0; i < pkts_n; ++i) {
212 enum mlx5_rqx_code rxq_code;
214 if (rxq->consumed_strd == strd_n) {
215 /* Replace WQE if the buffer is still in use. */
216 mprq_buf_replace(rxq, rq_ci & wqe_mask);
217 /* Advance to the next WQE. */
218 rxq->consumed_strd = 0;
220 buf = (*rxq->mprq_bufs)[rq_ci & wqe_mask];
223 if (!elts[i]->pkt_len) {
224 rxq->consumed_strd = strd_n;
225 rte_pktmbuf_free_seg(elts[i]);
226 #ifdef MLX5_PMD_SOFT_COUNTERS
227 rxq->stats.ipackets -= 1;
231 strd_cnt = (elts[i]->pkt_len / strd_sz) +
232 ((elts[i]->pkt_len % strd_sz) ? 1 : 0);
233 rxq_code = mprq_buf_to_pkt(rxq, elts[i], elts[i]->pkt_len,
234 buf, rxq->consumed_strd, strd_cnt);
235 rxq->consumed_strd += strd_cnt;
236 if (unlikely(rxq_code != MLX5_RXQ_CODE_EXIT)) {
237 rte_pktmbuf_free_seg(elts[i]);
238 #ifdef MLX5_PMD_SOFT_COUNTERS
239 rxq->stats.ipackets -= 1;
240 rxq->stats.ibytes -= elts[i]->pkt_len;
242 if (rxq_code == MLX5_RXQ_CODE_NOMBUF) {
243 ++rxq->stats.rx_nombuf;
246 if (rxq_code == MLX5_RXQ_CODE_DROPPED) {
247 ++rxq->stats.idropped;
251 pkts[copied++] = elts[i];
256 *rxq->cq_db = rte_cpu_to_be_32(rxq->cq_ci);
257 if (rq_ci != rxq->rq_ci) {
260 *rxq->rq_db = rte_cpu_to_be_32(rxq->rq_ci);
266 * Receive burst of packets. An errored completion also consumes a mbuf, but the
267 * packet_type is set to be RTE_PTYPE_ALL_MASK. Marked mbufs should be freed
268 * before returning to application.
271 * Pointer to RX queue structure.
273 * Array to store received packets.
275 * Maximum number of packets in array.
277 * Pointer to a flag. Set non-zero value if pkts array has at least one error
280 * Pointer to a boolean. Set true if no new CQE seen.
283 * Number of packets received including errors (<= pkts_n).
285 static inline uint16_t
286 rxq_burst_v(struct mlx5_rxq_data *rxq, struct rte_mbuf **pkts,
287 uint16_t pkts_n, uint64_t *err, bool *no_cq)
289 const uint16_t q_n = 1 << rxq->cqe_n;
290 const uint16_t q_mask = q_n - 1;
291 const uint16_t e_n = 1 << rxq->elts_n;
292 const uint16_t e_mask = e_n - 1;
293 volatile struct mlx5_cqe *cq;
294 struct rte_mbuf **elts;
295 uint64_t comp_idx = MLX5_VPMD_DESCS_PER_LOOP;
296 uint16_t nocmp_n = 0;
297 uint16_t rcvd_pkt = 0;
298 unsigned int cq_idx = rxq->cq_ci & q_mask;
299 unsigned int elts_idx;
301 MLX5_ASSERT(rxq->sges_n == 0);
302 MLX5_ASSERT(rxq->cqe_n == rxq->elts_n);
303 cq = &(*rxq->cqes)[cq_idx];
305 rte_prefetch0(cq + 1);
306 rte_prefetch0(cq + 2);
307 rte_prefetch0(cq + 3);
308 pkts_n = RTE_MIN(pkts_n, MLX5_VPMD_RX_MAX_BURST);
309 mlx5_rx_replenish_bulk_mbuf(rxq);
310 /* See if there're unreturned mbufs from compressed CQE. */
311 rcvd_pkt = rxq->decompressed;
313 rcvd_pkt = RTE_MIN(rcvd_pkt, pkts_n);
314 rxq_copy_mbuf_v(&(*rxq->elts)[rxq->rq_pi & e_mask],
316 rxq->rq_pi += rcvd_pkt;
317 rxq->decompressed -= rcvd_pkt;
320 elts_idx = rxq->rq_pi & e_mask;
321 elts = &(*rxq->elts)[elts_idx];
322 /* Not to overflow pkts array. */
323 pkts_n = RTE_ALIGN_FLOOR(pkts_n - rcvd_pkt, MLX5_VPMD_DESCS_PER_LOOP);
324 /* Not to cross queue end. */
325 pkts_n = RTE_MIN(pkts_n, q_n - elts_idx);
326 pkts_n = RTE_MIN(pkts_n, q_n - cq_idx);
331 /* At this point, there shouldn't be any remaining packets. */
332 MLX5_ASSERT(rxq->decompressed == 0);
333 /* Process all the CQEs */
334 nocmp_n = rxq_cq_process_v(rxq, cq, elts, pkts, pkts_n, err, &comp_idx);
335 /* If no new CQE seen, return without updating cq_db. */
336 if (unlikely(!nocmp_n && comp_idx == MLX5_VPMD_DESCS_PER_LOOP)) {
340 /* Update the consumer indexes for non-compressed CQEs. */
341 MLX5_ASSERT(nocmp_n <= pkts_n);
342 rxq->cq_ci += nocmp_n;
343 rxq->rq_pi += nocmp_n;
345 /* Decompress the last CQE if compressed. */
346 if (comp_idx < MLX5_VPMD_DESCS_PER_LOOP) {
347 MLX5_ASSERT(comp_idx == (nocmp_n % MLX5_VPMD_DESCS_PER_LOOP));
348 rxq->decompressed = rxq_cq_decompress_v(rxq, &cq[nocmp_n],
350 rxq->cq_ci += rxq->decompressed;
351 /* Return more packets if needed. */
352 if (nocmp_n < pkts_n) {
353 uint16_t n = rxq->decompressed;
355 n = RTE_MIN(n, pkts_n - nocmp_n);
356 rxq_copy_mbuf_v(&(*rxq->elts)[rxq->rq_pi & e_mask],
360 rxq->decompressed -= n;
364 *rxq->cq_db = rte_cpu_to_be_32(rxq->cq_ci);
370 * DPDK callback for vectorized RX.
373 * Generic pointer to RX queue structure.
375 * Array to store received packets.
377 * Maximum number of packets in array.
380 * Number of packets successfully received (<= pkts_n).
383 mlx5_rx_burst_vec(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
385 struct mlx5_rxq_data *rxq = dpdk_rxq;
392 nb_rx = rxq_burst_v(rxq, pkts + tn, pkts_n - tn,
394 if (unlikely(err | rxq->err_state))
395 nb_rx = rxq_handle_pending_error(rxq, pkts + tn, nb_rx);
399 } while (tn != pkts_n);
404 * Receive burst of packets. An errored completion also consumes a mbuf, but the
405 * packet_type is set to be RTE_PTYPE_ALL_MASK. Marked mbufs should be freed
406 * before returning to application.
409 * Pointer to RX queue structure.
411 * Array to store received packets.
413 * Maximum number of packets in array.
415 * Pointer to a flag. Set non-zero value if pkts array has at least one error
418 * Pointer to a boolean. Set true if no new CQE seen.
421 * Number of packets received including errors (<= pkts_n).
423 static inline uint16_t
424 rxq_burst_mprq_v(struct mlx5_rxq_data *rxq, struct rte_mbuf **pkts,
425 uint16_t pkts_n, uint64_t *err, bool *no_cq)
427 const uint16_t q_n = 1 << rxq->cqe_n;
428 const uint16_t q_mask = q_n - 1;
429 const uint16_t wqe_n = 1 << rxq->elts_n;
430 const uint32_t strd_n = 1 << rxq->strd_num_n;
431 const uint32_t elts_n = wqe_n * strd_n;
432 const uint32_t elts_mask = elts_n - 1;
433 volatile struct mlx5_cqe *cq;
434 struct rte_mbuf **elts;
435 uint64_t comp_idx = MLX5_VPMD_DESCS_PER_LOOP;
436 uint16_t nocmp_n = 0;
437 uint16_t rcvd_pkt = 0;
439 unsigned int cq_idx = rxq->cq_ci & q_mask;
440 unsigned int elts_idx;
442 MLX5_ASSERT(rxq->sges_n == 0);
443 cq = &(*rxq->cqes)[cq_idx];
445 rte_prefetch0(cq + 1);
446 rte_prefetch0(cq + 2);
447 rte_prefetch0(cq + 3);
448 pkts_n = RTE_MIN(pkts_n, MLX5_VPMD_RX_MAX_BURST);
449 mlx5_rx_mprq_replenish_bulk_mbuf(rxq);
450 /* See if there're unreturned mbufs from compressed CQE. */
451 rcvd_pkt = rxq->decompressed;
453 rcvd_pkt = RTE_MIN(rcvd_pkt, pkts_n);
454 cp_pkt = rxq_copy_mprq_mbuf_v(rxq, pkts, rcvd_pkt);
455 rxq->decompressed -= rcvd_pkt;
458 elts_idx = rxq->rq_pi & elts_mask;
459 elts = &(*rxq->elts)[elts_idx];
460 /* Not to overflow pkts array. */
461 pkts_n = RTE_ALIGN_FLOOR(pkts_n - cp_pkt, MLX5_VPMD_DESCS_PER_LOOP);
462 /* Not to cross queue end. */
463 pkts_n = RTE_MIN(pkts_n, elts_n - elts_idx);
464 pkts_n = RTE_MIN(pkts_n, q_n - cq_idx);
465 /* Not to move past the allocated mbufs. */
466 pkts_n = RTE_MIN(pkts_n, rxq->elts_ci - rxq->rq_pi);
471 /* At this point, there shouldn't be any remaining packets. */
472 MLX5_ASSERT(rxq->decompressed == 0);
473 /* Process all the CQEs */
474 nocmp_n = rxq_cq_process_v(rxq, cq, elts, pkts, pkts_n, err, &comp_idx);
475 /* If no new CQE seen, return without updating cq_db. */
476 if (unlikely(!nocmp_n && comp_idx == MLX5_VPMD_DESCS_PER_LOOP)) {
480 /* Update the consumer indexes for non-compressed CQEs. */
481 MLX5_ASSERT(nocmp_n <= pkts_n);
482 cp_pkt = rxq_copy_mprq_mbuf_v(rxq, pkts, nocmp_n);
484 /* Decompress the last CQE if compressed. */
485 if (comp_idx < MLX5_VPMD_DESCS_PER_LOOP) {
486 MLX5_ASSERT(comp_idx == (nocmp_n % MLX5_VPMD_DESCS_PER_LOOP));
487 rxq->decompressed = rxq_cq_decompress_v(rxq, &cq[nocmp_n],
489 /* Return more packets if needed. */
490 if (nocmp_n < pkts_n) {
491 uint16_t n = rxq->decompressed;
493 n = RTE_MIN(n, pkts_n - nocmp_n);
494 cp_pkt = rxq_copy_mprq_mbuf_v(rxq, &pkts[cp_pkt], n);
496 rxq->decompressed -= n;
504 * DPDK callback for vectorized MPRQ RX.
507 * Generic pointer to RX queue structure.
509 * Array to store received packets.
511 * Maximum number of packets in array.
514 * Number of packets successfully received (<= pkts_n).
517 mlx5_rx_burst_mprq_vec(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
519 struct mlx5_rxq_data *rxq = dpdk_rxq;
526 nb_rx = rxq_burst_mprq_v(rxq, pkts + tn, pkts_n - tn,
528 if (unlikely(err | rxq->err_state))
529 nb_rx = rxq_handle_pending_error(rxq, pkts + tn, nb_rx);
533 } while (tn != pkts_n);
538 * Check a RX queue can support vectorized RX.
541 * Pointer to RX queue.
544 * 1 if supported, negative errno value if not.
547 mlx5_rxq_check_vec_support(struct mlx5_rxq_data *rxq)
549 struct mlx5_rxq_ctrl *ctrl =
550 container_of(rxq, struct mlx5_rxq_ctrl, rxq);
552 if (!ctrl->priv->config.rx_vec_en || rxq->sges_n != 0)
560 * Check a device can support vectorized RX.
563 * Pointer to Ethernet device.
566 * 1 if supported, negative errno value if not.
569 mlx5_check_vec_rx_support(struct rte_eth_dev *dev)
571 struct mlx5_priv *priv = dev->data->dev_private;
574 if (rte_vect_get_max_simd_bitwidth() < RTE_VECT_SIMD_128)
576 if (!priv->config.rx_vec_en)
578 /* All the configured queues should support. */
579 for (i = 0; i < priv->rxqs_n; ++i) {
580 struct mlx5_rxq_data *rxq = (*priv->rxqs)[i];
584 if (mlx5_rxq_check_vec_support(rxq) < 0)
587 if (i != priv->rxqs_n)