blob: a0f039775824c36e7507bec85c6a865c9cf1c199 [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);
33 if (conn->destroy_cb)
34 conn->destroy_cb(conn);
35 /* We don't bother unsubscribing here, as we're about to destroy
36 * both the connection and the mux_pt_ctx
37 */
38 conn_free(conn);
39 pool_free(pool_head_pt_ctx, ctx);
40}
41
42/* Callback, used when we get I/Os while in idle mode */
43static struct task *mux_pt_io_cb(struct task *t, void *tctx, unsigned short status)
44{
45 struct mux_pt_ctx *ctx = tctx;
46
47 conn_sock_drain(ctx->conn);
48 if (ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))
49 mux_pt_destroy(ctx);
50 else
51 ctx->conn->xprt->subscribe(ctx->conn, SUB_CAN_RECV,
52 &ctx->wait_event);
53
54 return NULL;
55}
Willy Tarreau53a47662017-08-28 10:53:00 +020056
Olivier Houchard9aaf7782017-09-13 18:30:23 +020057/* Initialize the mux once it's attached. It is expected that conn->mux_ctx
58 * points to the existing conn_stream (for outgoing connections) or NULL (for
59 * incoming ones, in which case one will be allocated and a new stream will be
60 * instanciated). Returns < 0 on error.
Willy Tarreau53a47662017-08-28 10:53:00 +020061 */
Willy Tarreau175a2bb2018-09-12 12:02:05 +020062static int mux_pt_init(struct connection *conn, struct proxy *prx)
Willy Tarreau53a47662017-08-28 10:53:00 +020063{
Olivier Houchard9aaf7782017-09-13 18:30:23 +020064 struct conn_stream *cs = conn->mux_ctx;
Olivier Houchardb6c32ee2018-11-05 18:28:43 +010065 struct mux_pt_ctx *ctx = pool_alloc(pool_head_pt_ctx);
66
67 if (!ctx)
68 goto fail;
69
70 ctx->wait_event.task = tasklet_new();
71 if (!ctx->wait_event.task)
72 goto fail_free_ctx;
73 ctx->wait_event.task->context = ctx;
74 ctx->wait_event.task->process = mux_pt_io_cb;
75 ctx->wait_event.wait_reason = 0;
76 ctx->conn = conn;
Olivier Houchard9aaf7782017-09-13 18:30:23 +020077
78 if (!cs) {
79 cs = cs_new(conn);
80 if (!cs)
Olivier Houchardb6c32ee2018-11-05 18:28:43 +010081 goto fail_free_ctx;
Olivier Houchard9aaf7782017-09-13 18:30:23 +020082
83 if (stream_create_from_cs(cs) < 0)
84 goto fail_free;
85
Olivier Houchard9aaf7782017-09-13 18:30:23 +020086 }
Olivier Houchardb6c32ee2018-11-05 18:28:43 +010087 conn->mux_ctx = ctx;
88 ctx->cs = cs;
Willy Tarreau53a47662017-08-28 10:53:00 +020089 return 0;
Olivier Houchard9aaf7782017-09-13 18:30:23 +020090
91 fail_free:
92 cs_free(cs);
Olivier Houchardb6c32ee2018-11-05 18:28:43 +010093fail_free_ctx:
94 if (ctx->wait_event.task)
95 tasklet_free(ctx->wait_event.task);
96 pool_free(pool_head_pt_ctx, ctx);
Olivier Houchard9aaf7782017-09-13 18:30:23 +020097 fail:
98 return -1;
Willy Tarreau53a47662017-08-28 10:53:00 +020099}
100
101/* callback to be used by default for the pass-through mux. It calls the data
102 * layer wake() callback if it is set otherwise returns 0.
103 */
104static int mux_pt_wake(struct connection *conn)
105{
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100106 struct mux_pt_ctx *ctx = conn->mux_ctx;
107 struct conn_stream *cs = ctx->cs;
108 int ret = 0;
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200109
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100110 if (cs) {
111 ret = cs->data_cb->wake ? cs->data_cb->wake(cs) : 0;
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200112
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100113 if (ret < 0)
114 return ret;
115 } else {
116 conn_sock_drain(conn);
117 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH)) {
118 mux_pt_destroy(ctx);
119 return -1;
120 }
121 }
Willy Tarreauad7f0ad2018-08-24 15:48:59 +0200122
Olivier Houchard7fc96d52017-11-23 18:25:47 +0100123 /* If we had early data, and we're done with the handshake
124 * then whe know the data are safe, and we can remove the flag.
125 */
126 if ((conn->flags & (CO_FL_EARLY_DATA | CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE)) ==
127 CO_FL_EARLY_DATA)
128 conn->flags &= ~CO_FL_EARLY_DATA;
Willy Tarreaued339a32017-11-03 15:55:24 +0100129 return ret;
Willy Tarreau53a47662017-08-28 10:53:00 +0200130}
131
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200132/*
133 * Attach a new stream to a connection
134 * (Used for outgoing connections)
135 */
136static struct conn_stream *mux_pt_attach(struct connection *conn)
137{
138 return NULL;
139}
140
Willy Tarreaufafd3982018-11-18 21:29:20 +0100141/* Retrieves a valid conn_stream from this connection, or returns NULL. For
142 * this mux, it's easy as we can only store a single conn_stream.
143 */
144static const struct conn_stream *mux_pt_get_first_cs(const struct connection *conn)
145{
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100146 struct mux_pt_ctx *ctx = conn->mux_ctx;
147 struct conn_stream *cs = ctx->cs;
Willy Tarreaufafd3982018-11-18 21:29:20 +0100148
149 return cs;
150}
151
Olivier Houchard060ed432018-11-06 16:32:42 +0100152/* Destroy the mux and the associated connection */
153static void mux_pt_destroy_meth(struct connection *conn)
154{
155 mux_pt_destroy(conn->mux_ctx);
156}
157
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200158/*
Willy Tarreau2c52a2b2017-10-08 11:00:17 +0200159 * Detach the stream from the connection and possibly release the connection.
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200160 */
161static void mux_pt_detach(struct conn_stream *cs)
162{
Willy Tarreau2c52a2b2017-10-08 11:00:17 +0200163 struct connection *conn = cs->conn;
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100164 struct mux_pt_ctx *ctx = cs->conn->mux_ctx;
Willy Tarreau2c52a2b2017-10-08 11:00:17 +0200165
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100166 /* Subscribe, to know if we got disconnected */
167 conn->xprt->subscribe(conn, SUB_CAN_RECV, &ctx->wait_event);
168 ctx->cs = NULL;
169 mux_pt_destroy(ctx);
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200170}
171
Olivier Houchardd540b362018-11-05 18:37:53 +0100172static int mux_pt_avail_streams(struct connection *conn)
173{
174 struct mux_pt_ctx *ctx = conn->mux_ctx;
175
176 return (ctx->cs == NULL ? 1 : 0);
177}
178
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200179static void mux_pt_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200180{
Willy Tarreau4b795242017-10-05 18:47:38 +0200181 if (cs->flags & CS_FL_SHR)
182 return;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200183 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200184 cs->conn->xprt->shutr(cs->conn, (mode == CS_SHR_DRAIN));
Willy Tarreau4b795242017-10-05 18:47:38 +0200185 if (cs->flags & CS_FL_SHW)
186 conn_full_close(cs->conn);
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200187}
188
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200189static void mux_pt_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200190{
Willy Tarreau4b795242017-10-05 18:47:38 +0200191 if (cs->flags & CS_FL_SHW)
192 return;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200193 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutw)
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200194 cs->conn->xprt->shutw(cs->conn, (mode == CS_SHW_NORMAL));
Willy Tarreau4b795242017-10-05 18:47:38 +0200195 if (!(cs->flags & CS_FL_SHR))
Willy Tarreaua48c1412017-12-22 18:46:33 +0100196 conn_sock_shutw(cs->conn, (mode == CS_SHW_NORMAL));
Willy Tarreau4b795242017-10-05 18:47:38 +0200197 else
198 conn_full_close(cs->conn);
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200199}
200
201/*
202 * Called from the upper layer, to get more data
203 */
Willy Tarreau7f3225f2018-06-19 06:15:17 +0200204static 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 +0200205{
Willy Tarreaud9cf5402018-07-18 11:29:06 +0200206 size_t ret;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200207
Christopher Faulet4eb7d742018-10-11 15:29:21 +0200208 if (!count) {
209 cs->flags |= CS_FL_RCV_MORE;
210 return 0;
211 }
212 cs->flags &= ~CS_FL_RCV_MORE;
Willy Tarreau7f3225f2018-06-19 06:15:17 +0200213 ret = cs->conn->xprt->rcv_buf(cs->conn, buf, count, flags);
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200214 if (conn_xprt_read0_pending(cs->conn))
215 cs->flags |= CS_FL_EOS;
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200216 if (cs->conn->flags & CO_FL_ERROR)
217 cs->flags |= CS_FL_ERROR;
Willy Tarreaud9cf5402018-07-18 11:29:06 +0200218 return ret;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200219}
220
221/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +0200222static 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 +0200223{
Christopher Fauletd44a9b32018-07-27 11:59:41 +0200224 size_t ret = cs->conn->xprt->snd_buf(cs->conn, buf, count, flags);
225
226 if (ret > 0)
227 b_del(buf, ret);
228 return ret;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200229}
230
Olivier Houchard6ff20392018-07-17 18:46:31 +0200231/* Called from the upper layer, to subscribe to events */
232static int mux_pt_subscribe(struct conn_stream *cs, int event_type, void *param)
233{
234 return (cs->conn->xprt->subscribe(cs->conn, event_type, param));
235}
236
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200237static int mux_pt_unsubscribe(struct conn_stream *cs, int event_type, void *param)
238{
239 return (cs->conn->xprt->unsubscribe(cs->conn, event_type, param));
240}
241
Olivier Houchard7da120b2017-11-01 13:55:10 +0100242#if defined(CONFIG_HAP_LINUX_SPLICE)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200243/* Send and get, using splicing */
244static int mux_pt_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
245{
246 int ret;
247
248 ret = cs->conn->xprt->rcv_pipe(cs->conn, pipe, count);
249 if (conn_xprt_read0_pending(cs->conn))
250 cs->flags |= CS_FL_EOS;
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200251 if (cs->conn->flags & CO_FL_ERROR)
252 cs->flags |= CS_FL_ERROR;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200253 return (ret);
254}
255
256static int mux_pt_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
257{
258 return (cs->conn->xprt->snd_pipe(cs->conn, pipe));
259}
Olivier Houchard7da120b2017-11-01 13:55:10 +0100260#endif
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200261
Willy Tarreau53a47662017-08-28 10:53:00 +0200262/* The mux operations */
263const struct mux_ops mux_pt_ops = {
264 .init = mux_pt_init,
Willy Tarreau53a47662017-08-28 10:53:00 +0200265 .wake = mux_pt_wake,
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200266 .rcv_buf = mux_pt_rcv_buf,
267 .snd_buf = mux_pt_snd_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +0200268 .subscribe = mux_pt_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200269 .unsubscribe = mux_pt_unsubscribe,
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200270#if defined(CONFIG_HAP_LINUX_SPLICE)
271 .rcv_pipe = mux_pt_rcv_pipe,
272 .snd_pipe = mux_pt_snd_pipe,
273#endif
274 .attach = mux_pt_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +0100275 .get_first_cs = mux_pt_get_first_cs,
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200276 .detach = mux_pt_detach,
Olivier Houchardd540b362018-11-05 18:37:53 +0100277 .avail_streams = mux_pt_avail_streams,
Olivier Houchard060ed432018-11-06 16:32:42 +0100278 .destroy = mux_pt_destroy_meth,
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200279 .shutr = mux_pt_shutr,
280 .shutw = mux_pt_shutw,
Willy Tarreau28f1cb92017-12-20 16:14:44 +0100281 .flags = MX_FL_NONE,
Willy Tarreau53a47662017-08-28 10:53:00 +0200282 .name = "PASS",
283};
Willy Tarreauf6490822017-09-21 19:43:21 +0200284
Christopher Faulet32f61c02018-04-10 14:33:41 +0200285/* PROT selection : default mux has empty name */
286static struct mux_proto_list mux_proto_pt =
287 { .token = IST(""), .mode = PROTO_MODE_ANY, .side = PROTO_SIDE_BOTH, .mux = &mux_pt_ops };
Willy Tarreauf6490822017-09-21 19:43:21 +0200288
289__attribute__((constructor))
290static void __mux_pt_init(void)
291{
Christopher Faulet32f61c02018-04-10 14:33:41 +0200292 register_mux_proto(&mux_proto_pt);
Olivier Houchardb6c32ee2018-11-05 18:28:43 +0100293 pool_head_pt_ctx = create_pool("mux_pt", sizeof(struct mux_pt_ctx),
294 MEM_F_SHARED);
Willy Tarreauf6490822017-09-21 19:43:21 +0200295}