blob: 5f4c041ea2206c73e257887358169eb72580a208 [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 {
Willy Tarreauea59b022022-05-17 17:53:22 +020023 struct sedesc *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
Willy Tarreau32c095b2022-05-18 07:36:10 +020094/* returns the stconn associated to the stream */
95static forceinline struct stconn *pt_sc(const struct mux_pt_ctx *pt)
96{
Willy Tarreauc1054922022-05-18 07:43:52 +020097 return pt->endp->sc;
Willy Tarreau32c095b2022-05-18 07:36:10 +020098}
99
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200100static inline void pt_trace_buf(const struct buffer *buf, size_t ofs, size_t len)
101{
102 size_t block1, block2;
103 int line, ptr, newptr;
104
105 block1 = b_contig_data(buf, ofs);
106 block2 = 0;
107 if (block1 > len)
108 block1 = len;
109 block2 = len - block1;
110
111 ofs = b_peek_ofs(buf, ofs);
112
113 line = 0;
114 ptr = ofs;
115 while (ptr < ofs + block1) {
116 newptr = dump_text_line(&trace_buf, b_orig(buf), b_size(buf), ofs + block1, &line, ptr);
117 if (newptr == ptr)
118 break;
119 ptr = newptr;
120 }
121
122 line = ptr = 0;
123 while (ptr < block2) {
124 newptr = dump_text_line(&trace_buf, b_orig(buf), b_size(buf), block2, &line, ptr);
125 if (newptr == ptr)
126 break;
127 ptr = newptr;
128 }
129}
130
131/* the PT traces always expect that arg1, if non-null, is of type connection
132 * (from which we can derive the pt context), that arg2, if non-null, is a
Willy Tarreau4596fe22022-05-17 19:07:51 +0200133 * stream connector, and that arg3, if non-null, is a buffer.
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200134 */
135static void pt_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
136 const struct ist where, const struct ist func,
137 const void *a1, const void *a2, const void *a3, const void *a4)
138{
139 const struct connection *conn = a1;
140 const struct mux_pt_ctx *ctx = conn ? conn->ctx : NULL;
Willy Tarreau4596fe22022-05-17 19:07:51 +0200141 const struct stconn *cs = a2;
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200142 const struct buffer *buf = a3;
143 const size_t *val = a4;
144
Christopher Faulet1bceee22022-03-24 10:51:08 +0100145 if (!ctx || src->verbosity < PT_VERB_CLEAN)
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200146 return;
147
148 /* Display frontend/backend info by default */
149 chunk_appendf(&trace_buf, " : [%c]", (conn_is_back(conn) ? 'B' : 'F'));
150
151 if (src->verbosity == PT_VERB_CLEAN)
152 return;
153
Christopher Faulet1bceee22022-03-24 10:51:08 +0100154 if (!cs)
Willy Tarreau32c095b2022-05-18 07:36:10 +0200155 cs = pt_sc(ctx);
Christopher Faulet1bceee22022-03-24 10:51:08 +0100156
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200157 /* Display the value to the 4th argument (level > STATE) */
158 if (src->level > TRACE_LEVEL_STATE && val)
159 chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
160
161 /* Display conn and cs info, if defined (pointer + flags) */
162 chunk_appendf(&trace_buf, " - conn=%p(0x%08x)", conn, conn->flags);
Willy Tarreaud56377c2022-05-17 16:31:36 +0200163 chunk_appendf(&trace_buf, " endp=%p(0x%08x)", ctx->endp, se_fl_get(ctx->endp));
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200164 if (cs)
165 chunk_appendf(&trace_buf, " cs=%p(0x%08x)", cs, cs->flags);
166
167 if (src->verbosity == PT_VERB_MINIMAL)
168 return;
169
170 /* Display buffer info, if defined (level > USER & verbosity > SIMPLE) */
171 if (src->level > TRACE_LEVEL_USER && buf) {
172 int full = 0, max = 3000, chunk = 1024;
173
174 /* Full info (level > STATE && verbosity > SIMPLE) */
175 if (src->level > TRACE_LEVEL_STATE) {
176 if (src->verbosity == PT_VERB_COMPLETE)
177 full = 1;
178 else if (src->verbosity == PT_VERB_ADVANCED) {
179 full = 1;
180 max = 256;
181 chunk = 64;
182 }
183 }
184
185 chunk_appendf(&trace_buf, " buf=%u@%p+%u/%u",
186 (unsigned int)b_data(buf), b_orig(buf),
187 (unsigned int)b_head_ofs(buf), (unsigned int)b_size(buf));
188
189 if (b_data(buf) && full) {
190 chunk_memcat(&trace_buf, "\n", 1);
191 if (b_data(buf) < max)
192 pt_trace_buf(buf, 0, b_data(buf));
193 else {
194 pt_trace_buf(buf, 0, chunk);
195 chunk_memcat(&trace_buf, " ...\n", 6);
196 pt_trace_buf(buf, b_data(buf) - chunk, chunk);
197 }
198 }
199 }
200}
201
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100202static void mux_pt_destroy(struct mux_pt_ctx *ctx)
203{
Christopher Faulet5a7ca292020-11-03 09:11:43 +0100204 struct connection *conn = NULL;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200205
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200206 TRACE_POINT(PT_EV_CONN_END);
207
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200208 /* The connection must be attached to this mux to be released */
209 if (ctx->conn && ctx->conn->ctx == ctx)
210 conn = ctx->conn;
Christopher Faulet5a7ca292020-11-03 09:11:43 +0100211
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200212 tasklet_free(ctx->wait_event.tasklet);
Christopher Faulet5a7ca292020-11-03 09:11:43 +0100213
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200214 if (conn && ctx->wait_event.events != 0)
215 conn->xprt->unsubscribe(conn, conn->xprt_ctx, ctx->wait_event.events,
216 &ctx->wait_event);
Willy Tarreaub605c422022-05-17 17:04:55 +0200217 BUG_ON(ctx->endp && !se_fl_test(ctx->endp, SE_FL_ORPHAN));
Willy Tarreauea59b022022-05-17 17:53:22 +0200218 sedesc_free(ctx->endp);
Christopher Faulet4de1bff2022-04-14 11:36:41 +0200219 pool_free(pool_head_pt_ctx, ctx);
Christopher Faulet5a7ca292020-11-03 09:11:43 +0100220
221 if (conn) {
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200222 conn->mux = NULL;
223 conn->ctx = NULL;
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200224 TRACE_DEVEL("freeing conn", PT_EV_CONN_END, conn);
Christopher Faulet5a7ca292020-11-03 09:11:43 +0100225
226 conn_stop_tracking(conn);
227 conn_full_close(conn);
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200228 if (conn->destroy_cb)
229 conn->destroy_cb(conn);
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200230 conn_free(conn);
231 }
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100232}
233
Willy Tarreau691d5032021-01-20 14:55:01 +0100234/* Callback, used when we get I/Os while in idle mode. This one is exported so
235 * that "show fd" can resolve it.
236 */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100237struct task *mux_pt_io_cb(struct task *t, void *tctx, unsigned int status)
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100238{
239 struct mux_pt_ctx *ctx = tctx;
240
Christopher Faulet1bceee22022-03-24 10:51:08 +0100241 TRACE_ENTER(PT_EV_CONN_WAKE, ctx->conn);
Willy Tarreaub605c422022-05-17 17:04:55 +0200242 if (!se_fl_test(ctx->endp, SE_FL_ORPHAN)) {
Olivier Houchardea510fc2019-10-18 13:56:40 +0200243 /* There's a small race condition.
244 * mux_pt_io_cb() is only supposed to be called if we have no
245 * stream attached. However, maybe the tasklet got woken up,
246 * and this connection was then attached to a new stream.
Olivier Houchard2ed389d2019-10-18 14:18:29 +0200247 * If this happened, just wake the tasklet up if anybody
248 * subscribed to receive events, and otherwise call the wake
249 * method, to make sure the event is noticed.
Olivier Houchardea510fc2019-10-18 13:56:40 +0200250 */
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100251 if (ctx->conn->subs) {
252 ctx->conn->subs->events = 0;
253 tasklet_wakeup(ctx->conn->subs->tasklet);
254 ctx->conn->subs = NULL;
Willy Tarreau2f2318d2022-05-18 10:17:16 +0200255 } else if (pt_sc(ctx)->app_ops->wake)
256 pt_sc(ctx)->app_ops->wake(pt_sc(ctx));
Christopher Faulet1bceee22022-03-24 10:51:08 +0100257 TRACE_DEVEL("leaving waking up CS", PT_EV_CONN_WAKE, ctx->conn);
Willy Tarreau74163142021-03-13 11:30:19 +0100258 return t;
Olivier Houchardea510fc2019-10-18 13:56:40 +0200259 }
Willy Tarreau2ded48d2020-12-11 16:20:34 +0100260 conn_ctrl_drain(ctx->conn);
Willy Tarreau74163142021-03-13 11:30:19 +0100261 if (ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) {
Christopher Faulete2c65ba2021-04-10 09:02:32 +0200262 TRACE_DEVEL("leaving destroying pt context", PT_EV_CONN_WAKE, ctx->conn);
Olivier Houchard9dce2c52019-10-18 10:59:30 +0200263 mux_pt_destroy(ctx);
Willy Tarreau74163142021-03-13 11:30:19 +0100264 t = NULL;
265 }
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200266 else {
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100267 ctx->conn->xprt->subscribe(ctx->conn, ctx->conn->xprt_ctx, SUB_RETRY_RECV,
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200268 &ctx->wait_event);
Christopher Faulete2c65ba2021-04-10 09:02:32 +0200269 TRACE_DEVEL("leaving subscribing for reads", PT_EV_CONN_WAKE, ctx->conn);
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200270 }
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100271
Willy Tarreau74163142021-03-13 11:30:19 +0100272 return t;
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100273}
Willy Tarreau53a47662017-08-28 10:53:00 +0200274
Willy Tarreau4596fe22022-05-17 19:07:51 +0200275/* Initialize the mux once it's attached. It is expected that conn->ctx points
276 * to the existing stream connector (for outgoing connections) or NULL (for
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200277 * incoming ones, in which case one will be allocated and a new stream will be
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500278 * instantiated). Returns < 0 on error.
Willy Tarreau53a47662017-08-28 10:53:00 +0200279 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200280static int mux_pt_init(struct connection *conn, struct proxy *prx, struct session *sess,
281 struct buffer *input)
Willy Tarreau53a47662017-08-28 10:53:00 +0200282{
Willy Tarreau4596fe22022-05-17 19:07:51 +0200283 struct stconn *cs = conn->ctx;
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100284 struct mux_pt_ctx *ctx = pool_alloc(pool_head_pt_ctx);
285
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200286 TRACE_ENTER(PT_EV_CONN_NEW);
287
288 if (!ctx) {
289 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 +0100290 goto fail;
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200291 }
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100292
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200293 ctx->wait_event.tasklet = tasklet_new();
294 if (!ctx->wait_event.tasklet)
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100295 goto fail_free_ctx;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200296 ctx->wait_event.tasklet->context = ctx;
297 ctx->wait_event.tasklet->process = mux_pt_io_cb;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100298 ctx->wait_event.events = 0;
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100299 ctx->conn = conn;
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200300
301 if (!cs) {
Willy Tarreauea59b022022-05-17 17:53:22 +0200302 ctx->endp = sedesc_new();
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100303 if (!ctx->endp) {
304 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 +0100305 goto fail_free_ctx;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100306 }
Willy Tarreau65d05972022-05-16 17:29:42 +0200307 ctx->endp->se = ctx;
Willy Tarreau24d15b12022-05-16 17:17:16 +0200308 ctx->endp->conn = conn;
Willy Tarreaub605c422022-05-17 17:04:55 +0200309 se_fl_set(ctx->endp, SE_FL_T_MUX | SE_FL_ORPHAN);
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100310
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200311 cs = sc_new_from_endp(ctx->endp, sess, input);
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200312 if (!cs) {
313 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 +0100314 goto fail_free_endp;
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200315 }
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200316 TRACE_POINT(PT_EV_STRM_NEW, conn, cs);
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200317 }
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100318 else {
Christopher Faulet070b91b2022-03-31 19:27:18 +0200319 if (cs_attach_mux(cs, ctx, conn) < 0)
320 goto fail_free_ctx;
Willy Tarreau798465b2022-05-17 18:20:02 +0200321 ctx->endp = cs->sedesc;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100322 }
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100323 conn->ctx = ctx;
Willy Tarreaub605c422022-05-17 17:04:55 +0200324 se_fl_set(ctx->endp, SE_FL_RCV_MORE);
Willy Tarreau17ccd1a2020-01-17 16:19:34 +0100325 if (global.tune.options & GTUNE_USE_SPLICE)
Willy Tarreaub605c422022-05-17 17:04:55 +0200326 se_fl_set(ctx->endp, SE_FL_MAY_SPLICE);
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200327
Christopher Faulet1bceee22022-03-24 10:51:08 +0100328 TRACE_LEAVE(PT_EV_CONN_NEW, conn);
Willy Tarreau53a47662017-08-28 10:53:00 +0200329 return 0;
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200330
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100331 fail_free_endp:
Willy Tarreauea59b022022-05-17 17:53:22 +0200332 sedesc_free(ctx->endp);
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100333 fail_free_ctx:
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200334 if (ctx->wait_event.tasklet)
335 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100336 pool_free(pool_head_pt_ctx, ctx);
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200337 fail:
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200338 TRACE_DEVEL("leaving in error", PT_EV_CONN_NEW|PT_EV_CONN_END|PT_EV_CONN_ERR);
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200339 return -1;
Willy Tarreau53a47662017-08-28 10:53:00 +0200340}
341
342/* callback to be used by default for the pass-through mux. It calls the data
343 * layer wake() callback if it is set otherwise returns 0.
344 */
345static int mux_pt_wake(struct connection *conn)
346{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100347 struct mux_pt_ctx *ctx = conn->ctx;
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100348 int ret = 0;
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200349
Christopher Faulet1bceee22022-03-24 10:51:08 +0100350 TRACE_ENTER(PT_EV_CONN_WAKE, ctx->conn);
Willy Tarreaub605c422022-05-17 17:04:55 +0200351 if (!se_fl_test(ctx->endp, SE_FL_ORPHAN)) {
Willy Tarreau2f2318d2022-05-18 10:17:16 +0200352 ret = pt_sc(ctx)->app_ops->wake ? pt_sc(ctx)->app_ops->wake(pt_sc(ctx)) : 0;
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200353
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200354 if (ret < 0) {
Christopher Faulet1bceee22022-03-24 10:51:08 +0100355 TRACE_DEVEL("leaving waking up CS", PT_EV_CONN_WAKE, ctx->conn);
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100356 return ret;
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200357 }
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100358 } else {
Willy Tarreau2ded48d2020-12-11 16:20:34 +0100359 conn_ctrl_drain(conn);
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100360 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH)) {
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200361 TRACE_DEVEL("leaving destroying PT context", PT_EV_CONN_WAKE, ctx->conn);
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100362 mux_pt_destroy(ctx);
363 return -1;
364 }
365 }
Willy Tarreauad7f0ad2018-08-24 15:48:59 +0200366
Olivier Houchard7fc96d52017-11-23 18:25:47 +0100367 /* If we had early data, and we're done with the handshake
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500368 * then we know the data are safe, and we can remove the flag.
Olivier Houchard7fc96d52017-11-23 18:25:47 +0100369 */
Willy Tarreau911db9b2020-01-23 16:27:54 +0100370 if ((conn->flags & (CO_FL_EARLY_DATA | CO_FL_EARLY_SSL_HS | CO_FL_WAIT_XPRT)) ==
Olivier Houchard7fc96d52017-11-23 18:25:47 +0100371 CO_FL_EARLY_DATA)
372 conn->flags &= ~CO_FL_EARLY_DATA;
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200373
374 TRACE_LEAVE(PT_EV_CONN_WAKE, ctx->conn);
Willy Tarreaued339a32017-11-03 15:55:24 +0100375 return ret;
Willy Tarreau53a47662017-08-28 10:53:00 +0200376}
377
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200378/*
379 * Attach a new stream to a connection
380 * (Used for outgoing connections)
381 */
Willy Tarreauea59b022022-05-17 17:53:22 +0200382static int mux_pt_attach(struct connection *conn, struct sedesc *endp, struct session *sess)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200383{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100384 struct mux_pt_ctx *ctx = conn->ctx;
Olivier Houchard7c6f8b12018-11-13 16:48:36 +0100385
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200386 TRACE_ENTER(PT_EV_STRM_NEW, conn);
Olivier Houchardea32b0f2019-08-10 23:56:16 +0200387 if (ctx->wait_event.events)
388 conn->xprt->unsubscribe(ctx->conn, conn->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event);
Willy Tarreauc1054922022-05-18 07:43:52 +0200389 if (cs_attach_mux(endp->sc, ctx, conn) < 0)
Christopher Faulet070b91b2022-03-31 19:27:18 +0200390 return -1;
Willy Tarreau4201ab72022-05-10 19:18:52 +0200391 ctx->endp = endp;
Willy Tarreaub605c422022-05-17 17:04:55 +0200392 se_fl_set(ctx->endp, SE_FL_RCV_MORE);
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200393
Willy Tarreauc1054922022-05-18 07:43:52 +0200394 TRACE_LEAVE(PT_EV_STRM_NEW, conn, endp->sc);
Christopher Faulete00ad352021-12-16 14:44:31 +0100395 return 0;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200396}
397
Willy Tarreau4596fe22022-05-17 19:07:51 +0200398/* Retrieves a valid stream connector from this connection, or returns NULL.
399 * For this mux, it's easy as we can only store a single stream connector.
Willy Tarreaufafd3982018-11-18 21:29:20 +0100400 */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200401static struct stconn *mux_pt_get_first_cs(const struct connection *conn)
Willy Tarreaufafd3982018-11-18 21:29:20 +0100402{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100403 struct mux_pt_ctx *ctx = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +0100404
Willy Tarreau32c095b2022-05-18 07:36:10 +0200405 return pt_sc(ctx);
Willy Tarreaufafd3982018-11-18 21:29:20 +0100406}
407
Christopher Faulet73c12072019-04-08 11:23:22 +0200408/* Destroy the mux and the associated connection if still attached to this mux
409 * and no longer used */
410static void mux_pt_destroy_meth(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +0100411{
Christopher Faulet73c12072019-04-08 11:23:22 +0200412 struct mux_pt_ctx *pt = ctx;
Olivier Houchard7c6f8b12018-11-13 16:48:36 +0100413
Willy Tarreau32c095b2022-05-18 07:36:10 +0200414 TRACE_POINT(PT_EV_CONN_END, pt->conn, pt_sc(pt));
Willy Tarreaub605c422022-05-17 17:04:55 +0200415 if (se_fl_test(pt->endp, SE_FL_ORPHAN) || pt->conn->ctx != pt) {
Christopher Faulet7c452cc2022-04-14 11:08:26 +0200416 if (pt->conn->ctx != pt) {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100417 pt->endp = NULL;
Christopher Faulet7c452cc2022-04-14 11:08:26 +0200418 }
Christopher Faulet73c12072019-04-08 11:23:22 +0200419 mux_pt_destroy(pt);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100420 }
Olivier Houchard060ed432018-11-06 16:32:42 +0100421}
422
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200423/*
Willy Tarreau2c52a2b2017-10-08 11:00:17 +0200424 * Detach the stream from the connection and possibly release the connection.
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200425 */
Willy Tarreauea59b022022-05-17 17:53:22 +0200426static void mux_pt_detach(struct sedesc *endp)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200427{
Willy Tarreau24d15b12022-05-16 17:17:16 +0200428 struct connection *conn = endp->conn;
Christopher Faulet2da02ae2022-02-24 13:45:27 +0100429 struct mux_pt_ctx *ctx;
430
Willy Tarreauc1054922022-05-18 07:43:52 +0200431 TRACE_ENTER(PT_EV_STRM_END, conn, endp->sc);
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200432
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100433 ctx = conn->ctx;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100434
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100435 /* Subscribe, to know if we got disconnected */
Christopher Fauletfbff8542022-03-09 15:55:58 +0100436 if (!conn_is_back(conn) && conn->owner != NULL &&
Olivier Houchard7c6f8b12018-11-13 16:48:36 +0100437 !(conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100438 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event);
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200439 } else {
Olivier Houchard7c6f8b12018-11-13 16:48:36 +0100440 /* There's no session attached to that connection, destroy it */
Willy Tarreauc1054922022-05-18 07:43:52 +0200441 TRACE_DEVEL("killing dead connection", PT_EV_STRM_END, conn, endp->sc);
Olivier Houchard7c6f8b12018-11-13 16:48:36 +0100442 mux_pt_destroy(ctx);
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200443 }
444
445 TRACE_LEAVE(PT_EV_STRM_END);
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200446}
447
Willy Tarreau00f18a32019-01-26 12:19:01 +0100448/* returns the number of streams in use on a connection */
449static int mux_pt_used_streams(struct connection *conn)
Olivier Houchardd540b362018-11-05 18:37:53 +0100450{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100451 struct mux_pt_ctx *ctx = conn->ctx;
Olivier Houchardd540b362018-11-05 18:37:53 +0100452
Willy Tarreaub605c422022-05-17 17:04:55 +0200453 return (!se_fl_test(ctx->endp, SE_FL_ORPHAN) ? 1 : 0);
Olivier Houchardd540b362018-11-05 18:37:53 +0100454}
455
Willy Tarreau00f18a32019-01-26 12:19:01 +0100456/* returns the number of streams still available on a connection */
457static int mux_pt_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100458{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100459 return 1 - mux_pt_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100460}
461
Willy Tarreau4596fe22022-05-17 19:07:51 +0200462static void mux_pt_shutr(struct stconn *cs, enum co_shr_mode mode)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200463{
Willy Tarreaufd9417b2022-05-18 16:23:22 +0200464 struct connection *conn = __sc_conn(cs);
Willy Tarreau7a2705f2022-05-10 11:21:15 +0200465 struct mux_pt_ctx *ctx = conn->ctx;
Christopher Faulet897d6122021-12-17 17:28:35 +0100466
467 TRACE_ENTER(PT_EV_STRM_SHUT, conn, cs);
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200468
Willy Tarreaub605c422022-05-17 17:04:55 +0200469 if (se_fl_test(ctx->endp, SE_FL_SHR))
Willy Tarreau4b795242017-10-05 18:47:38 +0200470 return;
Willy Tarreaub605c422022-05-17 17:04:55 +0200471 se_fl_clr(ctx->endp, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Christopher Faulet897d6122021-12-17 17:28:35 +0100472 if (conn_xprt_ready(conn) && conn->xprt->shutr)
473 conn->xprt->shutr(conn, conn->xprt_ctx,
Christopher Faulet07976562022-03-31 11:05:05 +0200474 (mode == CO_SHR_DRAIN));
475 else if (mode == CO_SHR_DRAIN)
Christopher Faulet897d6122021-12-17 17:28:35 +0100476 conn_ctrl_drain(conn);
Willy Tarreaub605c422022-05-17 17:04:55 +0200477 if (se_fl_test(ctx->endp, SE_FL_SHW))
Christopher Faulet897d6122021-12-17 17:28:35 +0100478 conn_full_close(conn);
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200479
Christopher Faulet897d6122021-12-17 17:28:35 +0100480 TRACE_LEAVE(PT_EV_STRM_SHUT, conn, cs);
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200481}
482
Willy Tarreau4596fe22022-05-17 19:07:51 +0200483static void mux_pt_shutw(struct stconn *cs, enum co_shw_mode mode)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200484{
Willy Tarreaufd9417b2022-05-18 16:23:22 +0200485 struct connection *conn = __sc_conn(cs);
Willy Tarreau7a2705f2022-05-10 11:21:15 +0200486 struct mux_pt_ctx *ctx = conn->ctx;
Christopher Faulet897d6122021-12-17 17:28:35 +0100487
488 TRACE_ENTER(PT_EV_STRM_SHUT, conn, cs);
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200489
Willy Tarreaub605c422022-05-17 17:04:55 +0200490 if (se_fl_test(ctx->endp, SE_FL_SHW))
Willy Tarreau4b795242017-10-05 18:47:38 +0200491 return;
Christopher Faulet897d6122021-12-17 17:28:35 +0100492 if (conn_xprt_ready(conn) && conn->xprt->shutw)
493 conn->xprt->shutw(conn, conn->xprt_ctx,
Christopher Faulet07976562022-03-31 11:05:05 +0200494 (mode == CO_SHW_NORMAL));
Willy Tarreaub605c422022-05-17 17:04:55 +0200495 if (!se_fl_test(ctx->endp, SE_FL_SHR))
Christopher Faulet07976562022-03-31 11:05:05 +0200496 conn_sock_shutw(conn, (mode == CO_SHW_NORMAL));
Willy Tarreau4b795242017-10-05 18:47:38 +0200497 else
Christopher Faulet897d6122021-12-17 17:28:35 +0100498 conn_full_close(conn);
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200499
Christopher Faulet897d6122021-12-17 17:28:35 +0100500 TRACE_LEAVE(PT_EV_STRM_SHUT, conn, cs);
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200501}
502
503/*
504 * Called from the upper layer, to get more data
Christopher Faulet564e39c2021-09-21 15:50:55 +0200505 *
506 * The caller is responsible for defragmenting <buf> if necessary. But <flags>
507 * must be tested to know the calling context. If CO_RFL_BUF_FLUSH is set, it
508 * means the caller wants to flush input data (from the mux buffer and the
509 * channel buffer) to be able to use kernel splicing or any kind of mux-to-mux
510 * xfer. If CO_RFL_KEEP_RECV is set, the mux must always subscribe for read
511 * events before giving back. CO_RFL_BUF_WET is set if <buf> is congested with
512 * data scheduled for leaving soon. CO_RFL_BUF_NOT_STUCK is set to instruct the
513 * mux it may optimize the data copy to <buf> if necessary. Otherwise, it should
514 * copy as much data as possible.
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200515 */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200516static size_t mux_pt_rcv_buf(struct stconn *cs, struct buffer *buf, size_t count, int flags)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200517{
Willy Tarreaufd9417b2022-05-18 16:23:22 +0200518 struct connection *conn = __sc_conn(cs);
Willy Tarreau7a2705f2022-05-10 11:21:15 +0200519 struct mux_pt_ctx *ctx = conn->ctx;
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200520 size_t ret = 0;
521
Christopher Faulet897d6122021-12-17 17:28:35 +0100522 TRACE_ENTER(PT_EV_RX_DATA, conn, cs, buf, (size_t[]){count});
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200523
Christopher Faulet4eb7d742018-10-11 15:29:21 +0200524 if (!count) {
Willy Tarreaub605c422022-05-17 17:04:55 +0200525 se_fl_set(ctx->endp, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200526 goto end;
Christopher Faulet4eb7d742018-10-11 15:29:21 +0200527 }
Willy Tarreaue0f24ee2018-12-14 10:51:23 +0100528 b_realign_if_empty(buf);
Christopher Faulet897d6122021-12-17 17:28:35 +0100529 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, count, flags);
530 if (conn_xprt_read0_pending(conn)) {
Willy Tarreaub605c422022-05-17 17:04:55 +0200531 se_fl_clr(ctx->endp, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
532 se_fl_set(ctx->endp, SE_FL_EOS);
Christopher Faulet897d6122021-12-17 17:28:35 +0100533 TRACE_DEVEL("read0 on connection", PT_EV_RX_DATA, conn, cs);
Olivier Houchard8706c812018-12-04 19:17:25 +0100534 }
Christopher Faulet897d6122021-12-17 17:28:35 +0100535 if (conn->flags & CO_FL_ERROR) {
Willy Tarreaub605c422022-05-17 17:04:55 +0200536 se_fl_clr(ctx->endp, SE_FL_RCV_MORE | SE_FL_WANT_ROOM);
537 se_fl_set(ctx->endp, SE_FL_ERROR);
Christopher Faulet897d6122021-12-17 17:28:35 +0100538 TRACE_DEVEL("error on connection", PT_EV_RX_DATA|PT_EV_CONN_ERR, conn, cs);
Olivier Houchard8706c812018-12-04 19:17:25 +0100539 }
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200540 end:
Christopher Faulet897d6122021-12-17 17:28:35 +0100541 TRACE_LEAVE(PT_EV_RX_DATA, conn, cs, buf, (size_t[]){ret});
Willy Tarreaud9cf5402018-07-18 11:29:06 +0200542 return ret;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200543}
544
545/* Called from the upper layer, to send data */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200546static size_t mux_pt_snd_buf(struct stconn *cs, struct buffer *buf, size_t count, int flags)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200547{
Willy Tarreaufd9417b2022-05-18 16:23:22 +0200548 struct connection *conn = __sc_conn(cs);
Willy Tarreau7a2705f2022-05-10 11:21:15 +0200549 struct mux_pt_ctx *ctx = conn->ctx;
Olivier Houchardb72d98a2018-11-30 13:17:48 +0100550 size_t ret;
551
Christopher Faulet897d6122021-12-17 17:28:35 +0100552 TRACE_ENTER(PT_EV_TX_DATA, conn, cs, buf, (size_t[]){count});
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200553
Christopher Faulet897d6122021-12-17 17:28:35 +0100554 ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, count, flags);
Christopher Fauletd44a9b32018-07-27 11:59:41 +0200555
556 if (ret > 0)
557 b_del(buf, ret);
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200558
Willy Tarreau413713f2022-03-31 16:47:46 +0200559 if (conn->flags & CO_FL_ERROR) {
Willy Tarreaub605c422022-05-17 17:04:55 +0200560 se_fl_set(ctx->endp, SE_FL_ERROR);
Willy Tarreau413713f2022-03-31 16:47:46 +0200561 TRACE_DEVEL("error on connection", PT_EV_TX_DATA|PT_EV_CONN_ERR, conn, cs);
562 }
563
Christopher Faulet897d6122021-12-17 17:28:35 +0100564 TRACE_LEAVE(PT_EV_TX_DATA, conn, cs, buf, (size_t[]){ret});
Christopher Fauletd44a9b32018-07-27 11:59:41 +0200565 return ret;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200566}
567
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100568/* Called from the upper layer, to subscribe <es> to events <event_type>. The
569 * event subscriber <es> is not allowed to change from a previous call as long
570 * as at least one event is still subscribed. The <event_type> must only be a
571 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
572 */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200573static int mux_pt_subscribe(struct stconn *cs, int event_type, struct wait_event *es)
Olivier Houchard6ff20392018-07-17 18:46:31 +0200574{
Willy Tarreaufd9417b2022-05-18 16:23:22 +0200575 struct connection *conn = __sc_conn(cs);
Christopher Faulet897d6122021-12-17 17:28:35 +0100576
577 TRACE_POINT(PT_EV_RX_DATA|PT_EV_TX_DATA, conn, cs, 0, (size_t[]){event_type});
578 return conn->xprt->subscribe(conn, conn->xprt_ctx, event_type, es);
Olivier Houchard6ff20392018-07-17 18:46:31 +0200579}
580
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100581/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
582 * The <es> pointer is not allowed to differ from the one passed to the
583 * subscribe() call. It always returns zero.
584 */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200585static int mux_pt_unsubscribe(struct stconn *cs, int event_type, struct wait_event *es)
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200586{
Willy Tarreaufd9417b2022-05-18 16:23:22 +0200587 struct connection *conn = __sc_conn(cs);
Christopher Faulet897d6122021-12-17 17:28:35 +0100588
589 TRACE_POINT(PT_EV_RX_DATA|PT_EV_TX_DATA, conn, cs, 0, (size_t[]){event_type});
590 return conn->xprt->unsubscribe(conn, conn->xprt_ctx, event_type, es);
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200591}
592
Willy Tarreaue5733232019-05-22 19:24:06 +0200593#if defined(USE_LINUX_SPLICE)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200594/* Send and get, using splicing */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200595static int mux_pt_rcv_pipe(struct stconn *cs, struct pipe *pipe, unsigned int count)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200596{
Willy Tarreaufd9417b2022-05-18 16:23:22 +0200597 struct connection *conn = __sc_conn(cs);
Willy Tarreau7a2705f2022-05-10 11:21:15 +0200598 struct mux_pt_ctx *ctx = conn->ctx;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200599 int ret;
600
Christopher Faulet897d6122021-12-17 17:28:35 +0100601 TRACE_ENTER(PT_EV_RX_DATA, conn, cs, 0, (size_t[]){count});
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200602
Christopher Faulet897d6122021-12-17 17:28:35 +0100603 ret = conn->xprt->rcv_pipe(conn, conn->xprt_ctx, pipe, count);
604 if (conn_xprt_read0_pending(conn)) {
Willy Tarreaub605c422022-05-17 17:04:55 +0200605 se_fl_set(ctx->endp, SE_FL_EOS);
Christopher Faulet897d6122021-12-17 17:28:35 +0100606 TRACE_DEVEL("read0 on connection", PT_EV_RX_DATA, conn, cs);
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200607 }
Christopher Faulet897d6122021-12-17 17:28:35 +0100608 if (conn->flags & CO_FL_ERROR) {
Willy Tarreaub605c422022-05-17 17:04:55 +0200609 se_fl_set(ctx->endp, SE_FL_ERROR);
Christopher Faulet897d6122021-12-17 17:28:35 +0100610 TRACE_DEVEL("error on connection", PT_EV_RX_DATA|PT_EV_CONN_ERR, conn, cs);
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200611 }
612
Christopher Faulet897d6122021-12-17 17:28:35 +0100613 TRACE_LEAVE(PT_EV_RX_DATA, conn, cs, 0, (size_t[]){ret});
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200614 return (ret);
615}
616
Willy Tarreau4596fe22022-05-17 19:07:51 +0200617static int mux_pt_snd_pipe(struct stconn *cs, struct pipe *pipe)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200618{
Willy Tarreaufd9417b2022-05-18 16:23:22 +0200619 struct connection *conn = __sc_conn(cs);
Willy Tarreau7a2705f2022-05-10 11:21:15 +0200620 struct mux_pt_ctx *ctx = conn->ctx;
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200621 int ret;
622
Christopher Faulet897d6122021-12-17 17:28:35 +0100623 TRACE_ENTER(PT_EV_TX_DATA, conn, cs, 0, (size_t[]){pipe->data});
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200624
Christopher Faulet897d6122021-12-17 17:28:35 +0100625 ret = conn->xprt->snd_pipe(conn, conn->xprt_ctx, pipe);
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200626
Willy Tarreau413713f2022-03-31 16:47:46 +0200627 if (conn->flags & CO_FL_ERROR) {
Willy Tarreaub605c422022-05-17 17:04:55 +0200628 se_fl_set(ctx->endp, SE_FL_ERROR);
Willy Tarreau413713f2022-03-31 16:47:46 +0200629 TRACE_DEVEL("error on connection", PT_EV_TX_DATA|PT_EV_CONN_ERR, conn, cs);
630 }
631
Christopher Faulet897d6122021-12-17 17:28:35 +0100632 TRACE_LEAVE(PT_EV_TX_DATA, conn, cs, 0, (size_t[]){ret});
Christopher Fauletc0ae0972021-04-08 16:45:11 +0200633 return ret;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200634}
Olivier Houchard7da120b2017-11-01 13:55:10 +0100635#endif
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200636
Olivier Houchard9b8e11e2019-10-25 16:19:26 +0200637static int mux_pt_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
638{
639 int ret = 0;
640 switch (mux_ctl) {
641 case MUX_STATUS:
Willy Tarreau911db9b2020-01-23 16:27:54 +0100642 if (!(conn->flags & CO_FL_WAIT_XPRT))
Olivier Houchard9b8e11e2019-10-25 16:19:26 +0200643 ret |= MUX_STATUS_READY;
644 return ret;
Christopher Faulet4c8ad842020-10-06 14:59:17 +0200645 case MUX_EXIT_STATUS:
646 return MUX_ES_UNKNOWN;
Olivier Houchard9b8e11e2019-10-25 16:19:26 +0200647 default:
648 return -1;
649 }
650}
651
Willy Tarreau53a47662017-08-28 10:53:00 +0200652/* The mux operations */
Christopher Faulet28da3f52021-02-05 16:44:46 +0100653const struct mux_ops mux_tcp_ops = {
Willy Tarreau53a47662017-08-28 10:53:00 +0200654 .init = mux_pt_init,
Willy Tarreau53a47662017-08-28 10:53:00 +0200655 .wake = mux_pt_wake,
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200656 .rcv_buf = mux_pt_rcv_buf,
657 .snd_buf = mux_pt_snd_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +0200658 .subscribe = mux_pt_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200659 .unsubscribe = mux_pt_unsubscribe,
Willy Tarreaue5733232019-05-22 19:24:06 +0200660#if defined(USE_LINUX_SPLICE)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200661 .rcv_pipe = mux_pt_rcv_pipe,
662 .snd_pipe = mux_pt_snd_pipe,
663#endif
664 .attach = mux_pt_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +0100665 .get_first_cs = mux_pt_get_first_cs,
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200666 .detach = mux_pt_detach,
Olivier Houchardd540b362018-11-05 18:37:53 +0100667 .avail_streams = mux_pt_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +0100668 .used_streams = mux_pt_used_streams,
Olivier Houchard060ed432018-11-06 16:32:42 +0100669 .destroy = mux_pt_destroy_meth,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +0200670 .ctl = mux_pt_ctl,
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200671 .shutr = mux_pt_shutr,
672 .shutw = mux_pt_shutw,
Willy Tarreau28f1cb92017-12-20 16:14:44 +0100673 .flags = MX_FL_NONE,
Willy Tarreau53a47662017-08-28 10:53:00 +0200674 .name = "PASS",
675};
Willy Tarreauf6490822017-09-21 19:43:21 +0200676
Christopher Faulet28da3f52021-02-05 16:44:46 +0100677
678const struct mux_ops mux_pt_ops = {
679 .init = mux_pt_init,
680 .wake = mux_pt_wake,
681 .rcv_buf = mux_pt_rcv_buf,
682 .snd_buf = mux_pt_snd_buf,
683 .subscribe = mux_pt_subscribe,
684 .unsubscribe = mux_pt_unsubscribe,
685#if defined(USE_LINUX_SPLICE)
686 .rcv_pipe = mux_pt_rcv_pipe,
687 .snd_pipe = mux_pt_snd_pipe,
688#endif
689 .attach = mux_pt_attach,
690 .get_first_cs = mux_pt_get_first_cs,
691 .detach = mux_pt_detach,
692 .avail_streams = mux_pt_avail_streams,
693 .used_streams = mux_pt_used_streams,
694 .destroy = mux_pt_destroy_meth,
695 .ctl = mux_pt_ctl,
696 .shutr = mux_pt_shutr,
697 .shutw = mux_pt_shutw,
698 .flags = MX_FL_NONE|MX_FL_NO_UPG,
699 .name = "PASS",
700};
701
Christopher Faulet32f61c02018-04-10 14:33:41 +0200702/* PROT selection : default mux has empty name */
Christopher Faulet28da3f52021-02-05 16:44:46 +0100703static struct mux_proto_list mux_proto_none =
704 { .token = IST("none"), .mode = PROTO_MODE_TCP, .side = PROTO_SIDE_BOTH, .mux = &mux_pt_ops };
705static struct mux_proto_list mux_proto_tcp =
706 { .token = IST(""), .mode = PROTO_MODE_TCP, .side = PROTO_SIDE_BOTH, .mux = &mux_tcp_ops };
Willy Tarreauf6490822017-09-21 19:43:21 +0200707
Christopher Faulet28da3f52021-02-05 16:44:46 +0100708INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_none);
709INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_tcp);