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