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