]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_gr.c
Merge pull request #12798 from donaldsharp/rib_match_multicast
[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 "lib/thread.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 thread *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 THREAD_OFF(info->t_stale_removal);
84 info->t_stale_removal = NULL;
85 /* Process the stale routes */
86 thread_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 TAILQ_REMOVE(&(client->gr_info_queue), info, gr_info);
115
116 THREAD_OFF(info->t_stale_removal);
117
118 XFREE(MTYPE_ZEBRA_GR, info->current_prefix);
119
120 LOG_GR("%s: Instance info is being deleted for client %s", __func__,
121 zebra_route_string(client->proto));
122
123 /* Delete all the stale routes. */
124 info->do_delete = true;
125 zebra_gr_delete_stale_routes(info);
126
127 XFREE(MTYPE_ZEBRA_GR, info);
128 }
129
130 /*
131 * Function to handle client when it disconnect.
132 */
133 int32_t zebra_gr_client_disconnect(struct zserv *client)
134 {
135 struct zserv *stale_client;
136 struct timeval tv;
137 struct client_gr_info *info = NULL;
138
139 /* Find the stale client */
140 stale_client = zebra_gr_find_stale_client(client);
141
142 /*
143 * We should never be here.
144 */
145 if (stale_client) {
146 LOG_GR("%s: Stale client %s exist, we should not be here!",
147 __func__, zebra_route_string(client->proto));
148 assert(0);
149 }
150
151 client->restart_time = monotime(&tv);
152
153 /* For all the GR instance start the stale removal timer. */
154 TAILQ_FOREACH (info, &client->gr_info_queue, gr_info) {
155 if (ZEBRA_CLIENT_GR_ENABLED(info->capabilities)
156 && (info->t_stale_removal == NULL)) {
157 thread_add_timer(
158 zrouter.master,
159 zebra_gr_route_stale_delete_timer_expiry, info,
160 info->stale_removal_time,
161 &info->t_stale_removal);
162 info->current_afi = AFI_IP;
163 info->stale_client_ptr = client;
164 info->stale_client = true;
165 LOG_GR("%s: Client %s Stale timer update to %d",
166 __func__, zebra_route_string(client->proto),
167 info->stale_removal_time);
168 }
169 }
170
171 listnode_add(zrouter.stale_client_list, client);
172
173 return 0;
174 }
175
176 /*
177 * Function to delete stale client
178 */
179 static void zebra_gr_delete_stale_client(struct client_gr_info *info)
180 {
181 struct client_gr_info *bgp_info;
182 struct zserv *s_client = NULL;
183
184 s_client = info->stale_client_ptr;
185
186 if (!s_client || !info->stale_client)
187 return;
188
189 /*
190 * If there are bgp instances with the stale delete timer pending
191 * then stale client is not deleted
192 */
193 if ((s_client->gr_instance_count > 0) && info->gr_enable)
194 s_client->gr_instance_count--;
195
196 TAILQ_REMOVE(&(s_client->gr_info_queue), info, gr_info);
197
198 LOG_GR("%s: Client %s gr count %d", __func__,
199 zebra_route_string(s_client->proto),
200 s_client->gr_instance_count);
201
202 TAILQ_FOREACH (bgp_info, &s_client->gr_info_queue, gr_info) {
203 if (bgp_info->t_stale_removal != NULL)
204 return;
205 }
206
207 LOG_GR("%s: Client %s is being deleted", __func__,
208 zebra_route_string(s_client->proto));
209
210 TAILQ_INIT(&(s_client->gr_info_queue));
211 listnode_delete(zrouter.stale_client_list, s_client);
212 if (info->stale_client)
213 zserv_client_delete(s_client);
214 XFREE(MTYPE_ZEBRA_GR, info);
215 }
216
217 /*
218 * Function to find stale client.
219 */
220 static struct zserv *zebra_gr_find_stale_client(struct zserv *client)
221 {
222 struct listnode *node, *nnode;
223 struct zserv *stale_client;
224
225 /* Find the stale client */
226 for (ALL_LIST_ELEMENTS(zrouter.stale_client_list, node, nnode,
227 stale_client)) {
228 if (client->proto == stale_client->proto
229 && client->instance == stale_client->instance) {
230 return stale_client;
231 }
232 }
233
234 return NULL;
235 }
236
237 /*
238 * Function to handle reconnect of client post restart.
239 */
240 void zebra_gr_client_reconnect(struct zserv *client)
241 {
242 struct listnode *node, *nnode;
243 struct zserv *old_client = NULL;
244 struct client_gr_info *info = NULL;
245
246 /* Find the stale client */
247 for (ALL_LIST_ELEMENTS(zrouter.stale_client_list, node, nnode,
248 old_client)) {
249 if (client->proto == old_client->proto
250 && client->instance == old_client->instance)
251 break;
252 }
253
254 /* Copy the timers */
255 if (!old_client)
256 return;
257
258 client->gr_instance_count = old_client->gr_instance_count;
259 client->restart_time = old_client->restart_time;
260
261 LOG_GR("%s : old client %s, gr_instance_count %d", __func__,
262 zebra_route_string(old_client->proto),
263 old_client->gr_instance_count);
264
265 if (TAILQ_FIRST(&old_client->gr_info_queue)) {
266 TAILQ_CONCAT(&client->gr_info_queue, &old_client->gr_info_queue,
267 gr_info);
268 TAILQ_INIT(&old_client->gr_info_queue);
269 }
270
271 TAILQ_FOREACH (info, &client->gr_info_queue, gr_info) {
272 info->stale_client_ptr = client;
273 info->stale_client = false;
274 }
275
276 /* Delete the stale client */
277 listnode_delete(zrouter.stale_client_list, old_client);
278 /* Delete old client */
279 zserv_client_delete(old_client);
280 }
281
282 /*
283 * Functions to deal with capabilities
284 */
285
286 /*
287 * Update the graceful restart information
288 * for the client instance.
289 * This function handles all the capabilities that are received.
290 */
291 static void zebra_client_update_info(struct zserv *client, struct zapi_cap *api)
292 {
293 struct client_gr_info *info = NULL;
294
295 /* Find the bgp information for the specified vrf id */
296 TAILQ_FOREACH (info, &client->gr_info_queue, gr_info) {
297 if (info->vrf_id == api->vrf_id)
298 break;
299 }
300
301
302 /*
303 * If the command is delete, then cancel the stale timer and
304 * delete the bgp info
305 */
306 switch (api->cap) {
307 case ZEBRA_CLIENT_GR_DISABLE:
308 if (!info)
309 return;
310
311 LOG_GR("%s: Client %s instance GR disabled count %d", __func__,
312 zebra_route_string(client->proto),
313 client->gr_instance_count);
314
315 if ((info->gr_enable) && (client->gr_instance_count > 0))
316 client->gr_instance_count--;
317
318 zebra_gr_client_info_delte(client, info);
319 break;
320 case ZEBRA_CLIENT_GR_CAPABILITIES:
321 /* Allocate bgp info */
322 if (!info)
323 info = zebra_gr_client_info_create(client);
324
325 /* Update other parameters */
326 if (!info->gr_enable) {
327 client->gr_instance_count++;
328
329 LOG_GR("%s: Cient %s GR enabled count %d", __func__,
330 zebra_route_string(client->proto),
331 client->gr_instance_count);
332
333 info->capabilities = api->cap;
334 info->stale_removal_time = api->stale_removal_time;
335 info->vrf_id = api->vrf_id;
336 info->gr_enable = true;
337 }
338 break;
339 case ZEBRA_CLIENT_RIB_STALE_TIME:
340 LOG_GR("%s: Client %s stale time update event", __func__,
341 zebra_route_string(client->proto));
342
343 /* Update the stale removal timer */
344 if (info && info->t_stale_removal == NULL) {
345
346 LOG_GR("%s: Stale time: %d is now update to: %d",
347 __func__, info->stale_removal_time,
348 api->stale_removal_time);
349
350 info->stale_removal_time = api->stale_removal_time;
351 }
352
353 break;
354 case ZEBRA_CLIENT_ROUTE_UPDATE_COMPLETE:
355 LOG_GR(
356 "%s: Client %s route update complete for AFI %d, SAFI %d",
357 __func__, zebra_route_string(client->proto), api->afi,
358 api->safi);
359 if (info)
360 info->route_sync[api->afi][api->safi] = true;
361 break;
362 case ZEBRA_CLIENT_ROUTE_UPDATE_PENDING:
363 LOG_GR("%s: Client %s route update pending for AFI %d, SAFI %d",
364 __func__, zebra_route_string(client->proto), api->afi,
365 api->safi);
366 if (info)
367 info->af_enabled[api->afi][api->safi] = true;
368 break;
369 }
370 }
371
372 /*
373 * Handler for capabilities that are received from client.
374 */
375 static void zebra_client_capabilities_handler(struct zserv *client,
376 struct zapi_cap *api)
377 {
378 switch (api->cap) {
379 case ZEBRA_CLIENT_GR_CAPABILITIES:
380 case ZEBRA_CLIENT_ROUTE_UPDATE_PENDING:
381 case ZEBRA_CLIENT_GR_DISABLE:
382 case ZEBRA_CLIENT_RIB_STALE_TIME:
383 /*
384 * For all the cases we need to update the client info.
385 */
386 zebra_client_update_info(client, api);
387 break;
388 case ZEBRA_CLIENT_ROUTE_UPDATE_COMPLETE:
389 /*
390 * After client info has been updated delete all
391 * stale routes
392 */
393 zebra_client_update_info(client, api);
394 zebra_gr_process_client_stale_routes(client, api->vrf_id);
395 break;
396 }
397 }
398
399 /*
400 * Function to decode and call appropriate functions
401 * to handle client capabilities.
402 */
403 void zread_client_capabilities(ZAPI_HANDLER_ARGS)
404 {
405 struct zapi_cap api;
406 struct stream *s;
407
408 s = msg;
409
410 if (zapi_capabilities_decode(s, &api)) {
411 LOG_GR("%s: Error in reading capabilities for client %s",
412 __func__, zebra_route_string(client->proto));
413 return;
414 }
415
416 /* GR only for dynamic clients */
417 if (client->proto <= ZEBRA_ROUTE_CONNECT) {
418 LOG_GR("%s: GR capabilities for client %s not supported",
419 __func__, zebra_route_string(client->proto));
420 return;
421 }
422 /* Call the capabilities handler */
423 zebra_client_capabilities_handler(client, &api);
424 }
425
426
427 /*
428 * Stale route handling
429 */
430
431 /*
432 * Delete all the stale routes that have not been refreshed
433 * post restart.
434 */
435 static void zebra_gr_route_stale_delete_timer_expiry(struct thread *thread)
436 {
437 struct client_gr_info *info;
438 int32_t cnt = 0;
439 struct zserv *client;
440
441 info = THREAD_ARG(thread);
442 info->t_stale_removal = NULL;
443 client = (struct zserv *)info->stale_client_ptr;
444
445 /* Set the flag to indicate all stale route deletion */
446 if (thread->u.val == 1)
447 info->do_delete = true;
448
449 cnt = zebra_gr_delete_stale_routes(info);
450
451 /* Restart the timer */
452 if (cnt > 0) {
453 LOG_GR("%s: Client %s processed %d routes. Start timer again",
454 __func__, zebra_route_string(client->proto), cnt);
455
456 thread_add_timer(zrouter.master,
457 zebra_gr_route_stale_delete_timer_expiry, info,
458 ZEBRA_DEFAULT_STALE_UPDATE_DELAY,
459 &info->t_stale_removal);
460 } else {
461 /* No routes to delete for the VRF */
462 LOG_GR("%s: Client %s all stale routes processed", __func__,
463 zebra_route_string(client->proto));
464
465 XFREE(MTYPE_ZEBRA_GR, info->current_prefix);
466 info->current_afi = 0;
467 zebra_gr_delete_stale_client(info);
468 }
469 }
470
471
472 /*
473 * Function to process to check if route entry is stale
474 * or has been updated.
475 */
476 static void zebra_gr_process_route_entry(struct zserv *client,
477 struct route_node *rn,
478 struct route_entry *re)
479 {
480 if ((client == NULL) || (rn == NULL) || (re == NULL))
481 return;
482
483 /* If the route is not refreshed after restart, delete the entry */
484 if (re->uptime < client->restart_time) {
485 if (IS_ZEBRA_DEBUG_RIB)
486 zlog_debug("%s: Client %s stale route %pFX is deleted",
487 __func__, zebra_route_string(client->proto),
488 &rn->p);
489 rib_delnode(rn, re);
490 }
491 }
492
493 /*
494 * This function walks through the route table for all vrf and deletes
495 * the stale routes for the restarted client specified by the protocol
496 * type
497 */
498 static int32_t zebra_gr_delete_stale_route(struct client_gr_info *info,
499 struct zebra_vrf *zvrf)
500 {
501 struct route_node *rn, *curr;
502 struct route_entry *re;
503 struct route_entry *next;
504 struct route_table *table;
505 int32_t n = 0;
506 afi_t afi, curr_afi;
507 uint8_t proto;
508 uint16_t instance;
509 struct zserv *s_client;
510
511 if ((info == NULL) || (zvrf == NULL))
512 return -1;
513
514 s_client = info->stale_client_ptr;
515 if (s_client == NULL) {
516 LOG_GR("%s: Stale client not present", __func__);
517 return -1;
518 }
519
520 proto = s_client->proto;
521 instance = s_client->instance;
522 curr_afi = info->current_afi;
523
524 LOG_GR("%s: Client %s stale routes are being deleted", __func__,
525 zebra_route_string(proto));
526
527 /* Process routes for all AFI */
528 for (afi = curr_afi; afi < AFI_MAX; afi++) {
529 table = zvrf->table[afi][SAFI_UNICAST];
530
531 if (table) {
532 /*
533 * If the current prefix is NULL then get the first
534 * route entry in the table
535 */
536 if (info->current_prefix == NULL) {
537 rn = route_top(table);
538 if (rn == NULL)
539 continue;
540 curr = rn;
541 } else
542 /* Get the next route entry */
543 curr = route_table_get_next(
544 table, info->current_prefix);
545
546 for (rn = curr; rn; rn = srcdest_route_next(rn)) {
547 RNODE_FOREACH_RE_SAFE (rn, re, next) {
548 if (CHECK_FLAG(re->status,
549 ROUTE_ENTRY_REMOVED))
550 continue;
551 /* If the route refresh is received
552 * after restart then do not delete
553 * the route
554 */
555 if (re->type == proto
556 && re->instance == instance) {
557 zebra_gr_process_route_entry(
558 s_client, rn, re);
559 n++;
560 }
561
562 /* If the max route count is reached
563 * then timer thread will be restarted
564 * Store the current prefix and afi
565 */
566 if ((n >= ZEBRA_MAX_STALE_ROUTE_COUNT)
567 && (info->do_delete == false)) {
568 info->current_afi = afi;
569 info->current_prefix = XCALLOC(
570 MTYPE_ZEBRA_GR,
571 sizeof(struct prefix));
572 prefix_copy(
573 info->current_prefix,
574 &rn->p);
575 return n;
576 }
577 }
578 }
579 }
580 /*
581 * Reset the current prefix to indicate processing completion
582 * of the current AFI
583 */
584 XFREE(MTYPE_ZEBRA_GR, info->current_prefix);
585 }
586 return 0;
587 }
588
589 /*
590 * Delete the stale routes when client is restarted and routes are not
591 * refreshed within the stale timeout
592 */
593 static int32_t zebra_gr_delete_stale_routes(struct client_gr_info *info)
594 {
595 struct vrf *vrf;
596 struct zebra_vrf *zvrf;
597 uint64_t cnt = 0;
598
599 if (info == NULL)
600 return -1;
601
602 /* Get the current VRF */
603 vrf = vrf_lookup_by_id(info->vrf_id);
604 if (vrf == NULL) {
605 LOG_GR("%s: Invalid VRF %d", __func__, info->vrf_id);
606 return -1;
607 }
608
609 zvrf = vrf->info;
610 if (zvrf == NULL) {
611 LOG_GR("%s: Invalid VRF entry %d", __func__, info->vrf_id);
612 return -1;
613 }
614
615 cnt = zebra_gr_delete_stale_route(info, zvrf);
616 return cnt;
617 }
618
619 /*
620 * This function checks if route update for all AFI, SAFI is completed
621 * and cancels the stale timer
622 */
623 static void zebra_gr_process_client_stale_routes(struct zserv *client,
624 vrf_id_t vrf_id)
625 {
626 struct client_gr_info *info = NULL;
627 afi_t afi;
628 safi_t safi;
629
630 TAILQ_FOREACH (info, &client->gr_info_queue, gr_info) {
631 if (info->vrf_id == vrf_id)
632 break;
633 }
634
635 if (info == NULL)
636 return;
637
638 /* Check if route update completed for all AFI, SAFI */
639 FOREACH_AFI_SAFI_NSF (afi, safi) {
640 if (info->af_enabled[afi][safi]) {
641 if (!info->route_sync[afi][safi]) {
642 LOG_GR("%s: Client %s route update not completed for AFI %d, SAFI %d",
643 __func__,
644 zebra_route_string(client->proto), afi,
645 safi);
646 return;
647 }
648 }
649 }
650
651 /*
652 * Route update completed for all AFI, SAFI
653 * Cancel the stale timer and process the routes
654 */
655 if (info->t_stale_removal) {
656 LOG_GR("%s: Client %s canceled stale delete timer vrf %d",
657 __func__, zebra_route_string(client->proto),
658 info->vrf_id);
659 THREAD_OFF(info->t_stale_removal);
660 thread_execute(zrouter.master,
661 zebra_gr_route_stale_delete_timer_expiry, info,
662 0);
663 }
664 }