bgpd: Optimize the way parsing communities if no community alias exists

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>
This commit is contained in:
Donatas Abraitis 2024-11-18 23:29:53 +02:00
parent 724624a35c
commit 004d770ec4
3 changed files with 30 additions and 11 deletions

View file

@ -534,19 +534,28 @@ static bool community_regexp_match(struct community *com, regex_t *reg)
const char *str; const char *str;
char *regstr; char *regstr;
int rv; int rv;
bool translate_alias = !!bgp_ca_alias_hash->count;
/* When there is no communities attribute it is treated as empty /* When there is no communities attribute it is treated as empty
string. */ string. */
if (com == NULL || com->size == 0) if (com == NULL || com->size == 0)
str = ""; return false;
else
str = community_str(com, false, true);
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. */ /* Regular expression match. */
rv = regexec(reg, regstr, 0, NULL, 0); rv = regexec(reg, regstr, 0, NULL, 0);
/* This is allocated by frrstr_join(), and needs to be freed
* only if it was created.
*/
if (translate_alias)
XFREE(MTYPE_TMP, regstr); XFREE(MTYPE_TMP, regstr);
return rv == 0; return rv == 0;
@ -608,19 +617,28 @@ static bool lcommunity_regexp_match(struct lcommunity *com, regex_t *reg)
const char *str; const char *str;
char *regstr; char *regstr;
int rv; int rv;
bool translate_alias = !!bgp_ca_alias_hash->count;
/* When there is no communities attribute it is treated as empty /* When there is no communities attribute it is treated as empty
string. */ string. */
if (com == NULL || com->size == 0) if (com == NULL || com->size == 0)
str = ""; return false;
else
str = lcommunity_str(com, false, true);
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. */ /* Regular expression match. */
rv = regexec(reg, regstr, 0, NULL, 0); rv = regexec(reg, regstr, 0, NULL, 0);
/* This is allocated by frrstr_join(), and needs to be freed
* only if it was created.
*/
if (translate_alias)
XFREE(MTYPE_TMP, regstr); XFREE(MTYPE_TMP, regstr);
return rv == 0; return rv == 0;

View file

@ -13,7 +13,7 @@
#include "bgpd/bgpd.h" #include "bgpd/bgpd.h"
#include "bgpd/bgp_community_alias.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 struct hash *bgp_ca_community_hash;
static unsigned int bgp_ca_community_hash_key(const void *p) static unsigned int bgp_ca_community_hash_key(const void *p)

View file

@ -17,6 +17,7 @@ struct community_alias {
char alias[BUFSIZ]; char alias[BUFSIZ];
}; };
extern struct hash *bgp_ca_alias_hash;
extern void bgp_community_alias_init(void); extern void bgp_community_alias_init(void);
extern void bgp_community_alias_finish(void); extern void bgp_community_alias_finish(void);
extern struct community_alias *bgp_ca_alias_lookup(struct community_alias *ca); extern struct community_alias *bgp_ca_alias_lookup(struct community_alias *ca);