4 * Copyright 2015 6WIND S.A.
5 * Copyright 2015 Mellanox.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
40 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
42 #pragma GCC diagnostic ignored "-pedantic"
44 #include <infiniband/verbs.h>
46 #pragma GCC diagnostic error "-pedantic"
49 /* DPDK headers don't like -pedantic. */
51 #pragma GCC diagnostic ignored "-pedantic"
54 #include <rte_mempool.h>
55 #include <rte_prefetch.h>
56 #include <rte_common.h>
57 #include <rte_branch_prediction.h>
58 #include <rte_memory.h>
60 #pragma GCC diagnostic error "-pedantic"
64 #include "mlx5_utils.h"
65 #include "mlx5_rxtx.h"
66 #include "mlx5_autoconf.h"
67 #include "mlx5_defs.h"
70 * Manage TX completions.
72 * When sending a burst, mlx5_tx_burst() posts several WRs.
73 * To improve performance, a completion event is only required once every
74 * MLX5_PMD_TX_PER_COMP_REQ sends. Doing so discards completion information
75 * for other WRs, but this information would not be used anyway.
78 * Pointer to TX queue structure.
81 * 0 on success, -1 on failure.
84 txq_complete(struct txq *txq)
86 unsigned int elts_comp = txq->elts_comp;
87 unsigned int elts_tail = txq->elts_tail;
88 unsigned int elts_free = txq->elts_tail;
89 const unsigned int elts_n = txq->elts_n;
92 if (unlikely(elts_comp == 0))
95 DEBUG("%p: processing %u work requests completions",
96 (void *)txq, elts_comp);
98 wcs_n = txq->poll_cnt(txq->cq, elts_comp);
99 if (unlikely(wcs_n == 0))
101 if (unlikely(wcs_n < 0)) {
102 DEBUG("%p: ibv_poll_cq() failed (wcs_n=%d)",
107 assert(elts_comp <= txq->elts_comp);
109 * Assume WC status is successful as nothing can be done about it
112 elts_tail += wcs_n * txq->elts_comp_cd_init;
113 if (elts_tail >= elts_n)
116 while (elts_free != elts_tail) {
117 struct txq_elt *elt = &(*txq->elts)[elts_free];
118 unsigned int elts_free_next =
119 (((elts_free + 1) == elts_n) ? 0 : elts_free + 1);
120 struct rte_mbuf *tmp = elt->buf;
121 struct txq_elt *elt_next = &(*txq->elts)[elts_free_next];
123 RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
124 /* Faster than rte_pktmbuf_free(). */
126 struct rte_mbuf *next = NEXT(tmp);
128 rte_pktmbuf_free_seg(tmp);
130 } while (tmp != NULL);
131 elts_free = elts_free_next;
134 txq->elts_tail = elts_tail;
135 txq->elts_comp = elts_comp;
139 /* For best performance, this function should not be inlined. */
140 struct ibv_mr *mlx5_mp2mr(struct ibv_pd *, const struct rte_mempool *)
141 __attribute__((noinline));
144 * Register mempool as a memory region.
147 * Pointer to protection domain.
149 * Pointer to memory pool.
152 * Memory region pointer, NULL in case of error.
155 mlx5_mp2mr(struct ibv_pd *pd, const struct rte_mempool *mp)
157 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
158 uintptr_t start = mp->elt_va_start;
159 uintptr_t end = mp->elt_va_end;
162 DEBUG("mempool %p area start=%p end=%p size=%zu",
163 (const void *)mp, (void *)start, (void *)end,
164 (size_t)(end - start));
165 /* Round start and end to page boundary if found in memory segments. */
166 for (i = 0; (i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL); ++i) {
167 uintptr_t addr = (uintptr_t)ms[i].addr;
168 size_t len = ms[i].len;
169 unsigned int align = ms[i].hugepage_sz;
171 if ((start > addr) && (start < addr + len))
172 start = RTE_ALIGN_FLOOR(start, align);
173 if ((end > addr) && (end < addr + len))
174 end = RTE_ALIGN_CEIL(end, align);
176 DEBUG("mempool %p using start=%p end=%p size=%zu for MR",
177 (const void *)mp, (void *)start, (void *)end,
178 (size_t)(end - start));
179 return ibv_reg_mr(pd,
182 IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE);
186 * Get Memory Pool (MP) from mbuf. If mbuf is indirect, the pool from which
187 * the cloned mbuf is allocated is returned instead.
193 * Memory pool where data is located for given mbuf.
195 static struct rte_mempool *
196 txq_mb2mp(struct rte_mbuf *buf)
198 if (unlikely(RTE_MBUF_INDIRECT(buf)))
199 return rte_mbuf_from_indirect(buf)->pool;
204 * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
205 * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
206 * remove an entry first.
209 * Pointer to TX queue structure.
211 * Memory Pool for which a Memory Region lkey must be returned.
214 * mr->lkey on success, (uint32_t)-1 on failure.
217 txq_mp2mr(struct txq *txq, const struct rte_mempool *mp)
222 for (i = 0; (i != RTE_DIM(txq->mp2mr)); ++i) {
223 if (unlikely(txq->mp2mr[i].mp == NULL)) {
224 /* Unknown MP, add a new MR for it. */
227 if (txq->mp2mr[i].mp == mp) {
228 assert(txq->mp2mr[i].lkey != (uint32_t)-1);
229 assert(txq->mp2mr[i].mr->lkey == txq->mp2mr[i].lkey);
230 return txq->mp2mr[i].lkey;
233 /* Add a new entry, register MR first. */
234 DEBUG("%p: discovered new memory pool \"%s\" (%p)",
235 (void *)txq, mp->name, (const void *)mp);
236 mr = mlx5_mp2mr(txq->priv->pd, mp);
237 if (unlikely(mr == NULL)) {
238 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
242 if (unlikely(i == RTE_DIM(txq->mp2mr))) {
243 /* Table is full, remove oldest entry. */
244 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
247 claim_zero(ibv_dereg_mr(txq->mp2mr[0].mr));
248 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
249 (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
251 /* Store the new entry. */
252 txq->mp2mr[i].mp = mp;
253 txq->mp2mr[i].mr = mr;
254 txq->mp2mr[i].lkey = mr->lkey;
255 DEBUG("%p: new MR lkey for MP \"%s\" (%p): 0x%08" PRIu32,
256 (void *)txq, mp->name, (const void *)mp, txq->mp2mr[i].lkey);
257 return txq->mp2mr[i].lkey;
260 struct txq_mp2mr_mbuf_check_data {
261 const struct rte_mempool *mp;
266 * Callback function for rte_mempool_obj_iter() to check whether a given
267 * mempool object looks like a mbuf.
269 * @param[in, out] arg
270 * Context data (struct txq_mp2mr_mbuf_check_data). Contains mempool pointer
273 * Object start address.
275 * Object end address.
280 * Nonzero value when object is not a mbuf.
283 txq_mp2mr_mbuf_check(void *arg, void *start, void *end,
284 uint32_t index __rte_unused)
286 struct txq_mp2mr_mbuf_check_data *data = arg;
287 struct rte_mbuf *buf =
288 (void *)((uintptr_t)start + data->mp->header_size);
291 /* Check whether mbuf structure fits element size and whether mempool
292 * pointer is valid. */
293 if (((uintptr_t)end >= (uintptr_t)(buf + 1)) &&
294 (buf->pool == data->mp))
301 * Iterator function for rte_mempool_walk() to register existing mempools and
302 * fill the MP to MR cache of a TX queue.
305 * Memory Pool to register.
307 * Pointer to TX queue structure.
310 txq_mp2mr_iter(const struct rte_mempool *mp, void *arg)
312 struct txq *txq = arg;
313 struct txq_mp2mr_mbuf_check_data data = {
318 /* Discard empty mempools. */
321 /* Register mempool only if the first element looks like a mbuf. */
322 rte_mempool_obj_iter((void *)mp->elt_va_start,
324 mp->header_size + mp->elt_size + mp->trailer_size,
329 txq_mp2mr_mbuf_check,
336 #if MLX5_PMD_SGE_WR_N > 1
339 * Copy scattered mbuf contents to a single linear buffer.
342 * Linear output buffer.
344 * Scattered input buffer.
347 * Number of bytes copied to the output buffer or 0 if not large enough.
350 linearize_mbuf(linear_t *linear, struct rte_mbuf *buf)
352 unsigned int size = 0;
356 unsigned int len = DATA_LEN(buf);
360 if (unlikely(size > sizeof(*linear)))
362 memcpy(&(*linear)[offset],
363 rte_pktmbuf_mtod(buf, uint8_t *),
366 } while (buf != NULL);
371 * Handle scattered buffers for mlx5_tx_burst().
374 * TX queue structure.
376 * Number of segments in buf.
378 * TX queue element to fill.
382 * Index of the linear buffer to use if necessary (normally txq->elts_head).
384 * Array filled with SGEs on success.
387 * A structure containing the processed packet size in bytes and the
388 * number of SGEs. Both fields are set to (unsigned int)-1 in case of
391 static struct tx_burst_sg_ret {
395 tx_burst_sg(struct txq *txq, unsigned int segs, struct txq_elt *elt,
396 struct rte_mbuf *buf, unsigned int elts_head,
397 struct ibv_sge (*sges)[MLX5_PMD_SGE_WR_N])
399 unsigned int sent_size = 0;
403 /* When there are too many segments, extra segments are
404 * linearized in the last SGE. */
405 if (unlikely(segs > RTE_DIM(*sges))) {
406 segs = (RTE_DIM(*sges) - 1);
409 /* Update element. */
411 /* Register segments as SGEs. */
412 for (j = 0; (j != segs); ++j) {
413 struct ibv_sge *sge = &(*sges)[j];
416 /* Retrieve Memory Region key for this memory pool. */
417 lkey = txq_mp2mr(txq, txq_mb2mp(buf));
418 if (unlikely(lkey == (uint32_t)-1)) {
419 /* MR does not exist. */
420 DEBUG("%p: unable to get MP <-> MR association",
422 /* Clean up TX element. */
427 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
429 rte_prefetch0((volatile void *)
430 (uintptr_t)sge->addr);
431 sge->length = DATA_LEN(buf);
433 sent_size += sge->length;
436 /* If buf is not NULL here and is not going to be linearized,
437 * nb_segs is not valid. */
439 assert((buf == NULL) || (linearize));
440 /* Linearize extra segments. */
442 struct ibv_sge *sge = &(*sges)[segs];
443 linear_t *linear = &(*txq->elts_linear)[elts_head];
444 unsigned int size = linearize_mbuf(linear, buf);
446 assert(segs == (RTE_DIM(*sges) - 1));
448 /* Invalid packet. */
449 DEBUG("%p: packet too large to be linearized.",
451 /* Clean up TX element. */
455 /* If MLX5_PMD_SGE_WR_N is 1, free mbuf immediately. */
456 if (RTE_DIM(*sges) == 1) {
458 struct rte_mbuf *next = NEXT(buf);
460 rte_pktmbuf_free_seg(buf);
462 } while (buf != NULL);
466 sge->addr = (uintptr_t)&(*linear)[0];
468 sge->lkey = txq->mr_linear->lkey;
470 /* Include last segment. */
473 return (struct tx_burst_sg_ret){
478 return (struct tx_burst_sg_ret){
484 #endif /* MLX5_PMD_SGE_WR_N > 1 */
487 * DPDK callback for TX.
490 * Generic pointer to TX queue structure.
492 * Packets to transmit.
494 * Number of packets in array.
497 * Number of packets successfully transmitted (<= pkts_n).
500 mlx5_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
502 struct txq *txq = (struct txq *)dpdk_txq;
503 unsigned int elts_head = txq->elts_head;
504 const unsigned int elts_n = txq->elts_n;
505 unsigned int elts_comp_cd = txq->elts_comp_cd;
506 unsigned int elts_comp = 0;
510 struct rte_mbuf *buf = pkts[0];
512 assert(elts_comp_cd != 0);
513 /* Prefetch first packet cacheline. */
516 max = (elts_n - (elts_head - txq->elts_tail));
520 assert(max <= elts_n);
521 /* Always leave one free entry in the ring. */
527 for (i = 0; (i != max); ++i) {
528 struct rte_mbuf *buf_next = pkts[i + 1];
529 unsigned int elts_head_next =
530 (((elts_head + 1) == elts_n) ? 0 : elts_head + 1);
531 struct txq_elt *elt = &(*txq->elts)[elts_head];
532 unsigned int segs = NB_SEGS(buf);
533 #ifdef MLX5_PMD_SOFT_COUNTERS
534 unsigned int sent_size = 0;
536 uint32_t send_flags = 0;
539 rte_prefetch0(buf_next);
540 /* Request TX completion. */
541 if (unlikely(--elts_comp_cd == 0)) {
542 elts_comp_cd = txq->elts_comp_cd_init;
544 send_flags |= IBV_EXP_QP_BURST_SIGNALED;
546 /* Should we enable HW CKSUM offload */
548 (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM)) {
549 send_flags |= IBV_EXP_QP_BURST_IP_CSUM;
550 /* HW does not support checksum offloads at arbitrary
551 * offsets but automatically recognizes the packet
552 * type. For inner L3/L4 checksums, only VXLAN (UDP)
553 * tunnels are currently supported. */
554 if (RTE_ETH_IS_TUNNEL_PKT(buf->packet_type))
555 send_flags |= IBV_EXP_QP_BURST_TUNNEL;
557 if (likely(segs == 1)) {
561 uintptr_t buf_next_addr;
563 /* Retrieve buffer information. */
564 addr = rte_pktmbuf_mtod(buf, uintptr_t);
565 length = DATA_LEN(buf);
566 /* Update element. */
569 rte_prefetch0((volatile void *)
571 /* Prefetch next buffer data. */
574 rte_pktmbuf_mtod(buf_next, uintptr_t);
575 rte_prefetch0((volatile void *)
576 (uintptr_t)buf_next_addr);
578 /* Put packet into send queue. */
579 #if MLX5_PMD_MAX_INLINE > 0
580 if (length <= txq->max_inline)
581 err = txq->send_pending_inline
589 /* Retrieve Memory Region key for this
591 lkey = txq_mp2mr(txq, txq_mb2mp(buf));
592 if (unlikely(lkey == (uint32_t)-1)) {
593 /* MR does not exist. */
594 DEBUG("%p: unable to get MP <-> MR"
595 " association", (void *)txq);
596 /* Clean up TX element. */
600 err = txq->send_pending
609 #ifdef MLX5_PMD_SOFT_COUNTERS
613 #if MLX5_PMD_SGE_WR_N > 1
614 struct ibv_sge sges[MLX5_PMD_SGE_WR_N];
615 struct tx_burst_sg_ret ret;
617 ret = tx_burst_sg(txq, segs, elt, buf, elts_head,
619 if (ret.length == (unsigned int)-1)
621 /* Put SG list into send queue. */
622 err = txq->send_pending_sg_list
629 #ifdef MLX5_PMD_SOFT_COUNTERS
630 sent_size += ret.length;
632 #else /* MLX5_PMD_SGE_WR_N > 1 */
633 DEBUG("%p: TX scattered buffers support not"
634 " compiled in", (void *)txq);
636 #endif /* MLX5_PMD_SGE_WR_N > 1 */
638 elts_head = elts_head_next;
640 #ifdef MLX5_PMD_SOFT_COUNTERS
641 /* Increment sent bytes counter. */
642 txq->stats.obytes += sent_size;
646 /* Take a shortcut if nothing must be sent. */
647 if (unlikely(i == 0))
649 #ifdef MLX5_PMD_SOFT_COUNTERS
650 /* Increment sent packets counter. */
651 txq->stats.opackets += i;
653 /* Ring QP doorbell. */
654 err = txq->send_flush(txq->qp);
656 /* A nonzero value is not supposed to be returned.
657 * Nothing can be done about it. */
658 DEBUG("%p: send_flush() failed with error %d",
661 txq->elts_head = elts_head;
662 txq->elts_comp += elts_comp;
663 txq->elts_comp_cd = elts_comp_cd;
668 * Translate RX completion flags to packet type.
671 * RX completion flags returned by poll_length_flags().
673 * @note: fix mlx5_dev_supported_ptypes_get() if any change here.
676 * Packet type for struct rte_mbuf.
678 static inline uint32_t
679 rxq_cq_to_pkt_type(uint32_t flags)
683 if (flags & IBV_EXP_CQ_RX_TUNNEL_PACKET)
686 IBV_EXP_CQ_RX_OUTER_IPV4_PACKET,
689 IBV_EXP_CQ_RX_OUTER_IPV6_PACKET,
692 IBV_EXP_CQ_RX_IPV4_PACKET,
693 RTE_PTYPE_INNER_L3_IPV4) |
695 IBV_EXP_CQ_RX_IPV6_PACKET,
696 RTE_PTYPE_INNER_L3_IPV6);
700 IBV_EXP_CQ_RX_IPV4_PACKET,
703 IBV_EXP_CQ_RX_IPV6_PACKET,
709 * Translate RX completion flags to offload flags.
712 * Pointer to RX queue structure.
714 * RX completion flags returned by poll_length_flags().
717 * Offload flags (ol_flags) for struct rte_mbuf.
719 static inline uint32_t
720 rxq_cq_to_ol_flags(const struct rxq *rxq, uint32_t flags)
722 uint32_t ol_flags = 0;
725 /* Set IP checksum flag only for IPv4/IPv6 packets. */
727 (IBV_EXP_CQ_RX_IPV4_PACKET | IBV_EXP_CQ_RX_IPV6_PACKET))
730 IBV_EXP_CQ_RX_IP_CSUM_OK,
731 PKT_RX_IP_CKSUM_BAD);
732 #ifdef HAVE_EXP_CQ_RX_TCP_PACKET
733 /* Set L4 checksum flag only for TCP/UDP packets. */
735 (IBV_EXP_CQ_RX_TCP_PACKET | IBV_EXP_CQ_RX_UDP_PACKET))
736 #endif /* HAVE_EXP_CQ_RX_TCP_PACKET */
739 IBV_EXP_CQ_RX_TCP_UDP_CSUM_OK,
740 PKT_RX_L4_CKSUM_BAD);
743 * PKT_RX_IP_CKSUM_BAD and PKT_RX_L4_CKSUM_BAD are used in place
744 * of PKT_RX_EIP_CKSUM_BAD because the latter is not functional
747 if ((flags & IBV_EXP_CQ_RX_TUNNEL_PACKET) && (rxq->csum_l2tun))
750 IBV_EXP_CQ_RX_OUTER_IP_CSUM_OK,
751 PKT_RX_IP_CKSUM_BAD) |
753 IBV_EXP_CQ_RX_OUTER_TCP_UDP_CSUM_OK,
754 PKT_RX_L4_CKSUM_BAD);
759 * DPDK callback for RX with scattered packets support.
762 * Generic pointer to RX queue structure.
764 * Array to store received packets.
766 * Maximum number of packets in array.
769 * Number of packets successfully received (<= pkts_n).
772 mlx5_rx_burst_sp(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
774 struct rxq *rxq = (struct rxq *)dpdk_rxq;
775 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
776 const unsigned int elts_n = rxq->elts_n;
777 unsigned int elts_head = rxq->elts_head;
779 unsigned int pkts_ret = 0;
782 if (unlikely(!rxq->sp))
783 return mlx5_rx_burst(dpdk_rxq, pkts, pkts_n);
784 if (unlikely(elts == NULL)) /* See RTE_DEV_CMD_SET_MTU. */
786 for (i = 0; (i != pkts_n); ++i) {
787 struct rxq_elt_sp *elt = &(*elts)[elts_head];
789 unsigned int pkt_buf_len;
790 struct rte_mbuf *pkt_buf = NULL; /* Buffer returned in pkts. */
791 struct rte_mbuf **pkt_buf_next = &pkt_buf;
792 unsigned int seg_headroom = RTE_PKTMBUF_HEADROOM;
798 assert(elts_head < rxq->elts_n);
799 assert(rxq->elts_head < rxq->elts_n);
800 ret = rxq->poll(rxq->cq, NULL, NULL, &flags, &vlan_tci);
801 if (unlikely(ret < 0)) {
805 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
807 /* ibv_poll_cq() must be used in case of failure. */
808 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
809 if (unlikely(wcs_n == 0))
811 if (unlikely(wcs_n < 0)) {
812 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
817 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
818 /* Whatever, just repost the offending WR. */
819 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
820 " completion status (%d): %s",
821 (void *)rxq, wc.wr_id, wc.status,
822 ibv_wc_status_str(wc.status));
823 #ifdef MLX5_PMD_SOFT_COUNTERS
824 /* Increment dropped packets counter. */
825 ++rxq->stats.idropped;
833 assert(ret >= (rxq->crc_present << 2));
834 len = ret - (rxq->crc_present << 2);
837 * Replace spent segments with new ones, concatenate and
838 * return them as pkt_buf.
841 struct ibv_sge *sge = &elt->sges[j];
842 struct rte_mbuf *seg = elt->bufs[j];
843 struct rte_mbuf *rep;
844 unsigned int seg_tailroom;
848 * Fetch initial bytes of packet descriptor into a
849 * cacheline while allocating rep.
852 rep = __rte_mbuf_raw_alloc(rxq->mp);
853 if (unlikely(rep == NULL)) {
855 * Unable to allocate a replacement mbuf,
858 DEBUG("rxq=%p: can't allocate a new mbuf",
860 if (pkt_buf != NULL) {
861 *pkt_buf_next = NULL;
862 rte_pktmbuf_free(pkt_buf);
864 /* Increment out of memory counters. */
865 ++rxq->stats.rx_nombuf;
866 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
870 /* Poison user-modifiable fields in rep. */
871 NEXT(rep) = (void *)((uintptr_t)-1);
872 SET_DATA_OFF(rep, 0xdead);
873 DATA_LEN(rep) = 0xd00d;
874 PKT_LEN(rep) = 0xdeadd00d;
879 assert(rep->buf_len == seg->buf_len);
880 assert(rep->buf_len == rxq->mb_len);
881 /* Reconfigure sge to use rep instead of seg. */
882 assert(sge->lkey == rxq->mr->lkey);
883 sge->addr = ((uintptr_t)rep->buf_addr + seg_headroom);
886 /* Update pkt_buf if it's the first segment, or link
887 * seg to the previous one and update pkt_buf_next. */
889 pkt_buf_next = &NEXT(seg);
890 /* Update seg information. */
891 seg_tailroom = (seg->buf_len - seg_headroom);
892 assert(sge->length == seg_tailroom);
893 SET_DATA_OFF(seg, seg_headroom);
894 if (likely(len <= seg_tailroom)) {
899 assert(rte_pktmbuf_headroom(seg) ==
901 assert(rte_pktmbuf_tailroom(seg) ==
902 (seg_tailroom - len));
905 DATA_LEN(seg) = seg_tailroom;
906 PKT_LEN(seg) = seg_tailroom;
908 assert(rte_pktmbuf_headroom(seg) == seg_headroom);
909 assert(rte_pktmbuf_tailroom(seg) == 0);
910 /* Fix len and clear headroom for next segments. */
914 /* Update head and tail segments. */
915 *pkt_buf_next = NULL;
916 assert(pkt_buf != NULL);
918 NB_SEGS(pkt_buf) = j;
919 PORT(pkt_buf) = rxq->port_id;
920 PKT_LEN(pkt_buf) = pkt_buf_len;
921 if (rxq->csum | rxq->csum_l2tun | rxq->vlan_strip) {
922 pkt_buf->packet_type = rxq_cq_to_pkt_type(flags);
923 pkt_buf->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
924 #ifdef HAVE_EXP_DEVICE_ATTR_VLAN_OFFLOADS
925 if (flags & IBV_EXP_CQ_RX_CVLAN_STRIPPED_V1) {
926 pkt_buf->ol_flags |= PKT_RX_VLAN_PKT;
927 pkt_buf->vlan_tci = vlan_tci;
929 #endif /* HAVE_EXP_DEVICE_ATTR_VLAN_OFFLOADS */
935 #ifdef MLX5_PMD_SOFT_COUNTERS
936 /* Increment bytes counter. */
937 rxq->stats.ibytes += pkt_buf_len;
940 ret = rxq->recv(rxq->wq, elt->sges, RTE_DIM(elt->sges));
942 /* Inability to repost WRs is fatal. */
943 DEBUG("%p: recv_sg_list(): failed (ret=%d)",
948 if (++elts_head >= elts_n)
952 if (unlikely(i == 0))
954 rxq->elts_head = elts_head;
955 #ifdef MLX5_PMD_SOFT_COUNTERS
956 /* Increment packets counter. */
957 rxq->stats.ipackets += pkts_ret;
963 * DPDK callback for RX.
965 * The following function is the same as mlx5_rx_burst_sp(), except it doesn't
966 * manage scattered packets. Improves performance when MRU is lower than the
967 * size of the first segment.
970 * Generic pointer to RX queue structure.
972 * Array to store received packets.
974 * Maximum number of packets in array.
977 * Number of packets successfully received (<= pkts_n).
980 mlx5_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
982 struct rxq *rxq = (struct rxq *)dpdk_rxq;
983 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
984 const unsigned int elts_n = rxq->elts_n;
985 unsigned int elts_head = rxq->elts_head;
986 struct ibv_sge sges[pkts_n];
988 unsigned int pkts_ret = 0;
991 if (unlikely(rxq->sp))
992 return mlx5_rx_burst_sp(dpdk_rxq, pkts, pkts_n);
993 for (i = 0; (i != pkts_n); ++i) {
994 struct rxq_elt *elt = &(*elts)[elts_head];
996 struct rte_mbuf *seg = elt->buf;
997 struct rte_mbuf *rep;
1001 /* Sanity checks. */
1002 assert(seg != NULL);
1003 assert(elts_head < rxq->elts_n);
1004 assert(rxq->elts_head < rxq->elts_n);
1006 * Fetch initial bytes of packet descriptor into a
1007 * cacheline while allocating rep.
1010 rte_prefetch0(&seg->cacheline1);
1011 ret = rxq->poll(rxq->cq, NULL, NULL, &flags, &vlan_tci);
1012 if (unlikely(ret < 0)) {
1016 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
1018 /* ibv_poll_cq() must be used in case of failure. */
1019 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
1020 if (unlikely(wcs_n == 0))
1022 if (unlikely(wcs_n < 0)) {
1023 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
1024 (void *)rxq, wcs_n);
1028 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
1029 /* Whatever, just repost the offending WR. */
1030 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
1031 " completion status (%d): %s",
1032 (void *)rxq, wc.wr_id, wc.status,
1033 ibv_wc_status_str(wc.status));
1034 #ifdef MLX5_PMD_SOFT_COUNTERS
1035 /* Increment dropped packets counter. */
1036 ++rxq->stats.idropped;
1038 /* Add SGE to array for repost. */
1046 assert(ret >= (rxq->crc_present << 2));
1047 len = ret - (rxq->crc_present << 2);
1048 rep = __rte_mbuf_raw_alloc(rxq->mp);
1049 if (unlikely(rep == NULL)) {
1051 * Unable to allocate a replacement mbuf,
1054 DEBUG("rxq=%p: can't allocate a new mbuf",
1056 /* Increment out of memory counters. */
1057 ++rxq->stats.rx_nombuf;
1058 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
1062 /* Reconfigure sge to use rep instead of seg. */
1063 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
1064 assert(elt->sge.lkey == rxq->mr->lkey);
1067 /* Add SGE to array for repost. */
1070 /* Update seg information. */
1071 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
1073 PORT(seg) = rxq->port_id;
1076 DATA_LEN(seg) = len;
1077 if (rxq->csum | rxq->csum_l2tun | rxq->vlan_strip) {
1078 seg->packet_type = rxq_cq_to_pkt_type(flags);
1079 seg->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
1080 #ifdef HAVE_EXP_DEVICE_ATTR_VLAN_OFFLOADS
1081 if (flags & IBV_EXP_CQ_RX_CVLAN_STRIPPED_V1) {
1082 seg->ol_flags |= PKT_RX_VLAN_PKT;
1083 seg->vlan_tci = vlan_tci;
1085 #endif /* HAVE_EXP_DEVICE_ATTR_VLAN_OFFLOADS */
1087 /* Return packet. */
1090 #ifdef MLX5_PMD_SOFT_COUNTERS
1091 /* Increment bytes counter. */
1092 rxq->stats.ibytes += len;
1095 if (++elts_head >= elts_n)
1099 if (unlikely(i == 0))
1103 DEBUG("%p: reposting %u WRs", (void *)rxq, i);
1105 ret = rxq->recv(rxq->wq, sges, i);
1106 if (unlikely(ret)) {
1107 /* Inability to repost WRs is fatal. */
1108 DEBUG("%p: recv_burst(): failed (ret=%d)",
1113 rxq->elts_head = elts_head;
1114 #ifdef MLX5_PMD_SOFT_COUNTERS
1115 /* Increment packets counter. */
1116 rxq->stats.ipackets += pkts_ret;
1122 * Dummy DPDK callback for TX.
1124 * This function is used to temporarily replace the real callback during
1125 * unsafe control operations on the queue, or in case of error.
1128 * Generic pointer to TX queue structure.
1130 * Packets to transmit.
1132 * Number of packets in array.
1135 * Number of packets successfully transmitted (<= pkts_n).
1138 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1147 * Dummy DPDK callback for RX.
1149 * This function is used to temporarily replace the real callback during
1150 * unsafe control operations on the queue, or in case of error.
1153 * Generic pointer to RX queue structure.
1155 * Array to store received packets.
1157 * Maximum number of packets in array.
1160 * Number of packets successfully received (<= pkts_n).
1163 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)