update Intel copyright years to 2014
[dpdk.git] / app / test / test_red.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <stdint.h>
38 #include <unistd.h>
39 #include <inttypes.h>
40 #include <sys/time.h>
41 #include <time.h>
42 #include <math.h>
43 #include <cmdline_parse.h>
44
45 #include "test.h"
46
47 #ifdef RTE_LIBRTE_SCHED
48
49 #include <rte_red.h>
50
51 #ifdef __INTEL_COMPILER
52 #pragma warning(disable:2259)       /* conversion may lose significant bits */
53 #pragma warning(disable:181)        /* Arg incompatible with format string */
54 #endif
55
56 #define DIM(x) (sizeof(x)/sizeof(x[0]))
57 #define TEST_HZ_PER_KHZ 1000
58 #define TEST_NSEC_MARGIN 500        /**< nanosecond margin when calculating clk freq */
59
60 #define MAX_QEMPTY_TIME_MSEC   50000
61 #define MSEC_PER_SEC           1000      /**< Milli-seconds per second */
62 #define USEC_PER_MSEC          1000      /**< Micro-seconds per milli-second */
63 #define USEC_PER_SEC           1000000   /**< Micro-seconds per second */
64
65 /**< structures for testing rte_red performance and function */
66 struct test_rte_red_config {        /**< Test structure for RTE_RED config */
67         struct rte_red_config *rconfig; /**< RTE_RED configuration parameters */
68         uint8_t num_cfg;                /**< Number of RTE_RED configs to test */
69         uint8_t *wq_log2;               /**< Test wq_log2 value to use */
70         uint32_t min_th;                /**< Queue minimum threshold */
71         uint32_t max_th;                /**< Queue maximum threshold */
72         uint8_t *maxp_inv;              /**< Inverse mark probability */
73 };
74
75 struct test_queue {                 /**< Test structure for RTE_RED Queues */
76         struct rte_red *rdata;          /**< RTE_RED runtime data */
77         uint32_t num_queues;            /**< Number of RTE_RED queues to test */
78         uint32_t *qconfig;              /**< Configuration of RTE_RED queues for test */
79         uint32_t *q;                    /**< Queue size */
80         uint32_t q_ramp_up;             /**< Num of enqueues to ramp up the queue */
81         uint32_t avg_ramp_up;           /**< Average num of enqueues to ramp up the queue */ 
82         uint32_t avg_tolerance;         /**< Tolerance in queue average */
83         double drop_tolerance;          /**< Drop tolerance of packets not enqueued */
84 };
85
86 struct test_var {                   /**< Test variables used for testing RTE_RED */
87         uint32_t wait_usec;             /**< Micro second wait interval */
88         uint32_t num_iterations;        /**< Number of test iterations */
89         uint32_t num_ops;               /**< Number of test operations */
90         uint64_t clk_freq;              /**< CPU clock frequency */
91         uint32_t sleep_sec;             /**< Seconds to sleep */
92         uint32_t *dropped;              /**< Test operations dropped */
93         uint32_t *enqueued;             /**< Test operations enqueued */
94 };
95
96 struct test_config {                /**< Master test structure for RTE_RED */
97         const char *ifname;             /**< Interface name */
98         const char *msg;                /**< Test message for display */
99         const char *htxt;               /**< Header txt display for result output */
100         struct test_rte_red_config *tconfig; /**< Test structure for RTE_RED config */
101         struct test_queue *tqueue;      /**< Test structure for RTE_RED Queues */
102         struct test_var *tvar;          /**< Test variables used for testing RTE_RED */
103         uint32_t *tlevel;               /**< Queue levels */
104 };
105
106 enum test_result {
107         FAIL = 0,
108         PASS
109 };
110
111 /**< Test structure to define tests to run */
112 struct tests {
113         struct test_config *testcfg;
114         enum test_result (*testfn)(struct test_config *);
115 };
116
117 struct rdtsc_prof {
118         uint64_t clk_start;
119         uint64_t clk_min;               /**< min clocks */
120         uint64_t clk_max;               /**< max clocks */
121         uint64_t clk_avgc;              /**< count to calc average */
122         double clk_avg;                 /**< cumulative sum to calc average */
123         const char *name;
124 };
125
126 static const uint64_t port_speed_bytes = (10ULL*1000ULL*1000ULL*1000ULL)/8ULL;
127 static double inv_cycles_per_byte = 0;
128 static double pkt_time_usec = 0;
129
130 static void init_port_ts(uint64_t cpu_clock)
131 {
132         double cycles_per_byte = (double)(cpu_clock) / (double)(port_speed_bytes);
133         inv_cycles_per_byte = 1.0 / cycles_per_byte;
134         pkt_time_usec = 1000000.0 / ((double)port_speed_bytes / (double)RTE_RED_S);
135 }
136
137 static uint64_t get_port_ts(void)
138 {
139         return (uint64_t)((double)rte_rdtsc() * inv_cycles_per_byte);
140 }
141
142 static void rdtsc_prof_init(struct rdtsc_prof *p, const char *name)
143 {
144         p->clk_min = (uint64_t)(-1LL);
145         p->clk_max = 0;
146         p->clk_avg = 0;
147         p->clk_avgc = 0;
148         p->name = name;
149 }
150
151 static inline void rdtsc_prof_start(struct rdtsc_prof *p)
152 {
153 #ifdef __PIC__
154     asm volatile (
155     "mov %%ebx, %%edi\n"
156     "cpuid\n"
157     "xchgl %%ebx, %%edi;\n"
158         : : : "%eax", "%edi", "%ecx", "%edx" );
159 #else
160         asm( "cpuid" : : : "%eax", "%ebx", "%ecx", "%edx" );
161 #endif
162         p->clk_start = rte_rdtsc();
163 }
164
165 static inline void rdtsc_prof_end(struct rdtsc_prof *p)
166 {
167         uint64_t clk_start = rte_rdtsc() - p->clk_start;
168
169         p->clk_avgc++;
170         p->clk_avg += (double) clk_start;
171
172         if (clk_start > p->clk_max)
173                 p->clk_max = clk_start;
174         if (clk_start < p->clk_min)
175                 p->clk_min = clk_start;
176 }
177
178 static void rdtsc_prof_print(struct rdtsc_prof *p)
179 {
180         if (p->clk_avgc>0) {
181                 printf("RDTSC stats for %s: n=%" PRIu64 ", min=%" PRIu64 ", max=%" PRIu64 ", avg=%.1f\n",
182                         p->name,
183                         p->clk_avgc,
184                         p->clk_min,
185                         p->clk_max,
186                         (p->clk_avg / ((double) p->clk_avgc)));
187         }
188 }
189
190 static uint32_t rte_red_get_avg_int(const struct rte_red_config *red_cfg,
191                                     struct rte_red *red)
192 {
193         /**
194          * scale by 1/n and convert from fixed-point to integer
195          */
196         return red->avg >> (RTE_RED_SCALING + red_cfg->wq_log2);
197 }
198
199 static double rte_red_get_avg_float(const struct rte_red_config *red_cfg,
200                                     struct rte_red *red)
201 {
202         /**
203          * scale by 1/n and convert from fixed-point to floating-point
204          */
205         return ldexp((double)red->avg,  -(RTE_RED_SCALING + red_cfg->wq_log2));
206 }
207
208 static void rte_red_set_avg_int(const struct rte_red_config *red_cfg,
209                                 struct rte_red *red,
210                                 uint32_t avg)
211 {
212         /**
213          * scale by n and convert from integer to fixed-point
214          */
215         red->avg = avg << (RTE_RED_SCALING + red_cfg->wq_log2);
216 }
217
218 static double calc_exp_avg_on_empty(double avg, uint32_t n, uint32_t time_diff)
219 {
220         return avg * pow((1.0 - 1.0 / (double)n), (double)time_diff / pkt_time_usec);
221 }
222
223 static double calc_drop_rate(uint32_t enqueued, uint32_t dropped)
224 {
225         return (double)dropped / ((double)enqueued + (double)dropped);
226 }
227
228 /**
229  * calculate the drop probability
230  */
231 static double calc_drop_prob(uint32_t min_th, uint32_t max_th,
232                              uint32_t maxp_inv, uint32_t avg)
233 {
234         double drop_prob = 0.0;
235
236         if (avg < min_th) {
237                 drop_prob = 0.0;
238         } else if (avg < max_th) {
239                 drop_prob = (1.0 / (double)maxp_inv)
240                         * ((double)(avg - min_th)
241                            / (double)(max_th - min_th));
242         } else {
243                 drop_prob = 1.0;
244         }
245         return (drop_prob);
246 }
247
248 /**
249  *  check if drop rate matches drop probability within tolerance
250  */
251 static int check_drop_rate(double *diff, double drop_rate, double drop_prob, double tolerance)
252 {
253         double abs_diff = 0.0;
254         int ret = 1;
255
256         abs_diff = fabs(drop_rate - drop_prob);
257         if ((int)abs_diff == 0) {
258                 *diff = 0.0;
259         } else {
260                 *diff = (abs_diff / drop_prob) * 100.0;
261                 if (*diff > tolerance) {
262                         ret = 0;
263                 }
264         }
265         return (ret);
266 }
267
268 /**
269  *  check if average queue size is within tolerance
270  */
271 static int check_avg(double *diff, double avg, double exp_avg, double tolerance)
272 {
273         double abs_diff = 0.0;
274         int ret = 1;
275
276         abs_diff = fabs(avg - exp_avg);
277         if ((int)abs_diff == 0) {
278                 *diff = 0.0;
279         } else {
280                 *diff = (abs_diff / exp_avg) * 100.0;
281                 if (*diff > tolerance) {
282                         ret = 0;
283                 }
284         }
285         return (ret);
286 }
287
288 /**
289  * get the clk frequency in Hz
290  */
291 static uint64_t get_machclk_freq(void)
292 {
293         uint64_t start = 0;
294         uint64_t end = 0;
295         uint64_t diff = 0;
296         uint64_t clk_freq_hz = 0;
297         struct timespec tv_start = {0, 0}, tv_end = {0, 0};
298         struct timespec req = {0, 0};
299
300         req.tv_sec = 1;
301         req.tv_nsec = 0;
302
303         clock_gettime(CLOCK_REALTIME, &tv_start);
304         start = rte_rdtsc();
305
306         if (nanosleep(&req, NULL) != 0) {
307                 perror("get_machclk_freq()");
308                 exit(EXIT_FAILURE);
309         }
310
311         clock_gettime(CLOCK_REALTIME, &tv_end);
312         end = rte_rdtsc();
313
314         diff = (uint64_t)(tv_end.tv_sec - tv_start.tv_sec) * USEC_PER_SEC
315                 + ((tv_end.tv_nsec - tv_start.tv_nsec + TEST_NSEC_MARGIN) / 
316                    USEC_PER_MSEC); /**< diff is in micro secs */
317
318         if (diff == 0)
319                 return(0);
320
321         clk_freq_hz = ((end - start) * USEC_PER_SEC / diff);
322         return (clk_freq_hz);
323 }
324
325 /**
326  * initialize the test rte_red config
327  */
328 static enum test_result
329 test_rte_red_init(struct test_config *tcfg)
330 {
331         unsigned i = 0;
332
333         tcfg->tvar->clk_freq = get_machclk_freq();
334         init_port_ts( tcfg->tvar->clk_freq );
335
336         for (i = 0; i < tcfg->tconfig->num_cfg; i++) {
337                 if (rte_red_config_init(&tcfg->tconfig->rconfig[i],
338                                         (uint16_t)tcfg->tconfig->wq_log2[i],
339                                         (uint16_t)tcfg->tconfig->min_th,
340                                         (uint16_t)tcfg->tconfig->max_th,
341                                         (uint16_t)tcfg->tconfig->maxp_inv[i]) != 0) {
342                         return(FAIL);
343                 }
344         }
345
346         *tcfg->tqueue->q = 0;
347         *tcfg->tvar->dropped = 0;
348         *tcfg->tvar->enqueued = 0;
349         return(PASS);
350 }
351
352 /**
353  * enqueue until actual queue size reaches target level
354  */
355 static int
356 increase_actual_qsize(struct rte_red_config *red_cfg,
357                       struct rte_red *red,
358                       uint32_t *q,
359                       uint32_t level,
360                       uint32_t attempts)
361 {
362         uint32_t i = 0;
363
364         for (i = 0; i < attempts; i++) {
365                 int ret = 0;
366
367                 /**
368                  * enqueue
369                  */
370                 ret = rte_red_enqueue(red_cfg, red, *q, get_port_ts() );
371                 if (ret == 0) {
372                         if (++(*q) >= level)
373                                 break;
374                 }
375         }
376         /**
377         * check if target actual queue size has been reached
378         */
379         if (*q != level)
380                 return (-1);
381         /**
382          * success
383          */
384         return (0);
385 }
386
387 /**
388  * enqueue until average queue size reaches target level
389  */
390 static int
391 increase_average_qsize(struct rte_red_config *red_cfg,
392                        struct rte_red *red,
393                        uint32_t *q,
394                        uint32_t level,
395                        uint32_t num_ops)
396 {
397         uint32_t avg = 0;
398         uint32_t i = 0;
399
400         for (i = 0; i < num_ops; i++) {
401                 /**
402                  * enqueue
403                  */
404                 rte_red_enqueue(red_cfg, red, *q, get_port_ts());
405         }
406         /**
407          * check if target average queue size has been reached
408          */
409         avg = rte_red_get_avg_int(red_cfg, red);
410         if (avg != level)
411                 return (-1);
412         /**
413          * success
414          */
415         return (0);
416 }
417
418 /**
419  * setup default values for the functional test structures
420  */
421 static struct rte_red_config ft_wrconfig[1];
422 static struct rte_red ft_rtdata[1];
423 static uint8_t ft_wq_log2[] = {9};
424 static uint8_t ft_maxp_inv[] = {10}; 
425 static uint32_t  ft_qconfig[] = {0, 0, 1, 1};
426 static uint32_t  ft_q[] ={0};
427 static uint32_t  ft_dropped[] ={0};
428 static uint32_t  ft_enqueued[] ={0};
429
430 static struct test_rte_red_config ft_tconfig =  {
431         .rconfig = ft_wrconfig,
432         .num_cfg = DIM(ft_wrconfig),
433         .wq_log2 = ft_wq_log2,
434         .min_th = 32,
435         .max_th = 128,
436         .maxp_inv = ft_maxp_inv,
437 };
438
439 static struct test_queue ft_tqueue = {
440         .rdata = ft_rtdata,
441         .num_queues = DIM(ft_rtdata),
442         .qconfig = ft_qconfig,
443         .q = ft_q,
444         .q_ramp_up = 1000000,
445         .avg_ramp_up = 1000000,
446         .avg_tolerance = 5,  /* 5 percent */
447         .drop_tolerance = 50,  /* 50 percent */
448 };
449
450 static struct test_var ft_tvar = {
451         .wait_usec = 250000,
452         .num_iterations = 20,
453         .num_ops = 10000,
454         .clk_freq = 0,
455         .dropped = ft_dropped,
456         .enqueued = ft_enqueued,
457         .sleep_sec = (MAX_QEMPTY_TIME_MSEC / MSEC_PER_SEC) + 2,
458 };
459
460 /**
461  * functional test enqueue/dequeue packets
462  */
463 static void enqueue_dequeue_func(struct rte_red_config *red_cfg,
464                                  struct rte_red *red,
465                                  uint32_t *q,
466                                  uint32_t num_ops,
467                                  uint32_t *enqueued,
468                                  uint32_t *dropped)
469 {
470         uint32_t i = 0;
471
472         for (i = 0; i < num_ops; i++) {
473                 int ret = 0;
474
475                 /**
476                  * enqueue
477                  */
478                 ret = rte_red_enqueue(red_cfg, red, *q, get_port_ts());
479                 if (ret == 0)
480                         (*enqueued)++;
481                 else
482                         (*dropped)++;
483         }
484 }
485
486 /**
487  * Test F1: functional test 1
488  */
489 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};
490
491 static struct test_config func_test1_config = {
492         .ifname = "functional test 1 interface",
493         .msg = "functional test 1 : use one rte_red configuration,\n"
494         "                   increase average queue size to various levels,\n"
495         "                   compare drop rate to drop probability\n\n",
496         .htxt = "                "
497         "avg queue size "
498         "enqueued       "
499         "dropped        "
500         "drop prob %    "
501         "drop rate %    "
502         "diff %         "
503         "tolerance %    "
504         "\n",
505         .tconfig = &ft_tconfig,
506         .tqueue = &ft_tqueue,
507         .tvar = &ft_tvar,
508         .tlevel = ft1_tlevels,
509 };
510
511 static enum test_result func_test1(struct test_config *tcfg)
512 {
513         enum test_result result = PASS;
514         uint32_t i = 0;
515
516         printf("%s", tcfg->msg);
517
518         if (test_rte_red_init(tcfg) != PASS) {
519                 result = FAIL;
520                 goto out;
521         }
522
523         printf("%s", tcfg->htxt); 
524
525         for (i = 0; i < DIM(ft1_tlevels); i++) {
526                 const char *label = NULL;
527                 uint32_t avg = 0;
528                 double drop_rate = 0.0;
529                 double drop_prob = 0.0;
530                 double diff = 0.0;
531
532                 /**
533                  * reset rte_red run-time data
534                  */
535                 rte_red_rt_data_init(tcfg->tqueue->rdata);
536                 *tcfg->tvar->enqueued = 0;
537                 *tcfg->tvar->dropped = 0;
538
539                 if (increase_actual_qsize(tcfg->tconfig->rconfig,
540                                           tcfg->tqueue->rdata,
541                                           tcfg->tqueue->q,
542                                           tcfg->tlevel[i],
543                                           tcfg->tqueue->q_ramp_up) != 0) {
544                         result = FAIL;
545                         goto out;
546                 }
547
548                 if (increase_average_qsize(tcfg->tconfig->rconfig,
549                                            tcfg->tqueue->rdata,
550                                            tcfg->tqueue->q,
551                                            tcfg->tlevel[i],
552                                            tcfg->tqueue->avg_ramp_up) != 0)  {
553                         result = FAIL;
554                         goto out;
555                 }
556
557                 enqueue_dequeue_func(tcfg->tconfig->rconfig,
558                                      tcfg->tqueue->rdata,
559                                      tcfg->tqueue->q,
560                                      tcfg->tvar->num_ops,
561                                      tcfg->tvar->enqueued,
562                                      tcfg->tvar->dropped);
563
564                 avg = rte_red_get_avg_int(tcfg->tconfig->rconfig, tcfg->tqueue->rdata);
565                 if (avg != tcfg->tlevel[i]) {
566                         fprintf(stderr, "Fail: avg != level\n");
567                         result = FAIL;
568                 }
569
570                 drop_rate = calc_drop_rate(*tcfg->tvar->enqueued, *tcfg->tvar->dropped);
571                 drop_prob = calc_drop_prob(tcfg->tconfig->min_th, tcfg->tconfig->max_th,
572                                            *tcfg->tconfig->maxp_inv, tcfg->tlevel[i]);
573                 if (!check_drop_rate(&diff, drop_rate, drop_prob, (double)tcfg->tqueue->drop_tolerance))
574                         result = FAIL;
575
576                 if (tcfg->tlevel[i] == tcfg->tconfig->min_th)
577                         label = "min thresh:     ";
578                 else if (tcfg->tlevel[i] == tcfg->tconfig->max_th)
579                         label = "max thresh:     ";
580                 else
581                         label = "                ";
582                 printf("%s%-15u%-15u%-15u%-15.4lf%-15.4lf%-15.4lf%-15.4lf\n",
583                        label, avg, *tcfg->tvar->enqueued, *tcfg->tvar->dropped,
584                        drop_prob * 100.0, drop_rate * 100.0, diff,
585                        (double)tcfg->tqueue->drop_tolerance);
586         }
587 out:
588         return (result);
589 }
590
591 /**
592  * Test F2: functional test 2
593  */
594 static uint32_t ft2_tlevel[] = {127};
595 static uint8_t ft2_wq_log2[] = {9, 9, 9, 9, 9, 9, 9, 9, 9, 9};
596 static uint8_t ft2_maxp_inv[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
597 static struct rte_red_config ft2_rconfig[10];
598
599 static struct test_rte_red_config ft2_tconfig =  {
600         .rconfig = ft2_rconfig,
601         .num_cfg = DIM(ft2_rconfig),
602         .wq_log2 = ft2_wq_log2,
603         .min_th = 32,
604         .max_th = 128,
605         .maxp_inv = ft2_maxp_inv,
606 };
607
608 static struct test_config func_test2_config = {
609         .ifname = "functional test 2 interface",
610         .msg = "functional test 2 : use several RED configurations,\n"
611         "                   increase average queue size to just below maximum threshold,\n"
612         "                   compare drop rate to drop probability\n\n",
613         .htxt = "RED config     "
614         "avg queue size "
615         "min threshold  "
616         "max threshold  "
617         "drop prob %    "
618         "drop rate %    "
619         "diff %         "
620         "tolerance %    "
621         "\n",
622         .tconfig = &ft2_tconfig,
623         .tqueue = &ft_tqueue,
624         .tvar = &ft_tvar,
625         .tlevel = ft2_tlevel,
626 };
627
628 static enum test_result func_test2(struct test_config *tcfg)
629 {
630         enum test_result result = PASS;
631         double prev_drop_rate = 1.0;
632         uint32_t i = 0;
633
634         printf("%s", tcfg->msg);
635
636         if (test_rte_red_init(tcfg) != PASS) {
637                 result = FAIL;
638                 goto out;
639         }
640         rte_red_rt_data_init(tcfg->tqueue->rdata);
641
642         if (increase_actual_qsize(tcfg->tconfig->rconfig,
643                                   tcfg->tqueue->rdata,
644                                   tcfg->tqueue->q,
645                                   *tcfg->tlevel,
646                                   tcfg->tqueue->q_ramp_up) != 0) {
647                 result = FAIL;
648                 goto out;
649         }
650
651         if (increase_average_qsize(tcfg->tconfig->rconfig,
652                                    tcfg->tqueue->rdata,
653                                    tcfg->tqueue->q,
654                                    *tcfg->tlevel,
655                                    tcfg->tqueue->avg_ramp_up) != 0) {
656                 result = FAIL;
657                 goto out;
658         }
659         printf("%s", tcfg->htxt);
660
661         for (i = 0; i < tcfg->tconfig->num_cfg; i++) {
662                 uint32_t avg = 0;
663                 double drop_rate = 0.0;
664                 double drop_prob = 0.0;
665                 double diff = 0.0;
666
667                 *tcfg->tvar->dropped = 0;
668                 *tcfg->tvar->enqueued = 0;
669
670                 enqueue_dequeue_func(&tcfg->tconfig->rconfig[i],
671                                      tcfg->tqueue->rdata,
672                                      tcfg->tqueue->q,
673                                      tcfg->tvar->num_ops,
674                                      tcfg->tvar->enqueued,
675                                      tcfg->tvar->dropped);
676
677                 avg = rte_red_get_avg_int(&tcfg->tconfig->rconfig[i], tcfg->tqueue->rdata);
678                 if (avg != *tcfg->tlevel)
679                         result = FAIL;
680
681                 drop_rate = calc_drop_rate(*tcfg->tvar->enqueued, *tcfg->tvar->dropped);
682                 drop_prob = calc_drop_prob(tcfg->tconfig->min_th, tcfg->tconfig->max_th,
683                                            tcfg->tconfig->maxp_inv[i], *tcfg->tlevel);
684                 if (!check_drop_rate(&diff, drop_rate, drop_prob, (double)tcfg->tqueue->drop_tolerance))
685                         result = FAIL;
686                 /**
687                  * drop rate should decrease as maxp_inv increases
688                  */
689                 if (drop_rate > prev_drop_rate)
690                         result = FAIL;
691                 prev_drop_rate = drop_rate;
692
693                 printf("%-15u%-15u%-15u%-15u%-15.4lf%-15.4lf%-15.4lf%-15.4lf\n",
694                        i, avg, tcfg->tconfig->min_th, tcfg->tconfig->max_th,
695                        drop_prob * 100.0, drop_rate * 100.0, diff,
696                        (double)tcfg->tqueue->drop_tolerance);
697         }
698 out:
699         return (result);
700 }
701
702 /**
703  * Test F3: functional test 3
704  */
705 static uint32_t ft3_tlevel[] = {1022};
706
707 static struct test_rte_red_config ft3_tconfig =  {
708         .rconfig = ft_wrconfig,
709         .num_cfg = DIM(ft_wrconfig),
710         .wq_log2 = ft_wq_log2,
711         .min_th = 32,
712         .max_th = 1023,
713         .maxp_inv = ft_maxp_inv,
714 };
715
716 static struct test_config func_test3_config = {
717         .ifname = "functional test 3 interface",
718         .msg = "functional test 3 : use one RED configuration,\n"
719         "                   increase average queue size to target level,\n"
720         "                   dequeue all packets until queue is empty,\n"
721         "                   confirm that average queue size is computed correctly while queue is empty\n\n",
722         .htxt = "q avg before   "
723         "q avg after    "
724         "expected       "
725         "difference %   "
726         "tolerance %    "
727         "result  "
728         "\n",
729         .tconfig = &ft3_tconfig,
730         .tqueue = &ft_tqueue,
731         .tvar = &ft_tvar,
732         .tlevel = ft3_tlevel,
733 };
734
735 static enum test_result func_test3(struct test_config *tcfg)
736 {
737         enum test_result result = PASS;
738         uint32_t i = 0;
739
740         printf("%s", tcfg->msg);
741
742         if (test_rte_red_init(tcfg) != PASS) {
743                 result = FAIL;
744                 goto out;
745         }
746
747         rte_red_rt_data_init(tcfg->tqueue->rdata);
748
749         if (increase_actual_qsize(tcfg->tconfig->rconfig,
750                                   tcfg->tqueue->rdata,
751                                   tcfg->tqueue->q,
752                                   *tcfg->tlevel,
753                                   tcfg->tqueue->q_ramp_up) != 0) {
754                 result = FAIL;
755                 goto out;
756         }
757
758         if (increase_average_qsize(tcfg->tconfig->rconfig,
759                                    tcfg->tqueue->rdata,
760                                    tcfg->tqueue->q,
761                                    *tcfg->tlevel,
762                                    tcfg->tqueue->avg_ramp_up) != 0) {
763                 result = FAIL;
764                 goto out;
765         }
766
767         printf("%s", tcfg->htxt);
768
769         for (i = 0; i < tcfg->tvar->num_iterations; i++) {
770                 double avg_before = 0;
771                 double avg_after = 0;
772                 double exp_avg = 0;
773                 double diff = 0.0;
774
775                 avg_before = rte_red_get_avg_float(tcfg->tconfig->rconfig, tcfg->tqueue->rdata);
776
777                 /**
778                 * empty the queue
779                 */
780                 *tcfg->tqueue->q = 0;
781                 rte_red_mark_queue_empty(tcfg->tqueue->rdata, get_port_ts());
782
783                 rte_delay_us(tcfg->tvar->wait_usec);
784
785                 /**
786                  * enqueue one packet to recalculate average queue size
787                  */
788                 if (rte_red_enqueue(tcfg->tconfig->rconfig,
789                                     tcfg->tqueue->rdata,
790                                     *tcfg->tqueue->q,
791                                     get_port_ts()) == 0) {
792                         (*tcfg->tqueue->q)++;
793                 } else {
794                         printf("%s:%d: packet enqueued on empty queue was dropped\n", __func__, __LINE__);
795                         result = FAIL;
796                 }
797
798                 exp_avg = calc_exp_avg_on_empty(avg_before, 
799                                               (1 << *tcfg->tconfig->wq_log2),
800                                               tcfg->tvar->wait_usec);
801                 avg_after = rte_red_get_avg_float(tcfg->tconfig->rconfig, 
802                                                   tcfg->tqueue->rdata);
803                 if (!check_avg(&diff, avg_after, exp_avg, (double)tcfg->tqueue->avg_tolerance))
804                         result = FAIL;
805
806                 printf("%-15.4lf%-15.4lf%-15.4lf%-15.4lf%-15.4lf%-15s\n",
807                        avg_before, avg_after, exp_avg, diff,
808                        (double)tcfg->tqueue->avg_tolerance,
809                        diff <= (double)tcfg->tqueue->avg_tolerance ? "pass" : "fail");
810         }
811 out:
812         return (result);
813 }
814
815 /**
816  * Test F4: functional test 4
817  */
818 static uint32_t ft4_tlevel[] = {1022};
819 static uint8_t ft4_wq_log2[] = {11};
820
821 static struct test_rte_red_config ft4_tconfig =  {
822         .rconfig = ft_wrconfig,
823         .num_cfg = DIM(ft_wrconfig),
824         .min_th = 32,
825         .max_th = 1023,
826         .wq_log2 = ft4_wq_log2,
827         .maxp_inv = ft_maxp_inv,
828 };
829
830 static struct test_queue ft4_tqueue = {
831         .rdata = ft_rtdata,
832         .num_queues = DIM(ft_rtdata),
833         .qconfig = ft_qconfig,
834         .q = ft_q,
835         .q_ramp_up = 1000000,
836         .avg_ramp_up = 1000000,
837         .avg_tolerance = 0,  /* 0 percent */
838         .drop_tolerance = 50,  /* 50 percent */
839 };
840
841 static struct test_config func_test4_config = {
842         .ifname = "functional test 4 interface",
843         .msg = "functional test 4 : use one RED configuration,\n"
844         "                   increase average queue size to target level,\n"
845         "                   dequeue all packets until queue is empty,\n"
846         "                   confirm that average queue size is computed correctly while\n"
847         "                   queue is empty for more than 50 sec,\n"
848         "                   (this test takes 52 sec to run)\n\n",
849         .htxt = "q avg before   "
850         "q avg after    "
851         "expected       "
852         "difference %   "
853         "tolerance %    "
854         "result  "
855         "\n",
856         .tconfig = &ft4_tconfig,
857         .tqueue = &ft4_tqueue,
858         .tvar = &ft_tvar,
859         .tlevel = ft4_tlevel,
860 };
861
862 static enum test_result func_test4(struct test_config *tcfg)
863 {
864         enum test_result result = PASS;
865         uint64_t time_diff = 0;
866         uint64_t start = 0;
867         double avg_before = 0.0;
868         double avg_after = 0.0;
869         double exp_avg = 0.0;
870         double diff = 0.0;
871
872         printf("%s", tcfg->msg);
873
874         if (test_rte_red_init(tcfg) != PASS) {
875                 result = FAIL;
876                 goto out;
877         }
878
879         rte_red_rt_data_init(tcfg->tqueue->rdata);
880
881         if (increase_actual_qsize(tcfg->tconfig->rconfig,
882                                   tcfg->tqueue->rdata,
883                                   tcfg->tqueue->q,
884                                   *tcfg->tlevel,
885                                   tcfg->tqueue->q_ramp_up) != 0) {
886                 result = FAIL;
887                 goto out;
888         }
889
890         if (increase_average_qsize(tcfg->tconfig->rconfig,
891                                    tcfg->tqueue->rdata,
892                                    tcfg->tqueue->q,
893                                    *tcfg->tlevel,
894                                    tcfg->tqueue->avg_ramp_up) != 0) {
895                 result = FAIL;
896                 goto out;
897         }
898
899         printf("%s", tcfg->htxt);
900
901         avg_before = rte_red_get_avg_float(tcfg->tconfig->rconfig, tcfg->tqueue->rdata);
902
903         /**
904          * empty the queue
905          */
906         *tcfg->tqueue->q = 0;
907         rte_red_mark_queue_empty(tcfg->tqueue->rdata, get_port_ts());
908
909         /**
910          * record empty time locally 
911          */
912         start = rte_rdtsc();
913
914         sleep(tcfg->tvar->sleep_sec);
915
916         /**
917          * enqueue one packet to recalculate average queue size
918          */
919         if (rte_red_enqueue(tcfg->tconfig->rconfig,  
920                             tcfg->tqueue->rdata, 
921                             *tcfg->tqueue->q,
922                             get_port_ts()) != 0) {
923                 result = FAIL;
924                 goto out;
925         }
926         (*tcfg->tqueue->q)++;
927
928         /**
929          * calculate how long queue has been empty
930          */
931         time_diff = ((rte_rdtsc() - start) / tcfg->tvar->clk_freq)
932                   * MSEC_PER_SEC;
933         if (time_diff < MAX_QEMPTY_TIME_MSEC) {
934                 /**
935                  * this could happen if sleep was interrupted for some reason
936                  */
937                 result = FAIL;
938                 goto out;
939         }
940
941         /**
942          * confirm that average queue size is now at expected level
943          */
944         exp_avg = 0.0;
945         avg_after = rte_red_get_avg_float(tcfg->tconfig->rconfig, tcfg->tqueue->rdata);
946         if (!check_avg(&diff, avg_after, exp_avg, (double)tcfg->tqueue->avg_tolerance))
947                 result = FAIL;
948
949         printf("%-15.4lf%-15.4lf%-15.4lf%-15.4lf%-15.4lf%-15s\n",
950                avg_before, avg_after, exp_avg,
951                diff, (double)tcfg->tqueue->avg_tolerance,
952                diff <= (double)tcfg->tqueue->avg_tolerance ? "pass" : "fail");
953 out:
954         return (result);
955 }
956
957 /**
958  * Test F5: functional test 5
959  */
960 static uint32_t ft5_tlevel[] = {127};
961 static uint8_t ft5_wq_log2[] = {9, 8};
962 static uint8_t ft5_maxp_inv[] = {10, 20};
963 static struct rte_red_config ft5_config[2];
964 static struct rte_red ft5_data[4];
965 static uint32_t ft5_q[4];
966 static uint32_t ft5_dropped[] = {0, 0, 0, 0};
967 static uint32_t ft5_enqueued[] = {0, 0, 0, 0};
968
969 static struct test_rte_red_config ft5_tconfig =  {
970         .rconfig = ft5_config,
971         .num_cfg = DIM(ft5_config),
972         .min_th = 32,
973         .max_th = 128,
974         .wq_log2 = ft5_wq_log2,
975         .maxp_inv = ft5_maxp_inv,
976 };
977
978 static struct test_queue ft5_tqueue = {
979         .rdata = ft5_data,
980         .num_queues = DIM(ft5_data),
981         .qconfig = ft_qconfig,
982         .q = ft5_q,
983         .q_ramp_up = 1000000,
984         .avg_ramp_up = 1000000,
985         .avg_tolerance = 5,  /* 10 percent */
986         .drop_tolerance = 50,  /* 50 percent */
987 };
988
989 struct test_var ft5_tvar = {
990         .wait_usec = 0,
991         .num_iterations = 15,
992         .num_ops = 10000,
993         .clk_freq = 0,
994         .dropped = ft5_dropped,
995         .enqueued = ft5_enqueued,
996         .sleep_sec = 0,
997 };
998
999 static struct test_config func_test5_config = {
1000         .ifname = "functional test 5 interface",
1001         .msg = "functional test 5 : use several queues (each with its own run-time data),\n"
1002         "                   use several RED configurations (such that each configuration is shared by multiple queues),\n"
1003         "                   increase average queue size to just below maximum threshold,\n"
1004         "                   compare drop rate to drop probability,\n"
1005         "                   (this is a larger scale version of functional test 2)\n\n",
1006         .htxt = "queue          "
1007         "config         "
1008         "avg queue size "
1009         "min threshold  "
1010         "max threshold  "
1011         "drop prob %    "
1012         "drop rate %    "
1013         "diff %         "
1014         "tolerance %    "
1015         "\n",
1016         .tconfig = &ft5_tconfig,
1017         .tqueue = &ft5_tqueue,
1018         .tvar = &ft5_tvar,
1019         .tlevel = ft5_tlevel,
1020 };
1021
1022 static enum test_result func_test5(struct test_config *tcfg)
1023 {
1024         enum test_result result = PASS;
1025         uint32_t j = 0;
1026
1027         printf("%s", tcfg->msg);
1028
1029         if (test_rte_red_init(tcfg) != PASS) {
1030                 result = FAIL;
1031                 goto out;
1032         }
1033
1034         printf("%s", tcfg->htxt);
1035
1036         for (j = 0; j < tcfg->tqueue->num_queues; j++) {
1037                 rte_red_rt_data_init(&tcfg->tqueue->rdata[j]);
1038                 tcfg->tqueue->q[j] = 0;
1039
1040                 if (increase_actual_qsize(&tcfg->tconfig->rconfig[tcfg->tqueue->qconfig[j]],
1041                                           &tcfg->tqueue->rdata[j],
1042                                           &tcfg->tqueue->q[j],
1043                                           *tcfg->tlevel,
1044                                           tcfg->tqueue->q_ramp_up) != 0) {
1045                         result = FAIL;
1046                         goto out;
1047                 }
1048
1049                 if (increase_average_qsize(&tcfg->tconfig->rconfig[tcfg->tqueue->qconfig[j]],
1050                                            &tcfg->tqueue->rdata[j],
1051                                            &tcfg->tqueue->q[j],
1052                                            *tcfg->tlevel,
1053                                            tcfg->tqueue->avg_ramp_up) != 0) {
1054                         result = FAIL;
1055                         goto out;
1056                 }
1057         }
1058
1059         for (j = 0; j < tcfg->tqueue->num_queues; j++) {
1060                 uint32_t avg = 0;
1061                 double drop_rate = 0.0;
1062                 double drop_prob = 0.0;
1063                 double diff = 0.0;
1064
1065                 tcfg->tvar->dropped[j] = 0;
1066                 tcfg->tvar->enqueued[j] = 0;
1067
1068                 enqueue_dequeue_func(&tcfg->tconfig->rconfig[tcfg->tqueue->qconfig[j]],
1069                                      &tcfg->tqueue->rdata[j],
1070                                      &tcfg->tqueue->q[j],
1071                                      tcfg->tvar->num_ops,
1072                                      &tcfg->tvar->enqueued[j],
1073                                      &tcfg->tvar->dropped[j]);
1074
1075                 avg = rte_red_get_avg_int(&tcfg->tconfig->rconfig[tcfg->tqueue->qconfig[j]],
1076                                           &tcfg->tqueue->rdata[j]);
1077                 if (avg != *tcfg->tlevel)
1078                         result = FAIL;
1079
1080                 drop_rate = calc_drop_rate(tcfg->tvar->enqueued[j],tcfg->tvar->dropped[j]);
1081                 drop_prob = calc_drop_prob(tcfg->tconfig->min_th, tcfg->tconfig->max_th,
1082                                            tcfg->tconfig->maxp_inv[tcfg->tqueue->qconfig[j]], 
1083                                            *tcfg->tlevel);
1084                 if (!check_drop_rate(&diff, drop_rate, drop_prob, (double)tcfg->tqueue->drop_tolerance))
1085                         result = FAIL;
1086
1087                 printf("%-15u%-15u%-15u%-15u%-15u%-15.4lf%-15.4lf%-15.4lf%-15.4lf\n",
1088                        j, tcfg->tqueue->qconfig[j], avg,
1089                        tcfg->tconfig->min_th, tcfg->tconfig->max_th,
1090                        drop_prob * 100.0, drop_rate * 100.0,
1091                        diff, (double)tcfg->tqueue->drop_tolerance);
1092         }
1093 out:
1094         return (result);
1095 }
1096
1097 /**
1098  * Test F6: functional test 6
1099  */
1100 static uint32_t ft6_tlevel[] = {1022};
1101 static uint8_t ft6_wq_log2[] = {9, 8};
1102 static uint8_t ft6_maxp_inv[] = {10, 20};
1103 static struct rte_red_config ft6_config[2];
1104 static struct rte_red ft6_data[4];
1105 static uint32_t ft6_q[4];
1106
1107 static struct test_rte_red_config ft6_tconfig =  {
1108         .rconfig = ft6_config,
1109         .num_cfg = DIM(ft6_config),
1110         .min_th = 32,
1111         .max_th = 1023,
1112         .wq_log2 = ft6_wq_log2,
1113         .maxp_inv = ft6_maxp_inv,
1114 };
1115
1116 static struct test_queue ft6_tqueue = {
1117         .rdata = ft6_data,
1118         .num_queues = DIM(ft6_data),
1119         .qconfig = ft_qconfig,
1120         .q = ft6_q,
1121         .q_ramp_up = 1000000,
1122         .avg_ramp_up = 1000000,
1123         .avg_tolerance = 5,  /* 10 percent */
1124         .drop_tolerance = 50,  /* 50 percent */
1125 };
1126
1127 static struct test_config func_test6_config = {
1128         .ifname = "functional test 6 interface",
1129         .msg = "functional test 6 : use several queues (each with its own run-time data),\n"
1130         "                   use several RED configurations (such that each configuration is sharte_red by multiple queues),\n"
1131         "                   increase average queue size to target level,\n"
1132         "                   dequeue all packets until queue is empty,\n"
1133         "                   confirm that average queue size is computed correctly while queue is empty\n"
1134         "                   (this is a larger scale version of functional test 3)\n\n",
1135         .htxt = "queue          "
1136         "config         "
1137         "q avg before   "
1138         "q avg after    "
1139         "expected       "
1140         "difference %   "
1141         "tolerance %    "
1142         "result  ""\n",
1143         .tconfig = &ft6_tconfig,
1144         .tqueue = &ft6_tqueue,
1145         .tvar = &ft_tvar,
1146         .tlevel = ft6_tlevel,
1147 };
1148
1149 static enum test_result func_test6(struct test_config *tcfg)
1150 {
1151         enum test_result result = PASS;
1152         uint32_t j = 0;
1153
1154         printf("%s", tcfg->msg);
1155         if (test_rte_red_init(tcfg) != PASS) {
1156                 result = FAIL;
1157                 goto out;
1158         }
1159         printf("%s", tcfg->htxt);
1160
1161         for (j = 0; j < tcfg->tqueue->num_queues; j++) {
1162                 rte_red_rt_data_init(&tcfg->tqueue->rdata[j]);
1163                 tcfg->tqueue->q[j] = 0;
1164
1165                 if (increase_actual_qsize(&tcfg->tconfig->rconfig[tcfg->tqueue->qconfig[j]],
1166                                           &tcfg->tqueue->rdata[j],
1167                                           &tcfg->tqueue->q[j],
1168                                           *tcfg->tlevel,
1169                                           tcfg->tqueue->q_ramp_up) != 0) {
1170                         result = FAIL;
1171                         goto out;
1172                 }
1173                 if (increase_average_qsize(&tcfg->tconfig->rconfig[tcfg->tqueue->qconfig[j]],
1174                                            &tcfg->tqueue->rdata[j],
1175                                            &tcfg->tqueue->q[j],
1176                                            *tcfg->tlevel,
1177                                            tcfg->tqueue->avg_ramp_up) != 0) {
1178                         result = FAIL;
1179                         goto out;
1180                 }
1181         }
1182         for (j = 0; j < tcfg->tqueue->num_queues; j++) {
1183                 double avg_before = 0;
1184                 double avg_after = 0;
1185                 double exp_avg = 0;
1186                 double diff = 0.0;
1187
1188                 avg_before = rte_red_get_avg_float(&tcfg->tconfig->rconfig[tcfg->tqueue->qconfig[j]], 
1189                                                    &tcfg->tqueue->rdata[j]);
1190
1191                 /**
1192                  * empty the queue
1193                  */
1194                 tcfg->tqueue->q[j] = 0;
1195                 rte_red_mark_queue_empty(&tcfg->tqueue->rdata[j], get_port_ts());
1196                 rte_delay_us(tcfg->tvar->wait_usec);
1197
1198                 /**
1199                  * enqueue one packet to recalculate average queue size
1200                  */
1201                 if (rte_red_enqueue(&tcfg->tconfig->rconfig[tcfg->tqueue->qconfig[j]], 
1202                                     &tcfg->tqueue->rdata[j],
1203                                     tcfg->tqueue->q[j],
1204                                     get_port_ts()) == 0) {
1205                         tcfg->tqueue->q[j]++;
1206                 } else {
1207                         printf("%s:%d: packet enqueued on empty queue was dropped\n", __func__, __LINE__);
1208                         result = FAIL;
1209                 }
1210
1211                 exp_avg = calc_exp_avg_on_empty(avg_before, 
1212                                 (1 << tcfg->tconfig->wq_log2[tcfg->tqueue->qconfig[j]]),
1213                                 tcfg->tvar->wait_usec);
1214                 avg_after = rte_red_get_avg_float(&tcfg->tconfig->rconfig[tcfg->tqueue->qconfig[j]],
1215                                                 &tcfg->tqueue->rdata[j]);
1216                 if (!check_avg(&diff, avg_after, exp_avg, (double)tcfg->tqueue->avg_tolerance))
1217                         result = FAIL;
1218
1219                 printf("%-15u%-15u%-15.4lf%-15.4lf%-15.4lf%-15.4lf%-15.4lf%-15s\n",
1220                        j, tcfg->tqueue->qconfig[j], avg_before, avg_after,
1221                        exp_avg, diff, (double)tcfg->tqueue->avg_tolerance,
1222                        diff <= tcfg->tqueue->avg_tolerance ? "pass" : "fail");
1223         }
1224 out:
1225         return (result);
1226 }
1227
1228 /**
1229  * setup default values for the performance test structures
1230  */
1231 static struct rte_red_config pt_wrconfig[1];
1232 static struct rte_red pt_rtdata[1];
1233 static uint8_t pt_wq_log2[] = {9};
1234 static uint8_t pt_maxp_inv[] = {10}; 
1235 static uint32_t pt_qconfig[] = {0};
1236 static uint32_t pt_q[] = {0};
1237 static uint32_t pt_dropped[] = {0};
1238 static uint32_t pt_enqueued[] = {0};
1239
1240 static struct test_rte_red_config pt_tconfig =  {
1241         .rconfig = pt_wrconfig,
1242         .num_cfg = DIM(pt_wrconfig),
1243         .wq_log2 = pt_wq_log2,
1244         .min_th = 32,
1245         .max_th = 128,
1246         .maxp_inv = pt_maxp_inv,
1247 };
1248
1249 static struct test_queue pt_tqueue = {
1250         .rdata = pt_rtdata,
1251         .num_queues = DIM(pt_rtdata),
1252         .qconfig = pt_qconfig,
1253         .q = pt_q,
1254         .q_ramp_up = 1000000,
1255         .avg_ramp_up = 1000000,
1256         .avg_tolerance = 5,  /* 10 percent */
1257         .drop_tolerance = 50,  /* 50 percent */
1258 };
1259
1260 /**
1261  * enqueue/dequeue packets
1262  */
1263 static void enqueue_dequeue_perf(struct rte_red_config *red_cfg,
1264                                  struct rte_red *red,
1265                                  uint32_t *q,
1266                                  uint32_t num_ops,
1267                                  uint32_t *enqueued,
1268                                  uint32_t *dropped,
1269                                  struct rdtsc_prof *prof)
1270 {
1271         uint32_t i = 0;
1272
1273         for (i = 0; i < num_ops; i++) {
1274                 uint64_t ts = 0;
1275                 int ret = 0;
1276                 /**
1277                  * enqueue
1278                  */
1279                 ts = get_port_ts();
1280                 rdtsc_prof_start(prof);
1281                 ret = rte_red_enqueue(red_cfg, red, *q, ts );
1282                 rdtsc_prof_end(prof);
1283                 if (ret == 0)
1284                         (*enqueued)++;
1285                 else
1286                         (*dropped)++;
1287         }
1288 }
1289
1290 /**
1291  * Setup test structures for tests P1, P2, P3 
1292  * performance tests 1, 2 and 3
1293  */
1294 static uint32_t pt1_tlevel[] = {16};
1295 static uint32_t pt2_tlevel[] = {80};
1296 static uint32_t pt3_tlevel[] = {144};
1297
1298 static struct test_var perf1_tvar = {
1299         .wait_usec = 0,
1300         .num_iterations = 15,
1301         .num_ops = 50000000,
1302         .clk_freq = 0,
1303         .dropped = pt_dropped,
1304         .enqueued = pt_enqueued,
1305         .sleep_sec = 0
1306 };
1307
1308 static struct test_config perf1_test1_config = {
1309         .ifname = "performance test 1 interface",
1310         .msg = "performance test 1 : use one RED configuration,\n"
1311         "                    set actual and average queue sizes to level below min threshold,\n"
1312         "                    measure enqueue performance\n\n",
1313         .tconfig = &pt_tconfig,
1314         .tqueue = &pt_tqueue,
1315         .tvar = &perf1_tvar,
1316         .tlevel = pt1_tlevel,
1317 };
1318
1319 static struct test_config perf1_test2_config = {
1320         .ifname = "performance test 2 interface",
1321         .msg = "performance test 2 : use one RED configuration,\n"
1322         "                    set actual and average queue sizes to level in between min and max thresholds,\n"
1323         "                    measure enqueue performance\n\n",
1324         .tconfig = &pt_tconfig,
1325         .tqueue = &pt_tqueue,
1326         .tvar = &perf1_tvar,
1327         .tlevel = pt2_tlevel,
1328 };
1329
1330 static struct test_config perf1_test3_config = {
1331         .ifname = "performance test 3 interface",
1332         .msg = "performance test 3 : use one RED configuration,\n"
1333         "                    set actual and average queue sizes to level above max threshold,\n"
1334         "                    measure enqueue performance\n\n",
1335         .tconfig = &pt_tconfig,
1336         .tqueue = &pt_tqueue,
1337         .tvar = &perf1_tvar,
1338         .tlevel = pt3_tlevel,
1339 };
1340
1341 /**
1342  * Performance test function to measure enqueue performance. 
1343  * This runs performance tests 1, 2 and 3 
1344  */
1345 static enum test_result perf1_test(struct test_config *tcfg)
1346 {
1347         enum test_result result = PASS;
1348         struct rdtsc_prof prof = {0, 0, 0, 0, 0.0, NULL};
1349         uint32_t total = 0;
1350
1351         printf("%s", tcfg->msg);
1352
1353         rdtsc_prof_init(&prof, "enqueue");
1354
1355         if (test_rte_red_init(tcfg) != PASS) {
1356                 result = FAIL;
1357                 goto out;
1358         }
1359
1360         /**
1361          * set average queue size to target level
1362          */
1363         *tcfg->tqueue->q = *tcfg->tlevel;
1364
1365         /**
1366          * initialize the rte_red run time data structure
1367          */
1368         rte_red_rt_data_init(tcfg->tqueue->rdata);
1369
1370         /**
1371          *  set the queue average
1372          */
1373         rte_red_set_avg_int(tcfg->tconfig->rconfig, tcfg->tqueue->rdata, *tcfg->tlevel);
1374         if (rte_red_get_avg_int(tcfg->tconfig->rconfig, tcfg->tqueue->rdata) 
1375             != *tcfg->tlevel) {
1376                 result = FAIL;
1377                 goto out;
1378         }
1379
1380         enqueue_dequeue_perf(tcfg->tconfig->rconfig,
1381                              tcfg->tqueue->rdata,
1382                              tcfg->tqueue->q,
1383                              tcfg->tvar->num_ops,
1384                              tcfg->tvar->enqueued,
1385                              tcfg->tvar->dropped,
1386                              &prof);
1387
1388         total = *tcfg->tvar->enqueued + *tcfg->tvar->dropped;
1389
1390         printf("\ntotal: %u, enqueued: %u (%.2lf%%), dropped: %u (%.2lf%%)\n", total,
1391                *tcfg->tvar->enqueued, ((double)(*tcfg->tvar->enqueued) / (double)total) * 100.0,
1392                *tcfg->tvar->dropped, ((double)(*tcfg->tvar->dropped) / (double)total) * 100.0);
1393
1394         rdtsc_prof_print(&prof);
1395 out:
1396         return (result);
1397 }
1398
1399 /**
1400  * Setup test structures for tests P4, P5, P6 
1401  * performance tests 4, 5 and 6
1402  */
1403 static uint32_t pt4_tlevel[] = {16};
1404 static uint32_t pt5_tlevel[] = {80};
1405 static uint32_t pt6_tlevel[] = {144};
1406
1407 static struct test_var perf2_tvar = {
1408         .wait_usec = 500,
1409         .num_iterations = 10000,
1410         .num_ops = 10000,
1411         .dropped = pt_dropped,
1412         .enqueued = pt_enqueued,
1413         .sleep_sec = 0
1414 };
1415
1416 static struct test_config perf2_test4_config = {
1417         .ifname = "performance test 4 interface",
1418         .msg = "performance test 4 : use one RED configuration,\n"
1419         "                    set actual and average queue sizes to level below min threshold,\n"
1420         "                    dequeue all packets until queue is empty,\n"
1421         "                    measure enqueue performance when queue is empty\n\n",
1422         .htxt = "iteration      "
1423         "q avg before   "
1424         "q avg after    "
1425         "expected       "
1426         "difference %   "
1427         "tolerance %    "
1428         "result  ""\n",
1429         .tconfig = &pt_tconfig,
1430         .tqueue = &pt_tqueue,
1431         .tvar = &perf2_tvar,
1432         .tlevel = pt4_tlevel,
1433 };
1434
1435 static struct test_config perf2_test5_config = {
1436         .ifname = "performance test 5 interface",
1437         .msg = "performance test 5 : use one RED configuration,\n"
1438         "                    set actual and average queue sizes to level in between min and max thresholds,\n"
1439         "                    dequeue all packets until queue is empty,\n"
1440         "                    measure enqueue performance when queue is empty\n\n",
1441         .htxt = "iteration      "
1442         "q avg before   "
1443         "q avg after    "
1444         "expected       "
1445         "difference     "
1446         "tolerance      "
1447         "result  ""\n",
1448         .tconfig = &pt_tconfig,
1449         .tqueue = &pt_tqueue,
1450         .tvar = &perf2_tvar,
1451         .tlevel = pt5_tlevel,
1452 };
1453
1454 static struct test_config perf2_test6_config = {
1455         .ifname = "performance test 6 interface",
1456         .msg = "performance test 6 : use one RED configuration,\n"
1457         "                    set actual and average queue sizes to level above max threshold,\n"
1458         "                    dequeue all packets until queue is empty,\n"
1459         "                    measure enqueue performance when queue is empty\n\n",
1460         .htxt = "iteration      "
1461         "q avg before   "
1462         "q avg after    "
1463         "expected       "
1464         "difference %   "
1465         "tolerance %    "
1466         "result  ""\n",
1467         .tconfig = &pt_tconfig,
1468         .tqueue = &pt_tqueue,
1469         .tvar = &perf2_tvar,
1470         .tlevel = pt6_tlevel,
1471 };
1472
1473 /**
1474  * Performance test function to measure enqueue performance when the 
1475  * queue is empty. This runs performance tests 4, 5 and 6 
1476  */
1477 static enum test_result perf2_test(struct test_config *tcfg)
1478 {
1479         enum test_result result = PASS;
1480         struct rdtsc_prof prof = {0, 0, 0, 0, 0.0, NULL};
1481         uint32_t total = 0;
1482         uint32_t i = 0;
1483
1484         printf("%s", tcfg->msg);
1485
1486         rdtsc_prof_init(&prof, "enqueue");
1487
1488         if (test_rte_red_init(tcfg) != PASS) {
1489                 result = FAIL;
1490                 goto out;
1491         }
1492
1493         printf("%s", tcfg->htxt); 
1494
1495         for (i = 0; i < tcfg->tvar->num_iterations; i++) {
1496                 uint32_t count = 0;
1497                 uint64_t ts = 0;
1498                 double avg_before = 0;
1499                 int ret = 0;
1500
1501                 /**
1502                  * set average queue size to target level
1503                  */
1504                 *tcfg->tqueue->q = *tcfg->tlevel;
1505                 count = (*tcfg->tqueue->rdata).count;
1506
1507                 /**
1508                  * initialize the rte_red run time data structure
1509                  */
1510                 rte_red_rt_data_init(tcfg->tqueue->rdata);
1511                 (*tcfg->tqueue->rdata).count = count;
1512
1513                 /**
1514                  * set the queue average
1515                  */
1516                 rte_red_set_avg_int(tcfg->tconfig->rconfig, tcfg->tqueue->rdata, *tcfg->tlevel);
1517                 avg_before = rte_red_get_avg_float(tcfg->tconfig->rconfig, tcfg->tqueue->rdata);
1518                 if ((avg_before < *tcfg->tlevel) || (avg_before > *tcfg->tlevel)) {
1519                         result = FAIL;
1520                         goto out;
1521                 }
1522
1523                 /**
1524                  * empty the queue
1525                  */
1526                 *tcfg->tqueue->q = 0;
1527                 rte_red_mark_queue_empty(tcfg->tqueue->rdata, get_port_ts());
1528
1529                 /**
1530                  * wait for specified period of time
1531                  */
1532                 rte_delay_us(tcfg->tvar->wait_usec);
1533
1534                 /**
1535                  * measure performance of enqueue operation while queue is empty
1536                  */
1537                 ts = get_port_ts();
1538                 rdtsc_prof_start(&prof);
1539                 ret = rte_red_enqueue(tcfg->tconfig->rconfig, tcfg->tqueue->rdata, 
1540                                       *tcfg->tqueue->q, ts );
1541                 rdtsc_prof_end(&prof);
1542
1543                 /**
1544                  * gather enqueued/dropped statistics
1545                  */
1546                 if (ret == 0)
1547                         (*tcfg->tvar->enqueued)++;
1548                 else
1549                         (*tcfg->tvar->dropped)++;
1550
1551                 /**
1552                  * on first and last iteration, confirm that
1553                  * average queue size was computed correctly
1554                  */
1555                 if ((i == 0) || (i == tcfg->tvar->num_iterations - 1)) {
1556                         double avg_after = 0;
1557                         double exp_avg = 0;
1558                         double diff = 0.0;
1559                         int ok = 0;
1560
1561                         avg_after = rte_red_get_avg_float(tcfg->tconfig->rconfig, tcfg->tqueue->rdata);
1562                         exp_avg = calc_exp_avg_on_empty(avg_before, 
1563                                                   (1 << *tcfg->tconfig->wq_log2),
1564                                                   tcfg->tvar->wait_usec);
1565                         if (check_avg(&diff, avg_after, exp_avg, (double)tcfg->tqueue->avg_tolerance))
1566                                 ok = 1;
1567                         printf("%-15u%-15.4lf%-15.4lf%-15.4lf%-15.4lf%-15.4lf%-15s\n",
1568                                 i, avg_before, avg_after, exp_avg, diff,
1569                                 (double)tcfg->tqueue->avg_tolerance, ok ? "pass" : "fail");
1570                         if (!ok) {
1571                                 result = FAIL;
1572                                 goto out;
1573                         }
1574                 }
1575         }
1576         total =  *tcfg->tvar->enqueued +  *tcfg->tvar->dropped;
1577         printf("\ntotal: %u, enqueued: %u (%.2lf%%), dropped: %u (%.2lf%%)\n", total,
1578                *tcfg->tvar->enqueued, ((double)(*tcfg->tvar->enqueued) / (double)total) * 100.0,
1579                *tcfg->tvar->dropped, ((double)(*tcfg->tvar->dropped) / (double)total) * 100.0);
1580
1581         rdtsc_prof_print(&prof);
1582 out:
1583         return (result);
1584 }
1585
1586 /**
1587  * setup default values for overflow test structures
1588  */
1589 static uint32_t avg_max = 0;
1590 static uint32_t avg_max_bits = 0;
1591
1592 static struct rte_red_config ovfl_wrconfig[1];
1593 static struct rte_red ovfl_rtdata[1];
1594 static uint8_t ovfl_maxp_inv[] = {10}; 
1595 static uint32_t ovfl_qconfig[] = {0, 0, 1, 1};
1596 static uint32_t ovfl_q[] ={0};
1597 static uint32_t ovfl_dropped[] ={0};
1598 static uint32_t ovfl_enqueued[] ={0};
1599 static uint32_t ovfl_tlevel[] = {1023};
1600 static uint8_t ovfl_wq_log2[] = {12};
1601
1602 static struct test_rte_red_config ovfl_tconfig =  {
1603         .rconfig = ovfl_wrconfig,
1604         .num_cfg = DIM(ovfl_wrconfig),
1605         .wq_log2 = ovfl_wq_log2,
1606         .min_th = 32,
1607         .max_th = 1023,
1608         .maxp_inv = ovfl_maxp_inv,
1609 };
1610
1611 static struct test_queue ovfl_tqueue = {
1612         .rdata = ovfl_rtdata,
1613         .num_queues = DIM(ovfl_rtdata),
1614         .qconfig = ovfl_qconfig,
1615         .q = ovfl_q,
1616         .q_ramp_up = 1000000,
1617         .avg_ramp_up = 1000000,
1618         .avg_tolerance = 5,  /* 10 percent */
1619         .drop_tolerance = 50,  /* 50 percent */
1620 };
1621
1622 static struct test_var ovfl_tvar = {
1623         .wait_usec = 10000,
1624         .num_iterations = 1,
1625         .num_ops = 10000,
1626         .clk_freq = 0,
1627         .dropped = ovfl_dropped,
1628         .enqueued = ovfl_enqueued,
1629         .sleep_sec = 0
1630 };
1631
1632 static void ovfl_check_avg(uint32_t avg)
1633 {
1634         if (avg > avg_max) {
1635                 double avg_log = 0;
1636                 uint32_t bits = 0;
1637                 avg_max = avg;
1638                 avg_log = log(((double)avg_max));
1639                 avg_log = avg_log / log(2.0);
1640                 bits = (uint32_t)ceil(avg_log);
1641                 if (bits > avg_max_bits)
1642                         avg_max_bits = bits;
1643         }
1644 }
1645
1646 static struct test_config ovfl_test1_config = {
1647         .ifname = "queue avergage overflow test interface",
1648         .msg = "overflow test 1 : use one RED configuration,\n"
1649         "                 increase average queue size to target level,\n"
1650         "                 check maximum number of bits requirte_red to represent avg_s\n\n",
1651         .htxt = "avg queue size  "
1652         "wq_log2  "
1653         "fraction bits  "
1654         "max queue avg  "
1655         "num bits  "
1656         "enqueued  "
1657         "dropped   "
1658         "drop prob %  "
1659         "drop rate %  "
1660         "\n",
1661         .tconfig = &ovfl_tconfig,
1662         .tqueue = &ovfl_tqueue,
1663         .tvar = &ovfl_tvar,
1664         .tlevel = ovfl_tlevel,
1665 };
1666
1667 static enum test_result ovfl_test1(struct test_config *tcfg)
1668 {
1669         enum test_result result = PASS;
1670         uint32_t avg = 0;
1671         uint32_t i = 0;
1672         double drop_rate = 0.0;
1673         double drop_prob = 0.0;
1674         double diff = 0.0;
1675         int ret = 0;
1676
1677         printf("%s", tcfg->msg);
1678
1679         if (test_rte_red_init(tcfg) != PASS) {
1680
1681                 result = FAIL;
1682                 goto out;
1683         }
1684
1685         /**
1686          * reset rte_red run-time data
1687          */
1688         rte_red_rt_data_init(tcfg->tqueue->rdata);
1689
1690         /**
1691          * increase actual queue size
1692          */
1693         for (i = 0; i < tcfg->tqueue->q_ramp_up; i++) {
1694                 ret = rte_red_enqueue(tcfg->tconfig->rconfig, tcfg->tqueue->rdata,
1695                                       *tcfg->tqueue->q, get_port_ts());
1696
1697                 if (ret == 0) {
1698                         if (++(*tcfg->tqueue->q) >= *tcfg->tlevel)
1699                                 break;
1700                 }
1701         }
1702
1703         /**
1704          * enqueue
1705          */
1706         for (i = 0; i < tcfg->tqueue->avg_ramp_up; i++) {
1707                 ret = rte_red_enqueue(tcfg->tconfig->rconfig, tcfg->tqueue->rdata,
1708                                       *tcfg->tqueue->q, get_port_ts());
1709                 ovfl_check_avg((*tcfg->tqueue->rdata).avg);
1710                 avg = rte_red_get_avg_int(tcfg->tconfig->rconfig, tcfg->tqueue->rdata);
1711                 if (avg == *tcfg->tlevel) {
1712                         if (ret == 0)
1713                                 (*tcfg->tvar->enqueued)++;
1714                         else
1715                                 (*tcfg->tvar->dropped)++;
1716                 }
1717         }
1718
1719         /**
1720          * check if target average queue size has been reached
1721          */
1722         avg = rte_red_get_avg_int(tcfg->tconfig->rconfig, tcfg->tqueue->rdata);
1723         if (avg != *tcfg->tlevel) {
1724                 result = FAIL;
1725                 goto out;
1726         }
1727
1728         /**
1729          * check drop rate against drop probability
1730          */
1731         drop_rate = calc_drop_rate(*tcfg->tvar->enqueued, *tcfg->tvar->dropped);
1732         drop_prob = calc_drop_prob(tcfg->tconfig->min_th,
1733                                    tcfg->tconfig->max_th,
1734                                    *tcfg->tconfig->maxp_inv,
1735                                    *tcfg->tlevel);
1736         if (!check_drop_rate(&diff, drop_rate, drop_prob, (double)tcfg->tqueue->drop_tolerance))
1737                 result = FAIL;
1738
1739         printf("%s", tcfg->htxt);
1740         
1741         printf("%-16u%-9u%-15u0x%08x     %-10u%-10u%-10u%-13.2lf%-13.2lf\n",
1742                avg, *tcfg->tconfig->wq_log2, RTE_RED_SCALING,
1743                avg_max, avg_max_bits,
1744                *tcfg->tvar->enqueued, *tcfg->tvar->dropped,
1745                drop_prob * 100.0, drop_rate * 100.0);
1746 out:
1747         return (result);
1748 }
1749
1750 /**
1751  * define the functional and performance tests to be executed
1752  */
1753 struct tests func_tests[] = { 
1754         { &func_test1_config, func_test1 },
1755         { &func_test2_config, func_test2 },             
1756         { &func_test3_config, func_test3 },
1757         { &func_test4_config, func_test4 },
1758         { &func_test5_config, func_test5 },
1759         { &func_test6_config, func_test6 },
1760         { &ovfl_test1_config, ovfl_test1 }, 
1761 };
1762
1763 struct tests perf_tests[] = { 
1764         { &perf1_test1_config, perf1_test },
1765         { &perf1_test2_config, perf1_test },
1766         { &perf1_test3_config, perf1_test },
1767         { &perf2_test4_config, perf2_test },
1768         { &perf2_test5_config, perf2_test },
1769         { &perf2_test6_config, perf2_test },
1770 };
1771
1772 /**
1773  * function to execute the required_red tests
1774  */
1775 static void run_tests(struct tests *test_type, uint32_t test_count, uint32_t *num_tests, uint32_t *num_pass)
1776 {
1777         enum test_result result = PASS;
1778         uint32_t i = 0;
1779
1780         for (i = 0; i < test_count; i++) {
1781                 printf("\n--------------------------------------------------------------------------------\n");
1782                 result = test_type[i].testfn(test_type[i].testcfg);
1783                 (*num_tests)++;
1784                 if (result == PASS) {
1785                         (*num_pass)++;
1786                                 printf("-------------------------------------<pass>-------------------------------------\n");
1787                 } else {
1788                         printf("-------------------------------------<fail>-------------------------------------\n");
1789                 }
1790         }
1791         return;
1792 }
1793
1794 /**
1795  * check if functions accept invalid parameters
1796  *
1797  * First, all functions will be called without initialized RED
1798  * Then, all of them will be called with NULL/invalid parameters
1799  *
1800  * Some functions are not tested as they are performance-critical and thus
1801  * don't do any parameter checking.
1802  */
1803 static int
1804 test_invalid_parameters(void)
1805 {
1806         struct rte_red_config config;
1807
1808         if (rte_red_rt_data_init(NULL) == 0) {
1809                 printf("rte_red_rt_data_init should have failed!\n");
1810                 return -1;
1811         }
1812
1813         if (rte_red_config_init(NULL, 0, 0, 0, 0) == 0) {
1814                 printf("rte_red_config_init should have failed!\n");
1815                 return -1;
1816         }
1817
1818         if (rte_red_rt_data_init(NULL) == 0) {
1819                 printf("rte_red_rt_data_init should have failed!\n");
1820                 return -1;
1821         }
1822
1823         /* NULL config */
1824         if (rte_red_config_init(NULL, 0, 0, 0, 0) == 0) {
1825                 printf("%i: rte_red_config_init should have failed!\n", __LINE__);
1826                 return -1;
1827         }
1828         /* min_treshold == max_treshold */
1829         if (rte_red_config_init(&config, 0, 1, 1, 0) == 0) {
1830                 printf("%i: rte_red_config_init should have failed!\n", __LINE__);
1831                 return -1;
1832         }
1833         /* min_treshold > max_treshold */
1834         if (rte_red_config_init(&config, 0, 2, 1, 0) == 0) {
1835                 printf("%i: rte_red_config_init should have failed!\n", __LINE__);
1836                 return -1;
1837         }
1838         /* wq_log2 > RTE_RED_WQ_LOG2_MAX */
1839         if (rte_red_config_init(&config,
1840                         RTE_RED_WQ_LOG2_MAX + 1, 1, 2, 0) == 0) {
1841                 printf("%i: rte_red_config_init should have failed!\n", __LINE__);
1842                 return -1;
1843         }
1844         /* wq_log2 < RTE_RED_WQ_LOG2_MIN */
1845         if (rte_red_config_init(&config,
1846                         RTE_RED_WQ_LOG2_MIN - 1, 1, 2, 0) == 0) {
1847                 printf("%i: rte_red_config_init should have failed!\n", __LINE__);
1848                 return -1;
1849         }
1850         /* maxp_inv > RTE_RED_MAXP_INV_MAX */
1851         if (rte_red_config_init(&config,
1852                         RTE_RED_WQ_LOG2_MIN, 1, 2, RTE_RED_MAXP_INV_MAX + 1) == 0) {
1853                 printf("%i: rte_red_config_init should have failed!\n", __LINE__);
1854                 return -1;
1855         }
1856         /* maxp_inv < RTE_RED_MAXP_INV_MIN */
1857         if (rte_red_config_init(&config,
1858                         RTE_RED_WQ_LOG2_MIN, 1, 2, RTE_RED_MAXP_INV_MIN - 1) == 0) {
1859                 printf("%i: rte_red_config_init should have failed!\n", __LINE__);
1860                 return -1;
1861         }
1862
1863         return 0;
1864 }
1865
1866 int test_red(void)
1867 {
1868         uint32_t num_tests = 0;
1869         uint32_t num_pass = 0;
1870         int ret = 0;
1871
1872         if (test_invalid_parameters() < 0)
1873                 return -1;
1874
1875         run_tests(func_tests, DIM(func_tests), &num_tests, &num_pass);
1876         run_tests(perf_tests, DIM(perf_tests), &num_tests, &num_pass);
1877
1878         if (num_pass == num_tests) {
1879                 printf("[total: %u, pass: %u]\n", num_tests, num_pass);
1880                 ret = 0;
1881         } else {
1882                 printf("[total: %u, pass: %u, fail: %u]\n", num_tests, num_pass, num_tests - num_pass);
1883                 ret = -1;
1884         }
1885         return (ret);
1886 }
1887
1888 #else
1889
1890 int
1891 test_red(void)
1892 {
1893         printf("The SCHED library is not included in this build\n");
1894         return 0;
1895 }
1896
1897 #endif