blob: 1fd7007ff85e9c1e6b9bf02fe2b141f4d2303446 [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#ifdef DEBUG_OT
24struct flt_ot_debug flt_ot_debug;
25THREAD_LOCAL int dbg_indent_level = 0;
26#endif
27
28#ifdef OTC_DBG_MEM
29static struct otc_dbg_mem_data dbg_mem_data[1000000];
30static struct otc_dbg_mem dbg_mem;
31#endif
32
33static struct flt_ot_conf *flt_ot_current_config = NULL;
34static struct flt_ot_conf_tracer *flt_ot_current_tracer = NULL;
35static struct flt_ot_conf_group *flt_ot_current_group = NULL;
36static struct flt_ot_conf_scope *flt_ot_current_scope = NULL;
37static struct flt_ot_conf_span *flt_ot_current_span = NULL;
38
39
40/***
41 * NAME
42 * flt_ot_parse_strdup -
43 *
44 * ARGUMENTS
45 * ptr -
46 * str -
47 * err -
48 * err_msg -
49 *
50 * DESCRIPTION
51 * -
52 *
53 * RETURN VALUE
54 * Returns ERR_NONE (== 0) in case of success,
55 * or a combination of ERR_* flags if an error is encountered.
56 */
57static int flt_ot_parse_strdup(char **ptr, const char *str, char **err, const char *err_msg)
58{
59 int retval = ERR_NONE;
60
61 FLT_OT_FUNC("%p:%p, %p, %p:%p, \"%s\"", FLT_OT_DPTR_ARGS(ptr), str, FLT_OT_DPTR_ARGS(err), err_msg);
62
63 *ptr = FLT_OT_STRDUP(str);
64 if (*ptr == NULL) {
65 FLT_OT_PARSE_ERR(err, "'%s' : out of memory", err_msg);
66
67 retval |= ERR_ABORT | ERR_ALERT;
68 }
69
Miroslav Zagoracca09e012022-03-04 09:56:00 +010070 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +010071}
72
73
74/***
75 * NAME
76 * flt_ot_parse_keyword -
77 *
78 * ARGUMENTS
79 * ptr -
80 * args -
81 * cur_arg -
82 * pos -
83 * err -
84 * err_msg -
85 *
86 * DESCRIPTION
87 * -
88 *
89 * RETURN VALUE
90 * Returns ERR_NONE (== 0) in case of success,
91 * or a combination of ERR_* flags if an error is encountered.
92 */
93static int flt_ot_parse_keyword(char **ptr, char **args, int cur_arg, int pos, char **err, const char *err_msg)
94{
95 int retval = ERR_NONE;
96
97 FLT_OT_FUNC("%p:%p, %p, %d, %d, %p:%p, \"%s\"", FLT_OT_DPTR_ARGS(ptr), args, cur_arg, pos, FLT_OT_DPTR_ARGS(err), err_msg);
98
99 if (*ptr != NULL) {
100 if (cur_arg == pos)
101 FLT_OT_PARSE_ERR(err, FLT_OT_FMT_TYPE "%s already set", err_msg);
102 else
103 FLT_OT_PARSE_ERR(err, "'%s' : %s already set", args[cur_arg], err_msg);
104 }
105 else if (!FLT_OT_ARG_ISVALID(pos + 1)) {
106 if (cur_arg == pos)
107 FLT_OT_PARSE_ERR(err, FLT_OT_FMT_TYPE "no %s set", err_msg);
108 else
109 FLT_OT_PARSE_ERR(err, "'%s' : no %s set", args[cur_arg], err_msg);
110 }
111 else {
112 retval = flt_ot_parse_strdup(ptr, args[pos + 1], err, args[cur_arg]);
113 }
114
Miroslav Zagoracca09e012022-03-04 09:56:00 +0100115 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100116}
117
118
119/***
120 * NAME
121 * flt_ot_parse_invalid_char -
122 *
123 * ARGUMENTS
124 * name -
125 * type -
126 *
127 * DESCRIPTION
128 * -
129 *
130 * RETURN VALUE
131 * -
132 */
133static const char *flt_ot_parse_invalid_char(const char *name, int type)
134{
135 const char *retptr = NULL;
136
137 FLT_OT_FUNC("\"%s\", %d", name, type);
138
139 if (!FLT_OT_STR_ISVALID(name))
Miroslav Zagoracca09e012022-03-04 09:56:00 +0100140 FLT_OT_RETURN_EX(retptr, const char *, "%p");
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100141
Miroslav Zagorac854384f2022-03-07 13:14:16 +0100142 if (type == FLT_OT_PARSE_INVALID_CHAR) {
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100143 retptr = invalid_char(name);
144 }
Miroslav Zagorac854384f2022-03-07 13:14:16 +0100145 else if (type == FLT_OT_PARSE_INVALID_DOM) {
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100146 retptr = invalid_domainchar(name);
147 }
Miroslav Zagorac854384f2022-03-07 13:14:16 +0100148 else if (type == FLT_OT_PARSE_INVALID_CTX) {
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100149 retptr = invalid_prefix_char(name);
150 }
Miroslav Zagorac854384f2022-03-07 13:14:16 +0100151 else if (type == FLT_OT_PARSE_INVALID_VAR) {
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100152 retptr = name;
153
154 /*
155 * Allowed characters are letters, numbers and '_', the first
156 * character in the string must not be a number.
157 */
158 if (!isdigit(*retptr))
159 for (++retptr; (*retptr == '_') || isalnum(*retptr); retptr++);
160
161 if (*retptr == '\0')
162 retptr = NULL;
163 }
164
Miroslav Zagoracca09e012022-03-04 09:56:00 +0100165 FLT_OT_RETURN_EX(retptr, const char *, "%p");
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100166}
167
168
169/***
170 * NAME
171 * flt_ot_parse_cfg_check -
172 *
173 * ARGUMENTS
174 * file -
175 * linenum -
176 * args -
177 * id -
178 * parse_data -
179 * parse_data_size -
180 * pdata -
181 * err -
182 *
183 * DESCRIPTION
184 * -
185 *
186 * RETURN VALUE
187 * Returns ERR_NONE (== 0) in case of success,
188 * or a combination of ERR_* flags if an error is encountered.
189 */
190static int flt_ot_parse_cfg_check(const char *file, int linenum, char **args, const void *id, const struct flt_ot_parse_data *parse_data, size_t parse_data_size, const struct flt_ot_parse_data **pdata, char **err)
191{
Miroslav Zagorac4b3eb0a2021-04-14 11:44:58 +0200192 int i, argc, retval = ERR_NONE;
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100193
194 FLT_OT_FUNC("\"%s\", %d, %p, %p, %p, %zu, %p:%p, %p:%p", file, linenum, args, id, parse_data, parse_data_size, FLT_OT_DPTR_ARGS(pdata), FLT_OT_DPTR_ARGS(err));
195
196 FLT_OT_ARGS_DUMP();
197
198 *pdata = NULL;
199
Miroslav Zagorac4b3eb0a2021-04-14 11:44:58 +0200200 /* First check here if args[0] is the correct keyword. */
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100201 for (i = 0; (*pdata == NULL) && (i < parse_data_size); i++)
202 if (strcmp(parse_data[i].name, args[0]) == 0)
203 *pdata = parse_data + i;
204
205 if (*pdata == NULL)
206 FLT_OT_PARSE_ERR(err, "'%s' : unknown keyword", args[0]);
Miroslav Zagorac4b3eb0a2021-04-14 11:44:58 +0200207 else
208 argc = flt_ot_args_count(args);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100209
210 if ((retval & ERR_CODE) || (id == NULL))
211 /* Do nothing. */;
212 else if ((id != flt_ot_current_tracer) && (flt_ot_current_config->tracer == NULL))
213 FLT_OT_PARSE_ERR(err, "tracer not defined");
214
215 /*
216 * Checking that fewer arguments are specified in the configuration
217 * line than is required.
218 */
219 if (!(retval & ERR_CODE))
Miroslav Zagorac4b3eb0a2021-04-14 11:44:58 +0200220 if (argc < (*pdata)->args_min)
221 FLT_OT_PARSE_ERR(err, "'%s' : too few arguments (use '%s%s')", args[0], (*pdata)->name, (*pdata)->usage);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100222
223 /*
224 * Checking that more arguments are specified in the configuration
225 * line than the maximum allowed.
226 */
Miroslav Zagorac4b3eb0a2021-04-14 11:44:58 +0200227 if (!(retval & ERR_CODE) && ((*pdata)->args_max > 0))
228 if (argc > (*pdata)->args_max)
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100229 FLT_OT_PARSE_ERR(err, "'%s' : too many arguments (use '%s%s')", args[0], (*pdata)->name, (*pdata)->usage);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100230
231 /* Checking that the first argument has only allowed characters. */
Miroslav Zagorac854384f2022-03-07 13:14:16 +0100232 if (!(retval & ERR_CODE) && ((*pdata)->check_name != FLT_OT_PARSE_INVALID_NONE)) {
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100233 const char *ic;
234
235 ic = flt_ot_parse_invalid_char(args[1], (*pdata)->check_name);
236 if (ic != NULL)
237 FLT_OT_PARSE_ERR(err, "%s '%s' : invalid character '%c'", args[0], args[1], *ic);
238 }
239
240 /* Checking that the data group name is defined. */
241 if (!(retval & ERR_CODE) && (*pdata)->flag_check_id && (id == NULL))
242 FLT_OT_PARSE_ERR(err, "'%s' : %s ID not set (use '%s%s')", args[0], parse_data[1].name, parse_data[1].name, parse_data[1].usage);
243
Miroslav Zagoracca09e012022-03-04 09:56:00 +0100244 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100245}
246
247
248/***
249 * NAME
250 * flt_ot_parse_cfg_sample_expr -
251 *
252 * ARGUMENTS
253 * file -
254 * linenum -
255 * args -
256 * idx -
257 * head -
258 * err -
259 *
260 * DESCRIPTION
261 * -
262 *
263 * RETURN VALUE
264 * Returns ERR_NONE (== 0) in case of success,
265 * or a combination of ERR_* flags if an error is encountered.
266 */
267static int flt_ot_parse_cfg_sample_expr(const char *file, int linenum, char **args, int *idx, struct list *head, char **err)
268{
269 struct flt_ot_conf_sample_expr *expr;
270 int retval = ERR_NONE;
271
272 FLT_OT_FUNC("\"%s\", %d, %p, %p, %p, %p:%p", file, linenum, args, idx, head, FLT_OT_DPTR_ARGS(err));
273
274 expr = flt_ot_conf_sample_expr_init(args[*idx], linenum, head, err);
275 if (expr != NULL) {
276 expr->expr = sample_parse_expr(args, idx, file, linenum, err, &(flt_ot_current_config->proxy->conf.args), NULL);
277 if (expr->expr != NULL)
278 FLT_OT_DBG(3, "sample expression '%s' added", expr->value);
279 else
280 retval |= ERR_ABORT | ERR_ALERT;
281 } else {
282 retval |= ERR_ABORT | ERR_ALERT;
283 }
284
285 if (retval & ERR_CODE)
286 flt_ot_conf_sample_expr_free(&expr);
287
Miroslav Zagoracca09e012022-03-04 09:56:00 +0100288 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100289}
290
291
292/***
293 * NAME
294 * flt_ot_parse_cfg_sample -
295 *
296 * ARGUMENTS
297 * file -
298 * linenum -
299 * args -
300 * head -
301 * err -
302 *
303 * DESCRIPTION
304 * -
305 *
306 * RETURN VALUE
307 * Returns ERR_NONE (== 0) in case of success,
308 * or a combination of ERR_* flags if an error is encountered.
309 */
310static int flt_ot_parse_cfg_sample(const char *file, int linenum, char **args, struct list *head, char **err)
311{
312 struct flt_ot_conf_sample *sample;
313 int idx = 2, retval = ERR_NONE;
314
315 FLT_OT_FUNC("\"%s\", %d, %p, %p, %p:%p", file, linenum, args, head, FLT_OT_DPTR_ARGS(err));
316
317 sample = flt_ot_conf_sample_init(args, linenum, head, err);
318 if (sample == NULL)
319 FLT_OT_PARSE_ERR(err, "'%s' : out of memory", args[0]);
320
321 if (!(retval & ERR_CODE)) {
322 flt_ot_current_config->proxy->conf.args.ctx = ARGC_OT;
323 flt_ot_current_config->proxy->conf.args.file = file;
324 flt_ot_current_config->proxy->conf.args.line = linenum;
325
326 while (!(retval & ERR_CODE) && FLT_OT_ARG_ISVALID(idx))
327 retval = flt_ot_parse_cfg_sample_expr(file, linenum, args, &idx, &(sample->exprs), err);
328
329 flt_ot_current_config->proxy->conf.args.file = NULL;
330 flt_ot_current_config->proxy->conf.args.line = 0;
331 }
332
333 if (retval & ERR_CODE)
334 flt_ot_conf_sample_free(&sample);
335 else
336 FLT_OT_DBG(3, "sample '%s' -> '%s' added", sample->key, sample->value);
337
Miroslav Zagoracca09e012022-03-04 09:56:00 +0100338 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100339}
340
341
342/***
343 * NAME
344 * flt_ot_parse_cfg_str -
345 *
346 * ARGUMENTS
347 * file -
348 * linenum -
349 * args -
350 * head -
351 * err -
352 *
353 * DESCRIPTION
354 * -
355 *
356 * RETURN VALUE
357 * Returns ERR_NONE (== 0) in case of success,
358 * or a combination of ERR_* flags if an error is encountered.
359 */
360static int flt_ot_parse_cfg_str(const char *file, int linenum, char **args, struct list *head, char **err)
361{
362 struct flt_ot_conf_str *str = NULL;
363 int i, retval = ERR_NONE;
364
365 FLT_OT_FUNC("\"%s\", %d, %p, %p, %p:%p", file, linenum, args, head, FLT_OT_DPTR_ARGS(err));
366
367 for (i = 1; !(retval & ERR_CODE) && FLT_OT_ARG_ISVALID(i); i++)
368 if (flt_ot_conf_str_init(args[i], linenum, head, err) == NULL)
369 retval |= ERR_ABORT | ERR_ALERT;
370
371 if (retval & ERR_CODE)
372 flt_ot_conf_str_free(&str);
373
Miroslav Zagoracca09e012022-03-04 09:56:00 +0100374 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100375}
376
377
378/***
379 * NAME
380 * flt_ot_parse_cfg_file -
381 *
382 * ARGUMENTS
383 * ptr -
384 * file -
385 * linenum -
386 * args -
387 * err -
388 * err_msg -
389 *
390 * DESCRIPTION
391 * -
392 *
393 * RETURN VALUE
394 * Returns ERR_NONE (== 0) in case of success,
395 * or a combination of ERR_* flags if an error is encountered.
396 */
397static int flt_ot_parse_cfg_file(char **ptr, const char *file, int linenum, char **args, char **err, const char *err_msg)
398{
399 int retval = ERR_NONE;
400
401 FLT_OT_FUNC("%p:%p, \"%s\", %d, %p, %p:%p, \"%s\"", FLT_OT_DPTR_ARGS(ptr), file, linenum, args, FLT_OT_DPTR_ARGS(err), err_msg);
402
403 if (!FLT_OT_ARG_ISVALID(1))
404 FLT_OT_PARSE_ERR(err, "'%s' : no %s specified", flt_ot_current_tracer->id, err_msg);
405 else if (alertif_too_many_args(1, file, linenum, args, &retval))
406 retval |= ERR_ABORT | ERR_ALERT;
407 else if (access(args[1], R_OK) == -1)
408 FLT_OT_PARSE_ERR(err, "'%s' : %s", args[1], strerror(errno));
409 else
410 retval = flt_ot_parse_keyword(ptr, args, 0, 0, err, err_msg);
411
Miroslav Zagoracca09e012022-03-04 09:56:00 +0100412 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100413}
414
415
416/***
417 * NAME
418 * flt_ot_parse_check_scope -
419 *
420 * ARGUMENTS
421 * This function takes no arguments.
422 *
423 * DESCRIPTION
424 * -
425 *
426 * RETURN VALUE
427 * Returns TRUE in case the configuration is not in the currently defined
428 * scope, FALSE otherwise.
429 */
430static bool flt_ot_parse_check_scope(void)
431{
432 bool retval = 0;
433
434 if ((cfg_scope != NULL) && (flt_ot_current_config->id != NULL) && (strcmp(flt_ot_current_config->id, cfg_scope) != 0)) {
435 FLT_OT_DBG(1, "cfg_scope: '%s', id: '%s'", cfg_scope, flt_ot_current_config->id);
436
437 retval = 1;
438 }
439
440 return retval;
441}
442
443
444/***
445 * NAME
446 * flt_ot_parse_cfg_tracer -
447 *
448 * ARGUMENTS
449 * file -
450 * linenum -
451 * args -
452 * kw_mod -
453 *
454 * DESCRIPTION
455 * -
456 *
457 * RETURN VALUE
458 * Returns ERR_NONE (== 0) in case of success,
459 * or a combination of ERR_* flags if an error is encountered.
460 */
461static int flt_ot_parse_cfg_tracer(const char *file, int linenum, char **args, int kw_mod)
462{
Miroslav Zagorac854384f2022-03-07 13:14:16 +0100463#define FLT_OT_PARSE_TRACER_DEF(a,b,c,d,e,f,g) { FLT_OT_PARSE_TRACER_##a, b, FLT_OT_PARSE_INVALID_##c, d, e, f, g },
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100464 static const struct flt_ot_parse_data parse_data[] = { FLT_OT_PARSE_TRACER_DEFINES };
465#undef FLT_OT_PARSE_TRACER_DEF
466 const struct flt_ot_parse_data *pdata = NULL;
467 char *err = NULL, *err_log = NULL;
468 int i, retval = ERR_NONE;
469
470 FLT_OT_FUNC("\"%s\", %d, %p, 0x%08x", file, linenum, args, kw_mod);
471
472 if (flt_ot_parse_check_scope())
Miroslav Zagoracca09e012022-03-04 09:56:00 +0100473 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100474
475 retval = flt_ot_parse_cfg_check(file, linenum, args, flt_ot_current_tracer, parse_data, FLT_OT_TABLESIZE(parse_data), &pdata, &err);
476 if (retval & ERR_CODE) {
477 FLT_OT_PARSE_IFERR_ALERT();
478
Miroslav Zagoracca09e012022-03-04 09:56:00 +0100479 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100480 }
481
482 if (pdata->keyword == FLT_OT_PARSE_TRACER_ID) {
483 if (flt_ot_current_config->tracer != NULL) {
484 FLT_OT_PARSE_ERR(&err, "'%s' : tracer can be defined only once", args[1]);
485 } else {
486 flt_ot_current_tracer = flt_ot_conf_tracer_init(args[1], linenum, &err);
487 if (flt_ot_current_tracer == NULL)
488 retval |= ERR_ABORT | ERR_ALERT;
489 }
490 }
491 else if (pdata->keyword == FLT_OT_PARSE_TRACER_LOG) {
Miroslav Zagorac98272252021-04-07 11:14:23 +0200492 if (parse_logsrv(args, &(flt_ot_current_tracer->proxy_log.logsrvs), kw_mod == KWM_NO, file, linenum, &err_log) == 0) {
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100493 FLT_OT_PARSE_ERR(&err, "'%s %s ...' : %s", args[0], args[1], err_log);
494 FLT_OT_FREE_CLEAR(err_log);
495
496 retval |= ERR_ABORT | ERR_ALERT;
497 } else {
498 flt_ot_current_tracer->logging |= FLT_OT_LOGGING_ON;
499 }
500 }
501 else if (pdata->keyword == FLT_OT_PARSE_TRACER_CONFIG) {
502 retval = flt_ot_parse_cfg_file(&(flt_ot_current_tracer->config), file, linenum, args, &err, "configuration file");
503 }
504 else if (pdata->keyword == FLT_OT_PARSE_TRACER_PLUGIN) {
505 retval = flt_ot_parse_cfg_file(&(flt_ot_current_tracer->plugin), file, linenum, args, &err, "plugin library");
506 }
507 else if (pdata->keyword == FLT_OT_PARSE_TRACER_GROUPS) {
508 for (i = 1; !(retval & ERR_CODE) && FLT_OT_ARG_ISVALID(i); i++)
509 if (flt_ot_conf_ph_init(args[i], linenum, &(flt_ot_current_tracer->ph_groups), &err) == NULL)
510 retval |= ERR_ABORT | ERR_ALERT;
511 }
512 else if (pdata->keyword == FLT_OT_PARSE_TRACER_SCOPES) {
513 for (i = 1; !(retval & ERR_CODE) && FLT_OT_ARG_ISVALID(i); i++)
514 if (flt_ot_conf_ph_init(args[i], linenum, &(flt_ot_current_tracer->ph_scopes), &err) == NULL)
515 retval |= ERR_ABORT | ERR_ALERT;
516 }
517 else if (pdata->keyword == FLT_OT_PARSE_TRACER_ACL) {
518 if (strcasecmp(args[1], "or") == 0)
519 FLT_OT_PARSE_ERR(&err, "'%s %s ...' : invalid ACL name", args[0], args[1]);
520 else if (parse_acl((const char **)args + 1, &(flt_ot_current_tracer->acls), &err, &(flt_ot_current_config->proxy->conf.args), file, linenum) == NULL)
521 retval |= ERR_ABORT | ERR_ALERT;
522 }
523 else if (pdata->keyword == FLT_OT_PARSE_TRACER_RATE_LIMIT) {
524 flt_ot_current_tracer->rate_limit = FLT_OT_FLOAT_U32(flt_ot_strtod(args[1], 0.0, FLT_OT_RATE_LIMIT_MAX, &err), FLT_OT_RATE_LIMIT_MAX);
525 }
526 else if (pdata->keyword == FLT_OT_PARSE_TRACER_OPTION) {
527 if (strcmp(args[1], FLT_OT_PARSE_OPTION_DISABLED) == 0) {
528 flt_ot_current_tracer->flag_disabled = (kw_mod == KWM_NO) ? 0 : 1;
529 }
530 else if (strcmp(args[1], FLT_OT_PARSE_OPTION_HARDERR) == 0) {
531 flt_ot_current_tracer->flag_harderr = (kw_mod == KWM_NO) ? 0 : 1;
532 }
533 else if (strcmp(args[1], FLT_OT_PARSE_OPTION_NOLOGNORM) == 0) {
534 if (kw_mod == KWM_NO)
535 flt_ot_current_tracer->logging &= ~FLT_OT_LOGGING_NOLOGNORM;
536 else
537 flt_ot_current_tracer->logging |= FLT_OT_LOGGING_NOLOGNORM;
538 }
539 else
540 FLT_OT_PARSE_ERR(&err, "'%s' : unknown option '%s'", args[0], args[1]);
541 }
542#ifdef DEBUG_OT
543 else if (pdata->keyword == FLT_OT_PARSE_TRACER_DEBUG_LEVEL) {
544 flt_ot_debug.level = flt_ot_strtoll(args[1], 0, 255, &err);
545 }
546#else
547 else {
548 FLT_OT_PARSE_WARNING("'%s' : keyword ignored", file, linenum, args[0]);
549 }
550#endif
551
552 FLT_OT_PARSE_IFERR_ALERT();
553
554 if ((retval & ERR_CODE) && (flt_ot_current_tracer != NULL))
555 flt_ot_conf_tracer_free(&flt_ot_current_tracer);
556
Miroslav Zagoracca09e012022-03-04 09:56:00 +0100557 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100558}
559
560
561/***
562 * NAME
563 * flt_ot_post_parse_cfg_tracer -
564 *
565 * ARGUMENTS
566 * This function takes no arguments.
567 *
568 * DESCRIPTION
569 * -
570 *
571 * RETURN VALUE
572 * Returns ERR_NONE (== 0) in case of success,
573 * or a combination of ERR_* flags if an error is encountered.
574 */
575static int flt_ot_post_parse_cfg_tracer(void)
576{
Miroslav Zagorac9425ed42021-06-10 01:23:15 +0200577 char errbuf[BUFSIZ] = "";
578 int retval = ERR_NONE;
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100579
580 FLT_OT_FUNC("");
581
582 if (flt_ot_current_tracer == NULL)
Miroslav Zagoracca09e012022-03-04 09:56:00 +0100583 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100584
585 flt_ot_current_config->tracer = flt_ot_current_tracer;
586
587 if (flt_ot_current_tracer->id == NULL)
Miroslav Zagoracca09e012022-03-04 09:56:00 +0100588 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100589
Miroslav Zagorac9425ed42021-06-10 01:23:15 +0200590 if (flt_ot_current_tracer->config == NULL) {
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100591 FLT_OT_POST_PARSE_ALERT("tracer '%s' has no configuration file specified", flt_ot_current_tracer->cfg_line, flt_ot_current_tracer->id);
Miroslav Zagorac9425ed42021-06-10 01:23:15 +0200592 } else {
593 flt_ot_current_tracer->cfgbuf = otc_file_read(flt_ot_current_tracer->config, "#", errbuf, sizeof(errbuf));
594 if (flt_ot_current_tracer->cfgbuf == NULL)
595 FLT_OT_POST_PARSE_ALERT("tracer '%s' %s", flt_ot_current_tracer->cfg_line, flt_ot_current_tracer->id, (*errbuf == '\0') ? "cannot load configuration file" : errbuf);
596 }
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100597
598 if (flt_ot_current_tracer->plugin == NULL)
599 FLT_OT_POST_PARSE_ALERT("tracer '%s' has no plugin library specified", flt_ot_current_tracer->cfg_line, flt_ot_current_tracer->id);
600
601 flt_ot_current_tracer = NULL;
602
Miroslav Zagoracca09e012022-03-04 09:56:00 +0100603 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100604}
605
606
607/***
608 * NAME
609 * flt_ot_parse_cfg_group -
610 *
611 * ARGUMENTS
612 * file -
613 * linenum -
614 * args -
615 * kw_mod -
616 *
617 * DESCRIPTION
618 * -
619 *
620 * RETURN VALUE
621 * Returns ERR_NONE (== 0) in case of success,
622 * or a combination of ERR_* flags if an error is encountered.
623 */
624static int flt_ot_parse_cfg_group(const char *file, int linenum, char **args, int kw_mod)
625{
Miroslav Zagorac854384f2022-03-07 13:14:16 +0100626#define FLT_OT_PARSE_GROUP_DEF(a,b,c,d,e,f,g) { FLT_OT_PARSE_GROUP_##a, b, FLT_OT_PARSE_INVALID_##c, d, e, f, g },
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100627 static const struct flt_ot_parse_data parse_data[] = { FLT_OT_PARSE_GROUP_DEFINES };
628#undef FLT_OT_PARSE_GROUP_DEF
629 const struct flt_ot_parse_data *pdata = NULL;
630 char *err = NULL;
631 int i, retval = ERR_NONE;
632
633 FLT_OT_FUNC("\"%s\", %d, %p, 0x%08x", file, linenum, args, kw_mod);
634
635 if (flt_ot_parse_check_scope())
Miroslav Zagoracca09e012022-03-04 09:56:00 +0100636 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100637
638 retval = flt_ot_parse_cfg_check(file, linenum, args, flt_ot_current_group, parse_data, FLT_OT_TABLESIZE(parse_data), &pdata, &err);
639 if (retval & ERR_CODE) {
640 FLT_OT_PARSE_IFERR_ALERT();
641
Miroslav Zagoracca09e012022-03-04 09:56:00 +0100642 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100643 }
644
645 if (pdata->keyword == FLT_OT_PARSE_GROUP_ID) {
646 flt_ot_current_group = flt_ot_conf_group_init(args[1], linenum, &(flt_ot_current_config->groups), &err);
647 if (flt_ot_current_config == NULL)
648 retval |= ERR_ABORT | ERR_ALERT;
649 }
650 else if (pdata->keyword == FLT_OT_PARSE_GROUP_SCOPES) {
651 for (i = 1; !(retval & ERR_CODE) && FLT_OT_ARG_ISVALID(i); i++)
652 if (flt_ot_conf_ph_init(args[i], linenum, &(flt_ot_current_group->ph_scopes), &err) == NULL)
653 retval |= ERR_ABORT | ERR_ALERT;
654 }
655
656 FLT_OT_PARSE_IFERR_ALERT();
657
658 if ((retval & ERR_CODE) && (flt_ot_current_group != NULL))
659 flt_ot_conf_group_free(&flt_ot_current_group);
660
Miroslav Zagoracca09e012022-03-04 09:56:00 +0100661 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100662}
663
664
665/***
666 * NAME
667 * flt_ot_post_parse_cfg_group -
668 *
669 * ARGUMENTS
670 * This function takes no arguments.
671 *
672 * DESCRIPTION
673 * -
674 *
675 * RETURN VALUE
676 * Returns ERR_NONE (== 0) in case of success,
677 * or a combination of ERR_* flags if an error is encountered.
678 */
679static int flt_ot_post_parse_cfg_group(void)
680{
681 int retval = ERR_NONE;
682
683 FLT_OT_FUNC("");
684
685 if (flt_ot_current_group == NULL)
Miroslav Zagoracca09e012022-03-04 09:56:00 +0100686 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100687
688 /* Check that the group has at least one scope defined. */
689 if (LIST_ISEMPTY(&(flt_ot_current_group->ph_scopes)))
690 FLT_OT_POST_PARSE_ALERT("group '%s' has no defined scope(s)", flt_ot_current_group->cfg_line, flt_ot_current_group->id);
691
692 flt_ot_current_group = NULL;
693
Miroslav Zagoracca09e012022-03-04 09:56:00 +0100694 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100695}
696
697
698/***
699 * NAME
700 * flt_ot_parse_cfg_scope_ctx -
701 *
702 * ARGUMENTS
703 * args -
704 * cur_arg -
705 * err -
706 *
707 * DESCRIPTION
708 * -
709 *
710 * RETURN VALUE
711 * Returns ERR_NONE (== 0) in case of success,
712 * or a combination of ERR_* flags if an error is encountered.
713 */
714static int flt_ot_parse_cfg_scope_ctx(char **args, int cur_arg, char **err)
715{
716 uint8_t flags = 0;
717 int retval = ERR_NONE;
718
719 FLT_OT_FUNC("%p, %d, %p:%p", args, cur_arg, FLT_OT_DPTR_ARGS(err));
720
721 if (strcmp(args[cur_arg], FLT_OT_PARSE_CTX_USE_HEADERS) == 0)
722 flags = FLT_OT_CTX_USE_HEADERS;
Miroslav Zagorac4cb2c832021-09-08 20:56:05 +0200723#ifdef USE_OT_VARS
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100724 else if (strcmp(args[cur_arg], FLT_OT_PARSE_CTX_USE_VARS) == 0)
725 flags = FLT_OT_CTX_USE_VARS;
Miroslav Zagorac4cb2c832021-09-08 20:56:05 +0200726#endif
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100727 else
728 FLT_OT_PARSE_ERR(err, "'%s' : invalid context storage type", args[0]);
729
730 if (flags == 0)
731 /* Do nothing. */;
732 else if (flt_ot_current_span->ctx_flags & flags)
733 FLT_OT_PARSE_ERR(err, "'%s' : %s already used", args[0], args[cur_arg]);
734 else
735 flt_ot_current_span->ctx_flags |= flags;
736
737 FLT_OT_DBG(2, "ctx_flags: 0x%02hhx (0x%02hhx)", flt_ot_current_span->ctx_flags, flags);
738
Miroslav Zagoracca09e012022-03-04 09:56:00 +0100739 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100740}
741
742
743/***
744 * NAME
745 * flt_ot_parse_acl -
746 *
747 * ARGUMENTS
748 * file -
749 * linenum -
750 * px -
751 * args -
752 * err -
753 * head -
754 *
755 * DESCRIPTION
756 * -
757 *
758 * RETURN VALUE
759 * -
760 */
761static struct acl_cond *flt_ot_parse_acl(const char *file, int linenum, struct proxy *px, const char **args, char **err, struct list *head, ...)
762{
763 va_list ap;
764 int n = 0;
765 struct acl_cond *retptr = NULL;
766
767 FLT_OT_FUNC("\"%s\", %d, %p, %p, %p:%p, %p, ...", file, linenum, px, args, FLT_OT_DPTR_ARGS(err), head);
768
769 for (va_start(ap, head); (retptr == NULL) && (head != NULL); head = va_arg(ap, typeof(head)), n++) {
770 retptr = build_acl_cond(file, linenum, head, px, args, (n == 0) ? err : NULL);
771 if (retptr != NULL)
772 FLT_OT_DBG(2, "ACL build done, using list %p %d", head, n);
773 }
774 va_end(ap);
775
776 if ((retptr != NULL) && (err != NULL))
777 FLT_OT_FREE_CLEAR(*err);
778
Miroslav Zagoracca09e012022-03-04 09:56:00 +0100779 FLT_OT_RETURN_PTR(retptr);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100780}
781
782
783/***
784 * NAME
785 * flt_ot_parse_cfg_scope -
786 *
787 * ARGUMENTS
788 * file -
789 * linenum -
790 * args -
791 * kw_mod -
792 *
793 * DESCRIPTION
794 * Function used to load the scope block configuration.
795 *
796 * RETURN VALUE
797 * Returns ERR_NONE (== 0) in case of success,
798 * or a combination of ERR_* flags if an error is encountered.
799 */
800static int flt_ot_parse_cfg_scope(const char *file, int linenum, char **args, int kw_mod)
801{
Miroslav Zagorac854384f2022-03-07 13:14:16 +0100802#define FLT_OT_PARSE_SCOPE_DEF(a,b,c,d,e,f,g) { FLT_OT_PARSE_SCOPE_##a, b, FLT_OT_PARSE_INVALID_##c, d, e, f, g },
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100803 static const struct flt_ot_parse_data parse_data[] = { FLT_OT_PARSE_SCOPE_DEFINES };
804#undef FLT_OT_PARSE_SCOPE_DEF
805 const struct flt_ot_parse_data *pdata = NULL;
806 char *err = NULL;
807 int i, retval = ERR_NONE;
808
809 FLT_OT_FUNC("\"%s\", %d, %p, 0x%08x", file, linenum, args, kw_mod);
810
811 if (flt_ot_parse_check_scope())
Miroslav Zagoracca09e012022-03-04 09:56:00 +0100812 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100813
814 retval = flt_ot_parse_cfg_check(file, linenum, args, flt_ot_current_span, parse_data, FLT_OT_TABLESIZE(parse_data), &pdata, &err);
815 if (retval & ERR_CODE) {
816 FLT_OT_PARSE_IFERR_ALERT();
817
Miroslav Zagoracca09e012022-03-04 09:56:00 +0100818 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100819 }
820
821 if (pdata->keyword == FLT_OT_PARSE_SCOPE_ID) {
822 /* Initialization of a new scope. */
823 flt_ot_current_scope = flt_ot_conf_scope_init(args[1], linenum, &(flt_ot_current_config->scopes), &err);
824 if (flt_ot_current_scope == NULL)
825 retval |= ERR_ABORT | ERR_ALERT;
826 }
827 else if (pdata->keyword == FLT_OT_PARSE_SCOPE_SPAN) {
828 /*
829 * Checking if this is the beginning of the definition of
830 * a new span.
831 */
832 if (flt_ot_current_span != NULL) {
833 FLT_OT_DBG(3, "span '%s' (done)", flt_ot_current_span->id);
834
835 flt_ot_current_span = NULL;
836 }
837
838 /* Initialization of a new span. */
839 flt_ot_current_span = flt_ot_conf_span_init(args[1], linenum, &(flt_ot_current_scope->spans), &err);
840
841 /*
842 * In case the span has a defined reference,
843 * the correctness of the arguments is checked here.
844 */
845 if (flt_ot_current_span == NULL) {
846 retval |= ERR_ABORT | ERR_ALERT;
847 }
848 else if (FLT_OT_ARG_ISVALID(2)) {
849 for (i = 2; (i < pdata->args_max) && FLT_OT_ARG_ISVALID(i); i++)
850 if (strcmp(args[i], FLT_OT_PARSE_SPAN_ROOT) == 0) {
851 if (flt_ot_current_span->flag_root)
852 FLT_OT_PARSE_ERR(&err, "'%s' : already set (use '%s%s')", args[i], pdata->name, pdata->usage);
853 else
854 flt_ot_current_span->flag_root = 1;
855 }
856 else if ((strcmp(args[i], FLT_OT_PARSE_SPAN_REF_CHILD) == 0) || (strcmp(args[i], FLT_OT_PARSE_SPAN_REF_FOLLOWS) == 0)) {
857 if (!FLT_OT_ARG_ISVALID(i + 1)) {
858 FLT_OT_PARSE_ERR(&err, "'%s' : too few arguments (use '%s%s')", args[i], pdata->name, pdata->usage);
859 }
860 else if (strcmp(args[i++], FLT_OT_PARSE_SPAN_REF_CHILD) == 0) {
861 flt_ot_current_span->ref_type = otc_span_reference_child_of;
862 flt_ot_current_span->ref_id_len = strlen(args[i]);
863
864 retval = flt_ot_parse_strdup(&(flt_ot_current_span->ref_id), args[i], &err, args[1]);
865 }
866 else {
867 flt_ot_current_span->ref_type = otc_span_reference_follows_from;
868 flt_ot_current_span->ref_id_len = strlen(args[i]);
869
870 retval = flt_ot_parse_strdup(&(flt_ot_current_span->ref_id), args[i], &err, args[1]);
871 }
872 }
873 else {
874 FLT_OT_PARSE_ERR(&err, "'%s' : invalid argument (use '%s%s')", args[i], pdata->name, pdata->usage);
875 }
876 }
877 else {
878 /*
879 * This is not a faulty configuration, only such a case
880 * will be logged.
881 */
882 FLT_OT_DBG(3, "new span '%s' without reference", flt_ot_current_span->id);
883 }
884 }
885 else if (pdata->keyword == FLT_OT_PARSE_SCOPE_TAG) {
886 retval = flt_ot_parse_cfg_sample(file, linenum, args, &(flt_ot_current_span->tags), &err);
887 }
888 else if (pdata->keyword == FLT_OT_PARSE_SCOPE_LOG) {
889 retval = flt_ot_parse_cfg_sample(file, linenum, args, &(flt_ot_current_span->logs), &err);
890 }
891 else if (pdata->keyword == FLT_OT_PARSE_SCOPE_BAGGAGE) {
892 retval = flt_ot_parse_cfg_sample(file, linenum, args, &(flt_ot_current_span->baggages), &err);
893 }
894 else if (pdata->keyword == FLT_OT_PARSE_SCOPE_INJECT) {
895 /*
896 * Automatic context name generation can be specified here
897 * if the contents of the FLT_OT_PARSE_CTX_AUTONAME macro
898 * are used as the name. In that case, if the context is
899 * after a particular event, it gets its name; otherwise
900 * it gets the name of the current span.
901 */
902 if (flt_ot_current_span->ctx_id != NULL)
903 FLT_OT_PARSE_ERR(&err, "'%s' : only one context per span is allowed", args[1]);
904 else if (strcmp(args[1], FLT_OT_PARSE_CTX_AUTONAME) != 0)
905 retval = flt_ot_parse_strdup(&(flt_ot_current_span->ctx_id), args[1], &err, args[0]);
906 else if (flt_ot_current_scope->event != FLT_OT_EVENT_REQ_NONE)
907 retval = flt_ot_parse_strdup(&(flt_ot_current_span->ctx_id), flt_ot_event_data[flt_ot_current_scope->event].name, &err, args[0]);
908 else
909 retval = flt_ot_parse_strdup(&(flt_ot_current_span->ctx_id), flt_ot_current_span->id, &err, args[0]);
910
911 if (flt_ot_current_span->ctx_id != NULL) {
912 flt_ot_current_span->ctx_id_len = strlen(flt_ot_current_span->ctx_id);
913
914 /*
915 * Here is checked the context storage type; which, if
916 * not explicitly specified, is set to HTTP headers.
917 *
918 * It is possible to use both types of context storage
919 * at the same time.
920 */
921 if (FLT_OT_ARG_ISVALID(2)) {
922 retval = flt_ot_parse_cfg_scope_ctx(args, 2, &err);
923 if (!(retval & ERR_CODE) && FLT_OT_ARG_ISVALID(3))
924 retval = flt_ot_parse_cfg_scope_ctx(args, 3, &err);
925 } else {
926 flt_ot_current_span->ctx_flags = FLT_OT_CTX_USE_HEADERS;
927 }
928 }
929 }
930 else if (pdata->keyword == FLT_OT_PARSE_SCOPE_EXTRACT) {
931 struct flt_ot_conf_context *conf_ctx;
932
933 /*
934 * Here is checked the context storage type; which, if
935 * not explicitly specified, is set to HTTP headers.
936 */
937 conf_ctx = flt_ot_conf_context_init(args[1], linenum, &(flt_ot_current_scope->contexts), &err);
938 if (conf_ctx == NULL)
939 retval |= ERR_ABORT | ERR_ALERT;
940 else if (!FLT_OT_ARG_ISVALID(2))
941 conf_ctx->flags = FLT_OT_CTX_USE_HEADERS;
942 else if (strcmp(args[2], FLT_OT_PARSE_CTX_USE_HEADERS) == 0)
943 conf_ctx->flags = FLT_OT_CTX_USE_HEADERS;
Miroslav Zagorac4cb2c832021-09-08 20:56:05 +0200944#ifdef USE_OT_VARS
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100945 else if (strcmp(args[2], FLT_OT_PARSE_CTX_USE_VARS) == 0)
946 conf_ctx->flags = FLT_OT_CTX_USE_VARS;
Miroslav Zagorac4cb2c832021-09-08 20:56:05 +0200947#endif
Miroslav Zagorac70230c62020-12-09 16:54:31 +0100948 else
949 FLT_OT_PARSE_ERR(&err, "'%s' : invalid context storage type", args[2]);
950 }
951 else if (pdata->keyword == FLT_OT_PARSE_SCOPE_FINISH) {
952 retval = flt_ot_parse_cfg_str(file, linenum, args, &(flt_ot_current_scope->finish), &err);
953 }
954 else if (pdata->keyword == FLT_OT_PARSE_SCOPE_ACL) {
955 if (strcasecmp(args[1], "or") == 0)
956 FLT_OT_PARSE_ERR(&err, "'%s %s ...' : invalid ACL name", args[0], args[1]);
957 else if (parse_acl((const char **)args + 1, &(flt_ot_current_scope->acls), &err, &(flt_ot_current_config->proxy->conf.args), file, linenum) == NULL)
958 retval |= ERR_ABORT | ERR_ALERT;
959 }
960 else if (pdata->keyword == FLT_OT_PARSE_SCOPE_EVENT) {
961 /* Scope can only have one event defined. */
962 if (flt_ot_current_scope->event != FLT_OT_EVENT_REQ_NONE) {
963 FLT_OT_PARSE_ERR(&err, "'%s' : event already set", flt_ot_current_scope->id);
964 } else {
965 /* Check the event name. */
966 for (i = 0; i < FLT_OT_TABLESIZE(flt_ot_event_data); i++)
967 if (strcmp(flt_ot_event_data[i].name, args[1]) == 0) {
968 flt_ot_current_scope->event = i;
969
970 break;
971 }
972
973 /*
974 * The event can have some condition defined and this
975 * is checked here.
976 */
977 if (flt_ot_current_scope->event == FLT_OT_EVENT_REQ_NONE) {
978 FLT_OT_PARSE_ERR(&err, "'%s' : unknown event", args[1]);
979 }
980 else if (!FLT_OT_ARG_ISVALID(2)) {
981 /* Do nothing. */
982 }
983 else if ((strcmp(args[2], FLT_OT_CONDITION_IF) == 0) || (strcmp(args[2], FLT_OT_CONDITION_UNLESS) == 0)) {
984 /*
985 * We will first try to build ACL condition using
986 * local settings and then if that fails, using
987 * global settings (from tracer block). If it
988 * also fails, then try to use ACL defined in
989 * the HAProxy configuration.
990 */
991 flt_ot_current_scope->cond = flt_ot_parse_acl(file, linenum, flt_ot_current_config->proxy, (const char **)args + 2, &err, &(flt_ot_current_scope->acls), &(flt_ot_current_config->tracer->acls), &(flt_ot_current_config->proxy->acl), NULL);
992 if (flt_ot_current_scope->cond == NULL)
993 retval |= ERR_ABORT | ERR_ALERT;
994 }
995 else {
996 FLT_OT_PARSE_ERR(&err, "'%s' : expects either 'if' or 'unless' followed by a condition but found '%s'", args[1], args[2]);
997 }
998
999 if (!(retval & ERR_CODE))
1000 FLT_OT_DBG(3, "event '%s'", args[1]);
1001 }
1002 }
1003
1004 FLT_OT_PARSE_IFERR_ALERT();
1005
1006 if ((retval & ERR_CODE) && (flt_ot_current_scope != NULL)) {
1007 flt_ot_conf_scope_free(&flt_ot_current_scope);
1008
1009 flt_ot_current_span = NULL;
1010 }
1011
Miroslav Zagoracca09e012022-03-04 09:56:00 +01001012 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +01001013}
1014
1015
1016/***
1017 * NAME
1018 * flt_ot_post_parse_cfg_scope -
1019 *
1020 * ARGUMENTS
1021 * This function takes no arguments.
1022 *
1023 * DESCRIPTION
1024 * In this function the correctness of the complete scope block is examined.
1025 * This does not mean that all elements are checked here, but only those for
1026 * which it has not been possible to establish their complete correctness in
1027 * the function flt_ot_parse_cfg_scope().
1028 *
1029 * RETURN VALUE
1030 * Returns ERR_NONE (== 0) in case of success,
1031 * or a combination of ERR_* flags if an error is encountered.
1032 */
1033static int flt_ot_post_parse_cfg_scope(void)
1034{
1035 struct flt_ot_conf_span *conf_span;
1036 int retval = ERR_NONE;
1037
1038 FLT_OT_FUNC("");
1039
1040 if (flt_ot_current_scope == NULL)
Miroslav Zagoracca09e012022-03-04 09:56:00 +01001041 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +01001042
1043 /* If span context inject is used, check that this is possible. */
1044 list_for_each_entry(conf_span, &(flt_ot_current_scope->spans), list)
1045 if ((conf_span->ctx_id != NULL) && (conf_span->ctx_flags & FLT_OT_CTX_USE_HEADERS))
1046 if (!flt_ot_event_data[flt_ot_current_scope->event].flag_http_inject)
1047 FLT_OT_POST_PARSE_ALERT("inject '%s' : cannot use on this event", conf_span->cfg_line, conf_span->ctx_id);
1048
1049 if (retval & ERR_CODE)
1050 flt_ot_conf_scope_free(&flt_ot_current_scope);
1051
1052 flt_ot_current_scope = NULL;
1053 flt_ot_current_span = NULL;
1054
Miroslav Zagoracca09e012022-03-04 09:56:00 +01001055 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +01001056}
1057
1058
1059/***
1060 * NAME
1061 * flt_ot_parse_cfg -
1062 *
1063 * ARGUMENTS
1064 * conf -
1065 * flt_name -
1066 * err -
1067 *
1068 * DESCRIPTION
1069 * -
1070 *
1071 * RETURN VALUE
1072 * Returns ERR_NONE (== 0) in case of success,
1073 * or a combination of ERR_* flags if an error is encountered.
1074 */
1075static int flt_ot_parse_cfg(struct flt_ot_conf *conf, const char *flt_name, char **err)
1076{
1077 struct list backup_sections;
1078 int retval = ERR_ABORT | ERR_ALERT;
1079
1080 FLT_OT_FUNC("%p, \"%s\", %p:%p", conf, flt_name, FLT_OT_DPTR_ARGS(err));
1081
1082 flt_ot_current_config = conf;
1083
1084 /* Backup sections. */
1085 LIST_INIT(&backup_sections);
1086 cfg_backup_sections(&backup_sections);
1087
1088 /* Register new OT sections and parse the OT filter configuration file. */
1089 if (!cfg_register_section(FLT_OT_PARSE_SECTION_TRACER_ID, flt_ot_parse_cfg_tracer, flt_ot_post_parse_cfg_tracer))
1090 /* Do nothing. */;
1091 else if (!cfg_register_section(FLT_OT_PARSE_SECTION_GROUP_ID, flt_ot_parse_cfg_group, flt_ot_post_parse_cfg_group))
1092 /* Do nothing. */;
1093 else if (!cfg_register_section(FLT_OT_PARSE_SECTION_SCOPE_ID, flt_ot_parse_cfg_scope, flt_ot_post_parse_cfg_scope))
1094 /* Do nothing. */;
1095 else if (access(conf->cfg_file, R_OK) == -1)
1096 FLT_OT_PARSE_ERR(err, "'%s' : %s", conf->cfg_file, strerror(errno));
1097 else
1098 retval = readcfgfile(conf->cfg_file);
1099
1100 /* Unregister OT sections and restore previous sections. */
1101 cfg_unregister_sections();
1102 cfg_restore_sections(&backup_sections);
1103
1104 flt_ot_current_config = NULL;
1105
Miroslav Zagoracca09e012022-03-04 09:56:00 +01001106 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +01001107}
1108
1109
1110/***
1111 * NAME
1112 * flt_ot_parse -
1113 *
1114 * ARGUMENTS
1115 * args -
1116 * cur_arg -
1117 * px -
1118 * fconf -
1119 * err -
1120 * private -
1121 *
1122 * DESCRIPTION
1123 * -
1124 *
1125 * RETURN VALUE
1126 * Returns ERR_NONE (== 0) in case of success,
1127 * or a combination of ERR_* flags if an error is encountered.
1128 */
1129static int flt_ot_parse(char **args, int *cur_arg, struct proxy *px, struct flt_conf *fconf, char **err, void *private)
1130{
1131 struct flt_ot_conf *conf = NULL;
1132 int pos, retval = ERR_NONE;
1133
1134#ifdef DEBUG_OT
1135 FLT_OT_RUN_ONCE(
1136# ifndef DEBUG_OT_SYSTIME
1137 (void)memcpy(&(flt_ot_debug.start), &now, sizeof(flt_ot_debug.start));
1138# endif
1139
1140 flt_ot_debug.level = FLT_OT_DEBUG_LEVEL;
1141 );
1142#endif
1143
1144 FLT_OT_FUNC("%p, %p, %p, %p, %p:%p, %p", args, cur_arg, px, fconf, FLT_OT_DPTR_ARGS(err), private);
1145
1146#ifdef OTC_DBG_MEM
1147 FLT_OT_RUN_ONCE(
1148 if (otc_dbg_mem_init(&dbg_mem, dbg_mem_data, FLT_OT_TABLESIZE(dbg_mem_data), 0xff) == -1) {
1149 FLT_OT_PARSE_ERR(err, "cannot initialize memory debugger");
1150
Miroslav Zagoracca09e012022-03-04 09:56:00 +01001151 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +01001152 }
1153 );
1154#endif
1155
1156 FLT_OT_ARGS_DUMP();
1157
1158 conf = flt_ot_conf_init(px);
1159 if (conf == NULL) {
1160 FLT_OT_PARSE_ERR(err, "'%s' : out of memory", args[*cur_arg]);
1161
Miroslav Zagoracca09e012022-03-04 09:56:00 +01001162 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +01001163 }
1164
1165 for (pos = *cur_arg + 1; !(retval & ERR_CODE) && FLT_OT_ARG_ISVALID(pos); pos++) {
1166 FLT_OT_DBG(3, "args[%d:2] : { '%s' '%s' }", pos, args[pos], args[pos + 1]);
1167
1168 if (strcmp(args[pos], FLT_OT_OPT_FILTER_ID) == 0) {
1169 retval = flt_ot_parse_keyword(&(conf->id), args, *cur_arg, pos, err, "name");
1170 pos++;
1171 }
1172 else if (strcmp(args[pos], FLT_OT_OPT_CONFIG) == 0) {
1173 retval = flt_ot_parse_keyword(&(conf->cfg_file), args, *cur_arg, pos, err, "configuration file");
1174 if (!(retval & ERR_CODE))
1175 retval = flt_ot_parse_cfg(conf, args[*cur_arg], err);
1176 pos++;
1177 }
1178 else {
1179 FLT_OT_PARSE_ERR(err, "'%s' : unknown keyword '%s'", args[*cur_arg], args[pos]);
1180 }
1181 }
1182
1183 /* If the OpenTracing filter ID is not set, use default name. */
1184 if (!(retval & ERR_CODE) && (conf->id == NULL)) {
1185 ha_warning("parsing : " FLT_OT_FMT_TYPE FLT_OT_FMT_NAME "'no filter id set, using default id '%s'\n", FLT_OT_OPT_FILTER_ID_DEFAULT);
1186
1187 retval = flt_ot_parse_strdup(&(conf->id), FLT_OT_OPT_FILTER_ID_DEFAULT, err, args[*cur_arg]);
1188 }
1189
1190 if (!(retval & ERR_CODE) && (conf->cfg_file == NULL))
1191 FLT_OT_PARSE_ERR(err, "'%s' : no configuration file specified", args[*cur_arg]);
1192
1193 if (retval & ERR_CODE) {
1194 flt_ot_conf_free(&conf);
1195 } else {
1196 fconf->id = ot_flt_id;
1197 fconf->ops = &flt_ot_ops;
1198 fconf->conf = conf;
1199
1200 *cur_arg = pos;
1201
1202 FLT_OT_DBG(3, "filter set: id '%s', config '%s'", conf->id, conf->cfg_file);
1203 }
1204
Miroslav Zagoracca09e012022-03-04 09:56:00 +01001205 FLT_OT_RETURN_INT(retval);
Miroslav Zagorac70230c62020-12-09 16:54:31 +01001206}
1207
1208
1209/* Declare the filter parser for FLT_OT_OPT_NAME keyword. */
1210static struct flt_kw_list flt_kws = { FLT_OT_SCOPE, { }, {
1211 { FLT_OT_OPT_NAME, flt_ot_parse, NULL },
1212 { NULL, NULL, NULL },
1213 }
1214};
1215
1216INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws);
1217
1218/*
1219 * Local variables:
1220 * c-indent-level: 8
1221 * c-basic-offset: 8
1222 * End:
1223 *
1224 * vi: noexpandtab shiftwidth=8 tabstop=8
1225 */