Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Pass-through mux-demux for connections |
| 3 | * |
| 4 | * Copyright 2017 Willy Tarreau <w@1wt.eu> |
| 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 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 13 | #include <haproxy/api.h> |
Willy Tarreau | 16f958c | 2020-06-03 08:44:35 +0200 | [diff] [blame] | 14 | #include <haproxy/buf.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 15 | #include <haproxy/connection.h> |
Christopher Faulet | 1329f2a | 2021-12-16 17:32:56 +0100 | [diff] [blame] | 16 | #include <haproxy/conn_stream.h> |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 17 | #include <haproxy/pipe-t.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 18 | #include <haproxy/stream.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 19 | #include <haproxy/task.h> |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 20 | #include <haproxy/trace.h> |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 21 | |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 22 | struct mux_pt_ctx { |
| 23 | struct conn_stream *cs; |
| 24 | struct connection *conn; |
| 25 | struct wait_event wait_event; |
| 26 | }; |
| 27 | |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 28 | DECLARE_STATIC_POOL(pool_head_pt_ctx, "mux_pt", sizeof(struct mux_pt_ctx)); |
| 29 | |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 30 | /* trace source and events */ |
| 31 | static void pt_trace(enum trace_level level, uint64_t mask, |
| 32 | const struct trace_source *src, |
| 33 | const struct ist where, const struct ist func, |
| 34 | const void *a1, const void *a2, const void *a3, const void *a4); |
| 35 | |
| 36 | /* The event representation is split like this : |
| 37 | * pt_ctx - internal PT context |
| 38 | * strm - application layer |
| 39 | */ |
| 40 | static const struct trace_event pt_trace_events[] = { |
| 41 | #define PT_EV_CONN_NEW (1ULL << 0) |
| 42 | { .mask = PT_EV_CONN_NEW, .name = "pt_conn_new", .desc = "new PT connection" }, |
| 43 | #define PT_EV_CONN_WAKE (1ULL << 1) |
| 44 | { .mask = PT_EV_CONN_WAKE, .name = "pt_conn_wake", .desc = "PT connection woken up" }, |
| 45 | #define PT_EV_CONN_END (1ULL << 2) |
| 46 | { .mask = PT_EV_CONN_END, .name = "pt_conn_end", .desc = "PT connection terminated" }, |
| 47 | #define PT_EV_CONN_ERR (1ULL << 3) |
| 48 | { .mask = PT_EV_CONN_ERR, .name = "pt_conn_err", .desc = "error on PT connection" }, |
| 49 | #define PT_EV_STRM_NEW (1ULL << 4) |
| 50 | { .mask = PT_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" }, |
| 51 | #define PT_EV_STRM_SHUT (1ULL << 5) |
| 52 | { .mask = PT_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" }, |
| 53 | #define PT_EV_STRM_END (1ULL << 6) |
| 54 | { .mask = PT_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" }, |
| 55 | #define PT_EV_STRM_ERR (1ULL << 7) |
| 56 | { .mask = PT_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" }, |
| 57 | #define PT_EV_RX_DATA (1ULL << 8) |
| 58 | { .mask = PT_EV_RX_DATA, .name = "pt_rx_data", .desc = "Rx on PT connection" }, |
| 59 | #define PT_EV_TX_DATA (1ULL << 9) |
| 60 | { .mask = PT_EV_TX_DATA, .name = "pt_tx_data", .desc = "Tx on PT connection" }, |
| 61 | |
| 62 | {} |
| 63 | }; |
| 64 | |
| 65 | |
| 66 | static const struct name_desc pt_trace_decoding[] = { |
| 67 | #define PT_VERB_CLEAN 1 |
| 68 | { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" }, |
| 69 | #define PT_VERB_MINIMAL 2 |
| 70 | { .name="minimal", .desc="report only h1c/h1s state and flags, no real decoding" }, |
| 71 | #define PT_VERB_SIMPLE 3 |
| 72 | { .name="simple", .desc="add request/response status line or htx info when available" }, |
| 73 | #define PT_VERB_ADVANCED 4 |
| 74 | { .name="advanced", .desc="add header fields or frame decoding when available" }, |
| 75 | #define PT_VERB_COMPLETE 5 |
| 76 | { .name="complete", .desc="add full data dump when available" }, |
| 77 | { /* end */ } |
| 78 | }; |
| 79 | |
Willy Tarreau | 6eb3d37 | 2021-04-10 19:29:26 +0200 | [diff] [blame] | 80 | static struct trace_source trace_pt __read_mostly = { |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 81 | .name = IST("pt"), |
| 82 | .desc = "Passthrough multiplexer", |
| 83 | .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection |
| 84 | .default_cb = pt_trace, |
| 85 | .known_events = pt_trace_events, |
| 86 | .lockon_args = NULL, |
| 87 | .decoding = pt_trace_decoding, |
| 88 | .report_events = ~0, // report everything by default |
| 89 | }; |
| 90 | |
| 91 | #define TRACE_SOURCE &trace_pt |
| 92 | INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE); |
| 93 | |
| 94 | static inline void pt_trace_buf(const struct buffer *buf, size_t ofs, size_t len) |
| 95 | { |
| 96 | size_t block1, block2; |
| 97 | int line, ptr, newptr; |
| 98 | |
| 99 | block1 = b_contig_data(buf, ofs); |
| 100 | block2 = 0; |
| 101 | if (block1 > len) |
| 102 | block1 = len; |
| 103 | block2 = len - block1; |
| 104 | |
| 105 | ofs = b_peek_ofs(buf, ofs); |
| 106 | |
| 107 | line = 0; |
| 108 | ptr = ofs; |
| 109 | while (ptr < ofs + block1) { |
| 110 | newptr = dump_text_line(&trace_buf, b_orig(buf), b_size(buf), ofs + block1, &line, ptr); |
| 111 | if (newptr == ptr) |
| 112 | break; |
| 113 | ptr = newptr; |
| 114 | } |
| 115 | |
| 116 | line = ptr = 0; |
| 117 | while (ptr < block2) { |
| 118 | newptr = dump_text_line(&trace_buf, b_orig(buf), b_size(buf), block2, &line, ptr); |
| 119 | if (newptr == ptr) |
| 120 | break; |
| 121 | ptr = newptr; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /* the PT traces always expect that arg1, if non-null, is of type connection |
| 126 | * (from which we can derive the pt context), that arg2, if non-null, is a |
| 127 | * conn-stream, and that arg3, if non-null, is a buffer. |
| 128 | */ |
| 129 | static void pt_trace(enum trace_level level, uint64_t mask, const struct trace_source *src, |
| 130 | const struct ist where, const struct ist func, |
| 131 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 132 | { |
| 133 | const struct connection *conn = a1; |
| 134 | const struct mux_pt_ctx *ctx = conn ? conn->ctx : NULL; |
| 135 | const struct conn_stream *cs = a2; |
| 136 | const struct buffer *buf = a3; |
| 137 | const size_t *val = a4; |
| 138 | |
| 139 | if (!ctx|| src->verbosity < PT_VERB_CLEAN) |
| 140 | return; |
| 141 | |
| 142 | /* Display frontend/backend info by default */ |
| 143 | chunk_appendf(&trace_buf, " : [%c]", (conn_is_back(conn) ? 'B' : 'F')); |
| 144 | |
| 145 | if (src->verbosity == PT_VERB_CLEAN) |
| 146 | return; |
| 147 | |
| 148 | /* Display the value to the 4th argument (level > STATE) */ |
| 149 | if (src->level > TRACE_LEVEL_STATE && val) |
| 150 | chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val); |
| 151 | |
| 152 | /* Display conn and cs info, if defined (pointer + flags) */ |
| 153 | chunk_appendf(&trace_buf, " - conn=%p(0x%08x)", conn, conn->flags); |
| 154 | if (cs) |
| 155 | chunk_appendf(&trace_buf, " cs=%p(0x%08x)", cs, cs->flags); |
| 156 | |
| 157 | if (src->verbosity == PT_VERB_MINIMAL) |
| 158 | return; |
| 159 | |
| 160 | /* Display buffer info, if defined (level > USER & verbosity > SIMPLE) */ |
| 161 | if (src->level > TRACE_LEVEL_USER && buf) { |
| 162 | int full = 0, max = 3000, chunk = 1024; |
| 163 | |
| 164 | /* Full info (level > STATE && verbosity > SIMPLE) */ |
| 165 | if (src->level > TRACE_LEVEL_STATE) { |
| 166 | if (src->verbosity == PT_VERB_COMPLETE) |
| 167 | full = 1; |
| 168 | else if (src->verbosity == PT_VERB_ADVANCED) { |
| 169 | full = 1; |
| 170 | max = 256; |
| 171 | chunk = 64; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | chunk_appendf(&trace_buf, " buf=%u@%p+%u/%u", |
| 176 | (unsigned int)b_data(buf), b_orig(buf), |
| 177 | (unsigned int)b_head_ofs(buf), (unsigned int)b_size(buf)); |
| 178 | |
| 179 | if (b_data(buf) && full) { |
| 180 | chunk_memcat(&trace_buf, "\n", 1); |
| 181 | if (b_data(buf) < max) |
| 182 | pt_trace_buf(buf, 0, b_data(buf)); |
| 183 | else { |
| 184 | pt_trace_buf(buf, 0, chunk); |
| 185 | chunk_memcat(&trace_buf, " ...\n", 6); |
| 186 | pt_trace_buf(buf, b_data(buf) - chunk, chunk); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 192 | static void mux_pt_destroy(struct mux_pt_ctx *ctx) |
| 193 | { |
Christopher Faulet | 5a7ca29 | 2020-11-03 09:11:43 +0100 | [diff] [blame] | 194 | struct connection *conn = NULL; |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 195 | |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 196 | TRACE_POINT(PT_EV_CONN_END); |
| 197 | |
Christopher Faulet | 5a7ca29 | 2020-11-03 09:11:43 +0100 | [diff] [blame] | 198 | if (ctx) { |
| 199 | /* The connection must be attached to this mux to be released */ |
| 200 | if (ctx->conn && ctx->conn->ctx == ctx) |
| 201 | conn = ctx->conn; |
| 202 | |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 203 | TRACE_DEVEL("freeing pt context", PT_EV_CONN_END, conn); |
| 204 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 205 | tasklet_free(ctx->wait_event.tasklet); |
Christopher Faulet | 5a7ca29 | 2020-11-03 09:11:43 +0100 | [diff] [blame] | 206 | |
| 207 | if (conn && ctx->wait_event.events != 0) |
| 208 | conn->xprt->unsubscribe(conn, conn->xprt_ctx, ctx->wait_event.events, |
| 209 | &ctx->wait_event); |
| 210 | pool_free(pool_head_pt_ctx, ctx); |
| 211 | } |
| 212 | |
| 213 | if (conn) { |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 214 | conn->mux = NULL; |
| 215 | conn->ctx = NULL; |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 216 | TRACE_DEVEL("freeing conn", PT_EV_CONN_END, conn); |
Christopher Faulet | 5a7ca29 | 2020-11-03 09:11:43 +0100 | [diff] [blame] | 217 | |
| 218 | conn_stop_tracking(conn); |
| 219 | conn_full_close(conn); |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 220 | if (conn->destroy_cb) |
| 221 | conn->destroy_cb(conn); |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 222 | conn_free(conn); |
| 223 | } |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 224 | } |
| 225 | |
Willy Tarreau | 691d503 | 2021-01-20 14:55:01 +0100 | [diff] [blame] | 226 | /* Callback, used when we get I/Os while in idle mode. This one is exported so |
| 227 | * that "show fd" can resolve it. |
| 228 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 229 | struct task *mux_pt_io_cb(struct task *t, void *tctx, unsigned int status) |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 230 | { |
| 231 | struct mux_pt_ctx *ctx = tctx; |
| 232 | |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 233 | TRACE_ENTER(PT_EV_CONN_WAKE, ctx->conn, ctx->cs); |
Olivier Houchard | ea510fc | 2019-10-18 13:56:40 +0200 | [diff] [blame] | 234 | if (ctx->cs) { |
| 235 | /* There's a small race condition. |
| 236 | * mux_pt_io_cb() is only supposed to be called if we have no |
| 237 | * stream attached. However, maybe the tasklet got woken up, |
| 238 | * and this connection was then attached to a new stream. |
Olivier Houchard | 2ed389d | 2019-10-18 14:18:29 +0200 | [diff] [blame] | 239 | * If this happened, just wake the tasklet up if anybody |
| 240 | * subscribed to receive events, and otherwise call the wake |
| 241 | * method, to make sure the event is noticed. |
Olivier Houchard | ea510fc | 2019-10-18 13:56:40 +0200 | [diff] [blame] | 242 | */ |
Willy Tarreau | 7872d1f | 2020-01-10 07:06:05 +0100 | [diff] [blame] | 243 | if (ctx->conn->subs) { |
| 244 | ctx->conn->subs->events = 0; |
| 245 | tasklet_wakeup(ctx->conn->subs->tasklet); |
| 246 | ctx->conn->subs = NULL; |
Olivier Houchard | 2ed389d | 2019-10-18 14:18:29 +0200 | [diff] [blame] | 247 | } else if (ctx->cs->data_cb->wake) |
Olivier Houchard | ea510fc | 2019-10-18 13:56:40 +0200 | [diff] [blame] | 248 | ctx->cs->data_cb->wake(ctx->cs); |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 249 | TRACE_DEVEL("leaving waking up CS", PT_EV_CONN_WAKE, ctx->conn, ctx->cs); |
Willy Tarreau | 7416314 | 2021-03-13 11:30:19 +0100 | [diff] [blame] | 250 | return t; |
Olivier Houchard | ea510fc | 2019-10-18 13:56:40 +0200 | [diff] [blame] | 251 | } |
Willy Tarreau | 2ded48d | 2020-12-11 16:20:34 +0100 | [diff] [blame] | 252 | conn_ctrl_drain(ctx->conn); |
Willy Tarreau | 7416314 | 2021-03-13 11:30:19 +0100 | [diff] [blame] | 253 | if (ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) { |
Christopher Faulet | e2c65ba | 2021-04-10 09:02:32 +0200 | [diff] [blame] | 254 | TRACE_DEVEL("leaving destroying pt context", PT_EV_CONN_WAKE, ctx->conn); |
Olivier Houchard | 9dce2c5 | 2019-10-18 10:59:30 +0200 | [diff] [blame] | 255 | mux_pt_destroy(ctx); |
Willy Tarreau | 7416314 | 2021-03-13 11:30:19 +0100 | [diff] [blame] | 256 | t = NULL; |
| 257 | } |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 258 | else { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 259 | ctx->conn->xprt->subscribe(ctx->conn, ctx->conn->xprt_ctx, SUB_RETRY_RECV, |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 260 | &ctx->wait_event); |
Christopher Faulet | e2c65ba | 2021-04-10 09:02:32 +0200 | [diff] [blame] | 261 | TRACE_DEVEL("leaving subscribing for reads", PT_EV_CONN_WAKE, ctx->conn); |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 262 | } |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 263 | |
Willy Tarreau | 7416314 | 2021-03-13 11:30:19 +0100 | [diff] [blame] | 264 | return t; |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 265 | } |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 266 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 267 | /* Initialize the mux once it's attached. It is expected that conn->ctx |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 268 | * points to the existing conn_stream (for outgoing connections) or NULL (for |
| 269 | * incoming ones, in which case one will be allocated and a new stream will be |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 270 | * instantiated). Returns < 0 on error. |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 271 | */ |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 272 | static int mux_pt_init(struct connection *conn, struct proxy *prx, struct session *sess, |
| 273 | struct buffer *input) |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 274 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 275 | struct conn_stream *cs = conn->ctx; |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 276 | struct mux_pt_ctx *ctx = pool_alloc(pool_head_pt_ctx); |
| 277 | |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 278 | TRACE_ENTER(PT_EV_CONN_NEW); |
| 279 | |
| 280 | if (!ctx) { |
| 281 | TRACE_ERROR("PT context allocation failure", PT_EV_CONN_NEW|PT_EV_CONN_END|PT_EV_CONN_ERR); |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 282 | goto fail; |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 283 | } |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 284 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 285 | ctx->wait_event.tasklet = tasklet_new(); |
| 286 | if (!ctx->wait_event.tasklet) |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 287 | goto fail_free_ctx; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 288 | ctx->wait_event.tasklet->context = ctx; |
| 289 | ctx->wait_event.tasklet->process = mux_pt_io_cb; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 290 | ctx->wait_event.events = 0; |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 291 | ctx->conn = conn; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 292 | |
| 293 | if (!cs) { |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 294 | cs = cs_new(); |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 295 | if (!cs) { |
| 296 | TRACE_ERROR("CS allocation failure", PT_EV_STRM_NEW|PT_EV_STRM_END|PT_EV_STRM_ERR, conn); |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 297 | goto fail_free_ctx; |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 298 | } |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 299 | cs_attach_endp(cs, &conn->obj_type, NULL); |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 300 | |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 301 | if (!stream_new(conn->owner, cs, &BUF_NULL)) { |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 302 | TRACE_ERROR("stream creation failure", PT_EV_STRM_NEW|PT_EV_STRM_END|PT_EV_STRM_ERR, conn, cs); |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 303 | goto fail_free; |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 304 | } |
| 305 | TRACE_POINT(PT_EV_STRM_NEW, conn, cs); |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 306 | } |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 307 | conn->ctx = ctx; |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 308 | ctx->cs = cs; |
Olivier Houchard | 8706c81 | 2018-12-04 19:17:25 +0100 | [diff] [blame] | 309 | cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 17ccd1a | 2020-01-17 16:19:34 +0100 | [diff] [blame] | 310 | if (global.tune.options & GTUNE_USE_SPLICE) |
| 311 | cs->flags |= CS_FL_MAY_SPLICE; |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 312 | |
| 313 | TRACE_LEAVE(PT_EV_CONN_NEW, conn, cs); |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 314 | return 0; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 315 | |
| 316 | fail_free: |
| 317 | cs_free(cs); |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 318 | fail_free_ctx: |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 319 | if (ctx->wait_event.tasklet) |
| 320 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 321 | pool_free(pool_head_pt_ctx, ctx); |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 322 | fail: |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 323 | TRACE_DEVEL("leaving in error", PT_EV_CONN_NEW|PT_EV_CONN_END|PT_EV_CONN_ERR); |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 324 | return -1; |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | /* callback to be used by default for the pass-through mux. It calls the data |
| 328 | * layer wake() callback if it is set otherwise returns 0. |
| 329 | */ |
| 330 | static int mux_pt_wake(struct connection *conn) |
| 331 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 332 | struct mux_pt_ctx *ctx = conn->ctx; |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 333 | struct conn_stream *cs = ctx->cs; |
| 334 | int ret = 0; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 335 | |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 336 | TRACE_ENTER(PT_EV_CONN_WAKE, ctx->conn, cs); |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 337 | if (cs) { |
| 338 | ret = cs->data_cb->wake ? cs->data_cb->wake(cs) : 0; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 339 | |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 340 | if (ret < 0) { |
| 341 | TRACE_DEVEL("leaving waking up CS", PT_EV_CONN_WAKE, ctx->conn, cs); |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 342 | return ret; |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 343 | } |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 344 | } else { |
Willy Tarreau | 2ded48d | 2020-12-11 16:20:34 +0100 | [diff] [blame] | 345 | conn_ctrl_drain(conn); |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 346 | if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH)) { |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 347 | TRACE_DEVEL("leaving destroying PT context", PT_EV_CONN_WAKE, ctx->conn); |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 348 | mux_pt_destroy(ctx); |
| 349 | return -1; |
| 350 | } |
| 351 | } |
Willy Tarreau | ad7f0ad | 2018-08-24 15:48:59 +0200 | [diff] [blame] | 352 | |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 353 | /* If we had early data, and we're done with the handshake |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 354 | * then we know the data are safe, and we can remove the flag. |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 355 | */ |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 356 | if ((conn->flags & (CO_FL_EARLY_DATA | CO_FL_EARLY_SSL_HS | CO_FL_WAIT_XPRT)) == |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 357 | CO_FL_EARLY_DATA) |
| 358 | conn->flags &= ~CO_FL_EARLY_DATA; |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 359 | |
| 360 | TRACE_LEAVE(PT_EV_CONN_WAKE, ctx->conn); |
Willy Tarreau | ed339a3 | 2017-11-03 15:55:24 +0100 | [diff] [blame] | 361 | return ret; |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 362 | } |
| 363 | |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 364 | /* |
| 365 | * Attach a new stream to a connection |
| 366 | * (Used for outgoing connections) |
| 367 | */ |
Christopher Faulet | e00ad35 | 2021-12-16 14:44:31 +0100 | [diff] [blame] | 368 | static int mux_pt_attach(struct connection *conn, struct conn_stream *cs, struct session *sess) |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 369 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 370 | struct mux_pt_ctx *ctx = conn->ctx; |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 371 | |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 372 | TRACE_ENTER(PT_EV_STRM_NEW, conn); |
Olivier Houchard | ea32b0f | 2019-08-10 23:56:16 +0200 | [diff] [blame] | 373 | if (ctx->wait_event.events) |
| 374 | conn->xprt->unsubscribe(ctx->conn, conn->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event); |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 375 | ctx->cs = cs; |
Olivier Houchard | 8706c81 | 2018-12-04 19:17:25 +0100 | [diff] [blame] | 376 | cs->flags |= CS_FL_RCV_MORE; |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 377 | |
| 378 | TRACE_LEAVE(PT_EV_STRM_NEW, conn, cs); |
Christopher Faulet | e00ad35 | 2021-12-16 14:44:31 +0100 | [diff] [blame] | 379 | return 0; |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 380 | } |
| 381 | |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 382 | /* Retrieves a valid conn_stream from this connection, or returns NULL. For |
| 383 | * this mux, it's easy as we can only store a single conn_stream. |
| 384 | */ |
| 385 | static const struct conn_stream *mux_pt_get_first_cs(const struct connection *conn) |
| 386 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 387 | struct mux_pt_ctx *ctx = conn->ctx; |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 388 | struct conn_stream *cs = ctx->cs; |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 389 | |
| 390 | return cs; |
| 391 | } |
| 392 | |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 393 | /* Destroy the mux and the associated connection if still attached to this mux |
| 394 | * and no longer used */ |
| 395 | static void mux_pt_destroy_meth(void *ctx) |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 396 | { |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 397 | struct mux_pt_ctx *pt = ctx; |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 398 | |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 399 | TRACE_POINT(PT_EV_CONN_END, pt->conn, pt->cs); |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 400 | if (!(pt->cs) || !(pt->conn) || pt->conn->ctx != pt) |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 401 | mux_pt_destroy(pt); |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 402 | } |
| 403 | |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 404 | /* |
Willy Tarreau | 2c52a2b | 2017-10-08 11:00:17 +0200 | [diff] [blame] | 405 | * Detach the stream from the connection and possibly release the connection. |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 406 | */ |
| 407 | static void mux_pt_detach(struct conn_stream *cs) |
| 408 | { |
Christopher Faulet | 693b23b | 2022-02-28 09:09:05 +0100 | [diff] [blame] | 409 | struct connection *conn = __cs_conn(cs); |
Christopher Faulet | 2da02ae | 2022-02-24 13:45:27 +0100 | [diff] [blame] | 410 | struct mux_pt_ctx *ctx; |
| 411 | |
| 412 | ALREADY_CHECKED(conn); |
| 413 | ctx = conn->ctx; |
Willy Tarreau | 2c52a2b | 2017-10-08 11:00:17 +0200 | [diff] [blame] | 414 | |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 415 | TRACE_ENTER(PT_EV_STRM_END, conn, cs); |
| 416 | |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 417 | /* Subscribe, to know if we got disconnected */ |
Christopher Faulet | fbff854 | 2022-03-09 15:55:58 +0100 | [diff] [blame] | 418 | if (!conn_is_back(conn) && conn->owner != NULL && |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 419 | !(conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) { |
| 420 | ctx->cs = NULL; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 421 | conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event); |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 422 | } else { |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 423 | /* There's no session attached to that connection, destroy it */ |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 424 | TRACE_DEVEL("killing dead connection", PT_EV_STRM_END, conn, cs); |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 425 | mux_pt_destroy(ctx); |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | TRACE_LEAVE(PT_EV_STRM_END); |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 429 | } |
| 430 | |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 431 | /* returns the number of streams in use on a connection */ |
| 432 | static int mux_pt_used_streams(struct connection *conn) |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 433 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 434 | struct mux_pt_ctx *ctx = conn->ctx; |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 435 | |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 436 | return ctx->cs ? 1 : 0; |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 437 | } |
| 438 | |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 439 | /* returns the number of streams still available on a connection */ |
| 440 | static int mux_pt_avail_streams(struct connection *conn) |
Olivier Houchard | 8defe4b | 2018-12-02 01:31:17 +0100 | [diff] [blame] | 441 | { |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 442 | return 1 - mux_pt_used_streams(conn); |
Olivier Houchard | 8defe4b | 2018-12-02 01:31:17 +0100 | [diff] [blame] | 443 | } |
| 444 | |
Willy Tarreau | ecdb3fe | 2017-10-05 15:25:48 +0200 | [diff] [blame] | 445 | static void mux_pt_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 446 | { |
Christopher Faulet | 693b23b | 2022-02-28 09:09:05 +0100 | [diff] [blame] | 447 | struct connection *conn = __cs_conn(cs); |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 448 | |
| 449 | TRACE_ENTER(PT_EV_STRM_SHUT, conn, cs); |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 450 | |
Willy Tarreau | 4b79524 | 2017-10-05 18:47:38 +0200 | [diff] [blame] | 451 | if (cs->flags & CS_FL_SHR) |
| 452 | return; |
Christopher Faulet | d94f877 | 2018-12-17 13:21:02 +0100 | [diff] [blame] | 453 | cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 454 | if (conn_xprt_ready(conn) && conn->xprt->shutr) |
| 455 | conn->xprt->shutr(conn, conn->xprt_ctx, |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 456 | (mode == CS_SHR_DRAIN)); |
Willy Tarreau | 7d7b11c | 2020-12-11 11:07:19 +0100 | [diff] [blame] | 457 | else if (mode == CS_SHR_DRAIN) |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 458 | conn_ctrl_drain(conn); |
Willy Tarreau | 4b79524 | 2017-10-05 18:47:38 +0200 | [diff] [blame] | 459 | if (cs->flags & CS_FL_SHW) |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 460 | conn_full_close(conn); |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 461 | |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 462 | TRACE_LEAVE(PT_EV_STRM_SHUT, conn, cs); |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 463 | } |
| 464 | |
Willy Tarreau | ecdb3fe | 2017-10-05 15:25:48 +0200 | [diff] [blame] | 465 | static void mux_pt_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 466 | { |
Christopher Faulet | 693b23b | 2022-02-28 09:09:05 +0100 | [diff] [blame] | 467 | struct connection *conn = __cs_conn(cs); |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 468 | |
| 469 | TRACE_ENTER(PT_EV_STRM_SHUT, conn, cs); |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 470 | |
Willy Tarreau | 4b79524 | 2017-10-05 18:47:38 +0200 | [diff] [blame] | 471 | if (cs->flags & CS_FL_SHW) |
| 472 | return; |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 473 | if (conn_xprt_ready(conn) && conn->xprt->shutw) |
| 474 | conn->xprt->shutw(conn, conn->xprt_ctx, |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 475 | (mode == CS_SHW_NORMAL)); |
Willy Tarreau | 4b79524 | 2017-10-05 18:47:38 +0200 | [diff] [blame] | 476 | if (!(cs->flags & CS_FL_SHR)) |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 477 | conn_sock_shutw(conn, (mode == CS_SHW_NORMAL)); |
Willy Tarreau | 4b79524 | 2017-10-05 18:47:38 +0200 | [diff] [blame] | 478 | else |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 479 | conn_full_close(conn); |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 480 | |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 481 | TRACE_LEAVE(PT_EV_STRM_SHUT, conn, cs); |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | /* |
| 485 | * Called from the upper layer, to get more data |
Christopher Faulet | 564e39c | 2021-09-21 15:50:55 +0200 | [diff] [blame] | 486 | * |
| 487 | * The caller is responsible for defragmenting <buf> if necessary. But <flags> |
| 488 | * must be tested to know the calling context. If CO_RFL_BUF_FLUSH is set, it |
| 489 | * means the caller wants to flush input data (from the mux buffer and the |
| 490 | * channel buffer) to be able to use kernel splicing or any kind of mux-to-mux |
| 491 | * xfer. If CO_RFL_KEEP_RECV is set, the mux must always subscribe for read |
| 492 | * events before giving back. CO_RFL_BUF_WET is set if <buf> is congested with |
| 493 | * data scheduled for leaving soon. CO_RFL_BUF_NOT_STUCK is set to instruct the |
| 494 | * mux it may optimize the data copy to <buf> if necessary. Otherwise, it should |
| 495 | * copy as much data as possible. |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 496 | */ |
Willy Tarreau | 7f3225f | 2018-06-19 06:15:17 +0200 | [diff] [blame] | 497 | static size_t mux_pt_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 498 | { |
Christopher Faulet | 693b23b | 2022-02-28 09:09:05 +0100 | [diff] [blame] | 499 | struct connection *conn = __cs_conn(cs); |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 500 | size_t ret = 0; |
| 501 | |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 502 | TRACE_ENTER(PT_EV_RX_DATA, conn, cs, buf, (size_t[]){count}); |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 503 | |
Christopher Faulet | 4eb7d74 | 2018-10-11 15:29:21 +0200 | [diff] [blame] | 504 | if (!count) { |
Christopher Faulet | d94f877 | 2018-12-17 13:21:02 +0100 | [diff] [blame] | 505 | cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 506 | goto end; |
Christopher Faulet | 4eb7d74 | 2018-10-11 15:29:21 +0200 | [diff] [blame] | 507 | } |
Willy Tarreau | e0f24ee | 2018-12-14 10:51:23 +0100 | [diff] [blame] | 508 | b_realign_if_empty(buf); |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 509 | ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, count, flags); |
| 510 | if (conn_xprt_read0_pending(conn)) { |
Willy Tarreau | 9cca8df | 2019-07-15 06:47:54 +0200 | [diff] [blame] | 511 | cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Christopher Faulet | 87a8f35 | 2019-03-22 14:51:36 +0100 | [diff] [blame] | 512 | cs->flags |= CS_FL_EOS; |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 513 | TRACE_DEVEL("read0 on connection", PT_EV_RX_DATA, conn, cs); |
Olivier Houchard | 8706c81 | 2018-12-04 19:17:25 +0100 | [diff] [blame] | 514 | } |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 515 | if (conn->flags & CO_FL_ERROR) { |
Willy Tarreau | 9cca8df | 2019-07-15 06:47:54 +0200 | [diff] [blame] | 516 | cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Willy Tarreau | 4ff3b89 | 2017-10-16 15:17:17 +0200 | [diff] [blame] | 517 | cs->flags |= CS_FL_ERROR; |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 518 | TRACE_DEVEL("error on connection", PT_EV_RX_DATA|PT_EV_CONN_ERR, conn, cs); |
Olivier Houchard | 8706c81 | 2018-12-04 19:17:25 +0100 | [diff] [blame] | 519 | } |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 520 | end: |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 521 | TRACE_LEAVE(PT_EV_RX_DATA, conn, cs, buf, (size_t[]){ret}); |
Willy Tarreau | d9cf540 | 2018-07-18 11:29:06 +0200 | [diff] [blame] | 522 | return ret; |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | /* Called from the upper layer, to send data */ |
Christopher Faulet | d44a9b3 | 2018-07-27 11:59:41 +0200 | [diff] [blame] | 526 | static size_t mux_pt_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 527 | { |
Christopher Faulet | 693b23b | 2022-02-28 09:09:05 +0100 | [diff] [blame] | 528 | struct connection *conn = __cs_conn(cs); |
Olivier Houchard | b72d98a | 2018-11-30 13:17:48 +0100 | [diff] [blame] | 529 | size_t ret; |
| 530 | |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 531 | TRACE_ENTER(PT_EV_TX_DATA, conn, cs, buf, (size_t[]){count}); |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 532 | |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 533 | ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, count, flags); |
Christopher Faulet | d44a9b3 | 2018-07-27 11:59:41 +0200 | [diff] [blame] | 534 | |
| 535 | if (ret > 0) |
| 536 | b_del(buf, ret); |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 537 | |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 538 | TRACE_LEAVE(PT_EV_TX_DATA, conn, cs, buf, (size_t[]){ret}); |
Christopher Faulet | d44a9b3 | 2018-07-27 11:59:41 +0200 | [diff] [blame] | 539 | return ret; |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 540 | } |
| 541 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 542 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 543 | * event subscriber <es> is not allowed to change from a previous call as long |
| 544 | * as at least one event is still subscribed. The <event_type> must only be a |
| 545 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0. |
| 546 | */ |
| 547 | static int mux_pt_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es) |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 548 | { |
Christopher Faulet | 693b23b | 2022-02-28 09:09:05 +0100 | [diff] [blame] | 549 | struct connection *conn = __cs_conn(cs); |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 550 | |
| 551 | TRACE_POINT(PT_EV_RX_DATA|PT_EV_TX_DATA, conn, cs, 0, (size_t[]){event_type}); |
| 552 | return conn->xprt->subscribe(conn, conn->xprt_ctx, event_type, es); |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 553 | } |
| 554 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 555 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 556 | * The <es> pointer is not allowed to differ from the one passed to the |
| 557 | * subscribe() call. It always returns zero. |
| 558 | */ |
| 559 | static int mux_pt_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es) |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 560 | { |
Christopher Faulet | 693b23b | 2022-02-28 09:09:05 +0100 | [diff] [blame] | 561 | struct connection *conn = __cs_conn(cs); |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 562 | |
| 563 | TRACE_POINT(PT_EV_RX_DATA|PT_EV_TX_DATA, conn, cs, 0, (size_t[]){event_type}); |
| 564 | return conn->xprt->unsubscribe(conn, conn->xprt_ctx, event_type, es); |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 565 | } |
| 566 | |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 567 | #if defined(USE_LINUX_SPLICE) |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 568 | /* Send and get, using splicing */ |
| 569 | static int mux_pt_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count) |
| 570 | { |
Christopher Faulet | 693b23b | 2022-02-28 09:09:05 +0100 | [diff] [blame] | 571 | struct connection *conn = __cs_conn(cs); |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 572 | int ret; |
| 573 | |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 574 | TRACE_ENTER(PT_EV_RX_DATA, conn, cs, 0, (size_t[]){count}); |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 575 | |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 576 | ret = conn->xprt->rcv_pipe(conn, conn->xprt_ctx, pipe, count); |
| 577 | if (conn_xprt_read0_pending(conn)) { |
Christopher Faulet | 87a8f35 | 2019-03-22 14:51:36 +0100 | [diff] [blame] | 578 | cs->flags |= CS_FL_EOS; |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 579 | TRACE_DEVEL("read0 on connection", PT_EV_RX_DATA, conn, cs); |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 580 | } |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 581 | if (conn->flags & CO_FL_ERROR) { |
Willy Tarreau | 4ff3b89 | 2017-10-16 15:17:17 +0200 | [diff] [blame] | 582 | cs->flags |= CS_FL_ERROR; |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 583 | TRACE_DEVEL("error on connection", PT_EV_RX_DATA|PT_EV_CONN_ERR, conn, cs); |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 584 | } |
| 585 | |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 586 | TRACE_LEAVE(PT_EV_RX_DATA, conn, cs, 0, (size_t[]){ret}); |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 587 | return (ret); |
| 588 | } |
| 589 | |
| 590 | static int mux_pt_snd_pipe(struct conn_stream *cs, struct pipe *pipe) |
| 591 | { |
Christopher Faulet | 693b23b | 2022-02-28 09:09:05 +0100 | [diff] [blame] | 592 | struct connection *conn = __cs_conn(cs); |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 593 | int ret; |
| 594 | |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 595 | TRACE_ENTER(PT_EV_TX_DATA, conn, cs, 0, (size_t[]){pipe->data}); |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 596 | |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 597 | ret = conn->xprt->snd_pipe(conn, conn->xprt_ctx, pipe); |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 598 | |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 599 | TRACE_LEAVE(PT_EV_TX_DATA, conn, cs, 0, (size_t[]){ret}); |
Christopher Faulet | c0ae097 | 2021-04-08 16:45:11 +0200 | [diff] [blame] | 600 | return ret; |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 601 | } |
Olivier Houchard | 7da120b | 2017-11-01 13:55:10 +0100 | [diff] [blame] | 602 | #endif |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 603 | |
Olivier Houchard | 9b8e11e | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 604 | static int mux_pt_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output) |
| 605 | { |
| 606 | int ret = 0; |
| 607 | switch (mux_ctl) { |
| 608 | case MUX_STATUS: |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 609 | if (!(conn->flags & CO_FL_WAIT_XPRT)) |
Olivier Houchard | 9b8e11e | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 610 | ret |= MUX_STATUS_READY; |
| 611 | return ret; |
Christopher Faulet | 4c8ad84 | 2020-10-06 14:59:17 +0200 | [diff] [blame] | 612 | case MUX_EXIT_STATUS: |
| 613 | return MUX_ES_UNKNOWN; |
Olivier Houchard | 9b8e11e | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 614 | default: |
| 615 | return -1; |
| 616 | } |
| 617 | } |
| 618 | |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 619 | /* The mux operations */ |
Christopher Faulet | 28da3f5 | 2021-02-05 16:44:46 +0100 | [diff] [blame] | 620 | const struct mux_ops mux_tcp_ops = { |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 621 | .init = mux_pt_init, |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 622 | .wake = mux_pt_wake, |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 623 | .rcv_buf = mux_pt_rcv_buf, |
| 624 | .snd_buf = mux_pt_snd_buf, |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 625 | .subscribe = mux_pt_subscribe, |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 626 | .unsubscribe = mux_pt_unsubscribe, |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 627 | #if defined(USE_LINUX_SPLICE) |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 628 | .rcv_pipe = mux_pt_rcv_pipe, |
| 629 | .snd_pipe = mux_pt_snd_pipe, |
| 630 | #endif |
| 631 | .attach = mux_pt_attach, |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 632 | .get_first_cs = mux_pt_get_first_cs, |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 633 | .detach = mux_pt_detach, |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 634 | .avail_streams = mux_pt_avail_streams, |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 635 | .used_streams = mux_pt_used_streams, |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 636 | .destroy = mux_pt_destroy_meth, |
Olivier Houchard | 9b8e11e | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 637 | .ctl = mux_pt_ctl, |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 638 | .shutr = mux_pt_shutr, |
| 639 | .shutw = mux_pt_shutw, |
Willy Tarreau | 28f1cb9 | 2017-12-20 16:14:44 +0100 | [diff] [blame] | 640 | .flags = MX_FL_NONE, |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 641 | .name = "PASS", |
| 642 | }; |
Willy Tarreau | f649082 | 2017-09-21 19:43:21 +0200 | [diff] [blame] | 643 | |
Christopher Faulet | 28da3f5 | 2021-02-05 16:44:46 +0100 | [diff] [blame] | 644 | |
| 645 | const struct mux_ops mux_pt_ops = { |
| 646 | .init = mux_pt_init, |
| 647 | .wake = mux_pt_wake, |
| 648 | .rcv_buf = mux_pt_rcv_buf, |
| 649 | .snd_buf = mux_pt_snd_buf, |
| 650 | .subscribe = mux_pt_subscribe, |
| 651 | .unsubscribe = mux_pt_unsubscribe, |
| 652 | #if defined(USE_LINUX_SPLICE) |
| 653 | .rcv_pipe = mux_pt_rcv_pipe, |
| 654 | .snd_pipe = mux_pt_snd_pipe, |
| 655 | #endif |
| 656 | .attach = mux_pt_attach, |
| 657 | .get_first_cs = mux_pt_get_first_cs, |
| 658 | .detach = mux_pt_detach, |
| 659 | .avail_streams = mux_pt_avail_streams, |
| 660 | .used_streams = mux_pt_used_streams, |
| 661 | .destroy = mux_pt_destroy_meth, |
| 662 | .ctl = mux_pt_ctl, |
| 663 | .shutr = mux_pt_shutr, |
| 664 | .shutw = mux_pt_shutw, |
| 665 | .flags = MX_FL_NONE|MX_FL_NO_UPG, |
| 666 | .name = "PASS", |
| 667 | }; |
| 668 | |
Christopher Faulet | 32f61c0 | 2018-04-10 14:33:41 +0200 | [diff] [blame] | 669 | /* PROT selection : default mux has empty name */ |
Christopher Faulet | 28da3f5 | 2021-02-05 16:44:46 +0100 | [diff] [blame] | 670 | static struct mux_proto_list mux_proto_none = |
| 671 | { .token = IST("none"), .mode = PROTO_MODE_TCP, .side = PROTO_SIDE_BOTH, .mux = &mux_pt_ops }; |
| 672 | static struct mux_proto_list mux_proto_tcp = |
| 673 | { .token = IST(""), .mode = PROTO_MODE_TCP, .side = PROTO_SIDE_BOTH, .mux = &mux_tcp_ops }; |
Willy Tarreau | f649082 | 2017-09-21 19:43:21 +0200 | [diff] [blame] | 674 | |
Christopher Faulet | 28da3f5 | 2021-02-05 16:44:46 +0100 | [diff] [blame] | 675 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_none); |
| 676 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_tcp); |