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; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 57 | } _vlib_init_function_list_elt_t; |
| 58 | |
| 59 | /* Configuration functions: called with configuration input just before |
| 60 | main polling loop starts. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 61 | typedef clib_error_t *(vlib_config_function_t) (struct vlib_main_t * vm, |
| 62 | unformat_input_t * input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 63 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 64 | typedef struct vlib_config_function_runtime_t |
| 65 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | /* Function to call. Set to null once function has already been called. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 67 | vlib_config_function_t *function; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 68 | |
| 69 | /* Input for function. */ |
| 70 | unformat_input_t input; |
| 71 | |
| 72 | /* next config function registration */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 73 | struct vlib_config_function_runtime_t *next_registration; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 74 | |
| 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) \ |
| 98 | vlib_init_function_t * _VLIB_INIT_FUNCTION_SYMBOL (x, tag) = x; \ |
| 99 | static void __vlib_add_##tag##_function_##x (void) \ |
| 100 | __attribute__((__constructor__)) ; \ |
| 101 | static 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 Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 109 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 110 | |
| 111 | #define VLIB_INIT_FUNCTION(x) VLIB_DECLARE_INIT_FUNCTION(x,init) |
Damjan Marion | e9f929b | 2017-03-16 11:32:09 +0100 | [diff] [blame] | 112 | #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] | 113 | |
| 114 | #define VLIB_MAIN_LOOP_ENTER_FUNCTION(x) \ |
| 115 | VLIB_DECLARE_INIT_FUNCTION(x,main_loop_enter) |
| 116 | #define VLIB_MAIN_LOOP_EXIT_FUNCTION(x) \ |
| 117 | VLIB_DECLARE_INIT_FUNCTION(x,main_loop_exit) |
| 118 | |
| 119 | #define VLIB_CONFIG_FUNCTION(x,n,...) \ |
| 120 | __VA_ARGS__ vlib_config_function_runtime_t \ |
| 121 | VLIB_CONFIG_FUNCTION_SYMBOL(x); \ |
| 122 | static void __vlib_add_config_function_##x (void) \ |
| 123 | __attribute__((__constructor__)) ; \ |
| 124 | static void __vlib_add_config_function_##x (void) \ |
| 125 | { \ |
| 126 | vlib_main_t * vm = vlib_get_main(); \ |
| 127 | VLIB_CONFIG_FUNCTION_SYMBOL(x).next_registration \ |
| 128 | = vm->config_function_registrations; \ |
| 129 | vm->config_function_registrations \ |
| 130 | = &VLIB_CONFIG_FUNCTION_SYMBOL(x); \ |
| 131 | } \ |
| 132 | vlib_config_function_runtime_t \ |
| 133 | VLIB_CONFIG_FUNCTION_SYMBOL (x) \ |
| 134 | = { \ |
| 135 | .name = n, \ |
| 136 | .function = x, \ |
| 137 | .is_early = 0, \ |
| 138 | } |
| 139 | |
| 140 | #define VLIB_EARLY_CONFIG_FUNCTION(x,n,...) \ |
| 141 | __VA_ARGS__ vlib_config_function_runtime_t \ |
| 142 | VLIB_CONFIG_FUNCTION_SYMBOL(x); \ |
| 143 | static void __vlib_add_config_function_##x (void) \ |
| 144 | __attribute__((__constructor__)) ; \ |
| 145 | static void __vlib_add_config_function_##x (void) \ |
| 146 | { \ |
| 147 | vlib_main_t * vm = vlib_get_main(); \ |
| 148 | VLIB_CONFIG_FUNCTION_SYMBOL(x).next_registration \ |
| 149 | = vm->config_function_registrations; \ |
| 150 | vm->config_function_registrations \ |
| 151 | = &VLIB_CONFIG_FUNCTION_SYMBOL(x); \ |
| 152 | } \ |
| 153 | vlib_config_function_runtime_t \ |
| 154 | VLIB_CONFIG_FUNCTION_SYMBOL (x) \ |
| 155 | = { \ |
| 156 | .name = n, \ |
| 157 | .function = x, \ |
| 158 | .is_early = 1, \ |
| 159 | } |
| 160 | |
| 161 | /* Call given init function: used for init function dependencies. */ |
| 162 | #define vlib_call_init_function(vm, x) \ |
| 163 | ({ \ |
| 164 | extern vlib_init_function_t * VLIB_INIT_FUNCTION_SYMBOL (x); \ |
| 165 | vlib_init_function_t * _f = VLIB_INIT_FUNCTION_SYMBOL (x); \ |
| 166 | clib_error_t * _error = 0; \ |
| 167 | if (! hash_get (vm->init_functions_called, _f)) \ |
| 168 | { \ |
| 169 | hash_set1 (vm->init_functions_called, _f); \ |
| 170 | _error = _f (vm); \ |
| 171 | } \ |
| 172 | _error; \ |
| 173 | }) |
| 174 | |
Dave Barach | 1f49ed6 | 2016-02-24 11:29:06 -0500 | [diff] [blame] | 175 | /* Don't call given init function: used to suppress parts of the netstack */ |
| 176 | #define vlib_mark_init_function_complete(vm, x) \ |
| 177 | ({ \ |
| 178 | extern vlib_init_function_t * VLIB_INIT_FUNCTION_SYMBOL (x); \ |
| 179 | vlib_init_function_t * _f = VLIB_INIT_FUNCTION_SYMBOL (x); \ |
| 180 | hash_set1 (vm->init_functions_called, _f); \ |
| 181 | }) |
| 182 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 183 | #define vlib_call_post_graph_init_function(vm, x) \ |
| 184 | ({ \ |
| 185 | extern vlib_init_function_t * VLIB_POST_GRAPH_INIT_FUNCTION_SYMBOL (x); \ |
| 186 | vlib_init_function_t * _f = VLIB_POST_GRAPH_INIT_FUNCTION_SYMBOL (x); \ |
| 187 | clib_error_t * _error = 0; \ |
| 188 | if (! hash_get (vm->init_functions_called, _f)) \ |
| 189 | { \ |
| 190 | hash_set1 (vm->init_functions_called, _f); \ |
| 191 | _error = _f (vm); \ |
| 192 | } \ |
| 193 | _error; \ |
| 194 | }) |
| 195 | |
| 196 | #define vlib_call_config_function(vm, x) \ |
| 197 | ({ \ |
| 198 | vlib_config_function_runtime_t * _r; \ |
| 199 | clib_error_t * _error = 0; \ |
| 200 | extern vlib_config_function_runtime_t \ |
| 201 | VLIB_CONFIG_FUNCTION_SYMBOL (x); \ |
| 202 | \ |
| 203 | _r = &VLIB_CONFIG_FUNCTION_SYMBOL (x); \ |
| 204 | if (! hash_get (vm->init_functions_called, _r->function)) \ |
| 205 | { \ |
| 206 | hash_set1 (vm->init_functions_called, _r->function); \ |
| 207 | _error = _r->function (vm, &_r->input); \ |
| 208 | } \ |
| 209 | _error; \ |
| 210 | }) |
| 211 | |
| 212 | /* External functions. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 213 | clib_error_t *vlib_call_all_init_functions (struct vlib_main_t *vm); |
| 214 | clib_error_t *vlib_call_all_config_functions (struct vlib_main_t *vm, |
| 215 | unformat_input_t * input, |
| 216 | int is_early); |
| 217 | clib_error_t *vlib_call_all_main_loop_enter_functions (struct vlib_main_t |
| 218 | *vm); |
| 219 | clib_error_t *vlib_call_all_main_loop_exit_functions (struct vlib_main_t *vm); |
| 220 | clib_error_t *vlib_call_init_exit_functions (struct vlib_main_t *vm, |
| 221 | _vlib_init_function_list_elt_t * |
| 222 | head, int call_once); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 223 | |
| 224 | #define foreach_vlib_module_reference \ |
| 225 | _ (node_cli) \ |
| 226 | _ (trace_cli) |
| 227 | |
| 228 | /* Dummy function to get node_cli.c linked in. */ |
| 229 | #define _(x) void vlib_##x##_reference (void); |
| 230 | foreach_vlib_module_reference |
| 231 | #undef _ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 232 | #endif /* included_vlib_init_h */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 233 | /* |
| 234 | * fd.io coding-style-patch-verification: ON |
| 235 | * |
| 236 | * Local Variables: |
| 237 | * eval: (c-set-style "gnu") |
| 238 | * End: |
| 239 | */ |