mlx5: fix memory registration for indirect mbuf data
[dpdk.git] / drivers / net / mlx5 / mlx5_rxtx.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2015 6WIND S.A.
5  *   Copyright 2015 Mellanox.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include <assert.h>
35 #include <stdint.h>
36 #include <string.h>
37 #include <stdlib.h>
38
39 /* Verbs header. */
40 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
41 #ifdef PEDANTIC
42 #pragma GCC diagnostic ignored "-pedantic"
43 #endif
44 #include <infiniband/verbs.h>
45 #ifdef PEDANTIC
46 #pragma GCC diagnostic error "-pedantic"
47 #endif
48
49 /* DPDK headers don't like -pedantic. */
50 #ifdef PEDANTIC
51 #pragma GCC diagnostic ignored "-pedantic"
52 #endif
53 #include <rte_mbuf.h>
54 #include <rte_mempool.h>
55 #include <rte_prefetch.h>
56 #include <rte_common.h>
57 #include <rte_branch_prediction.h>
58 #ifdef PEDANTIC
59 #pragma GCC diagnostic error "-pedantic"
60 #endif
61
62 #include "mlx5.h"
63 #include "mlx5_utils.h"
64 #include "mlx5_rxtx.h"
65 #include "mlx5_defs.h"
66
67 /**
68  * Manage TX completions.
69  *
70  * When sending a burst, mlx5_tx_burst() posts several WRs.
71  * To improve performance, a completion event is only required once every
72  * MLX5_PMD_TX_PER_COMP_REQ sends. Doing so discards completion information
73  * for other WRs, but this information would not be used anyway.
74  *
75  * @param txq
76  *   Pointer to TX queue structure.
77  *
78  * @return
79  *   0 on success, -1 on failure.
80  */
81 static int
82 txq_complete(struct txq *txq)
83 {
84         unsigned int elts_comp = txq->elts_comp;
85         unsigned int elts_tail = txq->elts_tail;
86         const unsigned int elts_n = txq->elts_n;
87         int wcs_n;
88
89         if (unlikely(elts_comp == 0))
90                 return 0;
91 #ifdef DEBUG_SEND
92         DEBUG("%p: processing %u work requests completions",
93               (void *)txq, elts_comp);
94 #endif
95         wcs_n = txq->if_cq->poll_cnt(txq->cq, elts_comp);
96         if (unlikely(wcs_n == 0))
97                 return 0;
98         if (unlikely(wcs_n < 0)) {
99                 DEBUG("%p: ibv_poll_cq() failed (wcs_n=%d)",
100                       (void *)txq, wcs_n);
101                 return -1;
102         }
103         elts_comp -= wcs_n;
104         assert(elts_comp <= txq->elts_comp);
105         /*
106          * Assume WC status is successful as nothing can be done about it
107          * anyway.
108          */
109         elts_tail += wcs_n * txq->elts_comp_cd_init;
110         if (elts_tail >= elts_n)
111                 elts_tail -= elts_n;
112         txq->elts_tail = elts_tail;
113         txq->elts_comp = elts_comp;
114         return 0;
115 }
116
117 /**
118  * Get Memory Pool (MP) from mbuf. If mbuf is indirect, the pool from which
119  * the cloned mbuf is allocated is returned instead.
120  *
121  * @param buf
122  *   Pointer to mbuf.
123  *
124  * @return
125  *   Memory pool where data is located for given mbuf.
126  */
127 static struct rte_mempool *
128 txq_mb2mp(struct rte_mbuf *buf)
129 {
130         if (unlikely(RTE_MBUF_INDIRECT(buf)))
131                 return rte_mbuf_from_indirect(buf)->pool;
132         return buf->pool;
133 }
134
135 /**
136  * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
137  * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
138  * remove an entry first.
139  *
140  * @param txq
141  *   Pointer to TX queue structure.
142  * @param[in] mp
143  *   Memory Pool for which a Memory Region lkey must be returned.
144  *
145  * @return
146  *   mr->lkey on success, (uint32_t)-1 on failure.
147  */
148 static uint32_t
149 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
150 {
151         unsigned int i;
152         struct ibv_mr *mr;
153
154         for (i = 0; (i != RTE_DIM(txq->mp2mr)); ++i) {
155                 if (unlikely(txq->mp2mr[i].mp == NULL)) {
156                         /* Unknown MP, add a new MR for it. */
157                         break;
158                 }
159                 if (txq->mp2mr[i].mp == mp) {
160                         assert(txq->mp2mr[i].lkey != (uint32_t)-1);
161                         assert(txq->mp2mr[i].mr->lkey == txq->mp2mr[i].lkey);
162                         return txq->mp2mr[i].lkey;
163                 }
164         }
165         /* Add a new entry, register MR first. */
166         DEBUG("%p: discovered new memory pool %p", (void *)txq, (void *)mp);
167         mr = ibv_reg_mr(txq->priv->pd,
168                         (void *)mp->elt_va_start,
169                         (mp->elt_va_end - mp->elt_va_start),
170                         (IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE));
171         if (unlikely(mr == NULL)) {
172                 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
173                       (void *)txq);
174                 return (uint32_t)-1;
175         }
176         if (unlikely(i == RTE_DIM(txq->mp2mr))) {
177                 /* Table is full, remove oldest entry. */
178                 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
179                       (void *)txq);
180                 --i;
181                 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
182                 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
183                         (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
184         }
185         /* Store the new entry. */
186         txq->mp2mr[i].mp = mp;
187         txq->mp2mr[i].mr = mr;
188         txq->mp2mr[i].lkey = mr->lkey;
189         DEBUG("%p: new MR lkey for MP %p: 0x%08" PRIu32,
190               (void *)txq, (void *)mp, txq->mp2mr[i].lkey);
191         return txq->mp2mr[i].lkey;
192 }
193
194 #if MLX5_PMD_SGE_WR_N > 1
195
196 /**
197  * Copy scattered mbuf contents to a single linear buffer.
198  *
199  * @param[out] linear
200  *   Linear output buffer.
201  * @param[in] buf
202  *   Scattered input buffer.
203  *
204  * @return
205  *   Number of bytes copied to the output buffer or 0 if not large enough.
206  */
207 static unsigned int
208 linearize_mbuf(linear_t *linear, struct rte_mbuf *buf)
209 {
210         unsigned int size = 0;
211         unsigned int offset;
212
213         do {
214                 unsigned int len = DATA_LEN(buf);
215
216                 offset = size;
217                 size += len;
218                 if (unlikely(size > sizeof(*linear)))
219                         return 0;
220                 memcpy(&(*linear)[offset],
221                        rte_pktmbuf_mtod(buf, uint8_t *),
222                        len);
223                 buf = NEXT(buf);
224         } while (buf != NULL);
225         return size;
226 }
227
228 /**
229  * Handle scattered buffers for mlx5_tx_burst().
230  *
231  * @param txq
232  *   TX queue structure.
233  * @param segs
234  *   Number of segments in buf.
235  * @param elt
236  *   TX queue element to fill.
237  * @param[in] buf
238  *   Buffer to process.
239  * @param elts_head
240  *   Index of the linear buffer to use if necessary (normally txq->elts_head).
241  * @param[out] sges
242  *   Array filled with SGEs on success.
243  *
244  * @return
245  *   A structure containing the processed packet size in bytes and the
246  *   number of SGEs. Both fields are set to (unsigned int)-1 in case of
247  *   failure.
248  */
249 static struct tx_burst_sg_ret {
250         unsigned int length;
251         unsigned int num;
252 }
253 tx_burst_sg(struct txq *txq, unsigned int segs, struct txq_elt *elt,
254             struct rte_mbuf *buf, unsigned int elts_head,
255             struct ibv_sge (*sges)[MLX5_PMD_SGE_WR_N])
256 {
257         unsigned int sent_size = 0;
258         unsigned int j;
259         int linearize = 0;
260
261         /* When there are too many segments, extra segments are
262          * linearized in the last SGE. */
263         if (unlikely(segs > RTE_DIM(*sges))) {
264                 segs = (RTE_DIM(*sges) - 1);
265                 linearize = 1;
266         }
267         /* Update element. */
268         elt->buf = buf;
269         /* Register segments as SGEs. */
270         for (j = 0; (j != segs); ++j) {
271                 struct ibv_sge *sge = &(*sges)[j];
272                 uint32_t lkey;
273
274                 /* Retrieve Memory Region key for this memory pool. */
275                 lkey = txq_mp2mr(txq, txq_mb2mp(buf));
276                 if (unlikely(lkey == (uint32_t)-1)) {
277                         /* MR does not exist. */
278                         DEBUG("%p: unable to get MP <-> MR association",
279                               (void *)txq);
280                         /* Clean up TX element. */
281                         elt->buf = NULL;
282                         goto stop;
283                 }
284                 /* Update SGE. */
285                 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
286                 if (txq->priv->vf)
287                         rte_prefetch0((volatile void *)
288                                       (uintptr_t)sge->addr);
289                 sge->length = DATA_LEN(buf);
290                 sge->lkey = lkey;
291                 sent_size += sge->length;
292                 buf = NEXT(buf);
293         }
294         /* If buf is not NULL here and is not going to be linearized,
295          * nb_segs is not valid. */
296         assert(j == segs);
297         assert((buf == NULL) || (linearize));
298         /* Linearize extra segments. */
299         if (linearize) {
300                 struct ibv_sge *sge = &(*sges)[segs];
301                 linear_t *linear = &(*txq->elts_linear)[elts_head];
302                 unsigned int size = linearize_mbuf(linear, buf);
303
304                 assert(segs == (RTE_DIM(*sges) - 1));
305                 if (size == 0) {
306                         /* Invalid packet. */
307                         DEBUG("%p: packet too large to be linearized.",
308                               (void *)txq);
309                         /* Clean up TX element. */
310                         elt->buf = NULL;
311                         goto stop;
312                 }
313                 /* If MLX5_PMD_SGE_WR_N is 1, free mbuf immediately. */
314                 if (RTE_DIM(*sges) == 1) {
315                         do {
316                                 struct rte_mbuf *next = NEXT(buf);
317
318                                 rte_pktmbuf_free_seg(buf);
319                                 buf = next;
320                         } while (buf != NULL);
321                         elt->buf = NULL;
322                 }
323                 /* Update SGE. */
324                 sge->addr = (uintptr_t)&(*linear)[0];
325                 sge->length = size;
326                 sge->lkey = txq->mr_linear->lkey;
327                 sent_size += size;
328         }
329         return (struct tx_burst_sg_ret){
330                 .length = sent_size,
331                 .num = segs,
332         };
333 stop:
334         return (struct tx_burst_sg_ret){
335                 .length = -1,
336                 .num = -1,
337         };
338 }
339
340 #endif /* MLX5_PMD_SGE_WR_N > 1 */
341
342 /**
343  * DPDK callback for TX.
344  *
345  * @param dpdk_txq
346  *   Generic pointer to TX queue structure.
347  * @param[in] pkts
348  *   Packets to transmit.
349  * @param pkts_n
350  *   Number of packets in array.
351  *
352  * @return
353  *   Number of packets successfully transmitted (<= pkts_n).
354  */
355 uint16_t
356 mlx5_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
357 {
358         struct txq *txq = (struct txq *)dpdk_txq;
359         unsigned int elts_head = txq->elts_head;
360         const unsigned int elts_tail = txq->elts_tail;
361         const unsigned int elts_n = txq->elts_n;
362         unsigned int elts_comp_cd = txq->elts_comp_cd;
363         unsigned int elts_comp = 0;
364         unsigned int i;
365         unsigned int max;
366         int err;
367
368         assert(elts_comp_cd != 0);
369         txq_complete(txq);
370         max = (elts_n - (elts_head - elts_tail));
371         if (max > elts_n)
372                 max -= elts_n;
373         assert(max >= 1);
374         assert(max <= elts_n);
375         /* Always leave one free entry in the ring. */
376         --max;
377         if (max == 0)
378                 return 0;
379         if (max > pkts_n)
380                 max = pkts_n;
381         for (i = 0; (i != max); ++i) {
382                 struct rte_mbuf *buf = pkts[i];
383                 unsigned int elts_head_next =
384                         (((elts_head + 1) == elts_n) ? 0 : elts_head + 1);
385                 struct txq_elt *elt_next = &(*txq->elts)[elts_head_next];
386                 struct txq_elt *elt = &(*txq->elts)[elts_head];
387                 unsigned int segs = NB_SEGS(buf);
388 #ifdef MLX5_PMD_SOFT_COUNTERS
389                 unsigned int sent_size = 0;
390 #endif
391                 uint32_t send_flags = 0;
392
393                 /* Clean up old buffer. */
394                 if (likely(elt->buf != NULL)) {
395                         struct rte_mbuf *tmp = elt->buf;
396
397                         /* Faster than rte_pktmbuf_free(). */
398                         do {
399                                 struct rte_mbuf *next = NEXT(tmp);
400
401                                 rte_pktmbuf_free_seg(tmp);
402                                 tmp = next;
403                         } while (tmp != NULL);
404                 }
405                 /* Request TX completion. */
406                 if (unlikely(--elts_comp_cd == 0)) {
407                         elts_comp_cd = txq->elts_comp_cd_init;
408                         ++elts_comp;
409                         send_flags |= IBV_EXP_QP_BURST_SIGNALED;
410                 }
411                 /* Should we enable HW CKSUM offload */
412                 if (buf->ol_flags &
413                     (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM)) {
414                         send_flags |= IBV_EXP_QP_BURST_IP_CSUM;
415                         /* HW does not support checksum offloads at arbitrary
416                          * offsets but automatically recognizes the packet
417                          * type. For inner L3/L4 checksums, only VXLAN (UDP)
418                          * tunnels are currently supported. */
419                         if (RTE_ETH_IS_TUNNEL_PKT(buf->packet_type))
420                                 send_flags |= IBV_EXP_QP_BURST_TUNNEL;
421                 }
422                 if (likely(segs == 1)) {
423                         uintptr_t addr;
424                         uint32_t length;
425                         uint32_t lkey;
426
427                         /* Retrieve buffer information. */
428                         addr = rte_pktmbuf_mtod(buf, uintptr_t);
429                         length = DATA_LEN(buf);
430                         /* Retrieve Memory Region key for this memory pool. */
431                         lkey = txq_mp2mr(txq, txq_mb2mp(buf));
432                         if (unlikely(lkey == (uint32_t)-1)) {
433                                 /* MR does not exist. */
434                                 DEBUG("%p: unable to get MP <-> MR"
435                                       " association", (void *)txq);
436                                 /* Clean up TX element. */
437                                 elt->buf = NULL;
438                                 goto stop;
439                         }
440                         /* Update element. */
441                         elt->buf = buf;
442                         if (txq->priv->vf)
443                                 rte_prefetch0((volatile void *)
444                                               (uintptr_t)addr);
445                         RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
446                         /* Put packet into send queue. */
447 #if MLX5_PMD_MAX_INLINE > 0
448                         if (length <= txq->max_inline)
449                                 err = txq->if_qp->send_pending_inline
450                                         (txq->qp,
451                                          (void *)addr,
452                                          length,
453                                          send_flags);
454                         else
455 #endif
456                                 err = txq->if_qp->send_pending
457                                         (txq->qp,
458                                          addr,
459                                          length,
460                                          lkey,
461                                          send_flags);
462                         if (unlikely(err))
463                                 goto stop;
464 #ifdef MLX5_PMD_SOFT_COUNTERS
465                         sent_size += length;
466 #endif
467                 } else {
468 #if MLX5_PMD_SGE_WR_N > 1
469                         struct ibv_sge sges[MLX5_PMD_SGE_WR_N];
470                         struct tx_burst_sg_ret ret;
471
472                         ret = tx_burst_sg(txq, segs, elt, buf, elts_head,
473                                           &sges);
474                         if (ret.length == (unsigned int)-1)
475                                 goto stop;
476                         RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
477                         /* Put SG list into send queue. */
478                         err = txq->if_qp->send_pending_sg_list
479                                 (txq->qp,
480                                  sges,
481                                  ret.num,
482                                  send_flags);
483                         if (unlikely(err))
484                                 goto stop;
485 #ifdef MLX5_PMD_SOFT_COUNTERS
486                         sent_size += ret.length;
487 #endif
488 #else /* MLX5_PMD_SGE_WR_N > 1 */
489                         DEBUG("%p: TX scattered buffers support not"
490                               " compiled in", (void *)txq);
491                         goto stop;
492 #endif /* MLX5_PMD_SGE_WR_N > 1 */
493                 }
494                 elts_head = elts_head_next;
495 #ifdef MLX5_PMD_SOFT_COUNTERS
496                 /* Increment sent bytes counter. */
497                 txq->stats.obytes += sent_size;
498 #endif
499         }
500 stop:
501         /* Take a shortcut if nothing must be sent. */
502         if (unlikely(i == 0))
503                 return 0;
504 #ifdef MLX5_PMD_SOFT_COUNTERS
505         /* Increment sent packets counter. */
506         txq->stats.opackets += i;
507 #endif
508         /* Ring QP doorbell. */
509         err = txq->if_qp->send_flush(txq->qp);
510         if (unlikely(err)) {
511                 /* A nonzero value is not supposed to be returned.
512                  * Nothing can be done about it. */
513                 DEBUG("%p: send_flush() failed with error %d",
514                       (void *)txq, err);
515         }
516         txq->elts_head = elts_head;
517         txq->elts_comp += elts_comp;
518         txq->elts_comp_cd = elts_comp_cd;
519         return i;
520 }
521
522 /**
523  * Translate RX completion flags to packet type.
524  *
525  * @param flags
526  *   RX completion flags returned by poll_length_flags().
527  *
528  * @return
529  *   Packet type for struct rte_mbuf.
530  */
531 static inline uint32_t
532 rxq_cq_to_pkt_type(uint32_t flags)
533 {
534         uint32_t pkt_type;
535
536         if (flags & IBV_EXP_CQ_RX_TUNNEL_PACKET)
537                 pkt_type =
538                         TRANSPOSE(flags,
539                                   IBV_EXP_CQ_RX_OUTER_IPV4_PACKET,
540                                   RTE_PTYPE_L3_IPV4) |
541                         TRANSPOSE(flags,
542                                   IBV_EXP_CQ_RX_OUTER_IPV6_PACKET,
543                                   RTE_PTYPE_L3_IPV6) |
544                         TRANSPOSE(flags,
545                                   IBV_EXP_CQ_RX_IPV4_PACKET,
546                                   RTE_PTYPE_INNER_L3_IPV4) |
547                         TRANSPOSE(flags,
548                                   IBV_EXP_CQ_RX_IPV6_PACKET,
549                                   RTE_PTYPE_INNER_L3_IPV6);
550         else
551                 pkt_type =
552                         TRANSPOSE(flags,
553                                   IBV_EXP_CQ_RX_IPV4_PACKET,
554                                   RTE_PTYPE_L3_IPV4) |
555                         TRANSPOSE(flags,
556                                   IBV_EXP_CQ_RX_IPV6_PACKET,
557                                   RTE_PTYPE_L3_IPV6);
558         return pkt_type;
559 }
560
561 /**
562  * Translate RX completion flags to offload flags.
563  *
564  * @param[in] rxq
565  *   Pointer to RX queue structure.
566  * @param flags
567  *   RX completion flags returned by poll_length_flags().
568  *
569  * @return
570  *   Offload flags (ol_flags) for struct rte_mbuf.
571  */
572 static inline uint32_t
573 rxq_cq_to_ol_flags(const struct rxq *rxq, uint32_t flags)
574 {
575         uint32_t ol_flags = 0;
576
577         if (rxq->csum)
578                 ol_flags |=
579                         TRANSPOSE(~flags,
580                                   IBV_EXP_CQ_RX_IP_CSUM_OK,
581                                   PKT_RX_IP_CKSUM_BAD) |
582                         TRANSPOSE(~flags,
583                                   IBV_EXP_CQ_RX_TCP_UDP_CSUM_OK,
584                                   PKT_RX_L4_CKSUM_BAD);
585         /*
586          * PKT_RX_IP_CKSUM_BAD and PKT_RX_L4_CKSUM_BAD are used in place
587          * of PKT_RX_EIP_CKSUM_BAD because the latter is not functional
588          * (its value is 0).
589          */
590         if ((flags & IBV_EXP_CQ_RX_TUNNEL_PACKET) && (rxq->csum_l2tun))
591                 ol_flags |=
592                         TRANSPOSE(~flags,
593                                   IBV_EXP_CQ_RX_OUTER_IP_CSUM_OK,
594                                   PKT_RX_IP_CKSUM_BAD) |
595                         TRANSPOSE(~flags,
596                                   IBV_EXP_CQ_RX_OUTER_TCP_UDP_CSUM_OK,
597                                   PKT_RX_L4_CKSUM_BAD);
598         return ol_flags;
599 }
600
601 /**
602  * DPDK callback for RX with scattered packets support.
603  *
604  * @param dpdk_rxq
605  *   Generic pointer to RX queue structure.
606  * @param[out] pkts
607  *   Array to store received packets.
608  * @param pkts_n
609  *   Maximum number of packets in array.
610  *
611  * @return
612  *   Number of packets successfully received (<= pkts_n).
613  */
614 uint16_t
615 mlx5_rx_burst_sp(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
616 {
617         struct rxq *rxq = (struct rxq *)dpdk_rxq;
618         struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
619         const unsigned int elts_n = rxq->elts_n;
620         unsigned int elts_head = rxq->elts_head;
621         unsigned int i;
622         unsigned int pkts_ret = 0;
623         int ret;
624
625         if (unlikely(!rxq->sp))
626                 return mlx5_rx_burst(dpdk_rxq, pkts, pkts_n);
627         if (unlikely(elts == NULL)) /* See RTE_DEV_CMD_SET_MTU. */
628                 return 0;
629         for (i = 0; (i != pkts_n); ++i) {
630                 struct rxq_elt_sp *elt = &(*elts)[elts_head];
631                 unsigned int len;
632                 unsigned int pkt_buf_len;
633                 struct rte_mbuf *pkt_buf = NULL; /* Buffer returned in pkts. */
634                 struct rte_mbuf **pkt_buf_next = &pkt_buf;
635                 unsigned int seg_headroom = RTE_PKTMBUF_HEADROOM;
636                 unsigned int j = 0;
637                 uint32_t flags;
638
639                 /* Sanity checks. */
640                 assert(elts_head < rxq->elts_n);
641                 assert(rxq->elts_head < rxq->elts_n);
642                 ret = rxq->if_cq->poll_length_flags(rxq->cq, NULL, NULL,
643                                                     &flags);
644                 if (unlikely(ret < 0)) {
645                         struct ibv_wc wc;
646                         int wcs_n;
647
648                         DEBUG("rxq=%p, poll_length() failed (ret=%d)",
649                               (void *)rxq, ret);
650                         /* ibv_poll_cq() must be used in case of failure. */
651                         wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
652                         if (unlikely(wcs_n == 0))
653                                 break;
654                         if (unlikely(wcs_n < 0)) {
655                                 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
656                                       (void *)rxq, wcs_n);
657                                 break;
658                         }
659                         assert(wcs_n == 1);
660                         if (unlikely(wc.status != IBV_WC_SUCCESS)) {
661                                 /* Whatever, just repost the offending WR. */
662                                 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
663                                       " completion status (%d): %s",
664                                       (void *)rxq, wc.wr_id, wc.status,
665                                       ibv_wc_status_str(wc.status));
666 #ifdef MLX5_PMD_SOFT_COUNTERS
667                                 /* Increment dropped packets counter. */
668                                 ++rxq->stats.idropped;
669 #endif
670                                 goto repost;
671                         }
672                         ret = wc.byte_len;
673                 }
674                 if (ret == 0)
675                         break;
676                 len = ret;
677                 pkt_buf_len = len;
678                 /*
679                  * Replace spent segments with new ones, concatenate and
680                  * return them as pkt_buf.
681                  */
682                 while (1) {
683                         struct ibv_sge *sge = &elt->sges[j];
684                         struct rte_mbuf *seg = elt->bufs[j];
685                         struct rte_mbuf *rep;
686                         unsigned int seg_tailroom;
687
688                         assert(seg != NULL);
689                         /*
690                          * Fetch initial bytes of packet descriptor into a
691                          * cacheline while allocating rep.
692                          */
693                         rte_prefetch0(seg);
694                         rep = __rte_mbuf_raw_alloc(rxq->mp);
695                         if (unlikely(rep == NULL)) {
696                                 /*
697                                  * Unable to allocate a replacement mbuf,
698                                  * repost WR.
699                                  */
700                                 DEBUG("rxq=%p: can't allocate a new mbuf",
701                                       (void *)rxq);
702                                 if (pkt_buf != NULL) {
703                                         *pkt_buf_next = NULL;
704                                         rte_pktmbuf_free(pkt_buf);
705                                 }
706                                 /* Increment out of memory counters. */
707                                 ++rxq->stats.rx_nombuf;
708                                 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
709                                 goto repost;
710                         }
711 #ifndef NDEBUG
712                         /* Poison user-modifiable fields in rep. */
713                         NEXT(rep) = (void *)((uintptr_t)-1);
714                         SET_DATA_OFF(rep, 0xdead);
715                         DATA_LEN(rep) = 0xd00d;
716                         PKT_LEN(rep) = 0xdeadd00d;
717                         NB_SEGS(rep) = 0x2a;
718                         PORT(rep) = 0x2a;
719                         rep->ol_flags = -1;
720 #endif
721                         assert(rep->buf_len == seg->buf_len);
722                         assert(rep->buf_len == rxq->mb_len);
723                         /* Reconfigure sge to use rep instead of seg. */
724                         assert(sge->lkey == rxq->mr->lkey);
725                         sge->addr = ((uintptr_t)rep->buf_addr + seg_headroom);
726                         elt->bufs[j] = rep;
727                         ++j;
728                         /* Update pkt_buf if it's the first segment, or link
729                          * seg to the previous one and update pkt_buf_next. */
730                         *pkt_buf_next = seg;
731                         pkt_buf_next = &NEXT(seg);
732                         /* Update seg information. */
733                         seg_tailroom = (seg->buf_len - seg_headroom);
734                         assert(sge->length == seg_tailroom);
735                         SET_DATA_OFF(seg, seg_headroom);
736                         if (likely(len <= seg_tailroom)) {
737                                 /* Last segment. */
738                                 DATA_LEN(seg) = len;
739                                 PKT_LEN(seg) = len;
740                                 /* Sanity check. */
741                                 assert(rte_pktmbuf_headroom(seg) ==
742                                        seg_headroom);
743                                 assert(rte_pktmbuf_tailroom(seg) ==
744                                        (seg_tailroom - len));
745                                 break;
746                         }
747                         DATA_LEN(seg) = seg_tailroom;
748                         PKT_LEN(seg) = seg_tailroom;
749                         /* Sanity check. */
750                         assert(rte_pktmbuf_headroom(seg) == seg_headroom);
751                         assert(rte_pktmbuf_tailroom(seg) == 0);
752                         /* Fix len and clear headroom for next segments. */
753                         len -= seg_tailroom;
754                         seg_headroom = 0;
755                 }
756                 /* Update head and tail segments. */
757                 *pkt_buf_next = NULL;
758                 assert(pkt_buf != NULL);
759                 assert(j != 0);
760                 NB_SEGS(pkt_buf) = j;
761                 PORT(pkt_buf) = rxq->port_id;
762                 PKT_LEN(pkt_buf) = pkt_buf_len;
763                 pkt_buf->packet_type = rxq_cq_to_pkt_type(flags);
764                 pkt_buf->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
765
766                 /* Return packet. */
767                 *(pkts++) = pkt_buf;
768                 ++pkts_ret;
769 #ifdef MLX5_PMD_SOFT_COUNTERS
770                 /* Increment bytes counter. */
771                 rxq->stats.ibytes += pkt_buf_len;
772 #endif
773 repost:
774                 ret = rxq->if_wq->recv_sg_list(rxq->wq,
775                                                elt->sges,
776                                                RTE_DIM(elt->sges));
777                 if (unlikely(ret)) {
778                         /* Inability to repost WRs is fatal. */
779                         DEBUG("%p: recv_sg_list(): failed (ret=%d)",
780                               (void *)rxq->priv,
781                               ret);
782                         abort();
783                 }
784                 if (++elts_head >= elts_n)
785                         elts_head = 0;
786                 continue;
787         }
788         if (unlikely(i == 0))
789                 return 0;
790         rxq->elts_head = elts_head;
791 #ifdef MLX5_PMD_SOFT_COUNTERS
792         /* Increment packets counter. */
793         rxq->stats.ipackets += pkts_ret;
794 #endif
795         return pkts_ret;
796 }
797
798 /**
799  * DPDK callback for RX.
800  *
801  * The following function is the same as mlx5_rx_burst_sp(), except it doesn't
802  * manage scattered packets. Improves performance when MRU is lower than the
803  * size of the first segment.
804  *
805  * @param dpdk_rxq
806  *   Generic pointer to RX queue structure.
807  * @param[out] pkts
808  *   Array to store received packets.
809  * @param pkts_n
810  *   Maximum number of packets in array.
811  *
812  * @return
813  *   Number of packets successfully received (<= pkts_n).
814  */
815 uint16_t
816 mlx5_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
817 {
818         struct rxq *rxq = (struct rxq *)dpdk_rxq;
819         struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
820         const unsigned int elts_n = rxq->elts_n;
821         unsigned int elts_head = rxq->elts_head;
822         struct ibv_sge sges[pkts_n];
823         unsigned int i;
824         unsigned int pkts_ret = 0;
825         int ret;
826
827         if (unlikely(rxq->sp))
828                 return mlx5_rx_burst_sp(dpdk_rxq, pkts, pkts_n);
829         for (i = 0; (i != pkts_n); ++i) {
830                 struct rxq_elt *elt = &(*elts)[elts_head];
831                 unsigned int len;
832                 struct rte_mbuf *seg = elt->buf;
833                 struct rte_mbuf *rep;
834                 uint32_t flags;
835
836                 /* Sanity checks. */
837                 assert(seg != NULL);
838                 assert(elts_head < rxq->elts_n);
839                 assert(rxq->elts_head < rxq->elts_n);
840                 /*
841                  * Fetch initial bytes of packet descriptor into a
842                  * cacheline while allocating rep.
843                  */
844                 rte_prefetch0(seg);
845                 rte_prefetch0(&seg->cacheline1);
846                 ret = rxq->if_cq->poll_length_flags(rxq->cq, NULL, NULL,
847                                                     &flags);
848                 if (unlikely(ret < 0)) {
849                         struct ibv_wc wc;
850                         int wcs_n;
851
852                         DEBUG("rxq=%p, poll_length() failed (ret=%d)",
853                               (void *)rxq, ret);
854                         /* ibv_poll_cq() must be used in case of failure. */
855                         wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
856                         if (unlikely(wcs_n == 0))
857                                 break;
858                         if (unlikely(wcs_n < 0)) {
859                                 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
860                                       (void *)rxq, wcs_n);
861                                 break;
862                         }
863                         assert(wcs_n == 1);
864                         if (unlikely(wc.status != IBV_WC_SUCCESS)) {
865                                 /* Whatever, just repost the offending WR. */
866                                 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
867                                       " completion status (%d): %s",
868                                       (void *)rxq, wc.wr_id, wc.status,
869                                       ibv_wc_status_str(wc.status));
870 #ifdef MLX5_PMD_SOFT_COUNTERS
871                                 /* Increment dropped packets counter. */
872                                 ++rxq->stats.idropped;
873 #endif
874                                 /* Add SGE to array for repost. */
875                                 sges[i] = elt->sge;
876                                 goto repost;
877                         }
878                         ret = wc.byte_len;
879                 }
880                 if (ret == 0)
881                         break;
882                 len = ret;
883                 rep = __rte_mbuf_raw_alloc(rxq->mp);
884                 if (unlikely(rep == NULL)) {
885                         /*
886                          * Unable to allocate a replacement mbuf,
887                          * repost WR.
888                          */
889                         DEBUG("rxq=%p: can't allocate a new mbuf",
890                               (void *)rxq);
891                         /* Increment out of memory counters. */
892                         ++rxq->stats.rx_nombuf;
893                         ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
894                         goto repost;
895                 }
896
897                 /* Reconfigure sge to use rep instead of seg. */
898                 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
899                 assert(elt->sge.lkey == rxq->mr->lkey);
900                 elt->buf = rep;
901
902                 /* Add SGE to array for repost. */
903                 sges[i] = elt->sge;
904
905                 /* Update seg information. */
906                 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
907                 NB_SEGS(seg) = 1;
908                 PORT(seg) = rxq->port_id;
909                 NEXT(seg) = NULL;
910                 PKT_LEN(seg) = len;
911                 DATA_LEN(seg) = len;
912                 seg->packet_type = rxq_cq_to_pkt_type(flags);
913                 seg->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
914
915                 /* Return packet. */
916                 *(pkts++) = seg;
917                 ++pkts_ret;
918 #ifdef MLX5_PMD_SOFT_COUNTERS
919                 /* Increment bytes counter. */
920                 rxq->stats.ibytes += len;
921 #endif
922 repost:
923                 if (++elts_head >= elts_n)
924                         elts_head = 0;
925                 continue;
926         }
927         if (unlikely(i == 0))
928                 return 0;
929         /* Repost WRs. */
930 #ifdef DEBUG_RECV
931         DEBUG("%p: reposting %u WRs", (void *)rxq, i);
932 #endif
933         ret = rxq->if_wq->recv_burst(rxq->wq, sges, i);
934         if (unlikely(ret)) {
935                 /* Inability to repost WRs is fatal. */
936                 DEBUG("%p: recv_burst(): failed (ret=%d)",
937                       (void *)rxq->priv,
938                       ret);
939                 abort();
940         }
941         rxq->elts_head = elts_head;
942 #ifdef MLX5_PMD_SOFT_COUNTERS
943         /* Increment packets counter. */
944         rxq->stats.ipackets += pkts_ret;
945 #endif
946         return pkts_ret;
947 }
948
949 /**
950  * Dummy DPDK callback for TX.
951  *
952  * This function is used to temporarily replace the real callback during
953  * unsafe control operations on the queue, or in case of error.
954  *
955  * @param dpdk_txq
956  *   Generic pointer to TX queue structure.
957  * @param[in] pkts
958  *   Packets to transmit.
959  * @param pkts_n
960  *   Number of packets in array.
961  *
962  * @return
963  *   Number of packets successfully transmitted (<= pkts_n).
964  */
965 uint16_t
966 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
967 {
968         (void)dpdk_txq;
969         (void)pkts;
970         (void)pkts_n;
971         return 0;
972 }
973
974 /**
975  * Dummy DPDK callback for RX.
976  *
977  * This function is used to temporarily replace the real callback during
978  * unsafe control operations on the queue, or in case of error.
979  *
980  * @param dpdk_rxq
981  *   Generic pointer to RX queue structure.
982  * @param[out] pkts
983  *   Array to store received packets.
984  * @param pkts_n
985  *   Maximum number of packets in array.
986  *
987  * @return
988  *   Number of packets successfully received (<= pkts_n).
989  */
990 uint16_t
991 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
992 {
993         (void)dpdk_rxq;
994         (void)pkts;
995         (void)pkts_n;
996         return 0;
997 }