summaryrefslogtreecommitdiffstats
path: root/VNFs/vFW/pipeline/pipeline_vfw_be.c
blob: 0d3f5279a0a72a1aff9cfed5328c1d9c7b5444c7 (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
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
/*
// Copyright (c) 2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/

/**
 * @file
 * Pipeline VFW BE Implementation.
 *
 * Implementation of Pipeline VFW Back End (BE).
 * Responsible for packet processing.
 *
 */

#define EN_SWP_ACL 1
#define EN_SWP_ARP 1

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include <unistd.h>

#include <rte_common.h>
#include <rte_malloc.h>
#include <rte_ether.h>
#include <rte_ethdev.h>
#include <rte_ip.h>
#include <rte_udp.h>
#include <rte_icmp.h>
#include <rte_byteorder.h>

#include <rte_table_lpm.h>
#include <rte_table_hash.h>
#include <rte_table_array.h>
#include <rte_table_acl.h>
#include <rte_table_stub.h>
#include <rte_timer.h>
#include <rte_cycles.h>
#include <rte_pipeline.h>
#include <rte_spinlock.h>
#include <rte_prefetch.h>
#include "pipeline_actions_common.h"
#include "hash_func.h"
#include "pipeline_vfw.h"
#include "pipeline_vfw_be.h"
#include "rte_cnxn_tracking.h"
#include "pipeline_arpicmp_be.h"
#include "vnf_common.h"
#include "vnf_define.h"

#include "lib_arp.h"
#include "lib_icmpv6.h"
#include "pipeline_common_fe.h"

uint32_t timer_lcore;

uint8_t firewall_flag = 1;
uint8_t VFW_DEBUG;
uint8_t cnxn_tracking_is_active = 1;
/**
 * A structure defining the VFW pipeline input port per thread data.
 */
struct vfw_ports_in_args {
       struct pipeline *pipe;
       struct rte_ct_cnxn_tracker *cnxn_tracker;
} __rte_cache_aligned;
/**
 * A structure defining the VFW pipeline per thread data.
 */
struct pipeline_vfw {
       struct pipeline pipe;
       pipeline_msg_req_handler custom_handlers[PIPELINE_VFW_MSG_REQS];

       struct rte_ct_cnxn_tracker *cnxn_tracker;
       struct rte_VFW_counter_block *counters;
       struct rte_mbuf *pkt_buffer[PKT_BUFFER_SIZE];
       struct lib_acl *plib_acl;
       /* timestamp retrieved during in-port computations */
       uint32_t n_flows;
       uint8_t pipeline_num;
       uint8_t traffic_type;
       uint8_t links_map[PIPELINE_MAX_PORT_IN];
       uint8_t outport_id[PIPELINE_MAX_PORT_IN];
       /* Local ARP & ND Tables */
       struct lib_arp_route_table_entry
              local_lib_arp_route_table[MAX_ARP_RT_ENTRY];
       uint8_t local_lib_arp_route_ent_cnt;
       struct lib_nd_route_table_entry
              local_lib_nd_route_table[MAX_ND_RT_ENTRY];
       uint8_t local_lib_nd_route_ent_cnt;

} __rte_cache_aligned;
/**
 * A structure defining the mbuf meta data for VFW.
 */
struct mbuf_tcp_meta_data {
/* output port stored for RTE_PIPELINE_ACTION_PORT_META */
       uint32_t output_port;
       struct rte_mbuf *next;       /* next pointer for chained buffers */
} __rte_cache_aligned;

#define DONT_CARE_TCP_PACKET 0
#define IS_NOT_TCP_PACKET 0
#define IS_TCP_PACKET 1

#define META_DATA_OFFSET 128

#define RTE_PKTMBUF_HEADROOM 128       /* where is this defined ? */
#define ETHERNET_START (META_DATA_OFFSET + RTE_PKTMBUF_HEADROOM)
#define ETH_HDR_SIZE 14
#define PROTOCOL_START (IP_START + 9)

#define TCP_START (IP_START + 20)
#define RTE_LB_PORT_OFFSET 204       /* TODO: Need definition in LB header */
#define TCP_START_IPV6 (IP_START + 40)
#define PROTOCOL_START_IPV6 (IP_START + 6)
#define IP_HDR_DSCP_OFST 1

#define TCP_PROTOCOL 6
#define UDP_PROTOCOL 17

#define DELETE_BUFFERED_PACKETS 0
#define FORWARD_BUFFERED_PACKETS 1
#define DO_ARP 1
#define NO_ARP 0

#define IPv4_HEADER_SIZE 20
#define IPv6_HEADER_SIZE 40

#define IP_VERSION_4 4
#define IP_VERSION_6 6
#define MIX 10
/* IPv6 */
#define IP_HDR_SIZE_IPV6  40
#define IP_HDR_DSCP_OFST_IPV6 0
#define IP_HDR_LENGTH_OFST_IPV6 4
#define IP_HDR_PROTOCOL_OFST_IPV6 6
#define IP_HDR_DST_ADR_OFST_IPV6 24
#define MAX_NUM_LOCAL_MAC_ADDRESS 16
/** The counter table for VFW pipeline per thread data.*/
struct rte_VFW_counter_block rte_vfw_counter_table[MAX_VFW_INSTANCES]
__rte_cache_aligned;
int rte_VFW_hi_counter_block_in_use = -1;

/* a spin lock used during vfw initialization only */
rte_spinlock_t rte_VFW_init_lock = RTE_SPINLOCK_INITIALIZER;

/* Action Array */
struct pipeline_action_key *action_array_a;
struct pipeline_action_key *action_array_b;
struct pipeline_action_key *action_array_active;
struct pipeline_action_key *action_array_standby;
uint32_t action_array_size;
struct action_counter_block
action_counter_table[MAX_VFW_INSTANCES][action_array_max]
__rte_cache_aligned;
/*
  * Pipeline table strategy for firewall. Unfortunately, there does not seem to
  * be any use for the built-in table lookup of ip_pipeline for the firewall.
  * The main table requirement of the firewall is the hash table to maintain
  * connection info, but that is implemented seperately in the connection
  * tracking library. So a "dummy" table lookup will be performed.
  * TODO: look into "stub" table and see if that can be used
  * to avoid useless table lookup
  */
/***** ARP local cache *****/
uint8_t link_hw_laddr_valid[MAX_NUM_LOCAL_MAC_ADDRESS] = {
       0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0
};

static struct ether_addr link_hw_laddr[MAX_NUM_LOCAL_MAC_ADDRESS] = {
       {.addr_bytes = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
       {.addr_bytes = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
       {.addr_bytes = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
       {.addr_bytes = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
       {.addr_bytes = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
       {.addr_bytes = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
       {.addr_bytes = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
       {.addr_bytes = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
       {.addr_bytes = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
       {.addr_bytes = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
       {.addr_bytes = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
       {.addr_bytes = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
       {.addr_bytes = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
       {.addr_bytes = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
       {.addr_bytes = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
       {.addr_bytes = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00} }
};

/* Start TSC measurement */
/* Prefetch counters and pipe before this function */
static inline void start_tsc_measure(struct pipeline_vfw *vfw_pipe) {
       vfw_pipe->counters->entry_timestamp = rte_get_tsc_cycles();
       if (likely(vfw_pipe->counters->exit_timestamp))
              vfw_pipe->counters->external_time_sum +=
                     vfw_pipe->counters->entry_timestamp -
                     vfw_pipe->counters->exit_timestamp;
}

/* End TSC measurement */
static inline void end_tsc_measure(
       struct pipeline_vfw *vfw_pipe,
       uint8_t n_pkts)
{
       if (likely(n_pkts > 1)) {
              vfw_pipe->counters->exit_timestamp = rte_get_tsc_cycles();
              vfw_pipe->counters->internal_time_sum +=
                     vfw_pipe->counters->exit_timestamp -
                     vfw_pipe->counters->entry_timestamp;
              vfw_pipe->counters->time_measurements++;
       } else {
              /* small counts skew results, ignore */
              vfw_pipe->counters->exit_timestamp = 0;
       }
}

static struct ether_addr *get_local_link_hw_addr(uint8_t out_port)
{
       return &link_hw_laddr[out_port];
}

static uint8_t local_dest_mac_present(uint8_t out_port)
{
       return link_hw_laddr_valid[out_port];
}

static uint32_t local_get_nh_ipv4(
       uint32_t ip,
       uint32_t *port,
       uint32_t *nhip,
       struct pipeline_vfw *vfw_pipe)
{
       int i;

       for (i = 0; i < vfw_pipe->local_lib_arp_route_ent_cnt; i++) {
              if (((vfw_pipe->local_lib_arp_route_table[i].ip &
              vfw_pipe->local_lib_arp_route_table[i].mask) ==
              (ip & vfw_pipe->local_lib_arp_route_table[i].mask))) {
                     *port = vfw_pipe->local_lib_arp_route_table[i].port;

                     *nhip = vfw_pipe->local_lib_arp_route_table[i].nh;
                     return 1;
              }
       }
       return 0;
}

static void do_local_nh_ipv4_cache(uint32_t dest_if,
              struct pipeline_vfw *vfw_pipe)
{

       /* Search for the entry and do local copy */
       int i;

       for (i = 0; i < MAX_ARP_RT_ENTRY; i++) {
              if (lib_arp_route_table[i].port == dest_if) {

                     struct lib_arp_route_table_entry *lentry =
                            &vfw_pipe->
                            local_lib_arp_route_table[vfw_pipe->
                            local_lib_arp_route_ent_cnt];

                     lentry->ip   = lib_arp_route_table[i].ip;
                     lentry->mask = lib_arp_route_table[i].mask;
                     lentry->port = lib_arp_route_table[i].port;
                     lentry->nh   = lib_arp_route_table[i].nh;

                     vfw_pipe->local_lib_arp_route_ent_cnt++;
                     break;
              }
       }
}
static uint32_t local_get_nh_ipv6(
       uint8_t *ip,
       uint32_t *port,
       uint8_t nhip[],
        struct pipeline_vfw *vfw_pipe)
{
       uint8_t netmask_ipv6[IPV6_ADD_SIZE], netip_nd[IPV6_ADD_SIZE],
              netip_in[IPV6_ADD_SIZE];
       uint8_t i = 0, j = 0, k = 0, l = 0, depthflags = 0, depthflags1 = 0;
       memset(netmask_ipv6, 0, sizeof(netmask_ipv6));
       memset(netip_nd, 0, sizeof(netip_nd));
       memset(netip_in, 0, sizeof(netip_in));

       for (i = 0; i < vfw_pipe->local_lib_nd_route_ent_cnt; i++) {

              convert_prefixlen_to_netmask_ipv6(
                     vfw_pipe->local_lib_nd_route_table[i].depth,
                     netmask_ipv6);

              for (k = 0; k < IPV6_ADD_SIZE; k++)
                     if (vfw_pipe->local_lib_nd_route_table[i].ipv6[k] &
                                   netmask_ipv6[k]) {
                            depthflags++;
                            netip_nd[k] = vfw_pipe->
                                   local_lib_nd_route_table[i].ipv6[k];
                     }

              for (l = 0; l < IPV6_ADD_SIZE; l++)
                     if (ip[l] & netmask_ipv6[l]) {
                            depthflags1++;
                            netip_in[l] = ip[l];
                     }


              if ((depthflags == depthflags1) && (memcmp(netip_nd, netip_in,
                                          sizeof(netip_nd)) == 0)) {

                     *port = vfw_pipe->local_lib_nd_route_table[i].port;

                     for (j = 0; j < IPV6_ADD_SIZE; j++)
                            nhip[j] = vfw_pipe->
                                   local_lib_nd_route_table[i].nhipv6[j];
                     return 1;
              }

              depthflags = 0;
              depthflags1 = 0;
                     }
                     return 0;
}

static void do_local_nh_ipv6_cache(uint32_t dest_if,
              struct pipeline_vfw *vfw_pipe)
{
              /* Search for the entry and do local copy */
       int i, l;

       for (i = 0; i < MAX_ND_RT_ENTRY; i++) {

              if (lib_nd_route_table[i].port == dest_if) {

                     struct lib_nd_route_table_entry *lentry = &vfw_pipe->
                            local_lib_nd_route_table[vfw_pipe->
                            local_lib_nd_route_ent_cnt];

                     for (l = 0; l < IPV6_ADD_SIZE; l++) {
                            lentry->ipv6[l]   =
                                   lib_nd_route_table[i].ipv6[l];
                            lentry->nhipv6[l] =
                                   lib_nd_route_table[i].nhipv6[l];
                     }
                     lentry->depth = lib_nd_route_table[i].depth;
                     lentry->port  = lib_nd_route_table[i].port;

                     vfw_pipe->local_lib_nd_route_ent_cnt++;
                     break;
                     } /* if */
              } /* for */
}
/**
 * Print packet for debugging.
 *
 * @param pkt
 *  A pointer to the packet.
 *
 */
static __rte_unused  void print_pkt(struct rte_mbuf *pkt)
{
       int i;
       int size = (int)sizeof(struct mbuf_tcp_meta_data);
       uint8_t *rd = RTE_MBUF_METADATA_UINT8_PTR(pkt, META_DATA_OFFSET);

       printf("Meta-data:\n");
       for (i = 0; i < size; i++) {
              printf("%02x ", rd[i]);
              if ((i & TWO_BYTE_PRINT) == TWO_BYTE_PRINT)
                     printf("\n");
       }
       printf("\n");
       printf("IP and TCP/UDP headers:\n");
       rd = RTE_MBUF_METADATA_UINT8_PTR(pkt, IP_START);
       for (i = 0; i < IP_HDR_SIZE_IPV6; i++) {
              printf("%02x ", rd[i]);
              if ((i & TWO_BYTE_PRINT) == TWO_BYTE_PRINT)
                     printf("\n");
       }
       printf("\n");
}

/* TODO: are the protocol numbers defined somewhere with meaningful names? */
#define IP_ICMP_PROTOCOL 1
#define IP_TCP_PROTOCOL 6
#define IP_UDP_PROTOCOL 17
#define IPv6_FRAGMENT_HEADER 44

/**
 * Return ethernet header structure form packet.
 *
 * @param pkt
 *  A pointer to the packet.
 *
 */
static inline struct ether_hdr *rte_vfw_get_ether_addr(struct rte_mbuf *pkt)
{
       return (struct ether_hdr *)RTE_MBUF_METADATA_UINT32_PTR(pkt,
                                                        ETHERNET_START);
}

/**
 * Return IPV4 header structure form packet.
 *
 * @param pkt
 *  A pointer to the packet.
 *
 */

static inline struct ipv4_hdr *rte_vfw_get_IPv4_hdr_addr(
              struct rte_mbuf *pkt)
{
       return (struct ipv4_hdr *)RTE_MBUF_METADATA_UINT32_PTR(pkt, IP_START);
}

static inline int rte_vfw_is_IPv4(struct rte_mbuf *pkt)
{
       /* NOTE: Only supporting IP headers with no options,
        * so header is fixed size */
       uint8_t ip_type = RTE_MBUF_METADATA_UINT8(pkt, IP_START)
              >> VERSION_NO_BYTE;

       return ip_type == IPv4_HDR_VERSION;
}

static inline int rte_vfw_is_IPv6(struct rte_mbuf *pkt)
{
       /* NOTE: Only supporting IP headers with no options,
        * so header is fixed size */
       uint8_t ip_type = RTE_MBUF_METADATA_UINT8(pkt, IP_START)
              >> VERSION_NO_BYTE;

       return ip_type == IPv6_HDR_VERSION;
}

static inline void rte_vfw_incr_drop_ctr(uint64_t *counter)
{
       if (likely(firewall_flag))
              (*counter)++;
}

static uint8_t check_arp_icmp(
              struct rte_mbuf *pkt,
              struct pipeline_vfw *vfw_pipe)
{
       struct ether_hdr *ehdr;
       struct app_link_params *link;
        uint8_t solicited_node_multicast_addr[IPV6_ADD_SIZE] = {
                0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x01, 0xff, 0x00, 0x00, 0x00};

        /* ARP outport number */
       uint16_t out_port = vfw_pipe->pipe.n_ports_out - 1;
       struct ipv4_hdr *ipv4_h;
       struct ipv6_hdr *ipv6_h;
       link = &myApp->link_params[pkt->port];

       ehdr = rte_vfw_get_ether_addr(pkt);
       switch (rte_be_to_cpu_16(ehdr->ether_type)) {

       case ETH_TYPE_ARP:
              rte_pipeline_port_out_packet_insert(
                            vfw_pipe->pipe.p,
                            out_port,
                            pkt);

              vfw_pipe->counters->arpicmpPktCount++;

              return 0;
       case ETH_TYPE_IPV4:
              ipv4_h = (struct ipv4_hdr *)
                     RTE_MBUF_METADATA_UINT32_PTR(pkt, IP_START);
              if ((ipv4_h->next_proto_id == IP_PROTOCOL_ICMP) &&
                            link->ip ==
                            rte_be_to_cpu_32(ipv4_h->dst_addr)) {
                     if (is_phy_port_privte(pkt->port)) {
                            rte_pipeline_port_out_packet_insert(
                                          vfw_pipe->pipe.p,
                                          out_port,
                                          pkt);

                     vfw_pipe->counters->arpicmpPktCount++;
                            return 0;
                     }
              }
              break;
#ifdef IPV6
        case ETH_TYPE_IPV6:
                ipv6_h = (struct ipv6_hdr *)
                        RTE_MBUF_METADATA_UINT32_PTR(pkt, IP_START);

                if (ipv6_h->proto == ICMPV6_PROTOCOL_ID) {
                        if (!memcmp(ipv6_h->dst_addr, link->ipv6, IPV6_ADD_SIZE)
                                        || !memcmp(ipv6_h->dst_addr,
                                                solicited_node_multicast_addr,
                                                IPV6_ADD_CMP_MULTI)) {

                                rte_pipeline_port_out_packet_insert(
                                                vfw_pipe->pipe.p,
                                                out_port,
                                                pkt);

                                vfw_pipe->counters->arpicmpPktCount++;

                        } else
                                vfw_pipe->counters->
                                        pkts_drop_unsupported_type++;

                        return 0;
                }
                break;
#endif
       default:
              break;
}
       return 1;
}


/**
 * Performs basic VFW packet filtering.
 * @param pkts
 *  A pointer to the packets.
 * @param pkts_mask
 *  packet mask.
 * @param vfw_pipe
 *  A pointer to VFW pipeline.
 */

static uint64_t
rte_vfw_packet_filter_and_process(struct rte_mbuf **pkts,
                                 uint64_t pkts_mask,
                                 struct pipeline_vfw *vfw_pipe)
{

       /*
        * Make use of cache prefetch. At beginning of loop, want to prefetch
        * mbuf data for next iteration (not current one).
        * Note that ethernet header (14 bytes) is cache aligned. IPv4 header
        * is 20 bytes (extensions not supported), while the IPv6 header is 40
        * bytes. TCP header is 20 bytes, UDP is 8. One cache line prefetch
        * will cover IPv4 and TCP or UDP, but to get IPv6 and TCP,
        * need two pre-fetches.
        */

       uint8_t pos, next_pos = 0;
       uint64_t pkt_mask;       /* bitmask representing a single packet */
       struct rte_mbuf *pkt;
       struct rte_mbuf *next_pkt = NULL;
       void *iphdr;
       void *next_iphdr = NULL;

       if (unlikely(pkts_mask == 0))
              return pkts_mask;
       pos = (uint8_t) __builtin_ctzll(pkts_mask);
       pkt_mask = 1LLU << pos;       /* bitmask representing only this packet */
       pkt = pkts[pos];
       iphdr = RTE_MBUF_METADATA_UINT32_PTR(pkt, IP_START);
       rte_prefetch0(iphdr);

       uint64_t bytes_processed = 0;
       /* bitmap of packets left to process */
       uint64_t pkts_to_process = pkts_mask;
       /* bitmap of valid packets to return */
       uint64_t valid_packets = pkts_mask;

       /* prefetch counters, updated below. Most likely counters to update
        * at beginnning */
       rte_prefetch0(&vfw_pipe->counters);

       do {                     /* always execute at least once */

              /* remove this packet from remaining list */
              uint64_t next_pkts_to_process = pkts_to_process &= ~pkt_mask;

              if (likely(next_pkts_to_process)) {
                     /* another packet to process after this, prefetch it */

                     next_pos =
                         (uint8_t) __builtin_ctzll(next_pkts_to_process);
                     next_pkt = pkts[next_pos];
                     next_iphdr =
                         RTE_MBUF_METADATA_UINT32_PTR(next_pkt, IP_START);
                     rte_prefetch0(next_iphdr);
              }

              int discard = 0;
              /* remove this packet from remaining list */
              pkts_to_process &= ~pkt_mask;
              if (enable_hwlb)
                     if (!check_arp_icmp(pkt, vfw_pipe))
                            discard = 1;
              uint32_t packet_length = rte_pktmbuf_pkt_len(pkt);

              bytes_processed += packet_length;

              if (rte_vfw_is_IPv4(pkt)) {
                     struct ipv4_hdr *ihdr4 = (struct ipv4_hdr *)iphdr;

                     /* verify that packet size according to mbuf is at least
                      * as large as the size according to the IP header.
                      */

                     uint32_t ip_length = rte_bswap16(ihdr4->total_length);

                     if (unlikely
                         (ip_length > (packet_length - ETH_HDR_SIZE))) {
                            discard = 1;
                            vfw_pipe->counters->pkts_drop_bad_size++;
                     }

                     /*
                      * IPv4 fragmented if: MF (more fragments) or Fragment
                      * Offset are non-zero. Header in Intel order, so flip
                      * constant to compensate. Note that IPv6 uses a header
                      * extension for identifying fragments.
                      */

                     int fragmented = (ihdr4->fragment_offset & 0xff3f) != 0;
                     uint8_t ttl = ihdr4->time_to_live;

                     if (unlikely(fragmented)) {
                            discard = 1;
                            vfw_pipe->counters->pkts_drop_fragmented++;
                     }

                     /*
                      * Behave like a router, and decrement the TTL of an
                      * IP packet. If this causes the TTL to become zero,
                      * the packet will be discarded. Unlike a router,
                      * no ICMP code 11 (Time * Exceeded) message will be
                      * sent back to the packet originator.
                      */

                     if (unlikely(ttl <= 1)) {
                            /*
                             * about to decrement to zero (or is somehow
                             * already zero), so discard
                             */
                            discard = 1;
                            vfw_pipe->counters->pkts_drop_ttl++;
                     }

                     /*
                      * Dropping the packets other than TCP AND UDP.
                      */

                     uint8_t proto = ihdr4->next_proto_id;

                     if (unlikely(!(proto == IP_TCP_PROTOCOL ||
                                   proto == IP_UDP_PROTOCOL ||
                                   proto == IP_ICMP_PROTOCOL))) {
                            discard = 1;
                            vfw_pipe->counters->
                                   pkts_drop_unsupported_type++;
                     }

                     if (unlikely(discard)) {
                            valid_packets &= ~pkt_mask;
                     }

              } else if (likely(rte_vfw_is_IPv6(pkt))) {
                     struct ipv6_hdr *ihdr6 = (struct ipv6_hdr *)iphdr;

                     /*
                      * verify that packet size according to mbuf is at least
                      * as large as the size according to the IP header.
                      * For IPv6, note that size includes header extensions
                      * but not the base header size
                      */

                     uint32_t ip_length =
                         rte_bswap16(ihdr6->payload_len) + IPv6_HEADER_SIZE;

                     if (unlikely
                         (ip_length > (packet_length - ETH_HDR_SIZE))) {
                            discard = 1;
                            vfw_pipe->counters->pkts_drop_bad_size++;
                     }

                     /*
                      * Dropping the packets other than TCP AND UDP.
                      */

                     uint8_t proto = ihdr6->proto;

                     if (unlikely(!(proto == IP_TCP_PROTOCOL ||
                                   proto == IP_UDP_PROTOCOL ||
                                   proto == IP_ICMP_PROTOCOL))) {
                            discard = 1;
                            if (proto == IPv6_FRAGMENT_HEADER)
                                   vfw_pipe->counters->
                                       pkts_drop_fragmented++;
                            else
                                   vfw_pipe->counters->
                                       pkts_drop_unsupported_type++;
                     }

                     /*
                      * Behave like a router, and decrement the TTL of an
                      * IP packet. If this causes the TTL to become zero,
                      * the packet will be discarded. Unlike a router,
                      * no ICMP code 11 (Time * Exceeded) message will be
                      * sent back to the packet originator.
                      */

                     if (unlikely(ihdr6->hop_limits <= 1)) {
                            /*
                             * about to decrement to zero (or is somehow
                             * already zero), so discard
                             */
                            discard = 1;
                            vfw_pipe->counters->pkts_drop_ttl++;
                     }

                     if (unlikely(discard))
                            valid_packets &= ~pkt_mask;
                     else
                            ihdr6->hop_limits--;
              } else
                     /* discard non-ip */
                     valid_packets &= ~pkt_mask;

              /* make next packet data the current */
              pkts_to_process = next_pkts_to_process;
              pos = next_pos;
              pkt = next_pkt;
              iphdr = next_iphdr;
              pkt_mask = 1LLU << pos;

       } while (pkts_to_process);

       /* finalize counters, etc. */
       vfw_pipe->counters->bytes_processed += bytes_processed;

       if (likely(firewall_flag))
              return valid_packets;
       else
              return pkts_mask;
}
/**
 * Performs basic VFW ipv4 packet filtering.
 * @param pkts
 *  A pointer to the packets.
 * @param pkts_mask
 *  packet mask.
 * @param vfw_pipe
 *  A pointer to VFW pipeline.
 */

static uint64_t
rte_vfw_ipv4_packet_filter_and_process(struct rte_mbuf **pkts,
                                 uint64_t pkts_mask,
                                 struct pipeline_vfw *vfw_pipe)
{

       /*
        * Make use of cache prefetch. At beginning of loop, want to prefetch
        * mbuf data for next iteration (not current one).
        * Note that ethernet header (14 bytes) is cache aligned. IPv4 header
        * is 20 bytes (extensions not supported), while the IPv6 header is 40
        * bytes. TCP header is 20 bytes, UDP is 8. One cache line prefetch
        * will cover IPv4 and TCP or UDP, but to get IPv6 and TCP,
        * need two pre-fetches.
        */

       uint8_t pos, next_pos = 0;
       uint64_t pkt_mask;       /* bitmask representing a single packet */
       struct rte_mbuf *pkt;
       struct rte_mbuf *next_pkt = NULL;
       struct ipv4_hdr *ihdr4;
       void *next_iphdr = NULL;

       if (unlikely(pkts_mask == 0))
              return pkts_mask;
       pos = (uint8_t) __builtin_ctzll(pkts_mask);
       pkt_mask = 1LLU << pos;       /* bitmask representing only this packet */
       pkt = pkts[pos];

       uint64_t bytes_processed = 0;
       /* bitmap of packets left to process */
       uint64_t pkts_to_process = pkts_mask;
       /* bitmap of valid packets to return */
       uint64_t valid_packets = pkts_mask;

       rte_prefetch0(pkt);
       /* prefetch counters, updated below. Most likely counters to update
        * at beginnning */
       rte_prefetch0(&vfw_pipe->counters);

       do {                     /* always execute at least once */

              /* remove this packet from remaining list */
              uint64_t next_pkts_to_process = pkts_to_process &= ~pkt_mask;

              if (likely(next_pkts_to_process)) {
                     /* another packet to process after this, prefetch it */

                     next_pos =
                            (uint8_t) __builtin_ctzll(next_pkts_to_process);
                     next_pkt = pkts[next_pos];
                     next_iphdr = RTE_MBUF_METADATA_UINT32_PTR(next_pkt,
                                   IP_START);
                     rte_prefetch0(next_iphdr);
              }

              int discard = 0;
              /* remove this packet from remaining list */
              pkts_to_process &= ~pkt_mask;

              if (enable_hwlb)
                     if (!check_arp_icmp(pkt, vfw_pipe))
                            discard = 1;

              uint32_t packet_length = rte_pktmbuf_pkt_len(pkt);

              bytes_processed += packet_length;

              ihdr4 = (struct ipv4_hdr *)
                     RTE_MBUF_METADATA_UINT32_PTR(pkt, IP_START);

              /* verify that packet size according to mbuf is at least
               * as large as the size according to the IP header.
               */

              uint32_t ip_length = rte_bswap16(ihdr4->total_length);

              if (unlikely
                            (ip_length > (packet_length - ETH_HDR_SIZE))) {
                     discard = 1;
                     vfw_pipe->counters->pkts_drop_bad_size++;
              }

              /*
               * IPv4 fragmented if: MF (more fragments) or Fragment
               * Offset are non-zero. Header in Intel order, so flip
               * constant to compensate. Note that IPv6 uses a header
               * extension for identifying fragments.
               */

              int fragmented = (ihdr4->fragment_offset & 0xff3f) != 0;
              uint8_t ttl = ihdr4->time_to_live;

              if (unlikely(fragmented)) {
                     discard = 1;
                     vfw_pipe->counters->pkts_drop_fragmented++;
              }

              /*
               * Behave like a router, and decrement the TTL of an
               * IP packet. If this causes the TTL to become zero,
               * the packet will be discarded. Unlike a router,
               * no ICMP code 11 (Time * Exceeded) message will be
               * sent back to the packet originator.
               */

              if (unlikely(ttl <= 1)) {
                     /*
                      * about to decrement to zero (or is somehow
                      * already zero), so discard
                      */
                     discard = 1;
                     vfw_pipe->counters->pkts_drop_ttl++;
              }

              /*
               * Dropping the packets other than TCP AND UDP.
               */

              uint8_t proto = ihdr4->next_proto_id;

              if (unlikely(!(proto == IP_TCP_PROTOCOL ||
                                          proto == IP_UDP_PROTOCOL ||
                                          proto == IP_ICMP_PROTOCOL))) {
                     discard = 1;
                     vfw_pipe->counters->
                            pkts_drop_unsupported_type++;
              }

              if (unlikely(discard)) {
                     valid_packets &= ~pkt_mask;
              } else {
                     ihdr4->time_to_live = ttl - 1;

                     /* update header checksum, from rfc 1141 */
                     uint32_t sum;
                     uint16_t checksum = rte_bswap16(
                                   ihdr4->hdr_checksum);
                     /* increment checksum high byte */
                     sum = checksum + 0x100;
                     /* add carry */
                     checksum = (sum + (sum >> BIT_CARRY));
                     ihdr4->hdr_checksum = rte_bswap16(checksum);
              }

              /* make next packet data the current */
              pkts_to_process = next_pkts_to_process;
              pos = next_pos;
              pkt = next_pkt;
              ihdr4 = next_iphdr;
              pkt_mask = 1LLU << pos;

       } while (pkts_to_process);

       /* finalize counters, etc. */
       vfw_pipe->counters->bytes_processed += bytes_processed;

       if (likely(firewall_flag))
              return valid_packets;
       else
              return pkts_mask;
}
/**
 * Performs basic VFW IPV6 packet filtering.
 * @param pkts
 *  A pointer to the packets.
 * @param pkts_mask
 *  packet mask.
 * @param vfw_pipe
 *  A pointer to VFW pipeline.
 */
       static uint64_t
rte_vfw_ipv6_packet_filter_and_process(struct rte_mbuf **pkts,
              uint64_t pkts_mask,
              struct pipeline_vfw *vfw_pipe)
{

       /*
        * Make use of cache prefetch. At beginning of loop, want to prefetch
        * mbuf data for next iteration (not current one).
        * Note that ethernet header (14 bytes) is cache aligned. IPv4 header
        * is 20 bytes (extensions not supported), while the IPv6 header is 40
        * bytes. TCP header is 20 bytes, UDP is 8. One cache line prefetch
        * will cover IPv4 and TCP or UDP, but to get IPv6 and TCP,
        * need two pre-fetches.
        */

       uint8_t pos, next_pos = 0;
       uint64_t pkt_mask;       /* bitmask representing a single packet */
       struct rte_mbuf *pkt;
       struct rte_mbuf *next_pkt = NULL;
       struct ipv6_hdr *ihdr6;
       void *next_iphdr = NULL;

       if (unlikely(pkts_mask == 0))
              return pkts_mask;
       pos = (uint8_t) __builtin_ctzll(pkts_mask);
       pkt_mask = 1LLU << pos;       /* bitmask representing only this packet */
       pkt = pkts[pos];

       uint64_t bytes_processed = 0;
       /* bitmap of packets left to process */
       uint64_t pkts_to_process = pkts_mask;
       /* bitmap of valid packets to return */
       uint64_t valid_packets = pkts_mask;

       /* prefetch counters, updated below. Most likely counters to update
        * at beginnning */
       rte_prefetch0(&vfw_pipe->counters);

       do {                     /* always execute at least once */

              /* remove this packet from remaining list */
              uint64_t next_pkts_to_process = pkts_to_process &= ~pkt_mask;

              if (likely(next_pkts_to_process)) {
                     /* another packet to process after this, prefetch it */

                     next_pos =
                         (uint8_t) __builtin_ctzll(next_pkts_to_process);
                     next_pkt = pkts[next_pos];
                     next_iphdr =
                         RTE_MBUF_METADATA_UINT32_PTR(next_pkt, IP_START);
                     rte_prefetch0(next_iphdr);
              }

              int discard = 0;
              /* remove this packet from remaining list */
              pkts_to_process &= ~pkt_mask;

              if (enable_hwlb)
                     if (!check_arp_icmp(pkt, vfw_pipe))
                            discard = 1;

              uint32_t packet_length = rte_pktmbuf_pkt_len(pkt);

              bytes_processed += packet_length;

              ihdr6 = (struct ipv6_hdr *)
                     RTE_MBUF_METADATA_UINT32_PTR(pkt, IP_START);

              /*
               * verify that packet size according to mbuf is at least
               * as large as the size according to the IP header.
               * For IPv6, note that size includes header extensions
               * but not the base header size
               */

              uint32_t ip_length =
                     rte_bswap16(ihdr6->payload_len) + IPv6_HEADER_SIZE;

              if (unlikely
                            (ip_length > (packet_length - ETH_HDR_SIZE))) {
                     discard = 1;
                     vfw_pipe->counters->pkts_drop_bad_size++;
              }

              /*
               * Dropping the packets other than TCP AND UDP.
               */

              uint8_t proto = ihdr6->proto;

              if (unlikely(!(proto == IP_TCP_PROTOCOL ||
                                          proto == IP_UDP_PROTOCOL ||
                                          proto == IP_ICMP_PROTOCOL))) {
                     discard = 1;
                     if (proto == IPv6_FRAGMENT_HEADER)
                            vfw_pipe->counters->
                                   pkts_drop_fragmented++;
                     else
                            vfw_pipe->counters->
                                   pkts_drop_unsupported_type++;
              }

              /*
               * Behave like a router, and decrement the TTL of an
               * IP packet. If this causes the TTL to become zero,
               * the packet will be discarded. Unlike a router,
               * no ICMP code 11 (Time * Exceeded) message will be
               * sent back to the packet originator.
               */

              if (unlikely(ihdr6->hop_limits <= 1)) {
                     /*
                      * about to decrement to zero (or is somehow
                      * already zero), so discard
                      */
                     discard = 1;
                     vfw_pipe->counters->pkts_drop_ttl++;
              }

              if (unlikely(discard))
                     valid_packets &= ~pkt_mask;
              else
                     ihdr6->hop_limits--;

              /* make next packet data the current */
              pkts_to_process = next_pkts_to_process;
              pos = next_pos;
              pkt = next_pkt;
              ihdr6 = next_iphdr;
              pkt_mask = 1LLU << pos;

       } while (pkts_to_process);

       /* finalize counters, etc. */
       vfw_pipe->counters->bytes_processed += bytes_processed;

       if (likely(firewall_flag))
              return valid_packets;
       else
              return pkts_mask;
}

/**
 * exchange the mac address so source becomes destination and vice versa.
 *
 * @param ehdr
 *  A pointer to the ethernet header.
 *
 */
static inline void rte_sp_exchange_mac_addresses(struct ether_hdr *ehdr)
{
       struct ether_addr saved_copy;

       ether_addr_copy(&ehdr->d_addr, &saved_copy);
       ether_addr_copy(&ehdr->s_addr, &ehdr->d_addr);
       ether_addr_copy(&saved_copy, &ehdr->s_addr);
}
/**
 * walk every valid mbuf (denoted by pkts_mask) and apply arp to the packet.
 * To support synproxy, some (altered) packets may need to be sent back where
 * they came from. The ip header has already been adjusted, but the ethernet
 * header has not, so this must be performed here.
 * Return an updated pkts_mask, since arp may drop some packets
 *
 * @param pkts
 *  A pointer to the packet.
 * @param pkts_mask
 *  Packet mask
 * @param synproxy_reply_mask
 *  Reply Packet mask for Synproxy
 * @param vfw_pipe
 *  A pointer to VFW pipeline.
 */

static uint64_t
rte_vfw_arp_packets(struct rte_mbuf **pkts,
              uint64_t pkts_mask,
              uint64_t synproxy_reply_mask,
              struct pipeline_vfw *vfw_pipe)
{
       uint64_t pkts_to_arp = pkts_mask;
       uint32_t ret;
       uint32_t dest_if = INVALID_DESTIF;
       int ret_mac;

       for (; pkts_to_arp;) {
              struct ether_addr hw_addr;
              struct mbuf_tcp_meta_data *meta_data_addr;
              struct ether_hdr *ehdr;
              struct rte_mbuf *pkt;
              uint16_t phy_port;
              uint8_t pos = (uint8_t) __builtin_ctzll(pkts_to_arp);
              /* bitmask representing only this packet */
              uint64_t pkt_mask = 1LLU << pos;
              /* remove this packet from remaining list */
              pkts_to_arp &= ~pkt_mask;
              pkt = pkts[pos];
              int must_reverse = ((synproxy_reply_mask & pkt_mask) != 0);

              phy_port = pkt->port;
              meta_data_addr = (struct mbuf_tcp_meta_data *)
                     RTE_MBUF_METADATA_UINT32_PTR(pkt, META_DATA_OFFSET);
              ehdr = rte_vfw_get_ether_addr(pkt);

              void *iphdr = RTE_MBUF_METADATA_UINT32_PTR(pkt, IP_START);

              if (rte_vfw_is_IPv4(pkt)) {
                     struct ipv4_hdr *ihdr = (struct ipv4_hdr *)iphdr;
                     uint32_t nhip = 0;

                     uint32_t dest_address = rte_bswap32(ihdr->dst_addr);

                     ret = local_get_nh_ipv4(dest_address, &dest_if,
                                   &nhip, vfw_pipe);
                     if (must_reverse) {
                            rte_sp_exchange_mac_addresses(ehdr);
                            if (is_phy_port_privte(phy_port)) {
                                   if (!ret) {
                                          dest_if = get_pub_to_prv_port(
                                                        &dest_address,
                                                        IP_VERSION_4);
                                          if (dest_if == INVALID_DESTIF) {
                                                 pkts_mask &= ~pkt_mask;
                                                 vfw_pipe->counters->
                                          pkts_drop_without_arp_entry++;
                                          }
                                          do_local_nh_ipv4_cache(dest_if,
                                                        vfw_pipe);
                                   }

                            } else {
                                   if (!ret) {
                                          dest_if = get_prv_to_pub_port(
                                                        &dest_address,
                                                        IP_VERSION_4);
                                          if (dest_if == INVALID_DESTIF) {
                                                 pkts_mask &= ~pkt_mask;
                                                 vfw_pipe->counters->
                                          pkts_drop_without_arp_entry++;
                                          }
                                          do_local_nh_ipv4_cache(dest_if,
                                                        vfw_pipe);
                                   }
                            }

                     } else if (is_phy_port_privte(phy_port)) {
                            if (!ret) {
                                   dest_if = get_prv_to_pub_port(
                                                 &dest_address,
                                                 IP_VERSION_4);
                                   if (dest_if == INVALID_DESTIF) {
                                          pkts_mask &= ~pkt_mask;
                                          vfw_pipe->counters->
                                          pkts_drop_without_arp_entry++;
                                   }
                                   do_local_nh_ipv4_cache(dest_if,
                                                 vfw_pipe);
                            }

                     } else {
                            if (!ret) {
                                   dest_if = get_pub_to_prv_port(
                                                 &dest_address,
                                                 IP_VERSION_4);
                                   if (dest_if == INVALID_DESTIF) {
                                          pkts_mask &= ~pkt_mask;
                                          vfw_pipe->counters->
                                          pkts_drop_without_arp_entry++;
                                   }
                                   do_local_nh_ipv4_cache(dest_if,
                                                 vfw_pipe);
                            }
                     }

                     meta_data_addr->output_port =
                            vfw_pipe->outport_id[dest_if];
                     if (local_dest_mac_present(dest_if)) {
                            ether_addr_copy(get_local_link_hw_addr(dest_if),
                                          &ehdr->d_addr);
                            ether_addr_copy(get_link_hw_addr(dest_if),
                                          &ehdr->s_addr);
                     } else {
                            ret_mac = get_dest_mac_addr_port(dest_address,
                                          &dest_if, &hw_addr);
                            if (ret_mac == ARP_FOUND) {

                                   link_hw_laddr_valid[dest_if] = 1;
                                   memcpy(&link_hw_laddr[dest_if], &hw_addr,
                                                 sizeof(struct ether_addr));

                                   ether_addr_copy(&hw_addr,
                                          &ehdr->d_addr);
                            ether_addr_copy(get_link_hw_addr(dest_if),
                                          &ehdr->s_addr);

                            if (vfw_debug >= DEBUG_LEVEL_4) {
                                   char buf[HW_ADDR_SIZE];

                                   ether_format_addr(buf, sizeof(buf),
                                                 &hw_addr);
                                   printf("MAC found for ip 0x%"PRIx32
                                                 ",dest_if %d: %s, ",
                                                 dest_address,
                                                 dest_if, buf);
                                   ether_format_addr(buf, sizeof(buf),
                                                 &ehdr->s_addr);
                                   printf("new eth hdr src: %s, ", buf);
                                   ether_format_addr(buf, sizeof(buf),
                                                 &ehdr->d_addr);
                                   printf("new eth hdr dst: %s\n", buf);
                            }

                     } else {

                            if (vfw_debug >= DEBUG_LEVEL_4) {
                                   char buf[HW_ADDR_SIZE];

                                   ether_format_addr(buf, sizeof(buf),
                                                 &hw_addr);
                                   printf("MAC NOT FOUND for ip 0x%"
                                                 PRIx32", dest_if %"
                                                 PRId16": %s, ",
                                                 dest_address,
                                                 dest_if, buf);
                            }
                            /* ICMP req sent, drop packet by
                             * changing the mask */
                            pkts_mask &= ~pkt_mask;
                            vfw_pipe->
                                   counters->pkts_drop_without_arp_entry++;
                     }

                     }
              } else if (likely(rte_vfw_is_IPv6(pkt))) {
                     struct ipv6_hdr *ihdr = (struct ipv6_hdr *)iphdr;
                     uint8_t dest_addr_ipv6[IPV6_ADD_SIZE];

                     rte_mov16(dest_addr_ipv6, ihdr->dst_addr);
                     uint8_t nh_ipv6[IPV6_ADD_SIZE];

                     memset(nh_ipv6, 0, IPV6_ADD_SIZE);
                     ret = local_get_nh_ipv6(&dest_addr_ipv6[0], &dest_if,
                                   &nh_ipv6[0], vfw_pipe);
                     if (must_reverse) {
                            rte_sp_exchange_mac_addresses(ehdr);
                            if (is_phy_port_privte(phy_port)) {
                                   if (!ret) {
                                          dest_if = get_pub_to_prv_port(
                                                 (uint32_t *)
                                                 &dest_addr_ipv6[0],
                                                 IP_VERSION_6);
                                          if (dest_if == INVALID_DESTIF) {
                                                 pkts_mask &= ~pkt_mask;
                                                 vfw_pipe->counters->
                                          pkts_drop_without_arp_entry++;
                                          }
                                          do_local_nh_ipv6_cache(dest_if,
                                                        vfw_pipe);
                                   }

                            } else {
                                   if (!ret) {
                                          dest_if = get_prv_to_pub_port(
                                                 (uint32_t *)
                                                 &dest_addr_ipv6[0],
                                                 IP_VERSION_6);
                                          if (dest_if == INVALID_DESTIF) {
                                                 pkts_mask &= ~pkt_mask;
                                                 vfw_pipe->counters->
                                          pkts_drop_without_arp_entry++;
                                          }
                                          do_local_nh_ipv6_cache(dest_if,
                                                        vfw_pipe);
                                   }


                            }

                     } else if (is_phy_port_privte(phy_port)) {
                            if (!ret) {
                                   dest_if = get_prv_to_pub_port(
                                                 (uint32_t *)
                                                 &dest_addr_ipv6[0],
                                                 IP_VERSION_6);
                                   if (dest_if == INVALID_DESTIF) {
                                          pkts_mask &= ~pkt_mask;
                                          vfw_pipe->counters->
                                          pkts_drop_without_arp_entry++;
                                   }
                                   do_local_nh_ipv6_cache(dest_if,
                                                 vfw_pipe);
                            }

                     } else {
                            if (!ret) {
                                   dest_if = get_pub_to_prv_port(
                                                 (uint32_t *)
                                                 &dest_addr_ipv6[0],
                                                 IP_VERSION_6);
                                   if (dest_if == INVALID_DESTIF) {
                                          pkts_mask &= ~pkt_mask;
                                          vfw_pipe->counters->
                                          pkts_drop_without_arp_entry++;
                                   }
                                   do_local_nh_ipv6_cache(dest_if,
                                                 vfw_pipe);
                            }

                     }
                     meta_data_addr->output_port = vfw_pipe->
                                          outport_id[dest_if];

                     memset(nh_ipv6, 0, IPV6_ADD_SIZE);
                     if (get_dest_mac_address_ipv6_port(
                                          &dest_addr_ipv6[0],
                                          &dest_if,
                                          &hw_addr,
                                          &nh_ipv6[0])) {
                            ether_addr_copy(&hw_addr, &ehdr->d_addr);
                            ether_addr_copy(get_link_hw_addr(dest_if),
                                          &ehdr->s_addr);

                            if (vfw_debug >= DEBUG_LEVEL_4) {
                                   char buf[HW_ADDR_SIZE];

                                   ether_format_addr(buf, sizeof(buf),
                                                 &hw_addr);
                                   printf("MAC found for  dest_if %d: %s,",
                                                 dest_if, buf);
                                   ether_format_addr(buf, sizeof(buf),
                                                 &ehdr->s_addr);
                                   printf("new eth hdr src: %s, ", buf);
                                   ether_format_addr(buf, sizeof(buf),
                                                 &ehdr->d_addr);
                                   printf("new eth hdr dst: %s\n", buf);
                            }

                     } else {
                            printf("deleting ipv6\n");
                            pkts_mask &= ~pkt_mask;
                            /*Next Neighbor is not yet implemented
                             * for ipv6.*/
                            vfw_pipe->counters->
                                   pkts_drop_without_arp_entry++;
                     }

              } else
                     /* neither IPv4 or IPv6, drop quietly */
                     pkts_mask &= ~pkt_mask;
       }
       return pkts_mask;
}

#ifdef EN_SWP_ARP

/**
 * walk every valid mbuf (denoted by pkts_mask) and apply arp to the packet.
 * To support synproxy, some (altered) packets may need to be sent back where
 * they came from. The ip header has already been adjusted, but the ethernet
 * header has not, so this must be performed here.
 * Return an updated pkts_mask, since arp may drop some packets
 *
 * @param pkts
 *  A pointer to the packet array.
 * @param pkt_num
 *  Packet num to start processing
 * @param pkts_mask
 *  Packet mask
 * @param synproxy_reply_mask
 *  Reply Packet mask for Synproxy
 * @param vfw_pipe
 *  A pointer to VFW pipeline.
 */
static void
pkt4_work_vfw_arp_ipv4_packets(struct rte_mbuf **pkts,
              uint16_t pkt_num,
              uint64_t *pkts_mask,
              uint64_t synproxy_reply_mask,
              struct pipeline_vfw *vfw_pipe)
{

       uint32_t ret;
       int ret_mac;
       uint8_t i;

       struct ether_addr hw_addr;
       struct mbuf_tcp_meta_data *meta_data_addr;
       struct ether_hdr *ehdr;
       struct rte_mbuf *pkt;
       uint16_t phy_port;

       for (i = 0; i < 4; i++) {
              uint32_t dest_if = INVALID_DESTIF;
              /* bitmask representing only this packet */
              uint64_t pkt_mask = 1LLU << (pkt_num + i);

              pkt = pkts[i];

              if(!(*pkts_mask & pkt_mask))
                     continue;

              int must_reverse = ((synproxy_reply_mask & pkt_mask) != 0);

              phy_port = pkt->port;
              meta_data_addr = (struct mbuf_tcp_meta_data *)
                     RTE_MBUF_METADATA_UINT32_PTR(pkt, META_DATA_OFFSET);
              ehdr = rte_vfw_get_ether_addr(pkt);


              struct ipv4_hdr *ihdr = (struct ipv4_hdr *)
                     RTE_MBUF_METADATA_UINT32_PTR(pkt, IP_START);
              uint32_t nhip = 0;

              uint32_t dest_address = rte_bswap32(ihdr->dst_addr);

              ret = local_get_nh_ipv4(dest_address, &dest_if,
                            &nhip, vfw_pipe);
              if (must_reverse) {
                     rte_sp_exchange_mac_addresses(ehdr);
                     if (is_phy_port_privte(phy_port)) {
                            if (!ret) {
                                   dest_if = get_pub_to_prv_port(
                                                 &dest_address,
                                                 IP_VERSION_4);
                                   if (dest_if == INVALID_DESTIF) {
                                          *pkts_mask &= ~pkt_mask;
                                          vfw_pipe->counters->
                                          pkts_drop_without_arp_entry++;
                                   }
                                   do_local_nh_ipv4_cache(
                                                 dest_if, vfw_pipe);
                            }

                     } else {
                            if (!ret) {
                                   dest_if = get_prv_to_pub_port(
                                                 &dest_address,
                                                 IP_VERSION_4);
                                   if (dest_if == INVALID_DESTIF) {
                                          *pkts_mask &= ~pkt_mask;
                                          vfw_pipe->counters->
                                          pkts_drop_without_arp_entry++;
                                   }
                                   do_local_nh_ipv4_cache(dest_if,
                                                 vfw_pipe);
                            }
                     }
              } else if (is_phy_port_privte(phy_port)) {
                     if (!ret) {
                            dest_if = get_prv_to_pub_port(&dest_address,
                                          IP_VERSION_4);
                            if (dest_if == INVALID_DESTIF) {
                                   *pkts_mask &= ~pkt_mask;
                                   vfw_pipe->counters->
                                          pkts_drop_without_arp_entry++;
                            }
                            do_local_nh_ipv4_cache(dest_if, vfw_pipe);
                     }

              } else {
                     if (!ret) {
                            dest_if = get_pub_to_prv_port(&dest_address,
                                          IP_VERSION_4);
                            if (dest_if == INVALID_DESTIF) {
                                   *pkts_mask &= ~pkt_mask;
                                   vfw_pipe->counters->
                                          pkts_drop_without_arp_entry++;
                            }
                            do_local_nh_ipv4_cache(dest_if, vfw_pipe);
                     }

              }
              meta_data_addr->output_port =  vfw_pipe->outport_id[dest_if];
              if (local_dest_mac_present(dest_if)) {
                     ether_addr_copy(get_local_link_hw_addr(dest_if),
                                   &ehdr->d_addr);
                     ether_addr_copy(get_link_hw_addr(dest_if),
                                   &ehdr->s_addr);
              } else {
                     ret_mac = get_dest_mac_addr_port(dest_address,
                                   &dest_if, &hw_addr);
                     if (ret_mac == ARP_FOUND) {

                            link_hw_laddr_valid[dest_if] = 1;
                            memcpy(&link_hw_laddr[dest_if], &hw_addr,
                                          sizeof(struct ether_addr));

                            ether_addr_copy(&hw_addr, &ehdr->d_addr);
                            ether_addr_copy(get_link_hw_addr(dest_if),
                                          &ehdr->s_addr);

                            if (vfw_debug >= DEBUG_LEVEL_4) {
                                   char buf[HW_ADDR_SIZE];

                                   ether_format_addr(buf, sizeof(buf),
                                                 &hw_addr);
                                   printf("MAC found for ip 0x%"
                                          PRIx32", dest_if %d: %s, ",
                                          dest_address,
                                          dest_if, buf);
                                   ether_format_addr(buf, sizeof(buf),
                                                 &ehdr->s_addr);
                                   printf("new eth hdr src: %s, ", buf);
                                   ether_format_addr(buf, sizeof(buf),
                                                 &ehdr->d_addr);
                                   printf("new eth hdr dst: %s\n", buf);
                            }

                     } else {

                            if (vfw_debug >= DEBUG_LEVEL_4) {
                                   char buf[HW_ADDR_SIZE];

                                   ether_format_addr(buf, sizeof(buf),
                                          &hw_addr);
                                   printf("MAC NOT FOUND for ip 0x%"
                                          PRIx32", dest_if %"
                                          PRId16": %s, ",
                                          dest_address,
                                          dest_if, buf);
                            }
                            /* ICMP req sent, drop packet by
                             * changing the mask */
                            *pkts_mask &= ~pkt_mask;
                            vfw_pipe->
                                   counters->pkts_drop_without_arp_entry++;
                     }
              }
       }
}


/**
 * walk every valid mbuf (denoted by pkts_mask) and apply arp to the packet.
 * To support synproxy, some (altered) packets may need to be sent back where
 * they came from. The ip header has already been adjusted, but the ethernet
 * header has not, so this must be performed here.
 * Return an updated pkts_mask, since arp may drop some packets
 *
 * @param pkts
 *  A pointer to the packet.
 * @param packet_num
 *  Packet number to process
 * @param pkts_mask
 *  Packet mask pointer
 * @param synproxy_reply_mask
 *  Reply Packet mask for Synproxy
 * @param vfw_pipe
 *  A pointer to VFW pipeline.
 */
static void
pkt_work_vfw_arp_ipv4_packets(struct rte_mbuf *pkts,
              uint16_t pkt_num,
              uint64_t *pkts_mask,
              uint64_t synproxy_reply_mask,
              struct pipeline_vfw *vfw_pipe)
{

       uint32_t ret;
       uint32_t dest_if = INVALID_DESTIF;
       int ret_mac;

       struct ether_addr hw_addr;
       struct mbuf_tcp_meta_data *meta_data_addr;
       struct ether_hdr *ehdr;
       struct rte_mbuf *pkt;
       uint16_t phy_port;
       uint64_t pkt_mask = 1LLU << pkt_num;

       pkt = pkts;

       if(*pkts_mask & pkt_mask) {

              int must_reverse = ((synproxy_reply_mask & pkt_mask) != 0);

              phy_port = pkt->port;
              meta_data_addr = (struct mbuf_tcp_meta_data *)
                     RTE_MBUF_METADATA_UINT32_PTR(pkt, META_DATA_OFFSET);
              ehdr = rte_vfw_get_ether_addr(pkt);


              struct ipv4_hdr *ihdr = (struct ipv4_hdr *)
                     RTE_MBUF_METADATA_UINT32_PTR(pkt, IP_START);
              uint32_t nhip = 0;

              uint32_t dest_address = rte_bswap32(ihdr->dst_addr);

              ret = local_get_nh_ipv4(dest_address, &dest_if,
                            &nhip, vfw_pipe);
              if (must_reverse) {
                     rte_sp_exchange_mac_addresses(ehdr);
                     if (is_phy_port_privte(phy_port)) {
                            if (!ret) {
                                   dest_if = get_pub_to_prv_port(
                                                 &dest_address,
                                                 IP_VERSION_4);
                                   if (dest_if == INVALID_DESTIF) {
                                          *pkts_mask &= ~pkt_mask;
                                          vfw_pipe->counters->
                                          pkts_drop_without_arp_entry++;
                                   }
                                   do_local_nh_ipv4_cache(
                                                 dest_if, vfw_pipe);
                            }

                     } else {
                            if (!ret) {
                                   dest_if = get_prv_to_pub_port(
                                                 &dest_address,
                                                 IP_VERSION_4);
                                   if (dest_if == INVALID_DESTIF) {
                                          *pkts_mask &= ~pkt_mask;
                                          vfw_pipe->counters->
                                          pkts_drop_without_arp_entry++;
                                   }
                                   do_local_nh_ipv4_cache(dest_if,
                                                 vfw_pipe);
                            }
                     }
              } else if (is_phy_port_privte(phy_port)) {
                     if (!ret) {
                            dest_if = get_prv_to_pub_port(&dest_address,
                                          IP_VERSION_4);
                            if (dest_if == INVALID_DESTIF) {
                                   *pkts_mask &= ~pkt_mask;
                                   vfw_pipe->counters->
                                          pkts_drop_without_arp_entry++;
                            }
                            do_local_nh_ipv4_cache(dest_if, vfw_pipe);
                     }

              } else {
                     if (!ret) {
                            dest_if = get_pub_to_prv_port(&dest_address,
                                          IP_VERSION_4);
                            if (dest_if == INVALID_DESTIF) {
                                   *pkts_mask &= ~pkt_mask;
                                   vfw_pipe->counters->
                                          pkts_drop_without_arp_entry++;
                            }
                            do_local_nh_ipv4_cache(dest_if, vfw_pipe);
                     }

              }
              meta_data_addr->output_port =  vfw_pipe->outport_id[dest_if];
              if (local_dest_mac_present(dest_if)) {
                     ether_addr_copy(get_local_link_hw_addr(dest_if),
                                   &ehdr->d_addr);
                     ether_addr_copy(get_link_hw_addr(dest_if),
                                   &ehdr->s_addr);
              } else {
                     ret_mac = get_dest_mac_addr_port(dest_address,
                                   &dest_if, &hw_addr);
                     if (ret_mac) {
                            link_hw_laddr_valid[dest_if] = 1;
                            memcpy(&link_hw_laddr[dest_if], &hw_addr,
                                          sizeof(struct ether_addr));

                            ether_addr_copy(&hw_addr, &ehdr->d_addr);
                            ether_addr_copy(get_link_hw_addr(dest_if),
                                          &ehdr->s_addr);

                            if (vfw_debug >= DEBUG_LEVEL_4) {
                                   char buf[HW_ADDR_SIZE];

                                   ether_format_addr(buf, sizeof(buf),
                                                 &hw_addr);
                                   printf("MAC found for ip 0x%"
                                          PRIx32", dest_if %d: %s, ",
                                          dest_address,
                                          dest_if, buf);
                                   ether_format_addr(buf, sizeof(buf),
                                                 &ehdr->s_addr);
                                   printf("new eth hdr src: %s, ", buf);
                                   ether_format_addr(buf, sizeof(buf),
                                                 &ehdr->d_addr);
                                   printf("new eth hdr dst: %s\n", buf);
                            }

                     } else {
                            if (vfw_debug >= DEBUG_LEVEL_4) {
                                   char buf[HW_ADDR_SIZE];

                                   ether_format_addr(buf, sizeof(buf),
                                                 &hw_addr);
                                   printf("MAC NOT FOUND for ip 0x%"
                                                 PRIx32", dest_if %"
                                                 PRId16": %s, ",
                                                 dest_address,
                                                 dest_if, buf);
                            }
                            /* ICMP req sent, drop packet by
                             * changing the mask */
                            *pkts_mask &= ~pkt_mask;
                            vfw_pipe->
                                   counters->pkts_drop_without_arp_entry++;
                     }
              }

       }
}


/**
 * walk every valid mbuf (denoted by pkts_mask) and apply arp to the packet.
 * To support synproxy, some (altered) packets may need to be sent back where
 * they came from. The ip header has already been adjusted, but the ethernet
 * header has not, so this must be performed here.
 * Return an updated pkts_mask, since arp may drop some packets
 *
 * @param pkts
 *  A pointer to the packets array.
 * @param pkt_num
 *  Packet number to start processing.
 * @param pkts_mask
 *  Packet mask pointer
 * @param synproxy_reply_mask
 *  Reply Packet mask for Synproxy
 * @param vfw_pipe
 *  A pointer to VFW pipeline.
 */

static void
pkt4_work_vfw_arp_ipv6_packets(struct rte_mbuf **pkts,
              uint16_t pkt_num,
              uint64_t *pkts_mask,
              uint64_t synproxy_reply_mask,
              struct pipeline_vfw *vfw_pipe)
{
       uint8_t nh_ipv6[IPV6_ADD_SIZE];
       uint32_t ret;
       struct ether_addr hw_addr;
       struct mbuf_tcp_meta_data *meta_data_addr;
       struct ether_hdr *ehdr;
       struct rte_mbuf *pkt;
       uint16_t phy_port;
       uint8_t i;

       for (i = 0; i < 4; i++) {
              uint32_t dest_if = INVALID_DESTIF;
              /* bitmask representing only this packet */
              uint64_t pkt_mask = 1LLU << (pkt_num + i);

              pkt = pkts[i];

              if(!(*pkts_mask & pkt_mask))
                     continue;
              int must_reverse = ((synproxy_reply_mask & pkt_mask) != 0);

              phy_port = pkt->port;
              meta_data_addr = (struct mbuf_tcp_meta_data *)
                     RTE_MBUF_METADATA_UINT32_PTR(pkt, META_DATA_OFFSET);
              ehdr = rte_vfw_get_ether_addr(pkt);

              struct ipv6_hdr *ihdr = (struct ipv6_hdr *)
                     RTE_MBUF_METADATA_UINT32_PTR(pkt, IP_START);

              uint8_t nhip[IPV6_ADD_SIZE];
              uint8_t dest_address[IPV6_ADD_SIZE];

              memset(nhip, 0, IPV6_ADD_SIZE);

              rte_mov16(dest_address, ihdr->dst_addr);
              ret = local_get_nh_ipv6(&dest_address[0], &dest_if,
                            &nhip[0], vfw_pipe);
              if (must_reverse) {
                     rte_sp_exchange_mac_addresses(ehdr);
                     if (is_phy_port_privte(phy_port)) {
                            if (!ret) {
                                   dest_if = get_pub_to_prv_port(
                                                 (uint32_t *)
                                                 &dest_address[0],
                                                 IP_VERSION_6);
                                   if (dest_if == INVALID_DESTIF) {
                                          *pkts_mask &= ~pkt_mask;
                                          vfw_pipe->counters->
                                          pkts_drop_without_arp_entry++;
                                   }
                                   do_local_nh_ipv6_cache(dest_if,
                                                 vfw_pipe);
                            }

                     } else {
                            if (!ret) {
                                   dest_if = get_prv_to_pub_port(
                                                 (uint32_t *)
                                                 &dest_address[0],
                                                 IP_VERSION_6);
                                   if (dest_if == INVALID_DESTIF) {
                                          *pkts_mask &= ~pkt_mask;
                                          vfw_pipe->counters->
                                          pkts_drop_without_arp_entry++;
                                   }
                                   do_local_nh_ipv6_cache(dest_if,
                                                 vfw_pipe);
                            }
                     }

              } else if (is_phy_port_privte(phy_port)) {
                     if (!ret) {
                            dest_if = get_prv_to_pub_port((uint32_t *)
                                          &dest_address[0], IP_VERSION_6);
                            if (dest_if == INVALID_DESTIF) {
                                   *pkts_mask &= ~pkt_mask;
                                   vfw_pipe->counters->
                                          pkts_drop_without_arp_entry++;
                            }
                            do_local_nh_ipv6_cache(dest_if, vfw_pipe);
                     }

              } else {
                     if (!ret) {
                            dest_if = get_pub_to_prv_port((uint32_t *)
                                          &dest_address[0], IP_VERSION_6);
                            if (dest_if == INVALID_DESTIF) {
                                   *pkts_mask &= ~pkt_mask;
                                   vfw_pipe->counters->
                                          pkts_drop_without_arp_entry++;

                            }
                            do_local_nh_ipv6_cache(dest_if, vfw_pipe);
                     }

              }

              meta_data_addr->output_port = vfw_pipe->outport_id[dest_if];

              memset(nh_ipv6, 0, IPV6_ADD_SIZE);
              if (get_dest_mac_address_ipv6_port(
                                   &dest_address[0],
                                   &dest_if,
                                   &hw_addr,
                                   &nh_ipv6[0])) {
                     ether_addr_copy(&hw_addr, &ehdr->d_addr);
                     ether_addr_copy(get_link_hw_addr(dest_if),
                                   &ehdr->s_addr);

                     if (vfw_debug >= DEBUG_LEVEL_4) {
                            char buf[HW_ADDR_SIZE];

                            ether_format_addr(buf, sizeof(buf),
                                          &hw_addr);
                            printf("MAC found for  dest_if %d: %s, ",
                                          dest_if, buf);
                            ether_format_addr(buf, sizeof(buf),
                                          &ehdr->s_addr);
                            printf("new eth hdr src: %s, ", buf);
                            ether_format_addr(buf, sizeof(buf),
                                          &ehdr->d_addr);
                            printf("new eth hdr dst: %s\n", buf);
                     }

              } else {
                     printf("deleting ipv6\n");
                     *pkts_mask &= ~pkt_mask;
                     /*Next Neighbor is not yet implemented
                      * for ipv6.*/
                     vfw_pipe->counters->
                            pkts_drop_without_arp_entry++;
              }

       }
}


/**
 * walk every valid mbuf (denoted by pkts_mask) and apply arp to the packet.
 * To support synproxy, some (altered) packets may need to be sent back where
 * they came from. The ip header has already been adjusted, but the ethernet
 * header has not, so this must be performed here.
 * Return an updated pkts_mask, since arp may drop some packets
 *
 * @param pkts
 *  A pointer to the packets.
 * @param pkt_num
 *  Packet number to process.
 * @param pkts_mask
 *  Packet mask pointer
 * @param synproxy_reply_mask
 *  Reply Packet mask for Synproxy
 * @param vfw_pipe
 *  A pointer to VFW pipeline.
 */

static void
pkt_work_vfw_arp_ipv6_packets(struct rte_mbuf *pkts,
              uint16_t pkt_num,
              uint64_t *pkts_mask,
              uint64_t synproxy_reply_mask,
              struct pipeline_vfw *vfw_pipe)
{
       uint8_t nh_ipv6[IPV6_ADD_SIZE];
       uint32_t ret;
       struct ether_addr hw_addr;
       struct mbuf_tcp_meta_data *meta_data_addr;
       struct ether_hdr *ehdr;
       struct rte_mbuf *pkt;
       uint16_t phy_port;

       uint32_t dest_if = INVALID_DESTIF;
       /* bitmask representing only this packet */
       uint64_t pkt_mask = 1LLU << pkt_num;

       pkt = pkts;

       if(*pkts_mask & pkt_mask) {

              int must_reverse = ((synproxy_reply_mask & pkt_mask) != 0);

              phy_port = pkt->port;
              meta_data_addr = (struct mbuf_tcp_meta_data *)
                     RTE_MBUF_METADATA_UINT32_PTR(pkt, META_DATA_OFFSET);
              ehdr = rte_vfw_get_ether_addr(pkt);

              struct ipv6_hdr *ihdr = (struct ipv6_hdr *)
                     RTE_MBUF_METADATA_UINT32_PTR(pkt, IP_START);

              uint8_t nhip[IPV6_ADD_SIZE];
              uint8_t dest_address[IPV6_ADD_SIZE];

              memset(nhip, 0, IPV6_ADD_SIZE);

              rte_mov16(dest_address, ihdr->dst_addr);
              ret = local_get_nh_ipv6(&dest_address[0], &dest_if,
                            &nhip[0], vfw_pipe);
              if (must_reverse) {
                     rte_sp_exchange_mac_addresses(ehdr);
                     if (is_phy_port_privte(phy_port)) {
                            if (!ret) {
                                   dest_if = get_pub_to_prv_port(
                                                 (uint32_t *)
                                                 &dest_address[0],
                                                 IP_VERSION_6);
                                   if (dest_if == INVALID_DESTIF) {
                                          *pkts_mask &= ~pkt_mask;
                                          vfw_pipe->counters->
                                          pkts_drop_without_arp_entry++;
                                   }
                                   do_local_nh_ipv6_cache(dest_if,
                                                 vfw_pipe);
                            }

                     } else {
                            if (!ret) {
                                   dest_if = get_prv_to_pub_port(
                                                 (uint32_t *)
                                                 &dest_address[0],
                                                 IP_VERSION_6);
                                   if (dest_if == INVALID_DESTIF) {
                                          *pkts_mask &= ~pkt_mask;
                                          vfw_pipe->counters->
                                          pkts_drop_without_arp_entry++;
                                   }
                                   do_local_nh_ipv6_cache(dest_if,
                                                 vfw_pipe);
                            }
                     }

              } else if (is_phy_port_privte(phy_port)) {
                     if (!ret) {
                            dest_if = get_prv_to_pub_port((uint32_t *)
                                          &dest_address[0], IP_VERSION_6);
                            if (dest_if == INVALID_DESTIF) {
                                   *pkts_mask &= ~pkt_mask;
                                   vfw_pipe->counters->
                                          pkts_drop_without_arp_entry++;
                            }
                            do_local_nh_ipv6_cache(dest_if, vfw_pipe);
                     }

              } else {
                     if (!ret) {
                            dest_if = get_pub_to_prv_port((uint32_t *)
                                          &dest_address[0], IP_VERSION_6);
                            if (dest_if == INVALID_DESTIF) {
                                   *pkts_mask &= ~pkt_mask;
                                   vfw_pipe->counters->
                                          pkts_drop_without_arp_entry++;

                            }
                            do_local_nh_ipv6_cache(dest_if, vfw_pipe);
                     }

              }

              meta_data_addr->output_port = vfw_pipe->outport_id[dest_if];

              memset(nh_ipv6, 0, IPV6_ADD_SIZE);
              if (get_dest_mac_address_ipv6_port(
                                   &dest_address[0],
                                   &dest_if,
                                   &hw_addr,
                                   &nh_ipv6[0])) {
                     ether_addr_copy(&hw_addr, &ehdr->d_addr);
                     ether_addr_copy(get_link_hw_addr(dest_if),
                                   &ehdr->s_addr);

                     if (vfw_debug >= DEBUG_LEVEL_4) {
                            char buf[HW_ADDR_SIZE];

                            ether_format_addr(buf, sizeof(buf),
                                          &hw_addr);
                            printf("MAC found for  dest_if %d: %s, ",
                                          dest_if, buf);
                            ether_format_addr(buf, sizeof(buf),
                                          &ehdr->s_addr);
                            printf("new eth hdr src: %s, ", buf);
                            ether_format_addr(buf, sizeof(buf),
                                          &ehdr->d_addr);
                            printf("new eth hdr dst: %s\n", buf);
                     }

              } else {
                     printf("deleting ipv6\n");
                     *pkts_mask &= ~pkt_mask;
                     /*Next Neighbor is not yet implemented
                      * for ipv6.*/
                     vfw_pipe->counters->
                            pkts_drop_without_arp_entry++;
              }

       }

}

#else

/**
 * walk every valid mbuf (denoted by pkts_mask) and apply arp to the packet.
 * To support synproxy, some (altered) packets may need to be sent back where
 * they came from. The ip header has already been adjusted, but the ethernet
 * header has not, so this must be performed here.
 * Return an updated pkts_mask, since arp may drop some packets
 *
 * @param pkts
 *  A pointer to the packet.
 * @param pkts_mask
 *  Packet mask
 * @param synproxy_reply_mask
 *  Reply Packet mask for Synproxy
 * @param vfw_pipe
 *  A pointer to VFW pipeline.
 */
static uint64_t
rte_vfw_arp_ipv4_packets(struct rte_mbuf **pkts,
              uint64_t pkts_mask,
              uint64_t synproxy_reply_mask,
              struct pipeline_vfw *vfw_pipe)
{
       uint64_t pkts_to_arp = pkts_mask;

       uint32_t ret;
       uint32_t dest_if = INVALID_DESTIF;
       int ret_mac;
       for (; pkts_to_arp;) {
              struct ether_addr hw_addr;
              struct mbuf_tcp_meta_data *meta_data_addr;
              struct ether_hdr *ehdr;
              struct rte_mbuf *pkt;
              uint16_t phy_port;

              uint8_t pos = (uint8_t) __builtin_ctzll(pkts_to_arp);
              /* bitmask representing only this packet */
              uint64_t pkt_mask = 1LLU << pos;
              /* remove this packet from remaining list */
              pkts_to_arp &= ~pkt_mask;
              pkt = pkts[pos];
              int must_reverse = ((synproxy_reply_mask & pkt_mask) != 0);

              phy_port = pkt->port;
              meta_data_addr = (struct mbuf_tcp_meta_data *)
                     RTE_MBUF_METADATA_UINT32_PTR(pkt, META_DATA_OFFSET);
              ehdr = rte_vfw_get_ether_addr(pkt);


              struct ipv4_hdr *ihdr = (struct ipv4_hdr *)
                     RTE_MBUF_METADATA_UINT32_PTR(pkt, IP_START);
              uint32_t nhip = 0;

              uint32_t dest_address = rte_bswap32(ihdr->dst_addr);

              ret = local_get_nh_ipv4(dest_address, &dest_if,
                            &nhip, vfw_pipe);
              if (must_reverse) {
                     rte_sp_exchange_mac_addresses(ehdr);
                     if (is_phy_port_privte(phy_port)) {
                            if (!ret) {
                                   dest_if = get_pub_to_prv_port(
                                                 &dest_address,
                                                 IP_VERSION_4);
                                   if (dest_if == INVALID_DESTIF) {
                                          pkts_mask &= ~pkt_mask;
                                          vfw_pipe->counters->
                                          pkts_drop_without_arp_entry++;
                                   }
                                   do_local_nh_ipv4_cache(
                                                 dest_if, vfw_pipe);
                            }

                     } else {
                            if (!ret) {
                                   dest_if = get_prv_to_pub_port(
                                                 &dest_address,
                                                 IP_VERSION_4);
                                   if (dest_if == INVALID_DESTIF) {
                                          pkts_mask &= ~pkt_mask;
                                          vfw_pipe->counters->
                                          pkts_drop_without_arp_entry++;
                                   }
                                   do_local_nh_ipv4_cache(dest_if,
                                                 vfw_pipe);
                            }
                     }
              } else if (is_phy_port_privte(phy_port)) {
                     if (!ret) {
                            dest_if = get_prv_to_pub_port(&dest_address,
                                          IP_VERSION_4);
                            if (dest_if == INVALID_DESTIF) {
                                   pkts_mask &= ~pkt_mask;
                                   vfw_pipe->counters->
                                          pkts_drop_without_arp_entry++;
                            }
                            do_local_nh_ipv4_cache(dest_if, vfw_pipe);
                     }

              } else {
                     if (!ret) {
                            dest_if = get_pub_to_prv_port(&dest_address,
                                          IP_VERSION_4);
                            if (dest_if == INVALID_DESTIF) {
                                   pkts_mask &= ~pkt_mask;
                                   vfw_pipe->counters->
                                          pkts_drop_without_arp_entry++;
                            }
                            do_local_nh_ipv4_cache(dest_if, vfw_pipe);
                     }

              }
              meta_data_addr->output_port =  vfw_pipe->outport_id[dest_if];
              if (local_dest_mac_present(dest_if)) {
                     ether_addr_copy(get_local_link_hw_addr(dest_if),
                                   &ehdr->d_addr);
                     ether_addr_copy(get_link_hw_addr(dest_if),
                                   &ehdr->s_addr);
              } else {
                     ret_mac = get_dest_mac_addr_port(dest_address,
                                   &dest_if, &hw_addr);
                     if (ret_mac) {
                            link_hw_laddr_valid[dest_if] = 1;
                            memcpy(&link_hw_laddr[dest_if], &hw_addr,
                                          sizeof(struct ether_addr));

                            ether_addr_copy(&hw_addr, &ehdr->d_addr);
                            ether_addr_copy(get_link_hw_addr(dest_if),
                                          &ehdr->s_addr);

                            if (vfw_debug >= DEBUG_LEVEL_4) {
                                   char buf[HW_ADDR_SIZE];

                                   ether_format_addr(buf, sizeof(buf),
                                                 &hw_addr);
                                   printf("MAC found for ip 0x%"
                                          PRIx32", dest_if %d: %s, ",
                                          dest_address,
                                          dest_if, buf);
                                   ether_format_addr(buf, sizeof(buf),
                                                 &ehdr->s_addr);
                                   printf("new eth hdr src: %s, ", buf);
                                   ether_format_addr(buf, sizeof(buf),
                                                 &ehdr->d_addr);
                                   printf("new eth hdr dst: %s\n", buf);
                            }

                     } else {
                            if (unlikely(ret_mac == 0))
                                   request_arp(meta_data_addr->output_port,
                                          nhip);

                            if (vfw_debug >= DEBUG_LEVEL_4) {
                                   char buf[HW_ADDR_SIZE];

                            ether_format_addr(buf, sizeof(buf),
                                          &hw_addr);
                            printf("MAC NOT FOUND for ip 0x%"
                                          PRIx32", dest_if %"
                                          PRId16": %s, ",
                                          dest_address,
                                          dest_if, buf);
                     }
                     /* ICMP req sent, drop packet by
                      * changing the mask */
                     pkts_mask &= ~pkt_mask;
                     vfw_pipe->
                            counters->pkts_drop_without_arp_entry++;
              }
}

       }

       return pkts_mask;
}
/**
 * walk every valid mbuf (denoted by pkts_mask) and apply arp to the packet.
 * To support synproxy, some (altered) packets may need to be sent back where
 * they came from. The ip header has already been adjusted, but the ethernet
 * header has not, so this must be performed here.
 * Return an updated pkts_mask, since arp may drop some packets
 *
 * @param pkts
 *  A pointer to the packet.
 * @param pkts_mask
 *  Packet mask
 * @param synproxy_reply_mask
 *  Reply Packet mask for Synproxy
 * @param vfw_pipe
 *  A pointer to VFW pipeline.
 */

       static uint64_t
rte_vfw_arp_ipv6_packets(struct rte_mbuf **pkts,
              uint64_t pkts_mask,
              uint64_t synproxy_reply_mask,
              struct pipeline_vfw *vfw_pipe)
{
       uint64_t pkts_to_arp = pkts_mask;
       uint8_t nh_ipv6[IPV6_ADD_SIZE];
       uint32_t ret;
       uint32_t dest_if = INVALID_DESTIF;

       for (; pkts_to_arp;) {
              struct ether_addr hw_addr;
              struct mbuf_tcp_meta_data *meta_data_addr;
              struct ether_hdr *ehdr;
              struct rte_mbuf *pkt;
              uint16_t phy_port;

              uint8_t pos = (uint8_t) __builtin_ctzll(pkts_to_arp);
              /* bitmask representing only this packet */
              uint64_t pkt_mask = 1LLU << pos;
              /* remove this packet from remaining list */
              pkts_to_arp &= ~pkt_mask;
              pkt = pkts[pos];
              int must_reverse = ((synproxy_reply_mask & pkt_mask) != 0);

              phy_port = pkt->port;
              meta_data_addr = (struct mbuf_tcp_meta_data *)
                     RTE_MBUF_METADATA_UINT32_PTR(pkt, META_DATA_OFFSET);
              ehdr = rte_vfw_get_ether_addr(pkt);

              struct ipv6_hdr *ihdr = (struct ipv6_hdr *)
                     RTE_MBUF_METADATA_UINT32_PTR(pkt, IP_START);

              uint8_t nhip[IPV6_ADD_SIZE];
              uint8_t dest_address[IPV6_ADD_SIZE];

              memset(nhip, 0, IPV6_ADD_SIZE);

              rte_mov16(dest_address, ihdr->dst_addr);
              ret = local_get_nh_ipv6(&dest_address[0], &dest_if,
                            &nhip[0], vfw_pipe);
              if (must_reverse) {
                     rte_sp_exchange_mac_addresses(ehdr);
                     if (is_phy_port_privte(phy_port)) {
                            if (!ret) {
                                   dest_if = get_pub_to_prv_port(
                                                 (uint32_t *)
                                                 &dest_address[0],
                                                 IP_VERSION_6);
                                   if (dest_if == INVALID_DESTIF) {
                                          pkts_mask &= ~pkt_mask;
                                          vfw_pipe->counters->
                                          pkts_drop_without_arp_entry++;
                                   }
                                   do_local_nh_ipv6_cache(dest_if,
                                                 vfw_pipe);
                            }

                     } else {
                            if (!ret) {
                                   dest_if = get_prv_to_pub_port(
                                                 (uint32_t *)
                                                 &dest_address[0],
                                                 IP_VERSION_6);
                                   if (dest_if == INVALID_DESTIF) {
                                          pkts_mask &= ~pkt_mask;
                                          vfw_pipe->counters->
                                          pkts_drop_without_arp_entry++;
                                   }
                                   do_local_nh_ipv6_cache(dest_if,
                                                 vfw_pipe);
                            }
                     }

              } else if (is_phy_port_privte(phy_port)) {
                     if (!ret) {
                            dest_if = get_prv_to_pub_port((uint32_t *)
                                          &dest_address[0], IP_VERSION_6);
                            if (dest_if == INVALID_DESTIF) {
                                   pkts_mask &= ~pkt_mask;
                                   vfw_pipe->counters->
                                          pkts_drop_without_arp_entry++;
                            }
                            do_local_nh_ipv6_cache(dest_if, vfw_pipe);
                     }

              } else {
                     if (!ret) {
                            dest_if = get_pub_to_prv_port((uint32_t *)
                                          &dest_address[0], IP_VERSION_6);
                            if (dest_if == INVALID_DESTIF) {
                                   pkts_mask &= ~pkt_mask;
                                   vfw_pipe->counters->
                                          pkts_drop_without_arp_entry++;

                            }
                            do_local_nh_ipv6_cache(dest_if, vfw_pipe);
                     }

              }

              meta_data_addr->output_port = vfw_pipe->outport_id[dest_if];

              memset(nh_ipv6, 0, IPV6_ADD_SIZE);
              if (get_dest_mac_address_ipv6_port(
                                   &dest_address[0],
                                   &dest_if,
                                   &hw_addr,
                                   &nh_ipv6[0])) {
                     ether_addr_copy(&hw_addr, &ehdr->d_addr);
                     ether_addr_copy(get_link_hw_addr(dest_if),
                                   &ehdr->s_addr);

                     if (vfw_debug >= DEBUG_LEVEL_4) {
                            char buf[HW_ADDR_SIZE];

                            ether_format_addr(buf, sizeof(buf),
                                          &hw_addr);
                            printf("MAC found for  dest_if %d: %s, ",
                                          dest_if, buf);
                            ether_format_addr(buf, sizeof(buf),
                                          &ehdr->s_addr);
                            printf("new eth hdr src: %s, ", buf);
                            ether_format_addr(buf, sizeof(buf),
                                          &ehdr->d_addr);
                            printf("new eth hdr dst: %s\n", buf);
                     }

              } else {
                     printf("deleting ipv6\n");
                     pkts_mask &= ~pkt_mask;
                     /*Next Neighbor is not yet implemented
                      * for ipv6.*/
                     vfw_pipe->counters->
                            pkts_drop_without_arp_entry++;
              }


       }

       return pkts_mask;
}

#endif
/**
 * Packets processing for connection tracking.
 *
 * @param vfw_pipe
 *  A pointer to the pipeline.
 * @param ct
 *  A pointer to the connetion tracker .
 * @param pkts
 *  A pointer to a burst of packets.
 * @param packet_mask_in
 *  Input packets Mask.
 */

       static  uint64_t
vfw_process_buffered_pkts(__rte_unused struct pipeline_vfw *vfw_pipe,
              struct rte_ct_cnxn_tracker *ct,
                          struct rte_mbuf **pkts, uint64_t packet_mask_in)
{
       uint64_t keep_mask = packet_mask_in;
       struct rte_synproxy_helper sp_helper;       /* for synproxy */

       keep_mask =
           rte_ct_cnxn_tracker_batch_lookup_with_synproxy(ct, pkts, keep_mask,
                                                    &sp_helper);

       if (unlikely(sp_helper.hijack_mask))
              printf("buffered hijack pkts severe error\n");

       if (unlikely(sp_helper.reply_pkt_mask))
              printf("buffered reply pkts severe error\n");

       return keep_mask;
}

/**
 * Free Packets from mbuf.
 *
 * @param ct
 *  A pointer to the connection tracker to increment drop counter.
 *
 * @param pkt
 *  Packet to be free.
 */
static inline void
vfw_pktmbuf_free(struct rte_ct_cnxn_tracker *ct, struct rte_mbuf *pkt)
{
       ct->counters->pkts_drop++;
       rte_pktmbuf_free(pkt);
}

static void
vfw_output_or_delete_buffered_packets(struct rte_ct_cnxn_tracker *ct,
                                    struct rte_pipeline *p,
                                    struct rte_mbuf **pkts,
                                    int num_pkts, uint64_t pkts_mask)
{
       int i;
       struct mbuf_tcp_meta_data *meta_data_addr;
       uint64_t pkt_mask = 1;

       /* any clear bits in low-order num_pkts bit of
        * pkt_mask must be discarded */

       for (i = 0; i < num_pkts; i++) {
              struct rte_mbuf *pkt = pkts[i];

              if (pkts_mask & pkt_mask) {
                     printf("vfw_output_or_delete_buffered_packets\n");
                     meta_data_addr = (struct mbuf_tcp_meta_data *)
                         RTE_MBUF_METADATA_UINT32_PTR(pkt, META_DATA_OFFSET);
                     rte_pipeline_port_out_packet_insert(
                                   p, meta_data_addr->output_port, pkt);

              } else {
                     vfw_pktmbuf_free(ct, pkt);
              }

              pkt_mask = pkt_mask << 1;
       }
}

/**
 *Packet buffered for synproxy.
 *
 * @param p
 *  A pointer to the pipeline.
 * @param vfw_pipe
 *  A pointer to the vfw pipeline.
 * @param ct
 *  A pointer to the connection tracker.
 * @param forward_pkts
 *  Packet forwarded by synproxy.
 *
 */
static void
vfw_handle_buffered_packets(struct rte_pipeline *p,
                            struct pipeline_vfw *vfw_pipe,
                            struct rte_ct_cnxn_tracker *ct, int forward_pkts)
{
       struct rte_mbuf *pkt_list = rte_ct_get_buffered_synproxy_packets(ct);

       if (likely(pkt_list == NULL))       /* only during proxy setup is != NULL */
              return;

       int pkt_count = 0;
       uint64_t keep_mask = 0;
       struct rte_mbuf **pkts = vfw_pipe->pkt_buffer;
       struct rte_mbuf *pkt;

       while (pkt_list != NULL) {
              struct mbuf_tcp_meta_data *meta_data =
              (struct mbuf_tcp_meta_data *)
              RTE_MBUF_METADATA_UINT32_PTR(pkt_list, META_DATA_OFFSET);

              /* detach head of list and advance list */
              pkt = pkt_list;
              pkt_list = meta_data->next;

              if (forward_pkts) {

                     pkts[pkt_count++] = pkt;

                     if (pkt_count == PKT_BUFFER_SIZE) {
                            /* need to send out packets */
                            /* currently 0, set all bits */
                            keep_mask = ~keep_mask;

                            keep_mask =
                                vfw_process_buffered_pkts(vfw_pipe,
                                                         ct, pkts,
                                                         keep_mask);
                            vfw_output_or_delete_buffered_packets(
                                          ct, p,
                                          pkts,
                                          PKT_BUFFER_SIZE,
                                          keep_mask);
                            pkt_count = 0;
                            keep_mask = 0;
                     }

              } else {
                     vfw_pktmbuf_free(ct, pkt);
              }
       }

       if (pkt_count != 0) {
              /* need to send out packets */
              keep_mask = RTE_LEN2MASK(pkt_count, uint64_t);

              keep_mask =
                     vfw_process_buffered_pkts(vfw_pipe, ct, pkts,
                                   keep_mask);

              vfw_output_or_delete_buffered_packets(ct, p, pkts, pkt_count,
                            keep_mask);

              pkt_count = 0;
              keep_mask = 0;
       }
}

/**
 * The pipeline port-in action is used to do all the firewall and
 * connection tracking work.
 *
 * @param p
 *  A pointer to the pipeline.
  * @param pkts
 *  A pointer to a burst of packets.
 * @param n_pkts
 *  Number of packets to process.
 * @param arg
 *  A pointer to pipeline specific data.
 *
 * @return
 *  0 on success, negative on error.
 */

static int
vfw_port_in_action(struct rte_pipeline *p,
              struct rte_mbuf **pkts,
              __rte_unused uint32_t n_pkts, __rte_unused void *arg)
{
       struct vfw_ports_in_args *port_in_args =
              (struct vfw_ports_in_args *)arg;
       struct pipeline_vfw *vfw_pipe =
              (struct pipeline_vfw *)port_in_args->pipe;
       struct rte_ct_cnxn_tracker *ct = port_in_args->cnxn_tracker;

       start_tsc_measure(vfw_pipe);

       uint64_t packet_mask_in = RTE_LEN2MASK(n_pkts, uint64_t);
       uint64_t pkts_drop_mask;
       uint64_t hijack_mask = 0;
       uint64_t synproxy_reply_mask = 0;       /* for synproxy */
       uint64_t keep_mask = packet_mask_in;
       struct rte_CT_helper ct_helper;

       memset(&ct_helper, 0, sizeof(struct rte_CT_helper));


       /*
        * This routine uses a bit mask to represent which packets in the
        * "pkts" table are considered valid. Any table entry which exists
        * and is considered valid has the corresponding bit in the mask set.
        * Otherwise, it is cleared. Note that the mask is 64 bits,
        * but the number of packets in the table may be considerably less.
        * Any mask bits which do correspond to actual packets are cleared.
        * Various routines are called which may determine that an existing
        * packet is somehow invalid. The routine will return an altered bit
        * mask, with the bit cleared. At the end of all the checks,
        * packets are dropped if their mask bit is a zero
        */

       if (vfw_debug > 1)
              printf("Enter in-port action with %p packet mask\n",
                            (void *)packet_mask_in);
       vfw_pipe->counters->pkts_received =
              vfw_pipe->counters->pkts_received + n_pkts;
       if (VFW_DEBUG)
              printf("vfw_port_in_action pkts_received: %" PRIu64
                            " n_pkts: %u\n",
                            vfw_pipe->counters->pkts_received, n_pkts);

       /* first handle handle any previously buffered packets now released */
       vfw_handle_buffered_packets(p, vfw_pipe, ct,
                     FORWARD_BUFFERED_PACKETS);

       /* now handle any new packets on input ports */
       if (likely(firewall_flag)) {
              keep_mask =
                     rte_vfw_packet_filter_and_process(pkts, keep_mask,
                                   vfw_pipe);
              vfw_pipe->counters->pkts_fw_forwarded +=
                     __builtin_popcountll(keep_mask);
       }
#ifdef ACL_ENABLE
       uint64_t conntrack_mask = 0, connexist_mask = 0;
       keep_mask = lib_acl_pkt_work_key(
                     vfw_pipe->plib_acl, pkts, keep_mask,
                     &vfw_pipe->counters->pkts_drop_without_rule,
                     vfw_rule_table_ipv4_active,
                     vfw_rule_table_ipv6_active,
                     action_array_active,
                     action_counter_table,
                     &conntrack_mask, &connexist_mask,
                     vfw_ipv4_enabled,
                     vfw_ipv6_enabled);
       vfw_pipe->counters->pkts_acl_forwarded +=
              __builtin_popcountll(keep_mask);
       if (conntrack_mask > 0) {
              keep_mask = conntrack_mask;
              ct_helper.no_new_cnxn_mask = connexist_mask;
              cnxn_tracking_is_active = 1;
       } else
              cnxn_tracking_is_active = 0;
#endif
       if (likely(cnxn_tracking_is_active)) {
              keep_mask = rte_ct_cnxn_tracker_batch_lookup(ct, pkts,
                            keep_mask, &ct_helper);
              synproxy_reply_mask = ct_helper.reply_pkt_mask;
              hijack_mask = ct_helper.hijack_mask;

       }


       keep_mask =
              rte_vfw_arp_packets(pkts, keep_mask, synproxy_reply_mask,
                            vfw_pipe);

       if (vfw_debug > 1) {
              printf("  Exit in-port action with %p packet mask\n",
                            (void *)keep_mask);
              if (keep_mask != packet_mask_in)
                     printf("dropped packets, %p in, %p out\n",
                                   (void *)packet_mask_in,
                                   (void *)keep_mask);
       }

       /* Update mask before returning, so that bad packets are dropped */

       pkts_drop_mask = packet_mask_in & ~keep_mask;

       if (unlikely(pkts_drop_mask != 0)) {
              /* printf("drop %p\n", (void *) pkts_drop_mask); */
              rte_pipeline_ah_packet_drop(p, pkts_drop_mask);
       }

       if (unlikely(hijack_mask != 0))
              rte_pipeline_ah_packet_hijack(p, hijack_mask);

       vfw_pipe->counters->num_batch_pkts_sum += n_pkts;
       vfw_pipe->counters->num_pkts_measurements++;

       end_tsc_measure(vfw_pipe, n_pkts);

       return 0;
}
/**
 * The pipeline port-in action is used to do all the firewall and
 * connection tracking work for IPV4 packets.
 *
 * @param p
 *  A pointer to the pipeline.
  * @param pkts
 *  A pointer to a burst of packets.
 * @param n_pkts
 *  Number of packets to process.
 * @param arg
 *  A pointer to pipeline specific data.
 *
 * @return
 *  0 on success, negative on error.
 */

static int
vfw_port_in_action_ipv4(struct rte_pipeline *p,
              struct rte_mbuf **pkts,
              __rte_unused uint32_t n_pkts, __rte_unused void *arg)
{
       struct vfw_ports_in_args *port_in_args =
              (struct vfw_ports_in_args *)arg;
       struct pipeline_vfw *vfw_pipe =
              (struct pipeline_vfw *)port_in_args->pipe;
       struct rte_ct_cnxn_tracker *ct = port_in_args->cnxn_tracker;

       start_tsc_measure(vfw_pipe);

       uint64_t packet_mask_in = RTE_LEN2MASK(n_pkts, uint64_t);
       uint64_t pkts_drop_mask;
       uint64_t hijack_mask = 0;
       uint64_t synproxy_reply_mask = 0;       /* for synproxy */
       uint64_t keep_mask = packet_mask_in;

       uint64_t conntrack_mask = 0, connexist_mask = 0;
       struct rte_CT_helper ct_helper;
       uint8_t j;

       /*
        * This routine uses a bit mask to represent which packets in the
        * "pkts" table are considered valid. Any table entry which exists
        * and is considered valid has the corresponding bit in the mask set.
        * Otherwise, it is cleared. Note that the mask is 64 bits,
        * but the number of packets in the table may be considerably less.
        * Any mask bits which do correspond to actual packets are cleared.
        * Various routines are called which may determine that an existing
        * packet is somehow invalid. The routine will return an altered bit
        * mask, with the bit cleared. At the end of all the checks,
        * packets are dropped if their mask bit is a zero
        */

       rte_prefetch0(& vfw_pipe->counters);

#ifdef EN_SWP_ACL
       /* Pre-fetch all rte_mbuf header */
       for(j = 0; j < n_pkts; j++)
              rte_prefetch0(pkts[j]);
#endif
       memset(&ct_helper, 0, sizeof(struct rte_CT_helper));
#ifdef EN_SWP_ACL
       rte_prefetch0(& vfw_pipe->counters->pkts_drop_ttl);
       rte_prefetch0(& vfw_pipe->counters->sum_latencies);
#endif

       if (unlikely(vfw_debug > 1))
              printf("Enter in-port action IPV4 with %p packet mask\n",
                            (void *)packet_mask_in);
       vfw_pipe->counters->pkts_received =
              vfw_pipe->counters->pkts_received + n_pkts;

       if (unlikely(VFW_DEBUG))
              printf("vfw_port_in_action_ipv4 pkts_received: %" PRIu64
                            " n_pkts: %u\n",
                            vfw_pipe->counters->pkts_received, n_pkts);

       /* first handle handle any previously buffered packets now released */
       vfw_handle_buffered_packets(p, vfw_pipe, ct,
                     FORWARD_BUFFERED_PACKETS);

       /* now handle any new packets on input ports */
       if (likely(firewall_flag)) {
              keep_mask = rte_vfw_ipv4_packet_filter_and_process(pkts,
                            keep_mask, vfw_pipe);
              vfw_pipe->counters->pkts_fw_forwarded +=
                     __builtin_popcountll(keep_mask);
       }
#ifdef ACL_ENABLE
#ifdef EN_SWP_ACL
       rte_prefetch0((void*)vfw_pipe->plib_acl);
       rte_prefetch0((void*)vfw_rule_table_ipv4_active);
#endif /* EN_SWP_ACL */
       keep_mask = lib_acl_ipv4_pkt_work_key(
                     vfw_pipe->plib_acl, pkts, keep_mask,
                     &vfw_pipe->counters->pkts_drop_without_rule,
                     vfw_rule_table_ipv4_active,
                     action_array_active,
                     action_counter_table,
                     &conntrack_mask, &connexist_mask);
       vfw_pipe->counters->pkts_acl_forwarded +=
              __builtin_popcountll(keep_mask);
       if (conntrack_mask > 0) {
              keep_mask = conntrack_mask;
              ct_helper.no_new_cnxn_mask = connexist_mask;
              cnxn_tracking_is_active = 1;
       } else
              cnxn_tracking_is_active = 0;
#endif /* ACL_ENABLE */

       if (likely(cnxn_tracking_is_active)) {
              rte_ct_cnxn_tracker_batch_lookup_type(ct, pkts,
                            &keep_mask, &ct_helper, IPv4_HEADER_SIZE);
              synproxy_reply_mask = ct_helper.reply_pkt_mask;
              hijack_mask = ct_helper.hijack_mask;

       }

#ifdef EN_SWP_ARP
       for(j = 0; j < (n_pkts & 0x3LLU); j++) {
               rte_prefetch0(RTE_MBUF_METADATA_UINT32_PTR(pkts[j],
                                   META_DATA_OFFSET));
               rte_prefetch0(RTE_MBUF_METADATA_UINT32_PTR(pkts[j],
                                   ETHERNET_START));
       }
       rte_prefetch0((void*)in_port_dir_a);
       rte_prefetch0((void*)prv_to_pub_map);

       uint8_t i;
       for (i = 0; i < (n_pkts & (~0x3LLU)); i += 4) {
              for (j = i+4; ((j < n_pkts) && (j < i+8)); j++) {
                     rte_prefetch0(RTE_MBUF_METADATA_UINT32_PTR(pkts[j],
                                          META_DATA_OFFSET));
                     rte_prefetch0(RTE_MBUF_METADATA_UINT32_PTR(pkts[j],
                                          ETHERNET_START));
              }
              pkt4_work_vfw_arp_ipv4_packets(&pkts[i], i, &keep_mask,
                            synproxy_reply_mask, vfw_pipe);
       }
       for (j = i; j < n_pkts; j++) {
              rte_prefetch0(RTE_MBUF_METADATA_UINT32_PTR(pkts[j],
                                   META_DATA_OFFSET));
              rte_prefetch0(RTE_MBUF_METADATA_UINT32_PTR(pkts[j],
                                   ETHERNET_START));
       }
       for (; i < n_pkts; i++) {
              pkt_work_vfw_arp_ipv4_packets(pkts[i], i, &keep_mask,
                            synproxy_reply_mask, vfw_pipe);
       }
#else
       rte_prefetch0((void*)in_port_dir_a);
       rte_prefetch0((void*)prv_to_pub_map);
       rte_prefetch0((void*) & vfw_pipe->local_lib_arp_route_table);
       keep_mask = rte_vfw_arp_ipv4_packets(pkts, keep_mask,
                     synproxy_reply_mask, vfw_pipe);
#endif

       if (vfw_debug > 1) {
              printf("  Exit in-port action with %p packet mask\n",
                            (void *)keep_mask);
              if (keep_mask != packet_mask_in)
                     printf("dropped packets, %p in, %p out\n",
                                   (void *)packet_mask_in,
                                   (void *)keep_mask);
       }

       /* Update mask before returning, so that bad packets are dropped */

       pkts_drop_mask = packet_mask_in & ~keep_mask;

       if (unlikely(pkts_drop_mask != 0)) {
              /* printf("drop %p\n", (void *) pkts_drop_mask); */
              rte_pipeline_ah_packet_drop(p, pkts_drop_mask);
       }

       if (unlikely(hijack_mask != 0))
              rte_pipeline_ah_packet_hijack(p, hijack_mask);

       vfw_pipe->counters->num_batch_pkts_sum += n_pkts;
       vfw_pipe->counters->num_pkts_measurements++;

       end_tsc_measure(vfw_pipe, n_pkts);

       return 0;
}
/**
 * The pipeline port-in action is used to do all the firewall and
 * connection tracking work for IPV6 packet.
 *
 * @param p
 *  A pointer to the pipeline.
  * @param pkts
 *  A pointer to a burst of packets.
 * @param n_pkts
 *  Number of packets to process.
 * @param arg
 *  A pointer to pipeline specific data.
 *
 * @return
 *  0 on success, negative on error.
 */

static int
vfw_port_in_action_ipv6(struct rte_pipeline *p,
              struct rte_mbuf **pkts,
              __rte_unused uint32_t n_pkts, __rte_unused void *arg)
{
       struct vfw_ports_in_args *port_in_args =
              (struct vfw_ports_in_args *)arg;
       struct pipeline_vfw *vfw_pipe =
              (struct pipeline_vfw *)port_in_args->pipe;
       struct rte_ct_cnxn_tracker *ct = port_in_args->cnxn_tracker;

       start_tsc_measure(vfw_pipe);

       uint64_t packet_mask_in = RTE_LEN2MASK(n_pkts, uint64_t);
       uint64_t pkts_drop_mask;
       uint64_t hijack_mask = 0;
       uint64_t synproxy_reply_mask = 0;       /* for synproxy */
       uint64_t keep_mask = packet_mask_in;

       uint64_t conntrack_mask = 0, connexist_mask = 0;
       struct rte_CT_helper ct_helper;
       uint32_t j;

       /*
        * This routine uses a bit mask to represent which packets in the
        * "pkts" table are considered valid. Any table entry which exists
        * and is considered valid has the corresponding bit in the mask set.
        * Otherwise, it is cleared. Note that the mask is 64 bits,
        * but the number of packets in the table may be considerably less.
        * Any mask bits which do correspond to actual packets are cleared.
        * Various routines are called which may determine that an existing
        * packet is somehow invalid. The routine will return an altered bit
        * mask, with the bit cleared. At the end of all the checks,
        * packets are dropped if their mask bit is a zero
        */

       rte_prefetch0(& vfw_pipe->counters);

       /* Pre-fetch all rte_mbuf header */
       for(j = 0; j < n_pkts; j++)
               rte_prefetch0(pkts[j]);

       memset(&ct_helper, 0, sizeof(struct rte_CT_helper));
       rte_prefetch0(& vfw_pipe->counters->pkts_drop_ttl);
       rte_prefetch0(& vfw_pipe->counters->sum_latencies);

       if (vfw_debug > 1)
              printf("Enter in-port action with %p packet mask\n",
                            (void *)packet_mask_in);
       vfw_pipe->counters->pkts_received =
              vfw_pipe->counters->pkts_received + n_pkts;
       if (VFW_DEBUG)
              printf("vfw_port_in_action pkts_received: %" PRIu64
                            " n_pkts: %u\n",
                            vfw_pipe->counters->pkts_received, n_pkts);

       /* first handle handle any previously buffered packets now released */
       vfw_handle_buffered_packets(p, vfw_pipe, ct,
                     FORWARD_BUFFERED_PACKETS);

       /* now handle any new packets on input ports */
       if (likely(firewall_flag)) {
              keep_mask = rte_vfw_ipv6_packet_filter_and_process(pkts,
                            keep_mask, vfw_pipe);
              vfw_pipe->counters->pkts_fw_forwarded +=
                     __builtin_popcountll(keep_mask);
       }
#ifdef ACL_ENABLE

#ifdef EN_SWP_ACL
       rte_prefetch0((void*)vfw_pipe->plib_acl);
       rte_prefetch0((void*)vfw_rule_table_ipv6_active);
#endif /* EN_SWP_ACL */
       keep_mask = lib_acl_ipv6_pkt_work_key(
                     vfw_pipe->plib_acl, pkts, keep_mask,
                     &vfw_pipe->counters->pkts_drop_without_rule,
                     vfw_rule_table_ipv6_active,
                     action_array_active,
                     action_counter_table,
                     &conntrack_mask, &connexist_mask);
       vfw_pipe->counters->pkts_acl_forwarded +=
              __builtin_popcountll(keep_mask);
       if (conntrack_mask > 0) {
              keep_mask = conntrack_mask;
              ct_helper.no_new_cnxn_mask = connexist_mask;
              cnxn_tracking_is_active = 1;
       } else
              cnxn_tracking_is_active = 0;
#endif /* ACL_ENABLE */
       if (likely(cnxn_tracking_is_active)) {
              rte_ct_cnxn_tracker_batch_lookup_type(ct, pkts,
                            &keep_mask, &ct_helper, IPv6_HEADER_SIZE);
              synproxy_reply_mask = ct_helper.reply_pkt_mask;
              hijack_mask = ct_helper.hijack_mask;

       }

#ifdef EN_SWP_ARP
       for(j = 0; j < (n_pkts & 0x3LLU); j++) {
               rte_prefetch0(RTE_MBUF_METADATA_UINT32_PTR(pkts[j],
                                   META_DATA_OFFSET));
               rte_prefetch0(RTE_MBUF_METADATA_UINT32_PTR(pkts[j],
                                   ETHERNET_START));
       }
       rte_prefetch0((void*)in_port_dir_a);
       rte_prefetch0(vfw_pipe->local_lib_nd_route_table);
       uint32_t i;

       for (i = 0; i < (n_pkts & (~0x3LLU)); i += 4) {
              for (j = i+4; ((j < n_pkts) && (j < i+8)); j++) {
                     rte_prefetch0(RTE_MBUF_METADATA_UINT32_PTR(pkts[j],
                                          META_DATA_OFFSET));
                     rte_prefetch0(RTE_MBUF_METADATA_UINT32_PTR(pkts[j],
                                          ETHERNET_START));
              }
              pkt4_work_vfw_arp_ipv6_packets(&pkts[i], i, &keep_mask,
                            synproxy_reply_mask, vfw_pipe);
       }
       for (j = i; j < n_pkts; j++) {
              rte_prefetch0(RTE_MBUF_METADATA_UINT32_PTR(pkts[j],
                                   META_DATA_OFFSET));
              rte_prefetch0(RTE_MBUF_METADATA_UINT32_PTR(pkts[j],
                                   ETHERNET_START));
       }
       for (; i < n_pkts; i++) {
              pkt_work_vfw_arp_ipv6_packets(pkts[i], i, &keep_mask,
                            synproxy_reply_mask, vfw_pipe);
       }
#else
       rte_prefetch0((void*)in_port_dir_a);
       rte_prefetch0((void*) & vfw_pipe->local_lib_arp_route_table);
       keep_mask = rte_vfw_arp_ipv6_packets(pkts, keep_mask,
                     synproxy_reply_mask, vfw_pipe);
#endif

       if (vfw_debug > 1) {
              printf("  Exit in-port action with %p packet mask\n",
                            (void *)keep_mask);
              if (keep_mask != packet_mask_in)
                     printf("dropped packets, %p in, %p out\n",
                                   (void *)packet_mask_in,
                                   (void *)keep_mask);
       }

       /* Update mask before returning, so that bad packets are dropped */

       pkts_drop_mask = packet_mask_in & ~keep_mask;

       if (unlikely(pkts_drop_mask != 0)) {
              /* printf("drop %p\n", (void *) pkts_drop_mask); */
              rte_pipeline_ah_packet_drop(p, pkts_drop_mask);
       }

       if (unlikely(hijack_mask != 0))
              rte_pipeline_ah_packet_hijack(p, hijack_mask);

       vfw_pipe->counters->num_batch_pkts_sum += n_pkts;
       vfw_pipe->counters->num_pkts_measurements++;

       end_tsc_measure(vfw_pipe, n_pkts);

       return 0;
}


/**
 * Parse arguments in config file.
 *
 * @param vfw_pipe
 *  A pointer to the pipeline.
 * @param params
 *  A pointer to pipeline specific parameters.
 *
 * @return
 *  0 on success, negative on error.
 */
static int
pipeline_vfw_parse_args(struct pipeline_vfw *vfw_pipe,
              struct pipeline_params *params)
{
       uint32_t i;
       int status;

       if (vfw_debug)
              printf("VFW pipeline_vfw_parse_args params->n_args: %d\n",
                            params->n_args);

       for (i = 0; i < params->n_args; i++) {
              char *arg_name = params->args_name[i];
              char *arg_value = params->args_value[i];

              printf("VFW args[%d]: %s %d, %s\n", i, arg_name,
                            atoi(arg_value), arg_value);
#ifdef ACL_ENABLE
              status = lib_acl_parse_config(vfw_pipe->plib_acl,
                                   arg_name, arg_value, &vfw_n_rules);
              if (status < 0) {
                     printf("rte_ct_set_configuration_options =%s,%s",
                                   arg_name, arg_value);
                     return -1;
              } else if (status == 0)
                     continue;

#endif              /* traffic_type */
              if (strcmp(arg_name, "traffic_type") == 0) {
                     int traffic_type = atoi(arg_value);

                     if (traffic_type == 0 ||
                                   !(traffic_type == IP_VERSION_4 ||
                                          traffic_type == IP_VERSION_6)) {
                            printf("not IPV4/IPV6");
                            return -1;
                     }

                     vfw_pipe->traffic_type = traffic_type;
                     continue;
              }


              /* n_flows */
              if (strcmp(arg_name, "n_flows") == 0) {
                     int n_flows = atoi(arg_value);

                     if (n_flows == 0)
                            return -1;

                     /* must be power of 2, round up if not */
                     if (!rte_is_power_of_2(n_flows))
                            n_flows = rte_align32pow2(n_flows);

                     vfw_pipe->n_flows = n_flows;
                     continue;
              }

              /* not firewall option, process as cnxn tracking option */
              status = rte_ct_set_configuration_options(
                            vfw_pipe->cnxn_tracker,
                            arg_name, arg_value);
              if (status < 0) {
                     printf("rte_ct_set_configuration_options =%s,%s",
                                   arg_name, arg_value);
                     return -1;
              } else if (status == 0)
                     continue;

       }

       return 0;
}

static void *pipeline_vfw_msg_req_custom_handler(struct pipeline *p,
                                              void *msg);

static pipeline_msg_req_handler handlers[] = {
       [PIPELINE_MSG_REQ_PING] = pipeline_msg_req_ping_handler,
       [PIPELINE_MSG_REQ_STATS_PORT_IN] =
           pipeline_msg_req_stats_port_in_handler,
       [PIPELINE_MSG_REQ_STATS_PORT_OUT] =
           pipeline_msg_req_stats_port_out_handler,
       [PIPELINE_MSG_REQ_STATS_TABLE] = pipeline_msg_req_stats_table_handler,
       [PIPELINE_MSG_REQ_PORT_IN_ENABLE] =
           pipeline_msg_req_port_in_enable_handler,
       [PIPELINE_MSG_REQ_PORT_IN_DISABLE] =
           pipeline_msg_req_port_in_disable_handler,
       [PIPELINE_MSG_REQ_CUSTOM] = pipeline_vfw_msg_req_custom_handler,
};

static void *pipeline_vfw_msg_req_synproxy_flag_handler(struct pipeline *p,
                                                    void *msg);
static pipeline_msg_req_handler custom_handlers[] = {

       [PIPELINE_VFW_MSG_REQ_SYNPROXY_FLAGS] =
           pipeline_vfw_msg_req_synproxy_flag_handler
};

/**
 * Create and initialize Pipeline Back End (BE).
 *
 * @param params
 *  A pointer to the pipeline specific parameters..
 * @param arg
 *  A pointer to pipeline specific data.
 *
 * @return
 *  A pointer to the pipeline create, NULL on error.
 */
static void
*pipeline_vfw_init(struct pipeline_params *params, __rte_unused void *arg)
{
       uint32_t size, i;

       /* Check input arguments */
       if ((params == NULL) ||
                     (params->n_ports_in == 0) || (params->n_ports_out == 0))
              return NULL;

       if (vfw_debug)
              printf("num ports in %d / num ports out %d\n",
                            params->n_ports_in, params->n_ports_out);

       /* Create a single pipeline instance and initialize. */
       struct pipeline_vfw *pipe_vfw;

       size = RTE_CACHE_LINE_ROUNDUP(sizeof(struct pipeline_vfw));
       pipe_vfw = rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE);

       if (pipe_vfw == NULL)
              return NULL;

       struct pipeline *pipe;

       pipe = &pipe_vfw->pipe;

       strncpy(pipe->name, params->name, sizeof(pipe->name));
       pipe->log_level = params->log_level;
       pipe_vfw->n_flows = 4096;       /* small default value */
       pipe_vfw->traffic_type = MIX;
       pipe_vfw->pipeline_num = 0xff;
       for (i = 0; i < PIPELINE_MAX_PORT_IN; i++) {
              pipe_vfw->links_map[i] = 0xff;
              pipe_vfw->outport_id[i] = 0xff;
       }
       PLOG(pipe, HIGH, "VFW");

       /* Create a firewall instance and initialize. */
       pipe_vfw->cnxn_tracker =
              rte_zmalloc(NULL, rte_ct_get_cnxn_tracker_size(),
                            RTE_CACHE_LINE_SIZE);

       if (pipe_vfw->cnxn_tracker == NULL)
              return NULL;
#ifdef ACL_ENABLE
       /* Create a acl instance and initialize. */
       pipe_vfw->plib_acl =
              rte_zmalloc(NULL, sizeof(struct lib_acl),
                            RTE_CACHE_LINE_SIZE);

       if (pipe_vfw->plib_acl == NULL)
              return NULL;
#endif
       timer_lcore = rte_lcore_id();
       /*
        * Now allocate a counter block entry. It appears that the
        * initialization of all instances is serialized on core 0,
        * so no lock is necessary.
        */
       struct rte_VFW_counter_block *counter_ptr;

       if (rte_VFW_hi_counter_block_in_use == MAX_VFW_INSTANCES)
              /* error, exceeded table bounds */
              return NULL;

       rte_VFW_hi_counter_block_in_use++;
       counter_ptr =
              &rte_vfw_counter_table[rte_VFW_hi_counter_block_in_use];
       strncpy(counter_ptr->name, params->name, sizeof(counter_ptr->name));

       pipe_vfw->counters = counter_ptr;

       rte_ct_initialize_default_timeouts(pipe_vfw->cnxn_tracker);
       /* Parse arguments */
       if (pipeline_vfw_parse_args(pipe_vfw, params))
              return NULL;

       uint16_t pointers_offset =
              META_DATA_OFFSET + offsetof(struct mbuf_tcp_meta_data, next);

       if (pipe_vfw->n_flows > 0)
              rte_ct_initialize_cnxn_tracker_with_synproxy(
                            pipe_vfw->cnxn_tracker,
                            pipe_vfw->n_flows,
                            params->name,
                            pointers_offset);

       pipe_vfw->counters->ct_counters =
              rte_ct_get_counter_address(pipe_vfw->cnxn_tracker);

       /* Pipeline */
       {
              struct rte_pipeline_params pipeline_params = {
                     .name = params->name,
                     .socket_id = params->socket_id,
                     .offset_port_id = META_DATA_OFFSET +
                            offsetof(struct mbuf_tcp_meta_data, output_port)
              };

              pipe->p = rte_pipeline_create(&pipeline_params);
              if (pipe->p == NULL) {
                     rte_free(pipe_vfw);
                     return NULL;
              }
       }

       /* Input ports */

       /*
        * create a different "arg_ah" for each input port.
        * They differ only in the recorded port number. Unfortunately,
        * IP_PIPELINE does not pass port number in to input port handler
        */

       uint32_t in_ports_arg_size =
              RTE_CACHE_LINE_ROUNDUP((sizeof(struct vfw_ports_in_args)) *
                            (params->n_ports_in));
       struct vfw_ports_in_args *port_in_args =
              (struct vfw_ports_in_args *)
              rte_zmalloc(NULL, in_ports_arg_size, RTE_CACHE_LINE_SIZE);

       if (port_in_args == NULL)
              return NULL;

       pipe->n_ports_in = params->n_ports_in;
       for (i = 0; i < pipe->n_ports_in; i++) {

              /* initialize this instance of port_in_args as necessary */
              port_in_args[i].pipe = pipe;
              port_in_args[i].cnxn_tracker = pipe_vfw->cnxn_tracker;

              struct rte_pipeline_port_in_params port_params = {
                     .ops =
                            pipeline_port_in_params_get_ops(&params->port_in
                                          [i]),
                     .arg_create =
                            pipeline_port_in_params_convert(&params->port_in
                                          [i]),
                     .f_action = vfw_port_in_action,
                     .arg_ah = &(port_in_args[i]),
                     .burst_size = params->port_in[i].burst_size,
              };
              if (pipe_vfw->traffic_type == IP_VERSION_4)
                     port_params.f_action = vfw_port_in_action_ipv4;

               if (pipe_vfw->traffic_type == IP_VERSION_6)
                     port_params.f_action = vfw_port_in_action_ipv6;
              int status = rte_pipeline_port_in_create(pipe->p, &port_params,
                            &pipe->port_in_id[i]);

              if (status) {
                     rte_pipeline_free(pipe->p);
                     rte_free(pipe_vfw);
                     return NULL;
              }
       }

       /* Output ports */
       pipe->n_ports_out = params->n_ports_out;
       for (i = 0; i < pipe->n_ports_out; i++) {
              struct rte_pipeline_port_out_params port_params = {
                     .ops = pipeline_port_out_params_get_ops(
                                   &params->port_out[i]),
                     .arg_create = pipeline_port_out_params_convert(
                                   &params->port_out[i]),
                     .f_action = NULL,
                     .arg_ah = NULL,
              };

              int status = rte_pipeline_port_out_create(pipe->p, &port_params,
                            &pipe->port_out_id[i]);

              if (status) {
                     rte_pipeline_free(pipe->p);
                     rte_free(pipe_vfw);
                     return NULL;
              }
       }

       int pipeline_num = 0;
       int dont_care = sscanf(params->name, "PIPELINE%d", &pipeline_num);

       if (dont_care < 0)
              printf("sscanf unble to read pipeline id\n");
       pipe_vfw->pipeline_num = (uint8_t) pipeline_num;
       register_pipeline_Qs(pipe_vfw->pipeline_num, pipe);
       set_link_map(pipe_vfw->pipeline_num, pipe, pipe_vfw->links_map);
       set_outport_id(pipe_vfw->pipeline_num, pipe,
                     pipe_vfw->outport_id);
       printf("pipeline_num=%d\n", pipeline_num);
#ifdef ACL_ENABLE
       /*If this is the first VFW thread, create common VFW Rule tables*/
       if (rte_VFW_hi_counter_block_in_use == 0) {
              vfw_rule_table_ipv4_active =
                     lib_acl_create_active_standby_table_ipv4(1,
                                   &vfw_n_rules);
              if (vfw_rule_table_ipv4_active == NULL) {
                     printf("Failed to create active table for IPV4\n");
                     rte_pipeline_free(pipe->p);
                     rte_free(pipe_vfw->cnxn_tracker);
                     rte_free(pipe_vfw->plib_acl);
                     rte_free(pipe_vfw);
                     return NULL;
              }
              vfw_rule_table_ipv4_standby =
                     lib_acl_create_active_standby_table_ipv4(2,
                                   &vfw_n_rules);
              if (vfw_rule_table_ipv4_standby == NULL) {
                     printf("Failed to create standby table for IPV4\n");
                     rte_pipeline_free(pipe->p);
                     rte_free(pipe_vfw->cnxn_tracker);
                     rte_free(pipe_vfw->plib_acl);
                     rte_free(pipe_vfw);
                     return NULL;
              }

              vfw_rule_table_ipv6_active =
                     lib_acl_create_active_standby_table_ipv6(1,
                                   &vfw_n_rules);

              if (vfw_rule_table_ipv6_active == NULL) {
                     printf("Failed to create active table for IPV6\n");
                     rte_pipeline_free(pipe->p);
                     rte_free(pipe_vfw->cnxn_tracker);
                     rte_free(pipe_vfw->plib_acl);
                     rte_free(pipe_vfw);
                     return NULL;
              }
              vfw_rule_table_ipv6_standby =
                     lib_acl_create_active_standby_table_ipv6(2,
                                   &vfw_n_rules);
              if (vfw_rule_table_ipv6_standby == NULL) {
                     printf("Failed to create standby table for IPV6\n");
                     rte_pipeline_free(pipe->p);
                     rte_free(pipe_vfw->cnxn_tracker);
                     rte_free(pipe_vfw->plib_acl);
                     rte_free(pipe_vfw);
                     return NULL;
              }
       }

#endif

       /* Tables */

       pipe->n_tables = 1;

       struct rte_pipeline_table_params table_params = {
              .ops = &rte_table_stub_ops,
              .arg_create = NULL,
              .f_action_hit = NULL,
              .f_action_miss = NULL,
              .arg_ah = NULL,
              .action_data_size = 0,
       };

       int status = rte_pipeline_table_create(pipe->p,
                     &table_params,
                     &pipe->table_id[0]);

       if (status) {
              rte_pipeline_free(pipe->p);
              rte_free(pipe);
              return NULL;
       }

       struct rte_pipeline_table_entry default_entry = {
              .action = RTE_PIPELINE_ACTION_PORT_META
       };

       struct rte_pipeline_table_entry *default_entry_ptr;

       status = rte_pipeline_table_default_entry_add(pipe->p,
                                                pipe->table_id[0],
                                                &default_entry,
                                                &default_entry_ptr);

       if (status) {
              rte_pipeline_free(pipe->p);
              rte_free(pipe);
              return NULL;
       }
       for (i = 0; i < pipe->n_ports_in; i++) {
              int status = rte_pipeline_port_in_connect_to_table(
                            pipe->p,
                            pipe->port_in_id[i],
                            pipe->table_id[0]);

              if (status) {
                     rte_pipeline_free(pipe->p);
                     rte_free(pipe_vfw);
                     return NULL;
              }
       }

       /* Enable input ports */
       for (i = 0; i < pipe->n_ports_in; i++) {
              int status =
                  rte_pipeline_port_in_enable(pipe->p, pipe->port_in_id[i]);

              if (status) {
                     rte_pipeline_free(pipe->p);
                     rte_free(pipe_vfw);
                     return NULL;
              }
       }

       /* Check pipeline consistency */
       if (rte_pipeline_check(pipe->p) < 0) {
              rte_pipeline_free(pipe->p);
              rte_free(pipe_vfw);
              return NULL;
       }

       /* Message queues */
       pipe->n_msgq = params->n_msgq;
       for (i = 0; i < pipe->n_msgq; i++)
              pipe->msgq_in[i] = params->msgq_in[i];

       for (i = 0; i < pipe->n_msgq; i++)
              pipe->msgq_out[i] = params->msgq_out[i];

       /* Message handlers */
       memcpy(pipe->handlers, handlers, sizeof(pipe->handlers));
       memcpy(pipe_vfw->custom_handlers, custom_handlers,
              sizeof(pipe_vfw->custom_handlers));

       return pipe_vfw;
}

/**
 * Free resources and delete pipeline.
 *
 * @param pipeline
 *  A pointer to the pipeline.
 *
 * @return
 *  0 on success, negative on error.
 */
static int pipeline_vfw_free(void *pipeline)
{
       struct pipeline *p = (struct pipeline *)pipeline;

       /* Check input arguments */
       if (p == NULL)
              return -1;

       /* Free resources */
       rte_pipeline_free(p->p);
       rte_free(p);
       return 0;
}

/**
 * Callback function to map input/output ports.
 *
 * @param pipeline
 *  A pointer to the pipeline.
 * @param port_in
 *  Input port ID
 * @param port_out
 *  A pointer to the Output port.
 *
 * @return
 *  0 on success, negative on error.
 */
static int
pipeline_vfw_track(void *pipeline, __rte_unused uint32_t port_in,
                    uint32_t *port_out)
{
       struct pipeline *p = (struct pipeline *)pipeline;

       /* Check input arguments */
       if ((p == NULL) || (port_in >= p->n_ports_in) || (port_out == NULL))
              return -1;

       if (p->n_ports_in == 1) {
              *port_out = 0;
              return 0;
       }

       return -1;
}

/**
 * Callback function to process timers.
 *
 * @param pipeline
 *  A pointer to the pipeline.
 *
 * @return
 *  0 on success, negative on error.
 */
static int pipeline_vfw_timer(void *pipeline)
{
       struct pipeline_vfw *p = (struct pipeline_vfw *)pipeline;

       /*
        * handle any good buffered packets released by synproxy before checking
        * for packets relased by synproxy due to timeout.
        * Don't want packets missed
        */

       vfw_handle_buffered_packets(p->pipe.p, p, p->cnxn_tracker,
                                   FORWARD_BUFFERED_PACKETS);

       pipeline_msg_req_handle(&p->pipe);
       rte_pipeline_flush(p->pipe.p);

       rte_ct_handle_expired_timers(p->cnxn_tracker);

       /* now handle packets released by synproxy due to timeout. */
       vfw_handle_buffered_packets(p->pipe.p, p, p->cnxn_tracker,
                                   DELETE_BUFFERED_PACKETS);

       return 0;
}

/**
 * Callback function to process CLI commands from FE.
 *
 * @param p
 *  A pointer to the pipeline.
 * @param msg
 *  A pointer to command specific data.
 *
 * @return
 *  A pointer to message handler on success,
 *  pipeline_msg_req_invalid_hander on error.
 */
void *pipeline_vfw_msg_req_custom_handler(struct pipeline *p, void *msg)
{
       struct pipeline_vfw *pipe_vfw = (struct pipeline_vfw *)p;
       struct pipeline_custom_msg_req *req = msg;
       pipeline_msg_req_handler f_handle;

       f_handle = (req->subtype < PIPELINE_VFW_MSG_REQS) ?
           pipe_vfw->custom_handlers[req->subtype] :
           pipeline_msg_req_invalid_handler;

       if (f_handle == NULL)
              f_handle = pipeline_msg_req_invalid_handler;

       return f_handle(p, req);
}

/**
 * Handler for synproxy ON/OFF CLI command.
 *
 * @param p
 *  A pointer to the pipeline.
 * @param msg
 *  A pointer to command specific data.
 *
 * @return
 *  Response message contains status.
 */

void *pipeline_vfw_msg_req_synproxy_flag_handler(struct pipeline *p,
                                              void *msg)
{
       struct pipeline_vfw *pipe_vfw = (struct pipeline_vfw *)p;
       struct pipeline_vfw_synproxy_flag_msg_req *req = msg;
       struct pipeline_vfw_synproxy_flag_msg_rsp *rsp = msg;

       if (req->synproxy_flag == 0) {
              rte_ct_disable_synproxy(pipe_vfw->cnxn_tracker);
              rsp->status = 0;
              printf("synproxy turned OFF for %s\n", p->name);
       } else if (req->synproxy_flag == 1) {
              rte_ct_enable_synproxy(pipe_vfw->cnxn_tracker);
              rsp->status = 0;
              printf("synproxy turned ON for %s\n", p->name);
       } else {
              printf("Invalid synproxy setting\n");
              rsp->status = -1;
       }

       return rsp;
}

struct pipeline_be_ops pipeline_vfw_be_ops = {
       .f_init = pipeline_vfw_init,
       .f_free = pipeline_vfw_free,
       .f_run = NULL,
       .f_timer = pipeline_vfw_timer,
       .f_track = pipeline_vfw_track,
};