blob: 1eddbb13d5af956d20000a3be131742ccc3f9377 [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;
Ed Warnickecb9cada2015-12-08 15:45:58 -070057} _vlib_init_function_list_elt_t;
58
59/* Configuration functions: called with configuration input just before
60 main polling loop starts. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040061typedef clib_error_t *(vlib_config_function_t) (struct vlib_main_t * vm,
62 unformat_input_t * input);
Ed Warnickecb9cada2015-12-08 15:45:58 -070063
Dave Barach9b8ffd92016-07-08 08:13:45 -040064typedef struct vlib_config_function_runtime_t
65{
Ed Warnickecb9cada2015-12-08 15:45:58 -070066 /* Function to call. Set to null once function has already been called. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040067 vlib_config_function_t *function;
Ed Warnickecb9cada2015-12-08 15:45:58 -070068
69 /* Input for function. */
70 unformat_input_t input;
71
72 /* next config function registration */
Dave Barach9b8ffd92016-07-08 08:13:45 -040073 struct vlib_config_function_runtime_t *next_registration;
Ed Warnickecb9cada2015-12-08 15:45:58 -070074
75 /* To be invoked as soon as the clib heap is available */
76 u8 is_early;
77
78 /* Name used to distinguish input on command line. */
79 char name[32];
80} vlib_config_function_runtime_t;
81
Damjan Marion72d2c4f2018-04-05 21:32:29 +020082#define VLIB_REMOVE_FROM_LINKED_LIST(first,p,next) \
83{ \
84 if (first == p) \
85 first = (p)->next; \
86 else \
87 { \
88 __typeof__ (p) current = first; \
89 while (current->next) \
90 { \
91 if (current->next == p) \
92 { \
93 current->next = current->next->next; \
94 break; \
95 } \
96 current = current->next; \
97 } \
98 } \
99}
100
Ed Warnickecb9cada2015-12-08 15:45:58 -0700101#define _VLIB_INIT_FUNCTION_SYMBOL(x, type) \
102 _vlib_##type##_function_##x
103
104#define VLIB_INIT_FUNCTION_SYMBOL(x) \
105 _VLIB_INIT_FUNCTION_SYMBOL(x, init)
106#define VLIB_MAIN_LOOP_ENTER_FUNCTION_SYMBOL(x) \
107 _VLIB_INIT_FUNCTION_SYMBOL(x, main_loop_enter)
108#define VLIB_MAIN_LOOP_EXIT_FUNCTION_SYMBOL(x) \
109 _VLIB_INIT_FUNCTION_SYMBOL(x, main_loop_exit)
110#define VLIB_CONFIG_FUNCTION_SYMBOL(x) \
111 _VLIB_INIT_FUNCTION_SYMBOL(x, config)
112
113/* Declaration is global (e.g. not static) so that init functions can
114 be called from other modules to resolve init function depend. */
115
116#define VLIB_DECLARE_INIT_FUNCTION(x, tag) \
117vlib_init_function_t * _VLIB_INIT_FUNCTION_SYMBOL (x, tag) = x; \
118static void __vlib_add_##tag##_function_##x (void) \
119 __attribute__((__constructor__)) ; \
120static void __vlib_add_##tag##_function_##x (void) \
121{ \
122 vlib_main_t * vm = vlib_get_main(); \
123 static _vlib_init_function_list_elt_t _vlib_init_function; \
124 _vlib_init_function.next_init_function \
125 = vm->tag##_function_registrations; \
126 vm->tag##_function_registrations = &_vlib_init_function; \
127 _vlib_init_function.f = &x; \
Damjan Marion72d2c4f2018-04-05 21:32:29 +0200128} \
129static void __vlib_rm_##tag##_function_##x (void) \
130 __attribute__((__destructor__)) ; \
131static void __vlib_rm_##tag##_function_##x (void) \
132{ \
133 vlib_main_t * vm = vlib_get_main(); \
134 _vlib_init_function_list_elt_t *next; \
135 if (vm->tag##_function_registrations->f == &x) \
136 { \
137 vm->tag##_function_registrations = \
138 vm->tag##_function_registrations->next_init_function; \
139 return; \
140 } \
141 next = vm->tag##_function_registrations; \
142 while (next->next_init_function) \
143 { \
144 if (next->next_init_function->f == &x) \
145 { \
146 next->next_init_function = \
147 next->next_init_function->next_init_function; \
148 return; \
149 } \
150 next = next->next_init_function; \
151 } \
Dave Barach9b8ffd92016-07-08 08:13:45 -0400152}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153
154#define VLIB_INIT_FUNCTION(x) VLIB_DECLARE_INIT_FUNCTION(x,init)
Damjan Marione9f929b2017-03-16 11:32:09 +0100155#define VLIB_WORKER_INIT_FUNCTION(x) VLIB_DECLARE_INIT_FUNCTION(x,worker_init)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156
157#define VLIB_MAIN_LOOP_ENTER_FUNCTION(x) \
158 VLIB_DECLARE_INIT_FUNCTION(x,main_loop_enter)
159#define VLIB_MAIN_LOOP_EXIT_FUNCTION(x) \
160VLIB_DECLARE_INIT_FUNCTION(x,main_loop_exit)
161
162#define VLIB_CONFIG_FUNCTION(x,n,...) \
163 __VA_ARGS__ vlib_config_function_runtime_t \
164 VLIB_CONFIG_FUNCTION_SYMBOL(x); \
165static void __vlib_add_config_function_##x (void) \
166 __attribute__((__constructor__)) ; \
167static void __vlib_add_config_function_##x (void) \
168{ \
169 vlib_main_t * vm = vlib_get_main(); \
170 VLIB_CONFIG_FUNCTION_SYMBOL(x).next_registration \
171 = vm->config_function_registrations; \
172 vm->config_function_registrations \
173 = &VLIB_CONFIG_FUNCTION_SYMBOL(x); \
174} \
Damjan Marion72d2c4f2018-04-05 21:32:29 +0200175static void __vlib_rm_config_function_##x (void) \
176 __attribute__((__destructor__)) ; \
177static void __vlib_rm_config_function_##x (void) \
178{ \
179 vlib_main_t * vm = vlib_get_main(); \
180 vlib_config_function_runtime_t *p = \
181 & VLIB_CONFIG_FUNCTION_SYMBOL (x); \
182 VLIB_REMOVE_FROM_LINKED_LIST \
183 (vm->config_function_registrations, p, next_registration);\
184} \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185 vlib_config_function_runtime_t \
186 VLIB_CONFIG_FUNCTION_SYMBOL (x) \
187 = { \
188 .name = n, \
189 .function = x, \
190 .is_early = 0, \
191 }
192
193#define VLIB_EARLY_CONFIG_FUNCTION(x,n,...) \
194 __VA_ARGS__ vlib_config_function_runtime_t \
195 VLIB_CONFIG_FUNCTION_SYMBOL(x); \
196static void __vlib_add_config_function_##x (void) \
197 __attribute__((__constructor__)) ; \
198static void __vlib_add_config_function_##x (void) \
199{ \
200 vlib_main_t * vm = vlib_get_main(); \
201 VLIB_CONFIG_FUNCTION_SYMBOL(x).next_registration \
202 = vm->config_function_registrations; \
203 vm->config_function_registrations \
204 = &VLIB_CONFIG_FUNCTION_SYMBOL(x); \
205} \
Damjan Marion72d2c4f2018-04-05 21:32:29 +0200206static void __vlib_rm_config_function_##x (void) \
207 __attribute__((__destructor__)) ; \
208static void __vlib_rm_config_function_##x (void) \
209{ \
210 vlib_main_t * vm = vlib_get_main(); \
211 vlib_config_function_runtime_t *p = \
212 & VLIB_CONFIG_FUNCTION_SYMBOL (x); \
213 VLIB_REMOVE_FROM_LINKED_LIST \
214 (vm->config_function_registrations, p, next_registration);\
215} \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216 vlib_config_function_runtime_t \
217 VLIB_CONFIG_FUNCTION_SYMBOL (x) \
218 = { \
219 .name = n, \
220 .function = x, \
221 .is_early = 1, \
222 }
223
224/* Call given init function: used for init function dependencies. */
225#define vlib_call_init_function(vm, x) \
226 ({ \
227 extern vlib_init_function_t * VLIB_INIT_FUNCTION_SYMBOL (x); \
228 vlib_init_function_t * _f = VLIB_INIT_FUNCTION_SYMBOL (x); \
229 clib_error_t * _error = 0; \
230 if (! hash_get (vm->init_functions_called, _f)) \
231 { \
232 hash_set1 (vm->init_functions_called, _f); \
233 _error = _f (vm); \
234 } \
235 _error; \
236 })
237
Dave Barach1f49ed62016-02-24 11:29:06 -0500238/* Don't call given init function: used to suppress parts of the netstack */
239#define vlib_mark_init_function_complete(vm, x) \
240 ({ \
241 extern vlib_init_function_t * VLIB_INIT_FUNCTION_SYMBOL (x); \
242 vlib_init_function_t * _f = VLIB_INIT_FUNCTION_SYMBOL (x); \
243 hash_set1 (vm->init_functions_called, _f); \
244 })
245
Ed Warnickecb9cada2015-12-08 15:45:58 -0700246#define vlib_call_post_graph_init_function(vm, x) \
247 ({ \
248 extern vlib_init_function_t * VLIB_POST_GRAPH_INIT_FUNCTION_SYMBOL (x); \
249 vlib_init_function_t * _f = VLIB_POST_GRAPH_INIT_FUNCTION_SYMBOL (x); \
250 clib_error_t * _error = 0; \
251 if (! hash_get (vm->init_functions_called, _f)) \
252 { \
253 hash_set1 (vm->init_functions_called, _f); \
254 _error = _f (vm); \
255 } \
256 _error; \
257 })
258
259#define vlib_call_config_function(vm, x) \
260 ({ \
261 vlib_config_function_runtime_t * _r; \
262 clib_error_t * _error = 0; \
263 extern vlib_config_function_runtime_t \
264 VLIB_CONFIG_FUNCTION_SYMBOL (x); \
265 \
266 _r = &VLIB_CONFIG_FUNCTION_SYMBOL (x); \
267 if (! hash_get (vm->init_functions_called, _r->function)) \
268 { \
269 hash_set1 (vm->init_functions_called, _r->function); \
270 _error = _r->function (vm, &_r->input); \
271 } \
272 _error; \
273 })
274
275/* External functions. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400276clib_error_t *vlib_call_all_init_functions (struct vlib_main_t *vm);
277clib_error_t *vlib_call_all_config_functions (struct vlib_main_t *vm,
278 unformat_input_t * input,
279 int is_early);
280clib_error_t *vlib_call_all_main_loop_enter_functions (struct vlib_main_t
281 *vm);
282clib_error_t *vlib_call_all_main_loop_exit_functions (struct vlib_main_t *vm);
283clib_error_t *vlib_call_init_exit_functions (struct vlib_main_t *vm,
284 _vlib_init_function_list_elt_t *
285 head, int call_once);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700286
287#define foreach_vlib_module_reference \
288 _ (node_cli) \
289 _ (trace_cli)
290
291/* Dummy function to get node_cli.c linked in. */
292#define _(x) void vlib_##x##_reference (void);
293foreach_vlib_module_reference
294#undef _
Ed Warnickecb9cada2015-12-08 15:45:58 -0700295#endif /* included_vlib_init_h */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400296/*
297 * fd.io coding-style-patch-verification: ON
298 *
299 * Local Variables:
300 * eval: (c-set-style "gnu")
301 * End:
302 */