Statistics
| Branch: | Revision:

iof-bird / bird-2.0.1 / nest / proto.c @ 6b3f1a54

History | View | Annotate | Download (45.7 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/timer.h"
17
#include "lib/string.h"
18
#include "conf/conf.h"
19
#include "nest/route.h"
20
#include "nest/iface.h"
21
#include "nest/cli.h"
22
#include "filter/filter.h"
23

    
24
pool *proto_pool;
25
list  proto_list;
26

    
27
static list protocol_list;
28

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

    
31
static timer *proto_shutdown_timer;
32
static timer *gr_wait_timer;
33

    
34
#define GRS_NONE        0
35
#define GRS_INIT        1
36
#define GRS_ACTIVE        2
37
#define GRS_DONE        3
38

    
39
static int graceful_restart_state;
40
static u32 graceful_restart_locks;
41

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

    
45
extern struct protocol proto_unix_iface;
46

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

    
53

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

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

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

    
76

    
77
struct channel_config *
78
proto_cf_find_channel(struct proto_config *pc, uint net_type)
79
{
80
  struct channel_config *cc;
81

    
82
  WALK_LIST(cc, pc->channels)
83
    if (cc->net_type == net_type)
84
      return cc;
85

    
86
  return NULL;
87
}
88

    
89
/**
90
 * proto_find_channel_by_table - find channel connected to a routing table
91
 * @p: protocol instance
92
 * @t: routing table
93
 *
94
 * Returns pointer to channel or NULL
95
 */
96
struct channel *
97
proto_find_channel_by_table(struct proto *p, struct rtable *t)
98
{
99
  struct channel *c;
100

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

    
105
  return NULL;
106
}
107

    
108
/**
109
 * proto_find_channel_by_name - find channel by its name
110
 * @p: protocol instance
111
 * @n: channel name
112
 *
113
 * Returns pointer to channel or NULL
114
 */
115
struct channel *
116
proto_find_channel_by_name(struct proto *p, const char *n)
117
{
118
  struct channel *c;
119

    
120
  WALK_LIST(c, p->channels)
121
    if (!strcmp(c->name, n))
122
      return c;
123

    
124
  return NULL;
125
}
126

    
127
/**
128
 * proto_add_channel - connect protocol to a routing table
129
 * @p: protocol instance
130
 * @cf: channel configuration
131
 *
132
 * This function creates a channel between the protocol instance @p and the
133
 * routing table specified in the configuration @cf, making the protocol hear
134
 * all changes in the table and allowing the protocol to update routes in the
135
 * table.
136
 *
137
 * The channel is linked in the protocol channel list and when active also in
138
 * the table channel list. Channels are allocated from the global resource pool
139
 * (@proto_pool) and they are automatically freed when the protocol is removed.
140
 */
141

    
142
struct channel *
143
proto_add_channel(struct proto *p, struct channel_config *cf)
144
{
145
  struct channel *c = mb_allocz(proto_pool, cf->channel->channel_size);
146

    
147
  c->name = cf->name;
148
  c->channel = cf->channel;
149
  c->proto = p;
150
  c->table = cf->table->table;
151

    
152
  c->in_filter = cf->in_filter;
153
  c->out_filter = cf->out_filter;
154
  c->rx_limit = cf->rx_limit;
155
  c->in_limit = cf->in_limit;
156
  c->out_limit = cf->out_limit;
157

    
158
  c->net_type = cf->net_type;
159
  c->ra_mode = cf->ra_mode;
160
  c->preference = cf->preference;
161
  c->merge_limit = cf->merge_limit;
162
  c->in_keep_filtered = cf->in_keep_filtered;
163

    
164
  c->channel_state = CS_DOWN;
165
  c->export_state = ES_DOWN;
166
  c->last_state_change = current_time();
167
  c->reloadable = 1;
168

    
169
  CALL(c->channel->init, c, cf);
170

    
171
  add_tail(&p->channels, &c->n);
172

    
173
  PD(p, "Channel %s connected to table %s", c->name, c->table->name);
174

    
175
  return c;
176
}
177

    
178
void
179
proto_remove_channel(struct proto *p, struct channel *c)
180
{
181
  ASSERT(c->channel_state == CS_DOWN);
182

    
183
  PD(p, "Channel %s removed", c->name);
184

    
185
  rem_node(&c->n);
186
  mb_free(c);
187
}
188

    
189

    
190
static void
191
proto_start_channels(struct proto *p)
192
{
193
  struct channel *c;
194
  WALK_LIST(c, p->channels)
195
    if (!c->disabled)
196
      channel_set_state(c, CS_UP);
197
}
198

    
199
static void
200
proto_pause_channels(struct proto *p)
201
{
202
  struct channel *c;
203
  WALK_LIST(c, p->channels)
204
    if (!c->disabled && channel_is_active(c))
205
      channel_set_state(c, CS_START);
206
}
207

    
208
static void
209
proto_stop_channels(struct proto *p)
210
{
211
  struct channel *c;
212
  WALK_LIST(c, p->channels)
213
    if (!c->disabled && channel_is_active(c))
214
      channel_set_state(c, CS_FLUSHING);
215
}
216

    
217
static void
218
proto_remove_channels(struct proto *p)
219
{
220
  struct channel *c;
221
  WALK_LIST_FIRST(c, p->channels)
222
    proto_remove_channel(p, c);
223
}
224

    
225
static void
226
channel_schedule_feed(struct channel *c, int initial)
227
{
228
  // DBG("%s: Scheduling meal\n", p->name);
229
  ASSERT(c->channel_state == CS_UP);
230

    
231
  c->export_state = ES_FEEDING;
232
  c->refeeding = !initial;
233

    
234
  ev_schedule(c->feed_event);
235
}
236

    
237
static void
238
channel_feed_loop(void *ptr)
239
{
240
  struct channel *c = ptr;
241

    
242
  if (c->export_state != ES_FEEDING)
243
    return;
244

    
245
  if (!c->feed_active)
246
    if (c->proto->feed_begin)
247
      c->proto->feed_begin(c, !c->refeeding);
248

    
249
  // DBG("Feeding protocol %s continued\n", p->name);
250
  if (!rt_feed_channel(c))
251
  {
252
    ev_schedule(c->feed_event);
253
    return;
254
  }
255

    
256
  // DBG("Feeding protocol %s finished\n", p->name);
257
  c->export_state = ES_READY;
258
  // proto_log_state_change(p);
259

    
260
  if (c->proto->feed_end)
261
    c->proto->feed_end(c);
262
}
263

    
264

    
265
static void
266
channel_start_export(struct channel *c)
267
{
268
  ASSERT(c->channel_state == CS_UP);
269
  ASSERT(c->export_state == ES_DOWN);
270

    
271
  channel_schedule_feed(c, 1);        /* Sets ES_FEEDING */
272
}
273

    
274
static void
275
channel_stop_export(struct channel *c)
276
{
277
  /* Need to abort feeding */
278
  if (c->export_state == ES_FEEDING)
279
    rt_feed_channel_abort(c);
280

    
281
  c->export_state = ES_DOWN;
282
  c->stats.exp_routes = 0;
283
}
284

    
285
static void
286
channel_do_start(struct channel *c)
287
{
288
  rt_lock_table(c->table);
289
  add_tail(&c->table->channels, &c->table_node);
290
  c->proto->active_channels++;
291

    
292
  c->feed_event = ev_new(c->proto->pool);
293
  c->feed_event->data = c;
294
  c->feed_event->hook = channel_feed_loop;
295

    
296
  channel_reset_limit(&c->rx_limit);
297
  channel_reset_limit(&c->in_limit);
298
  channel_reset_limit(&c->out_limit);
299

    
300
  CALL(c->channel->start, c);
301
}
302

    
303
static void
304
channel_do_flush(struct channel *c)
305
{
306
  rt_schedule_prune(c->table);
307

    
308
  c->gr_wait = 0;
309
  if (c->gr_lock)
310
    channel_graceful_restart_unlock(c);
311

    
312
  CALL(c->channel->shutdown, c);
313
}
314

    
315
static void
316
channel_do_down(struct channel *c)
317
{
318
  rem_node(&c->table_node);
319
  rt_unlock_table(c->table);
320
  c->proto->active_channels--;
321

    
322
  if ((c->stats.imp_routes + c->stats.filt_routes) != 0)
323
    log(L_ERR "%s: Channel %s is down but still has some routes", c->proto->name, c->name);
324

    
325
  memset(&c->stats, 0, sizeof(struct proto_stats));
326

    
327
  CALL(c->channel->cleanup, c);
328

    
329
  /* Schedule protocol shutddown */
330
  if (proto_is_done(c->proto))
331
    ev_schedule(c->proto->event);
332
}
333

    
334
void
335
channel_set_state(struct channel *c, uint state)
336
{
337
  uint cs = c->channel_state;
338
  uint es = c->export_state;
339

    
340
  DBG("%s reporting channel %s state transition %s -> %s\n", c->proto->name, c->name, c_states[cs], c_states[state]);
341
  if (state == cs)
342
    return;
343

    
344
  c->channel_state = state;
345
  c->last_state_change = current_time();
346

    
347
  switch (state)
348
  {
349
  case CS_START:
350
    ASSERT(cs == CS_DOWN || cs == CS_UP);
351

    
352
    if (cs == CS_DOWN)
353
      channel_do_start(c);
354

    
355
    if (es != ES_DOWN)
356
      channel_stop_export(c);
357

    
358
    break;
359

    
360
  case CS_UP:
361
    ASSERT(cs == CS_DOWN || cs == CS_START);
362

    
363
    if (cs == CS_DOWN)
364
      channel_do_start(c);
365

    
366
    if (!c->gr_wait && c->proto->rt_notify)
367
      channel_start_export(c);
368

    
369
    break;
370

    
371
  case CS_FLUSHING:
372
    ASSERT(cs == CS_START || cs == CS_UP);
373

    
374
    if (es != ES_DOWN)
375
      channel_stop_export(c);
376

    
377
    channel_do_flush(c);
378
    break;
379

    
380
  case CS_DOWN:
381
    ASSERT(cs == CS_FLUSHING);
382

    
383
    channel_do_down(c);
384
    break;
385

    
386
  default:
387
    ASSERT(0);
388
  }
389
  // XXXX proto_log_state_change(c);
390
}
391

    
392
/**
393
 * channel_request_feeding - request feeding routes to the channel
394
 * @c: given channel
395
 *
396
 * Sometimes it is needed to send again all routes to the channel. This is
397
 * called feeding and can be requested by this function. This would cause
398
 * channel export state transition to ES_FEEDING (during feeding) and when
399
 * completed, it will switch back to ES_READY. This function can be called
400
 * even when feeding is already running, in that case it is restarted.
401
 */
402
void
403
channel_request_feeding(struct channel *c)
404
{
405
  ASSERT(c->channel_state == CS_UP);
406

    
407
  /* Do nothing if we are still waiting for feeding */
408
  if (c->export_state == ES_DOWN)
409
    return;
410

    
411
  /* If we are already feeding, we want to restart it */
412
  if (c->export_state == ES_FEEDING)
413
  {
414
    /* Unless feeding is in initial state */
415
    if (!c->feed_active)
416
        return;
417

    
418
    rt_feed_channel_abort(c);
419
  }
420

    
421
  channel_reset_limit(&c->out_limit);
422

    
423
  /* Hack: reset exp_routes during refeed, and do not decrease it later */
424
  c->stats.exp_routes = 0;
425

    
426
  channel_schedule_feed(c, 0);        /* Sets ES_FEEDING */
427
  // proto_log_state_change(c);
428
}
429

    
430
static inline int
431
channel_reloadable(struct channel *c)
432
{
433
  return c->proto->reload_routes && c->reloadable;
434
}
435

    
436
static void
437
channel_request_reload(struct channel *c)
438
{
439
  ASSERT(c->channel_state == CS_UP);
440
  ASSERT(channel_reloadable(c));
441

    
442
  c->proto->reload_routes(c);
443

    
444
  /*
445
   * Should this be done before reload_routes() hook?
446
   * Perhaps, but routes are updated asynchronously.
447
   */
448
  channel_reset_limit(&c->rx_limit);
449
  channel_reset_limit(&c->in_limit);
450
}
451

    
452
const struct channel_class channel_basic = {
453
  .channel_size = sizeof(struct channel),
454
  .config_size = sizeof(struct channel_config)
455
};
456

    
457
void *
458
channel_config_new(const struct channel_class *cc, const char *name, uint net_type, struct proto_config *proto)
459
{
460
  struct channel_config *cf = NULL;
461
  struct rtable_config *tab = NULL;
462

    
463
  if (net_type)
464
  {
465
    if (!net_val_match(net_type, proto->protocol->channel_mask))
466
      cf_error("Unsupported channel type");
467

    
468
    if (proto->net_type && (net_type != proto->net_type))
469
      cf_error("Different channel type");
470

    
471
    tab = new_config->def_tables[net_type];
472
  }
473

    
474
  if (!cc)
475
    cc = &channel_basic;
476

    
477
  cf = cfg_allocz(cc->config_size);
478
  cf->name = name;
479
  cf->channel = cc;
480
  cf->parent = proto;
481
  cf->table = tab;
482
  cf->out_filter = FILTER_REJECT;
483

    
484
  cf->net_type = net_type;
485
  cf->ra_mode = RA_OPTIMAL;
486
  cf->preference = proto->protocol->preference;
487

    
488
  add_tail(&proto->channels, &cf->n);
489

    
490
  return cf;
491
}
492

    
493
void *
494
channel_config_get(const struct channel_class *cc, const char *name, uint net_type, struct proto_config *proto)
495
{
496
  struct channel_config *cf;
497

    
498
  /* We are using name as token, so no strcmp() */
499
  WALK_LIST(cf, proto->channels)
500
    if (cf->name == name)
501
    {
502
      /* Allow to redefine channel only if inherited from template */
503
      if (cf->parent == proto)
504
        cf_error("Multiple %s channels", name);
505

    
506
      cf->parent = proto;
507
      return cf;
508
    }
509

    
510
  return channel_config_new(cc, name, net_type, proto);
511
}
512

    
513
struct channel_config *
514
channel_copy_config(struct channel_config *src, struct proto_config *proto)
515
{
516
  struct channel_config *dst = cfg_alloc(src->channel->config_size);
517

    
518
  memcpy(dst, src, src->channel->config_size);
519
  add_tail(&proto->channels, &dst->n);
520
  CALL(src->channel->copy_config, dst, src);
521

    
522
  return dst;
523
}
524

    
525

    
526
static int reconfigure_type;  /* Hack to propagate type info to channel_reconfigure() */
527

    
528
int
529
channel_reconfigure(struct channel *c, struct channel_config *cf)
530
{
531
  /* FIXME: better handle these changes, also handle in_keep_filtered */
532
  if ((c->table != cf->table->table) || (cf->ra_mode && (c->ra_mode != cf->ra_mode)))
533
    return 0;
534

    
535
  /* Note that filter_same() requires arguments in (new, old) order */
536
  int import_changed = !filter_same(cf->in_filter, c->in_filter);
537
  int export_changed = !filter_same(cf->out_filter, c->out_filter);
538

    
539
  if (c->preference != cf->preference)
540
    import_changed = 1;
541

    
542
  if (c->merge_limit != cf->merge_limit)
543
    export_changed = 1;
544

    
545
  /* Reconfigure channel fields */
546
  c->in_filter = cf->in_filter;
547
  c->out_filter = cf->out_filter;
548
  c->rx_limit = cf->rx_limit;
549
  c->in_limit = cf->in_limit;
550
  c->out_limit = cf->out_limit;
551

    
552
  // c->ra_mode = cf->ra_mode;
553
  c->merge_limit = cf->merge_limit;
554
  c->preference = cf->preference;
555
  c->in_keep_filtered = cf->in_keep_filtered;
556

    
557
  channel_verify_limits(c);
558

    
559
  /* Execute channel-specific reconfigure hook */
560
  if (c->channel->reconfigure && !c->channel->reconfigure(c, cf))
561
    return 0;
562

    
563
  /* If the channel is not open, it has no routes and we cannot reload it anyways */
564
  if (c->channel_state != CS_UP)
565
    return 1;
566

    
567
  if (reconfigure_type == RECONFIG_SOFT)
568
  {
569
    if (import_changed)
570
      log(L_INFO "Channel %s.%s changed import", c->proto->name, c->name);
571

    
572
    if (export_changed)
573
      log(L_INFO "Channel %s.%s changed export", c->proto->name, c->name);
574

    
575
    return 1;
576
  }
577

    
578
  /* Route reload may be not supported */
579
  if (import_changed && !channel_reloadable(c))
580
    return 0;
581

    
582
  if (import_changed || export_changed)
583
    log(L_INFO "Reloading channel %s.%s", c->proto->name, c->name);
584

    
585
  if (import_changed)
586
    channel_request_reload(c);
587

    
588
  if (export_changed)
589
    channel_request_feeding(c);
590

    
591
  return 1;
592
}
593

    
594

    
595
int
596
proto_configure_channel(struct proto *p, struct channel **pc, struct channel_config *cf)
597
{
598
  struct channel *c = *pc;
599

    
600
  if (!c && cf)
601
  {
602
    *pc = proto_add_channel(p, cf);
603
  }
604
  else if (c && !cf)
605
  {
606
    if (c->channel_state != CS_DOWN)
607
    {
608
      log(L_INFO "Cannot remove channel %s.%s", c->proto->name, c->name);
609
      return 0;
610
    }
611

    
612
    proto_remove_channel(p, c);
613
    *pc = NULL;
614
  }
615
  else if (c && cf)
616
  {
617
    if (!channel_reconfigure(c, cf))
618
    {
619
      log(L_INFO "Cannot reconfigure channel %s.%s", c->proto->name, c->name);
620
      return 0;
621
    }
622
  }
623

    
624
  return 1;
625
}
626

    
627

    
628
static void
629
proto_event(void *ptr)
630
{
631
  struct proto *p = ptr;
632

    
633
  if (p->do_start)
634
  {
635
    if_feed_baby(p);
636
    p->do_start = 0;
637
  }
638

    
639
  if (p->do_stop)
640
  {
641
    if (p->proto == &proto_unix_iface)
642
      if_flush_ifaces(p);
643
    p->do_stop = 0;
644
  }
645

    
646
  if (proto_is_done(p))
647
  {
648
    if (p->proto->cleanup)
649
      p->proto->cleanup(p);
650

    
651
    p->active = 0;
652
    proto_log_state_change(p);
653
    proto_rethink_goal(p);
654
  }
655
}
656

    
657

    
658
/**
659
 * proto_new - create a new protocol instance
660
 * @c: protocol configuration
661
 *
662
 * When a new configuration has been read in, the core code starts
663
 * initializing all the protocol instances configured by calling their
664
 * init() hooks with the corresponding instance configuration. The initialization
665
 * code of the protocol is expected to create a new instance according to the
666
 * configuration by calling this function and then modifying the default settings
667
 * to values wanted by the protocol.
668
 */
669
void *
670
proto_new(struct proto_config *cf)
671
{
672
  struct proto *p = mb_allocz(proto_pool, cf->protocol->proto_size);
673

    
674
  p->cf = cf;
675
  p->debug = cf->debug;
676
  p->mrtdump = cf->mrtdump;
677
  p->name = cf->name;
678
  p->proto = cf->protocol;
679
  p->net_type = cf->net_type;
680
  p->disabled = cf->disabled;
681
  p->hash_key = random_u32();
682
  cf->proto = p;
683

    
684
  init_list(&p->channels);
685

    
686
  return p;
687
}
688

    
689
static struct proto *
690
proto_init(struct proto_config *c, node *n)
691
{
692
  struct protocol *pr = c->protocol;
693
  struct proto *p = pr->init(c);
694

    
695
  p->proto_state = PS_DOWN;
696
  p->last_state_change = current_time();
697
  p->vrf = c->vrf;
698
  insert_node(&p->n, n);
699

    
700
  p->event = ev_new(proto_pool);
701
  p->event->hook = proto_event;
702
  p->event->data = p;
703

    
704
  PD(p, "Initializing%s", p->disabled ? " [disabled]" : "");
705

    
706
  return p;
707
}
708

    
709
static void
710
proto_start(struct proto *p)
711
{
712
  /* Here we cannot use p->cf->name since it won't survive reconfiguration */
713
  p->pool = rp_new(proto_pool, p->proto->name);
714

    
715
  if (graceful_restart_state == GRS_INIT)
716
    p->gr_recovery = 1;
717
}
718

    
719

    
720
/**
721
 * proto_config_new - create a new protocol configuration
722
 * @pr: protocol the configuration will belong to
723
 * @class: SYM_PROTO or SYM_TEMPLATE
724
 *
725
 * Whenever the configuration file says that a new instance
726
 * of a routing protocol should be created, the parser calls
727
 * proto_config_new() to create a configuration entry for this
728
 * instance (a structure staring with the &proto_config header
729
 * containing all the generic items followed by protocol-specific
730
 * ones). Also, the configuration entry gets added to the list
731
 * of protocol instances kept in the configuration.
732
 *
733
 * The function is also used to create protocol templates (when class
734
 * SYM_TEMPLATE is specified), the only difference is that templates
735
 * are not added to the list of protocol instances and therefore not
736
 * initialized during protos_commit()).
737
 */
738
void *
739
proto_config_new(struct protocol *pr, int class)
740
{
741
  struct proto_config *cf = cfg_allocz(pr->config_size);
742

    
743
  if (class == SYM_PROTO)
744
    add_tail(&new_config->protos, &cf->n);
745

    
746
  cf->global = new_config;
747
  cf->protocol = pr;
748
  cf->name = pr->name;
749
  cf->class = class;
750
  cf->debug = new_config->proto_default_debug;
751
  cf->mrtdump = new_config->proto_default_mrtdump;
752

    
753
  init_list(&cf->channels);
754

    
755
  return cf;
756
}
757

    
758

    
759
/**
760
 * proto_copy_config - copy a protocol configuration
761
 * @dest: destination protocol configuration
762
 * @src: source protocol configuration
763
 *
764
 * Whenever a new instance of a routing protocol is created from the
765
 * template, proto_copy_config() is called to copy a content of
766
 * the source protocol configuration to the new protocol configuration.
767
 * Name, class and a node in protos list of @dest are kept intact.
768
 * copy_config() protocol hook is used to copy protocol-specific data.
769
 */
770
void
771
proto_copy_config(struct proto_config *dest, struct proto_config *src)
772
{
773
  struct channel_config *cc;
774
  node old_node;
775
  int old_class;
776
  char *old_name;
777

    
778
  if (dest->protocol != src->protocol)
779
    cf_error("Can't copy configuration from a different protocol type");
780

    
781
  if (dest->protocol->copy_config == NULL)
782
    cf_error("Inheriting configuration for %s is not supported", src->protocol->name);
783

    
784
  DBG("Copying configuration from %s to %s\n", src->name, dest->name);
785

    
786
  /*
787
   * Copy struct proto_config here. Keep original node, class and name.
788
   * protocol-specific config copy is handled by protocol copy_config() hook
789
   */
790

    
791
  old_node = dest->n;
792
  old_class = dest->class;
793
  old_name = dest->name;
794

    
795
  memcpy(dest, src, src->protocol->config_size);
796

    
797
  dest->n = old_node;
798
  dest->class = old_class;
799
  dest->name = old_name;
800
  init_list(&dest->channels);
801

    
802
  WALK_LIST(cc, src->channels)
803
    channel_copy_config(cc, dest);
804

    
805
  /* FIXME: allow for undefined copy_config */
806
  dest->protocol->copy_config(dest, src);
807
}
808

    
809
/**
810
 * protos_preconfig - pre-configuration processing
811
 * @c: new configuration
812
 *
813
 * This function calls the preconfig() hooks of all routing
814
 * protocols available to prepare them for reading of the new
815
 * configuration.
816
 */
817
void
818
protos_preconfig(struct config *c)
819
{
820
  struct protocol *p;
821

    
822
  init_list(&c->protos);
823
  DBG("Protocol preconfig:");
824
  WALK_LIST(p, protocol_list)
825
  {
826
    DBG(" %s", p->name);
827
    p->name_counter = 0;
828
    if (p->preconfig)
829
      p->preconfig(p, c);
830
  }
831
  DBG("\n");
832
}
833

    
834
static int
835
proto_reconfigure(struct proto *p, struct proto_config *oc, struct proto_config *nc, int type)
836
{
837
  /* If the protocol is DOWN, we just restart it */
838
  if (p->proto_state == PS_DOWN)
839
    return 0;
840

    
841
  /* If there is a too big change in core attributes, ... */
842
  if ((nc->protocol != oc->protocol) ||
843
      (nc->net_type != oc->net_type) ||
844
      (nc->disabled != p->disabled) ||
845
      (nc->vrf != oc->vrf))
846
    return 0;
847

    
848
  p->name = nc->name;
849
  p->debug = nc->debug;
850
  p->mrtdump = nc->mrtdump;
851
  reconfigure_type = type;
852

    
853
  /* Execute protocol specific reconfigure hook */
854
  if (!p->proto->reconfigure || !p->proto->reconfigure(p, nc))
855
    return 0;
856

    
857
  DBG("\t%s: same\n", oc->name);
858
  PD(p, "Reconfigured");
859
  p->cf = nc;
860

    
861
  return 1;
862
}
863

    
864
/**
865
 * protos_commit - commit new protocol configuration
866
 * @new: new configuration
867
 * @old: old configuration or %NULL if it's boot time config
868
 * @force_reconfig: force restart of all protocols (used for example
869
 * when the router ID changes)
870
 * @type: type of reconfiguration (RECONFIG_SOFT or RECONFIG_HARD)
871
 *
872
 * Scan differences between @old and @new configuration and adjust all
873
 * protocol instances to conform to the new configuration.
874
 *
875
 * When a protocol exists in the new configuration, but it doesn't in the
876
 * original one, it's immediately started. When a collision with the other
877
 * running protocol would arise, the new protocol will be temporarily stopped
878
 * by the locking mechanism.
879
 *
880
 * When a protocol exists in the old configuration, but it doesn't in the
881
 * new one, it's shut down and deleted after the shutdown completes.
882
 *
883
 * When a protocol exists in both configurations, the core decides
884
 * whether it's possible to reconfigure it dynamically - it checks all
885
 * the core properties of the protocol (changes in filters are ignored
886
 * if type is RECONFIG_SOFT) and if they match, it asks the
887
 * reconfigure() hook of the protocol to see if the protocol is able
888
 * to switch to the new configuration.  If it isn't possible, the
889
 * protocol is shut down and a new instance is started with the new
890
 * configuration after the shutdown is completed.
891
 */
892
void
893
protos_commit(struct config *new, struct config *old, int force_reconfig, int type)
894
{
895
  struct proto_config *oc, *nc;
896
  struct symbol *sym;
897
  struct proto *p;
898
  node *n;
899

    
900

    
901
  DBG("protos_commit:\n");
902
  if (old)
903
  {
904
    WALK_LIST(oc, old->protos)
905
    {
906
      p = oc->proto;
907
      sym = cf_find_symbol(new, oc->name);
908
      if (sym && sym->class == SYM_PROTO && !new->shutdown)
909
      {
910
        /* Found match, let's check if we can smoothly switch to new configuration */
911
        /* No need to check description */
912
        nc = sym->def;
913
        nc->proto = p;
914

    
915
        /* We will try to reconfigure protocol p */
916
        if (! force_reconfig && proto_reconfigure(p, oc, nc, type))
917
          continue;
918

    
919
        /* Unsuccessful, we will restart it */
920
        if (!p->disabled && !nc->disabled)
921
          log(L_INFO "Restarting protocol %s", p->name);
922
        else if (p->disabled && !nc->disabled)
923
          log(L_INFO "Enabling protocol %s", p->name);
924
        else if (!p->disabled && nc->disabled)
925
          log(L_INFO "Disabling protocol %s", p->name);
926

    
927
        p->down_code = nc->disabled ? PDC_CF_DISABLE : PDC_CF_RESTART;
928
        p->cf_new = nc;
929
      }
930
      else if (!new->shutdown)
931
      {
932
        log(L_INFO "Removing protocol %s", p->name);
933
        p->down_code = PDC_CF_REMOVE;
934
        p->cf_new = NULL;
935
      }
936
      else /* global shutdown */
937
      {
938
        p->down_code = PDC_CMD_SHUTDOWN;
939
        p->cf_new = NULL;
940
      }
941

    
942
      p->reconfiguring = 1;
943
      config_add_obstacle(old);
944
      proto_rethink_goal(p);
945
    }
946
  }
947

    
948
  struct proto *first_dev_proto = NULL;
949

    
950
  n = NODE &(proto_list.head);
951
  WALK_LIST(nc, new->protos)
952
    if (!nc->proto)
953
    {
954
      /* Not a first-time configuration */
955
      if (old)
956
        log(L_INFO "Adding protocol %s", nc->name);
957

    
958
      p = proto_init(nc, n);
959
      n = NODE p;
960

    
961
      if (p->proto == &proto_unix_iface)
962
        first_dev_proto = p;
963
    }
964
    else
965
      n = NODE nc->proto;
966

    
967
  DBG("Protocol start\n");
968

    
969
  /* Start device protocol first */
970
  if (first_dev_proto)
971
    proto_rethink_goal(first_dev_proto);
972

    
973
  /* Determine router ID for the first time - it has to be here and not in
974
     global_commit() because it is postponed after start of device protocol */
975
  if (!config->router_id)
976
  {
977
    config->router_id = if_choose_router_id(config->router_id_from, 0);
978
    if (!config->router_id)
979
      die("Cannot determine router ID, please configure it manually");
980
  }
981

    
982
  /* Start all new protocols */
983
  WALK_LIST_DELSAFE(p, n, proto_list)
984
    proto_rethink_goal(p);
985
}
986

    
987
static void
988
proto_rethink_goal(struct proto *p)
989
{
990
  struct protocol *q;
991
  byte goal;
992

    
993
  if (p->reconfiguring && !p->active)
994
  {
995
    struct proto_config *nc = p->cf_new;
996
    node *n = p->n.prev;
997
    DBG("%s has shut down for reconfiguration\n", p->name);
998
    p->cf->proto = NULL;
999
    config_del_obstacle(p->cf->global);
1000
    proto_remove_channels(p);
1001
    rem_node(&p->n);
1002
    rfree(p->event);
1003
    mb_free(p->message);
1004
    mb_free(p);
1005
    if (!nc)
1006
      return;
1007
    p = proto_init(nc, n);
1008
  }
1009

    
1010
  /* Determine what state we want to reach */
1011
  if (p->disabled || p->reconfiguring)
1012
    goal = PS_DOWN;
1013
  else
1014
    goal = PS_UP;
1015

    
1016
  q = p->proto;
1017
  if (goal == PS_UP)
1018
  {
1019
    if (!p->active)
1020
    {
1021
      /* Going up */
1022
      DBG("Kicking %s up\n", p->name);
1023
      PD(p, "Starting");
1024
      proto_start(p);
1025
      proto_notify_state(p, (q->start ? q->start(p) : PS_UP));
1026
    }
1027
  }
1028
  else
1029
  {
1030
    if (p->proto_state == PS_START || p->proto_state == PS_UP)
1031
    {
1032
      /* Going down */
1033
      DBG("Kicking %s down\n", p->name);
1034
      PD(p, "Shutting down");
1035
      proto_notify_state(p, (q->shutdown ? q->shutdown(p) : PS_DOWN));
1036
    }
1037
  }
1038
}
1039

    
1040

    
1041
/**
1042
 * DOC: Graceful restart recovery
1043
 *
1044
 * Graceful restart of a router is a process when the routing plane (e.g. BIRD)
1045
 * restarts but both the forwarding plane (e.g kernel routing table) and routing
1046
 * neighbors keep proper routes, and therefore uninterrupted packet forwarding
1047
 * is maintained.
1048
 *
1049
 * BIRD implements graceful restart recovery by deferring export of routes to
1050
 * protocols until routing tables are refilled with the expected content. After
1051
 * start, protocols generate routes as usual, but routes are not propagated to
1052
 * them, until protocols report that they generated all routes. After that,
1053
 * graceful restart recovery is finished and the export (and the initial feed)
1054
 * to protocols is enabled.
1055
 *
1056
 * When graceful restart recovery need is detected during initialization, then
1057
 * enabled protocols are marked with @gr_recovery flag before start. Such
1058
 * protocols then decide how to proceed with graceful restart, participation is
1059
 * voluntary. Protocols could lock the recovery for each channel by function
1060
 * channel_graceful_restart_lock() (state stored in @gr_lock flag), which means
1061
 * that they want to postpone the end of the recovery until they converge and
1062
 * then unlock it. They also could set @gr_wait before advancing to %PS_UP,
1063
 * which means that the core should defer route export to that channel until
1064
 * the end of the recovery. This should be done by protocols that expect their
1065
 * neigbors to keep the proper routes (kernel table, BGP sessions with BGP
1066
 * graceful restart capability).
1067
 *
1068
 * The graceful restart recovery is finished when either all graceful restart
1069
 * locks are unlocked or when graceful restart wait timer fires.
1070
 *
1071
 */
1072

    
1073
static void graceful_restart_done(timer *t);
1074

    
1075
/**
1076
 * graceful_restart_recovery - request initial graceful restart recovery
1077
 *
1078
 * Called by the platform initialization code if the need for recovery
1079
 * after graceful restart is detected during boot. Have to be called
1080
 * before protos_commit().
1081
 */
1082
void
1083
graceful_restart_recovery(void)
1084
{
1085
  graceful_restart_state = GRS_INIT;
1086
}
1087

    
1088
/**
1089
 * graceful_restart_init - initialize graceful restart
1090
 *
1091
 * When graceful restart recovery was requested, the function starts an active
1092
 * phase of the recovery and initializes graceful restart wait timer. The
1093
 * function have to be called after protos_commit().
1094
 */
1095
void
1096
graceful_restart_init(void)
1097
{
1098
  if (!graceful_restart_state)
1099
    return;
1100

    
1101
  log(L_INFO "Graceful restart started");
1102

    
1103
  if (!graceful_restart_locks)
1104
  {
1105
    graceful_restart_done(NULL);
1106
    return;
1107
  }
1108

    
1109
  graceful_restart_state = GRS_ACTIVE;
1110
  gr_wait_timer = tm_new_init(proto_pool, graceful_restart_done, NULL, 0, 0);
1111
  tm_start(gr_wait_timer, config->gr_wait S);
1112
}
1113

    
1114
/**
1115
 * graceful_restart_done - finalize graceful restart
1116
 * @t: unused
1117
 *
1118
 * When there are no locks on graceful restart, the functions finalizes the
1119
 * graceful restart recovery. Protocols postponing route export until the end of
1120
 * the recovery are awakened and the export to them is enabled. All other
1121
 * related state is cleared. The function is also called when the graceful
1122
 * restart wait timer fires (but there are still some locks).
1123
 */
1124
static void
1125
graceful_restart_done(timer *t UNUSED)
1126
{
1127
  log(L_INFO "Graceful restart done");
1128
  graceful_restart_state = GRS_DONE;
1129

    
1130
  struct proto *p;
1131
  WALK_LIST(p, proto_list)
1132
  {
1133
    if (!p->gr_recovery)
1134
      continue;
1135

    
1136
    struct channel *c;
1137
    WALK_LIST(c, p->channels)
1138
    {
1139
      /* Resume postponed export of routes */
1140
      if ((c->channel_state == CS_UP) && c->gr_wait && c->proto->rt_notify)
1141
        channel_start_export(c);
1142

    
1143
      /* Cleanup */
1144
      c->gr_wait = 0;
1145
      c->gr_lock = 0;
1146
    }
1147

    
1148
    p->gr_recovery = 0;
1149
  }
1150

    
1151
  graceful_restart_locks = 0;
1152
}
1153

    
1154
void
1155
graceful_restart_show_status(void)
1156
{
1157
  if (graceful_restart_state != GRS_ACTIVE)
1158
    return;
1159

    
1160
  cli_msg(-24, "Graceful restart recovery in progress");
1161
  cli_msg(-24, "  Waiting for %d channels to recover", graceful_restart_locks);
1162
  cli_msg(-24, "  Wait timer is %t/%u", tm_remains(gr_wait_timer), config->gr_wait);
1163
}
1164

    
1165
/**
1166
 * channel_graceful_restart_lock - lock graceful restart by channel
1167
 * @p: channel instance
1168
 *
1169
 * This function allows a protocol to postpone the end of graceful restart
1170
 * recovery until it converges. The lock is removed when the protocol calls
1171
 * channel_graceful_restart_unlock() or when the channel is closed.
1172
 *
1173
 * The function have to be called during the initial phase of graceful restart
1174
 * recovery and only for protocols that are part of graceful restart (i.e. their
1175
 * @gr_recovery is set), which means it should be called from protocol start
1176
 * hooks.
1177
 */
1178
void
1179
channel_graceful_restart_lock(struct channel *c)
1180
{
1181
  ASSERT(graceful_restart_state == GRS_INIT);
1182
  ASSERT(c->proto->gr_recovery);
1183

    
1184
  if (c->gr_lock)
1185
    return;
1186

    
1187
  c->gr_lock = 1;
1188
  graceful_restart_locks++;
1189
}
1190

    
1191
/**
1192
 * channel_graceful_restart_unlock - unlock graceful restart by channel
1193
 * @p: channel instance
1194
 *
1195
 * This function unlocks a lock from channel_graceful_restart_lock(). It is also
1196
 * automatically called when the lock holding protocol went down.
1197
 */
1198
void
1199
channel_graceful_restart_unlock(struct channel *c)
1200
{
1201
  if (!c->gr_lock)
1202
    return;
1203

    
1204
  c->gr_lock = 0;
1205
  graceful_restart_locks--;
1206

    
1207
  if ((graceful_restart_state == GRS_ACTIVE) && !graceful_restart_locks)
1208
    tm_start(gr_wait_timer, 0);
1209
}
1210

    
1211

    
1212

    
1213
/**
1214
 * protos_dump_all - dump status of all protocols
1215
 *
1216
 * This function dumps status of all existing protocol instances to the
1217
 * debug output. It involves printing of general status information
1218
 * such as protocol states, its position on the protocol lists
1219
 * and also calling of a dump() hook of the protocol to print
1220
 * the internals.
1221
 */
1222
void
1223
protos_dump_all(void)
1224
{
1225
  debug("Protocols:\n");
1226

    
1227
  struct proto *p;
1228
  WALK_LIST(p, proto_list)
1229
  {
1230
    debug("  protocol %s state %s\n", p->name, p_states[p->proto_state]);
1231

    
1232
    struct channel *c;
1233
    WALK_LIST(c, p->channels)
1234
    {
1235
      debug("\tTABLE %s\n", c->table->name);
1236
      if (c->in_filter)
1237
        debug("\tInput filter: %s\n", filter_name(c->in_filter));
1238
      if (c->out_filter)
1239
        debug("\tOutput filter: %s\n", filter_name(c->out_filter));
1240
    }
1241

    
1242
    if (p->proto->dump && (p->proto_state != PS_DOWN))
1243
      p->proto->dump(p);
1244
  }
1245
}
1246

    
1247
/**
1248
 * proto_build - make a single protocol available
1249
 * @p: the protocol
1250
 *
1251
 * After the platform specific initialization code uses protos_build()
1252
 * to add all the standard protocols, it should call proto_build() for
1253
 * all platform specific protocols to inform the core that they exist.
1254
 */
1255
void
1256
proto_build(struct protocol *p)
1257
{
1258
  add_tail(&protocol_list, &p->n);
1259
  if (p->attr_class)
1260
    {
1261
      ASSERT(!attr_class_to_protocol[p->attr_class]);
1262
      attr_class_to_protocol[p->attr_class] = p;
1263
    }
1264
}
1265

    
1266
/* FIXME: convert this call to some protocol hook */
1267
extern void bfd_init_all(void);
1268

    
1269
/**
1270
 * protos_build - build a protocol list
1271
 *
1272
 * This function is called during BIRD startup to insert
1273
 * all standard protocols to the global protocol list. Insertion
1274
 * of platform specific protocols (such as the kernel syncer)
1275
 * is in the domain of competence of the platform dependent
1276
 * startup code.
1277
 */
1278
void
1279
protos_build(void)
1280
{
1281
  init_list(&proto_list);
1282
  init_list(&protocol_list);
1283

    
1284
  proto_build(&proto_device);
1285
#ifdef CONFIG_RADV
1286
  proto_build(&proto_radv);
1287
#endif
1288
#ifdef CONFIG_RIP
1289
  proto_build(&proto_rip);
1290
#endif
1291
#ifdef CONFIG_STATIC
1292
  proto_build(&proto_static);
1293
#endif
1294
#ifdef CONFIG_OSPF
1295
  proto_build(&proto_ospf);
1296
#endif
1297
#ifdef CONFIG_PIPE
1298
  proto_build(&proto_pipe);
1299
#endif
1300
#ifdef CONFIG_BGP
1301
  proto_build(&proto_bgp);
1302
#endif
1303
#ifdef CONFIG_BFD
1304
  proto_build(&proto_bfd);
1305
  bfd_init_all();
1306
#endif
1307
#ifdef CONFIG_BABEL
1308
  proto_build(&proto_babel);
1309
#endif
1310
#ifdef CONFIG_RPKI
1311
  proto_build(&proto_rpki);
1312
#endif
1313

    
1314
  proto_pool = rp_new(&root_pool, "Protocols");
1315
  proto_shutdown_timer = tm_new(proto_pool);
1316
  proto_shutdown_timer->hook = proto_shutdown_loop;
1317
}
1318

    
1319

    
1320
/* Temporary hack to propagate restart to BGP */
1321
int proto_restart;
1322

    
1323
static void
1324
proto_shutdown_loop(timer *t UNUSED)
1325
{
1326
  struct proto *p, *p_next;
1327

    
1328
  WALK_LIST_DELSAFE(p, p_next, proto_list)
1329
    if (p->down_sched)
1330
    {
1331
      proto_restart = (p->down_sched == PDS_RESTART);
1332

    
1333
      p->disabled = 1;
1334
      proto_rethink_goal(p);
1335
      if (proto_restart)
1336
      {
1337
        p->disabled = 0;
1338
        proto_rethink_goal(p);
1339
      }
1340
    }
1341
}
1342

    
1343
static inline void
1344
proto_schedule_down(struct proto *p, byte restart, byte code)
1345
{
1346
  /* Does not work for other states (even PS_START) */
1347
  ASSERT(p->proto_state == PS_UP);
1348

    
1349
  /* Scheduled restart may change to shutdown, but not otherwise */
1350
  if (p->down_sched == PDS_DISABLE)
1351
    return;
1352

    
1353
  p->down_sched = restart ? PDS_RESTART : PDS_DISABLE;
1354
  p->down_code = code;
1355
  tm_start_max(proto_shutdown_timer, restart ? 250 MS : 0);
1356
}
1357

    
1358
/**
1359
 * proto_set_message - set administrative message to protocol
1360
 * @p: protocol
1361
 * @msg: message
1362
 * @len: message length (-1 for NULL-terminated string)
1363
 *
1364
 * The function sets administrative message (string) related to protocol state
1365
 * change. It is called by the nest code for manual enable/disable/restart
1366
 * commands all routes to the protocol, and by protocol-specific code when the
1367
 * protocol state change is initiated by the protocol. Using NULL message clears
1368
 * the last message. The message string may be either NULL-terminated or with an
1369
 * explicit length.
1370
 */
1371
void
1372
proto_set_message(struct proto *p, char *msg, int len)
1373
{
1374
  mb_free(p->message);
1375
  p->message = NULL;
1376

    
1377
  if (!msg || !len)
1378
    return;
1379

    
1380
  if (len < 0)
1381
    len = strlen(msg);
1382

    
1383
  if (!len)
1384
    return;
1385

    
1386
  p->message = mb_alloc(proto_pool, len + 1);
1387
  memcpy(p->message, msg, len);
1388
  p->message[len] = 0;
1389
}
1390

    
1391

    
1392
static const char *
1393
channel_limit_name(struct channel_limit *l)
1394
{
1395
  const char *actions[] = {
1396
    [PLA_WARN] = "warn",
1397
    [PLA_BLOCK] = "block",
1398
    [PLA_RESTART] = "restart",
1399
    [PLA_DISABLE] = "disable",
1400
  };
1401

    
1402
  return actions[l->action];
1403
}
1404

    
1405
/**
1406
 * channel_notify_limit: notify about limit hit and take appropriate action
1407
 * @c: channel
1408
 * @l: limit being hit
1409
 * @dir: limit direction (PLD_*)
1410
 * @rt_count: the number of routes
1411
 *
1412
 * The function is called by the route processing core when limit @l
1413
 * is breached. It activates the limit and tooks appropriate action
1414
 * according to @l->action.
1415
 */
1416
void
1417
channel_notify_limit(struct channel *c, struct channel_limit *l, int dir, u32 rt_count)
1418
{
1419
  const char *dir_name[PLD_MAX] = { "receive", "import" , "export" };
1420
  const byte dir_down[PLD_MAX] = { PDC_RX_LIMIT_HIT, PDC_IN_LIMIT_HIT, PDC_OUT_LIMIT_HIT };
1421
  struct proto *p = c->proto;
1422

    
1423
  if (l->state == PLS_BLOCKED)
1424
    return;
1425

    
1426
  /* For warning action, we want the log message every time we hit the limit */
1427
  if (!l->state || ((l->action == PLA_WARN) && (rt_count == l->limit)))
1428
    log(L_WARN "Protocol %s hits route %s limit (%d), action: %s",
1429
        p->name, dir_name[dir], l->limit, channel_limit_name(l));
1430

    
1431
  switch (l->action)
1432
  {
1433
  case PLA_WARN:
1434
    l->state = PLS_ACTIVE;
1435
    break;
1436

    
1437
  case PLA_BLOCK:
1438
    l->state = PLS_BLOCKED;
1439
    break;
1440

    
1441
  case PLA_RESTART:
1442
  case PLA_DISABLE:
1443
    l->state = PLS_BLOCKED;
1444
    if (p->proto_state == PS_UP)
1445
      proto_schedule_down(p, l->action == PLA_RESTART, dir_down[dir]);
1446
    break;
1447
  }
1448
}
1449

    
1450
static void
1451
channel_verify_limits(struct channel *c)
1452
{
1453
  struct channel_limit *l;
1454
  u32 all_routes = c->stats.imp_routes + c->stats.filt_routes;
1455

    
1456
  l = &c->rx_limit;
1457
  if (l->action && (all_routes > l->limit))
1458
    channel_notify_limit(c, l, PLD_RX, all_routes);
1459

    
1460
  l = &c->in_limit;
1461
  if (l->action && (c->stats.imp_routes > l->limit))
1462
    channel_notify_limit(c, l, PLD_IN, c->stats.imp_routes);
1463

    
1464
  l = &c->out_limit;
1465
  if (l->action && (c->stats.exp_routes > l->limit))
1466
    channel_notify_limit(c, l, PLD_OUT, c->stats.exp_routes);
1467
}
1468

    
1469
static inline void
1470
channel_reset_limit(struct channel_limit *l)
1471
{
1472
  if (l->action)
1473
    l->state = PLS_INITIAL;
1474
}
1475

    
1476
static inline void
1477
proto_do_start(struct proto *p)
1478
{
1479
  p->active = 1;
1480
  p->do_start = 1;
1481
  ev_schedule(p->event);
1482
}
1483

    
1484
static void
1485
proto_do_up(struct proto *p)
1486
{
1487
  if (!p->main_source)
1488
  {
1489
    p->main_source = rt_get_source(p, 0);
1490
    rt_lock_source(p->main_source);
1491
  }
1492

    
1493
  proto_start_channels(p);
1494
}
1495

    
1496
static inline void
1497
proto_do_pause(struct proto *p)
1498
{
1499
  proto_pause_channels(p);
1500
}
1501

    
1502
static void
1503
proto_do_stop(struct proto *p)
1504
{
1505
  p->down_sched = 0;
1506
  p->gr_recovery = 0;
1507

    
1508
  p->do_stop = 1;
1509
  ev_schedule(p->event);
1510

    
1511
  if (p->main_source)
1512
  {
1513
    rt_unlock_source(p->main_source);
1514
    p->main_source = NULL;
1515
  }
1516

    
1517
  proto_stop_channels(p);
1518
}
1519

    
1520
static void
1521
proto_do_down(struct proto *p)
1522
{
1523
  p->down_code = 0;
1524
  neigh_prune();
1525
  rfree(p->pool);
1526
  p->pool = NULL;
1527

    
1528
  /* Shutdown is finished in the protocol event */
1529
  if (proto_is_done(p))
1530
    ev_schedule(p->event);
1531
}
1532

    
1533

    
1534

    
1535
/**
1536
 * proto_notify_state - notify core about protocol state change
1537
 * @p: protocol the state of which has changed
1538
 * @ps: the new status
1539
 *
1540
 * Whenever a state of a protocol changes due to some event internal
1541
 * to the protocol (i.e., not inside a start() or shutdown() hook),
1542
 * it should immediately notify the core about the change by calling
1543
 * proto_notify_state() which will write the new state to the &proto
1544
 * structure and take all the actions necessary to adapt to the new
1545
 * state. State change to PS_DOWN immediately frees resources of protocol
1546
 * and might execute start callback of protocol; therefore,
1547
 * it should be used at tail positions of protocol callbacks.
1548
 */
1549
void
1550
proto_notify_state(struct proto *p, uint state)
1551
{
1552
  uint ps = p->proto_state;
1553

    
1554
  DBG("%s reporting state transition %s -> %s\n", p->name, p_states[ps], p_states[state]);
1555
  if (state == ps)
1556
    return;
1557

    
1558
  p->proto_state = state;
1559
  p->last_state_change = current_time();
1560

    
1561
  switch (state)
1562
  {
1563
  case PS_START:
1564
    ASSERT(ps == PS_DOWN || ps == PS_UP);
1565

    
1566
    if (ps == PS_DOWN)
1567
      proto_do_start(p);
1568
    else
1569
      proto_do_pause(p);
1570
    break;
1571

    
1572
  case PS_UP:
1573
    ASSERT(ps == PS_DOWN || ps == PS_START);
1574

    
1575
    if (ps == PS_DOWN)
1576
      proto_do_start(p);
1577

    
1578
    proto_do_up(p);
1579
    break;
1580

    
1581
  case PS_STOP:
1582
    ASSERT(ps == PS_START || ps == PS_UP);
1583

    
1584
    proto_do_stop(p);
1585
    break;
1586

    
1587
  case PS_DOWN:
1588
    if (ps != PS_STOP)
1589
      proto_do_stop(p);
1590

    
1591
    proto_do_down(p);
1592
    break;
1593

    
1594
  default:
1595
    bug("%s: Invalid state %d", p->name, ps);
1596
  }
1597

    
1598
  proto_log_state_change(p);
1599
}
1600

    
1601
/*
1602
 *  CLI Commands
1603
 */
1604

    
1605
static char *
1606
proto_state_name(struct proto *p)
1607
{
1608
  switch (p->proto_state)
1609
  {
1610
  case PS_DOWN:                return p->active ? "flush" : "down";
1611
  case PS_START:        return "start";
1612
  case PS_UP:                return "up";
1613
  case PS_STOP:                return "stop";
1614
  default:                return "???";
1615
  }
1616
}
1617

    
1618
static void
1619
channel_show_stats(struct channel *c)
1620
{
1621
  struct proto_stats *s = &c->stats;
1622

    
1623
  if (c->in_keep_filtered)
1624
    cli_msg(-1006, "    Routes:         %u imported, %u filtered, %u exported",
1625
            s->imp_routes, s->filt_routes, s->exp_routes);
1626
  else
1627
    cli_msg(-1006, "    Routes:         %u imported, %u exported",
1628
            s->imp_routes, s->exp_routes);
1629

    
1630
  cli_msg(-1006, "    Route change stats:     received   rejected   filtered    ignored   accepted");
1631
  cli_msg(-1006, "      Import updates:     %10u %10u %10u %10u %10u",
1632
          s->imp_updates_received, s->imp_updates_invalid,
1633
          s->imp_updates_filtered, s->imp_updates_ignored,
1634
          s->imp_updates_accepted);
1635
  cli_msg(-1006, "      Import withdraws:   %10u %10u        --- %10u %10u",
1636
          s->imp_withdraws_received, s->imp_withdraws_invalid,
1637
          s->imp_withdraws_ignored, s->imp_withdraws_accepted);
1638
  cli_msg(-1006, "      Export updates:     %10u %10u %10u        --- %10u",
1639
          s->exp_updates_received, s->exp_updates_rejected,
1640
          s->exp_updates_filtered, s->exp_updates_accepted);
1641
  cli_msg(-1006, "      Export withdraws:   %10u        ---        ---        --- %10u",
1642
          s->exp_withdraws_received, s->exp_withdraws_accepted);
1643
}
1644

    
1645
void
1646
channel_show_limit(struct channel_limit *l, const char *dsc)
1647
{
1648
  if (!l->action)
1649
    return;
1650

    
1651
  cli_msg(-1006, "    %-16s%d%s", dsc, l->limit, l->state ? " [HIT]" : "");
1652
  cli_msg(-1006, "      Action:       %s", channel_limit_name(l));
1653
}
1654

    
1655
void
1656
channel_show_info(struct channel *c)
1657
{
1658
  cli_msg(-1006, "  Channel %s", c->name);
1659
  cli_msg(-1006, "    State:          %s", c_states[c->channel_state]);
1660
  cli_msg(-1006, "    Table:          %s", c->table->name);
1661
  cli_msg(-1006, "    Preference:     %d", c->preference);
1662
  cli_msg(-1006, "    Input filter:   %s", filter_name(c->in_filter));
1663
  cli_msg(-1006, "    Output filter:  %s", filter_name(c->out_filter));
1664

    
1665
  if (graceful_restart_state == GRS_ACTIVE)
1666
    cli_msg(-1006, "    GR recovery:   %s%s",
1667
            c->gr_lock ? " pending" : "",
1668
            c->gr_wait ? " waiting" : "");
1669

    
1670
  channel_show_limit(&c->rx_limit, "Receive limit:");
1671
  channel_show_limit(&c->in_limit, "Import limit:");
1672
  channel_show_limit(&c->out_limit, "Export limit:");
1673

    
1674
  if (c->channel_state != CS_DOWN)
1675
    channel_show_stats(c);
1676
}
1677

    
1678
void
1679
proto_cmd_show(struct proto *p, uintptr_t verbose, int cnt)
1680
{
1681
  byte buf[256], tbuf[TM_DATETIME_BUFFER_SIZE];
1682

    
1683
  /* First protocol - show header */
1684
  if (!cnt)
1685
    cli_msg(-2002, "%-10s %-10s %-10s %-6s %-12s  %s",
1686
            "Name", "Proto", "Table", "State", "Since", "Info");
1687

    
1688
  buf[0] = 0;
1689
  if (p->proto->get_status)
1690
    p->proto->get_status(p, buf);
1691
  tm_format_time(tbuf, &config->tf_proto, p->last_state_change);
1692
  cli_msg(-1002, "%-10s %-10s %-10s %-6s %-12s  %s",
1693
          p->name,
1694
          p->proto->name,
1695
          p->main_channel ? p->main_channel->table->name : "---",
1696
          proto_state_name(p),
1697
          tbuf,
1698
          buf);
1699

    
1700
  if (verbose)
1701
  {
1702
    if (p->cf->dsc)
1703
      cli_msg(-1006, "  Description:    %s", p->cf->dsc);
1704
    if (p->message)
1705
      cli_msg(-1006, "  Message:        %s", p->message);
1706
    if (p->cf->router_id)
1707
      cli_msg(-1006, "  Router ID:      %R", p->cf->router_id);
1708
    if (p->vrf)
1709
      cli_msg(-1006, "  VRF:            %s", p->vrf->name);
1710

    
1711
    if (p->proto->show_proto_info)
1712
      p->proto->show_proto_info(p);
1713
    else
1714
    {
1715
      struct channel *c;
1716
      WALK_LIST(c, p->channels)
1717
        channel_show_info(c);
1718
    }
1719

    
1720
    cli_msg(-1006, "");
1721
  }
1722
}
1723

    
1724
void
1725
proto_cmd_disable(struct proto *p, uintptr_t arg, int cnt UNUSED)
1726
{
1727
  if (p->disabled)
1728
  {
1729
    cli_msg(-8, "%s: already disabled", p->name);
1730
    return;
1731
  }
1732

    
1733
  log(L_INFO "Disabling protocol %s", p->name);
1734
  p->disabled = 1;
1735
  p->down_code = PDC_CMD_DISABLE;
1736
  proto_set_message(p, (char *) arg, -1);
1737
  proto_rethink_goal(p);
1738
  cli_msg(-9, "%s: disabled", p->name);
1739
}
1740

    
1741
void
1742
proto_cmd_enable(struct proto *p, uintptr_t arg, int cnt UNUSED)
1743
{
1744
  if (!p->disabled)
1745
  {
1746
    cli_msg(-10, "%s: already enabled", p->name);
1747
    return;
1748
  }
1749

    
1750
  log(L_INFO "Enabling protocol %s", p->name);
1751
  p->disabled = 0;
1752
  proto_set_message(p, (char *) arg, -1);
1753
  proto_rethink_goal(p);
1754
  cli_msg(-11, "%s: enabled", p->name);
1755
}
1756

    
1757
void
1758
proto_cmd_restart(struct proto *p, uintptr_t arg, int cnt UNUSED)
1759
{
1760
  if (p->disabled)
1761
  {
1762
    cli_msg(-8, "%s: already disabled", p->name);
1763
    return;
1764
  }
1765

    
1766
  log(L_INFO "Restarting protocol %s", p->name);
1767
  p->disabled = 1;
1768
  p->down_code = PDC_CMD_RESTART;
1769
  proto_set_message(p, (char *) arg, -1);
1770
  proto_rethink_goal(p);
1771
  p->disabled = 0;
1772
  proto_rethink_goal(p);
1773
  cli_msg(-12, "%s: restarted", p->name);
1774
}
1775

    
1776
void
1777
proto_cmd_reload(struct proto *p, uintptr_t dir, int cnt UNUSED)
1778
{
1779
  struct channel *c;
1780

    
1781
  if (p->disabled)
1782
  {
1783
    cli_msg(-8, "%s: already disabled", p->name);
1784
    return;
1785
  }
1786

    
1787
  /* If the protocol in not UP, it has no routes */
1788
  if (p->proto_state != PS_UP)
1789
    return;
1790

    
1791
  /* All channels must support reload */
1792
  if (dir != CMD_RELOAD_OUT)
1793
    WALK_LIST(c, p->channels)
1794
      if (!channel_reloadable(c))
1795
      {
1796
        cli_msg(-8006, "%s: reload failed", p->name);
1797
        return;
1798
      }
1799

    
1800
  log(L_INFO "Reloading protocol %s", p->name);
1801

    
1802
  /* re-importing routes */
1803
  if (dir != CMD_RELOAD_OUT)
1804
    WALK_LIST(c, p->channels)
1805
      channel_request_reload(c);
1806

    
1807
  /* re-exporting routes */
1808
  if (dir != CMD_RELOAD_IN)
1809
    WALK_LIST(c, p->channels)
1810
      channel_request_feeding(c);
1811

    
1812
  cli_msg(-15, "%s: reloading", p->name);
1813
}
1814

    
1815
void
1816
proto_cmd_debug(struct proto *p, uintptr_t mask, int cnt UNUSED)
1817
{
1818
  p->debug = mask;
1819
}
1820

    
1821
void
1822
proto_cmd_mrtdump(struct proto *p, uintptr_t mask, int cnt UNUSED)
1823
{
1824
  p->mrtdump = mask;
1825
}
1826

    
1827
static void
1828
proto_apply_cmd_symbol(struct symbol *s, void (* cmd)(struct proto *, uintptr_t, int), uintptr_t arg)
1829
{
1830
  if (s->class != SYM_PROTO)
1831
  {
1832
    cli_msg(9002, "%s is not a protocol", s->name);
1833
    return;
1834
  }
1835

    
1836
  cmd(((struct proto_config *)s->def)->proto, arg, 0);
1837
  cli_msg(0, "");
1838
}
1839

    
1840
static void
1841
proto_apply_cmd_patt(char *patt, void (* cmd)(struct proto *, uintptr_t, int), uintptr_t arg)
1842
{
1843
  struct proto *p;
1844
  int cnt = 0;
1845

    
1846
  WALK_LIST(p, proto_list)
1847
    if (!patt || patmatch(patt, p->name))
1848
      cmd(p, arg, cnt++);
1849

    
1850
  if (!cnt)
1851
    cli_msg(8003, "No protocols match");
1852
  else
1853
    cli_msg(0, "");
1854
}
1855

    
1856
void
1857
proto_apply_cmd(struct proto_spec ps, void (* cmd)(struct proto *, uintptr_t, int),
1858
                int restricted, uintptr_t arg)
1859
{
1860
  if (restricted && cli_access_restricted())
1861
    return;
1862

    
1863
  if (ps.patt)
1864
    proto_apply_cmd_patt(ps.ptr, cmd, arg);
1865
  else
1866
    proto_apply_cmd_symbol(ps.ptr, cmd, arg);
1867
}
1868

    
1869
struct proto *
1870
proto_get_named(struct symbol *sym, struct protocol *pr)
1871
{
1872
  struct proto *p, *q;
1873

    
1874
  if (sym)
1875
  {
1876
    if (sym->class != SYM_PROTO)
1877
      cf_error("%s: Not a protocol", sym->name);
1878

    
1879
    p = ((struct proto_config *) sym->def)->proto;
1880
    if (!p || p->proto != pr)
1881
      cf_error("%s: Not a %s protocol", sym->name, pr->name);
1882
  }
1883
  else
1884
  {
1885
    p = NULL;
1886
    WALK_LIST(q, proto_list)
1887
      if ((q->proto == pr) && (q->proto_state != PS_DOWN))
1888
      {
1889
        if (p)
1890
          cf_error("There are multiple %s protocols running", pr->name);
1891
        p = q;
1892
      }
1893
    if (!p)
1894
      cf_error("There is no %s protocol running", pr->name);
1895
  }
1896

    
1897
  return p;
1898
}