From e77564ccd44aa527c7a26f1642bd8e8f5a5f724a Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Tue, 7 Sep 2021 09:36:39 -0400 Subject: [PATCH 1/3] ospfd: Fix usage of JSON_C_TO_STRING_NOSLASHESCAPE The usage of json_object_to_json_string_ext is meant for generation of output string and returns a `char *` pointer to the `formatted` output. Just calling it does nothing and it's expensive to boot. Modify the code in ospfd to just output with the NOSLASHESCAPE when outputting. Signed-off-by: Donald Sharp --- ospfd/ospf_vty.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/ospfd/ospf_vty.c b/ospfd/ospf_vty.c index 1d4aa65355..8af4494335 100644 --- a/ospfd/ospf_vty.c +++ b/ospfd/ospf_vty.c @@ -10608,11 +10608,8 @@ static void show_ip_ospf_route_network(struct vty *vty, struct ospf *ospf, prefix2str(&rn->p, buf1, sizeof(buf1)); json_route = json_object_new_object(); - if (json) { + if (json) json_object_object_add(json, buf1, json_route); - json_object_to_json_string_ext( - json, JSON_C_TO_STRING_NOSLASHESCAPE); - } switch (or->path_type) { case OSPF_PATH_INTER_AREA: @@ -10909,11 +10906,8 @@ static void show_ip_ospf_route_external(struct vty *vty, struct ospf *ospf, snprintfrr(buf1, sizeof(buf1), "%pFX", &rn->p); json_route = json_object_new_object(); - if (json) { + if (json) json_object_object_add(json, buf1, json_route); - json_object_to_json_string_ext( - json, JSON_C_TO_STRING_NOSLASHESCAPE); - } switch (er->path_type) { case OSPF_PATH_TYPE1_EXTERNAL: @@ -11224,7 +11218,9 @@ DEFUN (show_ip_ospf_route, if (uj) { /* Keep Non-pretty format */ vty_out(vty, "%s\n", - json_object_to_json_string(json)); + json_object_to_json_string_ext( + json, + JSON_C_TO_STRING_NOSLASHESCAPE)); json_object_free(json); } else if (!ospf_output) vty_out(vty, "%% OSPF instance not found\n"); @@ -11236,7 +11232,9 @@ DEFUN (show_ip_ospf_route, if (uj) { vty_out(vty, "%s\n", json_object_to_json_string_ext( - json, JSON_C_TO_STRING_PRETTY)); + json, + JSON_C_TO_STRING_PRETTY + | JSON_C_TO_STRING_NOSLASHESCAPE)); json_object_free(json); } else vty_out(vty, "%% OSPF instance not found\n"); @@ -11250,7 +11248,9 @@ DEFUN (show_ip_ospf_route, if (uj) { vty_out(vty, "%s\n", json_object_to_json_string_ext( - json, JSON_C_TO_STRING_PRETTY)); + json, + JSON_C_TO_STRING_PRETTY + | JSON_C_TO_STRING_NOSLASHESCAPE)); json_object_free(json); } else vty_out(vty, "%% OSPF instance not found\n"); @@ -11263,7 +11263,9 @@ DEFUN (show_ip_ospf_route, ret = show_ip_ospf_route_common(vty, ospf, json, use_vrf); /* Keep Non-pretty format */ if (uj) - vty_out(vty, "%s\n", json_object_to_json_string(json)); + vty_out(vty, "%s\n", + json_object_to_json_string_ext( + json, JSON_C_TO_STRING_NOSLASHESCAPE)); } if (uj) From 192422ef3b2e0bc533a25ca276f22f36a14265ef Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Tue, 7 Sep 2021 09:40:12 -0400 Subject: [PATCH 2/3] bgpd: Fix usage of JSON_C_TO_STRING_NOSLASHESCAPE The usage of json_object_to_json_string_ext is mean for generation of output string and returns a `char *` pointer to the `formatted` output. Just calling it does nothing and it's expensive to boot. Modify the code in bgpd to just output with the NOSLASHESCAPE when outputting. Signed-off-by: Donald Sharp --- bgpd/bgp_evpn_vty.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/bgpd/bgp_evpn_vty.c b/bgpd/bgp_evpn_vty.c index 2bda5dbf9a..867efb6099 100644 --- a/bgpd/bgp_evpn_vty.c +++ b/bgpd/bgp_evpn_vty.c @@ -396,8 +396,6 @@ static void display_l3vni(struct vty *vty, struct bgp *bgp_vrf, originator_ip, sizeof(originator_ip))); json_object_string_add(json, "advertiseGatewayMacip", "n/a"); json_object_string_add(json, "advertiseSviMacIp", "n/a"); - json_object_to_json_string_ext(json, - JSON_C_TO_STRING_NOSLASHESCAPE); json_object_string_add(json, "advertisePip", bgp_vrf->evpn_info->advertise_pip ? "Enabled" : "Disabled"); @@ -967,8 +965,6 @@ static void show_l3vni_entry(struct vty *vty, struct bgp *bgp, json_object_string_add(json_vni, "advertiseGatewayMacip", "n/a"); json_object_string_add(json_vni, "advertiseSviMacIp", "n/a"); - json_object_to_json_string_ext(json_vni, - JSON_C_TO_STRING_NOSLASHESCAPE); json_object_string_add( json_vni, "advertisePip", bgp->evpn_info->advertise_pip ? "Enabled" : "Disabled"); @@ -4413,8 +4409,11 @@ DEFUN(show_bgp_l2vpn_evpn_vni, } if (uj) { - vty_out(vty, "%s\n", json_object_to_json_string_ext( - json, JSON_C_TO_STRING_PRETTY)); + vty_out(vty, "%s\n", + json_object_to_json_string_ext( + json, + JSON_C_TO_STRING_PRETTY + | JSON_C_TO_STRING_NOSLASHESCAPE)); json_object_free(json); } From 19d37e54a010c38b18f6a3e8b577b6b016ce5a6b Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Tue, 7 Sep 2021 10:10:50 -0400 Subject: [PATCH 3/3] ospfd: Don't allocate json memory unless we have json output Several functions in ospf_vty.c were allocating json memory irrelevant if it was needed or not and then at the end of the loop free'ing it if it was not used. Clean up the access pattern. Signed-off-by: Donald Sharp --- ospfd/ospf_vty.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/ospfd/ospf_vty.c b/ospfd/ospf_vty.c index 8af4494335..3ae9707f5f 100644 --- a/ospfd/ospf_vty.c +++ b/ospfd/ospf_vty.c @@ -10607,9 +10607,10 @@ static void show_ip_ospf_route_network(struct vty *vty, struct ospf *ospf, prefix2str(&rn->p, buf1, sizeof(buf1)); - json_route = json_object_new_object(); - if (json) + if (json) { + json_route = json_object_new_object(); json_object_object_add(json, buf1, json_route); + } switch (or->path_type) { case OSPF_PATH_INTER_AREA: @@ -10730,8 +10731,6 @@ static void show_ip_ospf_route_network(struct vty *vty, struct ospf *ospf, } } } - if (!json) - json_object_free(json_route); } if (!json) vty_out(vty, "\n"); @@ -10759,8 +10758,8 @@ static void show_ip_ospf_route_router(struct vty *vty, struct ospf *ospf, continue; int flag = 0; - json_route = json_object_new_object(); if (json) { + json_route = json_object_new_object(); json_object_object_add( json, inet_ntop(AF_INET, &rn->p.u.prefix4, buf, sizeof(buf)), @@ -10875,8 +10874,6 @@ static void show_ip_ospf_route_router(struct vty *vty, struct ospf *ospf, } } } - if (!json) - json_object_free(json_route); } if (!json) vty_out(vty, "\n"); @@ -10905,9 +10902,10 @@ static void show_ip_ospf_route_external(struct vty *vty, struct ospf *ospf, char buf1[19]; snprintfrr(buf1, sizeof(buf1), "%pFX", &rn->p); - json_route = json_object_new_object(); - if (json) + if (json) { + json_route = json_object_new_object(); json_object_object_add(json, buf1, json_route); + } switch (er->path_type) { case OSPF_PATH_TYPE1_EXTERNAL: @@ -11004,8 +11002,6 @@ static void show_ip_ospf_route_external(struct vty *vty, struct ospf *ospf, } } } - if (!json) - json_object_free(json_route); } if (!json) vty_out(vty, "\n");