Statistics
| Branch: | Revision:

iof-bird-daemon / nest / proto.c @ 8a7fb885

History | View | Annotate | Download (23.4 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_HUNGRY:
77
      l = &inactive_proto_list;
78
      break;
79
    case FS_FEEDING:
80
    case FS_HAPPY:
81
      l = &active_proto_list;
82
      break;
83
    case FS_FLUSHING:
84
      l = &flush_proto_list;
85
      break;
86
    default:
87
      ASSERT(0);
88
    }
89
  proto_enqueue(l, p);
90
}
91

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

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

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

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

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

    
169
static void
170
proto_flush_hooks(struct proto *p)
171
{
172
  struct announce_hook *h;
173

    
174
  for(h=p->ahooks; h; h=h->next)
175
    rem_node(&h->n);
176
  p->ahooks = NULL;
177
}
178

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

    
197
  add_tail(&new_config->protos, &c->n);
198
  c->global = new_config;
199
  c->protocol = pr;
200
  c->name = pr->name;
201
  c->out_filter = FILTER_REJECT;
202
  c->table = c->global->master_rtc;
203
  c->debug = new_config->proto_default_debug;
204
  return c;
205
}
206

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

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

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

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

    
256
static struct proto *
257
proto_init(struct proto_config *c)
258
{
259
  struct protocol *p = c->protocol;
260
  struct proto *q = p->init(c);
261

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

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

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

    
358
  WALK_LIST(nc, new->protos)
359
    if (!nc->proto)
360
      {
361
        DBG("\t%s: adding\n", nc->name);
362
        proto_init(nc);
363
      }
364
  DBG("\tdone\n");
365

    
366
  DBG("Protocol start\n");
367
  WALK_LIST_DELSAFE(p, n, initial_proto_list)
368
    proto_rethink_goal(p);
369
}
370

    
371
static void
372
proto_rethink_goal(struct proto *p)
373
{
374
  struct protocol *q;
375

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

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

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

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

    
439
  debug("Protocols:\n");
440

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

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

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

    
520
static void
521
proto_fell_down(struct proto *p)
522
{
523
  DBG("Protocol %s down\n", p->name);
524

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

    
528
  bzero(&p->stats, sizeof(struct proto_stats));
529
  rt_unlock_table(p->table);
530
  proto_rethink_goal(p);
531
}
532

    
533
static void
534
proto_feed_more(void *P)
535
{
536
  struct proto *p = P;
537

    
538
  if (p->core_state != FS_FEEDING)
539
    return;
540

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

    
555
static void
556
proto_feed_initial(void *P)
557
{
558
  struct proto *p = P;
559

    
560
  if (p->core_state != FS_FEEDING)
561
    return;
562

    
563
  DBG("Feeding protocol %s\n", p->name);
564
  proto_add_announce_hook(p, p->table);
565
  if_feed_baby(p);
566
  proto_feed_more(P);
567
}
568

    
569
static void
570
proto_schedule_flush(struct proto *p)
571
{
572
  /* Need to abort feeding */
573
  if (p->core_state == FS_FEEDING)
574
    rt_feed_baby_abort(p);
575

    
576
  DBG("%s: Scheduling flush\n", p->name);
577
  p->core_state = FS_FLUSHING;
578
  proto_relink(p);
579
  proto_flush_hooks(p);
580
  ev_schedule(proto_flush_event);
581
}
582

    
583
static void
584
proto_schedule_feed(struct proto *p, int initial)
585
{
586
  DBG("%s: Scheduling meal\n", p->name);
587
  p->core_state = FS_FEEDING;
588
  p->refeeding = !initial;
589

    
590
  /* Hack: reset exp_routes during refeed, and do not decrease it later */
591
  if (!initial)
592
    p->stats.exp_routes = 0;
593

    
594
  proto_relink(p);
595
  p->attn->hook = initial ? proto_feed_initial : proto_feed_more;
596
  ev_schedule(p->attn);
597
}
598

    
599
/**
600
 * proto_request_feeding - request feeding routes to the protocol
601
 * @p: given protocol 
602
 *
603
 * Sometimes it is needed to send again all routes to the
604
 * protocol. This is called feeding and can be requested by this
605
 * function. This would cause protocol core state transition
606
 * to FS_FEEDING (during feeding) and when completed, it will
607
 * switch back to FS_HAPPY. This function can be called even
608
 * when feeding is already running, in that case it is restarted.
609
 */
610
void
611
proto_request_feeding(struct proto *p)
612
{
613
  ASSERT(p->proto_state == PS_UP);
614

    
615
  /* If we are already feeding, we want to restart it */
616
  if (p->core_state == FS_FEEDING)
617
    {
618
      /* Unless feeding is in initial state */
619
      if (p->attn->hook == proto_feed_initial)
620
        return;
621

    
622
      rt_feed_baby_abort(p);
623
    }
624

    
625
  proto_schedule_feed(p, 0);
626
}
627

    
628
/**
629
 * proto_notify_state - notify core about protocol state change
630
 * @p: protocol the state of which has changed
631
 * @ps: the new status
632
 *
633
 * Whenever a state of a protocol changes due to some event internal
634
 * to the protocol (i.e., not inside a start() or shutdown() hook),
635
 * it should immediately notify the core about the change by calling
636
 * proto_notify_state() which will write the new state to the &proto
637
 * structure and take all the actions necessary to adapt to the new
638
 * state. State change to PS_DOWN immediately frees resources of protocol
639
 * and might execute start callback of protocol; therefore,
640
 * it should be used at tail positions of protocol callbacks.
641
 */
642
void
643
proto_notify_state(struct proto *p, unsigned ps)
644
{
645
  unsigned ops = p->proto_state;
646
  unsigned cs = p->core_state;
647

    
648
  DBG("%s reporting state transition %s/%s -> */%s\n", p->name, c_states[cs], p_states[ops], p_states[ps]);
649
  if (ops == ps)
650
    return;
651

    
652
  p->proto_state = ps;
653

    
654
  switch (ps)
655
    {
656
    case PS_DOWN:
657
      if ((cs == FS_FEEDING) || (cs == FS_HAPPY))
658
        proto_schedule_flush(p);
659

    
660
      neigh_prune(); // FIXME convert neighbors to resource?
661
      rfree(p->pool);
662
      p->pool = NULL;
663

    
664
      if (cs == FS_HUNGRY)                /* Shutdown finished */
665
        {
666
          proto_fell_down(p);
667
          return;                        /* The protocol might have ceased to exist */
668
        }
669
      break;
670
    case PS_START:
671
      ASSERT(ops == PS_DOWN);
672
      ASSERT(cs == FS_HUNGRY);
673
      break;
674
    case PS_UP:
675
      ASSERT(ops == PS_DOWN || ops == PS_START);
676
      ASSERT(cs == FS_HUNGRY);
677
      proto_schedule_feed(p, 1);
678
      break;
679
    case PS_STOP:
680
      if ((cs == FS_FEEDING) || (cs == FS_HAPPY))
681
        proto_schedule_flush(p);
682
      break;
683
    default:
684
      bug("Invalid state transition for %s from %s/%s to */%s", p->name, c_states[cs], p_states[ops], p_states[ps]);
685
    }
686
}
687

    
688
static void
689
proto_flush_all(void *unused UNUSED)
690
{
691
  struct proto *p;
692

    
693
  rt_prune_all();
694
  while ((p = HEAD(flush_proto_list))->n.next)
695
    {
696
      DBG("Flushing protocol %s\n", p->name);
697
      p->core_state = FS_HUNGRY;
698
      proto_relink(p);
699
      if (p->proto_state == PS_DOWN)
700
        proto_fell_down(p);
701
    }
702
}
703

    
704
/*
705
 *  CLI Commands
706
 */
707

    
708
static char *
709
proto_state_name(struct proto *p)
710
{
711
#define P(x,y) ((x << 4) | y)
712
  switch (P(p->proto_state, p->core_state))
713
    {
714
    case P(PS_DOWN, FS_HUNGRY):                return "down";
715
    case P(PS_START, FS_HUNGRY):        return "start";
716
    case P(PS_UP, FS_HUNGRY):
717
    case P(PS_UP, FS_FEEDING):                return "feed";
718
    case P(PS_STOP, FS_HUNGRY):                return "stop";
719
    case P(PS_UP, FS_HAPPY):                return "up";
720
    case P(PS_STOP, FS_FLUSHING):
721
    case P(PS_DOWN, FS_FLUSHING):        return "flush";
722
    default:                              return "???";
723
    }
724
#undef P
725
}
726

    
727
static void
728
proto_do_show(struct proto *p, int verbose)
729
{
730
  byte buf[256], reltime[TM_RELTIME_BUFFER_SIZE];
731

    
732
  buf[0] = 0;
733
  if (p->proto->get_status)
734
    p->proto->get_status(p, buf);
735
  tm_format_reltime(reltime, p->last_state_change);
736
  cli_msg(-1002, "%-8s %-8s %-8s %-5s %-5s  %s",
737
          p->name,
738
          p->proto->name,
739
          p->table->name,
740
          proto_state_name(p),
741
          reltime,
742
          buf);
743
  if (verbose)
744
    {
745
      if (p->cf->dsc)
746
        cli_msg(-1006, "  Description:    %s", p->cf->dsc);
747
      cli_msg(-1006, "  Preference:     %d", p->preference);
748
      cli_msg(-1006, "  Input filter:   %s", filter_name(p->in_filter));
749
      cli_msg(-1006, "  Output filter:  %s", filter_name(p->out_filter));
750

    
751
      if (p->proto_state != PS_DOWN)
752
        {
753
          cli_msg(-1006, "  Routes:         %u imported, %u exported, %u preferred", 
754
                  p->stats.imp_routes, p->stats.exp_routes, p->stats.pref_routes);
755
          cli_msg(-1006, "  Route change stats:     received   rejected   filtered    ignored   accepted");
756
          cli_msg(-1006, "    Import updates:     %10u %10u %10u %10u %10u",
757
                  p->stats.imp_updates_received, p->stats.imp_updates_invalid,
758
                  p->stats.imp_updates_filtered, p->stats.imp_updates_ignored,
759
                  p->stats.imp_updates_accepted);
760
          cli_msg(-1006, "    Import withdraws:   %10u %10u        --- %10u %10u",
761
                  p->stats.imp_withdraws_received, p->stats.imp_withdraws_invalid,
762
                  p->stats.imp_withdraws_ignored, p->stats.imp_withdraws_accepted);
763
          cli_msg(-1006, "    Export updates:     %10u %10u %10u        --- %10u",
764
                  p->stats.exp_updates_received, p->stats.exp_updates_rejected,
765
                  p->stats.exp_updates_filtered, p->stats.exp_updates_accepted);
766
          cli_msg(-1006, "    Export withdraws:   %10u        ---        ---        --- %10u",
767
                  p->stats.exp_withdraws_received, p->stats.exp_withdraws_accepted);
768
        }
769

    
770
      cli_msg(-1006, "");
771
    }
772
}
773

    
774
void
775
proto_show(struct symbol *s, int verbose)
776
{
777
  if (s && s->class != SYM_PROTO)
778
    {
779
      cli_msg(9002, "%s is not a protocol", s->name);
780
      return;
781
    }
782
  cli_msg(-2002, "name     proto    table    state since  info");
783
  if (s)
784
    proto_do_show(((struct proto_config *)s->def)->proto, verbose);
785
  else
786
    {
787
      WALK_PROTO_LIST(p)
788
        proto_do_show(p, verbose);
789
      WALK_PROTO_LIST_END;
790
    }
791
  cli_msg(0, "");
792
}
793

    
794
struct proto *
795
proto_get_named(struct symbol *sym, struct protocol *pr)
796
{
797
  struct proto *p, *q;
798

    
799
  if (sym)
800
    {
801
      if (sym->class != SYM_PROTO)
802
        cf_error("%s: Not a protocol", sym->name);
803
      p = ((struct proto_config *)sym->def)->proto;
804
      if (!p || p->proto != pr)
805
        cf_error("%s: Not a %s protocol", sym->name, pr->name);
806
    }
807
  else
808
    {
809
      p = NULL;
810
      WALK_LIST(q, active_proto_list)
811
        if (q->proto == pr)
812
          {
813
            if (p)
814
              cf_error("There are multiple %s protocols running", pr->name);
815
            p = q;
816
          }
817
      if (!p)
818
        cf_error("There is no %s protocol running", pr->name);
819
    }
820
  return p;
821
}
822

    
823
void
824
proto_xxable(char *pattern, int xx)
825
{
826
  int cnt = 0;
827
  WALK_PROTO_LIST(p)
828
    if (patmatch(pattern, p->name))
829
      {
830
        cnt++;
831
        switch (xx)
832
          {
833
          case XX_DISABLE:
834
            if (p->disabled)
835
              cli_msg(-8, "%s: already disabled", p->name);
836
            else
837
              {
838
                cli_msg(-9, "%s: disabled", p->name);
839
                p->disabled = 1;
840
                proto_rethink_goal(p);
841
              }
842
            break;
843

    
844
          case XX_ENABLE:
845
            if (!p->disabled)
846
              cli_msg(-10, "%s: already enabled", p->name);
847
            else
848
              {
849
                cli_msg(-11, "%s: enabled", p->name);
850
                p->disabled = 0;
851
                proto_rethink_goal(p);
852
              }
853
            break;
854

    
855
          case XX_RESTART:
856
            if (p->disabled)
857
              cli_msg(-8, "%s: already disabled", p->name);
858
            else
859
              {
860
                p->disabled = 1;
861
                proto_rethink_goal(p);
862
                p->disabled = 0;
863
                proto_rethink_goal(p);
864
                cli_msg(-12, "%s: restarted", p->name);
865
              }
866
            break;
867

    
868
          case XX_RELOAD:
869
          case XX_RELOAD_IN:
870
          case XX_RELOAD_OUT:
871
            if (p->disabled)
872
              {
873
                cli_msg(-8, "%s: already disabled", p->name);
874
                break;
875
              }
876

    
877
            /* re-importing routes */
878
            if (xx != XX_RELOAD_OUT)
879
              if (! (p->reload_routes && p->reload_routes(p)))
880
                {
881
                  cli_msg(-8006, "%s: reload failed", p->name);
882
                  break;
883
                }
884
                 
885
            /* re-exporting routes */
886
            if (xx != XX_RELOAD_IN)
887
              proto_request_feeding(p);
888

    
889
            cli_msg(-15, "%s: reloading", p->name);
890
            break;
891

    
892
          default:
893
            ASSERT(0);
894
          }
895
      }
896
  WALK_PROTO_LIST_END;
897
  if (!cnt)
898
    cli_msg(8003, "No protocols match");
899
  else
900
    cli_msg(0, "");
901
}
902

    
903
void
904
proto_debug(char *pattern, unsigned int mask)
905
{
906
  int cnt = 0;
907
  WALK_PROTO_LIST(p)
908
    if (patmatch(pattern, p->name))
909
      {
910
        cnt++;
911
        p->debug = mask;
912
      }
913
  WALK_PROTO_LIST_END;
914
  if (!cnt)
915
    cli_msg(8003, "No protocols match");
916
  else
917
    cli_msg(0, "");
918
}