From a1df2ac5996a74dd65a8d1abfa3824ab5de11510 Mon Sep 17 00:00:00 2001 From: Chirag Shah Date: Wed, 22 Jan 2020 12:22:27 -0800 Subject: [PATCH] bgpd: fix memory leak in evpn json outputs Found memory leak in json output of evpn's route commands. After executing 'show bgp l2vpn evpn route type prefix json' and 'show bgp l2vpn evpn route type macip json' few times (6 times) with more than 600 routes in total seeing memory footprint for bgpd continue to grow. Memory statistics for bgpd: System allocator statistics: Total heap allocated: 12 MiB Holding block headers: 0 bytes Used small blocks: 0 bytes Used ordinary blocks: 8390 KiB Free small blocks: 1760 bytes Free ordinary blocks: 3762 KiB Ordinary blocks: 1161 Small blocks: 51 Holding blocks: 0 Ticket:CM-27920 Testing Done: After fix: excute few times, 'show bgp l2vpn evpn route type prefix json' and 'show bgp l2vpn evpn route type macip json' commands where used ordinary blocks (uordblks) is in steady state. Memory statistics for bgpd: System allocator statistics: Total heap allocated: 9968 KiB Holding block headers: 0 bytes Used small blocks: 0 bytes Used ordinary blocks: 6486 KiB Free small blocks: 1984 bytes Free ordinary blocks: 3482 KiB Ordinary blocks: 1110 Small blocks: 54 Holding blocks: 0 Memory statistics for bgpd: System allocator statistics: Total heap allocated: 10100 KiB Holding block headers: 0 bytes Used small blocks: 0 bytes Used ordinary blocks: 6488 KiB Free small blocks: 1984 bytes Free ordinary blocks: 3612 KiB Ordinary blocks: 1113 Small blocks: 54 Holding blocks: 0 Signed-off-by: Chirag Shah --- bgpd/bgp_evpn_vty.c | 56 +++++++++++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/bgpd/bgp_evpn_vty.c b/bgpd/bgp_evpn_vty.c index 3049a00ce3..125ed61e74 100644 --- a/bgpd/bgp_evpn_vty.c +++ b/bgpd/bgp_evpn_vty.c @@ -2579,18 +2579,29 @@ static void evpn_show_route_rd(struct vty *vty, struct bgp *bgp, add_rd_to_json = 1; } - if (json && add_prefix_to_json) { - json_object_object_add(json_prefix, "paths", - json_paths); - json_object_object_add(json_rd, prefix_str, - json_prefix); + if (json) { + if (add_prefix_to_json) { + json_object_object_add(json_prefix, "paths", + json_paths); + json_object_object_add(json_rd, prefix_str, + json_prefix); + } else { + json_object_free(json_paths); + json_object_free(json_prefix); + json_paths = NULL; + json_prefix = NULL; + } } } - if (json && add_rd_to_json) - json_object_object_add(json, rd_str, json_rd); - if (json) { + if (add_rd_to_json) + json_object_object_add(json, rd_str, json_rd); + else { + json_object_free(json_rd); + json_rd = NULL; + } + json_object_int_add(json, "numPrefix", prefix_cnt); json_object_int_add(json, "numPaths", path_cnt); } else { @@ -2732,16 +2743,31 @@ static void evpn_show_all_routes(struct vty *vty, struct bgp *bgp, int type, json_path); } - if (json && add_prefix_to_json) { - json_object_object_add(json_prefix, "paths", - json_paths); - json_object_object_add(json_rd, prefix_str, - json_prefix); + if (json) { + if (add_prefix_to_json) { + json_object_object_add(json_prefix, + "paths", + json_paths); + json_object_object_add(json_rd, + prefix_str, + json_prefix); + } else { + json_object_free(json_prefix); + json_object_free(json_paths); + json_prefix = NULL; + json_paths = NULL; + } } } - if (json && add_rd_to_json) - json_object_object_add(json, rd_str, json_rd); + if (json) { + if (add_rd_to_json) + json_object_object_add(json, rd_str, json_rd); + else { + json_object_free(json_rd); + json_rd = NULL; + } + } } if (json) { -- 2.39.5