vhost: fix IP checksum
[dpdk.git] / examples / quota_watermark / qwctl / commands.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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 <stdio.h>
35 #include <stdint.h>
36 #include <string.h>
37 #include <termios.h>
38
39 #include <cmdline_rdline.h>
40 #include <cmdline_parse.h>
41 #include <cmdline_parse_num.h>
42 #include <cmdline_parse_string.h>
43 #include <cmdline.h>
44
45 #include <rte_ring.h>
46
47 #include "qwctl.h"
48 #include "../include/conf.h"
49
50
51 /**
52  * help command
53  */
54
55 struct cmd_help_tokens {
56         cmdline_fixed_string_t verb;
57 };
58
59 cmdline_parse_token_string_t cmd_help_verb =
60                 TOKEN_STRING_INITIALIZER(struct cmd_help_tokens, verb, "help");
61
62 static void
63 cmd_help_handler(__attribute__((unused)) void *parsed_result,
64                 struct cmdline *cl,
65                 __attribute__((unused)) void *data)
66 {
67         cmdline_printf(cl, "Available commands:\n"
68                         "- help\n"
69                         "- set  [ring_name|variable] <value>\n"
70                         "- show [ring_name|variable]\n"
71                         "\n"
72                         "Available variables:\n"
73                         "- low_watermark\n"
74                         "- quota\n"
75                         "- ring names follow the core%%u_port%%u format\n");
76 }
77
78 cmdline_parse_inst_t cmd_help = {
79                 .f = cmd_help_handler,
80                 .data = NULL,
81                 .help_str = "show help",
82                 .tokens = {
83                                 (void *) &cmd_help_verb,
84                                 NULL,
85                 },
86 };
87
88
89 /**
90  * set command
91  */
92
93 struct cmd_set_tokens {
94         cmdline_fixed_string_t verb;
95         cmdline_fixed_string_t variable;
96         uint32_t value;
97 };
98
99 cmdline_parse_token_string_t cmd_set_verb =
100                 TOKEN_STRING_INITIALIZER(struct cmd_set_tokens, verb, "set");
101
102 cmdline_parse_token_string_t cmd_set_variable =
103                 TOKEN_STRING_INITIALIZER(struct cmd_set_tokens, variable, NULL);
104
105 cmdline_parse_token_num_t cmd_set_value =
106                 TOKEN_NUM_INITIALIZER(struct cmd_set_tokens, value, UINT32);
107
108 static void
109 cmd_set_handler(__attribute__((unused)) void *parsed_result,
110                 struct cmdline *cl,
111                 __attribute__((unused)) void *data)
112 {
113         struct cmd_set_tokens *tokens = parsed_result;
114         struct rte_ring *ring;
115
116         if (!strcmp(tokens->variable, "quota")) {
117
118                 if (tokens->value > 0 && tokens->value <= MAX_PKT_QUOTA)
119                         *quota = tokens->value;
120                 else
121                         cmdline_printf(cl, "quota must be between 1 and %u\n",
122                                         MAX_PKT_QUOTA);
123         }
124
125         else if (!strcmp(tokens->variable, "low_watermark")) {
126
127                 if (tokens->value <= 100)
128                         *low_watermark = tokens->value * RING_SIZE / 100;
129                 else
130                         cmdline_printf(cl,
131                                         "low_watermark must be between 0%% and 100%%\n");
132         }
133
134         else {
135
136                 ring = rte_ring_lookup(tokens->variable);
137                 if (ring == NULL)
138                         cmdline_printf(cl, "Cannot find ring \"%s\"\n",
139                                         tokens->variable);
140                 else
141                         if (tokens->value >= *low_watermark * 100 / RING_SIZE
142                                         && tokens->value <= 100)
143                                 *high_watermark = tokens->value *
144                                                 RING_SIZE / 100;
145                         else
146                                 cmdline_printf(cl,
147                                         "ring high watermark must be between %u%% and 100%%\n",
148                                         *low_watermark * 100 / RING_SIZE);
149         }
150 }
151
152 cmdline_parse_inst_t cmd_set = {
153                 .f = cmd_set_handler,
154                 .data = NULL,
155                 .help_str = "Set a variable value",
156                 .tokens = {
157                                 (void *) &cmd_set_verb,
158                                 (void *) &cmd_set_variable,
159                                 (void *) &cmd_set_value,
160                                 NULL,
161                 },
162 };
163
164
165 /**
166  * show command
167  */
168
169 struct cmd_show_tokens {
170         cmdline_fixed_string_t verb;
171         cmdline_fixed_string_t variable;
172 };
173
174 cmdline_parse_token_string_t cmd_show_verb =
175                 TOKEN_STRING_INITIALIZER(struct cmd_show_tokens, verb, "show");
176
177 cmdline_parse_token_string_t cmd_show_variable =
178                 TOKEN_STRING_INITIALIZER(struct cmd_show_tokens,
179                                 variable, NULL);
180
181
182 static void
183 cmd_show_handler(__attribute__((unused)) void *parsed_result,
184                 struct cmdline *cl,
185                 __attribute__((unused)) void *data)
186 {
187         struct cmd_show_tokens *tokens = parsed_result;
188         struct rte_ring *ring;
189
190         if (!strcmp(tokens->variable, "quota"))
191                 cmdline_printf(cl, "Global quota: %d\n", *quota);
192
193         else if (!strcmp(tokens->variable, "low_watermark"))
194                 cmdline_printf(cl, "Global low_watermark: %u\n",
195                                 *low_watermark);
196
197         else {
198
199                 ring = rte_ring_lookup(tokens->variable);
200                 if (ring == NULL)
201                         cmdline_printf(cl, "Cannot find ring \"%s\"\n",
202                                         tokens->variable);
203                 else
204                         rte_ring_dump(stdout, ring);
205         }
206 }
207
208 cmdline_parse_inst_t cmd_show = {
209                 .f = cmd_show_handler,
210                 .data = NULL,
211                 .help_str = "Show a variable value",
212                 .tokens = {
213                                 (void *) &cmd_show_verb,
214                                 (void *) &cmd_show_variable,
215                                 NULL,
216                 },
217 };
218
219
220 cmdline_parse_ctx_t qwctl_ctx[] = {
221                 (cmdline_parse_inst_t *)&cmd_help,
222                 (cmdline_parse_inst_t *)&cmd_set,
223                 (cmdline_parse_inst_t *)&cmd_show,
224                 NULL,
225 };