DPDK logo

Elixir Cross Referencer

   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
/*-
 *   BSD LICENSE
 * 
 *   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
 *   All rights reserved.
 * 
 *   Redistribution and use in source and binary forms, with or without
 *   modification, are permitted provided that the following conditions
 *   are met:
 * 
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in
 *       the documentation and/or other materials provided with the
 *       distribution.
 *     * Neither the name of Intel Corporation nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 * 
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <inttypes.h>
#include <sys/time.h>
#include <time.h>
#include <math.h>

#include "test.h"

#ifdef RTE_LIBRTE_SCHED

#include <rte_red.h>

#ifdef __INTEL_COMPILER
#pragma warning(disable:2259)       /* conversion may lose significant bits */
#pragma warning(disable:181)        /* Arg incompatible with format string */
#endif

#define DIM(x) (sizeof(x)/sizeof(x[0]))
#define TEST_HZ_PER_KHZ 1000
#define TEST_NSEC_MARGIN 500        /**< nanosecond margin when calculating clk freq */

#define MAX_QEMPTY_TIME_MSEC   50000
#define MSEC_PER_SEC           1000      /**< Milli-seconds per second */
#define USEC_PER_MSEC          1000      /**< Micro-seconds per milli-second */
#define USEC_PER_SEC           1000000   /**< Micro-seconds per second */

/**< structures for testing rte_red performance and function */
struct test_rte_red_config {        /**< Test structure for RTE_RED config */
	struct rte_red_config *rconfig; /**< RTE_RED configuration parameters */
	uint8_t num_cfg;                /**< Number of RTE_RED configs to test */
	uint8_t *wq_log2;               /**< Test wq_log2 value to use */
	uint32_t min_th;                /**< Queue minimum threshold */
	uint32_t max_th;                /**< Queue maximum threshold */
	uint8_t *maxp_inv;              /**< Inverse mark probability */
};

struct test_queue {                 /**< Test structure for RTE_RED Queues */
	struct rte_red *rdata;          /**< RTE_RED runtime data */
	uint32_t num_queues;            /**< Number of RTE_RED queues to test */
	uint32_t *qconfig;              /**< Configuration of RTE_RED queues for test */
	uint32_t *q;                    /**< Queue size */
	uint32_t q_ramp_up;             /**< Num of enqueues to ramp up the queue */
	uint32_t avg_ramp_up;           /**< Average num of enqueues to ramp up the queue */ 
	uint32_t avg_tolerance;         /**< Tolerance in queue average */
	double drop_tolerance;          /**< Drop tolerance of packets not enqueued */
};

struct test_var {                   /**< Test variables used for testing RTE_RED */
	uint32_t wait_usec;             /**< Micro second wait interval */
	uint32_t num_iterations;        /**< Number of test iterations */
	uint32_t num_ops;               /**< Number of test operations */
	uint64_t clk_freq;              /**< CPU clock frequency */
	uint32_t sleep_sec;             /**< Seconds to sleep */
	uint32_t *dropped;              /**< Test operations dropped */
	uint32_t *enqueued;             /**< Test operations enqueued */
};

struct test_config {                /**< Master test structure for RTE_RED */
	const char *ifname;             /**< Interface name */
	const char *msg;                /**< Test message for display */
	const char *htxt;               /**< Header txt display for result output */
	struct test_rte_red_config *tconfig; /**< Test structure for RTE_RED config */
	struct test_queue *tqueue;      /**< Test structure for RTE_RED Queues */
	struct test_var *tvar;          /**< Test variables used for testing RTE_RED */
	uint32_t *tlevel;               /**< Queue levels */
};

enum test_result {
	FAIL = 0,
	PASS
};

/**< Test structure to define tests to run */
struct tests {
	struct test_config *testcfg;
	enum test_result (*testfn)(struct test_config *);
};

struct rdtsc_prof {
	uint64_t clk_start;
	uint64_t clk_min;               /**< min clocks */
	uint64_t clk_max;               /**< max clocks */
	uint64_t clk_avgc;              /**< count to calc average */
	double clk_avg;                 /**< cumulative sum to calc average */
	const char *name;
};

static const uint64_t port_speed_bytes = (10ULL*1000ULL*1000ULL*1000ULL)/8ULL;
static double inv_cycles_per_byte = 0;
static double pkt_time_usec = 0;

static void init_port_ts(uint64_t cpu_clock)
{
	double cycles_per_byte = (double)(cpu_clock) / (double)(port_speed_bytes);
	inv_cycles_per_byte = 1.0 / cycles_per_byte;
	pkt_time_usec = 1000000.0 / ((double)port_speed_bytes / (double)RTE_RED_S);
}

static uint64_t get_port_ts(void)
{
	return (uint64_t)((double)rte_rdtsc() * inv_cycles_per_byte);
}

static void rdtsc_prof_init(struct rdtsc_prof *p, const char *name)
{
	p->clk_min = (uint64_t)(-1LL);
	p->clk_max = 0;
	p->clk_avg = 0;
	p->clk_avgc = 0;
	p->name = name;
}

static inline void rdtsc_prof_start(struct rdtsc_prof *p)
{
#ifdef __PIC__
    asm volatile (
    "mov %%ebx, %%edi\n"
    "cpuid\n"
    "xchgl %%ebx, %%edi;\n"
	: : : "%eax", "%edi", "%ecx", "%edx" );
#else
	asm( "cpuid" : : : "%eax", "%ebx", "%ecx", "%edx" );
#endif
	p->clk_start = rte_rdtsc();
}

static inline void rdtsc_prof_end(struct rdtsc_prof *p)
{
	uint64_t clk_start = rte_rdtsc() - p->clk_start;

	p->clk_avgc++;
	p->clk_avg += (double) clk_start;

	if (clk_start > p->clk_max)
		p->clk_max = clk_start;
	if (clk_start < p->clk_min)
		p->clk_min = clk_start;
}

static void rdtsc_prof_print(struct rdtsc_prof *p)
{
	if (p->clk_avgc>0) {
		printf("RDTSC stats for %s: n=%" PRIu64 ", min=%" PRIu64 ", max=%" PRIu64 ", avg=%.1f\n",
			p->name,
			p->clk_avgc,
			p->clk_min,
			p->clk_max,
			(p->clk_avg / ((double) p->clk_avgc)));
	}
}

static uint32_t rte_red_get_avg_int(const struct rte_red_config *red_cfg,
				    struct rte_red *red)
{
	/**
	 * scale by 1/n and convert from fixed-point to integer
	 */
	return red->avg >> (RTE_RED_SCALING + red_cfg->wq_log2);
}

static double rte_red_get_avg_float(const struct rte_red_config *red_cfg,
				    struct rte_red *red)
{
	/**
	 * scale by 1/n and convert from fixed-point to floating-point
	 */
	return ldexp((double)red->avg,  -(RTE_RED_SCALING + red_cfg->wq_log2));
}

static void rte_red_set_avg_int(const struct rte_red_config *red_cfg,
				struct rte_red *red,
				uint32_t avg)
{
	/**
	 * scale by n and convert from integer to fixed-point
	 */
	red->avg = avg << (RTE_RED_SCALING + red_cfg->wq_log2);
}

static double calc_exp_avg_on_empty(double avg, uint32_t n, uint32_t time_diff)
{
	return avg * pow((1.0 - 1.0 / (double)n), (double)time_diff / pkt_time_usec);
}

static double calc_drop_rate(uint32_t enqueued, uint32_t dropped)
{
	return (double)dropped / ((double)enqueued + (double)dropped);
}

/**
 * calculate the drop probability
 */
static double calc_drop_prob(uint32_t min_th, uint32_t max_th,
			     uint32_t maxp_inv, uint32_t avg)
{
	double drop_prob = 0.0;

	if (avg < min_th) {
		drop_prob = 0.0;
	} else if (avg < max_th) {
		drop_prob = (1.0 / (double)maxp_inv)
			* ((double)(avg - min_th)
			   / (double)(max_th - min_th));
	} else {
		drop_prob = 1.0;
	}
	return (drop_prob);
}

/**
 *  check if drop rate matches drop probability within tolerance
 */
static int check_drop_rate(double *diff, double drop_rate, double drop_prob, double tolerance)
{
	double abs_diff = 0.0;
	int ret = 1;

	abs_diff = fabs(drop_rate - drop_prob);
	if ((int)abs_diff == 0) {
	        *diff = 0.0;
	} else {
	        *diff = (abs_diff / drop_prob) * 100.0;
	        if (*diff > tolerance) {
	                ret = 0;
	        }
        }
	return (ret);
}

/**
 *  check if average queue size is within tolerance
 */
static int check_avg(double *diff, double avg, double exp_avg, double tolerance)
{
	double abs_diff = 0.0;
	int ret = 1;

	abs_diff = fabs(avg - exp_avg);
	if ((int)abs_diff == 0) {
	        *diff = 0.0;
	} else {
	        *diff = (abs_diff / exp_avg) * 100.0;
	        if (*diff > tolerance) {
	                ret = 0;
                }
	}
	return (ret);
}

/**
 * get the clk frequency in Hz
 */
static uint64_t get_machclk_freq(void)
{
	uint64_t start = 0;
	uint64_t end = 0;
	uint64_t diff = 0;
	uint64_t clk_freq_hz = 0;
	struct timespec tv_start = {0, 0}, tv_end = {0, 0};
	struct timespec req = {0, 0};

	req.tv_sec = 1;
	req.tv_nsec = 0;

	clock_gettime(CLOCK_REALTIME, &tv_start);
	start = rte_rdtsc();

	if (nanosleep(&req, NULL) != 0) {
		perror("get_machclk_freq()");
		exit(EXIT_FAILURE);
	}

	clock_gettime(CLOCK_REALTIME, &tv_end);
	end = rte_rdtsc();

	diff = (uint64_t)(tv_end.tv_sec - tv_start.tv_sec) * USEC_PER_SEC
		+ ((tv_end.tv_nsec - tv_start.tv_nsec + TEST_NSEC_MARGIN) / 
		   USEC_PER_MSEC); /**< diff is in micro secs */

	if (diff == 0)
		return(0);

	clk_freq_hz = ((end - start) * USEC_PER_SEC / diff);
	return (clk_freq_hz);
}

/**
 * initialize the test rte_red config
 */
static enum test_result
test_rte_red_init(struct test_config *tcfg)
{
	unsigned i = 0;

	tcfg->tvar->clk_freq = get_machclk_freq();
	init_port_ts( tcfg->tvar->clk_freq );

	for (i = 0; i < tcfg->tconfig->num_cfg; i++) {
		if (rte_red_config_init(&tcfg->tconfig->rconfig[i],
					(uint16_t)tcfg->tconfig->wq_log2[i],
					(uint16_t)tcfg->tconfig->min_th,
					(uint16_t)tcfg->tconfig->max_th,
					(uint16_t)tcfg->tconfig->maxp_inv[i]) != 0) {
			return(FAIL);
		}
	}

	*tcfg->tqueue->q = 0;
	*tcfg->tvar->dropped = 0;
	*tcfg->tvar->enqueued = 0;
	return(PASS);
}

/**
 * enqueue until actual queue size reaches target level
 */
static int
increase_actual_qsize(struct rte_red_config *red_cfg,
                      struct rte_red *red,
                      uint32_t *q,
                      uint32_t level,
                      uint32_t attempts)
{
        uint32_t i = 0;

        for (i = 0; i < attempts; i++) {
                int ret = 0;

                /**
                 * enqueue
                 */
                ret = rte_red_enqueue(red_cfg, red, *q, get_port_ts() );
                if (ret == 0) {
                        if (++(*q) >= level)
                                break;
                }
        }
        /**
        * check if target actual queue size has been reached
        */
        if (*q != level)
                return (-1);
        /**
         * success
         */
        return (0);
}

/**
 * enqueue until average queue size reaches target level
 */
static int
increase_average_qsize(struct rte_red_config *red_cfg,
                       struct rte_red *red,
                       uint32_t *q,
                       uint32_t level,
                       uint32_t num_ops)
{
        uint32_t avg = 0;
        uint32_t i = 0;

        for (i = 0; i < num_ops; i++) {
                /**
                 * enqueue
                 */
                rte_red_enqueue(red_cfg, red, *q, get_port_ts());
        }
        /**
         * check if target average queue size has been reached
         */
        avg = rte_red_get_avg_int(red_cfg, red);
        if (avg != level)
                return (-1);
        /**
         * success
         */
        return (0);
}

/**
 * setup default values for the functional test structures
 */
static struct rte_red_config ft_wrconfig[1];
static struct rte_red ft_rtdata[1];
static uint8_t ft_wq_log2[] = {9};
static uint8_t ft_maxp_inv[] = {10}; 
static uint32_t  ft_qconfig[] = {0, 0, 1, 1};
static uint32_t  ft_q[] ={0};
static uint32_t  ft_dropped[] ={0};
static uint32_t  ft_enqueued[] ={0};

static struct test_rte_red_config ft_tconfig =  {
	.rconfig = ft_wrconfig,
	.num_cfg = DIM(ft_wrconfig),
	.wq_log2 = ft_wq_log2,
	.min_th = 32,
	.max_th = 128,
	.maxp_inv = ft_maxp_inv,
};

static struct test_queue ft_tqueue = {
	.rdata = ft_rtdata,
	.num_queues = DIM(ft_rtdata),
	.qconfig = ft_qconfig,
	.q = ft_q,
	.q_ramp_up = 1000000,
	.avg_ramp_up = 1000000,
	.avg_tolerance = 5,  /* 5 percent */
	.drop_tolerance = 50,  /* 50 percent */
};

static struct test_var ft_tvar = {
	.wait_usec = 250000,
	.num_iterations = 20,
	.num_ops = 10000,
	.clk_freq = 0,
	.dropped = ft_dropped,
	.enqueued = ft_enqueued,
	.sleep_sec = (MAX_QEMPTY_TIME_MSEC / MSEC_PER_SEC) + 2,
};

/**
 * functional test enqueue/dequeue packets
 */
static void enqueue_dequeue_func(struct rte_red_config *red_cfg,
                                 struct rte_red *red,
                                 uint32_t *q,
                                 uint32_t num_ops,
                                 uint32_t *enqueued,
                                 uint32_t *dropped)
{
        uint32_t i = 0;

        for (i = 0; i < num_ops; i++) {
                int ret = 0;

                /**
                 * enqueue
                 */
                ret = rte_red_enqueue(red_cfg, red, *q, get_port_ts());
                if (ret == 0)
                        (*enqueued)++;
                else
                        (*dropped)++;
        }
}

/**
 * Test F1: functional test 1
 */
static uint32_t ft1_tlevels[] =  {6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72, 78, 84, 90, 96, 102, 108, 114, 120, 126, 132, 138, 144};

static struct test_config func_test1_config = {
	.ifname = "functional test 1 interface",
	.msg = "functional test 1 : use one rte_red configuration,\n"
	"		    increase average queue size to various levels,\n"
	"		    compare drop rate to drop probability\n\n",
	.htxt = "                "
	"avg queue size "
	"enqueued       "
	"dropped        "
	"drop prob %    "
	"drop rate %    "
	"diff %         "
	"tolerance %    "
	"\n",
	.tconfig = &ft_tconfig,
	.tqueue = &ft_tqueue,
	.tvar = &ft_tvar,
	.tlevel = ft1_tlevels,
};

static enum test_result func_test1(struct test_config *tcfg)
{
	enum test_result result = PASS;
	uint32_t i = 0;

	printf("%s", tcfg->msg);

	if (test_rte_red_init(tcfg) != PASS) {
		result = FAIL;
		goto out;
	}

	printf("%s", tcfg->htxt); 

	for (i = 0; i < DIM(ft1_tlevels); i++) {
		const char *label = NULL;
		uint32_t avg = 0;
		double drop_rate = 0.0;
		double drop_prob = 0.0;
		double diff = 0.0;

		/**
		 * reset rte_red run-time data
		 */
		rte_red_rt_data_init(tcfg->tqueue->rdata);
		*tcfg->tvar->enqueued = 0;
		*tcfg->tvar->dropped = 0;

		if (increase_actual_qsize(tcfg->tconfig->rconfig,
					  tcfg->tqueue->rdata,
					  tcfg->tqueue->q,
					  tcfg->tlevel[i],
					  tcfg->tqueue->q_ramp_up) != 0) {
			result = FAIL;
			goto out;
		}

		if (increase_average_qsize(tcfg->tconfig->rconfig,
					   tcfg->tqueue->rdata,
					   tcfg->tqueue->q,
					   tcfg->tlevel[i],
					   tcfg->tqueue->avg_ramp_up) != 0)  {
			result = FAIL;
			goto out;
		}

		enqueue_dequeue_func(tcfg->tconfig->rconfig,
				     tcfg->tqueue->rdata,
				     tcfg->tqueue->q,
				     tcfg->tvar->num_ops,
				     tcfg->tvar->enqueued,
				     tcfg->tvar->dropped);

		avg = rte_red_get_avg_int(tcfg->tconfig->rconfig, tcfg->tqueue->rdata);
		if (avg != tcfg->tlevel[i]) {
                        fprintf(stderr, "Fail: avg != level\n");
			result = FAIL;
                }

		drop_rate = calc_drop_rate(*tcfg->tvar->enqueued, *tcfg->tvar->dropped);
		drop_prob = calc_drop_prob(tcfg->tconfig->min_th, tcfg->tconfig->max_th,
					   *tcfg->tconfig->maxp_inv, tcfg->tlevel[i]);
		if (!check_drop_rate(&diff, drop_rate, drop_prob, (double)tcfg->tqueue->drop_tolerance))
		        result = FAIL;

		if (tcfg->tlevel[i] == tcfg->tconfig->min_th)
			label = "min thresh:     ";
		else if (tcfg->tlevel[i] == tcfg->tconfig->max_th)
			label = "max thresh:     ";
		else
			label = "                ";
		printf("%s%-15u%-15u%-15u%-15.4lf%-15.4lf%-15.4lf%-15.4lf\n",
		       label, avg, *tcfg->tvar->enqueued, *tcfg->tvar->dropped,
		       drop_prob * 100.0, drop_rate * 100.0, diff,
	               (double)tcfg->tqueue->drop_tolerance);
	}
out:
	return (result);
}

/**
 * Test F2: functional test 2
 */
static uint32_t ft2_tlevel[] = {127};
static uint8_t ft2_wq_log2[] = {9, 9, 9, 9, 9, 9, 9, 9, 9, 9};
static uint8_t ft2_maxp_inv[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
static struct rte_red_config ft2_rconfig[10];

static struct test_rte_red_config ft2_tconfig =  {
	.rconfig = ft2_rconfig,
	.num_cfg = DIM(ft2_rconfig),
	.wq_log2 = ft2_wq_log2,
	.min_th = 32,
	.max_th = 128,
	.maxp_inv = ft2_maxp_inv,
};

static struct test_config func_test2_config = {
	.ifname = "functional test 2 interface",
	.msg = "functional test 2 : use several RED configurations,\n"
	"		    increase average queue size to just below maximum threshold,\n"
	"		    compare drop rate to drop probability\n\n",
	.htxt = "RED config     "
	"avg queue size "
	"min threshold  "
	"max threshold  "
	"drop prob %    "
	"drop rate %    "
	"diff %         "
	"tolerance %    "
	"\n",
	.tconfig = &ft2_tconfig,
	.tqueue = &ft_tqueue,
	.tvar = &ft_tvar,
	.tlevel = ft2_tlevel,
};

static enum test_result func_test2(struct test_config *tcfg)
{
	enum test_result result = PASS;
        double prev_drop_rate = 1.0;
	uint32_t i = 0;

	printf("%s", tcfg->msg);

	if (test_rte_red_init(tcfg) != PASS) {
		result = FAIL;
		goto out;
	}
	rte_red_rt_data_init(tcfg->tqueue->rdata);

	if (increase_actual_qsize(tcfg->tconfig->rconfig,
				  tcfg->tqueue->rdata,
				  tcfg->tqueue->q,
				  *tcfg->tlevel,
				  tcfg->tqueue->q_ramp_up) != 0) {
		result = FAIL;
		goto out;
	}

	if (increase_average_qsize(tcfg->tconfig->rconfig,
				   tcfg->tqueue->rdata,
				   tcfg->tqueue->q,
				   *tcfg->tlevel,
				   tcfg->tqueue->avg_ramp_up) != 0) {
		result = FAIL;
		goto out;
	}
	printf("%s", tcfg->htxt);

	for (i = 0; i < tcfg->tconfig->num_cfg; i++) {
		uint32_t avg = 0;
		double drop_rate = 0.0;
		double drop_prob = 0.0;
		double diff = 0.0;

		*tcfg->tvar->dropped = 0;
		*tcfg->tvar->enqueued = 0;

		enqueue_dequeue_func(&tcfg->tconfig->rconfig[i],
				     tcfg->tqueue->rdata,
				     tcfg->tqueue->q,
				     tcfg->tvar->num_ops,
				     tcfg->tvar->enqueued,
				     tcfg->tvar->dropped);

		avg = rte_red_get_avg_int(&tcfg->tconfig->rconfig[i], tcfg->tqueue->rdata);
		if (avg != *tcfg->tlevel)
			result = FAIL;

		drop_rate = calc_drop_rate(*tcfg->tvar->enqueued, *tcfg->tvar->dropped);
		drop_prob = calc_drop_prob(tcfg->tconfig->min_th, tcfg->tconfig->max_th,
					   tcfg->tconfig->maxp_inv[i], *tcfg->tlevel);
		if (!check_drop_rate(&diff, drop_rate, drop_prob, (double)tcfg->tqueue->drop_tolerance))
		        result = FAIL;
	        /**
	         * drop rate should decrease as maxp_inv increases
	         */
	        if (drop_rate > prev_drop_rate)
	                result = FAIL;
	        prev_drop_rate = drop_rate;

		printf("%-15u%-15u%-15u%-15u%-15.4lf%-15.4lf%-15.4lf%-15.4lf\n",
		       i, avg, tcfg->tconfig->min_th, tcfg->tconfig->max_th,
		       drop_prob * 100.0, drop_rate * 100.0, diff,
	               (double)tcfg->tqueue->drop_tolerance);
	}
out:
	return (result);
}

/**
 * Test F3: functional test 3
 */
static uint32_t ft3_tlevel[] = {1022};

static struct test_rte_red_config ft3_tconfig =  {
	.rconfig = ft_wrconfig,
	.num_cfg = DIM(ft_wrconfig),
	.wq_log2 = ft_wq_log2,
	.min_th = 32,
	.max_th = 1023,
	.maxp_inv = ft_maxp_inv,
};

static struct test_config func_test3_config = {
	.ifname = "functional test 3 interface",
	.msg = "functional test 3 : use one RED configuration,\n"
	"		    increase average queue size to target level,\n"
	"		    dequeue all packets until queue is empty,\n"
	"		    confirm that average queue size is computed correctly while queue is empty\n\n",
	.htxt = "q avg before   "
	"q avg after    "
	"expected       "
	"difference %   "
	"tolerance %    "
	"result	 "
	"\n",
	.tconfig = &ft3_tconfig,
	.tqueue = &ft_tqueue,
	.tvar = &ft_tvar,
	.tlevel = ft3_tlevel,
};

static enum test_result func_test3(struct test_config *tcfg)
{
	enum test_result result = PASS;
	uint32_t i = 0;

	printf("%s", tcfg->msg);

	if (test_rte_red_init(tcfg) != PASS) {
		result = FAIL;
		goto out;
	}

	rte_red_rt_data_init(tcfg->tqueue->rdata);

	if (increase_actual_qsize(tcfg->tconfig->rconfig,
				  tcfg->tqueue->rdata,
				  tcfg->tqueue->q,
				  *tcfg->tlevel,
				  tcfg->tqueue->q_ramp_up) != 0) {
		result = FAIL;
		goto out;
	}

	if (increase_average_qsize(tcfg->tconfig->rconfig,
				   tcfg->tqueue->rdata,
				   tcfg->tqueue->q,
				   *tcfg->tlevel,
				   tcfg->tqueue->avg_ramp_up) != 0) {
		result = FAIL;
		goto out;
	}

	printf("%s", tcfg->htxt);

	for (i = 0; i < tcfg->tvar->num_iterations; i++) {
		double avg_before = 0;
		double avg_after = 0;
                double exp_avg = 0;
		double diff = 0.0;

		avg_before = rte_red_get_avg_float(tcfg->tconfig->rconfig, tcfg->tqueue->rdata);

		/**
		* empty the queue
		*/
		*tcfg->tqueue->q = 0;
		rte_red_mark_queue_empty(tcfg->tqueue->rdata, get_port_ts());

		rte_delay_us(tcfg->tvar->wait_usec);

		/**
		 * enqueue one packet to recalculate average queue size
		 */
		if (rte_red_enqueue(tcfg->tconfig->rconfig,
				    tcfg->tqueue->rdata,
				    *tcfg->tqueue->q,
				    get_port_ts()) == 0) {
			(*tcfg->tqueue->q)++;
		} else {
			printf("%s:%d: packet enqueued on empty queue was dropped\n", __func__, __LINE__);
			result = FAIL;
		}

		exp_avg = calc_exp_avg_on_empty(avg_before, 
					      (1 << *tcfg->tconfig->wq_log2),
					      tcfg->tvar->wait_usec);
		avg_after = rte_red_get_avg_float(tcfg->tconfig->rconfig, 
						  tcfg->tqueue->rdata);
		if (!check_avg(&diff, avg_after, exp_avg, (double)tcfg->tqueue->avg_tolerance))
		        result = FAIL;

		printf("%-15.4lf%-15.4lf%-15.4lf%-15.4lf%-15.4lf%-15s\n",
		       avg_before, avg_after, exp_avg, diff,
		       (double)tcfg->tqueue->avg_tolerance,
		       diff <= (double)tcfg->tqueue->avg_tolerance ? "pass" : "fail");
	}
out:
	return (result);
}

/**
 * Test F4: functional test 4
 */
static uint32_t ft4_tlevel[] = {1022};
static uint8_t ft4_wq_log2[] = {11};

static struct test_rte_red_config ft4_tconfig =  {
	.rconfig = ft_wrconfig,
	.num_cfg = DIM(ft_wrconfig),
	.min_th = 32,
	.max_th = 1023,
	.wq_log2 = ft4_wq_log2,
	.maxp_inv = ft_maxp_inv,
};

static struct test_queue ft4_tqueue = {
	.rdata = ft_rtdata,
	.num_queues = DIM(ft_rtdata),
	.qconfig = ft_qconfig,
	.q = ft_q,
	.q_ramp_up = 1000000,
	.avg_ramp_up = 1000000,
	.avg_tolerance = 0,  /* 0 percent */
	.drop_tolerance = 50,  /* 50 percent */
};

static struct test_config func_test4_config = {
	.ifname = "functional test 4 interface",
	.msg = "functional test 4 : use one RED configuration,\n"
	"		    increase average queue size to target level,\n"
	"		    dequeue all packets until queue is empty,\n"
	"		    confirm that average queue size is computed correctly while\n"
	"		    queue is empty for more than 50 sec,\n"
	"		    (this test takes 52 sec to run)\n\n",
	.htxt = "q avg before   "
	"q avg after    "
	"expected       "
	"difference %   "
	"tolerance %    "
	"result	 "
	"\n",
	.tconfig = &ft4_tconfig,
	.tqueue = &ft4_tqueue,
	.tvar = &ft_tvar,
	.tlevel = ft4_tlevel,
};

static enum test_result func_test4(struct test_config *tcfg)
{
	enum test_result result = PASS;
	uint64_t time_diff = 0;
	uint64_t start = 0;
	double avg_before = 0.0;
	double avg_after = 0.0;
        double exp_avg = 0.0;
        double diff = 0.0;

	printf("%s", tcfg->msg);

	if (test_rte_red_init(tcfg) != PASS) {
		result = FAIL;
		goto out;
	}

	rte_red_rt_data_init(tcfg->tqueue->rdata);

	if (increase_actual_qsize(tcfg->tconfig->rconfig,
				  tcfg->tqueue->rdata,
				  tcfg->tqueue->q,
				  *tcfg->tlevel,
				  tcfg->tqueue->q_ramp_up) != 0) {
		result = FAIL;
		goto out;
	}

	if (increase_average_qsize(tcfg->tconfig->rconfig,
				   tcfg->tqueue->rdata,
				   tcfg->tqueue->q,
				   *tcfg->tlevel,
				   tcfg->tqueue->avg_ramp_up) != 0) {
		result = FAIL;
		goto out;
	}

	printf("%s", tcfg->htxt);

	avg_before = rte_red_get_avg_float(tcfg->tconfig->rconfig, tcfg->tqueue->rdata);

	/**
	 * empty the queue
	 */
	*tcfg->tqueue->q = 0;
	rte_red_mark_queue_empty(tcfg->tqueue->rdata, get_port_ts());

	/**
	 * record empty time locally 
	 */
	start = rte_rdtsc();

	sleep(tcfg->tvar->sleep_sec);

	/**
	 * enqueue one packet to recalculate average queue size
	 */
	if (rte_red_enqueue(tcfg->tconfig->rconfig,  
			    tcfg->tqueue->rdata, 
			    *tcfg->tqueue->q,
			    get_port_ts()) != 0) {
		result = FAIL;
		goto out;
	}
	(*tcfg->tqueue->q)++;

	/**
	 * calculate how long queue has been empty
	 */
	time_diff = ((rte_rdtsc() - start) / tcfg->tvar->clk_freq)
		  * MSEC_PER_SEC;
	if (time_diff < MAX_QEMPTY_TIME_MSEC) {
		/**
		 * this could happen if sleep was interrupted for some reason
		 */
		result = FAIL;
		goto out;
	}

	/**
	 * confirm that average queue size is now at expected level
	 */
        exp_avg = 0.0;
	avg_after = rte_red_get_avg_float(tcfg->tconfig->rconfig, tcfg->tqueue->rdata);
	if (!check_avg(&diff, avg_after, exp_avg, (double)tcfg->tqueue->avg_tolerance))
	        result = FAIL;

	printf("%-15.4lf%-15.4lf%-15.4lf%-15.4lf%-15.4lf%-15s\n",
	       avg_before, avg_after, exp_avg,
	       diff, (double)tcfg->tqueue->avg_tolerance,
	       diff <= (double)tcfg->tqueue->avg_tolerance ? "pass" : "fail");
out:
	return (result);
}

/**
 * Test F5: functional test 5
 */
static uint32_t ft5_tlevel[] = {127};
static uint8_t ft5_wq_log2[] = {9, 8};
static uint8_t ft5_maxp_inv[] = {10, 20};
static struct rte_red_config ft5_config[2];
static struct rte_red ft5_data[4];
static uint32_t ft5_q[4];
static uint32_t ft5_dropped[] = {0, 0, 0, 0};
static uint32_t ft5_enqueued[] = {0, 0, 0, 0};

static struct test_rte_red_config ft5_tconfig =  {
	.rconfig = ft5_config,
	.num_cfg = DIM(ft5_config),
	.min_th = 32,
	.max_th = 128,
	.wq_log2 = ft5_wq_log2,
	.maxp_inv = ft5_maxp_inv,
};

static struct test_queue ft5_tqueue = {
	.rdata = ft5_data,
	.num_queues = DIM(ft5_data),
	.qconfig = ft_qconfig,
	.q = ft5_q,
	.q_ramp_up = 1000000,
	.avg_ramp_up = 1000000,
	.avg_tolerance = 5,  /* 10 percent */
	.drop_tolerance = 50,  /* 50 percent */
};

struct test_var ft5_tvar = {
	.wait_usec = 0,
	.num_iterations = 15,
	.num_ops = 10000,
	.clk_freq = 0,
	.dropped = ft5_dropped,
	.enqueued = ft5_enqueued,
	.sleep_sec = 0,
};

static struct test_config func_test5_config = {
	.ifname = "functional test 5 interface",
	.msg = "functional test 5 : use several queues (each with its own run-time data),\n"
	"		    use several RED configurations (such that each configuration is shared by multiple queues),\n"
	"		    increase average queue size to just below maximum threshold,\n"
	"		    compare drop rate to drop probability,\n"
	"		    (this is a larger scale version of functional test 2)\n\n",
	.htxt = "queue          "
	"config         "
	"avg queue size "
	"min threshold  "
	"max threshold  "
	"drop prob %    "
	"drop rate %    "
	"diff %         "
	"tolerance %    "
	"\n",
	.tconfig = &ft5_tconfig,
	.tqueue = &ft5_tqueue,
	.tvar = &ft5_tvar,
	.tlevel = ft5_tlevel,
};

static enum test_result func_test5(struct test_config *tcfg)
{
	enum test_result result = PASS;
	uint32_t j = 0;

	printf("%s", tcfg->msg);

	if (test_rte_red_init(tcfg) != PASS) {
		result = FAIL;
		goto out;
	}

	printf("%s", tcfg->htxt);

	for (j = 0; j < tcfg->tqueue->num_queues; j++) {
		rte_red_rt_data_init(&tcfg->tqueue->rdata[j]);
		tcfg->tqueue->q[j] = 0;

		if (increase_actual_qsize(&tcfg->tconfig->rconfig[tcfg->tqueue->qconfig[j]],
					  &tcfg->tqueue->rdata[j],
					  &tcfg->tqueue->q[j],
					  *tcfg->tlevel,
					  tcfg->tqueue->q_ramp_up) != 0) {
			result = FAIL;
			goto out;
		}

		if (increase_average_qsize(&tcfg->tconfig->rconfig[tcfg->tqueue->qconfig[j]],
					   &tcfg->tqueue->rdata[j],
					   &tcfg->tqueue->q[j],
					   *tcfg->tlevel,
					   tcfg->tqueue->avg_ramp_up) != 0) {
			result = FAIL;
			goto out;
		}
	}

	for (j = 0; j < tcfg->tqueue->num_queues; j++) {
		uint32_t avg = 0;
		double drop_rate = 0.0;
		double drop_prob = 0.0;
		double diff = 0.0;

		tcfg->tvar->dropped[j] = 0;
		tcfg->tvar->enqueued[j] = 0;

		enqueue_dequeue_func(&tcfg->tconfig->rconfig[tcfg->tqueue->qconfig[j]],
				     &tcfg->tqueue->rdata[j],
				     &tcfg->tqueue->q[j],
				     tcfg->tvar->num_ops,
				     &tcfg->tvar->enqueued[j],
				     &tcfg->tvar->dropped[j]);

		avg = rte_red_get_avg_int(&tcfg->tconfig->rconfig[tcfg->tqueue->qconfig[j]],
					  &tcfg->tqueue->rdata[j]);
		if (avg != *tcfg->tlevel)
			result = FAIL;

		drop_rate = calc_drop_rate(tcfg->tvar->enqueued[j],tcfg->tvar->dropped[j]);
		drop_prob = calc_drop_prob(tcfg->tconfig->min_th, tcfg->tconfig->max_th,
					   tcfg->tconfig->maxp_inv[tcfg->tqueue->qconfig[j]], 
					   *tcfg->tlevel);
		if (!check_drop_rate(&diff, drop_rate, drop_prob, (double)tcfg->tqueue->drop_tolerance))
		        result = FAIL;

		printf("%-15u%-15u%-15u%-15u%-15u%-15.4lf%-15.4lf%-15.4lf%-15.4lf\n",
		       j, tcfg->tqueue->qconfig[j], avg,
		       tcfg->tconfig->min_th, tcfg->tconfig->max_th,
		       drop_prob * 100.0, drop_rate * 100.0,
		       diff, (double)tcfg->tqueue->drop_tolerance);
	}
out:
	return (result);
}

/**
 * Test F6: functional test 6
 */
static uint32_t ft6_tlevel[] = {1022};
static uint8_t ft6_wq_log2[] = {9, 8};
static uint8_t ft6_maxp_inv[] = {10, 20};
static struct rte_red_config ft6_config[2];
static struct rte_red ft6_data[4];
static uint32_t ft6_q[4];

static struct test_rte_red_config ft6_tconfig =  {
	.rconfig = ft6_config,
	.num_cfg = DIM(ft6_config),
	.min_th = 32,
	.max_th = 1023,
	.wq_log2 = ft6_wq_log2,
	.maxp_inv = ft6_maxp_inv,
};

static struct test_queue ft6_tqueue = {
	.rdata = ft6_data,
	.num_queues = DIM(ft6_data),
	.qconfig = ft_qconfig,
	.q = ft6_q,
	.q_ramp_up = 1000000,
	.avg_ramp_up = 1000000,
	.avg_tolerance = 5,  /* 10 percent */
	.drop_tolerance = 50,  /* 50 percent */
};

static struct test_config func_test6_config = {
	.ifname = "functional test 6 interface",
	.msg = "functional test 6 : use several queues (each with its own run-time data),\n"
	"		    use several RED configurations (such that each configuration is sharte_red by multiple queues),\n"
	"		    increase average queue size to target level,\n"
	"		    dequeue all packets until queue is empty,\n"
	"		    confirm that average queue size is computed correctly while queue is empty\n"
	"		    (this is a larger scale version of functional test 3)\n\n",
	.htxt = "queue          "
	"config         "
	"q avg before   "
	"q avg after    "
	"expected       "
	"difference %   "
	"tolerance %    "
	"result	 ""\n",
	.tconfig = &ft6_tconfig,
	.tqueue = &ft6_tqueue,
	.tvar = &ft_tvar,
	.tlevel = ft6_tlevel,
};

static enum test_result func_test6(struct test_config *tcfg)
{
	enum test_result result = PASS;
	uint32_t j = 0;

	printf("%s", tcfg->msg);
	if (test_rte_red_init(tcfg) != PASS) {
		result = FAIL;
		goto out;
	}
	printf("%s", tcfg->htxt);

	for (j = 0; j < tcfg->tqueue->num_queues; j++) {
		rte_red_rt_data_init(&tcfg->tqueue->rdata[j]);
		tcfg->tqueue->q[j] = 0;

		if (increase_actual_qsize(&tcfg->tconfig->rconfig[tcfg->tqueue->qconfig[j]],
					  &tcfg->tqueue->rdata[j],
					  &tcfg->tqueue->q[j],
					  *tcfg->tlevel,
					  tcfg->tqueue->q_ramp_up) != 0) {
			result = FAIL;
			goto out;
		}
		if (increase_average_qsize(&tcfg->tconfig->rconfig[tcfg->tqueue->qconfig[j]],
					   &tcfg->tqueue->rdata[j],
					   &tcfg->tqueue->q[j],
					   *tcfg->tlevel,
					   tcfg->tqueue->avg_ramp_up) != 0) {
			result = FAIL;
			goto out;
		}
	}
	for (j = 0; j < tcfg->tqueue->num_queues; j++) {
		double avg_before = 0;
		double avg_after = 0;
		double exp_avg = 0;
		double diff = 0.0;

		avg_before = rte_red_get_avg_float(&tcfg->tconfig->rconfig[tcfg->tqueue->qconfig[j]], 
						   &tcfg->tqueue->rdata[j]);

		/**
		 * empty the queue
		 */
		tcfg->tqueue->q[j] = 0;
		rte_red_mark_queue_empty(&tcfg->tqueue->rdata[j], get_port_ts());
		rte_delay_us(tcfg->tvar->wait_usec);

		/**
		 * enqueue one packet to recalculate average queue size
		 */
		if (rte_red_enqueue(&tcfg->tconfig->rconfig[tcfg->tqueue->qconfig[j]], 
				    &tcfg->tqueue->rdata[j],
				    tcfg->tqueue->q[j],
				    get_port_ts()) == 0) {
			tcfg->tqueue->q[j]++;
		} else {
			printf("%s:%d: packet enqueued on empty queue was dropped\n", __func__, __LINE__);
			result = FAIL;
		}

		exp_avg = calc_exp_avg_on_empty(avg_before, 
				(1 << tcfg->tconfig->wq_log2[tcfg->tqueue->qconfig[j]]),
				tcfg->tvar->wait_usec);
		avg_after = rte_red_get_avg_float(&tcfg->tconfig->rconfig[tcfg->tqueue->qconfig[j]],
						&tcfg->tqueue->rdata[j]);
		if (!check_avg(&diff, avg_after, exp_avg, (double)tcfg->tqueue->avg_tolerance))
		        result = FAIL;

		printf("%-15u%-15u%-15.4lf%-15.4lf%-15.4lf%-15.4lf%-15.4lf%-15s\n",
		       j, tcfg->tqueue->qconfig[j], avg_before, avg_after,
		       exp_avg, diff, (double)tcfg->tqueue->avg_tolerance,
		       diff <= tcfg->tqueue->avg_tolerance ? "pass" : "fail");
	}
out:
	return (result);
}

/**
 * setup default values for the performance test structures
 */
static struct rte_red_config pt_wrconfig[1];
static struct rte_red pt_rtdata[1];
static uint8_t pt_wq_log2[] = {9};
static uint8_t pt_maxp_inv[] = {10}; 
static uint32_t pt_qconfig[] = {0};
static uint32_t pt_q[] = {0};
static uint32_t pt_dropped[] = {0};
static uint32_t pt_enqueued[] = {0};

static struct test_rte_red_config pt_tconfig =  {
	.rconfig = pt_wrconfig,
	.num_cfg = DIM(pt_wrconfig),
	.wq_log2 = pt_wq_log2,
	.min_th = 32,
	.max_th = 128,
	.maxp_inv = pt_maxp_inv,
};

static struct test_queue pt_tqueue = {
	.rdata = pt_rtdata,
	.num_queues = DIM(pt_rtdata),
	.qconfig = pt_qconfig,
	.q = pt_q,
	.q_ramp_up = 1000000,
	.avg_ramp_up = 1000000,
	.avg_tolerance = 5,  /* 10 percent */
	.drop_tolerance = 50,  /* 50 percent */
};

/**
 * enqueue/dequeue packets
 */
static void enqueue_dequeue_perf(struct rte_red_config *red_cfg,
				 struct rte_red *red,
				 uint32_t *q,
				 uint32_t num_ops,
				 uint32_t *enqueued,
				 uint32_t *dropped,
				 struct rdtsc_prof *prof)
{
	uint32_t i = 0;

	for (i = 0; i < num_ops; i++) {
		uint64_t ts = 0;
		int ret = 0;
		/**
		 * enqueue
		 */
		ts = get_port_ts();
		rdtsc_prof_start(prof);
		ret = rte_red_enqueue(red_cfg, red, *q, ts );
		rdtsc_prof_end(prof);
		if (ret == 0)
			(*enqueued)++;
		else
			(*dropped)++;
	}
}

/**
 * Setup test structures for tests P1, P2, P3 
 * performance tests 1, 2 and 3
 */
static uint32_t pt1_tlevel[] = {16};
static uint32_t pt2_tlevel[] = {80};
static uint32_t pt3_tlevel[] = {144};

static struct test_var perf1_tvar = {
	.wait_usec = 0,
	.num_iterations = 15,
	.num_ops = 50000000,
	.clk_freq = 0,
	.dropped = pt_dropped,
	.enqueued = pt_enqueued,
	.sleep_sec = 0
};

static struct test_config perf1_test1_config = {
	.ifname = "performance test 1 interface",
	.msg = "performance test 1 : use one RED configuration,\n"
	"		     set actual and average queue sizes to level below min threshold,\n"
	"		     measure enqueue performance\n\n",
	.tconfig = &pt_tconfig,
	.tqueue = &pt_tqueue,
	.tvar = &perf1_tvar,
	.tlevel = pt1_tlevel,
};

static struct test_config perf1_test2_config = {
	.ifname = "performance test 2 interface",
	.msg = "performance test 2 : use one RED configuration,\n"
	"		     set actual and average queue sizes to level in between min and max thresholds,\n"
	"		     measure enqueue performance\n\n",
	.tconfig = &pt_tconfig,
	.tqueue = &pt_tqueue,
	.tvar = &perf1_tvar,
	.tlevel = pt2_tlevel,
};

static struct test_config perf1_test3_config = {
	.ifname = "performance test 3 interface",
	.msg = "performance test 3 : use one RED configuration,\n"
	"		     set actual and average queue sizes to level above max threshold,\n"
	"		     measure enqueue performance\n\n",
	.tconfig = &pt_tconfig,
	.tqueue = &pt_tqueue,
	.tvar = &perf1_tvar,
	.tlevel = pt3_tlevel,
};

/**
 * Performance test function to measure enqueue performance. 
 * This runs performance tests 1, 2 and 3 
 */
static enum test_result perf1_test(struct test_config *tcfg)
{
	enum test_result result = PASS;
	struct rdtsc_prof prof = {0, 0, 0, 0, 0.0, NULL};
	uint32_t total = 0;

	printf("%s", tcfg->msg);

	rdtsc_prof_init(&prof, "enqueue");

	if (test_rte_red_init(tcfg) != PASS) {
		result = FAIL;
		goto out;
	}

	/**
	 * set average queue size to target level
	 */
	*tcfg->tqueue->q = *tcfg->tlevel;

	/**
	 * initialize the rte_red run time data structure
	 */
	rte_red_rt_data_init(tcfg->tqueue->rdata);

	/**
	 *  set the queue average
	 */
	rte_red_set_avg_int(tcfg->tconfig->rconfig, tcfg->tqueue->rdata, *tcfg->tlevel);
	if (rte_red_get_avg_int(tcfg->tconfig->rconfig, tcfg->tqueue->rdata) 
	    != *tcfg->tlevel) {
		result = FAIL;
		goto out;
	}

	enqueue_dequeue_perf(tcfg->tconfig->rconfig,
			     tcfg->tqueue->rdata,
			     tcfg->tqueue->q,
			     tcfg->tvar->num_ops,
			     tcfg->tvar->enqueued,
			     tcfg->tvar->dropped,
			     &prof);

	total = *tcfg->tvar->enqueued + *tcfg->tvar->dropped;

	printf("\ntotal: %u, enqueued: %u (%.2lf%%), dropped: %u (%.2lf%%)\n", total,
	       *tcfg->tvar->enqueued, ((double)(*tcfg->tvar->enqueued) / (double)total) * 100.0,
	       *tcfg->tvar->dropped, ((double)(*tcfg->tvar->dropped) / (double)total) * 100.0);

	rdtsc_prof_print(&prof);
out:
	return (result);
}

/**
 * Setup test structures for tests P4, P5, P6 
 * performance tests 4, 5 and 6
 */
static uint32_t pt4_tlevel[] = {16};
static uint32_t pt5_tlevel[] = {80};
static uint32_t pt6_tlevel[] = {144};

static struct test_var perf2_tvar = {
	.wait_usec = 500,
	.num_iterations = 10000,
	.num_ops = 10000,
	.dropped = pt_dropped,
	.enqueued = pt_enqueued,
	.sleep_sec = 0
};

static struct test_config perf2_test4_config = {
	.ifname = "performance test 4 interface",
	.msg = "performance test 4 : use one RED configuration,\n"
	"		     set actual and average queue sizes to level below min threshold,\n"
	"		     dequeue all packets until queue is empty,\n"
	"		     measure enqueue performance when queue is empty\n\n",
	.htxt = "iteration      "
	"q avg before   "
	"q avg after    "
	"expected       "
	"difference %   "
	"tolerance %    "
	"result	 ""\n",
	.tconfig = &pt_tconfig,
	.tqueue = &pt_tqueue,
	.tvar = &perf2_tvar,
	.tlevel = pt4_tlevel,
};

static struct test_config perf2_test5_config = {
	.ifname = "performance test 5 interface",
	.msg = "performance test 5 : use one RED configuration,\n"
	"		     set actual and average queue sizes to level in between min and max thresholds,\n"
	"		     dequeue all packets until queue is empty,\n"
	"		     measure enqueue performance when queue is empty\n\n",
	.htxt = "iteration      "
	"q avg before   "
	"q avg after    "
	"expected       "
	"difference     "
	"tolerance      "
	"result	 ""\n",
	.tconfig = &pt_tconfig,
	.tqueue = &pt_tqueue,
	.tvar = &perf2_tvar,
	.tlevel = pt5_tlevel,
};

static struct test_config perf2_test6_config = {
	.ifname = "performance test 6 interface",
	.msg = "performance test 6 : use one RED configuration,\n"
	"		     set actual and average queue sizes to level above max threshold,\n"
	"		     dequeue all packets until queue is empty,\n"
	"		     measure enqueue performance when queue is empty\n\n",
	.htxt = "iteration      "
	"q avg before   "
	"q avg after    "
	"expected       "
	"difference %   "
	"tolerance %    "
	"result	 ""\n",
	.tconfig = &pt_tconfig,
	.tqueue = &pt_tqueue,
	.tvar = &perf2_tvar,
	.tlevel = pt6_tlevel,
};

/**
 * Performance test function to measure enqueue performance when the 
 * queue is empty. This runs performance tests 4, 5 and 6 
 */
static enum test_result perf2_test(struct test_config *tcfg)
{
	enum test_result result = PASS;
	struct rdtsc_prof prof = {0, 0, 0, 0, 0.0, NULL};
	uint32_t total = 0;
	uint32_t i = 0;

	printf("%s", tcfg->msg);

	rdtsc_prof_init(&prof, "enqueue");

	if (test_rte_red_init(tcfg) != PASS) {
		result = FAIL;
		goto out;
	}

	printf("%s", tcfg->htxt); 

	for (i = 0; i < tcfg->tvar->num_iterations; i++) {
		uint32_t count = 0;
		uint64_t ts = 0;
		double avg_before = 0;
		int ret = 0;

		/**
		 * set average queue size to target level
		 */
		*tcfg->tqueue->q = *tcfg->tlevel;
		count = (*tcfg->tqueue->rdata).count;

		/**
		 * initialize the rte_red run time data structure
		 */
		rte_red_rt_data_init(tcfg->tqueue->rdata);
		(*tcfg->tqueue->rdata).count = count;

		/**
		 * set the queue average
		 */
		rte_red_set_avg_int(tcfg->tconfig->rconfig, tcfg->tqueue->rdata, *tcfg->tlevel);
		avg_before = rte_red_get_avg_float(tcfg->tconfig->rconfig, tcfg->tqueue->rdata);
		if ((avg_before < *tcfg->tlevel) || (avg_before > *tcfg->tlevel)) {
			result = FAIL;
			goto out;
		}

		/**
		 * empty the queue
		 */
		*tcfg->tqueue->q = 0;
		rte_red_mark_queue_empty(tcfg->tqueue->rdata, get_port_ts());

		/**
		 * wait for specified period of time
		 */
		rte_delay_us(tcfg->tvar->wait_usec);

		/**
		 * measure performance of enqueue operation while queue is empty
		 */
		ts = get_port_ts();
		rdtsc_prof_start(&prof);
		ret = rte_red_enqueue(tcfg->tconfig->rconfig, tcfg->tqueue->rdata, 
				      *tcfg->tqueue->q, ts );
		rdtsc_prof_end(&prof);

		/**
		 * gather enqueued/dropped statistics
		 */
		if (ret == 0)
			(*tcfg->tvar->enqueued)++;
		else
			(*tcfg->tvar->dropped)++;

		/**
		 * on first and last iteration, confirm that
		 * average queue size was computed correctly
		 */
		if ((i == 0) || (i == tcfg->tvar->num_iterations - 1)) {
			double avg_after = 0;
			double exp_avg = 0;
			double diff = 0.0;
			int ok = 0;

			avg_after = rte_red_get_avg_float(tcfg->tconfig->rconfig, tcfg->tqueue->rdata);
			exp_avg = calc_exp_avg_on_empty(avg_before, 
						  (1 << *tcfg->tconfig->wq_log2),
						  tcfg->tvar->wait_usec);
			if (check_avg(&diff, avg_after, exp_avg, (double)tcfg->tqueue->avg_tolerance))
		        	ok = 1;
			printf("%-15u%-15.4lf%-15.4lf%-15.4lf%-15.4lf%-15.4lf%-15s\n",
				i, avg_before, avg_after, exp_avg, diff,
				(double)tcfg->tqueue->avg_tolerance, ok ? "pass" : "fail");
			if (!ok) {
				result = FAIL;
				goto out;
			}
		}
	}
	total =  *tcfg->tvar->enqueued +  *tcfg->tvar->dropped;
	printf("\ntotal: %u, enqueued: %u (%.2lf%%), dropped: %u (%.2lf%%)\n", total,
	       *tcfg->tvar->enqueued, ((double)(*tcfg->tvar->enqueued) / (double)total) * 100.0,
	       *tcfg->tvar->dropped, ((double)(*tcfg->tvar->dropped) / (double)total) * 100.0);

	rdtsc_prof_print(&prof);
out:
	return (result);
}

/**
 * setup default values for overflow test structures
 */
static uint32_t avg_max = 0;
static uint32_t avg_max_bits = 0;

static struct rte_red_config ovfl_wrconfig[1];
static struct rte_red ovfl_rtdata[1];
static uint8_t ovfl_maxp_inv[] = {10}; 
static uint32_t ovfl_qconfig[] = {0, 0, 1, 1};
static uint32_t ovfl_q[] ={0};
static uint32_t ovfl_dropped[] ={0};
static uint32_t ovfl_enqueued[] ={0};
static uint32_t ovfl_tlevel[] = {1023};
static uint8_t ovfl_wq_log2[] = {12};

static struct test_rte_red_config ovfl_tconfig =  {
	.rconfig = ovfl_wrconfig,
	.num_cfg = DIM(ovfl_wrconfig),
	.wq_log2 = ovfl_wq_log2,
	.min_th = 32,
	.max_th = 1023,
	.maxp_inv = ovfl_maxp_inv,
};

static struct test_queue ovfl_tqueue = {
	.rdata = ovfl_rtdata,
	.num_queues = DIM(ovfl_rtdata),
	.qconfig = ovfl_qconfig,
	.q = ovfl_q,
	.q_ramp_up = 1000000,
	.avg_ramp_up = 1000000,
	.avg_tolerance = 5,  /* 10 percent */
	.drop_tolerance = 50,  /* 50 percent */
};

static struct test_var ovfl_tvar = {
	.wait_usec = 10000,
	.num_iterations = 1,
	.num_ops = 10000,
	.clk_freq = 0,
	.dropped = ovfl_dropped,
	.enqueued = ovfl_enqueued,
	.sleep_sec = 0
};

static void ovfl_check_avg(uint32_t avg)
{
	if (avg > avg_max) {
		double avg_log = 0;
		uint32_t bits = 0;
		avg_max = avg;
		avg_log = log(((double)avg_max));
		avg_log = avg_log / log(2.0);
		bits = (uint32_t)ceil(avg_log);
		if (bits > avg_max_bits)
			avg_max_bits = bits;
	}
}

static struct test_config ovfl_test1_config = {
	.ifname = "queue avergage overflow test interface",
	.msg = "overflow test 1 : use one RED configuration,\n"
	"		  increase average queue size to target level,\n"
	"		  check maximum number of bits requirte_red to represent avg_s\n\n",
	.htxt = "avg queue size  "
	"wq_log2  "
	"fraction bits  "
	"max queue avg  "
	"num bits  "
	"enqueued  "
	"dropped   "
	"drop prob %  "
	"drop rate %  "
	"\n",
	.tconfig = &ovfl_tconfig,
	.tqueue = &ovfl_tqueue,
	.tvar = &ovfl_tvar,
	.tlevel = ovfl_tlevel,
};

static enum test_result ovfl_test1(struct test_config *tcfg)
{
	enum test_result result = PASS;
	uint32_t avg = 0;
	uint32_t i = 0;
	double drop_rate = 0.0;
	double drop_prob = 0.0;
	double diff = 0.0;
	int ret = 0;

	printf("%s", tcfg->msg);

	if (test_rte_red_init(tcfg) != PASS) {

		result = FAIL;
		goto out;
	}

	/**
	 * reset rte_red run-time data
	 */
	rte_red_rt_data_init(tcfg->tqueue->rdata);

	/**
	 * increase actual queue size
	 */
	for (i = 0; i < tcfg->tqueue->q_ramp_up; i++) {
		ret = rte_red_enqueue(tcfg->tconfig->rconfig, tcfg->tqueue->rdata,
				      *tcfg->tqueue->q, get_port_ts());

		if (ret == 0) {
			if (++(*tcfg->tqueue->q) >= *tcfg->tlevel)
				break;
		}
	}

	/**
	 * enqueue
	 */
	for (i = 0; i < tcfg->tqueue->avg_ramp_up; i++) {
		ret = rte_red_enqueue(tcfg->tconfig->rconfig, tcfg->tqueue->rdata,
				      *tcfg->tqueue->q, get_port_ts());
		ovfl_check_avg((*tcfg->tqueue->rdata).avg);
		avg = rte_red_get_avg_int(tcfg->tconfig->rconfig, tcfg->tqueue->rdata);
		if (avg == *tcfg->tlevel) {
			if (ret == 0)
				(*tcfg->tvar->enqueued)++;
			else
				(*tcfg->tvar->dropped)++;
		}
	}

	/**
	 * check if target average queue size has been reached
	 */
	avg = rte_red_get_avg_int(tcfg->tconfig->rconfig, tcfg->tqueue->rdata);
	if (avg != *tcfg->tlevel) {
		result = FAIL;
		goto out;
	}

	/**
	 * check drop rate against drop probability
	 */
	drop_rate = calc_drop_rate(*tcfg->tvar->enqueued, *tcfg->tvar->dropped);
	drop_prob = calc_drop_prob(tcfg->tconfig->min_th,
				   tcfg->tconfig->max_th,
				   *tcfg->tconfig->maxp_inv,
				   *tcfg->tlevel);
	if (!check_drop_rate(&diff, drop_rate, drop_prob, (double)tcfg->tqueue->drop_tolerance))
	        result = FAIL;

	printf("%s", tcfg->htxt);
	
	printf("%-16u%-9u%-15u0x%08x     %-10u%-10u%-10u%-13.2lf%-13.2lf\n",
	       avg, *tcfg->tconfig->wq_log2, RTE_RED_SCALING,
	       avg_max, avg_max_bits,
	       *tcfg->tvar->enqueued, *tcfg->tvar->dropped,
	       drop_prob * 100.0, drop_rate * 100.0);
out:
	return (result);
}

/**
 * define the functional and performance tests to be executed
 */
struct tests func_tests[] = { 
	{ &func_test1_config, func_test1 },
	{ &func_test2_config, func_test2 },		
	{ &func_test3_config, func_test3 },
	{ &func_test4_config, func_test4 },
	{ &func_test5_config, func_test5 },
	{ &func_test6_config, func_test6 },
	{ &ovfl_test1_config, ovfl_test1 }, 
};

struct tests perf_tests[] = { 
	{ &perf1_test1_config, perf1_test },
	{ &perf1_test2_config, perf1_test },
	{ &perf1_test3_config, perf1_test },
	{ &perf2_test4_config, perf2_test },
	{ &perf2_test5_config, perf2_test },
	{ &perf2_test6_config, perf2_test },
};

/**
 * function to execute the required_red tests
 */
static void run_tests(struct tests *test_type, uint32_t test_count, uint32_t *num_tests, uint32_t *num_pass)
{
	enum test_result result = PASS;
	uint32_t i = 0;

	for (i = 0; i < test_count; i++) {
		printf("\n--------------------------------------------------------------------------------\n");
		result = test_type[i].testfn(test_type[i].testcfg);
		(*num_tests)++;
		if (result == PASS) {
			(*num_pass)++;
				printf("-------------------------------------<pass>-------------------------------------\n");
		} else {
			printf("-------------------------------------<fail>-------------------------------------\n");
		}
	}
	return;
}

/**
 * check if functions accept invalid parameters
 *
 * First, all functions will be called without initialized RED
 * Then, all of them will be called with NULL/invalid parameters
 *
 * Some functions are not tested as they are performance-critical and thus
 * don't do any parameter checking.
 */
static int
test_invalid_parameters(void)
{
	struct rte_red_config config;

	if (rte_red_rt_data_init(NULL) == 0) {
		printf("rte_red_rt_data_init should have failed!\n");
		return -1;
	}

	if (rte_red_config_init(NULL, 0, 0, 0, 0) == 0) {
		printf("rte_red_config_init should have failed!\n");
		return -1;
	}

	if (rte_red_rt_data_init(NULL) == 0) {
		printf("rte_red_rt_data_init should have failed!\n");
		return -1;
	}

	/* NULL config */
	if (rte_red_config_init(NULL, 0, 0, 0, 0) == 0) {
		printf("%i: rte_red_config_init should have failed!\n", __LINE__);
		return -1;
	}
	/* min_treshold == max_treshold */
	if (rte_red_config_init(&config, 0, 1, 1, 0) == 0) {
		printf("%i: rte_red_config_init should have failed!\n", __LINE__);
		return -1;
	}
	/* min_treshold > max_treshold */
	if (rte_red_config_init(&config, 0, 2, 1, 0) == 0) {
		printf("%i: rte_red_config_init should have failed!\n", __LINE__);
		return -1;
	}
	/* wq_log2 > RTE_RED_WQ_LOG2_MAX */
	if (rte_red_config_init(&config,
			RTE_RED_WQ_LOG2_MAX + 1, 1, 2, 0) == 0) {
		printf("%i: rte_red_config_init should have failed!\n", __LINE__);
		return -1;
	}
	/* wq_log2 < RTE_RED_WQ_LOG2_MIN */
	if (rte_red_config_init(&config,
			RTE_RED_WQ_LOG2_MIN - 1, 1, 2, 0) == 0) {
		printf("%i: rte_red_config_init should have failed!\n", __LINE__);
		return -1;
	}
	/* maxp_inv > RTE_RED_MAXP_INV_MAX */
	if (rte_red_config_init(&config,
			RTE_RED_WQ_LOG2_MIN, 1, 2, RTE_RED_MAXP_INV_MAX + 1) == 0) {
		printf("%i: rte_red_config_init should have failed!\n", __LINE__);
		return -1;
	}
	/* maxp_inv < RTE_RED_MAXP_INV_MIN */
	if (rte_red_config_init(&config,
			RTE_RED_WQ_LOG2_MIN, 1, 2, RTE_RED_MAXP_INV_MIN - 1) == 0) {
		printf("%i: rte_red_config_init should have failed!\n", __LINE__);
		return -1;
	}

	return 0;
}

int test_red(void)
{
	uint32_t num_tests = 0;
	uint32_t num_pass = 0;
	int ret = 0;

	if (test_invalid_parameters() < 0)
		return -1;

	run_tests(func_tests, DIM(func_tests), &num_tests, &num_pass);
	run_tests(perf_tests, DIM(perf_tests), &num_tests, &num_pass);

	if (num_pass == num_tests) {
		printf("[total: %u, pass: %u]\n", num_tests, num_pass);
		ret = 0;
	} else {
		printf("[total: %u, pass: %u, fail: %u]\n", num_tests, num_pass, num_tests - num_pass);
		ret = -1;
	}
	return (ret);
}

#else

int
test_red(void)
{
	printf("The SCHED library is not included in this build\n");
	return 0;
}

#endif