From 66dbdbd993115c57acafdb776d2165d0b4a90a45 Mon Sep 17 00:00:00 2001 From: "Ondrej Zajicek (work)" Date: Sat, 1 Oct 2016 12:50:29 +0200 Subject: BGP: Support for large communities Add support for large communities (draft-ietf-idr-large-community), 96bit alternative to RFC 1997 communities. Thanks to Matt Griswold for the original patch. --- nest/a-set.c | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- nest/attrs.h | 32 +++++++++++++++ nest/route.h | 5 ++- nest/rt-attr.c | 15 +++++++ 4 files changed, 175 insertions(+), 4 deletions(-) (limited to 'nest') diff --git a/nest/a-set.c b/nest/a-set.c index a6116022..0484ab98 100644 --- a/nest/a-set.c +++ b/nest/a-set.c @@ -116,7 +116,7 @@ int ec_set_format(struct adata *set, int from, byte *buf, uint size) { u32 *z = int_set_get_data(set); - byte *end = buf + size - 24; + byte *end = buf + size - 64; int from2 = MAX(from, 0); int to = int_set_get_size(set); int i; @@ -141,6 +141,43 @@ ec_set_format(struct adata *set, int from, byte *buf, uint size) return 0; } +int +lc_format(byte *buf, lcomm lc) +{ + return bsprintf(buf, "(%d, %d, %d)", lc.asn, lc.ldp1, lc.ldp2); +} + +int +lc_set_format(struct adata *set, int from, byte *buf, uint bufsize) +{ + u32 *d = (u32 *) set->data; + byte *end = buf + bufsize - 64; + int from2 = MAX(from, 0); + int to = set->length / 4; + int i; + + for (i = from2; i < to; i += 3) + { + if (buf > end) + { + if (from < 0) + strcpy(buf, "..."); + else + buf[-1] = 0; + return i; + } + + buf += bsprintf(buf, "(%d, %d, %d)", d[i], d[i+1], d[i+2]); + *buf++ = ' '; + } + + if (i != from2) + buf--; + + *buf = 0; + return 0; +} + int int_set_contains(struct adata *list, u32 val) { @@ -177,6 +214,24 @@ ec_set_contains(struct adata *list, u64 val) return 0; } +int +lc_set_contains(struct adata *list, lcomm val) +{ + if (!list) + return 0; + + u32 *l = int_set_get_data(list); + int len = int_set_get_size(list); + int i; + + for (i = 0; i < len; i += 3) + if (lc_match(l, i, val)) + return 1; + + return 0; +} + + struct adata * int_set_add(struct linpool *pool, struct adata *list, u32 val) { @@ -208,13 +263,30 @@ ec_set_add(struct linpool *pool, struct adata *list, u64 val) if (list) memcpy(res->data, list->data, list->length); - u32 *l = (u32 *) (res->data + res->length - 8); + u32 *l = (u32 *) (res->data + olen); l[0] = ec_hi(val); l[1] = ec_lo(val); return res; } +struct adata * +lc_set_add(struct linpool *pool, struct adata *list, lcomm val) +{ + if (lc_set_contains(list, val)) + return list; + + int olen = list ? list->length : 0; + struct adata *res = lp_alloc(pool, sizeof(struct adata) + olen + LCOMM_LENGTH); + res->length = olen + LCOMM_LENGTH; + + if (list) + memcpy(res->data, list->data, list->length); + + lc_put((u32 *) (res->data + olen), val); + + return res; +} struct adata * int_set_del(struct linpool *pool, struct adata *list, u32 val) @@ -265,6 +337,27 @@ ec_set_del(struct linpool *pool, struct adata *list, u64 val) return res; } +struct adata * +lc_set_del(struct linpool *pool, struct adata *list, lcomm val) +{ + if (!lc_set_contains(list, val)) + return list; + + struct adata *res; + res = lp_alloc(pool, sizeof(struct adata) + list->length - LCOMM_LENGTH); + res->length = list->length - LCOMM_LENGTH; + + u32 *l = int_set_get_data(list); + u32 *k = int_set_get_data(res); + int len = int_set_get_size(list); + int i; + + for (i=0; i < len; i += 3) + if (! lc_match(l, i, val)) + k = lc_copy(k, l+i); + + return res; +} struct adata * int_set_union(struct linpool *pool, struct adata *l1, struct adata *l2) @@ -328,3 +421,33 @@ ec_set_union(struct linpool *pool, struct adata *l1, struct adata *l2) memcpy(res->data + l1->length, tmp, len); return res; } + +struct adata * +lc_set_union(struct linpool *pool, struct adata *l1, struct adata *l2) +{ + if (!l1) + return l2; + if (!l2) + return l1; + + struct adata *res; + int len = int_set_get_size(l2); + u32 *l = int_set_get_data(l2); + u32 tmp[len]; + u32 *k = tmp; + int i; + + for (i = 0; i < len; i += 3) + if (!lc_set_contains(l1, lc_get(l, i))) + k = lc_copy(k, l+i); + + if (k == tmp) + return l1; + + len = (k - tmp) * 4; + res = lp_alloc(pool, sizeof(struct adata) + l1->length + len); + res->length = l1->length + len; + memcpy(res->data, l1->data, l1->length); + memcpy(res->data + l1->length, tmp, len); + return res; +} diff --git a/nest/attrs.h b/nest/attrs.h index 670b048f..548d71a9 100644 --- a/nest/attrs.h +++ b/nest/attrs.h @@ -68,6 +68,7 @@ int as_path_match(struct adata *path, struct f_path_mask *mask); /* Transitive bit (for first u32 half of EC) */ #define EC_TBIT 0x40000000 +#define ECOMM_LENGTH 8 static inline int int_set_get_size(struct adata *list) { return list->length / 4; } @@ -75,6 +76,9 @@ static inline int int_set_get_size(struct adata *list) static inline int ec_set_get_size(struct adata *list) { return list->length / 8; } +static inline int lc_set_get_size(struct adata *list) +{ return list->length / 12; } + static inline u32 *int_set_get_data(struct adata *list) { return (u32 *) list->data; } @@ -98,17 +102,45 @@ static inline u64 ec_ip4(u64 kind, u64 key, u64 val) static inline u64 ec_generic(u64 key, u64 val) { return (key << 32) | val; } +/* Large community value */ +typedef struct lcomm { + u32 asn; + u32 ldp1; + u32 ldp2; +} lcomm; + +#define LCOMM_LENGTH 12 + +static inline lcomm lc_get(const u32 *l, int i) +{ return (lcomm) { l[i], l[i+1], l[i+2] }; } + +static inline void lc_put(u32 *l, lcomm v) +{ l[0] = v.asn; l[1] = v.ldp1; l[2] = v.ldp2; } + +static inline int lc_match(const u32 *l, int i, lcomm v) +{ return (l[i] == v.asn && l[i+1] == v.ldp1 && l[i+2] == v.ldp2); } + +static inline u32 *lc_copy(u32 *dst, const u32 *src) +{ memcpy(dst, src, LCOMM_LENGTH); return dst + 3; } + + int int_set_format(struct adata *set, int way, int from, byte *buf, uint size); int ec_format(byte *buf, u64 ec); int ec_set_format(struct adata *set, int from, byte *buf, uint size); +int lc_format(byte *buf, lcomm lc); +int lc_set_format(struct adata *set, int from, byte *buf, uint size); int int_set_contains(struct adata *list, u32 val); int ec_set_contains(struct adata *list, u64 val); +int lc_set_contains(struct adata *list, lcomm val); struct adata *int_set_add(struct linpool *pool, struct adata *list, u32 val); struct adata *ec_set_add(struct linpool *pool, struct adata *list, u64 val); +struct adata *lc_set_add(struct linpool *pool, struct adata *list, lcomm val); struct adata *int_set_del(struct linpool *pool, struct adata *list, u32 val); struct adata *ec_set_del(struct linpool *pool, struct adata *list, u64 val); +struct adata *lc_set_del(struct linpool *pool, struct adata *list, lcomm val); struct adata *int_set_union(struct linpool *pool, struct adata *l1, struct adata *l2); struct adata *ec_set_union(struct linpool *pool, struct adata *l1, struct adata *l2); +struct adata *lc_set_union(struct linpool *pool, struct adata *l1, struct adata *l2); #endif diff --git a/nest/route.h b/nest/route.h index 2fcb189a..07320566 100644 --- a/nest/route.h +++ b/nest/route.h @@ -442,7 +442,7 @@ typedef struct eattr { #define EA_ALLOW_UNDEF 0x10000 /* ea_find: allow EAF_TYPE_UNDEF */ #define EA_BIT(n) ((n) << 24) /* Used in bitfield accessors */ -#define EAF_TYPE_MASK 0x0f /* Mask with this to get type */ +#define EAF_TYPE_MASK 0x1f /* Mask with this to get type */ #define EAF_TYPE_INT 0x01 /* 32-bit unsigned integer number */ #define EAF_TYPE_OPAQUE 0x02 /* Opaque byte string (not filterable) */ #define EAF_TYPE_IP_ADDRESS 0x04 /* IP address */ @@ -451,7 +451,8 @@ typedef struct eattr { #define EAF_TYPE_BITFIELD 0x09 /* 32-bit embedded bitfield */ #define EAF_TYPE_INT_SET 0x0a /* Set of u32's (e.g., a community list) */ #define EAF_TYPE_EC_SET 0x0e /* Set of pairs of u32's - ext. community list */ -#define EAF_TYPE_UNDEF 0x0f /* `force undefined' entry */ +#define EAF_TYPE_LC_SET 0x12 /* Set of triplets of u32's - large community list */ +#define EAF_TYPE_UNDEF 0x1f /* `force undefined' entry */ #define EAF_EMBEDDED 0x01 /* Data stored in eattr.u.data (part of type spec) */ #define EAF_VAR_LENGTH 0x02 /* Attribute length is variable (part of type spec) */ #define EAF_ORIGINATED 0x40 /* The attribute has originated locally */ diff --git a/nest/rt-attr.c b/nest/rt-attr.c index ab0069b5..9aeca846 100644 --- a/nest/rt-attr.c +++ b/nest/rt-attr.c @@ -828,6 +828,18 @@ ea_show_ec_set(struct cli *c, struct adata *ad, byte *pos, byte *buf, byte *end) } } +static inline void +ea_show_lc_set(struct cli *c, struct adata *ad, byte *pos, byte *buf, byte *end) +{ + int i = lc_set_format(ad, 0, pos, end - pos); + cli_printf(c, -1012, "\t%s", buf); + while (i) + { + i = lc_set_format(ad, i, buf, end - buf - 1); + cli_printf(c, -1012, "\t\t%s", buf); + } +} + /** * ea_show - print an &eattr to CLI * @c: destination CLI @@ -892,6 +904,9 @@ ea_show(struct cli *c, eattr *e) case EAF_TYPE_EC_SET: ea_show_ec_set(c, ad, pos, buf, end); return; + case EAF_TYPE_LC_SET: + ea_show_lc_set(c, ad, pos, buf, end); + return; case EAF_TYPE_UNDEF: default: bsprintf(pos, "", e->type); -- cgit v1.2.3