]>
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"
59 static jmp_buf conf_jmpbuf
;
61 struct config
*config
, *new_config
;
63 static struct config
*old_config
; /* Old configuration */
64 static struct config
*future_config
; /* New config held here if recon requested during recon */
65 static int old_cftype
; /* Type of transition old_config -> config (RECONFIG_SOFT/HARD) */
66 static int future_cftype
; /* Type of scheduled transition, may also be RECONFIG_UNDO */
67 /* Note that when future_cftype is RECONFIG_UNDO, then future_config is NULL,
68 therefore proper check for future scheduled config checks future_cftype */
70 static event
*config_event
; /* Event for finalizing reconfiguration */
71 static timer
*config_timer
; /* Timer for scheduled configuration rollback */
73 /* These are public just for cmd_show_status(), should not be accessed elsewhere */
74 int shutting_down
; /* Shutdown requested, do not accept new config changes */
75 int configuring
; /* Reconfiguration is running */
76 int undo_available
; /* Undo was not requested from last reconfiguration */
77 /* Note that both shutting_down and undo_available are related to requests, not processing */
80 * config_alloc - allocate a new configuration
81 * @name: name of the config
83 * This function creates new &config structure, attaches a resource
84 * pool and a linear memory pool to it and makes it available for
85 * further use. Returns a pointer to the structure.
88 config_alloc(byte
*name
)
90 pool
*p
= rp_new(&root_pool
, "Config");
91 linpool
*l
= lp_new(p
, 4080);
92 struct config
*c
= lp_allocz(l
, sizeof(struct config
));
94 /* Duplication of name string in local linear pool */
95 uint nlen
= strlen(name
) + 1;
96 char *ndup
= lp_allocu(l
, nlen
);
97 memcpy(ndup
, name
, nlen
);
99 c
->mrtdump_file
= -1; /* Hack, this should be sysdep-specific */
104 c
->tf_route
= c
->tf_proto
= (struct timeformat
){"%T", "%F", 20*3600};
105 c
->tf_base
= c
->tf_log
= (struct timeformat
){"%F %T", NULL
, 0};
106 c
->gr_wait
= DEFAULT_GR_WAIT
;
112 * config_parse - parse a configuration
115 * config_parse() reads input by calling a hook function pointed to
116 * by @cf_read_hook and parses it according to the configuration
117 * grammar. It also calls all the preconfig and postconfig hooks
118 * before, resp. after parsing.
120 * Result: 1 if the config has been parsed successfully, 0 if any
121 * error has occurred (such as anybody calling cf_error()) and
122 * the @err_msg field has been set to the error message.
125 config_parse(struct config
*c
)
128 DBG("Parsing configuration file `%s'\n", c
->file_name
);
131 if (setjmp(conf_jmpbuf
))
139 protos_postconfig(c
);
140 if (EMPTY_LIST(c
->protos
))
141 cf_error("No protocol is specified in the config file");
144 cf_error("Router ID must be configured manually");
155 * cli_parse - parse a CLI command
156 * @c: temporary config structure
158 * cli_parse() is similar to config_parse(), but instead of a configuration,
159 * it parses a CLI command. See the CLI module for more information.
162 cli_parse(struct config
*c
)
165 c
->sym_fallback
= config
->sym_hash
;
168 if (setjmp(conf_jmpbuf
))
176 c
->sym_fallback
= NULL
;
183 * config_free - free a configuration
184 * @c: configuration to be freed
186 * This function takes a &config structure and frees all resources
187 * associated with it.
190 config_free(struct config
*c
)
197 config_add_obstacle(struct config
*c
)
199 DBG("+++ adding obstacle %d\n", c
->obstacle_count
);
204 config_del_obstacle(struct config
*c
)
206 DBG("+++ deleting obstacle %d\n", c
->obstacle_count
);
208 if (!c
->obstacle_count
)
209 ev_schedule(config_event
);
213 global_commit(struct config
*new, struct config
*old
)
218 if (!ipa_equal(old
->listen_bgp_addr
, new->listen_bgp_addr
) ||
219 (old
->listen_bgp_port
!= new->listen_bgp_port
) ||
220 (old
->listen_bgp_flags
!= new->listen_bgp_flags
))
221 log(L_WARN
"Reconfiguration of BGP listening socket not implemented, please restart BIRD.");
225 new->router_id
= old
->router_id
;
227 if (new->router_id_from
)
229 u32 id
= if_choose_router_id(new->router_id_from
, old
->router_id
);
231 log(L_WARN
"Cannot determine router ID, using old one");
241 config_do_commit(struct config
*c
, int type
)
243 if (type
== RECONFIG_UNDO
)
249 config_free(old_config
);
256 if (old_config
&& !config
->shutdown
)
257 log(L_INFO
"Reconfiguring");
260 old_config
->obstacle_count
++;
262 DBG("sysdep_commit\n");
263 int force_restart
= sysdep_commit(c
, old_config
);
264 DBG("global_commit\n");
265 force_restart
|= global_commit(c
, old_config
);
267 rt_commit(c
, old_config
);
268 DBG("protos_commit\n");
269 protos_commit(c
, old_config
, force_restart
, type
);
273 obs
= --old_config
->obstacle_count
;
275 DBG("do_commit finished with %d obstacles remaining\n", obs
);
280 config_done(void *unused UNUSED
)
282 if (config
->shutdown
)
283 sysdep_shutdown_done();
287 log(L_INFO
"Reconfigured");
291 int type
= future_cftype
;
292 struct config
*conf
= future_config
;
293 future_cftype
= RECONFIG_NONE
;
294 future_config
= NULL
;
296 log(L_INFO
"Reconfiguring to queued configuration");
297 if (config_do_commit(conf
, type
))
303 * config_commit - commit a configuration
304 * @c: new configuration
305 * @type: type of reconfiguration (RECONFIG_SOFT or RECONFIG_HARD)
306 * @timeout: timeout for undo (or 0 for no timeout)
308 * When a configuration is parsed and prepared for use, the
309 * config_commit() function starts the process of reconfiguration.
310 * It checks whether there is already a reconfiguration in progress
311 * in which case it just queues the new config for later processing.
312 * Else it notifies all modules about the new configuration by calling
313 * their commit() functions which can either accept it immediately
314 * or call config_add_obstacle() to report that they need some time
315 * to complete the reconfiguration. After all such obstacles are removed
316 * using config_del_obstacle(), the old configuration is freed and
317 * everything runs according to the new one.
319 * When @timeout is nonzero, the undo timer is activated with given
320 * timeout. The timer is deactivated when config_commit(),
321 * config_confirm() or config_undo() is called.
323 * Result: %CONF_DONE if the configuration has been accepted immediately,
324 * %CONF_PROGRESS if it will take some time to switch to it, %CONF_QUEUED
325 * if it's been queued due to another reconfiguration being in progress now
326 * or %CONF_SHUTDOWN if BIRD is in shutdown mode and no new configurations
330 config_commit(struct config
*c
, int type
, int timeout
)
335 return CONF_SHUTDOWN
;
340 tm_start(config_timer
, timeout
);
342 tm_stop(config_timer
);
348 log(L_INFO
"Queueing new configuration, ignoring the one already queued");
349 config_free(future_config
);
352 log(L_INFO
"Queueing new configuration");
354 future_cftype
= type
;
359 if (config_do_commit(c
, type
))
364 return CONF_PROGRESS
;
368 * config_confirm - confirm a commited configuration
370 * When the undo timer is activated by config_commit() with nonzero timeout,
371 * this function can be used to deactivate it and therefore confirm
372 * the current configuration.
374 * Result: %CONF_CONFIRM when the current configuration is confirmed,
375 * %CONF_NONE when there is nothing to confirm (i.e. undo timer is not active).
380 if (config_timer
->expires
== 0)
383 tm_stop(config_timer
);
389 * config_undo - undo a configuration
391 * Function config_undo() can be used to change the current
392 * configuration back to stored %old_config. If no reconfiguration is
393 * running, this stored configuration is commited in the same way as a
394 * new configuration in config_commit(). If there is already a
395 * reconfiguration in progress and no next reconfiguration is
396 * scheduled, then the undo is scheduled for later processing as
397 * usual, but if another reconfiguration is already scheduled, then
398 * such reconfiguration is removed instead (i.e. undo is applied on
399 * the last commit that scheduled it).
401 * Result: %CONF_DONE if the configuration has been accepted immediately,
402 * %CONF_PROGRESS if it will take some time to switch to it, %CONF_QUEUED
403 * if it's been queued due to another reconfiguration being in progress now,
404 * %CONF_UNQUEUED if a scheduled reconfiguration is removed, %CONF_NOTHING
405 * if there is no relevant configuration to undo (the previous config request
406 * was config_undo() too) or %CONF_SHUTDOWN if BIRD is in shutdown mode and
407 * no new configuration changes are accepted.
413 return CONF_SHUTDOWN
;
415 if (!undo_available
|| !old_config
)
419 tm_stop(config_timer
);
425 config_free(future_config
);
426 future_config
= NULL
;
428 log(L_INFO
"Removing queued configuration");
429 future_cftype
= RECONFIG_NONE
;
430 return CONF_UNQUEUED
;
434 log(L_INFO
"Queueing undo configuration");
435 future_cftype
= RECONFIG_UNDO
;
440 if (config_do_commit(NULL
, RECONFIG_UNDO
))
445 return CONF_PROGRESS
;
448 extern void cmd_reconfig_undo_notify(void);
451 config_timeout(struct timer
*t
)
453 log(L_INFO
"Config timeout expired, starting undo");
454 cmd_reconfig_undo_notify();
456 int r
= config_undo();
458 log(L_ERR
"Undo request failed");
464 config_event
= ev_new(&root_pool
);
465 config_event
->hook
= config_done
;
467 config_timer
= tm_new(&root_pool
);
468 config_timer
->hook
= config_timeout
;
472 * order_shutdown - order BIRD shutdown
474 * This function initiates shutdown of BIRD. It's accomplished by asking
475 * for switching to an empty configuration.
485 log(L_INFO
"Shutting down");
486 c
= lp_alloc(config
->mem
, sizeof(struct config
));
487 memcpy(c
, config
, sizeof(struct config
));
488 init_list(&c
->protos
);
489 init_list(&c
->tables
);
492 config_commit(c
, RECONFIG_HARD
, 0);
497 * cf_error - report a configuration error
498 * @msg: printf-like format string
500 * cf_error() can be called during execution of config_parse(), that is
501 * from the parser, a preconfig hook or a postconfig hook, to report an
502 * error in the configuration.
505 cf_error(char *msg
, ...)
511 if (bvsnprintf(buf
, sizeof(buf
), msg
, args
) < 0)
512 strcpy(buf
, "<bug: error message too long>");
513 new_config
->err_msg
= cfg_strdup(buf
);
514 new_config
->err_lino
= ifs
->lino
;
515 new_config
->err_file_name
= ifs
->file_name
;
517 longjmp(conf_jmpbuf
, 1);
521 * cfg_strdup - copy a string to config memory
524 * cfg_strdup() creates a new copy of the string in the memory
525 * pool associated with the configuration being currently parsed.
526 * It's often used when a string literal occurs in the configuration
527 * and we want to preserve it for further use.
532 int l
= strlen(c
) + 1;
533 char *z
= cfg_allocu(l
);
540 cfg_copy_list(list
*dest
, list
*src
, unsigned node_size
)
547 dn
= cfg_alloc(node_size
);
548 memcpy(dn
, sn
, node_size
);