Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (21.8 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
static pool *proto_pool;
24

    
25
static list protocol_list;
26
static list proto_list;
27

    
28
#define WALK_PROTO_LIST(p) do {                                                        \
29
        node *nn;                                                                \
30
        WALK_LIST(nn, proto_list) {                                                \
31
                struct proto *p = SKIP_BACK(struct proto, glob_node, nn);
32
#define WALK_PROTO_LIST_END } } while(0)
33

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

    
36
list active_proto_list;
37
static list inactive_proto_list;
38
static list initial_proto_list;
39
static list flush_proto_list;
40

    
41
static event *proto_flush_event;
42

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

    
46
static void proto_flush_all(void *);
47
static void proto_rethink_goal(struct proto *p);
48
static char *proto_state_name(struct proto *p);
49

    
50
static void
51
proto_enqueue(list *l, struct proto *p)
52
{
53
  add_tail(l, &p->n);
54
  p->last_state_change = now;
55
}
56

    
57
static void
58
proto_relink(struct proto *p)
59
{
60
  list *l;
61

    
62
  if (p->debug & D_STATES)
63
    {
64
      char *name = proto_state_name(p);
65
      if (name != p->last_state_name_announced)
66
        {
67
          p->last_state_name_announced = name;
68
          PD(p, "State changed to %s", proto_state_name(p));
69
        }
70
    }
71
  else
72
    p->last_state_name_announced = NULL;
73
  rem_node(&p->n);
74
  switch (p->core_state)
75
    {
76
    case FS_HAPPY:
77
      l = &active_proto_list;
78
      break;
79
    case FS_FLUSHING:
80
      l = &flush_proto_list;
81
      break;
82
    default:
83
      l = &inactive_proto_list;
84
    }
85
  proto_enqueue(l, p);
86
}
87

    
88
/**
89
 * proto_new - create a new protocol instance
90
 * @c: protocol configuration
91
 * @size: size of protocol data structure (each protocol instance is represented by
92
 * a structure starting with generic part [struct &proto] and continued
93
 * with data specific to the protocol)
94
 *
95
 * When a new configuration has been read in, the core code starts
96
 * initializing all the protocol instances configured by calling their
97
 * init() hooks with the corresponding instance configuration. The initialization
98
 * code of the protocol is expected to create a new instance according to the
99
 * configuration by calling this function and then modifying the default settings
100
 * to values wanted by the protocol.
101
 */
102
void *
103
proto_new(struct proto_config *c, unsigned size)
104
{
105
  struct protocol *pr = c->protocol;
106
  struct proto *p = mb_allocz(proto_pool, size);
107

    
108
  p->cf = c;
109
  p->debug = c->debug;
110
  p->name = c->name;
111
  p->preference = c->preference;
112
  p->disabled = c->disabled;
113
  p->proto = pr;
114
  p->table = c->table->table;
115
  p->in_filter = c->in_filter;
116
  p->out_filter = c->out_filter;
117
  p->min_scope = SCOPE_SITE;
118
  p->hash_key = random_u32();
119
  c->proto = p;
120
  return p;
121
}
122

    
123
static void
124
proto_init_instance(struct proto *p)
125
{
126
  /* Here we cannot use p->cf->name since it won't survive reconfiguration */
127
  p->pool = rp_new(proto_pool, p->proto->name);
128
  p->attn = ev_new(p->pool);
129
  p->attn->data = p;
130
  rt_lock_table(p->table);
131
}
132

    
133
/**
134
 * proto_add_announce_hook - connect protocol to a routing table
135
 * @p: protocol instance
136
 * @t: routing table to connect to
137
 *
138
 * This function creates a connection between the protocol instance @p
139
 * and the routing table @t, making the protocol hear all changes in
140
 * the table.
141
 *
142
 * Unless you want to listen to multiple routing tables (as the Pipe
143
 * protocol does), you needn't to worry about this function since the
144
 * connection to the protocol's primary routing table is initialized
145
 * automatically by the core code.
146
 */
147
struct announce_hook *
148
proto_add_announce_hook(struct proto *p, struct rtable *t)
149
{
150
  struct announce_hook *h;
151

    
152
  if (!p->rt_notify)
153
    return NULL;
154
  DBG("Connecting protocol %s to table %s\n", p->name, t->name);
155
  PD(p, "Connected to table %s", t->name);
156
  h = mb_alloc(p->pool, sizeof(struct announce_hook));
157
  h->table = t;
158
  h->proto = p;
159
  h->next = p->ahooks;
160
  p->ahooks = h;
161
  add_tail(&t->hooks, &h->n);
162
  return h;
163
}
164

    
165
static void
166
proto_flush_hooks(struct proto *p)
167
{
168
  struct announce_hook *h;
169

    
170
  for(h=p->ahooks; h; h=h->next)
171
    rem_node(&h->n);
172
  p->ahooks = NULL;
173
}
174

    
175
/**
176
 * proto_config_new - create a new protocol configuration
177
 * @pr: protocol the configuration will belong to
178
 * @size: size of the structure including generic data
179
 *
180
 * Whenever the configuration file says that a new instance
181
 * of a routing protocol should be created, the parser calls
182
 * proto_config_new() to create a configuration entry for this
183
 * instance (a structure staring with the &proto_config header
184
 * containing all the generic items followed by protocol-specific
185
 * ones). Also, the configuration entry gets added to the list
186
 * of protocol instances kept in the configuration.
187
 */
188
void *
189
proto_config_new(struct protocol *pr, unsigned size)
190
{
191
  struct proto_config *c = cfg_allocz(size);
192

    
193
  add_tail(&new_config->protos, &c->n);
194
  c->global = new_config;
195
  c->protocol = pr;
196
  c->name = pr->name;
197
  c->out_filter = FILTER_REJECT;
198
  c->table = c->global->master_rtc;
199
  c->debug = new_config->proto_default_debug;
200
  return c;
201
}
202

    
203
/**
204
 * protos_preconfig - pre-configuration processing
205
 * @c: new configuration
206
 *
207
 * This function calls the preconfig() hooks of all routing
208
 * protocols available to prepare them for reading of the new
209
 * configuration.
210
 */
211
void
212
protos_preconfig(struct config *c)
213
{
214
  struct protocol *p;
215

    
216
  init_list(&c->protos);
217
  DBG("Protocol preconfig:");
218
  WALK_LIST(p, protocol_list)
219
    {
220
      DBG(" %s", p->name);
221
      p->name_counter = 0;
222
      if (p->preconfig)
223
        p->preconfig(p, c);
224
    }
225
  DBG("\n");
226
}
227

    
228
/**
229
 * protos_postconfig - post-configuration processing
230
 * @c: new configuration
231
 *
232
 * This function calls the postconfig() hooks of all protocol
233
 * instances specified in configuration @c.
234
 */
235
void
236
protos_postconfig(struct config *c)
237
{
238
  struct proto_config *x;
239
  struct protocol *p;
240

    
241
  DBG("Protocol postconfig:");
242
  WALK_LIST(x, c->protos)
243
    {
244
      DBG(" %s", x->name);
245
      p = x->protocol;
246
      if (p->postconfig)
247
        p->postconfig(x);
248
    }
249
  DBG("\n");
250
}
251

    
252
static struct proto *
253
proto_init(struct proto_config *c)
254
{
255
  struct protocol *p = c->protocol;
256
  struct proto *q = p->init(c);
257

    
258
  q->proto_state = PS_DOWN;
259
  q->core_state = FS_HUNGRY;
260
  proto_enqueue(&initial_proto_list, q);
261
  add_tail(&proto_list, &q->glob_node);
262
  PD(q, "Initializing%s", q->disabled ? " [disabled]" : "");
263
  return q;
264
}
265

    
266
/**
267
 * protos_commit - commit new protocol configuration
268
 * @new: new configuration
269
 * @old: old configuration or %NULL if it's boot time config
270
 * @force_reconfig: force restart of all protocols (used for example
271
 * when the router ID changes)
272
 * @type: type of reconfiguration (RECONFIG_SOFT or RECONFIG_HARD)
273
 *
274
 * Scan differences between @old and @new configuration and adjust all
275
 * protocol instances to conform to the new configuration.
276
 *
277
 * When a protocol exists in the new configuration, but it doesn't in the
278
 * original one, it's immediately started. When a collision with the other
279
 * running protocol would arise, the new protocol will be temporarily stopped
280
 * by the locking mechanism.
281
 *
282
 * When a protocol exists in the old configuration, but it doesn't in the
283
 * new one, it's shut down and deleted after the shutdown completes.
284
 *
285
 * When a protocol exists in both configurations, the core decides
286
 * whether it's possible to reconfigure it dynamically - it checks all
287
 * the core properties of the protocol (changes in filters are ignored
288
 * if type is RECONFIG_SOFT) and if they match, it asks the
289
 * reconfigure() hook of the protocol to see if the protocol is able
290
 * to switch to the new configuration.  If it isn't possible, the
291
 * protocol is shut down and a new instance is started with the new
292
 * configuration after the shutdown is completed.
293
 */
294
void
295
protos_commit(struct config *new, struct config *old, int force_reconfig, int type)
296
{
297
  struct proto_config *oc, *nc;
298
  struct proto *p, *n;
299

    
300
  DBG("protos_commit:\n");
301
  if (old)
302
    {
303
      WALK_LIST(oc, old->protos)
304
        {
305
          struct proto *p = oc->proto;
306
          struct symbol *sym = cf_find_symbol(oc->name);
307
          if (sym && sym->class == SYM_PROTO && !new->shutdown)
308
            {
309
              /* Found match, let's check if we can smoothly switch to new configuration */
310
              /* No need to check description */
311
              nc = sym->def;
312
              if (!force_reconfig
313
                  && nc->protocol == oc->protocol
314
                  && nc->preference == oc->preference
315
                  && nc->disabled == oc->disabled
316
                  && nc->table->table == oc->table->table
317
                  && proto_get_router_id(nc) == proto_get_router_id(oc)
318
                  && ((type == RECONFIG_SOFT) || filter_same(nc->in_filter, oc->in_filter))
319
                  && ((type == RECONFIG_SOFT) || filter_same(nc->out_filter, oc->out_filter))
320
                  && p->proto_state != PS_DOWN)
321
                {
322
                  /* Generic attributes match, try converting them and then ask the protocol */
323
                  p->debug = nc->debug;
324
                  if (p->proto->reconfigure && p->proto->reconfigure(p, nc))
325
                    {
326
                      DBG("\t%s: same\n", oc->name);
327
                      PD(p, "Reconfigured");
328
                      p->cf = nc;
329
                      p->name = nc->name;
330
                      p->in_filter = nc->in_filter;
331
                      p->out_filter = nc->out_filter;
332
                      nc->proto = p;
333
                      continue;
334
                    }
335
                }
336
              /* Unsuccessful, force reconfig */
337
              DBG("\t%s: power cycling\n", oc->name);
338
              PD(p, "Reconfiguration failed, restarting");
339
              p->cf_new = nc;
340
              nc->proto = p;
341
            }
342
          else
343
            {
344
              DBG("\t%s: deleting\n", oc->name);
345
              PD(p, "Unconfigured");
346
              p->cf_new = NULL;
347
            }
348
          p->reconfiguring = 1;
349
          config_add_obstacle(old);
350
          proto_rethink_goal(p);
351
        }
352
    }
353

    
354
  WALK_LIST(nc, new->protos)
355
    if (!nc->proto)
356
      {
357
        DBG("\t%s: adding\n", nc->name);
358
        proto_init(nc);
359
      }
360
  DBG("\tdone\n");
361

    
362
  DBG("Protocol start\n");
363
  WALK_LIST_DELSAFE(p, n, initial_proto_list)
364
    proto_rethink_goal(p);
365
}
366

    
367
static void
368
proto_rethink_goal(struct proto *p)
369
{
370
  struct protocol *q;
371

    
372
  if (p->reconfiguring && p->core_state == FS_HUNGRY && p->proto_state == PS_DOWN)
373
    {
374
      struct proto_config *nc = p->cf_new;
375
      DBG("%s has shut down for reconfiguration\n", p->name);
376
      config_del_obstacle(p->cf->global);
377
      rem_node(&p->n);
378
      rem_node(&p->glob_node);
379
      mb_free(p);
380
      if (!nc)
381
        return;
382
      p = proto_init(nc);
383
    }
384

    
385
  /* Determine what state we want to reach */
386
  if (p->disabled || p->reconfiguring)
387
    {
388
      p->core_goal = FS_HUNGRY;
389
      if (p->core_state == FS_HUNGRY && p->proto_state == PS_DOWN)
390
        return;
391
    }
392
  else
393
    {
394
      p->core_goal = FS_HAPPY;
395
      if (p->core_state == FS_HAPPY && p->proto_state == PS_UP)
396
        return;
397
    }
398

    
399
  q = p->proto;
400
  if (p->core_goal == FS_HAPPY)                /* Going up */
401
    {
402
      if (p->core_state == FS_HUNGRY && p->proto_state == PS_DOWN)
403
        {
404
          DBG("Kicking %s up\n", p->name);
405
          PD(p, "Starting");
406
          proto_init_instance(p);
407
          proto_notify_state(p, (q->start ? q->start(p) : PS_UP));
408
        }
409
    }
410
  else                                         /* Going down */
411
    {
412
      if (p->proto_state == PS_START || p->proto_state == PS_UP)
413
        {
414
          DBG("Kicking %s down\n", p->name);
415
          PD(p, "Shutting down");
416
          proto_notify_state(p, (q->shutdown ? q->shutdown(p) : PS_DOWN));
417
        }
418
    }
419
}
420

    
421
/**
422
 * protos_dump_all - dump status of all protocols
423
 *
424
 * This function dumps status of all existing protocol instances to the
425
 * debug output. It involves printing of general status information
426
 * such as protocol states, its position on the protocol lists
427
 * and also calling of a dump() hook of the protocol to print
428
 * the internals.
429
 */
430
void
431
protos_dump_all(void)
432
{
433
  struct proto *p;
434

    
435
  debug("Protocols:\n");
436

    
437
  WALK_LIST(p, active_proto_list)
438
    {
439
      debug("  protocol %s state %s/%s\n", p->name,
440
            p_states[p->proto_state], c_states[p->core_state]);
441
      if (p->in_filter)
442
        debug("\tInput filter: %s\n", filter_name(p->in_filter));
443
      if (p->out_filter != FILTER_REJECT)
444
        debug("\tOutput filter: %s\n", filter_name(p->out_filter));
445
      if (p->disabled)
446
        debug("\tDISABLED\n");
447
      else if (p->proto->dump)
448
        p->proto->dump(p);
449
    }
450
  WALK_LIST(p, inactive_proto_list)
451
    debug("  inactive %s: state %s/%s\n", p->name, p_states[p->proto_state], c_states[p->core_state]);
452
  WALK_LIST(p, initial_proto_list)
453
    debug("  initial %s\n", p->name);
454
  WALK_LIST(p, flush_proto_list)
455
    debug("  flushing %s\n", p->name);
456
}
457

    
458
/**
459
 * proto_build - make a single protocol available
460
 * @p: the protocol
461
 *
462
 * After the platform specific initialization code uses protos_build()
463
 * to add all the standard protocols, it should call proto_build() for
464
 * all platform specific protocols to inform the core that they exist.
465
 */
466
void
467
proto_build(struct protocol *p)
468
{
469
  add_tail(&protocol_list, &p->n);
470
  if (p->attr_class)
471
    {
472
      ASSERT(!attr_class_to_protocol[p->attr_class]);
473
      attr_class_to_protocol[p->attr_class] = p;
474
    }
475
}
476

    
477
/**
478
 * protos_build - build a protocol list
479
 *
480
 * This function is called during BIRD startup to insert
481
 * all standard protocols to the global protocol list. Insertion
482
 * of platform specific protocols (such as the kernel syncer)
483
 * is in the domain of competence of the platform dependent
484
 * startup code.
485
 */
486
void
487
protos_build(void)
488
{
489
  init_list(&protocol_list);
490
  init_list(&proto_list);
491
  init_list(&active_proto_list);
492
  init_list(&inactive_proto_list);
493
  init_list(&initial_proto_list);
494
  init_list(&flush_proto_list);
495
  proto_build(&proto_device);
496
#ifdef CONFIG_RIP
497
  proto_build(&proto_rip);
498
#endif
499
#ifdef CONFIG_STATIC
500
  proto_build(&proto_static);
501
#endif
502
#ifdef CONFIG_OSPF
503
  proto_build(&proto_ospf);
504
#endif
505
#ifdef CONFIG_PIPE
506
  proto_build(&proto_pipe);
507
#endif
508
#ifdef CONFIG_BGP
509
  proto_build(&proto_bgp);
510
#endif
511
  proto_pool = rp_new(&root_pool, "Protocols");
512
  proto_flush_event = ev_new(proto_pool);
513
  proto_flush_event->hook = proto_flush_all;
514
}
515

    
516
static void
517
proto_fell_down(struct proto *p)
518
{
519
  DBG("Protocol %s down\n", p->name);
520

    
521
  if (p->stats.imp_routes != 0)
522
    log(L_ERR "Protocol %s is down but still has %d routes", p->name, p->stats.imp_routes);
523

    
524
  bzero(&p->stats, sizeof(struct proto_stats));
525
  rt_unlock_table(p->table);
526
  proto_rethink_goal(p);
527
}
528

    
529
static void
530
proto_feed_more(void *P)
531
{
532
  struct proto *p = P;
533

    
534
  if (p->core_state != FS_FEEDING)
535
    return;
536

    
537
  DBG("Feeding protocol %s continued\n", p->name);
538
  if (rt_feed_baby(p))
539
    {
540
      p->core_state = FS_HAPPY;
541
      proto_relink(p);
542
      DBG("Protocol %s up and running\n", p->name);
543
    }
544
  else
545
    {
546
      p->attn->hook = proto_feed_more;
547
      ev_schedule(p->attn);                /* Will continue later... */
548
    }
549
}
550

    
551
static void
552
proto_feed(void *P)
553
{
554
  struct proto *p = P;
555

    
556
  if (p->core_state != FS_FEEDING)
557
    return;
558

    
559
  DBG("Feeding protocol %s\n", p->name);
560
  proto_add_announce_hook(p, p->table);
561
  if_feed_baby(p);
562
  proto_feed_more(P);
563
}
564

    
565
static void
566
proto_schedule_flush(struct proto *p)
567
{
568
  /* Need to abort feeding */
569
  if (p->core_state == FS_FEEDING)
570
    rt_feed_baby_abort(p);
571

    
572
  DBG("%s: Scheduling flush\n", p->name);
573
  p->core_state = FS_FLUSHING;
574
  proto_relink(p);
575
  proto_flush_hooks(p);
576
  ev_schedule(proto_flush_event);
577
}
578

    
579
static void
580
proto_schedule_feed(struct proto *p)
581
{
582
  DBG("%s: Scheduling meal\n", p->name);
583
  p->core_state = FS_FEEDING;
584
  proto_relink(p);
585
  p->attn->hook = proto_feed;
586
  ev_schedule(p->attn);
587
}
588

    
589
/**
590
 * proto_notify_state - notify core about protocol state change
591
 * @p: protocol the state of which has changed
592
 * @ps: the new status
593
 *
594
 * Whenever a state of a protocol changes due to some event internal
595
 * to the protocol (i.e., not inside a start() or shutdown() hook),
596
 * it should immediately notify the core about the change by calling
597
 * proto_notify_state() which will write the new state to the &proto
598
 * structure and take all the actions necessary to adapt to the new
599
 * state. State change to PS_DOWN immediately frees resources of protocol
600
 * and might execute start callback of protocol; therefore,
601
 * it should be used at tail positions of protocol callbacks.
602
 */
603
void
604
proto_notify_state(struct proto *p, unsigned ps)
605
{
606
  unsigned ops = p->proto_state;
607
  unsigned cs = p->core_state;
608

    
609
  DBG("%s reporting state transition %s/%s -> */%s\n", p->name, c_states[cs], p_states[ops], p_states[ps]);
610
  if (ops == ps)
611
    return;
612

    
613
  p->proto_state = ps;
614

    
615
  switch (ps)
616
    {
617
    case PS_DOWN:
618
      if ((cs = FS_FEEDING) || (cs == FS_HAPPY))
619
        proto_schedule_flush(p);
620

    
621
      neigh_prune(); // FIXME convert neighbors to resource?
622
      rfree(p->pool);
623
      p->pool = NULL;
624

    
625
      if (cs == FS_HUNGRY)                /* Shutdown finished */
626
        {
627
          proto_fell_down(p);
628
          return;                        /* The protocol might have ceased to exist */
629
        }
630
      break;
631
    case PS_START:
632
      ASSERT(ops == PS_DOWN);
633
      ASSERT(cs == FS_HUNGRY);
634
      break;
635
    case PS_UP:
636
      ASSERT(ops == PS_DOWN || ops == PS_START);
637
      ASSERT(cs == FS_HUNGRY);
638
      proto_schedule_feed(p);
639
      break;
640
    case PS_STOP:
641
      if ((cs = FS_FEEDING) || (cs == FS_HAPPY))
642
        proto_schedule_flush(p);
643
      break;
644
    default:
645
      bug("Invalid state transition for %s from %s/%s to */%s", p->name, c_states[cs], p_states[ops], p_states[ps]);
646
    }
647
}
648

    
649
static void
650
proto_flush_all(void *unused UNUSED)
651
{
652
  struct proto *p;
653

    
654
  rt_prune_all();
655
  while ((p = HEAD(flush_proto_list))->n.next)
656
    {
657
      DBG("Flushing protocol %s\n", p->name);
658
      p->core_state = FS_HUNGRY;
659
      proto_relink(p);
660
      if (p->proto_state == PS_DOWN)
661
        proto_fell_down(p);
662
    }
663
}
664

    
665
/*
666
 *  CLI Commands
667
 */
668

    
669
static char *
670
proto_state_name(struct proto *p)
671
{
672
#define P(x,y) ((x << 4) | y)
673
  switch (P(p->proto_state, p->core_state))
674
    {
675
    case P(PS_DOWN, FS_HUNGRY):                return "down";
676
    case P(PS_START, FS_HUNGRY):        return "start";
677
    case P(PS_UP, FS_HUNGRY):
678
    case P(PS_UP, FS_FEEDING):                return "feed";
679
    case P(PS_STOP, FS_HUNGRY):                return "stop";
680
    case P(PS_UP, FS_HAPPY):                return "up";
681
    case P(PS_STOP, FS_FLUSHING):
682
    case P(PS_DOWN, FS_FLUSHING):        return "flush";
683
    default:                              return "???";
684
    }
685
#undef P
686
}
687

    
688
static void
689
proto_do_show(struct proto *p, int verbose)
690
{
691
  byte buf[256], reltime[TM_RELTIME_BUFFER_SIZE];
692

    
693
  buf[0] = 0;
694
  if (p->proto->get_status)
695
    p->proto->get_status(p, buf);
696
  tm_format_reltime(reltime, p->last_state_change);
697
  cli_msg(-1002, "%-8s %-8s %-8s %-5s %-5s  %s",
698
          p->name,
699
          p->proto->name,
700
          p->table->name,
701
          proto_state_name(p),
702
          reltime,
703
          buf);
704
  if (verbose)
705
    {
706
      if (p->cf->dsc)
707
        cli_msg(-1006, "  Description:    %s", p->cf->dsc);
708
      cli_msg(-1006, "  Preference:     %d", p->preference);
709
      cli_msg(-1006, "  Input filter:   %s", filter_name(p->in_filter));
710
      cli_msg(-1006, "  Output filter:  %s", filter_name(p->out_filter));
711

    
712
      if (p->proto_state != PS_DOWN)
713
        {
714
          cli_msg(-1006, "  Routes:         %u imported, %u exported, %u preferred", 
715
                  p->stats.imp_routes, p->stats.exp_routes, p->stats.pref_routes);
716
          cli_msg(-1006, "  Route change stats:     received   rejected   filtered    ignored   accepted");
717
          cli_msg(-1006, "    Import updates:     %10u %10u %10u %10u %10u",
718
                  p->stats.imp_updates_received, p->stats.imp_updates_invalid,
719
                  p->stats.imp_updates_filtered, p->stats.imp_updates_ignored,
720
                  p->stats.imp_updates_accepted);
721
          cli_msg(-1006, "    Import withdraws:   %10u %10u        --- %10u %10u",
722
                  p->stats.imp_withdraws_received, p->stats.imp_withdraws_invalid,
723
                  p->stats.imp_withdraws_ignored, p->stats.imp_withdraws_accepted);
724
          cli_msg(-1006, "    Export updates:     %10u %10u %10u        --- %10u",
725
                  p->stats.exp_updates_received, p->stats.exp_updates_rejected,
726
                  p->stats.exp_updates_filtered, p->stats.exp_updates_accepted);
727
          cli_msg(-1006, "    Export withdraws:   %10u        ---        ---        --- %10u",
728
                  p->stats.exp_withdraws_received, p->stats.exp_withdraws_accepted);
729
        }
730

    
731
      cli_msg(-1006, "");
732
    }
733
}
734

    
735
void
736
proto_show(struct symbol *s, int verbose)
737
{
738
  if (s && s->class != SYM_PROTO)
739
    {
740
      cli_msg(9002, "%s is not a protocol", s->name);
741
      return;
742
    }
743
  cli_msg(-2002, "name     proto    table    state since  info");
744
  if (s)
745
    proto_do_show(((struct proto_config *)s->def)->proto, verbose);
746
  else
747
    {
748
      WALK_PROTO_LIST(p)
749
        proto_do_show(p, verbose);
750
      WALK_PROTO_LIST_END;
751
    }
752
  cli_msg(0, "");
753
}
754

    
755
struct proto *
756
proto_get_named(struct symbol *sym, struct protocol *pr)
757
{
758
  struct proto *p, *q;
759

    
760
  if (sym)
761
    {
762
      if (sym->class != SYM_PROTO)
763
        cf_error("%s: Not a protocol", sym->name);
764
      p = ((struct proto_config *)sym->def)->proto;
765
      if (!p || p->proto != pr)
766
        cf_error("%s: Not a %s protocol", sym->name, pr->name);
767
    }
768
  else
769
    {
770
      p = NULL;
771
      WALK_LIST(q, active_proto_list)
772
        if (q->proto == pr)
773
          {
774
            if (p)
775
              cf_error("There are multiple %s protocols running", pr->name);
776
            p = q;
777
          }
778
      if (!p)
779
        cf_error("There is no %s protocol running", pr->name);
780
    }
781
  return p;
782
}
783

    
784
void
785
proto_xxable(char *pattern, int xx)
786
{
787
  int cnt = 0;
788
  WALK_PROTO_LIST(p)
789
    if (patmatch(pattern, p->name))
790
      {
791
        cnt++;
792
        switch (xx)
793
          {
794
          case 0:
795
            if (p->disabled)
796
              cli_msg(-8, "%s: already disabled", p->name);
797
            else
798
              {
799
                cli_msg(-9, "%s: disabled", p->name);
800
                p->disabled = 1;
801
              }
802
            break;
803
          case 1:
804
            if (!p->disabled)
805
              cli_msg(-10, "%s: already enabled", p->name);
806
            else
807
              {
808
                cli_msg(-11, "%s: enabled", p->name);
809
                p->disabled = 0;
810
              }
811
            break;
812
          case 2:
813
            if (p->disabled)
814
              cli_msg(-8, "%s: already disabled", p->name);
815
            else
816
              {
817
                p->disabled = 1;
818
                proto_rethink_goal(p);
819
                p->disabled = 0;
820
                cli_msg(-12, "%s: restarted", p->name);
821
              }
822
            break;
823
          default:
824
            ASSERT(0);
825
          }
826
        proto_rethink_goal(p);
827
      }
828
  WALK_PROTO_LIST_END;
829
  if (!cnt)
830
    cli_msg(8003, "No protocols match");
831
  else
832
    cli_msg(0, "");
833
}
834

    
835
void
836
proto_debug(char *pattern, unsigned int mask)
837
{
838
  int cnt = 0;
839
  WALK_PROTO_LIST(p)
840
    if (patmatch(pattern, p->name))
841
      {
842
        cnt++;
843
        p->debug = mask;
844
      }
845
  WALK_PROTO_LIST_END;
846
  if (!cnt)
847
    cli_msg(8003, "No protocols match");
848
  else
849
    cli_msg(0, "");
850
}