From cb440058f2d85d1408f932ad820c38e8e1b17191 Mon Sep 17 00:00:00 2001 From: Louis Scalbert Date: Fri, 24 May 2024 17:06:59 +0200 Subject: [PATCH] zebra: fix show route vrf all memory consumption 0e2fc3d67f ("vtysh, zebra: Fix malformed json output for multiple vrfs in command 'show ip route vrf all json'") has been reverted in the previous commit. Although the fix was correct, it was consuming too muca memory when displaying large route tables. A root JSON object was storing all the JSON objects containing the route tables, each containing their respective prefixes in JSON objects. This resulted in excessive memory allocation for JSON objects, potentially leading to an out-of-memory error on the machine. To Fix the memory consumption issue for the "show ip[v6] route vrf all json" command, display the tables one by one and free the memory of each JSON object after it has been displayed. Signed-off-by: Louis Scalbert --- zebra/zebra_vty.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/zebra/zebra_vty.c b/zebra/zebra_vty.c index cc2d9b2164..97245d55b9 100644 --- a/zebra/zebra_vty.c +++ b/zebra/zebra_vty.c @@ -1739,6 +1739,7 @@ DEFPY (show_route, "Nexthop Group Information\n") { afi_t afi = ipv4 ? AFI_IP : AFI_IP6; + bool first_vrf_json = true; struct vrf *vrf; int type = 0; struct zebra_vrf *zvrf; @@ -1770,7 +1771,9 @@ DEFPY (show_route, if ((zvrf = vrf->info) == NULL || (zvrf->table[afi][SAFI_UNICAST] == NULL)) continue; - + if (json) + vty_json_key(vty, zvrf_name(zvrf), + &first_vrf_json); if (table_all) do_show_ip_route_all(vty, zvrf, afi, !!fib, !!json, tag, @@ -1786,6 +1789,8 @@ DEFPY (show_route, ospf_instance_id, table, !!ng, &ctx); } + if (json) + vty_json_close(vty, first_vrf_json); } else { vrf_id_t vrf_id = VRF_DEFAULT;