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 /* Reset export limit if the feed ended with acceptable number of exported routes */
259 struct channel_limit
*l
= &c
->out_limit
;
261 (l
->state
== PLS_BLOCKED
) &&
262 (c
->refeed_count
<= l
->limit
) &&
263 (c
->stats
.exp_routes
<= l
->limit
))
265 log(L_INFO
"Protocol %s resets route export limit (%u)", c
->proto
->name
, l
->limit
);
266 channel_reset_limit(&c
->out_limit
);
268 /* Continue in feed - it will process routing table again from beginning */
270 ev_schedule(c
->feed_event
);
274 // DBG("Feeding protocol %s finished\n", p->name);
275 c
->export_state
= ES_READY
;
276 // proto_log_state_change(p);
278 if (c
->proto
->feed_end
)
279 c
->proto
->feed_end(c
);
284 channel_start_export(struct channel
*c
)
286 ASSERT(c
->channel_state
== CS_UP
);
287 ASSERT(c
->export_state
== ES_DOWN
);
289 channel_schedule_feed(c
, 1); /* Sets ES_FEEDING */
293 channel_stop_export(struct channel
*c
)
295 /* Need to abort feeding */
296 if (c
->export_state
== ES_FEEDING
)
297 rt_feed_channel_abort(c
);
299 c
->export_state
= ES_DOWN
;
300 c
->stats
.exp_routes
= 0;
301 bmap_reset(&c
->export_map
, 1024);
305 /* Called by protocol for reload from in_table */
307 channel_schedule_reload(struct channel
*c
)
309 ASSERT(c
->channel_state
== CS_UP
);
311 rt_reload_channel_abort(c
);
312 ev_schedule(c
->reload_event
);
316 channel_reload_loop(void *ptr
)
318 struct channel
*c
= ptr
;
320 if (!rt_reload_channel(c
))
322 ev_schedule(c
->reload_event
);
328 channel_reset_import(struct channel
*c
)
330 /* Need to abort feeding */
331 ev_postpone(c
->reload_event
);
332 rt_reload_channel_abort(c
);
334 rt_prune_sync(c
->in_table
, 1);
338 channel_reset_export(struct channel
*c
)
340 /* Just free the routes */
341 rt_prune_sync(c
->out_table
, 1);
344 /* Called by protocol to activate in_table */
346 channel_setup_in_table(struct channel
*c
)
348 struct rtable_config
*cf
= mb_allocz(c
->proto
->pool
, sizeof(struct rtable_config
));
350 cf
->addr_type
= c
->net_type
;
352 c
->in_table
= mb_allocz(c
->proto
->pool
, sizeof(struct rtable
));
353 rt_setup(c
->proto
->pool
, c
->in_table
, cf
);
355 c
->reload_event
= ev_new_init(c
->proto
->pool
, channel_reload_loop
, c
);
358 /* Called by protocol to activate out_table */
360 channel_setup_out_table(struct channel
*c
)
362 struct rtable_config
*cf
= mb_allocz(c
->proto
->pool
, sizeof(struct rtable_config
));
364 cf
->addr_type
= c
->net_type
;
366 c
->out_table
= mb_allocz(c
->proto
->pool
, sizeof(struct rtable
));
367 rt_setup(c
->proto
->pool
, c
->out_table
, cf
);
372 channel_do_start(struct channel
*c
)
374 rt_lock_table(c
->table
);
375 add_tail(&c
->table
->channels
, &c
->table_node
);
376 c
->proto
->active_channels
++;
378 c
->feed_event
= ev_new_init(c
->proto
->pool
, channel_feed_loop
, c
);
380 bmap_init(&c
->export_map
, c
->proto
->pool
, 1024);
381 memset(&c
->stats
, 0, sizeof(struct proto_stats
));
383 channel_reset_limit(&c
->rx_limit
);
384 channel_reset_limit(&c
->in_limit
);
385 channel_reset_limit(&c
->out_limit
);
387 CALL(c
->channel
->start
, c
);
391 channel_do_flush(struct channel
*c
)
393 rt_schedule_prune(c
->table
);
397 channel_graceful_restart_unlock(c
);
399 CALL(c
->channel
->shutdown
, c
);
403 channel_do_down(struct channel
*c
)
405 ASSERT(!c
->feed_active
&& !c
->reload_active
);
407 rem_node(&c
->table_node
);
408 rt_unlock_table(c
->table
);
409 c
->proto
->active_channels
--;
411 if ((c
->stats
.imp_routes
+ c
->stats
.filt_routes
) != 0)
412 log(L_ERR
"%s: Channel %s is down but still has some routes", c
->proto
->name
, c
->name
);
414 bmap_free(&c
->export_map
);
415 memset(&c
->stats
, 0, sizeof(struct proto_stats
));
418 c
->reload_event
= NULL
;
421 CALL(c
->channel
->cleanup
, c
);
423 /* Schedule protocol shutddown */
424 if (proto_is_done(c
->proto
))
425 ev_schedule(c
->proto
->event
);
429 channel_set_state(struct channel
*c
, uint state
)
431 uint cs
= c
->channel_state
;
432 uint es
= c
->export_state
;
434 DBG("%s reporting channel %s state transition %s -> %s\n", c
->proto
->name
, c
->name
, c_states
[cs
], c_states
[state
]);
438 c
->channel_state
= state
;
439 c
->last_state_change
= current_time();
444 ASSERT(cs
== CS_DOWN
|| cs
== CS_UP
);
450 channel_stop_export(c
);
452 if (c
->in_table
&& (cs
== CS_UP
))
453 channel_reset_import(c
);
455 if (c
->out_table
&& (cs
== CS_UP
))
456 channel_reset_export(c
);
461 ASSERT(cs
== CS_DOWN
|| cs
== CS_START
);
466 if (!c
->gr_wait
&& c
->proto
->rt_notify
)
467 channel_start_export(c
);
472 ASSERT(cs
== CS_START
|| cs
== CS_UP
);
475 channel_stop_export(c
);
477 if (c
->in_table
&& (cs
== CS_UP
))
478 channel_reset_import(c
);
480 if (c
->out_table
&& (cs
== CS_UP
))
481 channel_reset_export(c
);
487 ASSERT(cs
== CS_FLUSHING
);
495 // XXXX proto_log_state_change(c);
499 * channel_request_feeding - request feeding routes to the channel
502 * Sometimes it is needed to send again all routes to the channel. This is
503 * called feeding and can be requested by this function. This would cause
504 * channel export state transition to ES_FEEDING (during feeding) and when
505 * completed, it will switch back to ES_READY. This function can be called
506 * even when feeding is already running, in that case it is restarted.
509 channel_request_feeding(struct channel
*c
)
511 ASSERT(c
->channel_state
== CS_UP
);
513 /* Do nothing if we are still waiting for feeding */
514 if (c
->export_state
== ES_DOWN
)
517 /* If we are already feeding, we want to restart it */
518 if (c
->export_state
== ES_FEEDING
)
520 /* Unless feeding is in initial state */
524 rt_feed_channel_abort(c
);
527 /* Track number of exported routes during refeed */
530 channel_schedule_feed(c
, 0); /* Sets ES_FEEDING */
531 // proto_log_state_change(c);
535 channel_reloadable(struct channel
*c
)
537 return c
->proto
->reload_routes
&& c
->reloadable
;
541 channel_request_reload(struct channel
*c
)
543 ASSERT(c
->channel_state
== CS_UP
);
544 ASSERT(channel_reloadable(c
));
546 c
->proto
->reload_routes(c
);
549 * Should this be done before reload_routes() hook?
550 * Perhaps, but routes are updated asynchronously.
552 channel_reset_limit(&c
->rx_limit
);
553 channel_reset_limit(&c
->in_limit
);
556 const struct channel_class channel_basic
= {
557 .channel_size
= sizeof(struct channel
),
558 .config_size
= sizeof(struct channel_config
)
562 channel_config_new(const struct channel_class
*cc
, const char *name
, uint net_type
, struct proto_config
*proto
)
564 struct channel_config
*cf
= NULL
;
565 struct rtable_config
*tab
= NULL
;
569 if (!net_val_match(net_type
, proto
->protocol
->channel_mask
))
570 cf_error("Unsupported channel type");
572 if (proto
->net_type
&& (net_type
!= proto
->net_type
))
573 cf_error("Different channel type");
575 tab
= new_config
->def_tables
[net_type
];
581 cf
= cfg_allocz(cc
->config_size
);
586 cf
->out_filter
= FILTER_REJECT
;
588 cf
->net_type
= net_type
;
589 cf
->ra_mode
= RA_OPTIMAL
;
590 cf
->preference
= proto
->protocol
->preference
;
592 add_tail(&proto
->channels
, &cf
->n
);
598 channel_config_get(const struct channel_class
*cc
, const char *name
, uint net_type
, struct proto_config
*proto
)
600 struct channel_config
*cf
;
602 /* We are using name as token, so no strcmp() */
603 WALK_LIST(cf
, proto
->channels
)
604 if (cf
->name
== name
)
606 /* Allow to redefine channel only if inherited from template */
607 if (cf
->parent
== proto
)
608 cf_error("Multiple %s channels", name
);
614 return channel_config_new(cc
, name
, net_type
, proto
);
617 struct channel_config
*
618 channel_copy_config(struct channel_config
*src
, struct proto_config
*proto
)
620 struct channel_config
*dst
= cfg_alloc(src
->channel
->config_size
);
622 memcpy(dst
, src
, src
->channel
->config_size
);
623 add_tail(&proto
->channels
, &dst
->n
);
624 CALL(src
->channel
->copy_config
, dst
, src
);
630 static int reconfigure_type
; /* Hack to propagate type info to channel_reconfigure() */
633 channel_reconfigure(struct channel
*c
, struct channel_config
*cf
)
635 /* FIXME: better handle these changes, also handle in_keep_filtered */
636 if ((c
->table
!= cf
->table
->table
) || (cf
->ra_mode
&& (c
->ra_mode
!= cf
->ra_mode
)))
639 /* Note that filter_same() requires arguments in (new, old) order */
640 int import_changed
= !filter_same(cf
->in_filter
, c
->in_filter
);
641 int export_changed
= !filter_same(cf
->out_filter
, c
->out_filter
);
643 if (c
->preference
!= cf
->preference
)
646 if (c
->merge_limit
!= cf
->merge_limit
)
649 /* Reconfigure channel fields */
650 c
->in_filter
= cf
->in_filter
;
651 c
->out_filter
= cf
->out_filter
;
652 c
->rx_limit
= cf
->rx_limit
;
653 c
->in_limit
= cf
->in_limit
;
654 c
->out_limit
= cf
->out_limit
;
656 // c->ra_mode = cf->ra_mode;
657 c
->merge_limit
= cf
->merge_limit
;
658 c
->preference
= cf
->preference
;
659 c
->in_keep_filtered
= cf
->in_keep_filtered
;
661 channel_verify_limits(c
);
664 c
->last_tx_filter_change
= current_time();
666 /* Execute channel-specific reconfigure hook */
667 if (c
->channel
->reconfigure
&& !c
->channel
->reconfigure(c
, cf
, &import_changed
, &export_changed
))
670 /* If the channel is not open, it has no routes and we cannot reload it anyways */
671 if (c
->channel_state
!= CS_UP
)
674 if (reconfigure_type
== RECONFIG_SOFT
)
677 log(L_INFO
"Channel %s.%s changed import", c
->proto
->name
, c
->name
);
680 log(L_INFO
"Channel %s.%s changed export", c
->proto
->name
, c
->name
);
685 /* Route reload may be not supported */
686 if (import_changed
&& !channel_reloadable(c
))
689 if (import_changed
|| export_changed
)
690 log(L_INFO
"Reloading channel %s.%s", c
->proto
->name
, c
->name
);
693 channel_request_reload(c
);
696 channel_request_feeding(c
);
703 proto_configure_channel(struct proto
*p
, struct channel
**pc
, struct channel_config
*cf
)
705 struct channel
*c
= *pc
;
709 /* We could add the channel, but currently it would just stay in down state
710 until protocol is restarted, so it is better to force restart anyways. */
711 if (p
->proto_state
!= PS_DOWN
)
713 log(L_INFO
"Cannot add channel %s.%s", p
->name
, cf
->name
);
717 *pc
= proto_add_channel(p
, cf
);
721 if (c
->channel_state
!= CS_DOWN
)
723 log(L_INFO
"Cannot remove channel %s.%s", c
->proto
->name
, c
->name
);
727 proto_remove_channel(p
, c
);
732 if (!channel_reconfigure(c
, cf
))
734 log(L_INFO
"Cannot reconfigure channel %s.%s", c
->proto
->name
, c
->name
);
744 proto_event(void *ptr
)
746 struct proto
*p
= ptr
;
756 if (p
->proto
== &proto_unix_iface
)
761 if (proto_is_done(p
))
763 if (p
->proto
->cleanup
)
764 p
->proto
->cleanup(p
);
767 proto_log_state_change(p
);
768 proto_rethink_goal(p
);
774 * proto_new - create a new protocol instance
775 * @c: protocol configuration
777 * When a new configuration has been read in, the core code starts
778 * initializing all the protocol instances configured by calling their
779 * init() hooks with the corresponding instance configuration. The initialization
780 * code of the protocol is expected to create a new instance according to the
781 * configuration by calling this function and then modifying the default settings
782 * to values wanted by the protocol.
785 proto_new(struct proto_config
*cf
)
787 struct proto
*p
= mb_allocz(proto_pool
, cf
->protocol
->proto_size
);
790 p
->debug
= cf
->debug
;
791 p
->mrtdump
= cf
->mrtdump
;
793 p
->proto
= cf
->protocol
;
794 p
->net_type
= cf
->net_type
;
795 p
->disabled
= cf
->disabled
;
796 p
->hash_key
= random_u32();
799 init_list(&p
->channels
);
804 static struct proto
*
805 proto_init(struct proto_config
*c
, node
*n
)
807 struct protocol
*pr
= c
->protocol
;
808 struct proto
*p
= pr
->init(c
);
810 p
->proto_state
= PS_DOWN
;
811 p
->last_state_change
= current_time();
813 p
->vrf_set
= c
->vrf_set
;
814 insert_node(&p
->n
, n
);
816 p
->event
= ev_new_init(proto_pool
, proto_event
, p
);
818 PD(p
, "Initializing%s", p
->disabled
? " [disabled]" : "");
824 proto_start(struct proto
*p
)
826 /* Here we cannot use p->cf->name since it won't survive reconfiguration */
827 p
->pool
= rp_new(proto_pool
, p
->proto
->name
);
829 if (graceful_restart_state
== GRS_INIT
)
835 * proto_config_new - create a new protocol configuration
836 * @pr: protocol the configuration will belong to
837 * @class: SYM_PROTO or SYM_TEMPLATE
839 * Whenever the configuration file says that a new instance
840 * of a routing protocol should be created, the parser calls
841 * proto_config_new() to create a configuration entry for this
842 * instance (a structure staring with the &proto_config header
843 * containing all the generic items followed by protocol-specific
844 * ones). Also, the configuration entry gets added to the list
845 * of protocol instances kept in the configuration.
847 * The function is also used to create protocol templates (when class
848 * SYM_TEMPLATE is specified), the only difference is that templates
849 * are not added to the list of protocol instances and therefore not
850 * initialized during protos_commit()).
853 proto_config_new(struct protocol
*pr
, int class)
855 struct proto_config
*cf
= cfg_allocz(pr
->config_size
);
857 if (class == SYM_PROTO
)
858 add_tail(&new_config
->protos
, &cf
->n
);
860 cf
->global
= new_config
;
864 cf
->debug
= new_config
->proto_default_debug
;
865 cf
->mrtdump
= new_config
->proto_default_mrtdump
;
867 init_list(&cf
->channels
);
874 * proto_copy_config - copy a protocol configuration
875 * @dest: destination protocol configuration
876 * @src: source protocol configuration
878 * Whenever a new instance of a routing protocol is created from the
879 * template, proto_copy_config() is called to copy a content of
880 * the source protocol configuration to the new protocol configuration.
881 * Name, class and a node in protos list of @dest are kept intact.
882 * copy_config() protocol hook is used to copy protocol-specific data.
885 proto_copy_config(struct proto_config
*dest
, struct proto_config
*src
)
887 struct channel_config
*cc
;
892 if (dest
->protocol
!= src
->protocol
)
893 cf_error("Can't copy configuration from a different protocol type");
895 if (dest
->protocol
->copy_config
== NULL
)
896 cf_error("Inheriting configuration for %s is not supported", src
->protocol
->name
);
898 DBG("Copying configuration from %s to %s\n", src
->name
, dest
->name
);
901 * Copy struct proto_config here. Keep original node, class and name.
902 * protocol-specific config copy is handled by protocol copy_config() hook
906 old_class
= dest
->class;
907 old_name
= dest
->name
;
909 memcpy(dest
, src
, src
->protocol
->config_size
);
912 dest
->class = old_class
;
913 dest
->name
= old_name
;
914 init_list(&dest
->channels
);
916 WALK_LIST(cc
, src
->channels
)
917 channel_copy_config(cc
, dest
);
919 /* FIXME: allow for undefined copy_config */
920 dest
->protocol
->copy_config(dest
, src
);
924 proto_clone_config(struct symbol
*sym
, struct proto_config
*parent
)
926 struct proto_config
*cf
= proto_config_new(parent
->protocol
, SYM_PROTO
);
927 proto_copy_config(cf
, parent
);
928 cf
->name
= sym
->name
;
932 sym
->class = cf
->class;
937 proto_undef_clone(struct symbol
*sym
, struct proto_config
*cf
)
941 sym
->class = SYM_VOID
;
946 * protos_preconfig - pre-configuration processing
947 * @c: new configuration
949 * This function calls the preconfig() hooks of all routing
950 * protocols available to prepare them for reading of the new
954 protos_preconfig(struct config
*c
)
958 init_list(&c
->protos
);
959 DBG("Protocol preconfig:");
960 WALK_LIST(p
, protocol_list
)
971 proto_reconfigure(struct proto
*p
, struct proto_config
*oc
, struct proto_config
*nc
, int type
)
973 /* If the protocol is DOWN, we just restart it */
974 if (p
->proto_state
== PS_DOWN
)
977 /* If there is a too big change in core attributes, ... */
978 if ((nc
->protocol
!= oc
->protocol
) ||
979 (nc
->net_type
!= oc
->net_type
) ||
980 (nc
->disabled
!= p
->disabled
) ||
981 (nc
->vrf
!= oc
->vrf
) ||
982 (nc
->vrf_set
!= oc
->vrf_set
))
986 p
->debug
= nc
->debug
;
987 p
->mrtdump
= nc
->mrtdump
;
988 reconfigure_type
= type
;
990 /* Execute protocol specific reconfigure hook */
991 if (!p
->proto
->reconfigure
|| !p
->proto
->reconfigure(p
, nc
))
994 DBG("\t%s: same\n", oc
->name
);
995 PD(p
, "Reconfigured");
1002 * protos_commit - commit new protocol configuration
1003 * @new: new configuration
1004 * @old: old configuration or %NULL if it's boot time config
1005 * @force_reconfig: force restart of all protocols (used for example
1006 * when the router ID changes)
1007 * @type: type of reconfiguration (RECONFIG_SOFT or RECONFIG_HARD)
1009 * Scan differences between @old and @new configuration and adjust all
1010 * protocol instances to conform to the new configuration.
1012 * When a protocol exists in the new configuration, but it doesn't in the
1013 * original one, it's immediately started. When a collision with the other
1014 * running protocol would arise, the new protocol will be temporarily stopped
1015 * by the locking mechanism.
1017 * When a protocol exists in the old configuration, but it doesn't in the
1018 * new one, it's shut down and deleted after the shutdown completes.
1020 * When a protocol exists in both configurations, the core decides
1021 * whether it's possible to reconfigure it dynamically - it checks all
1022 * the core properties of the protocol (changes in filters are ignored
1023 * if type is RECONFIG_SOFT) and if they match, it asks the
1024 * reconfigure() hook of the protocol to see if the protocol is able
1025 * to switch to the new configuration. If it isn't possible, the
1026 * protocol is shut down and a new instance is started with the new
1027 * configuration after the shutdown is completed.
1030 protos_commit(struct config
*new, struct config
*old
, int force_reconfig
, int type
)
1032 struct proto_config
*oc
, *nc
;
1038 DBG("protos_commit:\n");
1041 WALK_LIST(oc
, old
->protos
)
1044 sym
= cf_find_symbol(new, oc
->name
);
1046 /* Handle dynamic protocols */
1047 if (!sym
&& oc
->parent
&& !new->shutdown
)
1049 struct symbol
*parsym
= cf_find_symbol(new, oc
->parent
->name
);
1050 if (parsym
&& parsym
->class == SYM_PROTO
)
1052 /* This is hack, we would like to share config, but we need to copy it now */
1055 conf_this_scope
= new->root_scope
;
1056 sym
= cf_get_symbol(oc
->name
);
1057 proto_clone_config(sym
, parsym
->proto
);
1063 if (sym
&& sym
->class == SYM_PROTO
&& !new->shutdown
)
1065 /* Found match, let's check if we can smoothly switch to new configuration */
1066 /* No need to check description */
1070 /* We will try to reconfigure protocol p */
1071 if (! force_reconfig
&& proto_reconfigure(p
, oc
, nc
, type
))
1076 proto_undef_clone(sym
, nc
);
1080 /* Unsuccessful, we will restart it */
1081 if (!p
->disabled
&& !nc
->disabled
)
1082 log(L_INFO
"Restarting protocol %s", p
->name
);
1083 else if (p
->disabled
&& !nc
->disabled
)
1084 log(L_INFO
"Enabling protocol %s", p
->name
);
1085 else if (!p
->disabled
&& nc
->disabled
)
1086 log(L_INFO
"Disabling protocol %s", p
->name
);
1088 p
->down_code
= nc
->disabled
? PDC_CF_DISABLE
: PDC_CF_RESTART
;
1091 else if (!new->shutdown
)
1094 log(L_INFO
"Removing protocol %s", p
->name
);
1095 p
->down_code
= PDC_CF_REMOVE
;
1098 else if (new->gr_down
)
1100 p
->down_code
= PDC_CMD_GR_DOWN
;
1103 else /* global shutdown */
1105 p
->down_code
= PDC_CMD_SHUTDOWN
;
1109 p
->reconfiguring
= 1;
1110 config_add_obstacle(old
);
1111 proto_rethink_goal(p
);
1115 struct proto
*first_dev_proto
= NULL
;
1117 n
= NODE
&(proto_list
.head
);
1118 WALK_LIST(nc
, new->protos
)
1121 /* Not a first-time configuration */
1123 log(L_INFO
"Adding protocol %s", nc
->name
);
1125 p
= proto_init(nc
, n
);
1128 if (p
->proto
== &proto_unix_iface
)
1129 first_dev_proto
= p
;
1134 DBG("Protocol start\n");
1136 /* Start device protocol first */
1137 if (first_dev_proto
)
1138 proto_rethink_goal(first_dev_proto
);
1140 /* Determine router ID for the first time - it has to be here and not in
1141 global_commit() because it is postponed after start of device protocol */
1142 if (!config
->router_id
)
1144 config
->router_id
= if_choose_router_id(config
->router_id_from
, 0);
1145 if (!config
->router_id
)
1146 die("Cannot determine router ID, please configure it manually");
1149 /* Start all new protocols */
1150 WALK_LIST_DELSAFE(p
, n
, proto_list
)
1151 proto_rethink_goal(p
);
1155 proto_rethink_goal(struct proto
*p
)
1160 if (p
->reconfiguring
&& !p
->active
)
1162 struct proto_config
*nc
= p
->cf_new
;
1163 node
*n
= p
->n
.prev
;
1164 DBG("%s has shut down for reconfiguration\n", p
->name
);
1165 p
->cf
->proto
= NULL
;
1166 config_del_obstacle(p
->cf
->global
);
1167 proto_remove_channels(p
);
1170 mb_free(p
->message
);
1174 p
= proto_init(nc
, n
);
1177 /* Determine what state we want to reach */
1178 if (p
->disabled
|| p
->reconfiguring
)
1189 DBG("Kicking %s up\n", p
->name
);
1192 proto_notify_state(p
, (q
->start
? q
->start(p
) : PS_UP
));
1197 if (p
->proto_state
== PS_START
|| p
->proto_state
== PS_UP
)
1200 DBG("Kicking %s down\n", p
->name
);
1201 PD(p
, "Shutting down");
1202 proto_notify_state(p
, (q
->shutdown
? q
->shutdown(p
) : PS_DOWN
));
1208 proto_spawn(struct proto_config
*cf
, uint disabled
)
1210 struct proto
*p
= proto_init(cf
, TAIL(proto_list
));
1211 p
->disabled
= disabled
;
1212 proto_rethink_goal(p
);
1218 * DOC: Graceful restart recovery
1220 * Graceful restart of a router is a process when the routing plane (e.g. BIRD)
1221 * restarts but both the forwarding plane (e.g kernel routing table) and routing
1222 * neighbors keep proper routes, and therefore uninterrupted packet forwarding
1225 * BIRD implements graceful restart recovery by deferring export of routes to
1226 * protocols until routing tables are refilled with the expected content. After
1227 * start, protocols generate routes as usual, but routes are not propagated to
1228 * them, until protocols report that they generated all routes. After that,
1229 * graceful restart recovery is finished and the export (and the initial feed)
1230 * to protocols is enabled.
1232 * When graceful restart recovery need is detected during initialization, then
1233 * enabled protocols are marked with @gr_recovery flag before start. Such
1234 * protocols then decide how to proceed with graceful restart, participation is
1235 * voluntary. Protocols could lock the recovery for each channel by function
1236 * channel_graceful_restart_lock() (state stored in @gr_lock flag), which means
1237 * that they want to postpone the end of the recovery until they converge and
1238 * then unlock it. They also could set @gr_wait before advancing to %PS_UP,
1239 * which means that the core should defer route export to that channel until
1240 * the end of the recovery. This should be done by protocols that expect their
1241 * neigbors to keep the proper routes (kernel table, BGP sessions with BGP
1242 * graceful restart capability).
1244 * The graceful restart recovery is finished when either all graceful restart
1245 * locks are unlocked or when graceful restart wait timer fires.
1249 static void graceful_restart_done(timer
*t
);
1252 * graceful_restart_recovery - request initial graceful restart recovery
1254 * Called by the platform initialization code if the need for recovery
1255 * after graceful restart is detected during boot. Have to be called
1256 * before protos_commit().
1259 graceful_restart_recovery(void)
1261 graceful_restart_state
= GRS_INIT
;
1265 * graceful_restart_init - initialize graceful restart
1267 * When graceful restart recovery was requested, the function starts an active
1268 * phase of the recovery and initializes graceful restart wait timer. The
1269 * function have to be called after protos_commit().
1272 graceful_restart_init(void)
1274 if (!graceful_restart_state
)
1277 log(L_INFO
"Graceful restart started");
1279 if (!graceful_restart_locks
)
1281 graceful_restart_done(NULL
);
1285 graceful_restart_state
= GRS_ACTIVE
;
1286 gr_wait_timer
= tm_new_init(proto_pool
, graceful_restart_done
, NULL
, 0, 0);
1287 tm_start(gr_wait_timer
, config
->gr_wait S
);
1291 * graceful_restart_done - finalize graceful restart
1294 * When there are no locks on graceful restart, the functions finalizes the
1295 * graceful restart recovery. Protocols postponing route export until the end of
1296 * the recovery are awakened and the export to them is enabled. All other
1297 * related state is cleared. The function is also called when the graceful
1298 * restart wait timer fires (but there are still some locks).
1301 graceful_restart_done(timer
*t UNUSED
)
1303 log(L_INFO
"Graceful restart done");
1304 graceful_restart_state
= GRS_DONE
;
1307 WALK_LIST(p
, proto_list
)
1309 if (!p
->gr_recovery
)
1313 WALK_LIST(c
, p
->channels
)
1315 /* Resume postponed export of routes */
1316 if ((c
->channel_state
== CS_UP
) && c
->gr_wait
&& c
->proto
->rt_notify
)
1317 channel_start_export(c
);
1327 graceful_restart_locks
= 0;
1331 graceful_restart_show_status(void)
1333 if (graceful_restart_state
!= GRS_ACTIVE
)
1336 cli_msg(-24, "Graceful restart recovery in progress");
1337 cli_msg(-24, " Waiting for %d channels to recover", graceful_restart_locks
);
1338 cli_msg(-24, " Wait timer is %t/%u", tm_remains(gr_wait_timer
), config
->gr_wait
);
1342 * channel_graceful_restart_lock - lock graceful restart by channel
1343 * @p: channel instance
1345 * This function allows a protocol to postpone the end of graceful restart
1346 * recovery until it converges. The lock is removed when the protocol calls
1347 * channel_graceful_restart_unlock() or when the channel is closed.
1349 * The function have to be called during the initial phase of graceful restart
1350 * recovery and only for protocols that are part of graceful restart (i.e. their
1351 * @gr_recovery is set), which means it should be called from protocol start
1355 channel_graceful_restart_lock(struct channel
*c
)
1357 ASSERT(graceful_restart_state
== GRS_INIT
);
1358 ASSERT(c
->proto
->gr_recovery
);
1364 graceful_restart_locks
++;
1368 * channel_graceful_restart_unlock - unlock graceful restart by channel
1369 * @p: channel instance
1371 * This function unlocks a lock from channel_graceful_restart_lock(). It is also
1372 * automatically called when the lock holding protocol went down.
1375 channel_graceful_restart_unlock(struct channel
*c
)
1381 graceful_restart_locks
--;
1383 if ((graceful_restart_state
== GRS_ACTIVE
) && !graceful_restart_locks
)
1384 tm_start(gr_wait_timer
, 0);
1390 * protos_dump_all - dump status of all protocols
1392 * This function dumps status of all existing protocol instances to the
1393 * debug output. It involves printing of general status information
1394 * such as protocol states, its position on the protocol lists
1395 * and also calling of a dump() hook of the protocol to print
1399 protos_dump_all(void)
1401 debug("Protocols:\n");
1404 WALK_LIST(p
, proto_list
)
1406 debug(" protocol %s state %s\n", p
->name
, p_states
[p
->proto_state
]);
1409 WALK_LIST(c
, p
->channels
)
1411 debug("\tTABLE %s\n", c
->table
->name
);
1413 debug("\tInput filter: %s\n", filter_name(c
->in_filter
));
1415 debug("\tOutput filter: %s\n", filter_name(c
->out_filter
));
1418 if (p
->proto
->dump
&& (p
->proto_state
!= PS_DOWN
))
1424 * proto_build - make a single protocol available
1427 * After the platform specific initialization code uses protos_build()
1428 * to add all the standard protocols, it should call proto_build() for
1429 * all platform specific protocols to inform the core that they exist.
1432 proto_build(struct protocol
*p
)
1434 add_tail(&protocol_list
, &p
->n
);
1436 ASSERT(!class_to_protocol
[p
->class]);
1437 class_to_protocol
[p
->class] = p
;
1440 /* FIXME: convert this call to some protocol hook */
1441 extern void bfd_init_all(void);
1444 * protos_build - build a protocol list
1446 * This function is called during BIRD startup to insert
1447 * all standard protocols to the global protocol list. Insertion
1448 * of platform specific protocols (such as the kernel syncer)
1449 * is in the domain of competence of the platform dependent
1455 init_list(&proto_list
);
1456 init_list(&protocol_list
);
1458 proto_build(&proto_device
);
1460 proto_build(&proto_radv
);
1463 proto_build(&proto_rip
);
1465 #ifdef CONFIG_STATIC
1466 proto_build(&proto_static
);
1469 proto_build(&proto_mrt
);
1472 proto_build(&proto_ospf
);
1475 proto_build(&proto_pipe
);
1478 proto_build(&proto_bgp
);
1481 proto_build(&proto_bfd
);
1485 proto_build(&proto_babel
);
1488 proto_build(&proto_rpki
);
1491 proto_build(&proto_perf
);
1494 proto_pool
= rp_new(&root_pool
, "Protocols");
1495 proto_shutdown_timer
= tm_new(proto_pool
);
1496 proto_shutdown_timer
->hook
= proto_shutdown_loop
;
1500 /* Temporary hack to propagate restart to BGP */
1504 proto_shutdown_loop(timer
*t UNUSED
)
1506 struct proto
*p
, *p_next
;
1508 WALK_LIST_DELSAFE(p
, p_next
, proto_list
)
1511 proto_restart
= (p
->down_sched
== PDS_RESTART
);
1514 proto_rethink_goal(p
);
1518 proto_rethink_goal(p
);
1524 proto_schedule_down(struct proto
*p
, byte restart
, byte code
)
1526 /* Does not work for other states (even PS_START) */
1527 ASSERT(p
->proto_state
== PS_UP
);
1529 /* Scheduled restart may change to shutdown, but not otherwise */
1530 if (p
->down_sched
== PDS_DISABLE
)
1533 p
->down_sched
= restart
? PDS_RESTART
: PDS_DISABLE
;
1534 p
->down_code
= code
;
1535 tm_start_max(proto_shutdown_timer
, restart
? 250 MS
: 0);
1539 * proto_set_message - set administrative message to protocol
1542 * @len: message length (-1 for NULL-terminated string)
1544 * The function sets administrative message (string) related to protocol state
1545 * change. It is called by the nest code for manual enable/disable/restart
1546 * commands all routes to the protocol, and by protocol-specific code when the
1547 * protocol state change is initiated by the protocol. Using NULL message clears
1548 * the last message. The message string may be either NULL-terminated or with an
1552 proto_set_message(struct proto
*p
, char *msg
, int len
)
1554 mb_free(p
->message
);
1566 p
->message
= mb_alloc(proto_pool
, len
+ 1);
1567 memcpy(p
->message
, msg
, len
);
1568 p
->message
[len
] = 0;
1573 channel_limit_name(struct channel_limit
*l
)
1575 const char *actions
[] = {
1576 [PLA_WARN
] = "warn",
1577 [PLA_BLOCK
] = "block",
1578 [PLA_RESTART
] = "restart",
1579 [PLA_DISABLE
] = "disable",
1582 return actions
[l
->action
];
1586 * channel_notify_limit: notify about limit hit and take appropriate action
1588 * @l: limit being hit
1589 * @dir: limit direction (PLD_*)
1590 * @rt_count: the number of routes
1592 * The function is called by the route processing core when limit @l
1593 * is breached. It activates the limit and tooks appropriate action
1594 * according to @l->action.
1597 channel_notify_limit(struct channel
*c
, struct channel_limit
*l
, int dir
, u32 rt_count
)
1599 const char *dir_name
[PLD_MAX
] = { "receive", "import" , "export" };
1600 const byte dir_down
[PLD_MAX
] = { PDC_RX_LIMIT_HIT
, PDC_IN_LIMIT_HIT
, PDC_OUT_LIMIT_HIT
};
1601 struct proto
*p
= c
->proto
;
1603 if (l
->state
== PLS_BLOCKED
)
1606 /* For warning action, we want the log message every time we hit the limit */
1607 if (!l
->state
|| ((l
->action
== PLA_WARN
) && (rt_count
== l
->limit
)))
1608 log(L_WARN
"Protocol %s hits route %s limit (%d), action: %s",
1609 p
->name
, dir_name
[dir
], l
->limit
, channel_limit_name(l
));
1614 l
->state
= PLS_ACTIVE
;
1618 l
->state
= PLS_BLOCKED
;
1623 l
->state
= PLS_BLOCKED
;
1624 if (p
->proto_state
== PS_UP
)
1625 proto_schedule_down(p
, l
->action
== PLA_RESTART
, dir_down
[dir
]);
1631 channel_verify_limits(struct channel
*c
)
1633 struct channel_limit
*l
;
1634 u32 all_routes
= c
->stats
.imp_routes
+ c
->stats
.filt_routes
;
1637 if (l
->action
&& (all_routes
> l
->limit
))
1638 channel_notify_limit(c
, l
, PLD_RX
, all_routes
);
1641 if (l
->action
&& (c
->stats
.imp_routes
> l
->limit
))
1642 channel_notify_limit(c
, l
, PLD_IN
, c
->stats
.imp_routes
);
1645 if (l
->action
&& (c
->stats
.exp_routes
> l
->limit
))
1646 channel_notify_limit(c
, l
, PLD_OUT
, c
->stats
.exp_routes
);
1650 channel_reset_limit(struct channel_limit
*l
)
1653 l
->state
= PLS_INITIAL
;
1657 proto_do_start(struct proto
*p
)
1661 ev_schedule(p
->event
);
1665 proto_do_up(struct proto
*p
)
1667 if (!p
->main_source
)
1669 p
->main_source
= rt_get_source(p
, 0);
1670 rt_lock_source(p
->main_source
);
1673 proto_start_channels(p
);
1677 proto_do_pause(struct proto
*p
)
1679 proto_pause_channels(p
);
1683 proto_do_stop(struct proto
*p
)
1689 ev_schedule(p
->event
);
1693 rt_unlock_source(p
->main_source
);
1694 p
->main_source
= NULL
;
1697 proto_stop_channels(p
);
1701 proto_do_down(struct proto
*p
)
1708 /* Shutdown is finished in the protocol event */
1709 if (proto_is_done(p
))
1710 ev_schedule(p
->event
);
1716 * proto_notify_state - notify core about protocol state change
1717 * @p: protocol the state of which has changed
1718 * @ps: the new status
1720 * Whenever a state of a protocol changes due to some event internal
1721 * to the protocol (i.e., not inside a start() or shutdown() hook),
1722 * it should immediately notify the core about the change by calling
1723 * proto_notify_state() which will write the new state to the &proto
1724 * structure and take all the actions necessary to adapt to the new
1725 * state. State change to PS_DOWN immediately frees resources of protocol
1726 * and might execute start callback of protocol; therefore,
1727 * it should be used at tail positions of protocol callbacks.
1730 proto_notify_state(struct proto
*p
, uint state
)
1732 uint ps
= p
->proto_state
;
1734 DBG("%s reporting state transition %s -> %s\n", p
->name
, p_states
[ps
], p_states
[state
]);
1738 p
->proto_state
= state
;
1739 p
->last_state_change
= current_time();
1744 ASSERT(ps
== PS_DOWN
|| ps
== PS_UP
);
1753 ASSERT(ps
== PS_DOWN
|| ps
== PS_START
);
1762 ASSERT(ps
== PS_START
|| ps
== PS_UP
);
1775 bug("%s: Invalid state %d", p
->name
, ps
);
1778 proto_log_state_change(p
);
1786 proto_state_name(struct proto
*p
)
1788 switch (p
->proto_state
)
1790 case PS_DOWN
: return p
->active
? "flush" : "down";
1791 case PS_START
: return "start";
1792 case PS_UP
: return "up";
1793 case PS_STOP
: return "stop";
1794 default: return "???";
1799 channel_show_stats(struct channel
*c
)
1801 struct proto_stats
*s
= &c
->stats
;
1803 if (c
->in_keep_filtered
)
1804 cli_msg(-1006, " Routes: %u imported, %u filtered, %u exported, %u preferred",
1805 s
->imp_routes
, s
->filt_routes
, s
->exp_routes
, s
->pref_routes
);
1807 cli_msg(-1006, " Routes: %u imported, %u exported, %u preferred",
1808 s
->imp_routes
, s
->exp_routes
, s
->pref_routes
);
1810 cli_msg(-1006, " Route change stats: received rejected filtered ignored accepted");
1811 cli_msg(-1006, " Import updates: %10u %10u %10u %10u %10u",
1812 s
->imp_updates_received
, s
->imp_updates_invalid
,
1813 s
->imp_updates_filtered
, s
->imp_updates_ignored
,
1814 s
->imp_updates_accepted
);
1815 cli_msg(-1006, " Import withdraws: %10u %10u --- %10u %10u",
1816 s
->imp_withdraws_received
, s
->imp_withdraws_invalid
,
1817 s
->imp_withdraws_ignored
, s
->imp_withdraws_accepted
);
1818 cli_msg(-1006, " Export updates: %10u %10u %10u --- %10u",
1819 s
->exp_updates_received
, s
->exp_updates_rejected
,
1820 s
->exp_updates_filtered
, s
->exp_updates_accepted
);
1821 cli_msg(-1006, " Export withdraws: %10u --- --- --- %10u",
1822 s
->exp_withdraws_received
, s
->exp_withdraws_accepted
);
1826 channel_show_limit(struct channel_limit
*l
, const char *dsc
)
1831 cli_msg(-1006, " %-16s%d%s", dsc
, l
->limit
, l
->state
? " [HIT]" : "");
1832 cli_msg(-1006, " Action: %s", channel_limit_name(l
));
1836 channel_show_info(struct channel
*c
)
1838 cli_msg(-1006, " Channel %s", c
->name
);
1839 cli_msg(-1006, " State: %s", c_states
[c
->channel_state
]);
1840 cli_msg(-1006, " Table: %s", c
->table
->name
);
1841 cli_msg(-1006, " Preference: %d", c
->preference
);
1842 cli_msg(-1006, " Input filter: %s", filter_name(c
->in_filter
));
1843 cli_msg(-1006, " Output filter: %s", filter_name(c
->out_filter
));
1845 if (graceful_restart_state
== GRS_ACTIVE
)
1846 cli_msg(-1006, " GR recovery: %s%s",
1847 c
->gr_lock
? " pending" : "",
1848 c
->gr_wait
? " waiting" : "");
1850 channel_show_limit(&c
->rx_limit
, "Receive limit:");
1851 channel_show_limit(&c
->in_limit
, "Import limit:");
1852 channel_show_limit(&c
->out_limit
, "Export limit:");
1854 if (c
->channel_state
!= CS_DOWN
)
1855 channel_show_stats(c
);
1859 proto_cmd_show(struct proto
*p
, uintptr_t verbose
, int cnt
)
1861 byte buf
[256], tbuf
[TM_DATETIME_BUFFER_SIZE
];
1863 /* First protocol - show header */
1865 cli_msg(-2002, "%-10s %-10s %-10s %-6s %-12s %s",
1866 "Name", "Proto", "Table", "State", "Since", "Info");
1869 if (p
->proto
->get_status
)
1870 p
->proto
->get_status(p
, buf
);
1871 tm_format_time(tbuf
, &config
->tf_proto
, p
->last_state_change
);
1872 cli_msg(-1002, "%-10s %-10s %-10s %-6s %-12s %s",
1875 p
->main_channel
? p
->main_channel
->table
->name
: "---",
1876 proto_state_name(p
),
1883 cli_msg(-1006, " Description: %s", p
->cf
->dsc
);
1885 cli_msg(-1006, " Message: %s", p
->message
);
1886 if (p
->cf
->router_id
)
1887 cli_msg(-1006, " Router ID: %R", p
->cf
->router_id
);
1889 cli_msg(-1006, " VRF: %s", p
->vrf
? p
->vrf
->name
: "default");
1891 if (p
->proto
->show_proto_info
)
1892 p
->proto
->show_proto_info(p
);
1896 WALK_LIST(c
, p
->channels
)
1897 channel_show_info(c
);
1905 proto_cmd_disable(struct proto
*p
, uintptr_t arg
, int cnt UNUSED
)
1909 cli_msg(-8, "%s: already disabled", p
->name
);
1913 log(L_INFO
"Disabling protocol %s", p
->name
);
1915 p
->down_code
= PDC_CMD_DISABLE
;
1916 proto_set_message(p
, (char *) arg
, -1);
1917 proto_rethink_goal(p
);
1918 cli_msg(-9, "%s: disabled", p
->name
);
1922 proto_cmd_enable(struct proto
*p
, uintptr_t arg
, int cnt UNUSED
)
1926 cli_msg(-10, "%s: already enabled", p
->name
);
1930 log(L_INFO
"Enabling protocol %s", p
->name
);
1932 proto_set_message(p
, (char *) arg
, -1);
1933 proto_rethink_goal(p
);
1934 cli_msg(-11, "%s: enabled", p
->name
);
1938 proto_cmd_restart(struct proto
*p
, uintptr_t arg
, int cnt UNUSED
)
1942 cli_msg(-8, "%s: already disabled", p
->name
);
1946 log(L_INFO
"Restarting protocol %s", p
->name
);
1948 p
->down_code
= PDC_CMD_RESTART
;
1949 proto_set_message(p
, (char *) arg
, -1);
1950 proto_rethink_goal(p
);
1952 proto_rethink_goal(p
);
1953 cli_msg(-12, "%s: restarted", p
->name
);
1957 proto_cmd_reload(struct proto
*p
, uintptr_t dir
, int cnt UNUSED
)
1963 cli_msg(-8, "%s: already disabled", p
->name
);
1967 /* If the protocol in not UP, it has no routes */
1968 if (p
->proto_state
!= PS_UP
)
1971 /* All channels must support reload */
1972 if (dir
!= CMD_RELOAD_OUT
)
1973 WALK_LIST(c
, p
->channels
)
1974 if ((c
->channel_state
== CS_UP
) && !channel_reloadable(c
))
1976 cli_msg(-8006, "%s: reload failed", p
->name
);
1980 log(L_INFO
"Reloading protocol %s", p
->name
);
1982 /* re-importing routes */
1983 if (dir
!= CMD_RELOAD_OUT
)
1984 WALK_LIST(c
, p
->channels
)
1985 if (c
->channel_state
== CS_UP
)
1986 channel_request_reload(c
);
1988 /* re-exporting routes */
1989 if (dir
!= CMD_RELOAD_IN
)
1990 WALK_LIST(c
, p
->channels
)
1991 if (c
->channel_state
== CS_UP
)
1992 channel_request_feeding(c
);
1994 cli_msg(-15, "%s: reloading", p
->name
);
1998 proto_cmd_debug(struct proto
*p
, uintptr_t mask
, int cnt UNUSED
)
2004 proto_cmd_mrtdump(struct proto
*p
, uintptr_t mask
, int cnt UNUSED
)
2010 proto_apply_cmd_symbol(struct symbol
*s
, void (* cmd
)(struct proto
*, uintptr_t, int), uintptr_t arg
)
2012 if (s
->class != SYM_PROTO
)
2014 cli_msg(9002, "%s is not a protocol", s
->name
);
2018 cmd(s
->proto
->proto
, arg
, 0);
2023 proto_apply_cmd_patt(char *patt
, void (* cmd
)(struct proto
*, uintptr_t, int), uintptr_t arg
)
2028 WALK_LIST(p
, proto_list
)
2029 if (!patt
|| patmatch(patt
, p
->name
))
2033 cli_msg(8003, "No protocols match");
2039 proto_apply_cmd(struct proto_spec ps
, void (* cmd
)(struct proto
*, uintptr_t, int),
2040 int restricted
, uintptr_t arg
)
2042 if (restricted
&& cli_access_restricted())
2046 proto_apply_cmd_patt(ps
.ptr
, cmd
, arg
);
2048 proto_apply_cmd_symbol(ps
.ptr
, cmd
, arg
);
2052 proto_get_named(struct symbol
*sym
, struct protocol
*pr
)
2054 struct proto
*p
, *q
;
2058 if (sym
->class != SYM_PROTO
)
2059 cf_error("%s: Not a protocol", sym
->name
);
2061 p
= sym
->proto
->proto
;
2062 if (!p
|| p
->proto
!= pr
)
2063 cf_error("%s: Not a %s protocol", sym
->name
, pr
->name
);
2068 WALK_LIST(q
, proto_list
)
2069 if ((q
->proto
== pr
) && (q
->proto_state
!= PS_DOWN
))
2072 cf_error("There are multiple %s protocols running", pr
->name
);
2076 cf_error("There is no %s protocol running", pr
->name
);