net/vmxnet3: remove useless copy when setting MAC address
[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
192 static int
193 parse_multi_token_string(char *t_str, uint16_t *port_id,
194         uint32_t *mtr_id, enum rte_mtr_color *dscp_table)
195 {
196         char *token;
197         uint64_t val;
198         int ret;
199
200         /* First token: port id */
201         token = strtok_r(t_str, PARSE_DELIMITER, &t_str);
202         if (token ==  NULL)
203                 return -1;
204
205         ret = parse_uint(&val, token);
206         if (ret != 0 || val > UINT16_MAX)
207                 return -1;
208
209         *port_id = val;
210
211         /* Second token: meter id */
212         token = strtok_r(t_str, PARSE_DELIMITER, &t_str);
213         if (token == NULL)
214                 return 0;
215
216         ret = parse_uint(&val, token);
217         if (ret != 0 || val > UINT32_MAX)
218                 return -1;
219
220         *mtr_id = val;
221
222         ret = parse_dscp_table_entries(t_str, dscp_table);
223         if (ret != 0)
224                 return -1;
225
226         return 0;
227 }
228
229 /* *** Show Port Meter Capabilities *** */
230 struct cmd_show_port_meter_cap_result {
231         cmdline_fixed_string_t show;
232         cmdline_fixed_string_t port;
233         cmdline_fixed_string_t meter;
234         cmdline_fixed_string_t cap;
235         uint16_t port_id;
236 };
237
238 cmdline_parse_token_string_t cmd_show_port_meter_cap_show =
239         TOKEN_STRING_INITIALIZER(
240                 struct cmd_show_port_meter_cap_result, show, "show");
241 cmdline_parse_token_string_t cmd_show_port_meter_cap_port =
242         TOKEN_STRING_INITIALIZER(
243                 struct cmd_show_port_meter_cap_result, port, "port");
244 cmdline_parse_token_string_t cmd_show_port_meter_cap_meter =
245         TOKEN_STRING_INITIALIZER(
246                 struct cmd_show_port_meter_cap_result, meter, "meter");
247 cmdline_parse_token_string_t cmd_show_port_meter_cap_cap =
248         TOKEN_STRING_INITIALIZER(
249                 struct cmd_show_port_meter_cap_result, cap, "cap");
250 cmdline_parse_token_num_t cmd_show_port_meter_cap_port_id =
251         TOKEN_NUM_INITIALIZER(
252                 struct cmd_show_port_meter_cap_result, port_id, UINT16);
253
254 static void cmd_show_port_meter_cap_parsed(void *parsed_result,
255         __attribute__((unused)) struct cmdline *cl,
256         __attribute__((unused)) void *data)
257 {
258         struct cmd_show_port_meter_cap_result *res = parsed_result;
259         struct rte_mtr_capabilities cap;
260         struct rte_mtr_error error;
261         uint16_t port_id = res->port_id;
262         int ret;
263
264         if (port_id_is_invalid(port_id, ENABLED_WARN))
265                 return;
266
267         memset(&cap, 0, sizeof(struct rte_mtr_capabilities));
268         ret = rte_mtr_capabilities_get(port_id, &cap, &error);
269         if (ret) {
270                 print_err_msg(&error);
271                 return;
272         }
273
274         printf("\n****   Port Meter Object Capabilities ****\n\n");
275         printf("cap.n_max %" PRIu32 "\n", cap.n_max);
276         printf("cap.n_shared_max %" PRIu32 "\n", cap.n_shared_max);
277         printf("cap.identical %" PRId32 "\n", cap.identical);
278         printf("cap.shared_identical %" PRId32 "\n",
279                 cap.shared_identical);
280         printf("cap.shared_n_flows_per_mtr_max %" PRIu32 "\n",
281                 cap.shared_n_flows_per_mtr_max);
282         printf("cap.chaining_n_mtrs_per_flow_max %" PRIu32 "\n",
283                 cap.chaining_n_mtrs_per_flow_max);
284         printf("cap.chaining_use_prev_mtr_color_supported %" PRId32 "\n",
285                 cap.chaining_use_prev_mtr_color_supported);
286         printf("cap.chaining_use_prev_mtr_color_enforced %" PRId32 "\n",
287                 cap.chaining_use_prev_mtr_color_enforced);
288         printf("cap.meter_srtcm_rfc2697_n_max %" PRIu32 "\n",
289                 cap.meter_srtcm_rfc2697_n_max);
290         printf("cap.meter_trtcm_rfc2698_n_max %" PRIu32 "\n",
291                 cap.meter_trtcm_rfc2698_n_max);
292         printf("cap.meter_trtcm_rfc4115_n_max %" PRIu32 "\n",
293                 cap.meter_trtcm_rfc4115_n_max);
294         printf("cap.meter_rate_max %" PRIu64 "\n", cap.meter_rate_max);
295         printf("cap.color_aware_srtcm_rfc2697_supported %" PRId32 "\n",
296                 cap.color_aware_srtcm_rfc2697_supported);
297         printf("cap.color_aware_trtcm_rfc2698_supported %" PRId32 "\n",
298                 cap.color_aware_trtcm_rfc2698_supported);
299         printf("cap.color_aware_trtcm_rfc4115_supported %" PRId32 "\n",
300                 cap.color_aware_trtcm_rfc4115_supported);
301         printf("cap.policer_action_recolor_supported %" PRId32 "\n",
302                 cap.policer_action_recolor_supported);
303         printf("cap.policer_action_drop_supported %" PRId32 "\n",
304                 cap.policer_action_drop_supported);
305         printf("cap.stats_mask %" PRIx64 "\n", cap.stats_mask);
306 }
307
308 cmdline_parse_inst_t cmd_show_port_meter_cap = {
309         .f = cmd_show_port_meter_cap_parsed,
310         .data = NULL,
311         .help_str = "Show port meter cap",
312         .tokens = {
313                 (void *)&cmd_show_port_meter_cap_show,
314                 (void *)&cmd_show_port_meter_cap_port,
315                 (void *)&cmd_show_port_meter_cap_meter,
316                 (void *)&cmd_show_port_meter_cap_cap,
317                 (void *)&cmd_show_port_meter_cap_port_id,
318                 NULL,
319         },
320 };
321
322 /* *** Add Port Meter Profile srtcm_rfc2697 *** */
323 struct cmd_add_port_meter_profile_srtcm_result {
324         cmdline_fixed_string_t add;
325         cmdline_fixed_string_t port;
326         cmdline_fixed_string_t meter;
327         cmdline_fixed_string_t profile;
328         cmdline_fixed_string_t srtcm_rfc2697;
329         uint16_t port_id;
330         uint32_t profile_id;
331         uint64_t cir;
332         uint64_t cbs;
333         uint64_t ebs;
334 };
335
336 cmdline_parse_token_string_t cmd_add_port_meter_profile_srtcm_add =
337         TOKEN_STRING_INITIALIZER(
338                 struct cmd_add_port_meter_profile_srtcm_result, add, "add");
339 cmdline_parse_token_string_t cmd_add_port_meter_profile_srtcm_port =
340         TOKEN_STRING_INITIALIZER(
341                 struct cmd_add_port_meter_profile_srtcm_result,
342                         port, "port");
343 cmdline_parse_token_string_t cmd_add_port_meter_profile_srtcm_meter =
344         TOKEN_STRING_INITIALIZER(
345                 struct cmd_add_port_meter_profile_srtcm_result,
346                         meter, "meter");
347 cmdline_parse_token_string_t cmd_add_port_meter_profile_srtcm_profile =
348         TOKEN_STRING_INITIALIZER(
349                 struct cmd_add_port_meter_profile_srtcm_result,
350                         profile, "profile");
351 cmdline_parse_token_string_t cmd_add_port_meter_profile_srtcm_srtcm_rfc2697 =
352         TOKEN_STRING_INITIALIZER(
353                 struct cmd_add_port_meter_profile_srtcm_result,
354                         srtcm_rfc2697, "srtcm_rfc2697");
355 cmdline_parse_token_num_t cmd_add_port_meter_profile_srtcm_port_id =
356         TOKEN_NUM_INITIALIZER(
357                 struct cmd_add_port_meter_profile_srtcm_result,
358                         port_id, UINT16);
359 cmdline_parse_token_num_t cmd_add_port_meter_profile_srtcm_profile_id =
360         TOKEN_NUM_INITIALIZER(
361                 struct cmd_add_port_meter_profile_srtcm_result,
362                         profile_id, UINT32);
363 cmdline_parse_token_num_t cmd_add_port_meter_profile_srtcm_cir =
364         TOKEN_NUM_INITIALIZER(
365                 struct cmd_add_port_meter_profile_srtcm_result,
366                         cir, UINT64);
367 cmdline_parse_token_num_t cmd_add_port_meter_profile_srtcm_cbs =
368         TOKEN_NUM_INITIALIZER(
369                 struct cmd_add_port_meter_profile_srtcm_result,
370                         cbs, UINT64);
371 cmdline_parse_token_num_t cmd_add_port_meter_profile_srtcm_ebs =
372         TOKEN_NUM_INITIALIZER(
373                 struct cmd_add_port_meter_profile_srtcm_result,
374                         ebs, UINT64);
375
376 static void cmd_add_port_meter_profile_srtcm_parsed(void *parsed_result,
377         __attribute__((unused)) struct cmdline *cl,
378         __attribute__((unused)) void *data)
379 {
380         struct cmd_add_port_meter_profile_srtcm_result *res = parsed_result;
381         struct rte_mtr_meter_profile mp;
382         struct rte_mtr_error error;
383         uint32_t profile_id = res->profile_id;
384         uint16_t port_id = res->port_id;
385         int ret;
386
387         if (port_id_is_invalid(port_id, ENABLED_WARN))
388                 return;
389
390         /* Private shaper profile params */
391         memset(&mp, 0, sizeof(struct rte_mtr_meter_profile));
392         mp.alg = RTE_MTR_SRTCM_RFC2697;
393         mp.srtcm_rfc2697.cir = res->cir;
394         mp.srtcm_rfc2697.cbs = res->cbs;
395         mp.srtcm_rfc2697.ebs = res->ebs;
396
397         ret = rte_mtr_meter_profile_add(port_id, profile_id, &mp, &error);
398         if (ret != 0) {
399                 print_err_msg(&error);
400                 return;
401         }
402 }
403
404 cmdline_parse_inst_t cmd_add_port_meter_profile_srtcm = {
405         .f = cmd_add_port_meter_profile_srtcm_parsed,
406         .data = NULL,
407         .help_str = "Add port meter profile srtcm (rfc2697)",
408         .tokens = {
409                 (void *)&cmd_add_port_meter_profile_srtcm_add,
410                 (void *)&cmd_add_port_meter_profile_srtcm_port,
411                 (void *)&cmd_add_port_meter_profile_srtcm_meter,
412                 (void *)&cmd_add_port_meter_profile_srtcm_profile,
413                 (void *)&cmd_add_port_meter_profile_srtcm_port_id,
414                 (void *)&cmd_add_port_meter_profile_srtcm_profile_id,
415                 (void *)&cmd_add_port_meter_profile_srtcm_srtcm_rfc2697,
416                 (void *)&cmd_add_port_meter_profile_srtcm_cir,
417                 (void *)&cmd_add_port_meter_profile_srtcm_cbs,
418                 (void *)&cmd_add_port_meter_profile_srtcm_ebs,
419                 NULL,
420         },
421 };
422
423 /* *** Add Port Meter Profile trtcm_rfc2698 *** */
424 struct cmd_add_port_meter_profile_trtcm_result {
425         cmdline_fixed_string_t add;
426         cmdline_fixed_string_t port;
427         cmdline_fixed_string_t meter;
428         cmdline_fixed_string_t profile;
429         cmdline_fixed_string_t trtcm_rfc2698;
430         uint16_t port_id;
431         uint32_t profile_id;
432         uint64_t cir;
433         uint64_t pir;
434         uint64_t cbs;
435         uint64_t pbs;
436 };
437
438 cmdline_parse_token_string_t cmd_add_port_meter_profile_trtcm_add =
439         TOKEN_STRING_INITIALIZER(
440                 struct cmd_add_port_meter_profile_trtcm_result, add, "add");
441 cmdline_parse_token_string_t cmd_add_port_meter_profile_trtcm_port =
442         TOKEN_STRING_INITIALIZER(
443                 struct cmd_add_port_meter_profile_trtcm_result,
444                         port, "port");
445 cmdline_parse_token_string_t cmd_add_port_meter_profile_trtcm_meter =
446         TOKEN_STRING_INITIALIZER(
447                 struct cmd_add_port_meter_profile_trtcm_result,
448                         meter, "meter");
449 cmdline_parse_token_string_t cmd_add_port_meter_profile_trtcm_profile =
450         TOKEN_STRING_INITIALIZER(
451                 struct cmd_add_port_meter_profile_trtcm_result,
452                         profile, "profile");
453 cmdline_parse_token_string_t cmd_add_port_meter_profile_trtcm_trtcm_rfc2698 =
454         TOKEN_STRING_INITIALIZER(
455                 struct cmd_add_port_meter_profile_trtcm_result,
456                         trtcm_rfc2698, "trtcm_rfc2698");
457 cmdline_parse_token_num_t cmd_add_port_meter_profile_trtcm_port_id =
458         TOKEN_NUM_INITIALIZER(
459                 struct cmd_add_port_meter_profile_trtcm_result,
460                         port_id, UINT16);
461 cmdline_parse_token_num_t cmd_add_port_meter_profile_trtcm_profile_id =
462         TOKEN_NUM_INITIALIZER(
463                 struct cmd_add_port_meter_profile_trtcm_result,
464                         profile_id, UINT32);
465 cmdline_parse_token_num_t cmd_add_port_meter_profile_trtcm_cir =
466         TOKEN_NUM_INITIALIZER(
467                 struct cmd_add_port_meter_profile_trtcm_result,
468                         cir, UINT64);
469 cmdline_parse_token_num_t cmd_add_port_meter_profile_trtcm_pir =
470         TOKEN_NUM_INITIALIZER(
471                 struct cmd_add_port_meter_profile_trtcm_result,
472                         pir, UINT64);
473 cmdline_parse_token_num_t cmd_add_port_meter_profile_trtcm_cbs =
474         TOKEN_NUM_INITIALIZER(
475                 struct cmd_add_port_meter_profile_trtcm_result,
476                         cbs, UINT64);
477 cmdline_parse_token_num_t cmd_add_port_meter_profile_trtcm_pbs =
478         TOKEN_NUM_INITIALIZER(
479                 struct cmd_add_port_meter_profile_trtcm_result,
480                         pbs, UINT64);
481
482 static void cmd_add_port_meter_profile_trtcm_parsed(void *parsed_result,
483         __attribute__((unused)) struct cmdline *cl,
484         __attribute__((unused)) void *data)
485 {
486         struct cmd_add_port_meter_profile_trtcm_result *res = parsed_result;
487         struct rte_mtr_meter_profile mp;
488         struct rte_mtr_error error;
489         uint32_t profile_id = res->profile_id;
490         uint16_t port_id = res->port_id;
491         int ret;
492
493         if (port_id_is_invalid(port_id, ENABLED_WARN))
494                 return;
495
496         /* Private shaper profile params */
497         memset(&mp, 0, sizeof(struct rte_mtr_meter_profile));
498         mp.alg = RTE_MTR_TRTCM_RFC2698;
499         mp.trtcm_rfc2698.cir = res->cir;
500         mp.trtcm_rfc2698.pir = res->pir;
501         mp.trtcm_rfc2698.cbs = res->cbs;
502         mp.trtcm_rfc2698.pbs = res->pbs;
503
504         ret = rte_mtr_meter_profile_add(port_id, profile_id, &mp, &error);
505         if (ret != 0) {
506                 print_err_msg(&error);
507                 return;
508         }
509 }
510
511 cmdline_parse_inst_t cmd_add_port_meter_profile_trtcm = {
512         .f = cmd_add_port_meter_profile_trtcm_parsed,
513         .data = NULL,
514         .help_str = "Add port meter profile trtcm (rfc2698)",
515         .tokens = {
516                 (void *)&cmd_add_port_meter_profile_trtcm_add,
517                 (void *)&cmd_add_port_meter_profile_trtcm_port,
518                 (void *)&cmd_add_port_meter_profile_trtcm_meter,
519                 (void *)&cmd_add_port_meter_profile_trtcm_profile,
520                 (void *)&cmd_add_port_meter_profile_trtcm_port_id,
521                 (void *)&cmd_add_port_meter_profile_trtcm_profile_id,
522                 (void *)&cmd_add_port_meter_profile_trtcm_trtcm_rfc2698,
523                 (void *)&cmd_add_port_meter_profile_trtcm_cir,
524                 (void *)&cmd_add_port_meter_profile_trtcm_pir,
525                 (void *)&cmd_add_port_meter_profile_trtcm_cbs,
526                 (void *)&cmd_add_port_meter_profile_trtcm_pbs,
527                 NULL,
528         },
529 };
530
531 /* *** Add Port Meter Profile trtcm_rfc4115 *** */
532 struct cmd_add_port_meter_profile_trtcm_rfc4115_result {
533         cmdline_fixed_string_t add;
534         cmdline_fixed_string_t port;
535         cmdline_fixed_string_t meter;
536         cmdline_fixed_string_t profile;
537         cmdline_fixed_string_t trtcm_rfc4115;
538         uint16_t port_id;
539         uint32_t profile_id;
540         uint64_t cir;
541         uint64_t eir;
542         uint64_t cbs;
543         uint64_t ebs;
544 };
545
546 cmdline_parse_token_string_t cmd_add_port_meter_profile_trtcm_rfc4115_add =
547         TOKEN_STRING_INITIALIZER(
548                 struct cmd_add_port_meter_profile_trtcm_rfc4115_result, add,
549                 "add");
550 cmdline_parse_token_string_t cmd_add_port_meter_profile_trtcm_rfc4115_port =
551         TOKEN_STRING_INITIALIZER(
552                 struct cmd_add_port_meter_profile_trtcm_rfc4115_result,
553                         port, "port");
554 cmdline_parse_token_string_t cmd_add_port_meter_profile_trtcm_rfc4115_meter =
555         TOKEN_STRING_INITIALIZER(
556                 struct cmd_add_port_meter_profile_trtcm_rfc4115_result,
557                         meter, "meter");
558 cmdline_parse_token_string_t cmd_add_port_meter_profile_trtcm_rfc4115_profile =
559         TOKEN_STRING_INITIALIZER(
560                 struct cmd_add_port_meter_profile_trtcm_rfc4115_result,
561                         profile, "profile");
562 cmdline_parse_token_string_t
563         cmd_add_port_meter_profile_trtcm_rfc4115_trtcm_rfc4115 =
564         TOKEN_STRING_INITIALIZER(
565                 struct cmd_add_port_meter_profile_trtcm_rfc4115_result,
566                         trtcm_rfc4115, "trtcm_rfc4115");
567 cmdline_parse_token_num_t cmd_add_port_meter_profile_trtcm_rfc4115_port_id =
568         TOKEN_NUM_INITIALIZER(
569                 struct cmd_add_port_meter_profile_trtcm_rfc4115_result,
570                         port_id, UINT16);
571 cmdline_parse_token_num_t cmd_add_port_meter_profile_trtcm_rfc4115_profile_id =
572         TOKEN_NUM_INITIALIZER(
573                 struct cmd_add_port_meter_profile_trtcm_rfc4115_result,
574                         profile_id, UINT32);
575 cmdline_parse_token_num_t cmd_add_port_meter_profile_trtcm_rfc4115_cir =
576         TOKEN_NUM_INITIALIZER(
577                 struct cmd_add_port_meter_profile_trtcm_rfc4115_result,
578                         cir, UINT64);
579 cmdline_parse_token_num_t cmd_add_port_meter_profile_trtcm_rfc4115_eir =
580         TOKEN_NUM_INITIALIZER(
581                 struct cmd_add_port_meter_profile_trtcm_rfc4115_result,
582                         eir, UINT64);
583 cmdline_parse_token_num_t cmd_add_port_meter_profile_trtcm_rfc4115_cbs =
584         TOKEN_NUM_INITIALIZER(
585                 struct cmd_add_port_meter_profile_trtcm_rfc4115_result,
586                         cbs, UINT64);
587 cmdline_parse_token_num_t cmd_add_port_meter_profile_trtcm_rfc4115_ebs =
588         TOKEN_NUM_INITIALIZER(
589                 struct cmd_add_port_meter_profile_trtcm_rfc4115_result,
590                         ebs, UINT64);
591
592 static void cmd_add_port_meter_profile_trtcm_rfc4115_parsed(
593         void *parsed_result,
594         __attribute__((unused)) struct cmdline *cl,
595         __attribute__((unused)) void *data)
596 {
597         struct cmd_add_port_meter_profile_trtcm_rfc4115_result *res =
598                 parsed_result;
599         struct rte_mtr_meter_profile mp;
600         struct rte_mtr_error error;
601         uint32_t profile_id = res->profile_id;
602         uint16_t port_id = res->port_id;
603         int ret;
604
605         if (port_id_is_invalid(port_id, ENABLED_WARN))
606                 return;
607
608         /* Private shaper profile params */
609         memset(&mp, 0, sizeof(struct rte_mtr_meter_profile));
610         mp.alg = RTE_MTR_TRTCM_RFC4115;
611         mp.trtcm_rfc4115.cir = res->cir;
612         mp.trtcm_rfc4115.eir = res->eir;
613         mp.trtcm_rfc4115.cbs = res->cbs;
614         mp.trtcm_rfc4115.ebs = res->ebs;
615
616         ret = rte_mtr_meter_profile_add(port_id, profile_id, &mp, &error);
617         if (ret != 0) {
618                 print_err_msg(&error);
619                 return;
620         }
621 }
622
623 cmdline_parse_inst_t cmd_add_port_meter_profile_trtcm_rfc4115 = {
624         .f = cmd_add_port_meter_profile_trtcm_rfc4115_parsed,
625         .data = NULL,
626         .help_str = "Add port meter profile trtcm (rfc4115)",
627         .tokens = {
628                 (void *)&cmd_add_port_meter_profile_trtcm_rfc4115_add,
629                 (void *)&cmd_add_port_meter_profile_trtcm_rfc4115_port,
630                 (void *)&cmd_add_port_meter_profile_trtcm_rfc4115_meter,
631                 (void *)&cmd_add_port_meter_profile_trtcm_rfc4115_profile,
632                 (void *)&cmd_add_port_meter_profile_trtcm_rfc4115_port_id,
633                 (void *)&cmd_add_port_meter_profile_trtcm_rfc4115_profile_id,
634                 (void *)&cmd_add_port_meter_profile_trtcm_rfc4115_trtcm_rfc4115,
635                 (void *)&cmd_add_port_meter_profile_trtcm_rfc4115_cir,
636                 (void *)&cmd_add_port_meter_profile_trtcm_rfc4115_eir,
637                 (void *)&cmd_add_port_meter_profile_trtcm_rfc4115_cbs,
638                 (void *)&cmd_add_port_meter_profile_trtcm_rfc4115_ebs,
639                 NULL,
640         },
641 };
642
643 /* *** Delete Port Meter Profile *** */
644 struct cmd_del_port_meter_profile_result {
645         cmdline_fixed_string_t del;
646         cmdline_fixed_string_t port;
647         cmdline_fixed_string_t meter;
648         cmdline_fixed_string_t profile;
649         uint16_t port_id;
650         uint32_t profile_id;
651 };
652
653 cmdline_parse_token_string_t cmd_del_port_meter_profile_del =
654         TOKEN_STRING_INITIALIZER(
655                 struct cmd_del_port_meter_profile_result, del, "del");
656 cmdline_parse_token_string_t cmd_del_port_meter_profile_port =
657         TOKEN_STRING_INITIALIZER(
658                 struct cmd_del_port_meter_profile_result,
659                         port, "port");
660 cmdline_parse_token_string_t cmd_del_port_meter_profile_meter =
661         TOKEN_STRING_INITIALIZER(
662                 struct cmd_del_port_meter_profile_result,
663                         meter, "meter");
664 cmdline_parse_token_string_t cmd_del_port_meter_profile_profile =
665         TOKEN_STRING_INITIALIZER(
666                 struct cmd_del_port_meter_profile_result,
667                         profile, "profile");
668 cmdline_parse_token_num_t cmd_del_port_meter_profile_port_id =
669         TOKEN_NUM_INITIALIZER(
670                 struct cmd_del_port_meter_profile_result,
671                         port_id, UINT16);
672 cmdline_parse_token_num_t cmd_del_port_meter_profile_profile_id =
673         TOKEN_NUM_INITIALIZER(
674                 struct cmd_del_port_meter_profile_result,
675                         profile_id, UINT32);
676
677 static void cmd_del_port_meter_profile_parsed(void *parsed_result,
678         __attribute__((unused)) struct cmdline *cl,
679         __attribute__((unused)) void *data)
680 {
681         struct cmd_del_port_meter_profile_result *res = parsed_result;
682         struct rte_mtr_error error;
683         uint32_t profile_id = res->profile_id;
684         uint16_t port_id = res->port_id;
685         int ret;
686
687         if (port_id_is_invalid(port_id, ENABLED_WARN))
688                 return;
689
690         /* Delete meter profile */
691         ret = rte_mtr_meter_profile_delete(port_id, profile_id, &error);
692         if (ret != 0) {
693                 print_err_msg(&error);
694                 return;
695         }
696 }
697
698 cmdline_parse_inst_t cmd_del_port_meter_profile = {
699         .f = cmd_del_port_meter_profile_parsed,
700         .data = NULL,
701         .help_str = "Delete port meter profile",
702         .tokens = {
703                 (void *)&cmd_del_port_meter_profile_del,
704                 (void *)&cmd_del_port_meter_profile_port,
705                 (void *)&cmd_del_port_meter_profile_meter,
706                 (void *)&cmd_del_port_meter_profile_profile,
707                 (void *)&cmd_del_port_meter_profile_port_id,
708                 (void *)&cmd_del_port_meter_profile_profile_id,
709                 NULL,
710         },
711 };
712
713 /* *** Create Port Meter Object *** */
714 struct cmd_create_port_meter_result {
715         cmdline_fixed_string_t create;
716         cmdline_fixed_string_t port;
717         cmdline_fixed_string_t meter;
718         uint16_t port_id;
719         uint32_t mtr_id;
720         uint32_t profile_id;
721         cmdline_fixed_string_t meter_enable;
722         cmdline_fixed_string_t g_action;
723         cmdline_fixed_string_t y_action;
724         cmdline_fixed_string_t r_action;
725         uint64_t statistics_mask;
726         uint32_t shared;
727         cmdline_multi_string_t meter_input_color;
728 };
729
730 cmdline_parse_token_string_t cmd_create_port_meter_create =
731         TOKEN_STRING_INITIALIZER(
732                 struct cmd_create_port_meter_result, create, "create");
733 cmdline_parse_token_string_t cmd_create_port_meter_port =
734         TOKEN_STRING_INITIALIZER(
735                 struct cmd_create_port_meter_result, port, "port");
736 cmdline_parse_token_string_t cmd_create_port_meter_meter =
737         TOKEN_STRING_INITIALIZER(
738                 struct cmd_create_port_meter_result, meter, "meter");
739 cmdline_parse_token_num_t cmd_create_port_meter_port_id =
740         TOKEN_NUM_INITIALIZER(
741                 struct cmd_create_port_meter_result, port_id, UINT16);
742 cmdline_parse_token_num_t cmd_create_port_meter_mtr_id =
743         TOKEN_NUM_INITIALIZER(
744                 struct cmd_create_port_meter_result, mtr_id, UINT32);
745 cmdline_parse_token_num_t cmd_create_port_meter_profile_id =
746         TOKEN_NUM_INITIALIZER(
747                 struct cmd_create_port_meter_result, profile_id, UINT32);
748 cmdline_parse_token_string_t cmd_create_port_meter_meter_enable =
749         TOKEN_STRING_INITIALIZER(struct cmd_create_port_meter_result,
750                 meter_enable, "yes#no");
751 cmdline_parse_token_string_t cmd_create_port_meter_g_action =
752         TOKEN_STRING_INITIALIZER(struct cmd_create_port_meter_result,
753                 g_action, "R#Y#G#D#r#y#g#d");
754 cmdline_parse_token_string_t cmd_create_port_meter_y_action =
755         TOKEN_STRING_INITIALIZER(struct cmd_create_port_meter_result,
756                 y_action, "R#Y#G#D#r#y#g#d");
757 cmdline_parse_token_string_t cmd_create_port_meter_r_action =
758         TOKEN_STRING_INITIALIZER(struct cmd_create_port_meter_result,
759                 r_action, "R#Y#G#D#r#y#g#d");
760 cmdline_parse_token_num_t cmd_create_port_meter_statistics_mask =
761         TOKEN_NUM_INITIALIZER(struct cmd_create_port_meter_result,
762                 statistics_mask, UINT64);
763 cmdline_parse_token_num_t cmd_create_port_meter_shared =
764         TOKEN_NUM_INITIALIZER(struct cmd_create_port_meter_result,
765                 shared, UINT32);
766 cmdline_parse_token_string_t cmd_create_port_meter_input_color =
767         TOKEN_STRING_INITIALIZER(struct cmd_create_port_meter_result,
768                 meter_input_color, TOKEN_STRING_MULTI);
769
770 static void cmd_create_port_meter_parsed(void *parsed_result,
771         __attribute__((unused)) struct cmdline *cl,
772         __attribute__((unused)) void *data)
773 {
774         struct cmd_create_port_meter_result *res = parsed_result;
775         struct rte_mtr_error error;
776         struct rte_mtr_params params;
777         uint32_t mtr_id = res->mtr_id;
778         uint32_t shared = res->shared;
779         uint32_t use_prev_meter_color = 0;
780         uint16_t port_id = res->port_id;
781         enum rte_mtr_color *dscp_table = NULL;
782         char *c_str = res->meter_input_color;
783         int ret;
784
785         if (port_id_is_invalid(port_id, ENABLED_WARN))
786                 return;
787
788         /* Meter params */
789         memset(&params, 0, sizeof(struct rte_mtr_params));
790         params.meter_profile_id = res->profile_id;
791
792         /* Parse meter input color string params */
793         ret = parse_meter_color_str(c_str, &use_prev_meter_color, dscp_table);
794         if (ret) {
795                 printf(" Meter input color params string parse error\n");
796                 return;
797         }
798
799         params.use_prev_mtr_color = use_prev_meter_color;
800         params.dscp_table = dscp_table;
801
802         if (strcmp(res->meter_enable, "yes") == 0)
803                 params.meter_enable = 1;
804         else
805                 params.meter_enable = 0;
806
807         params.action[RTE_MTR_GREEN] =
808                 string_to_policer_action(res->g_action);
809         params.action[RTE_MTR_YELLOW] =
810                 string_to_policer_action(res->y_action);
811         params.action[RTE_MTR_RED] =
812                 string_to_policer_action(res->r_action);
813         params.stats_mask = res->statistics_mask;
814
815         ret = rte_mtr_create(port_id, mtr_id, &params, shared, &error);
816         if (ret != 0) {
817                 free(dscp_table);
818                 print_err_msg(&error);
819                 return;
820         }
821 }
822
823 cmdline_parse_inst_t cmd_create_port_meter = {
824         .f = cmd_create_port_meter_parsed,
825         .data = NULL,
826         .help_str = "Create port meter",
827         .tokens = {
828                 (void *)&cmd_create_port_meter_create,
829                 (void *)&cmd_create_port_meter_port,
830                 (void *)&cmd_create_port_meter_meter,
831                 (void *)&cmd_create_port_meter_port_id,
832                 (void *)&cmd_create_port_meter_mtr_id,
833                 (void *)&cmd_create_port_meter_profile_id,
834                 (void *)&cmd_create_port_meter_meter_enable,
835                 (void *)&cmd_create_port_meter_g_action,
836                 (void *)&cmd_create_port_meter_y_action,
837                 (void *)&cmd_create_port_meter_r_action,
838                 (void *)&cmd_create_port_meter_statistics_mask,
839                 (void *)&cmd_create_port_meter_shared,
840                 (void *)&cmd_create_port_meter_input_color,
841                 NULL,
842         },
843 };
844
845 /* *** Enable Meter of MTR Object  *** */
846 struct cmd_enable_port_meter_result {
847         cmdline_fixed_string_t enable;
848         cmdline_fixed_string_t port;
849         cmdline_fixed_string_t meter;
850         uint16_t port_id;
851         uint32_t mtr_id;
852 };
853
854 cmdline_parse_token_string_t cmd_enable_port_meter_enable =
855         TOKEN_STRING_INITIALIZER(
856                 struct cmd_enable_port_meter_result, enable, "enable");
857 cmdline_parse_token_string_t cmd_enable_port_meter_port =
858         TOKEN_STRING_INITIALIZER(
859                 struct cmd_enable_port_meter_result, port, "port");
860 cmdline_parse_token_string_t cmd_enable_port_meter_meter =
861         TOKEN_STRING_INITIALIZER(
862                 struct cmd_enable_port_meter_result, meter, "meter");
863 cmdline_parse_token_num_t cmd_enable_port_meter_port_id =
864         TOKEN_NUM_INITIALIZER(
865                 struct cmd_enable_port_meter_result, port_id, UINT16);
866 cmdline_parse_token_num_t cmd_enable_port_meter_mtr_id =
867         TOKEN_NUM_INITIALIZER(
868                 struct cmd_enable_port_meter_result, mtr_id, UINT32);
869
870 static void cmd_enable_port_meter_parsed(void *parsed_result,
871         __attribute__((unused)) struct cmdline *cl,
872         __attribute__((unused)) void *data)
873 {
874         struct cmd_enable_port_meter_result *res = parsed_result;
875         struct rte_mtr_error error;
876         uint32_t mtr_id = res->mtr_id;
877         uint16_t port_id = res->port_id;
878
879         int ret;
880
881         if (port_id_is_invalid(port_id, ENABLED_WARN))
882                 return;
883
884         /* Enable Meter */
885         ret = rte_mtr_meter_enable(port_id, mtr_id, &error);
886         if (ret != 0) {
887                 print_err_msg(&error);
888                 return;
889         }
890 }
891
892 cmdline_parse_inst_t cmd_enable_port_meter = {
893         .f = cmd_enable_port_meter_parsed,
894         .data = NULL,
895         .help_str = "Enable port meter",
896         .tokens = {
897                 (void *)&cmd_enable_port_meter_enable,
898                 (void *)&cmd_enable_port_meter_port,
899                 (void *)&cmd_enable_port_meter_meter,
900                 (void *)&cmd_enable_port_meter_port_id,
901                 (void *)&cmd_enable_port_meter_mtr_id,
902                 NULL,
903         },
904 };
905
906 /* *** Disable Meter of MTR Object  *** */
907 struct cmd_disable_port_meter_result {
908         cmdline_fixed_string_t disable;
909         cmdline_fixed_string_t port;
910         cmdline_fixed_string_t meter;
911         uint16_t port_id;
912         uint32_t mtr_id;
913 };
914
915 cmdline_parse_token_string_t cmd_disable_port_meter_disable =
916         TOKEN_STRING_INITIALIZER(
917                 struct cmd_disable_port_meter_result, disable, "disable");
918 cmdline_parse_token_string_t cmd_disable_port_meter_port =
919         TOKEN_STRING_INITIALIZER(
920                 struct cmd_disable_port_meter_result, port, "port");
921 cmdline_parse_token_string_t cmd_disable_port_meter_meter =
922         TOKEN_STRING_INITIALIZER(
923                 struct cmd_disable_port_meter_result, meter, "meter");
924 cmdline_parse_token_num_t cmd_disable_port_meter_port_id =
925         TOKEN_NUM_INITIALIZER(
926                 struct cmd_disable_port_meter_result, port_id, UINT16);
927 cmdline_parse_token_num_t cmd_disable_port_meter_mtr_id =
928         TOKEN_NUM_INITIALIZER(
929                 struct cmd_disable_port_meter_result, mtr_id, UINT32);
930
931 static void cmd_disable_port_meter_parsed(void *parsed_result,
932         __attribute__((unused)) struct cmdline *cl,
933         __attribute__((unused)) void *data)
934 {
935         struct cmd_disable_port_meter_result *res = parsed_result;
936         struct rte_mtr_error error;
937         uint32_t mtr_id = res->mtr_id;
938         uint16_t port_id = res->port_id;
939
940         int ret;
941
942         if (port_id_is_invalid(port_id, ENABLED_WARN))
943                 return;
944
945         /* Disable Meter */
946         ret = rte_mtr_meter_disable(port_id, mtr_id, &error);
947         if (ret != 0) {
948                 print_err_msg(&error);
949                 return;
950         }
951 }
952
953 cmdline_parse_inst_t cmd_disable_port_meter = {
954         .f = cmd_disable_port_meter_parsed,
955         .data = NULL,
956         .help_str = "Disable port meter",
957         .tokens = {
958                 (void *)&cmd_disable_port_meter_disable,
959                 (void *)&cmd_disable_port_meter_port,
960                 (void *)&cmd_disable_port_meter_meter,
961                 (void *)&cmd_disable_port_meter_port_id,
962                 (void *)&cmd_disable_port_meter_mtr_id,
963                 NULL,
964         },
965 };
966
967 /* *** Delete Port Meter Object *** */
968 struct cmd_del_port_meter_result {
969         cmdline_fixed_string_t del;
970         cmdline_fixed_string_t port;
971         cmdline_fixed_string_t meter;
972         uint16_t port_id;
973         uint32_t mtr_id;
974 };
975
976 cmdline_parse_token_string_t cmd_del_port_meter_del =
977         TOKEN_STRING_INITIALIZER(
978                 struct cmd_del_port_meter_result, del, "del");
979 cmdline_parse_token_string_t cmd_del_port_meter_port =
980         TOKEN_STRING_INITIALIZER(
981                 struct cmd_del_port_meter_result, port, "port");
982 cmdline_parse_token_string_t cmd_del_port_meter_meter =
983         TOKEN_STRING_INITIALIZER(
984                 struct cmd_del_port_meter_result, meter, "meter");
985 cmdline_parse_token_num_t cmd_del_port_meter_port_id =
986         TOKEN_NUM_INITIALIZER(
987                 struct cmd_del_port_meter_result, port_id, UINT16);
988 cmdline_parse_token_num_t cmd_del_port_meter_mtr_id =
989         TOKEN_NUM_INITIALIZER(
990                 struct cmd_del_port_meter_result, mtr_id, UINT32);
991
992 static void cmd_del_port_meter_parsed(void *parsed_result,
993         __attribute__((unused)) struct cmdline *cl,
994         __attribute__((unused)) void *data)
995 {
996         struct cmd_del_port_meter_result *res = parsed_result;
997         struct rte_mtr_error error;
998         uint32_t mtr_id = res->mtr_id;
999         uint16_t port_id = res->port_id;
1000
1001         int ret;
1002
1003         if (port_id_is_invalid(port_id, ENABLED_WARN))
1004                 return;
1005
1006         /* Destroy Meter */
1007         ret = rte_mtr_destroy(port_id, mtr_id, &error);
1008         if (ret != 0) {
1009                 print_err_msg(&error);
1010                 return;
1011         }
1012 }
1013
1014 cmdline_parse_inst_t cmd_del_port_meter = {
1015         .f = cmd_del_port_meter_parsed,
1016         .data = NULL,
1017         .help_str = "Delete port meter",
1018         .tokens = {
1019                 (void *)&cmd_del_port_meter_del,
1020                 (void *)&cmd_del_port_meter_port,
1021                 (void *)&cmd_del_port_meter_meter,
1022                 (void *)&cmd_del_port_meter_port_id,
1023                 (void *)&cmd_del_port_meter_mtr_id,
1024                 NULL,
1025         },
1026 };
1027
1028 /* *** Set Port Meter Profile *** */
1029 struct cmd_set_port_meter_profile_result {
1030         cmdline_fixed_string_t set;
1031         cmdline_fixed_string_t port;
1032         cmdline_fixed_string_t meter;
1033         cmdline_fixed_string_t profile;
1034         uint16_t port_id;
1035         uint32_t mtr_id;
1036         uint32_t profile_id;
1037 };
1038
1039 cmdline_parse_token_string_t cmd_set_port_meter_profile_set =
1040         TOKEN_STRING_INITIALIZER(
1041                 struct cmd_set_port_meter_profile_result, set, "set");
1042 cmdline_parse_token_string_t cmd_set_port_meter_profile_port =
1043         TOKEN_STRING_INITIALIZER(
1044                 struct cmd_set_port_meter_profile_result, port, "port");
1045 cmdline_parse_token_string_t cmd_set_port_meter_profile_meter =
1046         TOKEN_STRING_INITIALIZER(
1047                 struct cmd_set_port_meter_profile_result, meter, "meter");
1048 cmdline_parse_token_string_t cmd_set_port_meter_profile_profile =
1049         TOKEN_STRING_INITIALIZER(
1050                 struct cmd_set_port_meter_profile_result, profile, "profile");
1051 cmdline_parse_token_num_t cmd_set_port_meter_profile_port_id =
1052         TOKEN_NUM_INITIALIZER(
1053                 struct cmd_set_port_meter_profile_result, port_id, UINT16);
1054 cmdline_parse_token_num_t cmd_set_port_meter_profile_mtr_id =
1055         TOKEN_NUM_INITIALIZER(
1056                 struct cmd_set_port_meter_profile_result, mtr_id, UINT32);
1057 cmdline_parse_token_num_t cmd_set_port_meter_profile_profile_id =
1058         TOKEN_NUM_INITIALIZER(
1059                 struct cmd_set_port_meter_profile_result, profile_id, UINT32);
1060
1061 static void cmd_set_port_meter_profile_parsed(void *parsed_result,
1062         __attribute__((unused)) struct cmdline *cl,
1063         __attribute__((unused)) void *data)
1064 {
1065         struct cmd_set_port_meter_profile_result *res = parsed_result;
1066         struct rte_mtr_error error;
1067         uint32_t mtr_id = res->mtr_id;
1068         uint32_t profile_id = res->profile_id;
1069         uint16_t port_id = res->port_id;
1070
1071         int ret;
1072
1073         if (port_id_is_invalid(port_id, ENABLED_WARN))
1074                 return;
1075
1076         /* Set meter profile */
1077         ret = rte_mtr_meter_profile_update(port_id, mtr_id,
1078                 profile_id, &error);
1079         if (ret != 0) {
1080                 print_err_msg(&error);
1081                 return;
1082         }
1083 }
1084
1085 cmdline_parse_inst_t cmd_set_port_meter_profile = {
1086         .f = cmd_set_port_meter_profile_parsed,
1087         .data = NULL,
1088         .help_str = "Set port meter profile",
1089         .tokens = {
1090                 (void *)&cmd_set_port_meter_profile_set,
1091                 (void *)&cmd_set_port_meter_profile_port,
1092                 (void *)&cmd_set_port_meter_profile_meter,
1093                 (void *)&cmd_set_port_meter_profile_profile,
1094                 (void *)&cmd_set_port_meter_profile_port_id,
1095                 (void *)&cmd_set_port_meter_profile_mtr_id,
1096                 (void *)&cmd_set_port_meter_profile_profile_id,
1097                 NULL,
1098         },
1099 };
1100
1101 /* *** Set Port Meter DSCP Table *** */
1102 struct cmd_set_port_meter_dscp_table_result {
1103         cmdline_fixed_string_t set;
1104         cmdline_fixed_string_t port;
1105         cmdline_fixed_string_t meter;
1106         cmdline_fixed_string_t dscp_table;
1107         cmdline_multi_string_t token_string;
1108 };
1109
1110 cmdline_parse_token_string_t cmd_set_port_meter_dscp_table_set =
1111         TOKEN_STRING_INITIALIZER(
1112                 struct cmd_set_port_meter_dscp_table_result, set, "set");
1113 cmdline_parse_token_string_t cmd_set_port_meter_dscp_table_port =
1114         TOKEN_STRING_INITIALIZER(
1115                 struct cmd_set_port_meter_dscp_table_result, port, "port");
1116 cmdline_parse_token_string_t cmd_set_port_meter_dscp_table_meter =
1117         TOKEN_STRING_INITIALIZER(
1118                 struct cmd_set_port_meter_dscp_table_result, meter, "meter");
1119 cmdline_parse_token_string_t cmd_set_port_meter_dscp_table_dscp_table =
1120         TOKEN_STRING_INITIALIZER(
1121                 struct cmd_set_port_meter_dscp_table_result,
1122                 dscp_table, "dscp table");
1123 cmdline_parse_token_string_t cmd_set_port_meter_dscp_table_token_string =
1124         TOKEN_STRING_INITIALIZER(struct cmd_set_port_meter_dscp_table_result,
1125                 token_string, TOKEN_STRING_MULTI);
1126
1127 static void cmd_set_port_meter_dscp_table_parsed(void *parsed_result,
1128         __attribute__((unused)) struct cmdline *cl,
1129         __attribute__((unused)) void *data)
1130 {
1131         struct cmd_set_port_meter_dscp_table_result *res = parsed_result;
1132         struct rte_mtr_error error;
1133         enum rte_mtr_color *dscp_table = NULL;
1134         char *t_str = res->token_string;
1135         uint32_t mtr_id = 0;
1136         uint16_t port_id;
1137         int ret;
1138
1139         /* Parse string */
1140         ret = parse_multi_token_string(t_str, &port_id, &mtr_id, dscp_table);
1141         if (ret) {
1142                 printf(" Multi token string parse error\n");
1143                 return;
1144         }
1145
1146         if (port_id_is_invalid(port_id, ENABLED_WARN))
1147                 return;
1148
1149         /* Update Meter DSCP Table*/
1150         ret = rte_mtr_meter_dscp_table_update(port_id, mtr_id,
1151                 dscp_table, &error);
1152         if (ret != 0) {
1153                 print_err_msg(&error);
1154                 return;
1155         }
1156         free(dscp_table);
1157 }
1158
1159 cmdline_parse_inst_t cmd_set_port_meter_dscp_table = {
1160         .f = cmd_set_port_meter_dscp_table_parsed,
1161         .data = NULL,
1162         .help_str = "Update port meter dscp table",
1163         .tokens = {
1164                 (void *)&cmd_set_port_meter_dscp_table_set,
1165                 (void *)&cmd_set_port_meter_dscp_table_port,
1166                 (void *)&cmd_set_port_meter_dscp_table_meter,
1167                 (void *)&cmd_set_port_meter_dscp_table_dscp_table,
1168                 (void *)&cmd_set_port_meter_dscp_table_token_string,
1169                 NULL,
1170         },
1171 };
1172
1173 /* *** Set Port Meter Policer Action *** */
1174 struct cmd_set_port_meter_policer_action_result {
1175         cmdline_fixed_string_t set;
1176         cmdline_fixed_string_t port;
1177         cmdline_fixed_string_t meter;
1178         cmdline_fixed_string_t policer;
1179         cmdline_fixed_string_t action;
1180         uint16_t port_id;
1181         uint32_t mtr_id;
1182         uint32_t action_mask;
1183         cmdline_multi_string_t policer_action;
1184 };
1185
1186 cmdline_parse_token_string_t cmd_set_port_meter_policer_action_set =
1187         TOKEN_STRING_INITIALIZER(
1188                 struct cmd_set_port_meter_policer_action_result, set, "set");
1189 cmdline_parse_token_string_t cmd_set_port_meter_policer_action_port =
1190         TOKEN_STRING_INITIALIZER(
1191                 struct cmd_set_port_meter_policer_action_result, port, "port");
1192 cmdline_parse_token_string_t cmd_set_port_meter_policer_action_meter =
1193         TOKEN_STRING_INITIALIZER(
1194                 struct cmd_set_port_meter_policer_action_result, meter,
1195                 "meter");
1196 cmdline_parse_token_string_t cmd_set_port_meter_policer_action_policer =
1197         TOKEN_STRING_INITIALIZER(
1198                 struct cmd_set_port_meter_policer_action_result, policer,
1199                 "policer");
1200 cmdline_parse_token_string_t cmd_set_port_meter_policer_action_action =
1201         TOKEN_STRING_INITIALIZER(
1202                 struct cmd_set_port_meter_policer_action_result, action,
1203                 "action");
1204 cmdline_parse_token_num_t cmd_set_port_meter_policer_action_port_id =
1205         TOKEN_NUM_INITIALIZER(
1206                 struct cmd_set_port_meter_policer_action_result, port_id,
1207                 UINT16);
1208 cmdline_parse_token_num_t cmd_set_port_meter_policer_action_mtr_id =
1209         TOKEN_NUM_INITIALIZER(
1210                 struct cmd_set_port_meter_policer_action_result, mtr_id,
1211                 UINT32);
1212 cmdline_parse_token_num_t cmd_set_port_meter_policer_action_action_mask =
1213         TOKEN_NUM_INITIALIZER(
1214                 struct cmd_set_port_meter_policer_action_result, action_mask,
1215                 UINT32);
1216 cmdline_parse_token_string_t cmd_set_port_meter_policer_action_policer_action =
1217         TOKEN_STRING_INITIALIZER(
1218                 struct cmd_set_port_meter_policer_action_result,
1219                 policer_action, TOKEN_STRING_MULTI);
1220
1221 static void cmd_set_port_meter_policer_action_parsed(void *parsed_result,
1222         __attribute__((unused)) struct cmdline *cl,
1223         __attribute__((unused)) void *data)
1224 {
1225         struct cmd_set_port_meter_policer_action_result *res = parsed_result;
1226         enum rte_mtr_policer_action *actions;
1227         struct rte_mtr_error error;
1228         uint32_t mtr_id = res->mtr_id;
1229         uint32_t action_mask = res->action_mask;
1230         uint16_t port_id = res->port_id;
1231         char *p_str = res->policer_action;
1232         int ret;
1233
1234         if (port_id_is_invalid(port_id, ENABLED_WARN))
1235                 return;
1236
1237         /* Check: action mask */
1238         if (action_mask == 0 || (action_mask & (~0x7UL))) {
1239                 printf(" Policer action mask not correct (error)\n");
1240                 return;
1241         }
1242
1243         /* Allocate memory for policer actions */
1244         actions = (enum rte_mtr_policer_action *)malloc(RTE_MTR_COLORS *
1245                 sizeof(enum rte_mtr_policer_action));
1246         if (actions == NULL) {
1247                 printf("Memory for policer actions not allocated (error)\n");
1248                 return;
1249         }
1250         /* Parse policer action string */
1251         ret = parse_policer_action_string(p_str, action_mask, actions);
1252         if (ret) {
1253                 printf(" Policer action string parse error\n");
1254                 free(actions);
1255                 return;
1256         }
1257
1258         ret = rte_mtr_policer_actions_update(port_id, mtr_id,
1259                 action_mask, actions, &error);
1260         if (ret != 0) {
1261                 print_err_msg(&error);
1262                 return;
1263         }
1264
1265         free(actions);
1266 }
1267
1268 cmdline_parse_inst_t cmd_set_port_meter_policer_action = {
1269         .f = cmd_set_port_meter_policer_action_parsed,
1270         .data = NULL,
1271         .help_str = "Set port meter policer action",
1272         .tokens = {
1273                 (void *)&cmd_set_port_meter_policer_action_set,
1274                 (void *)&cmd_set_port_meter_policer_action_port,
1275                 (void *)&cmd_set_port_meter_policer_action_meter,
1276                 (void *)&cmd_set_port_meter_policer_action_policer,
1277                 (void *)&cmd_set_port_meter_policer_action_action,
1278                 (void *)&cmd_set_port_meter_policer_action_port_id,
1279                 (void *)&cmd_set_port_meter_policer_action_mtr_id,
1280                 (void *)&cmd_set_port_meter_policer_action_action_mask,
1281                 (void *)&cmd_set_port_meter_policer_action_policer_action,
1282                 NULL,
1283         },
1284 };
1285
1286 /* *** Set Port Meter Stats Mask *** */
1287 struct cmd_set_port_meter_stats_mask_result {
1288         cmdline_fixed_string_t set;
1289         cmdline_fixed_string_t port;
1290         cmdline_fixed_string_t meter;
1291         cmdline_fixed_string_t stats;
1292         cmdline_fixed_string_t mask;
1293         uint16_t port_id;
1294         uint32_t mtr_id;
1295         uint64_t stats_mask;
1296 };
1297
1298 cmdline_parse_token_string_t cmd_set_port_meter_stats_mask_set =
1299         TOKEN_STRING_INITIALIZER(
1300                 struct cmd_set_port_meter_stats_mask_result, set, "set");
1301 cmdline_parse_token_string_t cmd_set_port_meter_stats_mask_port =
1302         TOKEN_STRING_INITIALIZER(
1303                 struct cmd_set_port_meter_stats_mask_result, port, "port");
1304 cmdline_parse_token_string_t cmd_set_port_meter_stats_mask_meter =
1305         TOKEN_STRING_INITIALIZER(
1306                 struct cmd_set_port_meter_stats_mask_result, meter, "meter");
1307 cmdline_parse_token_string_t cmd_set_port_meter_stats_mask_stats =
1308         TOKEN_STRING_INITIALIZER(
1309                 struct cmd_set_port_meter_stats_mask_result, stats, "stats");
1310 cmdline_parse_token_string_t cmd_set_port_meter_stats_mask_mask =
1311         TOKEN_STRING_INITIALIZER(
1312                 struct cmd_set_port_meter_stats_mask_result, mask, "mask");
1313 cmdline_parse_token_num_t cmd_set_port_meter_stats_mask_port_id =
1314         TOKEN_NUM_INITIALIZER(
1315                 struct cmd_set_port_meter_stats_mask_result, port_id, UINT16);
1316 cmdline_parse_token_num_t cmd_set_port_meter_stats_mask_mtr_id =
1317         TOKEN_NUM_INITIALIZER(
1318                 struct cmd_set_port_meter_stats_mask_result, mtr_id, UINT32);
1319 cmdline_parse_token_num_t cmd_set_port_meter_stats_mask_stats_mask =
1320         TOKEN_NUM_INITIALIZER(
1321                 struct cmd_set_port_meter_stats_mask_result, stats_mask,
1322                 UINT64);
1323
1324 static void cmd_set_port_meter_stats_mask_parsed(void *parsed_result,
1325         __attribute__((unused)) struct cmdline *cl,
1326         __attribute__((unused)) void *data)
1327 {
1328         struct cmd_set_port_meter_stats_mask_result *res = parsed_result;
1329         struct rte_mtr_error error;
1330         uint64_t stats_mask = res->stats_mask;
1331         uint32_t mtr_id = res->mtr_id;
1332         uint16_t port_id = res->port_id;
1333         int ret;
1334
1335         if (port_id_is_invalid(port_id, ENABLED_WARN))
1336                 return;
1337
1338         ret = rte_mtr_stats_update(port_id, mtr_id, stats_mask, &error);
1339         if (ret != 0) {
1340                 print_err_msg(&error);
1341                 return;
1342         }
1343 }
1344
1345 cmdline_parse_inst_t cmd_set_port_meter_stats_mask = {
1346         .f = cmd_set_port_meter_stats_mask_parsed,
1347         .data = NULL,
1348         .help_str = "Set port meter stats mask",
1349         .tokens = {
1350                 (void *)&cmd_set_port_meter_stats_mask_set,
1351                 (void *)&cmd_set_port_meter_stats_mask_port,
1352                 (void *)&cmd_set_port_meter_stats_mask_meter,
1353                 (void *)&cmd_set_port_meter_stats_mask_stats,
1354                 (void *)&cmd_set_port_meter_stats_mask_mask,
1355                 (void *)&cmd_set_port_meter_stats_mask_port_id,
1356                 (void *)&cmd_set_port_meter_stats_mask_mtr_id,
1357                 (void *)&cmd_set_port_meter_stats_mask_stats_mask,
1358                 NULL,
1359         },
1360 };
1361
1362 /* *** Show Port Meter Stats *** */
1363 struct cmd_show_port_meter_stats_result {
1364         cmdline_fixed_string_t show;
1365         cmdline_fixed_string_t port;
1366         cmdline_fixed_string_t meter;
1367         cmdline_fixed_string_t stats;
1368         uint16_t port_id;
1369         uint32_t mtr_id;
1370         cmdline_fixed_string_t clear;
1371 };
1372
1373 cmdline_parse_token_string_t cmd_show_port_meter_stats_show =
1374         TOKEN_STRING_INITIALIZER(
1375                 struct cmd_show_port_meter_stats_result, show, "show");
1376 cmdline_parse_token_string_t cmd_show_port_meter_stats_port =
1377         TOKEN_STRING_INITIALIZER(
1378                 struct cmd_show_port_meter_stats_result, port, "port");
1379 cmdline_parse_token_string_t cmd_show_port_meter_stats_meter =
1380         TOKEN_STRING_INITIALIZER(
1381                 struct cmd_show_port_meter_stats_result, meter, "meter");
1382 cmdline_parse_token_string_t cmd_show_port_meter_stats_stats =
1383         TOKEN_STRING_INITIALIZER(
1384                 struct cmd_show_port_meter_stats_result, stats, "stats");
1385 cmdline_parse_token_num_t cmd_show_port_meter_stats_port_id =
1386         TOKEN_NUM_INITIALIZER(
1387                 struct cmd_show_port_meter_stats_result, port_id, UINT16);
1388 cmdline_parse_token_num_t cmd_show_port_meter_stats_mtr_id =
1389         TOKEN_NUM_INITIALIZER(
1390                 struct cmd_show_port_meter_stats_result, mtr_id, UINT32);
1391 cmdline_parse_token_string_t cmd_show_port_meter_stats_clear =
1392         TOKEN_STRING_INITIALIZER(
1393                 struct cmd_show_port_meter_stats_result, clear, "yes#no");
1394
1395 static void cmd_show_port_meter_stats_parsed(void *parsed_result,
1396         __attribute__((unused)) struct cmdline *cl,
1397         __attribute__((unused)) void *data)
1398 {
1399         struct cmd_show_port_meter_stats_result *res = parsed_result;
1400         struct rte_mtr_stats stats;
1401         uint64_t stats_mask = 0;
1402         struct rte_mtr_error error;
1403         uint32_t mtr_id = res->mtr_id;
1404         uint32_t clear = 0;
1405         uint16_t port_id = res->port_id;
1406         int ret;
1407
1408         if (port_id_is_invalid(port_id, ENABLED_WARN))
1409                 return;
1410
1411         if (strcmp(res->clear, "yes") == 0)
1412                 clear = 1;
1413
1414         memset(&stats, 0, sizeof(struct rte_mtr_stats));
1415         ret = rte_mtr_stats_read(port_id, mtr_id, &stats,
1416                 &stats_mask, clear, &error);
1417         if (ret != 0) {
1418                 print_err_msg(&error);
1419                 return;
1420         }
1421
1422         /* Display stats */
1423         if (stats_mask & RTE_MTR_STATS_N_PKTS_GREEN)
1424                 printf("\tPkts G: %" PRIu64 "\n",
1425                         stats.n_pkts[RTE_MTR_GREEN]);
1426         if (stats_mask & RTE_MTR_STATS_N_BYTES_GREEN)
1427                 printf("\tBytes G: %" PRIu64 "\n",
1428                         stats.n_bytes[RTE_MTR_GREEN]);
1429         if (stats_mask & RTE_MTR_STATS_N_PKTS_YELLOW)
1430                 printf("\tPkts Y: %" PRIu64 "\n",
1431                         stats.n_pkts[RTE_MTR_YELLOW]);
1432         if (stats_mask & RTE_MTR_STATS_N_BYTES_YELLOW)
1433                 printf("\tBytes Y: %" PRIu64 "\n",
1434                         stats.n_bytes[RTE_MTR_YELLOW]);
1435         if (stats_mask & RTE_MTR_STATS_N_PKTS_RED)
1436                 printf("\tPkts R: %" PRIu64 "\n",
1437                         stats.n_pkts[RTE_MTR_RED]);
1438         if (stats_mask & RTE_MTR_STATS_N_BYTES_RED)
1439                 printf("\tBytes Y: %" PRIu64 "\n",
1440                         stats.n_bytes[RTE_MTR_RED]);
1441         if (stats_mask & RTE_MTR_STATS_N_PKTS_DROPPED)
1442                 printf("\tPkts DROPPED: %" PRIu64 "\n",
1443                         stats.n_pkts_dropped);
1444         if (stats_mask & RTE_MTR_STATS_N_BYTES_DROPPED)
1445                 printf("\tBytes DROPPED: %" PRIu64 "\n",
1446                         stats.n_bytes_dropped);
1447 }
1448
1449 cmdline_parse_inst_t cmd_show_port_meter_stats = {
1450         .f = cmd_show_port_meter_stats_parsed,
1451         .data = NULL,
1452         .help_str = "Show port meter stats",
1453         .tokens = {
1454                 (void *)&cmd_show_port_meter_stats_show,
1455                 (void *)&cmd_show_port_meter_stats_port,
1456                 (void *)&cmd_show_port_meter_stats_meter,
1457                 (void *)&cmd_show_port_meter_stats_stats,
1458                 (void *)&cmd_show_port_meter_stats_port_id,
1459                 (void *)&cmd_show_port_meter_stats_mtr_id,
1460                 (void *)&cmd_show_port_meter_stats_clear,
1461                 NULL,
1462         },
1463 };