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