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