aboutsummaryrefslogtreecommitdiffstats
path: root/vlib/example/plex_test.c
blob: ce0c8ef1141544a793f6442bf684972de1fc9488 (plain)
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/*
 * Copyright (c) 2015 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <vlib/parse.h>
#include <vlib/unix/unix.h>

static u8 *
format_value_v4_address (u8 * s, va_list * args)
{
  vlib_parse_value_t *v = va_arg (*args, vlib_parse_value_t *);
  u32 a = v->value.as_uword;

  s = format (s, "%d.%d.%d.%d",
	      (a >> 24) & 0xFF,
	      (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a >> 0) & 0xFF);

  return s;
}

static vlib_parse_match_t
v4_address_match (vlib_parse_main_t * pm, vlib_parse_type_t * type,
		  vlib_lex_token_t * t, vlib_parse_value_t * valuep)
{
  u32 digit;
  u32 value = 0;
  int i;

  if (vec_len (pm->tokens) - (t - pm->tokens) < 7)
    return VLIB_PARSE_MATCH_FAIL;

  /* NUMBER DOT NUMBER DOT NUMBER DOT NUMBER */

  for (i = 0; i < 7; i++)
    {
      if ((i & 1) == 0)
	{
	  if (t[i].token != VLIB_LEX_number)
	    return VLIB_PARSE_MATCH_FAIL;
	  if (t[i].value.as_uword > 0xff)
	    return VLIB_PARSE_MATCH_FAIL;
	  digit = t[i].value.as_uword;
	  value = (value << 8) | digit;
	}
      else
	{
	  if (t[i].token != VLIB_LEX_dot)
	    return VLIB_PARSE_MATCH_FAIL;
	}
    }
  /* note: caller advances by 1 */
  pm->current_token_index += 6;
  valuep->value.as_uword = value;
  return VLIB_PARSE_MATCH_VALUE;
}

PARSE_TYPE_INIT (v4_address, v4_address_match, 0, format_value_v4_address)
     static u8 *format_value_v4_address_and_mask (u8 * s, va_list * args)
{
  vlib_parse_value_t *v = va_arg (*args, vlib_parse_value_t *);
  u32 *a = v->value.as_pointer;

  s = format (s, "%d.%d.%d.%d",
	      (a[0] >> 24) & 0xFF,
	      (a[0] >> 16) & 0xFF, (a[0] >> 8) & 0xFF, (a[0] >> 0) & 0xFF);
  s = format (s, "/%d", a[1]);

  return s;
}

static vlib_parse_match_t
v4_address_and_mask_match (vlib_parse_main_t * pm, vlib_parse_type_t * type,
			   vlib_lex_token_t * t, vlib_parse_value_t * valuep)
{
  u32 digit;
  u32 address = 0;
  u32 *rv = 0;
  int i;

  if (vec_len (pm->tokens) - (t - pm->tokens) < 9)
    return VLIB_PARSE_MATCH_FAIL;

  /* NUMBER DOT NUMBER DOT NUMBER DOT NUMBER */

  for (i = 0; i < 7; i++)
    {
      if ((i & 1) == 0)
	{
	  if (t[i].token != VLIB_LEX_number)
	    return VLIB_PARSE_MATCH_FAIL;
	  if (t[i].value.as_uword > 0xff)
	    return VLIB_PARSE_MATCH_FAIL;
	  digit = t[i].value.as_uword;
	  address = (address << 8) | digit;
	}
      else
	{
	  if (t[i].token != VLIB_LEX_dot)
	    return VLIB_PARSE_MATCH_FAIL;
	}
    }

  if (t[7].token != VLIB_LEX_slash || t[8].token != VLIB_LEX_number)
    return VLIB_PARSE_MATCH_FAIL;

  vec_add1 (rv, address);
  vec_add1 (rv, t[8].value.as_uword);

  /* note: caller advances by 1 */
  pm->current_token_index += 8;
  valuep->value.as_pointer = rv;
  return VLIB_PARSE_MATCH_VALUE;
}

void
v4_address_and_mask_cleanup (vlib_parse_value_t * valuep)
{
  u32 *trash = valuep->value.as_pointer;
  vec_free (trash);
}

PARSE_TYPE_INIT (v4_address_and_mask, v4_address_and_mask_match,
		 v4_address_and_mask_cleanup,
		 format_value_v4_address_and_mask)
     vlib_lex_main_t vlib_lex_main;



     vlib_parse_match_t eval_factor0 (vlib_parse_main_t * pm,
				      vlib_parse_item_t * item,
				      vlib_parse_value_t * value)
{
  clib_warning ("%U", format_vlib_parse_value, pm);
  return VLIB_PARSE_MATCH_RULE;
}

vlib_parse_match_t
eval_factor1 (vlib_parse_main_t * pm,
	      vlib_parse_item_t * item, vlib_parse_value_t * value)
{
  clib_warning ("%U", format_vlib_parse_value, pm);
  return VLIB_PARSE_MATCH_RULE;
}

vlib_parse_match_t
eval_factor2 (vlib_parse_main_t * pm,
	      vlib_parse_item_t * item, vlib_parse_value_t * value)
{
  word a;
  int index = vec_len (pm->parse_value) - 1;

  a = pm->parse_value[index].value.as_word;

  pm->parse_value[index].value.as_word = -a;
  return VLIB_PARSE_MATCH_RULE;
}

vlib_parse_match_t
eval_term0 (vlib_parse_main_t * pm,
	    vlib_parse_item_t * item, vlib_parse_value_t * value)
{
  clib_warning ("%U", format_vlib_parse_value, pm);
  return VLIB_PARSE_MATCH_RULE;
}

vlib_parse_match_t
eval_term1 (vlib_parse_main_t * pm,
	    vlib_parse_item_t * item, vlib_parse_value_t * value)
{
  uword a, b;
  int index = vec_len (pm->parse_value) - 2;

  a = pm->parse_value[index].value.as_uword;
  b = pm->parse_value[index + 1].value.as_uword;

  pm->parse_value[index].value.as_uword = a * b;
  _vec_len (pm->parse_value) -= 1;
  clib_warning ("%U", format_vlib_parse_value, pm);

  return VLIB_PARSE_MATCH_RULE;
}

vlib_parse_match_t
eval_term2 (vlib_parse_main_t * pm,
	    vlib_parse_item_t * item, vlib_parse_value_t * value)
{
  uword a, b;
  int index = vec_len (pm->parse_value) - 2;

  a = pm->parse_value[index].value.as_uword;
  b = pm->parse_value[index + 1].value.as_uword;

  pm->parse_value[index].value.as_uword = a / b;
  _vec_len (pm->parse_value) -= 1;
  clib_warning ("%U", format_vlib_parse_value, pm);

  return VLIB_PARSE_MATCH_RULE;
}

vlib_parse_match_t
eval_exp0 (vlib_parse_main_t * pm,
	   vlib_parse_item_t * item, vlib_parse_value_t * value)
{
  return VLIB_PARSE_MATCH_RULE;
}

vlib_parse_match_t
eval_exp1 (vlib_parse_main_t * pm,
	   vlib_parse_item_t * item, vlib_parse_value_t * value)
{
  uword a, b;
  int index = vec_len (pm->parse_value) - 2;

  a = pm->parse_value[index].value.as_uword;
  b = pm->parse_value[index + 1].value.as_uword;

  pm->parse_value[index].value.as_uword = a + b;
  _vec_len (pm->parse_value) -= 1;
  clib_warning ("%U", format_vlib_parse_value, pm);

  return VLIB_PARSE_MATCH_RULE;
}

vlib_parse_match_t
eval_exp2 (vlib_parse_main_t * pm,
	   vlib_parse_item_t * item, vlib_parse_value_t * value)
{
  uword a, b;
  int index = vec_len (pm->parse_value) - 2;

  a = pm->parse_value[index].value.as_uword;
  b = pm->parse_value[index + 1].value.as_uword;

  pm->parse_value[index].value.as_uword = a - b;
  _vec_len (pm->parse_value) -= 1;
  clib_warning ("%U", format_vlib_parse_value, pm);

  return VLIB_PARSE_MATCH_RULE;
}

vlib_parse_match_t
eval_result (vlib_parse_main_t * pm,
	     vlib_parse_item_t * item, vlib_parse_value_t * value)
{
  clib_warning ("%U", format_vlib_parse_value, pm);
  return VLIB_PARSE_MATCH_DONE;
}

vlib_parse_match_t
noop_match_rule (vlib_parse_main_t * pm,
		 vlib_parse_item_t * item, vlib_parse_value_t * value)
{
  clib_warning ("%U", format_vlib_parse_value, pm);
  return VLIB_PARSE_MATCH_RULE;
}

#if 0
PARSE_INIT (t1, "moo", eval0);
PARSE_INIT (t2, "moo cow mumble", eval1);
PARSE_INIT (t3, "moo cow", eval2);
PARSE_INIT (t4, "moo cow mumble grunch", eval3);
#endif

#if 0
PARSE_INIT (r1, "eval <exp>", eval_result);

PARSE_INIT (r2, "<exp> = <term><exp2>", eval_exp0);
PARSE_INIT (r3, "<exp2> = <plus> <exp>", eval_exp1);
PARSE_INIT (r4, "<exp2> = <minus> <exp>", eval_exp2);
PARSE_INIT (r5, "<exp2> = ", noop_match_rule);
PARSE_TYPE_INIT (exp, rule_match, 0, 0);
PARSE_TYPE_INIT (exp2, rule_match, 0, 0);

PARSE_INIT (r6, "<term> = <factor><term2>", eval_term0);
PARSE_INIT (r7, "<term2> = <star> <term>", eval_term1);
PARSE_INIT (r8, "<term2> = <slash> <term>", eval_term2);
PARSE_INIT (r9, "<term2> = ", noop_match_rule);
PARSE_TYPE_INIT (term, rule_match, 0, 0);
PARSE_TYPE_INIT (term2, rule_match, 0, 0);

PARSE_INIT (r11, "<factor> = <lpar> <exp> <rpar>", eval_factor1);
PARSE_INIT (r10, "<factor> = <number>", eval_factor0);
PARSE_INIT (r12, "<factor> = <minus> <factor>", eval_factor2);

PARSE_TYPE_INIT (factor, rule_match, 0, 0);
#endif

PARSE_INIT (r1, "eval <exp>", eval_result);

#if 1
PARSE_INIT (r2, "<exp> = <term><exp2>", eval_exp0);
PARSE_INIT (r3, "<exp2> = <plus> <exp>", eval_exp1);
PARSE_INIT (r4, "<exp2> = <minus> <exp>", eval_exp2);
PARSE_INIT (r5, "<exp2> = ", noop_match_rule);
PARSE_TYPE_INIT (exp, rule_match, 0, 0);
PARSE_TYPE_INIT (exp2, rule_match, 0, 0);

PARSE_INIT (r6, "<term> = <factor><term2>", eval_term0);
PARSE_INIT (r7, "<term2> = <star> <term>", eval_term1);
PARSE_INIT (r8, "<term2> = <slash> <term>", eval_term2);
PARSE_INIT (r9, "<term2> = ", noop_match_rule);
PARSE_TYPE_INIT (term, rule_match, 0, 0);
PARSE_TYPE_INIT (term2, rule_match, 0, 0);

PARSE_INIT (r11, "<factor> = <lpar> <exp> <rpar>", eval_factor1);
PARSE_INIT (r10, "<factor> = <number>", eval_factor0);
PARSE_INIT (r12, "<factor> = <minus> <factor>", eval_factor2);

PARSE_TYPE_INIT (factor, rule_match, 0, 0);
#endif

#if 0
PARSE_TYPE_INIT (exp, rule_match, 0, 0);
PARSE_INIT (r6, "<exp> = a b", eval_term0);
PARSE_INIT (r7, "<exp> = c d", eval_term1);
PARSE_INIT (r9, "<exp> = ", noop_match_rule);
#endif

#if 0
#define foreach_rule_evaluator                  \
_(0)                                            \
_(1)                                            \
_(2)						\
_(3)

#define _(n)                                            \
vlib_parse_match_t eval##n (vlib_parse_main_t *pm,      \
                            vlib_parse_item_t *item,    \
                            vlib_parse_value_t *value)  \
{                                                       \
  clib_warning ("%U", format_vlib_parse_value, pm);     \
  return VLIB_PARSE_MATCH_DONE;                         \
}
foreach_rule_evaluator
#undef _
PARSE_INIT (r1, "eval <moo>", eval_result);

PARSE_INIT (r2, "<moo> = cow", eval0);
PARSE_INIT (r4, "<moo> = ", eval1);
PARSE_TYPE_INIT (moo, rule_match, 0, 0);
#endif


clib_error_t *
test_init (vlib_main_t * vm)
{
  clib_error_t *error;

  if ((error = vlib_call_init_function (vm, parse_init)))
    return error;

  return 0;
}

VLIB_INIT_FUNCTION (test_init);

clib_error_t *
vlib_stdlex_init (vlib_main_t * vm)
{
  vlib_lex_main_t *lm = &vlib_lex_main;
  u16 top_index;
  u16 slash_index, slash_star_index, slash_slash_index, slash_star_star_index;
  u16 slash_token;
  u16 word_index;
  u16 zero_index, octal_index, decimal_index, hex_index, binary_index;

  top_index = vlib_lex_add_table ("top");

#define foreach_top_level_single_character_token        \
  _('(', lpar)                                          \
  _(')', rpar)                                          \
  _(';', semi)                                          \
  _('[', lbrack)                                        \
  _(']', rbrack)                                        \
  _('{', lcurly)                                        \
  _('}', rcurly)                                        \
  _('+', plus)                                          \
  _('-', minus)                                         \
  _('*', star)                                          \
  _('%', percent)                                       \
  _('@', atsign)                                        \
  _(',', comma)                                         \
  _('.', dot)                                           \
  _('?', qmark)

#define _(c,t) \
  vlib_lex_set_action_range(top_index,c,c,VLIB_LEX_RETURN,vlib_lex_add_token(lm, #t), top_index);
  foreach_top_level_single_character_token;
#undef _

  /* Numbers */
  zero_index = vlib_lex_add_table ("zero");
  octal_index = vlib_lex_add_table ("octal");
  decimal_index = vlib_lex_add_table ("decimal");
  hex_index = vlib_lex_add_table ("hex");
  binary_index = vlib_lex_add_table ("binary");

  /* Support 0x 0b 0t and 0123 [octal] */
  vlib_lex_set_action_range (top_index, '0', '0', VLIB_LEX_START_NUMBER, 10,
			     zero_index);
  vlib_lex_set_action_range (top_index, '1', '9', VLIB_LEX_START_NUMBER, 10,
			     decimal_index);

  vlib_lex_set_action_range (zero_index, 0, 0x7F, VLIB_LEX_RETURN_AND_RESCAN,
			     VLIB_LEX_number, top_index);

  vlib_lex_set_action_range (zero_index, 'x', 'x', VLIB_LEX_IGNORE, ~0,
			     hex_index);
  vlib_lex_set_action_range (zero_index, 'b', 'b', VLIB_LEX_IGNORE, ~0,
			     binary_index);
  vlib_lex_set_action_range (zero_index, 't', 't', VLIB_LEX_IGNORE, ~0,
			     decimal_index);
  vlib_lex_set_action_range (zero_index, '0', '7', VLIB_LEX_START_NUMBER, 8,
			     octal_index);

  /* Octal */
  vlib_lex_set_action_range (octal_index, 0, 0x7f, VLIB_LEX_RETURN_AND_RESCAN,
			     VLIB_LEX_number, top_index);
  vlib_lex_set_action_range (octal_index, '0', '7', VLIB_LEX_ADD_TO_NUMBER, 8,
			     octal_index);

  /* Decimal */
  vlib_lex_set_action_range (decimal_index, 0, 0x7f,
			     VLIB_LEX_RETURN_AND_RESCAN, VLIB_LEX_number,
			     top_index);
  vlib_lex_set_action_range (decimal_index, '0', '9', VLIB_LEX_ADD_TO_NUMBER,
			     10, decimal_index);

  /* Hex */
  vlib_lex_set_action_range (hex_index, 0, 0x7f, VLIB_LEX_RETURN_AND_RESCAN,
			     VLIB_LEX_number, top_index);
  vlib_lex_set_action_range (hex_index, '0', '9', VLIB_LEX_ADD_TO_NUMBER, 16,
			     hex_index);
  vlib_lex_set_action_range (hex_index, 'a', 'f', VLIB_LEX_ADD_TO_NUMBER, 16,
			     hex_index);
  vlib_lex_set_action_range (hex_index, 'A', 'F', VLIB_LEX_ADD_TO_NUMBER, 16,
			     hex_index);

  /* Binary */
  vlib_lex_set_action_range (binary_index, 0, 0x7f,
			     VLIB_LEX_RETURN_AND_RESCAN, VLIB_LEX_number,
			     top_index);
  vlib_lex_set_action_range (binary_index, '0', '1', VLIB_LEX_ADD_TO_NUMBER,
			     2, binary_index);

  /* c/c++ comment syntax is the worst... */

  slash_index = vlib_lex_add_table ("slash");
  slash_star_index = vlib_lex_add_table ("slash_star");
  slash_star_star_index = vlib_lex_add_table ("slash_star_star");
  slash_slash_index = vlib_lex_add_table ("slash_slash");
  slash_token = vlib_lex_add_token (lm, "slash");

  /* Top level: see a slash, ignore, go to slash table */
  vlib_lex_set_action_range (top_index, '/', '/', VLIB_LEX_IGNORE, ~0,
			     slash_index);

  /* default for slash table: return SLASH, go to top table */
  vlib_lex_set_action_range (slash_index, 1, 0x7F, VLIB_LEX_RETURN_AND_RESCAN,
			     slash_token, top_index);
  /* see slash-slash, go to s-s table */
  vlib_lex_set_action_range (slash_index, '/', '/', VLIB_LEX_IGNORE, ~0,
			     slash_slash_index);
  /* see slash-star, go to s-* table */
  vlib_lex_set_action_range (slash_index, '*', '*', VLIB_LEX_IGNORE, ~0,
			     slash_star_index);

  /* EOL in s-s table, ignore, go to top table */
  vlib_lex_set_action_range (slash_slash_index, '\n', '\n', VLIB_LEX_IGNORE,
			     ~0, top_index);

  /* slash-star blah blah star */
  vlib_lex_set_action_range (slash_star_index, '*', '*', VLIB_LEX_IGNORE, ~0,
			     slash_star_star_index);

  /* slash star blah blah star slash */
  vlib_lex_set_action_range (slash_star_star_index, '/', '/', VLIB_LEX_IGNORE,
			     ~0, top_index);

  /* LT, =, GT */
  vlib_lex_set_action_range (top_index, '<', '<', VLIB_LEX_RETURN,
			     VLIB_LEX_lt, top_index);
  vlib_lex_set_action_range (top_index, '=', '=', VLIB_LEX_RETURN,
			     VLIB_LEX_equals, top_index);
  vlib_lex_set_action_range (top_index, '>', '>', VLIB_LEX_RETURN,
			     VLIB_LEX_gt, top_index);

  /* words, key and otherwise */
  word_index = vlib_lex_add_table ("word");

  vlib_lex_set_action_range (top_index, 'a', 'z', VLIB_LEX_ADD_TO_TOKEN, ~0,
			     word_index);
  vlib_lex_set_action_range (top_index, 'A', 'Z', VLIB_LEX_ADD_TO_TOKEN, ~0,
			     word_index);

  vlib_lex_set_action_range (word_index, 0, 0x7f, VLIB_LEX_KEYWORD_CHECK, ~0,
			     top_index);

  vlib_lex_set_action_range (word_index, 'a', 'z', VLIB_LEX_ADD_TO_TOKEN, ~0,
			     word_index);
  vlib_lex_set_action_range (word_index, 'A', 'Z', VLIB_LEX_ADD_TO_TOKEN, ~0,
			     word_index);
  vlib_lex_set_action_range (word_index, '_', '_', VLIB_LEX_ADD_TO_TOKEN, ~0,
			     word_index);
  vlib_lex_set_action_range (word_index, '0', '9', VLIB_LEX_ADD_TO_TOKEN, ~0,
			     word_index);

  return 0;
}

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */