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