4 * (c) 1998--2000 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
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"
21 #include "filter/filter.h"
25 static list protocol_list
;
26 static list proto_list
;
28 #define PD(pr, msg, args...) do { if (pr->debug & D_STATES) { log(L_TRACE "%s: " msg, pr->name , ## args); } } while(0)
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
;
36 static event
*proto_flush_event
;
37 static timer
*proto_shutdown_timer
;
38 static timer
*gr_wait_timer
;
45 static int graceful_restart_state
;
46 static u32 graceful_restart_locks
;
48 static char *p_states
[] = { "DOWN", "START", "UP", "STOP" };
49 static char *c_states
[] = { "HUNGRY", "???", "HAPPY", "FLUSHING" };
51 static void proto_flush_loop(void *);
52 static void proto_shutdown_loop(struct timer
*);
53 static void proto_rethink_goal(struct proto
*p
);
54 static void proto_want_export_up(struct proto
*p
);
55 static void proto_fell_down(struct proto
*p
);
56 static char *proto_state_name(struct proto
*p
);
59 proto_relink(struct proto
*p
)
63 switch (p
->core_state
)
66 l
= &inactive_proto_list
;
69 l
= &active_proto_list
;
72 l
= &flush_proto_list
;
83 proto_log_state_change(struct proto
*p
)
85 if (p
->debug
& D_STATES
)
87 char *name
= proto_state_name(p
);
88 if (name
!= p
->last_state_name_announced
)
90 p
->last_state_name_announced
= name
;
91 PD(p
, "State changed to %s", proto_state_name(p
));
95 p
->last_state_name_announced
= NULL
;
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)
106 * When a new configuration has been read in, the core code starts
107 * initializing all the protocol instances configured by calling their
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.
114 proto_new(struct proto_config
*c
, unsigned size
)
116 struct protocol
*pr
= c
->protocol
;
117 struct proto
*p
= mb_allocz(proto_pool
, size
);
121 p
->mrtdump
= c
->mrtdump
;
123 p
->preference
= c
->preference
;
124 p
->disabled
= c
->disabled
;
126 p
->table
= c
->table
->table
;
127 p
->hash_key
= random_u32();
133 proto_init_instance(struct proto
*p
)
135 /* Here we cannot use p->cf->name since it won't survive reconfiguration */
136 p
->pool
= rp_new(proto_pool
, p
->proto
->name
);
137 p
->attn
= ev_new(p
->pool
);
140 if (graceful_restart_state
== GRS_INIT
)
143 if (! p
->proto
->multitable
)
144 rt_lock_table(p
->table
);
147 extern pool
*rt_table_pool
;
149 * proto_add_announce_hook - connect protocol to a routing table
150 * @p: protocol instance
151 * @t: routing table to connect to
152 * @stats: per-table protocol statistics
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.
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()).
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
169 struct announce_hook
*
170 proto_add_announce_hook(struct proto
*p
, struct rtable
*t
, struct proto_stats
*stats
)
172 struct announce_hook
*h
;
174 DBG("Connecting protocol %s to table %s\n", p
->name
, t
->name
);
175 PD(p
, "Connected to table %s", t
->name
);
177 h
= mb_allocz(rt_table_pool
, sizeof(struct announce_hook
));
185 if (p
->rt_notify
&& (p
->export_state
!= ES_DOWN
))
186 add_tail(&t
->hooks
, &h
->n
);
191 * proto_find_announce_hook - find announce hooks
192 * @p: protocol instance
195 * Returns pointer to announce hook or NULL
197 struct announce_hook
*
198 proto_find_announce_hook(struct proto
*p
, struct rtable
*t
)
200 struct announce_hook
*a
;
202 for (a
= p
->ahooks
; a
; a
= a
->next
)
210 proto_link_ahooks(struct proto
*p
)
212 struct announce_hook
*h
;
215 for(h
=p
->ahooks
; h
; h
=h
->next
)
216 add_tail(&h
->table
->hooks
, &h
->n
);
220 proto_unlink_ahooks(struct proto
*p
)
222 struct announce_hook
*h
;
225 for(h
=p
->ahooks
; h
; h
=h
->next
)
230 proto_free_ahooks(struct proto
*p
)
232 struct announce_hook
*h
, *hn
;
234 for(h
= p
->ahooks
; h
; h
= hn
)
241 p
->main_ahook
= NULL
;
246 * proto_config_new - create a new protocol configuration
247 * @pr: protocol the configuration will belong to
248 * @class: SYM_PROTO or SYM_TEMPLATE
250 * Whenever the configuration file says that a new instance
251 * of a routing protocol should be created, the parser calls
252 * proto_config_new() to create a configuration entry for this
253 * instance (a structure staring with the &proto_config header
254 * containing all the generic items followed by protocol-specific
255 * ones). Also, the configuration entry gets added to the list
256 * of protocol instances kept in the configuration.
258 * The function is also used to create protocol templates (when class
259 * SYM_TEMPLATE is specified), the only difference is that templates
260 * are not added to the list of protocol instances and therefore not
261 * initialized during protos_commit()).
264 proto_config_new(struct protocol
*pr
, int class)
266 struct proto_config
*c
= cfg_allocz(pr
->config_size
);
268 if (class == SYM_PROTO
)
269 add_tail(&new_config
->protos
, &c
->n
);
270 c
->global
= new_config
;
273 c
->preference
= pr
->preference
;
275 c
->out_filter
= FILTER_REJECT
;
276 c
->table
= c
->global
->master_rtc
;
277 c
->debug
= new_config
->proto_default_debug
;
278 c
->mrtdump
= new_config
->proto_default_mrtdump
;
283 * proto_copy_config - copy a protocol configuration
284 * @dest: destination protocol configuration
285 * @src: source protocol configuration
287 * Whenever a new instance of a routing protocol is created from the
288 * template, proto_copy_config() is called to copy a content of
289 * the source protocol configuration to the new protocol configuration.
290 * Name, class and a node in protos list of @dest are kept intact.
291 * copy_config() protocol hook is used to copy protocol-specific data.
294 proto_copy_config(struct proto_config
*dest
, struct proto_config
*src
)
300 if (dest
->protocol
!= src
->protocol
)
301 cf_error("Can't copy configuration from a different protocol type");
303 if (dest
->protocol
->copy_config
== NULL
)
304 cf_error("Inheriting configuration for %s is not supported", src
->protocol
->name
);
306 DBG("Copying configuration from %s to %s\n", src
->name
, dest
->name
);
309 * Copy struct proto_config here. Keep original node, class and name.
310 * protocol-specific config copy is handled by protocol copy_config() hook
314 old_class
= dest
->class;
315 old_name
= dest
->name
;
317 memcpy(dest
, src
, sizeof(struct proto_config
));
320 dest
->class = old_class
;
321 dest
->name
= old_name
;
323 dest
->protocol
->copy_config(dest
, src
);
327 * protos_preconfig - pre-configuration processing
328 * @c: new configuration
330 * This function calls the preconfig() hooks of all routing
331 * protocols available to prepare them for reading of the new
335 protos_preconfig(struct config
*c
)
339 init_list(&c
->protos
);
340 DBG("Protocol preconfig:");
341 WALK_LIST(p
, protocol_list
)
352 * protos_postconfig - post-configuration processing
353 * @c: new configuration
355 * This function calls the postconfig() hooks of all protocol
356 * instances specified in configuration @c. The hooks are not
357 * called for protocol templates.
360 protos_postconfig(struct config
*c
)
362 struct proto_config
*x
;
365 DBG("Protocol postconfig:");
366 WALK_LIST(x
, c
->protos
)
377 extern struct protocol proto_unix_iface
;
379 static struct proto
*
380 proto_init(struct proto_config
*c
)
382 struct protocol
*p
= c
->protocol
;
383 struct proto
*q
= p
->init(c
);
385 q
->proto_state
= PS_DOWN
;
386 q
->core_state
= FS_HUNGRY
;
387 q
->export_state
= ES_DOWN
;
388 q
->last_state_change
= now
;
390 add_tail(&initial_proto_list
, &q
->n
);
392 if (p
== &proto_unix_iface
)
393 initial_device_proto
= q
;
395 add_tail(&proto_list
, &q
->glob_node
);
396 PD(q
, "Initializing%s", q
->disabled
? " [disabled]" : "");
400 int proto_reconfig_type
; /* Hack to propagate type info to pipe reconfigure hook */
403 proto_reconfigure(struct proto
*p
, struct proto_config
*oc
, struct proto_config
*nc
, int type
)
405 /* If the protocol is DOWN, we just restart it */
406 if (p
->proto_state
== PS_DOWN
)
409 /* If there is a too big change in core attributes, ... */
410 if ((nc
->protocol
!= oc
->protocol
) ||
411 (nc
->disabled
!= p
->disabled
) ||
412 (nc
->table
->table
!= oc
->table
->table
))
415 p
->debug
= nc
->debug
;
416 p
->mrtdump
= nc
->mrtdump
;
417 proto_reconfig_type
= type
;
419 /* Execute protocol specific reconfigure hook */
420 if (! (p
->proto
->reconfigure
&& p
->proto
->reconfigure(p
, nc
)))
423 DBG("\t%s: same\n", oc
->name
);
424 PD(p
, "Reconfigured");
427 p
->preference
= nc
->preference
;
430 /* Multitable protocols handle rest in their reconfigure hooks */
431 if (p
->proto
->multitable
)
434 /* Update filters and limits in the main announce hook
435 Note that this also resets limit state */
438 struct announce_hook
*ah
= p
->main_ahook
;
439 ah
->in_filter
= nc
->in_filter
;
440 ah
->out_filter
= nc
->out_filter
;
441 ah
->rx_limit
= nc
->rx_limit
;
442 ah
->in_limit
= nc
->in_limit
;
443 ah
->out_limit
= nc
->out_limit
;
444 ah
->in_keep_filtered
= nc
->in_keep_filtered
;
445 proto_verify_limits(ah
);
448 /* Update routes when filters changed. If the protocol in not UP,
449 it has no routes and we can ignore such changes */
450 if ((p
->proto_state
!= PS_UP
) || (type
== RECONFIG_SOFT
))
453 int import_changed
= ! filter_same(nc
->in_filter
, oc
->in_filter
);
454 int export_changed
= ! filter_same(nc
->out_filter
, oc
->out_filter
);
456 /* We treat a change in preferences by reimporting routes */
457 if (nc
->preference
!= oc
->preference
)
460 if (import_changed
|| export_changed
)
461 log(L_INFO
"Reloading protocol %s", p
->name
);
463 /* If import filter changed, call reload hook */
464 if (import_changed
&& ! (p
->reload_routes
&& p
->reload_routes(p
)))
466 /* Now, the protocol is reconfigured. But route reload failed
467 and we have to do regular protocol restart. */
468 log(L_INFO
"Restarting protocol %s", p
->name
);
470 p
->down_code
= PDC_CF_RESTART
;
471 proto_rethink_goal(p
);
473 proto_rethink_goal(p
);
478 proto_request_feeding(p
);
484 * protos_commit - commit new protocol configuration
485 * @new: new configuration
486 * @old: old configuration or %NULL if it's boot time config
487 * @force_reconfig: force restart of all protocols (used for example
488 * when the router ID changes)
489 * @type: type of reconfiguration (RECONFIG_SOFT or RECONFIG_HARD)
491 * Scan differences between @old and @new configuration and adjust all
492 * protocol instances to conform to the new configuration.
494 * When a protocol exists in the new configuration, but it doesn't in the
495 * original one, it's immediately started. When a collision with the other
496 * running protocol would arise, the new protocol will be temporarily stopped
497 * by the locking mechanism.
499 * When a protocol exists in the old configuration, but it doesn't in the
500 * new one, it's shut down and deleted after the shutdown completes.
502 * When a protocol exists in both configurations, the core decides
503 * whether it's possible to reconfigure it dynamically - it checks all
504 * the core properties of the protocol (changes in filters are ignored
505 * if type is RECONFIG_SOFT) and if they match, it asks the
506 * reconfigure() hook of the protocol to see if the protocol is able
507 * to switch to the new configuration. If it isn't possible, the
508 * protocol is shut down and a new instance is started with the new
509 * configuration after the shutdown is completed.
512 protos_commit(struct config
*new, struct config
*old
, int force_reconfig
, int type
)
514 struct proto_config
*oc
, *nc
;
518 DBG("protos_commit:\n");
521 WALK_LIST(oc
, old
->protos
)
524 sym
= cf_find_symbol(new, oc
->name
);
525 if (sym
&& sym
->class == SYM_PROTO
&& !new->shutdown
)
527 /* Found match, let's check if we can smoothly switch to new configuration */
528 /* No need to check description */
532 /* We will try to reconfigure protocol p */
533 if (! force_reconfig
&& proto_reconfigure(p
, oc
, nc
, type
))
536 /* Unsuccessful, we will restart it */
537 if (!p
->disabled
&& !nc
->disabled
)
538 log(L_INFO
"Restarting protocol %s", p
->name
);
539 else if (p
->disabled
&& !nc
->disabled
)
540 log(L_INFO
"Enabling protocol %s", p
->name
);
541 else if (!p
->disabled
&& nc
->disabled
)
542 log(L_INFO
"Disabling protocol %s", p
->name
);
544 p
->down_code
= nc
->disabled
? PDC_CF_DISABLE
: PDC_CF_RESTART
;
547 else if (!new->shutdown
)
549 log(L_INFO
"Removing protocol %s", p
->name
);
550 p
->down_code
= PDC_CF_REMOVE
;
553 else /* global shutdown */
555 p
->down_code
= PDC_CMD_SHUTDOWN
;
559 p
->reconfiguring
= 1;
560 config_add_obstacle(old
);
561 proto_rethink_goal(p
);
565 WALK_LIST(nc
, new->protos
)
568 if (old
) /* Not a first-time configuration */
569 log(L_INFO
"Adding protocol %s", nc
->name
);
574 DBG("Protocol start\n");
576 /* Start device protocol first */
577 if (initial_device_proto
)
579 proto_rethink_goal(initial_device_proto
);
580 initial_device_proto
= NULL
;
583 /* Determine router ID for the first time - it has to be here and not in
584 global_commit() because it is postponed after start of device protocol */
585 if (!config
->router_id
)
587 config
->router_id
= if_choose_router_id(config
->router_id_from
, 0);
588 if (!config
->router_id
)
589 die("Cannot determine router ID, please configure it manually");
592 /* Start all other protocols */
593 WALK_LIST_DELSAFE(p
, n
, initial_proto_list
)
594 proto_rethink_goal(p
);
598 proto_rethink_goal(struct proto
*p
)
603 if (p
->reconfiguring
&& p
->core_state
== FS_HUNGRY
&& p
->proto_state
== PS_DOWN
)
605 struct proto_config
*nc
= p
->cf_new
;
606 DBG("%s has shut down for reconfiguration\n", p
->name
);
608 config_del_obstacle(p
->cf
->global
);
610 rem_node(&p
->glob_node
);
617 /* Determine what state we want to reach */
618 if (p
->disabled
|| p
->reconfiguring
)
624 if (goal
== PS_UP
) /* Going up */
626 if (p
->proto_state
== PS_DOWN
&& p
->core_state
== FS_HUNGRY
)
628 DBG("Kicking %s up\n", p
->name
);
630 proto_init_instance(p
);
631 proto_notify_state(p
, (q
->start
? q
->start(p
) : PS_UP
));
634 else /* Going down */
636 if (p
->proto_state
== PS_START
|| p
->proto_state
== PS_UP
)
638 DBG("Kicking %s down\n", p
->name
);
639 PD(p
, "Shutting down");
640 proto_notify_state(p
, (q
->shutdown
? q
->shutdown(p
) : PS_DOWN
));
647 * DOC: Graceful restart recovery
649 * Graceful restart of a router is a process when the routing plane (e.g. BIRD)
650 * restarts but both the forwarding plane (e.g kernel routing table) and routing
651 * neighbors keep proper routes, and therefore uninterrupted packet forwarding
654 * BIRD implements graceful restart recovery by deferring export of routes to
655 * protocols until routing tables are refilled with the expected content. After
656 * start, protocols generate routes as usual, but routes are not propagated to
657 * them, until protocols report that they generated all routes. After that,
658 * graceful restart recovery is finished and the export (and the initial feed)
659 * to protocols is enabled.
661 * When graceful restart recovery need is detected during initialization, then
662 * enabled protocols are marked with @gr_recovery flag before start. Such
663 * protocols then decide how to proceed with graceful restart, participation is
664 * voluntary. Protocols could lock the recovery by proto_graceful_restart_lock()
665 * (stored in @gr_lock flag), which means that they want to postpone the end of
666 * the recovery until they converge and then unlock it. They also could set
667 * @gr_wait before advancing to %PS_UP, which means that the core should defer
668 * route export to that protocol until the end of the recovery. This should be
669 * done by protocols that expect their neigbors to keep the proper routes
670 * (kernel table, BGP sessions with BGP graceful restart capability).
672 * The graceful restart recovery is finished when either all graceful restart
673 * locks are unlocked or when graceful restart wait timer fires.
677 static void graceful_restart_done(struct timer
*t
);
680 * graceful_restart_recovery - request initial graceful restart recovery
682 * Called by the platform initialization code if the need for recovery
683 * after graceful restart is detected during boot. Have to be called
684 * before protos_commit().
687 graceful_restart_recovery(void)
689 graceful_restart_state
= GRS_INIT
;
693 * graceful_restart_init - initialize graceful restart
695 * When graceful restart recovery was requested, the function starts an active
696 * phase of the recovery and initializes graceful restart wait timer. The
697 * function have to be called after protos_commit().
700 graceful_restart_init(void)
702 if (!graceful_restart_state
)
705 log(L_INFO
"Graceful restart started");
707 if (!graceful_restart_locks
)
709 graceful_restart_done(NULL
);
713 graceful_restart_state
= GRS_ACTIVE
;
714 gr_wait_timer
= tm_new(proto_pool
);
715 gr_wait_timer
->hook
= graceful_restart_done
;
716 tm_start(gr_wait_timer
, config
->gr_wait
);
720 * graceful_restart_done - finalize graceful restart
722 * When there are no locks on graceful restart, the functions finalizes the
723 * graceful restart recovery. Protocols postponing route export until the end of
724 * the recovery are awakened and the export to them is enabled. All other
725 * related state is cleared. The function is also called when the graceful
726 * restart wait timer fires (but there are still some locks).
729 graceful_restart_done(struct timer
*t UNUSED
)
734 log(L_INFO
"Graceful restart done");
735 graceful_restart_state
= GRS_DONE
;
737 WALK_LIST2(p
, n
, proto_list
, glob_node
)
742 /* Resume postponed export of routes */
743 if ((p
->proto_state
== PS_UP
) && p
->gr_wait
)
745 proto_want_export_up(p
);
746 proto_log_state_change(p
);
755 graceful_restart_locks
= 0;
759 graceful_restart_show_status(void)
761 if (graceful_restart_state
!= GRS_ACTIVE
)
764 cli_msg(-24, "Graceful restart recovery in progress");
765 cli_msg(-24, " Waiting for %d protocols to recover", graceful_restart_locks
);
766 cli_msg(-24, " Wait timer is %d/%d", tm_remains(gr_wait_timer
), config
->gr_wait
);
770 * proto_graceful_restart_lock - lock graceful restart by protocol
771 * @p: protocol instance
773 * This function allows a protocol to postpone the end of graceful restart
774 * recovery until it converges. The lock is removed when the protocol calls
775 * proto_graceful_restart_unlock() or when the protocol is stopped.
777 * The function have to be called during the initial phase of graceful restart
778 * recovery and only for protocols that are part of graceful restart (i.e. their
779 * @gr_recovery is set), which means it should be called from protocol start
783 proto_graceful_restart_lock(struct proto
*p
)
785 ASSERT(graceful_restart_state
== GRS_INIT
);
786 ASSERT(p
->gr_recovery
);
792 graceful_restart_locks
++;
796 * proto_graceful_restart_unlock - unlock graceful restart by protocol
797 * @p: protocol instance
799 * This function unlocks a lock from proto_graceful_restart_lock(). It is also
800 * automatically called when the lock holding protocol went down.
803 proto_graceful_restart_unlock(struct proto
*p
)
809 graceful_restart_locks
--;
811 if ((graceful_restart_state
== GRS_ACTIVE
) && !graceful_restart_locks
)
812 tm_start(gr_wait_timer
, 0);
818 * protos_dump_all - dump status of all protocols
820 * This function dumps status of all existing protocol instances to the
821 * debug output. It involves printing of general status information
822 * such as protocol states, its position on the protocol lists
823 * and also calling of a dump() hook of the protocol to print
827 protos_dump_all(void)
830 struct announce_hook
*a
;
832 debug("Protocols:\n");
834 WALK_LIST(p
, active_proto_list
)
836 debug(" protocol %s state %s/%s\n", p
->name
,
837 p_states
[p
->proto_state
], c_states
[p
->core_state
]);
838 for (a
= p
->ahooks
; a
; a
= a
->next
)
840 debug("\tTABLE %s\n", a
->table
->name
);
842 debug("\tInput filter: %s\n", filter_name(a
->in_filter
));
843 if (a
->out_filter
!= FILTER_REJECT
)
844 debug("\tOutput filter: %s\n", filter_name(a
->out_filter
));
847 debug("\tDISABLED\n");
848 else if (p
->proto
->dump
)
851 WALK_LIST(p
, inactive_proto_list
)
852 debug(" inactive %s: state %s/%s\n", p
->name
, p_states
[p
->proto_state
], c_states
[p
->core_state
]);
853 WALK_LIST(p
, initial_proto_list
)
854 debug(" initial %s\n", p
->name
);
855 WALK_LIST(p
, flush_proto_list
)
856 debug(" flushing %s\n", p
->name
);
860 * proto_build - make a single protocol available
863 * After the platform specific initialization code uses protos_build()
864 * to add all the standard protocols, it should call proto_build() for
865 * all platform specific protocols to inform the core that they exist.
868 proto_build(struct protocol
*p
)
870 add_tail(&protocol_list
, &p
->n
);
873 ASSERT(!attr_class_to_protocol
[p
->attr_class
]);
874 attr_class_to_protocol
[p
->attr_class
] = p
;
878 /* FIXME: convert this call to some protocol hook */
879 extern void bfd_init_all(void);
882 * protos_build - build a protocol list
884 * This function is called during BIRD startup to insert
885 * all standard protocols to the global protocol list. Insertion
886 * of platform specific protocols (such as the kernel syncer)
887 * is in the domain of competence of the platform dependent
893 init_list(&protocol_list
);
894 init_list(&proto_list
);
895 init_list(&active_proto_list
);
896 init_list(&inactive_proto_list
);
897 init_list(&initial_proto_list
);
898 init_list(&flush_proto_list
);
899 proto_build(&proto_device
);
901 proto_build(&proto_radv
);
904 proto_build(&proto_rip
);
907 proto_build(&proto_static
);
910 proto_build(&proto_ospf
);
913 proto_build(&proto_pipe
);
916 proto_build(&proto_bgp
);
919 proto_build(&proto_bfd
);
923 proto_pool
= rp_new(&root_pool
, "Protocols");
924 proto_flush_event
= ev_new(proto_pool
);
925 proto_flush_event
->hook
= proto_flush_loop
;
926 proto_shutdown_timer
= tm_new(proto_pool
);
927 proto_shutdown_timer
->hook
= proto_shutdown_loop
;
931 proto_feed_more(void *P
)
935 if (p
->export_state
!= ES_FEEDING
)
938 DBG("Feeding protocol %s continued\n", p
->name
);
941 DBG("Feeding protocol %s finished\n", p
->name
);
942 p
->export_state
= ES_READY
;
943 proto_log_state_change(p
);
950 p
->attn
->hook
= proto_feed_more
;
951 ev_schedule(p
->attn
); /* Will continue later... */
956 proto_feed_initial(void *P
)
960 if (p
->export_state
!= ES_FEEDING
)
963 DBG("Feeding protocol %s\n", p
->name
);
970 proto_schedule_feed(struct proto
*p
, int initial
)
972 DBG("%s: Scheduling meal\n", p
->name
);
974 p
->export_state
= ES_FEEDING
;
975 p
->refeeding
= !initial
;
977 p
->attn
->hook
= initial
? proto_feed_initial
: proto_feed_more
;
978 ev_schedule(p
->attn
);
981 p
->feed_begin(p
, initial
);
985 * Flushing loop is responsible for flushing routes and protocols
986 * after they went down. It runs in proto_flush_event. At the start of
987 * one round, protocols waiting to flush are marked in
988 * proto_schedule_flush_loop(). At the end of the round (when routing
989 * table flush is complete), marked protocols are flushed and a next
993 static int flush_loop_state
; /* 1 -> running */
996 proto_schedule_flush_loop(void)
999 struct announce_hook
*h
;
1001 if (flush_loop_state
)
1003 flush_loop_state
= 1;
1005 WALK_LIST(p
, flush_proto_list
)
1008 for (h
=p
->ahooks
; h
; h
=h
->next
)
1009 rt_mark_for_prune(h
->table
);
1012 ev_schedule(proto_flush_event
);
1016 proto_flush_loop(void *unused UNUSED
)
1020 if (! rt_prune_loop())
1022 /* Rtable pruning is not finished */
1023 ev_schedule(proto_flush_event
);
1030 WALK_LIST(p
, flush_proto_list
)
1033 /* This will flush interfaces in the same manner
1034 like rt_prune_all() flushes routes */
1035 if (p
->proto
== &proto_unix_iface
)
1038 DBG("Flushing protocol %s\n", p
->name
);
1040 p
->core_state
= FS_HUNGRY
;
1042 proto_log_state_change(p
);
1043 if (p
->proto_state
== PS_DOWN
)
1048 /* This round finished, perhaps there will be another one */
1049 flush_loop_state
= 0;
1050 if (!EMPTY_LIST(flush_proto_list
))
1051 proto_schedule_flush_loop();
1055 /* Temporary hack to propagate restart to BGP */
1059 proto_shutdown_loop(struct timer
*t UNUSED
)
1061 struct proto
*p
, *p_next
;
1063 WALK_LIST_DELSAFE(p
, p_next
, active_proto_list
)
1066 proto_restart
= (p
->down_sched
== PDS_RESTART
);
1069 proto_rethink_goal(p
);
1073 proto_rethink_goal(p
);
1079 proto_schedule_down(struct proto
*p
, byte restart
, byte code
)
1081 /* Does not work for other states (even PS_START) */
1082 ASSERT(p
->proto_state
== PS_UP
);
1084 /* Scheduled restart may change to shutdown, but not otherwise */
1085 if (p
->down_sched
== PDS_DISABLE
)
1088 p
->down_sched
= restart
? PDS_RESTART
: PDS_DISABLE
;
1089 p
->down_code
= code
;
1090 tm_start_max(proto_shutdown_timer
, restart
? 2 : 0);
1095 * proto_request_feeding - request feeding routes to the protocol
1096 * @p: given protocol
1098 * Sometimes it is needed to send again all routes to the
1099 * protocol. This is called feeding and can be requested by this
1100 * function. This would cause protocol export state transition
1101 * to ES_FEEDING (during feeding) and when completed, it will
1102 * switch back to ES_READY. This function can be called even
1103 * when feeding is already running, in that case it is restarted.
1106 proto_request_feeding(struct proto
*p
)
1108 ASSERT(p
->proto_state
== PS_UP
);
1110 /* Do nothing if we are still waiting for feeding */
1111 if (p
->export_state
== ES_DOWN
)
1114 /* If we are already feeding, we want to restart it */
1115 if (p
->export_state
== ES_FEEDING
)
1117 /* Unless feeding is in initial state */
1118 if (p
->attn
->hook
== proto_feed_initial
)
1121 rt_feed_baby_abort(p
);
1124 /* FIXME: This should be changed for better support of multitable protos */
1125 struct announce_hook
*ah
;
1126 for (ah
= p
->ahooks
; ah
; ah
= ah
->next
)
1127 proto_reset_limit(ah
->out_limit
);
1129 /* Hack: reset exp_routes during refeed, and do not decrease it later */
1130 p
->stats
.exp_routes
= 0;
1132 proto_schedule_feed(p
, 0);
1133 proto_log_state_change(p
);
1137 proto_limit_name(struct proto_limit
*l
)
1139 const char *actions
[] = {
1140 [PLA_WARN
] = "warn",
1141 [PLA_BLOCK
] = "block",
1142 [PLA_RESTART
] = "restart",
1143 [PLA_DISABLE
] = "disable",
1146 return actions
[l
->action
];
1150 * proto_notify_limit: notify about limit hit and take appropriate action
1151 * @ah: announce hook
1152 * @l: limit being hit
1153 * @dir: limit direction (PLD_*)
1154 * @rt_count: the number of routes
1156 * The function is called by the route processing core when limit @l
1157 * is breached. It activates the limit and tooks appropriate action
1158 * according to @l->action.
1161 proto_notify_limit(struct announce_hook
*ah
, struct proto_limit
*l
, int dir
, u32 rt_count
)
1163 const char *dir_name
[PLD_MAX
] = { "receive", "import" , "export" };
1164 const byte dir_down
[PLD_MAX
] = { PDC_RX_LIMIT_HIT
, PDC_IN_LIMIT_HIT
, PDC_OUT_LIMIT_HIT
};
1165 struct proto
*p
= ah
->proto
;
1167 if (l
->state
== PLS_BLOCKED
)
1170 /* For warning action, we want the log message every time we hit the limit */
1171 if (!l
->state
|| ((l
->action
== PLA_WARN
) && (rt_count
== l
->limit
)))
1172 log(L_WARN
"Protocol %s hits route %s limit (%d), action: %s",
1173 p
->name
, dir_name
[dir
], l
->limit
, proto_limit_name(l
));
1178 l
->state
= PLS_ACTIVE
;
1182 l
->state
= PLS_BLOCKED
;
1187 l
->state
= PLS_BLOCKED
;
1188 if (p
->proto_state
== PS_UP
)
1189 proto_schedule_down(p
, l
->action
== PLA_RESTART
, dir_down
[dir
]);
1195 proto_verify_limits(struct announce_hook
*ah
)
1197 struct proto_limit
*l
;
1198 struct proto_stats
*stats
= ah
->stats
;
1199 u32 all_routes
= stats
->imp_routes
+ stats
->filt_routes
;
1202 if (l
&& (all_routes
> l
->limit
))
1203 proto_notify_limit(ah
, l
, PLD_RX
, all_routes
);
1206 if (l
&& (stats
->imp_routes
> l
->limit
))
1207 proto_notify_limit(ah
, l
, PLD_IN
, stats
->imp_routes
);
1210 if (l
&& (stats
->exp_routes
> l
->limit
))
1211 proto_notify_limit(ah
, l
, PLD_OUT
, stats
->exp_routes
);
1216 proto_want_core_up(struct proto
*p
)
1218 ASSERT(p
->core_state
== FS_HUNGRY
);
1220 if (!p
->proto
->multitable
)
1222 p
->main_source
= rt_get_source(p
, 0);
1223 rt_lock_source(p
->main_source
);
1225 /* Connect protocol to routing table */
1226 p
->main_ahook
= proto_add_announce_hook(p
, p
->table
, &p
->stats
);
1227 p
->main_ahook
->in_filter
= p
->cf
->in_filter
;
1228 p
->main_ahook
->out_filter
= p
->cf
->out_filter
;
1229 p
->main_ahook
->rx_limit
= p
->cf
->rx_limit
;
1230 p
->main_ahook
->in_limit
= p
->cf
->in_limit
;
1231 p
->main_ahook
->out_limit
= p
->cf
->out_limit
;
1232 p
->main_ahook
->in_keep_filtered
= p
->cf
->in_keep_filtered
;
1234 proto_reset_limit(p
->main_ahook
->rx_limit
);
1235 proto_reset_limit(p
->main_ahook
->in_limit
);
1236 proto_reset_limit(p
->main_ahook
->out_limit
);
1239 p
->core_state
= FS_HAPPY
;
1244 proto_want_export_up(struct proto
*p
)
1246 ASSERT(p
->core_state
== FS_HAPPY
);
1247 ASSERT(p
->export_state
== ES_DOWN
);
1249 proto_link_ahooks(p
);
1250 proto_schedule_feed(p
, 1); /* Sets ES_FEEDING */
1254 proto_want_export_down(struct proto
*p
)
1256 ASSERT(p
->export_state
!= ES_DOWN
);
1258 /* Need to abort feeding */
1259 if (p
->export_state
== ES_FEEDING
)
1260 rt_feed_baby_abort(p
);
1262 p
->export_state
= ES_DOWN
;
1263 proto_unlink_ahooks(p
);
1267 proto_want_core_down(struct proto
*p
)
1269 ASSERT(p
->core_state
== FS_HAPPY
);
1270 ASSERT(p
->export_state
== ES_DOWN
);
1272 p
->core_state
= FS_FLUSHING
;
1274 proto_schedule_flush_loop();
1276 if (!p
->proto
->multitable
)
1278 rt_unlock_source(p
->main_source
);
1279 p
->main_source
= NULL
;
1284 proto_falling_down(struct proto
*p
)
1289 proto_graceful_restart_unlock(p
);
1293 proto_fell_down(struct proto
*p
)
1295 DBG("Protocol %s down\n", p
->name
);
1297 u32 all_routes
= p
->stats
.imp_routes
+ p
->stats
.filt_routes
;
1298 if (all_routes
!= 0)
1299 log(L_ERR
"Protocol %s is down but still has %d routes", p
->name
, all_routes
);
1301 bzero(&p
->stats
, sizeof(struct proto_stats
));
1302 proto_free_ahooks(p
);
1304 if (! p
->proto
->multitable
)
1305 rt_unlock_table(p
->table
);
1307 if (p
->proto
->cleanup
)
1308 p
->proto
->cleanup(p
);
1310 proto_rethink_goal(p
);
1315 * proto_notify_state - notify core about protocol state change
1316 * @p: protocol the state of which has changed
1317 * @ps: the new status
1319 * Whenever a state of a protocol changes due to some event internal
1320 * to the protocol (i.e., not inside a start() or shutdown() hook),
1321 * it should immediately notify the core about the change by calling
1322 * proto_notify_state() which will write the new state to the &proto
1323 * structure and take all the actions necessary to adapt to the new
1324 * state. State change to PS_DOWN immediately frees resources of protocol
1325 * and might execute start callback of protocol; therefore,
1326 * it should be used at tail positions of protocol callbacks.
1329 proto_notify_state(struct proto
*p
, unsigned ps
)
1331 unsigned ops
= p
->proto_state
;
1332 unsigned cs
= p
->core_state
;
1333 unsigned es
= p
->export_state
;
1335 DBG("%s reporting state transition %s/%s -> */%s\n", p
->name
, c_states
[cs
], p_states
[ops
], p_states
[ps
]);
1339 p
->proto_state
= ps
;
1340 p
->last_state_change
= now
;
1345 ASSERT(ops
== PS_DOWN
|| ops
== PS_UP
);
1346 ASSERT(cs
== FS_HUNGRY
|| cs
== FS_HAPPY
);
1349 proto_want_export_down(p
);
1353 ASSERT(ops
== PS_DOWN
|| ops
== PS_START
);
1354 ASSERT(cs
== FS_HUNGRY
|| cs
== FS_HAPPY
);
1355 ASSERT(es
== ES_DOWN
);
1357 if (cs
== FS_HUNGRY
)
1358 proto_want_core_up(p
);
1360 proto_want_export_up(p
);
1364 ASSERT(ops
== PS_START
|| ops
== PS_UP
);
1369 proto_want_export_down(p
);
1371 proto_want_core_down(p
);
1372 proto_falling_down(p
);
1380 proto_want_export_down(p
);
1382 proto_want_core_down(p
);
1384 proto_falling_down(p
);
1386 neigh_prune(); // FIXME convert neighbors to resource?
1390 if (cs
== FS_HUNGRY
) /* Shutdown finished */
1392 proto_log_state_change(p
);
1394 return; /* The protocol might have ceased to exist */
1399 bug("%s: Invalid state %d", p
->name
, ps
);
1402 proto_log_state_change(p
);
1410 proto_state_name(struct proto
*p
)
1412 #define P(x,y) ((x << 4) | y)
1413 switch (P(p
->proto_state
, p
->core_state
))
1415 case P(PS_DOWN
, FS_HUNGRY
): return "down";
1416 case P(PS_START
, FS_HUNGRY
):
1417 case P(PS_START
, FS_HAPPY
): return "start";
1418 case P(PS_UP
, FS_HAPPY
):
1419 switch (p
->export_state
)
1421 case ES_DOWN
: return "wait";
1422 case ES_FEEDING
: return "feed";
1423 case ES_READY
: return "up";
1424 default: return "???";
1426 case P(PS_STOP
, FS_HUNGRY
):
1427 case P(PS_STOP
, FS_FLUSHING
): return "stop";
1428 case P(PS_DOWN
, FS_FLUSHING
): return "flush";
1429 default: return "???";
1435 proto_show_stats(struct proto_stats
*s
, int in_keep_filtered
)
1437 if (in_keep_filtered
)
1438 cli_msg(-1006, " Routes: %u imported, %u filtered, %u exported, %u preferred",
1439 s
->imp_routes
, s
->filt_routes
, s
->exp_routes
, s
->pref_routes
);
1441 cli_msg(-1006, " Routes: %u imported, %u exported, %u preferred",
1442 s
->imp_routes
, s
->exp_routes
, s
->pref_routes
);
1444 cli_msg(-1006, " Route change stats: received rejected filtered ignored accepted");
1445 cli_msg(-1006, " Import updates: %10u %10u %10u %10u %10u",
1446 s
->imp_updates_received
, s
->imp_updates_invalid
,
1447 s
->imp_updates_filtered
, s
->imp_updates_ignored
,
1448 s
->imp_updates_accepted
);
1449 cli_msg(-1006, " Import withdraws: %10u %10u --- %10u %10u",
1450 s
->imp_withdraws_received
, s
->imp_withdraws_invalid
,
1451 s
->imp_withdraws_ignored
, s
->imp_withdraws_accepted
);
1452 cli_msg(-1006, " Export updates: %10u %10u %10u --- %10u",
1453 s
->exp_updates_received
, s
->exp_updates_rejected
,
1454 s
->exp_updates_filtered
, s
->exp_updates_accepted
);
1455 cli_msg(-1006, " Export withdraws: %10u --- --- --- %10u",
1456 s
->exp_withdraws_received
, s
->exp_withdraws_accepted
);
1460 proto_show_limit(struct proto_limit
*l
, const char *dsc
)
1465 cli_msg(-1006, " %-16s%d%s", dsc
, l
->limit
, l
->state
? " [HIT]" : "");
1466 cli_msg(-1006, " Action: %s", proto_limit_name(l
));
1470 proto_show_basic_info(struct proto
*p
)
1472 // cli_msg(-1006, " Table: %s", p->table->name);
1473 cli_msg(-1006, " Preference: %d", p
->preference
);
1474 cli_msg(-1006, " Input filter: %s", filter_name(p
->cf
->in_filter
));
1475 cli_msg(-1006, " Output filter: %s", filter_name(p
->cf
->out_filter
));
1477 if (graceful_restart_state
== GRS_ACTIVE
)
1478 cli_msg(-1006, " GR recovery: %s%s",
1479 p
->gr_lock
? " pending" : "",
1480 p
->gr_wait
? " waiting" : "");
1482 proto_show_limit(p
->cf
->rx_limit
, "Receive limit:");
1483 proto_show_limit(p
->cf
->in_limit
, "Import limit:");
1484 proto_show_limit(p
->cf
->out_limit
, "Export limit:");
1486 if (p
->proto_state
!= PS_DOWN
)
1487 proto_show_stats(&p
->stats
, p
->cf
->in_keep_filtered
);
1491 proto_cmd_show(struct proto
*p
, uint verbose
, int cnt
)
1493 byte buf
[256], tbuf
[TM_DATETIME_BUFFER_SIZE
];
1495 /* First protocol - show header */
1497 cli_msg(-2002, "name proto table state since info");
1500 if (p
->proto
->get_status
)
1501 p
->proto
->get_status(p
, buf
);
1502 tm_format_datetime(tbuf
, &config
->tf_proto
, p
->last_state_change
);
1503 cli_msg(-1002, "%-8s %-8s %-8s %-5s %-10s %s",
1507 proto_state_name(p
),
1513 cli_msg(-1006, " Description: %s", p
->cf
->dsc
);
1514 if (p
->cf
->router_id
)
1515 cli_msg(-1006, " Router ID: %R", p
->cf
->router_id
);
1517 if (p
->proto
->show_proto_info
)
1518 p
->proto
->show_proto_info(p
);
1520 proto_show_basic_info(p
);
1527 proto_cmd_disable(struct proto
*p
, uint arg UNUSED
, int cnt UNUSED
)
1531 cli_msg(-8, "%s: already disabled", p
->name
);
1535 log(L_INFO
"Disabling protocol %s", p
->name
);
1537 p
->down_code
= PDC_CMD_DISABLE
;
1538 proto_rethink_goal(p
);
1539 cli_msg(-9, "%s: disabled", p
->name
);
1543 proto_cmd_enable(struct proto
*p
, uint arg UNUSED
, int cnt UNUSED
)
1547 cli_msg(-10, "%s: already enabled", p
->name
);
1551 log(L_INFO
"Enabling protocol %s", p
->name
);
1553 proto_rethink_goal(p
);
1554 cli_msg(-11, "%s: enabled", p
->name
);
1558 proto_cmd_restart(struct proto
*p
, uint arg UNUSED
, int cnt UNUSED
)
1562 cli_msg(-8, "%s: already disabled", p
->name
);
1566 log(L_INFO
"Restarting protocol %s", p
->name
);
1568 p
->down_code
= PDC_CMD_RESTART
;
1569 proto_rethink_goal(p
);
1571 proto_rethink_goal(p
);
1572 cli_msg(-12, "%s: restarted", p
->name
);
1576 proto_cmd_reload(struct proto
*p
, uint dir
, int cnt UNUSED
)
1580 cli_msg(-8, "%s: already disabled", p
->name
);
1584 /* If the protocol in not UP, it has no routes */
1585 if (p
->proto_state
!= PS_UP
)
1588 log(L_INFO
"Reloading protocol %s", p
->name
);
1590 /* re-importing routes */
1591 if (dir
!= CMD_RELOAD_OUT
)
1593 if (! (p
->reload_routes
&& p
->reload_routes(p
)))
1595 cli_msg(-8006, "%s: reload failed", p
->name
);
1600 * Should be done before reload_routes() hook?
1601 * Perhaps, but these hooks work asynchronously.
1603 if (!p
->proto
->multitable
)
1605 proto_reset_limit(p
->main_ahook
->rx_limit
);
1606 proto_reset_limit(p
->main_ahook
->in_limit
);
1610 /* re-exporting routes */
1611 if (dir
!= CMD_RELOAD_IN
)
1612 proto_request_feeding(p
);
1614 cli_msg(-15, "%s: reloading", p
->name
);
1618 proto_cmd_debug(struct proto
*p
, uint mask
, int cnt UNUSED
)
1624 proto_cmd_mrtdump(struct proto
*p
, uint mask
, int cnt UNUSED
)
1630 proto_apply_cmd_symbol(struct symbol
*s
, void (* cmd
)(struct proto
*, uint
, int), uint arg
)
1632 if (s
->class != SYM_PROTO
)
1634 cli_msg(9002, "%s is not a protocol", s
->name
);
1638 cmd(((struct proto_config
*)s
->def
)->proto
, arg
, 0);
1643 proto_apply_cmd_patt(char *patt
, void (* cmd
)(struct proto
*, uint
, int), uint arg
)
1648 WALK_LIST(nn
, proto_list
)
1650 struct proto
*p
= SKIP_BACK(struct proto
, glob_node
, nn
);
1652 if (!patt
|| patmatch(patt
, p
->name
))
1657 cli_msg(8003, "No protocols match");
1663 proto_apply_cmd(struct proto_spec ps
, void (* cmd
)(struct proto
*, uint
, int),
1664 int restricted
, uint arg
)
1666 if (restricted
&& cli_access_restricted())
1670 proto_apply_cmd_patt(ps
.ptr
, cmd
, arg
);
1672 proto_apply_cmd_symbol(ps
.ptr
, cmd
, arg
);
1676 proto_get_named(struct symbol
*sym
, struct protocol
*pr
)
1678 struct proto
*p
, *q
;
1682 if (sym
->class != SYM_PROTO
)
1683 cf_error("%s: Not a protocol", sym
->name
);
1684 p
= ((struct proto_config
*)sym
->def
)->proto
;
1685 if (!p
|| p
->proto
!= pr
)
1686 cf_error("%s: Not a %s protocol", sym
->name
, pr
->name
);
1691 WALK_LIST(q
, active_proto_list
)
1695 cf_error("There are multiple %s protocols running", pr
->name
);
1699 cf_error("There is no %s protocol running", pr
->name
);