Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"show bgp ipv4 detail json" consumes too much memory #16880

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 62 additions & 1 deletion bgpd/bgp_aspath.c
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ static void aspath_make_str_count(struct aspath *as, bool make_json)
for (i = 0; i < seg->length; i++) {
if (make_json)
asn_asn2json_array(jseg_list, seg->as[i],
as->asnotation);
as->asnotation, NULL, false);
len += snprintfrr(str_buf + len, str_size - len,
ASN_FORMAT(as->asnotation),
&seg->as[i]);
Expand Down Expand Up @@ -662,6 +662,67 @@ void aspath_str_update(struct aspath *as, bool make_json)
aspath_make_str_count(as, make_json);
}

#if JSON_C_MINOR_VERSION >= 13 && JSON_C_MAJOR_VERSION >= 0
static int aspath_json_string(struct json_object *jso, struct printbuf *pb,
int level, int flags)
{
struct aspath *as;
struct assegment *seg;
int count_seg = 0, i;
char num_str[12];
const char *seg_type;


as = json_object_get_userdata(jso);
assert(as);
seg = as->segments;
if (seg == NULL)
printbuf_strappend(pb, "{\"string\":\"Local\",\"segments\":[");
else {
printbuf_strappend(pb, "{\"string\":\"");
printbuf_memappend(pb, as->str, strlen(as->str));
printbuf_strappend(pb, "\",\"segments\":[");
}
while (seg) {
if (count_seg)
printbuf_strappend(pb, ",");
printbuf_strappend(pb, "{\"type\":\"");
seg_type = aspath_segment_type_str[seg->type];
printbuf_memappend(pb, seg_type, strlen(seg_type));
printbuf_strappend(pb, "\",\"list\":[");

for (i = 0; i < seg->length; i++) {
asn_asn2json_array(NULL, seg->as[i], as->asnotation, pb,
true);
if (i < (seg->length - 1))
printbuf_strappend(pb, ",");
}
printbuf_strappend(pb, "]}");
seg = seg->next;
count_seg++;
}
printbuf_strappend(pb, "],\"length\":");
snprintf(num_str, sizeof(num_str), "%d}", aspath_count_hops(as));
return printbuf_memappend(pb, num_str, strlen(num_str));
}
#endif

json_object *aspath_get_json(struct aspath *aspath)
{
json_object *json_aspath = NULL;

#if JSON_C_MINOR_VERSION >= 13 && JSON_C_MAJOR_VERSION >= 0
json_aspath = json_object_new_object();
json_object_set_serializer(json_aspath, aspath_json_string, aspath,
NULL);
#else
if (!aspath->json)
aspath_str_update(aspath, true);
json_aspath = json_object_lock(aspath->json);
#endif
return json_aspath;
}

/* Intern allocated AS path. */
struct aspath *aspath_intern(struct aspath *aspath)
{
Expand Down
1 change: 1 addition & 0 deletions bgpd/bgp_aspath.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ extern struct aspath *aspath_str2aspath(const char *str,
extern void aspath_str_update(struct aspath *as, bool make_json);
extern void aspath_free(struct aspath *aspath);
extern struct aspath *aspath_intern(struct aspath *aspath);
extern json_object *aspath_get_json(struct aspath *aspath);
extern void aspath_unintern(struct aspath **aspath);
extern const char *aspath_print(struct aspath *aspath);
extern void aspath_print_vty(struct vty *vty, struct aspath *aspath);
Expand Down
Loading
Loading