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/timer.h"
17 #include "lib/string.h"
18 #include "conf/conf.h"
19 #include "nest/route.h"
20 #include "nest/iface.h"
22 #include "filter/filter.h"
27 static list protocol_list
;
28 struct protocol
*class_to_protocol
[PROTOCOL__MAX
];
30 #define PD(pr, msg, args...) do { if (pr->debug & D_STATES) { log(L_TRACE "%s: " msg, pr->name , ## args); } } while(0)
32 static timer
*proto_shutdown_timer
;
33 static timer
*gr_wait_timer
;
40 static int graceful_restart_state
;
41 static u32 graceful_restart_locks
;
43 static char *p_states
[] = { "DOWN", "START", "UP", "STOP" };
44 static char *c_states
[] = { "DOWN", "START", "UP", "FLUSHING" };
46 extern struct protocol proto_unix_iface
;
48 static void proto_shutdown_loop(timer
*);
49 static void proto_rethink_goal(struct proto
*p
);
50 static char *proto_state_name(struct proto
*p
);
51 static void channel_verify_limits(struct channel
*c
);
52 static inline void channel_reset_limit(struct channel_limit
*l
);
55 static inline int proto_is_done(struct proto
*p
)
56 { return (p
->proto_state
== PS_DOWN
) && (p
->active_channels
== 0); }
58 static inline int channel_is_active(struct channel
*c
)
59 { return (c
->channel_state
== CS_START
) || (c
->channel_state
== CS_UP
); }
62 proto_log_state_change(struct proto
*p
)
64 if (p
->debug
& D_STATES
)
66 char *name
= proto_state_name(p
);
67 if (name
!= p
->last_state_name_announced
)
69 p
->last_state_name_announced
= name
;
70 PD(p
, "State changed to %s", proto_state_name(p
));
74 p
->last_state_name_announced
= NULL
;
78 struct channel_config
*
79 proto_cf_find_channel(struct proto_config
*pc
, uint net_type
)
81 struct channel_config
*cc
;
83 WALK_LIST(cc
, pc
->channels
)
84 if (cc
->net_type
== net_type
)
91 * proto_find_channel_by_table - find channel connected to a routing table
92 * @p: protocol instance
95 * Returns pointer to channel or NULL
98 proto_find_channel_by_table(struct proto
*p
, struct rtable
*t
)
102 WALK_LIST(c
, p
->channels
)
110 * proto_find_channel_by_name - find channel by its name
111 * @p: protocol instance
114 * Returns pointer to channel or NULL
117 proto_find_channel_by_name(struct proto
*p
, const char *n
)
121 WALK_LIST(c
, p
->channels
)
122 if (!strcmp(c
->name
, n
))
129 * proto_add_channel - connect protocol to a routing table
130 * @p: protocol instance
131 * @cf: channel configuration
133 * This function creates a channel between the protocol instance @p and the
134 * routing table specified in the configuration @cf, making the protocol hear
135 * all changes in the table and allowing the protocol to update routes in the
138 * The channel is linked in the protocol channel list and when active also in
139 * the table channel list. Channels are allocated from the global resource pool
140 * (@proto_pool) and they are automatically freed when the protocol is removed.
144 proto_add_channel(struct proto
*p
, struct channel_config
*cf
)
146 struct channel
*c
= mb_allocz(proto_pool
, cf
->channel
->channel_size
);
149 c
->channel
= cf
->channel
;
151 c
->table
= cf
->table
->table
;
153 c
->in_filter
= cf
->in_filter
;
154 c
->out_filter
= cf
->out_filter
;
155 c
->rx_limit
= cf
->rx_limit
;
156 c
->in_limit
= cf
->in_limit
;
157 c
->out_limit
= cf
->out_limit
;
159 c
->net_type
= cf
->net_type
;
160 c
->ra_mode
= cf
->ra_mode
;
161 c
->preference
= cf
->preference
;
162 c
->merge_limit
= cf
->merge_limit
;
163 c
->in_keep_filtered
= cf
->in_keep_filtered
;
165 c
->channel_state
= CS_DOWN
;
166 c
->export_state
= ES_DOWN
;
167 c
->last_state_change
= current_time();
168 c
->last_tx_filter_change
= current_time();
171 CALL(c
->channel
->init
, c
, cf
);
173 add_tail(&p
->channels
, &c
->n
);
175 PD(p
, "Channel %s connected to table %s", c
->name
, c
->table
->name
);
181 proto_remove_channel(struct proto
*p
, struct channel
*c
)
183 ASSERT(c
->channel_state
== CS_DOWN
);
185 PD(p
, "Channel %s removed", c
->name
);
193 proto_start_channels(struct proto
*p
)
196 WALK_LIST(c
, p
->channels
)
198 channel_set_state(c
, CS_UP
);
202 proto_pause_channels(struct proto
*p
)
205 WALK_LIST(c
, p
->channels
)
206 if (!c
->disabled
&& channel_is_active(c
))
207 channel_set_state(c
, CS_START
);
211 proto_stop_channels(struct proto
*p
)
214 WALK_LIST(c
, p
->channels
)
215 if (!c
->disabled
&& channel_is_active(c
))
216 channel_set_state(c
, CS_FLUSHING
);
220 proto_remove_channels(struct proto
*p
)
223 WALK_LIST_FIRST(c
, p
->channels
)
224 proto_remove_channel(p
, c
);
228 channel_schedule_feed(struct channel
*c
, int initial
)
230 // DBG("%s: Scheduling meal\n", p->name);
231 ASSERT(c
->channel_state
== CS_UP
);
233 c
->export_state
= ES_FEEDING
;
234 c
->refeeding
= !initial
;
236 ev_schedule(c
->feed_event
);
240 channel_feed_loop(void *ptr
)
242 struct channel
*c
= ptr
;
244 if (c
->export_state
!= ES_FEEDING
)
248 if (c
->proto
->feed_begin
)
249 c
->proto
->feed_begin(c
, !c
->refeeding
);
251 // DBG("Feeding protocol %s continued\n", p->name);
252 if (!rt_feed_channel(c
))
254 ev_schedule(c
->feed_event
);
258 // DBG("Feeding protocol %s finished\n", p->name);
259 c
->export_state
= ES_READY
;
260 // proto_log_state_change(p);
262 if (c
->proto
->feed_end
)
263 c
->proto
->feed_end(c
);
268 channel_start_export(struct channel
*c
)
270 ASSERT(c
->channel_state
== CS_UP
);
271 ASSERT(c
->export_state
== ES_DOWN
);
273 channel_schedule_feed(c
, 1); /* Sets ES_FEEDING */
277 channel_stop_export(struct channel
*c
)
279 /* Need to abort feeding */
280 if (c
->export_state
== ES_FEEDING
)
281 rt_feed_channel_abort(c
);
283 c
->export_state
= ES_DOWN
;
284 c
->stats
.exp_routes
= 0;
288 channel_do_start(struct channel
*c
)
290 rt_lock_table(c
->table
);
291 add_tail(&c
->table
->channels
, &c
->table_node
);
292 c
->proto
->active_channels
++;
294 c
->feed_event
= ev_new_init(c
->proto
->pool
, channel_feed_loop
, c
);
296 channel_reset_limit(&c
->rx_limit
);
297 channel_reset_limit(&c
->in_limit
);
298 channel_reset_limit(&c
->out_limit
);
300 CALL(c
->channel
->start
, c
);
304 channel_do_flush(struct channel
*c
)
306 rt_schedule_prune(c
->table
);
310 channel_graceful_restart_unlock(c
);
312 CALL(c
->channel
->shutdown
, c
);
316 channel_do_down(struct channel
*c
)
318 rem_node(&c
->table_node
);
319 rt_unlock_table(c
->table
);
320 c
->proto
->active_channels
--;
322 if ((c
->stats
.imp_routes
+ c
->stats
.filt_routes
) != 0)
323 log(L_ERR
"%s: Channel %s is down but still has some routes", c
->proto
->name
, c
->name
);
325 memset(&c
->stats
, 0, sizeof(struct proto_stats
));
327 CALL(c
->channel
->cleanup
, c
);
329 /* Schedule protocol shutddown */
330 if (proto_is_done(c
->proto
))
331 ev_schedule(c
->proto
->event
);
335 channel_set_state(struct channel
*c
, uint state
)
337 uint cs
= c
->channel_state
;
338 uint es
= c
->export_state
;
340 DBG("%s reporting channel %s state transition %s -> %s\n", c
->proto
->name
, c
->name
, c_states
[cs
], c_states
[state
]);
344 c
->channel_state
= state
;
345 c
->last_state_change
= current_time();
350 ASSERT(cs
== CS_DOWN
|| cs
== CS_UP
);
356 channel_stop_export(c
);
361 ASSERT(cs
== CS_DOWN
|| cs
== CS_START
);
366 if (!c
->gr_wait
&& c
->proto
->rt_notify
)
367 channel_start_export(c
);
372 ASSERT(cs
== CS_START
|| cs
== CS_UP
);
375 channel_stop_export(c
);
381 ASSERT(cs
== CS_FLUSHING
);
389 // XXXX proto_log_state_change(c);
393 * channel_request_feeding - request feeding routes to the channel
396 * Sometimes it is needed to send again all routes to the channel. This is
397 * called feeding and can be requested by this function. This would cause
398 * channel export state transition to ES_FEEDING (during feeding) and when
399 * completed, it will switch back to ES_READY. This function can be called
400 * even when feeding is already running, in that case it is restarted.
403 channel_request_feeding(struct channel
*c
)
405 ASSERT(c
->channel_state
== CS_UP
);
407 /* Do nothing if we are still waiting for feeding */
408 if (c
->export_state
== ES_DOWN
)
411 /* If we are already feeding, we want to restart it */
412 if (c
->export_state
== ES_FEEDING
)
414 /* Unless feeding is in initial state */
418 rt_feed_channel_abort(c
);
421 channel_reset_limit(&c
->out_limit
);
423 /* Hack: reset exp_routes during refeed, and do not decrease it later */
424 c
->stats
.exp_routes
= 0;
426 channel_schedule_feed(c
, 0); /* Sets ES_FEEDING */
427 // proto_log_state_change(c);
431 channel_reloadable(struct channel
*c
)
433 return c
->proto
->reload_routes
&& c
->reloadable
;
437 channel_request_reload(struct channel
*c
)
439 ASSERT(c
->channel_state
== CS_UP
);
440 ASSERT(channel_reloadable(c
));
442 c
->proto
->reload_routes(c
);
445 * Should this be done before reload_routes() hook?
446 * Perhaps, but routes are updated asynchronously.
448 channel_reset_limit(&c
->rx_limit
);
449 channel_reset_limit(&c
->in_limit
);
452 const struct channel_class channel_basic
= {
453 .channel_size
= sizeof(struct channel
),
454 .config_size
= sizeof(struct channel_config
)
458 channel_config_new(const struct channel_class
*cc
, const char *name
, uint net_type
, struct proto_config
*proto
)
460 struct channel_config
*cf
= NULL
;
461 struct rtable_config
*tab
= NULL
;
465 if (!net_val_match(net_type
, proto
->protocol
->channel_mask
))
466 cf_error("Unsupported channel type");
468 if (proto
->net_type
&& (net_type
!= proto
->net_type
))
469 cf_error("Different channel type");
471 tab
= new_config
->def_tables
[net_type
];
477 cf
= cfg_allocz(cc
->config_size
);
482 cf
->out_filter
= FILTER_REJECT
;
484 cf
->net_type
= net_type
;
485 cf
->ra_mode
= RA_OPTIMAL
;
486 cf
->preference
= proto
->protocol
->preference
;
488 add_tail(&proto
->channels
, &cf
->n
);
494 channel_config_get(const struct channel_class
*cc
, const char *name
, uint net_type
, struct proto_config
*proto
)
496 struct channel_config
*cf
;
498 /* We are using name as token, so no strcmp() */
499 WALK_LIST(cf
, proto
->channels
)
500 if (cf
->name
== name
)
502 /* Allow to redefine channel only if inherited from template */
503 if (cf
->parent
== proto
)
504 cf_error("Multiple %s channels", name
);
510 return channel_config_new(cc
, name
, net_type
, proto
);
513 struct channel_config
*
514 channel_copy_config(struct channel_config
*src
, struct proto_config
*proto
)
516 struct channel_config
*dst
= cfg_alloc(src
->channel
->config_size
);
518 memcpy(dst
, src
, src
->channel
->config_size
);
519 add_tail(&proto
->channels
, &dst
->n
);
520 CALL(src
->channel
->copy_config
, dst
, src
);
526 static int reconfigure_type
; /* Hack to propagate type info to channel_reconfigure() */
529 channel_reconfigure(struct channel
*c
, struct channel_config
*cf
)
531 /* FIXME: better handle these changes, also handle in_keep_filtered */
532 if ((c
->table
!= cf
->table
->table
) || (cf
->ra_mode
&& (c
->ra_mode
!= cf
->ra_mode
)))
535 /* Note that filter_same() requires arguments in (new, old) order */
536 int import_changed
= !filter_same(cf
->in_filter
, c
->in_filter
);
537 int export_changed
= !filter_same(cf
->out_filter
, c
->out_filter
);
539 if (c
->preference
!= cf
->preference
)
542 if (c
->merge_limit
!= cf
->merge_limit
)
545 /* Reconfigure channel fields */
546 c
->in_filter
= cf
->in_filter
;
547 c
->out_filter
= cf
->out_filter
;
548 c
->rx_limit
= cf
->rx_limit
;
549 c
->in_limit
= cf
->in_limit
;
550 c
->out_limit
= cf
->out_limit
;
552 // c->ra_mode = cf->ra_mode;
553 c
->merge_limit
= cf
->merge_limit
;
554 c
->preference
= cf
->preference
;
555 c
->in_keep_filtered
= cf
->in_keep_filtered
;
557 channel_verify_limits(c
);
560 c
->last_tx_filter_change
= current_time();
562 /* Execute channel-specific reconfigure hook */
563 if (c
->channel
->reconfigure
&& !c
->channel
->reconfigure(c
, cf
))
566 /* If the channel is not open, it has no routes and we cannot reload it anyways */
567 if (c
->channel_state
!= CS_UP
)
570 if (reconfigure_type
== RECONFIG_SOFT
)
573 log(L_INFO
"Channel %s.%s changed import", c
->proto
->name
, c
->name
);
576 log(L_INFO
"Channel %s.%s changed export", c
->proto
->name
, c
->name
);
581 /* Route reload may be not supported */
582 if (import_changed
&& !channel_reloadable(c
))
585 if (import_changed
|| export_changed
)
586 log(L_INFO
"Reloading channel %s.%s", c
->proto
->name
, c
->name
);
589 channel_request_reload(c
);
592 channel_request_feeding(c
);
599 proto_configure_channel(struct proto
*p
, struct channel
**pc
, struct channel_config
*cf
)
601 struct channel
*c
= *pc
;
605 *pc
= proto_add_channel(p
, cf
);
609 if (c
->channel_state
!= CS_DOWN
)
611 log(L_INFO
"Cannot remove channel %s.%s", c
->proto
->name
, c
->name
);
615 proto_remove_channel(p
, c
);
620 if (!channel_reconfigure(c
, cf
))
622 log(L_INFO
"Cannot reconfigure channel %s.%s", c
->proto
->name
, c
->name
);
632 proto_event(void *ptr
)
634 struct proto
*p
= ptr
;
644 if (p
->proto
== &proto_unix_iface
)
649 if (proto_is_done(p
))
651 if (p
->proto
->cleanup
)
652 p
->proto
->cleanup(p
);
655 proto_log_state_change(p
);
656 proto_rethink_goal(p
);
662 * proto_new - create a new protocol instance
663 * @c: protocol configuration
665 * When a new configuration has been read in, the core code starts
666 * initializing all the protocol instances configured by calling their
667 * init() hooks with the corresponding instance configuration. The initialization
668 * code of the protocol is expected to create a new instance according to the
669 * configuration by calling this function and then modifying the default settings
670 * to values wanted by the protocol.
673 proto_new(struct proto_config
*cf
)
675 struct proto
*p
= mb_allocz(proto_pool
, cf
->protocol
->proto_size
);
678 p
->debug
= cf
->debug
;
679 p
->mrtdump
= cf
->mrtdump
;
681 p
->proto
= cf
->protocol
;
682 p
->net_type
= cf
->net_type
;
683 p
->disabled
= cf
->disabled
;
684 p
->hash_key
= random_u32();
687 init_list(&p
->channels
);
692 static struct proto
*
693 proto_init(struct proto_config
*c
, node
*n
)
695 struct protocol
*pr
= c
->protocol
;
696 struct proto
*p
= pr
->init(c
);
698 p
->proto_state
= PS_DOWN
;
699 p
->last_state_change
= current_time();
701 insert_node(&p
->n
, n
);
703 p
->event
= ev_new_init(proto_pool
, proto_event
, p
);
705 PD(p
, "Initializing%s", p
->disabled
? " [disabled]" : "");
711 proto_start(struct proto
*p
)
713 /* Here we cannot use p->cf->name since it won't survive reconfiguration */
714 p
->pool
= rp_new(proto_pool
, p
->proto
->name
);
716 if (graceful_restart_state
== GRS_INIT
)
722 * proto_config_new - create a new protocol configuration
723 * @pr: protocol the configuration will belong to
724 * @class: SYM_PROTO or SYM_TEMPLATE
726 * Whenever the configuration file says that a new instance
727 * of a routing protocol should be created, the parser calls
728 * proto_config_new() to create a configuration entry for this
729 * instance (a structure staring with the &proto_config header
730 * containing all the generic items followed by protocol-specific
731 * ones). Also, the configuration entry gets added to the list
732 * of protocol instances kept in the configuration.
734 * The function is also used to create protocol templates (when class
735 * SYM_TEMPLATE is specified), the only difference is that templates
736 * are not added to the list of protocol instances and therefore not
737 * initialized during protos_commit()).
740 proto_config_new(struct protocol
*pr
, int class)
742 struct proto_config
*cf
= cfg_allocz(pr
->config_size
);
744 if (class == SYM_PROTO
)
745 add_tail(&new_config
->protos
, &cf
->n
);
747 cf
->global
= new_config
;
751 cf
->debug
= new_config
->proto_default_debug
;
752 cf
->mrtdump
= new_config
->proto_default_mrtdump
;
754 init_list(&cf
->channels
);
761 * proto_copy_config - copy a protocol configuration
762 * @dest: destination protocol configuration
763 * @src: source protocol configuration
765 * Whenever a new instance of a routing protocol is created from the
766 * template, proto_copy_config() is called to copy a content of
767 * the source protocol configuration to the new protocol configuration.
768 * Name, class and a node in protos list of @dest are kept intact.
769 * copy_config() protocol hook is used to copy protocol-specific data.
772 proto_copy_config(struct proto_config
*dest
, struct proto_config
*src
)
774 struct channel_config
*cc
;
779 if (dest
->protocol
!= src
->protocol
)
780 cf_error("Can't copy configuration from a different protocol type");
782 if (dest
->protocol
->copy_config
== NULL
)
783 cf_error("Inheriting configuration for %s is not supported", src
->protocol
->name
);
785 DBG("Copying configuration from %s to %s\n", src
->name
, dest
->name
);
788 * Copy struct proto_config here. Keep original node, class and name.
789 * protocol-specific config copy is handled by protocol copy_config() hook
793 old_class
= dest
->class;
794 old_name
= dest
->name
;
796 memcpy(dest
, src
, src
->protocol
->config_size
);
799 dest
->class = old_class
;
800 dest
->name
= old_name
;
801 init_list(&dest
->channels
);
803 WALK_LIST(cc
, src
->channels
)
804 channel_copy_config(cc
, dest
);
806 /* FIXME: allow for undefined copy_config */
807 dest
->protocol
->copy_config(dest
, src
);
811 * protos_preconfig - pre-configuration processing
812 * @c: new configuration
814 * This function calls the preconfig() hooks of all routing
815 * protocols available to prepare them for reading of the new
819 protos_preconfig(struct config
*c
)
823 init_list(&c
->protos
);
824 DBG("Protocol preconfig:");
825 WALK_LIST(p
, protocol_list
)
836 proto_reconfigure(struct proto
*p
, struct proto_config
*oc
, struct proto_config
*nc
, int type
)
838 /* If the protocol is DOWN, we just restart it */
839 if (p
->proto_state
== PS_DOWN
)
842 /* If there is a too big change in core attributes, ... */
843 if ((nc
->protocol
!= oc
->protocol
) ||
844 (nc
->net_type
!= oc
->net_type
) ||
845 (nc
->disabled
!= p
->disabled
) ||
846 (nc
->vrf
!= oc
->vrf
))
850 p
->debug
= nc
->debug
;
851 p
->mrtdump
= nc
->mrtdump
;
852 reconfigure_type
= type
;
854 /* Execute protocol specific reconfigure hook */
855 if (!p
->proto
->reconfigure
|| !p
->proto
->reconfigure(p
, nc
))
858 DBG("\t%s: same\n", oc
->name
);
859 PD(p
, "Reconfigured");
866 * protos_commit - commit new protocol configuration
867 * @new: new configuration
868 * @old: old configuration or %NULL if it's boot time config
869 * @force_reconfig: force restart of all protocols (used for example
870 * when the router ID changes)
871 * @type: type of reconfiguration (RECONFIG_SOFT or RECONFIG_HARD)
873 * Scan differences between @old and @new configuration and adjust all
874 * protocol instances to conform to the new configuration.
876 * When a protocol exists in the new configuration, but it doesn't in the
877 * original one, it's immediately started. When a collision with the other
878 * running protocol would arise, the new protocol will be temporarily stopped
879 * by the locking mechanism.
881 * When a protocol exists in the old configuration, but it doesn't in the
882 * new one, it's shut down and deleted after the shutdown completes.
884 * When a protocol exists in both configurations, the core decides
885 * whether it's possible to reconfigure it dynamically - it checks all
886 * the core properties of the protocol (changes in filters are ignored
887 * if type is RECONFIG_SOFT) and if they match, it asks the
888 * reconfigure() hook of the protocol to see if the protocol is able
889 * to switch to the new configuration. If it isn't possible, the
890 * protocol is shut down and a new instance is started with the new
891 * configuration after the shutdown is completed.
894 protos_commit(struct config
*new, struct config
*old
, int force_reconfig
, int type
)
896 struct proto_config
*oc
, *nc
;
902 DBG("protos_commit:\n");
905 WALK_LIST(oc
, old
->protos
)
908 sym
= cf_find_symbol(new, oc
->name
);
909 if (sym
&& sym
->class == SYM_PROTO
&& !new->shutdown
)
911 /* Found match, let's check if we can smoothly switch to new configuration */
912 /* No need to check description */
916 /* We will try to reconfigure protocol p */
917 if (! force_reconfig
&& proto_reconfigure(p
, oc
, nc
, type
))
920 /* Unsuccessful, we will restart it */
921 if (!p
->disabled
&& !nc
->disabled
)
922 log(L_INFO
"Restarting protocol %s", p
->name
);
923 else if (p
->disabled
&& !nc
->disabled
)
924 log(L_INFO
"Enabling protocol %s", p
->name
);
925 else if (!p
->disabled
&& nc
->disabled
)
926 log(L_INFO
"Disabling protocol %s", p
->name
);
928 p
->down_code
= nc
->disabled
? PDC_CF_DISABLE
: PDC_CF_RESTART
;
931 else if (!new->shutdown
)
933 log(L_INFO
"Removing protocol %s", p
->name
);
934 p
->down_code
= PDC_CF_REMOVE
;
937 else /* global shutdown */
939 p
->down_code
= PDC_CMD_SHUTDOWN
;
943 p
->reconfiguring
= 1;
944 config_add_obstacle(old
);
945 proto_rethink_goal(p
);
949 struct proto
*first_dev_proto
= NULL
;
951 n
= NODE
&(proto_list
.head
);
952 WALK_LIST(nc
, new->protos
)
955 /* Not a first-time configuration */
957 log(L_INFO
"Adding protocol %s", nc
->name
);
959 p
= proto_init(nc
, n
);
962 if (p
->proto
== &proto_unix_iface
)
968 DBG("Protocol start\n");
970 /* Start device protocol first */
972 proto_rethink_goal(first_dev_proto
);
974 /* Determine router ID for the first time - it has to be here and not in
975 global_commit() because it is postponed after start of device protocol */
976 if (!config
->router_id
)
978 config
->router_id
= if_choose_router_id(config
->router_id_from
, 0);
979 if (!config
->router_id
)
980 die("Cannot determine router ID, please configure it manually");
983 /* Start all new protocols */
984 WALK_LIST_DELSAFE(p
, n
, proto_list
)
985 proto_rethink_goal(p
);
989 proto_rethink_goal(struct proto
*p
)
994 if (p
->reconfiguring
&& !p
->active
)
996 struct proto_config
*nc
= p
->cf_new
;
998 DBG("%s has shut down for reconfiguration\n", p
->name
);
1000 config_del_obstacle(p
->cf
->global
);
1001 proto_remove_channels(p
);
1004 mb_free(p
->message
);
1008 p
= proto_init(nc
, n
);
1011 /* Determine what state we want to reach */
1012 if (p
->disabled
|| p
->reconfiguring
)
1023 DBG("Kicking %s up\n", p
->name
);
1026 proto_notify_state(p
, (q
->start
? q
->start(p
) : PS_UP
));
1031 if (p
->proto_state
== PS_START
|| p
->proto_state
== PS_UP
)
1034 DBG("Kicking %s down\n", p
->name
);
1035 PD(p
, "Shutting down");
1036 proto_notify_state(p
, (q
->shutdown
? q
->shutdown(p
) : PS_DOWN
));
1043 * DOC: Graceful restart recovery
1045 * Graceful restart of a router is a process when the routing plane (e.g. BIRD)
1046 * restarts but both the forwarding plane (e.g kernel routing table) and routing
1047 * neighbors keep proper routes, and therefore uninterrupted packet forwarding
1050 * BIRD implements graceful restart recovery by deferring export of routes to
1051 * protocols until routing tables are refilled with the expected content. After
1052 * start, protocols generate routes as usual, but routes are not propagated to
1053 * them, until protocols report that they generated all routes. After that,
1054 * graceful restart recovery is finished and the export (and the initial feed)
1055 * to protocols is enabled.
1057 * When graceful restart recovery need is detected during initialization, then
1058 * enabled protocols are marked with @gr_recovery flag before start. Such
1059 * protocols then decide how to proceed with graceful restart, participation is
1060 * voluntary. Protocols could lock the recovery for each channel by function
1061 * channel_graceful_restart_lock() (state stored in @gr_lock flag), which means
1062 * that they want to postpone the end of the recovery until they converge and
1063 * then unlock it. They also could set @gr_wait before advancing to %PS_UP,
1064 * which means that the core should defer route export to that channel until
1065 * the end of the recovery. This should be done by protocols that expect their
1066 * neigbors to keep the proper routes (kernel table, BGP sessions with BGP
1067 * graceful restart capability).
1069 * The graceful restart recovery is finished when either all graceful restart
1070 * locks are unlocked or when graceful restart wait timer fires.
1074 static void graceful_restart_done(timer
*t
);
1077 * graceful_restart_recovery - request initial graceful restart recovery
1079 * Called by the platform initialization code if the need for recovery
1080 * after graceful restart is detected during boot. Have to be called
1081 * before protos_commit().
1084 graceful_restart_recovery(void)
1086 graceful_restart_state
= GRS_INIT
;
1090 * graceful_restart_init - initialize graceful restart
1092 * When graceful restart recovery was requested, the function starts an active
1093 * phase of the recovery and initializes graceful restart wait timer. The
1094 * function have to be called after protos_commit().
1097 graceful_restart_init(void)
1099 if (!graceful_restart_state
)
1102 log(L_INFO
"Graceful restart started");
1104 if (!graceful_restart_locks
)
1106 graceful_restart_done(NULL
);
1110 graceful_restart_state
= GRS_ACTIVE
;
1111 gr_wait_timer
= tm_new_init(proto_pool
, graceful_restart_done
, NULL
, 0, 0);
1112 tm_start(gr_wait_timer
, config
->gr_wait S
);
1116 * graceful_restart_done - finalize graceful restart
1119 * When there are no locks on graceful restart, the functions finalizes the
1120 * graceful restart recovery. Protocols postponing route export until the end of
1121 * the recovery are awakened and the export to them is enabled. All other
1122 * related state is cleared. The function is also called when the graceful
1123 * restart wait timer fires (but there are still some locks).
1126 graceful_restart_done(timer
*t UNUSED
)
1128 log(L_INFO
"Graceful restart done");
1129 graceful_restart_state
= GRS_DONE
;
1132 WALK_LIST(p
, proto_list
)
1134 if (!p
->gr_recovery
)
1138 WALK_LIST(c
, p
->channels
)
1140 /* Resume postponed export of routes */
1141 if ((c
->channel_state
== CS_UP
) && c
->gr_wait
&& c
->proto
->rt_notify
)
1142 channel_start_export(c
);
1152 graceful_restart_locks
= 0;
1156 graceful_restart_show_status(void)
1158 if (graceful_restart_state
!= GRS_ACTIVE
)
1161 cli_msg(-24, "Graceful restart recovery in progress");
1162 cli_msg(-24, " Waiting for %d channels to recover", graceful_restart_locks
);
1163 cli_msg(-24, " Wait timer is %t/%u", tm_remains(gr_wait_timer
), config
->gr_wait
);
1167 * channel_graceful_restart_lock - lock graceful restart by channel
1168 * @p: channel instance
1170 * This function allows a protocol to postpone the end of graceful restart
1171 * recovery until it converges. The lock is removed when the protocol calls
1172 * channel_graceful_restart_unlock() or when the channel is closed.
1174 * The function have to be called during the initial phase of graceful restart
1175 * recovery and only for protocols that are part of graceful restart (i.e. their
1176 * @gr_recovery is set), which means it should be called from protocol start
1180 channel_graceful_restart_lock(struct channel
*c
)
1182 ASSERT(graceful_restart_state
== GRS_INIT
);
1183 ASSERT(c
->proto
->gr_recovery
);
1189 graceful_restart_locks
++;
1193 * channel_graceful_restart_unlock - unlock graceful restart by channel
1194 * @p: channel instance
1196 * This function unlocks a lock from channel_graceful_restart_lock(). It is also
1197 * automatically called when the lock holding protocol went down.
1200 channel_graceful_restart_unlock(struct channel
*c
)
1206 graceful_restart_locks
--;
1208 if ((graceful_restart_state
== GRS_ACTIVE
) && !graceful_restart_locks
)
1209 tm_start(gr_wait_timer
, 0);
1215 * protos_dump_all - dump status of all protocols
1217 * This function dumps status of all existing protocol instances to the
1218 * debug output. It involves printing of general status information
1219 * such as protocol states, its position on the protocol lists
1220 * and also calling of a dump() hook of the protocol to print
1224 protos_dump_all(void)
1226 debug("Protocols:\n");
1229 WALK_LIST(p
, proto_list
)
1231 debug(" protocol %s state %s\n", p
->name
, p_states
[p
->proto_state
]);
1234 WALK_LIST(c
, p
->channels
)
1236 debug("\tTABLE %s\n", c
->table
->name
);
1238 debug("\tInput filter: %s\n", filter_name(c
->in_filter
));
1240 debug("\tOutput filter: %s\n", filter_name(c
->out_filter
));
1243 if (p
->proto
->dump
&& (p
->proto_state
!= PS_DOWN
))
1249 * proto_build - make a single protocol available
1252 * After the platform specific initialization code uses protos_build()
1253 * to add all the standard protocols, it should call proto_build() for
1254 * all platform specific protocols to inform the core that they exist.
1257 proto_build(struct protocol
*p
)
1259 add_tail(&protocol_list
, &p
->n
);
1261 ASSERT(!class_to_protocol
[p
->class]);
1262 class_to_protocol
[p
->class] = p
;
1265 /* FIXME: convert this call to some protocol hook */
1266 extern void bfd_init_all(void);
1269 * protos_build - build a protocol list
1271 * This function is called during BIRD startup to insert
1272 * all standard protocols to the global protocol list. Insertion
1273 * of platform specific protocols (such as the kernel syncer)
1274 * is in the domain of competence of the platform dependent
1280 init_list(&proto_list
);
1281 init_list(&protocol_list
);
1283 proto_build(&proto_device
);
1285 proto_build(&proto_radv
);
1288 proto_build(&proto_rip
);
1290 #ifdef CONFIG_STATIC
1291 proto_build(&proto_static
);
1294 proto_build(&proto_ospf
);
1297 proto_build(&proto_pipe
);
1300 proto_build(&proto_bgp
);
1303 proto_build(&proto_bfd
);
1307 proto_build(&proto_babel
);
1310 proto_build(&proto_rpki
);
1313 proto_pool
= rp_new(&root_pool
, "Protocols");
1314 proto_shutdown_timer
= tm_new(proto_pool
);
1315 proto_shutdown_timer
->hook
= proto_shutdown_loop
;
1319 /* Temporary hack to propagate restart to BGP */
1323 proto_shutdown_loop(timer
*t UNUSED
)
1325 struct proto
*p
, *p_next
;
1327 WALK_LIST_DELSAFE(p
, p_next
, proto_list
)
1330 proto_restart
= (p
->down_sched
== PDS_RESTART
);
1333 proto_rethink_goal(p
);
1337 proto_rethink_goal(p
);
1343 proto_schedule_down(struct proto
*p
, byte restart
, byte code
)
1345 /* Does not work for other states (even PS_START) */
1346 ASSERT(p
->proto_state
== PS_UP
);
1348 /* Scheduled restart may change to shutdown, but not otherwise */
1349 if (p
->down_sched
== PDS_DISABLE
)
1352 p
->down_sched
= restart
? PDS_RESTART
: PDS_DISABLE
;
1353 p
->down_code
= code
;
1354 tm_start_max(proto_shutdown_timer
, restart
? 250 MS
: 0);
1358 * proto_set_message - set administrative message to protocol
1361 * @len: message length (-1 for NULL-terminated string)
1363 * The function sets administrative message (string) related to protocol state
1364 * change. It is called by the nest code for manual enable/disable/restart
1365 * commands all routes to the protocol, and by protocol-specific code when the
1366 * protocol state change is initiated by the protocol. Using NULL message clears
1367 * the last message. The message string may be either NULL-terminated or with an
1371 proto_set_message(struct proto
*p
, char *msg
, int len
)
1373 mb_free(p
->message
);
1385 p
->message
= mb_alloc(proto_pool
, len
+ 1);
1386 memcpy(p
->message
, msg
, len
);
1387 p
->message
[len
] = 0;
1392 channel_limit_name(struct channel_limit
*l
)
1394 const char *actions
[] = {
1395 [PLA_WARN
] = "warn",
1396 [PLA_BLOCK
] = "block",
1397 [PLA_RESTART
] = "restart",
1398 [PLA_DISABLE
] = "disable",
1401 return actions
[l
->action
];
1405 * channel_notify_limit: notify about limit hit and take appropriate action
1407 * @l: limit being hit
1408 * @dir: limit direction (PLD_*)
1409 * @rt_count: the number of routes
1411 * The function is called by the route processing core when limit @l
1412 * is breached. It activates the limit and tooks appropriate action
1413 * according to @l->action.
1416 channel_notify_limit(struct channel
*c
, struct channel_limit
*l
, int dir
, u32 rt_count
)
1418 const char *dir_name
[PLD_MAX
] = { "receive", "import" , "export" };
1419 const byte dir_down
[PLD_MAX
] = { PDC_RX_LIMIT_HIT
, PDC_IN_LIMIT_HIT
, PDC_OUT_LIMIT_HIT
};
1420 struct proto
*p
= c
->proto
;
1422 if (l
->state
== PLS_BLOCKED
)
1425 /* For warning action, we want the log message every time we hit the limit */
1426 if (!l
->state
|| ((l
->action
== PLA_WARN
) && (rt_count
== l
->limit
)))
1427 log(L_WARN
"Protocol %s hits route %s limit (%d), action: %s",
1428 p
->name
, dir_name
[dir
], l
->limit
, channel_limit_name(l
));
1433 l
->state
= PLS_ACTIVE
;
1437 l
->state
= PLS_BLOCKED
;
1442 l
->state
= PLS_BLOCKED
;
1443 if (p
->proto_state
== PS_UP
)
1444 proto_schedule_down(p
, l
->action
== PLA_RESTART
, dir_down
[dir
]);
1450 channel_verify_limits(struct channel
*c
)
1452 struct channel_limit
*l
;
1453 u32 all_routes
= c
->stats
.imp_routes
+ c
->stats
.filt_routes
;
1456 if (l
->action
&& (all_routes
> l
->limit
))
1457 channel_notify_limit(c
, l
, PLD_RX
, all_routes
);
1460 if (l
->action
&& (c
->stats
.imp_routes
> l
->limit
))
1461 channel_notify_limit(c
, l
, PLD_IN
, c
->stats
.imp_routes
);
1464 if (l
->action
&& (c
->stats
.exp_routes
> l
->limit
))
1465 channel_notify_limit(c
, l
, PLD_OUT
, c
->stats
.exp_routes
);
1469 channel_reset_limit(struct channel_limit
*l
)
1472 l
->state
= PLS_INITIAL
;
1476 proto_do_start(struct proto
*p
)
1480 ev_schedule(p
->event
);
1484 proto_do_up(struct proto
*p
)
1486 if (!p
->main_source
)
1488 p
->main_source
= rt_get_source(p
, 0);
1489 rt_lock_source(p
->main_source
);
1492 proto_start_channels(p
);
1496 proto_do_pause(struct proto
*p
)
1498 proto_pause_channels(p
);
1502 proto_do_stop(struct proto
*p
)
1508 ev_schedule(p
->event
);
1512 rt_unlock_source(p
->main_source
);
1513 p
->main_source
= NULL
;
1516 proto_stop_channels(p
);
1520 proto_do_down(struct proto
*p
)
1527 /* Shutdown is finished in the protocol event */
1528 if (proto_is_done(p
))
1529 ev_schedule(p
->event
);
1535 * proto_notify_state - notify core about protocol state change
1536 * @p: protocol the state of which has changed
1537 * @ps: the new status
1539 * Whenever a state of a protocol changes due to some event internal
1540 * to the protocol (i.e., not inside a start() or shutdown() hook),
1541 * it should immediately notify the core about the change by calling
1542 * proto_notify_state() which will write the new state to the &proto
1543 * structure and take all the actions necessary to adapt to the new
1544 * state. State change to PS_DOWN immediately frees resources of protocol
1545 * and might execute start callback of protocol; therefore,
1546 * it should be used at tail positions of protocol callbacks.
1549 proto_notify_state(struct proto
*p
, uint state
)
1551 uint ps
= p
->proto_state
;
1553 DBG("%s reporting state transition %s -> %s\n", p
->name
, p_states
[ps
], p_states
[state
]);
1557 p
->proto_state
= state
;
1558 p
->last_state_change
= current_time();
1563 ASSERT(ps
== PS_DOWN
|| ps
== PS_UP
);
1572 ASSERT(ps
== PS_DOWN
|| ps
== PS_START
);
1581 ASSERT(ps
== PS_START
|| ps
== PS_UP
);
1594 bug("%s: Invalid state %d", p
->name
, ps
);
1597 proto_log_state_change(p
);
1605 proto_state_name(struct proto
*p
)
1607 switch (p
->proto_state
)
1609 case PS_DOWN
: return p
->active
? "flush" : "down";
1610 case PS_START
: return "start";
1611 case PS_UP
: return "up";
1612 case PS_STOP
: return "stop";
1613 default: return "???";
1618 channel_show_stats(struct channel
*c
)
1620 struct proto_stats
*s
= &c
->stats
;
1622 if (c
->in_keep_filtered
)
1623 cli_msg(-1006, " Routes: %u imported, %u filtered, %u exported",
1624 s
->imp_routes
, s
->filt_routes
, s
->exp_routes
);
1626 cli_msg(-1006, " Routes: %u imported, %u exported",
1627 s
->imp_routes
, s
->exp_routes
);
1629 cli_msg(-1006, " Route change stats: received rejected filtered ignored accepted");
1630 cli_msg(-1006, " Import updates: %10u %10u %10u %10u %10u",
1631 s
->imp_updates_received
, s
->imp_updates_invalid
,
1632 s
->imp_updates_filtered
, s
->imp_updates_ignored
,
1633 s
->imp_updates_accepted
);
1634 cli_msg(-1006, " Import withdraws: %10u %10u --- %10u %10u",
1635 s
->imp_withdraws_received
, s
->imp_withdraws_invalid
,
1636 s
->imp_withdraws_ignored
, s
->imp_withdraws_accepted
);
1637 cli_msg(-1006, " Export updates: %10u %10u %10u --- %10u",
1638 s
->exp_updates_received
, s
->exp_updates_rejected
,
1639 s
->exp_updates_filtered
, s
->exp_updates_accepted
);
1640 cli_msg(-1006, " Export withdraws: %10u --- --- --- %10u",
1641 s
->exp_withdraws_received
, s
->exp_withdraws_accepted
);
1645 channel_show_limit(struct channel_limit
*l
, const char *dsc
)
1650 cli_msg(-1006, " %-16s%d%s", dsc
, l
->limit
, l
->state
? " [HIT]" : "");
1651 cli_msg(-1006, " Action: %s", channel_limit_name(l
));
1655 channel_show_info(struct channel
*c
)
1657 cli_msg(-1006, " Channel %s", c
->name
);
1658 cli_msg(-1006, " State: %s", c_states
[c
->channel_state
]);
1659 cli_msg(-1006, " Table: %s", c
->table
->name
);
1660 cli_msg(-1006, " Preference: %d", c
->preference
);
1661 cli_msg(-1006, " Input filter: %s", filter_name(c
->in_filter
));
1662 cli_msg(-1006, " Output filter: %s", filter_name(c
->out_filter
));
1664 if (graceful_restart_state
== GRS_ACTIVE
)
1665 cli_msg(-1006, " GR recovery: %s%s",
1666 c
->gr_lock
? " pending" : "",
1667 c
->gr_wait
? " waiting" : "");
1669 channel_show_limit(&c
->rx_limit
, "Receive limit:");
1670 channel_show_limit(&c
->in_limit
, "Import limit:");
1671 channel_show_limit(&c
->out_limit
, "Export limit:");
1673 if (c
->channel_state
!= CS_DOWN
)
1674 channel_show_stats(c
);
1678 proto_cmd_show(struct proto
*p
, uintptr_t verbose
, int cnt
)
1680 byte buf
[256], tbuf
[TM_DATETIME_BUFFER_SIZE
];
1682 /* First protocol - show header */
1684 cli_msg(-2002, "%-10s %-10s %-10s %-6s %-12s %s",
1685 "Name", "Proto", "Table", "State", "Since", "Info");
1688 if (p
->proto
->get_status
)
1689 p
->proto
->get_status(p
, buf
);
1690 tm_format_time(tbuf
, &config
->tf_proto
, p
->last_state_change
);
1691 cli_msg(-1002, "%-10s %-10s %-10s %-6s %-12s %s",
1694 p
->main_channel
? p
->main_channel
->table
->name
: "---",
1695 proto_state_name(p
),
1702 cli_msg(-1006, " Description: %s", p
->cf
->dsc
);
1704 cli_msg(-1006, " Message: %s", p
->message
);
1705 if (p
->cf
->router_id
)
1706 cli_msg(-1006, " Router ID: %R", p
->cf
->router_id
);
1708 cli_msg(-1006, " VRF: %s", p
->vrf
->name
);
1710 if (p
->proto
->show_proto_info
)
1711 p
->proto
->show_proto_info(p
);
1715 WALK_LIST(c
, p
->channels
)
1716 channel_show_info(c
);
1724 proto_cmd_disable(struct proto
*p
, uintptr_t arg
, int cnt UNUSED
)
1728 cli_msg(-8, "%s: already disabled", p
->name
);
1732 log(L_INFO
"Disabling protocol %s", p
->name
);
1734 p
->down_code
= PDC_CMD_DISABLE
;
1735 proto_set_message(p
, (char *) arg
, -1);
1736 proto_rethink_goal(p
);
1737 cli_msg(-9, "%s: disabled", p
->name
);
1741 proto_cmd_enable(struct proto
*p
, uintptr_t arg
, int cnt UNUSED
)
1745 cli_msg(-10, "%s: already enabled", p
->name
);
1749 log(L_INFO
"Enabling protocol %s", p
->name
);
1751 proto_set_message(p
, (char *) arg
, -1);
1752 proto_rethink_goal(p
);
1753 cli_msg(-11, "%s: enabled", p
->name
);
1757 proto_cmd_restart(struct proto
*p
, uintptr_t arg
, int cnt UNUSED
)
1761 cli_msg(-8, "%s: already disabled", p
->name
);
1765 log(L_INFO
"Restarting protocol %s", p
->name
);
1767 p
->down_code
= PDC_CMD_RESTART
;
1768 proto_set_message(p
, (char *) arg
, -1);
1769 proto_rethink_goal(p
);
1771 proto_rethink_goal(p
);
1772 cli_msg(-12, "%s: restarted", p
->name
);
1776 proto_cmd_reload(struct proto
*p
, uintptr_t dir
, int cnt UNUSED
)
1782 cli_msg(-8, "%s: already disabled", p
->name
);
1786 /* If the protocol in not UP, it has no routes */
1787 if (p
->proto_state
!= PS_UP
)
1790 /* All channels must support reload */
1791 if (dir
!= CMD_RELOAD_OUT
)
1792 WALK_LIST(c
, p
->channels
)
1793 if (!channel_reloadable(c
))
1795 cli_msg(-8006, "%s: reload failed", p
->name
);
1799 log(L_INFO
"Reloading protocol %s", p
->name
);
1801 /* re-importing routes */
1802 if (dir
!= CMD_RELOAD_OUT
)
1803 WALK_LIST(c
, p
->channels
)
1804 channel_request_reload(c
);
1806 /* re-exporting routes */
1807 if (dir
!= CMD_RELOAD_IN
)
1808 WALK_LIST(c
, p
->channels
)
1809 channel_request_feeding(c
);
1811 cli_msg(-15, "%s: reloading", p
->name
);
1815 proto_cmd_debug(struct proto
*p
, uintptr_t mask
, int cnt UNUSED
)
1821 proto_cmd_mrtdump(struct proto
*p
, uintptr_t mask
, int cnt UNUSED
)
1827 proto_apply_cmd_symbol(struct symbol
*s
, void (* cmd
)(struct proto
*, uintptr_t, int), uintptr_t arg
)
1829 if (s
->class != SYM_PROTO
)
1831 cli_msg(9002, "%s is not a protocol", s
->name
);
1835 cmd(((struct proto_config
*)s
->def
)->proto
, arg
, 0);
1840 proto_apply_cmd_patt(char *patt
, void (* cmd
)(struct proto
*, uintptr_t, int), uintptr_t arg
)
1845 WALK_LIST(p
, proto_list
)
1846 if (!patt
|| patmatch(patt
, p
->name
))
1850 cli_msg(8003, "No protocols match");
1856 proto_apply_cmd(struct proto_spec ps
, void (* cmd
)(struct proto
*, uintptr_t, int),
1857 int restricted
, uintptr_t arg
)
1859 if (restricted
&& cli_access_restricted())
1863 proto_apply_cmd_patt(ps
.ptr
, cmd
, arg
);
1865 proto_apply_cmd_symbol(ps
.ptr
, cmd
, arg
);
1869 proto_get_named(struct symbol
*sym
, struct protocol
*pr
)
1871 struct proto
*p
, *q
;
1875 if (sym
->class != SYM_PROTO
)
1876 cf_error("%s: Not a protocol", sym
->name
);
1878 p
= ((struct proto_config
*) sym
->def
)->proto
;
1879 if (!p
|| p
->proto
!= pr
)
1880 cf_error("%s: Not a %s protocol", sym
->name
, pr
->name
);
1885 WALK_LIST(q
, proto_list
)
1886 if ((q
->proto
== pr
) && (q
->proto_state
!= PS_DOWN
))
1889 cf_error("There are multiple %s protocols running", pr
->name
);
1893 cf_error("There is no %s protocol running", pr
->name
);