Remove c-11 memcpy checks from perf-critical code
[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   _(RX_PACKET_ERROR, "Rx packet errors")
29
30 typedef enum
31 {
32 #define _(f,s) AVF_INPUT_ERROR_##f,
33   foreach_avf_input_error
34 #undef _
35     AVF_INPUT_N_ERROR,
36 } avf_input_error_t;
37
38 static __clib_unused char *avf_input_error_strings[] = {
39 #define _(n,s) s,
40   foreach_avf_input_error
41 #undef _
42 };
43
44 #define AVF_RX_DESC_STATUS(x)           (1 << x)
45 #define AVF_RX_DESC_STATUS_DD           AVF_RX_DESC_STATUS(0)
46 #define AVF_RX_DESC_STATUS_EOP          AVF_RX_DESC_STATUS(1)
47
48 #define AVF_INPUT_REFILL_TRESHOLD 32
49 static_always_inline void
50 avf_rxq_refill (vlib_main_t * vm, vlib_node_runtime_t * node, avf_rxq_t * rxq,
51                 int use_va_dma)
52 {
53   u16 n_refill, mask, n_alloc, slot;
54   u32 s0, s1, s2, s3;
55   vlib_buffer_t *b[4];
56   avf_rx_desc_t *d[4];
57
58   n_refill = rxq->size - 1 - rxq->n_enqueued;
59   if (PREDICT_TRUE (n_refill <= AVF_INPUT_REFILL_TRESHOLD))
60     return;
61
62   mask = rxq->size - 1;
63   slot = (rxq->next - n_refill - 1) & mask;
64
65   n_refill &= ~7;               /* round to 8 */
66   n_alloc = vlib_buffer_alloc_to_ring (vm, rxq->bufs, slot, rxq->size,
67                                        n_refill);
68
69   if (PREDICT_FALSE (n_alloc != n_refill))
70     {
71       vlib_error_count (vm, node->node_index,
72                         AVF_INPUT_ERROR_BUFFER_ALLOC, 1);
73       if (n_alloc)
74         vlib_buffer_free_from_ring (vm, rxq->bufs, slot, rxq->size, n_alloc);
75       return;
76     }
77
78   rxq->n_enqueued += n_alloc;
79
80   while (n_alloc >= 4)
81     {
82       if (PREDICT_TRUE (slot + 3 < rxq->size))
83         {
84           s0 = slot;
85           s1 = slot + 1;
86           s2 = slot + 2;
87           s3 = slot + 3;
88         }
89       else
90         {
91           s0 = slot;
92           s1 = (slot + 1) & mask;
93           s2 = (slot + 2) & mask;
94           s3 = (slot + 3) & mask;
95         }
96
97       d[0] = ((avf_rx_desc_t *) rxq->descs) + s0;
98       d[1] = ((avf_rx_desc_t *) rxq->descs) + s1;
99       d[2] = ((avf_rx_desc_t *) rxq->descs) + s2;
100       d[3] = ((avf_rx_desc_t *) rxq->descs) + s3;
101       b[0] = vlib_get_buffer (vm, rxq->bufs[s0]);
102       b[1] = vlib_get_buffer (vm, rxq->bufs[s1]);
103       b[2] = vlib_get_buffer (vm, rxq->bufs[s2]);
104       b[3] = vlib_get_buffer (vm, rxq->bufs[s3]);
105
106       if (use_va_dma)
107         {
108           d[0]->qword[0] = vlib_buffer_get_va (b[0]);
109           d[1]->qword[0] = vlib_buffer_get_va (b[1]);
110           d[2]->qword[0] = vlib_buffer_get_va (b[2]);
111           d[3]->qword[0] = vlib_buffer_get_va (b[3]);
112         }
113       else
114         {
115           d[0]->qword[0] = vlib_buffer_get_pa (vm, b[0]);
116           d[1]->qword[0] = vlib_buffer_get_pa (vm, b[1]);
117           d[2]->qword[0] = vlib_buffer_get_pa (vm, b[2]);
118           d[3]->qword[0] = vlib_buffer_get_pa (vm, b[3]);
119         }
120
121       d[0]->qword[1] = 0;
122       d[1]->qword[1] = 0;
123       d[2]->qword[1] = 0;
124       d[3]->qword[1] = 0;
125
126       /* next */
127       slot = (slot + 4) & mask;
128       n_alloc -= 4;
129     }
130   while (n_alloc)
131     {
132       s0 = slot;
133       d[0] = ((avf_rx_desc_t *) rxq->descs) + s0;
134       b[0] = vlib_get_buffer (vm, rxq->bufs[s0]);
135       if (use_va_dma)
136         d[0]->qword[0] = vlib_buffer_get_va (b[0]);
137       else
138         d[0]->qword[0] = vlib_buffer_get_pa (vm, b[0]);
139       d[0]->qword[1] = 0;
140
141       /* next */
142       slot = (slot + 1) & mask;
143       n_alloc -= 1;
144     }
145
146   CLIB_MEMORY_BARRIER ();
147   *(rxq->qrx_tail) = slot;
148 }
149
150 static_always_inline void
151 avf_check_for_error (vlib_node_runtime_t * node, avf_rx_vector_entry_t * rxve,
152                      vlib_buffer_t * b, u16 * next)
153 {
154   avf_main_t *am = &avf_main;
155   avf_ptype_t *ptype;
156   if (PREDICT_FALSE (rxve->error))
157     {
158       b->error = node->errors[AVF_INPUT_ERROR_RX_PACKET_ERROR];
159       ptype = am->ptypes + rxve->ptype;
160       /* retract */
161       vlib_buffer_advance (b, --ptype->buffer_advance);
162       *next = VNET_DEVICE_INPUT_NEXT_DROP;
163     }
164 }
165
166 static_always_inline u32
167 avf_find_next (avf_rx_vector_entry_t * rxve, vlib_buffer_t * b,
168                int maybe_tagged)
169 {
170   avf_main_t *am = &avf_main;
171   ethernet_header_t *e = (ethernet_header_t *) b->data;
172   avf_ptype_t *ptype;
173   if (maybe_tagged && ethernet_frame_is_tagged (e->type))
174     return VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
175   ptype = am->ptypes + rxve->ptype;
176   vlib_buffer_advance (b, ptype->buffer_advance);
177   b->flags |= ptype->flags;
178   return ptype->next_node;
179 }
180
181
182 static_always_inline uword
183 avf_process_rx_burst (vlib_main_t * vm, vlib_node_runtime_t * node,
184                       vlib_buffer_t * bt, avf_rx_vector_entry_t * rxve,
185                       vlib_buffer_t ** b, u16 * next, u32 n_rxv,
186                       u8 maybe_error, int known_next)
187 {
188   uword n_rx_bytes = 0;
189
190   while (n_rxv >= 4)
191     {
192       if (n_rxv >= 12)
193         {
194           vlib_prefetch_buffer_header (b[8], LOAD);
195           vlib_prefetch_buffer_header (b[9], LOAD);
196           vlib_prefetch_buffer_header (b[10], LOAD);
197           vlib_prefetch_buffer_header (b[11], LOAD);
198           if (!known_next)
199             {
200               CLIB_PREFETCH (b[8]->data, CLIB_CACHE_LINE_BYTES, LOAD);
201               CLIB_PREFETCH (b[9]->data, CLIB_CACHE_LINE_BYTES, LOAD);
202               CLIB_PREFETCH (b[10]->data, CLIB_CACHE_LINE_BYTES, LOAD);
203               CLIB_PREFETCH (b[11]->data, CLIB_CACHE_LINE_BYTES, LOAD);
204             }
205         }
206
207       n_rx_bytes += b[0]->current_length = rxve[0].length;
208       n_rx_bytes += b[1]->current_length = rxve[1].length;
209       n_rx_bytes += b[2]->current_length = rxve[2].length;
210       n_rx_bytes += b[3]->current_length = rxve[3].length;
211
212       if (!known_next)
213         {
214           ethernet_header_t *e0, *e1, *e2, *e3;
215
216           e0 = (ethernet_header_t *) b[0]->data;
217           e1 = (ethernet_header_t *) b[1]->data;
218           e2 = (ethernet_header_t *) b[2]->data;
219           e3 = (ethernet_header_t *) b[3]->data;
220
221           if (ethernet_frame_is_any_tagged_x4 (e0->type, e1->type,
222                                                e2->type, e3->type))
223             {
224               next[0] = avf_find_next (rxve, b[0], 1);
225               next[1] = avf_find_next (rxve + 1, b[1], 1);
226               next[2] = avf_find_next (rxve + 2, b[2], 1);
227               next[3] = avf_find_next (rxve + 3, b[3], 1);
228             }
229           else
230             {
231               next[0] = avf_find_next (rxve, b[0], 0);
232               next[1] = avf_find_next (rxve + 1, b[1], 0);
233               next[2] = avf_find_next (rxve + 2, b[2], 0);
234               next[3] = avf_find_next (rxve + 3, b[3], 0);
235             }
236
237           if (PREDICT_FALSE (maybe_error))
238             {
239               avf_check_for_error (node, rxve + 0, b[0], next);
240               avf_check_for_error (node, rxve + 1, b[1], next + 1);
241               avf_check_for_error (node, rxve + 2, b[2], next + 2);
242               avf_check_for_error (node, rxve + 3, b[3], next + 3);
243             }
244         }
245       else if (bt->current_config_index)
246         {
247           b[0]->current_config_index = bt->current_config_index;
248           b[1]->current_config_index = bt->current_config_index;
249           b[2]->current_config_index = bt->current_config_index;
250           b[3]->current_config_index = bt->current_config_index;
251           vnet_buffer (b[0])->feature_arc_index =
252             vnet_buffer (bt)->feature_arc_index;
253           vnet_buffer (b[1])->feature_arc_index =
254             vnet_buffer (bt)->feature_arc_index;
255           vnet_buffer (b[2])->feature_arc_index =
256             vnet_buffer (bt)->feature_arc_index;
257           vnet_buffer (b[3])->feature_arc_index =
258             vnet_buffer (bt)->feature_arc_index;
259         }
260
261       clib_memcpy_fast (vnet_buffer (b[0])->sw_if_index,
262                         vnet_buffer (bt)->sw_if_index, 2 * sizeof (u32));
263       clib_memcpy_fast (vnet_buffer (b[1])->sw_if_index,
264                         vnet_buffer (bt)->sw_if_index, 2 * sizeof (u32));
265       clib_memcpy_fast (vnet_buffer (b[2])->sw_if_index,
266                         vnet_buffer (bt)->sw_if_index, 2 * sizeof (u32));
267       clib_memcpy_fast (vnet_buffer (b[3])->sw_if_index,
268                         vnet_buffer (bt)->sw_if_index, 2 * sizeof (u32));
269
270       VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[0]);
271       VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[1]);
272       VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[2]);
273       VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[3]);
274
275       /* next */
276       rxve += 4;
277       b += 4;
278       next += 4;
279       n_rxv -= 4;
280     }
281   while (n_rxv)
282     {
283       b[0]->current_length = rxve->length;
284       n_rx_bytes += b[0]->current_length;
285
286       if (!known_next)
287         {
288           next[0] = avf_find_next (rxve, b[0], 1);
289           avf_check_for_error (node, rxve + 0, b[0], next);
290         }
291       else if (bt->current_config_index)
292         {
293           b[0]->current_config_index = bt->current_config_index;
294           vnet_buffer (b[0])->feature_arc_index =
295             vnet_buffer (bt)->feature_arc_index;
296         }
297
298       clib_memcpy_fast (vnet_buffer (b[0])->sw_if_index,
299                         vnet_buffer (bt)->sw_if_index, 2 * sizeof (u32));
300
301       VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[0]);
302
303       /* next */
304       rxve += 1;
305       b += 1;
306       next += 1;
307       n_rxv -= 1;
308
309     }
310   return n_rx_bytes;
311 }
312
313 static_always_inline uword
314 avf_device_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
315                          vlib_frame_t * frame, avf_device_t * ad, u16 qid)
316 {
317   avf_main_t *am = &avf_main;
318   vnet_main_t *vnm = vnet_get_main ();
319   u32 thr_idx = vlib_get_thread_index ();
320   avf_per_thread_data_t *ptd =
321     vec_elt_at_index (am->per_thread_data, thr_idx);
322   avf_rxq_t *rxq = vec_elt_at_index (ad->rxqs, qid);
323   avf_rx_vector_entry_t *rxve = 0;
324   uword n_trace;
325   avf_rx_desc_t *d;
326   u32 n_rx_packets = 0, n_rx_bytes = 0;
327   u16 mask = rxq->size - 1;
328   u16 n_rxv = 0;
329   u8 maybe_error = 0;
330   u32 buffer_indices[AVF_RX_VECTOR_SZ], *bi;
331   u16 nexts[AVF_RX_VECTOR_SZ], *next;
332   vlib_buffer_t *bufs[AVF_RX_VECTOR_SZ];
333   vlib_buffer_t *bt = &ptd->buffer_template;
334   int known_next = 0;
335   u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
336
337   STATIC_ASSERT_SIZEOF (avf_rx_vector_entry_t, 8);
338   STATIC_ASSERT_OFFSET_OF (avf_rx_vector_entry_t, status, 0);
339   STATIC_ASSERT_OFFSET_OF (avf_rx_vector_entry_t, length, 4);
340   STATIC_ASSERT_OFFSET_OF (avf_rx_vector_entry_t, ptype, 6);
341   STATIC_ASSERT_OFFSET_OF (avf_rx_vector_entry_t, error, 7);
342
343   /* fetch up to AVF_RX_VECTOR_SZ from the rx ring, unflatten them and
344      copy needed data from descriptor to rx vector */
345   d = rxq->descs + rxq->next;
346   bi = buffer_indices;
347   while (n_rxv < AVF_RX_VECTOR_SZ)
348     {
349       if (rxq->next + 11 < rxq->size)
350         {
351           int stride = 8;
352           CLIB_PREFETCH ((void *) (rxq->descs + (rxq->next + stride)),
353                          CLIB_CACHE_LINE_BYTES, LOAD);
354           CLIB_PREFETCH ((void *) (rxq->descs + (rxq->next + stride + 1)),
355                          CLIB_CACHE_LINE_BYTES, LOAD);
356           CLIB_PREFETCH ((void *) (rxq->descs + (rxq->next + stride + 2)),
357                          CLIB_CACHE_LINE_BYTES, LOAD);
358           CLIB_PREFETCH ((void *) (rxq->descs + (rxq->next + stride + 3)),
359                          CLIB_CACHE_LINE_BYTES, LOAD);
360         }
361
362 #ifdef CLIB_HAVE_VEC256
363       u64x4 q1x4, v, err4;
364       u64x4 status_dd_eop_mask = u64x4_splat (0x3);
365
366       if (n_rxv >= AVF_RX_VECTOR_SZ - 4)
367         goto one_by_one;
368
369       if (rxq->next >= rxq->size - 4)
370         goto one_by_one;
371
372       /* load 1st quadword of 4 dscriptors into 256-bit vector register */
373       /* *INDENT-OFF* */
374       q1x4 = (u64x4) {
375           d[0].qword[1],
376           d[1].qword[1],
377           d[2].qword[1],
378           d[3].qword[1]
379       };
380       /* *INDENT-ON* */
381
382       /* not all packets are ready or at least one of them is chained */
383       if (!u64x4_is_equal (q1x4 & status_dd_eop_mask, status_dd_eop_mask))
384         goto one_by_one;
385
386       /* shift and mask status, length, ptype and err */
387       v = q1x4 & u64x4_splat ((u64) 0x3FFFFULL);
388       v |= (q1x4 >> 6) & u64x4_splat ((u64) 0xFFFF << 32);
389       v |= (q1x4 << 18) & u64x4_splat ((u64) 0xFF << 48);
390       v |= err4 = (q1x4 << 37) & u64x4_splat ((u64) 0xFF << 56);
391
392       u64x4_store_unaligned (v, ptd->rx_vector + n_rxv);
393       maybe_error |= !u64x4_is_all_zero (err4);
394
395       clib_memcpy_fast (bi, rxq->bufs + rxq->next, 4 * sizeof (u32));
396
397       /* next */
398       rxq->next = (rxq->next + 4) & mask;
399       d = rxq->descs + rxq->next;
400       n_rxv += 4;
401       rxq->n_enqueued -= 4;
402       bi += 4;
403       continue;
404     one_by_one:
405 #endif
406       CLIB_PREFETCH ((void *) (rxq->descs + ((rxq->next + 8) & mask)),
407                      CLIB_CACHE_LINE_BYTES, LOAD);
408       if ((d->qword[1] & AVF_RX_DESC_STATUS_DD) == 0)
409         break;
410       rxve = ptd->rx_vector + n_rxv;
411       bi[0] = rxq->bufs[rxq->next];
412       rxve->status = avf_get_u64_bits ((void *) d, 8, 18, 0);
413       rxve->error = avf_get_u64_bits ((void *) d, 8, 26, 19);
414       rxve->ptype = avf_get_u64_bits ((void *) d, 8, 37, 30);
415       rxve->length = avf_get_u64_bits ((void *) d, 8, 63, 38);
416       maybe_error |= rxve->error;
417
418       /* deal with chained buffers */
419       while (PREDICT_FALSE ((d->qword[1] & AVF_RX_DESC_STATUS_EOP) == 0))
420         {
421           clib_error ("fixme");
422         }
423
424       /* next */
425       rxq->next = (rxq->next + 1) & mask;
426       d = rxq->descs + rxq->next;
427       n_rxv++;
428       rxq->n_enqueued--;
429       bi++;
430     }
431
432   if (n_rxv == 0)
433     goto done;
434
435   /* refill rx ring */
436   if (ad->flags & AVF_DEVICE_F_VA_DMA)
437     avf_rxq_refill (vm, node, rxq, 1 /* use_va_dma */ );
438   else
439     avf_rxq_refill (vm, node, rxq, 0 /* use_va_dma */ );
440
441   vlib_get_buffers (vm, buffer_indices, bufs, n_rxv);
442   n_rx_packets = n_rxv;
443
444   vnet_buffer (bt)->sw_if_index[VLIB_RX] = ad->sw_if_index;
445   vnet_buffer (bt)->sw_if_index[VLIB_TX] = ~0;
446
447   /* receive burst of packets from DPDK PMD */
448   if (PREDICT_FALSE (ad->per_interface_next_index != ~0))
449     {
450       known_next = 1;
451       next_index = ad->per_interface_next_index;
452     }
453
454   /* as all packets belong to thr same interface feature arc lookup
455      can be don once and result stored */
456   if (PREDICT_FALSE (vnet_device_input_have_features (ad->sw_if_index)))
457     {
458       vnet_feature_start_device_input_x1 (ad->sw_if_index, &next_index, bt);
459       known_next = 1;
460     }
461
462   if (known_next)
463     {
464       clib_memset_u16 (nexts, next_index, n_rxv);
465       n_rx_bytes = avf_process_rx_burst (vm, node, bt, ptd->rx_vector, bufs,
466                                          nexts, n_rxv, maybe_error, 1);
467       vnet_buffer (bt)->feature_arc_index = 0;
468       bt->current_config_index = 0;
469     }
470   else
471     n_rx_bytes = avf_process_rx_burst (vm, node, bt, ptd->rx_vector, bufs,
472                                        nexts, n_rxv, maybe_error, 0);
473
474   /* packet trace if enabled */
475   if (PREDICT_FALSE ((n_trace = vlib_get_trace_count (vm, node))))
476     {
477       u32 n_left = n_rx_packets;
478       bi = buffer_indices;
479       next = nexts;
480       while (n_trace && n_left)
481         {
482           vlib_buffer_t *b;
483           avf_input_trace_t *tr;
484           b = vlib_get_buffer (vm, bi[0]);
485           vlib_trace_buffer (vm, node, next[0], b, /* follow_chain */ 0);
486           tr = vlib_add_trace (vm, node, b, sizeof (*tr));
487           tr->next_index = next[0];
488           tr->hw_if_index = ad->hw_if_index;
489           clib_memcpy_fast (&tr->rxve, rxve, sizeof (avf_rx_vector_entry_t));
490
491           /* next */
492           n_trace--;
493           n_left--;
494           bi++;
495           next++;
496         }
497       vlib_set_trace_count (vm, node, n_trace);
498     }
499   vlib_buffer_enqueue_to_next (vm, node, buffer_indices, nexts, n_rx_packets);
500   vlib_increment_combined_counter (vnm->interface_main.combined_sw_if_counters
501                                    + VNET_INTERFACE_COUNTER_RX, thr_idx,
502                                    ad->hw_if_index, n_rx_packets, n_rx_bytes);
503
504 done:
505   return n_rx_packets;
506 }
507
508 VLIB_NODE_FN (avf_input_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
509                                vlib_frame_t * frame)
510 {
511   u32 n_rx = 0;
512   avf_main_t *am = &avf_main;
513   vnet_device_input_runtime_t *rt = (void *) node->runtime_data;
514   vnet_device_and_queue_t *dq;
515
516   foreach_device_and_queue (dq, rt->devices_and_queues)
517   {
518     avf_device_t *ad;
519     ad = vec_elt_at_index (am->devices, dq->dev_instance);
520     if ((ad->flags & AVF_DEVICE_F_ADMIN_UP) == 0)
521       continue;
522     n_rx += avf_device_input_inline (vm, node, frame, ad, dq->queue_id);
523   }
524   return n_rx;
525 }
526
527 /* *INDENT-OFF* */
528 VLIB_REGISTER_NODE (avf_input_node) = {
529   .name = "avf-input",
530   .sibling_of = "device-input",
531   .format_trace = format_avf_input_trace,
532   .type = VLIB_NODE_TYPE_INPUT,
533   .state = VLIB_NODE_STATE_DISABLED,
534   .n_errors = AVF_INPUT_N_ERROR,
535   .error_strings = avf_input_error_strings,
536 };
537
538 /* *INDENT-ON* */
539
540
541 /*
542  * fd.io coding-style-patch-verification: ON
543  *
544  * Local Variables:
545  * eval: (c-set-style "gnu")
546  * End:
547  */