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