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