]>
git.ipfire.org Git - thirdparty/bird.git/blob - conf/conf.c
2 * BIRD Internet Routing Daemon -- Configuration File Handling
4 * (c) 1998--2000 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
10 * DOC: Configuration manager
12 * Configuration of BIRD is complex, yet straightforward. There are three
13 * modules taking care of the configuration: config manager (which takes care
14 * of storage of the config information and controls switching between configs),
15 * lexical analyzer and parser.
17 * The configuration manager stores each config as a &config structure
18 * accompanied by a linear pool from which all information associated
19 * with the config and pointed to by the &config structure is allocated.
21 * There can exist up to four different configurations at one time: an active
22 * one (pointed to by @config), configuration we are just switching from
23 * (@old_config), one queued for the next reconfiguration (@future_config; if
24 * there is one and the user wants to reconfigure once again, we just free the
25 * previous queued config and replace it with the new one) and finally a config
26 * being parsed (@new_config). The stored @old_config is also used for undo
27 * reconfiguration, which works in a similar way. Reconfiguration could also
28 * have timeout (using @config_timer) and undo is automatically called if the
29 * new configuration is not confirmed later. The new config (@new_config) and
30 * associated linear pool (@cfg_mem) is non-NULL only during parsing.
32 * Loading of new configuration is very simple: just call config_alloc() to get
33 * a new &config structure, then use config_parse() to parse a configuration
34 * file and fill all fields of the structure and finally ask the config manager
35 * to switch to the new config by calling config_commit().
37 * CLI commands are parsed in a very similar way -- there is also a stripped-down
38 * &config structure associated with them and they are lex-ed and parsed by the
39 * same functions, only a special fake token is prepended before the command
40 * text to make the parser recognize only the rules corresponding to CLI commands.
48 #include "nest/bird.h"
49 #include "nest/route.h"
50 #include "nest/protocol.h"
51 #include "nest/iface.h"
52 #include "lib/resource.h"
53 #include "lib/string.h"
54 #include "lib/event.h"
55 #include "lib/timer.h"
56 #include "conf/conf.h"
57 #include "filter/filter.h"
58 #include "sysdep/unix/unix.h"
61 static jmp_buf conf_jmpbuf
;
63 struct config
*config
, *new_config
;
66 static struct config
*old_config
; /* Old configuration */
67 static struct config
*future_config
; /* New config held here if recon requested during recon */
68 static int old_cftype
; /* Type of transition old_config -> config (RECONFIG_SOFT/HARD) */
69 static int future_cftype
; /* Type of scheduled transition, may also be RECONFIG_UNDO */
70 /* Note that when future_cftype is RECONFIG_UNDO, then future_config is NULL,
71 therefore proper check for future scheduled config checks future_cftype */
73 static event
*config_event
; /* Event for finalizing reconfiguration */
74 static timer
*config_timer
; /* Timer for scheduled configuration rollback */
76 /* These are public just for cmd_show_status(), should not be accessed elsewhere */
77 int shutting_down
; /* Shutdown requested, do not accept new config changes */
78 int configuring
; /* Reconfiguration is running */
79 int undo_available
; /* Undo was not requested from last reconfiguration */
80 /* Note that both shutting_down and undo_available are related to requests, not processing */
83 * config_alloc - allocate a new configuration
84 * @name: name of the config
86 * This function creates new &config structure, attaches a resource
87 * pool and a linear memory pool to it and makes it available for
88 * further use. Returns a pointer to the structure.
91 config_alloc(const char *name
)
93 pool
*p
= rp_new(config_pool
, "Config");
94 linpool
*l
= lp_new_default(p
);
95 struct config
*c
= lp_allocz(l
, sizeof(struct config
));
97 /* Duplication of name string in local linear pool */
98 uint nlen
= strlen(name
) + 1;
99 char *ndup
= lp_allocu(l
, nlen
);
100 memcpy(ndup
, name
, nlen
);
102 init_list(&c
->tests
);
103 init_list(&c
->symbols
);
104 c
->mrtdump_file
= -1; /* Hack, this should be sysdep-specific */
108 c
->load_time
= current_time();
109 c
->tf_route
= c
->tf_proto
= TM_ISO_SHORT_MS
;
110 c
->tf_base
= c
->tf_log
= TM_ISO_LONG_MS
;
111 c
->gr_wait
= DEFAULT_GR_WAIT
;
117 * config_parse - parse a configuration
120 * config_parse() reads input by calling a hook function pointed to
121 * by @cf_read_hook and parses it according to the configuration
122 * grammar. It also calls all the preconfig and postconfig hooks
123 * before, resp. after parsing.
125 * Result: 1 if the config has been parsed successfully, 0 if any
126 * error has occurred (such as anybody calling cf_error()) and
127 * the @err_msg field has been set to the error message.
130 config_parse(struct config
*c
)
133 DBG("Parsing configuration file `%s'\n", c
->file_name
);
136 if (setjmp(conf_jmpbuf
))
146 if (EMPTY_LIST(c
->protos
))
147 cf_error("No protocol is specified in the config file");
151 cf_error("Router ID must be configured manually");
163 * cli_parse - parse a CLI command
164 * @c: temporary config structure
166 * cli_parse() is similar to config_parse(), but instead of a configuration,
167 * it parses a CLI command. See the CLI module for more information.
170 cli_parse(struct config
*c
)
175 if (setjmp(conf_jmpbuf
))
189 * config_free - free a configuration
190 * @c: configuration to be freed
192 * This function takes a &config structure and frees all resources
193 * associated with it.
196 config_free(struct config
*c
)
201 ASSERT(!c
->obstacle_count
);
207 * config_free_old - free stored old configuration
209 * This function frees the old configuration (%old_config) that is saved for the
210 * purpose of undo. It is useful before parsing a new config when reconfig is
211 * requested, to avoid keeping three (perhaps memory-heavy) configs together.
212 * Configuration is not freed when it is still active during reconfiguration.
215 config_free_old(void)
217 if (!old_config
|| old_config
->obstacle_count
)
220 tm_stop(config_timer
);
223 config_free(old_config
);
228 config_add_obstacle(struct config
*c
)
230 DBG("+++ adding obstacle %d\n", c
->obstacle_count
);
235 config_del_obstacle(struct config
*c
)
237 DBG("+++ deleting obstacle %d\n", c
->obstacle_count
);
239 if (!c
->obstacle_count
&& (c
!= config
))
240 ev_schedule(config_event
);
244 global_commit(struct config
*new, struct config
*old
)
248 new->hostname
= get_hostname(new->mem
);
251 log(L_WARN
"Cannot determine hostname");
259 new->router_id
= old
->router_id
;
261 if (new->router_id_from
)
263 u32 id
= if_choose_router_id(new->router_id_from
, old
->router_id
);
265 log(L_WARN
"Cannot determine router ID, using old one");
275 config_do_commit(struct config
*c
, int type
)
277 if (type
== RECONFIG_UNDO
)
283 config_free(old_config
);
290 if (old_config
&& !config
->shutdown
)
291 log(L_INFO
"Reconfiguring");
294 old_config
->obstacle_count
++;
296 DBG("filter_commit\n");
297 filter_commit(c
, old_config
);
298 DBG("sysdep_commit\n");
299 int force_restart
= sysdep_commit(c
, old_config
);
300 DBG("global_commit\n");
301 force_restart
|= global_commit(c
, old_config
);
303 rt_commit(c
, old_config
);
304 DBG("protos_commit\n");
305 protos_commit(c
, old_config
, force_restart
, type
);
309 obs
= --old_config
->obstacle_count
;
311 DBG("do_commit finished with %d obstacles remaining\n", obs
);
316 config_done(void *unused UNUSED
)
318 if (config
->shutdown
)
319 sysdep_shutdown_done();
323 log(L_INFO
"Reconfigured");
327 int type
= future_cftype
;
328 struct config
*conf
= future_config
;
329 future_cftype
= RECONFIG_NONE
;
330 future_config
= NULL
;
332 log(L_INFO
"Reconfiguring to queued configuration");
333 if (config_do_commit(conf
, type
))
339 * config_commit - commit a configuration
340 * @c: new configuration
341 * @type: type of reconfiguration (RECONFIG_SOFT or RECONFIG_HARD)
342 * @timeout: timeout for undo (in seconds; or 0 for no timeout)
344 * When a configuration is parsed and prepared for use, the
345 * config_commit() function starts the process of reconfiguration.
346 * It checks whether there is already a reconfiguration in progress
347 * in which case it just queues the new config for later processing.
348 * Else it notifies all modules about the new configuration by calling
349 * their commit() functions which can either accept it immediately
350 * or call config_add_obstacle() to report that they need some time
351 * to complete the reconfiguration. After all such obstacles are removed
352 * using config_del_obstacle(), the old configuration is freed and
353 * everything runs according to the new one.
355 * When @timeout is nonzero, the undo timer is activated with given
356 * timeout. The timer is deactivated when config_commit(),
357 * config_confirm() or config_undo() is called.
359 * Result: %CONF_DONE if the configuration has been accepted immediately,
360 * %CONF_PROGRESS if it will take some time to switch to it, %CONF_QUEUED
361 * if it's been queued due to another reconfiguration being in progress now
362 * or %CONF_SHUTDOWN if BIRD is in shutdown mode and no new configurations
366 config_commit(struct config
*c
, int type
, uint timeout
)
371 return CONF_SHUTDOWN
;
376 tm_start(config_timer
, timeout S
);
378 tm_stop(config_timer
);
384 log(L_INFO
"Queueing new configuration, ignoring the one already queued");
385 config_free(future_config
);
388 log(L_INFO
"Queueing new configuration");
390 future_cftype
= type
;
395 if (config_do_commit(c
, type
))
400 return CONF_PROGRESS
;
404 * config_confirm - confirm a commited configuration
406 * When the undo timer is activated by config_commit() with nonzero timeout,
407 * this function can be used to deactivate it and therefore confirm
408 * the current configuration.
410 * Result: %CONF_CONFIRM when the current configuration is confirmed,
411 * %CONF_NONE when there is nothing to confirm (i.e. undo timer is not active).
416 if (config_timer
->expires
== 0)
419 tm_stop(config_timer
);
425 * config_undo - undo a configuration
427 * Function config_undo() can be used to change the current
428 * configuration back to stored %old_config. If no reconfiguration is
429 * running, this stored configuration is commited in the same way as a
430 * new configuration in config_commit(). If there is already a
431 * reconfiguration in progress and no next reconfiguration is
432 * scheduled, then the undo is scheduled for later processing as
433 * usual, but if another reconfiguration is already scheduled, then
434 * such reconfiguration is removed instead (i.e. undo is applied on
435 * the last commit that scheduled it).
437 * Result: %CONF_DONE if the configuration has been accepted immediately,
438 * %CONF_PROGRESS if it will take some time to switch to it, %CONF_QUEUED
439 * if it's been queued due to another reconfiguration being in progress now,
440 * %CONF_UNQUEUED if a scheduled reconfiguration is removed, %CONF_NOTHING
441 * if there is no relevant configuration to undo (the previous config request
442 * was config_undo() too) or %CONF_SHUTDOWN if BIRD is in shutdown mode and
443 * no new configuration changes are accepted.
449 return CONF_SHUTDOWN
;
451 if (!undo_available
|| !old_config
)
455 tm_stop(config_timer
);
461 config_free(future_config
);
462 future_config
= NULL
;
464 log(L_INFO
"Removing queued configuration");
465 future_cftype
= RECONFIG_NONE
;
466 return CONF_UNQUEUED
;
470 log(L_INFO
"Queueing undo configuration");
471 future_cftype
= RECONFIG_UNDO
;
476 if (config_do_commit(NULL
, RECONFIG_UNDO
))
481 return CONF_PROGRESS
;
488 return CONF_SHUTDOWN
;
491 return future_cftype
? CONF_QUEUED
: CONF_PROGRESS
;
497 config_timer_status(void)
499 return tm_active(config_timer
) ? tm_remains(config_timer
) : -1;
502 extern void cmd_reconfig_undo_notify(void);
505 config_timeout(timer
*t UNUSED
)
507 log(L_INFO
"Config timeout expired, starting undo");
508 cmd_reconfig_undo_notify();
510 int r
= config_undo();
512 log(L_ERR
"Undo request failed");
518 config_pool
= rp_new(&root_pool
, "Configurations");
520 config_event
= ev_new(config_pool
);
521 config_event
->hook
= config_done
;
523 config_timer
= tm_new(config_pool
);
524 config_timer
->hook
= config_timeout
;
528 * order_shutdown - order BIRD shutdown
530 * This function initiates shutdown of BIRD. It's accomplished by asking
531 * for switching to an empty configuration.
534 order_shutdown(int gr
)
542 log(L_INFO
"Shutting down");
544 log(L_INFO
"Shutting down for graceful restart");
546 c
= lp_alloc(config
->mem
, sizeof(struct config
));
547 memcpy(c
, config
, sizeof(struct config
));
548 init_list(&c
->protos
);
549 init_list(&c
->tables
);
550 init_list(&c
->symbols
);
551 memset(c
->def_tables
, 0, sizeof(c
->def_tables
));
555 config_commit(c
, RECONFIG_HARD
, 0);
560 * cf_error - report a configuration error
561 * @msg: printf-like format string
563 * cf_error() can be called during execution of config_parse(), that is
564 * from the parser, a preconfig hook or a postconfig hook, to report an
565 * error in the configuration.
568 cf_error(const char *msg
, ...)
574 if (bvsnprintf(buf
, sizeof(buf
), msg
, args
) < 0)
575 strcpy(buf
, "<bug: error message too long>");
577 new_config
->err_msg
= cfg_strdup(buf
);
578 new_config
->err_lino
= ifs
->lino
;
579 new_config
->err_chno
= ifs
->chno
- ifs
->toklen
+ 1;
580 new_config
->err_file_name
= ifs
->file_name
;
582 longjmp(conf_jmpbuf
, 1);
586 * cfg_strdup - copy a string to config memory
589 * cfg_strdup() creates a new copy of the string in the memory
590 * pool associated with the configuration being currently parsed.
591 * It's often used when a string literal occurs in the configuration
592 * and we want to preserve it for further use.
595 cfg_strdup(const char *c
)
597 int l
= strlen(c
) + 1;
598 char *z
= cfg_allocu(l
);
605 cfg_copy_list(list
*dest
, list
*src
, unsigned node_size
)
612 dn
= cfg_alloc(node_size
);
613 memcpy(dn
, sn
, node_size
);
614 memset(dn
, 0, sizeof(node
));