2 * Zebra GR related helper functions.
5 * Copyright (C) 2019 VMware, Inc.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * You should have received a copy of the GNU General Public License along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "lib/prefix.h"
27 #include "lib/command.h"
29 #include "lib/thread.h"
30 #include "lib/stream.h"
31 #include "lib/memory.h"
32 #include "lib/table.h"
33 #include "lib/network.h"
34 #include "lib/sockunion.h"
36 #include "lib/zclient.h"
37 #include "lib/privs.h"
38 #include "lib/network.h"
39 #include "lib/buffer.h"
40 #include "lib/nexthop.h"
42 #include "lib/libfrr.h"
43 #include "lib/sockopt.h"
45 #include "zebra/zebra_router.h"
46 #include "zebra/debug.h"
47 #include "zebra/zapi_msg.h"
51 * Forward declaration.
53 static struct zserv
*zebra_gr_find_stale_client(struct zserv
*client
);
54 static int32_t zebra_gr_route_stale_delete_timer_expiry(struct thread
*thread
);
55 static int32_t zebra_gr_delete_stale_routes(struct client_gr_info
*info
);
56 static void zebra_gr_process_client_stale_routes(struct zserv
*client
,
62 #define LOG_GR(msg, ...) \
64 if (IS_ZEBRA_DEBUG_EVENT) \
65 zlog_debug(msg, ##__VA_ARGS__); \
70 * Client connection functions
74 * Function to clean all the stale clients,
75 * function will also clean up all per instance
76 * capabilities that are exchanged.
78 void zebra_gr_stale_client_cleanup(struct list
*client_list
)
80 struct listnode
*node
, *nnode
;
81 struct zserv
*s_client
= NULL
;
82 struct client_gr_info
*info
, *ninfo
;
84 /* Find the stale client */
85 for (ALL_LIST_ELEMENTS(client_list
, node
, nnode
, s_client
)) {
87 LOG_GR("%s: Stale client %s is being deleted", __func__
,
88 zebra_route_string(s_client
->proto
));
90 TAILQ_FOREACH_SAFE (info
, &s_client
->gr_info_queue
, gr_info
,
93 /* Cancel the stale timer */
94 if (info
->t_stale_removal
!= NULL
) {
95 THREAD_OFF(info
->t_stale_removal
);
96 info
->t_stale_removal
= NULL
;
97 /* Process the stale routes */
100 zebra_gr_route_stale_delete_timer_expiry
,
108 * A helper function to create client info.
110 static struct client_gr_info
*zebra_gr_client_info_create(struct zserv
*client
)
112 struct client_gr_info
*info
;
114 info
= XCALLOC(MTYPE_TMP
, sizeof(struct client_gr_info
));
116 TAILQ_INSERT_TAIL(&(client
->gr_info_queue
), info
, gr_info
);
121 * A helper function to delte and destory client info.
123 static void zebra_gr_client_info_delte(struct zserv
*client
,
124 struct client_gr_info
*info
)
126 TAILQ_REMOVE(&(client
->gr_info_queue
), info
, gr_info
);
128 THREAD_OFF(info
->t_stale_removal
);
130 if (info
->current_prefix
)
131 XFREE(MTYPE_TMP
, info
->current_prefix
);
133 LOG_GR("%s: Instance info is being deleted for client %s", __func__
,
134 zebra_route_string(client
->proto
));
136 /* Delete all the stale routes. */
138 zebra_gr_delete_stale_routes(info
);
140 XFREE(MTYPE_TMP
, info
);
144 * Function to handle client when it disconnect.
146 int32_t zebra_gr_client_disconnect(struct zserv
*client
)
148 struct zserv
*stale_client
;
150 struct client_gr_info
*info
= NULL
;
152 /* Find the stale client */
153 stale_client
= zebra_gr_find_stale_client(client
);
156 * We should never be here.
159 LOG_GR("%s: Stale client %s exist, we should not be here!",
160 __func__
, zebra_route_string(client
->proto
));
164 client
->restart_time
= monotime(&tv
);
166 /* For all the GR instance start the starle removal timer. */
167 TAILQ_FOREACH (info
, &client
->gr_info_queue
, gr_info
) {
168 if (ZEBRA_CLIENT_GR_ENABLED(info
->capabilities
)
169 && (info
->t_stale_removal
== NULL
)) {
172 zebra_gr_route_stale_delete_timer_expiry
, info
,
173 info
->stale_removal_time
,
174 &info
->t_stale_removal
);
175 info
->current_afi
= AFI_IP
;
176 info
->stale_client_ptr
= client
;
177 info
->stale_client
= true;
178 LOG_GR("%s: Client %s Stale timer update to %d",
179 __func__
, zebra_route_string(client
->proto
),
180 info
->stale_removal_time
);
184 listnode_add(zrouter
.stale_client_list
, client
);
190 * Function to delete stale client
192 static void zebra_gr_delete_stale_client(struct client_gr_info
*info
)
194 struct client_gr_info
*bgp_info
;
195 struct zserv
*s_client
= NULL
;
197 s_client
= info
->stale_client_ptr
;
199 if (!s_client
|| !info
->stale_client
)
203 * If there are bgp instances with the stale delete timer pending
204 * then stale client is not deleted
206 if ((s_client
->gr_instance_count
> 0) && info
->gr_enable
)
207 s_client
->gr_instance_count
--;
209 TAILQ_REMOVE(&(s_client
->gr_info_queue
), info
, gr_info
);
211 LOG_GR("%s: Client %s gr count %d", __func__
,
212 zebra_route_string(s_client
->proto
),
213 s_client
->gr_instance_count
);
215 TAILQ_FOREACH (bgp_info
, &s_client
->gr_info_queue
, gr_info
) {
216 if (bgp_info
->t_stale_removal
!= NULL
)
220 LOG_GR("%s: Client %s is being deleted", __func__
,
221 zebra_route_string(s_client
->proto
));
223 TAILQ_INIT(&(s_client
->gr_info_queue
));
224 listnode_delete(zrouter
.stale_client_list
, s_client
);
225 if (info
->stale_client
)
226 XFREE(MTYPE_TMP
, s_client
);
227 XFREE(MTYPE_TMP
, info
);
231 * Function to find stale client.
233 static struct zserv
*zebra_gr_find_stale_client(struct zserv
*client
)
235 struct listnode
*node
, *nnode
;
236 struct zserv
*stale_client
;
238 /* Find the stale client */
239 for (ALL_LIST_ELEMENTS(zrouter
.stale_client_list
, node
, nnode
,
241 if (client
->proto
== stale_client
->proto
242 && client
->instance
== stale_client
->instance
) {
251 * Function to handle reconnect of client post restart.
253 void zebra_gr_client_reconnect(struct zserv
*client
)
255 struct listnode
*node
, *nnode
;
256 struct zserv
*old_client
= NULL
;
257 struct client_gr_info
*info
= NULL
;
259 /* Find the stale client */
260 for (ALL_LIST_ELEMENTS(zrouter
.stale_client_list
, node
, nnode
,
262 if (client
->proto
== old_client
->proto
263 && client
->instance
== old_client
->instance
)
267 /* Copy the timers */
269 client
->gr_instance_count
= old_client
->gr_instance_count
;
270 client
->restart_time
= old_client
->restart_time
;
272 LOG_GR("%s : old client %s, gr_instance_count %d", __func__
,
273 zebra_route_string(old_client
->proto
),
274 old_client
->gr_instance_count
);
276 if (TAILQ_FIRST(&old_client
->gr_info_queue
)) {
277 TAILQ_CONCAT(&client
->gr_info_queue
,
278 &old_client
->gr_info_queue
, gr_info
);
279 TAILQ_INIT(&old_client
->gr_info_queue
);
282 TAILQ_FOREACH (info
, &client
->gr_info_queue
, gr_info
) {
283 info
->stale_client_ptr
= client
;
284 info
->stale_client
= false;
287 /* Delete the stale client */
288 listnode_delete(zrouter
.stale_client_list
, old_client
);
289 /* Delete old client */
290 XFREE(MTYPE_TMP
, old_client
);
295 * Functions to deal with capabilities
299 * Update the graceful restart information
300 * for the client instance.
301 * This function handles all the capabilties that are received.
303 static void zebra_client_update_info(struct zserv
*client
, struct zapi_cap
*api
)
305 struct client_gr_info
*info
= NULL
;
307 /* Find the bgp information for the specified vrf id */
308 TAILQ_FOREACH (info
, &client
->gr_info_queue
, gr_info
) {
309 if (info
->vrf_id
== api
->vrf_id
)
315 * If the command is delete, then cancel the stale timer and
316 * delete the bgp info
319 case ZEBRA_CLIENT_GR_DISABLE
:
323 LOG_GR("%s: Client %s instance GR disabled count %d", __func__
,
324 zebra_route_string(client
->proto
),
325 client
->gr_instance_count
);
327 if ((info
->gr_enable
) && (client
->gr_instance_count
> 0))
328 client
->gr_instance_count
--;
330 zebra_gr_client_info_delte(client
, info
);
332 case ZEBRA_CLIENT_GR_CAPABILITIES
:
333 /* Allocate bgp info */
335 info
= zebra_gr_client_info_create(client
);
337 /* Udpate other parameters */
338 if (!info
->gr_enable
) {
339 client
->gr_instance_count
++;
341 LOG_GR("%s: Cient %s GR enabled count %d", __func__
,
342 zebra_route_string(client
->proto
),
343 client
->gr_instance_count
);
345 info
->capabilities
= api
->cap
;
346 info
->stale_removal_time
= api
->stale_removal_time
;
347 info
->vrf_id
= api
->vrf_id
;
348 info
->gr_enable
= true;
351 case ZEBRA_CLIENT_RIB_STALE_TIME
:
352 LOG_GR("%s: Client %s stale time update event", __func__
,
353 zebra_route_string(client
->proto
));
355 /* Update the stale removal timer */
356 if (info
&& info
->t_stale_removal
== NULL
) {
358 LOG_GR("%s: Stale time: %d is now update to: %d",
359 __func__
, info
->stale_removal_time
,
360 api
->stale_removal_time
);
362 info
->stale_removal_time
= api
->stale_removal_time
;
366 case ZEBRA_CLIENT_ROUTE_UPDATE_COMPLETE
:
368 "%s: Client %s route update complete for AFI %d, SAFI %d",
369 __func__
, zebra_route_string(client
->proto
), api
->afi
,
372 info
->route_sync
[api
->afi
][api
->safi
] = true;
374 case ZEBRA_CLIENT_ROUTE_UPDATE_PENDING
:
375 LOG_GR("%s: Client %s route update pending for AFI %d, SAFI %d",
376 __func__
, zebra_route_string(client
->proto
), api
->afi
,
379 info
->af_enabled
[api
->afi
][api
->safi
] = true;
385 * Handler for capabilities that are received from client.
387 static void zebra_client_capabilities_handler(struct zserv
*client
,
388 struct zapi_cap
*api
)
391 case ZEBRA_CLIENT_GR_CAPABILITIES
:
392 case ZEBRA_CLIENT_ROUTE_UPDATE_PENDING
:
393 case ZEBRA_CLIENT_GR_DISABLE
:
394 case ZEBRA_CLIENT_RIB_STALE_TIME
:
396 * For all the cases we need to update the client info.
398 zebra_client_update_info(client
, api
);
400 case ZEBRA_CLIENT_ROUTE_UPDATE_COMPLETE
:
402 * After client info has been updated delete all
405 zebra_client_update_info(client
, api
);
406 zebra_gr_process_client_stale_routes(client
, api
->vrf_id
);
412 * Function to decode and call appropriate functions
413 * to handle client capabilities.
415 void zread_client_capabilities(ZAPI_HANDLER_ARGS
)
422 if (zapi_capabilities_decode(s
, &api
)) {
423 LOG_GR("%s: Error in reading capabilities for client %s",
424 __func__
, zebra_route_string(client
->proto
));
428 /* Call the capabilities handler */
429 zebra_client_capabilities_handler(client
, &api
);
434 * Stale route handling
438 * Delete all the stale routes that have not been refreshed
441 static int32_t zebra_gr_route_stale_delete_timer_expiry(struct thread
*thread
)
443 struct client_gr_info
*info
;
445 struct zserv
*client
;
447 info
= THREAD_ARG(thread
);
448 info
->t_stale_removal
= NULL
;
449 client
= (struct zserv
*)info
->stale_client_ptr
;
451 /* Set the flag to indicate all stale route deletion */
452 if (thread
->u
.val
== 1)
455 cnt
= zebra_gr_delete_stale_routes(info
);
457 /* Retsart the timer */
459 LOG_GR("%s: Client %s processed %d routes. Start timer again",
460 __func__
, zebra_route_string(client
->proto
), cnt
);
462 thread_add_timer(zrouter
.master
,
463 zebra_gr_route_stale_delete_timer_expiry
, info
,
464 ZEBRA_DEFAULT_STALE_UPDATE_DELAY
,
465 &info
->t_stale_removal
);
467 /* No routes to delete for the VRF */
468 LOG_GR("%s: Client %s all starle routes processed", __func__
,
469 zebra_route_string(client
->proto
));
471 if (info
->current_prefix
!= NULL
)
472 XFREE(MTYPE_TMP
, info
->current_prefix
);
473 info
->current_prefix
= NULL
;
474 info
->current_afi
= 0;
475 zebra_gr_delete_stale_client(info
);
482 * Function to process to check if route entry is stale
483 * or has been updated.
485 static void zebra_gr_process_route_entry(struct zserv
*client
,
486 struct route_node
*rn
,
487 struct route_entry
*re
)
489 char buf
[PREFIX2STR_BUFFER
];
491 if ((client
== NULL
) || (rn
== NULL
) || (re
== NULL
))
494 /* If the route is not refreshed after restart, delete the entry */
495 if (re
->uptime
< client
->restart_time
) {
496 if (IS_ZEBRA_DEBUG_RIB
) {
497 prefix2str(&rn
->p
, buf
, sizeof(buf
));
498 zlog_debug("%s: Client %s stale route %s is deleted",
499 __func__
, zebra_route_string(client
->proto
),
507 * This function walks through the route table for all vrf and deletes
508 * the stale routes for the restarted client specified by the protocol
511 static int32_t zebra_gr_delete_stale_route(struct client_gr_info
*info
,
512 struct zebra_vrf
*zvrf
)
514 struct route_node
*rn
, *curr
;
515 struct route_entry
*re
;
516 struct route_entry
*next
;
517 struct route_table
*table
;
523 struct zserv
*s_client
;
525 if ((info
== NULL
) || (zvrf
== NULL
))
528 s_client
= info
->stale_client_ptr
;
529 if (s_client
== NULL
) {
530 LOG_GR("%s: Stale client not present", __func__
);
534 proto
= s_client
->proto
;
535 instance
= s_client
->instance
;
536 curr_afi
= info
->current_afi
;
538 LOG_GR("%s: Client %s stale routes are being deleted", __func__
,
539 zebra_route_string(proto
));
541 /* Process routes for all AFI */
542 for (afi
= curr_afi
; afi
< AFI_MAX
; afi
++) {
543 table
= zvrf
->table
[afi
][SAFI_UNICAST
];
544 p
= info
->current_prefix
;
548 * If the current prefix is NULL then get the first
549 * route entry in the table
552 rn
= route_top(table
);
555 p
= XCALLOC(MTYPE_TMP
, sizeof(struct prefix
));
559 prefix_copy(p
, &rn
->p
);
561 /* Get the next route entry */
562 curr
= route_table_get_next(table
, p
);
564 for (rn
= curr
; rn
; rn
= srcdest_route_next(rn
)) {
565 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
566 if (CHECK_FLAG(re
->status
,
567 ROUTE_ENTRY_REMOVED
))
569 /* If the route refresh is received
570 * after restart then do not delete
573 if (re
->type
== proto
574 && re
->instance
== instance
) {
575 zebra_gr_process_route_entry(
580 /* If the max route count is reached
581 * then timer thread will be restarted
582 * Store the current prefix and afi
584 if ((n
>= ZEBRA_MAX_STALE_ROUTE_COUNT
)
585 && (info
->delete == false)) {
586 prefix_copy(p
, &rn
->p
);
587 info
->current_afi
= afi
;
588 info
->current_prefix
= p
;
595 * Reset the current prefix to indicate processing completion
598 if (info
->current_prefix
) {
599 XFREE(MTYPE_TMP
, info
->current_prefix
);
600 info
->current_prefix
= NULL
;
608 * Delete the stale routes when client is restarted and routes are not
609 * refreshed within the stale timeout
611 static int32_t zebra_gr_delete_stale_routes(struct client_gr_info
*info
)
614 struct zebra_vrf
*zvrf
;
620 /* Get the current VRF */
621 vrf
= vrf_lookup_by_id(info
->vrf_id
);
623 LOG_GR("%s: Invalid VRF %d", __func__
, info
->vrf_id
);
629 LOG_GR("%s: Invalid VRF entry %d", __func__
, info
->vrf_id
);
633 cnt
= zebra_gr_delete_stale_route(info
, zvrf
);
638 * This function checks if route update for all AFI, SAFI is completed
639 * and cancels the stale timer
641 static void zebra_gr_process_client_stale_routes(struct zserv
*client
,
644 struct client_gr_info
*info
= NULL
;
648 TAILQ_FOREACH (info
, &client
->gr_info_queue
, gr_info
) {
649 if (info
->vrf_id
== vrf_id
)
656 /* Check if route update completed for all AFI, SAFI */
657 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
658 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MPLS_VPN
; safi
++) {
659 if (info
->af_enabled
[afi
][safi
]) {
660 if (!info
->route_sync
[afi
][safi
]) {
662 "%s: Client %s route update not completed for AFI %d, SAFI %d",
663 __func__
, zebra_route_string(
672 * Route update completed for all AFI, SAFI
673 * Cancel the stale timer and process the routes
675 if (info
->t_stale_removal
) {
676 LOG_GR("%s: Client %s cancled stale delete timer vrf %d",
677 __func__
, zebra_route_string(client
->proto
),
679 THREAD_OFF(info
->t_stale_removal
);
680 thread_execute(zrouter
.master
,
681 zebra_gr_route_stale_delete_timer_expiry
, info
,