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