Skip to content

Commit

Permalink
bgpd: Optimize the way parsing communities if no community alias exists
Browse files Browse the repository at this point in the history
If at least one community alias is configured, then let's do the work,
otherwise we don't need to spend time on splitting stuff and creating
a new string.

This should improve the performance.

Signed-off-by: Donatas Abraitis <donatas@opensourcerouting.org>
  • Loading branch information
ton31337 committed Nov 18, 2024
1 parent 724624a commit 004d770
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
38 changes: 28 additions & 10 deletions bgpd/bgp_clist.c
Original file line number Diff line number Diff line change
Expand Up @@ -534,20 +534,29 @@ static bool community_regexp_match(struct community *com, regex_t *reg)
const char *str;
char *regstr;
int rv;
bool translate_alias = !!bgp_ca_alias_hash->count;

/* When there is no communities attribute it is treated as empty
string. */
if (com == NULL || com->size == 0)
str = "";
else
str = community_str(com, false, true);
return false;

regstr = bgp_alias2community_str(str);
str = community_str(com, false, translate_alias);

/* If at least one community alias is configured, then let's
* do the work, otherwise we don't need to spend time on splitting
* stuff and creating a new string.
*/
regstr = translate_alias ? bgp_alias2community_str(str) : (char *)str;

/* Regular expression match. */
rv = regexec(reg, regstr, 0, NULL, 0);

XFREE(MTYPE_TMP, regstr);
/* This is allocated by frrstr_join(), and needs to be freed
* only if it was created.
*/
if (translate_alias)
XFREE(MTYPE_TMP, regstr);

return rv == 0;
}
Expand Down Expand Up @@ -608,20 +617,29 @@ static bool lcommunity_regexp_match(struct lcommunity *com, regex_t *reg)
const char *str;
char *regstr;
int rv;
bool translate_alias = !!bgp_ca_alias_hash->count;

/* When there is no communities attribute it is treated as empty
string. */
if (com == NULL || com->size == 0)
str = "";
else
str = lcommunity_str(com, false, true);
return false;

regstr = bgp_alias2community_str(str);
str = lcommunity_str(com, false, translate_alias);

/* If at least one community alias is configured, then let's
* do the work, otherwise we don't need to spend time on splitting
* stuff and creating a new string.
*/
regstr = translate_alias ? bgp_alias2community_str(str) : (char *)str;

/* Regular expression match. */
rv = regexec(reg, regstr, 0, NULL, 0);

XFREE(MTYPE_TMP, regstr);
/* This is allocated by frrstr_join(), and needs to be freed
* only if it was created.
*/
if (translate_alias)
XFREE(MTYPE_TMP, regstr);

return rv == 0;
}
Expand Down
2 changes: 1 addition & 1 deletion bgpd/bgp_community_alias.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "bgpd/bgpd.h"
#include "bgpd/bgp_community_alias.h"

static struct hash *bgp_ca_alias_hash;
struct hash *bgp_ca_alias_hash;
static struct hash *bgp_ca_community_hash;

static unsigned int bgp_ca_community_hash_key(const void *p)
Expand Down
1 change: 1 addition & 0 deletions bgpd/bgp_community_alias.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ struct community_alias {
char alias[BUFSIZ];
};

extern struct hash *bgp_ca_alias_hash;
extern void bgp_community_alias_init(void);
extern void bgp_community_alias_finish(void);
extern struct community_alias *bgp_ca_alias_lookup(struct community_alias *ca);
Expand Down

0 comments on commit 004d770

Please sign in to comment.