diff options
-rw-r--r-- | .gitlab-ci.yml | 52 | ||||
-rw-r--r-- | conf/cf-lex.l | 39 | ||||
-rw-r--r-- | conf/conf.h | 7 | ||||
-rw-r--r-- | conf/confbase.Y | 4 | ||||
-rw-r--r-- | conf/gen_parser.m4 | 4 | ||||
-rw-r--r-- | doc/bird.sgml | 51 | ||||
-rw-r--r-- | filter/trie_test.c | 33 | ||||
-rw-r--r-- | lib/birdlib.h | 27 | ||||
-rw-r--r-- | lib/hash.h | 2 | ||||
-rw-r--r-- | lib/ip.h | 31 | ||||
-rw-r--r-- | lib/net.h | 34 | ||||
-rw-r--r-- | lib/timer.h | 3 | ||||
-rw-r--r-- | nest/Makefile | 2 | ||||
-rw-r--r-- | nest/config.Y | 2 | ||||
-rw-r--r-- | nest/rt-fib_test.c | 246 | ||||
-rw-r--r-- | proto/babel/babel.c | 84 | ||||
-rw-r--r-- | proto/babel/babel.h | 24 | ||||
-rw-r--r-- | proto/babel/config.Y | 20 | ||||
-rw-r--r-- | proto/babel/packets.c | 126 | ||||
-rw-r--r-- | proto/bgp/config.Y | 2 | ||||
-rw-r--r-- | sysdep/linux/netlink.c | 6 | ||||
-rw-r--r-- | sysdep/unix/io.c | 13 | ||||
-rw-r--r-- | test/birdtest.h | 4 | ||||
-rw-r--r-- | test/bt-utils.c | 132 | ||||
-rw-r--r-- | test/bt-utils.h | 6 |
25 files changed, 823 insertions, 131 deletions
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index d1a824ff..c913d0f8 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -60,14 +60,6 @@ stages: - linux - amd64 -build-debian-8-amd64: - <<: *build-linux - image: registry.nic.cz/labs/bird:debian-8-amd64 - -build-debian-8-i386: - <<: *build-linux - image: registry.nic.cz/labs/bird:debian-8-i386 - build-debian-9-amd64: <<: *build-linux image: registry.nic.cz/labs/bird:debian-9-amd64 @@ -140,18 +132,14 @@ build-fedora-34-amd64: <<: *build-linux image: registry.nic.cz/labs/bird:fedora-33-amd64 -build-centos-7-amd64: - <<: *build-linux - image: registry.nic.cz/labs/bird:centos-7-amd64 +#build-centos-7-amd64: +# <<: *build-linux +# image: registry.nic.cz/labs/bird:centos-7-amd64 build-centos-8-amd64: <<: *build-linux image: registry.nic.cz/labs/bird:centos-8-amd64 -build-ubuntu-14_04-amd64: - <<: *build-linux - image: registry.nic.cz/labs/bird:ubuntu-14.04-amd64 - build-ubuntu-16_04-amd64: <<: *build-linux image: registry.nic.cz/labs/bird:ubuntu-16.04-amd64 @@ -232,28 +220,6 @@ build-opensuse-15.3-amd64: paths: - pkg/pkgs/* -# Dpkg error: PATH is not set -#pkg-debian-8-amd64: -# <<: *pkg-deb -# needs: [build-debian-8-amd64] -# image: registry.nic.cz/labs/bird:debian-8-amd64 - -# Dpkg error: PATH is not set -#pkg-debian-8-i386: -# <<: *pkg-deb -# needs: [build-debian-8-i386] -# image: registry.nic.cz/labs/bird:debian-8-i386 - -pkg-debian-9-amd64: - <<: *pkg-deb - needs: [build-debian-9-amd64] - image: registry.nic.cz/labs/bird:debian-9-amd64 - -pkg-debian-9-i386: - <<: *pkg-deb - needs: [build-debian-9-i386] - image: registry.nic.cz/labs/bird:debian-9-i386 - pkg-debian-10-amd64: <<: *pkg-deb needs: [build-debian-10-amd64] @@ -294,12 +260,12 @@ pkg-fedora-34-amd64: needs: [build-fedora-34-amd64] image: registry.nic.cz/labs/bird:fedora-34-amd64 -pkg-centos-7-amd64: - <<: *pkg-rpm-wa - variables: - LC_ALL: en_US.UTF-8 - needs: [build-centos-7-amd64] - image: registry.nic.cz/labs/bird:centos-7-amd64 +#pkg-centos-7-amd64: +# <<: *pkg-rpm-wa +# variables: +# LC_ALL: en_US.UTF-8 +# needs: [build-centos-7-amd64] +# image: registry.nic.cz/labs/bird:centos-7-amd64 pkg-centos-8-amd64: <<: *pkg-rpm-wa diff --git a/conf/cf-lex.l b/conf/cf-lex.l index ceedee8a..9555949d 100644 --- a/conf/cf-lex.l +++ b/conf/cf-lex.l @@ -73,10 +73,10 @@ static uint cf_hash(const byte *c); #define KW_FN(k) cf_hash(k) #define KW_ORDER 8 /* Fixed */ -#define SYM_KEY(n) n->name, n->scope->active +#define SYM_KEY(n) n->name, n->scope #define SYM_NEXT(n) n->next #define SYM_EQ(a,s1,b,s2) !strcmp(a,b) && s1 == s2 -#define SYM_FN(k,s) cf_hash(k) +#define SYM_FN(k,s) cf_hash(k) ^ ptr_hash(s) #define SYM_ORDER 6 /* Initial */ #define SYM_REHASH sym_rehash @@ -601,30 +601,36 @@ cf_new_symbol(const byte *c) return s; } +struct symbol * +cf_symbol_from_keyword(const struct keyword *kw) +{ return cf_new_symbol(kw->name); } + /** - * cf_find_symbol - find a symbol by name + * cf_find_local_symbol - find a symbol by name * @cfg: specificed config + * @scope: specified symbol scope * @c: symbol name * * This functions searches the symbol table in the config @cfg for a symbol of - * given name. First it examines the current scope, then the second recent one + * given name. First it examines the scope @scope, then the parent scope * and so on until it either finds the symbol and returns a pointer to its * &symbol structure or reaches the end of the scope chain and returns %NULL to * signify no match. */ struct symbol * -cf_find_symbol(const struct config *cfg, const byte *c) +cf_find_local_symbol(const struct config *cfg, const struct sym_scope *scope, const byte *c) { struct symbol *s; - if (cfg->sym_hash.data && - (s = HASH_FIND(cfg->sym_hash, SYM, c, 1))) - return s; + if (cfg->sym_hash.data) + for (; scope; scope = scope->next) + if (s = HASH_FIND(cfg->sym_hash, SYM, c, scope)) + return s; /* In CLI command parsing, fallback points to the current config, otherwise it is NULL. */ if (cfg->fallback && cfg->fallback->sym_hash.data && - (s = HASH_FIND(cfg->fallback->sym_hash, SYM, c, 1))) + (s = HASH_FIND(cfg->fallback->sym_hash, SYM, c, cfg->fallback->root_scope))) return s; return NULL; @@ -642,7 +648,7 @@ cf_find_symbol(const struct config *cfg, const byte *c) struct symbol * cf_get_symbol(const byte *c) { - return cf_find_symbol(new_config, c) ?: cf_new_symbol(c); + return cf_find_local_symbol(new_config, conf_this_scope, c) ?: cf_new_symbol(c); } /** @@ -690,18 +696,23 @@ static enum yytokentype cf_lex_symbol(const char *data) { /* Have we defined such a symbol? */ - struct symbol *sym = cf_get_symbol(data); - cf_lval.s = sym; + struct symbol *sym = cf_find_local_symbol(new_config, conf_this_scope, data); - if (sym->class != SYM_VOID) + if (sym && (sym->class != SYM_VOID)) + { + cf_lval.s = sym; return CF_SYM_KNOWN; + } /* Is it a keyword? */ struct keyword *k = HASH_FIND(kw_hash, KW, data); if (k) { if (k->value > 0) + { + cf_lval.kw = k; return k->value; + } else { cf_lval.i = -k->value; @@ -710,7 +721,7 @@ cf_lex_symbol(const char *data) } /* OK, undefined symbol */ - cf_lval.s = sym; + cf_lval.s = cf_new_symbol(data); return CF_SYM_UNDEFINED; } diff --git a/conf/conf.h b/conf/conf.h index b409750e..d40f955e 100644 --- a/conf/conf.h +++ b/conf/conf.h @@ -187,7 +187,12 @@ int cf_lex(void); void cf_lex_init(int is_cli, struct config *c); void cf_lex_unwind(void); -struct symbol *cf_find_symbol(const struct config *cfg, const byte *c); +struct symbol *cf_find_local_symbol(const struct config *cfg, const struct sym_scope *scope, const byte *c); +static inline struct symbol *cf_find_symbol(const struct config *cfg, const byte *c) +{ return cf_find_local_symbol(cfg, cfg->root_scope, c); } + +struct keyword; +struct symbol *cf_symbol_from_keyword(const struct keyword *kw); struct symbol *cf_get_symbol(const byte *c); struct symbol *cf_default_name(char *template, int *counter); diff --git a/conf/confbase.Y b/conf/confbase.Y index 1d5738ff..3e8f5807 100644 --- a/conf/confbase.Y +++ b/conf/confbase.Y @@ -61,6 +61,7 @@ CF_DECLS net_addr net; net_addr *net_ptr; struct symbol *s; + struct keyword *kw; const char *t; struct rtable_config *r; struct channel_config *cc; @@ -117,6 +118,7 @@ CF_DECLS %type <t> text opttext %type <s> symbol +%type <kw> kw_sym %nonassoc PREFIX_DUMMY %left AND OR @@ -172,7 +174,7 @@ expr_us: | expr US { $$ = $1 US_; } ; -symbol: CF_SYM_UNDEFINED | CF_SYM_KNOWN ; +symbol: CF_SYM_UNDEFINED | CF_SYM_KNOWN | kw_sym { $$ = cf_symbol_from_keyword($1); } ; /* Switches */ diff --git a/conf/gen_parser.m4 b/conf/gen_parser.m4 index af4b1455..7a2a9de4 100644 --- a/conf/gen_parser.m4 +++ b/conf/gen_parser.m4 @@ -29,9 +29,9 @@ m4_define(CF_END, `m4_divert(-1)') m4_define(CF_itera, `m4_ifelse($#, 1, [[CF_iter($1)]], [[CF_iter($1)[[]]CF_itera(m4_shift($@))]])') m4_define(CF_iterate, `m4_define([[CF_iter]], m4_defn([[$1]]))CF_itera($2)') -# Keywords act as untyped %token +# Keywords act as %token<kw> m4_define(CF_keywd, `m4_ifdef([[CF_tok_$1]],,[[m4_define([[CF_tok_$1]],1)m4_define([[CF_toks]],CF_toks $1)]])') -m4_define(CF_KEYWORDS, `m4_define([[CF_toks]],[[]])CF_iterate([[CF_keywd]], [[$@]])m4_ifelse(CF_toks,,,%token<s>[[]]CF_toks +m4_define(CF_KEYWORDS, `m4_define([[CF_toks]],[[]])CF_iterate([[CF_keywd]], [[$@]])m4_ifelse(CF_toks,,,%token<kw>[[]]CF_toks )DNL') # CLI commands diff --git a/doc/bird.sgml b/doc/bird.sgml index 39eae4cd..8041faa9 100644 --- a/doc/bird.sgml +++ b/doc/bird.sgml @@ -1918,7 +1918,7 @@ protocol babel [<name>] { ipv6 [sadr] { <channel config> }; randomize router id <switch>; interface <interface pattern> { - type <wired|wireless>; + type <wired|wireless|tunnel>; rxcost <number>; limit <number>; hello interval <time>; @@ -1932,6 +1932,11 @@ protocol babel [<name>] { next hop ipv4 <address>; next hop ipv6 <address>; extended next hop <switch>; + rtt cost <number>; + rtt min <time>; + rtt max <time>; + rtt decay <number>; + send timestamps <switch>; authentication none|mac [permissive]; password "<text>"; password "<text>" { @@ -1962,15 +1967,16 @@ protocol babel [<name>] { router ID every time it starts up, which avoids this problem at the cost of not having stable router IDs in the network. Default: no. - <tag><label id="babel-type">type wired|wireless </tag> - This option specifies the interface type: Wired or wireless. On wired - interfaces a neighbor is considered unreachable after a small number of - Hello packets are lost, as described by <cf/limit/ option. On wireless + <tag><label id="babel-type">type wired|wireless|tunnel </tag> + This option specifies the interface type: Wired, wireless or tunnel. On + wired interfaces a neighbor is considered unreachable after a small number + of Hello packets are lost, as described by <cf/limit/ option. On wireless interfaces the ETX link quality estimation technique is used to compute the metrics of routes discovered over this interface. This technique will gradually degrade the metric of routes when packets are lost rather than - the more binary up/down mechanism of wired type links. Default: - <cf/wired/. + the more binary up/down mechanism of wired type links. A tunnel is like a + wired interface, but turns on RTT-based metrics with a default cost of 96. + Default: <cf/wired/. <tag><label id="babel-rxcost">rxcost <m/num/</tag> This option specifies the nominal RX cost of the interface. The effective @@ -2041,6 +2047,37 @@ protocol babel [<name>] { hop when IPv4 addresses are absent from the interface as described in <rfc id="9229">. Default: yes. + <tag><label id="babel-rtt-cost">rtt cost <m/number/</tag> + The RTT-based cost that will be applied to all routes from each neighbour + based on the measured RTT to that neighbour. If this value is set, + timestamps will be included in generated Babel Hello and IHU messages, and + (if the neighbours also have timestamps enabled), the RTT to each + neighbour will be computed. An additional cost is added to a neighbour if + its RTT is above the <ref id="babel-rtt-min" name="rtt min"> value + configured on the interface. The added cost scales linearly from 0 up to + the RTT cost configured in this option; the full cost is applied if the + neighbour RTT reaches the RTT configured in the <ref id="babel-rtt-max" + name="rtt max"> option (and for all RTTs above this value). Default: 0 + (disabled), except for tunnel interfaces, where it is 96. + + <tag><label id="babel-rtt-min">rtt min <m/time/ s|ms</tag> + The minimum RTT above which the RTT cost will start to be applied (scaling + linearly from zero up to the full cost). Default: 10 ms + + <tag><label id="babel-rtt-max">rtt max <m/time/ s|ms</tag> + The maximum RTT above which the full RTT cost will start be applied. + Default: 120 ms + + <tag><label id="babel-rtt-decay">rtt decay <m/number/</tag> + The decay factor used for the exponentional moving average of the RTT + samples from each neighbour, in units of 1/256. Higher values discards old + RTT samples faster. Must be between 1 and 256. Default: 42 + + <tag><label id="babel-send-timestamps">send timestamps <m/switch/</tag> + Whether to send the timestamps used for RTT calculation on this interface. + Sending the timestamps enables peers to calculate an RTT to this node, + even if no RTT cost is applied to the route metrics. Default: yes. + <tag><label id="babel-authentication">authentication none|mac [permissive]</tag> Selects authentication method to be used. <cf/none/ means that packets are not authenticated at all, <cf/mac/ means MAC authentication is diff --git a/filter/trie_test.c b/filter/trie_test.c index dc791280..5724e49f 100644 --- a/filter/trie_test.c +++ b/filter/trie_test.c @@ -28,12 +28,6 @@ struct f_prefix_node { struct f_prefix prefix; }; -static u32 -xrandom(u32 max) -{ - return (bt_random() % max); -} - static inline uint get_exp_random(void) { @@ -96,26 +90,9 @@ is_prefix_included(list *prefixes, const net_addr *needle) } static void -get_random_net(net_addr *net, int v6) -{ - if (!v6) - { - uint pxlen = xrandom(24)+8; - ip4_addr ip4 = ip4_from_u32((u32) bt_random()); - net_fill_ip4(net, ip4_and(ip4, ip4_mkmask(pxlen)), pxlen); - } - else - { - uint pxlen = xrandom(120)+8; - ip6_addr ip6 = ip6_build(bt_random(), bt_random(), bt_random(), bt_random()); - net_fill_ip6(net, ip6_and(ip6, ip6_mkmask(pxlen)), pxlen); - } -} - -static void get_random_prefix(struct f_prefix *px, int v6, int tight) { - get_random_net(&px->net, v6); + bt_random_net(&px->net, !v6 ? NET_IP4 : NET_IP6); if (tight) { @@ -379,7 +356,7 @@ select_random_prefix_subset(list *src[], net_addr dst[], int sn, int dn) struct f_prefix_node *px; WALK_LIST(px, *src[i]) { - if (xrandom(rnd) != 0) + if (bt_random_n(rnd) != 0) continue; net_copy(&dst[n], &px->prefix.net); @@ -395,7 +372,7 @@ done: /* Shuffle networks */ for (int i = 0; i < dn; i++) { - int j = xrandom(dn); + int j = bt_random_n(dn); if (i == j) continue; @@ -444,7 +421,7 @@ t_match_random_net(void) for (int i = 0; i < PREFIX_TESTS_NUM; i++) { net_addr net; - get_random_net(&net, v6); + bt_random_net(&net, !v6 ? NET_IP4 : NET_IP6); test_match_net(prefixes, trie, &net); } @@ -828,7 +805,7 @@ t_trie_walk_to_root(void) for (i = 0; i < (PREFIX_TESTS_NUM / 10); i++) { net_addr from; - get_random_net(&from, v6); + bt_random_net(&from, !v6 ? NET_IP4 : NET_IP6); net_addr found[129]; int found_num = find_covering_nets(pxset, num, &from, found); diff --git a/lib/birdlib.h b/lib/birdlib.h index e03bd0b2..b7226411 100644 --- a/lib/birdlib.h +++ b/lib/birdlib.h @@ -162,6 +162,7 @@ void bug(const char *msg, ...) NORET; void debug(const char *msg, ...); /* Printf to debug output */ void debug_safe(const char *msg); /* Printf to debug output, async-safe */ + /* Debugging */ #if defined(LOCAL_DEBUG) || defined(GLOBAL_DEBUG) @@ -196,10 +197,36 @@ asm( ); #endif + /* Pseudorandom numbers */ u32 random_u32(void); void random_init(void); void random_bytes(void *buf, size_t size); + +/* Hashing */ + +/* Constant parameter for non-parametrized hashes */ +#define HASH_PARAM 2902958171u + +/* Precomputed powers of HASH_PARAM */ +#define HASH_PARAM1 ((u64) HASH_PARAM) +#define HASH_PARAM2 (HASH_PARAM1 * HASH_PARAM) +#define HASH_PARAM3 (HASH_PARAM2 * HASH_PARAM) +#define HASH_PARAM4 (HASH_PARAM3 * HASH_PARAM) + +/* Reduce intermediate 64-bit value to final 32-bit value */ +static inline u32 hash_value(u64 a) +{ return ((u32) a) ^ ((u32) (a >> 32)); } + +static inline u64 u32_hash0(u32 v, u32 p, u64 acc) +{ return (acc + v) * p; } + +static inline u64 u64_hash0(u64 v, u32 p, u64 acc) +{ return u32_hash0(v >> 32, p, u32_hash0(v, p, acc)); } + +static inline u32 u64_hash(u64 v) +{ return hash_value(u64_hash0(v, HASH_PARAM, 0)); } + #endif @@ -237,7 +237,7 @@ mem_hash(const void *p, uint s) } static inline uint -ptr_hash(void *ptr) +ptr_hash(const void *ptr) { uintptr_t p = (uintptr_t) ptr; return p ^ (p << 8) ^ (p >> 16); @@ -194,14 +194,37 @@ static inline int ipa_nonzero2(ip_addr a) * Hash and compare functions */ +static inline u64 ip4_hash0(ip4_addr a, u32 p, u64 acc) +{ return (acc + _I(a)) * p; } + static inline u32 ip4_hash(ip4_addr a) -{ return u32_hash(_I(a)); } +{ + // return hash_value(ip4_hash0(a, HASH_PARAM, 0)); + + /* For some reason, the old hash works slightly better */ + return u32_hash(_I(a)); +} + +static inline u64 ip6_hash0(ip6_addr a, u32 p, u64 acc) +{ + acc += _I0(a); acc *= p; + acc += _I1(a); acc *= p; + acc += _I2(a); acc *= p; + acc += _I3(a); acc *= p; + return acc; +} static inline u32 ip6_hash(ip6_addr a) { - /* Returns a 32-bit hash key, although low-order bits are not mixed */ - u32 x = _I0(a) ^ _I1(a) ^ _I2(a) ^ _I3(a); - return x ^ (x << 16) ^ (x << 24); + // return hash_value(ip6_hash0(a, HASH_PARAM, 0)); + + /* Just use the expanded form */ + u64 acc = + _I0(a) * HASH_PARAM4 + + _I1(a) * HASH_PARAM3 + + _I2(a) * HASH_PARAM2 + + _I3(a) * HASH_PARAM1; + return hash_value(acc); } static inline int ip4_compare(ip4_addr a, ip4_addr b) @@ -479,39 +479,47 @@ static inline void net_copy_mpls(net_addr_mpls *dst, const net_addr_mpls *src) { memcpy(dst, src, sizeof(net_addr_mpls)); } -/* XXXX */ -static inline u32 u64_hash(u64 a) -{ return u32_hash(a); } +static inline u32 px4_hash(ip4_addr prefix, u32 pxlen) +{ return ip4_hash(prefix) ^ (pxlen << 26); } + +static inline u32 px6_hash(ip6_addr prefix, u32 pxlen) +{ return ip6_hash(prefix) ^ (pxlen << 26); } static inline u32 net_hash_ip4(const net_addr_ip4 *n) -{ return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26); } +{ return px4_hash(n->prefix, n->pxlen); } static inline u32 net_hash_ip6(const net_addr_ip6 *n) -{ return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26); } +{ return px6_hash(n->prefix, n->pxlen); } static inline u32 net_hash_vpn4(const net_addr_vpn4 *n) -{ return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26) ^ u64_hash(n->rd); } +{ + u64 acc = ip4_hash0(n->prefix, HASH_PARAM, 0) ^ (n->pxlen << 26); + return hash_value(u64_hash0(n->rd, HASH_PARAM, acc)); +} static inline u32 net_hash_vpn6(const net_addr_vpn6 *n) -{ return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26) ^ u64_hash(n->rd); } +{ + u64 acc = ip6_hash0(n->prefix, HASH_PARAM, 0) ^ (n->pxlen << 26); + return hash_value(u64_hash0(n->rd, HASH_PARAM, acc)); +} static inline u32 net_hash_roa4(const net_addr_roa4 *n) -{ return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26); } +{ return px4_hash(n->prefix, n->pxlen); } static inline u32 net_hash_roa6(const net_addr_roa6 *n) -{ return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26); } +{ return px6_hash(n->prefix, n->pxlen); } static inline u32 net_hash_flow4(const net_addr_flow4 *n) -{ return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26); } +{ return px4_hash(n->prefix, n->pxlen); } static inline u32 net_hash_flow6(const net_addr_flow6 *n) -{ return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26); } +{ return px6_hash(n->prefix, n->pxlen); } static inline u32 net_hash_ip6_sadr(const net_addr_ip6_sadr *n) -{ return net_hash_ip6((net_addr_ip6 *) n); } +{ return px6_hash(n->dst_prefix, n->dst_pxlen); } static inline u32 net_hash_mpls(const net_addr_mpls *n) -{ return n->label; } +{ return u32_hash(n->label); } u32 net_hash(const net_addr *a); diff --git a/lib/timer.h b/lib/timer.h index c5ea430c..0f87852b 100644 --- a/lib/timer.h +++ b/lib/timer.h @@ -46,6 +46,9 @@ extern struct timeloop main_timeloop; btime current_time(void); btime current_real_time(void); +/* In sysdep code */ +btime current_time_now(void); + //#define now (current_time() TO_S) //#define now_real (current_real_time() TO_S) extern btime boot_time; diff --git a/nest/Makefile b/nest/Makefile index 163a1199..5a244c75 100644 --- a/nest/Makefile +++ b/nest/Makefile @@ -9,6 +9,6 @@ $(o)proto-build.c: Makefile $(lastword $(MAKEFILE_LIST)) $(objdir)/.dir-stamp prepare: $(o)proto-build.c -tests_src := a-set_test.c a-path_test.c +tests_src := a-set_test.c a-path_test.c rt-fib_test.c tests_targets := $(tests_targets) $(tests-target-files) tests_objs := $(tests_objs) $(src-o-files) diff --git a/nest/config.Y b/nest/config.Y index e78350ca..c83c715b 100644 --- a/nest/config.Y +++ b/nest/config.Y @@ -154,6 +154,8 @@ CF_ENUM_PX(T_ENUM_AF, AF_, AFI_, IPV4, IPV6) CF_GRAMMAR +kw_sym: MIN MAX ; + /* Setting of router ID */ conf: rtrid ; diff --git a/nest/rt-fib_test.c b/nest/rt-fib_test.c new file mode 100644 index 00000000..2dd7ce8a --- /dev/null +++ b/nest/rt-fib_test.c @@ -0,0 +1,246 @@ +/* + * BIRD -- Forwarding Information Base -- Tests + * + * (c) 2023 CZ.NIC z.s.p.o. + * + * Can be freely distributed and used under the terms of the GNU GPL. + */ + +#include "test/birdtest.h" +#include "test/bt-utils.h" + +#include "nest/route.h" + + +#define TESTS_NUM 10 +#define PREFIXES_NUM 400000 +#define PREFIX_TESTS_NUM 200000 +#define PREFIX_BENCH_MAX 1000000 +#define PREFIX_BENCH_NUM 10000000 + +struct test_node +{ + int pos; + struct fib_node n; +}; + +static inline int net_match(struct test_node *tn, net_addr *query, net_addr *data) +{ return (tn->pos < PREFIXES_NUM) && net_equal(query, &data[tn->pos]); } + +static int +t_match_random_net(void) +{ + bt_bird_init(); + bt_config_parse(BT_CONFIG_SIMPLE); + + for (int round = 0; round < TESTS_NUM; round++) + { + int type = !(round & 1) ? NET_IP4 : NET_IP6; + + pool *p = rp_new(&root_pool, "FIB pool"); + net_addr *nets = bt_random_nets(type, PREFIXES_NUM); + + /* Make FIB structure */ + struct fib f; + fib_init(&f, &root_pool, type, sizeof(struct test_node), OFFSETOF(struct test_node, n), 4, NULL); + + for (int i = 0; i < PREFIXES_NUM; i++) + { + struct test_node *tn = fib_get(&f, &nets[i]); + bt_assert(!tn->pos || net_match(tn, &nets[i], nets)); + tn->pos = i; + } + + /* Test (mostly) negative matches */ + for (int i = 0; i < PREFIX_TESTS_NUM; i++) + { + net_addr net; + bt_random_net(&net, type); + + struct test_node *tn = fib_find(&f, &net); + bt_assert(!tn || net_match(tn, &net, nets)); + } + + /* Test positive matches */ + for (int i = 0; i < PREFIX_TESTS_NUM; i++) + { + int j = bt_random_n(PREFIXES_NUM); + + struct test_node *tn = fib_find(&f, &nets[j]); + bt_assert(tn && net_match(tn, &nets[j], nets)); + } + + rfree(p); + tmp_flush(); + } + + bt_bird_cleanup(); + return 1; +} + +static int +t_fib_walk(void) +{ + bt_bird_init(); + bt_config_parse(BT_CONFIG_SIMPLE); + + for (int round = 0; round < TESTS_NUM; round++) + { + int type = !(round & 1) ? NET_IP4 : NET_IP6; + + pool *p = rp_new(&root_pool, "FIB pool"); + net_addr *nets = bt_random_nets(type, PREFIXES_NUM); + byte *marks = tmp_allocz(PREFIXES_NUM); + + /* Make FIB structure */ + struct fib f; + fib_init(&f, p, type, sizeof(struct test_node), OFFSETOF(struct test_node, n), 4, NULL); + + for (int i = 1; i < PREFIXES_NUM; i++) + { + struct test_node *tn = fib_get(&f, &nets[i]); + bt_assert(!tn->pos || net_match(tn, &nets[i], nets)); + if (tn->pos) + { + /* Mark dupicate nets */ + bt_assert(!marks[tn->pos]); + marks[tn->pos] = 1; + } + tn->pos = i; + } + + /* Walk FIB and mark nets */ + FIB_WALK(&f, struct test_node, tn) + { + bt_assert(!marks[tn->pos]); + marks[tn->pos] = 1; + } + FIB_WALK_END; + + /* Check in all nets are marked */ + for (int i = 1; i < PREFIXES_NUM; i++) + bt_assert(marks[i]); + + rfree(p); + tmp_flush(); + } + + bt_bird_cleanup(); + return 1; +} + +static int +benchmark_fib_dataset(const char *filename, int type) +{ + net_addr *nets, *test_r, *test_s; + uint n = PREFIX_BENCH_MAX; + int tn = PREFIX_BENCH_NUM; + int match; + + bt_reset_suite_case_timer(); + bt_log_suite_case_result(1, "Reading %s", filename, n); + nets = bt_read_net_file(filename, type, &n); + bt_log_suite_case_result(1, "Read net data, %u nets", n); + bt_reset_suite_case_timer(); + + pool *p = rp_new(&root_pool, "FIB pool"); + + /* Make FIB structure */ + struct fib f; + fib_init(&f, p, type, sizeof(struct test_node), OFFSETOF(struct test_node, n), 0, NULL); + + for (int i = 0; i < (int) n; i++) + { + struct test_node *tn = fib_get(&f, &nets[i]); + tn->pos = i; + } + + bt_log_suite_case_result(1, "Fill FIB structure, %u nets, order %u", n, f.hash_order); + bt_reset_suite_case_timer(); + + /* Compute FIB size */ + size_t fib_size = rmemsize(p).effective * 1000 / (1024*1024); + bt_log_suite_case_result(1, "FIB size: %u.%03u MB", (uint) (fib_size / 1000), (uint) (fib_size % 1000)); + + /* Compute FIB histogram */ + uint hist[16] = {}; + uint sum = 0; + for (uint i = 0; i < f.hash_size; i++) + { + int len = 0; + for (struct fib_node *fn = f.hash_table[i]; fn; fn = fn->next) + len++; + + sum += len; + len = MIN(len, 15); + hist[len]++; + } + bt_log_suite_case_result(1, "FIB histogram:"); + for (uint i = 0; i < 16; i++) + if (hist[i]) + bt_log_suite_case_result(1, "%02u: %8u", i, hist[i]); + + uint avg = (sum * 1000) / (f.hash_size - hist[0]); + bt_log_suite_case_result(1, "FIB chain length: %u.%03u", (uint) (avg / 1000), (uint) (avg % 1000)); + bt_reset_suite_case_timer(); + + /* Make test data */ + test_r = bt_random_nets(type, tn); + test_s = bt_random_net_subset(nets, n, tn); + + bt_log_suite_case_result(1, "Make test data, 2x %u nets", tn); + bt_reset_suite_case_timer(); + + /* Test (mostly negative) random matches */ + match = 0; + for (int i = 0; i < tn; i++) + if (fib_find(&f, &test_r[i])) + match++; + + bt_log_suite_case_result(1, "Random match, %d / %d matches", match, tn); + bt_reset_suite_case_timer(); + + /* Test (positive) subset matches */ + match = 0; + for (int i = 0; i < tn; i++) + if (fib_find(&f, &test_s[i])) + match++; + + bt_log_suite_case_result(1, "Subset match, %d / %d matches", match, tn); + bt_log_suite_case_result(1, ""); + bt_reset_suite_case_timer(); + + rfree(p); + tmp_flush(); + return 1; +} + +static int UNUSED +t_bench_fib_datasets(void) +{ + bt_bird_init(); + bt_config_parse(BT_CONFIG_SIMPLE); + + /* Specific datasets, not included */ + benchmark_fib_dataset("fib-data-bgp-v4-1", NET_IP4); + benchmark_fib_dataset("fib-data-bgp-v4-10", NET_IP4); + benchmark_fib_dataset("fib-data-bgp-v6-1", NET_IP6); + benchmark_fib_dataset("fib-data-bgp-v6-10", NET_IP6); + + bt_bird_cleanup(); + + return 1; +} + +int +main(int argc, char *argv[]) +{ + bt_init(argc, argv); + + bt_test_suite(t_match_random_net, "Testing random prefix matching"); + bt_test_suite(t_fib_walk, "Testing FIB_WALK() on random FIB"); + + // bt_test_suite(t_bench_fib_datasets, "Benchmark FIB from datasets by random subset of nets"); + + return bt_exit_value(); +} diff --git a/proto/babel/babel.c b/proto/babel/babel.c index 9f33dd34..7f0cca73 100644 --- a/proto/babel/babel.c +++ b/proto/babel/babel.c @@ -596,6 +596,7 @@ babel_update_cost(struct babel_neighbor *nbr) switch (cf->type) { case BABEL_IFACE_TYPE_WIRED: + case BABEL_IFACE_TYPE_TUNNEL: /* k-out-of-j selection - Appendix 2.1 in the RFC. */ /* Link is bad if less than cf->limit/16 of expected hellos were received */ @@ -624,6 +625,24 @@ babel_update_cost(struct babel_neighbor *nbr) break; } + if (cf->rtt_cost && nbr->srtt > cf->rtt_min) + { + uint rtt_cost = cf->rtt_cost; + + if (nbr->srtt < cf->rtt_max) + { + uint rtt_interval = cf->rtt_max TO_US - cf->rtt_min TO_US; + uint rtt_diff = (nbr->srtt TO_US - cf->rtt_min TO_US); + + rtt_cost = (rtt_cost * rtt_diff) / rtt_interval; + } + + txcost = MIN(txcost + rtt_cost, BABEL_INFINITY); + + TRACE(D_EVENTS, "Added RTT cost %u to nbr %I on %s with srtt %t ms", + rtt_cost, nbr->addr, nbr->ifa->iface->name, nbr->srtt * 1000); + } + done: /* If RX cost changed, send IHU with next Hello */ if (rxcost != nbr->rxcost) @@ -854,6 +873,12 @@ babel_build_ihu(union babel_msg *msg, struct babel_iface *ifa, struct babel_neig msg->ihu.rxcost = n->rxcost; msg->ihu.interval = ifa->cf->ihu_interval; + if (n->last_tstamp_rcvd && ifa->cf->rtt_send) + { + msg->ihu.tstamp = n->last_tstamp; + msg->ihu.tstamp_rcvd = n->last_tstamp_rcvd TO_US; + } + TRACE(D_PACKETS, "Sending IHU for %I with rxcost %d interval %t", msg->ihu.addr, msg->ihu.rxcost, (btime) msg->ihu.interval); } @@ -893,6 +918,9 @@ babel_send_hello(struct babel_iface *ifa, uint interval) msg.hello.seqno = ifa->hello_seqno++; msg.hello.interval = interval ?: ifa->cf->hello_interval; + if (ifa->cf->rtt_send) + msg.hello.tstamp = 1; /* real timestamp will be set on TLV write */ + TRACE(D_PACKETS, "Sending hello on %s with seqno %d interval %t", ifa->ifname, msg.hello.seqno, (btime) msg.hello.interval); @@ -1199,14 +1227,26 @@ babel_handle_hello(union babel_msg *m, struct babel_iface *ifa) msg->seqno, (btime) msg->interval); struct babel_neighbor *n = babel_get_neighbor(ifa, msg->sender); + struct babel_iface_config *cf = n->ifa->cf; int first_hello = !n->hello_cnt; + if (msg->tstamp) + { + n->last_tstamp = msg->tstamp; + n->last_tstamp_rcvd = msg->pkt_received; + } babel_update_hello_history(n, msg->seqno, msg->interval); babel_update_cost(n); /* Speed up session establishment by sending IHU immediately */ if (first_hello) - babel_send_ihu(ifa, n); + { + /* if using RTT, all IHUs must be paired with hellos */ + if(cf->rtt_send) + babel_send_hello(ifa, 0); + else + babel_send_ihu(ifa, n); + } } void @@ -1225,6 +1265,39 @@ babel_handle_ihu(union babel_msg *m, struct babel_iface *ifa) struct babel_neighbor *n = babel_get_neighbor(ifa, msg->sender); n->txcost = msg->rxcost; n->ihu_expiry = current_time() + BABEL_IHU_EXPIRY_FACTOR(msg->interval); + + if (msg->tstamp) + { + u32 rtt_sample = 0, pkt_received = msg->pkt_received TO_US; + int remote_time, full_time; + + /* processing time reported by peer */ + remote_time = (n->last_tstamp - msg->tstamp_rcvd); + /* time since we sent the last timestamp - RTT including remote time */ + full_time = (pkt_received - msg->tstamp); + + /* sanity checks */ + if (remote_time < 0 || full_time < 0 || + remote_time US_ > BABEL_RTT_MAX_VALUE || full_time US_ > BABEL_RTT_MAX_VALUE) + goto out; + + if (remote_time < full_time) + rtt_sample = full_time - remote_time; + + if (n->srtt) + { + uint decay = n->ifa->cf->rtt_decay; + + n->srtt = (decay * rtt_sample + (256 - decay) * n->srtt) / 256; + } + else + n->srtt = rtt_sample; + + TRACE(D_EVENTS, "RTT sample for neighbour %I on %s: %u us (srtt %t ms)", + n->addr, ifa->ifname, rtt_sample, n->srtt * 1000); + } + +out: babel_update_cost(n); } @@ -2199,8 +2272,8 @@ babel_show_neighbors(struct proto *P, const char *iff) } cli_msg(-1024, "%s:", p->p.name); - cli_msg(-1024, "%-25s %-10s %6s %6s %6s %7s %4s", - "IP address", "Interface", "Metric", "Routes", "Hellos", "Expires", "Auth"); + cli_msg(-1024, "%-25s %-10s %6s %6s %6s %7s %4s %9s", + "IP address", "Interface", "Metric", "Routes", "Hellos", "Expires", "Auth", "RTT (ms)"); WALK_LIST(ifa, p->interfaces) { @@ -2215,9 +2288,10 @@ babel_show_neighbors(struct proto *P, const char *iff) uint hellos = u32_popcount(n->hello_map); btime timer = (n->hello_expiry ?: n->init_expiry) - current_time(); - cli_msg(-1024, "%-25I %-10s %6u %6u %6u %7t %-4s", + cli_msg(-1024, "%-25I %-10s %6u %6u %6u %7t %-4s %9t", n->addr, ifa->iface->name, n->cost, rts, hellos, MAX(timer, 0), - n->auth_passed ? "Yes" : "No"); + n->auth_passed ? "Yes" : "No", + n->srtt * 1000); } } } diff --git a/proto/babel/babel.h b/proto/babel/babel.h index dcd303e1..edde4cab 100644 --- a/proto/babel/babel.h +++ b/proto/babel/babel.h @@ -53,10 +53,16 @@ #define BABEL_GARBAGE_INTERVAL (300 S_) #define BABEL_RXCOST_WIRED 96 #define BABEL_RXCOST_WIRELESS 256 +#define BABEL_RXCOST_RTT 96 #define BABEL_INITIAL_HOP_COUNT 255 #define BABEL_MAX_SEND_INTERVAL 5 /* Unused ? */ #define BABEL_INITIAL_NEIGHBOR_TIMEOUT (60 S_) +#define BABEL_RTT_MAX_VALUE (600 S_) +#define BABEL_RTT_MIN (10 MS_) +#define BABEL_RTT_MAX (120 MS_) +#define BABEL_RTT_DECAY 42 + /* Max interval that will not overflow when carried as 16-bit centiseconds */ #define BABEL_TIME_UNITS 10000 /* On-wire times are counted in centiseconds */ #define BABEL_MIN_INTERVAL (0x0001 * BABEL_TIME_UNITS) @@ -96,6 +102,8 @@ enum babel_tlv_type { enum babel_subtlv_type { BABEL_SUBTLV_PAD1 = 0, BABEL_SUBTLV_PADN = 1, + BABEL_SUBTLV_DIVERSITY = 2, /* we don't support this */ + BABEL_SUBTLV_TIMESTAMP = 3, /* Mandatory subtlvs */ BABEL_SUBTLV_SOURCE_PREFIX = 128, @@ -106,6 +114,7 @@ enum babel_iface_type { BABEL_IFACE_TYPE_UNDEF = 0, BABEL_IFACE_TYPE_WIRED = 1, BABEL_IFACE_TYPE_WIRELESS = 2, + BABEL_IFACE_TYPE_TUNNEL = 3, BABEL_IFACE_TYPE_MAX }; @@ -141,6 +150,12 @@ struct babel_iface_config { uint ihu_interval; /* IHU interval, in us */ uint update_interval; /* Update interval, in us */ + btime rtt_min; /* rtt above which to start penalising metric */ + btime rtt_max; /* max rtt metric penalty applied above this */ + u16 rtt_cost; /* metric penalty to apply at rtt_max */ + u16 rtt_decay; /* decay of neighbour RTT (units of 1/256) */ + u8 rtt_send; /* whether to send timestamps on this interface */ + u16 rx_buffer; /* RX buffer size, 0 for MTU */ u16 tx_length; /* TX packet length limit (including headers), 0 for MTU */ int tx_tos; @@ -229,6 +244,10 @@ struct babel_neighbor { u16 next_hello_seqno; uint last_hello_int; + u32 last_tstamp; + btime last_tstamp_rcvd; + btime srtt; + u32 auth_pc_unicast; u32 auth_pc_multicast; u8 auth_passed; @@ -326,6 +345,8 @@ struct babel_msg_hello { u16 seqno; uint interval; ip_addr sender; + u32 tstamp; + btime pkt_received; }; struct babel_msg_ihu { @@ -335,6 +356,9 @@ struct babel_msg_ihu { uint interval; ip_addr addr; ip_addr sender; + u32 tstamp; + u32 tstamp_rcvd; + btime pkt_received; }; struct babel_msg_update { diff --git a/proto/babel/config.Y b/proto/babel/config.Y index 1b4dc6f5..b8af0267 100644 --- a/proto/babel/config.Y +++ b/proto/babel/config.Y @@ -26,7 +26,7 @@ CF_KEYWORDS(BABEL, INTERFACE, METRIC, RXCOST, HELLO, UPDATE, INTERVAL, PORT, TYPE, WIRED, WIRELESS, RX, TX, BUFFER, PRIORITY, LENGTH, CHECK, LINK, NEXT, HOP, IPV4, IPV6, BABEL_METRIC, SHOW, INTERFACES, NEIGHBORS, ENTRIES, RANDOMIZE, ROUTER, ID, AUTHENTICATION, NONE, MAC, PERMISSIVE, - EXTENDED) + EXTENDED, TUNNEL, RTT, MIN, MAX, DECAY, SEND, TIMESTAMPS) CF_GRAMMAR @@ -67,6 +67,10 @@ babel_iface_start: BABEL_IFACE->limit = BABEL_HELLO_LIMIT; BABEL_IFACE->tx_tos = IP_PREC_INTERNET_CONTROL; BABEL_IFACE->tx_priority = sk_priority_control; + BABEL_IFACE->rtt_min = BABEL_RTT_MIN; + BABEL_IFACE->rtt_max = BABEL_RTT_MAX; + BABEL_IFACE->rtt_decay = BABEL_RTT_DECAY; + BABEL_IFACE->rtt_send = 1; BABEL_IFACE->check_link = 1; BABEL_IFACE->ext_next_hop = 1; }; @@ -87,8 +91,16 @@ babel_iface_finish: BABEL_IFACE->hello_interval = BABEL_HELLO_INTERVAL_WIRED; if (!BABEL_IFACE->rxcost) BABEL_IFACE->rxcost = BABEL_RXCOST_WIRED; + if (BABEL_IFACE->type == BABEL_IFACE_TYPE_TUNNEL && !BABEL_IFACE->rtt_cost) + BABEL_IFACE->rtt_cost = BABEL_RXCOST_RTT; } + if (BABEL_IFACE->rtt_cost && !BABEL_IFACE->rtt_send) + cf_error("Can't set RTT cost when sending timestamps is disabled"); + + if (BABEL_IFACE->rtt_min >= BABEL_IFACE->rtt_max) + cf_error("Min RTT must be smaller than max RTT"); + /* Make sure we do not overflow the 16-bit centisec fields */ if (!BABEL_IFACE->update_interval) BABEL_IFACE->update_interval = MIN_(BABEL_IFACE->hello_interval*BABEL_UPDATE_INTERVAL_FACTOR, BABEL_MAX_INTERVAL); @@ -136,6 +148,7 @@ babel_iface_item: | LIMIT expr { BABEL_IFACE->limit = $2; if (($2<1) || ($2>16)) cf_error("Limit must be in range 1-16"); } | TYPE WIRED { BABEL_IFACE->type = BABEL_IFACE_TYPE_WIRED; } | TYPE WIRELESS { BABEL_IFACE->type = BABEL_IFACE_TYPE_WIRELESS; } + | TYPE TUNNEL { BABEL_IFACE->type = BABEL_IFACE_TYPE_TUNNEL; } | HELLO INTERVAL expr_us { BABEL_IFACE->hello_interval = $3; if (($3<BABEL_MIN_INTERVAL) || ($3>BABEL_MAX_INTERVAL)) cf_error("Hello interval must be in range 10 ms - 655 s"); } | UPDATE INTERVAL expr_us { BABEL_IFACE->update_interval = $3; if (($3<BABEL_MIN_INTERVAL) || ($3>BABEL_MAX_INTERVAL)) cf_error("Update interval must be in range 10 ms - 655 s"); } | RX BUFFER expr { BABEL_IFACE->rx_buffer = $3; if (($3<256) || ($3>65535)) cf_error("RX buffer must be in range 256-65535"); } @@ -149,6 +162,11 @@ babel_iface_item: | AUTHENTICATION NONE { BABEL_IFACE->auth_type = BABEL_AUTH_NONE; } | AUTHENTICATION MAC { BABEL_IFACE->auth_type = BABEL_AUTH_MAC; BABEL_IFACE->auth_permissive = 0; } | AUTHENTICATION MAC PERMISSIVE { BABEL_IFACE->auth_type = BABEL_AUTH_MAC; BABEL_IFACE->auth_permissive = 1; } + | RTT MIN expr_us { BABEL_IFACE->rtt_min = $3; } + | RTT MAX expr_us { BABEL_IFACE->rtt_max = $3; } + | RTT COST expr { BABEL_IFACE->rtt_cost = $3; if ($3 >= BABEL_INFINITY) cf_error("RTT cost must be < 65535"); } + | RTT DECAY expr { BABEL_IFACE->rtt_decay = $3; if (($3 < 1) || ($3 > 256)) cf_error("RTT decay must be between 1-256"); } + | SEND TIMESTAMPS bool { BABEL_IFACE->rtt_send = $3; } | password_list ; diff --git a/proto/babel/packets.c b/proto/babel/packets.c index 61c94cc5..f1895655 100644 --- a/proto/babel/packets.c +++ b/proto/babel/packets.c @@ -58,6 +58,13 @@ struct babel_tlv_ihu { u8 addr[0]; } PACKED; +struct babel_subtlv_timestamp { + u8 type; + u8 length; + u32 tstamp; + u32 tstamp_rcvd; /* only used in IHU */ +} PACKED; + struct babel_tlv_router_id { u8 type; u8 length; @@ -161,6 +168,7 @@ struct babel_parse_state { const struct babel_tlv_data* (*get_subtlv_data)(u8 type); struct babel_proto *proto; struct babel_iface *ifa; + btime received_time; ip_addr saddr; ip_addr next_hop_ip4; ip_addr next_hop_ip6; @@ -172,6 +180,7 @@ struct babel_parse_state { u8 def_ip6_prefix_seen; /* def_ip6_prefix is valid */ u8 def_ip4_prefix_seen; /* def_ip4_prefix is valid */ u8 def_ip4_via_ip6_prefix_seen; /* def_ip4_via_ip6_prefix is valid */ + u8 hello_tstamp_seen; /* pkt contains a hello timestamp */ u8 current_tlv_endpos; /* End of self-terminating TLVs (offset from start) */ u8 sadr_enabled; u8 is_unicast; @@ -336,6 +345,7 @@ static int babel_read_update(struct babel_tlv *hdr, union babel_msg *msg, struct static int babel_read_route_request(struct babel_tlv *hdr, union babel_msg *msg, struct babel_parse_state *state); static int babel_read_seqno_request(struct babel_tlv *hdr, union babel_msg *msg, struct babel_parse_state *state); static int babel_read_source_prefix(struct babel_tlv *hdr, union babel_msg *msg, struct babel_parse_state *state); +static int babel_read_timestamp(struct babel_tlv *hdr, union babel_msg *msg, struct babel_parse_state *state); static uint babel_write_ack(struct babel_tlv *hdr, union babel_msg *msg, struct babel_write_state *state, uint max_len); static uint babel_write_hello(struct babel_tlv *hdr, union babel_msg *msg, struct babel_write_state *state, uint max_len); @@ -344,6 +354,7 @@ static uint babel_write_update(struct babel_tlv *hdr, union babel_msg *msg, stru static uint babel_write_route_request(struct babel_tlv *hdr, union babel_msg *msg, struct babel_write_state *state, uint max_len); static uint babel_write_seqno_request(struct babel_tlv *hdr, union babel_msg *msg, struct babel_write_state *state, uint max_len); static int babel_write_source_prefix(struct babel_tlv *hdr, net_addr *net, uint max_len); +static int babel_write_timestamp(struct babel_tlv *hdr, u32 tstamp, u32 tstamp_rcvd, uint max_len); static const struct babel_tlv_data tlv_data[BABEL_TLV_MAX] = { [BABEL_TLV_ACK_REQ] = { @@ -419,6 +430,13 @@ static const struct babel_tlv_data *get_packet_tlv_data(u8 type) return type < sizeof(tlv_data) / sizeof(*tlv_data) ? &tlv_data[type] : NULL; } +static const struct babel_tlv_data timestamp_tlv_data = { + sizeof(struct babel_subtlv_timestamp), + babel_read_timestamp, + NULL, + NULL +}; + static const struct babel_tlv_data source_prefix_tlv_data = { sizeof(struct babel_subtlv_source_prefix), babel_read_source_prefix, @@ -430,6 +448,8 @@ static const struct babel_tlv_data *get_packet_subtlv_data(u8 type) { switch (type) { + case BABEL_SUBTLV_TIMESTAMP: + return ×tamp_tlv_data; case BABEL_SUBTLV_SOURCE_PREFIX: return &source_prefix_tlv_data; @@ -491,16 +511,34 @@ babel_read_hello(struct babel_tlv *hdr, union babel_msg *m, static uint babel_write_hello(struct babel_tlv *hdr, union babel_msg *m, - struct babel_write_state *state UNUSED, uint max_len UNUSED) + struct babel_write_state *state UNUSED, uint max_len) { struct babel_tlv_hello *tlv = (void *) hdr; struct babel_msg_hello *msg = &m->hello; + uint len = sizeof(struct babel_tlv_hello); TLV_HDR0(tlv, BABEL_TLV_HELLO); put_u16(&tlv->seqno, msg->seqno); put_time16(&tlv->interval, msg->interval); - return sizeof(struct babel_tlv_hello); + if (msg->tstamp) + { + /* + * There can be a substantial delay between when the babel_msg was created + * and when it is serialised. We don't want this included in the RTT + * measurement, so replace the timestamp with the current time to get as + * close as possible to on-wire time for the packet. + */ + u32 tstamp = current_time_now() TO_US; + + int l = babel_write_timestamp(hdr, tstamp, 0, max_len); + if (l < 0) + return 0; + + len += l; + } + + return len; } static int @@ -565,6 +603,7 @@ babel_write_ihu(struct babel_tlv *hdr, union babel_msg *m, { struct babel_tlv_ihu *tlv = (void *) hdr; struct babel_msg_ihu *msg = &m->ihu; + uint len = sizeof(*tlv); if (ipa_is_link_local(msg->addr) && max_len < sizeof(struct babel_tlv_ihu) + 8) return 0; @@ -576,12 +615,24 @@ babel_write_ihu(struct babel_tlv *hdr, union babel_msg *m, if (!ipa_is_link_local(msg->addr)) { tlv->ae = BABEL_AE_WILDCARD; - return sizeof(struct babel_tlv_ihu); + goto out; } put_ip6_ll(&tlv->addr, msg->addr); tlv->ae = BABEL_AE_IP6_LL; hdr->length += 8; - return sizeof(struct babel_tlv_ihu) + 8; + len += 8; + +out: + if (msg->tstamp) + { + int l = babel_write_timestamp(hdr, msg->tstamp, msg->tstamp_rcvd, max_len); + if (l < 0) + return 0; + + len += l; + } + + return len; } static int @@ -1249,6 +1300,66 @@ babel_write_source_prefix(struct babel_tlv *hdr, net_addr *n, uint max_len) return len; } +static int +babel_read_timestamp(struct babel_tlv *hdr, union babel_msg *msg, + struct babel_parse_state *state) +{ + struct babel_subtlv_timestamp *tlv = (void *) hdr; + + switch (msg->type) + { + case BABEL_TLV_HELLO: + if (tlv->length < 4) + return PARSE_ERROR; + + msg->hello.tstamp = get_u32(&tlv->tstamp); + msg->hello.pkt_received = state->received_time; + state->hello_tstamp_seen = 1; + break; + + case BABEL_TLV_IHU: + if (tlv->length < 8) + return PARSE_ERROR; + + /* RTT calculation relies on a Hello always being present with an IHU */ + if (!state->hello_tstamp_seen) + break; + + msg->ihu.tstamp = get_u32(&tlv->tstamp); + msg->ihu.tstamp_rcvd = get_u32(&tlv->tstamp_rcvd); + msg->ihu.pkt_received = state->received_time; + break; + + default: + return PARSE_ERROR; + } + + return PARSE_SUCCESS; +} + +static int +babel_write_timestamp(struct babel_tlv *hdr, u32 tstamp, u32 tstamp_rcvd, uint max_len) +{ + struct babel_subtlv_timestamp *tlv = (void *) NEXT_TLV(hdr); + uint len = sizeof(*tlv); + + if (hdr->type == BABEL_TLV_HELLO) + len -= 4; + + if (len > max_len) + return -1; + + TLV_HDR(tlv, BABEL_SUBTLV_TIMESTAMP, len); + hdr->length += len; + + put_u32(&tlv->tstamp, tstamp); + + if (hdr->type == BABEL_TLV_IHU) + put_u32(&tlv->tstamp_rcvd, tstamp_rcvd); + + return len; +} + static inline int babel_read_subtlvs(struct babel_tlv *hdr, union babel_msg *msg, @@ -1518,6 +1629,13 @@ babel_process_packet(struct babel_iface *ifa, .saddr = saddr, .next_hop_ip6 = saddr, .sadr_enabled = babel_sadr_enabled(p), + + /* + * The core updates current_time() after returning from poll(), so this is + * actually the time the packet was received, even though there may have + * been a bit of delay before we got to process it + */ + .received_time = current_time(), }; if ((pkt->magic != BABEL_MAGIC) || (pkt->version != BABEL_VERSION)) diff --git a/proto/bgp/config.Y b/proto/bgp/config.Y index 013d14af..218e0d04 100644 --- a/proto/bgp/config.Y +++ b/proto/bgp/config.Y @@ -46,7 +46,7 @@ CF_KEYWORDS(CEASE, PREFIX, LIMIT, HIT, ADMINISTRATIVE, SHUTDOWN, RESET, PEER, CF_GRAMMAR /* Workaround for collisions between keywords and symbols */ -symbol: ROLE | PEER | PROVIDER | CUSTOMER | RS_SERVER | RS_CLIENT ; +kw_sym: ROLE | PEER | PROVIDER | CUSTOMER | RS_SERVER | RS_CLIENT ; proto: bgp_proto '}' ; diff --git a/sysdep/linux/netlink.c b/sysdep/linux/netlink.c index 7f0d4736..e3298a0f 100644 --- a/sysdep/linux/netlink.c +++ b/sysdep/linux/netlink.c @@ -485,7 +485,6 @@ static inline ip_addr rta_get_ipa(struct rtattr *a) return ipa_from_ip6(rta_get_ip6(a)); } -#ifdef HAVE_MPLS_KERNEL static inline ip_addr rta_get_via(struct rtattr *a) { struct rtvia *v = RTA_DATA(a); @@ -496,6 +495,7 @@ static inline ip_addr rta_get_via(struct rtattr *a) return IPA_NONE; } +#ifdef HAVE_MPLS_KERNEL static u32 rta_mpls_stack[MPLS_MAX_LABEL_STACK]; static inline int rta_get_mpls(struct rtattr *a, u32 *stack) { @@ -763,10 +763,8 @@ nl_parse_multipath(struct nl_parse_state *s, struct krt_proto *p, const net_addr if (a[RTA_FLOW]) s->rta_flow = rta_get_u32(a[RTA_FLOW]); -#ifdef HAVE_MPLS_KERNEL if (a[RTA_VIA]) rv->gw = rta_get_via(a[RTA_VIA]); -#endif if (nh->rtnh_flags & RTNH_F_ONLINK) rv->flags |= RNF_ONLINK; @@ -1659,10 +1657,8 @@ nl_parse_route(struct nl_parse_state *s, struct nlmsghdr *h) if (a[RTA_GATEWAY]) ra->nh.gw = rta_get_ipa(a[RTA_GATEWAY]); -#ifdef HAVE_MPLS_KERNEL if (a[RTA_VIA]) ra->nh.gw = rta_get_via(a[RTA_VIA]); -#endif if (i->rtm_flags & RTNH_F_ONLINK) ra->nh.flags |= RNF_ONLINK; diff --git a/sysdep/unix/io.c b/sysdep/unix/io.c index e131ca41..6aedcfb6 100644 --- a/sysdep/unix/io.c +++ b/sysdep/unix/io.c @@ -171,6 +171,19 @@ times_update_real_time(struct timeloop *loop) loop->real_time = ts.tv_sec S + ts.tv_nsec NS; } +btime +current_time_now(void) +{ + struct timespec ts; + int rv; + + rv = clock_gettime(CLOCK_MONOTONIC, &ts); + if (rv < 0) + die("clock_gettime: %m"); + + return ts.tv_sec S + ts.tv_nsec NS; +} + /** * DOC: Sockets diff --git a/test/birdtest.h b/test/birdtest.h index cfeebb98..540092d6 100644 --- a/test/birdtest.h +++ b/test/birdtest.h @@ -37,6 +37,10 @@ int bt_test_suite_base(int (*test_fn)(const void *), const char *test_id, const static inline u64 bt_random(void) { return ((u64) random() & 0xffffffff) | ((u64) random() << 32); } +static inline u32 bt_random_n(u32 max) +{ return random() % max; } + + void bt_log_suite_result(int result, const char *fmt, ...); void bt_log_suite_case_result(int result, const char *fmt, ...); diff --git a/test/bt-utils.c b/test/bt-utils.c index 8496e185..fb42cd35 100644 --- a/test/bt-utils.c +++ b/test/bt-utils.c @@ -219,3 +219,135 @@ bt_bytes_to_hex(char *buf, const byte *in_data, size_t size) sprintf(buf + i*2, "%02x", in_data[i]); } +void +bt_random_net(net_addr *net, int type) +{ + ip4_addr ip4; + ip6_addr ip6; + uint pxlen; + + switch (type) + { + case NET_IP4: + pxlen = bt_random_n(24)+8; + ip4 = ip4_from_u32((u32) bt_random()); + net_fill_ip4(net, ip4_and(ip4, ip4_mkmask(pxlen)), pxlen); + break; + + case NET_IP6: + pxlen = bt_random_n(120)+8; + ip6 = ip6_build(bt_random(), bt_random(), bt_random(), bt_random()); + net_fill_ip6(net, ip6_and(ip6, ip6_mkmask(pxlen)), pxlen); + break; + + default: + die("Net type %d not implemented", type); + } +} + +net_addr * +bt_random_nets(int type, uint n) +{ + net_addr *nets = tmp_alloc(n * sizeof(net_addr)); + + for (uint i = 0; i < n; i++) + bt_random_net(&nets[i], type); + + return nets; +} + +net_addr * +bt_random_net_subset(net_addr *src, uint sn, uint dn) +{ + net_addr *nets = tmp_alloc(dn * sizeof(net_addr)); + + for (uint i = 0; i < dn; i++) + net_copy(&nets[i], &src[bt_random_n(sn)]); + + return nets; +} + +void +bt_read_net(const char *str, net_addr *net, int type) +{ + ip4_addr ip4; + ip6_addr ip6; + uint pxlen; + char addr[64]; + + switch (type) + { + case NET_IP4: + if (sscanf(str, "%[0-9.]/%u", addr, &pxlen) != 2) + goto err; + + if (!ip4_pton(addr, &ip4)) + goto err; + + if (!net_validate_px4(ip4, pxlen)) + goto err; + + net_fill_ip4(net, ip4, pxlen); + break; + + case NET_IP6: + if (sscanf(str, "%[0-9a-fA-F:.]/%u", addr, &pxlen) != 2) + goto err; + + if (!ip6_pton(addr, &ip6)) + goto err; + + if (!net_validate_px6(ip6, pxlen)) + goto err; + + net_fill_ip6(net, ip6, pxlen); + break; + + default: + die("Net type %d not implemented", type); + } + return; + +err: + bt_abort_msg("Invalid network '%s'", str); +} + +net_addr * +bt_read_nets(FILE *f, int type, uint *n) +{ + char str[80]; + + net_addr *nets = tmp_alloc(*n * sizeof(net_addr)); + uint i = 0; + + errno = 0; + while (fgets(str, sizeof(str), f)) + { + if (str[0] == '\n') + break; + + if (i >= *n) + bt_abort_msg("Too many networks"); + + bt_read_net(str, &nets[i], type); + bt_debug("ADD %s\n", str); + i++; + } + bt_syscall(errno, "fgets()"); + + bt_debug("DONE reading %u nets\n", i); + + *n = i; + return nets; +} + +net_addr * +bt_read_net_file(const char *filename, int type, uint *n) +{ + FILE *f = fopen(filename, "r"); + bt_syscall(!f, "fopen(%s)", filename); + net_addr *nets = bt_read_nets(f, type, n); + fclose(f); + + return nets; +} diff --git a/test/bt-utils.h b/test/bt-utils.h index 13d267cc..d29a0b7c 100644 --- a/test/bt-utils.h +++ b/test/bt-utils.h @@ -26,6 +26,12 @@ uint bt_naive_pow(uint base, uint power); void bt_bytes_to_hex(char *buf, const byte *in_data, size_t size); +void bt_random_net(net_addr *net, int type); +net_addr *bt_random_nets(int type, uint n); +net_addr *bt_random_net_subset(net_addr *src, uint sn, uint dn); +void bt_read_net(const char *str, net_addr *net, int type); +net_addr *bt_read_nets(FILE *f, int type, uint *n); +net_addr *bt_read_net_file(const char *filename, int type, uint *n); void bt_bird_init(void); void bt_bird_cleanup(void); |