aboutsummaryrefslogtreecommitdiff
path: root/platform/linux-generic/odp_packet_io.c
blob: 899f104255bc6c4c31c34e4af33cedea9267851d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
/* Copyright (c) 2013-2018, Linaro Limited
 * Copyright (c) 2019-2022, Nokia
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

#include <odp_posix_extensions.h>

#include <odp/api/buffer.h>
#include <odp/api/packet.h>
#include <odp/api/packet_io.h>
#include <odp/api/proto_stats.h>
#include <odp/api/shared_memory.h>
#include <odp/api/spinlock.h>
#include <odp/api/ticketlock.h>
#include <odp/api/time.h>

#include <odp/api/plat/packet_inlines.h>
#include <odp/api/plat/packet_io_inlines.h>
#include <odp/api/plat/queue_inlines.h>
#include <odp/api/plat/time_inlines.h>

#include <odp/autoheader_internal.h>
#include <odp_classification_internal.h>
#include <odp_config_internal.h>
#include <odp_debug_internal.h>
#include <odp_errno_define.h>
#include <odp_event_vector_internal.h>
#include <odp_init_internal.h>
#include <odp_libconfig_internal.h>
#include <odp_packet_internal.h>
#include <odp_packet_io_internal.h>
#include <odp_pcapng.h>
#include <odp_queue_if.h>
#include <odp_schedule_if.h>

#include <errno.h>
#include <ifaddrs.h>
#include <inttypes.h>
#include <string.h>
#include <sys/ioctl.h>
#include <time.h>

/* Sleep this many microseconds between pktin receive calls. Must be smaller
 * than 1000000 (a million), i.e. smaller than a second. */
#define SLEEP_USEC  1

/* Check total sleep time about every SLEEP_CHECK * SLEEP_USEC microseconds.
 * Must be power of two. */
#define SLEEP_CHECK 32

/* Max wait time supported to avoid potential overflow */
#define MAX_WAIT_TIME (UINT64_MAX / 1024)

typedef struct {
	const void *user_ptr;
	odp_queue_t queue;
	uint16_t idx;
} tx_compl_info_t;

/* Global variables */
static pktio_global_t *pktio_global;

/* pktio pointer entries ( for inlines) */
void *_odp_pktio_entry_ptr[ODP_CONFIG_PKTIO_ENTRIES];

static inline pktio_entry_t *pktio_entry_by_index(int index)
{
	return _odp_pktio_entry_ptr[index];
}

static inline _odp_event_hdr_t *packet_vector_to_event_hdr(odp_packet_vector_t pktv)
{
	return (_odp_event_hdr_t *)(uintptr_t)&_odp_packet_vector_hdr(pktv)->event_hdr;
}

static int read_config_file(pktio_global_t *pktio_glb)
{
	const char *str;
	int val = 0;

	ODP_PRINT("Packet IO config:\n");

	str = "pktio.pktin_frame_offset";
	if (!_odp_libconfig_lookup_int(str, &val)) {
		ODP_ERR("Config option '%s' not found.\n", str);
		return -1;
	}

	if (val < 0 || val > UINT16_MAX) {
		ODP_ERR("Bad value %s = %i\n", str, val);
		return -1;
	}

	pktio_glb->config.pktin_frame_offset = val;
	ODP_PRINT("  %s: %i\n", str, val);

	str = "pktio.tx_compl_pool_size";
	if (!_odp_libconfig_lookup_int(str, &val)) {
		ODP_ERR("Config option '%s' not found.\n", str);
		return -1;
	}

	if (val < 0) {
		ODP_ERR("Bad value %s = %i\n", str, val);
		return -1;
	}

	pktio_glb->config.tx_compl_pool_size = val;
	ODP_PRINT("  %s: %i\n", str, val);

	ODP_PRINT("\n");

	return 0;
}

int _odp_pktio_init_global(void)
{
	pktio_entry_t *pktio_entry;
	int i;
	odp_shm_t shm;
	int pktio_if;

	shm = odp_shm_reserve("_odp_pktio_global", sizeof(pktio_global_t),
			      ODP_CACHE_LINE_SIZE, 0);
	if (shm == ODP_SHM_INVALID)
		return -1;

	pktio_global = odp_shm_addr(shm);
	memset(pktio_global, 0, sizeof(pktio_global_t));
	pktio_global->shm = shm;

	odp_spinlock_init(&pktio_global->lock);

	if (read_config_file(pktio_global)) {
		odp_shm_free(shm);
		pktio_global = NULL;
		return -1;
	}

	for (i = 0; i < ODP_CONFIG_PKTIO_ENTRIES; ++i) {
		pktio_entry = &pktio_global->entries[i];

		pktio_entry->s.handle = _odp_cast_scalar(odp_pktio_t, i + 1);
		odp_ticketlock_init(&pktio_entry->s.rxl);
		odp_ticketlock_init(&pktio_entry->s.txl);
		odp_spinlock_init(&pktio_entry->s.cls.l2_cos_table.lock);
		odp_spinlock_init(&pktio_entry->s.cls.l3_cos_table.lock);

		_odp_pktio_entry_ptr[i] = pktio_entry;
	}

	for (pktio_if = 0; _odp_pktio_if_ops[pktio_if]; ++pktio_if) {
		if (_odp_pktio_if_ops[pktio_if]->init_global)
			if (_odp_pktio_if_ops[pktio_if]->init_global()) {
				ODP_ERR("failed to initialized pktio type %d",
					pktio_if);
				return -1;
			}
	}

	if (_ODP_PCAPNG) {
		if (_odp_pcapng_init_global()) {
			ODP_ERR("Failed to initialize pcapng\n");
			return -1;
		}
	}

	return 0;
}

int _odp_pktio_init_local(void)
{
	int pktio_if;

	for (pktio_if = 0; _odp_pktio_if_ops[pktio_if]; ++pktio_if) {
		if (_odp_pktio_if_ops[pktio_if]->init_local)
			if (_odp_pktio_if_ops[pktio_if]->init_local()) {
				ODP_ERR("failed to initialized pktio type %d",
					pktio_if);
				return -1;
			}
	}

	return 0;
}

static inline int is_free(pktio_entry_t *entry)
{
	return (entry->s.state == PKTIO_STATE_FREE);
}

static void lock_entry(pktio_entry_t *entry)
{
	odp_ticketlock_lock(&entry->s.rxl);
	odp_ticketlock_lock(&entry->s.txl);
}

static void unlock_entry(pktio_entry_t *entry)
{
	odp_ticketlock_unlock(&entry->s.txl);
	odp_ticketlock_unlock(&entry->s.rxl);
}

/**
 * Strip optional pktio type from device name by moving start pointer
 *
 * @param      name      Packet IO device name
 * @param[out] type_out  Optional char array (len = PKTIO_NAME_LEN) for storing
 *                       pktio type. Ignored when NULL.
 *
 * @return Pointer to the beginning of device name
 */
static const char *strip_pktio_type(const char *name, char *type_out)
{
	const char *if_name;

	if (type_out)
		type_out[0] = '\0';

	/* Strip pktio type prefix <pktio_type>:<if_name> */
	if_name = strchr(name, ':');

	if (if_name) {
		int pktio_if;
		int type_len = if_name - name;
		char pktio_type[type_len + 1];

		strncpy(pktio_type, name, type_len);
		pktio_type[type_len] = '\0';

		/* Remove colon */
		if_name++;

		/* Match if_type to enabled pktio devices */
		for (pktio_if = 0; _odp_pktio_if_ops[pktio_if]; pktio_if++) {
			if (!strcmp(pktio_type, _odp_pktio_if_ops[pktio_if]->name)) {
				if (type_out)
					strcpy(type_out, pktio_type);
				/* Some pktio devices expect device names to
				 * begin with pktio type */
				if (!strcmp(pktio_type, "ipc") ||
				    !strcmp(pktio_type, "null") ||
				    !strcmp(pktio_type, "pcap") ||
				    !strcmp(pktio_type, "tap"))
					return name;

				return if_name;
			}
		}
	}
	return name;
}

static void init_out_queues(pktio_entry_t *entry)
{
	int i;

	for (i = 0; i < PKTIO_MAX_QUEUES; i++) {
		entry->s.out_queue[i].queue  = ODP_QUEUE_INVALID;
		entry->s.out_queue[i].pktout = PKTOUT_INVALID;
	}
}

static void init_pktio_entry(pktio_entry_t *entry)
{
	int i;

	/* Clear all flags */
	entry->s.enabled.all_flags = 0;

	entry->s.tx_compl_pool = ODP_POOL_INVALID;

	odp_atomic_init_u64(&entry->s.stats_extra.in_discards, 0);
	odp_atomic_init_u64(&entry->s.stats_extra.out_discards, 0);
	odp_atomic_init_u64(&entry->s.tx_ts, 0);

	for (i = 0; i < PKTIO_MAX_QUEUES; i++) {
		entry->s.in_queue[i].queue = ODP_QUEUE_INVALID;
		entry->s.in_queue[i].pktin = PKTIN_INVALID;
	}

	init_out_queues(entry);

	_odp_pktio_classifier_init(entry);
}

static odp_pktio_t setup_pktio_entry(const char *name, odp_pool_t pool,
				     const odp_pktio_param_t *param)
{
	odp_pktio_t hdl;
	pktio_entry_t *pktio_entry;
	int i, pktio_if;
	char pktio_type[PKTIO_NAME_LEN];
	const char *if_name;
	uint16_t pktin_frame_offset = pktio_global->config.pktin_frame_offset;
	int ret = -1;

	if (strlen(name) >= PKTIO_NAME_LEN - 1) {
		/* ioctl names limitation */
		ODP_ERR("pktio name %s is too long (max: %d chars)\n", name, PKTIO_NAME_LEN - 1);
		return ODP_PKTIO_INVALID;
	}

	if_name = strip_pktio_type(name, pktio_type);

	for (i = 0; i < ODP_CONFIG_PKTIO_ENTRIES; ++i) {
		pktio_entry = &pktio_global->entries[i];
		if (is_free(pktio_entry)) {
			lock_entry(pktio_entry);
			if (is_free(pktio_entry))
				break;

			unlock_entry(pktio_entry);
		}
	}

	if (i == ODP_CONFIG_PKTIO_ENTRIES) {
		ODP_ERR("All pktios used already\n");
		return ODP_PKTIO_INVALID;
	}

	/* Entry was found and is now locked */
	pktio_entry->s.state = PKTIO_STATE_ACTIVE;
	hdl = pktio_entry->s.handle;

	init_pktio_entry(pktio_entry);

	snprintf(pktio_entry->s.name, sizeof(pktio_entry->s.name), "%s", if_name);
	snprintf(pktio_entry->s.full_name, sizeof(pktio_entry->s.full_name), "%s", name);
	pktio_entry->s.pool = pool;
	memcpy(&pktio_entry->s.param, param, sizeof(odp_pktio_param_t));
	pktio_entry->s.pktin_frame_offset = pktin_frame_offset;

	odp_pktio_config_init(&pktio_entry->s.config);

	for (pktio_if = 0; _odp_pktio_if_ops[pktio_if]; ++pktio_if) {
		/* Only use explicitly defined pktio type */
		if (strlen(pktio_type) &&
		    strcmp(_odp_pktio_if_ops[pktio_if]->name, pktio_type))
			continue;

		ret = _odp_pktio_if_ops[pktio_if]->open(hdl, pktio_entry, if_name, pool);

		if (!ret)
			break;
	}

	if (ret != 0) {
		pktio_entry->s.state = PKTIO_STATE_FREE;
		unlock_entry(pktio_entry);
		ODP_ERR("Unable to init any I/O type.\n");
		return ODP_PKTIO_INVALID;
	}

	pktio_entry->s.state = PKTIO_STATE_OPENED;
	pktio_entry->s.ops = _odp_pktio_if_ops[pktio_if];
	unlock_entry(pktio_entry);

	return hdl;
}

static int pool_type_is_packet(odp_pool_t pool)
{
	odp_pool_info_t pool_info;

	if (pool == ODP_POOL_INVALID)
		return 0;

	if (odp_pool_info(pool, &pool_info) != 0)
		return 0;

	return pool_info.params.type == ODP_POOL_PACKET;
}

static const char *driver_name(odp_pktio_t hdl)
{
	pktio_entry_t *entry;

	entry = get_pktio_entry(hdl);
	if (entry == NULL) {
		ODP_ERR("pktio entry %" PRIuPTR " does not exist\n", (uintptr_t)hdl);
		return "bad handle";
	}

	return entry->s.ops->name;
}

odp_pktio_t odp_pktio_open(const char *name, odp_pool_t pool,
			   const odp_pktio_param_t *param)
{
	odp_pktio_t hdl;
	odp_pktio_param_t default_param;

	if (param == NULL) {
		odp_pktio_param_init(&default_param);
		param = &default_param;
	}

	ODP_ASSERT(pool_type_is_packet(pool));

	hdl = odp_pktio_lookup(name);
	if (hdl != ODP_PKTIO_INVALID) {
		/* interface is already open */
		_odp_errno = EEXIST;
		return ODP_PKTIO_INVALID;
	}

	odp_spinlock_lock(&pktio_global->lock);
	hdl = setup_pktio_entry(name, pool, param);
	odp_spinlock_unlock(&pktio_global->lock);

	ODP_DBG("interface: %s, driver: %s\n", name, driver_name(hdl));

	return hdl;
}

static int _pktio_close(pktio_entry_t *entry)
{
	int ret;
	int state = entry->s.state;

	if (state != PKTIO_STATE_OPENED &&
	    state != PKTIO_STATE_STOPPED &&
	    state != PKTIO_STATE_STOP_PENDING)
		return -1;

	ret = entry->s.ops->close(entry);
	if (ret)
		return -1;

	if (state == PKTIO_STATE_STOP_PENDING)
		entry->s.state = PKTIO_STATE_CLOSE_PENDING;
	else
		entry->s.state = PKTIO_STATE_FREE;

	return 0;
}

static void destroy_in_queues(pktio_entry_t *entry, int num)
{
	int i;

	for (i = 0; i < num; i++) {
		if (entry->s.in_queue[i].queue != ODP_QUEUE_INVALID) {
			odp_queue_destroy(entry->s.in_queue[i].queue);
			entry->s.in_queue[i].queue = ODP_QUEUE_INVALID;
		}
	}
}

static void destroy_out_queues(pktio_entry_t *entry, int num)
{
	int i, rc;

	for (i = 0; i < num; i++) {
		if (entry->s.out_queue[i].queue != ODP_QUEUE_INVALID) {
			rc = odp_queue_destroy(entry->s.out_queue[i].queue);
			ODP_ASSERT(rc == 0);
			entry->s.out_queue[i].queue = ODP_QUEUE_INVALID;
		}
	}
}

static void flush_in_queues(pktio_entry_t *entry)
{
	odp_pktin_mode_t mode;
	int num, i;
	int max_pkts = 16;
	odp_packet_t packets[max_pkts];

	mode = entry->s.param.in_mode;
	num  = entry->s.num_in_queue;

	if (mode == ODP_PKTIN_MODE_DIRECT) {
		for (i = 0; i < num; i++) {
			int ret;
			odp_pktin_queue_t pktin = entry->s.in_queue[i].pktin;

			while ((ret = odp_pktin_recv(pktin, packets,
						     max_pkts))) {
				if (ret < 0) {
					ODP_ERR("Queue flush failed\n");
					return;
				}

				odp_packet_free_multi(packets, ret);
			}
		}
	}
}

int odp_pktio_close(odp_pktio_t hdl)
{
	pktio_entry_t *entry;
	int res;

	entry = get_pktio_entry(hdl);
	if (entry == NULL) {
		ODP_ERR("Bad handle\n");
		return -1;
	}

	if (entry->s.state == PKTIO_STATE_STARTED) {
		ODP_DBG("Missing odp_pktio_stop() before close.\n");
		return -1;
	}

	if (entry->s.state == PKTIO_STATE_STOPPED)
		flush_in_queues(entry);

	lock_entry(entry);

	destroy_in_queues(entry, entry->s.num_in_queue);
	destroy_out_queues(entry, entry->s.num_out_queue);

	entry->s.num_in_queue  = 0;
	entry->s.num_out_queue = 0;

	if (entry->s.tx_compl_pool != ODP_POOL_INVALID) {
		if (odp_pool_destroy(entry->s.tx_compl_pool)) {
			unlock_entry(entry);
			ODP_ERR("Unable to destroy Tx event completion pool\n");
			return -1;
		}
	}

	odp_spinlock_lock(&pktio_global->lock);
	res = _pktio_close(entry);
	odp_spinlock_unlock(&pktio_global->lock);
	if (res)
		ODP_ABORT("unable to close pktio\n");

	unlock_entry(entry);

	ODP_DBG("interface: %s\n", entry->s.name);

	return 0;
}

static int configure_tx_event_compl(pktio_entry_t *entry)
{
	odp_pool_param_t params;
	const char *name_base = "_odp_pktio_tx_compl_pool_";
	char pool_name[ODP_POOL_NAME_LEN];

	if (entry->s.tx_compl_pool != ODP_POOL_INVALID)
		return 0;

	snprintf(pool_name, sizeof(pool_name), "%s%d", name_base,
		 odp_pktio_index(entry->s.handle));
	odp_pool_param_init(&params);

	params.type = ODP_POOL_BUFFER;
	params.buf.num = pktio_global->config.tx_compl_pool_size;
	params.buf.size = sizeof(_odp_pktio_tx_compl_t);
	entry->s.tx_compl_pool = odp_pool_create(pool_name, &params);

	if (entry->s.tx_compl_pool == ODP_POOL_INVALID)
		return -1;

	return 0;
}

int odp_pktio_config(odp_pktio_t hdl, const odp_pktio_config_t *config)
{
	pktio_entry_t *entry;
	odp_pktio_capability_t capa;
	odp_pktio_config_t default_config;
	int res = 0;

	entry = get_pktio_entry(hdl);
	if (!entry) {
		ODP_ERR("Bad handle\n");
		return -1;
	}

	if (config == NULL) {
		odp_pktio_config_init(&default_config);
		config = &default_config;
	}

	if (odp_pktio_capability(hdl, &capa))
		return -1;

	/* Check config for invalid values */
	if (config->pktin.all_bits & ~capa.config.pktin.all_bits) {
		ODP_ERR("Unsupported input configuration option\n");
		return -1;
	}
	if (config->pktout.all_bits & ~capa.config.pktout.all_bits) {
		ODP_ERR("Unsupported output configuration option\n");
		return -1;
	}

	if (config->enable_loop && !capa.config.enable_loop) {
		ODP_ERR("Loopback mode not supported\n");
		return -1;
	}

	lock_entry(entry);
	if (entry->s.state == PKTIO_STATE_STARTED) {
		unlock_entry(entry);
		ODP_DBG("pktio %s: not stopped\n", entry->s.name);
		return -1;
	}

	entry->s.config = *config;

	entry->s.in_chksums.all_chksum = 0;
	entry->s.in_chksums.chksum.ipv4 = config->pktin.bit.ipv4_chksum;
	entry->s.in_chksums.chksum.tcp = config->pktin.bit.tcp_chksum;
	entry->s.in_chksums.chksum.udp = config->pktin.bit.udp_chksum;
	entry->s.in_chksums.chksum.sctp = config->pktin.bit.sctp_chksum;

	entry->s.enabled.tx_ts = config->pktout.bit.ts_ena;
	entry->s.enabled.tx_compl = config->pktout.bit.tx_compl_ena;

	if (entry->s.enabled.tx_compl)
		if (configure_tx_event_compl(entry)) {
			unlock_entry(entry);
			ODP_ERR("Unable to configure Tx event completion\n");
			return -1;
		}

	if (entry->s.ops->config)
		res = entry->s.ops->config(entry, config);

	unlock_entry(entry);

	return res;
}

int odp_pktio_start(odp_pktio_t hdl)
{
	pktio_entry_t *entry;
	odp_pktin_mode_t mode;
	int res = 0;

	entry = get_pktio_entry(hdl);
	if (!entry) {
		ODP_ERR("Bad handle\n");
		return -1;
	}

	lock_entry(entry);
	if (entry->s.state == PKTIO_STATE_STARTED) {
		unlock_entry(entry);
		ODP_ERR("Already started\n");
		return -1;
	}
	if (entry->s.ops->start)
		res = entry->s.ops->start(entry);
	if (!res)
		entry->s.state = PKTIO_STATE_STARTED;

	unlock_entry(entry);

	mode = entry->s.param.in_mode;

	if (mode == ODP_PKTIN_MODE_SCHED) {
		unsigned int i;
		unsigned int num = entry->s.num_in_queue;
		int index[num];
		odp_queue_t odpq[num];

		for (i = 0; i < num; i++) {
			index[i] = i;
			odpq[i] = entry->s.in_queue[i].queue;

			if (entry->s.in_queue[i].queue == ODP_QUEUE_INVALID) {
				ODP_ERR("No input queue\n");
				return -1;
			}
		}

		_odp_sched_fn->pktio_start(odp_pktio_index(hdl), num, index, odpq);
	}

	ODP_DBG("interface: %s, input queues: %u, output queues: %u\n",
		entry->s.name, entry->s.num_in_queue, entry->s.num_out_queue);

	if (_ODP_PCAPNG) {
		if (_odp_pcapng_start(entry))
			ODP_ERR("pcapng start failed, won't capture\n");
	}

	return res;
}

static int _pktio_stop(pktio_entry_t *entry)
{
	int res = 0;
	odp_pktin_mode_t mode = entry->s.param.in_mode;

	if (entry->s.state != PKTIO_STATE_STARTED) {
		ODP_ERR("Not started\n");
		return -1;
	}

	if (entry->s.ops->stop)
		res = entry->s.ops->stop(entry);

	if (res)
		return -1;

	if (mode == ODP_PKTIN_MODE_SCHED)
		entry->s.state = PKTIO_STATE_STOP_PENDING;
	else
		entry->s.state = PKTIO_STATE_STOPPED;

	if (_ODP_PCAPNG)
		_odp_pcapng_stop(entry);

	return res;
}

int odp_pktio_stop(odp_pktio_t hdl)
{
	pktio_entry_t *entry;
	int res;

	entry = get_pktio_entry(hdl);
	if (!entry) {
		ODP_ERR("Bad handle\n");
		return -1;
	}

	lock_entry(entry);
	res = _pktio_stop(entry);
	unlock_entry(entry);

	ODP_DBG("interface: %s\n", entry->s.name);

	return res;
}

odp_pktio_t odp_pktio_lookup(const char *name)
{
	odp_pktio_t hdl = ODP_PKTIO_INVALID;
	pktio_entry_t *entry;
	const char *ifname;
	int i;

	ifname = strip_pktio_type(name, NULL);

	odp_spinlock_lock(&pktio_global->lock);

	for (i = 0; i < ODP_CONFIG_PKTIO_ENTRIES; ++i) {
		entry = pktio_entry_by_index(i);
		if (!entry || is_free(entry))
			continue;

		lock_entry(entry);

		if (entry->s.state >= PKTIO_STATE_ACTIVE &&
		    strncmp(entry->s.name, ifname, sizeof(entry->s.name)) == 0)
			hdl = _odp_cast_scalar(odp_pktio_t, i + 1);

		unlock_entry(entry);

		if (hdl != ODP_PKTIO_INVALID)
			break;
	}

	odp_spinlock_unlock(&pktio_global->lock);

	return hdl;
}

static void packet_vector_enq_cos(odp_queue_t queue, odp_event_t events[],
				  uint32_t num, cos_t *cos_hdr)
{
	odp_packet_vector_t pktv;
	odp_pool_t pool = cos_hdr->s.vector.pool;
	uint32_t max_size = cos_hdr->s.vector.max_size;
	uint32_t num_enq;
	int num_pktv = (num + max_size - 1) / max_size;
	int ret;
	int i;
	odp_packet_vector_t pktv_tbl[num_pktv];
	odp_event_t event_tbl[num_pktv];

	for (i = 0; i < num_pktv; i++) {
		pktv = odp_packet_vector_alloc(pool);
		if (odp_unlikely(pktv == ODP_PACKET_VECTOR_INVALID))
			break;
		pktv_tbl[i] = pktv;
		event_tbl[i] = odp_packet_vector_to_event(pktv);
	}
	if (odp_unlikely(i == 0)) {
		odp_event_free_multi(events, num);
		_odp_cos_queue_stats_add(cos_hdr, queue, 0, num);
		return;
	}
	num_pktv = i;
	num_enq = 0;
	for (i = 0; i < num_pktv; i++) {
		odp_packet_t *pkt_tbl;
		int pktv_size = max_size;

		pktv = pktv_tbl[i];

		if (num_enq + max_size > num)
			pktv_size = num - num_enq;

		odp_packet_vector_tbl(pktv, &pkt_tbl);
		odp_packet_from_event_multi(pkt_tbl, &events[num_enq], pktv_size);
		odp_packet_vector_size_set(pktv, pktv_size);
		num_enq += pktv_size;
	}

	ret = odp_queue_enq_multi(queue, event_tbl, num_pktv);
	if (odp_likely(ret == num_pktv)) {
		_odp_cos_queue_stats_add(cos_hdr, queue, num_enq, num - num_enq);
	} else {
		uint32_t enqueued;

		if (ret < 0)
			ret = 0;
		enqueued = max_size * ret;
		_odp_cos_queue_stats_add(cos_hdr, queue, enqueued, num - enqueued);
		odp_event_free_multi(&event_tbl[ret], num_pktv - ret);
	}
}

static void packet_vector_enq(odp_queue_t queue, odp_event_t events[],
			      uint32_t num, odp_pool_t pool)
{
	odp_packet_vector_t pktv;
	odp_packet_t *pkt_tbl;

	pktv = odp_packet_vector_alloc(pool);
	if (odp_unlikely(pktv == ODP_PACKET_VECTOR_INVALID)) {
		odp_event_free_multi(events, num);
		return;
	}

	odp_packet_vector_tbl(pktv, &pkt_tbl);
	odp_packet_from_event_multi(pkt_tbl, events, num);
	odp_packet_vector_size_set(pktv, num);

	if (odp_unlikely(odp_queue_enq(queue, odp_packet_vector_to_event(pktv))))
		odp_event_free(odp_packet_vector_to_event(pktv));
}

static inline odp_packet_vector_t packet_vector_create(odp_packet_t packets[], uint32_t num,
						       odp_pool_t pool)
{
	odp_packet_vector_t pktv;
	odp_packet_t *pkt_tbl;
	uint32_t i;

	pktv = odp_packet_vector_alloc(pool);
	if (odp_unlikely(pktv == ODP_PACKET_VECTOR_INVALID)) {
		odp_packet_free_multi(packets, num);
		return ODP_PACKET_VECTOR_INVALID;
	}

	odp_packet_vector_tbl(pktv, &pkt_tbl);
	for (i = 0; i < num; i++)
		pkt_tbl[i] = packets[i];
	odp_packet_vector_size_set(pktv, num);

	return pktv;
}

static inline int pktin_recv_buf(pktio_entry_t *entry, int pktin_index,
				 _odp_event_hdr_t *event_hdrs[], int num)
{
	odp_packet_t pkt;
	odp_packet_t packets[num];
	odp_packet_hdr_t *pkt_hdr;
	odp_pool_t pool = ODP_POOL_INVALID;
	_odp_event_hdr_t *event_hdr;
	int i, pkts, num_rx, num_ev, num_dst;
	odp_queue_t cur_queue;
	odp_event_t ev[num];
	odp_queue_t dst[num];
	uint16_t cos[num];
	uint16_t cur_cos = 0;
	int dst_idx[num];
	odp_bool_t vector_enabled = entry->s.in_queue[pktin_index].vector.enable;

	if (vector_enabled) {
		/* Make sure all packets will fit into a single packet vector */
		if ((int)entry->s.in_queue[pktin_index].vector.max_size < num)
			num = entry->s.in_queue[pktin_index].vector.max_size;
		pool = entry->s.in_queue[pktin_index].vector.pool;
	}

	num_rx = 0;
	num_dst = 0;
	num_ev = 0;

	/* Some compilers need this dummy initialization */
	cur_queue = ODP_QUEUE_INVALID;

	pkts = entry->s.ops->recv(entry, pktin_index, packets, num);

	for (i = 0; i < pkts; i++) {
		pkt = packets[i];
		pkt_hdr = packet_hdr(pkt);
		event_hdr = packet_to_event_hdr(pkt);

		if (odp_unlikely(pkt_hdr->p.input_flags.dst_queue)) {
			/* Sort events for enqueue multi operation(s) based on CoS
			 * and destination queue. */
			if (odp_unlikely(num_dst == 0)) {
				num_dst = 1;
				cur_queue = pkt_hdr->dst_queue;
				cur_cos = pkt_hdr->cos;
				dst[0] = cur_queue;
				cos[0] = cur_cos;
				dst_idx[0] = 0;
			}

			ev[num_ev] = odp_packet_to_event(pkt);

			if (cur_queue != pkt_hdr->dst_queue || cur_cos != pkt_hdr->cos) {
				cur_queue = pkt_hdr->dst_queue;
				cur_cos = pkt_hdr->cos;
				dst[num_dst] = cur_queue;
				cos[num_dst] = cur_cos;
				dst_idx[num_dst] = num_ev;
				num_dst++;
			}

			num_ev++;
			continue;
		}
		event_hdrs[num_rx++] = event_hdr;
	}

	/* Optimization for the common case */
	if (odp_likely(num_dst == 0)) {
		if (!vector_enabled || num_rx < 1)
			return num_rx;

		/* Create packet vector */
		odp_packet_vector_t pktv = packet_vector_create((odp_packet_t *)event_hdrs,
								num_rx, pool);

		if (odp_unlikely(pktv == ODP_PACKET_VECTOR_INVALID))
			return 0;

		event_hdrs[0] = packet_vector_to_event_hdr(pktv);
		return 1;
	}

	for (i = 0; i < num_dst; i++) {
		cos_t *cos_hdr = NULL;
		int num_enq, ret;
		int idx = dst_idx[i];

		if (i == (num_dst - 1))
			num_enq = num_ev - idx;
		else
			num_enq = dst_idx[i + 1] - idx;

		if (cos[i] != CLS_COS_IDX_NONE) {
			/* Packets from classifier */
			cos_hdr = _odp_cos_entry_from_idx(cos[i]);

			if (cos_hdr->s.vector.enable) {
				packet_vector_enq_cos(dst[i], &ev[idx], num_enq, cos_hdr);
				continue;
			}
		} else if (vector_enabled) {
			/* Packets from inline IPsec */
			packet_vector_enq(dst[i], &ev[idx], num_enq, pool);
			continue;
		}

		ret = odp_queue_enq_multi(dst[i], &ev[idx], num_enq);

		if (ret < 0)
			ret = 0;

		if (ret < num_enq)
			odp_event_free_multi(&ev[idx + ret], num_enq - ret);

		/* Update CoS statistics */
		if (cos[i] != CLS_COS_IDX_NONE)
			_odp_cos_queue_stats_add(cos_hdr, dst[i], ret, num_enq - ret);
	}
	return num_rx;
}

static inline int packet_vector_send(odp_pktout_queue_t pktout_queue, odp_event_t event)
{
	odp_packet_vector_t pktv = odp_packet_vector_from_event(event);
	odp_packet_t *pkt_tbl;
	int num, sent;

	num = odp_packet_vector_tbl(pktv, &pkt_tbl);
	ODP_ASSERT(num > 0);
	sent = odp_pktout_send(pktout_queue, pkt_tbl, num);

	/* Return success if any packets were sent. Free the possible remaining
	   packets in the vector and increase out_discards count accordingly. */
	if (odp_unlikely(sent <= 0)) {
		return -1;
	} else if (odp_unlikely(sent != num)) {
		pktio_entry_t *entry = get_pktio_entry(pktout_queue.pktio);
		int discards = num - sent;

		ODP_ASSERT(entry != NULL);

		odp_atomic_add_u64(&entry->s.stats_extra.out_discards, discards);

		if (odp_unlikely(_odp_pktio_tx_compl_enabled(entry)))
			_odp_pktio_allocate_and_send_tx_compl_events(entry, &pkt_tbl[sent],
								     discards);

		odp_packet_free_multi(&pkt_tbl[sent], discards);
	}

	odp_packet_vector_free(pktv);

	return 0;
}

static int pktout_enqueue(odp_queue_t queue, _odp_event_hdr_t *event_hdr)
{
	odp_event_t event = _odp_event_from_hdr(event_hdr);
	odp_packet_t pkt = packet_from_event_hdr(event_hdr);
	odp_pktout_queue_t pktout_queue;
	int len = 1;
	int nbr;

	if (_odp_sched_fn->ord_enq_multi(queue, (void **)event_hdr, len, &nbr))
		return (nbr == len ? 0 : -1);

	pktout_queue = _odp_queue_fn->get_pktout(queue);

	if (odp_event_type(event) == ODP_EVENT_PACKET_VECTOR)
		return packet_vector_send(pktout_queue, event);

	nbr = odp_pktout_send(pktout_queue, &pkt, len);
	return (nbr == len ? 0 : -1);
}

static int pktout_enq_multi(odp_queue_t queue, _odp_event_hdr_t *event_hdr[],
			    int num)
{
	odp_event_t event;
	odp_packet_t pkt_tbl[QUEUE_MULTI_MAX];
	odp_pktout_queue_t pktout_queue;
	int have_pktv = 0;
	int nbr;
	int i;

	if (_odp_sched_fn->ord_enq_multi(queue, (void **)event_hdr, num, &nbr))
		return nbr;

	for (i = 0; i < num; ++i) {
		event = _odp_event_from_hdr(event_hdr[i]);

		if (odp_event_type(event) == ODP_EVENT_PACKET_VECTOR) {
			have_pktv = 1;
			break;
		}

		pkt_tbl[i] = packet_from_event_hdr(event_hdr[i]);
	}

	pktout_queue = _odp_queue_fn->get_pktout(queue);

	if (!have_pktv)
		return odp_pktout_send(pktout_queue, pkt_tbl, num);

	for (i = 0; i < num; ++i) {
		event = _odp_event_from_hdr(event_hdr[i]);

		if (odp_event_type(event) == ODP_EVENT_PACKET_VECTOR) {
			if (odp_unlikely(packet_vector_send(pktout_queue, event)))
				break;
		} else {
			odp_packet_t pkt = packet_from_event_hdr(event_hdr[i]);

			nbr = odp_pktout_send(pktout_queue, &pkt, 1);
			if (odp_unlikely(nbr != 1))
				break;
		}
	}
	return i;
}

static _odp_event_hdr_t *pktin_dequeue(odp_queue_t queue)
{
	_odp_event_hdr_t *event_hdr;
	_odp_event_hdr_t *hdr_tbl[QUEUE_MULTI_MAX];
	int pkts;
	odp_pktin_queue_t pktin_queue = _odp_queue_fn->get_pktin(queue);
	odp_pktio_t pktio = pktin_queue.pktio;
	int pktin_index   = pktin_queue.index;
	pktio_entry_t *entry = get_pktio_entry(pktio);

	ODP_ASSERT(entry != NULL);

	if (_odp_queue_fn->orig_deq_multi(queue, &event_hdr, 1) == 1)
		return event_hdr;

	pkts = pktin_recv_buf(entry, pktin_index, hdr_tbl, QUEUE_MULTI_MAX);

	if (pkts <= 0)
		return NULL;

	if (pkts > 1) {
		int num_enq;
		int num = pkts - 1;

		num_enq = odp_queue_enq_multi(queue,
					      (odp_event_t *)&hdr_tbl[1], num);

		if (odp_unlikely(num_enq < num)) {
			if (odp_unlikely(num_enq < 0))
				num_enq = 0;

			ODP_DBG("Interface %s dropped %i packets\n",
				entry->s.name, num - num_enq);
			_odp_event_free_multi(&hdr_tbl[num_enq + 1], num - num_enq);
		}
	}

	event_hdr = hdr_tbl[0];
	return event_hdr;
}

static int pktin_deq_multi(odp_queue_t queue, _odp_event_hdr_t *event_hdr[],
			   int num)
{
	int nbr;
	_odp_event_hdr_t *hdr_tbl[QUEUE_MULTI_MAX];
	int pkts, i, j;
	odp_pktin_queue_t pktin_queue = _odp_queue_fn->get_pktin(queue);
	odp_pktio_t pktio = pktin_queue.pktio;
	int pktin_index   = pktin_queue.index;
	pktio_entry_t *entry = get_pktio_entry(pktio);

	ODP_ASSERT(entry != NULL);

	nbr = _odp_queue_fn->orig_deq_multi(queue, event_hdr, num);
	if (odp_unlikely(nbr > num))
		ODP_ABORT("queue_deq_multi req: %d, returned %d\n", num, nbr);

	/** queue already has number of requested buffers,
	 *  do not do receive in that case.
	 */
	if (nbr == num)
		return nbr;

	pkts = pktin_recv_buf(entry, pktin_index, hdr_tbl, QUEUE_MULTI_MAX);

	if (pkts <= 0)
		return nbr;

	for (i = 0; i < pkts && nbr < num; i++, nbr++)
		event_hdr[nbr] = hdr_tbl[i];

	/* Queue the rest for later */
	for (j = 0; i < pkts; i++, j++)
		hdr_tbl[j] = hdr_tbl[i];

	if (j) {
		int num_enq;

		num_enq = odp_queue_enq_multi(queue, (odp_event_t *)hdr_tbl, j);

		if (odp_unlikely(num_enq < j)) {
			if (odp_unlikely(num_enq < 0))
				num_enq = 0;

			ODP_DBG("Interface %s dropped %i packets\n",
				entry->s.name, j - num_enq);
			_odp_event_free_multi(&event_hdr[num_enq], j - num_enq);
		}
	}

	return nbr;
}

int _odp_sched_cb_pktin_poll_one(int pktio_index,
				 int rx_queue,
				 odp_event_t evt_tbl[])
{
	int num_rx, num_pkts, i;
	pktio_entry_t *entry = pktio_entry_by_index(pktio_index);
	odp_packet_t pkt;
	odp_packet_hdr_t *pkt_hdr;
	odp_pool_t pool = ODP_POOL_INVALID;
	odp_packet_t packets[QUEUE_MULTI_MAX];
	odp_queue_t queue;
	odp_bool_t vector_enabled = entry->s.in_queue[rx_queue].vector.enable;
	uint32_t num = QUEUE_MULTI_MAX;
	cos_t *cos_hdr = NULL;

	if (odp_unlikely(entry->s.state != PKTIO_STATE_STARTED)) {
		if (entry->s.state < PKTIO_STATE_ACTIVE ||
		    entry->s.state == PKTIO_STATE_STOP_PENDING)
			return -1;

		ODP_DBG("interface not started\n");
		return 0;
	}

	if (vector_enabled) {
		/* Make sure all packets will fit into a single packet vector */
		if (entry->s.in_queue[rx_queue].vector.max_size < num)
			num = entry->s.in_queue[rx_queue].vector.max_size;
		pool = entry->s.in_queue[rx_queue].vector.pool;
	}

	ODP_ASSERT((unsigned int)rx_queue < entry->s.num_in_queue);
	num_pkts = entry->s.ops->recv(entry, rx_queue, packets, num);

	num_rx = 0;
	for (i = 0; i < num_pkts; i++) {
		pkt = packets[i];
		pkt_hdr = packet_hdr(pkt);
		if (odp_unlikely(pkt_hdr->p.input_flags.dst_queue)) {
			odp_event_t event = odp_packet_to_event(pkt);
			uint16_t cos_idx = pkt_hdr->cos;

			queue = pkt_hdr->dst_queue;

			if (cos_idx != CLS_COS_IDX_NONE) {
				/* Packets from classifier */
				cos_hdr = _odp_cos_entry_from_idx(cos_idx);

				if (cos_hdr->s.vector.enable) {
					packet_vector_enq_cos(queue, &event, 1, cos_hdr);
					continue;
				}
			} else if (vector_enabled) {
				/* Packets from inline IPsec */
				packet_vector_enq(queue, &event, 1, pool);
				continue;
			}

			if (odp_unlikely(odp_queue_enq(queue, event))) {
				/* Queue full? */
				odp_packet_free(pkt);
				if (cos_idx != CLS_COS_IDX_NONE)
					_odp_cos_queue_stats_add(cos_hdr, queue, 0, 1);
				else
					odp_atomic_inc_u64(&entry->s.stats_extra.in_discards);
			} else {
				if (cos_idx != CLS_COS_IDX_NONE)
					_odp_cos_queue_stats_add(cos_hdr, queue, 1, 0);
			}
		} else {
			evt_tbl[num_rx++] = odp_packet_to_event(pkt);
		}
	}

	/* Create packet vector */
	if (vector_enabled && num_rx > 0) {
		odp_packet_vector_t pktv = packet_vector_create((odp_packet_t *)evt_tbl,
								num_rx, pool);

		if (odp_unlikely(pktv == ODP_PACKET_VECTOR_INVALID))
			return 0;

		evt_tbl[0] = odp_packet_vector_to_event(pktv);
		return 1;
	}

	return num_rx;
}

int _odp_sched_cb_pktin_poll(int pktio_index, int pktin_index,
			     _odp_event_hdr_t *hdr_tbl[], int num)
{
	pktio_entry_t *entry = pktio_entry_by_index(pktio_index);
	int state = entry->s.state;

	if (odp_unlikely(state != PKTIO_STATE_STARTED)) {
		if (state < PKTIO_STATE_ACTIVE ||
		    state == PKTIO_STATE_STOP_PENDING)
			return -1;

		ODP_DBG("Interface %s not started\n", entry->s.name);
		return 0;
	}

	return pktin_recv_buf(entry, pktin_index, hdr_tbl, num);
}

void _odp_sched_cb_pktio_stop_finalize(int pktio_index)
{
	int state;
	pktio_entry_t *entry = pktio_entry_by_index(pktio_index);

	lock_entry(entry);

	state = entry->s.state;

	if (state != PKTIO_STATE_STOP_PENDING &&
	    state != PKTIO_STATE_CLOSE_PENDING) {
		unlock_entry(entry);
		ODP_ERR("Not in a pending state %i\n", state);
		return;
	}

	if (state == PKTIO_STATE_STOP_PENDING)
		entry->s.state = PKTIO_STATE_STOPPED;
	else
		entry->s.state = PKTIO_STATE_FREE;

	unlock_entry(entry);
}

static inline uint32_t pktio_maxlen(odp_pktio_t hdl)
{
	pktio_entry_t *entry;
	uint32_t ret = 0;

	entry = get_pktio_entry(hdl);
	if (entry == NULL) {
		ODP_DBG("pktio entry %" PRIuPTR " does not exist\n", (uintptr_t)hdl);
		return 0;
	}

	lock_entry(entry);

	if (odp_unlikely(is_free(entry))) {
		unlock_entry(entry);
		ODP_DBG("already freed pktio\n");
		return 0;
	}

	if (entry->s.ops->maxlen_get)
		ret = entry->s.ops->maxlen_get(entry);

	unlock_entry(entry);
	return ret;
}

uint32_t odp_pktin_maxlen(odp_pktio_t pktio)
{
	return pktio_maxlen(pktio);
}

uint32_t odp_pktout_maxlen(odp_pktio_t pktio)
{
	return pktio_maxlen(pktio);
}

int odp_pktio_maxlen_set(odp_pktio_t hdl, uint32_t maxlen_input,
			 uint32_t maxlen_output)
{
	odp_pktio_capability_t capa;
	pktio_entry_t *entry;
	int ret = 0;

	entry = get_pktio_entry(hdl);
	if (entry == NULL) {
		ODP_ERR("Pktio entry %" PRIuPTR " does not exist\n", (uintptr_t)hdl);
		return -1;
	}

	ret = odp_pktio_capability(hdl, &capa);
	if (ret) {
		ODP_ERR("Reading pktio capability failed\n");
		goto fail;
	}

	lock_entry(entry);

	if (odp_unlikely(is_free(entry))) {
		ODP_ERR("Pktio already freed\n");
		ret = -1;
		goto fail;
	}
	if (entry->s.state == PKTIO_STATE_STARTED) {
		ODP_ERR("Pktio not stopped\n");
		ret = -1;
		goto fail;
	}

	if (capa.set_op.op.maxlen == 0) {
		ODP_ERR("Setting maximum frame length not supported\n");
		ret = -1;
		goto fail;
	}

	if (capa.maxlen.equal && (maxlen_input != maxlen_output)) {
		ODP_ERR("Max input and output lengths don't match\n");
		ret = -1;
		goto fail;
	}

	if (maxlen_input < capa.maxlen.min_input ||
	    maxlen_input > capa.maxlen.max_input) {
		ODP_ERR("Invalid max input length value: %" PRIu32 "\n", maxlen_input);
		ret = -1;
		goto fail;
	}

	if (maxlen_output < capa.maxlen.min_output ||
	    maxlen_output > capa.maxlen.max_output) {
		ODP_ERR("Invalid max output length value: %" PRIu32 "\n", maxlen_output);
		ret = -1;
		goto fail;
	}

	if (entry->s.ops->maxlen_set)
		ret = entry->s.ops->maxlen_set(entry, maxlen_input, maxlen_output);

fail:
	unlock_entry(entry);
	return ret;
}

int odp_pktio_promisc_mode_set(odp_pktio_t hdl, odp_bool_t enable)
{
	pktio_entry_t *entry;
	int ret = -1;

	entry = get_pktio_entry(hdl);
	if (entry == NULL) {
		ODP_DBG("pktio entry %" PRIuPTR " does not exist\n", (uintptr_t)hdl);
		return -1;
	}

	lock_entry(entry);

	if (odp_unlikely(is_free(entry))) {
		unlock_entry(entry);
		ODP_DBG("already freed pktio\n");
		return -1;
	}
	if (entry->s.state == PKTIO_STATE_STARTED) {
		unlock_entry(entry);
		return -1;
	}

	if (entry->s.ops->promisc_mode_set)
		ret = entry->s.ops->promisc_mode_set(entry, enable);

	unlock_entry(entry);
	return ret;
}

int odp_pktio_promisc_mode(odp_pktio_t hdl)
{
	pktio_entry_t *entry;
	int ret = -1;

	entry = get_pktio_entry(hdl);
	if (entry == NULL) {
		ODP_DBG("pktio entry %" PRIuPTR " does not exist\n", (uintptr_t)hdl);
		return -1;
	}

	lock_entry(entry);

	if (odp_unlikely(is_free(entry))) {
		unlock_entry(entry);
		ODP_DBG("already freed pktio\n");
		return -1;
	}

	if (entry->s.ops->promisc_mode_get)
		ret = entry->s.ops->promisc_mode_get(entry);
	unlock_entry(entry);

	return ret;
}

int odp_pktio_mac_addr(odp_pktio_t hdl, void *mac_addr, int addr_size)
{
	pktio_entry_t *entry;
	int ret = ETH_ALEN;

	if (addr_size < ETH_ALEN) {
		/* Output buffer too small */
		return -1;
	}

	entry = get_pktio_entry(hdl);
	if (entry == NULL) {
		ODP_DBG("pktio entry %" PRIuPTR " does not exist\n", (uintptr_t)hdl);
		return -1;
	}

	lock_entry(entry);

	if (odp_unlikely(is_free(entry))) {
		unlock_entry(entry);
		ODP_DBG("already freed pktio\n");
		return -1;
	}

	if (entry->s.ops->mac_get) {
		ret = entry->s.ops->mac_get(entry, mac_addr);
	} else {
		ODP_DBG("pktio does not support mac addr get\n");
		ret = -1;
	}
	unlock_entry(entry);

	return ret;
}

int odp_pktio_mac_addr_set(odp_pktio_t hdl, const void *mac_addr, int addr_size)
{
	pktio_entry_t *entry;
	int ret = -1;

	if (addr_size < ETH_ALEN) {
		/* Input buffer too small */
		return -1;
	}

	entry = get_pktio_entry(hdl);
	if (entry == NULL) {
		ODP_DBG("pktio entry %" PRIuPTR " does not exist\n", (uintptr_t)hdl);
		return -1;
	}

	lock_entry(entry);

	if (odp_unlikely(is_free(entry))) {
		unlock_entry(entry);
		ODP_DBG("already freed pktio\n");
		return -1;
	}

	if (entry->s.state == PKTIO_STATE_STARTED) {
		unlock_entry(entry);
		return -1;
	}

	if (entry->s.ops->mac_set)
		ret = entry->s.ops->mac_set(entry, mac_addr);

	unlock_entry(entry);
	return ret;
}

odp_pktio_link_status_t odp_pktio_link_status(odp_pktio_t hdl)
{
	pktio_entry_t *entry;
	int ret = ODP_PKTIO_LINK_STATUS_UNKNOWN;

	entry = get_pktio_entry(hdl);
	if (entry == NULL) {
		ODP_DBG("pktio entry %" PRIuPTR " does not exist\n", (uintptr_t)hdl);
		return ODP_PKTIO_LINK_STATUS_UNKNOWN;
	}

	lock_entry(entry);

	if (odp_unlikely(is_free(entry))) {
		unlock_entry(entry);
		ODP_DBG("already freed pktio\n");
		return ODP_PKTIO_LINK_STATUS_UNKNOWN;
	}

	if (entry->s.ops->link_status)
		ret = entry->s.ops->link_status(entry);
	unlock_entry(entry);

	return ret;
}

void odp_pktio_param_init(odp_pktio_param_t *params)
{
	memset(params, 0, sizeof(odp_pktio_param_t));
	params->in_mode  = ODP_PKTIN_MODE_DIRECT;
	params->out_mode = ODP_PKTOUT_MODE_DIRECT;
}

void odp_pktin_queue_param_init(odp_pktin_queue_param_t *param)
{
	memset(param, 0, sizeof(odp_pktin_queue_param_t));
	param->op_mode = ODP_PKTIO_OP_MT;
	param->num_queues = 1;
	/* no need to choose queue type since pktin mode defines it */
	odp_queue_param_init(&param->queue_param);
}

void odp_pktout_queue_param_init(odp_pktout_queue_param_t *param)
{
	memset(param, 0, sizeof(odp_pktout_queue_param_t));
	param->op_mode = ODP_PKTIO_OP_MT;
	param->num_queues = 1;
}

void odp_pktio_config_init(odp_pktio_config_t *config)
{
	memset(config, 0, sizeof(odp_pktio_config_t));

	config->parser.layer = ODP_PROTO_LAYER_ALL;
	config->reassembly.max_num_frags = 2;
}

int odp_pktio_info(odp_pktio_t hdl, odp_pktio_info_t *info)
{
	pktio_entry_t *entry;

	entry = get_pktio_entry(hdl);

	if (entry == NULL) {
		ODP_DBG("pktio entry %" PRIuPTR " does not exist\n", (uintptr_t)hdl);
		return -1;
	}

	memset(info, 0, sizeof(odp_pktio_info_t));
	info->name = entry->s.full_name;
	info->drv_name = entry->s.ops->name;
	info->pool = entry->s.pool;
	memcpy(&info->param, &entry->s.param, sizeof(odp_pktio_param_t));

	return 0;
}

int odp_pktio_link_info(odp_pktio_t hdl, odp_pktio_link_info_t *info)
{
	pktio_entry_t *entry;

	entry = get_pktio_entry(hdl);

	if (entry == NULL) {
		ODP_DBG("pktio entry %" PRIuPTR " does not exist\n", (uintptr_t)hdl);
		return -1;
	}

	if (entry->s.ops->link_info)
		return entry->s.ops->link_info(entry, info);

	return -1;
}

uint64_t odp_pktio_ts_res(odp_pktio_t hdl)
{
	pktio_entry_t *entry;

	entry = get_pktio_entry(hdl);
	if (entry == NULL) {
		ODP_ERR("pktio entry %" PRIuPTR " does not exist\n", (uintptr_t)hdl);
		return 0;
	}

	if (entry->s.ops->pktio_ts_res)
		return entry->s.ops->pktio_ts_res(entry);

	return odp_time_global_res();
}

odp_time_t odp_pktio_ts_from_ns(odp_pktio_t hdl, uint64_t ns)
{
	pktio_entry_t *entry;

	entry = get_pktio_entry(hdl);

	if (entry == NULL) {
		ODP_ERR("pktio entry %" PRIuPTR " does not exist\n", (uintptr_t)hdl);
		return ODP_TIME_NULL;
	}

	if (entry->s.ops->pktio_ts_from_ns)
		return entry->s.ops->pktio_ts_from_ns(entry, ns);

	return odp_time_global_from_ns(ns);
}

odp_time_t odp_pktio_time(odp_pktio_t hdl, odp_time_t *global_ts)
{
	pktio_entry_t *entry;
	odp_time_t ts;

	entry = get_pktio_entry(hdl);
	if (entry == NULL) {
		ODP_ERR("pktio entry %" PRIuPTR " does not exist\n", (uintptr_t)hdl);
		return ODP_TIME_NULL;
	}

	/* Callback if present */
	if (entry->s.ops->pktio_time)
		return entry->s.ops->pktio_time(entry, global_ts);

	/* By default both Packet IO time source and
	 * global time source are same.
	 */
	ts = odp_time_global();
	if (global_ts)
		*global_ts = ts;
	return ts;
}

uint64_t ODP_DEPRECATE(odp_pktin_ts_res)(odp_pktio_t hdl)
{
	return odp_pktio_ts_res(hdl);
}

odp_time_t ODP_DEPRECATE(odp_pktin_ts_from_ns)(odp_pktio_t hdl, uint64_t ns)
{
	return odp_pktio_ts_from_ns(hdl, ns);
}

void odp_pktio_print(odp_pktio_t hdl)
{
	pktio_entry_t *entry;
	odp_pktio_capability_t capa;
	uint8_t addr[ETH_ALEN];
	int max_len = 512;
	char str[max_len];
	int len = 0;
	int n = max_len - 1;

	entry = get_pktio_entry(hdl);
	if (entry == NULL) {
		ODP_DBG("pktio entry %" PRIuPTR " does not exist\n", (uintptr_t)hdl);
		return;
	}

	len += snprintf(&str[len], n - len, "Pktio info\n----------\n");
	len += snprintf(&str[len], n - len,
			"  name              %s\n", entry->s.name);
	len += snprintf(&str[len], n - len,
			"  type              %s\n", entry->s.ops->name);
	len += snprintf(&str[len], n - len,
			"  index             %i\n", odp_pktio_index(hdl));
	len += snprintf(&str[len], n - len,
			"  handle            0x%" PRIx64 "\n",
			odp_pktio_to_u64(hdl));
	len += snprintf(&str[len], n - len,
			"  pool handle       0x%" PRIx64 "\n",
			odp_pool_to_u64(entry->s.pool));
	len += snprintf(&str[len], n - len,
			"  state             %s\n",
			entry->s.state ==  PKTIO_STATE_STARTED ? "start" :
		       (entry->s.state ==  PKTIO_STATE_STOPPED ? "stop" :
		       (entry->s.state ==  PKTIO_STATE_STOP_PENDING ?
			"stop pending" :
		       (entry->s.state ==  PKTIO_STATE_OPENED ? "opened" :
								"unknown"))));
	memset(addr, 0, sizeof(addr));
	odp_pktio_mac_addr(hdl, addr, ETH_ALEN);
	len += snprintf(&str[len], n - len,
			"  mac               %02x:%02x:%02x:%02x:%02x:%02x\n",
			addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
	len += snprintf(&str[len], n - len,
			"  pktin maxlen      %" PRIu32 "\n",
			odp_pktin_maxlen(hdl));
	len += snprintf(&str[len], n - len,
			"  pktout maxlen     %" PRIu32 "\n",
			odp_pktout_maxlen(hdl));
	len += snprintf(&str[len], n - len,
			"  promisc           %s\n",
			odp_pktio_promisc_mode(hdl) ? "yes" : "no");

	if (!odp_pktio_capability(hdl, &capa)) {
		len += snprintf(&str[len], n - len, "  max input queues  %u\n",
				capa.max_input_queues);
		len += snprintf(&str[len], n - len, "  max output queues %u\n",
				capa.max_output_queues);
	}

	str[len] = '\0';

	ODP_PRINT("\n%s", str);

	if (entry->s.ops->print)
		entry->s.ops->print(entry);

	ODP_PRINT("\n");
}

int _odp_pktio_term_global(void)
{
	odp_shm_t shm;
	int i, pktio_if;
	int ret = 0;

	if (pktio_global == NULL)
		return 0;

	for (i = 0; i < ODP_CONFIG_PKTIO_ENTRIES; ++i) {
		pktio_entry_t *pktio_entry;

		pktio_entry = &pktio_global->entries[i];

		if (is_free(pktio_entry))
			continue;

		lock_entry(pktio_entry);
		if (pktio_entry->s.state == PKTIO_STATE_STARTED) {
			ret = _pktio_stop(pktio_entry);
			if (ret)
				ODP_ABORT("unable to stop pktio %s\n",
					  pktio_entry->s.name);
		}

		if (pktio_entry->s.state != PKTIO_STATE_CLOSE_PENDING)
			ret = _pktio_close(pktio_entry);
		if (ret)
			ODP_ABORT("unable to close pktio %s\n",
				  pktio_entry->s.name);
		unlock_entry(pktio_entry);
	}

	for (pktio_if = 0; _odp_pktio_if_ops[pktio_if]; ++pktio_if) {
		if (_odp_pktio_if_ops[pktio_if]->term)
			if (_odp_pktio_if_ops[pktio_if]->term())
				ODP_ABORT("failed to terminate pktio type %d",
					  pktio_if);
	}

	if (_ODP_PCAPNG) {
		ret = _odp_pcapng_term_global();
		if (ret)
			ODP_ERR("Failed to terminate pcapng\n");
	}

	shm = pktio_global->shm;
	ret = odp_shm_free(shm);
	if (ret != 0)
		ODP_ERR("shm free failed\n");

	return ret;
}

static
int single_capability(odp_pktio_capability_t *capa)
{
	memset(capa, 0, sizeof(odp_pktio_capability_t));
	capa->max_input_queues  = 1;
	capa->max_output_queues = 1;
	capa->set_op.op.promisc_mode = 1;

	return 0;
}

int odp_pktio_capability(odp_pktio_t pktio, odp_pktio_capability_t *capa)
{
	pktio_entry_t *entry;
	int ret;

	entry = get_pktio_entry(pktio);
	if (entry == NULL) {
		ODP_DBG("pktio entry %" PRIuPTR " does not exist\n", (uintptr_t)pktio);
		return -1;
	}

	if (entry->s.ops->capability)
		ret = entry->s.ops->capability(entry, capa);
	else
		ret = single_capability(capa);

	if (ret == 0) {
		uint32_t mtu = pktio_maxlen(pktio);

		if (mtu == 0) {
			ODP_DBG("MTU query failed: %s\n", entry->s.name);
			return -1;
		}

		/* The same parser is used for all pktios */
		capa->config.parser.layer = ODP_PROTO_LAYER_ALL;
		/* Header skip is not supported */
		capa->set_op.op.skip_offset = 0;
		/* Irrespective of whether we optimize the fast path or not,
		 * we can report that it is supported.
		 */
		capa->config.pktout.bit.no_packet_refs = 1;

		/* LSO implementation is common to all pktios */
		capa->lso.max_profiles           = PKTIO_LSO_PROFILES;
		capa->lso.max_profiles_per_pktio = PKTIO_LSO_PROFILES;
		capa->lso.max_packet_segments    = PKT_MAX_SEGS;
		capa->lso.max_segments           = PKTIO_LSO_MAX_SEGMENTS;
		capa->lso.max_payload_len        = mtu - PKTIO_LSO_MIN_PAYLOAD_OFFSET;
		capa->lso.max_payload_offset     = PKTIO_LSO_MAX_PAYLOAD_OFFSET;
		capa->lso.max_num_custom         = ODP_LSO_MAX_CUSTOM;
		capa->lso.proto.ipv4             = 1;
		capa->lso.proto.custom           = 1;
		capa->lso.mod_op.add_segment_num = 1;

		capa->config.pktout.bit.tx_compl_ena = 1;
		capa->tx_compl.queue_type_sched = 1;
		capa->tx_compl.queue_type_plain = 1;
		capa->tx_compl.mode_all = 1;
	}

	/* Packet vector generation is common for all pktio types */
	if (ret == 0 && (entry->s.param.in_mode ==  ODP_PKTIN_MODE_QUEUE ||
			 entry->s.param.in_mode ==  ODP_PKTIN_MODE_SCHED)) {
		capa->vector.supported = ODP_SUPPORT_YES;
		capa->vector.max_size = CONFIG_PACKET_VECTOR_MAX_SIZE;
		capa->vector.min_size = 1;
		capa->vector.max_tmo_ns = 0;
		capa->vector.min_tmo_ns = 0;
	}

	capa->reassembly.ip = false;
	capa->reassembly.ipv4 = false;
	capa->reassembly.ipv6 = false;

	return ret;
}

unsigned int odp_pktio_max_index(void)
{
	return ODP_CONFIG_PKTIO_ENTRIES - 1;
}

int odp_pktio_stats(odp_pktio_t pktio,
		    odp_pktio_stats_t *stats)
{
	pktio_entry_t *entry;
	int ret = -1;

	entry = get_pktio_entry(pktio);
	if (entry == NULL) {
		ODP_DBG("pktio entry %" PRIuPTR " does not exist\n", (uintptr_t)pktio);
		return -1;
	}

	lock_entry(entry);

	if (odp_unlikely(is_free(entry))) {
		unlock_entry(entry);
		ODP_DBG("already freed pktio\n");
		return -1;
	}

	if (entry->s.ops->stats)
		ret = entry->s.ops->stats(entry, stats);
	if (odp_likely(ret == 0)) {
		stats->in_discards += odp_atomic_load_u64(&entry->s.stats_extra.in_discards);
		stats->out_discards += odp_atomic_load_u64(&entry->s.stats_extra.out_discards);
	}
	unlock_entry(entry);

	return ret;
}

int odp_pktio_stats_reset(odp_pktio_t pktio)
{
	pktio_entry_t *entry;
	int ret = -1;

	entry = get_pktio_entry(pktio);
	if (entry == NULL) {
		ODP_DBG("pktio entry %" PRIuPTR " does not exist\n", (uintptr_t)pktio);
		return -1;
	}

	lock_entry(entry);

	if (odp_unlikely(is_free(entry))) {
		unlock_entry(entry);
		ODP_DBG("already freed pktio\n");
		return -1;
	}

	odp_atomic_store_u64(&entry->s.stats_extra.in_discards, 0);
	odp_atomic_store_u64(&entry->s.stats_extra.out_discards, 0);
	if (entry->s.ops->stats)
		ret = entry->s.ops->stats_reset(entry);
	unlock_entry(entry);

	return ret;
}

int odp_pktin_queue_stats(odp_pktin_queue_t queue,
			  odp_pktin_queue_stats_t *stats)
{
	pktio_entry_t *entry;
	odp_pktin_mode_t mode;
	int ret = -1;

	entry = get_pktio_entry(queue.pktio);
	if (entry == NULL) {
		ODP_ERR("pktio entry %" PRIuPTR " does not exist\n", (uintptr_t)queue.pktio);
		return -1;
	}

	lock_entry(entry);

	if (odp_unlikely(is_free(entry))) {
		unlock_entry(entry);
		ODP_ERR("pktio entry already freed\n");
		return -1;
	}

	mode = entry->s.param.in_mode;
	if (odp_unlikely(mode != ODP_PKTIN_MODE_DIRECT)) {
		unlock_entry(entry);
		ODP_ERR("invalid packet input mode: %d\n", mode);
		return -1;
	}

	if (entry->s.ops->pktin_queue_stats)
		ret = entry->s.ops->pktin_queue_stats(entry, queue.index, stats);

	unlock_entry(entry);

	return ret;
}

int odp_pktin_event_queue_stats(odp_pktio_t pktio, odp_queue_t queue,
				odp_pktin_queue_stats_t *stats)
{
	pktio_entry_t *entry;
	odp_pktin_mode_t mode;
	odp_pktin_queue_t pktin_queue;
	int ret = -1;

	entry = get_pktio_entry(pktio);
	if (entry == NULL) {
		ODP_ERR("pktio entry %" PRIuPTR " does not exist\n", (uintptr_t)pktio);
		return -1;
	}

	lock_entry(entry);

	if (odp_unlikely(is_free(entry))) {
		unlock_entry(entry);
		ODP_ERR("pktio entry already freed\n");
		return -1;
	}

	mode = entry->s.param.in_mode;
	if (odp_unlikely(mode != ODP_PKTIN_MODE_SCHED && mode != ODP_PKTIN_MODE_QUEUE)) {
		unlock_entry(entry);
		ODP_ERR("invalid packet input mode: %d\n", mode);
		return -1;
	}

	pktin_queue = _odp_queue_fn->get_pktin(queue);

	if (entry->s.ops->pktin_queue_stats)
		ret = entry->s.ops->pktin_queue_stats(entry, pktin_queue.index, stats);

	unlock_entry(entry);

	return ret;
}

int odp_pktout_queue_stats(odp_pktout_queue_t queue,
			   odp_pktout_queue_stats_t *stats)
{
	pktio_entry_t *entry;
	odp_pktout_mode_t mode;
	int ret = -1;

	entry = get_pktio_entry(queue.pktio);
	if (entry == NULL) {
		ODP_ERR("pktio entry %" PRIuPTR " does not exist\n", (uintptr_t)queue.pktio);
		return -1;
	}

	lock_entry(entry);

	if (odp_unlikely(is_free(entry))) {
		unlock_entry(entry);
		ODP_ERR("pktio entry already freed\n");
		return -1;
	}

	mode = entry->s.param.out_mode;
	if (odp_unlikely(mode != ODP_PKTOUT_MODE_DIRECT)) {
		unlock_entry(entry);
		ODP_ERR("invalid packet output mode: %d\n", mode);
		return -1;
	}

	if (entry->s.ops->pktout_queue_stats)
		ret = entry->s.ops->pktout_queue_stats(entry, queue.index, stats);

	unlock_entry(entry);

	return ret;
}

int odp_pktout_event_queue_stats(odp_pktio_t pktio, odp_queue_t queue,
				 odp_pktout_queue_stats_t *stats)
{
	pktio_entry_t *entry;
	odp_pktout_mode_t mode;
	odp_pktout_queue_t pktout_queue;
	int ret = -1;

	entry = get_pktio_entry(pktio);
	if (entry == NULL) {
		ODP_ERR("pktio entry %" PRIuPTR " does not exist\n", (uintptr_t)pktio);
		return -1;
	}

	lock_entry(entry);

	if (odp_unlikely(is_free(entry))) {
		unlock_entry(entry);
		ODP_ERR("pktio entry already freed\n");
		return -1;
	}

	mode = entry->s.param.out_mode;
	if (odp_unlikely(mode != ODP_PKTOUT_MODE_QUEUE)) {
		unlock_entry(entry);
		ODP_ERR("invalid packet output mode: %d\n", mode);
		return -1;
	}

	pktout_queue = _odp_queue_fn->get_pktout(queue);

	if (entry->s.ops->pktout_queue_stats)
		ret = entry->s.ops->pktout_queue_stats(entry, pktout_queue.index, stats);

	unlock_entry(entry);

	return ret;
}

int odp_pktio_extra_stat_info(odp_pktio_t pktio,
			      odp_pktio_extra_stat_info_t info[], int num)
{
	pktio_entry_t *entry;
	int ret = 0;

	entry = get_pktio_entry(pktio);
	if (entry == NULL) {
		ODP_ERR("pktio entry %" PRIuPTR " does not exist\n", (uintptr_t)pktio);
		return -1;
	}

	lock_entry(entry);

	if (odp_unlikely(is_free(entry))) {
		unlock_entry(entry);
		ODP_ERR("already freed pktio\n");
		return -1;
	}

	if (entry->s.ops->extra_stat_info)
		ret = entry->s.ops->extra_stat_info(entry, info, num);

	unlock_entry(entry);

	return ret;
}

int odp_pktio_extra_stats(odp_pktio_t pktio, uint64_t stats[], int num)
{
	pktio_entry_t *entry;
	int ret = 0;

	entry = get_pktio_entry(pktio);
	if (entry == NULL) {
		ODP_ERR("pktio entry %" PRIuPTR " does not exist\n", (uintptr_t)pktio);
		return -1;
	}

	lock_entry(entry);

	if (odp_unlikely(is_free(entry))) {
		unlock_entry(entry);
		ODP_ERR("already freed pktio\n");
		return -1;
	}

	if (entry->s.ops->extra_stats)
		ret = entry->s.ops->extra_stats(entry, stats, num);

	unlock_entry(entry);

	return ret;
}

int odp_pktio_extra_stat_counter(odp_pktio_t pktio, uint32_t id, uint64_t *stat)
{
	pktio_entry_t *entry;
	int ret = -1;

	entry = get_pktio_entry(pktio);
	if (entry == NULL) {
		ODP_ERR("pktio entry %" PRIuPTR " does not exist\n", (uintptr_t)pktio);
		return -1;
	}

	lock_entry(entry);

	if (odp_unlikely(is_free(entry))) {
		unlock_entry(entry);
		ODP_ERR("already freed pktio\n");
		return -1;
	}

	if (entry->s.ops->extra_stat_counter)
		ret = entry->s.ops->extra_stat_counter(entry, id, stat);

	unlock_entry(entry);

	return ret;
}

void odp_pktio_extra_stats_print(odp_pktio_t pktio)
{
	int num_info, num_stats, i;

	num_info = odp_pktio_extra_stat_info(pktio, NULL, 0);
	if (num_info <= 0)
		return;

	num_stats = odp_pktio_extra_stats(pktio, NULL, 0);
	if (num_stats <= 0)
		return;

	if (num_info != num_stats) {
		ODP_ERR("extra statistics info counts not matching\n");
		return;
	}

	odp_pktio_extra_stat_info_t stats_info[num_stats];
	uint64_t extra_stats[num_stats];

	num_info = odp_pktio_extra_stat_info(pktio, stats_info, num_stats);
	if (num_info <= 0)
		return;

	num_stats = odp_pktio_extra_stats(pktio, extra_stats, num_stats);
	if (num_stats <= 0)
		return;

	if (num_info != num_stats) {
		ODP_ERR("extra statistics info counts not matching\n");
		return;
	}

	printf("Pktio extra statistics\n----------------------\n");
	for (i = 0; i < num_stats; i++)
		ODP_PRINT("  %s=%" PRIu64 "\n", stats_info[i].name, extra_stats[i]);
	ODP_PRINT("\n");
}

int odp_pktin_queue_config(odp_pktio_t pktio,
			   const odp_pktin_queue_param_t *param)
{
	pktio_entry_t *entry;
	odp_pktin_mode_t mode;
	odp_pktio_capability_t capa;
	unsigned int num_queues;
	unsigned int i;
	int rc;
	odp_queue_t queue;
	odp_pktin_queue_param_t default_param;

	if (param == NULL) {
		odp_pktin_queue_param_init(&default_param);
		param = &default_param;
	}

	entry = get_pktio_entry(pktio);
	if (entry == NULL) {
		ODP_DBG("pktio entry %" PRIuPTR " does not exist\n", (uintptr_t)pktio);
		return -1;
	}

	if (entry->s.state == PKTIO_STATE_STARTED) {
		ODP_DBG("pktio %s: not stopped\n", entry->s.name);
		return -1;
	}

	mode = entry->s.param.in_mode;

	/* Ignore the call when packet input is disabled. */
	if (mode == ODP_PKTIN_MODE_DISABLED)
		return 0;

	if (!param->classifier_enable && param->num_queues == 0) {
		ODP_DBG("invalid num_queues for operation mode\n");
		return -1;
	}

	num_queues = param->classifier_enable ? 1 : param->num_queues;

	rc = odp_pktio_capability(pktio, &capa);
	if (rc) {
		ODP_DBG("pktio %s: unable to read capabilities\n",
			entry->s.name);
		return -1;
	}

	entry->s.enabled.cls = !!param->classifier_enable;

	if (num_queues > capa.max_input_queues) {
		ODP_DBG("pktio %s: too many input queues\n", entry->s.name);
		return -1;
	}

	/* Validate packet vector parameters */
	if (param->vector.enable) {
		odp_pool_t pool = param->vector.pool;
		odp_pool_info_t pool_info;

		if (mode == ODP_PKTIN_MODE_DIRECT) {
			ODP_ERR("packet vectors not supported with ODP_PKTIN_MODE_DIRECT\n");
			return -1;
		}
		if (param->vector.max_size < capa.vector.min_size) {
			ODP_ERR("vector.max_size too small %" PRIu32 "\n",
				param->vector.max_size);
			return -1;
		}
		if (param->vector.max_size > capa.vector.max_size) {
			ODP_ERR("vector.max_size too large %" PRIu32 "\n",
				param->vector.max_size);
			return -1;
		}
		if (param->vector.max_tmo_ns > capa.vector.max_tmo_ns) {
			ODP_ERR("vector.max_tmo_ns too large %" PRIu64 "\n",
				param->vector.max_tmo_ns);
			return -1;
		}

		if (pool == ODP_POOL_INVALID || odp_pool_info(pool, &pool_info)) {
			ODP_ERR("invalid packet vector pool\n");
			return -1;
		}
		if (pool_info.params.type != ODP_POOL_VECTOR) {
			ODP_ERR("wrong pool type\n");
			return -1;
		}
		if (param->vector.max_size > pool_info.params.vector.max_size) {
			ODP_ERR("vector.max_size larger than pool max vector size\n");
			return -1;
		}
	}

	/* If re-configuring, destroy old queues */
	if (entry->s.num_in_queue)
		destroy_in_queues(entry, entry->s.num_in_queue);

	for (i = 0; i < num_queues; i++) {
		if (mode == ODP_PKTIN_MODE_QUEUE ||
		    mode == ODP_PKTIN_MODE_SCHED) {
			odp_queue_param_t queue_param;
			char name[ODP_QUEUE_NAME_LEN];
			int pktio_id = odp_pktio_index(pktio);
			odp_pktin_queue_param_ovr_t *queue_param_ovr = NULL;

			if (param->queue_param_ovr)
				queue_param_ovr = param->queue_param_ovr + i;

			snprintf(name, sizeof(name), "odp-pktin-%i-%i",
				 pktio_id, i);

			if (param->classifier_enable) {
				odp_queue_param_init(&queue_param);
			} else {
				memcpy(&queue_param, &param->queue_param,
				       sizeof(odp_queue_param_t));
				if (queue_param_ovr)
					queue_param.sched.group =
						queue_param_ovr->group;
			}

			queue_param.type = ODP_QUEUE_TYPE_PLAIN;

			if (mode == ODP_PKTIN_MODE_SCHED)
				queue_param.type = ODP_QUEUE_TYPE_SCHED;

			queue = odp_queue_create(name, &queue_param);

			if (queue == ODP_QUEUE_INVALID) {
				ODP_DBG("pktio %s: event queue create failed\n",
					entry->s.name);
				destroy_in_queues(entry, i + 1);
				return -1;
			}

			_odp_queue_fn->set_pktin(queue, pktio, i);
			if (mode == ODP_PKTIN_MODE_QUEUE)
				_odp_queue_fn->set_enq_deq_fn(queue,
							      NULL,
							      NULL,
							      pktin_dequeue,
							      pktin_deq_multi);

			entry->s.in_queue[i].queue = queue;

		} else {
			entry->s.in_queue[i].queue = ODP_QUEUE_INVALID;
		}

		entry->s.in_queue[i].pktin.index = i;
		entry->s.in_queue[i].pktin.pktio = entry->s.handle;
		entry->s.in_queue[i].vector = param->vector;
	}

	entry->s.num_in_queue = num_queues;

	if (entry->s.ops->input_queues_config)
		return entry->s.ops->input_queues_config(entry, param);

	return 0;
}

int _odp_pktio_pktout_tm_config(odp_pktio_t pktio_hdl,
				odp_pktout_queue_t *queue, bool reconf)
{
	odp_pktout_queue_param_t param;
	bool pktio_started = false;
	odp_pktout_mode_t mode;
	pktio_entry_t *entry;
	unsigned int i;
	int rc = 0;

	odp_pktout_queue_param_init(&param);
	param.num_queues = 1;

	entry = get_pktio_entry(pktio_hdl);
	if (entry == NULL) {
		ODP_DBG("pktio entry %" PRIuPTR " does not exist\n", (uintptr_t)pktio_hdl);
		return -1;
	}

	rc = -ENOTSUP;
	mode = entry->s.param.out_mode;
	/* Don't proceed further if mode is not TM */
	if (mode != ODP_PKTOUT_MODE_TM)
		return rc;

	/* Don't reconfigure unless requested */
	if (entry->s.num_out_queue && !reconf) {
		*queue = entry->s.out_queue[0].pktout;
		return 0;
	}

	if (entry->s.state == PKTIO_STATE_STARTED) {
		pktio_started = true;
		rc = odp_pktio_stop(pktio_hdl);
		if (rc) {
			ODP_ERR("Unable to stop pktio, rc=%d\n", rc);
			return rc;
		}
	}

	/* If re-configuring, destroy old queues */
	if (entry->s.num_out_queue) {
		destroy_out_queues(entry, entry->s.num_out_queue);
		entry->s.num_out_queue = 0;
	}

	init_out_queues(entry);
	for (i = 0; i < param.num_queues; i++) {
		entry->s.out_queue[i].pktout.index = i;
		entry->s.out_queue[i].pktout.pktio = pktio_hdl;
	}

	entry->s.num_out_queue = param.num_queues;

	rc = 0;
	if (entry->s.ops->output_queues_config) {
		rc = entry->s.ops->output_queues_config(entry, &param);
		if (rc)
			ODP_ERR("Unable to setup output queues, rc=%d\n", rc);
	}

	/* Return pktout queue on success */
	if (!rc)
		*queue = entry->s.out_queue[0].pktout;

	/* Take pktio back to its previous state */
	if (pktio_started)
		rc |= odp_pktio_start(pktio_hdl);
	return rc;
}

int odp_pktout_queue_config(odp_pktio_t pktio,
			    const odp_pktout_queue_param_t *param)
{
	pktio_entry_t *entry;
	odp_pktout_mode_t mode;
	odp_pktio_capability_t capa;
	unsigned int num_queues;
	unsigned int i;
	int rc;
	odp_pktout_queue_param_t default_param;

	if (param == NULL) {
		odp_pktout_queue_param_init(&default_param);
		param = &default_param;
	}

	entry = get_pktio_entry(pktio);
	if (entry == NULL) {
		ODP_ERR("pktio entry %" PRIuPTR " does not exist\n", (uintptr_t)pktio);
		return -1;
	}

	if (entry->s.state == PKTIO_STATE_STARTED) {
		ODP_ERR("pktio %s: not stopped\n", entry->s.name);
		return -1;
	}

	mode = entry->s.param.out_mode;

	/* Ignore the call when packet output is disabled, or routed through
	 * traffic manager. */
	if (mode == ODP_PKTOUT_MODE_DISABLED || mode == ODP_PKTOUT_MODE_TM)
		return 0;

	if (mode != ODP_PKTOUT_MODE_DIRECT && mode != ODP_PKTOUT_MODE_QUEUE) {
		ODP_ERR("pktio %s: bad packet output mode\n", entry->s.name);
		return -1;
	}

	num_queues = param->num_queues;

	if (num_queues == 0) {
		ODP_ERR("pktio %s: zero output queues\n", entry->s.name);
		return -1;
	}

	rc = odp_pktio_capability(pktio, &capa);
	if (rc) {
		ODP_ERR("pktio %s: unable to read capabilities\n",
			entry->s.name);
		return -1;
	}

	if (num_queues > capa.max_output_queues) {
		ODP_ERR("pktio %s: too many output queues\n", entry->s.name);
		return -1;
	}

	/* Check output queue sizes */
	for (i = 0; i < num_queues; i++) {
		uint32_t queue_size = param->queue_size[i];

		if (queue_size == 0)
			continue;

		if (capa.max_output_queue_size == 0) {
			ODP_ERR("pktio %s: configuring output queue size not supported\n",
				entry->s.name);
			return -1;
		}
		if (queue_size < capa.min_output_queue_size) {
			ODP_ERR("pktio %s: output queue size too small\n", entry->s.name);
			return -1;
		}
		if (queue_size > capa.max_output_queue_size) {
			ODP_ERR("pktio %s: output queue size too large\n", entry->s.name);
			return -1;
		}
	}

	/* If re-configuring, destroy old queues */
	if (entry->s.num_out_queue) {
		destroy_out_queues(entry, entry->s.num_out_queue);
		entry->s.num_out_queue = 0;
	}

	init_out_queues(entry);

	for (i = 0; i < num_queues; i++) {
		entry->s.out_queue[i].pktout.index = i;
		entry->s.out_queue[i].pktout.pktio = pktio;
	}

	entry->s.num_out_queue = num_queues;

	if (mode == ODP_PKTOUT_MODE_QUEUE) {
		for (i = 0; i < num_queues; i++) {
			odp_queue_t queue;
			odp_queue_param_t queue_param;
			char name[ODP_QUEUE_NAME_LEN];
			int pktio_id = odp_pktio_index(pktio);

			snprintf(name, sizeof(name), "odp-pktout-%i-%i",
				 pktio_id, i);

			odp_queue_param_init(&queue_param);
			/* Application cannot dequeue from the queue */
			queue_param.deq_mode = ODP_QUEUE_OP_DISABLED;

			queue = odp_queue_create(name, &queue_param);

			if (queue == ODP_QUEUE_INVALID) {
				ODP_ERR("pktout %s: event queue create failed\n",
					entry->s.name);
				destroy_out_queues(entry, i + 1);
				return -1;
			}

			_odp_queue_fn->set_pktout(queue, pktio, i);

			/* Override default enqueue / dequeue functions */
			_odp_queue_fn->set_enq_deq_fn(queue,
						      pktout_enqueue,
						      pktout_enq_multi,
						      NULL,
						      NULL);

			entry->s.out_queue[i].queue = queue;
		}
	}

	if (entry->s.ops->output_queues_config)
		return entry->s.ops->output_queues_config(entry, param);

	return 0;
}

int odp_pktin_event_queue(odp_pktio_t pktio, odp_queue_t queues[], int num)
{
	pktio_entry_t *entry;
	odp_pktin_mode_t mode;
	int i;
	int num_queues;

	entry = get_pktio_entry(pktio);
	if (entry == NULL) {
		ODP_DBG("pktio entry %" PRIuPTR " does not exist\n", (uintptr_t)pktio);
		return -1;
	}

	if (num < 0) {
		ODP_DBG("Bad param: num %i\n", num);
		return -1;
	}

	mode = entry->s.param.in_mode;

	if (mode == ODP_PKTIN_MODE_DISABLED)
		return 0;

	if (mode != ODP_PKTIN_MODE_QUEUE &&
	    mode != ODP_PKTIN_MODE_SCHED)
		return -1;

	num_queues = entry->s.num_in_queue;

	if (queues) {
		if (num_queues < num)
			num = num_queues;

		for (i = 0; i < num; i++)
			queues[i] = entry->s.in_queue[i].queue;
	}

	return num_queues;
}

int odp_pktin_queue(odp_pktio_t pktio, odp_pktin_queue_t queues[], int num)
{
	pktio_entry_t *entry;
	odp_pktin_mode_t mode;
	int i;
	int num_queues;

	entry = get_pktio_entry(pktio);
	if (entry == NULL) {
		ODP_DBG("pktio entry %" PRIuPTR " does not exist\n", (uintptr_t)pktio);
		return -1;
	}

	if (num < 0) {
		ODP_DBG("Bad param: num %i\n", num);
		return -1;
	}

	mode = entry->s.param.in_mode;

	if (mode == ODP_PKTIN_MODE_DISABLED)
		return 0;

	if (mode != ODP_PKTIN_MODE_DIRECT)
		return -1;

	num_queues = entry->s.num_in_queue;

	if (queues) {
		if (num_queues < num)
			num = num_queues;

		for (i = 0; i < num; i++)
			queues[i] = entry->s.in_queue[i].pktin;
	}

	return num_queues;
}

int odp_pktout_event_queue(odp_pktio_t pktio, odp_queue_t queues[], int num)
{
	pktio_entry_t *entry;
	odp_pktout_mode_t mode;
	int i;
	int num_queues;

	entry = get_pktio_entry(pktio);
	if (entry == NULL) {
		ODP_DBG("pktio entry %" PRIuPTR " does not exist\n", (uintptr_t)pktio);
		return -1;
	}

	mode = entry->s.param.out_mode;

	if (mode == ODP_PKTOUT_MODE_DISABLED)
		return 0;

	if (mode != ODP_PKTOUT_MODE_QUEUE)
		return -1;

	num_queues = entry->s.num_out_queue;

	if (queues && num > 0) {
		for (i = 0; i < num && i < num_queues; i++)
			queues[i] = entry->s.out_queue[i].queue;
	}

	return num_queues;
}

int odp_pktout_queue(odp_pktio_t pktio, odp_pktout_queue_t queues[], int num)
{
	pktio_entry_t *entry;
	odp_pktout_mode_t mode;
	int i;
	int num_queues;

	entry = get_pktio_entry(pktio);
	if (entry == NULL) {
		ODP_DBG("pktio entry %" PRIuPTR " does not exist\n", (uintptr_t)pktio);
		return -1;
	}

	mode = entry->s.param.out_mode;

	if (mode == ODP_PKTOUT_MODE_DISABLED)
		return 0;

	if (mode != ODP_PKTOUT_MODE_DIRECT)
		return -1;

	num_queues = entry->s.num_out_queue;

	if (queues && num > 0) {
		for (i = 0; i < num && i < num_queues; i++)
			queues[i] = entry->s.out_queue[i].pktout;
	}

	return num_queues;
}

static inline void _odp_dump_pcapng_pkts(pktio_entry_t *entry, int qidx,
					 const odp_packet_t packets[], int num)
{
	if (odp_unlikely(entry->s.pcapng.state[qidx] == PCAPNG_WR_PKT))
		_odp_pcapng_write_pkts(entry, qidx, packets, num);
}

int odp_pktin_recv(odp_pktin_queue_t queue, odp_packet_t packets[], int num)
{
	pktio_entry_t *entry;
	odp_pktio_t pktio = queue.pktio;
	int ret;

	entry = get_pktio_entry(pktio);
	if (entry == NULL) {
		ODP_DBG("pktio entry %" PRIuPTR " does not exist\n", (uintptr_t)pktio);
		return -1;
	}

	if (odp_unlikely(entry->s.state != PKTIO_STATE_STARTED))
		return 0;

	ret = entry->s.ops->recv(entry, queue.index, packets, num);
	if (_ODP_PCAPNG)
		_odp_dump_pcapng_pkts(entry, queue.index, packets, ret);

	return ret;
}

int odp_pktin_recv_tmo(odp_pktin_queue_t queue, odp_packet_t packets[], int num,
		       uint64_t wait)
{
	int ret;
	odp_time_t t1, t2;
	struct timespec ts;
	int started = 0;
	uint64_t sleep_round = 0;
	pktio_entry_t *entry;

	ts.tv_sec  = 0;
	ts.tv_nsec = 1000 * SLEEP_USEC;

	entry = get_pktio_entry(queue.pktio);
	if (entry == NULL) {
		ODP_DBG("pktio entry %" PRIuPTR " does not exist\n", (uintptr_t)queue.pktio);
		return -1;
	}

	if (odp_unlikely(entry->s.state != PKTIO_STATE_STARTED))
		return 0;

	if (entry->s.ops->recv_tmo && wait != ODP_PKTIN_NO_WAIT) {
		ret = entry->s.ops->recv_tmo(entry, queue.index, packets, num,
					      wait);
		if (_ODP_PCAPNG)
			_odp_dump_pcapng_pkts(entry, queue.index, packets, ret);

		return ret;
	}

	while (1) {
		ret = entry->s.ops->recv(entry, queue.index, packets, num);
		if (_ODP_PCAPNG)
			_odp_dump_pcapng_pkts(entry, queue.index, packets, ret);

		if (ret != 0 || wait == 0)
			return ret;

		/* Avoid unnecessary system calls. Record the start time
		 * only when needed and after the first call to recv. */
		if (odp_unlikely(!started)) {
			odp_time_t t;

			/* Avoid overflow issues for large wait times */
			if (wait > MAX_WAIT_TIME)
				wait = MAX_WAIT_TIME;
			t = odp_time_local_from_ns(wait * 1000);
			started = 1;
			t1 = odp_time_sum(odp_time_local(), t);
		}

		/* Check every SLEEP_CHECK rounds if total wait time
		 * has been exceeded. */
		if ((++sleep_round & (SLEEP_CHECK - 1)) == 0) {
			t2 = odp_time_local();

			if (odp_time_cmp(t2, t1) > 0)
				return 0;
		}
		wait = wait > SLEEP_USEC ? wait - SLEEP_USEC : 0;

		nanosleep(&ts, NULL);
	}
}

int odp_pktin_recv_mq_tmo(const odp_pktin_queue_t queues[], unsigned int num_q,
			  unsigned int *from, odp_packet_t packets[], int num,
			  uint64_t wait)
{
	unsigned int i;
	int ret;
	odp_time_t t1, t2;
	struct timespec ts;
	int started = 0;
	uint64_t sleep_round = 0;
	int trial_successful = 0;
	unsigned int lfrom = 0;

	for (i = 0; i < num_q; i++) {
		ret = odp_pktin_recv(queues[i], packets, num);

		if (ret > 0 && from)
			*from = i;

		if (ret != 0)
			return ret;
	}

	if (wait == 0)
		return 0;

	ret = _odp_sock_recv_mq_tmo_try_int_driven(queues, num_q, &lfrom,
						   packets, num, wait,
						   &trial_successful);
	if (ret > 0 && from)
		*from = lfrom;
	if (trial_successful) {
		if (_ODP_PCAPNG) {
			pktio_entry_t *entry;

			entry = get_pktio_entry(queues[lfrom].pktio);
			if (entry)
				_odp_dump_pcapng_pkts(entry, lfrom, packets,
						      ret);
		}

		return ret;
	}

	ts.tv_sec  = 0;
	ts.tv_nsec = 1000 * SLEEP_USEC;

	while (1) {
		for (i = 0; i < num_q; i++) {
			ret = odp_pktin_recv(queues[i], packets, num);

			if (ret > 0 && from)
				*from = i;

			if (ret != 0)
				return ret;
		}

		if (wait == 0)
			return 0;

		if (odp_unlikely(!started)) {
			odp_time_t t;

			/* Avoid overflow issues for large wait times */
			if (wait > MAX_WAIT_TIME)
				wait = MAX_WAIT_TIME;
			t = odp_time_local_from_ns(wait * 1000);
			started = 1;
			t1 = odp_time_sum(odp_time_local(), t);
		}

		/* Check every SLEEP_CHECK rounds if total wait time
		 * has been exceeded. */
		if ((++sleep_round & (SLEEP_CHECK - 1)) == 0) {
			t2 = odp_time_local();

			if (odp_time_cmp(t2, t1) > 0)
				return 0;
		}
		wait = wait > SLEEP_USEC ? wait - SLEEP_USEC : 0;

		nanosleep(&ts, NULL);
	}
}

uint64_t odp_pktin_wait_time(uint64_t nsec)
{
	if (nsec == 0)
		return 0;

	/* number of microseconds rounded up by one, so that
	 * recv_mq_tmo call waits at least 'nsec' nanoseconds. */
	return (nsec / (1000)) + 1;
}

static void check_tx_compl_ev(const odp_packet_hdr_t *hdr, int pkt_idx, tx_compl_info_t *info,
			      uint16_t *num)
{
	if (odp_unlikely(hdr->p.flags.tx_compl)) {
		info[*num].user_ptr = hdr->user_ptr;
		info[*num].queue = hdr->dst_queue;
		info[*num].idx = pkt_idx;
		(*num)++;
	}
}

static void send_tx_compl_event(odp_buffer_t buf, const void *user_ptr, odp_queue_t queue)
{
	_odp_pktio_tx_compl_t *data;
	odp_event_t ev;

	data = odp_buffer_addr(buf);
	data->user_ptr = user_ptr;
	ev = odp_buffer_to_event(buf);
	_odp_event_type_set(ev, ODP_EVENT_PACKET_TX_COMPL);

	if (odp_unlikely(odp_queue_enq(queue, ev))) {
		ODP_ERR("Failed to enqueue Tx completion event\n");
		odp_event_free(ev);
	}
}

static void send_tx_compl_events(tx_compl_info_t *info, uint16_t num, odp_buffer_t bufs[],
				 int num_sent)
{
	for (int i = 0; i < num; i++) {
		if (info[i].idx < num_sent) {
			send_tx_compl_event(bufs[i], info[i].user_ptr, info[i].queue);
		} else {
			odp_buffer_free_multi(&bufs[i], num - i);
			break;
		}
	}
}

int odp_pktout_send(odp_pktout_queue_t queue, const odp_packet_t packets[],
		    int num)
{
	pktio_entry_t *entry;
	odp_pktio_t pktio = queue.pktio;
	uint16_t num_tx_cevs = 0;
	tx_compl_info_t tx_compl_info[num];
	odp_buffer_t bufs[num];
	int num_to_send = num, num_sent;

	entry = get_pktio_entry(pktio);
	if (entry == NULL) {
		ODP_DBG("pktio entry %" PRIuPTR " does not exist\n", (uintptr_t)pktio);
		return -1;
	}

	if (odp_unlikely(entry->s.state != PKTIO_STATE_STARTED))
		return 0;

	if (_ODP_PCAPNG)
		_odp_dump_pcapng_pkts(entry, queue.index, packets, num);

	if (odp_unlikely(_odp_pktio_tx_compl_enabled(entry))) {
		for (int i = 0; i < num; i++)
			check_tx_compl_ev(packet_hdr(packets[i]), i, tx_compl_info, &num_tx_cevs);

		if (odp_unlikely(num_tx_cevs)) {
			int num_alloc = odp_buffer_alloc_multi(entry->s.tx_compl_pool, bufs,
							       num_tx_cevs);

			if (odp_unlikely(num_alloc < num_tx_cevs)) {
				if (odp_unlikely(num_alloc < 0))
					num_alloc = 0;

				num_to_send = tx_compl_info[num_alloc].idx;
				num_tx_cevs = num_alloc;
			}
		}
	}

	num_sent = entry->s.ops->send(entry, queue.index, packets, num_to_send);

	if (odp_unlikely(num_tx_cevs))
		send_tx_compl_events(tx_compl_info, num_tx_cevs, bufs, num_sent);

	return num_sent;
}

/** Get printable format of odp_pktio_t */
uint64_t odp_pktio_to_u64(odp_pktio_t hdl)
{
	return _odp_pri(hdl);
}

int odp_pktout_ts_read(odp_pktio_t hdl, odp_time_t *ts)
{
	pktio_entry_t *entry;
	uint64_t ts_val;

	entry = get_pktio_entry(hdl);
	if (odp_unlikely(entry == NULL)) {
		ODP_ERR("pktio entry %" PRIuPTR " does not exist\n", (uintptr_t)hdl);
		return -1;
	}

	if (odp_atomic_load_u64(&entry->s.tx_ts) == 0)
		return 1;

	ts_val = odp_atomic_xchg_u64(&entry->s.tx_ts, 0);
	if (odp_unlikely(ts_val == 0))
		return 1;

	ts->u64 = ts_val;
	return 0;
}

void odp_lso_profile_param_init(odp_lso_profile_param_t *param)
{
	memset(param, 0, sizeof(odp_lso_profile_param_t));

	param->lso_proto = ODP_LSO_PROTO_NONE;
}

odp_lso_profile_t odp_lso_profile_create(odp_pktio_t pktio, const odp_lso_profile_param_t *param)
{
	uint32_t i, num_custom, mod_op, offset, size;
	lso_profile_t *lso_prof = NULL;
	(void)pktio;

	/* Currently only IPv4 and custom implemented */
	if (param->lso_proto != ODP_LSO_PROTO_IPV4 &&
	    param->lso_proto != ODP_LSO_PROTO_CUSTOM) {
		ODP_ERR("Protocol not supported\n");
		return ODP_LSO_PROFILE_INVALID;
	}

	if (param->lso_proto == ODP_LSO_PROTO_CUSTOM) {
		num_custom = param->custom.num_custom;
		if (num_custom > ODP_LSO_MAX_CUSTOM) {
			ODP_ERR("Too many custom fields\n");
			return ODP_LSO_PROFILE_INVALID;
		}

		for (i = 0; i < num_custom; i++) {
			mod_op = param->custom.field[i].mod_op;
			offset = param->custom.field[i].offset;
			size   = param->custom.field[i].size;

			if (offset > PKTIO_LSO_MAX_PAYLOAD_OFFSET) {
				ODP_ERR("Too large custom field offset %u\n", offset);
				return ODP_LSO_PROFILE_INVALID;
			}

			/* Currently only segment number supported */
			if (mod_op != ODP_LSO_ADD_SEGMENT_NUM) {
				ODP_ERR("Custom modify operation %u not supported\n", mod_op);
				return ODP_LSO_PROFILE_INVALID;
			}

			if (size != 1 && size != 2 && size != 4 && size != 8) {
				ODP_ERR("Bad custom field size %u\n", size);
				return ODP_LSO_PROFILE_INVALID;
			}
		}
	}

	odp_spinlock_lock(&pktio_global->lock);

	if (pktio_global->num_lso_profiles >= PKTIO_LSO_PROFILES) {
		odp_spinlock_unlock(&pktio_global->lock);
		ODP_ERR("All LSO profiles used already: %u\n", PKTIO_LSO_PROFILES);
		return ODP_LSO_PROFILE_INVALID;
	}

	for (i = 0; i < PKTIO_LSO_PROFILES; i++) {
		if (pktio_global->lso_profile[i].used == 0) {
			lso_prof = &pktio_global->lso_profile[i];
			lso_prof->used = 1;
			pktio_global->num_lso_profiles++;
			break;
		}
	}

	odp_spinlock_unlock(&pktio_global->lock);

	if (lso_prof == NULL) {
		ODP_ERR("Did not find free LSO profile\n");
		return ODP_LSO_PROFILE_INVALID;
	}

	lso_prof->param = *param;
	lso_prof->index = i;

	return (odp_lso_profile_t)(uintptr_t)lso_prof;
}

odp_lso_profile_t _odp_lso_prof_from_idx(uint8_t idx)
{
	return (odp_lso_profile_t)(uintptr_t)&pktio_global->lso_profile[idx];
}

static inline lso_profile_t *lso_profile_ptr(odp_lso_profile_t handle)
{
	return (lso_profile_t *)(uintptr_t)handle;
}

int odp_lso_profile_destroy(odp_lso_profile_t lso_profile)
{
	lso_profile_t *lso_prof = lso_profile_ptr(lso_profile);

	if (lso_profile == ODP_LSO_PROFILE_INVALID || lso_prof->used == 0) {
		ODP_ERR("Bad handle\n");
		return -1;
	}

	odp_spinlock_lock(&pktio_global->lock);
	lso_prof->used = 0;
	pktio_global->num_lso_profiles--;
	odp_spinlock_unlock(&pktio_global->lock);

	return 0;
}

int odp_packet_lso_request(odp_packet_t pkt, const odp_packet_lso_opt_t *lso_opt)
{
	odp_packet_hdr_t *pkt_hdr = packet_hdr(pkt);
	lso_profile_t *lso_prof = lso_profile_ptr(lso_opt->lso_profile);
	uint32_t payload_offset = lso_opt->payload_offset;

	if (odp_unlikely(lso_opt->lso_profile == ODP_LSO_PROFILE_INVALID || lso_prof->used == 0)) {
		ODP_ERR("Bad LSO profile handle\n");
		return -1;
	}

	if (odp_unlikely(payload_offset > PKTIO_LSO_MAX_PAYLOAD_OFFSET)) {
		ODP_ERR("Too large LSO payload offset\n");
		return -1;
	}

	if (odp_unlikely((payload_offset + lso_opt->max_payload_len) > packet_len(pkt_hdr))) {
		ODP_ERR("LSO options larger than packet data length\n");
		return -1;
	}

	if (odp_packet_payload_offset_set(pkt, payload_offset)) {
		ODP_ERR("Payload offset set failed\n");
		return -1;
	}

	pkt_hdr->p.flags.lso     = 1;
	pkt_hdr->lso_max_payload = lso_opt->max_payload_len;
	pkt_hdr->lso_profile_idx = lso_prof->index;

	return 0;
}

static int lso_update_ipv4(odp_packet_t pkt, int index, int num_pkt,
			   uint32_t l3_offset, uint32_t payload_len)
{
	_odp_ipv4hdr_t *ipv4;
	uint32_t pkt_len = odp_packet_len(pkt);
	uint16_t tot_len = pkt_len - l3_offset;
	int ret = 0;
	uint16_t frag_offset;

	odp_packet_l3_offset_set(pkt, l3_offset);
	ipv4 = odp_packet_l3_ptr(pkt, NULL);
	ipv4->tot_len = odp_cpu_to_be_16(tot_len);

	/* IP payload offset in 8 byte blocks */
	frag_offset = ((uint32_t)index * payload_len) / 8;

	/* More fragments flag */
	if (index < (num_pkt - 1))
		frag_offset |= _ODP_IPV4HDR_FRAG_OFFSET_MORE_FRAGS;

	ipv4->frag_offset = odp_cpu_to_be_16(frag_offset);
	ret = _odp_packet_ipv4_chksum_insert(pkt);

	return ret;
}

static int lso_update_custom(lso_profile_t *lso_prof, odp_packet_t pkt, int segnum)
{
	void *ptr;
	int i, mod_op;
	uint32_t offset;
	uint8_t size;
	int num_custom = lso_prof->param.custom.num_custom;
	uint64_t u64 = 0;
	uint32_t u32 = 0;
	uint16_t u16 = 0;
	uint8_t  u8 = 0;

	for (i = 0; i < num_custom; i++) {
		mod_op = lso_prof->param.custom.field[i].mod_op;
		offset = lso_prof->param.custom.field[i].offset;
		size   = lso_prof->param.custom.field[i].size;

		if (size == 8)
			ptr = &u64;
		else if (size == 4)
			ptr = &u32;
		else if (size == 2)
			ptr = &u16;
		else
			ptr = &u8;

		if (odp_packet_copy_to_mem(pkt, offset, size, ptr)) {
			ODP_ERR("Read from packet failed at offset %u\n", offset);
			return -1;
		}

		if (mod_op == ODP_LSO_ADD_SEGMENT_NUM) {
			if (size == 8)
				u64 = odp_cpu_to_be_64(segnum + odp_be_to_cpu_64(u64));
			else if (size == 4)
				u32 = odp_cpu_to_be_32(segnum + odp_be_to_cpu_32(u32));
			else if (size == 2)
				u16 = odp_cpu_to_be_16(segnum + odp_be_to_cpu_16(u16));
			else
				u8 += segnum;
		}

		if (odp_packet_copy_from_mem(pkt, offset, size, ptr)) {
			ODP_ERR("Write to packet failed at offset %u\n", offset);
			return -1;
		}
	}

	return 0;
}

int _odp_lso_num_packets(odp_packet_t packet, const odp_packet_lso_opt_t *lso_opt,
			 uint32_t *len_out, uint32_t *left_over_out)
{
	uint32_t num_pkt, left_over, l3_offset, iphdr_len;
	odp_lso_profile_t lso_profile = lso_opt->lso_profile;
	lso_profile_t *lso_prof = lso_profile_ptr(lso_profile);
	uint32_t payload_len = lso_opt->max_payload_len;
	uint32_t hdr_len     = lso_opt->payload_offset;
	uint32_t pkt_len     = odp_packet_len(packet);
	uint32_t pkt_payload = pkt_len - hdr_len;

	if (odp_unlikely(hdr_len > PKTIO_LSO_MAX_PAYLOAD_OFFSET)) {
		ODP_ERR("Too large LSO payload offset\n");
		return -1;
	}

	if (odp_unlikely((hdr_len + payload_len) > pkt_len)) {
		ODP_ERR("LSO options larger than packet data length\n");
		return -1;
	}

	if (lso_prof->param.lso_proto == ODP_LSO_PROTO_IPV4) {
		l3_offset = odp_packet_l3_offset(packet);
		iphdr_len = hdr_len - l3_offset;

		if (l3_offset == ODP_PACKET_OFFSET_INVALID) {
			ODP_ERR("Invalid L3 offset\n");
			return -1;
		}

		if (hdr_len < l3_offset || iphdr_len < _ODP_IPV4HDR_LEN) {
			ODP_ERR("Bad payload or L3 offset\n");
			return -1;
		}

		/* Round down payload len to a multiple of 8 (on other than the last fragment). */
		payload_len = (payload_len / 8) * 8;
	}

	num_pkt = pkt_payload / payload_len;

	left_over = pkt_payload - (num_pkt * payload_len);
	if (left_over)
		num_pkt++;

	if (num_pkt > PKTIO_LSO_MAX_SEGMENTS) {
		ODP_ERR("Too many LSO segments %i. Maximum is %i\n", num_pkt,
			PKTIO_LSO_MAX_SEGMENTS);
		return -1;
	}

	*len_out       = payload_len;
	*left_over_out = left_over;

	return num_pkt;
}

int _odp_lso_create_packets(odp_packet_t packet, const odp_packet_lso_opt_t *lso_opt,
			    uint32_t payload_len, uint32_t left_over_len,
			    odp_packet_t pkt_out[], int num_pkt)
{
	int i, num;
	uint32_t offset;
	odp_packet_t pkt;
	odp_lso_profile_t lso_profile = lso_opt->lso_profile;
	lso_profile_t *lso_prof = lso_profile_ptr(lso_profile);
	const uint32_t hdr_len = lso_opt->payload_offset;
	const uint32_t pkt_len = hdr_len + payload_len;
	odp_pool_t pool = odp_packet_pool(packet);
	int num_free = 0;
	int num_full = num_pkt;

	if (left_over_len)
		num_full = num_pkt - 1;

	num = odp_packet_alloc_multi(pool, pkt_len, pkt_out, num_full);
	if (odp_unlikely(num < num_full)) {
		ODP_DBG("Alloc failed %i\n", num);
		if (num > 0) {
			num_free = num;
			goto error;
		}
	}

	if (left_over_len) {
		pkt = odp_packet_alloc(pool, hdr_len + left_over_len);
		if (pkt == ODP_PACKET_INVALID) {
			ODP_DBG("Alloc failed\n");
			num_free = num_full;
			goto error;
		}

		pkt_out[num_pkt - 1] = pkt;
	}

	num_free = num_pkt;

	/* Copy headers */
	for (i = 0; i < num_pkt; i++) {
		if (odp_packet_copy_from_pkt(pkt_out[i], 0, packet, 0, hdr_len)) {
			ODP_ERR("Header copy failed\n");
			goto error;
		}
	}

	/* Copy payload */
	for (i = 0; i < num_full; i++) {
		offset = hdr_len + (i * payload_len);
		if (odp_packet_copy_from_pkt(pkt_out[i], hdr_len, packet, offset, payload_len)) {
			ODP_ERR("Payload copy failed\n");
			goto error;
		}
	}

	/* Copy left over payload */
	if (left_over_len) {
		offset = hdr_len + (num_full * payload_len);
		if (odp_packet_copy_from_pkt(pkt_out[num_pkt - 1], hdr_len, packet, offset,
					     left_over_len)){
			ODP_ERR("Payload copy failed\n");
			goto error;
		}
	}

	if (lso_prof->param.lso_proto == ODP_LSO_PROTO_IPV4) {
		offset = odp_packet_l3_offset(packet);

		if (offset == ODP_PACKET_OFFSET_INVALID) {
			ODP_ERR("Invalid L3 offset\n");
			goto error;
		}

		for (i = 0; i < num_pkt; i++) {
			if (lso_update_ipv4(pkt_out[i], i, num_pkt, offset, payload_len)) {
				ODP_ERR("IPv4 header update failed. Packet %i.\n", i);
				goto error;
			}
		}
	} else {
		/* Update custom fields */
		int num_custom = lso_prof->param.custom.num_custom;

		for (i = 0; num_custom && i < num_pkt; i++) {
			if (lso_update_custom(lso_prof, pkt_out[i], i)) {
				ODP_ERR("Custom field update failed. Segment %i\n", i);
				goto error;
			}
		}
	}

	return 0;

error:
	odp_packet_free_multi(pkt_out, num_free);
	return -1;
}

static int pktout_send_lso(odp_pktout_queue_t queue, odp_packet_t packet,
			   const odp_packet_lso_opt_t *lso_opt)
{
	int ret, num_pkt;
	uint32_t payload_len, left_over_len;

	/* Calculate number of packets */
	num_pkt = _odp_lso_num_packets(packet, lso_opt, &payload_len, &left_over_len);
	if (odp_unlikely(num_pkt <= 0))
		return -1;

	/* Create packets */
	odp_packet_t pkt_out[num_pkt];

	ret = _odp_lso_create_packets(packet, lso_opt, payload_len, left_over_len, pkt_out,
				      num_pkt);

	if (odp_unlikely(ret))
		return -1;

	/* Send LSO packets */
	ret = odp_pktout_send(queue, pkt_out, num_pkt);

	if (ret < num_pkt) {
		int first_free = 0;
		int num_free = num_pkt;

		ODP_DBG("Packet send failed %i\n", ret);

		if (ret > 0) {
			first_free = ret;
			num_free = num_pkt - ret;
		}

		odp_packet_free_multi(&pkt_out[first_free], num_free);
		return -1;
	}

	/* Free original packet */
	odp_packet_free(packet);

	return 0;
}

int odp_pktout_send_lso(odp_pktout_queue_t queue, const odp_packet_t packet[], int num,
			const odp_packet_lso_opt_t *opt)
{
	int i;
	odp_packet_t pkt;
	odp_packet_lso_opt_t lso_opt;
	const odp_packet_lso_opt_t *opt_ptr = &lso_opt;

	if (odp_unlikely(num <= 0)) {
		ODP_ERR("No packets\n");
		return -1;
	}

	memset(&lso_opt, 0, sizeof(odp_packet_lso_opt_t));
	if (opt)
		opt_ptr = opt;

	for (i = 0; i < num; i++) {
		pkt = packet[i];

		if (opt == NULL) {
			odp_packet_hdr_t *pkt_hdr = packet_hdr(pkt);

			if (pkt_hdr->p.flags.lso == 0) {
				ODP_ERR("No LSO options on packet %i\n", i);
				if (i == 0)
					return -1;

				return i;
			}

			lso_opt.lso_profile     = _odp_lso_prof_from_idx(pkt_hdr->lso_profile_idx);
			lso_opt.payload_offset  = odp_packet_payload_offset(pkt);
			lso_opt.max_payload_len = pkt_hdr->lso_max_payload;
		}

		if (odp_unlikely(pktout_send_lso(queue, pkt, opt_ptr))) {
			ODP_DBG("LSO output failed on packet %i\n", i);
			return i;
		}
	}

	return i;
}

void _odp_pktio_allocate_and_send_tx_compl_events(const pktio_entry_t *entry,
						  const odp_packet_t packets[], int num)
{
	uint16_t num_tx_cevs = 0, num_alloc;
	int idx[num];
	odp_buffer_t bufs[num];
	odp_packet_hdr_t *hdr;

	for (int i = 0; i < num; i++)
		if (odp_unlikely(packet_hdr(packets[i])->p.flags.tx_compl))
			idx[num_tx_cevs++] = i;

	if (odp_unlikely(num_tx_cevs)) {
		num_alloc = odp_buffer_alloc_multi(entry->s.tx_compl_pool, bufs, num_tx_cevs);

		for (int i = 0; i < num_alloc; i++) {
			hdr = packet_hdr(packets[idx[i]]);
			send_tx_compl_event(bufs[i], hdr->user_ptr, hdr->dst_queue);
		}
	}
}

void
odp_proto_stats_param_init(odp_proto_stats_param_t *param)
{
	if (param)
		memset(param, 0, sizeof(*param));
}

int
odp_proto_stats_capability(odp_pktio_t pktio, odp_proto_stats_capability_t *capa)
{
	(void)pktio;

	if (capa == NULL)
		return -EINVAL;

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

	return 0;
}

odp_proto_stats_t
odp_proto_stats_lookup(const char *name)
{
	(void)name;

	return ODP_PROTO_STATS_INVALID;
}

odp_proto_stats_t
odp_proto_stats_create(const char *name, const odp_proto_stats_param_t *param)
{
	(void)name;
	(void)param;

	return ODP_PROTO_STATS_INVALID;
}

int
odp_proto_stats_destroy(odp_proto_stats_t stat)
{
	(void)stat;

	return 0;
}

int
odp_proto_stats(odp_proto_stats_t stat, odp_proto_stats_data_t *data)
{
	(void)stat;

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

	return 0;
}

void
odp_proto_stats_print(odp_proto_stats_t stat)
{
	(void)stat;
}