-
Notifications
You must be signed in to change notification settings - Fork 1
/
filter.h
367 lines (293 loc) · 6.23 KB
/
filter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#ifndef filter_h_included
#define filter_h_included
#include <stdint.h>
#include <math.h>
#include "xenoeye.h"
#include "iplist.h"
#include "geoip.h"
#define ERR_MSG_LEN 1024
struct flow_info;
struct ip_addr_and_mask_4
{
uint32_t addr;
uint32_t mask;
};
struct ip_addr_and_mask_6
{
xe_ip addr;
xe_ip mask;
};
struct ip_addr_and_mask
{
int version;
int mask_len;
union ip_addr_and_mask_addr {
struct ip_addr_and_mask_4 v4;
struct ip_addr_and_mask_6 v6;
} ip;
};
struct int_range
{
int low;
int high;
};
enum TOKEN_ID
{
ID,
INT_RANGE,
STRING,
LPAREN,
RPAREN,
OR,
AND,
NOT,
#define FIELD(NAME, STR, TYPE, SRC, DST) \
NAME,
#include "filter.def"
SRC,
DST,
/* aggregable fields */
#define FIELD(NAME, STR, FLD, SCALE) \
NAME,
#include "filter-ag.def"
ASC,
DESC,
/* functions */
DIV,
DIV_R,
DIV_L,
MIN,
MFREQ,
/* geoip */
#define DO(FIELD, SIZE) FIELD,
FOR_LIST_OF_GEOIP_FIELDS
#undef DO
ASN,
ASD,
COMMA
};
struct token
{
enum TOKEN_ID id;
int str_len;
union token_data {
char str[TOKEN_MAX_SIZE];
struct int_range range;
struct ip_addr_and_mask ip;
} data;
};
/* filter_basic */
#define FILTER_BASIC_DIR_NONE 0
#define FILTER_BASIC_DIR_SRC 1
#define FILTER_BASIC_DIR_DST 2
#define FILTER_BASIC_DIR_BOTH (FILTER_BASIC_DIR_SRC | FILTER_BASIC_DIR_DST)
struct filter_basic_data
{
int is_list;
union filter_basic_data_union {
struct int_range range;
struct ip_addr_and_mask ip;
struct iplist *addr_list;
char *str;
} data;
};
enum FILTER_BASIC_TYPE
{
FILTER_BASIC_ADDR4,
FILTER_BASIC_ADDR6,
FILTER_BASIC_RANGE,
FILTER_BASIC_STRING
};
enum FILTER_BASIC_NAME
{
FILTER_BASIC_NAME_NONE,
#define FIELD(NAME, STR, TYPE, SRC, DST) \
FILTER_BASIC_NAME_##NAME,
#include "filter.def"
FILTER_BASIC_NAME_DIV,
FILTER_BASIC_NAME_DIV_R,
FILTER_BASIC_NAME_DIV_L,
FILTER_BASIC_NAME_MIN,
FILTER_BASIC_NAME_MFREQ,
/* geoip */
#define DO(FIELD, SIZE) FILTER_BASIC_NAME_##FIELD,
FOR_LIST_OF_GEOIP_FIELDS
#undef DO
FILTER_BASIC_NAME_ASN,
FILTER_BASIC_NAME_ASD
};
struct function_div
{
/* offsets and sizes in struct flow_info */
unsigned int dividend_off;
unsigned int dividend_size;
unsigned int divisor_off;
unsigned int divisor_size;
int is_log;
int k;
};
struct function_min
{
/* offsets and sizes in struct flow_info */
unsigned int arg1_off;
unsigned int arg1_size;
unsigned int arg2_off;
unsigned int arg2_size;
};
struct function_mfreq
{
/* offsets and sizes in struct flow_info */
unsigned int arg1_off;
unsigned int arg1_size;
unsigned int arg2_off;
unsigned int arg2_size;
_Atomic uint64_t *freqmap;
};
struct function_geoip
{
/* offset and size in struct flow_info */
unsigned int ip_off;
unsigned int ip_size;
int *has_ip;
enum GEOIP_FIELD field;
};
struct function_as
{
/* offset and size in struct flow_info */
unsigned int ip_off;
unsigned int ip_size;
int *has_ip;
int num;
};
struct filter_basic
{
enum FILTER_BASIC_TYPE type;
enum FILTER_BASIC_NAME name;
int direction;
size_t n;
struct filter_basic_data *data;
int is_func;
union filter_func_data {
struct function_div *div;
struct function_min *min;
struct function_mfreq *mfreq;
struct function_geoip *geoip;
struct function_as *as;
} func_data;
};
enum FILTER_OP
{
FILTER_OP_BASIC,
FILTER_OP_NOT,
FILTER_OP_AND,
FILTER_OP_OR
};
struct filter_op
{
enum FILTER_OP op;
struct filter_basic *arg;
};
struct filter_expr
{
size_t n;
struct filter_op *filter;
};
struct filter_input
{
char *s;
int end;
int line, col;
int error;
char errmsg[ERR_MSG_LEN];
struct token current_token;
};
struct field
{
char name[TOKEN_MAX_SIZE], sql_name[TOKEN_MAX_SIZE];
int descending;
enum FILTER_BASIC_TYPE type;
enum TOKEN_ID id;
size_t nf_offset;
int size;
/* aggregable fields */
int aggr;
int scale;
/* functions */
int is_func;
union field_func_data {
struct function_div div;
struct function_min min;
struct function_mfreq mfreq;
struct function_geoip geoip;
struct function_as as;
} func_data;
};
struct filter_expr *parse_filter(struct filter_input *f);
int parse_field(char *s, struct field *f, char *err);
void mkerror(struct filter_input *f, char *msg);
void read_token(struct filter_input *f);
int filter_add_basic_filter(struct filter_expr *e,
enum FILTER_BASIC_TYPE type, enum FILTER_BASIC_NAME name, int dir);
int filter_add_to_basic_filter(struct filter_input *f,
struct filter_expr *e, struct token *tok, enum FILTER_BASIC_TYPE type);
int filter_add_op(struct filter_expr *e, enum FILTER_OP op);
int filter_match(struct filter_expr *expr, struct flow_info *flow);
void filter_dump(struct filter_expr *e, FILE *f);
void filter_free(struct filter_expr *e);
int accept_(struct filter_input *i, enum TOKEN_ID token);
int id(struct filter_input *f, struct filter_expr *e,
enum FILTER_BASIC_TYPE type);
int function_div_parse(struct filter_input *in, struct function_div *div,
enum TOKEN_ID *tok);
int function_div(struct filter_input *in, struct filter_expr *e,
enum TOKEN_ID *tok);
int function_min_parse(struct filter_input *in, struct function_min *min);
int function_min(struct filter_input *in, struct filter_expr *e);
int function_mfreq_parse(struct filter_input *in, struct function_mfreq *mfreq);
int function_mfreq(struct filter_input *in, struct filter_expr *e);
int function_geoip_parse(struct filter_input *in, struct function_geoip *geoip,
enum TOKEN_ID *tok);
int function_geoip(struct filter_input *in, struct filter_expr *e);
int function_as_parse(struct filter_input *in, struct function_as *as,
enum TOKEN_ID *tok);
int function_as(struct filter_input *in, struct filter_expr *e);
static inline uint64_t
get_nf_val(uintptr_t ptr, unsigned int size)
{
uint64_t val;
switch (size) {
case sizeof(uint64_t):
val = be64toh(*(uint64_t *)ptr);
break;
case sizeof(uint32_t):
val = be32toh(*(uint32_t *)ptr);
break;
case sizeof(uint16_t):
val = be16toh(*(uint16_t *)ptr);
break;
default:
val = 0;
break;
}
return val;
}
static inline int
xdiv(uint64_t dividend, uint64_t divisor, int is_log, int k)
{
int quotient;
if (divisor == 0) {
return 0;
}
quotient = dividend / divisor;
if (is_log) {
int lg = log(quotient) / log(k);
quotient = pow(k, lg);
} else {
if (k > 1) {
quotient /= k;
quotient *= k;
}
}
return quotient;
}
#endif