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 struct mlx5_check_mempool_data {
149 /* Called by mlx5_check_mempool() when iterating the memory chunks. */
150 static void mlx5_check_mempool_cb(struct rte_mempool *mp,
151 void *opaque, struct rte_mempool_memhdr *memhdr,
154 struct mlx5_check_mempool_data *data = opaque;
159 /* It already failed, skip the next chunks. */
162 /* It is the first chunk. */
163 if (data->start == NULL && data->end == NULL) {
164 data->start = memhdr->addr;
165 data->end = data->start + memhdr->len;
168 if (data->end == memhdr->addr) {
169 data->end += memhdr->len;
172 if (data->start == (char *)memhdr->addr + memhdr->len) {
173 data->start -= memhdr->len;
176 /* Error, mempool is not virtually contigous. */
181 * Check if a mempool can be used: it must be virtually contiguous.
184 * Pointer to memory pool.
186 * Pointer to the start address of the mempool virtual memory area
188 * Pointer to the end address of the mempool virtual memory area
191 * 0 on success (mempool is virtually contiguous), -1 on error.
193 static int mlx5_check_mempool(struct rte_mempool *mp, uintptr_t *start,
196 struct mlx5_check_mempool_data data;
198 memset(&data, 0, sizeof(data));
199 rte_mempool_mem_iter(mp, mlx5_check_mempool_cb, &data);
200 *start = (uintptr_t)data.start;
201 *end = (uintptr_t)data.end;
206 /* For best performance, this function should not be inlined. */
207 struct ibv_mr *mlx5_mp2mr(struct ibv_pd *, struct rte_mempool *)
208 __attribute__((noinline));
211 * Register mempool as a memory region.
214 * Pointer to protection domain.
216 * Pointer to memory pool.
219 * Memory region pointer, NULL in case of error.
222 mlx5_mp2mr(struct ibv_pd *pd, struct rte_mempool *mp)
224 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
229 if (mlx5_check_mempool(mp, &start, &end) != 0) {
230 ERROR("mempool %p: not virtually contiguous",
235 DEBUG("mempool %p area start=%p end=%p size=%zu",
236 (void *)mp, (void *)start, (void *)end,
237 (size_t)(end - start));
238 /* Round start and end to page boundary if found in memory segments. */
239 for (i = 0; (i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL); ++i) {
240 uintptr_t addr = (uintptr_t)ms[i].addr;
241 size_t len = ms[i].len;
242 unsigned int align = ms[i].hugepage_sz;
244 if ((start > addr) && (start < addr + len))
245 start = RTE_ALIGN_FLOOR(start, align);
246 if ((end > addr) && (end < addr + len))
247 end = RTE_ALIGN_CEIL(end, align);
249 DEBUG("mempool %p using start=%p end=%p size=%zu for MR",
250 (void *)mp, (void *)start, (void *)end,
251 (size_t)(end - start));
252 return ibv_reg_mr(pd,
255 IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE);
259 * Get Memory Pool (MP) from mbuf. If mbuf is indirect, the pool from which
260 * the cloned mbuf is allocated is returned instead.
266 * Memory pool where data is located for given mbuf.
268 static struct rte_mempool *
269 txq_mb2mp(struct rte_mbuf *buf)
271 if (unlikely(RTE_MBUF_INDIRECT(buf)))
272 return rte_mbuf_from_indirect(buf)->pool;
277 * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
278 * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
279 * remove an entry first.
282 * Pointer to TX queue structure.
284 * Memory Pool for which a Memory Region lkey must be returned.
287 * mr->lkey on success, (uint32_t)-1 on failure.
290 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
295 for (i = 0; (i != RTE_DIM(txq->mp2mr)); ++i) {
296 if (unlikely(txq->mp2mr[i].mp == NULL)) {
297 /* Unknown MP, add a new MR for it. */
300 if (txq->mp2mr[i].mp == mp) {
301 assert(txq->mp2mr[i].lkey != (uint32_t)-1);
302 assert(txq->mp2mr[i].mr->lkey == txq->mp2mr[i].lkey);
303 return txq->mp2mr[i].lkey;
306 /* Add a new entry, register MR first. */
307 DEBUG("%p: discovered new memory pool \"%s\" (%p)",
308 (void *)txq, mp->name, (void *)mp);
309 mr = mlx5_mp2mr(txq->priv->pd, mp);
310 if (unlikely(mr == NULL)) {
311 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
315 if (unlikely(i == RTE_DIM(txq->mp2mr))) {
316 /* Table is full, remove oldest entry. */
317 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
320 claim_zero(ibv_dereg_mr(txq->mp2mr[0].mr));
321 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
322 (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
324 /* Store the new entry. */
325 txq->mp2mr[i].mp = mp;
326 txq->mp2mr[i].mr = mr;
327 txq->mp2mr[i].lkey = mr->lkey;
328 DEBUG("%p: new MR lkey for MP \"%s\" (%p): 0x%08" PRIu32,
329 (void *)txq, mp->name, (void *)mp, txq->mp2mr[i].lkey);
330 return txq->mp2mr[i].lkey;
333 struct txq_mp2mr_mbuf_check_data {
338 * Callback function for rte_mempool_obj_iter() to check whether a given
339 * mempool object looks like a mbuf.
342 * The mempool pointer
344 * Context data (struct txq_mp2mr_mbuf_check_data). Contains the
349 * Object index, unused.
352 txq_mp2mr_mbuf_check(struct rte_mempool *mp, void *arg, void *obj,
353 uint32_t index __rte_unused)
355 struct txq_mp2mr_mbuf_check_data *data = arg;
356 struct rte_mbuf *buf = obj;
358 /* Check whether mbuf structure fits element size and whether mempool
359 * pointer is valid. */
360 if (sizeof(*buf) > mp->elt_size || buf->pool != mp)
365 * Iterator function for rte_mempool_walk() to register existing mempools and
366 * fill the MP to MR cache of a TX queue.
369 * Memory Pool to register.
371 * Pointer to TX queue structure.
374 txq_mp2mr_iter(struct rte_mempool *mp, void *arg)
376 struct txq *txq = arg;
377 struct txq_mp2mr_mbuf_check_data data = {
381 /* Register mempool only if the first element looks like a mbuf. */
382 if (rte_mempool_obj_iter(mp, txq_mp2mr_mbuf_check, &data) == 0 ||
389 * Insert VLAN using mbuf headroom space.
392 * Buffer for VLAN insertion.
395 * 0 on success, errno value on failure.
398 insert_vlan_sw(struct rte_mbuf *buf)
402 uint16_t head_room_len = rte_pktmbuf_headroom(buf);
404 if (head_room_len < 4)
407 addr = rte_pktmbuf_mtod(buf, uintptr_t);
408 vlan = htonl(0x81000000 | buf->vlan_tci);
409 memmove((void *)(addr - 4), (void *)addr, 12);
410 memcpy((void *)(addr + 8), &vlan, sizeof(vlan));
412 SET_DATA_OFF(buf, head_room_len - 4);
418 #if MLX5_PMD_SGE_WR_N > 1
421 * Copy scattered mbuf contents to a single linear buffer.
424 * Linear output buffer.
426 * Scattered input buffer.
429 * Number of bytes copied to the output buffer or 0 if not large enough.
432 linearize_mbuf(linear_t *linear, struct rte_mbuf *buf)
434 unsigned int size = 0;
438 unsigned int len = DATA_LEN(buf);
442 if (unlikely(size > sizeof(*linear)))
444 memcpy(&(*linear)[offset],
445 rte_pktmbuf_mtod(buf, uint8_t *),
448 } while (buf != NULL);
453 * Handle scattered buffers for mlx5_tx_burst().
456 * TX queue structure.
458 * Number of segments in buf.
460 * TX queue element to fill.
464 * Index of the linear buffer to use if necessary (normally txq->elts_head).
466 * Array filled with SGEs on success.
469 * A structure containing the processed packet size in bytes and the
470 * number of SGEs. Both fields are set to (unsigned int)-1 in case of
473 static struct tx_burst_sg_ret {
477 tx_burst_sg(struct txq *txq, unsigned int segs, struct txq_elt *elt,
478 struct rte_mbuf *buf, unsigned int elts_head,
479 struct ibv_sge (*sges)[MLX5_PMD_SGE_WR_N])
481 unsigned int sent_size = 0;
485 /* When there are too many segments, extra segments are
486 * linearized in the last SGE. */
487 if (unlikely(segs > RTE_DIM(*sges))) {
488 segs = (RTE_DIM(*sges) - 1);
491 /* Update element. */
493 /* Register segments as SGEs. */
494 for (j = 0; (j != segs); ++j) {
495 struct ibv_sge *sge = &(*sges)[j];
498 /* Retrieve Memory Region key for this memory pool. */
499 lkey = txq_mp2mr(txq, txq_mb2mp(buf));
500 if (unlikely(lkey == (uint32_t)-1)) {
501 /* MR does not exist. */
502 DEBUG("%p: unable to get MP <-> MR association",
504 /* Clean up TX element. */
509 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
511 rte_prefetch0((volatile void *)
512 (uintptr_t)sge->addr);
513 sge->length = DATA_LEN(buf);
515 sent_size += sge->length;
518 /* If buf is not NULL here and is not going to be linearized,
519 * nb_segs is not valid. */
521 assert((buf == NULL) || (linearize));
522 /* Linearize extra segments. */
524 struct ibv_sge *sge = &(*sges)[segs];
525 linear_t *linear = &(*txq->elts_linear)[elts_head];
526 unsigned int size = linearize_mbuf(linear, buf);
528 assert(segs == (RTE_DIM(*sges) - 1));
530 /* Invalid packet. */
531 DEBUG("%p: packet too large to be linearized.",
533 /* Clean up TX element. */
537 /* If MLX5_PMD_SGE_WR_N is 1, free mbuf immediately. */
538 if (RTE_DIM(*sges) == 1) {
540 struct rte_mbuf *next = NEXT(buf);
542 rte_pktmbuf_free_seg(buf);
544 } while (buf != NULL);
548 sge->addr = (uintptr_t)&(*linear)[0];
550 sge->lkey = txq->mr_linear->lkey;
552 /* Include last segment. */
555 return (struct tx_burst_sg_ret){
560 return (struct tx_burst_sg_ret){
566 #endif /* MLX5_PMD_SGE_WR_N > 1 */
569 * DPDK callback for TX.
572 * Generic pointer to TX queue structure.
574 * Packets to transmit.
576 * Number of packets in array.
579 * Number of packets successfully transmitted (<= pkts_n).
582 mlx5_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
584 struct txq *txq = (struct txq *)dpdk_txq;
585 unsigned int elts_head = txq->elts_head;
586 const unsigned int elts_n = txq->elts_n;
587 unsigned int elts_comp_cd = txq->elts_comp_cd;
588 unsigned int elts_comp = 0;
592 struct rte_mbuf *buf = pkts[0];
594 assert(elts_comp_cd != 0);
595 /* Prefetch first packet cacheline. */
598 max = (elts_n - (elts_head - txq->elts_tail));
602 assert(max <= elts_n);
603 /* Always leave one free entry in the ring. */
609 for (i = 0; (i != max); ++i) {
610 struct rte_mbuf *buf_next = pkts[i + 1];
611 unsigned int elts_head_next =
612 (((elts_head + 1) == elts_n) ? 0 : elts_head + 1);
613 struct txq_elt *elt = &(*txq->elts)[elts_head];
614 unsigned int segs = NB_SEGS(buf);
615 #ifdef MLX5_PMD_SOFT_COUNTERS
616 unsigned int sent_size = 0;
618 uint32_t send_flags = 0;
619 #ifdef HAVE_VERBS_VLAN_INSERTION
621 #endif /* HAVE_VERBS_VLAN_INSERTION */
624 rte_prefetch0(buf_next);
625 /* Request TX completion. */
626 if (unlikely(--elts_comp_cd == 0)) {
627 elts_comp_cd = txq->elts_comp_cd_init;
629 send_flags |= IBV_EXP_QP_BURST_SIGNALED;
631 /* Should we enable HW CKSUM offload */
633 (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM)) {
634 send_flags |= IBV_EXP_QP_BURST_IP_CSUM;
635 /* HW does not support checksum offloads at arbitrary
636 * offsets but automatically recognizes the packet
637 * type. For inner L3/L4 checksums, only VXLAN (UDP)
638 * tunnels are currently supported. */
639 if (RTE_ETH_IS_TUNNEL_PKT(buf->packet_type))
640 send_flags |= IBV_EXP_QP_BURST_TUNNEL;
642 if (buf->ol_flags & PKT_TX_VLAN_PKT) {
643 #ifdef HAVE_VERBS_VLAN_INSERTION
647 #endif /* HAVE_VERBS_VLAN_INSERTION */
649 err = insert_vlan_sw(buf);
654 if (likely(segs == 1)) {
658 uintptr_t buf_next_addr;
660 /* Retrieve buffer information. */
661 addr = rte_pktmbuf_mtod(buf, uintptr_t);
662 length = DATA_LEN(buf);
663 /* Update element. */
666 rte_prefetch0((volatile void *)
668 /* Prefetch next buffer data. */
671 rte_pktmbuf_mtod(buf_next, uintptr_t);
672 rte_prefetch0((volatile void *)
673 (uintptr_t)buf_next_addr);
675 /* Put packet into send queue. */
676 #if MLX5_PMD_MAX_INLINE > 0
677 if (length <= txq->max_inline) {
678 #ifdef HAVE_VERBS_VLAN_INSERTION
680 err = txq->send_pending_inline_vlan
687 #endif /* HAVE_VERBS_VLAN_INSERTION */
688 err = txq->send_pending_inline
696 /* Retrieve Memory Region key for this
698 lkey = txq_mp2mr(txq, txq_mb2mp(buf));
699 if (unlikely(lkey == (uint32_t)-1)) {
700 /* MR does not exist. */
701 DEBUG("%p: unable to get MP <-> MR"
702 " association", (void *)txq);
703 /* Clean up TX element. */
707 #ifdef HAVE_VERBS_VLAN_INSERTION
709 err = txq->send_pending_vlan
717 #endif /* HAVE_VERBS_VLAN_INSERTION */
718 err = txq->send_pending
727 #ifdef MLX5_PMD_SOFT_COUNTERS
731 #if MLX5_PMD_SGE_WR_N > 1
732 struct ibv_sge sges[MLX5_PMD_SGE_WR_N];
733 struct tx_burst_sg_ret ret;
735 ret = tx_burst_sg(txq, segs, elt, buf, elts_head,
737 if (ret.length == (unsigned int)-1)
739 /* Put SG list into send queue. */
740 #ifdef HAVE_VERBS_VLAN_INSERTION
742 err = txq->send_pending_sg_list_vlan
749 #endif /* HAVE_VERBS_VLAN_INSERTION */
750 err = txq->send_pending_sg_list
757 #ifdef MLX5_PMD_SOFT_COUNTERS
758 sent_size += ret.length;
760 #else /* MLX5_PMD_SGE_WR_N > 1 */
761 DEBUG("%p: TX scattered buffers support not"
762 " compiled in", (void *)txq);
764 #endif /* MLX5_PMD_SGE_WR_N > 1 */
766 elts_head = elts_head_next;
768 #ifdef MLX5_PMD_SOFT_COUNTERS
769 /* Increment sent bytes counter. */
770 txq->stats.obytes += sent_size;
774 /* Take a shortcut if nothing must be sent. */
775 if (unlikely(i == 0))
777 #ifdef MLX5_PMD_SOFT_COUNTERS
778 /* Increment sent packets counter. */
779 txq->stats.opackets += i;
781 /* Ring QP doorbell. */
782 err = txq->send_flush(txq->qp);
784 /* A nonzero value is not supposed to be returned.
785 * Nothing can be done about it. */
786 DEBUG("%p: send_flush() failed with error %d",
789 txq->elts_head = elts_head;
790 txq->elts_comp += elts_comp;
791 txq->elts_comp_cd = elts_comp_cd;
796 * Translate RX completion flags to packet type.
799 * RX completion flags returned by poll_length_flags().
801 * @note: fix mlx5_dev_supported_ptypes_get() if any change here.
804 * Packet type for struct rte_mbuf.
806 static inline uint32_t
807 rxq_cq_to_pkt_type(uint32_t flags)
811 if (flags & IBV_EXP_CQ_RX_TUNNEL_PACKET)
814 IBV_EXP_CQ_RX_OUTER_IPV4_PACKET,
817 IBV_EXP_CQ_RX_OUTER_IPV6_PACKET,
820 IBV_EXP_CQ_RX_IPV4_PACKET,
821 RTE_PTYPE_INNER_L3_IPV4) |
823 IBV_EXP_CQ_RX_IPV6_PACKET,
824 RTE_PTYPE_INNER_L3_IPV6);
828 IBV_EXP_CQ_RX_IPV4_PACKET,
831 IBV_EXP_CQ_RX_IPV6_PACKET,
837 * Translate RX completion flags to offload flags.
840 * Pointer to RX queue structure.
842 * RX completion flags returned by poll_length_flags().
845 * Offload flags (ol_flags) for struct rte_mbuf.
847 static inline uint32_t
848 rxq_cq_to_ol_flags(const struct rxq *rxq, uint32_t flags)
850 uint32_t ol_flags = 0;
853 /* Set IP checksum flag only for IPv4/IPv6 packets. */
855 (IBV_EXP_CQ_RX_IPV4_PACKET | IBV_EXP_CQ_RX_IPV6_PACKET))
858 IBV_EXP_CQ_RX_IP_CSUM_OK,
859 PKT_RX_IP_CKSUM_BAD);
860 #ifdef HAVE_EXP_CQ_RX_TCP_PACKET
861 /* Set L4 checksum flag only for TCP/UDP packets. */
863 (IBV_EXP_CQ_RX_TCP_PACKET | IBV_EXP_CQ_RX_UDP_PACKET))
864 #endif /* HAVE_EXP_CQ_RX_TCP_PACKET */
867 IBV_EXP_CQ_RX_TCP_UDP_CSUM_OK,
868 PKT_RX_L4_CKSUM_BAD);
871 * PKT_RX_IP_CKSUM_BAD and PKT_RX_L4_CKSUM_BAD are used in place
872 * of PKT_RX_EIP_CKSUM_BAD because the latter is not functional
875 if ((flags & IBV_EXP_CQ_RX_TUNNEL_PACKET) && (rxq->csum_l2tun))
878 IBV_EXP_CQ_RX_OUTER_IP_CSUM_OK,
879 PKT_RX_IP_CKSUM_BAD) |
881 IBV_EXP_CQ_RX_OUTER_TCP_UDP_CSUM_OK,
882 PKT_RX_L4_CKSUM_BAD);
887 * DPDK callback for RX with scattered packets support.
890 * Generic pointer to RX queue structure.
892 * Array to store received packets.
894 * Maximum number of packets in array.
897 * Number of packets successfully received (<= pkts_n).
900 mlx5_rx_burst_sp(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
902 struct rxq *rxq = (struct rxq *)dpdk_rxq;
903 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
904 const unsigned int elts_n = rxq->elts_n;
905 unsigned int elts_head = rxq->elts_head;
907 unsigned int pkts_ret = 0;
910 if (unlikely(!rxq->sp))
911 return mlx5_rx_burst(dpdk_rxq, pkts, pkts_n);
912 if (unlikely(elts == NULL)) /* See RTE_DEV_CMD_SET_MTU. */
914 for (i = 0; (i != pkts_n); ++i) {
915 struct rxq_elt_sp *elt = &(*elts)[elts_head];
917 unsigned int pkt_buf_len;
918 struct rte_mbuf *pkt_buf = NULL; /* Buffer returned in pkts. */
919 struct rte_mbuf **pkt_buf_next = &pkt_buf;
920 unsigned int seg_headroom = RTE_PKTMBUF_HEADROOM;
926 assert(elts_head < rxq->elts_n);
927 assert(rxq->elts_head < rxq->elts_n);
928 ret = rxq->poll(rxq->cq, NULL, NULL, &flags, &vlan_tci);
929 if (unlikely(ret < 0)) {
933 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
935 /* ibv_poll_cq() must be used in case of failure. */
936 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
937 if (unlikely(wcs_n == 0))
939 if (unlikely(wcs_n < 0)) {
940 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
945 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
946 /* Whatever, just repost the offending WR. */
947 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
948 " completion status (%d): %s",
949 (void *)rxq, wc.wr_id, wc.status,
950 ibv_wc_status_str(wc.status));
951 #ifdef MLX5_PMD_SOFT_COUNTERS
952 /* Increment dropped packets counter. */
953 ++rxq->stats.idropped;
961 assert(ret >= (rxq->crc_present << 2));
962 len = ret - (rxq->crc_present << 2);
965 * Replace spent segments with new ones, concatenate and
966 * return them as pkt_buf.
969 struct ibv_sge *sge = &elt->sges[j];
970 struct rte_mbuf *seg = elt->bufs[j];
971 struct rte_mbuf *rep;
972 unsigned int seg_tailroom;
976 * Fetch initial bytes of packet descriptor into a
977 * cacheline while allocating rep.
980 rep = rte_mbuf_raw_alloc(rxq->mp);
981 if (unlikely(rep == NULL)) {
983 * Unable to allocate a replacement mbuf,
986 DEBUG("rxq=%p: can't allocate a new mbuf",
988 if (pkt_buf != NULL) {
989 *pkt_buf_next = NULL;
990 rte_pktmbuf_free(pkt_buf);
992 /* Increment out of memory counters. */
993 ++rxq->stats.rx_nombuf;
994 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
998 /* Poison user-modifiable fields in rep. */
999 NEXT(rep) = (void *)((uintptr_t)-1);
1000 SET_DATA_OFF(rep, 0xdead);
1001 DATA_LEN(rep) = 0xd00d;
1002 PKT_LEN(rep) = 0xdeadd00d;
1003 NB_SEGS(rep) = 0x2a;
1007 assert(rep->buf_len == seg->buf_len);
1008 assert(rep->buf_len == rxq->mb_len);
1009 /* Reconfigure sge to use rep instead of seg. */
1010 assert(sge->lkey == rxq->mr->lkey);
1011 sge->addr = ((uintptr_t)rep->buf_addr + seg_headroom);
1014 /* Update pkt_buf if it's the first segment, or link
1015 * seg to the previous one and update pkt_buf_next. */
1016 *pkt_buf_next = seg;
1017 pkt_buf_next = &NEXT(seg);
1018 /* Update seg information. */
1019 seg_tailroom = (seg->buf_len - seg_headroom);
1020 assert(sge->length == seg_tailroom);
1021 SET_DATA_OFF(seg, seg_headroom);
1022 if (likely(len <= seg_tailroom)) {
1024 DATA_LEN(seg) = len;
1027 assert(rte_pktmbuf_headroom(seg) ==
1029 assert(rte_pktmbuf_tailroom(seg) ==
1030 (seg_tailroom - len));
1033 DATA_LEN(seg) = seg_tailroom;
1034 PKT_LEN(seg) = seg_tailroom;
1036 assert(rte_pktmbuf_headroom(seg) == seg_headroom);
1037 assert(rte_pktmbuf_tailroom(seg) == 0);
1038 /* Fix len and clear headroom for next segments. */
1039 len -= seg_tailroom;
1042 /* Update head and tail segments. */
1043 *pkt_buf_next = NULL;
1044 assert(pkt_buf != NULL);
1046 NB_SEGS(pkt_buf) = j;
1047 PORT(pkt_buf) = rxq->port_id;
1048 PKT_LEN(pkt_buf) = pkt_buf_len;
1049 if (rxq->csum | rxq->csum_l2tun | rxq->vlan_strip) {
1050 pkt_buf->packet_type = rxq_cq_to_pkt_type(flags);
1051 pkt_buf->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
1052 #ifdef HAVE_EXP_DEVICE_ATTR_VLAN_OFFLOADS
1053 if (flags & IBV_EXP_CQ_RX_CVLAN_STRIPPED_V1) {
1054 pkt_buf->ol_flags |= PKT_RX_VLAN_PKT |
1055 PKT_RX_VLAN_STRIPPED;
1056 pkt_buf->vlan_tci = vlan_tci;
1058 #endif /* HAVE_EXP_DEVICE_ATTR_VLAN_OFFLOADS */
1061 /* Return packet. */
1062 *(pkts++) = pkt_buf;
1064 #ifdef MLX5_PMD_SOFT_COUNTERS
1065 /* Increment bytes counter. */
1066 rxq->stats.ibytes += pkt_buf_len;
1069 ret = rxq->recv(rxq->wq, elt->sges, RTE_DIM(elt->sges));
1070 if (unlikely(ret)) {
1071 /* Inability to repost WRs is fatal. */
1072 DEBUG("%p: recv_sg_list(): failed (ret=%d)",
1077 if (++elts_head >= elts_n)
1081 if (unlikely(i == 0))
1083 rxq->elts_head = elts_head;
1084 #ifdef MLX5_PMD_SOFT_COUNTERS
1085 /* Increment packets counter. */
1086 rxq->stats.ipackets += pkts_ret;
1092 * DPDK callback for RX.
1094 * The following function is the same as mlx5_rx_burst_sp(), except it doesn't
1095 * manage scattered packets. Improves performance when MRU is lower than the
1096 * size of the first segment.
1099 * Generic pointer to RX queue structure.
1101 * Array to store received packets.
1103 * Maximum number of packets in array.
1106 * Number of packets successfully received (<= pkts_n).
1109 mlx5_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
1111 struct rxq *rxq = (struct rxq *)dpdk_rxq;
1112 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
1113 const unsigned int elts_n = rxq->elts_n;
1114 unsigned int elts_head = rxq->elts_head;
1115 struct ibv_sge sges[pkts_n];
1117 unsigned int pkts_ret = 0;
1120 if (unlikely(rxq->sp))
1121 return mlx5_rx_burst_sp(dpdk_rxq, pkts, pkts_n);
1122 for (i = 0; (i != pkts_n); ++i) {
1123 struct rxq_elt *elt = &(*elts)[elts_head];
1125 struct rte_mbuf *seg = elt->buf;
1126 struct rte_mbuf *rep;
1130 /* Sanity checks. */
1131 assert(seg != NULL);
1132 assert(elts_head < rxq->elts_n);
1133 assert(rxq->elts_head < rxq->elts_n);
1135 * Fetch initial bytes of packet descriptor into a
1136 * cacheline while allocating rep.
1138 rte_mbuf_prefetch_part1(seg);
1139 rte_mbuf_prefetch_part2(seg);
1140 ret = rxq->poll(rxq->cq, NULL, NULL, &flags, &vlan_tci);
1141 if (unlikely(ret < 0)) {
1145 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
1147 /* ibv_poll_cq() must be used in case of failure. */
1148 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
1149 if (unlikely(wcs_n == 0))
1151 if (unlikely(wcs_n < 0)) {
1152 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
1153 (void *)rxq, wcs_n);
1157 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
1158 /* Whatever, just repost the offending WR. */
1159 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
1160 " completion status (%d): %s",
1161 (void *)rxq, wc.wr_id, wc.status,
1162 ibv_wc_status_str(wc.status));
1163 #ifdef MLX5_PMD_SOFT_COUNTERS
1164 /* Increment dropped packets counter. */
1165 ++rxq->stats.idropped;
1167 /* Add SGE to array for repost. */
1175 assert(ret >= (rxq->crc_present << 2));
1176 len = ret - (rxq->crc_present << 2);
1177 rep = rte_mbuf_raw_alloc(rxq->mp);
1178 if (unlikely(rep == NULL)) {
1180 * Unable to allocate a replacement mbuf,
1183 DEBUG("rxq=%p: can't allocate a new mbuf",
1185 /* Increment out of memory counters. */
1186 ++rxq->stats.rx_nombuf;
1187 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
1191 /* Reconfigure sge to use rep instead of seg. */
1192 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
1193 assert(elt->sge.lkey == rxq->mr->lkey);
1196 /* Add SGE to array for repost. */
1199 /* Update seg information. */
1200 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
1202 PORT(seg) = rxq->port_id;
1205 DATA_LEN(seg) = len;
1206 if (rxq->csum | rxq->csum_l2tun | rxq->vlan_strip) {
1207 seg->packet_type = rxq_cq_to_pkt_type(flags);
1208 seg->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
1209 #ifdef HAVE_EXP_DEVICE_ATTR_VLAN_OFFLOADS
1210 if (flags & IBV_EXP_CQ_RX_CVLAN_STRIPPED_V1) {
1211 seg->ol_flags |= PKT_RX_VLAN_PKT |
1212 PKT_RX_VLAN_STRIPPED;
1213 seg->vlan_tci = vlan_tci;
1215 #endif /* HAVE_EXP_DEVICE_ATTR_VLAN_OFFLOADS */
1217 /* Return packet. */
1220 #ifdef MLX5_PMD_SOFT_COUNTERS
1221 /* Increment bytes counter. */
1222 rxq->stats.ibytes += len;
1225 if (++elts_head >= elts_n)
1229 if (unlikely(i == 0))
1233 DEBUG("%p: reposting %u WRs", (void *)rxq, i);
1235 ret = rxq->recv(rxq->wq, sges, i);
1236 if (unlikely(ret)) {
1237 /* Inability to repost WRs is fatal. */
1238 DEBUG("%p: recv_burst(): failed (ret=%d)",
1243 rxq->elts_head = elts_head;
1244 #ifdef MLX5_PMD_SOFT_COUNTERS
1245 /* Increment packets counter. */
1246 rxq->stats.ipackets += pkts_ret;
1252 * Dummy DPDK callback for TX.
1254 * This function is used to temporarily replace the real callback during
1255 * unsafe control operations on the queue, or in case of error.
1258 * Generic pointer to TX queue structure.
1260 * Packets to transmit.
1262 * Number of packets in array.
1265 * Number of packets successfully transmitted (<= pkts_n).
1268 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1277 * Dummy DPDK callback for RX.
1279 * This function is used to temporarily replace the real callback during
1280 * unsafe control operations on the queue, or in case of error.
1283 * Generic pointer to RX queue structure.
1285 * Array to store received packets.
1287 * Maximum number of packets in array.
1290 * Number of packets successfully received (<= pkts_n).
1293 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)