blob: 4195ec2505624e32a9a4480ac37a1eaa05170383 [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 */
Willy Tarreau175a2bb2018-09-12 12:02:05 +020022static int mux_pt_init(struct connection *conn, struct proxy *prx)
Willy Tarreau53a47662017-08-28 10:53:00 +020023{
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 Tarreauad7f0ad2018-08-24 15:48:59 +020054 if (ret < 0)
55 return ret;
56
Olivier Houchard7fc96d52017-11-23 18:25:47 +010057 /* If we had early data, and we're done with the handshake
58 * then whe know the data are safe, and we can remove the flag.
59 */
60 if ((conn->flags & (CO_FL_EARLY_DATA | CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE)) ==
61 CO_FL_EARLY_DATA)
62 conn->flags &= ~CO_FL_EARLY_DATA;
Willy Tarreaued339a32017-11-03 15:55:24 +010063 return ret;
Willy Tarreau53a47662017-08-28 10:53:00 +020064}
65
Olivier Houchard7a3f0df2017-09-13 18:30:23 +020066/*
67 * Attach a new stream to a connection
68 * (Used for outgoing connections)
69 */
70static struct conn_stream *mux_pt_attach(struct connection *conn)
71{
72 return NULL;
73}
74
75/*
Willy Tarreau2c52a2b2017-10-08 11:00:17 +020076 * Detach the stream from the connection and possibly release the connection.
Olivier Houchard7a3f0df2017-09-13 18:30:23 +020077 */
78static void mux_pt_detach(struct conn_stream *cs)
79{
Willy Tarreau2c52a2b2017-10-08 11:00:17 +020080 struct connection *conn = cs->conn;
81
82 LIST_DEL(&conn->list);
83 conn_stop_tracking(conn);
84 conn_full_close(conn);
Willy Tarreau436d3332017-10-08 11:16:46 +020085 if (conn->destroy_cb)
86 conn->destroy_cb(conn);
Willy Tarreau2c52a2b2017-10-08 11:00:17 +020087 conn_free(conn);
Olivier Houchard7a3f0df2017-09-13 18:30:23 +020088}
89
Willy Tarreauecdb3fe2017-10-05 15:25:48 +020090static void mux_pt_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +020091{
Willy Tarreau4b795242017-10-05 18:47:38 +020092 if (cs->flags & CS_FL_SHR)
93 return;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +020094 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
Willy Tarreauecdb3fe2017-10-05 15:25:48 +020095 cs->conn->xprt->shutr(cs->conn, (mode == CS_SHR_DRAIN));
Willy Tarreau4b795242017-10-05 18:47:38 +020096 if (cs->flags & CS_FL_SHW)
97 conn_full_close(cs->conn);
Olivier Houchard7a3f0df2017-09-13 18:30:23 +020098}
99
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200100static void mux_pt_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200101{
Willy Tarreau4b795242017-10-05 18:47:38 +0200102 if (cs->flags & CS_FL_SHW)
103 return;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200104 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutw)
Willy Tarreauecdb3fe2017-10-05 15:25:48 +0200105 cs->conn->xprt->shutw(cs->conn, (mode == CS_SHW_NORMAL));
Willy Tarreau4b795242017-10-05 18:47:38 +0200106 if (!(cs->flags & CS_FL_SHR))
Willy Tarreaua48c1412017-12-22 18:46:33 +0100107 conn_sock_shutw(cs->conn, (mode == CS_SHW_NORMAL));
Willy Tarreau4b795242017-10-05 18:47:38 +0200108 else
109 conn_full_close(cs->conn);
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200110}
111
112/*
113 * Called from the upper layer, to get more data
114 */
Willy Tarreau7f3225f2018-06-19 06:15:17 +0200115static 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 +0200116{
Willy Tarreaud9cf5402018-07-18 11:29:06 +0200117 size_t ret;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200118
Christopher Faulet4eb7d742018-10-11 15:29:21 +0200119 if (!count) {
120 cs->flags |= CS_FL_RCV_MORE;
121 return 0;
122 }
123 cs->flags &= ~CS_FL_RCV_MORE;
Willy Tarreau7f3225f2018-06-19 06:15:17 +0200124 ret = cs->conn->xprt->rcv_buf(cs->conn, buf, count, flags);
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200125 if (conn_xprt_read0_pending(cs->conn))
126 cs->flags |= CS_FL_EOS;
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200127 if (cs->conn->flags & CO_FL_ERROR)
128 cs->flags |= CS_FL_ERROR;
Willy Tarreaud9cf5402018-07-18 11:29:06 +0200129 return ret;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200130}
131
132/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +0200133static 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 +0200134{
Christopher Fauletd44a9b32018-07-27 11:59:41 +0200135 size_t ret = cs->conn->xprt->snd_buf(cs->conn, buf, count, flags);
136
137 if (ret > 0)
138 b_del(buf, ret);
139 return ret;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200140}
141
Olivier Houchard6ff20392018-07-17 18:46:31 +0200142/* Called from the upper layer, to subscribe to events */
143static int mux_pt_subscribe(struct conn_stream *cs, int event_type, void *param)
144{
145 return (cs->conn->xprt->subscribe(cs->conn, event_type, param));
146}
147
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200148static int mux_pt_unsubscribe(struct conn_stream *cs, int event_type, void *param)
149{
150 return (cs->conn->xprt->unsubscribe(cs->conn, event_type, param));
151}
152
Olivier Houchard7da120b2017-11-01 13:55:10 +0100153#if defined(CONFIG_HAP_LINUX_SPLICE)
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200154/* Send and get, using splicing */
155static int mux_pt_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
156{
157 int ret;
158
159 ret = cs->conn->xprt->rcv_pipe(cs->conn, pipe, 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
167static int mux_pt_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
168{
169 return (cs->conn->xprt->snd_pipe(cs->conn, pipe));
170}
Olivier Houchard7da120b2017-11-01 13:55:10 +0100171#endif
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200172
Willy Tarreau53a47662017-08-28 10:53:00 +0200173/* The mux operations */
174const struct mux_ops mux_pt_ops = {
175 .init = mux_pt_init,
Willy Tarreau53a47662017-08-28 10:53:00 +0200176 .wake = mux_pt_wake,
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200177 .rcv_buf = mux_pt_rcv_buf,
178 .snd_buf = mux_pt_snd_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +0200179 .subscribe = mux_pt_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200180 .unsubscribe = mux_pt_unsubscribe,
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200181#if defined(CONFIG_HAP_LINUX_SPLICE)
182 .rcv_pipe = mux_pt_rcv_pipe,
183 .snd_pipe = mux_pt_snd_pipe,
184#endif
185 .attach = mux_pt_attach,
186 .detach = mux_pt_detach,
187 .shutr = mux_pt_shutr,
188 .shutw = mux_pt_shutw,
Willy Tarreau28f1cb92017-12-20 16:14:44 +0100189 .flags = MX_FL_NONE,
Willy Tarreau53a47662017-08-28 10:53:00 +0200190 .name = "PASS",
191};
Willy Tarreauf6490822017-09-21 19:43:21 +0200192
Christopher Faulet32f61c02018-04-10 14:33:41 +0200193/* PROT selection : default mux has empty name */
194static struct mux_proto_list mux_proto_pt =
195 { .token = IST(""), .mode = PROTO_MODE_ANY, .side = PROTO_SIDE_BOTH, .mux = &mux_pt_ops };
Willy Tarreauf6490822017-09-21 19:43:21 +0200196
197__attribute__((constructor))
198static void __mux_pt_init(void)
199{
Christopher Faulet32f61c02018-04-10 14:33:41 +0200200 register_mux_proto(&mux_proto_pt);
Willy Tarreauf6490822017-09-21 19:43:21 +0200201}