Statistics
| Branch: | Revision:

iof-bird-daemon / nest / proto.c @ 11361a10

History | View | Annotate | Download (23.3 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
  proto_relink(p);
590
  p->attn->hook = initial ? proto_feed_initial : proto_feed_more;
591
  ev_schedule(p->attn);
592
}
593

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

    
610
  /* If we are already feeding, we want to restart it */
611
  if (p->core_state == FS_FEEDING)
612
    {
613
      /* Unless feeding is in initial state */
614
      if (p->attn->hook == proto_feed_initial)
615
        return;
616

    
617
      rt_feed_baby_abort(p);
618
    }
619

    
620
  proto_schedule_feed(p, 0);
621
}
622

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

    
643
  DBG("%s reporting state transition %s/%s -> */%s\n", p->name, c_states[cs], p_states[ops], p_states[ps]);
644
  if (ops == ps)
645
    return;
646

    
647
  p->proto_state = ps;
648

    
649
  switch (ps)
650
    {
651
    case PS_DOWN:
652
      if ((cs = FS_FEEDING) || (cs == FS_HAPPY))
653
        proto_schedule_flush(p);
654

    
655
      neigh_prune(); // FIXME convert neighbors to resource?
656
      rfree(p->pool);
657
      p->pool = NULL;
658

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

    
683
static void
684
proto_flush_all(void *unused UNUSED)
685
{
686
  struct proto *p;
687

    
688
  rt_prune_all();
689
  while ((p = HEAD(flush_proto_list))->n.next)
690
    {
691
      DBG("Flushing protocol %s\n", p->name);
692
      p->core_state = FS_HUNGRY;
693
      proto_relink(p);
694
      if (p->proto_state == PS_DOWN)
695
        proto_fell_down(p);
696
    }
697
}
698

    
699
/*
700
 *  CLI Commands
701
 */
702

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

    
722
static void
723
proto_do_show(struct proto *p, int verbose)
724
{
725
  byte buf[256], reltime[TM_RELTIME_BUFFER_SIZE];
726

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

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

    
765
      cli_msg(-1006, "");
766
    }
767
}
768

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

    
789
struct proto *
790
proto_get_named(struct symbol *sym, struct protocol *pr)
791
{
792
  struct proto *p, *q;
793

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

    
818
void
819
proto_xxable(char *pattern, int xx)
820
{
821
  int cnt = 0;
822
  WALK_PROTO_LIST(p)
823
    if (patmatch(pattern, p->name))
824
      {
825
        cnt++;
826
        switch (xx)
827
          {
828
          case 0:
829
            if (p->disabled)
830
              cli_msg(-8, "%s: already disabled", p->name);
831
            else
832
              {
833
                cli_msg(-9, "%s: disabled", p->name);
834
                p->disabled = 1;
835
                proto_rethink_goal(p);
836
              }
837
            break;
838
          case 1:
839
            if (!p->disabled)
840
              cli_msg(-10, "%s: already enabled", p->name);
841
            else
842
              {
843
                cli_msg(-11, "%s: enabled", p->name);
844
                p->disabled = 0;
845
                proto_rethink_goal(p);
846
              }
847
            break;
848
          case 2:
849
            if (p->disabled)
850
              cli_msg(-8, "%s: already disabled", p->name);
851
            else
852
              {
853
                p->disabled = 1;
854
                proto_rethink_goal(p);
855
                p->disabled = 0;
856
                proto_rethink_goal(p);
857
                cli_msg(-12, "%s: restarted", p->name);
858
              }
859
            break;
860
          case 3:
861
            // FIXME change msg number
862
            if (p->disabled)
863
              cli_msg(-8, "%s: already disabled", p->name);
864
            else if (p->reload_routes && p->reload_routes(p))
865
              cli_msg(-12, "%s: reloading", p->name);
866
            else
867
              cli_msg(-12, "%s: reload failed", p->name);
868
            break;
869
          case 4:
870
            // FIXME change msg number
871
            if (p->disabled)
872
              cli_msg(-8, "%s: already disabled", p->name);
873
            else
874
              {
875
                proto_request_feeding(p);
876
                cli_msg(-12, "%s: reexport failed", p->name);
877
              }
878
            break;
879

    
880
          default:
881
            ASSERT(0);
882
          }
883
      }
884
  WALK_PROTO_LIST_END;
885
  if (!cnt)
886
    cli_msg(8003, "No protocols match");
887
  else
888
    cli_msg(0, "");
889
}
890

    
891
void
892
proto_debug(char *pattern, unsigned int mask)
893
{
894
  int cnt = 0;
895
  WALK_PROTO_LIST(p)
896
    if (patmatch(pattern, p->name))
897
      {
898
        cnt++;
899
        p->debug = mask;
900
      }
901
  WALK_PROTO_LIST_END;
902
  if (!cnt)
903
    cli_msg(8003, "No protocols match");
904
  else
905
    cli_msg(0, "");
906
}