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