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