blob: 821f558ba651400fdd88805cdf4b234d16c1e6c2 [file] [log] [blame]
Willy Tarreau53a47662017-08-28 10:53:00 +02001/*
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 Tarreau4c7e4b72020-05-27 12:58:42 +020013#include <haproxy/api.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020014#include <haproxy/buf.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020015#include <haproxy/connection.h>
Christopher Faulet1329f2a2021-12-16 17:32:56 +010016#include <haproxy/conn_stream.h>
Christopher Fauletc0ae0972021-04-08 16:45:11 +020017#include <haproxy/pipe-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020018#include <haproxy/stream.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020019#include <haproxy/task.h>
Christopher Fauletc0ae0972021-04-08 16:45:11 +020020#include <haproxy/trace.h>
Olivier Houchardb6c32ee2018-11-05 18:28:43 +010021
Olivier Houchardb6c32ee2018-11-05 18:28:43 +010022struct mux_pt_ctx {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +010023 struct cs_endpoint *endp;
Olivier Houchardb6c32ee2018-11-05 18:28:43 +010024 struct connection *conn;
25 struct wait_event wait_event;
26};
27
Willy Tarreau8ceae722018-11-26 11:58:30 +010028DECLARE_STATIC_POOL(pool_head_pt_ctx, "mux_pt", sizeof(struct mux_pt_ctx));
29
Christopher Fauletc0ae0972021-04-08 16:45:11 +020030/* trace source and events */
31static 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 */
40static 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
66static 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 Tarreau6eb3d372021-04-10 19:29:26 +020080static struct trace_source trace_pt __read_mostly = {
Christopher Fauletc0ae0972021-04-08 16:45:11 +020081 .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
92INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
93
94static 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 */
129static 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
Christopher Faulet1bceee22022-03-24 10:51:08 +0100139 if (!ctx || src->verbosity < PT_VERB_CLEAN)
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200140 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
Christopher Faulet1bceee22022-03-24 10:51:08 +0100148 if (!cs)
Willy Tarreaucfbfc3f2022-05-10 19:23:39 +0200149 cs = ctx->endp->cs;
Christopher Faulet1bceee22022-03-24 10:51:08 +0100150
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200151 /* Display the value to the 4th argument (level > STATE) */
152 if (src->level > TRACE_LEVEL_STATE && val)
153 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
154
155 /* Display conn and cs info, if defined (pointer + flags) */
156 chunk_appendf(&trace_buf, " - conn=%p(0x%08x)", conn, conn->flags);
Christopher Faulet1bceee22022-03-24 10:51:08 +0100157 chunk_appendf(&trace_buf, " endp=%p(0x%08x)", ctx->endp, ctx->endp->flags);
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200158 if (cs)
159 chunk_appendf(&trace_buf, " cs=%p(0x%08x)", cs, cs->flags);
160
161 if (src->verbosity == PT_VERB_MINIMAL)
162 return;
163
164 /* Display buffer info, if defined (level > USER & verbosity > SIMPLE) */
165 if (src->level > TRACE_LEVEL_USER && buf) {
166 int full = 0, max = 3000, chunk = 1024;
167
168 /* Full info (level > STATE && verbosity > SIMPLE) */
169 if (src->level > TRACE_LEVEL_STATE) {
170 if (src->verbosity == PT_VERB_COMPLETE)
171 full = 1;
172 else if (src->verbosity == PT_VERB_ADVANCED) {
173 full = 1;
174 max = 256;
175 chunk = 64;
176 }
177 }
178
179 chunk_appendf(&trace_buf, " buf=%u@%p+%u/%u",
180 (unsigned int)b_data(buf), b_orig(buf),
181 (unsigned int)b_head_ofs(buf), (unsigned int)b_size(buf));
182
183 if (b_data(buf) && full) {
184 chunk_memcat(&trace_buf, "\n", 1);
185 if (b_data(buf) < max)
186 pt_trace_buf(buf, 0, b_data(buf));
187 else {
188 pt_trace_buf(buf, 0, chunk);
189 chunk_memcat(&trace_buf, " ...\n", 6);
190 pt_trace_buf(buf, b_data(buf) - chunk, chunk);
191 }
192 }
193 }
194}
195
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100196static void mux_pt_destroy(struct mux_pt_ctx *ctx)
197{
Christopher Faulet5a7ca292020-11-03 09:11:43 +0100198 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200199
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200200 TRACE_POINT(PT_EV_CONN_END);
201
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200202 /* The connection must be attached to this mux to be released */
203 if (ctx->conn && ctx->conn->ctx == ctx)
204 conn = ctx->conn;
Christopher Faulet5a7ca292020-11-03 09:11:43 +0100205
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200206 tasklet_free(ctx->wait_event.tasklet);
Christopher Faulet5a7ca292020-11-03 09:11:43 +0100207
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200208 if (conn && ctx->wait_event.events != 0)
209 conn->xprt->unsubscribe(conn, conn->xprt_ctx, ctx->wait_event.events,
210 &ctx->wait_event);
211 BUG_ON(ctx->endp && !(ctx->endp->flags & CS_EP_ORPHAN));
212 cs_endpoint_free(ctx->endp);
213 pool_free(pool_head_pt_ctx, ctx);
Christopher Faulet5a7ca292020-11-03 09:11:43 +0100214
215 if (conn) {
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200216 conn->mux = NULL;
217 conn->ctx = NULL;
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200218 TRACE_DEVEL("freeing conn", PT_EV_CONN_END, conn);
Christopher Faulet5a7ca292020-11-03 09:11:43 +0100219
220 conn_stop_tracking(conn);
221 conn_full_close(conn);
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200222 if (conn->destroy_cb)
223 conn->destroy_cb(conn);
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200224 conn_free(conn);
225 }
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100226}
227
Willy Tarreau691d5032021-01-20 14:55:01 +0100228/* Callback, used when we get I/Os while in idle mode. This one is exported so
229 * that "show fd" can resolve it.
230 */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100231struct task *mux_pt_io_cb(struct task *t, void *tctx, unsigned int status)
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100232{
233 struct mux_pt_ctx *ctx = tctx;
234
Christopher Faulet1bceee22022-03-24 10:51:08 +0100235 TRACE_ENTER(PT_EV_CONN_WAKE, ctx->conn);
236 if (!(ctx->endp->flags & CS_EP_ORPHAN)) {
Olivier Houchardea510fc2019-10-18 13:56:40 +0200237 /* There's a small race condition.
238 * mux_pt_io_cb() is only supposed to be called if we have no
239 * stream attached. However, maybe the tasklet got woken up,
240 * and this connection was then attached to a new stream.
Olivier Houchard2ed389d2019-10-18 14:18:29 +0200241 * If this happened, just wake the tasklet up if anybody
242 * subscribed to receive events, and otherwise call the wake
243 * method, to make sure the event is noticed.
Olivier Houchardea510fc2019-10-18 13:56:40 +0200244 */
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100245 if (ctx->conn->subs) {
246 ctx->conn->subs->events = 0;
247 tasklet_wakeup(ctx->conn->subs->tasklet);
248 ctx->conn->subs = NULL;
Willy Tarreaucfbfc3f2022-05-10 19:23:39 +0200249 } else if (ctx->endp->cs->data_cb->wake)
250 ctx->endp->cs->data_cb->wake(ctx->endp->cs);
Christopher Faulet1bceee22022-03-24 10:51:08 +0100251 TRACE_DEVEL("leaving waking up CS", PT_EV_CONN_WAKE, ctx->conn);
Willy Tarreau74163142021-03-13 11:30:19 +0100252 return t;
Olivier Houchardea510fc2019-10-18 13:56:40 +0200253 }
Willy Tarreau2ded48d2020-12-11 16:20:34 +0100254 conn_ctrl_drain(ctx->conn);
Willy Tarreau74163142021-03-13 11:30:19 +0100255 if (ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
Christopher Faulete2c65ba2021-04-10 09:02:32 +0200256 TRACE_DEVEL("leaving destroying pt context", PT_EV_CONN_WAKE, ctx->conn);
Olivier Houchard9dce2c52019-10-18 10:59:30 +0200257 mux_pt_destroy(ctx);
Willy Tarreau74163142021-03-13 11:30:19 +0100258 t = NULL;
259 }
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200260 else {
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100261 ctx->conn->xprt->subscribe(ctx->conn, ctx->conn->xprt_ctx, SUB_RETRY_RECV,
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200262 &ctx->wait_event);
Christopher Faulete2c65ba2021-04-10 09:02:32 +0200263 TRACE_DEVEL("leaving subscribing for reads", PT_EV_CONN_WAKE, ctx->conn);
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200264 }
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100265
Willy Tarreau74163142021-03-13 11:30:19 +0100266 return t;
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100267}
Willy Tarreau53a47662017-08-28 10:53:00 +0200268
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100269/* Initialize the mux once it's attached. It is expected that conn->ctx
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200270 * points to the existing conn_stream (for outgoing connections) or NULL (for
271 * incoming ones, in which case one will be allocated and a new stream will be
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500272 * instantiated). Returns < 0 on error.
Willy Tarreau53a47662017-08-28 10:53:00 +0200273 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200274static int mux_pt_init(struct connection *conn, struct proxy *prx, struct session *sess,
275 struct buffer *input)
Willy Tarreau53a47662017-08-28 10:53:00 +0200276{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100277 struct conn_stream *cs = conn->ctx;
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100278 struct mux_pt_ctx *ctx = pool_alloc(pool_head_pt_ctx);
279
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200280 TRACE_ENTER(PT_EV_CONN_NEW);
281
282 if (!ctx) {
283 TRACE_ERROR("PT context allocation failure", PT_EV_CONN_NEW|PT_EV_CONN_END|PT_EV_CONN_ERR);
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100284 goto fail;
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200285 }
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100286
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200287 ctx->wait_event.tasklet = tasklet_new();
288 if (!ctx->wait_event.tasklet)
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100289 goto fail_free_ctx;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200290 ctx->wait_event.tasklet->context = ctx;
291 ctx->wait_event.tasklet->process = mux_pt_io_cb;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100292 ctx->wait_event.events = 0;
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100293 ctx->conn = conn;
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200294
295 if (!cs) {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100296 ctx->endp = cs_endpoint_new();
297 if (!ctx->endp) {
298 TRACE_ERROR("CS allocation failure", PT_EV_STRM_NEW|PT_EV_STRM_END|PT_EV_STRM_ERR, conn);
Christopher Fauletb669d682022-03-22 18:37:19 +0100299 goto fail_free_ctx;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100300 }
301 ctx->endp->target = ctx;
302 ctx->endp->ctx = conn;
303 ctx->endp->flags |= (CS_EP_T_MUX|CS_EP_ORPHAN);
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100304
Willy Tarreau6796a062022-05-11 16:11:24 +0200305 cs = cs_new_from_endp(ctx->endp, sess, input);
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200306 if (!cs) {
307 TRACE_ERROR("CS allocation failure", PT_EV_STRM_NEW|PT_EV_STRM_END|PT_EV_STRM_ERR, conn);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100308 goto fail_free_endp;
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200309 }
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200310 TRACE_POINT(PT_EV_STRM_NEW, conn, cs);
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200311 }
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100312 else {
Christopher Faulet070b91b2022-03-31 19:27:18 +0200313 if (cs_attach_mux(cs, ctx, conn) < 0)
314 goto fail_free_ctx;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100315 ctx->endp = cs->endp;
316 }
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100317 conn->ctx = ctx;
Christopher Faulet1bceee22022-03-24 10:51:08 +0100318 ctx->endp->flags |= CS_EP_RCV_MORE;
Willy Tarreau17ccd1a2020-01-17 16:19:34 +0100319 if (global.tune.options & GTUNE_USE_SPLICE)
Christopher Faulet1bceee22022-03-24 10:51:08 +0100320 ctx->endp->flags |= CS_EP_MAY_SPLICE;
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200321
Christopher Faulet1bceee22022-03-24 10:51:08 +0100322 TRACE_LEAVE(PT_EV_CONN_NEW, conn);
Willy Tarreau53a47662017-08-28 10:53:00 +0200323 return 0;
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200324
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100325 fail_free_endp:
326 cs_endpoint_free(ctx->endp);
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100327 fail_free_ctx:
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200328 if (ctx->wait_event.tasklet)
329 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100330 pool_free(pool_head_pt_ctx, ctx);
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200331 fail:
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200332 TRACE_DEVEL("leaving in error", PT_EV_CONN_NEW|PT_EV_CONN_END|PT_EV_CONN_ERR);
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200333 return -1;
Willy Tarreau53a47662017-08-28 10:53:00 +0200334}
335
336/* callback to be used by default for the pass-through mux. It calls the data
337 * layer wake() callback if it is set otherwise returns 0.
338 */
339static int mux_pt_wake(struct connection *conn)
340{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100341 struct mux_pt_ctx *ctx = conn->ctx;
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100342 int ret = 0;
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200343
Christopher Faulet1bceee22022-03-24 10:51:08 +0100344 TRACE_ENTER(PT_EV_CONN_WAKE, ctx->conn);
345 if (!(ctx->endp->flags & CS_EP_ORPHAN)) {
Willy Tarreaucfbfc3f2022-05-10 19:23:39 +0200346 ret = ctx->endp->cs->data_cb->wake ? ctx->endp->cs->data_cb->wake(ctx->endp->cs) : 0;
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200347
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200348 if (ret < 0) {
Christopher Faulet1bceee22022-03-24 10:51:08 +0100349 TRACE_DEVEL("leaving waking up CS", PT_EV_CONN_WAKE, ctx->conn);
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100350 return ret;
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200351 }
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100352 } else {
Willy Tarreau2ded48d2020-12-11 16:20:34 +0100353 conn_ctrl_drain(conn);
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100354 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH)) {
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200355 TRACE_DEVEL("leaving destroying PT context", PT_EV_CONN_WAKE, ctx->conn);
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100356 mux_pt_destroy(ctx);
357 return -1;
358 }
359 }
Willy Tarreauad7f0ad2018-08-24 15:48:59 +0200360
Olivier Houchard7fc96d52017-11-23 18:25:47 +0100361 /* If we had early data, and we're done with the handshake
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500362 * then we know the data are safe, and we can remove the flag.
Olivier Houchard7fc96d52017-11-23 18:25:47 +0100363 */
Willy Tarreau911db9b2020-01-23 16:27:54 +0100364 if ((conn->flags & (CO_FL_EARLY_DATA | CO_FL_EARLY_SSL_HS | CO_FL_WAIT_XPRT)) ==
Olivier Houchard7fc96d52017-11-23 18:25:47 +0100365 CO_FL_EARLY_DATA)
366 conn->flags &= ~CO_FL_EARLY_DATA;
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200367
368 TRACE_LEAVE(PT_EV_CONN_WAKE, ctx->conn);
Willy Tarreaued339a32017-11-03 15:55:24 +0100369 return ret;
Willy Tarreau53a47662017-08-28 10:53:00 +0200370}
371
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200372/*
373 * Attach a new stream to a connection
374 * (Used for outgoing connections)
375 */
Willy Tarreau4201ab72022-05-10 19:18:52 +0200376static int mux_pt_attach(struct connection *conn, struct cs_endpoint *endp, struct session *sess)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200377{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100378 struct mux_pt_ctx *ctx = conn->ctx;
Olivier Houchard7c6f8b12018-11-13 16:48:36 +0100379
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200380 TRACE_ENTER(PT_EV_STRM_NEW, conn);
Olivier Houchardea32b0f2019-08-10 23:56:16 +0200381 if (ctx->wait_event.events)
382 conn->xprt->unsubscribe(ctx->conn, conn->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event);
Willy Tarreau4201ab72022-05-10 19:18:52 +0200383 if (cs_attach_mux(endp->cs, ctx, conn) < 0)
Christopher Faulet070b91b2022-03-31 19:27:18 +0200384 return -1;
Willy Tarreau4201ab72022-05-10 19:18:52 +0200385 ctx->endp = endp;
Christopher Faulet1bceee22022-03-24 10:51:08 +0100386 ctx->endp->flags |= CS_EP_RCV_MORE;
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200387
Willy Tarreau4201ab72022-05-10 19:18:52 +0200388 TRACE_LEAVE(PT_EV_STRM_NEW, conn, endp->cs);
Christopher Faulete00ad352021-12-16 14:44:31 +0100389 return 0;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200390}
391
Willy Tarreaufafd3982018-11-18 21:29:20 +0100392/* Retrieves a valid conn_stream from this connection, or returns NULL. For
393 * this mux, it's easy as we can only store a single conn_stream.
394 */
Christopher Faulet64b8d332022-04-01 13:21:41 +0200395static struct conn_stream *mux_pt_get_first_cs(const struct connection *conn)
Willy Tarreaufafd3982018-11-18 21:29:20 +0100396{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100397 struct mux_pt_ctx *ctx = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +0100398
Willy Tarreaucfbfc3f2022-05-10 19:23:39 +0200399 return ctx->endp->cs;
Willy Tarreaufafd3982018-11-18 21:29:20 +0100400}
401
Christopher Faulet73c12072019-04-08 11:23:22 +0200402/* Destroy the mux and the associated connection if still attached to this mux
403 * and no longer used */
404static void mux_pt_destroy_meth(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +0100405{
Christopher Faulet73c12072019-04-08 11:23:22 +0200406 struct mux_pt_ctx *pt = ctx;
Olivier Houchard7c6f8b12018-11-13 16:48:36 +0100407
Willy Tarreaucfbfc3f2022-05-10 19:23:39 +0200408 TRACE_POINT(PT_EV_CONN_END, pt->conn, pt->endp->cs);
Christopher Faulet7c452cc2022-04-14 11:08:26 +0200409 if ((pt->endp->flags & CS_EP_ORPHAN) || pt->conn->ctx != pt) {
410 if (pt->conn->ctx != pt) {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100411 pt->endp = NULL;
Christopher Faulet7c452cc2022-04-14 11:08:26 +0200412 }
Christopher Faulet73c12072019-04-08 11:23:22 +0200413 mux_pt_destroy(pt);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100414 }
Olivier Houchard060ed432018-11-06 16:32:42 +0100415}
416
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200417/*
Willy Tarreau2c52a2b2017-10-08 11:00:17 +0200418 * Detach the stream from the connection and possibly release the connection.
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200419 */
Willy Tarreau4201ab72022-05-10 19:18:52 +0200420static void mux_pt_detach(struct cs_endpoint *endp)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200421{
Willy Tarreau4201ab72022-05-10 19:18:52 +0200422 struct connection *conn = endp->ctx;
Christopher Faulet2da02ae2022-02-24 13:45:27 +0100423 struct mux_pt_ctx *ctx;
424
Willy Tarreau4201ab72022-05-10 19:18:52 +0200425 TRACE_ENTER(PT_EV_STRM_END, conn, endp->cs);
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200426
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100427 ctx = conn->ctx;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100428
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100429 /* Subscribe, to know if we got disconnected */
Christopher Fauletfbff8542022-03-09 15:55:58 +0100430 if (!conn_is_back(conn) && conn->owner != NULL &&
Olivier Houchard7c6f8b12018-11-13 16:48:36 +0100431 !(conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100432 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event);
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200433 } else {
Olivier Houchard7c6f8b12018-11-13 16:48:36 +0100434 /* There's no session attached to that connection, destroy it */
Willy Tarreau4201ab72022-05-10 19:18:52 +0200435 TRACE_DEVEL("killing dead connection", PT_EV_STRM_END, conn, endp->cs);
Olivier Houchard7c6f8b12018-11-13 16:48:36 +0100436 mux_pt_destroy(ctx);
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200437 }
438
439 TRACE_LEAVE(PT_EV_STRM_END);
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200440}
441
Willy Tarreau00f18a32019-01-26 12:19:01 +0100442/* returns the number of streams in use on a connection */
443static int mux_pt_used_streams(struct connection *conn)
Olivier Houchardd540b362018-11-05 18:37:53 +0100444{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100445 struct mux_pt_ctx *ctx = conn->ctx;
Olivier Houchardd540b362018-11-05 18:37:53 +0100446
Christopher Faulet1bceee22022-03-24 10:51:08 +0100447 return (!(ctx->endp->flags & CS_EP_ORPHAN) ? 1 : 0);
Olivier Houchardd540b362018-11-05 18:37:53 +0100448}
449
Willy Tarreau00f18a32019-01-26 12:19:01 +0100450/* returns the number of streams still available on a connection */
451static int mux_pt_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100452{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100453 return 1 - mux_pt_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100454}
455
Christopher Faulet07976562022-03-31 11:05:05 +0200456static void mux_pt_shutr(struct conn_stream *cs, enum co_shr_mode mode)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200457{
Christopher Faulet693b23b2022-02-28 09:09:05 +0100458 struct connection *conn = __cs_conn(cs);
Willy Tarreau7a2705f2022-05-10 11:21:15 +0200459 struct mux_pt_ctx *ctx = conn->ctx;
Christopher Faulet897d6122021-12-17 17:28:35 +0100460
461 TRACE_ENTER(PT_EV_STRM_SHUT, conn, cs);
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200462
Willy Tarreau7a2705f2022-05-10 11:21:15 +0200463 if (ctx->endp->flags & CS_EP_SHR)
Willy Tarreau4b795242017-10-05 18:47:38 +0200464 return;
Willy Tarreau7a2705f2022-05-10 11:21:15 +0200465 ctx->endp->flags &= ~(CS_EP_RCV_MORE | CS_EP_WANT_ROOM);
Christopher Faulet897d6122021-12-17 17:28:35 +0100466 if (conn_xprt_ready(conn) && conn->xprt->shutr)
467 conn->xprt->shutr(conn, conn->xprt_ctx,
Christopher Faulet07976562022-03-31 11:05:05 +0200468 (mode == CO_SHR_DRAIN));
469 else if (mode == CO_SHR_DRAIN)
Christopher Faulet897d6122021-12-17 17:28:35 +0100470 conn_ctrl_drain(conn);
Willy Tarreau7a2705f2022-05-10 11:21:15 +0200471 if (ctx->endp->flags & CS_EP_SHW)
Christopher Faulet897d6122021-12-17 17:28:35 +0100472 conn_full_close(conn);
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200473
Christopher Faulet897d6122021-12-17 17:28:35 +0100474 TRACE_LEAVE(PT_EV_STRM_SHUT, conn, cs);
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200475}
476
Christopher Faulet07976562022-03-31 11:05:05 +0200477static void mux_pt_shutw(struct conn_stream *cs, enum co_shw_mode mode)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200478{
Christopher Faulet693b23b2022-02-28 09:09:05 +0100479 struct connection *conn = __cs_conn(cs);
Willy Tarreau7a2705f2022-05-10 11:21:15 +0200480 struct mux_pt_ctx *ctx = conn->ctx;
Christopher Faulet897d6122021-12-17 17:28:35 +0100481
482 TRACE_ENTER(PT_EV_STRM_SHUT, conn, cs);
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200483
Willy Tarreau7a2705f2022-05-10 11:21:15 +0200484 if (ctx->endp->flags & CS_EP_SHW)
Willy Tarreau4b795242017-10-05 18:47:38 +0200485 return;
Christopher Faulet897d6122021-12-17 17:28:35 +0100486 if (conn_xprt_ready(conn) && conn->xprt->shutw)
487 conn->xprt->shutw(conn, conn->xprt_ctx,
Christopher Faulet07976562022-03-31 11:05:05 +0200488 (mode == CO_SHW_NORMAL));
Willy Tarreau7a2705f2022-05-10 11:21:15 +0200489 if (!(ctx->endp->flags & CS_EP_SHR))
Christopher Faulet07976562022-03-31 11:05:05 +0200490 conn_sock_shutw(conn, (mode == CO_SHW_NORMAL));
Willy Tarreau4b795242017-10-05 18:47:38 +0200491 else
Christopher Faulet897d6122021-12-17 17:28:35 +0100492 conn_full_close(conn);
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200493
Christopher Faulet897d6122021-12-17 17:28:35 +0100494 TRACE_LEAVE(PT_EV_STRM_SHUT, conn, cs);
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200495}
496
497/*
498 * Called from the upper layer, to get more data
Christopher Faulet564e39c2021-09-21 15:50:55 +0200499 *
500 * The caller is responsible for defragmenting <buf> if necessary. But <flags>
501 * must be tested to know the calling context. If CO_RFL_BUF_FLUSH is set, it
502 * means the caller wants to flush input data (from the mux buffer and the
503 * channel buffer) to be able to use kernel splicing or any kind of mux-to-mux
504 * xfer. If CO_RFL_KEEP_RECV is set, the mux must always subscribe for read
505 * events before giving back. CO_RFL_BUF_WET is set if <buf> is congested with
506 * data scheduled for leaving soon. CO_RFL_BUF_NOT_STUCK is set to instruct the
507 * mux it may optimize the data copy to <buf> if necessary. Otherwise, it should
508 * copy as much data as possible.
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200509 */
Willy Tarreau7f3225f2018-06-19 06:15:17 +0200510static size_t mux_pt_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200511{
Christopher Faulet693b23b2022-02-28 09:09:05 +0100512 struct connection *conn = __cs_conn(cs);
Willy Tarreau7a2705f2022-05-10 11:21:15 +0200513 struct mux_pt_ctx *ctx = conn->ctx;
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200514 size_t ret = 0;
515
Christopher Faulet897d6122021-12-17 17:28:35 +0100516 TRACE_ENTER(PT_EV_RX_DATA, conn, cs, buf, (size_t[]){count});
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200517
Christopher Faulet4eb7d742018-10-11 15:29:21 +0200518 if (!count) {
Willy Tarreau7a2705f2022-05-10 11:21:15 +0200519 ctx->endp->flags |= (CS_EP_RCV_MORE | CS_EP_WANT_ROOM);
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200520 goto end;
Christopher Faulet4eb7d742018-10-11 15:29:21 +0200521 }
Willy Tarreaue0f24ee2018-12-14 10:51:23 +0100522 b_realign_if_empty(buf);
Christopher Faulet897d6122021-12-17 17:28:35 +0100523 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, count, flags);
524 if (conn_xprt_read0_pending(conn)) {
Willy Tarreau7a2705f2022-05-10 11:21:15 +0200525 ctx->endp->flags &= ~(CS_EP_RCV_MORE | CS_EP_WANT_ROOM);
526 ctx->endp->flags |= CS_EP_EOS;
Christopher Faulet897d6122021-12-17 17:28:35 +0100527 TRACE_DEVEL("read0 on connection", PT_EV_RX_DATA, conn, cs);
Olivier Houchard8706c812018-12-04 19:17:25 +0100528 }
Christopher Faulet897d6122021-12-17 17:28:35 +0100529 if (conn->flags & CO_FL_ERROR) {
Willy Tarreau7a2705f2022-05-10 11:21:15 +0200530 ctx->endp->flags &= ~(CS_EP_RCV_MORE | CS_EP_WANT_ROOM);
531 ctx->endp->flags |= CS_EP_ERROR;
Christopher Faulet897d6122021-12-17 17:28:35 +0100532 TRACE_DEVEL("error on connection", PT_EV_RX_DATA|PT_EV_CONN_ERR, conn, cs);
Olivier Houchard8706c812018-12-04 19:17:25 +0100533 }
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200534 end:
Christopher Faulet897d6122021-12-17 17:28:35 +0100535 TRACE_LEAVE(PT_EV_RX_DATA, conn, cs, buf, (size_t[]){ret});
Willy Tarreaud9cf5402018-07-18 11:29:06 +0200536 return ret;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200537}
538
539/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +0200540static size_t mux_pt_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200541{
Christopher Faulet693b23b2022-02-28 09:09:05 +0100542 struct connection *conn = __cs_conn(cs);
Willy Tarreau7a2705f2022-05-10 11:21:15 +0200543 struct mux_pt_ctx *ctx = conn->ctx;
Olivier Houchardb72d98a2018-11-30 13:17:48 +0100544 size_t ret;
545
Christopher Faulet897d6122021-12-17 17:28:35 +0100546 TRACE_ENTER(PT_EV_TX_DATA, conn, cs, buf, (size_t[]){count});
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200547
Christopher Faulet897d6122021-12-17 17:28:35 +0100548 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, count, flags);
Christopher Fauletd44a9b32018-07-27 11:59:41 +0200549
550 if (ret > 0)
551 b_del(buf, ret);
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200552
Willy Tarreau413713f2022-03-31 16:47:46 +0200553 if (conn->flags & CO_FL_ERROR) {
Willy Tarreau7a2705f2022-05-10 11:21:15 +0200554 ctx->endp->flags |= CS_EP_ERROR;
Willy Tarreau413713f2022-03-31 16:47:46 +0200555 TRACE_DEVEL("error on connection", PT_EV_TX_DATA|PT_EV_CONN_ERR, conn, cs);
556 }
557
Christopher Faulet897d6122021-12-17 17:28:35 +0100558 TRACE_LEAVE(PT_EV_TX_DATA, conn, cs, buf, (size_t[]){ret});
Christopher Fauletd44a9b32018-07-27 11:59:41 +0200559 return ret;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200560}
561
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100562/* Called from the upper layer, to subscribe <es> to events <event_type>. The
563 * event subscriber <es> is not allowed to change from a previous call as long
564 * as at least one event is still subscribed. The <event_type> must only be a
565 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
566 */
567static int mux_pt_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Olivier Houchard6ff20392018-07-17 18:46:31 +0200568{
Christopher Faulet693b23b2022-02-28 09:09:05 +0100569 struct connection *conn = __cs_conn(cs);
Christopher Faulet897d6122021-12-17 17:28:35 +0100570
571 TRACE_POINT(PT_EV_RX_DATA|PT_EV_TX_DATA, conn, cs, 0, (size_t[]){event_type});
572 return conn->xprt->subscribe(conn, conn->xprt_ctx, event_type, es);
Olivier Houchard6ff20392018-07-17 18:46:31 +0200573}
574
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100575/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
576 * The <es> pointer is not allowed to differ from the one passed to the
577 * subscribe() call. It always returns zero.
578 */
579static int mux_pt_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200580{
Christopher Faulet693b23b2022-02-28 09:09:05 +0100581 struct connection *conn = __cs_conn(cs);
Christopher Faulet897d6122021-12-17 17:28:35 +0100582
583 TRACE_POINT(PT_EV_RX_DATA|PT_EV_TX_DATA, conn, cs, 0, (size_t[]){event_type});
584 return conn->xprt->unsubscribe(conn, conn->xprt_ctx, event_type, es);
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200585}
586
Willy Tarreaue5733232019-05-22 19:24:06 +0200587#if defined(USE_LINUX_SPLICE)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200588/* Send and get, using splicing */
589static int mux_pt_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
590{
Christopher Faulet693b23b2022-02-28 09:09:05 +0100591 struct connection *conn = __cs_conn(cs);
Willy Tarreau7a2705f2022-05-10 11:21:15 +0200592 struct mux_pt_ctx *ctx = conn->ctx;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200593 int ret;
594
Christopher Faulet897d6122021-12-17 17:28:35 +0100595 TRACE_ENTER(PT_EV_RX_DATA, conn, cs, 0, (size_t[]){count});
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200596
Christopher Faulet897d6122021-12-17 17:28:35 +0100597 ret = conn->xprt->rcv_pipe(conn, conn->xprt_ctx, pipe, count);
598 if (conn_xprt_read0_pending(conn)) {
Willy Tarreau7a2705f2022-05-10 11:21:15 +0200599 ctx->endp->flags |= CS_EP_EOS;
Christopher Faulet897d6122021-12-17 17:28:35 +0100600 TRACE_DEVEL("read0 on connection", PT_EV_RX_DATA, conn, cs);
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200601 }
Christopher Faulet897d6122021-12-17 17:28:35 +0100602 if (conn->flags & CO_FL_ERROR) {
Willy Tarreau7a2705f2022-05-10 11:21:15 +0200603 ctx->endp->flags |= CS_EP_ERROR;
Christopher Faulet897d6122021-12-17 17:28:35 +0100604 TRACE_DEVEL("error on connection", PT_EV_RX_DATA|PT_EV_CONN_ERR, conn, cs);
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200605 }
606
Christopher Faulet897d6122021-12-17 17:28:35 +0100607 TRACE_LEAVE(PT_EV_RX_DATA, conn, cs, 0, (size_t[]){ret});
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200608 return (ret);
609}
610
611static int mux_pt_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
612{
Christopher Faulet693b23b2022-02-28 09:09:05 +0100613 struct connection *conn = __cs_conn(cs);
Willy Tarreau7a2705f2022-05-10 11:21:15 +0200614 struct mux_pt_ctx *ctx = conn->ctx;
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200615 int ret;
616
Christopher Faulet897d6122021-12-17 17:28:35 +0100617 TRACE_ENTER(PT_EV_TX_DATA, conn, cs, 0, (size_t[]){pipe->data});
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200618
Christopher Faulet897d6122021-12-17 17:28:35 +0100619 ret = conn->xprt->snd_pipe(conn, conn->xprt_ctx, pipe);
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200620
Willy Tarreau413713f2022-03-31 16:47:46 +0200621 if (conn->flags & CO_FL_ERROR) {
Willy Tarreau7a2705f2022-05-10 11:21:15 +0200622 ctx->endp->flags |= CS_EP_ERROR;
Willy Tarreau413713f2022-03-31 16:47:46 +0200623 TRACE_DEVEL("error on connection", PT_EV_TX_DATA|PT_EV_CONN_ERR, conn, cs);
624 }
625
Christopher Faulet897d6122021-12-17 17:28:35 +0100626 TRACE_LEAVE(PT_EV_TX_DATA, conn, cs, 0, (size_t[]){ret});
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200627 return ret;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200628}
Olivier Houchard7da120b2017-11-01 13:55:10 +0100629#endif
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200630
Olivier Houchard9b8e11e2019-10-25 16:19:26 +0200631static int mux_pt_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
632{
633 int ret = 0;
634 switch (mux_ctl) {
635 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +0100636 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +0200637 ret |= MUX_STATUS_READY;
638 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +0200639 case MUX_EXIT_STATUS:
640 return MUX_ES_UNKNOWN;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +0200641 default:
642 return -1;
643 }
644}
645
Willy Tarreau53a47662017-08-28 10:53:00 +0200646/* The mux operations */
Christopher Faulet28da3f52021-02-05 16:44:46 +0100647const struct mux_ops mux_tcp_ops = {
Willy Tarreau53a47662017-08-28 10:53:00 +0200648 .init = mux_pt_init,
Willy Tarreau53a47662017-08-28 10:53:00 +0200649 .wake = mux_pt_wake,
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200650 .rcv_buf = mux_pt_rcv_buf,
651 .snd_buf = mux_pt_snd_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +0200652 .subscribe = mux_pt_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200653 .unsubscribe = mux_pt_unsubscribe,
Willy Tarreaue5733232019-05-22 19:24:06 +0200654#if defined(USE_LINUX_SPLICE)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200655 .rcv_pipe = mux_pt_rcv_pipe,
656 .snd_pipe = mux_pt_snd_pipe,
657#endif
658 .attach = mux_pt_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +0100659 .get_first_cs = mux_pt_get_first_cs,
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200660 .detach = mux_pt_detach,
Olivier Houchardd540b362018-11-05 18:37:53 +0100661 .avail_streams = mux_pt_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +0100662 .used_streams = mux_pt_used_streams,
Olivier Houchard060ed432018-11-06 16:32:42 +0100663 .destroy = mux_pt_destroy_meth,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +0200664 .ctl = mux_pt_ctl,
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200665 .shutr = mux_pt_shutr,
666 .shutw = mux_pt_shutw,
Willy Tarreau28f1cb92017-12-20 16:14:44 +0100667 .flags = MX_FL_NONE,
Willy Tarreau53a47662017-08-28 10:53:00 +0200668 .name = "PASS",
669};
Willy Tarreauf6490822017-09-21 19:43:21 +0200670
Christopher Faulet28da3f52021-02-05 16:44:46 +0100671
672const struct mux_ops mux_pt_ops = {
673 .init = mux_pt_init,
674 .wake = mux_pt_wake,
675 .rcv_buf = mux_pt_rcv_buf,
676 .snd_buf = mux_pt_snd_buf,
677 .subscribe = mux_pt_subscribe,
678 .unsubscribe = mux_pt_unsubscribe,
679#if defined(USE_LINUX_SPLICE)
680 .rcv_pipe = mux_pt_rcv_pipe,
681 .snd_pipe = mux_pt_snd_pipe,
682#endif
683 .attach = mux_pt_attach,
684 .get_first_cs = mux_pt_get_first_cs,
685 .detach = mux_pt_detach,
686 .avail_streams = mux_pt_avail_streams,
687 .used_streams = mux_pt_used_streams,
688 .destroy = mux_pt_destroy_meth,
689 .ctl = mux_pt_ctl,
690 .shutr = mux_pt_shutr,
691 .shutw = mux_pt_shutw,
692 .flags = MX_FL_NONE|MX_FL_NO_UPG,
693 .name = "PASS",
694};
695
Christopher Faulet32f61c02018-04-10 14:33:41 +0200696/* PROT selection : default mux has empty name */
Christopher Faulet28da3f52021-02-05 16:44:46 +0100697static struct mux_proto_list mux_proto_none =
698 { .token = IST("none"), .mode = PROTO_MODE_TCP, .side = PROTO_SIDE_BOTH, .mux = &mux_pt_ops };
699static struct mux_proto_list mux_proto_tcp =
700 { .token = IST(""), .mode = PROTO_MODE_TCP, .side = PROTO_SIDE_BOTH, .mux = &mux_tcp_ops };
Willy Tarreauf6490822017-09-21 19:43:21 +0200701
Christopher Faulet28da3f52021-02-05 16:44:46 +0100702INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_none);
703INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_tcp);