blob: 5d7ddd10175a32e861c8c278c108b53cf99e0850 [file] [log] [blame]
Christopher Faulet1329f2a2021-12-16 17:32:56 +01001/*
Willy Tarreau4596fe22022-05-17 19:07:51 +02002 * stream connector management functions
Christopher Faulet1329f2a2021-12-16 17:32:56 +01003 *
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 Faulet37046632022-04-01 11:36:58 +020014#include <haproxy/applet.h>
Christopher Faulet1329f2a2021-12-16 17:32:56 +010015#include <haproxy/connection.h>
Christopher Faulet5e29b762022-04-04 08:58:34 +020016#include <haproxy/check.h>
17#include <haproxy/http_ana.h>
18#include <haproxy/pipe.h>
Christopher Faulet1329f2a2021-12-16 17:32:56 +010019#include <haproxy/pool.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020020#include <haproxy/sc_strm.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020021#include <haproxy/stconn.h>
Christopher Faulet7542fb42023-05-11 14:40:27 +020022#include <haproxy/xref.h>
Christopher Faulet1329f2a2021-12-16 17:32:56 +010023
Willy Tarreau4596fe22022-05-17 19:07:51 +020024DECLARE_POOL(pool_head_connstream, "stconn", sizeof(struct stconn));
Willy Tarreauea59b022022-05-17 17:53:22 +020025DECLARE_POOL(pool_head_sedesc, "sedesc", sizeof(struct sedesc));
Christopher Faulet1329f2a2021-12-16 17:32:56 +010026
Willy Tarreau3a3f4802022-05-17 18:28:19 +020027/* functions used by default on a detached stream connector */
Christopher Fauletcfc11c02023-04-13 16:10:23 +020028static void sc_app_abort(struct stconn *sc);
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +020029static void sc_app_shut(struct stconn *sc);
Willy Tarreau0adb2812022-05-27 10:02:48 +020030static void sc_app_chk_rcv(struct stconn *sc);
31static void sc_app_chk_snd(struct stconn *sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +020032
Willy Tarreau3a3f4802022-05-17 18:28:19 +020033/* functions used on a mux-based stream connector */
Christopher Fauletcfc11c02023-04-13 16:10:23 +020034static void sc_app_abort_conn(struct stconn *sc);
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +020035static void sc_app_shut_conn(struct stconn *sc);
Willy Tarreau0adb2812022-05-27 10:02:48 +020036static void sc_app_chk_rcv_conn(struct stconn *sc);
37static void sc_app_chk_snd_conn(struct stconn *sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +020038
Willy Tarreau3a3f4802022-05-17 18:28:19 +020039/* functions used on an applet-based stream connector */
Christopher Fauletcfc11c02023-04-13 16:10:23 +020040static void sc_app_abort_applet(struct stconn *sc);
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +020041static void sc_app_shut_applet(struct stconn *sc);
Willy Tarreau0adb2812022-05-27 10:02:48 +020042static void sc_app_chk_rcv_applet(struct stconn *sc);
43static void sc_app_chk_snd_applet(struct stconn *sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +020044
Willy Tarreau0adb2812022-05-27 10:02:48 +020045static int sc_conn_process(struct stconn *sc);
46static int sc_conn_recv(struct stconn *sc);
47static int sc_conn_send(struct stconn *sc);
48static int sc_applet_process(struct stconn *sc);
Willy Tarreau2f2318d2022-05-18 10:17:16 +020049
Willy Tarreau3a3f4802022-05-17 18:28:19 +020050/* stream connector operations for connections */
51struct sc_app_ops sc_app_conn_ops = {
52 .chk_rcv = sc_app_chk_rcv_conn,
53 .chk_snd = sc_app_chk_snd_conn,
Christopher Fauletcfc11c02023-04-13 16:10:23 +020054 .abort = sc_app_abort_conn,
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +020055 .shutdown= sc_app_shut_conn,
Willy Tarreau462b9892022-05-18 18:06:53 +020056 .wake = sc_conn_process,
Willy Tarreau2f2318d2022-05-18 10:17:16 +020057 .name = "STRM",
Christopher Faulet9ffddd52022-04-01 14:04:29 +020058};
59
Willy Tarreau3a3f4802022-05-17 18:28:19 +020060/* stream connector operations for embedded tasks */
61struct sc_app_ops sc_app_embedded_ops = {
62 .chk_rcv = sc_app_chk_rcv,
63 .chk_snd = sc_app_chk_snd,
Christopher Fauletcfc11c02023-04-13 16:10:23 +020064 .abort = sc_app_abort,
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +020065 .shutdown= sc_app_shut,
Willy Tarreau2f2318d2022-05-18 10:17:16 +020066 .wake = NULL, /* may never be used */
67 .name = "NONE", /* may never be used */
Christopher Faulet9ffddd52022-04-01 14:04:29 +020068};
69
Willy Tarreau2f2318d2022-05-18 10:17:16 +020070/* stream connector operations for applets */
Willy Tarreau3a3f4802022-05-17 18:28:19 +020071struct sc_app_ops sc_app_applet_ops = {
72 .chk_rcv = sc_app_chk_rcv_applet,
73 .chk_snd = sc_app_chk_snd_applet,
Christopher Fauletcfc11c02023-04-13 16:10:23 +020074 .abort = sc_app_abort_applet,
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +020075 .shutdown= sc_app_shut_applet,
Willy Tarreau19c65a92022-05-27 08:49:24 +020076 .wake = sc_applet_process,
Christopher Faulet5e29b762022-04-04 08:58:34 +020077 .name = "STRM",
78};
79
Willy Tarreau2f2318d2022-05-18 10:17:16 +020080/* stream connector for health checks on connections */
81struct sc_app_ops sc_app_check_ops = {
82 .chk_rcv = NULL,
83 .chk_snd = NULL,
Christopher Fauletcfc11c02023-04-13 16:10:23 +020084 .abort = NULL,
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +020085 .shutdown= NULL,
Willy Tarreau2f2318d2022-05-18 10:17:16 +020086 .wake = wake_srv_chk,
87 .name = "CHCK",
88};
Christopher Faulet5e29b762022-04-04 08:58:34 +020089
Christopher Faulet9ed77422022-04-12 08:51:15 +020090/* Initializes an endpoint */
Willy Tarreauea59b022022-05-17 17:53:22 +020091void sedesc_init(struct sedesc *sedesc)
Christopher Fauletdb90f2a2022-03-22 16:06:25 +010092{
Willy Tarreauea59b022022-05-17 17:53:22 +020093 sedesc->se = NULL;
94 sedesc->conn = NULL;
Willy Tarreauc1054922022-05-18 07:43:52 +020095 sedesc->sc = NULL;
Christopher Faulet4c135682023-02-16 11:09:31 +010096 sedesc->lra = TICK_ETERNITY;
97 sedesc->fsb = TICK_ETERNITY;
Christopher Faulet7542fb42023-05-11 14:40:27 +020098 sedesc->xref.peer = NULL;
Willy Tarreauea59b022022-05-17 17:53:22 +020099 se_fl_setall(sedesc, SE_FL_NONE);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100100}
101
Christopher Faulet9ed77422022-04-12 08:51:15 +0200102/* Tries to alloc an endpoint and initialize it. Returns NULL on failure. */
Willy Tarreauea59b022022-05-17 17:53:22 +0200103struct sedesc *sedesc_new()
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100104{
Willy Tarreauea59b022022-05-17 17:53:22 +0200105 struct sedesc *sedesc;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100106
Willy Tarreauea59b022022-05-17 17:53:22 +0200107 sedesc = pool_alloc(pool_head_sedesc);
108 if (unlikely(!sedesc))
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100109 return NULL;
110
Willy Tarreauea59b022022-05-17 17:53:22 +0200111 sedesc_init(sedesc);
112 return sedesc;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100113}
114
Christopher Faulet9ed77422022-04-12 08:51:15 +0200115/* Releases an endpoint. It is the caller responsibility to be sure it is safe
116 * and it is not shared with another entity
117 */
Willy Tarreauea59b022022-05-17 17:53:22 +0200118void sedesc_free(struct sedesc *sedesc)
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100119{
Willy Tarreauea59b022022-05-17 17:53:22 +0200120 pool_free(pool_head_sedesc, sedesc);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100121}
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100122
Willy Tarreau4596fe22022-05-17 19:07:51 +0200123/* Tries to allocate a new stconn and initialize its main fields. On
Christopher Faulet9ed77422022-04-12 08:51:15 +0200124 * failure, nothing is allocated and NULL is returned. It is an internal
Willy Tarreaub605c422022-05-17 17:04:55 +0200125 * function. The caller must, at least, set the SE_FL_ORPHAN or SE_FL_DETACHED
Christopher Faulet9ed77422022-04-12 08:51:15 +0200126 * flag.
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100127 */
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200128static struct stconn *sc_new(struct sedesc *sedesc)
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100129{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200130 struct stconn *sc;
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100131
Willy Tarreau0adb2812022-05-27 10:02:48 +0200132 sc = pool_alloc(pool_head_connstream);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100133
Willy Tarreau0adb2812022-05-27 10:02:48 +0200134 if (unlikely(!sc))
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100135 goto alloc_error;
Christopher Fauletbb772d02022-03-22 15:28:36 +0100136
Willy Tarreau1d2c79a2022-05-27 11:15:19 +0200137 sc->obj_type = OBJ_TYPE_SC;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200138 sc->flags = SC_FL_NONE;
139 sc->state = SC_ST_INI;
Christopher Fauletbe5cc762023-02-20 08:41:55 +0100140 sc->ioto = TICK_ETERNITY;
Christopher Faulet9aed1122023-05-05 11:25:19 +0200141 sc->room_needed = 0;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200142 sc->app = NULL;
143 sc->app_ops = NULL;
144 sc->src = NULL;
145 sc->dst = NULL;
146 sc->wait_event.tasklet = NULL;
147 sc->wait_event.events = 0;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200148
Christopher Faulet9ed77422022-04-12 08:51:15 +0200149 /* If there is no endpoint, allocate a new one now */
Willy Tarreauea59b022022-05-17 17:53:22 +0200150 if (!sedesc) {
151 sedesc = sedesc_new();
152 if (unlikely(!sedesc))
Christopher Fauletb669d682022-03-22 18:37:19 +0100153 goto alloc_error;
154 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200155 sc->sedesc = sedesc;
156 sedesc->sc = sc;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100157
Willy Tarreau0adb2812022-05-27 10:02:48 +0200158 return sc;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100159
160 alloc_error:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200161 pool_free(pool_head_connstream, sc);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100162 return NULL;
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100163}
164
Willy Tarreau31219282022-05-27 16:21:33 +0200165/* Creates a new stream connector and its associated stream from a mux. <sd> must
166 * be defined. It returns NULL on error. On success, the new stream connector is
Willy Tarreaub605c422022-05-17 17:04:55 +0200167 * returned. In this case, SE_FL_ORPHAN flag is removed.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200168 */
Willy Tarreau31219282022-05-27 16:21:33 +0200169struct stconn *sc_new_from_endp(struct sedesc *sd, struct session *sess, struct buffer *input)
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100170{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200171 struct stconn *sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100172
Willy Tarreau31219282022-05-27 16:21:33 +0200173 sc = sc_new(sd);
Willy Tarreau0adb2812022-05-27 10:02:48 +0200174 if (unlikely(!sc))
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100175 return NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200176 if (unlikely(!stream_new(sess, sc, input))) {
Christopher Faulet3ab72c62022-09-27 09:18:20 +0200177 sd->sc = NULL;
Willy Tarreau7a8ca0a2023-03-20 19:53:14 +0100178 if (sc->sedesc != sd) {
179 /* none was provided so sc_new() allocated one */
180 sedesc_free(sc->sedesc);
181 }
182 pool_free(pool_head_connstream, sc);
Christopher Faulet3ab72c62022-09-27 09:18:20 +0200183 se_fl_set(sd, SE_FL_ORPHAN);
184 return NULL;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100185 }
Willy Tarreau31219282022-05-27 16:21:33 +0200186 se_fl_clr(sd, SE_FL_ORPHAN);
Willy Tarreau0adb2812022-05-27 10:02:48 +0200187 return sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100188}
189
Willy Tarreau4596fe22022-05-17 19:07:51 +0200190/* Creates a new stream connector from an stream. There is no endpoint here, thus it
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200191 * will be created by sc_new(). So the SE_FL_DETACHED flag is set. It returns
Willy Tarreau4596fe22022-05-17 19:07:51 +0200192 * NULL on error. On success, the new stream connector is returned.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200193 */
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200194struct stconn *sc_new_from_strm(struct stream *strm, unsigned int flags)
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100195{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200196 struct stconn *sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100197
Willy Tarreau0adb2812022-05-27 10:02:48 +0200198 sc = sc_new(NULL);
199 if (unlikely(!sc))
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100200 return NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200201 sc->flags |= flags;
202 sc_ep_set(sc, SE_FL_DETACHED);
203 sc->app = &strm->obj_type;
204 sc->app_ops = &sc_app_embedded_ops;
205 return sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100206}
207
Willy Tarreau4596fe22022-05-17 19:07:51 +0200208/* Creates a new stream connector from an health-check. There is no endpoint here,
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200209 * thus it will be created by sc_new(). So the SE_FL_DETACHED flag is set. It
Willy Tarreau4596fe22022-05-17 19:07:51 +0200210 * returns NULL on error. On success, the new stream connector is returned.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200211 */
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200212struct stconn *sc_new_from_check(struct check *check, unsigned int flags)
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100213{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200214 struct stconn *sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100215
Willy Tarreau0adb2812022-05-27 10:02:48 +0200216 sc = sc_new(NULL);
217 if (unlikely(!sc))
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100218 return NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200219 sc->flags |= flags;
220 sc_ep_set(sc, SE_FL_DETACHED);
221 sc->app = &check->obj_type;
222 sc->app_ops = &sc_app_check_ops;
223 return sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100224}
225
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200226/* Releases a stconn previously allocated by sc_new(), as well as its
Christopher Faulet9ed77422022-04-12 08:51:15 +0200227 * endpoint, if it exists. This function is called internally or on error path.
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100228 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200229void sc_free(struct stconn *sc)
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100230{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200231 sockaddr_free(&sc->src);
232 sockaddr_free(&sc->dst);
233 if (sc->sedesc) {
234 BUG_ON(!sc_ep_test(sc, SE_FL_DETACHED));
235 sedesc_free(sc->sedesc);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100236 }
Tim Duesterhusb1ec21d2023-04-22 17:47:32 +0200237 tasklet_free(sc->wait_event.tasklet);
Willy Tarreau0adb2812022-05-27 10:02:48 +0200238 pool_free(pool_head_connstream, sc);
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100239}
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100240
Willy Tarreau4596fe22022-05-17 19:07:51 +0200241/* Conditionally removes a stream connector if it is detached and if there is no app
Christopher Fauleteb50c012022-04-21 14:22:53 +0200242 * layer defined. Except on error path, this one must be used. if release, the
Willy Tarreaue68bc612022-05-27 11:23:05 +0200243 * pointer on the SC is set to NULL.
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200244 */
Willy Tarreaue68bc612022-05-27 11:23:05 +0200245static void sc_free_cond(struct stconn **scp)
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200246{
Willy Tarreaue68bc612022-05-27 11:23:05 +0200247 struct stconn *sc = *scp;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200248
Willy Tarreau0adb2812022-05-27 10:02:48 +0200249 if (!sc->app && (!sc->sedesc || sc_ep_test(sc, SE_FL_DETACHED))) {
250 sc_free(sc);
Willy Tarreaue68bc612022-05-27 11:23:05 +0200251 *scp = NULL;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200252 }
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200253}
254
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100255
Willy Tarreau4596fe22022-05-17 19:07:51 +0200256/* Attaches a stconn to a mux endpoint and sets the endpoint ctx. Returns
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500257 * -1 on error and 0 on success. SE_FL_DETACHED flag is removed. This function is
Christopher Faulet9ed77422022-04-12 08:51:15 +0200258 * called from a mux when it is attached to a stream or a health-check.
259 */
Willy Tarreau31219282022-05-27 16:21:33 +0200260int sc_attach_mux(struct stconn *sc, void *sd, void *ctx)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100261{
Christopher Faulet93882042022-01-19 14:56:50 +0100262 struct connection *conn = ctx;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200263 struct sedesc *sedesc = sc->sedesc;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100264
Willy Tarreau0adb2812022-05-27 10:02:48 +0200265 if (sc_strm(sc)) {
266 if (!sc->wait_event.tasklet) {
267 sc->wait_event.tasklet = tasklet_new();
268 if (!sc->wait_event.tasklet)
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200269 return -1;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200270 sc->wait_event.tasklet->process = sc_conn_io_cb;
271 sc->wait_event.tasklet->context = sc;
272 sc->wait_event.events = 0;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200273 }
274
Willy Tarreau0adb2812022-05-27 10:02:48 +0200275 sc->app_ops = &sc_app_conn_ops;
Christopher Faulet7542fb42023-05-11 14:40:27 +0200276 xref_create(&sc->sedesc->xref, &sc_opposite(sc)->sedesc->xref);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100277 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200278 else if (sc_check(sc)) {
279 if (!sc->wait_event.tasklet) {
280 sc->wait_event.tasklet = tasklet_new();
281 if (!sc->wait_event.tasklet)
Christopher Fauletc95eaef2022-05-18 15:57:15 +0200282 return -1;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200283 sc->wait_event.tasklet->process = srv_chk_io_cb;
284 sc->wait_event.tasklet->context = sc;
285 sc->wait_event.events = 0;
Christopher Fauletc95eaef2022-05-18 15:57:15 +0200286 }
287
Willy Tarreau0adb2812022-05-27 10:02:48 +0200288 sc->app_ops = &sc_app_check_ops;
Christopher Fauletc95eaef2022-05-18 15:57:15 +0200289 }
Willy Tarreaue2f79462023-03-20 19:45:41 +0100290
291 sedesc->se = sd;
292 sedesc->conn = ctx;
293 se_fl_set(sedesc, SE_FL_T_MUX);
294 se_fl_clr(sedesc, SE_FL_DETACHED);
295 if (!conn->ctx)
296 conn->ctx = sc;
Christopher Faulet070b91b2022-03-31 19:27:18 +0200297 return 0;
Christopher Faulet93882042022-01-19 14:56:50 +0100298}
299
Willy Tarreau4596fe22022-05-17 19:07:51 +0200300/* Attaches a stconn to an applet endpoint and sets the endpoint
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500301 * ctx. Returns -1 on error and 0 on success. SE_FL_DETACHED flag is
Christopher Faulet9ed77422022-04-12 08:51:15 +0200302 * removed. This function is called by a stream when a backend applet is
303 * registered.
304 */
Willy Tarreau31219282022-05-27 16:21:33 +0200305static void sc_attach_applet(struct stconn *sc, void *sd)
Christopher Faulet93882042022-01-19 14:56:50 +0100306{
Willy Tarreau31219282022-05-27 16:21:33 +0200307 sc->sedesc->se = sd;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200308 sc_ep_set(sc, SE_FL_T_APPLET);
309 sc_ep_clr(sc, SE_FL_DETACHED);
Christopher Faulet7542fb42023-05-11 14:40:27 +0200310 if (sc_strm(sc)) {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200311 sc->app_ops = &sc_app_applet_ops;
Christopher Faulet7542fb42023-05-11 14:40:27 +0200312 xref_create(&sc->sedesc->xref, &sc_opposite(sc)->sedesc->xref);
313 }
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100314}
315
Willy Tarreau4596fe22022-05-17 19:07:51 +0200316/* Attaches a stconn to a app layer and sets the relevant
Willy Tarreaub605c422022-05-17 17:04:55 +0200317 * callbacks. Returns -1 on error and 0 on success. SE_FL_ORPHAN flag is
Christopher Faulet9ed77422022-04-12 08:51:15 +0200318 * removed. This function is called by a stream when it is created to attach it
Willy Tarreau4596fe22022-05-17 19:07:51 +0200319 * on the stream connector on the client side.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200320 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200321int sc_attach_strm(struct stconn *sc, struct stream *strm)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100322{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200323 sc->app = &strm->obj_type;
324 sc_ep_clr(sc, SE_FL_ORPHAN);
Christopher Fauletf4ef3d92023-09-06 08:46:15 +0200325 sc_ep_report_read_activity(sc);
Willy Tarreau0adb2812022-05-27 10:02:48 +0200326 if (sc_ep_test(sc, SE_FL_T_MUX)) {
327 sc->wait_event.tasklet = tasklet_new();
328 if (!sc->wait_event.tasklet)
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200329 return -1;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200330 sc->wait_event.tasklet->process = sc_conn_io_cb;
331 sc->wait_event.tasklet->context = sc;
332 sc->wait_event.events = 0;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200333
Willy Tarreau0adb2812022-05-27 10:02:48 +0200334 sc->app_ops = &sc_app_conn_ops;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100335 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200336 else if (sc_ep_test(sc, SE_FL_T_APPLET)) {
337 sc->app_ops = &sc_app_applet_ops;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100338 }
339 else {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200340 sc->app_ops = &sc_app_embedded_ops;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100341 }
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100342 return 0;
343}
344
Willy Tarreau4596fe22022-05-17 19:07:51 +0200345/* Detaches the stconn from the endpoint, if any. For a connecrion, if a
Christopher Faulet9ed77422022-04-12 08:51:15 +0200346 * mux owns the connection ->detach() callback is called. Otherwise, it means
Willy Tarreau4596fe22022-05-17 19:07:51 +0200347 * the stream connector owns the connection. In this case the connection is closed
Christopher Faulet9ed77422022-04-12 08:51:15 +0200348 * and released. For an applet, the appctx is released. If still allocated, the
349 * endpoint is reset and flag as detached. If the app layer is also detached,
Willy Tarreau4596fe22022-05-17 19:07:51 +0200350 * the stream connector is released.
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100351 */
Willy Tarreaue68bc612022-05-27 11:23:05 +0200352static void sc_detach_endp(struct stconn **scp)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100353{
Willy Tarreaue68bc612022-05-27 11:23:05 +0200354 struct stconn *sc = *scp;
Christopher Faulet6eb53b12023-05-15 09:53:29 +0200355 struct xref *peer;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200356
Willy Tarreau0adb2812022-05-27 10:02:48 +0200357 if (!sc)
Christopher Fauleteb50c012022-04-21 14:22:53 +0200358 return;
359
Christopher Faulet7542fb42023-05-11 14:40:27 +0200360
Christopher Faulet6eb53b12023-05-15 09:53:29 +0200361 /* Remove my link in the original objects. */
362 peer = xref_get_peer_and_lock(&sc->sedesc->xref);
363 if (peer)
364 xref_disconnect(&sc->sedesc->xref, peer);
Christopher Faulet7542fb42023-05-11 14:40:27 +0200365
Willy Tarreau0adb2812022-05-27 10:02:48 +0200366 if (sc_ep_test(sc, SE_FL_T_MUX)) {
367 struct connection *conn = __sc_conn(sc);
368 struct sedesc *sedesc = sc->sedesc;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100369
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100370 if (conn->mux) {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200371 if (sc->wait_event.events != 0)
372 conn->mux->unsubscribe(sc, sc->wait_event.events, &sc->wait_event);
Willy Tarreau798465b2022-05-17 18:20:02 +0200373 se_fl_set(sedesc, SE_FL_ORPHAN);
Willy Tarreauc1054922022-05-18 07:43:52 +0200374 sedesc->sc = NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200375 sc->sedesc = NULL;
Willy Tarreau798465b2022-05-17 18:20:02 +0200376 conn->mux->detach(sedesc);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100377 }
378 else {
379 /* It's too early to have a mux, let's just destroy
380 * the connection
381 */
382 conn_stop_tracking(conn);
383 conn_full_close(conn);
384 if (conn->destroy_cb)
385 conn->destroy_cb(conn);
386 conn_free(conn);
387 }
388 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200389 else if (sc_ep_test(sc, SE_FL_T_APPLET)) {
390 struct appctx *appctx = __sc_appctx(sc);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100391
Willy Tarreau0adb2812022-05-27 10:02:48 +0200392 sc_ep_set(sc, SE_FL_ORPHAN);
393 sc->sedesc->sc = NULL;
394 sc->sedesc = NULL;
Willy Tarreau1c3ead42022-05-10 19:42:22 +0200395 appctx_shut(appctx);
396 appctx_free(appctx);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100397 }
398
Willy Tarreau0adb2812022-05-27 10:02:48 +0200399 if (sc->sedesc) {
Willy Tarreauda59c892022-05-27 17:03:34 +0200400 /* the SD wasn't used and can be recycled */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200401 sc->sedesc->se = NULL;
402 sc->sedesc->conn = NULL;
Willy Tarreauda59c892022-05-27 17:03:34 +0200403 sc->sedesc->flags = 0;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200404 sc_ep_set(sc, SE_FL_DETACHED);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100405 }
406
Willy Tarreaue68bc612022-05-27 11:23:05 +0200407 /* FIXME: Rest SC for now but must be reviewed. SC flags are only
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100408 * connection related for now but this will evolved
409 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200410 sc->flags &= SC_FL_ISBACK;
411 if (sc_strm(sc))
412 sc->app_ops = &sc_app_embedded_ops;
Willy Tarreau2f2318d2022-05-18 10:17:16 +0200413 else
Willy Tarreau0adb2812022-05-27 10:02:48 +0200414 sc->app_ops = NULL;
Willy Tarreaue68bc612022-05-27 11:23:05 +0200415 sc_free_cond(scp);
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100416}
417
Willy Tarreau4596fe22022-05-17 19:07:51 +0200418/* Detaches the stconn from the app layer. If there is no endpoint attached
419 * to the stconn
Christopher Faulet9ed77422022-04-12 08:51:15 +0200420 */
Willy Tarreaue68bc612022-05-27 11:23:05 +0200421static void sc_detach_app(struct stconn **scp)
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100422{
Willy Tarreaue68bc612022-05-27 11:23:05 +0200423 struct stconn *sc = *scp;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200424
Willy Tarreau0adb2812022-05-27 10:02:48 +0200425 if (!sc)
Christopher Fauleteb50c012022-04-21 14:22:53 +0200426 return;
427
Willy Tarreau0adb2812022-05-27 10:02:48 +0200428 sc->app = NULL;
429 sc->app_ops = NULL;
430 sockaddr_free(&sc->src);
431 sockaddr_free(&sc->dst);
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200432
Tim Duesterhusb1ec21d2023-04-22 17:47:32 +0200433 tasklet_free(sc->wait_event.tasklet);
Willy Tarreau0adb2812022-05-27 10:02:48 +0200434 sc->wait_event.tasklet = NULL;
435 sc->wait_event.events = 0;
Willy Tarreaue68bc612022-05-27 11:23:05 +0200436 sc_free_cond(scp);
Christopher Fauleteb50c012022-04-21 14:22:53 +0200437}
438
Willy Tarreau4596fe22022-05-17 19:07:51 +0200439/* Destroy the stconn. It is detached from its endpoint and its
440 * application. After this call, the stconn must be considered as released.
Christopher Fauleteb50c012022-04-21 14:22:53 +0200441 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200442void sc_destroy(struct stconn *sc)
Christopher Fauleteb50c012022-04-21 14:22:53 +0200443{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200444 sc_detach_endp(&sc);
445 sc_detach_app(&sc);
446 BUG_ON_HOT(sc);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100447}
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100448
Willy Tarreau4596fe22022-05-17 19:07:51 +0200449/* Resets the stream connector endpoint. It happens when the app layer want to renew
Christopher Faulet9ed77422022-04-12 08:51:15 +0200450 * its endpoint. For a connection retry for instance. If a mux or an applet is
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500451 * attached, a new endpoint is created. Returns -1 on error and 0 on success.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200452 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200453int sc_reset_endp(struct stconn *sc)
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100454{
Willy Tarreau31219282022-05-27 16:21:33 +0200455 struct sedesc *new_sd;
Christopher Fauletb041b232022-03-24 10:27:02 +0100456
Willy Tarreau0adb2812022-05-27 10:02:48 +0200457 BUG_ON(!sc->app);
Christopher Fauleta6c4a482022-04-28 18:25:24 +0200458
Willy Tarreau0adb2812022-05-27 10:02:48 +0200459 if (!__sc_endp(sc)) {
Christopher Fauletb041b232022-03-24 10:27:02 +0100460 /* endpoint not attached or attached to a mux with no
461 * target. Thus the endpoint will not be release but just
Willy Tarreau0adb2812022-05-27 10:02:48 +0200462 * reset. The app is still attached, the sc will not be
Christopher Fauleteb50c012022-04-21 14:22:53 +0200463 * released.
Christopher Fauletb041b232022-03-24 10:27:02 +0100464 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200465 sc_detach_endp(&sc);
Christopher Fauletb041b232022-03-24 10:27:02 +0100466 return 0;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100467 }
Christopher Fauletb041b232022-03-24 10:27:02 +0100468
469 /* allocate the new endpoint first to be able to set error if it
470 * fails */
Willy Tarreau31219282022-05-27 16:21:33 +0200471 new_sd = sedesc_new();
Christopher Faulet638fe6a2023-04-14 10:28:28 +0200472 if (!unlikely(new_sd))
Christopher Fauletb041b232022-03-24 10:27:02 +0100473 return -1;
Christopher Fauletb041b232022-03-24 10:27:02 +0100474
Willy Tarreau0adb2812022-05-27 10:02:48 +0200475 /* The app is still attached, the sc will not be released */
476 sc_detach_endp(&sc);
Willy Tarreau6a378d12022-08-11 13:56:42 +0200477 BUG_ON(!sc);
Willy Tarreau0adb2812022-05-27 10:02:48 +0200478 BUG_ON(sc->sedesc);
Willy Tarreau31219282022-05-27 16:21:33 +0200479 sc->sedesc = new_sd;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200480 sc->sedesc->sc = sc;
481 sc_ep_set(sc, SE_FL_DETACHED);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100482 return 0;
483}
Christopher Faulet37046632022-04-01 11:36:58 +0200484
485
Willy Tarreaue68bc612022-05-27 11:23:05 +0200486/* Create an applet to handle a stream connector as a new appctx. The SC will
Christopher Faulet37046632022-04-01 11:36:58 +0200487 * wake it up every time it is solicited. The appctx must be deleted by the task
Willy Tarreau19c65a92022-05-27 08:49:24 +0200488 * handler using sc_detach_endp(), possibly from within the function itself.
Christopher Faulet37046632022-04-01 11:36:58 +0200489 * It also pre-initializes the applet's context and returns it (or NULL in case
490 * it could not be allocated).
491 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200492struct appctx *sc_applet_create(struct stconn *sc, struct applet *app)
Christopher Faulet37046632022-04-01 11:36:58 +0200493{
494 struct appctx *appctx;
495
Willy Tarreau0adb2812022-05-27 10:02:48 +0200496 appctx = appctx_new_here(app, sc->sedesc);
Christopher Faulet37046632022-04-01 11:36:58 +0200497 if (!appctx)
498 return NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200499 sc_attach_applet(sc, appctx);
500 appctx->t->nice = __sc_strm(sc)->task->nice;
Willy Tarreau90e8b452022-05-25 18:21:43 +0200501 applet_need_more_data(appctx);
Christopher Faulet37046632022-04-01 11:36:58 +0200502 appctx_wakeup(appctx);
Christopher Fauleta33ff7a2022-04-21 11:52:07 +0200503
Willy Tarreau0adb2812022-05-27 10:02:48 +0200504 sc->state = SC_ST_RDY;
Christopher Faulet37046632022-04-01 11:36:58 +0200505 return appctx;
506}
507
Ilya Shipitsin07be66d2023-04-01 12:26:42 +0200508/* Conditionally forward the close to the write side. It return 1 if it can be
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100509 * forwarded. It is the caller responsibility to forward the close to the write
Christopher Faulete38534c2023-04-13 15:45:24 +0200510 * side. Otherwise, 0 is returned. In this case, SC_FL_SHUT_WANTED flag may be set on
Christopher Faulet87633c32023-04-03 18:32:50 +0200511 * the consumer SC if we are only waiting for the outgoing data to be flushed.
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100512 */
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +0200513static inline int sc_cond_forward_shut(struct stconn *sc)
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100514{
Christopher Faulet406b81c2023-09-06 08:59:33 +0200515 /* Foward the shutdown if an write error occurred on the input channel */
516 if (sc_ic(sc)->flags & CF_WRITE_TIMEOUT)
517 return 1;
518
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100519 /* The close must not be forwarded */
Christopher Fauletca5309a2023-04-17 16:17:32 +0200520 if (!(sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)) || !(sc->flags & SC_FL_NOHALF))
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100521 return 0;
522
523 if (!channel_is_empty(sc_ic(sc))) {
Christopher Fauletdf7cd712023-04-13 15:56:26 +0200524 /* the shutdown cannot be forwarded now because
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100525 * we should flush outgoing data first. But instruct the output
526 * channel it should be done ASAP.
527 */
Christopher Fauletdf7cd712023-04-13 15:56:26 +0200528 sc_schedule_shutdown(sc);
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100529 return 0;
530 }
531
532 /* the close can be immediately forwarded to the write side */
533 return 1;
534}
535
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200536/*
Willy Tarreau4596fe22022-05-17 19:07:51 +0200537 * This function performs a shutdown-read on a detached stream connector in a
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200538 * connected or init state (it does nothing for other states). It either shuts
539 * the read side or marks itself as closed. The buffer flags are updated to
Willy Tarreaucb041662022-05-17 19:44:42 +0200540 * reflect the new state. If the stream connector has SC_FL_NOHALF, we also
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200541 * forward the close to the write side. The owner task is woken up if it exists.
542 */
Christopher Fauletcfc11c02023-04-13 16:10:23 +0200543static void sc_app_abort(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200544{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200545 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200546
Christopher Fauletca5309a2023-04-17 16:17:32 +0200547 if (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))
Christopher Fauletc665bb52023-04-04 10:06:57 +0200548 return;
Christopher Faulet87633c32023-04-03 18:32:50 +0200549
Christopher Faulet0c370ee2023-04-13 16:05:13 +0200550 sc->flags |= SC_FL_ABRT_DONE;
Christopher Faulet87633c32023-04-03 18:32:50 +0200551 ic->flags |= CF_READ_EVENT;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200552
Willy Tarreau0adb2812022-05-27 10:02:48 +0200553 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200554 return;
555
Christopher Faulet208c7122023-04-13 16:16:15 +0200556 if (sc->flags & SC_FL_SHUT_DONE) {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200557 sc->state = SC_ST_DIS;
Christopher Fauletca679922022-07-20 13:24:04 +0200558 if (sc->flags & SC_FL_ISBACK)
559 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200560 }
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +0200561 else if (sc_cond_forward_shut(sc))
562 return sc_app_shut(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200563
564 /* note that if the task exists, it must unregister itself once it runs */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200565 if (!(sc->flags & SC_FL_DONT_WAKE))
566 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200567}
568
569/*
Willy Tarreau4596fe22022-05-17 19:07:51 +0200570 * This function performs a shutdown-write on a detached stream connector in a
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200571 * connected or init state (it does nothing for other states). It either shuts
572 * the write side or marks itself as closed. The buffer flags are updated to
Willy Tarreaue68bc612022-05-27 11:23:05 +0200573 * reflect the new state. It does also close everything if the SC was marked as
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200574 * being in error state. The owner task is woken up if it exists.
575 */
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +0200576static void sc_app_shut(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200577{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200578 struct channel *ic = sc_ic(sc);
579 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200580
Christopher Faulete38534c2023-04-13 15:45:24 +0200581 sc->flags &= ~SC_FL_SHUT_WANTED;
Christopher Faulet208c7122023-04-13 16:16:15 +0200582 if (sc->flags & SC_FL_SHUT_DONE)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200583 return;
Christopher Faulet208c7122023-04-13 16:16:15 +0200584 sc->flags |= SC_FL_SHUT_DONE;
Christopher Faulet87633c32023-04-03 18:32:50 +0200585 oc->flags |= CF_WRITE_EVENT;
Christopher Fauletbcdcfad2023-02-20 08:36:53 +0100586 sc_set_hcto(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200587
Willy Tarreau0adb2812022-05-27 10:02:48 +0200588 switch (sc->state) {
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200589 case SC_ST_RDY:
590 case SC_ST_EST:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200591 /* we have to shut before closing, otherwise some short messages
592 * may never leave the system, especially when there are remaining
593 * unread data in the socket input buffer, or when nolinger is set.
Willy Tarreaucb041662022-05-17 19:44:42 +0200594 * However, if SC_FL_NOLINGER is explicitly set, we know there is
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200595 * no risk so we close both sides immediately.
596 */
Christopher Fauletca5309a2023-04-17 16:17:32 +0200597 if (!(sc->flags & (SC_FL_ERROR|SC_FL_NOLINGER|SC_FL_EOS|SC_FL_ABRT_DONE)) &&
Christopher Fauletad46e522023-04-14 11:59:15 +0200598 !(ic->flags & CF_DONT_READ))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200599 return;
600
Willy Tarreau476c2802022-11-14 07:36:42 +0100601 __fallthrough;
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200602 case SC_ST_CON:
603 case SC_ST_CER:
604 case SC_ST_QUE:
605 case SC_ST_TAR:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200606 /* Note that none of these states may happen with applets */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200607 sc->state = SC_ST_DIS;
Willy Tarreau476c2802022-11-14 07:36:42 +0100608 __fallthrough;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200609 default:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200610 sc->flags &= ~SC_FL_NOLINGER;
Christopher Faulet0c370ee2023-04-13 16:05:13 +0200611 sc->flags |= SC_FL_ABRT_DONE;
Christopher Fauletca679922022-07-20 13:24:04 +0200612 if (sc->flags & SC_FL_ISBACK)
613 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200614 }
615
616 /* note that if the task exists, it must unregister itself once it runs */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200617 if (!(sc->flags & SC_FL_DONT_WAKE))
618 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200619}
620
621/* default chk_rcv function for scheduled tasks */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200622static void sc_app_chk_rcv(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200623{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200624 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200625
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200626 if (ic->pipe) {
627 /* stop reading */
Christopher Faulet7b3d38a2023-05-05 11:28:45 +0200628 sc_need_room(sc, -1);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200629 }
630 else {
631 /* (re)start reading */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200632 if (!(sc->flags & SC_FL_DONT_WAKE))
633 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200634 }
635}
636
637/* default chk_snd function for scheduled tasks */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200638static void sc_app_chk_snd(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200639{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200640 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200641
Christopher Faulet208c7122023-04-13 16:16:15 +0200642 if (unlikely(sc->state != SC_ST_EST || (sc->flags & SC_FL_SHUT_DONE)))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200643 return;
644
Willy Tarreau0adb2812022-05-27 10:02:48 +0200645 if (!sc_ep_test(sc, SE_FL_WAIT_DATA) || /* not waiting for data */
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200646 channel_is_empty(oc)) /* called with nothing to send ! */
647 return;
648
649 /* Otherwise there are remaining data to be sent in the buffer,
650 * so we tell the handler.
651 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200652 sc_ep_clr(sc, SE_FL_WAIT_DATA);
Willy Tarreau0adb2812022-05-27 10:02:48 +0200653 if (!(sc->flags & SC_FL_DONT_WAKE))
654 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200655}
656
657/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200658 * This function performs a shutdown-read on a stream connector attached to
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200659 * a connection in a connected or init state (it does nothing for other
660 * states). It either shuts the read side or marks itself as closed. The buffer
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200661 * flags are updated to reflect the new state. If the stream connector has
Willy Tarreaucb041662022-05-17 19:44:42 +0200662 * SC_FL_NOHALF, we also forward the close to the write side. If a control
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200663 * layer is defined, then it is supposed to be a socket layer and file
664 * descriptors are then shutdown or closed accordingly. The function
665 * automatically disables polling if needed.
666 */
Christopher Fauletcfc11c02023-04-13 16:10:23 +0200667static void sc_app_abort_conn(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200668{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200669 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200670
Willy Tarreau0adb2812022-05-27 10:02:48 +0200671 BUG_ON(!sc_conn(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200672
Christopher Fauletca5309a2023-04-17 16:17:32 +0200673 if (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200674 return;
Christopher Faulet0c370ee2023-04-13 16:05:13 +0200675 sc->flags |= SC_FL_ABRT_DONE;
Christopher Faulet87633c32023-04-03 18:32:50 +0200676 ic->flags |= CF_READ_EVENT;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200677
Willy Tarreau0adb2812022-05-27 10:02:48 +0200678 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200679 return;
680
Christopher Faulet208c7122023-04-13 16:16:15 +0200681 if (sc->flags & SC_FL_SHUT_DONE) {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200682 sc_conn_shut(sc);
683 sc->state = SC_ST_DIS;
Christopher Fauletca679922022-07-20 13:24:04 +0200684 if (sc->flags & SC_FL_ISBACK)
685 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200686 }
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +0200687 else if (sc_cond_forward_shut(sc))
688 return sc_app_shut_conn(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200689}
690
691/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200692 * This function performs a shutdown-write on a stream connector attached to
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200693 * a connection in a connected or init state (it does nothing for other
694 * states). It either shuts the write side or marks itself as closed. The
695 * buffer flags are updated to reflect the new state. It does also close
Willy Tarreaue68bc612022-05-27 11:23:05 +0200696 * everything if the SC was marked as being in error state. If there is a
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200697 * data-layer shutdown, it is called.
698 */
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +0200699static void sc_app_shut_conn(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200700{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200701 struct channel *ic = sc_ic(sc);
702 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200703
Willy Tarreau0adb2812022-05-27 10:02:48 +0200704 BUG_ON(!sc_conn(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200705
Christopher Faulete38534c2023-04-13 15:45:24 +0200706 sc->flags &= ~SC_FL_SHUT_WANTED;
Christopher Faulet208c7122023-04-13 16:16:15 +0200707 if (sc->flags & SC_FL_SHUT_DONE)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200708 return;
Christopher Faulet208c7122023-04-13 16:16:15 +0200709 sc->flags |= SC_FL_SHUT_DONE;
Christopher Faulet87633c32023-04-03 18:32:50 +0200710 oc->flags |= CF_WRITE_EVENT;
Christopher Fauletbcdcfad2023-02-20 08:36:53 +0100711 sc_set_hcto(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200712
Willy Tarreau0adb2812022-05-27 10:02:48 +0200713 switch (sc->state) {
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200714 case SC_ST_RDY:
715 case SC_ST_EST:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200716 /* we have to shut before closing, otherwise some short messages
717 * may never leave the system, especially when there are remaining
718 * unread data in the socket input buffer, or when nolinger is set.
Willy Tarreaucb041662022-05-17 19:44:42 +0200719 * However, if SC_FL_NOLINGER is explicitly set, we know there is
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200720 * no risk so we close both sides immediately.
721 */
722
Christopher Faulet25d9fe52023-04-14 12:09:35 +0200723 if (sc->flags & SC_FL_ERROR) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200724 /* quick close, the socket is already shut anyway */
725 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200726 else if (sc->flags & SC_FL_NOLINGER) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200727 /* unclean data-layer shutdown, typically an aborted request
728 * or a forwarded shutdown from a client to a server due to
729 * option abortonclose. No need for the TLS layer to try to
730 * emit a shutdown message.
731 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200732 sc_conn_shutw(sc, CO_SHW_SILENT);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200733 }
734 else {
735 /* clean data-layer shutdown. This only happens on the
736 * frontend side, or on the backend side when forwarding
737 * a client close in TCP mode or in HTTP TUNNEL mode
738 * while option abortonclose is set. We want the TLS
739 * layer to try to signal it to the peer before we close.
740 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200741 sc_conn_shutw(sc, CO_SHW_NORMAL);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200742
Christopher Fauletca5309a2023-04-17 16:17:32 +0200743 if (!(sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)) && !(ic->flags & CF_DONT_READ))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200744 return;
745 }
746
Willy Tarreau476c2802022-11-14 07:36:42 +0100747 __fallthrough;
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200748 case SC_ST_CON:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200749 /* we may have to close a pending connection, and mark the
Christopher Fauletcfc11c02023-04-13 16:10:23 +0200750 * response buffer as abort
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200751 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200752 sc_conn_shut(sc);
Willy Tarreau476c2802022-11-14 07:36:42 +0100753 __fallthrough;
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200754 case SC_ST_CER:
755 case SC_ST_QUE:
756 case SC_ST_TAR:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200757 sc->state = SC_ST_DIS;
Willy Tarreau476c2802022-11-14 07:36:42 +0100758 __fallthrough;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200759 default:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200760 sc->flags &= ~SC_FL_NOLINGER;
Christopher Faulet0c370ee2023-04-13 16:05:13 +0200761 sc->flags |= SC_FL_ABRT_DONE;
Christopher Fauletca679922022-07-20 13:24:04 +0200762 if (sc->flags & SC_FL_ISBACK)
763 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200764 }
765}
766
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200767/* This function is used for inter-stream connector calls. It is called by the
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200768 * consumer to inform the producer side that it may be interested in checking
769 * for free space in the buffer. Note that it intentionally does not update
770 * timeouts, so that we can still check them later at wake-up. This function is
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200771 * dedicated to connection-based stream connectors.
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200772 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200773static void sc_app_chk_rcv_conn(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200774{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200775 BUG_ON(!sc_conn(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200776
777 /* (re)start reading */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200778 if (sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
779 tasklet_wakeup(sc->wait_event.tasklet);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200780}
781
782
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200783/* This function is used for inter-stream connector calls. It is called by the
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200784 * producer to inform the consumer side that it may be interested in checking
785 * for data in the buffer. Note that it intentionally does not update timeouts,
786 * so that we can still check them later at wake-up.
787 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200788static void sc_app_chk_snd_conn(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200789{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200790 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200791
Willy Tarreau0adb2812022-05-27 10:02:48 +0200792 BUG_ON(!sc_conn(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200793
Willy Tarreau0adb2812022-05-27 10:02:48 +0200794 if (unlikely(!sc_state_in(sc->state, SC_SB_RDY|SC_SB_EST) ||
Christopher Faulet208c7122023-04-13 16:16:15 +0200795 (sc->flags & SC_FL_SHUT_DONE)))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200796 return;
797
798 if (unlikely(channel_is_empty(oc))) /* called with nothing to send ! */
799 return;
800
801 if (!oc->pipe && /* spliced data wants to be forwarded ASAP */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200802 !sc_ep_test(sc, SE_FL_WAIT_DATA)) /* not waiting for data */
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200803 return;
804
Willy Tarreau0adb2812022-05-27 10:02:48 +0200805 if (!(sc->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(sc_oc(sc)))
806 sc_conn_send(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200807
Willy Tarreau0adb2812022-05-27 10:02:48 +0200808 if (sc_ep_test(sc, SE_FL_ERROR | SE_FL_ERR_PENDING) || sc_is_conn_error(sc)) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200809 /* Write error on the file descriptor */
Christopher Faulet56a2b602023-04-14 09:42:59 +0200810 BUG_ON(sc_ep_test(sc, SE_FL_EOS|SE_FL_ERROR|SE_FL_ERR_PENDING) == (SE_FL_EOS|SE_FL_ERR_PENDING));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200811 goto out_wakeup;
812 }
813
814 /* OK, so now we know that some data might have been sent, and that we may
815 * have to poll first. We have to do that too if the buffer is not empty.
816 */
817 if (channel_is_empty(oc)) {
818 /* the connection is established but we can't write. Either the
819 * buffer is empty, or we just refrain from sending because the
820 * ->o limit was reached. Maybe we just wrote the last
821 * chunk and need to close.
822 */
Christopher Faulet87633c32023-04-03 18:32:50 +0200823 if ((oc->flags & CF_AUTO_CLOSE) &&
Christopher Faulet208c7122023-04-13 16:16:15 +0200824 ((sc->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED)) == SC_FL_SHUT_WANTED) &&
Willy Tarreau0adb2812022-05-27 10:02:48 +0200825 sc_state_in(sc->state, SC_SB_RDY|SC_SB_EST)) {
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +0200826 sc_shutdown(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200827 goto out_wakeup;
828 }
829
Christopher Faulet208c7122023-04-13 16:16:15 +0200830 if ((sc->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED)) == 0)
Willy Tarreau0adb2812022-05-27 10:02:48 +0200831 sc_ep_set(sc, SE_FL_WAIT_DATA);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200832 }
833 else {
834 /* Otherwise there are remaining data to be sent in the buffer,
835 * which means we have to poll before doing so.
836 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200837 sc_ep_clr(sc, SE_FL_WAIT_DATA);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200838 }
839
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200840 /* in case of special condition (error, shutdown, end of write...), we
841 * have to notify the task.
842 */
Christopher Faulet208c7122023-04-13 16:16:15 +0200843 if (likely((sc->flags & SC_FL_SHUT_DONE) ||
Christopher Faulet71c486b2023-02-09 14:14:38 +0100844 ((oc->flags & CF_WRITE_EVENT) && sc->state < SC_ST_EST) ||
845 ((oc->flags & CF_WAKE_WRITE) &&
846 ((channel_is_empty(oc) && !oc->to_forward) ||
847 !sc_state_in(sc->state, SC_SB_EST))))) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200848 out_wakeup:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200849 if (!(sc->flags & SC_FL_DONT_WAKE))
850 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200851 }
852}
853
854/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200855 * This function performs a shutdown-read on a stream connector attached to an
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200856 * applet in a connected or init state (it does nothing for other states). It
857 * either shuts the read side or marks itself as closed. The buffer flags are
Willy Tarreaucb041662022-05-17 19:44:42 +0200858 * updated to reflect the new state. If the stream connector has SC_FL_NOHALF,
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200859 * we also forward the close to the write side. The owner task is woken up if
860 * it exists.
861 */
Christopher Fauletcfc11c02023-04-13 16:10:23 +0200862static void sc_app_abort_applet(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200863{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200864 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200865
Willy Tarreau0adb2812022-05-27 10:02:48 +0200866 BUG_ON(!sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200867
Christopher Fauletca5309a2023-04-17 16:17:32 +0200868 if (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200869 return;
Christopher Faulet0c370ee2023-04-13 16:05:13 +0200870 sc->flags |= SC_FL_ABRT_DONE;
Christopher Faulet87633c32023-04-03 18:32:50 +0200871 ic->flags |= CF_READ_EVENT;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200872
Christopher Fauletcfc11c02023-04-13 16:10:23 +0200873 /* Note: on abort, we don't call the applet */
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200874
Willy Tarreau0adb2812022-05-27 10:02:48 +0200875 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200876 return;
877
Christopher Faulet208c7122023-04-13 16:16:15 +0200878 if (sc->flags & SC_FL_SHUT_DONE) {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200879 appctx_shut(__sc_appctx(sc));
880 sc->state = SC_ST_DIS;
Christopher Fauletca679922022-07-20 13:24:04 +0200881 if (sc->flags & SC_FL_ISBACK)
882 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200883 }
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +0200884 else if (sc_cond_forward_shut(sc))
885 return sc_app_shut_applet(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200886}
887
888/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200889 * This function performs a shutdown-write on a stream connector attached to an
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200890 * applet in a connected or init state (it does nothing for other states). It
891 * either shuts the write side or marks itself as closed. The buffer flags are
892 * updated to reflect the new state. It does also close everything if the SI
893 * was marked as being in error state. The owner task is woken up if it exists.
894 */
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +0200895static void sc_app_shut_applet(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200896{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200897 struct channel *ic = sc_ic(sc);
898 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200899
Willy Tarreau0adb2812022-05-27 10:02:48 +0200900 BUG_ON(!sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200901
Christopher Faulete38534c2023-04-13 15:45:24 +0200902 sc->flags &= ~SC_FL_SHUT_WANTED;
Christopher Faulet208c7122023-04-13 16:16:15 +0200903 if (sc->flags & SC_FL_SHUT_DONE)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200904 return;
Christopher Faulet208c7122023-04-13 16:16:15 +0200905 sc->flags |= SC_FL_SHUT_DONE;
Christopher Faulet87633c32023-04-03 18:32:50 +0200906 oc->flags |= CF_WRITE_EVENT;
Christopher Fauletbcdcfad2023-02-20 08:36:53 +0100907 sc_set_hcto(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200908
909 /* on shutw we always wake the applet up */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200910 appctx_wakeup(__sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200911
Willy Tarreau0adb2812022-05-27 10:02:48 +0200912 switch (sc->state) {
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200913 case SC_ST_RDY:
914 case SC_ST_EST:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200915 /* we have to shut before closing, otherwise some short messages
916 * may never leave the system, especially when there are remaining
917 * unread data in the socket input buffer, or when nolinger is set.
Willy Tarreaucb041662022-05-17 19:44:42 +0200918 * However, if SC_FL_NOLINGER is explicitly set, we know there is
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200919 * no risk so we close both sides immediately.
920 */
Christopher Fauletca5309a2023-04-17 16:17:32 +0200921 if (!(sc->flags & (SC_FL_ERROR|SC_FL_NOLINGER|SC_FL_EOS|SC_FL_ABRT_DONE)) &&
Christopher Faulet87633c32023-04-03 18:32:50 +0200922 !(ic->flags & CF_DONT_READ))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200923 return;
924
Willy Tarreau476c2802022-11-14 07:36:42 +0100925 __fallthrough;
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200926 case SC_ST_CON:
927 case SC_ST_CER:
928 case SC_ST_QUE:
929 case SC_ST_TAR:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200930 /* Note that none of these states may happen with applets */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200931 appctx_shut(__sc_appctx(sc));
932 sc->state = SC_ST_DIS;
Willy Tarreau476c2802022-11-14 07:36:42 +0100933 __fallthrough;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200934 default:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200935 sc->flags &= ~SC_FL_NOLINGER;
Christopher Faulet0c370ee2023-04-13 16:05:13 +0200936 sc->flags |= SC_FL_ABRT_DONE;
Christopher Fauletca679922022-07-20 13:24:04 +0200937 if (sc->flags & SC_FL_ISBACK)
938 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200939 }
940}
941
942/* chk_rcv function for applets */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200943static void sc_app_chk_rcv_applet(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200944{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200945 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200946
Willy Tarreau0adb2812022-05-27 10:02:48 +0200947 BUG_ON(!sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200948
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200949 if (!ic->pipe) {
950 /* (re)start reading */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200951 appctx_wakeup(__sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200952 }
953}
954
955/* chk_snd function for applets */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200956static void sc_app_chk_snd_applet(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200957{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200958 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200959
Willy Tarreau0adb2812022-05-27 10:02:48 +0200960 BUG_ON(!sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200961
Christopher Faulet208c7122023-04-13 16:16:15 +0200962 if (unlikely(sc->state != SC_ST_EST || (sc->flags & SC_FL_SHUT_DONE)))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200963 return;
964
Christopher Fauletf227db42023-09-01 13:55:42 +0200965 /* we only wake the applet up if it was waiting for some data and is ready to consume it
966 * or if there is a pending shutdown
967 */
968 if (!sc_ep_test(sc, SE_FL_WAIT_DATA|SE_FL_WONT_CONSUME) && !(sc->flags & SC_FL_SHUT_WANTED))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200969 return;
970
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200971 if (!channel_is_empty(oc)) {
972 /* (re)start sending */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200973 appctx_wakeup(__sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200974 }
975}
Christopher Faulet13045f02022-04-01 14:23:38 +0200976
977
978/* This function is designed to be called from within the stream handler to
Willy Tarreau4596fe22022-05-17 19:07:51 +0200979 * update the input channel's expiration timer and the stream connector's
Christopher Faulet13045f02022-04-01 14:23:38 +0200980 * Rx flags based on the channel's flags. It needs to be called only once
981 * after the channel's flags have settled down, and before they are cleared,
982 * though it doesn't harm to call it as often as desired (it just slightly
983 * hurts performance). It must not be called from outside of the stream
984 * handler, as what it does will be used to compute the stream task's
985 * expiration.
986 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200987void sc_update_rx(struct stconn *sc)
Christopher Faulet13045f02022-04-01 14:23:38 +0200988{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200989 struct channel *ic = sc_ic(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +0200990
Christopher Fauletca5309a2023-04-17 16:17:32 +0200991 if (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))
Christopher Faulet13045f02022-04-01 14:23:38 +0200992 return;
Christopher Faulet13045f02022-04-01 14:23:38 +0200993
Christopher Fauletfab82bf2023-05-05 11:30:16 +0200994 /* Unblock the SC if it needs room and the free space is large enough (0
995 * means it can always be unblocked). Do not unblock it if -1 was
996 * specified.
997 */
998 if (!sc->room_needed || (sc->room_needed > 0 && channel_recv_max(ic) >= sc->room_needed))
999 sc_have_room(sc);
1000
Christopher Faulet13045f02022-04-01 14:23:38 +02001001 /* Read not closed, update FD status and timeout for reads */
1002 if (ic->flags & CF_DONT_READ)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001003 sc_wont_read(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +02001004 else
Willy Tarreau0adb2812022-05-27 10:02:48 +02001005 sc_will_read(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +02001006
Willy Tarreau0adb2812022-05-27 10:02:48 +02001007 sc_chk_rcv(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +02001008}
1009
1010/* This function is designed to be called from within the stream handler to
Willy Tarreau4596fe22022-05-17 19:07:51 +02001011 * update the output channel's expiration timer and the stream connector's
Christopher Faulet13045f02022-04-01 14:23:38 +02001012 * Tx flags based on the channel's flags. It needs to be called only once
1013 * after the channel's flags have settled down, and before they are cleared,
1014 * though it doesn't harm to call it as often as desired (it just slightly
1015 * hurts performance). It must not be called from outside of the stream
1016 * handler, as what it does will be used to compute the stream task's
1017 * expiration.
1018 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001019void sc_update_tx(struct stconn *sc)
Christopher Faulet13045f02022-04-01 14:23:38 +02001020{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001021 struct channel *oc = sc_oc(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +02001022
Christopher Faulet208c7122023-04-13 16:16:15 +02001023 if (sc->flags & SC_FL_SHUT_DONE)
Christopher Faulet13045f02022-04-01 14:23:38 +02001024 return;
1025
1026 /* Write not closed, update FD status and timeout for writes */
1027 if (channel_is_empty(oc)) {
1028 /* stop writing */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001029 if (!sc_ep_test(sc, SE_FL_WAIT_DATA)) {
Christopher Faulete38534c2023-04-13 15:45:24 +02001030 if ((sc->flags & SC_FL_SHUT_WANTED) == 0)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001031 sc_ep_set(sc, SE_FL_WAIT_DATA);
Christopher Faulet13045f02022-04-01 14:23:38 +02001032 }
1033 return;
1034 }
1035
Christopher Faulet15315d62023-02-20 08:23:51 +01001036 /* (re)start writing */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001037 sc_ep_clr(sc, SE_FL_WAIT_DATA);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001038}
1039
Willy Tarreau19c65a92022-05-27 08:49:24 +02001040/* This function is the equivalent to sc_update() except that it's
Christopher Faulet5e29b762022-04-04 08:58:34 +02001041 * designed to be called from outside the stream handlers, typically the lower
1042 * layers (applets, connections) after I/O completion. After updating the stream
1043 * interface and timeouts, it will try to forward what can be forwarded, then to
1044 * wake the associated task up if an important event requires special handling.
Willy Tarreau15252cd2022-05-25 16:36:21 +02001045 * It may update SE_FL_WAIT_DATA and/or SC_FL_NEED_ROOM, that the callers are
Christopher Faulet5e29b762022-04-04 08:58:34 +02001046 * encouraged to watch to take appropriate action.
Willy Tarreau19c65a92022-05-27 08:49:24 +02001047 * It should not be called from within the stream itself, sc_update()
Christopher Faulet5e29b762022-04-04 08:58:34 +02001048 * is designed for this.
1049 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001050static void sc_notify(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001051{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001052 struct channel *ic = sc_ic(sc);
1053 struct channel *oc = sc_oc(sc);
Willy Tarreaue68bc612022-05-27 11:23:05 +02001054 struct stconn *sco = sc_opposite(sc);
Willy Tarreau0adb2812022-05-27 10:02:48 +02001055 struct task *task = sc_strm_task(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001056
1057 /* process consumer side */
1058 if (channel_is_empty(oc)) {
Willy Tarreau0adb2812022-05-27 10:02:48 +02001059 struct connection *conn = sc_conn(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001060
Christopher Faulet208c7122023-04-13 16:16:15 +02001061 if (((sc->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED)) == SC_FL_SHUT_WANTED) &&
Willy Tarreau0adb2812022-05-27 10:02:48 +02001062 (sc->state == SC_ST_EST) && (!conn || !(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS))))
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +02001063 sc_shutdown(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001064 }
1065
1066 /* indicate that we may be waiting for data from the output channel or
Christopher Faulete38534c2023-04-13 15:45:24 +02001067 * we're about to close and can't expect more data if SC_FL_SHUT_WANTED is there.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001068 */
Christopher Faulet208c7122023-04-13 16:16:15 +02001069 if (!(sc->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED)))
Willy Tarreau0adb2812022-05-27 10:02:48 +02001070 sc_ep_set(sc, SE_FL_WAIT_DATA);
Christopher Faulet208c7122023-04-13 16:16:15 +02001071 else if ((sc->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED)) == SC_FL_SHUT_WANTED)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001072 sc_ep_clr(sc, SE_FL_WAIT_DATA);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001073
Christopher Faulet5e29b762022-04-04 08:58:34 +02001074 if (oc->flags & CF_DONT_READ)
Willy Tarreaue68bc612022-05-27 11:23:05 +02001075 sc_wont_read(sco);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001076 else
Willy Tarreaue68bc612022-05-27 11:23:05 +02001077 sc_will_read(sco);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001078
1079 /* Notify the other side when we've injected data into the IC that
1080 * needs to be forwarded. We can do fast-forwarding as soon as there
1081 * are output data, but we avoid doing this if some of the data are
1082 * not yet scheduled for being forwarded, because it is very likely
1083 * that it will be done again immediately afterwards once the following
Willy Tarreau15252cd2022-05-25 16:36:21 +02001084 * data are parsed (eg: HTTP chunking). We only clear SC_FL_NEED_ROOM
1085 * once we've emptied *some* of the output buffer, and not just when
1086 * there is available room, because applets are often forced to stop
1087 * before the buffer is full. We must not stop based on input data
1088 * alone because an HTTP parser might need more data to complete the
1089 * parsing.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001090 */
1091 if (!channel_is_empty(ic) &&
Willy Tarreaue68bc612022-05-27 11:23:05 +02001092 sc_ep_test(sco, SE_FL_WAIT_DATA) &&
Christopher Faulet84d3ef92023-03-17 15:45:58 +01001093 (!(sc->flags & SC_FL_SND_EXP_MORE) || c_full(ic) || ci_data(ic) == 0 || ic->pipe)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001094 int new_len, last_len;
1095
1096 last_len = co_data(ic);
1097 if (ic->pipe)
1098 last_len += ic->pipe->data;
1099
Willy Tarreaue68bc612022-05-27 11:23:05 +02001100 sc_chk_snd(sco);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001101
1102 new_len = co_data(ic);
1103 if (ic->pipe)
1104 new_len += ic->pipe->data;
1105
1106 /* check if the consumer has freed some space either in the
1107 * buffer or in the pipe.
1108 */
Christopher Faulet18b33092023-05-05 11:40:07 +02001109 if (!sc->room_needed || (new_len < last_len && (sc->room_needed < 0 || channel_recv_max(ic) >= sc->room_needed)))
Willy Tarreau0adb2812022-05-27 10:02:48 +02001110 sc_have_room(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001111 }
1112
1113 if (!(ic->flags & CF_DONT_READ))
Willy Tarreau0adb2812022-05-27 10:02:48 +02001114 sc_will_read(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001115
Willy Tarreau0adb2812022-05-27 10:02:48 +02001116 sc_chk_rcv(sc);
Willy Tarreaue68bc612022-05-27 11:23:05 +02001117 sc_chk_rcv(sco);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001118
Christopher Faulet5e29b762022-04-04 08:58:34 +02001119 /* wake the task up only when needed */
Christopher Faulet285f7612022-12-12 08:28:55 +01001120 if (/* changes on the production side that must be handled:
Christopher Fauletad46e522023-04-14 11:59:15 +02001121 * - An error on receipt: SC_FL_ERROR
Christopher Fauletca5309a2023-04-17 16:17:32 +02001122 * - A read event: shutdown for reads (CF_READ_EVENT + EOS/ABRT_DONE)
Christopher Faulet904763f2023-03-22 14:53:11 +01001123 * end of input (CF_READ_EVENT + SC_FL_EOI)
Christopher Faulet285f7612022-12-12 08:28:55 +01001124 * data received and no fast-forwarding (CF_READ_EVENT + !to_forward)
1125 * read event while consumer side is not established (CF_READ_EVENT + sco->state != SC_ST_EST)
1126 */
Christopher Fauletca5309a2023-04-17 16:17:32 +02001127 ((ic->flags & CF_READ_EVENT) && ((sc->flags & SC_FL_EOI) || (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)) || !ic->to_forward || sco->state != SC_ST_EST)) ||
Christopher Faulet25d9fe52023-04-14 12:09:35 +02001128 (sc->flags & SC_FL_ERROR) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001129
1130 /* changes on the consumption side */
Christopher Faulet2e56a732023-01-26 16:18:09 +01001131 sc_ep_test(sc, SE_FL_ERR_PENDING) ||
Christopher Fauletd8988412022-12-20 18:10:04 +01001132 ((oc->flags & CF_WRITE_EVENT) &&
1133 ((sc->state < SC_ST_EST) ||
Christopher Faulet208c7122023-04-13 16:16:15 +02001134 (sc->flags & SC_FL_SHUT_DONE) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001135 (((oc->flags & CF_WAKE_WRITE) ||
Christopher Faulet87633c32023-04-03 18:32:50 +02001136 (!(oc->flags & CF_AUTO_CLOSE) &&
Christopher Faulet208c7122023-04-13 16:16:15 +02001137 !(sc->flags & (SC_FL_SHUT_WANTED|SC_FL_SHUT_DONE)))) &&
Christopher Faulet87633c32023-04-03 18:32:50 +02001138 (sco->state != SC_ST_EST ||
1139 (channel_is_empty(oc) && !oc->to_forward)))))) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001140 task_wakeup(task, TASK_WOKEN_IO);
1141 }
Christopher Faulet13a8a592023-09-06 09:09:10 +02001142 else {
Christopher Fauleta93d8d22023-11-09 09:54:51 +01001143 /* Update expiration date for the task and requeue it if not already expired */
1144 if (!tick_is_expired(task->expire, now_ms)) {
1145 task->expire = tick_first(task->expire, sc_ep_rcv_ex(sc));
1146 task->expire = tick_first(task->expire, sc_ep_snd_ex(sc));
1147 task->expire = tick_first(task->expire, sc_ep_rcv_ex(sco));
1148 task->expire = tick_first(task->expire, sc_ep_snd_ex(sco));
1149 task->expire = tick_first(task->expire, ic->analyse_exp);
1150 task->expire = tick_first(task->expire, oc->analyse_exp);
1151 task->expire = tick_first(task->expire, __sc_strm(sc)->conn_exp);
Christopher Faulet13a8a592023-09-06 09:09:10 +02001152
Christopher Fauleta93d8d22023-11-09 09:54:51 +01001153 task_queue(task);
1154 }
Christopher Faulet13a8a592023-09-06 09:09:10 +02001155 }
1156
Christopher Faulet2e56a732023-01-26 16:18:09 +01001157 if (ic->flags & CF_READ_EVENT)
Christopher Faulet9a790f62023-03-16 14:40:03 +01001158 sc->flags &= ~SC_FL_RCV_ONCE;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001159}
1160
1161/*
Christopher Faulet1aec6c92023-04-17 17:29:29 +02001162 * This function propagates an end-of-stream received on a socket-based connection.
Willy Tarreaucb041662022-05-17 19:44:42 +02001163 * It updates the stream connector. If the stream connector has SC_FL_NOHALF,
Christopher Faulet5e29b762022-04-04 08:58:34 +02001164 * the close is also forwarded to the write side as an abort.
1165 */
Christopher Faulet1aec6c92023-04-17 17:29:29 +02001166static void sc_conn_eos(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001167{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001168 struct channel *ic = sc_ic(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001169
Willy Tarreau0adb2812022-05-27 10:02:48 +02001170 BUG_ON(!sc_conn(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001171
Christopher Fauletca5309a2023-04-17 16:17:32 +02001172 if (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001173 return;
Christopher Faulet1aec6c92023-04-17 17:29:29 +02001174 sc->flags |= SC_FL_EOS;
Christopher Faulet87633c32023-04-03 18:32:50 +02001175 ic->flags |= CF_READ_EVENT;
Christopher Faulet4c135682023-02-16 11:09:31 +01001176 sc_ep_report_read_activity(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001177
Willy Tarreau0adb2812022-05-27 10:02:48 +02001178 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001179 return;
1180
Christopher Faulet208c7122023-04-13 16:16:15 +02001181 if (sc->flags & SC_FL_SHUT_DONE)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001182 goto do_close;
1183
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +02001184 if (sc_cond_forward_shut(sc)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001185 /* we want to immediately forward this close to the write side */
1186 /* force flag on ssl to keep stream in cache */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001187 sc_conn_shutw(sc, CO_SHW_SILENT);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001188 goto do_close;
1189 }
1190
1191 /* otherwise that's just a normal read shutdown */
1192 return;
1193
1194 do_close:
Willy Tarreauf61dd192022-05-27 09:00:19 +02001195 /* OK we completely close the socket here just as if we went through sc_shut[rw]() */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001196 sc_conn_shut(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001197
Christopher Faulete38534c2023-04-13 15:45:24 +02001198 sc->flags &= ~SC_FL_SHUT_WANTED;
Christopher Faulet208c7122023-04-13 16:16:15 +02001199 sc->flags |= SC_FL_SHUT_DONE;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001200
Willy Tarreau0adb2812022-05-27 10:02:48 +02001201 sc->state = SC_ST_DIS;
Christopher Fauletca679922022-07-20 13:24:04 +02001202 if (sc->flags & SC_FL_ISBACK)
1203 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001204 return;
1205}
1206
1207/*
1208 * This is the callback which is called by the connection layer to receive data
1209 * into the buffer from the connection. It iterates over the mux layer's
1210 * rcv_buf function.
1211 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001212static int sc_conn_recv(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001213{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001214 struct connection *conn = __sc_conn(sc);
1215 struct channel *ic = sc_ic(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001216 int ret, max, cur_read = 0;
1217 int read_poll = MAX_READ_POLL_LOOPS;
1218 int flags = 0;
1219
1220 /* If not established yet, do nothing. */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001221 if (sc->state != SC_ST_EST)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001222 return 0;
1223
Willy Tarreau462b9892022-05-18 18:06:53 +02001224 /* If another call to sc_conn_recv() failed, and we subscribed to
Christopher Faulet5e29b762022-04-04 08:58:34 +02001225 * recv events already, give up now.
1226 */
Christopher Faulet95125882023-04-12 18:35:18 +02001227 if ((sc->wait_event.events & SUB_RETRY_RECV) || sc_waiting_room(sc))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001228 return 0;
1229
Christopher Fauletcfc11c02023-04-13 16:10:23 +02001230 /* maybe we were called immediately after an asynchronous abort */
Christopher Fauletca5309a2023-04-17 16:17:32 +02001231 if (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001232 return 1;
1233
1234 /* we must wait because the mux is not installed yet */
1235 if (!conn->mux)
1236 return 0;
1237
Christopher Faulet5e29b762022-04-04 08:58:34 +02001238 /* stop immediately on errors. Note that we DON'T want to stop on
1239 * POLL_ERR, as the poller might report a write error while there
1240 * are still data available in the recv buffer. This typically
1241 * happens when we send too large a request to a backend server
1242 * which rejects it before reading it all.
1243 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001244 if (!sc_ep_test(sc, SE_FL_RCV_MORE)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001245 if (!conn_xprt_ready(conn))
1246 return 0;
Willy Tarreau0adb2812022-05-27 10:02:48 +02001247 if (sc_ep_test(sc, SE_FL_ERROR))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001248 goto end_recv;
1249 }
1250
1251 /* prepare to detect if the mux needs more room */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001252 sc_ep_clr(sc, SE_FL_WANT_ROOM);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001253
1254 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) && !co_data(ic) &&
1255 global.tune.idle_timer &&
1256 (unsigned short)(now_ms - ic->last_read) >= global.tune.idle_timer) {
1257 /* The buffer was empty and nothing was transferred for more
1258 * than one second. This was caused by a pause and not by
1259 * congestion. Reset any streaming mode to reduce latency.
1260 */
1261 ic->xfer_small = 0;
1262 ic->xfer_large = 0;
1263 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
1264 }
1265
1266 /* First, let's see if we may splice data across the channel without
1267 * using a buffer.
1268 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001269 if (sc_ep_test(sc, SE_FL_MAY_SPLICE) &&
Christopher Faulet5e29b762022-04-04 08:58:34 +02001270 (ic->pipe || ic->to_forward >= MIN_SPLICE_FORWARD) &&
1271 ic->flags & CF_KERN_SPLICING) {
1272 if (c_data(ic)) {
1273 /* We're embarrassed, there are already data pending in
1274 * the buffer and we don't want to have them at two
1275 * locations at a time. Let's indicate we need some
1276 * place and ask the consumer to hurry.
1277 */
1278 flags |= CO_RFL_BUF_FLUSH;
1279 goto abort_splice;
1280 }
1281
1282 if (unlikely(ic->pipe == NULL)) {
1283 if (pipes_used >= global.maxpipes || !(ic->pipe = get_pipe())) {
1284 ic->flags &= ~CF_KERN_SPLICING;
1285 goto abort_splice;
1286 }
1287 }
1288
Willy Tarreau0adb2812022-05-27 10:02:48 +02001289 ret = conn->mux->rcv_pipe(sc, ic->pipe, ic->to_forward);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001290 if (ret < 0) {
1291 /* splice not supported on this end, let's disable it */
1292 ic->flags &= ~CF_KERN_SPLICING;
1293 goto abort_splice;
1294 }
1295
1296 if (ret > 0) {
1297 if (ic->to_forward != CHN_INFINITE_FORWARD)
1298 ic->to_forward -= ret;
1299 ic->total += ret;
1300 cur_read += ret;
Christopher Faulet285f7612022-12-12 08:28:55 +01001301 ic->flags |= CF_READ_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001302 }
1303
Willy Tarreau0adb2812022-05-27 10:02:48 +02001304 if (sc_ep_test(sc, SE_FL_EOS | SE_FL_ERROR))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001305 goto end_recv;
1306
1307 if (conn->flags & CO_FL_WAIT_ROOM) {
1308 /* the pipe is full or we have read enough data that it
1309 * could soon be full. Let's stop before needing to poll.
1310 */
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02001311 sc_need_room(sc, 0);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001312 goto done_recv;
1313 }
1314
1315 /* splice not possible (anymore), let's go on on standard copy */
1316 }
1317
1318 abort_splice:
1319 if (ic->pipe && unlikely(!ic->pipe->data)) {
1320 put_pipe(ic->pipe);
1321 ic->pipe = NULL;
1322 }
1323
Willy Tarreau0adb2812022-05-27 10:02:48 +02001324 if (ic->pipe && ic->to_forward && !(flags & CO_RFL_BUF_FLUSH) && sc_ep_test(sc, SE_FL_MAY_SPLICE)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001325 /* don't break splicing by reading, but still call rcv_buf()
1326 * to pass the flag.
1327 */
1328 goto done_recv;
1329 }
1330
1331 /* now we'll need a input buffer for the stream */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001332 if (!sc_alloc_ibuf(sc, &(__sc_strm(sc)->buffer_wait)))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001333 goto end_recv;
1334
1335 /* For an HTX stream, if the buffer is stuck (no output data with some
1336 * input data) and if the HTX message is fragmented or if its free space
1337 * wraps, we force an HTX deframentation. It is a way to have a
1338 * contiguous free space nad to let the mux to copy as much data as
1339 * possible.
1340 *
1341 * NOTE: A possible optim may be to let the mux decides if defrag is
1342 * required or not, depending on amount of data to be xferred.
1343 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001344 if (IS_HTX_STRM(__sc_strm(sc)) && !co_data(ic)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001345 struct htx *htx = htxbuf(&ic->buf);
1346
1347 if (htx_is_not_empty(htx) && ((htx->flags & HTX_FL_FRAGMENTED) || htx_space_wraps(htx)))
1348 htx_defrag(htx, NULL, 0);
1349 }
1350
1351 /* Instruct the mux it must subscribed for read events */
Christopher Fauletd24b51a2023-11-13 19:16:09 +01001352 if (!conn_is_back(conn) && /* for frontend conns only */
1353 (sc_opposite(sc)->state != SC_ST_INI) && /* before backend connection setup */
1354 (__sc_strm(sc)->be->options & PR_O_ABRT_CLOSE)) /* if abortonclose option is set for the current backend */
1355 flags |= CO_RFL_KEEP_RECV;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001356
1357 /* Important note : if we're called with POLL_IN|POLL_HUP, it means the read polling
1358 * was enabled, which implies that the recv buffer was not full. So we have a guarantee
1359 * that if such an event is not handled above in splice, it will be handled here by
1360 * recv().
1361 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001362 while (sc_ep_test(sc, SE_FL_RCV_MORE) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001363 (!(conn->flags & CO_FL_HANDSHAKE) &&
Christopher Fauletca5309a2023-04-17 16:17:32 +02001364 (!sc_ep_test(sc, SE_FL_ERROR | SE_FL_EOS)) && !(sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)))) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001365 int cur_flags = flags;
1366
1367 /* Compute transient CO_RFL_* flags */
1368 if (co_data(ic)) {
1369 cur_flags |= (CO_RFL_BUF_WET | CO_RFL_BUF_NOT_STUCK);
1370 }
1371
1372 /* <max> may be null. This is the mux responsibility to set
Willy Tarreaue68bc612022-05-27 11:23:05 +02001373 * SE_FL_RCV_MORE on the SC if more space is needed.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001374 */
1375 max = channel_recv_max(ic);
Willy Tarreau0adb2812022-05-27 10:02:48 +02001376 ret = conn->mux->rcv_buf(sc, &ic->buf, max, cur_flags);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001377
Willy Tarreau0adb2812022-05-27 10:02:48 +02001378 if (sc_ep_test(sc, SE_FL_WANT_ROOM)) {
Willy Tarreaub605c422022-05-17 17:04:55 +02001379 /* SE_FL_WANT_ROOM must not be reported if the channel's
Christopher Faulet5e29b762022-04-04 08:58:34 +02001380 * buffer is empty.
1381 */
1382 BUG_ON(c_empty(ic));
1383
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02001384 sc_need_room(sc, channel_recv_max(ic) + 1);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001385 /* Add READ_PARTIAL because some data are pending but
1386 * cannot be xferred to the channel
1387 */
Christopher Faulet285f7612022-12-12 08:28:55 +01001388 ic->flags |= CF_READ_EVENT;
Christopher Faulet2bb2ee22023-11-07 07:45:43 +01001389 sc_ep_report_read_activity(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001390 }
1391
1392 if (ret <= 0) {
1393 /* if we refrained from reading because we asked for a
1394 * flush to satisfy rcv_pipe(), we must not subscribe
1395 * and instead report that there's not enough room
1396 * here to proceed.
1397 */
1398 if (flags & CO_RFL_BUF_FLUSH)
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02001399 sc_need_room(sc, -1);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001400 break;
1401 }
1402
1403 cur_read += ret;
1404
1405 /* if we're allowed to directly forward data, we must update ->o */
Christopher Faulet64350bb2023-04-13 16:37:37 +02001406 if (ic->to_forward && !(sc_opposite(sc)->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED))) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001407 unsigned long fwd = ret;
1408 if (ic->to_forward != CHN_INFINITE_FORWARD) {
1409 if (fwd > ic->to_forward)
1410 fwd = ic->to_forward;
1411 ic->to_forward -= fwd;
1412 }
1413 c_adv(ic, fwd);
1414 }
1415
Christopher Faulet285f7612022-12-12 08:28:55 +01001416 ic->flags |= CF_READ_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001417 ic->total += ret;
1418
1419 /* End-of-input reached, we can leave. In this case, it is
Willy Tarreaue68bc612022-05-27 11:23:05 +02001420 * important to break the loop to not block the SC because of
Christopher Faulet5e29b762022-04-04 08:58:34 +02001421 * the channel's policies.This way, we are still able to receive
1422 * shutdowns.
1423 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001424 if (sc_ep_test(sc, SE_FL_EOI))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001425 break;
1426
Christopher Faulet9a790f62023-03-16 14:40:03 +01001427 if ((sc->flags & SC_FL_RCV_ONCE) || --read_poll <= 0) {
1428 /* we don't expect to read more data */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001429 sc_wont_read(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001430 break;
1431 }
1432
1433 /* if too many bytes were missing from last read, it means that
1434 * it's pointless trying to read again because the system does
1435 * not have them in buffers.
1436 */
1437 if (ret < max) {
1438 /* if a streamer has read few data, it may be because we
1439 * have exhausted system buffers. It's not worth trying
1440 * again.
1441 */
1442 if (ic->flags & CF_STREAMER) {
1443 /* we're stopped by the channel's policy */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001444 sc_wont_read(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001445 break;
1446 }
1447
1448 /* if we read a large block smaller than what we requested,
1449 * it's almost certain we'll never get anything more.
1450 */
1451 if (ret >= global.tune.recv_enough) {
1452 /* we're stopped by the channel's policy */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001453 sc_wont_read(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001454 break;
1455 }
1456 }
1457
1458 /* if we are waiting for more space, don't try to read more data
1459 * right now.
1460 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001461 if (sc->flags & (SC_FL_WONT_READ|SC_FL_NEED_BUFF|SC_FL_NEED_ROOM))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001462 break;
1463 } /* while !flags */
1464
1465 done_recv:
1466 if (cur_read) {
1467 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) &&
1468 (cur_read <= ic->buf.size / 2)) {
1469 ic->xfer_large = 0;
1470 ic->xfer_small++;
1471 if (ic->xfer_small >= 3) {
1472 /* we have read less than half of the buffer in
1473 * one pass, and this happened at least 3 times.
1474 * This is definitely not a streamer.
1475 */
1476 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
1477 }
1478 else if (ic->xfer_small >= 2) {
1479 /* if the buffer has been at least half full twice,
1480 * we receive faster than we send, so at least it
1481 * is not a "fast streamer".
1482 */
1483 ic->flags &= ~CF_STREAMER_FAST;
1484 }
1485 }
Christopher Faulet0b25b6a2023-11-17 11:23:11 +01001486 else if (!(ic->flags & CF_STREAMER_FAST) && (cur_read >= channel_data_limit(ic))) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001487 /* we read a full buffer at once */
1488 ic->xfer_small = 0;
1489 ic->xfer_large++;
1490 if (ic->xfer_large >= 3) {
1491 /* we call this buffer a fast streamer if it manages
1492 * to be filled in one call 3 consecutive times.
1493 */
1494 ic->flags |= (CF_STREAMER | CF_STREAMER_FAST);
1495 }
1496 }
1497 else {
1498 ic->xfer_small = 0;
1499 ic->xfer_large = 0;
1500 }
1501 ic->last_read = now_ms;
Christopher Faulet4c135682023-02-16 11:09:31 +01001502 sc_ep_report_read_activity(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001503 }
1504
1505 end_recv:
1506 ret = (cur_read != 0);
1507
1508 /* Report EOI on the channel if it was reached from the mux point of
1509 * view. */
Christopher Faulet904763f2023-03-22 14:53:11 +01001510 if (sc_ep_test(sc, SE_FL_EOI) && !(sc->flags & SC_FL_EOI)) {
Christopher Faulet4c135682023-02-16 11:09:31 +01001511 sc_ep_report_read_activity(sc);
Christopher Faulet904763f2023-03-22 14:53:11 +01001512 sc->flags |= SC_FL_EOI;
1513 ic->flags |= CF_READ_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001514 ret = 1;
1515 }
1516
Christopher Fauletb208d8c2023-03-21 11:25:21 +01001517 if (sc_ep_test(sc, SE_FL_EOS)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001518 /* we received a shutdown */
Christopher Faulet5e29b762022-04-04 08:58:34 +02001519 if (ic->flags & CF_AUTO_CLOSE)
Christopher Fauletdf7cd712023-04-13 15:56:26 +02001520 sc_schedule_shutdown(sc_opposite(sc));
Christopher Faulet1aec6c92023-04-17 17:29:29 +02001521 sc_conn_eos(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001522 ret = 1;
1523 }
Christopher Fauletb208d8c2023-03-21 11:25:21 +01001524
Christopher Fauleta1d14a72023-04-14 10:42:08 +02001525 if (sc_ep_test(sc, SE_FL_ERROR)) {
1526 sc->flags |= SC_FL_ERROR;
Christopher Fauletb208d8c2023-03-21 11:25:21 +01001527 ret = 1;
Christopher Fauleta1d14a72023-04-14 10:42:08 +02001528 }
Willy Tarreau0adb2812022-05-27 10:02:48 +02001529 else if (!(sc->flags & (SC_FL_WONT_READ|SC_FL_NEED_BUFF|SC_FL_NEED_ROOM)) &&
Christopher Fauletca5309a2023-04-17 16:17:32 +02001530 !(sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001531 /* Subscribe to receive events if we're blocking on I/O */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001532 conn->mux->subscribe(sc, SUB_RETRY_RECV, &sc->wait_event);
1533 se_have_no_more_data(sc->sedesc);
Christopher Fauletb208d8c2023-03-21 11:25:21 +01001534 }
1535 else {
Willy Tarreau0adb2812022-05-27 10:02:48 +02001536 se_have_more_data(sc->sedesc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001537 ret = 1;
1538 }
Christopher Faulet8019f782023-03-23 17:30:29 +01001539
Christopher Faulet5e29b762022-04-04 08:58:34 +02001540 return ret;
1541}
1542
Willy Tarreau4596fe22022-05-17 19:07:51 +02001543/* This tries to perform a synchronous receive on the stream connector to
Christopher Faulet5e29b762022-04-04 08:58:34 +02001544 * try to collect last arrived data. In practice it's only implemented on
Willy Tarreau4596fe22022-05-17 19:07:51 +02001545 * stconns. Returns 0 if nothing was done, non-zero if new data or a
Christopher Faulet5e29b762022-04-04 08:58:34 +02001546 * shutdown were collected. This may result on some delayed receive calls
1547 * to be programmed and performed later, though it doesn't provide any
1548 * such guarantee.
1549 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001550int sc_conn_sync_recv(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001551{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001552 if (!sc_state_in(sc->state, SC_SB_RDY|SC_SB_EST))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001553 return 0;
1554
Willy Tarreau0adb2812022-05-27 10:02:48 +02001555 if (!sc_mux_ops(sc))
Willy Tarreau4596fe22022-05-17 19:07:51 +02001556 return 0; // only stconns are supported
Christopher Faulet5e29b762022-04-04 08:58:34 +02001557
Willy Tarreau0adb2812022-05-27 10:02:48 +02001558 if (sc->wait_event.events & SUB_RETRY_RECV)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001559 return 0; // already subscribed
1560
Willy Tarreau0adb2812022-05-27 10:02:48 +02001561 if (!sc_is_recv_allowed(sc))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001562 return 0; // already failed
1563
Willy Tarreau0adb2812022-05-27 10:02:48 +02001564 return sc_conn_recv(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001565}
1566
1567/*
1568 * This function is called to send buffer data to a stream socket.
1569 * It calls the mux layer's snd_buf function. It relies on the
1570 * caller to commit polling changes. The caller should check conn->flags
1571 * for errors.
1572 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001573static int sc_conn_send(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001574{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001575 struct connection *conn = __sc_conn(sc);
Christopher Faulet904763f2023-03-22 14:53:11 +01001576 struct stconn *sco = sc_opposite(sc);
Willy Tarreau0adb2812022-05-27 10:02:48 +02001577 struct stream *s = __sc_strm(sc);
1578 struct channel *oc = sc_oc(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001579 int ret;
1580 int did_send = 0;
1581
Willy Tarreau0adb2812022-05-27 10:02:48 +02001582 if (sc_ep_test(sc, SE_FL_ERROR | SE_FL_ERR_PENDING) || sc_is_conn_error(sc)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001583 /* We're probably there because the tasklet was woken up,
1584 * but process_stream() ran before, detected there were an
Willy Tarreaue68bc612022-05-27 11:23:05 +02001585 * error and put the SC back to SC_ST_TAR. There's still
Christopher Faulet5e29b762022-04-04 08:58:34 +02001586 * CO_FL_ERROR on the connection but we don't want to add
Willy Tarreaub605c422022-05-17 17:04:55 +02001587 * SE_FL_ERROR back, so give up
Christopher Faulet5e29b762022-04-04 08:58:34 +02001588 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001589 if (sc->state < SC_ST_CON)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001590 return 0;
Christopher Faulet56a2b602023-04-14 09:42:59 +02001591 BUG_ON(sc_ep_test(sc, SE_FL_EOS|SE_FL_ERROR|SE_FL_ERR_PENDING) == (SE_FL_EOS|SE_FL_ERR_PENDING));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001592 return 1;
1593 }
1594
1595 /* We're already waiting to be able to send, give up */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001596 if (sc->wait_event.events & SUB_RETRY_SEND)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001597 return 0;
1598
1599 /* we might have been called just after an asynchronous shutw */
Christopher Faulet208c7122023-04-13 16:16:15 +02001600 if (sc->flags & SC_FL_SHUT_DONE)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001601 return 1;
1602
1603 /* we must wait because the mux is not installed yet */
1604 if (!conn->mux)
1605 return 0;
1606
1607 if (oc->pipe && conn->xprt->snd_pipe && conn->mux->snd_pipe) {
Willy Tarreau0adb2812022-05-27 10:02:48 +02001608 ret = conn->mux->snd_pipe(sc, oc->pipe);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001609 if (ret > 0)
1610 did_send = 1;
1611
1612 if (!oc->pipe->data) {
1613 put_pipe(oc->pipe);
1614 oc->pipe = NULL;
1615 }
1616
1617 if (oc->pipe)
1618 goto end;
1619 }
1620
1621 /* At this point, the pipe is empty, but we may still have data pending
1622 * in the normal buffer.
1623 */
1624 if (co_data(oc)) {
1625 /* when we're here, we already know that there is no spliced
1626 * data left, and that there are sendable buffered data.
1627 */
1628
1629 /* check if we want to inform the kernel that we're interested in
1630 * sending more data after this call. We want this if :
1631 * - we're about to close after this last send and want to merge
1632 * the ongoing FIN with the last segment.
1633 * - we know we can't send everything at once and must get back
1634 * here because of unaligned data
1635 * - there is still a finite amount of data to forward
1636 * The test is arranged so that the most common case does only 2
1637 * tests.
1638 */
1639 unsigned int send_flag = 0;
1640
Christopher Faulet68ef2182023-03-17 15:38:18 +01001641 if ((!(sc->flags & (SC_FL_SND_ASAP|SC_FL_SND_NEVERWAIT)) &&
Christopher Faulet5e29b762022-04-04 08:58:34 +02001642 ((oc->to_forward && oc->to_forward != CHN_INFINITE_FORWARD) ||
Christopher Faulet84d3ef92023-03-17 15:45:58 +01001643 (sc->flags & SC_FL_SND_EXP_MORE) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001644 (IS_HTX_STRM(s) &&
Christopher Fauletca5309a2023-04-17 16:17:32 +02001645 (!(sco->flags & (SC_FL_EOI|SC_FL_EOS|SC_FL_ABRT_DONE)) && htx_expect_more(htxbuf(&oc->buf)))))) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001646 ((oc->flags & CF_ISRESP) &&
Christopher Faulet87633c32023-04-03 18:32:50 +02001647 (oc->flags & CF_AUTO_CLOSE) &&
Christopher Faulete38534c2023-04-13 15:45:24 +02001648 (sc->flags & SC_FL_SHUT_WANTED)))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001649 send_flag |= CO_SFL_MSG_MORE;
1650
1651 if (oc->flags & CF_STREAMER)
1652 send_flag |= CO_SFL_STREAMER;
1653
1654 if (s->txn && s->txn->flags & TX_L7_RETRY && !b_data(&s->txn->l7_buffer)) {
1655 /* If we want to be able to do L7 retries, copy
1656 * the data we're about to send, so that we are able
1657 * to resend them if needed
1658 */
1659 /* Try to allocate a buffer if we had none.
1660 * If it fails, the next test will just
1661 * disable the l7 retries by setting
1662 * l7_conn_retries to 0.
1663 */
1664 if (s->txn->req.msg_state != HTTP_MSG_DONE)
1665 s->txn->flags &= ~TX_L7_RETRY;
1666 else {
1667 if (b_alloc(&s->txn->l7_buffer) == NULL)
1668 s->txn->flags &= ~TX_L7_RETRY;
1669 else {
1670 memcpy(b_orig(&s->txn->l7_buffer),
1671 b_orig(&oc->buf),
1672 b_size(&oc->buf));
1673 s->txn->l7_buffer.head = co_data(oc);
1674 b_add(&s->txn->l7_buffer, co_data(oc));
1675 }
1676
1677 }
1678 }
1679
Willy Tarreau0adb2812022-05-27 10:02:48 +02001680 ret = conn->mux->snd_buf(sc, &oc->buf, co_data(oc), send_flag);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001681 if (ret > 0) {
1682 did_send = 1;
1683 c_rew(oc, ret);
1684 c_realign_if_empty(oc);
1685
1686 if (!co_data(oc)) {
1687 /* Always clear both flags once everything has been sent, they're one-shot */
Christopher Faulet84d3ef92023-03-17 15:45:58 +01001688 sc->flags &= ~(SC_FL_SND_ASAP|SC_FL_SND_EXP_MORE);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001689 }
1690 /* if some data remain in the buffer, it's only because the
1691 * system buffers are full, we will try next time.
1692 */
Christopher Faulet13045f02022-04-01 14:23:38 +02001693 }
1694 }
Christopher Faulet5e29b762022-04-04 08:58:34 +02001695
1696 end:
1697 if (did_send) {
Christopher Fauletd8988412022-12-20 18:10:04 +01001698 oc->flags |= CF_WRITE_EVENT | CF_WROTE_DATA;
Willy Tarreau0adb2812022-05-27 10:02:48 +02001699 if (sc->state == SC_ST_CON)
1700 sc->state = SC_ST_RDY;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001701 }
1702
Christopher Faulete7405d42023-05-05 11:40:30 +02001703 if (!sco->room_needed || (did_send && (sco->room_needed < 0 || channel_recv_max(sc_oc(sc)) >= sco->room_needed)))
1704 sc_have_room(sco);
1705
Willy Tarreau0adb2812022-05-27 10:02:48 +02001706 if (sc_ep_test(sc, SE_FL_ERROR | SE_FL_ERR_PENDING)) {
Christopher Faulet2e56a732023-01-26 16:18:09 +01001707 oc->flags |= CF_WRITE_EVENT;
Christopher Faulet56a2b602023-04-14 09:42:59 +02001708 BUG_ON(sc_ep_test(sc, SE_FL_EOS|SE_FL_ERROR|SE_FL_ERR_PENDING) == (SE_FL_EOS|SE_FL_ERR_PENDING));
Christopher Fauletd0c57d32023-04-18 18:38:32 +02001709 if (sc_ep_test(sc, SE_FL_ERROR))
1710 sc->flags |= SC_FL_ERROR;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001711 return 1;
1712 }
1713
Christopher Faulete65daba2023-11-15 17:33:06 +01001714 if (channel_is_empty(oc)) {
1715 sc_ep_report_send_activity(sc);
1716 }
1717 else {
Christopher Faulet59b240c2023-02-27 16:38:12 +01001718 /* We couldn't send all of our data, let the mux know we'd like to send more */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001719 conn->mux->subscribe(sc, SUB_RETRY_SEND, &sc->wait_event);
Christopher Faulete65daba2023-11-15 17:33:06 +01001720 if (sc_state_in(sc->state, SC_SB_EST|SC_SB_DIS|SC_SB_CLO))
1721 sc_ep_report_blocked_send(sc, did_send);
Christopher Faulet59b240c2023-02-27 16:38:12 +01001722 }
1723
Christopher Faulet5e29b762022-04-04 08:58:34 +02001724 return did_send;
1725}
1726
Christopher Fauletd8988412022-12-20 18:10:04 +01001727/* perform a synchronous send() for the stream connector. The CF_WRITE_EVENT
1728 * flag are cleared prior to the attempt, and will possibly be updated in case
1729 * of success.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001730 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001731void sc_conn_sync_send(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001732{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001733 struct channel *oc = sc_oc(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001734
Christopher Fauletd8988412022-12-20 18:10:04 +01001735 oc->flags &= ~CF_WRITE_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001736
Christopher Faulet208c7122023-04-13 16:16:15 +02001737 if (sc->flags & SC_FL_SHUT_DONE)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001738 return;
1739
1740 if (channel_is_empty(oc))
1741 return;
1742
Willy Tarreau0adb2812022-05-27 10:02:48 +02001743 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001744 return;
1745
Willy Tarreau0adb2812022-05-27 10:02:48 +02001746 if (!sc_mux_ops(sc))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001747 return;
1748
Willy Tarreau0adb2812022-05-27 10:02:48 +02001749 sc_conn_send(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001750}
1751
1752/* Called by I/O handlers after completion.. It propagates
Willy Tarreau4596fe22022-05-17 19:07:51 +02001753 * connection flags to the stream connector, updates the stream (which may or
Christopher Faulet5e29b762022-04-04 08:58:34 +02001754 * may not take this opportunity to try to forward data), then update the
Willy Tarreau4596fe22022-05-17 19:07:51 +02001755 * connection's polling based on the channels and stream connector's final
Christopher Faulet5e29b762022-04-04 08:58:34 +02001756 * states. The function always returns 0.
1757 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001758static int sc_conn_process(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001759{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001760 struct connection *conn = __sc_conn(sc);
1761 struct channel *ic = sc_ic(sc);
1762 struct channel *oc = sc_oc(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001763
1764 BUG_ON(!conn);
1765
1766 /* If we have data to send, try it now */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001767 if (!channel_is_empty(oc) && !(sc->wait_event.events & SUB_RETRY_SEND))
1768 sc_conn_send(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001769
Willy Tarreau4596fe22022-05-17 19:07:51 +02001770 /* First step, report to the stream connector what was detected at the
Christopher Faulet5e29b762022-04-04 08:58:34 +02001771 * connection layer : errors and connection establishment.
Christopher Faulet88d05a02023-04-14 12:03:50 +02001772 * Only add SC_FL_ERROR if we're connected, or we're attempting to
Christopher Faulet5e29b762022-04-04 08:58:34 +02001773 * connect, we may get there because we got woken up, but only run
1774 * after process_stream() noticed there were an error, and decided
1775 * to retry to connect, the connection may still have CO_FL_ERROR,
Christopher Faulet88d05a02023-04-14 12:03:50 +02001776 * and we don't want to add SC_FL_ERROR back
Christopher Faulet5e29b762022-04-04 08:58:34 +02001777 *
Willy Tarreau462b9892022-05-18 18:06:53 +02001778 * Note: This test is only required because sc_conn_process is also the SI
1779 * wake callback. Otherwise sc_conn_recv()/sc_conn_send() already take
Christopher Faulet5e29b762022-04-04 08:58:34 +02001780 * care of it.
1781 */
1782
Willy Tarreau0adb2812022-05-27 10:02:48 +02001783 if (sc->state >= SC_ST_CON) {
Christopher Faulet88d05a02023-04-14 12:03:50 +02001784 if (sc_is_conn_error(sc))
Christopher Fauleta1d14a72023-04-14 10:42:08 +02001785 sc->flags |= SC_FL_ERROR;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001786 }
1787
1788 /* If we had early data, and the handshake ended, then
1789 * we can remove the flag, and attempt to wake the task up,
1790 * in the event there's an analyser waiting for the end of
1791 * the handshake.
1792 */
1793 if (!(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS)) &&
Willy Tarreau0adb2812022-05-27 10:02:48 +02001794 sc_ep_test(sc, SE_FL_WAIT_FOR_HS)) {
1795 sc_ep_clr(sc, SE_FL_WAIT_FOR_HS);
1796 task_wakeup(sc_strm_task(sc), TASK_WOKEN_MSG);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001797 }
1798
Willy Tarreau0adb2812022-05-27 10:02:48 +02001799 if (!sc_state_in(sc->state, SC_SB_EST|SC_SB_DIS|SC_SB_CLO) &&
Christopher Faulet5e29b762022-04-04 08:58:34 +02001800 (conn->flags & CO_FL_WAIT_XPRT) == 0) {
Christopher Fauletca679922022-07-20 13:24:04 +02001801 if (sc->flags & SC_FL_ISBACK)
1802 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Fauletb96f2aa2022-12-12 08:11:36 +01001803 oc->flags |= CF_WRITE_EVENT;
Willy Tarreau0adb2812022-05-27 10:02:48 +02001804 if (sc->state == SC_ST_CON)
1805 sc->state = SC_ST_RDY;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001806 }
1807
1808 /* Report EOS on the channel if it was reached from the mux point of
1809 * view.
1810 *
Willy Tarreau462b9892022-05-18 18:06:53 +02001811 * Note: This test is only required because sc_conn_process is also the SI
1812 * wake callback. Otherwise sc_conn_recv()/sc_conn_send() already take
Christopher Faulet5e29b762022-04-04 08:58:34 +02001813 * care of it.
1814 */
Christopher Faulet1aec6c92023-04-17 17:29:29 +02001815 if (sc_ep_test(sc, SE_FL_EOS) && !(sc->flags & SC_FL_EOS)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001816 /* we received a shutdown */
Christopher Faulet5e29b762022-04-04 08:58:34 +02001817 if (ic->flags & CF_AUTO_CLOSE)
Christopher Fauletdf7cd712023-04-13 15:56:26 +02001818 sc_schedule_shutdown(sc_opposite(sc));
Christopher Faulet1aec6c92023-04-17 17:29:29 +02001819 sc_conn_eos(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001820 }
1821
1822 /* Report EOI on the channel if it was reached from the mux point of
1823 * view.
1824 *
Willy Tarreau462b9892022-05-18 18:06:53 +02001825 * Note: This test is only required because sc_conn_process is also the SI
1826 * wake callback. Otherwise sc_conn_recv()/sc_conn_send() already take
Christopher Faulet5e29b762022-04-04 08:58:34 +02001827 * care of it.
1828 */
Christopher Faulet904763f2023-03-22 14:53:11 +01001829 if (sc_ep_test(sc, SE_FL_EOI) && !(sc->flags & SC_FL_EOI)) {
1830 sc->flags |= SC_FL_EOI;
1831 ic->flags |= CF_READ_EVENT;
Christopher Faulet2bb2ee22023-11-07 07:45:43 +01001832 sc_ep_report_read_activity(sc);
Christopher Faulet904763f2023-03-22 14:53:11 +01001833 }
Christopher Faulet5e29b762022-04-04 08:58:34 +02001834
Christopher Fauleta1d14a72023-04-14 10:42:08 +02001835 if (sc_ep_test(sc, SE_FL_ERROR))
1836 sc->flags |= SC_FL_ERROR;
1837
Willy Tarreau4596fe22022-05-17 19:07:51 +02001838 /* Second step : update the stream connector and channels, try to forward any
Christopher Faulet5e29b762022-04-04 08:58:34 +02001839 * pending data, then possibly wake the stream up based on the new
Willy Tarreau4596fe22022-05-17 19:07:51 +02001840 * stream connector status.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001841 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001842 sc_notify(sc);
1843 stream_release_buffers(__sc_strm(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001844 return 0;
1845}
1846
Willy Tarreau4596fe22022-05-17 19:07:51 +02001847/* This is the ->process() function for any stream connector's wait_event task.
1848 * It's assigned during the stream connector's initialization, for any type of
1849 * stream connector. Thus it is always safe to perform a tasklet_wakeup() on a
Willy Tarreaue68bc612022-05-27 11:23:05 +02001850 * stream connector, as the presence of the SC is checked there.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001851 */
Willy Tarreau462b9892022-05-18 18:06:53 +02001852struct task *sc_conn_io_cb(struct task *t, void *ctx, unsigned int state)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001853{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001854 struct stconn *sc = ctx;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001855 int ret = 0;
1856
Willy Tarreau0adb2812022-05-27 10:02:48 +02001857 if (!sc_conn(sc))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001858 return t;
1859
Willy Tarreau0adb2812022-05-27 10:02:48 +02001860 if (!(sc->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(sc_oc(sc)))
1861 ret = sc_conn_send(sc);
1862 if (!(sc->wait_event.events & SUB_RETRY_RECV))
1863 ret |= sc_conn_recv(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001864 if (ret != 0)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001865 sc_conn_process(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001866
Willy Tarreau0adb2812022-05-27 10:02:48 +02001867 stream_release_buffers(__sc_strm(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001868 return t;
1869}
1870
Christopher Fauletb36e5122023-04-17 17:32:43 +02001871/*
1872 * This function propagates an end-of-stream received from an applet. It
1873 * updates the stream connector. If it is is already shut, the applet is
1874 * released. Otherwise, we try to forward the shutdown, immediately or ASAP.
1875 */
1876static void sc_applet_eos(struct stconn *sc)
1877{
1878 struct channel *ic = sc_ic(sc);
1879
1880 BUG_ON(!sc_appctx(sc));
1881
1882 if (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))
1883 return;
1884 sc->flags |= SC_FL_EOS;
1885 ic->flags |= CF_READ_EVENT;
Christopher Faulet2bb2ee22023-11-07 07:45:43 +01001886 sc_ep_report_read_activity(sc);
Christopher Fauletb36e5122023-04-17 17:32:43 +02001887
1888 /* Note: on abort, we don't call the applet */
1889
1890 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
1891 return;
1892
1893 if (sc->flags & SC_FL_SHUT_DONE) {
1894 appctx_shut(__sc_appctx(sc));
1895 sc->state = SC_ST_DIS;
1896 if (sc->flags & SC_FL_ISBACK)
1897 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
1898 }
1899 else if (sc_cond_forward_shut(sc))
1900 return sc_app_shut_applet(sc);
1901}
1902
Christopher Faulet5e29b762022-04-04 08:58:34 +02001903/* Callback to be used by applet handlers upon completion. It updates the stream
1904 * (which may or may not take this opportunity to try to forward data), then
Willy Tarreau4596fe22022-05-17 19:07:51 +02001905 * may re-enable the applet's based on the channels and stream connector's final
Christopher Faulet5e29b762022-04-04 08:58:34 +02001906 * states.
1907 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001908static int sc_applet_process(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001909{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001910 struct channel *ic = sc_ic(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001911
Willy Tarreau0adb2812022-05-27 10:02:48 +02001912 BUG_ON(!sc_appctx(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001913
Christopher Fauletf8fbb6d2023-03-21 11:49:21 +01001914 /* Report EOI on the channel if it was reached from the applet point of
1915 * view. */
Christopher Faulet904763f2023-03-22 14:53:11 +01001916 if (sc_ep_test(sc, SE_FL_EOI) && !(sc->flags & SC_FL_EOI)) {
Christopher Fauletf8fbb6d2023-03-21 11:49:21 +01001917 sc_ep_report_read_activity(sc);
Christopher Faulet904763f2023-03-22 14:53:11 +01001918 sc->flags |= SC_FL_EOI;
1919 ic->flags |= CF_READ_EVENT;
Christopher Fauletf8fbb6d2023-03-21 11:49:21 +01001920 }
1921
Christopher Fauleta1d14a72023-04-14 10:42:08 +02001922 if (sc_ep_test(sc, SE_FL_ERROR))
1923 sc->flags |= SC_FL_ERROR;
1924
Christopher Faulet0ffc9d72023-03-21 14:19:08 +01001925 if (sc_ep_test(sc, SE_FL_EOS)) {
1926 /* we received a shutdown */
Christopher Fauletb36e5122023-04-17 17:32:43 +02001927 sc_applet_eos(sc);
Christopher Faulet0ffc9d72023-03-21 14:19:08 +01001928 }
1929
Christopher Faulete8bcef52023-04-14 09:45:41 +02001930 BUG_ON(sc_ep_test(sc, SE_FL_HAVE_NO_DATA|SE_FL_EOI) == SE_FL_EOI);
1931
Christopher Faulet5e29b762022-04-04 08:58:34 +02001932 /* If the applet wants to write and the channel is closed, it's a
1933 * broken pipe and it must be reported.
1934 */
Christopher Fauletca5309a2023-04-17 16:17:32 +02001935 if (!sc_ep_test(sc, SE_FL_HAVE_NO_DATA) && (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)))
Willy Tarreau0adb2812022-05-27 10:02:48 +02001936 sc_ep_set(sc, SE_FL_ERROR);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001937
1938 /* automatically mark the applet having data available if it reported
1939 * begin blocked by the channel.
1940 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001941 if ((sc->flags & (SC_FL_WONT_READ|SC_FL_NEED_BUFF|SC_FL_NEED_ROOM)) ||
1942 sc_ep_test(sc, SE_FL_APPLET_NEED_CONN))
1943 applet_have_more_data(__sc_appctx(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001944
Willy Tarreau4596fe22022-05-17 19:07:51 +02001945 /* update the stream connector, channels, and possibly wake the stream up */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001946 sc_notify(sc);
1947 stream_release_buffers(__sc_strm(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001948
Willy Tarreau19c65a92022-05-27 08:49:24 +02001949 /* sc_notify may have passed through chk_snd and released some blocking
Willy Tarreau15252cd2022-05-25 16:36:21 +02001950 * flags. Process_stream will consider those flags to wake up the
Christopher Faulet5e29b762022-04-04 08:58:34 +02001951 * appctx but in the case the task is not in runqueue we may have to
1952 * wakeup the appctx immediately.
1953 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001954 if (sc_is_recv_allowed(sc) || sc_is_send_allowed(sc))
1955 appctx_wakeup(__sc_appctx(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001956 return 0;
Christopher Faulet13045f02022-04-01 14:23:38 +02001957}
Christopher Fauletb68f77d2022-06-16 16:24:16 +02001958
1959
1960/* Prepares an endpoint upgrade. We don't now at this stage if the upgrade will
1961 * succeed or not and if the stconn will be reused by the new endpoint. Thus,
1962 * for now, only pretend the stconn is detached.
1963 */
1964void sc_conn_prepare_endp_upgrade(struct stconn *sc)
1965{
1966 BUG_ON(!sc_conn(sc) || !sc->app);
1967 sc_ep_clr(sc, SE_FL_T_MUX);
1968 sc_ep_set(sc, SE_FL_DETACHED);
1969}
1970
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001971/* Endpoint upgrade failed. Restore the stconn state. */
Christopher Fauletb68f77d2022-06-16 16:24:16 +02001972void sc_conn_abort_endp_upgrade(struct stconn *sc)
1973{
1974 sc_ep_set(sc, SE_FL_T_MUX);
1975 sc_ep_clr(sc, SE_FL_DETACHED);
1976}
1977
1978/* Commit the endpoint upgrade. If stconn is attached, it means the new endpoint
1979 * use it. So we do nothing. Otherwise, the stconn will be destroy with the
1980 * overlying stream. So, it means we must commit the detach.
1981*/
1982void sc_conn_commit_endp_upgrade(struct stconn *sc)
1983{
1984 if (!sc_ep_test(sc, SE_FL_DETACHED))
1985 return;
1986 sc_detach_endp(&sc);
1987 /* Because it was already set as detached, the sedesc must be preserved */
Willy Tarreau6a378d12022-08-11 13:56:42 +02001988 BUG_ON(!sc);
Christopher Fauletb68f77d2022-06-16 16:24:16 +02001989 BUG_ON(!sc->sedesc);
1990}