]> git.ipfire.org Git - thirdparty/bird.git/blob - conf/conf.c
Filter: Move argument list reversal from function_call to var_list
[thirdparty/bird.git] / conf / conf.c
1 /*
2 * BIRD Internet Routing Daemon -- Configuration File Handling
3 *
4 * (c) 1998--2000 Martin Mares <mj@ucw.cz>
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL.
7 */
8
9 /**
10 * DOC: Configuration manager
11 *
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.
16 *
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.
20 *
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.
31 *
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().
36 *
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.
41 */
42
43 #include <setjmp.h>
44 #include <stdarg.h>
45
46 #undef LOCAL_DEBUG
47
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"
59
60
61 static jmp_buf conf_jmpbuf;
62
63 struct config *config, *new_config;
64 pool *config_pool;
65
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 */
72
73 static event *config_event; /* Event for finalizing reconfiguration */
74 static timer *config_timer; /* Timer for scheduled configuration rollback */
75
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 */
81
82 /**
83 * config_alloc - allocate a new configuration
84 * @name: name of the config
85 *
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.
89 */
90 struct config *
91 config_alloc(const char *name)
92 {
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));
96
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);
101
102 init_list(&c->tests);
103 init_list(&c->symbols);
104 c->mrtdump_file = -1; /* Hack, this should be sysdep-specific */
105 c->pool = p;
106 c->mem = l;
107 c->file_name = ndup;
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;
112
113 return c;
114 }
115
116 /**
117 * config_parse - parse a configuration
118 * @c: configuration
119 *
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.
124 *
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.
128 */
129 int
130 config_parse(struct config *c)
131 {
132 int done = 0;
133 DBG("Parsing configuration file `%s'\n", c->file_name);
134 new_config = c;
135 cfg_mem = c->mem;
136 if (setjmp(conf_jmpbuf))
137 goto cleanup;
138
139 cf_lex_init(0, c);
140 sysdep_preconfig(c);
141 protos_preconfig(c);
142 rt_preconfig(c);
143 cf_parse();
144 rt_postconfig(c);
145
146 if (EMPTY_LIST(c->protos))
147 cf_error("No protocol is specified in the config file");
148
149 /*
150 if (!c->router_id)
151 cf_error("Router ID must be configured manually");
152 */
153
154 done = 1;
155
156 cleanup:
157 new_config = NULL;
158 cfg_mem = NULL;
159 return done;
160 }
161
162 /**
163 * cli_parse - parse a CLI command
164 * @c: temporary config structure
165 *
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.
168 */
169 int
170 cli_parse(struct config *c)
171 {
172 int done = 0;
173 new_config = c;
174 cfg_mem = c->mem;
175 if (setjmp(conf_jmpbuf))
176 goto cleanup;
177
178 cf_lex_init(1, c);
179 cf_parse();
180 done = 1;
181
182 cleanup:
183 new_config = NULL;
184 cfg_mem = NULL;
185 return done;
186 }
187
188 /**
189 * config_free - free a configuration
190 * @c: configuration to be freed
191 *
192 * This function takes a &config structure and frees all resources
193 * associated with it.
194 */
195 void
196 config_free(struct config *c)
197 {
198 if (!c)
199 return;
200
201 ASSERT(!c->obstacle_count);
202
203 rfree(c->pool);
204 }
205
206 /**
207 * config_free_old - free stored old configuration
208 *
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.
213 */
214 void
215 config_free_old(void)
216 {
217 if (!old_config || old_config->obstacle_count)
218 return;
219
220 tm_stop(config_timer);
221 undo_available = 0;
222
223 config_free(old_config);
224 old_config = NULL;
225 }
226
227 void
228 config_add_obstacle(struct config *c)
229 {
230 DBG("+++ adding obstacle %d\n", c->obstacle_count);
231 c->obstacle_count++;
232 }
233
234 void
235 config_del_obstacle(struct config *c)
236 {
237 DBG("+++ deleting obstacle %d\n", c->obstacle_count);
238 c->obstacle_count--;
239 if (!c->obstacle_count && (c != config))
240 ev_schedule(config_event);
241 }
242
243 static int
244 global_commit(struct config *new, struct config *old)
245 {
246 if (!new->hostname)
247 {
248 new->hostname = get_hostname(new->mem);
249
250 if (!new->hostname)
251 log(L_WARN "Cannot determine hostname");
252 }
253
254 if (!old)
255 return 0;
256
257 if (!new->router_id)
258 {
259 new->router_id = old->router_id;
260
261 if (new->router_id_from)
262 {
263 u32 id = if_choose_router_id(new->router_id_from, old->router_id);
264 if (!id)
265 log(L_WARN "Cannot determine router ID, using old one");
266 else
267 new->router_id = id;
268 }
269 }
270
271 return 0;
272 }
273
274 static int
275 config_do_commit(struct config *c, int type)
276 {
277 if (type == RECONFIG_UNDO)
278 {
279 c = old_config;
280 type = old_cftype;
281 }
282 else
283 config_free(old_config);
284
285 old_config = config;
286 old_cftype = type;
287 config = c;
288
289 configuring = 1;
290 if (old_config && !config->shutdown)
291 log(L_INFO "Reconfiguring");
292
293 if (old_config)
294 old_config->obstacle_count++;
295
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);
302 DBG("rt_commit\n");
303 rt_commit(c, old_config);
304 DBG("protos_commit\n");
305 protos_commit(c, old_config, force_restart, type);
306
307 int obs = 0;
308 if (old_config)
309 obs = --old_config->obstacle_count;
310
311 DBG("do_commit finished with %d obstacles remaining\n", obs);
312 return !obs;
313 }
314
315 static void
316 config_done(void *unused UNUSED)
317 {
318 if (config->shutdown)
319 sysdep_shutdown_done();
320
321 configuring = 0;
322 if (old_config)
323 log(L_INFO "Reconfigured");
324
325 if (future_cftype)
326 {
327 int type = future_cftype;
328 struct config *conf = future_config;
329 future_cftype = RECONFIG_NONE;
330 future_config = NULL;
331
332 log(L_INFO "Reconfiguring to queued configuration");
333 if (config_do_commit(conf, type))
334 config_done(NULL);
335 }
336 }
337
338 /**
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)
343 *
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.
354 *
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.
358 *
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
363 * are accepted.
364 */
365 int
366 config_commit(struct config *c, int type, uint timeout)
367 {
368 if (shutting_down)
369 {
370 config_free(c);
371 return CONF_SHUTDOWN;
372 }
373
374 undo_available = 1;
375 if (timeout)
376 tm_start(config_timer, timeout S);
377 else
378 tm_stop(config_timer);
379
380 if (configuring)
381 {
382 if (future_cftype)
383 {
384 log(L_INFO "Queueing new configuration, ignoring the one already queued");
385 config_free(future_config);
386 }
387 else
388 log(L_INFO "Queueing new configuration");
389
390 future_cftype = type;
391 future_config = c;
392 return CONF_QUEUED;
393 }
394
395 if (config_do_commit(c, type))
396 {
397 config_done(NULL);
398 return CONF_DONE;
399 }
400 return CONF_PROGRESS;
401 }
402
403 /**
404 * config_confirm - confirm a commited configuration
405 *
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.
409 *
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).
412 */
413 int
414 config_confirm(void)
415 {
416 if (config_timer->expires == 0)
417 return CONF_NOTHING;
418
419 tm_stop(config_timer);
420
421 return CONF_CONFIRM;
422 }
423
424 /**
425 * config_undo - undo a configuration
426 *
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).
436 *
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.
444 */
445 int
446 config_undo(void)
447 {
448 if (shutting_down)
449 return CONF_SHUTDOWN;
450
451 if (!undo_available || !old_config)
452 return CONF_NOTHING;
453
454 undo_available = 0;
455 tm_stop(config_timer);
456
457 if (configuring)
458 {
459 if (future_cftype)
460 {
461 config_free(future_config);
462 future_config = NULL;
463
464 log(L_INFO "Removing queued configuration");
465 future_cftype = RECONFIG_NONE;
466 return CONF_UNQUEUED;
467 }
468 else
469 {
470 log(L_INFO "Queueing undo configuration");
471 future_cftype = RECONFIG_UNDO;
472 return CONF_QUEUED;
473 }
474 }
475
476 if (config_do_commit(NULL, RECONFIG_UNDO))
477 {
478 config_done(NULL);
479 return CONF_DONE;
480 }
481 return CONF_PROGRESS;
482 }
483
484 int
485 config_status(void)
486 {
487 if (shutting_down)
488 return CONF_SHUTDOWN;
489
490 if (configuring)
491 return future_cftype ? CONF_QUEUED : CONF_PROGRESS;
492
493 return CONF_DONE;
494 }
495
496 btime
497 config_timer_status(void)
498 {
499 return tm_active(config_timer) ? tm_remains(config_timer) : -1;
500 }
501
502 extern void cmd_reconfig_undo_notify(void);
503
504 static void
505 config_timeout(timer *t UNUSED)
506 {
507 log(L_INFO "Config timeout expired, starting undo");
508 cmd_reconfig_undo_notify();
509
510 int r = config_undo();
511 if (r < 0)
512 log(L_ERR "Undo request failed");
513 }
514
515 void
516 config_init(void)
517 {
518 config_pool = rp_new(&root_pool, "Configurations");
519
520 config_event = ev_new(config_pool);
521 config_event->hook = config_done;
522
523 config_timer = tm_new(config_pool);
524 config_timer->hook = config_timeout;
525 }
526
527 /**
528 * order_shutdown - order BIRD shutdown
529 *
530 * This function initiates shutdown of BIRD. It's accomplished by asking
531 * for switching to an empty configuration.
532 */
533 void
534 order_shutdown(int gr)
535 {
536 struct config *c;
537
538 if (shutting_down)
539 return;
540
541 if (!gr)
542 log(L_INFO "Shutting down");
543 else
544 log(L_INFO "Shutting down for graceful restart");
545
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));
552 c->shutdown = 1;
553 c->gr_down = gr;
554
555 config_commit(c, RECONFIG_HARD, 0);
556 shutting_down = 1;
557 }
558
559 /**
560 * cf_error - report a configuration error
561 * @msg: printf-like format string
562 *
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.
566 */
567 void
568 cf_error(const char *msg, ...)
569 {
570 char buf[1024];
571 va_list args;
572
573 va_start(args, msg);
574 if (bvsnprintf(buf, sizeof(buf), msg, args) < 0)
575 strcpy(buf, "<bug: error message too long>");
576 va_end(args);
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;
581 cf_lex_unwind();
582 longjmp(conf_jmpbuf, 1);
583 }
584
585 /**
586 * cfg_strdup - copy a string to config memory
587 * @c: string to copy
588 *
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.
593 */
594 char *
595 cfg_strdup(const char *c)
596 {
597 int l = strlen(c) + 1;
598 char *z = cfg_allocu(l);
599 memcpy(z, c, l);
600 return z;
601 }
602
603
604 void
605 cfg_copy_list(list *dest, list *src, unsigned node_size)
606 {
607 node *dn, *sn;
608
609 init_list(dest);
610 WALK_LIST(sn, *src)
611 {
612 dn = cfg_alloc(node_size);
613 memcpy(dn, sn, node_size);
614 memset(dn, 0, sizeof(node));
615 add_tail(dest, dn);
616 }
617 }