blob: 8e9c651fde71ce3e6c8c701e410798ebf173944d [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
19static struct pool_head *pool_head_pt_ctx;
20
21struct mux_pt_ctx {
22 struct conn_stream *cs;
23 struct connection *conn;
24 struct wait_event wait_event;
25};
26
27static void mux_pt_destroy(struct mux_pt_ctx *ctx)
28{
29 struct connection *conn = ctx->conn;
30
31 LIST_DEL(&conn->list);
32 conn_stop_tracking(conn);
33 conn_full_close(conn);
Olivier Houchard7c6f8b12018-11-13 16:48:36 +010034 tasklet_free(ctx->wait_event.task);
35 conn->mux = NULL;
36 conn->mux_ctx = NULL;
Olivier Houchardb6c32ee2018-11-05 18:28:43 +010037 if (conn->destroy_cb)
38 conn->destroy_cb(conn);
39 /* We don't bother unsubscribing here, as we're about to destroy
40 * both the connection and the mux_pt_ctx
41 */
42 conn_free(conn);
43 pool_free(pool_head_pt_ctx, ctx);
44}
45
46/* Callback, used when we get I/Os while in idle mode */
47static struct task *mux_pt_io_cb(struct task *t, void *tctx, unsigned short status)
48{
49 struct mux_pt_ctx *ctx = tctx;
50
51 conn_sock_drain(ctx->conn);
Olivier Houchard7c6f8b12018-11-13 16:48:36 +010052 if (ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))
Olivier Houchardb6c32ee2018-11-05 18:28:43 +010053 mux_pt_destroy(ctx);
54 else
55 ctx->conn->xprt->subscribe(ctx->conn, SUB_CAN_RECV,
56 &ctx->wait_event);
57
58 return NULL;
59}
Willy Tarreau53a47662017-08-28 10:53:00 +020060
Olivier Houchard9aaf7782017-09-13 18:30:23 +020061/* Initialize the mux once it's attached. It is expected that conn->mux_ctx
62 * points to the existing conn_stream (for outgoing connections) or NULL (for
63 * incoming ones, in which case one will be allocated and a new stream will be
64 * instanciated). Returns < 0 on error.
Willy Tarreau53a47662017-08-28 10:53:00 +020065 */
Willy Tarreau175a2bb2018-09-12 12:02:05 +020066static int mux_pt_init(struct connection *conn, struct proxy *prx)
Willy Tarreau53a47662017-08-28 10:53:00 +020067{
Olivier Houchard9aaf7782017-09-13 18:30:23 +020068 struct conn_stream *cs = conn->mux_ctx;
Olivier Houchardb6c32ee2018-11-05 18:28:43 +010069 struct mux_pt_ctx *ctx = pool_alloc(pool_head_pt_ctx);
70
71 if (!ctx)
72 goto fail;
73
74 ctx->wait_event.task = tasklet_new();
75 if (!ctx->wait_event.task)
76 goto fail_free_ctx;
77 ctx->wait_event.task->context = ctx;
78 ctx->wait_event.task->process = mux_pt_io_cb;
79 ctx->wait_event.wait_reason = 0;
80 ctx->conn = conn;
Olivier Houchard9aaf7782017-09-13 18:30:23 +020081
82 if (!cs) {
83 cs = cs_new(conn);
84 if (!cs)
Olivier Houchardb6c32ee2018-11-05 18:28:43 +010085 goto fail_free_ctx;
Olivier Houchard9aaf7782017-09-13 18:30:23 +020086
87 if (stream_create_from_cs(cs) < 0)
88 goto fail_free;
89
Olivier Houchard9aaf7782017-09-13 18:30:23 +020090 }
Olivier Houchardb6c32ee2018-11-05 18:28:43 +010091 conn->mux_ctx = ctx;
92 ctx->cs = cs;
Willy Tarreau53a47662017-08-28 10:53:00 +020093 return 0;
Olivier Houchard9aaf7782017-09-13 18:30:23 +020094
95 fail_free:
96 cs_free(cs);
Olivier Houchardb6c32ee2018-11-05 18:28:43 +010097fail_free_ctx:
98 if (ctx->wait_event.task)
99 tasklet_free(ctx->wait_event.task);
100 pool_free(pool_head_pt_ctx, ctx);
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200101 fail:
102 return -1;
Willy Tarreau53a47662017-08-28 10:53:00 +0200103}
104
105/* callback to be used by default for the pass-through mux. It calls the data
106 * layer wake() callback if it is set otherwise returns 0.
107 */
108static int mux_pt_wake(struct connection *conn)
109{
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100110 struct mux_pt_ctx *ctx = conn->mux_ctx;
111 struct conn_stream *cs = ctx->cs;
112 int ret = 0;
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200113
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100114 if (cs) {
115 ret = cs->data_cb->wake ? cs->data_cb->wake(cs) : 0;
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200116
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100117 if (ret < 0)
118 return ret;
119 } else {
120 conn_sock_drain(conn);
121 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH)) {
122 mux_pt_destroy(ctx);
123 return -1;
124 }
125 }
Willy Tarreauad7f0ad2018-08-24 15:48:59 +0200126
Olivier Houchard7fc96d52017-11-23 18:25:47 +0100127 /* If we had early data, and we're done with the handshake
128 * then whe know the data are safe, and we can remove the flag.
129 */
130 if ((conn->flags & (CO_FL_EARLY_DATA | CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE)) ==
131 CO_FL_EARLY_DATA)
132 conn->flags &= ~CO_FL_EARLY_DATA;
Willy Tarreaued339a32017-11-03 15:55:24 +0100133 return ret;
Willy Tarreau53a47662017-08-28 10:53:00 +0200134}
135
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200136/*
137 * Attach a new stream to a connection
138 * (Used for outgoing connections)
139 */
140static struct conn_stream *mux_pt_attach(struct connection *conn)
141{
Olivier Houchard7c6f8b12018-11-13 16:48:36 +0100142 struct conn_stream *cs;
143 struct mux_pt_ctx *ctx = conn->mux_ctx;
144
145 cs = cs_new(conn);
146 if (!cs)
147 goto fail;
148
149 ctx->cs = cs;
150 return (cs);
151fail:
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200152 return NULL;
153}
154
Willy Tarreaufafd3982018-11-18 21:29:20 +0100155/* Retrieves a valid conn_stream from this connection, or returns NULL. For
156 * this mux, it's easy as we can only store a single conn_stream.
157 */
158static const struct conn_stream *mux_pt_get_first_cs(const struct connection *conn)
159{
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100160 struct mux_pt_ctx *ctx = conn->mux_ctx;
161 struct conn_stream *cs = ctx->cs;
Willy Tarreaufafd3982018-11-18 21:29:20 +0100162
163 return cs;
164}
165
Olivier Houchard7c6f8b12018-11-13 16:48:36 +0100166/* Destroy the mux and the associated connection, if no longer used */
Olivier Houchard060ed432018-11-06 16:32:42 +0100167static void mux_pt_destroy_meth(struct connection *conn)
168{
Olivier Houchard7c6f8b12018-11-13 16:48:36 +0100169 struct mux_pt_ctx *ctx = conn->mux_ctx;
170
171 if (!(ctx->cs))
172 mux_pt_destroy(ctx);
Olivier Houchard060ed432018-11-06 16:32:42 +0100173}
174
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200175/*
Willy Tarreau2c52a2b2017-10-08 11:00:17 +0200176 * Detach the stream from the connection and possibly release the connection.
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200177 */
178static void mux_pt_detach(struct conn_stream *cs)
179{
Willy Tarreau2c52a2b2017-10-08 11:00:17 +0200180 struct connection *conn = cs->conn;
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100181 struct mux_pt_ctx *ctx = cs->conn->mux_ctx;
Willy Tarreau2c52a2b2017-10-08 11:00:17 +0200182
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100183 /* Subscribe, to know if we got disconnected */
Olivier Houchard7c6f8b12018-11-13 16:48:36 +0100184 if (conn->owner != NULL &&
185 !(conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
186 ctx->cs = NULL;
187 conn->xprt->subscribe(conn, SUB_CAN_RECV, &ctx->wait_event);
188 } else
189 /* There's no session attached to that connection, destroy it */
190 mux_pt_destroy(ctx);
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200191}
192
Olivier Houchardd540b362018-11-05 18:37:53 +0100193static int mux_pt_avail_streams(struct connection *conn)
194{
195 struct mux_pt_ctx *ctx = conn->mux_ctx;
196
197 return (ctx->cs == NULL ? 1 : 0);
198}
199
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200200static void mux_pt_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200201{
Willy Tarreau4b795242017-10-05 18:47:38 +0200202 if (cs->flags & CS_FL_SHR)
203 return;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200204 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200205 cs->conn->xprt->shutr(cs->conn, (mode == CS_SHR_DRAIN));
Willy Tarreau4b795242017-10-05 18:47:38 +0200206 if (cs->flags & CS_FL_SHW)
207 conn_full_close(cs->conn);
Olivier Houchard7c6f8b12018-11-13 16:48:36 +0100208 /* Maybe we've been put in the list of available idle connections,
209 * get ouf of here
210 */
211 LIST_DEL(&cs->conn->list);
212 LIST_INIT(&cs->conn->list);
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200213}
214
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200215static void mux_pt_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200216{
Willy Tarreau4b795242017-10-05 18:47:38 +0200217 if (cs->flags & CS_FL_SHW)
218 return;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200219 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutw)
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200220 cs->conn->xprt->shutw(cs->conn, (mode == CS_SHW_NORMAL));
Willy Tarreau4b795242017-10-05 18:47:38 +0200221 if (!(cs->flags & CS_FL_SHR))
Willy Tarreaua48c1412017-12-22 18:46:33 +0100222 conn_sock_shutw(cs->conn, (mode == CS_SHW_NORMAL));
Willy Tarreau4b795242017-10-05 18:47:38 +0200223 else
224 conn_full_close(cs->conn);
Olivier Houchard7c6f8b12018-11-13 16:48:36 +0100225 /* Maybe we've been put in the list of available idle connections,
226 * get ouf of here
227 */
228 LIST_DEL(&cs->conn->list);
229 LIST_INIT(&cs->conn->list);
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200230}
231
232/*
233 * Called from the upper layer, to get more data
234 */
Willy Tarreau7f3225f2018-06-19 06:15:17 +0200235static 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 +0200236{
Willy Tarreaud9cf5402018-07-18 11:29:06 +0200237 size_t ret;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200238
Christopher Faulet4eb7d742018-10-11 15:29:21 +0200239 if (!count) {
240 cs->flags |= CS_FL_RCV_MORE;
241 return 0;
242 }
243 cs->flags &= ~CS_FL_RCV_MORE;
Willy Tarreau7f3225f2018-06-19 06:15:17 +0200244 ret = cs->conn->xprt->rcv_buf(cs->conn, buf, count, flags);
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200245 if (conn_xprt_read0_pending(cs->conn))
246 cs->flags |= CS_FL_EOS;
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200247 if (cs->conn->flags & CO_FL_ERROR)
248 cs->flags |= CS_FL_ERROR;
Willy Tarreaud9cf5402018-07-18 11:29:06 +0200249 return ret;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200250}
251
252/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +0200253static 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 +0200254{
Christopher Fauletd44a9b32018-07-27 11:59:41 +0200255 size_t ret = cs->conn->xprt->snd_buf(cs->conn, buf, count, flags);
256
257 if (ret > 0)
258 b_del(buf, ret);
259 return ret;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200260}
261
Olivier Houchard6ff20392018-07-17 18:46:31 +0200262/* Called from the upper layer, to subscribe to events */
263static int mux_pt_subscribe(struct conn_stream *cs, int event_type, void *param)
264{
265 return (cs->conn->xprt->subscribe(cs->conn, event_type, param));
266}
267
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200268static int mux_pt_unsubscribe(struct conn_stream *cs, int event_type, void *param)
269{
270 return (cs->conn->xprt->unsubscribe(cs->conn, event_type, param));
271}
272
Olivier Houchard7da120b2017-11-01 13:55:10 +0100273#if defined(CONFIG_HAP_LINUX_SPLICE)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200274/* Send and get, using splicing */
275static int mux_pt_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
276{
277 int ret;
278
279 ret = cs->conn->xprt->rcv_pipe(cs->conn, pipe, count);
280 if (conn_xprt_read0_pending(cs->conn))
281 cs->flags |= CS_FL_EOS;
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200282 if (cs->conn->flags & CO_FL_ERROR)
283 cs->flags |= CS_FL_ERROR;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200284 return (ret);
285}
286
287static int mux_pt_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
288{
289 return (cs->conn->xprt->snd_pipe(cs->conn, pipe));
290}
Olivier Houchard7da120b2017-11-01 13:55:10 +0100291#endif
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200292
Willy Tarreau53a47662017-08-28 10:53:00 +0200293/* The mux operations */
294const struct mux_ops mux_pt_ops = {
295 .init = mux_pt_init,
Willy Tarreau53a47662017-08-28 10:53:00 +0200296 .wake = mux_pt_wake,
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200297 .rcv_buf = mux_pt_rcv_buf,
298 .snd_buf = mux_pt_snd_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +0200299 .subscribe = mux_pt_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200300 .unsubscribe = mux_pt_unsubscribe,
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200301#if defined(CONFIG_HAP_LINUX_SPLICE)
302 .rcv_pipe = mux_pt_rcv_pipe,
303 .snd_pipe = mux_pt_snd_pipe,
304#endif
305 .attach = mux_pt_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +0100306 .get_first_cs = mux_pt_get_first_cs,
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200307 .detach = mux_pt_detach,
Olivier Houchardd540b362018-11-05 18:37:53 +0100308 .avail_streams = mux_pt_avail_streams,
Olivier Houchard060ed432018-11-06 16:32:42 +0100309 .destroy = mux_pt_destroy_meth,
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200310 .shutr = mux_pt_shutr,
311 .shutw = mux_pt_shutw,
Willy Tarreau28f1cb92017-12-20 16:14:44 +0100312 .flags = MX_FL_NONE,
Willy Tarreau53a47662017-08-28 10:53:00 +0200313 .name = "PASS",
314};
Willy Tarreauf6490822017-09-21 19:43:21 +0200315
Christopher Faulet32f61c02018-04-10 14:33:41 +0200316/* PROT selection : default mux has empty name */
317static struct mux_proto_list mux_proto_pt =
318 { .token = IST(""), .mode = PROTO_MODE_ANY, .side = PROTO_SIDE_BOTH, .mux = &mux_pt_ops };
Willy Tarreauf6490822017-09-21 19:43:21 +0200319
Willy Tarreau0108d902018-11-25 19:14:37 +0100320INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_pt);
321
Willy Tarreauf6490822017-09-21 19:43:21 +0200322__attribute__((constructor))
323static void __mux_pt_init(void)
324{
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100325 pool_head_pt_ctx = create_pool("mux_pt", sizeof(struct mux_pt_ctx),
326 MEM_F_SHARED);
Willy Tarreauf6490822017-09-21 19:43:21 +0200327}