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 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 13 | #include <haproxy/api.h> |
Willy Tarreau | 16f958c | 2020-06-03 08:44:35 +0200 | [diff] [blame] | 14 | #include <haproxy/buf.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 15 | #include <haproxy/connection.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 16 | #include <haproxy/stream.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 17 | #include <haproxy/task.h> |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 18 | |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 19 | struct mux_pt_ctx { |
| 20 | struct conn_stream *cs; |
| 21 | struct connection *conn; |
| 22 | struct wait_event wait_event; |
| 23 | }; |
| 24 | |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 25 | DECLARE_STATIC_POOL(pool_head_pt_ctx, "mux_pt", sizeof(struct mux_pt_ctx)); |
| 26 | |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 27 | static void mux_pt_destroy(struct mux_pt_ctx *ctx) |
| 28 | { |
Christopher Faulet | 5a7ca29 | 2020-11-03 09:11:43 +0100 | [diff] [blame] | 29 | struct connection *conn = NULL; |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 30 | |
Christopher Faulet | 5a7ca29 | 2020-11-03 09:11:43 +0100 | [diff] [blame] | 31 | if (ctx) { |
| 32 | /* The connection must be attached to this mux to be released */ |
| 33 | if (ctx->conn && ctx->conn->ctx == ctx) |
| 34 | conn = ctx->conn; |
| 35 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 36 | tasklet_free(ctx->wait_event.tasklet); |
Christopher Faulet | 5a7ca29 | 2020-11-03 09:11:43 +0100 | [diff] [blame] | 37 | |
| 38 | if (conn && ctx->wait_event.events != 0) |
| 39 | conn->xprt->unsubscribe(conn, conn->xprt_ctx, ctx->wait_event.events, |
| 40 | &ctx->wait_event); |
| 41 | pool_free(pool_head_pt_ctx, ctx); |
| 42 | } |
| 43 | |
| 44 | if (conn) { |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 45 | conn->mux = NULL; |
| 46 | conn->ctx = NULL; |
Christopher Faulet | 5a7ca29 | 2020-11-03 09:11:43 +0100 | [diff] [blame] | 47 | |
| 48 | conn_stop_tracking(conn); |
| 49 | conn_full_close(conn); |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 50 | if (conn->destroy_cb) |
| 51 | conn->destroy_cb(conn); |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 52 | conn_free(conn); |
| 53 | } |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 54 | } |
| 55 | |
Willy Tarreau | 691d503 | 2021-01-20 14:55:01 +0100 | [diff] [blame] | 56 | /* Callback, used when we get I/Os while in idle mode. This one is exported so |
| 57 | * that "show fd" can resolve it. |
| 58 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 59 | struct task *mux_pt_io_cb(struct task *t, void *tctx, unsigned int status) |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 60 | { |
| 61 | struct mux_pt_ctx *ctx = tctx; |
| 62 | |
Olivier Houchard | ea510fc | 2019-10-18 13:56:40 +0200 | [diff] [blame] | 63 | if (ctx->cs) { |
| 64 | /* There's a small race condition. |
| 65 | * mux_pt_io_cb() is only supposed to be called if we have no |
| 66 | * stream attached. However, maybe the tasklet got woken up, |
| 67 | * and this connection was then attached to a new stream. |
Olivier Houchard | 2ed389d | 2019-10-18 14:18:29 +0200 | [diff] [blame] | 68 | * If this happened, just wake the tasklet up if anybody |
| 69 | * subscribed to receive events, and otherwise call the wake |
| 70 | * method, to make sure the event is noticed. |
Olivier Houchard | ea510fc | 2019-10-18 13:56:40 +0200 | [diff] [blame] | 71 | */ |
Willy Tarreau | 7872d1f | 2020-01-10 07:06:05 +0100 | [diff] [blame] | 72 | if (ctx->conn->subs) { |
| 73 | ctx->conn->subs->events = 0; |
| 74 | tasklet_wakeup(ctx->conn->subs->tasklet); |
| 75 | ctx->conn->subs = NULL; |
Olivier Houchard | 2ed389d | 2019-10-18 14:18:29 +0200 | [diff] [blame] | 76 | } else if (ctx->cs->data_cb->wake) |
Olivier Houchard | ea510fc | 2019-10-18 13:56:40 +0200 | [diff] [blame] | 77 | ctx->cs->data_cb->wake(ctx->cs); |
Willy Tarreau | 7416314 | 2021-03-13 11:30:19 +0100 | [diff] [blame] | 78 | return t; |
Olivier Houchard | ea510fc | 2019-10-18 13:56:40 +0200 | [diff] [blame] | 79 | } |
Willy Tarreau | 2ded48d | 2020-12-11 16:20:34 +0100 | [diff] [blame] | 80 | conn_ctrl_drain(ctx->conn); |
Willy Tarreau | 7416314 | 2021-03-13 11:30:19 +0100 | [diff] [blame] | 81 | if (ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) { |
Olivier Houchard | 9dce2c5 | 2019-10-18 10:59:30 +0200 | [diff] [blame] | 82 | mux_pt_destroy(ctx); |
Willy Tarreau | 7416314 | 2021-03-13 11:30:19 +0100 | [diff] [blame] | 83 | t = NULL; |
| 84 | } |
Olivier Houchard | 9dce2c5 | 2019-10-18 10:59:30 +0200 | [diff] [blame] | 85 | else |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 86 | ctx->conn->xprt->subscribe(ctx->conn, ctx->conn->xprt_ctx, SUB_RETRY_RECV, |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 87 | &ctx->wait_event); |
| 88 | |
Willy Tarreau | 7416314 | 2021-03-13 11:30:19 +0100 | [diff] [blame] | 89 | return t; |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 90 | } |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 91 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 92 | /* Initialize the mux once it's attached. It is expected that conn->ctx |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 93 | * points to the existing conn_stream (for outgoing connections) or NULL (for |
| 94 | * incoming ones, in which case one will be allocated and a new stream will be |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 95 | * instantiated). Returns < 0 on error. |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 96 | */ |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 97 | static int mux_pt_init(struct connection *conn, struct proxy *prx, struct session *sess, |
| 98 | struct buffer *input) |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 99 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 100 | struct conn_stream *cs = conn->ctx; |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 101 | struct mux_pt_ctx *ctx = pool_alloc(pool_head_pt_ctx); |
| 102 | |
| 103 | if (!ctx) |
| 104 | goto fail; |
| 105 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 106 | ctx->wait_event.tasklet = tasklet_new(); |
| 107 | if (!ctx->wait_event.tasklet) |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 108 | goto fail_free_ctx; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 109 | ctx->wait_event.tasklet->context = ctx; |
| 110 | ctx->wait_event.tasklet->process = mux_pt_io_cb; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 111 | ctx->wait_event.events = 0; |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 112 | ctx->conn = conn; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 113 | |
| 114 | if (!cs) { |
Christopher Faulet | 236c93b | 2020-07-02 09:19:54 +0200 | [diff] [blame] | 115 | cs = cs_new(conn, conn->target); |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 116 | if (!cs) |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 117 | goto fail_free_ctx; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 118 | |
Christopher Faulet | 26256f8 | 2020-09-14 11:40:13 +0200 | [diff] [blame] | 119 | if (stream_create_from_cs(cs, &BUF_NULL) < 0) |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 120 | goto fail_free; |
| 121 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 122 | } |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 123 | conn->ctx = ctx; |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 124 | ctx->cs = cs; |
Olivier Houchard | 8706c81 | 2018-12-04 19:17:25 +0100 | [diff] [blame] | 125 | cs->flags |= CS_FL_RCV_MORE; |
Willy Tarreau | 17ccd1a | 2020-01-17 16:19:34 +0100 | [diff] [blame] | 126 | if (global.tune.options & GTUNE_USE_SPLICE) |
| 127 | cs->flags |= CS_FL_MAY_SPLICE; |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 128 | return 0; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 129 | |
| 130 | fail_free: |
| 131 | cs_free(cs); |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 132 | fail_free_ctx: |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 133 | if (ctx->wait_event.tasklet) |
| 134 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 135 | pool_free(pool_head_pt_ctx, ctx); |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 136 | fail: |
| 137 | return -1; |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /* callback to be used by default for the pass-through mux. It calls the data |
| 141 | * layer wake() callback if it is set otherwise returns 0. |
| 142 | */ |
| 143 | static int mux_pt_wake(struct connection *conn) |
| 144 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 145 | struct mux_pt_ctx *ctx = conn->ctx; |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 146 | struct conn_stream *cs = ctx->cs; |
| 147 | int ret = 0; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 148 | |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 149 | if (cs) { |
| 150 | ret = cs->data_cb->wake ? cs->data_cb->wake(cs) : 0; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 151 | |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 152 | if (ret < 0) |
| 153 | return ret; |
| 154 | } else { |
Willy Tarreau | 2ded48d | 2020-12-11 16:20:34 +0100 | [diff] [blame] | 155 | conn_ctrl_drain(conn); |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 156 | if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH)) { |
| 157 | mux_pt_destroy(ctx); |
| 158 | return -1; |
| 159 | } |
| 160 | } |
Willy Tarreau | ad7f0ad | 2018-08-24 15:48:59 +0200 | [diff] [blame] | 161 | |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 162 | /* If we had early data, and we're done with the handshake |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 163 | * then we know the data are safe, and we can remove the flag. |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 164 | */ |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 165 | if ((conn->flags & (CO_FL_EARLY_DATA | CO_FL_EARLY_SSL_HS | CO_FL_WAIT_XPRT)) == |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 166 | CO_FL_EARLY_DATA) |
| 167 | conn->flags &= ~CO_FL_EARLY_DATA; |
Willy Tarreau | ed339a3 | 2017-11-03 15:55:24 +0100 | [diff] [blame] | 168 | return ret; |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 169 | } |
| 170 | |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 171 | /* |
| 172 | * Attach a new stream to a connection |
| 173 | * (Used for outgoing connections) |
| 174 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 175 | static struct conn_stream *mux_pt_attach(struct connection *conn, struct session *sess) |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 176 | { |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 177 | struct conn_stream *cs; |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 178 | struct mux_pt_ctx *ctx = conn->ctx; |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 179 | |
Olivier Houchard | ea32b0f | 2019-08-10 23:56:16 +0200 | [diff] [blame] | 180 | if (ctx->wait_event.events) |
| 181 | conn->xprt->unsubscribe(ctx->conn, conn->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event); |
Christopher Faulet | 236c93b | 2020-07-02 09:19:54 +0200 | [diff] [blame] | 182 | cs = cs_new(conn, conn->target); |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 183 | if (!cs) |
| 184 | goto fail; |
| 185 | |
| 186 | ctx->cs = cs; |
Olivier Houchard | 8706c81 | 2018-12-04 19:17:25 +0100 | [diff] [blame] | 187 | cs->flags |= CS_FL_RCV_MORE; |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 188 | return (cs); |
| 189 | fail: |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 190 | return NULL; |
| 191 | } |
| 192 | |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 193 | /* Retrieves a valid conn_stream from this connection, or returns NULL. For |
| 194 | * this mux, it's easy as we can only store a single conn_stream. |
| 195 | */ |
| 196 | static const struct conn_stream *mux_pt_get_first_cs(const struct connection *conn) |
| 197 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 198 | struct mux_pt_ctx *ctx = conn->ctx; |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 199 | struct conn_stream *cs = ctx->cs; |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 200 | |
| 201 | return cs; |
| 202 | } |
| 203 | |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 204 | /* Destroy the mux and the associated connection if still attached to this mux |
| 205 | * and no longer used */ |
| 206 | static void mux_pt_destroy_meth(void *ctx) |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 207 | { |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 208 | struct mux_pt_ctx *pt = ctx; |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 209 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 210 | if (!(pt->cs) || !(pt->conn) || pt->conn->ctx != pt) |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 211 | mux_pt_destroy(pt); |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 212 | } |
| 213 | |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 214 | /* |
Willy Tarreau | 2c52a2b | 2017-10-08 11:00:17 +0200 | [diff] [blame] | 215 | * Detach the stream from the connection and possibly release the connection. |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 216 | */ |
| 217 | static void mux_pt_detach(struct conn_stream *cs) |
| 218 | { |
Willy Tarreau | 2c52a2b | 2017-10-08 11:00:17 +0200 | [diff] [blame] | 219 | struct connection *conn = cs->conn; |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 220 | struct mux_pt_ctx *ctx = cs->conn->ctx; |
Willy Tarreau | 2c52a2b | 2017-10-08 11:00:17 +0200 | [diff] [blame] | 221 | |
Olivier Houchard | b6c32ee | 2018-11-05 18:28:43 +0100 | [diff] [blame] | 222 | /* Subscribe, to know if we got disconnected */ |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 223 | if (conn->owner != NULL && |
| 224 | !(conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) { |
| 225 | ctx->cs = NULL; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 226 | conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event); |
Olivier Houchard | 9dce2c5 | 2019-10-18 10:59:30 +0200 | [diff] [blame] | 227 | } else |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 228 | /* There's no session attached to that connection, destroy it */ |
| 229 | mux_pt_destroy(ctx); |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 230 | } |
| 231 | |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 232 | /* returns the number of streams in use on a connection */ |
| 233 | static int mux_pt_used_streams(struct connection *conn) |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 234 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 235 | struct mux_pt_ctx *ctx = conn->ctx; |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 236 | |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 237 | return ctx->cs ? 1 : 0; |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 238 | } |
| 239 | |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 240 | /* returns the number of streams still available on a connection */ |
| 241 | static int mux_pt_avail_streams(struct connection *conn) |
Olivier Houchard | 8defe4b | 2018-12-02 01:31:17 +0100 | [diff] [blame] | 242 | { |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 243 | return 1 - mux_pt_used_streams(conn); |
Olivier Houchard | 8defe4b | 2018-12-02 01:31:17 +0100 | [diff] [blame] | 244 | } |
| 245 | |
Willy Tarreau | ecdb3fe | 2017-10-05 15:25:48 +0200 | [diff] [blame] | 246 | 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] | 247 | { |
Willy Tarreau | 4b79524 | 2017-10-05 18:47:38 +0200 | [diff] [blame] | 248 | if (cs->flags & CS_FL_SHR) |
| 249 | return; |
Christopher Faulet | d94f877 | 2018-12-17 13:21:02 +0100 | [diff] [blame] | 250 | cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 251 | if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 252 | cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx, |
| 253 | (mode == CS_SHR_DRAIN)); |
Willy Tarreau | 7d7b11c | 2020-12-11 11:07:19 +0100 | [diff] [blame] | 254 | else if (mode == CS_SHR_DRAIN) |
Willy Tarreau | 2ded48d | 2020-12-11 16:20:34 +0100 | [diff] [blame] | 255 | conn_ctrl_drain(cs->conn); |
Willy Tarreau | 4b79524 | 2017-10-05 18:47:38 +0200 | [diff] [blame] | 256 | if (cs->flags & CS_FL_SHW) |
| 257 | conn_full_close(cs->conn); |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 258 | } |
| 259 | |
Willy Tarreau | ecdb3fe | 2017-10-05 15:25:48 +0200 | [diff] [blame] | 260 | 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] | 261 | { |
Willy Tarreau | 4b79524 | 2017-10-05 18:47:38 +0200 | [diff] [blame] | 262 | if (cs->flags & CS_FL_SHW) |
| 263 | return; |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 264 | if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutw) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 265 | cs->conn->xprt->shutw(cs->conn, cs->conn->xprt_ctx, |
| 266 | (mode == CS_SHW_NORMAL)); |
Willy Tarreau | 4b79524 | 2017-10-05 18:47:38 +0200 | [diff] [blame] | 267 | if (!(cs->flags & CS_FL_SHR)) |
Willy Tarreau | a48c141 | 2017-12-22 18:46:33 +0100 | [diff] [blame] | 268 | conn_sock_shutw(cs->conn, (mode == CS_SHW_NORMAL)); |
Willy Tarreau | 4b79524 | 2017-10-05 18:47:38 +0200 | [diff] [blame] | 269 | else |
| 270 | conn_full_close(cs->conn); |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /* |
| 274 | * Called from the upper layer, to get more data |
| 275 | */ |
Willy Tarreau | 7f3225f | 2018-06-19 06:15:17 +0200 | [diff] [blame] | 276 | 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] | 277 | { |
Willy Tarreau | d9cf540 | 2018-07-18 11:29:06 +0200 | [diff] [blame] | 278 | size_t ret; |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 279 | |
Christopher Faulet | 4eb7d74 | 2018-10-11 15:29:21 +0200 | [diff] [blame] | 280 | if (!count) { |
Christopher Faulet | d94f877 | 2018-12-17 13:21:02 +0100 | [diff] [blame] | 281 | cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Christopher Faulet | 4eb7d74 | 2018-10-11 15:29:21 +0200 | [diff] [blame] | 282 | return 0; |
| 283 | } |
Willy Tarreau | e0f24ee | 2018-12-14 10:51:23 +0100 | [diff] [blame] | 284 | b_realign_if_empty(buf); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 285 | ret = cs->conn->xprt->rcv_buf(cs->conn, cs->conn->xprt_ctx, buf, count, flags); |
Olivier Houchard | 8706c81 | 2018-12-04 19:17:25 +0100 | [diff] [blame] | 286 | if (conn_xprt_read0_pending(cs->conn)) { |
Willy Tarreau | 9cca8df | 2019-07-15 06:47:54 +0200 | [diff] [blame] | 287 | cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Christopher Faulet | 87a8f35 | 2019-03-22 14:51:36 +0100 | [diff] [blame] | 288 | cs->flags |= CS_FL_EOS; |
Olivier Houchard | 8706c81 | 2018-12-04 19:17:25 +0100 | [diff] [blame] | 289 | } |
| 290 | if (cs->conn->flags & CO_FL_ERROR) { |
Willy Tarreau | 9cca8df | 2019-07-15 06:47:54 +0200 | [diff] [blame] | 291 | cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
Willy Tarreau | 4ff3b89 | 2017-10-16 15:17:17 +0200 | [diff] [blame] | 292 | cs->flags |= CS_FL_ERROR; |
Olivier Houchard | 8706c81 | 2018-12-04 19:17:25 +0100 | [diff] [blame] | 293 | } |
Willy Tarreau | d9cf540 | 2018-07-18 11:29:06 +0200 | [diff] [blame] | 294 | return ret; |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /* Called from the upper layer, to send data */ |
Christopher Faulet | d44a9b3 | 2018-07-27 11:59:41 +0200 | [diff] [blame] | 298 | 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] | 299 | { |
Olivier Houchard | b72d98a | 2018-11-30 13:17:48 +0100 | [diff] [blame] | 300 | size_t ret; |
| 301 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 302 | ret = cs->conn->xprt->snd_buf(cs->conn, cs->conn->xprt_ctx, buf, count, flags); |
Christopher Faulet | d44a9b3 | 2018-07-27 11:59:41 +0200 | [diff] [blame] | 303 | |
| 304 | if (ret > 0) |
| 305 | b_del(buf, ret); |
| 306 | return ret; |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 307 | } |
| 308 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 309 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 310 | * event subscriber <es> is not allowed to change from a previous call as long |
| 311 | * as at least one event is still subscribed. The <event_type> must only be a |
| 312 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0. |
| 313 | */ |
| 314 | static int mux_pt_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es) |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 315 | { |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 316 | return cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, event_type, es); |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 317 | } |
| 318 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 319 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 320 | * The <es> pointer is not allowed to differ from the one passed to the |
| 321 | * subscribe() call. It always returns zero. |
| 322 | */ |
| 323 | static int mux_pt_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es) |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 324 | { |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 325 | return cs->conn->xprt->unsubscribe(cs->conn, cs->conn->xprt_ctx, event_type, es); |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 326 | } |
| 327 | |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 328 | #if defined(USE_LINUX_SPLICE) |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 329 | /* Send and get, using splicing */ |
| 330 | static int mux_pt_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count) |
| 331 | { |
| 332 | int ret; |
| 333 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 334 | ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count); |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 335 | if (conn_xprt_read0_pending(cs->conn)) |
Christopher Faulet | 87a8f35 | 2019-03-22 14:51:36 +0100 | [diff] [blame] | 336 | cs->flags |= CS_FL_EOS; |
Willy Tarreau | 4ff3b89 | 2017-10-16 15:17:17 +0200 | [diff] [blame] | 337 | if (cs->conn->flags & CO_FL_ERROR) |
| 338 | cs->flags |= CS_FL_ERROR; |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 339 | return (ret); |
| 340 | } |
| 341 | |
| 342 | static int mux_pt_snd_pipe(struct conn_stream *cs, struct pipe *pipe) |
| 343 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 344 | return (cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe)); |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 345 | } |
Olivier Houchard | 7da120b | 2017-11-01 13:55:10 +0100 | [diff] [blame] | 346 | #endif |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 347 | |
Olivier Houchard | 9b8e11e | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 348 | static int mux_pt_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output) |
| 349 | { |
| 350 | int ret = 0; |
| 351 | switch (mux_ctl) { |
| 352 | case MUX_STATUS: |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 353 | if (!(conn->flags & CO_FL_WAIT_XPRT)) |
Olivier Houchard | 9b8e11e | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 354 | ret |= MUX_STATUS_READY; |
| 355 | return ret; |
Christopher Faulet | 4c8ad84 | 2020-10-06 14:59:17 +0200 | [diff] [blame] | 356 | case MUX_EXIT_STATUS: |
| 357 | return MUX_ES_UNKNOWN; |
Olivier Houchard | 9b8e11e | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 358 | default: |
| 359 | return -1; |
| 360 | } |
| 361 | } |
| 362 | |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 363 | /* The mux operations */ |
Christopher Faulet | 28da3f5 | 2021-02-05 16:44:46 +0100 | [diff] [blame] | 364 | const struct mux_ops mux_tcp_ops = { |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 365 | .init = mux_pt_init, |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 366 | .wake = mux_pt_wake, |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 367 | .rcv_buf = mux_pt_rcv_buf, |
| 368 | .snd_buf = mux_pt_snd_buf, |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 369 | .subscribe = mux_pt_subscribe, |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 370 | .unsubscribe = mux_pt_unsubscribe, |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 371 | #if defined(USE_LINUX_SPLICE) |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 372 | .rcv_pipe = mux_pt_rcv_pipe, |
| 373 | .snd_pipe = mux_pt_snd_pipe, |
| 374 | #endif |
| 375 | .attach = mux_pt_attach, |
Willy Tarreau | fafd398 | 2018-11-18 21:29:20 +0100 | [diff] [blame] | 376 | .get_first_cs = mux_pt_get_first_cs, |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 377 | .detach = mux_pt_detach, |
Olivier Houchard | d540b36 | 2018-11-05 18:37:53 +0100 | [diff] [blame] | 378 | .avail_streams = mux_pt_avail_streams, |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 379 | .used_streams = mux_pt_used_streams, |
Olivier Houchard | 060ed43 | 2018-11-06 16:32:42 +0100 | [diff] [blame] | 380 | .destroy = mux_pt_destroy_meth, |
Olivier Houchard | 9b8e11e | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 381 | .ctl = mux_pt_ctl, |
Olivier Houchard | 7a3f0df | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 382 | .shutr = mux_pt_shutr, |
| 383 | .shutw = mux_pt_shutw, |
Willy Tarreau | 28f1cb9 | 2017-12-20 16:14:44 +0100 | [diff] [blame] | 384 | .flags = MX_FL_NONE, |
Willy Tarreau | 53a4766 | 2017-08-28 10:53:00 +0200 | [diff] [blame] | 385 | .name = "PASS", |
| 386 | }; |
Willy Tarreau | f649082 | 2017-09-21 19:43:21 +0200 | [diff] [blame] | 387 | |
Christopher Faulet | 28da3f5 | 2021-02-05 16:44:46 +0100 | [diff] [blame] | 388 | |
| 389 | const struct mux_ops mux_pt_ops = { |
| 390 | .init = mux_pt_init, |
| 391 | .wake = mux_pt_wake, |
| 392 | .rcv_buf = mux_pt_rcv_buf, |
| 393 | .snd_buf = mux_pt_snd_buf, |
| 394 | .subscribe = mux_pt_subscribe, |
| 395 | .unsubscribe = mux_pt_unsubscribe, |
| 396 | #if defined(USE_LINUX_SPLICE) |
| 397 | .rcv_pipe = mux_pt_rcv_pipe, |
| 398 | .snd_pipe = mux_pt_snd_pipe, |
| 399 | #endif |
| 400 | .attach = mux_pt_attach, |
| 401 | .get_first_cs = mux_pt_get_first_cs, |
| 402 | .detach = mux_pt_detach, |
| 403 | .avail_streams = mux_pt_avail_streams, |
| 404 | .used_streams = mux_pt_used_streams, |
| 405 | .destroy = mux_pt_destroy_meth, |
| 406 | .ctl = mux_pt_ctl, |
| 407 | .shutr = mux_pt_shutr, |
| 408 | .shutw = mux_pt_shutw, |
| 409 | .flags = MX_FL_NONE|MX_FL_NO_UPG, |
| 410 | .name = "PASS", |
| 411 | }; |
| 412 | |
Christopher Faulet | 32f61c0 | 2018-04-10 14:33:41 +0200 | [diff] [blame] | 413 | /* PROT selection : default mux has empty name */ |
Christopher Faulet | 28da3f5 | 2021-02-05 16:44:46 +0100 | [diff] [blame] | 414 | static struct mux_proto_list mux_proto_none = |
| 415 | { .token = IST("none"), .mode = PROTO_MODE_TCP, .side = PROTO_SIDE_BOTH, .mux = &mux_pt_ops }; |
| 416 | static struct mux_proto_list mux_proto_tcp = |
| 417 | { .token = IST(""), .mode = PROTO_MODE_TCP, .side = PROTO_SIDE_BOTH, .mux = &mux_tcp_ops }; |
Willy Tarreau | f649082 | 2017-09-21 19:43:21 +0200 | [diff] [blame] | 418 | |
Christopher Faulet | 28da3f5 | 2021-02-05 16:44:46 +0100 | [diff] [blame] | 419 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_none); |
| 420 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_tcp); |