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