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
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
|
#include "common.h"
#include "rte_branch_prediction.h"
#include <cJSON.h>
#include <ldbc.h>
#include <mrb_define.h>
#include <netinet/in.h>
#include <pkt_classifier_engine.h>
#include <rpc.h>
#include <rte_acl.h>
#include <rte_graph.h>
#include <rte_graph_worker.h>
#include <rte_rcu_qsbr.h>
#include <sc_vdev.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#define PASS_THROUGH_ENABLE 1
static uint64_t acl_serial_number = 0;
typedef struct domain * (*domain_create)(struct rule_field_engine * rules[], uint32_t nr_rules, uint16_t domain_index);
enum engine_algo_type
{
ENGINE_TYPE_TREE_SEARCH = 0,
ENGINE_TYPE_LINEAR_SEARCH,
ENGINE_TYPE_MAX,
};
struct match_field_tree
{
uint8_t proto;
uint16_t ether_type;
uint16_t src_port;
uint16_t dst_port;
union {
uint32_t src_addr_ipv4;
uint8_t u8_src_addr_ipv6[16];
uint32_t u32_src_addr_ipv6[4];
};
union {
uint32_t dst_addr_ipv4;
uint8_t u8_dst_addr_ipv6[16];
uint32_t u32_dst_addr_ipv6[4];
};
};
struct match_field_linear
{
uint8_t proto;
uint16_t ether_type;
uint16_t src_port;
uint16_t dst_port;
union {
struct rte_ipv4_hdr * ipv4_hdr;
struct rte_ipv6_hdr * ipv6_hdr;
};
};
/* Tree search (DPDK ACL LIB) */
enum ACL_GROUP_V4
{
GROUP_PROTO_V4,
GROUP_SRC_ADDR_V4,
GROUP_DST_ADDR_V4,
GROUP_PORTS_V4,
GROUP_NUM_V4
};
enum ACL_FIELD_V4
{
PROTO_V4,
SRC_ADDR_V4,
DST_ADDR_V4,
SRC_PORT_V4,
DST_PORT_V4,
FIELD_NUM_V4
};
struct rte_acl_field_def acl_defs_v4[FIELD_NUM_V4] = {
{
.type = RTE_ACL_FIELD_TYPE_BITMASK,
.size = sizeof(uint8_t),
.field_index = PROTO_V4,
.input_index = GROUP_PROTO_V4,
.offset = offsetof(struct match_field_tree, proto),
},
{
.type = RTE_ACL_FIELD_TYPE_MASK,
.size = sizeof(uint32_t),
.field_index = SRC_ADDR_V4,
.input_index = GROUP_SRC_ADDR_V4,
.offset = offsetof(struct match_field_tree, src_addr_ipv4),
},
{
.type = RTE_ACL_FIELD_TYPE_MASK,
.size = sizeof(uint32_t),
.field_index = DST_ADDR_V4,
.input_index = GROUP_DST_ADDR_V4,
.offset = offsetof(struct match_field_tree, dst_addr_ipv4),
},
{
.type = RTE_ACL_FIELD_TYPE_RANGE,
.size = sizeof(uint16_t),
.field_index = SRC_PORT_V4,
.input_index = GROUP_PORTS_V4,
.offset = offsetof(struct match_field_tree, src_port),
},
{
.type = RTE_ACL_FIELD_TYPE_RANGE,
.size = sizeof(uint16_t),
.field_index = DST_PORT_V4,
.input_index = GROUP_PORTS_V4,
.offset = offsetof(struct match_field_tree, dst_port),
},
};
RTE_ACL_RULE_DEF(acl_rule_v4, RTE_DIM(acl_defs_v4));
enum ACL_GROUP_V6
{
GROUP_PROTO_V6,
GROUP_SRC_ADDR_V6_1,
GROUP_SRC_ADDR_V6_2,
GROUP_SRC_ADDR_V6_3,
GROUP_SRC_ADDR_V6_4,
GROUP_DST_ADDR_V6_1,
GROUP_DST_ADDR_V6_2,
GROUP_DST_ADDR_V6_3,
GROUP_DST_ADDR_V6_4,
GROUP_PORTS_V6,
GROUP_NUM_V6
};
enum ACL_FIELD_V6
{
PROTO_V6,
SRC_ADDR_V6_1,
SRC_ADDR_V6_2,
SRC_ADDR_V6_3,
SRC_ADDR_V6_4,
DST_ADDR_V6_1,
DST_ADDR_V6_2,
DST_ADDR_V6_3,
DST_ADDR_V6_4,
SRC_PORT_V6,
DST_PORT_V6,
FIELD_NUM_V6
};
struct rte_acl_field_def acl_defs_v6[FIELD_NUM_V6] = {
{
.type = RTE_ACL_FIELD_TYPE_BITMASK,
.size = sizeof(uint8_t),
.field_index = PROTO_V6,
.input_index = GROUP_PROTO_V6,
.offset = offsetof(struct match_field_tree, proto),
},
{
.type = RTE_ACL_FIELD_TYPE_MASK,
.size = sizeof(uint32_t),
.field_index = SRC_ADDR_V6_1,
.input_index = GROUP_SRC_ADDR_V6_1,
.offset = offsetof(struct match_field_tree, u32_src_addr_ipv6[0]),
},
{
.type = RTE_ACL_FIELD_TYPE_MASK,
.size = sizeof(uint32_t),
.field_index = SRC_ADDR_V6_2,
.input_index = GROUP_SRC_ADDR_V6_2,
.offset = offsetof(struct match_field_tree, u32_src_addr_ipv6[1]),
},
{
.type = RTE_ACL_FIELD_TYPE_MASK,
.size = sizeof(uint32_t),
.field_index = SRC_ADDR_V6_3,
.input_index = GROUP_SRC_ADDR_V6_3,
.offset = offsetof(struct match_field_tree, u32_src_addr_ipv6[2]),
},
{
.type = RTE_ACL_FIELD_TYPE_MASK,
.size = sizeof(uint32_t),
.field_index = SRC_ADDR_V6_4,
.input_index = GROUP_SRC_ADDR_V6_4,
.offset = offsetof(struct match_field_tree, u32_src_addr_ipv6[3]),
},
{
.type = RTE_ACL_FIELD_TYPE_MASK,
.size = sizeof(uint32_t),
.field_index = DST_ADDR_V6_1,
.input_index = GROUP_DST_ADDR_V6_1,
.offset = offsetof(struct match_field_tree, u32_dst_addr_ipv6[0]),
},
{
.type = RTE_ACL_FIELD_TYPE_MASK,
.size = sizeof(uint32_t),
.field_index = DST_ADDR_V6_2,
.input_index = GROUP_DST_ADDR_V6_2,
.offset = offsetof(struct match_field_tree, u32_dst_addr_ipv6[1]),
},
{
.type = RTE_ACL_FIELD_TYPE_MASK,
.size = sizeof(uint32_t),
.field_index = DST_ADDR_V6_3,
.input_index = GROUP_DST_ADDR_V6_3,
.offset = offsetof(struct match_field_tree, u32_dst_addr_ipv6[2]),
},
{
.type = RTE_ACL_FIELD_TYPE_MASK,
.size = sizeof(uint32_t),
.field_index = DST_ADDR_V6_4,
.input_index = GROUP_DST_ADDR_V6_4,
.offset = offsetof(struct match_field_tree, u32_dst_addr_ipv6[3]),
},
{
.type = RTE_ACL_FIELD_TYPE_RANGE,
.size = sizeof(uint16_t),
.field_index = SRC_PORT_V6,
.input_index = GROUP_PORTS_V6,
.offset = offsetof(struct match_field_tree, src_port),
},
{
.type = RTE_ACL_FIELD_TYPE_RANGE,
.size = sizeof(uint16_t),
.field_index = DST_PORT_V6,
.input_index = GROUP_PORTS_V6,
.offset = offsetof(struct match_field_tree, dst_port),
},
};
RTE_ACL_RULE_DEF(acl_rule_v6, RTE_DIM(acl_defs_v6));
struct linear_ctx_v4
{
uint8_t proto;
uint8_t proto_mask;
uint8_t priority;
uint16_t action_index;
uint16_t src_port_start;
uint16_t src_port_end;
uint16_t dst_port_start;
uint16_t dst_port_end;
uint32_t src_addr;
uint32_t dst_addr;
uint32_t src_addr_mask;
uint32_t dst_addr_mask;
RTE_MARKER cacheline1 __rte_cache_min_aligned;
};
struct linear_ctx_v6
{
uint8_t src_addr[16];
uint8_t dst_addr[16];
uint8_t src_addr_mask[16];
uint8_t dst_addr_mask[16];
RTE_MARKER cacheline1 __rte_cache_min_aligned;
uint8_t proto;
uint8_t proto_mask;
uint8_t priority;
uint16_t action_index;
uint16_t src_port_start;
uint16_t src_port_end;
uint16_t dst_port_start;
uint16_t dst_port_end;
};
struct domain
{
RTE_MARKER cacheline0;
uint16_t nr_rules_v4;
uint16_t nr_rules_v6;
uint16_t can_pass_through_v4;
uint16_t can_pass_through_v6;
uint16_t can_pass_through_domain;
struct action pass_through_action_v4;
struct action pass_through_action_v6;
struct action pass_through_action_domain;
RTE_MARKER cacheline1 __rte_cache_min_aligned;
union {
/* ACL IPv4 context for tree search */
struct rte_acl_ctx * tree_ctx_v4;
/* IPv4 rules for linear search */
struct linear_ctx_v4 * linear_ctx_v4;
void * ctx_v4;
};
union {
/* ACL IPv6 context for tree search */
struct rte_acl_ctx * tree_ctx_v6;
/* IPv6 rules for linear search */
struct linear_ctx_v6 * linear_ctx_v6;
void * ctx_v6;
};
struct action action_list_v4[1025]; // rule number is 1024, rule 0 not used
struct action action_list_v6[1025]; // rule number is 1024, rule 0 not used
};
/* Linear search */
/**************************************************************************************************************************/
/* Filter engine */
struct filter_engine
{
uint16_t nr_domains;
enum engine_algo_type type;
struct domain * domains[1024];
};
/* Pkt classifier engine */
struct pkt_classifier_engine
{
uint32_t nr_domains;
enum ruleset_type ruleset_type;
struct rte_rcu_qsbr * qsv;
struct filter_engine * filter_engine;
} __rte_cache_aligned;
/* Private function */
int rule_validity_check(struct rule_field_engine rules[], uint32_t nr_rules, uint32_t nr_domains)
{
/* Check the number of rules */
assert(nr_rules <= MAX_RULES);
/* Iterate through all rules and individually validate the fields of each rule. */
for (uint32_t rule_index = 0; rule_index < nr_rules; rule_index++)
{
struct rule_field_engine * rule = &rules[rule_index];
/* Verify the rule field */
assert((rule->proto == IPPROTO_TCP) || (rule->proto == IPPROTO_UDP) || (rule->proto == 0));
assert((rule->proto_mask == UINT8_MAX) || (rule->proto_mask == 0));
assert((rule->ether_type == RTE_ETHER_TYPE_IPV4) || (rule->ether_type == RTE_ETHER_TYPE_IPV6) ||
(rule->ether_type == UINT16_MAX));
assert((rule->src_port_start <= rule->src_port_end) && (rule->src_port_end <= 65535));
assert((rule->dst_port_start <= rule->dst_port_end) && (rule->dst_port_end <= 65535));
assert(rule->priority < 16);
assert((rule->domain < nr_domains) || (rule->domain == UINT16_MAX));
switch (rule->ether_type)
{
case RTE_ETHER_TYPE_IPV4:
assert((rule->dst_addr_mask_len <= 32) && (rule->src_addr_mask_len <= 32));
break;
case RTE_ETHER_TYPE_IPV6:
assert((rule->dst_addr_mask_len <= 128) && (rule->src_addr_mask_len <= 128));
break;
case UINT16_MAX:
assert((rule->dst_addr_mask_len == 0) && (rule->src_addr_mask_len == 0));
break;
default:
assert(0);
break;
}
}
return RT_SUCCESS;
}
int duplicate_rules_check(struct rule_field_engine * rules, uint32_t nr_rules, char * str_error_reason,
unsigned int sz_error_reason)
{
for (uint32_t rule_index = 0; rule_index < nr_rules; rule_index++)
{
struct rule_field_engine * rule_to_check = &rules[rule_index];
for (uint32_t index = 0; index < nr_rules; index++)
{
struct rule_field_engine * comparison_rule = &rules[index];
if (rule_index == index)
continue;
if (rule_to_check->domain != comparison_rule->domain)
continue;
if (rule_to_check->ether_type != comparison_rule->ether_type)
continue;
if (rule_to_check->proto != comparison_rule->proto)
continue;
if (rule_to_check->proto_mask != comparison_rule->proto_mask)
continue;
if (rule_to_check->src_port_start != comparison_rule->src_port_start)
continue;
if (rule_to_check->src_port_end != comparison_rule->src_port_end)
continue;
if (rule_to_check->dst_port_start != comparison_rule->dst_port_start)
continue;
if (rule_to_check->dst_port_end != comparison_rule->dst_port_end)
continue;
if (rule_to_check->src_addr_mask_len != comparison_rule->src_addr_mask_len)
continue;
if (rule_to_check->dst_addr_mask_len != comparison_rule->dst_addr_mask_len)
continue;
if (rule_to_check->priority != comparison_rule->priority)
continue;
if (rule_to_check->ether_type == RTE_ETHER_TYPE_IPV4)
{
if (rule_to_check->src_addr_v4 != comparison_rule->src_addr_v4)
continue;
if (rule_to_check->dst_addr_v4 != comparison_rule->dst_addr_v4)
continue;
}
else if (rule_to_check->ether_type == RTE_ETHER_TYPE_IPV6)
{
if (memcmp(rule_to_check->src_addr_v6, comparison_rule->src_addr_v6, 16) != 0)
continue;
if (memcmp(rule_to_check->dst_addr_v6, comparison_rule->dst_addr_v6, 16) != 0)
continue;
}
sprintf(str_error_reason, "Duplicate rule %d and %d", rule_index, index);
return RT_ERR;
}
}
return RT_SUCCESS;
}
void evaluate_pass_through_condition(struct domain * domain, struct rule_field_engine * rules_v4[],
uint16_t nr_rules_v4, struct rule_field_engine * rules_v6[], uint16_t nr_rules_v6)
{
for (int32_t i = 0; i < 2; i++)
{
uint16_t nr_rules;
uint16_t * pass_through = NULL;
struct rule_field_engine * rule = NULL;
struct action * pass_through_action = NULL;
if (i == 0)
{
rule = rules_v4[0];
nr_rules = nr_rules_v4;
pass_through = &domain->can_pass_through_v4;
pass_through_action = &domain->pass_through_action_v4;
}
else
{
rule = rules_v6[0];
nr_rules = nr_rules_v6;
pass_through = &domain->can_pass_through_v6;
pass_through_action = &domain->pass_through_action_v6;
}
if (nr_rules != 1)
continue;
if (rule->proto_mask != 0)
continue;
if (rule->src_addr_mask_len != 0)
continue;
if (rule->dst_addr_mask_len != 0)
continue;
if ((rule->src_port_start != 0) || (rule->src_port_end != 65535))
continue;
if ((rule->dst_port_start != 0) || (rule->dst_port_end != 65535))
continue;
*pass_through = PASS_THROUGH_ENABLE;
memcpy(pass_through_action, &rule->action, sizeof(struct action));
}
if (domain->can_pass_through_v4 && (nr_rules_v6 == 0))
{
domain->can_pass_through_domain = PASS_THROUGH_ENABLE;
memcpy(&domain->pass_through_action_domain, &domain->pass_through_action_v4, sizeof(struct action));
}
else if (domain->can_pass_through_v6 && (nr_rules_v4 == 0))
{
domain->can_pass_through_domain = PASS_THROUGH_ENABLE;
memcpy(&domain->pass_through_action_domain, &domain->pass_through_action_v6, sizeof(struct action));
}
else if (domain->can_pass_through_v4 && domain->can_pass_through_v6)
{
if ((domain->pass_through_action_v4.type == domain->pass_through_action_v6.type) &&
(domain->pass_through_action_v4.data = domain->pass_through_action_v6.data))
{
domain->can_pass_through_domain = PASS_THROUGH_ENABLE;
memcpy(&domain->pass_through_action_domain, &domain->pass_through_action_v4, sizeof(struct action));
}
}
}
/* Make sure that 'out_acl_rules' has been initialized before use */
void acl_rule_conversion_v4(struct rule_field_engine * rules[], uint16_t nr_rules, struct acl_rule_v4 * out_acl_rules,
struct action * out_actions)
{
for (uint16_t index = 0; index < nr_rules; index++)
{
struct rule_field_engine * rule = rules[index];
struct acl_rule_v4 * acl_rule = &out_acl_rules[index];
struct action * action = &out_actions[index + 1];
acl_rule->field[PROTO_V4].value.u8 = rule->proto;
acl_rule->field[PROTO_V4].mask_range.u8 = rule->proto_mask;
acl_rule->field[SRC_ADDR_V4].value.u32 = ntohl(rule->src_addr_v4);
acl_rule->field[SRC_ADDR_V4].mask_range.u32 = rule->src_addr_mask_len;
acl_rule->field[DST_ADDR_V4].value.u32 = ntohl(rule->dst_addr_v4);
acl_rule->field[DST_ADDR_V4].mask_range.u32 = rule->dst_addr_mask_len;
acl_rule->field[SRC_PORT_V4].value.u16 = rule->src_port_start;
acl_rule->field[SRC_PORT_V4].mask_range.u16 = rule->src_port_end;
acl_rule->field[DST_PORT_V4].value.u16 = rule->dst_port_start;
acl_rule->field[DST_PORT_V4].mask_range.u16 = rule->dst_port_end;
acl_rule->data.priority = 1;
acl_rule->data.category_mask = 1 << rule->priority;
acl_rule->data.userdata = index + 1;
memcpy(action, &rule->action, sizeof(struct action));
}
}
/* Make sure that 'out_acl_rules' has been initialized before use */
void acl_rule_conversion_v6(struct rule_field_engine ** rules, uint16_t nr_rules, struct acl_rule_v6 * out_acl_rules,
struct action out_actions[])
{
for (uint16_t index = 0; index < nr_rules; index++)
{
struct rule_field_engine * rule = rules[index];
struct acl_rule_v6 * acl_rule = &out_acl_rules[index];
struct action * action = &out_actions[index + 1];
acl_rule->field[PROTO_V6].value.u8 = rule->proto;
acl_rule->field[PROTO_V6].mask_range.u8 = rule->proto_mask;
/* Populate IPv6 source address */
for (int i = 0; i < 4; i++)
{
if (rule->src_addr_mask_len > (i + 1) * 32)
acl_rule->field[SRC_ADDR_V6_1 + i].mask_range.u32 = 32;
else
acl_rule->field[SRC_ADDR_V6_1 + i].mask_range.u32 =
rule->src_addr_mask_len > (i * 32) ? rule->src_addr_mask_len - (i * 32) : 0;
acl_rule->field[SRC_ADDR_V6_1 + i].value.u32 = ntohl(rule->src_addr_v6[i]);
}
/* Populate IPv6 destination address */
for (int i = 0; i < 4; i++)
{
if (rule->dst_addr_mask_len > (i + 1) * 32)
acl_rule->field[DST_ADDR_V6_1 + i].mask_range.u32 = 32;
else
acl_rule->field[DST_ADDR_V6_1 + i].mask_range.u32 =
rule->dst_addr_mask_len > (i * 32) ? rule->dst_addr_mask_len - (i * 32) : 0;
acl_rule->field[DST_ADDR_V6_1 + i].value.u32 = ntohl(rule->dst_addr_v6[i]);
}
acl_rule->field[SRC_PORT_V6].value.u16 = rule->src_port_start;
acl_rule->field[SRC_PORT_V6].mask_range.u16 = rule->src_port_end;
acl_rule->field[DST_PORT_V6].value.u16 = rule->dst_port_start;
acl_rule->field[DST_PORT_V6].mask_range.u16 = rule->dst_port_end;
acl_rule->data.priority = 1;
acl_rule->data.category_mask = 1 << rule->priority;
acl_rule->data.userdata = index + 1;
memcpy(action, &rule->action, sizeof(struct action));
}
}
/* The acl context create. */
struct rte_acl_ctx * acl_ctx_create(char * context_name, char * acl_rules, uint16_t nr_rules, uint16_t ip_version)
{
int ret = 0, dim = 0;
uint64_t def_size = 0, offset_unit = 0;
struct rte_acl_field_def * defs = NULL;
/* Calculate the information needed for context creation. */
if (ip_version == RTE_ETHER_TYPE_IPV4)
{
dim = RTE_DIM(acl_defs_v4);
defs = acl_defs_v4;
def_size = sizeof(acl_defs_v4);
offset_unit = sizeof(struct acl_rule_v4);
}
else
{
dim = RTE_DIM(acl_defs_v6);
defs = acl_defs_v6;
def_size = sizeof(acl_defs_v6);
offset_unit = sizeof(struct acl_rule_v6);
}
/* Populate the 'rte_acl_param' structure*/
struct rte_acl_param context_param;
context_param.name = context_name;
context_param.socket_id = 0; // Multi-socket not currently supported
context_param.rule_size = RTE_ACL_RULE_SZ(dim);
context_param.max_rule_num = MAX_RULES;
/* Create ACL context */
struct rte_acl_ctx * context = rte_acl_create(&context_param);
if (context == NULL)
{
MR_ERROR("Failed to create context with the name: '%s' .", context_name);
return NULL;
}
/* Currently, we are using the default algorithm configuration, so there's no need to call
* "rte_acl_set_ctx_classify." */
/* Add rules to the ACL context. */
for (uint16_t index = 0; index < nr_rules; index++)
{
struct rte_acl_rule * rule = (struct rte_acl_rule *)((char *)acl_rules + index * offset_unit);
ret = rte_acl_add_rules(context, (struct rte_acl_rule *)rule, 1);
if (ret != 0)
{
MR_ERROR("Failed to add rule %d to the context with the name: '%s' .", index, context_name);
rte_acl_free(context);
return NULL;
}
}
/* Prepare ACL config data for build. */
struct rte_acl_config acl_build_param;
memset(&acl_build_param, 0, sizeof(acl_build_param));
acl_build_param.num_categories = 16;
acl_build_param.num_fields = dim;
memcpy(&acl_build_param.defs, defs, def_size);
/* Build the ACL context. */
ret = rte_acl_build(context, &acl_build_param);
if (ret != 0)
{
MR_ERROR("Failed to build the context with the name: '%s' .", context_name);
goto error;
}
/* Dump the ACL context for the new build */
rte_acl_dump(context);
return context;
error:
rte_acl_free(context);
return NULL;
}
void classify_rules_by_ip_version(struct rule_field_engine * rules[], uint32_t nr_rules,
struct rule_field_engine * out_rules_v4[], uint16_t * out_nr_rules_v4,
struct rule_field_engine * out_rules_v6[], uint16_t * out_nr_rules_v6)
{
uint16_t nr_rules_v4 = 0, nr_rules_v6 = 0;
/* Classify rules based on IP version. */
for (uint16_t index = 0; index < nr_rules; index++)
{
struct rule_field_engine * rule = rules[index];
switch (rule->ether_type)
{
case RTE_ETHER_TYPE_IPV4:
out_rules_v4[nr_rules_v4] = rule;
nr_rules_v4++;
break;
case RTE_ETHER_TYPE_IPV6:
out_rules_v6[nr_rules_v6] = rule;
nr_rules_v6++;
break;
case UINT16_MAX:
out_rules_v4[nr_rules_v4] = rule;
nr_rules_v4++;
out_rules_v6[nr_rules_v6] = rule;
nr_rules_v6++;
break;
default:
assert(0);
break;
}
}
*out_nr_rules_v4 = nr_rules_v4;
*out_nr_rules_v6 = nr_rules_v6;
}
/* Create the domain instance for tree search. */
struct domain * domain_create_for_tree_search(struct rule_field_engine * rules[], uint32_t nr_rules,
uint16_t domain_index)
{
/* Allocate memory and initialize the domain instance */
struct domain * domain = ZMALLOC(sizeof(struct domain));
MR_VERIFY_MALLOC(domain);
/* Classify rules based on IP version. */
uint16_t nr_rules_v4, nr_rules_v6;
struct rule_field_engine * rules_v4[MAX_RULES];
struct rule_field_engine * rules_v6[MAX_RULES];
classify_rules_by_ip_version(rules, nr_rules, rules_v4, &nr_rules_v4, rules_v6, &nr_rules_v6);
domain->nr_rules_v4 = nr_rules_v4;
domain->nr_rules_v6 = nr_rules_v6;
/* Since the underlying search algorithm for tree search is ACL, convert engine rule data to ACL rule data. */
struct acl_rule_v4 acl_rule_v4[MAX_RULES];
struct acl_rule_v6 acl_rule_v6[MAX_RULES];
acl_rule_conversion_v4(rules_v4, nr_rules_v4, acl_rule_v4, domain->action_list_v4);
acl_rule_conversion_v6(rules_v6, nr_rules_v6, acl_rule_v6, domain->action_list_v6);
/* Create ACL context for IPv4 */
if (nr_rules_v4 > 0)
{
/* Generate the ACL context name string */
char acl_context_name_v4[MR_STRING_MAX];
snprintf(acl_context_name_v4, sizeof(acl_context_name_v4) - 1, "acl-context-v4-sn-%lu-domain-%u",
acl_serial_number, domain_index);
struct rte_acl_ctx * ctx =
acl_ctx_create(acl_context_name_v4, (char *)acl_rule_v4, nr_rules_v4, RTE_ETHER_TYPE_IPV4);
if (ctx == NULL)
{
MR_ERROR("Failed to create acl context %s", acl_context_name_v4);
goto error;
}
domain->tree_ctx_v4 = ctx;
}
/* Create ACL context for IPv6 */
if (nr_rules_v6 > 0)
{
/* Generate the ACL context name string */
char acl_context_name_v6[MR_STRING_MAX];
snprintf(acl_context_name_v6, sizeof(acl_context_name_v6) - 1, "acl-context-v6-sn-%lu-domain-%u",
acl_serial_number, domain_index);
struct rte_acl_ctx * ctx =
acl_ctx_create(acl_context_name_v6, (char *)acl_rule_v6, nr_rules_v6, RTE_ETHER_TYPE_IPV6);
if (ctx == NULL)
{
MR_ERROR("Failed to create acl context %s", acl_context_name_v6);
goto error;
}
domain->tree_ctx_v6 = ctx;
}
/* Evaluate the pass through condition */
evaluate_pass_through_condition(domain, rules_v4, nr_rules_v4, rules_v6, nr_rules_v6);
acl_serial_number++;
return domain;
error:
if (domain->tree_ctx_v4 != NULL)
rte_acl_free(domain->tree_ctx_v4);
if (domain->tree_ctx_v6 != NULL)
rte_acl_free(domain->tree_ctx_v6);
FREE(domain);
return NULL;
}
void domain_array_free_for_tree_search(struct filter_engine * filter_engine)
{
for (uint16_t index = 0; index < RTE_DIM(filter_engine->domains); index++)
{
struct domain * domain = filter_engine->domains[index];
if (domain != NULL)
{
if (domain->tree_ctx_v4 != NULL)
{
rte_acl_free(domain->tree_ctx_v4);
domain->tree_ctx_v4 = NULL;
}
if (domain->tree_ctx_v6 != NULL)
{
rte_acl_free(domain->tree_ctx_v6);
domain->tree_ctx_v6 = NULL;
}
FREE(domain);
domain = NULL;
}
}
}
int mask_length_to_mask_v4(uint32_t mask_length, uint32_t * mask)
{
if (mask_length > 32)
return -1;
if (mask_length == 0)
{
*mask = 0;
}
else
{
*mask = rte_cpu_to_be_32(~((1U << (32 - mask_length)) - 1));
}
return 0;
}
int mask_length_to_mask_v6(uint32_t mask_length, uint8_t mask[])
{
if (mask_length > 128)
return -1;
if (mask_length == 0)
{
memset(mask, 0, 16);
}
else
{
memset(mask, 0xFF, 16);
for (int i = 0; i < 16; i++)
{
if (mask_length > (i + 1) * 8)
continue;
else if (mask_length > i * 8)
mask[i] = ~((1U << (8 - (mask_length - i * 8))) - 1);
else
mask[i] = 0;
}
}
return 0;
}
/* Create the domain instance for linear search. */
struct domain * domain_create_for_linear_search(struct rule_field_engine * rules[], uint32_t nr_rules,
uint16_t domain_index)
{
/* Allocate memory and initialize the domain instance */
struct domain * domain = ZMALLOC(sizeof(struct domain));
MR_VERIFY_MALLOC(domain);
/* Classify rules based on IP version. */
uint16_t nr_rules_v4, nr_rules_v6;
struct rule_field_engine * rules_v4[MAX_RULES];
struct rule_field_engine * rules_v6[MAX_RULES];
classify_rules_by_ip_version(rules, nr_rules, rules_v4, &nr_rules_v4, rules_v6, &nr_rules_v6);
domain->nr_rules_v4 = nr_rules_v4;
domain->nr_rules_v6 = nr_rules_v6;
/* Construct the linear_rule_ctx array for IPv4 */
if (nr_rules_v4 > 0)
{
struct linear_ctx_v4 * linear_ctx_v4 = ZMALLOC(sizeof(struct linear_ctx_v4) * nr_rules_v4);
MR_VERIFY_MALLOC(linear_ctx_v4);
domain->linear_ctx_v4 = linear_ctx_v4;
for (uint16_t index = 0; index < nr_rules_v4; index++)
{
struct rule_field_engine * rule = rules_v4[index];
struct action * action = &domain->action_list_v4[index + 1];
struct linear_ctx_v4 * ctx = &linear_ctx_v4[index];
ctx->priority = rule->priority;
ctx->action_index = index + 1;
ctx->proto = rule->proto & rule->proto_mask;
ctx->proto_mask = rule->proto_mask;
ctx->src_port_start = rule->src_port_start;
ctx->src_port_end = rule->src_port_end;
ctx->dst_port_start = rule->dst_port_start;
ctx->dst_port_end = rule->dst_port_end;
mask_length_to_mask_v4(rule->src_addr_mask_len, &ctx->src_addr_mask);
mask_length_to_mask_v4(rule->dst_addr_mask_len, &ctx->dst_addr_mask);
ctx->src_addr = rule->src_addr_v4 & ctx->src_addr_mask;
ctx->dst_addr = rule->dst_addr_v4 & ctx->dst_addr_mask;
memcpy(action, &rule->action, sizeof(struct action));
}
}
/* Construct the linear_rule_ctx array for IPv6 */
if (nr_rules_v6 > 0)
{
struct linear_ctx_v6 * linear_ctx_v6 = ZMALLOC(sizeof(struct linear_ctx_v6) * nr_rules_v6);
MR_VERIFY_MALLOC(linear_ctx_v6);
domain->linear_ctx_v6 = linear_ctx_v6;
for (uint16_t index = 0; index < nr_rules_v6; index++)
{
struct rule_field_engine * rule = rules_v6[index];
struct action * action = &domain->action_list_v6[index + 1];
struct linear_ctx_v6 * ctx = &linear_ctx_v6[index];
ctx->priority = rule->priority;
ctx->action_index = index + 1;
ctx->proto = rule->proto & rule->proto_mask;
ctx->proto_mask = rule->proto_mask;
ctx->src_port_start = rule->src_port_start;
ctx->src_port_end = rule->src_port_end;
ctx->dst_port_start = rule->dst_port_start;
ctx->dst_port_end = rule->dst_port_end;
mask_length_to_mask_v6(rule->src_addr_mask_len, ctx->src_addr_mask);
mask_length_to_mask_v6(rule->dst_addr_mask_len, ctx->dst_addr_mask);
for (int i = 0; i < 16; i++)
{
ctx->src_addr[i] = rule->u8_src_addr_v6[i] & ctx->src_addr_mask[i];
ctx->dst_addr[i] = rule->u8_dst_addr_v6[i] & ctx->dst_addr_mask[i];
}
memcpy(action, &rule->action, sizeof(struct action));
}
}
/* Evaluate the pass through condition */
evaluate_pass_through_condition(domain, rules_v4, nr_rules_v4, rules_v6, nr_rules_v6);
return domain;
}
void domain_array_free_for_linear_search(struct filter_engine * filter_engine)
{
for (uint16_t index = 0; index < RTE_DIM(filter_engine->domains); index++)
{
struct domain * domain = filter_engine->domains[index];
if (domain != NULL)
{
if (domain->linear_ctx_v4 != NULL)
{
FREE(domain->linear_ctx_v4);
domain->linear_ctx_v4 = NULL;
}
if (domain->linear_ctx_v6 != NULL)
{
FREE(domain->linear_ctx_v6);
domain->linear_ctx_v6 = NULL;
}
FREE(domain);
domain = NULL;
}
}
}
/* Create domain array */
int domain_array_create(struct filter_engine * filter_engine, struct rule_field_engine * rules, uint32_t nr_rules,
char * str_error_reason, unsigned int sz_error_reason)
{
/* Define the domain instance create function based on the engine type */
domain_create fn_domain_create;
switch (filter_engine->type)
{
case ENGINE_TYPE_TREE_SEARCH:
fn_domain_create = domain_create_for_tree_search;
break;
case ENGINE_TYPE_LINEAR_SEARCH:
fn_domain_create = domain_create_for_linear_search;
break;
default:
snprintf(str_error_reason, sz_error_reason - 1, "Unsupported engine type %d", filter_engine->type);
return RT_ERR;
}
/* Create each all domain instances. */
for (uint16_t domain_index = 0; domain_index < filter_engine->nr_domains; domain_index++)
{
/* Filtering rules to the current domain */
uint32_t nr_rules_in_domain = 0;
struct rule_field_engine * domain_rules[MAX_RULES];
for (uint32_t index = 0; index < nr_rules; index++)
{
struct rule_field_engine * rule = &rules[index];
if ((rule->domain == domain_index) || (rule->domain == UINT16_MAX))
{
domain_rules[nr_rules_in_domain] = rule;
nr_rules_in_domain++;
}
}
/* Skip the empty domain */
if (nr_rules_in_domain == 0)
continue;
/* Create domain instance based on the engine type */
struct domain * domain = fn_domain_create(domain_rules, nr_rules_in_domain, domain_index);
if (domain == NULL)
{
snprintf(str_error_reason, sz_error_reason - 1, "Failed to create domain instance,the domain field is: %d",
domain_index);
return RT_ERR;
}
/* Store the new created domain instance. */
filter_engine->domains[domain_index] = domain;
}
return RT_SUCCESS;
}
/* Free all domains */
void domain_array_free(struct filter_engine * filter_engine)
{
if (filter_engine->type == ENGINE_TYPE_TREE_SEARCH)
{
domain_array_free_for_tree_search(filter_engine);
}
else if (filter_engine->type == ENGINE_TYPE_LINEAR_SEARCH)
{
domain_array_free_for_linear_search(filter_engine);
}
}
/* Create a new filter engine instance based on the engine type and engine rule list. */
struct filter_engine * filter_engine_create(struct rule_field_engine * rules, uint32_t nr_rules, uint32_t nr_domains,
enum engine_algo_type type, char * str_error_reason,
unsigned int sz_error_reason)
{
/* Allocate memory for the engine. */
struct filter_engine * filter_engine = ZMALLOC(sizeof(struct filter_engine));
MR_VERIFY_MALLOC(filter_engine);
filter_engine->type = type;
filter_engine->nr_domains = nr_domains;
/* Create the pattern groups based on the engine type and engine rule list. */
int ret = domain_array_create(filter_engine, rules, nr_rules, str_error_reason, sz_error_reason);
if (ret != RT_SUCCESS)
{
domain_array_free(filter_engine);
FREE(filter_engine);
return NULL;
}
return filter_engine;
}
void filter_engine_free(struct filter_engine * filter_engine)
{
domain_array_free(filter_engine);
FREE(filter_engine);
}
/* Public func */
pkt_classifier_engine_t * pkt_classifier_engine_create(unsigned int nr_io_thread, cpu_set_t * cpu_set_io,
uint32_t nr_domains, enum ruleset_type type)
{
/* Check the ruleset type */
if (type >= RULESET_TYPE_MAX)
{
MR_ERROR("Ruleset type %d is not supported", type);
return NULL;
}
/* Allocate memory for the engine */
struct pkt_classifier_engine * engine = ZMALLOC(sizeof(struct pkt_classifier_engine));
MR_VERIFY_MALLOC(engine);
/* Define the ruleset type */
engine->ruleset_type = type;
engine->nr_domains = nr_domains;
/* Initialize RCU */
size_t sz = rte_rcu_qsbr_get_memsize(RTE_MAX_LCORE);
engine->qsv = (struct rte_rcu_qsbr *)rte_zmalloc(NULL, sz, RTE_CACHE_LINE_SIZE);
rte_rcu_qsbr_init(engine->qsv, RTE_MAX_LCORE);
for (uint32_t worker_id = 0; worker_id < nr_io_thread; worker_id++)
{
uint32_t lcore_id = cpu_set_location(cpu_set_io, worker_id);
rte_rcu_qsbr_thread_register(engine->qsv, lcore_id);
}
return (pkt_classifier_engine_t *)engine;
}
enum ruleset_type pkt_classifier_engine_get_ruleset_type(pkt_classifier_engine_t * engine)
{
return ((struct pkt_classifier_engine *)engine)->ruleset_type;
}
enum engine_algo_type get_engine_type(pkt_classifier_engine_t * engine)
{
return ((struct pkt_classifier_engine *)engine)->filter_engine->type;
}
void pkt_classifier_engine_fast_match_info_dump(pkt_classifier_engine_t * engine)
{
struct filter_engine * filter_engine = ((struct pkt_classifier_engine *)engine)->filter_engine;
for (uint16_t index = 0; index < RTE_DIM(filter_engine->domains); index++)
{
char str_info[2048] = {};
unsigned int max_len = sizeof(str_info) - 1;
struct domain * domain = filter_engine->domains[index];
if (domain != NULL)
{
int len = snprintf(str_info, max_len, "Domain: %3d, PassThrough:{ ", index);
if (domain->can_pass_through_domain)
{
len += snprintf(str_info + len, max_len - len, "Domain: Enable , ");
}
else
{
len += snprintf(str_info + len, max_len - len, "Domain: Disable, ");
}
if (domain->can_pass_through_v4)
{
len += snprintf(str_info + len, max_len - len, "IPv4: Enable , ");
}
else
{
len += snprintf(str_info + len, max_len - len, "IPv4: Disable, ");
}
if (domain->can_pass_through_v6)
{
len += snprintf(str_info + len, max_len - len, "IPv6: Enable }, ");
}
else
{
len += snprintf(str_info + len, max_len - len, "IPv6: Disable }, ");
}
len += snprintf(str_info + len, max_len - len, "IPv4 rules: %d, ", domain->nr_rules_v4);
len += snprintf(str_info + len, max_len - len, "IPv6 rules: %d", domain->nr_rules_v6);
MR_INFO("%s", str_info);
}
}
}
/* Build the classifier engine based on 'rule_field_engine'. */
int pkt_classifier_engine_build(pkt_classifier_engine_t * engine, struct rule_list_engine * rule_list_engine,
char * str_error_reason, unsigned int sz_error_reason)
{
/* Rule validity check. */
struct pkt_classifier_engine * pkt_classifier_engine = (struct pkt_classifier_engine *)engine;
uint16_t nr_rules = rule_list_engine->nr_rules;
uint32_t nr_domains = pkt_classifier_engine->nr_domains;
struct rule_field_engine * rules = rule_list_engine->rules;
int ret = rule_validity_check(rules, nr_rules, nr_domains);
if (ret != RT_SUCCESS)
return RT_ERR;
/* Duplicate rules check. */
ret = duplicate_rules_check(rules, nr_rules, str_error_reason, sz_error_reason);
if (ret != RT_SUCCESS)
return RT_ERR;
/* Calculate the engine type */
enum engine_algo_type engine_algo_type = (nr_rules > 100) ? ENGINE_TYPE_TREE_SEARCH : ENGINE_TYPE_LINEAR_SEARCH;
/* Initialize a new filter engine */
struct filter_engine * filter_engine_new =
filter_engine_create(rules, nr_rules, nr_domains, engine_algo_type, str_error_reason, sz_error_reason);
if (filter_engine_new == NULL)
return RT_ERR;
/* Retrieve the old filter engine*/
struct filter_engine * filter_engine_old = pkt_classifier_engine->filter_engine;
/* Update the filter engine */
pkt_classifier_engine->filter_engine = filter_engine_new;
/* Synchronize RCU and free the old filter engine */
rte_rcu_qsbr_synchronize(pkt_classifier_engine->qsv, RTE_QSBR_THRID_INVALID);
if (filter_engine_old != NULL)
filter_engine_free(filter_engine_old);
return RT_SUCCESS;
}
uint16_t outer_ether_type_get(struct pkt_parser_result * result, uint16_t * out_pkt_layers)
{
for (int i = result->start_layers; i < result->nr_layers; i++)
{
struct pkt_layer_result * layer_result = &result->layers[i];
if (layer_result->type_id == LAYER_TYPE_ID_IPV4)
{
*out_pkt_layers = i;
return LAYER_TYPE_ID_IPV4;
}
else if (layer_result->type_id == LAYER_TYPE_ID_IPV6)
{
*out_pkt_layers = i;
return LAYER_TYPE_ID_IPV6;
}
}
return 0;
}
/* Match field data prepare for tree search */
int match_field_data_prepare_for_tree(const char * mbuf_data, struct pkt_parser_result * pkt_parser_result,
uint16_t pkt_start_layers, struct match_field_tree * out_match_field)
{
unsigned int match_field_addr_set = 0;
unsigned int match_field_port_set = 0;
for (unsigned int i = pkt_start_layers; i < pkt_parser_result->nr_layers; i++)
{
struct pkt_layer_result * layer_result = &pkt_parser_result->layers[i];
if ((layer_result->type_id == LAYER_TYPE_ID_IPV4) && (match_field_addr_set == 0))
{
struct rte_ipv4_hdr * ipv4_hdr = (struct rte_ipv4_hdr *)(RTE_PTR_ADD(mbuf_data, layer_result->offset));
out_match_field->src_addr_ipv4 = ipv4_hdr->src_addr;
out_match_field->dst_addr_ipv4 = ipv4_hdr->dst_addr;
out_match_field->proto = ipv4_hdr->next_proto_id;
out_match_field->ether_type = RTE_ETHER_TYPE_IPV4;
match_field_addr_set = 1;
}
else if ((layer_result->type_id == LAYER_TYPE_ID_IPV6) && match_field_addr_set == 0)
{
struct rte_ipv6_hdr * ipv6_hdr = (struct rte_ipv6_hdr *)(RTE_PTR_ADD(mbuf_data, layer_result->offset));
memcpy(out_match_field->u8_src_addr_ipv6, ipv6_hdr->src_addr, sizeof(out_match_field->u8_src_addr_ipv6));
memcpy(out_match_field->u8_dst_addr_ipv6, ipv6_hdr->dst_addr, sizeof(out_match_field->u8_dst_addr_ipv6));
out_match_field->proto = ipv6_hdr->proto;
out_match_field->ether_type = RTE_ETHER_TYPE_IPV6;
match_field_addr_set = 1;
}
else if ((layer_result->type_id == LAYER_TYPE_ID_UDP) && match_field_port_set == 0)
{
struct rte_udp_hdr * udp_hdr = (struct rte_udp_hdr *)(RTE_PTR_ADD(mbuf_data, layer_result->offset));
out_match_field->src_port = udp_hdr->src_port;
out_match_field->dst_port = udp_hdr->dst_port;
match_field_port_set = 1;
}
else if ((layer_result->type_id == LAYER_TYPE_ID_TCP) && match_field_port_set == 0)
{
struct rte_tcp_hdr * tcp_hdr = (struct rte_tcp_hdr *)(RTE_PTR_ADD(mbuf_data, layer_result->offset));
out_match_field->src_port = tcp_hdr->src_port;
out_match_field->dst_port = tcp_hdr->dst_port;
match_field_port_set = 1;
}
}
if (unlikely(match_field_addr_set == 0))
{
return RT_ERR;
}
if (unlikely(match_field_port_set == 0))
{
out_match_field->src_port = 0;
out_match_field->dst_port = 0;
}
return RT_SUCCESS;
}
/* Match field data prepare for linear search */
int match_field_data_prepare_for_linear(const char * mbuf_data, struct pkt_parser_result * pkt_parser_result,
uint16_t pkt_start_layers, struct match_field_linear * out_match_field)
{
unsigned int match_field_addr_set = 0;
unsigned int match_field_port_set = 0;
for (unsigned int i = pkt_start_layers; i < pkt_parser_result->nr_layers; i++)
{
struct pkt_layer_result * layer_result = &pkt_parser_result->layers[i];
if ((layer_result->type_id == LAYER_TYPE_ID_IPV4) && (match_field_addr_set == 0))
{
struct rte_ipv4_hdr * ipv4_hdr = (struct rte_ipv4_hdr *)(RTE_PTR_ADD(mbuf_data, layer_result->offset));
out_match_field->proto = ipv4_hdr->next_proto_id;
out_match_field->ether_type = RTE_ETHER_TYPE_IPV4;
out_match_field->ipv4_hdr = ipv4_hdr;
match_field_addr_set = 1;
}
else if ((layer_result->type_id == LAYER_TYPE_ID_IPV6) && match_field_addr_set == 0)
{
struct rte_ipv6_hdr * ipv6_hdr = (struct rte_ipv6_hdr *)(RTE_PTR_ADD(mbuf_data, layer_result->offset));
out_match_field->proto = ipv6_hdr->proto;
out_match_field->ether_type = RTE_ETHER_TYPE_IPV6;
out_match_field->ipv6_hdr = ipv6_hdr;
match_field_addr_set = 1;
}
else if ((layer_result->type_id == LAYER_TYPE_ID_UDP) && match_field_port_set == 0)
{
struct rte_udp_hdr * udp_hdr = (struct rte_udp_hdr *)(RTE_PTR_ADD(mbuf_data, layer_result->offset));
out_match_field->src_port = udp_hdr->src_port;
out_match_field->dst_port = udp_hdr->dst_port;
match_field_port_set = 1;
}
else if ((layer_result->type_id == LAYER_TYPE_ID_TCP) && match_field_port_set == 0)
{
struct rte_tcp_hdr * tcp_hdr = (struct rte_tcp_hdr *)(RTE_PTR_ADD(mbuf_data, layer_result->offset));
out_match_field->src_port = tcp_hdr->src_port;
out_match_field->dst_port = tcp_hdr->dst_port;
match_field_port_set = 1;
}
}
if (unlikely(match_field_addr_set == 0))
{
return RT_ERR;
}
if (unlikely(match_field_port_set == 0))
{
out_match_field->src_port = 0;
out_match_field->dst_port = 0;
}
return RT_SUCCESS;
}
int ipv6_addr_match(struct linear_ctx_v6 * ctx, uint8_t src_addr[], uint8_t dst_addr[])
{
for (uint8_t index = 0; index < 16; index++)
{
if (ctx->src_addr[index] != (src_addr[index] & ctx->src_addr_mask[index]))
return RT_ERR;
if (ctx->dst_addr[index] != (dst_addr[index] & ctx->dst_addr_mask[index]))
return RT_ERR;
}
return RT_SUCCESS;
}
uint16_t linear_search(struct domain * domain, struct match_field_linear * match_field, uint32_t res_array[])
{
uint16_t nr_match = 0;
if (match_field->ether_type == RTE_ETHER_TYPE_IPV4)
{
uint16_t nr_rules = domain->nr_rules_v4;
struct rte_ipv4_hdr * ipv4_hdr = match_field->ipv4_hdr;
for (uint16_t index = 0; index < nr_rules; index++)
{
struct linear_ctx_v4 * ctx = &domain->linear_ctx_v4[index];
if (ctx->src_addr != (ipv4_hdr->src_addr & ctx->src_addr_mask))
continue;
if (ctx->dst_addr != (ipv4_hdr->dst_addr & ctx->dst_addr_mask))
continue;
if (ctx->proto != (match_field->proto & ctx->proto_mask))
continue;
uint16_t src_port = ntohs(match_field->src_port);
if (ctx->src_port_start > src_port || ctx->src_port_end < src_port)
continue;
uint16_t dst_port = ntohs(match_field->dst_port);
if (ctx->dst_port_start > dst_port || ctx->dst_port_end < dst_port)
continue;
res_array[ctx->priority] = ctx->action_index;
nr_match++;
}
}
else if (match_field->ether_type == RTE_ETHER_TYPE_IPV6)
{
uint16_t nr_rules = domain->nr_rules_v6;
struct rte_ipv6_hdr * ipv6_hdr = match_field->ipv6_hdr;
for (uint16_t index = 0; index < nr_rules; index++)
{
struct linear_ctx_v6 * ctx = &domain->linear_ctx_v6[index];
if (ipv6_addr_match(ctx, ipv6_hdr->src_addr, ipv6_hdr->dst_addr) != RT_SUCCESS)
continue;
if (ctx->proto != (match_field->proto & ctx->proto_mask))
continue;
uint16_t src_port = ntohs(match_field->src_port);
if (ctx->src_port_start > src_port || ctx->src_port_end < src_port)
continue;
uint16_t dst_port = ntohs(match_field->dst_port);
if (ctx->dst_port_start > dst_port || ctx->dst_port_end < dst_port)
continue;
res_array[ctx->priority] = ctx->action_index;
nr_match++;
}
}
else
{
return RT_ERR;
}
return nr_match;
}
/* Match multiple packets with the Linear search engine */
void packets_match(struct filter_engine * filter_engine, struct rte_mbuf * mbufs[], uint16_t nr_mbufs,
uint16_t domain_field_for_pkts[], struct match_result_engine result[])
{
uint16_t can_pass_through_domain = 0;
uint16_t last_domain_field = UINT16_MAX;
struct domain * domain = NULL;
for (uint16_t mbuf_index = 0; mbuf_index < nr_mbufs; mbuf_index++)
{
struct match_result_engine * match_result = &result[mbuf_index];
/* Retrieve the current domain field for the packet */
uint16_t current_domain_field = domain_field_for_pkts[mbuf_index];
assert(current_domain_field <= filter_engine->nr_domains);
/* If the domain field changes, update the domain instance and also update the last domain field
* and check pass through for the new domain instance */
if (unlikely(last_domain_field != current_domain_field))
{
domain = filter_engine->domains[current_domain_field];
if (domain == NULL)
{
match_result->nr_actions = 0;
continue;
}
last_domain_field = current_domain_field;
can_pass_through_domain = domain->can_pass_through_domain;
}
/* If the domain instance enables 'pass through', save the pass-through action and continue to the next packet*/
if (likely(can_pass_through_domain))
{
memcpy(&match_result->actions[0], &domain->pass_through_action_domain, sizeof(struct action));
match_result->nr_actions = 1;
continue;
}
/* Obtain the outer EtherType based on the packet parser result */
uint16_t pkt_start_layers;
struct rte_mbuf * mbuf = mbufs[mbuf_index];
struct mrb_metadata * mrb_metadata = mrbuf_cz_data(mbuf, MR_NODE_CTRLZONE_ID);
struct pkt_parser_result * pkt_parser_result = &mrb_metadata->pkt_parser_result;
uint16_t outer_ether_type = outer_ether_type_get(pkt_parser_result, &pkt_start_layers);
/* Retrieve ACL context based on the EtherType */
void * ctx = NULL;
struct action * action_list = NULL;
if (outer_ether_type == LAYER_TYPE_ID_IPV4)
{
/* If context-level pass-through is enabled, save the action and proceed to the next packet. */
if (domain->can_pass_through_v4)
{
memcpy(&match_result->actions[0], &domain->pass_through_action_v4, sizeof(struct action));
match_result->nr_actions = 1;
continue;
}
ctx = domain->ctx_v4;
action_list = domain->action_list_v4;
}
else if (outer_ether_type == LAYER_TYPE_ID_IPV6)
{
/* If context-level pass-through is enabled, save the action and proceed to the next packet. */
if (domain->can_pass_through_v6)
{
memcpy(&match_result->actions[0], &domain->pass_through_action_v6, sizeof(struct action));
match_result->nr_actions = 1;
continue;
}
ctx = domain->ctx_v6;
action_list = domain->action_list_v6;
}
/* If 'ctx' is null, it means the domain instance has no rules, indicating a packet match miss */
if (unlikely(ctx == NULL))
{
match_result->nr_actions = 0;
continue;
}
uint16_t nr_res = 16;
uint32_t res_array[16] = {0};
if (filter_engine->type == ENGINE_TYPE_TREE_SEARCH)
{
/* Prepare the match field data */
struct match_field_tree match_field;
const char * mbuf_data = rte_pktmbuf_mtod(mbuf, const char *);
int ret = match_field_data_prepare_for_tree(mbuf_data, pkt_parser_result, pkt_start_layers, &match_field);
if (unlikely(ret != RT_SUCCESS))
{
match_result->nr_actions = 0;
continue;
}
/* Initialize ACL result array and generate the ACL match field */
const uint8_t * acl_match_field[1];
acl_match_field[0] = (const uint8_t *)&match_field;
rte_acl_classify((struct rte_acl_ctx *)ctx, acl_match_field, res_array, 1, 16);
}
else if (filter_engine->type == ENGINE_TYPE_LINEAR_SEARCH)
{
/* Prepare the match field data */
struct match_field_linear match_field;
const char * mbuf_data = rte_pktmbuf_mtod(mbuf, const char *);
int ret = match_field_data_prepare_for_linear(mbuf_data, pkt_parser_result, pkt_start_layers, &match_field);
if (unlikely(ret != RT_SUCCESS))
{
match_result->nr_actions = 0;
continue;
}
/* Linear search */
nr_res = linear_search(domain, &match_field, res_array);
if (nr_res == 0)
{
match_result->nr_actions = 0;
continue;
}
}
else
{
match_result->nr_actions = 0;
continue;
}
/* Parser results */
match_result->nr_actions = 0;
for (uint16_t index = 0; index < RTE_DIM(res_array); index++)
{
uint32_t action_index = res_array[index];
if (action_index == 0)
continue;
memcpy(&match_result->actions[match_result->nr_actions], &action_list[action_index], sizeof(struct action));
match_result->nr_actions++;
if (match_result->nr_actions == nr_res)
break;
}
}
}
/* Match multiple packets with the Packet Classifier Engine */
int pkt_classifier_engine_multi_match(pkt_classifier_engine_t * engine_instance, uint32_t lcore_id,
struct rte_mbuf * mbufs[], uint16_t nr_mbufs, uint16_t domain_field_for_pkts[],
struct match_result_engine result[])
{
/* Verify the number of mbufs */
assert(nr_mbufs <= 16);
/* Set rcu thread online */
struct pkt_classifier_engine * engine = (struct pkt_classifier_engine *)engine_instance;
struct rte_rcu_qsbr * qsv = engine->qsv;
rte_rcu_qsbr_thread_online(qsv, lcore_id);
/* Invoke packet matching function */
packets_match(engine->filter_engine, mbufs, nr_mbufs, domain_field_for_pkts, result);
/* Update the quiescent state counter and mark it as offline */
rte_rcu_qsbr_quiescent(qsv, lcore_id);
rte_rcu_qsbr_thread_offline(qsv, lcore_id);
return RT_SUCCESS;
}
/* Dump all rules information. */
void pkt_classifier_engine_info_dump(pkt_classifier_engine_t * engine_instance)
{
/* Dump the fast match info */
pkt_classifier_engine_fast_match_info_dump(engine_instance);
/* Dump the ruleset type */
enum ruleset_type ruleset_type = pkt_classifier_engine_get_ruleset_type(engine_instance);
if (ruleset_type == RULESET_TYPE_CLASSIFIER)
{
MR_INFO("Pkt classifier ruleset type: Classifier");
}
else if (ruleset_type == RULESET_TYPE_PORT)
{
MR_INFO("Pkt classifier ruleset type: Port");
}
/* Dump the engine type */
enum engine_algo_type engine_algo_type = get_engine_type(engine_instance);
if (engine_algo_type == ENGINE_TYPE_TREE_SEARCH)
{
MR_INFO("Pkt classifier engine type: Tree search");
}
else if (engine_algo_type == ENGINE_TYPE_LINEAR_SEARCH)
{
MR_INFO("Pkt classifier engine type: Linear search");
}
}
|