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