Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (43.5 KB)

1
/*
2
 *        BIRD -- Protocols
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
#undef LOCAL_DEBUG
10

    
11
#include "nest/bird.h"
12
#include "nest/protocol.h"
13
#include "lib/resource.h"
14
#include "lib/lists.h"
15
#include "lib/event.h"
16
#include "lib/string.h"
17
#include "conf/conf.h"
18
#include "nest/route.h"
19
#include "nest/iface.h"
20
#include "nest/cli.h"
21
#include "filter/filter.h"
22

    
23
pool *proto_pool;
24
list  proto_list;
25

    
26
static list protocol_list;
27

    
28
#define PD(pr, msg, args...) do { if (pr->debug & D_STATES) { log(L_TRACE "%s: " msg, pr->name , ## args); } } while(0)
29

    
30
static timer *proto_shutdown_timer;
31
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

    
41
static char *p_states[] = { "DOWN", "START", "UP", "STOP" };
42
static char *c_states[] = { "DOWN", "START", "UP", "FLUSHING" };
43

    
44
extern struct protocol proto_unix_iface;
45

    
46
static void proto_shutdown_loop(struct timer *);
47
static void proto_rethink_goal(struct proto *p);
48
static char *proto_state_name(struct proto *p);
49
static void channel_verify_limits(struct channel *c);
50
static void channel_reset_limit(struct channel_limit *l);
51

    
52

    
53
static inline int proto_is_done(struct proto *p)
54
{ return (p->proto_state == PS_DOWN) && (p->active_channels == 0); }
55

    
56
static inline int channel_is_active(struct channel *c)
57
{ return (c->channel_state == CS_START) || (c->channel_state == CS_UP); }
58

    
59
static void
60
proto_log_state_change(struct proto *p)
61
{
62
  if (p->debug & D_STATES)
63
  {
64
    char *name = proto_state_name(p);
65
    if (name != p->last_state_name_announced)
66
    {
67
      p->last_state_name_announced = name;
68
      PD(p, "State changed to %s", proto_state_name(p));
69
    }
70
  }
71
  else
72
    p->last_state_name_announced = NULL;
73
}
74

    
75

    
76
struct channel_config *
77
proto_cf_find_channel(struct proto_config *pc, uint net_type)
78
{
79
  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
}
87

    
88
/**
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
{
98
  struct channel *c;
99

    
100
  WALK_LIST(c, p->channels)
101
    if (c->table == t)
102
      return c;
103

    
104
  return NULL;
105
}
106

    
107
/**
108
 * proto_add_channel - connect protocol to a routing table
109
 * @p: protocol instance
110
 * @cf: channel configuration
111
 *
112
 * 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
 *
117
 * 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
 */
121

    
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
{
182
  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

    
188
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

    
197
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

    
211
  c->export_state = ES_FEEDING;
212
  c->refeeding = !initial;
213

    
214
  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
  c->stats.exp_routes = 0;
263
}
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
  rem_node(&c->table_node);
299
  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
  CALL(c->channel->cleanup, c);
308

    
309
  /* 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
  DBG("%s reporting channel %s state transition %s -> %s\n", c->proto->name, c->name, c_states[cs], c_states[state]);
321
  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
    if (!c->gr_wait && c->proto->rt_notify)
347
      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
}
371

    
372
/**
373
 * channel_request_feeding - request feeding routes to the channel
374
 * @c: given channel
375
 *
376
 * 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
 */
382
void
383
channel_request_feeding(struct channel *c)
384
{
385
  ASSERT(c->channel_state == CS_UP);
386

    
387
  /* Do nothing if we are still waiting for feeding */
388
  if (c->export_state == ES_DOWN)
389
    return;
390

    
391
  /* 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
}
415

    
416
static void
417
channel_request_reload(struct channel *c)
418
{
419
  ASSERT(c->channel_state == CS_UP);
420
  // ASSERT(channel_reloadable(c));
421

    
422
  c->proto->reload_routes(c);
423

    
424
  /*
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
}
431

    
432
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
  /* Execute channel-specific reconfigure hook */
520
  if (c->channel->reconfigure && !c->channel->reconfigure(c, cf))
521
    return 0;
522

    
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
{
558
  struct channel *c = *pc;
559

    
560
  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
}
586

    
587

    
588
static void
589
proto_event(void *ptr)
590
{
591
  struct proto *p = ptr;
592

    
593
  if (p->do_start)
594
  {
595
    if_feed_baby(p);
596
    p->do_start = 0;
597
  }
598

    
599
  if (p->do_stop)
600
  {
601
    if (p->proto == &proto_unix_iface)
602
      if_flush_ifaces(p);
603
    p->do_stop = 0;
604
  }
605

    
606
  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
}
677

    
678

    
679
/**
680
 * proto_config_new - create a new protocol configuration
681
 * @pr: protocol the configuration will belong to
682
 * @class: SYM_PROTO or SYM_TEMPLATE
683
 *
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
 *
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
 */
697
void *
698
proto_config_new(struct protocol *pr, int class)
699
{
700
  struct proto_config *cf = cfg_allocz(pr->config_size);
701

    
702
  if (class == SYM_PROTO)
703
    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
}
716

    
717

    
718
/**
719
 * 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
  struct channel_config *cc;
733
  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
  /*
746
   * 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
  memcpy(dest, src, src->protocol->config_size);
755

    
756
  dest->n = old_node;
757
  dest->class = old_class;
758
  dest->name = old_name;
759
  init_list(&dest->channels);
760

    
761
  WALK_LIST(cc, src->channels)
762
    channel_copy_config(cc, dest);
763

    
764
  /* FIXME: allow for undefined copy_config */
765
  dest->protocol->copy_config(dest, src);
766
}
767

    
768
/**
769
 * 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
void
777
protos_preconfig(struct config *c)
778
{
779
  struct protocol *p;
780

    
781
  init_list(&c->protos);
782
  DBG("Protocol preconfig:");
783
  WALK_LIST(p, protocol_list)
784
  {
785
    DBG(" %s", p->name);
786
    p->name_counter = 0;
787
    if (p->preconfig)
788
      p->preconfig(p, c);
789
  }
790
  DBG("\n");
791
}
792

    
793
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
      (nc->net_type != oc->net_type) ||
803
      (nc->disabled != p->disabled))
804
    return 0;
805

    
806
  p->name = nc->name;
807
  p->debug = nc->debug;
808
  p->mrtdump = nc->mrtdump;
809
  reconfigure_type = type;
810

    
811
  /* Execute protocol specific reconfigure hook */
812
  if (!p->proto->reconfigure || !p->proto->reconfigure(p, nc))
813
    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
/**
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
 * @type: type of reconfiguration (RECONFIG_SOFT or RECONFIG_HARD)
829
 *
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
 * 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
 */
850
void
851
protos_commit(struct config *new, struct config *old, int force_reconfig, int type)
852
{
853
  struct proto_config *oc, *nc;
854
  struct symbol *sym;
855
  struct proto *p;
856
  node *n;
857

    
858

    
859
  DBG("protos_commit:\n");
860
  if (old)
861
  {
862
    WALK_LIST(oc, old->protos)
863
    {
864
      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
    }
904
  }
905

    
906
  struct proto *first_dev_proto = NULL;
907

    
908
  n = NODE &(proto_list.head);
909
  WALK_LIST(nc, new->protos)
910
    if (!nc->proto)
911
    {
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

    
925
  DBG("Protocol start\n");
926

    
927
  /* Start device protocol first */
928
  if (first_dev_proto)
929
    proto_rethink_goal(first_dev_proto);
930

    
931
  /* 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
  {
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

    
940
  /* Start all new protocols */
941
  WALK_LIST_DELSAFE(p, n, proto_list)
942
    proto_rethink_goal(p);
943
}
944

    
945
static void
946
proto_rethink_goal(struct proto *p)
947
{
948
  struct protocol *q;
949
  byte goal;
950

    
951
  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

    
967
  /* Determine what state we want to reach */
968
  if (p->disabled || p->reconfiguring)
969
    goal = PS_DOWN;
970
  else
971
    goal = PS_UP;
972

    
973
  q = p->proto;
974
  if (goal == PS_UP)
975
  {
976
    if (!p->active)
977
    {
978
      /* 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
    }
984
  }
985
  else
986
  {
987
    if (p->proto_state == PS_START || p->proto_state == PS_UP)
988
    {
989
      /* 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
    }
994
  }
995
}
996

    
997

    
998
/**
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
 * voluntary. Protocols could lock the recovery for each channel by function
1017
 * channel_graceful_restart_lock() (state stored in @gr_lock flag), which means
1018
 * 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
 *
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

    
1030
static void graceful_restart_done(struct timer *t);
1031

    
1032
/**
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
void
1040
graceful_restart_recovery(void)
1041
{
1042
  graceful_restart_state = GRS_INIT;
1043
}
1044

    
1045
/**
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
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
  {
1062
    graceful_restart_done(NULL);
1063
    return;
1064
  }
1065

    
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
/**
1073
 * graceful_restart_done - finalize graceful restart
1074
 * @t: unused
1075
 *
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
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
  struct proto *p;
1089
  WALK_LIST(p, proto_list)
1090
  {
1091
    if (!p->gr_recovery)
1092
      continue;
1093

    
1094
    struct channel *c;
1095
    WALK_LIST(c, p->channels)
1096
    {
1097
      /* Resume postponed export of routes */
1098
      if ((c->channel_state == CS_UP) && c->gr_wait && c->proto->rt_notify)
1099
        channel_start_export(c);
1100

    
1101
      /* Cleanup */
1102
      c->gr_wait = 0;
1103
      c->gr_lock = 0;
1104
    }
1105

    
1106
    p->gr_recovery = 0;
1107
  }
1108

    
1109
  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
  cli_msg(-24, "  Waiting for %d channels to recover", graceful_restart_locks);
1120
  cli_msg(-24, "  Wait timer is %d/%d", tm_remains(gr_wait_timer), config->gr_wait);
1121
}
1122

    
1123
/**
1124
 * channel_graceful_restart_lock - lock graceful restart by channel
1125
 * @p: channel instance
1126
 *
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
 * channel_graceful_restart_unlock() or when the channel is closed.
1130
 *
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
void
1137
channel_graceful_restart_lock(struct channel *c)
1138
{
1139
  ASSERT(graceful_restart_state == GRS_INIT);
1140
  ASSERT(c->proto->gr_recovery);
1141

    
1142
  if (c->gr_lock)
1143
    return;
1144

    
1145
  c->gr_lock = 1;
1146
  graceful_restart_locks++;
1147
}
1148

    
1149
/**
1150
 * channel_graceful_restart_unlock - unlock graceful restart by channel
1151
 * @p: channel instance
1152
 *
1153
 * This function unlocks a lock from channel_graceful_restart_lock(). It is also
1154
 * automatically called when the lock holding protocol went down.
1155
 */
1156
void
1157
channel_graceful_restart_unlock(struct channel *c)
1158
{
1159
  if (!c->gr_lock)
1160
    return;
1161

    
1162
  c->gr_lock = 0;
1163
  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
/**
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
void
1181
protos_dump_all(void)
1182
{
1183
  debug("Protocols:\n");
1184

    
1185
  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
    {
1193
      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
    }
1199

    
1200
    if (p->proto->dump && (p->proto_state != PS_DOWN))
1201
      p->proto->dump(p);
1202
  }
1203
}
1204

    
1205
/**
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
 * all platform specific protocols to inform the core that they exist.
1212
 */
1213
void
1214
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
/* FIXME: convert this call to some protocol hook */
1225
extern void bfd_init_all(void);
1226

    
1227
/**
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
void
1237
protos_build(void)
1238
{
1239
  init_list(&proto_list);
1240
  init_list(&protocol_list);
1241

    
1242
  proto_build(&proto_device);
1243
#ifdef CONFIG_RADV
1244
  proto_build(&proto_radv);
1245
#endif
1246
#ifdef CONFIG_RIP
1247
  proto_build(&proto_rip);
1248
#endif
1249
#ifdef CONFIG_STATIC
1250
  proto_build(&proto_static);
1251
#endif
1252
#ifdef CONFIG_OSPF
1253
  proto_build(&proto_ospf);
1254
#endif
1255
#ifdef CONFIG_PIPE
1256
  proto_build(&proto_pipe);
1257
#endif
1258
#ifdef CONFIG_BGP
1259
  proto_build(&proto_bgp);
1260
#endif
1261
#ifdef CONFIG_BFD
1262
  proto_build(&proto_bfd);
1263
  bfd_init_all();
1264
#endif
1265
#ifdef CONFIG_BABEL
1266
  proto_build(&proto_babel);
1267
#endif
1268
#ifdef CONFIG_RPKI
1269
  proto_build(&proto_rpki);
1270
#endif
1271

    
1272
  proto_pool = rp_new(&root_pool, "Protocols");
1273
  proto_shutdown_timer = tm_new(proto_pool);
1274
  proto_shutdown_timer->hook = proto_shutdown_loop;
1275
}
1276

    
1277

    
1278
/* Temporary hack to propagate restart to BGP */
1279
int proto_restart;
1280

    
1281
static void
1282
proto_shutdown_loop(struct timer *t UNUSED)
1283
{
1284
  struct proto *p, *p_next;
1285

    
1286
  WALK_LIST_DELSAFE(p, p_next, proto_list)
1287
    if (p->down_sched)
1288
    {
1289
      proto_restart = (p->down_sched == PDS_RESTART);
1290

    
1291
      p->disabled = 1;
1292
      proto_rethink_goal(p);
1293
      if (proto_restart)
1294
      {
1295
        p->disabled = 0;
1296
        proto_rethink_goal(p);
1297
      }
1298
    }
1299
}
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
channel_limit_name(struct channel_limit *l)
1319
{
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
 * channel_notify_limit: notify about limit hit and take appropriate action
1332
 * @c: channel
1333
 * @l: limit being hit
1334
 * @dir: limit direction (PLD_*)
1335
 * @rt_count: the number of routes
1336
 *
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
 * according to @l->action.
1340
 */
1341
void
1342
channel_notify_limit(struct channel *c, struct channel_limit *l, int dir, u32 rt_count)
1343
{
1344
  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
  struct proto *p = c->proto;
1347

    
1348
  if (l->state == PLS_BLOCKED)
1349
    return;
1350

    
1351
  /* 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
    log(L_WARN "Protocol %s hits route %s limit (%d), action: %s",
1354
        p->name, dir_name[dir], l->limit, channel_limit_name(l));
1355

    
1356
  switch (l->action)
1357
  {
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
}
1374

    
1375
static void
1376
channel_verify_limits(struct channel *c)
1377
{
1378
  struct channel_limit *l;
1379
  u32 all_routes = c->stats.imp_routes + c->stats.filt_routes;
1380

    
1381
  l = &c->rx_limit;
1382
  if (l->action && (all_routes > l->limit))
1383
    channel_notify_limit(c, l, PLD_RX, all_routes);
1384

    
1385
  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

    
1389
  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
}
1393

    
1394
static inline void
1395
channel_reset_limit(struct channel_limit *l)
1396
{
1397
  if (l->action)
1398
    l->state = PLS_INITIAL;
1399
}
1400

    
1401
static inline void
1402
proto_do_start(struct proto *p)
1403
{
1404
  p->active = 1;
1405
  p->do_start = 1;
1406
  ev_schedule(p->event);
1407
}
1408

    
1409
static void
1410
proto_do_up(struct proto *p)
1411
{
1412
  if (!p->main_source)
1413
  {
1414
    p->main_source = rt_get_source(p, 0);
1415
    rt_lock_source(p->main_source);
1416
  }
1417

    
1418
  proto_start_channels(p);
1419
}
1420

    
1421
static inline void
1422
proto_do_pause(struct proto *p)
1423
{
1424
  proto_pause_channels(p);
1425
}
1426

    
1427
static void
1428
proto_do_stop(struct proto *p)
1429
{
1430
  p->down_sched = 0;
1431
  p->gr_recovery = 0;
1432

    
1433
  p->do_stop = 1;
1434
  ev_schedule(p->event);
1435

    
1436
  if (p->main_source)
1437
  {
1438
    rt_unlock_source(p->main_source);
1439
    p->main_source = NULL;
1440
  }
1441

    
1442
  proto_stop_channels(p);
1443
}
1444

    
1445
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
}
1457

    
1458

    
1459

    
1460
/**
1461
 * 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
 * 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
 */
1474
void
1475
proto_notify_state(struct proto *p, uint state)
1476
{
1477
  uint ps = p->proto_state;
1478

    
1479
  DBG("%s reporting state transition %s -> %s\n", p->name, p_states[ps], p_states[state]);
1480
  if (state == ps)
1481
    return;
1482

    
1483
  p->proto_state = state;
1484
  p->last_state_change = now;
1485

    
1486
  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

    
1523
  proto_log_state_change(p);
1524
}
1525

    
1526
/*
1527
 *  CLI Commands
1528
 */
1529

    
1530
static char *
1531
proto_state_name(struct proto *p)
1532
{
1533
  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
}
1542

    
1543
static void
1544
channel_show_stats(struct channel *c)
1545
{
1546
  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
  else
1552
    cli_msg(-1006, "    Routes:         %u imported, %u exported",
1553
            s->imp_routes, s->exp_routes);
1554

    
1555
  cli_msg(-1006, "    Route change stats:     received   rejected   filtered    ignored   accepted");
1556
  cli_msg(-1006, "      Import updates:     %10u %10u %10u %10u %10u",
1557
          s->imp_updates_received, s->imp_updates_invalid,
1558
          s->imp_updates_filtered, s->imp_updates_ignored,
1559
          s->imp_updates_accepted);
1560
  cli_msg(-1006, "      Import withdraws:   %10u %10u        --- %10u %10u",
1561
          s->imp_withdraws_received, s->imp_withdraws_invalid,
1562
          s->imp_withdraws_ignored, s->imp_withdraws_accepted);
1563
  cli_msg(-1006, "      Export updates:     %10u %10u %10u        --- %10u",
1564
          s->exp_updates_received, s->exp_updates_rejected,
1565
          s->exp_updates_filtered, s->exp_updates_accepted);
1566
  cli_msg(-1006, "      Export withdraws:   %10u        ---        ---        --- %10u",
1567
          s->exp_withdraws_received, s->exp_withdraws_accepted);
1568
}
1569

    
1570
void
1571
channel_show_limit(struct channel_limit *l, const char *dsc)
1572
{
1573
  if (!l->action)
1574
    return;
1575

    
1576
  cli_msg(-1006, "    %-16s%d%s", dsc, l->limit, l->state ? " [HIT]" : "");
1577
  cli_msg(-1006, "      Action:       %s", channel_limit_name(l));
1578
}
1579

    
1580
void
1581
channel_show_info(struct channel *c)
1582
{
1583
  cli_msg(-1006, "  Channel %s", c->name);
1584
  cli_msg(-1006, "    State:          %s", c_states[c->channel_state]);
1585
  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

    
1590
  if (graceful_restart_state == GRS_ACTIVE)
1591
    cli_msg(-1006, "    GR recovery:   %s%s",
1592
            c->gr_lock ? " pending" : "",
1593
            c->gr_wait ? " waiting" : "");
1594

    
1595
  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

    
1599
  if (c->channel_state != CS_DOWN)
1600
    channel_show_stats(c);
1601
}
1602

    
1603
void
1604
proto_cmd_show(struct proto *p, uint verbose, int cnt)
1605
{
1606
  byte buf[256], tbuf[TM_DATETIME_BUFFER_SIZE];
1607

    
1608
  /* First protocol - show header */
1609
  if (!cnt)
1610
    cli_msg(-2002, "name     proto    table    state  since       info");
1611

    
1612
  buf[0] = 0;
1613
  if (p->proto->get_status)
1614
    p->proto->get_status(p, buf);
1615
  tm_format_datetime(tbuf, &config->tf_proto, p->last_state_change);
1616
  cli_msg(-1002, "%-8s %-8s %-8s %-5s  %-10s  %s",
1617
          p->name,
1618
          p->proto->name,
1619
          p->main_channel ? p->main_channel->table->name : "---",
1620
          proto_state_name(p),
1621
          tbuf,
1622
          buf);
1623

    
1624
  if (verbose)
1625
  {
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
    {
1635
      struct channel *c;
1636
      WALK_LIST(c, p->channels)
1637
        channel_show_info(c);
1638
    }
1639

    
1640
    cli_msg(-1006, "");
1641
  }
1642
}
1643

    
1644
void
1645
proto_cmd_disable(struct proto *p, uint arg UNUSED, int cnt UNUSED)
1646
{
1647
  if (p->disabled)
1648
  {
1649
    cli_msg(-8, "%s: already disabled", p->name);
1650
    return;
1651
  }
1652

    
1653
  log(L_INFO "Disabling protocol %s", p->name);
1654
  p->disabled = 1;
1655
  p->down_code = PDC_CMD_DISABLE;
1656
  proto_rethink_goal(p);
1657
  cli_msg(-9, "%s: disabled", p->name);
1658
}
1659

    
1660
void
1661
proto_cmd_enable(struct proto *p, uint arg UNUSED, int cnt UNUSED)
1662
{
1663
  if (!p->disabled)
1664
  {
1665
    cli_msg(-10, "%s: already enabled", p->name);
1666
    return;
1667
  }
1668

    
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
proto_cmd_restart(struct proto *p, uint arg UNUSED, int cnt UNUSED)
1677
{
1678
  if (p->disabled)
1679
  {
1680
    cli_msg(-8, "%s: already disabled", p->name);
1681
    return;
1682
  }
1683

    
1684
  log(L_INFO "Restarting protocol %s", p->name);
1685
  p->disabled = 1;
1686
  p->down_code = PDC_CMD_RESTART;
1687
  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
proto_cmd_reload(struct proto *p, uint dir, int cnt UNUSED)
1695
{
1696
  struct channel *c;
1697

    
1698
  if (p->disabled)
1699
  {
1700
    cli_msg(-8, "%s: already disabled", p->name);
1701
    return;
1702
  }
1703

    
1704
  /* If the protocol in not UP, it has no routes */
1705
  if (p->proto_state != PS_UP)
1706
    return;
1707

    
1708
  /* 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
  log(L_INFO "Reloading protocol %s", p->name);
1718

    
1719
  /* re-importing routes */
1720
  if (dir != CMD_RELOAD_OUT)
1721
    WALK_LIST(c, p->channels)
1722
      channel_request_reload(c);
1723

    
1724
  /* re-exporting routes */
1725
  if (dir != CMD_RELOAD_IN)
1726
    WALK_LIST(c, p->channels)
1727
      channel_request_feeding(c);
1728

    
1729
  cli_msg(-15, "%s: reloading", p->name);
1730
}
1731

    
1732
void
1733
proto_cmd_debug(struct proto *p, uint mask, int cnt UNUSED)
1734
{
1735
  p->debug = mask;
1736
}
1737

    
1738
void
1739
proto_cmd_mrtdump(struct proto *p, uint mask, int cnt UNUSED)
1740
{
1741
  p->mrtdump = mask;
1742
}
1743

    
1744
static void
1745
proto_apply_cmd_symbol(struct symbol *s, void (* cmd)(struct proto *, uint, int), uint arg)
1746
{
1747
  if (s->class != SYM_PROTO)
1748
  {
1749
    cli_msg(9002, "%s is not a protocol", s->name);
1750
    return;
1751
  }
1752

    
1753
  cmd(((struct proto_config *)s->def)->proto, arg, 0);
1754
  cli_msg(0, "");
1755
}
1756

    
1757
static void
1758
proto_apply_cmd_patt(char *patt, void (* cmd)(struct proto *, uint, int), uint arg)
1759
{
1760
  struct proto *p;
1761
  int cnt = 0;
1762

    
1763
  WALK_LIST(p, proto_list)
1764
    if (!patt || patmatch(patt, p->name))
1765
      cmd(p, arg, cnt++);
1766

    
1767
  if (!cnt)
1768
    cli_msg(8003, "No protocols match");
1769
  else
1770
    cli_msg(0, "");
1771
}
1772

    
1773
void
1774
proto_apply_cmd(struct proto_spec ps, void (* cmd)(struct proto *, uint, int),
1775
                int restricted, uint arg)
1776
{
1777
  if (restricted && cli_access_restricted())
1778
    return;
1779

    
1780
  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
struct proto *
1787
proto_get_named(struct symbol *sym, struct protocol *pr)
1788
{
1789
  struct proto *p, *q;
1790

    
1791
  if (sym)
1792
  {
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
  else
1801
  {
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
  return p;
1815
}