aboutsummaryrefslogtreecommitdiffstats
path: root/examples/quota_watermark/qwctl/commands.c
blob: a1c646b9fb520b21c00c7111d7ed06b3b80ae60a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2010-2014 Intel Corporation
 */

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <termios.h>

#include <cmdline_rdline.h>
#include <cmdline_parse.h>
#include <cmdline_parse_num.h>
#include <cmdline_parse_string.h>
#include <cmdline.h>

#include <rte_ring.h>

#include "qwctl.h"
#include "../include/conf.h"


/**
 * help command
 */

struct cmd_help_tokens {
	cmdline_fixed_string_t verb;
};

cmdline_parse_token_string_t cmd_help_verb =
		TOKEN_STRING_INITIALIZER(struct cmd_help_tokens, verb, "help");

static void
cmd_help_handler(__attribute__((unused)) void *parsed_result,
		struct cmdline *cl,
		__attribute__((unused)) void *data)
{
	cmdline_printf(cl, "Available commands:\n"
			"- help\n"
			"- set  [ring_name|variable] <value>\n"
			"- show [ring_name|variable]\n"
			"\n"
			"Available variables:\n"
			"- low_watermark\n"
			"- quota\n"
			"- ring names follow the core%%u_port%%u format\n");
}

cmdline_parse_inst_t cmd_help = {
		.f = cmd_help_handler,
		.data = NULL,
		.help_str = "show help",
		.tokens = {
				(void *) &cmd_help_verb,
				NULL,
		},
};


/**
 * set command
 */

struct cmd_set_tokens {
	cmdline_fixed_string_t verb;
	cmdline_fixed_string_t variable;
	uint32_t value;
};

cmdline_parse_token_string_t cmd_set_verb =
		TOKEN_STRING_INITIALIZER(struct cmd_set_tokens, verb, "set");

cmdline_parse_token_string_t cmd_set_variable =
		TOKEN_STRING_INITIALIZER(struct cmd_set_tokens, variable, NULL);

cmdline_parse_token_num_t cmd_set_value =
		TOKEN_NUM_INITIALIZER(struct cmd_set_tokens, value, UINT32);

static void
cmd_set_handler(__attribute__((unused)) void *parsed_result,
		struct cmdline *cl,
		__attribute__((unused)) void *data)
{
	struct cmd_set_tokens *tokens = parsed_result;
	struct rte_ring *ring;

	if (!strcmp(tokens->variable, "quota")) {

		if (tokens->value > 0 && tokens->value <= MAX_PKT_QUOTA)
			*quota = tokens->value;
		else
			cmdline_printf(cl, "quota must be between 1 and %u\n",
					MAX_PKT_QUOTA);
	}

	else if (!strcmp(tokens->variable, "low_watermark")) {

		if (tokens->value <= 100)
			*low_watermark = tokens->value * RING_SIZE / 100;
		else
			cmdline_printf(cl,
					"low_watermark must be between 0%% and 100%%\n");
	}

	else {

		ring = rte_ring_lookup(tokens->variable);
		if (ring == NULL)
			cmdline_printf(cl, "Cannot find ring \"%s\"\n",
					tokens->variable);
		else
			if (tokens->value >= *low_watermark * 100 / RING_SIZE
					&& tokens->value <= 100)
				*high_watermark = tokens->value *
						RING_SIZE / 100;
			else
				cmdline_printf(cl,
					"ring high watermark must be between %u%% and 100%%\n",
					*low_watermark * 100 / RING_SIZE);
	}
}

cmdline_parse_inst_t cmd_set = {
		.f = cmd_set_handler,
		.data = NULL,
		.help_str = "Set a variable value",
		.tokens = {
				(void *) &cmd_set_verb,
				(void *) &cmd_set_variable,
				(void *) &cmd_set_value,
				NULL,
		},
};


/**
 * show command
 */

struct cmd_show_tokens {
	cmdline_fixed_string_t verb;
	cmdline_fixed_string_t variable;
};

cmdline_parse_token_string_t cmd_show_verb =
		TOKEN_STRING_INITIALIZER(struct cmd_show_tokens, verb, "show");

cmdline_parse_token_string_t cmd_show_variable =
		TOKEN_STRING_INITIALIZER(struct cmd_show_tokens,
				variable, NULL);


static void
cmd_show_handler(__attribute__((unused)) void *parsed_result,
		struct cmdline *cl,
		__attribute__((unused)) void *data)
{
	struct cmd_show_tokens *tokens = parsed_result;
	struct rte_ring *ring;

	if (!strcmp(tokens->variable, "quota"))
		cmdline_printf(cl, "Global quota: %d\n", *quota);

	else if (!strcmp(tokens->variable, "low_watermark"))
		cmdline_printf(cl, "Global low_watermark: %u\n",
				*low_watermark);

	else {

		ring = rte_ring_lookup(tokens->variable);
		if (ring == NULL)
			cmdline_printf(cl, "Cannot find ring \"%s\"\n",
					tokens->variable);
		else
			rte_ring_dump(stdout, ring);
	}
}

cmdline_parse_inst_t cmd_show = {
		.f = cmd_show_handler,
		.data = NULL,
		.help_str = "Show a variable value",
		.tokens = {
				(void *) &cmd_show_verb,
				(void *) &cmd_show_variable,
				NULL,
		},
};


cmdline_parse_ctx_t qwctl_ctx[] = {
		(cmdline_parse_inst_t *)&cmd_help,
		(cmdline_parse_inst_t *)&cmd_set,
		(cmdline_parse_inst_t *)&cmd_show,
		NULL,
};