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