From 24882500ff0f6c84afd73916ffb2b69a3a76008f Mon Sep 17 00:00:00 2001 From: Chirag Shah Date: Mon, 27 Jan 2020 10:41:22 -0800 Subject: [PATCH] bgpd: fix memory leak in evpn json outpus II Two of the evpn show commands with json option has memory leak. 1) show bgp l2vpn evpn route vni all json 2) show bgp l2vpn evpn route esi json Before fix: ---------- Executed 'show bgp l2vpn evpn route vni all json' multiple times used ordinary blocks continue to increase. Note at the time of show command capture there were 22 evpn routes in vni evpn route table. Memory statistics for bgpd: System allocator statistics: Total heap allocated: 9152 KiB Holding block headers: 0 bytes Used small blocks: 0 bytes Used ordinary blocks: 7300 KiB Free small blocks: 1760 bytes Free ordinary blocks: 1852 KiB Ordinary blocks: 880 Small blocks: 51 Holding blocks: 0 Ticket:CM-27920 Reviewed By: Testing Done: After fix: --------- Executed 'show bgp l2vpn evpn route vni all json' multiple times Used ordinary blocks remains low. Memory statistics for bgpd: System allocator statistics: Total heap allocated: 8356 KiB Holding block headers: 0 bytes Used small blocks: 0 bytes Used ordinary blocks: 6492 KiB Free small blocks: 1840 bytes Free ordinary blocks: 1864 KiB Ordinary blocks: 939 Small blocks: 52 Holding blocks: 0 Signed-off-by: Chirag Shah --- bgpd/bgp_evpn_vty.c | 48 ++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/bgpd/bgp_evpn_vty.c b/bgpd/bgp_evpn_vty.c index 125ed61e74..44a884830a 100644 --- a/bgpd/bgp_evpn_vty.c +++ b/bgpd/bgp_evpn_vty.c @@ -687,14 +687,22 @@ static void show_esi_routes(struct bgp *bgp, add_prefix_to_json = 1; } - if (json && add_prefix_to_json) { - json_object_string_add(json_prefix, "prefix", - prefix_str); - json_object_int_add(json_prefix, "prefixLen", - rn->p.prefixlen); - json_object_object_add(json_prefix, "paths", - json_paths); - json_object_object_add(json, prefix_str, json_prefix); + if (json) { + if (add_prefix_to_json) { + json_object_string_add(json_prefix, "prefix", + prefix_str); + json_object_int_add(json_prefix, "prefixLen", + rn->p.prefixlen); + json_object_object_add(json_prefix, "paths", + json_paths); + json_object_object_add(json, prefix_str, + json_prefix); + } else { + json_object_free(json_paths); + json_object_free(json_prefix); + json_paths = NULL; + json_prefix = NULL; + } } } @@ -786,14 +794,22 @@ static void show_vni_routes(struct bgp *bgp, struct bgpevpn *vpn, int type, add_prefix_to_json = 1; } - if (json && add_prefix_to_json) { - json_object_string_add(json_prefix, "prefix", - prefix_str); - json_object_int_add(json_prefix, "prefixLen", - rn->p.prefixlen); - json_object_object_add(json_prefix, "paths", - json_paths); - json_object_object_add(json, prefix_str, json_prefix); + if (json) { + if (add_prefix_to_json) { + json_object_string_add(json_prefix, "prefix", + prefix_str); + json_object_int_add(json_prefix, "prefixLen", + rn->p.prefixlen); + json_object_object_add(json_prefix, "paths", + json_paths); + json_object_object_add(json, prefix_str, + json_prefix); + } else { + json_object_free(json_paths); + json_object_free(json_prefix); + json_paths = NULL; + json_prefix = NULL; + } } } -- 2.39.5