Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Stream filters related variables and functions. |
| 3 | * |
| 4 | * Copyright (C) 2015 Qualys Inc., Christopher Faulet <cfaulet@qualys.com> |
| 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 |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Christopher Faulet | fcd99f8 | 2016-10-31 11:27:21 +0100 | [diff] [blame] | 13 | #include <ctype.h> |
| 14 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 15 | #include <common/hathreads.h> |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 16 | #include <common/htx.h> |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 17 | #include <common/initcall.h> |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 18 | #include <common/standard.h> |
| 19 | #include <common/time.h> |
| 20 | #include <common/tools.h> |
| 21 | |
| 22 | #include <types/channel.h> |
| 23 | #include <types/filters.h> |
| 24 | #include <types/global.h> |
| 25 | #include <types/proxy.h> |
| 26 | #include <types/stream.h> |
| 27 | |
| 28 | #include <proto/filters.h> |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 29 | #include <proto/http_htx.h> |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 30 | #include <proto/log.h> |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 31 | #include <proto/http_ana.h> |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 32 | #include <proto/stream.h> |
| 33 | |
Christopher Faulet | f4a4ef7 | 2018-12-07 17:39:53 +0100 | [diff] [blame] | 34 | const char *trace_flt_id = "trace filter"; |
| 35 | |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 36 | struct flt_ops trace_ops; |
| 37 | |
| 38 | struct trace_config { |
| 39 | struct proxy *proxy; |
| 40 | char *name; |
| 41 | int rand_parsing; |
| 42 | int rand_forwarding; |
Christopher Faulet | fcd99f8 | 2016-10-31 11:27:21 +0100 | [diff] [blame] | 43 | int hexdump; |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 44 | }; |
| 45 | |
Christopher Faulet | a3ed271 | 2019-11-04 11:35:42 +0100 | [diff] [blame] | 46 | #define FLT_TRACE(conf, fmt, ...) \ |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 47 | fprintf(stderr, "%d.%06d [%-20s] " fmt "\n", \ |
| 48 | (int)now.tv_sec, (int)now.tv_usec, (conf)->name, \ |
| 49 | ##__VA_ARGS__) |
| 50 | |
Christopher Faulet | a3ed271 | 2019-11-04 11:35:42 +0100 | [diff] [blame] | 51 | #define FLT_STRM_TRACE(conf, strm, fmt, ...) \ |
Christopher Faulet | fcd99f8 | 2016-10-31 11:27:21 +0100 | [diff] [blame] | 52 | fprintf(stderr, "%d.%06d [%-20s] [strm %p(%x) 0x%08x 0x%08x] " fmt "\n", \ |
| 53 | (int)now.tv_sec, (int)now.tv_usec, (conf)->name, \ |
| 54 | strm, (strm ? ((struct stream *)strm)->uniq_id : ~0U), \ |
| 55 | (strm ? strm->req.analysers : 0), (strm ? strm->res.analysers : 0), \ |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 56 | ##__VA_ARGS__) |
| 57 | |
| 58 | |
| 59 | static const char * |
| 60 | channel_label(const struct channel *chn) |
| 61 | { |
| 62 | return (chn->flags & CF_ISRESP) ? "RESPONSE" : "REQUEST"; |
| 63 | } |
| 64 | |
| 65 | static const char * |
| 66 | proxy_mode(const struct stream *s) |
| 67 | { |
| 68 | struct proxy *px = (s->flags & SF_BE_ASSIGNED ? s->be : strm_fe(s)); |
| 69 | |
Christopher Faulet | 386a0cd | 2019-07-15 21:22:44 +0200 | [diff] [blame] | 70 | return ((px->mode == PR_MODE_HTTP) ? "HTTP" : "TCP"); |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static const char * |
| 74 | stream_pos(const struct stream *s) |
| 75 | { |
| 76 | return (s->flags & SF_BE_ASSIGNED) ? "backend" : "frontend"; |
| 77 | } |
| 78 | |
Christopher Faulet | 31ed32d | 2016-06-21 11:42:37 +0200 | [diff] [blame] | 79 | static const char * |
| 80 | filter_type(const struct filter *f) |
| 81 | { |
| 82 | return (f->flags & FLT_FL_IS_BACKEND_FILTER) ? "backend" : "frontend"; |
| 83 | } |
| 84 | |
Christopher Faulet | fcd99f8 | 2016-10-31 11:27:21 +0100 | [diff] [blame] | 85 | static void |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 86 | trace_hexdump(struct ist ist) |
Christopher Faulet | fcd99f8 | 2016-10-31 11:27:21 +0100 | [diff] [blame] | 87 | { |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 88 | int i, j, padding; |
Christopher Faulet | fcd99f8 | 2016-10-31 11:27:21 +0100 | [diff] [blame] | 89 | |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 90 | padding = ((ist.len % 16) ? (16 - ist.len % 16) : 0); |
| 91 | for (i = 0; i < ist.len + padding; i++) { |
Christopher Faulet | fcd99f8 | 2016-10-31 11:27:21 +0100 | [diff] [blame] | 92 | if (!(i % 16)) |
| 93 | fprintf(stderr, "\t0x%06x: ", i); |
| 94 | else if (!(i % 8)) |
| 95 | fprintf(stderr, " "); |
| 96 | |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 97 | if (i < ist.len) |
| 98 | fprintf(stderr, "%02x ", (unsigned char)*(ist.ptr+i)); |
Christopher Faulet | fcd99f8 | 2016-10-31 11:27:21 +0100 | [diff] [blame] | 99 | else |
| 100 | fprintf(stderr, " "); |
| 101 | |
| 102 | /* print ASCII dump */ |
| 103 | if (i % 16 == 15) { |
| 104 | fprintf(stderr, " |"); |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 105 | for(j = i - 15; j <= i && j < ist.len; j++) |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 106 | fprintf(stderr, "%c", (isprint((unsigned char)*(ist.ptr+j)) ? *(ist.ptr+j) : '.')); |
Christopher Faulet | fcd99f8 | 2016-10-31 11:27:21 +0100 | [diff] [blame] | 107 | fprintf(stderr, "|\n"); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 112 | static void |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 113 | trace_raw_hexdump(struct buffer *buf, unsigned int offset, unsigned int len) |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 114 | { |
| 115 | unsigned char p[len]; |
| 116 | int block1, block2; |
| 117 | |
| 118 | block1 = len; |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 119 | if (block1 > b_contig_data(buf, offset)) |
| 120 | block1 = b_contig_data(buf, offset); |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 121 | block2 = len - block1; |
| 122 | |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 123 | memcpy(p, b_peek(buf, offset), block1); |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 124 | memcpy(p+block1, b_orig(buf), block2); |
| 125 | trace_hexdump(ist2(p, len)); |
| 126 | } |
| 127 | |
| 128 | static void |
| 129 | trace_htx_hexdump(struct htx *htx, unsigned int offset, unsigned int len) |
| 130 | { |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 131 | struct htx_blk *blk; |
| 132 | |
Christopher Faulet | ee847d4 | 2019-05-23 11:55:33 +0200 | [diff] [blame] | 133 | for (blk = htx_get_first_blk(htx); blk && len; blk = htx_get_next_blk(htx, blk)) { |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 134 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | ee847d4 | 2019-05-23 11:55:33 +0200 | [diff] [blame] | 135 | uint32_t sz = htx_get_blksz(blk); |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 136 | struct ist v; |
| 137 | |
Christopher Faulet | ee847d4 | 2019-05-23 11:55:33 +0200 | [diff] [blame] | 138 | if (offset >= sz) { |
| 139 | offset -= sz; |
| 140 | continue; |
| 141 | } |
| 142 | |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 143 | v = htx_get_blk_value(htx, blk); |
| 144 | v.ptr += offset; |
| 145 | v.len -= offset; |
| 146 | offset = 0; |
| 147 | |
| 148 | if (v.len > len) |
| 149 | v.len = len; |
| 150 | len -= v.len; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 151 | if (type == HTX_BLK_DATA) |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 152 | trace_hexdump(v); |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 156 | static unsigned int |
| 157 | trace_get_htx_datalen(struct htx *htx, unsigned int offset, unsigned int len) |
| 158 | { |
| 159 | struct htx_blk *blk; |
| 160 | uint32_t sz, data = 0; |
| 161 | |
| 162 | for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) { |
| 163 | if (htx_get_blk_type(blk) != HTX_BLK_DATA) |
| 164 | break; |
| 165 | |
| 166 | sz = htx_get_blksz(blk); |
| 167 | if (offset >= sz) { |
| 168 | offset -= sz; |
| 169 | continue; |
| 170 | } |
| 171 | data += sz - offset; |
| 172 | offset = 0; |
| 173 | if (data > len) { |
| 174 | data = len; |
| 175 | break; |
| 176 | } |
| 177 | } |
| 178 | return data; |
| 179 | } |
| 180 | |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 181 | /*************************************************************************** |
| 182 | * Hooks that manage the filter lifecycle (init/check/deinit) |
| 183 | **************************************************************************/ |
| 184 | /* Initialize the filter. Returns -1 on error, else 0. */ |
| 185 | static int |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 186 | trace_init(struct proxy *px, struct flt_conf *fconf) |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 187 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 188 | struct trace_config *conf = fconf->conf; |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 189 | |
| 190 | if (conf->name) |
| 191 | memprintf(&conf->name, "%s/%s", conf->name, px->id); |
| 192 | else |
| 193 | memprintf(&conf->name, "TRACE/%s", px->id); |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 194 | |
Christopher Faulet | 6e54095 | 2018-12-03 22:43:41 +0100 | [diff] [blame] | 195 | fconf->flags |= FLT_CFG_FL_HTX; |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 196 | fconf->conf = conf; |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 197 | |
Christopher Faulet | a3ed271 | 2019-11-04 11:35:42 +0100 | [diff] [blame] | 198 | FLT_TRACE(conf, "filter initialized [read random=%s - fwd random=%s - hexdump=%s]", |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 199 | (conf->rand_parsing ? "true" : "false"), |
Christopher Faulet | fcd99f8 | 2016-10-31 11:27:21 +0100 | [diff] [blame] | 200 | (conf->rand_forwarding ? "true" : "false"), |
| 201 | (conf->hexdump ? "true" : "false")); |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | /* Free ressources allocated by the trace filter. */ |
| 206 | static void |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 207 | trace_deinit(struct proxy *px, struct flt_conf *fconf) |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 208 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 209 | struct trace_config *conf = fconf->conf; |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 210 | |
| 211 | if (conf) { |
Christopher Faulet | a3ed271 | 2019-11-04 11:35:42 +0100 | [diff] [blame] | 212 | FLT_TRACE(conf, "filter deinitialized"); |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 213 | free(conf->name); |
| 214 | free(conf); |
| 215 | } |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 216 | fconf->conf = NULL; |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /* Check configuration of a trace filter for a specified proxy. |
| 220 | * Return 1 on error, else 0. */ |
| 221 | static int |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 222 | trace_check(struct proxy *px, struct flt_conf *fconf) |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 223 | { |
| 224 | return 0; |
| 225 | } |
| 226 | |
Christopher Faulet | f227372 | 2017-07-27 16:58:42 +0200 | [diff] [blame] | 227 | /* Initialize the filter for each thread. Return -1 on error, else 0. */ |
| 228 | static int |
| 229 | trace_init_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 230 | { |
| 231 | struct trace_config *conf = fconf->conf; |
| 232 | |
Christopher Faulet | a3ed271 | 2019-11-04 11:35:42 +0100 | [diff] [blame] | 233 | FLT_TRACE(conf, "filter initialized for thread tid %u", tid); |
Christopher Faulet | f227372 | 2017-07-27 16:58:42 +0200 | [diff] [blame] | 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | /* Free ressources allocate by the trace filter for each thread. */ |
| 238 | static void |
| 239 | trace_deinit_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 240 | { |
| 241 | struct trace_config *conf = fconf->conf; |
| 242 | |
| 243 | if (conf) |
Christopher Faulet | a3ed271 | 2019-11-04 11:35:42 +0100 | [diff] [blame] | 244 | FLT_TRACE(conf, "filter deinitialized for thread tid %u", tid); |
Christopher Faulet | f227372 | 2017-07-27 16:58:42 +0200 | [diff] [blame] | 245 | } |
| 246 | |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 247 | /************************************************************************** |
| 248 | * Hooks to handle start/stop of streams |
| 249 | *************************************************************************/ |
Christopher Faulet | 31ed32d | 2016-06-21 11:42:37 +0200 | [diff] [blame] | 250 | /* Called when a filter instance is created and attach to a stream */ |
| 251 | static int |
| 252 | trace_attach(struct stream *s, struct filter *filter) |
| 253 | { |
| 254 | struct trace_config *conf = FLT_CONF(filter); |
| 255 | |
Christopher Faulet | a3ed271 | 2019-11-04 11:35:42 +0100 | [diff] [blame] | 256 | FLT_STRM_TRACE(conf, s, "%-25s: filter-type=%s", |
Christopher Faulet | 31ed32d | 2016-06-21 11:42:37 +0200 | [diff] [blame] | 257 | __FUNCTION__, filter_type(filter)); |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 258 | |
Christopher Faulet | 31ed32d | 2016-06-21 11:42:37 +0200 | [diff] [blame] | 259 | return 1; |
| 260 | } |
| 261 | |
| 262 | /* Called when a filter instance is detach from a stream, just before its |
| 263 | * destruction */ |
| 264 | static void |
| 265 | trace_detach(struct stream *s, struct filter *filter) |
| 266 | { |
| 267 | struct trace_config *conf = FLT_CONF(filter); |
| 268 | |
Christopher Faulet | a3ed271 | 2019-11-04 11:35:42 +0100 | [diff] [blame] | 269 | FLT_STRM_TRACE(conf, s, "%-25s: filter-type=%s", |
Christopher Faulet | 31ed32d | 2016-06-21 11:42:37 +0200 | [diff] [blame] | 270 | __FUNCTION__, filter_type(filter)); |
| 271 | } |
| 272 | |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 273 | /* Called when a stream is created */ |
| 274 | static int |
| 275 | trace_stream_start(struct stream *s, struct filter *filter) |
| 276 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 277 | struct trace_config *conf = FLT_CONF(filter); |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 278 | |
Christopher Faulet | a3ed271 | 2019-11-04 11:35:42 +0100 | [diff] [blame] | 279 | FLT_STRM_TRACE(conf, s, "%-25s", |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 280 | __FUNCTION__); |
| 281 | return 0; |
| 282 | } |
| 283 | |
Christopher Faulet | 31ed32d | 2016-06-21 11:42:37 +0200 | [diff] [blame] | 284 | |
| 285 | /* Called when a backend is set for a stream */ |
| 286 | static int |
| 287 | trace_stream_set_backend(struct stream *s, struct filter *filter, |
| 288 | struct proxy *be) |
| 289 | { |
| 290 | struct trace_config *conf = FLT_CONF(filter); |
| 291 | |
Christopher Faulet | a3ed271 | 2019-11-04 11:35:42 +0100 | [diff] [blame] | 292 | FLT_STRM_TRACE(conf, s, "%-25s: backend=%s", |
Christopher Faulet | 31ed32d | 2016-06-21 11:42:37 +0200 | [diff] [blame] | 293 | __FUNCTION__, be->id); |
| 294 | return 0; |
| 295 | } |
| 296 | |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 297 | /* Called when a stream is destroyed */ |
| 298 | static void |
| 299 | trace_stream_stop(struct stream *s, struct filter *filter) |
| 300 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 301 | struct trace_config *conf = FLT_CONF(filter); |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 302 | |
Christopher Faulet | a3ed271 | 2019-11-04 11:35:42 +0100 | [diff] [blame] | 303 | FLT_STRM_TRACE(conf, s, "%-25s", |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 304 | __FUNCTION__); |
| 305 | } |
| 306 | |
Christopher Faulet | a00d817 | 2016-11-10 14:58:05 +0100 | [diff] [blame] | 307 | /* Called when the stream is woken up because of an expired timer */ |
| 308 | static void |
| 309 | trace_check_timeouts(struct stream *s, struct filter *filter) |
| 310 | { |
| 311 | struct trace_config *conf = FLT_CONF(filter); |
| 312 | |
Christopher Faulet | a3ed271 | 2019-11-04 11:35:42 +0100 | [diff] [blame] | 313 | FLT_STRM_TRACE(conf, s, "%-25s", |
Christopher Faulet | a00d817 | 2016-11-10 14:58:05 +0100 | [diff] [blame] | 314 | __FUNCTION__); |
| 315 | } |
| 316 | |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 317 | /************************************************************************** |
| 318 | * Hooks to handle channels activity |
| 319 | *************************************************************************/ |
| 320 | /* Called when analyze starts for a given channel */ |
| 321 | static int |
| 322 | trace_chn_start_analyze(struct stream *s, struct filter *filter, |
| 323 | struct channel *chn) |
| 324 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 325 | struct trace_config *conf = FLT_CONF(filter); |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 326 | |
Christopher Faulet | a3ed271 | 2019-11-04 11:35:42 +0100 | [diff] [blame] | 327 | FLT_STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s)", |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 328 | __FUNCTION__, |
| 329 | channel_label(chn), proxy_mode(s), stream_pos(s)); |
Christopher Faulet | 3a394fa | 2016-05-11 17:13:39 +0200 | [diff] [blame] | 330 | filter->pre_analyzers |= (AN_REQ_ALL | AN_RES_ALL); |
| 331 | filter->post_analyzers |= (AN_REQ_ALL | AN_RES_ALL); |
Christopher Faulet | fcd99f8 | 2016-10-31 11:27:21 +0100 | [diff] [blame] | 332 | register_data_filter(s, chn, filter); |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 333 | return 1; |
| 334 | } |
| 335 | |
| 336 | /* Called before a processing happens on a given channel */ |
| 337 | static int |
| 338 | trace_chn_analyze(struct stream *s, struct filter *filter, |
| 339 | struct channel *chn, unsigned an_bit) |
| 340 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 341 | struct trace_config *conf = FLT_CONF(filter); |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 342 | char *ana; |
| 343 | |
| 344 | switch (an_bit) { |
| 345 | case AN_REQ_INSPECT_FE: |
| 346 | ana = "AN_REQ_INSPECT_FE"; |
| 347 | break; |
| 348 | case AN_REQ_WAIT_HTTP: |
| 349 | ana = "AN_REQ_WAIT_HTTP"; |
| 350 | break; |
| 351 | case AN_REQ_HTTP_BODY: |
| 352 | ana = "AN_REQ_HTTP_BODY"; |
| 353 | break; |
| 354 | case AN_REQ_HTTP_PROCESS_FE: |
| 355 | ana = "AN_REQ_HTTP_PROCESS_FE"; |
| 356 | break; |
| 357 | case AN_REQ_SWITCHING_RULES: |
| 358 | ana = "AN_REQ_SWITCHING_RULES"; |
| 359 | break; |
| 360 | case AN_REQ_INSPECT_BE: |
| 361 | ana = "AN_REQ_INSPECT_BE"; |
| 362 | break; |
| 363 | case AN_REQ_HTTP_PROCESS_BE: |
| 364 | ana = "AN_REQ_HTTP_PROCESS_BE"; |
| 365 | break; |
| 366 | case AN_REQ_SRV_RULES: |
| 367 | ana = "AN_REQ_SRV_RULES"; |
| 368 | break; |
| 369 | case AN_REQ_HTTP_INNER: |
| 370 | ana = "AN_REQ_HTTP_INNER"; |
| 371 | break; |
| 372 | case AN_REQ_HTTP_TARPIT: |
| 373 | ana = "AN_REQ_HTTP_TARPIT"; |
| 374 | break; |
| 375 | case AN_REQ_STICKING_RULES: |
| 376 | ana = "AN_REQ_STICKING_RULES"; |
| 377 | break; |
| 378 | case AN_REQ_PRST_RDP_COOKIE: |
| 379 | ana = "AN_REQ_PRST_RDP_COOKIE"; |
| 380 | break; |
| 381 | case AN_REQ_HTTP_XFER_BODY: |
| 382 | ana = "AN_REQ_HTTP_XFER_BODY"; |
| 383 | break; |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 384 | case AN_RES_INSPECT: |
| 385 | ana = "AN_RES_INSPECT"; |
| 386 | break; |
| 387 | case AN_RES_WAIT_HTTP: |
| 388 | ana = "AN_RES_WAIT_HTTP"; |
| 389 | break; |
| 390 | case AN_RES_HTTP_PROCESS_FE: // AN_RES_HTTP_PROCESS_BE |
| 391 | ana = "AN_RES_HTTP_PROCESS_FE/BE"; |
| 392 | break; |
| 393 | case AN_RES_STORE_RULES: |
| 394 | ana = "AN_RES_STORE_RULES"; |
| 395 | break; |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 396 | case AN_RES_HTTP_XFER_BODY: |
| 397 | ana = "AN_RES_HTTP_XFER_BODY"; |
| 398 | break; |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 399 | default: |
| 400 | ana = "unknown"; |
| 401 | } |
| 402 | |
Christopher Faulet | a3ed271 | 2019-11-04 11:35:42 +0100 | [diff] [blame] | 403 | FLT_STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s) - " |
Christopher Faulet | 3a394fa | 2016-05-11 17:13:39 +0200 | [diff] [blame] | 404 | "analyzer=%s - step=%s", |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 405 | __FUNCTION__, |
| 406 | channel_label(chn), proxy_mode(s), stream_pos(s), |
Christopher Faulet | 3a394fa | 2016-05-11 17:13:39 +0200 | [diff] [blame] | 407 | ana, ((chn->analysers & an_bit) ? "PRE" : "POST")); |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 408 | return 1; |
| 409 | } |
| 410 | |
| 411 | /* Called when analyze ends for a given channel */ |
| 412 | static int |
| 413 | trace_chn_end_analyze(struct stream *s, struct filter *filter, |
| 414 | struct channel *chn) |
| 415 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 416 | struct trace_config *conf = FLT_CONF(filter); |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 417 | |
Christopher Faulet | a3ed271 | 2019-11-04 11:35:42 +0100 | [diff] [blame] | 418 | FLT_STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s)", |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 419 | __FUNCTION__, |
| 420 | channel_label(chn), proxy_mode(s), stream_pos(s)); |
| 421 | return 1; |
| 422 | } |
| 423 | |
| 424 | /************************************************************************** |
| 425 | * Hooks to filter HTTP messages |
| 426 | *************************************************************************/ |
| 427 | static int |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 428 | trace_http_headers(struct stream *s, struct filter *filter, |
| 429 | struct http_msg *msg) |
| 430 | { |
| 431 | struct trace_config *conf = FLT_CONF(filter); |
Christopher Faulet | 386a0cd | 2019-07-15 21:22:44 +0200 | [diff] [blame] | 432 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 433 | struct htx_sl *sl = http_get_stline(htx); |
| 434 | int32_t pos; |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 435 | |
Christopher Faulet | a3ed271 | 2019-11-04 11:35:42 +0100 | [diff] [blame] | 436 | FLT_STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s)\t%.*s %.*s %.*s", |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 437 | __FUNCTION__, |
Christopher Faulet | 386a0cd | 2019-07-15 21:22:44 +0200 | [diff] [blame] | 438 | channel_label(msg->chn), proxy_mode(s), stream_pos(s), |
| 439 | HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl), |
| 440 | HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl), |
| 441 | HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl)); |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 442 | |
Christopher Faulet | 386a0cd | 2019-07-15 21:22:44 +0200 | [diff] [blame] | 443 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 444 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 445 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 446 | struct ist n, v; |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 447 | |
Christopher Faulet | 386a0cd | 2019-07-15 21:22:44 +0200 | [diff] [blame] | 448 | if (type == HTX_BLK_EOH) |
| 449 | break; |
| 450 | if (type != HTX_BLK_HDR) |
| 451 | continue; |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 452 | |
Christopher Faulet | 386a0cd | 2019-07-15 21:22:44 +0200 | [diff] [blame] | 453 | n = htx_get_blk_name(htx, blk); |
| 454 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | a3ed271 | 2019-11-04 11:35:42 +0100 | [diff] [blame] | 455 | FLT_STRM_TRACE(conf, s, "\t%.*s: %.*s", |
Christopher Faulet | 386a0cd | 2019-07-15 21:22:44 +0200 | [diff] [blame] | 456 | (int)n.len, n.ptr, (int)v.len, v.ptr); |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 457 | } |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 458 | return 1; |
| 459 | } |
| 460 | |
| 461 | static int |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 462 | trace_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg, |
| 463 | unsigned int offset, unsigned int len) |
| 464 | { |
| 465 | struct trace_config *conf = FLT_CONF(filter); |
| 466 | int ret = len; |
| 467 | |
Christopher Faulet | 0bdeeaa | 2019-06-04 22:09:53 +0200 | [diff] [blame] | 468 | if (ret && conf->rand_forwarding) { |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 469 | unsigned int data = trace_get_htx_datalen(htxbuf(&msg->chn->buf), offset, len); |
Christopher Faulet | 0bdeeaa | 2019-06-04 22:09:53 +0200 | [diff] [blame] | 470 | |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 471 | if (data) { |
Christopher Faulet | 647fe1d | 2019-06-12 16:07:48 +0200 | [diff] [blame] | 472 | ret = random() % (ret+1); |
| 473 | if (!ret || ret >= data) |
| 474 | ret = len; |
| 475 | } |
Christopher Faulet | 0bdeeaa | 2019-06-04 22:09:53 +0200 | [diff] [blame] | 476 | } |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 477 | |
Christopher Faulet | a3ed271 | 2019-11-04 11:35:42 +0100 | [diff] [blame] | 478 | FLT_STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s) - " |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 479 | "offset=%u - len=%u - forward=%d", |
| 480 | __FUNCTION__, |
| 481 | channel_label(msg->chn), proxy_mode(s), stream_pos(s), |
| 482 | offset, len, ret); |
| 483 | |
| 484 | if (conf->hexdump) |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 485 | trace_htx_hexdump(htxbuf(&msg->chn->buf), offset, ret); |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 486 | |
| 487 | if (ret != len) |
| 488 | task_wakeup(s->task, TASK_WOKEN_MSG); |
| 489 | return ret; |
| 490 | } |
| 491 | |
| 492 | static int |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 493 | trace_http_end(struct stream *s, struct filter *filter, |
| 494 | struct http_msg *msg) |
| 495 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 496 | struct trace_config *conf = FLT_CONF(filter); |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 497 | |
Christopher Faulet | a3ed271 | 2019-11-04 11:35:42 +0100 | [diff] [blame] | 498 | FLT_STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s)", |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 499 | __FUNCTION__, |
| 500 | channel_label(msg->chn), proxy_mode(s), stream_pos(s)); |
| 501 | return 1; |
| 502 | } |
| 503 | |
| 504 | static void |
| 505 | trace_http_reset(struct stream *s, struct filter *filter, |
| 506 | struct http_msg *msg) |
| 507 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 508 | struct trace_config *conf = FLT_CONF(filter); |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 509 | |
Christopher Faulet | a3ed271 | 2019-11-04 11:35:42 +0100 | [diff] [blame] | 510 | FLT_STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s)", |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 511 | __FUNCTION__, |
| 512 | channel_label(msg->chn), proxy_mode(s), stream_pos(s)); |
| 513 | } |
| 514 | |
| 515 | static void |
| 516 | trace_http_reply(struct stream *s, struct filter *filter, short status, |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 517 | const struct buffer *msg) |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 518 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 519 | struct trace_config *conf = FLT_CONF(filter); |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 520 | |
Christopher Faulet | a3ed271 | 2019-11-04 11:35:42 +0100 | [diff] [blame] | 521 | FLT_STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s)", |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 522 | __FUNCTION__, "-", proxy_mode(s), stream_pos(s)); |
| 523 | } |
| 524 | |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 525 | /************************************************************************** |
| 526 | * Hooks to filter TCP data |
| 527 | *************************************************************************/ |
| 528 | static int |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 529 | trace_tcp_payload(struct stream *s, struct filter *filter, struct channel *chn, |
| 530 | unsigned int offset, unsigned int len) |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 531 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 532 | struct trace_config *conf = FLT_CONF(filter); |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 533 | int ret = len; |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 534 | |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 535 | if (s->flags & SF_HTX) { |
| 536 | if (ret && conf->rand_forwarding) { |
| 537 | unsigned int data = trace_get_htx_datalen(htxbuf(&chn->buf), offset, len); |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 538 | |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 539 | if (data) { |
| 540 | ret = random() % (ret+1); |
| 541 | if (!ret || ret >= data) |
| 542 | ret = len; |
| 543 | } |
| 544 | } |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 545 | |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 546 | FLT_STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s) - " |
| 547 | "offset=%u - len=%u - forward=%d", |
| 548 | __FUNCTION__, |
| 549 | channel_label(chn), proxy_mode(s), stream_pos(s), |
| 550 | offset, len, ret); |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 551 | |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 552 | if (conf->hexdump) |
| 553 | trace_htx_hexdump(htxbuf(&chn->buf), offset, ret); |
| 554 | } |
| 555 | else { |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 556 | |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 557 | if (ret && conf->rand_forwarding) |
| 558 | ret = random() % (ret+1); |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 559 | |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 560 | FLT_STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s) - " |
| 561 | "offset=%u - len=%u - forward=%d", |
| 562 | __FUNCTION__, |
| 563 | channel_label(chn), proxy_mode(s), stream_pos(s), |
| 564 | offset, len, ret); |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 565 | |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 566 | if (conf->hexdump) |
| 567 | trace_raw_hexdump(&chn->buf, offset, ret); |
Christopher Faulet | fcd99f8 | 2016-10-31 11:27:21 +0100 | [diff] [blame] | 568 | } |
| 569 | |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 570 | if (ret != len) |
| 571 | task_wakeup(s->task, TASK_WOKEN_MSG); |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 572 | return ret; |
| 573 | } |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 574 | /******************************************************************** |
| 575 | * Functions that manage the filter initialization |
| 576 | ********************************************************************/ |
| 577 | struct flt_ops trace_ops = { |
| 578 | /* Manage trace filter, called for each filter declaration */ |
Christopher Faulet | f227372 | 2017-07-27 16:58:42 +0200 | [diff] [blame] | 579 | .init = trace_init, |
| 580 | .deinit = trace_deinit, |
| 581 | .check = trace_check, |
| 582 | .init_per_thread = trace_init_per_thread, |
| 583 | .deinit_per_thread = trace_deinit_per_thread, |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 584 | |
| 585 | /* Handle start/stop of streams */ |
Christopher Faulet | 31ed32d | 2016-06-21 11:42:37 +0200 | [diff] [blame] | 586 | .attach = trace_attach, |
| 587 | .detach = trace_detach, |
| 588 | .stream_start = trace_stream_start, |
| 589 | .stream_set_backend = trace_stream_set_backend, |
| 590 | .stream_stop = trace_stream_stop, |
Christopher Faulet | a00d817 | 2016-11-10 14:58:05 +0100 | [diff] [blame] | 591 | .check_timeouts = trace_check_timeouts, |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 592 | |
| 593 | /* Handle channels activity */ |
| 594 | .channel_start_analyze = trace_chn_start_analyze, |
Christopher Faulet | 3a394fa | 2016-05-11 17:13:39 +0200 | [diff] [blame] | 595 | .channel_pre_analyze = trace_chn_analyze, |
| 596 | .channel_post_analyze = trace_chn_analyze, |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 597 | .channel_end_analyze = trace_chn_end_analyze, |
| 598 | |
| 599 | /* Filter HTTP requests and responses */ |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 600 | .http_headers = trace_http_headers, |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 601 | .http_payload = trace_http_payload, |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 602 | .http_end = trace_http_end, |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 603 | .http_reset = trace_http_reset, |
| 604 | .http_reply = trace_http_reply, |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 605 | |
| 606 | /* Filter TCP data */ |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 607 | .tcp_payload = trace_tcp_payload, |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 608 | }; |
| 609 | |
| 610 | /* Return -1 on error, else 0 */ |
| 611 | static int |
| 612 | parse_trace_flt(char **args, int *cur_arg, struct proxy *px, |
Thierry Fournier | 3610c39 | 2016-04-13 18:27:51 +0200 | [diff] [blame] | 613 | struct flt_conf *fconf, char **err, void *private) |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 614 | { |
| 615 | struct trace_config *conf; |
| 616 | int pos = *cur_arg; |
| 617 | |
| 618 | conf = calloc(1, sizeof(*conf)); |
| 619 | if (!conf) { |
| 620 | memprintf(err, "%s: out of memory", args[*cur_arg]); |
| 621 | return -1; |
| 622 | } |
| 623 | conf->proxy = px; |
| 624 | |
| 625 | if (!strcmp(args[pos], "trace")) { |
| 626 | pos++; |
| 627 | |
| 628 | while (*args[pos]) { |
| 629 | if (!strcmp(args[pos], "name")) { |
| 630 | if (!*args[pos + 1]) { |
| 631 | memprintf(err, "'%s' : '%s' option without value", |
| 632 | args[*cur_arg], args[pos]); |
| 633 | goto error; |
| 634 | } |
| 635 | conf->name = strdup(args[pos + 1]); |
| 636 | if (!conf->name) { |
| 637 | memprintf(err, "%s: out of memory", args[*cur_arg]); |
| 638 | goto error; |
| 639 | } |
| 640 | pos++; |
| 641 | } |
| 642 | else if (!strcmp(args[pos], "random-parsing")) |
| 643 | conf->rand_parsing = 1; |
| 644 | else if (!strcmp(args[pos], "random-forwarding")) |
| 645 | conf->rand_forwarding = 1; |
Christopher Faulet | fcd99f8 | 2016-10-31 11:27:21 +0100 | [diff] [blame] | 646 | else if (!strcmp(args[pos], "hexdump")) |
| 647 | conf->hexdump = 1; |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 648 | else |
| 649 | break; |
| 650 | pos++; |
| 651 | } |
| 652 | *cur_arg = pos; |
Christopher Faulet | f4a4ef7 | 2018-12-07 17:39:53 +0100 | [diff] [blame] | 653 | fconf->id = trace_flt_id; |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 654 | fconf->ops = &trace_ops; |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 655 | } |
| 656 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 657 | fconf->conf = conf; |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 658 | return 0; |
| 659 | |
| 660 | error: |
| 661 | if (conf->name) |
| 662 | free(conf->name); |
| 663 | free(conf); |
| 664 | return -1; |
| 665 | } |
| 666 | |
| 667 | /* Declare the filter parser for "trace" keyword */ |
| 668 | static struct flt_kw_list flt_kws = { "TRACE", { }, { |
Thierry Fournier | 3610c39 | 2016-04-13 18:27:51 +0200 | [diff] [blame] | 669 | { "trace", parse_trace_flt, NULL }, |
| 670 | { NULL, NULL, NULL }, |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 671 | } |
| 672 | }; |
| 673 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 674 | INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws); |