Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (20.2 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
 *
273
 * Scan differences between @old and @new configuration and adjust all
274
 * protocol instances to conform to the new configuration.
275
 *
276
 * When a protocol exists in the new configuration, but it doesn't in the
277
 * original one, it's immediately started. When a collision with the other
278
 * running protocol would arise, the new protocol will be temporarily stopped
279
 * by the locking mechanism.
280
 *
281
 * When a protocol exists in the old configuration, but it doesn't in the
282
 * new one, it's shut down and deleted after the shutdown completes.
283
 *
284
 * When a protocol exists in both configurations, the core decides whether
285
 * it's possible to reconfigure it dynamically (it checks all the core properties
286
 * of the protocol and if they match, it asks the reconfigure() hook of the
287
 * protocol to see if the protocol is able to switch to the new configuration).
288
 * If it isn't possible, the protocol is shut down and a new instance is started
289
 * with the new configuration after the shutdown is completed.
290
 */
291
void
292
protos_commit(struct config *new, struct config *old, int force_reconfig)
293
{
294
  struct proto_config *oc, *nc;
295
  struct proto *p, *n;
296

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

    
349
  WALK_LIST(nc, new->protos)
350
    if (!nc->proto)
351
      {
352
        DBG("\t%s: adding\n", nc->name);
353
        proto_init(nc);
354
      }
355
  DBG("\tdone\n");
356

    
357
  DBG("Protocol start\n");
358
  WALK_LIST_DELSAFE(p, n, initial_proto_list)
359
    proto_rethink_goal(p);
360
}
361

    
362
static void
363
proto_rethink_goal(struct proto *p)
364
{
365
  struct protocol *q;
366

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

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

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

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

    
430
  debug("Protocols:\n");
431

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

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

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

    
511
static void
512
proto_fell_down(struct proto *p)
513
{
514
  DBG("Protocol %s down\n", p->name);
515
  rt_unlock_table(p->table);
516
  proto_rethink_goal(p);
517
}
518

    
519
static void
520
proto_feed_more(void *P)
521
{
522
  struct proto *p = P;
523

    
524
  if (p->core_state != FS_FEEDING)
525
    return;
526

    
527
  DBG("Feeding protocol %s continued\n", p->name);
528
  if (rt_feed_baby(p))
529
    {
530
      p->core_state = FS_HAPPY;
531
      proto_relink(p);
532
      DBG("Protocol %s up and running\n", p->name);
533
    }
534
  else
535
    {
536
      p->attn->hook = proto_feed_more;
537
      ev_schedule(p->attn);                /* Will continue later... */
538
    }
539
}
540

    
541
static void
542
proto_feed(void *P)
543
{
544
  struct proto *p = P;
545

    
546
  if (p->core_state != FS_FEEDING)
547
    return;
548

    
549
  DBG("Feeding protocol %s\n", p->name);
550
  proto_add_announce_hook(p, p->table);
551
  if_feed_baby(p);
552
  proto_feed_more(P);
553
}
554

    
555
static void
556
proto_schedule_flush(struct proto *p)
557
{
558
  /* Need to abort feeding */
559
  if (p->core_state == FS_FEEDING)
560
    rt_feed_baby_abort(p);
561

    
562
  DBG("%s: Scheduling flush\n", p->name);
563
  p->core_state = FS_FLUSHING;
564
  proto_relink(p);
565
  proto_flush_hooks(p);
566
  ev_schedule(proto_flush_event);
567
}
568

    
569
static void
570
proto_schedule_feed(struct proto *p)
571
{
572
  DBG("%s: Scheduling meal\n", p->name);
573
  p->core_state = FS_FEEDING;
574
  proto_relink(p);
575
  p->attn->hook = proto_feed;
576
  ev_schedule(p->attn);
577
}
578

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

    
599
  DBG("%s reporting state transition %s/%s -> */%s\n", p->name, c_states[cs], p_states[ops], p_states[ps]);
600
  if (ops == ps)
601
    return;
602

    
603
  p->proto_state = ps;
604

    
605
  switch (ps)
606
    {
607
    case PS_DOWN:
608
      neigh_prune(); // FIXME convert neighbors to resource?
609
      rfree(p->pool);
610
      p->pool = NULL;
611

    
612
      if (cs == FS_HUNGRY)                /* Shutdown finished */
613
        {
614
          proto_fell_down(p);
615
          return;                        /* The protocol might have ceased to exist */
616
        }
617
      /* Otherwise, we have something to flush... */
618
      else if (cs != FS_FLUSHING)
619
        proto_schedule_flush(p);
620
      break;
621
    case PS_START:
622
      ASSERT(ops == PS_DOWN);
623
      ASSERT(cs == FS_HUNGRY);
624
      break;
625
    case PS_UP:
626
      ASSERT(ops == PS_DOWN || ops == PS_START);
627
      ASSERT(cs == FS_HUNGRY);
628
      proto_schedule_feed(p);
629
      break;
630
    case PS_STOP:
631
      if ((cs = FS_FEEDING) || (cs == FS_HAPPY))
632
        proto_schedule_flush(p);
633
      break;
634
    default:
635
      bug("Invalid state transition for %s from %s/%s to */%s", p->name, c_states[cs], p_states[ops], p_states[ps]);
636
    }
637
}
638

    
639
static void
640
proto_flush_all(void *unused UNUSED)
641
{
642
  struct proto *p;
643

    
644
  rt_prune_all();
645
  while ((p = HEAD(flush_proto_list))->n.next)
646
    {
647
      DBG("Flushing protocol %s\n", p->name);
648
      p->core_state = FS_HUNGRY;
649
      proto_relink(p);
650
      if (p->proto_state == PS_DOWN)
651
        proto_fell_down(p);
652
    }
653
}
654

    
655
/*
656
 *  CLI Commands
657
 */
658

    
659
static char *
660
proto_state_name(struct proto *p)
661
{
662
#define P(x,y) ((x << 4) | y)
663
  switch (P(p->proto_state, p->core_state))
664
    {
665
    case P(PS_DOWN, FS_HUNGRY):                return "down";
666
    case P(PS_START, FS_HUNGRY):        return "start";
667
    case P(PS_UP, FS_HUNGRY):
668
    case P(PS_UP, FS_FEEDING):                return "feed";
669
    case P(PS_STOP, FS_HUNGRY):                return "stop";
670
    case P(PS_UP, FS_HAPPY):                return "up";
671
    case P(PS_STOP, FS_FLUSHING):
672
    case P(PS_DOWN, FS_FLUSHING):        return "flush";
673
    default:                              return "???";
674
    }
675
#undef P
676
}
677

    
678
static void
679
proto_do_show(struct proto *p, int verbose)
680
{
681
  byte buf[256], reltime[TM_RELTIME_BUFFER_SIZE];
682

    
683
  buf[0] = 0;
684
  if (p->proto->get_status)
685
    p->proto->get_status(p, buf);
686
  tm_format_reltime(reltime, p->last_state_change);
687
  cli_msg(-1002, "%-8s %-8s %-8s %-5s %-5s  %s",
688
          p->name,
689
          p->proto->name,
690
          p->table->name,
691
          proto_state_name(p),
692
          reltime,
693
          buf);
694
  if (verbose)
695
    {
696
      cli_msg(-1006, "\tPreference: %d", p->preference);
697
      cli_msg(-1006, "\tInput filter: %s", filter_name(p->in_filter));
698
      cli_msg(-1006, "\tOutput filter: %s", filter_name(p->out_filter));
699
    }
700
}
701

    
702
void
703
proto_show(struct symbol *s, int verbose)
704
{
705
  if (s && s->class != SYM_PROTO)
706
    {
707
      cli_msg(9002, "%s is not a protocol", s->name);
708
      return;
709
    }
710
  cli_msg(-2002, "name     proto    table    state since  info");
711
  if (s)
712
    proto_do_show(((struct proto_config *)s->def)->proto, verbose);
713
  else
714
    {
715
      WALK_PROTO_LIST(p)
716
        proto_do_show(p, verbose);
717
      WALK_PROTO_LIST_END;
718
    }
719
  cli_msg(0, "");
720
}
721

    
722
struct proto *
723
proto_get_named(struct symbol *sym, struct protocol *pr)
724
{
725
  struct proto *p, *q;
726

    
727
  if (sym)
728
    {
729
      if (sym->class != SYM_PROTO)
730
        cf_error("%s: Not a protocol", sym->name);
731
      p = ((struct proto_config *)sym->def)->proto;
732
      if (!p || p->proto != pr)
733
        cf_error("%s: Not a %s protocol", sym->name, pr->name);
734
    }
735
  else
736
    {
737
      p = NULL;
738
      WALK_LIST(q, active_proto_list)
739
        if (q->proto == pr)
740
          {
741
            if (p)
742
              cf_error("There are multiple %s protocols running", pr->name);
743
            p = q;
744
          }
745
      if (!p)
746
        cf_error("There is no %s protocol running", pr->name);
747
    }
748
  return p;
749
}
750

    
751
void
752
proto_xxable(char *pattern, int xx)
753
{
754
  int cnt = 0;
755
  WALK_PROTO_LIST(p)
756
    if (patmatch(pattern, p->name))
757
      {
758
        cnt++;
759
        switch (xx)
760
          {
761
          case 0:
762
            if (p->disabled)
763
              cli_msg(-8, "%s: already disabled", p->name);
764
            else
765
              {
766
                cli_msg(-9, "%s: disabled", p->name);
767
                p->disabled = 1;
768
              }
769
            break;
770
          case 1:
771
            if (!p->disabled)
772
              cli_msg(-10, "%s: already enabled", p->name);
773
            else
774
              {
775
                cli_msg(-11, "%s: enabled", p->name);
776
                p->disabled = 0;
777
              }
778
            break;
779
          case 2:
780
            if (p->disabled)
781
              cli_msg(-8, "%s: already disabled", p->name);
782
            else
783
              {
784
                p->disabled = 1;
785
                proto_rethink_goal(p);
786
                p->disabled = 0;
787
                cli_msg(-12, "%s: restarted", p->name);
788
              }
789
            break;
790
          default:
791
            ASSERT(0);
792
          }
793
        proto_rethink_goal(p);
794
      }
795
  WALK_PROTO_LIST_END;
796
  if (!cnt)
797
    cli_msg(8003, "No protocols match");
798
  else
799
    cli_msg(0, "");
800
}
801

    
802
void
803
proto_debug(char *pattern, unsigned int mask)
804
{
805
  int cnt = 0;
806
  WALK_PROTO_LIST(p)
807
    if (patmatch(pattern, p->name))
808
      {
809
        cnt++;
810
        p->debug = mask;
811
      }
812
  WALK_PROTO_LIST_END;
813
  if (!cnt)
814
    cli_msg(8003, "No protocols match");
815
  else
816
    cli_msg(0, "");
817
}