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