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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
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>
26 #define foreach_avf_input_error \
27 _(BUFFER_ALLOC, "buffer alloc error") \
28 _(RX_PACKET_ERROR, "Rx packet errors")
32 #define _(f,s) AVF_INPUT_ERROR_##f,
33 foreach_avf_input_error
38 static __clib_unused char *avf_input_error_strings[] = {
40 foreach_avf_input_error
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)
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,
53 u16 n_refill, mask, n_alloc, slot;
58 n_refill = rxq->size - 1 - rxq->n_enqueued;
59 if (PREDICT_TRUE (n_refill <= AVF_INPUT_REFILL_TRESHOLD))
63 slot = (rxq->next - n_refill - 1) & mask;
65 n_refill &= ~7; /* round to 8 */
66 n_alloc = vlib_buffer_alloc_to_ring (vm, rxq->bufs, slot, rxq->size,
69 if (PREDICT_FALSE (n_alloc != n_refill))
71 vlib_error_count (vm, node->node_index,
72 AVF_INPUT_ERROR_BUFFER_ALLOC, 1);
74 vlib_buffer_free_from_ring (vm, rxq->bufs, slot, rxq->size, n_alloc);
78 rxq->n_enqueued += n_alloc;
82 if (PREDICT_TRUE (slot + 3 < rxq->size))
92 s1 = (slot + 1) & mask;
93 s2 = (slot + 2) & mask;
94 s3 = (slot + 3) & mask;
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]);
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]);
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]);
127 slot = (slot + 4) & mask;
133 d[0] = ((avf_rx_desc_t *) rxq->descs) + s0;
134 b[0] = vlib_get_buffer (vm, rxq->bufs[s0]);
136 d[0]->qword[0] = vlib_buffer_get_va (b[0]);
138 d[0]->qword[0] = vlib_buffer_get_pa (vm, b[0]);
142 slot = (slot + 1) & mask;
146 CLIB_MEMORY_BARRIER ();
147 *(rxq->qrx_tail) = slot;
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)
154 avf_main_t *am = &avf_main;
156 if (PREDICT_FALSE (rxve->error))
158 b->error = node->errors[AVF_INPUT_ERROR_RX_PACKET_ERROR];
159 ptype = am->ptypes + rxve->ptype;
161 vlib_buffer_advance (b, --ptype->buffer_advance);
162 *next = VNET_DEVICE_INPUT_NEXT_DROP;
166 static_always_inline u32
167 avf_find_next (avf_rx_vector_entry_t * rxve, vlib_buffer_t * b,
170 avf_main_t *am = &avf_main;
171 ethernet_header_t *e = (ethernet_header_t *) b->data;
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;
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)
188 uword n_rx_bytes = 0;
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);
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);
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;
214 ethernet_header_t *e0, *e1, *e2, *e3;
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;
221 if (ethernet_frame_is_any_tagged_x4 (e0->type, e1->type,
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);
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);
237 if (PREDICT_FALSE (maybe_error))
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);
245 else if (bt->current_config_index)
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;
261 clib_memcpy (vnet_buffer (b[0])->sw_if_index,
262 vnet_buffer (bt)->sw_if_index, 2 * sizeof (u32));
263 clib_memcpy (vnet_buffer (b[1])->sw_if_index,
264 vnet_buffer (bt)->sw_if_index, 2 * sizeof (u32));
265 clib_memcpy (vnet_buffer (b[2])->sw_if_index,
266 vnet_buffer (bt)->sw_if_index, 2 * sizeof (u32));
267 clib_memcpy (vnet_buffer (b[3])->sw_if_index,
268 vnet_buffer (bt)->sw_if_index, 2 * sizeof (u32));
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]);
283 b[0]->current_length = rxve->length;
284 n_rx_bytes += b[0]->current_length;
288 next[0] = avf_find_next (rxve, b[0], 1);
289 avf_check_for_error (node, rxve + 0, b[0], next);
291 else if (bt->current_config_index)
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;
298 clib_memcpy (vnet_buffer (b[0])->sw_if_index,
299 vnet_buffer (bt)->sw_if_index, 2 * sizeof (u32));
301 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[0]);
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)
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;
326 u32 n_rx_packets = 0, n_rx_bytes = 0;
327 u16 mask = rxq->size - 1;
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;
335 u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
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);
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;
347 while (n_rxv < AVF_RX_VECTOR_SZ)
349 if (rxq->next + 11 < rxq->size)
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);
362 #ifdef CLIB_HAVE_VEC256
364 u64x4 status_dd_eop_mask = u64x4_splat (0x3);
366 if (n_rxv >= AVF_RX_VECTOR_SZ - 4)
369 if (rxq->next >= rxq->size - 4)
372 /* load 1st quadword of 4 dscriptors into 256-bit vector register */
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))
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);
392 u64x4_store_unaligned (v, ptd->rx_vector + n_rxv);
393 maybe_error |= !u64x4_is_all_zero (err4);
395 clib_memcpy (bi, rxq->bufs + rxq->next, 4 * sizeof (u32));
398 rxq->next = (rxq->next + 4) & mask;
399 d = rxq->descs + rxq->next;
401 rxq->n_enqueued -= 4;
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)
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;
418 /* deal with chained buffers */
419 while (PREDICT_FALSE ((d->qword[1] & AVF_RX_DESC_STATUS_EOP) == 0))
421 clib_error ("fixme");
425 rxq->next = (rxq->next + 1) & mask;
426 d = rxq->descs + rxq->next;
436 if (ad->flags & AVF_DEVICE_F_VA_DMA)
437 avf_rxq_refill (vm, node, rxq, 1 /* use_va_dma */ );
439 avf_rxq_refill (vm, node, rxq, 0 /* use_va_dma */ );
441 vlib_get_buffers (vm, buffer_indices, bufs, n_rxv);
442 n_rx_packets = n_rxv;
444 vnet_buffer (bt)->sw_if_index[VLIB_RX] = ad->sw_if_index;
445 vnet_buffer (bt)->sw_if_index[VLIB_TX] = ~0;
447 /* receive burst of packets from DPDK PMD */
448 if (PREDICT_FALSE (ad->per_interface_next_index != ~0))
451 next_index = ad->per_interface_next_index;
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)))
458 vnet_feature_start_device_input_x1 (ad->sw_if_index, &next_index, bt);
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;
471 n_rx_bytes = avf_process_rx_burst (vm, node, bt, ptd->rx_vector, bufs,
472 nexts, n_rxv, maybe_error, 0);
474 /* packet trace if enabled */
475 if (PREDICT_FALSE ((n_trace = vlib_get_trace_count (vm, node))))
477 u32 n_left = n_rx_packets;
480 while (n_trace && n_left)
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 (&tr->rxve, rxve, sizeof (avf_rx_vector_entry_t));
497 vlib_set_trace_count (vm, node, n_trace);
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);
508 VLIB_NODE_FN (avf_input_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
509 vlib_frame_t * frame)
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;
516 foreach_device_and_queue (dq, rt->devices_and_queues)
519 ad = vec_elt_at_index (am->devices, dq->dev_instance);
520 if ((ad->flags & AVF_DEVICE_F_ADMIN_UP) == 0)
522 n_rx += avf_device_input_inline (vm, node, frame, ad, dq->queue_id);
528 VLIB_REGISTER_NODE (avf_input_node) = {
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,
542 * fd.io coding-style-patch-verification: ON
545 * eval: (c-set-style "gnu")