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