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