avf: optimize rx ring refill
[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
50 static_always_inline void
51 avf_rx_desc_write (avf_rx_desc_t * d, u64 addr)
52 {
53 #ifdef CLIB_HAVE_VEC256
54   u64x4 v = { addr, 0, 0, 0 };
55   u64x4_store_unaligned (v, (void *) d);
56 #else
57   d->qword[0] = addr;
58   d->qword[1] = 0;
59 #endif
60 }
61
62 static_always_inline void
63 avf_rxq_refill (vlib_main_t * vm, vlib_node_runtime_t * node, avf_rxq_t * rxq,
64                 int use_va_dma)
65 {
66   u16 n_refill, mask, n_alloc, slot, size;
67   vlib_buffer_t *b[8];
68   avf_rx_desc_t *d, *first_d;
69   void *p[8];
70
71   size = rxq->size;
72   mask = size - 1;
73   n_refill = mask - rxq->n_enqueued;
74   if (PREDICT_TRUE (n_refill <= AVF_INPUT_REFILL_TRESHOLD))
75     return;
76
77   slot = (rxq->next - n_refill - 1) & mask;
78
79   n_refill &= ~7;               /* round to 8 */
80   n_alloc = vlib_buffer_alloc_to_ring (vm, rxq->bufs, slot, size, n_refill);
81
82   if (PREDICT_FALSE (n_alloc != n_refill))
83     {
84       vlib_error_count (vm, node->node_index,
85                         AVF_INPUT_ERROR_BUFFER_ALLOC, 1);
86       if (n_alloc)
87         vlib_buffer_free_from_ring (vm, rxq->bufs, slot, size, n_alloc);
88       return;
89     }
90
91   rxq->n_enqueued += n_alloc;
92   first_d = rxq->descs;
93
94   ASSERT (slot % 8 == 0);
95
96   while (n_alloc >= 8)
97     {
98       d = first_d + slot;
99
100       if (use_va_dma)
101         {
102           vlib_get_buffers_with_offset (vm, rxq->bufs + slot, p, 8,
103                                         sizeof (vlib_buffer_t));
104           avf_rx_desc_write (d + 0, pointer_to_uword (p[0]));
105           avf_rx_desc_write (d + 1, pointer_to_uword (p[1]));
106           avf_rx_desc_write (d + 2, pointer_to_uword (p[2]));
107           avf_rx_desc_write (d + 3, pointer_to_uword (p[3]));
108           avf_rx_desc_write (d + 4, pointer_to_uword (p[4]));
109           avf_rx_desc_write (d + 5, pointer_to_uword (p[5]));
110           avf_rx_desc_write (d + 6, pointer_to_uword (p[6]));
111           avf_rx_desc_write (d + 7, pointer_to_uword (p[7]));
112         }
113       else
114         {
115           vlib_get_buffers (vm, rxq->bufs + slot, b, 8);
116           avf_rx_desc_write (d + 0, vlib_buffer_get_pa (vm, b[0]));
117           avf_rx_desc_write (d + 1, vlib_buffer_get_pa (vm, b[1]));
118           avf_rx_desc_write (d + 2, vlib_buffer_get_pa (vm, b[2]));
119           avf_rx_desc_write (d + 3, vlib_buffer_get_pa (vm, b[3]));
120           avf_rx_desc_write (d + 4, vlib_buffer_get_pa (vm, b[4]));
121           avf_rx_desc_write (d + 5, vlib_buffer_get_pa (vm, b[5]));
122           avf_rx_desc_write (d + 6, vlib_buffer_get_pa (vm, b[6]));
123           avf_rx_desc_write (d + 7, vlib_buffer_get_pa (vm, b[7]));
124         }
125
126       /* next */
127       slot = (slot + 8) & mask;
128       n_alloc -= 8;
129     }
130
131   CLIB_MEMORY_STORE_BARRIER ();
132   *(rxq->qrx_tail) = slot;
133 }
134
135 static_always_inline uword
136 avf_process_rx_burst (vlib_main_t * vm, vlib_node_runtime_t * node,
137                       vlib_buffer_t * bt, avf_rx_vector_entry_t * rxve,
138                       vlib_buffer_t ** b, u32 n_rxv)
139 {
140   uword n_rx_bytes = 0;
141
142   while (n_rxv >= 4)
143     {
144       if (n_rxv >= 12)
145         {
146           vlib_prefetch_buffer_header (b[8], LOAD);
147           vlib_prefetch_buffer_header (b[9], LOAD);
148           vlib_prefetch_buffer_header (b[10], LOAD);
149           vlib_prefetch_buffer_header (b[11], LOAD);
150         }
151
152       n_rx_bytes += b[0]->current_length = rxve[0].length;
153       n_rx_bytes += b[1]->current_length = rxve[1].length;
154       n_rx_bytes += b[2]->current_length = rxve[2].length;
155       n_rx_bytes += b[3]->current_length = rxve[3].length;
156
157       clib_memcpy_fast (vnet_buffer (b[0])->sw_if_index,
158                         vnet_buffer (bt)->sw_if_index, 2 * sizeof (u32));
159       clib_memcpy_fast (vnet_buffer (b[1])->sw_if_index,
160                         vnet_buffer (bt)->sw_if_index, 2 * sizeof (u32));
161       clib_memcpy_fast (vnet_buffer (b[2])->sw_if_index,
162                         vnet_buffer (bt)->sw_if_index, 2 * sizeof (u32));
163       clib_memcpy_fast (vnet_buffer (b[3])->sw_if_index,
164                         vnet_buffer (bt)->sw_if_index, 2 * sizeof (u32));
165
166       VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[0]);
167       VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[1]);
168       VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[2]);
169       VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[3]);
170
171       /* next */
172       rxve += 4;
173       b += 4;
174       n_rxv -= 4;
175     }
176   while (n_rxv)
177     {
178       b[0]->current_length = rxve->length;
179       n_rx_bytes += b[0]->current_length;
180
181       clib_memcpy_fast (vnet_buffer (b[0])->sw_if_index,
182                         vnet_buffer (bt)->sw_if_index, 2 * sizeof (u32));
183
184       VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[0]);
185
186       /* next */
187       rxve += 1;
188       b += 1;
189       n_rxv -= 1;
190     }
191   return n_rx_bytes;
192 }
193
194 static_always_inline uword
195 avf_device_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
196                          vlib_frame_t * frame, avf_device_t * ad, u16 qid)
197 {
198   avf_main_t *am = &avf_main;
199   vnet_main_t *vnm = vnet_get_main ();
200   u32 thr_idx = vlib_get_thread_index ();
201   avf_per_thread_data_t *ptd =
202     vec_elt_at_index (am->per_thread_data, thr_idx);
203   avf_rxq_t *rxq = vec_elt_at_index (ad->rxqs, qid);
204   avf_rx_vector_entry_t *rxve = 0;
205   uword n_trace;
206   avf_rx_desc_t *d;
207   u32 n_rx_packets = 0, n_rx_bytes = 0;
208   u16 mask = rxq->size - 1;
209   u16 n_rxv = 0;
210   u8 or_error = 0;
211   u32 *bi, *to_next, n_left_to_next;
212   vlib_buffer_t *bufs[AVF_RX_VECTOR_SZ];
213   vlib_buffer_t *bt = &ptd->buffer_template;
214   u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
215
216   STATIC_ASSERT_SIZEOF (avf_rx_vector_entry_t, 8);
217   STATIC_ASSERT_OFFSET_OF (avf_rx_vector_entry_t, status, 0);
218   STATIC_ASSERT_OFFSET_OF (avf_rx_vector_entry_t, length, 4);
219   STATIC_ASSERT_OFFSET_OF (avf_rx_vector_entry_t, ptype, 6);
220   STATIC_ASSERT_OFFSET_OF (avf_rx_vector_entry_t, error, 7);
221
222   /* is there anything on the ring */
223   d = rxq->descs + rxq->next;
224   if ((d->qword[1] & AVF_RX_DESC_STATUS_DD) == 0)
225     goto done;
226
227   if (PREDICT_FALSE (ad->per_interface_next_index != ~0))
228     next_index = ad->per_interface_next_index;
229   vlib_get_new_next_frame (vm, node, next_index, to_next, n_left_to_next);
230
231   /* fetch up to AVF_RX_VECTOR_SZ from the rx ring, unflatten them and
232      copy needed data from descriptor to rx vector */
233   bi = to_next;
234   while (n_rxv < AVF_RX_VECTOR_SZ)
235     {
236       if (rxq->next + 11 < rxq->size)
237         {
238           int stride = 8;
239           CLIB_PREFETCH ((void *) (rxq->descs + (rxq->next + stride)),
240                          CLIB_CACHE_LINE_BYTES, LOAD);
241           CLIB_PREFETCH ((void *) (rxq->descs + (rxq->next + stride + 1)),
242                          CLIB_CACHE_LINE_BYTES, LOAD);
243           CLIB_PREFETCH ((void *) (rxq->descs + (rxq->next + stride + 2)),
244                          CLIB_CACHE_LINE_BYTES, LOAD);
245           CLIB_PREFETCH ((void *) (rxq->descs + (rxq->next + stride + 3)),
246                          CLIB_CACHE_LINE_BYTES, LOAD);
247         }
248
249 #ifdef CLIB_HAVE_VEC256
250       u64x4 q1x4, v, err4;
251       u64x4 status_dd_eop_mask = u64x4_splat (0x3);
252
253       if (n_rxv >= AVF_RX_VECTOR_SZ - 4)
254         goto one_by_one;
255
256       if (rxq->next >= rxq->size - 4)
257         goto one_by_one;
258
259       q1x4 = u64x4_gather ((void *) &d[0].qword[1], (void *) &d[1].qword[1],
260                            (void *) &d[2].qword[1], (void *) &d[3].qword[1]);
261
262       /* not all packets are ready or at least one of them is chained */
263       if (!u64x4_is_equal (q1x4 & status_dd_eop_mask, status_dd_eop_mask))
264         goto one_by_one;
265
266       /* shift and mask status, length, ptype and err */
267       v = q1x4 & u64x4_splat ((u64) 0x3FFFFULL);
268       v |= (q1x4 >> 6) & u64x4_splat ((u64) 0xFFFF << 32);
269       v |= (q1x4 << 18) & u64x4_splat ((u64) 0xFF << 48);
270       v |= err4 = (q1x4 << 37) & u64x4_splat ((u64) 0xFF << 56);
271
272       u64x4_store_unaligned (v, ptd->rx_vector + n_rxv);
273
274       if (!u64x4_is_all_zero (err4))
275         or_error |= err4[0] | err4[1] | err4[2] | err4[3];
276
277       clib_memcpy_fast (bi, rxq->bufs + rxq->next, 4 * sizeof (u32));
278
279       /* next */
280       rxq->next = (rxq->next + 4) & mask;
281       d = rxq->descs + rxq->next;
282       n_rxv += 4;
283       rxq->n_enqueued -= 4;
284       bi += 4;
285       continue;
286     one_by_one:
287 #endif
288       CLIB_PREFETCH ((void *) (rxq->descs + ((rxq->next + 8) & mask)),
289                      CLIB_CACHE_LINE_BYTES, LOAD);
290       if ((d->qword[1] & AVF_RX_DESC_STATUS_DD) == 0)
291         break;
292       rxve = ptd->rx_vector + n_rxv;
293       bi[0] = rxq->bufs[rxq->next];
294       rxve->status = avf_get_u64_bits ((void *) d, 8, 18, 0);
295       rxve->error = avf_get_u64_bits ((void *) d, 8, 26, 19);
296       rxve->ptype = avf_get_u64_bits ((void *) d, 8, 37, 30);
297       rxve->length = avf_get_u64_bits ((void *) d, 8, 63, 38);
298       or_error |= rxve->error;
299
300       /* deal with chained buffers */
301       while (PREDICT_FALSE ((d->qword[1] & AVF_RX_DESC_STATUS_EOP) == 0))
302         {
303           clib_error ("fixme");
304         }
305
306       /* next */
307       rxq->next = (rxq->next + 1) & mask;
308       d = rxq->descs + rxq->next;
309       n_rxv++;
310       rxq->n_enqueued--;
311       bi++;
312     }
313
314   if (n_rxv == 0)
315     goto done;
316
317   /* refill rx ring */
318   if (ad->flags & AVF_DEVICE_F_VA_DMA)
319     avf_rxq_refill (vm, node, rxq, 1 /* use_va_dma */ );
320   else
321     avf_rxq_refill (vm, node, rxq, 0 /* use_va_dma */ );
322
323   vlib_get_buffers (vm, to_next, bufs, n_rxv);
324   n_rx_packets = n_rxv;
325
326   vnet_buffer (bt)->sw_if_index[VLIB_RX] = ad->sw_if_index;
327   vnet_buffer (bt)->sw_if_index[VLIB_TX] = ~0;
328
329   n_rx_bytes = avf_process_rx_burst (vm, node, bt, ptd->rx_vector, bufs,
330                                      n_rxv);
331
332   /* packet trace if enabled */
333   if (PREDICT_FALSE ((n_trace = vlib_get_trace_count (vm, node))))
334     {
335       u32 n_left = n_rx_packets;
336       bi = to_next;
337       while (n_trace && n_left)
338         {
339           vlib_buffer_t *b;
340           avf_input_trace_t *tr;
341           b = vlib_get_buffer (vm, bi[0]);
342           vlib_trace_buffer (vm, node, next_index, b, /* follow_chain */ 0);
343           tr = vlib_add_trace (vm, node, b, sizeof (*tr));
344           tr->next_index = next_index;
345           tr->hw_if_index = ad->hw_if_index;
346           clib_memcpy_fast (&tr->rxve, rxve, sizeof (avf_rx_vector_entry_t));
347
348           /* next */
349           n_trace--;
350           n_left--;
351           bi++;
352         }
353       vlib_set_trace_count (vm, node, n_trace);
354     }
355
356   if (PREDICT_TRUE (next_index == VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT))
357     {
358       vlib_next_frame_t *nf;
359       vlib_frame_t *f;
360       ethernet_input_frame_t *ef;
361       nf = vlib_node_runtime_get_next_frame (vm, node, next_index);
362       f = vlib_get_frame (vm, nf->frame_index);
363       f->flags = ETH_INPUT_FRAME_F_SINGLE_SW_IF_IDX;
364
365       ef = vlib_frame_scalar_args (f);
366       ef->sw_if_index = ad->sw_if_index;
367       ef->hw_if_index = ad->hw_if_index;
368
369       if ((or_error & (1 << 3)) == 0)
370         f->flags |= ETH_INPUT_FRAME_F_IP4_CKSUM_OK;
371     }
372   n_left_to_next -= n_rx_packets;
373   vlib_put_next_frame (vm, node, next_index, n_left_to_next);
374
375   vlib_increment_combined_counter (vnm->interface_main.combined_sw_if_counters
376                                    + VNET_INTERFACE_COUNTER_RX, thr_idx,
377                                    ad->hw_if_index, n_rx_packets, n_rx_bytes);
378
379 done:
380   return n_rx_packets;
381 }
382
383 VLIB_NODE_FN (avf_input_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
384                                vlib_frame_t * frame)
385 {
386   u32 n_rx = 0;
387   avf_main_t *am = &avf_main;
388   vnet_device_input_runtime_t *rt = (void *) node->runtime_data;
389   vnet_device_and_queue_t *dq;
390
391   foreach_device_and_queue (dq, rt->devices_and_queues)
392   {
393     avf_device_t *ad;
394     ad = vec_elt_at_index (am->devices, dq->dev_instance);
395     if ((ad->flags & AVF_DEVICE_F_ADMIN_UP) == 0)
396       continue;
397     n_rx += avf_device_input_inline (vm, node, frame, ad, dq->queue_id);
398   }
399   return n_rx;
400 }
401
402 /* *INDENT-OFF* */
403 VLIB_REGISTER_NODE (avf_input_node) = {
404   .name = "avf-input",
405   .sibling_of = "device-input",
406   .format_trace = format_avf_input_trace,
407   .type = VLIB_NODE_TYPE_INPUT,
408   .state = VLIB_NODE_STATE_DISABLED,
409   .n_errors = AVF_INPUT_N_ERROR,
410   .error_strings = avf_input_error_strings,
411 };
412
413 /* *INDENT-ON* */
414
415
416 /*
417  * fd.io coding-style-patch-verification: ON
418  *
419  * Local Variables:
420  * eval: (c-set-style "gnu")
421  * End:
422  */