blob: 9132fe907c7bfa7aa45af5bd02ef93d4355f973b [file] [log] [blame]
Miroslav Zagorac70230c62020-12-09 16:54:31 +01001/***
2 * Copyright 2020 HAProxy Technologies
3 *
4 * This file is part of the HAProxy OpenTracing filter.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20#include "include.h"
21
22
23/***
24 * NAME
25 * flt_ot_cli_set_msg -
26 *
27 * ARGUMENTS
28 * appctx -
29 * err -
30 * msg -
31 * cli_state -
32 *
33 * DESCRIPTION
34 * -
35 *
36 * RETURN VALUE
37 * This function does not return a value.
38 */
39static void cmn_cli_set_msg(struct appctx *appctx, char *err, char *msg, int cli_state)
40{
Willy Tarreau1c0715b2022-05-06 17:16:35 +020041 struct cli_print_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
42
Miroslav Zagorac70230c62020-12-09 16:54:31 +010043 FLT_OT_FUNC("%p, %p, %p, %d", appctx, err, msg, cli_state);
44
45 if ((appctx == NULL) || ((err == NULL) && (msg == NULL)))
46 FLT_OT_RETURN();
47
Willy Tarreau1c0715b2022-05-06 17:16:35 +020048 ctx->err = (err == NULL) ? msg : err;
49 appctx->st0 = (ctx->err == NULL) ? CLI_ST_PROMPT : cli_state;
Miroslav Zagorac70230c62020-12-09 16:54:31 +010050
Willy Tarreau1c0715b2022-05-06 17:16:35 +020051 FLT_OT_DBG(1, "err(%d): \"%s\"", appctx->st0, ctx->err);
Miroslav Zagorac70230c62020-12-09 16:54:31 +010052
53 FLT_OT_RETURN();
54}
55
56
57#ifdef DEBUG_OT
58
59/***
60 * NAME
61 * flt_ot_cli_parse_debug -
62 *
63 * ARGUMENTS
64 * args -
65 * payload -
66 * appctx -
67 * private -
68 *
69 * DESCRIPTION
70 * -
71 *
72 * RETURN VALUE
73 * -
74 */
75static int flt_ot_cli_parse_debug(char **args, char *payload, struct appctx *appctx, void *private)
76{
77 char *err = NULL, *msg = NULL;
78 uint8_t value;
79 int retval = 0;
80
81 FLT_OT_FUNC("%p, \"%s\", %p, %p", args, payload, appctx, private);
82
83 FLT_OT_ARGS_DUMP();
84
85 if (FLT_OT_ARG_ISVALID(2)) {
86 value = flt_ot_strtoll(args[2], 0, 255, &err);
87 if (err == NULL) {
88 _HA_ATOMIC_STORE(&(flt_ot_debug.level), value);
89
90 (void)memprintf(&msg, FLT_OT_CLI_CMD " : debug level set to %hhu", value);
91 } else {
92 retval = 1;
93 }
94 } else {
95 value = _HA_ATOMIC_LOAD(&(flt_ot_debug.level));
96
97 (void)memprintf(&msg, FLT_OT_CLI_CMD " : current debug level is %hhu", value);
98 }
99
100 cmn_cli_set_msg(appctx, err, msg, CLI_ST_PRINT_FREE);
101
Miroslav Zagoracca09e012022-03-04 09:56:00 +0100102 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100103}
104
105#endif /* DEBUG_OT */
106
107
108/***
109 * NAME
110 * flt_ot_cli_parse_disabled -
111 *
112 * ARGUMENTS
113 * args -
114 * payload -
115 * appctx -
116 * private -
117 *
118 * DESCRIPTION
119 * -
120 *
121 * RETURN VALUE
122 * -
123 */
124static int flt_ot_cli_parse_disabled(char **args, char *payload, struct appctx *appctx, void *private)
125{
126 char *msg = NULL;
127 bool value = (uintptr_t)private;
128 int retval = 0;
129
130 FLT_OT_FUNC("%p, \"%s\", %p, %p", args, payload, appctx, private);
131
132 FLT_OT_ARGS_DUMP();
133
134 FLT_OT_PROXIES_LIST_START() {
135 _HA_ATOMIC_STORE(&(conf->tracer->flag_disabled), value);
136
137 (void)memprintf(&msg, "%s%s" FLT_OT_CLI_CMD " : filter %sabled", FLT_OT_CLI_MSG_CAT(msg), value ? "dis" : "en");
138 } FLT_OT_PROXIES_LIST_END();
139
140 cmn_cli_set_msg(appctx, NULL, msg, CLI_ST_PRINT_FREE);
141
Miroslav Zagoracca09e012022-03-04 09:56:00 +0100142 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100143}
144
145
146/***
147 * NAME
148 * flt_ot_cli_parse_option -
149 *
150 * ARGUMENTS
151 * args -
152 * payload -
153 * appctx -
154 * private -
155 *
156 * DESCRIPTION
157 * -
158 *
159 * RETURN VALUE
160 * -
161 */
162static int flt_ot_cli_parse_option(char **args, char *payload, struct appctx *appctx, void *private)
163{
164 char *msg = NULL;
165 bool value = (uintptr_t)private;
166 int retval = 0;
167
168 FLT_OT_FUNC("%p, \"%s\", %p, %p", args, payload, appctx, private);
169
170 FLT_OT_ARGS_DUMP();
171
172 FLT_OT_PROXIES_LIST_START() {
173 _HA_ATOMIC_STORE(&(conf->tracer->flag_harderr), value);
174
175 (void)memprintf(&msg, "%s%s" FLT_OT_CLI_CMD " : filter set %s-errors", FLT_OT_CLI_MSG_CAT(msg), value ? "hard" : "soft");
176 } FLT_OT_PROXIES_LIST_END();
177
178 cmn_cli_set_msg(appctx, NULL, msg, CLI_ST_PRINT_FREE);
179
Miroslav Zagoracca09e012022-03-04 09:56:00 +0100180 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100181}
182
183
184/***
185 * NAME
186 * flt_ot_cli_parse_logging -
187 *
188 * ARGUMENTS
189 * args -
190 * payload -
191 * appctx -
192 * private -
193 *
194 * DESCRIPTION
195 * -
196 *
197 * RETURN VALUE
198 * -
199 */
200static int flt_ot_cli_parse_logging(char **args, char *payload, struct appctx *appctx, void *private)
201{
202 char *err = NULL, *msg = NULL;
203 uint8_t value;
204 int retval = 0;
205
206 FLT_OT_FUNC("%p, \"%s\", %p, %p", args, payload, appctx, private);
207
208 FLT_OT_ARGS_DUMP();
209
210 if (FLT_OT_ARG_ISVALID(2)) {
211 if (strcasecmp(args[2], FLT_OT_CLI_LOGGING_OFF) == 0) {
212 value = FLT_OT_LOGGING_OFF;
213 }
214 else if (strcasecmp(args[2], FLT_OT_CLI_LOGGING_ON) == 0) {
215 value = FLT_OT_LOGGING_ON;
216 }
217 else if (strcasecmp(args[2], FLT_OT_CLI_LOGGING_NOLOGNORM) == 0) {
218 value = FLT_OT_LOGGING_ON | FLT_OT_LOGGING_NOLOGNORM;
219 }
220 else {
221 (void)memprintf(&err, "'%s' : invalid value, use <" FLT_OT_CLI_LOGGING_OFF " | " FLT_OT_CLI_LOGGING_ON " | " FLT_OT_CLI_LOGGING_NOLOGNORM ">", args[2]);
222
223 retval = 1;
224 }
225
226 if (retval == 0) {
227 FLT_OT_PROXIES_LIST_START() {
228 _HA_ATOMIC_STORE(&(conf->tracer->logging), value);
229
230 (void)memprintf(&msg, "%s%s" FLT_OT_CLI_CMD " : logging is %s", FLT_OT_CLI_MSG_CAT(msg), FLT_OT_CLI_LOGGING_STATE(value));
231 } FLT_OT_PROXIES_LIST_END();
232 }
233 } else {
234 FLT_OT_PROXIES_LIST_START() {
235 value = _HA_ATOMIC_LOAD(&(conf->tracer->logging));
236
237 (void)memprintf(&msg, "%s%s" FLT_OT_CLI_CMD " : logging is currently %s", FLT_OT_CLI_MSG_CAT(msg), FLT_OT_CLI_LOGGING_STATE(value));
238 } FLT_OT_PROXIES_LIST_END();
239 }
240
241 cmn_cli_set_msg(appctx, err, msg, CLI_ST_PRINT_FREE);
242
Miroslav Zagoracca09e012022-03-04 09:56:00 +0100243 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100244}
245
246
247/***
248 * NAME
249 * flt_ot_cli_parse_rate -
250 *
251 * ARGUMENTS
252 * args -
253 * payload -
254 * appctx -
255 * private -
256 *
257 * DESCRIPTION
258 * -
259 *
260 * RETURN VALUE
261 * -
262 */
263static int flt_ot_cli_parse_rate(char **args, char *payload, struct appctx *appctx, void *private)
264{
265 char *err = NULL, *msg = NULL;
266 uint32_t value;
267 int retval = 0;
268
269 FLT_OT_FUNC("%p, \"%s\", %p, %p", args, payload, appctx, private);
270
271 FLT_OT_ARGS_DUMP();
272
273 if (FLT_OT_ARG_ISVALID(2)) {
274 value = FLT_OT_FLOAT_U32(flt_ot_strtod(args[2], 0.0, FLT_OT_RATE_LIMIT_MAX, &err), FLT_OT_RATE_LIMIT_MAX);
275 if (err == NULL) {
276 FLT_OT_PROXIES_LIST_START() {
277 _HA_ATOMIC_STORE(&(conf->tracer->rate_limit), value);
278
279 (void)memprintf(&msg, "%s%s" FLT_OT_CLI_CMD " : rate limit set to %.2f", FLT_OT_CLI_MSG_CAT(msg), FLT_OT_U32_FLOAT(value, FLT_OT_RATE_LIMIT_MAX));
280 } FLT_OT_PROXIES_LIST_END();
281 } else {
282 retval = 1;
283 }
284 } else {
285 FLT_OT_PROXIES_LIST_START() {
286 value = _HA_ATOMIC_LOAD(&(conf->tracer->rate_limit));
287
288 (void)memprintf(&msg, "%s%s" FLT_OT_CLI_CMD " : current rate limit is %.2f", FLT_OT_CLI_MSG_CAT(msg), FLT_OT_U32_FLOAT(value, FLT_OT_RATE_LIMIT_MAX));
289 } FLT_OT_PROXIES_LIST_END();
290 }
291
292 cmn_cli_set_msg(appctx, err, msg, CLI_ST_PRINT_FREE);
293
Miroslav Zagoracca09e012022-03-04 09:56:00 +0100294 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100295}
296
297
298/***
299 * NAME
300 * flt_ot_cli_parse_status -
301 *
302 * ARGUMENTS
303 * args -
304 * payload -
305 * appctx -
306 * private -
307 *
308 * DESCRIPTION
309 * -
310 *
311 * RETURN VALUE
312 * -
313 */
314static int flt_ot_cli_parse_status(char **args, char *payload, struct appctx *appctx, void *private)
315{
316 const char *nl = "";
317 char *msg = NULL;
318 int retval = 0;
319
320 FLT_OT_FUNC("%p, \"%s\", %p, %p", args, payload, appctx, private);
321
322 FLT_OT_ARGS_DUMP();
323 flt_ot_filters_dump();
324
325 (void)memprintf(&msg, " " FLT_OT_OPT_NAME " filter status\n" FLT_OT_STR_DASH_78);
326#ifdef DEBUG_OT
327 (void)memprintf(&msg, "%s\n debug level: 0x%02hhx\n", msg, flt_ot_debug.level);
328#endif
329
330 FLT_OT_PROXIES_LIST_START() {
331 (void)memprintf(&msg, "%s\n%s filter %s\n", msg, nl, conf->id);
332 (void)memprintf(&msg, "%s configuration: %s\n", msg, conf->cfg_file);
333 (void)memprintf(&msg, "%s disable count: %" PRIu64 " %" PRIu64 "\n\n", msg, conf->cnt.disabled[0], conf->cnt.disabled[1]);
334 (void)memprintf(&msg, "%s tracer %s\n", msg, conf->tracer->id);
335 (void)memprintf(&msg, "%s configuration: %s\n", msg, conf->tracer->config);
336 (void)memprintf(&msg, "%s plugin: %s\n", msg, conf->tracer->plugin);
337 (void)memprintf(&msg, "%s rate limit: %.2f %%\n", msg, FLT_OT_U32_FLOAT(conf->tracer->rate_limit, FLT_OT_RATE_LIMIT_MAX));
338 (void)memprintf(&msg, "%s hard errors: %s\n", msg, FLT_OT_STR_FLAG_YN(conf->tracer->flag_harderr));
339 (void)memprintf(&msg, "%s disabled: %s\n", msg, FLT_OT_STR_FLAG_YN(conf->tracer->flag_disabled));
340 (void)memprintf(&msg, "%s logging: %s\n", msg, FLT_OT_CLI_LOGGING_STATE(conf->tracer->logging));
341 (void)memprintf(&msg, "%s analyzers: %08x", msg, conf->tracer->analyzers);
342
343 nl = "\n";
344 } FLT_OT_PROXIES_LIST_END();
345
346 cmn_cli_set_msg(appctx, NULL, msg, CLI_ST_PRINT_FREE);
347
Miroslav Zagoracca09e012022-03-04 09:56:00 +0100348 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100349}
350
351
352static struct cli_kw_list cli_kws = { { }, {
353#ifdef DEBUG_OT
354 { { FLT_OT_CLI_CMD, "debug", NULL }, FLT_OT_CLI_CMD " debug [level] : set the OT filter debug level (default: get current debug level)", flt_ot_cli_parse_debug, NULL, NULL, NULL, 0 },
355#endif
356 { { FLT_OT_CLI_CMD, "disable", NULL }, FLT_OT_CLI_CMD " disable : disable the OT filter", flt_ot_cli_parse_disabled, NULL, NULL, (void *)1, 0 },
357 { { FLT_OT_CLI_CMD, "enable", NULL }, FLT_OT_CLI_CMD " enable : enable the OT filter", flt_ot_cli_parse_disabled, NULL, NULL, (void *)0, 0 },
358 { { FLT_OT_CLI_CMD, "soft-errors", NULL }, FLT_OT_CLI_CMD " soft-errors : turning off hard-errors mode", flt_ot_cli_parse_option, NULL, NULL, (void *)0, 0 },
359 { { FLT_OT_CLI_CMD, "hard-errors", NULL }, FLT_OT_CLI_CMD " hard-errors : enabling hard-errors mode", flt_ot_cli_parse_option, NULL, NULL, (void *)1, 0 },
360 { { FLT_OT_CLI_CMD, "logging", NULL }, FLT_OT_CLI_CMD " logging [state] : set logging state (default: get current logging state)", flt_ot_cli_parse_logging, NULL, NULL, NULL, 0 },
361 { { FLT_OT_CLI_CMD, "rate", NULL }, FLT_OT_CLI_CMD " rate [value] : set the rate limit (default: get current rate value)", flt_ot_cli_parse_rate, NULL, NULL, NULL, 0 },
362 { { FLT_OT_CLI_CMD, "status", NULL }, FLT_OT_CLI_CMD " status : show the OT filter status", flt_ot_cli_parse_status, NULL, NULL, NULL, 0 },
363 { /* END */ }
364}};
365
366
367/***
368 * NAME
369 * flt_ot_cli_init -
370 *
371 * ARGUMENTS
372 * This function takes no arguments.
373 *
374 * DESCRIPTION
375 * -
376 *
377 * RETURN VALUE
378 * This function does not return a value.
379 */
380void flt_ot_cli_init(void)
381{
382 FLT_OT_FUNC("");
383
384 /* Register CLI keywords. */
385 cli_register_kw(&cli_kws);
386
387 FLT_OT_RETURN();
388}
389
390/*
391 * Local variables:
392 * c-indent-level: 8
393 * c-basic-offset: 8
394 * End:
395 *
396 * vi: noexpandtab shiftwidth=8 tabstop=8
397 */