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