diff options
| author | Naveen Thanikachalam <nthanikachal@vmware.com> | 2019-02-06 06:35:04 -0800 | 
|---|---|---|
| committer | Naveen Thanikachalam <nthanikachal@vmware.com> | 2019-02-28 20:22:41 -0800 | 
| commit | 4c99b6c2f341d223e7c39f0002d6aad3daf2cef5 (patch) | |
| tree | 52ec6b5435a933b3f0c603fad328255d8259e81a | |
| parent | 5b820d9e8c2c05711061bd499d09756150e65f9e (diff) | |
bgpd: Code to handle BGP aggregate's l-communities.
With this commit:
1) The code to manage the large-communities attribute of the routes that are
   aggregatable under a configured aggregate-address is introduced.
2) The code to compute the aggregate-route's large-communities attribute is
   introduced.
Signed-off-by: NaveenThanikachalam <nthanikachal@vmware.com>
| -rw-r--r-- | bgpd/bgp_lcommunity.c | 109 | ||||
| -rw-r--r-- | bgpd/bgp_lcommunity.h | 10 | 
2 files changed, 119 insertions, 0 deletions
diff --git a/bgpd/bgp_lcommunity.c b/bgpd/bgp_lcommunity.c index cfc9af7777..1e45897192 100644 --- a/bgpd/bgp_lcommunity.c +++ b/bgpd/bgp_lcommunity.c @@ -537,3 +537,112 @@ void lcommunity_del_val(struct lcommunity *lcom, uint8_t *ptr)  		i++;  	}  } + +static struct lcommunity *bgp_aggr_lcommunity_lookup( +						struct bgp_aggregate *aggregate, +						struct lcommunity *lcommunity) +{ +	return hash_lookup(aggregate->lcommunity_hash, lcommunity); +} + +static void *bgp_aggr_lcommunty_hash_alloc(void *p) +{ +	struct lcommunity *ref = (struct lcommunity *)p; +	struct lcommunity *lcommunity = NULL; + +	lcommunity = lcommunity_dup(ref); +	return lcommunity; +} + +static void bgp_aggr_lcommunity_prepare(struct hash_backet *hb, void *arg) +{ +	struct lcommunity *lcommerge = NULL; +	struct lcommunity *hb_lcommunity = hb->data; +	struct lcommunity **aggr_lcommunity = arg; + +	if (*aggr_lcommunity) { +		lcommerge = lcommunity_merge(*aggr_lcommunity, hb_lcommunity); +		*aggr_lcommunity = lcommunity_uniq_sort(lcommerge); +		lcommunity_free(&lcommerge); +	} else +		*aggr_lcommunity = lcommunity_dup(hb_lcommunity); +} + +void bgp_aggr_lcommunity_remove(void *arg) +{ +	struct lcommunity *lcommunity = arg; + +	lcommunity_free(&lcommunity); +} + +void bgp_compute_aggregate_lcommunity(struct bgp_aggregate *aggregate, +				      struct lcommunity *lcommunity) +{ +	struct lcommunity *aggr_lcommunity = NULL; + +	if ((aggregate == NULL) || (lcommunity == NULL)) +		return; + +	/* Create hash if not already created. +	 */ +	if (aggregate->lcommunity_hash == NULL) +		aggregate->lcommunity_hash = hash_create( +					lcommunity_hash_make, lcommunity_cmp, +					"BGP Aggregator lcommunity hash"); + +	aggr_lcommunity = bgp_aggr_lcommunity_lookup(aggregate, lcommunity); +	if (aggr_lcommunity == NULL) { +		/* Insert lcommunity into hash. +		 */ +		aggr_lcommunity = hash_get(aggregate->lcommunity_hash, +					   lcommunity, +					   bgp_aggr_lcommunty_hash_alloc); + +		/* Re-compute aggregate's lcommunity. +		 */ +		if (aggregate->lcommunity) +			lcommunity_free(&aggregate->lcommunity); + +		hash_iterate(aggregate->lcommunity_hash, +			     bgp_aggr_lcommunity_prepare, +			     &aggregate->lcommunity); +	} + +	/* Increment refernce counter. +	 */ +	aggr_lcommunity->refcnt++; +} + +void bgp_remove_lcommunity_from_aggregate(struct bgp_aggregate *aggregate, +					  struct lcommunity *lcommunity) +{ +	struct lcommunity *aggr_lcommunity = NULL; +	struct lcommunity *ret_lcomm = NULL; + +	if ((aggregate == NULL) || (lcommunity == NULL)) +		return; + +	if (aggregate->lcommunity_hash == NULL) +		return; + +	/* Look-up the lcommunity in the hash. +	 */ +	aggr_lcommunity = bgp_aggr_lcommunity_lookup(aggregate, lcommunity); +	if (aggr_lcommunity) { +		aggr_lcommunity->refcnt--; + +		if (aggr_lcommunity->refcnt == 0) { +			ret_lcomm = hash_release(aggregate->lcommunity_hash, +						 aggr_lcommunity); +			lcommunity_free(&ret_lcomm); + +			lcommunity_free(&aggregate->lcommunity); + +			/* Compute aggregate's lcommunity. +			 */ +			hash_iterate(aggregate->lcommunity_hash, +				     bgp_aggr_lcommunity_prepare, +				     &aggregate->lcommunity); +		} +	} +} diff --git a/bgpd/bgp_lcommunity.h b/bgpd/bgp_lcommunity.h index 23c777d9fd..aa4e8c69fe 100644 --- a/bgpd/bgp_lcommunity.h +++ b/bgpd/bgp_lcommunity.h @@ -22,6 +22,7 @@  #define _QUAGGA_BGP_LCOMMUNITY_H  #include "lib/json.h" +#include "bgpd/bgp_route.h"  /* Large Communities value is twelve octets long.  */  #define LCOMMUNITY_SIZE                        12 @@ -70,4 +71,13 @@ extern int lcommunity_match(const struct lcommunity *,  extern char *lcommunity_str(struct lcommunity *, bool make_json);  extern int lcommunity_include(struct lcommunity *lcom, uint8_t *ptr);  extern void lcommunity_del_val(struct lcommunity *lcom, uint8_t *ptr); + +extern void bgp_compute_aggregate_lcommunity( +					struct bgp_aggregate *aggregate, +					struct lcommunity *lcommunity); +extern void bgp_remove_lcommunity_from_aggregate( +					struct bgp_aggregate *aggregate, +					struct lcommunity *lcommunity); +extern void bgp_aggr_lcommunity_remove(void *arg); +  #endif /* _QUAGGA_BGP_LCOMMUNITY_H */  | 
