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