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;
144 * Get Memory Pool (MP) from mbuf. If mbuf is indirect, the pool from which
145 * the cloned mbuf is allocated is returned instead.
151 * Memory pool where data is located for given mbuf.
153 static struct rte_mempool *
154 txq_mb2mp(struct rte_mbuf *buf)
156 if (unlikely(RTE_MBUF_INDIRECT(buf)))
157 return rte_mbuf_from_indirect(buf)->pool;
161 static inline uint32_t
162 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
163 __attribute__((always_inline));
166 * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
167 * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
168 * remove an entry first.
171 * Pointer to TX queue structure.
173 * Memory Pool for which a Memory Region lkey must be returned.
176 * mr->lkey on success, (uint32_t)-1 on failure.
178 static inline uint32_t
179 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
182 uint32_t lkey = (uint32_t)-1;
184 for (i = 0; (i != RTE_DIM(txq->mp2mr)); ++i) {
185 if (unlikely(txq->mp2mr[i].mp == NULL)) {
186 /* Unknown MP, add a new MR for it. */
189 if (txq->mp2mr[i].mp == mp) {
190 assert(txq->mp2mr[i].lkey != (uint32_t)-1);
191 assert(txq->mp2mr[i].mr->lkey == txq->mp2mr[i].lkey);
192 lkey = txq->mp2mr[i].lkey;
196 if (unlikely(lkey == (uint32_t)-1))
197 lkey = txq_mp2mr_reg(txq, mp, i);
202 * Insert VLAN using mbuf headroom space.
205 * Buffer for VLAN insertion.
208 * 0 on success, errno value on failure.
211 insert_vlan_sw(struct rte_mbuf *buf)
215 uint16_t head_room_len = rte_pktmbuf_headroom(buf);
217 if (head_room_len < 4)
220 addr = rte_pktmbuf_mtod(buf, uintptr_t);
221 vlan = htonl(0x81000000 | buf->vlan_tci);
222 memmove((void *)(addr - 4), (void *)addr, 12);
223 memcpy((void *)(addr + 8), &vlan, sizeof(vlan));
225 SET_DATA_OFF(buf, head_room_len - 4);
232 * DPDK callback for TX.
235 * Generic pointer to TX queue structure.
237 * Packets to transmit.
239 * Number of packets in array.
242 * Number of packets successfully transmitted (<= pkts_n).
245 mlx5_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
247 struct txq *txq = (struct txq *)dpdk_txq;
248 unsigned int elts_head = txq->elts_head;
249 const unsigned int elts_n = txq->elts_n;
250 unsigned int elts_comp_cd = txq->elts_comp_cd;
251 unsigned int elts_comp = 0;
255 struct rte_mbuf *buf = pkts[0];
257 assert(elts_comp_cd != 0);
258 /* Prefetch first packet cacheline. */
261 max = (elts_n - (elts_head - txq->elts_tail));
265 assert(max <= elts_n);
266 /* Always leave one free entry in the ring. */
272 for (i = 0; (i != max); ++i) {
273 struct rte_mbuf *buf_next = pkts[i + 1];
274 unsigned int elts_head_next =
275 (((elts_head + 1) == elts_n) ? 0 : elts_head + 1);
276 struct txq_elt *elt = &(*txq->elts)[elts_head];
277 uint32_t send_flags = 0;
278 #ifdef HAVE_VERBS_VLAN_INSERTION
280 #endif /* HAVE_VERBS_VLAN_INSERTION */
284 uintptr_t buf_next_addr;
287 rte_prefetch0(buf_next);
288 /* Request TX completion. */
289 if (unlikely(--elts_comp_cd == 0)) {
290 elts_comp_cd = txq->elts_comp_cd_init;
292 send_flags |= IBV_EXP_QP_BURST_SIGNALED;
294 /* Should we enable HW CKSUM offload */
296 (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM)) {
297 send_flags |= IBV_EXP_QP_BURST_IP_CSUM;
298 /* HW does not support checksum offloads at arbitrary
299 * offsets but automatically recognizes the packet
300 * type. For inner L3/L4 checksums, only VXLAN (UDP)
301 * tunnels are currently supported. */
302 if (RTE_ETH_IS_TUNNEL_PKT(buf->packet_type))
303 send_flags |= IBV_EXP_QP_BURST_TUNNEL;
305 if (buf->ol_flags & PKT_TX_VLAN_PKT) {
306 #ifdef HAVE_VERBS_VLAN_INSERTION
310 #endif /* HAVE_VERBS_VLAN_INSERTION */
312 err = insert_vlan_sw(buf);
317 /* Retrieve buffer information. */
318 addr = rte_pktmbuf_mtod(buf, uintptr_t);
319 length = DATA_LEN(buf);
320 /* Update element. */
322 if (txq->priv->sriov)
323 rte_prefetch0((volatile void *)
325 /* Prefetch next buffer data. */
328 rte_pktmbuf_mtod(buf_next, uintptr_t);
329 rte_prefetch0((volatile void *)
330 (uintptr_t)buf_next_addr);
332 /* Retrieve Memory Region key for this memory pool. */
333 lkey = txq_mp2mr(txq, txq_mb2mp(buf));
334 if (unlikely(lkey == (uint32_t)-1)) {
335 /* MR does not exist. */
336 DEBUG("%p: unable to get MP <-> MR"
337 " association", (void *)txq);
338 /* Clean up TX element. */
342 #ifdef HAVE_VERBS_VLAN_INSERTION
344 err = txq->send_pending_vlan
352 #endif /* HAVE_VERBS_VLAN_INSERTION */
353 err = txq->send_pending
361 #ifdef MLX5_PMD_SOFT_COUNTERS
362 /* Increment sent bytes counter. */
363 txq->stats.obytes += length;
366 elts_head = elts_head_next;
369 /* Take a shortcut if nothing must be sent. */
370 if (unlikely(i == 0))
372 #ifdef MLX5_PMD_SOFT_COUNTERS
373 /* Increment sent packets counter. */
374 txq->stats.opackets += i;
376 /* Ring QP doorbell. */
377 err = txq->send_flush(txq->qp);
379 /* A nonzero value is not supposed to be returned.
380 * Nothing can be done about it. */
381 DEBUG("%p: send_flush() failed with error %d",
384 txq->elts_head = elts_head;
385 txq->elts_comp += elts_comp;
386 txq->elts_comp_cd = elts_comp_cd;
391 * Translate RX completion flags to packet type.
394 * RX completion flags returned by poll_length_flags().
396 * @note: fix mlx5_dev_supported_ptypes_get() if any change here.
399 * Packet type for struct rte_mbuf.
401 static inline uint32_t
402 rxq_cq_to_pkt_type(uint32_t flags)
406 if (flags & IBV_EXP_CQ_RX_TUNNEL_PACKET)
409 IBV_EXP_CQ_RX_OUTER_IPV4_PACKET,
412 IBV_EXP_CQ_RX_OUTER_IPV6_PACKET,
415 IBV_EXP_CQ_RX_IPV4_PACKET,
416 RTE_PTYPE_INNER_L3_IPV4) |
418 IBV_EXP_CQ_RX_IPV6_PACKET,
419 RTE_PTYPE_INNER_L3_IPV6);
423 IBV_EXP_CQ_RX_IPV4_PACKET,
426 IBV_EXP_CQ_RX_IPV6_PACKET,
432 * Translate RX completion flags to offload flags.
435 * Pointer to RX queue structure.
437 * RX completion flags returned by poll_length_flags().
440 * Offload flags (ol_flags) for struct rte_mbuf.
442 static inline uint32_t
443 rxq_cq_to_ol_flags(const struct rxq *rxq, uint32_t flags)
445 uint32_t ol_flags = 0;
448 /* Set IP checksum flag only for IPv4/IPv6 packets. */
450 (IBV_EXP_CQ_RX_IPV4_PACKET | IBV_EXP_CQ_RX_IPV6_PACKET))
453 IBV_EXP_CQ_RX_IP_CSUM_OK,
454 PKT_RX_IP_CKSUM_BAD);
455 #ifdef HAVE_EXP_CQ_RX_TCP_PACKET
456 /* Set L4 checksum flag only for TCP/UDP packets. */
458 (IBV_EXP_CQ_RX_TCP_PACKET | IBV_EXP_CQ_RX_UDP_PACKET))
459 #endif /* HAVE_EXP_CQ_RX_TCP_PACKET */
462 IBV_EXP_CQ_RX_TCP_UDP_CSUM_OK,
463 PKT_RX_L4_CKSUM_BAD);
466 * PKT_RX_IP_CKSUM_BAD and PKT_RX_L4_CKSUM_BAD are used in place
467 * of PKT_RX_EIP_CKSUM_BAD because the latter is not functional
470 if ((flags & IBV_EXP_CQ_RX_TUNNEL_PACKET) && (rxq->csum_l2tun))
473 IBV_EXP_CQ_RX_OUTER_IP_CSUM_OK,
474 PKT_RX_IP_CKSUM_BAD) |
476 IBV_EXP_CQ_RX_OUTER_TCP_UDP_CSUM_OK,
477 PKT_RX_L4_CKSUM_BAD);
482 * DPDK callback for RX.
485 * Generic pointer to RX queue structure.
487 * Array to store received packets.
489 * Maximum number of packets in array.
492 * Number of packets successfully received (<= pkts_n).
495 mlx5_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
497 struct rxq *rxq = (struct rxq *)dpdk_rxq;
498 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts;
499 const unsigned int elts_n = rxq->elts_n;
500 unsigned int elts_head = rxq->elts_head;
501 struct ibv_sge sges[pkts_n];
503 unsigned int pkts_ret = 0;
506 for (i = 0; (i != pkts_n); ++i) {
507 struct rxq_elt *elt = &(*elts)[elts_head];
509 struct rte_mbuf *seg = elt->buf;
510 struct rte_mbuf *rep;
516 assert(elts_head < rxq->elts_n);
517 assert(rxq->elts_head < rxq->elts_n);
519 * Fetch initial bytes of packet descriptor into a
520 * cacheline while allocating rep.
522 rte_mbuf_prefetch_part1(seg);
523 rte_mbuf_prefetch_part2(seg);
524 ret = rxq->poll(rxq->cq, NULL, NULL, &flags, &vlan_tci);
525 if (unlikely(ret < 0)) {
529 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
531 /* ibv_poll_cq() must be used in case of failure. */
532 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
533 if (unlikely(wcs_n == 0))
535 if (unlikely(wcs_n < 0)) {
536 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
541 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
542 /* Whatever, just repost the offending WR. */
543 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
544 " completion status (%d): %s",
545 (void *)rxq, wc.wr_id, wc.status,
546 ibv_wc_status_str(wc.status));
547 #ifdef MLX5_PMD_SOFT_COUNTERS
548 /* Increment dropped packets counter. */
549 ++rxq->stats.idropped;
551 /* Add SGE to array for repost. */
559 assert(ret >= (rxq->crc_present << 2));
560 len = ret - (rxq->crc_present << 2);
561 rep = rte_mbuf_raw_alloc(rxq->mp);
562 if (unlikely(rep == NULL)) {
564 * Unable to allocate a replacement mbuf,
567 DEBUG("rxq=%p: can't allocate a new mbuf",
569 /* Increment out of memory counters. */
570 ++rxq->stats.rx_nombuf;
571 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
575 /* Reconfigure sge to use rep instead of seg. */
576 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
577 assert(elt->sge.lkey == rxq->mr->lkey);
580 /* Add SGE to array for repost. */
583 /* Update seg information. */
584 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
586 PORT(seg) = rxq->port_id;
590 if (rxq->csum | rxq->csum_l2tun | rxq->vlan_strip) {
591 seg->packet_type = rxq_cq_to_pkt_type(flags);
592 seg->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
593 #ifdef HAVE_EXP_DEVICE_ATTR_VLAN_OFFLOADS
594 if (flags & IBV_EXP_CQ_RX_CVLAN_STRIPPED_V1) {
595 seg->ol_flags |= PKT_RX_VLAN_PKT |
596 PKT_RX_VLAN_STRIPPED;
597 seg->vlan_tci = vlan_tci;
599 #endif /* HAVE_EXP_DEVICE_ATTR_VLAN_OFFLOADS */
604 #ifdef MLX5_PMD_SOFT_COUNTERS
605 /* Increment bytes counter. */
606 rxq->stats.ibytes += len;
609 if (++elts_head >= elts_n)
613 if (unlikely(i == 0))
617 DEBUG("%p: reposting %u WRs", (void *)rxq, i);
619 ret = rxq->recv(rxq->wq, sges, i);
621 /* Inability to repost WRs is fatal. */
622 DEBUG("%p: recv_burst(): failed (ret=%d)",
627 rxq->elts_head = elts_head;
628 #ifdef MLX5_PMD_SOFT_COUNTERS
629 /* Increment packets counter. */
630 rxq->stats.ipackets += pkts_ret;
636 * Dummy DPDK callback for TX.
638 * This function is used to temporarily replace the real callback during
639 * unsafe control operations on the queue, or in case of error.
642 * Generic pointer to TX queue structure.
644 * Packets to transmit.
646 * Number of packets in array.
649 * Number of packets successfully transmitted (<= pkts_n).
652 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
661 * Dummy DPDK callback for RX.
663 * This function is used to temporarily replace the real callback during
664 * unsafe control operations on the queue, or in case of error.
667 * Generic pointer to RX queue structure.
669 * Array to store received packets.
671 * Maximum number of packets in array.
674 * Number of packets successfully received (<= pkts_n).
677 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)