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