Statistics
| Branch: | Revision:

iof-bird / bird-2.0.1 / conf / conf.c @ 6b3f1a54

History | View | Annotate | Download (14.5 KB)

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

    
59

    
60
static jmp_buf conf_jmpbuf;
61

    
62
struct config *config, *new_config;
63

    
64
static struct config *old_config;        /* Old configuration */
65
static struct config *future_config;        /* New config held here if recon requested during recon */
66
static int old_cftype;                        /* Type of transition old_config -> config (RECONFIG_SOFT/HARD) */
67
static int future_cftype;                /* Type of scheduled transition, may also be RECONFIG_UNDO */
68
/* Note that when future_cftype is RECONFIG_UNDO, then future_config is NULL,
69
   therefore proper check for future scheduled config checks future_cftype */
70

    
71
static event *config_event;                /* Event for finalizing reconfiguration */
72
static timer *config_timer;                /* Timer for scheduled configuration rollback */
73

    
74
/* These are public just for cmd_show_status(), should not be accessed elsewhere */
75
int shutting_down;                        /* Shutdown requested, do not accept new config changes */
76
int configuring;                        /* Reconfiguration is running */
77
int undo_available;                        /* Undo was not requested from last reconfiguration */
78
/* Note that both shutting_down and undo_available are related to requests, not processing */
79

    
80
/**
81
 * config_alloc - allocate a new configuration
82
 * @name: name of the config
83
 *
84
 * This function creates new &config structure, attaches a resource
85
 * pool and a linear memory pool to it and makes it available for
86
 * further use. Returns a pointer to the structure.
87
 */
88
struct config *
89
config_alloc(const char *name)
90
{
91
  pool *p = rp_new(&root_pool, "Config");
92
  linpool *l = lp_new_default(p);
93
  struct config *c = lp_allocz(l, sizeof(struct config));
94

    
95
  /* Duplication of name string in local linear pool */
96
  uint nlen = strlen(name) + 1;
97
  char *ndup = lp_allocu(l, nlen);
98
  memcpy(ndup, name, nlen);
99

    
100
  init_list(&c->tests);
101
  c->mrtdump_file = -1; /* Hack, this should be sysdep-specific */
102
  c->pool = p;
103
  c->mem = l;
104
  c->file_name = ndup;
105
  c->load_time = current_time();
106
  c->tf_route = c->tf_proto = TM_ISO_SHORT_MS;
107
  c->tf_base = c->tf_log = TM_ISO_LONG_MS;
108
  c->gr_wait = DEFAULT_GR_WAIT;
109

    
110
  return c;
111
}
112

    
113
/**
114
 * config_parse - parse a configuration
115
 * @c: configuration
116
 *
117
 * config_parse() reads input by calling a hook function pointed to
118
 * by @cf_read_hook and parses it according to the configuration
119
 * grammar. It also calls all the preconfig and postconfig hooks
120
 * before, resp. after parsing.
121
 *
122
 * Result: 1 if the config has been parsed successfully, 0 if any
123
 * error has occurred (such as anybody calling cf_error()) and
124
 * the @err_msg field has been set to the error message.
125
 */
126
int
127
config_parse(struct config *c)
128
{
129
  int done = 0;
130
  DBG("Parsing configuration file `%s'\n", c->file_name);
131
  new_config = c;
132
  cfg_mem = c->mem;
133
  if (setjmp(conf_jmpbuf))
134
    goto cleanup;
135

    
136
  cf_lex_init(0, c);
137
  sysdep_preconfig(c);
138
  protos_preconfig(c);
139
  rt_preconfig(c);
140
  cf_parse();
141

    
142
  if (EMPTY_LIST(c->protos))
143
    cf_error("No protocol is specified in the config file");
144

    
145
  /*
146
  if (!c->router_id)
147
    cf_error("Router ID must be configured manually");
148
  */
149

    
150
  done = 1;
151

    
152
cleanup:
153
  new_config = NULL;
154
  cfg_mem = NULL;
155
  return done;
156
}
157

    
158
/**
159
 * cli_parse - parse a CLI command
160
 * @c: temporary config structure
161
 *
162
 * cli_parse() is similar to config_parse(), but instead of a configuration,
163
 * it parses a CLI command. See the CLI module for more information.
164
 */
165
int
166
cli_parse(struct config *c)
167
{
168
  int done = 0;
169
  c->fallback = config;
170
  new_config = c;
171
  cfg_mem = c->mem;
172
  if (setjmp(conf_jmpbuf))
173
    goto cleanup;
174

    
175
  cf_lex_init(1, c);
176
  cf_parse();
177
  done = 1;
178

    
179
cleanup:
180
  c->fallback = NULL;
181
  new_config = NULL;
182
  cfg_mem = NULL;
183
  return done;
184
}
185

    
186
/**
187
 * config_free - free a configuration
188
 * @c: configuration to be freed
189
 *
190
 * This function takes a &config structure and frees all resources
191
 * associated with it.
192
 */
193
void
194
config_free(struct config *c)
195
{
196
  if (c)
197
    rfree(c->pool);
198
}
199

    
200
void
201
config_add_obstacle(struct config *c)
202
{
203
  DBG("+++ adding obstacle %d\n", c->obstacle_count);
204
  c->obstacle_count++;
205
}
206

    
207
void
208
config_del_obstacle(struct config *c)
209
{
210
  DBG("+++ deleting obstacle %d\n", c->obstacle_count);
211
  c->obstacle_count--;
212
  if (!c->obstacle_count)
213
    ev_schedule(config_event);
214
}
215

    
216
static int
217
global_commit(struct config *new, struct config *old)
218
{
219
  if (!old)
220
    return 0;
221

    
222
  if (!new->router_id)
223
    {
224
      new->router_id = old->router_id;
225

    
226
      if (new->router_id_from)
227
        {
228
          u32 id = if_choose_router_id(new->router_id_from, old->router_id);
229
          if (!id)
230
            log(L_WARN "Cannot determine router ID, using old one");
231
          else
232
            new->router_id = id;
233
        }
234
    }
235

    
236
  return 0;
237
}
238

    
239
static int
240
config_do_commit(struct config *c, int type)
241
{
242
  if (type == RECONFIG_UNDO)
243
    {
244
      c = old_config;
245
      type = old_cftype;
246
    }
247
  else
248
    config_free(old_config);
249

    
250
  old_config = config;
251
  old_cftype = type;
252
  config = c;
253

    
254
  configuring = 1;
255
  if (old_config && !config->shutdown)
256
    log(L_INFO "Reconfiguring");
257

    
258
  if (old_config)
259
    old_config->obstacle_count++;
260

    
261
  DBG("sysdep_commit\n");
262
  int force_restart = sysdep_commit(c, old_config);
263
  DBG("global_commit\n");
264
  force_restart |= global_commit(c, old_config);
265
  DBG("rt_commit\n");
266
  rt_commit(c, old_config);
267
  DBG("protos_commit\n");
268
  protos_commit(c, old_config, force_restart, type);
269

    
270
  int obs = 0;
271
  if (old_config)
272
    obs = --old_config->obstacle_count;
273

    
274
  DBG("do_commit finished with %d obstacles remaining\n", obs);
275
  return !obs;
276
}
277

    
278
static void
279
config_done(void *unused UNUSED)
280
{
281
  if (config->shutdown)
282
    sysdep_shutdown_done();
283

    
284
  configuring = 0;
285
  if (old_config)
286
    log(L_INFO "Reconfigured");
287

    
288
  if (future_cftype)
289
    {
290
      int type = future_cftype;
291
      struct config *conf = future_config;
292
      future_cftype = RECONFIG_NONE;
293
      future_config = NULL;
294

    
295
      log(L_INFO "Reconfiguring to queued configuration");
296
      if (config_do_commit(conf, type))
297
        config_done(NULL);
298
    }
299
}
300

    
301
/**
302
 * config_commit - commit a configuration
303
 * @c: new configuration
304
 * @type: type of reconfiguration (RECONFIG_SOFT or RECONFIG_HARD)
305
 * @timeout: timeout for undo (in seconds; or 0 for no timeout)
306
 *
307
 * When a configuration is parsed and prepared for use, the
308
 * config_commit() function starts the process of reconfiguration.
309
 * It checks whether there is already a reconfiguration in progress
310
 * in which case it just queues the new config for later processing.
311
 * Else it notifies all modules about the new configuration by calling
312
 * their commit() functions which can either accept it immediately
313
 * or call config_add_obstacle() to report that they need some time
314
 * to complete the reconfiguration. After all such obstacles are removed
315
 * using config_del_obstacle(), the old configuration is freed and
316
 * everything runs according to the new one.
317
 *
318
 * When @timeout is nonzero, the undo timer is activated with given
319
 * timeout. The timer is deactivated when config_commit(),
320
 * config_confirm() or config_undo() is called.
321
 *
322
 * Result: %CONF_DONE if the configuration has been accepted immediately,
323
 * %CONF_PROGRESS if it will take some time to switch to it, %CONF_QUEUED
324
 * if it's been queued due to another reconfiguration being in progress now
325
 * or %CONF_SHUTDOWN if BIRD is in shutdown mode and no new configurations
326
 * are accepted.
327
 */
328
int
329
config_commit(struct config *c, int type, uint timeout)
330
{
331
  if (shutting_down)
332
    {
333
      config_free(c);
334
      return CONF_SHUTDOWN;
335
    }
336

    
337
  undo_available = 1;
338
  if (timeout)
339
    tm_start(config_timer, timeout S);
340
  else
341
    tm_stop(config_timer);
342

    
343
  if (configuring)
344
    {
345
      if (future_cftype)
346
        {
347
          log(L_INFO "Queueing new configuration, ignoring the one already queued");
348
          config_free(future_config);
349
        }
350
      else
351
        log(L_INFO "Queueing new configuration");
352

    
353
      future_cftype = type;
354
      future_config = c;
355
      return CONF_QUEUED;
356
    }
357

    
358
  if (config_do_commit(c, type))
359
    {
360
      config_done(NULL);
361
      return CONF_DONE;
362
    }
363
  return CONF_PROGRESS;
364
}
365

    
366
/**
367
 * config_confirm - confirm a commited configuration
368
 *
369
 * When the undo timer is activated by config_commit() with nonzero timeout,
370
 * this function can be used to deactivate it and therefore confirm
371
 * the current configuration.
372
 *
373
 * Result: %CONF_CONFIRM when the current configuration is confirmed,
374
 * %CONF_NONE when there is nothing to confirm (i.e. undo timer is not active).
375
 */
376
int
377
config_confirm(void)
378
{
379
  if (config_timer->expires == 0)
380
    return CONF_NOTHING;
381

    
382
  tm_stop(config_timer);
383

    
384
  return CONF_CONFIRM;
385
}
386

    
387
/**
388
 * config_undo - undo a configuration
389
 *
390
 * Function config_undo() can be used to change the current
391
 * configuration back to stored %old_config. If no reconfiguration is
392
 * running, this stored configuration is commited in the same way as a
393
 * new configuration in config_commit(). If there is already a
394
 * reconfiguration in progress and no next reconfiguration is
395
 * scheduled, then the undo is scheduled for later processing as
396
 * usual, but if another reconfiguration is already scheduled, then
397
 * such reconfiguration is removed instead (i.e. undo is applied on
398
 * the last commit that scheduled it).
399
 *
400
 * Result: %CONF_DONE if the configuration has been accepted immediately,
401
 * %CONF_PROGRESS if it will take some time to switch to it, %CONF_QUEUED
402
 * if it's been queued due to another reconfiguration being in progress now,
403
 * %CONF_UNQUEUED if a scheduled reconfiguration is removed, %CONF_NOTHING
404
 * if there is no relevant configuration to undo (the previous config request
405
 * was config_undo() too)  or %CONF_SHUTDOWN if BIRD is in shutdown mode and
406
 * no new configuration changes  are accepted.
407
 */
408
int
409
config_undo(void)
410
{
411
  if (shutting_down)
412
    return CONF_SHUTDOWN;
413

    
414
  if (!undo_available || !old_config)
415
    return CONF_NOTHING;
416

    
417
  undo_available = 0;
418
  tm_stop(config_timer);
419

    
420
  if (configuring)
421
    {
422
      if (future_cftype)
423
        {
424
          config_free(future_config);
425
          future_config = NULL;
426

    
427
          log(L_INFO "Removing queued configuration");
428
          future_cftype = RECONFIG_NONE;
429
          return CONF_UNQUEUED;
430
        }
431
      else
432
        {
433
          log(L_INFO "Queueing undo configuration");
434
          future_cftype = RECONFIG_UNDO;
435
          return CONF_QUEUED;
436
        }
437
    }
438

    
439
  if (config_do_commit(NULL, RECONFIG_UNDO))
440
    {
441
      config_done(NULL);
442
      return CONF_DONE;
443
    }
444
  return CONF_PROGRESS;
445
}
446

    
447
extern void cmd_reconfig_undo_notify(void);
448

    
449
static void
450
config_timeout(timer *t UNUSED)
451
{
452
  log(L_INFO "Config timeout expired, starting undo");
453
  cmd_reconfig_undo_notify();
454

    
455
  int r = config_undo();
456
  if (r < 0)
457
    log(L_ERR "Undo request failed");
458
}
459

    
460
void
461
config_init(void)
462
{
463
  config_event = ev_new(&root_pool);
464
  config_event->hook = config_done;
465

    
466
  config_timer = tm_new(&root_pool);
467
  config_timer->hook = config_timeout;
468
}
469

    
470
/**
471
 * order_shutdown - order BIRD shutdown
472
 *
473
 * This function initiates shutdown of BIRD. It's accomplished by asking
474
 * for switching to an empty configuration.
475
 */
476
void
477
order_shutdown(void)
478
{
479
  struct config *c;
480

    
481
  if (shutting_down)
482
    return;
483

    
484
  log(L_INFO "Shutting down");
485
  c = lp_alloc(config->mem, sizeof(struct config));
486
  memcpy(c, config, sizeof(struct config));
487
  init_list(&c->protos);
488
  init_list(&c->tables);
489
  c->shutdown = 1;
490

    
491
  config_commit(c, RECONFIG_HARD, 0);
492
  shutting_down = 1;
493
}
494

    
495
/**
496
 * cf_error - report a configuration error
497
 * @msg: printf-like format string
498
 *
499
 * cf_error() can be called during execution of config_parse(), that is
500
 * from the parser, a preconfig hook or a postconfig hook, to report an
501
 * error in the configuration.
502
 */
503
void
504
cf_error(const char *msg, ...)
505
{
506
  char buf[1024];
507
  va_list args;
508

    
509
  va_start(args, msg);
510
  if (bvsnprintf(buf, sizeof(buf), msg, args) < 0)
511
    strcpy(buf, "<bug: error message too long>");
512
  va_end(args);
513
  new_config->err_msg = cfg_strdup(buf);
514
  new_config->err_lino = ifs->lino;
515
  new_config->err_file_name = ifs->file_name;
516
  cf_lex_unwind();
517
  longjmp(conf_jmpbuf, 1);
518
}
519

    
520
/**
521
 * cfg_strdup - copy a string to config memory
522
 * @c: string to copy
523
 *
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.
528
 */
529
char *
530
cfg_strdup(const char *c)
531
{
532
  int l = strlen(c) + 1;
533
  char *z = cfg_allocu(l);
534
  memcpy(z, c, l);
535
  return z;
536
}
537

    
538

    
539
void
540
cfg_copy_list(list *dest, list *src, unsigned node_size)
541
{
542
  node *dn, *sn;
543

    
544
  init_list(dest);
545
  WALK_LIST(sn, *src)
546
  {
547
    dn = cfg_alloc(node_size);
548
    memcpy(dn, sn, node_size);
549
    add_tail(dest, dn);
550
  }
551
}