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