5eb91f13e1f3d3747d832b40f98423003df4b27d
[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 /** Display Meter Error Message */
17 static void
18 print_err_msg(struct rte_mtr_error *error)
19 {
20         static const char *const errstrlist[] = {
21                 [RTE_MTR_ERROR_TYPE_NONE] = "no error",
22                 [RTE_MTR_ERROR_TYPE_UNSPECIFIED] = "cause unspecified",
23                 [RTE_MTR_ERROR_TYPE_METER_PROFILE_ID] = "meter profile id",
24                 [RTE_MTR_ERROR_TYPE_METER_PROFILE] = "meter profile null",
25                 [RTE_MTR_ERROR_TYPE_MTR_ID] = "meter id",
26                 [RTE_MTR_ERROR_TYPE_MTR_PARAMS] = "meter params null",
27                 [RTE_MTR_ERROR_TYPE_POLICER_ACTION_GREEN]
28                         = "policer action(green)",
29                 [RTE_MTR_ERROR_TYPE_POLICER_ACTION_YELLOW]
30                         = "policer action(yellow)",
31                 [RTE_MTR_ERROR_TYPE_POLICER_ACTION_RED]
32                         = "policer action(red)",
33                 [RTE_MTR_ERROR_TYPE_STATS_MASK] = "stats mask",
34                 [RTE_MTR_ERROR_TYPE_STATS] = "stats",
35                 [RTE_MTR_ERROR_TYPE_SHARED]
36                         = "shared meter",
37         };
38
39         const char *errstr;
40         char buf[64];
41
42         if ((unsigned int)error->type >= RTE_DIM(errstrlist) ||
43                 !errstrlist[error->type])
44                 errstr = "unknown type";
45         else
46                 errstr = errstrlist[error->type];
47
48         if (error->cause)
49                 snprintf(buf, sizeof(buf), "cause: %p, ", error->cause);
50
51         printf("%s: %s%s (error %d)\n", errstr, error->cause ? buf : "",
52                 error->message ? error->message : "(no stated reason)",
53                 error->type);
54 }
55
56 static int
57 string_to_policer_action(char *s)
58 {
59         if (strcmp(s, "G") == 0)
60                 return MTR_POLICER_ACTION_COLOR_GREEN;
61
62         if (strcmp(s, "Y") == 0)
63                 return MTR_POLICER_ACTION_COLOR_YELLOW;
64
65         if (strcmp(s, "R") == 0)
66                 return MTR_POLICER_ACTION_COLOR_RED;
67
68         if (strcmp(s, "D") == 0)
69                 return MTR_POLICER_ACTION_DROP;
70
71         return -1;
72 }
73
74 /* *** Add Port Meter Profile srtcm_rfc2697 *** */
75 struct cmd_add_port_meter_profile_srtcm_result {
76         cmdline_fixed_string_t add;
77         cmdline_fixed_string_t port;
78         cmdline_fixed_string_t meter;
79         cmdline_fixed_string_t profile;
80         cmdline_fixed_string_t srtcm_rfc2697;
81         uint16_t port_id;
82         uint32_t profile_id;
83         uint64_t cir;
84         uint64_t cbs;
85         uint64_t ebs;
86         uint8_t color_aware;
87 };
88
89 cmdline_parse_token_string_t cmd_add_port_meter_profile_srtcm_add =
90         TOKEN_STRING_INITIALIZER(
91                 struct cmd_add_port_meter_profile_srtcm_result, add, "add");
92 cmdline_parse_token_string_t cmd_add_port_meter_profile_srtcm_port =
93         TOKEN_STRING_INITIALIZER(
94                 struct cmd_add_port_meter_profile_srtcm_result,
95                         port, "port");
96 cmdline_parse_token_string_t cmd_add_port_meter_profile_srtcm_meter =
97         TOKEN_STRING_INITIALIZER(
98                 struct cmd_add_port_meter_profile_srtcm_result,
99                         meter, "meter");
100 cmdline_parse_token_string_t cmd_add_port_meter_profile_srtcm_profile =
101         TOKEN_STRING_INITIALIZER(
102                 struct cmd_add_port_meter_profile_srtcm_result,
103                         profile, "profile");
104 cmdline_parse_token_string_t cmd_add_port_meter_profile_srtcm_srtcm_rfc2697 =
105         TOKEN_STRING_INITIALIZER(
106                 struct cmd_add_port_meter_profile_srtcm_result,
107                         srtcm_rfc2697, "srtcm_rfc2697");
108 cmdline_parse_token_num_t cmd_add_port_meter_profile_srtcm_port_id =
109         TOKEN_NUM_INITIALIZER(
110                 struct cmd_add_port_meter_profile_srtcm_result,
111                         port_id, UINT16);
112 cmdline_parse_token_num_t cmd_add_port_meter_profile_srtcm_profile_id =
113         TOKEN_NUM_INITIALIZER(
114                 struct cmd_add_port_meter_profile_srtcm_result,
115                         profile_id, UINT32);
116 cmdline_parse_token_num_t cmd_add_port_meter_profile_srtcm_cir =
117         TOKEN_NUM_INITIALIZER(
118                 struct cmd_add_port_meter_profile_srtcm_result,
119                         cir, UINT64);
120 cmdline_parse_token_num_t cmd_add_port_meter_profile_srtcm_cbs =
121         TOKEN_NUM_INITIALIZER(
122                 struct cmd_add_port_meter_profile_srtcm_result,
123                         cbs, UINT64);
124 cmdline_parse_token_num_t cmd_add_port_meter_profile_srtcm_ebs =
125         TOKEN_NUM_INITIALIZER(
126                 struct cmd_add_port_meter_profile_srtcm_result,
127                         ebs, UINT64);
128
129 static void cmd_add_port_meter_profile_srtcm_parsed(void *parsed_result,
130         __attribute__((unused)) struct cmdline *cl,
131         __attribute__((unused)) void *data)
132 {
133         struct cmd_add_port_meter_profile_srtcm_result *res = parsed_result;
134         struct rte_mtr_meter_profile mp;
135         struct rte_mtr_error error;
136         uint32_t profile_id = res->profile_id;
137         uint16_t port_id = res->port_id;
138         int ret;
139
140         if (port_id_is_invalid(port_id, ENABLED_WARN))
141                 return;
142
143         /* Private shaper profile params */
144         memset(&mp, 0, sizeof(struct rte_mtr_meter_profile));
145         mp.alg = 0;
146         mp.srtcm_rfc2697.cir = res->cir;
147         mp.srtcm_rfc2697.cbs = res->cbs;
148         mp.srtcm_rfc2697.ebs = res->ebs;
149
150         ret = rte_mtr_meter_profile_add(port_id, profile_id, &mp, &error);
151         if (ret != 0) {
152                 print_err_msg(&error);
153                 return;
154         }
155 }
156
157 cmdline_parse_inst_t cmd_add_port_meter_profile_srtcm = {
158         .f = cmd_add_port_meter_profile_srtcm_parsed,
159         .data = NULL,
160         .help_str = "Add port meter profile srtcm (rfc2697)",
161         .tokens = {
162                 (void *)&cmd_add_port_meter_profile_srtcm_add,
163                 (void *)&cmd_add_port_meter_profile_srtcm_port,
164                 (void *)&cmd_add_port_meter_profile_srtcm_meter,
165                 (void *)&cmd_add_port_meter_profile_srtcm_profile,
166                 (void *)&cmd_add_port_meter_profile_srtcm_port_id,
167                 (void *)&cmd_add_port_meter_profile_srtcm_profile_id,
168                 (void *)&cmd_add_port_meter_profile_srtcm_srtcm_rfc2697,
169                 (void *)&cmd_add_port_meter_profile_srtcm_cir,
170                 (void *)&cmd_add_port_meter_profile_srtcm_cbs,
171                 (void *)&cmd_add_port_meter_profile_srtcm_ebs,
172                 NULL,
173         },
174 };
175
176 /* *** Add Port Meter Profile trtcm_rfc2698 *** */
177 struct cmd_add_port_meter_profile_trtcm_result {
178         cmdline_fixed_string_t add;
179         cmdline_fixed_string_t port;
180         cmdline_fixed_string_t meter;
181         cmdline_fixed_string_t profile;
182         cmdline_fixed_string_t trtcm_rfc2698;
183         uint16_t port_id;
184         uint32_t profile_id;
185         uint64_t cir;
186         uint64_t pir;
187         uint64_t cbs;
188         uint64_t pbs;
189 };
190
191 cmdline_parse_token_string_t cmd_add_port_meter_profile_trtcm_add =
192         TOKEN_STRING_INITIALIZER(
193                 struct cmd_add_port_meter_profile_trtcm_result, add, "add");
194 cmdline_parse_token_string_t cmd_add_port_meter_profile_trtcm_port =
195         TOKEN_STRING_INITIALIZER(
196                 struct cmd_add_port_meter_profile_trtcm_result,
197                         port, "port");
198 cmdline_parse_token_string_t cmd_add_port_meter_profile_trtcm_meter =
199         TOKEN_STRING_INITIALIZER(
200                 struct cmd_add_port_meter_profile_trtcm_result,
201                         meter, "meter");
202 cmdline_parse_token_string_t cmd_add_port_meter_profile_trtcm_profile =
203         TOKEN_STRING_INITIALIZER(
204                 struct cmd_add_port_meter_profile_trtcm_result,
205                         profile, "profile");
206 cmdline_parse_token_string_t cmd_add_port_meter_profile_trtcm_trtcm_rfc2698 =
207         TOKEN_STRING_INITIALIZER(
208                 struct cmd_add_port_meter_profile_trtcm_result,
209                         trtcm_rfc2698, "trtcm_rfc2698");
210 cmdline_parse_token_num_t cmd_add_port_meter_profile_trtcm_port_id =
211         TOKEN_NUM_INITIALIZER(
212                 struct cmd_add_port_meter_profile_trtcm_result,
213                         port_id, UINT16);
214 cmdline_parse_token_num_t cmd_add_port_meter_profile_trtcm_profile_id =
215         TOKEN_NUM_INITIALIZER(
216                 struct cmd_add_port_meter_profile_trtcm_result,
217                         profile_id, UINT32);
218 cmdline_parse_token_num_t cmd_add_port_meter_profile_trtcm_cir =
219         TOKEN_NUM_INITIALIZER(
220                 struct cmd_add_port_meter_profile_trtcm_result,
221                         cir, UINT64);
222 cmdline_parse_token_num_t cmd_add_port_meter_profile_trtcm_pir =
223         TOKEN_NUM_INITIALIZER(
224                 struct cmd_add_port_meter_profile_trtcm_result,
225                         pir, UINT64);
226 cmdline_parse_token_num_t cmd_add_port_meter_profile_trtcm_cbs =
227         TOKEN_NUM_INITIALIZER(
228                 struct cmd_add_port_meter_profile_trtcm_result,
229                         cbs, UINT64);
230 cmdline_parse_token_num_t cmd_add_port_meter_profile_trtcm_pbs =
231         TOKEN_NUM_INITIALIZER(
232                 struct cmd_add_port_meter_profile_trtcm_result,
233                         pbs, UINT64);
234
235 static void cmd_add_port_meter_profile_trtcm_parsed(void *parsed_result,
236         __attribute__((unused)) struct cmdline *cl,
237         __attribute__((unused)) void *data)
238 {
239         struct cmd_add_port_meter_profile_trtcm_result *res = parsed_result;
240         struct rte_mtr_meter_profile mp;
241         struct rte_mtr_error error;
242         uint32_t profile_id = res->profile_id;
243         uint16_t port_id = res->port_id;
244         int ret;
245
246         if (port_id_is_invalid(port_id, ENABLED_WARN))
247                 return;
248
249         /* Private shaper profile params */
250         memset(&mp, 0, sizeof(struct rte_mtr_meter_profile));
251         mp.alg = 0;
252         mp.trtcm_rfc2698.cir = res->cir;
253         mp.trtcm_rfc2698.pir = res->pir;
254         mp.trtcm_rfc2698.cbs = res->cbs;
255         mp.trtcm_rfc2698.pbs = res->pbs;
256
257         ret = rte_mtr_meter_profile_add(port_id, profile_id, &mp, &error);
258         if (ret != 0) {
259                 print_err_msg(&error);
260                 return;
261         }
262 }
263
264 cmdline_parse_inst_t cmd_add_port_meter_profile_trtcm = {
265         .f = cmd_add_port_meter_profile_trtcm_parsed,
266         .data = NULL,
267         .help_str = "Add port meter profile trtcm (rfc2698)",
268         .tokens = {
269                 (void *)&cmd_add_port_meter_profile_trtcm_add,
270                 (void *)&cmd_add_port_meter_profile_trtcm_port,
271                 (void *)&cmd_add_port_meter_profile_trtcm_meter,
272                 (void *)&cmd_add_port_meter_profile_trtcm_profile,
273                 (void *)&cmd_add_port_meter_profile_trtcm_port_id,
274                 (void *)&cmd_add_port_meter_profile_trtcm_profile_id,
275                 (void *)&cmd_add_port_meter_profile_trtcm_trtcm_rfc2698,
276                 (void *)&cmd_add_port_meter_profile_trtcm_cir,
277                 (void *)&cmd_add_port_meter_profile_trtcm_pir,
278                 (void *)&cmd_add_port_meter_profile_trtcm_cbs,
279                 (void *)&cmd_add_port_meter_profile_trtcm_pbs,
280                 NULL,
281         },
282 };
283
284 /* *** Add Port Meter Profile trtcm_rfc4115 *** */
285 struct cmd_add_port_meter_profile_trtcm_rfc4115_result {
286         cmdline_fixed_string_t add;
287         cmdline_fixed_string_t port;
288         cmdline_fixed_string_t meter;
289         cmdline_fixed_string_t profile;
290         cmdline_fixed_string_t trtcm_rfc4115;
291         uint16_t port_id;
292         uint32_t profile_id;
293         uint64_t cir;
294         uint64_t eir;
295         uint64_t cbs;
296         uint64_t ebs;
297 };
298
299 cmdline_parse_token_string_t cmd_add_port_meter_profile_trtcm_rfc4115_add =
300         TOKEN_STRING_INITIALIZER(
301                 struct cmd_add_port_meter_profile_trtcm_rfc4115_result, add,
302                 "add");
303 cmdline_parse_token_string_t cmd_add_port_meter_profile_trtcm_rfc4115_port =
304         TOKEN_STRING_INITIALIZER(
305                 struct cmd_add_port_meter_profile_trtcm_rfc4115_result,
306                         port, "port");
307 cmdline_parse_token_string_t cmd_add_port_meter_profile_trtcm_rfc4115_meter =
308         TOKEN_STRING_INITIALIZER(
309                 struct cmd_add_port_meter_profile_trtcm_rfc4115_result,
310                         meter, "meter");
311 cmdline_parse_token_string_t cmd_add_port_meter_profile_trtcm_rfc4115_profile =
312         TOKEN_STRING_INITIALIZER(
313                 struct cmd_add_port_meter_profile_trtcm_rfc4115_result,
314                         profile, "profile");
315 cmdline_parse_token_string_t
316         cmd_add_port_meter_profile_trtcm_rfc4115_trtcm_rfc4115 =
317         TOKEN_STRING_INITIALIZER(
318                 struct cmd_add_port_meter_profile_trtcm_rfc4115_result,
319                         trtcm_rfc4115, "trtcm_rfc4115");
320 cmdline_parse_token_num_t cmd_add_port_meter_profile_trtcm_rfc4115_port_id =
321         TOKEN_NUM_INITIALIZER(
322                 struct cmd_add_port_meter_profile_trtcm_rfc4115_result,
323                         port_id, UINT16);
324 cmdline_parse_token_num_t cmd_add_port_meter_profile_trtcm_rfc4115_profile_id =
325         TOKEN_NUM_INITIALIZER(
326                 struct cmd_add_port_meter_profile_trtcm_rfc4115_result,
327                         profile_id, UINT32);
328 cmdline_parse_token_num_t cmd_add_port_meter_profile_trtcm_rfc4115_cir =
329         TOKEN_NUM_INITIALIZER(
330                 struct cmd_add_port_meter_profile_trtcm_rfc4115_result,
331                         cir, UINT64);
332 cmdline_parse_token_num_t cmd_add_port_meter_profile_trtcm_rfc4115_eir =
333         TOKEN_NUM_INITIALIZER(
334                 struct cmd_add_port_meter_profile_trtcm_rfc4115_result,
335                         eir, UINT64);
336 cmdline_parse_token_num_t cmd_add_port_meter_profile_trtcm_rfc4115_cbs =
337         TOKEN_NUM_INITIALIZER(
338                 struct cmd_add_port_meter_profile_trtcm_rfc4115_result,
339                         cbs, UINT64);
340 cmdline_parse_token_num_t cmd_add_port_meter_profile_trtcm_rfc4115_ebs =
341         TOKEN_NUM_INITIALIZER(
342                 struct cmd_add_port_meter_profile_trtcm_rfc4115_result,
343                         ebs, UINT64);
344
345 static void cmd_add_port_meter_profile_trtcm_rfc4115_parsed(
346         void *parsed_result,
347         __attribute__((unused)) struct cmdline *cl,
348         __attribute__((unused)) void *data)
349 {
350         struct cmd_add_port_meter_profile_trtcm_rfc4115_result *res =
351                 parsed_result;
352         struct rte_mtr_meter_profile mp;
353         struct rte_mtr_error error;
354         uint32_t profile_id = res->profile_id;
355         uint16_t port_id = res->port_id;
356         int ret;
357
358         if (port_id_is_invalid(port_id, ENABLED_WARN))
359                 return;
360
361         /* Private shaper profile params */
362         memset(&mp, 0, sizeof(struct rte_mtr_meter_profile));
363         mp.alg = 0;
364         mp.trtcm_rfc4115.cir = res->cir;
365         mp.trtcm_rfc4115.eir = res->eir;
366         mp.trtcm_rfc4115.cbs = res->cbs;
367         mp.trtcm_rfc4115.ebs = res->ebs;
368
369         ret = rte_mtr_meter_profile_add(port_id, profile_id, &mp, &error);
370         if (ret != 0) {
371                 print_err_msg(&error);
372                 return;
373         }
374 }
375
376 cmdline_parse_inst_t cmd_add_port_meter_profile_trtcm_rfc4115 = {
377         .f = cmd_add_port_meter_profile_trtcm_rfc4115_parsed,
378         .data = NULL,
379         .help_str = "Add port meter profile trtcm (rfc4115)",
380         .tokens = {
381                 (void *)&cmd_add_port_meter_profile_trtcm_rfc4115_add,
382                 (void *)&cmd_add_port_meter_profile_trtcm_rfc4115_port,
383                 (void *)&cmd_add_port_meter_profile_trtcm_rfc4115_meter,
384                 (void *)&cmd_add_port_meter_profile_trtcm_rfc4115_profile,
385                 (void *)&cmd_add_port_meter_profile_trtcm_rfc4115_port_id,
386                 (void *)&cmd_add_port_meter_profile_trtcm_rfc4115_profile_id,
387                 (void *)&cmd_add_port_meter_profile_trtcm_rfc4115_trtcm_rfc4115,
388                 (void *)&cmd_add_port_meter_profile_trtcm_rfc4115_cir,
389                 (void *)&cmd_add_port_meter_profile_trtcm_rfc4115_eir,
390                 (void *)&cmd_add_port_meter_profile_trtcm_rfc4115_cbs,
391                 (void *)&cmd_add_port_meter_profile_trtcm_rfc4115_ebs,
392                 NULL,
393         },
394 };
395
396 /* *** Delete Port Meter Profile *** */
397 struct cmd_del_port_meter_profile_result {
398         cmdline_fixed_string_t del;
399         cmdline_fixed_string_t port;
400         cmdline_fixed_string_t meter;
401         cmdline_fixed_string_t profile;
402         uint16_t port_id;
403         uint32_t profile_id;
404 };
405
406 cmdline_parse_token_string_t cmd_del_port_meter_profile_del =
407         TOKEN_STRING_INITIALIZER(
408                 struct cmd_del_port_meter_profile_result, del, "del");
409 cmdline_parse_token_string_t cmd_del_port_meter_profile_port =
410         TOKEN_STRING_INITIALIZER(
411                 struct cmd_del_port_meter_profile_result,
412                         port, "port");
413 cmdline_parse_token_string_t cmd_del_port_meter_profile_meter =
414         TOKEN_STRING_INITIALIZER(
415                 struct cmd_del_port_meter_profile_result,
416                         meter, "meter");
417 cmdline_parse_token_string_t cmd_del_port_meter_profile_profile =
418         TOKEN_STRING_INITIALIZER(
419                 struct cmd_del_port_meter_profile_result,
420                         profile, "profile");
421 cmdline_parse_token_num_t cmd_del_port_meter_profile_port_id =
422         TOKEN_NUM_INITIALIZER(
423                 struct cmd_del_port_meter_profile_result,
424                         port_id, UINT16);
425 cmdline_parse_token_num_t cmd_del_port_meter_profile_profile_id =
426         TOKEN_NUM_INITIALIZER(
427                 struct cmd_del_port_meter_profile_result,
428                         profile_id, UINT32);
429
430 static void cmd_del_port_meter_profile_parsed(void *parsed_result,
431         __attribute__((unused)) struct cmdline *cl,
432         __attribute__((unused)) void *data)
433 {
434         struct cmd_del_port_meter_profile_result *res = parsed_result;
435         struct rte_mtr_error error;
436         uint32_t profile_id = res->profile_id;
437         uint16_t port_id = res->port_id;
438         int ret;
439
440         if (port_id_is_invalid(port_id, ENABLED_WARN))
441                 return;
442
443         /* Delete meter profile */
444         ret = rte_mtr_meter_profile_delete(port_id, profile_id, &error);
445         if (ret != 0) {
446                 print_err_msg(&error);
447                 return;
448         }
449 }
450
451 cmdline_parse_inst_t cmd_del_port_meter_profile = {
452         .f = cmd_del_port_meter_profile_parsed,
453         .data = NULL,
454         .help_str = "Delete port meter profile",
455         .tokens = {
456                 (void *)&cmd_del_port_meter_profile_del,
457                 (void *)&cmd_del_port_meter_profile_port,
458                 (void *)&cmd_del_port_meter_profile_meter,
459                 (void *)&cmd_del_port_meter_profile_profile,
460                 (void *)&cmd_del_port_meter_profile_port_id,
461                 (void *)&cmd_del_port_meter_profile_profile_id,
462                 NULL,
463         },
464 };
465
466 /* *** Create Port Meter Object *** */
467 struct cmd_set_port_meter_result {
468         cmdline_fixed_string_t set;
469         cmdline_fixed_string_t port;
470         cmdline_fixed_string_t meter;
471         uint16_t port_id;
472         uint32_t mtr_id;
473         uint32_t profile_id;
474         cmdline_fixed_string_t g_action;
475         cmdline_fixed_string_t y_action;
476         cmdline_fixed_string_t r_action;
477         uint64_t statistics_mask;
478         uint32_t shared;
479 };
480
481 cmdline_parse_token_string_t cmd_set_port_meter_set =
482         TOKEN_STRING_INITIALIZER(
483                 struct cmd_set_port_meter_result, set, "set");
484 cmdline_parse_token_string_t cmd_set_port_meter_port =
485         TOKEN_STRING_INITIALIZER(
486                 struct cmd_set_port_meter_result, port, "port");
487 cmdline_parse_token_string_t cmd_set_port_meter_meter =
488         TOKEN_STRING_INITIALIZER(
489                 struct cmd_set_port_meter_result, meter, "meter");
490 cmdline_parse_token_num_t cmd_set_port_meter_port_id =
491         TOKEN_NUM_INITIALIZER(
492                 struct cmd_set_port_meter_result, port_id, UINT16);
493 cmdline_parse_token_num_t cmd_set_port_meter_mtr_id =
494         TOKEN_NUM_INITIALIZER(
495                 struct cmd_set_port_meter_result, mtr_id, UINT32);
496 cmdline_parse_token_num_t cmd_set_port_meter_profile_id =
497         TOKEN_NUM_INITIALIZER(
498                 struct cmd_set_port_meter_result, profile_id, UINT32);
499 cmdline_parse_token_string_t cmd_set_port_meter_g_action =
500         TOKEN_STRING_INITIALIZER(struct cmd_set_port_meter_result,
501                 g_action, "R#Y#G#D");
502 cmdline_parse_token_string_t cmd_set_port_meter_y_action =
503         TOKEN_STRING_INITIALIZER(struct cmd_set_port_meter_result,
504                 y_action, "R#Y#G#D");
505 cmdline_parse_token_string_t cmd_set_port_meter_r_action =
506         TOKEN_STRING_INITIALIZER(struct cmd_set_port_meter_result,
507                 r_action, "R#Y#G#D");
508 cmdline_parse_token_num_t cmd_set_port_meter_statistics_mask =
509         TOKEN_NUM_INITIALIZER(struct cmd_set_port_meter_result,
510                 statistics_mask, UINT64);
511 cmdline_parse_token_num_t cmd_set_port_meter_shared =
512         TOKEN_NUM_INITIALIZER(struct cmd_set_port_meter_result,
513                 shared, UINT32);
514
515 static void cmd_set_port_meter_parsed(void *parsed_result,
516         __attribute__((unused)) struct cmdline *cl,
517         __attribute__((unused)) void *data)
518 {
519         struct cmd_set_port_meter_result *res = parsed_result;
520         struct rte_mtr_error error;
521         struct rte_mtr_params params;
522         uint32_t mtr_id = res->mtr_id;
523         uint32_t shared = res->shared;
524         uint16_t port_id = res->port_id;
525
526         int ret;
527
528         if (port_id_is_invalid(port_id, ENABLED_WARN))
529                 return;
530
531         /* Meter params */
532         memset(&params, 0, sizeof(struct rte_mtr_params));
533         params.meter_profile_id = res->profile_id;
534         params.use_prev_mtr_color = 1;
535         params.dscp_table = NULL;
536         params.meter_enable = 1;
537         params.action[RTE_MTR_GREEN] =
538                 string_to_policer_action(res->g_action);
539         params.action[RTE_MTR_YELLOW] =
540                 string_to_policer_action(res->y_action);
541         params.action[RTE_MTR_RED] =
542                 string_to_policer_action(res->r_action);
543         params.stats_mask = res->statistics_mask;
544
545         ret = rte_mtr_create(port_id, mtr_id, &params, shared, &error);
546         if (ret != 0) {
547                 print_err_msg(&error);
548                 return;
549         }
550 }
551
552 cmdline_parse_inst_t cmd_set_port_meter = {
553         .f = cmd_set_port_meter_parsed,
554         .data = NULL,
555         .help_str = "Set port meter",
556         .tokens = {
557                 (void *)&cmd_set_port_meter_set,
558                 (void *)&cmd_set_port_meter_port,
559                 (void *)&cmd_set_port_meter_meter,
560                 (void *)&cmd_set_port_meter_port_id,
561                 (void *)&cmd_set_port_meter_mtr_id,
562                 (void *)&cmd_set_port_meter_profile_id,
563                 (void *)&cmd_set_port_meter_g_action,
564                 (void *)&cmd_set_port_meter_y_action,
565                 (void *)&cmd_set_port_meter_r_action,
566                 (void *)&cmd_set_port_meter_statistics_mask,
567                 (void *)&cmd_set_port_meter_shared,
568                 NULL,
569         },
570 };
571
572 /* *** Delete Port Meter Object *** */
573 struct cmd_del_port_meter_result {
574         cmdline_fixed_string_t del;
575         cmdline_fixed_string_t port;
576         cmdline_fixed_string_t meter;
577         uint16_t port_id;
578         uint32_t mtr_id;
579 };
580
581 cmdline_parse_token_string_t cmd_del_port_meter_del =
582         TOKEN_STRING_INITIALIZER(
583                 struct cmd_del_port_meter_result, del, "del");
584 cmdline_parse_token_string_t cmd_del_port_meter_port =
585         TOKEN_STRING_INITIALIZER(
586                 struct cmd_del_port_meter_result, port, "port");
587 cmdline_parse_token_string_t cmd_del_port_meter_meter =
588         TOKEN_STRING_INITIALIZER(
589                 struct cmd_del_port_meter_result, meter, "meter");
590 cmdline_parse_token_num_t cmd_del_port_meter_port_id =
591         TOKEN_NUM_INITIALIZER(
592                 struct cmd_del_port_meter_result, port_id, UINT16);
593 cmdline_parse_token_num_t cmd_del_port_meter_mtr_id =
594         TOKEN_NUM_INITIALIZER(
595                 struct cmd_del_port_meter_result, mtr_id, UINT32);
596
597 static void cmd_del_port_meter_parsed(void *parsed_result,
598         __attribute__((unused)) struct cmdline *cl,
599         __attribute__((unused)) void *data)
600 {
601         struct cmd_del_port_meter_result *res = parsed_result;
602         struct rte_mtr_error error;
603         uint32_t mtr_id = res->mtr_id;
604         uint16_t port_id = res->port_id;
605
606         int ret;
607
608         if (port_id_is_invalid(port_id, ENABLED_WARN))
609                 return;
610
611         /* Destroy Meter */
612         ret = rte_mtr_destroy(port_id, mtr_id, &error);
613         if (ret != 0) {
614                 print_err_msg(&error);
615                 return;
616         }
617 }
618
619 cmdline_parse_inst_t cmd_del_port_meter = {
620         .f = cmd_del_port_meter_parsed,
621         .data = NULL,
622         .help_str = "Delete port meter",
623         .tokens = {
624                 (void *)&cmd_del_port_meter_del,
625                 (void *)&cmd_del_port_meter_port,
626                 (void *)&cmd_del_port_meter_meter,
627                 (void *)&cmd_del_port_meter_port_id,
628                 (void *)&cmd_del_port_meter_mtr_id,
629                 NULL,
630         },
631 };
632
633 /* *** Set Port Meter Profile *** */
634 struct cmd_set_port_meter_profile_result {
635         cmdline_fixed_string_t set;
636         cmdline_fixed_string_t port;
637         cmdline_fixed_string_t meter;
638         cmdline_fixed_string_t profile;
639         uint16_t port_id;
640         uint32_t mtr_id;
641         uint32_t profile_id;
642 };
643
644 cmdline_parse_token_string_t cmd_set_port_meter_profile_set =
645         TOKEN_STRING_INITIALIZER(
646                 struct cmd_set_port_meter_profile_result, set, "set");
647 cmdline_parse_token_string_t cmd_set_port_meter_profile_port =
648         TOKEN_STRING_INITIALIZER(
649                 struct cmd_set_port_meter_profile_result, port, "port");
650 cmdline_parse_token_string_t cmd_set_port_meter_profile_meter =
651         TOKEN_STRING_INITIALIZER(
652                 struct cmd_set_port_meter_profile_result, meter, "meter");
653 cmdline_parse_token_string_t cmd_set_port_meter_profile_profile =
654         TOKEN_STRING_INITIALIZER(
655                 struct cmd_set_port_meter_profile_result, profile, "profile");
656 cmdline_parse_token_num_t cmd_set_port_meter_profile_port_id =
657         TOKEN_NUM_INITIALIZER(
658                 struct cmd_set_port_meter_profile_result, port_id, UINT16);
659 cmdline_parse_token_num_t cmd_set_port_meter_profile_mtr_id =
660         TOKEN_NUM_INITIALIZER(
661                 struct cmd_set_port_meter_profile_result, mtr_id, UINT32);
662 cmdline_parse_token_num_t cmd_set_port_meter_profile_profile_id =
663         TOKEN_NUM_INITIALIZER(
664                 struct cmd_set_port_meter_profile_result, profile_id, UINT32);
665
666 static void cmd_set_port_meter_profile_parsed(void *parsed_result,
667         __attribute__((unused)) struct cmdline *cl,
668         __attribute__((unused)) void *data)
669 {
670         struct cmd_set_port_meter_profile_result *res = parsed_result;
671         struct rte_mtr_error error;
672         uint32_t mtr_id = res->mtr_id;
673         uint32_t profile_id = res->profile_id;
674         uint16_t port_id = res->port_id;
675
676         int ret;
677
678         if (port_id_is_invalid(port_id, ENABLED_WARN))
679                 return;
680
681         /* Set meter profile */
682         ret = rte_mtr_meter_profile_update(port_id, mtr_id,
683                 profile_id, &error);
684         if (ret != 0) {
685                 print_err_msg(&error);
686                 return;
687         }
688 }
689
690 cmdline_parse_inst_t cmd_set_port_meter_profile = {
691         .f = cmd_set_port_meter_profile_parsed,
692         .data = NULL,
693         .help_str = "Set port meter profile",
694         .tokens = {
695                 (void *)&cmd_set_port_meter_profile_set,
696                 (void *)&cmd_set_port_meter_profile_port,
697                 (void *)&cmd_set_port_meter_profile_meter,
698                 (void *)&cmd_set_port_meter_profile_profile,
699                 (void *)&cmd_set_port_meter_profile_port_id,
700                 (void *)&cmd_set_port_meter_profile_mtr_id,
701                 (void *)&cmd_set_port_meter_profile_profile_id,
702                 NULL,
703         },
704 };
705
706 /* *** Set Port Meter Policer Action *** */
707 struct cmd_set_port_meter_policer_action_result {
708         cmdline_fixed_string_t set;
709         cmdline_fixed_string_t port;
710         cmdline_fixed_string_t meter;
711         cmdline_fixed_string_t policer;
712         cmdline_fixed_string_t action;
713         uint16_t port_id;
714         uint32_t mtr_id;
715         cmdline_fixed_string_t color;
716         cmdline_fixed_string_t policer_action;
717 };
718
719 cmdline_parse_token_string_t cmd_set_port_meter_policer_action_set =
720         TOKEN_STRING_INITIALIZER(
721                 struct cmd_set_port_meter_policer_action_result, set, "set");
722 cmdline_parse_token_string_t cmd_set_port_meter_policer_action_port =
723         TOKEN_STRING_INITIALIZER(
724                 struct cmd_set_port_meter_policer_action_result, port, "port");
725 cmdline_parse_token_string_t cmd_set_port_meter_policer_action_meter =
726         TOKEN_STRING_INITIALIZER(
727                 struct cmd_set_port_meter_policer_action_result, meter,
728                 "meter");
729 cmdline_parse_token_string_t cmd_set_port_meter_policer_action_policer =
730         TOKEN_STRING_INITIALIZER(
731                 struct cmd_set_port_meter_policer_action_result, policer,
732                 "policer");
733 cmdline_parse_token_string_t cmd_set_port_meter_policer_action_action =
734         TOKEN_STRING_INITIALIZER(
735                 struct cmd_set_port_meter_policer_action_result, action,
736                 "action");
737 cmdline_parse_token_num_t cmd_set_port_meter_policer_action_port_id =
738         TOKEN_NUM_INITIALIZER(
739                 struct cmd_set_port_meter_policer_action_result, port_id,
740                 UINT16);
741 cmdline_parse_token_num_t cmd_set_port_meter_policer_action_mtr_id =
742         TOKEN_NUM_INITIALIZER(
743                 struct cmd_set_port_meter_policer_action_result, mtr_id,
744                 UINT32);
745 cmdline_parse_token_string_t cmd_set_port_meter_policer_action_color =
746         TOKEN_STRING_INITIALIZER(
747                 struct cmd_set_port_meter_policer_action_result, color,
748                 "G#Y#R");
749 cmdline_parse_token_string_t cmd_set_port_meter_policer_action_policer_action =
750         TOKEN_STRING_INITIALIZER(
751                 struct cmd_set_port_meter_policer_action_result,
752                 policer_action, "G#Y#R#D");
753
754 static void cmd_set_port_meter_policer_action_parsed(void *parsed_result,
755         __attribute__((unused)) struct cmdline *cl,
756         __attribute__((unused)) void *data)
757 {
758         struct cmd_set_port_meter_policer_action_result *res = parsed_result;
759         enum rte_mtr_color color;
760         enum rte_mtr_policer_action action[RTE_MTR_COLORS];
761         struct rte_mtr_error error;
762         uint32_t mtr_id = res->mtr_id;
763         uint16_t port_id = res->port_id;
764         char *c = res->color;
765         char *a = res->policer_action;
766         int ret;
767
768         if (port_id_is_invalid(port_id, ENABLED_WARN))
769                 return;
770
771         /* Color */
772         if (strcmp(c, "G") == 0)
773                 color = RTE_MTR_GREEN;
774         else if (strcmp(c, "Y") == 0)
775                 color = RTE_MTR_YELLOW;
776         else
777                 color = RTE_MTR_RED;
778
779         /* Action */
780         if (strcmp(a, "G") == 0)
781                 action[color] = MTR_POLICER_ACTION_COLOR_GREEN;
782         else if (strcmp(a, "Y") == 0)
783                 action[color] = MTR_POLICER_ACTION_COLOR_YELLOW;
784         else if (strcmp(a, "R") == 0)
785                 action[color] = MTR_POLICER_ACTION_COLOR_RED;
786         else
787                 action[color] = MTR_POLICER_ACTION_DROP;
788
789         ret = rte_mtr_policer_actions_update(port_id, mtr_id,
790                 1 << color, action, &error);
791         if (ret != 0) {
792                 print_err_msg(&error);
793                 return;
794         }
795 }
796
797 cmdline_parse_inst_t cmd_set_port_meter_policer_action = {
798         .f = cmd_set_port_meter_policer_action_parsed,
799         .data = NULL,
800         .help_str = "Set port meter policer action",
801         .tokens = {
802                 (void *)&cmd_set_port_meter_policer_action_set,
803                 (void *)&cmd_set_port_meter_policer_action_port,
804                 (void *)&cmd_set_port_meter_policer_action_meter,
805                 (void *)&cmd_set_port_meter_policer_action_policer,
806                 (void *)&cmd_set_port_meter_policer_action_action,
807                 (void *)&cmd_set_port_meter_policer_action_port_id,
808                 (void *)&cmd_set_port_meter_policer_action_mtr_id,
809                 (void *)&cmd_set_port_meter_policer_action_color,
810                 (void *)&cmd_set_port_meter_policer_action_policer_action,
811                 NULL,
812         },
813 };
814
815 /* *** Set Port Meter Stats Mask *** */
816 struct cmd_set_port_meter_stats_mask_result {
817         cmdline_fixed_string_t set;
818         cmdline_fixed_string_t port;
819         cmdline_fixed_string_t meter;
820         cmdline_fixed_string_t stats;
821         cmdline_fixed_string_t mask;
822         uint16_t port_id;
823         uint32_t mtr_id;
824         uint64_t stats_mask;
825 };
826
827 cmdline_parse_token_string_t cmd_set_port_meter_stats_mask_set =
828         TOKEN_STRING_INITIALIZER(
829                 struct cmd_set_port_meter_stats_mask_result, set, "set");
830 cmdline_parse_token_string_t cmd_set_port_meter_stats_mask_port =
831         TOKEN_STRING_INITIALIZER(
832                 struct cmd_set_port_meter_stats_mask_result, port, "port");
833 cmdline_parse_token_string_t cmd_set_port_meter_stats_mask_meter =
834         TOKEN_STRING_INITIALIZER(
835                 struct cmd_set_port_meter_stats_mask_result, meter, "meter");
836 cmdline_parse_token_string_t cmd_set_port_meter_stats_mask_stats =
837         TOKEN_STRING_INITIALIZER(
838                 struct cmd_set_port_meter_stats_mask_result, stats, "stats");
839 cmdline_parse_token_string_t cmd_set_port_meter_stats_mask_mask =
840         TOKEN_STRING_INITIALIZER(
841                 struct cmd_set_port_meter_stats_mask_result, mask, "mask");
842 cmdline_parse_token_num_t cmd_set_port_meter_stats_mask_port_id =
843         TOKEN_NUM_INITIALIZER(
844                 struct cmd_set_port_meter_stats_mask_result, port_id, UINT16);
845 cmdline_parse_token_num_t cmd_set_port_meter_stats_mask_mtr_id =
846         TOKEN_NUM_INITIALIZER(
847                 struct cmd_set_port_meter_stats_mask_result, mtr_id, UINT32);
848 cmdline_parse_token_num_t cmd_set_port_meter_stats_mask_stats_mask =
849         TOKEN_NUM_INITIALIZER(
850                 struct cmd_set_port_meter_stats_mask_result, stats_mask,
851                 UINT64);
852
853 static void cmd_set_port_meter_stats_mask_parsed(void *parsed_result,
854         __attribute__((unused)) struct cmdline *cl,
855         __attribute__((unused)) void *data)
856 {
857         struct cmd_set_port_meter_stats_mask_result *res = parsed_result;
858         struct rte_mtr_error error;
859         uint64_t stats_mask = res->stats_mask;
860         uint32_t mtr_id = res->mtr_id;
861         uint16_t port_id = res->port_id;
862         int ret;
863
864         if (port_id_is_invalid(port_id, ENABLED_WARN))
865                 return;
866
867         ret = rte_mtr_stats_update(port_id, mtr_id, stats_mask, &error);
868         if (ret != 0) {
869                 print_err_msg(&error);
870                 return;
871         }
872 }
873
874 cmdline_parse_inst_t cmd_set_port_meter_stats_mask = {
875         .f = cmd_set_port_meter_stats_mask_parsed,
876         .data = NULL,
877         .help_str = "Set port meter stats mask",
878         .tokens = {
879                 (void *)&cmd_set_port_meter_stats_mask_set,
880                 (void *)&cmd_set_port_meter_stats_mask_port,
881                 (void *)&cmd_set_port_meter_stats_mask_meter,
882                 (void *)&cmd_set_port_meter_stats_mask_stats,
883                 (void *)&cmd_set_port_meter_stats_mask_mask,
884                 (void *)&cmd_set_port_meter_stats_mask_port_id,
885                 (void *)&cmd_set_port_meter_stats_mask_mtr_id,
886                 (void *)&cmd_set_port_meter_stats_mask_stats_mask,
887                 NULL,
888         },
889 };
890
891 /* *** Show Port Meter Stats *** */
892 struct cmd_show_port_meter_stats_result {
893         cmdline_fixed_string_t show;
894         cmdline_fixed_string_t port;
895         cmdline_fixed_string_t meter;
896         cmdline_fixed_string_t stats;
897         uint16_t port_id;
898         uint32_t mtr_id;
899         uint32_t clear;
900 };
901
902 cmdline_parse_token_string_t cmd_show_port_meter_stats_show =
903         TOKEN_STRING_INITIALIZER(
904                 struct cmd_show_port_meter_stats_result, show, "show");
905 cmdline_parse_token_string_t cmd_show_port_meter_stats_port =
906         TOKEN_STRING_INITIALIZER(
907                 struct cmd_show_port_meter_stats_result, port, "port");
908 cmdline_parse_token_string_t cmd_show_port_meter_stats_meter =
909         TOKEN_STRING_INITIALIZER(
910                 struct cmd_show_port_meter_stats_result, meter, "meter");
911 cmdline_parse_token_string_t cmd_show_port_meter_stats_stats =
912         TOKEN_STRING_INITIALIZER(
913                 struct cmd_show_port_meter_stats_result, stats, "stats");
914 cmdline_parse_token_num_t cmd_show_port_meter_stats_port_id =
915         TOKEN_NUM_INITIALIZER(
916                 struct cmd_show_port_meter_stats_result, port_id, UINT16);
917 cmdline_parse_token_num_t cmd_show_port_meter_stats_mtr_id =
918         TOKEN_NUM_INITIALIZER(
919                 struct cmd_show_port_meter_stats_result, mtr_id, UINT32);
920 cmdline_parse_token_num_t cmd_show_port_meter_stats_clear =
921         TOKEN_NUM_INITIALIZER(
922                 struct cmd_show_port_meter_stats_result, clear, UINT32);
923
924 static void cmd_show_port_meter_stats_parsed(void *parsed_result,
925         __attribute__((unused)) struct cmdline *cl,
926         __attribute__((unused)) void *data)
927 {
928         struct cmd_show_port_meter_stats_result *res = parsed_result;
929         struct rte_mtr_stats stats;
930         uint64_t stats_mask = 0;
931         struct rte_mtr_error error;
932         uint32_t mtr_id = res->mtr_id;
933         uint32_t clear = res->clear;
934         uint16_t port_id = res->port_id;
935         int ret;
936
937         if (port_id_is_invalid(port_id, ENABLED_WARN))
938                 return;
939
940         memset(&stats, 0, sizeof(struct rte_mtr_stats));
941         ret = rte_mtr_stats_read(port_id, mtr_id, &stats,
942                 &stats_mask, clear, &error);
943         if (ret != 0) {
944                 print_err_msg(&error);
945                 return;
946         }
947
948         /* Display stats */
949         if (stats_mask & RTE_MTR_STATS_N_PKTS_GREEN)
950                 printf("\tPkts G: %" PRIu64 "\n",
951                         stats.n_pkts[RTE_MTR_GREEN]);
952         if (stats_mask & RTE_MTR_STATS_N_BYTES_GREEN)
953                 printf("\tBytes G: %" PRIu64 "\n",
954                         stats.n_bytes[RTE_MTR_GREEN]);
955         if (stats_mask & RTE_MTR_STATS_N_PKTS_YELLOW)
956                 printf("\tPkts Y: %" PRIu64 "\n",
957                         stats.n_pkts[RTE_MTR_YELLOW]);
958         if (stats_mask & RTE_MTR_STATS_N_BYTES_YELLOW)
959                 printf("\tBytes Y: %" PRIu64 "\n",
960                         stats.n_bytes[RTE_MTR_YELLOW]);
961         if (stats_mask & RTE_MTR_STATS_N_PKTS_RED)
962                 printf("\tPkts R: %" PRIu64 "\n",
963                         stats.n_pkts[RTE_MTR_RED]);
964         if (stats_mask & RTE_MTR_STATS_N_BYTES_RED)
965                 printf("\tBytes Y: %" PRIu64 "\n",
966                         stats.n_bytes[RTE_MTR_RED]);
967         if (stats_mask & RTE_MTR_STATS_N_PKTS_DROPPED)
968                 printf("\tPkts DROPPED: %" PRIu64 "\n",
969                         stats.n_pkts_dropped);
970         if (stats_mask & RTE_MTR_STATS_N_BYTES_DROPPED)
971                 printf("\tBytes DROPPED: %" PRIu64 "\n",
972                         stats.n_bytes_dropped);
973 }
974
975 cmdline_parse_inst_t cmd_show_port_meter_stats = {
976         .f = cmd_show_port_meter_stats_parsed,
977         .data = NULL,
978         .help_str = "Show port meter stats",
979         .tokens = {
980                 (void *)&cmd_show_port_meter_stats_show,
981                 (void *)&cmd_show_port_meter_stats_port,
982                 (void *)&cmd_show_port_meter_stats_meter,
983                 (void *)&cmd_show_port_meter_stats_stats,
984                 (void *)&cmd_show_port_meter_stats_port_id,
985                 (void *)&cmd_show_port_meter_stats_mtr_id,
986                 (void *)&cmd_show_port_meter_stats_clear,
987                 NULL,
988         },
989 };