1 /* SPDX-License-Identifier: Apache-2.0
2 * Copyright(c) 2021 Cisco Systems, Inc.
5 #include <vppinfra/clib.h>
7 #include <vppinfra/vector/mask_compare.h>
8 #include <vppinfra/vector/compress.h>
10 static_always_inline u32
11 enqueue_one (vlib_main_t *vm, vlib_node_runtime_t *node,
12 vlib_frame_bitmap_t used_elt_bmp, u16 next_index, u32 *buffers,
13 u16 *nexts, u32 n_buffers, u32 n_left, u32 *tmp)
15 vlib_frame_bitmap_t match_bmp;
17 u32 n_extracted, n_free;
20 f = vlib_get_next_frame_internal (vm, node, next_index, 0);
22 n_free = VLIB_FRAME_SIZE - f->n_vectors;
24 /* if frame contains enough space for worst case scenario, we can avoid
27 to = (u32 *) vlib_frame_vector_args (f) + f->n_vectors;
31 clib_mask_compare_u16 (next_index, nexts, match_bmp, n_buffers);
32 n_extracted = clib_compress_u32 (to, buffers, match_bmp, n_buffers);
33 vlib_frame_bitmap_or (used_elt_bmp, match_bmp);
37 /* indices already written to frame, just close it */
38 vlib_put_next_frame (vm, node, next_index, n_free - n_extracted);
40 else if (n_free >= n_extracted)
42 /* enough space in the existing frame */
43 to = (u32 *) vlib_frame_vector_args (f) + f->n_vectors;
44 vlib_buffer_copy_indices (to, tmp, n_extracted);
45 vlib_put_next_frame (vm, node, next_index, n_free - n_extracted);
50 to = (u32 *) vlib_frame_vector_args (f) + f->n_vectors;
51 vlib_buffer_copy_indices (to, tmp, n_free);
52 vlib_put_next_frame (vm, node, next_index, 0);
55 u32 n_2nd_frame = n_extracted - n_free;
56 f = vlib_get_next_frame_internal (vm, node, next_index, 1);
57 to = vlib_frame_vector_args (f);
58 vlib_buffer_copy_indices (to, tmp + n_free, n_2nd_frame);
59 vlib_put_next_frame (vm, node, next_index,
60 VLIB_FRAME_SIZE - n_2nd_frame);
63 return n_left - n_extracted;
66 void __clib_section (".vlib_buffer_enqueue_to_next_fn")
67 CLIB_MULTIARCH_FN (vlib_buffer_enqueue_to_next_fn)
68 (vlib_main_t *vm, vlib_node_runtime_t *node, u32 *buffers, u16 *nexts,
71 u32 tmp[VLIB_FRAME_SIZE];
75 while (count >= VLIB_FRAME_SIZE)
77 vlib_frame_bitmap_t used_elt_bmp = {};
78 n_left = VLIB_FRAME_SIZE;
81 next_index = nexts[0];
82 n_left = enqueue_one (vm, node, used_elt_bmp, next_index, buffers, nexts,
83 VLIB_FRAME_SIZE, n_left, tmp);
87 while (PREDICT_FALSE (used_elt_bmp[off] == ~0))
90 ASSERT (off < ARRAY_LEN (used_elt_bmp));
94 nexts[off * 64 + count_trailing_zeros (~used_elt_bmp[off])];
95 n_left = enqueue_one (vm, node, used_elt_bmp, next_index, buffers,
96 nexts, VLIB_FRAME_SIZE, n_left, tmp);
99 buffers += VLIB_FRAME_SIZE;
100 nexts += VLIB_FRAME_SIZE;
101 count -= VLIB_FRAME_SIZE;
106 vlib_frame_bitmap_t used_elt_bmp = {};
107 next_index = nexts[0];
111 n_left = enqueue_one (vm, node, used_elt_bmp, next_index, buffers, nexts,
116 while (PREDICT_FALSE (used_elt_bmp[off] == ~0))
119 ASSERT (off < ARRAY_LEN (used_elt_bmp));
123 nexts[off * 64 + count_trailing_zeros (~used_elt_bmp[off])];
124 n_left = enqueue_one (vm, node, used_elt_bmp, next_index, buffers,
125 nexts, count, n_left, tmp);
130 CLIB_MARCH_FN_REGISTRATION (vlib_buffer_enqueue_to_next_fn);
132 void __clib_section (".vlib_buffer_enqueue_to_single_next_fn")
133 CLIB_MULTIARCH_FN (vlib_buffer_enqueue_to_single_next_fn)
134 (vlib_main_t *vm, vlib_node_runtime_t *node, u32 *buffers, u16 next_index,
137 u32 *to_next, n_left_to_next, n_enq;
139 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
141 if (PREDICT_TRUE (n_left_to_next >= count))
143 vlib_buffer_copy_indices (to_next, buffers, count);
144 n_left_to_next -= count;
145 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
149 n_enq = n_left_to_next;
151 vlib_buffer_copy_indices (to_next, buffers, n_enq);
152 n_left_to_next -= n_enq;
154 if (PREDICT_FALSE (count > n_enq))
159 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
160 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
161 n_enq = clib_min (n_left_to_next, count);
164 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
166 CLIB_MARCH_FN_REGISTRATION (vlib_buffer_enqueue_to_single_next_fn);
168 static inline vlib_frame_queue_elt_t *
169 vlib_get_frame_queue_elt (vlib_frame_queue_main_t *fqm, u32 index,
172 vlib_frame_queue_t *fq;
173 u64 nelts, tail, new_tail;
175 fq = fqm->vlib_frame_queues[index];
180 tail = __atomic_load_n (&fq->tail, __ATOMIC_ACQUIRE);
183 if (new_tail >= fq->head + nelts)
188 /* Wait until a ring slot is available */
189 while (new_tail >= fq->head + nelts)
190 vlib_worker_thread_barrier_check ();
193 if (!__atomic_compare_exchange_n (&fq->tail, &tail, new_tail, 0 /* weak */,
194 __ATOMIC_RELAXED, __ATOMIC_RELAXED))
197 return fq->elts + (new_tail & (nelts - 1));
200 static_always_inline u32
201 vlib_buffer_enqueue_to_thread_inline (vlib_main_t *vm,
202 vlib_node_runtime_t *node,
203 vlib_frame_queue_main_t *fqm,
204 u32 *buffer_indices, u16 *thread_indices,
205 u32 n_packets, int drop_on_congestion,
206 int with_aux, u32 *aux_data)
208 u32 drop_list[VLIB_FRAME_SIZE], n_drop = 0;
209 vlib_frame_bitmap_t mask, used_elts = {};
210 vlib_frame_queue_elt_t *hf = 0;
212 u32 n_comp, off = 0, n_left = n_packets;
214 thread_index = thread_indices[0];
217 clib_mask_compare_u16 (thread_index, thread_indices, mask, n_packets);
218 hf = vlib_get_frame_queue_elt (fqm, thread_index, drop_on_congestion);
220 n_comp = clib_compress_u32 (hf ? hf->buffer_index : drop_list + n_drop,
221 buffer_indices, mask, n_packets);
223 clib_compress_u32 (hf ? hf->aux_data : drop_list + n_drop, aux_data, mask,
228 if (node->flags & VLIB_NODE_FLAG_TRACE)
230 hf->n_vectors = n_comp;
231 __atomic_store_n (&hf->valid, 1, __ATOMIC_RELEASE);
232 vlib_get_main_by_index (thread_index)->check_frame_queues = 1;
241 vlib_frame_bitmap_or (used_elts, mask);
243 while (PREDICT_FALSE (used_elts[off] == ~0))
246 ASSERT (off < ARRAY_LEN (used_elts));
250 thread_indices[off * 64 + count_trailing_zeros (~used_elts[off])];
254 if (drop_on_congestion && n_drop)
255 vlib_buffer_free (vm, drop_list, n_drop);
257 return n_packets - n_drop;
260 u32 __clib_section (".vlib_buffer_enqueue_to_thread_fn")
261 CLIB_MULTIARCH_FN (vlib_buffer_enqueue_to_thread_fn)
262 (vlib_main_t *vm, vlib_node_runtime_t *node, u32 frame_queue_index,
263 u32 *buffer_indices, u16 *thread_indices, u32 n_packets,
264 int drop_on_congestion)
266 vlib_thread_main_t *tm = vlib_get_thread_main ();
267 vlib_frame_queue_main_t *fqm;
270 fqm = vec_elt_at_index (tm->frame_queue_mains, frame_queue_index);
272 while (n_packets >= VLIB_FRAME_SIZE)
274 n_enq += vlib_buffer_enqueue_to_thread_inline (
275 vm, node, fqm, buffer_indices, thread_indices, VLIB_FRAME_SIZE,
276 drop_on_congestion, 0 /* with_aux */, NULL);
277 buffer_indices += VLIB_FRAME_SIZE;
278 thread_indices += VLIB_FRAME_SIZE;
279 n_packets -= VLIB_FRAME_SIZE;
285 n_enq += vlib_buffer_enqueue_to_thread_inline (
286 vm, node, fqm, buffer_indices, thread_indices, n_packets,
287 drop_on_congestion, 0 /* with_aux */, NULL);
292 u32 __clib_section (".vlib_buffer_enqueue_to_thread_with_aux_fn")
293 CLIB_MULTIARCH_FN (vlib_buffer_enqueue_to_thread_with_aux_fn)
294 (vlib_main_t *vm, vlib_node_runtime_t *node, u32 frame_queue_index,
295 u32 *buffer_indices, u32 *aux, u16 *thread_indices, u32 n_packets,
296 int drop_on_congestion)
298 vlib_thread_main_t *tm = vlib_get_thread_main ();
299 vlib_frame_queue_main_t *fqm;
302 fqm = vec_elt_at_index (tm->frame_queue_mains, frame_queue_index);
304 while (n_packets >= VLIB_FRAME_SIZE)
306 n_enq += vlib_buffer_enqueue_to_thread_inline (
307 vm, node, fqm, buffer_indices, thread_indices, VLIB_FRAME_SIZE,
308 drop_on_congestion, 1 /* with_aux */, aux);
309 buffer_indices += VLIB_FRAME_SIZE;
310 thread_indices += VLIB_FRAME_SIZE;
311 n_packets -= VLIB_FRAME_SIZE;
317 n_enq += vlib_buffer_enqueue_to_thread_inline (
318 vm, node, fqm, buffer_indices, thread_indices, n_packets,
319 drop_on_congestion, 1 /* with_aux */, aux);
324 CLIB_MARCH_FN_REGISTRATION (vlib_buffer_enqueue_to_thread_fn);
325 CLIB_MARCH_FN_REGISTRATION (vlib_buffer_enqueue_to_thread_with_aux_fn);
327 static_always_inline u32
328 vlib_frame_queue_dequeue_inline (vlib_main_t *vm, vlib_frame_queue_main_t *fqm,
331 u32 thread_id = vm->thread_index;
332 vlib_frame_queue_t *fq = fqm->vlib_frame_queues[thread_id];
333 u32 mask = fq->nelts - 1;
334 vlib_frame_queue_elt_t *elt;
335 u32 n_free, n_copy, *from, *from_aux, *to = 0, *to_aux = 0, processed = 0,
340 ASSERT (vm == vlib_global_main.vlib_mains[thread_id]);
342 if (PREDICT_FALSE (fqm->node_index == ~0))
345 * Gather trace data for frame queues
347 if (PREDICT_FALSE (fq->trace))
349 frame_queue_trace_t *fqt;
350 frame_queue_nelt_counter_t *fqh;
353 fqt = &fqm->frame_queue_traces[thread_id];
355 fqt->nelts = fq->nelts;
356 fqt->head = fq->head;
357 fqt->tail = fq->tail;
358 fqt->threshold = fq->vector_threshold;
359 fqt->n_in_use = fqt->tail - fqt->head;
360 if (fqt->n_in_use >= fqt->nelts)
362 // if beyond max then use max
363 fqt->n_in_use = fqt->nelts - 1;
366 /* Record the number of elements in use in the histogram */
367 fqh = &fqm->frame_queue_histogram[thread_id];
368 fqh->count[fqt->n_in_use]++;
370 /* Record a snapshot of the elements in use */
371 for (elix = 0; elix < fqt->nelts; elix++)
373 elt = fq->elts + ((fq->head + 1 + elix) & (mask));
376 fqt->n_vectors[elix] = elt->n_vectors;
384 if (fq->head == fq->tail)
387 elt = fq->elts + ((fq->head + 1) & mask);
389 if (!__atomic_load_n (&elt->valid, __ATOMIC_ACQUIRE))
392 from = elt->buffer_index + elt->offset;
394 from_aux = elt->aux_data + elt->offset;
395 ASSERT (elt->offset + elt->n_vectors <= VLIB_FRAME_SIZE);
399 f = vlib_get_frame_to_node (vm, fqm->node_index);
400 to = vlib_frame_vector_args (f);
402 to_aux = vlib_frame_aux_args (f);
403 n_free = VLIB_FRAME_SIZE;
406 if (elt->maybe_trace)
407 f->frame_flags |= VLIB_NODE_FLAG_TRACE;
409 n_copy = clib_min (n_free, elt->n_vectors);
411 vlib_buffer_copy_indices (to, from, n_copy);
415 vlib_buffer_copy_indices (to_aux, from_aux, n_copy);
424 f->n_vectors = VLIB_FRAME_SIZE;
425 vlib_put_frame_to_node (vm, fqm->node_index, f);
429 if (n_copy < elt->n_vectors)
431 /* not empty - leave it on the ring */
432 elt->n_vectors -= n_copy;
433 elt->offset += n_copy;
437 /* empty - reset and bump head */
438 u32 sz = STRUCT_OFFSET_OF (vlib_frame_queue_elt_t, end_of_reset);
439 clib_memset (elt, 0, sz);
440 __atomic_store_n (&fq->head, fq->head + 1, __ATOMIC_RELEASE);
444 /* Limit the number of packets pushed into the graph */
445 if (vectors >= fq->vector_threshold)
451 f->n_vectors = VLIB_FRAME_SIZE - n_free;
452 vlib_put_frame_to_node (vm, fqm->node_index, f);
458 u32 __clib_section (".vlib_frame_queue_dequeue_fn")
459 CLIB_MULTIARCH_FN (vlib_frame_queue_dequeue_fn)
460 (vlib_main_t *vm, vlib_frame_queue_main_t *fqm)
462 return vlib_frame_queue_dequeue_inline (vm, fqm, 0 /* with_aux */);
465 CLIB_MARCH_FN_REGISTRATION (vlib_frame_queue_dequeue_fn);
467 u32 __clib_section (".vlib_frame_queue_dequeue_with_aux_fn")
468 CLIB_MULTIARCH_FN (vlib_frame_queue_dequeue_with_aux_fn)
469 (vlib_main_t *vm, vlib_frame_queue_main_t *fqm)
471 return vlib_frame_queue_dequeue_inline (vm, fqm, 1 /* with_aux */);
474 CLIB_MARCH_FN_REGISTRATION (vlib_frame_queue_dequeue_with_aux_fn);
476 #ifndef CLIB_MARCH_VARIANT
477 vlib_buffer_func_main_t vlib_buffer_func_main;
479 static clib_error_t *
480 vlib_buffer_funcs_init (vlib_main_t *vm)
482 vlib_buffer_func_main_t *bfm = &vlib_buffer_func_main;
483 bfm->buffer_enqueue_to_next_fn =
484 CLIB_MARCH_FN_POINTER (vlib_buffer_enqueue_to_next_fn);
485 bfm->buffer_enqueue_to_single_next_fn =
486 CLIB_MARCH_FN_POINTER (vlib_buffer_enqueue_to_single_next_fn);
487 bfm->buffer_enqueue_to_thread_fn =
488 CLIB_MARCH_FN_POINTER (vlib_buffer_enqueue_to_thread_fn);
489 bfm->buffer_enqueue_to_thread_with_aux_fn =
490 CLIB_MARCH_FN_POINTER (vlib_buffer_enqueue_to_thread_with_aux_fn);
494 VLIB_INIT_FUNCTION (vlib_buffer_funcs_init);