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