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,
337 * Insert VLAN using mbuf headroom space.
340 * Buffer for VLAN insertion.
343 * 0 on success, errno value on failure.
346 insert_vlan_sw(struct rte_mbuf *buf)
350 uint16_t head_room_len = rte_pktmbuf_headroom(buf);
352 if (head_room_len < 4)
355 addr = rte_pktmbuf_mtod(buf, uintptr_t);
356 vlan = htonl(0x81000000 | buf->vlan_tci);
357 memmove((void *)(addr - 4), (void *)addr, 12);
358 memcpy((void *)(addr + 8), &vlan, sizeof(vlan));
360 SET_DATA_OFF(buf, head_room_len - 4);
366 #if MLX5_PMD_SGE_WR_N > 1
369 * Copy scattered mbuf contents to a single linear buffer.
372 * Linear output buffer.
374 * Scattered input buffer.
377 * Number of bytes copied to the output buffer or 0 if not large enough.
380 linearize_mbuf(linear_t *linear, struct rte_mbuf *buf)
382 unsigned int size = 0;
386 unsigned int len = DATA_LEN(buf);
390 if (unlikely(size > sizeof(*linear)))
392 memcpy(&(*linear)[offset],
393 rte_pktmbuf_mtod(buf, uint8_t *),
396 } while (buf != NULL);
401 * Handle scattered buffers for mlx5_tx_burst().
404 * TX queue structure.
406 * Number of segments in buf.
408 * TX queue element to fill.
412 * Index of the linear buffer to use if necessary (normally txq->elts_head).
414 * Array filled with SGEs on success.
417 * A structure containing the processed packet size in bytes and the
418 * number of SGEs. Both fields are set to (unsigned int)-1 in case of
421 static struct tx_burst_sg_ret {
425 tx_burst_sg(struct txq *txq, unsigned int segs, struct txq_elt *elt,
426 struct rte_mbuf *buf, unsigned int elts_head,
427 struct ibv_sge (*sges)[MLX5_PMD_SGE_WR_N])
429 unsigned int sent_size = 0;
433 /* When there are too many segments, extra segments are
434 * linearized in the last SGE. */
435 if (unlikely(segs > RTE_DIM(*sges))) {
436 segs = (RTE_DIM(*sges) - 1);
439 /* Update element. */
441 /* Register segments as SGEs. */
442 for (j = 0; (j != segs); ++j) {
443 struct ibv_sge *sge = &(*sges)[j];
446 /* Retrieve Memory Region key for this memory pool. */
447 lkey = txq_mp2mr(txq, txq_mb2mp(buf));
448 if (unlikely(lkey == (uint32_t)-1)) {
449 /* MR does not exist. */
450 DEBUG("%p: unable to get MP <-> MR association",
452 /* Clean up TX element. */
457 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
459 rte_prefetch0((volatile void *)
460 (uintptr_t)sge->addr);
461 sge->length = DATA_LEN(buf);
463 sent_size += sge->length;
466 /* If buf is not NULL here and is not going to be linearized,
467 * nb_segs is not valid. */
469 assert((buf == NULL) || (linearize));
470 /* Linearize extra segments. */
472 struct ibv_sge *sge = &(*sges)[segs];
473 linear_t *linear = &(*txq->elts_linear)[elts_head];
474 unsigned int size = linearize_mbuf(linear, buf);
476 assert(segs == (RTE_DIM(*sges) - 1));
478 /* Invalid packet. */
479 DEBUG("%p: packet too large to be linearized.",
481 /* Clean up TX element. */
485 /* If MLX5_PMD_SGE_WR_N is 1, free mbuf immediately. */
486 if (RTE_DIM(*sges) == 1) {
488 struct rte_mbuf *next = NEXT(buf);
490 rte_pktmbuf_free_seg(buf);
492 } while (buf != NULL);
496 sge->addr = (uintptr_t)&(*linear)[0];
498 sge->lkey = txq->mr_linear->lkey;
500 /* Include last segment. */
503 return (struct tx_burst_sg_ret){
508 return (struct tx_burst_sg_ret){
514 #endif /* MLX5_PMD_SGE_WR_N > 1 */
517 * DPDK callback for TX.
520 * Generic pointer to TX queue structure.
522 * Packets to transmit.
524 * Number of packets in array.
527 * Number of packets successfully transmitted (<= pkts_n).
530 mlx5_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
532 struct txq *txq = (struct txq *)dpdk_txq;
533 unsigned int elts_head = txq->elts_head;
534 const unsigned int elts_n = txq->elts_n;
535 unsigned int elts_comp_cd = txq->elts_comp_cd;
536 unsigned int elts_comp = 0;
540 struct rte_mbuf *buf = pkts[0];
542 assert(elts_comp_cd != 0);
543 /* Prefetch first packet cacheline. */
546 max = (elts_n - (elts_head - txq->elts_tail));
550 assert(max <= elts_n);
551 /* Always leave one free entry in the ring. */
557 for (i = 0; (i != max); ++i) {
558 struct rte_mbuf *buf_next = pkts[i + 1];
559 unsigned int elts_head_next =
560 (((elts_head + 1) == elts_n) ? 0 : elts_head + 1);
561 struct txq_elt *elt = &(*txq->elts)[elts_head];
562 unsigned int segs = NB_SEGS(buf);
563 #ifdef MLX5_PMD_SOFT_COUNTERS
564 unsigned int sent_size = 0;
566 uint32_t send_flags = 0;
567 #ifdef HAVE_VERBS_VLAN_INSERTION
569 #endif /* HAVE_VERBS_VLAN_INSERTION */
572 rte_prefetch0(buf_next);
573 /* Request TX completion. */
574 if (unlikely(--elts_comp_cd == 0)) {
575 elts_comp_cd = txq->elts_comp_cd_init;
577 send_flags |= IBV_EXP_QP_BURST_SIGNALED;
579 /* Should we enable HW CKSUM offload */
581 (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM)) {
582 send_flags |= IBV_EXP_QP_BURST_IP_CSUM;
583 /* HW does not support checksum offloads at arbitrary
584 * offsets but automatically recognizes the packet
585 * type. For inner L3/L4 checksums, only VXLAN (UDP)
586 * tunnels are currently supported. */
587 if (RTE_ETH_IS_TUNNEL_PKT(buf->packet_type))
588 send_flags |= IBV_EXP_QP_BURST_TUNNEL;
590 if (buf->ol_flags & PKT_TX_VLAN_PKT) {
591 #ifdef HAVE_VERBS_VLAN_INSERTION
595 #endif /* HAVE_VERBS_VLAN_INSERTION */
597 err = insert_vlan_sw(buf);
602 if (likely(segs == 1)) {
606 uintptr_t buf_next_addr;
608 /* Retrieve buffer information. */
609 addr = rte_pktmbuf_mtod(buf, uintptr_t);
610 length = DATA_LEN(buf);
611 /* Update element. */
614 rte_prefetch0((volatile void *)
616 /* Prefetch next buffer data. */
619 rte_pktmbuf_mtod(buf_next, uintptr_t);
620 rte_prefetch0((volatile void *)
621 (uintptr_t)buf_next_addr);
623 /* Put packet into send queue. */
624 #if MLX5_PMD_MAX_INLINE > 0
625 if (length <= txq->max_inline) {
626 #ifdef HAVE_VERBS_VLAN_INSERTION
628 err = txq->send_pending_inline_vlan
635 #endif /* HAVE_VERBS_VLAN_INSERTION */
636 err = txq->send_pending_inline
644 /* Retrieve Memory Region key for this
646 lkey = txq_mp2mr(txq, txq_mb2mp(buf));
647 if (unlikely(lkey == (uint32_t)-1)) {
648 /* MR does not exist. */
649 DEBUG("%p: unable to get MP <-> MR"
650 " association", (void *)txq);
651 /* Clean up TX element. */
655 #ifdef HAVE_VERBS_VLAN_INSERTION
657 err = txq->send_pending_vlan
665 #endif /* HAVE_VERBS_VLAN_INSERTION */
666 err = txq->send_pending
675 #ifdef MLX5_PMD_SOFT_COUNTERS
679 #if MLX5_PMD_SGE_WR_N > 1
680 struct ibv_sge sges[MLX5_PMD_SGE_WR_N];
681 struct tx_burst_sg_ret ret;
683 ret = tx_burst_sg(txq, segs, elt, buf, elts_head,
685 if (ret.length == (unsigned int)-1)
687 /* Put SG list into send queue. */
688 #ifdef HAVE_VERBS_VLAN_INSERTION
690 err = txq->send_pending_sg_list_vlan
697 #endif /* HAVE_VERBS_VLAN_INSERTION */
698 err = txq->send_pending_sg_list
705 #ifdef MLX5_PMD_SOFT_COUNTERS
706 sent_size += ret.length;
708 #else /* MLX5_PMD_SGE_WR_N > 1 */
709 DEBUG("%p: TX scattered buffers support not"
710 " compiled in", (void *)txq);
712 #endif /* MLX5_PMD_SGE_WR_N > 1 */
714 elts_head = elts_head_next;
716 #ifdef MLX5_PMD_SOFT_COUNTERS
717 /* Increment sent bytes counter. */
718 txq->stats.obytes += sent_size;
722 /* Take a shortcut if nothing must be sent. */
723 if (unlikely(i == 0))
725 #ifdef MLX5_PMD_SOFT_COUNTERS
726 /* Increment sent packets counter. */
727 txq->stats.opackets += i;
729 /* Ring QP doorbell. */
730 err = txq->send_flush(txq->qp);
732 /* A nonzero value is not supposed to be returned.
733 * Nothing can be done about it. */
734 DEBUG("%p: send_flush() failed with error %d",
737 txq->elts_head = elts_head;
738 txq->elts_comp += elts_comp;
739 txq->elts_comp_cd = elts_comp_cd;
744 * Translate RX completion flags to packet type.
747 * RX completion flags returned by poll_length_flags().
749 * @note: fix mlx5_dev_supported_ptypes_get() if any change here.
752 * Packet type for struct rte_mbuf.
754 static inline uint32_t
755 rxq_cq_to_pkt_type(uint32_t flags)
759 if (flags & IBV_EXP_CQ_RX_TUNNEL_PACKET)
762 IBV_EXP_CQ_RX_OUTER_IPV4_PACKET,
765 IBV_EXP_CQ_RX_OUTER_IPV6_PACKET,
768 IBV_EXP_CQ_RX_IPV4_PACKET,
769 RTE_PTYPE_INNER_L3_IPV4) |
771 IBV_EXP_CQ_RX_IPV6_PACKET,
772 RTE_PTYPE_INNER_L3_IPV6);
776 IBV_EXP_CQ_RX_IPV4_PACKET,
779 IBV_EXP_CQ_RX_IPV6_PACKET,
785 * Translate RX completion flags to offload flags.
788 * Pointer to RX queue structure.
790 * RX completion flags returned by poll_length_flags().
793 * Offload flags (ol_flags) for struct rte_mbuf.
795 static inline uint32_t
796 rxq_cq_to_ol_flags(const struct rxq *rxq, uint32_t flags)
798 uint32_t ol_flags = 0;
801 /* Set IP checksum flag only for IPv4/IPv6 packets. */
803 (IBV_EXP_CQ_RX_IPV4_PACKET | IBV_EXP_CQ_RX_IPV6_PACKET))
806 IBV_EXP_CQ_RX_IP_CSUM_OK,
807 PKT_RX_IP_CKSUM_BAD);
808 #ifdef HAVE_EXP_CQ_RX_TCP_PACKET
809 /* Set L4 checksum flag only for TCP/UDP packets. */
811 (IBV_EXP_CQ_RX_TCP_PACKET | IBV_EXP_CQ_RX_UDP_PACKET))
812 #endif /* HAVE_EXP_CQ_RX_TCP_PACKET */
815 IBV_EXP_CQ_RX_TCP_UDP_CSUM_OK,
816 PKT_RX_L4_CKSUM_BAD);
819 * PKT_RX_IP_CKSUM_BAD and PKT_RX_L4_CKSUM_BAD are used in place
820 * of PKT_RX_EIP_CKSUM_BAD because the latter is not functional
823 if ((flags & IBV_EXP_CQ_RX_TUNNEL_PACKET) && (rxq->csum_l2tun))
826 IBV_EXP_CQ_RX_OUTER_IP_CSUM_OK,
827 PKT_RX_IP_CKSUM_BAD) |
829 IBV_EXP_CQ_RX_OUTER_TCP_UDP_CSUM_OK,
830 PKT_RX_L4_CKSUM_BAD);
835 * DPDK callback for RX with scattered packets support.
838 * Generic pointer to RX queue structure.
840 * Array to store received packets.
842 * Maximum number of packets in array.
845 * Number of packets successfully received (<= pkts_n).
848 mlx5_rx_burst_sp(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
850 struct rxq *rxq = (struct rxq *)dpdk_rxq;
851 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
852 const unsigned int elts_n = rxq->elts_n;
853 unsigned int elts_head = rxq->elts_head;
855 unsigned int pkts_ret = 0;
858 if (unlikely(!rxq->sp))
859 return mlx5_rx_burst(dpdk_rxq, pkts, pkts_n);
860 if (unlikely(elts == NULL)) /* See RTE_DEV_CMD_SET_MTU. */
862 for (i = 0; (i != pkts_n); ++i) {
863 struct rxq_elt_sp *elt = &(*elts)[elts_head];
865 unsigned int pkt_buf_len;
866 struct rte_mbuf *pkt_buf = NULL; /* Buffer returned in pkts. */
867 struct rte_mbuf **pkt_buf_next = &pkt_buf;
868 unsigned int seg_headroom = RTE_PKTMBUF_HEADROOM;
874 assert(elts_head < rxq->elts_n);
875 assert(rxq->elts_head < rxq->elts_n);
876 ret = rxq->poll(rxq->cq, NULL, NULL, &flags, &vlan_tci);
877 if (unlikely(ret < 0)) {
881 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
883 /* ibv_poll_cq() must be used in case of failure. */
884 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
885 if (unlikely(wcs_n == 0))
887 if (unlikely(wcs_n < 0)) {
888 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
893 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
894 /* Whatever, just repost the offending WR. */
895 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
896 " completion status (%d): %s",
897 (void *)rxq, wc.wr_id, wc.status,
898 ibv_wc_status_str(wc.status));
899 #ifdef MLX5_PMD_SOFT_COUNTERS
900 /* Increment dropped packets counter. */
901 ++rxq->stats.idropped;
909 assert(ret >= (rxq->crc_present << 2));
910 len = ret - (rxq->crc_present << 2);
913 * Replace spent segments with new ones, concatenate and
914 * return them as pkt_buf.
917 struct ibv_sge *sge = &elt->sges[j];
918 struct rte_mbuf *seg = elt->bufs[j];
919 struct rte_mbuf *rep;
920 unsigned int seg_tailroom;
924 * Fetch initial bytes of packet descriptor into a
925 * cacheline while allocating rep.
928 rep = __rte_mbuf_raw_alloc(rxq->mp);
929 if (unlikely(rep == NULL)) {
931 * Unable to allocate a replacement mbuf,
934 DEBUG("rxq=%p: can't allocate a new mbuf",
936 if (pkt_buf != NULL) {
937 *pkt_buf_next = NULL;
938 rte_pktmbuf_free(pkt_buf);
940 /* Increment out of memory counters. */
941 ++rxq->stats.rx_nombuf;
942 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
946 /* Poison user-modifiable fields in rep. */
947 NEXT(rep) = (void *)((uintptr_t)-1);
948 SET_DATA_OFF(rep, 0xdead);
949 DATA_LEN(rep) = 0xd00d;
950 PKT_LEN(rep) = 0xdeadd00d;
955 assert(rep->buf_len == seg->buf_len);
956 assert(rep->buf_len == rxq->mb_len);
957 /* Reconfigure sge to use rep instead of seg. */
958 assert(sge->lkey == rxq->mr->lkey);
959 sge->addr = ((uintptr_t)rep->buf_addr + seg_headroom);
962 /* Update pkt_buf if it's the first segment, or link
963 * seg to the previous one and update pkt_buf_next. */
965 pkt_buf_next = &NEXT(seg);
966 /* Update seg information. */
967 seg_tailroom = (seg->buf_len - seg_headroom);
968 assert(sge->length == seg_tailroom);
969 SET_DATA_OFF(seg, seg_headroom);
970 if (likely(len <= seg_tailroom)) {
975 assert(rte_pktmbuf_headroom(seg) ==
977 assert(rte_pktmbuf_tailroom(seg) ==
978 (seg_tailroom - len));
981 DATA_LEN(seg) = seg_tailroom;
982 PKT_LEN(seg) = seg_tailroom;
984 assert(rte_pktmbuf_headroom(seg) == seg_headroom);
985 assert(rte_pktmbuf_tailroom(seg) == 0);
986 /* Fix len and clear headroom for next segments. */
990 /* Update head and tail segments. */
991 *pkt_buf_next = NULL;
992 assert(pkt_buf != NULL);
994 NB_SEGS(pkt_buf) = j;
995 PORT(pkt_buf) = rxq->port_id;
996 PKT_LEN(pkt_buf) = pkt_buf_len;
997 if (rxq->csum | rxq->csum_l2tun | rxq->vlan_strip) {
998 pkt_buf->packet_type = rxq_cq_to_pkt_type(flags);
999 pkt_buf->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
1000 #ifdef HAVE_EXP_DEVICE_ATTR_VLAN_OFFLOADS
1001 if (flags & IBV_EXP_CQ_RX_CVLAN_STRIPPED_V1) {
1002 pkt_buf->ol_flags |= PKT_RX_VLAN_PKT;
1003 pkt_buf->vlan_tci = vlan_tci;
1005 #endif /* HAVE_EXP_DEVICE_ATTR_VLAN_OFFLOADS */
1008 /* Return packet. */
1009 *(pkts++) = pkt_buf;
1011 #ifdef MLX5_PMD_SOFT_COUNTERS
1012 /* Increment bytes counter. */
1013 rxq->stats.ibytes += pkt_buf_len;
1016 ret = rxq->recv(rxq->wq, elt->sges, RTE_DIM(elt->sges));
1017 if (unlikely(ret)) {
1018 /* Inability to repost WRs is fatal. */
1019 DEBUG("%p: recv_sg_list(): failed (ret=%d)",
1024 if (++elts_head >= elts_n)
1028 if (unlikely(i == 0))
1030 rxq->elts_head = elts_head;
1031 #ifdef MLX5_PMD_SOFT_COUNTERS
1032 /* Increment packets counter. */
1033 rxq->stats.ipackets += pkts_ret;
1039 * DPDK callback for RX.
1041 * The following function is the same as mlx5_rx_burst_sp(), except it doesn't
1042 * manage scattered packets. Improves performance when MRU is lower than the
1043 * size of the first segment.
1046 * Generic pointer to RX queue structure.
1048 * Array to store received packets.
1050 * Maximum number of packets in array.
1053 * Number of packets successfully received (<= pkts_n).
1056 mlx5_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
1058 struct rxq *rxq = (struct rxq *)dpdk_rxq;
1059 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
1060 const unsigned int elts_n = rxq->elts_n;
1061 unsigned int elts_head = rxq->elts_head;
1062 struct ibv_sge sges[pkts_n];
1064 unsigned int pkts_ret = 0;
1067 if (unlikely(rxq->sp))
1068 return mlx5_rx_burst_sp(dpdk_rxq, pkts, pkts_n);
1069 for (i = 0; (i != pkts_n); ++i) {
1070 struct rxq_elt *elt = &(*elts)[elts_head];
1072 struct rte_mbuf *seg = elt->buf;
1073 struct rte_mbuf *rep;
1077 /* Sanity checks. */
1078 assert(seg != NULL);
1079 assert(elts_head < rxq->elts_n);
1080 assert(rxq->elts_head < rxq->elts_n);
1082 * Fetch initial bytes of packet descriptor into a
1083 * cacheline while allocating rep.
1086 rte_prefetch0(&seg->cacheline1);
1087 ret = rxq->poll(rxq->cq, NULL, NULL, &flags, &vlan_tci);
1088 if (unlikely(ret < 0)) {
1092 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
1094 /* ibv_poll_cq() must be used in case of failure. */
1095 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
1096 if (unlikely(wcs_n == 0))
1098 if (unlikely(wcs_n < 0)) {
1099 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
1100 (void *)rxq, wcs_n);
1104 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
1105 /* Whatever, just repost the offending WR. */
1106 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
1107 " completion status (%d): %s",
1108 (void *)rxq, wc.wr_id, wc.status,
1109 ibv_wc_status_str(wc.status));
1110 #ifdef MLX5_PMD_SOFT_COUNTERS
1111 /* Increment dropped packets counter. */
1112 ++rxq->stats.idropped;
1114 /* Add SGE to array for repost. */
1122 assert(ret >= (rxq->crc_present << 2));
1123 len = ret - (rxq->crc_present << 2);
1124 rep = __rte_mbuf_raw_alloc(rxq->mp);
1125 if (unlikely(rep == NULL)) {
1127 * Unable to allocate a replacement mbuf,
1130 DEBUG("rxq=%p: can't allocate a new mbuf",
1132 /* Increment out of memory counters. */
1133 ++rxq->stats.rx_nombuf;
1134 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
1138 /* Reconfigure sge to use rep instead of seg. */
1139 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
1140 assert(elt->sge.lkey == rxq->mr->lkey);
1143 /* Add SGE to array for repost. */
1146 /* Update seg information. */
1147 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
1149 PORT(seg) = rxq->port_id;
1152 DATA_LEN(seg) = len;
1153 if (rxq->csum | rxq->csum_l2tun | rxq->vlan_strip) {
1154 seg->packet_type = rxq_cq_to_pkt_type(flags);
1155 seg->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
1156 #ifdef HAVE_EXP_DEVICE_ATTR_VLAN_OFFLOADS
1157 if (flags & IBV_EXP_CQ_RX_CVLAN_STRIPPED_V1) {
1158 seg->ol_flags |= PKT_RX_VLAN_PKT;
1159 seg->vlan_tci = vlan_tci;
1161 #endif /* HAVE_EXP_DEVICE_ATTR_VLAN_OFFLOADS */
1163 /* Return packet. */
1166 #ifdef MLX5_PMD_SOFT_COUNTERS
1167 /* Increment bytes counter. */
1168 rxq->stats.ibytes += len;
1171 if (++elts_head >= elts_n)
1175 if (unlikely(i == 0))
1179 DEBUG("%p: reposting %u WRs", (void *)rxq, i);
1181 ret = rxq->recv(rxq->wq, sges, i);
1182 if (unlikely(ret)) {
1183 /* Inability to repost WRs is fatal. */
1184 DEBUG("%p: recv_burst(): failed (ret=%d)",
1189 rxq->elts_head = elts_head;
1190 #ifdef MLX5_PMD_SOFT_COUNTERS
1191 /* Increment packets counter. */
1192 rxq->stats.ipackets += pkts_ret;
1198 * Dummy DPDK callback for TX.
1200 * This function is used to temporarily replace the real callback during
1201 * unsafe control operations on the queue, or in case of error.
1204 * Generic pointer to TX queue structure.
1206 * Packets to transmit.
1208 * Number of packets in array.
1211 * Number of packets successfully transmitted (<= pkts_n).
1214 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1223 * Dummy DPDK callback for RX.
1225 * This function is used to temporarily replace the real callback during
1226 * unsafe control operations on the queue, or in case of error.
1229 * Generic pointer to RX queue structure.
1231 * Array to store received packets.
1233 * Maximum number of packets in array.
1236 * Number of packets successfully received (<= pkts_n).
1239 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)