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
50 static_always_inline void
51 avf_rx_desc_write (avf_rx_desc_t * d, u64 addr)
53 #ifdef CLIB_HAVE_VEC256
54 u64x4 v = { addr, 0, 0, 0 };
55 u64x4_store_unaligned (v, (void *) d);
62 static_always_inline void
63 avf_rxq_refill (vlib_main_t * vm, vlib_node_runtime_t * node, avf_rxq_t * rxq,
66 u16 n_refill, mask, n_alloc, slot, size;
68 avf_rx_desc_t *d, *first_d;
73 n_refill = mask - rxq->n_enqueued;
74 if (PREDICT_TRUE (n_refill <= AVF_INPUT_REFILL_TRESHOLD))
77 slot = (rxq->next - n_refill - 1) & mask;
79 n_refill &= ~7; /* round to 8 */
80 n_alloc = vlib_buffer_alloc_to_ring (vm, rxq->bufs, slot, size, n_refill);
82 if (PREDICT_FALSE (n_alloc != n_refill))
84 vlib_error_count (vm, node->node_index,
85 AVF_INPUT_ERROR_BUFFER_ALLOC, 1);
87 vlib_buffer_free_from_ring (vm, rxq->bufs, slot, size, n_alloc);
91 rxq->n_enqueued += n_alloc;
94 ASSERT (slot % 8 == 0);
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]));
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]));
127 slot = (slot + 8) & mask;
131 CLIB_MEMORY_STORE_BARRIER ();
132 *(rxq->qrx_tail) = slot;
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)
140 uword n_rx_bytes = 0;
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);
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;
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));
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]);
178 b[0]->current_length = rxve->length;
179 n_rx_bytes += b[0]->current_length;
181 clib_memcpy_fast (vnet_buffer (b[0])->sw_if_index,
182 vnet_buffer (bt)->sw_if_index, 2 * sizeof (u32));
184 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b[0]);
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)
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;
207 u32 n_rx_packets = 0, n_rx_bytes = 0;
208 u16 mask = rxq->size - 1;
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;
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);
222 /* is there anything on the ring */
223 d = rxq->descs + rxq->next;
224 if ((d->qword[1] & AVF_RX_DESC_STATUS_DD) == 0)
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);
231 /* fetch up to AVF_RX_VECTOR_SZ from the rx ring, unflatten them and
232 copy needed data from descriptor to rx vector */
234 while (n_rxv < AVF_RX_VECTOR_SZ)
236 if (rxq->next + 11 < rxq->size)
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);
249 #ifdef CLIB_HAVE_VEC256
251 u64x4 status_dd_eop_mask = u64x4_splat (0x3);
253 if (n_rxv >= AVF_RX_VECTOR_SZ - 4)
256 if (rxq->next >= rxq->size - 4)
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]);
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))
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);
272 u64x4_store_unaligned (v, ptd->rx_vector + n_rxv);
274 if (!u64x4_is_all_zero (err4))
275 or_error |= err4[0] | err4[1] | err4[2] | err4[3];
277 clib_memcpy_fast (bi, rxq->bufs + rxq->next, 4 * sizeof (u32));
280 rxq->next = (rxq->next + 4) & mask;
281 d = rxq->descs + rxq->next;
283 rxq->n_enqueued -= 4;
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)
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;
300 /* deal with chained buffers */
301 while (PREDICT_FALSE ((d->qword[1] & AVF_RX_DESC_STATUS_EOP) == 0))
303 clib_error ("fixme");
307 rxq->next = (rxq->next + 1) & mask;
308 d = rxq->descs + rxq->next;
318 if (ad->flags & AVF_DEVICE_F_VA_DMA)
319 avf_rxq_refill (vm, node, rxq, 1 /* use_va_dma */ );
321 avf_rxq_refill (vm, node, rxq, 0 /* use_va_dma */ );
323 vlib_get_buffers (vm, to_next, bufs, n_rxv);
324 n_rx_packets = n_rxv;
326 vnet_buffer (bt)->sw_if_index[VLIB_RX] = ad->sw_if_index;
327 vnet_buffer (bt)->sw_if_index[VLIB_TX] = ~0;
329 n_rx_bytes = avf_process_rx_burst (vm, node, bt, ptd->rx_vector, bufs,
332 /* packet trace if enabled */
333 if (PREDICT_FALSE ((n_trace = vlib_get_trace_count (vm, node))))
335 u32 n_left = n_rx_packets;
337 while (n_trace && n_left)
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));
353 vlib_set_trace_count (vm, node, n_trace);
356 if (PREDICT_TRUE (next_index == VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT))
358 vlib_next_frame_t *nf;
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;
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;
369 if ((or_error & (1 << 3)) == 0)
370 f->flags |= ETH_INPUT_FRAME_F_IP4_CKSUM_OK;
372 n_left_to_next -= n_rx_packets;
373 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
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);
383 VLIB_NODE_FN (avf_input_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
384 vlib_frame_t * frame)
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;
391 foreach_device_and_queue (dq, rt->devices_and_queues)
394 ad = vec_elt_at_index (am->devices, dq->dev_instance);
395 if ((ad->flags & AVF_DEVICE_F_ADMIN_UP) == 0)
397 n_rx += avf_device_input_inline (vm, node, frame, ad, dq->queue_id);
403 VLIB_REGISTER_NODE (avf_input_node) = {
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,
417 * fd.io coding-style-patch-verification: ON
420 * eval: (c-set-style "gnu")