b784bf731c1611c5843fe86d538a8cfc35df91b1
[vpp.git] / src / plugins / avf / 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 <avf/avf.h>
25
26 #define foreach_avf_input_error \
27   _(BUFFER_ALLOC, "buffer alloc error")
28
29 typedef enum
30 {
31 #define _(f,s) AVF_INPUT_ERROR_##f,
32   foreach_avf_input_error
33 #undef _
34     AVF_INPUT_N_ERROR,
35 } avf_input_error_t;
36
37 static __clib_unused char *avf_input_error_strings[] = {
38 #define _(n,s) s,
39   foreach_avf_input_error
40 #undef _
41 };
42
43 #define AVF_INPUT_REFILL_TRESHOLD 32
44
45 static_always_inline void
46 avf_rx_desc_write (avf_rx_desc_t * d, u64 addr)
47 {
48 #ifdef CLIB_HAVE_VEC256
49   u64x4 v = { addr, 0, 0, 0 };
50   u64x4_store_unaligned (v, (void *) d);
51 #else
52   d->qword[0] = addr;
53   d->qword[1] = 0;
54 #endif
55 }
56
57 static_always_inline void
58 avf_rxq_refill (vlib_main_t * vm, vlib_node_runtime_t * node, avf_rxq_t * rxq,
59                 int use_va_dma)
60 {
61   u16 n_refill, mask, n_alloc, slot, size;
62   vlib_buffer_t *b[8];
63   avf_rx_desc_t *d, *first_d;
64   void *p[8];
65
66   size = rxq->size;
67   mask = size - 1;
68   n_refill = mask - rxq->n_enqueued;
69   if (PREDICT_TRUE (n_refill <= AVF_INPUT_REFILL_TRESHOLD))
70     return;
71
72   slot = (rxq->next - n_refill - 1) & mask;
73
74   n_refill &= ~7;               /* round to 8 */
75   n_alloc = vlib_buffer_alloc_to_ring (vm, rxq->bufs, slot, size, n_refill);
76
77   if (PREDICT_FALSE (n_alloc != n_refill))
78     {
79       vlib_error_count (vm, node->node_index,
80                         AVF_INPUT_ERROR_BUFFER_ALLOC, 1);
81       if (n_alloc)
82         vlib_buffer_free_from_ring (vm, rxq->bufs, slot, size, n_alloc);
83       return;
84     }
85
86   rxq->n_enqueued += n_alloc;
87   first_d = rxq->descs;
88
89   ASSERT (slot % 8 == 0);
90
91   while (n_alloc >= 8)
92     {
93       d = first_d + slot;
94
95       if (use_va_dma)
96         {
97           vlib_get_buffers_with_offset (vm, rxq->bufs + slot, p, 8,
98                                         sizeof (vlib_buffer_t));
99           avf_rx_desc_write (d + 0, pointer_to_uword (p[0]));
100           avf_rx_desc_write (d + 1, pointer_to_uword (p[1]));
101           avf_rx_desc_write (d + 2, pointer_to_uword (p[2]));
102           avf_rx_desc_write (d + 3, pointer_to_uword (p[3]));
103           avf_rx_desc_write (d + 4, pointer_to_uword (p[4]));
104           avf_rx_desc_write (d + 5, pointer_to_uword (p[5]));
105           avf_rx_desc_write (d + 6, pointer_to_uword (p[6]));
106           avf_rx_desc_write (d + 7, pointer_to_uword (p[7]));
107         }
108       else
109         {
110           vlib_get_buffers (vm, rxq->bufs + slot, b, 8);
111           avf_rx_desc_write (d + 0, vlib_buffer_get_pa (vm, b[0]));
112           avf_rx_desc_write (d + 1, vlib_buffer_get_pa (vm, b[1]));
113           avf_rx_desc_write (d + 2, vlib_buffer_get_pa (vm, b[2]));
114           avf_rx_desc_write (d + 3, vlib_buffer_get_pa (vm, b[3]));
115           avf_rx_desc_write (d + 4, vlib_buffer_get_pa (vm, b[4]));
116           avf_rx_desc_write (d + 5, vlib_buffer_get_pa (vm, b[5]));
117           avf_rx_desc_write (d + 6, vlib_buffer_get_pa (vm, b[6]));
118           avf_rx_desc_write (d + 7, vlib_buffer_get_pa (vm, b[7]));
119         }
120
121       /* next */
122       slot = (slot + 8) & mask;
123       n_alloc -= 8;
124     }
125
126   CLIB_MEMORY_STORE_BARRIER ();
127   *(rxq->qrx_tail) = slot;
128 }
129
130
131 static_always_inline uword
132 avf_rx_attach_tail (vlib_main_t * vm, vlib_buffer_t * bt, vlib_buffer_t * b,
133                     u64 qw1, avf_rx_tail_t * t)
134 {
135   vlib_buffer_t *hb = b;
136   u32 tlnifb = 0, i = 0;
137
138   if (qw1 & AVF_RXD_STATUS_EOP)
139     return 0;
140
141   while ((qw1 & AVF_RXD_STATUS_EOP) == 0)
142     {
143       ASSERT (i < AVF_RX_MAX_DESC_IN_CHAIN - 1);
144       ASSERT (qw1 & AVF_RXD_STATUS_DD);
145       qw1 = t->qw1s[i];
146       b->next_buffer = t->buffers[i];
147       b->flags |= VLIB_BUFFER_NEXT_PRESENT;
148       b = vlib_get_buffer (vm, b->next_buffer);
149       vlib_buffer_copy_template (b, bt);
150       tlnifb += b->current_length = qw1 >> AVF_RXD_LEN_SHIFT;
151       i++;
152     }
153
154   hb->total_length_not_including_first_buffer = tlnifb;
155   hb->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
156   return tlnifb;
157 }
158
159 static_always_inline uword
160 avf_process_rx_burst (vlib_main_t * vm, vlib_node_runtime_t * node,
161                       avf_per_thread_data_t * ptd, u32 n_left,
162                       int maybe_multiseg)
163 {
164   vlib_buffer_t bt;
165   vlib_buffer_t **b = ptd->bufs;
166   u64 *qw1 = ptd->qw1s;
167   avf_rx_tail_t *tail = ptd->tails;
168   uword n_rx_bytes = 0;
169
170   /* copy template into local variable - will save per packet load */
171   vlib_buffer_copy_template (&bt, &ptd->buffer_template);
172
173   while (n_left >= 4)
174     {
175       if (n_left >= 12)
176         {
177           vlib_prefetch_buffer_header (b[8], LOAD);
178           vlib_prefetch_buffer_header (b[9], LOAD);
179           vlib_prefetch_buffer_header (b[10], LOAD);
180           vlib_prefetch_buffer_header (b[11], LOAD);
181         }
182
183       vlib_buffer_copy_template (b[0], &bt);
184       vlib_buffer_copy_template (b[1], &bt);
185       vlib_buffer_copy_template (b[2], &bt);
186       vlib_buffer_copy_template (b[3], &bt);
187
188       n_rx_bytes += b[0]->current_length = qw1[0] >> AVF_RXD_LEN_SHIFT;
189       n_rx_bytes += b[1]->current_length = qw1[1] >> AVF_RXD_LEN_SHIFT;
190       n_rx_bytes += b[2]->current_length = qw1[2] >> AVF_RXD_LEN_SHIFT;
191       n_rx_bytes += b[3]->current_length = qw1[3] >> AVF_RXD_LEN_SHIFT;
192
193       if (maybe_multiseg)
194         {
195           n_rx_bytes += avf_rx_attach_tail (vm, &bt, b[0], qw1[0], tail + 0);
196           n_rx_bytes += avf_rx_attach_tail (vm, &bt, b[1], qw1[1], tail + 1);
197           n_rx_bytes += avf_rx_attach_tail (vm, &bt, b[2], qw1[2], tail + 2);
198           n_rx_bytes += avf_rx_attach_tail (vm, &bt, b[3], qw1[3], tail + 3);
199         }
200
201       VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[0]);
202       VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[1]);
203       VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[2]);
204       VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[3]);
205
206       /* next */
207       qw1 += 4;
208       tail += 4;
209       b += 4;
210       n_left -= 4;
211     }
212   while (n_left)
213     {
214       vlib_buffer_copy_template (b[0], &bt);
215
216       n_rx_bytes += b[0]->current_length = qw1[0] >> AVF_RXD_LEN_SHIFT;
217
218       if (maybe_multiseg)
219         n_rx_bytes += avf_rx_attach_tail (vm, &bt, b[0], qw1[0], tail + 0);
220
221       VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[0]);
222
223       /* next */
224       qw1 += 1;
225       tail += 1;
226       b += 1;
227       n_left -= 1;
228     }
229   return n_rx_bytes;
230 }
231
232 static_always_inline uword
233 avf_device_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
234                          vlib_frame_t * frame, avf_device_t * ad, u16 qid)
235 {
236   avf_main_t *am = &avf_main;
237   vnet_main_t *vnm = vnet_get_main ();
238   u32 thr_idx = vlib_get_thread_index ();
239   avf_per_thread_data_t *ptd =
240     vec_elt_at_index (am->per_thread_data, thr_idx);
241   avf_rxq_t *rxq = vec_elt_at_index (ad->rxqs, qid);
242   u32 n_trace, n_rx_packets = 0, n_rx_bytes = 0;
243   u16 n_tail_desc = 0;
244   u64 or_qw1 = 0;
245   u32 *bi, *to_next, n_left_to_next;
246   vlib_buffer_t *bt = &ptd->buffer_template;
247   u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
248   u16 next = rxq->next;
249   u16 size = rxq->size;
250   u16 mask = size - 1;
251   avf_rx_desc_t *d, *fd = rxq->descs;
252 #ifdef CLIB_HAVE_VEC256
253   u64x4 q1x4, or_q1x4 = { 0 };
254   u64x4 dd_eop_mask4 = u64x4_splat (AVF_RXD_STATUS_DD | AVF_RXD_STATUS_EOP);
255 #endif
256
257   /* is there anything on the ring */
258   d = fd + next;
259   if ((d->qword[1] & AVF_RXD_STATUS_DD) == 0)
260     goto done;
261
262   if (PREDICT_FALSE (ad->per_interface_next_index != ~0))
263     next_index = ad->per_interface_next_index;
264   vlib_get_new_next_frame (vm, node, next_index, to_next, n_left_to_next);
265
266   /* fetch up to AVF_RX_VECTOR_SZ from the rx ring, unflatten them and
267      copy needed data from descriptor to rx vector */
268   bi = to_next;
269
270   while (n_rx_packets < AVF_RX_VECTOR_SZ)
271     {
272       if (next + 11 < size)
273         {
274           int stride = 8;
275           CLIB_PREFETCH ((void *) (fd + (next + stride)),
276                          CLIB_CACHE_LINE_BYTES, LOAD);
277           CLIB_PREFETCH ((void *) (fd + (next + stride + 1)),
278                          CLIB_CACHE_LINE_BYTES, LOAD);
279           CLIB_PREFETCH ((void *) (fd + (next + stride + 2)),
280                          CLIB_CACHE_LINE_BYTES, LOAD);
281           CLIB_PREFETCH ((void *) (fd + (next + stride + 3)),
282                          CLIB_CACHE_LINE_BYTES, LOAD);
283         }
284
285 #ifdef CLIB_HAVE_VEC256
286       if (n_rx_packets >= AVF_RX_VECTOR_SZ - 4 || next >= size - 4)
287         goto one_by_one;
288
289       q1x4 = u64x4_gather ((void *) &d[0].qword[1], (void *) &d[1].qword[1],
290                            (void *) &d[2].qword[1], (void *) &d[3].qword[1]);
291
292       /* not all packets are ready or at least one of them is chained */
293       if (!u64x4_is_equal (q1x4 & dd_eop_mask4, dd_eop_mask4))
294         goto one_by_one;
295
296       or_q1x4 |= q1x4;
297       u64x4_store_unaligned (q1x4, ptd->qw1s + n_rx_packets);
298       clib_memcpy_fast (bi, rxq->bufs + next, 4 * sizeof (u32));
299
300       /* next */
301       next = (next + 4) & mask;
302       d = fd + next;
303       n_rx_packets += 4;
304       bi += 4;
305       continue;
306     one_by_one:
307 #endif
308       CLIB_PREFETCH ((void *) (fd + ((next + 8) & mask)),
309                      CLIB_CACHE_LINE_BYTES, LOAD);
310
311       if (avf_rxd_is_not_dd (d))
312         break;
313
314       bi[0] = rxq->bufs[next];
315
316       /* deal with chained buffers */
317       if (PREDICT_FALSE (avf_rxd_is_not_eop (d)))
318         {
319           u16 tail_desc = 0;
320           u16 tail_next = next;
321           avf_rx_tail_t *tail = ptd->tails + n_rx_packets;
322           avf_rx_desc_t *td;
323           do
324             {
325               tail_next = (tail_next + 1) & mask;
326               td = fd + tail_next;
327
328               /* bail out in case of incomplete transaction */
329               if (avf_rxd_is_not_dd (td))
330                 goto no_more_desc;
331
332               or_qw1 |= tail->qw1s[tail_desc] = td[0].qword[1];
333               tail->buffers[tail_desc] = rxq->bufs[tail_next];
334               tail_desc++;
335             }
336           while (avf_rxd_is_not_eop (td));
337           next = tail_next;
338           n_tail_desc += tail_desc;
339         }
340
341       or_qw1 |= ptd->qw1s[n_rx_packets] = d[0].qword[1];
342
343       /* next */
344       next = (next + 1) & mask;
345       d = fd + next;
346       n_rx_packets++;
347       bi++;
348     }
349 no_more_desc:
350
351   if (n_rx_packets == 0)
352     goto done;
353
354   rxq->next = next;
355   rxq->n_enqueued -= n_rx_packets + n_tail_desc;
356
357 #ifdef CLIB_HAVE_VEC256
358   or_qw1 |= or_q1x4[0] | or_q1x4[1] | or_q1x4[2] | or_q1x4[3];
359 #endif
360
361   /* refill rx ring */
362   if (ad->flags & AVF_DEVICE_F_VA_DMA)
363     avf_rxq_refill (vm, node, rxq, 1 /* use_va_dma */ );
364   else
365     avf_rxq_refill (vm, node, rxq, 0 /* use_va_dma */ );
366
367   vlib_get_buffers (vm, to_next, ptd->bufs, n_rx_packets);
368
369   vnet_buffer (bt)->sw_if_index[VLIB_RX] = ad->sw_if_index;
370   vnet_buffer (bt)->sw_if_index[VLIB_TX] = ~0;
371
372   if (n_tail_desc)
373     n_rx_bytes = avf_process_rx_burst (vm, node, ptd, n_rx_packets, 1);
374   else
375     n_rx_bytes = avf_process_rx_burst (vm, node, ptd, n_rx_packets, 0);
376
377   /* packet trace if enabled */
378   if (PREDICT_FALSE ((n_trace = vlib_get_trace_count (vm, node))))
379     {
380       u32 n_left = n_rx_packets, i = 0, j;
381       bi = to_next;
382
383       while (n_trace && n_left)
384         {
385           vlib_buffer_t *b;
386           avf_input_trace_t *tr;
387           b = vlib_get_buffer (vm, bi[0]);
388           vlib_trace_buffer (vm, node, next_index, b, /* follow_chain */ 0);
389           tr = vlib_add_trace (vm, node, b, sizeof (*tr));
390           tr->next_index = next_index;
391           tr->hw_if_index = ad->hw_if_index;
392           tr->qw1s[0] = ptd->qw1s[i];
393           for (j = 1; j < AVF_RX_MAX_DESC_IN_CHAIN; j++)
394             tr->qw1s[j] = ptd->tails[i].qw1s[j - 1];
395
396           /* next */
397           n_trace--;
398           n_left--;
399           bi++;
400           i++;
401         }
402       vlib_set_trace_count (vm, node, n_trace);
403     }
404
405   if (PREDICT_TRUE (next_index == VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT))
406     {
407       vlib_next_frame_t *nf;
408       vlib_frame_t *f;
409       ethernet_input_frame_t *ef;
410       nf = vlib_node_runtime_get_next_frame (vm, node, next_index);
411       f = vlib_get_frame (vm, nf->frame_index);
412       f->flags = ETH_INPUT_FRAME_F_SINGLE_SW_IF_IDX;
413
414       ef = vlib_frame_scalar_args (f);
415       ef->sw_if_index = ad->sw_if_index;
416       ef->hw_if_index = ad->hw_if_index;
417
418       if ((or_qw1 & AVF_RXD_ERROR_IPE) == 0)
419         f->flags |= ETH_INPUT_FRAME_F_IP4_CKSUM_OK;
420     }
421
422   n_left_to_next -= n_rx_packets;
423   vlib_put_next_frame (vm, node, next_index, n_left_to_next);
424
425   vlib_increment_combined_counter (vnm->interface_main.combined_sw_if_counters
426                                    + VNET_INTERFACE_COUNTER_RX, thr_idx,
427                                    ad->hw_if_index, n_rx_packets, n_rx_bytes);
428
429 done:
430   return n_rx_packets;
431 }
432
433 VLIB_NODE_FN (avf_input_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
434                                vlib_frame_t * frame)
435 {
436   u32 n_rx = 0;
437   avf_main_t *am = &avf_main;
438   vnet_device_input_runtime_t *rt = (void *) node->runtime_data;
439   vnet_device_and_queue_t *dq;
440
441   foreach_device_and_queue (dq, rt->devices_and_queues)
442   {
443     avf_device_t *ad;
444     ad = vec_elt_at_index (am->devices, dq->dev_instance);
445     if ((ad->flags & AVF_DEVICE_F_ADMIN_UP) == 0)
446       continue;
447     n_rx += avf_device_input_inline (vm, node, frame, ad, dq->queue_id);
448   }
449   return n_rx;
450 }
451
452 /* *INDENT-OFF* */
453 VLIB_REGISTER_NODE (avf_input_node) = {
454   .name = "avf-input",
455   .sibling_of = "device-input",
456   .format_trace = format_avf_input_trace,
457   .type = VLIB_NODE_TYPE_INPUT,
458   .state = VLIB_NODE_STATE_DISABLED,
459   .n_errors = AVF_INPUT_N_ERROR,
460   .error_strings = avf_input_error_strings,
461 };
462
463 /* *INDENT-ON* */
464
465
466 /*
467  * fd.io coding-style-patch-verification: ON
468  *
469  * Local Variables:
470  * eval: (c-set-style "gnu")
471  * End:
472  */