1d267ad6cc05afd86eb58b5ec260fb11399a42f4
[vpp.git] / src / plugins / rdma / input.c
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2018 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17
18 #include <vlib/vlib.h>
19 #include <vlib/unix/unix.h>
20 #include <vlib/pci/pci.h>
21 #include <vnet/ethernet/ethernet.h>
22 #include <vnet/devices/devices.h>
23
24 #include <rdma/rdma.h>
25
26 #define foreach_rdma_input_error \
27   _(BUFFER_ALLOC, "buffer alloc error")
28
29 typedef enum
30 {
31 #define _(f,s) RDMA_INPUT_ERROR_##f,
32   foreach_rdma_input_error
33 #undef _
34     RDMA_INPUT_N_ERROR,
35 } rdma_input_error_t;
36
37 static __clib_unused char *rdma_input_error_strings[] = {
38 #define _(n,s) s,
39   foreach_rdma_input_error
40 #undef _
41 };
42
43
44 static_always_inline void
45 ibv_set_recv_wr_and_sge (struct ibv_recv_wr *w, struct ibv_sge *s, u64 va,
46                          u32 data_size, u32 lkey)
47 {
48   s[0].addr = va;
49   s[0].length = data_size;
50   s[0].lkey = lkey;
51   w[0].next = w + 1;
52   w[0].sg_list = s;
53   w[0].num_sge = 1;
54 }
55
56 static_always_inline void
57 rdma_device_input_refill (vlib_main_t * vm, rdma_device_t * rd,
58                           rdma_rxq_t * rxq, int is_mlx5dv, int is_striding)
59 {
60   u32 n_alloc, n;
61   u16 ring_space;
62   struct ibv_recv_wr wr[VLIB_FRAME_SIZE], *w = wr;
63   struct ibv_sge sge[VLIB_FRAME_SIZE], *s = sge;
64   u32 mask = rxq->size - 1;
65   u32 slot = rxq->tail & mask;
66   u32 *bufs = rxq->bufs + slot;
67   u32 data_size = rxq->buf_sz;
68   u32 lkey = rd->lkey;
69   int log_stride_per_wqe = rxq->log_stride_per_wqe;
70   int log_wqe_sz = rxq->log_wqe_sz;
71
72   /* refilled buffers must be a multiple of 8 and of strides per WQE */
73   u32 alloc_multiple = 1 << (clib_max (3, log_stride_per_wqe));
74
75   ring_space = rxq->size - (rxq->tail - rxq->head);
76
77   n_alloc = clib_min (VLIB_FRAME_SIZE, ring_space);
78
79   /* do not bother to allocate if too small */
80   if (n_alloc < 2 * alloc_multiple)
81     return;
82
83   /* avoid wrap-around logic in core loop */
84   n_alloc = clib_min (n_alloc, rxq->size - slot);
85
86   n_alloc &= ~(alloc_multiple - 1);     /* round to alloc_multiple */
87
88   n = vlib_buffer_alloc_to_ring_from_pool (vm, rxq->bufs, slot, rxq->size,
89                                            n_alloc, rd->pool);
90
91   if (PREDICT_FALSE (n != n_alloc))
92     {
93       u32 n_free;
94       if (n < alloc_multiple)
95         {
96           if (n)
97             vlib_buffer_free_from_ring (vm, rxq->bufs, slot, rxq->size, n);
98           return;
99         }
100
101       /* partial allocation, round and return rest */
102       n_free = n & (alloc_multiple - 1);
103       n -= n_free;
104       if (n_free)
105         vlib_buffer_free_from_ring (vm, rxq->bufs, (slot + n) & mask,
106                                     rxq->size, n_free);
107     }
108
109   n_alloc = n;
110
111   if (is_mlx5dv)
112     {
113       u64 __clib_aligned (32) va[8];
114
115       /* slot does not necessarily correspond to the slot
116          in the wqes ring (in 16B words) */
117       u32 wqes_slot = slot << (log_wqe_sz - log_stride_per_wqe);
118       u32 wqe_cnt = rxq->wqe_cnt;
119       mlx5dv_wqe_ds_t *wqe = rxq->wqes + wqes_slot;
120       int wqe_sz = 1 << log_wqe_sz;
121       int stride_per_wqe = 1 << log_stride_per_wqe;
122       int current_data_seg = 0;
123
124       while (n >= 1)
125         {
126           vlib_get_buffers_with_offset (vm, rxq->bufs + slot, (void **) va, 8,
127                                         sizeof (vlib_buffer_t));
128 #ifdef CLIB_HAVE_VEC256
129           *(u64x4 *) va = u64x4_byte_swap (*(u64x4 *) va);
130           *(u64x4 *) (va + 4) = u64x4_byte_swap (*(u64x4 *) (va + 4));
131 #else
132           for (int i = 0; i < 8; i++)
133             va[i] = clib_host_to_net_u64 (va[i]);
134 #endif
135
136           /*In striding RQ mode, the first 16B-word of the WQE is the SRQ header.
137              It is initialised as if it were a LINKED_LIST, as we have no guarantee
138              about what RDMA core does (CYCLIC_RQ or LINKED_LIST_RQ). In cyclic
139              mode, the SRQ header is ignored anyways... */
140
141 /* *INDENT-OFF* */
142           if (is_striding && !(current_data_seg & (wqe_sz - 1)))
143             *(mlx5dv_wqe_srq_next_t *) wqe = (mlx5dv_wqe_srq_next_t)
144             {
145               .rsvd0 = {0},
146               .next_wqe_index = clib_host_to_net_u16 (((wqes_slot >> log_wqe_sz) + 1) & (wqe_cnt - 1)),
147               .signature = 0,
148               .rsvd1 = {0}
149             };
150 /* *INDENT-ON* */
151
152           if (!is_striding || !(current_data_seg & ~(stride_per_wqe - 1)))
153             {
154               wqe[0 + is_striding].addr = va[0];
155               wqe[1 + is_striding].addr = va[1];
156               wqe[2 + is_striding].addr = va[2];
157               wqe[3 + is_striding].addr = va[3];
158               wqe[4 + is_striding].addr = va[4];
159               wqe[5 + is_striding].addr = va[5];
160               wqe[6 + is_striding].addr = va[6];
161               wqe[7 + is_striding].addr = va[7];
162               slot += 8;
163               n -= 8;
164             }
165           wqe += 8;
166           wqes_slot += 8;
167           current_data_seg += 8;
168           current_data_seg &= wqe_sz - 1;
169         }
170
171       CLIB_MEMORY_STORE_BARRIER ();
172       rxq->tail += n_alloc;
173       if (is_striding)
174         {
175           rxq->striding_wqe_tail += n_alloc >> log_stride_per_wqe;
176           rxq->wq_db[MLX5_RCV_DBR] =
177             clib_host_to_net_u32 (rxq->striding_wqe_tail);
178         }
179       else
180         rxq->wq_db[MLX5_RCV_DBR] = clib_host_to_net_u32 (rxq->tail);
181       return;
182     }
183
184   while (n >= 8)
185     {
186       u64 va[8];
187       if (PREDICT_TRUE (n >= 16))
188         {
189           clib_prefetch_store (s + 16);
190           clib_prefetch_store (w + 16);
191         }
192
193       vlib_get_buffers_with_offset (vm, bufs, (void **) va, 8,
194                                     sizeof (vlib_buffer_t));
195
196       ibv_set_recv_wr_and_sge (w++, s++, va[0], data_size, lkey);
197       ibv_set_recv_wr_and_sge (w++, s++, va[1], data_size, lkey);
198       ibv_set_recv_wr_and_sge (w++, s++, va[2], data_size, lkey);
199       ibv_set_recv_wr_and_sge (w++, s++, va[3], data_size, lkey);
200       ibv_set_recv_wr_and_sge (w++, s++, va[4], data_size, lkey);
201       ibv_set_recv_wr_and_sge (w++, s++, va[5], data_size, lkey);
202       ibv_set_recv_wr_and_sge (w++, s++, va[6], data_size, lkey);
203       ibv_set_recv_wr_and_sge (w++, s++, va[7], data_size, lkey);
204
205       bufs += 8;
206       n -= 8;
207     }
208
209   w[-1].next = 0;               /* fix next pointer in WR linked-list last item */
210
211   n = n_alloc;
212   if (ibv_post_wq_recv (rxq->wq, wr, &w) != 0)
213     {
214       n = w - wr;
215       vlib_buffer_free_from_ring (vm, rxq->bufs, slot + n, rxq->size,
216                                   n_alloc - n);
217     }
218
219   rxq->tail += n;
220 }
221
222 static_always_inline void
223 rdma_device_input_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
224                          const rdma_device_t * rd, u32 n_left,
225                          const u32 * bi, u32 next_index, u16 * cqe_flags,
226                          int is_mlx5dv)
227 {
228   u32 n_trace, i;
229
230   if (PREDICT_TRUE (0 == (n_trace = vlib_get_trace_count (vm, node))))
231     return;
232
233   i = 0;
234   while (n_trace && n_left)
235     {
236       vlib_buffer_t *b;
237       rdma_input_trace_t *tr;
238       b = vlib_get_buffer (vm, bi[0]);
239       vlib_trace_buffer (vm, node, next_index, b,
240                          /* follow_chain */ 0);
241       tr = vlib_add_trace (vm, node, b, sizeof (*tr));
242       tr->next_index = next_index;
243       tr->hw_if_index = rd->hw_if_index;
244       tr->cqe_flags = is_mlx5dv ? clib_net_to_host_u16 (cqe_flags[0]) : 0;
245
246       /* next */
247       n_trace--;
248       n_left--;
249       cqe_flags++;
250       bi++;
251       i++;
252     }
253   vlib_set_trace_count (vm, node, n_trace);
254 }
255
256 static_always_inline void
257 rdma_device_input_ethernet (vlib_main_t * vm, vlib_node_runtime_t * node,
258                             const rdma_device_t * rd, u32 next_index,
259                             int skip_ip4_cksum)
260 {
261   vlib_next_frame_t *nf;
262   vlib_frame_t *f;
263   ethernet_input_frame_t *ef;
264
265   if (PREDICT_FALSE (VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT != next_index))
266     return;
267
268   nf =
269     vlib_node_runtime_get_next_frame (vm, node,
270                                       VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT);
271   f = vlib_get_frame (vm, nf->frame);
272   f->flags = ETH_INPUT_FRAME_F_SINGLE_SW_IF_IDX;
273   if (skip_ip4_cksum)
274     f->flags |= ETH_INPUT_FRAME_F_IP4_CKSUM_OK;
275
276   ef = vlib_frame_scalar_args (f);
277   ef->sw_if_index = rd->sw_if_index;
278   ef->hw_if_index = rd->hw_if_index;
279 }
280
281 static_always_inline u32
282 rdma_device_input_bufs (vlib_main_t * vm, const rdma_device_t * rd,
283                         vlib_buffer_t ** b, struct ibv_wc *wc,
284                         u32 n_left_from, vlib_buffer_t * bt)
285 {
286   u32 n_rx_bytes = 0;
287
288   while (n_left_from >= 4)
289     {
290       if (PREDICT_TRUE (n_left_from >= 8))
291         {
292           CLIB_PREFETCH (&wc[4 + 0], CLIB_CACHE_LINE_BYTES, LOAD);
293           CLIB_PREFETCH (&wc[4 + 1], CLIB_CACHE_LINE_BYTES, LOAD);
294           CLIB_PREFETCH (&wc[4 + 2], CLIB_CACHE_LINE_BYTES, LOAD);
295           CLIB_PREFETCH (&wc[4 + 3], CLIB_CACHE_LINE_BYTES, LOAD);
296           vlib_prefetch_buffer_header (b[4 + 0], STORE);
297           vlib_prefetch_buffer_header (b[4 + 1], STORE);
298           vlib_prefetch_buffer_header (b[4 + 2], STORE);
299           vlib_prefetch_buffer_header (b[4 + 3], STORE);
300         }
301
302       vlib_buffer_copy_template (b[0], bt);
303       vlib_buffer_copy_template (b[1], bt);
304       vlib_buffer_copy_template (b[2], bt);
305       vlib_buffer_copy_template (b[3], bt);
306
307       n_rx_bytes += b[0]->current_length = wc[0].byte_len;
308       n_rx_bytes += b[1]->current_length = wc[1].byte_len;
309       n_rx_bytes += b[2]->current_length = wc[2].byte_len;
310       n_rx_bytes += b[3]->current_length = wc[3].byte_len;
311
312       b += 4;
313       wc += 4;
314       n_left_from -= 4;
315     }
316
317   while (n_left_from >= 1)
318     {
319       vlib_buffer_copy_template (b[0], bt);
320       n_rx_bytes += b[0]->current_length = wc[0].byte_len;
321
322       b += 1;
323       wc += 1;
324       n_left_from -= 1;
325     }
326
327   return n_rx_bytes;
328 }
329
330 static_always_inline void
331 process_mini_cqes (rdma_rxq_t * rxq, u32 skip, u32 n_left, u32 cq_ci,
332                    u32 mask, u32 * byte_cnt)
333 {
334   mlx5dv_mini_cqe_t *mcqe;
335   u32 mcqe_array_index = (cq_ci + 1) & mask;
336   mcqe = (mlx5dv_mini_cqe_t *) (rxq->cqes + mcqe_array_index);
337
338   mcqe_array_index = cq_ci;
339
340   if (skip)
341     {
342       u32 n = skip & ~7;
343
344       if (n)
345         {
346           mcqe_array_index = (mcqe_array_index + n) & mask;
347           mcqe = (mlx5dv_mini_cqe_t *) (rxq->cqes + mcqe_array_index);
348           skip -= n;
349         }
350
351       if (skip)
352         {
353           n = clib_min (8 - skip, n_left);
354           for (int i = 0; i < n; i++)
355             byte_cnt[i] = mcqe[skip + i].byte_count;
356           mcqe_array_index = (mcqe_array_index + 8) & mask;
357           mcqe = (mlx5dv_mini_cqe_t *) (rxq->cqes + mcqe_array_index);
358           n_left -= n;
359           byte_cnt += n;
360         }
361
362     }
363
364   while (n_left >= 8)
365     {
366       for (int i = 0; i < 8; i++)
367         byte_cnt[i] = mcqe[i].byte_count;
368
369       n_left -= 8;
370       byte_cnt += 8;
371       mcqe_array_index = (mcqe_array_index + 8) & mask;
372       mcqe = (mlx5dv_mini_cqe_t *) (rxq->cqes + mcqe_array_index);
373     }
374
375   if (n_left)
376     {
377       for (int i = 0; i < n_left; i++)
378         byte_cnt[i] = mcqe[i].byte_count;
379     }
380 }
381
382 static_always_inline void
383 cqe_set_owner (mlx5dv_cqe_t * cqe, u32 n_left, u8 owner)
384 {
385   while (n_left >= 8)
386     {
387       cqe[0].opcode_cqefmt_se_owner = owner;
388       cqe[1].opcode_cqefmt_se_owner = owner;
389       cqe[2].opcode_cqefmt_se_owner = owner;
390       cqe[3].opcode_cqefmt_se_owner = owner;
391       cqe[4].opcode_cqefmt_se_owner = owner;
392       cqe[5].opcode_cqefmt_se_owner = owner;
393       cqe[6].opcode_cqefmt_se_owner = owner;
394       cqe[7].opcode_cqefmt_se_owner = owner;
395       n_left -= 8;
396       cqe += 8;
397     }
398   while (n_left)
399     {
400       cqe[0].opcode_cqefmt_se_owner = owner;
401       n_left--;
402       cqe++;
403     }
404 }
405
406 static_always_inline void
407 compressed_cqe_reset_owner (rdma_rxq_t * rxq, u32 n_mini_cqes, u32 cq_ci,
408                             u32 mask, u32 log2_cq_size)
409 {
410   u8 owner;
411   u32 offset, cq_size = 1 << log2_cq_size;
412
413
414   /* first CQE is reset by hardware */
415   cq_ci++;
416   n_mini_cqes--;
417
418   offset = cq_ci & mask;
419   owner = 0xf0 | ((cq_ci >> log2_cq_size) & 1);
420
421   if (offset + n_mini_cqes < cq_size)
422     {
423       cqe_set_owner (rxq->cqes + offset, n_mini_cqes, owner);
424     }
425   else
426     {
427       u32 n = cq_size - offset;
428       cqe_set_owner (rxq->cqes + offset, n, owner);
429       cqe_set_owner (rxq->cqes, n_mini_cqes - n, owner ^ 1);
430     }
431
432 }
433
434 static_always_inline uword
435 rdma_device_poll_cq_mlx5dv (rdma_device_t * rd, rdma_rxq_t * rxq,
436                             u32 * byte_cnt, u16 * cqe_flags)
437 {
438   u32 n_rx_packets = 0;
439   u32 log2_cq_size = rxq->log2_cq_size;
440   u32 mask = pow2_mask (log2_cq_size);
441   u32 cq_ci = rxq->cq_ci;
442
443   if (rxq->n_mini_cqes_left)
444     {
445       /* partially processed mini-cqe array */
446       u32 n_mini_cqes = rxq->n_mini_cqes;
447       u32 n_mini_cqes_left = rxq->n_mini_cqes_left;
448       process_mini_cqes (rxq, n_mini_cqes - n_mini_cqes_left,
449                          n_mini_cqes_left, cq_ci, mask, byte_cnt);
450       compressed_cqe_reset_owner (rxq, n_mini_cqes, cq_ci, mask,
451                                   log2_cq_size);
452       clib_memset_u16 (cqe_flags, rxq->last_cqe_flags, n_mini_cqes_left);
453       n_rx_packets = n_mini_cqes_left;
454       byte_cnt += n_mini_cqes_left;
455       cqe_flags += n_mini_cqes_left;
456       rxq->n_mini_cqes_left = 0;
457       rxq->cq_ci = cq_ci = cq_ci + n_mini_cqes;
458     }
459
460   while (n_rx_packets < VLIB_FRAME_SIZE)
461     {
462       u8 cqe_last_byte, owner;
463       mlx5dv_cqe_t *cqe = rxq->cqes + (cq_ci & mask);
464
465       clib_prefetch_load (rxq->cqes + ((cq_ci + 8) & mask));
466
467       owner = (cq_ci >> log2_cq_size) & 1;
468       cqe_last_byte = cqe->opcode_cqefmt_se_owner;
469
470       if ((cqe_last_byte & 0x1) != owner)
471         break;
472
473       cqe_last_byte &= 0xfc;    /* remove owner and solicited bits */
474
475       if (cqe_last_byte == 0x2c)        /* OPCODE = 0x2 (Responder Send), Format = 0x3 (Compressed CQE) */
476         {
477           u32 n_mini_cqes = clib_net_to_host_u32 (cqe->mini_cqe_num);
478           u32 n_left = VLIB_FRAME_SIZE - n_rx_packets;
479           u16 flags = cqe->flags;
480
481           if (n_left >= n_mini_cqes)
482             {
483               process_mini_cqes (rxq, 0, n_mini_cqes, cq_ci, mask, byte_cnt);
484               clib_memset_u16 (cqe_flags, flags, n_mini_cqes);
485               compressed_cqe_reset_owner (rxq, n_mini_cqes, cq_ci, mask,
486                                           log2_cq_size);
487               n_rx_packets += n_mini_cqes;
488               byte_cnt += n_mini_cqes;
489               cqe_flags += n_mini_cqes;
490               cq_ci += n_mini_cqes;
491             }
492           else
493             {
494               process_mini_cqes (rxq, 0, n_left, cq_ci, mask, byte_cnt);
495               clib_memset_u16 (cqe_flags, flags, n_left);
496               n_rx_packets = VLIB_FRAME_SIZE;
497               rxq->n_mini_cqes = n_mini_cqes;
498               rxq->n_mini_cqes_left = n_mini_cqes - n_left;
499               rxq->last_cqe_flags = flags;
500               goto done;
501             }
502           continue;
503         }
504
505       if (cqe_last_byte == 0x20)        /* OPCODE = 0x2 (Responder Send), Format = 0x0 (no inline data) */
506         {
507           byte_cnt[0] = cqe->byte_cnt;
508           cqe_flags[0] = cqe->flags;
509           n_rx_packets++;
510           cq_ci++;
511           byte_cnt++;
512           continue;
513         }
514
515       rd->flags |= RDMA_DEVICE_F_ERROR;
516       break;
517     }
518
519 done:
520   if (n_rx_packets)
521     rxq->cq_db[0] = rxq->cq_ci = cq_ci;
522   return n_rx_packets;
523 }
524
525 static_always_inline int
526 rdma_device_mlx5dv_striding_rq_parse_bc (int n_rx_packets, int *n_rx_segs,
527                                          u32 * bc)
528 {
529 /* Determine if slow path is needed  */
530   int filler = 0;
531   for (int i = 0; i < n_rx_packets; i++)
532     {
533       *n_rx_segs +=
534         (bc[i] & CQE_BC_CONSUMED_STRIDES_MASK) >>
535         CQE_BC_CONSUMED_STRIDES_SHIFT;
536       filler |= ! !(bc[i] & CQE_BC_FILLER_MASK);
537     }
538   return n_rx_packets != *n_rx_segs || filler;
539 }
540
541 static_always_inline int
542 rdma_device_mlx5dv_l3_validate_and_swap_bc (rdma_per_thread_data_t
543                                             * ptd, int n_rx_packets, u32 * bc)
544 {
545   u16 mask = CQE_FLAG_L3_HDR_TYPE_MASK | CQE_FLAG_L3_OK;
546   u16 match = CQE_FLAG_L3_HDR_TYPE_IP4 << CQE_FLAG_L3_HDR_TYPE_SHIFT;
547
548   /* verify that all ip4 packets have l3_ok flag set and convert packet
549      length from network to host byte order */
550   int skip_ip4_cksum = 1;
551
552 #if defined CLIB_HAVE_VEC256
553   u16x16 mask16 = u16x16_splat (mask);
554   u16x16 match16 = u16x16_splat (match);
555   u16x16 r = { };
556
557   for (int i = 0; i * 16 < n_rx_packets; i++)
558     r |= (ptd->cqe_flags16[i] & mask16) != match16;
559
560   if (!u16x16_is_all_zero (r))
561     skip_ip4_cksum = 0;
562
563   for (int i = 0; i < n_rx_packets; i += 8)
564     *(u32x8 *) (bc + i) = u32x8_byte_swap (*(u32x8 *) (bc + i));
565 #elif defined CLIB_HAVE_VEC128
566   u16x8 mask8 = u16x8_splat (mask);
567   u16x8 match8 = u16x8_splat (match);
568   u16x8 r = { };
569
570   for (int i = 0; i * 8 < n_rx_packets; i++)
571     r |= (ptd->cqe_flags8[i] & mask8) != match8;
572
573   if (!u16x8_is_all_zero (r))
574     skip_ip4_cksum = 0;
575
576   for (int i = 0; i < n_rx_packets; i += 4)
577     *(u32x4 *) (bc + i) = u32x4_byte_swap (*(u32x4 *) (bc + i));
578 #else
579   for (int i = 0; i < n_rx_packets; i++)
580     if ((ptd->cqe_flags[i] & mask) == match)
581       skip_ip4_cksum = 0;
582
583   for (int i = 0; i < n_rx_packets; i++)
584     bc[i] = clib_net_to_host_u32 (bc[i]);
585 #endif
586   return skip_ip4_cksum;
587 }
588
589 static_always_inline u32
590 rdma_device_mlx5dv_fast_input (vlib_main_t * vm, rdma_rxq_t * rxq,
591                                u32 qs_mask, vlib_buffer_t * bt,
592                                u32 * to_next, u32 n_rx_segs, u32 * bc,
593                                u32 bc_mask)
594 {
595   vlib_buffer_t *bufs[VLIB_FRAME_SIZE];
596   vlib_buffer_t **b = bufs;
597   u32 n_left = n_rx_segs;
598   u32 n_rx_bytes = 0;
599   vlib_buffer_copy_indices_from_ring (to_next, rxq->bufs,
600                                       rxq->head & qs_mask, rxq->size,
601                                       n_rx_segs);
602   rxq->head += n_rx_segs;
603   vlib_get_buffers (vm, to_next, bufs, n_rx_segs);
604   while (n_left >= 8)
605     {
606       clib_prefetch_store (b[4]);
607       vlib_buffer_copy_template (b[0], bt);
608       n_rx_bytes += b[0]->current_length = bc[0] & bc_mask;
609       clib_prefetch_store (b[5]);
610       vlib_buffer_copy_template (b[1], bt);
611       n_rx_bytes += b[1]->current_length = bc[1] & bc_mask;
612       clib_prefetch_store (b[6]);
613       vlib_buffer_copy_template (b[2], bt);
614       n_rx_bytes += b[2]->current_length = bc[2] & bc_mask;
615       clib_prefetch_store (b[7]);
616       vlib_buffer_copy_template (b[3], bt);
617       n_rx_bytes += b[3]->current_length = bc[3] & bc_mask;
618       /* next */
619       bc += 4;
620       b += 4;
621       n_left -= 4;
622     }
623   while (n_left)
624     {
625       vlib_buffer_copy_template (b[0], bt);
626       n_rx_bytes += b[0]->current_length = bc[0] & bc_mask;
627       /* next */
628       bc++;
629       b++;
630       n_left--;
631     }
632   return n_rx_bytes;
633 }
634
635 static_always_inline u32
636 rdma_device_mlx5dv_striding_rq_input (vlib_main_t * vm,
637                                       rdma_per_thread_data_t * ptd,
638                                       rdma_rxq_t * rxq,
639                                       vlib_buffer_t * bt, u32 * to_next,
640                                       int n_rx_segs, int *n_rx_packets,
641                                       u32 * bc, int slow_path_needed)
642 {
643   u32 mask = rxq->size - 1;
644   u32 n_rx_bytes = 0;
645   if (PREDICT_TRUE (!slow_path_needed))
646     {
647       n_rx_bytes +=
648         rdma_device_mlx5dv_fast_input (vm, rxq, mask, bt, to_next,
649                                        n_rx_segs, bc, CQE_BC_BYTE_COUNT_MASK);
650     }
651   else                          /* Slow path with multiseg */
652     {
653       vlib_buffer_t *pkt_head;  /*Current head buffer */
654       vlib_buffer_t *pkt_prev;  /* Buffer processed at the previous iteration */
655       u32 pkt_head_idx;
656       vlib_buffer_t **pkt;
657       uword n_segs_remaining = 0;       /*Remaining strides in current buffer */
658       u32 n_bytes_remaining = 0;        /*Remaining bytes in current buffer */
659       u32 *next_in_frame = to_next;
660       u32 *next_to_free = ptd->to_free_buffers;
661       bt->current_length = vlib_buffer_get_default_data_size (vm);
662       do
663         {
664           vlib_buffer_t *bufs[VLIB_FRAME_SIZE];
665           u32 n_left = clib_min (n_rx_segs, VLIB_FRAME_SIZE);
666           n_rx_segs -= n_left;
667           vlib_buffer_copy_indices_from_ring (ptd->current_segs,
668                                               rxq->bufs, rxq->head & mask,
669                                               rxq->size, n_left);
670           rxq->head += n_left;
671           vlib_get_buffers (vm, ptd->current_segs, bufs, n_left);
672           pkt = bufs;
673           while (n_left > 0)
674             {
675               /* Initialize the current buffer as full size */
676               vlib_buffer_copy_template (pkt[0], bt);
677               if (!n_segs_remaining)    /* No pending chain */
678                 {
679                   n_segs_remaining =
680                     (bc[0] & CQE_BC_CONSUMED_STRIDES_MASK) >>
681                     CQE_BC_CONSUMED_STRIDES_SHIFT;
682                   pkt_head = pkt[0];
683                   pkt_head_idx = ptd->current_segs[pkt - bufs];
684                   n_bytes_remaining = bc[0] & CQE_BC_BYTE_COUNT_MASK;
685                   pkt_head->total_length_not_including_first_buffer =
686                     n_segs_remaining >
687                     1 ? n_bytes_remaining - pkt[0]->current_length : 0;
688                 }
689               else              /* Perform chaining if it's a continuation buffer */
690                 {
691                   pkt_prev->next_buffer = ptd->current_segs[pkt - bufs];
692                   pkt_prev->flags |= VLIB_BUFFER_NEXT_PRESENT;
693                   pkt[0]->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
694                 }
695               if (n_segs_remaining == 1)        /* Last buffer of the chain */
696                 {
697                   pkt[0]->current_length = n_bytes_remaining;
698                   if (bc[0] & CQE_BC_FILLER_MASK)
699                     {
700                       (next_to_free++)[0] = pkt_head_idx;
701                       (*n_rx_packets)--;
702                     }
703
704                   else
705                     {
706                       (next_in_frame++)[0] = pkt_head_idx;
707                       n_rx_bytes +=
708                         pkt_head->current_length +
709                         pkt_head->total_length_not_including_first_buffer;
710                     }
711                   /*Go to next CQE */
712                   bc++;
713                 }
714               else
715                 {
716                   n_bytes_remaining -= pkt[0]->current_length;
717                   pkt_prev = pkt[0];
718                 }
719               n_segs_remaining--;
720               n_left--;
721               pkt++;
722             }
723
724         }
725       while (n_rx_segs > 0);
726       vlib_buffer_free (vm, ptd->to_free_buffers,
727                         next_to_free - ptd->to_free_buffers);
728     }
729   return n_rx_bytes;
730 }
731
732 static_always_inline uword
733 rdma_device_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
734                           vlib_frame_t * frame, rdma_device_t * rd,
735                           u16 qid, int use_mlx5dv)
736 {
737   rdma_main_t *rm = &rdma_main;
738   vnet_main_t *vnm = vnet_get_main ();
739   rdma_per_thread_data_t *ptd = vec_elt_at_index (rm->per_thread_data,
740                                                   vm->thread_index);
741   rdma_rxq_t *rxq = vec_elt_at_index (rd->rxqs, qid);
742   struct ibv_wc wc[VLIB_FRAME_SIZE];
743   u32 __clib_aligned (32) byte_cnts[VLIB_FRAME_SIZE];
744   vlib_buffer_t bt;
745   u32 next_index, *to_next, n_left_to_next, n_rx_bytes = 0;
746   int n_rx_packets, skip_ip4_cksum = 0;
747   u32 mask = rxq->size - 1;
748
749   if (use_mlx5dv)
750     n_rx_packets = rdma_device_poll_cq_mlx5dv (rd, rxq, byte_cnts,
751                                                ptd->cqe_flags);
752   else
753     n_rx_packets = ibv_poll_cq (rxq->cq, VLIB_FRAME_SIZE, wc);
754
755   if (PREDICT_FALSE (n_rx_packets <= 0))
756     goto refill;
757
758   /* init buffer template */
759   vlib_buffer_copy_template (&bt, &ptd->buffer_template);
760   vnet_buffer (&bt)->sw_if_index[VLIB_RX] = rd->sw_if_index;
761   bt.buffer_pool_index = rd->pool;
762
763   /* update buffer template for input feature arcs if any */
764   next_index = rd->per_interface_next_index;
765   if (PREDICT_FALSE (vnet_device_input_have_features (rd->sw_if_index)))
766     vnet_feature_start_device_input_x1 (rd->sw_if_index, &next_index, &bt);
767
768   vlib_get_new_next_frame (vm, node, next_index, to_next, n_left_to_next);
769
770   if (use_mlx5dv)
771     {
772       u32 *bc = byte_cnts;
773       int slow_path_needed;
774       skip_ip4_cksum =
775         rdma_device_mlx5dv_l3_validate_and_swap_bc (ptd, n_rx_packets, bc);
776       if (rd->flags & RDMA_DEVICE_F_STRIDING_RQ)
777         {
778           int n_rx_segs = 0;
779           slow_path_needed =
780             rdma_device_mlx5dv_striding_rq_parse_bc (n_rx_packets,
781                                                      &n_rx_segs, bc);
782           n_rx_bytes =
783             rdma_device_mlx5dv_striding_rq_input (vm, ptd, rxq, &bt,
784                                                   to_next, n_rx_segs,
785                                                   &n_rx_packets, bc,
786                                                   slow_path_needed);
787         }
788       else
789         {
790           /*For now, legacy path doesn't support multiseg */
791           n_rx_bytes =
792             rdma_device_mlx5dv_fast_input (vm, rxq, mask, &bt, to_next,
793                                            n_rx_packets, bc, ~1);
794         }
795
796     }
797   else
798     {
799       vlib_buffer_t *bufs[VLIB_FRAME_SIZE];
800       vlib_buffer_copy_indices_from_ring (to_next, rxq->bufs,
801                                           rxq->head & mask,
802                                           rxq->size, n_rx_packets);
803       vlib_get_buffers (vm, to_next, bufs, n_rx_packets);
804       rxq->head += n_rx_packets;
805       n_rx_bytes =
806         rdma_device_input_bufs (vm, rd, bufs, wc, n_rx_packets, &bt);
807
808     }
809
810   rdma_device_input_ethernet (vm, node, rd, next_index, skip_ip4_cksum);
811   vlib_put_next_frame (vm, node, next_index, n_left_to_next - n_rx_packets);
812   rdma_device_input_trace (vm, node, rd, n_rx_packets, to_next,
813                            next_index, ptd->cqe_flags, use_mlx5dv);
814   /* reset flags to zero for the next run */
815   if (use_mlx5dv)
816     clib_memset_u16 (ptd->cqe_flags, 0, VLIB_FRAME_SIZE);
817   vlib_increment_combined_counter (vnm->interface_main.
818                                    combined_sw_if_counters +
819                                    VNET_INTERFACE_COUNTER_RX,
820                                    vm->thread_index, rd->hw_if_index,
821                                    n_rx_packets, n_rx_bytes);
822 refill:
823   rdma_device_input_refill (vm, rd, rxq, use_mlx5dv,
824                             ! !(rd->flags & RDMA_DEVICE_F_STRIDING_RQ));
825   return n_rx_packets;
826 }
827
828 VLIB_NODE_FN (rdma_input_node) (vlib_main_t * vm,
829                                 vlib_node_runtime_t * node,
830                                 vlib_frame_t * frame)
831 {
832   u32 n_rx = 0;
833   rdma_main_t *rm = &rdma_main;
834   vnet_device_input_runtime_t *rt = (void *) node->runtime_data;
835   vnet_device_and_queue_t *dq;
836
837   foreach_device_and_queue (dq, rt->devices_and_queues)
838   {
839     rdma_device_t *rd;
840     rd = vec_elt_at_index (rm->devices, dq->dev_instance);
841     if (PREDICT_TRUE (rd->flags & RDMA_DEVICE_F_ADMIN_UP) == 0)
842       continue;
843
844     if (PREDICT_TRUE (rd->flags & RDMA_DEVICE_F_ERROR))
845       continue;
846
847     if (PREDICT_TRUE (rd->flags & RDMA_DEVICE_F_MLX5DV))
848       n_rx += rdma_device_input_inline (vm, node, frame, rd, dq->queue_id, 1);
849     else
850       n_rx += rdma_device_input_inline (vm, node, frame, rd, dq->queue_id, 0);
851   }
852   return n_rx;
853 }
854
855 /* *INDENT-OFF* */
856 VLIB_REGISTER_NODE (rdma_input_node) = {
857   .name = "rdma-input",
858   .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED,
859   .sibling_of = "device-input",
860   .format_trace = format_rdma_input_trace,
861   .type = VLIB_NODE_TYPE_INPUT,
862   .state = VLIB_NODE_STATE_DISABLED,
863   .n_errors = RDMA_INPUT_N_ERROR,
864   .error_strings = rdma_input_error_strings,
865 };
866
867 /* *INDENT-ON* */
868
869
870 /*
871  * fd.io coding-style-patch-verification: ON
872  *
873  * Local Variables:
874  * eval: (c-set-style "gnu")
875  * End:
876  */