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];
125 memset(elt, 0x66, sizeof(*elt));
127 RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
128 /* Faster than rte_pktmbuf_free(). */
130 struct rte_mbuf *next = NEXT(tmp);
132 rte_pktmbuf_free_seg(tmp);
134 } while (tmp != NULL);
135 elts_free = elts_free_next;
138 txq->elts_tail = elts_tail;
139 txq->elts_comp = elts_comp;
143 /* For best performance, this function should not be inlined. */
144 struct ibv_mr *mlx5_mp2mr(struct ibv_pd *, const struct rte_mempool *)
145 __attribute__((noinline));
148 * Register mempool as a memory region.
151 * Pointer to protection domain.
153 * Pointer to memory pool.
156 * Memory region pointer, NULL in case of error.
159 mlx5_mp2mr(struct ibv_pd *pd, const struct rte_mempool *mp)
161 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
162 uintptr_t start = (uintptr_t)STAILQ_FIRST(&mp->mem_list)->addr;
163 uintptr_t end = start + STAILQ_FIRST(&mp->mem_list)->len;
166 DEBUG("mempool %p area start=%p end=%p size=%zu",
167 (const void *)mp, (void *)start, (void *)end,
168 (size_t)(end - start));
169 /* Round start and end to page boundary if found in memory segments. */
170 for (i = 0; (i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL); ++i) {
171 uintptr_t addr = (uintptr_t)ms[i].addr;
172 size_t len = ms[i].len;
173 unsigned int align = ms[i].hugepage_sz;
175 if ((start > addr) && (start < addr + len))
176 start = RTE_ALIGN_FLOOR(start, align);
177 if ((end > addr) && (end < addr + len))
178 end = RTE_ALIGN_CEIL(end, align);
180 DEBUG("mempool %p using start=%p end=%p size=%zu for MR",
181 (const void *)mp, (void *)start, (void *)end,
182 (size_t)(end - start));
183 return ibv_reg_mr(pd,
186 IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE);
190 * Get Memory Pool (MP) from mbuf. If mbuf is indirect, the pool from which
191 * the cloned mbuf is allocated is returned instead.
197 * Memory pool where data is located for given mbuf.
199 static struct rte_mempool *
200 txq_mb2mp(struct rte_mbuf *buf)
202 if (unlikely(RTE_MBUF_INDIRECT(buf)))
203 return rte_mbuf_from_indirect(buf)->pool;
208 * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
209 * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
210 * remove an entry first.
213 * Pointer to TX queue structure.
215 * Memory Pool for which a Memory Region lkey must be returned.
218 * mr->lkey on success, (uint32_t)-1 on failure.
221 txq_mp2mr(struct txq *txq, const struct rte_mempool *mp)
226 for (i = 0; (i != RTE_DIM(txq->mp2mr)); ++i) {
227 if (unlikely(txq->mp2mr[i].mp == NULL)) {
228 /* Unknown MP, add a new MR for it. */
231 if (txq->mp2mr[i].mp == mp) {
232 assert(txq->mp2mr[i].lkey != (uint32_t)-1);
233 assert(txq->mp2mr[i].mr->lkey == txq->mp2mr[i].lkey);
234 return txq->mp2mr[i].lkey;
237 /* Add a new entry, register MR first. */
238 DEBUG("%p: discovered new memory pool \"%s\" (%p)",
239 (void *)txq, mp->name, (const void *)mp);
240 mr = mlx5_mp2mr(txq->priv->pd, mp);
241 if (unlikely(mr == NULL)) {
242 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
246 if (unlikely(i == RTE_DIM(txq->mp2mr))) {
247 /* Table is full, remove oldest entry. */
248 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
251 claim_zero(ibv_dereg_mr(txq->mp2mr[0].mr));
252 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
253 (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
255 /* Store the new entry. */
256 txq->mp2mr[i].mp = mp;
257 txq->mp2mr[i].mr = mr;
258 txq->mp2mr[i].lkey = mr->lkey;
259 DEBUG("%p: new MR lkey for MP \"%s\" (%p): 0x%08" PRIu32,
260 (void *)txq, mp->name, (const void *)mp, txq->mp2mr[i].lkey);
261 return txq->mp2mr[i].lkey;
264 struct txq_mp2mr_mbuf_check_data {
269 * Callback function for rte_mempool_obj_iter() to check whether a given
270 * mempool object looks like a mbuf.
273 * The mempool pointer
275 * Context data (struct txq_mp2mr_mbuf_check_data). Contains the
280 * Object index, unused.
283 txq_mp2mr_mbuf_check(struct rte_mempool *mp, void *arg, void *obj,
284 uint32_t index __rte_unused)
286 struct txq_mp2mr_mbuf_check_data *data = arg;
287 struct rte_mbuf *buf = obj;
289 /* Check whether mbuf structure fits element size and whether mempool
290 * pointer is valid. */
291 if (sizeof(*buf) > mp->elt_size || buf->pool != mp)
296 * Iterator function for rte_mempool_walk() to register existing mempools and
297 * fill the MP to MR cache of a TX queue.
300 * Memory Pool to register.
302 * Pointer to TX queue structure.
305 txq_mp2mr_iter(struct rte_mempool *mp, void *arg)
307 struct txq *txq = arg;
308 struct txq_mp2mr_mbuf_check_data data = {
312 /* Register mempool only if the first element looks like a mbuf. */
313 if (rte_mempool_obj_iter(mp, txq_mp2mr_mbuf_check, &data) == 0 ||
320 * Insert VLAN using mbuf headroom space.
323 * Buffer for VLAN insertion.
326 * 0 on success, errno value on failure.
329 insert_vlan_sw(struct rte_mbuf *buf)
333 uint16_t head_room_len = rte_pktmbuf_headroom(buf);
335 if (head_room_len < 4)
338 addr = rte_pktmbuf_mtod(buf, uintptr_t);
339 vlan = htonl(0x81000000 | buf->vlan_tci);
340 memmove((void *)(addr - 4), (void *)addr, 12);
341 memcpy((void *)(addr + 8), &vlan, sizeof(vlan));
343 SET_DATA_OFF(buf, head_room_len - 4);
349 #if MLX5_PMD_SGE_WR_N > 1
352 * Copy scattered mbuf contents to a single linear buffer.
355 * Linear output buffer.
357 * Scattered input buffer.
360 * Number of bytes copied to the output buffer or 0 if not large enough.
363 linearize_mbuf(linear_t *linear, struct rte_mbuf *buf)
365 unsigned int size = 0;
369 unsigned int len = DATA_LEN(buf);
373 if (unlikely(size > sizeof(*linear)))
375 memcpy(&(*linear)[offset],
376 rte_pktmbuf_mtod(buf, uint8_t *),
379 } while (buf != NULL);
384 * Handle scattered buffers for mlx5_tx_burst().
387 * TX queue structure.
389 * Number of segments in buf.
391 * TX queue element to fill.
395 * Index of the linear buffer to use if necessary (normally txq->elts_head).
397 * Array filled with SGEs on success.
400 * A structure containing the processed packet size in bytes and the
401 * number of SGEs. Both fields are set to (unsigned int)-1 in case of
404 static struct tx_burst_sg_ret {
408 tx_burst_sg(struct txq *txq, unsigned int segs, struct txq_elt *elt,
409 struct rte_mbuf *buf, unsigned int elts_head,
410 struct ibv_sge (*sges)[MLX5_PMD_SGE_WR_N])
412 unsigned int sent_size = 0;
416 /* When there are too many segments, extra segments are
417 * linearized in the last SGE. */
418 if (unlikely(segs > RTE_DIM(*sges))) {
419 segs = (RTE_DIM(*sges) - 1);
422 /* Update element. */
424 /* Register segments as SGEs. */
425 for (j = 0; (j != segs); ++j) {
426 struct ibv_sge *sge = &(*sges)[j];
429 /* Retrieve Memory Region key for this memory pool. */
430 lkey = txq_mp2mr(txq, txq_mb2mp(buf));
431 if (unlikely(lkey == (uint32_t)-1)) {
432 /* MR does not exist. */
433 DEBUG("%p: unable to get MP <-> MR association",
435 /* Clean up TX element. */
440 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
442 rte_prefetch0((volatile void *)
443 (uintptr_t)sge->addr);
444 sge->length = DATA_LEN(buf);
446 sent_size += sge->length;
449 /* If buf is not NULL here and is not going to be linearized,
450 * nb_segs is not valid. */
452 assert((buf == NULL) || (linearize));
453 /* Linearize extra segments. */
455 struct ibv_sge *sge = &(*sges)[segs];
456 linear_t *linear = &(*txq->elts_linear)[elts_head];
457 unsigned int size = linearize_mbuf(linear, buf);
459 assert(segs == (RTE_DIM(*sges) - 1));
461 /* Invalid packet. */
462 DEBUG("%p: packet too large to be linearized.",
464 /* Clean up TX element. */
468 /* If MLX5_PMD_SGE_WR_N is 1, free mbuf immediately. */
469 if (RTE_DIM(*sges) == 1) {
471 struct rte_mbuf *next = NEXT(buf);
473 rte_pktmbuf_free_seg(buf);
475 } while (buf != NULL);
479 sge->addr = (uintptr_t)&(*linear)[0];
481 sge->lkey = txq->mr_linear->lkey;
483 /* Include last segment. */
486 return (struct tx_burst_sg_ret){
491 return (struct tx_burst_sg_ret){
497 #endif /* MLX5_PMD_SGE_WR_N > 1 */
500 * DPDK callback for TX.
503 * Generic pointer to TX queue structure.
505 * Packets to transmit.
507 * Number of packets in array.
510 * Number of packets successfully transmitted (<= pkts_n).
513 mlx5_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
515 struct txq *txq = (struct txq *)dpdk_txq;
516 unsigned int elts_head = txq->elts_head;
517 const unsigned int elts_n = txq->elts_n;
518 unsigned int elts_comp_cd = txq->elts_comp_cd;
519 unsigned int elts_comp = 0;
523 struct rte_mbuf *buf = pkts[0];
525 assert(elts_comp_cd != 0);
526 /* Prefetch first packet cacheline. */
529 max = (elts_n - (elts_head - txq->elts_tail));
533 assert(max <= elts_n);
534 /* Always leave one free entry in the ring. */
540 for (i = 0; (i != max); ++i) {
541 struct rte_mbuf *buf_next = pkts[i + 1];
542 unsigned int elts_head_next =
543 (((elts_head + 1) == elts_n) ? 0 : elts_head + 1);
544 struct txq_elt *elt = &(*txq->elts)[elts_head];
545 unsigned int segs = NB_SEGS(buf);
546 #ifdef MLX5_PMD_SOFT_COUNTERS
547 unsigned int sent_size = 0;
549 uint32_t send_flags = 0;
550 #ifdef HAVE_VERBS_VLAN_INSERTION
552 #endif /* HAVE_VERBS_VLAN_INSERTION */
555 rte_prefetch0(buf_next);
556 /* Request TX completion. */
557 if (unlikely(--elts_comp_cd == 0)) {
558 elts_comp_cd = txq->elts_comp_cd_init;
560 send_flags |= IBV_EXP_QP_BURST_SIGNALED;
562 /* Should we enable HW CKSUM offload */
564 (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM)) {
565 send_flags |= IBV_EXP_QP_BURST_IP_CSUM;
566 /* HW does not support checksum offloads at arbitrary
567 * offsets but automatically recognizes the packet
568 * type. For inner L3/L4 checksums, only VXLAN (UDP)
569 * tunnels are currently supported. */
570 if (RTE_ETH_IS_TUNNEL_PKT(buf->packet_type))
571 send_flags |= IBV_EXP_QP_BURST_TUNNEL;
573 if (buf->ol_flags & PKT_TX_VLAN_PKT) {
574 #ifdef HAVE_VERBS_VLAN_INSERTION
578 #endif /* HAVE_VERBS_VLAN_INSERTION */
580 err = insert_vlan_sw(buf);
585 if (likely(segs == 1)) {
589 uintptr_t buf_next_addr;
591 /* Retrieve buffer information. */
592 addr = rte_pktmbuf_mtod(buf, uintptr_t);
593 length = DATA_LEN(buf);
594 /* Update element. */
597 rte_prefetch0((volatile void *)
599 /* Prefetch next buffer data. */
602 rte_pktmbuf_mtod(buf_next, uintptr_t);
603 rte_prefetch0((volatile void *)
604 (uintptr_t)buf_next_addr);
606 /* Put packet into send queue. */
607 #if MLX5_PMD_MAX_INLINE > 0
608 if (length <= txq->max_inline) {
609 #ifdef HAVE_VERBS_VLAN_INSERTION
611 err = txq->send_pending_inline_vlan
618 #endif /* HAVE_VERBS_VLAN_INSERTION */
619 err = txq->send_pending_inline
627 /* Retrieve Memory Region key for this
629 lkey = txq_mp2mr(txq, txq_mb2mp(buf));
630 if (unlikely(lkey == (uint32_t)-1)) {
631 /* MR does not exist. */
632 DEBUG("%p: unable to get MP <-> MR"
633 " association", (void *)txq);
634 /* Clean up TX element. */
638 #ifdef HAVE_VERBS_VLAN_INSERTION
640 err = txq->send_pending_vlan
648 #endif /* HAVE_VERBS_VLAN_INSERTION */
649 err = txq->send_pending
658 #ifdef MLX5_PMD_SOFT_COUNTERS
662 #if MLX5_PMD_SGE_WR_N > 1
663 struct ibv_sge sges[MLX5_PMD_SGE_WR_N];
664 struct tx_burst_sg_ret ret;
666 ret = tx_burst_sg(txq, segs, elt, buf, elts_head,
668 if (ret.length == (unsigned int)-1)
670 /* Put SG list into send queue. */
671 #ifdef HAVE_VERBS_VLAN_INSERTION
673 err = txq->send_pending_sg_list_vlan
680 #endif /* HAVE_VERBS_VLAN_INSERTION */
681 err = txq->send_pending_sg_list
688 #ifdef MLX5_PMD_SOFT_COUNTERS
689 sent_size += ret.length;
691 #else /* MLX5_PMD_SGE_WR_N > 1 */
692 DEBUG("%p: TX scattered buffers support not"
693 " compiled in", (void *)txq);
695 #endif /* MLX5_PMD_SGE_WR_N > 1 */
697 elts_head = elts_head_next;
699 #ifdef MLX5_PMD_SOFT_COUNTERS
700 /* Increment sent bytes counter. */
701 txq->stats.obytes += sent_size;
705 /* Take a shortcut if nothing must be sent. */
706 if (unlikely(i == 0))
708 #ifdef MLX5_PMD_SOFT_COUNTERS
709 /* Increment sent packets counter. */
710 txq->stats.opackets += i;
712 /* Ring QP doorbell. */
713 err = txq->send_flush(txq->qp);
715 /* A nonzero value is not supposed to be returned.
716 * Nothing can be done about it. */
717 DEBUG("%p: send_flush() failed with error %d",
720 txq->elts_head = elts_head;
721 txq->elts_comp += elts_comp;
722 txq->elts_comp_cd = elts_comp_cd;
727 * Translate RX completion flags to packet type.
730 * RX completion flags returned by poll_length_flags().
732 * @note: fix mlx5_dev_supported_ptypes_get() if any change here.
735 * Packet type for struct rte_mbuf.
737 static inline uint32_t
738 rxq_cq_to_pkt_type(uint32_t flags)
742 if (flags & IBV_EXP_CQ_RX_TUNNEL_PACKET)
745 IBV_EXP_CQ_RX_OUTER_IPV4_PACKET,
748 IBV_EXP_CQ_RX_OUTER_IPV6_PACKET,
751 IBV_EXP_CQ_RX_IPV4_PACKET,
752 RTE_PTYPE_INNER_L3_IPV4) |
754 IBV_EXP_CQ_RX_IPV6_PACKET,
755 RTE_PTYPE_INNER_L3_IPV6);
759 IBV_EXP_CQ_RX_IPV4_PACKET,
762 IBV_EXP_CQ_RX_IPV6_PACKET,
768 * Translate RX completion flags to offload flags.
771 * Pointer to RX queue structure.
773 * RX completion flags returned by poll_length_flags().
776 * Offload flags (ol_flags) for struct rte_mbuf.
778 static inline uint32_t
779 rxq_cq_to_ol_flags(const struct rxq *rxq, uint32_t flags)
781 uint32_t ol_flags = 0;
784 /* Set IP checksum flag only for IPv4/IPv6 packets. */
786 (IBV_EXP_CQ_RX_IPV4_PACKET | IBV_EXP_CQ_RX_IPV6_PACKET))
789 IBV_EXP_CQ_RX_IP_CSUM_OK,
790 PKT_RX_IP_CKSUM_BAD);
791 #ifdef HAVE_EXP_CQ_RX_TCP_PACKET
792 /* Set L4 checksum flag only for TCP/UDP packets. */
794 (IBV_EXP_CQ_RX_TCP_PACKET | IBV_EXP_CQ_RX_UDP_PACKET))
795 #endif /* HAVE_EXP_CQ_RX_TCP_PACKET */
798 IBV_EXP_CQ_RX_TCP_UDP_CSUM_OK,
799 PKT_RX_L4_CKSUM_BAD);
802 * PKT_RX_IP_CKSUM_BAD and PKT_RX_L4_CKSUM_BAD are used in place
803 * of PKT_RX_EIP_CKSUM_BAD because the latter is not functional
806 if ((flags & IBV_EXP_CQ_RX_TUNNEL_PACKET) && (rxq->csum_l2tun))
809 IBV_EXP_CQ_RX_OUTER_IP_CSUM_OK,
810 PKT_RX_IP_CKSUM_BAD) |
812 IBV_EXP_CQ_RX_OUTER_TCP_UDP_CSUM_OK,
813 PKT_RX_L4_CKSUM_BAD);
818 * DPDK callback for RX with scattered packets support.
821 * Generic pointer to RX queue structure.
823 * Array to store received packets.
825 * Maximum number of packets in array.
828 * Number of packets successfully received (<= pkts_n).
831 mlx5_rx_burst_sp(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
833 struct rxq *rxq = (struct rxq *)dpdk_rxq;
834 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
835 const unsigned int elts_n = rxq->elts_n;
836 unsigned int elts_head = rxq->elts_head;
838 unsigned int pkts_ret = 0;
841 if (unlikely(!rxq->sp))
842 return mlx5_rx_burst(dpdk_rxq, pkts, pkts_n);
843 if (unlikely(elts == NULL)) /* See RTE_DEV_CMD_SET_MTU. */
845 for (i = 0; (i != pkts_n); ++i) {
846 struct rxq_elt_sp *elt = &(*elts)[elts_head];
848 unsigned int pkt_buf_len;
849 struct rte_mbuf *pkt_buf = NULL; /* Buffer returned in pkts. */
850 struct rte_mbuf **pkt_buf_next = &pkt_buf;
851 unsigned int seg_headroom = RTE_PKTMBUF_HEADROOM;
857 assert(elts_head < rxq->elts_n);
858 assert(rxq->elts_head < rxq->elts_n);
859 ret = rxq->poll(rxq->cq, NULL, NULL, &flags, &vlan_tci);
860 if (unlikely(ret < 0)) {
864 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
866 /* ibv_poll_cq() must be used in case of failure. */
867 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
868 if (unlikely(wcs_n == 0))
870 if (unlikely(wcs_n < 0)) {
871 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
876 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
877 /* Whatever, just repost the offending WR. */
878 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
879 " completion status (%d): %s",
880 (void *)rxq, wc.wr_id, wc.status,
881 ibv_wc_status_str(wc.status));
882 #ifdef MLX5_PMD_SOFT_COUNTERS
883 /* Increment dropped packets counter. */
884 ++rxq->stats.idropped;
892 assert(ret >= (rxq->crc_present << 2));
893 len = ret - (rxq->crc_present << 2);
896 * Replace spent segments with new ones, concatenate and
897 * return them as pkt_buf.
900 struct ibv_sge *sge = &elt->sges[j];
901 struct rte_mbuf *seg = elt->bufs[j];
902 struct rte_mbuf *rep;
903 unsigned int seg_tailroom;
907 * Fetch initial bytes of packet descriptor into a
908 * cacheline while allocating rep.
911 rep = rte_mbuf_raw_alloc(rxq->mp);
912 if (unlikely(rep == NULL)) {
914 * Unable to allocate a replacement mbuf,
917 DEBUG("rxq=%p: can't allocate a new mbuf",
919 if (pkt_buf != NULL) {
920 *pkt_buf_next = NULL;
921 rte_pktmbuf_free(pkt_buf);
923 /* Increment out of memory counters. */
924 ++rxq->stats.rx_nombuf;
925 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
929 /* Poison user-modifiable fields in rep. */
930 NEXT(rep) = (void *)((uintptr_t)-1);
931 SET_DATA_OFF(rep, 0xdead);
932 DATA_LEN(rep) = 0xd00d;
933 PKT_LEN(rep) = 0xdeadd00d;
938 assert(rep->buf_len == seg->buf_len);
939 assert(rep->buf_len == rxq->mb_len);
940 /* Reconfigure sge to use rep instead of seg. */
941 assert(sge->lkey == rxq->mr->lkey);
942 sge->addr = ((uintptr_t)rep->buf_addr + seg_headroom);
945 /* Update pkt_buf if it's the first segment, or link
946 * seg to the previous one and update pkt_buf_next. */
948 pkt_buf_next = &NEXT(seg);
949 /* Update seg information. */
950 seg_tailroom = (seg->buf_len - seg_headroom);
951 assert(sge->length == seg_tailroom);
952 SET_DATA_OFF(seg, seg_headroom);
953 if (likely(len <= seg_tailroom)) {
958 assert(rte_pktmbuf_headroom(seg) ==
960 assert(rte_pktmbuf_tailroom(seg) ==
961 (seg_tailroom - len));
964 DATA_LEN(seg) = seg_tailroom;
965 PKT_LEN(seg) = seg_tailroom;
967 assert(rte_pktmbuf_headroom(seg) == seg_headroom);
968 assert(rte_pktmbuf_tailroom(seg) == 0);
969 /* Fix len and clear headroom for next segments. */
973 /* Update head and tail segments. */
974 *pkt_buf_next = NULL;
975 assert(pkt_buf != NULL);
977 NB_SEGS(pkt_buf) = j;
978 PORT(pkt_buf) = rxq->port_id;
979 PKT_LEN(pkt_buf) = pkt_buf_len;
980 if (rxq->csum | rxq->csum_l2tun | rxq->vlan_strip) {
981 pkt_buf->packet_type = rxq_cq_to_pkt_type(flags);
982 pkt_buf->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
983 #ifdef HAVE_EXP_DEVICE_ATTR_VLAN_OFFLOADS
984 if (flags & IBV_EXP_CQ_RX_CVLAN_STRIPPED_V1) {
985 pkt_buf->ol_flags |= PKT_RX_VLAN_PKT;
986 pkt_buf->vlan_tci = vlan_tci;
988 #endif /* HAVE_EXP_DEVICE_ATTR_VLAN_OFFLOADS */
994 #ifdef MLX5_PMD_SOFT_COUNTERS
995 /* Increment bytes counter. */
996 rxq->stats.ibytes += pkt_buf_len;
999 ret = rxq->recv(rxq->wq, elt->sges, RTE_DIM(elt->sges));
1000 if (unlikely(ret)) {
1001 /* Inability to repost WRs is fatal. */
1002 DEBUG("%p: recv_sg_list(): failed (ret=%d)",
1007 if (++elts_head >= elts_n)
1011 if (unlikely(i == 0))
1013 rxq->elts_head = elts_head;
1014 #ifdef MLX5_PMD_SOFT_COUNTERS
1015 /* Increment packets counter. */
1016 rxq->stats.ipackets += pkts_ret;
1022 * DPDK callback for RX.
1024 * The following function is the same as mlx5_rx_burst_sp(), except it doesn't
1025 * manage scattered packets. Improves performance when MRU is lower than the
1026 * size of the first segment.
1029 * Generic pointer to RX queue structure.
1031 * Array to store received packets.
1033 * Maximum number of packets in array.
1036 * Number of packets successfully received (<= pkts_n).
1039 mlx5_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
1041 struct rxq *rxq = (struct rxq *)dpdk_rxq;
1042 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
1043 const unsigned int elts_n = rxq->elts_n;
1044 unsigned int elts_head = rxq->elts_head;
1045 struct ibv_sge sges[pkts_n];
1047 unsigned int pkts_ret = 0;
1050 if (unlikely(rxq->sp))
1051 return mlx5_rx_burst_sp(dpdk_rxq, pkts, pkts_n);
1052 for (i = 0; (i != pkts_n); ++i) {
1053 struct rxq_elt *elt = &(*elts)[elts_head];
1055 struct rte_mbuf *seg = elt->buf;
1056 struct rte_mbuf *rep;
1060 /* Sanity checks. */
1061 assert(seg != NULL);
1062 assert(elts_head < rxq->elts_n);
1063 assert(rxq->elts_head < rxq->elts_n);
1065 * Fetch initial bytes of packet descriptor into a
1066 * cacheline while allocating rep.
1069 rte_prefetch0(&seg->cacheline1);
1070 ret = rxq->poll(rxq->cq, NULL, NULL, &flags, &vlan_tci);
1071 if (unlikely(ret < 0)) {
1075 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
1077 /* ibv_poll_cq() must be used in case of failure. */
1078 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
1079 if (unlikely(wcs_n == 0))
1081 if (unlikely(wcs_n < 0)) {
1082 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
1083 (void *)rxq, wcs_n);
1087 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
1088 /* Whatever, just repost the offending WR. */
1089 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
1090 " completion status (%d): %s",
1091 (void *)rxq, wc.wr_id, wc.status,
1092 ibv_wc_status_str(wc.status));
1093 #ifdef MLX5_PMD_SOFT_COUNTERS
1094 /* Increment dropped packets counter. */
1095 ++rxq->stats.idropped;
1097 /* Add SGE to array for repost. */
1105 assert(ret >= (rxq->crc_present << 2));
1106 len = ret - (rxq->crc_present << 2);
1107 rep = rte_mbuf_raw_alloc(rxq->mp);
1108 if (unlikely(rep == NULL)) {
1110 * Unable to allocate a replacement mbuf,
1113 DEBUG("rxq=%p: can't allocate a new mbuf",
1115 /* Increment out of memory counters. */
1116 ++rxq->stats.rx_nombuf;
1117 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
1121 /* Reconfigure sge to use rep instead of seg. */
1122 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
1123 assert(elt->sge.lkey == rxq->mr->lkey);
1126 /* Add SGE to array for repost. */
1129 /* Update seg information. */
1130 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
1132 PORT(seg) = rxq->port_id;
1135 DATA_LEN(seg) = len;
1136 if (rxq->csum | rxq->csum_l2tun | rxq->vlan_strip) {
1137 seg->packet_type = rxq_cq_to_pkt_type(flags);
1138 seg->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
1139 #ifdef HAVE_EXP_DEVICE_ATTR_VLAN_OFFLOADS
1140 if (flags & IBV_EXP_CQ_RX_CVLAN_STRIPPED_V1) {
1141 seg->ol_flags |= PKT_RX_VLAN_PKT;
1142 seg->vlan_tci = vlan_tci;
1144 #endif /* HAVE_EXP_DEVICE_ATTR_VLAN_OFFLOADS */
1146 /* Return packet. */
1149 #ifdef MLX5_PMD_SOFT_COUNTERS
1150 /* Increment bytes counter. */
1151 rxq->stats.ibytes += len;
1154 if (++elts_head >= elts_n)
1158 if (unlikely(i == 0))
1162 DEBUG("%p: reposting %u WRs", (void *)rxq, i);
1164 ret = rxq->recv(rxq->wq, sges, i);
1165 if (unlikely(ret)) {
1166 /* Inability to repost WRs is fatal. */
1167 DEBUG("%p: recv_burst(): failed (ret=%d)",
1172 rxq->elts_head = elts_head;
1173 #ifdef MLX5_PMD_SOFT_COUNTERS
1174 /* Increment packets counter. */
1175 rxq->stats.ipackets += pkts_ret;
1181 * Dummy DPDK callback for TX.
1183 * This function is used to temporarily replace the real callback during
1184 * unsafe control operations on the queue, or in case of error.
1187 * Generic pointer to TX queue structure.
1189 * Packets to transmit.
1191 * Number of packets in array.
1194 * Number of packets successfully transmitted (<= pkts_n).
1197 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1206 * Dummy DPDK callback for RX.
1208 * This function is used to temporarily replace the real callback during
1209 * unsafe control operations on the queue, or in case of error.
1212 * Generic pointer to RX queue structure.
1214 * Array to store received packets.
1216 * Maximum number of packets in array.
1219 * Number of packets successfully received (<= pkts_n).
1222 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)