Christopher Faulet | 1329f2a | 2021-12-16 17:32:56 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Conn-stream management functions |
| 3 | * |
| 4 | * Copyright 2021 Christopher Faulet <cfaulet@haproxy.com> |
| 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 <haproxy/api.h> |
Christopher Faulet | 3704663 | 2022-04-01 11:36:58 +0200 | [diff] [blame] | 14 | #include <haproxy/applet.h> |
Christopher Faulet | 1329f2a | 2021-12-16 17:32:56 +0100 | [diff] [blame] | 15 | #include <haproxy/connection.h> |
| 16 | #include <haproxy/conn_stream.h> |
Christopher Faulet | 19bd728 | 2022-04-01 13:58:09 +0200 | [diff] [blame] | 17 | #include <haproxy/cs_utils.h> |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 18 | #include <haproxy/check.h> |
| 19 | #include <haproxy/http_ana.h> |
| 20 | #include <haproxy/pipe.h> |
Christopher Faulet | 1329f2a | 2021-12-16 17:32:56 +0100 | [diff] [blame] | 21 | #include <haproxy/pool.h> |
Christopher Faulet | 1329f2a | 2021-12-16 17:32:56 +0100 | [diff] [blame] | 22 | |
| 23 | DECLARE_POOL(pool_head_connstream, "conn_stream", sizeof(struct conn_stream)); |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 24 | DECLARE_POOL(pool_head_cs_endpoint, "cs_endpoint", sizeof(struct cs_endpoint)); |
Christopher Faulet | 1329f2a | 2021-12-16 17:32:56 +0100 | [diff] [blame] | 25 | |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 26 | /* functions used by default on a detached conn-stream */ |
| 27 | static void cs_app_shutr(struct conn_stream *cs); |
| 28 | static void cs_app_shutw(struct conn_stream *cs); |
| 29 | static void cs_app_chk_rcv(struct conn_stream *cs); |
| 30 | static void cs_app_chk_snd(struct conn_stream *cs); |
| 31 | |
| 32 | /* functions used on a mux-based conn-stream */ |
| 33 | static void cs_app_shutr_conn(struct conn_stream *cs); |
| 34 | static void cs_app_shutw_conn(struct conn_stream *cs); |
| 35 | static void cs_app_chk_rcv_conn(struct conn_stream *cs); |
| 36 | static void cs_app_chk_snd_conn(struct conn_stream *cs); |
| 37 | |
| 38 | /* functions used on an applet-based conn-stream */ |
| 39 | static void cs_app_shutr_applet(struct conn_stream *cs); |
| 40 | static void cs_app_shutw_applet(struct conn_stream *cs); |
| 41 | static void cs_app_chk_rcv_applet(struct conn_stream *cs); |
| 42 | static void cs_app_chk_snd_applet(struct conn_stream *cs); |
| 43 | |
| 44 | /* conn-stream operations for connections */ |
| 45 | struct cs_app_ops cs_app_conn_ops = { |
| 46 | .chk_rcv = cs_app_chk_rcv_conn, |
| 47 | .chk_snd = cs_app_chk_snd_conn, |
| 48 | .shutr = cs_app_shutr_conn, |
| 49 | .shutw = cs_app_shutw_conn, |
| 50 | }; |
| 51 | |
| 52 | /* conn-stream operations for embedded tasks */ |
| 53 | struct cs_app_ops cs_app_embedded_ops = { |
| 54 | .chk_rcv = cs_app_chk_rcv, |
| 55 | .chk_snd = cs_app_chk_snd, |
| 56 | .shutr = cs_app_shutr, |
| 57 | .shutw = cs_app_shutw, |
| 58 | }; |
| 59 | |
| 60 | /* conn-stream operations for connections */ |
| 61 | struct cs_app_ops cs_app_applet_ops = { |
| 62 | .chk_rcv = cs_app_chk_rcv_applet, |
| 63 | .chk_snd = cs_app_chk_snd_applet, |
| 64 | .shutr = cs_app_shutr_applet, |
| 65 | .shutw = cs_app_shutw_applet, |
| 66 | }; |
| 67 | |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 68 | static int cs_conn_process(struct conn_stream *cs); |
| 69 | static int cs_conn_recv(struct conn_stream *cs); |
| 70 | static int cs_conn_send(struct conn_stream *cs); |
| 71 | static int cs_applet_process(struct conn_stream *cs); |
| 72 | |
| 73 | struct data_cb cs_data_conn_cb = { |
| 74 | .wake = cs_conn_process, |
| 75 | .name = "STRM", |
| 76 | }; |
| 77 | |
| 78 | struct data_cb cs_data_applet_cb = { |
| 79 | .wake = cs_applet_process, |
| 80 | .name = "STRM", |
| 81 | }; |
| 82 | |
| 83 | |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 84 | |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 85 | void cs_endpoint_init(struct cs_endpoint *endp) |
| 86 | { |
| 87 | endp->target = NULL; |
| 88 | endp->ctx = NULL; |
| 89 | endp->flags = CS_EP_NONE; |
| 90 | } |
| 91 | |
| 92 | struct cs_endpoint *cs_endpoint_new() |
| 93 | { |
| 94 | struct cs_endpoint *endp; |
| 95 | |
| 96 | endp = pool_alloc(pool_head_cs_endpoint); |
| 97 | if (unlikely(!endp)) |
| 98 | return NULL; |
| 99 | |
| 100 | cs_endpoint_init(endp); |
| 101 | return endp; |
| 102 | } |
| 103 | |
| 104 | void cs_endpoint_free(struct cs_endpoint *endp) |
| 105 | { |
| 106 | pool_free(pool_head_cs_endpoint, endp); |
| 107 | } |
Christopher Faulet | 1329f2a | 2021-12-16 17:32:56 +0100 | [diff] [blame] | 108 | |
Christopher Faulet | dd2d0d8 | 2021-12-20 09:34:32 +0100 | [diff] [blame] | 109 | /* Tries to allocate a new conn_stream and initialize its main fields. On |
| 110 | * failure, nothing is allocated and NULL is returned. |
Christopher Faulet | 1329f2a | 2021-12-16 17:32:56 +0100 | [diff] [blame] | 111 | */ |
Christopher Faulet | 265e165 | 2022-04-12 08:49:27 +0200 | [diff] [blame^] | 112 | static struct conn_stream *cs_new(struct cs_endpoint *endp) |
Christopher Faulet | 1329f2a | 2021-12-16 17:32:56 +0100 | [diff] [blame] | 113 | { |
| 114 | struct conn_stream *cs; |
| 115 | |
| 116 | cs = pool_alloc(pool_head_connstream); |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 117 | |
Christopher Faulet | 1329f2a | 2021-12-16 17:32:56 +0100 | [diff] [blame] | 118 | if (unlikely(!cs)) |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 119 | goto alloc_error; |
Christopher Faulet | bb772d0 | 2022-03-22 15:28:36 +0100 | [diff] [blame] | 120 | |
| 121 | cs->obj_type = OBJ_TYPE_CS; |
| 122 | cs->flags = CS_FL_NONE; |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 123 | cs->state = CS_ST_INI; |
Christopher Faulet | 1d98777 | 2022-03-29 18:03:35 +0200 | [diff] [blame] | 124 | cs->hcto = TICK_ETERNITY; |
Christopher Faulet | bb772d0 | 2022-03-22 15:28:36 +0100 | [diff] [blame] | 125 | cs->app = NULL; |
Christopher Faulet | bb772d0 | 2022-03-22 15:28:36 +0100 | [diff] [blame] | 126 | cs->data_cb = NULL; |
Christopher Faulet | 8da67aa | 2022-03-29 17:53:09 +0200 | [diff] [blame] | 127 | cs->src = NULL; |
| 128 | cs->dst = NULL; |
Christopher Faulet | 2f35e7b | 2022-03-31 11:09:28 +0200 | [diff] [blame] | 129 | cs->wait_event.tasklet = NULL; |
| 130 | cs->wait_event.events = 0; |
| 131 | |
Christopher Faulet | b669d68 | 2022-03-22 18:37:19 +0100 | [diff] [blame] | 132 | if (!endp) { |
| 133 | endp = cs_endpoint_new(); |
| 134 | if (unlikely(!endp)) |
| 135 | goto alloc_error; |
| 136 | } |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 137 | cs->endp = endp; |
| 138 | |
Christopher Faulet | 1329f2a | 2021-12-16 17:32:56 +0100 | [diff] [blame] | 139 | return cs; |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 140 | |
| 141 | alloc_error: |
| 142 | pool_free(pool_head_connstream, cs); |
| 143 | return NULL; |
Christopher Faulet | 1329f2a | 2021-12-16 17:32:56 +0100 | [diff] [blame] | 144 | } |
| 145 | |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 146 | struct conn_stream *cs_new_from_mux(struct cs_endpoint *endp, struct session *sess, struct buffer *input) |
| 147 | { |
| 148 | struct conn_stream *cs; |
| 149 | |
| 150 | cs = cs_new(endp); |
| 151 | if (unlikely(!cs)) |
| 152 | return NULL; |
| 153 | if (unlikely(!stream_new(sess, cs, input))) { |
| 154 | pool_free(pool_head_connstream, cs); |
| 155 | cs = NULL; |
| 156 | } |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 157 | endp->flags &= ~CS_EP_ORPHAN; |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 158 | return cs; |
| 159 | } |
| 160 | |
| 161 | struct conn_stream *cs_new_from_applet(struct cs_endpoint *endp, struct session *sess, struct buffer *input) |
| 162 | { |
| 163 | struct conn_stream *cs; |
| 164 | struct appctx *appctx = endp->ctx; |
| 165 | |
| 166 | cs = cs_new(endp); |
| 167 | if (unlikely(!cs)) |
| 168 | return NULL; |
| 169 | appctx->owner = cs; |
| 170 | if (unlikely(!stream_new(sess, cs, input))) { |
| 171 | pool_free(pool_head_connstream, cs); |
| 172 | cs = NULL; |
| 173 | } |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 174 | endp->flags &= ~CS_EP_ORPHAN; |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 175 | return cs; |
| 176 | } |
| 177 | |
| 178 | struct conn_stream *cs_new_from_strm(struct stream *strm, unsigned int flags) |
| 179 | { |
| 180 | struct conn_stream *cs; |
| 181 | |
| 182 | cs = cs_new(NULL); |
| 183 | if (unlikely(!cs)) |
| 184 | return NULL; |
| 185 | cs->flags |= flags; |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 186 | cs->endp->flags |= CS_EP_DETACHED; |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 187 | cs->app = &strm->obj_type; |
Christopher Faulet | 0c6a64c | 2022-04-01 08:58:29 +0200 | [diff] [blame] | 188 | cs->ops = &cs_app_embedded_ops; |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 189 | cs->data_cb = NULL; |
| 190 | return cs; |
| 191 | } |
| 192 | |
| 193 | struct conn_stream *cs_new_from_check(struct check *check, unsigned int flags) |
| 194 | { |
| 195 | struct conn_stream *cs; |
| 196 | |
| 197 | cs = cs_new(NULL); |
| 198 | if (unlikely(!cs)) |
| 199 | return NULL; |
| 200 | cs->flags |= flags; |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 201 | cs->endp->flags |= CS_EP_DETACHED; |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 202 | cs->app = &check->obj_type; |
| 203 | cs->data_cb = &check_conn_cb; |
| 204 | return cs; |
| 205 | } |
| 206 | |
Christopher Faulet | 1329f2a | 2021-12-16 17:32:56 +0100 | [diff] [blame] | 207 | /* Releases a conn_stream previously allocated by cs_new(), as well as any |
| 208 | * buffer it would still hold. |
| 209 | */ |
| 210 | void cs_free(struct conn_stream *cs) |
| 211 | { |
Christopher Faulet | 8da67aa | 2022-03-29 17:53:09 +0200 | [diff] [blame] | 212 | sockaddr_free(&cs->src); |
| 213 | sockaddr_free(&cs->dst); |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 214 | if (cs->endp) { |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 215 | BUG_ON(!(cs->endp->flags & CS_EP_DETACHED)); |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 216 | cs_endpoint_free(cs->endp); |
| 217 | } |
Christopher Faulet | 2f35e7b | 2022-03-31 11:09:28 +0200 | [diff] [blame] | 218 | if (cs->wait_event.tasklet) |
| 219 | tasklet_free(cs->wait_event.tasklet); |
Christopher Faulet | 1329f2a | 2021-12-16 17:32:56 +0100 | [diff] [blame] | 220 | pool_free(pool_head_connstream, cs); |
| 221 | } |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 222 | |
| 223 | |
Christopher Faulet | 9388204 | 2022-01-19 14:56:50 +0100 | [diff] [blame] | 224 | /* Attaches a conn_stream to an mux endpoint and sets the endpoint ctx */ |
Christopher Faulet | 070b91b | 2022-03-31 19:27:18 +0200 | [diff] [blame] | 225 | int cs_attach_mux(struct conn_stream *cs, void *target, void *ctx) |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 226 | { |
Christopher Faulet | 9388204 | 2022-01-19 14:56:50 +0100 | [diff] [blame] | 227 | struct connection *conn = ctx; |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 228 | |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 229 | cs->endp->target = target; |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 230 | cs->endp->ctx = ctx; |
| 231 | cs->endp->flags |= CS_EP_T_MUX; |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 232 | cs->endp->flags &= ~CS_EP_DETACHED; |
Christopher Faulet | 9388204 | 2022-01-19 14:56:50 +0100 | [diff] [blame] | 233 | if (!conn->ctx) |
| 234 | conn->ctx = cs; |
| 235 | if (cs_strm(cs)) { |
Christopher Faulet | 2f35e7b | 2022-03-31 11:09:28 +0200 | [diff] [blame] | 236 | if (!cs->wait_event.tasklet) { |
| 237 | cs->wait_event.tasklet = tasklet_new(); |
| 238 | if (!cs->wait_event.tasklet) |
| 239 | return -1; |
Christopher Faulet | 4a7764a | 2022-04-01 16:58:52 +0200 | [diff] [blame] | 240 | cs->wait_event.tasklet->process = cs_conn_io_cb; |
| 241 | cs->wait_event.tasklet->context = cs; |
Christopher Faulet | 2f35e7b | 2022-03-31 11:09:28 +0200 | [diff] [blame] | 242 | cs->wait_event.events = 0; |
| 243 | } |
| 244 | |
Christopher Faulet | 0c6a64c | 2022-04-01 08:58:29 +0200 | [diff] [blame] | 245 | cs->ops = &cs_app_conn_ops; |
Christopher Faulet | 000ba3e | 2022-04-01 17:06:32 +0200 | [diff] [blame] | 246 | cs->data_cb = &cs_data_conn_cb; |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 247 | } |
Christopher Faulet | 9388204 | 2022-01-19 14:56:50 +0100 | [diff] [blame] | 248 | else if (cs_check(cs)) |
| 249 | cs->data_cb = &check_conn_cb; |
Christopher Faulet | 070b91b | 2022-03-31 19:27:18 +0200 | [diff] [blame] | 250 | return 0; |
Christopher Faulet | 9388204 | 2022-01-19 14:56:50 +0100 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /* Attaches a conn_stream to an applet endpoint and sets the endpoint ctx */ |
Christopher Faulet | 265e165 | 2022-04-12 08:49:27 +0200 | [diff] [blame^] | 254 | static void cs_attach_applet(struct conn_stream *cs, void *target, void *ctx) |
Christopher Faulet | 9388204 | 2022-01-19 14:56:50 +0100 | [diff] [blame] | 255 | { |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 256 | struct appctx *appctx = target; |
Christopher Faulet | 9388204 | 2022-01-19 14:56:50 +0100 | [diff] [blame] | 257 | |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 258 | cs->endp->target = target; |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 259 | cs->endp->ctx = ctx; |
| 260 | cs->endp->flags |= CS_EP_T_APPLET; |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 261 | cs->endp->flags &= ~CS_EP_DETACHED; |
Christopher Faulet | 9388204 | 2022-01-19 14:56:50 +0100 | [diff] [blame] | 262 | appctx->owner = cs; |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 263 | if (cs_strm(cs)) { |
Christopher Faulet | 0c6a64c | 2022-04-01 08:58:29 +0200 | [diff] [blame] | 264 | cs->ops = &cs_app_applet_ops; |
Christopher Faulet | 6059ba4 | 2022-04-01 16:34:53 +0200 | [diff] [blame] | 265 | cs->data_cb = &cs_data_applet_cb; |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
| 269 | /* Attaches a conn_stream to a app layer and sets the relevant callbacks */ |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 270 | int cs_attach_strm(struct conn_stream *cs, struct stream *strm) |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 271 | { |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 272 | cs->app = &strm->obj_type; |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 273 | cs->endp->flags &= ~CS_EP_ORPHAN; |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 274 | if (cs->endp->flags & CS_EP_T_MUX) { |
Christopher Faulet | 2f35e7b | 2022-03-31 11:09:28 +0200 | [diff] [blame] | 275 | cs->wait_event.tasklet = tasklet_new(); |
Christopher Faulet | 582a226 | 2022-04-04 11:25:59 +0200 | [diff] [blame] | 276 | if (!cs->wait_event.tasklet) |
Christopher Faulet | 2f35e7b | 2022-03-31 11:09:28 +0200 | [diff] [blame] | 277 | return -1; |
Christopher Faulet | 4a7764a | 2022-04-01 16:58:52 +0200 | [diff] [blame] | 278 | cs->wait_event.tasklet->process = cs_conn_io_cb; |
| 279 | cs->wait_event.tasklet->context = cs; |
Christopher Faulet | 2f35e7b | 2022-03-31 11:09:28 +0200 | [diff] [blame] | 280 | cs->wait_event.events = 0; |
| 281 | |
Christopher Faulet | 0c6a64c | 2022-04-01 08:58:29 +0200 | [diff] [blame] | 282 | cs->ops = &cs_app_conn_ops; |
Christopher Faulet | 000ba3e | 2022-04-01 17:06:32 +0200 | [diff] [blame] | 283 | cs->data_cb = &cs_data_conn_cb; |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 284 | } |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 285 | else if (cs->endp->flags & CS_EP_T_APPLET) { |
Christopher Faulet | 0c6a64c | 2022-04-01 08:58:29 +0200 | [diff] [blame] | 286 | cs->ops = &cs_app_applet_ops; |
Christopher Faulet | 6059ba4 | 2022-04-01 16:34:53 +0200 | [diff] [blame] | 287 | cs->data_cb = &cs_data_applet_cb; |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 288 | } |
| 289 | else { |
Christopher Faulet | 0c6a64c | 2022-04-01 08:58:29 +0200 | [diff] [blame] | 290 | cs->ops = &cs_app_embedded_ops; |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 291 | cs->data_cb = NULL; |
| 292 | } |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | /* Detach the conn_stream from the endpoint, if any. For a connecrion, if a mux |
| 297 | * owns the connection ->detach() callback is called. Otherwise, it means the |
| 298 | * conn-stream owns the connection. In this case the connection is closed and |
| 299 | * released. For an applet, the appctx is released. At the end, the conn-stream |
| 300 | * is not released but some fields a reset. |
| 301 | */ |
| 302 | void cs_detach_endp(struct conn_stream *cs) |
| 303 | { |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 304 | if (!cs->endp) |
| 305 | goto reset_cs; |
| 306 | |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 307 | if (cs->endp->flags & CS_EP_T_MUX) { |
| 308 | struct connection *conn = cs_conn(cs); |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 309 | |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 310 | if (conn->mux) { |
Christopher Faulet | 54e85cb | 2022-01-06 08:46:56 +0100 | [diff] [blame] | 311 | /* TODO: handle unsubscribe for healthchecks too */ |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 312 | cs->endp->flags |= CS_EP_ORPHAN; |
Christopher Faulet | 2f35e7b | 2022-03-31 11:09:28 +0200 | [diff] [blame] | 313 | if (cs->wait_event.events != 0) |
| 314 | conn->mux->unsubscribe(cs, cs->wait_event.events, &cs->wait_event); |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 315 | conn->mux->detach(cs); |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 316 | cs->endp = NULL; |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 317 | } |
| 318 | else { |
| 319 | /* It's too early to have a mux, let's just destroy |
| 320 | * the connection |
| 321 | */ |
| 322 | conn_stop_tracking(conn); |
| 323 | conn_full_close(conn); |
| 324 | if (conn->destroy_cb) |
| 325 | conn->destroy_cb(conn); |
| 326 | conn_free(conn); |
| 327 | } |
| 328 | } |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 329 | else if (cs->endp->flags & CS_EP_T_APPLET) { |
| 330 | struct appctx *appctx = cs_appctx(cs); |
| 331 | |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 332 | cs->endp->flags |= CS_EP_ORPHAN; |
Christopher Faulet | 3704663 | 2022-04-01 11:36:58 +0200 | [diff] [blame] | 333 | cs_applet_release(cs); |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 334 | appctx_free(appctx); |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 335 | cs->endp = NULL; |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 336 | } |
| 337 | |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 338 | if (cs->endp) { |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 339 | /* the cs is the only one one the endpoint */ |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 340 | cs_endpoint_init(cs->endp); |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 341 | cs->endp->flags |= CS_EP_DETACHED; |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 342 | } |
| 343 | |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 344 | reset_cs: |
Christopher Faulet | c36de9d | 2022-01-06 08:44:58 +0100 | [diff] [blame] | 345 | /* FIXME: Rest CS for now but must be reviewed. CS flags are only |
| 346 | * connection related for now but this will evolved |
| 347 | */ |
Christopher Faulet | 3099511 | 2022-03-25 15:32:38 +0100 | [diff] [blame] | 348 | cs->flags &= CS_FL_ISBACK; |
Christopher Faulet | 582a226 | 2022-04-04 11:25:59 +0200 | [diff] [blame] | 349 | if (cs_strm(cs)) |
Christopher Faulet | 0c6a64c | 2022-04-01 08:58:29 +0200 | [diff] [blame] | 350 | cs->ops = &cs_app_embedded_ops; |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 351 | cs->data_cb = NULL; |
Christopher Faulet | c36de9d | 2022-01-06 08:44:58 +0100 | [diff] [blame] | 352 | |
| 353 | if (cs->app == NULL) |
| 354 | cs_free(cs); |
| 355 | } |
| 356 | |
| 357 | void cs_detach_app(struct conn_stream *cs) |
| 358 | { |
Christopher Faulet | c36de9d | 2022-01-06 08:44:58 +0100 | [diff] [blame] | 359 | cs->app = NULL; |
Christopher Faulet | c36de9d | 2022-01-06 08:44:58 +0100 | [diff] [blame] | 360 | cs->data_cb = NULL; |
Christopher Faulet | 8da67aa | 2022-03-29 17:53:09 +0200 | [diff] [blame] | 361 | sockaddr_free(&cs->src); |
| 362 | sockaddr_free(&cs->dst); |
Christopher Faulet | 2f35e7b | 2022-03-31 11:09:28 +0200 | [diff] [blame] | 363 | |
| 364 | if (cs->wait_event.tasklet) |
| 365 | tasklet_free(cs->wait_event.tasklet); |
| 366 | cs->wait_event.tasklet = NULL; |
| 367 | cs->wait_event.events = 0; |
| 368 | |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 369 | if (!cs->endp || (cs->endp->flags & CS_EP_DETACHED)) |
Christopher Faulet | c36de9d | 2022-01-06 08:44:58 +0100 | [diff] [blame] | 370 | cs_free(cs); |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 371 | } |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 372 | |
| 373 | int cs_reset_endp(struct conn_stream *cs) |
| 374 | { |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 375 | struct cs_endpoint *new_endp; |
| 376 | |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 377 | BUG_ON(!cs->app); |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 378 | if (!__cs_endp_target(cs)) { |
| 379 | /* endpoint not attached or attached to a mux with no |
| 380 | * target. Thus the endpoint will not be release but just |
| 381 | * reset |
| 382 | */ |
| 383 | cs_detach_endp(cs); |
| 384 | return 0; |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 385 | } |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 386 | |
| 387 | /* allocate the new endpoint first to be able to set error if it |
| 388 | * fails */ |
| 389 | new_endp = cs_endpoint_new(); |
| 390 | if (!unlikely(new_endp)) { |
| 391 | cs->endp->flags |= CS_EP_ERROR; |
| 392 | return -1; |
| 393 | } |
| 394 | |
| 395 | cs_detach_endp(cs); |
| 396 | BUG_ON(cs->endp); |
| 397 | cs->endp = new_endp; |
| 398 | cs->endp->flags |= CS_EP_DETACHED; |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 399 | return 0; |
| 400 | } |
Christopher Faulet | 3704663 | 2022-04-01 11:36:58 +0200 | [diff] [blame] | 401 | |
| 402 | |
| 403 | /* Register an applet to handle a conn-stream as a new appctx. The CS will |
| 404 | * wake it up every time it is solicited. The appctx must be deleted by the task |
| 405 | * handler using cs_detach_endp(), possibly from within the function itself. |
| 406 | * It also pre-initializes the applet's context and returns it (or NULL in case |
| 407 | * it could not be allocated). |
| 408 | */ |
| 409 | struct appctx *cs_register_applet(struct conn_stream *cs, struct applet *app) |
| 410 | { |
| 411 | struct appctx *appctx; |
| 412 | |
| 413 | DPRINTF(stderr, "registering handler %p for cs %p (was %p)\n", app, cs, cs_strm_task(cs)); |
| 414 | |
| 415 | appctx = appctx_new(app, cs->endp); |
| 416 | if (!appctx) |
| 417 | return NULL; |
| 418 | cs_attach_applet(cs, appctx, appctx); |
| 419 | appctx->owner = cs; |
| 420 | appctx->t->nice = __cs_strm(cs)->task->nice; |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 421 | cs_cant_get(cs); |
Christopher Faulet | 3704663 | 2022-04-01 11:36:58 +0200 | [diff] [blame] | 422 | appctx_wakeup(appctx); |
| 423 | return appctx; |
| 424 | } |
| 425 | |
| 426 | /* call the applet's release function if any. Needs to be called upon close() */ |
| 427 | void cs_applet_release(struct conn_stream *cs) |
| 428 | { |
| 429 | struct appctx *appctx = __cs_appctx(cs); |
| 430 | |
| 431 | if (appctx->applet->release && !cs_state_in(cs->state, CS_SB_DIS|CS_SB_CLO)) |
| 432 | appctx->applet->release(appctx); |
| 433 | } |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 434 | |
| 435 | /* |
| 436 | * This function performs a shutdown-read on a detached conn-stream in a |
| 437 | * connected or init state (it does nothing for other states). It either shuts |
| 438 | * the read side or marks itself as closed. The buffer flags are updated to |
Christopher Faulet | 6b0a0fb | 2022-04-04 11:29:28 +0200 | [diff] [blame] | 439 | * reflect the new state. If the conn-stream has CS_FL_NOHALF, we also |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 440 | * forward the close to the write side. The owner task is woken up if it exists. |
| 441 | */ |
| 442 | static void cs_app_shutr(struct conn_stream *cs) |
| 443 | { |
| 444 | struct channel *ic = cs_ic(cs); |
| 445 | |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 446 | cs_rx_shut_blk(cs); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 447 | if (ic->flags & CF_SHUTR) |
| 448 | return; |
| 449 | ic->flags |= CF_SHUTR; |
| 450 | ic->rex = TICK_ETERNITY; |
| 451 | |
| 452 | if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST)) |
| 453 | return; |
| 454 | |
| 455 | if (cs_oc(cs)->flags & CF_SHUTW) { |
| 456 | cs->state = CS_ST_DIS; |
| 457 | __cs_strm(cs)->conn_exp = TICK_ETERNITY; |
| 458 | } |
| 459 | else if (cs->flags & CS_FL_NOHALF) { |
| 460 | /* we want to immediately forward this close to the write side */ |
| 461 | return cs_app_shutw(cs); |
| 462 | } |
| 463 | |
| 464 | /* note that if the task exists, it must unregister itself once it runs */ |
| 465 | if (!(cs->flags & CS_FL_DONT_WAKE)) |
| 466 | task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO); |
| 467 | } |
| 468 | |
| 469 | /* |
| 470 | * This function performs a shutdown-write on a detached conn-stream in a |
| 471 | * connected or init state (it does nothing for other states). It either shuts |
| 472 | * the write side or marks itself as closed. The buffer flags are updated to |
Christopher Faulet | 6b0a0fb | 2022-04-04 11:29:28 +0200 | [diff] [blame] | 473 | * reflect the new state. It does also close everything if the CS was marked as |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 474 | * being in error state. The owner task is woken up if it exists. |
| 475 | */ |
| 476 | static void cs_app_shutw(struct conn_stream *cs) |
| 477 | { |
| 478 | struct channel *ic = cs_ic(cs); |
| 479 | struct channel *oc = cs_oc(cs); |
| 480 | |
| 481 | oc->flags &= ~CF_SHUTW_NOW; |
| 482 | if (oc->flags & CF_SHUTW) |
| 483 | return; |
| 484 | oc->flags |= CF_SHUTW; |
| 485 | oc->wex = TICK_ETERNITY; |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 486 | cs_done_get(cs); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 487 | |
| 488 | if (tick_isset(cs->hcto)) { |
| 489 | ic->rto = cs->hcto; |
| 490 | ic->rex = tick_add(now_ms, ic->rto); |
| 491 | } |
| 492 | |
| 493 | switch (cs->state) { |
| 494 | case CS_ST_RDY: |
| 495 | case CS_ST_EST: |
| 496 | /* we have to shut before closing, otherwise some short messages |
| 497 | * may never leave the system, especially when there are remaining |
| 498 | * unread data in the socket input buffer, or when nolinger is set. |
| 499 | * However, if CS_FL_NOLINGER is explicitly set, we know there is |
| 500 | * no risk so we close both sides immediately. |
| 501 | */ |
| 502 | if (!(cs->endp->flags & CS_EP_ERROR) && !(cs->flags & CS_FL_NOLINGER) && |
| 503 | !(ic->flags & (CF_SHUTR|CF_DONT_READ))) |
| 504 | return; |
| 505 | |
| 506 | /* fall through */ |
| 507 | case CS_ST_CON: |
| 508 | case CS_ST_CER: |
| 509 | case CS_ST_QUE: |
| 510 | case CS_ST_TAR: |
| 511 | /* Note that none of these states may happen with applets */ |
| 512 | cs->state = CS_ST_DIS; |
| 513 | /* fall through */ |
| 514 | default: |
| 515 | cs->flags &= ~CS_FL_NOLINGER; |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 516 | cs_rx_shut_blk(cs); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 517 | ic->flags |= CF_SHUTR; |
| 518 | ic->rex = TICK_ETERNITY; |
| 519 | __cs_strm(cs)->conn_exp = TICK_ETERNITY; |
| 520 | } |
| 521 | |
| 522 | /* note that if the task exists, it must unregister itself once it runs */ |
| 523 | if (!(cs->flags & CS_FL_DONT_WAKE)) |
| 524 | task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO); |
| 525 | } |
| 526 | |
| 527 | /* default chk_rcv function for scheduled tasks */ |
| 528 | static void cs_app_chk_rcv(struct conn_stream *cs) |
| 529 | { |
| 530 | struct channel *ic = cs_ic(cs); |
| 531 | |
| 532 | DPRINTF(stderr, "%s: cs=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n", |
| 533 | __FUNCTION__, |
| 534 | cs, cs->state, ic->flags, cs_oc(cs)->flags); |
| 535 | |
| 536 | if (ic->pipe) { |
| 537 | /* stop reading */ |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 538 | cs_rx_room_blk(cs); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 539 | } |
| 540 | else { |
| 541 | /* (re)start reading */ |
| 542 | if (!(cs->flags & CS_FL_DONT_WAKE)) |
| 543 | task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | /* default chk_snd function for scheduled tasks */ |
| 548 | static void cs_app_chk_snd(struct conn_stream *cs) |
| 549 | { |
| 550 | struct channel *oc = cs_oc(cs); |
| 551 | |
| 552 | DPRINTF(stderr, "%s: cs=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n", |
| 553 | __FUNCTION__, |
| 554 | cs, cs->state, cs_ic(cs)->flags, oc->flags); |
| 555 | |
| 556 | if (unlikely(cs->state != CS_ST_EST || (oc->flags & CF_SHUTW))) |
| 557 | return; |
| 558 | |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 559 | if (!(cs->endp->flags & CS_EP_WAIT_DATA) || /* not waiting for data */ |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 560 | channel_is_empty(oc)) /* called with nothing to send ! */ |
| 561 | return; |
| 562 | |
| 563 | /* Otherwise there are remaining data to be sent in the buffer, |
| 564 | * so we tell the handler. |
| 565 | */ |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 566 | cs->endp->flags &= ~CS_EP_WAIT_DATA; |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 567 | if (!tick_isset(oc->wex)) |
| 568 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
| 569 | |
| 570 | if (!(cs->flags & CS_FL_DONT_WAKE)) |
| 571 | task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO); |
| 572 | } |
| 573 | |
| 574 | /* |
| 575 | * This function performs a shutdown-read on a conn-stream attached to |
| 576 | * a connection in a connected or init state (it does nothing for other |
| 577 | * states). It either shuts the read side or marks itself as closed. The buffer |
Christopher Faulet | 6b0a0fb | 2022-04-04 11:29:28 +0200 | [diff] [blame] | 578 | * flags are updated to reflect the new state. If the conn-stream has |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 579 | * CS_FL_NOHALF, we also forward the close to the write side. If a control |
| 580 | * layer is defined, then it is supposed to be a socket layer and file |
| 581 | * descriptors are then shutdown or closed accordingly. The function |
| 582 | * automatically disables polling if needed. |
| 583 | */ |
| 584 | static void cs_app_shutr_conn(struct conn_stream *cs) |
| 585 | { |
| 586 | struct channel *ic = cs_ic(cs); |
| 587 | |
| 588 | BUG_ON(!cs_conn(cs)); |
| 589 | |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 590 | cs_rx_shut_blk(cs); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 591 | if (ic->flags & CF_SHUTR) |
| 592 | return; |
| 593 | ic->flags |= CF_SHUTR; |
| 594 | ic->rex = TICK_ETERNITY; |
| 595 | |
| 596 | if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST)) |
| 597 | return; |
| 598 | |
| 599 | if (cs_oc(cs)->flags & CF_SHUTW) { |
| 600 | cs_conn_close(cs); |
| 601 | cs->state = CS_ST_DIS; |
| 602 | __cs_strm(cs)->conn_exp = TICK_ETERNITY; |
| 603 | } |
| 604 | else if (cs->flags & CS_FL_NOHALF) { |
| 605 | /* we want to immediately forward this close to the write side */ |
| 606 | return cs_app_shutw_conn(cs); |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | /* |
| 611 | * This function performs a shutdown-write on a conn-stream attached to |
| 612 | * a connection in a connected or init state (it does nothing for other |
| 613 | * states). It either shuts the write side or marks itself as closed. The |
| 614 | * buffer flags are updated to reflect the new state. It does also close |
Christopher Faulet | 6b0a0fb | 2022-04-04 11:29:28 +0200 | [diff] [blame] | 615 | * everything if the CS was marked as being in error state. If there is a |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 616 | * data-layer shutdown, it is called. |
| 617 | */ |
| 618 | static void cs_app_shutw_conn(struct conn_stream *cs) |
| 619 | { |
| 620 | struct channel *ic = cs_ic(cs); |
| 621 | struct channel *oc = cs_oc(cs); |
| 622 | |
| 623 | BUG_ON(!cs_conn(cs)); |
| 624 | |
| 625 | oc->flags &= ~CF_SHUTW_NOW; |
| 626 | if (oc->flags & CF_SHUTW) |
| 627 | return; |
| 628 | oc->flags |= CF_SHUTW; |
| 629 | oc->wex = TICK_ETERNITY; |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 630 | cs_done_get(cs); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 631 | |
| 632 | if (tick_isset(cs->hcto)) { |
| 633 | ic->rto = cs->hcto; |
| 634 | ic->rex = tick_add(now_ms, ic->rto); |
| 635 | } |
| 636 | |
| 637 | switch (cs->state) { |
| 638 | case CS_ST_RDY: |
| 639 | case CS_ST_EST: |
| 640 | /* we have to shut before closing, otherwise some short messages |
| 641 | * may never leave the system, especially when there are remaining |
| 642 | * unread data in the socket input buffer, or when nolinger is set. |
| 643 | * However, if CS_FL_NOLINGER is explicitly set, we know there is |
| 644 | * no risk so we close both sides immediately. |
| 645 | */ |
| 646 | |
| 647 | if (cs->endp->flags & CS_EP_ERROR) { |
| 648 | /* quick close, the socket is already shut anyway */ |
| 649 | } |
| 650 | else if (cs->flags & CS_FL_NOLINGER) { |
| 651 | /* unclean data-layer shutdown, typically an aborted request |
| 652 | * or a forwarded shutdown from a client to a server due to |
| 653 | * option abortonclose. No need for the TLS layer to try to |
| 654 | * emit a shutdown message. |
| 655 | */ |
| 656 | cs_conn_shutw(cs, CO_SHW_SILENT); |
| 657 | } |
| 658 | else { |
| 659 | /* clean data-layer shutdown. This only happens on the |
| 660 | * frontend side, or on the backend side when forwarding |
| 661 | * a client close in TCP mode or in HTTP TUNNEL mode |
| 662 | * while option abortonclose is set. We want the TLS |
| 663 | * layer to try to signal it to the peer before we close. |
| 664 | */ |
| 665 | cs_conn_shutw(cs, CO_SHW_NORMAL); |
| 666 | |
| 667 | if (!(ic->flags & (CF_SHUTR|CF_DONT_READ))) |
| 668 | return; |
| 669 | } |
| 670 | |
| 671 | /* fall through */ |
| 672 | case CS_ST_CON: |
| 673 | /* we may have to close a pending connection, and mark the |
| 674 | * response buffer as shutr |
| 675 | */ |
| 676 | cs_conn_close(cs); |
| 677 | /* fall through */ |
| 678 | case CS_ST_CER: |
| 679 | case CS_ST_QUE: |
| 680 | case CS_ST_TAR: |
| 681 | cs->state = CS_ST_DIS; |
| 682 | /* fall through */ |
| 683 | default: |
| 684 | cs->flags &= ~CS_FL_NOLINGER; |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 685 | cs_rx_shut_blk(cs); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 686 | ic->flags |= CF_SHUTR; |
| 687 | ic->rex = TICK_ETERNITY; |
| 688 | __cs_strm(cs)->conn_exp = TICK_ETERNITY; |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | /* This function is used for inter-conn-stream calls. It is called by the |
| 693 | * consumer to inform the producer side that it may be interested in checking |
| 694 | * for free space in the buffer. Note that it intentionally does not update |
| 695 | * timeouts, so that we can still check them later at wake-up. This function is |
Christopher Faulet | 6b0a0fb | 2022-04-04 11:29:28 +0200 | [diff] [blame] | 696 | * dedicated to connection-based conn-streams. |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 697 | */ |
| 698 | static void cs_app_chk_rcv_conn(struct conn_stream *cs) |
| 699 | { |
| 700 | BUG_ON(!cs_conn(cs)); |
| 701 | |
| 702 | /* (re)start reading */ |
| 703 | if (cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST)) |
| 704 | tasklet_wakeup(cs->wait_event.tasklet); |
| 705 | } |
| 706 | |
| 707 | |
| 708 | /* This function is used for inter-conn-stream calls. It is called by the |
| 709 | * producer to inform the consumer side that it may be interested in checking |
| 710 | * for data in the buffer. Note that it intentionally does not update timeouts, |
| 711 | * so that we can still check them later at wake-up. |
| 712 | */ |
| 713 | static void cs_app_chk_snd_conn(struct conn_stream *cs) |
| 714 | { |
| 715 | struct channel *oc = cs_oc(cs); |
| 716 | |
| 717 | BUG_ON(!cs_conn(cs)); |
| 718 | |
| 719 | if (unlikely(!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST) || |
| 720 | (oc->flags & CF_SHUTW))) |
| 721 | return; |
| 722 | |
| 723 | if (unlikely(channel_is_empty(oc))) /* called with nothing to send ! */ |
| 724 | return; |
| 725 | |
| 726 | if (!oc->pipe && /* spliced data wants to be forwarded ASAP */ |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 727 | !(cs->endp->flags & CS_EP_WAIT_DATA)) /* not waiting for data */ |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 728 | return; |
| 729 | |
| 730 | if (!(cs->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(cs_oc(cs))) |
Christopher Faulet | 000ba3e | 2022-04-01 17:06:32 +0200 | [diff] [blame] | 731 | cs_conn_send(cs); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 732 | |
Christopher Faulet | 158f336 | 2022-04-01 17:15:10 +0200 | [diff] [blame] | 733 | if (cs->endp->flags & (CS_EP_ERROR|CS_EP_ERR_PENDING) || cs_is_conn_error(cs)) { |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 734 | /* Write error on the file descriptor */ |
| 735 | if (cs->state >= CS_ST_CON) |
| 736 | cs->endp->flags |= CS_EP_ERROR; |
| 737 | goto out_wakeup; |
| 738 | } |
| 739 | |
| 740 | /* OK, so now we know that some data might have been sent, and that we may |
| 741 | * have to poll first. We have to do that too if the buffer is not empty. |
| 742 | */ |
| 743 | if (channel_is_empty(oc)) { |
| 744 | /* the connection is established but we can't write. Either the |
| 745 | * buffer is empty, or we just refrain from sending because the |
| 746 | * ->o limit was reached. Maybe we just wrote the last |
| 747 | * chunk and need to close. |
| 748 | */ |
| 749 | if (((oc->flags & (CF_SHUTW|CF_AUTO_CLOSE|CF_SHUTW_NOW)) == |
| 750 | (CF_AUTO_CLOSE|CF_SHUTW_NOW)) && |
| 751 | cs_state_in(cs->state, CS_SB_RDY|CS_SB_EST)) { |
| 752 | cs_shutw(cs); |
| 753 | goto out_wakeup; |
| 754 | } |
| 755 | |
| 756 | if ((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == 0) |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 757 | cs->endp->flags |= CS_EP_WAIT_DATA; |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 758 | oc->wex = TICK_ETERNITY; |
| 759 | } |
| 760 | else { |
| 761 | /* Otherwise there are remaining data to be sent in the buffer, |
| 762 | * which means we have to poll before doing so. |
| 763 | */ |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 764 | cs->endp->flags &= ~CS_EP_WAIT_DATA; |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 765 | if (!tick_isset(oc->wex)) |
| 766 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
| 767 | } |
| 768 | |
| 769 | if (likely(oc->flags & CF_WRITE_ACTIVITY)) { |
| 770 | struct channel *ic = cs_ic(cs); |
| 771 | |
| 772 | /* update timeout if we have written something */ |
| 773 | if ((oc->flags & (CF_SHUTW|CF_WRITE_PARTIAL)) == CF_WRITE_PARTIAL && |
| 774 | !channel_is_empty(oc)) |
| 775 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
| 776 | |
| 777 | if (tick_isset(ic->rex) && !(cs->flags & CS_FL_INDEP_STR)) { |
| 778 | /* Note: to prevent the client from expiring read timeouts |
| 779 | * during writes, we refresh it. We only do this if the |
| 780 | * interface is not configured for "independent streams", |
| 781 | * because for some applications it's better not to do this, |
| 782 | * for instance when continuously exchanging small amounts |
| 783 | * of data which can full the socket buffers long before a |
| 784 | * write timeout is detected. |
| 785 | */ |
| 786 | ic->rex = tick_add_ifset(now_ms, ic->rto); |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | /* in case of special condition (error, shutdown, end of write...), we |
| 791 | * have to notify the task. |
| 792 | */ |
| 793 | if (likely((oc->flags & (CF_WRITE_NULL|CF_WRITE_ERROR|CF_SHUTW)) || |
| 794 | ((oc->flags & CF_WAKE_WRITE) && |
| 795 | ((channel_is_empty(oc) && !oc->to_forward) || |
| 796 | !cs_state_in(cs->state, CS_SB_EST))))) { |
| 797 | out_wakeup: |
| 798 | if (!(cs->flags & CS_FL_DONT_WAKE)) |
| 799 | task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO); |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | /* |
| 804 | * This function performs a shutdown-read on a conn-stream attached to an |
| 805 | * applet in a connected or init state (it does nothing for other states). It |
| 806 | * either shuts the read side or marks itself as closed. The buffer flags are |
Christopher Faulet | 6b0a0fb | 2022-04-04 11:29:28 +0200 | [diff] [blame] | 807 | * updated to reflect the new state. If the conn-stream has CS_FL_NOHALF, |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 808 | * we also forward the close to the write side. The owner task is woken up if |
| 809 | * it exists. |
| 810 | */ |
| 811 | static void cs_app_shutr_applet(struct conn_stream *cs) |
| 812 | { |
| 813 | struct channel *ic = cs_ic(cs); |
| 814 | |
| 815 | BUG_ON(!cs_appctx(cs)); |
| 816 | |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 817 | cs_rx_shut_blk(cs); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 818 | if (ic->flags & CF_SHUTR) |
| 819 | return; |
| 820 | ic->flags |= CF_SHUTR; |
| 821 | ic->rex = TICK_ETERNITY; |
| 822 | |
| 823 | /* Note: on shutr, we don't call the applet */ |
| 824 | |
| 825 | if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST)) |
| 826 | return; |
| 827 | |
| 828 | if (cs_oc(cs)->flags & CF_SHUTW) { |
| 829 | cs_applet_release(cs); |
| 830 | cs->state = CS_ST_DIS; |
| 831 | __cs_strm(cs)->conn_exp = TICK_ETERNITY; |
| 832 | } |
| 833 | else if (cs->flags & CS_FL_NOHALF) { |
| 834 | /* we want to immediately forward this close to the write side */ |
| 835 | return cs_app_shutw_applet(cs); |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | /* |
| 840 | * This function performs a shutdown-write on a conn-stream attached to an |
| 841 | * applet in a connected or init state (it does nothing for other states). It |
| 842 | * either shuts the write side or marks itself as closed. The buffer flags are |
| 843 | * updated to reflect the new state. It does also close everything if the SI |
| 844 | * was marked as being in error state. The owner task is woken up if it exists. |
| 845 | */ |
| 846 | static void cs_app_shutw_applet(struct conn_stream *cs) |
| 847 | { |
| 848 | struct channel *ic = cs_ic(cs); |
| 849 | struct channel *oc = cs_oc(cs); |
| 850 | |
| 851 | BUG_ON(!cs_appctx(cs)); |
| 852 | |
| 853 | oc->flags &= ~CF_SHUTW_NOW; |
| 854 | if (oc->flags & CF_SHUTW) |
| 855 | return; |
| 856 | oc->flags |= CF_SHUTW; |
| 857 | oc->wex = TICK_ETERNITY; |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 858 | cs_done_get(cs); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 859 | |
| 860 | if (tick_isset(cs->hcto)) { |
| 861 | ic->rto = cs->hcto; |
| 862 | ic->rex = tick_add(now_ms, ic->rto); |
| 863 | } |
| 864 | |
| 865 | /* on shutw we always wake the applet up */ |
| 866 | appctx_wakeup(__cs_appctx(cs)); |
| 867 | |
| 868 | switch (cs->state) { |
| 869 | case CS_ST_RDY: |
| 870 | case CS_ST_EST: |
| 871 | /* we have to shut before closing, otherwise some short messages |
| 872 | * may never leave the system, especially when there are remaining |
| 873 | * unread data in the socket input buffer, or when nolinger is set. |
| 874 | * However, if CS_FL_NOLINGER is explicitly set, we know there is |
| 875 | * no risk so we close both sides immediately. |
| 876 | */ |
| 877 | if (!(cs->endp->flags & CS_EP_ERROR) && !(cs->flags & CS_FL_NOLINGER) && |
| 878 | !(ic->flags & (CF_SHUTR|CF_DONT_READ))) |
| 879 | return; |
| 880 | |
| 881 | /* fall through */ |
| 882 | case CS_ST_CON: |
| 883 | case CS_ST_CER: |
| 884 | case CS_ST_QUE: |
| 885 | case CS_ST_TAR: |
| 886 | /* Note that none of these states may happen with applets */ |
| 887 | cs_applet_release(cs); |
| 888 | cs->state = CS_ST_DIS; |
| 889 | /* fall through */ |
| 890 | default: |
| 891 | cs->flags &= ~CS_FL_NOLINGER; |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 892 | cs_rx_shut_blk(cs); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 893 | ic->flags |= CF_SHUTR; |
| 894 | ic->rex = TICK_ETERNITY; |
| 895 | __cs_strm(cs)->conn_exp = TICK_ETERNITY; |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | /* chk_rcv function for applets */ |
| 900 | static void cs_app_chk_rcv_applet(struct conn_stream *cs) |
| 901 | { |
| 902 | struct channel *ic = cs_ic(cs); |
| 903 | |
| 904 | BUG_ON(!cs_appctx(cs)); |
| 905 | |
| 906 | DPRINTF(stderr, "%s: cs=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n", |
| 907 | __FUNCTION__, |
| 908 | cs, cs->state, ic->flags, cs_oc(cs)->flags); |
| 909 | |
| 910 | if (!ic->pipe) { |
| 911 | /* (re)start reading */ |
| 912 | appctx_wakeup(__cs_appctx(cs)); |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | /* chk_snd function for applets */ |
| 917 | static void cs_app_chk_snd_applet(struct conn_stream *cs) |
| 918 | { |
| 919 | struct channel *oc = cs_oc(cs); |
| 920 | |
| 921 | BUG_ON(!cs_appctx(cs)); |
| 922 | |
| 923 | DPRINTF(stderr, "%s: cs=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n", |
| 924 | __FUNCTION__, |
| 925 | cs, cs->state, cs_ic(cs)->flags, oc->flags); |
| 926 | |
| 927 | if (unlikely(cs->state != CS_ST_EST || (oc->flags & CF_SHUTW))) |
| 928 | return; |
| 929 | |
| 930 | /* we only wake the applet up if it was waiting for some data */ |
| 931 | |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 932 | if (!(cs->endp->flags & CS_EP_WAIT_DATA)) |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 933 | return; |
| 934 | |
| 935 | if (!tick_isset(oc->wex)) |
| 936 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
| 937 | |
| 938 | if (!channel_is_empty(oc)) { |
| 939 | /* (re)start sending */ |
| 940 | appctx_wakeup(__cs_appctx(cs)); |
| 941 | } |
| 942 | } |
Christopher Faulet | 13045f0 | 2022-04-01 14:23:38 +0200 | [diff] [blame] | 943 | |
| 944 | |
| 945 | /* This function is designed to be called from within the stream handler to |
| 946 | * update the input channel's expiration timer and the conn-stream's |
| 947 | * Rx flags based on the channel's flags. It needs to be called only once |
| 948 | * after the channel's flags have settled down, and before they are cleared, |
| 949 | * though it doesn't harm to call it as often as desired (it just slightly |
| 950 | * hurts performance). It must not be called from outside of the stream |
| 951 | * handler, as what it does will be used to compute the stream task's |
| 952 | * expiration. |
| 953 | */ |
| 954 | void cs_update_rx(struct conn_stream *cs) |
| 955 | { |
| 956 | struct channel *ic = cs_ic(cs); |
| 957 | |
| 958 | if (ic->flags & CF_SHUTR) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 959 | cs_rx_shut_blk(cs); |
Christopher Faulet | 13045f0 | 2022-04-01 14:23:38 +0200 | [diff] [blame] | 960 | return; |
| 961 | } |
| 962 | |
| 963 | /* Read not closed, update FD status and timeout for reads */ |
| 964 | if (ic->flags & CF_DONT_READ) |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 965 | cs_rx_chan_blk(cs); |
Christopher Faulet | 13045f0 | 2022-04-01 14:23:38 +0200 | [diff] [blame] | 966 | else |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 967 | cs_rx_chan_rdy(cs); |
Christopher Faulet | 13045f0 | 2022-04-01 14:23:38 +0200 | [diff] [blame] | 968 | |
| 969 | if (!channel_is_empty(ic) || !channel_may_recv(ic)) { |
| 970 | /* stop reading, imposed by channel's policy or contents */ |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 971 | cs_rx_room_blk(cs); |
Christopher Faulet | 13045f0 | 2022-04-01 14:23:38 +0200 | [diff] [blame] | 972 | } |
| 973 | else { |
| 974 | /* (re)start reading and update timeout. Note: we don't recompute the timeout |
| 975 | * every time we get here, otherwise it would risk never to expire. We only |
| 976 | * update it if is was not yet set. The stream socket handler will already |
| 977 | * have updated it if there has been a completed I/O. |
| 978 | */ |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 979 | cs_rx_room_rdy(cs); |
Christopher Faulet | 13045f0 | 2022-04-01 14:23:38 +0200 | [diff] [blame] | 980 | } |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 981 | if (cs->endp->flags & CS_EP_RXBLK_ANY & ~CS_EP_RX_WAIT_EP) |
Christopher Faulet | 13045f0 | 2022-04-01 14:23:38 +0200 | [diff] [blame] | 982 | ic->rex = TICK_ETERNITY; |
| 983 | else if (!(ic->flags & CF_READ_NOEXP) && !tick_isset(ic->rex)) |
| 984 | ic->rex = tick_add_ifset(now_ms, ic->rto); |
| 985 | |
| 986 | cs_chk_rcv(cs); |
| 987 | } |
| 988 | |
| 989 | /* This function is designed to be called from within the stream handler to |
| 990 | * update the output channel's expiration timer and the conn-stream's |
| 991 | * Tx flags based on the channel's flags. It needs to be called only once |
| 992 | * after the channel's flags have settled down, and before they are cleared, |
| 993 | * though it doesn't harm to call it as often as desired (it just slightly |
| 994 | * hurts performance). It must not be called from outside of the stream |
| 995 | * handler, as what it does will be used to compute the stream task's |
| 996 | * expiration. |
| 997 | */ |
| 998 | void cs_update_tx(struct conn_stream *cs) |
| 999 | { |
| 1000 | struct channel *oc = cs_oc(cs); |
| 1001 | struct channel *ic = cs_ic(cs); |
| 1002 | |
| 1003 | if (oc->flags & CF_SHUTW) |
| 1004 | return; |
| 1005 | |
| 1006 | /* Write not closed, update FD status and timeout for writes */ |
| 1007 | if (channel_is_empty(oc)) { |
| 1008 | /* stop writing */ |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 1009 | if (!(cs->endp->flags & CS_EP_WAIT_DATA)) { |
Christopher Faulet | 13045f0 | 2022-04-01 14:23:38 +0200 | [diff] [blame] | 1010 | if ((oc->flags & CF_SHUTW_NOW) == 0) |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 1011 | cs->endp->flags |= CS_EP_WAIT_DATA; |
Christopher Faulet | 13045f0 | 2022-04-01 14:23:38 +0200 | [diff] [blame] | 1012 | oc->wex = TICK_ETERNITY; |
| 1013 | } |
| 1014 | return; |
| 1015 | } |
| 1016 | |
| 1017 | /* (re)start writing and update timeout. Note: we don't recompute the timeout |
| 1018 | * every time we get here, otherwise it would risk never to expire. We only |
| 1019 | * update it if is was not yet set. The stream socket handler will already |
| 1020 | * have updated it if there has been a completed I/O. |
| 1021 | */ |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 1022 | cs->endp->flags &= ~CS_EP_WAIT_DATA; |
Christopher Faulet | 13045f0 | 2022-04-01 14:23:38 +0200 | [diff] [blame] | 1023 | if (!tick_isset(oc->wex)) { |
| 1024 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
| 1025 | if (tick_isset(ic->rex) && !(cs->flags & CS_FL_INDEP_STR)) { |
| 1026 | /* Note: depending on the protocol, we don't know if we're waiting |
| 1027 | * for incoming data or not. So in order to prevent the socket from |
| 1028 | * expiring read timeouts during writes, we refresh the read timeout, |
| 1029 | * except if it was already infinite or if we have explicitly setup |
| 1030 | * independent streams. |
| 1031 | */ |
| 1032 | ic->rex = tick_add_ifset(now_ms, ic->rto); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1033 | } |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | /* This function is the equivalent to cs_update() except that it's |
| 1038 | * designed to be called from outside the stream handlers, typically the lower |
| 1039 | * layers (applets, connections) after I/O completion. After updating the stream |
| 1040 | * interface and timeouts, it will try to forward what can be forwarded, then to |
| 1041 | * wake the associated task up if an important event requires special handling. |
| 1042 | * It may update CS_EP_WAIT_DATA and/or CS_EP_RXBLK_ROOM, that the callers are |
| 1043 | * encouraged to watch to take appropriate action. |
| 1044 | * It should not be called from within the stream itself, cs_update() |
| 1045 | * is designed for this. |
| 1046 | */ |
| 1047 | static void cs_notify(struct conn_stream *cs) |
| 1048 | { |
| 1049 | struct channel *ic = cs_ic(cs); |
| 1050 | struct channel *oc = cs_oc(cs); |
| 1051 | struct conn_stream *cso = cs_opposite(cs); |
| 1052 | struct task *task = cs_strm_task(cs); |
| 1053 | |
| 1054 | /* process consumer side */ |
| 1055 | if (channel_is_empty(oc)) { |
| 1056 | struct connection *conn = cs_conn(cs); |
| 1057 | |
| 1058 | if (((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW) && |
| 1059 | (cs->state == CS_ST_EST) && (!conn || !(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS)))) |
| 1060 | cs_shutw(cs); |
| 1061 | oc->wex = TICK_ETERNITY; |
| 1062 | } |
| 1063 | |
| 1064 | /* indicate that we may be waiting for data from the output channel or |
| 1065 | * we're about to close and can't expect more data if SHUTW_NOW is there. |
| 1066 | */ |
| 1067 | if (!(oc->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 1068 | cs->endp->flags |= CS_EP_WAIT_DATA; |
| 1069 | else if ((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW) |
| 1070 | cs->endp->flags &= ~CS_EP_WAIT_DATA; |
| 1071 | |
| 1072 | /* update OC timeouts and wake the other side up if it's waiting for room */ |
| 1073 | if (oc->flags & CF_WRITE_ACTIVITY) { |
| 1074 | if ((oc->flags & (CF_SHUTW|CF_WRITE_PARTIAL)) == CF_WRITE_PARTIAL && |
| 1075 | !channel_is_empty(oc)) |
| 1076 | if (tick_isset(oc->wex)) |
| 1077 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
| 1078 | |
| 1079 | if (!(cs->flags & CS_FL_INDEP_STR)) |
| 1080 | if (tick_isset(ic->rex)) |
| 1081 | ic->rex = tick_add_ifset(now_ms, ic->rto); |
| 1082 | } |
| 1083 | |
| 1084 | if (oc->flags & CF_DONT_READ) |
| 1085 | cs_rx_chan_blk(cso); |
| 1086 | else |
| 1087 | cs_rx_chan_rdy(cso); |
| 1088 | |
| 1089 | /* Notify the other side when we've injected data into the IC that |
| 1090 | * needs to be forwarded. We can do fast-forwarding as soon as there |
| 1091 | * are output data, but we avoid doing this if some of the data are |
| 1092 | * not yet scheduled for being forwarded, because it is very likely |
| 1093 | * that it will be done again immediately afterwards once the following |
| 1094 | * data are parsed (eg: HTTP chunking). We only CS_EP_RXBLK_ROOM once |
| 1095 | * we've emptied *some* of the output buffer, and not just when there |
| 1096 | * is available room, because applets are often forced to stop before |
| 1097 | * the buffer is full. We must not stop based on input data alone because |
| 1098 | * an HTTP parser might need more data to complete the parsing. |
| 1099 | */ |
| 1100 | if (!channel_is_empty(ic) && |
| 1101 | (cso->endp->flags & CS_EP_WAIT_DATA) && |
| 1102 | (!(ic->flags & CF_EXPECT_MORE) || c_full(ic) || ci_data(ic) == 0 || ic->pipe)) { |
| 1103 | int new_len, last_len; |
| 1104 | |
| 1105 | last_len = co_data(ic); |
| 1106 | if (ic->pipe) |
| 1107 | last_len += ic->pipe->data; |
| 1108 | |
| 1109 | cs_chk_snd(cso); |
| 1110 | |
| 1111 | new_len = co_data(ic); |
| 1112 | if (ic->pipe) |
| 1113 | new_len += ic->pipe->data; |
| 1114 | |
| 1115 | /* check if the consumer has freed some space either in the |
| 1116 | * buffer or in the pipe. |
| 1117 | */ |
| 1118 | if (new_len < last_len) |
| 1119 | cs_rx_room_rdy(cs); |
| 1120 | } |
| 1121 | |
| 1122 | if (!(ic->flags & CF_DONT_READ)) |
| 1123 | cs_rx_chan_rdy(cs); |
| 1124 | |
| 1125 | cs_chk_rcv(cs); |
| 1126 | cs_chk_rcv(cso); |
| 1127 | |
| 1128 | if (cs_rx_blocked(cs)) { |
| 1129 | ic->rex = TICK_ETERNITY; |
| 1130 | } |
| 1131 | else if ((ic->flags & (CF_SHUTR|CF_READ_PARTIAL)) == CF_READ_PARTIAL) { |
| 1132 | /* we must re-enable reading if cs_chk_snd() has freed some space */ |
| 1133 | if (!(ic->flags & CF_READ_NOEXP) && tick_isset(ic->rex)) |
| 1134 | ic->rex = tick_add_ifset(now_ms, ic->rto); |
| 1135 | } |
| 1136 | |
| 1137 | /* wake the task up only when needed */ |
| 1138 | if (/* changes on the production side */ |
| 1139 | (ic->flags & (CF_READ_NULL|CF_READ_ERROR)) || |
| 1140 | !cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST) || |
| 1141 | (cs->endp->flags & CS_EP_ERROR) || |
| 1142 | ((ic->flags & CF_READ_PARTIAL) && |
| 1143 | ((ic->flags & CF_EOI) || !ic->to_forward || cso->state != CS_ST_EST)) || |
| 1144 | |
| 1145 | /* changes on the consumption side */ |
| 1146 | (oc->flags & (CF_WRITE_NULL|CF_WRITE_ERROR)) || |
| 1147 | ((oc->flags & CF_WRITE_ACTIVITY) && |
| 1148 | ((oc->flags & CF_SHUTW) || |
| 1149 | (((oc->flags & CF_WAKE_WRITE) || |
| 1150 | !(oc->flags & (CF_AUTO_CLOSE|CF_SHUTW_NOW|CF_SHUTW))) && |
| 1151 | (cso->state != CS_ST_EST || |
| 1152 | (channel_is_empty(oc) && !oc->to_forward)))))) { |
| 1153 | task_wakeup(task, TASK_WOKEN_IO); |
| 1154 | } |
| 1155 | else { |
| 1156 | /* Update expiration date for the task and requeue it */ |
| 1157 | task->expire = tick_first((tick_is_expired(task->expire, now_ms) ? 0 : task->expire), |
| 1158 | tick_first(tick_first(ic->rex, ic->wex), |
| 1159 | tick_first(oc->rex, oc->wex))); |
| 1160 | |
| 1161 | task->expire = tick_first(task->expire, ic->analyse_exp); |
| 1162 | task->expire = tick_first(task->expire, oc->analyse_exp); |
| 1163 | task->expire = tick_first(task->expire, __cs_strm(cs)->conn_exp); |
| 1164 | |
| 1165 | task_queue(task); |
| 1166 | } |
| 1167 | if (ic->flags & CF_READ_ACTIVITY) |
| 1168 | ic->flags &= ~CF_READ_DONTWAIT; |
| 1169 | } |
| 1170 | |
| 1171 | /* |
| 1172 | * This function propagates a null read received on a socket-based connection. |
Christopher Faulet | 6b0a0fb | 2022-04-04 11:29:28 +0200 | [diff] [blame] | 1173 | * It updates the conn-stream. If the conn-stream has CS_FL_NOHALF, |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1174 | * the close is also forwarded to the write side as an abort. |
| 1175 | */ |
| 1176 | static void cs_conn_read0(struct conn_stream *cs) |
| 1177 | { |
| 1178 | struct channel *ic = cs_ic(cs); |
| 1179 | struct channel *oc = cs_oc(cs); |
| 1180 | |
| 1181 | BUG_ON(!cs_conn(cs)); |
| 1182 | |
| 1183 | cs_rx_shut_blk(cs); |
| 1184 | if (ic->flags & CF_SHUTR) |
| 1185 | return; |
| 1186 | ic->flags |= CF_SHUTR; |
| 1187 | ic->rex = TICK_ETERNITY; |
| 1188 | |
| 1189 | if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST)) |
| 1190 | return; |
| 1191 | |
| 1192 | if (oc->flags & CF_SHUTW) |
| 1193 | goto do_close; |
| 1194 | |
| 1195 | if (cs->flags & CS_FL_NOHALF) { |
| 1196 | /* we want to immediately forward this close to the write side */ |
| 1197 | /* force flag on ssl to keep stream in cache */ |
| 1198 | cs_conn_shutw(cs, CO_SHW_SILENT); |
| 1199 | goto do_close; |
| 1200 | } |
| 1201 | |
| 1202 | /* otherwise that's just a normal read shutdown */ |
| 1203 | return; |
| 1204 | |
| 1205 | do_close: |
| 1206 | /* OK we completely close the socket here just as if we went through cs_shut[rw]() */ |
| 1207 | cs_conn_close(cs); |
| 1208 | |
| 1209 | oc->flags &= ~CF_SHUTW_NOW; |
| 1210 | oc->flags |= CF_SHUTW; |
| 1211 | oc->wex = TICK_ETERNITY; |
| 1212 | |
| 1213 | cs_done_get(cs); |
| 1214 | |
| 1215 | cs->state = CS_ST_DIS; |
| 1216 | __cs_strm(cs)->conn_exp = TICK_ETERNITY; |
| 1217 | return; |
| 1218 | } |
| 1219 | |
| 1220 | /* |
| 1221 | * This is the callback which is called by the connection layer to receive data |
| 1222 | * into the buffer from the connection. It iterates over the mux layer's |
| 1223 | * rcv_buf function. |
| 1224 | */ |
| 1225 | static int cs_conn_recv(struct conn_stream *cs) |
| 1226 | { |
| 1227 | struct connection *conn = __cs_conn(cs); |
| 1228 | struct channel *ic = cs_ic(cs); |
| 1229 | int ret, max, cur_read = 0; |
| 1230 | int read_poll = MAX_READ_POLL_LOOPS; |
| 1231 | int flags = 0; |
| 1232 | |
| 1233 | /* If not established yet, do nothing. */ |
| 1234 | if (cs->state != CS_ST_EST) |
| 1235 | return 0; |
| 1236 | |
| 1237 | /* If another call to cs_conn_recv() failed, and we subscribed to |
| 1238 | * recv events already, give up now. |
| 1239 | */ |
| 1240 | if (cs->wait_event.events & SUB_RETRY_RECV) |
| 1241 | return 0; |
| 1242 | |
| 1243 | /* maybe we were called immediately after an asynchronous shutr */ |
| 1244 | if (ic->flags & CF_SHUTR) |
| 1245 | return 1; |
| 1246 | |
| 1247 | /* we must wait because the mux is not installed yet */ |
| 1248 | if (!conn->mux) |
| 1249 | return 0; |
| 1250 | |
| 1251 | /* stop here if we reached the end of data */ |
| 1252 | if (cs->endp->flags & CS_EP_EOS) |
| 1253 | goto end_recv; |
| 1254 | |
| 1255 | /* stop immediately on errors. Note that we DON'T want to stop on |
| 1256 | * POLL_ERR, as the poller might report a write error while there |
| 1257 | * are still data available in the recv buffer. This typically |
| 1258 | * happens when we send too large a request to a backend server |
| 1259 | * which rejects it before reading it all. |
| 1260 | */ |
| 1261 | if (!(cs->endp->flags & CS_EP_RCV_MORE)) { |
| 1262 | if (!conn_xprt_ready(conn)) |
| 1263 | return 0; |
| 1264 | if (cs->endp->flags & CS_EP_ERROR) |
| 1265 | goto end_recv; |
| 1266 | } |
| 1267 | |
| 1268 | /* prepare to detect if the mux needs more room */ |
| 1269 | cs->endp->flags &= ~CS_EP_WANT_ROOM; |
| 1270 | |
| 1271 | if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) && !co_data(ic) && |
| 1272 | global.tune.idle_timer && |
| 1273 | (unsigned short)(now_ms - ic->last_read) >= global.tune.idle_timer) { |
| 1274 | /* The buffer was empty and nothing was transferred for more |
| 1275 | * than one second. This was caused by a pause and not by |
| 1276 | * congestion. Reset any streaming mode to reduce latency. |
| 1277 | */ |
| 1278 | ic->xfer_small = 0; |
| 1279 | ic->xfer_large = 0; |
| 1280 | ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST); |
| 1281 | } |
| 1282 | |
| 1283 | /* First, let's see if we may splice data across the channel without |
| 1284 | * using a buffer. |
| 1285 | */ |
| 1286 | if (cs->endp->flags & CS_EP_MAY_SPLICE && |
| 1287 | (ic->pipe || ic->to_forward >= MIN_SPLICE_FORWARD) && |
| 1288 | ic->flags & CF_KERN_SPLICING) { |
| 1289 | if (c_data(ic)) { |
| 1290 | /* We're embarrassed, there are already data pending in |
| 1291 | * the buffer and we don't want to have them at two |
| 1292 | * locations at a time. Let's indicate we need some |
| 1293 | * place and ask the consumer to hurry. |
| 1294 | */ |
| 1295 | flags |= CO_RFL_BUF_FLUSH; |
| 1296 | goto abort_splice; |
| 1297 | } |
| 1298 | |
| 1299 | if (unlikely(ic->pipe == NULL)) { |
| 1300 | if (pipes_used >= global.maxpipes || !(ic->pipe = get_pipe())) { |
| 1301 | ic->flags &= ~CF_KERN_SPLICING; |
| 1302 | goto abort_splice; |
| 1303 | } |
| 1304 | } |
| 1305 | |
| 1306 | ret = conn->mux->rcv_pipe(cs, ic->pipe, ic->to_forward); |
| 1307 | if (ret < 0) { |
| 1308 | /* splice not supported on this end, let's disable it */ |
| 1309 | ic->flags &= ~CF_KERN_SPLICING; |
| 1310 | goto abort_splice; |
| 1311 | } |
| 1312 | |
| 1313 | if (ret > 0) { |
| 1314 | if (ic->to_forward != CHN_INFINITE_FORWARD) |
| 1315 | ic->to_forward -= ret; |
| 1316 | ic->total += ret; |
| 1317 | cur_read += ret; |
| 1318 | ic->flags |= CF_READ_PARTIAL; |
| 1319 | } |
| 1320 | |
| 1321 | if (cs->endp->flags & (CS_EP_EOS|CS_EP_ERROR)) |
| 1322 | goto end_recv; |
| 1323 | |
| 1324 | if (conn->flags & CO_FL_WAIT_ROOM) { |
| 1325 | /* the pipe is full or we have read enough data that it |
| 1326 | * could soon be full. Let's stop before needing to poll. |
| 1327 | */ |
| 1328 | cs_rx_room_blk(cs); |
| 1329 | goto done_recv; |
| 1330 | } |
| 1331 | |
| 1332 | /* splice not possible (anymore), let's go on on standard copy */ |
| 1333 | } |
| 1334 | |
| 1335 | abort_splice: |
| 1336 | if (ic->pipe && unlikely(!ic->pipe->data)) { |
| 1337 | put_pipe(ic->pipe); |
| 1338 | ic->pipe = NULL; |
| 1339 | } |
| 1340 | |
| 1341 | if (ic->pipe && ic->to_forward && !(flags & CO_RFL_BUF_FLUSH) && cs->endp->flags & CS_EP_MAY_SPLICE) { |
| 1342 | /* don't break splicing by reading, but still call rcv_buf() |
| 1343 | * to pass the flag. |
| 1344 | */ |
| 1345 | goto done_recv; |
| 1346 | } |
| 1347 | |
| 1348 | /* now we'll need a input buffer for the stream */ |
| 1349 | if (!cs_alloc_ibuf(cs, &(__cs_strm(cs)->buffer_wait))) |
| 1350 | goto end_recv; |
| 1351 | |
| 1352 | /* For an HTX stream, if the buffer is stuck (no output data with some |
| 1353 | * input data) and if the HTX message is fragmented or if its free space |
| 1354 | * wraps, we force an HTX deframentation. It is a way to have a |
| 1355 | * contiguous free space nad to let the mux to copy as much data as |
| 1356 | * possible. |
| 1357 | * |
| 1358 | * NOTE: A possible optim may be to let the mux decides if defrag is |
| 1359 | * required or not, depending on amount of data to be xferred. |
| 1360 | */ |
| 1361 | if (IS_HTX_STRM(__cs_strm(cs)) && !co_data(ic)) { |
| 1362 | struct htx *htx = htxbuf(&ic->buf); |
| 1363 | |
| 1364 | if (htx_is_not_empty(htx) && ((htx->flags & HTX_FL_FRAGMENTED) || htx_space_wraps(htx))) |
| 1365 | htx_defrag(htx, NULL, 0); |
| 1366 | } |
| 1367 | |
| 1368 | /* Instruct the mux it must subscribed for read events */ |
| 1369 | flags |= ((!conn_is_back(conn) && (__cs_strm(cs)->be->options & PR_O_ABRT_CLOSE)) ? CO_RFL_KEEP_RECV : 0); |
| 1370 | |
| 1371 | /* Important note : if we're called with POLL_IN|POLL_HUP, it means the read polling |
| 1372 | * was enabled, which implies that the recv buffer was not full. So we have a guarantee |
| 1373 | * that if such an event is not handled above in splice, it will be handled here by |
| 1374 | * recv(). |
| 1375 | */ |
| 1376 | while ((cs->endp->flags & CS_EP_RCV_MORE) || |
| 1377 | (!(conn->flags & CO_FL_HANDSHAKE) && |
| 1378 | (!(cs->endp->flags & (CS_EP_ERROR|CS_EP_EOS))) && !(ic->flags & CF_SHUTR))) { |
| 1379 | int cur_flags = flags; |
| 1380 | |
| 1381 | /* Compute transient CO_RFL_* flags */ |
| 1382 | if (co_data(ic)) { |
| 1383 | cur_flags |= (CO_RFL_BUF_WET | CO_RFL_BUF_NOT_STUCK); |
| 1384 | } |
| 1385 | |
| 1386 | /* <max> may be null. This is the mux responsibility to set |
| 1387 | * CS_EP_RCV_MORE on the CS if more space is needed. |
| 1388 | */ |
| 1389 | max = channel_recv_max(ic); |
| 1390 | ret = conn->mux->rcv_buf(cs, &ic->buf, max, cur_flags); |
| 1391 | |
| 1392 | if (cs->endp->flags & CS_EP_WANT_ROOM) { |
| 1393 | /* CS_EP_WANT_ROOM must not be reported if the channel's |
| 1394 | * buffer is empty. |
| 1395 | */ |
| 1396 | BUG_ON(c_empty(ic)); |
| 1397 | |
| 1398 | cs_rx_room_blk(cs); |
| 1399 | /* Add READ_PARTIAL because some data are pending but |
| 1400 | * cannot be xferred to the channel |
| 1401 | */ |
| 1402 | ic->flags |= CF_READ_PARTIAL; |
| 1403 | } |
| 1404 | |
| 1405 | if (ret <= 0) { |
| 1406 | /* if we refrained from reading because we asked for a |
| 1407 | * flush to satisfy rcv_pipe(), we must not subscribe |
| 1408 | * and instead report that there's not enough room |
| 1409 | * here to proceed. |
| 1410 | */ |
| 1411 | if (flags & CO_RFL_BUF_FLUSH) |
| 1412 | cs_rx_room_blk(cs); |
| 1413 | break; |
| 1414 | } |
| 1415 | |
| 1416 | cur_read += ret; |
| 1417 | |
| 1418 | /* if we're allowed to directly forward data, we must update ->o */ |
| 1419 | if (ic->to_forward && !(ic->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 1420 | unsigned long fwd = ret; |
| 1421 | if (ic->to_forward != CHN_INFINITE_FORWARD) { |
| 1422 | if (fwd > ic->to_forward) |
| 1423 | fwd = ic->to_forward; |
| 1424 | ic->to_forward -= fwd; |
| 1425 | } |
| 1426 | c_adv(ic, fwd); |
| 1427 | } |
| 1428 | |
| 1429 | ic->flags |= CF_READ_PARTIAL; |
| 1430 | ic->total += ret; |
| 1431 | |
| 1432 | /* End-of-input reached, we can leave. In this case, it is |
Christopher Faulet | 6b0a0fb | 2022-04-04 11:29:28 +0200 | [diff] [blame] | 1433 | * important to break the loop to not block the CS because of |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1434 | * the channel's policies.This way, we are still able to receive |
| 1435 | * shutdowns. |
| 1436 | */ |
| 1437 | if (cs->endp->flags & CS_EP_EOI) |
| 1438 | break; |
| 1439 | |
| 1440 | if ((ic->flags & CF_READ_DONTWAIT) || --read_poll <= 0) { |
| 1441 | /* we're stopped by the channel's policy */ |
| 1442 | cs_rx_chan_blk(cs); |
| 1443 | break; |
| 1444 | } |
| 1445 | |
| 1446 | /* if too many bytes were missing from last read, it means that |
| 1447 | * it's pointless trying to read again because the system does |
| 1448 | * not have them in buffers. |
| 1449 | */ |
| 1450 | if (ret < max) { |
| 1451 | /* if a streamer has read few data, it may be because we |
| 1452 | * have exhausted system buffers. It's not worth trying |
| 1453 | * again. |
| 1454 | */ |
| 1455 | if (ic->flags & CF_STREAMER) { |
| 1456 | /* we're stopped by the channel's policy */ |
| 1457 | cs_rx_chan_blk(cs); |
| 1458 | break; |
| 1459 | } |
| 1460 | |
| 1461 | /* if we read a large block smaller than what we requested, |
| 1462 | * it's almost certain we'll never get anything more. |
| 1463 | */ |
| 1464 | if (ret >= global.tune.recv_enough) { |
| 1465 | /* we're stopped by the channel's policy */ |
| 1466 | cs_rx_chan_blk(cs); |
| 1467 | break; |
| 1468 | } |
| 1469 | } |
| 1470 | |
| 1471 | /* if we are waiting for more space, don't try to read more data |
| 1472 | * right now. |
| 1473 | */ |
| 1474 | if (cs_rx_blocked(cs)) |
| 1475 | break; |
| 1476 | } /* while !flags */ |
| 1477 | |
| 1478 | done_recv: |
| 1479 | if (cur_read) { |
| 1480 | if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) && |
| 1481 | (cur_read <= ic->buf.size / 2)) { |
| 1482 | ic->xfer_large = 0; |
| 1483 | ic->xfer_small++; |
| 1484 | if (ic->xfer_small >= 3) { |
| 1485 | /* we have read less than half of the buffer in |
| 1486 | * one pass, and this happened at least 3 times. |
| 1487 | * This is definitely not a streamer. |
| 1488 | */ |
| 1489 | ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST); |
| 1490 | } |
| 1491 | else if (ic->xfer_small >= 2) { |
| 1492 | /* if the buffer has been at least half full twice, |
| 1493 | * we receive faster than we send, so at least it |
| 1494 | * is not a "fast streamer". |
| 1495 | */ |
| 1496 | ic->flags &= ~CF_STREAMER_FAST; |
| 1497 | } |
| 1498 | } |
| 1499 | else if (!(ic->flags & CF_STREAMER_FAST) && |
| 1500 | (cur_read >= ic->buf.size - global.tune.maxrewrite)) { |
| 1501 | /* we read a full buffer at once */ |
| 1502 | ic->xfer_small = 0; |
| 1503 | ic->xfer_large++; |
| 1504 | if (ic->xfer_large >= 3) { |
| 1505 | /* we call this buffer a fast streamer if it manages |
| 1506 | * to be filled in one call 3 consecutive times. |
| 1507 | */ |
| 1508 | ic->flags |= (CF_STREAMER | CF_STREAMER_FAST); |
| 1509 | } |
| 1510 | } |
| 1511 | else { |
| 1512 | ic->xfer_small = 0; |
| 1513 | ic->xfer_large = 0; |
| 1514 | } |
| 1515 | ic->last_read = now_ms; |
| 1516 | } |
| 1517 | |
| 1518 | end_recv: |
| 1519 | ret = (cur_read != 0); |
| 1520 | |
| 1521 | /* Report EOI on the channel if it was reached from the mux point of |
| 1522 | * view. */ |
| 1523 | if ((cs->endp->flags & CS_EP_EOI) && !(ic->flags & CF_EOI)) { |
| 1524 | ic->flags |= (CF_EOI|CF_READ_PARTIAL); |
| 1525 | ret = 1; |
| 1526 | } |
| 1527 | |
| 1528 | if (cs->endp->flags & CS_EP_ERROR) |
| 1529 | ret = 1; |
| 1530 | else if (cs->endp->flags & CS_EP_EOS) { |
| 1531 | /* we received a shutdown */ |
| 1532 | ic->flags |= CF_READ_NULL; |
| 1533 | if (ic->flags & CF_AUTO_CLOSE) |
| 1534 | channel_shutw_now(ic); |
| 1535 | cs_conn_read0(cs); |
| 1536 | ret = 1; |
| 1537 | } |
| 1538 | else if (!cs_rx_blocked(cs)) { |
| 1539 | /* Subscribe to receive events if we're blocking on I/O */ |
| 1540 | conn->mux->subscribe(cs, SUB_RETRY_RECV, &cs->wait_event); |
| 1541 | cs_rx_endp_done(cs); |
| 1542 | } else { |
| 1543 | cs_rx_endp_more(cs); |
| 1544 | ret = 1; |
| 1545 | } |
| 1546 | return ret; |
| 1547 | } |
| 1548 | |
Christopher Faulet | 6b0a0fb | 2022-04-04 11:29:28 +0200 | [diff] [blame] | 1549 | /* This tries to perform a synchronous receive on the conn-stream to |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1550 | * try to collect last arrived data. In practice it's only implemented on |
| 1551 | * conn_streams. Returns 0 if nothing was done, non-zero if new data or a |
| 1552 | * shutdown were collected. This may result on some delayed receive calls |
| 1553 | * to be programmed and performed later, though it doesn't provide any |
| 1554 | * such guarantee. |
| 1555 | */ |
| 1556 | int cs_conn_sync_recv(struct conn_stream *cs) |
| 1557 | { |
| 1558 | if (!cs_state_in(cs->state, CS_SB_RDY|CS_SB_EST)) |
| 1559 | return 0; |
| 1560 | |
| 1561 | if (!cs_conn_mux(cs)) |
| 1562 | return 0; // only conn_streams are supported |
| 1563 | |
| 1564 | if (cs->wait_event.events & SUB_RETRY_RECV) |
| 1565 | return 0; // already subscribed |
| 1566 | |
| 1567 | if (!cs_rx_endp_ready(cs) || cs_rx_blocked(cs)) |
| 1568 | return 0; // already failed |
| 1569 | |
| 1570 | return cs_conn_recv(cs); |
| 1571 | } |
| 1572 | |
| 1573 | /* |
| 1574 | * This function is called to send buffer data to a stream socket. |
| 1575 | * It calls the mux layer's snd_buf function. It relies on the |
| 1576 | * caller to commit polling changes. The caller should check conn->flags |
| 1577 | * for errors. |
| 1578 | */ |
| 1579 | static int cs_conn_send(struct conn_stream *cs) |
| 1580 | { |
| 1581 | struct connection *conn = __cs_conn(cs); |
| 1582 | struct stream *s = __cs_strm(cs); |
| 1583 | struct channel *oc = cs_oc(cs); |
| 1584 | int ret; |
| 1585 | int did_send = 0; |
| 1586 | |
| 1587 | if (cs->endp->flags & (CS_EP_ERROR|CS_EP_ERR_PENDING) || cs_is_conn_error(cs)) { |
| 1588 | /* We're probably there because the tasklet was woken up, |
| 1589 | * but process_stream() ran before, detected there were an |
Christopher Faulet | 6b0a0fb | 2022-04-04 11:29:28 +0200 | [diff] [blame] | 1590 | * error and put the CS back to CS_ST_TAR. There's still |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1591 | * CO_FL_ERROR on the connection but we don't want to add |
| 1592 | * CS_EP_ERROR back, so give up |
| 1593 | */ |
| 1594 | if (cs->state < CS_ST_CON) |
| 1595 | return 0; |
| 1596 | cs->endp->flags |= CS_EP_ERROR; |
| 1597 | return 1; |
| 1598 | } |
| 1599 | |
| 1600 | /* We're already waiting to be able to send, give up */ |
| 1601 | if (cs->wait_event.events & SUB_RETRY_SEND) |
| 1602 | return 0; |
| 1603 | |
| 1604 | /* we might have been called just after an asynchronous shutw */ |
| 1605 | if (oc->flags & CF_SHUTW) |
| 1606 | return 1; |
| 1607 | |
| 1608 | /* we must wait because the mux is not installed yet */ |
| 1609 | if (!conn->mux) |
| 1610 | return 0; |
| 1611 | |
| 1612 | if (oc->pipe && conn->xprt->snd_pipe && conn->mux->snd_pipe) { |
| 1613 | ret = conn->mux->snd_pipe(cs, oc->pipe); |
| 1614 | if (ret > 0) |
| 1615 | did_send = 1; |
| 1616 | |
| 1617 | if (!oc->pipe->data) { |
| 1618 | put_pipe(oc->pipe); |
| 1619 | oc->pipe = NULL; |
| 1620 | } |
| 1621 | |
| 1622 | if (oc->pipe) |
| 1623 | goto end; |
| 1624 | } |
| 1625 | |
| 1626 | /* At this point, the pipe is empty, but we may still have data pending |
| 1627 | * in the normal buffer. |
| 1628 | */ |
| 1629 | if (co_data(oc)) { |
| 1630 | /* when we're here, we already know that there is no spliced |
| 1631 | * data left, and that there are sendable buffered data. |
| 1632 | */ |
| 1633 | |
| 1634 | /* check if we want to inform the kernel that we're interested in |
| 1635 | * sending more data after this call. We want this if : |
| 1636 | * - we're about to close after this last send and want to merge |
| 1637 | * the ongoing FIN with the last segment. |
| 1638 | * - we know we can't send everything at once and must get back |
| 1639 | * here because of unaligned data |
| 1640 | * - there is still a finite amount of data to forward |
| 1641 | * The test is arranged so that the most common case does only 2 |
| 1642 | * tests. |
| 1643 | */ |
| 1644 | unsigned int send_flag = 0; |
| 1645 | |
| 1646 | if ((!(oc->flags & (CF_NEVER_WAIT|CF_SEND_DONTWAIT)) && |
| 1647 | ((oc->to_forward && oc->to_forward != CHN_INFINITE_FORWARD) || |
| 1648 | (oc->flags & CF_EXPECT_MORE) || |
| 1649 | (IS_HTX_STRM(s) && |
| 1650 | (!(oc->flags & (CF_EOI|CF_SHUTR)) && htx_expect_more(htxbuf(&oc->buf)))))) || |
| 1651 | ((oc->flags & CF_ISRESP) && |
| 1652 | ((oc->flags & (CF_AUTO_CLOSE|CF_SHUTW_NOW)) == (CF_AUTO_CLOSE|CF_SHUTW_NOW)))) |
| 1653 | send_flag |= CO_SFL_MSG_MORE; |
| 1654 | |
| 1655 | if (oc->flags & CF_STREAMER) |
| 1656 | send_flag |= CO_SFL_STREAMER; |
| 1657 | |
| 1658 | if (s->txn && s->txn->flags & TX_L7_RETRY && !b_data(&s->txn->l7_buffer)) { |
| 1659 | /* If we want to be able to do L7 retries, copy |
| 1660 | * the data we're about to send, so that we are able |
| 1661 | * to resend them if needed |
| 1662 | */ |
| 1663 | /* Try to allocate a buffer if we had none. |
| 1664 | * If it fails, the next test will just |
| 1665 | * disable the l7 retries by setting |
| 1666 | * l7_conn_retries to 0. |
| 1667 | */ |
| 1668 | if (s->txn->req.msg_state != HTTP_MSG_DONE) |
| 1669 | s->txn->flags &= ~TX_L7_RETRY; |
| 1670 | else { |
| 1671 | if (b_alloc(&s->txn->l7_buffer) == NULL) |
| 1672 | s->txn->flags &= ~TX_L7_RETRY; |
| 1673 | else { |
| 1674 | memcpy(b_orig(&s->txn->l7_buffer), |
| 1675 | b_orig(&oc->buf), |
| 1676 | b_size(&oc->buf)); |
| 1677 | s->txn->l7_buffer.head = co_data(oc); |
| 1678 | b_add(&s->txn->l7_buffer, co_data(oc)); |
| 1679 | } |
| 1680 | |
| 1681 | } |
| 1682 | } |
| 1683 | |
| 1684 | ret = conn->mux->snd_buf(cs, &oc->buf, co_data(oc), send_flag); |
| 1685 | if (ret > 0) { |
| 1686 | did_send = 1; |
| 1687 | c_rew(oc, ret); |
| 1688 | c_realign_if_empty(oc); |
| 1689 | |
| 1690 | if (!co_data(oc)) { |
| 1691 | /* Always clear both flags once everything has been sent, they're one-shot */ |
| 1692 | oc->flags &= ~(CF_EXPECT_MORE | CF_SEND_DONTWAIT); |
| 1693 | } |
| 1694 | /* if some data remain in the buffer, it's only because the |
| 1695 | * system buffers are full, we will try next time. |
| 1696 | */ |
Christopher Faulet | 13045f0 | 2022-04-01 14:23:38 +0200 | [diff] [blame] | 1697 | } |
| 1698 | } |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1699 | |
| 1700 | end: |
| 1701 | if (did_send) { |
| 1702 | oc->flags |= CF_WRITE_PARTIAL | CF_WROTE_DATA; |
| 1703 | if (cs->state == CS_ST_CON) |
| 1704 | cs->state = CS_ST_RDY; |
| 1705 | |
| 1706 | cs_rx_room_rdy(cs_opposite(cs)); |
| 1707 | } |
| 1708 | |
| 1709 | if (cs->endp->flags & (CS_EP_ERROR|CS_EP_ERR_PENDING)) { |
| 1710 | cs->endp->flags |= CS_EP_ERROR; |
| 1711 | return 1; |
| 1712 | } |
| 1713 | |
| 1714 | /* We couldn't send all of our data, let the mux know we'd like to send more */ |
| 1715 | if (!channel_is_empty(oc)) |
| 1716 | conn->mux->subscribe(cs, SUB_RETRY_SEND, &cs->wait_event); |
| 1717 | return did_send; |
| 1718 | } |
| 1719 | |
Christopher Faulet | 6b0a0fb | 2022-04-04 11:29:28 +0200 | [diff] [blame] | 1720 | /* perform a synchronous send() for the conn-stream. The CF_WRITE_NULL and |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1721 | * CF_WRITE_PARTIAL flags are cleared prior to the attempt, and will possibly |
| 1722 | * be updated in case of success. |
| 1723 | */ |
| 1724 | void cs_conn_sync_send(struct conn_stream *cs) |
| 1725 | { |
| 1726 | struct channel *oc = cs_oc(cs); |
| 1727 | |
| 1728 | oc->flags &= ~(CF_WRITE_NULL|CF_WRITE_PARTIAL); |
| 1729 | |
| 1730 | if (oc->flags & CF_SHUTW) |
| 1731 | return; |
| 1732 | |
| 1733 | if (channel_is_empty(oc)) |
| 1734 | return; |
| 1735 | |
| 1736 | if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST)) |
| 1737 | return; |
| 1738 | |
| 1739 | if (!cs_conn_mux(cs)) |
| 1740 | return; |
| 1741 | |
| 1742 | cs_conn_send(cs); |
| 1743 | } |
| 1744 | |
| 1745 | /* Called by I/O handlers after completion.. It propagates |
Christopher Faulet | 6b0a0fb | 2022-04-04 11:29:28 +0200 | [diff] [blame] | 1746 | * connection flags to the conn-stream, updates the stream (which may or |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1747 | * may not take this opportunity to try to forward data), then update the |
Christopher Faulet | 6b0a0fb | 2022-04-04 11:29:28 +0200 | [diff] [blame] | 1748 | * connection's polling based on the channels and conn-stream's final |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1749 | * states. The function always returns 0. |
| 1750 | */ |
| 1751 | static int cs_conn_process(struct conn_stream *cs) |
| 1752 | { |
| 1753 | struct connection *conn = __cs_conn(cs); |
| 1754 | struct channel *ic = cs_ic(cs); |
| 1755 | struct channel *oc = cs_oc(cs); |
| 1756 | |
| 1757 | BUG_ON(!conn); |
| 1758 | |
| 1759 | /* If we have data to send, try it now */ |
| 1760 | if (!channel_is_empty(oc) && !(cs->wait_event.events & SUB_RETRY_SEND)) |
| 1761 | cs_conn_send(cs); |
| 1762 | |
| 1763 | /* First step, report to the conn-stream what was detected at the |
| 1764 | * connection layer : errors and connection establishment. |
| 1765 | * Only add CS_EP_ERROR if we're connected, or we're attempting to |
| 1766 | * connect, we may get there because we got woken up, but only run |
| 1767 | * after process_stream() noticed there were an error, and decided |
| 1768 | * to retry to connect, the connection may still have CO_FL_ERROR, |
| 1769 | * and we don't want to add CS_EP_ERROR back |
| 1770 | * |
| 1771 | * Note: This test is only required because cs_conn_process is also the SI |
| 1772 | * wake callback. Otherwise cs_conn_recv()/cs_conn_send() already take |
| 1773 | * care of it. |
| 1774 | */ |
| 1775 | |
| 1776 | if (cs->state >= CS_ST_CON) { |
| 1777 | if (cs_is_conn_error(cs)) |
| 1778 | cs->endp->flags |= CS_EP_ERROR; |
| 1779 | } |
| 1780 | |
| 1781 | /* If we had early data, and the handshake ended, then |
| 1782 | * we can remove the flag, and attempt to wake the task up, |
| 1783 | * in the event there's an analyser waiting for the end of |
| 1784 | * the handshake. |
| 1785 | */ |
| 1786 | if (!(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS)) && |
| 1787 | (cs->endp->flags & CS_EP_WAIT_FOR_HS)) { |
| 1788 | cs->endp->flags &= ~CS_EP_WAIT_FOR_HS; |
| 1789 | task_wakeup(cs_strm_task(cs), TASK_WOKEN_MSG); |
| 1790 | } |
| 1791 | |
| 1792 | if (!cs_state_in(cs->state, CS_SB_EST|CS_SB_DIS|CS_SB_CLO) && |
| 1793 | (conn->flags & CO_FL_WAIT_XPRT) == 0) { |
| 1794 | __cs_strm(cs)->conn_exp = TICK_ETERNITY; |
| 1795 | oc->flags |= CF_WRITE_NULL; |
| 1796 | if (cs->state == CS_ST_CON) |
| 1797 | cs->state = CS_ST_RDY; |
| 1798 | } |
| 1799 | |
| 1800 | /* Report EOS on the channel if it was reached from the mux point of |
| 1801 | * view. |
| 1802 | * |
| 1803 | * Note: This test is only required because cs_conn_process is also the SI |
| 1804 | * wake callback. Otherwise cs_conn_recv()/cs_conn_send() already take |
| 1805 | * care of it. |
| 1806 | */ |
| 1807 | if (cs->endp->flags & CS_EP_EOS && !(ic->flags & CF_SHUTR)) { |
| 1808 | /* we received a shutdown */ |
| 1809 | ic->flags |= CF_READ_NULL; |
| 1810 | if (ic->flags & CF_AUTO_CLOSE) |
| 1811 | channel_shutw_now(ic); |
| 1812 | cs_conn_read0(cs); |
| 1813 | } |
| 1814 | |
| 1815 | /* Report EOI on the channel if it was reached from the mux point of |
| 1816 | * view. |
| 1817 | * |
| 1818 | * Note: This test is only required because cs_conn_process is also the SI |
| 1819 | * wake callback. Otherwise cs_conn_recv()/cs_conn_send() already take |
| 1820 | * care of it. |
| 1821 | */ |
| 1822 | if ((cs->endp->flags & CS_EP_EOI) && !(ic->flags & CF_EOI)) |
| 1823 | ic->flags |= (CF_EOI|CF_READ_PARTIAL); |
| 1824 | |
Christopher Faulet | 6b0a0fb | 2022-04-04 11:29:28 +0200 | [diff] [blame] | 1825 | /* Second step : update the conn-stream and channels, try to forward any |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1826 | * pending data, then possibly wake the stream up based on the new |
Christopher Faulet | 6b0a0fb | 2022-04-04 11:29:28 +0200 | [diff] [blame] | 1827 | * conn-stream status. |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1828 | */ |
| 1829 | cs_notify(cs); |
| 1830 | stream_release_buffers(__cs_strm(cs)); |
| 1831 | return 0; |
| 1832 | } |
| 1833 | |
| 1834 | /* This is the ->process() function for any conn-stream's wait_event task. |
Christopher Faulet | 6b0a0fb | 2022-04-04 11:29:28 +0200 | [diff] [blame] | 1835 | * It's assigned during the conn-stream's initialization, for any type of |
| 1836 | * conn-stream. Thus it is always safe to perform a tasklet_wakeup() on a |
| 1837 | * conn-stream, as the presence of the CS is checked there. |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1838 | */ |
| 1839 | struct task *cs_conn_io_cb(struct task *t, void *ctx, unsigned int state) |
| 1840 | { |
| 1841 | struct conn_stream *cs = ctx; |
| 1842 | int ret = 0; |
| 1843 | |
| 1844 | if (!cs_conn(cs)) |
| 1845 | return t; |
| 1846 | |
| 1847 | if (!(cs->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(cs_oc(cs))) |
| 1848 | ret = cs_conn_send(cs); |
| 1849 | if (!(cs->wait_event.events & SUB_RETRY_RECV)) |
| 1850 | ret |= cs_conn_recv(cs); |
| 1851 | if (ret != 0) |
| 1852 | cs_conn_process(cs); |
| 1853 | |
| 1854 | stream_release_buffers(__cs_strm(cs)); |
| 1855 | return t; |
| 1856 | } |
| 1857 | |
| 1858 | /* Callback to be used by applet handlers upon completion. It updates the stream |
| 1859 | * (which may or may not take this opportunity to try to forward data), then |
Christopher Faulet | 6b0a0fb | 2022-04-04 11:29:28 +0200 | [diff] [blame] | 1860 | * may re-enable the applet's based on the channels and conn-stream's final |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1861 | * states. |
| 1862 | */ |
| 1863 | static int cs_applet_process(struct conn_stream *cs) |
| 1864 | { |
| 1865 | struct channel *ic = cs_ic(cs); |
| 1866 | |
| 1867 | BUG_ON(!cs_appctx(cs)); |
| 1868 | |
| 1869 | /* If the applet wants to write and the channel is closed, it's a |
| 1870 | * broken pipe and it must be reported. |
| 1871 | */ |
| 1872 | if (!(cs->endp->flags & CS_EP_RX_WAIT_EP) && (ic->flags & CF_SHUTR)) |
| 1873 | cs->endp->flags |= CS_EP_ERROR; |
| 1874 | |
| 1875 | /* automatically mark the applet having data available if it reported |
| 1876 | * begin blocked by the channel. |
| 1877 | */ |
| 1878 | if (cs_rx_blocked(cs)) |
| 1879 | cs_rx_endp_more(cs); |
| 1880 | |
Christopher Faulet | 6b0a0fb | 2022-04-04 11:29:28 +0200 | [diff] [blame] | 1881 | /* update the conn-stream, channels, and possibly wake the stream up */ |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1882 | cs_notify(cs); |
| 1883 | stream_release_buffers(__cs_strm(cs)); |
| 1884 | |
| 1885 | /* cs_notify may have passed through chk_snd and released some |
| 1886 | * RXBLK flags. Process_stream will consider those flags to wake up the |
| 1887 | * appctx but in the case the task is not in runqueue we may have to |
| 1888 | * wakeup the appctx immediately. |
| 1889 | */ |
| 1890 | if ((cs_rx_endp_ready(cs) && !cs_rx_blocked(cs)) || |
| 1891 | (cs_tx_endp_ready(cs) && !cs_tx_blocked(cs))) |
| 1892 | appctx_wakeup(__cs_appctx(cs)); |
| 1893 | return 0; |
Christopher Faulet | 13045f0 | 2022-04-01 14:23:38 +0200 | [diff] [blame] | 1894 | } |