]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_gr.c
ldpd: changes for code maintainability
[mirror_frr.git] / zebra / zebra_gr.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Zebra GR related helper functions.
4 *
5 * Portions:
6 * Copyright (C) 2019 VMware, Inc.
7 * et al.
8 */
9
10 #include <zebra.h>
11 #include <libgen.h>
12
13 #include "lib/prefix.h"
14 #include "lib/command.h"
15 #include "lib/if.h"
16 #include "frrevent.h"
17 #include "lib/stream.h"
18 #include "lib/memory.h"
19 #include "lib/table.h"
20 #include "lib/network.h"
21 #include "lib/sockunion.h"
22 #include "lib/log.h"
23 #include "lib/zclient.h"
24 #include "lib/privs.h"
25 #include "lib/network.h"
26 #include "lib/buffer.h"
27 #include "lib/nexthop.h"
28 #include "lib/vrf.h"
29 #include "lib/libfrr.h"
30 #include "lib/sockopt.h"
31
32 #include "zebra/zebra_router.h"
33 #include "zebra/debug.h"
34 #include "zebra/zapi_msg.h"
35
36 DEFINE_MTYPE_STATIC(ZEBRA, ZEBRA_GR, "GR");
37
38 /*
39 * Forward declaration.
40 */
41 static struct zserv *zebra_gr_find_stale_client(struct zserv *client);
42 static void zebra_gr_route_stale_delete_timer_expiry(struct event *thread);
43 static int32_t zebra_gr_delete_stale_routes(struct client_gr_info *info);
44 static void zebra_gr_process_client_stale_routes(struct zserv *client,
45 vrf_id_t vrf_id);
46
47 /*
48 * Debug macros.
49 */
50 #define LOG_GR(msg, ...) \
51 do { \
52 if (IS_ZEBRA_DEBUG_EVENT) \
53 zlog_debug(msg, ##__VA_ARGS__); \
54 } while (0)
55
56
57 /*
58 * Client connection functions
59 */
60
61 /*
62 * Function to clean all the stale clients,
63 * function will also clean up all per instance
64 * capabilities that are exchanged.
65 */
66 void zebra_gr_stale_client_cleanup(struct list *client_list)
67 {
68 struct listnode *node, *nnode;
69 struct zserv *s_client = NULL;
70 struct client_gr_info *info, *ninfo;
71
72 /* Find the stale client */
73 for (ALL_LIST_ELEMENTS(client_list, node, nnode, s_client)) {
74
75 LOG_GR("%s: Stale client %s is being deleted", __func__,
76 zebra_route_string(s_client->proto));
77
78 TAILQ_FOREACH_SAFE (info, &s_client->gr_info_queue, gr_info,
79 ninfo) {
80
81 /* Cancel the stale timer */
82 if (info->t_stale_removal != NULL) {
83 EVENT_OFF(info->t_stale_removal);
84 info->t_stale_removal = NULL;
85 /* Process the stale routes */
86 event_execute(
87 zrouter.master,
88 zebra_gr_route_stale_delete_timer_expiry,
89 info, 1);
90 }
91 }
92 }
93 }
94
95 /*
96 * A helper function to create client info.
97 */
98 static struct client_gr_info *zebra_gr_client_info_create(struct zserv *client)
99 {
100 struct client_gr_info *info;
101
102 info = XCALLOC(MTYPE_ZEBRA_GR, sizeof(struct client_gr_info));
103
104 TAILQ_INSERT_TAIL(&(client->gr_info_queue), info, gr_info);
105 return info;
106 }
107
108 /*
109 * A helper function to delete and destroy client info.
110 */
111 static void zebra_gr_client_info_delte(struct zserv *client,
112 struct client_gr_info *info)
113 {
114 struct vrf *vrf = vrf_lookup_by_id(info->vrf_id);
115
116 TAILQ_REMOVE(&(client->gr_info_queue), info, gr_info);
117
118 EVENT_OFF(info->t_stale_removal);
119
120 XFREE(MTYPE_ZEBRA_GR, info->current_prefix);
121
122 LOG_GR("%s: Instance info is being deleted for client %s vrf %s(%u)",
123 __func__, zebra_route_string(client->proto), VRF_LOGNAME(vrf),
124 info->vrf_id);
125
126 /* Delete all the stale routes. */
127 info->do_delete = true;
128 zebra_gr_delete_stale_routes(info);
129
130 XFREE(MTYPE_ZEBRA_GR, info);
131 }
132
133 /*
134 * Function to handle client when it disconnect.
135 */
136 int32_t zebra_gr_client_disconnect(struct zserv *client)
137 {
138 struct zserv *stale_client;
139 struct timeval tv;
140 struct client_gr_info *info = NULL;
141
142 /* Find the stale client */
143 stale_client = zebra_gr_find_stale_client(client);
144
145 /*
146 * We should never be here.
147 */
148 if (stale_client) {
149 LOG_GR("%s: Stale client %s exist, we should not be here!",
150 __func__, zebra_route_string(client->proto));
151 assert(0);
152 }
153
154 client->restart_time = monotime(&tv);
155
156 /* For all the GR instance start the stale removal timer. */
157 TAILQ_FOREACH (info, &client->gr_info_queue, gr_info) {
158 if (ZEBRA_CLIENT_GR_ENABLED(info->capabilities)
159 && (info->t_stale_removal == NULL)) {
160 struct vrf *vrf = vrf_lookup_by_id(info->vrf_id);
161
162 event_add_timer(
163 zrouter.master,
164 zebra_gr_route_stale_delete_timer_expiry, info,
165 info->stale_removal_time,
166 &info->t_stale_removal);
167 info->current_afi = AFI_IP;
168 info->stale_client_ptr = client;
169 info->stale_client = true;
170 LOG_GR("%s: Client %s vrf %s(%u) Stale timer update to %d",
171 __func__, zebra_route_string(client->proto),
172 VRF_LOGNAME(vrf), info->vrf_id,
173 info->stale_removal_time);
174 }
175 }
176
177 listnode_add(zrouter.stale_client_list, client);
178
179 return 0;
180 }
181
182 /*
183 * Function to delete stale client
184 */
185 static void zebra_gr_delete_stale_client(struct client_gr_info *info)
186 {
187 struct client_gr_info *bgp_info;
188 struct zserv *s_client = NULL;
189 struct vrf *vrf = vrf_lookup_by_id(info->vrf_id);
190
191 s_client = info->stale_client_ptr;
192
193 if (!s_client || !info->stale_client)
194 return;
195
196 /*
197 * If there are bgp instances with the stale delete timer pending
198 * then stale client is not deleted
199 */
200 if ((s_client->gr_instance_count > 0) && info->gr_enable)
201 s_client->gr_instance_count--;
202
203 TAILQ_REMOVE(&(s_client->gr_info_queue), info, gr_info);
204
205 LOG_GR("%s: Client %s gr count %d", __func__,
206 zebra_route_string(s_client->proto),
207 s_client->gr_instance_count);
208
209 TAILQ_FOREACH (bgp_info, &s_client->gr_info_queue, gr_info) {
210 if (bgp_info->t_stale_removal != NULL)
211 return;
212 }
213
214 LOG_GR("%s: Client %s vrf %s(%u) is being deleted", __func__,
215 zebra_route_string(s_client->proto), VRF_LOGNAME(vrf),
216 info->vrf_id);
217
218 TAILQ_INIT(&(s_client->gr_info_queue));
219 listnode_delete(zrouter.stale_client_list, s_client);
220 if (info->stale_client)
221 zserv_client_delete(s_client);
222 XFREE(MTYPE_ZEBRA_GR, info);
223 }
224
225 /*
226 * Function to find stale client.
227 */
228 static struct zserv *zebra_gr_find_stale_client(struct zserv *client)
229 {
230 struct listnode *node, *nnode;
231 struct zserv *stale_client;
232
233 /* Find the stale client */
234 for (ALL_LIST_ELEMENTS(zrouter.stale_client_list, node, nnode,
235 stale_client)) {
236 if (client->proto == stale_client->proto
237 && client->instance == stale_client->instance) {
238 return stale_client;
239 }
240 }
241
242 return NULL;
243 }
244
245 /*
246 * Function to handle reconnect of client post restart.
247 */
248 void zebra_gr_client_reconnect(struct zserv *client)
249 {
250 struct listnode *node, *nnode;
251 struct zserv *old_client = NULL;
252 struct client_gr_info *info = NULL;
253
254 /* Find the stale client */
255 for (ALL_LIST_ELEMENTS(zrouter.stale_client_list, node, nnode,
256 old_client)) {
257 if (client->proto == old_client->proto
258 && client->instance == old_client->instance)
259 break;
260 }
261
262 /* Copy the timers */
263 if (!old_client)
264 return;
265
266 client->gr_instance_count = old_client->gr_instance_count;
267 client->restart_time = old_client->restart_time;
268
269 LOG_GR("%s : old client %s, gr_instance_count %d", __func__,
270 zebra_route_string(old_client->proto),
271 old_client->gr_instance_count);
272
273 if (TAILQ_FIRST(&old_client->gr_info_queue)) {
274 TAILQ_CONCAT(&client->gr_info_queue, &old_client->gr_info_queue,
275 gr_info);
276 TAILQ_INIT(&old_client->gr_info_queue);
277 }
278
279 TAILQ_FOREACH (info, &client->gr_info_queue, gr_info) {
280 info->stale_client_ptr = client;
281 info->stale_client = false;
282 }
283
284 /* Delete the stale client */
285 listnode_delete(zrouter.stale_client_list, old_client);
286 /* Delete old client */
287 zserv_client_delete(old_client);
288 }
289
290 /*
291 * Functions to deal with capabilities
292 */
293
294 /*
295 * Update the graceful restart information
296 * for the client instance.
297 * This function handles all the capabilities that are received.
298 */
299 static void zebra_client_update_info(struct zserv *client, struct zapi_cap *api)
300 {
301 struct client_gr_info *info = NULL;
302
303 /* Find the bgp information for the specified vrf id */
304 TAILQ_FOREACH (info, &client->gr_info_queue, gr_info) {
305 if (info->vrf_id == api->vrf_id)
306 break;
307 }
308
309
310 /*
311 * If the command is delete, then cancel the stale timer and
312 * delete the bgp info
313 */
314 switch (api->cap) {
315 case ZEBRA_CLIENT_GR_DISABLE:
316 if (!info)
317 return;
318
319 LOG_GR("%s: Client %s instance GR disabled count %d", __func__,
320 zebra_route_string(client->proto),
321 client->gr_instance_count);
322
323 if ((info->gr_enable) && (client->gr_instance_count > 0))
324 client->gr_instance_count--;
325
326 zebra_gr_client_info_delte(client, info);
327 break;
328 case ZEBRA_CLIENT_GR_CAPABILITIES:
329 /* Allocate bgp info */
330 if (!info)
331 info = zebra_gr_client_info_create(client);
332
333 /* Update other parameters */
334 if (!info->gr_enable) {
335 struct vrf *vrf = vrf_lookup_by_id(api->vrf_id);
336
337 client->gr_instance_count++;
338
339 LOG_GR("%s: Cient %s vrf %s(%u) GR enabled count %d",
340 __func__, zebra_route_string(client->proto),
341 VRF_LOGNAME(vrf), api->vrf_id,
342 client->gr_instance_count);
343
344 info->capabilities = api->cap;
345 info->stale_removal_time = api->stale_removal_time;
346 info->vrf_id = api->vrf_id;
347 info->gr_enable = true;
348 }
349 break;
350 case ZEBRA_CLIENT_RIB_STALE_TIME:
351 LOG_GR("%s: Client %s stale time update event", __func__,
352 zebra_route_string(client->proto));
353
354 /* Update the stale removal timer */
355 if (info && info->t_stale_removal == NULL) {
356 struct vrf *vrf = vrf_lookup_by_id(info->vrf_id);
357
358 LOG_GR("%s: vrf %s(%u) Stale time: %d is now update to: %d",
359 __func__, VRF_LOGNAME(vrf), info->vrf_id,
360 info->stale_removal_time,
361 api->stale_removal_time);
362
363 info->stale_removal_time = api->stale_removal_time;
364 }
365
366 break;
367 case ZEBRA_CLIENT_ROUTE_UPDATE_COMPLETE:
368 if (!info) {
369 LOG_GR("%s: Client %s route update complete for AFI %d, SAFI %d",
370 __func__, zebra_route_string(client->proto),
371 api->afi, api->safi);
372 } else {
373 struct vrf *vrf = vrf_lookup_by_id(info->vrf_id);
374
375 LOG_GR("%s: Client %s vrf %s(%u) route update complete for AFI %d, SAFI %d",
376 __func__, zebra_route_string(client->proto),
377 VRF_LOGNAME(vrf), info->vrf_id, api->afi,
378 api->safi);
379 info->route_sync[api->afi][api->safi] = true;
380 }
381 break;
382 case ZEBRA_CLIENT_ROUTE_UPDATE_PENDING:
383 if (!info) {
384 LOG_GR("%s: Client %s route update pending for AFI %d, SAFI %d",
385 __func__, zebra_route_string(client->proto),
386 api->afi, api->safi);
387 } else {
388 struct vrf *vrf = vrf_lookup_by_id(info->vrf_id);
389
390 LOG_GR("%s: Client %s vrf %s(%u) route update pending for AFI %d, SAFI %d",
391 __func__, zebra_route_string(client->proto),
392 VRF_LOGNAME(vrf), info->vrf_id, api->afi,
393 api->safi);
394
395 info->af_enabled[api->afi][api->safi] = true;
396 }
397 break;
398 }
399 }
400
401 /*
402 * Handler for capabilities that are received from client.
403 */
404 static void zebra_client_capabilities_handler(struct zserv *client,
405 struct zapi_cap *api)
406 {
407 switch (api->cap) {
408 case ZEBRA_CLIENT_GR_CAPABILITIES:
409 case ZEBRA_CLIENT_ROUTE_UPDATE_PENDING:
410 case ZEBRA_CLIENT_GR_DISABLE:
411 case ZEBRA_CLIENT_RIB_STALE_TIME:
412 /*
413 * For all the cases we need to update the client info.
414 */
415 zebra_client_update_info(client, api);
416 break;
417 case ZEBRA_CLIENT_ROUTE_UPDATE_COMPLETE:
418 /*
419 * After client info has been updated delete all
420 * stale routes
421 */
422 zebra_client_update_info(client, api);
423 zebra_gr_process_client_stale_routes(client, api->vrf_id);
424 break;
425 }
426 }
427
428 /*
429 * Function to decode and call appropriate functions
430 * to handle client capabilities.
431 */
432 void zread_client_capabilities(ZAPI_HANDLER_ARGS)
433 {
434 struct zapi_cap api;
435 struct stream *s;
436
437 s = msg;
438
439 if (zapi_capabilities_decode(s, &api)) {
440 LOG_GR("%s: Error in reading capabilities for client %s",
441 __func__, zebra_route_string(client->proto));
442 return;
443 }
444
445 /* GR only for dynamic clients */
446 if (client->proto <= ZEBRA_ROUTE_CONNECT) {
447 LOG_GR("%s: GR capabilities for client %s not supported",
448 __func__, zebra_route_string(client->proto));
449 return;
450 }
451 /* Call the capabilities handler */
452 zebra_client_capabilities_handler(client, &api);
453 }
454
455
456 /*
457 * Stale route handling
458 */
459
460 /*
461 * Delete all the stale routes that have not been refreshed
462 * post restart.
463 */
464 static void zebra_gr_route_stale_delete_timer_expiry(struct event *thread)
465 {
466 struct client_gr_info *info = EVENT_ARG(thread);
467 int32_t cnt = 0;
468 struct zserv *client;
469 struct vrf *vrf = vrf_lookup_by_id(info->vrf_id);
470
471 client = (struct zserv *)info->stale_client_ptr;
472
473 /* Set the flag to indicate all stale route deletion */
474 if (thread->u.val == 1)
475 info->do_delete = true;
476
477 cnt = zebra_gr_delete_stale_routes(info);
478
479 /* Restart the timer */
480 if (cnt > 0) {
481 LOG_GR("%s: Client %s vrf %s(%u) processed %d routes. Start timer again",
482 __func__, zebra_route_string(client->proto),
483 VRF_LOGNAME(vrf), info->vrf_id, cnt);
484
485 event_add_timer(zrouter.master,
486 zebra_gr_route_stale_delete_timer_expiry, info,
487 ZEBRA_DEFAULT_STALE_UPDATE_DELAY,
488 &info->t_stale_removal);
489 } else {
490 /* No routes to delete for the VRF */
491 LOG_GR("%s: Client %s vrf %s(%u) all stale routes processed",
492 __func__, zebra_route_string(client->proto),
493 VRF_LOGNAME(vrf), info->vrf_id);
494
495 XFREE(MTYPE_ZEBRA_GR, info->current_prefix);
496 info->current_afi = 0;
497 zebra_gr_delete_stale_client(info);
498 }
499 }
500
501
502 /*
503 * Function to process to check if route entry is stale
504 * or has been updated.
505 */
506 static void zebra_gr_process_route_entry(struct zserv *client,
507 struct route_node *rn,
508 struct route_entry *re)
509 {
510 if ((client == NULL) || (rn == NULL) || (re == NULL))
511 return;
512
513 /* If the route is not refreshed after restart, delete the entry */
514 if (re->uptime < client->restart_time) {
515 if (IS_ZEBRA_DEBUG_RIB)
516 zlog_debug("%s: Client %s stale route %pFX is deleted",
517 __func__, zebra_route_string(client->proto),
518 &rn->p);
519 rib_delnode(rn, re);
520 }
521 }
522
523 /*
524 * This function walks through the route table for all vrf and deletes
525 * the stale routes for the restarted client specified by the protocol
526 * type
527 */
528 static int32_t zebra_gr_delete_stale_route(struct client_gr_info *info,
529 struct zebra_vrf *zvrf)
530 {
531 struct route_node *rn, *curr;
532 struct route_entry *re;
533 struct route_entry *next;
534 struct route_table *table;
535 int32_t n = 0;
536 afi_t afi, curr_afi;
537 uint8_t proto;
538 uint16_t instance;
539 struct zserv *s_client;
540
541 if ((info == NULL) || (zvrf == NULL))
542 return -1;
543
544 s_client = info->stale_client_ptr;
545 if (s_client == NULL) {
546 LOG_GR("%s: Stale client %s(%u) not present", __func__,
547 zvrf->vrf->name, zvrf->vrf->vrf_id);
548 return -1;
549 }
550
551 proto = s_client->proto;
552 instance = s_client->instance;
553 curr_afi = info->current_afi;
554
555 LOG_GR("%s: Client %s %s(%u) stale routes are being deleted", __func__,
556 zebra_route_string(proto), zvrf->vrf->name, zvrf->vrf->vrf_id);
557
558 /* Process routes for all AFI */
559 for (afi = curr_afi; afi < AFI_MAX; afi++) {
560 table = zvrf->table[afi][SAFI_UNICAST];
561
562 if (table) {
563 /*
564 * If the current prefix is NULL then get the first
565 * route entry in the table
566 */
567 if (info->current_prefix == NULL) {
568 rn = route_top(table);
569 if (rn == NULL)
570 continue;
571 curr = rn;
572 } else
573 /* Get the next route entry */
574 curr = route_table_get_next(
575 table, info->current_prefix);
576
577 for (rn = curr; rn; rn = srcdest_route_next(rn)) {
578 RNODE_FOREACH_RE_SAFE (rn, re, next) {
579 if (CHECK_FLAG(re->status,
580 ROUTE_ENTRY_REMOVED))
581 continue;
582 /* If the route refresh is received
583 * after restart then do not delete
584 * the route
585 */
586 if (re->type == proto
587 && re->instance == instance) {
588 zebra_gr_process_route_entry(
589 s_client, rn, re);
590 n++;
591 }
592
593 /* If the max route count is reached
594 * then timer thread will be restarted
595 * Store the current prefix and afi
596 */
597 if ((n >= ZEBRA_MAX_STALE_ROUTE_COUNT)
598 && (info->do_delete == false)) {
599 info->current_afi = afi;
600 info->current_prefix = XCALLOC(
601 MTYPE_ZEBRA_GR,
602 sizeof(struct prefix));
603 prefix_copy(
604 info->current_prefix,
605 &rn->p);
606 return n;
607 }
608 }
609 }
610 }
611 /*
612 * Reset the current prefix to indicate processing completion
613 * of the current AFI
614 */
615 XFREE(MTYPE_ZEBRA_GR, info->current_prefix);
616 }
617 return 0;
618 }
619
620 /*
621 * Delete the stale routes when client is restarted and routes are not
622 * refreshed within the stale timeout
623 */
624 static int32_t zebra_gr_delete_stale_routes(struct client_gr_info *info)
625 {
626 struct vrf *vrf;
627 struct zebra_vrf *zvrf;
628 uint64_t cnt = 0;
629
630 if (info == NULL)
631 return -1;
632
633 /* Get the current VRF */
634 vrf = vrf_lookup_by_id(info->vrf_id);
635 if (vrf == NULL) {
636 LOG_GR("%s: Invalid VRF specified %u", __func__, info->vrf_id);
637 return -1;
638 }
639
640 zvrf = vrf->info;
641 if (zvrf == NULL) {
642 LOG_GR("%s: Invalid VRF entry %u", __func__, info->vrf_id);
643 return -1;
644 }
645
646 cnt = zebra_gr_delete_stale_route(info, zvrf);
647 return cnt;
648 }
649
650 /*
651 * This function checks if route update for all AFI, SAFI is completed
652 * and cancels the stale timer
653 */
654 static void zebra_gr_process_client_stale_routes(struct zserv *client,
655 vrf_id_t vrf_id)
656 {
657 struct client_gr_info *info = NULL;
658 afi_t afi;
659 safi_t safi;
660
661 TAILQ_FOREACH (info, &client->gr_info_queue, gr_info) {
662 if (info->vrf_id == vrf_id)
663 break;
664 }
665
666 if (info == NULL)
667 return;
668
669 /* Check if route update completed for all AFI, SAFI */
670 FOREACH_AFI_SAFI_NSF (afi, safi) {
671 if (info->af_enabled[afi][safi]) {
672 if (!info->route_sync[afi][safi]) {
673 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
674
675 LOG_GR("%s: Client %s vrf: %s(%u) route update not completed for AFI %d, SAFI %d",
676 __func__,
677 zebra_route_string(client->proto),
678 VRF_LOGNAME(vrf), info->vrf_id, afi,
679 safi);
680 return;
681 }
682 }
683 }
684
685 /*
686 * Route update completed for all AFI, SAFI
687 * Cancel the stale timer and process the routes
688 */
689 if (info->t_stale_removal) {
690 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
691
692 LOG_GR("%s: Client %s canceled stale delete timer vrf %s(%d)",
693 __func__, zebra_route_string(client->proto),
694 VRF_LOGNAME(vrf), info->vrf_id);
695 EVENT_OFF(info->t_stale_removal);
696 event_execute(zrouter.master,
697 zebra_gr_route_stale_delete_timer_expiry, info,
698 0);
699 }
700 }