4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
42 #ifdef RTE_LIBRTE_METER
44 #include <rte_cycles.h>
45 #include <rte_meter.h>
47 #define mlog(format, ...) do{\
48 printf("Line %d:",__LINE__);\
49 printf(format, ##__VA_ARGS__);\
53 #define melog(format, ...) do{\
54 printf("Line %d:",__LINE__);\
55 printf(format, ##__VA_ARGS__);\
56 printf(" failed!\n");\
60 #define TM_TEST_SRTCM_CIR_DF 46000000
61 #define TM_TEST_SRTCM_CBS_DF 2048
62 #define TM_TEST_SRTCM_EBS_DF 4096
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
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,};
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,};
81 * functional test for rte_meter_srtcm_config
84 tm_test_srtcm_config(void)
86 #define SRTCM_CFG_MSG "srtcm_config"
87 struct rte_meter_srtcm sm;
88 struct rte_meter_srtcm_params sparams1;
90 /* invalid parameter test */
91 if(rte_meter_srtcm_config(NULL, NULL) == 0)
93 if(rte_meter_srtcm_config(&sm, NULL) == 0)
95 if(rte_meter_srtcm_config(NULL, &sparams) == 0)
98 /* cbs and ebs can't both be zero */
102 if(rte_meter_srtcm_config(&sm, &sparams1) == 0)
103 melog(SRTCM_CFG_MSG);
105 /* cir should never be 0 */
108 if(rte_meter_srtcm_config(&sm, &sparams1) == 0)
109 melog(SRTCM_CFG_MSG);
111 /* one of ebs and cbs can be zero, should be successful */
114 if(rte_meter_srtcm_config(&sm, &sparams1) != 0)
115 melog(SRTCM_CFG_MSG);
119 if(rte_meter_srtcm_config(&sm, &sparams1) != 0)
120 melog(SRTCM_CFG_MSG);
122 /* usual parameter, should be successful */
123 if(rte_meter_srtcm_config(&sm, &sparams) != 0)
124 melog(SRTCM_CFG_MSG);
131 * functional test for rte_meter_trtcm_config
134 tm_test_trtcm_config(void)
136 struct rte_meter_trtcm tm;
137 struct rte_meter_trtcm_params tparams1;
138 #define TRTCM_CFG_MSG "trtcm_config"
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);
148 /* cir, cbs, pir and pbs never be zero */
151 if(rte_meter_trtcm_config(&tm, &tparams1) == 0)
152 melog(TRTCM_CFG_MSG);
156 if(rte_meter_trtcm_config(&tm, &tparams1) == 0)
157 melog(TRTCM_CFG_MSG);
161 if(rte_meter_trtcm_config(&tm, &tparams1) == 0)
162 melog(TRTCM_CFG_MSG);
166 if(rte_meter_trtcm_config(&tm, &tparams1) == 0)
167 melog(TRTCM_CFG_MSG);
169 /* pir should be greater or equal to cir */
171 tparams1.pir = tparams1.cir - 1;
172 if(rte_meter_trtcm_config(&tm, &tparams1) == 0)
173 melog(TRTCM_CFG_MSG" pir < cir test");
175 /* usual parameter, should be successful */
176 if(rte_meter_trtcm_config(&tm, &tparams) != 0)
177 melog(TRTCM_CFG_MSG);
183 * functional test for rte_meter_srtcm_color_blind_check
186 tm_test_srtcm_color_blind_check(void)
188 #define SRTCM_BLIND_CHECK_MSG "srtcm_blind_check"
189 struct rte_meter_srtcm sm;
191 uint64_t hz = rte_get_tsc_hz();
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");
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");
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");
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)
225 melog(SRTCM_BLIND_CHECK_MSG" RED");
232 * functional test for rte_meter_trtcm_color_blind_check
235 tm_test_trtcm_color_blind_check(void)
237 #define TRTCM_BLIND_CHECK_MSG "trtcm_blind_check"
240 struct rte_meter_trtcm tm;
241 uint64_t hz = rte_get_tsc_hz();
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");
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");
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");
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)
276 melog(TRTCM_BLIND_CHECK_MSG" RED");
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]
292 tm_test_srtcm_aware_check
293 (enum rte_meter_color in[4], enum rte_meter_color out[4])
295 #define SRTCM_AWARE_CHECK_MSG "srtcm_aware_check"
296 struct rte_meter_srtcm sm;
298 uint64_t hz = rte_get_tsc_hz();
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]);
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]);
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]);
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]);
333 * functional test for rte_meter_srtcm_color_aware_check
336 tm_test_srtcm_color_aware_check(void)
338 enum rte_meter_color in[4], out[4];
341 * test 4 points that will produce green, yellow, yellow, red flag
342 * if using blind check
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)
355 * previously have a yellow, green & yellow = yellow
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)
367 * previously have a red, red & green = red
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)
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]
390 tm_test_trtcm_aware_check
391 (enum rte_meter_color in[4], enum rte_meter_color out[4])
393 #define TRTCM_AWARE_CHECK_MSG "trtcm_aware_check"
394 struct rte_meter_trtcm tm;
396 uint64_t hz = rte_get_tsc_hz();
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]);
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]);
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]);
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]);
431 * functional test for rte_meter_trtcm_color_aware_check
435 tm_test_trtcm_color_aware_check(void)
437 enum rte_meter_color in[4], out[4];
439 * test 4 points that will produce green, yellow, yellow, red flag
440 * if using blind check
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)
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)
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)
472 * test main entrance for library meter
477 if(tm_test_srtcm_config() != 0 )
480 if(tm_test_trtcm_config() != 0 )
483 if(tm_test_srtcm_color_blind_check() != 0)
486 if(tm_test_trtcm_color_blind_check()!= 0)
489 if(tm_test_srtcm_color_aware_check()!= 0)
492 if(tm_test_trtcm_color_aware_check()!= 0)
499 #else /* RTE_LIBRTE_METER */
504 printf("The meter library is not included in this build\n");
507 #endif /* RTE_LIBRTE_METER */