blob: 364989eafe02fae279fcfc104b981624faaf4019 [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 * init.h: mechanism for functions to be called at init/exit.
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
40#ifndef included_vlib_init_h
41#define included_vlib_init_h
42
43#include <vppinfra/error.h>
44#include <vppinfra/format.h>
45#include <vppinfra/hash.h>
46
47/* Init/exit functions: called at start/end of main routine. Init
48 functions are typically used to register and setup packet
49 processing nodes. */
50
Dave Barach9b8ffd92016-07-08 08:13:45 -040051typedef clib_error_t *(vlib_init_function_t) (struct vlib_main_t * vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -070052
Dave Barach9b8ffd92016-07-08 08:13:45 -040053typedef struct _vlib_init_function_list_elt
54{
55 struct _vlib_init_function_list_elt *next_init_function;
56 vlib_init_function_t *f;
Dave Barachf8d50682019-05-14 18:01:44 -040057 char *name;
58 char **runs_before;
59 char **runs_after;
60 char **init_order;
Ed Warnickecb9cada2015-12-08 15:45:58 -070061} _vlib_init_function_list_elt_t;
62
63/* Configuration functions: called with configuration input just before
64 main polling loop starts. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040065typedef clib_error_t *(vlib_config_function_t) (struct vlib_main_t * vm,
66 unformat_input_t * input);
Ed Warnickecb9cada2015-12-08 15:45:58 -070067
Dave Barach9b8ffd92016-07-08 08:13:45 -040068typedef struct vlib_config_function_runtime_t
69{
Ed Warnickecb9cada2015-12-08 15:45:58 -070070 /* Function to call. Set to null once function has already been called. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040071 vlib_config_function_t *function;
Ed Warnickecb9cada2015-12-08 15:45:58 -070072
73 /* Input for function. */
74 unformat_input_t input;
75
76 /* next config function registration */
Dave Barach9b8ffd92016-07-08 08:13:45 -040077 struct vlib_config_function_runtime_t *next_registration;
Ed Warnickecb9cada2015-12-08 15:45:58 -070078
79 /* To be invoked as soon as the clib heap is available */
80 u8 is_early;
81
82 /* Name used to distinguish input on command line. */
83 char name[32];
84} vlib_config_function_runtime_t;
85
Damjan Marion72d2c4f2018-04-05 21:32:29 +020086#define VLIB_REMOVE_FROM_LINKED_LIST(first,p,next) \
87{ \
Damjan Marion13adc3d2018-04-09 20:59:53 +020088 ASSERT (first); \
Damjan Marion72d2c4f2018-04-05 21:32:29 +020089 if (first == p) \
90 first = (p)->next; \
91 else \
92 { \
93 __typeof__ (p) current = first; \
Damjan Marion13adc3d2018-04-09 20:59:53 +020094 while (current->next) \
95 { \
96 if (current->next == p) \
97 { \
98 current->next = current->next->next; \
99 break; \
100 } \
101 current = current->next; \
102 } \
103 ASSERT (current); \
Damjan Marion72d2c4f2018-04-05 21:32:29 +0200104 } \
105}
106
Ed Warnickecb9cada2015-12-08 15:45:58 -0700107#define _VLIB_INIT_FUNCTION_SYMBOL(x, type) \
108 _vlib_##type##_function_##x
109
110#define VLIB_INIT_FUNCTION_SYMBOL(x) \
111 _VLIB_INIT_FUNCTION_SYMBOL(x, init)
112#define VLIB_MAIN_LOOP_ENTER_FUNCTION_SYMBOL(x) \
113 _VLIB_INIT_FUNCTION_SYMBOL(x, main_loop_enter)
114#define VLIB_MAIN_LOOP_EXIT_FUNCTION_SYMBOL(x) \
115 _VLIB_INIT_FUNCTION_SYMBOL(x, main_loop_exit)
116#define VLIB_CONFIG_FUNCTION_SYMBOL(x) \
117 _VLIB_INIT_FUNCTION_SYMBOL(x, config)
118
119/* Declaration is global (e.g. not static) so that init functions can
120 be called from other modules to resolve init function depend. */
121
Damjan Marion6e363512018-08-10 22:39:11 +0200122#ifndef CLIB_MARCH_VARIANT
Damjan Marionfd8deb42021-03-06 12:26:28 +0100123#define VLIB_DECLARE_INIT_FUNCTION(x, tag) \
124 vlib_init_function_t *_VLIB_INIT_FUNCTION_SYMBOL (x, tag) = x; \
125 static void __vlib_add_##tag##_function_##x (void) \
126 __attribute__ ((__constructor__)); \
127 static _vlib_init_function_list_elt_t _vlib_init_function_##tag##_##x; \
128 static void __vlib_add_##tag##_function_##x (void) \
129 { \
130 vlib_global_main_t *vgm = vlib_get_global_main (); \
131 _vlib_init_function_##tag##_##x.next_init_function = \
132 vgm->tag##_function_registrations; \
133 vgm->tag##_function_registrations = &_vlib_init_function_##tag##_##x; \
134 _vlib_init_function_##tag##_##x.f = &x; \
135 _vlib_init_function_##tag##_##x.name = #x; \
136 } \
137 static void __vlib_rm_##tag##_function_##x (void) \
138 __attribute__ ((__destructor__)); \
139 static void __vlib_rm_##tag##_function_##x (void) \
140 { \
141 vlib_global_main_t *vgm = vlib_get_global_main (); \
142 _vlib_init_function_list_elt_t *this, *prev; \
143 this = vgm->tag##_function_registrations; \
144 if (this == 0) \
145 return; \
146 if (this->f == &x) \
147 { \
148 vgm->tag##_function_registrations = this->next_init_function; \
149 return; \
150 } \
151 prev = this; \
152 this = this->next_init_function; \
153 while (this) \
154 { \
155 if (this->f == &x) \
156 { \
157 prev->next_init_function = this->next_init_function; \
158 return; \
159 } \
160 prev = this; \
161 this = this->next_init_function; \
162 } \
163 } \
164 static _vlib_init_function_list_elt_t _vlib_init_function_##tag##_##x
Damjan Marion6e363512018-08-10 22:39:11 +0200165#else
166/* create unused pointer to silence compiler warnings and get whole
167 function optimized out */
168#define VLIB_DECLARE_INIT_FUNCTION(x, tag) \
Dave Barachf8d50682019-05-14 18:01:44 -0400169static __clib_unused void * __clib_unused_##tag##_##x = x
Damjan Marion6e363512018-08-10 22:39:11 +0200170#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171
172#define VLIB_INIT_FUNCTION(x) VLIB_DECLARE_INIT_FUNCTION(x,init)
Damjan Marione9f929b2017-03-16 11:32:09 +0100173#define VLIB_WORKER_INIT_FUNCTION(x) VLIB_DECLARE_INIT_FUNCTION(x,worker_init)
Damjan Marion321bd102022-06-01 00:45:18 +0200174#define VLIB_NUM_WORKERS_CHANGE_FN(x) \
175 VLIB_DECLARE_INIT_FUNCTION (x, num_workers_change)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700176
177#define VLIB_MAIN_LOOP_ENTER_FUNCTION(x) \
178 VLIB_DECLARE_INIT_FUNCTION(x,main_loop_enter)
179#define VLIB_MAIN_LOOP_EXIT_FUNCTION(x) \
180VLIB_DECLARE_INIT_FUNCTION(x,main_loop_exit)
181
Damjan Marion6e363512018-08-10 22:39:11 +0200182#ifndef CLIB_MARCH_VARIANT
Damjan Marionfd8deb42021-03-06 12:26:28 +0100183#define VLIB_CONFIG_FUNCTION(x, n, ...) \
184 __VA_ARGS__ vlib_config_function_runtime_t VLIB_CONFIG_FUNCTION_SYMBOL (x); \
185 static void __vlib_add_config_function_##x (void) \
186 __attribute__ ((__constructor__)); \
187 static void __vlib_add_config_function_##x (void) \
188 { \
189 vlib_global_main_t *vgm = vlib_get_global_main (); \
190 VLIB_CONFIG_FUNCTION_SYMBOL (x).next_registration = \
191 vgm->config_function_registrations; \
192 vgm->config_function_registrations = &VLIB_CONFIG_FUNCTION_SYMBOL (x); \
193 } \
194 static void __vlib_rm_config_function_##x (void) \
195 __attribute__ ((__destructor__)); \
196 static void __vlib_rm_config_function_##x (void) \
197 { \
198 vlib_global_main_t *vgm = vlib_get_global_main (); \
199 vlib_config_function_runtime_t *p = &VLIB_CONFIG_FUNCTION_SYMBOL (x); \
200 VLIB_REMOVE_FROM_LINKED_LIST (vgm->config_function_registrations, p, \
201 next_registration); \
202 } \
203 vlib_config_function_runtime_t VLIB_CONFIG_FUNCTION_SYMBOL (x) = { \
204 .name = n, \
205 .function = x, \
206 .is_early = 0, \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700207 }
Damjan Marion6e363512018-08-10 22:39:11 +0200208#else
209/* create unused pointer to silence compiler warnings and get whole
210 function optimized out */
211#define VLIB_CONFIG_FUNCTION(x,n,...) \
212 static __clib_unused vlib_config_function_runtime_t \
213 VLIB_CONFIG_FUNCTION_SYMBOL (__clib_unused_##x) \
214 = { \
215 .name = n, \
216 .function = x, \
217 .is_early = 0, \
218 }
219#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -0700220
Damjan Marion6e363512018-08-10 22:39:11 +0200221#ifndef CLIB_MARCH_VARIANT
Damjan Marionfd8deb42021-03-06 12:26:28 +0100222#define VLIB_EARLY_CONFIG_FUNCTION(x, n, ...) \
223 __VA_ARGS__ vlib_config_function_runtime_t VLIB_CONFIG_FUNCTION_SYMBOL (x); \
224 static void __vlib_add_config_function_##x (void) \
225 __attribute__ ((__constructor__)); \
226 static void __vlib_add_config_function_##x (void) \
227 { \
228 vlib_global_main_t *vgm = vlib_get_global_main (); \
229 VLIB_CONFIG_FUNCTION_SYMBOL (x).next_registration = \
230 vgm->config_function_registrations; \
231 vgm->config_function_registrations = &VLIB_CONFIG_FUNCTION_SYMBOL (x); \
232 } \
233 static void __vlib_rm_config_function_##x (void) \
234 __attribute__ ((__destructor__)); \
235 static void __vlib_rm_config_function_##x (void) \
236 { \
237 vlib_global_main_t *vgm = vlib_get_global_main (); \
238 vlib_config_function_runtime_t *p = &VLIB_CONFIG_FUNCTION_SYMBOL (x); \
239 VLIB_REMOVE_FROM_LINKED_LIST (vgm->config_function_registrations, p, \
240 next_registration); \
241 } \
242 vlib_config_function_runtime_t VLIB_CONFIG_FUNCTION_SYMBOL (x) = { \
243 .name = n, \
244 .function = x, \
245 .is_early = 1, \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700246 }
Damjan Marion6e363512018-08-10 22:39:11 +0200247#else
248/* create unused pointer to silence compiler warnings and get whole
249 function optimized out */
250#define VLIB_EARLY_CONFIG_FUNCTION(x,n,...) \
251 static __clib_unused vlib_config_function_runtime_t \
252 VLIB_CONFIG_FUNCTION_SYMBOL (__clib_unused_##x) \
253 = { \
254 .name = n, \
255 .function = x, \
256 .is_early = 1, \
257 }
258#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259
260/* Call given init function: used for init function dependencies. */
Damjan Marionfd8deb42021-03-06 12:26:28 +0100261#define vlib_call_init_function(vm, x) \
262 ({ \
263 vlib_global_main_t *vgm = &vlib_global_main; \
264 extern vlib_init_function_t *VLIB_INIT_FUNCTION_SYMBOL (x); \
265 vlib_init_function_t *_f = VLIB_INIT_FUNCTION_SYMBOL (x); \
266 clib_error_t *_error = 0; \
267 if (!hash_get (vgm->init_functions_called, _f)) \
268 { \
269 hash_set1 (vgm->init_functions_called, _f); \
270 _error = _f (vm); \
271 } \
272 _error; \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700273 })
274
Dave Barach1f49ed62016-02-24 11:29:06 -0500275/* Don't call given init function: used to suppress parts of the netstack */
Damjan Marionfd8deb42021-03-06 12:26:28 +0100276#define vlib_mark_init_function_complete(vm, x) \
277 ({ \
278 vlib_global_main_t *vgm = &vlib_global_main; \
279 extern vlib_init_function_t *VLIB_INIT_FUNCTION_SYMBOL (x); \
280 vlib_init_function_t *_f = VLIB_INIT_FUNCTION_SYMBOL (x); \
281 hash_set1 (vgm->init_functions_called, _f); \
Dave Barach1f49ed62016-02-24 11:29:06 -0500282 })
283
Damjan Marionfd8deb42021-03-06 12:26:28 +0100284#define vlib_call_post_graph_init_function(vm, x) \
285 ({ \
286 vlib_global_main_t *vgm = &vlib_global_main; \
287 extern vlib_init_function_t *VLIB_POST_GRAPH_INIT_FUNCTION_SYMBOL (x); \
288 vlib_init_function_t *_f = VLIB_POST_GRAPH_INIT_FUNCTION_SYMBOL (x); \
289 clib_error_t *_error = 0; \
290 if (!hash_get (vgm->init_functions_called, _f)) \
291 { \
292 hash_set1 (vgm->init_functions_called, _f); \
293 _error = _f (vm); \
294 } \
295 _error; \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700296 })
297
Damjan Marionfd8deb42021-03-06 12:26:28 +0100298#define vlib_call_config_function(vm, x) \
299 ({ \
300 vlib_global_main_t *vgm = &vlib_global_main; \
301 vlib_config_function_runtime_t *_r; \
302 clib_error_t *_error = 0; \
303 extern vlib_config_function_runtime_t VLIB_CONFIG_FUNCTION_SYMBOL (x); \
304 \
305 _r = &VLIB_CONFIG_FUNCTION_SYMBOL (x); \
306 if (!hash_get (vgm->init_functions_called, _r->function)) \
307 { \
308 hash_set1 (vgm->init_functions_called, _r->function); \
309 _error = _r->function (vm, &_r->input); \
310 } \
311 _error; \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700312 })
313
Damjan Marionfd8deb42021-03-06 12:26:28 +0100314#define vlib_call_main_loop_enter_function(vm, x) \
315 ({ \
316 vlib_global_main_t *vgm = &vlib_global_main; \
317 extern vlib_init_function_t *VLIB_MAIN_LOOP_ENTER_FUNCTION_SYMBOL (x); \
318 vlib_init_function_t *_f = VLIB_MAIN_LOOP_ENTER_FUNCTION_SYMBOL (x); \
319 clib_error_t *_error = 0; \
320 if (!hash_get (vgm->init_functions_called, _f)) \
321 { \
322 hash_set1 (vgm->init_functions_called, _f); \
323 _error = _f (vm); \
324 } \
325 _error; \
Tom Seidenberg6c81f5a2020-07-10 15:49:03 +0000326 })
327
Ed Warnickecb9cada2015-12-08 15:45:58 -0700328/* External functions. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400329clib_error_t *vlib_call_all_init_functions (struct vlib_main_t *vm);
330clib_error_t *vlib_call_all_config_functions (struct vlib_main_t *vm,
331 unformat_input_t * input,
332 int is_early);
333clib_error_t *vlib_call_all_main_loop_enter_functions (struct vlib_main_t
334 *vm);
335clib_error_t *vlib_call_all_main_loop_exit_functions (struct vlib_main_t *vm);
Damjan Marionfd8deb42021-03-06 12:26:28 +0100336clib_error_t *
337vlib_call_init_exit_functions (struct vlib_main_t *vm,
338 _vlib_init_function_list_elt_t **headp,
339 int call_once, int is_global);
340clib_error_t *
341vlib_call_init_exit_functions_no_sort (struct vlib_main_t *vm,
342 _vlib_init_function_list_elt_t **headp,
343 int call_once, int is_global);
Dave Barachc602b382019-06-03 19:48:22 -0400344clib_error_t *vlib_sort_init_exit_functions (_vlib_init_function_list_elt_t
345 **);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700346#define foreach_vlib_module_reference \
347 _ (node_cli) \
348 _ (trace_cli)
349
350/* Dummy function to get node_cli.c linked in. */
351#define _(x) void vlib_##x##_reference (void);
352foreach_vlib_module_reference
353#undef _
Dave Barachf8d50682019-05-14 18:01:44 -0400354#define VLIB_INITS(...) (char*[]) { __VA_ARGS__, 0}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700355#endif /* included_vlib_init_h */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400356/*
357 * fd.io coding-style-patch-verification: ON
358 *
359 * Local Variables:
360 * eval: (c-set-style "gnu")
361 * End:
362 */