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