blob: 54244c34efcd2caa3202da1dae72817879223d45 [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
54 cs_update_mux_polling(cs);
55 return (ret);
Willy Tarreau53a47662017-08-28 10:53:00 +020056}
57
Olivier Houchard7a3f0df2017-09-13 18:30:23 +020058/* callback used to update the mux's polling flags after changing a cs' status.
59 * The caller (cs_mux_update_poll) will take care of propagating any changes to
60 * the transport layer.
61 */
62static void mux_pt_update_poll(struct conn_stream *cs)
63{
64 struct connection *conn = cs->conn;
65 int flags = 0;
66
67 conn_refresh_polling_flags(conn);
68
69 if (cs->flags & CS_FL_DATA_RD_ENA)
70 flags |= CO_FL_XPRT_RD_ENA;
71 if (cs->flags & CS_FL_DATA_WR_ENA)
72 flags |= CO_FL_XPRT_WR_ENA;
73
74 conn->flags = (conn->flags & ~(CO_FL_XPRT_RD_ENA | CO_FL_XPRT_WR_ENA)) | flags;
75 conn_cond_update_xprt_polling(conn);
76}
77
Willy Tarreau53a47662017-08-28 10:53:00 +020078/* callback to be used by default for the pass-through mux. It simply calls the
79 * data layer recv() callback much must be set.
80 */
81static void mux_pt_recv(struct connection *conn)
82{
Olivier Houchard9aaf7782017-09-13 18:30:23 +020083 struct conn_stream *cs = conn->mux_ctx;
84
Willy Tarreau4ff3b892017-10-16 15:17:17 +020085 if (conn->flags & CO_FL_ERROR)
86 cs->flags |= CS_FL_ERROR;
Olivier Houchard9aaf7782017-09-13 18:30:23 +020087 if (conn_xprt_read0_pending(conn))
88 cs->flags |= CS_FL_EOS;
89 cs->data_cb->recv(cs);
90 cs_update_mux_polling(cs);
Willy Tarreau53a47662017-08-28 10:53:00 +020091}
92
93/* callback to be used by default for the pass-through mux. It simply calls the
94 * data layer send() callback which must be set.
95 */
96static void mux_pt_send(struct connection *conn)
97{
Olivier Houchard9aaf7782017-09-13 18:30:23 +020098 struct conn_stream *cs = conn->mux_ctx;
99
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200100 if (conn->flags & CO_FL_ERROR)
101 cs->flags |= CS_FL_ERROR;
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200102 cs->data_cb->send(cs);
103 cs_update_mux_polling(cs);
Willy Tarreau53a47662017-08-28 10:53:00 +0200104}
105
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200106/*
107 * Attach a new stream to a connection
108 * (Used for outgoing connections)
109 */
110static struct conn_stream *mux_pt_attach(struct connection *conn)
111{
112 return NULL;
113}
114
115/*
Willy Tarreau2c52a2b2017-10-08 11:00:17 +0200116 * Detach the stream from the connection and possibly release the connection.
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200117 */
118static void mux_pt_detach(struct conn_stream *cs)
119{
Willy Tarreau2c52a2b2017-10-08 11:00:17 +0200120 struct connection *conn = cs->conn;
121
122 LIST_DEL(&conn->list);
123 conn_stop_tracking(conn);
124 conn_full_close(conn);
Willy Tarreau436d3332017-10-08 11:16:46 +0200125 if (conn->destroy_cb)
126 conn->destroy_cb(conn);
Willy Tarreau2c52a2b2017-10-08 11:00:17 +0200127 conn_free(conn);
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200128}
129
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200130static void mux_pt_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200131{
Willy Tarreau4b795242017-10-05 18:47:38 +0200132 if (cs->flags & CS_FL_SHR)
133 return;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200134 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200135 cs->conn->xprt->shutr(cs->conn, (mode == CS_SHR_DRAIN));
Willy Tarreau4b795242017-10-05 18:47:38 +0200136 if (cs->flags & CS_FL_SHW)
137 conn_full_close(cs->conn);
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200138}
139
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200140static void mux_pt_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200141{
Willy Tarreau4b795242017-10-05 18:47:38 +0200142 if (cs->flags & CS_FL_SHW)
143 return;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200144 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutw)
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200145 cs->conn->xprt->shutw(cs->conn, (mode == CS_SHW_NORMAL));
Willy Tarreau4b795242017-10-05 18:47:38 +0200146 if (!(cs->flags & CS_FL_SHR))
147 conn_sock_shutw(cs->conn);
148 else
149 conn_full_close(cs->conn);
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200150}
151
152/*
153 * Called from the upper layer, to get more data
154 */
155static int mux_pt_rcv_buf(struct conn_stream *cs, struct buffer *buf, int count)
156{
157 int ret;
158
159 ret = cs->conn->xprt->rcv_buf(cs->conn, buf, count);
160 if (conn_xprt_read0_pending(cs->conn))
161 cs->flags |= CS_FL_EOS;
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200162 if (cs->conn->flags & CO_FL_ERROR)
163 cs->flags |= CS_FL_ERROR;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200164 return (ret);
165}
166
167/* Called from the upper layer, to send data */
168static int mux_pt_snd_buf(struct conn_stream *cs, struct buffer *buf, int flags)
169{
170 return (cs->conn->xprt->snd_buf(cs->conn, buf, flags));
171}
172
173/* Send and get, using splicing */
174static int mux_pt_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
175{
176 int ret;
177
178 ret = cs->conn->xprt->rcv_pipe(cs->conn, pipe, count);
179 if (conn_xprt_read0_pending(cs->conn))
180 cs->flags |= CS_FL_EOS;
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200181 if (cs->conn->flags & CO_FL_ERROR)
182 cs->flags |= CS_FL_ERROR;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200183 return (ret);
184}
185
186static int mux_pt_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
187{
188 return (cs->conn->xprt->snd_pipe(cs->conn, pipe));
189}
190
Willy Tarreau53a47662017-08-28 10:53:00 +0200191/* The mux operations */
192const struct mux_ops mux_pt_ops = {
193 .init = mux_pt_init,
194 .recv = mux_pt_recv,
195 .send = mux_pt_send,
196 .wake = mux_pt_wake,
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200197 .update_poll = mux_pt_update_poll,
198 .rcv_buf = mux_pt_rcv_buf,
199 .snd_buf = mux_pt_snd_buf,
200#if defined(CONFIG_HAP_LINUX_SPLICE)
201 .rcv_pipe = mux_pt_rcv_pipe,
202 .snd_pipe = mux_pt_snd_pipe,
203#endif
204 .attach = mux_pt_attach,
205 .detach = mux_pt_detach,
206 .shutr = mux_pt_shutr,
207 .shutw = mux_pt_shutw,
Willy Tarreau53a47662017-08-28 10:53:00 +0200208 .name = "PASS",
209};
Willy Tarreauf6490822017-09-21 19:43:21 +0200210
211/* ALPN selection : default mux has empty name */
212static struct alpn_mux_list alpn_mux_pt =
213 { .token = IST(""), .mode = ALPN_MODE_ANY, .mux = &mux_pt_ops };
214
215__attribute__((constructor))
216static void __mux_pt_init(void)
217{
218 alpn_register_mux(&alpn_mux_pt);
219}