]> git.ipfire.org Git - thirdparty/bird.git/blame - filter/f-inst.h
Filter: Some people can't pronounce "postfixify" correctly. Let's try "linearize...
[thirdparty/bird.git] / filter / f-inst.h
CommitLineData
9b46748d
MM
1/*
2 * BIRD Internet Routing Daemon -- Filter instructions
3 *
8bdb05ed 4 * (c) 1999 Pavel Machek <pavel@ucw.cz>
9b46748d
MM
5 * (c) 2018--2019 Maria Matejka <mq@jmq.cz>
6 *
7 * Can be freely distributed and used under the terms of the GNU GPL.
e1ac6f1e
MM
8 *
9 * Filter interpreter data structures and internal API.
10 * The filter code goes through several phases:
11 *
12 * 1 Parsing
13 * Flex- and Bison-generated parser decodes the human-readable data into
14 * a struct f_inst tree. This is an infix tree that was interpreted by
15 * depth-first search execution in previous versions of the interpreter.
16 * All instructions have their constructor: f_new_inst(FI_code, ...)
17 * translates into f_new_inst_FI_code(...) and the types are checked in
18 * compile time.
19 *
23e3b1e6 20 * 2 Linearize before interpreting
e1ac6f1e
MM
21 * The infix tree is always interpreted in the same order. Therefore we
22 * sort the instructions one after another into struct f_line. Results
23 * and arguments of these instructions are implicitly put on a value
24 * stack; e.g. the + operation just takes two arguments from the value
25 * stack and puts the result on there.
26 *
27 * 3 Interpret
28 * The given line is put on a custom execution stack. If needed (FI_CALL,
29 * FI_SWITCH, FI_AND, FI_OR, FI_CONDITION, ...), another line is put on top
30 * of the stack; when that line finishes, the execution continues on the
31 * older lines on the stack where it stopped before.
32 *
33 * 4 Same
34 * On config reload, the filters have to be compared whether channel
35 * reload is needed or not. The comparison is done by comparing the
36 * struct f_line's recursively.
37 *
38 * The main purpose of this rework was to improve filter performance
39 * by making the interpreter non-recursive.
40 *
41 * The other outcome is concentration of instruction definitions to
42 * one place -- filter/f-inst.c
9b46748d
MM
43 */
44
8bdb05ed
MM
45#ifndef _BIRD_F_INST_H_
46#define _BIRD_F_INST_H_
47
4f082dfa
MM
48#include "nest/bird.h"
49#include "conf/conf.h"
8bdb05ed 50#include "filter/filter.h"
4f082dfa 51#include "filter/data.h"
8bdb05ed 52
ea4f55e3
MM
53/* Flags for instructions */
54enum f_instruction_flags {
55 FIF_PRINTED = 1, /* FI_PRINT_AND_DIE: message put in buffer */
56} PACKED;
57
4f082dfa 58/* Include generated filter instruction declarations */
87bd7cd7 59#include "filter/inst-gen.h"
8bdb05ed 60
4f082dfa 61#define f_new_inst(...) MACRO_CONCAT_AFTER(f_new_inst_, MACRO_FIRST(__VA_ARGS__))(__VA_ARGS__)
9b46748d
MM
62
63/* Convert the instruction back to the enum name */
64const char *f_instruction_name(enum f_instruction_code fi);
65
8bdb05ed 66/* Filter structures for execution */
8bdb05ed
MM
67/* Line of instructions to be unconditionally executed one after another */
68struct f_line {
69 uint len; /* Line length */
f249d0b8 70 u8 args; /* Function: Args required */
96d757c1 71 u8 vars;
8bdb05ed
MM
72 struct f_line_item items[0]; /* The items themselves */
73};
9b46748d 74
8bdb05ed 75/* Convert the f_inst infix tree to the f_line structures */
23e3b1e6
JMM
76struct f_line *f_linearize_concat(const struct f_inst * const inst[], uint count);
77static inline struct f_line *f_linearize(const struct f_inst *root)
78{ return f_linearize_concat(&root, 1); }
8bdb05ed 79
ea4f55e3
MM
80void f_dump_line(const struct f_line *, uint indent);
81
8bdb05ed
MM
82struct filter *f_new_where(const struct f_inst *);
83static inline struct f_dynamic_attr f_new_dynamic_attr(u8 type, u8 bit, enum f_type f_type, uint code) /* Type as core knows it, type as filters know it, and code of dynamic attribute */
84{ return (struct f_dynamic_attr) { .type = type, .bit = bit, .f_type = f_type, .ea_code = code }; } /* f_type currently unused; will be handy for static type checking */
85static inline struct f_static_attr f_new_static_attr(int f_type, int code, int readonly)
86{ return (struct f_static_attr) { .f_type = f_type, .sa_code = code, .readonly = readonly }; }
87struct f_inst *f_generate_complex(enum f_instruction_code fi_code, struct f_dynamic_attr da, struct f_inst *argument);
88struct f_inst *f_generate_roa_check(struct rtable_config *table, struct f_inst *prefix, struct f_inst *asn);
89
90/* Hook for call bt_assert() function in configuration */
91extern void (*bt_assert_hook)(int result, const struct f_line_item *assert);
92
93/* Bird Tests */
94struct f_bt_test_suite {
95 node n; /* Node in config->tests */
0b39b1cb
MM
96 const struct f_line *fn; /* Root of function */
97 const struct f_line *cmp; /* Compare to this function */
8bdb05ed
MM
98 const char *fn_name; /* Name of test */
99 const char *dsc; /* Description */
132529ce 100 int result; /* Desired result */
9b46748d
MM
101};
102
8bdb05ed 103#endif