FD.io VPP  v16.06
Vector Packet Processing
init.h
Go to the documentation of this file.
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 
51 typedef clib_error_t * (vlib_init_function_t) (struct vlib_main_t * vm);
52 
53 typedef struct _vlib_init_function_list_elt {
54  struct _vlib_init_function_list_elt * next_init_function;
56 } _vlib_init_function_list_elt_t;
57 
58 /* Configuration functions: called with configuration input just before
59  main polling loop starts. */
61  unformat_input_t * input);
62 
64  /* Function to call. Set to null once function has already been called. */
66 
67  /* Input for function. */
69 
70  /* next config function registration */
72 
73  /* To be invoked as soon as the clib heap is available */
75 
76  /* Name used to distinguish input on command line. */
77  char name[32];
79 
80 #define _VLIB_INIT_FUNCTION_SYMBOL(x, type) \
81  _vlib_##type##_function_##x
82 
83 #define VLIB_INIT_FUNCTION_SYMBOL(x) \
84  _VLIB_INIT_FUNCTION_SYMBOL(x, init)
85 #define VLIB_MAIN_LOOP_ENTER_FUNCTION_SYMBOL(x) \
86  _VLIB_INIT_FUNCTION_SYMBOL(x, main_loop_enter)
87 #define VLIB_MAIN_LOOP_EXIT_FUNCTION_SYMBOL(x) \
88  _VLIB_INIT_FUNCTION_SYMBOL(x, main_loop_exit)
89 #define VLIB_CONFIG_FUNCTION_SYMBOL(x) \
90  _VLIB_INIT_FUNCTION_SYMBOL(x, config)
91 
92 /* Declaration is global (e.g. not static) so that init functions can
93  be called from other modules to resolve init function depend. */
94 
95 #define VLIB_DECLARE_INIT_FUNCTION(x, tag) \
96 vlib_init_function_t * _VLIB_INIT_FUNCTION_SYMBOL (x, tag) = x; \
97 static void __vlib_add_##tag##_function_##x (void) \
98  __attribute__((__constructor__)) ; \
99 static void __vlib_add_##tag##_function_##x (void) \
100 { \
101  vlib_main_t * vm = vlib_get_main(); \
102  static _vlib_init_function_list_elt_t _vlib_init_function; \
103  _vlib_init_function.next_init_function \
104  = vm->tag##_function_registrations; \
105  vm->tag##_function_registrations = &_vlib_init_function; \
106  _vlib_init_function.f = &x; \
107 }
108 
109 #define VLIB_INIT_FUNCTION(x) VLIB_DECLARE_INIT_FUNCTION(x,init)
110 
111 #define VLIB_MAIN_LOOP_ENTER_FUNCTION(x) \
112  VLIB_DECLARE_INIT_FUNCTION(x,main_loop_enter)
113 #define VLIB_MAIN_LOOP_EXIT_FUNCTION(x) \
114 VLIB_DECLARE_INIT_FUNCTION(x,main_loop_exit)
115 
116 #define VLIB_CONFIG_FUNCTION(x,n,...) \
117  __VA_ARGS__ vlib_config_function_runtime_t \
118  VLIB_CONFIG_FUNCTION_SYMBOL(x); \
119 static void __vlib_add_config_function_##x (void) \
120  __attribute__((__constructor__)) ; \
121 static void __vlib_add_config_function_##x (void) \
122 { \
123  vlib_main_t * vm = vlib_get_main(); \
124  VLIB_CONFIG_FUNCTION_SYMBOL(x).next_registration \
125  = vm->config_function_registrations; \
126  vm->config_function_registrations \
127  = &VLIB_CONFIG_FUNCTION_SYMBOL(x); \
128 } \
129  vlib_config_function_runtime_t \
130  VLIB_CONFIG_FUNCTION_SYMBOL (x) \
131  = { \
132  .name = n, \
133  .function = x, \
134  .is_early = 0, \
135  }
136 
137 #define VLIB_EARLY_CONFIG_FUNCTION(x,n,...) \
138  __VA_ARGS__ vlib_config_function_runtime_t \
139  VLIB_CONFIG_FUNCTION_SYMBOL(x); \
140 static void __vlib_add_config_function_##x (void) \
141  __attribute__((__constructor__)) ; \
142 static void __vlib_add_config_function_##x (void) \
143 { \
144  vlib_main_t * vm = vlib_get_main(); \
145  VLIB_CONFIG_FUNCTION_SYMBOL(x).next_registration \
146  = vm->config_function_registrations; \
147  vm->config_function_registrations \
148  = &VLIB_CONFIG_FUNCTION_SYMBOL(x); \
149 } \
150  vlib_config_function_runtime_t \
151  VLIB_CONFIG_FUNCTION_SYMBOL (x) \
152  = { \
153  .name = n, \
154  .function = x, \
155  .is_early = 1, \
156  }
157 
158 /* Call given init function: used for init function dependencies. */
159 #define vlib_call_init_function(vm, x) \
160  ({ \
161  extern vlib_init_function_t * VLIB_INIT_FUNCTION_SYMBOL (x); \
162  vlib_init_function_t * _f = VLIB_INIT_FUNCTION_SYMBOL (x); \
163  clib_error_t * _error = 0; \
164  if (! hash_get (vm->init_functions_called, _f)) \
165  { \
166  hash_set1 (vm->init_functions_called, _f); \
167  _error = _f (vm); \
168  } \
169  _error; \
170  })
171 
172 /* Don't call given init function: used to suppress parts of the netstack */
173 #define vlib_mark_init_function_complete(vm, x) \
174  ({ \
175  extern vlib_init_function_t * VLIB_INIT_FUNCTION_SYMBOL (x); \
176  vlib_init_function_t * _f = VLIB_INIT_FUNCTION_SYMBOL (x); \
177  hash_set1 (vm->init_functions_called, _f); \
178  })
179 
180 #define vlib_call_post_graph_init_function(vm, x) \
181  ({ \
182  extern vlib_init_function_t * VLIB_POST_GRAPH_INIT_FUNCTION_SYMBOL (x); \
183  vlib_init_function_t * _f = VLIB_POST_GRAPH_INIT_FUNCTION_SYMBOL (x); \
184  clib_error_t * _error = 0; \
185  if (! hash_get (vm->init_functions_called, _f)) \
186  { \
187  hash_set1 (vm->init_functions_called, _f); \
188  _error = _f (vm); \
189  } \
190  _error; \
191  })
192 
193 #define vlib_call_config_function(vm, x) \
194  ({ \
195  vlib_config_function_runtime_t * _r; \
196  clib_error_t * _error = 0; \
197  extern vlib_config_function_runtime_t \
198  VLIB_CONFIG_FUNCTION_SYMBOL (x); \
199  \
200  _r = &VLIB_CONFIG_FUNCTION_SYMBOL (x); \
201  if (! hash_get (vm->init_functions_called, _r->function)) \
202  { \
203  hash_set1 (vm->init_functions_called, _r->function); \
204  _error = _r->function (vm, &_r->input); \
205  } \
206  _error; \
207  })
208 
209 /* External functions. */
213  int is_early);
216 clib_error_t *
218  _vlib_init_function_list_elt_t *head,
219  int call_once);
220 
221 #define foreach_vlib_module_reference \
222  _ (node_cli) \
223  _ (trace_cli)
224 
225 /* Dummy function to get node_cli.c linked in. */
226 #define _(x) void vlib_##x##_reference (void);
228 #undef _
229 
230 #endif /* included_vlib_init_h */
clib_error_t * vlib_call_all_main_loop_exit_functions(struct vlib_main_t *vm)
Definition: init.c:84
#define foreach_vlib_module_reference
Definition: init.h:221
clib_error_t * vlib_call_all_init_functions(struct vlib_main_t *vm)
Definition: init.c:66
unformat_input_t input
Definition: init.h:68
clib_error_t *( vlib_config_function_t)(struct vlib_main_t *vm, unformat_input_t *input)
Definition: init.h:60
clib_error_t * vlib_call_init_exit_functions(struct vlib_main_t *vm, _vlib_init_function_list_elt_t *head, int call_once)
Definition: init.c:43
clib_error_t *( vlib_init_function_t)(struct vlib_main_t *vm)
Definition: init.h:51
struct vlib_config_function_runtime_t vlib_config_function_runtime_t
clib_error_t * vlib_call_all_config_functions(struct vlib_main_t *vm, unformat_input_t *input, int is_early)
Definition: init.c:90
clib_error_t * vlib_call_all_main_loop_enter_functions(struct vlib_main_t *vm)
Definition: init.c:78
unsigned char u8
Definition: types.h:56
struct vlib_config_function_runtime_t * next_registration
Definition: init.h:71
struct _unformat_input_t unformat_input_t