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