Statistics
| Branch: | Revision:

iof-bird-daemon / conf / conf.c @ 9b0a0ba9

History | View | Annotate | Download (14.8 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/event.h"
53
#include "sysdep/unix/timer.h"
54
#include "conf/conf.h"
55

    
56

    
57
static jmp_buf conf_jmpbuf;
58

    
59
struct config *config, *new_config;
60

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

    
68
static event *config_event;                /* Event for finalizing reconfiguration */
69
static timer *config_timer;                /* Timer for scheduled configuration rollback */
70

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

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

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

    
97
  init_list(&c->tests);
98
  c->mrtdump_file = -1; /* Hack, this should be sysdep-specific */
99
  c->pool = p;
100
  c->mem = l;
101
  c->file_name = ndup;
102
  c->load_time = now;
103
  c->tf_route = c->tf_proto = (struct timeformat){"%T", "%F", 20*3600};
104
  c->tf_base = c->tf_log = (struct timeformat){"%F %T", NULL, 0};
105
  c->gr_wait = DEFAULT_GR_WAIT;
106

    
107
  return c;
108
}
109

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

    
133
  cf_lex_init(0, c);
134
  sysdep_preconfig(c);
135
  protos_preconfig(c);
136
  rt_preconfig(c);
137
  cf_parse();
138

    
139
  if (EMPTY_LIST(c->protos))
140
    cf_error("No protocol is specified in the config file");
141

    
142
  /*
143
  if (!c->router_id)
144
    cf_error("Router ID must be configured manually");
145
  */
146

    
147
  done = 1;
148

    
149
cleanup:
150
  new_config = NULL;
151
  cfg_mem = NULL;
152
  return done;
153
}
154

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

    
172
  cf_lex_init(1, c);
173
  cf_parse();
174
  done = 1;
175

    
176
cleanup:
177
  c->sym_fallback = NULL;
178
  new_config = NULL;
179
  cfg_mem = NULL;
180
  return done;
181
}
182

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

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

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

    
213
static int
214
global_commit(struct config *new, struct config *old)
215
{
216
  if (!old)
217
    return 0;
218

    
219
  if (!ipa_equal(old->listen_bgp_addr, new->listen_bgp_addr) ||
220
      (old->listen_bgp_port != new->listen_bgp_port) ||
221
      (old->listen_bgp_flags != new->listen_bgp_flags))
222
    log(L_WARN "Reconfiguration of BGP listening socket not implemented, please restart BIRD.");
223

    
224
  if (!new->router_id)
225
    {
226
      new->router_id = old->router_id;
227

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

    
238
  return 0;
239
}
240

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

    
252
  old_config = config;
253
  old_cftype = type;
254
  config = c;
255

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

    
260
  if (old_config)
261
    old_config->obstacle_count++;
262

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

    
272
  int obs = 0;
273
  if (old_config)
274
    obs = --old_config->obstacle_count;
275

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

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

    
286
  configuring = 0;
287
  if (old_config)
288
    log(L_INFO "Reconfigured");
289

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

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

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

    
339
  undo_available = 1;
340
  if (timeout > 0)
341
    tm_start(config_timer, timeout);
342
  else
343
    tm_stop(config_timer);
344

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

    
355
      future_cftype = type;
356
      future_config = c;
357
      return CONF_QUEUED;
358
    }
359

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

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

    
384
  tm_stop(config_timer);
385

    
386
  return CONF_CONFIRM;
387
}
388

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

    
416
  if (!undo_available || !old_config)
417
    return CONF_NOTHING;
418

    
419
  undo_available = 0;
420
  tm_stop(config_timer);
421

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

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

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

    
449
extern void cmd_reconfig_undo_notify(void);
450

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

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

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

    
468
  config_timer = tm_new(&root_pool);
469
  config_timer->hook = config_timeout;
470
}
471

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

    
483
  if (shutting_down)
484
    return;
485

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

    
493
  config_commit(c, RECONFIG_HARD, 0);
494
  shutting_down = 1;
495
}
496

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

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

    
522
/**
523
 * cfg_strdup - copy a string to config memory
524
 * @c: string to copy
525
 *
526
 * cfg_strdup() creates a new copy of the string in the memory
527
 * pool associated with the configuration being currently parsed.
528
 * It's often used when a string literal occurs in the configuration
529
 * and we want to preserve it for further use.
530
 */
531
char *
532
cfg_strdup(const char *c)
533
{
534
  int l = strlen(c) + 1;
535
  char *z = cfg_allocu(l);
536
  memcpy(z, c, l);
537
  return z;
538
}
539

    
540

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

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