blob: a974ec30202f51d20897146fe3eb736dd506e49c [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
13#include <common/config.h>
14#include <proto/connection.h>
15#include <proto/stream.h>
Olivier Houchardb6c32ee2018-11-05 18:28:43 +010016#include <proto/task.h>
17
18static struct pool_head *pool_head_pt_ctx;
19
20struct mux_pt_ctx {
21 struct conn_stream *cs;
22 struct connection *conn;
23 struct wait_event wait_event;
24};
25
26static void mux_pt_destroy(struct mux_pt_ctx *ctx)
27{
28 struct connection *conn = ctx->conn;
29
30 LIST_DEL(&conn->list);
31 conn_stop_tracking(conn);
32 conn_full_close(conn);
Olivier Houchard7c6f8b12018-11-13 16:48:36 +010033 tasklet_free(ctx->wait_event.task);
34 conn->mux = NULL;
35 conn->mux_ctx = NULL;
Olivier Houchardb6c32ee2018-11-05 18:28:43 +010036 if (conn->destroy_cb)
37 conn->destroy_cb(conn);
38 /* We don't bother unsubscribing here, as we're about to destroy
39 * both the connection and the mux_pt_ctx
40 */
41 conn_free(conn);
42 pool_free(pool_head_pt_ctx, ctx);
43}
44
45/* Callback, used when we get I/Os while in idle mode */
46static struct task *mux_pt_io_cb(struct task *t, void *tctx, unsigned short status)
47{
48 struct mux_pt_ctx *ctx = tctx;
49
50 conn_sock_drain(ctx->conn);
Olivier Houchard7c6f8b12018-11-13 16:48:36 +010051 if (ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))
Olivier Houchardb6c32ee2018-11-05 18:28:43 +010052 mux_pt_destroy(ctx);
53 else
54 ctx->conn->xprt->subscribe(ctx->conn, SUB_CAN_RECV,
55 &ctx->wait_event);
56
57 return NULL;
58}
Willy Tarreau53a47662017-08-28 10:53:00 +020059
Olivier Houchard9aaf7782017-09-13 18:30:23 +020060/* Initialize the mux once it's attached. It is expected that conn->mux_ctx
61 * points to the existing conn_stream (for outgoing connections) or NULL (for
62 * incoming ones, in which case one will be allocated and a new stream will be
63 * instanciated). Returns < 0 on error.
Willy Tarreau53a47662017-08-28 10:53:00 +020064 */
Willy Tarreau175a2bb2018-09-12 12:02:05 +020065static int mux_pt_init(struct connection *conn, struct proxy *prx)
Willy Tarreau53a47662017-08-28 10:53:00 +020066{
Olivier Houchard9aaf7782017-09-13 18:30:23 +020067 struct conn_stream *cs = conn->mux_ctx;
Olivier Houchardb6c32ee2018-11-05 18:28:43 +010068 struct mux_pt_ctx *ctx = pool_alloc(pool_head_pt_ctx);
69
70 if (!ctx)
71 goto fail;
72
73 ctx->wait_event.task = tasklet_new();
74 if (!ctx->wait_event.task)
75 goto fail_free_ctx;
76 ctx->wait_event.task->context = ctx;
77 ctx->wait_event.task->process = mux_pt_io_cb;
78 ctx->wait_event.wait_reason = 0;
79 ctx->conn = conn;
Olivier Houchard9aaf7782017-09-13 18:30:23 +020080
81 if (!cs) {
82 cs = cs_new(conn);
83 if (!cs)
Olivier Houchardb6c32ee2018-11-05 18:28:43 +010084 goto fail_free_ctx;
Olivier Houchard9aaf7782017-09-13 18:30:23 +020085
86 if (stream_create_from_cs(cs) < 0)
87 goto fail_free;
88
Olivier Houchard9aaf7782017-09-13 18:30:23 +020089 }
Olivier Houchardb6c32ee2018-11-05 18:28:43 +010090 conn->mux_ctx = ctx;
91 ctx->cs = cs;
Willy Tarreau53a47662017-08-28 10:53:00 +020092 return 0;
Olivier Houchard9aaf7782017-09-13 18:30:23 +020093
94 fail_free:
95 cs_free(cs);
Olivier Houchardb6c32ee2018-11-05 18:28:43 +010096fail_free_ctx:
97 if (ctx->wait_event.task)
98 tasklet_free(ctx->wait_event.task);
99 pool_free(pool_head_pt_ctx, ctx);
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200100 fail:
101 return -1;
Willy Tarreau53a47662017-08-28 10:53:00 +0200102}
103
104/* callback to be used by default for the pass-through mux. It calls the data
105 * layer wake() callback if it is set otherwise returns 0.
106 */
107static int mux_pt_wake(struct connection *conn)
108{
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100109 struct mux_pt_ctx *ctx = conn->mux_ctx;
110 struct conn_stream *cs = ctx->cs;
111 int ret = 0;
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200112
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100113 if (cs) {
114 ret = cs->data_cb->wake ? cs->data_cb->wake(cs) : 0;
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200115
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100116 if (ret < 0)
117 return ret;
118 } else {
119 conn_sock_drain(conn);
120 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH)) {
121 mux_pt_destroy(ctx);
122 return -1;
123 }
124 }
Willy Tarreauad7f0ad2018-08-24 15:48:59 +0200125
Olivier Houchard7fc96d52017-11-23 18:25:47 +0100126 /* If we had early data, and we're done with the handshake
127 * then whe know the data are safe, and we can remove the flag.
128 */
129 if ((conn->flags & (CO_FL_EARLY_DATA | CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE)) ==
130 CO_FL_EARLY_DATA)
131 conn->flags &= ~CO_FL_EARLY_DATA;
Willy Tarreaued339a32017-11-03 15:55:24 +0100132 return ret;
Willy Tarreau53a47662017-08-28 10:53:00 +0200133}
134
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200135/*
136 * Attach a new stream to a connection
137 * (Used for outgoing connections)
138 */
139static struct conn_stream *mux_pt_attach(struct connection *conn)
140{
Olivier Houchard7c6f8b12018-11-13 16:48:36 +0100141 struct conn_stream *cs;
142 struct mux_pt_ctx *ctx = conn->mux_ctx;
143
144 cs = cs_new(conn);
145 if (!cs)
146 goto fail;
147
148 ctx->cs = cs;
149 return (cs);
150fail:
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200151 return NULL;
152}
153
Willy Tarreaufafd3982018-11-18 21:29:20 +0100154/* Retrieves a valid conn_stream from this connection, or returns NULL. For
155 * this mux, it's easy as we can only store a single conn_stream.
156 */
157static const struct conn_stream *mux_pt_get_first_cs(const struct connection *conn)
158{
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100159 struct mux_pt_ctx *ctx = conn->mux_ctx;
160 struct conn_stream *cs = ctx->cs;
Willy Tarreaufafd3982018-11-18 21:29:20 +0100161
162 return cs;
163}
164
Olivier Houchard7c6f8b12018-11-13 16:48:36 +0100165/* Destroy the mux and the associated connection, if no longer used */
Olivier Houchard060ed432018-11-06 16:32:42 +0100166static void mux_pt_destroy_meth(struct connection *conn)
167{
Olivier Houchard7c6f8b12018-11-13 16:48:36 +0100168 struct mux_pt_ctx *ctx = conn->mux_ctx;
169
170 if (!(ctx->cs))
171 mux_pt_destroy(ctx);
Olivier Houchard060ed432018-11-06 16:32:42 +0100172}
173
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200174/*
Willy Tarreau2c52a2b2017-10-08 11:00:17 +0200175 * Detach the stream from the connection and possibly release the connection.
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200176 */
177static void mux_pt_detach(struct conn_stream *cs)
178{
Willy Tarreau2c52a2b2017-10-08 11:00:17 +0200179 struct connection *conn = cs->conn;
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100180 struct mux_pt_ctx *ctx = cs->conn->mux_ctx;
Willy Tarreau2c52a2b2017-10-08 11:00:17 +0200181
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100182 /* Subscribe, to know if we got disconnected */
Olivier Houchard7c6f8b12018-11-13 16:48:36 +0100183 if (conn->owner != NULL &&
184 !(conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
185 ctx->cs = NULL;
186 conn->xprt->subscribe(conn, SUB_CAN_RECV, &ctx->wait_event);
187 } else
188 /* There's no session attached to that connection, destroy it */
189 mux_pt_destroy(ctx);
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200190}
191
Olivier Houchardd540b362018-11-05 18:37:53 +0100192static int mux_pt_avail_streams(struct connection *conn)
193{
194 struct mux_pt_ctx *ctx = conn->mux_ctx;
195
196 return (ctx->cs == NULL ? 1 : 0);
197}
198
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200199static void mux_pt_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200200{
Willy Tarreau4b795242017-10-05 18:47:38 +0200201 if (cs->flags & CS_FL_SHR)
202 return;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200203 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200204 cs->conn->xprt->shutr(cs->conn, (mode == CS_SHR_DRAIN));
Willy Tarreau4b795242017-10-05 18:47:38 +0200205 if (cs->flags & CS_FL_SHW)
206 conn_full_close(cs->conn);
Olivier Houchard7c6f8b12018-11-13 16:48:36 +0100207 /* Maybe we've been put in the list of available idle connections,
208 * get ouf of here
209 */
210 LIST_DEL(&cs->conn->list);
211 LIST_INIT(&cs->conn->list);
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200212}
213
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200214static void mux_pt_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200215{
Willy Tarreau4b795242017-10-05 18:47:38 +0200216 if (cs->flags & CS_FL_SHW)
217 return;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200218 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutw)
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200219 cs->conn->xprt->shutw(cs->conn, (mode == CS_SHW_NORMAL));
Willy Tarreau4b795242017-10-05 18:47:38 +0200220 if (!(cs->flags & CS_FL_SHR))
Willy Tarreaua48c1412017-12-22 18:46:33 +0100221 conn_sock_shutw(cs->conn, (mode == CS_SHW_NORMAL));
Willy Tarreau4b795242017-10-05 18:47:38 +0200222 else
223 conn_full_close(cs->conn);
Olivier Houchard7c6f8b12018-11-13 16:48:36 +0100224 /* Maybe we've been put in the list of available idle connections,
225 * get ouf of here
226 */
227 LIST_DEL(&cs->conn->list);
228 LIST_INIT(&cs->conn->list);
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200229}
230
231/*
232 * Called from the upper layer, to get more data
233 */
Willy Tarreau7f3225f2018-06-19 06:15:17 +0200234static 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 +0200235{
Willy Tarreaud9cf5402018-07-18 11:29:06 +0200236 size_t ret;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200237
Christopher Faulet4eb7d742018-10-11 15:29:21 +0200238 if (!count) {
239 cs->flags |= CS_FL_RCV_MORE;
240 return 0;
241 }
242 cs->flags &= ~CS_FL_RCV_MORE;
Willy Tarreau7f3225f2018-06-19 06:15:17 +0200243 ret = cs->conn->xprt->rcv_buf(cs->conn, buf, count, flags);
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200244 if (conn_xprt_read0_pending(cs->conn))
245 cs->flags |= CS_FL_EOS;
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200246 if (cs->conn->flags & CO_FL_ERROR)
247 cs->flags |= CS_FL_ERROR;
Willy Tarreaud9cf5402018-07-18 11:29:06 +0200248 return ret;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200249}
250
251/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +0200252static 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 +0200253{
Christopher Fauletd44a9b32018-07-27 11:59:41 +0200254 size_t ret = cs->conn->xprt->snd_buf(cs->conn, buf, count, flags);
255
256 if (ret > 0)
257 b_del(buf, ret);
258 return ret;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200259}
260
Olivier Houchard6ff20392018-07-17 18:46:31 +0200261/* Called from the upper layer, to subscribe to events */
262static int mux_pt_subscribe(struct conn_stream *cs, int event_type, void *param)
263{
264 return (cs->conn->xprt->subscribe(cs->conn, event_type, param));
265}
266
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200267static int mux_pt_unsubscribe(struct conn_stream *cs, int event_type, void *param)
268{
269 return (cs->conn->xprt->unsubscribe(cs->conn, event_type, param));
270}
271
Olivier Houchard7da120b2017-11-01 13:55:10 +0100272#if defined(CONFIG_HAP_LINUX_SPLICE)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200273/* Send and get, using splicing */
274static int mux_pt_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
275{
276 int ret;
277
278 ret = cs->conn->xprt->rcv_pipe(cs->conn, pipe, count);
279 if (conn_xprt_read0_pending(cs->conn))
280 cs->flags |= CS_FL_EOS;
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200281 if (cs->conn->flags & CO_FL_ERROR)
282 cs->flags |= CS_FL_ERROR;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200283 return (ret);
284}
285
286static int mux_pt_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
287{
288 return (cs->conn->xprt->snd_pipe(cs->conn, pipe));
289}
Olivier Houchard7da120b2017-11-01 13:55:10 +0100290#endif
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200291
Willy Tarreau53a47662017-08-28 10:53:00 +0200292/* The mux operations */
293const struct mux_ops mux_pt_ops = {
294 .init = mux_pt_init,
Willy Tarreau53a47662017-08-28 10:53:00 +0200295 .wake = mux_pt_wake,
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200296 .rcv_buf = mux_pt_rcv_buf,
297 .snd_buf = mux_pt_snd_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +0200298 .subscribe = mux_pt_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200299 .unsubscribe = mux_pt_unsubscribe,
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200300#if defined(CONFIG_HAP_LINUX_SPLICE)
301 .rcv_pipe = mux_pt_rcv_pipe,
302 .snd_pipe = mux_pt_snd_pipe,
303#endif
304 .attach = mux_pt_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +0100305 .get_first_cs = mux_pt_get_first_cs,
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200306 .detach = mux_pt_detach,
Olivier Houchardd540b362018-11-05 18:37:53 +0100307 .avail_streams = mux_pt_avail_streams,
Olivier Houchard060ed432018-11-06 16:32:42 +0100308 .destroy = mux_pt_destroy_meth,
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200309 .shutr = mux_pt_shutr,
310 .shutw = mux_pt_shutw,
Willy Tarreau28f1cb92017-12-20 16:14:44 +0100311 .flags = MX_FL_NONE,
Willy Tarreau53a47662017-08-28 10:53:00 +0200312 .name = "PASS",
313};
Willy Tarreauf6490822017-09-21 19:43:21 +0200314
Christopher Faulet32f61c02018-04-10 14:33:41 +0200315/* PROT selection : default mux has empty name */
316static struct mux_proto_list mux_proto_pt =
317 { .token = IST(""), .mode = PROTO_MODE_ANY, .side = PROTO_SIDE_BOTH, .mux = &mux_pt_ops };
Willy Tarreauf6490822017-09-21 19:43:21 +0200318
319__attribute__((constructor))
320static void __mux_pt_init(void)
321{
Christopher Faulet32f61c02018-04-10 14:33:41 +0200322 register_mux_proto(&mux_proto_pt);
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100323 pool_head_pt_ctx = create_pool("mux_pt", sizeof(struct mux_pt_ctx),
324 MEM_F_SHARED);
Willy Tarreauf6490822017-09-21 19:43:21 +0200325}