]> git.ipfire.org Git - thirdparty/bird.git/blame - nest/proto.c
Extends multipath support for OSPF.
[thirdparty/bird.git] / nest / proto.c
CommitLineData
2326b001
MM
1/*
2 * BIRD -- Protocols
3 *
50fe90ed 4 * (c) 1998--2000 Martin Mares <mj@ucw.cz>
2326b001
MM
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL.
7 */
8
6b9fa320 9#undef LOCAL_DEBUG
7f4a3988 10
2326b001
MM
11#include "nest/bird.h"
12#include "nest/protocol.h"
13#include "lib/resource.h"
14#include "lib/lists.h"
67bd949a 15#include "lib/event.h"
f14a4bec 16#include "lib/string.h"
fe7cec12 17#include "conf/conf.h"
47b79306
MM
18#include "nest/route.h"
19#include "nest/iface.h"
ae97b946 20#include "nest/cli.h"
529c4149 21#include "filter/filter.h"
2326b001 22
acb60628 23pool *proto_pool;
67bd949a 24
3991d84e 25static list protocol_list;
f14a4bec 26static list proto_list;
67bd949a 27
839380d7
MM
28#define PD(pr, msg, args...) do { if (pr->debug & D_STATES) { log(L_TRACE "%s: " msg, pr->name , ## args); } } while(0)
29
f14a4bec 30list active_proto_list;
67bd949a
MM
31static list inactive_proto_list;
32static list initial_proto_list;
1a54b1c6 33static list flush_proto_list;
4ef09506 34static struct proto *initial_device_proto;
1a54b1c6
MM
35
36static event *proto_flush_event;
ebecb6f6 37static timer *proto_shutdown_timer;
0c791f87
OZ
38static timer *gr_wait_timer;
39
40#define GRS_NONE 0
41#define GRS_INIT 1
42#define GRS_ACTIVE 2
43#define GRS_DONE 3
44
45static int graceful_restart_state;
46static u32 graceful_restart_locks;
67bd949a
MM
47
48static char *p_states[] = { "DOWN", "START", "UP", "STOP" };
0c791f87 49static char *c_states[] = { "HUNGRY", "???", "HAPPY", "FLUSHING" };
67bd949a 50
fb829de6 51static void proto_flush_loop(void *);
ebecb6f6 52static void proto_shutdown_loop(struct timer *);
50fe90ed 53static void proto_rethink_goal(struct proto *p);
6eda3f13
OZ
54static void proto_want_export_up(struct proto *p);
55static void proto_fell_down(struct proto *p);
839380d7 56static char *proto_state_name(struct proto *p);
1a54b1c6 57
b2280748 58static void
227af309 59proto_relink(struct proto *p)
67bd949a 60{
e81b440f 61 list *l = NULL;
1a54b1c6 62
1a54b1c6
MM
63 switch (p->core_state)
64 {
bf47fe4b
OZ
65 case FS_HUNGRY:
66 l = &inactive_proto_list;
67 break;
1a54b1c6 68 case FS_HAPPY:
f14a4bec 69 l = &active_proto_list;
1a54b1c6
MM
70 break;
71 case FS_FLUSHING:
72 l = &flush_proto_list;
73 break;
74 default:
bf47fe4b 75 ASSERT(0);
1a54b1c6 76 }
227af309
OZ
77
78 rem_node(&p->n);
79 add_tail(l, &p->n);
80}
81
82static void
83proto_log_state_change(struct proto *p)
84{
85 if (p->debug & D_STATES)
86 {
87 char *name = proto_state_name(p);
88 if (name != p->last_state_name_announced)
89 {
90 p->last_state_name_announced = name;
91 PD(p, "State changed to %s", proto_state_name(p));
92 }
93 }
94 else
95 p->last_state_name_announced = NULL;
67bd949a 96}
2326b001 97
227af309 98
3c6269b8
MM
99/**
100 * proto_new - create a new protocol instance
101 * @c: protocol configuration
102 * @size: size of protocol data structure (each protocol instance is represented by
103 * a structure starting with generic part [struct &proto] and continued
104 * with data specific to the protocol)
105 *
106 * When a new configuration has been read in, the core code starts
2e9b2421 107 * initializing all the protocol instances configured by calling their
3c6269b8
MM
108 * init() hooks with the corresponding instance configuration. The initialization
109 * code of the protocol is expected to create a new instance according to the
110 * configuration by calling this function and then modifying the default settings
111 * to values wanted by the protocol.
112 */
7f4a3988 113void *
31b3e1bb 114proto_new(struct proto_config *c, unsigned size)
7f4a3988 115{
1d2664a4 116 struct protocol *pr = c->protocol;
8fe48f13 117 struct proto *p = mb_allocz(proto_pool, size);
7f4a3988 118
31b3e1bb
MM
119 p->cf = c;
120 p->debug = c->debug;
cf31112f 121 p->mrtdump = c->mrtdump;
67bd949a 122 p->name = c->name;
31b3e1bb
MM
123 p->preference = c->preference;
124 p->disabled = c->disabled;
7f4a3988 125 p->proto = pr;
9d885689 126 p->table = c->table->table;
7293c5dd 127 p->hash_key = random_u32();
1d2664a4 128 c->proto = p;
7f4a3988
MM
129 return p;
130}
131
1a54b1c6
MM
132static void
133proto_init_instance(struct proto *p)
134{
5bc512aa
MM
135 /* Here we cannot use p->cf->name since it won't survive reconfiguration */
136 p->pool = rp_new(proto_pool, p->proto->name);
1a54b1c6
MM
137 p->attn = ev_new(p->pool);
138 p->attn->data = p;
c0adf7e9 139
0c791f87
OZ
140 if (graceful_restart_state == GRS_INIT)
141 p->gr_recovery = 1;
142
c0adf7e9
OZ
143 if (! p->proto->multitable)
144 rt_lock_table(p->table);
1a54b1c6
MM
145}
146
c0adf7e9 147extern pool *rt_table_pool;
3c6269b8
MM
148/**
149 * proto_add_announce_hook - connect protocol to a routing table
150 * @p: protocol instance
151 * @t: routing table to connect to
c0adf7e9 152 * @stats: per-table protocol statistics
3c6269b8 153 *
6eda3f13
OZ
154 * This function creates a connection between the protocol instance @p and the
155 * routing table @t, making the protocol hear all changes in the table.
3c6269b8 156 *
6eda3f13
OZ
157 * The announce hook is linked in the protocol ahook list. Announce hooks are
158 * allocated from the routing table resource pool and when protocol accepts
159 * routes also in the table ahook list. The are linked to the table ahook list
160 * and unlinked from it depending on export_state (in proto_want_export_up() and
161 * proto_want_export_down()) and they are automatically freed after the protocol
162 * is flushed (in proto_fell_down()).
c0adf7e9 163 *
6eda3f13
OZ
164 * Unless you want to listen to multiple routing tables (as the Pipe protocol
165 * does), you needn't to worry about this function since the connection to the
166 * protocol's primary routing table is initialized automatically by the core
167 * code.
3c6269b8 168 */
0e02abfd 169struct announce_hook *
ebecb6f6 170proto_add_announce_hook(struct proto *p, struct rtable *t, struct proto_stats *stats)
0e02abfd
MM
171{
172 struct announce_hook *h;
173
0e02abfd 174 DBG("Connecting protocol %s to table %s\n", p->name, t->name);
839380d7 175 PD(p, "Connected to table %s", t->name);
c0adf7e9
OZ
176
177 h = mb_allocz(rt_table_pool, sizeof(struct announce_hook));
0e02abfd
MM
178 h->table = t;
179 h->proto = p;
c0adf7e9
OZ
180 h->stats = stats;
181
0e02abfd
MM
182 h->next = p->ahooks;
183 p->ahooks = h;
c0adf7e9 184
6eda3f13 185 if (p->rt_notify && (p->export_state != ES_DOWN))
c0adf7e9 186 add_tail(&t->hooks, &h->n);
0e02abfd
MM
187 return h;
188}
189
c0adf7e9
OZ
190/**
191 * proto_find_announce_hook - find announce hooks
192 * @p: protocol instance
193 * @t: routing table
194 *
195 * Returns pointer to announce hook or NULL
196 */
197struct announce_hook *
198proto_find_announce_hook(struct proto *p, struct rtable *t)
199{
200 struct announce_hook *a;
201
202 for (a = p->ahooks; a; a = a->next)
203 if (a->table == t)
204 return a;
205
206 return NULL;
207}
208
0c791f87
OZ
209static void
210proto_link_ahooks(struct proto *p)
211{
212 struct announce_hook *h;
213
214 if (p->rt_notify)
215 for(h=p->ahooks; h; h=h->next)
216 add_tail(&h->table->hooks, &h->n);
217}
218
0e02abfd 219static void
c0adf7e9 220proto_unlink_ahooks(struct proto *p)
0e02abfd
MM
221{
222 struct announce_hook *h;
223
c0adf7e9
OZ
224 if (p->rt_notify)
225 for(h=p->ahooks; h; h=h->next)
226 rem_node(&h->n);
227}
228
229static void
230proto_free_ahooks(struct proto *p)
231{
232 struct announce_hook *h, *hn;
233
234 for(h = p->ahooks; h; h = hn)
235 {
236 hn = h->next;
237 mb_free(h);
238 }
239
0e02abfd 240 p->ahooks = NULL;
c0adf7e9 241 p->main_ahook = NULL;
0e02abfd
MM
242}
243
094d2bdb 244
3c6269b8
MM
245/**
246 * proto_config_new - create a new protocol configuration
247 * @pr: protocol the configuration will belong to
248 * @size: size of the structure including generic data
a7f23f58 249 * @class: SYM_PROTO or SYM_TEMPLATE
3c6269b8
MM
250 *
251 * Whenever the configuration file says that a new instance
252 * of a routing protocol should be created, the parser calls
253 * proto_config_new() to create a configuration entry for this
254 * instance (a structure staring with the &proto_config header
255 * containing all the generic items followed by protocol-specific
256 * ones). Also, the configuration entry gets added to the list
257 * of protocol instances kept in the configuration.
a7f23f58
OZ
258 *
259 * The function is also used to create protocol templates (when class
260 * SYM_TEMPLATE is specified), the only difference is that templates
261 * are not added to the list of protocol instances and therefore not
262 * initialized during protos_commit()).
3c6269b8 263 */
31b3e1bb 264void *
a7f23f58 265proto_config_new(struct protocol *pr, unsigned size, int class)
31b3e1bb
MM
266{
267 struct proto_config *c = cfg_allocz(size);
268
a7f23f58
OZ
269 if (class == SYM_PROTO)
270 add_tail(&new_config->protos, &c->n);
31b3e1bb 271 c->global = new_config;
1d2664a4 272 c->protocol = pr;
31b3e1bb 273 c->name = pr->name;
39c028e9 274 c->preference = pr->preference;
a7f23f58 275 c->class = class;
5056c559 276 c->out_filter = FILTER_REJECT;
9d885689 277 c->table = c->global->master_rtc;
839380d7 278 c->debug = new_config->proto_default_debug;
cf31112f 279 c->mrtdump = new_config->proto_default_mrtdump;
31b3e1bb
MM
280 return c;
281}
282
a7f23f58
OZ
283/**
284 * proto_copy_config - copy a protocol configuration
285 * @dest: destination protocol configuration
286 * @src: source protocol configuration
287 *
288 * Whenever a new instance of a routing protocol is created from the
289 * template, proto_copy_config() is called to copy a content of
290 * the source protocol configuration to the new protocol configuration.
291 * Name, class and a node in protos list of @dest are kept intact.
292 * copy_config() protocol hook is used to copy protocol-specific data.
293 */
294void
295proto_copy_config(struct proto_config *dest, struct proto_config *src)
296{
297 node old_node;
298 int old_class;
299 char *old_name;
300
301 if (dest->protocol != src->protocol)
302 cf_error("Can't copy configuration from a different protocol type");
303
304 if (dest->protocol->copy_config == NULL)
305 cf_error("Inheriting configuration for %s is not supported", src->protocol->name);
306
307 DBG("Copying configuration from %s to %s\n", src->name, dest->name);
308
309 /*
310 * Copy struct proto_config here. Keep original node, class and name.
311 * protocol-specific config copy is handled by protocol copy_config() hook
312 */
313
314 old_node = dest->n;
315 old_class = dest->class;
316 old_name = dest->name;
317
318 memcpy(dest, src, sizeof(struct proto_config));
319
320 dest->n = old_node;
321 dest->class = old_class;
322 dest->name = old_name;
323
324 dest->protocol->copy_config(dest, src);
325}
326
3c6269b8
MM
327/**
328 * protos_preconfig - pre-configuration processing
329 * @c: new configuration
330 *
331 * This function calls the preconfig() hooks of all routing
332 * protocols available to prepare them for reading of the new
333 * configuration.
334 */
2326b001 335void
31b3e1bb 336protos_preconfig(struct config *c)
2326b001 337{
7f4a3988
MM
338 struct protocol *p;
339
7c0cc76e 340 init_list(&c->protos);
6b9fa320 341 DBG("Protocol preconfig:");
7f4a3988
MM
342 WALK_LIST(p, protocol_list)
343 {
6b9fa320 344 DBG(" %s", p->name);
4ba84ebc 345 p->name_counter = 0;
3629bcf0 346 if (p->preconfig)
31b3e1bb 347 p->preconfig(p, c);
7f4a3988 348 }
6b9fa320 349 DBG("\n");
7f4a3988
MM
350}
351
3c6269b8
MM
352/**
353 * protos_postconfig - post-configuration processing
354 * @c: new configuration
355 *
356 * This function calls the postconfig() hooks of all protocol
a7f23f58
OZ
357 * instances specified in configuration @c. The hooks are not
358 * called for protocol templates.
3c6269b8 359 */
7f4a3988 360void
31b3e1bb 361protos_postconfig(struct config *c)
7f4a3988 362{
31b3e1bb 363 struct proto_config *x;
7f4a3988
MM
364 struct protocol *p;
365
6b9fa320 366 DBG("Protocol postconfig:");
31b3e1bb 367 WALK_LIST(x, c->protos)
7f4a3988 368 {
6b9fa320 369 DBG(" %s", x->name);
b662290f 370
1d2664a4 371 p = x->protocol;
3629bcf0 372 if (p->postconfig)
31b3e1bb
MM
373 p->postconfig(x);
374 }
6b9fa320 375 DBG("\n");
31b3e1bb
MM
376}
377
4ef09506
OZ
378extern struct protocol proto_unix_iface;
379
50fe90ed
MM
380static struct proto *
381proto_init(struct proto_config *c)
382{
383 struct protocol *p = c->protocol;
384 struct proto *q = p->init(c);
385
386 q->proto_state = PS_DOWN;
387 q->core_state = FS_HUNGRY;
0c791f87 388 q->export_state = ES_DOWN;
5ebc9293
OZ
389 q->last_state_change = now;
390
227af309
OZ
391 add_tail(&initial_proto_list, &q->n);
392
4ef09506
OZ
393 if (p == &proto_unix_iface)
394 initial_device_proto = q;
395
f14a4bec 396 add_tail(&proto_list, &q->glob_node);
498c3339 397 PD(q, "Initializing%s", q->disabled ? " [disabled]" : "");
50fe90ed
MM
398 return q;
399}
400
c0adf7e9
OZ
401int proto_reconfig_type; /* Hack to propagate type info to pipe reconfigure hook */
402
ebae4770
OZ
403static int
404proto_reconfigure(struct proto *p, struct proto_config *oc, struct proto_config *nc, int type)
405{
ec57bbf6 406 struct announce_hook *ah = p->main_ahook;
ebae4770
OZ
407 /* If the protocol is DOWN, we just restart it */
408 if (p->proto_state == PS_DOWN)
409 return 0;
410
411 /* If there is a too big change in core attributes, ... */
412 if ((nc->protocol != oc->protocol) ||
23fd4644 413 (nc->disabled != p->disabled) ||
79b4e12e 414 (nc->table->table != oc->table->table))
ebae4770
OZ
415 return 0;
416
ebae4770
OZ
417 p->debug = nc->debug;
418 p->mrtdump = nc->mrtdump;
c0adf7e9 419 proto_reconfig_type = type;
ebae4770
OZ
420
421 /* Execute protocol specific reconfigure hook */
422 if (! (p->proto->reconfigure && p->proto->reconfigure(p, nc)))
423 return 0;
424
425 DBG("\t%s: same\n", oc->name);
426 PD(p, "Reconfigured");
427 p->cf = nc;
428 p->name = nc->name;
5a56f27c 429 p->preference = nc->preference;
ebae4770 430
c0adf7e9
OZ
431
432 /* Multitable protocols handle rest in their reconfigure hooks */
433 if (p->proto->multitable)
434 return 1;
435
d9b77cc2
OZ
436 /* Update filters and limits in the main announce hook
437 Note that this also resets limit state */
ec57bbf6 438 if (ah)
c0adf7e9 439 {
ec57bbf6
OF
440 ah->in_filter = nc->in_filter;
441 ah->out_filter = nc->out_filter;
442 ah->rx_limit = nc->rx_limit;
443 ah->in_limit = nc->in_limit;
444 ah->out_limit = nc->out_limit;
445 ah->in_keep_filtered = nc->in_keep_filtered;
446
447 if (p->proto_state == PS_UP) /* Recheck export/import/receive limit */
448 {
449 struct proto_stats *stats = ah->stats;
450 struct proto_limit *l = ah->in_limit;
451 u32 all_routes = stats->imp_routes + stats->filt_routes;
452
453 if (l && (stats->imp_routes >= l->limit)) proto_notify_limit(ah, l, PLD_IN, stats->imp_routes);
454
455 l = ah->rx_limit;
456
457 if (l && ( all_routes >= l->limit)) proto_notify_limit(ah, l, PLD_RX, all_routes );
458
459 l = ah->out_limit;
460
461 if (l && ( stats->exp_routes >= l->limit)) proto_notify_limit(ah, l, PLD_OUT, stats->exp_routes);
462 }
c0adf7e9
OZ
463 }
464
465 /* Update routes when filters changed. If the protocol in not UP,
466 it has no routes and we can ignore such changes */
467 if ((p->proto_state != PS_UP) || (type == RECONFIG_SOFT))
468 return 1;
469
470 int import_changed = ! filter_same(nc->in_filter, oc->in_filter);
471 int export_changed = ! filter_same(nc->out_filter, oc->out_filter);
472
473 /* We treat a change in preferences by reimporting routes */
474 if (nc->preference != oc->preference)
475 import_changed = 1;
476
76b53a4e
OZ
477 if (import_changed || export_changed)
478 log(L_INFO "Reloading protocol %s", p->name);
479
c0adf7e9
OZ
480 /* If import filter changed, call reload hook */
481 if (import_changed && ! (p->reload_routes && p->reload_routes(p)))
ebae4770
OZ
482 {
483 /* Now, the protocol is reconfigured. But route reload failed
484 and we have to do regular protocol restart. */
76b53a4e 485 log(L_INFO "Restarting protocol %s", p->name);
ebae4770 486 p->disabled = 1;
ebecb6f6 487 p->down_code = PDC_CF_RESTART;
ebae4770
OZ
488 proto_rethink_goal(p);
489 p->disabled = 0;
490 proto_rethink_goal(p);
491 return 1;
492 }
493
494 if (export_changed)
495 proto_request_feeding(p);
496
497 return 1;
498}
499
3c6269b8
MM
500/**
501 * protos_commit - commit new protocol configuration
502 * @new: new configuration
503 * @old: old configuration or %NULL if it's boot time config
504 * @force_reconfig: force restart of all protocols (used for example
505 * when the router ID changes)
bf1aec97 506 * @type: type of reconfiguration (RECONFIG_SOFT or RECONFIG_HARD)
3c6269b8
MM
507 *
508 * Scan differences between @old and @new configuration and adjust all
509 * protocol instances to conform to the new configuration.
510 *
511 * When a protocol exists in the new configuration, but it doesn't in the
512 * original one, it's immediately started. When a collision with the other
513 * running protocol would arise, the new protocol will be temporarily stopped
514 * by the locking mechanism.
515 *
516 * When a protocol exists in the old configuration, but it doesn't in the
517 * new one, it's shut down and deleted after the shutdown completes.
518 *
bf1aec97
OZ
519 * When a protocol exists in both configurations, the core decides
520 * whether it's possible to reconfigure it dynamically - it checks all
521 * the core properties of the protocol (changes in filters are ignored
522 * if type is RECONFIG_SOFT) and if they match, it asks the
523 * reconfigure() hook of the protocol to see if the protocol is able
524 * to switch to the new configuration. If it isn't possible, the
525 * protocol is shut down and a new instance is started with the new
526 * configuration after the shutdown is completed.
3c6269b8 527 */
31b3e1bb 528void
bf1aec97 529protos_commit(struct config *new, struct config *old, int force_reconfig, int type)
31b3e1bb 530{
50fe90ed
MM
531 struct proto_config *oc, *nc;
532 struct proto *p, *n;
a7f23f58 533 struct symbol *sym;
31b3e1bb 534
50fe90ed
MM
535 DBG("protos_commit:\n");
536 if (old)
31b3e1bb 537 {
50fe90ed
MM
538 WALK_LIST(oc, old->protos)
539 {
a7f23f58
OZ
540 p = oc->proto;
541 sym = cf_find_symbol(oc->name);
bf8558bc 542 if (sym && sym->class == SYM_PROTO && !new->shutdown)
50fe90ed
MM
543 {
544 /* Found match, let's check if we can smoothly switch to new configuration */
e04555c0 545 /* No need to check description */
50fe90ed 546 nc = sym->def;
ebae4770
OZ
547 nc->proto = p;
548
549 /* We will try to reconfigure protocol p */
550 if (! force_reconfig && proto_reconfigure(p, oc, nc, type))
551 continue;
552
553 /* Unsuccessful, we will restart it */
76b53a4e
OZ
554 if (!p->disabled && !nc->disabled)
555 log(L_INFO "Restarting protocol %s", p->name);
556 else if (p->disabled && !nc->disabled)
557 log(L_INFO "Enabling protocol %s", p->name);
558 else if (!p->disabled && nc->disabled)
559 log(L_INFO "Disabling protocol %s", p->name);
560
ebecb6f6 561 p->down_code = nc->disabled ? PDC_CF_DISABLE : PDC_CF_RESTART;
50fe90ed
MM
562 p->cf_new = nc;
563 }
a92cf57d 564 else if (!new->shutdown)
50fe90ed 565 {
5400c0e7 566 log(L_INFO "Removing protocol %s", p->name);
ebecb6f6 567 p->down_code = PDC_CF_REMOVE;
50fe90ed
MM
568 p->cf_new = NULL;
569 }
5400c0e7
OZ
570 else /* global shutdown */
571 {
572 p->down_code = PDC_CMD_SHUTDOWN;
573 p->cf_new = NULL;
574 }
ebecb6f6 575
5400c0e7 576 p->reconfiguring = 1;
50fe90ed
MM
577 config_add_obstacle(old);
578 proto_rethink_goal(p);
579 }
7f4a3988 580 }
50fe90ed
MM
581
582 WALK_LIST(nc, new->protos)
583 if (!nc->proto)
584 {
a92cf57d 585 if (old) /* Not a first-time configuration */
76b53a4e 586 log(L_INFO "Adding protocol %s", nc->name);
50fe90ed
MM
587 proto_init(nc);
588 }
589 DBG("\tdone\n");
590
591 DBG("Protocol start\n");
4ef09506
OZ
592
593 /* Start device protocol first */
594 if (initial_device_proto)
595 {
596 proto_rethink_goal(initial_device_proto);
597 initial_device_proto = NULL;
598 }
599
79b4e12e
OZ
600 /* Determine router ID for the first time - it has to be here and not in
601 global_commit() because it is postponed after start of device protocol */
602 if (!config->router_id)
603 {
604 config->router_id = if_choose_router_id(config->router_id_from, 0);
605 if (!config->router_id)
606 die("Cannot determine router ID, please configure it manually");
607 }
608
609 /* Start all other protocols */
50fe90ed
MM
610 WALK_LIST_DELSAFE(p, n, initial_proto_list)
611 proto_rethink_goal(p);
7f4a3988
MM
612}
613
47b79306 614static void
67bd949a 615proto_rethink_goal(struct proto *p)
47b79306 616{
50fe90ed 617 struct protocol *q;
0c791f87 618 byte goal;
50fe90ed
MM
619
620 if (p->reconfiguring && p->core_state == FS_HUNGRY && p->proto_state == PS_DOWN)
621 {
622 struct proto_config *nc = p->cf_new;
623 DBG("%s has shut down for reconfiguration\n", p->name);
624 config_del_obstacle(p->cf->global);
625 rem_node(&p->n);
f14a4bec 626 rem_node(&p->glob_node);
50fe90ed
MM
627 mb_free(p);
628 if (!nc)
629 return;
f098e072 630 p = proto_init(nc);
50fe90ed
MM
631 }
632
633 /* Determine what state we want to reach */
bf8558bc 634 if (p->disabled || p->reconfiguring)
0c791f87 635 goal = PS_DOWN;
50fe90ed 636 else
0c791f87 637 goal = PS_UP;
50fe90ed
MM
638
639 q = p->proto;
0c791f87 640 if (goal == PS_UP) /* Going up */
67bd949a 641 {
0c791f87 642 if (p->proto_state == PS_DOWN && p->core_state == FS_HUNGRY)
67bd949a
MM
643 {
644 DBG("Kicking %s up\n", p->name);
839380d7 645 PD(p, "Starting");
1a54b1c6 646 proto_init_instance(p);
67bd949a
MM
647 proto_notify_state(p, (q->start ? q->start(p) : PS_UP));
648 }
649 }
650 else /* Going down */
651 {
652 if (p->proto_state == PS_START || p->proto_state == PS_UP)
653 {
654 DBG("Kicking %s down\n", p->name);
839380d7 655 PD(p, "Shutting down");
67bd949a
MM
656 proto_notify_state(p, (q->shutdown ? q->shutdown(p) : PS_DOWN));
657 }
658 }
659}
660
0c791f87 661
6eda3f13
OZ
662/**
663 * DOC: Graceful restart recovery
664 *
665 * Graceful restart of a router is a process when the routing plane (e.g. BIRD)
666 * restarts but both the forwarding plane (e.g kernel routing table) and routing
667 * neighbors keep proper routes, and therefore uninterrupted packet forwarding
668 * is maintained.
669 *
670 * BIRD implements graceful restart recovery by deferring export of routes to
671 * protocols until routing tables are refilled with the expected content. After
672 * start, protocols generate routes as usual, but routes are not propagated to
673 * them, until protocols report that they generated all routes. After that,
674 * graceful restart recovery is finished and the export (and the initial feed)
675 * to protocols is enabled.
676 *
677 * When graceful restart recovery need is detected during initialization, then
678 * enabled protocols are marked with @gr_recovery flag before start. Such
679 * protocols then decide how to proceed with graceful restart, participation is
680 * voluntary. Protocols could lock the recovery by proto_graceful_restart_lock()
681 * (stored in @gr_lock flag), which means that they want to postpone the end of
682 * the recovery until they converge and then unlock it. They also could set
683 * @gr_wait before advancing to %PS_UP, which means that the core should defer
684 * route export to that protocol until the end of the recovery. This should be
685 * done by protocols that expect their neigbors to keep the proper routes
686 * (kernel table, BGP sessions with BGP graceful restart capability).
687 *
688 * The graceful restart recovery is finished when either all graceful restart
689 * locks are unlocked or when graceful restart wait timer fires.
690 *
691 */
0c791f87 692
6eda3f13 693static void graceful_restart_done(struct timer *t);
0c791f87 694
6eda3f13
OZ
695/**
696 * graceful_restart_recovery - request initial graceful restart recovery
697 *
698 * Called by the platform initialization code if the need for recovery
699 * after graceful restart is detected during boot. Have to be called
700 * before protos_commit().
701 */
0c791f87
OZ
702void
703graceful_restart_recovery(void)
704{
705 graceful_restart_state = GRS_INIT;
706}
707
6eda3f13
OZ
708/**
709 * graceful_restart_init - initialize graceful restart
710 *
711 * When graceful restart recovery was requested, the function starts an active
712 * phase of the recovery and initializes graceful restart wait timer. The
713 * function have to be called after protos_commit().
714 */
0c791f87
OZ
715void
716graceful_restart_init(void)
717{
718 if (!graceful_restart_state)
719 return;
720
721 log(L_INFO "Graceful restart started");
722
723 if (!graceful_restart_locks)
724 {
725 graceful_restart_done(NULL);
726 return;
727 }
728
729 graceful_restart_state = GRS_ACTIVE;
730 gr_wait_timer = tm_new(proto_pool);
731 gr_wait_timer->hook = graceful_restart_done;
732 tm_start(gr_wait_timer, config->gr_wait);
733}
734
6eda3f13
OZ
735/**
736 * graceful_restart_done - finalize graceful restart
737 *
738 * When there are no locks on graceful restart, the functions finalizes the
739 * graceful restart recovery. Protocols postponing route export until the end of
740 * the recovery are awakened and the export to them is enabled. All other
741 * related state is cleared. The function is also called when the graceful
742 * restart wait timer fires (but there are still some locks).
743 */
0c791f87
OZ
744static void
745graceful_restart_done(struct timer *t UNUSED)
746{
747 struct proto *p;
748 node *n;
749
750 log(L_INFO "Graceful restart done");
751 graceful_restart_state = GRS_DONE;
752
753 WALK_LIST2(p, n, proto_list, glob_node)
754 {
755 if (!p->gr_recovery)
756 continue;
757
758 /* Resume postponed export of routes */
759 if ((p->proto_state == PS_UP) && p->gr_wait)
227af309 760 {
0c791f87 761 proto_want_export_up(p);
227af309
OZ
762 proto_log_state_change(p);
763 }
0c791f87
OZ
764
765 /* Cleanup */
766 p->gr_recovery = 0;
767 p->gr_wait = 0;
768 p->gr_lock = 0;
769 }
770
771 graceful_restart_locks = 0;
772}
773
774void
775graceful_restart_show_status(void)
776{
777 if (graceful_restart_state != GRS_ACTIVE)
778 return;
779
780 cli_msg(-24, "Graceful restart recovery in progress");
781 cli_msg(-24, " Waiting for %d protocols to recover", graceful_restart_locks);
782 cli_msg(-24, " Wait timer is %d/%d", tm_remains(gr_wait_timer), config->gr_wait);
783}
784
6eda3f13
OZ
785/**
786 * proto_graceful_restart_lock - lock graceful restart by protocol
787 * @p: protocol instance
788 *
789 * This function allows a protocol to postpone the end of graceful restart
790 * recovery until it converges. The lock is removed when the protocol calls
791 * proto_graceful_restart_unlock() or when the protocol is stopped.
792 *
793 * The function have to be called during the initial phase of graceful restart
794 * recovery and only for protocols that are part of graceful restart (i.e. their
795 * @gr_recovery is set), which means it should be called from protocol start
796 * hooks.
797 */
0c791f87
OZ
798void
799proto_graceful_restart_lock(struct proto *p)
800{
801 ASSERT(graceful_restart_state == GRS_INIT);
802 ASSERT(p->gr_recovery);
803
804 if (p->gr_lock)
805 return;
806
807 p->gr_lock = 1;
808 graceful_restart_locks++;
809}
810
6eda3f13
OZ
811/**
812 * proto_graceful_restart_unlock - unlock graceful restart by protocol
813 * @p: protocol instance
814 *
815 * This function unlocks a lock from proto_graceful_restart_lock(). It is also
816 * automatically called when the lock holding protocol went down.
817 */
0c791f87
OZ
818void
819proto_graceful_restart_unlock(struct proto *p)
820{
821 if (!p->gr_lock)
822 return;
823
824 p->gr_lock = 0;
825 graceful_restart_locks--;
826
827 if ((graceful_restart_state == GRS_ACTIVE) && !graceful_restart_locks)
828 tm_start(gr_wait_timer, 0);
829}
830
831
832
3c6269b8
MM
833/**
834 * protos_dump_all - dump status of all protocols
835 *
836 * This function dumps status of all existing protocol instances to the
837 * debug output. It involves printing of general status information
838 * such as protocol states, its position on the protocol lists
839 * and also calling of a dump() hook of the protocol to print
840 * the internals.
841 */
87d2be86
PM
842void
843protos_dump_all(void)
844{
845 struct proto *p;
c0adf7e9 846 struct announce_hook *a;
87d2be86
PM
847
848 debug("Protocols:\n");
849
f14a4bec 850 WALK_LIST(p, active_proto_list)
87d2be86 851 {
3ea1ba63 852 debug(" protocol %s state %s/%s\n", p->name,
b2280748 853 p_states[p->proto_state], c_states[p->core_state]);
c0adf7e9
OZ
854 for (a = p->ahooks; a; a = a->next)
855 {
856 debug("\tTABLE %s\n", a->table->name);
857 if (a->in_filter)
858 debug("\tInput filter: %s\n", filter_name(a->in_filter));
859 if (a->out_filter != FILTER_REJECT)
860 debug("\tOutput filter: %s\n", filter_name(a->out_filter));
861 }
66efdf96
MM
862 if (p->disabled)
863 debug("\tDISABLED\n");
31b3e1bb
MM
864 else if (p->proto->dump)
865 p->proto->dump(p);
87d2be86 866 }
47b79306 867 WALK_LIST(p, inactive_proto_list)
67bd949a
MM
868 debug(" inactive %s: state %s/%s\n", p->name, p_states[p->proto_state], c_states[p->core_state]);
869 WALK_LIST(p, initial_proto_list)
870 debug(" initial %s\n", p->name);
f14a4bec
MM
871 WALK_LIST(p, flush_proto_list)
872 debug(" flushing %s\n", p->name);
87d2be86
PM
873}
874
3c6269b8
MM
875/**
876 * proto_build - make a single protocol available
877 * @p: the protocol
878 *
879 * After the platform specific initialization code uses protos_build()
880 * to add all the standard protocols, it should call proto_build() for
2e9b2421 881 * all platform specific protocols to inform the core that they exist.
3c6269b8 882 */
3991d84e
MM
883void
884proto_build(struct protocol *p)
885{
886 add_tail(&protocol_list, &p->n);
887 if (p->attr_class)
888 {
889 ASSERT(!attr_class_to_protocol[p->attr_class]);
890 attr_class_to_protocol[p->attr_class] = p;
891 }
892}
893
1ec52253
OZ
894/* FIXME: convert this call to some protocol hook */
895extern void bfd_init_all(void);
896
3c6269b8
MM
897/**
898 * protos_build - build a protocol list
899 *
900 * This function is called during BIRD startup to insert
901 * all standard protocols to the global protocol list. Insertion
902 * of platform specific protocols (such as the kernel syncer)
903 * is in the domain of competence of the platform dependent
904 * startup code.
905 */
0432c017
MM
906void
907protos_build(void)
908{
909 init_list(&protocol_list);
471cc0be
MM
910 init_list(&proto_list);
911 init_list(&active_proto_list);
912 init_list(&inactive_proto_list);
913 init_list(&initial_proto_list);
914 init_list(&flush_proto_list);
3991d84e 915 proto_build(&proto_device);
93e868c7
OZ
916#ifdef CONFIG_RADV
917 proto_build(&proto_radv);
918#endif
18fff6a1 919#ifdef CONFIG_RIP
3991d84e 920 proto_build(&proto_rip);
18fff6a1
MM
921#endif
922#ifdef CONFIG_STATIC
3991d84e 923 proto_build(&proto_static);
c1f8dc91
OF
924#endif
925#ifdef CONFIG_OSPF
3991d84e 926 proto_build(&proto_ospf);
26368f65
MM
927#endif
928#ifdef CONFIG_PIPE
3991d84e 929 proto_build(&proto_pipe);
2638249d
MM
930#endif
931#ifdef CONFIG_BGP
3991d84e 932 proto_build(&proto_bgp);
18fff6a1 933#endif
1ec52253 934#ifdef CONFIG_BFD
6a8d3f1c 935 proto_build(&proto_bfd);
1ec52253
OZ
936 bfd_init_all();
937#endif
6a8d3f1c 938
67bd949a 939 proto_pool = rp_new(&root_pool, "Protocols");
1a54b1c6 940 proto_flush_event = ev_new(proto_pool);
fb829de6 941 proto_flush_event->hook = proto_flush_loop;
ebecb6f6
OZ
942 proto_shutdown_timer = tm_new(proto_pool);
943 proto_shutdown_timer->hook = proto_shutdown_loop;
67bd949a
MM
944}
945
ac5d8012
MM
946static void
947proto_feed_more(void *P)
948{
949 struct proto *p = P;
950
0c791f87 951 if (p->export_state != ES_FEEDING)
075898de 952 return;
fbde6c39
OZ
953
954 DBG("Feeding protocol %s continued\n", p->name);
ac5d8012
MM
955 if (rt_feed_baby(p))
956 {
0c791f87
OZ
957 DBG("Feeding protocol %s finished\n", p->name);
958 p->export_state = ES_READY;
227af309 959 proto_log_state_change(p);
0c791f87
OZ
960
961 if (p->feed_done)
962 p->feed_done(p);
ac5d8012
MM
963 }
964 else
965 {
966 p->attn->hook = proto_feed_more;
967 ev_schedule(p->attn); /* Will continue later... */
968 }
969}
970
8f6accb5 971static void
bf47fe4b 972proto_feed_initial(void *P)
67bd949a
MM
973{
974 struct proto *p = P;
975
0c791f87 976 if (p->export_state != ES_FEEDING)
fbde6c39
OZ
977 return;
978
67bd949a 979 DBG("Feeding protocol %s\n", p->name);
c0adf7e9 980
67bd949a 981 if_feed_baby(p);
ac5d8012 982 proto_feed_more(P);
67bd949a
MM
983}
984
d6a836f8 985static void
bf47fe4b 986proto_schedule_feed(struct proto *p, int initial)
d6a836f8
OZ
987{
988 DBG("%s: Scheduling meal\n", p->name);
8a7fb885 989
0c791f87
OZ
990 p->export_state = ES_FEEDING;
991 p->refeeding = !initial;
c0adf7e9 992
bf47fe4b 993 p->attn->hook = initial ? proto_feed_initial : proto_feed_more;
d6a836f8
OZ
994 ev_schedule(p->attn);
995}
996
fb829de6
OZ
997/*
998 * Flushing loop is responsible for flushing routes and protocols
999 * after they went down. It runs in proto_flush_event. At the start of
1000 * one round, protocols waiting to flush are marked in
1001 * proto_schedule_flush_loop(). At the end of the round (when routing
1002 * table flush is complete), marked protocols are flushed and a next
1003 * round may start.
1004 */
1005
1006static int flush_loop_state; /* 1 -> running */
1007
1008static void
1009proto_schedule_flush_loop(void)
1010{
1011 struct proto *p;
9135c1f0 1012 struct announce_hook *h;
fb829de6
OZ
1013
1014 if (flush_loop_state)
1015 return;
1016 flush_loop_state = 1;
1017
fb829de6 1018 WALK_LIST(p, flush_proto_list)
9135c1f0 1019 {
fb829de6 1020 p->flushing = 1;
9135c1f0 1021 for (h=p->ahooks; h; h=h->next)
0c791f87 1022 rt_mark_for_prune(h->table);
9135c1f0 1023 }
fb829de6
OZ
1024
1025 ev_schedule(proto_flush_event);
1026}
1027
1028static void
1029proto_flush_loop(void *unused UNUSED)
1030{
1031 struct proto *p;
1032
1033 if (! rt_prune_loop())
1034 {
1035 /* Rtable pruning is not finished */
1036 ev_schedule(proto_flush_event);
1037 return;
1038 }
1039
094d2bdb
OZ
1040 rt_prune_sources();
1041
fb829de6
OZ
1042 again:
1043 WALK_LIST(p, flush_proto_list)
1044 if (p->flushing)
1045 {
1046 /* This will flush interfaces in the same manner
1047 like rt_prune_all() flushes routes */
1048 if (p->proto == &proto_unix_iface)
1049 if_flush_ifaces(p);
1050
1051 DBG("Flushing protocol %s\n", p->name);
1052 p->flushing = 0;
227af309
OZ
1053 p->core_state = FS_HUNGRY;
1054 proto_relink(p);
1055 proto_log_state_change(p);
fb829de6
OZ
1056 if (p->proto_state == PS_DOWN)
1057 proto_fell_down(p);
1058 goto again;
1059 }
1060
1061 /* This round finished, perhaps there will be another one */
1062 flush_loop_state = 0;
1063 if (!EMPTY_LIST(flush_proto_list))
1064 proto_schedule_flush_loop();
1065}
1066
fb829de6 1067
d9b77cc2
OZ
1068/* Temporary hack to propagate restart to BGP */
1069int proto_restart;
fb829de6 1070
ebecb6f6
OZ
1071static void
1072proto_shutdown_loop(struct timer *t UNUSED)
1073{
1074 struct proto *p, *p_next;
1075
1076 WALK_LIST_DELSAFE(p, p_next, active_proto_list)
1077 if (p->down_sched)
1078 {
d9b77cc2 1079 proto_restart = (p->down_sched == PDS_RESTART);
ebecb6f6
OZ
1080
1081 p->disabled = 1;
1082 proto_rethink_goal(p);
d9b77cc2 1083 if (proto_restart)
ebecb6f6
OZ
1084 {
1085 p->disabled = 0;
1086 proto_rethink_goal(p);
1087 }
1088 }
1089}
1090
1091static inline void
1092proto_schedule_down(struct proto *p, byte restart, byte code)
1093{
1094 /* Does not work for other states (even PS_START) */
1095 ASSERT(p->proto_state == PS_UP);
1096
1097 /* Scheduled restart may change to shutdown, but not otherwise */
1098 if (p->down_sched == PDS_DISABLE)
1099 return;
1100
1101 p->down_sched = restart ? PDS_RESTART : PDS_DISABLE;
1102 p->down_code = code;
1103 tm_start_max(proto_shutdown_timer, restart ? 2 : 0);
1104}
1105
1106
bf47fe4b
OZ
1107/**
1108 * proto_request_feeding - request feeding routes to the protocol
1109 * @p: given protocol
1110 *
1111 * Sometimes it is needed to send again all routes to the
1112 * protocol. This is called feeding and can be requested by this
0c791f87
OZ
1113 * function. This would cause protocol export state transition
1114 * to ES_FEEDING (during feeding) and when completed, it will
1115 * switch back to ES_READY. This function can be called even
bf47fe4b
OZ
1116 * when feeding is already running, in that case it is restarted.
1117 */
1118void
1119proto_request_feeding(struct proto *p)
1120{
1121 ASSERT(p->proto_state == PS_UP);
1122
6eda3f13
OZ
1123 /* Do nothing if we are still waiting for feeding */
1124 if (p->export_state == ES_DOWN)
1125 return;
1126
bf47fe4b 1127 /* If we are already feeding, we want to restart it */
0c791f87 1128 if (p->export_state == ES_FEEDING)
bf47fe4b
OZ
1129 {
1130 /* Unless feeding is in initial state */
1131 if (p->attn->hook == proto_feed_initial)
1132 return;
1133
1134 rt_feed_baby_abort(p);
1135 }
1136
0c791f87
OZ
1137 /* FIXME: This should be changed for better support of multitable protos */
1138 struct announce_hook *ah;
1139 for (ah = p->ahooks; ah; ah = ah->next)
1140 proto_reset_limit(ah->out_limit);
1141
1142 /* Hack: reset exp_routes during refeed, and do not decrease it later */
1143 p->stats.exp_routes = 0;
1144
bf47fe4b 1145 proto_schedule_feed(p, 0);
227af309 1146 proto_log_state_change(p);
bf47fe4b
OZ
1147}
1148
ebecb6f6
OZ
1149static const char *
1150proto_limit_name(struct proto_limit *l)
1151{
1152 const char *actions[] = {
1153 [PLA_WARN] = "warn",
1154 [PLA_BLOCK] = "block",
1155 [PLA_RESTART] = "restart",
1156 [PLA_DISABLE] = "disable",
1157 };
1158
1159 return actions[l->action];
1160}
1161
1162/**
1163 * proto_notify_limit: notify about limit hit and take appropriate action
1164 * @ah: announce hook
1165 * @l: limit being hit
b662290f 1166 * @dir: limit direction (PLD_*)
7d0a31de 1167 * @rt_count: the number of routes
ebecb6f6
OZ
1168 *
1169 * The function is called by the route processing core when limit @l
1170 * is breached. It activates the limit and tooks appropriate action
7d0a31de 1171 * according to @l->action.
ebecb6f6 1172 */
7d0a31de 1173void
b662290f 1174proto_notify_limit(struct announce_hook *ah, struct proto_limit *l, int dir, u32 rt_count)
ebecb6f6 1175{
b662290f
OZ
1176 const char *dir_name[PLD_MAX] = { "receive", "import" , "export" };
1177 const byte dir_down[PLD_MAX] = { PDC_RX_LIMIT_HIT, PDC_IN_LIMIT_HIT, PDC_OUT_LIMIT_HIT };
ebecb6f6 1178 struct proto *p = ah->proto;
ebecb6f6 1179
7d0a31de
OZ
1180 if (l->state == PLS_BLOCKED)
1181 return;
ebecb6f6 1182
d9b77cc2
OZ
1183 /* For warning action, we want the log message every time we hit the limit */
1184 if (!l->state || ((l->action == PLA_WARN) && (rt_count == l->limit)))
7d0a31de 1185 log(L_WARN "Protocol %s hits route %s limit (%d), action: %s",
b662290f 1186 p->name, dir_name[dir], l->limit, proto_limit_name(l));
ebecb6f6
OZ
1187
1188 switch (l->action)
1189 {
1190 case PLA_WARN:
7d0a31de
OZ
1191 l->state = PLS_ACTIVE;
1192 break;
ebecb6f6
OZ
1193
1194 case PLA_BLOCK:
7d0a31de
OZ
1195 l->state = PLS_BLOCKED;
1196 break;
ebecb6f6
OZ
1197
1198 case PLA_RESTART:
1199 case PLA_DISABLE:
7d0a31de 1200 l->state = PLS_BLOCKED;
b662290f 1201 proto_schedule_down(p, l->action == PLA_RESTART, dir_down[dir]);
7d0a31de 1202 break;
ebecb6f6 1203 }
ebecb6f6
OZ
1204}
1205
0c791f87
OZ
1206
1207static void
1208proto_want_core_up(struct proto *p)
1209{
1210 ASSERT(p->core_state == FS_HUNGRY);
1211
1212 if (!p->proto->multitable)
1213 {
1214 p->main_source = rt_get_source(p, 0);
1215 rt_lock_source(p->main_source);
1216
1217 /* Connect protocol to routing table */
1218 p->main_ahook = proto_add_announce_hook(p, p->table, &p->stats);
1219 p->main_ahook->in_filter = p->cf->in_filter;
1220 p->main_ahook->out_filter = p->cf->out_filter;
1221 p->main_ahook->rx_limit = p->cf->rx_limit;
1222 p->main_ahook->in_limit = p->cf->in_limit;
1223 p->main_ahook->out_limit = p->cf->out_limit;
1224 p->main_ahook->in_keep_filtered = p->cf->in_keep_filtered;
1225
1226 proto_reset_limit(p->main_ahook->rx_limit);
1227 proto_reset_limit(p->main_ahook->in_limit);
1228 proto_reset_limit(p->main_ahook->out_limit);
1229 }
1230
227af309
OZ
1231 p->core_state = FS_HAPPY;
1232 proto_relink(p);
0c791f87
OZ
1233}
1234
1235static void
1236proto_want_export_up(struct proto *p)
1237{
1cb0f83d 1238 ASSERT(p->core_state == FS_HAPPY);
0c791f87
OZ
1239 ASSERT(p->export_state == ES_DOWN);
1240
1241 proto_link_ahooks(p);
1242 proto_schedule_feed(p, 1); /* Sets ES_FEEDING */
1243}
1244
1245static void
1246proto_want_export_down(struct proto *p)
1247{
1248 ASSERT(p->export_state != ES_DOWN);
1249
1250 /* Need to abort feeding */
1251 if (p->export_state == ES_FEEDING)
1252 rt_feed_baby_abort(p);
1253
1254 p->export_state = ES_DOWN;
1255 proto_unlink_ahooks(p);
1256}
1257
1258static void
1259proto_want_core_down(struct proto *p)
1260{
1cb0f83d 1261 ASSERT(p->core_state == FS_HAPPY);
0c791f87
OZ
1262 ASSERT(p->export_state == ES_DOWN);
1263
227af309
OZ
1264 p->core_state = FS_FLUSHING;
1265 proto_relink(p);
0c791f87
OZ
1266 proto_schedule_flush_loop();
1267
1268 if (!p->proto->multitable)
1269 {
1270 rt_unlock_source(p->main_source);
1271 p->main_source = NULL;
1272 }
1273}
1274
1275static void
1276proto_falling_down(struct proto *p)
1277{
1278 p->gr_recovery = 0;
1279 p->gr_wait = 0;
1280 if (p->gr_lock)
1281 proto_graceful_restart_unlock(p);
1282}
1283
6eda3f13
OZ
1284static void
1285proto_fell_down(struct proto *p)
1286{
1287 DBG("Protocol %s down\n", p->name);
1288
1289 u32 all_routes = p->stats.imp_routes + p->stats.filt_routes;
1290 if (all_routes != 0)
1291 log(L_ERR "Protocol %s is down but still has %d routes", p->name, all_routes);
1292
1293 bzero(&p->stats, sizeof(struct proto_stats));
1294 proto_free_ahooks(p);
1295
1296 if (! p->proto->multitable)
1297 rt_unlock_table(p->table);
1298
1299 if (p->proto->cleanup)
1300 p->proto->cleanup(p);
1301
1302 proto_rethink_goal(p);
1303}
1304
0c791f87 1305
3c6269b8
MM
1306/**
1307 * proto_notify_state - notify core about protocol state change
1308 * @p: protocol the state of which has changed
1309 * @ps: the new status
1310 *
1311 * Whenever a state of a protocol changes due to some event internal
1312 * to the protocol (i.e., not inside a start() or shutdown() hook),
1313 * it should immediately notify the core about the change by calling
1314 * proto_notify_state() which will write the new state to the &proto
1315 * structure and take all the actions necessary to adapt to the new
d6a836f8
OZ
1316 * state. State change to PS_DOWN immediately frees resources of protocol
1317 * and might execute start callback of protocol; therefore,
1318 * it should be used at tail positions of protocol callbacks.
3c6269b8 1319 */
67bd949a
MM
1320void
1321proto_notify_state(struct proto *p, unsigned ps)
1322{
1323 unsigned ops = p->proto_state;
1324 unsigned cs = p->core_state;
0c791f87 1325 unsigned es = p->export_state;
67bd949a
MM
1326
1327 DBG("%s reporting state transition %s/%s -> */%s\n", p->name, c_states[cs], p_states[ops], p_states[ps]);
1328 if (ops == ps)
1329 return;
1330
d6a836f8 1331 p->proto_state = ps;
5ebc9293 1332 p->last_state_change = now;
d6a836f8 1333
67bd949a
MM
1334 switch (ps)
1335 {
0c791f87
OZ
1336 case PS_START:
1337 ASSERT(ops == PS_DOWN || ops == PS_UP);
1338 ASSERT(cs == FS_HUNGRY || cs == FS_HAPPY);
1339
1340 if (es != ES_DOWN)
1341 proto_want_export_down(p);
1342 break;
1343
1344 case PS_UP:
1345 ASSERT(ops == PS_DOWN || ops == PS_START);
1346 ASSERT(cs == FS_HUNGRY || cs == FS_HAPPY);
1347 ASSERT(es == ES_DOWN);
1348
1349 if (cs == FS_HUNGRY)
1350 proto_want_core_up(p);
1351 if (!p->gr_wait)
1352 proto_want_export_up(p);
1353 break;
1354
1355 case PS_STOP:
1356 ASSERT(ops == PS_START || ops == PS_UP);
1357
1358 p->down_sched = 0;
1359
1360 if (es != ES_DOWN)
1361 proto_want_export_down(p);
1362 if (cs == FS_HAPPY)
1363 proto_want_core_down(p);
1364 proto_falling_down(p);
1365 break;
1366
67bd949a 1367 case PS_DOWN:
ebecb6f6
OZ
1368 p->down_code = 0;
1369 p->down_sched = 0;
b807ef9a 1370
0c791f87
OZ
1371 if (es != ES_DOWN)
1372 proto_want_export_down(p);
1373 if (cs == FS_HAPPY)
1374 proto_want_core_down(p);
1375 if (ops != PS_STOP)
1376 proto_falling_down(p);
094d2bdb 1377
d6a836f8
OZ
1378 neigh_prune(); // FIXME convert neighbors to resource?
1379 rfree(p->pool);
1380 p->pool = NULL;
1381
67bd949a 1382 if (cs == FS_HUNGRY) /* Shutdown finished */
50fe90ed 1383 {
227af309 1384 proto_log_state_change(p);
50fe90ed
MM
1385 proto_fell_down(p);
1386 return; /* The protocol might have ceased to exist */
1387 }
67bd949a 1388 break;
0c791f87 1389
67bd949a 1390 default:
0c791f87 1391 bug("%s: Invalid state %d", p->name, ps);
67bd949a 1392 }
227af309
OZ
1393
1394 proto_log_state_change(p);
0432c017 1395}
1a54b1c6 1396
0d3e6bce
MM
1397/*
1398 * CLI Commands
1399 */
1400
1401static char *
1402proto_state_name(struct proto *p)
1403{
1404#define P(x,y) ((x << 4) | y)
1405 switch (P(p->proto_state, p->core_state))
1406 {
1407 case P(PS_DOWN, FS_HUNGRY): return "down";
0c791f87
OZ
1408 case P(PS_START, FS_HUNGRY):
1409 case P(PS_START, FS_HAPPY): return "start";
1410 case P(PS_UP, FS_HAPPY):
1411 switch (p->export_state)
1412 {
1413 case ES_DOWN: return "wait";
1414 case ES_FEEDING: return "feed";
1415 case ES_READY: return "up";
1416 default: return "???";
1417 }
227af309
OZ
1418 case P(PS_STOP, FS_HUNGRY):
1419 case P(PS_STOP, FS_FLUSHING): return "stop";
0d3e6bce
MM
1420 case P(PS_DOWN, FS_FLUSHING): return "flush";
1421 default: return "???";
1422 }
1423#undef P
1424}
1425
9db74169 1426static void
15550957 1427proto_show_stats(struct proto_stats *s, int in_keep_filtered)
9db74169 1428{
15550957
OZ
1429 if (in_keep_filtered)
1430 cli_msg(-1006, " Routes: %u imported, %u filtered, %u exported, %u preferred",
1431 s->imp_routes, s->filt_routes, s->exp_routes, s->pref_routes);
cf98be7b
OZ
1432 else
1433 cli_msg(-1006, " Routes: %u imported, %u exported, %u preferred",
1434 s->imp_routes, s->exp_routes, s->pref_routes);
1435
9db74169
OZ
1436 cli_msg(-1006, " Route change stats: received rejected filtered ignored accepted");
1437 cli_msg(-1006, " Import updates: %10u %10u %10u %10u %10u",
1438 s->imp_updates_received, s->imp_updates_invalid,
1439 s->imp_updates_filtered, s->imp_updates_ignored,
1440 s->imp_updates_accepted);
1441 cli_msg(-1006, " Import withdraws: %10u %10u --- %10u %10u",
1442 s->imp_withdraws_received, s->imp_withdraws_invalid,
1443 s->imp_withdraws_ignored, s->imp_withdraws_accepted);
1444 cli_msg(-1006, " Export updates: %10u %10u %10u --- %10u",
1445 s->exp_updates_received, s->exp_updates_rejected,
1446 s->exp_updates_filtered, s->exp_updates_accepted);
1447 cli_msg(-1006, " Export withdraws: %10u --- --- --- %10u",
1448 s->exp_withdraws_received, s->exp_withdraws_accepted);
1449}
1450
ebecb6f6
OZ
1451void
1452proto_show_limit(struct proto_limit *l, const char *dsc)
1453{
7d0a31de
OZ
1454 if (!l)
1455 return;
1456
1457 cli_msg(-1006, " %-16s%d%s", dsc, l->limit, l->state ? " [HIT]" : "");
1458 cli_msg(-1006, " Action: %s", proto_limit_name(l));
ebecb6f6
OZ
1459}
1460
c0adf7e9
OZ
1461void
1462proto_show_basic_info(struct proto *p)
9db74169 1463{
c0adf7e9
OZ
1464 // cli_msg(-1006, " Table: %s", p->table->name);
1465 cli_msg(-1006, " Preference: %d", p->preference);
1466 cli_msg(-1006, " Input filter: %s", filter_name(p->cf->in_filter));
1467 cli_msg(-1006, " Output filter: %s", filter_name(p->cf->out_filter));
9db74169 1468
0c791f87
OZ
1469 if (graceful_restart_state == GRS_ACTIVE)
1470 cli_msg(-1006, " GR recovery: %s%s",
1471 p->gr_lock ? " pending" : "",
1472 p->gr_wait ? " waiting" : "");
1473
b662290f 1474 proto_show_limit(p->cf->rx_limit, "Receive limit:");
ebecb6f6 1475 proto_show_limit(p->cf->in_limit, "Import limit:");
d9b77cc2 1476 proto_show_limit(p->cf->out_limit, "Export limit:");
ebecb6f6 1477
c0adf7e9 1478 if (p->proto_state != PS_DOWN)
15550957 1479 proto_show_stats(&p->stats, p->cf->in_keep_filtered);
9db74169
OZ
1480}
1481
e304fd4b
OZ
1482void
1483proto_cmd_show(struct proto *p, unsigned int verbose, int cnt)
1d2664a4 1484{
c37e7851 1485 byte buf[256], tbuf[TM_DATETIME_BUFFER_SIZE];
9685deb9 1486
e304fd4b
OZ
1487 /* First protocol - show header */
1488 if (!cnt)
1489 cli_msg(-2002, "name proto table state since info");
1490
9685deb9
MM
1491 buf[0] = 0;
1492 if (p->proto->get_status)
1493 p->proto->get_status(p, buf);
c37e7851
OZ
1494 tm_format_datetime(tbuf, &config->tf_proto, p->last_state_change);
1495 cli_msg(-1002, "%-8s %-8s %-8s %-5s %-10s %s",
1d2664a4
MM
1496 p->name,
1497 p->proto->name,
1498 p->table->name,
1499 proto_state_name(p),
c37e7851 1500 tbuf,
9685deb9 1501 buf);
1d2664a4
MM
1502 if (verbose)
1503 {
e04555c0
OZ
1504 if (p->cf->dsc)
1505 cli_msg(-1006, " Description: %s", p->cf->dsc);
b8113a5e
OZ
1506 if (p->cf->router_id)
1507 cli_msg(-1006, " Router ID: %R", p->cf->router_id);
925fe2d3 1508
b8113a5e
OZ
1509 if (p->proto->show_proto_info)
1510 p->proto->show_proto_info(p);
c0adf7e9
OZ
1511 else
1512 proto_show_basic_info(p);
b8113a5e 1513
925fe2d3 1514 cli_msg(-1006, "");
1d2664a4
MM
1515 }
1516}
1517
ae97b946 1518void
e304fd4b 1519proto_cmd_disable(struct proto *p, unsigned int arg UNUSED, int cnt UNUSED)
ae97b946 1520{
e304fd4b 1521 if (p->disabled)
1d2664a4 1522 {
e304fd4b 1523 cli_msg(-8, "%s: already disabled", p->name);
1d2664a4
MM
1524 return;
1525 }
e304fd4b
OZ
1526
1527 log(L_INFO "Disabling protocol %s", p->name);
1528 p->disabled = 1;
ebecb6f6 1529 p->down_code = PDC_CMD_DISABLE;
e304fd4b
OZ
1530 proto_rethink_goal(p);
1531 cli_msg(-9, "%s: disabled", p->name);
1532}
1533
1534void
1535proto_cmd_enable(struct proto *p, unsigned int arg UNUSED, int cnt UNUSED)
1536{
1537 if (!p->disabled)
1538 {
1539 cli_msg(-10, "%s: already enabled", p->name);
1540 return;
1541 }
1542
1543 log(L_INFO "Enabling protocol %s", p->name);
1544 p->disabled = 0;
1545 proto_rethink_goal(p);
1546 cli_msg(-11, "%s: enabled", p->name);
1547}
1548
1549void
1550proto_cmd_restart(struct proto *p, unsigned int arg UNUSED, int cnt UNUSED)
1551{
1552 if (p->disabled)
1553 {
1554 cli_msg(-8, "%s: already disabled", p->name);
1555 return;
1556 }
1557
1558 log(L_INFO "Restarting protocol %s", p->name);
1559 p->disabled = 1;
ebecb6f6 1560 p->down_code = PDC_CMD_RESTART;
e304fd4b
OZ
1561 proto_rethink_goal(p);
1562 p->disabled = 0;
1563 proto_rethink_goal(p);
1564 cli_msg(-12, "%s: restarted", p->name);
1565}
1566
1567void
1568proto_cmd_reload(struct proto *p, unsigned int dir, int cnt UNUSED)
1569{
1570 if (p->disabled)
1571 {
1572 cli_msg(-8, "%s: already disabled", p->name);
1573 return;
1574 }
1575
1576 /* If the protocol in not UP, it has no routes */
1577 if (p->proto_state != PS_UP)
1578 return;
1579
1580 log(L_INFO "Reloading protocol %s", p->name);
1581
1582 /* re-importing routes */
1583 if (dir != CMD_RELOAD_OUT)
ebecb6f6
OZ
1584 {
1585 if (! (p->reload_routes && p->reload_routes(p)))
1586 {
1587 cli_msg(-8006, "%s: reload failed", p->name);
1588 return;
1589 }
1590
1591 /*
1592 * Should be done before reload_routes() hook?
1593 * Perhaps, but these hooks work asynchronously.
1594 */
7d0a31de 1595 if (!p->proto->multitable)
b662290f
OZ
1596 {
1597 proto_reset_limit(p->main_ahook->rx_limit);
1598 proto_reset_limit(p->main_ahook->in_limit);
1599 }
ebecb6f6
OZ
1600 }
1601
e304fd4b
OZ
1602 /* re-exporting routes */
1603 if (dir != CMD_RELOAD_IN)
1604 proto_request_feeding(p);
1605
1606 cli_msg(-15, "%s: reloading", p->name);
1607}
1608
1609void
1610proto_cmd_debug(struct proto *p, unsigned int mask, int cnt UNUSED)
1611{
1612 p->debug = mask;
1613}
1614
1615void
1616proto_cmd_mrtdump(struct proto *p, unsigned int mask, int cnt UNUSED)
1617{
1618 p->mrtdump = mask;
1619}
1620
1621static void
1622proto_apply_cmd_symbol(struct symbol *s, void (* cmd)(struct proto *, unsigned int, int), unsigned int arg)
1623{
1624 if (s->class != SYM_PROTO)
1d2664a4 1625 {
e304fd4b
OZ
1626 cli_msg(9002, "%s is not a protocol", s->name);
1627 return;
1d2664a4 1628 }
e304fd4b
OZ
1629
1630 cmd(((struct proto_config *)s->def)->proto, arg, 0);
ae97b946
MM
1631 cli_msg(0, "");
1632}
02c1fbdd 1633
e304fd4b
OZ
1634static void
1635proto_apply_cmd_patt(char *patt, void (* cmd)(struct proto *, unsigned int, int), unsigned int arg)
1636{
1637 int cnt = 0;
1638
1639 node *nn;
1640 WALK_LIST(nn, proto_list)
1641 {
1642 struct proto *p = SKIP_BACK(struct proto, glob_node, nn);
1643
1644 if (!patt || patmatch(patt, p->name))
1645 cmd(p, arg, cnt++);
1646 }
1647
1648 if (!cnt)
1649 cli_msg(8003, "No protocols match");
1650 else
1651 cli_msg(0, "");
1652}
1653
1654void
e0a45fb4
OZ
1655proto_apply_cmd(struct proto_spec ps, void (* cmd)(struct proto *, unsigned int, int),
1656 int restricted, unsigned int arg)
e304fd4b 1657{
e0a45fb4
OZ
1658 if (restricted && cli_access_restricted())
1659 return;
1660
e304fd4b
OZ
1661 if (ps.patt)
1662 proto_apply_cmd_patt(ps.ptr, cmd, arg);
1663 else
1664 proto_apply_cmd_symbol(ps.ptr, cmd, arg);
1665}
1666
02c1fbdd
MM
1667struct proto *
1668proto_get_named(struct symbol *sym, struct protocol *pr)
1669{
1670 struct proto *p, *q;
1671
1672 if (sym)
1673 {
1674 if (sym->class != SYM_PROTO)
1675 cf_error("%s: Not a protocol", sym->name);
1676 p = ((struct proto_config *)sym->def)->proto;
1677 if (!p || p->proto != pr)
1678 cf_error("%s: Not a %s protocol", sym->name, pr->name);
1679 }
1680 else
1681 {
1682 p = NULL;
f14a4bec 1683 WALK_LIST(q, active_proto_list)
02c1fbdd
MM
1684 if (q->proto == pr)
1685 {
1686 if (p)
1687 cf_error("There are multiple %s protocols running", pr->name);
1688 p = q;
1689 }
1690 if (!p)
1691 cf_error("There is no %s protocol running", pr->name);
1692 }
1693 return p;
1694}