]> git.ipfire.org Git - thirdparty/bird.git/blob - nest/proto.c
BGP: Prefix hash is too small, increase its max size.
[thirdparty/bird.git] / nest / proto.c
1 /*
2 * BIRD -- Protocols
3 *
4 * (c) 1998--2000 Martin Mares <mj@ucw.cz>
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL.
7 */
8
9 #undef LOCAL_DEBUG
10
11 #include "nest/bird.h"
12 #include "nest/protocol.h"
13 #include "lib/resource.h"
14 #include "lib/lists.h"
15 #include "lib/event.h"
16 #include "lib/timer.h"
17 #include "lib/string.h"
18 #include "conf/conf.h"
19 #include "nest/route.h"
20 #include "nest/iface.h"
21 #include "nest/cli.h"
22 #include "filter/filter.h"
23
24 pool *proto_pool;
25 list proto_list;
26
27 static list protocol_list;
28 struct protocol *class_to_protocol[PROTOCOL__MAX];
29
30 #define PD(pr, msg, args...) do { if (pr->debug & D_STATES) { log(L_TRACE "%s: " msg, pr->name , ## args); } } while(0)
31
32 static timer *proto_shutdown_timer;
33 static timer *gr_wait_timer;
34
35 #define GRS_NONE 0
36 #define GRS_INIT 1
37 #define GRS_ACTIVE 2
38 #define GRS_DONE 3
39
40 static int graceful_restart_state;
41 static u32 graceful_restart_locks;
42
43 static char *p_states[] = { "DOWN", "START", "UP", "STOP" };
44 static char *c_states[] = { "DOWN", "START", "UP", "FLUSHING" };
45
46 extern struct protocol proto_unix_iface;
47
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);
53
54
55 static inline int proto_is_done(struct proto *p)
56 { return (p->proto_state == PS_DOWN) && (p->active_channels == 0); }
57
58 static inline int channel_is_active(struct channel *c)
59 { return (c->channel_state == CS_START) || (c->channel_state == CS_UP); }
60
61 static void
62 proto_log_state_change(struct proto *p)
63 {
64 if (p->debug & D_STATES)
65 {
66 char *name = proto_state_name(p);
67 if (name != p->last_state_name_announced)
68 {
69 p->last_state_name_announced = name;
70 PD(p, "State changed to %s", proto_state_name(p));
71 }
72 }
73 else
74 p->last_state_name_announced = NULL;
75 }
76
77
78 struct channel_config *
79 proto_cf_find_channel(struct proto_config *pc, uint net_type)
80 {
81 struct channel_config *cc;
82
83 WALK_LIST(cc, pc->channels)
84 if (cc->net_type == net_type)
85 return cc;
86
87 return NULL;
88 }
89
90 /**
91 * proto_find_channel_by_table - find channel connected to a routing table
92 * @p: protocol instance
93 * @t: routing table
94 *
95 * Returns pointer to channel or NULL
96 */
97 struct channel *
98 proto_find_channel_by_table(struct proto *p, struct rtable *t)
99 {
100 struct channel *c;
101
102 WALK_LIST(c, p->channels)
103 if (c->table == t)
104 return c;
105
106 return NULL;
107 }
108
109 /**
110 * proto_find_channel_by_name - find channel by its name
111 * @p: protocol instance
112 * @n: channel name
113 *
114 * Returns pointer to channel or NULL
115 */
116 struct channel *
117 proto_find_channel_by_name(struct proto *p, const char *n)
118 {
119 struct channel *c;
120
121 WALK_LIST(c, p->channels)
122 if (!strcmp(c->name, n))
123 return c;
124
125 return NULL;
126 }
127
128 /**
129 * proto_add_channel - connect protocol to a routing table
130 * @p: protocol instance
131 * @cf: channel configuration
132 *
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
136 * table.
137 *
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.
141 */
142
143 struct channel *
144 proto_add_channel(struct proto *p, struct channel_config *cf)
145 {
146 struct channel *c = mb_allocz(proto_pool, cf->channel->channel_size);
147
148 c->name = cf->name;
149 c->channel = cf->channel;
150 c->proto = p;
151 c->table = cf->table->table;
152
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;
158
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;
164
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();
169 c->reloadable = 1;
170
171 CALL(c->channel->init, c, cf);
172
173 add_tail(&p->channels, &c->n);
174
175 PD(p, "Channel %s connected to table %s", c->name, c->table->name);
176
177 return c;
178 }
179
180 void
181 proto_remove_channel(struct proto *p, struct channel *c)
182 {
183 ASSERT(c->channel_state == CS_DOWN);
184
185 PD(p, "Channel %s removed", c->name);
186
187 rem_node(&c->n);
188 mb_free(c);
189 }
190
191
192 static void
193 proto_start_channels(struct proto *p)
194 {
195 struct channel *c;
196 WALK_LIST(c, p->channels)
197 if (!c->disabled)
198 channel_set_state(c, CS_UP);
199 }
200
201 static void
202 proto_pause_channels(struct proto *p)
203 {
204 struct channel *c;
205 WALK_LIST(c, p->channels)
206 if (!c->disabled && channel_is_active(c))
207 channel_set_state(c, CS_START);
208 }
209
210 static void
211 proto_stop_channels(struct proto *p)
212 {
213 struct channel *c;
214 WALK_LIST(c, p->channels)
215 if (!c->disabled && channel_is_active(c))
216 channel_set_state(c, CS_FLUSHING);
217 }
218
219 static void
220 proto_remove_channels(struct proto *p)
221 {
222 struct channel *c;
223 WALK_LIST_FIRST(c, p->channels)
224 proto_remove_channel(p, c);
225 }
226
227 static void
228 channel_schedule_feed(struct channel *c, int initial)
229 {
230 // DBG("%s: Scheduling meal\n", p->name);
231 ASSERT(c->channel_state == CS_UP);
232
233 c->export_state = ES_FEEDING;
234 c->refeeding = !initial;
235
236 ev_schedule(c->feed_event);
237 }
238
239 static void
240 channel_feed_loop(void *ptr)
241 {
242 struct channel *c = ptr;
243
244 if (c->export_state != ES_FEEDING)
245 return;
246
247 if (!c->feed_active)
248 if (c->proto->feed_begin)
249 c->proto->feed_begin(c, !c->refeeding);
250
251 // DBG("Feeding protocol %s continued\n", p->name);
252 if (!rt_feed_channel(c))
253 {
254 ev_schedule(c->feed_event);
255 return;
256 }
257
258 // DBG("Feeding protocol %s finished\n", p->name);
259 c->export_state = ES_READY;
260 // proto_log_state_change(p);
261
262 if (c->proto->feed_end)
263 c->proto->feed_end(c);
264 }
265
266
267 static void
268 channel_start_export(struct channel *c)
269 {
270 ASSERT(c->channel_state == CS_UP);
271 ASSERT(c->export_state == ES_DOWN);
272
273 channel_schedule_feed(c, 1); /* Sets ES_FEEDING */
274 }
275
276 static void
277 channel_stop_export(struct channel *c)
278 {
279 /* Need to abort feeding */
280 if (c->export_state == ES_FEEDING)
281 rt_feed_channel_abort(c);
282
283 c->export_state = ES_DOWN;
284 c->stats.exp_routes = 0;
285 }
286
287
288 /* Called by protocol for reload from in_table */
289 void
290 channel_schedule_reload(struct channel *c)
291 {
292 ASSERT(c->channel_state == CS_UP);
293
294 rt_reload_channel_abort(c);
295 ev_schedule(c->reload_event);
296 }
297
298 static void
299 channel_reload_loop(void *ptr)
300 {
301 struct channel *c = ptr;
302
303 if (!rt_reload_channel(c))
304 {
305 ev_schedule(c->reload_event);
306 return;
307 }
308 }
309
310 static void
311 channel_reset_import(struct channel *c)
312 {
313 /* Need to abort feeding */
314 ev_postpone(c->reload_event);
315 rt_reload_channel_abort(c);
316
317 rt_prune_sync(c->in_table, 1);
318 }
319
320 /* Called by protocol to activate in_table */
321 void
322 channel_setup_in_table(struct channel *c)
323 {
324 struct rtable_config *cf = mb_allocz(c->proto->pool, sizeof(struct rtable_config));
325 cf->name = "import";
326 cf->addr_type = c->net_type;
327
328 c->in_table = mb_allocz(c->proto->pool, sizeof(struct rtable));
329 rt_setup(c->proto->pool, c->in_table, cf);
330
331 c->reload_event = ev_new_init(c->proto->pool, channel_reload_loop, c);
332 }
333
334
335 static void
336 channel_do_start(struct channel *c)
337 {
338 rt_lock_table(c->table);
339 add_tail(&c->table->channels, &c->table_node);
340 c->proto->active_channels++;
341
342 c->feed_event = ev_new_init(c->proto->pool, channel_feed_loop, c);
343
344 channel_reset_limit(&c->rx_limit);
345 channel_reset_limit(&c->in_limit);
346 channel_reset_limit(&c->out_limit);
347
348 CALL(c->channel->start, c);
349 }
350
351 static void
352 channel_do_flush(struct channel *c)
353 {
354 rt_schedule_prune(c->table);
355
356 c->gr_wait = 0;
357 if (c->gr_lock)
358 channel_graceful_restart_unlock(c);
359
360 CALL(c->channel->shutdown, c);
361 }
362
363 static void
364 channel_do_down(struct channel *c)
365 {
366 ASSERT(!c->feed_active && !c->reload_active);
367
368 rem_node(&c->table_node);
369 rt_unlock_table(c->table);
370 c->proto->active_channels--;
371
372 if ((c->stats.imp_routes + c->stats.filt_routes) != 0)
373 log(L_ERR "%s: Channel %s is down but still has some routes", c->proto->name, c->name);
374
375 memset(&c->stats, 0, sizeof(struct proto_stats));
376
377 c->in_table = NULL;
378 c->reload_event = NULL;
379
380 CALL(c->channel->cleanup, c);
381
382 /* Schedule protocol shutddown */
383 if (proto_is_done(c->proto))
384 ev_schedule(c->proto->event);
385 }
386
387 void
388 channel_set_state(struct channel *c, uint state)
389 {
390 uint cs = c->channel_state;
391 uint es = c->export_state;
392
393 DBG("%s reporting channel %s state transition %s -> %s\n", c->proto->name, c->name, c_states[cs], c_states[state]);
394 if (state == cs)
395 return;
396
397 c->channel_state = state;
398 c->last_state_change = current_time();
399
400 switch (state)
401 {
402 case CS_START:
403 ASSERT(cs == CS_DOWN || cs == CS_UP);
404
405 if (cs == CS_DOWN)
406 channel_do_start(c);
407
408 if (es != ES_DOWN)
409 channel_stop_export(c);
410
411 if (c->in_table && (cs == CS_UP))
412 channel_reset_import(c);
413
414 break;
415
416 case CS_UP:
417 ASSERT(cs == CS_DOWN || cs == CS_START);
418
419 if (cs == CS_DOWN)
420 channel_do_start(c);
421
422 if (!c->gr_wait && c->proto->rt_notify)
423 channel_start_export(c);
424
425 break;
426
427 case CS_FLUSHING:
428 ASSERT(cs == CS_START || cs == CS_UP);
429
430 if (es != ES_DOWN)
431 channel_stop_export(c);
432
433 if (c->in_table && (cs == CS_UP))
434 channel_reset_import(c);
435
436 channel_do_flush(c);
437 break;
438
439 case CS_DOWN:
440 ASSERT(cs == CS_FLUSHING);
441
442 channel_do_down(c);
443 break;
444
445 default:
446 ASSERT(0);
447 }
448 // XXXX proto_log_state_change(c);
449 }
450
451 /**
452 * channel_request_feeding - request feeding routes to the channel
453 * @c: given channel
454 *
455 * Sometimes it is needed to send again all routes to the channel. This is
456 * called feeding and can be requested by this function. This would cause
457 * channel export state transition to ES_FEEDING (during feeding) and when
458 * completed, it will switch back to ES_READY. This function can be called
459 * even when feeding is already running, in that case it is restarted.
460 */
461 void
462 channel_request_feeding(struct channel *c)
463 {
464 ASSERT(c->channel_state == CS_UP);
465
466 /* Do nothing if we are still waiting for feeding */
467 if (c->export_state == ES_DOWN)
468 return;
469
470 /* If we are already feeding, we want to restart it */
471 if (c->export_state == ES_FEEDING)
472 {
473 /* Unless feeding is in initial state */
474 if (!c->feed_active)
475 return;
476
477 rt_feed_channel_abort(c);
478 }
479
480 channel_reset_limit(&c->out_limit);
481
482 /* Hack: reset exp_routes during refeed, and do not decrease it later */
483 c->stats.exp_routes = 0;
484
485 channel_schedule_feed(c, 0); /* Sets ES_FEEDING */
486 // proto_log_state_change(c);
487 }
488
489 static inline int
490 channel_reloadable(struct channel *c)
491 {
492 return c->proto->reload_routes && c->reloadable;
493 }
494
495 static void
496 channel_request_reload(struct channel *c)
497 {
498 ASSERT(c->channel_state == CS_UP);
499 ASSERT(channel_reloadable(c));
500
501 c->proto->reload_routes(c);
502
503 /*
504 * Should this be done before reload_routes() hook?
505 * Perhaps, but routes are updated asynchronously.
506 */
507 channel_reset_limit(&c->rx_limit);
508 channel_reset_limit(&c->in_limit);
509 }
510
511 const struct channel_class channel_basic = {
512 .channel_size = sizeof(struct channel),
513 .config_size = sizeof(struct channel_config)
514 };
515
516 void *
517 channel_config_new(const struct channel_class *cc, const char *name, uint net_type, struct proto_config *proto)
518 {
519 struct channel_config *cf = NULL;
520 struct rtable_config *tab = NULL;
521
522 if (net_type)
523 {
524 if (!net_val_match(net_type, proto->protocol->channel_mask))
525 cf_error("Unsupported channel type");
526
527 if (proto->net_type && (net_type != proto->net_type))
528 cf_error("Different channel type");
529
530 tab = new_config->def_tables[net_type];
531 }
532
533 if (!cc)
534 cc = &channel_basic;
535
536 cf = cfg_allocz(cc->config_size);
537 cf->name = name;
538 cf->channel = cc;
539 cf->parent = proto;
540 cf->table = tab;
541 cf->out_filter = FILTER_REJECT;
542
543 cf->net_type = net_type;
544 cf->ra_mode = RA_OPTIMAL;
545 cf->preference = proto->protocol->preference;
546
547 add_tail(&proto->channels, &cf->n);
548
549 return cf;
550 }
551
552 void *
553 channel_config_get(const struct channel_class *cc, const char *name, uint net_type, struct proto_config *proto)
554 {
555 struct channel_config *cf;
556
557 /* We are using name as token, so no strcmp() */
558 WALK_LIST(cf, proto->channels)
559 if (cf->name == name)
560 {
561 /* Allow to redefine channel only if inherited from template */
562 if (cf->parent == proto)
563 cf_error("Multiple %s channels", name);
564
565 cf->parent = proto;
566 return cf;
567 }
568
569 return channel_config_new(cc, name, net_type, proto);
570 }
571
572 struct channel_config *
573 channel_copy_config(struct channel_config *src, struct proto_config *proto)
574 {
575 struct channel_config *dst = cfg_alloc(src->channel->config_size);
576
577 memcpy(dst, src, src->channel->config_size);
578 add_tail(&proto->channels, &dst->n);
579 CALL(src->channel->copy_config, dst, src);
580
581 return dst;
582 }
583
584
585 static int reconfigure_type; /* Hack to propagate type info to channel_reconfigure() */
586
587 int
588 channel_reconfigure(struct channel *c, struct channel_config *cf)
589 {
590 /* FIXME: better handle these changes, also handle in_keep_filtered */
591 if ((c->table != cf->table->table) || (cf->ra_mode && (c->ra_mode != cf->ra_mode)))
592 return 0;
593
594 /* Note that filter_same() requires arguments in (new, old) order */
595 int import_changed = !filter_same(cf->in_filter, c->in_filter);
596 int export_changed = !filter_same(cf->out_filter, c->out_filter);
597
598 if (c->preference != cf->preference)
599 import_changed = 1;
600
601 if (c->merge_limit != cf->merge_limit)
602 export_changed = 1;
603
604 /* Reconfigure channel fields */
605 c->in_filter = cf->in_filter;
606 c->out_filter = cf->out_filter;
607 c->rx_limit = cf->rx_limit;
608 c->in_limit = cf->in_limit;
609 c->out_limit = cf->out_limit;
610
611 // c->ra_mode = cf->ra_mode;
612 c->merge_limit = cf->merge_limit;
613 c->preference = cf->preference;
614 c->in_keep_filtered = cf->in_keep_filtered;
615
616 channel_verify_limits(c);
617
618 if (export_changed)
619 c->last_tx_filter_change = current_time();
620
621 /* Execute channel-specific reconfigure hook */
622 if (c->channel->reconfigure && !c->channel->reconfigure(c, cf))
623 return 0;
624
625 /* If the channel is not open, it has no routes and we cannot reload it anyways */
626 if (c->channel_state != CS_UP)
627 return 1;
628
629 if (reconfigure_type == RECONFIG_SOFT)
630 {
631 if (import_changed)
632 log(L_INFO "Channel %s.%s changed import", c->proto->name, c->name);
633
634 if (export_changed)
635 log(L_INFO "Channel %s.%s changed export", c->proto->name, c->name);
636
637 return 1;
638 }
639
640 /* Route reload may be not supported */
641 if (import_changed && !channel_reloadable(c))
642 return 0;
643
644 if (import_changed || export_changed)
645 log(L_INFO "Reloading channel %s.%s", c->proto->name, c->name);
646
647 if (import_changed)
648 channel_request_reload(c);
649
650 if (export_changed)
651 channel_request_feeding(c);
652
653 return 1;
654 }
655
656
657 int
658 proto_configure_channel(struct proto *p, struct channel **pc, struct channel_config *cf)
659 {
660 struct channel *c = *pc;
661
662 if (!c && cf)
663 {
664 /* We could add the channel, but currently it would just stay in down state
665 until protocol is restarted, so it is better to force restart anyways. */
666 if (p->proto_state != PS_DOWN)
667 {
668 log(L_INFO "Cannot add channel %s.%s", p->name, cf->name);
669 return 0;
670 }
671
672 *pc = proto_add_channel(p, cf);
673 }
674 else if (c && !cf)
675 {
676 if (c->channel_state != CS_DOWN)
677 {
678 log(L_INFO "Cannot remove channel %s.%s", c->proto->name, c->name);
679 return 0;
680 }
681
682 proto_remove_channel(p, c);
683 *pc = NULL;
684 }
685 else if (c && cf)
686 {
687 if (!channel_reconfigure(c, cf))
688 {
689 log(L_INFO "Cannot reconfigure channel %s.%s", c->proto->name, c->name);
690 return 0;
691 }
692 }
693
694 return 1;
695 }
696
697
698 static void
699 proto_event(void *ptr)
700 {
701 struct proto *p = ptr;
702
703 if (p->do_start)
704 {
705 if_feed_baby(p);
706 p->do_start = 0;
707 }
708
709 if (p->do_stop)
710 {
711 if (p->proto == &proto_unix_iface)
712 if_flush_ifaces(p);
713 p->do_stop = 0;
714 }
715
716 if (proto_is_done(p))
717 {
718 if (p->proto->cleanup)
719 p->proto->cleanup(p);
720
721 p->active = 0;
722 proto_log_state_change(p);
723 proto_rethink_goal(p);
724 }
725 }
726
727
728 /**
729 * proto_new - create a new protocol instance
730 * @c: protocol configuration
731 *
732 * When a new configuration has been read in, the core code starts
733 * initializing all the protocol instances configured by calling their
734 * init() hooks with the corresponding instance configuration. The initialization
735 * code of the protocol is expected to create a new instance according to the
736 * configuration by calling this function and then modifying the default settings
737 * to values wanted by the protocol.
738 */
739 void *
740 proto_new(struct proto_config *cf)
741 {
742 struct proto *p = mb_allocz(proto_pool, cf->protocol->proto_size);
743
744 p->cf = cf;
745 p->debug = cf->debug;
746 p->mrtdump = cf->mrtdump;
747 p->name = cf->name;
748 p->proto = cf->protocol;
749 p->net_type = cf->net_type;
750 p->disabled = cf->disabled;
751 p->hash_key = random_u32();
752 cf->proto = p;
753
754 init_list(&p->channels);
755
756 return p;
757 }
758
759 static struct proto *
760 proto_init(struct proto_config *c, node *n)
761 {
762 struct protocol *pr = c->protocol;
763 struct proto *p = pr->init(c);
764
765 p->proto_state = PS_DOWN;
766 p->last_state_change = current_time();
767 p->vrf = c->vrf;
768 insert_node(&p->n, n);
769
770 p->event = ev_new_init(proto_pool, proto_event, p);
771
772 PD(p, "Initializing%s", p->disabled ? " [disabled]" : "");
773
774 return p;
775 }
776
777 static void
778 proto_start(struct proto *p)
779 {
780 /* Here we cannot use p->cf->name since it won't survive reconfiguration */
781 p->pool = rp_new(proto_pool, p->proto->name);
782
783 if (graceful_restart_state == GRS_INIT)
784 p->gr_recovery = 1;
785 }
786
787
788 /**
789 * proto_config_new - create a new protocol configuration
790 * @pr: protocol the configuration will belong to
791 * @class: SYM_PROTO or SYM_TEMPLATE
792 *
793 * Whenever the configuration file says that a new instance
794 * of a routing protocol should be created, the parser calls
795 * proto_config_new() to create a configuration entry for this
796 * instance (a structure staring with the &proto_config header
797 * containing all the generic items followed by protocol-specific
798 * ones). Also, the configuration entry gets added to the list
799 * of protocol instances kept in the configuration.
800 *
801 * The function is also used to create protocol templates (when class
802 * SYM_TEMPLATE is specified), the only difference is that templates
803 * are not added to the list of protocol instances and therefore not
804 * initialized during protos_commit()).
805 */
806 void *
807 proto_config_new(struct protocol *pr, int class)
808 {
809 struct proto_config *cf = cfg_allocz(pr->config_size);
810
811 if (class == SYM_PROTO)
812 add_tail(&new_config->protos, &cf->n);
813
814 cf->global = new_config;
815 cf->protocol = pr;
816 cf->name = pr->name;
817 cf->class = class;
818 cf->debug = new_config->proto_default_debug;
819 cf->mrtdump = new_config->proto_default_mrtdump;
820
821 init_list(&cf->channels);
822
823 return cf;
824 }
825
826
827 /**
828 * proto_copy_config - copy a protocol configuration
829 * @dest: destination protocol configuration
830 * @src: source protocol configuration
831 *
832 * Whenever a new instance of a routing protocol is created from the
833 * template, proto_copy_config() is called to copy a content of
834 * the source protocol configuration to the new protocol configuration.
835 * Name, class and a node in protos list of @dest are kept intact.
836 * copy_config() protocol hook is used to copy protocol-specific data.
837 */
838 void
839 proto_copy_config(struct proto_config *dest, struct proto_config *src)
840 {
841 struct channel_config *cc;
842 node old_node;
843 int old_class;
844 char *old_name;
845
846 if (dest->protocol != src->protocol)
847 cf_error("Can't copy configuration from a different protocol type");
848
849 if (dest->protocol->copy_config == NULL)
850 cf_error("Inheriting configuration for %s is not supported", src->protocol->name);
851
852 DBG("Copying configuration from %s to %s\n", src->name, dest->name);
853
854 /*
855 * Copy struct proto_config here. Keep original node, class and name.
856 * protocol-specific config copy is handled by protocol copy_config() hook
857 */
858
859 old_node = dest->n;
860 old_class = dest->class;
861 old_name = dest->name;
862
863 memcpy(dest, src, src->protocol->config_size);
864
865 dest->n = old_node;
866 dest->class = old_class;
867 dest->name = old_name;
868 init_list(&dest->channels);
869
870 WALK_LIST(cc, src->channels)
871 channel_copy_config(cc, dest);
872
873 /* FIXME: allow for undefined copy_config */
874 dest->protocol->copy_config(dest, src);
875 }
876
877 void
878 proto_clone_config(struct symbol *sym, struct proto_config *parent)
879 {
880 struct proto_config *cf = proto_config_new(parent->protocol, SYM_PROTO);
881 proto_copy_config(cf, parent);
882 cf->name = sym->name;
883 cf->proto = NULL;
884 cf->parent = parent;
885
886 sym->class = cf->class;
887 sym->def = cf;
888 }
889
890 static void
891 proto_undef_clone(struct symbol *sym, struct proto_config *cf)
892 {
893 rem_node(&cf->n);
894
895 sym->class = SYM_VOID;
896 sym->def = NULL;
897 }
898
899 /**
900 * protos_preconfig - pre-configuration processing
901 * @c: new configuration
902 *
903 * This function calls the preconfig() hooks of all routing
904 * protocols available to prepare them for reading of the new
905 * configuration.
906 */
907 void
908 protos_preconfig(struct config *c)
909 {
910 struct protocol *p;
911
912 init_list(&c->protos);
913 DBG("Protocol preconfig:");
914 WALK_LIST(p, protocol_list)
915 {
916 DBG(" %s", p->name);
917 p->name_counter = 0;
918 if (p->preconfig)
919 p->preconfig(p, c);
920 }
921 DBG("\n");
922 }
923
924 static int
925 proto_reconfigure(struct proto *p, struct proto_config *oc, struct proto_config *nc, int type)
926 {
927 /* If the protocol is DOWN, we just restart it */
928 if (p->proto_state == PS_DOWN)
929 return 0;
930
931 /* If there is a too big change in core attributes, ... */
932 if ((nc->protocol != oc->protocol) ||
933 (nc->net_type != oc->net_type) ||
934 (nc->disabled != p->disabled) ||
935 (nc->vrf != oc->vrf))
936 return 0;
937
938 p->name = nc->name;
939 p->debug = nc->debug;
940 p->mrtdump = nc->mrtdump;
941 reconfigure_type = type;
942
943 /* Execute protocol specific reconfigure hook */
944 if (!p->proto->reconfigure || !p->proto->reconfigure(p, nc))
945 return 0;
946
947 DBG("\t%s: same\n", oc->name);
948 PD(p, "Reconfigured");
949 p->cf = nc;
950
951 return 1;
952 }
953
954 /**
955 * protos_commit - commit new protocol configuration
956 * @new: new configuration
957 * @old: old configuration or %NULL if it's boot time config
958 * @force_reconfig: force restart of all protocols (used for example
959 * when the router ID changes)
960 * @type: type of reconfiguration (RECONFIG_SOFT or RECONFIG_HARD)
961 *
962 * Scan differences between @old and @new configuration and adjust all
963 * protocol instances to conform to the new configuration.
964 *
965 * When a protocol exists in the new configuration, but it doesn't in the
966 * original one, it's immediately started. When a collision with the other
967 * running protocol would arise, the new protocol will be temporarily stopped
968 * by the locking mechanism.
969 *
970 * When a protocol exists in the old configuration, but it doesn't in the
971 * new one, it's shut down and deleted after the shutdown completes.
972 *
973 * When a protocol exists in both configurations, the core decides
974 * whether it's possible to reconfigure it dynamically - it checks all
975 * the core properties of the protocol (changes in filters are ignored
976 * if type is RECONFIG_SOFT) and if they match, it asks the
977 * reconfigure() hook of the protocol to see if the protocol is able
978 * to switch to the new configuration. If it isn't possible, the
979 * protocol is shut down and a new instance is started with the new
980 * configuration after the shutdown is completed.
981 */
982 void
983 protos_commit(struct config *new, struct config *old, int force_reconfig, int type)
984 {
985 struct proto_config *oc, *nc;
986 struct symbol *sym;
987 struct proto *p;
988 node *n;
989
990
991 DBG("protos_commit:\n");
992 if (old)
993 {
994 WALK_LIST(oc, old->protos)
995 {
996 p = oc->proto;
997 sym = cf_find_symbol(new, oc->name);
998
999 /* Handle dynamic protocols */
1000 if (!sym && oc->parent && !new->shutdown)
1001 {
1002 struct symbol *parsym = cf_find_symbol(new, oc->parent->name);
1003 if (parsym && parsym->class == SYM_PROTO)
1004 {
1005 /* This is hack, we would like to share config, but we need to copy it now */
1006 new_config = new;
1007 cfg_mem = new->mem;
1008 conf_this_scope = new->root_scope;
1009 sym = cf_get_symbol(oc->name);
1010 proto_clone_config(sym, parsym->def);
1011 new_config = NULL;
1012 cfg_mem = NULL;
1013 }
1014 }
1015
1016 if (sym && sym->class == SYM_PROTO && !new->shutdown)
1017 {
1018 /* Found match, let's check if we can smoothly switch to new configuration */
1019 /* No need to check description */
1020 nc = sym->def;
1021 nc->proto = p;
1022
1023 /* We will try to reconfigure protocol p */
1024 if (! force_reconfig && proto_reconfigure(p, oc, nc, type))
1025 continue;
1026
1027 if (nc->parent)
1028 {
1029 proto_undef_clone(sym, nc);
1030 goto remove;
1031 }
1032
1033 /* Unsuccessful, we will restart it */
1034 if (!p->disabled && !nc->disabled)
1035 log(L_INFO "Restarting protocol %s", p->name);
1036 else if (p->disabled && !nc->disabled)
1037 log(L_INFO "Enabling protocol %s", p->name);
1038 else if (!p->disabled && nc->disabled)
1039 log(L_INFO "Disabling protocol %s", p->name);
1040
1041 p->down_code = nc->disabled ? PDC_CF_DISABLE : PDC_CF_RESTART;
1042 p->cf_new = nc;
1043 }
1044 else if (!new->shutdown)
1045 {
1046 remove:
1047 log(L_INFO "Removing protocol %s", p->name);
1048 p->down_code = PDC_CF_REMOVE;
1049 p->cf_new = NULL;
1050 }
1051 else if (new->gr_down)
1052 {
1053 p->down_code = PDC_CMD_GR_DOWN;
1054 p->cf_new = NULL;
1055 }
1056 else /* global shutdown */
1057 {
1058 p->down_code = PDC_CMD_SHUTDOWN;
1059 p->cf_new = NULL;
1060 }
1061
1062 p->reconfiguring = 1;
1063 config_add_obstacle(old);
1064 proto_rethink_goal(p);
1065 }
1066 }
1067
1068 struct proto *first_dev_proto = NULL;
1069
1070 n = NODE &(proto_list.head);
1071 WALK_LIST(nc, new->protos)
1072 if (!nc->proto)
1073 {
1074 /* Not a first-time configuration */
1075 if (old)
1076 log(L_INFO "Adding protocol %s", nc->name);
1077
1078 p = proto_init(nc, n);
1079 n = NODE p;
1080
1081 if (p->proto == &proto_unix_iface)
1082 first_dev_proto = p;
1083 }
1084 else
1085 n = NODE nc->proto;
1086
1087 DBG("Protocol start\n");
1088
1089 /* Start device protocol first */
1090 if (first_dev_proto)
1091 proto_rethink_goal(first_dev_proto);
1092
1093 /* Determine router ID for the first time - it has to be here and not in
1094 global_commit() because it is postponed after start of device protocol */
1095 if (!config->router_id)
1096 {
1097 config->router_id = if_choose_router_id(config->router_id_from, 0);
1098 if (!config->router_id)
1099 die("Cannot determine router ID, please configure it manually");
1100 }
1101
1102 /* Start all new protocols */
1103 WALK_LIST_DELSAFE(p, n, proto_list)
1104 proto_rethink_goal(p);
1105 }
1106
1107 static void
1108 proto_rethink_goal(struct proto *p)
1109 {
1110 struct protocol *q;
1111 byte goal;
1112
1113 if (p->reconfiguring && !p->active)
1114 {
1115 struct proto_config *nc = p->cf_new;
1116 node *n = p->n.prev;
1117 DBG("%s has shut down for reconfiguration\n", p->name);
1118 p->cf->proto = NULL;
1119 config_del_obstacle(p->cf->global);
1120 proto_remove_channels(p);
1121 rem_node(&p->n);
1122 rfree(p->event);
1123 mb_free(p->message);
1124 mb_free(p);
1125 if (!nc)
1126 return;
1127 p = proto_init(nc, n);
1128 }
1129
1130 /* Determine what state we want to reach */
1131 if (p->disabled || p->reconfiguring)
1132 goal = PS_DOWN;
1133 else
1134 goal = PS_UP;
1135
1136 q = p->proto;
1137 if (goal == PS_UP)
1138 {
1139 if (!p->active)
1140 {
1141 /* Going up */
1142 DBG("Kicking %s up\n", p->name);
1143 PD(p, "Starting");
1144 proto_start(p);
1145 proto_notify_state(p, (q->start ? q->start(p) : PS_UP));
1146 }
1147 }
1148 else
1149 {
1150 if (p->proto_state == PS_START || p->proto_state == PS_UP)
1151 {
1152 /* Going down */
1153 DBG("Kicking %s down\n", p->name);
1154 PD(p, "Shutting down");
1155 proto_notify_state(p, (q->shutdown ? q->shutdown(p) : PS_DOWN));
1156 }
1157 }
1158 }
1159
1160 struct proto *
1161 proto_spawn(struct proto_config *cf, uint disabled)
1162 {
1163 struct proto *p = proto_init(cf, TAIL(proto_list));
1164 p->disabled = disabled;
1165 proto_rethink_goal(p);
1166 return p;
1167 }
1168
1169
1170 /**
1171 * DOC: Graceful restart recovery
1172 *
1173 * Graceful restart of a router is a process when the routing plane (e.g. BIRD)
1174 * restarts but both the forwarding plane (e.g kernel routing table) and routing
1175 * neighbors keep proper routes, and therefore uninterrupted packet forwarding
1176 * is maintained.
1177 *
1178 * BIRD implements graceful restart recovery by deferring export of routes to
1179 * protocols until routing tables are refilled with the expected content. After
1180 * start, protocols generate routes as usual, but routes are not propagated to
1181 * them, until protocols report that they generated all routes. After that,
1182 * graceful restart recovery is finished and the export (and the initial feed)
1183 * to protocols is enabled.
1184 *
1185 * When graceful restart recovery need is detected during initialization, then
1186 * enabled protocols are marked with @gr_recovery flag before start. Such
1187 * protocols then decide how to proceed with graceful restart, participation is
1188 * voluntary. Protocols could lock the recovery for each channel by function
1189 * channel_graceful_restart_lock() (state stored in @gr_lock flag), which means
1190 * that they want to postpone the end of the recovery until they converge and
1191 * then unlock it. They also could set @gr_wait before advancing to %PS_UP,
1192 * which means that the core should defer route export to that channel until
1193 * the end of the recovery. This should be done by protocols that expect their
1194 * neigbors to keep the proper routes (kernel table, BGP sessions with BGP
1195 * graceful restart capability).
1196 *
1197 * The graceful restart recovery is finished when either all graceful restart
1198 * locks are unlocked or when graceful restart wait timer fires.
1199 *
1200 */
1201
1202 static void graceful_restart_done(timer *t);
1203
1204 /**
1205 * graceful_restart_recovery - request initial graceful restart recovery
1206 *
1207 * Called by the platform initialization code if the need for recovery
1208 * after graceful restart is detected during boot. Have to be called
1209 * before protos_commit().
1210 */
1211 void
1212 graceful_restart_recovery(void)
1213 {
1214 graceful_restart_state = GRS_INIT;
1215 }
1216
1217 /**
1218 * graceful_restart_init - initialize graceful restart
1219 *
1220 * When graceful restart recovery was requested, the function starts an active
1221 * phase of the recovery and initializes graceful restart wait timer. The
1222 * function have to be called after protos_commit().
1223 */
1224 void
1225 graceful_restart_init(void)
1226 {
1227 if (!graceful_restart_state)
1228 return;
1229
1230 log(L_INFO "Graceful restart started");
1231
1232 if (!graceful_restart_locks)
1233 {
1234 graceful_restart_done(NULL);
1235 return;
1236 }
1237
1238 graceful_restart_state = GRS_ACTIVE;
1239 gr_wait_timer = tm_new_init(proto_pool, graceful_restart_done, NULL, 0, 0);
1240 tm_start(gr_wait_timer, config->gr_wait S);
1241 }
1242
1243 /**
1244 * graceful_restart_done - finalize graceful restart
1245 * @t: unused
1246 *
1247 * When there are no locks on graceful restart, the functions finalizes the
1248 * graceful restart recovery. Protocols postponing route export until the end of
1249 * the recovery are awakened and the export to them is enabled. All other
1250 * related state is cleared. The function is also called when the graceful
1251 * restart wait timer fires (but there are still some locks).
1252 */
1253 static void
1254 graceful_restart_done(timer *t UNUSED)
1255 {
1256 log(L_INFO "Graceful restart done");
1257 graceful_restart_state = GRS_DONE;
1258
1259 struct proto *p;
1260 WALK_LIST(p, proto_list)
1261 {
1262 if (!p->gr_recovery)
1263 continue;
1264
1265 struct channel *c;
1266 WALK_LIST(c, p->channels)
1267 {
1268 /* Resume postponed export of routes */
1269 if ((c->channel_state == CS_UP) && c->gr_wait && c->proto->rt_notify)
1270 channel_start_export(c);
1271
1272 /* Cleanup */
1273 c->gr_wait = 0;
1274 c->gr_lock = 0;
1275 }
1276
1277 p->gr_recovery = 0;
1278 }
1279
1280 graceful_restart_locks = 0;
1281 }
1282
1283 void
1284 graceful_restart_show_status(void)
1285 {
1286 if (graceful_restart_state != GRS_ACTIVE)
1287 return;
1288
1289 cli_msg(-24, "Graceful restart recovery in progress");
1290 cli_msg(-24, " Waiting for %d channels to recover", graceful_restart_locks);
1291 cli_msg(-24, " Wait timer is %t/%u", tm_remains(gr_wait_timer), config->gr_wait);
1292 }
1293
1294 /**
1295 * channel_graceful_restart_lock - lock graceful restart by channel
1296 * @p: channel instance
1297 *
1298 * This function allows a protocol to postpone the end of graceful restart
1299 * recovery until it converges. The lock is removed when the protocol calls
1300 * channel_graceful_restart_unlock() or when the channel is closed.
1301 *
1302 * The function have to be called during the initial phase of graceful restart
1303 * recovery and only for protocols that are part of graceful restart (i.e. their
1304 * @gr_recovery is set), which means it should be called from protocol start
1305 * hooks.
1306 */
1307 void
1308 channel_graceful_restart_lock(struct channel *c)
1309 {
1310 ASSERT(graceful_restart_state == GRS_INIT);
1311 ASSERT(c->proto->gr_recovery);
1312
1313 if (c->gr_lock)
1314 return;
1315
1316 c->gr_lock = 1;
1317 graceful_restart_locks++;
1318 }
1319
1320 /**
1321 * channel_graceful_restart_unlock - unlock graceful restart by channel
1322 * @p: channel instance
1323 *
1324 * This function unlocks a lock from channel_graceful_restart_lock(). It is also
1325 * automatically called when the lock holding protocol went down.
1326 */
1327 void
1328 channel_graceful_restart_unlock(struct channel *c)
1329 {
1330 if (!c->gr_lock)
1331 return;
1332
1333 c->gr_lock = 0;
1334 graceful_restart_locks--;
1335
1336 if ((graceful_restart_state == GRS_ACTIVE) && !graceful_restart_locks)
1337 tm_start(gr_wait_timer, 0);
1338 }
1339
1340
1341
1342 /**
1343 * protos_dump_all - dump status of all protocols
1344 *
1345 * This function dumps status of all existing protocol instances to the
1346 * debug output. It involves printing of general status information
1347 * such as protocol states, its position on the protocol lists
1348 * and also calling of a dump() hook of the protocol to print
1349 * the internals.
1350 */
1351 void
1352 protos_dump_all(void)
1353 {
1354 debug("Protocols:\n");
1355
1356 struct proto *p;
1357 WALK_LIST(p, proto_list)
1358 {
1359 debug(" protocol %s state %s\n", p->name, p_states[p->proto_state]);
1360
1361 struct channel *c;
1362 WALK_LIST(c, p->channels)
1363 {
1364 debug("\tTABLE %s\n", c->table->name);
1365 if (c->in_filter)
1366 debug("\tInput filter: %s\n", filter_name(c->in_filter));
1367 if (c->out_filter)
1368 debug("\tOutput filter: %s\n", filter_name(c->out_filter));
1369 }
1370
1371 if (p->proto->dump && (p->proto_state != PS_DOWN))
1372 p->proto->dump(p);
1373 }
1374 }
1375
1376 /**
1377 * proto_build - make a single protocol available
1378 * @p: the protocol
1379 *
1380 * After the platform specific initialization code uses protos_build()
1381 * to add all the standard protocols, it should call proto_build() for
1382 * all platform specific protocols to inform the core that they exist.
1383 */
1384 void
1385 proto_build(struct protocol *p)
1386 {
1387 add_tail(&protocol_list, &p->n);
1388 ASSERT(p->class);
1389 ASSERT(!class_to_protocol[p->class]);
1390 class_to_protocol[p->class] = p;
1391 }
1392
1393 /* FIXME: convert this call to some protocol hook */
1394 extern void bfd_init_all(void);
1395
1396 /**
1397 * protos_build - build a protocol list
1398 *
1399 * This function is called during BIRD startup to insert
1400 * all standard protocols to the global protocol list. Insertion
1401 * of platform specific protocols (such as the kernel syncer)
1402 * is in the domain of competence of the platform dependent
1403 * startup code.
1404 */
1405 void
1406 protos_build(void)
1407 {
1408 init_list(&proto_list);
1409 init_list(&protocol_list);
1410
1411 proto_build(&proto_device);
1412 #ifdef CONFIG_RADV
1413 proto_build(&proto_radv);
1414 #endif
1415 #ifdef CONFIG_RIP
1416 proto_build(&proto_rip);
1417 #endif
1418 #ifdef CONFIG_STATIC
1419 proto_build(&proto_static);
1420 #endif
1421 #ifdef CONFIG_MRT
1422 proto_build(&proto_mrt);
1423 #endif
1424 #ifdef CONFIG_OSPF
1425 proto_build(&proto_ospf);
1426 #endif
1427 #ifdef CONFIG_PIPE
1428 proto_build(&proto_pipe);
1429 #endif
1430 #ifdef CONFIG_BGP
1431 proto_build(&proto_bgp);
1432 #endif
1433 #ifdef CONFIG_BFD
1434 proto_build(&proto_bfd);
1435 bfd_init_all();
1436 #endif
1437 #ifdef CONFIG_BABEL
1438 proto_build(&proto_babel);
1439 #endif
1440 #ifdef CONFIG_RPKI
1441 proto_build(&proto_rpki);
1442 #endif
1443 #ifdef CONFIG_PERF
1444 proto_build(&proto_perf);
1445 #endif
1446
1447 proto_pool = rp_new(&root_pool, "Protocols");
1448 proto_shutdown_timer = tm_new(proto_pool);
1449 proto_shutdown_timer->hook = proto_shutdown_loop;
1450 }
1451
1452
1453 /* Temporary hack to propagate restart to BGP */
1454 int proto_restart;
1455
1456 static void
1457 proto_shutdown_loop(timer *t UNUSED)
1458 {
1459 struct proto *p, *p_next;
1460
1461 WALK_LIST_DELSAFE(p, p_next, proto_list)
1462 if (p->down_sched)
1463 {
1464 proto_restart = (p->down_sched == PDS_RESTART);
1465
1466 p->disabled = 1;
1467 proto_rethink_goal(p);
1468 if (proto_restart)
1469 {
1470 p->disabled = 0;
1471 proto_rethink_goal(p);
1472 }
1473 }
1474 }
1475
1476 static inline void
1477 proto_schedule_down(struct proto *p, byte restart, byte code)
1478 {
1479 /* Does not work for other states (even PS_START) */
1480 ASSERT(p->proto_state == PS_UP);
1481
1482 /* Scheduled restart may change to shutdown, but not otherwise */
1483 if (p->down_sched == PDS_DISABLE)
1484 return;
1485
1486 p->down_sched = restart ? PDS_RESTART : PDS_DISABLE;
1487 p->down_code = code;
1488 tm_start_max(proto_shutdown_timer, restart ? 250 MS : 0);
1489 }
1490
1491 /**
1492 * proto_set_message - set administrative message to protocol
1493 * @p: protocol
1494 * @msg: message
1495 * @len: message length (-1 for NULL-terminated string)
1496 *
1497 * The function sets administrative message (string) related to protocol state
1498 * change. It is called by the nest code for manual enable/disable/restart
1499 * commands all routes to the protocol, and by protocol-specific code when the
1500 * protocol state change is initiated by the protocol. Using NULL message clears
1501 * the last message. The message string may be either NULL-terminated or with an
1502 * explicit length.
1503 */
1504 void
1505 proto_set_message(struct proto *p, char *msg, int len)
1506 {
1507 mb_free(p->message);
1508 p->message = NULL;
1509
1510 if (!msg || !len)
1511 return;
1512
1513 if (len < 0)
1514 len = strlen(msg);
1515
1516 if (!len)
1517 return;
1518
1519 p->message = mb_alloc(proto_pool, len + 1);
1520 memcpy(p->message, msg, len);
1521 p->message[len] = 0;
1522 }
1523
1524
1525 static const char *
1526 channel_limit_name(struct channel_limit *l)
1527 {
1528 const char *actions[] = {
1529 [PLA_WARN] = "warn",
1530 [PLA_BLOCK] = "block",
1531 [PLA_RESTART] = "restart",
1532 [PLA_DISABLE] = "disable",
1533 };
1534
1535 return actions[l->action];
1536 }
1537
1538 /**
1539 * channel_notify_limit: notify about limit hit and take appropriate action
1540 * @c: channel
1541 * @l: limit being hit
1542 * @dir: limit direction (PLD_*)
1543 * @rt_count: the number of routes
1544 *
1545 * The function is called by the route processing core when limit @l
1546 * is breached. It activates the limit and tooks appropriate action
1547 * according to @l->action.
1548 */
1549 void
1550 channel_notify_limit(struct channel *c, struct channel_limit *l, int dir, u32 rt_count)
1551 {
1552 const char *dir_name[PLD_MAX] = { "receive", "import" , "export" };
1553 const byte dir_down[PLD_MAX] = { PDC_RX_LIMIT_HIT, PDC_IN_LIMIT_HIT, PDC_OUT_LIMIT_HIT };
1554 struct proto *p = c->proto;
1555
1556 if (l->state == PLS_BLOCKED)
1557 return;
1558
1559 /* For warning action, we want the log message every time we hit the limit */
1560 if (!l->state || ((l->action == PLA_WARN) && (rt_count == l->limit)))
1561 log(L_WARN "Protocol %s hits route %s limit (%d), action: %s",
1562 p->name, dir_name[dir], l->limit, channel_limit_name(l));
1563
1564 switch (l->action)
1565 {
1566 case PLA_WARN:
1567 l->state = PLS_ACTIVE;
1568 break;
1569
1570 case PLA_BLOCK:
1571 l->state = PLS_BLOCKED;
1572 break;
1573
1574 case PLA_RESTART:
1575 case PLA_DISABLE:
1576 l->state = PLS_BLOCKED;
1577 if (p->proto_state == PS_UP)
1578 proto_schedule_down(p, l->action == PLA_RESTART, dir_down[dir]);
1579 break;
1580 }
1581 }
1582
1583 static void
1584 channel_verify_limits(struct channel *c)
1585 {
1586 struct channel_limit *l;
1587 u32 all_routes = c->stats.imp_routes + c->stats.filt_routes;
1588
1589 l = &c->rx_limit;
1590 if (l->action && (all_routes > l->limit))
1591 channel_notify_limit(c, l, PLD_RX, all_routes);
1592
1593 l = &c->in_limit;
1594 if (l->action && (c->stats.imp_routes > l->limit))
1595 channel_notify_limit(c, l, PLD_IN, c->stats.imp_routes);
1596
1597 l = &c->out_limit;
1598 if (l->action && (c->stats.exp_routes > l->limit))
1599 channel_notify_limit(c, l, PLD_OUT, c->stats.exp_routes);
1600 }
1601
1602 static inline void
1603 channel_reset_limit(struct channel_limit *l)
1604 {
1605 if (l->action)
1606 l->state = PLS_INITIAL;
1607 }
1608
1609 static inline void
1610 proto_do_start(struct proto *p)
1611 {
1612 p->active = 1;
1613 p->do_start = 1;
1614 ev_schedule(p->event);
1615 }
1616
1617 static void
1618 proto_do_up(struct proto *p)
1619 {
1620 if (!p->main_source)
1621 {
1622 p->main_source = rt_get_source(p, 0);
1623 rt_lock_source(p->main_source);
1624 }
1625
1626 proto_start_channels(p);
1627 }
1628
1629 static inline void
1630 proto_do_pause(struct proto *p)
1631 {
1632 proto_pause_channels(p);
1633 }
1634
1635 static void
1636 proto_do_stop(struct proto *p)
1637 {
1638 p->down_sched = 0;
1639 p->gr_recovery = 0;
1640
1641 p->do_stop = 1;
1642 ev_schedule(p->event);
1643
1644 if (p->main_source)
1645 {
1646 rt_unlock_source(p->main_source);
1647 p->main_source = NULL;
1648 }
1649
1650 proto_stop_channels(p);
1651 }
1652
1653 static void
1654 proto_do_down(struct proto *p)
1655 {
1656 p->down_code = 0;
1657 neigh_prune();
1658 rfree(p->pool);
1659 p->pool = NULL;
1660
1661 /* Shutdown is finished in the protocol event */
1662 if (proto_is_done(p))
1663 ev_schedule(p->event);
1664 }
1665
1666
1667
1668 /**
1669 * proto_notify_state - notify core about protocol state change
1670 * @p: protocol the state of which has changed
1671 * @ps: the new status
1672 *
1673 * Whenever a state of a protocol changes due to some event internal
1674 * to the protocol (i.e., not inside a start() or shutdown() hook),
1675 * it should immediately notify the core about the change by calling
1676 * proto_notify_state() which will write the new state to the &proto
1677 * structure and take all the actions necessary to adapt to the new
1678 * state. State change to PS_DOWN immediately frees resources of protocol
1679 * and might execute start callback of protocol; therefore,
1680 * it should be used at tail positions of protocol callbacks.
1681 */
1682 void
1683 proto_notify_state(struct proto *p, uint state)
1684 {
1685 uint ps = p->proto_state;
1686
1687 DBG("%s reporting state transition %s -> %s\n", p->name, p_states[ps], p_states[state]);
1688 if (state == ps)
1689 return;
1690
1691 p->proto_state = state;
1692 p->last_state_change = current_time();
1693
1694 switch (state)
1695 {
1696 case PS_START:
1697 ASSERT(ps == PS_DOWN || ps == PS_UP);
1698
1699 if (ps == PS_DOWN)
1700 proto_do_start(p);
1701 else
1702 proto_do_pause(p);
1703 break;
1704
1705 case PS_UP:
1706 ASSERT(ps == PS_DOWN || ps == PS_START);
1707
1708 if (ps == PS_DOWN)
1709 proto_do_start(p);
1710
1711 proto_do_up(p);
1712 break;
1713
1714 case PS_STOP:
1715 ASSERT(ps == PS_START || ps == PS_UP);
1716
1717 proto_do_stop(p);
1718 break;
1719
1720 case PS_DOWN:
1721 if (ps != PS_STOP)
1722 proto_do_stop(p);
1723
1724 proto_do_down(p);
1725 break;
1726
1727 default:
1728 bug("%s: Invalid state %d", p->name, ps);
1729 }
1730
1731 proto_log_state_change(p);
1732 }
1733
1734 /*
1735 * CLI Commands
1736 */
1737
1738 static char *
1739 proto_state_name(struct proto *p)
1740 {
1741 switch (p->proto_state)
1742 {
1743 case PS_DOWN: return p->active ? "flush" : "down";
1744 case PS_START: return "start";
1745 case PS_UP: return "up";
1746 case PS_STOP: return "stop";
1747 default: return "???";
1748 }
1749 }
1750
1751 static void
1752 channel_show_stats(struct channel *c)
1753 {
1754 struct proto_stats *s = &c->stats;
1755
1756 if (c->in_keep_filtered)
1757 cli_msg(-1006, " Routes: %u imported, %u filtered, %u exported, %u preferred",
1758 s->imp_routes, s->filt_routes, s->exp_routes, s->pref_routes);
1759 else
1760 cli_msg(-1006, " Routes: %u imported, %u exported, %u preferred",
1761 s->imp_routes, s->exp_routes, s->pref_routes);
1762
1763 cli_msg(-1006, " Route change stats: received rejected filtered ignored accepted");
1764 cli_msg(-1006, " Import updates: %10u %10u %10u %10u %10u",
1765 s->imp_updates_received, s->imp_updates_invalid,
1766 s->imp_updates_filtered, s->imp_updates_ignored,
1767 s->imp_updates_accepted);
1768 cli_msg(-1006, " Import withdraws: %10u %10u --- %10u %10u",
1769 s->imp_withdraws_received, s->imp_withdraws_invalid,
1770 s->imp_withdraws_ignored, s->imp_withdraws_accepted);
1771 cli_msg(-1006, " Export updates: %10u %10u %10u --- %10u",
1772 s->exp_updates_received, s->exp_updates_rejected,
1773 s->exp_updates_filtered, s->exp_updates_accepted);
1774 cli_msg(-1006, " Export withdraws: %10u --- --- --- %10u",
1775 s->exp_withdraws_received, s->exp_withdraws_accepted);
1776 }
1777
1778 void
1779 channel_show_limit(struct channel_limit *l, const char *dsc)
1780 {
1781 if (!l->action)
1782 return;
1783
1784 cli_msg(-1006, " %-16s%d%s", dsc, l->limit, l->state ? " [HIT]" : "");
1785 cli_msg(-1006, " Action: %s", channel_limit_name(l));
1786 }
1787
1788 void
1789 channel_show_info(struct channel *c)
1790 {
1791 cli_msg(-1006, " Channel %s", c->name);
1792 cli_msg(-1006, " State: %s", c_states[c->channel_state]);
1793 cli_msg(-1006, " Table: %s", c->table->name);
1794 cli_msg(-1006, " Preference: %d", c->preference);
1795 cli_msg(-1006, " Input filter: %s", filter_name(c->in_filter));
1796 cli_msg(-1006, " Output filter: %s", filter_name(c->out_filter));
1797
1798 if (graceful_restart_state == GRS_ACTIVE)
1799 cli_msg(-1006, " GR recovery: %s%s",
1800 c->gr_lock ? " pending" : "",
1801 c->gr_wait ? " waiting" : "");
1802
1803 channel_show_limit(&c->rx_limit, "Receive limit:");
1804 channel_show_limit(&c->in_limit, "Import limit:");
1805 channel_show_limit(&c->out_limit, "Export limit:");
1806
1807 if (c->channel_state != CS_DOWN)
1808 channel_show_stats(c);
1809 }
1810
1811 void
1812 proto_cmd_show(struct proto *p, uintptr_t verbose, int cnt)
1813 {
1814 byte buf[256], tbuf[TM_DATETIME_BUFFER_SIZE];
1815
1816 /* First protocol - show header */
1817 if (!cnt)
1818 cli_msg(-2002, "%-10s %-10s %-10s %-6s %-12s %s",
1819 "Name", "Proto", "Table", "State", "Since", "Info");
1820
1821 buf[0] = 0;
1822 if (p->proto->get_status)
1823 p->proto->get_status(p, buf);
1824 tm_format_time(tbuf, &config->tf_proto, p->last_state_change);
1825 cli_msg(-1002, "%-10s %-10s %-10s %-6s %-12s %s",
1826 p->name,
1827 p->proto->name,
1828 p->main_channel ? p->main_channel->table->name : "---",
1829 proto_state_name(p),
1830 tbuf,
1831 buf);
1832
1833 if (verbose)
1834 {
1835 if (p->cf->dsc)
1836 cli_msg(-1006, " Description: %s", p->cf->dsc);
1837 if (p->message)
1838 cli_msg(-1006, " Message: %s", p->message);
1839 if (p->cf->router_id)
1840 cli_msg(-1006, " Router ID: %R", p->cf->router_id);
1841 if (p->vrf)
1842 cli_msg(-1006, " VRF: %s", p->vrf->name);
1843
1844 if (p->proto->show_proto_info)
1845 p->proto->show_proto_info(p);
1846 else
1847 {
1848 struct channel *c;
1849 WALK_LIST(c, p->channels)
1850 channel_show_info(c);
1851 }
1852
1853 cli_msg(-1006, "");
1854 }
1855 }
1856
1857 void
1858 proto_cmd_disable(struct proto *p, uintptr_t arg, int cnt UNUSED)
1859 {
1860 if (p->disabled)
1861 {
1862 cli_msg(-8, "%s: already disabled", p->name);
1863 return;
1864 }
1865
1866 log(L_INFO "Disabling protocol %s", p->name);
1867 p->disabled = 1;
1868 p->down_code = PDC_CMD_DISABLE;
1869 proto_set_message(p, (char *) arg, -1);
1870 proto_rethink_goal(p);
1871 cli_msg(-9, "%s: disabled", p->name);
1872 }
1873
1874 void
1875 proto_cmd_enable(struct proto *p, uintptr_t arg, int cnt UNUSED)
1876 {
1877 if (!p->disabled)
1878 {
1879 cli_msg(-10, "%s: already enabled", p->name);
1880 return;
1881 }
1882
1883 log(L_INFO "Enabling protocol %s", p->name);
1884 p->disabled = 0;
1885 proto_set_message(p, (char *) arg, -1);
1886 proto_rethink_goal(p);
1887 cli_msg(-11, "%s: enabled", p->name);
1888 }
1889
1890 void
1891 proto_cmd_restart(struct proto *p, uintptr_t arg, int cnt UNUSED)
1892 {
1893 if (p->disabled)
1894 {
1895 cli_msg(-8, "%s: already disabled", p->name);
1896 return;
1897 }
1898
1899 log(L_INFO "Restarting protocol %s", p->name);
1900 p->disabled = 1;
1901 p->down_code = PDC_CMD_RESTART;
1902 proto_set_message(p, (char *) arg, -1);
1903 proto_rethink_goal(p);
1904 p->disabled = 0;
1905 proto_rethink_goal(p);
1906 cli_msg(-12, "%s: restarted", p->name);
1907 }
1908
1909 void
1910 proto_cmd_reload(struct proto *p, uintptr_t dir, int cnt UNUSED)
1911 {
1912 struct channel *c;
1913
1914 if (p->disabled)
1915 {
1916 cli_msg(-8, "%s: already disabled", p->name);
1917 return;
1918 }
1919
1920 /* If the protocol in not UP, it has no routes */
1921 if (p->proto_state != PS_UP)
1922 return;
1923
1924 /* All channels must support reload */
1925 if (dir != CMD_RELOAD_OUT)
1926 WALK_LIST(c, p->channels)
1927 if (!channel_reloadable(c))
1928 {
1929 cli_msg(-8006, "%s: reload failed", p->name);
1930 return;
1931 }
1932
1933 log(L_INFO "Reloading protocol %s", p->name);
1934
1935 /* re-importing routes */
1936 if (dir != CMD_RELOAD_OUT)
1937 WALK_LIST(c, p->channels)
1938 channel_request_reload(c);
1939
1940 /* re-exporting routes */
1941 if (dir != CMD_RELOAD_IN)
1942 WALK_LIST(c, p->channels)
1943 channel_request_feeding(c);
1944
1945 cli_msg(-15, "%s: reloading", p->name);
1946 }
1947
1948 void
1949 proto_cmd_debug(struct proto *p, uintptr_t mask, int cnt UNUSED)
1950 {
1951 p->debug = mask;
1952 }
1953
1954 void
1955 proto_cmd_mrtdump(struct proto *p, uintptr_t mask, int cnt UNUSED)
1956 {
1957 p->mrtdump = mask;
1958 }
1959
1960 static void
1961 proto_apply_cmd_symbol(struct symbol *s, void (* cmd)(struct proto *, uintptr_t, int), uintptr_t arg)
1962 {
1963 if (s->class != SYM_PROTO)
1964 {
1965 cli_msg(9002, "%s is not a protocol", s->name);
1966 return;
1967 }
1968
1969 cmd(((struct proto_config *)s->def)->proto, arg, 0);
1970 cli_msg(0, "");
1971 }
1972
1973 static void
1974 proto_apply_cmd_patt(char *patt, void (* cmd)(struct proto *, uintptr_t, int), uintptr_t arg)
1975 {
1976 struct proto *p;
1977 int cnt = 0;
1978
1979 WALK_LIST(p, proto_list)
1980 if (!patt || patmatch(patt, p->name))
1981 cmd(p, arg, cnt++);
1982
1983 if (!cnt)
1984 cli_msg(8003, "No protocols match");
1985 else
1986 cli_msg(0, "");
1987 }
1988
1989 void
1990 proto_apply_cmd(struct proto_spec ps, void (* cmd)(struct proto *, uintptr_t, int),
1991 int restricted, uintptr_t arg)
1992 {
1993 if (restricted && cli_access_restricted())
1994 return;
1995
1996 if (ps.patt)
1997 proto_apply_cmd_patt(ps.ptr, cmd, arg);
1998 else
1999 proto_apply_cmd_symbol(ps.ptr, cmd, arg);
2000 }
2001
2002 struct proto *
2003 proto_get_named(struct symbol *sym, struct protocol *pr)
2004 {
2005 struct proto *p, *q;
2006
2007 if (sym)
2008 {
2009 if (sym->class != SYM_PROTO)
2010 cf_error("%s: Not a protocol", sym->name);
2011
2012 p = ((struct proto_config *) sym->def)->proto;
2013 if (!p || p->proto != pr)
2014 cf_error("%s: Not a %s protocol", sym->name, pr->name);
2015 }
2016 else
2017 {
2018 p = NULL;
2019 WALK_LIST(q, proto_list)
2020 if ((q->proto == pr) && (q->proto_state != PS_DOWN))
2021 {
2022 if (p)
2023 cf_error("There are multiple %s protocols running", pr->name);
2024 p = q;
2025 }
2026 if (!p)
2027 cf_error("There is no %s protocol running", pr->name);
2028 }
2029
2030 return p;
2031 }