blob: 9438d641add7b0d49d62379f5944a14dea3f8557 [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/*
116 * Detach the stream from the connection
117 * (Used for outgoing connections)
118 */
119static void mux_pt_detach(struct conn_stream *cs)
120{
121}
122
123static void mux_pt_shutr(struct conn_stream *cs, int clean)
124{
125 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr)
126 cs->conn->xprt->shutr(cs->conn, clean);
127}
128
129static void mux_pt_shutw(struct conn_stream *cs, int clean)
130{
131 if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutw)
132 cs->conn->xprt->shutw(cs->conn, clean);
133}
134
135/*
136 * Called from the upper layer, to get more data
137 */
138static int mux_pt_rcv_buf(struct conn_stream *cs, struct buffer *buf, int count)
139{
140 int ret;
141
142 ret = cs->conn->xprt->rcv_buf(cs->conn, buf, count);
143 if (conn_xprt_read0_pending(cs->conn))
144 cs->flags |= CS_FL_EOS;
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200145 if (cs->conn->flags & CO_FL_ERROR)
146 cs->flags |= CS_FL_ERROR;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200147 return (ret);
148}
149
150/* Called from the upper layer, to send data */
151static int mux_pt_snd_buf(struct conn_stream *cs, struct buffer *buf, int flags)
152{
153 return (cs->conn->xprt->snd_buf(cs->conn, buf, flags));
154}
155
156/* Send and get, using splicing */
157static int mux_pt_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count)
158{
159 int ret;
160
161 ret = cs->conn->xprt->rcv_pipe(cs->conn, pipe, count);
162 if (conn_xprt_read0_pending(cs->conn))
163 cs->flags |= CS_FL_EOS;
Willy Tarreau4ff3b892017-10-16 15:17:17 +0200164 if (cs->conn->flags & CO_FL_ERROR)
165 cs->flags |= CS_FL_ERROR;
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200166 return (ret);
167}
168
169static int mux_pt_snd_pipe(struct conn_stream *cs, struct pipe *pipe)
170{
171 return (cs->conn->xprt->snd_pipe(cs->conn, pipe));
172}
173
Willy Tarreau53a47662017-08-28 10:53:00 +0200174/* The mux operations */
175const struct mux_ops mux_pt_ops = {
176 .init = mux_pt_init,
177 .recv = mux_pt_recv,
178 .send = mux_pt_send,
179 .wake = mux_pt_wake,
Olivier Houchard7a3f0df2017-09-13 18:30:23 +0200180 .update_poll = mux_pt_update_poll,
181 .rcv_buf = mux_pt_rcv_buf,
182 .snd_buf = mux_pt_snd_buf,
183#if defined(CONFIG_HAP_LINUX_SPLICE)
184 .rcv_pipe = mux_pt_rcv_pipe,
185 .snd_pipe = mux_pt_snd_pipe,
186#endif
187 .attach = mux_pt_attach,
188 .detach = mux_pt_detach,
189 .shutr = mux_pt_shutr,
190 .shutw = mux_pt_shutw,
Willy Tarreau53a47662017-08-28 10:53:00 +0200191 .name = "PASS",
192};
Willy Tarreauf6490822017-09-21 19:43:21 +0200193
194/* ALPN selection : default mux has empty name */
195static struct alpn_mux_list alpn_mux_pt =
196 { .token = IST(""), .mode = ALPN_MODE_ANY, .mux = &mux_pt_ops };
197
198__attribute__((constructor))
199static void __mux_pt_init(void)
200{
201 alpn_register_mux(&alpn_mux_pt);
202}