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 | */ |
Willy Tarreau | 175a2bb | 2018-09-12 12:02:05 +0200 | [diff] [blame] | 22 | static int mux_pt_init(struct connection *conn, struct proxy *prx) |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 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 | |
Willy Tarreau | ad7f0ad | 2018-08-24 15:48:59 +0200 | [diff] [blame] | 54 | if (ret < 0) |
| 55 | return ret; |
| 56 | |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 57 | /* 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 Tarreau | ed339a3 | 2017-11-03 15:55:24 +0100 | [diff] [blame] | 63 | return ret; |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 64 | } |
| 65 | |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 66 | /* |
| 67 | * Attach a new stream to a connection |
| 68 | * (Used for outgoing connections) |
| 69 | */ |
| 70 | static struct conn_stream *mux_pt_attach(struct connection *conn) |
| 71 | { |
| 72 | return NULL; |
| 73 | } |
| 74 | |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 75 | /* Retrieves a valid conn_stream from this connection, or returns NULL. For |
| 76 | * this mux, it's easy as we can only store a single conn_stream. |
| 77 | */ |
| 78 | static const struct conn_stream *mux_pt_get_first_cs(const struct connection *conn) |
| 79 | { |
| 80 | struct conn_stream *cs = conn->mux_ctx; |
| 81 | |
| 82 | return cs; |
| 83 | } |
| 84 | |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 85 | /* |
Willy Tarreau | 2c52a2b | 2017-10-08 11:00:17 +0200 | [diff] [blame] | 86 | * Detach the stream from the connection and possibly release the connection. |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 87 | */ |
| 88 | static void mux_pt_detach(struct conn_stream *cs) |
| 89 | { |
Willy Tarreau | 2c52a2b | 2017-10-08 11:00:17 +0200 | [diff] [blame] | 90 | struct connection *conn = cs->conn; |
| 91 | |
| 92 | LIST_DEL(&conn->list); |
| 93 | conn_stop_tracking(conn); |
| 94 | conn_full_close(conn); |
Willy Tarreau | 436d333 | 2017-10-08 11:16:46 +0200 | [diff] [blame] | 95 | if (conn->destroy_cb) |
| 96 | conn->destroy_cb(conn); |
Willy Tarreau | 2c52a2b | 2017-10-08 11:00:17 +0200 | [diff] [blame] | 97 | conn_free(conn); |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 98 | } |
| 99 | |
Willy Tarreau | ecdb3fe | 2017-10-05 15:25:48 +0200 | [diff] [blame] | 100 | static void mux_pt_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 101 | { |
Willy Tarreau | 4b79524 | 2017-10-05 18:47:38 +0200 | [diff] [blame] | 102 | if (cs->flags & CS_FL_SHR) |
| 103 | return; |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 104 | if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr) |
Willy Tarreau | ecdb3fe | 2017-10-05 15:25:48 +0200 | [diff] [blame] | 105 | cs->conn->xprt->shutr(cs->conn, (mode == CS_SHR_DRAIN)); |
Willy Tarreau | 4b79524 | 2017-10-05 18:47:38 +0200 | [diff] [blame] | 106 | if (cs->flags & CS_FL_SHW) |
| 107 | conn_full_close(cs->conn); |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 108 | } |
| 109 | |
Willy Tarreau | ecdb3fe | 2017-10-05 15:25:48 +0200 | [diff] [blame] | 110 | static void mux_pt_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 111 | { |
Willy Tarreau | 4b79524 | 2017-10-05 18:47:38 +0200 | [diff] [blame] | 112 | if (cs->flags & CS_FL_SHW) |
| 113 | return; |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 114 | if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutw) |
Willy Tarreau | ecdb3fe | 2017-10-05 15:25:48 +0200 | [diff] [blame] | 115 | cs->conn->xprt->shutw(cs->conn, (mode == CS_SHW_NORMAL)); |
Willy Tarreau | 4b79524 | 2017-10-05 18:47:38 +0200 | [diff] [blame] | 116 | if (!(cs->flags & CS_FL_SHR)) |
Willy Tarreau | a48c141 | 2017-12-22 18:46:33 +0100 | [diff] [blame] | 117 | conn_sock_shutw(cs->conn, (mode == CS_SHW_NORMAL)); |
Willy Tarreau | 4b79524 | 2017-10-05 18:47:38 +0200 | [diff] [blame] | 118 | else |
| 119 | conn_full_close(cs->conn); |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /* |
| 123 | * Called from the upper layer, to get more data |
| 124 | */ |
Willy Tarreau | 7f3225f | 2018-06-19 06:15:17 +0200 | [diff] [blame] | 125 | static size_t mux_pt_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 126 | { |
Willy Tarreau | d9cf540 | 2018-07-18 11:29:06 +0200 | [diff] [blame] | 127 | size_t ret; |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 128 | |
Christopher Faulet | 4eb7d74 | 2018-10-11 15:29:21 +0200 | [diff] [blame] | 129 | if (!count) { |
| 130 | cs->flags |= CS_FL_RCV_MORE; |
| 131 | return 0; |
| 132 | } |
| 133 | cs->flags &= ~CS_FL_RCV_MORE; |
Willy Tarreau | 7f3225f | 2018-06-19 06:15:17 +0200 | [diff] [blame] | 134 | ret = cs->conn->xprt->rcv_buf(cs->conn, buf, count, flags); |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 135 | if (conn_xprt_read0_pending(cs->conn)) |
| 136 | cs->flags |= CS_FL_EOS; |
Willy Tarreau | 4ff3b89 | 2017-10-16 15:17:17 +0200 | [diff] [blame] | 137 | if (cs->conn->flags & CO_FL_ERROR) |
| 138 | cs->flags |= CS_FL_ERROR; |
Willy Tarreau | d9cf540 | 2018-07-18 11:29:06 +0200 | [diff] [blame] | 139 | return ret; |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /* Called from the upper layer, to send data */ |
Christopher Faulet | d44a9b3 | 2018-07-27 11:59:41 +0200 | [diff] [blame] | 143 | static size_t mux_pt_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 144 | { |
Christopher Faulet | d44a9b3 | 2018-07-27 11:59:41 +0200 | [diff] [blame] | 145 | size_t ret = cs->conn->xprt->snd_buf(cs->conn, buf, count, flags); |
| 146 | |
| 147 | if (ret > 0) |
| 148 | b_del(buf, ret); |
| 149 | return ret; |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 150 | } |
| 151 | |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 152 | /* Called from the upper layer, to subscribe to events */ |
| 153 | static int mux_pt_subscribe(struct conn_stream *cs, int event_type, void *param) |
| 154 | { |
| 155 | return (cs->conn->xprt->subscribe(cs->conn, event_type, param)); |
| 156 | } |
| 157 | |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 158 | static int mux_pt_unsubscribe(struct conn_stream *cs, int event_type, void *param) |
| 159 | { |
| 160 | return (cs->conn->xprt->unsubscribe(cs->conn, event_type, param)); |
| 161 | } |
| 162 | |
Olivier Houchard | 7da120b | 2017-11-01 13:55:10 +0100 | [diff] [blame] | 163 | #if defined(CONFIG_HAP_LINUX_SPLICE) |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 164 | /* Send and get, using splicing */ |
| 165 | static int mux_pt_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count) |
| 166 | { |
| 167 | int ret; |
| 168 | |
| 169 | ret = cs->conn->xprt->rcv_pipe(cs->conn, pipe, count); |
| 170 | if (conn_xprt_read0_pending(cs->conn)) |
| 171 | cs->flags |= CS_FL_EOS; |
Willy Tarreau | 4ff3b89 | 2017-10-16 15:17:17 +0200 | [diff] [blame] | 172 | if (cs->conn->flags & CO_FL_ERROR) |
| 173 | cs->flags |= CS_FL_ERROR; |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 174 | return (ret); |
| 175 | } |
| 176 | |
| 177 | static int mux_pt_snd_pipe(struct conn_stream *cs, struct pipe *pipe) |
| 178 | { |
| 179 | return (cs->conn->xprt->snd_pipe(cs->conn, pipe)); |
| 180 | } |
Olivier Houchard | 7da120b | 2017-11-01 13:55:10 +0100 | [diff] [blame] | 181 | #endif |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 182 | |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 183 | /* The mux operations */ |
| 184 | const struct mux_ops mux_pt_ops = { |
| 185 | .init = mux_pt_init, |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 186 | .wake = mux_pt_wake, |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 187 | .rcv_buf = mux_pt_rcv_buf, |
| 188 | .snd_buf = mux_pt_snd_buf, |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 189 | .subscribe = mux_pt_subscribe, |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 190 | .unsubscribe = mux_pt_unsubscribe, |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 191 | #if defined(CONFIG_HAP_LINUX_SPLICE) |
| 192 | .rcv_pipe = mux_pt_rcv_pipe, |
| 193 | .snd_pipe = mux_pt_snd_pipe, |
| 194 | #endif |
| 195 | .attach = mux_pt_attach, |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 196 | .get_first_cs = mux_pt_get_first_cs, |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 197 | .detach = mux_pt_detach, |
| 198 | .shutr = mux_pt_shutr, |
| 199 | .shutw = mux_pt_shutw, |
Willy Tarreau | 28f1cb9 | 2017-12-20 16:14:44 +0100 | [diff] [blame] | 200 | .flags = MX_FL_NONE, |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 201 | .name = "PASS", |
| 202 | }; |
Willy Tarreau | f649082 | 2017-09-21 19:43:21 +0200 | [diff] [blame] | 203 | |
Christopher Faulet | 32f61c0 | 2018-04-10 14:33:41 +0200 | [diff] [blame] | 204 | /* PROT selection : default mux has empty name */ |
| 205 | static struct mux_proto_list mux_proto_pt = |
| 206 | { .token = IST(""), .mode = PROTO_MODE_ANY, .side = PROTO_SIDE_BOTH, .mux = &mux_pt_ops }; |
Willy Tarreau | f649082 | 2017-09-21 19:43:21 +0200 | [diff] [blame] | 207 | |
| 208 | __attribute__((constructor)) |
| 209 | static void __mux_pt_init(void) |
| 210 | { |
Christopher Faulet | 32f61c0 | 2018-04-10 14:33:41 +0200 | [diff] [blame] | 211 | register_mux_proto(&mux_proto_pt); |
Willy Tarreau | f649082 | 2017-09-21 19:43:21 +0200 | [diff] [blame] | 212 | } |