aboutsummaryrefslogtreecommitdiffstats
path: root/lib/librte_pipeline/rte_table_action.c
blob: 537e6593e4a0b2f1b6ef687d164c8504b2c3a46b (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
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2010-2018 Intel Corporation
 */
#include <stdlib.h>
#include <string.h>

#include <rte_common.h>
#include <rte_byteorder.h>
#include <rte_cycles.h>
#include <rte_malloc.h>
#include <rte_memcpy.h>
#include <rte_ether.h>
#include <rte_ip.h>
#include <rte_esp.h>
#include <rte_tcp.h>
#include <rte_udp.h>
#include <rte_cryptodev.h>
#include <rte_cryptodev_pmd.h>

#include "rte_table_action.h"

#define rte_htons rte_cpu_to_be_16
#define rte_htonl rte_cpu_to_be_32

#define rte_ntohs rte_be_to_cpu_16
#define rte_ntohl rte_be_to_cpu_32

/**
 * RTE_TABLE_ACTION_FWD
 */
#define fwd_data rte_pipeline_table_entry

static int
fwd_apply(struct fwd_data *data,
	struct rte_table_action_fwd_params *p)
{
	data->action = p->action;

	if (p->action == RTE_PIPELINE_ACTION_PORT)
		data->port_id = p->id;

	if (p->action == RTE_PIPELINE_ACTION_TABLE)
		data->table_id = p->id;

	return 0;
}

/**
 * RTE_TABLE_ACTION_LB
 */
static int
lb_cfg_check(struct rte_table_action_lb_config *cfg)
{
	if ((cfg == NULL) ||
		(cfg->key_size < RTE_TABLE_ACTION_LB_KEY_SIZE_MIN) ||
		(cfg->key_size > RTE_TABLE_ACTION_LB_KEY_SIZE_MAX) ||
		(!rte_is_power_of_2(cfg->key_size)) ||
		(cfg->f_hash == NULL))
		return -1;

	return 0;
}

struct lb_data {
	uint32_t out[RTE_TABLE_ACTION_LB_TABLE_SIZE];
} __attribute__((__packed__));

static int
lb_apply(struct lb_data *data,
	struct rte_table_action_lb_params *p)
{
	memcpy(data->out, p->out, sizeof(data->out));

	return 0;
}

static __rte_always_inline void
pkt_work_lb(struct rte_mbuf *mbuf,
	struct lb_data *data,
	struct rte_table_action_lb_config *cfg)
{
	uint8_t *pkt_key = RTE_MBUF_METADATA_UINT8_PTR(mbuf, cfg->key_offset);
	uint32_t *out = RTE_MBUF_METADATA_UINT32_PTR(mbuf, cfg->out_offset);
	uint64_t digest, pos;
	uint32_t out_val;

	digest = cfg->f_hash(pkt_key,
		cfg->key_mask,
		cfg->key_size,
		cfg->seed);
	pos = digest & (RTE_TABLE_ACTION_LB_TABLE_SIZE - 1);
	out_val = data->out[pos];

	*out = out_val;
}

/**
 * RTE_TABLE_ACTION_MTR
 */
static int
mtr_cfg_check(struct rte_table_action_mtr_config *mtr)
{
	if ((mtr->alg == RTE_TABLE_ACTION_METER_SRTCM) ||
		((mtr->n_tc != 1) && (mtr->n_tc != 4)) ||
		(mtr->n_bytes_enabled != 0))
		return -ENOTSUP;
	return 0;
}

#define MBUF_SCHED_QUEUE_TC_COLOR(queue, tc, color)        \
	((uint16_t)((((uint64_t)(queue)) & 0x3) |          \
	((((uint64_t)(tc)) & 0x3) << 2) |                  \
	((((uint64_t)(color)) & 0x3) << 4)))

#define MBUF_SCHED_COLOR(sched, color)                     \
	(((sched) & (~0x30LLU)) | ((color) << 4))

struct mtr_trtcm_data {
	struct rte_meter_trtcm trtcm;
	uint64_t stats[e_RTE_METER_COLORS];
} __attribute__((__packed__));

#define MTR_TRTCM_DATA_METER_PROFILE_ID_GET(data)          \
	(((data)->stats[e_RTE_METER_GREEN] & 0xF8LLU) >> 3)

static void
mtr_trtcm_data_meter_profile_id_set(struct mtr_trtcm_data *data,
	uint32_t profile_id)
{
	data->stats[e_RTE_METER_GREEN] &= ~0xF8LLU;
	data->stats[e_RTE_METER_GREEN] |= (profile_id % 32) << 3;
}

#define MTR_TRTCM_DATA_POLICER_ACTION_DROP_GET(data, color)\
	(((data)->stats[(color)] & 4LLU) >> 2)

#define MTR_TRTCM_DATA_POLICER_ACTION_COLOR_GET(data, color)\
	((enum rte_meter_color)((data)->stats[(color)] & 3LLU))

static void
mtr_trtcm_data_policer_action_set(struct mtr_trtcm_data *data,
	enum rte_meter_color color,
	enum rte_table_action_policer action)
{
	if (action == RTE_TABLE_ACTION_POLICER_DROP) {
		data->stats[color] |= 4LLU;
	} else {
		data->stats[color] &= ~7LLU;
		data->stats[color] |= color & 3LLU;
	}
}

static uint64_t
mtr_trtcm_data_stats_get(struct mtr_trtcm_data *data,
	enum rte_meter_color color)
{
	return data->stats[color] >> 8;
}

static void
mtr_trtcm_data_stats_reset(struct mtr_trtcm_data *data,
	enum rte_meter_color color)
{
	data->stats[color] &= 0xFFLU;
}

#define MTR_TRTCM_DATA_STATS_INC(data, color)              \
	((data)->stats[(color)] += (1LLU << 8))

static size_t
mtr_data_size(struct rte_table_action_mtr_config *mtr)
{
	return mtr->n_tc * sizeof(struct mtr_trtcm_data);
}

struct dscp_table_entry_data {
	enum rte_meter_color color;
	uint16_t tc;
	uint16_t queue_tc_color;
};

struct dscp_table_data {
	struct dscp_table_entry_data entry[64];
};

struct meter_profile_data {
	struct rte_meter_trtcm_profile profile;
	uint32_t profile_id;
	int valid;
};

static struct meter_profile_data *
meter_profile_data_find(struct meter_profile_data *mp,
	uint32_t mp_size,
	uint32_t profile_id)
{
	uint32_t i;

	for (i = 0; i < mp_size; i++) {
		struct meter_profile_data *mp_data = &mp[i];

		if (mp_data->valid && (mp_data->profile_id == profile_id))
			return mp_data;
	}

	return NULL;
}

static struct meter_profile_data *
meter_profile_data_find_unused(struct meter_profile_data *mp,
	uint32_t mp_size)
{
	uint32_t i;

	for (i = 0; i < mp_size; i++) {
		struct meter_profile_data *mp_data = &mp[i];

		if (!mp_data->valid)
			return mp_data;
	}

	return NULL;
}

static int
mtr_apply_check(struct rte_table_action_mtr_params *p,
	struct rte_table_action_mtr_config *cfg,
	struct meter_profile_data *mp,
	uint32_t mp_size)
{
	uint32_t i;

	if (p->tc_mask > RTE_LEN2MASK(cfg->n_tc, uint32_t))
		return -EINVAL;

	for (i = 0; i < RTE_TABLE_ACTION_TC_MAX; i++) {
		struct rte_table_action_mtr_tc_params *p_tc = &p->mtr[i];
		struct meter_profile_data *mp_data;

		if ((p->tc_mask & (1LLU << i)) == 0)
			continue;

		mp_data = meter_profile_data_find(mp,
			mp_size,
			p_tc->meter_profile_id);
		if (!mp_data)
			return -EINVAL;
	}

	return 0;
}

static int
mtr_apply(struct mtr_trtcm_data *data,
	struct rte_table_action_mtr_params *p,
	struct rte_table_action_mtr_config *cfg,
	struct meter_profile_data *mp,
	uint32_t mp_size)
{
	uint32_t i;
	int status;

	/* Check input arguments */
	status = mtr_apply_check(p, cfg, mp, mp_size);
	if (status)
		return status;

	/* Apply */
	for (i = 0; i < RTE_TABLE_ACTION_TC_MAX; i++) {
		struct rte_table_action_mtr_tc_params *p_tc = &p->mtr[i];
		struct mtr_trtcm_data *data_tc = &data[i];
		struct meter_profile_data *mp_data;

		if ((p->tc_mask & (1LLU << i)) == 0)
			continue;

		/* Find profile */
		mp_data = meter_profile_data_find(mp,
			mp_size,
			p_tc->meter_profile_id);
		if (!mp_data)
			return -EINVAL;

		memset(data_tc, 0, sizeof(*data_tc));

		/* Meter object */
		status = rte_meter_trtcm_config(&data_tc->trtcm,
			&mp_data->profile);
		if (status)
			return status;

		/* Meter profile */
		mtr_trtcm_data_meter_profile_id_set(data_tc,
			mp_data - mp);

		/* Policer actions */
		mtr_trtcm_data_policer_action_set(data_tc,
			e_RTE_METER_GREEN,
			p_tc->policer[e_RTE_METER_GREEN]);

		mtr_trtcm_data_policer_action_set(data_tc,
			e_RTE_METER_YELLOW,
			p_tc->policer[e_RTE_METER_YELLOW]);

		mtr_trtcm_data_policer_action_set(data_tc,
			e_RTE_METER_RED,
			p_tc->policer[e_RTE_METER_RED]);
	}

	return 0;
}

static __rte_always_inline uint64_t
pkt_work_mtr(struct rte_mbuf *mbuf,
	struct mtr_trtcm_data *data,
	struct dscp_table_data *dscp_table,
	struct meter_profile_data *mp,
	uint64_t time,
	uint32_t dscp,
	uint16_t total_length)
{
	uint64_t drop_mask, sched;
	uint64_t *sched_ptr = (uint64_t *) &mbuf->hash.sched;
	struct dscp_table_entry_data *dscp_entry = &dscp_table->entry[dscp];
	enum rte_meter_color color_in, color_meter, color_policer;
	uint32_t tc, mp_id;

	tc = dscp_entry->tc;
	color_in = dscp_entry->color;
	data += tc;
	mp_id = MTR_TRTCM_DATA_METER_PROFILE_ID_GET(data);
	sched = *sched_ptr;

	/* Meter */
	color_meter = rte_meter_trtcm_color_aware_check(
		&data->trtcm,
		&mp[mp_id].profile,
		time,
		total_length,
		color_in);

	/* Stats */
	MTR_TRTCM_DATA_STATS_INC(data, color_meter);

	/* Police */
	drop_mask = MTR_TRTCM_DATA_POLICER_ACTION_DROP_GET(data, color_meter);
	color_policer =
		MTR_TRTCM_DATA_POLICER_ACTION_COLOR_GET(data, color_meter);
	*sched_ptr = MBUF_SCHED_COLOR(sched, color_policer);

	return drop_mask;
}

/**
 * RTE_TABLE_ACTION_TM
 */
static int
tm_cfg_check(struct rte_table_action_tm_config *tm)
{
	if ((tm->n_subports_per_port == 0) ||
		(rte_is_power_of_2(tm->n_subports_per_port) == 0) ||
		(tm->n_subports_per_port > UINT16_MAX) ||
		(tm->n_pipes_per_subport == 0) ||
		(rte_is_power_of_2(tm->n_pipes_per_subport) == 0))
		return -ENOTSUP;

	return 0;
}

struct tm_data {
	uint16_t queue_tc_color;
	uint16_t subport;
	uint32_t pipe;
} __attribute__((__packed__));

static int
tm_apply_check(struct rte_table_action_tm_params *p,
	struct rte_table_action_tm_config *cfg)
{
	if ((p->subport_id >= cfg->n_subports_per_port) ||
		(p->pipe_id >= cfg->n_pipes_per_subport))
		return -EINVAL;

	return 0;
}

static int
tm_apply(struct tm_data *data,
	struct rte_table_action_tm_params *p,
	struct rte_table_action_tm_config *cfg)
{
	int status;

	/* Check input arguments */
	status = tm_apply_check(p, cfg);
	if (status)
		return status;

	/* Apply */
	data->queue_tc_color = 0;
	data->subport = (uint16_t) p->subport_id;
	data->pipe = p->pipe_id;

	return 0;
}

static __rte_always_inline void
pkt_work_tm(struct rte_mbuf *mbuf,
	struct tm_data *data,
	struct dscp_table_data *dscp_table,
	uint32_t dscp)
{
	struct dscp_table_entry_data *dscp_entry = &dscp_table->entry[dscp];
	struct tm_data *sched_ptr = (struct tm_data *) &mbuf->hash.sched;
	struct tm_data sched;

	sched = *data;
	sched.queue_tc_color = dscp_entry->queue_tc_color;
	*sched_ptr = sched;
}

/**
 * RTE_TABLE_ACTION_ENCAP
 */
static int
encap_valid(enum rte_table_action_encap_type encap)
{
	switch (encap) {
	case RTE_TABLE_ACTION_ENCAP_ETHER:
	case RTE_TABLE_ACTION_ENCAP_VLAN:
	case RTE_TABLE_ACTION_ENCAP_QINQ:
	case RTE_TABLE_ACTION_ENCAP_MPLS:
	case RTE_TABLE_ACTION_ENCAP_PPPOE:
	case RTE_TABLE_ACTION_ENCAP_VXLAN:
		return 1;
	default:
		return 0;
	}
}

static int
encap_cfg_check(struct rte_table_action_encap_config *encap)
{
	if ((encap->encap_mask == 0) ||
		(__builtin_popcountll(encap->encap_mask) != 1))
		return -ENOTSUP;

	return 0;
}

struct encap_ether_data {
	struct ether_hdr ether;
} __attribute__((__packed__));

#define VLAN(pcp, dei, vid)                                \
	((uint16_t)((((uint64_t)(pcp)) & 0x7LLU) << 13) |  \
	((((uint64_t)(dei)) & 0x1LLU) << 12) |             \
	(((uint64_t)(vid)) & 0xFFFLLU))                    \

struct encap_vlan_data {
	struct ether_hdr ether;
	struct vlan_hdr vlan;
} __attribute__((__packed__));

struct encap_qinq_data {
	struct ether_hdr ether;
	struct vlan_hdr svlan;
	struct vlan_hdr cvlan;
} __attribute__((__packed__));

#define ETHER_TYPE_MPLS_UNICAST                            0x8847

#define ETHER_TYPE_MPLS_MULTICAST                          0x8848

#define MPLS(label, tc, s, ttl)                            \
	((uint32_t)(((((uint64_t)(label)) & 0xFFFFFLLU) << 12) |\
	((((uint64_t)(tc)) & 0x7LLU) << 9) |               \
	((((uint64_t)(s)) & 0x1LLU) << 8) |                \
	(((uint64_t)(ttl)) & 0xFFLLU)))

struct encap_mpls_data {
	struct ether_hdr ether;
	uint32_t mpls[RTE_TABLE_ACTION_MPLS_LABELS_MAX];
	uint32_t mpls_count;
} __attribute__((__packed__));

#define ETHER_TYPE_PPPOE_SESSION                           0x8864

#define PPP_PROTOCOL_IP                                    0x0021

struct pppoe_ppp_hdr {
	uint16_t ver_type_code;
	uint16_t session_id;
	uint16_t length;
	uint16_t protocol;
} __attribute__((__packed__));

struct encap_pppoe_data {
	struct ether_hdr ether;
	struct pppoe_ppp_hdr pppoe_ppp;
} __attribute__((__packed__));

#define IP_PROTO_UDP                                       17

struct encap_vxlan_ipv4_data {
	struct ether_hdr ether;
	struct ipv4_hdr ipv4;
	struct udp_hdr udp;
	struct vxlan_hdr vxlan;
} __attribute__((__packed__));

struct encap_vxlan_ipv4_vlan_data {
	struct ether_hdr ether;
	struct vlan_hdr vlan;
	struct ipv4_hdr ipv4;
	struct udp_hdr udp;
	struct vxlan_hdr vxlan;
} __attribute__((__packed__));

struct encap_vxlan_ipv6_data {
	struct ether_hdr ether;
	struct ipv6_hdr ipv6;
	struct udp_hdr udp;
	struct vxlan_hdr vxlan;
} __attribute__((__packed__));

struct encap_vxlan_ipv6_vlan_data {
	struct ether_hdr ether;
	struct vlan_hdr vlan;
	struct ipv6_hdr ipv6;
	struct udp_hdr udp;
	struct vxlan_hdr vxlan;
} __attribute__((__packed__));

static size_t
encap_data_size(struct rte_table_action_encap_config *encap)
{
	switch (encap->encap_mask) {
	case 1LLU << RTE_TABLE_ACTION_ENCAP_ETHER:
		return sizeof(struct encap_ether_data);

	case 1LLU << RTE_TABLE_ACTION_ENCAP_VLAN:
		return sizeof(struct encap_vlan_data);

	case 1LLU << RTE_TABLE_ACTION_ENCAP_QINQ:
		return sizeof(struct encap_qinq_data);

	case 1LLU << RTE_TABLE_ACTION_ENCAP_MPLS:
		return sizeof(struct encap_mpls_data);

	case 1LLU << RTE_TABLE_ACTION_ENCAP_PPPOE:
		return sizeof(struct encap_pppoe_data);

	case 1LLU << RTE_TABLE_ACTION_ENCAP_VXLAN:
		if (encap->vxlan.ip_version)
			if (encap->vxlan.vlan)
				return sizeof(struct encap_vxlan_ipv4_vlan_data);
			else
				return sizeof(struct encap_vxlan_ipv4_data);
		else
			if (encap->vxlan.vlan)
				return sizeof(struct encap_vxlan_ipv6_vlan_data);
			else
				return sizeof(struct encap_vxlan_ipv6_data);

	default:
		return 0;
	}
}

static int
encap_apply_check(struct rte_table_action_encap_params *p,
	struct rte_table_action_encap_config *cfg)
{
	if ((encap_valid(p->type) == 0) ||
		((cfg->encap_mask & (1LLU << p->type)) == 0))
		return -EINVAL;

	switch (p->type) {
	case RTE_TABLE_ACTION_ENCAP_ETHER:
		return 0;

	case RTE_TABLE_ACTION_ENCAP_VLAN:
		return 0;

	case RTE_TABLE_ACTION_ENCAP_QINQ:
		return 0;

	case RTE_TABLE_ACTION_ENCAP_MPLS:
		if ((p->mpls.mpls_count == 0) ||
			(p->mpls.mpls_count > RTE_TABLE_ACTION_MPLS_LABELS_MAX))
			return -EINVAL;

		return 0;

	case RTE_TABLE_ACTION_ENCAP_PPPOE:
		return 0;

	case RTE_TABLE_ACTION_ENCAP_VXLAN:
		return 0;

	default:
		return -EINVAL;
	}
}

static int
encap_ether_apply(void *data,
	struct rte_table_action_encap_params *p,
	struct rte_table_action_common_config *common_cfg)
{
	struct encap_ether_data *d = data;
	uint16_t ethertype = (common_cfg->ip_version) ?
		ETHER_TYPE_IPv4 :
		ETHER_TYPE_IPv6;

	/* Ethernet */
	ether_addr_copy(&p->ether.ether.da, &d->ether.d_addr);
	ether_addr_copy(&p->ether.ether.sa, &d->ether.s_addr);
	d->ether.ether_type = rte_htons(ethertype);

	return 0;
}

static int
encap_vlan_apply(void *data,
	struct rte_table_action_encap_params *p,
	struct rte_table_action_common_config *common_cfg)
{
	struct encap_vlan_data *d = data;
	uint16_t ethertype = (common_cfg->ip_version) ?
		ETHER_TYPE_IPv4 :
		ETHER_TYPE_IPv6;

	/* Ethernet */
	ether_addr_copy(&p->vlan.ether.da, &d->ether.d_addr);
	ether_addr_copy(&p->vlan.ether.sa, &d->ether.s_addr);
	d->ether.ether_type = rte_htons(ETHER_TYPE_VLAN);

	/* VLAN */
	d->vlan.vlan_tci = rte_htons(VLAN(p->vlan.vlan.pcp,
		p->vlan.vlan.dei,
		p->vlan.vlan.vid));
	d->vlan.eth_proto = rte_htons(ethertype);

	return 0;
}

static int
encap_qinq_apply(void *data,
	struct rte_table_action_encap_params *p,
	struct rte_table_action_common_config *common_cfg)
{
	struct encap_qinq_data *d = data;
	uint16_t ethertype = (common_cfg->ip_version) ?
		ETHER_TYPE_IPv4 :
		ETHER_TYPE_IPv6;

	/* Ethernet */
	ether_addr_copy(&p->qinq.ether.da, &d->ether.d_addr);
	ether_addr_copy(&p->qinq.ether.sa, &d->ether.s_addr);
	d->ether.ether_type = rte_htons(ETHER_TYPE_QINQ);

	/* SVLAN */
	d->svlan.vlan_tci = rte_htons(VLAN(p->qinq.svlan.pcp,
		p->qinq.svlan.dei,
		p->qinq.svlan.vid));
	d->svlan.eth_proto = rte_htons(ETHER_TYPE_VLAN);

	/* CVLAN */
	d->cvlan.vlan_tci = rte_htons(VLAN(p->qinq.cvlan.pcp,
		p->qinq.cvlan.dei,
		p->qinq.cvlan.vid));
	d->cvlan.eth_proto = rte_htons(ethertype);

	return 0;
}

static int
encap_mpls_apply(void *data,
	struct rte_table_action_encap_params *p)
{
	struct encap_mpls_data *d = data;
	uint16_t ethertype = (p->mpls.unicast) ?
		ETHER_TYPE_MPLS_UNICAST :
		ETHER_TYPE_MPLS_MULTICAST;
	uint32_t i;

	/* Ethernet */
	ether_addr_copy(&p->mpls.ether.da, &d->ether.d_addr);
	ether_addr_copy(&p->mpls.ether.sa, &d->ether.s_addr);
	d->ether.ether_type = rte_htons(ethertype);

	/* MPLS */
	for (i = 0; i < p->mpls.mpls_count - 1; i++)
		d->mpls[i] = rte_htonl(MPLS(p->mpls.mpls[i].label,
			p->mpls.mpls[i].tc,
			0,
			p->mpls.mpls[i].ttl));

	d->mpls[i] = rte_htonl(MPLS(p->mpls.mpls[i].label,
		p->mpls.mpls[i].tc,
		1,
		p->mpls.mpls[i].ttl));

	d->mpls_count = p->mpls.mpls_count;
	return 0;
}

static int
encap_pppoe_apply(void *data,
	struct rte_table_action_encap_params *p)
{
	struct encap_pppoe_data *d = data;

	/* Ethernet */
	ether_addr_copy(&p->pppoe.ether.da, &d->ether.d_addr);
	ether_addr_copy(&p->pppoe.ether.sa, &d->ether.s_addr);
	d->ether.ether_type = rte_htons(ETHER_TYPE_PPPOE_SESSION);

	/* PPPoE and PPP*/
	d->pppoe_ppp.ver_type_code = rte_htons(0x1100);
	d->pppoe_ppp.session_id = rte_htons(p->pppoe.pppoe.session_id);
	d->pppoe_ppp.length = 0; /* not pre-computed */
	d->pppoe_ppp.protocol = rte_htons(PPP_PROTOCOL_IP);

	return 0;
}

static int
encap_vxlan_apply(void *data,
	struct rte_table_action_encap_params *p,
	struct rte_table_action_encap_config *cfg)
{
	if ((p->vxlan.vxlan.vni > 0xFFFFFF) ||
		(cfg->vxlan.ip_version && (p->vxlan.ipv4.dscp > 0x3F)) ||
		(!cfg->vxlan.ip_version && (p->vxlan.ipv6.flow_label > 0xFFFFF)) ||
		(!cfg->vxlan.ip_version && (p->vxlan.ipv6.dscp > 0x3F)) ||
		(cfg->vxlan.vlan && (p->vxlan.vlan.vid > 0xFFF)))
		return -1;

	if (cfg->vxlan.ip_version)
		if (cfg->vxlan.vlan) {
			struct encap_vxlan_ipv4_vlan_data *d = data;

			/* Ethernet */
			ether_addr_copy(&p->vxlan.ether.da, &d->ether.d_addr);
			ether_addr_copy(&p->vxlan.ether.sa, &d->ether.s_addr);
			d->ether.ether_type = rte_htons(ETHER_TYPE_VLAN);

			/* VLAN */
			d->vlan.vlan_tci = rte_htons(VLAN(p->vxlan.vlan.pcp,
				p->vxlan.vlan.dei,
				p->vxlan.vlan.vid));
			d->vlan.eth_proto = rte_htons(ETHER_TYPE_IPv4);

			/* IPv4*/
			d->ipv4.version_ihl = 0x45;
			d->ipv4.type_of_service = p->vxlan.ipv4.dscp << 2;
			d->ipv4.total_length = 0; /* not pre-computed */
			d->ipv4.packet_id = 0;
			d->ipv4.fragment_offset = 0;
			d->ipv4.time_to_live = p->vxlan.ipv4.ttl;
			d->ipv4.next_proto_id = IP_PROTO_UDP;
			d->ipv4.hdr_checksum = 0;
			d->ipv4.src_addr = rte_htonl(p->vxlan.ipv4.sa);
			d->ipv4.dst_addr = rte_htonl(p->vxlan.ipv4.da);

			d->ipv4.hdr_checksum = rte_ipv4_cksum(&d->ipv4);

			/* UDP */
			d->udp.src_port = rte_htons(p->vxlan.udp.sp);
			d->udp.dst_port = rte_htons(p->vxlan.udp.dp);
			d->udp.dgram_len = 0; /* not pre-computed */
			d->udp.dgram_cksum = 0;

			/* VXLAN */
			d->vxlan.vx_flags = rte_htonl(0x08000000);
			d->vxlan.vx_vni = rte_htonl(p->vxlan.vxlan.vni << 8);

			return 0;
		} else {
			struct encap_vxlan_ipv4_data *d = data;

			/* Ethernet */
			ether_addr_copy(&p->vxlan.ether.da, &d->ether.d_addr);
			ether_addr_copy(&p->vxlan.ether.sa, &d->ether.s_addr);
			d->ether.ether_type = rte_htons(ETHER_TYPE_IPv4);

			/* IPv4*/
			d->ipv4.version_ihl = 0x45;
			d->ipv4.type_of_service = p->vxlan.ipv4.dscp << 2;
			d->ipv4.total_length = 0; /* not pre-computed */
			d->ipv4.packet_id = 0;
			d->ipv4.fragment_offset = 0;
			d->ipv4.time_to_live = p->vxlan.ipv4.ttl;
			d->ipv4.next_proto_id = IP_PROTO_UDP;
			d->ipv4.hdr_checksum = 0;
			d->ipv4.src_addr = rte_htonl(p->vxlan.ipv4.sa);
			d->ipv4.dst_addr = rte_htonl(p->vxlan.ipv4.da);

			d->ipv4.hdr_checksum = rte_ipv4_cksum(&d->ipv4);

			/* UDP */
			d->udp.src_port = rte_htons(p->vxlan.udp.sp);
			d->udp.dst_port = rte_htons(p->vxlan.udp.dp);
			d->udp.dgram_len = 0; /* not pre-computed */
			d->udp.dgram_cksum = 0;

			/* VXLAN */
			d->vxlan.vx_flags = rte_htonl(0x08000000);
			d->vxlan.vx_vni = rte_htonl(p->vxlan.vxlan.vni << 8);

			return 0;
		}
	else
		if (cfg->vxlan.vlan) {
			struct encap_vxlan_ipv6_vlan_data *d = data;

			/* Ethernet */
			ether_addr_copy(&p->vxlan.ether.da, &d->ether.d_addr);
			ether_addr_copy(&p->vxlan.ether.sa, &d->ether.s_addr);
			d->ether.ether_type = rte_htons(ETHER_TYPE_VLAN);

			/* VLAN */
			d->vlan.vlan_tci = rte_htons(VLAN(p->vxlan.vlan.pcp,
				p->vxlan.vlan.dei,
				p->vxlan.vlan.vid));
			d->vlan.eth_proto = rte_htons(ETHER_TYPE_IPv6);

			/* IPv6*/
			d->ipv6.vtc_flow = rte_htonl((6 << 28) |
				(p->vxlan.ipv6.dscp << 22) |
				p->vxlan.ipv6.flow_label);
			d->ipv6.payload_len = 0; /* not pre-computed */
			d->ipv6.proto = IP_PROTO_UDP;
			d->ipv6.hop_limits = p->vxlan.ipv6.hop_limit;
			memcpy(d->ipv6.src_addr,
				p->vxlan.ipv6.sa,
				sizeof(p->vxlan.ipv6.sa));
			memcpy(d->ipv6.dst_addr,
				p->vxlan.ipv6.da,
				sizeof(p->vxlan.ipv6.da));

			/* UDP */
			d->udp.src_port = rte_htons(p->vxlan.udp.sp);
			d->udp.dst_port = rte_htons(p->vxlan.udp.dp);
			d->udp.dgram_len = 0; /* not pre-computed */
			d->udp.dgram_cksum = 0;

			/* VXLAN */
			d->vxlan.vx_flags = rte_htonl(0x08000000);
			d->vxlan.vx_vni = rte_htonl(p->vxlan.vxlan.vni << 8);

			return 0;
		} else {
			struct encap_vxlan_ipv6_data *d = data;

			/* Ethernet */
			ether_addr_copy(&p->vxlan.ether.da, &d->ether.d_addr);
			ether_addr_copy(&p->vxlan.ether.sa, &d->ether.s_addr);
			d->ether.ether_type = rte_htons(ETHER_TYPE_IPv6);

			/* IPv6*/
			d->ipv6.vtc_flow = rte_htonl((6 << 28) |
				(p->vxlan.ipv6.dscp << 22) |
				p->vxlan.ipv6.flow_label);
			d->ipv6.payload_len = 0; /* not pre-computed */
			d->ipv6.proto = IP_PROTO_UDP;
			d->ipv6.hop_limits = p->vxlan.ipv6.hop_limit;
			memcpy(d->ipv6.src_addr,
				p->vxlan.ipv6.sa,
				sizeof(p->vxlan.ipv6.sa));
			memcpy(d->ipv6.dst_addr,
				p->vxlan.ipv6.da,
				sizeof(p->vxlan.ipv6.da));

			/* UDP */
			d->udp.src_port = rte_htons(p->vxlan.udp.sp);
			d->udp.dst_port = rte_htons(p->vxlan.udp.dp);
			d->udp.dgram_len = 0; /* not pre-computed */
			d->udp.dgram_cksum = 0;

			/* VXLAN */
			d->vxlan.vx_flags = rte_htonl(0x08000000);
			d->vxlan.vx_vni = rte_htonl(p->vxlan.vxlan.vni << 8);

			return 0;
		}
}

static int
encap_apply(void *data,
	struct rte_table_action_encap_params *p,
	struct rte_table_action_encap_config *cfg,
	struct rte_table_action_common_config *common_cfg)
{
	int status;

	/* Check input arguments */
	status = encap_apply_check(p, cfg);
	if (status)
		return status;

	switch (p->type) {
	case RTE_TABLE_ACTION_ENCAP_ETHER:
		return encap_ether_apply(data, p, common_cfg);

	case RTE_TABLE_ACTION_ENCAP_VLAN:
		return encap_vlan_apply(data, p, common_cfg);

	case RTE_TABLE_ACTION_ENCAP_QINQ:
		return encap_qinq_apply(data, p, common_cfg);

	case RTE_TABLE_ACTION_ENCAP_MPLS:
		return encap_mpls_apply(data, p);

	case RTE_TABLE_ACTION_ENCAP_PPPOE:
		return encap_pppoe_apply(data, p);

	case RTE_TABLE_ACTION_ENCAP_VXLAN:
		return encap_vxlan_apply(data, p, cfg);

	default:
		return -EINVAL;
	}
}

static __rte_always_inline uint16_t
encap_vxlan_ipv4_checksum_update(uint16_t cksum0,
	uint16_t total_length)
{
	int32_t cksum1;

	cksum1 = cksum0;
	cksum1 = ~cksum1 & 0xFFFF;

	/* Add total length (one's complement logic) */
	cksum1 += total_length;
	cksum1 = (cksum1 & 0xFFFF) + (cksum1 >> 16);
	cksum1 = (cksum1 & 0xFFFF) + (cksum1 >> 16);

	return (uint16_t)(~cksum1);
}

static __rte_always_inline void *
encap(void *dst, const void *src, size_t n)
{
	dst = ((uint8_t *) dst) - n;
	return rte_memcpy(dst, src, n);
}

static __rte_always_inline void
pkt_work_encap_vxlan_ipv4(struct rte_mbuf *mbuf,
	struct encap_vxlan_ipv4_data *vxlan_tbl,
	struct rte_table_action_encap_config *cfg)
{
	uint32_t ether_offset = cfg->vxlan.data_offset;
	void *ether = RTE_MBUF_METADATA_UINT32_PTR(mbuf, ether_offset);
	struct encap_vxlan_ipv4_data *vxlan_pkt;
	uint16_t ether_length, ipv4_total_length, ipv4_hdr_cksum, udp_length;

	ether_length = (uint16_t)mbuf->pkt_len;
	ipv4_total_length = ether_length +
		(sizeof(struct vxlan_hdr) +
		sizeof(struct udp_hdr) +
		sizeof(struct ipv4_hdr));
	ipv4_hdr_cksum = encap_vxlan_ipv4_checksum_update(vxlan_tbl->ipv4.hdr_checksum,
		rte_htons(ipv4_total_length));
	udp_length = ether_length +
		(sizeof(struct vxlan_hdr) +
		sizeof(struct udp_hdr));

	vxlan_pkt = encap(ether, vxlan_tbl, sizeof(*vxlan_tbl));
	vxlan_pkt->ipv4.total_length = rte_htons(ipv4_total_length);
	vxlan_pkt->ipv4.hdr_checksum = ipv4_hdr_cksum;
	vxlan_pkt->udp.dgram_len = rte_htons(udp_length);

	mbuf->data_off = ether_offset - (sizeof(struct rte_mbuf) + sizeof(*vxlan_pkt));
	mbuf->pkt_len = mbuf->data_len = ether_length + sizeof(*vxlan_pkt);
}

static __rte_always_inline void
pkt_work_encap_vxlan_ipv4_vlan(struct rte_mbuf *mbuf,
	struct encap_vxlan_ipv4_vlan_data *vxlan_tbl,
	struct rte_table_action_encap_config *cfg)
{
	uint32_t ether_offset = cfg->vxlan.data_offset;
	void *ether = RTE_MBUF_METADATA_UINT32_PTR(mbuf, ether_offset);
	struct encap_vxlan_ipv4_vlan_data *vxlan_pkt;
	uint16_t ether_length, ipv4_total_length, ipv4_hdr_cksum, udp_length;

	ether_length = (uint16_t)mbuf->pkt_len;
	ipv4_total_length = ether_length +
		(sizeof(struct vxlan_hdr) +
		sizeof(struct udp_hdr) +
		sizeof(struct ipv4_hdr));
	ipv4_hdr_cksum = encap_vxlan_ipv4_checksum_update(vxlan_tbl->ipv4.hdr_checksum,
		rte_htons(ipv4_total_length));
	udp_length = ether_length +
		(sizeof(struct vxlan_hdr) +
		sizeof(struct udp_hdr));

	vxlan_pkt = encap(ether, vxlan_tbl, sizeof(*vxlan_tbl));
	vxlan_pkt->ipv4.total_length = rte_htons(ipv4_total_length);
	vxlan_pkt->ipv4.hdr_checksum = ipv4_hdr_cksum;
	vxlan_pkt->udp.dgram_len = rte_htons(udp_length);

	mbuf->data_off = ether_offset - (sizeof(struct rte_mbuf) + sizeof(*vxlan_pkt));
	mbuf->pkt_len = mbuf->data_len = ether_length + sizeof(*vxlan_pkt);
}

static __rte_always_inline void
pkt_work_encap_vxlan_ipv6(struct rte_mbuf *mbuf,
	struct encap_vxlan_ipv6_data *vxlan_tbl,
	struct rte_table_action_encap_config *cfg)
{
	uint32_t ether_offset = cfg->vxlan.data_offset;
	void *ether = RTE_MBUF_METADATA_UINT32_PTR(mbuf, ether_offset);
	struct encap_vxlan_ipv6_data *vxlan_pkt;
	uint16_t ether_length, ipv6_payload_length, udp_length;

	ether_length = (uint16_t)mbuf->pkt_len;
	ipv6_payload_length = ether_length +
		(sizeof(struct vxlan_hdr) +
		sizeof(struct udp_hdr));
	udp_length = ether_length +
		(sizeof(struct vxlan_hdr) +
		sizeof(struct udp_hdr));

	vxlan_pkt = encap(ether, vxlan_tbl, sizeof(*vxlan_tbl));
	vxlan_pkt->ipv6.payload_len = rte_htons(ipv6_payload_length);
	vxlan_pkt->udp.dgram_len = rte_htons(udp_length);

	mbuf->data_off = ether_offset - (sizeof(struct rte_mbuf) + sizeof(*vxlan_pkt));
	mbuf->pkt_len = mbuf->data_len = ether_length + sizeof(*vxlan_pkt);
}

static __rte_always_inline void
pkt_work_encap_vxlan_ipv6_vlan(struct rte_mbuf *mbuf,
	struct encap_vxlan_ipv6_vlan_data *vxlan_tbl,
	struct rte_table_action_encap_config *cfg)
{
	uint32_t ether_offset = cfg->vxlan.data_offset;
	void *ether = RTE_MBUF_METADATA_UINT32_PTR(mbuf, ether_offset);
	struct encap_vxlan_ipv6_vlan_data *vxlan_pkt;
	uint16_t ether_length, ipv6_payload_length, udp_length;

	ether_length = (uint16_t)mbuf->pkt_len;
	ipv6_payload_length = ether_length +
		(sizeof(struct vxlan_hdr) +
		sizeof(struct udp_hdr));
	udp_length = ether_length +
		(sizeof(struct vxlan_hdr) +
		sizeof(struct udp_hdr));

	vxlan_pkt = encap(ether, vxlan_tbl, sizeof(*vxlan_tbl));
	vxlan_pkt->ipv6.payload_len = rte_htons(ipv6_payload_length);
	vxlan_pkt->udp.dgram_len = rte_htons(udp_length);

	mbuf->data_off = ether_offset - (sizeof(struct rte_mbuf) + sizeof(*vxlan_pkt));
	mbuf->pkt_len = mbuf->data_len = ether_length + sizeof(*vxlan_pkt);
}

static __rte_always_inline void
pkt_work_encap(struct rte_mbuf *mbuf,
	void *data,
	struct rte_table_action_encap_config *cfg,
	void *ip,
	uint16_t total_length,
	uint32_t ip_offset)
{
	switch (cfg->encap_mask) {
	case 1LLU << RTE_TABLE_ACTION_ENCAP_ETHER:
		encap(ip, data, sizeof(struct encap_ether_data));
		mbuf->data_off = ip_offset - (sizeof(struct rte_mbuf) +
			sizeof(struct encap_ether_data));
		mbuf->pkt_len = mbuf->data_len = total_length +
			sizeof(struct encap_ether_data);
		break;

	case 1LLU << RTE_TABLE_ACTION_ENCAP_VLAN:
		encap(ip, data, sizeof(struct encap_vlan_data));
		mbuf->data_off = ip_offset - (sizeof(struct rte_mbuf) +
			sizeof(struct encap_vlan_data));
		mbuf->pkt_len = mbuf->data_len = total_length +
			sizeof(struct encap_vlan_data);
		break;

	case 1LLU << RTE_TABLE_ACTION_ENCAP_QINQ:
		encap(ip, data, sizeof(struct encap_qinq_data));
		mbuf->data_off = ip_offset - (sizeof(struct rte_mbuf) +
			sizeof(struct encap_qinq_data));
		mbuf->pkt_len = mbuf->data_len = total_length +
			sizeof(struct encap_qinq_data);
		break;

	case 1LLU << RTE_TABLE_ACTION_ENCAP_MPLS:
	{
		struct encap_mpls_data *mpls = data;
		size_t size = sizeof(struct ether_hdr) +
			mpls->mpls_count * 4;

		encap(ip, data, size);
		mbuf->data_off = ip_offset - (sizeof(struct rte_mbuf) + size);
		mbuf->pkt_len = mbuf->data_len = total_length + size;
		break;
	}

	case 1LLU << RTE_TABLE_ACTION_ENCAP_PPPOE:
	{
		struct encap_pppoe_data *pppoe =
			encap(ip, data, sizeof(struct encap_pppoe_data));
		pppoe->pppoe_ppp.length = rte_htons(total_length + 2);
		mbuf->data_off = ip_offset - (sizeof(struct rte_mbuf) +
			sizeof(struct encap_pppoe_data));
		mbuf->pkt_len = mbuf->data_len = total_length +
			sizeof(struct encap_pppoe_data);
		break;
	}

	case 1LLU << RTE_TABLE_ACTION_ENCAP_VXLAN:
	{
		if (cfg->vxlan.ip_version)
			if (cfg->vxlan.vlan)
				pkt_work_encap_vxlan_ipv4_vlan(mbuf, data, cfg);
			else
				pkt_work_encap_vxlan_ipv4(mbuf, data, cfg);
		else
			if (cfg->vxlan.vlan)
				pkt_work_encap_vxlan_ipv6_vlan(mbuf, data, cfg);
			else
				pkt_work_encap_vxlan_ipv6(mbuf, data, cfg);
	}

	default:
		break;
	}
}

/**
 * RTE_TABLE_ACTION_NAT
 */
static int
nat_cfg_check(struct rte_table_action_nat_config *nat)
{
	if ((nat->proto != 0x06) &&
		(nat->proto != 0x11))
		return -ENOTSUP;

	return 0;
}

struct nat_ipv4_data {
	uint32_t addr;
	uint16_t port;
} __attribute__((__packed__));

struct nat_ipv6_data {
	uint8_t addr[16];
	uint16_t port;
} __attribute__((__packed__));

static size_t
nat_data_size(struct rte_table_action_nat_config *nat __rte_unused,
	struct rte_table_action_common_config *common)
{
	int ip_version = common->ip_version;

	return (ip_version) ?
		sizeof(struct nat_ipv4_data) :
		sizeof(struct nat_ipv6_data);
}

static int
nat_apply_check(struct rte_table_action_nat_params *p,
	struct rte_table_action_common_config *cfg)
{
	if ((p->ip_version && (cfg->ip_version == 0)) ||
		((p->ip_version == 0) && cfg->ip_version))
		return -EINVAL;

	return 0;
}

static int
nat_apply(void *data,
	struct rte_table_action_nat_params *p,
	struct rte_table_action_common_config *cfg)
{
	int status;

	/* Check input arguments */
	status = nat_apply_check(p, cfg);
	if (status)
		return status;

	/* Apply */
	if (p->ip_version) {
		struct nat_ipv4_data *d = data;

		d->addr = rte_htonl(p->addr.ipv4);
		d->port = rte_htons(p->port);
	} else {
		struct nat_ipv6_data *d = data;

		memcpy(d->addr, p->addr.ipv6, sizeof(d->addr));
		d->port = rte_htons(p->port);
	}

	return 0;
}

static __rte_always_inline uint16_t
nat_ipv4_checksum_update(uint16_t cksum0,
	uint32_t ip0,
	uint32_t ip1)
{
	int32_t cksum1;

	cksum1 = cksum0;
	cksum1 = ~cksum1 & 0xFFFF;

	/* Subtract ip0 (one's complement logic) */
	cksum1 -= (ip0 >> 16) + (ip0 & 0xFFFF);
	cksum1 = (cksum1 & 0xFFFF) + (cksum1 >> 16);
	cksum1 = (cksum1 & 0xFFFF) + (cksum1 >> 16);

	/* Add ip1 (one's complement logic) */
	cksum1 += (ip1 >> 16) + (ip1 & 0xFFFF);
	cksum1 = (cksum1 & 0xFFFF) + (cksum1 >> 16);
	cksum1 = (cksum1 & 0xFFFF) + (cksum1 >> 16);

	return (uint16_t)(~cksum1);
}

static __rte_always_inline uint16_t
nat_ipv4_tcp_udp_checksum_update(uint16_t cksum0,
	uint32_t ip0,
	uint32_t ip1,
	uint16_t port0,
	uint16_t port1)
{
	int32_t cksum1;

	cksum1 = cksum0;
	cksum1 = ~cksum1 & 0xFFFF;

	/* Subtract ip0 and port 0 (one's complement logic) */
	cksum1 -= (ip0 >> 16) + (ip0 & 0xFFFF) + port0;
	cksum1 = (cksum1 & 0xFFFF) + (cksum1 >> 16);
	cksum1 = (cksum1 & 0xFFFF) + (cksum1 >> 16);

	/* Add ip1 and port1 (one's complement logic) */
	cksum1 += (ip1 >> 16) + (ip1 & 0xFFFF) + port1;
	cksum1 = (cksum1 & 0xFFFF) + (cksum1 >> 16);
	cksum1 = (cksum1 & 0xFFFF) + (cksum1 >> 16);

	return (uint16_t)(~cksum1);
}

static __rte_always_inline uint16_t
nat_ipv6_tcp_udp_checksum_update(uint16_t cksum0,
	uint16_t *ip0,
	uint16_t *ip1,
	uint16_t port0,
	uint16_t port1)
{
	int32_t cksum1;

	cksum1 = cksum0;
	cksum1 = ~cksum1 & 0xFFFF;

	/* Subtract ip0 and port 0 (one's complement logic) */
	cksum1 -= ip0[0] + ip0[1] + ip0[2] + ip0[3] +
		ip0[4] + ip0[5] + ip0[6] + ip0[7] + port0;
	cksum1 = (cksum1 & 0xFFFF) + (cksum1 >> 16);
	cksum1 = (cksum1 & 0xFFFF) + (cksum1 >> 16);

	/* Add ip1 and port1 (one's complement logic) */
	cksum1 += ip1[0] + ip1[1] + ip1[2] + ip1[3] +
		ip1[4] + ip1[5] + ip1[6] + ip1[7] + port1;
	cksum1 = (cksum1 & 0xFFFF) + (cksum1 >> 16);
	cksum1 = (cksum1 & 0xFFFF) + (cksum1 >> 16);

	return (uint16_t)(~cksum1);
}

static __rte_always_inline void
pkt_ipv4_work_nat(struct ipv4_hdr *ip,
	struct nat_ipv4_data *data,
	struct rte_table_action_nat_config *cfg)
{
	if (cfg->source_nat) {
		if (cfg->proto == 0x6) {
			struct tcp_hdr *tcp = (struct tcp_hdr *) &ip[1];
			uint16_t ip_cksum, tcp_cksum;

			ip_cksum = nat_ipv4_checksum_update(ip->hdr_checksum,
				ip->src_addr,
				data->addr);

			tcp_cksum = nat_ipv4_tcp_udp_checksum_update(tcp->cksum,
				ip->src_addr,
				data->addr,
				tcp->src_port,
				data->port);

			ip->src_addr = data->addr;
			ip->hdr_checksum = ip_cksum;
			tcp->src_port = data->port;
			tcp->cksum = tcp_cksum;
		} else {
			struct udp_hdr *udp = (struct udp_hdr *) &ip[1];
			uint16_t ip_cksum, udp_cksum;

			ip_cksum = nat_ipv4_checksum_update(ip->hdr_checksum,
				ip->src_addr,
				data->addr);

			udp_cksum = nat_ipv4_tcp_udp_checksum_update(udp->dgram_cksum,
				ip->src_addr,
				data->addr,
				udp->src_port,
				data->port);

			ip->src_addr = data->addr;
			ip->hdr_checksum = ip_cksum;
			udp->src_port = data->port;
			if (udp->dgram_cksum)
				udp->dgram_cksum = udp_cksum;
		}
	} else {
		if (cfg->proto == 0x6) {
			struct tcp_hdr *tcp = (struct tcp_hdr *) &ip[1];
			uint16_t ip_cksum, tcp_cksum;

			ip_cksum = nat_ipv4_checksum_update(ip->hdr_checksum,
				ip->dst_addr,
				data->addr);

			tcp_cksum = nat_ipv4_tcp_udp_checksum_update(tcp->cksum,
				ip->dst_addr,
				data->addr,
				tcp->dst_port,
				data->port);

			ip->dst_addr = data->addr;
			ip->hdr_checksum = ip_cksum;
			tcp->dst_port = data->port;
			tcp->cksum = tcp_cksum;
		} else {
			struct udp_hdr *udp = (struct udp_hdr *) &ip[1];
			uint16_t ip_cksum, udp_cksum;

			ip_cksum = nat_ipv4_checksum_update(ip->hdr_checksum,
				ip->dst_addr,
				data->addr);

			udp_cksum = nat_ipv4_tcp_udp_checksum_update(udp->dgram_cksum,
				ip->dst_addr,
				data->addr,
				udp->dst_port,
				data->port);

			ip->dst_addr = data->addr;
			ip->hdr_checksum = ip_cksum;
			udp->dst_port = data->port;
			if (udp->dgram_cksum)
				udp->dgram_cksum = udp_cksum;
		}
	}
}

static __rte_always_inline void
pkt_ipv6_work_nat(struct ipv6_hdr *ip,
	struct nat_ipv6_data *data,
	struct rte_table_action_nat_config *cfg)
{
	if (cfg->source_nat) {
		if (cfg->proto == 0x6) {
			struct tcp_hdr *tcp = (struct tcp_hdr *) &ip[1];
			uint16_t tcp_cksum;

			tcp_cksum = nat_ipv6_tcp_udp_checksum_update(tcp->cksum,
				(uint16_t *)ip->src_addr,
				(uint16_t *)data->addr,
				tcp->src_port,
				data->port);

			rte_memcpy(ip->src_addr, data->addr, 16);
			tcp->src_port = data->port;
			tcp->cksum = tcp_cksum;
		} else {
			struct udp_hdr *udp = (struct udp_hdr *) &ip[1];
			uint16_t udp_cksum;

			udp_cksum = nat_ipv6_tcp_udp_checksum_update(udp->dgram_cksum,
				(uint16_t *)ip->src_addr,
				(uint16_t *)data->addr,
				udp->src_port,
				data->port);

			rte_memcpy(ip->src_addr, data->addr, 16);
			udp->src_port = data->port;
			udp->dgram_cksum = udp_cksum;
		}
	} else {
		if (cfg->proto == 0x6) {
			struct tcp_hdr *tcp = (struct tcp_hdr *) &ip[1];
			uint16_t tcp_cksum;

			tcp_cksum = nat_ipv6_tcp_udp_checksum_update(tcp->cksum,
				(uint16_t *)ip->dst_addr,
				(uint16_t *)data->addr,
				tcp->dst_port,
				data->port);

			rte_memcpy(ip->dst_addr, data->addr, 16);
			tcp->dst_port = data->port;
			tcp->cksum = tcp_cksum;
		} else {
			struct udp_hdr *udp = (struct udp_hdr *) &ip[1];
			uint16_t udp_cksum;

			udp_cksum = nat_ipv6_tcp_udp_checksum_update(udp->dgram_cksum,
				(uint16_t *)ip->dst_addr,
				(uint16_t *)data->addr,
				udp->dst_port,
				data->port);

			rte_memcpy(ip->dst_addr, data->addr, 16);
			udp->dst_port = data->port;
			udp->dgram_cksum = udp_cksum;
		}
	}
}

/**
 * RTE_TABLE_ACTION_TTL
 */
static int
ttl_cfg_check(struct rte_table_action_ttl_config *ttl)
{
	if (ttl->drop == 0)
		return -ENOTSUP;

	return 0;
}

struct ttl_data {
	uint32_t n_packets;
} __attribute__((__packed__));

#define TTL_INIT(data, decrement)                         \
	((data)->n_packets = (decrement) ? 1 : 0)

#define TTL_DEC_GET(data)                                  \
	((uint8_t)((data)->n_packets & 1))

#define TTL_STATS_RESET(data)                             \
	((data)->n_packets = ((data)->n_packets & 1))

#define TTL_STATS_READ(data)                               \
	((data)->n_packets >> 1)

#define TTL_STATS_ADD(data, value)                        \
	((data)->n_packets =                                  \
		(((((data)->n_packets >> 1) + (value)) << 1) |    \
		((data)->n_packets & 1)))

static int
ttl_apply(void *data,
	struct rte_table_action_ttl_params *p)
{
	struct ttl_data *d = data;

	TTL_INIT(d, p->decrement);

	return 0;
}

static __rte_always_inline uint64_t
pkt_ipv4_work_ttl(struct ipv4_hdr *ip,
	struct ttl_data *data)
{
	uint32_t drop;
	uint16_t cksum = ip->hdr_checksum;
	uint8_t ttl = ip->time_to_live;
	uint8_t ttl_diff = TTL_DEC_GET(data);

	cksum += ttl_diff;
	ttl -= ttl_diff;

	ip->hdr_checksum = cksum;
	ip->time_to_live = ttl;

	drop = (ttl == 0) ? 1 : 0;
	TTL_STATS_ADD(data, drop);

	return drop;
}

static __rte_always_inline uint64_t
pkt_ipv6_work_ttl(struct ipv6_hdr *ip,
	struct ttl_data *data)
{
	uint32_t drop;
	uint8_t ttl = ip->hop_limits;
	uint8_t ttl_diff = TTL_DEC_GET(data);

	ttl -= ttl_diff;

	ip->hop_limits = ttl;

	drop = (ttl == 0) ? 1 : 0;
	TTL_STATS_ADD(data, drop);

	return drop;
}

/**
 * RTE_TABLE_ACTION_STATS
 */
static int
stats_cfg_check(struct rte_table_action_stats_config *stats)
{
	if ((stats->n_packets_enabled == 0) && (stats->n_bytes_enabled == 0))
		return -EINVAL;

	return 0;
}

struct stats_data {
	uint64_t n_packets;
	uint64_t n_bytes;
} __attribute__((__packed__));

static int
stats_apply(struct stats_data *data,
	struct rte_table_action_stats_params *p)
{
	data->n_packets = p->n_packets;
	data->n_bytes = p->n_bytes;

	return 0;
}

static __rte_always_inline void
pkt_work_stats(struct stats_data *data,
	uint16_t total_length)
{
	data->n_packets++;
	data->n_bytes += total_length;
}

/**
 * RTE_TABLE_ACTION_TIME
 */
struct time_data {
	uint64_t time;
} __attribute__((__packed__));

static int
time_apply(struct time_data *data,
	struct rte_table_action_time_params *p)
{
	data->time = p->time;
	return 0;
}

static __rte_always_inline void
pkt_work_time(struct time_data *data,
	uint64_t time)
{
	data->time = time;
}


/**
 * RTE_TABLE_ACTION_CRYPTO
 */

#define CRYPTO_OP_MASK_CIPHER	0x1
#define CRYPTO_OP_MASK_AUTH	0x2
#define CRYPTO_OP_MASK_AEAD	0x4

struct crypto_op_sym_iv_aad {
	struct rte_crypto_op op;
	struct rte_crypto_sym_op sym_op;
	union {
		struct {
			uint8_t cipher_iv[
				RTE_TABLE_ACTION_SYM_CRYPTO_IV_SIZE_MAX];
			uint8_t auth_iv[
				RTE_TABLE_ACTION_SYM_CRYPTO_IV_SIZE_MAX];
		} cipher_auth;

		struct {
			uint8_t iv[RTE_TABLE_ACTION_SYM_CRYPTO_IV_SIZE_MAX];
			uint8_t aad[RTE_TABLE_ACTION_SYM_CRYPTO_AAD_SIZE_MAX];
		} aead_iv_aad;

	} iv_aad;
};

struct sym_crypto_data {

	union {
		struct {

			/** Length of cipher iv. */
			uint16_t cipher_iv_len;

			/** Offset from start of IP header to the cipher iv. */
			uint16_t cipher_iv_data_offset;

			/** Length of cipher iv to be updated in the mbuf. */
			uint16_t cipher_iv_update_len;

			/** Offset from start of IP header to the auth iv. */
			uint16_t auth_iv_data_offset;

			/** Length of auth iv in the mbuf. */
			uint16_t auth_iv_len;

			/** Length of auth iv to be updated in the mbuf. */
			uint16_t auth_iv_update_len;

		} cipher_auth;
		struct {

			/** Length of iv. */
			uint16_t iv_len;

			/** Offset from start of IP header to the aead iv. */
			uint16_t iv_data_offset;

			/** Length of iv to be updated in the mbuf. */
			uint16_t iv_update_len;

			/** Length of aad */
			uint16_t aad_len;

			/** Offset from start of IP header to the aad. */
			uint16_t aad_data_offset;

			/** Length of aad to updated in the mbuf. */
			uint16_t aad_update_len;

		} aead;
	};

	/** Offset from start of IP header to the data. */
	uint16_t data_offset;

	/** Digest length. */
	uint16_t digest_len;

	/** block size */
	uint16_t block_size;

	/** Mask of crypto operation */
	uint16_t op_mask;

	/** Session pointer. */
	struct rte_cryptodev_sym_session *session;

	/** Direction of crypto, encrypt or decrypt */
	uint16_t direction;

	/** Private data size to store cipher iv / aad. */
	uint8_t iv_aad_data[32];

} __attribute__((__packed__));

static int
sym_crypto_cfg_check(struct rte_table_action_sym_crypto_config *cfg)
{
	if (!rte_cryptodev_pmd_is_valid_dev(cfg->cryptodev_id))
		return -EINVAL;
	if (cfg->mp_create == NULL || cfg->mp_init == NULL)
		return -EINVAL;

	return 0;
}

static int
get_block_size(const struct rte_crypto_sym_xform *xform, uint8_t cdev_id)
{
	struct rte_cryptodev_info dev_info;
	const struct rte_cryptodev_capabilities *cap;
	uint32_t i;

	rte_cryptodev_info_get(cdev_id, &dev_info);

	for (i = 0;; i++) {
		cap = &dev_info.capabilities[i];
		if (!cap)
			break;

		if (cap->sym.xform_type != xform->type)
			continue;

		if ((xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) &&
				(cap->sym.cipher.algo == xform->cipher.algo))
			return cap->sym.cipher.block_size;

		if ((xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) &&
				(cap->sym.aead.algo == xform->aead.algo))
			return cap->sym.aead.block_size;

		if (xform->type == RTE_CRYPTO_SYM_XFORM_NOT_SPECIFIED)
			break;
	}

	return -1;
}

static int
sym_crypto_apply(struct sym_crypto_data *data,
	struct rte_table_action_sym_crypto_config *cfg,
	struct rte_table_action_sym_crypto_params *p)
{
	const struct rte_crypto_cipher_xform *cipher_xform = NULL;
	const struct rte_crypto_auth_xform *auth_xform = NULL;
	const struct rte_crypto_aead_xform *aead_xform = NULL;
	struct rte_crypto_sym_xform *xform = p->xform;
	struct rte_cryptodev_sym_session *session;
	int ret;

	memset(data, 0, sizeof(*data));

	while (xform) {
		if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
			cipher_xform = &xform->cipher;

			if (cipher_xform->iv.length >
				RTE_TABLE_ACTION_SYM_CRYPTO_IV_SIZE_MAX)
				return -ENOMEM;
			if (cipher_xform->iv.offset !=
					RTE_TABLE_ACTION_SYM_CRYPTO_IV_OFFSET)
				return -EINVAL;

			ret = get_block_size(xform, cfg->cryptodev_id);
			if (ret < 0)
				return -1;
			data->block_size = (uint16_t)ret;
			data->op_mask |= CRYPTO_OP_MASK_CIPHER;

			data->cipher_auth.cipher_iv_len =
					cipher_xform->iv.length;
			data->cipher_auth.cipher_iv_data_offset = (uint16_t)
					p->cipher_auth.cipher_iv_update.offset;
			data->cipher_auth.cipher_iv_update_len = (uint16_t)
					p->cipher_auth.cipher_iv_update.length;

			rte_memcpy(data->iv_aad_data,
					p->cipher_auth.cipher_iv.val,
					p->cipher_auth.cipher_iv.length);

			data->direction = cipher_xform->op;

		} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
			auth_xform = &xform->auth;
			if (auth_xform->iv.length >
				RTE_TABLE_ACTION_SYM_CRYPTO_IV_SIZE_MAX)
				return -ENOMEM;
			data->op_mask |= CRYPTO_OP_MASK_AUTH;

			data->cipher_auth.auth_iv_len = auth_xform->iv.length;
			data->cipher_auth.auth_iv_data_offset = (uint16_t)
					p->cipher_auth.auth_iv_update.offset;
			data->cipher_auth.auth_iv_update_len = (uint16_t)
					p->cipher_auth.auth_iv_update.length;
			data->digest_len = auth_xform->digest_length;

			data->direction = (auth_xform->op ==
					RTE_CRYPTO_AUTH_OP_GENERATE) ?
					RTE_CRYPTO_CIPHER_OP_ENCRYPT :
					RTE_CRYPTO_CIPHER_OP_DECRYPT;

		} else if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
			aead_xform = &xform->aead;

			if ((aead_xform->iv.length >
				RTE_TABLE_ACTION_SYM_CRYPTO_IV_SIZE_MAX) || (
				aead_xform->aad_length >
				RTE_TABLE_ACTION_SYM_CRYPTO_AAD_SIZE_MAX))
				return -EINVAL;
			if (aead_xform->iv.offset !=
					RTE_TABLE_ACTION_SYM_CRYPTO_IV_OFFSET)
				return -EINVAL;

			ret = get_block_size(xform, cfg->cryptodev_id);
			if (ret < 0)
				return -1;
			data->block_size = (uint16_t)ret;
			data->op_mask |= CRYPTO_OP_MASK_AEAD;

			data->digest_len = aead_xform->digest_length;
			data->aead.iv_len = aead_xform->iv.length;
			data->aead.aad_len = aead_xform->aad_length;

			data->aead.iv_data_offset = (uint16_t)
					p->aead.iv_update.offset;
			data->aead.iv_update_len = (uint16_t)
					p->aead.iv_update.length;
			data->aead.aad_data_offset = (uint16_t)
					p->aead.aad_update.offset;
			data->aead.aad_update_len = (uint16_t)
					p->aead.aad_update.length;

			rte_memcpy(data->iv_aad_data,
					p->aead.iv.val,
					p->aead.iv.length);

			rte_memcpy(data->iv_aad_data + p->aead.iv.length,
					p->aead.aad.val,
					p->aead.aad.length);

			data->direction = (aead_xform->op ==
					RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
					RTE_CRYPTO_CIPHER_OP_ENCRYPT :
					RTE_CRYPTO_CIPHER_OP_DECRYPT;
		} else
			return -EINVAL;

		xform = xform->next;
	}

	if (auth_xform && auth_xform->iv.length) {
		if (cipher_xform) {
			if (auth_xform->iv.offset !=
					RTE_TABLE_ACTION_SYM_CRYPTO_IV_OFFSET +
					cipher_xform->iv.length)
				return -EINVAL;

			rte_memcpy(data->iv_aad_data + cipher_xform->iv.length,
					p->cipher_auth.auth_iv.val,
					p->cipher_auth.auth_iv.length);
		} else {
			rte_memcpy(data->iv_aad_data,
					p->cipher_auth.auth_iv.val,
					p->cipher_auth.auth_iv.length);
		}
	}

	session = rte_cryptodev_sym_session_create(cfg->mp_create);
	if (!session)
		return -ENOMEM;

	ret = rte_cryptodev_sym_session_init(cfg->cryptodev_id, session,
			p->xform, cfg->mp_init);
	if (ret < 0) {
		rte_cryptodev_sym_session_free(session);
		return ret;
	}

	data->data_offset = (uint16_t)p->data_offset;
	data->session = session;

	return 0;
}

static __rte_always_inline uint64_t
pkt_work_sym_crypto(struct rte_mbuf *mbuf, struct sym_crypto_data *data,
		struct rte_table_action_sym_crypto_config *cfg,
		uint16_t ip_offset)
{
	struct crypto_op_sym_iv_aad *crypto_op = (struct crypto_op_sym_iv_aad *)
			RTE_MBUF_METADATA_UINT8_PTR(mbuf, cfg->op_offset);
	struct rte_crypto_op *op = &crypto_op->op;
	struct rte_crypto_sym_op *sym = op->sym;
	uint32_t pkt_offset = sizeof(*mbuf) + mbuf->data_off;
	uint32_t payload_len = pkt_offset + mbuf->data_len - data->data_offset;

	op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
	op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
	op->phys_addr = mbuf->buf_iova + cfg->op_offset - sizeof(*mbuf);
	op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
	sym->m_src = mbuf;
	sym->m_dst = NULL;
	sym->session = data->session;

	/** pad the packet */
	if (data->direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
		uint32_t append_len = RTE_ALIGN_CEIL(payload_len,
				data->block_size) - payload_len;

		if (unlikely(rte_pktmbuf_append(mbuf, append_len +
				data->digest_len) == NULL))
			return 1;

		payload_len += append_len;
	} else
		payload_len -= data->digest_len;

	if (data->op_mask & CRYPTO_OP_MASK_CIPHER) {
		/** prepare cipher op */
		uint8_t *iv = crypto_op->iv_aad.cipher_auth.cipher_iv;

		sym->cipher.data.length = payload_len;
		sym->cipher.data.offset = data->data_offset - pkt_offset;

		if (data->cipher_auth.cipher_iv_update_len) {
			uint8_t *pkt_iv = RTE_MBUF_METADATA_UINT8_PTR(mbuf,
				data->cipher_auth.cipher_iv_data_offset
				+ ip_offset);

			/** For encryption, update the pkt iv field, otherwise
			 *  update the iv_aad_field
			 **/
			if (data->direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
				rte_memcpy(pkt_iv, data->iv_aad_data,
					data->cipher_auth.cipher_iv_update_len);
			else
				rte_memcpy(data->iv_aad_data, pkt_iv,
					data->cipher_auth.cipher_iv_update_len);
		}

		/** write iv */
		rte_memcpy(iv, data->iv_aad_data,
				data->cipher_auth.cipher_iv_len);
	}

	if (data->op_mask & CRYPTO_OP_MASK_AUTH) {
		/** authentication always start from IP header. */
		sym->auth.data.offset = ip_offset - pkt_offset;
		sym->auth.data.length = mbuf->data_len - sym->auth.data.offset -
				data->digest_len;
		sym->auth.digest.data = rte_pktmbuf_mtod_offset(mbuf,
				uint8_t *, rte_pktmbuf_pkt_len(mbuf) -
				data->digest_len);
		sym->auth.digest.phys_addr = rte_pktmbuf_iova_offset(mbuf,
				rte_pktmbuf_pkt_len(mbuf) - data->digest_len);

		if (data->cipher_auth.auth_iv_update_len) {
			uint8_t *pkt_iv = RTE_MBUF_METADATA_UINT8_PTR(mbuf,
					data->cipher_auth.auth_iv_data_offset
					+ ip_offset);
			uint8_t *data_iv = data->iv_aad_data +
					data->cipher_auth.cipher_iv_len;

			if (data->direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
				rte_memcpy(pkt_iv, data_iv,
					data->cipher_auth.auth_iv_update_len);
			else
				rte_memcpy(data_iv, pkt_iv,
					data->cipher_auth.auth_iv_update_len);
		}

		if (data->cipher_auth.auth_iv_len) {
			/** prepare cipher op */
			uint8_t *iv = crypto_op->iv_aad.cipher_auth.auth_iv;

			rte_memcpy(iv, data->iv_aad_data +
					data->cipher_auth.cipher_iv_len,
					data->cipher_auth.auth_iv_len);
		}
	}

	if (data->op_mask & CRYPTO_OP_MASK_AEAD) {
		uint8_t *iv = crypto_op->iv_aad.aead_iv_aad.iv;
		uint8_t *aad = crypto_op->iv_aad.aead_iv_aad.aad;

		sym->aead.aad.data = aad;
		sym->aead.aad.phys_addr = rte_pktmbuf_iova_offset(mbuf,
				aad - rte_pktmbuf_mtod(mbuf, uint8_t *));
		sym->aead.digest.data = rte_pktmbuf_mtod_offset(mbuf,
				uint8_t *, rte_pktmbuf_pkt_len(mbuf) -
				data->digest_len);
		sym->aead.digest.phys_addr = rte_pktmbuf_iova_offset(mbuf,
				rte_pktmbuf_pkt_len(mbuf) - data->digest_len);
		sym->aead.data.offset = data->data_offset - pkt_offset;
		sym->aead.data.length = payload_len;

		if (data->aead.iv_update_len) {
			uint8_t *pkt_iv = RTE_MBUF_METADATA_UINT8_PTR(mbuf,
					data->aead.iv_data_offset + ip_offset);
			uint8_t *data_iv = data->iv_aad_data;

			if (data->direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
				rte_memcpy(pkt_iv, data_iv,
						data->aead.iv_update_len);
			else
				rte_memcpy(data_iv, pkt_iv,
					data->aead.iv_update_len);
		}

		rte_memcpy(iv, data->iv_aad_data, data->aead.iv_len);

		if (data->aead.aad_update_len) {
			uint8_t *pkt_aad = RTE_MBUF_METADATA_UINT8_PTR(mbuf,
					data->aead.aad_data_offset + ip_offset);
			uint8_t *data_aad = data->iv_aad_data +
					data->aead.iv_len;

			if (data->direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
				rte_memcpy(pkt_aad, data_aad,
						data->aead.iv_update_len);
			else
				rte_memcpy(data_aad, pkt_aad,
					data->aead.iv_update_len);
		}

		rte_memcpy(aad, data->iv_aad_data + data->aead.iv_len,
					data->aead.aad_len);
	}

	return 0;
}

/**
 * RTE_TABLE_ACTION_TAG
 */
struct tag_data {
	uint32_t tag;
} __attribute__((__packed__));

static int
tag_apply(struct tag_data *data,
	struct rte_table_action_tag_params *p)
{
	data->tag = p->tag;
	return 0;
}

static __rte_always_inline void
pkt_work_tag(struct rte_mbuf *mbuf,
	struct tag_data *data)
{
	mbuf->hash.fdir.hi = data->tag;
	mbuf->ol_flags |= PKT_RX_FDIR | PKT_RX_FDIR_ID;
}

static __rte_always_inline void
pkt4_work_tag(struct rte_mbuf *mbuf0,
	struct rte_mbuf *mbuf1,
	struct rte_mbuf *mbuf2,
	struct rte_mbuf *mbuf3,
	struct tag_data *data0,
	struct tag_data *data1,
	struct tag_data *data2,
	struct tag_data *data3)
{
	mbuf0->hash.fdir.hi = data0->tag;
	mbuf1->hash.fdir.hi = data1->tag;
	mbuf2->hash.fdir.hi = data2->tag;
	mbuf3->hash.fdir.hi = data3->tag;

	mbuf0->ol_flags |= PKT_RX_FDIR | PKT_RX_FDIR_ID;
	mbuf1->ol_flags |= PKT_RX_FDIR | PKT_RX_FDIR_ID;
	mbuf2->ol_flags |= PKT_RX_FDIR | PKT_RX_FDIR_ID;
	mbuf3->ol_flags |= PKT_RX_FDIR | PKT_RX_FDIR_ID;
}

/**
 * RTE_TABLE_ACTION_DECAP
 */
struct decap_data {
	uint16_t n;
} __attribute__((__packed__));

static int
decap_apply(struct decap_data *data,
	struct rte_table_action_decap_params *p)
{
	data->n = p->n;
	return 0;
}

static __rte_always_inline void
pkt_work_decap(struct rte_mbuf *mbuf,
	struct decap_data *data)
{
	uint16_t data_off = mbuf->data_off;
	uint16_t data_len = mbuf->data_len;
	uint32_t pkt_len = mbuf->pkt_len;
	uint16_t n = data->n;

	mbuf->data_off = data_off + n;
	mbuf->data_len = data_len - n;
	mbuf->pkt_len = pkt_len - n;
}

static __rte_always_inline void
pkt4_work_decap(struct rte_mbuf *mbuf0,
	struct rte_mbuf *mbuf1,
	struct rte_mbuf *mbuf2,
	struct rte_mbuf *mbuf3,
	struct decap_data *data0,
	struct decap_data *data1,
	struct decap_data *data2,
	struct decap_data *data3)
{
	uint16_t data_off0 = mbuf0->data_off;
	uint16_t data_len0 = mbuf0->data_len;
	uint32_t pkt_len0 = mbuf0->pkt_len;

	uint16_t data_off1 = mbuf1->data_off;
	uint16_t data_len1 = mbuf1->data_len;
	uint32_t pkt_len1 = mbuf1->pkt_len;

	uint16_t data_off2 = mbuf2->data_off;
	uint16_t data_len2 = mbuf2->data_len;
	uint32_t pkt_len2 = mbuf2->pkt_len;

	uint16_t data_off3 = mbuf3->data_off;
	uint16_t data_len3 = mbuf3->data_len;
	uint32_t pkt_len3 = mbuf3->pkt_len;

	uint16_t n0 = data0->n;
	uint16_t n1 = data1->n;
	uint16_t n2 = data2->n;
	uint16_t n3 = data3->n;

	mbuf0->data_off = data_off0 + n0;
	mbuf0->data_len = data_len0 - n0;
	mbuf0->pkt_len = pkt_len0 - n0;

	mbuf1->data_off = data_off1 + n1;
	mbuf1->data_len = data_len1 - n1;
	mbuf1->pkt_len = pkt_len1 - n1;

	mbuf2->data_off = data_off2 + n2;
	mbuf2->data_len = data_len2 - n2;
	mbuf2->pkt_len = pkt_len2 - n2;

	mbuf3->data_off = data_off3 + n3;
	mbuf3->data_len = data_len3 - n3;
	mbuf3->pkt_len = pkt_len3 - n3;
}

/**
 * Action profile
 */
static int
action_valid(enum rte_table_action_type action)
{
	switch (action) {
	case RTE_TABLE_ACTION_FWD:
	case RTE_TABLE_ACTION_LB:
	case RTE_TABLE_ACTION_MTR:
	case RTE_TABLE_ACTION_TM:
	case RTE_TABLE_ACTION_ENCAP:
	case RTE_TABLE_ACTION_NAT:
	case RTE_TABLE_ACTION_TTL:
	case RTE_TABLE_ACTION_STATS:
	case RTE_TABLE_ACTION_TIME:
	case RTE_TABLE_ACTION_SYM_CRYPTO:
	case RTE_TABLE_ACTION_TAG:
	case RTE_TABLE_ACTION_DECAP:
		return 1;
	default:
		return 0;
	}
}


#define RTE_TABLE_ACTION_MAX                      64

struct ap_config {
	uint64_t action_mask;
	struct rte_table_action_common_config common;
	struct rte_table_action_lb_config lb;
	struct rte_table_action_mtr_config mtr;
	struct rte_table_action_tm_config tm;
	struct rte_table_action_encap_config encap;
	struct rte_table_action_nat_config nat;
	struct rte_table_action_ttl_config ttl;
	struct rte_table_action_stats_config stats;
	struct rte_table_action_sym_crypto_config sym_crypto;
};

static size_t
action_cfg_size(enum rte_table_action_type action)
{
	switch (action) {
	case RTE_TABLE_ACTION_LB:
		return sizeof(struct rte_table_action_lb_config);
	case RTE_TABLE_ACTION_MTR:
		return sizeof(struct rte_table_action_mtr_config);
	case RTE_TABLE_ACTION_TM:
		return sizeof(struct rte_table_action_tm_config);
	case RTE_TABLE_ACTION_ENCAP:
		return sizeof(struct rte_table_action_encap_config);
	case RTE_TABLE_ACTION_NAT:
		return sizeof(struct rte_table_action_nat_config);
	case RTE_TABLE_ACTION_TTL:
		return sizeof(struct rte_table_action_ttl_config);
	case RTE_TABLE_ACTION_STATS:
		return sizeof(struct rte_table_action_stats_config);
	case RTE_TABLE_ACTION_SYM_CRYPTO:
		return sizeof(struct rte_table_action_sym_crypto_config);
	default:
		return 0;
	}
}

static void*
action_cfg_get(struct ap_config *ap_config,
	enum rte_table_action_type type)
{
	switch (type) {
	case RTE_TABLE_ACTION_LB:
		return &ap_config->lb;

	case RTE_TABLE_ACTION_MTR:
		return &ap_config->mtr;

	case RTE_TABLE_ACTION_TM:
		return &ap_config->tm;

	case RTE_TABLE_ACTION_ENCAP:
		return &ap_config->encap;

	case RTE_TABLE_ACTION_NAT:
		return &ap_config->nat;

	case RTE_TABLE_ACTION_TTL:
		return &ap_config->ttl;

	case RTE_TABLE_ACTION_STATS:
		return &ap_config->stats;

	case RTE_TABLE_ACTION_SYM_CRYPTO:
		return &ap_config->sym_crypto;
	default:
		return NULL;
	}
}

static void
action_cfg_set(struct ap_config *ap_config,
	enum rte_table_action_type type,
	void *action_cfg)
{
	void *dst = action_cfg_get(ap_config, type);

	if (dst)
		memcpy(dst, action_cfg, action_cfg_size(type));

	ap_config->action_mask |= 1LLU << type;
}

struct ap_data {
	size_t offset[RTE_TABLE_ACTION_MAX];
	size_t total_size;
};

static size_t
action_data_size(enum rte_table_action_type action,
	struct ap_config *ap_config)
{
	switch (action) {
	case RTE_TABLE_ACTION_FWD:
		return sizeof(struct fwd_data);

	case RTE_TABLE_ACTION_LB:
		return sizeof(struct lb_data);

	case RTE_TABLE_ACTION_MTR:
		return mtr_data_size(&ap_config->mtr);

	case RTE_TABLE_ACTION_TM:
		return sizeof(struct tm_data);

	case RTE_TABLE_ACTION_ENCAP:
		return encap_data_size(&ap_config->encap);

	case RTE_TABLE_ACTION_NAT:
		return nat_data_size(&ap_config->nat,
			&ap_config->common);

	case RTE_TABLE_ACTION_TTL:
		return sizeof(struct ttl_data);

	case RTE_TABLE_ACTION_STATS:
		return sizeof(struct stats_data);

	case RTE_TABLE_ACTION_TIME:
		return sizeof(struct time_data);

	case RTE_TABLE_ACTION_SYM_CRYPTO:
		return (sizeof(struct sym_crypto_data));

	case RTE_TABLE_ACTION_TAG:
		return sizeof(struct tag_data);

	case RTE_TABLE_ACTION_DECAP:
		return sizeof(struct decap_data);

	default:
		return 0;
	}
}


static void
action_data_offset_set(struct ap_data *ap_data,
	struct ap_config *ap_config)
{
	uint64_t action_mask = ap_config->action_mask;
	size_t offset;
	uint32_t action;

	memset(ap_data->offset, 0, sizeof(ap_data->offset));

	offset = 0;
	for (action = 0; action < RTE_TABLE_ACTION_MAX; action++)
		if (action_mask & (1LLU << action)) {
			ap_data->offset[action] = offset;
			offset += action_data_size((enum rte_table_action_type)action,
				ap_config);
		}

	ap_data->total_size = offset;
}

struct rte_table_action_profile {
	struct ap_config cfg;
	struct ap_data data;
	int frozen;
};

struct rte_table_action_profile *
rte_table_action_profile_create(struct rte_table_action_common_config *common)
{
	struct rte_table_action_profile *ap;

	/* Check input arguments */
	if (common == NULL)
		return NULL;

	/* Memory allocation */
	ap = calloc(1, sizeof(struct rte_table_action_profile));
	if (ap == NULL)
		return NULL;

	/* Initialization */
	memcpy(&ap->cfg.common, common, sizeof(*common));

	return ap;
}


int
rte_table_action_profile_action_register(struct rte_table_action_profile *profile,
	enum rte_table_action_type type,
	void *action_config)
{
	int status;

	/* Check input arguments */
	if ((profile == NULL) ||
		profile->frozen ||
		(action_valid(type) == 0) ||
		(profile->cfg.action_mask & (1LLU << type)) ||
		((action_cfg_size(type) == 0) && action_config) ||
		(action_cfg_size(type) && (action_config == NULL)))
		return -EINVAL;

	switch (type) {
	case RTE_TABLE_ACTION_LB:
		status = lb_cfg_check(action_config);
		break;

	case RTE_TABLE_ACTION_MTR:
		status = mtr_cfg_check(action_config);
		break;

	case RTE_TABLE_ACTION_TM:
		status = tm_cfg_check(action_config);
		break;

	case RTE_TABLE_ACTION_ENCAP:
		status = encap_cfg_check(action_config);
		break;

	case RTE_TABLE_ACTION_NAT:
		status = nat_cfg_check(action_config);
		break;

	case RTE_TABLE_ACTION_TTL:
		status = ttl_cfg_check(action_config);
		break;

	case RTE_TABLE_ACTION_STATS:
		status = stats_cfg_check(action_config);
		break;

	case RTE_TABLE_ACTION_SYM_CRYPTO:
		status = sym_crypto_cfg_check(action_config);
		break;

	default:
		status = 0;
		break;
	}

	if (status)
		return status;

	/* Action enable */
	action_cfg_set(&profile->cfg, type, action_config);

	return 0;
}

int
rte_table_action_profile_freeze(struct rte_table_action_profile *profile)
{
	if (profile->frozen)
		return -EBUSY;

	profile->cfg.action_mask |= 1LLU << RTE_TABLE_ACTION_FWD;
	action_data_offset_set(&profile->data, &profile->cfg);
	profile->frozen = 1;

	return 0;
}

int
rte_table_action_profile_free(struct rte_table_action_profile *profile)
{
	if (profile == NULL)
		return 0;

	free(profile);
	return 0;
}

/**
 * Action
 */
#define METER_PROFILES_MAX                                 32

struct rte_table_action {
	struct ap_config cfg;
	struct ap_data data;
	struct dscp_table_data dscp_table;
	struct meter_profile_data mp[METER_PROFILES_MAX];
};

struct rte_table_action *
rte_table_action_create(struct rte_table_action_profile *profile,
	uint32_t socket_id)
{
	struct rte_table_action *action;

	/* Check input arguments */
	if ((profile == NULL) ||
		(profile->frozen == 0))
		return NULL;

	/* Memory allocation */
	action = rte_zmalloc_socket(NULL,
		sizeof(struct rte_table_action),
		RTE_CACHE_LINE_SIZE,
		socket_id);
	if (action == NULL)
		return NULL;

	/* Initialization */
	memcpy(&action->cfg, &profile->cfg, sizeof(profile->cfg));
	memcpy(&action->data, &profile->data, sizeof(profile->data));

	return action;
}

static __rte_always_inline void *
action_data_get(void *data,
	struct rte_table_action *action,
	enum rte_table_action_type type)
{
	size_t offset = action->data.offset[type];
	uint8_t *data_bytes = data;

	return &data_bytes[offset];
}

int
rte_table_action_apply(struct rte_table_action *action,
	void *data,
	enum rte_table_action_type type,
	void *action_params)
{
	void *action_data;

	/* Check input arguments */
	if ((action == NULL) ||
		(data == NULL) ||
		(action_valid(type) == 0) ||
		((action->cfg.action_mask & (1LLU << type)) == 0) ||
		(action_params == NULL))
		return -EINVAL;

	/* Data update */
	action_data = action_data_get(data, action, type);

	switch (type) {
	case RTE_TABLE_ACTION_FWD:
		return fwd_apply(action_data,
			action_params);

	case RTE_TABLE_ACTION_LB:
		return lb_apply(action_data,
			action_params);

	case RTE_TABLE_ACTION_MTR:
		return mtr_apply(action_data,
			action_params,
			&action->cfg.mtr,
			action->mp,
			RTE_DIM(action->mp));

	case RTE_TABLE_ACTION_TM:
		return tm_apply(action_data,
			action_params,
			&action->cfg.tm);

	case RTE_TABLE_ACTION_ENCAP:
		return encap_apply(action_data,
			action_params,
			&action->cfg.encap,
			&action->cfg.common);

	case RTE_TABLE_ACTION_NAT:
		return nat_apply(action_data,
			action_params,
			&action->cfg.common);

	case RTE_TABLE_ACTION_TTL:
		return ttl_apply(action_data,
			action_params);

	case RTE_TABLE_ACTION_STATS:
		return stats_apply(action_data,
			action_params);

	case RTE_TABLE_ACTION_TIME:
		return time_apply(action_data,
			action_params);

	case RTE_TABLE_ACTION_SYM_CRYPTO:
		return sym_crypto_apply(action_data,
				&action->cfg.sym_crypto,
				action_params);

	case RTE_TABLE_ACTION_TAG:
		return tag_apply(action_data,
			action_params);

	case RTE_TABLE_ACTION_DECAP:
		return decap_apply(action_data,
			action_params);

	default:
		return -EINVAL;
	}
}

int
rte_table_action_dscp_table_update(struct rte_table_action *action,
	uint64_t dscp_mask,
	struct rte_table_action_dscp_table *table)
{
	uint32_t i;

	/* Check input arguments */
	if ((action == NULL) ||
		((action->cfg.action_mask & ((1LLU << RTE_TABLE_ACTION_MTR) |
		(1LLU << RTE_TABLE_ACTION_TM))) == 0) ||
		(dscp_mask == 0) ||
		(table == NULL))
		return -EINVAL;

	for (i = 0; i < RTE_DIM(table->entry); i++) {
		struct dscp_table_entry_data *data =
			&action->dscp_table.entry[i];
		struct rte_table_action_dscp_table_entry *entry =
			&table->entry[i];
		uint16_t queue_tc_color =
			MBUF_SCHED_QUEUE_TC_COLOR(entry->tc_queue_id,
				entry->tc_id,
				entry->color);

		if ((dscp_mask & (1LLU << i)) == 0)
			continue;

		data->color = entry->color;
		data->tc = entry->tc_id;
		data->queue_tc_color = queue_tc_color;
	}

	return 0;
}

int
rte_table_action_meter_profile_add(struct rte_table_action *action,
	uint32_t meter_profile_id,
	struct rte_table_action_meter_profile *profile)
{
	struct meter_profile_data *mp_data;
	uint32_t status;

	/* Check input arguments */
	if ((action == NULL) ||
		((action->cfg.action_mask & (1LLU << RTE_TABLE_ACTION_MTR)) == 0) ||
		(profile == NULL))
		return -EINVAL;

	if (profile->alg != RTE_TABLE_ACTION_METER_TRTCM)
		return -ENOTSUP;

	mp_data = meter_profile_data_find(action->mp,
		RTE_DIM(action->mp),
		meter_profile_id);
	if (mp_data)
		return -EEXIST;

	mp_data = meter_profile_data_find_unused(action->mp,
		RTE_DIM(action->mp));
	if (!mp_data)
		return -ENOSPC;

	/* Install new profile */
	status = rte_meter_trtcm_profile_config(&mp_data->profile,
		&profile->trtcm);
	if (status)
		return status;

	mp_data->profile_id = meter_profile_id;
	mp_data->valid = 1;

	return 0;
}

int
rte_table_action_meter_profile_delete(struct rte_table_action *action,
	uint32_t meter_profile_id)
{
	struct meter_profile_data *mp_data;

	/* Check input arguments */
	if ((action == NULL) ||
		((action->cfg.action_mask & (1LLU << RTE_TABLE_ACTION_MTR)) == 0))
		return -EINVAL;

	mp_data = meter_profile_data_find(action->mp,
		RTE_DIM(action->mp),
		meter_profile_id);
	if (!mp_data)
		return 0;

	/* Uninstall profile */
	mp_data->valid = 0;

	return 0;
}

int
rte_table_action_meter_read(struct rte_table_action *action,
	void *data,
	uint32_t tc_mask,
	struct rte_table_action_mtr_counters *stats,
	int clear)
{
	struct mtr_trtcm_data *mtr_data;
	uint32_t i;

	/* Check input arguments */
	if ((action == NULL) ||
		((action->cfg.action_mask & (1LLU << RTE_TABLE_ACTION_MTR)) == 0) ||
		(data == NULL) ||
		(tc_mask > RTE_LEN2MASK(action->cfg.mtr.n_tc, uint32_t)))
		return -EINVAL;

	mtr_data = action_data_get(data, action, RTE_TABLE_ACTION_MTR);

	/* Read */
	if (stats) {
		for (i = 0; i < RTE_TABLE_ACTION_TC_MAX; i++) {
			struct rte_table_action_mtr_counters_tc *dst =
				&stats->stats[i];
			struct mtr_trtcm_data *src = &mtr_data[i];

			if ((tc_mask & (1 << i)) == 0)
				continue;

			dst->n_packets[e_RTE_METER_GREEN] =
				mtr_trtcm_data_stats_get(src, e_RTE_METER_GREEN);

			dst->n_packets[e_RTE_METER_YELLOW] =
				mtr_trtcm_data_stats_get(src, e_RTE_METER_YELLOW);

			dst->n_packets[e_RTE_METER_RED] =
				mtr_trtcm_data_stats_get(src, e_RTE_METER_RED);

			dst->n_packets_valid = 1;
			dst->n_bytes_valid = 0;
		}

		stats->tc_mask = tc_mask;
	}

	/* Clear */
	if (clear)
		for (i = 0; i < RTE_TABLE_ACTION_TC_MAX; i++) {
			struct mtr_trtcm_data *src = &mtr_data[i];

			if ((tc_mask & (1 << i)) == 0)
				continue;

			mtr_trtcm_data_stats_reset(src, e_RTE_METER_GREEN);
			mtr_trtcm_data_stats_reset(src, e_RTE_METER_YELLOW);
			mtr_trtcm_data_stats_reset(src, e_RTE_METER_RED);
		}


	return 0;
}

int
rte_table_action_ttl_read(struct rte_table_action *action,
	void *data,
	struct rte_table_action_ttl_counters *stats,
	int clear)
{
	struct ttl_data *ttl_data;

	/* Check input arguments */
	if ((action == NULL) ||
		((action->cfg.action_mask &
		(1LLU << RTE_TABLE_ACTION_TTL)) == 0) ||
		(data == NULL))
		return -EINVAL;

	ttl_data = action_data_get(data, action, RTE_TABLE_ACTION_TTL);

	/* Read */
	if (stats)
		stats->n_packets = TTL_STATS_READ(ttl_data);

	/* Clear */
	if (clear)
		TTL_STATS_RESET(ttl_data);

	return 0;
}

int
rte_table_action_stats_read(struct rte_table_action *action,
	void *data,
	struct rte_table_action_stats_counters *stats,
	int clear)
{
	struct stats_data *stats_data;

	/* Check input arguments */
	if ((action == NULL) ||
		((action->cfg.action_mask &
		(1LLU << RTE_TABLE_ACTION_STATS)) == 0) ||
		(data == NULL))
		return -EINVAL;

	stats_data = action_data_get(data, action,
		RTE_TABLE_ACTION_STATS);

	/* Read */
	if (stats) {
		stats->n_packets = stats_data->n_packets;
		stats->n_bytes = stats_data->n_bytes;
		stats->n_packets_valid = 1;
		stats->n_bytes_valid = 1;
	}

	/* Clear */
	if (clear) {
		stats_data->n_packets = 0;
		stats_data->n_bytes = 0;
	}

	return 0;
}

int
rte_table_action_time_read(struct rte_table_action *action,
	void *data,
	uint64_t *timestamp)
{
	struct time_data *time_data;

	/* Check input arguments */
	if ((action == NULL) ||
		((action->cfg.action_mask &
		(1LLU << RTE_TABLE_ACTION_TIME)) == 0) ||
		(data == NULL) ||
		(timestamp == NULL))
		return -EINVAL;

	time_data = action_data_get(data, action, RTE_TABLE_ACTION_TIME);

	/* Read */
	*timestamp = time_data->time;

	return 0;
}

struct rte_cryptodev_sym_session *
rte_table_action_crypto_sym_session_get(struct rte_table_action *action,
	void *data)
{
	struct sym_crypto_data *sym_crypto_data;

	/* Check input arguments */
	if ((action == NULL) ||
		((action->cfg.action_mask &
		(1LLU << RTE_TABLE_ACTION_SYM_CRYPTO)) == 0) ||
		(data == NULL))
		return NULL;

	sym_crypto_data = action_data_get(data, action,
			RTE_TABLE_ACTION_SYM_CRYPTO);

	return sym_crypto_data->session;
}

static __rte_always_inline uint64_t
pkt_work(struct rte_mbuf *mbuf,
	struct rte_pipeline_table_entry *table_entry,
	uint64_t time,
	struct rte_table_action *action,
	struct ap_config *cfg)
{
	uint64_t drop_mask = 0;

	uint32_t ip_offset = action->cfg.common.ip_offset;
	void *ip = RTE_MBUF_METADATA_UINT32_PTR(mbuf, ip_offset);

	uint32_t dscp;
	uint16_t total_length;

	if (cfg->common.ip_version) {
		struct ipv4_hdr *hdr = ip;

		dscp = hdr->type_of_service >> 2;
		total_length = rte_ntohs(hdr->total_length);
	} else {
		struct ipv6_hdr *hdr = ip;

		dscp = (rte_ntohl(hdr->vtc_flow) & 0x0F600000) >> 18;
		total_length =
			rte_ntohs(hdr->payload_len) + sizeof(struct ipv6_hdr);
	}

	if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_LB)) {
		void *data =
			action_data_get(table_entry, action, RTE_TABLE_ACTION_LB);

		pkt_work_lb(mbuf,
			data,
			&cfg->lb);
	}
	if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_MTR)) {
		void *data =
			action_data_get(table_entry, action, RTE_TABLE_ACTION_MTR);

		drop_mask |= pkt_work_mtr(mbuf,
			data,
			&action->dscp_table,
			action->mp,
			time,
			dscp,
			total_length);
	}

	if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_TM)) {
		void *data =
			action_data_get(table_entry, action, RTE_TABLE_ACTION_TM);

		pkt_work_tm(mbuf,
			data,
			&action->dscp_table,
			dscp);
	}

	if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_DECAP)) {
		void *data = action_data_get(table_entry,
			action,
			RTE_TABLE_ACTION_DECAP);

		pkt_work_decap(mbuf, data);
	}

	if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_ENCAP)) {
		void *data =
			action_data_get(table_entry, action, RTE_TABLE_ACTION_ENCAP);

		pkt_work_encap(mbuf,
			data,
			&cfg->encap,
			ip,
			total_length,
			ip_offset);
	}

	if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_NAT)) {
		void *data =
			action_data_get(table_entry, action, RTE_TABLE_ACTION_NAT);

		if (cfg->common.ip_version)
			pkt_ipv4_work_nat(ip, data, &cfg->nat);
		else
			pkt_ipv6_work_nat(ip, data, &cfg->nat);
	}

	if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_TTL)) {
		void *data =
			action_data_get(table_entry, action, RTE_TABLE_ACTION_TTL);

		if (cfg->common.ip_version)
			drop_mask |= pkt_ipv4_work_ttl(ip, data);
		else
			drop_mask |= pkt_ipv6_work_ttl(ip, data);
	}

	if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_STATS)) {
		void *data =
			action_data_get(table_entry, action, RTE_TABLE_ACTION_STATS);

		pkt_work_stats(data, total_length);
	}

	if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_TIME)) {
		void *data =
			action_data_get(table_entry, action, RTE_TABLE_ACTION_TIME);

		pkt_work_time(data, time);
	}

	if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_SYM_CRYPTO)) {
		void *data = action_data_get(table_entry, action,
				RTE_TABLE_ACTION_SYM_CRYPTO);

		drop_mask |= pkt_work_sym_crypto(mbuf, data, &cfg->sym_crypto,
				ip_offset);
	}

	if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_TAG)) {
		void *data = action_data_get(table_entry,
			action,
			RTE_TABLE_ACTION_TAG);

		pkt_work_tag(mbuf, data);
	}

	return drop_mask;
}

static __rte_always_inline uint64_t
pkt4_work(struct rte_mbuf **mbufs,
	struct rte_pipeline_table_entry **table_entries,
	uint64_t time,
	struct rte_table_action *action,
	struct ap_config *cfg)
{
	uint64_t drop_mask0 = 0;
	uint64_t drop_mask1 = 0;
	uint64_t drop_mask2 = 0;
	uint64_t drop_mask3 = 0;

	struct rte_mbuf *mbuf0 = mbufs[0];
	struct rte_mbuf *mbuf1 = mbufs[1];
	struct rte_mbuf *mbuf2 = mbufs[2];
	struct rte_mbuf *mbuf3 = mbufs[3];

	struct rte_pipeline_table_entry *table_entry0 = table_entries[0];
	struct rte_pipeline_table_entry *table_entry1 = table_entries[1];
	struct rte_pipeline_table_entry *table_entry2 = table_entries[2];
	struct rte_pipeline_table_entry *table_entry3 = table_entries[3];

	uint32_t ip_offset = action->cfg.common.ip_offset;
	void *ip0 = RTE_MBUF_METADATA_UINT32_PTR(mbuf0, ip_offset);
	void *ip1 = RTE_MBUF_METADATA_UINT32_PTR(mbuf1, ip_offset);
	void *ip2 = RTE_MBUF_METADATA_UINT32_PTR(mbuf2, ip_offset);
	void *ip3 = RTE_MBUF_METADATA_UINT32_PTR(mbuf3, ip_offset);

	uint32_t dscp0, dscp1, dscp2, dscp3;
	uint16_t total_length0, total_length1, total_length2, total_length3;

	if (cfg->common.ip_version) {
		struct ipv4_hdr *hdr0 = ip0;
		struct ipv4_hdr *hdr1 = ip1;
		struct ipv4_hdr *hdr2 = ip2;
		struct ipv4_hdr *hdr3 = ip3;

		dscp0 = hdr0->type_of_service >> 2;
		dscp1 = hdr1->type_of_service >> 2;
		dscp2 = hdr2->type_of_service >> 2;
		dscp3 = hdr3->type_of_service >> 2;

		total_length0 = rte_ntohs(hdr0->total_length);
		total_length1 = rte_ntohs(hdr1->total_length);
		total_length2 = rte_ntohs(hdr2->total_length);
		total_length3 = rte_ntohs(hdr3->total_length);
	} else {
		struct ipv6_hdr *hdr0 = ip0;
		struct ipv6_hdr *hdr1 = ip1;
		struct ipv6_hdr *hdr2 = ip2;
		struct ipv6_hdr *hdr3 = ip3;

		dscp0 = (rte_ntohl(hdr0->vtc_flow) & 0x0F600000) >> 18;
		dscp1 = (rte_ntohl(hdr1->vtc_flow) & 0x0F600000) >> 18;
		dscp2 = (rte_ntohl(hdr2->vtc_flow) & 0x0F600000) >> 18;
		dscp3 = (rte_ntohl(hdr3->vtc_flow) & 0x0F600000) >> 18;

		total_length0 =
			rte_ntohs(hdr0->payload_len) + sizeof(struct ipv6_hdr);
		total_length1 =
			rte_ntohs(hdr1->payload_len) + sizeof(struct ipv6_hdr);
		total_length2 =
			rte_ntohs(hdr2->payload_len) + sizeof(struct ipv6_hdr);
		total_length3 =
			rte_ntohs(hdr3->payload_len) + sizeof(struct ipv6_hdr);
	}

	if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_LB)) {
		void *data0 =
			action_data_get(table_entry0, action, RTE_TABLE_ACTION_LB);
		void *data1 =
			action_data_get(table_entry1, action, RTE_TABLE_ACTION_LB);
		void *data2 =
			action_data_get(table_entry2, action, RTE_TABLE_ACTION_LB);
		void *data3 =
			action_data_get(table_entry3, action, RTE_TABLE_ACTION_LB);

		pkt_work_lb(mbuf0,
			data0,
			&cfg->lb);

		pkt_work_lb(mbuf1,
			data1,
			&cfg->lb);

		pkt_work_lb(mbuf2,
			data2,
			&cfg->lb);

		pkt_work_lb(mbuf3,
			data3,
			&cfg->lb);
	}

	if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_MTR)) {
		void *data0 =
			action_data_get(table_entry0, action, RTE_TABLE_ACTION_MTR);
		void *data1 =
			action_data_get(table_entry1, action, RTE_TABLE_ACTION_MTR);
		void *data2 =
			action_data_get(table_entry2, action, RTE_TABLE_ACTION_MTR);
		void *data3 =
			action_data_get(table_entry3, action, RTE_TABLE_ACTION_MTR);

		drop_mask0 |= pkt_work_mtr(mbuf0,
			data0,
			&action->dscp_table,
			action->mp,
			time,
			dscp0,
			total_length0);

		drop_mask1 |= pkt_work_mtr(mbuf1,
			data1,
			&action->dscp_table,
			action->mp,
			time,
			dscp1,
			total_length1);

		drop_mask2 |= pkt_work_mtr(mbuf2,
			data2,
			&action->dscp_table,
			action->mp,
			time,
			dscp2,
			total_length2);

		drop_mask3 |= pkt_work_mtr(mbuf3,
			data3,
			&action->dscp_table,
			action->mp,
			time,
			dscp3,
			total_length3);
	}

	if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_TM)) {
		void *data0 =
			action_data_get(table_entry0, action, RTE_TABLE_ACTION_TM);
		void *data1 =
			action_data_get(table_entry1, action, RTE_TABLE_ACTION_TM);
		void *data2 =
			action_data_get(table_entry2, action, RTE_TABLE_ACTION_TM);
		void *data3 =
			action_data_get(table_entry3, action, RTE_TABLE_ACTION_TM);

		pkt_work_tm(mbuf0,
			data0,
			&action->dscp_table,
			dscp0);

		pkt_work_tm(mbuf1,
			data1,
			&action->dscp_table,
			dscp1);

		pkt_work_tm(mbuf2,
			data2,
			&action->dscp_table,
			dscp2);

		pkt_work_tm(mbuf3,
			data3,
			&action->dscp_table,
			dscp3);
	}

	if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_DECAP)) {
		void *data0 = action_data_get(table_entry0,
			action,
			RTE_TABLE_ACTION_DECAP);
		void *data1 = action_data_get(table_entry1,
			action,
			RTE_TABLE_ACTION_DECAP);
		void *data2 = action_data_get(table_entry2,
			action,
			RTE_TABLE_ACTION_DECAP);
		void *data3 = action_data_get(table_entry3,
			action,
			RTE_TABLE_ACTION_DECAP);

		pkt4_work_decap(mbuf0, mbuf1, mbuf2, mbuf3,
			data0, data1, data2, data3);
	}

	if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_ENCAP)) {
		void *data0 =
			action_data_get(table_entry0, action, RTE_TABLE_ACTION_ENCAP);
		void *data1 =
			action_data_get(table_entry1, action, RTE_TABLE_ACTION_ENCAP);
		void *data2 =
			action_data_get(table_entry2, action, RTE_TABLE_ACTION_ENCAP);
		void *data3 =
			action_data_get(table_entry3, action, RTE_TABLE_ACTION_ENCAP);

		pkt_work_encap(mbuf0,
			data0,
			&cfg->encap,
			ip0,
			total_length0,
			ip_offset);

		pkt_work_encap(mbuf1,
			data1,
			&cfg->encap,
			ip1,
			total_length1,
			ip_offset);

		pkt_work_encap(mbuf2,
			data2,
			&cfg->encap,
			ip2,
			total_length2,
			ip_offset);

		pkt_work_encap(mbuf3,
			data3,
			&cfg->encap,
			ip3,
			total_length3,
			ip_offset);
	}

	if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_NAT)) {
		void *data0 =
			action_data_get(table_entry0, action, RTE_TABLE_ACTION_NAT);
		void *data1 =
			action_data_get(table_entry1, action, RTE_TABLE_ACTION_NAT);
		void *data2 =
			action_data_get(table_entry2, action, RTE_TABLE_ACTION_NAT);
		void *data3 =
			action_data_get(table_entry3, action, RTE_TABLE_ACTION_NAT);

		if (cfg->common.ip_version) {
			pkt_ipv4_work_nat(ip0, data0, &cfg->nat);
			pkt_ipv4_work_nat(ip1, data1, &cfg->nat);
			pkt_ipv4_work_nat(ip2, data2, &cfg->nat);
			pkt_ipv4_work_nat(ip3, data3, &cfg->nat);
		} else {
			pkt_ipv6_work_nat(ip0, data0, &cfg->nat);
			pkt_ipv6_work_nat(ip1, data1, &cfg->nat);
			pkt_ipv6_work_nat(ip2, data2, &cfg->nat);
			pkt_ipv6_work_nat(ip3, data3, &cfg->nat);
		}
	}

	if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_TTL)) {
		void *data0 =
			action_data_get(table_entry0, action, RTE_TABLE_ACTION_TTL);
		void *data1 =
			action_data_get(table_entry1, action, RTE_TABLE_ACTION_TTL);
		void *data2 =
			action_data_get(table_entry2, action, RTE_TABLE_ACTION_TTL);
		void *data3 =
			action_data_get(table_entry3, action, RTE_TABLE_ACTION_TTL);

		if (cfg->common.ip_version) {
			drop_mask0 |= pkt_ipv4_work_ttl(ip0, data0);
			drop_mask1 |= pkt_ipv4_work_ttl(ip1, data1);
			drop_mask2 |= pkt_ipv4_work_ttl(ip2, data2);
			drop_mask3 |= pkt_ipv4_work_ttl(ip3, data3);
		} else {
			drop_mask0 |= pkt_ipv6_work_ttl(ip0, data0);
			drop_mask1 |= pkt_ipv6_work_ttl(ip1, data1);
			drop_mask2 |= pkt_ipv6_work_ttl(ip2, data2);
			drop_mask3 |= pkt_ipv6_work_ttl(ip3, data3);
		}
	}

	if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_STATS)) {
		void *data0 =
			action_data_get(table_entry0, action, RTE_TABLE_ACTION_STATS);
		void *data1 =
			action_data_get(table_entry1, action, RTE_TABLE_ACTION_STATS);
		void *data2 =
			action_data_get(table_entry2, action, RTE_TABLE_ACTION_STATS);
		void *data3 =
			action_data_get(table_entry3, action, RTE_TABLE_ACTION_STATS);

		pkt_work_stats(data0, total_length0);
		pkt_work_stats(data1, total_length1);
		pkt_work_stats(data2, total_length2);
		pkt_work_stats(data3, total_length3);
	}

	if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_TIME)) {
		void *data0 =
			action_data_get(table_entry0, action, RTE_TABLE_ACTION_TIME);
		void *data1 =
			action_data_get(table_entry1, action, RTE_TABLE_ACTION_TIME);
		void *data2 =
			action_data_get(table_entry2, action, RTE_TABLE_ACTION_TIME);
		void *data3 =
			action_data_get(table_entry3, action, RTE_TABLE_ACTION_TIME);

		pkt_work_time(data0, time);
		pkt_work_time(data1, time);
		pkt_work_time(data2, time);
		pkt_work_time(data3, time);
	}

	if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_SYM_CRYPTO)) {
		void *data0 = action_data_get(table_entry0, action,
				RTE_TABLE_ACTION_SYM_CRYPTO);
		void *data1 = action_data_get(table_entry1, action,
				RTE_TABLE_ACTION_SYM_CRYPTO);
		void *data2 = action_data_get(table_entry2, action,
				RTE_TABLE_ACTION_SYM_CRYPTO);
		void *data3 = action_data_get(table_entry3, action,
				RTE_TABLE_ACTION_SYM_CRYPTO);

		drop_mask0 |= pkt_work_sym_crypto(mbuf0, data0, &cfg->sym_crypto,
				ip_offset);
		drop_mask1 |= pkt_work_sym_crypto(mbuf1, data1, &cfg->sym_crypto,
				ip_offset);
		drop_mask2 |= pkt_work_sym_crypto(mbuf2, data2, &cfg->sym_crypto,
				ip_offset);
		drop_mask3 |= pkt_work_sym_crypto(mbuf3, data3, &cfg->sym_crypto,
				ip_offset);
	}

	if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_TAG)) {
		void *data0 = action_data_get(table_entry0,
			action,
			RTE_TABLE_ACTION_TAG);
		void *data1 = action_data_get(table_entry1,
			action,
			RTE_TABLE_ACTION_TAG);
		void *data2 = action_data_get(table_entry2,
			action,
			RTE_TABLE_ACTION_TAG);
		void *data3 = action_data_get(table_entry3,
			action,
			RTE_TABLE_ACTION_TAG);

		pkt4_work_tag(mbuf0, mbuf1, mbuf2, mbuf3,
			data0, data1, data2, data3);
	}

	return drop_mask0 |
		(drop_mask1 << 1) |
		(drop_mask2 << 2) |
		(drop_mask3 << 3);
}

static __rte_always_inline int
ah(struct rte_pipeline *p,
	struct rte_mbuf **pkts,
	uint64_t pkts_mask,
	struct rte_pipeline_table_entry **entries,
	struct rte_table_action *action,
	struct ap_config *cfg)
{
	uint64_t pkts_drop_mask = 0;
	uint64_t time = 0;

	if (cfg->action_mask & ((1LLU << RTE_TABLE_ACTION_MTR) |
		(1LLU << RTE_TABLE_ACTION_TIME)))
		time = rte_rdtsc();

	if ((pkts_mask & (pkts_mask + 1)) == 0) {
		uint64_t n_pkts = __builtin_popcountll(pkts_mask);
		uint32_t i;

		for (i = 0; i < (n_pkts & (~0x3LLU)); i += 4) {
			uint64_t drop_mask;

			drop_mask = pkt4_work(&pkts[i],
				&entries[i],
				time,
				action,
				cfg);

			pkts_drop_mask |= drop_mask << i;
		}

		for ( ; i < n_pkts; i++) {
			uint64_t drop_mask;

			drop_mask = pkt_work(pkts[i],
				entries[i],
				time,
				action,
				cfg);

			pkts_drop_mask |= drop_mask << i;
		}
	} else
		for ( ; pkts_mask; ) {
			uint32_t pos = __builtin_ctzll(pkts_mask);
			uint64_t pkt_mask = 1LLU << pos;
			uint64_t drop_mask;

			drop_mask = pkt_work(pkts[pos],
				entries[pos],
				time,
				action,
				cfg);

			pkts_mask &= ~pkt_mask;
			pkts_drop_mask |= drop_mask << pos;
		}

	rte_pipeline_ah_packet_drop(p, pkts_drop_mask);

	return 0;
}

static int
ah_default(struct rte_pipeline *p,
	struct rte_mbuf **pkts,
	uint64_t pkts_mask,
	struct rte_pipeline_table_entry **entries,
	void *arg)
{
	struct rte_table_action *action = arg;

	return ah(p,
		pkts,
		pkts_mask,
		entries,
		action,
		&action->cfg);
}

static rte_pipeline_table_action_handler_hit
ah_selector(struct rte_table_action *action)
{
	if (action->cfg.action_mask == (1LLU << RTE_TABLE_ACTION_FWD))
		return NULL;

	return ah_default;
}

int
rte_table_action_table_params_get(struct rte_table_action *action,
	struct rte_pipeline_table_params *params)
{
	rte_pipeline_table_action_handler_hit f_action_hit;
	uint32_t total_size;

	/* Check input arguments */
	if ((action == NULL) ||
		(params == NULL))
		return -EINVAL;

	f_action_hit = ah_selector(action);
	total_size = rte_align32pow2(action->data.total_size);

	/* Fill in params */
	params->f_action_hit = f_action_hit;
	params->f_action_miss = NULL;
	params->arg_ah = (f_action_hit) ? action : NULL;
	params->action_data_size = total_size -
		sizeof(struct rte_pipeline_table_entry);

	return 0;
}

int
rte_table_action_free(struct rte_table_action *action)
{
	if (action == NULL)
		return 0;

	rte_free(action);

	return 0;
}