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