1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Zebra GR related helper functions.
6 * Copyright (C) 2019 VMware, Inc.
13 #include "lib/prefix.h"
14 #include "lib/command.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"
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"
29 #include "lib/libfrr.h"
30 #include "lib/sockopt.h"
32 #include "zebra/zebra_router.h"
33 #include "zebra/debug.h"
34 #include "zebra/zapi_msg.h"
36 DEFINE_MTYPE_STATIC(ZEBRA
, ZEBRA_GR
, "GR");
39 * Forward declaration.
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
,
50 #define LOG_GR(msg, ...) \
52 if (IS_ZEBRA_DEBUG_EVENT) \
53 zlog_debug(msg, ##__VA_ARGS__); \
58 * Client connection functions
62 * Function to clean all the stale clients,
63 * function will also clean up all per instance
64 * capabilities that are exchanged.
66 void zebra_gr_stale_client_cleanup(struct list
*client_list
)
68 struct listnode
*node
, *nnode
;
69 struct zserv
*s_client
= NULL
;
70 struct client_gr_info
*info
, *ninfo
;
72 /* Find the stale client */
73 for (ALL_LIST_ELEMENTS(client_list
, node
, nnode
, s_client
)) {
75 LOG_GR("%s: Stale client %s is being deleted", __func__
,
76 zebra_route_string(s_client
->proto
));
78 TAILQ_FOREACH_SAFE (info
, &s_client
->gr_info_queue
, gr_info
,
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 */
88 zebra_gr_route_stale_delete_timer_expiry
,
96 * A helper function to create client info.
98 static struct client_gr_info
*zebra_gr_client_info_create(struct zserv
*client
)
100 struct client_gr_info
*info
;
102 info
= XCALLOC(MTYPE_ZEBRA_GR
, sizeof(struct client_gr_info
));
104 TAILQ_INSERT_TAIL(&(client
->gr_info_queue
), info
, gr_info
);
109 * A helper function to delete and destroy client info.
111 static void zebra_gr_client_info_delte(struct zserv
*client
,
112 struct client_gr_info
*info
)
114 struct vrf
*vrf
= vrf_lookup_by_id(info
->vrf_id
);
116 TAILQ_REMOVE(&(client
->gr_info_queue
), info
, gr_info
);
118 EVENT_OFF(info
->t_stale_removal
);
120 XFREE(MTYPE_ZEBRA_GR
, info
->current_prefix
);
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
),
126 /* Delete all the stale routes. */
127 info
->do_delete
= true;
128 zebra_gr_delete_stale_routes(info
);
130 XFREE(MTYPE_ZEBRA_GR
, info
);
134 * Function to handle client when it disconnect.
136 int32_t zebra_gr_client_disconnect(struct zserv
*client
)
138 struct zserv
*stale_client
;
140 struct client_gr_info
*info
= NULL
;
142 /* Find the stale client */
143 stale_client
= zebra_gr_find_stale_client(client
);
146 * We should never be here.
149 LOG_GR("%s: Stale client %s exist, we should not be here!",
150 __func__
, zebra_route_string(client
->proto
));
154 client
->restart_time
= monotime(&tv
);
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
);
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
);
177 listnode_add(zrouter
.stale_client_list
, client
);
183 * Function to delete stale client
185 static void zebra_gr_delete_stale_client(struct client_gr_info
*info
)
187 struct client_gr_info
*bgp_info
;
188 struct zserv
*s_client
= NULL
;
189 struct vrf
*vrf
= vrf_lookup_by_id(info
->vrf_id
);
191 s_client
= info
->stale_client_ptr
;
193 if (!s_client
|| !info
->stale_client
)
197 * If there are bgp instances with the stale delete timer pending
198 * then stale client is not deleted
200 if ((s_client
->gr_instance_count
> 0) && info
->gr_enable
)
201 s_client
->gr_instance_count
--;
203 TAILQ_REMOVE(&(s_client
->gr_info_queue
), info
, gr_info
);
205 LOG_GR("%s: Client %s gr count %d", __func__
,
206 zebra_route_string(s_client
->proto
),
207 s_client
->gr_instance_count
);
209 TAILQ_FOREACH (bgp_info
, &s_client
->gr_info_queue
, gr_info
) {
210 if (bgp_info
->t_stale_removal
!= NULL
)
214 LOG_GR("%s: Client %s vrf %s(%u) is being deleted", __func__
,
215 zebra_route_string(s_client
->proto
), VRF_LOGNAME(vrf
),
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
);
226 * Function to find stale client.
228 static struct zserv
*zebra_gr_find_stale_client(struct zserv
*client
)
230 struct listnode
*node
, *nnode
;
231 struct zserv
*stale_client
;
233 /* Find the stale client */
234 for (ALL_LIST_ELEMENTS(zrouter
.stale_client_list
, node
, nnode
,
236 if (client
->proto
== stale_client
->proto
237 && client
->instance
== stale_client
->instance
) {
246 * Function to handle reconnect of client post restart.
248 void zebra_gr_client_reconnect(struct zserv
*client
)
250 struct listnode
*node
, *nnode
;
251 struct zserv
*old_client
= NULL
;
252 struct client_gr_info
*info
= NULL
;
254 /* Find the stale client */
255 for (ALL_LIST_ELEMENTS(zrouter
.stale_client_list
, node
, nnode
,
257 if (client
->proto
== old_client
->proto
258 && client
->instance
== old_client
->instance
)
262 /* Copy the timers */
266 client
->gr_instance_count
= old_client
->gr_instance_count
;
267 client
->restart_time
= old_client
->restart_time
;
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
);
273 if (TAILQ_FIRST(&old_client
->gr_info_queue
)) {
274 TAILQ_CONCAT(&client
->gr_info_queue
, &old_client
->gr_info_queue
,
276 TAILQ_INIT(&old_client
->gr_info_queue
);
279 TAILQ_FOREACH (info
, &client
->gr_info_queue
, gr_info
) {
280 info
->stale_client_ptr
= client
;
281 info
->stale_client
= false;
284 /* Delete the stale client */
285 listnode_delete(zrouter
.stale_client_list
, old_client
);
286 /* Delete old client */
287 zserv_client_delete(old_client
);
291 * Functions to deal with capabilities
295 * Update the graceful restart information
296 * for the client instance.
297 * This function handles all the capabilities that are received.
299 static void zebra_client_update_info(struct zserv
*client
, struct zapi_cap
*api
)
301 struct client_gr_info
*info
= NULL
;
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
)
311 * If the command is delete, then cancel the stale timer and
312 * delete the bgp info
315 case ZEBRA_CLIENT_GR_DISABLE
:
319 LOG_GR("%s: Client %s instance GR disabled count %d", __func__
,
320 zebra_route_string(client
->proto
),
321 client
->gr_instance_count
);
323 if ((info
->gr_enable
) && (client
->gr_instance_count
> 0))
324 client
->gr_instance_count
--;
326 zebra_gr_client_info_delte(client
, info
);
328 case ZEBRA_CLIENT_GR_CAPABILITIES
:
329 /* Allocate bgp info */
331 info
= zebra_gr_client_info_create(client
);
333 /* Update other parameters */
334 if (!info
->gr_enable
) {
335 struct vrf
*vrf
= vrf_lookup_by_id(api
->vrf_id
);
337 client
->gr_instance_count
++;
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
);
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;
350 case ZEBRA_CLIENT_RIB_STALE_TIME
:
351 LOG_GR("%s: Client %s stale time update event", __func__
,
352 zebra_route_string(client
->proto
));
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
);
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
);
363 info
->stale_removal_time
= api
->stale_removal_time
;
367 case ZEBRA_CLIENT_ROUTE_UPDATE_COMPLETE
:
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
);
373 struct vrf
*vrf
= vrf_lookup_by_id(info
->vrf_id
);
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
,
379 info
->route_sync
[api
->afi
][api
->safi
] = true;
382 case ZEBRA_CLIENT_ROUTE_UPDATE_PENDING
:
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
);
388 struct vrf
*vrf
= vrf_lookup_by_id(info
->vrf_id
);
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
,
395 info
->af_enabled
[api
->afi
][api
->safi
] = true;
402 * Handler for capabilities that are received from client.
404 static void zebra_client_capabilities_handler(struct zserv
*client
,
405 struct zapi_cap
*api
)
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
:
413 * For all the cases we need to update the client info.
415 zebra_client_update_info(client
, api
);
417 case ZEBRA_CLIENT_ROUTE_UPDATE_COMPLETE
:
419 * After client info has been updated delete all
422 zebra_client_update_info(client
, api
);
423 zebra_gr_process_client_stale_routes(client
, api
->vrf_id
);
429 * Function to decode and call appropriate functions
430 * to handle client capabilities.
432 void zread_client_capabilities(ZAPI_HANDLER_ARGS
)
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
));
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
));
451 /* Call the capabilities handler */
452 zebra_client_capabilities_handler(client
, &api
);
457 * Stale route handling
461 * Delete all the stale routes that have not been refreshed
464 static void zebra_gr_route_stale_delete_timer_expiry(struct event
*thread
)
466 struct client_gr_info
*info
= EVENT_ARG(thread
);
468 struct zserv
*client
;
469 struct vrf
*vrf
= vrf_lookup_by_id(info
->vrf_id
);
471 client
= (struct zserv
*)info
->stale_client_ptr
;
473 /* Set the flag to indicate all stale route deletion */
474 if (thread
->u
.val
== 1)
475 info
->do_delete
= true;
477 cnt
= zebra_gr_delete_stale_routes(info
);
479 /* Restart the timer */
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
);
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
);
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
);
495 XFREE(MTYPE_ZEBRA_GR
, info
->current_prefix
);
496 info
->current_afi
= 0;
497 zebra_gr_delete_stale_client(info
);
503 * Function to process to check if route entry is stale
504 * or has been updated.
506 static void zebra_gr_process_route_entry(struct zserv
*client
,
507 struct route_node
*rn
,
508 struct route_entry
*re
)
510 if ((client
== NULL
) || (rn
== NULL
) || (re
== NULL
))
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
),
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
528 static int32_t zebra_gr_delete_stale_route(struct client_gr_info
*info
,
529 struct zebra_vrf
*zvrf
)
531 struct route_node
*rn
, *curr
;
532 struct route_entry
*re
;
533 struct route_entry
*next
;
534 struct route_table
*table
;
539 struct zserv
*s_client
;
541 if ((info
== NULL
) || (zvrf
== NULL
))
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
);
551 proto
= s_client
->proto
;
552 instance
= s_client
->instance
;
553 curr_afi
= info
->current_afi
;
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
);
558 /* Process routes for all AFI */
559 for (afi
= curr_afi
; afi
< AFI_MAX
; afi
++) {
560 table
= zvrf
->table
[afi
][SAFI_UNICAST
];
564 * If the current prefix is NULL then get the first
565 * route entry in the table
567 if (info
->current_prefix
== NULL
) {
568 rn
= route_top(table
);
573 /* Get the next route entry */
574 curr
= route_table_get_next(
575 table
, info
->current_prefix
);
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
))
582 /* If the route refresh is received
583 * after restart then do not delete
586 if (re
->type
== proto
587 && re
->instance
== instance
) {
588 zebra_gr_process_route_entry(
593 /* If the max route count is reached
594 * then timer thread will be restarted
595 * Store the current prefix and afi
597 if ((n
>= ZEBRA_MAX_STALE_ROUTE_COUNT
)
598 && (info
->do_delete
== false)) {
599 info
->current_afi
= afi
;
600 info
->current_prefix
= XCALLOC(
602 sizeof(struct prefix
));
604 info
->current_prefix
,
612 * Reset the current prefix to indicate processing completion
615 XFREE(MTYPE_ZEBRA_GR
, info
->current_prefix
);
621 * Delete the stale routes when client is restarted and routes are not
622 * refreshed within the stale timeout
624 static int32_t zebra_gr_delete_stale_routes(struct client_gr_info
*info
)
627 struct zebra_vrf
*zvrf
;
633 /* Get the current VRF */
634 vrf
= vrf_lookup_by_id(info
->vrf_id
);
636 LOG_GR("%s: Invalid VRF specified %u", __func__
, info
->vrf_id
);
642 LOG_GR("%s: Invalid VRF entry %u", __func__
, info
->vrf_id
);
646 cnt
= zebra_gr_delete_stale_route(info
, zvrf
);
651 * This function checks if route update for all AFI, SAFI is completed
652 * and cancels the stale timer
654 static void zebra_gr_process_client_stale_routes(struct zserv
*client
,
657 struct client_gr_info
*info
= NULL
;
661 TAILQ_FOREACH (info
, &client
->gr_info_queue
, gr_info
) {
662 if (info
->vrf_id
== vrf_id
)
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
);
675 LOG_GR("%s: Client %s vrf: %s(%u) route update not completed for AFI %d, SAFI %d",
677 zebra_route_string(client
->proto
),
678 VRF_LOGNAME(vrf
), info
->vrf_id
, afi
,
686 * Route update completed for all AFI, SAFI
687 * Cancel the stale timer and process the routes
689 if (info
->t_stale_removal
) {
690 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
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
,