blob: 4fa5b3045907134f276359f3e503586b85b05bd2 [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
82#define _VLIB_INIT_FUNCTION_SYMBOL(x, type) \
83 _vlib_##type##_function_##x
84
85#define VLIB_INIT_FUNCTION_SYMBOL(x) \
86 _VLIB_INIT_FUNCTION_SYMBOL(x, init)
87#define VLIB_MAIN_LOOP_ENTER_FUNCTION_SYMBOL(x) \
88 _VLIB_INIT_FUNCTION_SYMBOL(x, main_loop_enter)
89#define VLIB_MAIN_LOOP_EXIT_FUNCTION_SYMBOL(x) \
90 _VLIB_INIT_FUNCTION_SYMBOL(x, main_loop_exit)
91#define VLIB_CONFIG_FUNCTION_SYMBOL(x) \
92 _VLIB_INIT_FUNCTION_SYMBOL(x, config)
93
94/* Declaration is global (e.g. not static) so that init functions can
95 be called from other modules to resolve init function depend. */
96
97#define VLIB_DECLARE_INIT_FUNCTION(x, tag) \
98vlib_init_function_t * _VLIB_INIT_FUNCTION_SYMBOL (x, tag) = x; \
99static void __vlib_add_##tag##_function_##x (void) \
100 __attribute__((__constructor__)) ; \
101static void __vlib_add_##tag##_function_##x (void) \
102{ \
103 vlib_main_t * vm = vlib_get_main(); \
104 static _vlib_init_function_list_elt_t _vlib_init_function; \
105 _vlib_init_function.next_init_function \
106 = vm->tag##_function_registrations; \
107 vm->tag##_function_registrations = &_vlib_init_function; \
108 _vlib_init_function.f = &x; \
Dave Barach9b8ffd92016-07-08 08:13:45 -0400109}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110
111#define VLIB_INIT_FUNCTION(x) VLIB_DECLARE_INIT_FUNCTION(x,init)
112
113#define VLIB_MAIN_LOOP_ENTER_FUNCTION(x) \
114 VLIB_DECLARE_INIT_FUNCTION(x,main_loop_enter)
115#define VLIB_MAIN_LOOP_EXIT_FUNCTION(x) \
116VLIB_DECLARE_INIT_FUNCTION(x,main_loop_exit)
117
118#define VLIB_CONFIG_FUNCTION(x,n,...) \
119 __VA_ARGS__ vlib_config_function_runtime_t \
120 VLIB_CONFIG_FUNCTION_SYMBOL(x); \
121static void __vlib_add_config_function_##x (void) \
122 __attribute__((__constructor__)) ; \
123static void __vlib_add_config_function_##x (void) \
124{ \
125 vlib_main_t * vm = vlib_get_main(); \
126 VLIB_CONFIG_FUNCTION_SYMBOL(x).next_registration \
127 = vm->config_function_registrations; \
128 vm->config_function_registrations \
129 = &VLIB_CONFIG_FUNCTION_SYMBOL(x); \
130} \
131 vlib_config_function_runtime_t \
132 VLIB_CONFIG_FUNCTION_SYMBOL (x) \
133 = { \
134 .name = n, \
135 .function = x, \
136 .is_early = 0, \
137 }
138
139#define VLIB_EARLY_CONFIG_FUNCTION(x,n,...) \
140 __VA_ARGS__ vlib_config_function_runtime_t \
141 VLIB_CONFIG_FUNCTION_SYMBOL(x); \
142static void __vlib_add_config_function_##x (void) \
143 __attribute__((__constructor__)) ; \
144static void __vlib_add_config_function_##x (void) \
145{ \
146 vlib_main_t * vm = vlib_get_main(); \
147 VLIB_CONFIG_FUNCTION_SYMBOL(x).next_registration \
148 = vm->config_function_registrations; \
149 vm->config_function_registrations \
150 = &VLIB_CONFIG_FUNCTION_SYMBOL(x); \
151} \
152 vlib_config_function_runtime_t \
153 VLIB_CONFIG_FUNCTION_SYMBOL (x) \
154 = { \
155 .name = n, \
156 .function = x, \
157 .is_early = 1, \
158 }
159
160/* Call given init function: used for init function dependencies. */
161#define vlib_call_init_function(vm, x) \
162 ({ \
163 extern vlib_init_function_t * VLIB_INIT_FUNCTION_SYMBOL (x); \
164 vlib_init_function_t * _f = VLIB_INIT_FUNCTION_SYMBOL (x); \
165 clib_error_t * _error = 0; \
166 if (! hash_get (vm->init_functions_called, _f)) \
167 { \
168 hash_set1 (vm->init_functions_called, _f); \
169 _error = _f (vm); \
170 } \
171 _error; \
172 })
173
Dave Barach1f49ed62016-02-24 11:29:06 -0500174/* Don't call given init function: used to suppress parts of the netstack */
175#define vlib_mark_init_function_complete(vm, x) \
176 ({ \
177 extern vlib_init_function_t * VLIB_INIT_FUNCTION_SYMBOL (x); \
178 vlib_init_function_t * _f = VLIB_INIT_FUNCTION_SYMBOL (x); \
179 hash_set1 (vm->init_functions_called, _f); \
180 })
181
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182#define vlib_call_post_graph_init_function(vm, x) \
183 ({ \
184 extern vlib_init_function_t * VLIB_POST_GRAPH_INIT_FUNCTION_SYMBOL (x); \
185 vlib_init_function_t * _f = VLIB_POST_GRAPH_INIT_FUNCTION_SYMBOL (x); \
186 clib_error_t * _error = 0; \
187 if (! hash_get (vm->init_functions_called, _f)) \
188 { \
189 hash_set1 (vm->init_functions_called, _f); \
190 _error = _f (vm); \
191 } \
192 _error; \
193 })
194
195#define vlib_call_config_function(vm, x) \
196 ({ \
197 vlib_config_function_runtime_t * _r; \
198 clib_error_t * _error = 0; \
199 extern vlib_config_function_runtime_t \
200 VLIB_CONFIG_FUNCTION_SYMBOL (x); \
201 \
202 _r = &VLIB_CONFIG_FUNCTION_SYMBOL (x); \
203 if (! hash_get (vm->init_functions_called, _r->function)) \
204 { \
205 hash_set1 (vm->init_functions_called, _r->function); \
206 _error = _r->function (vm, &_r->input); \
207 } \
208 _error; \
209 })
210
211/* External functions. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400212clib_error_t *vlib_call_all_init_functions (struct vlib_main_t *vm);
213clib_error_t *vlib_call_all_config_functions (struct vlib_main_t *vm,
214 unformat_input_t * input,
215 int is_early);
216clib_error_t *vlib_call_all_main_loop_enter_functions (struct vlib_main_t
217 *vm);
218clib_error_t *vlib_call_all_main_loop_exit_functions (struct vlib_main_t *vm);
219clib_error_t *vlib_call_init_exit_functions (struct vlib_main_t *vm,
220 _vlib_init_function_list_elt_t *
221 head, int call_once);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700222
223#define foreach_vlib_module_reference \
224 _ (node_cli) \
225 _ (trace_cli)
226
227/* Dummy function to get node_cli.c linked in. */
228#define _(x) void vlib_##x##_reference (void);
229foreach_vlib_module_reference
230#undef _
Ed Warnickecb9cada2015-12-08 15:45:58 -0700231#endif /* included_vlib_init_h */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400232/*
233 * fd.io coding-style-patch-verification: ON
234 *
235 * Local Variables:
236 * eval: (c-set-style "gnu")
237 * End:
238 */