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

bgpd: generate json by default for [l]communities and aspaths #16654

Closed
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
49 changes: 29 additions & 20 deletions bgpd/bgp_aspath.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand All @@ -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);

Expand Down
1 change: 1 addition & 0 deletions bgpd/bgp_aspath.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions bgpd/bgp_attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions bgpd/bgp_clist.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
15 changes: 14 additions & 1 deletion bgpd/bgp_community.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
4 changes: 4 additions & 0 deletions bgpd/bgp_community.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
11 changes: 5 additions & 6 deletions bgpd/bgp_debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
15 changes: 14 additions & 1 deletion bgpd/bgp_lcommunity.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
4 changes: 4 additions & 0 deletions bgpd/bgp_lcommunity.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
2 changes: 1 addition & 1 deletion bgpd/bgp_routemap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 6 additions & 4 deletions bgpd/bgp_vty.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -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;

Expand Down
Loading