blob: 89f212374e9123494ddf6c576e24e8ee9d8cfb71 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15/*
16 * node_funcs.h: processing nodes global functions/inlines
17 *
18 * Copyright (c) 2008 Eliot Dresselhaus
19 *
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
27 *
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
30 *
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 */
39
Dave Barach9b8ffd92016-07-08 08:13:45 -040040/** \file
Dave Barach9770e202016-07-06 10:29:27 -040041 vlib node functions
42*/
43
44
Ed Warnickecb9cada2015-12-08 15:45:58 -070045#ifndef included_vlib_node_funcs_h
46#define included_vlib_node_funcs_h
47
48#include <vppinfra/fifo.h>
Dave Barach5c20a012017-06-13 08:48:31 -040049#include <vppinfra/tw_timer_1t_3w_1024sl_ov.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070050
Damjan Marioncea46522020-05-21 16:47:05 +020051#ifdef CLIB_SANITIZE_ADDR
52#include <sanitizer/asan_interface.h>
53#endif
54
55static_always_inline void
56vlib_process_start_switch_stack (vlib_main_t * vm, vlib_process_t * p)
57{
58#ifdef CLIB_SANITIZE_ADDR
59 void *stack = p ? (void *) p->stack : vlib_thread_stacks[vm->thread_index];
60 u32 stack_bytes = p ? p->log2_n_stack_bytes : VLIB_THREAD_STACK_SIZE;
61 __sanitizer_start_switch_fiber (&vm->asan_stack_save, stack, stack_bytes);
62#endif
63}
64
65static_always_inline void
66vlib_process_finish_switch_stack (vlib_main_t * vm)
67{
68#ifdef CLIB_SANITIZE_ADDR
69 const void *bottom_old;
70 size_t size_old;
71
72 __sanitizer_finish_switch_fiber (&vm->asan_stack_save, &bottom_old,
73 &size_old);
74#endif
75}
76
Dave Barach9770e202016-07-06 10:29:27 -040077/** \brief Get vlib node by index.
78 @warning This function will ASSERT if @c i is out of range.
79 @param vm vlib_main_t pointer, varies by thread
80 @param i node index.
81 @return pointer to the requested vlib_node_t.
82*/
83
Ed Warnickecb9cada2015-12-08 15:45:58 -070084always_inline vlib_node_t *
85vlib_get_node (vlib_main_t * vm, u32 i)
Dave Barach9b8ffd92016-07-08 08:13:45 -040086{
87 return vec_elt (vm->node_main.nodes, i);
88}
Ed Warnickecb9cada2015-12-08 15:45:58 -070089
Dave Barach9770e202016-07-06 10:29:27 -040090/** \brief Get vlib node by graph arc (next) index.
91 @param vm vlib_main_t pointer, varies by thread
92 @param node_index index of original node
93 @param next_index graph arc index
94 @return pointer to the vlib_node_t at the end of the indicated arc
95*/
96
Ed Warnickecb9cada2015-12-08 15:45:58 -070097always_inline vlib_node_t *
98vlib_get_next_node (vlib_main_t * vm, u32 node_index, u32 next_index)
99{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400100 vlib_node_main_t *nm = &vm->node_main;
101 vlib_node_t *n;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102
103 n = vec_elt (nm->nodes, node_index);
104 ASSERT (next_index < vec_len (n->next_nodes));
105 return vlib_get_node (vm, n->next_nodes[next_index]);
106}
107
Dave Barach9770e202016-07-06 10:29:27 -0400108/** \brief Get node runtime by node index.
109 @param vm vlib_main_t pointer, varies by thread
110 @param node_index index of node
111 @return pointer to the indicated vlib_node_runtime_t
112*/
113
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114always_inline vlib_node_runtime_t *
115vlib_node_get_runtime (vlib_main_t * vm, u32 node_index)
116{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400117 vlib_node_main_t *nm = &vm->node_main;
118 vlib_node_t *n = vec_elt (nm->nodes, node_index);
119 vlib_process_t *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700120 if (n->type != VLIB_NODE_TYPE_PROCESS)
121 return vec_elt_at_index (nm->nodes_by_type[n->type], n->runtime_index);
122 else
123 {
124 p = vec_elt (nm->processes, n->runtime_index);
125 return &p->node_runtime;
126 }
127}
128
Dave Barach9770e202016-07-06 10:29:27 -0400129/** \brief Get node runtime private data by node index.
130 @param vm vlib_main_t pointer, varies by thread
131 @param node_index index of the node
132 @return pointer to the indicated vlib_node_runtime_t private data
133*/
134
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135always_inline void *
136vlib_node_get_runtime_data (vlib_main_t * vm, u32 node_index)
137{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400138 vlib_node_runtime_t *r = vlib_node_get_runtime (vm, node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139 return r->runtime_data;
140}
141
Dave Barach9770e202016-07-06 10:29:27 -0400142/** \brief Set node runtime private data.
143 @param vm vlib_main_t pointer, varies by thread
144 @param node_index index of the node
145 @param runtime_data arbitrary runtime private data
146 @param n_runtime_data_bytes size of runtime private data
147*/
148
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149always_inline void
150vlib_node_set_runtime_data (vlib_main_t * vm, u32 node_index,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400151 void *runtime_data, u32 n_runtime_data_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400153 vlib_node_t *n = vlib_get_node (vm, node_index);
154 vlib_node_runtime_t *r = vlib_node_get_runtime (vm, node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155
156 n->runtime_data_bytes = n_runtime_data_bytes;
157 vec_free (n->runtime_data);
158 vec_add (n->runtime_data, runtime_data, n_runtime_data_bytes);
159
Damjan Marioneb90b7f2016-11-01 01:26:01 +0100160 ASSERT (vec_len (n->runtime_data) <= sizeof (vlib_node_runtime_t) -
161 STRUCT_OFFSET_OF (vlib_node_runtime_t, runtime_data));
162
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163 if (vec_len (n->runtime_data) > 0)
Dave Barach178cf492018-11-13 16:34:13 -0500164 clib_memcpy_fast (r->runtime_data, n->runtime_data,
165 vec_len (n->runtime_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166}
167
Dave Barach9770e202016-07-06 10:29:27 -0400168/** \brief Set node dispatch state.
169 @param vm vlib_main_t pointer, varies by thread
170 @param node_index index of the node
171 @param new_state new state for node, see vlib_node_state_t
172*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -0400174vlib_node_set_state (vlib_main_t * vm, u32 node_index,
175 vlib_node_state_t new_state)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700176{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400177 vlib_node_main_t *nm = &vm->node_main;
178 vlib_node_t *n;
179 vlib_node_runtime_t *r;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700180
181 n = vec_elt (nm->nodes, node_index);
182 if (n->type == VLIB_NODE_TYPE_PROCESS)
183 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400184 vlib_process_t *p = vec_elt (nm->processes, n->runtime_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185 r = &p->node_runtime;
186
187 /* When disabling make sure flags are cleared. */
188 p->flags &= ~(VLIB_PROCESS_RESUME_PENDING
189 | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK
190 | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT);
191 }
192 else
193 r = vec_elt_at_index (nm->nodes_by_type[n->type], n->runtime_index);
194
195 ASSERT (new_state < VLIB_N_NODE_STATE);
196
197 if (n->type == VLIB_NODE_TYPE_INPUT)
198 {
199 ASSERT (nm->input_node_counts_by_state[n->state] > 0);
200 nm->input_node_counts_by_state[n->state] -= 1;
201 nm->input_node_counts_by_state[new_state] += 1;
202 }
203
204 n->state = new_state;
205 r->state = new_state;
206}
207
Damjan Marion153646e2017-04-05 18:15:45 +0200208/** \brief Get node dispatch state.
209 @param vm vlib_main_t pointer, varies by thread
210 @param node_index index of the node
211 @return state for node, see vlib_node_state_t
212*/
213always_inline vlib_node_state_t
214vlib_node_get_state (vlib_main_t * vm, u32 node_index)
215{
216 vlib_node_main_t *nm = &vm->node_main;
217 vlib_node_t *n;
218 n = vec_elt (nm->nodes, node_index);
219 return n->state;
220}
221
Ed Warnickecb9cada2015-12-08 15:45:58 -0700222always_inline void
Damjan Marion1033b492020-06-03 12:20:41 +0200223vlib_node_set_interrupt_pending_with_data (vlib_main_t * vm, u32 node_index,
224 u32 data)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700225{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400226 vlib_node_main_t *nm = &vm->node_main;
227 vlib_node_t *n = vec_elt (nm->nodes, node_index);
Damjan Marion1033b492020-06-03 12:20:41 +0200228 vlib_node_interrupt_t *i;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700229 ASSERT (n->type == VLIB_NODE_TYPE_INPUT);
Damjan Marion1033b492020-06-03 12:20:41 +0200230
231 if (vm == vlib_get_main ())
232 {
233 /* local thread */
234 vec_add2 (nm->pending_local_interrupts, i, 1);
235 i->node_runtime_index = n->runtime_index;
236 i->data = data;
237 }
238 else
239 {
240 /* remote thread */
241 clib_spinlock_lock (&nm->pending_interrupt_lock);
242 vec_add2 (nm->pending_remote_interrupts, i, 1);
243 i->node_runtime_index = n->runtime_index;
244 i->data = data;
Benoît Ganned25147d2020-06-30 18:17:06 +0200245 *nm->pending_remote_interrupts_notify = 1;
Damjan Marion1033b492020-06-03 12:20:41 +0200246 clib_spinlock_unlock (&nm->pending_interrupt_lock);
247 }
248}
249
250always_inline void
251vlib_node_set_interrupt_pending (vlib_main_t * vm, u32 node_index)
252{
253 vlib_node_set_interrupt_pending_with_data (vm, node_index, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700254}
255
256always_inline vlib_process_t *
257vlib_get_process_from_node (vlib_main_t * vm, vlib_node_t * node)
258{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400259 vlib_node_main_t *nm = &vm->node_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700260 ASSERT (node->type == VLIB_NODE_TYPE_PROCESS);
261 return vec_elt (nm->processes, node->runtime_index);
262}
263
Ed Warnickecb9cada2015-12-08 15:45:58 -0700264always_inline vlib_frame_t *
Andreas Schultz58b2eb12019-07-15 15:40:56 +0200265vlib_get_frame (vlib_main_t * vm, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700266{
Andreas Schultz58b2eb12019-07-15 15:40:56 +0200267 ASSERT (f != NULL);
Damjan Marion633b6fd2018-09-14 14:38:53 +0200268 ASSERT (f->frame_flags & VLIB_FRAME_IS_ALLOCATED);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700269 return f;
270}
271
Damjan Marion296988d2019-02-21 20:24:54 +0100272always_inline void
273vlib_frame_no_append (vlib_frame_t * f)
274{
275 f->frame_flags |= VLIB_FRAME_NO_APPEND;
276}
277
Ed Warnickecb9cada2015-12-08 15:45:58 -0700278/* Byte alignment for vector arguments. */
279#define VLIB_FRAME_VECTOR_ALIGN (1 << 4)
280
281always_inline u32
282vlib_frame_vector_byte_offset (u32 scalar_size)
283{
284 return round_pow2 (sizeof (vlib_frame_t) + scalar_size,
285 VLIB_FRAME_VECTOR_ALIGN);
286}
287
Dave Barach9770e202016-07-06 10:29:27 -0400288/** \brief Get pointer to frame vector data.
289 @param f vlib_frame_t pointer
290 @return pointer to first vector element in frame
291*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700292always_inline void *
293vlib_frame_vector_args (vlib_frame_t * f)
294{
295 return (void *) f + vlib_frame_vector_byte_offset (f->scalar_size);
296}
297
Dave Barach9770e202016-07-06 10:29:27 -0400298/** \brief Get pointer to frame scalar data.
299
Dave Barach9770e202016-07-06 10:29:27 -0400300 @param f vlib_frame_t pointer
301
302 @return arbitrary node scalar data
303
304 @sa vlib_frame_vector_args
305*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700306always_inline void *
Damjan Mariona3d59862018-11-10 10:23:00 +0100307vlib_frame_scalar_args (vlib_frame_t * f)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400308{
309 return vlib_frame_vector_args (f) - f->scalar_size;
310}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700311
312always_inline vlib_next_frame_t *
313vlib_node_runtime_get_next_frame (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400314 vlib_node_runtime_t * n, u32 next_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700315{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400316 vlib_node_main_t *nm = &vm->node_main;
317 vlib_next_frame_t *nf;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700318
319 ASSERT (next_index < n->n_next_nodes);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400320 nf = vec_elt_at_index (nm->next_frames, n->next_frame_index + next_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700321
322 if (CLIB_DEBUG > 0)
323 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400324 vlib_node_t *node, *next;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700325 node = vec_elt (nm->nodes, n->node_index);
326 next = vec_elt (nm->nodes, node->next_nodes[next_index]);
327 ASSERT (nf->node_runtime_index == next->runtime_index);
328 }
329
330 return nf;
331}
332
Dave Barach9770e202016-07-06 10:29:27 -0400333/** \brief Get pointer to frame by (@c node_index, @c next_index).
334
335 @warning This is not a function that you should call directly.
336 See @ref vlib_get_next_frame instead.
337
338 @param vm vlib_main_t pointer, varies by thread
339 @param node_index index of the node
340 @param next_index graph arc index
341
342 @return pointer to the requested vlib_next_frame_t
343
344 @sa vlib_get_next_frame
345*/
346
Ed Warnickecb9cada2015-12-08 15:45:58 -0700347always_inline vlib_next_frame_t *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400348vlib_node_get_next_frame (vlib_main_t * vm, u32 node_index, u32 next_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700349{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400350 vlib_node_main_t *nm = &vm->node_main;
351 vlib_node_t *n;
352 vlib_node_runtime_t *r;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700353
354 n = vec_elt (nm->nodes, node_index);
355 r = vec_elt_at_index (nm->nodes_by_type[n->type], n->runtime_index);
356 return vlib_node_runtime_get_next_frame (vm, r, next_index);
357}
358
Dave Barach9b8ffd92016-07-08 08:13:45 -0400359vlib_frame_t *vlib_get_next_frame_internal (vlib_main_t * vm,
360 vlib_node_runtime_t * node,
361 u32 next_index,
362 u32 alloc_new_frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700363
364#define vlib_get_next_frame_macro(vm,node,next_index,vectors,n_vectors_left,alloc_new_frame) \
365do { \
366 vlib_frame_t * _f \
367 = vlib_get_next_frame_internal ((vm), (node), (next_index), \
368 (alloc_new_frame)); \
369 u32 _n = _f->n_vectors; \
370 (vectors) = vlib_frame_vector_args (_f) + _n * sizeof ((vectors)[0]); \
371 (n_vectors_left) = VLIB_FRAME_SIZE - _n; \
372} while (0)
373
Dave Barach9770e202016-07-06 10:29:27 -0400374
Dave Barach9b8ffd92016-07-08 08:13:45 -0400375/** \brief Get pointer to next frame vector data by
Dave Barach9770e202016-07-06 10:29:27 -0400376 (@c vlib_node_runtime_t, @c next_index).
377 Standard single/dual loop boilerplate element.
378 @attention This is a MACRO, with SIDE EFFECTS.
379
380 @param vm vlib_main_t pointer, varies by thread
381 @param node current node vlib_node_runtime_t pointer
382 @param next_index requested graph arc index
383
384 @return @c vectors -- pointer to next available vector slot
385 @return @c n_vectors_left -- number of vector slots available
386*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700387#define vlib_get_next_frame(vm,node,next_index,vectors,n_vectors_left) \
388 vlib_get_next_frame_macro (vm, node, next_index, \
389 vectors, n_vectors_left, \
390 /* alloc new frame */ 0)
391
392#define vlib_get_new_next_frame(vm,node,next_index,vectors,n_vectors_left) \
393 vlib_get_next_frame_macro (vm, node, next_index, \
394 vectors, n_vectors_left, \
395 /* alloc new frame */ 1)
396
Dave Barach9770e202016-07-06 10:29:27 -0400397/** \brief Release pointer to next frame vector data.
398 Standard single/dual loop boilerplate element.
399 @param vm vlib_main_t pointer, varies by thread
400 @param r current node vlib_node_runtime_t pointer
401 @param next_index graph arc index
402 @param n_packets_left number of slots still available in vector
403*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700404void
405vlib_put_next_frame (vlib_main_t * vm,
406 vlib_node_runtime_t * r,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400407 u32 next_index, u32 n_packets_left);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700408
409/* Combination get plus put. Returns vector argument just added. */
410#define vlib_set_next_frame(vm,node,next_index,v) \
411({ \
412 uword _n_left; \
413 vlib_get_next_frame ((vm), (node), (next_index), (v), _n_left); \
414 ASSERT (_n_left > 0); \
415 vlib_put_next_frame ((vm), (node), (next_index), _n_left - 1); \
416 (v); \
417})
418
419always_inline void
420vlib_set_next_frame_buffer (vlib_main_t * vm,
421 vlib_node_runtime_t * node,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400422 u32 next_index, u32 buffer_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400424 u32 *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700425 p = vlib_set_next_frame (vm, node, next_index, p);
426 p[0] = buffer_index;
427}
428
Dave Barach9b8ffd92016-07-08 08:13:45 -0400429vlib_frame_t *vlib_get_frame_to_node (vlib_main_t * vm, u32 to_node_index);
430void vlib_put_frame_to_node (vlib_main_t * vm, u32 to_node_index,
431 vlib_frame_t * f);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700432
Ed Warnickecb9cada2015-12-08 15:45:58 -0700433always_inline uword
434vlib_in_process_context (vlib_main_t * vm)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400435{
436 return vm->node_main.current_process_index != ~0;
437}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700438
Florin Coras66b11312017-07-31 17:18:03 -0700439always_inline vlib_process_t *
440vlib_get_current_process (vlib_main_t * vm)
441{
442 vlib_node_main_t *nm = &vm->node_main;
443 if (vlib_in_process_context (vm))
444 return vec_elt (nm->processes, nm->current_process_index);
445 return 0;
446}
447
Ed Warnickecb9cada2015-12-08 15:45:58 -0700448always_inline uword
449vlib_current_process (vlib_main_t * vm)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400450{
451 return vlib_get_current_process (vm)->node_runtime.node_index;
452}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700453
Dave Barach5c20a012017-06-13 08:48:31 -0400454/** Returns TRUE if a process suspend time is less than 10us
Dave Barach2ab470a2016-08-10 18:38:36 -0400455 @param dt - remaining poll time in seconds
Dave Barach5c20a012017-06-13 08:48:31 -0400456 @returns 1 if dt < 10e-6, 0 otherwise
Dave Barach2ab470a2016-08-10 18:38:36 -0400457*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700458always_inline uword
459vlib_process_suspend_time_is_zero (f64 dt)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400460{
Dave Barach5c20a012017-06-13 08:48:31 -0400461 return dt < 10e-6;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400462}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700463
Dave Barach2ab470a2016-08-10 18:38:36 -0400464/** Suspend a vlib cooperative multi-tasking thread for a period of time
465 @param vm - vlib_main_t *
466 @param dt - suspend interval in seconds
467 @returns VLIB_PROCESS_RESUME_LONGJMP_RESUME, routinely ignored
468*/
469
Ed Warnickecb9cada2015-12-08 15:45:58 -0700470always_inline uword
471vlib_process_suspend (vlib_main_t * vm, f64 dt)
472{
473 uword r;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400474 vlib_node_main_t *nm = &vm->node_main;
475 vlib_process_t *p = vec_elt (nm->processes, nm->current_process_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700476
477 if (vlib_process_suspend_time_is_zero (dt))
478 return VLIB_PROCESS_RESUME_LONGJMP_RESUME;
479
480 p->flags |= VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK;
481 r = clib_setjmp (&p->resume_longjmp, VLIB_PROCESS_RESUME_LONGJMP_SUSPEND);
482 if (r == VLIB_PROCESS_RESUME_LONGJMP_SUSPEND)
483 {
Dave Barach5c20a012017-06-13 08:48:31 -0400484 /* expiration time in 10us ticks */
485 p->resume_clock_interval = dt * 1e5;
Damjan Marioncea46522020-05-21 16:47:05 +0200486 vlib_process_start_switch_stack (vm, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700487 clib_longjmp (&p->return_longjmp, VLIB_PROCESS_RETURN_LONGJMP_SUSPEND);
488 }
Damjan Marioncea46522020-05-21 16:47:05 +0200489 else
490 vlib_process_finish_switch_stack (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700491
492 return r;
493}
494
495always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -0400496vlib_process_free_event_type (vlib_process_t * p, uword t,
497 uword is_one_time_event)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700498{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400499 ASSERT (!pool_is_free_index (p->event_type_pool, t));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700500 pool_put_index (p->event_type_pool, t);
501 if (is_one_time_event)
502 p->one_time_event_type_bitmap =
503 clib_bitmap_andnoti (p->one_time_event_type_bitmap, t);
504}
505
506always_inline void
507vlib_process_maybe_free_event_type (vlib_process_t * p, uword t)
508{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400509 ASSERT (!pool_is_free_index (p->event_type_pool, t));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700510 if (clib_bitmap_get (p->one_time_event_type_bitmap, t))
511 vlib_process_free_event_type (p, t, /* is_one_time_event */ 1);
512}
513
514always_inline void *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400515vlib_process_get_event_data (vlib_main_t * vm,
516 uword * return_event_type_opaque)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700517{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400518 vlib_node_main_t *nm = &vm->node_main;
519 vlib_process_t *p;
520 vlib_process_event_type_t *et;
Gabriel Ganne71d73fe2017-02-04 10:51:04 +0100521 uword t;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400522 void *event_data_vector;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700523
524 p = vec_elt (nm->processes, nm->current_process_index);
525
526 /* Find first type with events ready.
527 Return invalid type when there's nothing there. */
528 t = clib_bitmap_first_set (p->non_empty_event_type_bitmap);
529 if (t == ~0)
530 return 0;
531
Dave Barach9b8ffd92016-07-08 08:13:45 -0400532 p->non_empty_event_type_bitmap =
533 clib_bitmap_andnoti (p->non_empty_event_type_bitmap, t);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700534
Gabriel Ganne71d73fe2017-02-04 10:51:04 +0100535 ASSERT (_vec_len (p->pending_event_data_by_type_index[t]) > 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700536 event_data_vector = p->pending_event_data_by_type_index[t];
537 p->pending_event_data_by_type_index[t] = 0;
538
539 et = pool_elt_at_index (p->event_type_pool, t);
540
541 /* Return user's opaque value and possibly index. */
542 *return_event_type_opaque = et->opaque;
543
544 vlib_process_maybe_free_event_type (p, t);
545
546 return event_data_vector;
547}
548
549/* Return event data vector for later reuse. We reuse event data to avoid
550 repeatedly allocating event vectors in cases where we care about speed. */
551always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -0400552vlib_process_put_event_data (vlib_main_t * vm, void *event_data)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700553{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400554 vlib_node_main_t *nm = &vm->node_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700555 vec_add1 (nm->recycled_event_data_vectors, event_data);
556}
557
Dave Barach2ab470a2016-08-10 18:38:36 -0400558/** Return the first event type which has occurred and a vector of per-event
559 data of that type, or a timeout indication
560
561 @param vm - vlib_main_t pointer
562 @param data_vector - pointer to a (uword *) vector to receive event data
563 @returns either an event type and a vector of per-event instance data,
564 or ~0 to indicate a timeout.
565*/
566
Ed Warnickecb9cada2015-12-08 15:45:58 -0700567always_inline uword
568vlib_process_get_events (vlib_main_t * vm, uword ** data_vector)
569{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400570 vlib_node_main_t *nm = &vm->node_main;
571 vlib_process_t *p;
572 vlib_process_event_type_t *et;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700573 uword r, t, l;
574
575 p = vec_elt (nm->processes, nm->current_process_index);
576
577 /* Find first type with events ready.
578 Return invalid type when there's nothing there. */
579 t = clib_bitmap_first_set (p->non_empty_event_type_bitmap);
580 if (t == ~0)
581 return t;
582
Dave Barach9b8ffd92016-07-08 08:13:45 -0400583 p->non_empty_event_type_bitmap =
584 clib_bitmap_andnoti (p->non_empty_event_type_bitmap, t);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700585
586 l = _vec_len (p->pending_event_data_by_type_index[t]);
587 if (data_vector)
588 vec_add (*data_vector, p->pending_event_data_by_type_index[t], l);
589 _vec_len (p->pending_event_data_by_type_index[t]) = 0;
590
591 et = pool_elt_at_index (p->event_type_pool, t);
592
593 /* Return user's opaque value. */
594 r = et->opaque;
595
596 vlib_process_maybe_free_event_type (p, t);
597
598 return r;
599}
600
601always_inline uword
Dave Barach9b8ffd92016-07-08 08:13:45 -0400602vlib_process_get_events_helper (vlib_process_t * p, uword t,
603 uword ** data_vector)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700604{
605 uword l;
606
Dave Barach9b8ffd92016-07-08 08:13:45 -0400607 p->non_empty_event_type_bitmap =
608 clib_bitmap_andnoti (p->non_empty_event_type_bitmap, t);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700609
610 l = _vec_len (p->pending_event_data_by_type_index[t]);
611 if (data_vector)
612 vec_add (*data_vector, p->pending_event_data_by_type_index[t], l);
613 _vec_len (p->pending_event_data_by_type_index[t]) = 0;
614
615 vlib_process_maybe_free_event_type (p, t);
616
617 return l;
618}
619
620/* As above but query as specified type of event. Returns number of
621 events found. */
622always_inline uword
623vlib_process_get_events_with_type (vlib_main_t * vm, uword ** data_vector,
624 uword with_type_opaque)
625{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400626 vlib_node_main_t *nm = &vm->node_main;
627 vlib_process_t *p;
628 uword t, *h;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700629
630 p = vec_elt (nm->processes, nm->current_process_index);
631 h = hash_get (p->event_type_index_by_type_opaque, with_type_opaque);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400632 if (!h)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700633 /* This can happen when an event has not yet been
634 signaled with given opaque type. */
635 return 0;
636
637 t = h[0];
Dave Barach9b8ffd92016-07-08 08:13:45 -0400638 if (!clib_bitmap_get (p->non_empty_event_type_bitmap, t))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700639 return 0;
640
641 return vlib_process_get_events_helper (p, t, data_vector);
642}
643
644always_inline uword *
645vlib_process_wait_for_event (vlib_main_t * vm)
646{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400647 vlib_node_main_t *nm = &vm->node_main;
648 vlib_process_t *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700649 uword r;
650
651 p = vec_elt (nm->processes, nm->current_process_index);
652 if (clib_bitmap_is_zero (p->non_empty_event_type_bitmap))
653 {
654 p->flags |= VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400655 r =
656 clib_setjmp (&p->resume_longjmp, VLIB_PROCESS_RESUME_LONGJMP_SUSPEND);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700657 if (r == VLIB_PROCESS_RESUME_LONGJMP_SUSPEND)
Damjan Marioncea46522020-05-21 16:47:05 +0200658 {
659 vlib_process_start_switch_stack (vm, 0);
660 clib_longjmp (&p->return_longjmp,
661 VLIB_PROCESS_RETURN_LONGJMP_SUSPEND);
662 }
663 else
664 vlib_process_finish_switch_stack (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700665 }
666
667 return p->non_empty_event_type_bitmap;
668}
669
670always_inline uword
671vlib_process_wait_for_one_time_event (vlib_main_t * vm,
672 uword ** data_vector,
673 uword with_type_index)
674{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400675 vlib_node_main_t *nm = &vm->node_main;
676 vlib_process_t *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700677 uword r;
678
679 p = vec_elt (nm->processes, nm->current_process_index);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400680 ASSERT (!pool_is_free_index (p->event_type_pool, with_type_index));
681 while (!clib_bitmap_get (p->non_empty_event_type_bitmap, with_type_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700682 {
683 p->flags |= VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400684 r =
685 clib_setjmp (&p->resume_longjmp, VLIB_PROCESS_RESUME_LONGJMP_SUSPEND);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700686 if (r == VLIB_PROCESS_RESUME_LONGJMP_SUSPEND)
Damjan Marioncea46522020-05-21 16:47:05 +0200687 {
688 vlib_process_start_switch_stack (vm, 0);
689 clib_longjmp (&p->return_longjmp,
690 VLIB_PROCESS_RETURN_LONGJMP_SUSPEND);
691 }
692 else
693 vlib_process_finish_switch_stack (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700694 }
695
696 return vlib_process_get_events_helper (p, with_type_index, data_vector);
697}
698
699always_inline uword
700vlib_process_wait_for_event_with_type (vlib_main_t * vm,
701 uword ** data_vector,
702 uword with_type_opaque)
703{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400704 vlib_node_main_t *nm = &vm->node_main;
705 vlib_process_t *p;
706 uword r, *h;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700707
708 p = vec_elt (nm->processes, nm->current_process_index);
709 h = hash_get (p->event_type_index_by_type_opaque, with_type_opaque);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400710 while (!h || !clib_bitmap_get (p->non_empty_event_type_bitmap, h[0]))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700711 {
712 p->flags |= VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400713 r =
714 clib_setjmp (&p->resume_longjmp, VLIB_PROCESS_RESUME_LONGJMP_SUSPEND);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700715 if (r == VLIB_PROCESS_RESUME_LONGJMP_SUSPEND)
Damjan Marioncea46522020-05-21 16:47:05 +0200716 {
717 vlib_process_start_switch_stack (vm, 0);
718 clib_longjmp (&p->return_longjmp,
719 VLIB_PROCESS_RETURN_LONGJMP_SUSPEND);
720 }
721 else
722 vlib_process_finish_switch_stack (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700723
724 /* See if unknown event type has been signaled now. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400725 if (!h)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700726 h = hash_get (p->event_type_index_by_type_opaque, with_type_opaque);
727 }
728
729 return vlib_process_get_events_helper (p, h[0], data_vector);
730}
731
Dave Barach2ab470a2016-08-10 18:38:36 -0400732/** Suspend a cooperative multi-tasking thread
733 Waits for an event, or for the indicated number of seconds to elapse
734 @param vm - vlib_main_t pointer
Damjan Marion607de1a2016-08-16 22:53:54 +0200735 @param dt - timeout, in seconds.
Dave Barach2ab470a2016-08-10 18:38:36 -0400736 @returns the remaining time interval
737*/
738
Ed Warnickecb9cada2015-12-08 15:45:58 -0700739always_inline f64
740vlib_process_wait_for_event_or_clock (vlib_main_t * vm, f64 dt)
741{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400742 vlib_node_main_t *nm = &vm->node_main;
743 vlib_process_t *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700744 f64 wakeup_time;
745 uword r;
746
747 p = vec_elt (nm->processes, nm->current_process_index);
748
749 if (vlib_process_suspend_time_is_zero (dt)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400750 || !clib_bitmap_is_zero (p->non_empty_event_type_bitmap))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700751 return dt;
752
753 wakeup_time = vlib_time_now (vm) + dt;
754
755 /* Suspend waiting for both clock and event to occur. */
756 p->flags |= (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT
757 | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK);
758
759 r = clib_setjmp (&p->resume_longjmp, VLIB_PROCESS_RESUME_LONGJMP_SUSPEND);
760 if (r == VLIB_PROCESS_RESUME_LONGJMP_SUSPEND)
761 {
Dave Barach5c20a012017-06-13 08:48:31 -0400762 p->resume_clock_interval = dt * 1e5;
Damjan Marioncea46522020-05-21 16:47:05 +0200763 vlib_process_start_switch_stack (vm, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700764 clib_longjmp (&p->return_longjmp, VLIB_PROCESS_RETURN_LONGJMP_SUSPEND);
765 }
Damjan Marioncea46522020-05-21 16:47:05 +0200766 else
767 vlib_process_finish_switch_stack (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700768
769 /* Return amount of time still left to sleep.
770 If <= 0 then we've been waken up by the clock (and not an event). */
771 return wakeup_time - vlib_time_now (vm);
772}
773
774always_inline vlib_process_event_type_t *
775vlib_process_new_event_type (vlib_process_t * p, uword with_type_opaque)
776{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400777 vlib_process_event_type_t *et;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700778 pool_get (p->event_type_pool, et);
779 et->opaque = with_type_opaque;
780 return et;
781}
782
783always_inline uword
Dave Barach9b8ffd92016-07-08 08:13:45 -0400784vlib_process_create_one_time_event (vlib_main_t * vm, uword node_index,
785 uword with_type_opaque)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700786{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400787 vlib_node_main_t *nm = &vm->node_main;
788 vlib_node_t *n = vlib_get_node (vm, node_index);
789 vlib_process_t *p = vec_elt (nm->processes, n->runtime_index);
790 vlib_process_event_type_t *et;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700791 uword t;
792
793 et = vlib_process_new_event_type (p, with_type_opaque);
794 t = et - p->event_type_pool;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400795 p->one_time_event_type_bitmap =
796 clib_bitmap_ori (p->one_time_event_type_bitmap, t);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700797 return t;
798}
799
800always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -0400801vlib_process_delete_one_time_event (vlib_main_t * vm, uword node_index,
802 uword t)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700803{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400804 vlib_node_main_t *nm = &vm->node_main;
805 vlib_node_t *n = vlib_get_node (vm, node_index);
806 vlib_process_t *p = vec_elt (nm->processes, n->runtime_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700807
808 ASSERT (clib_bitmap_get (p->one_time_event_type_bitmap, t));
809 vlib_process_free_event_type (p, t, /* is_one_time_event */ 1);
810}
811
812always_inline void *
813vlib_process_signal_event_helper (vlib_node_main_t * nm,
814 vlib_node_t * n,
815 vlib_process_t * p,
816 uword t,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400817 uword n_data_elts, uword n_data_elt_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700818{
819 uword p_flags, add_to_pending, delete_from_wheel;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400820 void *data_to_be_written_by_caller;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700821
Dave Barach1403fcd2018-02-05 09:45:43 -0500822 ASSERT (n->type == VLIB_NODE_TYPE_PROCESS);
823
Dave Barach9b8ffd92016-07-08 08:13:45 -0400824 ASSERT (!pool_is_free_index (p->event_type_pool, t));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700825
826 vec_validate (p->pending_event_data_by_type_index, t);
827
828 /* Resize data vector and return caller's data to be written. */
829 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400830 void *data_vec = p->pending_event_data_by_type_index[t];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700831 uword l;
832
Dave Barach9b8ffd92016-07-08 08:13:45 -0400833 if (!data_vec && vec_len (nm->recycled_event_data_vectors))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700834 {
835 data_vec = vec_pop (nm->recycled_event_data_vectors);
836 _vec_len (data_vec) = 0;
837 }
838
839 l = vec_len (data_vec);
840
841 data_vec = _vec_resize (data_vec,
842 /* length_increment */ n_data_elts,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400843 /* total size after increment */
844 (l + n_data_elts) * n_data_elt_bytes,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700845 /* header_bytes */ 0, /* data_align */ 0);
846
847 p->pending_event_data_by_type_index[t] = data_vec;
848 data_to_be_written_by_caller = data_vec + l * n_data_elt_bytes;
849 }
850
Dave Barach9b8ffd92016-07-08 08:13:45 -0400851 p->non_empty_event_type_bitmap =
852 clib_bitmap_ori (p->non_empty_event_type_bitmap, t);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700853
854 p_flags = p->flags;
855
856 /* Event was already signalled? */
857 add_to_pending = (p_flags & VLIB_PROCESS_RESUME_PENDING) == 0;
858
859 /* Process will resume when suspend time elapses? */
860 delete_from_wheel = 0;
861 if (p_flags & VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK)
862 {
863 /* Waiting for both event and clock? */
864 if (p_flags & VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT)
Florin Coras40f92462018-08-01 16:25:45 -0700865 {
866 if (!TW (tw_timer_handle_is_free)
867 ((TWT (tw_timer_wheel) *) nm->timing_wheel,
868 p->stop_timer_handle))
869 delete_from_wheel = 1;
870 else
871 /* timer just popped so process should already be on the list */
872 add_to_pending = 0;
873 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700874 else
875 /* Waiting only for clock. Event will be queue and may be
876 handled when timer expires. */
877 add_to_pending = 0;
878 }
879
880 /* Never add current process to pending vector since current process is
881 already running. */
882 add_to_pending &= nm->current_process_index != n->runtime_index;
883
884 if (add_to_pending)
885 {
886 u32 x = vlib_timing_wheel_data_set_suspended_process (n->runtime_index);
887 p->flags = p_flags | VLIB_PROCESS_RESUME_PENDING;
888 vec_add1 (nm->data_from_advancing_timing_wheel, x);
889 if (delete_from_wheel)
Dave Barach5c20a012017-06-13 08:48:31 -0400890 TW (tw_timer_stop) ((TWT (tw_timer_wheel) *) nm->timing_wheel,
891 p->stop_timer_handle);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700892 }
893
894 return data_to_be_written_by_caller;
895}
896
897always_inline void *
898vlib_process_signal_event_data (vlib_main_t * vm,
899 uword node_index,
900 uword type_opaque,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400901 uword n_data_elts, uword n_data_elt_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700902{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400903 vlib_node_main_t *nm = &vm->node_main;
904 vlib_node_t *n = vlib_get_node (vm, node_index);
905 vlib_process_t *p = vec_elt (nm->processes, n->runtime_index);
906 uword *h, t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700907
John Lo609707e2017-09-19 21:45:10 -0400908 /* Must be in main thread */
909 ASSERT (vlib_get_thread_index () == 0);
910
Ed Warnickecb9cada2015-12-08 15:45:58 -0700911 h = hash_get (p->event_type_index_by_type_opaque, type_opaque);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400912 if (!h)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700913 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400914 vlib_process_event_type_t *et =
915 vlib_process_new_event_type (p, type_opaque);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700916 t = et - p->event_type_pool;
917 hash_set (p->event_type_index_by_type_opaque, type_opaque, t);
918 }
919 else
920 t = h[0];
921
Dave Barach9b8ffd92016-07-08 08:13:45 -0400922 return vlib_process_signal_event_helper (nm, n, p, t, n_data_elts,
923 n_data_elt_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700924}
925
926always_inline void *
927vlib_process_signal_event_at_time (vlib_main_t * vm,
928 f64 dt,
929 uword node_index,
930 uword type_opaque,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400931 uword n_data_elts, uword n_data_elt_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700932{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400933 vlib_node_main_t *nm = &vm->node_main;
934 vlib_node_t *n = vlib_get_node (vm, node_index);
935 vlib_process_t *p = vec_elt (nm->processes, n->runtime_index);
936 uword *h, t;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700937
938 h = hash_get (p->event_type_index_by_type_opaque, type_opaque);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400939 if (!h)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700940 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400941 vlib_process_event_type_t *et =
942 vlib_process_new_event_type (p, type_opaque);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700943 t = et - p->event_type_pool;
944 hash_set (p->event_type_index_by_type_opaque, type_opaque, t);
945 }
946 else
947 t = h[0];
948
949 if (vlib_process_suspend_time_is_zero (dt))
Dave Barach9b8ffd92016-07-08 08:13:45 -0400950 return vlib_process_signal_event_helper (nm, n, p, t, n_data_elts,
951 n_data_elt_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700952 else
953 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400954 vlib_signal_timed_event_data_t *te;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700955
956 pool_get_aligned (nm->signal_timed_event_data_pool, te, sizeof (te[0]));
957
958 te->n_data_elts = n_data_elts;
959 te->n_data_elt_bytes = n_data_elt_bytes;
960 te->n_data_bytes = n_data_elts * n_data_elt_bytes;
961
962 /* Assert that structure fields are big enough. */
963 ASSERT (te->n_data_elts == n_data_elts);
964 ASSERT (te->n_data_elt_bytes == n_data_elt_bytes);
965 ASSERT (te->n_data_bytes == n_data_elts * n_data_elt_bytes);
966
967 te->process_node_index = n->runtime_index;
968 te->event_type_index = t;
969
Dave Barach5c20a012017-06-13 08:48:31 -0400970 p->stop_timer_handle =
971 TW (tw_timer_start) ((TWT (tw_timer_wheel) *) nm->timing_wheel,
972 vlib_timing_wheel_data_set_timed_event
973 (te - nm->signal_timed_event_data_pool),
974 0 /* timer_id */ ,
975 (vlib_time_now (vm) + dt) * 1e5);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700976
977 /* Inline data big enough to hold event? */
978 if (te->n_data_bytes < sizeof (te->inline_event_data))
979 return te->inline_event_data;
980 else
981 {
982 te->event_data_as_vector = 0;
983 vec_resize (te->event_data_as_vector, te->n_data_bytes);
984 return te->event_data_as_vector;
985 }
986 }
987}
988
989always_inline void *
990vlib_process_signal_one_time_event_data (vlib_main_t * vm,
991 uword node_index,
992 uword type_index,
993 uword n_data_elts,
994 uword n_data_elt_bytes)
995{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400996 vlib_node_main_t *nm = &vm->node_main;
997 vlib_node_t *n = vlib_get_node (vm, node_index);
998 vlib_process_t *p = vec_elt (nm->processes, n->runtime_index);
999 return vlib_process_signal_event_helper (nm, n, p, type_index, n_data_elts,
1000 n_data_elt_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001001}
1002
1003always_inline void
1004vlib_process_signal_event (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001005 uword node_index, uword type_opaque, uword data)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001006{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001007 uword *d = vlib_process_signal_event_data (vm, node_index, type_opaque,
1008 1 /* elts */ , sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001009 d[0] = data;
1010}
1011
1012always_inline void
1013vlib_process_signal_event_pointer (vlib_main_t * vm,
1014 uword node_index,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001015 uword type_opaque, void *data)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001016{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001017 void **d = vlib_process_signal_event_data (vm, node_index, type_opaque,
1018 1 /* elts */ , sizeof (data));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001019 d[0] = data;
1020}
1021
Dave Barach69128d02017-09-26 10:54:34 -04001022/**
1023 * Signal event to process from any thread.
1024 *
1025 * When in doubt, use this.
1026 */
1027always_inline void
1028vlib_process_signal_event_mt (vlib_main_t * vm,
1029 uword node_index, uword type_opaque, uword data)
1030{
1031 if (vlib_get_thread_index () != 0)
1032 {
1033 vlib_process_signal_event_mt_args_t args = {
1034 .node_index = node_index,
1035 .type_opaque = type_opaque,
1036 .data = data,
1037 };
1038 vlib_rpc_call_main_thread (vlib_process_signal_event_mt_helper,
1039 (u8 *) & args, sizeof (args));
1040 }
1041 else
1042 vlib_process_signal_event (vm, node_index, type_opaque, data);
1043}
1044
Ed Warnickecb9cada2015-12-08 15:45:58 -07001045always_inline void
1046vlib_process_signal_one_time_event (vlib_main_t * vm,
1047 uword node_index,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001048 uword type_index, uword data)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001049{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001050 uword *d =
1051 vlib_process_signal_one_time_event_data (vm, node_index, type_index,
1052 1 /* elts */ , sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001053 d[0] = data;
1054}
1055
1056always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -04001057vlib_signal_one_time_waiting_process (vlib_main_t * vm,
1058 vlib_one_time_waiting_process_t * p)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001059{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001060 vlib_process_signal_one_time_event (vm, p->node_index, p->one_time_event,
1061 /* data */ ~0);
Dave Barachb7b92992018-10-17 10:38:51 -04001062 clib_memset (p, ~0, sizeof (p[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001063}
1064
1065always_inline void
1066vlib_signal_one_time_waiting_process_vector (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001067 vlib_one_time_waiting_process_t
1068 ** wps)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001069{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001070 vlib_one_time_waiting_process_t *wp;
1071 vec_foreach (wp, *wps) vlib_signal_one_time_waiting_process (vm, wp);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001072 vec_free (*wps);
1073}
1074
1075always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -04001076vlib_current_process_wait_for_one_time_event (vlib_main_t * vm,
1077 vlib_one_time_waiting_process_t
1078 * p)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001079{
1080 p->node_index = vlib_current_process (vm);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001081 p->one_time_event = vlib_process_create_one_time_event (vm, p->node_index, /* type opaque */
1082 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001083 vlib_process_wait_for_one_time_event (vm,
1084 /* don't care about data */ 0,
1085 p->one_time_event);
1086}
1087
1088always_inline void
1089vlib_current_process_wait_for_one_time_event_vector (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001090 vlib_one_time_waiting_process_t
1091 ** wps)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001092{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001093 vlib_one_time_waiting_process_t *wp;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001094 vec_add2 (*wps, wp, 1);
1095 vlib_current_process_wait_for_one_time_event (vm, wp);
1096}
1097
1098always_inline u32
1099vlib_node_runtime_update_main_loop_vector_stats (vlib_main_t * vm,
1100 vlib_node_runtime_t * node,
1101 uword n_vectors)
1102{
1103 u32 i, d, vi0, vi1;
1104 u32 i0, i1;
1105
1106 ASSERT (is_pow2 (ARRAY_LEN (node->main_loop_vector_stats)));
1107 i = ((vm->main_loop_count >> VLIB_LOG2_MAIN_LOOPS_PER_STATS_UPDATE)
1108 & (ARRAY_LEN (node->main_loop_vector_stats) - 1));
1109 i0 = i ^ 0;
1110 i1 = i ^ 1;
1111 d = ((vm->main_loop_count >> VLIB_LOG2_MAIN_LOOPS_PER_STATS_UPDATE)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001112 -
1113 (node->main_loop_count_last_dispatch >>
1114 VLIB_LOG2_MAIN_LOOPS_PER_STATS_UPDATE));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001115 vi0 = node->main_loop_vector_stats[i0];
1116 vi1 = node->main_loop_vector_stats[i1];
1117 vi0 = d == 0 ? vi0 : 0;
1118 vi1 = d <= 1 ? vi1 : 0;
1119 vi0 += n_vectors;
1120 node->main_loop_vector_stats[i0] = vi0;
1121 node->main_loop_vector_stats[i1] = vi1;
1122 node->main_loop_count_last_dispatch = vm->main_loop_count;
1123 /* Return previous counter. */
1124 return node->main_loop_vector_stats[i1];
1125}
1126
1127always_inline f64
1128vlib_node_vectors_per_main_loop_as_float (vlib_main_t * vm, u32 node_index)
1129{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001130 vlib_node_runtime_t *rt = vlib_node_get_runtime (vm, node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001131 u32 v;
1132
Dave Barach9b8ffd92016-07-08 08:13:45 -04001133 v = vlib_node_runtime_update_main_loop_vector_stats (vm, rt, /* n_vectors */
1134 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001135 return (f64) v / (1 << VLIB_LOG2_MAIN_LOOPS_PER_STATS_UPDATE);
1136}
1137
1138always_inline u32
1139vlib_node_vectors_per_main_loop_as_integer (vlib_main_t * vm, u32 node_index)
1140{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001141 vlib_node_runtime_t *rt = vlib_node_get_runtime (vm, node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001142 u32 v;
1143
Dave Barach9b8ffd92016-07-08 08:13:45 -04001144 v = vlib_node_runtime_update_main_loop_vector_stats (vm, rt, /* n_vectors */
1145 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001146 return v >> VLIB_LOG2_MAIN_LOOPS_PER_STATS_UPDATE;
1147}
1148
1149void
Dave Barach9b8ffd92016-07-08 08:13:45 -04001150vlib_frame_free (vlib_main_t * vm, vlib_node_runtime_t * r, vlib_frame_t * f);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001151
Neale Rannsbb620d72017-06-29 00:19:08 -07001152/* Return the edge index if present, ~0 otherwise */
1153uword vlib_node_get_next (vlib_main_t * vm, uword node, uword next_node);
1154
Ed Warnickecb9cada2015-12-08 15:45:58 -07001155/* Add next node to given node in given slot. */
1156uword
1157vlib_node_add_next_with_slot (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001158 uword node, uword next_node, uword slot);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001159
1160/* As above but adds to end of node's next vector. */
1161always_inline uword
1162vlib_node_add_next (vlib_main_t * vm, uword node, uword next_node)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001163{
1164 return vlib_node_add_next_with_slot (vm, node, next_node, ~0);
1165}
Ed Warnickecb9cada2015-12-08 15:45:58 -07001166
1167/* Add next node to given node in given slot. */
1168uword
1169vlib_node_add_named_next_with_slot (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001170 uword node, char *next_name, uword slot);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001171
1172/* As above but adds to end of node's next vector. */
1173always_inline uword
Dave Barach9b8ffd92016-07-08 08:13:45 -04001174vlib_node_add_named_next (vlib_main_t * vm, uword node, char *name)
1175{
1176 return vlib_node_add_named_next_with_slot (vm, node, name, ~0);
1177}
Ed Warnickecb9cada2015-12-08 15:45:58 -07001178
Florin Corase86a8ed2018-01-05 03:20:25 -08001179/**
1180 * Get list of nodes
1181 */
Dave Barach1ddbc012018-06-13 09:26:05 -04001182void
1183vlib_node_get_nodes (vlib_main_t * vm, u32 max_threads, int include_stats,
1184 int barrier_sync, vlib_node_t **** node_dupsp,
1185 vlib_main_t *** stat_vmsp);
Florin Corase86a8ed2018-01-05 03:20:25 -08001186
Ed Warnickecb9cada2015-12-08 15:45:58 -07001187/* Query node given name. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001188vlib_node_t *vlib_get_node_by_name (vlib_main_t * vm, u8 * name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001189
1190/* Rename a node. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001191void vlib_node_rename (vlib_main_t * vm, u32 node_index, char *fmt, ...);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001192
1193/* Register new packet processing node. Nodes can be registered
1194 dynamically via this call or statically via the VLIB_REGISTER_NODE
1195 macro. */
1196u32 vlib_register_node (vlib_main_t * vm, vlib_node_registration_t * r);
1197
1198/* Register all static nodes registered via VLIB_REGISTER_NODE. */
1199void vlib_register_all_static_nodes (vlib_main_t * vm);
1200
1201/* Start a process. */
1202void vlib_start_process (vlib_main_t * vm, uword process_index);
1203
1204/* Sync up runtime and main node stats. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001205void vlib_node_sync_stats (vlib_main_t * vm, vlib_node_t * n);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001206
1207/* Node graph initialization function. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001208clib_error_t *vlib_node_main_init (vlib_main_t * vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001209
1210format_function_t format_vlib_node_graph;
1211format_function_t format_vlib_node_name;
1212format_function_t format_vlib_next_node_name;
1213format_function_t format_vlib_node_and_next;
1214format_function_t format_vlib_cpu_time;
1215format_function_t format_vlib_time;
1216/* Parse node name -> node index. */
1217unformat_function_t unformat_vlib_node;
1218
Dave Barach9b8ffd92016-07-08 08:13:45 -04001219always_inline void
1220vlib_node_increment_counter (vlib_main_t * vm, u32 node_index,
1221 u32 counter_index, u64 increment)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001222{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001223 vlib_node_t *n = vlib_get_node (vm, node_index);
1224 vlib_error_main_t *em = &vm->error_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001225 u32 node_counter_base_index = n->error_heap_index;
1226 em->counters[node_counter_base_index + counter_index] += increment;
1227}
1228
Dave Barach11965c72019-05-28 16:31:05 -04001229/** @brief Create a vlib process
1230 * @param vm &vlib_global_main
1231 * @param f the process node function
1232 * @param log2_n_stack_bytes size of the process stack, defaults to 16K
1233 * @return newly-create node index
1234 * @warning call only on the main thread. Barrier sync required
1235 */
1236u32 vlib_process_create (vlib_main_t * vm, char *name,
1237 vlib_node_function_t * f, u32 log2_n_stack_bytes);
1238
Ed Warnickecb9cada2015-12-08 15:45:58 -07001239#endif /* included_vlib_node_funcs_h */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001240
1241/*
1242 * fd.io coding-style-patch-verification: ON
1243 *
1244 * Local Variables:
1245 * eval: (c-set-style "gnu")
1246 * End:
1247 */