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