diff --git a/bgpd/bgp_aspath.c b/bgpd/bgp_aspath.c index 4c1615a5c625..a4e2bc0729c7 100644 --- a/bgpd/bgp_aspath.c +++ b/bgpd/bgp_aspath.c @@ -80,6 +80,9 @@ static struct stream *snmp_stream; /* as-path orphan exclude list */ static struct as_list_list_head as_exclude_list_orphan; +/* as-path foramt json variable */ +static bool aspath_format_json = false; + /* Callers are required to initialize the memory */ static as_t *assegment_data_new(int num) { @@ -662,6 +665,11 @@ void aspath_str_update(struct aspath *as, bool make_json) aspath_make_str_count(as, make_json); } +void aspath_set_format_json(bool make_json) +{ + aspath_format_json = make_json; +} + /* Intern allocated AS path. */ struct aspath *aspath_intern(struct aspath *aspath) { @@ -1112,7 +1120,7 @@ struct aspath *aspath_aggregate(struct aspath *as1, struct aspath *as2) } assegment_normalise(aspath->segments); - aspath_str_update(aspath, false); + aspath_str_update(aspath, aspath_format_json); return aspath; } @@ -1267,7 +1275,7 @@ struct aspath *aspath_replace_regex_asn(struct aspath *aspath, cur_seg = cur_seg->next; } - aspath_str_update(new, false); + aspath_str_update(new, aspath_format_json); return new; } @@ -1292,7 +1300,7 @@ struct aspath *aspath_replace_specific_asn(struct aspath *aspath, seg = seg->next; } - aspath_str_update(new, false); + aspath_str_update(new, aspath_format_json); return new; } @@ -1314,7 +1322,7 @@ struct aspath *aspath_replace_all_asn(struct aspath *aspath, as_t our_asn) seg = seg->next; } - aspath_str_update(new, false); + aspath_str_update(new, aspath_format_json); return new; } @@ -1340,7 +1348,7 @@ struct aspath *aspath_replace_private_asns(struct aspath *aspath, as_t asn, seg = seg->next; } - aspath_str_update(new, false); + aspath_str_update(new, aspath_format_json); return new; } @@ -1410,9 +1418,10 @@ struct aspath *aspath_remove_private_asns(struct aspath *aspath, as_t peer_asn) last_new_seg = new_seg; seg = seg->next; } + if (!aspath->refcnt) aspath_free(aspath); - aspath_str_update(new, false); + aspath_str_update(new, aspath_format_json); return new; } @@ -1468,7 +1477,7 @@ static struct aspath *aspath_merge(struct aspath *as1, struct aspath *as2) if (last) last->next = as2->segments; as2->segments = new; - aspath_str_update(as2, false); + aspath_str_update(as2, aspath_format_json); return as2; } @@ -1485,7 +1494,7 @@ struct aspath *aspath_prepend(struct aspath *as1, struct aspath *as2) /* If as2 is empty, only need to dupe as1's chain onto as2 */ if (as2->segments == NULL) { as2->segments = assegment_dup_all(as1->segments); - aspath_str_update(as2, false); + aspath_str_update(as2, aspath_format_json); return as2; } @@ -1505,7 +1514,7 @@ struct aspath *aspath_prepend(struct aspath *as1, struct aspath *as2) if (!as2->segments) { as2->segments = assegment_dup_all(as1->segments); - aspath_str_update(as2, false); + aspath_str_update(as2, aspath_format_json); return as2; } @@ -1550,7 +1559,7 @@ struct aspath *aspath_prepend(struct aspath *as1, struct aspath *as2) /* we've now prepended as1's segment chain to as2, merging * the inbetween AS_SEQUENCE of seg2 in the process */ - aspath_str_update(as2, false); + aspath_str_update(as2, aspath_format_json); return as2; } else { /* AS_SET merge code is needed at here. */ @@ -1661,7 +1670,7 @@ struct aspath *aspath_filter_exclude(struct aspath *source, lastseg->next = newseg; lastseg = newseg; } - aspath_str_update(newpath, false); + aspath_str_update(newpath, aspath_format_json); /* We are happy returning even an empty AS_PATH, because the * administrator * might expect this very behaviour. There's a mean to avoid this, if @@ -1679,7 +1688,7 @@ struct aspath *aspath_filter_exclude_all(struct aspath *source) newpath = aspath_new(source->asnotation); - aspath_str_update(newpath, false); + aspath_str_update(newpath, aspath_format_json); /* We are happy returning even an empty AS_PATH, because the * administrator * might expect this very behaviour. There's a mean to avoid this, if @@ -1766,7 +1775,7 @@ struct aspath *aspath_filter_exclude_acl(struct aspath *source, } - aspath_str_update(source, false); + aspath_str_update(source, aspath_format_json); /* We are happy returning even an empty AS_PATH, because the * administrator * might expect this very behaviour. There's a mean to avoid this, if @@ -1804,7 +1813,7 @@ static struct aspath *aspath_add_asns(struct aspath *aspath, as_t asno, aspath->segments = newsegment; } - aspath_str_update(aspath, false); + aspath_str_update(aspath, aspath_format_json); return aspath; } @@ -1895,7 +1904,7 @@ struct aspath *aspath_reconcile_as4(struct aspath *aspath, if (!hops) { newpath = aspath_dup(as4path); - aspath_str_update(newpath, false); + aspath_str_update(newpath, aspath_format_json); return newpath; } @@ -1956,7 +1965,7 @@ struct aspath *aspath_reconcile_as4(struct aspath *aspath, mergedpath = aspath_merge(newpath, aspath_dup(as4path)); aspath_free(newpath); mergedpath->segments = assegment_normalise(mergedpath->segments); - aspath_str_update(mergedpath, false); + aspath_str_update(mergedpath, aspath_format_json); if (BGP_DEBUG(as4, AS4)) zlog_debug("[AS4] result of synthesizing is %s", @@ -2028,7 +2037,7 @@ struct aspath *aspath_delete_confed_seq(struct aspath *aspath) } if (removed_confed_segment) - aspath_str_update(aspath, false); + aspath_str_update(aspath, aspath_format_json); return aspath; } @@ -2079,7 +2088,7 @@ struct aspath *aspath_empty_get(void) struct aspath *aspath; aspath = aspath_new(bgp_get_asnotation(NULL)); - aspath_make_str_count(aspath, false); + aspath_make_str_count(aspath, aspath_format_json); return aspath; } @@ -2220,7 +2229,7 @@ struct aspath *aspath_str2aspath(const char *str, } } - aspath_make_str_count(aspath, false); + aspath_make_str_count(aspath, aspath_format_json); return aspath; } @@ -2232,7 +2241,7 @@ unsigned int aspath_key_make(const void *p) unsigned int key = 0; if (!aspath->str) - aspath_str_update((struct aspath *)aspath, false); + aspath_str_update((struct aspath *)aspath, aspath_format_json); key = jhash(aspath->str, aspath->str_len, 2334325); diff --git a/bgpd/bgp_aspath.h b/bgpd/bgp_aspath.h index f7e57fd66dda..b8ec19e4aed2 100644 --- a/bgpd/bgp_aspath.h +++ b/bgpd/bgp_aspath.h @@ -110,6 +110,7 @@ extern struct aspath *aspath_empty_get(void); extern struct aspath *aspath_str2aspath(const char *str, enum asnotation_mode asnotation); extern void aspath_str_update(struct aspath *as, bool make_json); +extern void aspath_set_format_json(bool make_json); extern void aspath_free(struct aspath *aspath); extern struct aspath *aspath_intern(struct aspath *aspath); extern void aspath_unintern(struct aspath **aspath); diff --git a/bgpd/bgp_attr.c b/bgpd/bgp_attr.c index c178844cc9bc..7ca16f1bfb35 100644 --- a/bgpd/bgp_attr.c +++ b/bgpd/bgp_attr.c @@ -5098,10 +5098,13 @@ void bgp_packet_mpunreach_end(struct stream *s, size_t attrlen_pnt) void bgp_attr_init(void) { aspath_init(); + aspath_set_format_json(true); attrhash_init(); community_init(); + community_set_format_json(true); ecommunity_init(); lcommunity_init(); + lcommunity_set_format_json(true); cluster_init(); transit_init(); encap_init(); diff --git a/bgpd/bgp_clist.c b/bgpd/bgp_clist.c index 153cbd6e5015..edc1aeba912e 100644 --- a/bgpd/bgp_clist.c +++ b/bgpd/bgp_clist.c @@ -540,7 +540,7 @@ static bool community_regexp_match(struct community *com, regex_t *reg) if (com == NULL || com->size == 0) str = ""; else - str = community_str(com, false, true); + str = community_str(com, community_get_format_json(), true); regstr = bgp_alias2community_str(str); @@ -614,7 +614,7 @@ static bool lcommunity_regexp_match(struct lcommunity *com, regex_t *reg) if (com == NULL || com->size == 0) str = ""; else - str = lcommunity_str(com, false, true); + str = lcommunity_str(com, lcommunity_get_format_json(), true); regstr = bgp_alias2community_str(str); diff --git a/bgpd/bgp_community.c b/bgpd/bgp_community.c index 8e4c4305552b..2686150edb80 100644 --- a/bgpd/bgp_community.c +++ b/bgpd/bgp_community.c @@ -18,6 +18,9 @@ /* Hash of community attribute. */ static struct hash *comhash; +/* community foramt json variable */ +static bool community_format_json = false; + /* Allocate a new communities value. */ static struct community *community_new(void) { @@ -462,7 +465,7 @@ struct community *community_intern(struct community *com) /* Make string. */ if (!find->str) - set_community_string(find, false, true); + set_community_string(find, community_get_format_json(), true); return find; } @@ -1041,3 +1044,13 @@ void bgp_remove_comm_from_aggregate_hash(struct bgp_aggregate *aggregate, } } } + +void community_set_format_json(bool make_json) +{ + community_format_json = make_json; +} + +bool community_get_format_json() +{ + return community_format_json; +} diff --git a/bgpd/bgp_community.h b/bgpd/bgp_community.h index 7c7e7af4d29d..6942325f7ce2 100644 --- a/bgpd/bgp_community.h +++ b/bgpd/bgp_community.h @@ -120,4 +120,8 @@ static inline void community_strip_accept_own(struct attr *attr) } } +extern bool community_get_format_json(void); + +extern void community_set_format_json(bool make_json); + #endif /* _QUAGGA_BGP_COMMUNITY_H */ diff --git a/bgpd/bgp_debug.c b/bgpd/bgp_debug.c index 6228432bd2e8..de7e548278ea 100644 --- a/bgpd/bgp_debug.c +++ b/bgpd/bgp_debug.c @@ -433,16 +433,15 @@ bool bgp_dump_attr(struct attr *attr, char *buf, size_t size) attr->med); if (CHECK_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES))) - snprintf(buf + strlen(buf), size - strlen(buf), - ", community %s", - community_str(bgp_attr_get_community(attr), false, - true)); + snprintf(buf + strlen(buf), size - strlen(buf), ", community %s", + community_str(bgp_attr_get_community(attr), + community_get_format_json(), true)); if (CHECK_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_LARGE_COMMUNITIES))) snprintf(buf + strlen(buf), size - strlen(buf), ", large-community %s", - lcommunity_str(bgp_attr_get_lcommunity(attr), false, - true)); + lcommunity_str(attr->lcommunity, + lcommunity_get_format_json(), true)); if (CHECK_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES))) snprintf(buf + strlen(buf), size - strlen(buf), diff --git a/bgpd/bgp_lcommunity.c b/bgpd/bgp_lcommunity.c index 1b8c22a5038c..eec1134e6b34 100644 --- a/bgpd/bgp_lcommunity.c +++ b/bgpd/bgp_lcommunity.c @@ -22,6 +22,9 @@ /* Hash of community attribute. */ static struct hash *lcomhash; +/* large-community foramt json variable */ +static bool lcommunity_format_json = false; + /* Allocate a new lcommunities. */ static struct lcommunity *lcommunity_new(void) { @@ -264,7 +267,7 @@ struct lcommunity *lcommunity_intern(struct lcommunity *lcom) find->refcnt++; if (!find->str) - set_lcommunity_string(find, false, true); + set_lcommunity_string(find, lcommunity_get_format_json(), true); return find; } @@ -672,3 +675,13 @@ void bgp_remove_lcomm_from_aggregate_hash(struct bgp_aggregate *aggregate, } } } + +void lcommunity_set_format_json(bool make_json) +{ + lcommunity_format_json = make_json; +} + +bool lcommunity_get_format_json() +{ + return lcommunity_format_json; +} diff --git a/bgpd/bgp_lcommunity.h b/bgpd/bgp_lcommunity.h index 151b79fa63c4..e8f2a8b49dc8 100644 --- a/bgpd/bgp_lcommunity.h +++ b/bgpd/bgp_lcommunity.h @@ -78,4 +78,8 @@ extern void bgp_remove_lcomm_from_aggregate_hash( struct lcommunity *lcommunity); extern void bgp_aggr_lcommunity_remove(void *arg); +extern bool lcommunity_get_format_json(void); + +extern void lcommunity_set_format_json(bool make_json); + #endif /* _QUAGGA_BGP_LCOMMUNITY_H */ diff --git a/bgpd/bgp_routemap.c b/bgpd/bgp_routemap.c index 79c61e2ee293..68baea0a0730 100644 --- a/bgpd/bgp_routemap.c +++ b/bgpd/bgp_routemap.c @@ -6509,7 +6509,7 @@ DEFUN_YANG (set_community, XFREE(MTYPE_TMP, str); /* Set communites attribute string. */ - str = community_str(com, false, false); + str = community_str(com, community_get_format_json(), false); if (additive) { size_t argstr_sz = strlen(str) + strlen(" additive") + 1; diff --git a/bgpd/bgp_vty.c b/bgpd/bgp_vty.c index c9c7b8049644..0c496bfae48c 100644 --- a/bgpd/bgp_vty.c +++ b/bgpd/bgp_vty.c @@ -16406,7 +16406,7 @@ static void community_show_all_iterator(struct hash_bucket *bucket, com = (struct community *)bucket->data; vty_out(vty, "[%p] (%ld) %s\n", (void *)com, com->refcnt, - community_str(com, false, false)); + community_str(com, community_get_format_json(), false)); } /* Show BGP's community internal data. */ @@ -16435,7 +16435,7 @@ static void lcommunity_show_all_iterator(struct hash_bucket *bucket, lcom = (struct lcommunity *)bucket->data; vty_out(vty, "[%p] (%ld) %s\n", (void *)lcom, lcom->refcnt, - lcommunity_str(lcom, false, false)); + lcommunity_str(lcom, lcommunity_get_format_json(), false)); } /* Show BGP's community internal data. */ @@ -22190,9 +22190,11 @@ static const char *community_list_config_str(struct community_entry *entry) const char *str; if (entry->style == COMMUNITY_LIST_STANDARD) - str = community_str(entry->u.com, false, false); + str = community_str(entry->u.com, community_get_format_json(), + false); else if (entry->style == LARGE_COMMUNITY_LIST_STANDARD) - str = lcommunity_str(entry->u.lcom, false, false); + str = lcommunity_str(entry->u.lcom, + lcommunity_get_format_json(), false); else str = entry->config;