Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (21.8 KB)

1 2326b001 Martin Mares
/*
2
 *        BIRD -- Protocols
3
 *
4 50fe90ed Martin Mares
 *        (c) 1998--2000 Martin Mares <mj@ucw.cz>
5 2326b001 Martin Mares
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8
9 6b9fa320 Martin Mares
#undef LOCAL_DEBUG
10 7f4a3988 Martin Mares
11 2326b001 Martin Mares
#include "nest/bird.h"
12
#include "nest/protocol.h"
13
#include "lib/resource.h"
14
#include "lib/lists.h"
15 67bd949a Martin Mares
#include "lib/event.h"
16 f14a4bec Martin Mares
#include "lib/string.h"
17 fe7cec12 Martin Mares
#include "conf/conf.h"
18 47b79306 Martin Mares
#include "nest/route.h"
19
#include "nest/iface.h"
20 ae97b946 Martin Mares
#include "nest/cli.h"
21 529c4149 Martin Mares
#include "filter/filter.h"
22 2326b001 Martin Mares
23 67bd949a Martin Mares
static pool *proto_pool;
24
25 3991d84e Martin Mares
static list protocol_list;
26 f14a4bec Martin Mares
static list proto_list;
27 67bd949a Martin Mares
28 f14a4bec Martin Mares
#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 839380d7 Martin Mares
#define PD(pr, msg, args...) do { if (pr->debug & D_STATES) { log(L_TRACE "%s: " msg, pr->name , ## args); } } while(0)
35
36 f14a4bec Martin Mares
list active_proto_list;
37 67bd949a Martin Mares
static list inactive_proto_list;
38
static list initial_proto_list;
39 1a54b1c6 Martin Mares
static list flush_proto_list;
40
41
static event *proto_flush_event;
42 67bd949a Martin Mares
43
static char *p_states[] = { "DOWN", "START", "UP", "STOP" };
44
static char *c_states[] = { "HUNGRY", "FEEDING", "HAPPY", "FLUSHING" };
45
46 8f6accb5 Martin Mares
static void proto_flush_all(void *);
47 50fe90ed Martin Mares
static void proto_rethink_goal(struct proto *p);
48 839380d7 Martin Mares
static char *proto_state_name(struct proto *p);
49 1a54b1c6 Martin Mares
50 67bd949a Martin Mares
static void
51 b2280748 Martin Mares
proto_enqueue(list *l, struct proto *p)
52
{
53 3ea1ba63 Martin Mares
  add_tail(l, &p->n);
54 9685deb9 Martin Mares
  p->last_state_change = now;
55 b2280748 Martin Mares
}
56
57
static void
58 67bd949a Martin Mares
proto_relink(struct proto *p)
59
{
60 1a54b1c6 Martin Mares
  list *l;
61
62 df9f0fb3 Martin Mares
  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 67bd949a Martin Mares
  rem_node(&p->n);
74 1a54b1c6 Martin Mares
  switch (p->core_state)
75
    {
76
    case FS_HAPPY:
77 f14a4bec Martin Mares
      l = &active_proto_list;
78 1a54b1c6 Martin Mares
      break;
79
    case FS_FLUSHING:
80
      l = &flush_proto_list;
81
      break;
82
    default:
83
      l = &inactive_proto_list;
84
    }
85 b2280748 Martin Mares
  proto_enqueue(l, p);
86 67bd949a Martin Mares
}
87 2326b001 Martin Mares
88 3c6269b8 Martin Mares
/**
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 2e9b2421 Martin Mares
 * initializing all the protocol instances configured by calling their
97 3c6269b8 Martin Mares
 * 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 7f4a3988 Martin Mares
void *
103 31b3e1bb Martin Mares
proto_new(struct proto_config *c, unsigned size)
104 7f4a3988 Martin Mares
{
105 1d2664a4 Martin Mares
  struct protocol *pr = c->protocol;
106 8fe48f13 Martin Mares
  struct proto *p = mb_allocz(proto_pool, size);
107 7f4a3988 Martin Mares
108 31b3e1bb Martin Mares
  p->cf = c;
109
  p->debug = c->debug;
110 67bd949a Martin Mares
  p->name = c->name;
111 31b3e1bb Martin Mares
  p->preference = c->preference;
112
  p->disabled = c->disabled;
113 7f4a3988 Martin Mares
  p->proto = pr;
114 9d885689 Martin Mares
  p->table = c->table->table;
115 529c4149 Martin Mares
  p->in_filter = c->in_filter;
116
  p->out_filter = c->out_filter;
117 0da472d7 Martin Mares
  p->min_scope = SCOPE_SITE;
118 7293c5dd Martin Mares
  p->hash_key = random_u32();
119 1d2664a4 Martin Mares
  c->proto = p;
120 7f4a3988 Martin Mares
  return p;
121
}
122
123 1a54b1c6 Martin Mares
static void
124
proto_init_instance(struct proto *p)
125
{
126 5bc512aa Martin Mares
  /* Here we cannot use p->cf->name since it won't survive reconfiguration */
127
  p->pool = rp_new(proto_pool, p->proto->name);
128 1a54b1c6 Martin Mares
  p->attn = ev_new(p->pool);
129
  p->attn->data = p;
130 50fe90ed Martin Mares
  rt_lock_table(p->table);
131 1a54b1c6 Martin Mares
}
132
133 3c6269b8 Martin Mares
/**
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 0e02abfd Martin Mares
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 839380d7 Martin Mares
  PD(p, "Connected to table %s", t->name);
156 0e02abfd Martin Mares
  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 3c6269b8 Martin Mares
/**
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 31b3e1bb Martin Mares
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 1d2664a4 Martin Mares
  c->protocol = pr;
196 31b3e1bb Martin Mares
  c->name = pr->name;
197 5056c559 Martin Mares
  c->out_filter = FILTER_REJECT;
198 9d885689 Martin Mares
  c->table = c->global->master_rtc;
199 839380d7 Martin Mares
  c->debug = new_config->proto_default_debug;
200 31b3e1bb Martin Mares
  return c;
201
}
202
203 3c6269b8 Martin Mares
/**
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 2326b001 Martin Mares
void
212 31b3e1bb Martin Mares
protos_preconfig(struct config *c)
213 2326b001 Martin Mares
{
214 7f4a3988 Martin Mares
  struct protocol *p;
215
216 7c0cc76e Martin Mares
  init_list(&c->protos);
217 6b9fa320 Martin Mares
  DBG("Protocol preconfig:");
218 7f4a3988 Martin Mares
  WALK_LIST(p, protocol_list)
219
    {
220 6b9fa320 Martin Mares
      DBG(" %s", p->name);
221 4ba84ebc Martin Mares
      p->name_counter = 0;
222 3629bcf0 Martin Mares
      if (p->preconfig)
223 31b3e1bb Martin Mares
        p->preconfig(p, c);
224 7f4a3988 Martin Mares
    }
225 6b9fa320 Martin Mares
  DBG("\n");
226 7f4a3988 Martin Mares
}
227
228 3c6269b8 Martin Mares
/**
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 7f4a3988 Martin Mares
void
236 31b3e1bb Martin Mares
protos_postconfig(struct config *c)
237 7f4a3988 Martin Mares
{
238 31b3e1bb Martin Mares
  struct proto_config *x;
239 7f4a3988 Martin Mares
  struct protocol *p;
240
241 6b9fa320 Martin Mares
  DBG("Protocol postconfig:");
242 31b3e1bb Martin Mares
  WALK_LIST(x, c->protos)
243 7f4a3988 Martin Mares
    {
244 6b9fa320 Martin Mares
      DBG(" %s", x->name);
245 1d2664a4 Martin Mares
      p = x->protocol;
246 3629bcf0 Martin Mares
      if (p->postconfig)
247 31b3e1bb Martin Mares
        p->postconfig(x);
248
    }
249 6b9fa320 Martin Mares
  DBG("\n");
250 31b3e1bb Martin Mares
}
251
252 50fe90ed Martin Mares
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 f14a4bec Martin Mares
  add_tail(&proto_list, &q->glob_node);
262 498c3339 Martin Mares
  PD(q, "Initializing%s", q->disabled ? " [disabled]" : "");
263 50fe90ed Martin Mares
  return q;
264
}
265
266 3c6269b8 Martin Mares
/**
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 bf1aec97 Ondrej Zajicek
 * @type: type of reconfiguration (RECONFIG_SOFT or RECONFIG_HARD)
273 3c6269b8 Martin Mares
 *
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 bf1aec97 Ondrej Zajicek
 * 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 3c6269b8 Martin Mares
 */
294 31b3e1bb Martin Mares
void
295 bf1aec97 Ondrej Zajicek
protos_commit(struct config *new, struct config *old, int force_reconfig, int type)
296 31b3e1bb Martin Mares
{
297 50fe90ed Martin Mares
  struct proto_config *oc, *nc;
298
  struct proto *p, *n;
299 31b3e1bb Martin Mares
300 50fe90ed Martin Mares
  DBG("protos_commit:\n");
301
  if (old)
302 31b3e1bb Martin Mares
    {
303 50fe90ed Martin Mares
      WALK_LIST(oc, old->protos)
304
        {
305
          struct proto *p = oc->proto;
306
          struct symbol *sym = cf_find_symbol(oc->name);
307 bf8558bc Martin Mares
          if (sym && sym->class == SYM_PROTO && !new->shutdown)
308 50fe90ed Martin Mares
            {
309
              /* Found match, let's check if we can smoothly switch to new configuration */
310 e04555c0 Ondrej Zajicek
              /* No need to check description */
311 50fe90ed Martin Mares
              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 4cdd0784 Ondrej Zajicek
                  && proto_get_router_id(nc) == proto_get_router_id(oc)
318 bf1aec97 Ondrej Zajicek
                  && ((type == RECONFIG_SOFT) || filter_same(nc->in_filter, oc->in_filter))
319
                  && ((type == RECONFIG_SOFT) || filter_same(nc->out_filter, oc->out_filter))
320 50fe90ed Martin Mares
                  && p->proto_state != PS_DOWN)
321
                {
322
                  /* Generic attributes match, try converting them and then ask the protocol */
323
                  p->debug = nc->debug;
324 bf8558bc Martin Mares
                  if (p->proto->reconfigure && p->proto->reconfigure(p, nc))
325 50fe90ed Martin Mares
                    {
326
                      DBG("\t%s: same\n", oc->name);
327 839380d7 Martin Mares
                      PD(p, "Reconfigured");
328 50fe90ed Martin Mares
                      p->cf = nc;
329 26368f65 Martin Mares
                      p->name = nc->name;
330 41b26cfb Martin Mares
                      p->in_filter = nc->in_filter;
331
                      p->out_filter = nc->out_filter;
332 50fe90ed Martin Mares
                      nc->proto = p;
333
                      continue;
334
                    }
335
                }
336
              /* Unsuccessful, force reconfig */
337
              DBG("\t%s: power cycling\n", oc->name);
338 839380d7 Martin Mares
              PD(p, "Reconfiguration failed, restarting");
339 50fe90ed Martin Mares
              p->cf_new = nc;
340 bf8558bc Martin Mares
              nc->proto = p;
341 50fe90ed Martin Mares
            }
342
          else
343
            {
344
              DBG("\t%s: deleting\n", oc->name);
345 839380d7 Martin Mares
              PD(p, "Unconfigured");
346 50fe90ed Martin Mares
              p->cf_new = NULL;
347
            }
348
          p->reconfiguring = 1;
349
          config_add_obstacle(old);
350
          proto_rethink_goal(p);
351
        }
352 7f4a3988 Martin Mares
    }
353 50fe90ed Martin Mares
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 7f4a3988 Martin Mares
}
366
367 47b79306 Martin Mares
static void
368 67bd949a Martin Mares
proto_rethink_goal(struct proto *p)
369 47b79306 Martin Mares
{
370 50fe90ed Martin Mares
  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 f14a4bec Martin Mares
      rem_node(&p->glob_node);
379 50fe90ed Martin Mares
      mb_free(p);
380
      if (!nc)
381
        return;
382 f098e072 Martin Mares
      p = proto_init(nc);
383 50fe90ed Martin Mares
    }
384
385
  /* Determine what state we want to reach */
386 bf8558bc Martin Mares
  if (p->disabled || p->reconfiguring)
387 ebd3720f Martin Mares
    {
388
      p->core_goal = FS_HUNGRY;
389
      if (p->core_state == FS_HUNGRY && p->proto_state == PS_DOWN)
390
        return;
391
    }
392 50fe90ed Martin Mares
  else
393 ebd3720f Martin Mares
    {
394
      p->core_goal = FS_HAPPY;
395
      if (p->core_state == FS_HAPPY && p->proto_state == PS_UP)
396
        return;
397
    }
398 50fe90ed Martin Mares
399
  q = p->proto;
400 67bd949a Martin Mares
  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 839380d7 Martin Mares
          PD(p, "Starting");
406 1a54b1c6 Martin Mares
          proto_init_instance(p);
407 67bd949a Martin Mares
          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 839380d7 Martin Mares
          PD(p, "Shutting down");
416 67bd949a Martin Mares
          proto_notify_state(p, (q->shutdown ? q->shutdown(p) : PS_DOWN));
417
        }
418
    }
419
}
420
421 3c6269b8 Martin Mares
/**
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 7f4a3988 Martin Mares
void
431 87d2be86 Pavel Machek
protos_dump_all(void)
432
{
433
  struct proto *p;
434
435
  debug("Protocols:\n");
436
437 f14a4bec Martin Mares
  WALK_LIST(p, active_proto_list)
438 87d2be86 Pavel Machek
    {
439 3ea1ba63 Martin Mares
      debug("  protocol %s state %s/%s\n", p->name,
440 b2280748 Martin Mares
            p_states[p->proto_state], c_states[p->core_state]);
441 529c4149 Martin Mares
      if (p->in_filter)
442 5056c559 Martin Mares
        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 66efdf96 Martin Mares
      if (p->disabled)
446
        debug("\tDISABLED\n");
447 31b3e1bb Martin Mares
      else if (p->proto->dump)
448
        p->proto->dump(p);
449 87d2be86 Pavel Machek
    }
450 47b79306 Martin Mares
  WALK_LIST(p, inactive_proto_list)
451 67bd949a Martin Mares
    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 f14a4bec Martin Mares
  WALK_LIST(p, flush_proto_list)
455
    debug("  flushing %s\n", p->name);
456 87d2be86 Pavel Machek
}
457
458 3c6269b8 Martin Mares
/**
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 2e9b2421 Martin Mares
 * all platform specific protocols to inform the core that they exist.
465 3c6269b8 Martin Mares
 */
466 87d2be86 Pavel Machek
void
467 3991d84e Martin Mares
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 3c6269b8 Martin Mares
/**
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 3991d84e Martin Mares
void
487 0432c017 Martin Mares
protos_build(void)
488
{
489
  init_list(&protocol_list);
490 471cc0be Martin Mares
  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 3991d84e Martin Mares
  proto_build(&proto_device);
496 18fff6a1 Martin Mares
#ifdef CONFIG_RIP
497 3991d84e Martin Mares
  proto_build(&proto_rip);
498 18fff6a1 Martin Mares
#endif
499
#ifdef CONFIG_STATIC
500 3991d84e Martin Mares
  proto_build(&proto_static);
501 18fff6a1 Martin Mares
#endif
502 c1f8dc91 Ondrej Filip
#ifdef CONFIG_OSPF
503 3991d84e Martin Mares
  proto_build(&proto_ospf);
504 c1f8dc91 Ondrej Filip
#endif
505 26368f65 Martin Mares
#ifdef CONFIG_PIPE
506 3991d84e Martin Mares
  proto_build(&proto_pipe);
507 26368f65 Martin Mares
#endif
508 2638249d Martin Mares
#ifdef CONFIG_BGP
509 3991d84e Martin Mares
  proto_build(&proto_bgp);
510 2638249d Martin Mares
#endif
511 67bd949a Martin Mares
  proto_pool = rp_new(&root_pool, "Protocols");
512 1a54b1c6 Martin Mares
  proto_flush_event = ev_new(proto_pool);
513
  proto_flush_event->hook = proto_flush_all;
514 67bd949a Martin Mares
}
515
516
static void
517
proto_fell_down(struct proto *p)
518
{
519
  DBG("Protocol %s down\n", p->name);
520 ac07aacd Ondrej Zajicek
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 925fe2d3 Ondrej Zajicek
524
  bzero(&p->stats, sizeof(struct proto_stats));
525 50fe90ed Martin Mares
  rt_unlock_table(p->table);
526 67bd949a Martin Mares
  proto_rethink_goal(p);
527
}
528
529 8f6accb5 Martin Mares
static void
530 ac5d8012 Martin Mares
proto_feed_more(void *P)
531
{
532
  struct proto *p = P;
533
534 075898de Martin Mares
  if (p->core_state != FS_FEEDING)
535
    return;
536 fbde6c39 Ondrej Zajicek
537
  DBG("Feeding protocol %s continued\n", p->name);
538 ac5d8012 Martin Mares
  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 67bd949a Martin Mares
proto_feed(void *P)
553
{
554
  struct proto *p = P;
555
556 fbde6c39 Ondrej Zajicek
  if (p->core_state != FS_FEEDING)
557
    return;
558
559 67bd949a Martin Mares
  DBG("Feeding protocol %s\n", p->name);
560 0e02abfd Martin Mares
  proto_add_announce_hook(p, p->table);
561 67bd949a Martin Mares
  if_feed_baby(p);
562 ac5d8012 Martin Mares
  proto_feed_more(P);
563 67bd949a Martin Mares
}
564
565 d6a836f8 Ondrej Zajicek
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 3c6269b8 Martin Mares
/**
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 d6a836f8 Ondrej Zajicek
 * 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 3c6269b8 Martin Mares
 */
603 67bd949a Martin Mares
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 d6a836f8 Ondrej Zajicek
  p->proto_state = ps;
614
615 67bd949a Martin Mares
  switch (ps)
616
    {
617
    case PS_DOWN:
618 b807ef9a Ondrej Zajicek
      if ((cs = FS_FEEDING) || (cs == FS_HAPPY))
619
        proto_schedule_flush(p);
620
621 d6a836f8 Ondrej Zajicek
      neigh_prune(); // FIXME convert neighbors to resource?
622
      rfree(p->pool);
623
      p->pool = NULL;
624
625 67bd949a Martin Mares
      if (cs == FS_HUNGRY)                /* Shutdown finished */
626 50fe90ed Martin Mares
        {
627
          proto_fell_down(p);
628
          return;                        /* The protocol might have ceased to exist */
629
        }
630 67bd949a Martin Mares
      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 d6a836f8 Ondrej Zajicek
      proto_schedule_feed(p);
639 67bd949a Martin Mares
      break;
640
    case PS_STOP:
641 d6a836f8 Ondrej Zajicek
      if ((cs = FS_FEEDING) || (cs == FS_HAPPY))
642
        proto_schedule_flush(p);
643 f4aabcee Martin Mares
      break;
644 67bd949a Martin Mares
    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 0432c017 Martin Mares
}
648 1a54b1c6 Martin Mares
649 8f6accb5 Martin Mares
static void
650 7c103b1e Martin Mares
proto_flush_all(void *unused UNUSED)
651 1a54b1c6 Martin Mares
{
652
  struct proto *p;
653
654 0e02abfd Martin Mares
  rt_prune_all();
655 1a54b1c6 Martin Mares
  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 d6a836f8 Ondrej Zajicek
      if (p->proto_state == PS_DOWN)
661
        proto_fell_down(p);
662 1a54b1c6 Martin Mares
    }
663
}
664 ae97b946 Martin Mares
665 0d3e6bce Martin Mares
/*
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 1d2664a4 Martin Mares
proto_do_show(struct proto *p, int verbose)
690
{
691 9685deb9 Martin Mares
  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 caab3bb3 Martin Mares
  cli_msg(-1002, "%-8s %-8s %-8s %-5s %-5s  %s",
698 1d2664a4 Martin Mares
          p->name,
699
          p->proto->name,
700
          p->table->name,
701
          proto_state_name(p),
702 9685deb9 Martin Mares
          reltime,
703
          buf);
704 1d2664a4 Martin Mares
  if (verbose)
705
    {
706 e04555c0 Ondrej Zajicek
      if (p->cf->dsc)
707
        cli_msg(-1006, "  Description:    %s", p->cf->dsc);
708 925fe2d3 Ondrej Zajicek
      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 1d2664a4 Martin Mares
    }
733
}
734
735 ae97b946 Martin Mares
void
736 0d3e6bce Martin Mares
proto_show(struct symbol *s, int verbose)
737 ae97b946 Martin Mares
{
738 1d2664a4 Martin Mares
  if (s && s->class != SYM_PROTO)
739
    {
740
      cli_msg(9002, "%s is not a protocol", s->name);
741
      return;
742
    }
743 caab3bb3 Martin Mares
  cli_msg(-2002, "name     proto    table    state since  info");
744 1d2664a4 Martin Mares
  if (s)
745
    proto_do_show(((struct proto_config *)s->def)->proto, verbose);
746
  else
747
    {
748 f14a4bec Martin Mares
      WALK_PROTO_LIST(p)
749
        proto_do_show(p, verbose);
750
      WALK_PROTO_LIST_END;
751 1d2664a4 Martin Mares
    }
752 ae97b946 Martin Mares
  cli_msg(0, "");
753
}
754 02c1fbdd Martin Mares
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 f14a4bec Martin Mares
      WALK_LIST(q, active_proto_list)
772 02c1fbdd Martin Mares
        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 f14a4bec Martin Mares
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 96d8e3bf Martin Mares
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
}