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