Statistics
| Branch: | Revision:

iof-bird-daemon / nest / proto.c @ 62e64905

History | View | Annotate | Download (43.5 KB)

1 2326b001 Martin Mares
/*
2
 *        BIRD -- Protocols
3
 *
4 50fe90ed Martin Mares
 *        (c) 1998--2000 Martin Mares <mj@ucw.cz>
5 2326b001 Martin Mares
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8
9 6b9fa320 Martin Mares
#undef LOCAL_DEBUG
10 7f4a3988 Martin Mares
11 2326b001 Martin Mares
#include "nest/bird.h"
12
#include "nest/protocol.h"
13
#include "lib/resource.h"
14
#include "lib/lists.h"
15 67bd949a Martin Mares
#include "lib/event.h"
16 f14a4bec Martin Mares
#include "lib/string.h"
17 fe7cec12 Martin Mares
#include "conf/conf.h"
18 47b79306 Martin Mares
#include "nest/route.h"
19
#include "nest/iface.h"
20 ae97b946 Martin Mares
#include "nest/cli.h"
21 529c4149 Martin Mares
#include "filter/filter.h"
22 2326b001 Martin Mares
23 acb60628 Ondrej Zajicek
pool *proto_pool;
24 f4a60a9b Ondrej Zajicek (work)
list  proto_list;
25 67bd949a Martin Mares
26 3991d84e Martin Mares
static list protocol_list;
27 67bd949a Martin Mares
28 839380d7 Martin Mares
#define PD(pr, msg, args...) do { if (pr->debug & D_STATES) { log(L_TRACE "%s: " msg, pr->name , ## args); } } while(0)
29
30 ebecb6f6 Ondrej Zajicek
static timer *proto_shutdown_timer;
31 0c791f87 Ondrej Zajicek
static timer *gr_wait_timer;
32
33
#define GRS_NONE        0
34
#define GRS_INIT        1
35
#define GRS_ACTIVE        2
36
#define GRS_DONE        3
37
38
static int graceful_restart_state;
39
static u32 graceful_restart_locks;
40 67bd949a Martin Mares
41
static char *p_states[] = { "DOWN", "START", "UP", "STOP" };
42 d15b0b0a Ondrej Zajicek (work)
static char *c_states[] = { "DOWN", "START", "UP", "FLUSHING" };
43 67bd949a Martin Mares
44 f4a60a9b Ondrej Zajicek (work)
extern struct protocol proto_unix_iface;
45
46 ebecb6f6 Ondrej Zajicek
static void proto_shutdown_loop(struct timer *);
47 50fe90ed Martin Mares
static void proto_rethink_goal(struct proto *p);
48 839380d7 Martin Mares
static char *proto_state_name(struct proto *p);
49 f4a60a9b Ondrej Zajicek (work)
static void channel_verify_limits(struct channel *c);
50
static void channel_reset_limit(struct channel_limit *l);
51 1a54b1c6 Martin Mares
52
53 f4a60a9b Ondrej Zajicek (work)
static inline int proto_is_done(struct proto *p)
54
{ return (p->proto_state == PS_DOWN) && (p->active_channels == 0); }
55 227af309 Ondrej Zajicek
56 f4a60a9b Ondrej Zajicek (work)
static inline int channel_is_active(struct channel *c)
57
{ return (c->channel_state == CS_START) || (c->channel_state == CS_UP); }
58 227af309 Ondrej Zajicek
59
static void
60
proto_log_state_change(struct proto *p)
61
{
62
  if (p->debug & D_STATES)
63 f4a60a9b Ondrej Zajicek (work)
  {
64
    char *name = proto_state_name(p);
65
    if (name != p->last_state_name_announced)
66 227af309 Ondrej Zajicek
    {
67 f4a60a9b Ondrej Zajicek (work)
      p->last_state_name_announced = name;
68
      PD(p, "State changed to %s", proto_state_name(p));
69 227af309 Ondrej Zajicek
    }
70 f4a60a9b Ondrej Zajicek (work)
  }
71 227af309 Ondrej Zajicek
  else
72
    p->last_state_name_announced = NULL;
73 67bd949a Martin Mares
}
74 2326b001 Martin Mares
75 227af309 Ondrej Zajicek
76 f4a60a9b Ondrej Zajicek (work)
struct channel_config *
77
proto_cf_find_channel(struct proto_config *pc, uint net_type)
78 7f4a3988 Martin Mares
{
79 f4a60a9b Ondrej Zajicek (work)
  struct channel_config *cc;
80
81
  WALK_LIST(cc, pc->channels)
82
    if (cc->net_type == net_type)
83
      return cc;
84
85
  return NULL;
86 7f4a3988 Martin Mares
}
87
88 f4a60a9b Ondrej Zajicek (work)
/**
89
 * proto_find_channel_by_table - find channel connected to a routing table
90
 * @p: protocol instance
91
 * @t: routing table
92
 *
93
 * Returns pointer to channel or NULL
94
 */
95
struct channel *
96
proto_find_channel_by_table(struct proto *p, struct rtable *t)
97 1a54b1c6 Martin Mares
{
98 f4a60a9b Ondrej Zajicek (work)
  struct channel *c;
99 c0adf7e9 Ondrej Zajicek
100 f4a60a9b Ondrej Zajicek (work)
  WALK_LIST(c, p->channels)
101
    if (c->table == t)
102
      return c;
103 0c791f87 Ondrej Zajicek
104 f4a60a9b Ondrej Zajicek (work)
  return NULL;
105 1a54b1c6 Martin Mares
}
106
107 3c6269b8 Martin Mares
/**
108 f4a60a9b Ondrej Zajicek (work)
 * proto_add_channel - connect protocol to a routing table
109 3c6269b8 Martin Mares
 * @p: protocol instance
110 f4a60a9b Ondrej Zajicek (work)
 * @cf: channel configuration
111 3c6269b8 Martin Mares
 *
112 f4a60a9b Ondrej Zajicek (work)
 * This function creates a channel between the protocol instance @p and the
113
 * routing table specified in the configuration @cf, making the protocol hear
114
 * all changes in the table and allowing the protocol to update routes in the
115
 * table.
116 c0adf7e9 Ondrej Zajicek
 *
117 f4a60a9b Ondrej Zajicek (work)
 * The channel is linked in the protocol channel list and when active also in
118
 * the table channel list. Channels are allocated from the global resource pool
119
 * (@proto_pool) and they are automatically freed when the protocol is removed.
120 3c6269b8 Martin Mares
 */
121 f4a60a9b Ondrej Zajicek (work)
122
struct channel *
123
proto_add_channel(struct proto *p, struct channel_config *cf)
124
{
125
  struct channel *c = mb_allocz(proto_pool, cf->channel->channel_size);
126
127
  c->name = cf->name;
128
  c->channel = cf->channel;
129
  c->proto = p;
130
  c->table = cf->table->table;
131
132
  c->in_filter = cf->in_filter;
133
  c->out_filter = cf->out_filter;
134
  c->rx_limit = cf->rx_limit;
135
  c->in_limit = cf->in_limit;
136
  c->out_limit = cf->out_limit;
137
138
  c->net_type = cf->net_type;
139
  c->ra_mode = cf->ra_mode;
140
  c->preference = cf->preference;
141
  c->merge_limit = cf->merge_limit;
142
  c->in_keep_filtered = cf->in_keep_filtered;
143
144
  c->channel_state = CS_DOWN;
145
  c->export_state = ES_DOWN;
146
  c->last_state_change = now;
147
  c->reloadable = 1;
148
149
  CALL(c->channel->init, c, cf);
150
151
  add_tail(&p->channels, &c->n);
152
153
  PD(p, "Channel %s connected to table %s", c->name, c->table->name);
154
155
  return c;
156
}
157
158
void
159
proto_remove_channel(struct proto *p, struct channel *c)
160
{
161
  ASSERT(c->channel_state == CS_DOWN);
162
163
  PD(p, "Channel %s removed", c->name);
164
165
  rem_node(&c->n);
166
  mb_free(c);
167
}
168
169
170
static void
171
proto_start_channels(struct proto *p)
172
{
173
  struct channel *c;
174
  WALK_LIST(c, p->channels)
175
    if (!c->disabled)
176
      channel_set_state(c, CS_UP);
177
}
178
179
static void
180
proto_pause_channels(struct proto *p)
181 0e02abfd Martin Mares
{
182 f4a60a9b Ondrej Zajicek (work)
  struct channel *c;
183
  WALK_LIST(c, p->channels)
184
    if (!c->disabled && channel_is_active(c))
185
      channel_set_state(c, CS_START);
186
}
187 0e02abfd Martin Mares
188 f4a60a9b Ondrej Zajicek (work)
static void
189
proto_stop_channels(struct proto *p)
190
{
191
  struct channel *c;
192
  WALK_LIST(c, p->channels)
193
    if (!c->disabled && channel_is_active(c))
194
      channel_set_state(c, CS_FLUSHING);
195
}
196 c0adf7e9 Ondrej Zajicek
197 f4a60a9b Ondrej Zajicek (work)
static void
198
proto_remove_channels(struct proto *p)
199
{
200
  struct channel *c;
201
  WALK_LIST_FIRST(c, p->channels)
202
    proto_remove_channel(p, c);
203
}
204
205
static void
206
channel_schedule_feed(struct channel *c, int initial)
207
{
208
  // DBG("%s: Scheduling meal\n", p->name);
209
  ASSERT(c->channel_state == CS_UP);
210 c0adf7e9 Ondrej Zajicek
211 f4a60a9b Ondrej Zajicek (work)
  c->export_state = ES_FEEDING;
212
  c->refeeding = !initial;
213 c0adf7e9 Ondrej Zajicek
214 f4a60a9b Ondrej Zajicek (work)
  ev_schedule(c->feed_event);
215
}
216
217
static void
218
channel_feed_loop(void *ptr)
219
{
220
  struct channel *c = ptr;
221
222
  if (c->export_state != ES_FEEDING)
223
    return;
224
225
  if (!c->feed_active)
226
    if (c->proto->feed_begin)
227
      c->proto->feed_begin(c, !c->refeeding);
228
229
  // DBG("Feeding protocol %s continued\n", p->name);
230
  if (!rt_feed_channel(c))
231
  {
232
    ev_schedule(c->feed_event);
233
    return;
234
  }
235
236
  // DBG("Feeding protocol %s finished\n", p->name);
237
  c->export_state = ES_READY;
238
  // proto_log_state_change(p);
239
240
  if (c->proto->feed_end)
241
    c->proto->feed_end(c);
242
}
243
244
245
static void
246
channel_start_export(struct channel *c)
247
{
248
  ASSERT(c->channel_state == CS_UP);
249
  ASSERT(c->export_state == ES_DOWN);
250
251
  channel_schedule_feed(c, 1);        /* Sets ES_FEEDING */
252
}
253
254
static void
255
channel_stop_export(struct channel *c)
256
{
257
  /* Need to abort feeding */
258
  if (c->export_state == ES_FEEDING)
259
    rt_feed_channel_abort(c);
260
261
  c->export_state = ES_DOWN;
262 7a7ac656 Jan Moskyto Matejka
  c->stats.exp_routes = 0;
263 f4a60a9b Ondrej Zajicek (work)
}
264
265
static void
266
channel_do_start(struct channel *c)
267
{
268
  rt_lock_table(c->table);
269
  add_tail(&c->table->channels, &c->table_node);
270
  c->proto->active_channels++;
271
272
  c->feed_event = ev_new(c->proto->pool);
273
  c->feed_event->data = c;
274
  c->feed_event->hook = channel_feed_loop;
275
276
  channel_reset_limit(&c->rx_limit);
277
  channel_reset_limit(&c->in_limit);
278
  channel_reset_limit(&c->out_limit);
279
280
  CALL(c->channel->start, c);
281
}
282
283
static void
284
channel_do_flush(struct channel *c)
285
{
286
  rt_schedule_prune(c->table);
287
288
  c->gr_wait = 0;
289
  if (c->gr_lock)
290
    channel_graceful_restart_unlock(c);
291
292
  CALL(c->channel->shutdown, c);
293
}
294
295
static void
296
channel_do_down(struct channel *c)
297
{
298 7a7ac656 Jan Moskyto Matejka
  rem_node(&c->table_node);
299 f4a60a9b Ondrej Zajicek (work)
  rt_unlock_table(c->table);
300
  c->proto->active_channels--;
301
302
  if ((c->stats.imp_routes + c->stats.filt_routes) != 0)
303
    log(L_ERR "%s: Channel %s is down but still has some routes", c->proto->name, c->name);
304
305
  memset(&c->stats, 0, sizeof(struct proto_stats));
306
307 d15b0b0a Ondrej Zajicek (work)
  CALL(c->channel->cleanup, c);
308
309 f4a60a9b Ondrej Zajicek (work)
  /* Schedule protocol shutddown */
310
  if (proto_is_done(c->proto))
311
    ev_schedule(c->proto->event);
312
}
313
314
void
315
channel_set_state(struct channel *c, uint state)
316
{
317
  uint cs = c->channel_state;
318
  uint es = c->export_state;
319
320 286e2011 Ondrej Zajicek (work)
  DBG("%s reporting channel %s state transition %s -> %s\n", c->proto->name, c->name, c_states[cs], c_states[state]);
321 f4a60a9b Ondrej Zajicek (work)
  if (state == cs)
322
    return;
323
324
  c->channel_state = state;
325
  c->last_state_change = now;
326
327
  switch (state)
328
  {
329
  case CS_START:
330
    ASSERT(cs == CS_DOWN || cs == CS_UP);
331
332
    if (cs == CS_DOWN)
333
      channel_do_start(c);
334
335
    if (es != ES_DOWN)
336
      channel_stop_export(c);
337
338
    break;
339
340
  case CS_UP:
341
    ASSERT(cs == CS_DOWN || cs == CS_START);
342
343
    if (cs == CS_DOWN)
344
      channel_do_start(c);
345
346 2a013bb3 Jan Moskyto Matejka
    if (!c->gr_wait && c->proto->rt_notify)
347 f4a60a9b Ondrej Zajicek (work)
      channel_start_export(c);
348
349
    break;
350
351
  case CS_FLUSHING:
352
    ASSERT(cs == CS_START || cs == CS_UP);
353
354
    if (es != ES_DOWN)
355
      channel_stop_export(c);
356
357
    channel_do_flush(c);
358
    break;
359
360
  case CS_DOWN:
361
    ASSERT(cs == CS_FLUSHING);
362
363
    channel_do_down(c);
364
    break;
365
366
  default:
367
    ASSERT(0);
368
  }
369
  // XXXX proto_log_state_change(c);
370 0e02abfd Martin Mares
}
371
372 c0adf7e9 Ondrej Zajicek
/**
373 f4a60a9b Ondrej Zajicek (work)
 * channel_request_feeding - request feeding routes to the channel
374
 * @c: given channel
375 c0adf7e9 Ondrej Zajicek
 *
376 f4a60a9b Ondrej Zajicek (work)
 * Sometimes it is needed to send again all routes to the channel. This is
377
 * called feeding and can be requested by this function. This would cause
378
 * channel export state transition to ES_FEEDING (during feeding) and when
379
 * completed, it will switch back to ES_READY. This function can be called
380
 * even when feeding is already running, in that case it is restarted.
381 c0adf7e9 Ondrej Zajicek
 */
382 f4a60a9b Ondrej Zajicek (work)
void
383
channel_request_feeding(struct channel *c)
384 c0adf7e9 Ondrej Zajicek
{
385 f4a60a9b Ondrej Zajicek (work)
  ASSERT(c->channel_state == CS_UP);
386 c0adf7e9 Ondrej Zajicek
387 f4a60a9b Ondrej Zajicek (work)
  /* Do nothing if we are still waiting for feeding */
388
  if (c->export_state == ES_DOWN)
389
    return;
390 c0adf7e9 Ondrej Zajicek
391 f4a60a9b Ondrej Zajicek (work)
  /* If we are already feeding, we want to restart it */
392
  if (c->export_state == ES_FEEDING)
393
  {
394
    /* Unless feeding is in initial state */
395
    if (!c->feed_active)
396
        return;
397
398
    rt_feed_channel_abort(c);
399
  }
400
401
  channel_reset_limit(&c->out_limit);
402
403
  /* Hack: reset exp_routes during refeed, and do not decrease it later */
404
  c->stats.exp_routes = 0;
405
406
  channel_schedule_feed(c, 0);        /* Sets ES_FEEDING */
407
  // proto_log_state_change(c);
408
}
409
410
static inline int
411
channel_reloadable(struct channel *c)
412
{
413
  return c->proto->reload_routes && c->reloadable;
414 c0adf7e9 Ondrej Zajicek
}
415
416 0e02abfd Martin Mares
static void
417 f4a60a9b Ondrej Zajicek (work)
channel_request_reload(struct channel *c)
418 0c791f87 Ondrej Zajicek
{
419 f4a60a9b Ondrej Zajicek (work)
  ASSERT(c->channel_state == CS_UP);
420
  // ASSERT(channel_reloadable(c));
421
422
  c->proto->reload_routes(c);
423 0c791f87 Ondrej Zajicek
424 f4a60a9b Ondrej Zajicek (work)
  /*
425
   * Should this be done before reload_routes() hook?
426
   * Perhaps, but routes are updated asynchronously.
427
   */
428
  channel_reset_limit(&c->rx_limit);
429
  channel_reset_limit(&c->in_limit);
430 0c791f87 Ondrej Zajicek
}
431
432 f4a60a9b Ondrej Zajicek (work)
const struct channel_class channel_basic = {
433
  .channel_size = sizeof(struct channel),
434
  .config_size = sizeof(struct channel_config)
435
};
436
437
void *
438
channel_config_new(const struct channel_class *cc, uint net_type, struct proto_config *proto)
439
{
440
  struct channel_config *cf = NULL;
441
  struct rtable_config *tab = NULL;
442
  const char *name = NULL;
443
444
  if (net_type)
445
  {
446
    if (!net_val_match(net_type, proto->protocol->channel_mask))
447
      cf_error("Unsupported channel type");
448
449
    if (proto->net_type && (net_type != proto->net_type))
450
      cf_error("Different channel type");
451
452
    tab = new_config->def_tables[net_type];
453
    name = net_label[net_type];
454
  }
455
456
  if (!cc)
457
    cc = &channel_basic;
458
459
  cf = cfg_allocz(cc->config_size);
460
  cf->name = name;
461
  cf->channel = cc;
462
  cf->table = tab;
463
  cf->out_filter = FILTER_REJECT;
464
465
  cf->net_type = net_type;
466
  cf->ra_mode = RA_OPTIMAL;
467
  cf->preference = proto->protocol->preference;
468
469
  add_tail(&proto->channels, &cf->n);
470
471
  return cf;
472
}
473
474
struct channel_config *
475
channel_copy_config(struct channel_config *src, struct proto_config *proto)
476
{
477
  struct channel_config *dst = cfg_alloc(src->channel->config_size);
478
479
  memcpy(dst, src, src->channel->config_size);
480
  add_tail(&proto->channels, &dst->n);
481
  CALL(src->channel->copy_config, dst, src);
482
483
  return dst;
484
}
485
486
487
static int reconfigure_type;  /* Hack to propagate type info to channel_reconfigure() */
488
489
int
490
channel_reconfigure(struct channel *c, struct channel_config *cf)
491
{
492
  /* FIXME: better handle these changes, also handle in_keep_filtered */
493
  if ((c->table != cf->table->table) || (c->ra_mode != cf->ra_mode))
494
    return 0;
495
496
  int import_changed = !filter_same(c->in_filter, cf->in_filter);
497
  int export_changed = !filter_same(c->out_filter, cf->out_filter);
498
499
  if (c->preference != cf->preference)
500
    import_changed = 1;
501
502
  if (c->merge_limit != cf->merge_limit)
503
    export_changed = 1;
504
505
  /* Reconfigure channel fields */
506
  c->in_filter = cf->in_filter;
507
  c->out_filter = cf->out_filter;
508
  c->rx_limit = cf->rx_limit;
509
  c->in_limit = cf->in_limit;
510
  c->out_limit = cf->out_limit;
511
512
  // c->ra_mode = cf->ra_mode;
513
  c->merge_limit = cf->merge_limit;
514
  c->preference = cf->preference;
515
  c->in_keep_filtered = cf->in_keep_filtered;
516
517
  channel_verify_limits(c);
518
519 d15b0b0a Ondrej Zajicek (work)
  /* Execute channel-specific reconfigure hook */
520
  if (c->channel->reconfigure && !c->channel->reconfigure(c, cf))
521
    return 0;
522 f4a60a9b Ondrej Zajicek (work)
523
  /* If the channel is not open, it has no routes and we cannot reload it anyways */
524
  if (c->channel_state != CS_UP)
525
    return 1;
526
527
  if (reconfigure_type == RECONFIG_SOFT)
528
  {
529
    if (import_changed)
530
      log(L_INFO "Channel %s.%s changed import", c->proto->name, c->name);
531
532
    if (export_changed)
533
      log(L_INFO "Channel %s.%s changed export", c->proto->name, c->name);
534
535
    return 1;
536
  }
537
538
  /* Route reload may be not supported */
539
  if (import_changed && !channel_reloadable(c))
540
    return 0;
541
542
  if (import_changed || export_changed)
543
    log(L_INFO "Reloading channel %s.%s", c->proto->name, c->name);
544
545
  if (import_changed)
546
    channel_request_reload(c);
547
548
  if (export_changed)
549
    channel_request_feeding(c);
550
551
  return 1;
552
}
553
554
555
int
556
proto_configure_channel(struct proto *p, struct channel **pc, struct channel_config *cf)
557 0e02abfd Martin Mares
{
558 f4a60a9b Ondrej Zajicek (work)
  struct channel *c = *pc;
559 0e02abfd Martin Mares
560 f4a60a9b Ondrej Zajicek (work)
  if (!c && cf)
561
  {
562
    *pc = proto_add_channel(p, cf);
563
  }
564
  else if (c && !cf)
565
  {
566
    if (c->channel_state != CS_DOWN)
567
    {
568
      log(L_INFO "Cannot remove channel %s.%s", c->proto->name, c->name);
569
      return 0;
570
    }
571
572
    proto_remove_channel(p, c);
573
    *pc = NULL;
574
  }
575
  else if (c && cf)
576
  {
577
    if (!channel_reconfigure(c, cf))
578
    {
579
      log(L_INFO "Cannot reconfigure channel %s.%s", c->proto->name, c->name);
580
      return 0;
581
    }
582
  }
583
584
  return 1;
585 c0adf7e9 Ondrej Zajicek
}
586
587 f4a60a9b Ondrej Zajicek (work)
588 c0adf7e9 Ondrej Zajicek
static void
589 f4a60a9b Ondrej Zajicek (work)
proto_event(void *ptr)
590 c0adf7e9 Ondrej Zajicek
{
591 f4a60a9b Ondrej Zajicek (work)
  struct proto *p = ptr;
592
593
  if (p->do_start)
594
  {
595
    if_feed_baby(p);
596
    p->do_start = 0;
597
  }
598 c0adf7e9 Ondrej Zajicek
599 f4a60a9b Ondrej Zajicek (work)
  if (p->do_stop)
600 c0adf7e9 Ondrej Zajicek
  {
601 f4a60a9b Ondrej Zajicek (work)
    if (p->proto == &proto_unix_iface)
602
      if_flush_ifaces(p);
603
    p->do_stop = 0;
604 c0adf7e9 Ondrej Zajicek
  }
605
606 f4a60a9b Ondrej Zajicek (work)
  if (proto_is_done(p))
607
  {
608
    if (p->proto->cleanup)
609
      p->proto->cleanup(p);
610
611
    p->active = 0;
612
    proto_log_state_change(p);
613
    proto_rethink_goal(p);
614
  }
615
}
616
617
618
/**
619
 * proto_new - create a new protocol instance
620
 * @c: protocol configuration
621
 *
622
 * When a new configuration has been read in, the core code starts
623
 * initializing all the protocol instances configured by calling their
624
 * init() hooks with the corresponding instance configuration. The initialization
625
 * code of the protocol is expected to create a new instance according to the
626
 * configuration by calling this function and then modifying the default settings
627
 * to values wanted by the protocol.
628
 */
629
void *
630
proto_new(struct proto_config *cf)
631
{
632
  struct proto *p = mb_allocz(proto_pool, cf->protocol->proto_size);
633
634
  p->cf = cf;
635
  p->debug = cf->debug;
636
  p->mrtdump = cf->mrtdump;
637
  p->name = cf->name;
638
  p->proto = cf->protocol;
639
  p->net_type = cf->net_type;
640
  p->disabled = cf->disabled;
641
  p->hash_key = random_u32();
642
  cf->proto = p;
643
644
  init_list(&p->channels);
645
646
  return p;
647
}
648
649
static struct proto *
650
proto_init(struct proto_config *c, node *n)
651
{
652
  struct protocol *pr = c->protocol;
653
  struct proto *p = pr->init(c);
654
655
  p->proto_state = PS_DOWN;
656
  p->last_state_change = now;
657
  insert_node(&p->n, n);
658
659
  p->event = ev_new(proto_pool);
660
  p->event->hook = proto_event;
661
  p->event->data = p;
662
663
  PD(p, "Initializing%s", p->disabled ? " [disabled]" : "");
664
665
  return p;
666
}
667
668
static void
669
proto_start(struct proto *p)
670
{
671
  /* Here we cannot use p->cf->name since it won't survive reconfiguration */
672
  p->pool = rp_new(proto_pool, p->proto->name);
673
674
  if (graceful_restart_state == GRS_INIT)
675
    p->gr_recovery = 1;
676 0e02abfd Martin Mares
}
677
678 094d2bdb Ondrej Zajicek
679 3c6269b8 Martin Mares
/**
680
 * proto_config_new - create a new protocol configuration
681
 * @pr: protocol the configuration will belong to
682 a7f23f58 Ondrej Zajicek
 * @class: SYM_PROTO or SYM_TEMPLATE
683 3c6269b8 Martin Mares
 *
684
 * Whenever the configuration file says that a new instance
685
 * of a routing protocol should be created, the parser calls
686
 * proto_config_new() to create a configuration entry for this
687
 * instance (a structure staring with the &proto_config header
688
 * containing all the generic items followed by protocol-specific
689
 * ones). Also, the configuration entry gets added to the list
690
 * of protocol instances kept in the configuration.
691 a7f23f58 Ondrej Zajicek
 *
692
 * The function is also used to create protocol templates (when class
693
 * SYM_TEMPLATE is specified), the only difference is that templates
694
 * are not added to the list of protocol instances and therefore not
695
 * initialized during protos_commit()).
696 3c6269b8 Martin Mares
 */
697 31b3e1bb Martin Mares
void *
698 2bbc3083 Ondrej Zajicek
proto_config_new(struct protocol *pr, int class)
699 31b3e1bb Martin Mares
{
700 f4a60a9b Ondrej Zajicek (work)
  struct proto_config *cf = cfg_allocz(pr->config_size);
701 31b3e1bb Martin Mares
702 a7f23f58 Ondrej Zajicek
  if (class == SYM_PROTO)
703 f4a60a9b Ondrej Zajicek (work)
    add_tail(&new_config->protos, &cf->n);
704
705
  cf->global = new_config;
706
  cf->protocol = pr;
707
  cf->name = pr->name;
708
  cf->class = class;
709
  cf->debug = new_config->proto_default_debug;
710
  cf->mrtdump = new_config->proto_default_mrtdump;
711
712
  init_list(&cf->channels);
713
714
  return cf;
715 31b3e1bb Martin Mares
}
716
717 f4a60a9b Ondrej Zajicek (work)
718 3c6269b8 Martin Mares
/**
719 a7f23f58 Ondrej Zajicek
 * proto_copy_config - copy a protocol configuration
720
 * @dest: destination protocol configuration
721
 * @src: source protocol configuration
722
 *
723
 * Whenever a new instance of a routing protocol is created from the
724
 * template, proto_copy_config() is called to copy a content of
725
 * the source protocol configuration to the new protocol configuration.
726
 * Name, class and a node in protos list of @dest are kept intact.
727
 * copy_config() protocol hook is used to copy protocol-specific data.
728
 */
729
void
730
proto_copy_config(struct proto_config *dest, struct proto_config *src)
731
{
732 f4a60a9b Ondrej Zajicek (work)
  struct channel_config *cc;
733 a7f23f58 Ondrej Zajicek
  node old_node;
734
  int old_class;
735
  char *old_name;
736
737
  if (dest->protocol != src->protocol)
738
    cf_error("Can't copy configuration from a different protocol type");
739
740
  if (dest->protocol->copy_config == NULL)
741
    cf_error("Inheriting configuration for %s is not supported", src->protocol->name);
742
743
  DBG("Copying configuration from %s to %s\n", src->name, dest->name);
744
745 f4a60a9b Ondrej Zajicek (work)
  /*
746 a7f23f58 Ondrej Zajicek
   * Copy struct proto_config here. Keep original node, class and name.
747
   * protocol-specific config copy is handled by protocol copy_config() hook
748
   */
749
750
  old_node = dest->n;
751
  old_class = dest->class;
752
  old_name = dest->name;
753
754 f4a60a9b Ondrej Zajicek (work)
  memcpy(dest, src, src->protocol->config_size);
755 a7f23f58 Ondrej Zajicek
756
  dest->n = old_node;
757
  dest->class = old_class;
758
  dest->name = old_name;
759 f4a60a9b Ondrej Zajicek (work)
  init_list(&dest->channels);
760 a7f23f58 Ondrej Zajicek
761 f4a60a9b Ondrej Zajicek (work)
  WALK_LIST(cc, src->channels)
762
    channel_copy_config(cc, dest);
763
764
  /* FIXME: allow for undefined copy_config */
765 a7f23f58 Ondrej Zajicek
  dest->protocol->copy_config(dest, src);
766
}
767
768
/**
769 3c6269b8 Martin Mares
 * protos_preconfig - pre-configuration processing
770
 * @c: new configuration
771
 *
772
 * This function calls the preconfig() hooks of all routing
773
 * protocols available to prepare them for reading of the new
774
 * configuration.
775
 */
776 2326b001 Martin Mares
void
777 31b3e1bb Martin Mares
protos_preconfig(struct config *c)
778 2326b001 Martin Mares
{
779 7f4a3988 Martin Mares
  struct protocol *p;
780
781 7c0cc76e Martin Mares
  init_list(&c->protos);
782 6b9fa320 Martin Mares
  DBG("Protocol preconfig:");
783 7f4a3988 Martin Mares
  WALK_LIST(p, protocol_list)
784 f4a60a9b Ondrej Zajicek (work)
  {
785
    DBG(" %s", p->name);
786
    p->name_counter = 0;
787
    if (p->preconfig)
788
      p->preconfig(p, c);
789
  }
790 6b9fa320 Martin Mares
  DBG("\n");
791 31b3e1bb Martin Mares
}
792
793 ebae4770 Ondrej Zajicek
static int
794
proto_reconfigure(struct proto *p, struct proto_config *oc, struct proto_config *nc, int type)
795
{
796
  /* If the protocol is DOWN, we just restart it */
797
  if (p->proto_state == PS_DOWN)
798
    return 0;
799
800
  /* If there is a too big change in core attributes, ... */
801
  if ((nc->protocol != oc->protocol) ||
802 f4a60a9b Ondrej Zajicek (work)
      (nc->net_type != oc->net_type) ||
803
      (nc->disabled != p->disabled))
804 ebae4770 Ondrej Zajicek
    return 0;
805
806 f4a60a9b Ondrej Zajicek (work)
  p->name = nc->name;
807 ebae4770 Ondrej Zajicek
  p->debug = nc->debug;
808
  p->mrtdump = nc->mrtdump;
809 f4a60a9b Ondrej Zajicek (work)
  reconfigure_type = type;
810 ebae4770 Ondrej Zajicek
811
  /* Execute protocol specific reconfigure hook */
812 f4a60a9b Ondrej Zajicek (work)
  if (!p->proto->reconfigure || !p->proto->reconfigure(p, nc))
813 ebae4770 Ondrej Zajicek
    return 0;
814
815
  DBG("\t%s: same\n", oc->name);
816
  PD(p, "Reconfigured");
817
  p->cf = nc;
818
819
  return 1;
820
}
821
822 3c6269b8 Martin Mares
/**
823
 * protos_commit - commit new protocol configuration
824
 * @new: new configuration
825
 * @old: old configuration or %NULL if it's boot time config
826
 * @force_reconfig: force restart of all protocols (used for example
827
 * when the router ID changes)
828 bf1aec97 Ondrej Zajicek
 * @type: type of reconfiguration (RECONFIG_SOFT or RECONFIG_HARD)
829 3c6269b8 Martin Mares
 *
830
 * Scan differences between @old and @new configuration and adjust all
831
 * protocol instances to conform to the new configuration.
832
 *
833
 * When a protocol exists in the new configuration, but it doesn't in the
834
 * original one, it's immediately started. When a collision with the other
835
 * running protocol would arise, the new protocol will be temporarily stopped
836
 * by the locking mechanism.
837
 *
838
 * When a protocol exists in the old configuration, but it doesn't in the
839
 * new one, it's shut down and deleted after the shutdown completes.
840
 *
841 bf1aec97 Ondrej Zajicek
 * When a protocol exists in both configurations, the core decides
842
 * whether it's possible to reconfigure it dynamically - it checks all
843
 * the core properties of the protocol (changes in filters are ignored
844
 * if type is RECONFIG_SOFT) and if they match, it asks the
845
 * reconfigure() hook of the protocol to see if the protocol is able
846
 * to switch to the new configuration.  If it isn't possible, the
847
 * protocol is shut down and a new instance is started with the new
848
 * configuration after the shutdown is completed.
849 3c6269b8 Martin Mares
 */
850 31b3e1bb Martin Mares
void
851 bf1aec97 Ondrej Zajicek
protos_commit(struct config *new, struct config *old, int force_reconfig, int type)
852 31b3e1bb Martin Mares
{
853 50fe90ed Martin Mares
  struct proto_config *oc, *nc;
854 a7f23f58 Ondrej Zajicek
  struct symbol *sym;
855 f4a60a9b Ondrej Zajicek (work)
  struct proto *p;
856
  node *n;
857
858 31b3e1bb Martin Mares
859 50fe90ed Martin Mares
  DBG("protos_commit:\n");
860
  if (old)
861 f4a60a9b Ondrej Zajicek (work)
  {
862
    WALK_LIST(oc, old->protos)
863 31b3e1bb Martin Mares
    {
864 f4a60a9b Ondrej Zajicek (work)
      p = oc->proto;
865
      sym = cf_find_symbol(new, oc->name);
866
      if (sym && sym->class == SYM_PROTO && !new->shutdown)
867
      {
868
        /* Found match, let's check if we can smoothly switch to new configuration */
869
        /* No need to check description */
870
        nc = sym->def;
871
        nc->proto = p;
872
873
        /* We will try to reconfigure protocol p */
874
        if (! force_reconfig && proto_reconfigure(p, oc, nc, type))
875
          continue;
876
877
        /* Unsuccessful, we will restart it */
878
        if (!p->disabled && !nc->disabled)
879
          log(L_INFO "Restarting protocol %s", p->name);
880
        else if (p->disabled && !nc->disabled)
881
          log(L_INFO "Enabling protocol %s", p->name);
882
        else if (!p->disabled && nc->disabled)
883
          log(L_INFO "Disabling protocol %s", p->name);
884
885
        p->down_code = nc->disabled ? PDC_CF_DISABLE : PDC_CF_RESTART;
886
        p->cf_new = nc;
887
      }
888
      else if (!new->shutdown)
889
      {
890
        log(L_INFO "Removing protocol %s", p->name);
891
        p->down_code = PDC_CF_REMOVE;
892
        p->cf_new = NULL;
893
      }
894
      else /* global shutdown */
895
      {
896
        p->down_code = PDC_CMD_SHUTDOWN;
897
        p->cf_new = NULL;
898
      }
899
900
      p->reconfiguring = 1;
901
      config_add_obstacle(old);
902
      proto_rethink_goal(p);
903 7f4a3988 Martin Mares
    }
904 f4a60a9b Ondrej Zajicek (work)
  }
905
906
  struct proto *first_dev_proto = NULL;
907 50fe90ed Martin Mares
908 f4a60a9b Ondrej Zajicek (work)
  n = NODE &(proto_list.head);
909 50fe90ed Martin Mares
  WALK_LIST(nc, new->protos)
910
    if (!nc->proto)
911 f4a60a9b Ondrej Zajicek (work)
    {
912
      /* Not a first-time configuration */
913
      if (old)
914
        log(L_INFO "Adding protocol %s", nc->name);
915
916
      p = proto_init(nc, n);
917
      n = NODE p;
918
919
      if (p->proto == &proto_unix_iface)
920
        first_dev_proto = p;
921
    }
922
    else
923
      n = NODE nc->proto;
924 50fe90ed Martin Mares
925
  DBG("Protocol start\n");
926 4ef09506 Ondrej Zajicek
927
  /* Start device protocol first */
928 f4a60a9b Ondrej Zajicek (work)
  if (first_dev_proto)
929
    proto_rethink_goal(first_dev_proto);
930 4ef09506 Ondrej Zajicek
931 79b4e12e Ondrej Zajicek
  /* Determine router ID for the first time - it has to be here and not in
932
     global_commit() because it is postponed after start of device protocol */
933
  if (!config->router_id)
934 f4a60a9b Ondrej Zajicek (work)
  {
935
    config->router_id = if_choose_router_id(config->router_id_from, 0);
936
    if (!config->router_id)
937
      die("Cannot determine router ID, please configure it manually");
938
  }
939 79b4e12e Ondrej Zajicek
940 f4a60a9b Ondrej Zajicek (work)
  /* Start all new protocols */
941
  WALK_LIST_DELSAFE(p, n, proto_list)
942 50fe90ed Martin Mares
    proto_rethink_goal(p);
943 7f4a3988 Martin Mares
}
944
945 47b79306 Martin Mares
static void
946 67bd949a Martin Mares
proto_rethink_goal(struct proto *p)
947 47b79306 Martin Mares
{
948 50fe90ed Martin Mares
  struct protocol *q;
949 0c791f87 Ondrej Zajicek
  byte goal;
950 50fe90ed Martin Mares
951 f4a60a9b Ondrej Zajicek (work)
  if (p->reconfiguring && !p->active)
952
  {
953
    struct proto_config *nc = p->cf_new;
954
    node *n = p->n.prev;
955
    DBG("%s has shut down for reconfiguration\n", p->name);
956
    p->cf->proto = NULL;
957
    config_del_obstacle(p->cf->global);
958
    proto_remove_channels(p);
959
    rem_node(&p->n);
960
    rfree(p->event);
961
    mb_free(p);
962
    if (!nc)
963
      return;
964
    p = proto_init(nc, n);
965
  }
966 50fe90ed Martin Mares
967
  /* Determine what state we want to reach */
968 bf8558bc Martin Mares
  if (p->disabled || p->reconfiguring)
969 0c791f87 Ondrej Zajicek
    goal = PS_DOWN;
970 50fe90ed Martin Mares
  else
971 0c791f87 Ondrej Zajicek
    goal = PS_UP;
972 50fe90ed Martin Mares
973
  q = p->proto;
974 f4a60a9b Ondrej Zajicek (work)
  if (goal == PS_UP)
975
  {
976
    if (!p->active)
977 67bd949a Martin Mares
    {
978 f4a60a9b Ondrej Zajicek (work)
      /* Going up */
979
      DBG("Kicking %s up\n", p->name);
980
      PD(p, "Starting");
981
      proto_start(p);
982
      proto_notify_state(p, (q->start ? q->start(p) : PS_UP));
983 67bd949a Martin Mares
    }
984 f4a60a9b Ondrej Zajicek (work)
  }
985
  else
986
  {
987
    if (p->proto_state == PS_START || p->proto_state == PS_UP)
988 67bd949a Martin Mares
    {
989 f4a60a9b Ondrej Zajicek (work)
      /* Going down */
990
      DBG("Kicking %s down\n", p->name);
991
      PD(p, "Shutting down");
992
      proto_notify_state(p, (q->shutdown ? q->shutdown(p) : PS_DOWN));
993 67bd949a Martin Mares
    }
994 f4a60a9b Ondrej Zajicek (work)
  }
995 67bd949a Martin Mares
}
996
997 0c791f87 Ondrej Zajicek
998 6eda3f13 Ondrej Zajicek
/**
999
 * DOC: Graceful restart recovery
1000
 *
1001
 * Graceful restart of a router is a process when the routing plane (e.g. BIRD)
1002
 * restarts but both the forwarding plane (e.g kernel routing table) and routing
1003
 * neighbors keep proper routes, and therefore uninterrupted packet forwarding
1004
 * is maintained.
1005
 *
1006
 * BIRD implements graceful restart recovery by deferring export of routes to
1007
 * protocols until routing tables are refilled with the expected content. After
1008
 * start, protocols generate routes as usual, but routes are not propagated to
1009
 * them, until protocols report that they generated all routes. After that,
1010
 * graceful restart recovery is finished and the export (and the initial feed)
1011
 * to protocols is enabled.
1012
 *
1013
 * When graceful restart recovery need is detected during initialization, then
1014
 * enabled protocols are marked with @gr_recovery flag before start. Such
1015
 * protocols then decide how to proceed with graceful restart, participation is
1016 f4a60a9b Ondrej Zajicek (work)
 * voluntary. Protocols could lock the recovery for each channel by function
1017 286e2011 Ondrej Zajicek (work)
 * channel_graceful_restart_lock() (state stored in @gr_lock flag), which means
1018 f4a60a9b Ondrej Zajicek (work)
 * that they want to postpone the end of the recovery until they converge and
1019
 * then unlock it. They also could set @gr_wait before advancing to %PS_UP,
1020
 * which means that the core should defer route export to that channel until
1021
 * the end of the recovery. This should be done by protocols that expect their
1022
 * neigbors to keep the proper routes (kernel table, BGP sessions with BGP
1023
 * graceful restart capability).
1024 6eda3f13 Ondrej Zajicek
 *
1025
 * The graceful restart recovery is finished when either all graceful restart
1026
 * locks are unlocked or when graceful restart wait timer fires.
1027
 *
1028
 */
1029 0c791f87 Ondrej Zajicek
1030 6eda3f13 Ondrej Zajicek
static void graceful_restart_done(struct timer *t);
1031 0c791f87 Ondrej Zajicek
1032 6eda3f13 Ondrej Zajicek
/**
1033
 * graceful_restart_recovery - request initial graceful restart recovery
1034
 *
1035
 * Called by the platform initialization code if the need for recovery
1036
 * after graceful restart is detected during boot. Have to be called
1037
 * before protos_commit().
1038
 */
1039 0c791f87 Ondrej Zajicek
void
1040
graceful_restart_recovery(void)
1041
{
1042
  graceful_restart_state = GRS_INIT;
1043
}
1044
1045 6eda3f13 Ondrej Zajicek
/**
1046
 * graceful_restart_init - initialize graceful restart
1047
 *
1048
 * When graceful restart recovery was requested, the function starts an active
1049
 * phase of the recovery and initializes graceful restart wait timer. The
1050
 * function have to be called after protos_commit().
1051
 */
1052 0c791f87 Ondrej Zajicek
void
1053
graceful_restart_init(void)
1054
{
1055
  if (!graceful_restart_state)
1056
    return;
1057
1058
  log(L_INFO "Graceful restart started");
1059
1060
  if (!graceful_restart_locks)
1061 f4a60a9b Ondrej Zajicek (work)
  {
1062
    graceful_restart_done(NULL);
1063
    return;
1064
  }
1065 0c791f87 Ondrej Zajicek
1066
  graceful_restart_state = GRS_ACTIVE;
1067
  gr_wait_timer = tm_new(proto_pool);
1068
  gr_wait_timer->hook = graceful_restart_done;
1069
  tm_start(gr_wait_timer, config->gr_wait);
1070
}
1071
1072 6eda3f13 Ondrej Zajicek
/**
1073
 * graceful_restart_done - finalize graceful restart
1074 8e433d6a Pavel Tvrdik
 * @t: unused
1075 6eda3f13 Ondrej Zajicek
 *
1076
 * When there are no locks on graceful restart, the functions finalizes the
1077
 * graceful restart recovery. Protocols postponing route export until the end of
1078
 * the recovery are awakened and the export to them is enabled. All other
1079
 * related state is cleared. The function is also called when the graceful
1080
 * restart wait timer fires (but there are still some locks).
1081
 */
1082 0c791f87 Ondrej Zajicek
static void
1083
graceful_restart_done(struct timer *t UNUSED)
1084
{
1085
  log(L_INFO "Graceful restart done");
1086
  graceful_restart_state = GRS_DONE;
1087
1088 f4a60a9b Ondrej Zajicek (work)
  struct proto *p;
1089
  WALK_LIST(p, proto_list)
1090
  {
1091
    if (!p->gr_recovery)
1092
      continue;
1093 0c791f87 Ondrej Zajicek
1094 f4a60a9b Ondrej Zajicek (work)
    struct channel *c;
1095
    WALK_LIST(c, p->channels)
1096
    {
1097 0c791f87 Ondrej Zajicek
      /* Resume postponed export of routes */
1098 2a013bb3 Jan Moskyto Matejka
      if ((c->channel_state == CS_UP) && c->gr_wait && c->proto->rt_notify)
1099 f4a60a9b Ondrej Zajicek (work)
        channel_start_export(c);
1100 0c791f87 Ondrej Zajicek
1101
      /* Cleanup */
1102 f4a60a9b Ondrej Zajicek (work)
      c->gr_wait = 0;
1103
      c->gr_lock = 0;
1104 0c791f87 Ondrej Zajicek
    }
1105
1106 f4a60a9b Ondrej Zajicek (work)
    p->gr_recovery = 0;
1107
  }
1108
1109 0c791f87 Ondrej Zajicek
  graceful_restart_locks = 0;
1110
}
1111
1112
void
1113
graceful_restart_show_status(void)
1114
{
1115
  if (graceful_restart_state != GRS_ACTIVE)
1116
    return;
1117
1118
  cli_msg(-24, "Graceful restart recovery in progress");
1119 f4a60a9b Ondrej Zajicek (work)
  cli_msg(-24, "  Waiting for %d channels to recover", graceful_restart_locks);
1120 0c791f87 Ondrej Zajicek
  cli_msg(-24, "  Wait timer is %d/%d", tm_remains(gr_wait_timer), config->gr_wait);
1121
}
1122
1123 6eda3f13 Ondrej Zajicek
/**
1124 f4a60a9b Ondrej Zajicek (work)
 * channel_graceful_restart_lock - lock graceful restart by channel
1125
 * @p: channel instance
1126 6eda3f13 Ondrej Zajicek
 *
1127
 * This function allows a protocol to postpone the end of graceful restart
1128
 * recovery until it converges. The lock is removed when the protocol calls
1129 f4a60a9b Ondrej Zajicek (work)
 * channel_graceful_restart_unlock() or when the channel is closed.
1130 6eda3f13 Ondrej Zajicek
 *
1131
 * The function have to be called during the initial phase of graceful restart
1132
 * recovery and only for protocols that are part of graceful restart (i.e. their
1133
 * @gr_recovery is set), which means it should be called from protocol start
1134
 * hooks.
1135
 */
1136 0c791f87 Ondrej Zajicek
void
1137 f4a60a9b Ondrej Zajicek (work)
channel_graceful_restart_lock(struct channel *c)
1138 0c791f87 Ondrej Zajicek
{
1139
  ASSERT(graceful_restart_state == GRS_INIT);
1140 f4a60a9b Ondrej Zajicek (work)
  ASSERT(c->proto->gr_recovery);
1141 0c791f87 Ondrej Zajicek
1142 f4a60a9b Ondrej Zajicek (work)
  if (c->gr_lock)
1143 0c791f87 Ondrej Zajicek
    return;
1144
1145 f4a60a9b Ondrej Zajicek (work)
  c->gr_lock = 1;
1146 0c791f87 Ondrej Zajicek
  graceful_restart_locks++;
1147
}
1148
1149 6eda3f13 Ondrej Zajicek
/**
1150 f4a60a9b Ondrej Zajicek (work)
 * channel_graceful_restart_unlock - unlock graceful restart by channel
1151
 * @p: channel instance
1152 6eda3f13 Ondrej Zajicek
 *
1153 f4a60a9b Ondrej Zajicek (work)
 * This function unlocks a lock from channel_graceful_restart_lock(). It is also
1154 6eda3f13 Ondrej Zajicek
 * automatically called when the lock holding protocol went down.
1155
 */
1156 0c791f87 Ondrej Zajicek
void
1157 f4a60a9b Ondrej Zajicek (work)
channel_graceful_restart_unlock(struct channel *c)
1158 0c791f87 Ondrej Zajicek
{
1159 f4a60a9b Ondrej Zajicek (work)
  if (!c->gr_lock)
1160 0c791f87 Ondrej Zajicek
    return;
1161
1162 f4a60a9b Ondrej Zajicek (work)
  c->gr_lock = 0;
1163 0c791f87 Ondrej Zajicek
  graceful_restart_locks--;
1164
1165
  if ((graceful_restart_state == GRS_ACTIVE) && !graceful_restart_locks)
1166
    tm_start(gr_wait_timer, 0);
1167
}
1168
1169
1170
1171 3c6269b8 Martin Mares
/**
1172
 * protos_dump_all - dump status of all protocols
1173
 *
1174
 * This function dumps status of all existing protocol instances to the
1175
 * debug output. It involves printing of general status information
1176
 * such as protocol states, its position on the protocol lists
1177
 * and also calling of a dump() hook of the protocol to print
1178
 * the internals.
1179
 */
1180 7f4a3988 Martin Mares
void
1181 87d2be86 Pavel Machek
protos_dump_all(void)
1182
{
1183
  debug("Protocols:\n");
1184
1185 f4a60a9b Ondrej Zajicek (work)
  struct proto *p;
1186
  WALK_LIST(p, proto_list)
1187
  {
1188
    debug("  protocol %s state %s\n", p->name, p_states[p->proto_state]);
1189
1190
    struct channel *c;
1191
    WALK_LIST(c, p->channels)
1192 87d2be86 Pavel Machek
    {
1193 f4a60a9b Ondrej Zajicek (work)
      debug("\tTABLE %s\n", c->table->name);
1194
      if (c->in_filter)
1195
        debug("\tInput filter: %s\n", filter_name(c->in_filter));
1196
      if (c->out_filter)
1197
        debug("\tOutput filter: %s\n", filter_name(c->out_filter));
1198 87d2be86 Pavel Machek
    }
1199 f4a60a9b Ondrej Zajicek (work)
1200
    if (p->proto->dump && (p->proto_state != PS_DOWN))
1201
      p->proto->dump(p);
1202
  }
1203 87d2be86 Pavel Machek
}
1204
1205 3c6269b8 Martin Mares
/**
1206
 * proto_build - make a single protocol available
1207
 * @p: the protocol
1208
 *
1209
 * After the platform specific initialization code uses protos_build()
1210
 * to add all the standard protocols, it should call proto_build() for
1211 2e9b2421 Martin Mares
 * all platform specific protocols to inform the core that they exist.
1212 3c6269b8 Martin Mares
 */
1213 87d2be86 Pavel Machek
void
1214 3991d84e Martin Mares
proto_build(struct protocol *p)
1215
{
1216
  add_tail(&protocol_list, &p->n);
1217
  if (p->attr_class)
1218
    {
1219
      ASSERT(!attr_class_to_protocol[p->attr_class]);
1220
      attr_class_to_protocol[p->attr_class] = p;
1221
    }
1222
}
1223
1224 1ec52253 Ondrej Zajicek
/* FIXME: convert this call to some protocol hook */
1225
extern void bfd_init_all(void);
1226
1227 3c6269b8 Martin Mares
/**
1228
 * protos_build - build a protocol list
1229
 *
1230
 * This function is called during BIRD startup to insert
1231
 * all standard protocols to the global protocol list. Insertion
1232
 * of platform specific protocols (such as the kernel syncer)
1233
 * is in the domain of competence of the platform dependent
1234
 * startup code.
1235
 */
1236 3991d84e Martin Mares
void
1237 0432c017 Martin Mares
protos_build(void)
1238
{
1239 471cc0be Martin Mares
  init_list(&proto_list);
1240 f4a60a9b Ondrej Zajicek (work)
  init_list(&protocol_list);
1241
1242 3991d84e Martin Mares
  proto_build(&proto_device);
1243 93e868c7 Ondrej Zajicek
#ifdef CONFIG_RADV
1244
  proto_build(&proto_radv);
1245
#endif
1246 18fff6a1 Martin Mares
#ifdef CONFIG_RIP
1247 3991d84e Martin Mares
  proto_build(&proto_rip);
1248 18fff6a1 Martin Mares
#endif
1249
#ifdef CONFIG_STATIC
1250 3991d84e Martin Mares
  proto_build(&proto_static);
1251 18fff6a1 Martin Mares
#endif
1252 c1f8dc91 Ondrej Filip
#ifdef CONFIG_OSPF
1253 3991d84e Martin Mares
  proto_build(&proto_ospf);
1254 c1f8dc91 Ondrej Filip
#endif
1255 26368f65 Martin Mares
#ifdef CONFIG_PIPE
1256 3991d84e Martin Mares
  proto_build(&proto_pipe);
1257 26368f65 Martin Mares
#endif
1258 2638249d Martin Mares
#ifdef CONFIG_BGP
1259 3991d84e Martin Mares
  proto_build(&proto_bgp);
1260 2638249d Martin Mares
#endif
1261 1ec52253 Ondrej Zajicek
#ifdef CONFIG_BFD
1262 6a8d3f1c Ondrej Zajicek
  proto_build(&proto_bfd);
1263 1ec52253 Ondrej Zajicek
  bfd_init_all();
1264
#endif
1265 937e75d8 Ondrej Zajicek (work)
#ifdef CONFIG_BABEL
1266
  proto_build(&proto_babel);
1267
#endif
1268 65d2a88d Pavel Tvrdík
#ifdef CONFIG_RPKI
1269
  proto_build(&proto_rpki);
1270
#endif
1271 6a8d3f1c Ondrej Zajicek
1272 67bd949a Martin Mares
  proto_pool = rp_new(&root_pool, "Protocols");
1273 ebecb6f6 Ondrej Zajicek
  proto_shutdown_timer = tm_new(proto_pool);
1274
  proto_shutdown_timer->hook = proto_shutdown_loop;
1275 67bd949a Martin Mares
}
1276
1277 fb829de6 Ondrej Zajicek
1278 d9b77cc2 Ondrej Zajicek
/* Temporary hack to propagate restart to BGP */
1279
int proto_restart;
1280 fb829de6 Ondrej Zajicek
1281 ebecb6f6 Ondrej Zajicek
static void
1282
proto_shutdown_loop(struct timer *t UNUSED)
1283
{
1284
  struct proto *p, *p_next;
1285
1286 f4a60a9b Ondrej Zajicek (work)
  WALK_LIST_DELSAFE(p, p_next, proto_list)
1287 ebecb6f6 Ondrej Zajicek
    if (p->down_sched)
1288 f4a60a9b Ondrej Zajicek (work)
    {
1289
      proto_restart = (p->down_sched == PDS_RESTART);
1290 ebecb6f6 Ondrej Zajicek
1291 f4a60a9b Ondrej Zajicek (work)
      p->disabled = 1;
1292
      proto_rethink_goal(p);
1293
      if (proto_restart)
1294
      {
1295
        p->disabled = 0;
1296 ebecb6f6 Ondrej Zajicek
        proto_rethink_goal(p);
1297
      }
1298 f4a60a9b Ondrej Zajicek (work)
    }
1299 ebecb6f6 Ondrej Zajicek
}
1300
1301
static inline void
1302
proto_schedule_down(struct proto *p, byte restart, byte code)
1303
{
1304
  /* Does not work for other states (even PS_START) */
1305
  ASSERT(p->proto_state == PS_UP);
1306
1307
  /* Scheduled restart may change to shutdown, but not otherwise */
1308
  if (p->down_sched == PDS_DISABLE)
1309
    return;
1310
1311
  p->down_sched = restart ? PDS_RESTART : PDS_DISABLE;
1312
  p->down_code = code;
1313
  tm_start_max(proto_shutdown_timer, restart ? 2 : 0);
1314
}
1315
1316
1317
static const char *
1318 f4a60a9b Ondrej Zajicek (work)
channel_limit_name(struct channel_limit *l)
1319 ebecb6f6 Ondrej Zajicek
{
1320
  const char *actions[] = {
1321
    [PLA_WARN] = "warn",
1322
    [PLA_BLOCK] = "block",
1323
    [PLA_RESTART] = "restart",
1324
    [PLA_DISABLE] = "disable",
1325
  };
1326
1327
  return actions[l->action];
1328
}
1329
1330
/**
1331 f4a60a9b Ondrej Zajicek (work)
 * channel_notify_limit: notify about limit hit and take appropriate action
1332
 * @c: channel
1333 ebecb6f6 Ondrej Zajicek
 * @l: limit being hit
1334 b662290f Ondrej Zajicek
 * @dir: limit direction (PLD_*)
1335 f4a60a9b Ondrej Zajicek (work)
 * @rt_count: the number of routes
1336 ebecb6f6 Ondrej Zajicek
 *
1337
 * The function is called by the route processing core when limit @l
1338
 * is breached. It activates the limit and tooks appropriate action
1339 7d0a31de Ondrej Zajicek
 * according to @l->action.
1340 ebecb6f6 Ondrej Zajicek
 */
1341 7d0a31de Ondrej Zajicek
void
1342 f4a60a9b Ondrej Zajicek (work)
channel_notify_limit(struct channel *c, struct channel_limit *l, int dir, u32 rt_count)
1343 ebecb6f6 Ondrej Zajicek
{
1344 b662290f Ondrej Zajicek
  const char *dir_name[PLD_MAX] = { "receive", "import" , "export" };
1345
  const byte dir_down[PLD_MAX] = { PDC_RX_LIMIT_HIT, PDC_IN_LIMIT_HIT, PDC_OUT_LIMIT_HIT };
1346 f4a60a9b Ondrej Zajicek (work)
  struct proto *p = c->proto;
1347 ebecb6f6 Ondrej Zajicek
1348 7d0a31de Ondrej Zajicek
  if (l->state == PLS_BLOCKED)
1349
    return;
1350 ebecb6f6 Ondrej Zajicek
1351 d9b77cc2 Ondrej Zajicek
  /* For warning action, we want the log message every time we hit the limit */
1352
  if (!l->state || ((l->action == PLA_WARN) && (rt_count == l->limit)))
1353 7d0a31de Ondrej Zajicek
    log(L_WARN "Protocol %s hits route %s limit (%d), action: %s",
1354 f4a60a9b Ondrej Zajicek (work)
        p->name, dir_name[dir], l->limit, channel_limit_name(l));
1355 ebecb6f6 Ondrej Zajicek
1356
  switch (l->action)
1357 f4a60a9b Ondrej Zajicek (work)
  {
1358
  case PLA_WARN:
1359
    l->state = PLS_ACTIVE;
1360
    break;
1361
1362
  case PLA_BLOCK:
1363
    l->state = PLS_BLOCKED;
1364
    break;
1365
1366
  case PLA_RESTART:
1367
  case PLA_DISABLE:
1368
    l->state = PLS_BLOCKED;
1369
    if (p->proto_state == PS_UP)
1370
      proto_schedule_down(p, l->action == PLA_RESTART, dir_down[dir]);
1371
    break;
1372
  }
1373 ebecb6f6 Ondrej Zajicek
}
1374
1375 f4a60a9b Ondrej Zajicek (work)
static void
1376
channel_verify_limits(struct channel *c)
1377 984d7349 Ondrej Zajicek
{
1378 f4a60a9b Ondrej Zajicek (work)
  struct channel_limit *l;
1379
  u32 all_routes = c->stats.imp_routes + c->stats.filt_routes;
1380 984d7349 Ondrej Zajicek
1381 f4a60a9b Ondrej Zajicek (work)
  l = &c->rx_limit;
1382
  if (l->action && (all_routes > l->limit))
1383
    channel_notify_limit(c, l, PLD_RX, all_routes);
1384 984d7349 Ondrej Zajicek
1385 f4a60a9b Ondrej Zajicek (work)
  l = &c->in_limit;
1386
  if (l->action && (c->stats.imp_routes > l->limit))
1387
    channel_notify_limit(c, l, PLD_IN, c->stats.imp_routes);
1388 984d7349 Ondrej Zajicek
1389 f4a60a9b Ondrej Zajicek (work)
  l = &c->out_limit;
1390
  if (l->action && (c->stats.exp_routes > l->limit))
1391
    channel_notify_limit(c, l, PLD_OUT, c->stats.exp_routes);
1392 984d7349 Ondrej Zajicek
}
1393
1394 f4a60a9b Ondrej Zajicek (work)
static inline void
1395
channel_reset_limit(struct channel_limit *l)
1396 0c791f87 Ondrej Zajicek
{
1397 f4a60a9b Ondrej Zajicek (work)
  if (l->action)
1398
    l->state = PLS_INITIAL;
1399 0c791f87 Ondrej Zajicek
}
1400
1401 f4a60a9b Ondrej Zajicek (work)
static inline void
1402
proto_do_start(struct proto *p)
1403 0c791f87 Ondrej Zajicek
{
1404 f4a60a9b Ondrej Zajicek (work)
  p->active = 1;
1405
  p->do_start = 1;
1406
  ev_schedule(p->event);
1407 0c791f87 Ondrej Zajicek
}
1408
1409
static void
1410 f4a60a9b Ondrej Zajicek (work)
proto_do_up(struct proto *p)
1411 0c791f87 Ondrej Zajicek
{
1412 f4a60a9b Ondrej Zajicek (work)
  if (!p->main_source)
1413
  {
1414
    p->main_source = rt_get_source(p, 0);
1415
    rt_lock_source(p->main_source);
1416
  }
1417 0c791f87 Ondrej Zajicek
1418 f4a60a9b Ondrej Zajicek (work)
  proto_start_channels(p);
1419 0c791f87 Ondrej Zajicek
}
1420
1421 f4a60a9b Ondrej Zajicek (work)
static inline void
1422
proto_do_pause(struct proto *p)
1423 0c791f87 Ondrej Zajicek
{
1424 f4a60a9b Ondrej Zajicek (work)
  proto_pause_channels(p);
1425 0c791f87 Ondrej Zajicek
}
1426
1427
static void
1428 f4a60a9b Ondrej Zajicek (work)
proto_do_stop(struct proto *p)
1429 0c791f87 Ondrej Zajicek
{
1430 f4a60a9b Ondrej Zajicek (work)
  p->down_sched = 0;
1431 0c791f87 Ondrej Zajicek
  p->gr_recovery = 0;
1432 6eda3f13 Ondrej Zajicek
1433 f4a60a9b Ondrej Zajicek (work)
  p->do_stop = 1;
1434
  ev_schedule(p->event);
1435 6eda3f13 Ondrej Zajicek
1436 f4a60a9b Ondrej Zajicek (work)
  if (p->main_source)
1437
  {
1438
    rt_unlock_source(p->main_source);
1439
    p->main_source = NULL;
1440
  }
1441 6eda3f13 Ondrej Zajicek
1442 f4a60a9b Ondrej Zajicek (work)
  proto_stop_channels(p);
1443
}
1444 6eda3f13 Ondrej Zajicek
1445 f4a60a9b Ondrej Zajicek (work)
static void
1446
proto_do_down(struct proto *p)
1447
{
1448
  p->down_code = 0;
1449
  neigh_prune();
1450
  rfree(p->pool);
1451
  p->pool = NULL;
1452
1453
  /* Shutdown is finished in the protocol event */
1454
  if (proto_is_done(p))
1455
    ev_schedule(p->event);
1456 6eda3f13 Ondrej Zajicek
}
1457
1458 0c791f87 Ondrej Zajicek
1459 f4a60a9b Ondrej Zajicek (work)
1460 bf47fe4b Ondrej Zajicek
/**
1461 3c6269b8 Martin Mares
 * proto_notify_state - notify core about protocol state change
1462
 * @p: protocol the state of which has changed
1463
 * @ps: the new status
1464
 *
1465
 * Whenever a state of a protocol changes due to some event internal
1466
 * to the protocol (i.e., not inside a start() or shutdown() hook),
1467
 * it should immediately notify the core about the change by calling
1468
 * proto_notify_state() which will write the new state to the &proto
1469
 * structure and take all the actions necessary to adapt to the new
1470 d6a836f8 Ondrej Zajicek
 * state. State change to PS_DOWN immediately frees resources of protocol
1471
 * and might execute start callback of protocol; therefore,
1472
 * it should be used at tail positions of protocol callbacks.
1473 3c6269b8 Martin Mares
 */
1474 67bd949a Martin Mares
void
1475 f4a60a9b Ondrej Zajicek (work)
proto_notify_state(struct proto *p, uint state)
1476 67bd949a Martin Mares
{
1477 f4a60a9b Ondrej Zajicek (work)
  uint ps = p->proto_state;
1478 67bd949a Martin Mares
1479 f4a60a9b Ondrej Zajicek (work)
  DBG("%s reporting state transition %s -> %s\n", p->name, p_states[ps], p_states[state]);
1480
  if (state == ps)
1481 67bd949a Martin Mares
    return;
1482
1483 f4a60a9b Ondrej Zajicek (work)
  p->proto_state = state;
1484 5ebc9293 Ondrej Zajicek
  p->last_state_change = now;
1485 d6a836f8 Ondrej Zajicek
1486 f4a60a9b Ondrej Zajicek (work)
  switch (state)
1487
  {
1488
  case PS_START:
1489
    ASSERT(ps == PS_DOWN || ps == PS_UP);
1490
1491
    if (ps == PS_DOWN)
1492
      proto_do_start(p);
1493
    else
1494
      proto_do_pause(p);
1495
    break;
1496
1497
  case PS_UP:
1498
    ASSERT(ps == PS_DOWN || ps == PS_START);
1499
1500
    if (ps == PS_DOWN)
1501
      proto_do_start(p);
1502
1503
    proto_do_up(p);
1504
    break;
1505
1506
  case PS_STOP:
1507
    ASSERT(ps == PS_START || ps == PS_UP);
1508
1509
    proto_do_stop(p);
1510
    break;
1511
1512
  case PS_DOWN:
1513
    if (ps != PS_STOP)
1514
      proto_do_stop(p);
1515
1516
    proto_do_down(p);
1517
    break;
1518
1519
  default:
1520
    bug("%s: Invalid state %d", p->name, ps);
1521
  }
1522 227af309 Ondrej Zajicek
1523
  proto_log_state_change(p);
1524 0432c017 Martin Mares
}
1525 1a54b1c6 Martin Mares
1526 0d3e6bce Martin Mares
/*
1527
 *  CLI Commands
1528
 */
1529
1530
static char *
1531
proto_state_name(struct proto *p)
1532
{
1533 f4a60a9b Ondrej Zajicek (work)
  switch (p->proto_state)
1534
  {
1535
  case PS_DOWN:                return p->active ? "flush" : "down";
1536
  case PS_START:        return "start";
1537
  case PS_UP:                return "up";
1538
  case PS_STOP:                return "stop";
1539
  default:                return "???";
1540
  }
1541 0d3e6bce Martin Mares
}
1542
1543
static void
1544 f4a60a9b Ondrej Zajicek (work)
channel_show_stats(struct channel *c)
1545 9db74169 Ondrej Zajicek
{
1546 f4a60a9b Ondrej Zajicek (work)
  struct proto_stats *s = &c->stats;
1547
1548
  if (c->in_keep_filtered)
1549
    cli_msg(-1006, "    Routes:         %u imported, %u filtered, %u exported",
1550
            s->imp_routes, s->filt_routes, s->exp_routes);
1551 cf98be7b Ondrej Zajicek
  else
1552 f4a60a9b Ondrej Zajicek (work)
    cli_msg(-1006, "    Routes:         %u imported, %u exported",
1553
            s->imp_routes, s->exp_routes);
1554 cf98be7b Ondrej Zajicek
1555 f4a60a9b Ondrej Zajicek (work)
  cli_msg(-1006, "    Route change stats:     received   rejected   filtered    ignored   accepted");
1556
  cli_msg(-1006, "      Import updates:     %10u %10u %10u %10u %10u",
1557 9db74169 Ondrej Zajicek
          s->imp_updates_received, s->imp_updates_invalid,
1558
          s->imp_updates_filtered, s->imp_updates_ignored,
1559
          s->imp_updates_accepted);
1560 f4a60a9b Ondrej Zajicek (work)
  cli_msg(-1006, "      Import withdraws:   %10u %10u        --- %10u %10u",
1561 9db74169 Ondrej Zajicek
          s->imp_withdraws_received, s->imp_withdraws_invalid,
1562
          s->imp_withdraws_ignored, s->imp_withdraws_accepted);
1563 f4a60a9b Ondrej Zajicek (work)
  cli_msg(-1006, "      Export updates:     %10u %10u %10u        --- %10u",
1564 9db74169 Ondrej Zajicek
          s->exp_updates_received, s->exp_updates_rejected,
1565
          s->exp_updates_filtered, s->exp_updates_accepted);
1566 f4a60a9b Ondrej Zajicek (work)
  cli_msg(-1006, "      Export withdraws:   %10u        ---        ---        --- %10u",
1567 9db74169 Ondrej Zajicek
          s->exp_withdraws_received, s->exp_withdraws_accepted);
1568
}
1569
1570 c0adf7e9 Ondrej Zajicek
void
1571 f4a60a9b Ondrej Zajicek (work)
channel_show_limit(struct channel_limit *l, const char *dsc)
1572 ebecb6f6 Ondrej Zajicek
{
1573 f4a60a9b Ondrej Zajicek (work)
  if (!l->action)
1574 7d0a31de Ondrej Zajicek
    return;
1575
1576 f4a60a9b Ondrej Zajicek (work)
  cli_msg(-1006, "    %-16s%d%s", dsc, l->limit, l->state ? " [HIT]" : "");
1577
  cli_msg(-1006, "      Action:       %s", channel_limit_name(l));
1578 ebecb6f6 Ondrej Zajicek
}
1579
1580
void
1581 f4a60a9b Ondrej Zajicek (work)
channel_show_info(struct channel *c)
1582 9db74169 Ondrej Zajicek
{
1583 f4a60a9b Ondrej Zajicek (work)
  cli_msg(-1006, "  Channel %s", c->name);
1584 d15b0b0a Ondrej Zajicek (work)
  cli_msg(-1006, "    State:          %s", c_states[c->channel_state]);
1585 f4a60a9b Ondrej Zajicek (work)
  cli_msg(-1006, "    Table:          %s", c->table->name);
1586
  cli_msg(-1006, "    Preference:     %d", c->preference);
1587
  cli_msg(-1006, "    Input filter:   %s", filter_name(c->in_filter));
1588
  cli_msg(-1006, "    Output filter:  %s", filter_name(c->out_filter));
1589 9db74169 Ondrej Zajicek
1590 0c791f87 Ondrej Zajicek
  if (graceful_restart_state == GRS_ACTIVE)
1591 f4a60a9b Ondrej Zajicek (work)
    cli_msg(-1006, "    GR recovery:   %s%s",
1592
            c->gr_lock ? " pending" : "",
1593
            c->gr_wait ? " waiting" : "");
1594 0c791f87 Ondrej Zajicek
1595 f4a60a9b Ondrej Zajicek (work)
  channel_show_limit(&c->rx_limit, "Receive limit:");
1596
  channel_show_limit(&c->in_limit, "Import limit:");
1597
  channel_show_limit(&c->out_limit, "Export limit:");
1598 ebecb6f6 Ondrej Zajicek
1599 f4a60a9b Ondrej Zajicek (work)
  if (c->channel_state != CS_DOWN)
1600
    channel_show_stats(c);
1601 9db74169 Ondrej Zajicek
}
1602
1603 e304fd4b Ondrej Zajicek
void
1604 ae80a2de Pavel Tvrdík
proto_cmd_show(struct proto *p, uint verbose, int cnt)
1605 1d2664a4 Martin Mares
{
1606 c37e7851 Ondrej Zajicek
  byte buf[256], tbuf[TM_DATETIME_BUFFER_SIZE];
1607 9685deb9 Martin Mares
1608 e304fd4b Ondrej Zajicek
  /* First protocol - show header */
1609
  if (!cnt)
1610
    cli_msg(-2002, "name     proto    table    state  since       info");
1611
1612 9685deb9 Martin Mares
  buf[0] = 0;
1613
  if (p->proto->get_status)
1614
    p->proto->get_status(p, buf);
1615 c37e7851 Ondrej Zajicek
  tm_format_datetime(tbuf, &config->tf_proto, p->last_state_change);
1616
  cli_msg(-1002, "%-8s %-8s %-8s %-5s  %-10s  %s",
1617 1d2664a4 Martin Mares
          p->name,
1618
          p->proto->name,
1619 f4a60a9b Ondrej Zajicek (work)
          p->main_channel ? p->main_channel->table->name : "---",
1620 1d2664a4 Martin Mares
          proto_state_name(p),
1621 c37e7851 Ondrej Zajicek
          tbuf,
1622 9685deb9 Martin Mares
          buf);
1623 f4a60a9b Ondrej Zajicek (work)
1624 1d2664a4 Martin Mares
  if (verbose)
1625 f4a60a9b Ondrej Zajicek (work)
  {
1626
    if (p->cf->dsc)
1627
      cli_msg(-1006, "  Description:    %s", p->cf->dsc);
1628
    if (p->cf->router_id)
1629
      cli_msg(-1006, "  Router ID:      %R", p->cf->router_id);
1630
1631
    if (p->proto->show_proto_info)
1632
      p->proto->show_proto_info(p);
1633
    else
1634 1d2664a4 Martin Mares
    {
1635 f4a60a9b Ondrej Zajicek (work)
      struct channel *c;
1636
      WALK_LIST(c, p->channels)
1637
        channel_show_info(c);
1638 1d2664a4 Martin Mares
    }
1639 f4a60a9b Ondrej Zajicek (work)
1640
    cli_msg(-1006, "");
1641
  }
1642 1d2664a4 Martin Mares
}
1643
1644 ae97b946 Martin Mares
void
1645 ae80a2de Pavel Tvrdík
proto_cmd_disable(struct proto *p, uint arg UNUSED, int cnt UNUSED)
1646 ae97b946 Martin Mares
{
1647 e304fd4b Ondrej Zajicek
  if (p->disabled)
1648 f4a60a9b Ondrej Zajicek (work)
  {
1649
    cli_msg(-8, "%s: already disabled", p->name);
1650
    return;
1651
  }
1652 e304fd4b Ondrej Zajicek
1653
  log(L_INFO "Disabling protocol %s", p->name);
1654
  p->disabled = 1;
1655 ebecb6f6 Ondrej Zajicek
  p->down_code = PDC_CMD_DISABLE;
1656 e304fd4b Ondrej Zajicek
  proto_rethink_goal(p);
1657
  cli_msg(-9, "%s: disabled", p->name);
1658
}
1659
1660
void
1661 ae80a2de Pavel Tvrdík
proto_cmd_enable(struct proto *p, uint arg UNUSED, int cnt UNUSED)
1662 e304fd4b Ondrej Zajicek
{
1663
  if (!p->disabled)
1664 f4a60a9b Ondrej Zajicek (work)
  {
1665
    cli_msg(-10, "%s: already enabled", p->name);
1666
    return;
1667
  }
1668 e304fd4b Ondrej Zajicek
1669
  log(L_INFO "Enabling protocol %s", p->name);
1670
  p->disabled = 0;
1671
  proto_rethink_goal(p);
1672
  cli_msg(-11, "%s: enabled", p->name);
1673
}
1674
1675
void
1676 ae80a2de Pavel Tvrdík
proto_cmd_restart(struct proto *p, uint arg UNUSED, int cnt UNUSED)
1677 e304fd4b Ondrej Zajicek
{
1678
  if (p->disabled)
1679 f4a60a9b Ondrej Zajicek (work)
  {
1680
    cli_msg(-8, "%s: already disabled", p->name);
1681
    return;
1682
  }
1683 e304fd4b Ondrej Zajicek
1684
  log(L_INFO "Restarting protocol %s", p->name);
1685
  p->disabled = 1;
1686 ebecb6f6 Ondrej Zajicek
  p->down_code = PDC_CMD_RESTART;
1687 e304fd4b Ondrej Zajicek
  proto_rethink_goal(p);
1688
  p->disabled = 0;
1689
  proto_rethink_goal(p);
1690
  cli_msg(-12, "%s: restarted", p->name);
1691
}
1692
1693
void
1694 ae80a2de Pavel Tvrdík
proto_cmd_reload(struct proto *p, uint dir, int cnt UNUSED)
1695 e304fd4b Ondrej Zajicek
{
1696 f4a60a9b Ondrej Zajicek (work)
  struct channel *c;
1697
1698 e304fd4b Ondrej Zajicek
  if (p->disabled)
1699 f4a60a9b Ondrej Zajicek (work)
  {
1700
    cli_msg(-8, "%s: already disabled", p->name);
1701
    return;
1702
  }
1703 e304fd4b Ondrej Zajicek
1704
  /* If the protocol in not UP, it has no routes */
1705
  if (p->proto_state != PS_UP)
1706
    return;
1707
1708 f4a60a9b Ondrej Zajicek (work)
  /* All channels must support reload */
1709
  if (dir != CMD_RELOAD_OUT)
1710
    WALK_LIST(c, p->channels)
1711
      if (!channel_reloadable(c))
1712
      {
1713
        cli_msg(-8006, "%s: reload failed", p->name);
1714
        return;
1715
      }
1716
1717 e304fd4b Ondrej Zajicek
  log(L_INFO "Reloading protocol %s", p->name);
1718
1719
  /* re-importing routes */
1720
  if (dir != CMD_RELOAD_OUT)
1721 f4a60a9b Ondrej Zajicek (work)
    WALK_LIST(c, p->channels)
1722
      channel_request_reload(c);
1723 ebecb6f6 Ondrej Zajicek
1724 e304fd4b Ondrej Zajicek
  /* re-exporting routes */
1725
  if (dir != CMD_RELOAD_IN)
1726 f4a60a9b Ondrej Zajicek (work)
    WALK_LIST(c, p->channels)
1727
      channel_request_feeding(c);
1728 e304fd4b Ondrej Zajicek
1729
  cli_msg(-15, "%s: reloading", p->name);
1730
}
1731
1732
void
1733 ae80a2de Pavel Tvrdík
proto_cmd_debug(struct proto *p, uint mask, int cnt UNUSED)
1734 e304fd4b Ondrej Zajicek
{
1735
  p->debug = mask;
1736
}
1737
1738
void
1739 ae80a2de Pavel Tvrdík
proto_cmd_mrtdump(struct proto *p, uint mask, int cnt UNUSED)
1740 e304fd4b Ondrej Zajicek
{
1741
  p->mrtdump = mask;
1742
}
1743
1744
static void
1745 ae80a2de Pavel Tvrdík
proto_apply_cmd_symbol(struct symbol *s, void (* cmd)(struct proto *, uint, int), uint arg)
1746 e304fd4b Ondrej Zajicek
{
1747
  if (s->class != SYM_PROTO)
1748 f4a60a9b Ondrej Zajicek (work)
  {
1749
    cli_msg(9002, "%s is not a protocol", s->name);
1750
    return;
1751
  }
1752 e304fd4b Ondrej Zajicek
1753
  cmd(((struct proto_config *)s->def)->proto, arg, 0);
1754 ae97b946 Martin Mares
  cli_msg(0, "");
1755
}
1756 02c1fbdd Martin Mares
1757 e304fd4b Ondrej Zajicek
static void
1758 ae80a2de Pavel Tvrdík
proto_apply_cmd_patt(char *patt, void (* cmd)(struct proto *, uint, int), uint arg)
1759 e304fd4b Ondrej Zajicek
{
1760 f4a60a9b Ondrej Zajicek (work)
  struct proto *p;
1761 e304fd4b Ondrej Zajicek
  int cnt = 0;
1762
1763 f4a60a9b Ondrej Zajicek (work)
  WALK_LIST(p, proto_list)
1764
    if (!patt || patmatch(patt, p->name))
1765
      cmd(p, arg, cnt++);
1766 e304fd4b Ondrej Zajicek
1767
  if (!cnt)
1768
    cli_msg(8003, "No protocols match");
1769
  else
1770
    cli_msg(0, "");
1771
}
1772
1773
void
1774 ae80a2de Pavel Tvrdík
proto_apply_cmd(struct proto_spec ps, void (* cmd)(struct proto *, uint, int),
1775
                int restricted, uint arg)
1776 e304fd4b Ondrej Zajicek
{
1777 e0a45fb4 Ondrej Zajicek
  if (restricted && cli_access_restricted())
1778
    return;
1779
1780 e304fd4b Ondrej Zajicek
  if (ps.patt)
1781
    proto_apply_cmd_patt(ps.ptr, cmd, arg);
1782
  else
1783
    proto_apply_cmd_symbol(ps.ptr, cmd, arg);
1784
}
1785
1786 02c1fbdd Martin Mares
struct proto *
1787
proto_get_named(struct symbol *sym, struct protocol *pr)
1788
{
1789
  struct proto *p, *q;
1790
1791
  if (sym)
1792 f4a60a9b Ondrej Zajicek (work)
  {
1793
    if (sym->class != SYM_PROTO)
1794
      cf_error("%s: Not a protocol", sym->name);
1795
1796
    p = ((struct proto_config *) sym->def)->proto;
1797
    if (!p || p->proto != pr)
1798
      cf_error("%s: Not a %s protocol", sym->name, pr->name);
1799
  }
1800 02c1fbdd Martin Mares
  else
1801 f4a60a9b Ondrej Zajicek (work)
  {
1802
    p = NULL;
1803
    WALK_LIST(q, proto_list)
1804
      if ((q->proto == pr) && (q->proto_state != PS_DOWN))
1805
      {
1806
        if (p)
1807
          cf_error("There are multiple %s protocols running", pr->name);
1808
        p = q;
1809
      }
1810
    if (!p)
1811
      cf_error("There is no %s protocol running", pr->name);
1812
  }
1813
1814 02c1fbdd Martin Mares
  return p;
1815
}