blob: 8bffba73df0ab4aaa0d5552bbbb728c71cc2a520 [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);
325 if (sc_ep_test(sc, SE_FL_T_MUX)) {
326 sc->wait_event.tasklet = tasklet_new();
327 if (!sc->wait_event.tasklet)
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200328 return -1;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200329 sc->wait_event.tasklet->process = sc_conn_io_cb;
330 sc->wait_event.tasklet->context = sc;
331 sc->wait_event.events = 0;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200332
Willy Tarreau0adb2812022-05-27 10:02:48 +0200333 sc->app_ops = &sc_app_conn_ops;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100334 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200335 else if (sc_ep_test(sc, SE_FL_T_APPLET)) {
336 sc->app_ops = &sc_app_applet_ops;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100337 }
338 else {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200339 sc->app_ops = &sc_app_embedded_ops;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100340 }
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100341 return 0;
342}
343
Willy Tarreau4596fe22022-05-17 19:07:51 +0200344/* Detaches the stconn from the endpoint, if any. For a connecrion, if a
Christopher Faulet9ed77422022-04-12 08:51:15 +0200345 * mux owns the connection ->detach() callback is called. Otherwise, it means
Willy Tarreau4596fe22022-05-17 19:07:51 +0200346 * the stream connector owns the connection. In this case the connection is closed
Christopher Faulet9ed77422022-04-12 08:51:15 +0200347 * and released. For an applet, the appctx is released. If still allocated, the
348 * endpoint is reset and flag as detached. If the app layer is also detached,
Willy Tarreau4596fe22022-05-17 19:07:51 +0200349 * the stream connector is released.
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100350 */
Willy Tarreaue68bc612022-05-27 11:23:05 +0200351static void sc_detach_endp(struct stconn **scp)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100352{
Willy Tarreaue68bc612022-05-27 11:23:05 +0200353 struct stconn *sc = *scp;
Christopher Faulet6eb53b12023-05-15 09:53:29 +0200354 struct xref *peer;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200355
Willy Tarreau0adb2812022-05-27 10:02:48 +0200356 if (!sc)
Christopher Fauleteb50c012022-04-21 14:22:53 +0200357 return;
358
Christopher Faulet7542fb42023-05-11 14:40:27 +0200359
Christopher Faulet6eb53b12023-05-15 09:53:29 +0200360 /* Remove my link in the original objects. */
361 peer = xref_get_peer_and_lock(&sc->sedesc->xref);
362 if (peer)
363 xref_disconnect(&sc->sedesc->xref, peer);
Christopher Faulet7542fb42023-05-11 14:40:27 +0200364
Willy Tarreau0adb2812022-05-27 10:02:48 +0200365 if (sc_ep_test(sc, SE_FL_T_MUX)) {
366 struct connection *conn = __sc_conn(sc);
367 struct sedesc *sedesc = sc->sedesc;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100368
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100369 if (conn->mux) {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200370 if (sc->wait_event.events != 0)
371 conn->mux->unsubscribe(sc, sc->wait_event.events, &sc->wait_event);
Willy Tarreau798465b2022-05-17 18:20:02 +0200372 se_fl_set(sedesc, SE_FL_ORPHAN);
Willy Tarreauc1054922022-05-18 07:43:52 +0200373 sedesc->sc = NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200374 sc->sedesc = NULL;
Willy Tarreau798465b2022-05-17 18:20:02 +0200375 conn->mux->detach(sedesc);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100376 }
377 else {
378 /* It's too early to have a mux, let's just destroy
379 * the connection
380 */
381 conn_stop_tracking(conn);
382 conn_full_close(conn);
383 if (conn->destroy_cb)
384 conn->destroy_cb(conn);
385 conn_free(conn);
386 }
387 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200388 else if (sc_ep_test(sc, SE_FL_T_APPLET)) {
389 struct appctx *appctx = __sc_appctx(sc);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100390
Willy Tarreau0adb2812022-05-27 10:02:48 +0200391 sc_ep_set(sc, SE_FL_ORPHAN);
392 sc->sedesc->sc = NULL;
393 sc->sedesc = NULL;
Willy Tarreau1c3ead42022-05-10 19:42:22 +0200394 appctx_shut(appctx);
395 appctx_free(appctx);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100396 }
397
Willy Tarreau0adb2812022-05-27 10:02:48 +0200398 if (sc->sedesc) {
Willy Tarreauda59c892022-05-27 17:03:34 +0200399 /* the SD wasn't used and can be recycled */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200400 sc->sedesc->se = NULL;
401 sc->sedesc->conn = NULL;
Willy Tarreauda59c892022-05-27 17:03:34 +0200402 sc->sedesc->flags = 0;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200403 sc_ep_set(sc, SE_FL_DETACHED);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100404 }
405
Willy Tarreaue68bc612022-05-27 11:23:05 +0200406 /* FIXME: Rest SC for now but must be reviewed. SC flags are only
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100407 * connection related for now but this will evolved
408 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200409 sc->flags &= SC_FL_ISBACK;
410 if (sc_strm(sc))
411 sc->app_ops = &sc_app_embedded_ops;
Willy Tarreau2f2318d2022-05-18 10:17:16 +0200412 else
Willy Tarreau0adb2812022-05-27 10:02:48 +0200413 sc->app_ops = NULL;
Willy Tarreaue68bc612022-05-27 11:23:05 +0200414 sc_free_cond(scp);
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100415}
416
Willy Tarreau4596fe22022-05-17 19:07:51 +0200417/* Detaches the stconn from the app layer. If there is no endpoint attached
418 * to the stconn
Christopher Faulet9ed77422022-04-12 08:51:15 +0200419 */
Willy Tarreaue68bc612022-05-27 11:23:05 +0200420static void sc_detach_app(struct stconn **scp)
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100421{
Willy Tarreaue68bc612022-05-27 11:23:05 +0200422 struct stconn *sc = *scp;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200423
Willy Tarreau0adb2812022-05-27 10:02:48 +0200424 if (!sc)
Christopher Fauleteb50c012022-04-21 14:22:53 +0200425 return;
426
Willy Tarreau0adb2812022-05-27 10:02:48 +0200427 sc->app = NULL;
428 sc->app_ops = NULL;
429 sockaddr_free(&sc->src);
430 sockaddr_free(&sc->dst);
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200431
Tim Duesterhusb1ec21d2023-04-22 17:47:32 +0200432 tasklet_free(sc->wait_event.tasklet);
Willy Tarreau0adb2812022-05-27 10:02:48 +0200433 sc->wait_event.tasklet = NULL;
434 sc->wait_event.events = 0;
Willy Tarreaue68bc612022-05-27 11:23:05 +0200435 sc_free_cond(scp);
Christopher Fauleteb50c012022-04-21 14:22:53 +0200436}
437
Willy Tarreau4596fe22022-05-17 19:07:51 +0200438/* Destroy the stconn. It is detached from its endpoint and its
439 * application. After this call, the stconn must be considered as released.
Christopher Fauleteb50c012022-04-21 14:22:53 +0200440 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200441void sc_destroy(struct stconn *sc)
Christopher Fauleteb50c012022-04-21 14:22:53 +0200442{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200443 sc_detach_endp(&sc);
444 sc_detach_app(&sc);
445 BUG_ON_HOT(sc);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100446}
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100447
Willy Tarreau4596fe22022-05-17 19:07:51 +0200448/* Resets the stream connector endpoint. It happens when the app layer want to renew
Christopher Faulet9ed77422022-04-12 08:51:15 +0200449 * its endpoint. For a connection retry for instance. If a mux or an applet is
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500450 * attached, a new endpoint is created. Returns -1 on error and 0 on success.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200451 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200452int sc_reset_endp(struct stconn *sc)
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100453{
Willy Tarreau31219282022-05-27 16:21:33 +0200454 struct sedesc *new_sd;
Christopher Fauletb041b232022-03-24 10:27:02 +0100455
Willy Tarreau0adb2812022-05-27 10:02:48 +0200456 BUG_ON(!sc->app);
Christopher Fauleta6c4a482022-04-28 18:25:24 +0200457
Willy Tarreau0adb2812022-05-27 10:02:48 +0200458 if (!__sc_endp(sc)) {
Christopher Fauletb041b232022-03-24 10:27:02 +0100459 /* endpoint not attached or attached to a mux with no
460 * target. Thus the endpoint will not be release but just
Willy Tarreau0adb2812022-05-27 10:02:48 +0200461 * reset. The app is still attached, the sc will not be
Christopher Fauleteb50c012022-04-21 14:22:53 +0200462 * released.
Christopher Fauletb041b232022-03-24 10:27:02 +0100463 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200464 sc_detach_endp(&sc);
Christopher Fauletb041b232022-03-24 10:27:02 +0100465 return 0;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100466 }
Christopher Fauletb041b232022-03-24 10:27:02 +0100467
468 /* allocate the new endpoint first to be able to set error if it
469 * fails */
Willy Tarreau31219282022-05-27 16:21:33 +0200470 new_sd = sedesc_new();
Christopher Faulet638fe6a2023-04-14 10:28:28 +0200471 if (!unlikely(new_sd))
Christopher Fauletb041b232022-03-24 10:27:02 +0100472 return -1;
Christopher Fauletb041b232022-03-24 10:27:02 +0100473
Willy Tarreau0adb2812022-05-27 10:02:48 +0200474 /* The app is still attached, the sc will not be released */
475 sc_detach_endp(&sc);
Willy Tarreau6a378d12022-08-11 13:56:42 +0200476 BUG_ON(!sc);
Willy Tarreau0adb2812022-05-27 10:02:48 +0200477 BUG_ON(sc->sedesc);
Willy Tarreau31219282022-05-27 16:21:33 +0200478 sc->sedesc = new_sd;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200479 sc->sedesc->sc = sc;
480 sc_ep_set(sc, SE_FL_DETACHED);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100481 return 0;
482}
Christopher Faulet37046632022-04-01 11:36:58 +0200483
484
Willy Tarreaue68bc612022-05-27 11:23:05 +0200485/* Create an applet to handle a stream connector as a new appctx. The SC will
Christopher Faulet37046632022-04-01 11:36:58 +0200486 * wake it up every time it is solicited. The appctx must be deleted by the task
Willy Tarreau19c65a92022-05-27 08:49:24 +0200487 * handler using sc_detach_endp(), possibly from within the function itself.
Christopher Faulet37046632022-04-01 11:36:58 +0200488 * It also pre-initializes the applet's context and returns it (or NULL in case
489 * it could not be allocated).
490 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200491struct appctx *sc_applet_create(struct stconn *sc, struct applet *app)
Christopher Faulet37046632022-04-01 11:36:58 +0200492{
493 struct appctx *appctx;
494
Willy Tarreau0adb2812022-05-27 10:02:48 +0200495 appctx = appctx_new_here(app, sc->sedesc);
Christopher Faulet37046632022-04-01 11:36:58 +0200496 if (!appctx)
497 return NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200498 sc_attach_applet(sc, appctx);
499 appctx->t->nice = __sc_strm(sc)->task->nice;
Willy Tarreau90e8b452022-05-25 18:21:43 +0200500 applet_need_more_data(appctx);
Christopher Faulet37046632022-04-01 11:36:58 +0200501 appctx_wakeup(appctx);
Christopher Fauleta33ff7a2022-04-21 11:52:07 +0200502
Willy Tarreau0adb2812022-05-27 10:02:48 +0200503 sc->state = SC_ST_RDY;
Christopher Faulet37046632022-04-01 11:36:58 +0200504 return appctx;
505}
506
Ilya Shipitsin07be66d2023-04-01 12:26:42 +0200507/* Conditionally forward the close to the write side. It return 1 if it can be
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100508 * forwarded. It is the caller responsibility to forward the close to the write
Christopher Faulete38534c2023-04-13 15:45:24 +0200509 * side. Otherwise, 0 is returned. In this case, SC_FL_SHUT_WANTED flag may be set on
Christopher Faulet87633c32023-04-03 18:32:50 +0200510 * the consumer SC if we are only waiting for the outgoing data to be flushed.
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100511 */
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +0200512static inline int sc_cond_forward_shut(struct stconn *sc)
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100513{
514 /* The close must not be forwarded */
Christopher Fauletca5309a2023-04-17 16:17:32 +0200515 if (!(sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)) || !(sc->flags & SC_FL_NOHALF))
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100516 return 0;
517
518 if (!channel_is_empty(sc_ic(sc))) {
Christopher Fauletdf7cd712023-04-13 15:56:26 +0200519 /* the shutdown cannot be forwarded now because
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100520 * we should flush outgoing data first. But instruct the output
521 * channel it should be done ASAP.
522 */
Christopher Fauletdf7cd712023-04-13 15:56:26 +0200523 sc_schedule_shutdown(sc);
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100524 return 0;
525 }
526
527 /* the close can be immediately forwarded to the write side */
528 return 1;
529}
530
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200531/*
Willy Tarreau4596fe22022-05-17 19:07:51 +0200532 * This function performs a shutdown-read on a detached stream connector in a
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200533 * connected or init state (it does nothing for other states). It either shuts
534 * the read side or marks itself as closed. The buffer flags are updated to
Willy Tarreaucb041662022-05-17 19:44:42 +0200535 * reflect the new state. If the stream connector has SC_FL_NOHALF, we also
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200536 * forward the close to the write side. The owner task is woken up if it exists.
537 */
Christopher Fauletcfc11c02023-04-13 16:10:23 +0200538static void sc_app_abort(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200539{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200540 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200541
Christopher Fauletca5309a2023-04-17 16:17:32 +0200542 if (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))
Christopher Fauletc665bb52023-04-04 10:06:57 +0200543 return;
Christopher Faulet87633c32023-04-03 18:32:50 +0200544
Christopher Faulet0c370ee2023-04-13 16:05:13 +0200545 sc->flags |= SC_FL_ABRT_DONE;
Christopher Faulet87633c32023-04-03 18:32:50 +0200546 ic->flags |= CF_READ_EVENT;
Christopher Faulet4c135682023-02-16 11:09:31 +0100547 sc_ep_report_read_activity(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200548
Willy Tarreau0adb2812022-05-27 10:02:48 +0200549 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200550 return;
551
Christopher Faulet208c7122023-04-13 16:16:15 +0200552 if (sc->flags & SC_FL_SHUT_DONE) {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200553 sc->state = SC_ST_DIS;
Christopher Fauletca679922022-07-20 13:24:04 +0200554 if (sc->flags & SC_FL_ISBACK)
555 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200556 }
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +0200557 else if (sc_cond_forward_shut(sc))
558 return sc_app_shut(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200559
560 /* note that if the task exists, it must unregister itself once it runs */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200561 if (!(sc->flags & SC_FL_DONT_WAKE))
562 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200563}
564
565/*
Willy Tarreau4596fe22022-05-17 19:07:51 +0200566 * This function performs a shutdown-write on a detached stream connector in a
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200567 * connected or init state (it does nothing for other states). It either shuts
568 * the write side or marks itself as closed. The buffer flags are updated to
Willy Tarreaue68bc612022-05-27 11:23:05 +0200569 * reflect the new state. It does also close everything if the SC was marked as
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200570 * being in error state. The owner task is woken up if it exists.
571 */
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +0200572static void sc_app_shut(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200573{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200574 struct channel *ic = sc_ic(sc);
575 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200576
Christopher Faulete38534c2023-04-13 15:45:24 +0200577 sc->flags &= ~SC_FL_SHUT_WANTED;
Christopher Faulet208c7122023-04-13 16:16:15 +0200578 if (sc->flags & SC_FL_SHUT_DONE)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200579 return;
Christopher Faulet208c7122023-04-13 16:16:15 +0200580 sc->flags |= SC_FL_SHUT_DONE;
Christopher Faulet87633c32023-04-03 18:32:50 +0200581 oc->flags |= CF_WRITE_EVENT;
Christopher Fauletbcdcfad2023-02-20 08:36:53 +0100582 sc_set_hcto(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200583
Willy Tarreau0adb2812022-05-27 10:02:48 +0200584 switch (sc->state) {
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200585 case SC_ST_RDY:
586 case SC_ST_EST:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200587 /* we have to shut before closing, otherwise some short messages
588 * may never leave the system, especially when there are remaining
589 * unread data in the socket input buffer, or when nolinger is set.
Willy Tarreaucb041662022-05-17 19:44:42 +0200590 * However, if SC_FL_NOLINGER is explicitly set, we know there is
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200591 * no risk so we close both sides immediately.
592 */
Christopher Fauletca5309a2023-04-17 16:17:32 +0200593 if (!(sc->flags & (SC_FL_ERROR|SC_FL_NOLINGER|SC_FL_EOS|SC_FL_ABRT_DONE)) &&
Christopher Fauletad46e522023-04-14 11:59:15 +0200594 !(ic->flags & CF_DONT_READ))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200595 return;
596
Willy Tarreau476c2802022-11-14 07:36:42 +0100597 __fallthrough;
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200598 case SC_ST_CON:
599 case SC_ST_CER:
600 case SC_ST_QUE:
601 case SC_ST_TAR:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200602 /* Note that none of these states may happen with applets */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200603 sc->state = SC_ST_DIS;
Willy Tarreau476c2802022-11-14 07:36:42 +0100604 __fallthrough;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200605 default:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200606 sc->flags &= ~SC_FL_NOLINGER;
Christopher Faulet0c370ee2023-04-13 16:05:13 +0200607 sc->flags |= SC_FL_ABRT_DONE;
Christopher Fauletca679922022-07-20 13:24:04 +0200608 if (sc->flags & SC_FL_ISBACK)
609 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200610 }
611
612 /* note that if the task exists, it must unregister itself once it runs */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200613 if (!(sc->flags & SC_FL_DONT_WAKE))
614 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200615}
616
617/* default chk_rcv function for scheduled tasks */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200618static void sc_app_chk_rcv(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200619{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200620 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200621
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200622 if (ic->pipe) {
623 /* stop reading */
Christopher Faulet7b3d38a2023-05-05 11:28:45 +0200624 sc_need_room(sc, -1);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200625 }
626 else {
627 /* (re)start reading */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200628 if (!(sc->flags & SC_FL_DONT_WAKE))
629 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200630 }
631}
632
633/* default chk_snd function for scheduled tasks */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200634static void sc_app_chk_snd(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200635{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200636 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200637
Christopher Faulet208c7122023-04-13 16:16:15 +0200638 if (unlikely(sc->state != SC_ST_EST || (sc->flags & SC_FL_SHUT_DONE)))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200639 return;
640
Willy Tarreau0adb2812022-05-27 10:02:48 +0200641 if (!sc_ep_test(sc, SE_FL_WAIT_DATA) || /* not waiting for data */
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200642 channel_is_empty(oc)) /* called with nothing to send ! */
643 return;
644
645 /* Otherwise there are remaining data to be sent in the buffer,
646 * so we tell the handler.
647 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200648 sc_ep_clr(sc, SE_FL_WAIT_DATA);
Willy Tarreau0adb2812022-05-27 10:02:48 +0200649 if (!(sc->flags & SC_FL_DONT_WAKE))
650 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200651}
652
653/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200654 * This function performs a shutdown-read on a stream connector attached to
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200655 * a connection in a connected or init state (it does nothing for other
656 * states). It either shuts the read side or marks itself as closed. The buffer
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200657 * flags are updated to reflect the new state. If the stream connector has
Willy Tarreaucb041662022-05-17 19:44:42 +0200658 * SC_FL_NOHALF, we also forward the close to the write side. If a control
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200659 * layer is defined, then it is supposed to be a socket layer and file
660 * descriptors are then shutdown or closed accordingly. The function
661 * automatically disables polling if needed.
662 */
Christopher Fauletcfc11c02023-04-13 16:10:23 +0200663static void sc_app_abort_conn(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200664{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200665 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200666
Willy Tarreau0adb2812022-05-27 10:02:48 +0200667 BUG_ON(!sc_conn(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200668
Christopher Fauletca5309a2023-04-17 16:17:32 +0200669 if (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200670 return;
Christopher Faulet0c370ee2023-04-13 16:05:13 +0200671 sc->flags |= SC_FL_ABRT_DONE;
Christopher Faulet87633c32023-04-03 18:32:50 +0200672 ic->flags |= CF_READ_EVENT;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200673
Willy Tarreau0adb2812022-05-27 10:02:48 +0200674 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200675 return;
676
Christopher Faulet208c7122023-04-13 16:16:15 +0200677 if (sc->flags & SC_FL_SHUT_DONE) {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200678 sc_conn_shut(sc);
679 sc->state = SC_ST_DIS;
Christopher Fauletca679922022-07-20 13:24:04 +0200680 if (sc->flags & SC_FL_ISBACK)
681 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200682 }
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +0200683 else if (sc_cond_forward_shut(sc))
684 return sc_app_shut_conn(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200685}
686
687/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200688 * This function performs a shutdown-write on a stream connector attached to
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200689 * a connection in a connected or init state (it does nothing for other
690 * states). It either shuts the write side or marks itself as closed. The
691 * buffer flags are updated to reflect the new state. It does also close
Willy Tarreaue68bc612022-05-27 11:23:05 +0200692 * everything if the SC was marked as being in error state. If there is a
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200693 * data-layer shutdown, it is called.
694 */
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +0200695static void sc_app_shut_conn(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200696{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200697 struct channel *ic = sc_ic(sc);
698 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200699
Willy Tarreau0adb2812022-05-27 10:02:48 +0200700 BUG_ON(!sc_conn(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200701
Christopher Faulete38534c2023-04-13 15:45:24 +0200702 sc->flags &= ~SC_FL_SHUT_WANTED;
Christopher Faulet208c7122023-04-13 16:16:15 +0200703 if (sc->flags & SC_FL_SHUT_DONE)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200704 return;
Christopher Faulet208c7122023-04-13 16:16:15 +0200705 sc->flags |= SC_FL_SHUT_DONE;
Christopher Faulet87633c32023-04-03 18:32:50 +0200706 oc->flags |= CF_WRITE_EVENT;
Christopher Fauletbcdcfad2023-02-20 08:36:53 +0100707 sc_set_hcto(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200708
Willy Tarreau0adb2812022-05-27 10:02:48 +0200709 switch (sc->state) {
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200710 case SC_ST_RDY:
711 case SC_ST_EST:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200712 /* we have to shut before closing, otherwise some short messages
713 * may never leave the system, especially when there are remaining
714 * unread data in the socket input buffer, or when nolinger is set.
Willy Tarreaucb041662022-05-17 19:44:42 +0200715 * However, if SC_FL_NOLINGER is explicitly set, we know there is
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200716 * no risk so we close both sides immediately.
717 */
718
Christopher Faulet25d9fe52023-04-14 12:09:35 +0200719 if (sc->flags & SC_FL_ERROR) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200720 /* quick close, the socket is already shut anyway */
721 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200722 else if (sc->flags & SC_FL_NOLINGER) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200723 /* unclean data-layer shutdown, typically an aborted request
724 * or a forwarded shutdown from a client to a server due to
725 * option abortonclose. No need for the TLS layer to try to
726 * emit a shutdown message.
727 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200728 sc_conn_shutw(sc, CO_SHW_SILENT);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200729 }
730 else {
731 /* clean data-layer shutdown. This only happens on the
732 * frontend side, or on the backend side when forwarding
733 * a client close in TCP mode or in HTTP TUNNEL mode
734 * while option abortonclose is set. We want the TLS
735 * layer to try to signal it to the peer before we close.
736 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200737 sc_conn_shutw(sc, CO_SHW_NORMAL);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200738
Christopher Fauletca5309a2023-04-17 16:17:32 +0200739 if (!(sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)) && !(ic->flags & CF_DONT_READ))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200740 return;
741 }
742
Willy Tarreau476c2802022-11-14 07:36:42 +0100743 __fallthrough;
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200744 case SC_ST_CON:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200745 /* we may have to close a pending connection, and mark the
Christopher Fauletcfc11c02023-04-13 16:10:23 +0200746 * response buffer as abort
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200747 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200748 sc_conn_shut(sc);
Willy Tarreau476c2802022-11-14 07:36:42 +0100749 __fallthrough;
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200750 case SC_ST_CER:
751 case SC_ST_QUE:
752 case SC_ST_TAR:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200753 sc->state = SC_ST_DIS;
Willy Tarreau476c2802022-11-14 07:36:42 +0100754 __fallthrough;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200755 default:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200756 sc->flags &= ~SC_FL_NOLINGER;
Christopher Faulet0c370ee2023-04-13 16:05:13 +0200757 sc->flags |= SC_FL_ABRT_DONE;
Christopher Fauletca679922022-07-20 13:24:04 +0200758 if (sc->flags & SC_FL_ISBACK)
759 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200760 }
761}
762
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200763/* This function is used for inter-stream connector calls. It is called by the
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200764 * consumer to inform the producer side that it may be interested in checking
765 * for free space in the buffer. Note that it intentionally does not update
766 * timeouts, so that we can still check them later at wake-up. This function is
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200767 * dedicated to connection-based stream connectors.
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200768 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200769static void sc_app_chk_rcv_conn(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200770{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200771 BUG_ON(!sc_conn(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200772
773 /* (re)start reading */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200774 if (sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
775 tasklet_wakeup(sc->wait_event.tasklet);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200776}
777
778
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200779/* This function is used for inter-stream connector calls. It is called by the
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200780 * producer to inform the consumer side that it may be interested in checking
781 * for data in the buffer. Note that it intentionally does not update timeouts,
782 * so that we can still check them later at wake-up.
783 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200784static void sc_app_chk_snd_conn(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200785{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200786 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200787
Willy Tarreau0adb2812022-05-27 10:02:48 +0200788 BUG_ON(!sc_conn(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200789
Willy Tarreau0adb2812022-05-27 10:02:48 +0200790 if (unlikely(!sc_state_in(sc->state, SC_SB_RDY|SC_SB_EST) ||
Christopher Faulet208c7122023-04-13 16:16:15 +0200791 (sc->flags & SC_FL_SHUT_DONE)))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200792 return;
793
794 if (unlikely(channel_is_empty(oc))) /* called with nothing to send ! */
795 return;
796
797 if (!oc->pipe && /* spliced data wants to be forwarded ASAP */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200798 !sc_ep_test(sc, SE_FL_WAIT_DATA)) /* not waiting for data */
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200799 return;
800
Willy Tarreau0adb2812022-05-27 10:02:48 +0200801 if (!(sc->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(sc_oc(sc)))
802 sc_conn_send(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200803
Willy Tarreau0adb2812022-05-27 10:02:48 +0200804 if (sc_ep_test(sc, SE_FL_ERROR | SE_FL_ERR_PENDING) || sc_is_conn_error(sc)) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200805 /* Write error on the file descriptor */
Christopher Faulet56a2b602023-04-14 09:42:59 +0200806 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 +0200807 goto out_wakeup;
808 }
809
810 /* OK, so now we know that some data might have been sent, and that we may
811 * have to poll first. We have to do that too if the buffer is not empty.
812 */
813 if (channel_is_empty(oc)) {
814 /* the connection is established but we can't write. Either the
815 * buffer is empty, or we just refrain from sending because the
816 * ->o limit was reached. Maybe we just wrote the last
817 * chunk and need to close.
818 */
Christopher Faulet87633c32023-04-03 18:32:50 +0200819 if ((oc->flags & CF_AUTO_CLOSE) &&
Christopher Faulet208c7122023-04-13 16:16:15 +0200820 ((sc->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED)) == SC_FL_SHUT_WANTED) &&
Willy Tarreau0adb2812022-05-27 10:02:48 +0200821 sc_state_in(sc->state, SC_SB_RDY|SC_SB_EST)) {
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +0200822 sc_shutdown(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200823 goto out_wakeup;
824 }
825
Christopher Faulet208c7122023-04-13 16:16:15 +0200826 if ((sc->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED)) == 0)
Willy Tarreau0adb2812022-05-27 10:02:48 +0200827 sc_ep_set(sc, SE_FL_WAIT_DATA);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200828 }
829 else {
830 /* Otherwise there are remaining data to be sent in the buffer,
831 * which means we have to poll before doing so.
832 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200833 sc_ep_clr(sc, SE_FL_WAIT_DATA);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200834 }
835
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200836 /* in case of special condition (error, shutdown, end of write...), we
837 * have to notify the task.
838 */
Christopher Faulet208c7122023-04-13 16:16:15 +0200839 if (likely((sc->flags & SC_FL_SHUT_DONE) ||
Christopher Faulet71c486b2023-02-09 14:14:38 +0100840 ((oc->flags & CF_WRITE_EVENT) && sc->state < SC_ST_EST) ||
841 ((oc->flags & CF_WAKE_WRITE) &&
842 ((channel_is_empty(oc) && !oc->to_forward) ||
843 !sc_state_in(sc->state, SC_SB_EST))))) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200844 out_wakeup:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200845 if (!(sc->flags & SC_FL_DONT_WAKE))
846 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200847 }
848}
849
850/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200851 * This function performs a shutdown-read on a stream connector attached to an
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200852 * applet in a connected or init state (it does nothing for other states). It
853 * either shuts the read side or marks itself as closed. The buffer flags are
Willy Tarreaucb041662022-05-17 19:44:42 +0200854 * updated to reflect the new state. If the stream connector has SC_FL_NOHALF,
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200855 * we also forward the close to the write side. The owner task is woken up if
856 * it exists.
857 */
Christopher Fauletcfc11c02023-04-13 16:10:23 +0200858static void sc_app_abort_applet(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200859{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200860 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200861
Willy Tarreau0adb2812022-05-27 10:02:48 +0200862 BUG_ON(!sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200863
Christopher Fauletca5309a2023-04-17 16:17:32 +0200864 if (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200865 return;
Christopher Faulet0c370ee2023-04-13 16:05:13 +0200866 sc->flags |= SC_FL_ABRT_DONE;
Christopher Faulet87633c32023-04-03 18:32:50 +0200867 ic->flags |= CF_READ_EVENT;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200868
Christopher Fauletcfc11c02023-04-13 16:10:23 +0200869 /* Note: on abort, we don't call the applet */
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200870
Willy Tarreau0adb2812022-05-27 10:02:48 +0200871 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200872 return;
873
Christopher Faulet208c7122023-04-13 16:16:15 +0200874 if (sc->flags & SC_FL_SHUT_DONE) {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200875 appctx_shut(__sc_appctx(sc));
876 sc->state = SC_ST_DIS;
Christopher Fauletca679922022-07-20 13:24:04 +0200877 if (sc->flags & SC_FL_ISBACK)
878 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200879 }
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +0200880 else if (sc_cond_forward_shut(sc))
881 return sc_app_shut_applet(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200882}
883
884/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200885 * This function performs a shutdown-write on a stream connector attached to an
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200886 * applet in a connected or init state (it does nothing for other states). It
887 * either shuts the write side or marks itself as closed. The buffer flags are
888 * updated to reflect the new state. It does also close everything if the SI
889 * was marked as being in error state. The owner task is woken up if it exists.
890 */
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +0200891static void sc_app_shut_applet(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200892{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200893 struct channel *ic = sc_ic(sc);
894 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200895
Willy Tarreau0adb2812022-05-27 10:02:48 +0200896 BUG_ON(!sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200897
Christopher Faulete38534c2023-04-13 15:45:24 +0200898 sc->flags &= ~SC_FL_SHUT_WANTED;
Christopher Faulet208c7122023-04-13 16:16:15 +0200899 if (sc->flags & SC_FL_SHUT_DONE)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200900 return;
Christopher Faulet208c7122023-04-13 16:16:15 +0200901 sc->flags |= SC_FL_SHUT_DONE;
Christopher Faulet87633c32023-04-03 18:32:50 +0200902 oc->flags |= CF_WRITE_EVENT;
Christopher Fauletbcdcfad2023-02-20 08:36:53 +0100903 sc_set_hcto(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200904
905 /* on shutw we always wake the applet up */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200906 appctx_wakeup(__sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200907
Willy Tarreau0adb2812022-05-27 10:02:48 +0200908 switch (sc->state) {
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200909 case SC_ST_RDY:
910 case SC_ST_EST:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200911 /* we have to shut before closing, otherwise some short messages
912 * may never leave the system, especially when there are remaining
913 * unread data in the socket input buffer, or when nolinger is set.
Willy Tarreaucb041662022-05-17 19:44:42 +0200914 * However, if SC_FL_NOLINGER is explicitly set, we know there is
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200915 * no risk so we close both sides immediately.
916 */
Christopher Fauletca5309a2023-04-17 16:17:32 +0200917 if (!(sc->flags & (SC_FL_ERROR|SC_FL_NOLINGER|SC_FL_EOS|SC_FL_ABRT_DONE)) &&
Christopher Faulet87633c32023-04-03 18:32:50 +0200918 !(ic->flags & CF_DONT_READ))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200919 return;
920
Willy Tarreau476c2802022-11-14 07:36:42 +0100921 __fallthrough;
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200922 case SC_ST_CON:
923 case SC_ST_CER:
924 case SC_ST_QUE:
925 case SC_ST_TAR:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200926 /* Note that none of these states may happen with applets */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200927 appctx_shut(__sc_appctx(sc));
928 sc->state = SC_ST_DIS;
Willy Tarreau476c2802022-11-14 07:36:42 +0100929 __fallthrough;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200930 default:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200931 sc->flags &= ~SC_FL_NOLINGER;
Christopher Faulet0c370ee2023-04-13 16:05:13 +0200932 sc->flags |= SC_FL_ABRT_DONE;
Christopher Fauletca679922022-07-20 13:24:04 +0200933 if (sc->flags & SC_FL_ISBACK)
934 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200935 }
936}
937
938/* chk_rcv function for applets */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200939static void sc_app_chk_rcv_applet(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200940{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200941 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200942
Willy Tarreau0adb2812022-05-27 10:02:48 +0200943 BUG_ON(!sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200944
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200945 if (!ic->pipe) {
946 /* (re)start reading */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200947 appctx_wakeup(__sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200948 }
949}
950
951/* chk_snd function for applets */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200952static void sc_app_chk_snd_applet(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200953{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200954 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200955
Willy Tarreau0adb2812022-05-27 10:02:48 +0200956 BUG_ON(!sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200957
Christopher Faulet208c7122023-04-13 16:16:15 +0200958 if (unlikely(sc->state != SC_ST_EST || (sc->flags & SC_FL_SHUT_DONE)))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200959 return;
960
Christopher Faulet04f03e12022-06-01 17:35:34 +0200961 /* we only wake the applet up if it was waiting for some data and is ready to consume it */
962 if (!sc_ep_test(sc, SE_FL_WAIT_DATA) || sc_ep_test(sc, SE_FL_WONT_CONSUME))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200963 return;
964
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200965 if (!channel_is_empty(oc)) {
966 /* (re)start sending */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200967 appctx_wakeup(__sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200968 }
969}
Christopher Faulet13045f02022-04-01 14:23:38 +0200970
971
972/* This function is designed to be called from within the stream handler to
Willy Tarreau4596fe22022-05-17 19:07:51 +0200973 * update the input channel's expiration timer and the stream connector's
Christopher Faulet13045f02022-04-01 14:23:38 +0200974 * Rx flags based on the channel's flags. It needs to be called only once
975 * after the channel's flags have settled down, and before they are cleared,
976 * though it doesn't harm to call it as often as desired (it just slightly
977 * hurts performance). It must not be called from outside of the stream
978 * handler, as what it does will be used to compute the stream task's
979 * expiration.
980 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200981void sc_update_rx(struct stconn *sc)
Christopher Faulet13045f02022-04-01 14:23:38 +0200982{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200983 struct channel *ic = sc_ic(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +0200984
Christopher Fauletca5309a2023-04-17 16:17:32 +0200985 if (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))
Christopher Faulet13045f02022-04-01 14:23:38 +0200986 return;
Christopher Faulet13045f02022-04-01 14:23:38 +0200987
Christopher Fauletfab82bf2023-05-05 11:30:16 +0200988 /* Unblock the SC if it needs room and the free space is large enough (0
989 * means it can always be unblocked). Do not unblock it if -1 was
990 * specified.
991 */
992 if (!sc->room_needed || (sc->room_needed > 0 && channel_recv_max(ic) >= sc->room_needed))
993 sc_have_room(sc);
994
Christopher Faulet13045f02022-04-01 14:23:38 +0200995 /* Read not closed, update FD status and timeout for reads */
996 if (ic->flags & CF_DONT_READ)
Willy Tarreau0adb2812022-05-27 10:02:48 +0200997 sc_wont_read(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +0200998 else
Willy Tarreau0adb2812022-05-27 10:02:48 +0200999 sc_will_read(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +02001000
Willy Tarreau0adb2812022-05-27 10:02:48 +02001001 sc_chk_rcv(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +02001002}
1003
1004/* This function is designed to be called from within the stream handler to
Willy Tarreau4596fe22022-05-17 19:07:51 +02001005 * update the output channel's expiration timer and the stream connector's
Christopher Faulet13045f02022-04-01 14:23:38 +02001006 * Tx flags based on the channel's flags. It needs to be called only once
1007 * after the channel's flags have settled down, and before they are cleared,
1008 * though it doesn't harm to call it as often as desired (it just slightly
1009 * hurts performance). It must not be called from outside of the stream
1010 * handler, as what it does will be used to compute the stream task's
1011 * expiration.
1012 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001013void sc_update_tx(struct stconn *sc)
Christopher Faulet13045f02022-04-01 14:23:38 +02001014{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001015 struct channel *oc = sc_oc(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +02001016
Christopher Faulet208c7122023-04-13 16:16:15 +02001017 if (sc->flags & SC_FL_SHUT_DONE)
Christopher Faulet13045f02022-04-01 14:23:38 +02001018 return;
1019
1020 /* Write not closed, update FD status and timeout for writes */
1021 if (channel_is_empty(oc)) {
1022 /* stop writing */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001023 if (!sc_ep_test(sc, SE_FL_WAIT_DATA)) {
Christopher Faulete38534c2023-04-13 15:45:24 +02001024 if ((sc->flags & SC_FL_SHUT_WANTED) == 0)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001025 sc_ep_set(sc, SE_FL_WAIT_DATA);
Christopher Faulet13045f02022-04-01 14:23:38 +02001026 }
1027 return;
1028 }
1029
Christopher Faulet15315d62023-02-20 08:23:51 +01001030 /* (re)start writing */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001031 sc_ep_clr(sc, SE_FL_WAIT_DATA);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001032}
1033
Willy Tarreau19c65a92022-05-27 08:49:24 +02001034/* This function is the equivalent to sc_update() except that it's
Christopher Faulet5e29b762022-04-04 08:58:34 +02001035 * designed to be called from outside the stream handlers, typically the lower
1036 * layers (applets, connections) after I/O completion. After updating the stream
1037 * interface and timeouts, it will try to forward what can be forwarded, then to
1038 * wake the associated task up if an important event requires special handling.
Willy Tarreau15252cd2022-05-25 16:36:21 +02001039 * It may update SE_FL_WAIT_DATA and/or SC_FL_NEED_ROOM, that the callers are
Christopher Faulet5e29b762022-04-04 08:58:34 +02001040 * encouraged to watch to take appropriate action.
Willy Tarreau19c65a92022-05-27 08:49:24 +02001041 * It should not be called from within the stream itself, sc_update()
Christopher Faulet5e29b762022-04-04 08:58:34 +02001042 * is designed for this.
1043 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001044static void sc_notify(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001045{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001046 struct channel *ic = sc_ic(sc);
1047 struct channel *oc = sc_oc(sc);
Willy Tarreaue68bc612022-05-27 11:23:05 +02001048 struct stconn *sco = sc_opposite(sc);
Willy Tarreau0adb2812022-05-27 10:02:48 +02001049 struct task *task = sc_strm_task(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001050
1051 /* process consumer side */
1052 if (channel_is_empty(oc)) {
Willy Tarreau0adb2812022-05-27 10:02:48 +02001053 struct connection *conn = sc_conn(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001054
Christopher Faulet208c7122023-04-13 16:16:15 +02001055 if (((sc->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED)) == SC_FL_SHUT_WANTED) &&
Willy Tarreau0adb2812022-05-27 10:02:48 +02001056 (sc->state == SC_ST_EST) && (!conn || !(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS))))
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +02001057 sc_shutdown(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001058 }
1059
1060 /* indicate that we may be waiting for data from the output channel or
Christopher Faulete38534c2023-04-13 15:45:24 +02001061 * 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 +02001062 */
Christopher Faulet208c7122023-04-13 16:16:15 +02001063 if (!(sc->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED)))
Willy Tarreau0adb2812022-05-27 10:02:48 +02001064 sc_ep_set(sc, SE_FL_WAIT_DATA);
Christopher Faulet208c7122023-04-13 16:16:15 +02001065 else if ((sc->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED)) == SC_FL_SHUT_WANTED)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001066 sc_ep_clr(sc, SE_FL_WAIT_DATA);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001067
Christopher Faulet5e29b762022-04-04 08:58:34 +02001068 if (oc->flags & CF_DONT_READ)
Willy Tarreaue68bc612022-05-27 11:23:05 +02001069 sc_wont_read(sco);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001070 else
Willy Tarreaue68bc612022-05-27 11:23:05 +02001071 sc_will_read(sco);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001072
1073 /* Notify the other side when we've injected data into the IC that
1074 * needs to be forwarded. We can do fast-forwarding as soon as there
1075 * are output data, but we avoid doing this if some of the data are
1076 * not yet scheduled for being forwarded, because it is very likely
1077 * that it will be done again immediately afterwards once the following
Willy Tarreau15252cd2022-05-25 16:36:21 +02001078 * data are parsed (eg: HTTP chunking). We only clear SC_FL_NEED_ROOM
1079 * once we've emptied *some* of the output buffer, and not just when
1080 * there is available room, because applets are often forced to stop
1081 * before the buffer is full. We must not stop based on input data
1082 * alone because an HTTP parser might need more data to complete the
1083 * parsing.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001084 */
1085 if (!channel_is_empty(ic) &&
Willy Tarreaue68bc612022-05-27 11:23:05 +02001086 sc_ep_test(sco, SE_FL_WAIT_DATA) &&
Christopher Faulet84d3ef92023-03-17 15:45:58 +01001087 (!(sc->flags & SC_FL_SND_EXP_MORE) || c_full(ic) || ci_data(ic) == 0 || ic->pipe)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001088 int new_len, last_len;
1089
1090 last_len = co_data(ic);
1091 if (ic->pipe)
1092 last_len += ic->pipe->data;
1093
Willy Tarreaue68bc612022-05-27 11:23:05 +02001094 sc_chk_snd(sco);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001095
1096 new_len = co_data(ic);
1097 if (ic->pipe)
1098 new_len += ic->pipe->data;
1099
1100 /* check if the consumer has freed some space either in the
1101 * buffer or in the pipe.
1102 */
Christopher Faulet18b33092023-05-05 11:40:07 +02001103 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 +02001104 sc_have_room(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001105 }
1106
1107 if (!(ic->flags & CF_DONT_READ))
Willy Tarreau0adb2812022-05-27 10:02:48 +02001108 sc_will_read(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001109
Willy Tarreau0adb2812022-05-27 10:02:48 +02001110 sc_chk_rcv(sc);
Willy Tarreaue68bc612022-05-27 11:23:05 +02001111 sc_chk_rcv(sco);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001112
Christopher Faulet5e29b762022-04-04 08:58:34 +02001113 /* wake the task up only when needed */
Christopher Faulet285f7612022-12-12 08:28:55 +01001114 if (/* changes on the production side that must be handled:
Christopher Fauletad46e522023-04-14 11:59:15 +02001115 * - An error on receipt: SC_FL_ERROR
Christopher Fauletca5309a2023-04-17 16:17:32 +02001116 * - A read event: shutdown for reads (CF_READ_EVENT + EOS/ABRT_DONE)
Christopher Faulet904763f2023-03-22 14:53:11 +01001117 * end of input (CF_READ_EVENT + SC_FL_EOI)
Christopher Faulet285f7612022-12-12 08:28:55 +01001118 * data received and no fast-forwarding (CF_READ_EVENT + !to_forward)
1119 * read event while consumer side is not established (CF_READ_EVENT + sco->state != SC_ST_EST)
1120 */
Christopher Fauletca5309a2023-04-17 16:17:32 +02001121 ((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 +02001122 (sc->flags & SC_FL_ERROR) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001123
1124 /* changes on the consumption side */
Christopher Faulet2e56a732023-01-26 16:18:09 +01001125 sc_ep_test(sc, SE_FL_ERR_PENDING) ||
Christopher Fauletd8988412022-12-20 18:10:04 +01001126 ((oc->flags & CF_WRITE_EVENT) &&
1127 ((sc->state < SC_ST_EST) ||
Christopher Faulet208c7122023-04-13 16:16:15 +02001128 (sc->flags & SC_FL_SHUT_DONE) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001129 (((oc->flags & CF_WAKE_WRITE) ||
Christopher Faulet87633c32023-04-03 18:32:50 +02001130 (!(oc->flags & CF_AUTO_CLOSE) &&
Christopher Faulet208c7122023-04-13 16:16:15 +02001131 !(sc->flags & (SC_FL_SHUT_WANTED|SC_FL_SHUT_DONE)))) &&
Christopher Faulet87633c32023-04-03 18:32:50 +02001132 (sco->state != SC_ST_EST ||
1133 (channel_is_empty(oc) && !oc->to_forward)))))) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001134 task_wakeup(task, TASK_WOKEN_IO);
1135 }
Christopher Faulet5e29b762022-04-04 08:58:34 +02001136
Christopher Faulet2e56a732023-01-26 16:18:09 +01001137 if (ic->flags & CF_READ_EVENT)
Christopher Faulet9a790f62023-03-16 14:40:03 +01001138 sc->flags &= ~SC_FL_RCV_ONCE;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001139}
1140
1141/*
Christopher Faulet1aec6c92023-04-17 17:29:29 +02001142 * This function propagates an end-of-stream received on a socket-based connection.
Willy Tarreaucb041662022-05-17 19:44:42 +02001143 * It updates the stream connector. If the stream connector has SC_FL_NOHALF,
Christopher Faulet5e29b762022-04-04 08:58:34 +02001144 * the close is also forwarded to the write side as an abort.
1145 */
Christopher Faulet1aec6c92023-04-17 17:29:29 +02001146static void sc_conn_eos(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001147{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001148 struct channel *ic = sc_ic(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001149
Willy Tarreau0adb2812022-05-27 10:02:48 +02001150 BUG_ON(!sc_conn(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001151
Christopher Fauletca5309a2023-04-17 16:17:32 +02001152 if (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001153 return;
Christopher Faulet1aec6c92023-04-17 17:29:29 +02001154 sc->flags |= SC_FL_EOS;
Christopher Faulet87633c32023-04-03 18:32:50 +02001155 ic->flags |= CF_READ_EVENT;
Christopher Faulet4c135682023-02-16 11:09:31 +01001156 sc_ep_report_read_activity(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001157
Willy Tarreau0adb2812022-05-27 10:02:48 +02001158 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001159 return;
1160
Christopher Faulet208c7122023-04-13 16:16:15 +02001161 if (sc->flags & SC_FL_SHUT_DONE)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001162 goto do_close;
1163
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +02001164 if (sc_cond_forward_shut(sc)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001165 /* we want to immediately forward this close to the write side */
1166 /* force flag on ssl to keep stream in cache */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001167 sc_conn_shutw(sc, CO_SHW_SILENT);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001168 goto do_close;
1169 }
1170
1171 /* otherwise that's just a normal read shutdown */
1172 return;
1173
1174 do_close:
Willy Tarreauf61dd192022-05-27 09:00:19 +02001175 /* OK we completely close the socket here just as if we went through sc_shut[rw]() */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001176 sc_conn_shut(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001177
Christopher Faulete38534c2023-04-13 15:45:24 +02001178 sc->flags &= ~SC_FL_SHUT_WANTED;
Christopher Faulet208c7122023-04-13 16:16:15 +02001179 sc->flags |= SC_FL_SHUT_DONE;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001180
Willy Tarreau0adb2812022-05-27 10:02:48 +02001181 sc->state = SC_ST_DIS;
Christopher Fauletca679922022-07-20 13:24:04 +02001182 if (sc->flags & SC_FL_ISBACK)
1183 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001184 return;
1185}
1186
1187/*
1188 * This is the callback which is called by the connection layer to receive data
1189 * into the buffer from the connection. It iterates over the mux layer's
1190 * rcv_buf function.
1191 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001192static int sc_conn_recv(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001193{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001194 struct connection *conn = __sc_conn(sc);
1195 struct channel *ic = sc_ic(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001196 int ret, max, cur_read = 0;
1197 int read_poll = MAX_READ_POLL_LOOPS;
1198 int flags = 0;
1199
1200 /* If not established yet, do nothing. */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001201 if (sc->state != SC_ST_EST)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001202 return 0;
1203
Willy Tarreau462b9892022-05-18 18:06:53 +02001204 /* If another call to sc_conn_recv() failed, and we subscribed to
Christopher Faulet5e29b762022-04-04 08:58:34 +02001205 * recv events already, give up now.
1206 */
Christopher Faulet95125882023-04-12 18:35:18 +02001207 if ((sc->wait_event.events & SUB_RETRY_RECV) || sc_waiting_room(sc))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001208 return 0;
1209
Christopher Fauletcfc11c02023-04-13 16:10:23 +02001210 /* maybe we were called immediately after an asynchronous abort */
Christopher Fauletca5309a2023-04-17 16:17:32 +02001211 if (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001212 return 1;
1213
1214 /* we must wait because the mux is not installed yet */
1215 if (!conn->mux)
1216 return 0;
1217
Christopher Faulet5e29b762022-04-04 08:58:34 +02001218 /* stop immediately on errors. Note that we DON'T want to stop on
1219 * POLL_ERR, as the poller might report a write error while there
1220 * are still data available in the recv buffer. This typically
1221 * happens when we send too large a request to a backend server
1222 * which rejects it before reading it all.
1223 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001224 if (!sc_ep_test(sc, SE_FL_RCV_MORE)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001225 if (!conn_xprt_ready(conn))
1226 return 0;
Willy Tarreau0adb2812022-05-27 10:02:48 +02001227 if (sc_ep_test(sc, SE_FL_ERROR))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001228 goto end_recv;
1229 }
1230
1231 /* prepare to detect if the mux needs more room */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001232 sc_ep_clr(sc, SE_FL_WANT_ROOM);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001233
1234 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) && !co_data(ic) &&
1235 global.tune.idle_timer &&
1236 (unsigned short)(now_ms - ic->last_read) >= global.tune.idle_timer) {
1237 /* The buffer was empty and nothing was transferred for more
1238 * than one second. This was caused by a pause and not by
1239 * congestion. Reset any streaming mode to reduce latency.
1240 */
1241 ic->xfer_small = 0;
1242 ic->xfer_large = 0;
1243 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
1244 }
1245
1246 /* First, let's see if we may splice data across the channel without
1247 * using a buffer.
1248 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001249 if (sc_ep_test(sc, SE_FL_MAY_SPLICE) &&
Christopher Faulet5e29b762022-04-04 08:58:34 +02001250 (ic->pipe || ic->to_forward >= MIN_SPLICE_FORWARD) &&
1251 ic->flags & CF_KERN_SPLICING) {
1252 if (c_data(ic)) {
1253 /* We're embarrassed, there are already data pending in
1254 * the buffer and we don't want to have them at two
1255 * locations at a time. Let's indicate we need some
1256 * place and ask the consumer to hurry.
1257 */
1258 flags |= CO_RFL_BUF_FLUSH;
1259 goto abort_splice;
1260 }
1261
1262 if (unlikely(ic->pipe == NULL)) {
1263 if (pipes_used >= global.maxpipes || !(ic->pipe = get_pipe())) {
1264 ic->flags &= ~CF_KERN_SPLICING;
1265 goto abort_splice;
1266 }
1267 }
1268
Willy Tarreau0adb2812022-05-27 10:02:48 +02001269 ret = conn->mux->rcv_pipe(sc, ic->pipe, ic->to_forward);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001270 if (ret < 0) {
1271 /* splice not supported on this end, let's disable it */
1272 ic->flags &= ~CF_KERN_SPLICING;
1273 goto abort_splice;
1274 }
1275
1276 if (ret > 0) {
1277 if (ic->to_forward != CHN_INFINITE_FORWARD)
1278 ic->to_forward -= ret;
1279 ic->total += ret;
1280 cur_read += ret;
Christopher Faulet285f7612022-12-12 08:28:55 +01001281 ic->flags |= CF_READ_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001282 }
1283
Willy Tarreau0adb2812022-05-27 10:02:48 +02001284 if (sc_ep_test(sc, SE_FL_EOS | SE_FL_ERROR))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001285 goto end_recv;
1286
1287 if (conn->flags & CO_FL_WAIT_ROOM) {
1288 /* the pipe is full or we have read enough data that it
1289 * could soon be full. Let's stop before needing to poll.
1290 */
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02001291 sc_need_room(sc, 0);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001292 goto done_recv;
1293 }
1294
1295 /* splice not possible (anymore), let's go on on standard copy */
1296 }
1297
1298 abort_splice:
1299 if (ic->pipe && unlikely(!ic->pipe->data)) {
1300 put_pipe(ic->pipe);
1301 ic->pipe = NULL;
1302 }
1303
Willy Tarreau0adb2812022-05-27 10:02:48 +02001304 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 +02001305 /* don't break splicing by reading, but still call rcv_buf()
1306 * to pass the flag.
1307 */
1308 goto done_recv;
1309 }
1310
1311 /* now we'll need a input buffer for the stream */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001312 if (!sc_alloc_ibuf(sc, &(__sc_strm(sc)->buffer_wait)))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001313 goto end_recv;
1314
1315 /* For an HTX stream, if the buffer is stuck (no output data with some
1316 * input data) and if the HTX message is fragmented or if its free space
1317 * wraps, we force an HTX deframentation. It is a way to have a
1318 * contiguous free space nad to let the mux to copy as much data as
1319 * possible.
1320 *
1321 * NOTE: A possible optim may be to let the mux decides if defrag is
1322 * required or not, depending on amount of data to be xferred.
1323 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001324 if (IS_HTX_STRM(__sc_strm(sc)) && !co_data(ic)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001325 struct htx *htx = htxbuf(&ic->buf);
1326
1327 if (htx_is_not_empty(htx) && ((htx->flags & HTX_FL_FRAGMENTED) || htx_space_wraps(htx)))
1328 htx_defrag(htx, NULL, 0);
1329 }
1330
1331 /* Instruct the mux it must subscribed for read events */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001332 flags |= ((!conn_is_back(conn) && (__sc_strm(sc)->be->options & PR_O_ABRT_CLOSE)) ? CO_RFL_KEEP_RECV : 0);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001333
1334 /* Important note : if we're called with POLL_IN|POLL_HUP, it means the read polling
1335 * was enabled, which implies that the recv buffer was not full. So we have a guarantee
1336 * that if such an event is not handled above in splice, it will be handled here by
1337 * recv().
1338 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001339 while (sc_ep_test(sc, SE_FL_RCV_MORE) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001340 (!(conn->flags & CO_FL_HANDSHAKE) &&
Christopher Fauletca5309a2023-04-17 16:17:32 +02001341 (!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 +02001342 int cur_flags = flags;
1343
1344 /* Compute transient CO_RFL_* flags */
1345 if (co_data(ic)) {
1346 cur_flags |= (CO_RFL_BUF_WET | CO_RFL_BUF_NOT_STUCK);
1347 }
1348
1349 /* <max> may be null. This is the mux responsibility to set
Willy Tarreaue68bc612022-05-27 11:23:05 +02001350 * SE_FL_RCV_MORE on the SC if more space is needed.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001351 */
1352 max = channel_recv_max(ic);
Willy Tarreau0adb2812022-05-27 10:02:48 +02001353 ret = conn->mux->rcv_buf(sc, &ic->buf, max, cur_flags);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001354
Willy Tarreau0adb2812022-05-27 10:02:48 +02001355 if (sc_ep_test(sc, SE_FL_WANT_ROOM)) {
Willy Tarreaub605c422022-05-17 17:04:55 +02001356 /* SE_FL_WANT_ROOM must not be reported if the channel's
Christopher Faulet5e29b762022-04-04 08:58:34 +02001357 * buffer is empty.
1358 */
1359 BUG_ON(c_empty(ic));
1360
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02001361 sc_need_room(sc, channel_recv_max(ic) + 1);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001362 /* Add READ_PARTIAL because some data are pending but
1363 * cannot be xferred to the channel
1364 */
Christopher Faulet285f7612022-12-12 08:28:55 +01001365 ic->flags |= CF_READ_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001366 }
1367
1368 if (ret <= 0) {
1369 /* if we refrained from reading because we asked for a
1370 * flush to satisfy rcv_pipe(), we must not subscribe
1371 * and instead report that there's not enough room
1372 * here to proceed.
1373 */
1374 if (flags & CO_RFL_BUF_FLUSH)
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02001375 sc_need_room(sc, -1);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001376 break;
1377 }
1378
1379 cur_read += ret;
1380
1381 /* if we're allowed to directly forward data, we must update ->o */
Christopher Faulet64350bb2023-04-13 16:37:37 +02001382 if (ic->to_forward && !(sc_opposite(sc)->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED))) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001383 unsigned long fwd = ret;
1384 if (ic->to_forward != CHN_INFINITE_FORWARD) {
1385 if (fwd > ic->to_forward)
1386 fwd = ic->to_forward;
1387 ic->to_forward -= fwd;
1388 }
1389 c_adv(ic, fwd);
1390 }
1391
Christopher Faulet285f7612022-12-12 08:28:55 +01001392 ic->flags |= CF_READ_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001393 ic->total += ret;
1394
1395 /* End-of-input reached, we can leave. In this case, it is
Willy Tarreaue68bc612022-05-27 11:23:05 +02001396 * important to break the loop to not block the SC because of
Christopher Faulet5e29b762022-04-04 08:58:34 +02001397 * the channel's policies.This way, we are still able to receive
1398 * shutdowns.
1399 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001400 if (sc_ep_test(sc, SE_FL_EOI))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001401 break;
1402
Christopher Faulet9a790f62023-03-16 14:40:03 +01001403 if ((sc->flags & SC_FL_RCV_ONCE) || --read_poll <= 0) {
1404 /* we don't expect to read more data */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001405 sc_wont_read(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001406 break;
1407 }
1408
1409 /* if too many bytes were missing from last read, it means that
1410 * it's pointless trying to read again because the system does
1411 * not have them in buffers.
1412 */
1413 if (ret < max) {
1414 /* if a streamer has read few data, it may be because we
1415 * have exhausted system buffers. It's not worth trying
1416 * again.
1417 */
1418 if (ic->flags & CF_STREAMER) {
1419 /* we're stopped by the channel's policy */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001420 sc_wont_read(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001421 break;
1422 }
1423
1424 /* if we read a large block smaller than what we requested,
1425 * it's almost certain we'll never get anything more.
1426 */
1427 if (ret >= global.tune.recv_enough) {
1428 /* we're stopped by the channel's policy */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001429 sc_wont_read(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001430 break;
1431 }
1432 }
1433
1434 /* if we are waiting for more space, don't try to read more data
1435 * right now.
1436 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001437 if (sc->flags & (SC_FL_WONT_READ|SC_FL_NEED_BUFF|SC_FL_NEED_ROOM))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001438 break;
1439 } /* while !flags */
1440
1441 done_recv:
1442 if (cur_read) {
1443 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) &&
1444 (cur_read <= ic->buf.size / 2)) {
1445 ic->xfer_large = 0;
1446 ic->xfer_small++;
1447 if (ic->xfer_small >= 3) {
1448 /* we have read less than half of the buffer in
1449 * one pass, and this happened at least 3 times.
1450 * This is definitely not a streamer.
1451 */
1452 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
1453 }
1454 else if (ic->xfer_small >= 2) {
1455 /* if the buffer has been at least half full twice,
1456 * we receive faster than we send, so at least it
1457 * is not a "fast streamer".
1458 */
1459 ic->flags &= ~CF_STREAMER_FAST;
1460 }
1461 }
1462 else if (!(ic->flags & CF_STREAMER_FAST) &&
1463 (cur_read >= ic->buf.size - global.tune.maxrewrite)) {
1464 /* we read a full buffer at once */
1465 ic->xfer_small = 0;
1466 ic->xfer_large++;
1467 if (ic->xfer_large >= 3) {
1468 /* we call this buffer a fast streamer if it manages
1469 * to be filled in one call 3 consecutive times.
1470 */
1471 ic->flags |= (CF_STREAMER | CF_STREAMER_FAST);
1472 }
1473 }
1474 else {
1475 ic->xfer_small = 0;
1476 ic->xfer_large = 0;
1477 }
1478 ic->last_read = now_ms;
Christopher Faulet4c135682023-02-16 11:09:31 +01001479 sc_ep_report_read_activity(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001480 }
1481
1482 end_recv:
1483 ret = (cur_read != 0);
1484
1485 /* Report EOI on the channel if it was reached from the mux point of
1486 * view. */
Christopher Faulet904763f2023-03-22 14:53:11 +01001487 if (sc_ep_test(sc, SE_FL_EOI) && !(sc->flags & SC_FL_EOI)) {
Christopher Faulet4c135682023-02-16 11:09:31 +01001488 sc_ep_report_read_activity(sc);
Christopher Faulet904763f2023-03-22 14:53:11 +01001489 sc->flags |= SC_FL_EOI;
1490 ic->flags |= CF_READ_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001491 ret = 1;
1492 }
1493
Christopher Fauletb208d8c2023-03-21 11:25:21 +01001494 if (sc_ep_test(sc, SE_FL_EOS)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001495 /* we received a shutdown */
Christopher Faulet5e29b762022-04-04 08:58:34 +02001496 if (ic->flags & CF_AUTO_CLOSE)
Christopher Fauletdf7cd712023-04-13 15:56:26 +02001497 sc_schedule_shutdown(sc_opposite(sc));
Christopher Faulet1aec6c92023-04-17 17:29:29 +02001498 sc_conn_eos(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001499 ret = 1;
1500 }
Christopher Fauletb208d8c2023-03-21 11:25:21 +01001501
Christopher Fauleta1d14a72023-04-14 10:42:08 +02001502 if (sc_ep_test(sc, SE_FL_ERROR)) {
1503 sc->flags |= SC_FL_ERROR;
Christopher Fauletb208d8c2023-03-21 11:25:21 +01001504 ret = 1;
Christopher Fauleta1d14a72023-04-14 10:42:08 +02001505 }
Willy Tarreau0adb2812022-05-27 10:02:48 +02001506 else if (!(sc->flags & (SC_FL_WONT_READ|SC_FL_NEED_BUFF|SC_FL_NEED_ROOM)) &&
Christopher Fauletca5309a2023-04-17 16:17:32 +02001507 !(sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001508 /* Subscribe to receive events if we're blocking on I/O */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001509 conn->mux->subscribe(sc, SUB_RETRY_RECV, &sc->wait_event);
1510 se_have_no_more_data(sc->sedesc);
Christopher Fauletb208d8c2023-03-21 11:25:21 +01001511 }
1512 else {
Willy Tarreau0adb2812022-05-27 10:02:48 +02001513 se_have_more_data(sc->sedesc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001514 ret = 1;
1515 }
Christopher Faulet8019f782023-03-23 17:30:29 +01001516
Christopher Faulet5e29b762022-04-04 08:58:34 +02001517 return ret;
1518}
1519
Willy Tarreau4596fe22022-05-17 19:07:51 +02001520/* This tries to perform a synchronous receive on the stream connector to
Christopher Faulet5e29b762022-04-04 08:58:34 +02001521 * try to collect last arrived data. In practice it's only implemented on
Willy Tarreau4596fe22022-05-17 19:07:51 +02001522 * stconns. Returns 0 if nothing was done, non-zero if new data or a
Christopher Faulet5e29b762022-04-04 08:58:34 +02001523 * shutdown were collected. This may result on some delayed receive calls
1524 * to be programmed and performed later, though it doesn't provide any
1525 * such guarantee.
1526 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001527int sc_conn_sync_recv(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001528{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001529 if (!sc_state_in(sc->state, SC_SB_RDY|SC_SB_EST))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001530 return 0;
1531
Willy Tarreau0adb2812022-05-27 10:02:48 +02001532 if (!sc_mux_ops(sc))
Willy Tarreau4596fe22022-05-17 19:07:51 +02001533 return 0; // only stconns are supported
Christopher Faulet5e29b762022-04-04 08:58:34 +02001534
Willy Tarreau0adb2812022-05-27 10:02:48 +02001535 if (sc->wait_event.events & SUB_RETRY_RECV)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001536 return 0; // already subscribed
1537
Willy Tarreau0adb2812022-05-27 10:02:48 +02001538 if (!sc_is_recv_allowed(sc))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001539 return 0; // already failed
1540
Willy Tarreau0adb2812022-05-27 10:02:48 +02001541 return sc_conn_recv(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001542}
1543
1544/*
1545 * This function is called to send buffer data to a stream socket.
1546 * It calls the mux layer's snd_buf function. It relies on the
1547 * caller to commit polling changes. The caller should check conn->flags
1548 * for errors.
1549 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001550static int sc_conn_send(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001551{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001552 struct connection *conn = __sc_conn(sc);
Christopher Faulet904763f2023-03-22 14:53:11 +01001553 struct stconn *sco = sc_opposite(sc);
Willy Tarreau0adb2812022-05-27 10:02:48 +02001554 struct stream *s = __sc_strm(sc);
1555 struct channel *oc = sc_oc(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001556 int ret;
1557 int did_send = 0;
1558
Willy Tarreau0adb2812022-05-27 10:02:48 +02001559 if (sc_ep_test(sc, SE_FL_ERROR | SE_FL_ERR_PENDING) || sc_is_conn_error(sc)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001560 /* We're probably there because the tasklet was woken up,
1561 * but process_stream() ran before, detected there were an
Willy Tarreaue68bc612022-05-27 11:23:05 +02001562 * error and put the SC back to SC_ST_TAR. There's still
Christopher Faulet5e29b762022-04-04 08:58:34 +02001563 * CO_FL_ERROR on the connection but we don't want to add
Willy Tarreaub605c422022-05-17 17:04:55 +02001564 * SE_FL_ERROR back, so give up
Christopher Faulet5e29b762022-04-04 08:58:34 +02001565 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001566 if (sc->state < SC_ST_CON)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001567 return 0;
Christopher Faulet56a2b602023-04-14 09:42:59 +02001568 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 +02001569 return 1;
1570 }
1571
1572 /* We're already waiting to be able to send, give up */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001573 if (sc->wait_event.events & SUB_RETRY_SEND)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001574 return 0;
1575
1576 /* we might have been called just after an asynchronous shutw */
Christopher Faulet208c7122023-04-13 16:16:15 +02001577 if (sc->flags & SC_FL_SHUT_DONE)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001578 return 1;
1579
1580 /* we must wait because the mux is not installed yet */
1581 if (!conn->mux)
1582 return 0;
1583
1584 if (oc->pipe && conn->xprt->snd_pipe && conn->mux->snd_pipe) {
Willy Tarreau0adb2812022-05-27 10:02:48 +02001585 ret = conn->mux->snd_pipe(sc, oc->pipe);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001586 if (ret > 0)
1587 did_send = 1;
1588
1589 if (!oc->pipe->data) {
1590 put_pipe(oc->pipe);
1591 oc->pipe = NULL;
1592 }
1593
1594 if (oc->pipe)
1595 goto end;
1596 }
1597
1598 /* At this point, the pipe is empty, but we may still have data pending
1599 * in the normal buffer.
1600 */
1601 if (co_data(oc)) {
1602 /* when we're here, we already know that there is no spliced
1603 * data left, and that there are sendable buffered data.
1604 */
1605
1606 /* check if we want to inform the kernel that we're interested in
1607 * sending more data after this call. We want this if :
1608 * - we're about to close after this last send and want to merge
1609 * the ongoing FIN with the last segment.
1610 * - we know we can't send everything at once and must get back
1611 * here because of unaligned data
1612 * - there is still a finite amount of data to forward
1613 * The test is arranged so that the most common case does only 2
1614 * tests.
1615 */
1616 unsigned int send_flag = 0;
1617
Christopher Faulet68ef2182023-03-17 15:38:18 +01001618 if ((!(sc->flags & (SC_FL_SND_ASAP|SC_FL_SND_NEVERWAIT)) &&
Christopher Faulet5e29b762022-04-04 08:58:34 +02001619 ((oc->to_forward && oc->to_forward != CHN_INFINITE_FORWARD) ||
Christopher Faulet84d3ef92023-03-17 15:45:58 +01001620 (sc->flags & SC_FL_SND_EXP_MORE) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001621 (IS_HTX_STRM(s) &&
Christopher Fauletca5309a2023-04-17 16:17:32 +02001622 (!(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 +02001623 ((oc->flags & CF_ISRESP) &&
Christopher Faulet87633c32023-04-03 18:32:50 +02001624 (oc->flags & CF_AUTO_CLOSE) &&
Christopher Faulete38534c2023-04-13 15:45:24 +02001625 (sc->flags & SC_FL_SHUT_WANTED)))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001626 send_flag |= CO_SFL_MSG_MORE;
1627
1628 if (oc->flags & CF_STREAMER)
1629 send_flag |= CO_SFL_STREAMER;
1630
1631 if (s->txn && s->txn->flags & TX_L7_RETRY && !b_data(&s->txn->l7_buffer)) {
1632 /* If we want to be able to do L7 retries, copy
1633 * the data we're about to send, so that we are able
1634 * to resend them if needed
1635 */
1636 /* Try to allocate a buffer if we had none.
1637 * If it fails, the next test will just
1638 * disable the l7 retries by setting
1639 * l7_conn_retries to 0.
1640 */
1641 if (s->txn->req.msg_state != HTTP_MSG_DONE)
1642 s->txn->flags &= ~TX_L7_RETRY;
1643 else {
1644 if (b_alloc(&s->txn->l7_buffer) == NULL)
1645 s->txn->flags &= ~TX_L7_RETRY;
1646 else {
1647 memcpy(b_orig(&s->txn->l7_buffer),
1648 b_orig(&oc->buf),
1649 b_size(&oc->buf));
1650 s->txn->l7_buffer.head = co_data(oc);
1651 b_add(&s->txn->l7_buffer, co_data(oc));
1652 }
1653
1654 }
1655 }
1656
Willy Tarreau0adb2812022-05-27 10:02:48 +02001657 ret = conn->mux->snd_buf(sc, &oc->buf, co_data(oc), send_flag);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001658 if (ret > 0) {
1659 did_send = 1;
1660 c_rew(oc, ret);
1661 c_realign_if_empty(oc);
1662
1663 if (!co_data(oc)) {
1664 /* Always clear both flags once everything has been sent, they're one-shot */
Christopher Faulet84d3ef92023-03-17 15:45:58 +01001665 sc->flags &= ~(SC_FL_SND_ASAP|SC_FL_SND_EXP_MORE);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001666 }
1667 /* if some data remain in the buffer, it's only because the
1668 * system buffers are full, we will try next time.
1669 */
Christopher Faulet13045f02022-04-01 14:23:38 +02001670 }
1671 }
Christopher Faulet5e29b762022-04-04 08:58:34 +02001672
1673 end:
1674 if (did_send) {
Christopher Fauletd8988412022-12-20 18:10:04 +01001675 oc->flags |= CF_WRITE_EVENT | CF_WROTE_DATA;
Willy Tarreau0adb2812022-05-27 10:02:48 +02001676 if (sc->state == SC_ST_CON)
1677 sc->state = SC_ST_RDY;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001678 }
1679
Christopher Faulete7405d42023-05-05 11:40:30 +02001680 if (!sco->room_needed || (did_send && (sco->room_needed < 0 || channel_recv_max(sc_oc(sc)) >= sco->room_needed)))
1681 sc_have_room(sco);
1682
Willy Tarreau0adb2812022-05-27 10:02:48 +02001683 if (sc_ep_test(sc, SE_FL_ERROR | SE_FL_ERR_PENDING)) {
Christopher Faulet2e56a732023-01-26 16:18:09 +01001684 oc->flags |= CF_WRITE_EVENT;
Christopher Faulet56a2b602023-04-14 09:42:59 +02001685 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 +02001686 if (sc_ep_test(sc, SE_FL_ERROR))
1687 sc->flags |= SC_FL_ERROR;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001688 return 1;
1689 }
1690
Christopher Faulet59b240c2023-02-27 16:38:12 +01001691 if (channel_is_empty(oc))
1692 sc_ep_report_send_activity(sc);
1693 else {
1694 /* 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 +02001695 conn->mux->subscribe(sc, SUB_RETRY_SEND, &sc->wait_event);
Christopher Faulet59b240c2023-02-27 16:38:12 +01001696 sc_ep_report_blocked_send(sc);
1697 }
1698
Christopher Faulet5e29b762022-04-04 08:58:34 +02001699 return did_send;
1700}
1701
Christopher Fauletd8988412022-12-20 18:10:04 +01001702/* perform a synchronous send() for the stream connector. The CF_WRITE_EVENT
1703 * flag are cleared prior to the attempt, and will possibly be updated in case
1704 * of success.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001705 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001706void sc_conn_sync_send(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001707{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001708 struct channel *oc = sc_oc(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001709
Christopher Fauletd8988412022-12-20 18:10:04 +01001710 oc->flags &= ~CF_WRITE_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001711
Christopher Faulet208c7122023-04-13 16:16:15 +02001712 if (sc->flags & SC_FL_SHUT_DONE)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001713 return;
1714
1715 if (channel_is_empty(oc))
1716 return;
1717
Willy Tarreau0adb2812022-05-27 10:02:48 +02001718 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001719 return;
1720
Willy Tarreau0adb2812022-05-27 10:02:48 +02001721 if (!sc_mux_ops(sc))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001722 return;
1723
Willy Tarreau0adb2812022-05-27 10:02:48 +02001724 sc_conn_send(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001725}
1726
1727/* Called by I/O handlers after completion.. It propagates
Willy Tarreau4596fe22022-05-17 19:07:51 +02001728 * connection flags to the stream connector, updates the stream (which may or
Christopher Faulet5e29b762022-04-04 08:58:34 +02001729 * may not take this opportunity to try to forward data), then update the
Willy Tarreau4596fe22022-05-17 19:07:51 +02001730 * connection's polling based on the channels and stream connector's final
Christopher Faulet5e29b762022-04-04 08:58:34 +02001731 * states. The function always returns 0.
1732 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001733static int sc_conn_process(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001734{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001735 struct connection *conn = __sc_conn(sc);
1736 struct channel *ic = sc_ic(sc);
1737 struct channel *oc = sc_oc(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001738
1739 BUG_ON(!conn);
1740
1741 /* If we have data to send, try it now */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001742 if (!channel_is_empty(oc) && !(sc->wait_event.events & SUB_RETRY_SEND))
1743 sc_conn_send(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001744
Willy Tarreau4596fe22022-05-17 19:07:51 +02001745 /* First step, report to the stream connector what was detected at the
Christopher Faulet5e29b762022-04-04 08:58:34 +02001746 * connection layer : errors and connection establishment.
Christopher Faulet88d05a02023-04-14 12:03:50 +02001747 * Only add SC_FL_ERROR if we're connected, or we're attempting to
Christopher Faulet5e29b762022-04-04 08:58:34 +02001748 * connect, we may get there because we got woken up, but only run
1749 * after process_stream() noticed there were an error, and decided
1750 * to retry to connect, the connection may still have CO_FL_ERROR,
Christopher Faulet88d05a02023-04-14 12:03:50 +02001751 * and we don't want to add SC_FL_ERROR back
Christopher Faulet5e29b762022-04-04 08:58:34 +02001752 *
Willy Tarreau462b9892022-05-18 18:06:53 +02001753 * Note: This test is only required because sc_conn_process is also the SI
1754 * wake callback. Otherwise sc_conn_recv()/sc_conn_send() already take
Christopher Faulet5e29b762022-04-04 08:58:34 +02001755 * care of it.
1756 */
1757
Willy Tarreau0adb2812022-05-27 10:02:48 +02001758 if (sc->state >= SC_ST_CON) {
Christopher Faulet88d05a02023-04-14 12:03:50 +02001759 if (sc_is_conn_error(sc))
Christopher Fauleta1d14a72023-04-14 10:42:08 +02001760 sc->flags |= SC_FL_ERROR;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001761 }
1762
1763 /* If we had early data, and the handshake ended, then
1764 * we can remove the flag, and attempt to wake the task up,
1765 * in the event there's an analyser waiting for the end of
1766 * the handshake.
1767 */
1768 if (!(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS)) &&
Willy Tarreau0adb2812022-05-27 10:02:48 +02001769 sc_ep_test(sc, SE_FL_WAIT_FOR_HS)) {
1770 sc_ep_clr(sc, SE_FL_WAIT_FOR_HS);
1771 task_wakeup(sc_strm_task(sc), TASK_WOKEN_MSG);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001772 }
1773
Willy Tarreau0adb2812022-05-27 10:02:48 +02001774 if (!sc_state_in(sc->state, SC_SB_EST|SC_SB_DIS|SC_SB_CLO) &&
Christopher Faulet5e29b762022-04-04 08:58:34 +02001775 (conn->flags & CO_FL_WAIT_XPRT) == 0) {
Christopher Fauletca679922022-07-20 13:24:04 +02001776 if (sc->flags & SC_FL_ISBACK)
1777 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Fauletb96f2aa2022-12-12 08:11:36 +01001778 oc->flags |= CF_WRITE_EVENT;
Willy Tarreau0adb2812022-05-27 10:02:48 +02001779 if (sc->state == SC_ST_CON)
1780 sc->state = SC_ST_RDY;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001781 }
1782
1783 /* Report EOS on the channel if it was reached from the mux point of
1784 * view.
1785 *
Willy Tarreau462b9892022-05-18 18:06:53 +02001786 * Note: This test is only required because sc_conn_process is also the SI
1787 * wake callback. Otherwise sc_conn_recv()/sc_conn_send() already take
Christopher Faulet5e29b762022-04-04 08:58:34 +02001788 * care of it.
1789 */
Christopher Faulet1aec6c92023-04-17 17:29:29 +02001790 if (sc_ep_test(sc, SE_FL_EOS) && !(sc->flags & SC_FL_EOS)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001791 /* we received a shutdown */
Christopher Faulet5e29b762022-04-04 08:58:34 +02001792 if (ic->flags & CF_AUTO_CLOSE)
Christopher Fauletdf7cd712023-04-13 15:56:26 +02001793 sc_schedule_shutdown(sc_opposite(sc));
Christopher Faulet1aec6c92023-04-17 17:29:29 +02001794 sc_conn_eos(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001795 }
1796
1797 /* Report EOI on the channel if it was reached from the mux point of
1798 * view.
1799 *
Willy Tarreau462b9892022-05-18 18:06:53 +02001800 * Note: This test is only required because sc_conn_process is also the SI
1801 * wake callback. Otherwise sc_conn_recv()/sc_conn_send() already take
Christopher Faulet5e29b762022-04-04 08:58:34 +02001802 * care of it.
1803 */
Christopher Faulet904763f2023-03-22 14:53:11 +01001804 if (sc_ep_test(sc, SE_FL_EOI) && !(sc->flags & SC_FL_EOI)) {
1805 sc->flags |= SC_FL_EOI;
1806 ic->flags |= CF_READ_EVENT;
1807 }
Christopher Faulet5e29b762022-04-04 08:58:34 +02001808
Christopher Fauleta1d14a72023-04-14 10:42:08 +02001809 if (sc_ep_test(sc, SE_FL_ERROR))
1810 sc->flags |= SC_FL_ERROR;
1811
Willy Tarreau4596fe22022-05-17 19:07:51 +02001812 /* Second step : update the stream connector and channels, try to forward any
Christopher Faulet5e29b762022-04-04 08:58:34 +02001813 * pending data, then possibly wake the stream up based on the new
Willy Tarreau4596fe22022-05-17 19:07:51 +02001814 * stream connector status.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001815 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001816 sc_notify(sc);
1817 stream_release_buffers(__sc_strm(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001818 return 0;
1819}
1820
Willy Tarreau4596fe22022-05-17 19:07:51 +02001821/* This is the ->process() function for any stream connector's wait_event task.
1822 * It's assigned during the stream connector's initialization, for any type of
1823 * stream connector. Thus it is always safe to perform a tasklet_wakeup() on a
Willy Tarreaue68bc612022-05-27 11:23:05 +02001824 * stream connector, as the presence of the SC is checked there.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001825 */
Willy Tarreau462b9892022-05-18 18:06:53 +02001826struct task *sc_conn_io_cb(struct task *t, void *ctx, unsigned int state)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001827{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001828 struct stconn *sc = ctx;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001829 int ret = 0;
1830
Willy Tarreau0adb2812022-05-27 10:02:48 +02001831 if (!sc_conn(sc))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001832 return t;
1833
Willy Tarreau0adb2812022-05-27 10:02:48 +02001834 if (!(sc->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(sc_oc(sc)))
1835 ret = sc_conn_send(sc);
1836 if (!(sc->wait_event.events & SUB_RETRY_RECV))
1837 ret |= sc_conn_recv(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001838 if (ret != 0)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001839 sc_conn_process(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001840
Willy Tarreau0adb2812022-05-27 10:02:48 +02001841 stream_release_buffers(__sc_strm(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001842 return t;
1843}
1844
Christopher Fauletb36e5122023-04-17 17:32:43 +02001845/*
1846 * This function propagates an end-of-stream received from an applet. It
1847 * updates the stream connector. If it is is already shut, the applet is
1848 * released. Otherwise, we try to forward the shutdown, immediately or ASAP.
1849 */
1850static void sc_applet_eos(struct stconn *sc)
1851{
1852 struct channel *ic = sc_ic(sc);
1853
1854 BUG_ON(!sc_appctx(sc));
1855
1856 if (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))
1857 return;
1858 sc->flags |= SC_FL_EOS;
1859 ic->flags |= CF_READ_EVENT;
1860
1861 /* Note: on abort, we don't call the applet */
1862
1863 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
1864 return;
1865
1866 if (sc->flags & SC_FL_SHUT_DONE) {
1867 appctx_shut(__sc_appctx(sc));
1868 sc->state = SC_ST_DIS;
1869 if (sc->flags & SC_FL_ISBACK)
1870 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
1871 }
1872 else if (sc_cond_forward_shut(sc))
1873 return sc_app_shut_applet(sc);
1874}
1875
Christopher Faulet5e29b762022-04-04 08:58:34 +02001876/* Callback to be used by applet handlers upon completion. It updates the stream
1877 * (which may or may not take this opportunity to try to forward data), then
Willy Tarreau4596fe22022-05-17 19:07:51 +02001878 * may re-enable the applet's based on the channels and stream connector's final
Christopher Faulet5e29b762022-04-04 08:58:34 +02001879 * states.
1880 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001881static int sc_applet_process(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001882{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001883 struct channel *ic = sc_ic(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001884
Willy Tarreau0adb2812022-05-27 10:02:48 +02001885 BUG_ON(!sc_appctx(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001886
Christopher Fauletf8fbb6d2023-03-21 11:49:21 +01001887 /* Report EOI on the channel if it was reached from the applet point of
1888 * view. */
Christopher Faulet904763f2023-03-22 14:53:11 +01001889 if (sc_ep_test(sc, SE_FL_EOI) && !(sc->flags & SC_FL_EOI)) {
Christopher Fauletf8fbb6d2023-03-21 11:49:21 +01001890 sc_ep_report_read_activity(sc);
Christopher Faulet904763f2023-03-22 14:53:11 +01001891 sc->flags |= SC_FL_EOI;
1892 ic->flags |= CF_READ_EVENT;
Christopher Fauletf8fbb6d2023-03-21 11:49:21 +01001893 }
1894
Christopher Fauleta1d14a72023-04-14 10:42:08 +02001895 if (sc_ep_test(sc, SE_FL_ERROR))
1896 sc->flags |= SC_FL_ERROR;
1897
Christopher Faulet0ffc9d72023-03-21 14:19:08 +01001898 if (sc_ep_test(sc, SE_FL_EOS)) {
1899 /* we received a shutdown */
Christopher Fauletb36e5122023-04-17 17:32:43 +02001900 sc_applet_eos(sc);
Christopher Faulet0ffc9d72023-03-21 14:19:08 +01001901 }
1902
Christopher Faulete8bcef52023-04-14 09:45:41 +02001903 BUG_ON(sc_ep_test(sc, SE_FL_HAVE_NO_DATA|SE_FL_EOI) == SE_FL_EOI);
1904
Christopher Faulet5e29b762022-04-04 08:58:34 +02001905 /* If the applet wants to write and the channel is closed, it's a
1906 * broken pipe and it must be reported.
1907 */
Christopher Fauletca5309a2023-04-17 16:17:32 +02001908 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 +02001909 sc_ep_set(sc, SE_FL_ERROR);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001910
1911 /* automatically mark the applet having data available if it reported
1912 * begin blocked by the channel.
1913 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001914 if ((sc->flags & (SC_FL_WONT_READ|SC_FL_NEED_BUFF|SC_FL_NEED_ROOM)) ||
1915 sc_ep_test(sc, SE_FL_APPLET_NEED_CONN))
1916 applet_have_more_data(__sc_appctx(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001917
Willy Tarreau4596fe22022-05-17 19:07:51 +02001918 /* update the stream connector, channels, and possibly wake the stream up */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001919 sc_notify(sc);
1920 stream_release_buffers(__sc_strm(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001921
Willy Tarreau19c65a92022-05-27 08:49:24 +02001922 /* sc_notify may have passed through chk_snd and released some blocking
Willy Tarreau15252cd2022-05-25 16:36:21 +02001923 * flags. Process_stream will consider those flags to wake up the
Christopher Faulet5e29b762022-04-04 08:58:34 +02001924 * appctx but in the case the task is not in runqueue we may have to
1925 * wakeup the appctx immediately.
1926 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001927 if (sc_is_recv_allowed(sc) || sc_is_send_allowed(sc))
1928 appctx_wakeup(__sc_appctx(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001929 return 0;
Christopher Faulet13045f02022-04-01 14:23:38 +02001930}
Christopher Fauletb68f77d2022-06-16 16:24:16 +02001931
1932
1933/* Prepares an endpoint upgrade. We don't now at this stage if the upgrade will
1934 * succeed or not and if the stconn will be reused by the new endpoint. Thus,
1935 * for now, only pretend the stconn is detached.
1936 */
1937void sc_conn_prepare_endp_upgrade(struct stconn *sc)
1938{
1939 BUG_ON(!sc_conn(sc) || !sc->app);
1940 sc_ep_clr(sc, SE_FL_T_MUX);
1941 sc_ep_set(sc, SE_FL_DETACHED);
1942}
1943
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001944/* Endpoint upgrade failed. Restore the stconn state. */
Christopher Fauletb68f77d2022-06-16 16:24:16 +02001945void sc_conn_abort_endp_upgrade(struct stconn *sc)
1946{
1947 sc_ep_set(sc, SE_FL_T_MUX);
1948 sc_ep_clr(sc, SE_FL_DETACHED);
1949}
1950
1951/* Commit the endpoint upgrade. If stconn is attached, it means the new endpoint
1952 * use it. So we do nothing. Otherwise, the stconn will be destroy with the
1953 * overlying stream. So, it means we must commit the detach.
1954*/
1955void sc_conn_commit_endp_upgrade(struct stconn *sc)
1956{
1957 if (!sc_ep_test(sc, SE_FL_DETACHED))
1958 return;
1959 sc_detach_endp(&sc);
1960 /* Because it was already set as detached, the sedesc must be preserved */
Willy Tarreau6a378d12022-08-11 13:56:42 +02001961 BUG_ON(!sc);
Christopher Fauletb68f77d2022-06-16 16:24:16 +02001962 BUG_ON(!sc->sedesc);
1963}