summaryrefslogtreecommitdiffstats
path: root/drivers/net/enic/enic_flow.c
blob: 0cf04aefd7b6bb3cae7335893904ed1f1042c288 (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
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
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright 2008-2017 Cisco Systems, Inc.  All rights reserved.
 */

#include <errno.h>
#include <stdint.h>
#include <rte_log.h>
#include <rte_ethdev_driver.h>
#include <rte_flow_driver.h>
#include <rte_ether.h>
#include <rte_ip.h>
#include <rte_udp.h>

#include "enic_compat.h"
#include "enic.h"
#include "vnic_dev.h"
#include "vnic_nic.h"

#define FLOW_TRACE() \
	rte_log(RTE_LOG_DEBUG, enicpmd_logtype_flow, \
		"%s()\n", __func__)
#define FLOW_LOG(level, fmt, args...) \
	rte_log(RTE_LOG_ ## level, enicpmd_logtype_flow, \
		fmt "\n", ##args)

/** Info about how to copy items into enic filters. */
struct enic_items {
	/** Function for copying and validating an item. */
	int (*copy_item)(const struct rte_flow_item *item,
			 struct filter_v2 *enic_filter, u8 *inner_ofst);
	/** List of valid previous items. */
	const enum rte_flow_item_type * const prev_items;
	/** True if it's OK for this item to be the first item. For some NIC
	 * versions, it's invalid to start the stack above layer 3.
	 */
	const u8 valid_start_item;
};

/** Filtering capabilities for various NIC and firmware versions. */
struct enic_filter_cap {
	/** list of valid items and their handlers and attributes. */
	const struct enic_items *item_info;
};

/* functions for copying flow actions into enic actions */
typedef int (copy_action_fn)(const struct rte_flow_action actions[],
			     struct filter_action_v2 *enic_action);

/* functions for copying items into enic filters */
typedef int(enic_copy_item_fn)(const struct rte_flow_item *item,
			  struct filter_v2 *enic_filter, u8 *inner_ofst);

/** Action capabilities for various NICs. */
struct enic_action_cap {
	/** list of valid actions */
	const enum rte_flow_action_type *actions;
	/** copy function for a particular NIC */
	int (*copy_fn)(const struct rte_flow_action actions[],
		       struct filter_action_v2 *enic_action);
};

/* Forward declarations */
static enic_copy_item_fn enic_copy_item_ipv4_v1;
static enic_copy_item_fn enic_copy_item_udp_v1;
static enic_copy_item_fn enic_copy_item_tcp_v1;
static enic_copy_item_fn enic_copy_item_eth_v2;
static enic_copy_item_fn enic_copy_item_vlan_v2;
static enic_copy_item_fn enic_copy_item_ipv4_v2;
static enic_copy_item_fn enic_copy_item_ipv6_v2;
static enic_copy_item_fn enic_copy_item_udp_v2;
static enic_copy_item_fn enic_copy_item_tcp_v2;
static enic_copy_item_fn enic_copy_item_sctp_v2;
static enic_copy_item_fn enic_copy_item_sctp_v2;
static enic_copy_item_fn enic_copy_item_vxlan_v2;
static copy_action_fn enic_copy_action_v1;
static copy_action_fn enic_copy_action_v2;

/**
 * Legacy NICs or NICs with outdated firmware. Only 5-tuple perfect match
 * is supported.
 */
static const struct enic_items enic_items_v1[] = {
	[RTE_FLOW_ITEM_TYPE_IPV4] = {
		.copy_item = enic_copy_item_ipv4_v1,
		.valid_start_item = 1,
		.prev_items = (const enum rte_flow_item_type[]) {
			       RTE_FLOW_ITEM_TYPE_END,
		},
	},
	[RTE_FLOW_ITEM_TYPE_UDP] = {
		.copy_item = enic_copy_item_udp_v1,
		.valid_start_item = 0,
		.prev_items = (const enum rte_flow_item_type[]) {
			       RTE_FLOW_ITEM_TYPE_IPV4,
			       RTE_FLOW_ITEM_TYPE_END,
		},
	},
	[RTE_FLOW_ITEM_TYPE_TCP] = {
		.copy_item = enic_copy_item_tcp_v1,
		.valid_start_item = 0,
		.prev_items = (const enum rte_flow_item_type[]) {
			       RTE_FLOW_ITEM_TYPE_IPV4,
			       RTE_FLOW_ITEM_TYPE_END,
		},
	},
};

/**
 * NICs have Advanced Filters capability but they are disabled. This means
 * that layer 3 must be specified.
 */
static const struct enic_items enic_items_v2[] = {
	[RTE_FLOW_ITEM_TYPE_ETH] = {
		.copy_item = enic_copy_item_eth_v2,
		.valid_start_item = 1,
		.prev_items = (const enum rte_flow_item_type[]) {
			       RTE_FLOW_ITEM_TYPE_VXLAN,
			       RTE_FLOW_ITEM_TYPE_END,
		},
	},
	[RTE_FLOW_ITEM_TYPE_VLAN] = {
		.copy_item = enic_copy_item_vlan_v2,
		.valid_start_item = 1,
		.prev_items = (const enum rte_flow_item_type[]) {
			       RTE_FLOW_ITEM_TYPE_ETH,
			       RTE_FLOW_ITEM_TYPE_END,
		},
	},
	[RTE_FLOW_ITEM_TYPE_IPV4] = {
		.copy_item = enic_copy_item_ipv4_v2,
		.valid_start_item = 1,
		.prev_items = (const enum rte_flow_item_type[]) {
			       RTE_FLOW_ITEM_TYPE_ETH,
			       RTE_FLOW_ITEM_TYPE_VLAN,
			       RTE_FLOW_ITEM_TYPE_END,
		},
	},
	[RTE_FLOW_ITEM_TYPE_IPV6] = {
		.copy_item = enic_copy_item_ipv6_v2,
		.valid_start_item = 1,
		.prev_items = (const enum rte_flow_item_type[]) {
			       RTE_FLOW_ITEM_TYPE_ETH,
			       RTE_FLOW_ITEM_TYPE_VLAN,
			       RTE_FLOW_ITEM_TYPE_END,
		},
	},
	[RTE_FLOW_ITEM_TYPE_UDP] = {
		.copy_item = enic_copy_item_udp_v2,
		.valid_start_item = 0,
		.prev_items = (const enum rte_flow_item_type[]) {
			       RTE_FLOW_ITEM_TYPE_IPV4,
			       RTE_FLOW_ITEM_TYPE_IPV6,
			       RTE_FLOW_ITEM_TYPE_END,
		},
	},
	[RTE_FLOW_ITEM_TYPE_TCP] = {
		.copy_item = enic_copy_item_tcp_v2,
		.valid_start_item = 0,
		.prev_items = (const enum rte_flow_item_type[]) {
			       RTE_FLOW_ITEM_TYPE_IPV4,
			       RTE_FLOW_ITEM_TYPE_IPV6,
			       RTE_FLOW_ITEM_TYPE_END,
		},
	},
	[RTE_FLOW_ITEM_TYPE_SCTP] = {
		.copy_item = enic_copy_item_sctp_v2,
		.valid_start_item = 0,
		.prev_items = (const enum rte_flow_item_type[]) {
			       RTE_FLOW_ITEM_TYPE_IPV4,
			       RTE_FLOW_ITEM_TYPE_IPV6,
			       RTE_FLOW_ITEM_TYPE_END,
		},
	},
	[RTE_FLOW_ITEM_TYPE_VXLAN] = {
		.copy_item = enic_copy_item_vxlan_v2,
		.valid_start_item = 0,
		.prev_items = (const enum rte_flow_item_type[]) {
			       RTE_FLOW_ITEM_TYPE_UDP,
			       RTE_FLOW_ITEM_TYPE_END,
		},
	},
};

/** NICs with Advanced filters enabled */
static const struct enic_items enic_items_v3[] = {
	[RTE_FLOW_ITEM_TYPE_ETH] = {
		.copy_item = enic_copy_item_eth_v2,
		.valid_start_item = 1,
		.prev_items = (const enum rte_flow_item_type[]) {
			       RTE_FLOW_ITEM_TYPE_VXLAN,
			       RTE_FLOW_ITEM_TYPE_END,
		},
	},
	[RTE_FLOW_ITEM_TYPE_VLAN] = {
		.copy_item = enic_copy_item_vlan_v2,
		.valid_start_item = 1,
		.prev_items = (const enum rte_flow_item_type[]) {
			       RTE_FLOW_ITEM_TYPE_ETH,
			       RTE_FLOW_ITEM_TYPE_END,
		},
	},
	[RTE_FLOW_ITEM_TYPE_IPV4] = {
		.copy_item = enic_copy_item_ipv4_v2,
		.valid_start_item = 1,
		.prev_items = (const enum rte_flow_item_type[]) {
			       RTE_FLOW_ITEM_TYPE_ETH,
			       RTE_FLOW_ITEM_TYPE_VLAN,
			       RTE_FLOW_ITEM_TYPE_END,
		},
	},
	[RTE_FLOW_ITEM_TYPE_IPV6] = {
		.copy_item = enic_copy_item_ipv6_v2,
		.valid_start_item = 1,
		.prev_items = (const enum rte_flow_item_type[]) {
			       RTE_FLOW_ITEM_TYPE_ETH,
			       RTE_FLOW_ITEM_TYPE_VLAN,
			       RTE_FLOW_ITEM_TYPE_END,
		},
	},
	[RTE_FLOW_ITEM_TYPE_UDP] = {
		.copy_item = enic_copy_item_udp_v2,
		.valid_start_item = 1,
		.prev_items = (const enum rte_flow_item_type[]) {
			       RTE_FLOW_ITEM_TYPE_IPV4,
			       RTE_FLOW_ITEM_TYPE_IPV6,
			       RTE_FLOW_ITEM_TYPE_END,
		},
	},
	[RTE_FLOW_ITEM_TYPE_TCP] = {
		.copy_item = enic_copy_item_tcp_v2,
		.valid_start_item = 1,
		.prev_items = (const enum rte_flow_item_type[]) {
			       RTE_FLOW_ITEM_TYPE_IPV4,
			       RTE_FLOW_ITEM_TYPE_IPV6,
			       RTE_FLOW_ITEM_TYPE_END,
		},
	},
	[RTE_FLOW_ITEM_TYPE_SCTP] = {
		.copy_item = enic_copy_item_sctp_v2,
		.valid_start_item = 1,
		.prev_items = (const enum rte_flow_item_type[]) {
			       RTE_FLOW_ITEM_TYPE_IPV4,
			       RTE_FLOW_ITEM_TYPE_IPV6,
			       RTE_FLOW_ITEM_TYPE_END,
		},
	},
	[RTE_FLOW_ITEM_TYPE_VXLAN] = {
		.copy_item = enic_copy_item_vxlan_v2,
		.valid_start_item = 1,
		.prev_items = (const enum rte_flow_item_type[]) {
			       RTE_FLOW_ITEM_TYPE_UDP,
			       RTE_FLOW_ITEM_TYPE_END,
		},
	},
};

/** Filtering capabilities indexed this NICs supported filter type. */
static const struct enic_filter_cap enic_filter_cap[] = {
	[FILTER_IPV4_5TUPLE] = {
		.item_info = enic_items_v1,
	},
	[FILTER_USNIC_IP] = {
		.item_info = enic_items_v2,
	},
	[FILTER_DPDK_1] = {
		.item_info = enic_items_v3,
	},
};

/** Supported actions for older NICs */
static const enum rte_flow_action_type enic_supported_actions_v1[] = {
	RTE_FLOW_ACTION_TYPE_QUEUE,
	RTE_FLOW_ACTION_TYPE_END,
};

/** Supported actions for newer NICs */
static const enum rte_flow_action_type enic_supported_actions_v2_id[] = {
	RTE_FLOW_ACTION_TYPE_QUEUE,
	RTE_FLOW_ACTION_TYPE_MARK,
	RTE_FLOW_ACTION_TYPE_FLAG,
	RTE_FLOW_ACTION_TYPE_END,
};

static const enum rte_flow_action_type enic_supported_actions_v2_drop[] = {
	RTE_FLOW_ACTION_TYPE_QUEUE,
	RTE_FLOW_ACTION_TYPE_MARK,
	RTE_FLOW_ACTION_TYPE_FLAG,
	RTE_FLOW_ACTION_TYPE_DROP,
	RTE_FLOW_ACTION_TYPE_END,
};

/** Action capabilities indexed by NIC version information */
static const struct enic_action_cap enic_action_cap[] = {
	[FILTER_ACTION_RQ_STEERING_FLAG] = {
		.actions = enic_supported_actions_v1,
		.copy_fn = enic_copy_action_v1,
	},
	[FILTER_ACTION_FILTER_ID_FLAG] = {
		.actions = enic_supported_actions_v2_id,
		.copy_fn = enic_copy_action_v2,
	},
	[FILTER_ACTION_DROP_FLAG] = {
		.actions = enic_supported_actions_v2_drop,
		.copy_fn = enic_copy_action_v2,
	},
};

static int
mask_exact_match(const u8 *supported, const u8 *supplied,
		 unsigned int size)
{
	unsigned int i;
	for (i = 0; i < size; i++) {
		if (supported[i] != supplied[i])
			return 0;
	}
	return 1;
}

/**
 * Copy IPv4 item into version 1 NIC filter.
 *
 * @param item[in]
 *   Item specification.
 * @param enic_filter[out]
 *   Partially filled in NIC filter structure.
 * @param inner_ofst[in]
 *   Should always be 0 for version 1.
 */
static int
enic_copy_item_ipv4_v1(const struct rte_flow_item *item,
		       struct filter_v2 *enic_filter, u8 *inner_ofst)
{
	const struct rte_flow_item_ipv4 *spec = item->spec;
	const struct rte_flow_item_ipv4 *mask = item->mask;
	struct filter_ipv4_5tuple *enic_5tup = &enic_filter->u.ipv4;
	struct ipv4_hdr supported_mask = {
		.src_addr = 0xffffffff,
		.dst_addr = 0xffffffff,
	};

	FLOW_TRACE();

	if (*inner_ofst)
		return ENOTSUP;

	if (!mask)
		mask = &rte_flow_item_ipv4_mask;

	/* This is an exact match filter, both fields must be set */
	if (!spec || !spec->hdr.src_addr || !spec->hdr.dst_addr) {
		FLOW_LOG(ERR, "IPv4 exact match src/dst addr");
		return ENOTSUP;
	}

	/* check that the suppied mask exactly matches capabilty */
	if (!mask_exact_match((const u8 *)&supported_mask,
			      (const u8 *)item->mask, sizeof(*mask))) {
		FLOW_LOG(ERR, "IPv4 exact match mask");
		return ENOTSUP;
	}

	enic_filter->u.ipv4.flags = FILTER_FIELDS_IPV4_5TUPLE;
	enic_5tup->src_addr = spec->hdr.src_addr;
	enic_5tup->dst_addr = spec->hdr.dst_addr;

	return 0;
}

/**
 * Copy UDP item into version 1 NIC filter.
 *
 * @param item[in]
 *   Item specification.
 * @param enic_filter[out]
 *   Partially filled in NIC filter structure.
 * @param inner_ofst[in]
 *   Should always be 0 for version 1.
 */
static int
enic_copy_item_udp_v1(const struct rte_flow_item *item,
		      struct filter_v2 *enic_filter, u8 *inner_ofst)
{
	const struct rte_flow_item_udp *spec = item->spec;
	const struct rte_flow_item_udp *mask = item->mask;
	struct filter_ipv4_5tuple *enic_5tup = &enic_filter->u.ipv4;
	struct udp_hdr supported_mask = {
		.src_port = 0xffff,
		.dst_port = 0xffff,
	};

	FLOW_TRACE();

	if (*inner_ofst)
		return ENOTSUP;

	if (!mask)
		mask = &rte_flow_item_udp_mask;

	/* This is an exact match filter, both ports must be set */
	if (!spec || !spec->hdr.src_port || !spec->hdr.dst_port) {
		FLOW_LOG(ERR, "UDP exact match src/dst addr");
		return ENOTSUP;
	}

	/* check that the suppied mask exactly matches capabilty */
	if (!mask_exact_match((const u8 *)&supported_mask,
			      (const u8 *)item->mask, sizeof(*mask))) {
		FLOW_LOG(ERR, "UDP exact match mask");
		return ENOTSUP;
	}

	enic_filter->u.ipv4.flags = FILTER_FIELDS_IPV4_5TUPLE;
	enic_5tup->src_port = spec->hdr.src_port;
	enic_5tup->dst_port = spec->hdr.dst_port;
	enic_5tup->protocol = PROTO_UDP;

	return 0;
}

/**
 * Copy TCP item into version 1 NIC filter.
 *
 * @param item[in]
 *   Item specification.
 * @param enic_filter[out]
 *   Partially filled in NIC filter structure.
 * @param inner_ofst[in]
 *   Should always be 0 for version 1.
 */
static int
enic_copy_item_tcp_v1(const struct rte_flow_item *item,
		      struct filter_v2 *enic_filter, u8 *inner_ofst)
{
	const struct rte_flow_item_tcp *spec = item->spec;
	const struct rte_flow_item_tcp *mask = item->mask;
	struct filter_ipv4_5tuple *enic_5tup = &enic_filter->u.ipv4;
	struct tcp_hdr supported_mask = {
		.src_port = 0xffff,
		.dst_port = 0xffff,
	};

	FLOW_TRACE();

	if (*inner_ofst)
		return ENOTSUP;

	if (!mask)
		mask = &rte_flow_item_tcp_mask;

	/* This is an exact match filter, both ports must be set */
	if (!spec || !spec->hdr.src_port || !spec->hdr.dst_port) {
		FLOW_LOG(ERR, "TCPIPv4 exact match src/dst addr");
		return ENOTSUP;
	}

	/* check that the suppied mask exactly matches capabilty */
	if (!mask_exact_match((const u8 *)&supported_mask,
			     (const u8 *)item->mask, sizeof(*mask))) {
		FLOW_LOG(ERR, "TCP exact match mask");
		return ENOTSUP;
	}

	enic_filter->u.ipv4.flags = FILTER_FIELDS_IPV4_5TUPLE;
	enic_5tup->src_port = spec->hdr.src_port;
	enic_5tup->dst_port = spec->hdr.dst_port;
	enic_5tup->protocol = PROTO_TCP;

	return 0;
}

/**
 * Copy ETH item into version 2 NIC filter.
 *
 * @param item[in]
 *   Item specification.
 * @param enic_filter[out]
 *   Partially filled in NIC filter structure.
 * @param inner_ofst[in]
 *   If zero, this is an outer header. If non-zero, this is the offset into L5
 *   where the header begins.
 */
static int
enic_copy_item_eth_v2(const struct rte_flow_item *item,
		      struct filter_v2 *enic_filter, u8 *inner_ofst)
{
	struct ether_hdr enic_spec;
	struct ether_hdr enic_mask;
	const struct rte_flow_item_eth *spec = item->spec;
	const struct rte_flow_item_eth *mask = item->mask;
	struct filter_generic_1 *gp = &enic_filter->u.generic_1;

	FLOW_TRACE();

	/* Match all if no spec */
	if (!spec)
		return 0;

	if (!mask)
		mask = &rte_flow_item_eth_mask;

	memcpy(enic_spec.d_addr.addr_bytes, spec->dst.addr_bytes,
	       ETHER_ADDR_LEN);
	memcpy(enic_spec.s_addr.addr_bytes, spec->src.addr_bytes,
	       ETHER_ADDR_LEN);

	memcpy(enic_mask.d_addr.addr_bytes, mask->dst.addr_bytes,
	       ETHER_ADDR_LEN);
	memcpy(enic_mask.s_addr.addr_bytes, mask->src.addr_bytes,
	       ETHER_ADDR_LEN);
	enic_spec.ether_type = spec->type;
	enic_mask.ether_type = mask->type;

	if (*inner_ofst == 0) {
		/* outer header */
		memcpy(gp->layer[FILTER_GENERIC_1_L2].mask, &enic_mask,
		       sizeof(struct ether_hdr));
		memcpy(gp->layer[FILTER_GENERIC_1_L2].val, &enic_spec,
		       sizeof(struct ether_hdr));
	} else {
		/* inner header */
		if ((*inner_ofst + sizeof(struct ether_hdr)) >
		     FILTER_GENERIC_1_KEY_LEN)
			return ENOTSUP;
		/* Offset into L5 where inner Ethernet header goes */
		memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
		       &enic_mask, sizeof(struct ether_hdr));
		memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
		       &enic_spec, sizeof(struct ether_hdr));
		*inner_ofst += sizeof(struct ether_hdr);
	}
	return 0;
}

/**
 * Copy VLAN item into version 2 NIC filter.
 *
 * @param item[in]
 *   Item specification.
 * @param enic_filter[out]
 *   Partially filled in NIC filter structure.
 * @param inner_ofst[in]
 *   If zero, this is an outer header. If non-zero, this is the offset into L5
 *   where the header begins.
 */
static int
enic_copy_item_vlan_v2(const struct rte_flow_item *item,
		       struct filter_v2 *enic_filter, u8 *inner_ofst)
{
	const struct rte_flow_item_vlan *spec = item->spec;
	const struct rte_flow_item_vlan *mask = item->mask;
	struct filter_generic_1 *gp = &enic_filter->u.generic_1;

	FLOW_TRACE();

	/* Match all if no spec */
	if (!spec)
		return 0;

	if (!mask)
		mask = &rte_flow_item_vlan_mask;

	if (*inner_ofst == 0) {
		struct ether_hdr *eth_mask =
			(void *)gp->layer[FILTER_GENERIC_1_L2].mask;
		struct ether_hdr *eth_val =
			(void *)gp->layer[FILTER_GENERIC_1_L2].val;

		/* Outer TPID cannot be matched */
		if (eth_mask->ether_type)
			return ENOTSUP;
		eth_mask->ether_type = mask->inner_type;
		eth_val->ether_type = spec->inner_type;

		/* Outer header. Use the vlan mask/val fields */
		gp->mask_vlan = mask->tci;
		gp->val_vlan = spec->tci;
	} else {
		/* Inner header. Mask/Val start at *inner_ofst into L5 */
		if ((*inner_ofst + sizeof(struct vlan_hdr)) >
		     FILTER_GENERIC_1_KEY_LEN)
			return ENOTSUP;
		memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
		       mask, sizeof(struct vlan_hdr));
		memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
		       spec, sizeof(struct vlan_hdr));
		*inner_ofst += sizeof(struct vlan_hdr);
	}
	return 0;
}

/**
 * Copy IPv4 item into version 2 NIC filter.
 *
 * @param item[in]
 *   Item specification.
 * @param enic_filter[out]
 *   Partially filled in NIC filter structure.
 * @param inner_ofst[in]
 *   Must be 0. Don't support inner IPv4 filtering.
 */
static int
enic_copy_item_ipv4_v2(const struct rte_flow_item *item,
		       struct filter_v2 *enic_filter, u8 *inner_ofst)
{
	const struct rte_flow_item_ipv4 *spec = item->spec;
	const struct rte_flow_item_ipv4 *mask = item->mask;
	struct filter_generic_1 *gp = &enic_filter->u.generic_1;

	FLOW_TRACE();

	if (*inner_ofst == 0) {
		/* Match IPv4 */
		gp->mask_flags |= FILTER_GENERIC_1_IPV4;
		gp->val_flags |= FILTER_GENERIC_1_IPV4;

		/* Match all if no spec */
		if (!spec)
			return 0;

		if (!mask)
			mask = &rte_flow_item_ipv4_mask;

		memcpy(gp->layer[FILTER_GENERIC_1_L3].mask, &mask->hdr,
		       sizeof(struct ipv4_hdr));
		memcpy(gp->layer[FILTER_GENERIC_1_L3].val, &spec->hdr,
		       sizeof(struct ipv4_hdr));
	} else {
		/* Inner IPv4 header. Mask/Val start at *inner_ofst into L5 */
		if ((*inner_ofst + sizeof(struct ipv4_hdr)) >
		     FILTER_GENERIC_1_KEY_LEN)
			return ENOTSUP;
		memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
		       mask, sizeof(struct ipv4_hdr));
		memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
		       spec, sizeof(struct ipv4_hdr));
		*inner_ofst += sizeof(struct ipv4_hdr);
	}
	return 0;
}

/**
 * Copy IPv6 item into version 2 NIC filter.
 *
 * @param item[in]
 *   Item specification.
 * @param enic_filter[out]
 *   Partially filled in NIC filter structure.
 * @param inner_ofst[in]
 *   Must be 0. Don't support inner IPv6 filtering.
 */
static int
enic_copy_item_ipv6_v2(const struct rte_flow_item *item,
		       struct filter_v2 *enic_filter, u8 *inner_ofst)
{
	const struct rte_flow_item_ipv6 *spec = item->spec;
	const struct rte_flow_item_ipv6 *mask = item->mask;
	struct filter_generic_1 *gp = &enic_filter->u.generic_1;

	FLOW_TRACE();

	/* Match IPv6 */
	gp->mask_flags |= FILTER_GENERIC_1_IPV6;
	gp->val_flags |= FILTER_GENERIC_1_IPV6;

	/* Match all if no spec */
	if (!spec)
		return 0;

	if (!mask)
		mask = &rte_flow_item_ipv6_mask;

	if (*inner_ofst == 0) {
		memcpy(gp->layer[FILTER_GENERIC_1_L3].mask, &mask->hdr,
		       sizeof(struct ipv6_hdr));
		memcpy(gp->layer[FILTER_GENERIC_1_L3].val, &spec->hdr,
		       sizeof(struct ipv6_hdr));
	} else {
		/* Inner IPv6 header. Mask/Val start at *inner_ofst into L5 */
		if ((*inner_ofst + sizeof(struct ipv6_hdr)) >
		     FILTER_GENERIC_1_KEY_LEN)
			return ENOTSUP;
		memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
		       mask, sizeof(struct ipv6_hdr));
		memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
		       spec, sizeof(struct ipv6_hdr));
		*inner_ofst += sizeof(struct ipv6_hdr);
	}
	return 0;
}

/**
 * Copy UDP item into version 2 NIC filter.
 *
 * @param item[in]
 *   Item specification.
 * @param enic_filter[out]
 *   Partially filled in NIC filter structure.
 * @param inner_ofst[in]
 *   Must be 0. Don't support inner UDP filtering.
 */
static int
enic_copy_item_udp_v2(const struct rte_flow_item *item,
		      struct filter_v2 *enic_filter, u8 *inner_ofst)
{
	const struct rte_flow_item_udp *spec = item->spec;
	const struct rte_flow_item_udp *mask = item->mask;
	struct filter_generic_1 *gp = &enic_filter->u.generic_1;

	FLOW_TRACE();

	/* Match UDP */
	gp->mask_flags |= FILTER_GENERIC_1_UDP;
	gp->val_flags |= FILTER_GENERIC_1_UDP;

	/* Match all if no spec */
	if (!spec)
		return 0;

	if (!mask)
		mask = &rte_flow_item_udp_mask;

	if (*inner_ofst == 0) {
		memcpy(gp->layer[FILTER_GENERIC_1_L4].mask, &mask->hdr,
		       sizeof(struct udp_hdr));
		memcpy(gp->layer[FILTER_GENERIC_1_L4].val, &spec->hdr,
		       sizeof(struct udp_hdr));
	} else {
		/* Inner IPv6 header. Mask/Val start at *inner_ofst into L5 */
		if ((*inner_ofst + sizeof(struct udp_hdr)) >
		     FILTER_GENERIC_1_KEY_LEN)
			return ENOTSUP;
		memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
		       mask, sizeof(struct udp_hdr));
		memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
		       spec, sizeof(struct udp_hdr));
		*inner_ofst += sizeof(struct udp_hdr);
	}
	return 0;
}

/**
 * Copy TCP item into version 2 NIC filter.
 *
 * @param item[in]
 *   Item specification.
 * @param enic_filter[out]
 *   Partially filled in NIC filter structure.
 * @param inner_ofst[in]
 *   Must be 0. Don't support inner TCP filtering.
 */
static int
enic_copy_item_tcp_v2(const struct rte_flow_item *item,
		      struct filter_v2 *enic_filter, u8 *inner_ofst)
{
	const struct rte_flow_item_tcp *spec = item->spec;
	const struct rte_flow_item_tcp *mask = item->mask;
	struct filter_generic_1 *gp = &enic_filter->u.generic_1;

	FLOW_TRACE();

	/* Match TCP */
	gp->mask_flags |= FILTER_GENERIC_1_TCP;
	gp->val_flags |= FILTER_GENERIC_1_TCP;

	/* Match all if no spec */
	if (!spec)
		return 0;

	if (!mask)
		return ENOTSUP;

	if (*inner_ofst == 0) {
		memcpy(gp->layer[FILTER_GENERIC_1_L4].mask, &mask->hdr,
		       sizeof(struct tcp_hdr));
		memcpy(gp->layer[FILTER_GENERIC_1_L4].val, &spec->hdr,
		       sizeof(struct tcp_hdr));
	} else {
		/* Inner IPv6 header. Mask/Val start at *inner_ofst into L5 */
		if ((*inner_ofst + sizeof(struct tcp_hdr)) >
		     FILTER_GENERIC_1_KEY_LEN)
			return ENOTSUP;
		memcpy(&gp->layer[FILTER_GENERIC_1_L5].mask[*inner_ofst],
		       mask, sizeof(struct tcp_hdr));
		memcpy(&gp->layer[FILTER_GENERIC_1_L5].val[*inner_ofst],
		       spec, sizeof(struct tcp_hdr));
		*inner_ofst += sizeof(struct tcp_hdr);
	}
	return 0;
}

/**
 * Copy SCTP item into version 2 NIC filter.
 *
 * @param item[in]
 *   Item specification.
 * @param enic_filter[out]
 *   Partially filled in NIC filter structure.
 * @param inner_ofst[in]
 *   Must be 0. Don't support inner SCTP filtering.
 */
static int
enic_copy_item_sctp_v2(const struct rte_flow_item *item,
		       struct filter_v2 *enic_filter, u8 *inner_ofst)
{
	const struct rte_flow_item_sctp *spec = item->spec;
	const struct rte_flow_item_sctp *mask = item->mask;
	struct filter_generic_1 *gp = &enic_filter->u.generic_1;

	FLOW_TRACE();

	if (*inner_ofst)
		return ENOTSUP;

	/* Match all if no spec */
	if (!spec)
		return 0;

	if (!mask)
		mask = &rte_flow_item_sctp_mask;

	memcpy(gp->layer[FILTER_GENERIC_1_L4].mask, &mask->hdr,
	       sizeof(struct sctp_hdr));
	memcpy(gp->layer[FILTER_GENERIC_1_L4].val, &spec->hdr,
	       sizeof(struct sctp_hdr));
	return 0;
}

/**
 * Copy UDP item into version 2 NIC filter.
 *
 * @param item[in]
 *   Item specification.
 * @param enic_filter[out]
 *   Partially filled in NIC filter structure.
 * @param inner_ofst[in]
 *   Must be 0. VxLAN headers always start at the beginning of L5.
 */
static int
enic_copy_item_vxlan_v2(const struct rte_flow_item *item,
			struct filter_v2 *enic_filter, u8 *inner_ofst)
{
	const struct rte_flow_item_vxlan *spec = item->spec;
	const struct rte_flow_item_vxlan *mask = item->mask;
	struct filter_generic_1 *gp = &enic_filter->u.generic_1;

	FLOW_TRACE();

	if (*inner_ofst)
		return EINVAL;

	/* Match all if no spec */
	if (!spec)
		return 0;

	if (!mask)
		mask = &rte_flow_item_vxlan_mask;

	memcpy(gp->layer[FILTER_GENERIC_1_L5].mask, mask,
	       sizeof(struct vxlan_hdr));
	memcpy(gp->layer[FILTER_GENERIC_1_L5].val, spec,
	       sizeof(struct vxlan_hdr));

	*inner_ofst = sizeof(struct vxlan_hdr);
	return 0;
}

/**
 * Return 1 if current item is valid on top of the previous one.
 *
 * @param prev_item[in]
 *   The item before this one in the pattern or RTE_FLOW_ITEM_TYPE_END if this
 *   is the first item.
 * @param item_info[in]
 *   Info about this item, like valid previous items.
 * @param is_first[in]
 *   True if this the first item in the pattern.
 */
static int
item_stacking_valid(enum rte_flow_item_type prev_item,
		    const struct enic_items *item_info, u8 is_first_item)
{
	enum rte_flow_item_type const *allowed_items = item_info->prev_items;

	FLOW_TRACE();

	for (; *allowed_items != RTE_FLOW_ITEM_TYPE_END; allowed_items++) {
		if (prev_item == *allowed_items)
			return 1;
	}

	/* This is the first item in the stack. Check if that's cool */
	if (is_first_item && item_info->valid_start_item)
		return 1;

	return 0;
}

/**
 * Build the intenal enic filter structure from the provided pattern. The
 * pattern is validated as the items are copied.
 *
 * @param pattern[in]
 * @param items_info[in]
 *   Info about this NICs item support, like valid previous items.
 * @param enic_filter[out]
 *   NIC specfilc filters derived from the pattern.
 * @param error[out]
 */
static int
enic_copy_filter(const struct rte_flow_item pattern[],
		 const struct enic_items *items_info,
		 struct filter_v2 *enic_filter,
		 struct rte_flow_error *error)
{
	int ret;
	const struct rte_flow_item *item = pattern;
	u8 inner_ofst = 0; /* If encapsulated, ofst into L5 */
	enum rte_flow_item_type prev_item;
	const struct enic_items *item_info;

	u8 is_first_item = 1;

	FLOW_TRACE();

	prev_item = 0;

	for (; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
		/* Get info about how to validate and copy the item. If NULL
		 * is returned the nic does not support the item.
		 */
		if (item->type == RTE_FLOW_ITEM_TYPE_VOID)
			continue;

		item_info = &items_info[item->type];

		/* check to see if item stacking is valid */
		if (!item_stacking_valid(prev_item, item_info, is_first_item))
			goto stacking_error;

		ret = item_info->copy_item(item, enic_filter, &inner_ofst);
		if (ret)
			goto item_not_supported;
		prev_item = item->type;
		is_first_item = 0;
	}
	return 0;

item_not_supported:
	rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_ITEM,
			   NULL, "enic type error");
	return -rte_errno;

stacking_error:
	rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
			   item, "stacking error");
	return -rte_errno;
}

/**
 * Build the intenal version 1 NIC action structure from the provided pattern.
 * The pattern is validated as the items are copied.
 *
 * @param actions[in]
 * @param enic_action[out]
 *   NIC specfilc actions derived from the actions.
 * @param error[out]
 */
static int
enic_copy_action_v1(const struct rte_flow_action actions[],
		    struct filter_action_v2 *enic_action)
{
	enum { FATE = 1, };
	uint32_t overlap = 0;

	FLOW_TRACE();

	for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
		if (actions->type == RTE_FLOW_ACTION_TYPE_VOID)
			continue;

		switch (actions->type) {
		case RTE_FLOW_ACTION_TYPE_QUEUE: {
			const struct rte_flow_action_queue *queue =
				(const struct rte_flow_action_queue *)
				actions->conf;

			if (overlap & FATE)
				return ENOTSUP;
			overlap |= FATE;
			enic_action->rq_idx =
				enic_rte_rq_idx_to_sop_idx(queue->index);
			break;
		}
		default:
			RTE_ASSERT(0);
			break;
		}
	}
	if (!(overlap & FATE))
		return ENOTSUP;
	enic_action->type = FILTER_ACTION_RQ_STEERING;
	return 0;
}

/**
 * Build the intenal version 2 NIC action structure from the provided pattern.
 * The pattern is validated as the items are copied.
 *
 * @param actions[in]
 * @param enic_action[out]
 *   NIC specfilc actions derived from the actions.
 * @param error[out]
 */
static int
enic_copy_action_v2(const struct rte_flow_action actions[],
		    struct filter_action_v2 *enic_action)
{
	enum { FATE = 1, MARK = 2, };
	uint32_t overlap = 0;

	FLOW_TRACE();

	for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
		switch (actions->type) {
		case RTE_FLOW_ACTION_TYPE_QUEUE: {
			const struct rte_flow_action_queue *queue =
				(const struct rte_flow_action_queue *)
				actions->conf;

			if (overlap & FATE)
				return ENOTSUP;
			overlap |= FATE;
			enic_action->rq_idx =
				enic_rte_rq_idx_to_sop_idx(queue->index);
			enic_action->flags |= FILTER_ACTION_RQ_STEERING_FLAG;
			break;
		}
		case RTE_FLOW_ACTION_TYPE_MARK: {
			const struct rte_flow_action_mark *mark =
				(const struct rte_flow_action_mark *)
				actions->conf;

			if (overlap & MARK)
				return ENOTSUP;
			overlap |= MARK;
			/* ENIC_MAGIC_FILTER_ID is reserved and is the highest
			 * in the range of allows mark ids.
			 */
			if (mark->id >= ENIC_MAGIC_FILTER_ID)
				return EINVAL;
			enic_action->filter_id = mark->id;
			enic_action->flags |= FILTER_ACTION_FILTER_ID_FLAG;
			break;
		}
		case RTE_FLOW_ACTION_TYPE_FLAG: {
			if (overlap & MARK)
				return ENOTSUP;
			overlap |= MARK;
			enic_action->filter_id = ENIC_MAGIC_FILTER_ID;
			enic_action->flags |= FILTER_ACTION_FILTER_ID_FLAG;
			break;
		}
		case RTE_FLOW_ACTION_TYPE_DROP: {
			if (overlap & FATE)
				return ENOTSUP;
			overlap |= FATE;
			enic_action->flags |= FILTER_ACTION_DROP_FLAG;
			break;
		}
		case RTE_FLOW_ACTION_TYPE_VOID:
			continue;
		default:
			RTE_ASSERT(0);
			break;
		}
	}
	if (!(overlap & FATE))
		return ENOTSUP;
	enic_action->type = FILTER_ACTION_V2;
	return 0;
}

/** Check if the action is supported */
static int
enic_match_action(const struct rte_flow_action *action,
		  const enum rte_flow_action_type *supported_actions)
{
	for (; *supported_actions != RTE_FLOW_ACTION_TYPE_END;
	     supported_actions++) {
		if (action->type == *supported_actions)
			return 1;
	}
	return 0;
}

/** Get the NIC filter capabilties structure */
static const struct enic_filter_cap *
enic_get_filter_cap(struct enic *enic)
{
	if (enic->flow_filter_mode)
		return &enic_filter_cap[enic->flow_filter_mode];

	return NULL;
}

/** Get the actions for this NIC version. */
static const struct enic_action_cap *
enic_get_action_cap(struct enic *enic)
{
	const struct enic_action_cap *ea;
	uint8_t actions;

	actions = enic->filter_actions;
	if (actions & FILTER_ACTION_DROP_FLAG)
		ea = &enic_action_cap[FILTER_ACTION_DROP_FLAG];
	else if (actions & FILTER_ACTION_FILTER_ID_FLAG)
		ea = &enic_action_cap[FILTER_ACTION_FILTER_ID_FLAG];
	else
		ea = &enic_action_cap[FILTER_ACTION_RQ_STEERING_FLAG];
	return ea;
}

/* Debug function to dump internal NIC action structure. */
static void
enic_dump_actions(const struct filter_action_v2 *ea)
{
	if (ea->type == FILTER_ACTION_RQ_STEERING) {
		FLOW_LOG(INFO, "Action(V1), queue: %u\n", ea->rq_idx);
	} else if (ea->type == FILTER_ACTION_V2) {
		FLOW_LOG(INFO, "Actions(V2)\n");
		if (ea->flags & FILTER_ACTION_RQ_STEERING_FLAG)
			FLOW_LOG(INFO, "\tqueue: %u\n",
			       enic_sop_rq_idx_to_rte_idx(ea->rq_idx));
		if (ea->flags & FILTER_ACTION_FILTER_ID_FLAG)
			FLOW_LOG(INFO, "\tfilter_id: %u\n", ea->filter_id);
	}
}

/* Debug function to dump internal NIC filter structure. */
static void
enic_dump_filter(const struct filter_v2 *filt)
{
	const struct filter_generic_1 *gp;
	int i, j, mbyte;
	char buf[128], *bp;
	char ip4[16], ip6[16], udp[16], tcp[16], tcpudp[16], ip4csum[16];
	char l4csum[16], ipfrag[16];

	switch (filt->type) {
	case FILTER_IPV4_5TUPLE:
		FLOW_LOG(INFO, "FILTER_IPV4_5TUPLE\n");
		break;
	case FILTER_USNIC_IP:
	case FILTER_DPDK_1:
		/* FIXME: this should be a loop */
		gp = &filt->u.generic_1;
		FLOW_LOG(INFO, "Filter: vlan: 0x%04x, mask: 0x%04x\n",
		       gp->val_vlan, gp->mask_vlan);

		if (gp->mask_flags & FILTER_GENERIC_1_IPV4)
			sprintf(ip4, "%s ",
				(gp->val_flags & FILTER_GENERIC_1_IPV4)
				 ? "ip4(y)" : "ip4(n)");
		else
			sprintf(ip4, "%s ", "ip4(x)");

		if (gp->mask_flags & FILTER_GENERIC_1_IPV6)
			sprintf(ip6, "%s ",
				(gp->val_flags & FILTER_GENERIC_1_IPV4)
				 ? "ip6(y)" : "ip6(n)");
		else
			sprintf(ip6, "%s ", "ip6(x)");

		if (gp->mask_flags & FILTER_GENERIC_1_UDP)
			sprintf(udp, "%s ",
				(gp->val_flags & FILTER_GENERIC_1_UDP)
				 ? "udp(y)" : "udp(n)");
		else
			sprintf(udp, "%s ", "udp(x)");

		if (gp->mask_flags & FILTER_GENERIC_1_TCP)
			sprintf(tcp, "%s ",
				(gp->val_flags & FILTER_GENERIC_1_TCP)
				 ? "tcp(y)" : "tcp(n)");
		else
			sprintf(tcp, "%s ", "tcp(x)");

		if (gp->mask_flags & FILTER_GENERIC_1_TCP_OR_UDP)
			sprintf(tcpudp, "%s ",
				(gp->val_flags & FILTER_GENERIC_1_TCP_OR_UDP)
				 ? "tcpudp(y)" : "tcpudp(n)");
		else
			sprintf(tcpudp, "%s ", "tcpudp(x)");

		if (gp->mask_flags & FILTER_GENERIC_1_IP4SUM_OK)
			sprintf(ip4csum, "%s ",
				(gp->val_flags & FILTER_GENERIC_1_IP4SUM_OK)
				 ? "ip4csum(y)" : "ip4csum(n)");
		else
			sprintf(ip4csum, "%s ", "ip4csum(x)");

		if (gp->mask_flags & FILTER_GENERIC_1_L4SUM_OK)
			sprintf(l4csum, "%s ",
				(gp->val_flags & FILTER_GENERIC_1_L4SUM_OK)
				 ? "l4csum(y)" : "l4csum(n)");
		else
			sprintf(l4csum, "%s ", "l4csum(x)");

		if (gp->mask_flags & FILTER_GENERIC_1_IPFRAG)
			sprintf(ipfrag, "%s ",
				(gp->val_flags & FILTER_GENERIC_1_IPFRAG)
				 ? "ipfrag(y)" : "ipfrag(n)");
		else
			sprintf(ipfrag, "%s ", "ipfrag(x)");
		FLOW_LOG(INFO, "\tFlags: %s%s%s%s%s%s%s%s\n", ip4, ip6, udp,
			 tcp, tcpudp, ip4csum, l4csum, ipfrag);

		for (i = 0; i < FILTER_GENERIC_1_NUM_LAYERS; i++) {
			mbyte = FILTER_GENERIC_1_KEY_LEN - 1;
			while (mbyte && !gp->layer[i].mask[mbyte])
				mbyte--;
			if (mbyte == 0)
				continue;

			bp = buf;
			for (j = 0; j <= mbyte; j++) {
				sprintf(bp, "%02x",
					gp->layer[i].mask[j]);
				bp += 2;
			}
			*bp = '\0';
			FLOW_LOG(INFO, "\tL%u mask: %s\n", i + 2, buf);
			bp = buf;
			for (j = 0; j <= mbyte; j++) {
				sprintf(bp, "%02x",
					gp->layer[i].val[j]);
				bp += 2;
			}
			*bp = '\0';
			FLOW_LOG(INFO, "\tL%u  val: %s\n", i + 2, buf);
		}
		break;
	default:
		FLOW_LOG(INFO, "FILTER UNKNOWN\n");
		break;
	}
}

/* Debug function to dump internal NIC flow structures. */
static void
enic_dump_flow(const struct filter_action_v2 *ea, const struct filter_v2 *filt)
{
	enic_dump_filter(filt);
	enic_dump_actions(ea);
}


/**
 * Internal flow parse/validate function.
 *
 * @param dev[in]
 *   This device pointer.
 * @param pattern[in]
 * @param actions[in]
 * @param error[out]
 * @param enic_filter[out]
 *   Internal NIC filter structure pointer.
 * @param enic_action[out]
 *   Internal NIC action structure pointer.
 */
static int
enic_flow_parse(struct rte_eth_dev *dev,
		const struct rte_flow_attr *attrs,
		const struct rte_flow_item pattern[],
		const struct rte_flow_action actions[],
		struct rte_flow_error *error,
		struct filter_v2 *enic_filter,
		struct filter_action_v2 *enic_action)
{
	unsigned int ret = 0;
	struct enic *enic = pmd_priv(dev);
	const struct enic_filter_cap *enic_filter_cap;
	const struct enic_action_cap *enic_action_cap;
	const struct rte_flow_action *action;

	FLOW_TRACE();

	memset(enic_filter, 0, sizeof(*enic_filter));
	memset(enic_action, 0, sizeof(*enic_action));

	if (!pattern) {
		rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM_NUM,
				   NULL, "No pattern specified");
		return -rte_errno;
	}

	if (!actions) {
		rte_flow_error_set(error, EINVAL,
				   RTE_FLOW_ERROR_TYPE_ACTION_NUM,
				   NULL, "No action specified");
		return -rte_errno;
	}

	if (attrs) {
		if (attrs->group) {
			rte_flow_error_set(error, ENOTSUP,
					   RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
					   NULL,
					   "priority groups are not supported");
			return -rte_errno;
		} else if (attrs->priority) {
			rte_flow_error_set(error, ENOTSUP,
					   RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
					   NULL,
					   "priorities are not supported");
			return -rte_errno;
		} else if (attrs->egress) {
			rte_flow_error_set(error, ENOTSUP,
					   RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
					   NULL,
					   "egress is not supported");
			return -rte_errno;
		} else if (attrs->transfer) {
			rte_flow_error_set(error, ENOTSUP,
					   RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
					   NULL,
					   "transfer is not supported");
			return -rte_errno;
		} else if (!attrs->ingress) {
			rte_flow_error_set(error, ENOTSUP,
					   RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
					   NULL,
					   "only ingress is supported");
			return -rte_errno;
		}

	} else {
		rte_flow_error_set(error, EINVAL,
				   RTE_FLOW_ERROR_TYPE_ATTR,
				   NULL, "No attribute specified");
		return -rte_errno;
	}

	/* Verify Actions. */
	enic_action_cap =  enic_get_action_cap(enic);
	for (action = &actions[0]; action->type != RTE_FLOW_ACTION_TYPE_END;
	     action++) {
		if (action->type == RTE_FLOW_ACTION_TYPE_VOID)
			continue;
		else if (!enic_match_action(action, enic_action_cap->actions))
			break;
	}
	if (action->type != RTE_FLOW_ACTION_TYPE_END) {
		rte_flow_error_set(error, EPERM, RTE_FLOW_ERROR_TYPE_ACTION,
				   action, "Invalid action.");
		return -rte_errno;
	}
	ret = enic_action_cap->copy_fn(actions, enic_action);
	if (ret) {
		rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
			   NULL, "Unsupported action.");
		return -rte_errno;
	}

	/* Verify Flow items. If copying the filter from flow format to enic
	 * format fails, the flow is not supported
	 */
	enic_filter_cap =  enic_get_filter_cap(enic);
	if (enic_filter_cap == NULL) {
		rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
			   NULL, "Flow API not available");
		return -rte_errno;
	}
	enic_filter->type = enic->flow_filter_mode;
	ret = enic_copy_filter(pattern, enic_filter_cap->item_info,
				       enic_filter, error);
	return ret;
}

/**
 * Push filter/action to the NIC.
 *
 * @param enic[in]
 *   Device structure pointer.
 * @param enic_filter[in]
 *   Internal NIC filter structure pointer.
 * @param enic_action[in]
 *   Internal NIC action structure pointer.
 * @param error[out]
 */
static struct rte_flow *
enic_flow_add_filter(struct enic *enic, struct filter_v2 *enic_filter,
		   struct filter_action_v2 *enic_action,
		   struct rte_flow_error *error)
{
	struct rte_flow *flow;
	int ret;
	u16 entry;

	FLOW_TRACE();

	flow = rte_calloc(__func__, 1, sizeof(*flow), 0);
	if (!flow) {
		rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
				   NULL, "cannot allocate flow memory");
		return NULL;
	}

	/* entry[in] is the queue id, entry[out] is the filter Id for delete */
	entry = enic_action->rq_idx;
	ret = vnic_dev_classifier(enic->vdev, CLSF_ADD, &entry, enic_filter,
				  enic_action);
	if (!ret) {
		flow->enic_filter_id = entry;
		flow->enic_filter = *enic_filter;
	} else {
		rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE,
				   NULL, "vnic_dev_classifier error");
		rte_free(flow);
		return NULL;
	}
	return flow;
}

/**
 * Remove filter/action from the NIC.
 *
 * @param enic[in]
 *   Device structure pointer.
 * @param filter_id[in]
 *   Id of NIC filter.
 * @param enic_action[in]
 *   Internal NIC action structure pointer.
 * @param error[out]
 */
static int
enic_flow_del_filter(struct enic *enic, u16 filter_id,
		   struct rte_flow_error *error)
{
	int ret;

	FLOW_TRACE();

	ret = vnic_dev_classifier(enic->vdev, CLSF_DEL, &filter_id, NULL, NULL);
	if (!ret)
		rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE,
				   NULL, "vnic_dev_classifier failed");
	return ret;
}

/*
 * The following functions are callbacks for Generic flow API.
 */

/**
 * Validate a flow supported by the NIC.
 *
 * @see rte_flow_validate()
 * @see rte_flow_ops
 */
static int
enic_flow_validate(struct rte_eth_dev *dev, const struct rte_flow_attr *attrs,
		   const struct rte_flow_item pattern[],
		   const struct rte_flow_action actions[],
		   struct rte_flow_error *error)
{
	struct filter_v2 enic_filter;
	struct filter_action_v2 enic_action;
	int ret;

	FLOW_TRACE();

	ret = enic_flow_parse(dev, attrs, pattern, actions, error,
			       &enic_filter, &enic_action);
	if (!ret)
		enic_dump_flow(&enic_action, &enic_filter);
	return ret;
}

/**
 * Create a flow supported by the NIC.
 *
 * @see rte_flow_create()
 * @see rte_flow_ops
 */
static struct rte_flow *
enic_flow_create(struct rte_eth_dev *dev,
		 const struct rte_flow_attr *attrs,
		 const struct rte_flow_item pattern[],
		 const struct rte_flow_action actions[],
		 struct rte_flow_error *error)
{
	int ret;
	struct filter_v2 enic_filter;
	struct filter_action_v2 enic_action;
	struct rte_flow *flow;
	struct enic *enic = pmd_priv(dev);

	FLOW_TRACE();

	ret = enic_flow_parse(dev, attrs, pattern, actions, error, &enic_filter,
			      &enic_action);
	if (ret < 0)
		return NULL;

	rte_spinlock_lock(&enic->flows_lock);
	flow = enic_flow_add_filter(enic, &enic_filter, &enic_action,
				    error);
	if (flow)
		LIST_INSERT_HEAD(&enic->flows, flow, next);
	rte_spinlock_unlock(&enic->flows_lock);

	return flow;
}

/**
 * Destroy a flow supported by the NIC.
 *
 * @see rte_flow_destroy()
 * @see rte_flow_ops
 */
static int
enic_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow,
		  __rte_unused struct rte_flow_error *error)
{
	struct enic *enic = pmd_priv(dev);

	FLOW_TRACE();

	rte_spinlock_lock(&enic->flows_lock);
	enic_flow_del_filter(enic, flow->enic_filter_id, error);
	LIST_REMOVE(flow, next);
	rte_spinlock_unlock(&enic->flows_lock);
	return 0;
}

/**
 * Flush all flows on the device.
 *
 * @see rte_flow_flush()
 * @see rte_flow_ops
 */
static int
enic_flow_flush(struct rte_eth_dev *dev, struct rte_flow_error *error)
{
	struct rte_flow *flow;
	struct enic *enic = pmd_priv(dev);

	FLOW_TRACE();

	rte_spinlock_lock(&enic->flows_lock);

	while (!LIST_EMPTY(&enic->flows)) {
		flow = LIST_FIRST(&enic->flows);
		enic_flow_del_filter(enic, flow->enic_filter_id, error);
		LIST_REMOVE(flow, next);
	}
	rte_spinlock_unlock(&enic->flows_lock);
	return 0;
}

/**
 * Flow callback registration.
 *
 * @see rte_flow_ops
 */
const struct rte_flow_ops enic_flow_ops = {
	.validate = enic_flow_validate,
	.create = enic_flow_create,
	.destroy = enic_flow_destroy,
	.flush = enic_flow_flush,
};