]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_gr.c
ospfd,ospf6d: Add missing newline for `graceful-restart prepare` CLI
[mirror_frr.git] / ospfd / ospf_gr.c
1 /*
2 * This is an implementation of RFC 3623 Graceful OSPF Restart.
3 *
4 * Copyright 2021 NetDEF (c), All rights reserved.
5 * Copyright 2020 6WIND (c), All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <zebra.h>
23
24 #include "memory.h"
25 #include "command.h"
26 #include "table.h"
27 #include "vty.h"
28 #include "log.h"
29 #include "printfrr.h"
30
31 #include "ospfd/ospfd.h"
32 #include "ospfd/ospf_abr.h"
33 #include "ospfd/ospf_flood.h"
34 #include "ospfd/ospf_ism.h"
35 #include "ospfd/ospf_interface.h"
36 #include "ospfd/ospf_asbr.h"
37 #include "ospfd/ospf_lsa.h"
38 #include "ospfd/ospf_route.h"
39 #include "ospfd/ospf_zebra.h"
40 #include "ospfd/ospf_lsdb.h"
41 #include "ospfd/ospf_neighbor.h"
42 #include "ospfd/ospf_opaque.h"
43 #include "ospfd/ospf_nsm.h"
44 #include "ospfd/ospf_gr.h"
45 #include "ospfd/ospf_errors.h"
46 #include "ospfd/ospf_dump.h"
47 #ifndef VTYSH_EXTRACT_PL
48 #include "ospfd/ospf_gr_clippy.c"
49 #endif
50
51 static void ospf_gr_nvm_delete(struct ospf *ospf);
52
53 /* Lookup self-originated Grace-LSA in the LSDB. */
54 static struct ospf_lsa *ospf_gr_lsa_lookup(struct ospf *ospf,
55 struct ospf_area *area)
56 {
57 struct ospf_lsa *lsa;
58 struct in_addr lsa_id;
59 uint32_t lsa_id_host_byte_order;
60
61 lsa_id_host_byte_order = SET_OPAQUE_LSID(OPAQUE_TYPE_GRACE_LSA, 0);
62 lsa_id.s_addr = htonl(lsa_id_host_byte_order);
63 lsa = ospf_lsa_lookup(ospf, area, OSPF_OPAQUE_LINK_LSA, lsa_id,
64 ospf->router_id);
65
66 return lsa;
67 }
68
69 /* Fill in fields of the Grace-LSA that is being originated. */
70 static void ospf_gr_lsa_body_set(struct ospf_gr_info *gr_info,
71 struct ospf_interface *oi, struct stream *s)
72 {
73 struct grace_tlv_graceperiod tlv_period = {};
74 struct grace_tlv_restart_reason tlv_reason = {};
75 struct grace_tlv_restart_addr tlv_address = {};
76
77 /* Put grace period. */
78 tlv_period.header.type = htons(GRACE_PERIOD_TYPE);
79 tlv_period.header.length = htons(GRACE_PERIOD_LENGTH);
80 tlv_period.interval = htonl(gr_info->grace_period);
81 stream_put(s, &tlv_period, sizeof(tlv_period));
82
83 /* Put restart reason. */
84 tlv_reason.header.type = htons(RESTART_REASON_TYPE);
85 tlv_reason.header.length = htons(RESTART_REASON_LENGTH);
86 if (gr_info->restart_support)
87 tlv_reason.reason = OSPF_GR_SW_RESTART;
88 else
89 tlv_reason.reason = OSPF_GR_UNKNOWN_RESTART;
90 stream_put(s, &tlv_reason, sizeof(tlv_reason));
91
92 /* Put IP address. */
93 if (oi->type == OSPF_IFTYPE_BROADCAST || oi->type == OSPF_IFTYPE_NBMA
94 || oi->type == OSPF_IFTYPE_POINTOMULTIPOINT) {
95 tlv_address.header.type = htons(RESTARTER_IP_ADDR_TYPE);
96 tlv_address.header.length = htons(RESTARTER_IP_ADDR_LEN);
97 tlv_address.addr = oi->address->u.prefix4;
98 stream_put(s, &tlv_address, sizeof(tlv_address));
99 }
100 }
101
102 /* Generate Grace-LSA for a given interface. */
103 static struct ospf_lsa *ospf_gr_lsa_new(struct ospf_interface *oi)
104 {
105 struct stream *s;
106 struct lsa_header *lsah;
107 struct ospf_lsa *new;
108 uint8_t options, lsa_type;
109 struct in_addr lsa_id;
110 uint32_t lsa_id_host_byte_order;
111 uint16_t length;
112
113 /* Create a stream for LSA. */
114 s = stream_new(OSPF_MAX_LSA_SIZE);
115
116 lsah = (struct lsa_header *)STREAM_DATA(s);
117
118 options = LSA_OPTIONS_GET(oi->area);
119 options |= LSA_OPTIONS_NSSA_GET(oi->area);
120 options |= OSPF_OPTION_O;
121
122 lsa_type = OSPF_OPAQUE_LINK_LSA;
123 lsa_id_host_byte_order = SET_OPAQUE_LSID(OPAQUE_TYPE_GRACE_LSA, 0);
124 lsa_id.s_addr = htonl(lsa_id_host_byte_order);
125
126 /* Set opaque-LSA header fields. */
127 lsa_header_set(s, options, lsa_type, lsa_id, oi->ospf->router_id);
128
129 /* Set opaque-LSA body fields. */
130 ospf_gr_lsa_body_set(&oi->ospf->gr_info, oi, s);
131
132 /* Set length. */
133 length = stream_get_endp(s);
134 lsah->length = htons(length);
135
136 /* Now, create an OSPF LSA instance. */
137 new = ospf_lsa_new_and_data(length);
138
139 if (IS_DEBUG_OSPF_GR)
140 zlog_debug("LSA[Type%d:%pI4]: Create an Opaque-LSA/GR instance",
141 lsa_type, &lsa_id);
142
143 new->area = oi->area;
144 new->oi = oi;
145 SET_FLAG(new->flags, OSPF_LSA_SELF);
146 memcpy(new->data, lsah, length);
147 stream_free(s);
148
149 return new;
150 }
151
152 /* Originate and install Grace-LSA for a given interface. */
153 static void ospf_gr_lsa_originate(struct ospf_interface *oi, bool maxage)
154 {
155 struct ospf_lsa *lsa, *old;
156
157 if (ospf_interface_neighbor_count(oi) == 0)
158 return;
159
160 /* Create new Grace-LSA. */
161 lsa = ospf_gr_lsa_new(oi);
162 if (!lsa) {
163 zlog_warn("%s: ospf_gr_lsa_new() failed", __func__);
164 return;
165 }
166
167 if (maxage)
168 lsa->data->ls_age = htons(OSPF_LSA_MAXAGE);
169
170 /* Find the old LSA and increase the seqno. */
171 old = ospf_gr_lsa_lookup(oi->ospf, oi->area);
172 if (old)
173 lsa->data->ls_seqnum = lsa_seqnum_increment(old);
174
175 /* Install this LSA into LSDB. */
176 if (ospf_lsa_install(oi->ospf, oi, lsa) == NULL) {
177 zlog_warn("%s: ospf_lsa_install() failed", __func__);
178 ospf_lsa_unlock(&lsa);
179 return;
180 }
181
182 /* Update new LSA origination count. */
183 oi->ospf->lsa_originate_count++;
184
185 /* Flood the LSA through out the interface */
186 ospf_flood_through_interface(oi, NULL, lsa);
187 }
188
189 /* Flush all self-originated Grace-LSAs. */
190 static void ospf_gr_flush_grace_lsas(struct ospf *ospf)
191 {
192 struct ospf_area *area;
193 struct listnode *anode;
194
195 for (ALL_LIST_ELEMENTS_RO(ospf->areas, anode, area)) {
196 struct ospf_interface *oi;
197 struct listnode *inode;
198
199 if (IS_DEBUG_OSPF_GR)
200 zlog_debug(
201 "GR: flushing self-originated Grace-LSAs [area %pI4]",
202 &area->area_id);
203
204 for (ALL_LIST_ELEMENTS_RO(area->oiflist, inode, oi))
205 ospf_gr_lsa_originate(oi, true);
206 }
207 }
208
209 /* Exit from the Graceful Restart mode. */
210 static void ospf_gr_restart_exit(struct ospf *ospf, const char *reason)
211 {
212 struct ospf_area *area;
213 struct listnode *onode, *anode;
214
215 if (IS_DEBUG_OSPF_GR)
216 zlog_debug("GR: exiting graceful restart: %s", reason);
217
218 ospf->gr_info.restart_in_progress = false;
219 OSPF_TIMER_OFF(ospf->gr_info.t_grace_period);
220
221 /* Record in non-volatile memory that the restart is complete. */
222 ospf_gr_nvm_delete(ospf);
223
224 for (ALL_LIST_ELEMENTS_RO(ospf->areas, onode, area)) {
225 struct ospf_interface *oi;
226
227 /*
228 * 1) The router should reoriginate its router-LSAs for all
229 * attached areas in order to make sure they have the correct
230 * contents.
231 */
232 ospf_router_lsa_update_area(area);
233
234 /*
235 * 2) The router should reoriginate network-LSAs on all segments
236 * where it is the Designated Router.
237 */
238 for (ALL_LIST_ELEMENTS_RO(area->oiflist, anode, oi))
239 if (oi->state == ISM_DR)
240 ospf_network_lsa_update(oi);
241 }
242
243 /*
244 * 5) Any received self-originated LSAs that are no longer valid should
245 * be flushed.
246 */
247 ospf_schedule_abr_task(ospf);
248
249 /*
250 * 3) The router reruns its OSPF routing calculations, this time
251 * installing the results into the system forwarding table, and
252 * originating summary-LSAs, Type-7 LSAs and AS-external-LSAs as
253 * necessary.
254 *
255 * 4) Any remnant entries in the system forwarding table that were
256 * installed before the restart, but that are no longer valid,
257 * should be removed.
258 */
259 ospf->gr_info.finishing_restart = true;
260 ospf_spf_calculate_schedule(ospf, SPF_FLAG_GR_FINISH);
261
262 /* 6) Any grace-LSAs that the router originated should be flushed. */
263 ospf_gr_flush_grace_lsas(ospf);
264 }
265
266 /* Check if a Router-LSA contains a given link. */
267 static bool ospf_router_lsa_contains_adj(struct ospf_lsa *lsa,
268 struct in_addr *id)
269 {
270 struct router_lsa *rl;
271
272 rl = (struct router_lsa *)lsa->data;
273 for (int i = 0; i < ntohs(rl->links); i++) {
274 struct in_addr *link_id = &rl->link[i].link_id;
275
276 if (rl->link[i].type != LSA_LINK_TYPE_POINTOPOINT)
277 continue;
278
279 if (IPV4_ADDR_SAME(id, link_id))
280 return true;
281 }
282
283 return false;
284 }
285
286 static bool ospf_gr_check_router_lsa_consistency(struct ospf *ospf,
287 struct ospf_area *area,
288 struct ospf_lsa *lsa)
289 {
290 if (CHECK_FLAG(lsa->flags, OSPF_LSA_SELF)) {
291 struct ospf_lsa *lsa_self = lsa;
292 struct router_lsa *rl = (struct router_lsa *)lsa->data;
293
294 for (int i = 0; i < ntohs(rl->links); i++) {
295 struct in_addr *link_id = &rl->link[i].link_id;
296 struct ospf_lsa *lsa_adj;
297
298 if (rl->link[i].type != LSA_LINK_TYPE_POINTOPOINT)
299 continue;
300
301 lsa_adj = ospf_lsa_lookup_by_id(area, OSPF_ROUTER_LSA,
302 *link_id);
303 if (!lsa_adj)
304 continue;
305
306 if (!ospf_router_lsa_contains_adj(lsa_adj,
307 &lsa_self->data->id))
308 return false;
309 }
310 } else {
311 struct ospf_lsa *lsa_self;
312
313 lsa_self = ospf_lsa_lookup_by_id(area, OSPF_ROUTER_LSA,
314 ospf->router_id);
315 if (!lsa_self
316 || !CHECK_FLAG(lsa_self->flags, OSPF_LSA_RECEIVED))
317 return true;
318
319 if (ospf_router_lsa_contains_adj(lsa, &ospf->router_id)
320 != ospf_router_lsa_contains_adj(lsa_self, &lsa->data->id))
321 return false;
322 }
323
324 return true;
325 }
326
327 /*
328 * Check for LSAs that are inconsistent with the pre-restart LSAs, and abort the
329 * ongoing graceful restart when that's the case.
330 */
331 void ospf_gr_check_lsdb_consistency(struct ospf *ospf, struct ospf_area *area)
332 {
333 struct route_node *rn;
334 struct ospf_lsa *lsa;
335
336 for (rn = route_top(ROUTER_LSDB(area)); rn; rn = route_next(rn)) {
337 lsa = rn->info;
338 if (!lsa)
339 continue;
340
341 if (!ospf_gr_check_router_lsa_consistency(ospf, area, lsa)) {
342 char reason[256];
343
344 snprintfrr(reason, sizeof(reason),
345 "detected inconsistent LSA[%s] [area %pI4]",
346 dump_lsa_key(lsa), &area->area_id);
347 ospf_gr_restart_exit(ospf, reason);
348 route_unlock_node(rn);
349 return;
350 }
351 }
352 }
353
354 /* Lookup neighbor by address in a given OSPF area. */
355 static struct ospf_neighbor *
356 ospf_area_nbr_lookup_by_addr(struct ospf_area *area, struct in_addr *addr)
357 {
358 struct ospf_interface *oi;
359 struct ospf_neighbor *nbr;
360 struct listnode *node;
361
362 for (ALL_LIST_ELEMENTS_RO(area->oiflist, node, oi)) {
363 nbr = ospf_nbr_lookup_by_addr(oi->nbrs, addr);
364 if (nbr)
365 return nbr;
366 }
367
368 return NULL;
369 }
370
371 /* Lookup neighbor by Router ID in a given OSPF area. */
372 static struct ospf_neighbor *
373 ospf_area_nbr_lookup_by_routerid(struct ospf_area *area, struct in_addr *id)
374 {
375 struct ospf_interface *oi;
376 struct ospf_neighbor *nbr;
377 struct listnode *node;
378
379 for (ALL_LIST_ELEMENTS_RO(area->oiflist, node, oi)) {
380 nbr = ospf_nbr_lookup_by_routerid(oi->nbrs, id);
381 if (nbr)
382 return nbr;
383 }
384
385 return NULL;
386 }
387
388 /* Check if there's a fully formed adjacency with the given neighbor ID. */
389 static bool ospf_gr_check_adj_id(struct ospf_area *area,
390 struct in_addr *nbr_id)
391 {
392 struct ospf_neighbor *nbr;
393
394 nbr = ospf_area_nbr_lookup_by_routerid(area, nbr_id);
395 if (!nbr || nbr->state < NSM_Full) {
396 if (IS_DEBUG_OSPF_GR)
397 zlog_debug("GR: missing adjacency to router %pI4",
398 nbr_id);
399 return false;
400 }
401
402 return true;
403 }
404
405 static bool ospf_gr_check_adjs_lsa_transit(struct ospf_area *area,
406 struct in_addr *link_id)
407 {
408 struct ospf *ospf = area->ospf;
409 struct ospf_interface *oi;
410
411 /*
412 * Check if the transit network refers to a local interface (in which
413 * case it must be a DR for that network).
414 */
415 oi = ospf_if_lookup_by_local_addr(ospf, NULL, *link_id);
416 if (oi) {
417 struct ospf_lsa *lsa;
418 struct network_lsa *nlsa;
419 size_t cnt;
420
421 /* Lookup Network LSA corresponding to this interface. */
422 lsa = ospf_lsa_lookup_by_id(area, OSPF_NETWORK_LSA, *link_id);
423 if (!lsa)
424 return false;
425
426 /* Iterate over all routers present in the network. */
427 nlsa = (struct network_lsa *)lsa->data;
428 cnt = (lsa->size - (OSPF_LSA_HEADER_SIZE + 4)) / 4;
429 for (size_t i = 0; i < cnt; i++) {
430 struct in_addr *nbr_id = &nlsa->routers[i];
431
432 /* Skip self in the pseudonode. */
433 if (IPV4_ADDR_SAME(nbr_id, &ospf->router_id))
434 continue;
435
436 /*
437 * Check if there's a fully formed adjacency with this
438 * router.
439 */
440 if (!ospf_gr_check_adj_id(area, nbr_id))
441 return false;
442 }
443 } else {
444 struct ospf_neighbor *nbr;
445
446 /* Check if there's a fully formed adjacency with the DR. */
447 nbr = ospf_area_nbr_lookup_by_addr(area, link_id);
448 if (!nbr || nbr->state < NSM_Full) {
449 if (IS_DEBUG_OSPF_GR)
450 zlog_debug(
451 "GR: missing adjacency to DR router %pI4",
452 link_id);
453 return false;
454 }
455 }
456
457 return true;
458 }
459
460 static bool ospf_gr_check_adjs_lsa(struct ospf_area *area, struct ospf_lsa *lsa)
461 {
462 struct router_lsa *rl = (struct router_lsa *)lsa->data;
463
464 for (int i = 0; i < ntohs(rl->links); i++) {
465 struct in_addr *link_id = &rl->link[i].link_id;
466
467 switch (rl->link[i].type) {
468 case LSA_LINK_TYPE_POINTOPOINT:
469 if (!ospf_gr_check_adj_id(area, link_id))
470 return false;
471 break;
472 case LSA_LINK_TYPE_TRANSIT:
473 if (!ospf_gr_check_adjs_lsa_transit(area, link_id))
474 return false;
475 break;
476 default:
477 break;
478 }
479 }
480
481 return true;
482 }
483
484 /*
485 * Check if all adjacencies prior to the restart were reestablished.
486 *
487 * This is done using pre-restart Router LSAs and pre-restart Network LSAs
488 * received from the helping neighbors.
489 */
490 void ospf_gr_check_adjs(struct ospf *ospf)
491 {
492 struct ospf_area *area;
493 struct listnode *node;
494
495 for (ALL_LIST_ELEMENTS_RO(ospf->areas, node, area)) {
496 struct ospf_lsa *lsa_self;
497
498 lsa_self = ospf_lsa_lookup_by_id(area, OSPF_ROUTER_LSA,
499 ospf->router_id);
500 if (!lsa_self || !ospf_gr_check_adjs_lsa(area, lsa_self)) {
501 if (IS_DEBUG_OSPF_GR)
502 zlog_debug(
503 "GR: not all adjacencies were reestablished yet [area %pI4]",
504 &area->area_id);
505 return;
506 }
507 }
508
509 ospf_gr_restart_exit(ospf, "all adjacencies were reestablished");
510 }
511
512 /* Handling of grace period expiry. */
513 static void ospf_gr_grace_period_expired(struct thread *thread)
514 {
515 struct ospf *ospf = THREAD_ARG(thread);
516
517 ospf->gr_info.t_grace_period = NULL;
518 ospf_gr_restart_exit(ospf, "grace period has expired");
519 }
520
521 /*
522 * Returns the path of the file (non-volatile memory) that contains GR status
523 * information.
524 */
525 static char *ospf_gr_nvm_filepath(struct ospf *ospf)
526 {
527 static char filepath[MAXPATHLEN];
528 char instance[16] = "";
529
530 if (ospf->instance)
531 snprintf(instance, sizeof(instance), "-%d", ospf->instance);
532 snprintf(filepath, sizeof(filepath), OSPFD_GR_STATE, instance);
533 return filepath;
534 }
535
536 /*
537 * Record in non-volatile memory that the given OSPF instance is attempting to
538 * perform a graceful restart.
539 */
540 static void ospf_gr_nvm_update(struct ospf *ospf)
541 {
542 char *filepath;
543 const char *inst_name;
544 json_object *json;
545 json_object *json_instances;
546 json_object *json_instance;
547
548 filepath = ospf_gr_nvm_filepath(ospf);
549 inst_name = ospf_get_name(ospf);
550
551 json = json_object_from_file(filepath);
552 if (json == NULL)
553 json = json_object_new_object();
554
555 json_object_object_get_ex(json, "instances", &json_instances);
556 if (!json_instances) {
557 json_instances = json_object_new_object();
558 json_object_object_add(json, "instances", json_instances);
559 }
560
561 json_object_object_get_ex(json_instances, inst_name, &json_instance);
562 if (!json_instance) {
563 json_instance = json_object_new_object();
564 json_object_object_add(json_instances, inst_name,
565 json_instance);
566 }
567
568 /*
569 * Record not only the grace period, but also a UNIX timestamp
570 * corresponding to the end of that period. That way, once ospfd is
571 * restarted, it will be possible to take into account the time that
572 * passed while ospfd wasn't running.
573 */
574 json_object_int_add(json_instance, "gracePeriod",
575 ospf->gr_info.grace_period);
576 json_object_int_add(json_instance, "timestamp",
577 time(NULL) + ospf->gr_info.grace_period);
578
579 json_object_to_file_ext(filepath, json, JSON_C_TO_STRING_PRETTY);
580 json_object_free(json);
581 }
582
583 /*
584 * Delete GR status information about the given OSPF instance from non-volatile
585 * memory.
586 */
587 static void ospf_gr_nvm_delete(struct ospf *ospf)
588 {
589 char *filepath;
590 const char *inst_name;
591 json_object *json;
592 json_object *json_instances;
593
594 filepath = ospf_gr_nvm_filepath(ospf);
595 inst_name = ospf_get_name(ospf);
596
597 json = json_object_from_file(filepath);
598 if (json == NULL)
599 json = json_object_new_object();
600
601 json_object_object_get_ex(json, "instances", &json_instances);
602 if (!json_instances) {
603 json_instances = json_object_new_object();
604 json_object_object_add(json, "instances", json_instances);
605 }
606
607 json_object_object_del(json_instances, inst_name);
608
609 json_object_to_file_ext(filepath, json, JSON_C_TO_STRING_PRETTY);
610 json_object_free(json);
611 }
612
613 /*
614 * Fetch from non-volatile memory whether the given OSPF instance is performing
615 * a graceful shutdown or not.
616 */
617 void ospf_gr_nvm_read(struct ospf *ospf)
618 {
619 char *filepath;
620 const char *inst_name;
621 json_object *json;
622 json_object *json_instances;
623 json_object *json_instance;
624 json_object *json_timestamp;
625 time_t timestamp = 0;
626
627 filepath = ospf_gr_nvm_filepath(ospf);
628 inst_name = ospf_get_name(ospf);
629
630 json = json_object_from_file(filepath);
631 if (json == NULL)
632 json = json_object_new_object();
633
634 json_object_object_get_ex(json, "instances", &json_instances);
635 if (!json_instances) {
636 json_instances = json_object_new_object();
637 json_object_object_add(json, "instances", json_instances);
638 }
639
640 json_object_object_get_ex(json_instances, inst_name, &json_instance);
641 if (!json_instance) {
642 json_instance = json_object_new_object();
643 json_object_object_add(json_instances, inst_name,
644 json_instance);
645 }
646
647 json_object_object_get_ex(json_instance, "timestamp", &json_timestamp);
648 if (json_timestamp) {
649 time_t now;
650 unsigned long remaining_time;
651
652 /* Check if the grace period has already expired. */
653 now = time(NULL);
654 timestamp = json_object_get_int(json_timestamp);
655 if (now > timestamp) {
656 ospf_gr_restart_exit(
657 ospf, "grace period has expired already");
658 } else {
659 /* Schedule grace period timeout. */
660 ospf->gr_info.restart_in_progress = true;
661 remaining_time = timestamp - time(NULL);
662 if (IS_DEBUG_OSPF_GR)
663 zlog_debug(
664 "GR: remaining time until grace period expires: %lu(s)",
665 remaining_time);
666 thread_add_timer(master, ospf_gr_grace_period_expired,
667 ospf, remaining_time,
668 &ospf->gr_info.t_grace_period);
669 }
670 }
671
672 json_object_object_del(json_instances, inst_name);
673
674 json_object_to_file_ext(filepath, json, JSON_C_TO_STRING_PRETTY);
675 json_object_free(json);
676 }
677
678 /* Prepare to start a Graceful Restart. */
679 static void ospf_gr_prepare(void)
680 {
681 struct ospf *ospf;
682 struct ospf_interface *oi;
683 struct listnode *onode;
684
685 for (ALL_LIST_ELEMENTS_RO(om->ospf, onode, ospf)) {
686 struct listnode *inode;
687
688 if (!ospf->gr_info.restart_support
689 || ospf->gr_info.prepare_in_progress)
690 continue;
691
692 if (IS_DEBUG_OSPF_GR)
693 zlog_debug(
694 "GR: preparing to perform a graceful restart [period %u second(s)] [vrf %s]",
695 ospf->gr_info.grace_period,
696 ospf_vrf_id_to_name(ospf->vrf_id));
697
698 if (!CHECK_FLAG(ospf->config, OSPF_OPAQUE_CAPABLE)) {
699 zlog_warn(
700 "%s: failed to activate graceful restart: opaque capability not enabled",
701 __func__);
702 continue;
703 }
704
705 /* Freeze OSPF routes in the RIB. */
706 if (ospf_zebra_gr_enable(ospf, ospf->gr_info.grace_period)) {
707 zlog_warn(
708 "%s: failed to activate graceful restart: not connected to zebra",
709 __func__);
710 continue;
711 }
712
713 /* Send a Grace-LSA to all neighbors. */
714 for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, inode, oi))
715 ospf_gr_lsa_originate(oi, false);
716
717 /* Record end of the grace period in non-volatile memory. */
718 ospf_gr_nvm_update(ospf);
719
720 /*
721 * Mark that a Graceful Restart preparation is in progress, to
722 * prevent ospfd from flushing its self-originated LSAs on exit.
723 */
724 ospf->gr_info.prepare_in_progress = true;
725 }
726 }
727
728 DEFPY(graceful_restart_prepare, graceful_restart_prepare_cmd,
729 "graceful-restart prepare ip ospf",
730 "Graceful Restart commands\n"
731 "Prepare upcoming graceful restart\n"
732 IP_STR
733 "Prepare to restart the OSPF process\n")
734 {
735 struct ospf *ospf;
736 struct listnode *node;
737
738 for (ALL_LIST_ELEMENTS_RO(om->ospf, node, ospf)) {
739 if (!CHECK_FLAG(ospf->config, OSPF_OPAQUE_CAPABLE)) {
740 vty_out(vty,
741 "%% Can't start graceful restart: opaque capability not enabled (VRF %s)\n\n",
742 ospf_get_name(ospf));
743 return CMD_WARNING;
744 }
745 }
746
747 ospf_gr_prepare();
748
749 return CMD_SUCCESS;
750 }
751
752 DEFPY(graceful_restart, graceful_restart_cmd,
753 "graceful-restart [grace-period (1-1800)$grace_period]",
754 OSPF_GR_STR
755 "Maximum length of the 'grace period'\n"
756 "Maximum length of the 'grace period' in seconds\n")
757 {
758 VTY_DECLVAR_INSTANCE_CONTEXT(ospf, ospf);
759
760 /* Check and get restart period if present. */
761 if (!grace_period_str)
762 grace_period = OSPF_DFLT_GRACE_INTERVAL;
763
764 ospf->gr_info.restart_support = true;
765 ospf->gr_info.grace_period = grace_period;
766
767 return CMD_SUCCESS;
768 }
769
770 DEFPY(no_graceful_restart, no_graceful_restart_cmd,
771 "no graceful-restart [grace-period (1-1800)]",
772 NO_STR OSPF_GR_STR
773 "Maximum length of the 'grace period'\n"
774 "Maximum length of the 'grace period' in seconds\n")
775 {
776 VTY_DECLVAR_INSTANCE_CONTEXT(ospf, ospf);
777
778 if (!ospf->gr_info.restart_support)
779 return CMD_SUCCESS;
780
781 if (ospf->gr_info.prepare_in_progress) {
782 vty_out(vty,
783 "%% Error: Graceful Restart preparation in progress\n");
784 return CMD_WARNING;
785 }
786
787 ospf->gr_info.restart_support = false;
788 ospf->gr_info.grace_period = OSPF_DFLT_GRACE_INTERVAL;
789
790 return CMD_SUCCESS;
791 }
792
793 void ospf_gr_init(void)
794 {
795 install_element(ENABLE_NODE, &graceful_restart_prepare_cmd);
796 install_element(OSPF_NODE, &graceful_restart_cmd);
797 install_element(OSPF_NODE, &no_graceful_restart_cmd);
798 }