blob: cd3a28b42a10245217fce9d2356cd52c144054d6 [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
Willy Tarreaued339a32017-11-03 15:55:24 +010054 if (ret >= 0)
55 cs_update_mux_polling(cs);
56 return ret;
Willy Tarreau53a47662017-08-28 10:53:00 +020057}
58
Olivier Houchard7a3f0df2017-09-13 18:30:23 +020059/* callback used to update the mux's polling flags after changing a cs' status.
60 * The caller (cs_mux_update_poll) will take care of propagating any changes to
61 * the transport layer.
62 */
63static void mux_pt_update_poll(struct conn_stream *cs)
64{
65 struct connection *conn = cs->conn;
66 int flags = 0;
67
68 conn_refresh_polling_flags(conn);
69
70 if (cs->flags & CS_FL_DATA_RD_ENA)
71 flags |= CO_FL_XPRT_RD_ENA;
72 if (cs->flags & CS_FL_DATA_WR_ENA)
73 flags |= CO_FL_XPRT_WR_ENA;
74
75 conn->flags = (conn->flags & ~(CO_FL_XPRT_RD_ENA | CO_FL_XPRT_WR_ENA)) | flags;
76 conn_cond_update_xprt_polling(conn);
77}
78
Willy Tarreau53a47662017-08-28 10:53:00 +020079/* callback to be used by default for the pass-through mux. It simply calls the
80 * data layer recv() callback much must be set.
81 */
82static void mux_pt_recv(struct connection *conn)
83{
Olivier Houchard9aaf7782017-09-13 18:30:23 +020084 struct conn_stream *cs = conn->mux_ctx;
85
Willy Tarreau4ff3b892017-10-16 15:17:17 +020086 if (conn->flags & CO_FL_ERROR)
87 cs->flags |= CS_FL_ERROR;
Olivier Houchard9aaf7782017-09-13 18:30:23 +020088 if (conn_xprt_read0_pending(conn))
89 cs->flags |= CS_FL_EOS;
90 cs->data_cb->recv(cs);
91 cs_update_mux_polling(cs);
Willy Tarreau53a47662017-08-28 10:53:00 +020092}
93
94/* callback to be used by default for the pass-through mux. It simply calls the
95 * data layer send() callback which must be set.
96 */
97static void mux_pt_send(struct connection *conn)
98{
Olivier Houchard9aaf7782017-09-13 18:30:23 +020099 struct conn_stream *cs = conn->mux_ctx;
100
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200101 if (conn->flags & CO_FL_ERROR)
102 cs->flags |= CS_FL_ERROR;
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200103 cs->data_cb->send(cs);
104 cs_update_mux_polling(cs);
Willy Tarreau53a47662017-08-28 10:53:00 +0200105}
106
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200107/*
108 * Attach a new stream to a connection
109 * (Used for outgoing connections)
110 */
111static struct conn_stream *mux_pt_attach(struct connection *conn)
112{
113 return NULL;
114}
115
116/*
Willy Tarreau2c52a2b2017-10-08 11:00:17 +0200117 * Detach the stream from the connection and possibly release the connection.
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200118 */
119static void mux_pt_detach(struct conn_stream *cs)
120{
Willy Tarreau2c52a2b2017-10-08 11:00:17 +0200121 struct connection *conn = cs->conn;
122
123 LIST_DEL(&conn->list);
124 conn_stop_tracking(conn);
125 conn_full_close(conn);
Willy Tarreau436d3332017-10-08 11:16:46 +0200126 if (conn->destroy_cb)
127 conn->destroy_cb(conn);
Willy Tarreau2c52a2b2017-10-08 11:00:17 +0200128 conn_free(conn);
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200129}
130
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200131static void mux_pt_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200132{
Willy Tarreau4b795242017-10-05 18:47:38 +0200133 if (cs->flags & CS_FL_SHR)
134 return;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200135 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200136 cs->conn->xprt->shutr(cs->conn, (mode == CS_SHR_DRAIN));
Willy Tarreau4b795242017-10-05 18:47:38 +0200137 if (cs->flags & CS_FL_SHW)
138 conn_full_close(cs->conn);
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200139}
140
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200141static void mux_pt_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200142{
Willy Tarreau4b795242017-10-05 18:47:38 +0200143 if (cs->flags & CS_FL_SHW)
144 return;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200145 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutw)
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200146 cs->conn->xprt->shutw(cs->conn, (mode == CS_SHW_NORMAL));
Willy Tarreau4b795242017-10-05 18:47:38 +0200147 if (!(cs->flags & CS_FL_SHR))
148 conn_sock_shutw(cs->conn);
149 else
150 conn_full_close(cs->conn);
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200151}
152
153/*
154 * Called from the upper layer, to get more data
155 */
156static int mux_pt_rcv_buf(struct conn_stream *cs, struct buffer *buf, int count)
157{
158 int ret;
159
160 ret = cs->conn->xprt->rcv_buf(cs->conn, buf, count);
161 if (conn_xprt_read0_pending(cs->conn))
162 cs->flags |= CS_FL_EOS;
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200163 if (cs->conn->flags & CO_FL_ERROR)
164 cs->flags |= CS_FL_ERROR;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200165 return (ret);
166}
167
168/* Called from the upper layer, to send data */
169static int mux_pt_snd_buf(struct conn_stream *cs, struct buffer *buf, int flags)
170{
171 return (cs->conn->xprt->snd_buf(cs->conn, buf, flags));
172}
173
Olivier Houchard7da120b2017-11-01 13:55:10 +0100174#if defined(CONFIG_HAP_LINUX_SPLICE)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200175/* Send and get, using splicing */
176static int mux_pt_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
177{
178 int ret;
179
180 ret = cs->conn->xprt->rcv_pipe(cs->conn, pipe, count);
181 if (conn_xprt_read0_pending(cs->conn))
182 cs->flags |= CS_FL_EOS;
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200183 if (cs->conn->flags & CO_FL_ERROR)
184 cs->flags |= CS_FL_ERROR;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200185 return (ret);
186}
187
188static int mux_pt_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
189{
190 return (cs->conn->xprt->snd_pipe(cs->conn, pipe));
191}
Olivier Houchard7da120b2017-11-01 13:55:10 +0100192#endif
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200193
Willy Tarreau53a47662017-08-28 10:53:00 +0200194/* The mux operations */
195const struct mux_ops mux_pt_ops = {
196 .init = mux_pt_init,
197 .recv = mux_pt_recv,
198 .send = mux_pt_send,
199 .wake = mux_pt_wake,
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200200 .update_poll = mux_pt_update_poll,
201 .rcv_buf = mux_pt_rcv_buf,
202 .snd_buf = mux_pt_snd_buf,
203#if defined(CONFIG_HAP_LINUX_SPLICE)
204 .rcv_pipe = mux_pt_rcv_pipe,
205 .snd_pipe = mux_pt_snd_pipe,
206#endif
207 .attach = mux_pt_attach,
208 .detach = mux_pt_detach,
209 .shutr = mux_pt_shutr,
210 .shutw = mux_pt_shutw,
Willy Tarreau53a47662017-08-28 10:53:00 +0200211 .name = "PASS",
212};
Willy Tarreauf6490822017-09-21 19:43:21 +0200213
214/* ALPN selection : default mux has empty name */
215static struct alpn_mux_list alpn_mux_pt =
216 { .token = IST(""), .mode = ALPN_MODE_ANY, .mux = &mux_pt_ops };
217
218__attribute__((constructor))
219static void __mux_pt_init(void)
220{
221 alpn_register_mux(&alpn_mux_pt);
222}