blob: 82ba124b11a3fffd71a490325ba7f7e745f6a527 [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>
Willy Tarreau0108d902018-11-25 19:14:37 +010014#include <common/initcall.h>
Willy Tarreau53a47662017-08-28 10:53:00 +020015#include <proto/connection.h>
16#include <proto/stream.h>
Olivier Houchardb6c32ee2018-11-05 18:28:43 +010017#include <proto/task.h>
18
Olivier Houchardb6c32ee2018-11-05 18:28:43 +010019struct mux_pt_ctx {
20 struct conn_stream *cs;
21 struct connection *conn;
22 struct wait_event wait_event;
23};
24
Willy Tarreau8ceae722018-11-26 11:58:30 +010025DECLARE_STATIC_POOL(pool_head_pt_ctx, "mux_pt", sizeof(struct mux_pt_ctx));
26
Olivier Houchardb6c32ee2018-11-05 18:28:43 +010027static void mux_pt_destroy(struct mux_pt_ctx *ctx)
28{
29 struct connection *conn = ctx->conn;
30
Christopher Faulet39a96ee2019-04-08 10:52:21 +020031 /* The connection was attached to another mux */
32 if (conn && conn->ctx != ctx)
33 conn = NULL;
34
35 if (conn) {
36 conn_stop_tracking(conn);
37 conn_full_close(conn);
38 tasklet_free(ctx->wait_event.task);
39 conn->mux = NULL;
40 conn->ctx = NULL;
41 if (conn->destroy_cb)
42 conn->destroy_cb(conn);
43 /* We don't bother unsubscribing here, as we're about to destroy
44 * both the connection and the mux_pt_ctx
45 */
46 conn_free(conn);
47 }
Olivier Houchardb6c32ee2018-11-05 18:28:43 +010048 pool_free(pool_head_pt_ctx, ctx);
49}
50
51/* Callback, used when we get I/Os while in idle mode */
52static struct task *mux_pt_io_cb(struct task *t, void *tctx, unsigned short status)
53{
54 struct mux_pt_ctx *ctx = tctx;
55
56 conn_sock_drain(ctx->conn);
Olivier Houchard7c6f8b12018-11-13 16:48:36 +010057 if (ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))
Olivier Houchardb6c32ee2018-11-05 18:28:43 +010058 mux_pt_destroy(ctx);
59 else
Willy Tarreau4f6516d2018-12-19 13:59:17 +010060 ctx->conn->xprt->subscribe(ctx->conn, SUB_RETRY_RECV,
Olivier Houchardb6c32ee2018-11-05 18:28:43 +010061 &ctx->wait_event);
62
63 return NULL;
64}
Willy Tarreau53a47662017-08-28 10:53:00 +020065
Willy Tarreau3d2ee552018-12-19 14:12:10 +010066/* Initialize the mux once it's attached. It is expected that conn->ctx
Olivier Houchard9aaf7782017-09-13 18:30:23 +020067 * points to the existing conn_stream (for outgoing connections) or NULL (for
68 * incoming ones, in which case one will be allocated and a new stream will be
69 * instanciated). Returns < 0 on error.
Willy Tarreau53a47662017-08-28 10:53:00 +020070 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +020071static int mux_pt_init(struct connection *conn, struct proxy *prx, struct session *sess,
72 struct buffer *input)
Willy Tarreau53a47662017-08-28 10:53:00 +020073{
Willy Tarreau3d2ee552018-12-19 14:12:10 +010074 struct conn_stream *cs = conn->ctx;
Olivier Houchardb6c32ee2018-11-05 18:28:43 +010075 struct mux_pt_ctx *ctx = pool_alloc(pool_head_pt_ctx);
76
77 if (!ctx)
78 goto fail;
79
80 ctx->wait_event.task = tasklet_new();
81 if (!ctx->wait_event.task)
82 goto fail_free_ctx;
83 ctx->wait_event.task->context = ctx;
84 ctx->wait_event.task->process = mux_pt_io_cb;
Willy Tarreau4f6516d2018-12-19 13:59:17 +010085 ctx->wait_event.events = 0;
Olivier Houchardb6c32ee2018-11-05 18:28:43 +010086 ctx->conn = conn;
Olivier Houchard9aaf7782017-09-13 18:30:23 +020087
88 if (!cs) {
89 cs = cs_new(conn);
90 if (!cs)
Olivier Houchardb6c32ee2018-11-05 18:28:43 +010091 goto fail_free_ctx;
Olivier Houchard9aaf7782017-09-13 18:30:23 +020092
93 if (stream_create_from_cs(cs) < 0)
94 goto fail_free;
95
Olivier Houchard9aaf7782017-09-13 18:30:23 +020096 }
Willy Tarreau3d2ee552018-12-19 14:12:10 +010097 conn->ctx = ctx;
Olivier Houchardb6c32ee2018-11-05 18:28:43 +010098 ctx->cs = cs;
Olivier Houchard8706c812018-12-04 19:17:25 +010099 cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau53a47662017-08-28 10:53:00 +0200100 return 0;
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200101
102 fail_free:
103 cs_free(cs);
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100104fail_free_ctx:
105 if (ctx->wait_event.task)
106 tasklet_free(ctx->wait_event.task);
107 pool_free(pool_head_pt_ctx, ctx);
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200108 fail:
109 return -1;
Willy Tarreau53a47662017-08-28 10:53:00 +0200110}
111
112/* callback to be used by default for the pass-through mux. It calls the data
113 * layer wake() callback if it is set otherwise returns 0.
114 */
115static int mux_pt_wake(struct connection *conn)
116{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100117 struct mux_pt_ctx *ctx = conn->ctx;
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100118 struct conn_stream *cs = ctx->cs;
119 int ret = 0;
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200120
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100121 if (cs) {
122 ret = cs->data_cb->wake ? cs->data_cb->wake(cs) : 0;
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200123
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100124 if (ret < 0)
125 return ret;
126 } else {
127 conn_sock_drain(conn);
128 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH)) {
129 mux_pt_destroy(ctx);
130 return -1;
131 }
132 }
Willy Tarreauad7f0ad2018-08-24 15:48:59 +0200133
Olivier Houchard7fc96d52017-11-23 18:25:47 +0100134 /* If we had early data, and we're done with the handshake
135 * then whe know the data are safe, and we can remove the flag.
136 */
137 if ((conn->flags & (CO_FL_EARLY_DATA | CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE)) ==
138 CO_FL_EARLY_DATA)
139 conn->flags &= ~CO_FL_EARLY_DATA;
Willy Tarreaued339a32017-11-03 15:55:24 +0100140 return ret;
Willy Tarreau53a47662017-08-28 10:53:00 +0200141}
142
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200143/*
144 * Attach a new stream to a connection
145 * (Used for outgoing connections)
146 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100147static struct conn_stream *mux_pt_attach(struct connection *conn, struct session *sess)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200148{
Olivier Houchard7c6f8b12018-11-13 16:48:36 +0100149 struct conn_stream *cs;
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100150 struct mux_pt_ctx *ctx = conn->ctx;
Olivier Houchard7c6f8b12018-11-13 16:48:36 +0100151
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100152 conn->xprt->unsubscribe(conn, SUB_RETRY_RECV, &ctx->wait_event);
Olivier Houchard7c6f8b12018-11-13 16:48:36 +0100153 cs = cs_new(conn);
154 if (!cs)
155 goto fail;
156
157 ctx->cs = cs;
Olivier Houchard8706c812018-12-04 19:17:25 +0100158 cs->flags |= CS_FL_RCV_MORE;
Olivier Houchard7c6f8b12018-11-13 16:48:36 +0100159 return (cs);
160fail:
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200161 return NULL;
162}
163
Willy Tarreaufafd3982018-11-18 21:29:20 +0100164/* Retrieves a valid conn_stream from this connection, or returns NULL. For
165 * this mux, it's easy as we can only store a single conn_stream.
166 */
167static const struct conn_stream *mux_pt_get_first_cs(const struct connection *conn)
168{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100169 struct mux_pt_ctx *ctx = conn->ctx;
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100170 struct conn_stream *cs = ctx->cs;
Willy Tarreaufafd3982018-11-18 21:29:20 +0100171
172 return cs;
173}
174
Christopher Faulet73c12072019-04-08 11:23:22 +0200175/* Destroy the mux and the associated connection if still attached to this mux
176 * and no longer used */
177static void mux_pt_destroy_meth(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +0100178{
Christopher Faulet73c12072019-04-08 11:23:22 +0200179 struct mux_pt_ctx *pt = ctx;
Olivier Houchard7c6f8b12018-11-13 16:48:36 +0100180
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200181 if (!(pt->cs) || !(pt->conn) || pt->conn->ctx != pt)
Christopher Faulet73c12072019-04-08 11:23:22 +0200182 mux_pt_destroy(pt);
Olivier Houchard060ed432018-11-06 16:32:42 +0100183}
184
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200185/*
Willy Tarreau2c52a2b2017-10-08 11:00:17 +0200186 * Detach the stream from the connection and possibly release the connection.
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200187 */
188static void mux_pt_detach(struct conn_stream *cs)
189{
Willy Tarreau2c52a2b2017-10-08 11:00:17 +0200190 struct connection *conn = cs->conn;
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100191 struct mux_pt_ctx *ctx = cs->conn->ctx;
Willy Tarreau2c52a2b2017-10-08 11:00:17 +0200192
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100193 /* Subscribe, to know if we got disconnected */
Olivier Houchard7c6f8b12018-11-13 16:48:36 +0100194 if (conn->owner != NULL &&
195 !(conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
196 ctx->cs = NULL;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100197 conn->xprt->subscribe(conn, SUB_RETRY_RECV, &ctx->wait_event);
Olivier Houchard7c6f8b12018-11-13 16:48:36 +0100198 } else
199 /* There's no session attached to that connection, destroy it */
200 mux_pt_destroy(ctx);
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200201}
202
Willy Tarreau00f18a32019-01-26 12:19:01 +0100203/* returns the number of streams in use on a connection */
204static int mux_pt_used_streams(struct connection *conn)
Olivier Houchardd540b362018-11-05 18:37:53 +0100205{
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100206 struct mux_pt_ctx *ctx = conn->ctx;
Olivier Houchardd540b362018-11-05 18:37:53 +0100207
Willy Tarreau00f18a32019-01-26 12:19:01 +0100208 return ctx->cs ? 1 : 0;
Olivier Houchardd540b362018-11-05 18:37:53 +0100209}
210
Willy Tarreau00f18a32019-01-26 12:19:01 +0100211/* returns the number of streams still available on a connection */
212static int mux_pt_avail_streams(struct connection *conn)
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100213{
Willy Tarreau00f18a32019-01-26 12:19:01 +0100214 return 1 - mux_pt_used_streams(conn);
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100215}
216
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200217static void mux_pt_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200218{
Willy Tarreau4b795242017-10-05 18:47:38 +0200219 if (cs->flags & CS_FL_SHR)
220 return;
Christopher Fauletd94f8772018-12-17 13:21:02 +0100221 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200222 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200223 cs->conn->xprt->shutr(cs->conn, (mode == CS_SHR_DRAIN));
Willy Tarreau4b795242017-10-05 18:47:38 +0200224 if (cs->flags & CS_FL_SHW)
225 conn_full_close(cs->conn);
Olivier Houchard7c6f8b12018-11-13 16:48:36 +0100226 /* Maybe we've been put in the list of available idle connections,
227 * get ouf of here
228 */
229 LIST_DEL(&cs->conn->list);
230 LIST_INIT(&cs->conn->list);
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200231}
232
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200233static void mux_pt_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200234{
Willy Tarreau4b795242017-10-05 18:47:38 +0200235 if (cs->flags & CS_FL_SHW)
236 return;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200237 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutw)
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200238 cs->conn->xprt->shutw(cs->conn, (mode == CS_SHW_NORMAL));
Willy Tarreau4b795242017-10-05 18:47:38 +0200239 if (!(cs->flags & CS_FL_SHR))
Willy Tarreaua48c1412017-12-22 18:46:33 +0100240 conn_sock_shutw(cs->conn, (mode == CS_SHW_NORMAL));
Willy Tarreau4b795242017-10-05 18:47:38 +0200241 else
242 conn_full_close(cs->conn);
Olivier Houchard7c6f8b12018-11-13 16:48:36 +0100243 /* Maybe we've been put in the list of available idle connections,
244 * get ouf of here
245 */
246 LIST_DEL(&cs->conn->list);
247 LIST_INIT(&cs->conn->list);
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200248}
249
250/*
251 * Called from the upper layer, to get more data
252 */
Willy Tarreau7f3225f2018-06-19 06:15:17 +0200253static 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 +0200254{
Willy Tarreaud9cf5402018-07-18 11:29:06 +0200255 size_t ret;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200256
Christopher Faulet4eb7d742018-10-11 15:29:21 +0200257 if (!count) {
Christopher Fauletd94f8772018-12-17 13:21:02 +0100258 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Faulet4eb7d742018-10-11 15:29:21 +0200259 return 0;
260 }
Willy Tarreaue0f24ee2018-12-14 10:51:23 +0100261 b_realign_if_empty(buf);
Willy Tarreau7f3225f2018-06-19 06:15:17 +0200262 ret = cs->conn->xprt->rcv_buf(cs->conn, buf, count, flags);
Olivier Houchard8706c812018-12-04 19:17:25 +0100263 if (conn_xprt_read0_pending(cs->conn)) {
264 if (ret == 0)
Christopher Fauletd94f8772018-12-17 13:21:02 +0100265 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Faulet87a8f352019-03-22 14:51:36 +0100266 cs->flags |= CS_FL_EOS;
Olivier Houchard8706c812018-12-04 19:17:25 +0100267 }
268 if (cs->conn->flags & CO_FL_ERROR) {
269 if (ret == 0)
Christopher Fauletd94f8772018-12-17 13:21:02 +0100270 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200271 cs->flags |= CS_FL_ERROR;
Olivier Houchard8706c812018-12-04 19:17:25 +0100272 }
Willy Tarreaud9cf5402018-07-18 11:29:06 +0200273 return ret;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200274}
275
276/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +0200277static 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 +0200278{
Olivier Houchardb72d98a2018-11-30 13:17:48 +0100279 size_t ret;
280
281 if (cs->conn->flags & CO_FL_HANDSHAKE)
282 return 0;
283 ret = cs->conn->xprt->snd_buf(cs->conn, buf, count, flags);
Christopher Fauletd44a9b32018-07-27 11:59:41 +0200284
285 if (ret > 0)
286 b_del(buf, ret);
287 return ret;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200288}
289
Olivier Houchard6ff20392018-07-17 18:46:31 +0200290/* Called from the upper layer, to subscribe to events */
291static int mux_pt_subscribe(struct conn_stream *cs, int event_type, void *param)
292{
293 return (cs->conn->xprt->subscribe(cs->conn, event_type, param));
294}
295
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200296static int mux_pt_unsubscribe(struct conn_stream *cs, int event_type, void *param)
297{
298 return (cs->conn->xprt->unsubscribe(cs->conn, event_type, param));
299}
300
Olivier Houchard7da120b2017-11-01 13:55:10 +0100301#if defined(CONFIG_HAP_LINUX_SPLICE)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200302/* Send and get, using splicing */
303static int mux_pt_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
304{
305 int ret;
306
307 ret = cs->conn->xprt->rcv_pipe(cs->conn, pipe, count);
308 if (conn_xprt_read0_pending(cs->conn))
Christopher Faulet87a8f352019-03-22 14:51:36 +0100309 cs->flags |= CS_FL_EOS;
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200310 if (cs->conn->flags & CO_FL_ERROR)
311 cs->flags |= CS_FL_ERROR;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200312 return (ret);
313}
314
315static int mux_pt_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
316{
317 return (cs->conn->xprt->snd_pipe(cs->conn, pipe));
318}
Olivier Houchard7da120b2017-11-01 13:55:10 +0100319#endif
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200320
Willy Tarreau53a47662017-08-28 10:53:00 +0200321/* The mux operations */
322const struct mux_ops mux_pt_ops = {
323 .init = mux_pt_init,
Willy Tarreau53a47662017-08-28 10:53:00 +0200324 .wake = mux_pt_wake,
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200325 .rcv_buf = mux_pt_rcv_buf,
326 .snd_buf = mux_pt_snd_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +0200327 .subscribe = mux_pt_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200328 .unsubscribe = mux_pt_unsubscribe,
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200329#if defined(CONFIG_HAP_LINUX_SPLICE)
330 .rcv_pipe = mux_pt_rcv_pipe,
331 .snd_pipe = mux_pt_snd_pipe,
332#endif
333 .attach = mux_pt_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +0100334 .get_first_cs = mux_pt_get_first_cs,
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200335 .detach = mux_pt_detach,
Olivier Houchardd540b362018-11-05 18:37:53 +0100336 .avail_streams = mux_pt_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +0100337 .used_streams = mux_pt_used_streams,
Olivier Houchard060ed432018-11-06 16:32:42 +0100338 .destroy = mux_pt_destroy_meth,
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200339 .shutr = mux_pt_shutr,
340 .shutw = mux_pt_shutw,
Willy Tarreau28f1cb92017-12-20 16:14:44 +0100341 .flags = MX_FL_NONE,
Willy Tarreau53a47662017-08-28 10:53:00 +0200342 .name = "PASS",
343};
Willy Tarreauf6490822017-09-21 19:43:21 +0200344
Christopher Faulet32f61c02018-04-10 14:33:41 +0200345/* PROT selection : default mux has empty name */
346static struct mux_proto_list mux_proto_pt =
347 { .token = IST(""), .mode = PROTO_MODE_ANY, .side = PROTO_SIDE_BOTH, .mux = &mux_pt_ops };
Willy Tarreauf6490822017-09-21 19:43:21 +0200348
Willy Tarreau0108d902018-11-25 19:14:37 +0100349INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_pt);