mirror of
https://gitlab.nic.cz/labs/bird.git
synced 2024-11-09 12:48:43 +00:00
Basic route aggregation
Add a new protocol offering route aggregation. User can specify list of route attributes in the configuration file and run route aggregation on the export side of the pipe protocol. Routes are sorted and for every group of equivalent routes new route is created and exported to the routing table. It is also possible to specify filter which will run for every route before aggregation. Furthermore, it will be possible to set attributes of new routes according to attributes of the aggregated routes. This is a work in progress. Original work by Igor Putovny, subsequent cleanups and finalization by Maria Matejka. This is a split-commit of the neighboring aggregator branch with a bit improved lvalue handling, to have easier merge into v3.
This commit is contained in:
parent
ba91f4c831
commit
f42c118aa7
@ -95,6 +95,7 @@ CF_DECLS
|
||||
struct timeformat *tf;
|
||||
mpls_label_stack *mls;
|
||||
const struct adata *bs;
|
||||
struct aggr_item_node *ai;
|
||||
}
|
||||
|
||||
%token END CLI_MARKER INVALID_TOKEN ELSECOL DDOT
|
||||
|
@ -312,7 +312,7 @@ if test "$enable_mpls_kernel" != no ; then
|
||||
fi
|
||||
fi
|
||||
|
||||
all_protocols="$proto_bfd babel bgp mrt ospf perf pipe radv rip rpki static"
|
||||
all_protocols="aggregator $proto_bfd babel bgp mrt ospf perf pipe radv rip rpki static"
|
||||
|
||||
all_protocols=`echo $all_protocols | sed 's/ /,/g'`
|
||||
|
||||
@ -320,6 +320,7 @@ if test "$with_protocols" = all ; then
|
||||
with_protocols="$all_protocols"
|
||||
fi
|
||||
|
||||
AH_TEMPLATE([CONFIG_AGGREGATOR],[Aggregator protocol])
|
||||
AH_TEMPLATE([CONFIG_BABEL], [Babel protocol])
|
||||
AH_TEMPLATE([CONFIG_BFD], [BFD protocol])
|
||||
AH_TEMPLATE([CONFIG_BGP], [BGP protocol])
|
||||
|
@ -1905,6 +1905,70 @@ protocol sections.
|
||||
<chapt>Protocols
|
||||
<label id="protocols">
|
||||
|
||||
<sect>Aggregator
|
||||
<label id="aggregator">
|
||||
|
||||
<sect1>Introduction
|
||||
<label id="aggregator-intro">
|
||||
<p>The Aggregator protocol explicitly merges routes by the given rules. There
|
||||
are four phases of aggregation. First routes are filtered, then sorted into buckets,
|
||||
then buckets are merged and finally the results are filtered once again.
|
||||
Aggregating an already aggregated route is forbidden.
|
||||
|
||||
<p>This is an experimental protocol, use with caution.
|
||||
|
||||
<sect1>Configuration
|
||||
<label id="aggregator-config">
|
||||
<p><descrip>
|
||||
<tag><label id="aggregator-table">table <m/table/</tag>
|
||||
The table from which routes are exported to get aggregated.
|
||||
|
||||
<tag><label id="aggregator-export">export <m/.../</tag>
|
||||
A standard channel's <cf/export/ clause, defining which routes are accepted into aggregation.
|
||||
|
||||
<tag><label id="aggregator-rule">aggregate on <m/expr/ | <m/attribute/ [<m/, .../]</tag>
|
||||
All the given filter expressions and route attributes are evaluated for each route. Then routes
|
||||
are sorted into buckets where <em/all/ values are the same. Note: due to performance reasons,
|
||||
all filter expressions must return a compact type, e.g. integer, a BGP
|
||||
(standard, extended, large) community or an IP address. If you need to compare e.g. modified
|
||||
AS Paths in the aggregation rule, you can define a custom route attribute and set this attribute
|
||||
in the export filter. For now, it's mandatory to say <cf/net/ here, we can't merge prefixes yet.
|
||||
|
||||
<tag><label id="aggregation-merge">merge by { <m/filter code/ }</tag>
|
||||
The given filter code has an extra symbol defined: <cf/routes/. By iterating over <cf/routes/,
|
||||
you get all the routes in the bucket and you can construct your new route. All attributes
|
||||
selected in <cf/aggregate on/ are already set to the common values. For now, it's not possible
|
||||
to use a named filter here. You have to finalize the route by calling <cf/accept/.
|
||||
|
||||
<tag><label id="aggregator-import">import <m/.../</tag>
|
||||
Filter applied to the route after <cf/merge by/. Here you can use a named filter.
|
||||
|
||||
<tag><label id="aggregator-peer-table">peer table <m/table/</tag>
|
||||
The table to which aggregated routes are imported. It may be the same table
|
||||
as <cf/table/.
|
||||
</descrip>
|
||||
|
||||
<sect1>Example
|
||||
<label id="aggregator-example">
|
||||
|
||||
<p><code>
|
||||
protocol aggregator {
|
||||
table master6;
|
||||
export where defined(bgp_path);
|
||||
/* Merge all routes with the same AS Path length */
|
||||
aggregate on net, bgp_path.len;
|
||||
merge by {
|
||||
for route r in routes do {
|
||||
if ! defined(bgp_path) then { bgp_path = r.bgp_path }
|
||||
bgp_community = bgp_community.add(r.bgp_community);
|
||||
}
|
||||
accept;
|
||||
};
|
||||
import all;
|
||||
peer table agr_result;
|
||||
}
|
||||
</code>
|
||||
|
||||
<sect>Babel
|
||||
<label id="babel">
|
||||
|
||||
|
@ -210,6 +210,8 @@ val_compare(const struct f_val *v1, const struct f_val *v2)
|
||||
return net_compare(v1->val.net, v2->val.net);
|
||||
case T_STRING:
|
||||
return strcmp(v1->val.s, v2->val.s);
|
||||
case T_PATH:
|
||||
return as_path_compare(v1->val.ad, v2->val.ad);
|
||||
case T_ROUTE:
|
||||
case T_ROUTES_BLOCK:
|
||||
default:
|
||||
@ -682,7 +684,6 @@ rte_block_format(const struct rte *rte, buffer *buf)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* val_format - format filter value
|
||||
*/
|
||||
|
@ -215,6 +215,14 @@ mem_hash_mix(u64 *h, const void *p, uint s)
|
||||
*h = *h * multiplier + pp[i];
|
||||
}
|
||||
|
||||
static inline void
|
||||
mem_hash_mix_str(u64 *h, const char *s)
|
||||
{
|
||||
const u64 multiplier = 0xb38bc09a61202731ULL;
|
||||
while (s)
|
||||
*h = *h * multiplier + *s++;
|
||||
}
|
||||
|
||||
static inline void
|
||||
mem_hash_mix_num(u64 *h, u64 val)
|
||||
{
|
||||
|
@ -669,6 +669,29 @@ as_path_filter(struct linpool *pool, const struct adata *path, const struct f_va
|
||||
return res;
|
||||
}
|
||||
|
||||
int
|
||||
as_path_compare(const struct adata *path1, const struct adata *path2)
|
||||
{
|
||||
uint pos1 = 0;
|
||||
uint pos2 = 0;
|
||||
uint val1 = 0;
|
||||
uint val2 = 0;
|
||||
|
||||
while (1)
|
||||
{
|
||||
int res1 = as_path_walk(path1, &pos1, &val1);
|
||||
int res2 = as_path_walk(path2, &pos2, &val2);
|
||||
|
||||
if (res1 == 0 && res2 == 0)
|
||||
return 0;
|
||||
|
||||
if (val1 == val2)
|
||||
continue;
|
||||
|
||||
return val1 < val2 ? -1 : 1;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
as_path_walk(const struct adata *path, uint *pos, uint *val)
|
||||
{
|
||||
|
@ -51,6 +51,7 @@ u32 as_path_get_last_nonaggregated(const struct adata *path);
|
||||
int as_path_contains(const struct adata *path, u32 as, int min);
|
||||
int as_path_match_set(const struct adata *path, const struct f_tree *set);
|
||||
const struct adata *as_path_filter(struct linpool *pool, const struct adata *path, const struct f_val *set, int pos);
|
||||
int as_path_compare(const struct adata *path1, const struct adata *path2);
|
||||
int as_path_walk(const struct adata *path, uint *pos, uint *val);
|
||||
|
||||
static inline struct adata *as_path_prepend(struct linpool *pool, const struct adata *path, u32 as)
|
||||
@ -238,6 +239,7 @@ int lc_set_max(const struct adata *list, lcomm *val);
|
||||
int int_set_walk(const struct adata *list, uint *pos, u32 *val);
|
||||
int ec_set_walk(const struct adata *list, uint *pos, u64 *val);
|
||||
int lc_set_walk(const struct adata *list, uint *pos, lcomm *val);
|
||||
int rte_set_walk(const struct adata *list, u32 *pos, struct rte **val);
|
||||
|
||||
void ec_set_sort_x(struct adata *set); /* Sort in place */
|
||||
|
||||
|
@ -90,7 +90,6 @@ proto_log_state_change(struct proto *p)
|
||||
p->last_state_name_announced = NULL;
|
||||
}
|
||||
|
||||
|
||||
struct channel_config *
|
||||
proto_cf_find_channel(struct proto_config *pc, uint net_type)
|
||||
{
|
||||
|
@ -39,6 +39,7 @@ struct symbol;
|
||||
|
||||
enum protocol_class {
|
||||
PROTOCOL_NONE,
|
||||
PROTOCOL_AGGREGATOR,
|
||||
PROTOCOL_BABEL,
|
||||
PROTOCOL_BFD,
|
||||
PROTOCOL_BGP,
|
||||
@ -103,7 +104,7 @@ void protos_dump_all(void);
|
||||
|
||||
extern struct protocol
|
||||
proto_device, proto_radv, proto_rip, proto_static, proto_mrt,
|
||||
proto_ospf, proto_perf,
|
||||
proto_ospf, proto_perf, proto_aggregator,
|
||||
proto_pipe, proto_bgp, proto_bmp, proto_bfd, proto_babel, proto_rpki;
|
||||
|
||||
/*
|
||||
|
@ -344,6 +344,8 @@ void rt_prune_sync(rtable *t, int all);
|
||||
int rte_update_out(struct channel *c, const net_addr *n, rte *new, rte *old0, int refeed);
|
||||
struct rtable_config *rt_new_table(struct symbol *s, uint addr_type);
|
||||
|
||||
int rte_same(rte *x, rte *y);
|
||||
|
||||
static inline int rt_is_ip(rtable *tab)
|
||||
{ return (tab->addr_type == NET_IP4) || (tab->addr_type == NET_IP6); }
|
||||
|
||||
@ -476,7 +478,8 @@ typedef struct rta {
|
||||
#define RTS_BABEL 13 /* Babel route */
|
||||
#define RTS_RPKI 14 /* Route Origin Authorization */
|
||||
#define RTS_PERF 15 /* Perf checker */
|
||||
#define RTS_MAX 16
|
||||
#define RTS_AGGREGATED 16 /* Aggregated route */
|
||||
#define RTS_MAX 17
|
||||
|
||||
#define RTD_NONE 0 /* Undefined next hop */
|
||||
#define RTD_UNICAST 1 /* Next hop is neighbor router */
|
||||
|
@ -75,6 +75,8 @@ const char * const rta_src_names[RTS_MAX] = {
|
||||
[RTS_PIPE] = "pipe",
|
||||
[RTS_BABEL] = "Babel",
|
||||
[RTS_RPKI] = "RPKI",
|
||||
[RTS_PERF] = "Perf",
|
||||
[RTS_AGGREGATED] = "aggregated",
|
||||
};
|
||||
|
||||
const char * rta_dest_names[RTD_MAX] = {
|
||||
@ -1272,7 +1274,8 @@ rta_dump(rta *a)
|
||||
static char *rts[] = { "", "RTS_STATIC", "RTS_INHERIT", "RTS_DEVICE",
|
||||
"RTS_STAT_DEV", "RTS_REDIR", "RTS_RIP",
|
||||
"RTS_OSPF", "RTS_OSPF_IA", "RTS_OSPF_EXT1",
|
||||
"RTS_OSPF_EXT2", "RTS_BGP", "RTS_PIPE", "RTS_BABEL" };
|
||||
"RTS_OSPF_EXT2", "RTS_BGP", "RTS_PIPE", "RTS_BABEL",
|
||||
"RTS_RPKI", "RTS_PERF", "RTS_AGGREGATED", };
|
||||
static char *rtd[] = { "", " DEV", " HOLE", " UNREACH", " PROHIBIT" };
|
||||
|
||||
debug("pref=%d uc=%d %s %s%s h=%04x",
|
||||
|
@ -975,7 +975,6 @@ rt_export_merged(struct channel *c, net *net, rte **rt_free, linpool *pool, int
|
||||
return best;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
rt_notify_merged(struct channel *c, net *net, rte *new_changed, rte *old_changed,
|
||||
rte *new_best, rte *old_best, int refeed)
|
||||
|
1
proto/aggregator/Doc
Normal file
1
proto/aggregator/Doc
Normal file
@ -0,0 +1 @@
|
||||
S aggregator.c
|
6
proto/aggregator/Makefile
Normal file
6
proto/aggregator/Makefile
Normal file
@ -0,0 +1,6 @@
|
||||
src := aggregator.c
|
||||
obj := $(src-o-files)
|
||||
$(all-daemon)
|
||||
$(cf-local)
|
||||
|
||||
tests_objs := $(tests_objs) $(src-o-files)
|
808
proto/aggregator/aggregator.c
Normal file
808
proto/aggregator/aggregator.c
Normal file
@ -0,0 +1,808 @@
|
||||
/*
|
||||
* BIRD Internet Routing Daemon -- Route aggregation
|
||||
*
|
||||
* (c) 2023--2023 Igor Putovny <igor.putovny@nic.cz>
|
||||
* (c) 2023 CZ.NIC, z.s.p.o.
|
||||
*
|
||||
* Can be freely distributed and used under the terms of the GNU GPL.
|
||||
*/
|
||||
|
||||
/**
|
||||
* DOC: Route aggregation
|
||||
*
|
||||
* This is an implementation of route aggregation functionality.
|
||||
* It enables user to specify a set of route attributes in the configuarion file
|
||||
* and then, for a given destination (net), aggregate routes with the same
|
||||
* values of these attributes into a single multi-path route.
|
||||
*
|
||||
* Structure &channel contains pointer to aggregation list which is represented
|
||||
* by &aggr_list_linearized. In rt_notify_aggregated(), attributes from this
|
||||
* list are evaluated for every route of a given net and results are stored
|
||||
* in &rte_val_list which contains pointer to this route and array of &f_val.
|
||||
* Array of pointers to &rte_val_list entries is sorted using
|
||||
* sort_rte_val_list(). For comparison of &f_val structures, val_compare()
|
||||
* is used. Comparator function is written so that sorting is stable. If all
|
||||
* attributes have the same values, routes are compared by their global IDs.
|
||||
*
|
||||
* After sorting, &rte_val_list entries containing equivalent routes will be
|
||||
* adjacent to each other. Function process_rte_list() iterates through these
|
||||
* entries to identify sequences of equivalent routes. New route will be
|
||||
* created for each such sequence, even if only from a single route.
|
||||
* Only attributes from the aggreagation list will be set for the new route.
|
||||
* New &rta is created and prepare_rta() is used to copy static and dynamic
|
||||
* attributes to new &rta from &rta of the original route. New route is created
|
||||
* by create_merged_rte() from new &rta and exported to the routing table.
|
||||
*/
|
||||
|
||||
#undef LOCAL_DEBUG
|
||||
|
||||
#ifndef _GNU_SOURCE
|
||||
#define _GNU_SOURCE
|
||||
#endif
|
||||
|
||||
#include "nest/bird.h"
|
||||
#include "nest/iface.h"
|
||||
#include "filter/filter.h"
|
||||
#include "aggregator.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
/*
|
||||
#include "nest/route.h"
|
||||
#include "nest/iface.h"
|
||||
#include "lib/resource.h"
|
||||
#include "lib/event.h"
|
||||
#include "lib/timer.h"
|
||||
#include "lib/string.h"
|
||||
#include "conf/conf.h"
|
||||
#include "filter/filter.h"
|
||||
#include "filter/data.h"
|
||||
#include "lib/hash.h"
|
||||
#include "lib/string.h"
|
||||
#include "lib/alloca.h"
|
||||
#include "lib/flowspec.h"
|
||||
*/
|
||||
|
||||
/*
|
||||
* Set static attribute in @rta from static attribute in @old according to @sa.
|
||||
*/
|
||||
static void
|
||||
rta_set_static_attr(struct rta *rta, const struct rta *old, struct f_static_attr sa)
|
||||
{
|
||||
switch (sa.sa_code)
|
||||
{
|
||||
case SA_NET:
|
||||
break;
|
||||
|
||||
case SA_FROM:
|
||||
rta->from = old->from;
|
||||
break;
|
||||
|
||||
case SA_GW:
|
||||
rta->dest = RTD_UNICAST;
|
||||
rta->nh.gw = old->nh.gw;
|
||||
rta->nh.iface = old->nh.iface;
|
||||
rta->nh.next = NULL;
|
||||
rta->hostentry = NULL;
|
||||
rta->nh.labels = 0;
|
||||
break;
|
||||
|
||||
case SA_SCOPE:
|
||||
rta->scope = old->scope;
|
||||
break;
|
||||
|
||||
case SA_DEST:
|
||||
rta->dest = old->dest;
|
||||
rta->nh.gw = IPA_NONE;
|
||||
rta->nh.iface = NULL;
|
||||
rta->nh.next = NULL;
|
||||
rta->hostentry = NULL;
|
||||
rta->nh.labels = 0;
|
||||
break;
|
||||
|
||||
case SA_IFNAME:
|
||||
rta->dest = RTD_UNICAST;
|
||||
rta->nh.gw = IPA_NONE;
|
||||
rta->nh.iface = old->nh.iface;
|
||||
rta->nh.next = NULL;
|
||||
rta->hostentry = NULL;
|
||||
rta->nh.labels = 0;
|
||||
break;
|
||||
|
||||
case SA_GW_MPLS:
|
||||
rta->nh.labels = old->nh.labels;
|
||||
memcpy(&rta->nh.label, &old->nh.label, sizeof(u32) * old->nh.labels);
|
||||
break;
|
||||
|
||||
case SA_WEIGHT:
|
||||
rta->nh.weight = old->nh.weight;
|
||||
break;
|
||||
|
||||
case SA_PREF:
|
||||
rta->pref = old->pref;
|
||||
break;
|
||||
|
||||
default:
|
||||
bug("Invalid static attribute access (%u/%u)", sa.f_type, sa.sa_code);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Compare list of &f_val entries.
|
||||
* @count: number of &f_val entries
|
||||
*/
|
||||
static int
|
||||
same_val_list(const struct f_val *v1, const struct f_val *v2, uint len)
|
||||
{
|
||||
for (uint i = 0; i < len; i++)
|
||||
if (!val_same(&v1[i], &v2[i]))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Create and export new merged route.
|
||||
* @old: first route in a sequence of equivalent routes that are to be merged
|
||||
* @rte_val: first element in a sequence of equivalent rte_val_list entries
|
||||
* @length: number of equivalent routes that are to be merged (at least 1)
|
||||
* @ail: aggregation list
|
||||
*/
|
||||
static void
|
||||
aggregator_bucket_update(struct aggregator_proto *p, struct aggregator_bucket *bucket, struct network *net)
|
||||
{
|
||||
/* Empty bucket */
|
||||
if (!bucket->rte)
|
||||
{
|
||||
rte_update2(p->dst, net->n.addr, NULL, bucket->last_src);
|
||||
bucket->last_src = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
/* Allocate RTA and EA list */
|
||||
struct rta *rta = allocz(rta_size(bucket->rte->attrs));
|
||||
rta->dest = RTD_UNREACHABLE;
|
||||
rta->source = RTS_AGGREGATED;
|
||||
rta->scope = SCOPE_UNIVERSE;
|
||||
|
||||
struct ea_list *eal = allocz(sizeof(struct ea_list) + sizeof(struct eattr) * p->aggr_on_da_count);
|
||||
eal->next = NULL;
|
||||
eal->count = 0;
|
||||
rta->eattrs = eal;
|
||||
|
||||
/* Seed the attributes from aggregator rule */
|
||||
for (uint i = 0; i < p->aggr_on_count; i++)
|
||||
{
|
||||
if (p->aggr_on[i].type == AGGR_ITEM_DYNAMIC_ATTR)
|
||||
{
|
||||
u32 ea_code = p->aggr_on[i].da.ea_code;
|
||||
const struct eattr *e = ea_find(bucket->rte->attrs->eattrs, ea_code);
|
||||
|
||||
if (e)
|
||||
eal->attrs[eal->count++] = *e;
|
||||
}
|
||||
else if (p->aggr_on[i].type == AGGR_ITEM_STATIC_ATTR)
|
||||
rta_set_static_attr(rta, bucket->rte->attrs, p->aggr_on[i].sa);
|
||||
}
|
||||
|
||||
struct rte *new = rte_get_temp(rta, bucket->rte->src);
|
||||
new->net = net;
|
||||
|
||||
/*
|
||||
log("=============== CREATE MERGED ROUTE ===============");
|
||||
log("New route created: id = %d, protocol: %s", new->src->global_id, new->src->proto->name);
|
||||
log("===================================================");
|
||||
*/
|
||||
|
||||
/* merge filter needs one argument called "routes" */
|
||||
struct f_val val = {
|
||||
.type = T_ROUTES_BLOCK,
|
||||
.val.rte = bucket->rte,
|
||||
};
|
||||
|
||||
struct lp_state tmp_state;
|
||||
lp_save(tmp_linpool, &tmp_state);
|
||||
|
||||
/* Actually run the filter */
|
||||
enum filter_return fret = f_eval_rte(p->merge_by, &new, tmp_linpool, 1, &val, 0);
|
||||
|
||||
/* Src must be stored now, rte_update2() may return new */
|
||||
struct rte_src *new_src = new ? new->src : NULL;
|
||||
|
||||
/* Finally import the route */
|
||||
switch (fret)
|
||||
{
|
||||
/* Pass the route to the protocol */
|
||||
case F_ACCEPT:
|
||||
rte_update2(p->dst, net->n.addr, new, bucket->last_src ?: new->src);
|
||||
break;
|
||||
|
||||
/* Something bad happened */
|
||||
default:
|
||||
ASSERT_DIE(fret == F_ERROR);
|
||||
/* fall through */
|
||||
|
||||
/* We actually don't want this route */
|
||||
case F_REJECT:
|
||||
if (bucket->last_src)
|
||||
rte_update2(p->dst, net->n.addr, NULL, bucket->last_src);
|
||||
break;
|
||||
}
|
||||
|
||||
/* Switch source lock for bucket->last_src */
|
||||
if (bucket->last_src != new_src)
|
||||
{
|
||||
if (new_src)
|
||||
rt_lock_source(new_src);
|
||||
if (bucket->last_src)
|
||||
rt_unlock_source(bucket->last_src);
|
||||
|
||||
bucket->last_src = new_src;
|
||||
}
|
||||
|
||||
lp_restore(tmp_linpool, &tmp_state);
|
||||
}
|
||||
|
||||
/*
|
||||
* Reload all the buckets on reconfiguration if merge filter has changed.
|
||||
* TODO: make this splitted
|
||||
*/
|
||||
static void
|
||||
aggregator_reload_buckets(void *data)
|
||||
{
|
||||
struct aggregator_proto *p = data;
|
||||
|
||||
HASH_WALK(p->buckets, next_hash, b)
|
||||
if (b->rte)
|
||||
aggregator_bucket_update(p, b, b->rte->net);
|
||||
HASH_WALK_END;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Evaluate static attribute of @rt1 according to @sa
|
||||
* and store result in @pos.
|
||||
*/
|
||||
static void
|
||||
eval_static_attr(const struct rte *rt1, struct f_static_attr sa, struct f_val *pos)
|
||||
{
|
||||
const struct rta *rta = rt1->attrs;
|
||||
|
||||
#define RESULT(_type, value, result) \
|
||||
do { \
|
||||
pos->type = _type; \
|
||||
pos->val.value = result; \
|
||||
} while (0)
|
||||
|
||||
switch (sa.sa_code)
|
||||
{
|
||||
case SA_NET: RESULT(sa.f_type, net, rt1->net->n.addr); break;
|
||||
case SA_FROM: RESULT(sa.f_type, ip, rta->from); break;
|
||||
case SA_GW: RESULT(sa.f_type, ip, rta->nh.gw); break;
|
||||
case SA_PROTO: RESULT(sa.f_type, s, rt1->src->proto->name); break;
|
||||
case SA_SOURCE: RESULT(sa.f_type, i, rta->source); break;
|
||||
case SA_SCOPE: RESULT(sa.f_type, i, rta->scope); break;
|
||||
case SA_DEST: RESULT(sa.f_type, i, rta->dest); break;
|
||||
case SA_IFNAME: RESULT(sa.f_type, s, rta->nh.iface ? rta->nh.iface->name : ""); break;
|
||||
case SA_IFINDEX: RESULT(sa.f_type, i, rta->nh.iface ? rta->nh.iface->index : 0); break;
|
||||
case SA_WEIGHT: RESULT(sa.f_type, i, rta->nh.weight + 1); break;
|
||||
case SA_PREF: RESULT(sa.f_type, i, rta->pref); break;
|
||||
case SA_GW_MPLS: RESULT(sa.f_type, i, rta->nh.labels ? rta->nh.label[0] : MPLS_NULL); break;
|
||||
default:
|
||||
bug("Invalid static attribute access (%u/%u)", sa.f_type, sa.sa_code);
|
||||
}
|
||||
|
||||
#undef RESULT
|
||||
}
|
||||
|
||||
/*
|
||||
* Evaluate dynamic attribute of @rt1 according to @da
|
||||
* and store result in @pos.
|
||||
*/
|
||||
static void
|
||||
eval_dynamic_attr(const struct rte *rt1, struct f_dynamic_attr da, struct f_val *pos)
|
||||
{
|
||||
const struct rta *rta = rt1->attrs;
|
||||
const struct eattr *e = ea_find(rta->eattrs, da.ea_code);
|
||||
|
||||
#define RESULT(_type, value, result) \
|
||||
do { \
|
||||
pos->type = _type; \
|
||||
pos->val.value = result; \
|
||||
} while (0)
|
||||
|
||||
#define RESULT_VOID \
|
||||
do { \
|
||||
pos->type = T_VOID; \
|
||||
} while (0)
|
||||
|
||||
if (!e)
|
||||
{
|
||||
/* A special case: undefined as_path looks like empty as_path */
|
||||
if (da.type == EAF_TYPE_AS_PATH)
|
||||
{
|
||||
RESULT(T_PATH, ad, &null_adata);
|
||||
return;
|
||||
}
|
||||
|
||||
/* The same special case for int_set */
|
||||
if (da.type == EAF_TYPE_INT_SET)
|
||||
{
|
||||
RESULT(T_CLIST, ad, &null_adata);
|
||||
return;
|
||||
}
|
||||
|
||||
/* The same special case for ec_set */
|
||||
if (da.type == EAF_TYPE_EC_SET)
|
||||
{
|
||||
RESULT(T_ECLIST, ad, &null_adata);
|
||||
return;
|
||||
}
|
||||
|
||||
/* The same special case for lc_set */
|
||||
if (da.type == EAF_TYPE_LC_SET)
|
||||
{
|
||||
RESULT(T_LCLIST, ad, &null_adata);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Undefined value */
|
||||
RESULT_VOID;
|
||||
return;
|
||||
}
|
||||
|
||||
switch (e->type & EAF_TYPE_MASK)
|
||||
{
|
||||
case EAF_TYPE_INT:
|
||||
RESULT(da.f_type, i, e->u.data);
|
||||
break;
|
||||
case EAF_TYPE_ROUTER_ID:
|
||||
RESULT(T_QUAD, i, e->u.data);
|
||||
break;
|
||||
case EAF_TYPE_OPAQUE:
|
||||
RESULT(T_ENUM_EMPTY, i, 0);
|
||||
break;
|
||||
case EAF_TYPE_IP_ADDRESS:
|
||||
RESULT(T_IP, ip, *((ip_addr *) e->u.ptr->data));
|
||||
break;
|
||||
case EAF_TYPE_AS_PATH:
|
||||
RESULT(T_PATH, ad, e->u.ptr);
|
||||
break;
|
||||
case EAF_TYPE_BITFIELD:
|
||||
RESULT(T_BOOL, i, !!(e->u.data & (1u << da.bit)));
|
||||
break;
|
||||
case EAF_TYPE_INT_SET:
|
||||
RESULT(T_CLIST, ad, e->u.ptr);
|
||||
break;
|
||||
case EAF_TYPE_EC_SET:
|
||||
RESULT(T_ECLIST, ad, e->u.ptr);
|
||||
break;
|
||||
case EAF_TYPE_LC_SET:
|
||||
RESULT(T_LCLIST, ad, e->u.ptr);
|
||||
break;
|
||||
default:
|
||||
bug("Unknown dynamic attribute type");
|
||||
}
|
||||
|
||||
#undef RESULT
|
||||
#undef RESULT_VOID
|
||||
}
|
||||
|
||||
static inline u32 aggr_route_hash(const rte *e)
|
||||
{
|
||||
struct {
|
||||
net *net;
|
||||
struct rte_src *src;
|
||||
} obj = {
|
||||
.net = e->net,
|
||||
.src = e->src,
|
||||
};
|
||||
|
||||
return mem_hash(&obj, sizeof obj);
|
||||
}
|
||||
|
||||
#define AGGR_RTE_KEY(n) (&(n)->rte)
|
||||
#define AGGR_RTE_NEXT(n) ((n)->next_hash)
|
||||
#define AGGR_RTE_EQ(a,b) (((a)->src == (b)->src) && ((a)->net == (b)->net))
|
||||
#define AGGR_RTE_FN(_n) aggr_route_hash(_n)
|
||||
#define AGGR_RTE_ORDER 4 /* Initial */
|
||||
|
||||
#define AGGR_RTE_REHASH aggr_rte_rehash
|
||||
#define AGGR_RTE_PARAMS /8, *2, 2, 2, 4, 24
|
||||
|
||||
HASH_DEFINE_REHASH_FN(AGGR_RTE, struct aggregator_route);
|
||||
|
||||
|
||||
#define AGGR_BUCK_KEY(n) (n)
|
||||
#define AGGR_BUCK_NEXT(n) ((n)->next_hash)
|
||||
#define AGGR_BUCK_EQ(a,b) (((a)->hash == (b)->hash) && (same_val_list((a)->aggr_data, (b)->aggr_data, p->aggr_on_count)))
|
||||
#define AGGR_BUCK_FN(n) ((n)->hash)
|
||||
#define AGGR_BUCK_ORDER 4 /* Initial */
|
||||
|
||||
#define AGGR_BUCK_REHASH aggr_buck_rehash
|
||||
#define AGGR_BUCK_PARAMS /8, *2, 2, 2, 4, 24
|
||||
|
||||
HASH_DEFINE_REHASH_FN(AGGR_BUCK, struct aggregator_bucket);
|
||||
|
||||
|
||||
#define AGGR_DATA_MEMSIZE (sizeof(struct f_val) * p->aggr_on_count)
|
||||
|
||||
static void
|
||||
aggregator_rt_notify(struct proto *P, struct channel *src_ch, net *net, rte *new, rte *old)
|
||||
{
|
||||
struct aggregator_proto *p = SKIP_BACK(struct aggregator_proto, p, P);
|
||||
ASSERT_DIE(src_ch == p->src);
|
||||
struct aggregator_bucket *new_bucket = NULL, *old_bucket = NULL;
|
||||
struct aggregator_route *old_route = NULL;
|
||||
|
||||
/* Find the objects for the old route */
|
||||
if (old)
|
||||
old_route = HASH_FIND(p->routes, AGGR_RTE, old);
|
||||
|
||||
if (old_route)
|
||||
old_bucket = old_route->bucket;
|
||||
|
||||
/* Find the bucket for the new route */
|
||||
if (new)
|
||||
{
|
||||
/* Routes are identical, do nothing */
|
||||
if (old_route && rte_same(&old_route->rte, new))
|
||||
return;
|
||||
|
||||
/* Evaluate route attributes. */
|
||||
struct aggregator_bucket *tmp_bucket = sl_allocz(p->bucket_slab);
|
||||
struct lp_state tmp_state;
|
||||
lp_save(tmp_linpool, &tmp_state);
|
||||
|
||||
for (uint val_idx = 0; val_idx < p->aggr_on_count; val_idx++)
|
||||
{
|
||||
int type = p->aggr_on[val_idx].type;
|
||||
struct f_val *pos = &tmp_bucket->aggr_data[val_idx];
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case AGGR_ITEM_TERM: {
|
||||
const struct f_line *line = p->aggr_on[val_idx].line;
|
||||
struct rte *rt1 = new;
|
||||
enum filter_return fret = f_eval_rte(line, &new, tmp_linpool, 0, NULL, pos);
|
||||
|
||||
if (rt1 != new)
|
||||
{
|
||||
rte_free(rt1);
|
||||
log(L_WARN "Aggregator rule modifies the route, reverting");
|
||||
}
|
||||
|
||||
if (fret > F_RETURN)
|
||||
log(L_WARN "%s.%s: Wrong number of items left on stack after evaluation of aggregation list", rt1->src->proto->name, rt1->sender->name);
|
||||
|
||||
switch (pos->type) {
|
||||
case T_VOID:
|
||||
case T_INT:
|
||||
case T_BOOL:
|
||||
case T_PAIR:
|
||||
case T_QUAD:
|
||||
case T_ENUM:
|
||||
case T_IP:
|
||||
case T_EC:
|
||||
case T_LC:
|
||||
case T_RD:
|
||||
/* Fits, OK */
|
||||
break;
|
||||
|
||||
default:
|
||||
log(L_WARN "%s.%s: Expression evaluated to type %s unsupported by aggregator. Store this value as a custom attribute instead", new->src->proto->name, new->sender->name, f_type_name(pos->type));
|
||||
*pos = (struct f_val) { .type = T_INT, .val.i = 0 };
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case AGGR_ITEM_STATIC_ATTR: {
|
||||
eval_static_attr(new, p->aggr_on[val_idx].sa, pos);
|
||||
break;
|
||||
}
|
||||
|
||||
case AGGR_ITEM_DYNAMIC_ATTR: {
|
||||
eval_dynamic_attr(new, p->aggr_on[val_idx].da, pos);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Compute the hash */
|
||||
u64 haux;
|
||||
mem_hash_init(&haux);
|
||||
for (uint i = 0; i < p->aggr_on_count; i++)
|
||||
{
|
||||
mem_hash_mix_num(&haux, tmp_bucket->aggr_data[i].type);
|
||||
|
||||
#define MX(k) mem_hash_mix(&haux, &IT(k), sizeof IT(k));
|
||||
#define IT(k) tmp_bucket->aggr_data[i].val.k
|
||||
|
||||
switch (tmp_bucket->aggr_data[i].type)
|
||||
{
|
||||
case T_VOID:
|
||||
break;
|
||||
case T_INT:
|
||||
case T_BOOL:
|
||||
case T_PAIR:
|
||||
case T_QUAD:
|
||||
case T_ENUM:
|
||||
MX(i);
|
||||
break;
|
||||
case T_EC:
|
||||
case T_RD:
|
||||
MX(ec);
|
||||
break;
|
||||
case T_LC:
|
||||
MX(lc);
|
||||
break;
|
||||
case T_IP:
|
||||
MX(ip);
|
||||
break;
|
||||
case T_NET:
|
||||
mem_hash_mix_num(&haux, net_hash(IT(net)));
|
||||
break;
|
||||
case T_STRING:
|
||||
mem_hash_mix_str(&haux, IT(s));
|
||||
break;
|
||||
case T_PATH_MASK:
|
||||
mem_hash_mix(&haux, IT(path_mask), sizeof(*IT(path_mask)) + IT(path_mask)->len * sizeof (IT(path_mask)->item));
|
||||
break;
|
||||
case T_PATH:
|
||||
case T_CLIST:
|
||||
case T_ECLIST:
|
||||
case T_LCLIST:
|
||||
case T_BYTESTRING:
|
||||
mem_hash_mix(&haux, IT(ad)->data, IT(ad)->length);
|
||||
break;
|
||||
case T_NONE:
|
||||
case T_PATH_MASK_ITEM:
|
||||
case T_ROUTE:
|
||||
case T_ROUTES_BLOCK:
|
||||
bug("Invalid type %s in hashing", f_type_name(tmp_bucket->aggr_data[i].type));
|
||||
case T_SET:
|
||||
MX(t);
|
||||
break;
|
||||
case T_PREFIX_SET:
|
||||
MX(ti);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
tmp_bucket->hash = mem_hash_value(&haux);
|
||||
|
||||
/* Find the existing bucket */
|
||||
if (new_bucket = HASH_FIND(p->buckets, AGGR_BUCK, tmp_bucket))
|
||||
sl_free(tmp_bucket);
|
||||
else
|
||||
{
|
||||
new_bucket = tmp_bucket;
|
||||
HASH_INSERT2(p->buckets, AGGR_BUCK, p->p.pool, new_bucket);
|
||||
}
|
||||
|
||||
/* Store the route attributes */
|
||||
if (rta_is_cached(new->attrs))
|
||||
rta_clone(new->attrs);
|
||||
else
|
||||
new->attrs = rta_lookup(new->attrs);
|
||||
|
||||
/* Insert the new route into the bucket */
|
||||
struct aggregator_route *arte = sl_alloc(p->route_slab);
|
||||
*arte = (struct aggregator_route) {
|
||||
.bucket = new_bucket,
|
||||
.rte = *new,
|
||||
};
|
||||
arte->rte.next = new_bucket->rte,
|
||||
new_bucket->rte = &arte->rte;
|
||||
new_bucket->count++;
|
||||
HASH_INSERT2(p->routes, AGGR_RTE, p->p.pool, arte);
|
||||
|
||||
lp_restore(tmp_linpool, &tmp_state);
|
||||
}
|
||||
|
||||
/* Remove the old route from its bucket */
|
||||
if (old_bucket)
|
||||
{
|
||||
for (struct rte **k = &old_bucket->rte; *k; k = &(*k)->next)
|
||||
if (*k == &old_route->rte)
|
||||
{
|
||||
*k = (*k)->next;
|
||||
break;
|
||||
}
|
||||
|
||||
old_bucket->count--;
|
||||
HASH_REMOVE2(p->routes, AGGR_RTE, p->p.pool, old_route);
|
||||
rta_free(old_route->rte.attrs);
|
||||
sl_free(old_route);
|
||||
}
|
||||
|
||||
/* Announce changes */
|
||||
if (old_bucket)
|
||||
aggregator_bucket_update(p, old_bucket, net);
|
||||
|
||||
if (new_bucket && (new_bucket != old_bucket))
|
||||
aggregator_bucket_update(p, new_bucket, net);
|
||||
|
||||
/* Cleanup the old bucket if empty */
|
||||
if (old_bucket && (!old_bucket->rte || !old_bucket->count))
|
||||
{
|
||||
ASSERT_DIE(!old_bucket->rte && !old_bucket->count);
|
||||
HASH_REMOVE2(p->buckets, AGGR_BUCK, p->p.pool, old_bucket);
|
||||
sl_free(old_bucket);
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
aggregator_preexport(struct channel *C, struct rte *new)
|
||||
{
|
||||
struct aggregator_proto *p = SKIP_BACK(struct aggregator_proto, p, C->proto);
|
||||
/* Reject our own routes */
|
||||
if (new->sender == p->dst)
|
||||
return -1;
|
||||
|
||||
/* Disallow aggregating already aggregated routes */
|
||||
if (new->attrs->source == RTS_AGGREGATED)
|
||||
{
|
||||
log(L_ERR "Multiple aggregations of the same route not supported in BIRD 2.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
aggregator_postconfig(struct proto_config *CF)
|
||||
{
|
||||
struct aggregator_config *cf = SKIP_BACK(struct aggregator_config, c, CF);
|
||||
|
||||
if (!cf->dst->table)
|
||||
cf_error("Source table not specified");
|
||||
|
||||
if (!cf->src->table)
|
||||
cf_error("Destination table not specified");
|
||||
|
||||
if (cf->dst->table->addr_type != cf->src->table->addr_type)
|
||||
cf_error("Both tables must be of the same type");
|
||||
|
||||
cf->dst->in_filter = cf->src->in_filter;
|
||||
|
||||
cf->src->in_filter = FILTER_REJECT;
|
||||
cf->dst->out_filter = FILTER_REJECT;
|
||||
|
||||
cf->dst->debug = cf->src->debug;
|
||||
}
|
||||
|
||||
static struct proto *
|
||||
aggregator_init(struct proto_config *CF)
|
||||
{
|
||||
struct proto *P = proto_new(CF);
|
||||
struct aggregator_proto *p = SKIP_BACK(struct aggregator_proto, p, P);
|
||||
struct aggregator_config *cf = SKIP_BACK(struct aggregator_config, c, CF);
|
||||
|
||||
proto_configure_channel(P, &p->src, cf->src);
|
||||
proto_configure_channel(P, &p->dst, cf->dst);
|
||||
|
||||
p->aggr_on_count = cf->aggr_on_count;
|
||||
p->aggr_on_da_count = cf->aggr_on_da_count;
|
||||
p->aggr_on = cf->aggr_on;
|
||||
p->merge_by = cf->merge_by;
|
||||
|
||||
P->rt_notify = aggregator_rt_notify;
|
||||
P->preexport = aggregator_preexport;
|
||||
|
||||
return P;
|
||||
}
|
||||
|
||||
static int
|
||||
aggregator_start(struct proto *P)
|
||||
{
|
||||
struct aggregator_proto *p = SKIP_BACK(struct aggregator_proto, p, P);
|
||||
|
||||
p->bucket_slab = sl_new(P->pool, sizeof(struct aggregator_bucket) + AGGR_DATA_MEMSIZE);
|
||||
HASH_INIT(p->buckets, P->pool, AGGR_BUCK_ORDER);
|
||||
|
||||
p->route_slab = sl_new(P->pool, sizeof(struct aggregator_route));
|
||||
HASH_INIT(p->routes, P->pool, AGGR_RTE_ORDER);
|
||||
|
||||
p->reload_buckets = (event) {
|
||||
.hook = aggregator_reload_buckets,
|
||||
.data = p,
|
||||
};
|
||||
|
||||
return PS_UP;
|
||||
}
|
||||
|
||||
static int
|
||||
aggregator_shutdown(struct proto *P)
|
||||
{
|
||||
struct aggregator_proto *p = SKIP_BACK(struct aggregator_proto, p, P);
|
||||
|
||||
HASH_WALK_DELSAFE(p->buckets, next_hash, b)
|
||||
{
|
||||
while (b->rte)
|
||||
{
|
||||
struct aggregator_route *arte = SKIP_BACK(struct aggregator_route, rte, b->rte);
|
||||
b->rte = arte->rte.next;
|
||||
b->count--;
|
||||
HASH_REMOVE(p->routes, AGGR_RTE, arte);
|
||||
rta_free(arte->rte.attrs);
|
||||
sl_free(arte);
|
||||
}
|
||||
|
||||
ASSERT_DIE(b->count == 0);
|
||||
HASH_REMOVE(p->buckets, AGGR_BUCK, b);
|
||||
sl_free(b);
|
||||
}
|
||||
HASH_WALK_END;
|
||||
|
||||
return PS_DOWN;
|
||||
}
|
||||
|
||||
static int
|
||||
aggregator_reconfigure(struct proto *P, struct proto_config *CF)
|
||||
{
|
||||
struct aggregator_proto *p = SKIP_BACK(struct aggregator_proto, p, P);
|
||||
struct aggregator_config *cf = SKIP_BACK(struct aggregator_config, c, CF);
|
||||
|
||||
TRACE(D_EVENTS, "Reconfiguring");
|
||||
|
||||
/* Compare numeric values (shortcut) */
|
||||
if (cf->aggr_on_count != p->aggr_on_count)
|
||||
return 0;
|
||||
|
||||
if (cf->aggr_on_da_count != p->aggr_on_da_count)
|
||||
return 0;
|
||||
|
||||
/* Compare aggregator rule */
|
||||
for (uint i = 0; i < p->aggr_on_count; i++)
|
||||
switch (cf->aggr_on[i].type)
|
||||
{
|
||||
case AGGR_ITEM_TERM:
|
||||
if (!f_same(cf->aggr_on[i].line, p->aggr_on[i].line))
|
||||
return 0;
|
||||
break;
|
||||
case AGGR_ITEM_STATIC_ATTR:
|
||||
if (memcmp(&cf->aggr_on[i].sa, &p->aggr_on[i].sa, sizeof(struct f_static_attr)) != 0)
|
||||
return 0;
|
||||
break;
|
||||
case AGGR_ITEM_DYNAMIC_ATTR:
|
||||
if (memcmp(&cf->aggr_on[i].da, &p->aggr_on[i].da, sizeof(struct f_dynamic_attr)) != 0)
|
||||
return 0;
|
||||
break;
|
||||
default:
|
||||
bug("Broken aggregator rule");
|
||||
}
|
||||
|
||||
/* Compare merge filter */
|
||||
if (!f_same(cf->merge_by, p->merge_by))
|
||||
ev_schedule(&p->reload_buckets);
|
||||
|
||||
p->aggr_on = cf->aggr_on;
|
||||
p->merge_by = cf->merge_by;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct protocol proto_aggregator = {
|
||||
.name = "Aggregator",
|
||||
.template = "aggregator%d",
|
||||
.class = PROTOCOL_AGGREGATOR,
|
||||
.preference = 1,
|
||||
.channel_mask = NB_ANY,
|
||||
.proto_size = sizeof(struct aggregator_proto),
|
||||
.config_size = sizeof(struct aggregator_config),
|
||||
.postconfig = aggregator_postconfig,
|
||||
.init = aggregator_init,
|
||||
.start = aggregator_start,
|
||||
.shutdown = aggregator_shutdown,
|
||||
.reconfigure = aggregator_reconfigure,
|
||||
};
|
||||
|
||||
void
|
||||
aggregator_build(void)
|
||||
{
|
||||
proto_build(&proto_aggregator);
|
||||
}
|
86
proto/aggregator/aggregator.h
Normal file
86
proto/aggregator/aggregator.h
Normal file
@ -0,0 +1,86 @@
|
||||
/*
|
||||
* BIRD -- Aggregator Pseudoprotocol
|
||||
*
|
||||
* (c) 2023 Igor Putovny <igor.putovny@nic.cz>
|
||||
* (c) 2023 Maria Matejka <mq@ucw.cz>
|
||||
* (c) 2023 CZ.NIC z.s.p.o.
|
||||
*
|
||||
* Can be freely distributed and used under the terms of the GNU GPL.
|
||||
*
|
||||
* This file contains the data structures used by Babel.
|
||||
*/
|
||||
|
||||
#ifndef _BIRD_AGGREGATOR_H_
|
||||
#define _BIRD_AGGREGATOR_H_
|
||||
|
||||
#include "nest/bird.h"
|
||||
#include "nest/protocol.h"
|
||||
#include "lib/hash.h"
|
||||
|
||||
struct aggregator_config {
|
||||
struct proto_config c;
|
||||
struct channel_config *src, *dst;
|
||||
uint aggr_on_count;
|
||||
uint aggr_on_da_count;
|
||||
struct aggr_item *aggr_on;
|
||||
const struct f_line *merge_by;
|
||||
};
|
||||
|
||||
struct aggregator_route {
|
||||
struct aggregator_route *next_hash;
|
||||
struct aggregator_bucket *bucket;
|
||||
struct rte rte;
|
||||
};
|
||||
|
||||
struct aggregator_bucket {
|
||||
struct aggregator_bucket *next_hash;
|
||||
struct rte *rte; /* Pointer to struct aggregator_route.rte */
|
||||
struct rte_src *last_src; /* Which src we announced the bucket last with */
|
||||
u32 count;
|
||||
u32 hash;
|
||||
struct f_val aggr_data[0];
|
||||
};
|
||||
|
||||
struct aggregator_proto {
|
||||
struct proto p;
|
||||
struct channel *src, *dst;
|
||||
|
||||
/* Buckets by aggregator rule */
|
||||
HASH(struct aggregator_bucket) buckets;
|
||||
slab *bucket_slab;
|
||||
|
||||
/* Routes by net and src */
|
||||
HASH(struct aggregator_route) routes;
|
||||
slab *route_slab;
|
||||
|
||||
/* Aggregator rule */
|
||||
uint aggr_on_count;
|
||||
uint aggr_on_da_count;
|
||||
struct aggr_item *aggr_on;
|
||||
|
||||
/* Merge filter */
|
||||
const struct f_line *merge_by;
|
||||
event reload_buckets;
|
||||
};
|
||||
|
||||
enum aggr_item_type {
|
||||
AGGR_ITEM_TERM,
|
||||
AGGR_ITEM_STATIC_ATTR,
|
||||
AGGR_ITEM_DYNAMIC_ATTR,
|
||||
};
|
||||
|
||||
struct aggr_item {
|
||||
enum aggr_item_type type;
|
||||
union {
|
||||
struct f_static_attr sa;
|
||||
struct f_dynamic_attr da;
|
||||
const struct f_line *line;
|
||||
};
|
||||
};
|
||||
|
||||
struct aggr_item_node {
|
||||
const struct aggr_item_node *next;
|
||||
struct aggr_item i;
|
||||
};
|
||||
|
||||
#endif
|
134
proto/aggregator/config.Y
Normal file
134
proto/aggregator/config.Y
Normal file
@ -0,0 +1,134 @@
|
||||
/*
|
||||
* BIRD -- Aggregator configuration
|
||||
*
|
||||
* (c) 2023 Igor Putovny <igor.putovny@nic.cz>
|
||||
* (c) 2023 Maria Matejka <mq@ucw.cz>
|
||||
* (c) 2023 CZ.NIC z.s.p.o.
|
||||
*
|
||||
* Can be freely distributed and used under the terms of the GNU GPL.
|
||||
*/
|
||||
|
||||
CF_HDR
|
||||
|
||||
#include "proto/aggregator/aggregator.h"
|
||||
|
||||
CF_DEFINES
|
||||
|
||||
#define AGGREGATOR_CFG ((struct aggregator_config *) this_proto)
|
||||
#define AGGR_ITEM_ALLOC ((struct aggr_item_node *) cfg_allocz(sizeof(struct aggr_item_node)))
|
||||
|
||||
|
||||
CF_DECLS
|
||||
|
||||
CF_KEYWORDS(AGGREGATOR, AGGREGATE, ON, MERGE, BY)
|
||||
|
||||
%type <ai> aggr_item aggr_list
|
||||
|
||||
CF_GRAMMAR
|
||||
|
||||
proto: aggregator_proto ;
|
||||
|
||||
aggregator_proto_start: proto_start AGGREGATOR
|
||||
{
|
||||
this_proto = proto_config_new(&proto_aggregator, $1);
|
||||
this_channel = AGGREGATOR_CFG->src = channel_config_new(NULL, "source", 0, this_proto);
|
||||
AGGREGATOR_CFG->dst = channel_config_new(NULL, "destination", 0, this_proto);
|
||||
|
||||
AGGREGATOR_CFG->src->ra_mode = AGGREGATOR_CFG->dst->ra_mode = RA_ANY;
|
||||
};
|
||||
|
||||
aggregator_proto_item:
|
||||
proto_item
|
||||
| channel_item_
|
||||
| PEER TABLE rtable { AGGREGATOR_CFG->dst->table = $3; }
|
||||
| AGGREGATE ON aggr_list {
|
||||
if (AGGREGATOR_CFG->aggr_on)
|
||||
cf_error("Only one aggregate on clause allowed");
|
||||
|
||||
_Bool net_present = 0;
|
||||
int count = 0;
|
||||
|
||||
for (const struct aggr_item_node *item = $3; item; item = item->next) {
|
||||
// log(L_WARN "type %d sacode %d", item->i.type, item->i.sa.sa_code);
|
||||
if (item->i.type == AGGR_ITEM_STATIC_ATTR && item->i.sa.sa_code == SA_NET)
|
||||
net_present = 1;
|
||||
|
||||
count++;
|
||||
}
|
||||
|
||||
if (!net_present)
|
||||
cf_error("'NET' must be present");
|
||||
|
||||
AGGREGATOR_CFG->aggr_on = cfg_alloc(sizeof(struct aggr_item) * count);
|
||||
|
||||
int pos = 0;
|
||||
for (const struct aggr_item_node *item = $3; item; item = item->next) {
|
||||
if (item->i.type == AGGR_ITEM_DYNAMIC_ATTR)
|
||||
AGGREGATOR_CFG->aggr_on_da_count++;
|
||||
|
||||
AGGREGATOR_CFG->aggr_on[pos++] = item->i;
|
||||
}
|
||||
|
||||
AGGREGATOR_CFG->aggr_on_count = pos;
|
||||
}
|
||||
| MERGE BY {
|
||||
cf_push_block_scope(new_config);
|
||||
f_predefined_variable(new_config, "routes", T_ROUTES_BLOCK);
|
||||
} function_body {
|
||||
cf_pop_block_scope(new_config);
|
||||
$4->args++; /* The predefined "routes" variable */
|
||||
AGGREGATOR_CFG->merge_by = $4;
|
||||
}
|
||||
;
|
||||
|
||||
aggregator_proto_opts: /* empty */ | aggregator_proto_opts aggregator_proto_item ';' ;
|
||||
aggregator_proto: aggregator_proto_start proto_name '{' aggregator_proto_opts '}' ;
|
||||
|
||||
|
||||
aggr_list:
|
||||
aggr_item
|
||||
| aggr_list ',' aggr_item {
|
||||
if ($3 == NULL) {
|
||||
$$ = $1;
|
||||
} else {
|
||||
$$ = $3;
|
||||
$$->next = $1;
|
||||
}
|
||||
}
|
||||
;
|
||||
|
||||
aggr_item:
|
||||
'(' term ')' {
|
||||
$$ = AGGR_ITEM_ALLOC;
|
||||
$$->i.type = AGGR_ITEM_TERM;
|
||||
$$->i.line = f_linearize($2, 1);
|
||||
}
|
||||
| CF_SYM_KNOWN {
|
||||
switch ($1->class) {
|
||||
case SYM_ATTRIBUTE:
|
||||
$$ = AGGR_ITEM_ALLOC;
|
||||
$$->i.type = AGGR_ITEM_DYNAMIC_ATTR;
|
||||
$$->i.da = *$1->attribute;
|
||||
break;
|
||||
case SYM_CONSTANT_RANGE:
|
||||
$$ = NULL;
|
||||
break;
|
||||
default:
|
||||
cf_error("Can't aggregate on symbol type %s.", cf_symbol_class_name($1));
|
||||
}
|
||||
}
|
||||
| dynamic_attr {
|
||||
$$ = AGGR_ITEM_ALLOC;
|
||||
$$->i.type = AGGR_ITEM_DYNAMIC_ATTR;
|
||||
$$->i.da = $1;
|
||||
}
|
||||
| static_attr {
|
||||
$$ = AGGR_ITEM_ALLOC;
|
||||
$$->i.type = AGGR_ITEM_STATIC_ATTR;
|
||||
$$->i.sa = $1;
|
||||
}
|
||||
;
|
||||
|
||||
CF_CODE
|
||||
|
||||
CF_END
|
116
proto/aggregator/test.conf
Normal file
116
proto/aggregator/test.conf
Normal file
@ -0,0 +1,116 @@
|
||||
log "bird.log" all;
|
||||
|
||||
protocol device {}
|
||||
|
||||
protocol static {
|
||||
ipv6;
|
||||
route 2001:db8:0::/48 unreachable { bgp_path.prepend(65432); bgp_path.prepend(4200000000); };
|
||||
route 2001:db8:1::/48 unreachable;
|
||||
route 2001:db8:2::/48 unreachable;
|
||||
route 2001:db8:3::/48 unreachable;
|
||||
route 2001:db8:4::/48 unreachable;
|
||||
route 2001:db8:5::/48 unreachable;
|
||||
route 2001:db8:6::/48 unreachable;
|
||||
route 2001:db8:7::/48 unreachable;
|
||||
route 2001:db8:8::/48 unreachable;
|
||||
route 2001:db8:9::/48 unreachable;
|
||||
route 2001:db8:a::/48 unreachable;
|
||||
route 2001:db8:b::/48 unreachable;
|
||||
route 2001:db8:c::/48 unreachable;
|
||||
route 2001:db8:d::/48 unreachable;
|
||||
route 2001:db8:e::/48 unreachable;
|
||||
route 2001:db8:f::/48 unreachable;
|
||||
}
|
||||
|
||||
protocol static {
|
||||
ipv6 {
|
||||
import filter {
|
||||
bgp_med = 1;
|
||||
bgp_community = -empty-.add((65533,1)).add((65500,0xe));
|
||||
accept;
|
||||
};
|
||||
};
|
||||
route 2001:db8:1::/48 unreachable;
|
||||
route 2001:db8:3::/48 unreachable;
|
||||
route 2001:db8:5::/48 unreachable;
|
||||
route 2001:db8:7::/48 unreachable;
|
||||
route 2001:db8:9::/48 unreachable;
|
||||
route 2001:db8:b::/48 unreachable;
|
||||
route 2001:db8:d::/48 unreachable;
|
||||
route 2001:db8:f::/48 unreachable;
|
||||
}
|
||||
|
||||
protocol static {
|
||||
ipv6 {
|
||||
import filter {
|
||||
bgp_med = 2;
|
||||
bgp_community = -empty-.add((65533,2)).add((65500,0xd));
|
||||
accept;
|
||||
};
|
||||
};
|
||||
route 2001:db8:2::/48 unreachable;
|
||||
route 2001:db8:3::/48 unreachable;
|
||||
route 2001:db8:6::/48 unreachable;
|
||||
route 2001:db8:7::/48 unreachable;
|
||||
route 2001:db8:a::/48 unreachable;
|
||||
route 2001:db8:b::/48 unreachable;
|
||||
route 2001:db8:e::/48 unreachable;
|
||||
route 2001:db8:f::/48 unreachable;
|
||||
}
|
||||
|
||||
protocol static {
|
||||
ipv6 {
|
||||
import filter {
|
||||
bgp_med = 4;
|
||||
bgp_community = -empty-.add((65533,4)).add((65500,0xb));
|
||||
accept;
|
||||
};
|
||||
};
|
||||
route 2001:db8:4::/48 unreachable;
|
||||
route 2001:db8:5::/48 unreachable;
|
||||
route 2001:db8:6::/48 unreachable;
|
||||
route 2001:db8:7::/48 unreachable;
|
||||
route 2001:db8:c::/48 unreachable;
|
||||
route 2001:db8:d::/48 unreachable;
|
||||
route 2001:db8:e::/48 unreachable;
|
||||
route 2001:db8:f::/48 unreachable;
|
||||
}
|
||||
|
||||
protocol static {
|
||||
ipv6 {
|
||||
import filter {
|
||||
bgp_med = 8;
|
||||
bgp_community = -empty-.add((65533,8)).add((65500,0x7));
|
||||
accept;
|
||||
};
|
||||
};
|
||||
route 2001:db8:8::/48 unreachable;
|
||||
route 2001:db8:9::/48 unreachable;
|
||||
route 2001:db8:a::/48 unreachable;
|
||||
route 2001:db8:b::/48 unreachable;
|
||||
route 2001:db8:c::/48 unreachable;
|
||||
route 2001:db8:d::/48 unreachable;
|
||||
route 2001:db8:e::/48 unreachable;
|
||||
route 2001:db8:f::/48 unreachable;
|
||||
}
|
||||
|
||||
ipv6 table agr_result;
|
||||
|
||||
protocol aggregator {
|
||||
table master6;
|
||||
peer table agr_result;
|
||||
export all;
|
||||
aggregate on net,(defined(bgp_med));
|
||||
merge by {
|
||||
print "Merging all these: ", routes;
|
||||
bgp_med = 0;
|
||||
for route r in routes do {
|
||||
if ! defined(r.bgp_med) then { unset(bgp_med); accept; }
|
||||
|
||||
print r, " bgp_med: ", r.bgp_med;
|
||||
bgp_med = bgp_med + r.bgp_med;
|
||||
bgp_community = bgp_community.add(r.bgp_community);
|
||||
}
|
||||
accept;
|
||||
};
|
||||
}
|
Loading…
Reference in New Issue
Block a user