app/test: convert all tests to register system
[dpdk.git] / app / test / test_meter.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
40 #include "test.h"
41
42 #ifdef RTE_LIBRTE_METER
43
44 #include <rte_cycles.h>
45 #include <rte_meter.h>
46
47 #define mlog(format, ...) do{\
48                 printf("Line %d:",__LINE__);\
49                 printf(format, ##__VA_ARGS__);\
50                 printf("\n");\
51         }while(0);
52
53 #define melog(format, ...) do{\
54                 printf("Line %d:",__LINE__);\
55                 printf(format, ##__VA_ARGS__);\
56                 printf(" failed!\n");\
57                 return -1;\
58         }while(0);
59
60 #define TM_TEST_SRTCM_CIR_DF 46000000
61 #define TM_TEST_SRTCM_CBS_DF 2048
62 #define TM_TEST_SRTCM_EBS_DF 4096
63
64 #define TM_TEST_TRTCM_CIR_DF 46000000
65 #define TM_TEST_TRTCM_PIR_DF 69000000
66 #define TM_TEST_TRTCM_CBS_DF 2048
67 #define TM_TEST_TRTCM_PBS_DF 4096
68
69 static struct rte_meter_srtcm_params sparams =
70                                 {.cir = TM_TEST_SRTCM_CIR_DF,
71                                  .cbs = TM_TEST_SRTCM_CBS_DF,
72                                  .ebs = TM_TEST_SRTCM_EBS_DF,};
73
74 static struct   rte_meter_trtcm_params tparams=
75                                 {.cir = TM_TEST_TRTCM_CIR_DF,
76                                  .pir = TM_TEST_TRTCM_PIR_DF,
77                                  .cbs = TM_TEST_TRTCM_CBS_DF,
78                                  .pbs = TM_TEST_TRTCM_PBS_DF,};
79
80 /**
81  * functional test for rte_meter_srtcm_config
82  */
83 static inline int
84 tm_test_srtcm_config(void)
85 {
86 #define SRTCM_CFG_MSG "srtcm_config"
87         struct rte_meter_srtcm sm;
88         struct  rte_meter_srtcm_params sparams1;
89
90         /* invalid parameter test */
91         if(rte_meter_srtcm_config(NULL, NULL) == 0)
92                 melog(SRTCM_CFG_MSG);
93         if(rte_meter_srtcm_config(&sm, NULL) == 0)
94                 melog(SRTCM_CFG_MSG);
95         if(rte_meter_srtcm_config(NULL, &sparams) == 0)
96                 melog(SRTCM_CFG_MSG);
97
98         /* cbs and ebs can't both be zero */
99         sparams1 = sparams;
100         sparams1.cbs = 0;
101         sparams1.ebs = 0;
102         if(rte_meter_srtcm_config(&sm, &sparams1) == 0)
103                 melog(SRTCM_CFG_MSG);
104
105         /* cir should never be 0 */
106         sparams1 = sparams;
107         sparams1.cir = 0;
108         if(rte_meter_srtcm_config(&sm, &sparams1) == 0)
109                 melog(SRTCM_CFG_MSG);
110
111         /* one of ebs and cbs can be zero, should be successful */
112         sparams1 = sparams;
113         sparams1.ebs = 0;
114         if(rte_meter_srtcm_config(&sm, &sparams1) != 0)
115                 melog(SRTCM_CFG_MSG);
116
117         sparams1 = sparams;
118         sparams1.cbs = 0;
119         if(rte_meter_srtcm_config(&sm, &sparams1) != 0)
120                 melog(SRTCM_CFG_MSG);
121
122         /* usual parameter, should be successful */
123         if(rte_meter_srtcm_config(&sm, &sparams) != 0)
124                 melog(SRTCM_CFG_MSG);
125
126         return 0;
127
128 }
129
130 /**
131  * functional test for rte_meter_trtcm_config
132  */
133 static inline int
134 tm_test_trtcm_config(void)
135 {
136         struct rte_meter_trtcm tm;
137         struct  rte_meter_trtcm_params tparams1;
138 #define TRTCM_CFG_MSG "trtcm_config"
139
140         /* invalid parameter test */
141         if(rte_meter_trtcm_config(NULL, NULL) == 0)
142                 melog(TRTCM_CFG_MSG);
143         if(rte_meter_trtcm_config(&tm, NULL) == 0)
144                 melog(TRTCM_CFG_MSG);
145         if(rte_meter_trtcm_config(NULL, &tparams) == 0)
146                 melog(TRTCM_CFG_MSG);
147
148         /* cir, cbs, pir and pbs never be zero */
149         tparams1 = tparams;
150         tparams1.cir = 0;
151         if(rte_meter_trtcm_config(&tm, &tparams1) == 0)
152                 melog(TRTCM_CFG_MSG);
153
154         tparams1 = tparams;
155         tparams1.cbs = 0;
156         if(rte_meter_trtcm_config(&tm, &tparams1) == 0)
157                 melog(TRTCM_CFG_MSG);
158
159         tparams1 = tparams;
160         tparams1.pbs = 0;
161         if(rte_meter_trtcm_config(&tm, &tparams1) == 0)
162                 melog(TRTCM_CFG_MSG);
163
164         tparams1 = tparams;
165         tparams1.pir = 0;
166         if(rte_meter_trtcm_config(&tm, &tparams1) == 0)
167                 melog(TRTCM_CFG_MSG);
168
169         /* pir should be greater or equal to cir */
170         tparams1 = tparams;
171         tparams1.pir = tparams1.cir - 1;
172         if(rte_meter_trtcm_config(&tm, &tparams1) == 0)
173                 melog(TRTCM_CFG_MSG" pir < cir test");
174
175         /* usual parameter, should be successful */
176         if(rte_meter_trtcm_config(&tm, &tparams) != 0)
177                 melog(TRTCM_CFG_MSG);
178
179         return 0;
180 }
181
182 /**
183  * functional test for rte_meter_srtcm_color_blind_check
184  */
185 static inline int
186 tm_test_srtcm_color_blind_check(void)
187 {
188 #define SRTCM_BLIND_CHECK_MSG "srtcm_blind_check"
189         struct rte_meter_srtcm sm;
190         uint64_t time;
191         uint64_t hz = rte_get_tsc_hz();
192
193         /* Test green */
194         if(rte_meter_srtcm_config(&sm, &sparams) != 0)
195                 melog(SRTCM_BLIND_CHECK_MSG);
196         time = rte_get_tsc_cycles() + hz;
197         if(rte_meter_srtcm_color_blind_check(
198                 &sm, time, TM_TEST_SRTCM_CBS_DF - 1)
199                 != e_RTE_METER_GREEN)
200                 melog(SRTCM_BLIND_CHECK_MSG" GREEN");
201
202         /* Test yellow */
203         if(rte_meter_srtcm_config(&sm, &sparams) != 0)
204                 melog(SRTCM_BLIND_CHECK_MSG);
205         time = rte_get_tsc_cycles() + hz;
206         if(rte_meter_srtcm_color_blind_check(
207                 &sm, time, TM_TEST_SRTCM_CBS_DF + 1)
208                 != e_RTE_METER_YELLOW)
209                 melog(SRTCM_BLIND_CHECK_MSG" YELLOW");
210
211         if(rte_meter_srtcm_config(&sm, &sparams) != 0)
212                 melog(SRTCM_BLIND_CHECK_MSG);
213         time = rte_get_tsc_cycles() + hz;
214         if(rte_meter_srtcm_color_blind_check(
215                 &sm, time, (uint32_t)sm.ebs - 1) != e_RTE_METER_YELLOW)
216                 melog(SRTCM_BLIND_CHECK_MSG" YELLOW");
217
218         /* Test red */
219         if(rte_meter_srtcm_config(&sm, &sparams) != 0)
220                 melog(SRTCM_BLIND_CHECK_MSG);
221         time = rte_get_tsc_cycles() + hz;
222         if(rte_meter_srtcm_color_blind_check(
223                 &sm, time, TM_TEST_SRTCM_EBS_DF + 1)
224                 != e_RTE_METER_RED)
225                 melog(SRTCM_BLIND_CHECK_MSG" RED");
226
227         return 0;
228
229 }
230
231 /**
232  * functional test for rte_meter_trtcm_color_blind_check
233  */
234 static inline int
235 tm_test_trtcm_color_blind_check(void)
236 {
237 #define TRTCM_BLIND_CHECK_MSG "trtcm_blind_check"
238
239         uint64_t time;
240         struct rte_meter_trtcm tm;
241         uint64_t hz = rte_get_tsc_hz();
242
243         /* Test green */
244         if(rte_meter_trtcm_config(&tm, &tparams) != 0)
245                 melog(TRTCM_BLIND_CHECK_MSG);
246         time = rte_get_tsc_cycles() + hz;
247         if(rte_meter_trtcm_color_blind_check(
248                 &tm, time, TM_TEST_TRTCM_CBS_DF - 1)
249                 != e_RTE_METER_GREEN)
250                 melog(TRTCM_BLIND_CHECK_MSG" GREEN");
251
252         /* Test yellow */
253         if(rte_meter_trtcm_config(&tm, &tparams) != 0)
254                 melog(TRTCM_BLIND_CHECK_MSG);
255         time = rte_get_tsc_cycles() + hz;
256         if(rte_meter_trtcm_color_blind_check(
257                 &tm, time, TM_TEST_TRTCM_CBS_DF + 1)
258                 != e_RTE_METER_YELLOW)
259                 melog(TRTCM_BLIND_CHECK_MSG" YELLOW");
260
261         if(rte_meter_trtcm_config(&tm, &tparams) != 0)
262                 melog(TRTCM_BLIND_CHECK_MSG);
263         time = rte_get_tsc_cycles() + hz;
264         if(rte_meter_trtcm_color_blind_check(
265                 &tm, time, TM_TEST_TRTCM_PBS_DF - 1)
266                 != e_RTE_METER_YELLOW)
267                 melog(TRTCM_BLIND_CHECK_MSG" YELLOW");
268
269         /* Test red */
270         if(rte_meter_trtcm_config(&tm, &tparams) != 0)
271                 melog(TRTCM_BLIND_CHECK_MSG);
272         time = rte_get_tsc_cycles() + hz;
273         if(rte_meter_trtcm_color_blind_check(
274                 &tm, time, TM_TEST_TRTCM_PBS_DF + 1)
275                 != e_RTE_METER_RED)
276                 melog(TRTCM_BLIND_CHECK_MSG" RED");
277
278         return 0;
279 }
280
281
282 /**
283  * @in[4] : the flags packets carries.
284  * @in[4] : the flags function expect to return.
285  * It will do blind check at the time of 1 second from beginning.
286  * At the time, it will use packets length of cbs -1, cbs + 1,
287  * ebs -1 and ebs +1 with flag in[0], in[1], in[2] and in[3] to do
288  * aware check, expect flag out[0], out[1], out[2] and out[3]
289  */
290
291 static inline int
292 tm_test_srtcm_aware_check
293 (enum rte_meter_color in[4], enum rte_meter_color out[4])
294 {
295 #define SRTCM_AWARE_CHECK_MSG "srtcm_aware_check"
296         struct rte_meter_srtcm sm;
297         uint64_t time;
298         uint64_t hz = rte_get_tsc_hz();
299
300         if(rte_meter_srtcm_config(&sm, &sparams) != 0)
301                 melog(SRTCM_AWARE_CHECK_MSG);
302         time = rte_get_tsc_cycles() + hz;
303         if(rte_meter_srtcm_color_aware_check(
304                 &sm, time, TM_TEST_SRTCM_CBS_DF - 1, in[0]) != out[0])
305                 melog(SRTCM_AWARE_CHECK_MSG" %u:%u", in[0], out[0]);
306
307         if(rte_meter_srtcm_config(&sm, &sparams) != 0)
308                 melog(SRTCM_AWARE_CHECK_MSG);
309         time = rte_get_tsc_cycles() + hz;
310         if(rte_meter_srtcm_color_aware_check(
311                 &sm, time, TM_TEST_SRTCM_CBS_DF + 1, in[1]) != out[1])
312                 melog(SRTCM_AWARE_CHECK_MSG" %u:%u", in[1], out[1]);
313
314         if(rte_meter_srtcm_config(&sm, &sparams) != 0)
315                 melog(SRTCM_AWARE_CHECK_MSG);
316         time = rte_get_tsc_cycles() + hz;
317         if(rte_meter_srtcm_color_aware_check(
318                 &sm, time, TM_TEST_SRTCM_EBS_DF - 1, in[2]) != out[2])
319                 melog(SRTCM_AWARE_CHECK_MSG" %u:%u", in[2], out[2]);
320
321         if(rte_meter_srtcm_config(&sm, &sparams) != 0)
322                 melog(SRTCM_AWARE_CHECK_MSG);
323         time = rte_get_tsc_cycles() + hz;
324         if(rte_meter_srtcm_color_aware_check(
325                 &sm, time, TM_TEST_SRTCM_EBS_DF + 1, in[3]) != out[3])
326                 melog(SRTCM_AWARE_CHECK_MSG" %u:%u", in[3], out[3]);
327
328         return 0;
329 }
330
331
332 /**
333  * functional test for rte_meter_srtcm_color_aware_check
334  */
335 static inline int
336 tm_test_srtcm_color_aware_check(void)
337 {
338         enum rte_meter_color in[4], out[4];
339
340         /**
341           * test 4 points that will produce green, yellow, yellow, red flag
342           * if using blind check
343           */
344
345         /* previouly have a green, test points should keep unchanged */
346         in[0] = in[1] = in[2] = in[3] = e_RTE_METER_GREEN;
347         out[0] = e_RTE_METER_GREEN;
348         out[1] = e_RTE_METER_YELLOW;
349         out[2] = e_RTE_METER_YELLOW;
350         out[3] = e_RTE_METER_RED;
351         if(tm_test_srtcm_aware_check(in, out) != 0)
352                 return -1;
353
354         /**
355           * previously have a yellow, green & yellow = yellow
356           * yellow & red = red
357           */
358         in[0] = in[1] = in[2] = in[3] = e_RTE_METER_YELLOW;
359         out[0] = e_RTE_METER_YELLOW;
360         out[1] = e_RTE_METER_YELLOW;
361         out[2] = e_RTE_METER_YELLOW;
362         out[3] = e_RTE_METER_RED;
363         if(tm_test_srtcm_aware_check(in, out) != 0)
364                 return -1;
365
366         /**
367           * previously have a red, red & green = red
368           * red & yellow = red
369           */
370         in[0] = in[1] = in[2] = in[3] = e_RTE_METER_RED;
371         out[0] = e_RTE_METER_RED;
372         out[1] = e_RTE_METER_RED;
373         out[2] = e_RTE_METER_RED;
374         out[3] = e_RTE_METER_RED;
375         if(tm_test_srtcm_aware_check(in, out) != 0)
376                 return -1;
377
378         return 0;
379 }
380
381 /**
382  * @in[4] : the flags packets carries.
383  * @in[4] : the flags function expect to return.
384  * It will do blind check at the time of 1 second from beginning.
385  * At the time, it will use packets length of cbs -1, cbs + 1,
386  * ebs -1 and ebs +1 with flag in[0], in[1], in[2] and in[3] to do
387  * aware check, expect flag out[0], out[1], out[2] and out[3]
388  */
389 static inline int
390 tm_test_trtcm_aware_check
391 (enum rte_meter_color in[4], enum rte_meter_color out[4])
392 {
393 #define TRTCM_AWARE_CHECK_MSG "trtcm_aware_check"
394         struct rte_meter_trtcm tm;
395         uint64_t time;
396         uint64_t hz = rte_get_tsc_hz();
397
398         if(rte_meter_trtcm_config(&tm, &tparams) != 0)
399                 melog(TRTCM_AWARE_CHECK_MSG);
400         time = rte_get_tsc_cycles() + hz;
401         if(rte_meter_trtcm_color_aware_check(
402                 &tm, time, TM_TEST_TRTCM_CBS_DF - 1, in[0]) != out[0])
403                 melog(TRTCM_AWARE_CHECK_MSG" %u:%u", in[0], out[0]);
404
405         if(rte_meter_trtcm_config(&tm, &tparams) != 0)
406                 melog(TRTCM_AWARE_CHECK_MSG);
407         time = rte_get_tsc_cycles() + hz;
408         if(rte_meter_trtcm_color_aware_check(
409                 &tm, time, TM_TEST_TRTCM_CBS_DF + 1, in[1]) != out[1])
410                 melog(TRTCM_AWARE_CHECK_MSG" %u:%u", in[1], out[1]);
411
412         if(rte_meter_trtcm_config(&tm, &tparams) != 0)
413                 melog(TRTCM_AWARE_CHECK_MSG);
414         time = rte_get_tsc_cycles() + hz;
415         if(rte_meter_trtcm_color_aware_check(
416                 &tm, time, TM_TEST_TRTCM_PBS_DF - 1, in[2]) != out[2])
417                 melog(TRTCM_AWARE_CHECK_MSG" %u:%u", in[2], out[2]);
418
419         if(rte_meter_trtcm_config(&tm, &tparams) != 0)
420                 melog(TRTCM_AWARE_CHECK_MSG);
421         time = rte_get_tsc_cycles() + hz;
422         if(rte_meter_trtcm_color_aware_check(
423                 &tm, time, TM_TEST_TRTCM_PBS_DF + 1, in[3]) != out[3])
424                 melog(TRTCM_AWARE_CHECK_MSG" %u:%u", in[3], out[3]);
425
426         return 0;
427 }
428
429
430 /**
431  * functional test for rte_meter_trtcm_color_aware_check
432  */
433
434 static inline int
435 tm_test_trtcm_color_aware_check(void)
436 {
437         enum rte_meter_color in[4], out[4];
438         /**
439           * test 4 points that will produce green, yellow, yellow, red flag
440           * if using blind check
441           */
442
443         /* previouly have a green, test points should keep unchanged */
444         in[0] = in[1] = in[2] = in[3] = e_RTE_METER_GREEN;
445         out[0] = e_RTE_METER_GREEN;
446         out[1] = e_RTE_METER_YELLOW;
447         out[2] = e_RTE_METER_YELLOW;
448         out[3] = e_RTE_METER_RED;
449         if(tm_test_trtcm_aware_check(in, out) != 0)
450                 return -1;
451
452         in[0] = in[1] = in[2] = in[3] = e_RTE_METER_YELLOW;
453         out[0] = e_RTE_METER_YELLOW;
454         out[1] = e_RTE_METER_YELLOW;
455         out[2] = e_RTE_METER_YELLOW;
456         out[3] = e_RTE_METER_RED;
457         if(tm_test_trtcm_aware_check(in, out) != 0)
458                 return -1;
459
460         in[0] = in[1] = in[2] = in[3] = e_RTE_METER_RED;
461         out[0] = e_RTE_METER_RED;
462         out[1] = e_RTE_METER_RED;
463         out[2] = e_RTE_METER_RED;
464         out[3] = e_RTE_METER_RED;
465         if(tm_test_trtcm_aware_check(in, out) != 0)
466                 return -1;
467
468         return 0;
469 }
470
471 /**
472  * test main entrance for library meter
473  */
474 static int
475 test_meter(void)
476 {
477         if(tm_test_srtcm_config() != 0 )
478                 return -1;
479
480         if(tm_test_trtcm_config() != 0 )
481                 return -1;
482
483         if(tm_test_srtcm_color_blind_check() != 0)
484                 return -1;
485
486         if(tm_test_trtcm_color_blind_check()!= 0)
487                 return -1;
488
489         if(tm_test_srtcm_color_aware_check()!= 0)
490                 return -1;
491
492         if(tm_test_trtcm_color_aware_check()!= 0)
493                 return -1;
494
495         return 0;
496
497 }
498
499 static struct test_command meter_cmd = {
500         .command = "meter_autotest",
501         .callback = test_meter,
502 };
503 REGISTER_TEST_COMMAND(meter_cmd);
504 #endif /* RTE_LIBRTE_METER */