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