blob: f0e2add8457737e22418b40c829f3a81d312f5d4 [file] [log] [blame]
Damjan Marion1c229712021-04-21 12:55:15 +02001/* SPDX-License-Identifier: Apache-2.0
2 * Copyright(c) 2021 Cisco Systems, Inc.
3 */
4
Damjan Marionef0bac72021-04-22 18:08:28 +02005#include <vppinfra/clib.h>
Damjan Marion1c229712021-04-21 12:55:15 +02006#include <vlib/vlib.h>
Damjan Mariond154a172021-07-13 21:12:41 +02007#include <vppinfra/vector/mask_compare.h>
8#include <vppinfra/vector/compress.h>
Damjan Marion1c229712021-04-21 12:55:15 +02009
Damjan Marionef0bac72021-04-22 18:08:28 +020010static_always_inline u32
Damjan Marionefeea5b2022-02-10 15:01:03 +010011enqueue_one (vlib_main_t *vm, vlib_node_runtime_t *node,
12 vlib_frame_bitmap_t used_elt_bmp, u16 next_index, u32 *buffers,
Mohammed Hawaricd758e62022-05-31 18:11:05 +020013 u16 *nexts, u32 n_buffers, u32 n_left, u32 *tmp, u8 maybe_aux,
14 u32 *aux_data, u32 *tmp_aux)
Damjan Marionef0bac72021-04-22 18:08:28 +020015{
Damjan Marionefeea5b2022-02-10 15:01:03 +010016 vlib_frame_bitmap_t match_bmp;
Damjan Marionef0bac72021-04-22 18:08:28 +020017 vlib_frame_t *f;
18 u32 n_extracted, n_free;
Mohammed Hawaricd758e62022-05-31 18:11:05 +020019 u32 *to, *to_aux;
Damjan Marionef0bac72021-04-22 18:08:28 +020020
21 f = vlib_get_next_frame_internal (vm, node, next_index, 0);
22
Mohammed Hawaricd758e62022-05-31 18:11:05 +020023 maybe_aux = maybe_aux && f->aux_offset;
24
Damjan Marionef0bac72021-04-22 18:08:28 +020025 n_free = VLIB_FRAME_SIZE - f->n_vectors;
26
27 /* if frame contains enough space for worst case scenario, we can avoid
28 * use of tmp */
29 if (n_free >= n_left)
Mohammed Hawaricd758e62022-05-31 18:11:05 +020030 {
31 to = (u32 *) vlib_frame_vector_args (f) + f->n_vectors;
32 if (maybe_aux)
33 to_aux = (u32 *) vlib_frame_aux_args (f) + f->n_vectors;
34 }
Damjan Marionef0bac72021-04-22 18:08:28 +020035 else
Mohammed Hawaricd758e62022-05-31 18:11:05 +020036 {
37 to = tmp;
38 if (maybe_aux)
39 to_aux = tmp_aux;
40 }
Damjan Marione3e35552021-05-06 17:34:49 +020041 clib_mask_compare_u16 (next_index, nexts, match_bmp, n_buffers);
Damjan Marione3e35552021-05-06 17:34:49 +020042 n_extracted = clib_compress_u32 (to, buffers, match_bmp, n_buffers);
Mohammed Hawaricd758e62022-05-31 18:11:05 +020043 if (maybe_aux)
44 clib_compress_u32 (to_aux, aux_data, match_bmp, n_buffers);
Damjan Marionefeea5b2022-02-10 15:01:03 +010045 vlib_frame_bitmap_or (used_elt_bmp, match_bmp);
Damjan Marionef0bac72021-04-22 18:08:28 +020046
47 if (to != tmp)
48 {
49 /* indices already written to frame, just close it */
50 vlib_put_next_frame (vm, node, next_index, n_free - n_extracted);
51 }
52 else if (n_free >= n_extracted)
53 {
54 /* enough space in the existing frame */
55 to = (u32 *) vlib_frame_vector_args (f) + f->n_vectors;
56 vlib_buffer_copy_indices (to, tmp, n_extracted);
Mohammed Hawaricd758e62022-05-31 18:11:05 +020057 if (maybe_aux)
58 {
59 to_aux = (u32 *) vlib_frame_aux_args (f) + f->n_vectors;
60 vlib_buffer_copy_indices (to_aux, tmp_aux, n_extracted);
61 }
Damjan Marionef0bac72021-04-22 18:08:28 +020062 vlib_put_next_frame (vm, node, next_index, n_free - n_extracted);
63 }
64 else
65 {
66 /* full frame */
67 to = (u32 *) vlib_frame_vector_args (f) + f->n_vectors;
68 vlib_buffer_copy_indices (to, tmp, n_free);
Mohammed Hawaricd758e62022-05-31 18:11:05 +020069 if (maybe_aux)
70 {
71 to_aux = (u32 *) vlib_frame_aux_args (f) + f->n_vectors;
72 vlib_buffer_copy_indices (to_aux, tmp_aux, n_free);
73 }
Damjan Marionef0bac72021-04-22 18:08:28 +020074 vlib_put_next_frame (vm, node, next_index, 0);
75
76 /* second frame */
77 u32 n_2nd_frame = n_extracted - n_free;
78 f = vlib_get_next_frame_internal (vm, node, next_index, 1);
79 to = vlib_frame_vector_args (f);
80 vlib_buffer_copy_indices (to, tmp + n_free, n_2nd_frame);
Mohammed Hawaricd758e62022-05-31 18:11:05 +020081 if (maybe_aux)
82 {
83 to_aux = vlib_frame_aux_args (f);
84 vlib_buffer_copy_indices (to_aux, tmp_aux + n_free, n_2nd_frame);
85 }
Damjan Marionef0bac72021-04-22 18:08:28 +020086 vlib_put_next_frame (vm, node, next_index,
87 VLIB_FRAME_SIZE - n_2nd_frame);
88 }
89
90 return n_left - n_extracted;
91}
92
Mohammed Hawaricd758e62022-05-31 18:11:05 +020093static_always_inline void
94vlib_buffer_enqueue_to_next_fn_inline (vlib_main_t *vm,
95 vlib_node_runtime_t *node, u32 *buffers,
96 u32 *aux_data, u16 *nexts, uword count,
97 u8 maybe_aux)
Damjan Marion1c229712021-04-21 12:55:15 +020098{
Damjan Marionef0bac72021-04-22 18:08:28 +020099 u32 tmp[VLIB_FRAME_SIZE];
Mohammed Hawaricd758e62022-05-31 18:11:05 +0200100 u32 tmp_aux[VLIB_FRAME_SIZE];
Damjan Marionef0bac72021-04-22 18:08:28 +0200101 u32 n_left;
Damjan Marion1c229712021-04-21 12:55:15 +0200102 u16 next_index;
103
Damjan Marionef0bac72021-04-22 18:08:28 +0200104 while (count >= VLIB_FRAME_SIZE)
Damjan Marion1c229712021-04-21 12:55:15 +0200105 {
Damjan Marionefeea5b2022-02-10 15:01:03 +0100106 vlib_frame_bitmap_t used_elt_bmp = {};
Damjan Marionef0bac72021-04-22 18:08:28 +0200107 n_left = VLIB_FRAME_SIZE;
Damjan Marione3e35552021-05-06 17:34:49 +0200108 u32 off = 0;
Damjan Marion1c229712021-04-21 12:55:15 +0200109
Damjan Marionef0bac72021-04-22 18:08:28 +0200110 next_index = nexts[0];
Damjan Marione3e35552021-05-06 17:34:49 +0200111 n_left = enqueue_one (vm, node, used_elt_bmp, next_index, buffers, nexts,
Mohammed Hawaricd758e62022-05-31 18:11:05 +0200112 VLIB_FRAME_SIZE, n_left, tmp, maybe_aux, aux_data,
113 tmp_aux);
Damjan Marion1c229712021-04-21 12:55:15 +0200114
Damjan Marionef0bac72021-04-22 18:08:28 +0200115 while (n_left)
Damjan Marion1c229712021-04-21 12:55:15 +0200116 {
Damjan Marione3e35552021-05-06 17:34:49 +0200117 while (PREDICT_FALSE (used_elt_bmp[off] == ~0))
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200118 {
119 off++;
120 ASSERT (off < ARRAY_LEN (used_elt_bmp));
121 }
Damjan Marione3e35552021-05-06 17:34:49 +0200122
123 next_index =
124 nexts[off * 64 + count_trailing_zeros (~used_elt_bmp[off])];
125 n_left = enqueue_one (vm, node, used_elt_bmp, next_index, buffers,
Mohammed Hawaricd758e62022-05-31 18:11:05 +0200126 nexts, VLIB_FRAME_SIZE, n_left, tmp, maybe_aux,
127 aux_data, tmp_aux);
Damjan Marion1c229712021-04-21 12:55:15 +0200128 }
129
Damjan Marionef0bac72021-04-22 18:08:28 +0200130 buffers += VLIB_FRAME_SIZE;
Mohammed Hawaricd758e62022-05-31 18:11:05 +0200131 if (maybe_aux)
132 aux_data += VLIB_FRAME_SIZE;
Damjan Marionef0bac72021-04-22 18:08:28 +0200133 nexts += VLIB_FRAME_SIZE;
134 count -= VLIB_FRAME_SIZE;
Damjan Marion1c229712021-04-21 12:55:15 +0200135 }
Damjan Marionef0bac72021-04-22 18:08:28 +0200136
137 if (count)
138 {
Damjan Marionefeea5b2022-02-10 15:01:03 +0100139 vlib_frame_bitmap_t used_elt_bmp = {};
Damjan Marionef0bac72021-04-22 18:08:28 +0200140 next_index = nexts[0];
141 n_left = count;
Damjan Marione3e35552021-05-06 17:34:49 +0200142 u32 off = 0;
Damjan Marionef0bac72021-04-22 18:08:28 +0200143
Damjan Marione3e35552021-05-06 17:34:49 +0200144 n_left = enqueue_one (vm, node, used_elt_bmp, next_index, buffers, nexts,
Mohammed Hawaricd758e62022-05-31 18:11:05 +0200145 count, n_left, tmp, maybe_aux, aux_data, tmp_aux);
Damjan Marionef0bac72021-04-22 18:08:28 +0200146
147 while (n_left)
148 {
Damjan Marione3e35552021-05-06 17:34:49 +0200149 while (PREDICT_FALSE (used_elt_bmp[off] == ~0))
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200150 {
151 off++;
152 ASSERT (off < ARRAY_LEN (used_elt_bmp));
153 }
Damjan Marione3e35552021-05-06 17:34:49 +0200154
155 next_index =
156 nexts[off * 64 + count_trailing_zeros (~used_elt_bmp[off])];
Mohammed Hawaricd758e62022-05-31 18:11:05 +0200157 n_left =
158 enqueue_one (vm, node, used_elt_bmp, next_index, buffers, nexts,
159 count, n_left, tmp, maybe_aux, aux_data, tmp_aux);
Damjan Marionef0bac72021-04-22 18:08:28 +0200160 }
161 }
Damjan Marion1c229712021-04-21 12:55:15 +0200162}
Damjan Marion23c34882021-04-25 10:46:26 +0200163
Mohammed Hawaricd758e62022-05-31 18:11:05 +0200164void __clib_section (".vlib_buffer_enqueue_to_next_fn")
165CLIB_MULTIARCH_FN (vlib_buffer_enqueue_to_next_fn)
166(vlib_main_t *vm, vlib_node_runtime_t *node, u32 *buffers, u16 *nexts,
167 uword count)
168{
169 vlib_buffer_enqueue_to_next_fn_inline (vm, node, buffers, NULL, nexts, count,
170 0 /* maybe_aux */);
171}
172
Damjan Marion1c229712021-04-21 12:55:15 +0200173CLIB_MARCH_FN_REGISTRATION (vlib_buffer_enqueue_to_next_fn);
174
Mohammed Hawaricd758e62022-05-31 18:11:05 +0200175void __clib_section (".vlib_buffer_enqueue_to_next_with_aux_fn")
176CLIB_MULTIARCH_FN (vlib_buffer_enqueue_to_next_with_aux_fn)
177(vlib_main_t *vm, vlib_node_runtime_t *node, u32 *buffers, u32 *aux_data,
178 u16 *nexts, uword count)
Damjan Marion1c229712021-04-21 12:55:15 +0200179{
Mohammed Hawaricd758e62022-05-31 18:11:05 +0200180 vlib_buffer_enqueue_to_next_fn_inline (vm, node, buffers, aux_data, nexts,
181 count, 1 /* maybe_aux */);
182}
Damjan Marion1c229712021-04-21 12:55:15 +0200183
Mohammed Hawaricd758e62022-05-31 18:11:05 +0200184CLIB_MARCH_FN_REGISTRATION (vlib_buffer_enqueue_to_next_with_aux_fn);
185
186static_always_inline void
187vlib_buffer_enqueue_to_single_next_fn_inline (vlib_main_t *vm,
188 vlib_node_runtime_t *node,
189 u32 *buffers, u32 *aux_data,
190 u16 next_index, u32 count,
191 u8 with_aux)
192{
193 u32 *to_next, *to_next_aux, n_left_to_next, n_enq;
194
195 if (with_aux)
196 vlib_get_next_frame_with_aux (vm, node, next_index, to_next, to_next_aux,
197 n_left_to_next);
198 else
199 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Damjan Marion1c229712021-04-21 12:55:15 +0200200
201 if (PREDICT_TRUE (n_left_to_next >= count))
202 {
203 vlib_buffer_copy_indices (to_next, buffers, count);
Mohammed Hawaricd758e62022-05-31 18:11:05 +0200204 if (with_aux)
205 vlib_buffer_copy_indices (to_next_aux, aux_data, count);
Damjan Marion1c229712021-04-21 12:55:15 +0200206 n_left_to_next -= count;
207 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
208 return;
209 }
210
211 n_enq = n_left_to_next;
212next:
213 vlib_buffer_copy_indices (to_next, buffers, n_enq);
Mohammed Hawaricd758e62022-05-31 18:11:05 +0200214 if (with_aux)
215 vlib_buffer_copy_indices (to_next_aux, aux_data, n_enq);
Damjan Marion1c229712021-04-21 12:55:15 +0200216 n_left_to_next -= n_enq;
217
218 if (PREDICT_FALSE (count > n_enq))
219 {
220 count -= n_enq;
221 buffers += n_enq;
Mohammed Hawaricd758e62022-05-31 18:11:05 +0200222 if (with_aux)
223 aux_data += n_enq;
Damjan Marion1c229712021-04-21 12:55:15 +0200224
225 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Mohammed Hawaricd758e62022-05-31 18:11:05 +0200226 if (with_aux)
227 vlib_get_next_frame_with_aux (vm, node, next_index, to_next,
228 to_next_aux, n_left_to_next);
229 else
230 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Damjan Marion1c229712021-04-21 12:55:15 +0200231 n_enq = clib_min (n_left_to_next, count);
232 goto next;
233 }
234 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
235}
Mohammed Hawaricd758e62022-05-31 18:11:05 +0200236
237void __clib_section (".vlib_buffer_enqueue_to_single_next_fn")
238CLIB_MULTIARCH_FN (vlib_buffer_enqueue_to_single_next_fn)
239(vlib_main_t *vm, vlib_node_runtime_t *node, u32 *buffers, u16 next_index,
240 u32 count)
241{
242 vlib_buffer_enqueue_to_single_next_fn_inline (
243 vm, node, buffers, NULL, next_index, count, 0 /* with_aux */);
244}
Damjan Marion1c229712021-04-21 12:55:15 +0200245CLIB_MARCH_FN_REGISTRATION (vlib_buffer_enqueue_to_single_next_fn);
246
Mohammed Hawaricd758e62022-05-31 18:11:05 +0200247void __clib_section (".vlib_buffer_enqueue_to_single_next_with_aux_fn")
248CLIB_MULTIARCH_FN (vlib_buffer_enqueue_to_single_next_with_aux_fn)
249(vlib_main_t *vm, vlib_node_runtime_t *node, u32 *buffers, u32 *aux_data,
250 u16 next_index, u32 count)
251{
252 vlib_buffer_enqueue_to_single_next_fn_inline (
253 vm, node, buffers, aux_data, next_index, count, 1 /* with_aux */);
254}
255CLIB_MARCH_FN_REGISTRATION (vlib_buffer_enqueue_to_single_next_with_aux_fn);
256
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200257static inline vlib_frame_queue_elt_t *
258vlib_get_frame_queue_elt (vlib_frame_queue_main_t *fqm, u32 index,
259 int dont_wait)
Damjan Marion1c229712021-04-21 12:55:15 +0200260{
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200261 vlib_frame_queue_t *fq;
262 u64 nelts, tail, new_tail;
Damjan Marion1c229712021-04-21 12:55:15 +0200263
Benoît Ganneb01efc52022-12-02 15:30:56 +0100264 fq = vec_elt (fqm->vlib_frame_queues, index);
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200265 ASSERT (fq);
266 nelts = fq->nelts;
Damjan Marion1c229712021-04-21 12:55:15 +0200267
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200268retry:
269 tail = __atomic_load_n (&fq->tail, __ATOMIC_ACQUIRE);
270 new_tail = tail + 1;
271
272 if (new_tail >= fq->head + nelts)
Damjan Marion1c229712021-04-21 12:55:15 +0200273 {
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200274 if (dont_wait)
275 return 0;
Damjan Marion1c229712021-04-21 12:55:15 +0200276
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200277 /* Wait until a ring slot is available */
278 while (new_tail >= fq->head + nelts)
279 vlib_worker_thread_barrier_check ();
Damjan Marion1c229712021-04-21 12:55:15 +0200280 }
281
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200282 if (!__atomic_compare_exchange_n (&fq->tail, &tail, new_tail, 0 /* weak */,
283 __ATOMIC_RELAXED, __ATOMIC_RELAXED))
284 goto retry;
Damjan Marion1c229712021-04-21 12:55:15 +0200285
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200286 return fq->elts + (new_tail & (nelts - 1));
287}
288
289static_always_inline u32
290vlib_buffer_enqueue_to_thread_inline (vlib_main_t *vm,
291 vlib_node_runtime_t *node,
292 vlib_frame_queue_main_t *fqm,
293 u32 *buffer_indices, u16 *thread_indices,
Mohammed Hawarie7149262022-05-18 10:08:47 +0200294 u32 n_packets, int drop_on_congestion,
295 int with_aux, u32 *aux_data)
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200296{
297 u32 drop_list[VLIB_FRAME_SIZE], n_drop = 0;
Damjan Marionefeea5b2022-02-10 15:01:03 +0100298 vlib_frame_bitmap_t mask, used_elts = {};
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200299 vlib_frame_queue_elt_t *hf = 0;
300 u16 thread_index;
301 u32 n_comp, off = 0, n_left = n_packets;
302
303 thread_index = thread_indices[0];
304
305more:
306 clib_mask_compare_u16 (thread_index, thread_indices, mask, n_packets);
307 hf = vlib_get_frame_queue_elt (fqm, thread_index, drop_on_congestion);
308
309 n_comp = clib_compress_u32 (hf ? hf->buffer_index : drop_list + n_drop,
310 buffer_indices, mask, n_packets);
Mohammed Hawarie7149262022-05-18 10:08:47 +0200311 if (with_aux)
312 clib_compress_u32 (hf ? hf->aux_data : drop_list + n_drop, aux_data, mask,
313 n_packets);
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200314
315 if (hf)
Damjan Marion1c229712021-04-21 12:55:15 +0200316 {
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200317 if (node->flags & VLIB_NODE_FLAG_TRACE)
318 hf->maybe_trace = 1;
319 hf->n_vectors = n_comp;
320 __atomic_store_n (&hf->valid, 1, __ATOMIC_RELEASE);
321 vlib_get_main_by_index (thread_index)->check_frame_queues = 1;
322 }
323 else
324 n_drop += n_comp;
325
326 n_left -= n_comp;
327
328 if (n_left)
329 {
Damjan Marionefeea5b2022-02-10 15:01:03 +0100330 vlib_frame_bitmap_or (used_elts, mask);
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200331
332 while (PREDICT_FALSE (used_elts[off] == ~0))
Damjan Marion1c229712021-04-21 12:55:15 +0200333 {
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200334 off++;
335 ASSERT (off < ARRAY_LEN (used_elts));
Damjan Marion1c229712021-04-21 12:55:15 +0200336 }
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200337
338 thread_index =
339 thread_indices[off * 64 + count_trailing_zeros (~used_elts[off])];
340 goto more;
Damjan Marion1c229712021-04-21 12:55:15 +0200341 }
342
343 if (drop_on_congestion && n_drop)
344 vlib_buffer_free (vm, drop_list, n_drop);
345
346 return n_packets - n_drop;
347}
348
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200349u32 __clib_section (".vlib_buffer_enqueue_to_thread_fn")
350CLIB_MULTIARCH_FN (vlib_buffer_enqueue_to_thread_fn)
351(vlib_main_t *vm, vlib_node_runtime_t *node, u32 frame_queue_index,
352 u32 *buffer_indices, u16 *thread_indices, u32 n_packets,
353 int drop_on_congestion)
354{
355 vlib_thread_main_t *tm = vlib_get_thread_main ();
356 vlib_frame_queue_main_t *fqm;
357 u32 n_enq = 0;
358
359 fqm = vec_elt_at_index (tm->frame_queue_mains, frame_queue_index);
360
361 while (n_packets >= VLIB_FRAME_SIZE)
362 {
363 n_enq += vlib_buffer_enqueue_to_thread_inline (
364 vm, node, fqm, buffer_indices, thread_indices, VLIB_FRAME_SIZE,
Mohammed Hawarie7149262022-05-18 10:08:47 +0200365 drop_on_congestion, 0 /* with_aux */, NULL);
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200366 buffer_indices += VLIB_FRAME_SIZE;
367 thread_indices += VLIB_FRAME_SIZE;
368 n_packets -= VLIB_FRAME_SIZE;
369 }
370
371 if (n_packets == 0)
372 return n_enq;
373
Mohammed Hawarie7149262022-05-18 10:08:47 +0200374 n_enq += vlib_buffer_enqueue_to_thread_inline (
375 vm, node, fqm, buffer_indices, thread_indices, n_packets,
376 drop_on_congestion, 0 /* with_aux */, NULL);
377
378 return n_enq;
379}
380
381u32 __clib_section (".vlib_buffer_enqueue_to_thread_with_aux_fn")
382CLIB_MULTIARCH_FN (vlib_buffer_enqueue_to_thread_with_aux_fn)
383(vlib_main_t *vm, vlib_node_runtime_t *node, u32 frame_queue_index,
384 u32 *buffer_indices, u32 *aux, u16 *thread_indices, u32 n_packets,
385 int drop_on_congestion)
386{
387 vlib_thread_main_t *tm = vlib_get_thread_main ();
388 vlib_frame_queue_main_t *fqm;
389 u32 n_enq = 0;
390
391 fqm = vec_elt_at_index (tm->frame_queue_mains, frame_queue_index);
392
393 while (n_packets >= VLIB_FRAME_SIZE)
394 {
395 n_enq += vlib_buffer_enqueue_to_thread_inline (
396 vm, node, fqm, buffer_indices, thread_indices, VLIB_FRAME_SIZE,
397 drop_on_congestion, 1 /* with_aux */, aux);
398 buffer_indices += VLIB_FRAME_SIZE;
399 thread_indices += VLIB_FRAME_SIZE;
400 n_packets -= VLIB_FRAME_SIZE;
401 }
402
403 if (n_packets == 0)
404 return n_enq;
405
406 n_enq += vlib_buffer_enqueue_to_thread_inline (
407 vm, node, fqm, buffer_indices, thread_indices, n_packets,
408 drop_on_congestion, 1 /* with_aux */, aux);
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200409
410 return n_enq;
411}
412
Damjan Marion1c229712021-04-21 12:55:15 +0200413CLIB_MARCH_FN_REGISTRATION (vlib_buffer_enqueue_to_thread_fn);
Mohammed Hawarie7149262022-05-18 10:08:47 +0200414CLIB_MARCH_FN_REGISTRATION (vlib_buffer_enqueue_to_thread_with_aux_fn);
Damjan Marion1c229712021-04-21 12:55:15 +0200415
Mohammed Hawarie7149262022-05-18 10:08:47 +0200416static_always_inline u32
417vlib_frame_queue_dequeue_inline (vlib_main_t *vm, vlib_frame_queue_main_t *fqm,
418 u8 with_aux)
Damjan Marioneee099e2021-05-01 14:56:13 +0200419{
420 u32 thread_id = vm->thread_index;
421 vlib_frame_queue_t *fq = fqm->vlib_frame_queues[thread_id];
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200422 u32 mask = fq->nelts - 1;
Damjan Marioneee099e2021-05-01 14:56:13 +0200423 vlib_frame_queue_elt_t *elt;
Mohammed Hawarie7149262022-05-18 10:08:47 +0200424 u32 n_free, n_copy, *from, *from_aux, *to = 0, *to_aux = 0, processed = 0,
425 vectors = 0;
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200426 vlib_frame_t *f = 0;
Damjan Marioneee099e2021-05-01 14:56:13 +0200427
428 ASSERT (fq);
429 ASSERT (vm == vlib_global_main.vlib_mains[thread_id]);
430
431 if (PREDICT_FALSE (fqm->node_index == ~0))
432 return 0;
433 /*
434 * Gather trace data for frame queues
435 */
436 if (PREDICT_FALSE (fq->trace))
437 {
438 frame_queue_trace_t *fqt;
439 frame_queue_nelt_counter_t *fqh;
440 u32 elix;
441
442 fqt = &fqm->frame_queue_traces[thread_id];
443
444 fqt->nelts = fq->nelts;
445 fqt->head = fq->head;
Damjan Marioneee099e2021-05-01 14:56:13 +0200446 fqt->tail = fq->tail;
447 fqt->threshold = fq->vector_threshold;
448 fqt->n_in_use = fqt->tail - fqt->head;
449 if (fqt->n_in_use >= fqt->nelts)
450 {
451 // if beyond max then use max
452 fqt->n_in_use = fqt->nelts - 1;
453 }
454
455 /* Record the number of elements in use in the histogram */
456 fqh = &fqm->frame_queue_histogram[thread_id];
457 fqh->count[fqt->n_in_use]++;
458
459 /* Record a snapshot of the elements in use */
460 for (elix = 0; elix < fqt->nelts; elix++)
461 {
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200462 elt = fq->elts + ((fq->head + 1 + elix) & (mask));
Damjan Marioneee099e2021-05-01 14:56:13 +0200463 if (1 || elt->valid)
464 {
465 fqt->n_vectors[elix] = elt->n_vectors;
466 }
467 }
468 fqt->written = 1;
469 }
470
471 while (1)
472 {
Damjan Marioneee099e2021-05-01 14:56:13 +0200473 if (fq->head == fq->tail)
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200474 break;
475
476 elt = fq->elts + ((fq->head + 1) & mask);
477
478 if (!__atomic_load_n (&elt->valid, __ATOMIC_ACQUIRE))
479 break;
480
481 from = elt->buffer_index + elt->offset;
Mohammed Hawarie7149262022-05-18 10:08:47 +0200482 if (with_aux)
483 from_aux = elt->aux_data + elt->offset;
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200484 ASSERT (elt->offset + elt->n_vectors <= VLIB_FRAME_SIZE);
485
486 if (f == 0)
Damjan Marioneee099e2021-05-01 14:56:13 +0200487 {
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200488 f = vlib_get_frame_to_node (vm, fqm->node_index);
489 to = vlib_frame_vector_args (f);
Mohammed Hawarie7149262022-05-18 10:08:47 +0200490 if (with_aux)
491 to_aux = vlib_frame_aux_args (f);
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200492 n_free = VLIB_FRAME_SIZE;
Damjan Marioneee099e2021-05-01 14:56:13 +0200493 }
494
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200495 if (elt->maybe_trace)
Damjan Marioneee099e2021-05-01 14:56:13 +0200496 f->frame_flags |= VLIB_NODE_FLAG_TRACE;
497
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200498 n_copy = clib_min (n_free, elt->n_vectors);
Damjan Marioneee099e2021-05-01 14:56:13 +0200499
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200500 vlib_buffer_copy_indices (to, from, n_copy);
501 to += n_copy;
Mohammed Hawarie7149262022-05-18 10:08:47 +0200502 if (with_aux)
503 {
504 vlib_buffer_copy_indices (to_aux, from_aux, n_copy);
505 to_aux += n_copy;
506 }
507
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200508 n_free -= n_copy;
509 vectors += n_copy;
Damjan Marioneee099e2021-05-01 14:56:13 +0200510
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200511 if (n_free == 0)
Damjan Marioneee099e2021-05-01 14:56:13 +0200512 {
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200513 f->n_vectors = VLIB_FRAME_SIZE;
514 vlib_put_frame_to_node (vm, fqm->node_index, f);
515 f = 0;
Damjan Marioneee099e2021-05-01 14:56:13 +0200516 }
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200517
518 if (n_copy < elt->n_vectors)
519 {
520 /* not empty - leave it on the ring */
521 elt->n_vectors -= n_copy;
522 elt->offset += n_copy;
523 }
524 else
525 {
526 /* empty - reset and bump head */
527 u32 sz = STRUCT_OFFSET_OF (vlib_frame_queue_elt_t, end_of_reset);
528 clib_memset (elt, 0, sz);
529 __atomic_store_n (&fq->head, fq->head + 1, __ATOMIC_RELEASE);
530 processed++;
531 }
532
533 /* Limit the number of packets pushed into the graph */
534 if (vectors >= fq->vector_threshold)
535 break;
Damjan Marioneee099e2021-05-01 14:56:13 +0200536 }
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200537
538 if (f)
539 {
540 f->n_vectors = VLIB_FRAME_SIZE - n_free;
541 vlib_put_frame_to_node (vm, fqm->node_index, f);
542 }
543
Damjan Marioneee099e2021-05-01 14:56:13 +0200544 return processed;
545}
Damjan Marionc0d9ca72021-05-11 09:39:24 +0200546
Mohammed Hawarie7149262022-05-18 10:08:47 +0200547u32 __clib_section (".vlib_frame_queue_dequeue_fn")
548CLIB_MULTIARCH_FN (vlib_frame_queue_dequeue_fn)
549(vlib_main_t *vm, vlib_frame_queue_main_t *fqm)
550{
551 return vlib_frame_queue_dequeue_inline (vm, fqm, 0 /* with_aux */);
552}
553
Damjan Marioneee099e2021-05-01 14:56:13 +0200554CLIB_MARCH_FN_REGISTRATION (vlib_frame_queue_dequeue_fn);
555
Mohammed Hawarie7149262022-05-18 10:08:47 +0200556u32 __clib_section (".vlib_frame_queue_dequeue_with_aux_fn")
557CLIB_MULTIARCH_FN (vlib_frame_queue_dequeue_with_aux_fn)
558(vlib_main_t *vm, vlib_frame_queue_main_t *fqm)
559{
560 return vlib_frame_queue_dequeue_inline (vm, fqm, 1 /* with_aux */);
561}
562
563CLIB_MARCH_FN_REGISTRATION (vlib_frame_queue_dequeue_with_aux_fn);
564
Damjan Marion1c229712021-04-21 12:55:15 +0200565#ifndef CLIB_MARCH_VARIANT
566vlib_buffer_func_main_t vlib_buffer_func_main;
567
568static clib_error_t *
569vlib_buffer_funcs_init (vlib_main_t *vm)
570{
571 vlib_buffer_func_main_t *bfm = &vlib_buffer_func_main;
572 bfm->buffer_enqueue_to_next_fn =
573 CLIB_MARCH_FN_POINTER (vlib_buffer_enqueue_to_next_fn);
Mohammed Hawaricd758e62022-05-31 18:11:05 +0200574 bfm->buffer_enqueue_to_next_with_aux_fn =
575 CLIB_MARCH_FN_POINTER (vlib_buffer_enqueue_to_next_with_aux_fn);
Damjan Marion1c229712021-04-21 12:55:15 +0200576 bfm->buffer_enqueue_to_single_next_fn =
577 CLIB_MARCH_FN_POINTER (vlib_buffer_enqueue_to_single_next_fn);
Mohammed Hawaricd758e62022-05-31 18:11:05 +0200578 bfm->buffer_enqueue_to_single_next_with_aux_fn =
579 CLIB_MARCH_FN_POINTER (vlib_buffer_enqueue_to_single_next_with_aux_fn);
Damjan Marion1c229712021-04-21 12:55:15 +0200580 bfm->buffer_enqueue_to_thread_fn =
581 CLIB_MARCH_FN_POINTER (vlib_buffer_enqueue_to_thread_fn);
Mohammed Hawarie7149262022-05-18 10:08:47 +0200582 bfm->buffer_enqueue_to_thread_with_aux_fn =
583 CLIB_MARCH_FN_POINTER (vlib_buffer_enqueue_to_thread_with_aux_fn);
Damjan Marion1c229712021-04-21 12:55:15 +0200584 return 0;
585}
586
587VLIB_INIT_FUNCTION (vlib_buffer_funcs_init);
588#endif