ethdev: query supported packet types
[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  * @note: fix mlx5_dev_supported_ptypes_get() if any change here.
674  *
675  * @return
676  *   Packet type for struct rte_mbuf.
677  */
678 static inline uint32_t
679 rxq_cq_to_pkt_type(uint32_t flags)
680 {
681         uint32_t pkt_type;
682
683         if (flags & IBV_EXP_CQ_RX_TUNNEL_PACKET)
684                 pkt_type =
685                         TRANSPOSE(flags,
686                                   IBV_EXP_CQ_RX_OUTER_IPV4_PACKET,
687                                   RTE_PTYPE_L3_IPV4) |
688                         TRANSPOSE(flags,
689                                   IBV_EXP_CQ_RX_OUTER_IPV6_PACKET,
690                                   RTE_PTYPE_L3_IPV6) |
691                         TRANSPOSE(flags,
692                                   IBV_EXP_CQ_RX_IPV4_PACKET,
693                                   RTE_PTYPE_INNER_L3_IPV4) |
694                         TRANSPOSE(flags,
695                                   IBV_EXP_CQ_RX_IPV6_PACKET,
696                                   RTE_PTYPE_INNER_L3_IPV6);
697         else
698                 pkt_type =
699                         TRANSPOSE(flags,
700                                   IBV_EXP_CQ_RX_IPV4_PACKET,
701                                   RTE_PTYPE_L3_IPV4) |
702                         TRANSPOSE(flags,
703                                   IBV_EXP_CQ_RX_IPV6_PACKET,
704                                   RTE_PTYPE_L3_IPV6);
705         return pkt_type;
706 }
707
708 /**
709  * Translate RX completion flags to offload flags.
710  *
711  * @param[in] rxq
712  *   Pointer to RX queue structure.
713  * @param flags
714  *   RX completion flags returned by poll_length_flags().
715  *
716  * @return
717  *   Offload flags (ol_flags) for struct rte_mbuf.
718  */
719 static inline uint32_t
720 rxq_cq_to_ol_flags(const struct rxq *rxq, uint32_t flags)
721 {
722         uint32_t ol_flags = 0;
723
724         if (rxq->csum) {
725                 /* Set IP checksum flag only for IPv4/IPv6 packets. */
726                 if (flags &
727                     (IBV_EXP_CQ_RX_IPV4_PACKET | IBV_EXP_CQ_RX_IPV6_PACKET))
728                         ol_flags |=
729                                 TRANSPOSE(~flags,
730                                         IBV_EXP_CQ_RX_IP_CSUM_OK,
731                                         PKT_RX_IP_CKSUM_BAD);
732 #ifdef HAVE_EXP_CQ_RX_TCP_PACKET
733                 /* Set L4 checksum flag only for TCP/UDP packets. */
734                 if (flags &
735                     (IBV_EXP_CQ_RX_TCP_PACKET | IBV_EXP_CQ_RX_UDP_PACKET))
736 #endif /* HAVE_EXP_CQ_RX_TCP_PACKET */
737                         ol_flags |=
738                                 TRANSPOSE(~flags,
739                                         IBV_EXP_CQ_RX_TCP_UDP_CSUM_OK,
740                                         PKT_RX_L4_CKSUM_BAD);
741         }
742         /*
743          * PKT_RX_IP_CKSUM_BAD and PKT_RX_L4_CKSUM_BAD are used in place
744          * of PKT_RX_EIP_CKSUM_BAD because the latter is not functional
745          * (its value is 0).
746          */
747         if ((flags & IBV_EXP_CQ_RX_TUNNEL_PACKET) && (rxq->csum_l2tun))
748                 ol_flags |=
749                         TRANSPOSE(~flags,
750                                   IBV_EXP_CQ_RX_OUTER_IP_CSUM_OK,
751                                   PKT_RX_IP_CKSUM_BAD) |
752                         TRANSPOSE(~flags,
753                                   IBV_EXP_CQ_RX_OUTER_TCP_UDP_CSUM_OK,
754                                   PKT_RX_L4_CKSUM_BAD);
755         return ol_flags;
756 }
757
758 /**
759  * DPDK callback for RX with scattered packets support.
760  *
761  * @param dpdk_rxq
762  *   Generic pointer to RX queue structure.
763  * @param[out] pkts
764  *   Array to store received packets.
765  * @param pkts_n
766  *   Maximum number of packets in array.
767  *
768  * @return
769  *   Number of packets successfully received (<= pkts_n).
770  */
771 uint16_t
772 mlx5_rx_burst_sp(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
773 {
774         struct rxq *rxq = (struct rxq *)dpdk_rxq;
775         struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
776         const unsigned int elts_n = rxq->elts_n;
777         unsigned int elts_head = rxq->elts_head;
778         unsigned int i;
779         unsigned int pkts_ret = 0;
780         int ret;
781
782         if (unlikely(!rxq->sp))
783                 return mlx5_rx_burst(dpdk_rxq, pkts, pkts_n);
784         if (unlikely(elts == NULL)) /* See RTE_DEV_CMD_SET_MTU. */
785                 return 0;
786         for (i = 0; (i != pkts_n); ++i) {
787                 struct rxq_elt_sp *elt = &(*elts)[elts_head];
788                 unsigned int len;
789                 unsigned int pkt_buf_len;
790                 struct rte_mbuf *pkt_buf = NULL; /* Buffer returned in pkts. */
791                 struct rte_mbuf **pkt_buf_next = &pkt_buf;
792                 unsigned int seg_headroom = RTE_PKTMBUF_HEADROOM;
793                 unsigned int j = 0;
794                 uint32_t flags;
795                 uint16_t vlan_tci;
796
797                 /* Sanity checks. */
798                 assert(elts_head < rxq->elts_n);
799                 assert(rxq->elts_head < rxq->elts_n);
800                 ret = rxq->poll(rxq->cq, NULL, NULL, &flags, &vlan_tci);
801                 if (unlikely(ret < 0)) {
802                         struct ibv_wc wc;
803                         int wcs_n;
804
805                         DEBUG("rxq=%p, poll_length() failed (ret=%d)",
806                               (void *)rxq, ret);
807                         /* ibv_poll_cq() must be used in case of failure. */
808                         wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
809                         if (unlikely(wcs_n == 0))
810                                 break;
811                         if (unlikely(wcs_n < 0)) {
812                                 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
813                                       (void *)rxq, wcs_n);
814                                 break;
815                         }
816                         assert(wcs_n == 1);
817                         if (unlikely(wc.status != IBV_WC_SUCCESS)) {
818                                 /* Whatever, just repost the offending WR. */
819                                 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
820                                       " completion status (%d): %s",
821                                       (void *)rxq, wc.wr_id, wc.status,
822                                       ibv_wc_status_str(wc.status));
823 #ifdef MLX5_PMD_SOFT_COUNTERS
824                                 /* Increment dropped packets counter. */
825                                 ++rxq->stats.idropped;
826 #endif
827                                 goto repost;
828                         }
829                         ret = wc.byte_len;
830                 }
831                 if (ret == 0)
832                         break;
833                 len = ret;
834                 pkt_buf_len = len;
835                 /*
836                  * Replace spent segments with new ones, concatenate and
837                  * return them as pkt_buf.
838                  */
839                 while (1) {
840                         struct ibv_sge *sge = &elt->sges[j];
841                         struct rte_mbuf *seg = elt->bufs[j];
842                         struct rte_mbuf *rep;
843                         unsigned int seg_tailroom;
844
845                         assert(seg != NULL);
846                         /*
847                          * Fetch initial bytes of packet descriptor into a
848                          * cacheline while allocating rep.
849                          */
850                         rte_prefetch0(seg);
851                         rep = __rte_mbuf_raw_alloc(rxq->mp);
852                         if (unlikely(rep == NULL)) {
853                                 /*
854                                  * Unable to allocate a replacement mbuf,
855                                  * repost WR.
856                                  */
857                                 DEBUG("rxq=%p: can't allocate a new mbuf",
858                                       (void *)rxq);
859                                 if (pkt_buf != NULL) {
860                                         *pkt_buf_next = NULL;
861                                         rte_pktmbuf_free(pkt_buf);
862                                 }
863                                 /* Increment out of memory counters. */
864                                 ++rxq->stats.rx_nombuf;
865                                 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
866                                 goto repost;
867                         }
868 #ifndef NDEBUG
869                         /* Poison user-modifiable fields in rep. */
870                         NEXT(rep) = (void *)((uintptr_t)-1);
871                         SET_DATA_OFF(rep, 0xdead);
872                         DATA_LEN(rep) = 0xd00d;
873                         PKT_LEN(rep) = 0xdeadd00d;
874                         NB_SEGS(rep) = 0x2a;
875                         PORT(rep) = 0x2a;
876                         rep->ol_flags = -1;
877 #endif
878                         assert(rep->buf_len == seg->buf_len);
879                         assert(rep->buf_len == rxq->mb_len);
880                         /* Reconfigure sge to use rep instead of seg. */
881                         assert(sge->lkey == rxq->mr->lkey);
882                         sge->addr = ((uintptr_t)rep->buf_addr + seg_headroom);
883                         elt->bufs[j] = rep;
884                         ++j;
885                         /* Update pkt_buf if it's the first segment, or link
886                          * seg to the previous one and update pkt_buf_next. */
887                         *pkt_buf_next = seg;
888                         pkt_buf_next = &NEXT(seg);
889                         /* Update seg information. */
890                         seg_tailroom = (seg->buf_len - seg_headroom);
891                         assert(sge->length == seg_tailroom);
892                         SET_DATA_OFF(seg, seg_headroom);
893                         if (likely(len <= seg_tailroom)) {
894                                 /* Last segment. */
895                                 DATA_LEN(seg) = len;
896                                 PKT_LEN(seg) = len;
897                                 /* Sanity check. */
898                                 assert(rte_pktmbuf_headroom(seg) ==
899                                        seg_headroom);
900                                 assert(rte_pktmbuf_tailroom(seg) ==
901                                        (seg_tailroom - len));
902                                 break;
903                         }
904                         DATA_LEN(seg) = seg_tailroom;
905                         PKT_LEN(seg) = seg_tailroom;
906                         /* Sanity check. */
907                         assert(rte_pktmbuf_headroom(seg) == seg_headroom);
908                         assert(rte_pktmbuf_tailroom(seg) == 0);
909                         /* Fix len and clear headroom for next segments. */
910                         len -= seg_tailroom;
911                         seg_headroom = 0;
912                 }
913                 /* Update head and tail segments. */
914                 *pkt_buf_next = NULL;
915                 assert(pkt_buf != NULL);
916                 assert(j != 0);
917                 NB_SEGS(pkt_buf) = j;
918                 PORT(pkt_buf) = rxq->port_id;
919                 PKT_LEN(pkt_buf) = pkt_buf_len;
920                 if (rxq->csum | rxq->csum_l2tun | rxq->vlan_strip) {
921                         pkt_buf->packet_type = rxq_cq_to_pkt_type(flags);
922                         pkt_buf->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
923 #ifdef HAVE_EXP_DEVICE_ATTR_VLAN_OFFLOADS
924                         if (flags & IBV_EXP_CQ_RX_CVLAN_STRIPPED_V1) {
925                                 pkt_buf->ol_flags |= PKT_RX_VLAN_PKT;
926                                 pkt_buf->vlan_tci = vlan_tci;
927                         }
928 #endif /* HAVE_EXP_DEVICE_ATTR_VLAN_OFFLOADS */
929                 }
930
931                 /* Return packet. */
932                 *(pkts++) = pkt_buf;
933                 ++pkts_ret;
934 #ifdef MLX5_PMD_SOFT_COUNTERS
935                 /* Increment bytes counter. */
936                 rxq->stats.ibytes += pkt_buf_len;
937 #endif
938 repost:
939                 ret = rxq->recv(rxq->wq, elt->sges, RTE_DIM(elt->sges));
940                 if (unlikely(ret)) {
941                         /* Inability to repost WRs is fatal. */
942                         DEBUG("%p: recv_sg_list(): failed (ret=%d)",
943                               (void *)rxq->priv,
944                               ret);
945                         abort();
946                 }
947                 if (++elts_head >= elts_n)
948                         elts_head = 0;
949                 continue;
950         }
951         if (unlikely(i == 0))
952                 return 0;
953         rxq->elts_head = elts_head;
954 #ifdef MLX5_PMD_SOFT_COUNTERS
955         /* Increment packets counter. */
956         rxq->stats.ipackets += pkts_ret;
957 #endif
958         return pkts_ret;
959 }
960
961 /**
962  * DPDK callback for RX.
963  *
964  * The following function is the same as mlx5_rx_burst_sp(), except it doesn't
965  * manage scattered packets. Improves performance when MRU is lower than the
966  * size of the first segment.
967  *
968  * @param dpdk_rxq
969  *   Generic pointer to RX queue structure.
970  * @param[out] pkts
971  *   Array to store received packets.
972  * @param pkts_n
973  *   Maximum number of packets in array.
974  *
975  * @return
976  *   Number of packets successfully received (<= pkts_n).
977  */
978 uint16_t
979 mlx5_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
980 {
981         struct rxq *rxq = (struct rxq *)dpdk_rxq;
982         struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
983         const unsigned int elts_n = rxq->elts_n;
984         unsigned int elts_head = rxq->elts_head;
985         struct ibv_sge sges[pkts_n];
986         unsigned int i;
987         unsigned int pkts_ret = 0;
988         int ret;
989
990         if (unlikely(rxq->sp))
991                 return mlx5_rx_burst_sp(dpdk_rxq, pkts, pkts_n);
992         for (i = 0; (i != pkts_n); ++i) {
993                 struct rxq_elt *elt = &(*elts)[elts_head];
994                 unsigned int len;
995                 struct rte_mbuf *seg = elt->buf;
996                 struct rte_mbuf *rep;
997                 uint32_t flags;
998                 uint16_t vlan_tci;
999
1000                 /* Sanity checks. */
1001                 assert(seg != NULL);
1002                 assert(elts_head < rxq->elts_n);
1003                 assert(rxq->elts_head < rxq->elts_n);
1004                 /*
1005                  * Fetch initial bytes of packet descriptor into a
1006                  * cacheline while allocating rep.
1007                  */
1008                 rte_prefetch0(seg);
1009                 rte_prefetch0(&seg->cacheline1);
1010                 ret = rxq->poll(rxq->cq, NULL, NULL, &flags, &vlan_tci);
1011                 if (unlikely(ret < 0)) {
1012                         struct ibv_wc wc;
1013                         int wcs_n;
1014
1015                         DEBUG("rxq=%p, poll_length() failed (ret=%d)",
1016                               (void *)rxq, ret);
1017                         /* ibv_poll_cq() must be used in case of failure. */
1018                         wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
1019                         if (unlikely(wcs_n == 0))
1020                                 break;
1021                         if (unlikely(wcs_n < 0)) {
1022                                 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
1023                                       (void *)rxq, wcs_n);
1024                                 break;
1025                         }
1026                         assert(wcs_n == 1);
1027                         if (unlikely(wc.status != IBV_WC_SUCCESS)) {
1028                                 /* Whatever, just repost the offending WR. */
1029                                 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
1030                                       " completion status (%d): %s",
1031                                       (void *)rxq, wc.wr_id, wc.status,
1032                                       ibv_wc_status_str(wc.status));
1033 #ifdef MLX5_PMD_SOFT_COUNTERS
1034                                 /* Increment dropped packets counter. */
1035                                 ++rxq->stats.idropped;
1036 #endif
1037                                 /* Add SGE to array for repost. */
1038                                 sges[i] = elt->sge;
1039                                 goto repost;
1040                         }
1041                         ret = wc.byte_len;
1042                 }
1043                 if (ret == 0)
1044                         break;
1045                 len = ret;
1046                 rep = __rte_mbuf_raw_alloc(rxq->mp);
1047                 if (unlikely(rep == NULL)) {
1048                         /*
1049                          * Unable to allocate a replacement mbuf,
1050                          * repost WR.
1051                          */
1052                         DEBUG("rxq=%p: can't allocate a new mbuf",
1053                               (void *)rxq);
1054                         /* Increment out of memory counters. */
1055                         ++rxq->stats.rx_nombuf;
1056                         ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
1057                         goto repost;
1058                 }
1059
1060                 /* Reconfigure sge to use rep instead of seg. */
1061                 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
1062                 assert(elt->sge.lkey == rxq->mr->lkey);
1063                 elt->buf = rep;
1064
1065                 /* Add SGE to array for repost. */
1066                 sges[i] = elt->sge;
1067
1068                 /* Update seg information. */
1069                 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
1070                 NB_SEGS(seg) = 1;
1071                 PORT(seg) = rxq->port_id;
1072                 NEXT(seg) = NULL;
1073                 PKT_LEN(seg) = len;
1074                 DATA_LEN(seg) = len;
1075                 if (rxq->csum | rxq->csum_l2tun | rxq->vlan_strip) {
1076                         seg->packet_type = rxq_cq_to_pkt_type(flags);
1077                         seg->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
1078 #ifdef HAVE_EXP_DEVICE_ATTR_VLAN_OFFLOADS
1079                         if (flags & IBV_EXP_CQ_RX_CVLAN_STRIPPED_V1) {
1080                                 seg->ol_flags |= PKT_RX_VLAN_PKT;
1081                                 seg->vlan_tci = vlan_tci;
1082                         }
1083 #endif /* HAVE_EXP_DEVICE_ATTR_VLAN_OFFLOADS */
1084                 }
1085                 /* Return packet. */
1086                 *(pkts++) = seg;
1087                 ++pkts_ret;
1088 #ifdef MLX5_PMD_SOFT_COUNTERS
1089                 /* Increment bytes counter. */
1090                 rxq->stats.ibytes += len;
1091 #endif
1092 repost:
1093                 if (++elts_head >= elts_n)
1094                         elts_head = 0;
1095                 continue;
1096         }
1097         if (unlikely(i == 0))
1098                 return 0;
1099         /* Repost WRs. */
1100 #ifdef DEBUG_RECV
1101         DEBUG("%p: reposting %u WRs", (void *)rxq, i);
1102 #endif
1103         ret = rxq->recv(rxq->wq, sges, i);
1104         if (unlikely(ret)) {
1105                 /* Inability to repost WRs is fatal. */
1106                 DEBUG("%p: recv_burst(): failed (ret=%d)",
1107                       (void *)rxq->priv,
1108                       ret);
1109                 abort();
1110         }
1111         rxq->elts_head = elts_head;
1112 #ifdef MLX5_PMD_SOFT_COUNTERS
1113         /* Increment packets counter. */
1114         rxq->stats.ipackets += pkts_ret;
1115 #endif
1116         return pkts_ret;
1117 }
1118
1119 /**
1120  * Dummy DPDK callback for TX.
1121  *
1122  * This function is used to temporarily replace the real callback during
1123  * unsafe control operations on the queue, or in case of error.
1124  *
1125  * @param dpdk_txq
1126  *   Generic pointer to TX queue structure.
1127  * @param[in] pkts
1128  *   Packets to transmit.
1129  * @param pkts_n
1130  *   Number of packets in array.
1131  *
1132  * @return
1133  *   Number of packets successfully transmitted (<= pkts_n).
1134  */
1135 uint16_t
1136 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1137 {
1138         (void)dpdk_txq;
1139         (void)pkts;
1140         (void)pkts_n;
1141         return 0;
1142 }
1143
1144 /**
1145  * Dummy DPDK callback for RX.
1146  *
1147  * This function is used to temporarily replace the real callback during
1148  * unsafe control operations on the queue, or in case of error.
1149  *
1150  * @param dpdk_rxq
1151  *   Generic pointer to RX queue structure.
1152  * @param[out] pkts
1153  *   Array to store received packets.
1154  * @param pkts_n
1155  *   Maximum number of packets in array.
1156  *
1157  * @return
1158  *   Number of packets successfully received (<= pkts_n).
1159  */
1160 uint16_t
1161 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
1162 {
1163         (void)dpdk_rxq;
1164         (void)pkts;
1165         (void)pkts_n;
1166         return 0;
1167 }