vppinfra: add universal array mask_compare and compress funcs
[vpp.git] / src / vlib / buffer_funcs.c
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright(c) 2021 Cisco Systems, Inc.
3  */
4
5 #include <vppinfra/clib.h>
6 #include <vlib/vlib.h>
7 #include <vppinfra/vector_funcs.h>
8
9 static_always_inline u32
10 enqueue_one (vlib_main_t *vm, vlib_node_runtime_t *node, u64 *used_elt_bmp,
11              u16 next_index, u32 *buffers, u16 *nexts, u32 n_buffers,
12              u32 n_left, u32 *tmp)
13 {
14   u64 match_bmp[VLIB_FRAME_SIZE / 64];
15   vlib_frame_t *f;
16   u32 n_extracted, n_free;
17   u32 *to;
18
19   f = vlib_get_next_frame_internal (vm, node, next_index, 0);
20
21   n_free = VLIB_FRAME_SIZE - f->n_vectors;
22
23   /* if frame contains enough space for worst case scenario, we can avoid
24    * use of tmp */
25   if (n_free >= n_left)
26     to = (u32 *) vlib_frame_vector_args (f) + f->n_vectors;
27   else
28     to = tmp;
29
30   clib_mask_compare_u16 (next_index, nexts, match_bmp, n_buffers);
31
32   n_extracted = clib_compress_u32 (to, buffers, match_bmp, n_buffers);
33
34   for (int i = 0; i < ARRAY_LEN (match_bmp); i++)
35     used_elt_bmp[i] |= match_bmp[i];
36
37   if (to != tmp)
38     {
39       /* indices already written to frame, just close it */
40       vlib_put_next_frame (vm, node, next_index, n_free - n_extracted);
41     }
42   else if (n_free >= n_extracted)
43     {
44       /* enough space in the existing frame */
45       to = (u32 *) vlib_frame_vector_args (f) + f->n_vectors;
46       vlib_buffer_copy_indices (to, tmp, n_extracted);
47       vlib_put_next_frame (vm, node, next_index, n_free - n_extracted);
48     }
49   else
50     {
51       /* full frame */
52       to = (u32 *) vlib_frame_vector_args (f) + f->n_vectors;
53       vlib_buffer_copy_indices (to, tmp, n_free);
54       vlib_put_next_frame (vm, node, next_index, 0);
55
56       /* second frame */
57       u32 n_2nd_frame = n_extracted - n_free;
58       f = vlib_get_next_frame_internal (vm, node, next_index, 1);
59       to = vlib_frame_vector_args (f);
60       vlib_buffer_copy_indices (to, tmp + n_free, n_2nd_frame);
61       vlib_put_next_frame (vm, node, next_index,
62                            VLIB_FRAME_SIZE - n_2nd_frame);
63     }
64
65   return n_left - n_extracted;
66 }
67
68 void __clib_section (".vlib_buffer_enqueue_to_next_fn")
69 CLIB_MULTIARCH_FN (vlib_buffer_enqueue_to_next_fn)
70 (vlib_main_t *vm, vlib_node_runtime_t *node, u32 *buffers, u16 *nexts,
71  uword count)
72 {
73   u32 tmp[VLIB_FRAME_SIZE];
74   u32 n_left;
75   u16 next_index;
76
77   while (count >= VLIB_FRAME_SIZE)
78     {
79       u64 used_elt_bmp[VLIB_FRAME_SIZE / 64] = {};
80       n_left = VLIB_FRAME_SIZE;
81       u32 off = 0;
82
83       next_index = nexts[0];
84       n_left = enqueue_one (vm, node, used_elt_bmp, next_index, buffers, nexts,
85                             VLIB_FRAME_SIZE, n_left, tmp);
86
87       while (n_left)
88         {
89           while (PREDICT_FALSE (used_elt_bmp[off] == ~0))
90             off++;
91
92           next_index =
93             nexts[off * 64 + count_trailing_zeros (~used_elt_bmp[off])];
94           n_left = enqueue_one (vm, node, used_elt_bmp, next_index, buffers,
95                                 nexts, VLIB_FRAME_SIZE, n_left, tmp);
96         }
97
98       buffers += VLIB_FRAME_SIZE;
99       nexts += VLIB_FRAME_SIZE;
100       count -= VLIB_FRAME_SIZE;
101     }
102
103   if (count)
104     {
105       u64 used_elt_bmp[VLIB_FRAME_SIZE / 64] = {};
106       next_index = nexts[0];
107       n_left = count;
108       u32 off = 0;
109
110       n_left = enqueue_one (vm, node, used_elt_bmp, next_index, buffers, nexts,
111                             count, n_left, tmp);
112
113       while (n_left)
114         {
115           while (PREDICT_FALSE (used_elt_bmp[off] == ~0))
116             off++;
117
118           next_index =
119             nexts[off * 64 + count_trailing_zeros (~used_elt_bmp[off])];
120           n_left = enqueue_one (vm, node, used_elt_bmp, next_index, buffers,
121                                 nexts, count, n_left, tmp);
122         }
123     }
124 }
125
126 CLIB_MARCH_FN_REGISTRATION (vlib_buffer_enqueue_to_next_fn);
127
128 void __clib_section (".vlib_buffer_enqueue_to_single_next_fn")
129 CLIB_MULTIARCH_FN (vlib_buffer_enqueue_to_single_next_fn)
130 (vlib_main_t *vm, vlib_node_runtime_t *node, u32 *buffers, u16 next_index,
131  u32 count)
132 {
133   u32 *to_next, n_left_to_next, n_enq;
134
135   vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
136
137   if (PREDICT_TRUE (n_left_to_next >= count))
138     {
139       vlib_buffer_copy_indices (to_next, buffers, count);
140       n_left_to_next -= count;
141       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
142       return;
143     }
144
145   n_enq = n_left_to_next;
146 next:
147   vlib_buffer_copy_indices (to_next, buffers, n_enq);
148   n_left_to_next -= n_enq;
149
150   if (PREDICT_FALSE (count > n_enq))
151     {
152       count -= n_enq;
153       buffers += n_enq;
154
155       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
156       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
157       n_enq = clib_min (n_left_to_next, count);
158       goto next;
159     }
160   vlib_put_next_frame (vm, node, next_index, n_left_to_next);
161 }
162 CLIB_MARCH_FN_REGISTRATION (vlib_buffer_enqueue_to_single_next_fn);
163
164 u32 __clib_section (".vlib_buffer_enqueue_to_thread_fn")
165 CLIB_MULTIARCH_FN (vlib_buffer_enqueue_to_thread_fn)
166 (vlib_main_t *vm, u32 frame_queue_index, u32 *buffer_indices,
167  u16 *thread_indices, u32 n_packets, int drop_on_congestion)
168 {
169   vlib_thread_main_t *tm = vlib_get_thread_main ();
170   vlib_frame_queue_main_t *fqm;
171   vlib_frame_queue_per_thread_data_t *ptd;
172   u32 n_left = n_packets;
173   u32 drop_list[VLIB_FRAME_SIZE], *dbi = drop_list, n_drop = 0;
174   vlib_frame_queue_elt_t *hf = 0;
175   u32 n_left_to_next_thread = 0, *to_next_thread = 0;
176   u32 next_thread_index, current_thread_index = ~0;
177   int i;
178
179   fqm = vec_elt_at_index (tm->frame_queue_mains, frame_queue_index);
180   ptd = vec_elt_at_index (fqm->per_thread_data, vm->thread_index);
181
182   while (n_left)
183     {
184       next_thread_index = thread_indices[0];
185
186       if (next_thread_index != current_thread_index)
187         {
188           if (drop_on_congestion &&
189               is_vlib_frame_queue_congested (
190                 frame_queue_index, next_thread_index, fqm->queue_hi_thresh,
191                 ptd->congested_handoff_queue_by_thread_index))
192             {
193               dbi[0] = buffer_indices[0];
194               dbi++;
195               n_drop++;
196               goto next;
197             }
198
199           if (hf)
200             hf->n_vectors = VLIB_FRAME_SIZE - n_left_to_next_thread;
201
202           hf = vlib_get_worker_handoff_queue_elt (
203             frame_queue_index, next_thread_index,
204             ptd->handoff_queue_elt_by_thread_index);
205
206           n_left_to_next_thread = VLIB_FRAME_SIZE - hf->n_vectors;
207           to_next_thread = &hf->buffer_index[hf->n_vectors];
208           current_thread_index = next_thread_index;
209         }
210
211       to_next_thread[0] = buffer_indices[0];
212       to_next_thread++;
213       n_left_to_next_thread--;
214
215       if (n_left_to_next_thread == 0)
216         {
217           hf->n_vectors = VLIB_FRAME_SIZE;
218           vlib_put_frame_queue_elt (hf);
219           vlib_get_main_by_index (current_thread_index)->check_frame_queues =
220             1;
221           current_thread_index = ~0;
222           ptd->handoff_queue_elt_by_thread_index[next_thread_index] = 0;
223           hf = 0;
224         }
225
226       /* next */
227     next:
228       thread_indices += 1;
229       buffer_indices += 1;
230       n_left -= 1;
231     }
232
233   if (hf)
234     hf->n_vectors = VLIB_FRAME_SIZE - n_left_to_next_thread;
235
236   /* Ship frames to the thread nodes */
237   for (i = 0; i < vec_len (ptd->handoff_queue_elt_by_thread_index); i++)
238     {
239       if (ptd->handoff_queue_elt_by_thread_index[i])
240         {
241           hf = ptd->handoff_queue_elt_by_thread_index[i];
242           /*
243            * It works better to let the handoff node
244            * rate-adapt, always ship the handoff queue element.
245            */
246           if (1 || hf->n_vectors == hf->last_n_vectors)
247             {
248               vlib_put_frame_queue_elt (hf);
249               vlib_get_main_by_index (i)->check_frame_queues = 1;
250               ptd->handoff_queue_elt_by_thread_index[i] = 0;
251             }
252           else
253             hf->last_n_vectors = hf->n_vectors;
254         }
255       ptd->congested_handoff_queue_by_thread_index[i] =
256         (vlib_frame_queue_t *) (~0);
257     }
258
259   if (drop_on_congestion && n_drop)
260     vlib_buffer_free (vm, drop_list, n_drop);
261
262   return n_packets - n_drop;
263 }
264
265 CLIB_MARCH_FN_REGISTRATION (vlib_buffer_enqueue_to_thread_fn);
266
267 /*
268  * Check the frame queue to see if any frames are available.
269  * If so, pull the packets off the frames and put them to
270  * the handoff node.
271  */
272 u32 __clib_section (".vlib_frame_queue_dequeue_fn")
273 CLIB_MULTIARCH_FN (vlib_frame_queue_dequeue_fn)
274 (vlib_main_t *vm, vlib_frame_queue_main_t *fqm)
275 {
276   u32 thread_id = vm->thread_index;
277   vlib_frame_queue_t *fq = fqm->vlib_frame_queues[thread_id];
278   vlib_frame_queue_elt_t *elt;
279   u32 *from, *to;
280   vlib_frame_t *f;
281   int msg_type;
282   int processed = 0;
283   u32 vectors = 0;
284
285   ASSERT (fq);
286   ASSERT (vm == vlib_global_main.vlib_mains[thread_id]);
287
288   if (PREDICT_FALSE (fqm->node_index == ~0))
289     return 0;
290   /*
291    * Gather trace data for frame queues
292    */
293   if (PREDICT_FALSE (fq->trace))
294     {
295       frame_queue_trace_t *fqt;
296       frame_queue_nelt_counter_t *fqh;
297       u32 elix;
298
299       fqt = &fqm->frame_queue_traces[thread_id];
300
301       fqt->nelts = fq->nelts;
302       fqt->head = fq->head;
303       fqt->head_hint = fq->head_hint;
304       fqt->tail = fq->tail;
305       fqt->threshold = fq->vector_threshold;
306       fqt->n_in_use = fqt->tail - fqt->head;
307       if (fqt->n_in_use >= fqt->nelts)
308         {
309           // if beyond max then use max
310           fqt->n_in_use = fqt->nelts - 1;
311         }
312
313       /* Record the number of elements in use in the histogram */
314       fqh = &fqm->frame_queue_histogram[thread_id];
315       fqh->count[fqt->n_in_use]++;
316
317       /* Record a snapshot of the elements in use */
318       for (elix = 0; elix < fqt->nelts; elix++)
319         {
320           elt = fq->elts + ((fq->head + 1 + elix) & (fq->nelts - 1));
321           if (1 || elt->valid)
322             {
323               fqt->n_vectors[elix] = elt->n_vectors;
324             }
325         }
326       fqt->written = 1;
327     }
328
329   while (1)
330     {
331       vlib_buffer_t *b;
332       if (fq->head == fq->tail)
333         {
334           fq->head_hint = fq->head;
335           return processed;
336         }
337
338       elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1));
339
340       if (!elt->valid)
341         {
342           fq->head_hint = fq->head;
343           return processed;
344         }
345
346       from = elt->buffer_index;
347       msg_type = elt->msg_type;
348
349       ASSERT (msg_type == VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME);
350       ASSERT (elt->n_vectors <= VLIB_FRAME_SIZE);
351
352       f = vlib_get_frame_to_node (vm, fqm->node_index);
353
354       /* If the first vector is traced, set the frame trace flag */
355       b = vlib_get_buffer (vm, from[0]);
356       if (b->flags & VLIB_BUFFER_IS_TRACED)
357         f->frame_flags |= VLIB_NODE_FLAG_TRACE;
358
359       to = vlib_frame_vector_args (f);
360
361       vlib_buffer_copy_indices (to, from, elt->n_vectors);
362
363       vectors += elt->n_vectors;
364       f->n_vectors = elt->n_vectors;
365       vlib_put_frame_to_node (vm, fqm->node_index, f);
366
367       elt->valid = 0;
368       elt->n_vectors = 0;
369       elt->msg_type = 0xfefefefe;
370       CLIB_MEMORY_BARRIER ();
371       fq->head++;
372       processed++;
373
374       /*
375        * Limit the number of packets pushed into the graph
376        */
377       if (vectors >= fq->vector_threshold)
378         {
379           fq->head_hint = fq->head;
380           return processed;
381         }
382     }
383   ASSERT (0);
384   return processed;
385 }
386 CLIB_MARCH_FN_REGISTRATION (vlib_frame_queue_dequeue_fn);
387
388 #ifndef CLIB_MARCH_VARIANT
389 vlib_buffer_func_main_t vlib_buffer_func_main;
390
391 static clib_error_t *
392 vlib_buffer_funcs_init (vlib_main_t *vm)
393 {
394   vlib_buffer_func_main_t *bfm = &vlib_buffer_func_main;
395   bfm->buffer_enqueue_to_next_fn =
396     CLIB_MARCH_FN_POINTER (vlib_buffer_enqueue_to_next_fn);
397   bfm->buffer_enqueue_to_single_next_fn =
398     CLIB_MARCH_FN_POINTER (vlib_buffer_enqueue_to_single_next_fn);
399   bfm->buffer_enqueue_to_thread_fn =
400     CLIB_MARCH_FN_POINTER (vlib_buffer_enqueue_to_thread_fn);
401   bfm->frame_queue_dequeue_fn =
402     CLIB_MARCH_FN_POINTER (vlib_frame_queue_dequeue_fn);
403   return 0;
404 }
405
406 VLIB_INIT_FUNCTION (vlib_buffer_funcs_init);
407 #endif