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