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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * init.c: mechanism for functions to be called at init/exit.
18 * Copyright (c) 2008 Eliot Dresselhaus
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:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
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.
40 #include <vlib/vlib.h>
41 #include <vppinfra/ptclosure.h>
45 * @brief Init function ordering and execution implementation
46 * Topological sort for all classes of init functions, and
47 * a relatively simple API routine to invoke them.
50 /*? %%clicmd:group_label Init functions %% ?*/
53 comma_split (u8 * s, u8 ** a, u8 ** b)
57 while (*s && *s != ',')
70 * @brief Topological sorter for init function chains.
71 * @param head [in/out] address of the listhead to be sorted
72 * @returns 0 on success, otherwise a clib_error_t *.
75 static clib_error_t *init_exit_function_sort
76 (_vlib_init_function_list_elt_t ** head)
80 u8 **init_f_names = 0;
82 char **these_constraints;
83 char *this_constraint_c;
95 _vlib_init_function_list_elt_t *this_reg = 0;
97 u8 **keys_to_delete = 0;
100 * two hash tables: name to index in init_f_names, and
101 * init function registration pointer by index
103 index_by_name = hash_create_string (0, sizeof (uword));
104 reg_by_index = hash_create (0, sizeof (uword));
108 /* pass 1, collect init fcn names, construct a before b pairs */
111 init_f_name = format (0, "%s%c", this_reg->name, 0);
112 hash_set (reg_by_index, vec_len (init_f_names), (uword) this_reg);
114 hash_set_mem (index_by_name, init_f_name, vec_len (init_f_names));
116 vec_add1 (init_f_names, init_f_name);
118 these_constraints = this_reg->runs_before;
119 while (these_constraints && these_constraints[0])
121 this_constraint_c = these_constraints[0];
123 constraint_tuple = format (0, "%s,%s%c", init_f_name,
124 this_constraint_c, 0);
125 vec_add1 (constraints, constraint_tuple);
129 these_constraints = this_reg->runs_after;
130 while (these_constraints && these_constraints[0])
132 this_constraint_c = these_constraints[0];
134 constraint_tuple = format (0, "%s,%s%c",
135 this_constraint_c, init_f_name, 0);
136 vec_add1 (constraints, constraint_tuple);
140 this_reg = this_reg->next_init_function;
144 * pass 2: collect "a then b then c then d" constraints.
145 * all init fcns must be known at this point.
150 these_constraints = this_reg->init_order;
153 /* Across the list of constraints */
154 while (these_constraints && these_constraints[0])
156 this_constraint_c = these_constraints[0];
157 p = hash_get_mem (index_by_name, this_constraint_c);
161 ("order constraint fcn '%s' not found", this_constraint_c);
168 prev_name = this_constraint_c;
173 constraint_tuple = format (0, "%s,%s%c", prev_name,
174 this_constraint_c, 0);
175 vec_add1 (constraints, constraint_tuple);
176 prev_name = this_constraint_c;
179 this_reg = this_reg->next_init_function;
182 n_init_fns = vec_len (init_f_names);
183 orig = clib_ptclosure_alloc (n_init_fns);
185 for (i = 0; i < vec_len (constraints); i++)
187 this_constraint = constraints[i];
189 if (comma_split (this_constraint, &a_name, &b_name))
190 return clib_error_return (0, "comma_split failed!");
192 p = hash_get_mem (index_by_name, a_name);
194 * Note: the next two errors mean that something is
195 * b0rked. As in: if you code "A runs before on B," and you type
196 * B incorrectly, you lose. Nonexistent init functions are tolerated.
200 clib_warning ("init function '%s' not found (before '%s')",
206 p = hash_get_mem (index_by_name, b_name);
209 clib_warning ("init function '%s' not found (after '%s')",
215 /* add a before b to the original set of constraints */
216 orig[a_index][b_index] = 1;
217 vec_free (this_constraint);
220 /* Compute the positive transitive closure of the original constraints */
221 closure = clib_ptclosure (orig);
223 /* Compute a partial order across feature nodes, if one exists. */
225 for (i = 0; i < n_init_fns; i++)
227 for (j = 0; j < n_init_fns; j++)
230 goto item_constrained;
232 /* Item i can be output */
233 vec_add1 (result, i);
235 for (k = 0; k < n_init_fns; k++)
238 * Add a "Magic" a before a constraint.
239 * This means we'll never output it again
248 /* see if we got a partial order... */
249 if (vec_len (result) != n_init_fns)
250 return clib_error_return
251 (0, "Failed to find a suitable init function order!");
255 * Bind the index variables, and output the feature node name vector
256 * using the partial order we just computed. Result is in stack
257 * order, because the entry with the fewest constraints (e.g. none)
258 * is output first, etc.
259 * Reset the listhead, and add items in result (aka reverse) order.
262 for (i = 0; i < n_init_fns; i++)
264 p = hash_get (reg_by_index, result[i]);
266 this_reg = (_vlib_init_function_list_elt_t *) p[0];
268 this_reg->next_init_function = *head;
272 /* Finally, clean up all the fine data we allocated */
274 hash_foreach_pair (hp, index_by_name,
276 vec_add1 (keys_to_delete, (u8 *)hp->key);
279 hash_free (index_by_name);
280 for (i = 0; i < vec_len (keys_to_delete); i++)
281 vec_free (keys_to_delete[i]);
282 vec_free (keys_to_delete);
283 hash_free (reg_by_index);
285 clib_ptclosure_free (orig);
286 clib_ptclosure_free (closure);
291 * @brief call a set of init / exit / main-loop enter functions
292 * @param vm vlib_main_t
293 * @param head address of the listhead to sort and then invoke
294 * @returns 0 on success, clib_error_t * on error
296 * The "init_functions_called" hash supports a subtle mix of procedural
297 * and formally-specified ordering constraints. The following schemes
298 * are *roughly* equivalent:
300 * static clib_error_t *init_runs_first (vlib_main_t *vm)
302 * clib_error_t *error;
304 * ... do some stuff...
306 * if ((error = vlib_call_init_function (init_runs_next)))
310 * VLIB_INIT_FUNCTION (init_runs_first);
314 * static clib_error_t *init_runs_first (vlib_main_t *vm)
316 * ... do some stuff...
318 * VLIB_INIT_FUNCTION (init_runs_first) =
320 * .runs_before = VLIB_INITS("init_runs_next"),
323 * The first form will [most likely] call "init_runs_next" on the
324 * spot. The second form means that "init_runs_first" runs before
325 * "init_runs_next," possibly much earlier in the sequence.
327 * Please DO NOT construct sets of init functions where A before B
328 * actually means A *right before* B. It's not necessary - simply combine
329 * A and B - and it leads to hugely annoying debugging exercises.
333 vlib_call_init_exit_functions (vlib_main_t * vm,
334 _vlib_init_function_list_elt_t ** headp,
337 clib_error_t *error = 0;
338 _vlib_init_function_list_elt_t *i;
340 if ((error = init_exit_function_sort (headp)))
346 if (call_once && !hash_get (vm->init_functions_called, i->f))
349 hash_set1 (vm->init_functions_called, i->f);
354 i = i->next_init_function;
360 vlib_call_all_init_functions (vlib_main_t * vm)
362 /* Call dummy functions to make sure purely static modules are
364 #define _(f) vlib_##f##_reference ();
365 foreach_vlib_module_reference;
368 return vlib_call_init_exit_functions
369 (vm, &vm->init_function_registrations, 1 /* call_once */ );
373 vlib_call_all_main_loop_enter_functions (vlib_main_t * vm)
375 return vlib_call_init_exit_functions
376 (vm, &vm->main_loop_enter_function_registrations, 1 /* call_once */ );
380 vlib_call_all_main_loop_exit_functions (vlib_main_t * vm)
382 return vlib_call_init_exit_functions
383 (vm, &vm->main_loop_exit_function_registrations, 1 /* call_once */ );
387 vlib_call_all_config_functions (vlib_main_t * vm,
388 unformat_input_t * input, int is_early)
390 clib_error_t *error = 0;
391 vlib_config_function_runtime_t *c, **all;
395 hash = hash_create_string (0, sizeof (uword));
398 c = vm->config_function_registrations;
402 hash_set_mem (hash, c->name, vec_len (all));
404 unformat_init (&c->input, 0, 0);
405 c = c->next_registration;
408 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
412 if (!unformat (input, "%s %v", &s, &v) || !(p = hash_get_mem (hash, s)))
414 error = clib_error_create ("unknown input `%s %v'", s, v);
419 if (vec_len (c->input.buffer) > 0)
420 vec_add1 (c->input.buffer, ' ');
421 vec_add (c->input.buffer, v, vec_len (v));
426 for (i = 0; i < vec_len (all); i++)
430 /* Is this an early config? Are we doing early configs? */
431 if (is_early ^ c->is_early)
434 /* Already called? */
435 if (hash_get (vm->init_functions_called, c->function))
437 hash_set1 (vm->init_functions_called, c->function);
439 error = c->function (vm, &c->input);
445 for (i = 0; i < vec_len (all); i++)
448 unformat_free (&c->input);
456 vlib_init_dump (void)
458 vlib_main_t *vm = vlib_get_main ();
461 _vlib_init_function_list_elt_t *head, *this;
462 head = vm->init_function_registrations;
467 fformat (stdout, "[%d]: %s\n", i++, this->name);
468 this = this->next_init_function;
472 static clib_error_t *
473 show_init_function_command_fn (vlib_main_t * vm,
474 unformat_input_t * input,
475 vlib_cli_command_t * cmd)
480 _vlib_init_function_list_elt_t *head, *this;
481 uword *index_by_name;
483 u8 **init_f_names = 0;
486 _vlib_init_function_list_elt_t *this_reg = 0;
488 u8 **keys_to_delete = 0;
490 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
492 if (unformat (input, "init"))
494 else if (unformat (input, "enter"))
496 else if (unformat (input, "exit"))
498 else if (unformat (input, "verbose %d", &verbose))
500 else if (unformat (input, "verbose"))
509 head = vm->init_function_registrations;
512 head = vm->main_loop_enter_function_registrations;
515 head = vm->main_loop_exit_function_registrations;
518 return clib_error_return (0, "BUG");
527 vlib_cli_output (vm, "[%d]: %s", i++, this->name);
528 this = this->next_init_function;
533 index_by_name = hash_create_string (0, sizeof (uword));
534 reg_by_index = hash_create (0, sizeof (uword));
538 /* collect init fcn names */
541 init_f_name = format (0, "%s%c", this_reg->name, 0);
542 hash_set (reg_by_index, vec_len (init_f_names), (uword) this_reg);
544 hash_set_mem (index_by_name, init_f_name, vec_len (init_f_names));
545 vec_add1 (init_f_names, init_f_name);
547 this_reg = this_reg->next_init_function;
550 for (i = 0; i < n_init_fns; i++)
552 p = hash_get (reg_by_index, i);
554 this_reg = (_vlib_init_function_list_elt_t *) p[0];
555 vlib_cli_output (vm, "[%d] %s", i, this_reg->name);
557 char **runs_before, **runs_after, **init_order;
558 runs_before = this_reg->runs_before;
559 while (runs_before && runs_before[0])
561 _vlib_init_function_list_elt_t *successor;
562 uword successor_index;
563 p = hash_get_mem (index_by_name, runs_before[0]);
566 clib_warning ("couldn't find successor '%s'", runs_before[0]);
570 successor_index = p[0];
571 p = hash_get (reg_by_index, p[0]);
573 successor = (_vlib_init_function_list_elt_t *) p[0];
574 vlib_cli_output (vm, " before '%s' [%lld]",
575 successor->name, successor_index);
578 runs_after = this_reg->runs_after;
579 while (runs_after && runs_after[0])
581 _vlib_init_function_list_elt_t *predecessor;
582 uword predecessor_index;
583 p = hash_get_mem (index_by_name, runs_after[0]);
586 clib_warning ("couldn't find predecessor '%s'",
591 predecessor_index = p[0];
592 p = hash_get (reg_by_index, p[0]);
594 predecessor = (_vlib_init_function_list_elt_t *) p[0];
595 vlib_cli_output (vm, " after '%s' [%lld]",
596 predecessor->name, predecessor_index);
599 init_order = this_reg->init_order;
600 while (init_order && init_order[0])
602 _vlib_init_function_list_elt_t *inorder;
604 p = hash_get_mem (index_by_name, init_order[0]);
607 clib_warning ("couldn't find order element'%s'",
612 inorder_index = p[0];
613 p = hash_get (reg_by_index, p[0]);
615 inorder = (_vlib_init_function_list_elt_t *) p[0];
616 vlib_cli_output (vm, " in order '%s' [%lld]",
617 inorder->name, inorder_index);
623 hash_foreach_pair (hp, index_by_name,
625 vec_add1 (keys_to_delete, (u8 *)hp->key);
628 hash_free (index_by_name);
629 for (i = 0; i < vec_len (keys_to_delete); i++)
630 vec_free (keys_to_delete[i]);
631 vec_free (keys_to_delete);
632 hash_free (reg_by_index);
638 * Show init function order
641 * @cliexstart{show init-function [init | enter | exit] [verbose [nn]]}
645 VLIB_CLI_COMMAND (show_init_function, static) = {
646 .path = "show init-function",
647 .short_help = "show init-function [init | enter | exit][verbose [nn]]",
648 .function = show_init_function_command_fn,
654 * fd.io coding-style-patch-verification: ON
657 * eval: (c-set-style "gnu")