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