Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 1 | /* |
| 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 Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 17 | /* 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 Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 21 | */ |
| 22 | static int mux_pt_init(struct connection *conn) |
| 23 | { |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 24 | 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 Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 36 | return 0; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 37 | |
| 38 | fail_free: |
| 39 | cs_free(cs); |
| 40 | fail: |
| 41 | return -1; |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 42 | } |
| 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 | */ |
| 47 | static int mux_pt_wake(struct connection *conn) |
| 48 | { |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 49 | 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 Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 56 | } |
| 57 | |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 58 | /* 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 | */ |
| 62 | static 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 Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 78 | /* 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 | */ |
| 81 | static void mux_pt_recv(struct connection *conn) |
| 82 | { |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 83 | struct conn_stream *cs = conn->mux_ctx; |
| 84 | |
Willy Tarreau | 4ff3b89 | 2017-10-16 15:17:17 +0200 | [diff] [blame] | 85 | if (conn->flags & CO_FL_ERROR) |
| 86 | cs->flags |= CS_FL_ERROR; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 87 | 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 Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 91 | } |
| 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 | */ |
| 96 | static void mux_pt_send(struct connection *conn) |
| 97 | { |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 98 | struct conn_stream *cs = conn->mux_ctx; |
| 99 | |
Willy Tarreau | 4ff3b89 | 2017-10-16 15:17:17 +0200 | [diff] [blame] | 100 | if (conn->flags & CO_FL_ERROR) |
| 101 | cs->flags |= CS_FL_ERROR; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 102 | cs->data_cb->send(cs); |
| 103 | cs_update_mux_polling(cs); |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 104 | } |
| 105 | |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 106 | /* |
| 107 | * Attach a new stream to a connection |
| 108 | * (Used for outgoing connections) |
| 109 | */ |
| 110 | static 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 | */ |
| 119 | static void mux_pt_detach(struct conn_stream *cs) |
| 120 | { |
| 121 | } |
| 122 | |
| 123 | static 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 | |
| 129 | static 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 | */ |
| 138 | static 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 Tarreau | 4ff3b89 | 2017-10-16 15:17:17 +0200 | [diff] [blame] | 145 | if (cs->conn->flags & CO_FL_ERROR) |
| 146 | cs->flags |= CS_FL_ERROR; |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 147 | return (ret); |
| 148 | } |
| 149 | |
| 150 | /* Called from the upper layer, to send data */ |
| 151 | static 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 */ |
| 157 | static 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 Tarreau | 4ff3b89 | 2017-10-16 15:17:17 +0200 | [diff] [blame] | 164 | if (cs->conn->flags & CO_FL_ERROR) |
| 165 | cs->flags |= CS_FL_ERROR; |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 166 | return (ret); |
| 167 | } |
| 168 | |
| 169 | static 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 Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 174 | /* The mux operations */ |
| 175 | const 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 Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 180 | .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 Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 191 | .name = "PASS", |
| 192 | }; |
Willy Tarreau | f649082 | 2017-09-21 19:43:21 +0200 | [diff] [blame] | 193 | |
| 194 | /* ALPN selection : default mux has empty name */ |
| 195 | static struct alpn_mux_list alpn_mux_pt = |
| 196 | { .token = IST(""), .mode = ALPN_MODE_ANY, .mux = &mux_pt_ops }; |
| 197 | |
| 198 | __attribute__((constructor)) |
| 199 | static void __mux_pt_init(void) |
| 200 | { |
| 201 | alpn_register_mux(&alpn_mux_pt); |
| 202 | } |