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); |
Tim Duesterhus | 154374c | 2021-03-02 18:57:27 +0100 | [diff] [blame] | 146 | v = istadv(v, offset); |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 147 | offset = 0; |
| 148 | |
| 149 | if (v.len > len) |
| 150 | v.len = len; |
| 151 | len -= v.len; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 152 | if (type == HTX_BLK_DATA) |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 153 | trace_hexdump(v); |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 157 | static unsigned int |
| 158 | trace_get_htx_datalen(struct htx *htx, unsigned int offset, unsigned int len) |
| 159 | { |
| 160 | struct htx_blk *blk; |
Christopher Faulet | 24598a4 | 2020-02-26 22:06:11 +0100 | [diff] [blame] | 161 | struct htx_ret htxret = htx_find_offset(htx, offset); |
| 162 | uint32_t data = 0; |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 163 | |
Christopher Faulet | 24598a4 | 2020-02-26 22:06:11 +0100 | [diff] [blame] | 164 | blk = htxret.blk; |
| 165 | if (blk && htxret.ret && htx_get_blk_type(blk) == HTX_BLK_DATA) { |
| 166 | data += htxret.ret; |
| 167 | blk = htx_get_next_blk(htx, blk); |
| 168 | } |
| 169 | while (blk) { |
| 170 | if (htx_get_blk_type(blk) == HTX_BLK_UNUSED) |
| 171 | goto next; |
| 172 | else if (htx_get_blk_type(blk) != HTX_BLK_DATA) |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 173 | break; |
Christopher Faulet | 24598a4 | 2020-02-26 22:06:11 +0100 | [diff] [blame] | 174 | data += htx_get_blksz(blk); |
| 175 | next: |
| 176 | blk = htx_get_next_blk(htx, blk); |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 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 | 96a577a | 2020-11-17 10:45:05 +0100 | [diff] [blame] | 198 | FLT_TRACE(conf, "filter initialized [quiet=%s - fwd random=%s - hexdump=%s]", |
Christopher Faulet | 47d9a4e | 2020-11-17 11:33:36 +0100 | [diff] [blame] | 199 | ((conf->flags & TRACE_F_QUIET) ? "true" : "false"), |
| 200 | ((conf->flags & TRACE_F_RAND_FWD) ? "true" : "false"), |
| 201 | ((conf->flags & TRACE_F_HEXDUMP) ? "true" : "false")); |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 202 | return 0; |
| 203 | } |
| 204 | |
Ilya Shipitsin | 6b79f38 | 2020-07-23 00:32:55 +0500 | [diff] [blame] | 205 | /* Free resources allocated by the trace filter. */ |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 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 | |
Ilya Shipitsin | 6b79f38 | 2020-07-23 00:32:55 +0500 | [diff] [blame] | 237 | /* Free resources allocate by the trace filter for each thread. */ |
Christopher Faulet | f227372 | 2017-07-27 16:58:42 +0200 | [diff] [blame] | 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 | 47d9a4e | 2020-11-17 11:33:36 +0100 | [diff] [blame] | 468 | if (ret && (conf->flags & TRACE_F_RAND_FWD)) { |
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) { |
Willy Tarreau | 52bf839 | 2020-03-08 00:42:37 +0100 | [diff] [blame] | 472 | ret = ha_random() % (ret+1); |
Christopher Faulet | 647fe1d | 2019-06-12 16:07:48 +0200 | [diff] [blame] | 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 | |
Christopher Faulet | 47d9a4e | 2020-11-17 11:33:36 +0100 | [diff] [blame] | 484 | if (conf->flags & TRACE_F_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) { |
Christopher Faulet | 47d9a4e | 2020-11-17 11:33:36 +0100 | [diff] [blame] | 536 | if (ret && (conf->flags & TRACE_F_RAND_FWD)) { |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 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) { |
Willy Tarreau | 52bf839 | 2020-03-08 00:42:37 +0100 | [diff] [blame] | 540 | ret = ha_random() % (ret+1); |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 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 | 47d9a4e | 2020-11-17 11:33:36 +0100 | [diff] [blame] | 552 | if (conf->flags & TRACE_F_HEXDUMP) |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 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 | 47d9a4e | 2020-11-17 11:33:36 +0100 | [diff] [blame] | 557 | if (ret && (conf->flags & TRACE_F_RAND_FWD)) |
Willy Tarreau | 52bf839 | 2020-03-08 00:42:37 +0100 | [diff] [blame] | 558 | ret = ha_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 | 47d9a4e | 2020-11-17 11:33:36 +0100 | [diff] [blame] | 566 | if (conf->flags & TRACE_F_HEXDUMP) |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 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; |
Christopher Faulet | 47d9a4e | 2020-11-17 11:33:36 +0100 | [diff] [blame] | 624 | conf->flags = 0; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 625 | if (strcmp(args[pos], "trace") == 0) { |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 626 | pos++; |
| 627 | |
| 628 | while (*args[pos]) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 629 | if (strcmp(args[pos], "name") == 0) { |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 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 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 642 | else if (strcmp(args[pos], "quiet") == 0) |
Christopher Faulet | 47d9a4e | 2020-11-17 11:33:36 +0100 | [diff] [blame] | 643 | conf->flags |= TRACE_F_QUIET; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 644 | else if (strcmp(args[pos], "random-parsing") == 0) |
Dragan Dosen | a8018eb | 2021-09-20 09:29:19 +0200 | [diff] [blame] | 645 | ; // ignore |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 646 | else if (strcmp(args[pos], "random-forwarding") == 0) |
Christopher Faulet | 47d9a4e | 2020-11-17 11:33:36 +0100 | [diff] [blame] | 647 | conf->flags |= TRACE_F_RAND_FWD; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 648 | else if (strcmp(args[pos], "hexdump") == 0) |
Christopher Faulet | 47d9a4e | 2020-11-17 11:33:36 +0100 | [diff] [blame] | 649 | conf->flags |= TRACE_F_HEXDUMP; |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 650 | else |
| 651 | break; |
| 652 | pos++; |
| 653 | } |
| 654 | *cur_arg = pos; |
Christopher Faulet | f4a4ef7 | 2018-12-07 17:39:53 +0100 | [diff] [blame] | 655 | fconf->id = trace_flt_id; |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 656 | fconf->ops = &trace_ops; |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 657 | } |
| 658 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 659 | fconf->conf = conf; |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 660 | return 0; |
| 661 | |
| 662 | error: |
| 663 | if (conf->name) |
| 664 | free(conf->name); |
| 665 | free(conf); |
| 666 | return -1; |
| 667 | } |
| 668 | |
| 669 | /* Declare the filter parser for "trace" keyword */ |
| 670 | static struct flt_kw_list flt_kws = { "TRACE", { }, { |
Thierry Fournier | 3610c39 | 2016-04-13 18:27:51 +0200 | [diff] [blame] | 671 | { "trace", parse_trace_flt, NULL }, |
| 672 | { NULL, NULL, NULL }, |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 673 | } |
| 674 | }; |
| 675 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 676 | INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws); |