app/testpmd: mark most cmdline symbols as static
[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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static cmdline_parse_token_string_t cmd_create_port_meter_create =
728         TOKEN_STRING_INITIALIZER(
729                 struct cmd_create_port_meter_result, create, "create");
730 static cmdline_parse_token_string_t cmd_create_port_meter_port =
731         TOKEN_STRING_INITIALIZER(
732                 struct cmd_create_port_meter_result, port, "port");
733 static cmdline_parse_token_string_t cmd_create_port_meter_meter =
734         TOKEN_STRING_INITIALIZER(
735                 struct cmd_create_port_meter_result, meter, "meter");
736 static 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 static 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 static 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 static 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 static 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 static cmdline_parse_token_num_t cmd_create_port_meter_statistics_mask =
752         TOKEN_NUM_INITIALIZER(struct cmd_create_port_meter_result,
753                 statistics_mask, RTE_UINT64);
754 static cmdline_parse_token_num_t cmd_create_port_meter_shared =
755         TOKEN_NUM_INITIALIZER(struct cmd_create_port_meter_result,
756                 shared, RTE_UINT32);
757 static cmdline_parse_token_string_t cmd_create_port_meter_input_color =
758         TOKEN_STRING_INITIALIZER(struct cmd_create_port_meter_result,
759                 meter_input_color, TOKEN_STRING_MULTI);
760
761 static void cmd_create_port_meter_parsed(void *parsed_result,
762         __rte_unused struct cmdline *cl,
763         __rte_unused void *data)
764 {
765         struct cmd_create_port_meter_result *res = parsed_result;
766         struct rte_mtr_error error;
767         struct rte_mtr_params params;
768         uint32_t mtr_id = res->mtr_id;
769         uint32_t shared = res->shared;
770         uint32_t use_prev_meter_color = 0;
771         uint16_t port_id = res->port_id;
772         enum rte_color *dscp_table = NULL;
773         char *c_str = res->meter_input_color;
774         int ret;
775
776         if (port_id_is_invalid(port_id, ENABLED_WARN))
777                 return;
778
779         /* Meter params */
780         memset(&params, 0, sizeof(struct rte_mtr_params));
781         params.meter_profile_id = res->profile_id;
782         params.meter_policy_id = res->policy_id;
783         /* Parse meter input color string params */
784         ret = parse_meter_color_str(c_str, &use_prev_meter_color, &dscp_table);
785         if (ret) {
786                 fprintf(stderr,
787                         " Meter input color params string parse error\n");
788                 return;
789         }
790
791         params.use_prev_mtr_color = use_prev_meter_color;
792         params.dscp_table = dscp_table;
793
794         if (strcmp(res->meter_enable, "yes") == 0)
795                 params.meter_enable = 1;
796         else
797                 params.meter_enable = 0;
798         params.stats_mask = res->statistics_mask;
799
800         ret = rte_mtr_create(port_id, mtr_id, &params, shared, &error);
801         if (ret != 0) {
802                 free(dscp_table);
803                 print_err_msg(&error);
804                 return;
805         }
806 }
807
808 cmdline_parse_inst_t cmd_create_port_meter = {
809         .f = cmd_create_port_meter_parsed,
810         .data = NULL,
811         .help_str = "create port meter <port_id> <mtr_id> <profile_id> <policy_id> "
812                 "<meter_enable>(yes|no) <stats_mask> <shared> <use_pre_meter_color> "
813                 "[<dscp_tbl_entry0> <dscp_tbl_entry1> ...<dscp_tbl_entry63>]",
814         .tokens = {
815                 (void *)&cmd_create_port_meter_create,
816                 (void *)&cmd_create_port_meter_port,
817                 (void *)&cmd_create_port_meter_meter,
818                 (void *)&cmd_create_port_meter_port_id,
819                 (void *)&cmd_create_port_meter_mtr_id,
820                 (void *)&cmd_create_port_meter_profile_id,
821                 (void *)&cmd_create_port_meter_policy_id,
822                 (void *)&cmd_create_port_meter_meter_enable,
823                 (void *)&cmd_create_port_meter_statistics_mask,
824                 (void *)&cmd_create_port_meter_shared,
825                 (void *)&cmd_create_port_meter_input_color,
826                 NULL,
827         },
828 };
829
830 /* *** Enable Meter of MTR Object  *** */
831 struct cmd_enable_port_meter_result {
832         cmdline_fixed_string_t enable;
833         cmdline_fixed_string_t port;
834         cmdline_fixed_string_t meter;
835         uint16_t port_id;
836         uint32_t mtr_id;
837 };
838
839 static cmdline_parse_token_string_t cmd_enable_port_meter_enable =
840         TOKEN_STRING_INITIALIZER(
841                 struct cmd_enable_port_meter_result, enable, "enable");
842 static cmdline_parse_token_string_t cmd_enable_port_meter_port =
843         TOKEN_STRING_INITIALIZER(
844                 struct cmd_enable_port_meter_result, port, "port");
845 static cmdline_parse_token_string_t cmd_enable_port_meter_meter =
846         TOKEN_STRING_INITIALIZER(
847                 struct cmd_enable_port_meter_result, meter, "meter");
848 static cmdline_parse_token_num_t cmd_enable_port_meter_port_id =
849         TOKEN_NUM_INITIALIZER(
850                 struct cmd_enable_port_meter_result, port_id, RTE_UINT16);
851 static cmdline_parse_token_num_t cmd_enable_port_meter_mtr_id =
852         TOKEN_NUM_INITIALIZER(
853                 struct cmd_enable_port_meter_result, mtr_id, RTE_UINT32);
854
855 static void cmd_enable_port_meter_parsed(void *parsed_result,
856         __rte_unused struct cmdline *cl,
857         __rte_unused void *data)
858 {
859         struct cmd_enable_port_meter_result *res = parsed_result;
860         struct rte_mtr_error error;
861         uint32_t mtr_id = res->mtr_id;
862         uint16_t port_id = res->port_id;
863
864         int ret;
865
866         if (port_id_is_invalid(port_id, ENABLED_WARN))
867                 return;
868
869         /* Enable Meter */
870         ret = rte_mtr_meter_enable(port_id, mtr_id, &error);
871         if (ret != 0) {
872                 print_err_msg(&error);
873                 return;
874         }
875 }
876
877 cmdline_parse_inst_t cmd_enable_port_meter = {
878         .f = cmd_enable_port_meter_parsed,
879         .data = NULL,
880         .help_str = "enable port meter <port_id> <mtr_id>",
881         .tokens = {
882                 (void *)&cmd_enable_port_meter_enable,
883                 (void *)&cmd_enable_port_meter_port,
884                 (void *)&cmd_enable_port_meter_meter,
885                 (void *)&cmd_enable_port_meter_port_id,
886                 (void *)&cmd_enable_port_meter_mtr_id,
887                 NULL,
888         },
889 };
890
891 /* *** Disable Meter of MTR Object  *** */
892 struct cmd_disable_port_meter_result {
893         cmdline_fixed_string_t disable;
894         cmdline_fixed_string_t port;
895         cmdline_fixed_string_t meter;
896         uint16_t port_id;
897         uint32_t mtr_id;
898 };
899
900 static cmdline_parse_token_string_t cmd_disable_port_meter_disable =
901         TOKEN_STRING_INITIALIZER(
902                 struct cmd_disable_port_meter_result, disable, "disable");
903 static cmdline_parse_token_string_t cmd_disable_port_meter_port =
904         TOKEN_STRING_INITIALIZER(
905                 struct cmd_disable_port_meter_result, port, "port");
906 static cmdline_parse_token_string_t cmd_disable_port_meter_meter =
907         TOKEN_STRING_INITIALIZER(
908                 struct cmd_disable_port_meter_result, meter, "meter");
909 static cmdline_parse_token_num_t cmd_disable_port_meter_port_id =
910         TOKEN_NUM_INITIALIZER(
911                 struct cmd_disable_port_meter_result, port_id, RTE_UINT16);
912 static cmdline_parse_token_num_t cmd_disable_port_meter_mtr_id =
913         TOKEN_NUM_INITIALIZER(
914                 struct cmd_disable_port_meter_result, mtr_id, RTE_UINT32);
915
916 static void cmd_disable_port_meter_parsed(void *parsed_result,
917         __rte_unused struct cmdline *cl,
918         __rte_unused void *data)
919 {
920         struct cmd_disable_port_meter_result *res = parsed_result;
921         struct rte_mtr_error error;
922         uint32_t mtr_id = res->mtr_id;
923         uint16_t port_id = res->port_id;
924
925         int ret;
926
927         if (port_id_is_invalid(port_id, ENABLED_WARN))
928                 return;
929
930         /* Disable Meter */
931         ret = rte_mtr_meter_disable(port_id, mtr_id, &error);
932         if (ret != 0) {
933                 print_err_msg(&error);
934                 return;
935         }
936 }
937
938 cmdline_parse_inst_t cmd_disable_port_meter = {
939         .f = cmd_disable_port_meter_parsed,
940         .data = NULL,
941         .help_str = "disable port meter <port_id> <mtr_id>",
942         .tokens = {
943                 (void *)&cmd_disable_port_meter_disable,
944                 (void *)&cmd_disable_port_meter_port,
945                 (void *)&cmd_disable_port_meter_meter,
946                 (void *)&cmd_disable_port_meter_port_id,
947                 (void *)&cmd_disable_port_meter_mtr_id,
948                 NULL,
949         },
950 };
951
952 /* *** Delete Port Meter Policy Object *** */
953 struct cmd_del_port_meter_policy_result {
954         cmdline_fixed_string_t del;
955         cmdline_fixed_string_t port;
956         cmdline_fixed_string_t meter;
957         cmdline_fixed_string_t policy;
958         uint16_t port_id;
959         uint32_t policy_id;
960 };
961
962 static cmdline_parse_token_string_t cmd_del_port_meter_policy_del =
963         TOKEN_STRING_INITIALIZER(
964                 struct cmd_del_port_meter_policy_result, del, "del");
965 static cmdline_parse_token_string_t cmd_del_port_meter_policy_port =
966         TOKEN_STRING_INITIALIZER(
967                 struct cmd_del_port_meter_policy_result, port, "port");
968 static cmdline_parse_token_string_t cmd_del_port_meter_policy_meter =
969         TOKEN_STRING_INITIALIZER(
970                 struct cmd_del_port_meter_policy_result, meter, "meter");
971 static cmdline_parse_token_string_t cmd_del_port_meter_policy_policy =
972         TOKEN_STRING_INITIALIZER(
973                 struct cmd_del_port_meter_policy_result, policy, "policy");
974 static cmdline_parse_token_num_t cmd_del_port_meter_policy_port_id =
975         TOKEN_NUM_INITIALIZER(
976                 struct cmd_del_port_meter_policy_result, port_id, RTE_UINT16);
977 static cmdline_parse_token_num_t cmd_del_port_meter_policy_policy_id =
978         TOKEN_NUM_INITIALIZER(
979                 struct cmd_del_port_meter_policy_result, policy_id, RTE_UINT32);
980
981 static void cmd_del_port_meter_policy_parsed(void *parsed_result,
982         __rte_unused struct cmdline *cl,
983         __rte_unused void *data)
984 {
985         struct cmd_del_port_meter_policy_result *res = parsed_result;
986         struct rte_mtr_error error;
987         uint32_t policy_id = res->policy_id;
988         uint16_t port_id = res->port_id;
989         int ret;
990
991         if (port_id_is_invalid(port_id, ENABLED_WARN))
992                 return;
993
994         /* Delete Meter Policy*/
995         ret = rte_mtr_meter_policy_delete(port_id, policy_id, &error);
996         if (ret != 0) {
997                 print_err_msg(&error);
998                 return;
999         }
1000 }
1001
1002 cmdline_parse_inst_t cmd_del_port_meter_policy = {
1003         .f = cmd_del_port_meter_policy_parsed,
1004         .data = NULL,
1005         .help_str = "Delete port meter policy",
1006         .tokens = {
1007                 (void *)&cmd_del_port_meter_policy_del,
1008                 (void *)&cmd_del_port_meter_policy_port,
1009                 (void *)&cmd_del_port_meter_policy_meter,
1010                 (void *)&cmd_del_port_meter_policy_policy,
1011                 (void *)&cmd_del_port_meter_policy_port_id,
1012                 (void *)&cmd_del_port_meter_policy_policy_id,
1013                 NULL,
1014         },
1015 };
1016
1017 /* *** Delete Port Meter Object *** */
1018 struct cmd_del_port_meter_result {
1019         cmdline_fixed_string_t del;
1020         cmdline_fixed_string_t port;
1021         cmdline_fixed_string_t meter;
1022         uint16_t port_id;
1023         uint32_t mtr_id;
1024 };
1025
1026 static cmdline_parse_token_string_t cmd_del_port_meter_del =
1027         TOKEN_STRING_INITIALIZER(
1028                 struct cmd_del_port_meter_result, del, "del");
1029 static cmdline_parse_token_string_t cmd_del_port_meter_port =
1030         TOKEN_STRING_INITIALIZER(
1031                 struct cmd_del_port_meter_result, port, "port");
1032 static cmdline_parse_token_string_t cmd_del_port_meter_meter =
1033         TOKEN_STRING_INITIALIZER(
1034                 struct cmd_del_port_meter_result, meter, "meter");
1035 static cmdline_parse_token_num_t cmd_del_port_meter_port_id =
1036         TOKEN_NUM_INITIALIZER(
1037                 struct cmd_del_port_meter_result, port_id, RTE_UINT16);
1038 static cmdline_parse_token_num_t cmd_del_port_meter_mtr_id =
1039         TOKEN_NUM_INITIALIZER(
1040                 struct cmd_del_port_meter_result, mtr_id, RTE_UINT32);
1041
1042 static void cmd_del_port_meter_parsed(void *parsed_result,
1043         __rte_unused struct cmdline *cl,
1044         __rte_unused void *data)
1045 {
1046         struct cmd_del_port_meter_result *res = parsed_result;
1047         struct rte_mtr_error error;
1048         uint32_t mtr_id = res->mtr_id;
1049         uint16_t port_id = res->port_id;
1050
1051         int ret;
1052
1053         if (port_id_is_invalid(port_id, ENABLED_WARN))
1054                 return;
1055
1056         /* Destroy Meter */
1057         ret = rte_mtr_destroy(port_id, mtr_id, &error);
1058         if (ret != 0) {
1059                 print_err_msg(&error);
1060                 return;
1061         }
1062 }
1063
1064 cmdline_parse_inst_t cmd_del_port_meter = {
1065         .f = cmd_del_port_meter_parsed,
1066         .data = NULL,
1067         .help_str = "del port meter <port_id> <mtr_id>",
1068         .tokens = {
1069                 (void *)&cmd_del_port_meter_del,
1070                 (void *)&cmd_del_port_meter_port,
1071                 (void *)&cmd_del_port_meter_meter,
1072                 (void *)&cmd_del_port_meter_port_id,
1073                 (void *)&cmd_del_port_meter_mtr_id,
1074                 NULL,
1075         },
1076 };
1077
1078 /* *** Set Port Meter Profile *** */
1079 struct cmd_set_port_meter_profile_result {
1080         cmdline_fixed_string_t set;
1081         cmdline_fixed_string_t port;
1082         cmdline_fixed_string_t meter;
1083         cmdline_fixed_string_t profile;
1084         uint16_t port_id;
1085         uint32_t mtr_id;
1086         uint32_t profile_id;
1087 };
1088
1089 static cmdline_parse_token_string_t cmd_set_port_meter_profile_set =
1090         TOKEN_STRING_INITIALIZER(
1091                 struct cmd_set_port_meter_profile_result, set, "set");
1092 static cmdline_parse_token_string_t cmd_set_port_meter_profile_port =
1093         TOKEN_STRING_INITIALIZER(
1094                 struct cmd_set_port_meter_profile_result, port, "port");
1095 static cmdline_parse_token_string_t cmd_set_port_meter_profile_meter =
1096         TOKEN_STRING_INITIALIZER(
1097                 struct cmd_set_port_meter_profile_result, meter, "meter");
1098 static cmdline_parse_token_string_t cmd_set_port_meter_profile_profile =
1099         TOKEN_STRING_INITIALIZER(
1100                 struct cmd_set_port_meter_profile_result, profile, "profile");
1101 static cmdline_parse_token_num_t cmd_set_port_meter_profile_port_id =
1102         TOKEN_NUM_INITIALIZER(
1103                 struct cmd_set_port_meter_profile_result, port_id,
1104                 RTE_UINT16);
1105 static cmdline_parse_token_num_t cmd_set_port_meter_profile_mtr_id =
1106         TOKEN_NUM_INITIALIZER(
1107                 struct cmd_set_port_meter_profile_result, mtr_id,
1108                 RTE_UINT32);
1109 static cmdline_parse_token_num_t cmd_set_port_meter_profile_profile_id =
1110         TOKEN_NUM_INITIALIZER(
1111                 struct cmd_set_port_meter_profile_result, profile_id,
1112                 RTE_UINT32);
1113
1114 static void cmd_set_port_meter_profile_parsed(void *parsed_result,
1115         __rte_unused struct cmdline *cl,
1116         __rte_unused void *data)
1117 {
1118         struct cmd_set_port_meter_profile_result *res = parsed_result;
1119         struct rte_mtr_error error;
1120         uint32_t mtr_id = res->mtr_id;
1121         uint32_t profile_id = res->profile_id;
1122         uint16_t port_id = res->port_id;
1123
1124         int ret;
1125
1126         if (port_id_is_invalid(port_id, ENABLED_WARN))
1127                 return;
1128
1129         /* Set meter profile */
1130         ret = rte_mtr_meter_profile_update(port_id, mtr_id,
1131                 profile_id, &error);
1132         if (ret != 0) {
1133                 print_err_msg(&error);
1134                 return;
1135         }
1136 }
1137
1138 cmdline_parse_inst_t cmd_set_port_meter_profile = {
1139         .f = cmd_set_port_meter_profile_parsed,
1140         .data = NULL,
1141         .help_str = "set port meter profile <port_id> <mtr_id> <profile_id>",
1142         .tokens = {
1143                 (void *)&cmd_set_port_meter_profile_set,
1144                 (void *)&cmd_set_port_meter_profile_port,
1145                 (void *)&cmd_set_port_meter_profile_meter,
1146                 (void *)&cmd_set_port_meter_profile_profile,
1147                 (void *)&cmd_set_port_meter_profile_port_id,
1148                 (void *)&cmd_set_port_meter_profile_mtr_id,
1149                 (void *)&cmd_set_port_meter_profile_profile_id,
1150                 NULL,
1151         },
1152 };
1153
1154 /* *** Set Port Meter DSCP Table *** */
1155 struct cmd_set_port_meter_dscp_table_result {
1156         cmdline_fixed_string_t set;
1157         cmdline_fixed_string_t port;
1158         cmdline_fixed_string_t meter;
1159         cmdline_fixed_string_t dscp_table;
1160         cmdline_multi_string_t token_string;
1161 };
1162
1163 static cmdline_parse_token_string_t cmd_set_port_meter_dscp_table_set =
1164         TOKEN_STRING_INITIALIZER(
1165                 struct cmd_set_port_meter_dscp_table_result, set, "set");
1166 static cmdline_parse_token_string_t cmd_set_port_meter_dscp_table_port =
1167         TOKEN_STRING_INITIALIZER(
1168                 struct cmd_set_port_meter_dscp_table_result, port, "port");
1169 static cmdline_parse_token_string_t cmd_set_port_meter_dscp_table_meter =
1170         TOKEN_STRING_INITIALIZER(
1171                 struct cmd_set_port_meter_dscp_table_result, meter, "meter");
1172 static cmdline_parse_token_string_t cmd_set_port_meter_dscp_table_dscp_table =
1173         TOKEN_STRING_INITIALIZER(
1174                 struct cmd_set_port_meter_dscp_table_result,
1175                 dscp_table, "dscp table");
1176 static cmdline_parse_token_string_t cmd_set_port_meter_dscp_table_token_string =
1177         TOKEN_STRING_INITIALIZER(struct cmd_set_port_meter_dscp_table_result,
1178                 token_string, TOKEN_STRING_MULTI);
1179
1180 static void cmd_set_port_meter_dscp_table_parsed(void *parsed_result,
1181         __rte_unused struct cmdline *cl,
1182         __rte_unused void *data)
1183 {
1184         struct cmd_set_port_meter_dscp_table_result *res = parsed_result;
1185         struct rte_mtr_error error;
1186         enum rte_color *dscp_table = NULL;
1187         char *t_str = res->token_string;
1188         uint32_t mtr_id = 0;
1189         uint16_t port_id;
1190         int ret;
1191
1192         /* Parse string */
1193         ret = parse_multi_token_string(t_str, &port_id, &mtr_id, &dscp_table);
1194         if (ret) {
1195                 fprintf(stderr, " Multi token string parse error\n");
1196                 return;
1197         }
1198
1199         if (port_id_is_invalid(port_id, ENABLED_WARN))
1200                 goto free_table;
1201
1202         /* Update Meter DSCP Table*/
1203         ret = rte_mtr_meter_dscp_table_update(port_id, mtr_id,
1204                 dscp_table, &error);
1205         if (ret != 0)
1206                 print_err_msg(&error);
1207
1208 free_table:
1209         free(dscp_table);
1210 }
1211
1212 cmdline_parse_inst_t cmd_set_port_meter_dscp_table = {
1213         .f = cmd_set_port_meter_dscp_table_parsed,
1214         .data = NULL,
1215         .help_str = "set port meter dscp table <port_id> <mtr_id> "
1216                 "[<dscp_tbl_entry0> <dscp_tbl_entry1> ... <dscp_tbl_entry63>]",
1217         .tokens = {
1218                 (void *)&cmd_set_port_meter_dscp_table_set,
1219                 (void *)&cmd_set_port_meter_dscp_table_port,
1220                 (void *)&cmd_set_port_meter_dscp_table_meter,
1221                 (void *)&cmd_set_port_meter_dscp_table_dscp_table,
1222                 (void *)&cmd_set_port_meter_dscp_table_token_string,
1223                 NULL,
1224         },
1225 };
1226
1227 /* *** Set Port Meter Stats Mask *** */
1228 struct cmd_set_port_meter_stats_mask_result {
1229         cmdline_fixed_string_t set;
1230         cmdline_fixed_string_t port;
1231         cmdline_fixed_string_t meter;
1232         cmdline_fixed_string_t stats;
1233         cmdline_fixed_string_t mask;
1234         uint16_t port_id;
1235         uint32_t mtr_id;
1236         uint64_t stats_mask;
1237 };
1238
1239 static cmdline_parse_token_string_t cmd_set_port_meter_stats_mask_set =
1240         TOKEN_STRING_INITIALIZER(
1241                 struct cmd_set_port_meter_stats_mask_result, set, "set");
1242 static cmdline_parse_token_string_t cmd_set_port_meter_stats_mask_port =
1243         TOKEN_STRING_INITIALIZER(
1244                 struct cmd_set_port_meter_stats_mask_result, port, "port");
1245 static cmdline_parse_token_string_t cmd_set_port_meter_stats_mask_meter =
1246         TOKEN_STRING_INITIALIZER(
1247                 struct cmd_set_port_meter_stats_mask_result, meter, "meter");
1248 static cmdline_parse_token_string_t cmd_set_port_meter_stats_mask_stats =
1249         TOKEN_STRING_INITIALIZER(
1250                 struct cmd_set_port_meter_stats_mask_result, stats, "stats");
1251 static cmdline_parse_token_string_t cmd_set_port_meter_stats_mask_mask =
1252         TOKEN_STRING_INITIALIZER(
1253                 struct cmd_set_port_meter_stats_mask_result, mask, "mask");
1254 static cmdline_parse_token_num_t cmd_set_port_meter_stats_mask_port_id =
1255         TOKEN_NUM_INITIALIZER(
1256                 struct cmd_set_port_meter_stats_mask_result, port_id,
1257                 RTE_UINT16);
1258 static cmdline_parse_token_num_t cmd_set_port_meter_stats_mask_mtr_id =
1259         TOKEN_NUM_INITIALIZER(
1260                 struct cmd_set_port_meter_stats_mask_result, mtr_id,
1261                 RTE_UINT32);
1262 static cmdline_parse_token_num_t cmd_set_port_meter_stats_mask_stats_mask =
1263         TOKEN_NUM_INITIALIZER(
1264                 struct cmd_set_port_meter_stats_mask_result, stats_mask,
1265                 RTE_UINT64);
1266
1267 static void cmd_set_port_meter_stats_mask_parsed(void *parsed_result,
1268         __rte_unused struct cmdline *cl,
1269         __rte_unused void *data)
1270 {
1271         struct cmd_set_port_meter_stats_mask_result *res = parsed_result;
1272         struct rte_mtr_error error;
1273         uint64_t stats_mask = res->stats_mask;
1274         uint32_t mtr_id = res->mtr_id;
1275         uint16_t port_id = res->port_id;
1276         int ret;
1277
1278         if (port_id_is_invalid(port_id, ENABLED_WARN))
1279                 return;
1280
1281         ret = rte_mtr_stats_update(port_id, mtr_id, stats_mask, &error);
1282         if (ret != 0) {
1283                 print_err_msg(&error);
1284                 return;
1285         }
1286 }
1287
1288 cmdline_parse_inst_t cmd_set_port_meter_stats_mask = {
1289         .f = cmd_set_port_meter_stats_mask_parsed,
1290         .data = NULL,
1291         .help_str = "set port meter stats mask <port_id> <mtr_id> <stats_mask>",
1292         .tokens = {
1293                 (void *)&cmd_set_port_meter_stats_mask_set,
1294                 (void *)&cmd_set_port_meter_stats_mask_port,
1295                 (void *)&cmd_set_port_meter_stats_mask_meter,
1296                 (void *)&cmd_set_port_meter_stats_mask_stats,
1297                 (void *)&cmd_set_port_meter_stats_mask_mask,
1298                 (void *)&cmd_set_port_meter_stats_mask_port_id,
1299                 (void *)&cmd_set_port_meter_stats_mask_mtr_id,
1300                 (void *)&cmd_set_port_meter_stats_mask_stats_mask,
1301                 NULL,
1302         },
1303 };
1304
1305 /* *** Show Port Meter Stats *** */
1306 struct cmd_show_port_meter_stats_result {
1307         cmdline_fixed_string_t show;
1308         cmdline_fixed_string_t port;
1309         cmdline_fixed_string_t meter;
1310         cmdline_fixed_string_t stats;
1311         uint16_t port_id;
1312         uint32_t mtr_id;
1313         cmdline_fixed_string_t clear;
1314 };
1315
1316 static cmdline_parse_token_string_t cmd_show_port_meter_stats_show =
1317         TOKEN_STRING_INITIALIZER(
1318                 struct cmd_show_port_meter_stats_result, show, "show");
1319 static cmdline_parse_token_string_t cmd_show_port_meter_stats_port =
1320         TOKEN_STRING_INITIALIZER(
1321                 struct cmd_show_port_meter_stats_result, port, "port");
1322 static cmdline_parse_token_string_t cmd_show_port_meter_stats_meter =
1323         TOKEN_STRING_INITIALIZER(
1324                 struct cmd_show_port_meter_stats_result, meter, "meter");
1325 static cmdline_parse_token_string_t cmd_show_port_meter_stats_stats =
1326         TOKEN_STRING_INITIALIZER(
1327                 struct cmd_show_port_meter_stats_result, stats, "stats");
1328 static cmdline_parse_token_num_t cmd_show_port_meter_stats_port_id =
1329         TOKEN_NUM_INITIALIZER(
1330                 struct cmd_show_port_meter_stats_result, port_id, RTE_UINT16);
1331 static cmdline_parse_token_num_t cmd_show_port_meter_stats_mtr_id =
1332         TOKEN_NUM_INITIALIZER(
1333                 struct cmd_show_port_meter_stats_result, mtr_id, RTE_UINT32);
1334 static cmdline_parse_token_string_t cmd_show_port_meter_stats_clear =
1335         TOKEN_STRING_INITIALIZER(
1336                 struct cmd_show_port_meter_stats_result, clear, "yes#no");
1337
1338 static void cmd_show_port_meter_stats_parsed(void *parsed_result,
1339         __rte_unused struct cmdline *cl,
1340         __rte_unused void *data)
1341 {
1342         struct cmd_show_port_meter_stats_result *res = parsed_result;
1343         struct rte_mtr_stats stats;
1344         uint64_t stats_mask = 0;
1345         struct rte_mtr_error error;
1346         uint32_t mtr_id = res->mtr_id;
1347         uint32_t clear = 0;
1348         uint16_t port_id = res->port_id;
1349         int ret;
1350
1351         if (port_id_is_invalid(port_id, ENABLED_WARN))
1352                 return;
1353
1354         if (strcmp(res->clear, "yes") == 0)
1355                 clear = 1;
1356
1357         memset(&stats, 0, sizeof(struct rte_mtr_stats));
1358         ret = rte_mtr_stats_read(port_id, mtr_id, &stats,
1359                 &stats_mask, clear, &error);
1360         if (ret != 0) {
1361                 print_err_msg(&error);
1362                 return;
1363         }
1364
1365         /* Display stats */
1366         if (stats_mask & RTE_MTR_STATS_N_PKTS_GREEN)
1367                 printf("\tPkts G: %" PRIu64 "\n",
1368                         stats.n_pkts[RTE_COLOR_GREEN]);
1369         if (stats_mask & RTE_MTR_STATS_N_BYTES_GREEN)
1370                 printf("\tBytes G: %" PRIu64 "\n",
1371                         stats.n_bytes[RTE_COLOR_GREEN]);
1372         if (stats_mask & RTE_MTR_STATS_N_PKTS_YELLOW)
1373                 printf("\tPkts Y: %" PRIu64 "\n",
1374                         stats.n_pkts[RTE_COLOR_YELLOW]);
1375         if (stats_mask & RTE_MTR_STATS_N_BYTES_YELLOW)
1376                 printf("\tBytes Y: %" PRIu64 "\n",
1377                         stats.n_bytes[RTE_COLOR_YELLOW]);
1378         if (stats_mask & RTE_MTR_STATS_N_PKTS_RED)
1379                 printf("\tPkts R: %" PRIu64 "\n",
1380                         stats.n_pkts[RTE_COLOR_RED]);
1381         if (stats_mask & RTE_MTR_STATS_N_BYTES_RED)
1382                 printf("\tBytes R: %" PRIu64 "\n",
1383                         stats.n_bytes[RTE_COLOR_RED]);
1384         if (stats_mask & RTE_MTR_STATS_N_PKTS_DROPPED)
1385                 printf("\tPkts DROPPED: %" PRIu64 "\n",
1386                         stats.n_pkts_dropped);
1387         if (stats_mask & RTE_MTR_STATS_N_BYTES_DROPPED)
1388                 printf("\tBytes DROPPED: %" PRIu64 "\n",
1389                         stats.n_bytes_dropped);
1390 }
1391
1392 cmdline_parse_inst_t cmd_show_port_meter_stats = {
1393         .f = cmd_show_port_meter_stats_parsed,
1394         .data = NULL,
1395         .help_str = "show port meter stats <port_id> <mtr_id> <clear>(yes|no)",
1396         .tokens = {
1397                 (void *)&cmd_show_port_meter_stats_show,
1398                 (void *)&cmd_show_port_meter_stats_port,
1399                 (void *)&cmd_show_port_meter_stats_meter,
1400                 (void *)&cmd_show_port_meter_stats_stats,
1401                 (void *)&cmd_show_port_meter_stats_port_id,
1402                 (void *)&cmd_show_port_meter_stats_mtr_id,
1403                 (void *)&cmd_show_port_meter_stats_clear,
1404                 NULL,
1405         },
1406 };