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