Statistics
| Branch: | Revision:

iof-bird-daemon / nest / proto.c @ f4a60a9b

History | View | Annotate | Download (43 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

    
43
extern struct protocol proto_unix_iface;
44

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

    
51

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

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

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

    
74

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

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

    
84
  return NULL;
85
}
86

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

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

    
103
  return NULL;
104
}
105

    
106
/**
107
 * proto_add_channel - connect protocol to a routing table
108
 * @p: protocol instance
109
 * @cf: channel configuration
110
 *
111
 * This function creates a channel between the protocol instance @p and the
112
 * routing table specified in the configuration @cf, making the protocol hear
113
 * all changes in the table and allowing the protocol to update routes in the
114
 * table.
115
 *
116
 * The channel is linked in the protocol channel list and when active also in
117
 * the table channel list. Channels are allocated from the global resource pool
118
 * (@proto_pool) and they are automatically freed when the protocol is removed.
119
 */
120

    
121
struct channel *
122
proto_add_channel(struct proto *p, struct channel_config *cf)
123
{
124
  struct channel *c = mb_allocz(proto_pool, cf->channel->channel_size);
125

    
126
  c->name = cf->name;
127
  c->channel = cf->channel;
128
  c->proto = p;
129
  c->table = cf->table->table;
130

    
131
  c->in_filter = cf->in_filter;
132
  c->out_filter = cf->out_filter;
133
  c->rx_limit = cf->rx_limit;
134
  c->in_limit = cf->in_limit;
135
  c->out_limit = cf->out_limit;
136

    
137
  c->net_type = cf->net_type;
138
  c->ra_mode = cf->ra_mode;
139
  c->preference = cf->preference;
140
  c->merge_limit = cf->merge_limit;
141
  c->in_keep_filtered = cf->in_keep_filtered;
142

    
143
  c->channel_state = CS_DOWN;
144
  c->export_state = ES_DOWN;
145
  c->last_state_change = now;
146
  c->reloadable = 1;
147

    
148
  CALL(c->channel->init, c, cf);
149

    
150
  add_tail(&p->channels, &c->n);
151

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

    
154
  return c;
155
}
156

    
157
void
158
proto_remove_channel(struct proto *p, struct channel *c)
159
{
160
  ASSERT(c->channel_state == CS_DOWN);
161

    
162
  PD(p, "Channel %s removed", c->name);
163

    
164
  rem_node(&c->n);
165
  mb_free(c);
166
}
167

    
168

    
169
static void
170
proto_start_channels(struct proto *p)
171
{
172
  struct channel *c;
173
  WALK_LIST(c, p->channels)
174
    if (!c->disabled)
175
      channel_set_state(c, CS_UP);
176
}
177

    
178
static void
179
proto_pause_channels(struct proto *p)
180
{
181
  struct channel *c;
182
  WALK_LIST(c, p->channels)
183
    if (!c->disabled && channel_is_active(c))
184
      channel_set_state(c, CS_START);
185
}
186

    
187
static void
188
proto_stop_channels(struct proto *p)
189
{
190
  struct channel *c;
191
  WALK_LIST(c, p->channels)
192
    if (!c->disabled && channel_is_active(c))
193
      channel_set_state(c, CS_FLUSHING);
194
}
195

    
196
static void
197
proto_remove_channels(struct proto *p)
198
{
199
  struct channel *c;
200
  WALK_LIST_FIRST(c, p->channels)
201
    proto_remove_channel(p, c);
202
}
203

    
204
static void
205
channel_schedule_feed(struct channel *c, int initial)
206
{
207
  // DBG("%s: Scheduling meal\n", p->name);
208
  ASSERT(c->channel_state == CS_UP);
209

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

    
213
  ev_schedule(c->feed_event);
214
}
215

    
216
static void
217
channel_feed_loop(void *ptr)
218
{
219
  struct channel *c = ptr;
220

    
221
  if (c->export_state != ES_FEEDING)
222
    return;
223

    
224
  if (!c->feed_active)
225
    if (c->proto->feed_begin)
226
      c->proto->feed_begin(c, !c->refeeding);
227

    
228
  // DBG("Feeding protocol %s continued\n", p->name);
229
  if (!rt_feed_channel(c))
230
  {
231
    ev_schedule(c->feed_event);
232
    return;
233
  }
234

    
235
  // DBG("Feeding protocol %s finished\n", p->name);
236
  c->export_state = ES_READY;
237
  // proto_log_state_change(p);
238

    
239
  if (c->proto->feed_end)
240
    c->proto->feed_end(c);
241
}
242

    
243

    
244
static void
245
channel_start_export(struct channel *c)
246
{
247
  ASSERT(c->channel_state == CS_UP);
248
  ASSERT(c->export_state == ES_DOWN);
249

    
250
  channel_schedule_feed(c, 1);        /* Sets ES_FEEDING */
251
}
252

    
253
static void
254
channel_stop_export(struct channel *c)
255
{
256
  /* Need to abort feeding */
257
  if (c->export_state == ES_FEEDING)
258
    rt_feed_channel_abort(c);
259

    
260
  c->export_state = ES_DOWN;
261
}
262

    
263
static void
264
channel_do_start(struct channel *c)
265
{
266
  rt_lock_table(c->table);
267
  add_tail(&c->table->channels, &c->table_node);
268
  c->proto->active_channels++;
269

    
270
  c->feed_event = ev_new(c->proto->pool);
271
  c->feed_event->data = c;
272
  c->feed_event->hook = channel_feed_loop;
273

    
274
  channel_reset_limit(&c->rx_limit);
275
  channel_reset_limit(&c->in_limit);
276
  channel_reset_limit(&c->out_limit);
277

    
278
  CALL(c->channel->start, c);
279
}
280

    
281
static void
282
channel_do_flush(struct channel *c)
283
{
284
  rt_schedule_prune(c->table);
285

    
286
  c->gr_wait = 0;
287
  if (c->gr_lock)
288
    channel_graceful_restart_unlock(c);
289

    
290
  CALL(c->channel->shutdown, c);
291
}
292

    
293
static void
294
channel_do_down(struct channel *c)
295
{
296
  rem2_node(&c->table_node);
297
  rt_unlock_table(c->table);
298
  c->proto->active_channels--;
299

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

    
303
  memset(&c->stats, 0, sizeof(struct proto_stats));
304

    
305
  /* Schedule protocol shutddown */
306
  if (proto_is_done(c->proto))
307
    ev_schedule(c->proto->event);
308
}
309

    
310
void
311
channel_set_state(struct channel *c, uint state)
312
{
313
  uint cs = c->channel_state;
314
  uint es = c->export_state;
315

    
316
  DBG("%s reporting state transition %s/%s -> */%s\n", p->name, c_states[cs], p_states[ops], p_states[ps]);
317
  if (state == cs)
318
    return;
319

    
320
  c->channel_state = state;
321
  c->last_state_change = now;
322

    
323
  switch (state)
324
  {
325
  case CS_START:
326
    ASSERT(cs == CS_DOWN || cs == CS_UP);
327

    
328
    if (cs == CS_DOWN)
329
      channel_do_start(c);
330

    
331
    if (es != ES_DOWN)
332
      channel_stop_export(c);
333

    
334
    break;
335

    
336
  case CS_UP:
337
    ASSERT(cs == CS_DOWN || cs == CS_START);
338

    
339
    if (cs == CS_DOWN)
340
      channel_do_start(c);
341

    
342
    if (!c->gr_wait)
343
      channel_start_export(c);
344

    
345
    break;
346

    
347
  case CS_FLUSHING:
348
    ASSERT(cs == CS_START || cs == CS_UP);
349

    
350
    if (es != ES_DOWN)
351
      channel_stop_export(c);
352

    
353
    channel_do_flush(c);
354
    break;
355

    
356
  case CS_DOWN:
357
    ASSERT(cs == CS_FLUSHING);
358

    
359
    channel_do_down(c);
360
    break;
361

    
362
  default:
363
    ASSERT(0);
364
  }
365
  // XXXX proto_log_state_change(c);
366
}
367

    
368
/**
369
 * channel_request_feeding - request feeding routes to the channel
370
 * @c: given channel
371
 *
372
 * Sometimes it is needed to send again all routes to the channel. This is
373
 * called feeding and can be requested by this function. This would cause
374
 * channel export state transition to ES_FEEDING (during feeding) and when
375
 * completed, it will switch back to ES_READY. This function can be called
376
 * even when feeding is already running, in that case it is restarted.
377
 */
378
void
379
channel_request_feeding(struct channel *c)
380
{
381
  ASSERT(c->channel_state == CS_UP);
382

    
383
  /* Do nothing if we are still waiting for feeding */
384
  if (c->export_state == ES_DOWN)
385
    return;
386

    
387
  /* If we are already feeding, we want to restart it */
388
  if (c->export_state == ES_FEEDING)
389
  {
390
    /* Unless feeding is in initial state */
391
    if (!c->feed_active)
392
        return;
393

    
394
    rt_feed_channel_abort(c);
395
  }
396

    
397
  channel_reset_limit(&c->out_limit);
398

    
399
  /* Hack: reset exp_routes during refeed, and do not decrease it later */
400
  c->stats.exp_routes = 0;
401

    
402
  channel_schedule_feed(c, 0);        /* Sets ES_FEEDING */
403
  // proto_log_state_change(c);
404
}
405

    
406
static inline int
407
channel_reloadable(struct channel *c)
408
{
409
  return c->proto->reload_routes && c->reloadable;
410
}
411

    
412
static void
413
channel_request_reload(struct channel *c)
414
{
415
  ASSERT(c->channel_state == CS_UP);
416
  // ASSERT(channel_reloadable(c));
417

    
418
  c->proto->reload_routes(c);
419

    
420
  /*
421
   * Should this be done before reload_routes() hook?
422
   * Perhaps, but routes are updated asynchronously.
423
   */
424
  channel_reset_limit(&c->rx_limit);
425
  channel_reset_limit(&c->in_limit);
426
}
427

    
428
const struct channel_class channel_basic = {
429
  .channel_size = sizeof(struct channel),
430
  .config_size = sizeof(struct channel_config)
431
};
432

    
433
void *
434
channel_config_new(const struct channel_class *cc, uint net_type, struct proto_config *proto)
435
{
436
  struct channel_config *cf = NULL;
437
  struct rtable_config *tab = NULL;
438
  const char *name = NULL;
439

    
440
  if (net_type)
441
  {
442
    if (!net_val_match(net_type, proto->protocol->channel_mask))
443
      cf_error("Unsupported channel type");
444

    
445
    if (proto->net_type && (net_type != proto->net_type))
446
      cf_error("Different channel type");
447

    
448
    tab = new_config->def_tables[net_type];
449
    name = net_label[net_type];
450
  }
451

    
452
  if (!cc)
453
    cc = &channel_basic;
454

    
455
  cf = cfg_allocz(cc->config_size);
456
  cf->name = name;
457
  cf->channel = cc;
458
  cf->table = tab;
459
  cf->out_filter = FILTER_REJECT;
460

    
461
  cf->net_type = net_type;
462
  cf->ra_mode = RA_OPTIMAL;
463
  cf->preference = proto->protocol->preference;
464

    
465
  add_tail(&proto->channels, &cf->n);
466

    
467
  return cf;
468
}
469

    
470
struct channel_config *
471
channel_copy_config(struct channel_config *src, struct proto_config *proto)
472
{
473
  struct channel_config *dst = cfg_alloc(src->channel->config_size);
474

    
475
  memcpy(dst, src, src->channel->config_size);
476
  add_tail(&proto->channels, &dst->n);
477
  CALL(src->channel->copy_config, dst, src);
478

    
479
  return dst;
480
}
481

    
482

    
483
static int reconfigure_type;  /* Hack to propagate type info to channel_reconfigure() */
484

    
485
int
486
channel_reconfigure(struct channel *c, struct channel_config *cf)
487
{
488
  /* FIXME: better handle these changes, also handle in_keep_filtered */
489
  if ((c->table != cf->table->table) || (c->ra_mode != cf->ra_mode))
490
    return 0;
491

    
492
  int import_changed = !filter_same(c->in_filter, cf->in_filter);
493
  int export_changed = !filter_same(c->out_filter, cf->out_filter);
494

    
495
  if (c->preference != cf->preference)
496
    import_changed = 1;
497

    
498
  if (c->merge_limit != cf->merge_limit)
499
    export_changed = 1;
500

    
501
  /* Reconfigure channel fields */
502
  c->in_filter = cf->in_filter;
503
  c->out_filter = cf->out_filter;
504
  c->rx_limit = cf->rx_limit;
505
  c->in_limit = cf->in_limit;
506
  c->out_limit = cf->out_limit;
507

    
508
  // c->ra_mode = cf->ra_mode;
509
  c->merge_limit = cf->merge_limit;
510
  c->preference = cf->preference;
511
  c->in_keep_filtered = cf->in_keep_filtered;
512

    
513
  channel_verify_limits(c);
514

    
515
  CALL(c->channel->reconfigure, c, cf);
516

    
517
  /* If the channel is not open, it has no routes and we cannot reload it anyways */
518
  if (c->channel_state != CS_UP)
519
    return 1;
520

    
521
  if (reconfigure_type == RECONFIG_SOFT)
522
  {
523
    if (import_changed)
524
      log(L_INFO "Channel %s.%s changed import", c->proto->name, c->name);
525

    
526
    if (export_changed)
527
      log(L_INFO "Channel %s.%s changed export", c->proto->name, c->name);
528

    
529
    return 1;
530
  }
531

    
532
  /* Route reload may be not supported */
533
  if (import_changed && !channel_reloadable(c))
534
    return 0;
535

    
536
  if (import_changed || export_changed)
537
    log(L_INFO "Reloading channel %s.%s", c->proto->name, c->name);
538

    
539
  if (import_changed)
540
    channel_request_reload(c);
541

    
542
  if (export_changed)
543
    channel_request_feeding(c);
544

    
545
  return 1;
546
}
547

    
548

    
549
int
550
proto_configure_channel(struct proto *p, struct channel **pc, struct channel_config *cf)
551
{
552
  struct channel *c = *pc;
553

    
554
  if (!c && cf)
555
  {
556
    *pc = proto_add_channel(p, cf);
557
  }
558
  else if (c && !cf)
559
  {
560
    if (c->channel_state != CS_DOWN)
561
    {
562
      log(L_INFO "Cannot remove channel %s.%s", c->proto->name, c->name);
563
      return 0;
564
    }
565

    
566
    proto_remove_channel(p, c);
567
    *pc = NULL;
568
  }
569
  else if (c && cf)
570
  {
571
    if (!channel_reconfigure(c, cf))
572
    {
573
      log(L_INFO "Cannot reconfigure channel %s.%s", c->proto->name, c->name);
574
      return 0;
575
    }
576
  }
577

    
578
  return 1;
579
}
580

    
581

    
582
static void
583
proto_event(void *ptr)
584
{
585
  struct proto *p = ptr;
586

    
587
  if (p->do_start)
588
  {
589
    if_feed_baby(p);
590
    p->do_start = 0;
591
  }
592

    
593
  if (p->do_stop)
594
  {
595
    if (p->proto == &proto_unix_iface)
596
      if_flush_ifaces(p);
597
    p->do_stop = 0;
598
  }
599

    
600
  if (proto_is_done(p))
601
  {
602
    if (p->proto->cleanup)
603
      p->proto->cleanup(p);
604

    
605
    p->active = 0;
606
    proto_log_state_change(p);
607
    proto_rethink_goal(p);
608
  }
609
}
610

    
611

    
612
/**
613
 * proto_new - create a new protocol instance
614
 * @c: protocol configuration
615
 *
616
 * When a new configuration has been read in, the core code starts
617
 * initializing all the protocol instances configured by calling their
618
 * init() hooks with the corresponding instance configuration. The initialization
619
 * code of the protocol is expected to create a new instance according to the
620
 * configuration by calling this function and then modifying the default settings
621
 * to values wanted by the protocol.
622
 */
623
void *
624
proto_new(struct proto_config *cf)
625
{
626
  struct proto *p = mb_allocz(proto_pool, cf->protocol->proto_size);
627

    
628
  p->cf = cf;
629
  p->debug = cf->debug;
630
  p->mrtdump = cf->mrtdump;
631
  p->name = cf->name;
632
  p->proto = cf->protocol;
633
  p->net_type = cf->net_type;
634
  p->disabled = cf->disabled;
635
  p->hash_key = random_u32();
636
  cf->proto = p;
637

    
638
  init_list(&p->channels);
639

    
640
  return p;
641
}
642

    
643
static struct proto *
644
proto_init(struct proto_config *c, node *n)
645
{
646
  struct protocol *pr = c->protocol;
647
  struct proto *p = pr->init(c);
648

    
649
  p->proto_state = PS_DOWN;
650
  p->last_state_change = now;
651
  insert_node(&p->n, n);
652

    
653
  p->event = ev_new(proto_pool);
654
  p->event->hook = proto_event;
655
  p->event->data = p;
656

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

    
659
  return p;
660
}
661

    
662
static void
663
proto_start(struct proto *p)
664
{
665
  /* Here we cannot use p->cf->name since it won't survive reconfiguration */
666
  p->pool = rp_new(proto_pool, p->proto->name);
667

    
668
  if (graceful_restart_state == GRS_INIT)
669
    p->gr_recovery = 1;
670
}
671

    
672

    
673
/**
674
 * proto_config_new - create a new protocol configuration
675
 * @pr: protocol the configuration will belong to
676
 * @class: SYM_PROTO or SYM_TEMPLATE
677
 *
678
 * Whenever the configuration file says that a new instance
679
 * of a routing protocol should be created, the parser calls
680
 * proto_config_new() to create a configuration entry for this
681
 * instance (a structure staring with the &proto_config header
682
 * containing all the generic items followed by protocol-specific
683
 * ones). Also, the configuration entry gets added to the list
684
 * of protocol instances kept in the configuration.
685
 *
686
 * The function is also used to create protocol templates (when class
687
 * SYM_TEMPLATE is specified), the only difference is that templates
688
 * are not added to the list of protocol instances and therefore not
689
 * initialized during protos_commit()).
690
 */
691
void *
692
proto_config_new(struct protocol *pr, int class)
693
{
694
  struct proto_config *cf = cfg_allocz(pr->config_size);
695

    
696
  if (class == SYM_PROTO)
697
    add_tail(&new_config->protos, &cf->n);
698

    
699
  cf->global = new_config;
700
  cf->protocol = pr;
701
  cf->name = pr->name;
702
  cf->class = class;
703
  cf->debug = new_config->proto_default_debug;
704
  cf->mrtdump = new_config->proto_default_mrtdump;
705

    
706
  init_list(&cf->channels);
707

    
708
  return cf;
709
}
710

    
711

    
712
/**
713
 * proto_copy_config - copy a protocol configuration
714
 * @dest: destination protocol configuration
715
 * @src: source protocol configuration
716
 *
717
 * Whenever a new instance of a routing protocol is created from the
718
 * template, proto_copy_config() is called to copy a content of
719
 * the source protocol configuration to the new protocol configuration.
720
 * Name, class and a node in protos list of @dest are kept intact.
721
 * copy_config() protocol hook is used to copy protocol-specific data.
722
 */
723
void
724
proto_copy_config(struct proto_config *dest, struct proto_config *src)
725
{
726
  struct channel_config *cc;
727
  node old_node;
728
  int old_class;
729
  char *old_name;
730

    
731
  if (dest->protocol != src->protocol)
732
    cf_error("Can't copy configuration from a different protocol type");
733

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

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

    
739
  /*
740
   * Copy struct proto_config here. Keep original node, class and name.
741
   * protocol-specific config copy is handled by protocol copy_config() hook
742
   */
743

    
744
  old_node = dest->n;
745
  old_class = dest->class;
746
  old_name = dest->name;
747

    
748
  memcpy(dest, src, src->protocol->config_size);
749

    
750
  dest->n = old_node;
751
  dest->class = old_class;
752
  dest->name = old_name;
753
  init_list(&dest->channels);
754

    
755
  WALK_LIST(cc, src->channels)
756
    channel_copy_config(cc, dest);
757

    
758
  /* FIXME: allow for undefined copy_config */
759
  dest->protocol->copy_config(dest, src);
760
}
761

    
762
/**
763
 * protos_preconfig - pre-configuration processing
764
 * @c: new configuration
765
 *
766
 * This function calls the preconfig() hooks of all routing
767
 * protocols available to prepare them for reading of the new
768
 * configuration.
769
 */
770
void
771
protos_preconfig(struct config *c)
772
{
773
  struct protocol *p;
774

    
775
  init_list(&c->protos);
776
  DBG("Protocol preconfig:");
777
  WALK_LIST(p, protocol_list)
778
  {
779
    DBG(" %s", p->name);
780
    p->name_counter = 0;
781
    if (p->preconfig)
782
      p->preconfig(p, c);
783
  }
784
  DBG("\n");
785
}
786

    
787
static int
788
proto_reconfigure(struct proto *p, struct proto_config *oc, struct proto_config *nc, int type)
789
{
790
  /* If the protocol is DOWN, we just restart it */
791
  if (p->proto_state == PS_DOWN)
792
    return 0;
793

    
794
  /* If there is a too big change in core attributes, ... */
795
  if ((nc->protocol != oc->protocol) ||
796
      (nc->net_type != oc->net_type) ||
797
      (nc->disabled != p->disabled))
798

    
799
    return 0;
800

    
801
  p->name = nc->name;
802
  p->debug = nc->debug;
803
  p->mrtdump = nc->mrtdump;
804
  reconfigure_type = type;
805

    
806
  /* Execute protocol specific reconfigure hook */
807
  if (!p->proto->reconfigure || !p->proto->reconfigure(p, nc))
808
    return 0;
809

    
810
  DBG("\t%s: same\n", oc->name);
811
  PD(p, "Reconfigured");
812
  p->cf = nc;
813

    
814
  return 1;
815
}
816

    
817
/**
818
 * protos_commit - commit new protocol configuration
819
 * @new: new configuration
820
 * @old: old configuration or %NULL if it's boot time config
821
 * @force_reconfig: force restart of all protocols (used for example
822
 * when the router ID changes)
823
 * @type: type of reconfiguration (RECONFIG_SOFT or RECONFIG_HARD)
824
 *
825
 * Scan differences between @old and @new configuration and adjust all
826
 * protocol instances to conform to the new configuration.
827
 *
828
 * When a protocol exists in the new configuration, but it doesn't in the
829
 * original one, it's immediately started. When a collision with the other
830
 * running protocol would arise, the new protocol will be temporarily stopped
831
 * by the locking mechanism.
832
 *
833
 * When a protocol exists in the old configuration, but it doesn't in the
834
 * new one, it's shut down and deleted after the shutdown completes.
835
 *
836
 * When a protocol exists in both configurations, the core decides
837
 * whether it's possible to reconfigure it dynamically - it checks all
838
 * the core properties of the protocol (changes in filters are ignored
839
 * if type is RECONFIG_SOFT) and if they match, it asks the
840
 * reconfigure() hook of the protocol to see if the protocol is able
841
 * to switch to the new configuration.  If it isn't possible, the
842
 * protocol is shut down and a new instance is started with the new
843
 * configuration after the shutdown is completed.
844
 */
845
void
846
protos_commit(struct config *new, struct config *old, int force_reconfig, int type)
847
{
848
  struct proto_config *oc, *nc;
849
  struct symbol *sym;
850
  struct proto *p;
851
  node *n;
852

    
853

    
854
  DBG("protos_commit:\n");
855
  if (old)
856
  {
857
    WALK_LIST(oc, old->protos)
858
    {
859
      p = oc->proto;
860
      sym = cf_find_symbol(new, oc->name);
861
      if (sym && sym->class == SYM_PROTO && !new->shutdown)
862
      {
863
        /* Found match, let's check if we can smoothly switch to new configuration */
864
        /* No need to check description */
865
        nc = sym->def;
866
        nc->proto = p;
867

    
868
        /* We will try to reconfigure protocol p */
869
        if (! force_reconfig && proto_reconfigure(p, oc, nc, type))
870
          continue;
871

    
872
        /* Unsuccessful, we will restart it */
873
        if (!p->disabled && !nc->disabled)
874
          log(L_INFO "Restarting protocol %s", p->name);
875
        else if (p->disabled && !nc->disabled)
876
          log(L_INFO "Enabling protocol %s", p->name);
877
        else if (!p->disabled && nc->disabled)
878
          log(L_INFO "Disabling protocol %s", p->name);
879

    
880
        p->down_code = nc->disabled ? PDC_CF_DISABLE : PDC_CF_RESTART;
881
        p->cf_new = nc;
882
      }
883
      else if (!new->shutdown)
884
      {
885
        log(L_INFO "Removing protocol %s", p->name);
886
        p->down_code = PDC_CF_REMOVE;
887
        p->cf_new = NULL;
888
      }
889
      else /* global shutdown */
890
      {
891
        p->down_code = PDC_CMD_SHUTDOWN;
892
        p->cf_new = NULL;
893
      }
894

    
895
      p->reconfiguring = 1;
896
      config_add_obstacle(old);
897
      proto_rethink_goal(p);
898
    }
899
  }
900

    
901
  struct proto *first_dev_proto = NULL;
902

    
903
  n = NODE &(proto_list.head);
904
  WALK_LIST(nc, new->protos)
905
    if (!nc->proto)
906
    {
907
      /* Not a first-time configuration */
908
      if (old)
909
        log(L_INFO "Adding protocol %s", nc->name);
910

    
911
      p = proto_init(nc, n);
912
      n = NODE p;
913

    
914
      if (p->proto == &proto_unix_iface)
915
        first_dev_proto = p;
916
    }
917
    else
918
      n = NODE nc->proto;
919

    
920
  DBG("Protocol start\n");
921

    
922
  /* Start device protocol first */
923
  if (first_dev_proto)
924
    proto_rethink_goal(first_dev_proto);
925

    
926
  /* Determine router ID for the first time - it has to be here and not in
927
     global_commit() because it is postponed after start of device protocol */
928
  if (!config->router_id)
929
  {
930
    config->router_id = if_choose_router_id(config->router_id_from, 0);
931
    if (!config->router_id)
932
      die("Cannot determine router ID, please configure it manually");
933
  }
934

    
935
  /* Start all new protocols */
936
  WALK_LIST_DELSAFE(p, n, proto_list)
937
    proto_rethink_goal(p);
938
}
939

    
940
static void
941
proto_rethink_goal(struct proto *p)
942
{
943
  struct protocol *q;
944
  byte goal;
945

    
946
  if (p->reconfiguring && !p->active)
947
  {
948
    struct proto_config *nc = p->cf_new;
949
    node *n = p->n.prev;
950
    DBG("%s has shut down for reconfiguration\n", p->name);
951
    p->cf->proto = NULL;
952
    config_del_obstacle(p->cf->global);
953
    proto_remove_channels(p);
954
    rem_node(&p->n);
955
    rfree(p->event);
956
    mb_free(p);
957
    if (!nc)
958
      return;
959
    p = proto_init(nc, n);
960
  }
961

    
962
  /* Determine what state we want to reach */
963
  if (p->disabled || p->reconfiguring)
964
    goal = PS_DOWN;
965
  else
966
    goal = PS_UP;
967

    
968
  q = p->proto;
969
  if (goal == PS_UP)
970
  {
971
    if (!p->active)
972
    {
973
      /* Going up */
974
      DBG("Kicking %s up\n", p->name);
975
      PD(p, "Starting");
976
      proto_start(p);
977
      proto_notify_state(p, (q->start ? q->start(p) : PS_UP));
978
    }
979
  }
980
  else
981
  {
982
    if (p->proto_state == PS_START || p->proto_state == PS_UP)
983
    {
984
      /* Going down */
985
      DBG("Kicking %s down\n", p->name);
986
      PD(p, "Shutting down");
987
      proto_notify_state(p, (q->shutdown ? q->shutdown(p) : PS_DOWN));
988
    }
989
  }
990
}
991

    
992

    
993
/**
994
 * DOC: Graceful restart recovery
995
 *
996
 * Graceful restart of a router is a process when the routing plane (e.g. BIRD)
997
 * restarts but both the forwarding plane (e.g kernel routing table) and routing
998
 * neighbors keep proper routes, and therefore uninterrupted packet forwarding
999
 * is maintained.
1000
 *
1001
 * BIRD implements graceful restart recovery by deferring export of routes to
1002
 * protocols until routing tables are refilled with the expected content. After
1003
 * start, protocols generate routes as usual, but routes are not propagated to
1004
 * them, until protocols report that they generated all routes. After that,
1005
 * graceful restart recovery is finished and the export (and the initial feed)
1006
 * to protocols is enabled.
1007
 *
1008
 * When graceful restart recovery need is detected during initialization, then
1009
 * enabled protocols are marked with @gr_recovery flag before start. Such
1010
 * protocols then decide how to proceed with graceful restart, participation is
1011
 * voluntary. Protocols could lock the recovery for each channel by function
1012
 * channel_graceful_restart_lock() (starte stored in @gr_lock flag), which means
1013
 * that they want to postpone the end of the recovery until they converge and
1014
 * then unlock it. They also could set @gr_wait before advancing to %PS_UP,
1015
 * which means that the core should defer route export to that channel until
1016
 * the end of the recovery. This should be done by protocols that expect their
1017
 * neigbors to keep the proper routes (kernel table, BGP sessions with BGP
1018
 * graceful restart capability).
1019
 *
1020
 * The graceful restart recovery is finished when either all graceful restart
1021
 * locks are unlocked or when graceful restart wait timer fires.
1022
 *
1023
 */
1024

    
1025
static void graceful_restart_done(struct timer *t);
1026

    
1027
/**
1028
 * graceful_restart_recovery - request initial graceful restart recovery
1029
 *
1030
 * Called by the platform initialization code if the need for recovery
1031
 * after graceful restart is detected during boot. Have to be called
1032
 * before protos_commit().
1033
 */
1034
void
1035
graceful_restart_recovery(void)
1036
{
1037
  graceful_restart_state = GRS_INIT;
1038
}
1039

    
1040
/**
1041
 * graceful_restart_init - initialize graceful restart
1042
 *
1043
 * When graceful restart recovery was requested, the function starts an active
1044
 * phase of the recovery and initializes graceful restart wait timer. The
1045
 * function have to be called after protos_commit().
1046
 */
1047
void
1048
graceful_restart_init(void)
1049
{
1050
  if (!graceful_restart_state)
1051
    return;
1052

    
1053
  log(L_INFO "Graceful restart started");
1054

    
1055
  if (!graceful_restart_locks)
1056
  {
1057
    graceful_restart_done(NULL);
1058
    return;
1059
  }
1060

    
1061
  graceful_restart_state = GRS_ACTIVE;
1062
  gr_wait_timer = tm_new(proto_pool);
1063
  gr_wait_timer->hook = graceful_restart_done;
1064
  tm_start(gr_wait_timer, config->gr_wait);
1065
}
1066

    
1067
/**
1068
 * graceful_restart_done - finalize graceful restart
1069
 *
1070
 * When there are no locks on graceful restart, the functions finalizes the
1071
 * graceful restart recovery. Protocols postponing route export until the end of
1072
 * the recovery are awakened and the export to them is enabled. All other
1073
 * related state is cleared. The function is also called when the graceful
1074
 * restart wait timer fires (but there are still some locks).
1075
 */
1076
static void
1077
graceful_restart_done(struct timer *t UNUSED)
1078
{
1079
  log(L_INFO "Graceful restart done");
1080
  graceful_restart_state = GRS_DONE;
1081

    
1082
  struct proto *p;
1083
  WALK_LIST(p, proto_list)
1084
  {
1085
    if (!p->gr_recovery)
1086
      continue;
1087

    
1088
    struct channel *c;
1089
    WALK_LIST(c, p->channels)
1090
    {
1091
      /* Resume postponed export of routes */
1092
      if ((c->channel_state == CS_UP) && c->gr_wait)
1093
        channel_start_export(c);
1094

    
1095
      /* Cleanup */
1096
      c->gr_wait = 0;
1097
      c->gr_lock = 0;
1098
    }
1099

    
1100
    p->gr_recovery = 0;
1101
  }
1102

    
1103
  graceful_restart_locks = 0;
1104
}
1105

    
1106
void
1107
graceful_restart_show_status(void)
1108
{
1109
  if (graceful_restart_state != GRS_ACTIVE)
1110
    return;
1111

    
1112
  cli_msg(-24, "Graceful restart recovery in progress");
1113
  cli_msg(-24, "  Waiting for %d channels to recover", graceful_restart_locks);
1114
  cli_msg(-24, "  Wait timer is %d/%d", tm_remains(gr_wait_timer), config->gr_wait);
1115
}
1116

    
1117
/**
1118
 * channel_graceful_restart_lock - lock graceful restart by channel
1119
 * @p: channel instance
1120
 *
1121
 * This function allows a protocol to postpone the end of graceful restart
1122
 * recovery until it converges. The lock is removed when the protocol calls
1123
 * channel_graceful_restart_unlock() or when the channel is closed.
1124
 *
1125
 * The function have to be called during the initial phase of graceful restart
1126
 * recovery and only for protocols that are part of graceful restart (i.e. their
1127
 * @gr_recovery is set), which means it should be called from protocol start
1128
 * hooks.
1129
 */
1130
void
1131
channel_graceful_restart_lock(struct channel *c)
1132
{
1133
  ASSERT(graceful_restart_state == GRS_INIT);
1134
  ASSERT(c->proto->gr_recovery);
1135

    
1136
  if (c->gr_lock)
1137
    return;
1138

    
1139
  c->gr_lock = 1;
1140
  graceful_restart_locks++;
1141
}
1142

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

    
1156
  c->gr_lock = 0;
1157
  graceful_restart_locks--;
1158

    
1159
  if ((graceful_restart_state == GRS_ACTIVE) && !graceful_restart_locks)
1160
    tm_start(gr_wait_timer, 0);
1161
}
1162

    
1163

    
1164

    
1165
/**
1166
 * protos_dump_all - dump status of all protocols
1167
 *
1168
 * This function dumps status of all existing protocol instances to the
1169
 * debug output. It involves printing of general status information
1170
 * such as protocol states, its position on the protocol lists
1171
 * and also calling of a dump() hook of the protocol to print
1172
 * the internals.
1173
 */
1174
void
1175
protos_dump_all(void)
1176
{
1177
  debug("Protocols:\n");
1178

    
1179
  struct proto *p;
1180
  WALK_LIST(p, proto_list)
1181
  {
1182
    debug("  protocol %s state %s\n", p->name, p_states[p->proto_state]);
1183

    
1184
    struct channel *c;
1185
    WALK_LIST(c, p->channels)
1186
    {
1187
      debug("\tTABLE %s\n", c->table->name);
1188
      if (c->in_filter)
1189
        debug("\tInput filter: %s\n", filter_name(c->in_filter));
1190
      if (c->out_filter)
1191
        debug("\tOutput filter: %s\n", filter_name(c->out_filter));
1192
    }
1193

    
1194
    if (p->proto->dump && (p->proto_state != PS_DOWN))
1195
      p->proto->dump(p);
1196
  }
1197
}
1198

    
1199
/**
1200
 * proto_build - make a single protocol available
1201
 * @p: the protocol
1202
 *
1203
 * After the platform specific initialization code uses protos_build()
1204
 * to add all the standard protocols, it should call proto_build() for
1205
 * all platform specific protocols to inform the core that they exist.
1206
 */
1207
void
1208
proto_build(struct protocol *p)
1209
{
1210
  add_tail(&protocol_list, &p->n);
1211
  if (p->attr_class)
1212
    {
1213
      ASSERT(!attr_class_to_protocol[p->attr_class]);
1214
      attr_class_to_protocol[p->attr_class] = p;
1215
    }
1216
}
1217

    
1218
/* FIXME: convert this call to some protocol hook */
1219
extern void bfd_init_all(void);
1220

    
1221
/**
1222
 * protos_build - build a protocol list
1223
 *
1224
 * This function is called during BIRD startup to insert
1225
 * all standard protocols to the global protocol list. Insertion
1226
 * of platform specific protocols (such as the kernel syncer)
1227
 * is in the domain of competence of the platform dependent
1228
 * startup code.
1229
 */
1230
void
1231
protos_build(void)
1232
{
1233
  init_list(&proto_list);
1234
  init_list(&protocol_list);
1235

    
1236
  proto_build(&proto_device);
1237
#ifdef CONFIG_RADV
1238
  proto_build(&proto_radv);
1239
#endif
1240
#ifdef CONFIG_RIP
1241
  proto_build(&proto_rip);
1242
#endif
1243
#ifdef CONFIG_STATIC
1244
  proto_build(&proto_static);
1245
#endif
1246
#ifdef CONFIG_OSPF
1247
  proto_build(&proto_ospf);
1248
#endif
1249
#ifdef CONFIG_PIPE
1250
  proto_build(&proto_pipe);
1251
#endif
1252
#ifdef CONFIG_BGP
1253
  proto_build(&proto_bgp);
1254
#endif
1255
#ifdef CONFIG_BFD
1256
  proto_build(&proto_bfd);
1257
  bfd_init_all();
1258
#endif
1259

    
1260
  proto_pool = rp_new(&root_pool, "Protocols");
1261
  proto_shutdown_timer = tm_new(proto_pool);
1262
  proto_shutdown_timer->hook = proto_shutdown_loop;
1263
}
1264

    
1265

    
1266
/* Temporary hack to propagate restart to BGP */
1267
int proto_restart;
1268

    
1269
static void
1270
proto_shutdown_loop(struct timer *t UNUSED)
1271
{
1272
  struct proto *p, *p_next;
1273

    
1274
  WALK_LIST_DELSAFE(p, p_next, proto_list)
1275
    if (p->down_sched)
1276
    {
1277
      proto_restart = (p->down_sched == PDS_RESTART);
1278

    
1279
      p->disabled = 1;
1280
      proto_rethink_goal(p);
1281
      if (proto_restart)
1282
      {
1283
        p->disabled = 0;
1284
        proto_rethink_goal(p);
1285
      }
1286
    }
1287
}
1288

    
1289
static inline void
1290
proto_schedule_down(struct proto *p, byte restart, byte code)
1291
{
1292
  /* Does not work for other states (even PS_START) */
1293
  ASSERT(p->proto_state == PS_UP);
1294

    
1295
  /* Scheduled restart may change to shutdown, but not otherwise */
1296
  if (p->down_sched == PDS_DISABLE)
1297
    return;
1298

    
1299
  p->down_sched = restart ? PDS_RESTART : PDS_DISABLE;
1300
  p->down_code = code;
1301
  tm_start_max(proto_shutdown_timer, restart ? 2 : 0);
1302
}
1303

    
1304

    
1305
static const char *
1306
channel_limit_name(struct channel_limit *l)
1307
{
1308
  const char *actions[] = {
1309
    [PLA_WARN] = "warn",
1310
    [PLA_BLOCK] = "block",
1311
    [PLA_RESTART] = "restart",
1312
    [PLA_DISABLE] = "disable",
1313
  };
1314

    
1315
  return actions[l->action];
1316
}
1317

    
1318
/**
1319
 * channel_notify_limit: notify about limit hit and take appropriate action
1320
 * @c: channel
1321
 * @l: limit being hit
1322
 * @dir: limit direction (PLD_*)
1323
 * @rt_count: the number of routes
1324
 *
1325
 * The function is called by the route processing core when limit @l
1326
 * is breached. It activates the limit and tooks appropriate action
1327
 * according to @l->action.
1328
 */
1329
void
1330
channel_notify_limit(struct channel *c, struct channel_limit *l, int dir, u32 rt_count)
1331
{
1332
  const char *dir_name[PLD_MAX] = { "receive", "import" , "export" };
1333
  const byte dir_down[PLD_MAX] = { PDC_RX_LIMIT_HIT, PDC_IN_LIMIT_HIT, PDC_OUT_LIMIT_HIT };
1334
  struct proto *p = c->proto;
1335

    
1336
  if (l->state == PLS_BLOCKED)
1337
    return;
1338

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

    
1344
  switch (l->action)
1345
  {
1346
  case PLA_WARN:
1347
    l->state = PLS_ACTIVE;
1348
    break;
1349

    
1350
  case PLA_BLOCK:
1351
    l->state = PLS_BLOCKED;
1352
    break;
1353

    
1354
  case PLA_RESTART:
1355
  case PLA_DISABLE:
1356
    l->state = PLS_BLOCKED;
1357
    if (p->proto_state == PS_UP)
1358
      proto_schedule_down(p, l->action == PLA_RESTART, dir_down[dir]);
1359
    break;
1360
  }
1361
}
1362

    
1363
static void
1364
channel_verify_limits(struct channel *c)
1365
{
1366
  struct channel_limit *l;
1367
  u32 all_routes = c->stats.imp_routes + c->stats.filt_routes;
1368

    
1369
  l = &c->rx_limit;
1370
  if (l->action && (all_routes > l->limit))
1371
    channel_notify_limit(c, l, PLD_RX, all_routes);
1372

    
1373
  l = &c->in_limit;
1374
  if (l->action && (c->stats.imp_routes > l->limit))
1375
    channel_notify_limit(c, l, PLD_IN, c->stats.imp_routes);
1376

    
1377
  l = &c->out_limit;
1378
  if (l->action && (c->stats.exp_routes > l->limit))
1379
    channel_notify_limit(c, l, PLD_OUT, c->stats.exp_routes);
1380
}
1381

    
1382
static inline void
1383
channel_reset_limit(struct channel_limit *l)
1384
{
1385
  if (l->action)
1386
    l->state = PLS_INITIAL;
1387
}
1388

    
1389
static inline void
1390
proto_do_start(struct proto *p)
1391
{
1392
  p->active = 1;
1393
  p->do_start = 1;
1394
  ev_schedule(p->event);
1395
}
1396

    
1397
static void
1398
proto_do_up(struct proto *p)
1399
{
1400
  if (!p->main_source)
1401
  {
1402
    p->main_source = rt_get_source(p, 0);
1403
    rt_lock_source(p->main_source);
1404
  }
1405

    
1406
  proto_start_channels(p);
1407
}
1408

    
1409
static inline void
1410
proto_do_pause(struct proto *p)
1411
{
1412
  proto_pause_channels(p);
1413
}
1414

    
1415
static void
1416
proto_do_stop(struct proto *p)
1417
{
1418
  p->down_sched = 0;
1419
  p->gr_recovery = 0;
1420

    
1421
  p->do_stop = 1;
1422
  ev_schedule(p->event);
1423

    
1424
  if (p->main_source)
1425
  {
1426
    rt_unlock_source(p->main_source);
1427
    p->main_source = NULL;
1428
  }
1429

    
1430
  proto_stop_channels(p);
1431
}
1432

    
1433
static void
1434
proto_do_down(struct proto *p)
1435
{
1436
  p->down_code = 0;
1437
  neigh_prune();
1438
  rfree(p->pool);
1439
  p->pool = NULL;
1440

    
1441
  /* Shutdown is finished in the protocol event */
1442
  if (proto_is_done(p))
1443
    ev_schedule(p->event);
1444
}
1445

    
1446

    
1447

    
1448
/**
1449
 * proto_notify_state - notify core about protocol state change
1450
 * @p: protocol the state of which has changed
1451
 * @ps: the new status
1452
 *
1453
 * Whenever a state of a protocol changes due to some event internal
1454
 * to the protocol (i.e., not inside a start() or shutdown() hook),
1455
 * it should immediately notify the core about the change by calling
1456
 * proto_notify_state() which will write the new state to the &proto
1457
 * structure and take all the actions necessary to adapt to the new
1458
 * state. State change to PS_DOWN immediately frees resources of protocol
1459
 * and might execute start callback of protocol; therefore,
1460
 * it should be used at tail positions of protocol callbacks.
1461
 */
1462
void
1463
proto_notify_state(struct proto *p, uint state)
1464
{
1465
  uint ps = p->proto_state;
1466

    
1467
  DBG("%s reporting state transition %s -> %s\n", p->name, p_states[ps], p_states[state]);
1468
  if (state == ps)
1469
    return;
1470

    
1471
  p->proto_state = state;
1472
  p->last_state_change = now;
1473

    
1474
  switch (state)
1475
  {
1476
  case PS_START:
1477
    ASSERT(ps == PS_DOWN || ps == PS_UP);
1478

    
1479
    if (ps == PS_DOWN)
1480
      proto_do_start(p);
1481
    else
1482
      proto_do_pause(p);
1483
    break;
1484

    
1485
  case PS_UP:
1486
    ASSERT(ps == PS_DOWN || ps == PS_START);
1487

    
1488
    if (ps == PS_DOWN)
1489
      proto_do_start(p);
1490

    
1491
    proto_do_up(p);
1492
    break;
1493

    
1494
  case PS_STOP:
1495
    ASSERT(ps == PS_START || ps == PS_UP);
1496

    
1497
    proto_do_stop(p);
1498
    break;
1499

    
1500
  case PS_DOWN:
1501
    if (ps != PS_STOP)
1502
      proto_do_stop(p);
1503

    
1504
    proto_do_down(p);
1505
    break;
1506

    
1507
  default:
1508
    bug("%s: Invalid state %d", p->name, ps);
1509
  }
1510

    
1511
  proto_log_state_change(p);
1512
}
1513

    
1514
/*
1515
 *  CLI Commands
1516
 */
1517

    
1518
static char *
1519
proto_state_name(struct proto *p)
1520
{
1521
  switch (p->proto_state)
1522
  {
1523
  case PS_DOWN:                return p->active ? "flush" : "down";
1524
  case PS_START:        return "start";
1525
  case PS_UP:                return "up";
1526
  case PS_STOP:                return "stop";
1527
  default:                return "???";
1528
  }
1529
}
1530

    
1531
static void
1532
channel_show_stats(struct channel *c)
1533
{
1534
  struct proto_stats *s = &c->stats;
1535

    
1536
  if (c->in_keep_filtered)
1537
    cli_msg(-1006, "    Routes:         %u imported, %u filtered, %u exported",
1538
            s->imp_routes, s->filt_routes, s->exp_routes);
1539
  else
1540
    cli_msg(-1006, "    Routes:         %u imported, %u exported",
1541
            s->imp_routes, s->exp_routes);
1542

    
1543
  cli_msg(-1006, "    Route change stats:     received   rejected   filtered    ignored   accepted");
1544
  cli_msg(-1006, "      Import updates:     %10u %10u %10u %10u %10u",
1545
          s->imp_updates_received, s->imp_updates_invalid,
1546
          s->imp_updates_filtered, s->imp_updates_ignored,
1547
          s->imp_updates_accepted);
1548
  cli_msg(-1006, "      Import withdraws:   %10u %10u        --- %10u %10u",
1549
          s->imp_withdraws_received, s->imp_withdraws_invalid,
1550
          s->imp_withdraws_ignored, s->imp_withdraws_accepted);
1551
  cli_msg(-1006, "      Export updates:     %10u %10u %10u        --- %10u",
1552
          s->exp_updates_received, s->exp_updates_rejected,
1553
          s->exp_updates_filtered, s->exp_updates_accepted);
1554
  cli_msg(-1006, "      Export withdraws:   %10u        ---        ---        --- %10u",
1555
          s->exp_withdraws_received, s->exp_withdraws_accepted);
1556
}
1557

    
1558
void
1559
channel_show_limit(struct channel_limit *l, const char *dsc)
1560
{
1561
  if (!l->action)
1562
    return;
1563

    
1564
  cli_msg(-1006, "    %-16s%d%s", dsc, l->limit, l->state ? " [HIT]" : "");
1565
  cli_msg(-1006, "      Action:       %s", channel_limit_name(l));
1566
}
1567

    
1568
void
1569
channel_show_info(struct channel *c)
1570
{
1571
  cli_msg(-1006, "  Channel %s", c->name);
1572
  cli_msg(-1006, "    Table:          %s", c->table->name);
1573
  cli_msg(-1006, "    Preference:     %d", c->preference);
1574
  cli_msg(-1006, "    Input filter:   %s", filter_name(c->in_filter));
1575
  cli_msg(-1006, "    Output filter:  %s", filter_name(c->out_filter));
1576

    
1577
  if (graceful_restart_state == GRS_ACTIVE)
1578
    cli_msg(-1006, "    GR recovery:   %s%s",
1579
            c->gr_lock ? " pending" : "",
1580
            c->gr_wait ? " waiting" : "");
1581

    
1582
  channel_show_limit(&c->rx_limit, "Receive limit:");
1583
  channel_show_limit(&c->in_limit, "Import limit:");
1584
  channel_show_limit(&c->out_limit, "Export limit:");
1585

    
1586
  if (c->channel_state != CS_DOWN)
1587
    channel_show_stats(c);
1588
}
1589

    
1590
void
1591
proto_cmd_show(struct proto *p, uint verbose, int cnt)
1592
{
1593
  byte buf[256], tbuf[TM_DATETIME_BUFFER_SIZE];
1594

    
1595
  /* First protocol - show header */
1596
  if (!cnt)
1597
    cli_msg(-2002, "name     proto    table    state  since       info");
1598

    
1599
  buf[0] = 0;
1600
  if (p->proto->get_status)
1601
    p->proto->get_status(p, buf);
1602
  tm_format_datetime(tbuf, &config->tf_proto, p->last_state_change);
1603
  cli_msg(-1002, "%-8s %-8s %-8s %-5s  %-10s  %s",
1604
          p->name,
1605
          p->proto->name,
1606
          p->main_channel ? p->main_channel->table->name : "---",
1607
          proto_state_name(p),
1608
          tbuf,
1609
          buf);
1610

    
1611
  if (verbose)
1612
  {
1613
    if (p->cf->dsc)
1614
      cli_msg(-1006, "  Description:    %s", p->cf->dsc);
1615
    if (p->cf->router_id)
1616
      cli_msg(-1006, "  Router ID:      %R", p->cf->router_id);
1617

    
1618
    if (p->proto->show_proto_info)
1619
      p->proto->show_proto_info(p);
1620
    else
1621
    {
1622
      struct channel *c;
1623
      WALK_LIST(c, p->channels)
1624
        channel_show_info(c);
1625
    }
1626

    
1627
    cli_msg(-1006, "");
1628
  }
1629
}
1630

    
1631
void
1632
proto_cmd_disable(struct proto *p, uint arg UNUSED, int cnt UNUSED)
1633
{
1634
  if (p->disabled)
1635
  {
1636
    cli_msg(-8, "%s: already disabled", p->name);
1637
    return;
1638
  }
1639

    
1640
  log(L_INFO "Disabling protocol %s", p->name);
1641
  p->disabled = 1;
1642
  p->down_code = PDC_CMD_DISABLE;
1643
  proto_rethink_goal(p);
1644
  cli_msg(-9, "%s: disabled", p->name);
1645
}
1646

    
1647
void
1648
proto_cmd_enable(struct proto *p, uint arg UNUSED, int cnt UNUSED)
1649
{
1650
  if (!p->disabled)
1651
  {
1652
    cli_msg(-10, "%s: already enabled", p->name);
1653
    return;
1654
  }
1655

    
1656
  log(L_INFO "Enabling protocol %s", p->name);
1657
  p->disabled = 0;
1658
  proto_rethink_goal(p);
1659
  cli_msg(-11, "%s: enabled", p->name);
1660
}
1661

    
1662
void
1663
proto_cmd_restart(struct proto *p, uint arg UNUSED, int cnt UNUSED)
1664
{
1665
  if (p->disabled)
1666
  {
1667
    cli_msg(-8, "%s: already disabled", p->name);
1668
    return;
1669
  }
1670

    
1671
  log(L_INFO "Restarting protocol %s", p->name);
1672
  p->disabled = 1;
1673
  p->down_code = PDC_CMD_RESTART;
1674
  proto_rethink_goal(p);
1675
  p->disabled = 0;
1676
  proto_rethink_goal(p);
1677
  cli_msg(-12, "%s: restarted", p->name);
1678
}
1679

    
1680
void
1681
proto_cmd_reload(struct proto *p, uint dir, int cnt UNUSED)
1682
{
1683
  struct channel *c;
1684

    
1685
  if (p->disabled)
1686
  {
1687
    cli_msg(-8, "%s: already disabled", p->name);
1688
    return;
1689
  }
1690

    
1691
  /* If the protocol in not UP, it has no routes */
1692
  if (p->proto_state != PS_UP)
1693
    return;
1694

    
1695
  /* All channels must support reload */
1696
  if (dir != CMD_RELOAD_OUT)
1697
    WALK_LIST(c, p->channels)
1698
      if (!channel_reloadable(c))
1699
      {
1700
        cli_msg(-8006, "%s: reload failed", p->name);
1701
        return;
1702
      }
1703

    
1704
  log(L_INFO "Reloading protocol %s", p->name);
1705

    
1706
  /* re-importing routes */
1707
  if (dir != CMD_RELOAD_OUT)
1708
    WALK_LIST(c, p->channels)
1709
      channel_request_reload(c);
1710

    
1711
  /* re-exporting routes */
1712
  if (dir != CMD_RELOAD_IN)
1713
    WALK_LIST(c, p->channels)
1714
      channel_request_feeding(c);
1715

    
1716
  cli_msg(-15, "%s: reloading", p->name);
1717
}
1718

    
1719
void
1720
proto_cmd_debug(struct proto *p, uint mask, int cnt UNUSED)
1721
{
1722
  p->debug = mask;
1723
}
1724

    
1725
void
1726
proto_cmd_mrtdump(struct proto *p, uint mask, int cnt UNUSED)
1727
{
1728
  p->mrtdump = mask;
1729
}
1730

    
1731
static void
1732
proto_apply_cmd_symbol(struct symbol *s, void (* cmd)(struct proto *, uint, int), uint arg)
1733
{
1734
  if (s->class != SYM_PROTO)
1735
  {
1736
    cli_msg(9002, "%s is not a protocol", s->name);
1737
    return;
1738
  }
1739

    
1740
  cmd(((struct proto_config *)s->def)->proto, arg, 0);
1741
  cli_msg(0, "");
1742
}
1743

    
1744
static void
1745
proto_apply_cmd_patt(char *patt, void (* cmd)(struct proto *, uint, int), uint arg)
1746
{
1747
  struct proto *p;
1748
  int cnt = 0;
1749

    
1750
  WALK_LIST(p, proto_list)
1751
    if (!patt || patmatch(patt, p->name))
1752
      cmd(p, arg, cnt++);
1753

    
1754
  if (!cnt)
1755
    cli_msg(8003, "No protocols match");
1756
  else
1757
    cli_msg(0, "");
1758
}
1759

    
1760
void
1761
proto_apply_cmd(struct proto_spec ps, void (* cmd)(struct proto *, uint, int),
1762
                int restricted, uint arg)
1763
{
1764
  if (restricted && cli_access_restricted())
1765
    return;
1766

    
1767
  if (ps.patt)
1768
    proto_apply_cmd_patt(ps.ptr, cmd, arg);
1769
  else
1770
    proto_apply_cmd_symbol(ps.ptr, cmd, arg);
1771
}
1772

    
1773
struct proto *
1774
proto_get_named(struct symbol *sym, struct protocol *pr)
1775
{
1776
  struct proto *p, *q;
1777

    
1778
  if (sym)
1779
  {
1780
    if (sym->class != SYM_PROTO)
1781
      cf_error("%s: Not a protocol", sym->name);
1782

    
1783
    p = ((struct proto_config *) sym->def)->proto;
1784
    if (!p || p->proto != pr)
1785
      cf_error("%s: Not a %s protocol", sym->name, pr->name);
1786
  }
1787
  else
1788
  {
1789
    p = NULL;
1790
    WALK_LIST(q, proto_list)
1791
      if ((q->proto == pr) && (q->proto_state != PS_DOWN))
1792
      {
1793
        if (p)
1794
          cf_error("There are multiple %s protocols running", pr->name);
1795
        p = q;
1796
      }
1797
    if (!p)
1798
      cf_error("There is no %s protocol running", pr->name);
1799
  }
1800

    
1801
  return p;
1802
}