blob: 39dcbb5f0cea022d6d7075378fbaaef8790e0648 [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 Faulet1329f2a2021-12-16 17:32:56 +010022
Willy Tarreau4596fe22022-05-17 19:07:51 +020023DECLARE_POOL(pool_head_connstream, "stconn", sizeof(struct stconn));
Willy Tarreauea59b022022-05-17 17:53:22 +020024DECLARE_POOL(pool_head_sedesc, "sedesc", sizeof(struct sedesc));
Christopher Faulet1329f2a2021-12-16 17:32:56 +010025
Willy Tarreau3a3f4802022-05-17 18:28:19 +020026/* functions used by default on a detached stream connector */
Christopher Fauletcfc11c02023-04-13 16:10:23 +020027static void sc_app_abort(struct stconn *sc);
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +020028static void sc_app_shut(struct stconn *sc);
Willy Tarreau0adb2812022-05-27 10:02:48 +020029static void sc_app_chk_rcv(struct stconn *sc);
30static void sc_app_chk_snd(struct stconn *sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +020031
Willy Tarreau3a3f4802022-05-17 18:28:19 +020032/* functions used on a mux-based stream connector */
Christopher Fauletcfc11c02023-04-13 16:10:23 +020033static void sc_app_abort_conn(struct stconn *sc);
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +020034static void sc_app_shut_conn(struct stconn *sc);
Willy Tarreau0adb2812022-05-27 10:02:48 +020035static void sc_app_chk_rcv_conn(struct stconn *sc);
36static void sc_app_chk_snd_conn(struct stconn *sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +020037
Willy Tarreau3a3f4802022-05-17 18:28:19 +020038/* functions used on an applet-based stream connector */
Christopher Fauletcfc11c02023-04-13 16:10:23 +020039static void sc_app_abort_applet(struct stconn *sc);
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +020040static void sc_app_shut_applet(struct stconn *sc);
Willy Tarreau0adb2812022-05-27 10:02:48 +020041static void sc_app_chk_rcv_applet(struct stconn *sc);
42static void sc_app_chk_snd_applet(struct stconn *sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +020043
Willy Tarreau0adb2812022-05-27 10:02:48 +020044static int sc_conn_process(struct stconn *sc);
45static int sc_conn_recv(struct stconn *sc);
46static int sc_conn_send(struct stconn *sc);
47static int sc_applet_process(struct stconn *sc);
Willy Tarreau2f2318d2022-05-18 10:17:16 +020048
Willy Tarreau3a3f4802022-05-17 18:28:19 +020049/* stream connector operations for connections */
50struct sc_app_ops sc_app_conn_ops = {
51 .chk_rcv = sc_app_chk_rcv_conn,
52 .chk_snd = sc_app_chk_snd_conn,
Christopher Fauletcfc11c02023-04-13 16:10:23 +020053 .abort = sc_app_abort_conn,
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +020054 .shutdown= sc_app_shut_conn,
Willy Tarreau462b9892022-05-18 18:06:53 +020055 .wake = sc_conn_process,
Willy Tarreau2f2318d2022-05-18 10:17:16 +020056 .name = "STRM",
Christopher Faulet9ffddd52022-04-01 14:04:29 +020057};
58
Willy Tarreau3a3f4802022-05-17 18:28:19 +020059/* stream connector operations for embedded tasks */
60struct sc_app_ops sc_app_embedded_ops = {
61 .chk_rcv = sc_app_chk_rcv,
62 .chk_snd = sc_app_chk_snd,
Christopher Fauletcfc11c02023-04-13 16:10:23 +020063 .abort = sc_app_abort,
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +020064 .shutdown= sc_app_shut,
Willy Tarreau2f2318d2022-05-18 10:17:16 +020065 .wake = NULL, /* may never be used */
66 .name = "NONE", /* may never be used */
Christopher Faulet9ffddd52022-04-01 14:04:29 +020067};
68
Willy Tarreau2f2318d2022-05-18 10:17:16 +020069/* stream connector operations for applets */
Willy Tarreau3a3f4802022-05-17 18:28:19 +020070struct sc_app_ops sc_app_applet_ops = {
71 .chk_rcv = sc_app_chk_rcv_applet,
72 .chk_snd = sc_app_chk_snd_applet,
Christopher Fauletcfc11c02023-04-13 16:10:23 +020073 .abort = sc_app_abort_applet,
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +020074 .shutdown= sc_app_shut_applet,
Willy Tarreau19c65a92022-05-27 08:49:24 +020075 .wake = sc_applet_process,
Christopher Faulet5e29b762022-04-04 08:58:34 +020076 .name = "STRM",
77};
78
Willy Tarreau2f2318d2022-05-18 10:17:16 +020079/* stream connector for health checks on connections */
80struct sc_app_ops sc_app_check_ops = {
81 .chk_rcv = NULL,
82 .chk_snd = NULL,
Christopher Fauletcfc11c02023-04-13 16:10:23 +020083 .abort = NULL,
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +020084 .shutdown= NULL,
Willy Tarreau2f2318d2022-05-18 10:17:16 +020085 .wake = wake_srv_chk,
86 .name = "CHCK",
87};
Christopher Faulet5e29b762022-04-04 08:58:34 +020088
Christopher Faulet9ed77422022-04-12 08:51:15 +020089/* Initializes an endpoint */
Willy Tarreauea59b022022-05-17 17:53:22 +020090void sedesc_init(struct sedesc *sedesc)
Christopher Fauletdb90f2a2022-03-22 16:06:25 +010091{
Willy Tarreauea59b022022-05-17 17:53:22 +020092 sedesc->se = NULL;
93 sedesc->conn = NULL;
Willy Tarreauc1054922022-05-18 07:43:52 +020094 sedesc->sc = NULL;
Christopher Faulet4c135682023-02-16 11:09:31 +010095 sedesc->lra = TICK_ETERNITY;
96 sedesc->fsb = TICK_ETERNITY;
Willy Tarreauea59b022022-05-17 17:53:22 +020097 se_fl_setall(sedesc, SE_FL_NONE);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +010098}
99
Christopher Faulet9ed77422022-04-12 08:51:15 +0200100/* Tries to alloc an endpoint and initialize it. Returns NULL on failure. */
Willy Tarreauea59b022022-05-17 17:53:22 +0200101struct sedesc *sedesc_new()
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100102{
Willy Tarreauea59b022022-05-17 17:53:22 +0200103 struct sedesc *sedesc;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100104
Willy Tarreauea59b022022-05-17 17:53:22 +0200105 sedesc = pool_alloc(pool_head_sedesc);
106 if (unlikely(!sedesc))
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100107 return NULL;
108
Willy Tarreauea59b022022-05-17 17:53:22 +0200109 sedesc_init(sedesc);
110 return sedesc;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100111}
112
Christopher Faulet9ed77422022-04-12 08:51:15 +0200113/* Releases an endpoint. It is the caller responsibility to be sure it is safe
114 * and it is not shared with another entity
115 */
Willy Tarreauea59b022022-05-17 17:53:22 +0200116void sedesc_free(struct sedesc *sedesc)
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100117{
Willy Tarreauea59b022022-05-17 17:53:22 +0200118 pool_free(pool_head_sedesc, sedesc);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100119}
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100120
Willy Tarreau4596fe22022-05-17 19:07:51 +0200121/* Tries to allocate a new stconn and initialize its main fields. On
Christopher Faulet9ed77422022-04-12 08:51:15 +0200122 * failure, nothing is allocated and NULL is returned. It is an internal
Willy Tarreaub605c422022-05-17 17:04:55 +0200123 * function. The caller must, at least, set the SE_FL_ORPHAN or SE_FL_DETACHED
Christopher Faulet9ed77422022-04-12 08:51:15 +0200124 * flag.
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100125 */
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200126static struct stconn *sc_new(struct sedesc *sedesc)
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100127{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200128 struct stconn *sc;
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100129
Willy Tarreau0adb2812022-05-27 10:02:48 +0200130 sc = pool_alloc(pool_head_connstream);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100131
Willy Tarreau0adb2812022-05-27 10:02:48 +0200132 if (unlikely(!sc))
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100133 goto alloc_error;
Christopher Fauletbb772d02022-03-22 15:28:36 +0100134
Willy Tarreau1d2c79a2022-05-27 11:15:19 +0200135 sc->obj_type = OBJ_TYPE_SC;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200136 sc->flags = SC_FL_NONE;
137 sc->state = SC_ST_INI;
Christopher Fauletbe5cc762023-02-20 08:41:55 +0100138 sc->ioto = TICK_ETERNITY;
Christopher Faulet9aed1122023-05-05 11:25:19 +0200139 sc->room_needed = 0;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200140 sc->app = NULL;
141 sc->app_ops = NULL;
142 sc->src = NULL;
143 sc->dst = NULL;
144 sc->wait_event.tasklet = NULL;
145 sc->wait_event.events = 0;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200146
Christopher Faulet9ed77422022-04-12 08:51:15 +0200147 /* If there is no endpoint, allocate a new one now */
Willy Tarreauea59b022022-05-17 17:53:22 +0200148 if (!sedesc) {
149 sedesc = sedesc_new();
150 if (unlikely(!sedesc))
Christopher Fauletb669d682022-03-22 18:37:19 +0100151 goto alloc_error;
152 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200153 sc->sedesc = sedesc;
154 sedesc->sc = sc;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100155
Willy Tarreau0adb2812022-05-27 10:02:48 +0200156 return sc;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100157
158 alloc_error:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200159 pool_free(pool_head_connstream, sc);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100160 return NULL;
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100161}
162
Willy Tarreau31219282022-05-27 16:21:33 +0200163/* Creates a new stream connector and its associated stream from a mux. <sd> must
164 * be defined. It returns NULL on error. On success, the new stream connector is
Willy Tarreaub605c422022-05-17 17:04:55 +0200165 * returned. In this case, SE_FL_ORPHAN flag is removed.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200166 */
Willy Tarreau31219282022-05-27 16:21:33 +0200167struct stconn *sc_new_from_endp(struct sedesc *sd, struct session *sess, struct buffer *input)
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100168{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200169 struct stconn *sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100170
Willy Tarreau31219282022-05-27 16:21:33 +0200171 sc = sc_new(sd);
Willy Tarreau0adb2812022-05-27 10:02:48 +0200172 if (unlikely(!sc))
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100173 return NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200174 if (unlikely(!stream_new(sess, sc, input))) {
Christopher Faulet3ab72c62022-09-27 09:18:20 +0200175 sd->sc = NULL;
Willy Tarreau7a8ca0a2023-03-20 19:53:14 +0100176 if (sc->sedesc != sd) {
177 /* none was provided so sc_new() allocated one */
178 sedesc_free(sc->sedesc);
179 }
180 pool_free(pool_head_connstream, sc);
Christopher Faulet3ab72c62022-09-27 09:18:20 +0200181 se_fl_set(sd, SE_FL_ORPHAN);
182 return NULL;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100183 }
Willy Tarreau31219282022-05-27 16:21:33 +0200184 se_fl_clr(sd, SE_FL_ORPHAN);
Willy Tarreau0adb2812022-05-27 10:02:48 +0200185 return sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100186}
187
Willy Tarreau4596fe22022-05-17 19:07:51 +0200188/* Creates a new stream connector from an stream. There is no endpoint here, thus it
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200189 * will be created by sc_new(). So the SE_FL_DETACHED flag is set. It returns
Willy Tarreau4596fe22022-05-17 19:07:51 +0200190 * NULL on error. On success, the new stream connector is returned.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200191 */
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200192struct stconn *sc_new_from_strm(struct stream *strm, unsigned int flags)
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100193{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200194 struct stconn *sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100195
Willy Tarreau0adb2812022-05-27 10:02:48 +0200196 sc = sc_new(NULL);
197 if (unlikely(!sc))
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100198 return NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200199 sc->flags |= flags;
200 sc_ep_set(sc, SE_FL_DETACHED);
201 sc->app = &strm->obj_type;
202 sc->app_ops = &sc_app_embedded_ops;
203 return sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100204}
205
Willy Tarreau4596fe22022-05-17 19:07:51 +0200206/* Creates a new stream connector from an health-check. There is no endpoint here,
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200207 * thus it will be created by sc_new(). So the SE_FL_DETACHED flag is set. It
Willy Tarreau4596fe22022-05-17 19:07:51 +0200208 * returns NULL on error. On success, the new stream connector is returned.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200209 */
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200210struct stconn *sc_new_from_check(struct check *check, unsigned int flags)
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100211{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200212 struct stconn *sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100213
Willy Tarreau0adb2812022-05-27 10:02:48 +0200214 sc = sc_new(NULL);
215 if (unlikely(!sc))
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100216 return NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200217 sc->flags |= flags;
218 sc_ep_set(sc, SE_FL_DETACHED);
219 sc->app = &check->obj_type;
220 sc->app_ops = &sc_app_check_ops;
221 return sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100222}
223
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200224/* Releases a stconn previously allocated by sc_new(), as well as its
Christopher Faulet9ed77422022-04-12 08:51:15 +0200225 * endpoint, if it exists. This function is called internally or on error path.
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100226 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200227void sc_free(struct stconn *sc)
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100228{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200229 sockaddr_free(&sc->src);
230 sockaddr_free(&sc->dst);
231 if (sc->sedesc) {
232 BUG_ON(!sc_ep_test(sc, SE_FL_DETACHED));
233 sedesc_free(sc->sedesc);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100234 }
Tim Duesterhusb1ec21d2023-04-22 17:47:32 +0200235 tasklet_free(sc->wait_event.tasklet);
Willy Tarreau0adb2812022-05-27 10:02:48 +0200236 pool_free(pool_head_connstream, sc);
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100237}
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100238
Willy Tarreau4596fe22022-05-17 19:07:51 +0200239/* Conditionally removes a stream connector if it is detached and if there is no app
Christopher Fauleteb50c012022-04-21 14:22:53 +0200240 * layer defined. Except on error path, this one must be used. if release, the
Willy Tarreaue68bc612022-05-27 11:23:05 +0200241 * pointer on the SC is set to NULL.
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200242 */
Willy Tarreaue68bc612022-05-27 11:23:05 +0200243static void sc_free_cond(struct stconn **scp)
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200244{
Willy Tarreaue68bc612022-05-27 11:23:05 +0200245 struct stconn *sc = *scp;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200246
Willy Tarreau0adb2812022-05-27 10:02:48 +0200247 if (!sc->app && (!sc->sedesc || sc_ep_test(sc, SE_FL_DETACHED))) {
248 sc_free(sc);
Willy Tarreaue68bc612022-05-27 11:23:05 +0200249 *scp = NULL;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200250 }
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200251}
252
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100253
Willy Tarreau4596fe22022-05-17 19:07:51 +0200254/* Attaches a stconn to a mux endpoint and sets the endpoint ctx. Returns
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500255 * -1 on error and 0 on success. SE_FL_DETACHED flag is removed. This function is
Christopher Faulet9ed77422022-04-12 08:51:15 +0200256 * called from a mux when it is attached to a stream or a health-check.
257 */
Willy Tarreau31219282022-05-27 16:21:33 +0200258int sc_attach_mux(struct stconn *sc, void *sd, void *ctx)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100259{
Christopher Faulet93882042022-01-19 14:56:50 +0100260 struct connection *conn = ctx;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200261 struct sedesc *sedesc = sc->sedesc;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100262
Willy Tarreau0adb2812022-05-27 10:02:48 +0200263 if (sc_strm(sc)) {
264 if (!sc->wait_event.tasklet) {
265 sc->wait_event.tasklet = tasklet_new();
266 if (!sc->wait_event.tasklet)
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200267 return -1;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200268 sc->wait_event.tasklet->process = sc_conn_io_cb;
269 sc->wait_event.tasklet->context = sc;
270 sc->wait_event.events = 0;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200271 }
272
Willy Tarreau0adb2812022-05-27 10:02:48 +0200273 sc->app_ops = &sc_app_conn_ops;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100274 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200275 else if (sc_check(sc)) {
276 if (!sc->wait_event.tasklet) {
277 sc->wait_event.tasklet = tasklet_new();
278 if (!sc->wait_event.tasklet)
Christopher Fauletc95eaef2022-05-18 15:57:15 +0200279 return -1;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200280 sc->wait_event.tasklet->process = srv_chk_io_cb;
281 sc->wait_event.tasklet->context = sc;
282 sc->wait_event.events = 0;
Christopher Fauletc95eaef2022-05-18 15:57:15 +0200283 }
284
Willy Tarreau0adb2812022-05-27 10:02:48 +0200285 sc->app_ops = &sc_app_check_ops;
Christopher Fauletc95eaef2022-05-18 15:57:15 +0200286 }
Willy Tarreaue2f79462023-03-20 19:45:41 +0100287
288 sedesc->se = sd;
289 sedesc->conn = ctx;
290 se_fl_set(sedesc, SE_FL_T_MUX);
291 se_fl_clr(sedesc, SE_FL_DETACHED);
292 if (!conn->ctx)
293 conn->ctx = sc;
Christopher Faulet070b91b2022-03-31 19:27:18 +0200294 return 0;
Christopher Faulet93882042022-01-19 14:56:50 +0100295}
296
Willy Tarreau4596fe22022-05-17 19:07:51 +0200297/* Attaches a stconn to an applet endpoint and sets the endpoint
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500298 * ctx. Returns -1 on error and 0 on success. SE_FL_DETACHED flag is
Christopher Faulet9ed77422022-04-12 08:51:15 +0200299 * removed. This function is called by a stream when a backend applet is
300 * registered.
301 */
Willy Tarreau31219282022-05-27 16:21:33 +0200302static void sc_attach_applet(struct stconn *sc, void *sd)
Christopher Faulet93882042022-01-19 14:56:50 +0100303{
Willy Tarreau31219282022-05-27 16:21:33 +0200304 sc->sedesc->se = sd;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200305 sc_ep_set(sc, SE_FL_T_APPLET);
306 sc_ep_clr(sc, SE_FL_DETACHED);
307 if (sc_strm(sc))
308 sc->app_ops = &sc_app_applet_ops;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100309}
310
Willy Tarreau4596fe22022-05-17 19:07:51 +0200311/* Attaches a stconn to a app layer and sets the relevant
Willy Tarreaub605c422022-05-17 17:04:55 +0200312 * callbacks. Returns -1 on error and 0 on success. SE_FL_ORPHAN flag is
Christopher Faulet9ed77422022-04-12 08:51:15 +0200313 * removed. This function is called by a stream when it is created to attach it
Willy Tarreau4596fe22022-05-17 19:07:51 +0200314 * on the stream connector on the client side.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200315 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200316int sc_attach_strm(struct stconn *sc, struct stream *strm)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100317{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200318 sc->app = &strm->obj_type;
319 sc_ep_clr(sc, SE_FL_ORPHAN);
320 if (sc_ep_test(sc, SE_FL_T_MUX)) {
321 sc->wait_event.tasklet = tasklet_new();
322 if (!sc->wait_event.tasklet)
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200323 return -1;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200324 sc->wait_event.tasklet->process = sc_conn_io_cb;
325 sc->wait_event.tasklet->context = sc;
326 sc->wait_event.events = 0;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200327
Willy Tarreau0adb2812022-05-27 10:02:48 +0200328 sc->app_ops = &sc_app_conn_ops;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100329 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200330 else if (sc_ep_test(sc, SE_FL_T_APPLET)) {
331 sc->app_ops = &sc_app_applet_ops;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100332 }
333 else {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200334 sc->app_ops = &sc_app_embedded_ops;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100335 }
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100336 return 0;
337}
338
Willy Tarreau4596fe22022-05-17 19:07:51 +0200339/* Detaches the stconn from the endpoint, if any. For a connecrion, if a
Christopher Faulet9ed77422022-04-12 08:51:15 +0200340 * mux owns the connection ->detach() callback is called. Otherwise, it means
Willy Tarreau4596fe22022-05-17 19:07:51 +0200341 * the stream connector owns the connection. In this case the connection is closed
Christopher Faulet9ed77422022-04-12 08:51:15 +0200342 * and released. For an applet, the appctx is released. If still allocated, the
343 * endpoint is reset and flag as detached. If the app layer is also detached,
Willy Tarreau4596fe22022-05-17 19:07:51 +0200344 * the stream connector is released.
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100345 */
Willy Tarreaue68bc612022-05-27 11:23:05 +0200346static void sc_detach_endp(struct stconn **scp)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100347{
Willy Tarreaue68bc612022-05-27 11:23:05 +0200348 struct stconn *sc = *scp;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200349
Willy Tarreau0adb2812022-05-27 10:02:48 +0200350 if (!sc)
Christopher Fauleteb50c012022-04-21 14:22:53 +0200351 return;
352
Willy Tarreau0adb2812022-05-27 10:02:48 +0200353 if (sc_ep_test(sc, SE_FL_T_MUX)) {
354 struct connection *conn = __sc_conn(sc);
355 struct sedesc *sedesc = sc->sedesc;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100356
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100357 if (conn->mux) {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200358 if (sc->wait_event.events != 0)
359 conn->mux->unsubscribe(sc, sc->wait_event.events, &sc->wait_event);
Willy Tarreau798465b2022-05-17 18:20:02 +0200360 se_fl_set(sedesc, SE_FL_ORPHAN);
Willy Tarreauc1054922022-05-18 07:43:52 +0200361 sedesc->sc = NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200362 sc->sedesc = NULL;
Willy Tarreau798465b2022-05-17 18:20:02 +0200363 conn->mux->detach(sedesc);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100364 }
365 else {
366 /* It's too early to have a mux, let's just destroy
367 * the connection
368 */
369 conn_stop_tracking(conn);
370 conn_full_close(conn);
371 if (conn->destroy_cb)
372 conn->destroy_cb(conn);
373 conn_free(conn);
374 }
375 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200376 else if (sc_ep_test(sc, SE_FL_T_APPLET)) {
377 struct appctx *appctx = __sc_appctx(sc);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100378
Willy Tarreau0adb2812022-05-27 10:02:48 +0200379 sc_ep_set(sc, SE_FL_ORPHAN);
380 sc->sedesc->sc = NULL;
381 sc->sedesc = NULL;
Willy Tarreau1c3ead42022-05-10 19:42:22 +0200382 appctx_shut(appctx);
383 appctx_free(appctx);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100384 }
385
Willy Tarreau0adb2812022-05-27 10:02:48 +0200386 if (sc->sedesc) {
Willy Tarreauda59c892022-05-27 17:03:34 +0200387 /* the SD wasn't used and can be recycled */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200388 sc->sedesc->se = NULL;
389 sc->sedesc->conn = NULL;
Willy Tarreauda59c892022-05-27 17:03:34 +0200390 sc->sedesc->flags = 0;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200391 sc_ep_set(sc, SE_FL_DETACHED);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100392 }
393
Willy Tarreaue68bc612022-05-27 11:23:05 +0200394 /* FIXME: Rest SC for now but must be reviewed. SC flags are only
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100395 * connection related for now but this will evolved
396 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200397 sc->flags &= SC_FL_ISBACK;
398 if (sc_strm(sc))
399 sc->app_ops = &sc_app_embedded_ops;
Willy Tarreau2f2318d2022-05-18 10:17:16 +0200400 else
Willy Tarreau0adb2812022-05-27 10:02:48 +0200401 sc->app_ops = NULL;
Willy Tarreaue68bc612022-05-27 11:23:05 +0200402 sc_free_cond(scp);
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100403}
404
Willy Tarreau4596fe22022-05-17 19:07:51 +0200405/* Detaches the stconn from the app layer. If there is no endpoint attached
406 * to the stconn
Christopher Faulet9ed77422022-04-12 08:51:15 +0200407 */
Willy Tarreaue68bc612022-05-27 11:23:05 +0200408static void sc_detach_app(struct stconn **scp)
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100409{
Willy Tarreaue68bc612022-05-27 11:23:05 +0200410 struct stconn *sc = *scp;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200411
Willy Tarreau0adb2812022-05-27 10:02:48 +0200412 if (!sc)
Christopher Fauleteb50c012022-04-21 14:22:53 +0200413 return;
414
Willy Tarreau0adb2812022-05-27 10:02:48 +0200415 sc->app = NULL;
416 sc->app_ops = NULL;
417 sockaddr_free(&sc->src);
418 sockaddr_free(&sc->dst);
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200419
Tim Duesterhusb1ec21d2023-04-22 17:47:32 +0200420 tasklet_free(sc->wait_event.tasklet);
Willy Tarreau0adb2812022-05-27 10:02:48 +0200421 sc->wait_event.tasklet = NULL;
422 sc->wait_event.events = 0;
Willy Tarreaue68bc612022-05-27 11:23:05 +0200423 sc_free_cond(scp);
Christopher Fauleteb50c012022-04-21 14:22:53 +0200424}
425
Willy Tarreau4596fe22022-05-17 19:07:51 +0200426/* Destroy the stconn. It is detached from its endpoint and its
427 * application. After this call, the stconn must be considered as released.
Christopher Fauleteb50c012022-04-21 14:22:53 +0200428 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200429void sc_destroy(struct stconn *sc)
Christopher Fauleteb50c012022-04-21 14:22:53 +0200430{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200431 sc_detach_endp(&sc);
432 sc_detach_app(&sc);
433 BUG_ON_HOT(sc);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100434}
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100435
Willy Tarreau4596fe22022-05-17 19:07:51 +0200436/* Resets the stream connector endpoint. It happens when the app layer want to renew
Christopher Faulet9ed77422022-04-12 08:51:15 +0200437 * its endpoint. For a connection retry for instance. If a mux or an applet is
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500438 * attached, a new endpoint is created. Returns -1 on error and 0 on success.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200439 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200440int sc_reset_endp(struct stconn *sc)
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100441{
Willy Tarreau31219282022-05-27 16:21:33 +0200442 struct sedesc *new_sd;
Christopher Fauletb041b232022-03-24 10:27:02 +0100443
Willy Tarreau0adb2812022-05-27 10:02:48 +0200444 BUG_ON(!sc->app);
Christopher Fauleta6c4a482022-04-28 18:25:24 +0200445
Willy Tarreau0adb2812022-05-27 10:02:48 +0200446 if (!__sc_endp(sc)) {
Christopher Fauletb041b232022-03-24 10:27:02 +0100447 /* endpoint not attached or attached to a mux with no
448 * target. Thus the endpoint will not be release but just
Willy Tarreau0adb2812022-05-27 10:02:48 +0200449 * reset. The app is still attached, the sc will not be
Christopher Fauleteb50c012022-04-21 14:22:53 +0200450 * released.
Christopher Fauletb041b232022-03-24 10:27:02 +0100451 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200452 sc_detach_endp(&sc);
Christopher Fauletb041b232022-03-24 10:27:02 +0100453 return 0;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100454 }
Christopher Fauletb041b232022-03-24 10:27:02 +0100455
456 /* allocate the new endpoint first to be able to set error if it
457 * fails */
Willy Tarreau31219282022-05-27 16:21:33 +0200458 new_sd = sedesc_new();
Christopher Faulet638fe6a2023-04-14 10:28:28 +0200459 if (!unlikely(new_sd))
Christopher Fauletb041b232022-03-24 10:27:02 +0100460 return -1;
Christopher Fauletb041b232022-03-24 10:27:02 +0100461
Willy Tarreau0adb2812022-05-27 10:02:48 +0200462 /* The app is still attached, the sc will not be released */
463 sc_detach_endp(&sc);
Willy Tarreau6a378d12022-08-11 13:56:42 +0200464 BUG_ON(!sc);
Willy Tarreau0adb2812022-05-27 10:02:48 +0200465 BUG_ON(sc->sedesc);
Willy Tarreau31219282022-05-27 16:21:33 +0200466 sc->sedesc = new_sd;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200467 sc->sedesc->sc = sc;
468 sc_ep_set(sc, SE_FL_DETACHED);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100469 return 0;
470}
Christopher Faulet37046632022-04-01 11:36:58 +0200471
472
Willy Tarreaue68bc612022-05-27 11:23:05 +0200473/* Create an applet to handle a stream connector as a new appctx. The SC will
Christopher Faulet37046632022-04-01 11:36:58 +0200474 * wake it up every time it is solicited. The appctx must be deleted by the task
Willy Tarreau19c65a92022-05-27 08:49:24 +0200475 * handler using sc_detach_endp(), possibly from within the function itself.
Christopher Faulet37046632022-04-01 11:36:58 +0200476 * It also pre-initializes the applet's context and returns it (or NULL in case
477 * it could not be allocated).
478 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200479struct appctx *sc_applet_create(struct stconn *sc, struct applet *app)
Christopher Faulet37046632022-04-01 11:36:58 +0200480{
481 struct appctx *appctx;
482
Willy Tarreau0adb2812022-05-27 10:02:48 +0200483 appctx = appctx_new_here(app, sc->sedesc);
Christopher Faulet37046632022-04-01 11:36:58 +0200484 if (!appctx)
485 return NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200486 sc_attach_applet(sc, appctx);
487 appctx->t->nice = __sc_strm(sc)->task->nice;
Willy Tarreau90e8b452022-05-25 18:21:43 +0200488 applet_need_more_data(appctx);
Christopher Faulet37046632022-04-01 11:36:58 +0200489 appctx_wakeup(appctx);
Christopher Fauleta33ff7a2022-04-21 11:52:07 +0200490
Willy Tarreau0adb2812022-05-27 10:02:48 +0200491 sc->state = SC_ST_RDY;
Christopher Faulet37046632022-04-01 11:36:58 +0200492 return appctx;
493}
494
Ilya Shipitsin07be66d2023-04-01 12:26:42 +0200495/* Conditionally forward the close to the write side. It return 1 if it can be
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100496 * forwarded. It is the caller responsibility to forward the close to the write
Christopher Faulete38534c2023-04-13 15:45:24 +0200497 * side. Otherwise, 0 is returned. In this case, SC_FL_SHUT_WANTED flag may be set on
Christopher Faulet87633c32023-04-03 18:32:50 +0200498 * the consumer SC if we are only waiting for the outgoing data to be flushed.
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100499 */
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +0200500static inline int sc_cond_forward_shut(struct stconn *sc)
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100501{
502 /* The close must not be forwarded */
Christopher Fauletca5309a2023-04-17 16:17:32 +0200503 if (!(sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)) || !(sc->flags & SC_FL_NOHALF))
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100504 return 0;
505
506 if (!channel_is_empty(sc_ic(sc))) {
Christopher Fauletdf7cd712023-04-13 15:56:26 +0200507 /* the shutdown cannot be forwarded now because
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100508 * we should flush outgoing data first. But instruct the output
509 * channel it should be done ASAP.
510 */
Christopher Fauletdf7cd712023-04-13 15:56:26 +0200511 sc_schedule_shutdown(sc);
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100512 return 0;
513 }
514
515 /* the close can be immediately forwarded to the write side */
516 return 1;
517}
518
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200519/*
Willy Tarreau4596fe22022-05-17 19:07:51 +0200520 * This function performs a shutdown-read on a detached stream connector in a
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200521 * connected or init state (it does nothing for other states). It either shuts
522 * the read side or marks itself as closed. The buffer flags are updated to
Willy Tarreaucb041662022-05-17 19:44:42 +0200523 * reflect the new state. If the stream connector has SC_FL_NOHALF, we also
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200524 * forward the close to the write side. The owner task is woken up if it exists.
525 */
Christopher Fauletcfc11c02023-04-13 16:10:23 +0200526static void sc_app_abort(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200527{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200528 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200529
Christopher Fauletca5309a2023-04-17 16:17:32 +0200530 if (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))
Christopher Fauletc665bb52023-04-04 10:06:57 +0200531 return;
Christopher Faulet87633c32023-04-03 18:32:50 +0200532
Christopher Faulet0c370ee2023-04-13 16:05:13 +0200533 sc->flags |= SC_FL_ABRT_DONE;
Christopher Faulet87633c32023-04-03 18:32:50 +0200534 ic->flags |= CF_READ_EVENT;
Christopher Faulet4c135682023-02-16 11:09:31 +0100535 sc_ep_report_read_activity(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200536
Willy Tarreau0adb2812022-05-27 10:02:48 +0200537 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200538 return;
539
Christopher Faulet208c7122023-04-13 16:16:15 +0200540 if (sc->flags & SC_FL_SHUT_DONE) {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200541 sc->state = SC_ST_DIS;
Christopher Fauletca679922022-07-20 13:24:04 +0200542 if (sc->flags & SC_FL_ISBACK)
543 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200544 }
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +0200545 else if (sc_cond_forward_shut(sc))
546 return sc_app_shut(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200547
548 /* note that if the task exists, it must unregister itself once it runs */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200549 if (!(sc->flags & SC_FL_DONT_WAKE))
550 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200551}
552
553/*
Willy Tarreau4596fe22022-05-17 19:07:51 +0200554 * This function performs a shutdown-write on a detached stream connector in a
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200555 * connected or init state (it does nothing for other states). It either shuts
556 * the write side or marks itself as closed. The buffer flags are updated to
Willy Tarreaue68bc612022-05-27 11:23:05 +0200557 * reflect the new state. It does also close everything if the SC was marked as
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200558 * being in error state. The owner task is woken up if it exists.
559 */
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +0200560static void sc_app_shut(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200561{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200562 struct channel *ic = sc_ic(sc);
563 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200564
Christopher Faulete38534c2023-04-13 15:45:24 +0200565 sc->flags &= ~SC_FL_SHUT_WANTED;
Christopher Faulet208c7122023-04-13 16:16:15 +0200566 if (sc->flags & SC_FL_SHUT_DONE)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200567 return;
Christopher Faulet208c7122023-04-13 16:16:15 +0200568 sc->flags |= SC_FL_SHUT_DONE;
Christopher Faulet87633c32023-04-03 18:32:50 +0200569 oc->flags |= CF_WRITE_EVENT;
Christopher Fauletbcdcfad2023-02-20 08:36:53 +0100570 sc_set_hcto(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200571
Willy Tarreau0adb2812022-05-27 10:02:48 +0200572 switch (sc->state) {
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200573 case SC_ST_RDY:
574 case SC_ST_EST:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200575 /* we have to shut before closing, otherwise some short messages
576 * may never leave the system, especially when there are remaining
577 * unread data in the socket input buffer, or when nolinger is set.
Willy Tarreaucb041662022-05-17 19:44:42 +0200578 * However, if SC_FL_NOLINGER is explicitly set, we know there is
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200579 * no risk so we close both sides immediately.
580 */
Christopher Fauletca5309a2023-04-17 16:17:32 +0200581 if (!(sc->flags & (SC_FL_ERROR|SC_FL_NOLINGER|SC_FL_EOS|SC_FL_ABRT_DONE)) &&
Christopher Fauletad46e522023-04-14 11:59:15 +0200582 !(ic->flags & CF_DONT_READ))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200583 return;
584
Willy Tarreau476c2802022-11-14 07:36:42 +0100585 __fallthrough;
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200586 case SC_ST_CON:
587 case SC_ST_CER:
588 case SC_ST_QUE:
589 case SC_ST_TAR:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200590 /* Note that none of these states may happen with applets */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200591 sc->state = SC_ST_DIS;
Willy Tarreau476c2802022-11-14 07:36:42 +0100592 __fallthrough;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200593 default:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200594 sc->flags &= ~SC_FL_NOLINGER;
Christopher Faulet0c370ee2023-04-13 16:05:13 +0200595 sc->flags |= SC_FL_ABRT_DONE;
Christopher Fauletca679922022-07-20 13:24:04 +0200596 if (sc->flags & SC_FL_ISBACK)
597 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200598 }
599
600 /* note that if the task exists, it must unregister itself once it runs */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200601 if (!(sc->flags & SC_FL_DONT_WAKE))
602 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200603}
604
605/* default chk_rcv function for scheduled tasks */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200606static void sc_app_chk_rcv(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200607{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200608 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200609
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200610 if (ic->pipe) {
611 /* stop reading */
Christopher Faulet7b3d38a2023-05-05 11:28:45 +0200612 sc_need_room(sc, -1);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200613 }
614 else {
615 /* (re)start reading */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200616 if (!(sc->flags & SC_FL_DONT_WAKE))
617 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200618 }
619}
620
621/* default chk_snd function for scheduled tasks */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200622static void sc_app_chk_snd(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200623{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200624 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200625
Christopher Faulet208c7122023-04-13 16:16:15 +0200626 if (unlikely(sc->state != SC_ST_EST || (sc->flags & SC_FL_SHUT_DONE)))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200627 return;
628
Willy Tarreau0adb2812022-05-27 10:02:48 +0200629 if (!sc_ep_test(sc, SE_FL_WAIT_DATA) || /* not waiting for data */
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200630 channel_is_empty(oc)) /* called with nothing to send ! */
631 return;
632
633 /* Otherwise there are remaining data to be sent in the buffer,
634 * so we tell the handler.
635 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200636 sc_ep_clr(sc, SE_FL_WAIT_DATA);
Willy Tarreau0adb2812022-05-27 10:02:48 +0200637 if (!(sc->flags & SC_FL_DONT_WAKE))
638 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200639}
640
641/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200642 * This function performs a shutdown-read on a stream connector attached to
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200643 * a connection in a connected or init state (it does nothing for other
644 * states). It either shuts the read side or marks itself as closed. The buffer
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200645 * flags are updated to reflect the new state. If the stream connector has
Willy Tarreaucb041662022-05-17 19:44:42 +0200646 * SC_FL_NOHALF, we also forward the close to the write side. If a control
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200647 * layer is defined, then it is supposed to be a socket layer and file
648 * descriptors are then shutdown or closed accordingly. The function
649 * automatically disables polling if needed.
650 */
Christopher Fauletcfc11c02023-04-13 16:10:23 +0200651static void sc_app_abort_conn(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200652{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200653 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200654
Willy Tarreau0adb2812022-05-27 10:02:48 +0200655 BUG_ON(!sc_conn(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200656
Christopher Fauletca5309a2023-04-17 16:17:32 +0200657 if (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200658 return;
Christopher Faulet0c370ee2023-04-13 16:05:13 +0200659 sc->flags |= SC_FL_ABRT_DONE;
Christopher Faulet87633c32023-04-03 18:32:50 +0200660 ic->flags |= CF_READ_EVENT;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200661
Willy Tarreau0adb2812022-05-27 10:02:48 +0200662 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200663 return;
664
Christopher Faulet208c7122023-04-13 16:16:15 +0200665 if (sc->flags & SC_FL_SHUT_DONE) {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200666 sc_conn_shut(sc);
667 sc->state = SC_ST_DIS;
Christopher Fauletca679922022-07-20 13:24:04 +0200668 if (sc->flags & SC_FL_ISBACK)
669 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200670 }
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +0200671 else if (sc_cond_forward_shut(sc))
672 return sc_app_shut_conn(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200673}
674
675/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200676 * This function performs a shutdown-write on a stream connector attached to
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200677 * a connection in a connected or init state (it does nothing for other
678 * states). It either shuts the write side or marks itself as closed. The
679 * buffer flags are updated to reflect the new state. It does also close
Willy Tarreaue68bc612022-05-27 11:23:05 +0200680 * everything if the SC was marked as being in error state. If there is a
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200681 * data-layer shutdown, it is called.
682 */
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +0200683static void sc_app_shut_conn(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200684{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200685 struct channel *ic = sc_ic(sc);
686 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200687
Willy Tarreau0adb2812022-05-27 10:02:48 +0200688 BUG_ON(!sc_conn(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200689
Christopher Faulete38534c2023-04-13 15:45:24 +0200690 sc->flags &= ~SC_FL_SHUT_WANTED;
Christopher Faulet208c7122023-04-13 16:16:15 +0200691 if (sc->flags & SC_FL_SHUT_DONE)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200692 return;
Christopher Faulet208c7122023-04-13 16:16:15 +0200693 sc->flags |= SC_FL_SHUT_DONE;
Christopher Faulet87633c32023-04-03 18:32:50 +0200694 oc->flags |= CF_WRITE_EVENT;
Christopher Fauletbcdcfad2023-02-20 08:36:53 +0100695 sc_set_hcto(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200696
Willy Tarreau0adb2812022-05-27 10:02:48 +0200697 switch (sc->state) {
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200698 case SC_ST_RDY:
699 case SC_ST_EST:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200700 /* we have to shut before closing, otherwise some short messages
701 * may never leave the system, especially when there are remaining
702 * unread data in the socket input buffer, or when nolinger is set.
Willy Tarreaucb041662022-05-17 19:44:42 +0200703 * However, if SC_FL_NOLINGER is explicitly set, we know there is
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200704 * no risk so we close both sides immediately.
705 */
706
Christopher Faulet25d9fe52023-04-14 12:09:35 +0200707 if (sc->flags & SC_FL_ERROR) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200708 /* quick close, the socket is already shut anyway */
709 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200710 else if (sc->flags & SC_FL_NOLINGER) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200711 /* unclean data-layer shutdown, typically an aborted request
712 * or a forwarded shutdown from a client to a server due to
713 * option abortonclose. No need for the TLS layer to try to
714 * emit a shutdown message.
715 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200716 sc_conn_shutw(sc, CO_SHW_SILENT);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200717 }
718 else {
719 /* clean data-layer shutdown. This only happens on the
720 * frontend side, or on the backend side when forwarding
721 * a client close in TCP mode or in HTTP TUNNEL mode
722 * while option abortonclose is set. We want the TLS
723 * layer to try to signal it to the peer before we close.
724 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200725 sc_conn_shutw(sc, CO_SHW_NORMAL);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200726
Christopher Fauletca5309a2023-04-17 16:17:32 +0200727 if (!(sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)) && !(ic->flags & CF_DONT_READ))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200728 return;
729 }
730
Willy Tarreau476c2802022-11-14 07:36:42 +0100731 __fallthrough;
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200732 case SC_ST_CON:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200733 /* we may have to close a pending connection, and mark the
Christopher Fauletcfc11c02023-04-13 16:10:23 +0200734 * response buffer as abort
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200735 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200736 sc_conn_shut(sc);
Willy Tarreau476c2802022-11-14 07:36:42 +0100737 __fallthrough;
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200738 case SC_ST_CER:
739 case SC_ST_QUE:
740 case SC_ST_TAR:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200741 sc->state = SC_ST_DIS;
Willy Tarreau476c2802022-11-14 07:36:42 +0100742 __fallthrough;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200743 default:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200744 sc->flags &= ~SC_FL_NOLINGER;
Christopher Faulet0c370ee2023-04-13 16:05:13 +0200745 sc->flags |= SC_FL_ABRT_DONE;
Christopher Fauletca679922022-07-20 13:24:04 +0200746 if (sc->flags & SC_FL_ISBACK)
747 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200748 }
749}
750
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200751/* This function is used for inter-stream connector calls. It is called by the
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200752 * consumer to inform the producer side that it may be interested in checking
753 * for free space in the buffer. Note that it intentionally does not update
754 * timeouts, so that we can still check them later at wake-up. This function is
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200755 * dedicated to connection-based stream connectors.
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200756 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200757static void sc_app_chk_rcv_conn(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200758{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200759 BUG_ON(!sc_conn(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200760
761 /* (re)start reading */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200762 if (sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
763 tasklet_wakeup(sc->wait_event.tasklet);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200764}
765
766
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200767/* This function is used for inter-stream connector calls. It is called by the
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200768 * producer to inform the consumer side that it may be interested in checking
769 * for data in the buffer. Note that it intentionally does not update timeouts,
770 * so that we can still check them later at wake-up.
771 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200772static void sc_app_chk_snd_conn(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200773{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200774 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200775
Willy Tarreau0adb2812022-05-27 10:02:48 +0200776 BUG_ON(!sc_conn(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200777
Willy Tarreau0adb2812022-05-27 10:02:48 +0200778 if (unlikely(!sc_state_in(sc->state, SC_SB_RDY|SC_SB_EST) ||
Christopher Faulet208c7122023-04-13 16:16:15 +0200779 (sc->flags & SC_FL_SHUT_DONE)))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200780 return;
781
782 if (unlikely(channel_is_empty(oc))) /* called with nothing to send ! */
783 return;
784
785 if (!oc->pipe && /* spliced data wants to be forwarded ASAP */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200786 !sc_ep_test(sc, SE_FL_WAIT_DATA)) /* not waiting for data */
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200787 return;
788
Willy Tarreau0adb2812022-05-27 10:02:48 +0200789 if (!(sc->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(sc_oc(sc)))
790 sc_conn_send(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200791
Willy Tarreau0adb2812022-05-27 10:02:48 +0200792 if (sc_ep_test(sc, SE_FL_ERROR | SE_FL_ERR_PENDING) || sc_is_conn_error(sc)) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200793 /* Write error on the file descriptor */
Christopher Faulet56a2b602023-04-14 09:42:59 +0200794 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 +0200795 goto out_wakeup;
796 }
797
798 /* OK, so now we know that some data might have been sent, and that we may
799 * have to poll first. We have to do that too if the buffer is not empty.
800 */
801 if (channel_is_empty(oc)) {
802 /* the connection is established but we can't write. Either the
803 * buffer is empty, or we just refrain from sending because the
804 * ->o limit was reached. Maybe we just wrote the last
805 * chunk and need to close.
806 */
Christopher Faulet87633c32023-04-03 18:32:50 +0200807 if ((oc->flags & CF_AUTO_CLOSE) &&
Christopher Faulet208c7122023-04-13 16:16:15 +0200808 ((sc->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED)) == SC_FL_SHUT_WANTED) &&
Willy Tarreau0adb2812022-05-27 10:02:48 +0200809 sc_state_in(sc->state, SC_SB_RDY|SC_SB_EST)) {
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +0200810 sc_shutdown(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200811 goto out_wakeup;
812 }
813
Christopher Faulet208c7122023-04-13 16:16:15 +0200814 if ((sc->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED)) == 0)
Willy Tarreau0adb2812022-05-27 10:02:48 +0200815 sc_ep_set(sc, SE_FL_WAIT_DATA);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200816 }
817 else {
818 /* Otherwise there are remaining data to be sent in the buffer,
819 * which means we have to poll before doing so.
820 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200821 sc_ep_clr(sc, SE_FL_WAIT_DATA);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200822 }
823
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200824 /* in case of special condition (error, shutdown, end of write...), we
825 * have to notify the task.
826 */
Christopher Faulet208c7122023-04-13 16:16:15 +0200827 if (likely((sc->flags & SC_FL_SHUT_DONE) ||
Christopher Faulet71c486b2023-02-09 14:14:38 +0100828 ((oc->flags & CF_WRITE_EVENT) && sc->state < SC_ST_EST) ||
829 ((oc->flags & CF_WAKE_WRITE) &&
830 ((channel_is_empty(oc) && !oc->to_forward) ||
831 !sc_state_in(sc->state, SC_SB_EST))))) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200832 out_wakeup:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200833 if (!(sc->flags & SC_FL_DONT_WAKE))
834 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200835 }
836}
837
838/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200839 * This function performs a shutdown-read on a stream connector attached to an
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200840 * applet in a connected or init state (it does nothing for other states). It
841 * either shuts the read side or marks itself as closed. The buffer flags are
Willy Tarreaucb041662022-05-17 19:44:42 +0200842 * updated to reflect the new state. If the stream connector has SC_FL_NOHALF,
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200843 * we also forward the close to the write side. The owner task is woken up if
844 * it exists.
845 */
Christopher Fauletcfc11c02023-04-13 16:10:23 +0200846static void sc_app_abort_applet(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200847{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200848 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200849
Willy Tarreau0adb2812022-05-27 10:02:48 +0200850 BUG_ON(!sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200851
Christopher Fauletca5309a2023-04-17 16:17:32 +0200852 if (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200853 return;
Christopher Faulet0c370ee2023-04-13 16:05:13 +0200854 sc->flags |= SC_FL_ABRT_DONE;
Christopher Faulet87633c32023-04-03 18:32:50 +0200855 ic->flags |= CF_READ_EVENT;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200856
Christopher Fauletcfc11c02023-04-13 16:10:23 +0200857 /* Note: on abort, we don't call the applet */
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200858
Willy Tarreau0adb2812022-05-27 10:02:48 +0200859 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200860 return;
861
Christopher Faulet208c7122023-04-13 16:16:15 +0200862 if (sc->flags & SC_FL_SHUT_DONE) {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200863 appctx_shut(__sc_appctx(sc));
864 sc->state = SC_ST_DIS;
Christopher Fauletca679922022-07-20 13:24:04 +0200865 if (sc->flags & SC_FL_ISBACK)
866 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200867 }
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +0200868 else if (sc_cond_forward_shut(sc))
869 return sc_app_shut_applet(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200870}
871
872/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200873 * This function performs a shutdown-write on a stream connector attached to an
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200874 * applet in a connected or init state (it does nothing for other states). It
875 * either shuts the write side or marks itself as closed. The buffer flags are
876 * updated to reflect the new state. It does also close everything if the SI
877 * was marked as being in error state. The owner task is woken up if it exists.
878 */
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +0200879static void sc_app_shut_applet(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200880{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200881 struct channel *ic = sc_ic(sc);
882 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200883
Willy Tarreau0adb2812022-05-27 10:02:48 +0200884 BUG_ON(!sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200885
Christopher Faulete38534c2023-04-13 15:45:24 +0200886 sc->flags &= ~SC_FL_SHUT_WANTED;
Christopher Faulet208c7122023-04-13 16:16:15 +0200887 if (sc->flags & SC_FL_SHUT_DONE)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200888 return;
Christopher Faulet208c7122023-04-13 16:16:15 +0200889 sc->flags |= SC_FL_SHUT_DONE;
Christopher Faulet87633c32023-04-03 18:32:50 +0200890 oc->flags |= CF_WRITE_EVENT;
Christopher Fauletbcdcfad2023-02-20 08:36:53 +0100891 sc_set_hcto(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200892
893 /* on shutw we always wake the applet up */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200894 appctx_wakeup(__sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200895
Willy Tarreau0adb2812022-05-27 10:02:48 +0200896 switch (sc->state) {
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200897 case SC_ST_RDY:
898 case SC_ST_EST:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200899 /* we have to shut before closing, otherwise some short messages
900 * may never leave the system, especially when there are remaining
901 * unread data in the socket input buffer, or when nolinger is set.
Willy Tarreaucb041662022-05-17 19:44:42 +0200902 * However, if SC_FL_NOLINGER is explicitly set, we know there is
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200903 * no risk so we close both sides immediately.
904 */
Christopher Fauletca5309a2023-04-17 16:17:32 +0200905 if (!(sc->flags & (SC_FL_ERROR|SC_FL_NOLINGER|SC_FL_EOS|SC_FL_ABRT_DONE)) &&
Christopher Faulet87633c32023-04-03 18:32:50 +0200906 !(ic->flags & CF_DONT_READ))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200907 return;
908
Willy Tarreau476c2802022-11-14 07:36:42 +0100909 __fallthrough;
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200910 case SC_ST_CON:
911 case SC_ST_CER:
912 case SC_ST_QUE:
913 case SC_ST_TAR:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200914 /* Note that none of these states may happen with applets */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200915 appctx_shut(__sc_appctx(sc));
916 sc->state = SC_ST_DIS;
Willy Tarreau476c2802022-11-14 07:36:42 +0100917 __fallthrough;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200918 default:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200919 sc->flags &= ~SC_FL_NOLINGER;
Christopher Faulet0c370ee2023-04-13 16:05:13 +0200920 sc->flags |= SC_FL_ABRT_DONE;
Christopher Fauletca679922022-07-20 13:24:04 +0200921 if (sc->flags & SC_FL_ISBACK)
922 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200923 }
924}
925
926/* chk_rcv function for applets */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200927static void sc_app_chk_rcv_applet(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200928{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200929 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200930
Willy Tarreau0adb2812022-05-27 10:02:48 +0200931 BUG_ON(!sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200932
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200933 if (!ic->pipe) {
934 /* (re)start reading */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200935 appctx_wakeup(__sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200936 }
937}
938
939/* chk_snd function for applets */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200940static void sc_app_chk_snd_applet(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200941{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200942 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200943
Willy Tarreau0adb2812022-05-27 10:02:48 +0200944 BUG_ON(!sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200945
Christopher Faulet208c7122023-04-13 16:16:15 +0200946 if (unlikely(sc->state != SC_ST_EST || (sc->flags & SC_FL_SHUT_DONE)))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200947 return;
948
Christopher Faulet04f03e12022-06-01 17:35:34 +0200949 /* we only wake the applet up if it was waiting for some data and is ready to consume it */
950 if (!sc_ep_test(sc, SE_FL_WAIT_DATA) || sc_ep_test(sc, SE_FL_WONT_CONSUME))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200951 return;
952
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200953 if (!channel_is_empty(oc)) {
954 /* (re)start sending */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200955 appctx_wakeup(__sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200956 }
957}
Christopher Faulet13045f02022-04-01 14:23:38 +0200958
959
960/* This function is designed to be called from within the stream handler to
Willy Tarreau4596fe22022-05-17 19:07:51 +0200961 * update the input channel's expiration timer and the stream connector's
Christopher Faulet13045f02022-04-01 14:23:38 +0200962 * Rx flags based on the channel's flags. It needs to be called only once
963 * after the channel's flags have settled down, and before they are cleared,
964 * though it doesn't harm to call it as often as desired (it just slightly
965 * hurts performance). It must not be called from outside of the stream
966 * handler, as what it does will be used to compute the stream task's
967 * expiration.
968 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200969void sc_update_rx(struct stconn *sc)
Christopher Faulet13045f02022-04-01 14:23:38 +0200970{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200971 struct channel *ic = sc_ic(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +0200972
Christopher Fauletca5309a2023-04-17 16:17:32 +0200973 if (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))
Christopher Faulet13045f02022-04-01 14:23:38 +0200974 return;
Christopher Faulet13045f02022-04-01 14:23:38 +0200975
Christopher Fauletfab82bf2023-05-05 11:30:16 +0200976 /* Unblock the SC if it needs room and the free space is large enough (0
977 * means it can always be unblocked). Do not unblock it if -1 was
978 * specified.
979 */
980 if (!sc->room_needed || (sc->room_needed > 0 && channel_recv_max(ic) >= sc->room_needed))
981 sc_have_room(sc);
982
Christopher Faulet13045f02022-04-01 14:23:38 +0200983 /* Read not closed, update FD status and timeout for reads */
984 if (ic->flags & CF_DONT_READ)
Willy Tarreau0adb2812022-05-27 10:02:48 +0200985 sc_wont_read(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +0200986 else
Willy Tarreau0adb2812022-05-27 10:02:48 +0200987 sc_will_read(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +0200988
Willy Tarreau0adb2812022-05-27 10:02:48 +0200989 sc_chk_rcv(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +0200990}
991
992/* This function is designed to be called from within the stream handler to
Willy Tarreau4596fe22022-05-17 19:07:51 +0200993 * update the output channel's expiration timer and the stream connector's
Christopher Faulet13045f02022-04-01 14:23:38 +0200994 * Tx flags based on the channel's flags. It needs to be called only once
995 * after the channel's flags have settled down, and before they are cleared,
996 * though it doesn't harm to call it as often as desired (it just slightly
997 * hurts performance). It must not be called from outside of the stream
998 * handler, as what it does will be used to compute the stream task's
999 * expiration.
1000 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001001void sc_update_tx(struct stconn *sc)
Christopher Faulet13045f02022-04-01 14:23:38 +02001002{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001003 struct channel *oc = sc_oc(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +02001004
Christopher Faulet208c7122023-04-13 16:16:15 +02001005 if (sc->flags & SC_FL_SHUT_DONE)
Christopher Faulet13045f02022-04-01 14:23:38 +02001006 return;
1007
1008 /* Write not closed, update FD status and timeout for writes */
1009 if (channel_is_empty(oc)) {
1010 /* stop writing */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001011 if (!sc_ep_test(sc, SE_FL_WAIT_DATA)) {
Christopher Faulete38534c2023-04-13 15:45:24 +02001012 if ((sc->flags & SC_FL_SHUT_WANTED) == 0)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001013 sc_ep_set(sc, SE_FL_WAIT_DATA);
Christopher Faulet13045f02022-04-01 14:23:38 +02001014 }
1015 return;
1016 }
1017
Christopher Faulet15315d62023-02-20 08:23:51 +01001018 /* (re)start writing */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001019 sc_ep_clr(sc, SE_FL_WAIT_DATA);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001020}
1021
Willy Tarreau19c65a92022-05-27 08:49:24 +02001022/* This function is the equivalent to sc_update() except that it's
Christopher Faulet5e29b762022-04-04 08:58:34 +02001023 * designed to be called from outside the stream handlers, typically the lower
1024 * layers (applets, connections) after I/O completion. After updating the stream
1025 * interface and timeouts, it will try to forward what can be forwarded, then to
1026 * wake the associated task up if an important event requires special handling.
Willy Tarreau15252cd2022-05-25 16:36:21 +02001027 * It may update SE_FL_WAIT_DATA and/or SC_FL_NEED_ROOM, that the callers are
Christopher Faulet5e29b762022-04-04 08:58:34 +02001028 * encouraged to watch to take appropriate action.
Willy Tarreau19c65a92022-05-27 08:49:24 +02001029 * It should not be called from within the stream itself, sc_update()
Christopher Faulet5e29b762022-04-04 08:58:34 +02001030 * is designed for this.
1031 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001032static void sc_notify(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001033{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001034 struct channel *ic = sc_ic(sc);
1035 struct channel *oc = sc_oc(sc);
Willy Tarreaue68bc612022-05-27 11:23:05 +02001036 struct stconn *sco = sc_opposite(sc);
Willy Tarreau0adb2812022-05-27 10:02:48 +02001037 struct task *task = sc_strm_task(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001038
1039 /* process consumer side */
1040 if (channel_is_empty(oc)) {
Willy Tarreau0adb2812022-05-27 10:02:48 +02001041 struct connection *conn = sc_conn(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001042
Christopher Faulet208c7122023-04-13 16:16:15 +02001043 if (((sc->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED)) == SC_FL_SHUT_WANTED) &&
Willy Tarreau0adb2812022-05-27 10:02:48 +02001044 (sc->state == SC_ST_EST) && (!conn || !(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS))))
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +02001045 sc_shutdown(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001046 }
1047
1048 /* indicate that we may be waiting for data from the output channel or
Christopher Faulete38534c2023-04-13 15:45:24 +02001049 * 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 +02001050 */
Christopher Faulet208c7122023-04-13 16:16:15 +02001051 if (!(sc->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED)))
Willy Tarreau0adb2812022-05-27 10:02:48 +02001052 sc_ep_set(sc, SE_FL_WAIT_DATA);
Christopher Faulet208c7122023-04-13 16:16:15 +02001053 else if ((sc->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED)) == SC_FL_SHUT_WANTED)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001054 sc_ep_clr(sc, SE_FL_WAIT_DATA);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001055
Christopher Faulet5e29b762022-04-04 08:58:34 +02001056 if (oc->flags & CF_DONT_READ)
Willy Tarreaue68bc612022-05-27 11:23:05 +02001057 sc_wont_read(sco);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001058 else
Willy Tarreaue68bc612022-05-27 11:23:05 +02001059 sc_will_read(sco);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001060
1061 /* Notify the other side when we've injected data into the IC that
1062 * needs to be forwarded. We can do fast-forwarding as soon as there
1063 * are output data, but we avoid doing this if some of the data are
1064 * not yet scheduled for being forwarded, because it is very likely
1065 * that it will be done again immediately afterwards once the following
Willy Tarreau15252cd2022-05-25 16:36:21 +02001066 * data are parsed (eg: HTTP chunking). We only clear SC_FL_NEED_ROOM
1067 * once we've emptied *some* of the output buffer, and not just when
1068 * there is available room, because applets are often forced to stop
1069 * before the buffer is full. We must not stop based on input data
1070 * alone because an HTTP parser might need more data to complete the
1071 * parsing.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001072 */
1073 if (!channel_is_empty(ic) &&
Willy Tarreaue68bc612022-05-27 11:23:05 +02001074 sc_ep_test(sco, SE_FL_WAIT_DATA) &&
Christopher Faulet84d3ef92023-03-17 15:45:58 +01001075 (!(sc->flags & SC_FL_SND_EXP_MORE) || c_full(ic) || ci_data(ic) == 0 || ic->pipe)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001076 int new_len, last_len;
1077
1078 last_len = co_data(ic);
1079 if (ic->pipe)
1080 last_len += ic->pipe->data;
1081
Willy Tarreaue68bc612022-05-27 11:23:05 +02001082 sc_chk_snd(sco);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001083
1084 new_len = co_data(ic);
1085 if (ic->pipe)
1086 new_len += ic->pipe->data;
1087
1088 /* check if the consumer has freed some space either in the
1089 * buffer or in the pipe.
1090 */
Christopher Faulet18b33092023-05-05 11:40:07 +02001091 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 +02001092 sc_have_room(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001093 }
1094
1095 if (!(ic->flags & CF_DONT_READ))
Willy Tarreau0adb2812022-05-27 10:02:48 +02001096 sc_will_read(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001097
Willy Tarreau0adb2812022-05-27 10:02:48 +02001098 sc_chk_rcv(sc);
Willy Tarreaue68bc612022-05-27 11:23:05 +02001099 sc_chk_rcv(sco);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001100
Christopher Faulet5e29b762022-04-04 08:58:34 +02001101 /* wake the task up only when needed */
Christopher Faulet285f7612022-12-12 08:28:55 +01001102 if (/* changes on the production side that must be handled:
Christopher Fauletad46e522023-04-14 11:59:15 +02001103 * - An error on receipt: SC_FL_ERROR
Christopher Fauletca5309a2023-04-17 16:17:32 +02001104 * - A read event: shutdown for reads (CF_READ_EVENT + EOS/ABRT_DONE)
Christopher Faulet904763f2023-03-22 14:53:11 +01001105 * end of input (CF_READ_EVENT + SC_FL_EOI)
Christopher Faulet285f7612022-12-12 08:28:55 +01001106 * data received and no fast-forwarding (CF_READ_EVENT + !to_forward)
1107 * read event while consumer side is not established (CF_READ_EVENT + sco->state != SC_ST_EST)
1108 */
Christopher Fauletca5309a2023-04-17 16:17:32 +02001109 ((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 +02001110 (sc->flags & SC_FL_ERROR) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001111
1112 /* changes on the consumption side */
Christopher Faulet2e56a732023-01-26 16:18:09 +01001113 sc_ep_test(sc, SE_FL_ERR_PENDING) ||
Christopher Fauletd8988412022-12-20 18:10:04 +01001114 ((oc->flags & CF_WRITE_EVENT) &&
1115 ((sc->state < SC_ST_EST) ||
Christopher Faulet208c7122023-04-13 16:16:15 +02001116 (sc->flags & SC_FL_SHUT_DONE) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001117 (((oc->flags & CF_WAKE_WRITE) ||
Christopher Faulet87633c32023-04-03 18:32:50 +02001118 (!(oc->flags & CF_AUTO_CLOSE) &&
Christopher Faulet208c7122023-04-13 16:16:15 +02001119 !(sc->flags & (SC_FL_SHUT_WANTED|SC_FL_SHUT_DONE)))) &&
Christopher Faulet87633c32023-04-03 18:32:50 +02001120 (sco->state != SC_ST_EST ||
1121 (channel_is_empty(oc) && !oc->to_forward)))))) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001122 task_wakeup(task, TASK_WOKEN_IO);
1123 }
Christopher Faulet5e29b762022-04-04 08:58:34 +02001124
Christopher Faulet2e56a732023-01-26 16:18:09 +01001125 if (ic->flags & CF_READ_EVENT)
Christopher Faulet9a790f62023-03-16 14:40:03 +01001126 sc->flags &= ~SC_FL_RCV_ONCE;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001127}
1128
1129/*
Christopher Faulet1aec6c92023-04-17 17:29:29 +02001130 * This function propagates an end-of-stream received on a socket-based connection.
Willy Tarreaucb041662022-05-17 19:44:42 +02001131 * It updates the stream connector. If the stream connector has SC_FL_NOHALF,
Christopher Faulet5e29b762022-04-04 08:58:34 +02001132 * the close is also forwarded to the write side as an abort.
1133 */
Christopher Faulet1aec6c92023-04-17 17:29:29 +02001134static void sc_conn_eos(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001135{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001136 struct channel *ic = sc_ic(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001137
Willy Tarreau0adb2812022-05-27 10:02:48 +02001138 BUG_ON(!sc_conn(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001139
Christopher Fauletca5309a2023-04-17 16:17:32 +02001140 if (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001141 return;
Christopher Faulet1aec6c92023-04-17 17:29:29 +02001142 sc->flags |= SC_FL_EOS;
Christopher Faulet87633c32023-04-03 18:32:50 +02001143 ic->flags |= CF_READ_EVENT;
Christopher Faulet4c135682023-02-16 11:09:31 +01001144 sc_ep_report_read_activity(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001145
Willy Tarreau0adb2812022-05-27 10:02:48 +02001146 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001147 return;
1148
Christopher Faulet208c7122023-04-13 16:16:15 +02001149 if (sc->flags & SC_FL_SHUT_DONE)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001150 goto do_close;
1151
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +02001152 if (sc_cond_forward_shut(sc)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001153 /* we want to immediately forward this close to the write side */
1154 /* force flag on ssl to keep stream in cache */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001155 sc_conn_shutw(sc, CO_SHW_SILENT);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001156 goto do_close;
1157 }
1158
1159 /* otherwise that's just a normal read shutdown */
1160 return;
1161
1162 do_close:
Willy Tarreauf61dd192022-05-27 09:00:19 +02001163 /* OK we completely close the socket here just as if we went through sc_shut[rw]() */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001164 sc_conn_shut(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001165
Christopher Faulete38534c2023-04-13 15:45:24 +02001166 sc->flags &= ~SC_FL_SHUT_WANTED;
Christopher Faulet208c7122023-04-13 16:16:15 +02001167 sc->flags |= SC_FL_SHUT_DONE;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001168
Willy Tarreau0adb2812022-05-27 10:02:48 +02001169 sc->state = SC_ST_DIS;
Christopher Fauletca679922022-07-20 13:24:04 +02001170 if (sc->flags & SC_FL_ISBACK)
1171 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001172 return;
1173}
1174
1175/*
1176 * This is the callback which is called by the connection layer to receive data
1177 * into the buffer from the connection. It iterates over the mux layer's
1178 * rcv_buf function.
1179 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001180static int sc_conn_recv(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001181{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001182 struct connection *conn = __sc_conn(sc);
1183 struct channel *ic = sc_ic(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001184 int ret, max, cur_read = 0;
1185 int read_poll = MAX_READ_POLL_LOOPS;
1186 int flags = 0;
1187
1188 /* If not established yet, do nothing. */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001189 if (sc->state != SC_ST_EST)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001190 return 0;
1191
Willy Tarreau462b9892022-05-18 18:06:53 +02001192 /* If another call to sc_conn_recv() failed, and we subscribed to
Christopher Faulet5e29b762022-04-04 08:58:34 +02001193 * recv events already, give up now.
1194 */
Christopher Faulet95125882023-04-12 18:35:18 +02001195 if ((sc->wait_event.events & SUB_RETRY_RECV) || sc_waiting_room(sc))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001196 return 0;
1197
Christopher Fauletcfc11c02023-04-13 16:10:23 +02001198 /* maybe we were called immediately after an asynchronous abort */
Christopher Fauletca5309a2023-04-17 16:17:32 +02001199 if (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001200 return 1;
1201
1202 /* we must wait because the mux is not installed yet */
1203 if (!conn->mux)
1204 return 0;
1205
Christopher Faulet5e29b762022-04-04 08:58:34 +02001206 /* stop immediately on errors. Note that we DON'T want to stop on
1207 * POLL_ERR, as the poller might report a write error while there
1208 * are still data available in the recv buffer. This typically
1209 * happens when we send too large a request to a backend server
1210 * which rejects it before reading it all.
1211 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001212 if (!sc_ep_test(sc, SE_FL_RCV_MORE)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001213 if (!conn_xprt_ready(conn))
1214 return 0;
Willy Tarreau0adb2812022-05-27 10:02:48 +02001215 if (sc_ep_test(sc, SE_FL_ERROR))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001216 goto end_recv;
1217 }
1218
1219 /* prepare to detect if the mux needs more room */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001220 sc_ep_clr(sc, SE_FL_WANT_ROOM);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001221
1222 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) && !co_data(ic) &&
1223 global.tune.idle_timer &&
1224 (unsigned short)(now_ms - ic->last_read) >= global.tune.idle_timer) {
1225 /* The buffer was empty and nothing was transferred for more
1226 * than one second. This was caused by a pause and not by
1227 * congestion. Reset any streaming mode to reduce latency.
1228 */
1229 ic->xfer_small = 0;
1230 ic->xfer_large = 0;
1231 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
1232 }
1233
1234 /* First, let's see if we may splice data across the channel without
1235 * using a buffer.
1236 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001237 if (sc_ep_test(sc, SE_FL_MAY_SPLICE) &&
Christopher Faulet5e29b762022-04-04 08:58:34 +02001238 (ic->pipe || ic->to_forward >= MIN_SPLICE_FORWARD) &&
1239 ic->flags & CF_KERN_SPLICING) {
1240 if (c_data(ic)) {
1241 /* We're embarrassed, there are already data pending in
1242 * the buffer and we don't want to have them at two
1243 * locations at a time. Let's indicate we need some
1244 * place and ask the consumer to hurry.
1245 */
1246 flags |= CO_RFL_BUF_FLUSH;
1247 goto abort_splice;
1248 }
1249
1250 if (unlikely(ic->pipe == NULL)) {
1251 if (pipes_used >= global.maxpipes || !(ic->pipe = get_pipe())) {
1252 ic->flags &= ~CF_KERN_SPLICING;
1253 goto abort_splice;
1254 }
1255 }
1256
Willy Tarreau0adb2812022-05-27 10:02:48 +02001257 ret = conn->mux->rcv_pipe(sc, ic->pipe, ic->to_forward);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001258 if (ret < 0) {
1259 /* splice not supported on this end, let's disable it */
1260 ic->flags &= ~CF_KERN_SPLICING;
1261 goto abort_splice;
1262 }
1263
1264 if (ret > 0) {
1265 if (ic->to_forward != CHN_INFINITE_FORWARD)
1266 ic->to_forward -= ret;
1267 ic->total += ret;
1268 cur_read += ret;
Christopher Faulet285f7612022-12-12 08:28:55 +01001269 ic->flags |= CF_READ_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001270 }
1271
Willy Tarreau0adb2812022-05-27 10:02:48 +02001272 if (sc_ep_test(sc, SE_FL_EOS | SE_FL_ERROR))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001273 goto end_recv;
1274
1275 if (conn->flags & CO_FL_WAIT_ROOM) {
1276 /* the pipe is full or we have read enough data that it
1277 * could soon be full. Let's stop before needing to poll.
1278 */
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02001279 sc_need_room(sc, 0);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001280 goto done_recv;
1281 }
1282
1283 /* splice not possible (anymore), let's go on on standard copy */
1284 }
1285
1286 abort_splice:
1287 if (ic->pipe && unlikely(!ic->pipe->data)) {
1288 put_pipe(ic->pipe);
1289 ic->pipe = NULL;
1290 }
1291
Willy Tarreau0adb2812022-05-27 10:02:48 +02001292 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 +02001293 /* don't break splicing by reading, but still call rcv_buf()
1294 * to pass the flag.
1295 */
1296 goto done_recv;
1297 }
1298
1299 /* now we'll need a input buffer for the stream */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001300 if (!sc_alloc_ibuf(sc, &(__sc_strm(sc)->buffer_wait)))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001301 goto end_recv;
1302
1303 /* For an HTX stream, if the buffer is stuck (no output data with some
1304 * input data) and if the HTX message is fragmented or if its free space
1305 * wraps, we force an HTX deframentation. It is a way to have a
1306 * contiguous free space nad to let the mux to copy as much data as
1307 * possible.
1308 *
1309 * NOTE: A possible optim may be to let the mux decides if defrag is
1310 * required or not, depending on amount of data to be xferred.
1311 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001312 if (IS_HTX_STRM(__sc_strm(sc)) && !co_data(ic)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001313 struct htx *htx = htxbuf(&ic->buf);
1314
1315 if (htx_is_not_empty(htx) && ((htx->flags & HTX_FL_FRAGMENTED) || htx_space_wraps(htx)))
1316 htx_defrag(htx, NULL, 0);
1317 }
1318
1319 /* Instruct the mux it must subscribed for read events */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001320 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 +02001321
1322 /* Important note : if we're called with POLL_IN|POLL_HUP, it means the read polling
1323 * was enabled, which implies that the recv buffer was not full. So we have a guarantee
1324 * that if such an event is not handled above in splice, it will be handled here by
1325 * recv().
1326 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001327 while (sc_ep_test(sc, SE_FL_RCV_MORE) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001328 (!(conn->flags & CO_FL_HANDSHAKE) &&
Christopher Fauletca5309a2023-04-17 16:17:32 +02001329 (!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 +02001330 int cur_flags = flags;
1331
1332 /* Compute transient CO_RFL_* flags */
1333 if (co_data(ic)) {
1334 cur_flags |= (CO_RFL_BUF_WET | CO_RFL_BUF_NOT_STUCK);
1335 }
1336
1337 /* <max> may be null. This is the mux responsibility to set
Willy Tarreaue68bc612022-05-27 11:23:05 +02001338 * SE_FL_RCV_MORE on the SC if more space is needed.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001339 */
1340 max = channel_recv_max(ic);
Willy Tarreau0adb2812022-05-27 10:02:48 +02001341 ret = conn->mux->rcv_buf(sc, &ic->buf, max, cur_flags);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001342
Willy Tarreau0adb2812022-05-27 10:02:48 +02001343 if (sc_ep_test(sc, SE_FL_WANT_ROOM)) {
Willy Tarreaub605c422022-05-17 17:04:55 +02001344 /* SE_FL_WANT_ROOM must not be reported if the channel's
Christopher Faulet5e29b762022-04-04 08:58:34 +02001345 * buffer is empty.
1346 */
1347 BUG_ON(c_empty(ic));
1348
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02001349 sc_need_room(sc, channel_recv_max(ic) + 1);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001350 /* Add READ_PARTIAL because some data are pending but
1351 * cannot be xferred to the channel
1352 */
Christopher Faulet285f7612022-12-12 08:28:55 +01001353 ic->flags |= CF_READ_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001354 }
1355
1356 if (ret <= 0) {
1357 /* if we refrained from reading because we asked for a
1358 * flush to satisfy rcv_pipe(), we must not subscribe
1359 * and instead report that there's not enough room
1360 * here to proceed.
1361 */
1362 if (flags & CO_RFL_BUF_FLUSH)
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02001363 sc_need_room(sc, -1);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001364 break;
1365 }
1366
1367 cur_read += ret;
1368
1369 /* if we're allowed to directly forward data, we must update ->o */
Christopher Faulet64350bb2023-04-13 16:37:37 +02001370 if (ic->to_forward && !(sc_opposite(sc)->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED))) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001371 unsigned long fwd = ret;
1372 if (ic->to_forward != CHN_INFINITE_FORWARD) {
1373 if (fwd > ic->to_forward)
1374 fwd = ic->to_forward;
1375 ic->to_forward -= fwd;
1376 }
1377 c_adv(ic, fwd);
1378 }
1379
Christopher Faulet285f7612022-12-12 08:28:55 +01001380 ic->flags |= CF_READ_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001381 ic->total += ret;
1382
1383 /* End-of-input reached, we can leave. In this case, it is
Willy Tarreaue68bc612022-05-27 11:23:05 +02001384 * important to break the loop to not block the SC because of
Christopher Faulet5e29b762022-04-04 08:58:34 +02001385 * the channel's policies.This way, we are still able to receive
1386 * shutdowns.
1387 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001388 if (sc_ep_test(sc, SE_FL_EOI))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001389 break;
1390
Christopher Faulet9a790f62023-03-16 14:40:03 +01001391 if ((sc->flags & SC_FL_RCV_ONCE) || --read_poll <= 0) {
1392 /* we don't expect to read more data */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001393 sc_wont_read(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001394 break;
1395 }
1396
1397 /* if too many bytes were missing from last read, it means that
1398 * it's pointless trying to read again because the system does
1399 * not have them in buffers.
1400 */
1401 if (ret < max) {
1402 /* if a streamer has read few data, it may be because we
1403 * have exhausted system buffers. It's not worth trying
1404 * again.
1405 */
1406 if (ic->flags & CF_STREAMER) {
1407 /* we're stopped by the channel's policy */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001408 sc_wont_read(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001409 break;
1410 }
1411
1412 /* if we read a large block smaller than what we requested,
1413 * it's almost certain we'll never get anything more.
1414 */
1415 if (ret >= global.tune.recv_enough) {
1416 /* we're stopped by the channel's policy */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001417 sc_wont_read(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001418 break;
1419 }
1420 }
1421
1422 /* if we are waiting for more space, don't try to read more data
1423 * right now.
1424 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001425 if (sc->flags & (SC_FL_WONT_READ|SC_FL_NEED_BUFF|SC_FL_NEED_ROOM))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001426 break;
1427 } /* while !flags */
1428
1429 done_recv:
1430 if (cur_read) {
1431 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) &&
1432 (cur_read <= ic->buf.size / 2)) {
1433 ic->xfer_large = 0;
1434 ic->xfer_small++;
1435 if (ic->xfer_small >= 3) {
1436 /* we have read less than half of the buffer in
1437 * one pass, and this happened at least 3 times.
1438 * This is definitely not a streamer.
1439 */
1440 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
1441 }
1442 else if (ic->xfer_small >= 2) {
1443 /* if the buffer has been at least half full twice,
1444 * we receive faster than we send, so at least it
1445 * is not a "fast streamer".
1446 */
1447 ic->flags &= ~CF_STREAMER_FAST;
1448 }
1449 }
1450 else if (!(ic->flags & CF_STREAMER_FAST) &&
1451 (cur_read >= ic->buf.size - global.tune.maxrewrite)) {
1452 /* we read a full buffer at once */
1453 ic->xfer_small = 0;
1454 ic->xfer_large++;
1455 if (ic->xfer_large >= 3) {
1456 /* we call this buffer a fast streamer if it manages
1457 * to be filled in one call 3 consecutive times.
1458 */
1459 ic->flags |= (CF_STREAMER | CF_STREAMER_FAST);
1460 }
1461 }
1462 else {
1463 ic->xfer_small = 0;
1464 ic->xfer_large = 0;
1465 }
1466 ic->last_read = now_ms;
Christopher Faulet4c135682023-02-16 11:09:31 +01001467 sc_ep_report_read_activity(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001468 }
1469
1470 end_recv:
1471 ret = (cur_read != 0);
1472
1473 /* Report EOI on the channel if it was reached from the mux point of
1474 * view. */
Christopher Faulet904763f2023-03-22 14:53:11 +01001475 if (sc_ep_test(sc, SE_FL_EOI) && !(sc->flags & SC_FL_EOI)) {
Christopher Faulet4c135682023-02-16 11:09:31 +01001476 sc_ep_report_read_activity(sc);
Christopher Faulet904763f2023-03-22 14:53:11 +01001477 sc->flags |= SC_FL_EOI;
1478 ic->flags |= CF_READ_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001479 ret = 1;
1480 }
1481
Christopher Fauletb208d8c2023-03-21 11:25:21 +01001482 if (sc_ep_test(sc, SE_FL_EOS)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001483 /* we received a shutdown */
Christopher Faulet5e29b762022-04-04 08:58:34 +02001484 if (ic->flags & CF_AUTO_CLOSE)
Christopher Fauletdf7cd712023-04-13 15:56:26 +02001485 sc_schedule_shutdown(sc_opposite(sc));
Christopher Faulet1aec6c92023-04-17 17:29:29 +02001486 sc_conn_eos(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001487 ret = 1;
1488 }
Christopher Fauletb208d8c2023-03-21 11:25:21 +01001489
Christopher Fauleta1d14a72023-04-14 10:42:08 +02001490 if (sc_ep_test(sc, SE_FL_ERROR)) {
1491 sc->flags |= SC_FL_ERROR;
Christopher Fauletb208d8c2023-03-21 11:25:21 +01001492 ret = 1;
Christopher Fauleta1d14a72023-04-14 10:42:08 +02001493 }
Willy Tarreau0adb2812022-05-27 10:02:48 +02001494 else if (!(sc->flags & (SC_FL_WONT_READ|SC_FL_NEED_BUFF|SC_FL_NEED_ROOM)) &&
Christopher Fauletca5309a2023-04-17 16:17:32 +02001495 !(sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001496 /* Subscribe to receive events if we're blocking on I/O */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001497 conn->mux->subscribe(sc, SUB_RETRY_RECV, &sc->wait_event);
1498 se_have_no_more_data(sc->sedesc);
Christopher Fauletb208d8c2023-03-21 11:25:21 +01001499 }
1500 else {
Willy Tarreau0adb2812022-05-27 10:02:48 +02001501 se_have_more_data(sc->sedesc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001502 ret = 1;
1503 }
Christopher Faulet8019f782023-03-23 17:30:29 +01001504
1505 BUG_ON_HOT((sc_ep_get(sc) & (SE_FL_EOI|SE_FL_EOS|SE_FL_ERROR)) == SE_FL_EOS);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001506 return ret;
1507}
1508
Willy Tarreau4596fe22022-05-17 19:07:51 +02001509/* This tries to perform a synchronous receive on the stream connector to
Christopher Faulet5e29b762022-04-04 08:58:34 +02001510 * try to collect last arrived data. In practice it's only implemented on
Willy Tarreau4596fe22022-05-17 19:07:51 +02001511 * stconns. Returns 0 if nothing was done, non-zero if new data or a
Christopher Faulet5e29b762022-04-04 08:58:34 +02001512 * shutdown were collected. This may result on some delayed receive calls
1513 * to be programmed and performed later, though it doesn't provide any
1514 * such guarantee.
1515 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001516int sc_conn_sync_recv(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001517{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001518 if (!sc_state_in(sc->state, SC_SB_RDY|SC_SB_EST))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001519 return 0;
1520
Willy Tarreau0adb2812022-05-27 10:02:48 +02001521 if (!sc_mux_ops(sc))
Willy Tarreau4596fe22022-05-17 19:07:51 +02001522 return 0; // only stconns are supported
Christopher Faulet5e29b762022-04-04 08:58:34 +02001523
Willy Tarreau0adb2812022-05-27 10:02:48 +02001524 if (sc->wait_event.events & SUB_RETRY_RECV)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001525 return 0; // already subscribed
1526
Willy Tarreau0adb2812022-05-27 10:02:48 +02001527 if (!sc_is_recv_allowed(sc))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001528 return 0; // already failed
1529
Willy Tarreau0adb2812022-05-27 10:02:48 +02001530 return sc_conn_recv(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001531}
1532
1533/*
1534 * This function is called to send buffer data to a stream socket.
1535 * It calls the mux layer's snd_buf function. It relies on the
1536 * caller to commit polling changes. The caller should check conn->flags
1537 * for errors.
1538 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001539static int sc_conn_send(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001540{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001541 struct connection *conn = __sc_conn(sc);
Christopher Faulet904763f2023-03-22 14:53:11 +01001542 struct stconn *sco = sc_opposite(sc);
Willy Tarreau0adb2812022-05-27 10:02:48 +02001543 struct stream *s = __sc_strm(sc);
1544 struct channel *oc = sc_oc(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001545 int ret;
1546 int did_send = 0;
1547
Willy Tarreau0adb2812022-05-27 10:02:48 +02001548 if (sc_ep_test(sc, SE_FL_ERROR | SE_FL_ERR_PENDING) || sc_is_conn_error(sc)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001549 /* We're probably there because the tasklet was woken up,
1550 * but process_stream() ran before, detected there were an
Willy Tarreaue68bc612022-05-27 11:23:05 +02001551 * error and put the SC back to SC_ST_TAR. There's still
Christopher Faulet5e29b762022-04-04 08:58:34 +02001552 * CO_FL_ERROR on the connection but we don't want to add
Willy Tarreaub605c422022-05-17 17:04:55 +02001553 * SE_FL_ERROR back, so give up
Christopher Faulet5e29b762022-04-04 08:58:34 +02001554 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001555 if (sc->state < SC_ST_CON)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001556 return 0;
Christopher Faulet56a2b602023-04-14 09:42:59 +02001557 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 +02001558 return 1;
1559 }
1560
1561 /* We're already waiting to be able to send, give up */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001562 if (sc->wait_event.events & SUB_RETRY_SEND)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001563 return 0;
1564
1565 /* we might have been called just after an asynchronous shutw */
Christopher Faulet208c7122023-04-13 16:16:15 +02001566 if (sc->flags & SC_FL_SHUT_DONE)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001567 return 1;
1568
1569 /* we must wait because the mux is not installed yet */
1570 if (!conn->mux)
1571 return 0;
1572
1573 if (oc->pipe && conn->xprt->snd_pipe && conn->mux->snd_pipe) {
Willy Tarreau0adb2812022-05-27 10:02:48 +02001574 ret = conn->mux->snd_pipe(sc, oc->pipe);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001575 if (ret > 0)
1576 did_send = 1;
1577
1578 if (!oc->pipe->data) {
1579 put_pipe(oc->pipe);
1580 oc->pipe = NULL;
1581 }
1582
1583 if (oc->pipe)
1584 goto end;
1585 }
1586
1587 /* At this point, the pipe is empty, but we may still have data pending
1588 * in the normal buffer.
1589 */
1590 if (co_data(oc)) {
1591 /* when we're here, we already know that there is no spliced
1592 * data left, and that there are sendable buffered data.
1593 */
1594
1595 /* check if we want to inform the kernel that we're interested in
1596 * sending more data after this call. We want this if :
1597 * - we're about to close after this last send and want to merge
1598 * the ongoing FIN with the last segment.
1599 * - we know we can't send everything at once and must get back
1600 * here because of unaligned data
1601 * - there is still a finite amount of data to forward
1602 * The test is arranged so that the most common case does only 2
1603 * tests.
1604 */
1605 unsigned int send_flag = 0;
1606
Christopher Faulet68ef2182023-03-17 15:38:18 +01001607 if ((!(sc->flags & (SC_FL_SND_ASAP|SC_FL_SND_NEVERWAIT)) &&
Christopher Faulet5e29b762022-04-04 08:58:34 +02001608 ((oc->to_forward && oc->to_forward != CHN_INFINITE_FORWARD) ||
Christopher Faulet84d3ef92023-03-17 15:45:58 +01001609 (sc->flags & SC_FL_SND_EXP_MORE) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001610 (IS_HTX_STRM(s) &&
Christopher Fauletca5309a2023-04-17 16:17:32 +02001611 (!(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 +02001612 ((oc->flags & CF_ISRESP) &&
Christopher Faulet87633c32023-04-03 18:32:50 +02001613 (oc->flags & CF_AUTO_CLOSE) &&
Christopher Faulete38534c2023-04-13 15:45:24 +02001614 (sc->flags & SC_FL_SHUT_WANTED)))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001615 send_flag |= CO_SFL_MSG_MORE;
1616
1617 if (oc->flags & CF_STREAMER)
1618 send_flag |= CO_SFL_STREAMER;
1619
1620 if (s->txn && s->txn->flags & TX_L7_RETRY && !b_data(&s->txn->l7_buffer)) {
1621 /* If we want to be able to do L7 retries, copy
1622 * the data we're about to send, so that we are able
1623 * to resend them if needed
1624 */
1625 /* Try to allocate a buffer if we had none.
1626 * If it fails, the next test will just
1627 * disable the l7 retries by setting
1628 * l7_conn_retries to 0.
1629 */
1630 if (s->txn->req.msg_state != HTTP_MSG_DONE)
1631 s->txn->flags &= ~TX_L7_RETRY;
1632 else {
1633 if (b_alloc(&s->txn->l7_buffer) == NULL)
1634 s->txn->flags &= ~TX_L7_RETRY;
1635 else {
1636 memcpy(b_orig(&s->txn->l7_buffer),
1637 b_orig(&oc->buf),
1638 b_size(&oc->buf));
1639 s->txn->l7_buffer.head = co_data(oc);
1640 b_add(&s->txn->l7_buffer, co_data(oc));
1641 }
1642
1643 }
1644 }
1645
Willy Tarreau0adb2812022-05-27 10:02:48 +02001646 ret = conn->mux->snd_buf(sc, &oc->buf, co_data(oc), send_flag);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001647 if (ret > 0) {
1648 did_send = 1;
1649 c_rew(oc, ret);
1650 c_realign_if_empty(oc);
1651
1652 if (!co_data(oc)) {
1653 /* Always clear both flags once everything has been sent, they're one-shot */
Christopher Faulet84d3ef92023-03-17 15:45:58 +01001654 sc->flags &= ~(SC_FL_SND_ASAP|SC_FL_SND_EXP_MORE);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001655 }
1656 /* if some data remain in the buffer, it's only because the
1657 * system buffers are full, we will try next time.
1658 */
Christopher Faulet13045f02022-04-01 14:23:38 +02001659 }
1660 }
Christopher Faulet5e29b762022-04-04 08:58:34 +02001661
1662 end:
1663 if (did_send) {
Christopher Fauletd8988412022-12-20 18:10:04 +01001664 oc->flags |= CF_WRITE_EVENT | CF_WROTE_DATA;
Willy Tarreau0adb2812022-05-27 10:02:48 +02001665 if (sc->state == SC_ST_CON)
1666 sc->state = SC_ST_RDY;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001667 }
1668
Christopher Faulete7405d42023-05-05 11:40:30 +02001669 if (!sco->room_needed || (did_send && (sco->room_needed < 0 || channel_recv_max(sc_oc(sc)) >= sco->room_needed)))
1670 sc_have_room(sco);
1671
Willy Tarreau0adb2812022-05-27 10:02:48 +02001672 if (sc_ep_test(sc, SE_FL_ERROR | SE_FL_ERR_PENDING)) {
Christopher Faulet2e56a732023-01-26 16:18:09 +01001673 oc->flags |= CF_WRITE_EVENT;
Christopher Faulet56a2b602023-04-14 09:42:59 +02001674 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 +02001675 if (sc_ep_test(sc, SE_FL_ERROR))
1676 sc->flags |= SC_FL_ERROR;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001677 return 1;
1678 }
1679
Christopher Faulet59b240c2023-02-27 16:38:12 +01001680 if (channel_is_empty(oc))
1681 sc_ep_report_send_activity(sc);
1682 else {
1683 /* 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 +02001684 conn->mux->subscribe(sc, SUB_RETRY_SEND, &sc->wait_event);
Christopher Faulet59b240c2023-02-27 16:38:12 +01001685 sc_ep_report_blocked_send(sc);
1686 }
1687
Christopher Faulet5e29b762022-04-04 08:58:34 +02001688 return did_send;
1689}
1690
Christopher Fauletd8988412022-12-20 18:10:04 +01001691/* perform a synchronous send() for the stream connector. The CF_WRITE_EVENT
1692 * flag are cleared prior to the attempt, and will possibly be updated in case
1693 * of success.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001694 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001695void sc_conn_sync_send(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001696{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001697 struct channel *oc = sc_oc(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001698
Christopher Fauletd8988412022-12-20 18:10:04 +01001699 oc->flags &= ~CF_WRITE_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001700
Christopher Faulet208c7122023-04-13 16:16:15 +02001701 if (sc->flags & SC_FL_SHUT_DONE)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001702 return;
1703
1704 if (channel_is_empty(oc))
1705 return;
1706
Willy Tarreau0adb2812022-05-27 10:02:48 +02001707 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001708 return;
1709
Willy Tarreau0adb2812022-05-27 10:02:48 +02001710 if (!sc_mux_ops(sc))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001711 return;
1712
Willy Tarreau0adb2812022-05-27 10:02:48 +02001713 sc_conn_send(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001714}
1715
1716/* Called by I/O handlers after completion.. It propagates
Willy Tarreau4596fe22022-05-17 19:07:51 +02001717 * connection flags to the stream connector, updates the stream (which may or
Christopher Faulet5e29b762022-04-04 08:58:34 +02001718 * may not take this opportunity to try to forward data), then update the
Willy Tarreau4596fe22022-05-17 19:07:51 +02001719 * connection's polling based on the channels and stream connector's final
Christopher Faulet5e29b762022-04-04 08:58:34 +02001720 * states. The function always returns 0.
1721 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001722static int sc_conn_process(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001723{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001724 struct connection *conn = __sc_conn(sc);
1725 struct channel *ic = sc_ic(sc);
1726 struct channel *oc = sc_oc(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001727
1728 BUG_ON(!conn);
1729
1730 /* If we have data to send, try it now */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001731 if (!channel_is_empty(oc) && !(sc->wait_event.events & SUB_RETRY_SEND))
1732 sc_conn_send(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001733
Willy Tarreau4596fe22022-05-17 19:07:51 +02001734 /* First step, report to the stream connector what was detected at the
Christopher Faulet5e29b762022-04-04 08:58:34 +02001735 * connection layer : errors and connection establishment.
Christopher Faulet88d05a02023-04-14 12:03:50 +02001736 * Only add SC_FL_ERROR if we're connected, or we're attempting to
Christopher Faulet5e29b762022-04-04 08:58:34 +02001737 * connect, we may get there because we got woken up, but only run
1738 * after process_stream() noticed there were an error, and decided
1739 * to retry to connect, the connection may still have CO_FL_ERROR,
Christopher Faulet88d05a02023-04-14 12:03:50 +02001740 * and we don't want to add SC_FL_ERROR back
Christopher Faulet5e29b762022-04-04 08:58:34 +02001741 *
Willy Tarreau462b9892022-05-18 18:06:53 +02001742 * Note: This test is only required because sc_conn_process is also the SI
1743 * wake callback. Otherwise sc_conn_recv()/sc_conn_send() already take
Christopher Faulet5e29b762022-04-04 08:58:34 +02001744 * care of it.
1745 */
1746
Willy Tarreau0adb2812022-05-27 10:02:48 +02001747 if (sc->state >= SC_ST_CON) {
Christopher Faulet88d05a02023-04-14 12:03:50 +02001748 if (sc_is_conn_error(sc))
Christopher Fauleta1d14a72023-04-14 10:42:08 +02001749 sc->flags |= SC_FL_ERROR;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001750 }
1751
1752 /* If we had early data, and the handshake ended, then
1753 * we can remove the flag, and attempt to wake the task up,
1754 * in the event there's an analyser waiting for the end of
1755 * the handshake.
1756 */
1757 if (!(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS)) &&
Willy Tarreau0adb2812022-05-27 10:02:48 +02001758 sc_ep_test(sc, SE_FL_WAIT_FOR_HS)) {
1759 sc_ep_clr(sc, SE_FL_WAIT_FOR_HS);
1760 task_wakeup(sc_strm_task(sc), TASK_WOKEN_MSG);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001761 }
1762
Willy Tarreau0adb2812022-05-27 10:02:48 +02001763 if (!sc_state_in(sc->state, SC_SB_EST|SC_SB_DIS|SC_SB_CLO) &&
Christopher Faulet5e29b762022-04-04 08:58:34 +02001764 (conn->flags & CO_FL_WAIT_XPRT) == 0) {
Christopher Fauletca679922022-07-20 13:24:04 +02001765 if (sc->flags & SC_FL_ISBACK)
1766 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Fauletb96f2aa2022-12-12 08:11:36 +01001767 oc->flags |= CF_WRITE_EVENT;
Willy Tarreau0adb2812022-05-27 10:02:48 +02001768 if (sc->state == SC_ST_CON)
1769 sc->state = SC_ST_RDY;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001770 }
1771
1772 /* Report EOS on the channel if it was reached from the mux point of
1773 * view.
1774 *
Willy Tarreau462b9892022-05-18 18:06:53 +02001775 * Note: This test is only required because sc_conn_process is also the SI
1776 * wake callback. Otherwise sc_conn_recv()/sc_conn_send() already take
Christopher Faulet5e29b762022-04-04 08:58:34 +02001777 * care of it.
1778 */
Christopher Faulet1aec6c92023-04-17 17:29:29 +02001779 if (sc_ep_test(sc, SE_FL_EOS) && !(sc->flags & SC_FL_EOS)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001780 /* we received a shutdown */
Christopher Faulet5e29b762022-04-04 08:58:34 +02001781 if (ic->flags & CF_AUTO_CLOSE)
Christopher Fauletdf7cd712023-04-13 15:56:26 +02001782 sc_schedule_shutdown(sc_opposite(sc));
Christopher Faulet1aec6c92023-04-17 17:29:29 +02001783 sc_conn_eos(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001784 }
1785
1786 /* Report EOI on the channel if it was reached from the mux point of
1787 * view.
1788 *
Willy Tarreau462b9892022-05-18 18:06:53 +02001789 * Note: This test is only required because sc_conn_process is also the SI
1790 * wake callback. Otherwise sc_conn_recv()/sc_conn_send() already take
Christopher Faulet5e29b762022-04-04 08:58:34 +02001791 * care of it.
1792 */
Christopher Faulet904763f2023-03-22 14:53:11 +01001793 if (sc_ep_test(sc, SE_FL_EOI) && !(sc->flags & SC_FL_EOI)) {
1794 sc->flags |= SC_FL_EOI;
1795 ic->flags |= CF_READ_EVENT;
1796 }
Christopher Faulet5e29b762022-04-04 08:58:34 +02001797
Christopher Fauleta1d14a72023-04-14 10:42:08 +02001798 if (sc_ep_test(sc, SE_FL_ERROR))
1799 sc->flags |= SC_FL_ERROR;
1800
Willy Tarreau4596fe22022-05-17 19:07:51 +02001801 /* Second step : update the stream connector and channels, try to forward any
Christopher Faulet5e29b762022-04-04 08:58:34 +02001802 * pending data, then possibly wake the stream up based on the new
Willy Tarreau4596fe22022-05-17 19:07:51 +02001803 * stream connector status.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001804 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001805 sc_notify(sc);
1806 stream_release_buffers(__sc_strm(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001807 return 0;
1808}
1809
Willy Tarreau4596fe22022-05-17 19:07:51 +02001810/* This is the ->process() function for any stream connector's wait_event task.
1811 * It's assigned during the stream connector's initialization, for any type of
1812 * stream connector. Thus it is always safe to perform a tasklet_wakeup() on a
Willy Tarreaue68bc612022-05-27 11:23:05 +02001813 * stream connector, as the presence of the SC is checked there.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001814 */
Willy Tarreau462b9892022-05-18 18:06:53 +02001815struct task *sc_conn_io_cb(struct task *t, void *ctx, unsigned int state)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001816{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001817 struct stconn *sc = ctx;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001818 int ret = 0;
1819
Willy Tarreau0adb2812022-05-27 10:02:48 +02001820 if (!sc_conn(sc))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001821 return t;
1822
Willy Tarreau0adb2812022-05-27 10:02:48 +02001823 if (!(sc->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(sc_oc(sc)))
1824 ret = sc_conn_send(sc);
1825 if (!(sc->wait_event.events & SUB_RETRY_RECV))
1826 ret |= sc_conn_recv(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001827 if (ret != 0)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001828 sc_conn_process(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001829
Willy Tarreau0adb2812022-05-27 10:02:48 +02001830 stream_release_buffers(__sc_strm(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001831 return t;
1832}
1833
Christopher Fauletb36e5122023-04-17 17:32:43 +02001834/*
1835 * This function propagates an end-of-stream received from an applet. It
1836 * updates the stream connector. If it is is already shut, the applet is
1837 * released. Otherwise, we try to forward the shutdown, immediately or ASAP.
1838 */
1839static void sc_applet_eos(struct stconn *sc)
1840{
1841 struct channel *ic = sc_ic(sc);
1842
1843 BUG_ON(!sc_appctx(sc));
1844
1845 if (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))
1846 return;
1847 sc->flags |= SC_FL_EOS;
1848 ic->flags |= CF_READ_EVENT;
1849
1850 /* Note: on abort, we don't call the applet */
1851
1852 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
1853 return;
1854
1855 if (sc->flags & SC_FL_SHUT_DONE) {
1856 appctx_shut(__sc_appctx(sc));
1857 sc->state = SC_ST_DIS;
1858 if (sc->flags & SC_FL_ISBACK)
1859 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
1860 }
1861 else if (sc_cond_forward_shut(sc))
1862 return sc_app_shut_applet(sc);
1863}
1864
Christopher Faulet5e29b762022-04-04 08:58:34 +02001865/* Callback to be used by applet handlers upon completion. It updates the stream
1866 * (which may or may not take this opportunity to try to forward data), then
Willy Tarreau4596fe22022-05-17 19:07:51 +02001867 * may re-enable the applet's based on the channels and stream connector's final
Christopher Faulet5e29b762022-04-04 08:58:34 +02001868 * states.
1869 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001870static int sc_applet_process(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001871{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001872 struct channel *ic = sc_ic(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001873
Willy Tarreau0adb2812022-05-27 10:02:48 +02001874 BUG_ON(!sc_appctx(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001875
Christopher Fauletf8fbb6d2023-03-21 11:49:21 +01001876 /* Report EOI on the channel if it was reached from the applet point of
1877 * view. */
Christopher Faulet904763f2023-03-22 14:53:11 +01001878 if (sc_ep_test(sc, SE_FL_EOI) && !(sc->flags & SC_FL_EOI)) {
Christopher Fauletf8fbb6d2023-03-21 11:49:21 +01001879 sc_ep_report_read_activity(sc);
Christopher Faulet904763f2023-03-22 14:53:11 +01001880 sc->flags |= SC_FL_EOI;
1881 ic->flags |= CF_READ_EVENT;
Christopher Fauletf8fbb6d2023-03-21 11:49:21 +01001882 }
1883
Christopher Fauleta1d14a72023-04-14 10:42:08 +02001884 if (sc_ep_test(sc, SE_FL_ERROR))
1885 sc->flags |= SC_FL_ERROR;
1886
Christopher Faulet0ffc9d72023-03-21 14:19:08 +01001887 if (sc_ep_test(sc, SE_FL_EOS)) {
1888 /* we received a shutdown */
Christopher Fauletb36e5122023-04-17 17:32:43 +02001889 sc_applet_eos(sc);
Christopher Faulet0ffc9d72023-03-21 14:19:08 +01001890 }
1891
Christopher Faulete8bcef52023-04-14 09:45:41 +02001892 BUG_ON(sc_ep_test(sc, SE_FL_HAVE_NO_DATA|SE_FL_EOI) == SE_FL_EOI);
1893
Christopher Faulet5e29b762022-04-04 08:58:34 +02001894 /* If the applet wants to write and the channel is closed, it's a
1895 * broken pipe and it must be reported.
1896 */
Christopher Fauletca5309a2023-04-17 16:17:32 +02001897 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 +02001898 sc_ep_set(sc, SE_FL_ERROR);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001899
1900 /* automatically mark the applet having data available if it reported
1901 * begin blocked by the channel.
1902 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001903 if ((sc->flags & (SC_FL_WONT_READ|SC_FL_NEED_BUFF|SC_FL_NEED_ROOM)) ||
1904 sc_ep_test(sc, SE_FL_APPLET_NEED_CONN))
1905 applet_have_more_data(__sc_appctx(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001906
Willy Tarreau4596fe22022-05-17 19:07:51 +02001907 /* update the stream connector, channels, and possibly wake the stream up */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001908 sc_notify(sc);
1909 stream_release_buffers(__sc_strm(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001910
Willy Tarreau19c65a92022-05-27 08:49:24 +02001911 /* sc_notify may have passed through chk_snd and released some blocking
Willy Tarreau15252cd2022-05-25 16:36:21 +02001912 * flags. Process_stream will consider those flags to wake up the
Christopher Faulet5e29b762022-04-04 08:58:34 +02001913 * appctx but in the case the task is not in runqueue we may have to
1914 * wakeup the appctx immediately.
1915 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001916 if (sc_is_recv_allowed(sc) || sc_is_send_allowed(sc))
1917 appctx_wakeup(__sc_appctx(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001918 return 0;
Christopher Faulet13045f02022-04-01 14:23:38 +02001919}
Christopher Fauletb68f77d2022-06-16 16:24:16 +02001920
1921
1922/* Prepares an endpoint upgrade. We don't now at this stage if the upgrade will
1923 * succeed or not and if the stconn will be reused by the new endpoint. Thus,
1924 * for now, only pretend the stconn is detached.
1925 */
1926void sc_conn_prepare_endp_upgrade(struct stconn *sc)
1927{
1928 BUG_ON(!sc_conn(sc) || !sc->app);
1929 sc_ep_clr(sc, SE_FL_T_MUX);
1930 sc_ep_set(sc, SE_FL_DETACHED);
1931}
1932
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001933/* Endpoint upgrade failed. Restore the stconn state. */
Christopher Fauletb68f77d2022-06-16 16:24:16 +02001934void sc_conn_abort_endp_upgrade(struct stconn *sc)
1935{
1936 sc_ep_set(sc, SE_FL_T_MUX);
1937 sc_ep_clr(sc, SE_FL_DETACHED);
1938}
1939
1940/* Commit the endpoint upgrade. If stconn is attached, it means the new endpoint
1941 * use it. So we do nothing. Otherwise, the stconn will be destroy with the
1942 * overlying stream. So, it means we must commit the detach.
1943*/
1944void sc_conn_commit_endp_upgrade(struct stconn *sc)
1945{
1946 if (!sc_ep_test(sc, SE_FL_DETACHED))
1947 return;
1948 sc_detach_endp(&sc);
1949 /* Because it was already set as detached, the sedesc must be preserved */
Willy Tarreau6a378d12022-08-11 13:56:42 +02001950 BUG_ON(!sc);
Christopher Fauletb68f77d2022-06-16 16:24:16 +02001951 BUG_ON(!sc->sedesc);
1952}