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