New upstream version 18.02
[deb_dpdk.git] / examples / quota_watermark / qwctl / commands.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <stdint.h>
7 #include <string.h>
8 #include <termios.h>
9
10 #include <cmdline_rdline.h>
11 #include <cmdline_parse.h>
12 #include <cmdline_parse_num.h>
13 #include <cmdline_parse_string.h>
14 #include <cmdline.h>
15
16 #include <rte_ring.h>
17
18 #include "qwctl.h"
19 #include "../include/conf.h"
20
21
22 /**
23  * help command
24  */
25
26 struct cmd_help_tokens {
27         cmdline_fixed_string_t verb;
28 };
29
30 cmdline_parse_token_string_t cmd_help_verb =
31                 TOKEN_STRING_INITIALIZER(struct cmd_help_tokens, verb, "help");
32
33 static void
34 cmd_help_handler(__attribute__((unused)) void *parsed_result,
35                 struct cmdline *cl,
36                 __attribute__((unused)) void *data)
37 {
38         cmdline_printf(cl, "Available commands:\n"
39                         "- help\n"
40                         "- set  [ring_name|variable] <value>\n"
41                         "- show [ring_name|variable]\n"
42                         "\n"
43                         "Available variables:\n"
44                         "- low_watermark\n"
45                         "- quota\n"
46                         "- ring names follow the core%%u_port%%u format\n");
47 }
48
49 cmdline_parse_inst_t cmd_help = {
50                 .f = cmd_help_handler,
51                 .data = NULL,
52                 .help_str = "show help",
53                 .tokens = {
54                                 (void *) &cmd_help_verb,
55                                 NULL,
56                 },
57 };
58
59
60 /**
61  * set command
62  */
63
64 struct cmd_set_tokens {
65         cmdline_fixed_string_t verb;
66         cmdline_fixed_string_t variable;
67         uint32_t value;
68 };
69
70 cmdline_parse_token_string_t cmd_set_verb =
71                 TOKEN_STRING_INITIALIZER(struct cmd_set_tokens, verb, "set");
72
73 cmdline_parse_token_string_t cmd_set_variable =
74                 TOKEN_STRING_INITIALIZER(struct cmd_set_tokens, variable, NULL);
75
76 cmdline_parse_token_num_t cmd_set_value =
77                 TOKEN_NUM_INITIALIZER(struct cmd_set_tokens, value, UINT32);
78
79 static void
80 cmd_set_handler(__attribute__((unused)) void *parsed_result,
81                 struct cmdline *cl,
82                 __attribute__((unused)) void *data)
83 {
84         struct cmd_set_tokens *tokens = parsed_result;
85         struct rte_ring *ring;
86
87         if (!strcmp(tokens->variable, "quota")) {
88
89                 if (tokens->value > 0 && tokens->value <= MAX_PKT_QUOTA)
90                         *quota = tokens->value;
91                 else
92                         cmdline_printf(cl, "quota must be between 1 and %u\n",
93                                         MAX_PKT_QUOTA);
94         }
95
96         else if (!strcmp(tokens->variable, "low_watermark")) {
97
98                 if (tokens->value <= 100)
99                         *low_watermark = tokens->value * RING_SIZE / 100;
100                 else
101                         cmdline_printf(cl,
102                                         "low_watermark must be between 0%% and 100%%\n");
103         }
104
105         else {
106
107                 ring = rte_ring_lookup(tokens->variable);
108                 if (ring == NULL)
109                         cmdline_printf(cl, "Cannot find ring \"%s\"\n",
110                                         tokens->variable);
111                 else
112                         if (tokens->value >= *low_watermark * 100 / RING_SIZE
113                                         && tokens->value <= 100)
114                                 *high_watermark = tokens->value *
115                                                 RING_SIZE / 100;
116                         else
117                                 cmdline_printf(cl,
118                                         "ring high watermark must be between %u%% and 100%%\n",
119                                         *low_watermark * 100 / RING_SIZE);
120         }
121 }
122
123 cmdline_parse_inst_t cmd_set = {
124                 .f = cmd_set_handler,
125                 .data = NULL,
126                 .help_str = "Set a variable value",
127                 .tokens = {
128                                 (void *) &cmd_set_verb,
129                                 (void *) &cmd_set_variable,
130                                 (void *) &cmd_set_value,
131                                 NULL,
132                 },
133 };
134
135
136 /**
137  * show command
138  */
139
140 struct cmd_show_tokens {
141         cmdline_fixed_string_t verb;
142         cmdline_fixed_string_t variable;
143 };
144
145 cmdline_parse_token_string_t cmd_show_verb =
146                 TOKEN_STRING_INITIALIZER(struct cmd_show_tokens, verb, "show");
147
148 cmdline_parse_token_string_t cmd_show_variable =
149                 TOKEN_STRING_INITIALIZER(struct cmd_show_tokens,
150                                 variable, NULL);
151
152
153 static void
154 cmd_show_handler(__attribute__((unused)) void *parsed_result,
155                 struct cmdline *cl,
156                 __attribute__((unused)) void *data)
157 {
158         struct cmd_show_tokens *tokens = parsed_result;
159         struct rte_ring *ring;
160
161         if (!strcmp(tokens->variable, "quota"))
162                 cmdline_printf(cl, "Global quota: %d\n", *quota);
163
164         else if (!strcmp(tokens->variable, "low_watermark"))
165                 cmdline_printf(cl, "Global low_watermark: %u\n",
166                                 *low_watermark);
167
168         else {
169
170                 ring = rte_ring_lookup(tokens->variable);
171                 if (ring == NULL)
172                         cmdline_printf(cl, "Cannot find ring \"%s\"\n",
173                                         tokens->variable);
174                 else
175                         rte_ring_dump(stdout, ring);
176         }
177 }
178
179 cmdline_parse_inst_t cmd_show = {
180                 .f = cmd_show_handler,
181                 .data = NULL,
182                 .help_str = "Show a variable value",
183                 .tokens = {
184                                 (void *) &cmd_show_verb,
185                                 (void *) &cmd_show_variable,
186                                 NULL,
187                 },
188 };
189
190
191 cmdline_parse_ctx_t qwctl_ctx[] = {
192                 (cmdline_parse_inst_t *)&cmd_help,
193                 (cmdline_parse_inst_t *)&cmd_set,
194                 (cmdline_parse_inst_t *)&cmd_show,
195                 NULL,
196 };