blob: a68b962154e6159d785bf5143d33c80a32ec71ff [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>
16
Olivier Houchard9aaf7782017-09-13 18:30:23 +020017/* Initialize the mux once it's attached. It is expected that conn->mux_ctx
18 * points to the existing conn_stream (for outgoing connections) or NULL (for
19 * incoming ones, in which case one will be allocated and a new stream will be
20 * instanciated). Returns < 0 on error.
Willy Tarreau53a47662017-08-28 10:53:00 +020021 */
22static int mux_pt_init(struct connection *conn)
23{
Olivier Houchard9aaf7782017-09-13 18:30:23 +020024 struct conn_stream *cs = conn->mux_ctx;
25
26 if (!cs) {
27 cs = cs_new(conn);
28 if (!cs)
29 goto fail;
30
31 if (stream_create_from_cs(cs) < 0)
32 goto fail_free;
33
34 conn->mux_ctx = cs;
35 }
Willy Tarreau53a47662017-08-28 10:53:00 +020036 return 0;
Olivier Houchard9aaf7782017-09-13 18:30:23 +020037
38 fail_free:
39 cs_free(cs);
40 fail:
41 return -1;
Willy Tarreau53a47662017-08-28 10:53:00 +020042}
43
44/* callback to be used by default for the pass-through mux. It calls the data
45 * layer wake() callback if it is set otherwise returns 0.
46 */
47static int mux_pt_wake(struct connection *conn)
48{
Olivier Houchard9aaf7782017-09-13 18:30:23 +020049 struct conn_stream *cs = conn->mux_ctx;
50 int ret;
51
52 ret = cs->data_cb->wake ? cs->data_cb->wake(cs) : 0;
53
Olivier Houchard7fc96d52017-11-23 18:25:47 +010054 /* If we had early data, and we're done with the handshake
55 * then whe know the data are safe, and we can remove the flag.
56 */
57 if ((conn->flags & (CO_FL_EARLY_DATA | CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE)) ==
58 CO_FL_EARLY_DATA)
59 conn->flags &= ~CO_FL_EARLY_DATA;
Willy Tarreaued339a32017-11-03 15:55:24 +010060 if (ret >= 0)
61 cs_update_mux_polling(cs);
62 return ret;
Willy Tarreau53a47662017-08-28 10:53:00 +020063}
64
Olivier Houchard7a3f0df2017-09-13 18:30:23 +020065/* callback used to update the mux's polling flags after changing a cs' status.
66 * The caller (cs_mux_update_poll) will take care of propagating any changes to
67 * the transport layer.
68 */
69static void mux_pt_update_poll(struct conn_stream *cs)
70{
71 struct connection *conn = cs->conn;
72 int flags = 0;
73
74 conn_refresh_polling_flags(conn);
75
76 if (cs->flags & CS_FL_DATA_RD_ENA)
77 flags |= CO_FL_XPRT_RD_ENA;
78 if (cs->flags & CS_FL_DATA_WR_ENA)
79 flags |= CO_FL_XPRT_WR_ENA;
80
81 conn->flags = (conn->flags & ~(CO_FL_XPRT_RD_ENA | CO_FL_XPRT_WR_ENA)) | flags;
82 conn_cond_update_xprt_polling(conn);
83}
84
Willy Tarreau53a47662017-08-28 10:53:00 +020085/* callback to be used by default for the pass-through mux. It simply calls the
86 * data layer recv() callback much must be set.
87 */
88static void mux_pt_recv(struct connection *conn)
89{
Olivier Houchard9aaf7782017-09-13 18:30:23 +020090 struct conn_stream *cs = conn->mux_ctx;
91
Willy Tarreau4ff3b892017-10-16 15:17:17 +020092 if (conn->flags & CO_FL_ERROR)
93 cs->flags |= CS_FL_ERROR;
Olivier Houchard9aaf7782017-09-13 18:30:23 +020094 if (conn_xprt_read0_pending(conn))
95 cs->flags |= CS_FL_EOS;
96 cs->data_cb->recv(cs);
97 cs_update_mux_polling(cs);
Willy Tarreau53a47662017-08-28 10:53:00 +020098}
99
100/* callback to be used by default for the pass-through mux. It simply calls the
101 * data layer send() callback which must be set.
102 */
103static void mux_pt_send(struct connection *conn)
104{
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200105 struct conn_stream *cs = conn->mux_ctx;
106
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200107 if (conn->flags & CO_FL_ERROR)
108 cs->flags |= CS_FL_ERROR;
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200109 cs->data_cb->send(cs);
110 cs_update_mux_polling(cs);
Willy Tarreau53a47662017-08-28 10:53:00 +0200111}
112
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200113/*
114 * Attach a new stream to a connection
115 * (Used for outgoing connections)
116 */
117static struct conn_stream *mux_pt_attach(struct connection *conn)
118{
119 return NULL;
120}
121
122/*
Willy Tarreau2c52a2b2017-10-08 11:00:17 +0200123 * Detach the stream from the connection and possibly release the connection.
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200124 */
125static void mux_pt_detach(struct conn_stream *cs)
126{
Willy Tarreau2c52a2b2017-10-08 11:00:17 +0200127 struct connection *conn = cs->conn;
128
129 LIST_DEL(&conn->list);
130 conn_stop_tracking(conn);
131 conn_full_close(conn);
Willy Tarreau436d3332017-10-08 11:16:46 +0200132 if (conn->destroy_cb)
133 conn->destroy_cb(conn);
Willy Tarreau2c52a2b2017-10-08 11:00:17 +0200134 conn_free(conn);
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200135}
136
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200137static void mux_pt_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200138{
Willy Tarreau4b795242017-10-05 18:47:38 +0200139 if (cs->flags & CS_FL_SHR)
140 return;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200141 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200142 cs->conn->xprt->shutr(cs->conn, (mode == CS_SHR_DRAIN));
Willy Tarreau4b795242017-10-05 18:47:38 +0200143 if (cs->flags & CS_FL_SHW)
144 conn_full_close(cs->conn);
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200145}
146
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200147static void mux_pt_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200148{
Willy Tarreau4b795242017-10-05 18:47:38 +0200149 if (cs->flags & CS_FL_SHW)
150 return;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200151 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutw)
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200152 cs->conn->xprt->shutw(cs->conn, (mode == CS_SHW_NORMAL));
Willy Tarreau4b795242017-10-05 18:47:38 +0200153 if (!(cs->flags & CS_FL_SHR))
Willy Tarreaua48c1412017-12-22 18:46:33 +0100154 conn_sock_shutw(cs->conn, (mode == CS_SHW_NORMAL));
Willy Tarreau4b795242017-10-05 18:47:38 +0200155 else
156 conn_full_close(cs->conn);
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200157}
158
159/*
160 * Called from the upper layer, to get more data
161 */
162static int mux_pt_rcv_buf(struct conn_stream *cs, struct buffer *buf, int count)
163{
164 int ret;
165
166 ret = cs->conn->xprt->rcv_buf(cs->conn, buf, count);
167 if (conn_xprt_read0_pending(cs->conn))
168 cs->flags |= CS_FL_EOS;
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200169 if (cs->conn->flags & CO_FL_ERROR)
170 cs->flags |= CS_FL_ERROR;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200171 return (ret);
172}
173
174/* Called from the upper layer, to send data */
175static int mux_pt_snd_buf(struct conn_stream *cs, struct buffer *buf, int flags)
176{
177 return (cs->conn->xprt->snd_buf(cs->conn, buf, flags));
178}
179
Olivier Houchard7da120b2017-11-01 13:55:10 +0100180#if defined(CONFIG_HAP_LINUX_SPLICE)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200181/* Send and get, using splicing */
182static int mux_pt_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
183{
184 int ret;
185
186 ret = cs->conn->xprt->rcv_pipe(cs->conn, pipe, count);
187 if (conn_xprt_read0_pending(cs->conn))
188 cs->flags |= CS_FL_EOS;
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200189 if (cs->conn->flags & CO_FL_ERROR)
190 cs->flags |= CS_FL_ERROR;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200191 return (ret);
192}
193
194static int mux_pt_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
195{
196 return (cs->conn->xprt->snd_pipe(cs->conn, pipe));
197}
Olivier Houchard7da120b2017-11-01 13:55:10 +0100198#endif
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200199
Willy Tarreau53a47662017-08-28 10:53:00 +0200200/* The mux operations */
201const struct mux_ops mux_pt_ops = {
202 .init = mux_pt_init,
203 .recv = mux_pt_recv,
204 .send = mux_pt_send,
205 .wake = mux_pt_wake,
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200206 .update_poll = mux_pt_update_poll,
207 .rcv_buf = mux_pt_rcv_buf,
208 .snd_buf = mux_pt_snd_buf,
209#if defined(CONFIG_HAP_LINUX_SPLICE)
210 .rcv_pipe = mux_pt_rcv_pipe,
211 .snd_pipe = mux_pt_snd_pipe,
212#endif
213 .attach = mux_pt_attach,
214 .detach = mux_pt_detach,
215 .shutr = mux_pt_shutr,
216 .shutw = mux_pt_shutw,
Willy Tarreau28f1cb92017-12-20 16:14:44 +0100217 .flags = MX_FL_NONE,
Willy Tarreau53a47662017-08-28 10:53:00 +0200218 .name = "PASS",
219};
Willy Tarreauf6490822017-09-21 19:43:21 +0200220
221/* ALPN selection : default mux has empty name */
222static struct alpn_mux_list alpn_mux_pt =
223 { .token = IST(""), .mode = ALPN_MODE_ANY, .mux = &mux_pt_ops };
224
225__attribute__((constructor))
226static void __mux_pt_init(void)
227{
228 alpn_register_mux(&alpn_mux_pt);
229}