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