0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-11-08 12:18:42 +00:00

Merge remote-tracking branch 'origin/mq-filter-stack'

This commit is contained in:
Ondrej Zajicek (work) 2019-07-24 15:38:32 +02:00
commit cec40a7467
73 changed files with 4409 additions and 2936 deletions

View File

@ -79,11 +79,6 @@ docker_fedora-26-amd64:
IMG_NAME: "fedora-26-amd64"
<<: *docker_build
docker_centos-6-amd64:
variables:
IMG_NAME: "centos-6-amd64"
<<: *docker_build
docker_centos-7-amd64:
variables:
IMG_NAME: "centos-7-amd64"
@ -174,13 +169,6 @@ docker_ubuntu-16_04-amd64:
- linux
- amd64
.centos-6-amd64: &centos-6-amd64_env
image: registry.labs.nic.cz/labs/bird:centos-6-amd64
tags:
- docker
- linux
- amd64
.centos-7-amd64: &centos-7-amd64_env
image: registry.labs.nic.cz/labs/bird:centos-7-amd64
tags:
@ -264,10 +252,6 @@ build-fedora-26-amd64:
<<: *fedora-26-amd64_env
<<: *build_job
build-centos-6-amd64:
<<: *centos-6-amd64_env
<<: *build_job
build-centos-7-amd64:
<<: *centos-7-amd64_env
<<: *build_job

View File

@ -77,6 +77,8 @@ $(daemon): LIBS += $(DAEMON_LIBS)
# Include directories
dirs := client conf doc filter lib nest test $(addprefix proto/,$(protocols)) @sysdep_dirs@
# conf/Makefile declarations needed for all other modules
conf-lex-targets := $(addprefix $(objdir)/conf/,cf-lex.o)
conf-y-targets := $(addprefix $(objdir)/conf/,cf-parse.y keywords.h commands.h)
cf-local = $(conf-y-targets): $(s)config.Y
@ -100,6 +102,7 @@ endef
clean = $(eval $(call clean_in,$(1)))
# Include main Makefiles of the directories
include $(addsuffix /Makefile,$(addprefix $(srcdir)/,$(dirs)))
# Generic rules

43
aclocal.m4 vendored
View File

@ -1,6 +1,25 @@
dnl ** Additional Autoconf tests for BIRD configure script
dnl ** (c) 1999 Martin Mares <mj@ucw.cz>
AC_DEFUN([BIRD_CHECK_THREAD_LOCAL],
[
AC_CACHE_CHECK(
[whether _Thread_local is known],
[bird_cv_thread_local],
AC_COMPILE_IFELSE([
AC_LANG_PROGRAM(
[
_Thread_local static int x = 42;
],
[]
)
],
[bird_cv_thread_local=yes],
[bird_cv_thread_local=no]
)
)
])
AC_DEFUN([BIRD_CHECK_PTHREADS],
[
bird_tmp_cflags="$CFLAGS"
@ -131,6 +150,30 @@ AC_DEFUN([BIRD_CHECK_ANDROID_LOG],
)
])
AC_DEFUN([BIRD_CHECK_LTO],
[
bird_tmp_cflags="$CFLAGS"
bird_tmp_ldflags="$LDFLAGS"
CFLAGS="-flto"
LDFLAGS="-flto"
AC_CACHE_CHECK(
[whether link time optimizer is available],
[bird_cv_c_lto],
[
AC_LINK_IFELSE(
[AC_LANG_PROGRAM()],
[bird_cv_c_lto=yes],
[bird_cv_c_lto=no]
)
]
)
CFLAGS="$bird_tmp_cflags"
LDFLAGS="$bird_tmp_ldflags"
])
AC_DEFUN([BIRD_CHECK_GCC_OPTION],
[
bird_tmp_cflags="$CFLAGS"

153
bird-gdb.py Normal file
View File

@ -0,0 +1,153 @@
class BIRDPrinter:
def __init__(self, val):
self.val = val
@classmethod
def lookup(cls, val):
if val.type.code != cls.typeCode:
return None
if val.type.tag != cls.typeTag:
return None
return cls(val)
class BIRDFValPrinter(BIRDPrinter):
"Print BIRD\s struct f_val"
typeCode = gdb.TYPE_CODE_STRUCT
typeTag = "f_val"
codemap = {
"T_INT": "i",
"T_BOOL": "i",
"T_PAIR": "i",
"T_QUAD": "i",
"T_ENUM_RTS": "i",
"T_ENUM_BGP_ORIGIN": "i",
"T_ENUM_SCOPE": "i",
"T_ENUM_RTC": "i",
"T_ENUM_RTD": "i",
"T_ENUM_ROA": "i",
"T_ENUM_NETTYPE": "i",
"T_ENUM_RA_PREFERENCE": "i",
"T_IP": "ip",
"T_NET": "net",
"T_STRING": "s",
"T_PATH_MASK": "path_mask",
"T_PATH": "ad",
"T_CLIST": "ad",
"T_EC": "ec",
"T_ECLIST": "ad",
"T_LC": "lc",
"T_LCLIST": "ad",
"T_RD": "ec",
"T_PATH_MASK_ITEM": "pmi",
"T_SET": "t",
"T_PREFIX_SET": "ti",
}
def to_string(self):
code = self.val['type']
if code.type.code != gdb.TYPE_CODE_ENUM or code.type.tag != "f_type":
raise Exception("Strange 'type' element in f_val")
if str(code) == "T_VOID":
return "T_VOID"
else:
return "(%(c)s) %(v)s" % { "c": code, "v": self.val['val'][self.codemap[str(code)]] }
def display_hint(self):
return "map"
class BIRDFValStackPrinter(BIRDPrinter):
"Print BIRD's struct f_val_stack"
typeCode = gdb.TYPE_CODE_STRUCT
typeTag = "f_val_stack"
def to_string(self):
cnt = self.val['cnt']
return ("Value stack (%(cnt)d):\n\t" % { "cnt": cnt }) + \
"\n\t".join([ (".val[%(n) 3d] = " % { "n": n}) + str(self.val['val'][n]) for n in range(cnt-1, -1, -1) ])
def display_hint(self):
return "map"
class BIRDFInstPrinter(BIRDPrinter):
"Print BIRD's struct f_inst"
typeCode = gdb.TYPE_CODE_STRUCT
typeTag = "f_inst"
def to_string(self):
code = self.val['fi_code']
if str(code) == "FI_NOP":
return str(code) + ": " + str(self.val.cast(gdb.lookup_type("const char [%(siz)d]" % { "siz": self.val.type.sizeof })))
return "%(code)s:\t%(lineno) 6dL\t%(size)6dS\tnext = %(next)s: .i_%(code)s = %(union)s" % {
"code": str(code),
"lineno": self.val['lineno'],
"size": self.val['size'],
"next": str(self.val['next']),
"union": str(self.val['i_' + str(code)])
}
# def children(self): # children iterator
def display_hint(self):
return "map"
class BIRDFLineItemPrinter(BIRDPrinter):
"Print BIRD's struct f_line_item"
typeCode = gdb.TYPE_CODE_STRUCT
typeTag = "f_line_item"
def to_string(self):
code = self.val['fi_code']
if str(code) == "FI_NOP":
return str(code) + ": " + str(self.val.cast(gdb.lookup_type("const char [%(siz)d]" % { "siz": self.val.type.sizeof })))
return "%(code)s:\t%(lineno) 6dL\t%(flags)2dF: .i_%(code)s = %(union)s" % {
"code": str(code),
"lineno": self.val['lineno'],
"flags": self.val['flags'],
"union": str(self.val['i_' + str(code)])
}
class BIRDFLinePrinter(BIRDPrinter):
"Print BIRD's struct f_line"
typeCode = gdb.TYPE_CODE_STRUCT
typeTag = "f_line"
def to_string(self):
cnt = self.val['len']
return ("FLine (%(cnt)d, args=%(args)d): " % { "cnt": cnt, "args" : self.val['args'] } + \
", ".join([
".items[%(n) 3d] = %(code)s" % {
"n": n,
"code": str(self.val['items'][n]['fi_code']),
} if n % 8 == 0 else str(self.val['items'][n]['fi_code']) for n in range(cnt)]))
class BIRDFExecStackPrinter(BIRDPrinter):
"Print BIRD's struct f_exec_stack"
typeCode = gdb.TYPE_CODE_STRUCT
typeTag = "f_exec_stack"
def to_string(self):
cnt = self.val['cnt']
return ("Exec stack (%(cnt)d):\n\t" % { "cnt": cnt }) + \
"\n\t".join([ ".item[%(n) 3d] = %(retflag)d V%(ventry) 3d P%(pos) 4d %(line)s" % {
"retflag": self.val['item'][n]['emask'],
"ventry": self.val['item'][n]['ventry'],
"pos": self.val['item'][n]['pos'],
"line": str(self.val['item'][n]['line'].dereference()),
"n": n
} for n in range(cnt-1, -1, -1) ])
def register_printers(objfile):
objfile.pretty_printers.append(BIRDFInstPrinter.lookup)
objfile.pretty_printers.append(BIRDFValPrinter.lookup)
objfile.pretty_printers.append(BIRDFValStackPrinter.lookup)
objfile.pretty_printers.append(BIRDFLineItemPrinter.lookup)
objfile.pretty_printers.append(BIRDFLinePrinter.lookup)
objfile.pretty_printers.append(BIRDFExecStackPrinter.lookup)
register_printers(gdb.current_objfile())
print("BIRD pretty printers loaded OK.")

View File

@ -2,6 +2,7 @@ src := commands.c util.c client.c
obj := $(src-o-files)
$(all-client)
$(conf-y-targets): $(s)cmds.Y
$(exedir)/birdc: $(o)birdc.o
$(exedir)/birdc: LIBS += $(CLIENT_LIBS)

View File

@ -10,12 +10,12 @@ BISON_DEBUG=-t
#FLEX_DEBUG=-d
endif
$(conf-y-targets): $(s)confbase.Y $(s)flowspec.Y
$(M4) $(M4FLAGS) -P $| $^ >$@
$(o)cf-parse.y: $(s)gen_parser.m4
$(o)keywords.h: $(s)gen_keywords.m4
$(o)commands.h: $(s)gen_commands.m4
$(o)cf-parse.y: | $(s)gen_parser.m4
$(o)keywords.h: | $(s)gen_keywords.m4
$(o)commands.h: | $(s)gen_commands.m4 $(srcdir)/client/cmds.m4
$(conf-y-targets): $(s)confbase.Y $(s)flowspec.Y
$(M4) $(M4FLAGS) -P $(if $(word 2,$(filter %.m4,$^)),$(error "Too many M4 scripts for one target"),$(filter %.m4,$^)) $(filter %.Y,$^) >$@
$(o)cf-parse.tab.h: $(o)cf-parse.tab.c

View File

@ -45,6 +45,7 @@
#include "nest/route.h"
#include "nest/protocol.h"
#include "filter/filter.h"
#include "filter/f-inst.h"
#include "conf/conf.h"
#include "conf/cf-parse.tab.h"
#include "lib/string.h"
@ -64,7 +65,7 @@ struct keyword {
#endif
static uint cf_hash(byte *c);
static uint cf_hash(const byte *c);
#define KW_KEY(n) n->name
#define KW_NEXT(n) n->next
@ -95,6 +96,15 @@ int (*cf_read_hook)(byte *buf, unsigned int max, int fd);
struct include_file_stack *ifs;
static struct include_file_stack *ifs_head;
#define QUOTED_BUFFER_SIZE 4096
static BUFFER_(char) quoted_buffer;
static char quoted_buffer_data[QUOTED_BUFFER_SIZE];
static inline void quoted_buffer_init(void) {
quoted_buffer.used = 0;
quoted_buffer.size = QUOTED_BUFFER_SIZE;
quoted_buffer.data = quoted_buffer_data;
}
#define MAX_INCLUDE_DEPTH 8
#define YY_INPUT(buf,result,max) result = cf_read_hook(buf, max, ifs->fd);
@ -105,6 +115,8 @@ static struct include_file_stack *ifs_head;
static void cf_include(char *arg, int alen);
static int check_eof(void);
static enum yytokentype cf_lex_symbol(const char *data);
%}
%option noyywrap
@ -112,24 +124,26 @@ static int check_eof(void);
%option nounput
%option noreject
%x COMMENT CCOMM CLI
%x COMMENT CCOMM CLI QUOTED APOSTROPHED INCLUDE
ALPHA [a-zA-Z_]
DIGIT [0-9]
XIGIT [0-9a-fA-F]
ALNUM [a-zA-Z_0-9]
WHITE [ \t]
include ^{WHITE}*include{WHITE}*\".*\"{WHITE}*;
%%
{include} {
char *start, *end;
^{WHITE}*include{WHITE}*\" {
if (!ifs->depth)
cf_error("Include not allowed in CLI");
start = strchr(yytext, '"');
start++;
BEGIN(INCLUDE);
}
<INCLUDE>[^"\n]+["]{WHITE}*; {
char *start, *end;
start = yytext;
end = strchr(start, '"');
*end = 0;
@ -138,15 +152,23 @@ include ^{WHITE}*include{WHITE}*\".*\"{WHITE}*;
cf_error("Include with empty argument");
cf_include(start, end-start);
BEGIN(INITIAL);
}
<INCLUDE>["] cf_error("Include with empty argument");
<INCLUDE>. cf_error("Unterminated include");
<INCLUDE>\n cf_error("Unterminated include");
<INCLUDE><<EOF>> cf_error("Unterminated include");
{DIGIT}+:{DIGIT}+ {
uint len1 UNUSED, len2;
u64 l;
char *e;
errno = 0;
l = strtoul(yytext, &e, 10);
l = bstrtoul10(yytext, &e);
if (e && (*e != ':') || (errno == ERANGE) || (l >> 32))
cf_error("ASN out of range");
@ -164,7 +186,7 @@ include ^{WHITE}*include{WHITE}*\".*\"{WHITE}*;
}
errno = 0;
l = strtoul(e+1, &e, 10);
l = bstrtoul10(e+1, &e);
if (e && *e || (errno == ERANGE) || (l >> len2))
cf_error("Number out of range");
cf_lval.i64 |= l;
@ -191,13 +213,13 @@ include ^{WHITE}*include{WHITE}*\".*\"{WHITE}*;
}
errno = 0;
l = strtoul(yytext+2, &e, 10);
l = bstrtoul10(yytext+2, &e);
if (e && (*e != ':') || (errno == ERANGE) || (l >> len1))
cf_error("ASN out of range");
cf_lval.i64 |= ((u64) l) << len2;
errno = 0;
l = strtoul(e+1, &e, 10);
l = bstrtoul10(e+1, &e);
if (e && *e || (errno == ERANGE) || (l >> len2))
cf_error("Number out of range");
cf_lval.i64 |= l;
@ -219,7 +241,7 @@ include ^{WHITE}*include{WHITE}*\".*\"{WHITE}*;
cf_lval.i64 |= ((u64) ip4_to_u32(ip4)) << 16;
errno = 0;
l = strtoul(e, &e, 10);
l = bstrtoul10(e, &e);
if (e && *e || (errno == ERANGE) || (l >> 16))
cf_error("Number out of range");
cf_lval.i64 |= l;
@ -243,7 +265,7 @@ include ^{WHITE}*include{WHITE}*\".*\"{WHITE}*;
char *e;
unsigned long int l;
errno = 0;
l = strtoul(yytext+2, &e, 16);
l = bstrtoul16(yytext+2, &e);
if (e && *e || errno == ERANGE || (unsigned long int)(unsigned int) l != l)
cf_error("Number out of range");
cf_lval.i = l;
@ -254,7 +276,7 @@ include ^{WHITE}*include{WHITE}*\".*\"{WHITE}*;
char *e;
unsigned long int l;
errno = 0;
l = strtoul(yytext, &e, 10);
l = bstrtoul10(yytext, &e);
if (e && *e || errno == ERANGE || (unsigned long int)(unsigned int) l != l)
cf_error("Number out of range");
cf_lval.i = l;
@ -266,26 +288,23 @@ else: {
return ELSECOL;
}
({ALPHA}{ALNUM}*|[']({ALNUM}|[-]|[\.]|[:])*[']) {
if(*yytext == '\'') {
yytext[yyleng-1] = 0;
yytext++;
}
['] {
BEGIN(APOSTROPHED);
quoted_buffer_init();
}
struct keyword *k = HASH_FIND(kw_hash, KW, yytext);
if (k)
{
if (k->value > 0)
return k->value;
else
{
cf_lval.i = -k->value;
return ENUM;
}
}
<APOSTROPHED>{ALNUM}|[-]|[.:] BUFFER_PUSH(quoted_buffer) = yytext[0];
<APOSTROPHED>\n cf_error("Unterminated symbol");
<APOSTROPHED><<EOF>> cf_error("Unterminated symbol");
<APOSTROPHED>['] {
BEGIN(INITIAL);
BUFFER_PUSH(quoted_buffer) = 0;
return cf_lex_symbol(quoted_buffer_data);
}
<APOSTROPHED>. cf_error("Invalid character in apostrophed symbol");
cf_lval.s = cf_get_symbol(yytext);
return SYM;
({ALPHA}{ALNUM}*) {
return cf_lex_symbol(yytext);
}
<CLI>(.|\n) {
@ -301,14 +320,21 @@ else: {
return yytext[0];
}
["][^"\n]*["] {
yytext[yyleng-1] = 0;
cf_lval.t = cfg_strdup(yytext+1);
yytext[yyleng-1] = '"';
["] {
BEGIN(QUOTED);
quoted_buffer_init();
}
<QUOTED>\n cf_error("Unterminated string");
<QUOTED><<EOF>> cf_error("Unterminated string");
<QUOTED>["] {
BEGIN(INITIAL);
BUFFER_PUSH(quoted_buffer) = 0;
cf_lval.t = cfg_strdup(quoted_buffer_data);
return TEXT;
}
["][^"\n]*\n cf_error("Unterminated string");
<QUOTED>. BUFFER_PUSH(quoted_buffer) = yytext[0];
<INITIAL,COMMENT><<EOF>> { if (check_eof()) return END; }
@ -349,7 +375,7 @@ else: {
%%
static uint
cf_hash(byte *c)
cf_hash(const byte *c)
{
uint h = 13 << 24;
@ -358,7 +384,6 @@ cf_hash(byte *c)
return h;
}
/*
* IFS stack - it contains structures needed for recursive processing
* of include in config files. On the top of the stack is a structure
@ -519,7 +544,7 @@ check_eof(void)
}
static struct symbol *
cf_new_symbol(byte *c)
cf_new_symbol(const byte *c)
{
struct symbol *s;
@ -527,11 +552,8 @@ cf_new_symbol(byte *c)
if (l > SYM_MAX_LEN)
cf_error("Symbol too long");
s = cfg_alloc(sizeof(struct symbol) + l);
s->scope = conf_this_scope;
s->class = SYM_VOID;
s->def = NULL;
s->aux = 0;
s = cfg_allocz(sizeof(struct symbol) + l + 1);
*s = (struct symbol) { .scope = conf_this_scope, .class = SYM_VOID, };
strcpy(s->name, c);
if (!new_config->sym_hash.data)
@ -539,6 +561,8 @@ cf_new_symbol(byte *c)
HASH_INSERT2(new_config->sym_hash, SYM, new_config->pool, s);
add_tail(&(new_config->symbols), &(s->n));
return s;
}
@ -554,7 +578,7 @@ cf_new_symbol(byte *c)
* signify no match.
*/
struct symbol *
cf_find_symbol(struct config *cfg, byte *c)
cf_find_symbol(const struct config *cfg, const byte *c)
{
struct symbol *s;
@ -562,6 +586,7 @@ cf_find_symbol(struct config *cfg, byte *c)
(s = HASH_FIND(cfg->sym_hash, SYM, c, 1)))
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)))
@ -580,11 +605,33 @@ cf_find_symbol(struct config *cfg, byte *c)
* existing symbol is found.
*/
struct symbol *
cf_get_symbol(byte *c)
cf_get_symbol(const byte *c)
{
return cf_find_symbol(new_config, c) ?: cf_new_symbol(c);
}
/**
* cf_localize_symbol - get the local instance of given symbol
* @sym: the symbol to localize
*
* This functions finds the symbol that is local to current scope
* for purposes of cf_define_symbol().
*/
struct symbol *
cf_localize_symbol(struct symbol *sym)
{
/* If the symbol type is void, it has been recently allocated just in this scope. */
if (!sym->class)
return sym;
/* If the scope is the current, it is already defined in this scope. */
if (sym->scope == conf_this_scope)
cf_error("Symbol already defined");
/* Not allocated here yet, doing it now. */
return cf_new_symbol(sym->name);
}
struct symbol *
cf_default_name(char *template, int *counter)
{
@ -604,33 +651,32 @@ cf_default_name(char *template, int *counter)
cf_error("Unable to generate default name");
}
/**
* cf_define_symbol - define meaning of a symbol
* @sym: symbol to be defined
* @type: symbol class to assign
* @def: class dependent data
*
* Defines new meaning of a symbol. If the symbol is an undefined
* one (%SYM_VOID), it's just re-defined to the new type. If it's defined
* in different scope, a new symbol in current scope is created and the
* meaning is assigned to it. If it's already defined in the current scope,
* an error is reported via cf_error().
*
* Result: Pointer to the newly defined symbol. If we are in the top-level
* scope, it's the same @sym as passed to the function.
*/
struct symbol *
cf_define_symbol(struct symbol *sym, int type, void *def)
static enum yytokentype
cf_lex_symbol(const char *data)
{
if (sym->class)
/* Have we defined such a symbol? */
struct symbol *sym = cf_get_symbol(data);
cf_lval.s = sym;
if (sym->class != SYM_VOID)
return CF_SYM_KNOWN;
/* Is it a keyword? */
struct keyword *k = HASH_FIND(kw_hash, KW, data);
if (k)
{
if (k->value > 0)
return k->value;
else
{
if (sym->scope == conf_this_scope)
cf_error("Symbol already defined");
sym = cf_new_symbol(sym->name);
cf_lval.i = -k->value;
return ENUM;
}
sym->class = type;
sym->def = def;
return sym;
}
/* OK, undefined symbol */
cf_lval.s = sym;
return CF_SYM_UNDEFINED;
}
static void
@ -697,6 +743,7 @@ cf_push_scope(struct symbol *sym)
conf_this_scope = s;
s->active = 1;
s->name = sym;
s->slots = 0;
}
/**
@ -711,6 +758,7 @@ cf_pop_scope(void)
{
conf_this_scope->active = 0;
conf_this_scope = conf_this_scope->next;
ASSERT(conf_this_scope);
}
@ -724,9 +772,6 @@ cf_pop_scope(void)
char *
cf_symbol_class_name(struct symbol *sym)
{
if (cf_symbol_is_constant(sym))
return "constant";
switch (sym->class)
{
case SYM_VOID:
@ -741,6 +786,12 @@ cf_symbol_class_name(struct symbol *sym)
return "filter";
case SYM_TABLE:
return "routing table";
case SYM_ATTRIBUTE:
return "custom attribute";
case SYM_CONSTANT_RANGE:
return "constant";
case SYM_VARIABLE_RANGE:
return "variable";
default:
return "unknown type";
}

View File

@ -98,6 +98,7 @@ config_alloc(const char *name)
memcpy(ndup, name, nlen);
init_list(&c->tests);
init_list(&c->symbols);
c->mrtdump_file = -1; /* Hack, this should be sysdep-specific */
c->pool = p;
c->mem = l;
@ -258,6 +259,8 @@ config_do_commit(struct config *c, int type)
if (old_config)
old_config->obstacle_count++;
DBG("filter_commit\n");
filter_commit(c, old_config);
DBG("sysdep_commit\n");
int force_restart = sysdep_commit(c, old_config);
DBG("global_commit\n");

View File

@ -24,6 +24,7 @@ struct config {
list tables; /* Configured routing tables (struct rtable_config) */
list logfiles; /* Configured log files (sysdep) */
list tests; /* Configured unit tests (f_bt_test_suite) */
list symbols; /* Configured symbols in config order */
int mrtdump_file; /* Configured MRTDump file (sysdep, fd in unix) */
char *syslog_name; /* Name used for syslog (NULL -> no syslog) */
@ -107,18 +108,29 @@ void cfg_copy_list(list *dest, list *src, unsigned node_size);
extern int (*cf_read_hook)(byte *buf, uint max, int fd);
struct symbol {
node n; /* In list of symbols in config */
struct symbol *next;
struct sym_scope *scope;
int class;
int aux;
void *aux2;
void *def;
char name[1];
int class; /* SYM_* */
uint flags; /* SYM_FLAG_* */
union {
struct proto_config *proto; /* For SYM_PROTO and SYM_TEMPLATE */
const struct f_line *function; /* For SYM_FUNCTION */
const struct filter *filter; /* For SYM_FILTER */
struct rtable_config *table; /* For SYM_TABLE */
struct f_dynamic_attr *attribute; /* For SYM_ATTRIBUTE */
struct f_val *val; /* For SYM_CONSTANT */
uint offset; /* For SYM_VARIABLE */
};
char name[0];
};
struct sym_scope {
struct sym_scope *next; /* Next on scope stack */
struct symbol *name; /* Name of this scope */
uint slots; /* Variable slots */
int active; /* Currently entered */
};
@ -138,8 +150,11 @@ struct sym_scope {
#define SYM_CONSTANT 0x200 /* 0x200-0x2ff are variable types */
#define SYM_CONSTANT_RANGE SYM_CONSTANT ... (SYM_CONSTANT | 0xff)
#define SYM_TYPE(s) (((struct f_val *) (s)->def)->type)
#define SYM_VAL(s) (((struct f_val *) (s)->def)->val)
#define SYM_TYPE(s) ((s)->val->type)
#define SYM_VAL(s) ((s)->val->val)
/* Symbol flags */
#define SYM_FLAG_SAME 0x1 /* For SYM_FUNCTION and SYM_FILTER */
struct include_file_stack {
void *buffer; /* Internal lexer state */
@ -162,19 +177,37 @@ int cf_lex(void);
void cf_lex_init(int is_cli, struct config *c);
void cf_lex_unwind(void);
struct symbol *cf_find_symbol(struct config *cfg, byte *c);
struct symbol *cf_find_symbol(const struct config *cfg, const byte *c);
struct symbol *cf_get_symbol(byte *c);
struct symbol *cf_get_symbol(const byte *c);
struct symbol *cf_default_name(char *template, int *counter);
struct symbol *cf_define_symbol(struct symbol *symbol, int type, void *def);
struct symbol *cf_localize_symbol(struct symbol *sym);
/**
* cf_define_symbol - define meaning of a symbol
* @sym: symbol to be defined
* @type: symbol class to assign
* @def: class dependent data
*
* Defines new meaning of a symbol. If the symbol is an undefined
* one (%SYM_VOID), it's just re-defined to the new type. If it's defined
* in different scope, a new symbol in current scope is created and the
* meaning is assigned to it. If it's already defined in the current scope,
* an error is reported via cf_error().
*
* Result: Pointer to the newly defined symbol. If we are in the top-level
* scope, it's the same @sym as passed to the function.
*/
#define cf_define_symbol(sym_, type_, var_, def_) ({ \
struct symbol *sym = cf_localize_symbol(sym_); \
sym->class = type_; \
sym->var_ = def_; \
sym; })
void cf_push_scope(struct symbol *);
void cf_pop_scope(void);
char *cf_symbol_class_name(struct symbol *sym);
static inline int cf_symbol_is_constant(struct symbol *sym)
{ return (sym->class & 0xff00) == SYM_CONSTANT; }
/* Parser */
extern char *cf_text;

View File

@ -33,6 +33,21 @@ check_u16(uint val)
cf_error("Value %u out of range (0-65535)", val);
}
#define cf_assert(cond, ...) do { if (!(cond)) cf_error(__VA_ARGS__); } while (0)
static inline void cf_assert_symbol(const struct symbol *sym, uint class) {
switch (class) {
case SYM_PROTO: cf_assert(sym->class == SYM_PROTO, "Protocol name required"); break;
case SYM_TEMPLATE: cf_assert(sym->class == SYM_TEMPLATE, "Protocol template name required"); break;
case SYM_FUNCTION: cf_assert(sym->class == SYM_FUNCTION, "Function name required"); break;
case SYM_FILTER: cf_assert(sym->class == SYM_FILTER, "Filter name required"); break;
case SYM_TABLE: cf_assert(sym->class == SYM_TABLE, "Table name required"); break;
case SYM_ATTRIBUTE: cf_assert(sym->class == SYM_ATTRIBUTE, "Custom attribute name required"); break;
case SYM_VARIABLE: cf_assert((sym->class & ~0xff) == SYM_VARIABLE, "Variable name required"); break;
case SYM_CONSTANT: cf_assert((sym->class & ~0xff) == SYM_CONSTANT, "Constant name required"); break;
default: bug("This shall not happen");
}
}
CF_DECLS
%union {
@ -49,13 +64,19 @@ CF_DECLS
struct rtable_config *r;
struct channel_config *cc;
struct f_inst *x;
struct {
struct f_inst *begin, *end;
} xp;
enum filter_return fret;
enum ec_subtype ecs;
struct f_dynamic_attr fda;
struct f_static_attr fsa;
struct filter *f;
struct f_lval flv;
struct f_line *fl;
const struct filter *f;
struct f_tree *e;
struct f_trie *trie;
struct f_val v;
struct f_path_mask *h;
struct password_item *p;
struct rt_show_data *ra;
struct sym_show_data *sd;
@ -78,7 +99,7 @@ CF_DECLS
%token <ip4> IP4
%token <ip6> IP6
%token <i64> VPN_RD
%token <s> SYM
%token <s> CF_SYM_KNOWN CF_SYM_UNDEFINED
%token <t> TEXT
%type <iface> ipa_scope
@ -88,8 +109,10 @@ CF_DECLS
%type <net> net_ip4_ net_ip6_ net_ip6 net_ip_ net_ip net_or_ipa
%type <net_ptr> net_ net_any net_vpn4_ net_vpn6_ net_vpn_ net_roa4_ net_roa6_ net_roa_ net_ip6_sadr_ net_mpls_
%type <mls> label_stack_start label_stack
%type <s> CF_SYM_VOID
%type <t> text opttext
%type <s> symbol
%nonassoc PREFIX_DUMMY
%left AND OR
@ -99,6 +122,8 @@ CF_DECLS
%left '!'
%nonassoc '.'
%start config
CF_KEYWORDS(DEFINE, ON, OFF, YES, NO, S, MS, US, PORT, VPN, MPLS, FROM)
CF_GRAMMAR
@ -122,29 +147,31 @@ conf: ';' ;
conf: definition ;
definition:
DEFINE SYM '=' term ';' {
DEFINE CF_SYM_VOID '=' term ';' {
struct f_val *val = cfg_alloc(sizeof(struct f_val));
*val = f_eval($4, cfg_mem);
if (val->type == T_RETURN) cf_error("Runtime error");
cf_define_symbol($2, SYM_CONSTANT | val->type, val);
if (f_eval(f_linearize($4), cfg_mem, val) > F_RETURN) cf_error("Runtime error");
cf_define_symbol($2, SYM_CONSTANT | val->type, val, val);
}
;
expr:
NUM
| '(' term ')' { $$ = f_eval_int($2); }
| SYM {
if ($1->class != (SYM_CONSTANT | T_INT)) cf_error("Number expected");
| '(' term ')' { $$ = f_eval_int(f_linearize($2)); }
| CF_SYM_KNOWN {
if ($1->class != (SYM_CONSTANT | T_INT)) cf_error("Number constant expected");
$$ = SYM_VAL($1).i; }
;
expr_us:
expr S { $$ = $1 S_; }
| expr MS { $$ = $1 MS_; }
| expr US { $$ = $1 US_; }
;
CF_SYM_VOID: CF_SYM_UNDEFINED ;
symbol: CF_SYM_VOID | CF_SYM_KNOWN ;
/* Switches */
bool:
@ -162,15 +189,15 @@ bool:
ipa:
IP4 { $$ = ipa_from_ip4($1); }
| IP6 { $$ = ipa_from_ip6($1); }
| SYM {
if ($1->class != (SYM_CONSTANT | T_IP)) cf_error("IP address expected");
| CF_SYM_KNOWN {
if ($1->class != (SYM_CONSTANT | T_IP)) cf_error("IP address constant expected");
$$ = SYM_VAL($1).ip;
}
;
ipa_scope:
/* empty */ { $$ = NULL; }
| '%' SYM { $$ = if_get_by_name($2->name); }
| '%' symbol { $$ = if_get_by_name($2->name); }
;
@ -277,27 +304,27 @@ net_:
net_ip6:
net_ip6_
| SYM {
| CF_SYM_KNOWN {
if (($1->class != (SYM_CONSTANT | T_NET)) || (SYM_VAL($1).net->type != NET_IP6))
cf_error("IPv6 network expected");
cf_error("IPv6 network constant expected");
$$ = * SYM_VAL($1).net;
}
;
net_ip:
net_ip_
| SYM {
| CF_SYM_KNOWN {
if (($1->class != (SYM_CONSTANT | T_NET)) || !net_is_ip(SYM_VAL($1).net))
cf_error("IP network expected");
cf_error("IP network constant expected");
$$ = * SYM_VAL($1).net;
}
;
net_any:
net_
| SYM {
| CF_SYM_KNOWN {
if ($1->class != (SYM_CONSTANT | T_NET))
cf_error("Network expected");
cf_error("Network constant expected");
$$ = (net_addr *) SYM_VAL($1).net; /* Avoid const warning */
}
;
@ -307,13 +334,13 @@ net_or_ipa:
| net_ip6_
| IP4 { net_fill_ip4(&($$), $1, IP4_MAX_PREFIX_LENGTH); }
| IP6 { net_fill_ip6(&($$), $1, IP6_MAX_PREFIX_LENGTH); }
| SYM {
| CF_SYM_KNOWN {
if ($1->class == (SYM_CONSTANT | T_IP))
net_fill_ip_host(&($$), SYM_VAL($1).ip);
else if (($1->class == (SYM_CONSTANT | T_NET)) && net_is_ip(SYM_VAL($1).net))
$$ = * SYM_VAL($1).net;
else
cf_error("IP address or network expected");
cf_error("IP address or network constant expected");
}
;
@ -344,8 +371,8 @@ time:
text:
TEXT
| SYM {
if ($1->class != (SYM_CONSTANT | T_STRING)) cf_error("String expected");
| CF_SYM_KNOWN {
if ($1->class != (SYM_CONSTANT | T_STRING)) cf_error("String constant expected");
$$ = SYM_VAL($1).s;
}
;

View File

@ -18,6 +18,12 @@ AC_ARG_ENABLE([debug],
[enable_debug=no]
)
AC_ARG_ENABLE([debug-generated],
[AS_HELP_STRING([--enable-debug-generated], [enable this to abstain from generating #line @<:@no@:>@])],
[],
[enable_debug_generated=no]
)
AC_ARG_ENABLE([memcheck],
[AS_HELP_STRING([--enable-memcheck], [check memory allocations when debugging @<:@yes@:>@])],
[],
@ -109,6 +115,11 @@ if test -z "$GCC" ; then
AC_MSG_ERROR([This program requires the GNU C Compiler.])
fi
BIRD_CHECK_THREAD_LOCAL
if test "$bird_cv_thread_local" = yes ; then
AC_DEFINE([HAVE_THREAD_LOCAL], [1], [Define to 1 if _Thread_local is available])
fi
if test "$enable_pthreads" != no ; then
BIRD_CHECK_PTHREADS
@ -141,6 +152,14 @@ fi
AC_MSG_CHECKING([CFLAGS])
AC_MSG_RESULT([$CFLAGS])
if test "$enable_debug" = no; then
BIRD_CHECK_LTO
fi
if test "$bird_cv_c_lto" = yes; then
CFLAGS="$CFLAGS -flto"
LDFLAGS="$LDFLAGS -flto"
fi
AC_PROG_CPP
AC_PROG_INSTALL
@ -156,7 +175,7 @@ test -z "$M4" && AC_MSG_ERROR([M4 is missing.])
AC_MSG_CHECKING([bison version])
BIRD_CHECK_BISON_VERSION(BISON_VERSION)
AC_MSG_RESULT([$BISON_VERSION])
if test "$bird_bison_synclines" = yes; then
if test "$bird_bison_synclines" = yes && test "$enable_debug_generated" = no; then
M4FLAGS="$M4FLAGS -s"
fi
@ -364,6 +383,9 @@ if test "$enable_debug" = yes ; then
AC_CHECK_LIB([efence], [malloc])
fi
fi
else
BIRD_CHECK_GCC_OPTION([bird_cv_c_option_wno_implicit_fallthrough], [-Wno-implicit-fallthrough])
BIRD_ADD_GCC_OPTION([bird_cv_c_option_wno_implicit_fallthrough], [-Wno-implicit-fallthrough])
fi
CLIENT=birdcl

View File

@ -1,8 +1,24 @@
src := filter.c f-util.c tree.c trie.c
src := filter.c data.c f-util.c tree.c trie.c inst-gen.c
obj := $(src-o-files)
$(all-daemon)
$(cf-local)
#M4FLAGS_FILTERS=$(filter-out -s,$(M4FLAGS))
M4FLAGS_FILTERS=$(M4FLAGS)
$(o)inst-gen.h: $(s)decl.m4 $(s)f-inst.c $(objdir)/.dir-stamp
$(M4) $(M4FLAGS_FILTERS) -DTARGET=H -P $^ >$@
$(o)inst-gen.c: $(s)decl.m4 $(s)f-inst.c $(objdir)/.dir-stamp
$(M4) $(M4FLAGS_FILTERS) -DTARGET=C -P $^ >$@
$(o)inst-interpret.c: $(s)decl.m4 $(s)f-inst.c $(objdir)/.dir-stamp
$(M4) $(M4FLAGS_FILTERS) -DTARGET=I -P $^ >$@
prepare: $(o)inst-interpret.c $(o)inst-gen.h
tests_src := tree_test.c filter_test.c trie_test.c
tests_targets := $(tests_targets) $(tests-target-files)
tests_objs := $(tests_objs) $(src-o-files)
$(call clean,inst-gen.h inst-gen.c inst-interpret.c)

File diff suppressed because it is too large Load Diff

537
filter/data.c Normal file
View File

@ -0,0 +1,537 @@
/*
* Filters: utility functions
*
* (c) 1998 Pavel Machek <pavel@ucw.cz>
* (c) 2019 Maria Matejka <mq@jmq.cz>
*
* Can be freely distributed and used under the terms of the GNU GPL.
*
*/
#include "nest/bird.h"
#include "lib/lists.h"
#include "lib/resource.h"
#include "lib/socket.h"
#include "lib/string.h"
#include "lib/unaligned.h"
#include "lib/net.h"
#include "lib/ip.h"
#include "nest/route.h"
#include "nest/protocol.h"
#include "nest/iface.h"
#include "nest/attrs.h"
#include "conf/conf.h"
#include "filter/filter.h"
#include "filter/f-inst.h"
#include "filter/data.h"
const struct f_val f_const_empty_path = {
.type = T_PATH,
.val.ad = &null_adata,
}, f_const_empty_clist = {
.type = T_CLIST,
.val.ad = &null_adata,
}, f_const_empty_eclist = {
.type = T_ECLIST,
.val.ad = &null_adata,
}, f_const_empty_lclist = {
.type = T_LCLIST,
.val.ad = &null_adata,
};
static struct adata *
adata_empty(struct linpool *pool, int l)
{
struct adata *res = lp_alloc(pool, sizeof(struct adata) + l);
res->length = l;
return res;
}
static void
pm_format(const struct f_path_mask *p, buffer *buf)
{
buffer_puts(buf, "[= ");
for (uint i=0; i<p->len; i++)
{
switch(p->item[i].kind)
{
case PM_ASN:
buffer_print(buf, "%u ", p->item[i].asn);
break;
case PM_QUESTION:
buffer_puts(buf, "? ");
break;
case PM_ASTERISK:
buffer_puts(buf, "* ");
break;
case PM_ASN_RANGE:
buffer_print(buf, "%u..%u ", p->item[i].from, p->item[i].to);
break;
case PM_ASN_EXPR:
ASSERT(0);
}
}
buffer_puts(buf, "=]");
}
static inline int
lcomm_cmp(lcomm v1, lcomm v2)
{
if (v1.asn != v2.asn)
return (v1.asn > v2.asn) ? 1 : -1;
if (v1.ldp1 != v2.ldp1)
return (v1.ldp1 > v2.ldp1) ? 1 : -1;
if (v1.ldp2 != v2.ldp2)
return (v1.ldp2 > v2.ldp2) ? 1 : -1;
return 0;
}
/**
* val_compare - compare two values
* @v1: first value
* @v2: second value
*
* Compares two values and returns -1, 0, 1 on <, =, > or F_CMP_ERROR on
* error. Tree module relies on this giving consistent results so
* that it can be used for building balanced trees.
*/
int
val_compare(const struct f_val *v1, const struct f_val *v2)
{
if (v1->type != v2->type) {
if (v1->type == T_VOID) /* Hack for else */
return -1;
if (v2->type == T_VOID)
return 1;
/* IP->Quad implicit conversion */
if ((v1->type == T_QUAD) && val_is_ip4(v2))
return uint_cmp(v1->val.i, ipa_to_u32(v2->val.ip));
if (val_is_ip4(v1) && (v2->type == T_QUAD))
return uint_cmp(ipa_to_u32(v1->val.ip), v2->val.i);
debug( "Types do not match in val_compare\n" );
return F_CMP_ERROR;
}
switch (v1->type) {
case T_VOID:
return 0;
case T_ENUM:
case T_INT:
case T_BOOL:
case T_PAIR:
case T_QUAD:
return uint_cmp(v1->val.i, v2->val.i);
case T_EC:
case T_RD:
return u64_cmp(v1->val.ec, v2->val.ec);
case T_LC:
return lcomm_cmp(v1->val.lc, v2->val.lc);
case T_IP:
return ipa_compare(v1->val.ip, v2->val.ip);
case T_NET:
return net_compare(v1->val.net, v2->val.net);
case T_STRING:
return strcmp(v1->val.s, v2->val.s);
default:
return F_CMP_ERROR;
}
}
static inline int
pmi_same(const struct f_path_mask_item *mi1, const struct f_path_mask_item *mi2)
{
if (mi1->kind != mi2->kind)
return 0;
switch (mi1->kind) {
case PM_ASN:
if (mi1->asn != mi2->asn)
return 0;
break;
case PM_ASN_EXPR:
if (!f_same(mi1->expr, mi2->expr))
return 0;
break;
case PM_ASN_RANGE:
if (mi1->from != mi2->from)
return 0;
if (mi1->to != mi2->to)
return 0;
break;
}
return 1;
}
static int
pm_same(const struct f_path_mask *m1, const struct f_path_mask *m2)
{
if (m1->len != m2->len)
for (uint i=0; i<m1->len; i++)
if (!pmi_same(&(m1->item[i]), &(m2->item[i])))
return 0;
return 1;
}
/**
* val_same - compare two values
* @v1: first value
* @v2: second value
*
* Compares two values and returns 1 if they are same and 0 if not.
* Comparison of values of different types is valid and returns 0.
*/
int
val_same(const struct f_val *v1, const struct f_val *v2)
{
int rc;
rc = val_compare(v1, v2);
if (rc != F_CMP_ERROR)
return !rc;
if (v1->type != v2->type)
return 0;
switch (v1->type) {
case T_PATH_MASK:
return pm_same(v1->val.path_mask, v2->val.path_mask);
case T_PATH_MASK_ITEM:
return pmi_same(&(v1->val.pmi), &(v2->val.pmi));
case T_PATH:
case T_CLIST:
case T_ECLIST:
case T_LCLIST:
return adata_same(v1->val.ad, v2->val.ad);
case T_SET:
return same_tree(v1->val.t, v2->val.t);
case T_PREFIX_SET:
return trie_same(v1->val.ti, v2->val.ti);
default:
bug("Invalid type in val_same(): %x", v1->type);
}
}
int
clist_set_type(const struct f_tree *set, struct f_val *v)
{
switch (set->from.type)
{
case T_PAIR:
v->type = T_PAIR;
return 1;
case T_QUAD:
v->type = T_QUAD;
return 1;
case T_IP:
if (val_is_ip4(&(set->from)) && val_is_ip4(&(set->to)))
{
v->type = T_QUAD;
return 1;
}
/* Fall through */
default:
v->type = T_VOID;
return 0;
}
}
static int
clist_match_set(const struct adata *clist, const struct f_tree *set)
{
if (!clist)
return 0;
struct f_val v;
if (!clist_set_type(set, &v))
return F_CMP_ERROR;
u32 *l = (u32 *) clist->data;
u32 *end = l + clist->length/4;
while (l < end) {
v.val.i = *l++;
if (find_tree(set, &v))
return 1;
}
return 0;
}
static int
eclist_match_set(const struct adata *list, const struct f_tree *set)
{
if (!list)
return 0;
if (!eclist_set_type(set))
return F_CMP_ERROR;
struct f_val v;
u32 *l = int_set_get_data(list);
int len = int_set_get_size(list);
int i;
v.type = T_EC;
for (i = 0; i < len; i += 2) {
v.val.ec = ec_get(l, i);
if (find_tree(set, &v))
return 1;
}
return 0;
}
static int
lclist_match_set(const struct adata *list, const struct f_tree *set)
{
if (!list)
return 0;
if (!lclist_set_type(set))
return F_CMP_ERROR;
struct f_val v;
u32 *l = int_set_get_data(list);
int len = int_set_get_size(list);
int i;
v.type = T_LC;
for (i = 0; i < len; i += 3) {
v.val.lc = lc_get(l, i);
if (find_tree(set, &v))
return 1;
}
return 0;
}
const struct adata *
clist_filter(struct linpool *pool, const struct adata *list, const struct f_val *set, int pos)
{
if (!list)
return NULL;
int tree = (set->type == T_SET); /* 1 -> set is T_SET, 0 -> set is T_CLIST */
struct f_val v;
if (tree)
clist_set_type(set->val.t, &v);
else
v.type = T_PAIR;
int len = int_set_get_size(list);
u32 *l = int_set_get_data(list);
u32 tmp[len];
u32 *k = tmp;
u32 *end = l + len;
while (l < end) {
v.val.i = *l++;
/* pos && member(val, set) || !pos && !member(val, set), member() depends on tree */
if ((tree ? !!find_tree(set->val.t, &v) : int_set_contains(set->val.ad, v.val.i)) == pos)
*k++ = v.val.i;
}
uint nl = (k - tmp) * sizeof(u32);
if (nl == list->length)
return list;
struct adata *res = adata_empty(pool, nl);
memcpy(res->data, tmp, nl);
return res;
}
const struct adata *
eclist_filter(struct linpool *pool, const struct adata *list, const struct f_val *set, int pos)
{
if (!list)
return NULL;
int tree = (set->type == T_SET); /* 1 -> set is T_SET, 0 -> set is T_CLIST */
struct f_val v;
int len = int_set_get_size(list);
u32 *l = int_set_get_data(list);
u32 tmp[len];
u32 *k = tmp;
int i;
v.type = T_EC;
for (i = 0; i < len; i += 2) {
v.val.ec = ec_get(l, i);
/* pos && member(val, set) || !pos && !member(val, set), member() depends on tree */
if ((tree ? !!find_tree(set->val.t, &v) : ec_set_contains(set->val.ad, v.val.ec)) == pos) {
*k++ = l[i];
*k++ = l[i+1];
}
}
uint nl = (k - tmp) * sizeof(u32);
if (nl == list->length)
return list;
struct adata *res = adata_empty(pool, nl);
memcpy(res->data, tmp, nl);
return res;
}
const struct adata *
lclist_filter(struct linpool *pool, const struct adata *list, const struct f_val *set, int pos)
{
if (!list)
return NULL;
int tree = (set->type == T_SET); /* 1 -> set is T_SET, 0 -> set is T_CLIST */
struct f_val v;
int len = int_set_get_size(list);
u32 *l = int_set_get_data(list);
u32 tmp[len];
u32 *k = tmp;
int i;
v.type = T_LC;
for (i = 0; i < len; i += 3) {
v.val.lc = lc_get(l, i);
/* pos && member(val, set) || !pos && !member(val, set), member() depends on tree */
if ((tree ? !!find_tree(set->val.t, &v) : lc_set_contains(set->val.ad, v.val.lc)) == pos)
k = lc_copy(k, l+i);
}
uint nl = (k - tmp) * sizeof(u32);
if (nl == list->length)
return list;
struct adata *res = adata_empty(pool, nl);
memcpy(res->data, tmp, nl);
return res;
}
/**
* val_in_range - implement |~| operator
* @v1: element
* @v2: set
*
* Checks if @v1 is element (|~| operator) of @v2.
*/
int
val_in_range(const struct f_val *v1, const struct f_val *v2)
{
if ((v1->type == T_PATH) && (v2->type == T_PATH_MASK))
return as_path_match(v1->val.ad, v2->val.path_mask);
if ((v1->type == T_INT) && (v2->type == T_PATH))
return as_path_contains(v2->val.ad, v1->val.i, 1);
if (((v1->type == T_PAIR) || (v1->type == T_QUAD)) && (v2->type == T_CLIST))
return int_set_contains(v2->val.ad, v1->val.i);
/* IP->Quad implicit conversion */
if (val_is_ip4(v1) && (v2->type == T_CLIST))
return int_set_contains(v2->val.ad, ipa_to_u32(v1->val.ip));
if ((v1->type == T_EC) && (v2->type == T_ECLIST))
return ec_set_contains(v2->val.ad, v1->val.ec);
if ((v1->type == T_LC) && (v2->type == T_LCLIST))
return lc_set_contains(v2->val.ad, v1->val.lc);
if ((v1->type == T_STRING) && (v2->type == T_STRING))
return patmatch(v2->val.s, v1->val.s);
if ((v1->type == T_IP) && (v2->type == T_NET))
return ipa_in_netX(v1->val.ip, v2->val.net);
if ((v1->type == T_NET) && (v2->type == T_NET))
return net_in_netX(v1->val.net, v2->val.net);
if ((v1->type == T_NET) && (v2->type == T_PREFIX_SET))
return trie_match_net(v2->val.ti, v1->val.net);
if (v2->type != T_SET)
return F_CMP_ERROR;
/* With integrated Quad<->IP implicit conversion */
if ((v1->type == v2->val.t->from.type) ||
((v1->type == T_QUAD) && val_is_ip4(&(v2->val.t->from)) && val_is_ip4(&(v2->val.t->to))))
return !!find_tree(v2->val.t, v1);
if (v1->type == T_CLIST)
return clist_match_set(v1->val.ad, v2->val.t);
if (v1->type == T_ECLIST)
return eclist_match_set(v1->val.ad, v2->val.t);
if (v1->type == T_LCLIST)
return lclist_match_set(v1->val.ad, v2->val.t);
if (v1->type == T_PATH)
return as_path_match_set(v1->val.ad, v2->val.t);
return F_CMP_ERROR;
}
/*
* val_format - format filter value
*/
void
val_format(const struct f_val *v, buffer *buf)
{
char buf2[1024];
switch (v->type)
{
case T_VOID: buffer_puts(buf, "(void)"); return;
case T_BOOL: buffer_puts(buf, v->val.i ? "TRUE" : "FALSE"); return;
case T_INT: buffer_print(buf, "%u", v->val.i); return;
case T_STRING: buffer_print(buf, "%s", v->val.s); return;
case T_IP: buffer_print(buf, "%I", v->val.ip); return;
case T_NET: buffer_print(buf, "%N", v->val.net); return;
case T_PAIR: buffer_print(buf, "(%u,%u)", v->val.i >> 16, v->val.i & 0xffff); return;
case T_QUAD: buffer_print(buf, "%R", v->val.i); return;
case T_EC: ec_format(buf2, v->val.ec); buffer_print(buf, "%s", buf2); return;
case T_LC: lc_format(buf2, v->val.lc); buffer_print(buf, "%s", buf2); return;
case T_RD: rd_format(v->val.ec, buf2, 1024); buffer_print(buf, "%s", buf2); return;
case T_PREFIX_SET: trie_format(v->val.ti, buf); return;
case T_SET: tree_format(v->val.t, buf); return;
case T_ENUM: buffer_print(buf, "(enum %x)%u", v->type, v->val.i); return;
case T_PATH: as_path_format(v->val.ad, buf2, 1000); buffer_print(buf, "(path %s)", buf2); return;
case T_CLIST: int_set_format(v->val.ad, 1, -1, buf2, 1000); buffer_print(buf, "(clist %s)", buf2); return;
case T_ECLIST: ec_set_format(v->val.ad, -1, buf2, 1000); buffer_print(buf, "(eclist %s)", buf2); return;
case T_LCLIST: lc_set_format(v->val.ad, -1, buf2, 1000); buffer_print(buf, "(lclist %s)", buf2); return;
case T_PATH_MASK: pm_format(v->val.path_mask, buf); return;
default: buffer_print(buf, "[unknown type %x]", v->type); return;
}
}
char *
val_format_str(struct linpool *lp, const struct f_val *v) {
buffer b;
LOG_BUFFER_INIT(b);
val_format(v, &b);
return lp_strdup(lp, b.start);
}
static char val_dump_buffer[1024];
const char *
val_dump(const struct f_val *v) {
static buffer b = {
.start = val_dump_buffer,
.end = val_dump_buffer + 1024,
};
b.pos = b.start;
val_format(v, &b);
return val_dump_buffer;
}

203
filter/data.h Normal file
View File

@ -0,0 +1,203 @@
/*
* BIRD Internet Routing Daemon -- Dynamic data structures
*
* (c) 1999 Pavel Machek <pavel@ucw.cz>
* (c) 2018--2019 Maria Matejka <mq@jmq.cz>
*
* Can be freely distributed and used under the terms of the GNU GPL.
*/
#ifndef _BIRD_FILTER_DATA_H_
#define _BIRD_FILTER_DATA_H_
#include "nest/bird.h"
/* Type numbers must be in 0..0xff range */
#define T_MASK 0xff
/* Internal types */
enum f_type {
/* Nothing. Simply nothing. */
T_VOID = 0,
/* User visible types, which fit in int */
T_INT = 0x10,
T_BOOL = 0x11,
T_PAIR = 0x12, /* Notice that pair is stored as integer: first << 16 | second */
T_QUAD = 0x13,
/* Put enumerational types in 0x30..0x3f range */
T_ENUM_LO = 0x30,
T_ENUM_HI = 0x3f,
T_ENUM_RTS = 0x30,
T_ENUM_BGP_ORIGIN = 0x31,
T_ENUM_SCOPE = 0x32,
T_ENUM_RTC = 0x33,
T_ENUM_RTD = 0x34,
T_ENUM_ROA = 0x35,
T_ENUM_NETTYPE = 0x36,
T_ENUM_RA_PREFERENCE = 0x37,
/* new enums go here */
T_ENUM_EMPTY = 0x3f, /* Special hack for atomic_aggr */
#define T_ENUM T_ENUM_LO ... T_ENUM_HI
/* Bigger ones */
T_IP = 0x20,
T_NET = 0x21,
T_STRING = 0x22,
T_PATH_MASK = 0x23, /* mask for BGP path */
T_PATH = 0x24, /* BGP path */
T_CLIST = 0x25, /* Community list */
T_EC = 0x26, /* Extended community value, u64 */
T_ECLIST = 0x27, /* Extended community list */
T_LC = 0x28, /* Large community value, lcomm */
T_LCLIST = 0x29, /* Large community list */
T_RD = 0x2a, /* Route distinguisher for VPN addresses */
T_PATH_MASK_ITEM = 0x2b, /* Path mask item for path mask constructors */
T_SET = 0x80,
T_PREFIX_SET = 0x81,
} PACKED;
/* Filter value; size of this affects filter memory consumption */
struct f_val {
enum f_type type; /* T_* */
union {
uint i;
u64 ec;
lcomm lc;
ip_addr ip;
const net_addr *net;
char *s;
const struct f_tree *t;
const struct f_trie *ti;
const struct adata *ad;
const struct f_path_mask *path_mask;
struct f_path_mask_item pmi;
} val;
};
/* Dynamic attribute definition (eattrs) */
struct f_dynamic_attr {
u8 type; /* EA type (EAF_*) */
u8 bit; /* For bitfield accessors */
enum f_type f_type; /* Filter type */
uint ea_code; /* EA code */
};
enum f_sa_code {
SA_FROM = 1,
SA_GW,
SA_NET,
SA_PROTO,
SA_SOURCE,
SA_SCOPE,
SA_DEST,
SA_IFNAME,
SA_IFINDEX,
} PACKED;
/* Static attribute definition (members of struct rta) */
struct f_static_attr {
enum f_type f_type; /* Filter type */
enum f_sa_code sa_code; /* Static attribute id */
int readonly:1; /* Don't allow writing */
};
/* Filter l-value type */
enum f_lval_type {
F_LVAL_VARIABLE,
F_LVAL_PREFERENCE,
F_LVAL_SA,
F_LVAL_EA,
};
/* Filter l-value */
struct f_lval {
enum f_lval_type type;
union {
struct symbol *sym;
struct f_dynamic_attr da;
struct f_static_attr sa;
};
};
/* IP prefix range structure */
struct f_prefix {
net_addr net; /* The matching prefix must match this net */
u8 lo, hi; /* And its length must fit between lo and hi */
};
struct f_tree {
struct f_tree *left, *right;
struct f_val from, to;
void *data;
};
struct f_trie_node
{
ip_addr addr, mask, accept;
uint plen;
struct f_trie_node *c[2];
};
struct f_trie
{
linpool *lp;
int zero;
uint node_size;
struct f_trie_node root[0]; /* Root trie node follows */
};
struct f_tree *f_new_tree(void);
struct f_tree *build_tree(struct f_tree *);
const struct f_tree *find_tree(const struct f_tree *t, const struct f_val *val);
int same_tree(const struct f_tree *t0, const struct f_tree *t2);
void tree_format(const struct f_tree *t, buffer *buf);
struct f_trie *f_new_trie(linpool *lp, uint node_size);
void *trie_add_prefix(struct f_trie *t, const net_addr *n, uint l, uint h);
int trie_match_net(const struct f_trie *t, const net_addr *n);
int trie_same(const struct f_trie *t1, const struct f_trie *t2);
void trie_format(const struct f_trie *t, buffer *buf);
#define F_CMP_ERROR 999
int val_same(const struct f_val *v1, const struct f_val *v2);
int val_compare(const struct f_val *v1, const struct f_val *v2);
void val_format(const struct f_val *v, buffer *buf);
char *val_format_str(struct linpool *lp, const struct f_val *v);
const char *val_dump(const struct f_val *v);
static inline int val_is_ip4(const struct f_val *v)
{ return (v->type == T_IP) && ipa_is_ip4(v->val.ip); }
int val_in_range(const struct f_val *v1, const struct f_val *v2);
int clist_set_type(const struct f_tree *set, struct f_val *v);
static inline int eclist_set_type(const struct f_tree *set)
{ return set->from.type == T_EC; }
static inline int lclist_set_type(const struct f_tree *set)
{ return set->from.type == T_LC; }
const struct adata *clist_filter(struct linpool *pool, const struct adata *list, const struct f_val *set, int pos);
const struct adata *eclist_filter(struct linpool *pool, const struct adata *list, const struct f_val *set, int pos);
const struct adata *lclist_filter(struct linpool *pool, const struct adata *list, const struct f_val *set, int pos);
/* Special undef value for paths and clists */
static inline int
undef_value(struct f_val v)
{
return ((v.type == T_PATH) || (v.type == T_CLIST) ||
(v.type == T_ECLIST) || (v.type == T_LCLIST)) &&
(v.val.ad == &null_adata);
}
extern const struct f_val f_const_empty_path, f_const_empty_clist, f_const_empty_eclist, f_const_empty_lclist;
enum filter_return f_eval(const struct f_line *expr, struct linpool *tmp_pool, struct f_val *pres);
#endif

583
filter/decl.m4 Normal file
View File

@ -0,0 +1,583 @@
m4_divert(-1)m4_dnl
#
# BIRD -- Construction of per-instruction structures
#
# (c) 2018 Maria Matejka <mq@jmq.cz>
#
# Can be freely distributed and used under the terms of the GNU GPL.
#
# THIS IS A M4 MACRO FILE GENERATING 3 FILES ALTOGETHER.
# KEEP YOUR HANDS OFF UNLESS YOU KNOW WHAT YOU'RE DOING.
# EDITING AND DEBUGGING THIS FILE MAY DAMAGE YOUR BRAIN SERIOUSLY.
#
# But you're welcome to read and edit and debug if you aren't scared.
#
# Uncomment the following line to get exhaustive debug output.
# m4_debugmode(aceflqtx)
#
# How it works:
# 1) Instruction to code conversion (uses diversions 100..199)
# 2) Code wrapping (uses diversions 1..99)
# 3) Final preparation (uses diversions 200..299)
# 4) Shipout
#
# See below for detailed description.
#
#
# 1) Instruction to code conversion
# The code provided in f-inst.c between consecutive INST() calls
# is interleaved for many different places. It is here processed
# and split into separate instances where split-by-instruction
# happens. These parts are stored in temporary diversions listed:
#
# 101 content of per-inst struct
# 102 constructor arguments
# 103 constructor body
# 104 dump line item content
# (there may be nothing in dump-line content and
# it must be handled specially in phase 2)
# 105 linearize body
# 106 comparator body
# 107 struct f_line_item content
# 108 interpreter body
#
# Here are macros to allow you to _divert to the right directions.
m4_define(FID_STRUCT_IN, `m4_divert(101)')
m4_define(FID_NEW_ARGS, `m4_divert(102)')
m4_define(FID_NEW_BODY, `m4_divert(103)')
m4_define(FID_DUMP_BODY, `m4_divert(104)m4_define([[FID_DUMP_BODY_EXISTS]])')
m4_define(FID_LINEARIZE_BODY, `m4_divert(105)')
m4_define(FID_SAME_BODY, `m4_divert(106)')
m4_define(FID_LINE_IN, `m4_divert(107)')
m4_define(FID_INTERPRET_BODY, `m4_divert(108)')
# Sometimes you want slightly different code versions in different
# outputs.
# Use FID_HIC(code for inst-gen.h, code for inst-gen.c, code for inst-interpret.c)
# and put it into [[ ]] quotes if it shall contain commas.
m4_define(FID_HIC, `m4_ifelse(TARGET, [[H]], [[$1]], TARGET, [[I]], [[$2]], TARGET, [[C]], [[$3]])')
# In interpreter code, this is quite common.
m4_define(FID_INTERPRET_EXEC, `FID_HIC(,[[FID_INTERPRET_BODY()]],[[m4_divert(-1)]])')
m4_define(FID_INTERPRET_NEW, `FID_HIC(,[[m4_divert(-1)]],[[FID_INTERPRET_BODY()]])')
# If the instruction is never converted to constant, the interpret
# code is not produced at all for constructor
m4_define(NEVER_CONSTANT, `m4_define([[INST_NEVER_CONSTANT]])')
m4_define(FID_IFCONST, `m4_ifdef([[INST_NEVER_CONSTANT]],[[$2]],[[$1]])')
# If the instruction has some attributes (here called members),
# these are typically carried with the instruction from constructor
# to interpreter. This yields a line of code everywhere on the path.
# FID_MEMBER is a macro to help with this task.
m4_define(FID_MEMBER, `m4_dnl
FID_LINE_IN()m4_dnl
$1 $2;
FID_STRUCT_IN()m4_dnl
$1 $2;
FID_NEW_ARGS()m4_dnl
, $1 $2
FID_NEW_BODY()m4_dnl
whati->$2 = $2;
FID_LINEARIZE_BODY()m4_dnl
item->$2 = whati->$2;
m4_ifelse($3,,,[[
FID_SAME_BODY()m4_dnl
if ($3) return 0;
]])
m4_ifelse($4,,,[[
FID_DUMP_BODY()m4_dnl
debug("%s" $4 "\n", INDENT, $5);
]])
FID_INTERPRET_EXEC()m4_dnl
const $1 $2 = whati->$2
FID_INTERPRET_BODY')
# Instruction arguments are needed only until linearization is done.
# This puts the arguments into the filter line to be executed before
# the instruction itself.
#
# To achieve this, ARG_ANY must be called before anything writes into
# the instruction line as it moves the instruction pointer forward.
m4_define(ARG_ANY, `
FID_STRUCT_IN()m4_dnl
struct f_inst * f$1;
FID_NEW_ARGS()m4_dnl
, struct f_inst * f$1
FID_NEW_BODY
whati->f$1 = f$1;
for (const struct f_inst *child = f$1; child; child = child->next) {
what->size += child->size;
FID_IFCONST([[
if (child->fi_code != FI_CONSTANT)
constargs = 0;
]])
}
FID_LINEARIZE_BODY
pos = linearize(dest, whati->f$1, pos);
FID_INTERPRET_BODY()')
# Some instructions accept variable number of arguments.
m4_define(VARARG, `
FID_NEW_ARGS()m4_dnl
, struct f_inst * fvar
FID_STRUCT_IN()m4_dnl
struct f_inst * fvar;
uint varcount;
FID_LINE_IN()m4_dnl
uint varcount;
FID_NEW_BODY()m4_dnl
whati->varcount = 0;
whati->fvar = fvar;
for (const struct f_inst *child = fvar; child; child = child->next, whati->varcount++) {
what->size += child->size;
FID_IFCONST([[
if (child->fi_code != FI_CONSTANT)
constargs = 0;
]])
}
FID_IFCONST([[
const struct f_inst **items = NULL;
if (constargs) {
items = alloca(whati->varcount * sizeof(struct f_inst *));
const struct f_inst *child = fvar;
for (uint i=0; child; i++)
child = (items[i] = child)->next;
}
]])
FID_LINEARIZE_BODY()m4_dnl
pos = linearize(dest, whati->fvar, pos);
item->varcount = whati->varcount;
FID_DUMP_BODY()m4_dnl
debug("%snumber of varargs %u\n", INDENT, item->varcount);
FID_SAME_BODY()m4_dnl
if (f1->varcount != f2->varcount) return 0;
FID_INTERPRET_BODY()
FID_HIC(,[[
if (fstk->vcnt < whati->varcount) runtime("Stack underflow");
fstk->vcnt -= whati->varcount;
]],)
')
# Some arguments need to check their type. After that, ARG_ANY is called.
m4_define(ARG, `ARG_ANY($1)
FID_INTERPRET_EXEC()m4_dnl
if (v$1.type != $2) runtime("Argument $1 of instruction %s must be of type $2, got 0x%02x", f_instruction_name(what->fi_code), v$1.type)m4_dnl
FID_INTERPRET_BODY()')
# Executing another filter line. This replaces the recursion
# that was needed in the former implementation.
m4_define(LINEX, `FID_INTERPRET_EXEC()LINEX_($1)FID_INTERPRET_NEW()return $1 FID_INTERPRET_BODY()')
m4_define(LINEX_, `do {
fstk->estk[fstk->ecnt].pos = 0;
fstk->estk[fstk->ecnt].line = $1;
fstk->estk[fstk->ecnt].ventry = fstk->vcnt;
fstk->estk[fstk->ecnt].vbase = fstk->estk[fstk->ecnt-1].vbase;
fstk->estk[fstk->ecnt].emask = 0;
fstk->ecnt++;
} while (0)')
m4_define(LINE, `
FID_LINE_IN()m4_dnl
const struct f_line * fl$1;
FID_STRUCT_IN()m4_dnl
struct f_inst * f$1;
FID_NEW_ARGS()m4_dnl
, struct f_inst * f$1
FID_NEW_BODY()m4_dnl
whati->f$1 = f$1;
FID_DUMP_BODY()m4_dnl
f_dump_line(item->fl$1, indent + 1);
FID_LINEARIZE_BODY()m4_dnl
item->fl$1 = f_linearize(whati->f$1);
FID_SAME_BODY()m4_dnl
if (!f_same(f1->fl$1, f2->fl$1)) return 0;
FID_INTERPRET_EXEC()m4_dnl
do { if (whati->fl$1) {
LINEX_(whati->fl$1);
} } while(0)
FID_INTERPRET_NEW()m4_dnl
return whati->f$1
FID_INTERPRET_BODY()')
# Some of the instructions have a result. These constructions
# state the result and put it to the right place.
m4_define(RESULT, `RESULT_VAL([[ (struct f_val) { .type = $1, .val.$2 = $3 } ]])')
m4_define(RESULT_VAL, `FID_HIC(, [[do { res = $1; fstk->vcnt++; } while (0)]],
[[return fi_constant(what, $1)]])')
m4_define(RESULT_VOID, `RESULT_VAL([[ (struct f_val) { .type = T_VOID } ]])')
# Some common filter instruction members
m4_define(SYMBOL, `FID_MEMBER(struct symbol *, sym, [[strcmp(f1->sym->name, f2->sym->name) || (f1->sym->class != f2->sym->class)]], "symbol %s", item->sym->name)')
m4_define(RTC, `FID_MEMBER(struct rtable_config *, rtc, [[strcmp(f1->rtc->name, f2->rtc->name)]], "route table %s", item->rtc->name)')
m4_define(STATIC_ATTR, `FID_MEMBER(struct f_static_attr, sa, f1->sa.sa_code != f2->sa.sa_code,,)')
m4_define(DYNAMIC_ATTR, `FID_MEMBER(struct f_dynamic_attr, da, f1->da.ea_code != f2->da.ea_code,,)')
m4_define(ACCESS_RTE, `FID_HIC(,[[do { if (!fs->rte) runtime("No route to access"); } while (0)]],NEVER_CONSTANT())')
# 2) Code wrapping
# The code produced in 1xx temporary diversions is a raw code without
# any auxiliary commands and syntactical structures around. When the
# instruction is done, INST_FLUSH is called. More precisely, it is called
# at the beginning of INST() call and at the end of file.
#
# INST_FLUSH picks all the temporary diversions, wraps their content
# into appropriate headers and structures and saves them into global
# diversions listed:
#
# 4 enum fi_code
# 5 enum fi_code to string
# 6 dump line item
# 7 dump line item callers
# 8 linearize
# 9 same (filter comparator)
# 1 union in struct f_inst
# 3 constructors + interpreter
#
# These global diversions contain blocks of code that can be directly
# put into the final file, yet it still can't be written out now as
# every instruction writes to all of these diversions.
# Code wrapping diversion names. Here we want an explicit newline
# after the C comment.
m4_define(FID_ZONE, `m4_divert($1) /* $2 for INST_NAME() */
')
m4_define(FID_INST, `FID_ZONE(1, Instruction structure for config)')
m4_define(FID_LINE, `FID_ZONE(2, Instruction structure for interpreter)')
m4_define(FID_NEW, `FID_ZONE(3, Constructor)')
m4_define(FID_ENUM, `FID_ZONE(4, Code enum)')
m4_define(FID_ENUM_STR, `FID_ZONE(5, Code enum to string)')
m4_define(FID_DUMP, `FID_ZONE(6, Dump line)')
m4_define(FID_DUMP_CALLER, `FID_ZONE(7, Dump line caller)')
m4_define(FID_LINEARIZE, `FID_ZONE(8, Linearize)')
m4_define(FID_SAME, `FID_ZONE(9, Comparison)')
# This macro does all the code wrapping. See inline comments.
m4_define(INST_FLUSH, `m4_ifdef([[INST_NAME]], [[
FID_ENUM()m4_dnl Contents of enum fi_code { ... }
INST_NAME(),
FID_ENUM_STR()m4_dnl Contents of const char * indexed by enum fi_code
[INST_NAME()] = "INST_NAME()",
FID_INST()m4_dnl Anonymous structure inside struct f_inst
struct {
m4_undivert(101)m4_dnl
} i_[[]]INST_NAME();
FID_LINE()m4_dnl Anonymous structure inside struct f_line_item
struct {
m4_undivert(107)m4_dnl
} i_[[]]INST_NAME();
FID_NEW()m4_dnl Constructor and interpreter code together
FID_HIC(
[[m4_dnl Public declaration of constructor in H file
struct f_inst *f_new_inst_]]INST_NAME()[[(enum f_instruction_code fi_code
m4_undivert(102)m4_dnl
);]],
[[m4_dnl The one case in The Big Switch inside interpreter
case INST_NAME():
#define whati (&(what->i_]]INST_NAME()[[))
m4_ifelse(m4_eval(INST_INVAL() > 0), 1, [[if (fstk->vcnt < INST_INVAL()) runtime("Stack underflow"); fstk->vcnt -= INST_INVAL(); ]])
m4_undivert(108)m4_dnl
#undef whati
break;
]],
[[m4_dnl Constructor itself
struct f_inst *f_new_inst_]]INST_NAME()[[(enum f_instruction_code fi_code
m4_undivert(102)m4_dnl
)
{
/* Allocate the structure */
struct f_inst *what = fi_new(fi_code);
FID_IFCONST([[uint constargs = 1;]])
/* Initialize all the members */
#define whati (&(what->i_]]INST_NAME()[[))
m4_undivert(103)m4_dnl
/* If not constant, return the instruction itself */
FID_IFCONST([[if (!constargs)]])
return what;
/* Try to pre-calculate the result */
FID_IFCONST([[m4_undivert(108)]])m4_dnl
#undef whati
}
]])
FID_DUMP_CALLER()m4_dnl Case in another big switch used in instruction dumping (debug)
case INST_NAME(): f_dump_line_item_]]INST_NAME()[[(item, indent + 1); break;
FID_DUMP()m4_dnl The dumper itself
m4_ifdef([[FID_DUMP_BODY_EXISTS]],
[[static inline void f_dump_line_item_]]INST_NAME()[[(const struct f_line_item *item_, const int indent)]],
[[static inline void f_dump_line_item_]]INST_NAME()[[(const struct f_line_item *item UNUSED, const int indent UNUSED)]])
m4_undefine([[FID_DUMP_BODY_EXISTS]])
{
#define item (&(item_->i_]]INST_NAME()[[))
m4_undivert(104)m4_dnl
#undef item
}
FID_LINEARIZE()m4_dnl The linearizer
case INST_NAME(): {
#define whati (&(what->i_]]INST_NAME()[[))
#define item (&(dest->items[pos].i_]]INST_NAME()[[))
m4_undivert(105)m4_dnl
#undef whati
#undef item
dest->items[pos].fi_code = what->fi_code;
dest->items[pos].lineno = what->lineno;
break;
}
FID_SAME()m4_dnl This code compares two f_line"s while reconfiguring
case INST_NAME():
#define f1 (&(f1_->i_]]INST_NAME()[[))
#define f2 (&(f2_->i_]]INST_NAME()[[))
m4_undivert(106)m4_dnl
#undef f1
#undef f2
break;
m4_divert(-1)FID_FLUSH(101,200)m4_dnl And finally this flushes all the unused diversions
]])')
m4_define(INST, `m4_dnl This macro is called on beginning of each instruction.
INST_FLUSH()m4_dnl First, old data is flushed
m4_define([[INST_NAME]], [[$1]])m4_dnl Then we store instruction name,
m4_define([[INST_INVAL]], [[$2]])m4_dnl instruction input value count
m4_undefine([[INST_NEVER_CONSTANT]])m4_dnl and reset NEVER_CONSTANT trigger.
FID_INTERPRET_BODY()m4_dnl By default, every code is interpreter code.
')
# 3) Final preparation
#
# Now we prepare all the code around the global diversions.
# It must be here, not in m4wrap, as we want M4 to mark the code
# by #line directives correctly, not to claim that every single line
# is at the beginning of the m4wrap directive.
#
# This part is split by the final file.
# H for inst-gen.h
# I for inst-interpret.c
# C for inst-gen.c
#
# So we in cycle:
# A. open a diversion
# B. send there some code
# C. close that diversion
# D. flush a global diversion
# E. open another diversion and goto B.
#
# Final diversions
# 200+ completed text before it is flushed to output
# This is a list of output diversions
m4_define(FID_WR_PUT_LIST)
# This macro does the steps C to E, see before.
m4_define(FID_WR_PUT_ALSO, `m4_define([[FID_WR_PUT_LIST]],FID_WR_PUT_LIST()[[FID_WR_DPUT(]]FID_WR_DIDX[[)FID_WR_DPUT(]]$1[[)]])m4_define([[FID_WR_DIDX]],m4_eval(FID_WR_DIDX+1))m4_divert(FID_WR_DIDX)')
# These macros do the splitting between H/I/C
m4_define(FID_WR_DIRECT, `m4_ifelse(TARGET,[[$1]],[[FID_WR_INIT()]],[[FID_WR_STOP()]])')
m4_define(FID_WR_INIT, `m4_define([[FID_WR_DIDX]],200)m4_define([[FID_WR_PUT]],[[FID_WR_PUT_ALSO($]][[@)]])m4_divert(200)')
m4_define(FID_WR_STOP, `m4_define([[FID_WR_PUT]])m4_divert(-1)')
# Here is the direct code to be put into the output files
# together with the undiversions, being hidden under FID_WR_PUT()
m4_changequote([[,]])
FID_WR_DIRECT(I)
FID_WR_PUT(3)
FID_WR_DIRECT(C)
#if defined(__GNUC__) && __GNUC__ >= 6
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmisleading-indentation"
#endif
#include "nest/bird.h"
#include "filter/filter.h"
#include "filter/f-inst.h"
/* Instruction codes to string */
static const char * const f_instruction_name_str[] = {
FID_WR_PUT(5)
};
const char *
f_instruction_name(enum f_instruction_code fi)
{
if (fi < (sizeof(f_instruction_name_str) / sizeof(f_instruction_name_str[0])))
return f_instruction_name_str[fi];
else
bug("Got unknown instruction code: %d", fi);
}
static inline struct f_inst *
fi_new(enum f_instruction_code fi_code)
{
struct f_inst *what = cfg_allocz(sizeof(struct f_inst));
what->lineno = ifs->lino;
what->size = 1;
what->fi_code = fi_code;
return what;
}
static inline struct f_inst *
fi_constant(struct f_inst *what, struct f_val val)
{
what->fi_code = FI_CONSTANT;
what->i_FI_CONSTANT.val = val;
return what;
}
#define v1 whati->f1->i_FI_CONSTANT.val
#define v2 whati->f2->i_FI_CONSTANT.val
#define v3 whati->f3->i_FI_CONSTANT.val
#define vv(i) items[i]->i_FI_CONSTANT.val
#define runtime(fmt, ...) cf_error("filter preevaluation, line %d: " fmt, ifs->lino, ##__VA_ARGS__)
#define fpool cfg_mem
#define falloc(size) cfg_alloc(size)
/* Instruction constructors */
FID_WR_PUT(3)
#undef v1
#undef v2
#undef v3
#undef vv
/* Line dumpers */
#define INDENT (((const char *) f_dump_line_indent_str) + sizeof(f_dump_line_indent_str) - (indent) - 1)
static const char f_dump_line_indent_str[] = " ";
FID_WR_PUT(6)
void f_dump_line(const struct f_line *dest, uint indent)
{
if (!dest) {
debug("%sNo filter line (NULL)\n", INDENT);
return;
}
debug("%sFilter line %p (len=%u)\n", INDENT, dest, dest->len);
for (uint i=0; i<dest->len; i++) {
const struct f_line_item *item = &dest->items[i];
debug("%sInstruction %s at line %u\n", INDENT, f_instruction_name(item->fi_code), item->lineno);
switch (item->fi_code) {
FID_WR_PUT(7)
default: bug("Unknown instruction %x in f_dump_line", item->fi_code);
}
}
debug("%sFilter line %p dump done\n", INDENT, dest);
}
/* Linearize */
static uint
linearize(struct f_line *dest, const struct f_inst *what, uint pos)
{
for ( ; what; what = what->next) {
switch (what->fi_code) {
FID_WR_PUT(8)
}
pos++;
}
return pos;
}
struct f_line *
f_linearize_concat(const struct f_inst * const inst[], uint count)
{
uint len = 0;
for (uint i=0; i<count; i++)
for (const struct f_inst *what = inst[i]; what; what = what->next)
len += what->size;
struct f_line *out = cfg_allocz(sizeof(struct f_line) + sizeof(struct f_line_item)*len);
for (uint i=0; i<count; i++)
out->len = linearize(out, inst[i], out->len);
#if DEBUGGING
f_dump_line(out, 0);
#endif
return out;
}
/* Filter line comparison */
int
f_same(const struct f_line *fl1, const struct f_line *fl2)
{
if ((!fl1) && (!fl2))
return 1;
if ((!fl1) || (!fl2))
return 0;
if (fl1->len != fl2->len)
return 0;
for (uint i=0; i<fl1->len; i++) {
#define f1_ (&(fl1->items[i]))
#define f2_ (&(fl2->items[i]))
if (f1_->fi_code != f2_->fi_code)
return 0;
if (f1_->flags != f2_->flags)
return 0;
switch(f1_->fi_code) {
FID_WR_PUT(9)
}
}
#undef f1_
#undef f2_
return 1;
}
#if defined(__GNUC__) && __GNUC__ >= 6
#pragma GCC diagnostic pop
#endif
FID_WR_DIRECT(H)
/* Filter instruction codes */
enum f_instruction_code {
FID_WR_PUT(4)m4_dnl
} PACKED;
/* Filter instruction structure for config */
struct f_inst {
struct f_inst *next; /* Next instruction */
enum f_instruction_code fi_code; /* Instruction code */
int size; /* How many instructions are underneath */
int lineno; /* Line number */
union {
FID_WR_PUT(1)m4_dnl
};
};
/* Filter line item */
struct f_line_item {
enum f_instruction_code fi_code; /* What to do */
enum f_instruction_flags flags; /* Flags, instruction-specific */
uint lineno; /* Where */
union {
FID_WR_PUT(2)m4_dnl
};
};
/* Instruction constructors */
FID_WR_PUT(3)
m4_divert(-1)
# 4) Shipout
#
# Everything is prepared in FID_WR_PUT_LIST now. Let's go!
m4_changequote(`,')
# Flusher auxiliary macro
m4_define(FID_FLUSH, `m4_ifelse($1,$2,,[[m4_undivert($1)FID_FLUSH(m4_eval($1+1),$2)]])')
# Defining the macro used in FID_WR_PUT_LIST
m4_define(FID_WR_DPUT, `m4_undivert($1)')
# After the code is read and parsed, we:
m4_m4wrap(`INST_FLUSH()m4_divert(0)FID_WR_PUT_LIST()m4_divert(-1)FID_FLUSH(1,200)')
m4_changequote([[,]])
# And now M4 is going to parse f-inst.c, fill the diversions
# and after the file is done, the content of m4_m4wrap (see before)
# is executed.

1173
filter/f-inst.c Normal file

File diff suppressed because it is too large Load Diff

73
filter/f-inst.h Normal file
View File

@ -0,0 +1,73 @@
/*
* BIRD Internet Routing Daemon -- Filter instructions
*
* (c) 1999 Pavel Machek <pavel@ucw.cz>
* (c) 2018--2019 Maria Matejka <mq@jmq.cz>
*
* Can be freely distributed and used under the terms of the GNU GPL.
*
* Filter interpreter data structures and internal API.
* See filter/f-inst.c for documentation.
*/
#ifndef _BIRD_F_INST_H_
#define _BIRD_F_INST_H_
#include "nest/bird.h"
#include "conf/conf.h"
#include "filter/filter.h"
#include "filter/data.h"
/* Flags for instructions */
enum f_instruction_flags {
FIF_PRINTED = 1, /* FI_PRINT_AND_DIE: message put in buffer */
} PACKED;
/* Include generated filter instruction declarations */
#include "filter/inst-gen.h"
#define f_new_inst(...) MACRO_CONCAT_AFTER(f_new_inst_, MACRO_FIRST(__VA_ARGS__))(__VA_ARGS__)
/* Convert the instruction back to the enum name */
const char *f_instruction_name(enum f_instruction_code fi);
/* Filter structures for execution */
/* Line of instructions to be unconditionally executed one after another */
struct f_line {
uint len; /* Line length */
u8 args; /* Function: Args required */
u8 vars;
struct f_line_item items[0]; /* The items themselves */
};
/* Convert the f_inst infix tree to the f_line structures */
struct f_line *f_linearize_concat(const struct f_inst * const inst[], uint count);
static inline struct f_line *f_linearize(const struct f_inst *root)
{ return f_linearize_concat(&root, 1); }
void f_dump_line(const struct f_line *, uint indent);
struct filter *f_new_where(struct f_inst *);
static inline struct f_dynamic_attr f_new_dynamic_attr(u8 type, enum f_type f_type, uint code) /* Type as core knows it, type as filters know it, and code of dynamic attribute */
{ return (struct f_dynamic_attr) { .type = type, .f_type = f_type, .ea_code = code }; } /* f_type currently unused; will be handy for static type checking */
static inline struct f_dynamic_attr f_new_dynamic_attr_bit(u8 bit, enum f_type f_type, uint code) /* Type as core knows it, type as filters know it, and code of dynamic attribute */
{ return (struct f_dynamic_attr) { .type = EAF_TYPE_BITFIELD, .bit = bit, .f_type = f_type, .ea_code = code }; } /* f_type currently unused; will be handy for static type checking */
static inline struct f_static_attr f_new_static_attr(int f_type, int code, int readonly)
{ return (struct f_static_attr) { .f_type = f_type, .sa_code = code, .readonly = readonly }; }
struct f_inst *f_generate_complex(enum f_instruction_code fi_code, struct f_dynamic_attr da, struct f_inst *argument);
struct f_inst *f_generate_roa_check(struct rtable_config *table, struct f_inst *prefix, struct f_inst *asn);
/* Hook for call bt_assert() function in configuration */
extern void (*bt_assert_hook)(int result, const struct f_line_item *assert);
/* Bird Tests */
struct f_bt_test_suite {
node n; /* Node in config->tests */
const struct f_line *fn; /* Root of function */
const struct f_line *cmp; /* Compare to this function */
const char *fn_name; /* Name of test */
const char *dsc; /* Description */
int result; /* Desired result */
};
#endif

View File

@ -10,103 +10,56 @@
#include "nest/bird.h"
#include "conf/conf.h"
#include "filter/filter.h"
#include "filter/f-inst.h"
#include "lib/idm.h"
#include "nest/protocol.h"
#include "nest/route.h"
#define P(a,b) ((a<<8) | b)
struct f_inst *
f_new_inst(enum f_instruction_code fi_code)
{
struct f_inst * ret;
ret = cfg_allocz(sizeof(struct f_inst));
ret->fi_code = fi_code;
ret->lineno = ifs->lino;
return ret;
}
struct f_inst *
f_new_inst_da(enum f_instruction_code fi_code, struct f_dynamic_attr da)
{
struct f_inst *ret = f_new_inst(fi_code);
ret->aux = (da.f_type << 8) | da.type;
ret->a2.i = da.ea_code;
return ret;
}
struct f_inst *
f_new_inst_sa(enum f_instruction_code fi_code, struct f_static_attr sa)
{
struct f_inst *ret = f_new_inst(fi_code);
ret->aux = sa.f_type;
ret->a2.i = sa.sa_code;
ret->a1.i = sa.readonly;
return ret;
}
/*
* Generate set_dynamic( operation( get_dynamic(), argument ) )
*/
struct f_inst *
f_generate_complex(int operation, int operation_aux, struct f_dynamic_attr da, struct f_inst *argument)
{
struct f_inst *set_dyn = f_new_inst_da(FI_EA_SET, da),
*oper = f_new_inst(operation),
*get_dyn = f_new_inst_da(FI_EA_GET, da);
oper->aux = operation_aux;
oper->a1.p = get_dyn;
oper->a2.p = argument;
set_dyn->a1.p = oper;
return set_dyn;
}
struct f_inst *
f_generate_roa_check(struct rtable_config *table, struct f_inst *prefix, struct f_inst *asn)
{
struct f_inst_roa_check *ret = cfg_allocz(sizeof(struct f_inst_roa_check));
ret->i.fi_code = FI_ROA_CHECK;
ret->i.lineno = ifs->lino;
ret->i.arg1 = prefix;
ret->i.arg2 = asn;
/* prefix == NULL <-> asn == NULL */
if (table->addr_type != NET_ROA4 && table->addr_type != NET_ROA6)
cf_error("%s is not a ROA table", table->name);
ret->rtc = table;
return &ret->i;
}
static const char * const f_instruction_name_str[] = {
#define F(c,a,b) \
[c] = #c,
FI__LIST
#undef F
};
const char *
f_instruction_name(enum f_instruction_code fi)
{
if (fi < FI__MAX)
return f_instruction_name_str[fi];
else
bug("Got unknown instruction code: %d", fi);
}
char *
filter_name(struct filter *filter)
filter_name(const struct filter *filter)
{
if (!filter)
return "ACCEPT";
else if (filter == FILTER_REJECT)
return "REJECT";
else if (!filter->name)
else if (!filter->sym)
return "(unnamed)";
else
return filter->name;
return filter->sym->name;
}
struct filter *f_new_where(struct f_inst *where)
{
struct f_inst acc = {
.fi_code = FI_DIE,
.lineno = ifs->lino,
.size = 1,
.i_FI_DIE = { .fret = F_ACCEPT, },
};
struct f_inst rej = {
.fi_code = FI_DIE,
.lineno = ifs->lino,
.size = 1,
.i_FI_DIE = { .fret = F_REJECT, },
};
struct f_inst i = {
.fi_code = FI_CONDITION,
.lineno = ifs->lino,
.size = 3 + where->size,
.i_FI_CONDITION = {
.f1 = where,
.f2 = &acc,
.f3 = &rej,
},
};
struct filter *f = cfg_allocz(sizeof(struct filter));
f->root = f_linearize(&i);
return f;
}
#define CA_KEY(n) n->name, n->fda.type

File diff suppressed because it is too large Load Diff

View File

@ -2,6 +2,7 @@
* BIRD Internet Routing Daemon -- Filters
*
* (c) 1999 Pavel Machek <pavel@ucw.cz>
* (c) 2018--2019 Maria Matejka <mq@jmq.cz>
*
* Can be freely distributed and used under the terms of the GNU GPL.
*/
@ -11,279 +12,63 @@
#include "lib/resource.h"
#include "lib/ip.h"
#include "lib/macro.h"
#include "nest/route.h"
#include "nest/attrs.h"
/* Filter instruction types */
#define FI__TWOCHAR(a,b) ((a<<8) | b)
#define FI__LIST \
F(FI_ADD, 0, '+') \
F(FI_SUBTRACT, 0, '-') \
F(FI_MULTIPLY, 0, '*') \
F(FI_DIVIDE, 0, '/') \
F(FI_AND, 0, '&') \
F(FI_OR, 0, '|') \
F(FI_PAIR_CONSTRUCT, 'm', 'p') \
F(FI_EC_CONSTRUCT, 'm', 'c') \
F(FI_LC_CONSTRUCT, 'm', 'l') \
F(FI_PATHMASK_CONSTRUCT, 'm', 'P') \
F(FI_NEQ, '!', '=') \
F(FI_EQ, '=', '=') \
F(FI_LT, 0, '<') \
F(FI_LTE, '<', '=') \
F(FI_NOT, 0, '!') \
F(FI_MATCH, 0, '~') \
F(FI_NOT_MATCH, '!', '~') \
F(FI_DEFINED, 'd', 'e') \
F(FI_TYPE, 0, 'T') \
F(FI_IS_V4, 'I', 'i') \
F(FI_SET, 0, 's') \
F(FI_CONSTANT, 0, 'c') \
F(FI_VARIABLE, 0, 'V') \
F(FI_CONSTANT_INDIRECT, 0, 'C') \
F(FI_PRINT, 0, 'p') \
F(FI_CONDITION, 0, '?') \
F(FI_NOP, 0, '0') \
F(FI_PRINT_AND_DIE, 'p', ',') \
F(FI_RTA_GET, 0, 'a') \
F(FI_RTA_SET, 'a', 'S') \
F(FI_EA_GET, 'e', 'a') \
F(FI_EA_SET, 'e', 'S') \
F(FI_PREF_GET, 0, 'P') \
F(FI_PREF_SET, 'P', 'S') \
F(FI_LENGTH, 0, 'L') \
F(FI_ROA_MAXLEN, 'R', 'M') \
F(FI_ROA_ASN, 'R', 'A') \
F(FI_SADR_SRC, 'n', 's') \
F(FI_IP, 'c', 'p') \
F(FI_ROUTE_DISTINGUISHER, 'R', 'D') \
F(FI_AS_PATH_FIRST, 'a', 'f') \
F(FI_AS_PATH_LAST, 'a', 'l') \
F(FI_AS_PATH_LAST_NAG, 'a', 'L') \
F(FI_RETURN, 0, 'r') \
F(FI_CALL, 'c', 'a') \
F(FI_CLEAR_LOCAL_VARS, 'c', 'V') \
F(FI_SWITCH, 'S', 'W') \
F(FI_IP_MASK, 'i', 'M') \
F(FI_EMPTY, 0, 'E') \
F(FI_PATH_PREPEND, 'A', 'p') \
F(FI_CLIST_ADD_DEL, 'C', 'a') \
F(FI_ROA_CHECK, 'R', 'C') \
F(FI_FORMAT, 0, 'F') \
F(FI_ASSERT, 'a', 's')
enum f_instruction_code {
#define F(c,a,b) \
c,
FI__LIST
#undef F
FI__MAX,
} PACKED;
const char *f_instruction_name(enum f_instruction_code fi);
struct f_inst { /* Instruction */
struct f_inst *next; /* Structure is 16 bytes, anyway */
enum f_instruction_code fi_code;
u16 aux; /* Extension to instruction code, T_*, EA_*, EAF_* */
union {
uint i;
void *p;
} a1; /* The first argument */
union {
uint i;
void *p;
} a2; /* The second argument */
union {
int i;
void *p;
} a3; /* The third argument */
int lineno;
/* Possible return values of filter execution */
enum filter_return {
F_NOP = 0,
F_NONL,
F_RETURN,
F_ACCEPT, /* Need to preserve ordering: accepts < rejects! */
F_REJECT,
F_ERROR,
F_QUITBIRD,
};
#define arg1 a1.p
#define arg2 a2.p
static inline const char *filter_return_str(const enum filter_return fret) {
switch (fret) {
#define FRS(x) case x: return #x
FRS(F_NOP);
FRS(F_NONL);
FRS(F_RETURN);
FRS(F_ACCEPT);
FRS(F_REJECT);
FRS(F_ERROR);
FRS(F_QUITBIRD);
#undef FRS
default: bug("This shall not happen");
}
}
/* Not enough fields in f_inst for three args used by roa_check() */
struct f_inst_roa_check {
struct f_inst i;
struct rtable_config *rtc;
};
struct f_prefix {
net_addr net;
u8 lo, hi;
};
struct f_val {
int type; /* T_* */
union {
uint i;
u64 ec;
lcomm lc;
ip_addr ip;
const net_addr *net;
char *s;
struct f_tree *t;
struct f_trie *ti;
struct adata *ad;
struct f_path_mask *path_mask;
} val;
};
struct f_dynamic_attr {
int type;
int f_type;
int ea_code;
};
struct f_static_attr {
int f_type;
int sa_code;
int readonly;
};
struct f_val;
/* The filter encapsulating structure to be pointed-to from outside */
struct f_line;
struct filter {
char *name;
struct f_inst *root;
struct symbol *sym;
const struct f_line *root;
};
struct f_inst *f_new_inst(enum f_instruction_code fi_code);
struct f_inst *f_new_inst_da(enum f_instruction_code fi_code, struct f_dynamic_attr da);
struct f_inst *f_new_inst_sa(enum f_instruction_code fi_code, struct f_static_attr sa);
static inline struct f_dynamic_attr f_new_dynamic_attr(int type, int f_type, int code) /* Type as core knows it, type as filters know it, and code of dynamic attribute */
{ return (struct f_dynamic_attr) { .type = type, .f_type = f_type, .ea_code = code }; } /* f_type currently unused; will be handy for static type checking */
static inline struct f_static_attr f_new_static_attr(int f_type, int code, int readonly)
{ return (struct f_static_attr) { .f_type = f_type, .sa_code = code, .readonly = readonly }; }
struct f_tree *f_new_tree(void);
struct f_inst *f_generate_complex(int operation, int operation_aux, struct f_dynamic_attr da, struct f_inst *argument);
struct f_inst *f_generate_roa_check(struct rtable_config *table, struct f_inst *prefix, struct f_inst *asn);
struct f_tree *build_tree(struct f_tree *);
struct f_tree *find_tree(struct f_tree *t, struct f_val val);
int same_tree(struct f_tree *t1, struct f_tree *t2);
void tree_format(struct f_tree *t, buffer *buf);
struct f_trie *f_new_trie(linpool *lp, uint node_size);
void *trie_add_prefix(struct f_trie *t, const net_addr *n, uint l, uint h);
int trie_match_net(struct f_trie *t, const net_addr *n);
int trie_same(struct f_trie *t1, struct f_trie *t2);
void trie_format(struct f_trie *t, buffer *buf);
struct ea_list;
struct rte;
int f_run(struct filter *filter, struct rte **rte, struct linpool *tmp_pool, int flags);
struct f_val f_eval_rte(struct f_inst *expr, struct rte **rte, struct linpool *tmp_pool);
struct f_val f_eval(struct f_inst *expr, struct linpool *tmp_pool);
uint f_eval_int(struct f_inst *expr);
enum filter_return f_run(const struct filter *filter, struct rte **rte, struct linpool *tmp_pool, int flags);
enum filter_return f_eval_rte(const struct f_line *expr, struct rte **rte, struct linpool *tmp_pool);
uint f_eval_int(const struct f_line *expr);
enum filter_return f_eval_buf(const struct f_line *expr, struct linpool *tmp_pool, buffer *buf);
char *filter_name(struct filter *filter);
int filter_same(struct filter *new, struct filter *old);
const char *filter_name(const struct filter *filter);
int filter_same(const struct filter *new, const struct filter *old);
int f_same(const struct f_line *f1, const struct f_line *f2);
int i_same(struct f_inst *f1, struct f_inst *f2);
void filter_commit(struct config *new, struct config *old);
int val_compare(struct f_val v1, struct f_val v2);
int val_same(struct f_val v1, struct f_val v2);
void val_format(struct f_val v, buffer *buf);
#define F_NOP 0
#define F_NONL 1
#define F_ACCEPT 2 /* Need to preserve ordering: accepts < rejects! */
#define F_REJECT 3
#define F_ERROR 4
#define F_QUITBIRD 5
void filters_dump_all(void);
#define FILTER_ACCEPT NULL
#define FILTER_REJECT ((void *) 1)
#define FILTER_UNDEF ((void *) 2) /* Used in BGP */
/* Type numbers must be in 0..0xff range */
#define T_MASK 0xff
/* Internal types */
/* Do not use type of zero, that way we'll see errors easier. */
#define T_VOID 1
/* User visible types, which fit in int */
#define T_INT 0x10
#define T_BOOL 0x11
#define T_PAIR 0x12 /* Notice that pair is stored as integer: first << 16 | second */
#define T_QUAD 0x13
/* Put enumerational types in 0x30..0x3f range */
#define T_ENUM_LO 0x30
#define T_ENUM_HI 0x3f
#define T_ENUM_RTS 0x30
#define T_ENUM_BGP_ORIGIN 0x31
#define T_ENUM_SCOPE 0x32
#define T_ENUM_RTC 0x33
#define T_ENUM_RTD 0x34
#define T_ENUM_ROA 0x35
#define T_ENUM_NETTYPE 0x36
#define T_ENUM_RA_PREFERENCE 0x37
/* new enums go here */
#define T_ENUM_EMPTY 0x3f /* Special hack for atomic_aggr */
#define T_ENUM T_ENUM_LO ... T_ENUM_HI
/* Bigger ones */
#define T_IP 0x20
#define T_NET 0x21
#define T_STRING 0x22
#define T_PATH_MASK 0x23 /* mask for BGP path */
#define T_PATH 0x24 /* BGP path */
#define T_CLIST 0x25 /* Community list */
#define T_EC 0x26 /* Extended community value, u64 */
#define T_ECLIST 0x27 /* Extended community list */
#define T_LC 0x28 /* Large community value, lcomm */
#define T_LCLIST 0x29 /* Large community list */
#define T_RD 0x2a /* Route distinguisher for VPN addresses */
#define T_RETURN 0x40
#define T_SET 0x80
#define T_PREFIX_SET 0x81
#define SA_FROM 1
#define SA_GW 2
#define SA_NET 3
#define SA_PROTO 4
#define SA_SOURCE 5
#define SA_SCOPE 6
#define SA_DEST 7
#define SA_IFNAME 8
#define SA_IFINDEX 9
struct f_tree {
struct f_tree *left, *right;
struct f_val from, to;
void *data;
};
struct f_trie_node
{
ip_addr addr, mask, accept;
uint plen;
struct f_trie_node *c[2];
};
struct f_trie
{
linpool *lp;
int zero;
uint node_size;
struct f_trie_node root[0]; /* Root trie node follows */
};
#define NEW_F_VAL struct f_val * val; val = cfg_alloc(sizeof(struct f_val));
#define FILTER_REJECT ((struct filter *) 1)
#define FILTER_UNDEF ((struct filter *) 2) /* Used in BGP */
#define FF_SILENT 2 /* Silent filter execution */
@ -296,15 +81,4 @@ struct custom_attribute {
struct custom_attribute *ca_lookup(pool *p, const char *name, int ea_type);
/* Bird Tests */
struct f_bt_test_suite {
node n; /* Node in config->tests */
struct f_inst *fn; /* Root of function */
const char *fn_name; /* Name of test */
const char *dsc; /* Description */
};
/* Hook for call bt_assert() function in configuration */
extern void (*bt_assert_hook)(int result, struct f_inst *assert);
#endif

View File

@ -17,44 +17,53 @@
#include "test/bt-utils.h"
#include "filter/filter.h"
#include "filter/data.h"
#include "filter/f-inst.h"
#include "conf/conf.h"
#define BT_CONFIG_FILE "filter/test.conf"
static struct config *
parse_config_file(const void *filename_void)
struct parse_config_file_arg {
struct config **cp;
const char *filename;
};
static int
parse_config_file(const void *argv)
{
bt_bird_init();
size_t fn_size = strlen((const char *) filename_void) + 1;
const struct parse_config_file_arg *arg = argv;
size_t fn_size = strlen(arg->filename) + 1;
char *filename = alloca(fn_size);
memcpy(filename, filename_void, fn_size);
memcpy(filename, arg->filename, fn_size);
struct config *c = bt_config_file_parse(filename);
bt_bird_cleanup();
return c;
*(arg->cp) = bt_config_file_parse(filename);
return !!*(arg->cp);
}
static int
run_function(const void *parsed_fn_def)
run_function(const void *arg)
{
/* XXX: const -> non-const */
struct f_inst *f = (struct f_inst *) parsed_fn_def;
const struct f_bt_test_suite *t = arg;
if (t->cmp)
return t->result == f_same(t->fn, t->cmp);
if (!f_same(t->fn, t->fn)) {
bt_result = bt_suite_result = 0;
bt_log_suite_case_result(0, "The function doesn't compare to itself as the same");
return 0;
}
linpool *tmp = lp_new_default(&root_pool);
struct f_val res = f_eval(f, tmp);
enum filter_return fret = f_eval(t->fn, tmp, NULL);
rfree(tmp);
if (res.type == T_RETURN && res.val.i >= F_REJECT)
return 0;
return 1;
return (fret < F_REJECT);
}
static void
bt_assert_filter(int result, struct f_inst *assert)
bt_assert_filter(int result, const struct f_line_item *assert)
{
int bt_suit_case_result = 1;
if (!result)
@ -64,23 +73,30 @@ bt_assert_filter(int result, struct f_inst *assert)
bt_suit_case_result = 0;
}
bt_log_suite_case_result(bt_suit_case_result, "Assertion at line %d (%s)", assert->lineno, (char *) assert->a2.p);
bt_log_suite_case_result(bt_suit_case_result, "Assertion at line %d (%s)",
assert->lineno, assert->i_FI_ASSERT.s);
}
int
main(int argc, char *argv[])
{
bt_init(argc, argv);
bt_bird_init();
struct config *c = parse_config_file(BT_CONFIG_FILE);
bt_assert_hook = bt_assert_filter;
struct config *c = NULL;
struct parse_config_file_arg pcfa = { .cp = &c, .filename = BT_CONFIG_FILE };
bt_test_suite_base(parse_config_file, "conf", (const void *) &pcfa, 0, 0, "parse config file");
bt_test_suite_base(parse_config_file, "reconf", (const void *) &pcfa, 0, 0, "reconfigure with the same file");
bt_bird_cleanup();
if (c)
{
bt_assert_hook = bt_assert_filter;
struct f_bt_test_suite *t;
WALK_LIST(t, c->tests)
bt_test_suite_base(run_function, t->fn_name, t->fn, BT_FORKING, BT_TIMEOUT, "%s", t->dsc);
bt_test_suite_base(run_function, t->fn_name, t, BT_FORKING, BT_TIMEOUT, "%s", t->dsc);
}
return bt_exit_value();

View File

@ -0,0 +1,23 @@
router id 1.1.1.1;
protocol device {}
function a() {
return true;
}
function b() {
return a();
}
function c() {
return b();
}
filter d {
if c() then accept; else reject;
}
protocol static {
ipv4 { import filter d; };
route 10.0.0.0/24 unreachable;
}

View File

@ -0,0 +1,23 @@
router id 1.1.1.1;
protocol device {}
function a() {
return false;
}
function b() {
return a();
}
function c() {
return b();
}
filter d {
if c() then accept; else reject;
}
protocol static {
ipv4 { import filter d; };
route 10.0.0.0/24 unreachable;
}

View File

@ -7,8 +7,7 @@
router id 62.168.0.1;
/* We have to setup any protocol */
protocol static { ipv4; }
protocol device { }
@ -25,8 +24,19 @@ function onef(int a)
return 1;
}
function twof(int a)
{
return 2;
}
function oneg(int a)
{
return 1;
}
bt_test_same(onef, onef, 1);
bt_test_same(onef, oneg, 1);
bt_test_same(onef, twof, 0);
/*
* Testing boolean expressions
@ -62,7 +72,6 @@ bt_test_suite(t_bool, "Testing boolean expressions");
/*
* Testing integers
* ----------------
@ -126,6 +135,7 @@ int set is;
bt_assert(2 ~ [ 1, 2, 3 ]);
bt_assert(5 ~ [ 4 .. 7 ]);
bt_assert(1 !~ [ 2, 3, 4 ]);
bt_assert(999 !~ [ 666, 333 ]);
is = [ 2, 3, 4, 7..11 ];
bt_assert(10 ~ is);
@ -1107,6 +1117,11 @@ int i;
return 0;
}
function callmeagain(int a; int b; int c)
{
return a + b + c;
}
function fifteen()
{
return 15;
@ -1123,6 +1138,7 @@ function t_call_function()
bt_assert(callme(3, 2) = 6);
bt_assert(callme(4, 4) = 16);
bt_assert(callme(7, 2) = 14);
bt_assert(callmeagain(1, 2, 3) = 6);
}
bt_test_suite(t_call_function, "Testing calling functions");
@ -1336,7 +1352,7 @@ bt_test_suite(t_mixed_prefix, "Testing mixed net types");
filter vpn_filter
{
bt_assert(format(net) = "0:1:2 10.1.10.0/24");
bt_assert(format(net) = "1:2 10.1.10.0/24");
bt_assert(net.type = NET_VPN4);
bt_assert(net.type != NET_IP4);
bt_assert(net.type != NET_IP6);
@ -1347,6 +1363,13 @@ filter vpn_filter
NET_IP6: print "IPV6";
}
bt_check_assign(from, 10.20.30.40);
bt_check_assign(gw, 55.55.55.44);
bgp_community.add((3,5));
bgp_ext_community.add((ro, 135, 999));
bgp_large_community.add((6464156, 89646354, 8675643));
accept;
}
@ -1358,3 +1381,9 @@ protocol static
vpn4 { table v4; import filter vpn_filter; };
route 0:1:2 10.1.10.0/24 unreachable;
}
protocol static
{
ipv6 { import where false; };
route fd01::/48 unreachable;
}

View File

@ -10,6 +10,7 @@
#include "nest/bird.h"
#include "conf/conf.h"
#include "filter/filter.h"
#include "filter/data.h"
/**
* find_tree
@ -23,15 +24,15 @@
* Both set matching and |switch() { }| construction is implemented using this function,
* thus both are as fast as they can be.
*/
struct f_tree *
find_tree(struct f_tree *t, struct f_val val)
const struct f_tree *
find_tree(const struct f_tree *t, const struct f_val *val)
{
if (!t)
return NULL;
if ((val_compare(t->from, val) != 1) &&
(val_compare(t->to, val) != -1))
if ((val_compare(&(t->from), val) != 1) &&
(val_compare(&(t->to), val) != -1))
return t;
if (val_compare(t->from, val) == -1)
if (val_compare(&(t->from), val) == -1)
return find_tree(t->right, val);
else
return find_tree(t->left, val);
@ -56,7 +57,7 @@ build_tree_rec(struct f_tree **buf, int l, int h)
static int
tree_compare(const void *p1, const void *p2)
{
return val_compare((* (struct f_tree **) p1)->from, (* (struct f_tree **) p2)->from);
return val_compare(&((* (struct f_tree **) p1)->from), &((* (struct f_tree **) p2)->from));
}
/**
@ -119,39 +120,39 @@ f_new_tree(void)
* Compares two trees and returns 1 if they are same
*/
int
same_tree(struct f_tree *t1, struct f_tree *t2)
same_tree(const struct f_tree *t1, const struct f_tree *t2)
{
if ((!!t1) != (!!t2))
return 0;
if (!t1)
return 1;
if (val_compare(t1->from, t2->from))
if (val_compare(&(t1->from), &(t2->from)))
return 0;
if (val_compare(t1->to, t2->to))
if (val_compare(&(t1->to), &(t2->to)))
return 0;
if (!same_tree(t1->left, t2->left))
return 0;
if (!same_tree(t1->right, t2->right))
return 0;
if (!i_same(t1->data, t2->data))
if (!f_same(t1->data, t2->data))
return 0;
return 1;
}
static void
tree_node_format(struct f_tree *t, buffer *buf)
tree_node_format(const struct f_tree *t, buffer *buf)
{
if (t == NULL)
return;
tree_node_format(t->left, buf);
val_format(t->from, buf);
if (val_compare(t->from, t->to) != 0)
val_format(&(t->from), buf);
if (val_compare(&(t->from), &(t->to)) != 0)
{
buffer_puts(buf, "..");
val_format(t->to, buf);
val_format(&(t->to), buf);
}
buffer_puts(buf, ", ");
@ -159,7 +160,7 @@ tree_node_format(struct f_tree *t, buffer *buf)
}
void
tree_format(struct f_tree *t, buffer *buf)
tree_format(const struct f_tree *t, buffer *buf)
{
buffer_puts(buf, "[");

View File

@ -10,6 +10,7 @@
#include "test/bt-utils.h"
#include "filter/filter.h"
#include "filter/data.h"
#include "conf/conf.h"
#define MAX_TREE_HEIGHT 13
@ -226,8 +227,8 @@ t_find(void)
};
for(looking_up_value.val.i = 0; looking_up_value.val.i < nodes_count; looking_up_value.val.i++)
{
struct f_tree *found_tree = find_tree(tree, looking_up_value);
bt_assert((val_compare(looking_up_value, found_tree->from) == 0) && (val_compare(looking_up_value, found_tree->to) == 0));
const struct f_tree *found_tree = find_tree(tree, &looking_up_value);
bt_assert((val_compare(&looking_up_value, &(found_tree->from)) == 0) && (val_compare(&looking_up_value, &(found_tree->to)) == 0));
}
}
@ -278,11 +279,11 @@ t_find_ranges(void)
for(*i = 0; *i <= max_value; *i += (uint)bt_random()/nodes_count)
{
struct f_tree *found_tree = find_tree(tree, needle);
const struct f_tree *found_tree = find_tree(tree, &needle);
bt_debug("searching: %u \n", *i);
bt_assert(
(val_compare(needle, found_tree->from) == 0) || (val_compare(needle, found_tree->to) == 0) ||
((val_compare(needle, found_tree->from) == 1) && (val_compare(needle, found_tree->to) == -1))
(val_compare(&needle, &(found_tree->from)) == 0) || (val_compare(&needle, &(found_tree->to)) == 0) ||
((val_compare(&needle, &(found_tree->from)) == 1) && (val_compare(&needle, &(found_tree->to)) == -1))
);
}
}

View File

@ -73,6 +73,7 @@
#include "lib/string.h"
#include "conf/conf.h"
#include "filter/filter.h"
#include "filter/data.h"
/*
@ -220,7 +221,7 @@ trie_add_prefix(struct f_trie *t, const net_addr *net, uint l, uint h)
}
static int
trie_match_prefix(struct f_trie *t, ip_addr px, uint plen)
trie_match_prefix(const struct f_trie *t, ip_addr px, uint plen)
{
ip_addr pmask = ipa_mkmask(plen);
ip_addr paddr = ipa_and(px, pmask);
@ -229,7 +230,7 @@ trie_match_prefix(struct f_trie *t, ip_addr px, uint plen)
return t->zero;
int plentest = plen - 1;
struct f_trie_node *n = t->root;
const struct f_trie_node *n = t->root;
while(n)
{
@ -264,7 +265,7 @@ trie_match_prefix(struct f_trie *t, ip_addr px, uint plen)
* is such prefix pattern in the trie.
*/
int
trie_match_net(struct f_trie *t, const net_addr *n)
trie_match_net(const struct f_trie *t, const net_addr *n)
{
uint add = 0;
@ -279,7 +280,7 @@ trie_match_net(struct f_trie *t, const net_addr *n)
}
static int
trie_node_same(struct f_trie_node *t1, struct f_trie_node *t2)
trie_node_same(const struct f_trie_node *t1, const struct f_trie_node *t2)
{
if ((t1 == NULL) && (t2 == NULL))
return 1;
@ -303,13 +304,13 @@ trie_node_same(struct f_trie_node *t1, struct f_trie_node *t2)
* Compares two tries and returns 1 if they are same
*/
int
trie_same(struct f_trie *t1, struct f_trie *t2)
trie_same(const struct f_trie *t1, const struct f_trie *t2)
{
return (t1->zero == t2->zero) && trie_node_same(t1->root, t2->root);
}
static void
trie_node_format(struct f_trie_node *t, buffer *buf)
trie_node_format(const struct f_trie_node *t, buffer *buf)
{
if (t == NULL)
return;
@ -329,7 +330,7 @@ trie_node_format(struct f_trie_node *t, buffer *buf)
* Prints the trie to the supplied buffer.
*/
void
trie_format(struct f_trie *t, buffer *buf)
trie_format(const struct f_trie *t, buffer *buf)
{
buffer_puts(buf, "[");

View File

@ -10,6 +10,7 @@
#include "test/bt-utils.h"
#include "filter/filter.h"
#include "filter/data.h"
#include "conf/conf.h"
#define TESTS_NUM 10

View File

@ -1,4 +1,4 @@
src := bitops.c checksum.c event.c flowspec.c idm.c ip.c lists.c mac.c md5.c mempool.c net.c patmatch.c printf.c resource.c sha1.c sha256.c sha512.c slab.c slists.c tbf.c timer.c xmalloc.c
src := bitops.c checksum.c event.c flowspec.c idm.c ip.c lists.c mac.c md5.c mempool.c net.c patmatch.c printf.c resource.c sha1.c sha256.c sha512.c slab.c slists.c strtoul.c tbf.c timer.c xmalloc.c
obj := $(src-o-files)
$(all-daemon)

View File

@ -73,6 +73,10 @@ static inline int u64_cmp(u64 i1, u64 i2)
#define UNUSED __attribute__((unused))
#define PACKED __attribute__((packed))
#ifndef HAVE_THREAD_LOCAL
#define _Thread_local
#endif
/* Microsecond time */
typedef s64 btime;
@ -164,6 +168,15 @@ void debug(const char *msg, ...); /* Printf to debug output */
#define ASSERT(x) do { if (!(x)) log(L_BUG "Assertion '%s' failed at %s:%d", #x, __FILE__, __LINE__); } while(0)
#endif
#ifdef DEBUGGING
asm(
".pushsection \".debug_gdb_scripts\", \"MS\",@progbits,1\n"
".byte 1\n" /* Python */
".asciz \"bird-gdb.py\"\n"
".popsection\n"
);
#endif
/* Pseudorandom numbers */
u32 random_u32(void);

View File

@ -245,7 +245,7 @@ ip4_pton(const char *a, ip4_addr *o)
char *d, *c = strchr(a, '.');
if (!c != !i)
return 0;
l = strtoul(a, &d, 10);
l = bstrtoul10(a, &d);
if (((d != c) && *d) || (l > 255))
return 0;
ia = (ia << 8) | l;

View File

@ -354,12 +354,12 @@ mpls_put(char *buf, int len, u32 *stack)
* Unaligned data access (in network order)
*/
static inline ip4_addr get_ip4(void *buf)
static inline ip4_addr get_ip4(const void *buf)
{
return _MI4(get_u32(buf));
}
static inline ip6_addr get_ip6(void *buf)
static inline ip6_addr get_ip6(const void *buf)
{
ip6_addr a;
memcpy(&a, buf, 16);

View File

@ -101,9 +101,13 @@ void buffer_realloc(void **buf, unsigned *size, unsigned need, unsigned item_siz
*/
#define DMALLOC_DISABLE
#include <dmalloc.h>
#define xmalloc(size) dmalloc_malloc(__FILE__, __LINE__, (size), DMALLOC_FUNC_MALLOC, 0, 1)
#define xrealloc(ptr, size) dmalloc_realloc(__FILE__, __LINE__, (ptr), (size), DMALLOC_FUNC_REALLOC, 1)
#define xfree(ptr) dmalloc_free(__FILE__, __LINE__, (ptr), DMALLOC_FUNC_FREE)
#define xmalloc(size) \
dmalloc_malloc(__FILE__, __LINE__, (size), DMALLOC_FUNC_MALLOC, 0, 1)
#define xrealloc(ptr, size) \
dmalloc_realloc(__FILE__, __LINE__, (ptr), (size), DMALLOC_FUNC_REALLOC, 1)
#define xfree(ptr) \
dmalloc_free(__FILE__, __LINE__, (ptr), DMALLOC_FUNC_FREE)
#else
/*
* Unfortunately, several libraries we might want to link to define

View File

@ -24,6 +24,9 @@ int buffer_vprint(buffer *buf, const char *fmt, va_list args);
int buffer_print(buffer *buf, const char *fmt, ...);
void buffer_puts(buffer *buf, const char *str);
u64 bstrtoul10(const char *str, char **end);
u64 bstrtoul16(const char *str, char **end);
int patmatch(const byte *pat, const byte *str);
static inline char *xbasename(const char *str)

61
lib/strtoul.c Normal file
View File

@ -0,0 +1,61 @@
/*
* BIRD Library -- Parse numbers
*
* (c) 2019 Maria Matejka <mq@jmq.cz>
*
* Can be freely distributed and used under the terms of the GNU GPL.
*/
#include "nest/bird.h"
#include "lib/string.h"
#include <errno.h>
#define ULI_MAX_DIV10 (UINT64_MAX / 10)
#define ULI_MAX_MOD10 (UINT64_MAX % 10)
u64
bstrtoul10(const char *str, char **end)
{
u64 out = 0;
for (*end = (char *) str; (**end >= '0') && (**end <= '9'); (*end)++) {
u64 digit = **end - '0';
if ((out > ULI_MAX_DIV10) ||
(out == ULI_MAX_DIV10) && (digit > ULI_MAX_MOD10)) {
errno = ERANGE;
return UINT64_MAX;
}
out *= 10;
out += (**end) - '0';
}
return out;
}
u64
bstrtoul16(const char *str, char **end)
{
u64 out = 0;
for (int i=0; i<=(64/4); i++) {
switch (str[i]) {
case '0' ... '9':
out *= 16;
out += str[i] - '0';
break;
case 'a' ... 'f':
out *= 16;
out += str[i] + 10 - 'a';
break;
case 'A' ... 'F':
out *= 16;
out += str[i] + 10 - 'A';
break;
default:
*end = (char *) &(str[i]);
return out;
}
}
errno = ERANGE;
return UINT64_MAX;
}

View File

@ -13,7 +13,7 @@
#include "lib/resource.h"
#include "lib/unaligned.h"
#include "lib/string.h"
#include "filter/filter.h"
#include "filter/data.h"
// static inline void put_as(byte *data, u32 as) { put_u32(data, as); }
// static inline u32 get_as(byte *data) { return get_u32(data); }
@ -77,10 +77,10 @@ bad:
}
int
as_path_16to32(byte *dst, byte *src, uint len)
as_path_16to32(byte *dst, const byte *src, uint len)
{
byte *dst0 = dst;
byte *end = src + len;
const byte *end = src + len;
uint i, n;
while (src < end)
@ -101,10 +101,10 @@ as_path_16to32(byte *dst, byte *src, uint len)
}
int
as_path_32to16(byte *dst, byte *src, uint len)
as_path_32to16(byte *dst, const byte *src, uint len)
{
byte *dst0 = dst;
byte *end = src + len;
const byte *end = src + len;
uint i, n;
while (src < end)
@ -271,13 +271,12 @@ as_path_to_old(struct linpool *pool, const struct adata *path)
/*
* Cut the path to the length @num, measured to the usual path metric. Note that
* AS_CONFED_* segments have zero length and must be added if they are on edge.
* In contrast to other as_path_* functions, @path is modified in place.
*/
void
as_path_cut(struct adata *path, uint num)
struct adata *
as_path_cut(struct linpool *pool, const struct adata *path, uint num)
{
byte *pos = path->data;
byte *end = pos + path->length;
const byte *pos = path->data;
const byte *end = pos + path->length;
while (pos < end)
{
@ -297,28 +296,39 @@ as_path_cut(struct adata *path, uint num)
/* Cannot add whole segment, so try partial one and finish */
if (num < n)
{
const byte *nend = pos;
if (num)
nend += 2 + BS * num;
struct adata *res = lp_alloc_adata(pool, path->length);
res->length = nend - (const byte *) path->data;
memcpy(res->data, path->data, res->length);
if (num)
{
pos[1] = num;
pos += 2 + BS * num;
byte *dpos = ((byte *) res->data) + (pos - (const byte *) path->data);
dpos[1] = num;
}
break;
return res;
}
num -= n;
pos += 2 + BS * l;
}
path->length = pos - path->data;
struct adata *res = lp_alloc_adata(pool, path->length);
res->length = path->length;
memcpy(res->data, path->data, res->length);
return res;
}
/*
* Merge (concatenate) paths @p1 and @p2 and return the result.
* In contrast to other as_path_* functions, @p1 and @p2 may be reused.
*/
struct adata *
as_path_merge(struct linpool *pool, struct adata *p1, struct adata *p2)
const struct adata *
as_path_merge(struct linpool *pool, const struct adata *p1, const struct adata *p2)
{
if (p1->length == 0)
return p2;
@ -561,7 +571,7 @@ as_path_contains(const struct adata *path, u32 as, int min)
}
int
as_path_match_set(const struct adata *path, struct f_tree *set)
as_path_match_set(const struct adata *path, const struct f_tree *set)
{
const u8 *p = path->data;
const u8 *q = p+path->length;
@ -574,7 +584,7 @@ as_path_match_set(const struct adata *path, struct f_tree *set)
for (i=0; i<n; i++)
{
struct f_val v = {T_INT, .val.i = get_as(p)};
if (find_tree(set, v))
if (find_tree(set, &v))
return 1;
p += BS;
}
@ -583,8 +593,8 @@ as_path_match_set(const struct adata *path, struct f_tree *set)
return 0;
}
struct adata *
as_path_filter(struct linpool *pool, struct adata *path, struct f_tree *set, u32 key, int pos)
const struct adata *
as_path_filter(struct linpool *pool, const struct adata *path, const struct f_tree *set, u32 key, int pos)
{
if (!path)
return NULL;
@ -612,7 +622,10 @@ as_path_filter(struct linpool *pool, struct adata *path, struct f_tree *set, u32
int match;
if (set)
match = !!find_tree(set, (struct f_val){T_INT, .val.i = as});
{
struct f_val v = {T_INT, .val.i = as};
match = !!find_tree(set, &v);
}
else
match = (as == key);
@ -771,7 +784,7 @@ pm_mark(struct pm_pos *pos, int i, int plen, int *nl, int *nh)
* is marked.
*/
int
as_path_match(const struct adata *path, struct f_path_mask *mask)
as_path_match(const struct adata *path, const struct f_path_mask *mask)
{
struct pm_pos pos[2048 + 1];
int plen = parse_path(path, pos);
@ -788,12 +801,12 @@ as_path_match(const struct adata *path, struct f_path_mask *mask)
l = h = 0;
pos[0].mark = 1;
while (mask)
for (uint m=0; m < mask->len; m++)
{
/* We remove this mark to not step after pos[plen] */
pos[plen].mark = 0;
switch (mask->kind)
switch (mask->item[m].kind)
{
case PM_ASTERISK:
for (i = l; i <= plen; i++)
@ -802,13 +815,13 @@ as_path_match(const struct adata *path, struct f_path_mask *mask)
break;
case PM_ASN: /* Define single ASN as ASN..ASN - very narrow interval */
val2 = val = mask->val;
val2 = val = mask->item[m].asn;
goto step;
case PM_ASN_EXPR:
bug("Expressions should be evaluated on AS path mask construction.");
case PM_ASN_RANGE:
val = mask->val;
val2 = mask->val2;
val = mask->item[m].from;
val2 = mask->item[m].to;
goto step;
case PM_QUESTION:
step:
@ -817,7 +830,7 @@ as_path_match(const struct adata *path, struct f_path_mask *mask)
if (pos[i].mark)
{
pos[i].mark = 0;
if ((mask->kind == PM_QUESTION) || pm_match(pos + i, val, val2))
if ((mask->item[m].kind == PM_QUESTION) || pm_match(pos + i, val, val2))
pm_mark(pos, i, plen, &nl, &nh);
}
@ -828,8 +841,6 @@ as_path_match(const struct adata *path, struct f_path_mask *mask)
l = nl;
break;
}
mask = mask->next;
}
return pos[plen].mark;

View File

@ -34,26 +34,24 @@ t_as_path_match(void)
first_prepended = last_prepended = 0;
struct linpool *lp = lp_new_default(&root_pool);
struct f_path_mask mask[AS_PATH_LENGTH] = {};
int i;
for (i = 0; i < AS_PATH_LENGTH; i++)
struct f_path_mask *mask = alloca(sizeof(struct f_path_mask) + AS_PATH_LENGTH * sizeof(struct f_path_mask_item));
mask->len = AS_PATH_LENGTH;
for (int i = AS_PATH_LENGTH - 1; i >= 0; i--)
{
u32 val = bt_random();
as_path = as_path_prepend(lp, as_path, val);
bt_debug("Prepending ASN: %10u \n", val);
if (i == 0)
first_prepended = val;
if (i == AS_PATH_LENGTH-1)
last_prepended = val;
if (i == AS_PATH_LENGTH-1)
first_prepended = val;
mask[i].kind = PM_ASN;
mask[i].val = val;
if (i)
mask[i].next = &mask[i-1];
mask->item[i].kind = PM_ASN;
mask->item[i].asn = val;
}
bt_assert_msg(as_path_match(as_path, &mask[AS_PATH_LENGTH-1]), "Mask should match with AS path");
bt_assert_msg(as_path_match(as_path, mask), "Mask should match with AS path");
u32 asn;

View File

@ -34,7 +34,7 @@
* the buffer to indicate truncation.
*/
int
int_set_format(struct adata *set, int way, int from, byte *buf, uint size)
int_set_format(const struct adata *set, int way, int from, byte *buf, uint size)
{
u32 *z = (u32 *) set->data;
byte *end = buf + size - 24;
@ -69,18 +69,16 @@ int
ec_format(byte *buf, u64 ec)
{
u32 type, key, val;
char tbuf[16], *kind;
char tbuf[16];
const char *kind;
type = ec >> 48;
switch (type & 0xf0ff)
{
case EC_RT: kind = "rt"; break;
case EC_RO: kind = "ro"; break;
kind = ec_subtype_str(type & 0xf0ff);
default:
kind = tbuf;
bsprintf(kind, "unknown 0x%x", type);
}
if (!kind) {
bsprintf(tbuf, "unknown 0x%x", type);
kind = tbuf;
}
switch (ec >> 56)
{
@ -115,7 +113,7 @@ ec_format(byte *buf, u64 ec)
}
int
ec_set_format(struct adata *set, int from, byte *buf, uint size)
ec_set_format(const struct adata *set, int from, byte *buf, uint size)
{
u32 *z = int_set_get_data(set);
byte *end = buf + size - 64;
@ -150,7 +148,7 @@ lc_format(byte *buf, lcomm lc)
}
int
lc_set_format(struct adata *set, int from, byte *buf, uint bufsize)
lc_set_format(const struct adata *set, int from, byte *buf, uint bufsize)
{
u32 *d = (u32 *) set->data;
byte *end = buf + bufsize - 64;
@ -181,7 +179,7 @@ lc_set_format(struct adata *set, int from, byte *buf, uint bufsize)
}
int
int_set_contains(struct adata *list, u32 val)
int_set_contains(const struct adata *list, u32 val)
{
if (!list)
return 0;
@ -198,7 +196,7 @@ int_set_contains(struct adata *list, u32 val)
}
int
ec_set_contains(struct adata *list, u64 val)
ec_set_contains(const struct adata *list, u64 val)
{
if (!list)
return 0;
@ -217,7 +215,7 @@ ec_set_contains(struct adata *list, u64 val)
}
int
lc_set_contains(struct adata *list, lcomm val)
lc_set_contains(const struct adata *list, lcomm val)
{
if (!list)
return 0;
@ -233,8 +231,8 @@ lc_set_contains(struct adata *list, lcomm val)
return 0;
}
struct adata *
int_set_prepend(struct linpool *pool, struct adata *list, u32 val)
const struct adata *
int_set_prepend(struct linpool *pool, const struct adata *list, u32 val)
{
struct adata *res;
int len;
@ -254,8 +252,8 @@ int_set_prepend(struct linpool *pool, struct adata *list, u32 val)
return res;
}
struct adata *
int_set_add(struct linpool *pool, struct adata *list, u32 val)
const struct adata *
int_set_add(struct linpool *pool, const struct adata *list, u32 val)
{
struct adata *res;
int len;
@ -275,8 +273,8 @@ int_set_add(struct linpool *pool, struct adata *list, u32 val)
return res;
}
struct adata *
ec_set_add(struct linpool *pool, struct adata *list, u64 val)
const struct adata *
ec_set_add(struct linpool *pool, const struct adata *list, u64 val)
{
if (ec_set_contains(list, val))
return list;
@ -295,8 +293,8 @@ ec_set_add(struct linpool *pool, struct adata *list, u64 val)
return res;
}
struct adata *
lc_set_add(struct linpool *pool, struct adata *list, lcomm val)
const struct adata *
lc_set_add(struct linpool *pool, const struct adata *list, lcomm val)
{
if (lc_set_contains(list, val))
return list;
@ -313,8 +311,8 @@ lc_set_add(struct linpool *pool, struct adata *list, lcomm val)
return res;
}
struct adata *
int_set_del(struct linpool *pool, struct adata *list, u32 val)
const struct adata *
int_set_del(struct linpool *pool, const struct adata *list, u32 val)
{
if (!int_set_contains(list, val))
return list;
@ -335,8 +333,8 @@ int_set_del(struct linpool *pool, struct adata *list, u32 val)
return res;
}
struct adata *
ec_set_del(struct linpool *pool, struct adata *list, u64 val)
const struct adata *
ec_set_del(struct linpool *pool, const struct adata *list, u64 val)
{
if (!ec_set_contains(list, val))
return list;
@ -362,8 +360,8 @@ ec_set_del(struct linpool *pool, struct adata *list, u64 val)
return res;
}
struct adata *
lc_set_del(struct linpool *pool, struct adata *list, lcomm val)
const struct adata *
lc_set_del(struct linpool *pool, const struct adata *list, lcomm val)
{
if (!lc_set_contains(list, val))
return list;
@ -384,8 +382,8 @@ lc_set_del(struct linpool *pool, struct adata *list, lcomm val)
return res;
}
struct adata *
int_set_union(struct linpool *pool, struct adata *l1, struct adata *l2)
const struct adata *
int_set_union(struct linpool *pool, const struct adata *l1, const struct adata *l2)
{
if (!l1)
return l2;
@ -414,8 +412,8 @@ int_set_union(struct linpool *pool, struct adata *l1, struct adata *l2)
return res;
}
struct adata *
ec_set_union(struct linpool *pool, struct adata *l1, struct adata *l2)
const struct adata *
ec_set_union(struct linpool *pool, const struct adata *l1, const struct adata *l2)
{
if (!l1)
return l2;
@ -447,8 +445,8 @@ ec_set_union(struct linpool *pool, struct adata *l1, struct adata *l2)
return res;
}
struct adata *
lc_set_union(struct linpool *pool, struct adata *l1, struct adata *l2)
const struct adata *
lc_set_union(struct linpool *pool, const struct adata *l1, const struct adata *l2)
{
if (!l1)
return l2;
@ -479,7 +477,7 @@ lc_set_union(struct linpool *pool, struct adata *l1, struct adata *l2)
struct adata *
ec_set_del_nontrans(struct linpool *pool, struct adata *set)
ec_set_del_nontrans(struct linpool *pool, const struct adata *set)
{
adata *res = lp_alloc_adata(pool, set->length);
u32 *src = int_set_get_data(set);
@ -510,7 +508,7 @@ int_set_cmp(const void *X, const void *Y)
}
struct adata *
int_set_sort(struct linpool *pool, struct adata *src)
int_set_sort(struct linpool *pool, const struct adata *src)
{
struct adata *dst = lp_alloc_adata(pool, src->length);
memcpy(dst->data, src->data, src->length);
@ -528,7 +526,7 @@ ec_set_cmp(const void *X, const void *Y)
}
struct adata *
ec_set_sort(struct linpool *pool, struct adata *src)
ec_set_sort(struct linpool *pool, const struct adata *src)
{
struct adata *dst = lp_alloc_adata(pool, src->length);
memcpy(dst->data, src->data, src->length);
@ -558,7 +556,7 @@ lc_set_cmp(const void *X, const void *Y)
}
struct adata *
lc_set_sort(struct linpool *pool, struct adata *src)
lc_set_sort(struct linpool *pool, const struct adata *src)
{
struct adata *dst = lp_alloc_adata(pool, src->length);
memcpy(dst->data, src->data, src->length);

View File

@ -15,10 +15,10 @@
#include "lib/resource.h"
#define SET_SIZE 10
static struct adata *set_sequence; /* <0; SET_SIZE) */
static struct adata *set_sequence_same; /* <0; SET_SIZE) */
static struct adata *set_sequence_higher; /* <SET_SIZE; 2*SET_SIZE) */
static struct adata *set_random;
static const struct adata *set_sequence; /* <0; SET_SIZE) */
static const struct adata *set_sequence_same; /* <0; SET_SIZE) */
static const struct adata *set_sequence_higher; /* <SET_SIZE; 2*SET_SIZE) */
static const struct adata *set_random;
#define BUFFER_SIZE 1000
static byte buf[BUFFER_SIZE] = {};
@ -34,14 +34,14 @@ enum set_type
};
static void
generate_set_sequence(enum set_type type)
generate_set_sequence(enum set_type type, int len)
{
struct adata empty_as_path = {};
set_sequence = set_sequence_same = set_sequence_higher = set_random = &empty_as_path;
lp = lp_new_default(&root_pool);
int i;
for (i = 0; i < SET_SIZE; i++)
for (i = 0; i < len; i++)
{
if (type == SET_TYPE_INT)
{
@ -72,7 +72,7 @@ t_set_int_contains(void)
int i;
resource_init();
generate_set_sequence(SET_TYPE_INT);
generate_set_sequence(SET_TYPE_INT, SET_SIZE);
bt_assert(int_set_get_size(set_sequence) == SET_SIZE);
@ -93,9 +93,9 @@ static int
t_set_int_union(void)
{
resource_init();
generate_set_sequence(SET_TYPE_INT);
generate_set_sequence(SET_TYPE_INT, SET_SIZE);
struct adata *set_union;
const struct adata *set_union;
set_union = int_set_union(lp, set_sequence, set_sequence_same);
bt_assert(int_set_get_size(set_union) == SET_SIZE);
bt_assert(int_set_format(set_union, 0, 2, buf, BUFFER_SIZE) == 0);
@ -112,9 +112,8 @@ static int
t_set_int_format(void)
{
resource_init();
generate_set_sequence(SET_TYPE_INT);
generate_set_sequence(SET_TYPE_INT, SET_SIZE_FOR_FORMAT_OUTPUT);
set_sequence->length = 4 * SET_SIZE_FOR_FORMAT_OUTPUT; /* dirty */
bt_assert(int_set_format(set_sequence, 0, 0, buf, BUFFER_SIZE) == 0);
bt_assert(strcmp(buf, "0.0.0.0 0.0.0.1 0.0.0.2 0.0.0.3 0.0.0.4 0.0.0.5 0.0.0.6 0.0.0.7 0.0.0.8 0.0.0.9") == 0);
@ -134,9 +133,9 @@ static int
t_set_int_delete(void)
{
resource_init();
generate_set_sequence(SET_TYPE_INT);
generate_set_sequence(SET_TYPE_INT, SET_SIZE);
struct adata *deleting_sequence = set_sequence;
const struct adata *deleting_sequence = set_sequence;
u32 i;
for (i = 0; i < SET_SIZE; i++)
{
@ -162,7 +161,7 @@ t_set_ec_contains(void)
u32 i;
resource_init();
generate_set_sequence(SET_TYPE_EC);
generate_set_sequence(SET_TYPE_EC, SET_SIZE);
bt_assert(ec_set_get_size(set_sequence) == SET_SIZE);
@ -183,9 +182,9 @@ static int
t_set_ec_union(void)
{
resource_init();
generate_set_sequence(SET_TYPE_EC);
generate_set_sequence(SET_TYPE_EC, SET_SIZE);
struct adata *set_union;
const struct adata *set_union;
set_union = ec_set_union(lp, set_sequence, set_sequence_same);
bt_assert(ec_set_get_size(set_union) == SET_SIZE);
bt_assert(ec_set_format(set_union, 0, buf, BUFFER_SIZE) == 0);
@ -203,7 +202,7 @@ t_set_ec_format(void)
{
resource_init();
struct adata empty_as_path = {};
const struct adata empty_as_path = {};
set_sequence = set_sequence_same = set_sequence_higher = set_random = &empty_as_path;
lp = lp_new_default(&root_pool);
@ -224,9 +223,9 @@ static int
t_set_ec_delete(void)
{
resource_init();
generate_set_sequence(SET_TYPE_EC);
generate_set_sequence(SET_TYPE_EC, SET_SIZE);
struct adata *deleting_sequence = set_sequence;
const struct adata *deleting_sequence = set_sequence;
u32 i;
for (i = 0; i < SET_SIZE; i++)
{

View File

@ -31,15 +31,15 @@
struct f_tree;
int as_path_valid(byte *data, uint len, int bs, int confed, char *err, uint elen);
int as_path_16to32(byte *dst, byte *src, uint len);
int as_path_32to16(byte *dst, byte *src, uint len);
int as_path_16to32(byte *dst, const byte *src, uint len);
int as_path_32to16(byte *dst, const byte *src, uint len);
int as_path_contains_as4(const struct adata *path);
int as_path_contains_confed(const struct adata *path);
struct adata *as_path_strip_confed(struct linpool *pool, const struct adata *op);
struct adata *as_path_prepend2(struct linpool *pool, const struct adata *op, int seq, u32 as);
struct adata *as_path_to_old(struct linpool *pool, const struct adata *path);
void as_path_cut(struct adata *path, uint num);
struct adata *as_path_merge(struct linpool *pool, struct adata *p1, struct adata *p2);
struct adata *as_path_cut(struct linpool *pool, const struct adata *path, uint num);
const struct adata *as_path_merge(struct linpool *pool, const struct adata *p1, const struct adata *p2);
void as_path_format(const struct adata *path, byte *buf, uint size);
int as_path_getlen(const struct adata *path);
int as_path_getlen_int(const struct adata *path, int bs);
@ -48,8 +48,8 @@ int as_path_get_first_regular(const struct adata *path, u32 *last_as);
int as_path_get_last(const struct adata *path, u32 *last_as);
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, struct f_tree *set);
struct adata *as_path_filter(struct linpool *pool, struct adata *path, struct f_tree *set, u32 key, int pos);
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_tree *set, u32 key, int pos);
static inline struct adata *as_path_prepend(struct linpool *pool, const struct adata *path, u32 as)
{ return as_path_prepend2(pool, path, AS_PATH_SEQUENCE, as); }
@ -61,20 +61,30 @@ static inline struct adata *as_path_prepend(struct linpool *pool, const struct a
#define PM_ASN_EXPR 3
#define PM_ASN_RANGE 4
struct f_path_mask {
struct f_path_mask *next;
struct f_path_mask_item {
union {
u32 asn; /* PM_ASN */
struct f_line *expr; /* PM_ASN_EXPR */
struct { /* PM_ASN_RANGE */
u32 from;
u32 to;
};
};
int kind;
uintptr_t val;
uintptr_t val2;
};
int as_path_match(const struct adata *path, struct f_path_mask *mask);
struct f_path_mask {
uint len;
struct f_path_mask_item item[0];
};
int as_path_match(const struct adata *path, const struct f_path_mask *mask);
/* Counterparts to appropriate as_path_* functions */
static inline int
aggregator_16to32(byte *dst, byte *src)
aggregator_16to32(byte *dst, const byte *src)
{
put_u32(dst, get_u16(src));
memcpy(dst+4, src+2, 4);
@ -82,7 +92,7 @@ aggregator_16to32(byte *dst, byte *src)
}
static inline int
aggregator_32to16(byte *dst, byte *src)
aggregator_32to16(byte *dst, const byte *src)
{
put_u16(dst, get_u32(src));
memcpy(dst+2, src+4, 4);
@ -90,13 +100,13 @@ aggregator_32to16(byte *dst, byte *src)
}
static inline int
aggregator_contains_as4(struct adata *a)
aggregator_contains_as4(const struct adata *a)
{
return get_u32(a->data) > 0xFFFF;
}
static inline struct adata *
aggregator_to_old(struct linpool *pool, struct adata *a)
aggregator_to_old(struct linpool *pool, const struct adata *a)
{
struct adata *d = lp_alloc_adata(pool, 8);
put_u32(d->data, 0xFFFF);
@ -109,26 +119,35 @@ aggregator_to_old(struct linpool *pool, struct adata *a)
/* Extended Community subtypes (kinds) */
#define EC_RT 0x0002
#define EC_RO 0x0003
enum ec_subtype {
EC_RT = 0x0002,
EC_RO = 0x0003,
EC_GENERIC = 0xFFFF,
};
#define EC_GENERIC 0xFFFF
static inline const char *ec_subtype_str(const enum ec_subtype ecs) {
switch (ecs) {
case EC_RT: return "rt";
case EC_RO: return "ro";
default: return NULL;
}
}
/* Transitive bit (for first u32 half of EC) */
#define EC_TBIT 0x40000000
#define ECOMM_LENGTH 8
static inline int int_set_get_size(struct adata *list)
static inline int int_set_get_size(const struct adata *list)
{ return list->length / 4; }
static inline int ec_set_get_size(struct adata *list)
static inline int ec_set_get_size(const struct adata *list)
{ return list->length / 8; }
static inline int lc_set_get_size(struct adata *list)
static inline int lc_set_get_size(const struct adata *list)
{ return list->length / 12; }
static inline u32 *int_set_get_data(struct adata *list)
static inline u32 *int_set_get_data(const struct adata *list)
{ return (u32 *) list->data; }
static inline u32 ec_hi(u64 ec) { return ec >> 32; }
@ -137,16 +156,16 @@ static inline u64 ec_get(const u32 *l, int i)
{ return (((u64) l[i]) << 32) | l[i+1]; }
/* RFC 4360 3.1. Two-Octet AS Specific Extended Community */
static inline u64 ec_as2(u64 kind, u64 key, u64 val)
{ return ((kind | 0x0000) << 48) | (key << 32) | val; }
static inline u64 ec_as2(enum ec_subtype kind, u64 key, u64 val)
{ return (((u64) kind | 0x0000) << 48) | (key << 32) | val; }
/* RFC 5668 4-Octet AS Specific BGP Extended Community */
static inline u64 ec_as4(u64 kind, u64 key, u64 val)
{ return ((kind | 0x0200) << 48) | (key << 16) | val; }
static inline u64 ec_as4(enum ec_subtype kind, u64 key, u64 val)
{ return (((u64) kind | 0x0200) << 48) | (key << 16) | val; }
/* RFC 4360 3.2. IPv4 Address Specific Extended Community */
static inline u64 ec_ip4(u64 kind, u64 key, u64 val)
{ return ((kind | 0x0100) << 48) | (key << 16) | val; }
static inline u64 ec_ip4(enum ec_subtype kind, u64 key, u64 val)
{ return (((u64) kind | 0x0100) << 48) | (key << 16) | val; }
static inline u64 ec_generic(u64 key, u64 val)
{ return (key << 32) | val; }
@ -173,29 +192,29 @@ static inline u32 *lc_copy(u32 *dst, const u32 *src)
{ memcpy(dst, src, LCOMM_LENGTH); return dst + 3; }
int int_set_format(struct adata *set, int way, int from, byte *buf, uint size);
int int_set_format(const struct adata *set, int way, int from, byte *buf, uint size);
int ec_format(byte *buf, u64 ec);
int ec_set_format(struct adata *set, int from, byte *buf, uint size);
int ec_set_format(const struct adata *set, int from, byte *buf, uint size);
int lc_format(byte *buf, lcomm lc);
int lc_set_format(struct adata *set, int from, byte *buf, uint size);
int int_set_contains(struct adata *list, u32 val);
int ec_set_contains(struct adata *list, u64 val);
int lc_set_contains(struct adata *list, lcomm val);
struct adata *int_set_prepend(struct linpool *pool, struct adata *list, u32 val);
struct adata *int_set_add(struct linpool *pool, struct adata *list, u32 val);
struct adata *ec_set_add(struct linpool *pool, struct adata *list, u64 val);
struct adata *lc_set_add(struct linpool *pool, struct adata *list, lcomm val);
struct adata *int_set_del(struct linpool *pool, struct adata *list, u32 val);
struct adata *ec_set_del(struct linpool *pool, struct adata *list, u64 val);
struct adata *lc_set_del(struct linpool *pool, struct adata *list, lcomm val);
struct adata *int_set_union(struct linpool *pool, struct adata *l1, struct adata *l2);
struct adata *ec_set_union(struct linpool *pool, struct adata *l1, struct adata *l2);
struct adata *lc_set_union(struct linpool *pool, struct adata *l1, struct adata *l2);
int lc_set_format(const struct adata *set, int from, byte *buf, uint size);
int int_set_contains(const struct adata *list, u32 val);
int ec_set_contains(const struct adata *list, u64 val);
int lc_set_contains(const struct adata *list, lcomm val);
const struct adata *int_set_prepend(struct linpool *pool, const struct adata *list, u32 val);
const struct adata *int_set_add(struct linpool *pool, const struct adata *list, u32 val);
const struct adata *ec_set_add(struct linpool *pool, const struct adata *list, u64 val);
const struct adata *lc_set_add(struct linpool *pool, const struct adata *list, lcomm val);
const struct adata *int_set_del(struct linpool *pool, const struct adata *list, u32 val);
const struct adata *ec_set_del(struct linpool *pool, const struct adata *list, u64 val);
const struct adata *lc_set_del(struct linpool *pool, const struct adata *list, lcomm val);
const struct adata *int_set_union(struct linpool *pool, const struct adata *l1, const struct adata *l2);
const struct adata *ec_set_union(struct linpool *pool, const struct adata *l1, const struct adata *l2);
const struct adata *lc_set_union(struct linpool *pool, const struct adata *l1, const struct adata *l2);
struct adata *ec_set_del_nontrans(struct linpool *pool, struct adata *set);
struct adata *int_set_sort(struct linpool *pool, struct adata *src);
struct adata *ec_set_sort(struct linpool *pool, struct adata *src);
struct adata *lc_set_sort(struct linpool *pool, struct adata *src);
struct adata *ec_set_del_nontrans(struct linpool *pool, const struct adata *set);
struct adata *int_set_sort(struct linpool *pool, const struct adata *src);
struct adata *ec_set_sort(struct linpool *pool, const struct adata *src);
struct adata *lc_set_sort(struct linpool *pool, const struct adata *src);
void ec_set_sort_x(struct adata *set); /* Sort in place */

View File

@ -262,6 +262,7 @@ cli_command(struct cli *c)
bzero(&f, sizeof(f));
f.mem = c->parser_pool;
f.pool = rp_new(c->pool, "Config");
init_list(&f.symbols);
cf_read_hook = cli_cmd_read_hook;
cli_rh_pos = c->rx_buf;
cli_rh_len = strlen(c->rx_buf);

View File

@ -95,18 +95,16 @@ cmd_show_memory(void)
}
void
cmd_eval(struct f_inst *expr)
cmd_eval(const struct f_line *expr)
{
struct f_val v = f_eval(expr, this_cli->parser_pool);
buffer buf;
LOG_BUFFER_INIT(buf);
if (v.type == T_RETURN)
if (f_eval_buf(expr, this_cli->parser_pool, &buf) > F_RETURN)
{
cli_msg(8008, "runtime error");
return;
}
buffer buf;
LOG_BUFFER_INIT(buf);
val_format(v, &buf);
cli_msg(23, "%s", buf.start);
}

View File

@ -16,4 +16,6 @@ struct f_inst;
void cmd_show_status(void);
void cmd_show_symbols(struct sym_show_data *sym);
void cmd_show_memory(void);
void cmd_eval(struct f_inst *expr);
struct f_line;
void cmd_eval(const struct f_line *expr);

View File

@ -88,7 +88,7 @@ CF_ENUM(T_ENUM_ROA, ROA_, UNKNOWN, VALID, INVALID)
%type <i32> idval
%type <f> imexport
%type <r> rtable
%type <s> optsym
%type <s> optproto
%type <ra> r_args
%type <sd> sym_args
%type <i> proto_start echo_mask echo_size debug_mask debug_list debug_flag mrtdump_mask mrtdump_list mrtdump_flag export_mode limit_action net_type table_sorted tos password_algorithm
@ -112,9 +112,9 @@ rtrid:
idval:
NUM { $$ = $1; }
| '(' term ')' { $$ = f_eval_int($2); }
| '(' term ')' { $$ = f_eval_int(f_linearize($2)); }
| IP4 { $$ = ip4_to_u32($1); }
| SYM {
| CF_SYM_KNOWN {
if ($1->class == (SYM_CONSTANT | T_INT) || $1->class == (SYM_CONSTANT | T_QUAD))
$$ = SYM_VAL($1).i;
else if (($1->class == (SYM_CONSTANT | T_IP)) && ipa_is_ip4(SYM_VAL($1).ip))
@ -156,7 +156,7 @@ table_sorted:
| SORTED { $$ = 1; }
;
table: net_type TABLE SYM table_sorted {
table: net_type TABLE CF_SYM_VOID table_sorted {
struct rtable_config *cf;
cf = rt_new_table($3, $1);
cf->sorted = $4;
@ -177,28 +177,30 @@ proto_name:
/* EMPTY */ {
struct symbol *s = cf_default_name(this_proto->protocol->template, &this_proto->protocol->name_counter);
s->class = this_proto->class;
s->def = this_proto;
s->proto = this_proto;
this_proto->name = s->name;
}
| SYM {
cf_define_symbol($1, this_proto->class, this_proto);
| CF_SYM_VOID {
cf_define_symbol($1, this_proto->class, proto, this_proto);
this_proto->name = $1->name;
}
| FROM SYM {
| FROM CF_SYM_KNOWN {
if (($2->class != SYM_TEMPLATE) && ($2->class != SYM_PROTO)) cf_error("Template or protocol name expected");
struct symbol *s = cf_default_name(this_proto->protocol->template, &this_proto->protocol->name_counter);
s->class = this_proto->class;
s->def = this_proto;
s->proto = this_proto;
this_proto->name = s->name;
if (($2->class != SYM_TEMPLATE) && ($2->class != SYM_PROTO)) cf_error("Template or protocol name expected");
proto_copy_config(this_proto, $2->def);
proto_copy_config(this_proto, $2->proto);
}
| SYM FROM SYM {
cf_define_symbol($1, this_proto->class, this_proto);
| CF_SYM_VOID FROM CF_SYM_KNOWN {
if (($3->class != SYM_TEMPLATE) && ($3->class != SYM_PROTO)) cf_error("Template or protocol name expected");
cf_define_symbol($1, this_proto->class, proto, this_proto);
this_proto->name = $1->name;
if (($3->class != SYM_TEMPLATE) && ($3->class != SYM_PROTO)) cf_error("Template or protocol name expected");
proto_copy_config(this_proto, $3->def);
proto_copy_config(this_proto, $3->proto);
}
;
@ -255,12 +257,7 @@ channel_end:
proto_channel: channel_start channel_opt_list channel_end;
rtable:
SYM {
if ($1->class != SYM_TABLE) cf_error("Table expected");
$$ = $1->def;
}
;
rtable: CF_SYM_KNOWN { cf_assert_symbol($1, SYM_TABLE); $$ = $1->table; } ;
imexport:
FILTER filter { $$ = $2; }
@ -513,8 +510,8 @@ CF_CLI(SHOW PROTOCOLS, proto_patt2, [<protocol> | \"<pattern>\"], [[Show routing
CF_CLI(SHOW PROTOCOLS ALL, proto_patt2, [<protocol> | \"<pattern>\"], [[Show routing protocol details]])
{ proto_apply_cmd($4, proto_cmd_show, 0, 1); } ;
optsym:
SYM
optproto:
CF_SYM_KNOWN { cf_assert_symbol($1, SYM_PROTO); $$ = $1; }
| /* empty */ { $$ = NULL; }
;
@ -546,10 +543,10 @@ r_args:
$$->show_for = 1;
$$->addr = $3;
}
| r_args TABLE SYM {
| r_args TABLE CF_SYM_KNOWN {
cf_assert_symbol($3, SYM_TABLE);
$$ = $1;
if ($3->class != SYM_TABLE) cf_error("%s is not a table", $3->name);
rt_show_add_table($$, ((struct rtable_config *)$3->def)->table);
rt_show_add_table($$, $3->table->table);
$$->tables_defined_by = RSD_TDB_DIRECT;
}
| r_args TABLE ALL {
@ -559,10 +556,11 @@ r_args:
rt_show_add_table($$, t->table);
$$->tables_defined_by = RSD_TDB_ALL;
}
| r_args IMPORT TABLE SYM '.' r_args_channel {
| r_args IMPORT TABLE CF_SYM_KNOWN '.' r_args_channel {
cf_assert_symbol($4, SYM_PROTO);
$$ = $1;
struct proto_config *cf = (void *) $4->def;
if ($4->class != SYM_PROTO || !cf->proto) cf_error("%s is not a protocol", $4->name);
struct proto_config *cf = $4->proto;
if (!cf->proto) cf_error("%s is not a protocol", $4->name);
struct channel *c = proto_find_channel_by_name(cf->proto, $6);
if (!c) cf_error("Channel %s.%s not found", $4->name, $6);
if (!c->in_table) cf_error("No import table in channel %s.%s", $4->name, $6);
@ -591,30 +589,33 @@ r_args:
$$ = $1;
$$->filtered = 1;
}
| r_args export_mode SYM {
struct proto_config *c = (struct proto_config *) $3->def;
| r_args export_mode CF_SYM_KNOWN {
cf_assert_symbol($3, SYM_PROTO);
struct proto_config *c = (struct proto_config *) $3->proto;
$$ = $1;
if ($$->export_mode) cf_error("Export specified twice");
if ($3->class != SYM_PROTO || !c->proto) cf_error("%s is not a protocol", $3->name);
if (!c->proto) cf_error("%s is not a protocol", $3->name);
$$->export_mode = $2;
$$->export_protocol = c->proto;
$$->tables_defined_by = RSD_TDB_INDIRECT;
}
| r_args export_mode SYM '.' r_args_channel {
struct proto_config *c = (struct proto_config *) $3->def;
| r_args export_mode CF_SYM_KNOWN '.' r_args_channel {
cf_assert_symbol($3, SYM_PROTO);
struct proto_config *c = (struct proto_config *) $3->proto;
$$ = $1;
if ($$->export_mode) cf_error("Export specified twice");
if ($3->class != SYM_PROTO || !c->proto) cf_error("%s is not a protocol", $3->name);
if (!c->proto) cf_error("%s is not a protocol", $3->name);
$$->export_mode = $2;
$$->export_channel = proto_find_channel_by_name(c->proto, $5);
if (!$$->export_channel) cf_error("Export channel not found");
$$->tables_defined_by = RSD_TDB_INDIRECT;
}
| r_args PROTOCOL SYM {
struct proto_config *c = (struct proto_config *) $3->def;
| r_args PROTOCOL CF_SYM_KNOWN {
cf_assert_symbol($3, SYM_PROTO);
struct proto_config *c = (struct proto_config *) $3->proto;
$$ = $1;
if ($$->show_protocol) cf_error("Protocol specified twice");
if ($3->class != SYM_PROTO || !c->proto) cf_error("%s is not a protocol", $3->name);
if (!c->proto) cf_error("%s is not a protocol", $3->name);
$$->show_protocol = c->proto;
$$->tables_defined_by = RSD_TDB_INDIRECT;
}
@ -648,7 +649,7 @@ r_args_for:
$$ = cfg_alloc(sizeof(net_addr_ip6_sadr));
net_fill_ip6_sadr($$, $1, IP6_MAX_PREFIX_LENGTH, $3, IP6_MAX_PREFIX_LENGTH);
}
| SYM {
| CF_SYM_KNOWN {
if ($1->class == (SYM_CONSTANT | T_IP))
{
$$ = cfg_alloc(ipa_is_ip4(SYM_VAL($1).ip) ? sizeof(net_addr_ip4) : sizeof(net_addr_ip6));
@ -657,7 +658,7 @@ r_args_for:
else if (($1->class == (SYM_CONSTANT | T_NET)) && net_type_match(SYM_VAL($1).net, NB_IP | NB_VPN))
$$ = (net_addr *) SYM_VAL($1).net; /* Avoid const warning */
else
cf_error("IP address or network expected");
cf_error("IP address or network constant expected");
}
;
@ -710,7 +711,7 @@ sym_args:
| sym_args FILTER { $$ = $1; $$->type = SYM_FILTER; }
| sym_args PROTOCOL { $$ = $1; $$->type = SYM_PROTO; }
| sym_args TEMPLATE { $$ = $1; $$->type = SYM_TEMPLATE; }
| sym_args SYM { $$ = $1; $$->sym = $2; }
| sym_args symbol { $$ = $1; $$->sym = $2; }
;
@ -731,9 +732,11 @@ CF_CLI(DUMP ROUTES,,, [[Dump routing table]])
{ rt_dump_all(); cli_msg(0, ""); } ;
CF_CLI(DUMP PROTOCOLS,,, [[Dump protocol information]])
{ protos_dump_all(); cli_msg(0, ""); } ;
CF_CLI(DUMP FILTER ALL,,, [[Dump all filters in linearized form]])
{ filters_dump_all(); cli_msg(0, ""); } ;
CF_CLI(EVAL, term, <expr>, [[Evaluate an expression]])
{ cmd_eval($2); } ;
{ cmd_eval(f_linearize($2)); } ;
CF_CLI_HELP(ECHO, ..., [[Control echoing of log messages]])
CF_CLI(ECHO, echo_mask echo_size, (all | off | { debug|trace|info|remote|warning|error|auth [, ...] }) [<buffer-size>], [[Control echoing of log messages]]) {
@ -780,13 +783,13 @@ CF_CLI(RESTRICT,,,[[Restrict current CLI session to safe commands]])
{ this_cli->restricted = 1; cli_msg(16, "Access restricted"); } ;
proto_patt:
SYM { $$.ptr = $1; $$.patt = 0; }
CF_SYM_KNOWN { cf_assert_symbol($1, SYM_PROTO); $$.ptr = $1; $$.patt = 0; }
| ALL { $$.ptr = NULL; $$.patt = 1; }
| TEXT { $$.ptr = $1; $$.patt = 1; }
;
proto_patt2:
SYM { $$.ptr = $1; $$.patt = 0; }
CF_SYM_KNOWN { cf_assert_symbol($1, SYM_PROTO); $$.ptr = $1; $$.patt = 0; }
| { $$.ptr = NULL; $$.patt = 1; }
| TEXT { $$.ptr = $1; $$.patt = 1; }
;

View File

@ -885,7 +885,7 @@ proto_clone_config(struct symbol *sym, struct proto_config *parent)
cf->parent = parent;
sym->class = cf->class;
sym->def = cf;
sym->proto = cf;
}
static void
@ -894,7 +894,7 @@ proto_undef_clone(struct symbol *sym, struct proto_config *cf)
rem_node(&cf->n);
sym->class = SYM_VOID;
sym->def = NULL;
sym->proto = NULL;
}
/**
@ -1009,7 +1009,7 @@ protos_commit(struct config *new, struct config *old, int force_reconfig, int ty
cfg_mem = new->mem;
conf_this_scope = new->root_scope;
sym = cf_get_symbol(oc->name);
proto_clone_config(sym, parsym->def);
proto_clone_config(sym, parsym->proto);
new_config = NULL;
cfg_mem = NULL;
}
@ -1019,7 +1019,7 @@ protos_commit(struct config *new, struct config *old, int force_reconfig, int ty
{
/* Found match, let's check if we can smoothly switch to new configuration */
/* No need to check description */
nc = sym->def;
nc = sym->proto;
nc->proto = p;
/* We will try to reconfigure protocol p */
@ -1968,7 +1968,7 @@ proto_apply_cmd_symbol(struct symbol *s, void (* cmd)(struct proto *, uintptr_t,
return;
}
cmd(((struct proto_config *)s->def)->proto, arg, 0);
cmd(s->proto->proto, arg, 0);
cli_msg(0, "");
}
@ -2011,7 +2011,7 @@ proto_get_named(struct symbol *sym, struct protocol *pr)
if (sym->class != SYM_PROTO)
cf_error("%s: Not a protocol", sym->name);
p = ((struct proto_config *) sym->def)->proto;
p = sym->proto->proto;
if (!p || p->proto != pr)
cf_error("%s: Not a %s protocol", sym->name, pr->name);
}

View File

@ -483,7 +483,7 @@ struct channel_config {
struct proto_config *parent; /* Where channel is defined (proto or template) */
struct rtable_config *table; /* Table we're attached to */
struct filter *in_filter, *out_filter; /* Attached filters */
const struct filter *in_filter, *out_filter; /* Attached filters */
struct channel_limit rx_limit; /* Limit for receiving routes from protocol
(relevant when in_keep_filtered is active) */
struct channel_limit in_limit; /* Limit for importing routes from protocol */
@ -505,8 +505,8 @@ struct channel {
struct proto *proto;
struct rtable *table;
struct filter *in_filter; /* Input filter */
struct filter *out_filter; /* Output filter */
const struct filter *in_filter; /* Input filter */
const struct filter *out_filter; /* Output filter */
struct channel_limit rx_limit; /* Receive limit (for in_keep_filtered) */
struct channel_limit in_limit; /* Input limit */
struct channel_limit out_limit; /* Output limit */

View File

@ -297,7 +297,7 @@ rte *rte_find(net *net, struct rte_src *src);
rte *rte_get_temp(struct rta *);
void rte_update2(struct channel *c, const net_addr *n, rte *new, struct rte_src *src);
/* rte_update() moved to protocol.h to avoid dependency conflicts */
int rt_examine(rtable *t, net_addr *a, struct proto *p, struct filter *filter);
int rt_examine(rtable *t, net_addr *a, struct proto *p, const struct filter *filter);
rte *rt_export_merged(struct channel *c, net *net, rte **rt_free, linpool *pool, int silent);
void rt_refresh_begin(rtable *t, struct channel *c);
void rt_refresh_end(rtable *t, struct channel *c);
@ -339,7 +339,7 @@ struct rt_show_data {
struct rt_show_data_rtable *last_table; /* Last table in output */
struct fib_iterator fit; /* Iterator over networks in table */
int verbose, tables_defined_by;
struct filter *filter;
const struct filter *filter;
struct proto *show_protocol;
struct proto *export_protocol;
struct channel *export_channel;
@ -477,7 +477,7 @@ typedef struct eattr {
byte type; /* Attribute type and several flags (EAF_...) */
union {
u32 data;
struct adata *ptr; /* Attribute data elsewhere */
const struct adata *ptr; /* Attribute data elsewhere */
} u;
} eattr;
@ -498,6 +498,7 @@ const char *ea_custom_name(uint ea);
#define EA_CUSTOM_BIT 0x8000
#define EA_ALLOW_UNDEF 0x10000 /* ea_find: allow EAF_TYPE_UNDEF */
#define EA_BIT(n) ((n) << 24) /* Used in bitfield accessors */
#define EA_BIT_GET(ea) ((ea) >> 24)
#define EAF_TYPE_MASK 0x1f /* Mask with this to get type */
#define EAF_TYPE_INT 0x01 /* 32-bit unsigned integer number */
@ -520,6 +521,8 @@ typedef struct adata {
byte data[0];
} adata;
extern const adata null_adata; /* adata of length 0 */
static inline struct adata *
lp_alloc_adata(struct linpool *pool, uint len)
{
@ -528,7 +531,7 @@ lp_alloc_adata(struct linpool *pool, uint len)
return ad;
}
static inline int adata_same(struct adata *a, struct adata *b)
static inline int adata_same(const struct adata *a, const struct adata *b)
{ return (a->length == b->length && !memcmp(a->data, b->data, a->length)); }

View File

@ -58,6 +58,8 @@
#include <stddef.h>
const adata null_adata; /* adata of length 0 */
const char * const rta_src_names[RTS_MAX] = {
[RTS_DUMMY] = "",
[RTS_STATIC] = "static",
@ -777,7 +779,7 @@ ea_free(ea_list *o)
{
eattr *a = &o->attrs[i];
if (!(a->type & EAF_EMBEDDED))
mb_free(a->u.ptr);
mb_free((void *) a->u.ptr);
}
mb_free(o);
}
@ -822,7 +824,7 @@ ea_format_bitfield(struct eattr *a, byte *buf, int bufsize, const char **names,
}
static inline void
opaque_format(struct adata *ad, byte *buf, uint size)
opaque_format(const struct adata *ad, byte *buf, uint size)
{
byte *bound = buf + size - 10;
uint i;
@ -845,7 +847,7 @@ opaque_format(struct adata *ad, byte *buf, uint size)
}
static inline void
ea_show_int_set(struct cli *c, struct adata *ad, int way, byte *pos, byte *buf, byte *end)
ea_show_int_set(struct cli *c, const struct adata *ad, int way, byte *pos, byte *buf, byte *end)
{
int i = int_set_format(ad, way, 0, pos, end - pos);
cli_printf(c, -1012, "\t%s", buf);
@ -857,7 +859,7 @@ ea_show_int_set(struct cli *c, struct adata *ad, int way, byte *pos, byte *buf,
}
static inline void
ea_show_ec_set(struct cli *c, struct adata *ad, byte *pos, byte *buf, byte *end)
ea_show_ec_set(struct cli *c, const struct adata *ad, byte *pos, byte *buf, byte *end)
{
int i = ec_set_format(ad, 0, pos, end - pos);
cli_printf(c, -1012, "\t%s", buf);
@ -869,7 +871,7 @@ ea_show_ec_set(struct cli *c, struct adata *ad, byte *pos, byte *buf, byte *end)
}
static inline void
ea_show_lc_set(struct cli *c, struct adata *ad, byte *pos, byte *buf, byte *end)
ea_show_lc_set(struct cli *c, const struct adata *ad, byte *pos, byte *buf, byte *end)
{
int i = lc_set_format(ad, 0, pos, end - pos);
cli_printf(c, -1012, "\t%s", buf);
@ -896,7 +898,7 @@ ea_show(struct cli *c, eattr *e)
{
struct protocol *p;
int status = GA_UNKNOWN;
struct adata *ad = (e->type & EAF_EMBEDDED) ? NULL : e->u.ptr;
const struct adata *ad = (e->type & EAF_EMBEDDED) ? NULL : e->u.ptr;
byte buf[CLI_MSG_SIZE];
byte *pos = buf, *end = buf + sizeof(buf);
@ -1035,7 +1037,7 @@ ea_hash(ea_list *e)
h ^= a->u.data;
else
{
struct adata *d = a->u.ptr;
const struct adata *d = a->u.ptr;
h ^= mem_hash(d->data, d->length);
}
h *= mul;

View File

@ -39,6 +39,7 @@
#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"
@ -567,7 +568,7 @@ static rte *
export_filter_(struct channel *c, rte *rt0, rte **rt_free, linpool *pool, int silent)
{
struct proto *p = c->proto;
struct filter *filter = c->out_filter;
const struct filter *filter = c->out_filter;
struct proto_stats *stats = &c->stats;
rte *rt;
int v;
@ -1534,7 +1535,7 @@ rte_update2(struct channel *c, const net_addr *n, rte *new, struct rte_src *src)
{
struct proto *p = c->proto;
struct proto_stats *stats = &c->stats;
struct filter *filter = c->in_filter;
const struct filter *filter = c->in_filter;
rte *dummy = NULL;
net *nn;
@ -1676,7 +1677,7 @@ rte_modify(rte *old)
/* Check rtable for best route to given net whether it would be exported do p */
int
rt_examine(rtable *t, net_addr *a, struct proto *p, struct filter *filter)
rt_examine(rtable *t, net_addr *a, struct proto *p, const struct filter *filter)
{
net *n = net_find(t, a);
rte *rt = n ? n->routes : NULL;
@ -2279,13 +2280,13 @@ rt_new_table(struct symbol *s, uint addr_type)
{
/* Hack that allows to 'redefine' the master table */
if ((s->class == SYM_TABLE) &&
(s->def == new_config->def_tables[addr_type]) &&
(s->table == new_config->def_tables[addr_type]) &&
((addr_type == NET_IP4) || (addr_type == NET_IP6)))
return s->def;
return s->table;
struct rtable_config *c = cfg_allocz(sizeof(struct rtable_config));
cf_define_symbol(s, SYM_TABLE, c);
cf_define_symbol(s, SYM_TABLE, table, c);
c->name = s->name;
c->addr_type = addr_type;
c->gc_max_ops = 1000;
@ -2344,7 +2345,7 @@ static struct rtable_config *
rt_find_table_config(struct config *cf, char *name)
{
struct symbol *sym = cf_find_symbol(cf, name);
return (sym && (sym->class == SYM_TABLE)) ? sym->def : NULL;
return (sym && (sym->class == SYM_TABLE)) ? sym->table : NULL;
}
/**

View File

@ -129,16 +129,16 @@ dynamic_attr: BABEL_METRIC { $$ = f_new_dynamic_attr(EAF_TYPE_INT, T_INT, EA_BAB
CF_CLI_HELP(SHOW BABEL, ..., [[Show information about Babel protocol]]);
CF_CLI(SHOW BABEL INTERFACES, optsym opttext, [<name>] [\"<interface>\"], [[Show information about Babel interfaces]])
CF_CLI(SHOW BABEL INTERFACES, optproto opttext, [<name>] [\"<interface>\"], [[Show information about Babel interfaces]])
{ babel_show_interfaces(proto_get_named($4, &proto_babel), $5); };
CF_CLI(SHOW BABEL NEIGHBORS, optsym opttext, [<name>] [\"<interface>\"], [[Show information about Babel neighbors]])
CF_CLI(SHOW BABEL NEIGHBORS, optproto opttext, [<name>] [\"<interface>\"], [[Show information about Babel neighbors]])
{ babel_show_neighbors(proto_get_named($4, &proto_babel), $5); };
CF_CLI(SHOW BABEL ENTRIES, optsym opttext, [<name>], [[Show information about Babel prefix entries]])
CF_CLI(SHOW BABEL ENTRIES, optproto opttext, [<name>], [[Show information about Babel prefix entries]])
{ babel_show_entries(proto_get_named($4, &proto_babel)); };
CF_CLI(SHOW BABEL ROUTES, optsym opttext, [<name>], [[Show information about Babel route entries]])
CF_CLI(SHOW BABEL ROUTES, optproto opttext, [<name>], [[Show information about Babel route entries]])
{ babel_show_routes(proto_get_named($4, &proto_babel)); };
CF_CODE

View File

@ -130,7 +130,7 @@ bfd_multihop:
bfd_neigh_iface:
/* empty */ { $$ = NULL; }
| '%' SYM { $$ = if_get_by_name($2->name); }
| '%' symbol { $$ = if_get_by_name($2->name); }
| DEV text { $$ = if_get_by_name($2); }
;
@ -163,7 +163,7 @@ bfd_neighbor: ipa bfd_neigh_iface bfd_neigh_local bfd_neigh_multihop
CF_CLI_HELP(SHOW BFD, ..., [[Show information about BFD protocol]]);
CF_CLI(SHOW BFD SESSIONS, optsym, [<name>], [[Show information about BFD sessions]])
CF_CLI(SHOW BFD SESSIONS, optproto, [<name>], [[Show information about BFD sessions]])
{ bfd_show_sessions(proto_get_named($4, &proto_bfd)); };
CF_CODE

View File

@ -181,7 +181,7 @@ bgp_encode_u32s(struct bgp_write_state *s UNUSED, eattr *a, byte *buf, uint size
}
static int
bgp_put_attr(byte *buf, uint size, uint code, uint flags, byte *data, uint len)
bgp_put_attr(byte *buf, uint size, uint code, uint flags, const byte *data, uint len)
{
if (size < (4+len))
return -1;
@ -234,15 +234,15 @@ bgp_format_origin(eattr *a, byte *buf, uint size UNUSED)
static int
bgp_encode_as_path(struct bgp_write_state *s, eattr *a, byte *buf, uint size)
{
byte *data = a->u.ptr->data;
const byte *data = a->u.ptr->data;
uint len = a->u.ptr->length;
if (!s->as4_session)
{
/* Prepare 16-bit AS_PATH (from 32-bit one) in a temporary buffer */
byte *src = data;
data = alloca(len);
len = as_path_32to16(data, src, len);
byte *dst = alloca(len);
len = as_path_32to16(dst, data, len);
data = dst;
}
return bgp_put_attr(buf, size, BA_AS_PATH, a->flags, data, len);
@ -381,15 +381,14 @@ bgp_decode_atomic_aggr(struct bgp_parse_state *s, uint code UNUSED, uint flags,
static int
bgp_encode_aggregator(struct bgp_write_state *s, eattr *a, byte *buf, uint size)
{
byte *data = a->u.ptr->data;
const byte *data = a->u.ptr->data;
uint len = a->u.ptr->length;
if (!s->as4_session)
{
/* Prepare 16-bit AGGREGATOR (from 32-bit one) in a temporary buffer */
byte *src = data;
data = alloca(6);
len = aggregator_32to16(data, src);
byte *dst = alloca(6);
len = aggregator_32to16(dst, data);
}
return bgp_put_attr(buf, size, BA_AGGREGATOR, a->flags, data, len);
@ -415,7 +414,7 @@ bgp_decode_aggregator(struct bgp_parse_state *s, uint code UNUSED, uint flags, b
static void
bgp_format_aggregator(eattr *a, byte *buf, uint size UNUSED)
{
byte *data = a->u.ptr->data;
const byte *data = a->u.ptr->data;
bsprintf(buf, "%I4 AS%u", get_ip4(data+4), get_u32(data+0));
}
@ -545,12 +544,13 @@ bgp_decode_mp_unreach_nlri(struct bgp_parse_state *s, uint code UNUSED, uint fla
static void
bgp_export_ext_community(struct bgp_export_state *s, eattr *a)
{
a->u.ptr = ec_set_del_nontrans(s->pool, a->u.ptr);
struct adata *ad = ec_set_del_nontrans(s->pool, a->u.ptr);
if (a->u.ptr->length == 0)
if (ad->length == 0)
UNSET(a);
ec_set_sort_x(a->u.ptr);
ec_set_sort_x(ad);
a->u.ptr = ad;
}
static void
@ -1232,7 +1232,7 @@ bgp_get_bucket(struct bgp_channel *c, ea_list *new)
if (!(a->type & EAF_EMBEDDED))
{
struct adata *oa = a->u.ptr;
const struct adata *oa = a->u.ptr;
struct adata *na = (struct adata *) dest;
memcpy(na, oa, sizeof(struct adata) + oa->length);
a->u.ptr = na;
@ -1404,7 +1404,7 @@ bgp_preexport(struct proto *P, rte **new, struct linpool *pool UNUSED)
if (p->cf->interpret_communities &&
(c = ea_find(e->attrs->eattrs, EA_CODE(PROTOCOL_BGP, BA_COMMUNITY))))
{
struct adata *d = c->u.ptr;
const struct adata *d = c->u.ptr;
/* Do not export anywhere */
if (int_set_contains(d, BGP_COMM_NO_ADVERTISE))
@ -1426,9 +1426,6 @@ bgp_preexport(struct proto *P, rte **new, struct linpool *pool UNUSED)
return 0;
}
static adata null_adata; /* adata of length 0 */
static ea_list *
bgp_update_attrs(struct bgp_proto *p, struct bgp_channel *c, rte *e, ea_list *attrs0, struct linpool *pool)
{
@ -1437,7 +1434,7 @@ bgp_update_attrs(struct bgp_proto *p, struct bgp_channel *c, rte *e, ea_list *at
struct bgp_export_state s = { .proto = p, .channel = c, .pool = pool, .src = src, .route = e, .mpls = c->desc->mpls };
ea_list *attrs = attrs0;
eattr *a;
adata *ad;
const adata *ad;
/* ORIGIN attribute - mandatory, attach if missing */
if (! bgp_find_attr(attrs0, BA_ORIGIN))
@ -1962,7 +1959,7 @@ struct rte *
bgp_rte_modify_stale(struct rte *r, struct linpool *pool)
{
eattr *a = ea_find(r->attrs->eattrs, EA_CODE(PROTOCOL_BGP, BA_COMMUNITY));
struct adata *ad = a ? a->u.ptr : NULL;
const struct adata *ad = a ? a->u.ptr : NULL;
uint flags = a ? a->flags : BAF_PARTIAL;
if (ad && int_set_contains(ad, BGP_COMM_NO_LLGR))
@ -2021,8 +2018,8 @@ bgp_process_as4_attrs(ea_list **attrs, struct linpool *pool)
return;
/* Merge AS_PATH and AS4_PATH */
as_path_cut(p2->u.ptr, p2_len - p4_len);
p2->u.ptr = as_path_merge(pool, p2->u.ptr, p4->u.ptr);
struct adata *apc = as_path_cut(pool, p2->u.ptr, p2_len - p4_len);
p2->u.ptr = as_path_merge(pool, apc, p4->u.ptr);
}
}

View File

@ -501,9 +501,9 @@ bgp_spawn(struct bgp_proto *pp, ip_addr remote_ip)
cfg_mem = NULL;
/* Just pass remote_ip to bgp_init() */
((struct bgp_config *) sym->def)->remote_ip = remote_ip;
((struct bgp_config *) sym->proto)->remote_ip = remote_ip;
return (void *) proto_spawn(sym->def, 0);
return (void *) proto_spawn(sym->proto, 0);
}
void

View File

@ -392,7 +392,7 @@ struct bgp_write_state {
int mpls;
eattr *mp_next_hop;
adata *mpls_labels;
const adata *mpls_labels;
};
struct bgp_parse_state {
@ -523,7 +523,7 @@ bgp_set_attr_u32(ea_list **to, struct linpool *pool, uint code, uint flags, u32
{ bgp_set_attr(to, pool, code, flags, (uintptr_t) val); }
static inline void
bgp_set_attr_ptr(ea_list **to, struct linpool *pool, uint code, uint flags, struct adata *val)
bgp_set_attr_ptr(ea_list **to, struct linpool *pool, uint code, uint flags, const struct adata *val)
{ bgp_set_attr(to, pool, code, flags, (uintptr_t) val); }
static inline void

View File

@ -1268,10 +1268,10 @@ bgp_rte_update(struct bgp_parse_state *s, net_addr *n, u32 path_id, rta *a0)
}
static void
bgp_encode_mpls_labels(struct bgp_write_state *s UNUSED, adata *mpls, byte **pos, uint *size, byte *pxlen)
bgp_encode_mpls_labels(struct bgp_write_state *s UNUSED, const adata *mpls, byte **pos, uint *size, byte *pxlen)
{
u32 dummy = 0;
u32 *labels = mpls ? (u32 *) mpls->data : &dummy;
const u32 dummy = 0;
const u32 *labels = mpls ? (const u32 *) mpls->data : &dummy;
uint lnum = mpls ? (mpls->length / 4) : 1;
for (uint i = 0; i < lnum; i++)

View File

@ -23,7 +23,7 @@ struct mrt_config {
struct rtable_config *table_cf;
const char *table_expr;
struct filter *filter;
const struct filter *filter;
const char *filename;
uint period;
int always_add_path;
@ -41,7 +41,7 @@ struct mrt_proto {
struct mrt_dump_data {
const char *table_expr;
struct rtable *table_ptr;
struct filter *filter;
const struct filter *filter;
char *filename;
};
@ -61,7 +61,7 @@ struct mrt_table_dump_state {
/* Configuration information */
const char *table_expr; /* Wildcard for table name (or NULL) */
struct rtable *table_ptr; /* Explicit table (or NULL) */
struct filter *filter; /* Optional filter */
const struct filter *filter; /* Optional filter */
const char *filename; /* Filename pattern */
int always_add_path; /* Always use *_ADDPATH message subtypes */

View File

@ -510,29 +510,29 @@ dynamic_attr: OSPF_TAG { $$ = f_new_dynamic_attr(EAF_TYPE_INT, T_INT, EA_OSPF_TA
dynamic_attr: OSPF_ROUTER_ID { $$ = f_new_dynamic_attr(EAF_TYPE_ROUTER_ID, T_QUAD, EA_OSPF_ROUTER_ID); } ;
CF_CLI_HELP(SHOW OSPF, ..., [[Show information about OSPF protocol]]);
CF_CLI(SHOW OSPF, optsym, [<name>], [[Show information about OSPF protocol]])
CF_CLI(SHOW OSPF, optproto, [<name>], [[Show information about OSPF protocol]])
{ ospf_sh(proto_get_named($3, &proto_ospf)); };
CF_CLI(SHOW OSPF NEIGHBORS, optsym opttext, [<name>] [\"<interface>\"], [[Show information about OSPF neighbors]])
CF_CLI(SHOW OSPF NEIGHBORS, optproto opttext, [<name>] [\"<interface>\"], [[Show information about OSPF neighbors]])
{ ospf_sh_neigh(proto_get_named($4, &proto_ospf), $5); };
CF_CLI(SHOW OSPF INTERFACE, optsym opttext, [<name>] [\"<interface>\"], [[Show information about interface]])
CF_CLI(SHOW OSPF INTERFACE, optproto opttext, [<name>] [\"<interface>\"], [[Show information about interface]])
{ ospf_sh_iface(proto_get_named($4, &proto_ospf), $5); };
CF_CLI_HELP(SHOW OSPF TOPOLOGY, [all] [<name>], [[Show information about OSPF network topology]])
CF_CLI(SHOW OSPF TOPOLOGY, optsym opttext, [<name>], [[Show information about reachable OSPF network topology]])
CF_CLI(SHOW OSPF TOPOLOGY, optproto opttext, [<name>], [[Show information about reachable OSPF network topology]])
{ ospf_sh_state(proto_get_named($4, &proto_ospf), 0, 1); };
CF_CLI(SHOW OSPF TOPOLOGY ALL, optsym opttext, [<name>], [[Show information about all OSPF network topology]])
CF_CLI(SHOW OSPF TOPOLOGY ALL, optproto opttext, [<name>], [[Show information about all OSPF network topology]])
{ ospf_sh_state(proto_get_named($5, &proto_ospf), 0, 0); };
CF_CLI_HELP(SHOW OSPF STATE, [all] [<name>], [[Show information about OSPF network state]])
CF_CLI(SHOW OSPF STATE, optsym opttext, [<name>], [[Show information about reachable OSPF network state]])
CF_CLI(SHOW OSPF STATE, optproto opttext, [<name>], [[Show information about reachable OSPF network state]])
{ ospf_sh_state(proto_get_named($4, &proto_ospf), 1, 1); };
CF_CLI(SHOW OSPF STATE ALL, optsym opttext, [<name>], [[Show information about all OSPF network state]])
CF_CLI(SHOW OSPF STATE ALL, optproto opttext, [<name>], [[Show information about all OSPF network state]])
{ ospf_sh_state(proto_get_named($5, &proto_ospf), 1, 0); };
CF_CLI_HELP(SHOW OSPF LSADB, ..., [[Show content of OSPF LSA database]]);
@ -550,7 +550,7 @@ lsadb_args:
| lsadb_args LSID idval { $$ = $1; $$->lsid = $3; }
| lsadb_args SELF { $$ = $1; $$->router = SH_ROUTER_SELF; }
| lsadb_args ROUTER idval { $$ = $1; $$->router = $3; }
| lsadb_args SYM { $$ = $1; $$->name = $2; }
| lsadb_args CF_SYM_KNOWN { cf_assert_symbol($2, SYM_PROTO); $$ = $1; $$->proto = (struct ospf_proto *) proto_get_named($2, &proto_ospf); }
;
CF_CODE

View File

@ -1451,7 +1451,7 @@ lsa_compare_for_lsadb(const void *p1, const void *p2)
void
ospf_sh_lsadb(struct lsadb_show_data *ld)
{
struct ospf_proto *p = (struct ospf_proto *) proto_get_named(ld->name, &proto_ospf);
struct ospf_proto *p = ld->proto;
uint num = p->gr->hash_entries;
uint i, j;
int last_dscope = -1;

View File

@ -929,7 +929,7 @@ struct ospf_lsreq_header
#define SH_ROUTER_SELF 0xffffffff
struct lsadb_show_data {
struct symbol *name; /* Protocol to request data from */
struct ospf_proto *proto; /* Protocol to request data from */
u16 type; /* LSA Type, 0 -> all */
u16 scope; /* Scope, 0 -> all, hack to handle link scope as 1 */
u32 area; /* Specified for area scope */

View File

@ -90,6 +90,8 @@ struct perf_random_routes {
struct rta a;
};
static const uint perf_random_routes_size = sizeof(net_addr) + sizeof(rte *) + RTA_MAX_SIZE;
static inline s64 timediff(struct timespec *begin, struct timespec *end)
{ return (end->tv_sec - begin->tv_sec) * (s64) 1000000000 + end->tv_nsec - begin->tv_nsec; }
@ -124,7 +126,7 @@ perf_loop(void *data)
struct perf_proto *p = data;
const uint N = 1U << p->exp;
const uint offset = sizeof(net_addr) + RTA_MAX_SIZE;
const uint offset = perf_random_routes_size;
if (!p->run) {
ASSERT(p->data == NULL);

View File

@ -191,10 +191,10 @@ dynamic_attr: RIP_TAG { $$ = f_new_dynamic_attr(EAF_TYPE_INT, T_INT, EA_RIP_TAG)
CF_CLI_HELP(SHOW RIP, ..., [[Show information about RIP protocol]]);
CF_CLI(SHOW RIP INTERFACES, optsym opttext, [<name>] [\"<interface>\"], [[Show information about RIP interfaces]])
CF_CLI(SHOW RIP INTERFACES, optproto opttext, [<name>] [\"<interface>\"], [[Show information about RIP interfaces]])
{ rip_show_interfaces(proto_get_named($4, &proto_rip), $5); };
CF_CLI(SHOW RIP NEIGHBORS, optsym opttext, [<name>] [\"<interface>\"], [[Show information about RIP neighbors]])
CF_CLI(SHOW RIP NEIGHBORS, optproto opttext, [<name>] [\"<interface>\"], [[Show information about RIP neighbors]])
{ rip_show_neighbors(proto_get_named($4, &proto_rip), $5); };

View File

@ -14,7 +14,7 @@ CF_DEFINES
#define STATIC_CFG ((struct static_config *) this_proto)
static struct static_route *this_srt, *this_snh;
static struct f_inst **this_srt_last_cmd;
static struct f_inst *this_srt_cmds, *this_srt_last_cmd;
static struct static_route *
static_nexthop_new(void)
@ -39,6 +39,8 @@ static_route_finish(void)
{
if (net_type_match(this_srt->net, NB_DEST) == !this_srt->dest)
cf_error("Unexpected or missing nexthop/type");
this_srt->cmds = f_linearize(this_srt_cmds);
}
CF_DECLS
@ -108,7 +110,8 @@ stat_route0: ROUTE net_any {
this_srt = cfg_allocz(sizeof(struct static_route));
add_tail(&STATIC_CFG->routes, &this_srt->n);
this_srt->net = $2;
this_srt_last_cmd = &(this_srt->cmds);
this_srt_cmds = NULL;
this_srt_last_cmd = NULL;
this_srt->mp_next = NULL;
this_snh = NULL;
}
@ -134,7 +137,13 @@ stat_route:
;
stat_route_item:
cmd { *this_srt_last_cmd = $1; this_srt_last_cmd = &($1->next); }
cmd {
if (this_srt_last_cmd)
this_srt_last_cmd->next = $1;
else
this_srt_cmds = $1;
this_srt_last_cmd = $1;
}
;
stat_route_opts:
@ -148,7 +157,7 @@ stat_route_opt_list:
;
CF_CLI(SHOW STATIC, optsym, [<name>], [[Show details of static protocol]])
CF_CLI(SHOW STATIC, optproto, [<name>], [[Show details of static protocol]])
{ static_show(proto_get_named($3, &proto_static)); } ;
CF_CODE

View File

@ -309,7 +309,7 @@ static inline int
static_same_rte(struct static_route *or, struct static_route *nr)
{
/* Note that i_same() requires arguments in (new, old) order */
return static_same_dest(or, nr) && i_same(nr->cmds, or->cmds);
return static_same_dest(or, nr) && f_same(nr->cmds, or->cmds);
}
static void

View File

@ -39,7 +39,7 @@ struct static_route {
struct static_route *chain; /* Next for the same neighbor */
struct static_route *mp_head; /* First nexthop of this route */
struct static_route *mp_next; /* Nexthops for multipath routes */
struct f_inst *cmds; /* List of commands for setting attributes */
struct f_line *cmds; /* List of commands for setting attributes */
byte dest; /* Destination type (RTD_*) */
byte state; /* State of route announcement (SRS_*) */
byte active; /* Next hop is active (nbr/iface/BFD available) */

View File

@ -65,27 +65,6 @@ static inline struct ifa * kif_get_primary_ip(struct iface *i UNUSED) { return N
#define EA_KRT_INITRWND EA_CODE(PROTOCOL_KERNEL, 0x2e)
#define EA_KRT_QUICKACK EA_CODE(PROTOCOL_KERNEL, 0x2f)
/* Bits of EA_KRT_LOCK, also based on RTAX_* constants */
#define EA_KRT_LOCK_MTU EA_KRT_LOCK | EA_BIT(0x2)
#define EA_KRT_LOCK_WINDOW EA_KRT_LOCK | EA_BIT(0x3)
#define EA_KRT_LOCK_RTT EA_KRT_LOCK | EA_BIT(0x4)
#define EA_KRT_LOCK_RTTVAR EA_KRT_LOCK | EA_BIT(0x5)
#define EA_KRT_LOCK_SSTHRESH EA_KRT_LOCK | EA_BIT(0x6)
#define EA_KRT_LOCK_CWND EA_KRT_LOCK | EA_BIT(0x7)
#define EA_KRT_LOCK_ADVMSS EA_KRT_LOCK | EA_BIT(0x8)
#define EA_KRT_LOCK_REORDERING EA_KRT_LOCK | EA_BIT(0x9)
#define EA_KRT_LOCK_HOPLIMIT EA_KRT_LOCK | EA_BIT(0xa)
// define EA_KRT_LOCK_INITCWND EA_KRT_LOCK | EA_BIT(0xb)
// define EA_KRT_LOCK_FEATURES EA_KRT_LOCK | EA_BIT(0xc)
#define EA_KRT_LOCK_RTO_MIN EA_KRT_LOCK | EA_BIT(0xd)
// define EA_KRT_LOCK_INITRWND EA_KRT_LOCK | EA_BIT(0xe)
/* Bits of EA_KRT_FEATURES, based on RTAX_FEATURE_* constants */
#define EA_KRT_FEATURE_ECN EA_KRT_FEATURES | EA_BIT(0x0)
// define EA_KRT_FEATURE_SACK EA_KRT_FEATURES | EA_BIT(0x1)
// define EA_KRT_FEATURE_TSTAMP EA_KRT_FEATURES | EA_BIT(0x2)
#define EA_KRT_FEATURE_ALLFRAG EA_KRT_FEATURES | EA_BIT(0x3)
struct krt_params {
u32 table_id; /* Kernel table ID we sync with */

View File

@ -44,19 +44,21 @@ dynamic_attr: KRT_RTO_MIN { $$ = f_new_dynamic_attr(EAF_TYPE_INT, T_INT, EA_KRT_
dynamic_attr: KRT_INITRWND { $$ = f_new_dynamic_attr(EAF_TYPE_INT, T_INT, EA_KRT_INITRWND); } ;
dynamic_attr: KRT_QUICKACK { $$ = f_new_dynamic_attr(EAF_TYPE_INT, T_INT, EA_KRT_QUICKACK); } ;
dynamic_attr: KRT_LOCK_MTU { $$ = f_new_dynamic_attr(EAF_TYPE_BITFIELD, T_BOOL, EA_KRT_LOCK_MTU); } ;
dynamic_attr: KRT_LOCK_WINDOW { $$ = f_new_dynamic_attr(EAF_TYPE_BITFIELD, T_BOOL, EA_KRT_LOCK_WINDOW); } ;
dynamic_attr: KRT_LOCK_RTT { $$ = f_new_dynamic_attr(EAF_TYPE_BITFIELD, T_BOOL, EA_KRT_LOCK_RTT); } ;
dynamic_attr: KRT_LOCK_RTTVAR { $$ = f_new_dynamic_attr(EAF_TYPE_BITFIELD, T_BOOL, EA_KRT_LOCK_RTTVAR); } ;
dynamic_attr: KRT_LOCK_SSTRESH { $$ = f_new_dynamic_attr(EAF_TYPE_BITFIELD, T_BOOL, EA_KRT_LOCK_SSTHRESH); } ;
dynamic_attr: KRT_LOCK_CWND { $$ = f_new_dynamic_attr(EAF_TYPE_BITFIELD, T_BOOL, EA_KRT_LOCK_CWND); } ;
dynamic_attr: KRT_LOCK_ADVMSS { $$ = f_new_dynamic_attr(EAF_TYPE_BITFIELD, T_BOOL, EA_KRT_LOCK_ADVMSS); } ;
dynamic_attr: KRT_LOCK_REORDERING { $$ = f_new_dynamic_attr(EAF_TYPE_BITFIELD, T_BOOL, EA_KRT_LOCK_REORDERING); } ;
dynamic_attr: KRT_LOCK_HOPLIMIT { $$ = f_new_dynamic_attr(EAF_TYPE_BITFIELD, T_BOOL, EA_KRT_LOCK_HOPLIMIT); } ;
dynamic_attr: KRT_LOCK_RTO_MIN { $$ = f_new_dynamic_attr(EAF_TYPE_BITFIELD, T_BOOL, EA_KRT_LOCK_RTO_MIN); } ;
/* Bits of EA_KRT_LOCK, based on RTAX_* constants */
dynamic_attr: KRT_FEATURE_ECN { $$ = f_new_dynamic_attr(EAF_TYPE_BITFIELD, T_BOOL, EA_KRT_FEATURE_ECN); } ;
dynamic_attr: KRT_FEATURE_ALLFRAG { $$ = f_new_dynamic_attr(EAF_TYPE_BITFIELD, T_BOOL, EA_KRT_FEATURE_ALLFRAG); } ;
dynamic_attr: KRT_LOCK_MTU { $$ = f_new_dynamic_attr_bit(2, T_BOOL, EA_KRT_LOCK); } ;
dynamic_attr: KRT_LOCK_WINDOW { $$ = f_new_dynamic_attr_bit(3, T_BOOL, EA_KRT_LOCK); } ;
dynamic_attr: KRT_LOCK_RTT { $$ = f_new_dynamic_attr_bit(4, T_BOOL, EA_KRT_LOCK); } ;
dynamic_attr: KRT_LOCK_RTTVAR { $$ = f_new_dynamic_attr_bit(5, T_BOOL, EA_KRT_LOCK); } ;
dynamic_attr: KRT_LOCK_SSTRESH { $$ = f_new_dynamic_attr_bit(6, T_BOOL, EA_KRT_LOCK); } ;
dynamic_attr: KRT_LOCK_CWND { $$ = f_new_dynamic_attr_bit(7, T_BOOL, EA_KRT_LOCK); } ;
dynamic_attr: KRT_LOCK_ADVMSS { $$ = f_new_dynamic_attr_bit(8, T_BOOL, EA_KRT_LOCK); } ;
dynamic_attr: KRT_LOCK_REORDERING { $$ = f_new_dynamic_attr_bit(9, T_BOOL, EA_KRT_LOCK); } ;
dynamic_attr: KRT_LOCK_HOPLIMIT { $$ = f_new_dynamic_attr_bit(10, T_BOOL, EA_KRT_LOCK); } ;
dynamic_attr: KRT_LOCK_RTO_MIN { $$ = f_new_dynamic_attr_bit(13, T_BOOL, EA_KRT_LOCK); } ;
dynamic_attr: KRT_FEATURE_ECN { $$ = f_new_dynamic_attr_bit(0, T_BOOL, EA_KRT_FEATURES); } ;
dynamic_attr: KRT_FEATURE_ALLFRAG { $$ = f_new_dynamic_attr(3, T_BOOL, EA_KRT_FEATURES); } ;
CF_CODE

View File

@ -1740,9 +1740,12 @@ nl_parse_route(struct nl_parse_state *s, struct nlmsghdr *h)
ea->attrs[0].id = EA_KRT_PREFSRC;
ea->attrs[0].flags = 0;
ea->attrs[0].type = EAF_TYPE_IP_ADDRESS;
ea->attrs[0].u.ptr = lp_alloc(s->pool, sizeof(struct adata) + sizeof(ps));
ea->attrs[0].u.ptr->length = sizeof(ps);
memcpy(ea->attrs[0].u.ptr->data, &ps, sizeof(ps));
struct adata *ad = lp_alloc(s->pool, sizeof(struct adata) + sizeof(ps));
ad->length = sizeof(ps);
memcpy(ad->data, &ps, sizeof(ps));
ea->attrs[0].u.ptr = ad;
}
if (a[RTA_FLOW])

View File

@ -132,7 +132,7 @@ times_init(struct timeloop *loop)
if (rv < 0)
die("Monotonic clock is missing");
if ((ts.tv_sec < 0) || (((s64) ts.tv_sec) > ((s64) 1 << 40)))
if ((ts.tv_sec < 0) || (((u64) ts.tv_sec) > ((u64) 1 << 40)))
log(L_WARN "Monotonic clock is crazy");
loop->last_time = ts.tv_sec S + ts.tv_nsec NS;

View File

@ -562,7 +562,7 @@ static struct rte *
krt_export_net(struct krt_proto *p, net *net, rte **rt_free)
{
struct channel *c = p->p.main_channel;
struct filter *filter = c->out_filter;
const struct filter *filter = c->out_filter;
rte *rt;
if (c->ra_mode == RA_MERGED)

View File

@ -36,6 +36,7 @@
#include "nest/locks.h"
#include "conf/conf.h"
#include "filter/filter.h"
#include "filter/data.h"
#include "unix.h"
#include "krt.h"
@ -93,11 +94,9 @@ drop_gid(gid_t gid)
static inline void
add_num_const(char *name, int val)
{
struct symbol *s = cf_get_symbol(name);
s->class = SYM_CONSTANT | T_INT;
s->def = cfg_allocz(sizeof(struct f_val));
SYM_TYPE(s) = T_INT;
SYM_VAL(s).i = val;
struct f_val *v = cfg_alloc(sizeof(struct f_val));
*v = (struct f_val) { .type = T_INT, .val.i = val };
cf_define_symbol(cf_get_symbol(name), SYM_CONSTANT | T_INT, val, v);
}
/* the code of read_iproute_table() is based on

View File

@ -31,6 +31,7 @@
static const char *request;
static int list_tests;
static int do_core;
static int do_die;
static int no_fork;
static int no_timeout;
static int is_terminal; /* Whether stdout is a live terminal or pipe redirect */
@ -61,7 +62,7 @@ bt_init(int argc, char *argv[])
bt_test_id = NULL;
is_terminal = isatty(fileno(stdout));
while ((c = getopt(argc, argv, "lcftv")) >= 0)
while ((c = getopt(argc, argv, "lcdftv")) >= 0)
switch (c)
{
case 'l':
@ -72,6 +73,10 @@ bt_init(int argc, char *argv[])
do_core = 1;
break;
case 'd':
do_die = 1;
break;
case 'f':
no_fork = 1;
break;
@ -105,10 +110,11 @@ bt_init(int argc, char *argv[])
return;
usage:
printf("Usage: %s [-l] [-c] [-f] [-t] [-vvv] [<test_suit_name>]\n", argv[0]);
printf("Usage: %s [-l] [-c] [-d] [-f] [-t] [-vvv] [<test_suit_name>]\n", argv[0]);
printf("Options: \n");
printf(" -l List all test suite names and descriptions \n");
printf(" -c Force unlimit core dumps (needs root privileges) \n");
printf(" -d Die on first failed test case \n");
printf(" -f No forking \n");
printf(" -t No timeout limit \n");
printf(" -v More verbosity, maximum is 3 -vvv \n");
@ -217,6 +223,9 @@ bt_log_result(int result, const char *fmt, va_list argptr)
result_str = is_terminal ? BT_PROMPT_FAIL : BT_PROMPT_FAIL_NO_COLOR;
printf("%s\n", result_str);
if (do_die && !result)
abort();
}
/**