summaryrefslogtreecommitdiff
path: root/filter/f-inst.c
blob: 8ce78a998e63f72d9d00ae4359bbfa1a8be64cd1 (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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
/*
 *	Filters: Instructions themselves
 *
 *	Copyright 1998 Pavel Machek <pavel@ucw.cz>
 *	Copyright 2018 Maria Matejka <mq@jmq.cz>
 *	Copyright 2018 CZ.NIC z.s.p.o.
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 *
 *	The filter code goes through several phases:
 *
 *	1  Parsing
 *	Flex- and Bison-generated parser decodes the human-readable data into
 *	a struct f_inst tree. This is an infix tree that was interpreted by
 *	depth-first search execution in previous versions of the interpreter.
 *	All instructions have their constructor: f_new_inst(FI_EXAMPLE, ...)
 *	translates into f_new_inst_FI_EXAMPLE(...) and the types are checked in
 *	compile time. If the result of the instruction is always the same,
 *	it's reduced to FI_CONSTANT directly in constructor. This phase also
 *	counts how many instructions are underlying in means of f_line_item
 *	fields to know how much we have to allocate in the next phase.
 *
 *	2  Linearize before interpreting
 *	The infix tree is always interpreted in the same order. Therefore we
 *	sort the instructions one after another into struct f_line. Results
 *	and arguments of these instructions are implicitly put on a value
 *	stack; e.g. the + operation just takes two arguments from the value
 *	stack and puts the result on there.
 *
 *	3  Interpret
 *	The given line is put on a custom execution stack. If needed (FI_CALL,
 *	FI_SWITCH, FI_AND, FI_OR, FI_CONDITION, ...), another line is put on top
 *	of the stack; when that line finishes, the execution continues on the
 *	older lines on the stack where it stopped before.
 *
 *	4  Same
 *	On config reload, the filters have to be compared whether channel
 *	reload is needed or not. The comparison is done by comparing the
 *	struct f_line's recursively.
 *
 *	The main purpose of this rework was to improve filter performance
 *	by making the interpreter non-recursive.
 *
 *	The other outcome is concentration of instruction definitions to
 *	one place -- right here. You shall define your instruction only here
 *	and nowhere else.
 *
 *	Beware. This file is interpreted by M4 macros. These macros
 *	may be more stupid than you could imagine. If something strange
 *	happens after changing this file, compare the results before and
 *	after your change (see the Makefile to find out where the results are)
 *	and see what really happened.
 *
 *	This file is not directly a C source code -> it is a generator input
 *	for several C sources; every instruction block gets expanded into many
 *	different places.
 *
 *	All the arguments are processed literally; if you need an argument including comma,
 *	you have to quote it by [[ ... ]]
 *
 *	What is the syntax here?
 *	m4_dnl	INST(FI_NOP, in, out) {			enum value, input args, output args
 *	m4_dnl	  ARG(num, type);			argument, its id (in data fields) and type accessible by v1, v2, v3
 *	m4_dnl	  ARG_ANY(num);				argument with no type check accessible by v1, v2, v3
 *	m4_dnl	  VARARG;				variable-length argument list; accessible by vv(i) and whati->varcount
 *	m4_dnl	  LINE(num, unused);			this argument has to be converted to its own f_line
 *	m4_dnl	  SYMBOL;				symbol handed from config
 *	m4_dnl	  STATIC_ATTR;				static attribute definition
 *	m4_dnl	  DYNAMIC_ATTR;				dynamic attribute definition
 *	m4_dnl	  RTC;					route table config
 *	m4_dnl	  ACCESS_RTE;				this instruction needs route
 *	m4_dnl	  ACCESS_EATTRS;			this instruction needs extended attributes
 *
 *	m4_dnl	  FID_MEMBER(				custom instruction member
 *	m4_dnl	    C type,				for storage in structs
 *	m4_dnl	    name,				how the member is named
 *	m4_dnl	    comparator for same(),		if different, this should be TRUE (CAVEAT)
 *	m4_dnl	    dump format string			debug -> format string for bvsnprintf
 *	m4_dnl	    dump format args			appropriate args
 *	m4_dnl	  )
 *
 *	m4_dnl	  RESULT(type, union-field, value);	putting this on value stack
 *	m4_dnl	  RESULT_VAL(value-struct);		pass the struct f_val directly
 *	m4_dnl	  RESULT_VOID;				return undef
 *	m4_dnl	}
 *
 *	Also note that the { ... } blocks are not respected by M4 at all.
 *	If you get weird unmatched-brace-pair errors, check what it generated and why.
 *	What is really considered as one instruction is not the { ... } block
 *	after m4_dnl INST() but all the code between them.
 *
 *	Other code is just copied into the interpreter part.
 *
 *	If you are satisfied with this, you don't need to read the following
 *	detailed description of what is really done with the instruction definitions.
 *
 *	m4_dnl	Now let's look under the cover. The code between each INST()
 *	m4_dnl	is copied to several places, namely these (numbered by the M4 diversions
 *	m4_dnl	used in filter/decl.m4):
 *
 *	m4_dnl	(102)	struct f_inst *f_new_inst(FI_EXAMPLE [[ put it here ]])
 *	m4_dnl		{
 *	m4_dnl		  ... (common code)
 *	m4_dnl	(103)	  [[ put it here ]]
 *	m4_dnl		  ...
 *	m4_dnl		  if (all arguments are constant)
 *	m4_dnl	(108)	    [[ put it here ]]
 *	m4_dnl		}
 *	m4_dnl	For writing directly to constructor argument list, use FID_NEW_ARGS.
 *	m4_dnl	For computing something in constructor (103), use FID_NEW_BODY.
 *	m4_dnl	For constant pre-interpretation (108), see below at FID_INTERPRET_BODY.
 *
 *	m4_dnl		struct f_inst {
 *	m4_dnl		  ... (common fields)
 *	m4_dnl		  union {
 *	m4_dnl		    struct {
 *	m4_dnl	(101)	      [[ put it here ]]
 *	m4_dnl		    } i_FI_EXAMPLE;
 *	m4_dnl		    ...
 *	m4_dnl		  };
 *	m4_dnl		};
 *	m4_dnl	This structure is returned from constructor.
 *	m4_dnl	For writing directly to this structure, use FID_STRUCT_IN.
 *
 *	m4_dnl		linearize(struct f_line *dest, const struct f_inst *what, uint pos) {
 *	m4_dnl		  ...
 *	m4_dnl		    switch (what->fi_code) {
 *	m4_dnl		      case FI_EXAMPLE:
 *	m4_dnl	(105)		[[ put it here ]]
 *	m4_dnl			break;
 *	m4_dnl		    }
 *	m4_dnl		}
 *	m4_dnl	This is called when translating from struct f_inst to struct f_line_item.
 *	m4_dnl	For accessing your custom instruction data, use following macros:
 *	m4_dnl	  whati	-> for accessing (struct f_inst).i_FI_EXAMPLE
 *	m4_dnl	  item	-> for accessing (struct f_line)[pos].i_FI_EXAMPLE
 *	m4_dnl	For writing directly here, use FID_LINEARIZE_BODY.
 *
 *	m4_dnl	(107)	struct f_line_item {
 *	m4_dnl		  ... (common fields)
 *	m4_dnl		  union {
 *	m4_dnl		    struct {
 *	m4_dnl	(101)	      [[ put it here ]]
 *	m4_dnl		    } i_FI_EXAMPLE;
 *	m4_dnl		    ...
 *	m4_dnl		  };
 *	m4_dnl		};
 *	m4_dnl	The same as FID_STRUCT_IN (101) but for the other structure.
 *	m4_dnl	This structure is returned from the linearizer (105).
 *	m4_dnl	For writing directly to this structure, use FID_LINE_IN.
 *
 *	m4_dnl		f_dump_line_item_FI_EXAMPLE(const struct f_line_item *item, const int indent)
 *	m4_dnl		{
 *	m4_dnl	(104)	  [[ put it here ]]
 *	m4_dnl		}
 *	m4_dnl	This code dumps the instruction on debug. Note that the argument
 *	m4_dnl	is the linearized instruction; if the instruction has arguments,
 *	m4_dnl	their code has already been linearized and their value is taken
 *	m4_dnl	from the value stack.
 *	m4_dnl	For writing directly here, use FID_DUMP_BODY.
 *
 *	m4_dnl		f_same(...)
 *	m4_dnl		{
 *	m4_dnl		  switch (f1_->fi_code) {
 *	m4_dnl		    case FI_EXAMPLE:
 *	m4_dnl	(106)	      [[ put it here ]]
 *	m4_dnl		      break;
 *	m4_dnl		  }
 *	m4_dnl		}
 *	m4_dnl	This code compares the two given instrucions (f1_ and f2_)
 *	m4_dnl	on reconfigure. For accessing your custom instruction data,
 *	m4_dnl	use macros f1 and f2.
 *	m4_dnl	For writing directly here, use FID_SAME_BODY.
 *
 *	m4_dnl		f_add_lines(...)
 *	m4_dnl		{
 *	m4_dnl		  switch (what_->fi_code) {
 *	m4_dnl		    case FI_EXAMPLE:
 *	m4_dnl	(109)	      [[ put it here ]]
 *	m4_dnl		      break;
 *	m4_dnl		  }
 *	m4_dnl		}
 *	m4_dnl	This code adds new filter lines reachable from the instruction
 *	m4_dnl	to the filter iterator line buffer. This is for instructions
 *	m4_dnl  that changes conrol flow, like FI_CONDITION or FI_CALL, most
 *	m4_dnl  instructions do not need to update it. It is used in generic
 *	m4_dnl  filter iteration code (FILTER_ITERATE*). For accessing your
 *	m4_dnl  custom instruction data, use macros f1 and f2. For writing
 *	m4_dnl	directly here, use FID_ITERATE_BODY.
 *
 *	m4_dnl		interpret(...)
 *	m4_dnl		{
 *	m4_dnl		  switch (what->fi_code) {
 *	m4_dnl		    case FI_EXAMPLE:
 *	m4_dnl	(108)	      [[ put it here ]]
 *	m4_dnl		      break;
 *	m4_dnl		  }
 *	m4_dnl		}
 *	m4_dnl	This code executes the instruction. Every pre-defined macro
 *	m4_dnl	resets the output here. For setting it explicitly,
 *	m4_dnl	use FID_INTERPRET_BODY.
 *	m4_dnl	This code is put on two places; one is the interpreter, the other
 *	m4_dnl	is instruction constructor. If you need to distinguish between
 *	m4_dnl	these two, use FID_INTERPRET_EXEC or FID_INTERPRET_NEW respectively.
 *	m4_dnl	To address the difference between interpreter and constructor
 *	m4_dnl	environments, there are several convenience macros defined:
 *	m4_dnl	  runtime()	-> for spitting out runtime error like division by zero
 *	m4_dnl	  RESULT(...)	-> declare result; may overwrite arguments
 *	m4_dnl	  v1, v2, v3	-> positional arguments, may be overwritten by RESULT()
 *	m4_dnl	  falloc(size)	-> allocate memory from the appropriate linpool
 *	m4_dnl	  fpool		-> the current linpool
 *	m4_dnl	  NEVER_CONSTANT-> don't generate pre-interpretation code at all
 *	m4_dnl	  ACCESS_RTE	-> check that route is available, also NEVER_CONSTANT
 *	m4_dnl	  ACCESS_EATTRS	-> pre-cache the eattrs; use only with ACCESS_RTE
 *	m4_dnl	  f_rta_cow(fs)	-> function to call before any change to route should be done
 *
 *	m4_dnl	If you are stymied, see FI_CALL or FI_CONSTANT or just search for
 *	m4_dnl	the mentioned macros in this file to see what is happening there in wild.
 */

/* Binary operators */
  INST(FI_ADD, 2, 1) {
    ARG(1,T_INT);
    ARG(2,T_INT);
    RESULT(T_INT, i, v1.val.i + v2.val.i);
  }
  INST(FI_SUBTRACT, 2, 1) {
    ARG(1,T_INT);
    ARG(2,T_INT);
    RESULT(T_INT, i, v1.val.i - v2.val.i);
  }
  INST(FI_MULTIPLY, 2, 1) {
    ARG(1,T_INT);
    ARG(2,T_INT);
    RESULT(T_INT, i, v1.val.i * v2.val.i);
  }
  INST(FI_DIVIDE, 2, 1) {
    ARG(1,T_INT);
    ARG(2,T_INT);
    if (v2.val.i == 0) runtime( "Mother told me not to divide by 0" );
    RESULT(T_INT, i, v1.val.i / v2.val.i);
  }
  INST(FI_BITOR, 2, 1) {
    ARG(1,T_INT);
    ARG(2,T_INT);
    RESULT(T_INT, i, v1.val.i | v2.val.i);
  }
  INST(FI_BITAND, 2, 1) {
    ARG(1,T_INT);
    ARG(2,T_INT);
    RESULT(T_INT, i, v1.val.i & v2.val.i);
  }
  INST(FI_AND, 1, 1) {
    ARG(1,T_BOOL);
    ARG_TYPE_STATIC(2,T_BOOL);
    RESULT_TYPE(T_BOOL);

    if (v1.val.i)
      LINE(2,0);
    else
      RESULT_VAL(v1);
  }
  INST(FI_OR, 1, 1) {
    ARG(1,T_BOOL);
    ARG_TYPE_STATIC(2,T_BOOL);
    RESULT_TYPE(T_BOOL);

    if (!v1.val.i)
      LINE(2,0);
    else
      RESULT_VAL(v1);
  }

  INST(FI_PAIR_CONSTRUCT, 2, 1) {
    ARG(1,T_INT);
    ARG(2,T_INT);
    uint u1 = v1.val.i;
    uint u2 = v2.val.i;
    if ((u1 > 0xFFFF) || (u2 > 0xFFFF))
      runtime( "Can't operate with value out of bounds in pair constructor" );
    RESULT(T_PAIR, i, (u1 << 16) | u2);
  }

  INST(FI_EC_CONSTRUCT, 2, 1) {
    ARG_ANY(1);
    ARG(2, T_INT);

    FID_MEMBER(enum ec_subtype, ecs, f1->ecs != f2->ecs, "ec subtype %s", ec_subtype_str(item->ecs));

    int ipv4_used;
    u32 key, val;

    if (v1.type == T_INT) {
      ipv4_used = 0; key = v1.val.i;
    }
    else if (v1.type == T_QUAD) {
      ipv4_used = 1; key = v1.val.i;
    }
    /* IP->Quad implicit conversion */
    else if (val_is_ip4(&v1)) {
      ipv4_used = 1; key = ipa_to_u32(v1.val.ip);
    }
    else
      runtime("Argument 1 of EC constructor must be integer or IPv4 address, got 0x%02x", v1.type);

    val = v2.val.i;

    if (ecs == EC_GENERIC)
      RESULT(T_EC, ec, ec_generic(key, val));
    else if (ipv4_used)
      if (val <= 0xFFFF)
	RESULT(T_EC, ec, ec_ip4(ecs, key, val));
      else
	runtime("4-byte value %u can't be used with IP-address key in extended community", val);
    else if (key < 0x10000)
      RESULT(T_EC, ec, ec_as2(ecs, key, val));
    else
      if (val <= 0xFFFF)
	RESULT(T_EC, ec, ec_as4(ecs, key, val));
      else
	runtime("4-byte value %u can't be used with 4-byte ASN in extended community", val);
  }

  INST(FI_LC_CONSTRUCT, 3, 1) {
    ARG(1, T_INT);
    ARG(2, T_INT);
    ARG(3, T_INT);
    RESULT(T_LC, lc, [[(lcomm) { v1.val.i, v2.val.i, v3.val.i }]]);
  }

  INST(FI_PATHMASK_CONSTRUCT, 0, 1) {
    VARARG;

    struct f_path_mask *pm = falloc(sizeof(struct f_path_mask) + whati->varcount * sizeof(struct f_path_mask_item));
    pm->len = whati->varcount;

    for (uint i=0; i<whati->varcount; i++) {
      switch (vv(i).type) {
	case T_PATH_MASK_ITEM:
	  if (vv(i).val.pmi.kind == PM_LOOP)
	  {
	    if (i == 0)
	      runtime("Path mask iterator '+' cannot be first");

	    /* We want PM_LOOP as prefix operator */
	    pm->item[i] = pm->item[i - 1];
	    pm->item[i - 1] = vv(i).val.pmi;
	    break;
	  }

	  pm->item[i] = vv(i).val.pmi;
	  break;

	case T_INT:
	  pm->item[i] = (struct f_path_mask_item) {
	    .asn = vv(i).val.i,
	    .kind = PM_ASN,
	  };
	  break;

	case T_SET:
	  if (vv(i).val.t->from.type != T_INT)
	    runtime("Only integer sets allowed in path mask");

	  pm->item[i] = (struct f_path_mask_item) {
	    .set = vv(i).val.t,
	    .kind = PM_ASN_SET,
	  };
	  break;

	default:
	  runtime( "Error resolving path mask template: value not an integer" );
      }
    }

    RESULT(T_PATH_MASK, path_mask, pm);
  }

/* Relational operators */

  INST(FI_NEQ, 2, 1) {
    ARG_ANY(1);
    ARG_ANY(2);
    RESULT(T_BOOL, i, !val_same(&v1, &v2));
  }

  INST(FI_EQ, 2, 1) {
    ARG_ANY(1);
    ARG_ANY(2);
    RESULT(T_BOOL, i, val_same(&v1, &v2));
  }

  INST(FI_LT, 2, 1) {
    ARG_ANY(1);
    ARG_ANY(2);
    ARG_SAME_TYPE(1, 2);

    int i = val_compare(&v1, &v2);
    if (i == F_CMP_ERROR)
      runtime( "Can't compare values of incompatible types" );
    RESULT(T_BOOL, i, (i == -1));
  }

  INST(FI_LTE, 2, 1) {
    ARG_ANY(1);
    ARG_ANY(2);
    ARG_SAME_TYPE(1, 2);

    int i = val_compare(&v1, &v2);
    if (i == F_CMP_ERROR)
      runtime( "Can't compare values of incompatible types" );
    RESULT(T_BOOL, i, (i != 1));
  }

  INST(FI_NOT, 1, 1) {
    ARG(1,T_BOOL);
    RESULT(T_BOOL, i, !v1.val.i);
  }

  INST(FI_MATCH, 2, 1) {
    ARG_ANY(1);
    ARG_ANY(2);
    int i = val_in_range(&v1, &v2);
    if (i == F_CMP_ERROR)
      runtime( "~ applied on unknown type pair" );
    RESULT(T_BOOL, i, !!i);
  }

  INST(FI_NOT_MATCH, 2, 1) {
    ARG_ANY(1);
    ARG_ANY(2);
    int i = val_in_range(&v1, &v2);
    if (i == F_CMP_ERROR)
      runtime( "!~ applied on unknown type pair" );
    RESULT(T_BOOL, i, !i);
  }

  INST(FI_DEFINED, 1, 1) {
    ARG_ANY(1);
    RESULT(T_BOOL, i, (v1.type != T_VOID) && !undef_value(v1));
  }

  INST(FI_TYPE, 1, 1) {
    ARG_ANY(1); /* There may be more types supporting this operation */
    switch (v1.type)
    {
      case T_NET:
	RESULT(T_ENUM_NETTYPE, i, v1.val.net->type);
	break;
      default:
	runtime( "Can't determine type of this item" );
    }
  }

  INST(FI_IS_V4, 1, 1) {
    ARG(1, T_IP);
    RESULT(T_BOOL, i, ipa_is_ip4(v1.val.ip));
  }

  /* Set to indirect value prepared in v1 */
  INST(FI_VAR_SET, 1, 0) {
    NEVER_CONSTANT;
    ARG_ANY(1);
    SYMBOL;
    ARG_TYPE(1, sym->class & 0xff);

    fstk->vstk[curline.vbase + sym->offset] = v1;
  }

  INST(FI_VAR_GET, 0, 1) {
    SYMBOL;
    NEVER_CONSTANT;
    RESULT_TYPE(sym->class & 0xff);
    RESULT_VAL(fstk->vstk[curline.vbase + sym->offset]);
  }

  INST(FI_CONSTANT, 0, 1) {
    FID_MEMBER(
      struct f_val,
      val,
      [[ !val_same(&(f1->val), &(f2->val)) ]],
      "value %s",
      val_dump(&(item->val))
    );

    RESULT_TYPE(val.type);
    RESULT_VAL(val);
  }

  INST(FI_CONDITION, 1, 0) {
    ARG(1, T_BOOL);
    if (v1.val.i)
      LINE(2,0);
    else
      LINE(3,1);
  }

  INST(FI_PRINT, 0, 0) {
    NEVER_CONSTANT;
    VARARG;

    if (whati->varcount && !(fs->flags & FF_SILENT))
      for (uint i=0; i<whati->varcount; i++)
	val_format(&(vv(i)), &fs->buf);
  }

  INST(FI_FLUSH, 0, 0) {
    NEVER_CONSTANT;
    if (!(fs->flags & FF_SILENT))
      /* After log_commit, the buffer is reset */
      log_commit(*L_INFO, &fs->buf);
  }

  INST(FI_DIE, 0, 0) {
    NEVER_CONSTANT;
    FID_MEMBER(enum filter_return, fret, f1->fret != f2->fret, "%s", filter_return_str(item->fret));

    switch (whati->fret) {
    case F_ACCEPT:	/* Should take care about turning ACCEPT into MODIFY */
    case F_ERROR:
    case F_REJECT:	/* Maybe print complete route along with reason to reject route? */
      return fret;	/* We have to return now, no more processing. */
    default:
      bug( "unknown return type: Can't happen");
    }
  }

  INST(FI_RTA_GET, 0, 1) {
    {
      STATIC_ATTR;
      ACCESS_RTE;
      ACCESS_EATTRS;
      struct rta *rta = (*fs->rte)->attrs;

      switch (sa.sa_code)
      {
      case SA_NET:	RESULT(sa.type, net, (*fs->rte)->net->n.addr); break;
      case SA_PROTO:	RESULT(sa.type, s, (*fs->rte)->src->proto->name); break;
      case SA_DEST:	RESULT(sa.type, i, rta->dest); break;
      default:
	{
	  struct eattr *nh_ea = ea_find(*fs->eattrs, &ea_gen_nexthop);
	  struct nexthop *nh = nh_ea ? &((struct nexthop_adata *) nh_ea->u.ptr)->nh : NULL;

	  switch (sa.sa_code)
	  {
	    case SA_GW:
	      RESULT(sa.type, ip, nh ? nh->gw : IPA_NONE);
	      break;
	    case SA_IFNAME:
	      RESULT(sa.type, s, (nh && nh->iface) ? nh->iface->name : "");
	      break;
	    case SA_IFINDEX:
	      RESULT(sa.type, i, (nh && nh->iface) ? nh->iface->index : 0);
	      break;
	    case SA_WEIGHT:
	      RESULT(sa.type, i, (nh ? nh->weight : 0) + 1);
	      break;
	    case SA_GW_MPLS:
	      RESULT(sa.type, i, (nh && nh->labels) ? nh->label[0] : MPLS_NULL);
	      break;
	    default:
	      bug("Invalid static attribute access (%u/%u)", sa.type, sa.sa_code);
	  }
	}
      }
    }
  }

  INST(FI_RTA_SET, 1, 0) {
    ACCESS_RTE;
    ACCESS_EATTRS;
    ARG_ANY(1);
    STATIC_ATTR;
    ARG_TYPE(1, sa.type);

    f_rta_cow(fs);
    {
      struct rta *rta = (*fs->rte)->attrs;

      if (sa.sa_code == SA_DEST)
	{
	  int i = v1.val.i;
	  if ((i != RTD_BLACKHOLE) && (i != RTD_UNREACHABLE) && (i != RTD_PROHIBIT))
	    runtime( "Destination can be changed only to blackhole, unreachable or prohibit" );

	  rta->dest = i;
	  ea_unset_attr(fs->eattrs, 1, &ea_gen_nexthop);
	}
      else
      {
	union {
	  struct nexthop_adata nha;
	  struct {
	    struct adata ad;
	    struct nexthop nh;
	    u32 label;
	  };
	} nha;

	nha.ad = (struct adata) {
	  .length = sizeof (struct nexthop_adata) - sizeof (struct adata),
	};

	eattr *a = NULL;

	switch (sa.sa_code)
	  {
      case SA_GW:
	{
	  struct eattr *nh_ea = ea_find(*fs->eattrs, &ea_gen_nexthop);

	  ip_addr ip = v1.val.ip;
	  struct iface *ifa = (ipa_is_link_local(ip) && nh_ea) ?
	    ((struct nexthop_adata *) nh_ea->u.ptr)->nh.iface : NULL;
	  
	  neighbor *n = neigh_find((*fs->rte)->src->proto, ip, ifa, 0);
	  if (!n || (n->scope == SCOPE_HOST))
	    runtime( "Invalid gw address" );

	  rta->dest = RTD_UNICAST;
	  nha.nh = (struct nexthop) {
	    .gw = ip,
	    .iface = n->iface,
	  };
	}
	break;

      case SA_IFNAME:
	{
	  struct iface *ifa = if_find_by_name(v1.val.s);
	  if (!ifa)
	    runtime( "Invalid iface name" );

	  rta->dest = RTD_UNICAST;
	  nha.nh = (struct nexthop) {
	    .iface = ifa,
	  };
	}
	break;

      case SA_GW_MPLS:
	{
	  if (v1.val.i >= 0x100000)
	    runtime( "Invalid MPLS label" );

	  struct eattr *nh_ea = ea_find(*fs->eattrs, &ea_gen_nexthop);
	  if (!nh_ea)
	    runtime( "No nexthop to add a MPLS label to" );

	  nha.nh = ((struct nexthop_adata *) nh_ea->u.ptr)->nh;
	  
	  if (v1.val.i != MPLS_NULL)
	  {
	    nha.nh.label[0] = v1.val.i;
	    nha.nh.labels = 1;
	    nha.ad.length = sizeof nha - sizeof (struct adata);
	  }
	  else
	    nha.nh.labels = 0;
	}
	break;

      case SA_WEIGHT:
        {
	  int i = v1.val.i;
	  if (i < 1 || i > 256)
	    runtime( "Setting weight value out of bounds" );
	  if (rta->dest != RTD_UNICAST)
	    runtime( "Setting weight needs regular nexthop " );

	  struct eattr *nh_ea = ea_find(*fs->eattrs, &ea_gen_nexthop);
	  if (!nh_ea)
	    runtime( "No nexthop to set weight on" );

	  struct nexthop_adata *nhax = (struct nexthop_adata *)
	    tmp_copy_adata(&((struct nexthop_adata *) nh_ea->u.ptr)->ad);

	  /* Set weight on all next hops */
	  NEXTHOP_WALK(nh, nhax)
	    nh->weight = i - 1;

	  a = ea_set_attr(fs->eattrs,
	      EA_LITERAL_DIRECT_ADATA(&ea_gen_nexthop, 0, &nhax->ad));
        }
	break;

      default:
	bug("Invalid static attribute access (%u/%u)", sa.type, sa.sa_code);
	  }

	if (!a)
	  a = ea_set_attr(fs->eattrs,
	      EA_LITERAL_DIRECT_ADATA(&ea_gen_nexthop, 0, tmp_copy_adata(&nha.ad)));

	a->originated = 1;
	a->fresh = 1;
      }
    }
  }

  INST(FI_EA_GET, 0, 1) {	/* Access to extended attributes */
    DYNAMIC_ATTR;
    ACCESS_RTE;
    ACCESS_EATTRS;
    RESULT_TYPE(da->type);
    {
      const struct f_val *empty;
      const eattr *e = ea_find(*fs->eattrs, da->id);

      if (e)
      {
	ASSERT_DIE(e->type == da->type);

	switch (e->type) {
	  case T_IP:
	    RESULT_(T_IP, ip, *((const ip_addr *) e->u.ptr->data));
	    break;
	  default:
	    RESULT_VAL([[(struct f_val) {
		.type = e->type,
		.val.bval = e->u,
		}]]);
	}
      }
      else if (empty = f_get_empty(da->type))
	RESULT_VAL(*empty);
      else
	RESULT_VOID;
    }
  }

  INST(FI_EA_SET, 1, 0) {
    ACCESS_RTE;
    ACCESS_EATTRS;
    ARG_ANY(1);
    DYNAMIC_ATTR;
    ARG_TYPE(1, da->type);
    {
      struct eattr *a;

      if (da->type >= EAF_TYPE__MAX)
	bug("Unsupported attribute type");

      f_rta_cow(fs);

      switch (da->type) {
      case T_OPAQUE:
      case T_IFACE:
	runtime( "Setting opaque attribute is not allowed" );
	break;

      case T_IP:
	a = ea_set_attr(fs->eattrs,
	    EA_LITERAL_STORE_ADATA(da, 0, &v1.val.ip, sizeof(ip_addr)));
	break;

      default:
	a = ea_set_attr(fs->eattrs,
	    EA_LITERAL_GENERIC(da->id, da->type, 0, .u = v1.val.bval));
	break;
      }

      a->originated = 1;
      a->fresh = 1;
    }
  }

  INST(FI_EA_UNSET, 0, 0) {
    DYNAMIC_ATTR;
    ACCESS_RTE;
    ACCESS_EATTRS;

    f_rta_cow(fs);
    ea_unset_attr(fs->eattrs, 1, da);
  }

  INST(FI_LENGTH, 1, 1) {	/* Get length of */
    ARG_ANY(1);
    switch(v1.type) {
    case T_NET:    RESULT(T_INT, i, net_pxlen(v1.val.net)); break;
    case T_PATH:   RESULT(T_INT, i, as_path_getlen(v1.val.ad)); break;
    case T_CLIST:  RESULT(T_INT, i, int_set_get_size(v1.val.ad)); break;
    case T_ECLIST: RESULT(T_INT, i, ec_set_get_size(v1.val.ad)); break;
    case T_LCLIST: RESULT(T_INT, i, lc_set_get_size(v1.val.ad)); break;
    default: runtime( "Prefix, path, clist or eclist expected" );
    }
  }

  INST(FI_NET_SRC, 1, 1) { 	/* Get src prefix */
    ARG(1, T_NET);

    net_addr_union *net = (void *) v1.val.net;
    net_addr *src = falloc(sizeof(net_addr_ip6));
    const byte *part;

    switch(v1.val.net->type) {
    case NET_FLOW4:
      part = flow4_get_part(&net->flow4, FLOW_TYPE_SRC_PREFIX);
      if (part)
	net_fill_ip4(src, flow_read_ip4_part(part), flow_read_pxlen(part));
      else
	net_fill_ip4(src, IP4_NONE, 0);
      break;

    case NET_FLOW6:
      part = flow6_get_part(&net->flow6, FLOW_TYPE_SRC_PREFIX);
      if (part)
	net_fill_ip6(src, flow_read_ip6_part(part), flow_read_pxlen(part));
      else
	net_fill_ip6(src, IP6_NONE, 0);
      break;

    case NET_IP6_SADR:
      net_fill_ip6(src, net->ip6_sadr.src_prefix, net->ip6_sadr.src_pxlen);
      break;

    default:
      runtime( "Flow or SADR expected" );
    }

    RESULT(T_NET, net, src);
  }

  INST(FI_NET_DST, 1, 1) { 	/* Get dst prefix */
    ARG(1, T_NET);

    net_addr_union *net = (void *) v1.val.net;
    net_addr *dst = falloc(sizeof(net_addr_ip6));
    const byte *part;

    switch(v1.val.net->type) {
    case NET_FLOW4:
      part = flow4_get_part(&net->flow4, FLOW_TYPE_DST_PREFIX);
      if (part)
	net_fill_ip4(dst, flow_read_ip4_part(part), flow_read_pxlen(part));
      else
	net_fill_ip4(dst, IP4_NONE, 0);
      break;

    case NET_FLOW6:
      part = flow6_get_part(&net->flow6, FLOW_TYPE_DST_PREFIX);
      if (part)
	net_fill_ip6(dst, flow_read_ip6_part(part), flow_read_pxlen(part));
      else
	net_fill_ip6(dst, IP6_NONE, 0);
      break;

    case NET_IP6_SADR:
      net_fill_ip6(dst, net->ip6_sadr.dst_prefix, net->ip6_sadr.dst_pxlen);
      break;

    default:
      runtime( "Flow or SADR expected" );
    }

    RESULT(T_NET, net, dst);
  }

  INST(FI_ROA_MAXLEN, 1, 1) { 	/* Get ROA max prefix length */
    ARG(1, T_NET);
    if (!net_is_roa(v1.val.net))
      runtime( "ROA expected" );

    RESULT(T_INT, i, (v1.val.net->type == NET_ROA4) ?
      ((net_addr_roa4 *) v1.val.net)->max_pxlen :
      ((net_addr_roa6 *) v1.val.net)->max_pxlen);
  }

  INST(FI_ASN, 1, 1) { 	/* Get ROA ASN or community ASN part */
    ARG_ANY(1);
    RESULT_TYPE(T_INT);
    switch(v1.type)
    {
      case T_NET:
        if (!net_is_roa(v1.val.net))
          runtime( "ROA expected" );

        RESULT_(T_INT, i, (v1.val.net->type == NET_ROA4) ?
          ((net_addr_roa4 *) v1.val.net)->asn :
          ((net_addr_roa6 *) v1.val.net)->asn);
        break;

      case T_PAIR:
        RESULT_(T_INT, i, v1.val.i >> 16);
        break;

      case T_LC:
        RESULT_(T_INT, i, v1.val.lc.asn);
        break;

      default:
        runtime( "Net, pair or lc expected" );
    }
  }

  INST(FI_IP, 1, 1) {	/* Convert prefix to ... */
    ARG(1, T_NET);
    RESULT(T_IP, ip, net_prefix(v1.val.net));
  }

  INST(FI_ROUTE_DISTINGUISHER, 1, 1) {
    ARG(1, T_NET);
    if (!net_is_vpn(v1.val.net))
      runtime( "VPN address expected" );
    RESULT(T_RD, ec, net_rd(v1.val.net));
  }

  INST(FI_AS_PATH_FIRST, 1, 1) {	/* Get first ASN from AS PATH */
    ARG(1, T_PATH);
    u32 as = 0;
    as_path_get_first(v1.val.ad, &as);
    RESULT(T_INT, i, as);
  }

  INST(FI_AS_PATH_LAST, 1, 1) {		/* Get last ASN from AS PATH */
    ARG(1, T_PATH);
    u32 as = 0;
    as_path_get_last(v1.val.ad, &as);
    RESULT(T_INT, i, as);
  }

  INST(FI_AS_PATH_LAST_NAG, 1, 1) {	/* Get last ASN from non-aggregated part of AS PATH */
    ARG(1, T_PATH);
    RESULT(T_INT, i, as_path_get_last_nonaggregated(v1.val.ad));
  }

  INST(FI_PAIR_DATA, 1, 1) {	/* Get data part from the standard community */
    ARG(1, T_PAIR);
    RESULT(T_INT, i, v1.val.i & 0xFFFF);
  }

  INST(FI_LC_DATA1, 1, 1) {	/* Get data1 part from the large community */
    ARG(1, T_LC);
    RESULT(T_INT, i, v1.val.lc.ldp1);
  }

  INST(FI_LC_DATA2, 1, 1) {	/* Get data2 part from the large community */
    ARG(1, T_LC);
    RESULT(T_INT, i, v1.val.lc.ldp2);
  }

  INST(FI_MIN, 1, 1) {	/* Get minimum element from set */
    ARG_ANY(1);
    RESULT_TYPE(f_type_element_type(v1.type));
    switch(v1.type)
    {
      case T_CLIST:
        {
          u32 val = 0;
          int_set_min(v1.val.ad, &val);
          RESULT_(T_PAIR, i, val);
        }
        break;

      case T_ECLIST:
        {
          u64 val = 0;
          ec_set_min(v1.val.ad, &val);
          RESULT_(T_EC, ec, val);
        }
        break;

      case T_LCLIST:
        {
          lcomm val = { 0, 0, 0 };
          lc_set_min(v1.val.ad, &val);
          RESULT_(T_LC, lc, val);
        }
        break;

      default:
        runtime( "Clist or lclist expected" );
    }
  }

  INST(FI_MAX, 1, 1) {	/* Get maximum element from set */
    ARG_ANY(1);
    RESULT_TYPE(f_type_element_type(v1.type));
    switch(v1.type)
    {
      case T_CLIST:
        {
          u32 val = 0;
          int_set_max(v1.val.ad, &val);
          RESULT_(T_PAIR, i, val);
        }
        break;

      case T_ECLIST:
        {
          u64 val = 0;
          ec_set_max(v1.val.ad, &val);
          RESULT_(T_EC, ec, val);
        }
        break;

      case T_LCLIST:
        {
          lcomm val = { 0, 0, 0 };
          lc_set_max(v1.val.ad, &val);
          RESULT_(T_LC, lc, val);
        }
        break;

      default:
        runtime( "Clist or lclist expected" );
    }
  }

  INST(FI_RETURN, 1, 1) {
    NEVER_CONSTANT;
    /* Acquire the return value */
    ARG_ANY(1);
    uint retpos = fstk->vcnt;

    /* Drop every sub-block including ourselves */
    do fstk->ecnt--;
    while ((fstk->ecnt > 0) && !(fstk->estk[fstk->ecnt].emask & FE_RETURN));

    /* Now we are at the caller frame; if no such, try to convert to accept/reject. */
    if (!fstk->ecnt)
    {
      if (fstk->vstk[retpos].type == T_BOOL)
	return (fstk->vstk[retpos].val.i) ? F_ACCEPT :  F_REJECT;
      else
	runtime("Can't return non-bool from non-function");
    }

    /* Set the value stack position, overwriting the former implicit void */
    fstk->vcnt = fstk->estk[fstk->ecnt].ventry - 1;

    /* Copy the return value */
    RESULT_VAL(fstk->vstk[retpos]);
  }

  INST(FI_CALL, 0, 1) {
    NEVER_CONSTANT;
    SYMBOL;

    FID_SAME_BODY()
      if (!(f1->sym->flags & SYM_FLAG_SAME))
	return 0;

    FID_ITERATE_BODY()
      BUFFER_PUSH(fit->lines) = whati->sym->function;

    FID_INTERPRET_BODY()

    /* Push the body on stack */
    LINEX(sym->function);
    curline.emask |= FE_RETURN;

    /* Before this instruction was called, there was the T_VOID
     * automatic return value pushed on value stack and also
     * sym->function->args function arguments. Setting the
     * vbase to point to first argument. */
    ASSERT(curline.ventry >= sym->function->args);
    curline.ventry -= sym->function->args;
    curline.vbase = curline.ventry;

    /* Storage for local variables */
    f_vcnt_check_overflow(sym->function->vars);
    memset(&(fstk->vstk[fstk->vcnt]), 0, sizeof(struct f_val) * sym->function->vars);
    fstk->vcnt += sym->function->vars;
  }

  INST(FI_DROP_RESULT, 1, 0) {
    NEVER_CONSTANT;
    ARG_ANY(1);
  }

  INST(FI_SWITCH, 1, 0) {
    ARG_ANY(1);

    FID_MEMBER(struct f_tree *, tree, [[!same_tree(f1->tree, f2->tree)]], "tree %p", item->tree);

    FID_ITERATE_BODY()
      tree_walk(whati->tree, f_add_tree_lines, fit);

    FID_INTERPRET_BODY()
    const struct f_tree *t = find_tree(tree, &v1);
    if (!t) {
      v1.type = T_VOID;
      t = find_tree(tree, &v1);
      if (!t) {
	debug( "No else statement?\n");
	FID_HIC(,break,return NULL);
      }
    }
    /* It is actually possible to have t->data NULL */

    LINEX(t->data);
  }

  INST(FI_IP_MASK, 2, 1) { /* IP.MASK(val) */
    ARG(1, T_IP);
    ARG(2, T_INT);
    RESULT(T_IP, ip, [[ ipa_is_ip4(v1.val.ip) ?
      ipa_from_ip4(ip4_and(ipa_to_ip4(v1.val.ip), ip4_mkmask(v2.val.i))) :
      ipa_from_ip6(ip6_and(ipa_to_ip6(v1.val.ip), ip6_mkmask(v2.val.i))) ]]);
  }

  INST(FI_PATH_PREPEND, 2, 1) {	/* Path prepend */
    ARG(1, T_PATH);
    ARG(2, T_INT);
    RESULT(T_PATH, ad, [[ as_path_prepend(fpool, v1.val.ad, v2.val.i) ]]);
  }

  INST(FI_CLIST_ADD, 2, 1) {	/* (Extended) Community list add */
    ARG_ANY(1);
    ARG_ANY(2);
    RESULT_TYPE(f1->type);

    if (v1.type == T_PATH)
      runtime("Can't add to path");

    else if (v1.type == T_CLIST)
    {
      /* Community (or cluster) list */
      struct f_val dummy;

      if ((v2.type == T_PAIR) || (v2.type == T_QUAD))
	RESULT_(T_CLIST, ad, [[ int_set_add(fpool, v1.val.ad, v2.val.i) ]]);
      /* IP->Quad implicit conversion */
      else if (val_is_ip4(&v2))
	RESULT_(T_CLIST, ad, [[ int_set_add(fpool, v1.val.ad, ipa_to_u32(v2.val.ip)) ]]);
      else if ((v2.type == T_SET) && clist_set_type(v2.val.t, &dummy))
	runtime("Can't add set");
      else if (v2.type == T_CLIST)
	RESULT_(T_CLIST, ad, [[ int_set_union(fpool, v1.val.ad, v2.val.ad) ]]);
      else
	runtime("Can't add non-pair");
    }

    else if (v1.type == T_ECLIST)
    {
      /* v2.val is either EC or EC-set */
      if ((v2.type == T_SET) && eclist_set_type(v2.val.t))
	runtime("Can't add set");
      else if (v2.type == T_ECLIST)
	RESULT_(T_ECLIST, ad, [[ ec_set_union(fpool, v1.val.ad, v2.val.ad) ]]);
      else if (v2.type != T_EC)
	runtime("Can't add non-ec");
      else
	RESULT_(T_ECLIST, ad, [[ ec_set_add(fpool, v1.val.ad, v2.val.ec) ]]);
    }

    else if (v1.type == T_LCLIST)
    {
      /* v2.val is either LC or LC-set */
      if ((v2.type == T_SET) && lclist_set_type(v2.val.t))
	runtime("Can't add set");
      else if (v2.type == T_LCLIST)
	RESULT_(T_LCLIST, ad, [[ lc_set_union(fpool, v1.val.ad, v2.val.ad) ]]);
      else if (v2.type != T_LC)
	runtime("Can't add non-lc");
      else
	RESULT_(T_LCLIST, ad, [[ lc_set_add(fpool, v1.val.ad, v2.val.lc) ]]);

    }

    else
      runtime("Can't add to non-[e|l]clist");
  }

  INST(FI_CLIST_DEL, 2, 1) {	/* (Extended) Community list add or delete */
    ARG_ANY(1);
    ARG_ANY(2);
    RESULT_TYPE(f1->type);

    if (v1.type == T_PATH)
    {
      const struct f_tree *set = NULL;
      u32 key = 0;

      if (v2.type == T_INT)
	key = v2.val.i;
      else if ((v2.type == T_SET) && (v2.val.t->from.type == T_INT))
	set = v2.val.t;
      else
	runtime("Can't delete non-integer (set)");

      RESULT_(T_PATH, ad, [[ as_path_filter(fpool, v1.val.ad, set, key, 0) ]]);
    }

    else if (v1.type == T_CLIST)
    {
      /* Community (or cluster) list */
      struct f_val dummy;

      if ((v2.type == T_PAIR) || (v2.type == T_QUAD))
	RESULT_(T_CLIST, ad, [[ int_set_del(fpool, v1.val.ad, v2.val.i) ]]);
      /* IP->Quad implicit conversion */
      else if (val_is_ip4(&v2))
	RESULT_(T_CLIST, ad, [[ int_set_del(fpool, v1.val.ad, ipa_to_u32(v2.val.ip)) ]]);
      else if ((v2.type == T_SET) && clist_set_type(v2.val.t, &dummy) || (v2.type == T_CLIST))
	RESULT_(T_CLIST, ad, [[ clist_filter(fpool, v1.val.ad, &v2, 0) ]]);
      else
	runtime("Can't delete non-pair");
    }

    else if (v1.type == T_ECLIST)
    {
      /* v2.val is either EC or EC-set */
      if ((v2.type == T_SET) && eclist_set_type(v2.val.t) || (v2.type == T_ECLIST))
	RESULT_(T_ECLIST, ad, [[ eclist_filter(fpool, v1.val.ad, &v2, 0) ]]);
      else if (v2.type != T_EC)
	runtime("Can't delete non-ec");
      else
	RESULT_(T_ECLIST, ad, [[ ec_set_del(fpool, v1.val.ad, v2.val.ec) ]]);
    }

    else if (v1.type == T_LCLIST)
    {
      /* v2.val is either LC or LC-set */
      if ((v2.type == T_SET) && lclist_set_type(v2.val.t) || (v2.type == T_LCLIST))
	RESULT_(T_LCLIST, ad, [[ lclist_filter(fpool, v1.val.ad, &v2, 0) ]]);
      else if (v2.type != T_LC)
	runtime("Can't delete non-lc");
      else
	RESULT_(T_LCLIST, ad, [[ lc_set_del(fpool, v1.val.ad, v2.val.lc) ]]);
    }

    else
      runtime("Can't delete in non-[e|l]clist");
  }

  INST(FI_CLIST_FILTER, 2, 1) {	/* (Extended) Community list add or delete */
    ARG_ANY(1);
    ARG_ANY(2);
    RESULT_TYPE(f1->type);

    if (v1.type == T_PATH)
    {
      u32 key = 0;

      if ((v2.type == T_SET) && (v2.val.t->from.type == T_INT))
	RESULT_(T_PATH, ad, [[ as_path_filter(fpool, v1.val.ad, v2.val.t, key, 1) ]]);
      else
	runtime("Can't filter integer");
    }

    else if (v1.type == T_CLIST)
    {
      /* Community (or cluster) list */
      struct f_val dummy;

      if ((v2.type == T_SET) && clist_set_type(v2.val.t, &dummy) || (v2.type == T_CLIST))
	RESULT_(T_CLIST, ad, [[ clist_filter(fpool, v1.val.ad, &v2, 1) ]]);
      else
	runtime("Can't filter pair");
    }

    else if (v1.type == T_ECLIST)
    {
      /* v2.val is either EC or EC-set */
      if ((v2.type == T_SET) && eclist_set_type(v2.val.t) || (v2.type == T_ECLIST))
	RESULT_(T_ECLIST, ad, [[ eclist_filter(fpool, v1.val.ad, &v2, 1) ]]);
      else
	runtime("Can't filter ec");
    }

    else if (v1.type == T_LCLIST)
    {
      /* v2.val is either LC or LC-set */
      if ((v2.type == T_SET) && lclist_set_type(v2.val.t) || (v2.type == T_LCLIST))
	RESULT_(T_LCLIST, ad, [[ lclist_filter(fpool, v1.val.ad, &v2, 1) ]]);
      else
	runtime("Can't filter lc");
    }

    else
      runtime("Can't filter non-[e|l]clist");
  }

  INST(FI_ROA_CHECK, 2, 1) {	/* ROA Check */
    NEVER_CONSTANT;
    ARG(1, T_NET);
    ARG(2, T_INT);
    RTC(3);
    struct rtable *table = rtc->table;

    u32 as = v2.val.i;

    if (!table)
      runtime("Missing ROA table");

    if (table->addr_type != NET_ROA4 && table->addr_type != NET_ROA6)
      runtime("Table type must be either ROA4 or ROA6");

    if (table->addr_type != (v1.val.net->type == NET_IP4 ? NET_ROA4 : NET_ROA6))
      RESULT(T_ENUM_ROA, i, ROA_UNKNOWN); /* Prefix and table type mismatch */
    else
      RESULT(T_ENUM_ROA, i, [[ net_roa_check(table, v1.val.net, as) ]]);

  }

  INST(FI_FORMAT, 1, 0) {	/* Format */
    ARG_ANY(1);
    RESULT(T_STRING, s, val_format_str(fpool, &v1));
  }

  INST(FI_ASSERT, 1, 0) {	/* Birdtest Assert */
    NEVER_CONSTANT;
    ARG(1, T_BOOL);

    FID_MEMBER(char *, s, [[strcmp(f1->s, f2->s)]], "string %s", item->s);

    ASSERT(s);

    if (!bt_assert_hook)
      runtime("No bt_assert hook registered, can't assert");

    bt_assert_hook(v1.val.i, what);
  }