Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 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 Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 51 | typedef clib_error_t *(vlib_init_function_t) (struct vlib_main_t * vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 52 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 53 | typedef struct _vlib_init_function_list_elt |
| 54 | { |
| 55 | struct _vlib_init_function_list_elt *next_init_function; |
| 56 | vlib_init_function_t *f; |
Dave Barach | f8d5068 | 2019-05-14 18:01:44 -0400 | [diff] [blame] | 57 | char *name; |
| 58 | char **runs_before; |
| 59 | char **runs_after; |
| 60 | char **init_order; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 61 | } _vlib_init_function_list_elt_t; |
| 62 | |
| 63 | /* Configuration functions: called with configuration input just before |
| 64 | main polling loop starts. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 65 | typedef clib_error_t *(vlib_config_function_t) (struct vlib_main_t * vm, |
| 66 | unformat_input_t * input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 67 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 68 | typedef struct vlib_config_function_runtime_t |
| 69 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 70 | /* Function to call. Set to null once function has already been called. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 71 | vlib_config_function_t *function; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 72 | |
| 73 | /* Input for function. */ |
| 74 | unformat_input_t input; |
| 75 | |
| 76 | /* next config function registration */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 77 | struct vlib_config_function_runtime_t *next_registration; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 78 | |
| 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 Marion | 72d2c4f | 2018-04-05 21:32:29 +0200 | [diff] [blame] | 86 | #define VLIB_REMOVE_FROM_LINKED_LIST(first,p,next) \ |
| 87 | { \ |
Damjan Marion | 13adc3d | 2018-04-09 20:59:53 +0200 | [diff] [blame] | 88 | ASSERT (first); \ |
Damjan Marion | 72d2c4f | 2018-04-05 21:32:29 +0200 | [diff] [blame] | 89 | if (first == p) \ |
| 90 | first = (p)->next; \ |
| 91 | else \ |
| 92 | { \ |
| 93 | __typeof__ (p) current = first; \ |
Damjan Marion | 13adc3d | 2018-04-09 20:59:53 +0200 | [diff] [blame] | 94 | 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 Marion | 72d2c4f | 2018-04-05 21:32:29 +0200 | [diff] [blame] | 104 | } \ |
| 105 | } |
| 106 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 107 | #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 Marion | 6e36351 | 2018-08-10 22:39:11 +0200 | [diff] [blame] | 122 | #ifndef CLIB_MARCH_VARIANT |
Dave Barach | f8d5068 | 2019-05-14 18:01:44 -0400 | [diff] [blame] | 123 | #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_main_t * vm = vlib_get_main(); \ |
| 131 | _vlib_init_function_##tag_##x.next_init_function \ |
| 132 | = vm->tag##_function_registrations; \ |
| 133 | vm->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_main_t * vm = vlib_get_main(); \ |
| 142 | _vlib_init_function_list_elt_t *this, *prev; \ |
| 143 | this = vm->tag##_function_registrations; \ |
| 144 | if (this == 0) \ |
| 145 | return; \ |
| 146 | if (this->f == &x) \ |
| 147 | { \ |
| 148 | vm->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 = \ |
| 158 | this->next_init_function; \ |
| 159 | return; \ |
| 160 | } \ |
| 161 | prev = this; \ |
| 162 | this = this->next_init_function; \ |
| 163 | } \ |
| 164 | } \ |
| 165 | static _vlib_init_function_list_elt_t _vlib_init_function_##tag_##x |
Damjan Marion | 6e36351 | 2018-08-10 22:39:11 +0200 | [diff] [blame] | 166 | #else |
| 167 | /* create unused pointer to silence compiler warnings and get whole |
| 168 | function optimized out */ |
| 169 | #define VLIB_DECLARE_INIT_FUNCTION(x, tag) \ |
Dave Barach | f8d5068 | 2019-05-14 18:01:44 -0400 | [diff] [blame] | 170 | static __clib_unused void * __clib_unused_##tag##_##x = x |
Damjan Marion | 6e36351 | 2018-08-10 22:39:11 +0200 | [diff] [blame] | 171 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 172 | |
| 173 | #define VLIB_INIT_FUNCTION(x) VLIB_DECLARE_INIT_FUNCTION(x,init) |
Damjan Marion | e9f929b | 2017-03-16 11:32:09 +0100 | [diff] [blame] | 174 | #define VLIB_WORKER_INIT_FUNCTION(x) VLIB_DECLARE_INIT_FUNCTION(x,worker_init) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 175 | |
| 176 | #define VLIB_MAIN_LOOP_ENTER_FUNCTION(x) \ |
| 177 | VLIB_DECLARE_INIT_FUNCTION(x,main_loop_enter) |
| 178 | #define VLIB_MAIN_LOOP_EXIT_FUNCTION(x) \ |
| 179 | VLIB_DECLARE_INIT_FUNCTION(x,main_loop_exit) |
| 180 | |
Damjan Marion | 6e36351 | 2018-08-10 22:39:11 +0200 | [diff] [blame] | 181 | #ifndef CLIB_MARCH_VARIANT |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 182 | #define VLIB_CONFIG_FUNCTION(x,n,...) \ |
| 183 | __VA_ARGS__ vlib_config_function_runtime_t \ |
| 184 | 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_main_t * vm = vlib_get_main(); \ |
| 190 | VLIB_CONFIG_FUNCTION_SYMBOL(x).next_registration \ |
| 191 | = vm->config_function_registrations; \ |
| 192 | vm->config_function_registrations \ |
| 193 | = &VLIB_CONFIG_FUNCTION_SYMBOL(x); \ |
| 194 | } \ |
Damjan Marion | 72d2c4f | 2018-04-05 21:32:29 +0200 | [diff] [blame] | 195 | static void __vlib_rm_config_function_##x (void) \ |
| 196 | __attribute__((__destructor__)) ; \ |
| 197 | static void __vlib_rm_config_function_##x (void) \ |
| 198 | { \ |
| 199 | vlib_main_t * vm = vlib_get_main(); \ |
| 200 | vlib_config_function_runtime_t *p = \ |
| 201 | & VLIB_CONFIG_FUNCTION_SYMBOL (x); \ |
| 202 | VLIB_REMOVE_FROM_LINKED_LIST \ |
| 203 | (vm->config_function_registrations, p, next_registration);\ |
| 204 | } \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 205 | vlib_config_function_runtime_t \ |
| 206 | VLIB_CONFIG_FUNCTION_SYMBOL (x) \ |
| 207 | = { \ |
| 208 | .name = n, \ |
| 209 | .function = x, \ |
| 210 | .is_early = 0, \ |
| 211 | } |
Damjan Marion | 6e36351 | 2018-08-10 22:39:11 +0200 | [diff] [blame] | 212 | #else |
| 213 | /* create unused pointer to silence compiler warnings and get whole |
| 214 | function optimized out */ |
| 215 | #define VLIB_CONFIG_FUNCTION(x,n,...) \ |
| 216 | static __clib_unused vlib_config_function_runtime_t \ |
| 217 | VLIB_CONFIG_FUNCTION_SYMBOL (__clib_unused_##x) \ |
| 218 | = { \ |
| 219 | .name = n, \ |
| 220 | .function = x, \ |
| 221 | .is_early = 0, \ |
| 222 | } |
| 223 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 224 | |
Damjan Marion | 6e36351 | 2018-08-10 22:39:11 +0200 | [diff] [blame] | 225 | #ifndef CLIB_MARCH_VARIANT |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 226 | #define VLIB_EARLY_CONFIG_FUNCTION(x,n,...) \ |
| 227 | __VA_ARGS__ vlib_config_function_runtime_t \ |
| 228 | VLIB_CONFIG_FUNCTION_SYMBOL(x); \ |
| 229 | static void __vlib_add_config_function_##x (void) \ |
| 230 | __attribute__((__constructor__)) ; \ |
| 231 | static void __vlib_add_config_function_##x (void) \ |
| 232 | { \ |
| 233 | vlib_main_t * vm = vlib_get_main(); \ |
| 234 | VLIB_CONFIG_FUNCTION_SYMBOL(x).next_registration \ |
| 235 | = vm->config_function_registrations; \ |
| 236 | vm->config_function_registrations \ |
| 237 | = &VLIB_CONFIG_FUNCTION_SYMBOL(x); \ |
| 238 | } \ |
Damjan Marion | 72d2c4f | 2018-04-05 21:32:29 +0200 | [diff] [blame] | 239 | static void __vlib_rm_config_function_##x (void) \ |
| 240 | __attribute__((__destructor__)) ; \ |
| 241 | static void __vlib_rm_config_function_##x (void) \ |
| 242 | { \ |
| 243 | vlib_main_t * vm = vlib_get_main(); \ |
| 244 | vlib_config_function_runtime_t *p = \ |
| 245 | & VLIB_CONFIG_FUNCTION_SYMBOL (x); \ |
| 246 | VLIB_REMOVE_FROM_LINKED_LIST \ |
| 247 | (vm->config_function_registrations, p, next_registration);\ |
| 248 | } \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 249 | vlib_config_function_runtime_t \ |
| 250 | VLIB_CONFIG_FUNCTION_SYMBOL (x) \ |
| 251 | = { \ |
| 252 | .name = n, \ |
| 253 | .function = x, \ |
| 254 | .is_early = 1, \ |
| 255 | } |
Damjan Marion | 6e36351 | 2018-08-10 22:39:11 +0200 | [diff] [blame] | 256 | #else |
| 257 | /* create unused pointer to silence compiler warnings and get whole |
| 258 | function optimized out */ |
| 259 | #define VLIB_EARLY_CONFIG_FUNCTION(x,n,...) \ |
| 260 | static __clib_unused vlib_config_function_runtime_t \ |
| 261 | VLIB_CONFIG_FUNCTION_SYMBOL (__clib_unused_##x) \ |
| 262 | = { \ |
| 263 | .name = n, \ |
| 264 | .function = x, \ |
| 265 | .is_early = 1, \ |
| 266 | } |
| 267 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 268 | |
| 269 | /* Call given init function: used for init function dependencies. */ |
| 270 | #define vlib_call_init_function(vm, x) \ |
| 271 | ({ \ |
| 272 | extern vlib_init_function_t * VLIB_INIT_FUNCTION_SYMBOL (x); \ |
| 273 | vlib_init_function_t * _f = VLIB_INIT_FUNCTION_SYMBOL (x); \ |
| 274 | clib_error_t * _error = 0; \ |
| 275 | if (! hash_get (vm->init_functions_called, _f)) \ |
| 276 | { \ |
| 277 | hash_set1 (vm->init_functions_called, _f); \ |
| 278 | _error = _f (vm); \ |
| 279 | } \ |
| 280 | _error; \ |
| 281 | }) |
| 282 | |
Dave Barach | 1f49ed6 | 2016-02-24 11:29:06 -0500 | [diff] [blame] | 283 | /* Don't call given init function: used to suppress parts of the netstack */ |
| 284 | #define vlib_mark_init_function_complete(vm, x) \ |
| 285 | ({ \ |
| 286 | extern vlib_init_function_t * VLIB_INIT_FUNCTION_SYMBOL (x); \ |
| 287 | vlib_init_function_t * _f = VLIB_INIT_FUNCTION_SYMBOL (x); \ |
| 288 | hash_set1 (vm->init_functions_called, _f); \ |
| 289 | }) |
| 290 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 291 | #define vlib_call_post_graph_init_function(vm, x) \ |
| 292 | ({ \ |
| 293 | extern vlib_init_function_t * VLIB_POST_GRAPH_INIT_FUNCTION_SYMBOL (x); \ |
| 294 | vlib_init_function_t * _f = VLIB_POST_GRAPH_INIT_FUNCTION_SYMBOL (x); \ |
| 295 | clib_error_t * _error = 0; \ |
| 296 | if (! hash_get (vm->init_functions_called, _f)) \ |
| 297 | { \ |
| 298 | hash_set1 (vm->init_functions_called, _f); \ |
| 299 | _error = _f (vm); \ |
| 300 | } \ |
| 301 | _error; \ |
| 302 | }) |
| 303 | |
| 304 | #define vlib_call_config_function(vm, x) \ |
| 305 | ({ \ |
| 306 | vlib_config_function_runtime_t * _r; \ |
| 307 | clib_error_t * _error = 0; \ |
| 308 | extern vlib_config_function_runtime_t \ |
| 309 | VLIB_CONFIG_FUNCTION_SYMBOL (x); \ |
| 310 | \ |
| 311 | _r = &VLIB_CONFIG_FUNCTION_SYMBOL (x); \ |
| 312 | if (! hash_get (vm->init_functions_called, _r->function)) \ |
| 313 | { \ |
| 314 | hash_set1 (vm->init_functions_called, _r->function); \ |
| 315 | _error = _r->function (vm, &_r->input); \ |
| 316 | } \ |
| 317 | _error; \ |
| 318 | }) |
| 319 | |
| 320 | /* External functions. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 321 | clib_error_t *vlib_call_all_init_functions (struct vlib_main_t *vm); |
| 322 | clib_error_t *vlib_call_all_config_functions (struct vlib_main_t *vm, |
| 323 | unformat_input_t * input, |
| 324 | int is_early); |
| 325 | clib_error_t *vlib_call_all_main_loop_enter_functions (struct vlib_main_t |
| 326 | *vm); |
| 327 | clib_error_t *vlib_call_all_main_loop_exit_functions (struct vlib_main_t *vm); |
| 328 | clib_error_t *vlib_call_init_exit_functions (struct vlib_main_t *vm, |
Dave Barach | f8d5068 | 2019-05-14 18:01:44 -0400 | [diff] [blame] | 329 | _vlib_init_function_list_elt_t ** |
| 330 | headp, int call_once); |
Dave Barach | c602b38 | 2019-06-03 19:48:22 -0400 | [diff] [blame^] | 331 | clib_error_t *vlib_call_init_exit_functions_no_sort (struct vlib_main_t *vm, |
| 332 | _vlib_init_function_list_elt_t |
| 333 | ** headp, int call_once); |
| 334 | clib_error_t *vlib_sort_init_exit_functions (_vlib_init_function_list_elt_t |
| 335 | **); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 336 | #define foreach_vlib_module_reference \ |
| 337 | _ (node_cli) \ |
| 338 | _ (trace_cli) |
| 339 | |
| 340 | /* Dummy function to get node_cli.c linked in. */ |
| 341 | #define _(x) void vlib_##x##_reference (void); |
| 342 | foreach_vlib_module_reference |
| 343 | #undef _ |
Dave Barach | f8d5068 | 2019-05-14 18:01:44 -0400 | [diff] [blame] | 344 | #define VLIB_INITS(...) (char*[]) { __VA_ARGS__, 0} |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 345 | #endif /* included_vlib_init_h */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 346 | /* |
| 347 | * fd.io coding-style-patch-verification: ON |
| 348 | * |
| 349 | * Local Variables: |
| 350 | * eval: (c-set-style "gnu") |
| 351 | * End: |
| 352 | */ |