app/testpmd: clean metering and policing commands
[dpdk.git] / app / test-pmd / cmdline_mtr.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Intel Corporation
3  */
4
5 #include <cmdline_parse.h>
6 #include <cmdline_parse_num.h>
7 #include <cmdline_parse_string.h>
8
9 #include <rte_ethdev.h>
10 #include <rte_flow.h>
11 #include <rte_mtr.h>
12
13 #include "testpmd.h"
14 #include "cmdline_mtr.h"
15
16 #define PARSE_DELIMITER                         " \f\n\r\t\v"
17 #define MAX_DSCP_TABLE_ENTRIES          64
18
19 /** Display Meter Error Message */
20 static void
21 print_err_msg(struct rte_mtr_error *error)
22 {
23         static const char *const errstrlist[] = {
24                 [RTE_MTR_ERROR_TYPE_NONE] = "no error",
25                 [RTE_MTR_ERROR_TYPE_UNSPECIFIED] = "cause unspecified",
26                 [RTE_MTR_ERROR_TYPE_METER_PROFILE_ID] = "meter profile id",
27                 [RTE_MTR_ERROR_TYPE_METER_PROFILE] = "meter profile null",
28                 [RTE_MTR_ERROR_TYPE_MTR_ID] = "meter id",
29                 [RTE_MTR_ERROR_TYPE_MTR_PARAMS] = "meter params null",
30                 [RTE_MTR_ERROR_TYPE_POLICER_ACTION_GREEN]
31                         = "policer action(green)",
32                 [RTE_MTR_ERROR_TYPE_POLICER_ACTION_YELLOW]
33                         = "policer action(yellow)",
34                 [RTE_MTR_ERROR_TYPE_POLICER_ACTION_RED]
35                         = "policer action(red)",
36                 [RTE_MTR_ERROR_TYPE_STATS_MASK] = "stats mask",
37                 [RTE_MTR_ERROR_TYPE_STATS] = "stats",
38                 [RTE_MTR_ERROR_TYPE_SHARED]
39                         = "shared meter",
40         };
41
42         const char *errstr;
43         char buf[64];
44
45         if ((unsigned int)error->type >= RTE_DIM(errstrlist) ||
46                 !errstrlist[error->type])
47                 errstr = "unknown type";
48         else
49                 errstr = errstrlist[error->type];
50
51         if (error->cause)
52                 snprintf(buf, sizeof(buf), "cause: %p, ", error->cause);
53
54         printf("%s: %s%s (error %d)\n", errstr, error->cause ? buf : "",
55                 error->message ? error->message : "(no stated reason)",
56                 error->type);
57 }
58
59 static int
60 parse_uint(uint64_t *value, const char *str)
61 {
62         char *next = NULL;
63         uint64_t n;
64
65         errno = 0;
66         /* Parse number string */
67         n = strtol(str, &next, 10);
68         if (errno != 0 || str == next || *next != '\0')
69                 return -1;
70
71         *value = n;
72
73         return 0;
74 }
75
76 static int
77 parse_dscp_table_entries(char *str, enum rte_mtr_color *dscp_table)
78 {
79         char *token;
80         int i = 0;
81
82         token = strtok_r(str, PARSE_DELIMITER, &str);
83         if (token == NULL)
84                 return 0;
85
86         /* Allocate memory for dscp table */
87         dscp_table = (enum rte_mtr_color *)malloc(MAX_DSCP_TABLE_ENTRIES *
88                 sizeof(enum rte_mtr_color));
89
90         while (1) {
91                 if (strcmp(token, "G") == 0 ||
92                         strcmp(token, "g") == 0)
93                         dscp_table[i++] = RTE_MTR_GREEN;
94                 else if (strcmp(token, "Y") == 0 ||
95                         strcmp(token, "y") == 0)
96                         dscp_table[i++] = RTE_MTR_YELLOW;
97                 else if (strcmp(token, "R") == 0 ||
98                         strcmp(token, "r") == 0)
99                         dscp_table[i++] = RTE_MTR_RED;
100                 else {
101                         free(dscp_table);
102                         return -1;
103                 }
104                 if (i == MAX_DSCP_TABLE_ENTRIES)
105                         break;
106
107                 token = strtok_r(str, PARSE_DELIMITER, &str);
108                 if (token == NULL)
109                         return -1;
110         }
111         return 0;
112 }
113
114 static int
115 parse_meter_color_str(char *c_str, uint32_t *use_prev_meter_color,
116         enum rte_mtr_color *dscp_table)
117 {
118         char *token;
119         uint64_t previous_mtr_color = 0;
120         int ret;
121
122         /* First token: use previous meter color */
123         token = strtok_r(c_str, PARSE_DELIMITER, &c_str);
124         if (token ==  NULL)
125                 return -1;
126
127         ret = parse_uint(&previous_mtr_color, token);
128         if (ret != 0)
129                 return -1;
130
131         /* Check if previous meter color to be used */
132         if (previous_mtr_color) {
133                 *use_prev_meter_color = previous_mtr_color;
134                 return 0;
135         }
136
137         /* Parse dscp table entries */
138         ret = parse_dscp_table_entries(c_str, dscp_table);
139         if (ret != 0)
140                 return -1;
141
142         return 0;
143 }
144
145 static int
146 string_to_policer_action(char *s)
147 {
148         if ((strcmp(s, "G") == 0) || (strcmp(s, "g") == 0))
149                 return MTR_POLICER_ACTION_COLOR_GREEN;
150
151         if ((strcmp(s, "Y") == 0) || (strcmp(s, "y") == 0))
152                 return MTR_POLICER_ACTION_COLOR_YELLOW;
153
154         if ((strcmp(s, "R") == 0) || (strcmp(s, "r") == 0))
155                 return MTR_POLICER_ACTION_COLOR_RED;
156
157         if ((strcmp(s, "D") == 0) || (strcmp(s, "d") == 0))
158                 return MTR_POLICER_ACTION_DROP;
159
160         return -1;
161 }
162
163 static int
164 parse_policer_action_string(char *p_str, uint32_t action_mask,
165         enum rte_mtr_policer_action actions[])
166 {
167         char *token;
168         int count = __builtin_popcount(action_mask);
169         int g_color = 0, y_color = 0, action, i;
170
171         for (i = 0; i < count; i++) {
172                 token = strtok_r(p_str, PARSE_DELIMITER, &p_str);
173                 if (token ==  NULL)
174                         return -1;
175
176                 action = string_to_policer_action(token);
177                 if (action == -1)
178                         return -1;
179
180                 if (g_color == 0 && (action_mask & 0x1)) {
181                         actions[RTE_MTR_GREEN] = action;
182                         g_color = 1;
183                 } else if (y_color == 0 && (action_mask & 0x2)) {
184                         actions[RTE_MTR_YELLOW] = action;
185                         y_color = 1;
186                 } else
187                         actions[RTE_MTR_RED] = action;
188         }
189         return 0;
190 }
191 /* *** Add Port Meter Profile srtcm_rfc2697 *** */
192 struct cmd_add_port_meter_profile_srtcm_result {
193         cmdline_fixed_string_t add;
194         cmdline_fixed_string_t port;
195         cmdline_fixed_string_t meter;
196         cmdline_fixed_string_t profile;
197         cmdline_fixed_string_t srtcm_rfc2697;
198         uint16_t port_id;
199         uint32_t profile_id;
200         uint64_t cir;
201         uint64_t cbs;
202         uint64_t ebs;
203 };
204
205 cmdline_parse_token_string_t cmd_add_port_meter_profile_srtcm_add =
206         TOKEN_STRING_INITIALIZER(
207                 struct cmd_add_port_meter_profile_srtcm_result, add, "add");
208 cmdline_parse_token_string_t cmd_add_port_meter_profile_srtcm_port =
209         TOKEN_STRING_INITIALIZER(
210                 struct cmd_add_port_meter_profile_srtcm_result,
211                         port, "port");
212 cmdline_parse_token_string_t cmd_add_port_meter_profile_srtcm_meter =
213         TOKEN_STRING_INITIALIZER(
214                 struct cmd_add_port_meter_profile_srtcm_result,
215                         meter, "meter");
216 cmdline_parse_token_string_t cmd_add_port_meter_profile_srtcm_profile =
217         TOKEN_STRING_INITIALIZER(
218                 struct cmd_add_port_meter_profile_srtcm_result,
219                         profile, "profile");
220 cmdline_parse_token_string_t cmd_add_port_meter_profile_srtcm_srtcm_rfc2697 =
221         TOKEN_STRING_INITIALIZER(
222                 struct cmd_add_port_meter_profile_srtcm_result,
223                         srtcm_rfc2697, "srtcm_rfc2697");
224 cmdline_parse_token_num_t cmd_add_port_meter_profile_srtcm_port_id =
225         TOKEN_NUM_INITIALIZER(
226                 struct cmd_add_port_meter_profile_srtcm_result,
227                         port_id, UINT16);
228 cmdline_parse_token_num_t cmd_add_port_meter_profile_srtcm_profile_id =
229         TOKEN_NUM_INITIALIZER(
230                 struct cmd_add_port_meter_profile_srtcm_result,
231                         profile_id, UINT32);
232 cmdline_parse_token_num_t cmd_add_port_meter_profile_srtcm_cir =
233         TOKEN_NUM_INITIALIZER(
234                 struct cmd_add_port_meter_profile_srtcm_result,
235                         cir, UINT64);
236 cmdline_parse_token_num_t cmd_add_port_meter_profile_srtcm_cbs =
237         TOKEN_NUM_INITIALIZER(
238                 struct cmd_add_port_meter_profile_srtcm_result,
239                         cbs, UINT64);
240 cmdline_parse_token_num_t cmd_add_port_meter_profile_srtcm_ebs =
241         TOKEN_NUM_INITIALIZER(
242                 struct cmd_add_port_meter_profile_srtcm_result,
243                         ebs, UINT64);
244
245 static void cmd_add_port_meter_profile_srtcm_parsed(void *parsed_result,
246         __attribute__((unused)) struct cmdline *cl,
247         __attribute__((unused)) void *data)
248 {
249         struct cmd_add_port_meter_profile_srtcm_result *res = parsed_result;
250         struct rte_mtr_meter_profile mp;
251         struct rte_mtr_error error;
252         uint32_t profile_id = res->profile_id;
253         uint16_t port_id = res->port_id;
254         int ret;
255
256         if (port_id_is_invalid(port_id, ENABLED_WARN))
257                 return;
258
259         /* Private shaper profile params */
260         memset(&mp, 0, sizeof(struct rte_mtr_meter_profile));
261         mp.alg = RTE_MTR_SRTCM_RFC2697;
262         mp.srtcm_rfc2697.cir = res->cir;
263         mp.srtcm_rfc2697.cbs = res->cbs;
264         mp.srtcm_rfc2697.ebs = res->ebs;
265
266         ret = rte_mtr_meter_profile_add(port_id, profile_id, &mp, &error);
267         if (ret != 0) {
268                 print_err_msg(&error);
269                 return;
270         }
271 }
272
273 cmdline_parse_inst_t cmd_add_port_meter_profile_srtcm = {
274         .f = cmd_add_port_meter_profile_srtcm_parsed,
275         .data = NULL,
276         .help_str = "Add port meter profile srtcm (rfc2697)",
277         .tokens = {
278                 (void *)&cmd_add_port_meter_profile_srtcm_add,
279                 (void *)&cmd_add_port_meter_profile_srtcm_port,
280                 (void *)&cmd_add_port_meter_profile_srtcm_meter,
281                 (void *)&cmd_add_port_meter_profile_srtcm_profile,
282                 (void *)&cmd_add_port_meter_profile_srtcm_port_id,
283                 (void *)&cmd_add_port_meter_profile_srtcm_profile_id,
284                 (void *)&cmd_add_port_meter_profile_srtcm_srtcm_rfc2697,
285                 (void *)&cmd_add_port_meter_profile_srtcm_cir,
286                 (void *)&cmd_add_port_meter_profile_srtcm_cbs,
287                 (void *)&cmd_add_port_meter_profile_srtcm_ebs,
288                 NULL,
289         },
290 };
291
292 /* *** Add Port Meter Profile trtcm_rfc2698 *** */
293 struct cmd_add_port_meter_profile_trtcm_result {
294         cmdline_fixed_string_t add;
295         cmdline_fixed_string_t port;
296         cmdline_fixed_string_t meter;
297         cmdline_fixed_string_t profile;
298         cmdline_fixed_string_t trtcm_rfc2698;
299         uint16_t port_id;
300         uint32_t profile_id;
301         uint64_t cir;
302         uint64_t pir;
303         uint64_t cbs;
304         uint64_t pbs;
305 };
306
307 cmdline_parse_token_string_t cmd_add_port_meter_profile_trtcm_add =
308         TOKEN_STRING_INITIALIZER(
309                 struct cmd_add_port_meter_profile_trtcm_result, add, "add");
310 cmdline_parse_token_string_t cmd_add_port_meter_profile_trtcm_port =
311         TOKEN_STRING_INITIALIZER(
312                 struct cmd_add_port_meter_profile_trtcm_result,
313                         port, "port");
314 cmdline_parse_token_string_t cmd_add_port_meter_profile_trtcm_meter =
315         TOKEN_STRING_INITIALIZER(
316                 struct cmd_add_port_meter_profile_trtcm_result,
317                         meter, "meter");
318 cmdline_parse_token_string_t cmd_add_port_meter_profile_trtcm_profile =
319         TOKEN_STRING_INITIALIZER(
320                 struct cmd_add_port_meter_profile_trtcm_result,
321                         profile, "profile");
322 cmdline_parse_token_string_t cmd_add_port_meter_profile_trtcm_trtcm_rfc2698 =
323         TOKEN_STRING_INITIALIZER(
324                 struct cmd_add_port_meter_profile_trtcm_result,
325                         trtcm_rfc2698, "trtcm_rfc2698");
326 cmdline_parse_token_num_t cmd_add_port_meter_profile_trtcm_port_id =
327         TOKEN_NUM_INITIALIZER(
328                 struct cmd_add_port_meter_profile_trtcm_result,
329                         port_id, UINT16);
330 cmdline_parse_token_num_t cmd_add_port_meter_profile_trtcm_profile_id =
331         TOKEN_NUM_INITIALIZER(
332                 struct cmd_add_port_meter_profile_trtcm_result,
333                         profile_id, UINT32);
334 cmdline_parse_token_num_t cmd_add_port_meter_profile_trtcm_cir =
335         TOKEN_NUM_INITIALIZER(
336                 struct cmd_add_port_meter_profile_trtcm_result,
337                         cir, UINT64);
338 cmdline_parse_token_num_t cmd_add_port_meter_profile_trtcm_pir =
339         TOKEN_NUM_INITIALIZER(
340                 struct cmd_add_port_meter_profile_trtcm_result,
341                         pir, UINT64);
342 cmdline_parse_token_num_t cmd_add_port_meter_profile_trtcm_cbs =
343         TOKEN_NUM_INITIALIZER(
344                 struct cmd_add_port_meter_profile_trtcm_result,
345                         cbs, UINT64);
346 cmdline_parse_token_num_t cmd_add_port_meter_profile_trtcm_pbs =
347         TOKEN_NUM_INITIALIZER(
348                 struct cmd_add_port_meter_profile_trtcm_result,
349                         pbs, UINT64);
350
351 static void cmd_add_port_meter_profile_trtcm_parsed(void *parsed_result,
352         __attribute__((unused)) struct cmdline *cl,
353         __attribute__((unused)) void *data)
354 {
355         struct cmd_add_port_meter_profile_trtcm_result *res = parsed_result;
356         struct rte_mtr_meter_profile mp;
357         struct rte_mtr_error error;
358         uint32_t profile_id = res->profile_id;
359         uint16_t port_id = res->port_id;
360         int ret;
361
362         if (port_id_is_invalid(port_id, ENABLED_WARN))
363                 return;
364
365         /* Private shaper profile params */
366         memset(&mp, 0, sizeof(struct rte_mtr_meter_profile));
367         mp.alg = RTE_MTR_TRTCM_RFC2698;
368         mp.trtcm_rfc2698.cir = res->cir;
369         mp.trtcm_rfc2698.pir = res->pir;
370         mp.trtcm_rfc2698.cbs = res->cbs;
371         mp.trtcm_rfc2698.pbs = res->pbs;
372
373         ret = rte_mtr_meter_profile_add(port_id, profile_id, &mp, &error);
374         if (ret != 0) {
375                 print_err_msg(&error);
376                 return;
377         }
378 }
379
380 cmdline_parse_inst_t cmd_add_port_meter_profile_trtcm = {
381         .f = cmd_add_port_meter_profile_trtcm_parsed,
382         .data = NULL,
383         .help_str = "Add port meter profile trtcm (rfc2698)",
384         .tokens = {
385                 (void *)&cmd_add_port_meter_profile_trtcm_add,
386                 (void *)&cmd_add_port_meter_profile_trtcm_port,
387                 (void *)&cmd_add_port_meter_profile_trtcm_meter,
388                 (void *)&cmd_add_port_meter_profile_trtcm_profile,
389                 (void *)&cmd_add_port_meter_profile_trtcm_port_id,
390                 (void *)&cmd_add_port_meter_profile_trtcm_profile_id,
391                 (void *)&cmd_add_port_meter_profile_trtcm_trtcm_rfc2698,
392                 (void *)&cmd_add_port_meter_profile_trtcm_cir,
393                 (void *)&cmd_add_port_meter_profile_trtcm_pir,
394                 (void *)&cmd_add_port_meter_profile_trtcm_cbs,
395                 (void *)&cmd_add_port_meter_profile_trtcm_pbs,
396                 NULL,
397         },
398 };
399
400 /* *** Add Port Meter Profile trtcm_rfc4115 *** */
401 struct cmd_add_port_meter_profile_trtcm_rfc4115_result {
402         cmdline_fixed_string_t add;
403         cmdline_fixed_string_t port;
404         cmdline_fixed_string_t meter;
405         cmdline_fixed_string_t profile;
406         cmdline_fixed_string_t trtcm_rfc4115;
407         uint16_t port_id;
408         uint32_t profile_id;
409         uint64_t cir;
410         uint64_t eir;
411         uint64_t cbs;
412         uint64_t ebs;
413 };
414
415 cmdline_parse_token_string_t cmd_add_port_meter_profile_trtcm_rfc4115_add =
416         TOKEN_STRING_INITIALIZER(
417                 struct cmd_add_port_meter_profile_trtcm_rfc4115_result, add,
418                 "add");
419 cmdline_parse_token_string_t cmd_add_port_meter_profile_trtcm_rfc4115_port =
420         TOKEN_STRING_INITIALIZER(
421                 struct cmd_add_port_meter_profile_trtcm_rfc4115_result,
422                         port, "port");
423 cmdline_parse_token_string_t cmd_add_port_meter_profile_trtcm_rfc4115_meter =
424         TOKEN_STRING_INITIALIZER(
425                 struct cmd_add_port_meter_profile_trtcm_rfc4115_result,
426                         meter, "meter");
427 cmdline_parse_token_string_t cmd_add_port_meter_profile_trtcm_rfc4115_profile =
428         TOKEN_STRING_INITIALIZER(
429                 struct cmd_add_port_meter_profile_trtcm_rfc4115_result,
430                         profile, "profile");
431 cmdline_parse_token_string_t
432         cmd_add_port_meter_profile_trtcm_rfc4115_trtcm_rfc4115 =
433         TOKEN_STRING_INITIALIZER(
434                 struct cmd_add_port_meter_profile_trtcm_rfc4115_result,
435                         trtcm_rfc4115, "trtcm_rfc4115");
436 cmdline_parse_token_num_t cmd_add_port_meter_profile_trtcm_rfc4115_port_id =
437         TOKEN_NUM_INITIALIZER(
438                 struct cmd_add_port_meter_profile_trtcm_rfc4115_result,
439                         port_id, UINT16);
440 cmdline_parse_token_num_t cmd_add_port_meter_profile_trtcm_rfc4115_profile_id =
441         TOKEN_NUM_INITIALIZER(
442                 struct cmd_add_port_meter_profile_trtcm_rfc4115_result,
443                         profile_id, UINT32);
444 cmdline_parse_token_num_t cmd_add_port_meter_profile_trtcm_rfc4115_cir =
445         TOKEN_NUM_INITIALIZER(
446                 struct cmd_add_port_meter_profile_trtcm_rfc4115_result,
447                         cir, UINT64);
448 cmdline_parse_token_num_t cmd_add_port_meter_profile_trtcm_rfc4115_eir =
449         TOKEN_NUM_INITIALIZER(
450                 struct cmd_add_port_meter_profile_trtcm_rfc4115_result,
451                         eir, UINT64);
452 cmdline_parse_token_num_t cmd_add_port_meter_profile_trtcm_rfc4115_cbs =
453         TOKEN_NUM_INITIALIZER(
454                 struct cmd_add_port_meter_profile_trtcm_rfc4115_result,
455                         cbs, UINT64);
456 cmdline_parse_token_num_t cmd_add_port_meter_profile_trtcm_rfc4115_ebs =
457         TOKEN_NUM_INITIALIZER(
458                 struct cmd_add_port_meter_profile_trtcm_rfc4115_result,
459                         ebs, UINT64);
460
461 static void cmd_add_port_meter_profile_trtcm_rfc4115_parsed(
462         void *parsed_result,
463         __attribute__((unused)) struct cmdline *cl,
464         __attribute__((unused)) void *data)
465 {
466         struct cmd_add_port_meter_profile_trtcm_rfc4115_result *res =
467                 parsed_result;
468         struct rte_mtr_meter_profile mp;
469         struct rte_mtr_error error;
470         uint32_t profile_id = res->profile_id;
471         uint16_t port_id = res->port_id;
472         int ret;
473
474         if (port_id_is_invalid(port_id, ENABLED_WARN))
475                 return;
476
477         /* Private shaper profile params */
478         memset(&mp, 0, sizeof(struct rte_mtr_meter_profile));
479         mp.alg = RTE_MTR_TRTCM_RFC4115;
480         mp.trtcm_rfc4115.cir = res->cir;
481         mp.trtcm_rfc4115.eir = res->eir;
482         mp.trtcm_rfc4115.cbs = res->cbs;
483         mp.trtcm_rfc4115.ebs = res->ebs;
484
485         ret = rte_mtr_meter_profile_add(port_id, profile_id, &mp, &error);
486         if (ret != 0) {
487                 print_err_msg(&error);
488                 return;
489         }
490 }
491
492 cmdline_parse_inst_t cmd_add_port_meter_profile_trtcm_rfc4115 = {
493         .f = cmd_add_port_meter_profile_trtcm_rfc4115_parsed,
494         .data = NULL,
495         .help_str = "Add port meter profile trtcm (rfc4115)",
496         .tokens = {
497                 (void *)&cmd_add_port_meter_profile_trtcm_rfc4115_add,
498                 (void *)&cmd_add_port_meter_profile_trtcm_rfc4115_port,
499                 (void *)&cmd_add_port_meter_profile_trtcm_rfc4115_meter,
500                 (void *)&cmd_add_port_meter_profile_trtcm_rfc4115_profile,
501                 (void *)&cmd_add_port_meter_profile_trtcm_rfc4115_port_id,
502                 (void *)&cmd_add_port_meter_profile_trtcm_rfc4115_profile_id,
503                 (void *)&cmd_add_port_meter_profile_trtcm_rfc4115_trtcm_rfc4115,
504                 (void *)&cmd_add_port_meter_profile_trtcm_rfc4115_cir,
505                 (void *)&cmd_add_port_meter_profile_trtcm_rfc4115_eir,
506                 (void *)&cmd_add_port_meter_profile_trtcm_rfc4115_cbs,
507                 (void *)&cmd_add_port_meter_profile_trtcm_rfc4115_ebs,
508                 NULL,
509         },
510 };
511
512 /* *** Delete Port Meter Profile *** */
513 struct cmd_del_port_meter_profile_result {
514         cmdline_fixed_string_t del;
515         cmdline_fixed_string_t port;
516         cmdline_fixed_string_t meter;
517         cmdline_fixed_string_t profile;
518         uint16_t port_id;
519         uint32_t profile_id;
520 };
521
522 cmdline_parse_token_string_t cmd_del_port_meter_profile_del =
523         TOKEN_STRING_INITIALIZER(
524                 struct cmd_del_port_meter_profile_result, del, "del");
525 cmdline_parse_token_string_t cmd_del_port_meter_profile_port =
526         TOKEN_STRING_INITIALIZER(
527                 struct cmd_del_port_meter_profile_result,
528                         port, "port");
529 cmdline_parse_token_string_t cmd_del_port_meter_profile_meter =
530         TOKEN_STRING_INITIALIZER(
531                 struct cmd_del_port_meter_profile_result,
532                         meter, "meter");
533 cmdline_parse_token_string_t cmd_del_port_meter_profile_profile =
534         TOKEN_STRING_INITIALIZER(
535                 struct cmd_del_port_meter_profile_result,
536                         profile, "profile");
537 cmdline_parse_token_num_t cmd_del_port_meter_profile_port_id =
538         TOKEN_NUM_INITIALIZER(
539                 struct cmd_del_port_meter_profile_result,
540                         port_id, UINT16);
541 cmdline_parse_token_num_t cmd_del_port_meter_profile_profile_id =
542         TOKEN_NUM_INITIALIZER(
543                 struct cmd_del_port_meter_profile_result,
544                         profile_id, UINT32);
545
546 static void cmd_del_port_meter_profile_parsed(void *parsed_result,
547         __attribute__((unused)) struct cmdline *cl,
548         __attribute__((unused)) void *data)
549 {
550         struct cmd_del_port_meter_profile_result *res = parsed_result;
551         struct rte_mtr_error error;
552         uint32_t profile_id = res->profile_id;
553         uint16_t port_id = res->port_id;
554         int ret;
555
556         if (port_id_is_invalid(port_id, ENABLED_WARN))
557                 return;
558
559         /* Delete meter profile */
560         ret = rte_mtr_meter_profile_delete(port_id, profile_id, &error);
561         if (ret != 0) {
562                 print_err_msg(&error);
563                 return;
564         }
565 }
566
567 cmdline_parse_inst_t cmd_del_port_meter_profile = {
568         .f = cmd_del_port_meter_profile_parsed,
569         .data = NULL,
570         .help_str = "Delete port meter profile",
571         .tokens = {
572                 (void *)&cmd_del_port_meter_profile_del,
573                 (void *)&cmd_del_port_meter_profile_port,
574                 (void *)&cmd_del_port_meter_profile_meter,
575                 (void *)&cmd_del_port_meter_profile_profile,
576                 (void *)&cmd_del_port_meter_profile_port_id,
577                 (void *)&cmd_del_port_meter_profile_profile_id,
578                 NULL,
579         },
580 };
581
582 /* *** Create Port Meter Object *** */
583 struct cmd_create_port_meter_result {
584         cmdline_fixed_string_t create;
585         cmdline_fixed_string_t port;
586         cmdline_fixed_string_t meter;
587         uint16_t port_id;
588         uint32_t mtr_id;
589         uint32_t profile_id;
590         cmdline_fixed_string_t meter_enable;
591         cmdline_fixed_string_t g_action;
592         cmdline_fixed_string_t y_action;
593         cmdline_fixed_string_t r_action;
594         uint64_t statistics_mask;
595         uint32_t shared;
596         cmdline_multi_string_t meter_input_color;
597 };
598
599 cmdline_parse_token_string_t cmd_create_port_meter_create =
600         TOKEN_STRING_INITIALIZER(
601                 struct cmd_create_port_meter_result, create, "create");
602 cmdline_parse_token_string_t cmd_create_port_meter_port =
603         TOKEN_STRING_INITIALIZER(
604                 struct cmd_create_port_meter_result, port, "port");
605 cmdline_parse_token_string_t cmd_create_port_meter_meter =
606         TOKEN_STRING_INITIALIZER(
607                 struct cmd_create_port_meter_result, meter, "meter");
608 cmdline_parse_token_num_t cmd_create_port_meter_port_id =
609         TOKEN_NUM_INITIALIZER(
610                 struct cmd_create_port_meter_result, port_id, UINT16);
611 cmdline_parse_token_num_t cmd_create_port_meter_mtr_id =
612         TOKEN_NUM_INITIALIZER(
613                 struct cmd_create_port_meter_result, mtr_id, UINT32);
614 cmdline_parse_token_num_t cmd_create_port_meter_profile_id =
615         TOKEN_NUM_INITIALIZER(
616                 struct cmd_create_port_meter_result, profile_id, UINT32);
617 cmdline_parse_token_string_t cmd_create_port_meter_meter_enable =
618         TOKEN_STRING_INITIALIZER(struct cmd_create_port_meter_result,
619                 meter_enable, "yes#no");
620 cmdline_parse_token_string_t cmd_create_port_meter_g_action =
621         TOKEN_STRING_INITIALIZER(struct cmd_create_port_meter_result,
622                 g_action, "R#Y#G#D#r#y#g#d");
623 cmdline_parse_token_string_t cmd_create_port_meter_y_action =
624         TOKEN_STRING_INITIALIZER(struct cmd_create_port_meter_result,
625                 y_action, "R#Y#G#D#r#y#g#d");
626 cmdline_parse_token_string_t cmd_create_port_meter_r_action =
627         TOKEN_STRING_INITIALIZER(struct cmd_create_port_meter_result,
628                 r_action, "R#Y#G#D#r#y#g#d");
629 cmdline_parse_token_num_t cmd_create_port_meter_statistics_mask =
630         TOKEN_NUM_INITIALIZER(struct cmd_create_port_meter_result,
631                 statistics_mask, UINT64);
632 cmdline_parse_token_num_t cmd_create_port_meter_shared =
633         TOKEN_NUM_INITIALIZER(struct cmd_create_port_meter_result,
634                 shared, UINT32);
635 cmdline_parse_token_string_t cmd_create_port_meter_input_color =
636         TOKEN_STRING_INITIALIZER(struct cmd_create_port_meter_result,
637                 meter_input_color, TOKEN_STRING_MULTI);
638
639 static void cmd_create_port_meter_parsed(void *parsed_result,
640         __attribute__((unused)) struct cmdline *cl,
641         __attribute__((unused)) void *data)
642 {
643         struct cmd_create_port_meter_result *res = parsed_result;
644         struct rte_mtr_error error;
645         struct rte_mtr_params params;
646         uint32_t mtr_id = res->mtr_id;
647         uint32_t shared = res->shared;
648         uint32_t use_prev_meter_color = 0;
649         uint16_t port_id = res->port_id;
650         enum rte_mtr_color *dscp_table = NULL;
651         char *c_str = res->meter_input_color;
652         int ret;
653
654         if (port_id_is_invalid(port_id, ENABLED_WARN))
655                 return;
656
657         /* Meter params */
658         memset(&params, 0, sizeof(struct rte_mtr_params));
659         params.meter_profile_id = res->profile_id;
660
661         /* Parse meter input color string params */
662         ret = parse_meter_color_str(c_str, &use_prev_meter_color, dscp_table);
663         if (ret) {
664                 printf(" Meter input color params string parse error\n");
665                 return;
666         }
667
668         params.use_prev_mtr_color = use_prev_meter_color;
669         params.dscp_table = dscp_table;
670
671         if (strcmp(res->meter_enable, "yes") == 0)
672                 params.meter_enable = 1;
673         else
674                 params.meter_enable = 0;
675
676         params.action[RTE_MTR_GREEN] =
677                 string_to_policer_action(res->g_action);
678         params.action[RTE_MTR_YELLOW] =
679                 string_to_policer_action(res->y_action);
680         params.action[RTE_MTR_RED] =
681                 string_to_policer_action(res->r_action);
682         params.stats_mask = res->statistics_mask;
683
684         ret = rte_mtr_create(port_id, mtr_id, &params, shared, &error);
685         if (ret != 0) {
686                 free(dscp_table);
687                 print_err_msg(&error);
688                 return;
689         }
690 }
691
692 cmdline_parse_inst_t cmd_create_port_meter = {
693         .f = cmd_create_port_meter_parsed,
694         .data = NULL,
695         .help_str = "Create port meter",
696         .tokens = {
697                 (void *)&cmd_create_port_meter_create,
698                 (void *)&cmd_create_port_meter_port,
699                 (void *)&cmd_create_port_meter_meter,
700                 (void *)&cmd_create_port_meter_port_id,
701                 (void *)&cmd_create_port_meter_mtr_id,
702                 (void *)&cmd_create_port_meter_profile_id,
703                 (void *)&cmd_create_port_meter_meter_enable,
704                 (void *)&cmd_create_port_meter_g_action,
705                 (void *)&cmd_create_port_meter_y_action,
706                 (void *)&cmd_create_port_meter_r_action,
707                 (void *)&cmd_create_port_meter_statistics_mask,
708                 (void *)&cmd_create_port_meter_shared,
709                 (void *)&cmd_create_port_meter_input_color,
710                 NULL,
711         },
712 };
713
714 /* *** Delete Port Meter Object *** */
715 struct cmd_del_port_meter_result {
716         cmdline_fixed_string_t del;
717         cmdline_fixed_string_t port;
718         cmdline_fixed_string_t meter;
719         uint16_t port_id;
720         uint32_t mtr_id;
721 };
722
723 cmdline_parse_token_string_t cmd_del_port_meter_del =
724         TOKEN_STRING_INITIALIZER(
725                 struct cmd_del_port_meter_result, del, "del");
726 cmdline_parse_token_string_t cmd_del_port_meter_port =
727         TOKEN_STRING_INITIALIZER(
728                 struct cmd_del_port_meter_result, port, "port");
729 cmdline_parse_token_string_t cmd_del_port_meter_meter =
730         TOKEN_STRING_INITIALIZER(
731                 struct cmd_del_port_meter_result, meter, "meter");
732 cmdline_parse_token_num_t cmd_del_port_meter_port_id =
733         TOKEN_NUM_INITIALIZER(
734                 struct cmd_del_port_meter_result, port_id, UINT16);
735 cmdline_parse_token_num_t cmd_del_port_meter_mtr_id =
736         TOKEN_NUM_INITIALIZER(
737                 struct cmd_del_port_meter_result, mtr_id, UINT32);
738
739 static void cmd_del_port_meter_parsed(void *parsed_result,
740         __attribute__((unused)) struct cmdline *cl,
741         __attribute__((unused)) void *data)
742 {
743         struct cmd_del_port_meter_result *res = parsed_result;
744         struct rte_mtr_error error;
745         uint32_t mtr_id = res->mtr_id;
746         uint16_t port_id = res->port_id;
747
748         int ret;
749
750         if (port_id_is_invalid(port_id, ENABLED_WARN))
751                 return;
752
753         /* Destroy Meter */
754         ret = rte_mtr_destroy(port_id, mtr_id, &error);
755         if (ret != 0) {
756                 print_err_msg(&error);
757                 return;
758         }
759 }
760
761 cmdline_parse_inst_t cmd_del_port_meter = {
762         .f = cmd_del_port_meter_parsed,
763         .data = NULL,
764         .help_str = "Delete port meter",
765         .tokens = {
766                 (void *)&cmd_del_port_meter_del,
767                 (void *)&cmd_del_port_meter_port,
768                 (void *)&cmd_del_port_meter_meter,
769                 (void *)&cmd_del_port_meter_port_id,
770                 (void *)&cmd_del_port_meter_mtr_id,
771                 NULL,
772         },
773 };
774
775 /* *** Set Port Meter Profile *** */
776 struct cmd_set_port_meter_profile_result {
777         cmdline_fixed_string_t set;
778         cmdline_fixed_string_t port;
779         cmdline_fixed_string_t meter;
780         cmdline_fixed_string_t profile;
781         uint16_t port_id;
782         uint32_t mtr_id;
783         uint32_t profile_id;
784 };
785
786 cmdline_parse_token_string_t cmd_set_port_meter_profile_set =
787         TOKEN_STRING_INITIALIZER(
788                 struct cmd_set_port_meter_profile_result, set, "set");
789 cmdline_parse_token_string_t cmd_set_port_meter_profile_port =
790         TOKEN_STRING_INITIALIZER(
791                 struct cmd_set_port_meter_profile_result, port, "port");
792 cmdline_parse_token_string_t cmd_set_port_meter_profile_meter =
793         TOKEN_STRING_INITIALIZER(
794                 struct cmd_set_port_meter_profile_result, meter, "meter");
795 cmdline_parse_token_string_t cmd_set_port_meter_profile_profile =
796         TOKEN_STRING_INITIALIZER(
797                 struct cmd_set_port_meter_profile_result, profile, "profile");
798 cmdline_parse_token_num_t cmd_set_port_meter_profile_port_id =
799         TOKEN_NUM_INITIALIZER(
800                 struct cmd_set_port_meter_profile_result, port_id, UINT16);
801 cmdline_parse_token_num_t cmd_set_port_meter_profile_mtr_id =
802         TOKEN_NUM_INITIALIZER(
803                 struct cmd_set_port_meter_profile_result, mtr_id, UINT32);
804 cmdline_parse_token_num_t cmd_set_port_meter_profile_profile_id =
805         TOKEN_NUM_INITIALIZER(
806                 struct cmd_set_port_meter_profile_result, profile_id, UINT32);
807
808 static void cmd_set_port_meter_profile_parsed(void *parsed_result,
809         __attribute__((unused)) struct cmdline *cl,
810         __attribute__((unused)) void *data)
811 {
812         struct cmd_set_port_meter_profile_result *res = parsed_result;
813         struct rte_mtr_error error;
814         uint32_t mtr_id = res->mtr_id;
815         uint32_t profile_id = res->profile_id;
816         uint16_t port_id = res->port_id;
817
818         int ret;
819
820         if (port_id_is_invalid(port_id, ENABLED_WARN))
821                 return;
822
823         /* Set meter profile */
824         ret = rte_mtr_meter_profile_update(port_id, mtr_id,
825                 profile_id, &error);
826         if (ret != 0) {
827                 print_err_msg(&error);
828                 return;
829         }
830 }
831
832 cmdline_parse_inst_t cmd_set_port_meter_profile = {
833         .f = cmd_set_port_meter_profile_parsed,
834         .data = NULL,
835         .help_str = "Set port meter profile",
836         .tokens = {
837                 (void *)&cmd_set_port_meter_profile_set,
838                 (void *)&cmd_set_port_meter_profile_port,
839                 (void *)&cmd_set_port_meter_profile_meter,
840                 (void *)&cmd_set_port_meter_profile_profile,
841                 (void *)&cmd_set_port_meter_profile_port_id,
842                 (void *)&cmd_set_port_meter_profile_mtr_id,
843                 (void *)&cmd_set_port_meter_profile_profile_id,
844                 NULL,
845         },
846 };
847
848 /* *** Set Port Meter Policer Action *** */
849 struct cmd_set_port_meter_policer_action_result {
850         cmdline_fixed_string_t set;
851         cmdline_fixed_string_t port;
852         cmdline_fixed_string_t meter;
853         cmdline_fixed_string_t policer;
854         cmdline_fixed_string_t action;
855         uint16_t port_id;
856         uint32_t mtr_id;
857         uint32_t action_mask;
858         cmdline_multi_string_t policer_action;
859 };
860
861 cmdline_parse_token_string_t cmd_set_port_meter_policer_action_set =
862         TOKEN_STRING_INITIALIZER(
863                 struct cmd_set_port_meter_policer_action_result, set, "set");
864 cmdline_parse_token_string_t cmd_set_port_meter_policer_action_port =
865         TOKEN_STRING_INITIALIZER(
866                 struct cmd_set_port_meter_policer_action_result, port, "port");
867 cmdline_parse_token_string_t cmd_set_port_meter_policer_action_meter =
868         TOKEN_STRING_INITIALIZER(
869                 struct cmd_set_port_meter_policer_action_result, meter,
870                 "meter");
871 cmdline_parse_token_string_t cmd_set_port_meter_policer_action_policer =
872         TOKEN_STRING_INITIALIZER(
873                 struct cmd_set_port_meter_policer_action_result, policer,
874                 "policer");
875 cmdline_parse_token_string_t cmd_set_port_meter_policer_action_action =
876         TOKEN_STRING_INITIALIZER(
877                 struct cmd_set_port_meter_policer_action_result, action,
878                 "action");
879 cmdline_parse_token_num_t cmd_set_port_meter_policer_action_port_id =
880         TOKEN_NUM_INITIALIZER(
881                 struct cmd_set_port_meter_policer_action_result, port_id,
882                 UINT16);
883 cmdline_parse_token_num_t cmd_set_port_meter_policer_action_mtr_id =
884         TOKEN_NUM_INITIALIZER(
885                 struct cmd_set_port_meter_policer_action_result, mtr_id,
886                 UINT32);
887 cmdline_parse_token_num_t cmd_set_port_meter_policer_action_action_mask =
888         TOKEN_NUM_INITIALIZER(
889                 struct cmd_set_port_meter_policer_action_result, action_mask,
890                 UINT32);
891 cmdline_parse_token_string_t cmd_set_port_meter_policer_action_policer_action =
892         TOKEN_STRING_INITIALIZER(
893                 struct cmd_set_port_meter_policer_action_result,
894                 policer_action, TOKEN_STRING_MULTI);
895
896 static void cmd_set_port_meter_policer_action_parsed(void *parsed_result,
897         __attribute__((unused)) struct cmdline *cl,
898         __attribute__((unused)) void *data)
899 {
900         struct cmd_set_port_meter_policer_action_result *res = parsed_result;
901         enum rte_mtr_policer_action *actions;
902         struct rte_mtr_error error;
903         uint32_t mtr_id = res->mtr_id;
904         uint32_t action_mask = res->action_mask;
905         uint16_t port_id = res->port_id;
906         char *p_str = res->policer_action;
907         int ret;
908
909         if (port_id_is_invalid(port_id, ENABLED_WARN))
910                 return;
911
912         /* Check: action mask */
913         if (action_mask == 0 || (action_mask & (~0x7UL))) {
914                 printf(" Policer action mask not correct (error)\n");
915                 return;
916         }
917
918         /* Allocate memory for policer actions */
919         actions = (enum rte_mtr_policer_action *)malloc(RTE_MTR_COLORS *
920                 sizeof(enum rte_mtr_policer_action));
921         if (actions == NULL) {
922                 printf("Memory for policer actions not allocated (error)\n");
923                 return;
924         }
925         /* Parse policer action string */
926         ret = parse_policer_action_string(p_str, action_mask, actions);
927         if (ret) {
928                 printf(" Policer action string parse error\n");
929                 free(actions);
930                 return;
931         }
932
933         ret = rte_mtr_policer_actions_update(port_id, mtr_id,
934                 action_mask, actions, &error);
935         if (ret != 0) {
936                 print_err_msg(&error);
937                 return;
938         }
939
940         free(actions);
941 }
942
943 cmdline_parse_inst_t cmd_set_port_meter_policer_action = {
944         .f = cmd_set_port_meter_policer_action_parsed,
945         .data = NULL,
946         .help_str = "Set port meter policer action",
947         .tokens = {
948                 (void *)&cmd_set_port_meter_policer_action_set,
949                 (void *)&cmd_set_port_meter_policer_action_port,
950                 (void *)&cmd_set_port_meter_policer_action_meter,
951                 (void *)&cmd_set_port_meter_policer_action_policer,
952                 (void *)&cmd_set_port_meter_policer_action_action,
953                 (void *)&cmd_set_port_meter_policer_action_port_id,
954                 (void *)&cmd_set_port_meter_policer_action_mtr_id,
955                 (void *)&cmd_set_port_meter_policer_action_action_mask,
956                 (void *)&cmd_set_port_meter_policer_action_policer_action,
957                 NULL,
958         },
959 };
960
961 /* *** Set Port Meter Stats Mask *** */
962 struct cmd_set_port_meter_stats_mask_result {
963         cmdline_fixed_string_t set;
964         cmdline_fixed_string_t port;
965         cmdline_fixed_string_t meter;
966         cmdline_fixed_string_t stats;
967         cmdline_fixed_string_t mask;
968         uint16_t port_id;
969         uint32_t mtr_id;
970         uint64_t stats_mask;
971 };
972
973 cmdline_parse_token_string_t cmd_set_port_meter_stats_mask_set =
974         TOKEN_STRING_INITIALIZER(
975                 struct cmd_set_port_meter_stats_mask_result, set, "set");
976 cmdline_parse_token_string_t cmd_set_port_meter_stats_mask_port =
977         TOKEN_STRING_INITIALIZER(
978                 struct cmd_set_port_meter_stats_mask_result, port, "port");
979 cmdline_parse_token_string_t cmd_set_port_meter_stats_mask_meter =
980         TOKEN_STRING_INITIALIZER(
981                 struct cmd_set_port_meter_stats_mask_result, meter, "meter");
982 cmdline_parse_token_string_t cmd_set_port_meter_stats_mask_stats =
983         TOKEN_STRING_INITIALIZER(
984                 struct cmd_set_port_meter_stats_mask_result, stats, "stats");
985 cmdline_parse_token_string_t cmd_set_port_meter_stats_mask_mask =
986         TOKEN_STRING_INITIALIZER(
987                 struct cmd_set_port_meter_stats_mask_result, mask, "mask");
988 cmdline_parse_token_num_t cmd_set_port_meter_stats_mask_port_id =
989         TOKEN_NUM_INITIALIZER(
990                 struct cmd_set_port_meter_stats_mask_result, port_id, UINT16);
991 cmdline_parse_token_num_t cmd_set_port_meter_stats_mask_mtr_id =
992         TOKEN_NUM_INITIALIZER(
993                 struct cmd_set_port_meter_stats_mask_result, mtr_id, UINT32);
994 cmdline_parse_token_num_t cmd_set_port_meter_stats_mask_stats_mask =
995         TOKEN_NUM_INITIALIZER(
996                 struct cmd_set_port_meter_stats_mask_result, stats_mask,
997                 UINT64);
998
999 static void cmd_set_port_meter_stats_mask_parsed(void *parsed_result,
1000         __attribute__((unused)) struct cmdline *cl,
1001         __attribute__((unused)) void *data)
1002 {
1003         struct cmd_set_port_meter_stats_mask_result *res = parsed_result;
1004         struct rte_mtr_error error;
1005         uint64_t stats_mask = res->stats_mask;
1006         uint32_t mtr_id = res->mtr_id;
1007         uint16_t port_id = res->port_id;
1008         int ret;
1009
1010         if (port_id_is_invalid(port_id, ENABLED_WARN))
1011                 return;
1012
1013         ret = rte_mtr_stats_update(port_id, mtr_id, stats_mask, &error);
1014         if (ret != 0) {
1015                 print_err_msg(&error);
1016                 return;
1017         }
1018 }
1019
1020 cmdline_parse_inst_t cmd_set_port_meter_stats_mask = {
1021         .f = cmd_set_port_meter_stats_mask_parsed,
1022         .data = NULL,
1023         .help_str = "Set port meter stats mask",
1024         .tokens = {
1025                 (void *)&cmd_set_port_meter_stats_mask_set,
1026                 (void *)&cmd_set_port_meter_stats_mask_port,
1027                 (void *)&cmd_set_port_meter_stats_mask_meter,
1028                 (void *)&cmd_set_port_meter_stats_mask_stats,
1029                 (void *)&cmd_set_port_meter_stats_mask_mask,
1030                 (void *)&cmd_set_port_meter_stats_mask_port_id,
1031                 (void *)&cmd_set_port_meter_stats_mask_mtr_id,
1032                 (void *)&cmd_set_port_meter_stats_mask_stats_mask,
1033                 NULL,
1034         },
1035 };
1036
1037 /* *** Show Port Meter Stats *** */
1038 struct cmd_show_port_meter_stats_result {
1039         cmdline_fixed_string_t show;
1040         cmdline_fixed_string_t port;
1041         cmdline_fixed_string_t meter;
1042         cmdline_fixed_string_t stats;
1043         uint16_t port_id;
1044         uint32_t mtr_id;
1045         cmdline_fixed_string_t clear;
1046 };
1047
1048 cmdline_parse_token_string_t cmd_show_port_meter_stats_show =
1049         TOKEN_STRING_INITIALIZER(
1050                 struct cmd_show_port_meter_stats_result, show, "show");
1051 cmdline_parse_token_string_t cmd_show_port_meter_stats_port =
1052         TOKEN_STRING_INITIALIZER(
1053                 struct cmd_show_port_meter_stats_result, port, "port");
1054 cmdline_parse_token_string_t cmd_show_port_meter_stats_meter =
1055         TOKEN_STRING_INITIALIZER(
1056                 struct cmd_show_port_meter_stats_result, meter, "meter");
1057 cmdline_parse_token_string_t cmd_show_port_meter_stats_stats =
1058         TOKEN_STRING_INITIALIZER(
1059                 struct cmd_show_port_meter_stats_result, stats, "stats");
1060 cmdline_parse_token_num_t cmd_show_port_meter_stats_port_id =
1061         TOKEN_NUM_INITIALIZER(
1062                 struct cmd_show_port_meter_stats_result, port_id, UINT16);
1063 cmdline_parse_token_num_t cmd_show_port_meter_stats_mtr_id =
1064         TOKEN_NUM_INITIALIZER(
1065                 struct cmd_show_port_meter_stats_result, mtr_id, UINT32);
1066 cmdline_parse_token_string_t cmd_show_port_meter_stats_clear =
1067         TOKEN_STRING_INITIALIZER(
1068                 struct cmd_show_port_meter_stats_result, clear, "yes#no");
1069
1070 static void cmd_show_port_meter_stats_parsed(void *parsed_result,
1071         __attribute__((unused)) struct cmdline *cl,
1072         __attribute__((unused)) void *data)
1073 {
1074         struct cmd_show_port_meter_stats_result *res = parsed_result;
1075         struct rte_mtr_stats stats;
1076         uint64_t stats_mask = 0;
1077         struct rte_mtr_error error;
1078         uint32_t mtr_id = res->mtr_id;
1079         uint32_t clear = 0;
1080         uint16_t port_id = res->port_id;
1081         int ret;
1082
1083         if (port_id_is_invalid(port_id, ENABLED_WARN))
1084                 return;
1085
1086         if (strcmp(res->clear, "yes") == 0)
1087                 clear = 1;
1088
1089         memset(&stats, 0, sizeof(struct rte_mtr_stats));
1090         ret = rte_mtr_stats_read(port_id, mtr_id, &stats,
1091                 &stats_mask, clear, &error);
1092         if (ret != 0) {
1093                 print_err_msg(&error);
1094                 return;
1095         }
1096
1097         /* Display stats */
1098         if (stats_mask & RTE_MTR_STATS_N_PKTS_GREEN)
1099                 printf("\tPkts G: %" PRIu64 "\n",
1100                         stats.n_pkts[RTE_MTR_GREEN]);
1101         if (stats_mask & RTE_MTR_STATS_N_BYTES_GREEN)
1102                 printf("\tBytes G: %" PRIu64 "\n",
1103                         stats.n_bytes[RTE_MTR_GREEN]);
1104         if (stats_mask & RTE_MTR_STATS_N_PKTS_YELLOW)
1105                 printf("\tPkts Y: %" PRIu64 "\n",
1106                         stats.n_pkts[RTE_MTR_YELLOW]);
1107         if (stats_mask & RTE_MTR_STATS_N_BYTES_YELLOW)
1108                 printf("\tBytes Y: %" PRIu64 "\n",
1109                         stats.n_bytes[RTE_MTR_YELLOW]);
1110         if (stats_mask & RTE_MTR_STATS_N_PKTS_RED)
1111                 printf("\tPkts R: %" PRIu64 "\n",
1112                         stats.n_pkts[RTE_MTR_RED]);
1113         if (stats_mask & RTE_MTR_STATS_N_BYTES_RED)
1114                 printf("\tBytes Y: %" PRIu64 "\n",
1115                         stats.n_bytes[RTE_MTR_RED]);
1116         if (stats_mask & RTE_MTR_STATS_N_PKTS_DROPPED)
1117                 printf("\tPkts DROPPED: %" PRIu64 "\n",
1118                         stats.n_pkts_dropped);
1119         if (stats_mask & RTE_MTR_STATS_N_BYTES_DROPPED)
1120                 printf("\tBytes DROPPED: %" PRIu64 "\n",
1121                         stats.n_bytes_dropped);
1122 }
1123
1124 cmdline_parse_inst_t cmd_show_port_meter_stats = {
1125         .f = cmd_show_port_meter_stats_parsed,
1126         .data = NULL,
1127         .help_str = "Show port meter stats",
1128         .tokens = {
1129                 (void *)&cmd_show_port_meter_stats_show,
1130                 (void *)&cmd_show_port_meter_stats_port,
1131                 (void *)&cmd_show_port_meter_stats_meter,
1132                 (void *)&cmd_show_port_meter_stats_stats,
1133                 (void *)&cmd_show_port_meter_stats_port_id,
1134                 (void *)&cmd_show_port_meter_stats_mtr_id,
1135                 (void *)&cmd_show_port_meter_stats_clear,
1136                 NULL,
1137         },
1138 };