iavf: new driver using new dev infra
[vpp.git] / src / plugins / dev_iavf / rx_node.c
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright (c) 2023 Cisco Systems, Inc.
3  */
4
5 #include <vlib/vlib.h>
6 #include <vnet/dev/dev.h>
7 #include <vnet/ethernet/ethernet.h>
8 #include <dev_iavf/iavf.h>
9
10 #define IAVF_RX_REFILL_TRESHOLD 32
11
12 static const iavf_rx_desc_qw1_t mask_eop = { .eop = 1 };
13 static const iavf_rx_desc_qw1_t mask_flm = { .flm = 1 };
14 static const iavf_rx_desc_qw1_t mask_dd = { .dd = 1 };
15 static const iavf_rx_desc_qw1_t mask_ipe = { .ipe = 1 };
16 static const iavf_rx_desc_qw1_t mask_dd_eop = { .dd = 1, .eop = 1 };
17
18 static_always_inline int
19 iavf_rxd_is_not_eop (iavf_rx_desc_t *d)
20 {
21   return (d->qw1.as_u64 & mask_eop.as_u64) == 0;
22 }
23
24 static_always_inline int
25 iavf_rxd_is_not_dd (iavf_rx_desc_t *d)
26 {
27   return (d->qw1.as_u64 & mask_dd.as_u64) == 0;
28 }
29
30 static_always_inline void
31 iavf_rx_desc_write (iavf_rx_desc_t *d, u64 addr)
32 {
33 #ifdef CLIB_HAVE_VEC256
34   *(u64x4 *) d = (u64x4){ addr, 0, 0, 0 };
35 #else
36   d->qword[0] = addr;
37   d->qword[1] = 0;
38 #endif
39 }
40
41 static_always_inline void
42 iavf_rxq_refill (vlib_main_t *vm, vlib_node_runtime_t *node,
43                  vnet_dev_rx_queue_t *rxq, int use_va_dma)
44 {
45   u16 n_refill, mask, n_alloc, slot, size;
46   iavf_rxq_t *arq = vnet_dev_get_rx_queue_data (rxq);
47   vlib_buffer_t *b[8];
48   iavf_rx_desc_t *d, *first_d;
49   void *p[8];
50
51   size = rxq->size;
52   mask = size - 1;
53   n_refill = mask - arq->n_enqueued;
54   if (PREDICT_TRUE (n_refill <= IAVF_RX_REFILL_TRESHOLD))
55     return;
56
57   slot = (arq->next - n_refill - 1) & mask;
58
59   n_refill &= ~7; /* round to 8 */
60   n_alloc = vlib_buffer_alloc_to_ring_from_pool (
61     vm, arq->buffer_indices, slot, size, n_refill,
62     vnet_dev_get_rx_queue_buffer_pool_index (rxq));
63
64   if (PREDICT_FALSE (n_alloc != n_refill))
65     {
66       vlib_error_count (vm, node->node_index, IAVF_RX_NODE_CTR_BUFFER_ALLOC,
67                         1);
68       if (n_alloc)
69         vlib_buffer_free_from_ring (vm, arq->buffer_indices, slot, size,
70                                     n_alloc);
71       return;
72     }
73
74   arq->n_enqueued += n_alloc;
75   first_d = arq->descs;
76
77   ASSERT (slot % 8 == 0);
78
79   while (n_alloc >= 8)
80     {
81       d = first_d + slot;
82
83       if (use_va_dma)
84         {
85           vlib_get_buffers_with_offset (vm, arq->buffer_indices + slot, p, 8,
86                                         sizeof (vlib_buffer_t));
87           iavf_rx_desc_write (d + 0, pointer_to_uword (p[0]));
88           iavf_rx_desc_write (d + 1, pointer_to_uword (p[1]));
89           iavf_rx_desc_write (d + 2, pointer_to_uword (p[2]));
90           iavf_rx_desc_write (d + 3, pointer_to_uword (p[3]));
91           iavf_rx_desc_write (d + 4, pointer_to_uword (p[4]));
92           iavf_rx_desc_write (d + 5, pointer_to_uword (p[5]));
93           iavf_rx_desc_write (d + 6, pointer_to_uword (p[6]));
94           iavf_rx_desc_write (d + 7, pointer_to_uword (p[7]));
95         }
96       else
97         {
98           vlib_get_buffers (vm, arq->buffer_indices + slot, b, 8);
99           iavf_rx_desc_write (d + 0, vlib_buffer_get_pa (vm, b[0]));
100           iavf_rx_desc_write (d + 1, vlib_buffer_get_pa (vm, b[1]));
101           iavf_rx_desc_write (d + 2, vlib_buffer_get_pa (vm, b[2]));
102           iavf_rx_desc_write (d + 3, vlib_buffer_get_pa (vm, b[3]));
103           iavf_rx_desc_write (d + 4, vlib_buffer_get_pa (vm, b[4]));
104           iavf_rx_desc_write (d + 5, vlib_buffer_get_pa (vm, b[5]));
105           iavf_rx_desc_write (d + 6, vlib_buffer_get_pa (vm, b[6]));
106           iavf_rx_desc_write (d + 7, vlib_buffer_get_pa (vm, b[7]));
107         }
108
109       /* next */
110       slot = (slot + 8) & mask;
111       n_alloc -= 8;
112     }
113
114   __atomic_store_n (arq->qrx_tail, slot, __ATOMIC_RELEASE);
115 }
116
117 static_always_inline uword
118 iavf_rx_attach_tail (vlib_main_t *vm, vlib_buffer_template_t *bt,
119                      vlib_buffer_t *b, u64 qw1, iavf_rx_tail_t *t)
120 {
121   vlib_buffer_t *hb = b;
122   u32 tlnifb = 0, i = 0;
123
124   if (qw1 & mask_eop.as_u64)
125     return 0;
126
127   while ((qw1 & mask_eop.as_u64) == 0)
128     {
129       ASSERT (i < IAVF_RX_MAX_DESC_IN_CHAIN - 1);
130       ASSERT (qw1 & mask_dd.as_u64);
131       qw1 = t->qw1s[i];
132       b->next_buffer = t->buffers[i];
133       b->flags |= VLIB_BUFFER_NEXT_PRESENT;
134       b = vlib_get_buffer (vm, b->next_buffer);
135       b->template = *bt;
136       tlnifb += b->current_length = ((iavf_rx_desc_qw1_t) qw1).length;
137       i++;
138     }
139
140   hb->total_length_not_including_first_buffer = tlnifb;
141   hb->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
142   return tlnifb;
143 }
144
145 static_always_inline void
146 iavf_process_flow_offload (vnet_dev_port_t *port, iavf_rt_data_t *rtd,
147                            uword n_rx_packets)
148 {
149   uword n;
150   iavf_flow_lookup_entry_t fle;
151   iavf_port_t *ap = vnet_dev_get_port_data (port);
152
153   for (n = 0; n < n_rx_packets; n++)
154     {
155       if ((rtd->qw1s[n] & mask_flm.as_u64) == 0)
156         continue;
157
158       fle = *pool_elt_at_index (ap->flow_lookup_entries, rtd->flow_ids[n]);
159
160       if (fle.next_index != (u16) ~0)
161         rtd->next[n] = fle.next_index;
162
163       if (fle.flow_id != ~0)
164         rtd->bufs[n]->flow_id = fle.flow_id;
165
166       if (fle.buffer_advance != ~0)
167         vlib_buffer_advance (rtd->bufs[n], fle.buffer_advance);
168     }
169 }
170
171 static_always_inline uword
172 iavf_process_rx_burst (vlib_main_t *vm, vlib_node_runtime_t *node,
173                        vnet_dev_rx_queue_t *rxq, iavf_rt_data_t *rtd,
174                        vlib_buffer_template_t *bt, u32 n_left,
175                        int maybe_multiseg)
176 {
177   vlib_buffer_t **b = rtd->bufs;
178   u64 *qw1 = rtd->qw1s;
179   iavf_rx_tail_t *tail = rtd->tails;
180   uword n_rx_bytes = 0;
181
182   while (n_left >= 4)
183     {
184       if (n_left >= 12)
185         {
186           vlib_prefetch_buffer_header (b[8], LOAD);
187           vlib_prefetch_buffer_header (b[9], LOAD);
188           vlib_prefetch_buffer_header (b[10], LOAD);
189           vlib_prefetch_buffer_header (b[11], LOAD);
190         }
191
192       b[0]->template = *bt;
193       b[1]->template = *bt;
194       b[2]->template = *bt;
195       b[3]->template = *bt;
196
197       n_rx_bytes += b[0]->current_length =
198         ((iavf_rx_desc_qw1_t) qw1[0]).length;
199       n_rx_bytes += b[1]->current_length =
200         ((iavf_rx_desc_qw1_t) qw1[1]).length;
201       n_rx_bytes += b[2]->current_length =
202         ((iavf_rx_desc_qw1_t) qw1[2]).length;
203       n_rx_bytes += b[3]->current_length =
204         ((iavf_rx_desc_qw1_t) qw1[3]).length;
205
206       if (maybe_multiseg)
207         {
208           n_rx_bytes += iavf_rx_attach_tail (vm, bt, b[0], qw1[0], tail + 0);
209           n_rx_bytes += iavf_rx_attach_tail (vm, bt, b[1], qw1[1], tail + 1);
210           n_rx_bytes += iavf_rx_attach_tail (vm, bt, b[2], qw1[2], tail + 2);
211           n_rx_bytes += iavf_rx_attach_tail (vm, bt, b[3], qw1[3], tail + 3);
212         }
213
214       /* next */
215       qw1 += 4;
216       tail += 4;
217       b += 4;
218       n_left -= 4;
219     }
220
221   while (n_left)
222     {
223       b[0]->template = *bt;
224
225       n_rx_bytes += b[0]->current_length =
226         ((iavf_rx_desc_qw1_t) qw1[0]).length;
227
228       if (maybe_multiseg)
229         n_rx_bytes += iavf_rx_attach_tail (vm, bt, b[0], qw1[0], tail + 0);
230
231       /* next */
232       qw1 += 1;
233       tail += 1;
234       b += 1;
235       n_left -= 1;
236     }
237   return n_rx_bytes;
238 }
239
240 static_always_inline uword
241 iavf_device_input_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
242                           vlib_frame_t *frame, vnet_dev_port_t *port,
243                           vnet_dev_rx_queue_t *rxq, int with_flows)
244 {
245   vnet_main_t *vnm = vnet_get_main ();
246   u32 thr_idx = vlib_get_thread_index ();
247   iavf_rt_data_t *rtd = vnet_dev_get_rt_temp_space (vm);
248   iavf_rxq_t *arq = vnet_dev_get_rx_queue_data (rxq);
249   vlib_buffer_template_t bt = rxq->buffer_template;
250   u32 n_trace, n_rx_packets = 0, n_rx_bytes = 0;
251   u16 n_tail_desc = 0;
252   u64 or_qw1 = 0;
253   u32 *bi, *to_next, n_left_to_next;
254   u32 next_index = rxq->next_index;
255   u32 sw_if_index = port->intf.sw_if_index;
256   u32 hw_if_index = port->intf.hw_if_index;
257   u16 next = arq->next;
258   u16 size = rxq->size;
259   u16 mask = size - 1;
260   iavf_rx_desc_t *d, *descs = arq->descs;
261 #ifdef CLIB_HAVE_VEC256
262   u64x4 q1x4, or_q1x4 = { 0 };
263   u32x4 fdidx4;
264   u64x4 dd_eop_mask4 = u64x4_splat (mask_dd_eop.as_u64);
265 #elif defined(CLIB_HAVE_VEC128)
266   u32x4 q1x4_lo, q1x4_hi, or_q1x4 = { 0 };
267   u32x4 fdidx4;
268   u32x4 dd_eop_mask4 = u32x4_splat (mask_dd_eop.as_u64);
269 #endif
270   int single_next = 1;
271
272   /* is there anything on the ring */
273   d = descs + next;
274   if ((d->qword[1] & mask_dd.as_u64) == 0)
275     goto done;
276
277   vlib_get_new_next_frame (vm, node, next_index, to_next, n_left_to_next);
278
279   /* fetch up to IAVF_RX_VECTOR_SZ from the rx ring, unflatten them and
280      copy needed data from descriptor to rx vector */
281   bi = to_next;
282
283   while (n_rx_packets < IAVF_RX_VECTOR_SZ)
284     {
285       if (next + 11 < size)
286         {
287           int stride = 8;
288           clib_prefetch_load ((void *) (descs + (next + stride)));
289           clib_prefetch_load ((void *) (descs + (next + stride + 1)));
290           clib_prefetch_load ((void *) (descs + (next + stride + 2)));
291           clib_prefetch_load ((void *) (descs + (next + stride + 3)));
292         }
293
294 #ifdef CLIB_HAVE_VEC256
295       if (n_rx_packets >= IAVF_RX_VECTOR_SZ - 4 || next >= size - 4)
296         goto one_by_one;
297
298       q1x4 = u64x4_gather ((void *) &d[0].qword[1], (void *) &d[1].qword[1],
299                            (void *) &d[2].qword[1], (void *) &d[3].qword[1]);
300
301       /* not all packets are ready or at least one of them is chained */
302       if (!u64x4_is_equal (q1x4 & dd_eop_mask4, dd_eop_mask4))
303         goto one_by_one;
304
305       or_q1x4 |= q1x4;
306
307       u64x4_store_unaligned (q1x4, rtd->qw1s + n_rx_packets);
308 #elif defined(CLIB_HAVE_VEC128)
309       if (n_rx_packets >= IAVF_RX_VECTOR_SZ - 4 || next >= size - 4)
310         goto one_by_one;
311
312       q1x4_lo =
313         u32x4_gather ((void *) &d[0].qword[1], (void *) &d[1].qword[1],
314                       (void *) &d[2].qword[1], (void *) &d[3].qword[1]);
315
316       /* not all packets are ready or at least one of them is chained */
317       if (!u32x4_is_equal (q1x4_lo & dd_eop_mask4, dd_eop_mask4))
318         goto one_by_one;
319
320       q1x4_hi = u32x4_gather (
321         (void *) &d[0].qword[1] + 4, (void *) &d[1].qword[1] + 4,
322         (void *) &d[2].qword[1] + 4, (void *) &d[3].qword[1] + 4);
323
324       or_q1x4 |= q1x4_lo;
325       rtd->qw1s[n_rx_packets + 0] = (u64) q1x4_hi[0] << 32 | (u64) q1x4_lo[0];
326       rtd->qw1s[n_rx_packets + 1] = (u64) q1x4_hi[1] << 32 | (u64) q1x4_lo[1];
327       rtd->qw1s[n_rx_packets + 2] = (u64) q1x4_hi[2] << 32 | (u64) q1x4_lo[2];
328       rtd->qw1s[n_rx_packets + 3] = (u64) q1x4_hi[3] << 32 | (u64) q1x4_lo[3];
329 #endif
330 #if defined(CLIB_HAVE_VEC256) || defined(CLIB_HAVE_VEC128)
331
332       if (with_flows)
333         {
334           fdidx4 = u32x4_gather (
335             (void *) &d[0].fdid_flex_hi, (void *) &d[1].fdid_flex_hi,
336             (void *) &d[2].fdid_flex_hi, (void *) &d[3].fdid_flex_hi);
337           u32x4_store_unaligned (fdidx4, rtd->flow_ids + n_rx_packets);
338         }
339
340       vlib_buffer_copy_indices (bi, arq->buffer_indices + next, 4);
341
342       /* next */
343       next = (next + 4) & mask;
344       d = descs + next;
345       n_rx_packets += 4;
346       bi += 4;
347       continue;
348     one_by_one:
349 #endif
350       clib_prefetch_load ((void *) (descs + ((next + 8) & mask)));
351
352       if (iavf_rxd_is_not_dd (d))
353         break;
354
355       bi[0] = arq->buffer_indices[next];
356
357       /* deal with chained buffers */
358       if (PREDICT_FALSE (iavf_rxd_is_not_eop (d)))
359         {
360           u16 tail_desc = 0;
361           u16 tail_next = next;
362           iavf_rx_tail_t *tail = rtd->tails + n_rx_packets;
363           iavf_rx_desc_t *td;
364           do
365             {
366               tail_next = (tail_next + 1) & mask;
367               td = descs + tail_next;
368
369               /* bail out in case of incomplete transaction */
370               if (iavf_rxd_is_not_dd (td))
371                 goto no_more_desc;
372
373               or_qw1 |= tail->qw1s[tail_desc] = td[0].qword[1];
374               tail->buffers[tail_desc] = arq->buffer_indices[tail_next];
375               tail_desc++;
376             }
377           while (iavf_rxd_is_not_eop (td));
378           next = tail_next;
379           n_tail_desc += tail_desc;
380         }
381
382       or_qw1 |= rtd->qw1s[n_rx_packets] = d[0].qword[1];
383       if (PREDICT_FALSE (with_flows))
384         {
385           rtd->flow_ids[n_rx_packets] = d[0].fdid_flex_hi;
386         }
387
388       /* next */
389       next = (next + 1) & mask;
390       d = descs + next;
391       n_rx_packets++;
392       bi++;
393     }
394 no_more_desc:
395
396   if (n_rx_packets == 0)
397     goto done;
398
399   arq->next = next;
400   arq->n_enqueued -= n_rx_packets + n_tail_desc;
401
402   /* avoid eating our own tail */
403   arq->descs[(next + arq->n_enqueued) & mask].qword[1] = 0;
404
405 #if defined(CLIB_HAVE_VEC256) || defined(CLIB_HAVE_VEC128)
406   or_qw1 |= or_q1x4[0] | or_q1x4[1] | or_q1x4[2] | or_q1x4[3];
407 #endif
408
409   vlib_get_buffers (vm, to_next, rtd->bufs, n_rx_packets);
410
411   n_rx_bytes =
412     n_tail_desc ?
413             iavf_process_rx_burst (vm, node, rxq, rtd, &bt, n_rx_packets, 1) :
414             iavf_process_rx_burst (vm, node, rxq, rtd, &bt, n_rx_packets, 0);
415
416   /* the MARKed packets may have different next nodes */
417   if (PREDICT_FALSE (with_flows && (or_qw1 & mask_flm.as_u64)))
418     {
419       u32 n;
420       single_next = 0;
421       for (n = 0; n < n_rx_packets; n++)
422         rtd->next[n] = next_index;
423
424       iavf_process_flow_offload (port, rtd, n_rx_packets);
425     }
426
427   /* packet trace if enabled */
428   if (PREDICT_FALSE ((n_trace = vlib_get_trace_count (vm, node))))
429     {
430       u32 n_left = n_rx_packets;
431       u32 i, j;
432       u16 *next_indices = rtd->next;
433
434       i = 0;
435       while (n_trace && n_left)
436         {
437           vlib_buffer_t *b = rtd->bufs[i];
438           if (PREDICT_FALSE (single_next == 0))
439             next_index = next_indices[0];
440
441           if (PREDICT_TRUE (vlib_trace_buffer (vm, node, next_index, b,
442                                                /* follow_chain */ 0)))
443             {
444               iavf_rx_trace_t *tr = vlib_add_trace (vm, node, b, sizeof (*tr));
445               tr->next_index = next_index;
446               tr->qid = rxq->queue_id;
447               tr->hw_if_index = hw_if_index;
448               tr->qw1s[0] = rtd->qw1s[i];
449               tr->flow_id =
450                 (tr->qw1s[0] & mask_flm.as_u64) ? rtd->flow_ids[i] : 0;
451               for (j = 1; j < IAVF_RX_MAX_DESC_IN_CHAIN; j++)
452                 tr->qw1s[j] = rtd->tails[i].qw1s[j - 1];
453
454               n_trace--;
455             }
456
457           /* next */
458           n_left--;
459           i++;
460           next_indices++;
461         }
462       vlib_set_trace_count (vm, node, n_trace);
463     }
464
465   /* enqueu the packets to the next nodes */
466   if (PREDICT_FALSE (with_flows && (or_qw1 & mask_flm.as_u64)))
467     {
468       /* release next node's frame vector, in this case we use
469          vlib_buffer_enqueue_to_next to place the packets
470        */
471       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
472
473       /* enqueue buffers to the next node */
474       vlib_buffer_enqueue_to_next (vm, node, to_next, rtd->next, n_rx_packets);
475     }
476   else
477     {
478       if (PREDICT_TRUE (next_index == VNET_DEV_ETH_RX_PORT_NEXT_ETH_INPUT))
479         {
480           vlib_next_frame_t *nf;
481           vlib_frame_t *f;
482           ethernet_input_frame_t *ef;
483           nf = vlib_node_runtime_get_next_frame (vm, node, next_index);
484           f = vlib_get_frame (vm, nf->frame);
485           f->flags = ETH_INPUT_FRAME_F_SINGLE_SW_IF_IDX;
486
487           ef = vlib_frame_scalar_args (f);
488           ef->sw_if_index = sw_if_index;
489           ef->hw_if_index = hw_if_index;
490
491           if ((or_qw1 & mask_ipe.as_u64) == 0)
492             f->flags |= ETH_INPUT_FRAME_F_IP4_CKSUM_OK;
493           vlib_frame_no_append (f);
494         }
495
496       n_left_to_next -= n_rx_packets;
497       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
498     }
499
500   vlib_increment_combined_counter (
501     vnm->interface_main.combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
502     thr_idx, hw_if_index, n_rx_packets, n_rx_bytes);
503
504 done:
505   return n_rx_packets;
506 }
507
508 VNET_DEV_NODE_FN (iavf_rx_node)
509 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
510 {
511   u32 n_rx = 0;
512   foreach_vnet_dev_rx_queue_runtime (rxq, node)
513     {
514       vnet_dev_port_t *port = rxq->port;
515       iavf_port_t *ap = vnet_dev_get_port_data (port);
516       if (PREDICT_FALSE (ap->flow_offload))
517         n_rx += iavf_device_input_inline (vm, node, frame, port, rxq, 1);
518       else
519         n_rx += iavf_device_input_inline (vm, node, frame, port, rxq, 0);
520
521       /* refill rx ring */
522       if (rxq->port->dev->va_dma)
523         iavf_rxq_refill (vm, node, rxq, 1 /* use_va_dma */);
524       else
525         iavf_rxq_refill (vm, node, rxq, 0 /* use_va_dma */);
526     }
527
528   return n_rx;
529 }