3b14906f43b37d857d28c6b709f7636e02f1f523
[dpdk.git] / app / test / test_meter.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without 
8  *   modification, are permitted provided that the following conditions 
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright 
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright 
14  *       notice, this list of conditions and the following disclaimer in 
15  *       the documentation and/or other materials provided with the 
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its 
18  *       contributors may be used to endorse or promote products derived 
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  * 
33  */
34
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <stdint.h>
39 #include <unistd.h>
40
41 #include <cmdline_parse.h>
42
43 #include "test.h"
44
45 #ifdef RTE_LIBRTE_METER
46
47 #include <rte_cycles.h>
48 #include <rte_meter.h>
49
50 #define mlog(format, ...) do{\
51                 printf("Line %d:",__LINE__);\
52                 printf(format, ##__VA_ARGS__);\
53                 printf("\n");\
54         }while(0);
55
56 #define melog(format, ...) do{\
57                 printf("Line %d:",__LINE__);\
58                 printf(format, ##__VA_ARGS__);\
59                 printf(" failed!\n");\
60                 return -1;\
61         }while(0);
62
63 #define TM_TEST_SRTCM_CIR_DF 46000000
64 #define TM_TEST_SRTCM_CBS_DF 2048
65 #define TM_TEST_SRTCM_EBS_DF 4096
66
67 #define TM_TEST_TRTCM_CIR_DF 46000000
68 #define TM_TEST_TRTCM_PIR_DF 69000000
69 #define TM_TEST_TRTCM_CBS_DF 2048
70 #define TM_TEST_TRTCM_PBS_DF 4096
71
72 static struct rte_meter_srtcm_params sparams = 
73                                 {.cir = TM_TEST_SRTCM_CIR_DF,  
74                                  .cbs = TM_TEST_SRTCM_CBS_DF, 
75                                  .ebs = TM_TEST_SRTCM_EBS_DF,};
76
77 static struct   rte_meter_trtcm_params tparams= 
78                                 {.cir = TM_TEST_TRTCM_CIR_DF,  
79                                  .pir = TM_TEST_TRTCM_PIR_DF,  
80                                  .cbs = TM_TEST_TRTCM_CBS_DF, 
81                                  .pbs = TM_TEST_TRTCM_PBS_DF,};
82
83 /**
84  * functional test for rte_meter_srtcm_config
85  */
86 static inline int 
87 tm_test_srtcm_config(void)
88 {
89 #define SRTCM_CFG_MSG "srtcm_config"
90         struct rte_meter_srtcm sm;
91         struct  rte_meter_srtcm_params sparams1;
92
93         /* invalid parameter test */
94         if(rte_meter_srtcm_config(NULL, NULL) == 0)
95                 melog(SRTCM_CFG_MSG);
96         if(rte_meter_srtcm_config(&sm, NULL) == 0)
97                 melog(SRTCM_CFG_MSG);
98         if(rte_meter_srtcm_config(NULL, &sparams) == 0)
99                 melog(SRTCM_CFG_MSG);
100
101         /* cbs and ebs can't both be zero */
102         sparams1 = sparams;
103         sparams1.cbs = 0;
104         sparams1.ebs = 0;
105         if(rte_meter_srtcm_config(&sm, &sparams1) == 0)
106                 melog(SRTCM_CFG_MSG);
107
108         /* cir should never be 0 */
109         sparams1 = sparams;
110         sparams1.cir = 0;
111         if(rte_meter_srtcm_config(&sm, &sparams1) == 0)
112                 melog(SRTCM_CFG_MSG);
113
114         /* one of ebs and cbs can be zero, should be successful */
115         sparams1 = sparams;
116         sparams1.ebs = 0;
117         if(rte_meter_srtcm_config(&sm, &sparams1) != 0)
118                 melog(SRTCM_CFG_MSG);   
119
120         sparams1 = sparams;
121         sparams1.cbs = 0;
122         if(rte_meter_srtcm_config(&sm, &sparams1) != 0)
123                 melog(SRTCM_CFG_MSG);   
124
125         /* usual parameter, should be successful */
126         if(rte_meter_srtcm_config(&sm, &sparams) != 0)
127                 melog(SRTCM_CFG_MSG);   
128
129         return 0;
130
131 }
132
133 /**
134  * functional test for rte_meter_trtcm_config
135  */
136 static inline int 
137 tm_test_trtcm_config(void)
138 {
139         struct rte_meter_trtcm tm;
140         struct  rte_meter_trtcm_params tparams1;
141 #define TRTCM_CFG_MSG "trtcm_config"
142
143         /* invalid parameter test */
144         if(rte_meter_trtcm_config(NULL, NULL) == 0)
145                 melog(TRTCM_CFG_MSG);
146         if(rte_meter_trtcm_config(&tm, NULL) == 0)
147                 melog(TRTCM_CFG_MSG);
148         if(rte_meter_trtcm_config(NULL, &tparams) == 0)
149                 melog(TRTCM_CFG_MSG);
150
151         /* cir, cbs, pir and pbs never be zero */
152         tparams1 = tparams;
153         tparams1.cir = 0;
154         if(rte_meter_trtcm_config(&tm, &tparams1) == 0)
155                 melog(TRTCM_CFG_MSG);
156
157         tparams1 = tparams;
158         tparams1.cbs = 0;
159         if(rte_meter_trtcm_config(&tm, &tparams1) == 0)
160                 melog(TRTCM_CFG_MSG);
161
162         tparams1 = tparams;
163         tparams1.pbs = 0;
164         if(rte_meter_trtcm_config(&tm, &tparams1) == 0)
165                 melog(TRTCM_CFG_MSG);                   
166
167         tparams1 = tparams;
168         tparams1.pir = 0;
169         if(rte_meter_trtcm_config(&tm, &tparams1) == 0)
170                 melog(TRTCM_CFG_MSG);   
171
172         /* pir should be greater or equal to cir */
173         tparams1 = tparams;
174         tparams1.pir = tparams1.cir - 1;
175         if(rte_meter_trtcm_config(&tm, &tparams1) == 0)
176                 melog(TRTCM_CFG_MSG" pir < cir test");
177
178         /* usual parameter, should be successful */
179         if(rte_meter_trtcm_config(&tm, &tparams) != 0)
180                 melog(TRTCM_CFG_MSG);   
181
182         return 0;
183 }
184
185 /**
186  * functional test for rte_meter_srtcm_color_blind_check
187  */
188 static inline int 
189 tm_test_srtcm_color_blind_check(void)
190 {
191 #define SRTCM_BLIND_CHECK_MSG "srtcm_blind_check"
192         struct rte_meter_srtcm sm;
193         uint64_t time;
194         uint64_t hz = rte_get_tsc_hz();
195         
196         /* Test green */
197         if(rte_meter_srtcm_config(&sm, &sparams) != 0)
198                 melog(SRTCM_BLIND_CHECK_MSG);
199         time = rte_get_tsc_cycles() + hz;
200         if(rte_meter_srtcm_color_blind_check(
201                 &sm, time, TM_TEST_SRTCM_CBS_DF - 1) 
202                 != e_RTE_METER_GREEN)
203                 melog(SRTCM_BLIND_CHECK_MSG" GREEN");
204         
205         /* Test yellow */
206         if(rte_meter_srtcm_config(&sm, &sparams) != 0)
207                 melog(SRTCM_BLIND_CHECK_MSG);
208         time = rte_get_tsc_cycles() + hz;
209         if(rte_meter_srtcm_color_blind_check(
210                 &sm, time, TM_TEST_SRTCM_CBS_DF + 1) 
211                 != e_RTE_METER_YELLOW)
212                 melog(SRTCM_BLIND_CHECK_MSG" YELLOW");
213
214         if(rte_meter_srtcm_config(&sm, &sparams) != 0)
215                 melog(SRTCM_BLIND_CHECK_MSG);
216         time = rte_get_tsc_cycles() + hz;
217         if(rte_meter_srtcm_color_blind_check(
218                 &sm, time, (uint32_t)sm.ebs - 1) != e_RTE_METER_YELLOW)
219                 melog(SRTCM_BLIND_CHECK_MSG" YELLOW");
220
221         /* Test red */
222         if(rte_meter_srtcm_config(&sm, &sparams) != 0)
223                 melog(SRTCM_BLIND_CHECK_MSG);
224         time = rte_get_tsc_cycles() + hz;
225         if(rte_meter_srtcm_color_blind_check(
226                 &sm, time, TM_TEST_SRTCM_EBS_DF + 1) 
227                 != e_RTE_METER_RED)
228                 melog(SRTCM_BLIND_CHECK_MSG" RED");
229         
230         return 0;
231
232 }
233
234 /**
235  * functional test for rte_meter_trtcm_color_blind_check
236  */
237 static inline int 
238 tm_test_trtcm_color_blind_check(void)
239 {
240 #define TRTCM_BLIND_CHECK_MSG "trtcm_blind_check"
241
242         uint64_t time;
243         struct rte_meter_trtcm tm;
244         uint64_t hz = rte_get_tsc_hz();
245         
246         /* Test green */
247         if(rte_meter_trtcm_config(&tm, &tparams) != 0)
248                 melog(TRTCM_BLIND_CHECK_MSG);
249         time = rte_get_tsc_cycles() + hz;
250         if(rte_meter_trtcm_color_blind_check(
251                 &tm, time, TM_TEST_TRTCM_CBS_DF - 1) 
252                 != e_RTE_METER_GREEN)
253                 melog(TRTCM_BLIND_CHECK_MSG" GREEN");
254         
255         /* Test yellow */
256         if(rte_meter_trtcm_config(&tm, &tparams) != 0)
257                 melog(TRTCM_BLIND_CHECK_MSG);
258         time = rte_get_tsc_cycles() + hz;
259         if(rte_meter_trtcm_color_blind_check(
260                 &tm, time, TM_TEST_TRTCM_CBS_DF + 1) 
261                 != e_RTE_METER_YELLOW)
262                 melog(TRTCM_BLIND_CHECK_MSG" YELLOW");
263
264         if(rte_meter_trtcm_config(&tm, &tparams) != 0)
265                 melog(TRTCM_BLIND_CHECK_MSG);
266         time = rte_get_tsc_cycles() + hz;
267         if(rte_meter_trtcm_color_blind_check(
268                 &tm, time, TM_TEST_TRTCM_PBS_DF - 1) 
269                 != e_RTE_METER_YELLOW)
270                 melog(TRTCM_BLIND_CHECK_MSG" YELLOW");
271
272         /* Test red */
273         if(rte_meter_trtcm_config(&tm, &tparams) != 0)
274                 melog(TRTCM_BLIND_CHECK_MSG);
275         time = rte_get_tsc_cycles() + hz;
276         if(rte_meter_trtcm_color_blind_check(
277                 &tm, time, TM_TEST_TRTCM_PBS_DF + 1) 
278                 != e_RTE_METER_RED)
279                 melog(TRTCM_BLIND_CHECK_MSG" RED");
280
281         return 0;
282 }
283
284
285 /**
286  * @in[4] : the flags packets carries.
287  * @in[4] : the flags function expect to return.
288  * It will do blind check at the time of 1 second from beginning. 
289  * At the time, it will use packets length of cbs -1, cbs + 1, 
290  * ebs -1 and ebs +1 with flag in[0], in[1], in[2] and in[3] to do
291  * aware check, expect flag out[0], out[1], out[2] and out[3]
292  */
293
294 static inline int 
295 tm_test_srtcm_aware_check
296 (enum rte_meter_color in[4], enum rte_meter_color out[4])
297 {
298 #define SRTCM_AWARE_CHECK_MSG "srtcm_aware_check"
299         struct rte_meter_srtcm sm;
300         uint64_t time;
301         uint64_t hz = rte_get_tsc_hz();
302         
303         if(rte_meter_srtcm_config(&sm, &sparams) != 0)
304                 melog(SRTCM_AWARE_CHECK_MSG);
305         time = rte_get_tsc_cycles() + hz;
306         if(rte_meter_srtcm_color_aware_check(
307                 &sm, time, TM_TEST_SRTCM_CBS_DF - 1, in[0]) != out[0])
308                 melog(SRTCM_AWARE_CHECK_MSG" %u:%u", in[0], out[0]);
309         
310         if(rte_meter_srtcm_config(&sm, &sparams) != 0)
311                 melog(SRTCM_AWARE_CHECK_MSG);
312         time = rte_get_tsc_cycles() + hz;
313         if(rte_meter_srtcm_color_aware_check(
314                 &sm, time, TM_TEST_SRTCM_CBS_DF + 1, in[1]) != out[1])
315                 melog(SRTCM_AWARE_CHECK_MSG" %u:%u", in[1], out[1]);
316
317         if(rte_meter_srtcm_config(&sm, &sparams) != 0)
318                 melog(SRTCM_AWARE_CHECK_MSG);
319         time = rte_get_tsc_cycles() + hz;
320         if(rte_meter_srtcm_color_aware_check(
321                 &sm, time, TM_TEST_SRTCM_EBS_DF - 1, in[2]) != out[2])
322                 melog(SRTCM_AWARE_CHECK_MSG" %u:%u", in[2], out[2]);
323
324         if(rte_meter_srtcm_config(&sm, &sparams) != 0)
325                 melog(SRTCM_AWARE_CHECK_MSG);
326         time = rte_get_tsc_cycles() + hz;
327         if(rte_meter_srtcm_color_aware_check(
328                 &sm, time, TM_TEST_SRTCM_EBS_DF + 1, in[3]) != out[3])
329                 melog(SRTCM_AWARE_CHECK_MSG" %u:%u", in[3], out[3]);
330         
331         return 0;
332 }
333
334
335 /**
336  * functional test for rte_meter_srtcm_color_aware_check
337  */
338 static inline int 
339 tm_test_srtcm_color_aware_check(void)
340 {
341         enum rte_meter_color in[4], out[4];
342
343         /**
344           * test 4 points that will produce green, yellow, yellow, red flag 
345           * if using blind check 
346           */
347
348         /* previouly have a green, test points should keep unchanged */
349         in[0] = in[1] = in[2] = in[3] = e_RTE_METER_GREEN;
350         out[0] = e_RTE_METER_GREEN;
351         out[1] = e_RTE_METER_YELLOW;
352         out[2] = e_RTE_METER_YELLOW;
353         out[3] = e_RTE_METER_RED;
354         if(tm_test_srtcm_aware_check(in, out) != 0)
355                 return -1;
356
357         /**
358           * previously have a yellow, green & yellow = yellow 
359           * yellow & red = red
360           */
361         in[0] = in[1] = in[2] = in[3] = e_RTE_METER_YELLOW;
362         out[0] = e_RTE_METER_YELLOW;
363         out[1] = e_RTE_METER_YELLOW;
364         out[2] = e_RTE_METER_YELLOW;
365         out[3] = e_RTE_METER_RED;
366         if(tm_test_srtcm_aware_check(in, out) != 0)
367                 return -1;
368         
369         /**
370           * previously have a red, red & green = red 
371           * red & yellow = red
372           */
373         in[0] = in[1] = in[2] = in[3] = e_RTE_METER_RED;
374         out[0] = e_RTE_METER_RED;
375         out[1] = e_RTE_METER_RED;
376         out[2] = e_RTE_METER_RED;
377         out[3] = e_RTE_METER_RED;
378         if(tm_test_srtcm_aware_check(in, out) != 0)
379                 return -1;
380
381         return 0;
382 }
383
384 /**
385  * @in[4] : the flags packets carries.
386  * @in[4] : the flags function expect to return.
387  * It will do blind check at the time of 1 second from beginning. 
388  * At the time, it will use packets length of cbs -1, cbs + 1, 
389  * ebs -1 and ebs +1 with flag in[0], in[1], in[2] and in[3] to do
390  * aware check, expect flag out[0], out[1], out[2] and out[3]
391  */
392 static inline int 
393 tm_test_trtcm_aware_check
394 (enum rte_meter_color in[4], enum rte_meter_color out[4])
395 {
396 #define TRTCM_AWARE_CHECK_MSG "trtcm_aware_check"
397         struct rte_meter_trtcm tm;
398         uint64_t time;
399         uint64_t hz = rte_get_tsc_hz();
400         
401         if(rte_meter_trtcm_config(&tm, &tparams) != 0)
402                 melog(TRTCM_AWARE_CHECK_MSG);
403         time = rte_get_tsc_cycles() + hz;
404         if(rte_meter_trtcm_color_aware_check(
405                 &tm, time, TM_TEST_TRTCM_CBS_DF - 1, in[0]) != out[0])
406                 melog(TRTCM_AWARE_CHECK_MSG" %u:%u", in[0], out[0]);
407         
408         if(rte_meter_trtcm_config(&tm, &tparams) != 0)
409                 melog(TRTCM_AWARE_CHECK_MSG);
410         time = rte_get_tsc_cycles() + hz;
411         if(rte_meter_trtcm_color_aware_check(
412                 &tm, time, TM_TEST_TRTCM_CBS_DF + 1, in[1]) != out[1])
413                 melog(TRTCM_AWARE_CHECK_MSG" %u:%u", in[1], out[1]);
414
415         if(rte_meter_trtcm_config(&tm, &tparams) != 0)
416                 melog(TRTCM_AWARE_CHECK_MSG);
417         time = rte_get_tsc_cycles() + hz;
418         if(rte_meter_trtcm_color_aware_check(
419                 &tm, time, TM_TEST_TRTCM_PBS_DF - 1, in[2]) != out[2])
420                 melog(TRTCM_AWARE_CHECK_MSG" %u:%u", in[2], out[2]);
421
422         if(rte_meter_trtcm_config(&tm, &tparams) != 0)
423                 melog(TRTCM_AWARE_CHECK_MSG);
424         time = rte_get_tsc_cycles() + hz;
425         if(rte_meter_trtcm_color_aware_check(
426                 &tm, time, TM_TEST_TRTCM_PBS_DF + 1, in[3]) != out[3])
427                 melog(TRTCM_AWARE_CHECK_MSG" %u:%u", in[3], out[3]);
428         
429         return 0;
430 }
431
432
433 /**
434  * functional test for rte_meter_trtcm_color_aware_check
435  */
436
437 static inline int 
438 tm_test_trtcm_color_aware_check(void)
439 {
440         enum rte_meter_color in[4], out[4];
441         /**
442           * test 4 points that will produce green, yellow, yellow, red flag 
443           * if using blind check 
444           */
445
446         /* previouly have a green, test points should keep unchanged */
447         in[0] = in[1] = in[2] = in[3] = e_RTE_METER_GREEN;
448         out[0] = e_RTE_METER_GREEN;
449         out[1] = e_RTE_METER_YELLOW;
450         out[2] = e_RTE_METER_YELLOW;
451         out[3] = e_RTE_METER_RED;
452         if(tm_test_trtcm_aware_check(in, out) != 0)
453                 return -1;
454         
455         in[0] = in[1] = in[2] = in[3] = e_RTE_METER_YELLOW;
456         out[0] = e_RTE_METER_YELLOW;
457         out[1] = e_RTE_METER_YELLOW;
458         out[2] = e_RTE_METER_YELLOW;
459         out[3] = e_RTE_METER_RED;
460         if(tm_test_trtcm_aware_check(in, out) != 0)
461                 return -1;
462         
463         in[0] = in[1] = in[2] = in[3] = e_RTE_METER_RED;
464         out[0] = e_RTE_METER_RED;
465         out[1] = e_RTE_METER_RED;
466         out[2] = e_RTE_METER_RED;
467         out[3] = e_RTE_METER_RED;
468         if(tm_test_trtcm_aware_check(in, out) != 0)
469                 return -1;
470
471         return 0;
472 }
473
474 /**
475  * test main entrance for library meter
476  */
477 int 
478 test_meter(void)
479 {
480         if(tm_test_srtcm_config() != 0 )
481                 return -1;
482
483         if(tm_test_trtcm_config() != 0 )
484                 return -1;      
485
486         if(tm_test_srtcm_color_blind_check() != 0)
487                 return -1;
488
489         if(tm_test_trtcm_color_blind_check()!= 0)
490                 return -1;
491
492         if(tm_test_srtcm_color_aware_check()!= 0)
493                 return -1;
494         
495         if(tm_test_trtcm_color_aware_check()!= 0)
496                 return -1;
497         
498         return 0;
499
500 }
501
502 #else /* RTE_LIBRTE_METER */
503
504 int
505 test_meter(void)
506 {
507         printf("The meter library is not included in this build\n");
508         return 0;
509 }
510 #endif /* RTE_LIBRTE_METER */