blob: 8458331895d6d57f83a04560a5e9be62c0008b63 [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;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200139 sc->app = NULL;
140 sc->app_ops = NULL;
141 sc->src = NULL;
142 sc->dst = NULL;
143 sc->wait_event.tasklet = NULL;
144 sc->wait_event.events = 0;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200145
Christopher Faulet9ed77422022-04-12 08:51:15 +0200146 /* If there is no endpoint, allocate a new one now */
Willy Tarreauea59b022022-05-17 17:53:22 +0200147 if (!sedesc) {
148 sedesc = sedesc_new();
149 if (unlikely(!sedesc))
Christopher Fauletb669d682022-03-22 18:37:19 +0100150 goto alloc_error;
151 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200152 sc->sedesc = sedesc;
153 sedesc->sc = sc;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100154
Willy Tarreau0adb2812022-05-27 10:02:48 +0200155 return sc;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100156
157 alloc_error:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200158 pool_free(pool_head_connstream, sc);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100159 return NULL;
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100160}
161
Willy Tarreau31219282022-05-27 16:21:33 +0200162/* Creates a new stream connector and its associated stream from a mux. <sd> must
163 * be defined. It returns NULL on error. On success, the new stream connector is
Willy Tarreaub605c422022-05-17 17:04:55 +0200164 * returned. In this case, SE_FL_ORPHAN flag is removed.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200165 */
Willy Tarreau31219282022-05-27 16:21:33 +0200166struct stconn *sc_new_from_endp(struct sedesc *sd, struct session *sess, struct buffer *input)
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100167{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200168 struct stconn *sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100169
Willy Tarreau31219282022-05-27 16:21:33 +0200170 sc = sc_new(sd);
Willy Tarreau0adb2812022-05-27 10:02:48 +0200171 if (unlikely(!sc))
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100172 return NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200173 if (unlikely(!stream_new(sess, sc, input))) {
Christopher Faulet3ab72c62022-09-27 09:18:20 +0200174 sd->sc = NULL;
Willy Tarreau7a8ca0a2023-03-20 19:53:14 +0100175 if (sc->sedesc != sd) {
176 /* none was provided so sc_new() allocated one */
177 sedesc_free(sc->sedesc);
178 }
179 pool_free(pool_head_connstream, sc);
Christopher Faulet3ab72c62022-09-27 09:18:20 +0200180 se_fl_set(sd, SE_FL_ORPHAN);
181 return NULL;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100182 }
Willy Tarreau31219282022-05-27 16:21:33 +0200183 se_fl_clr(sd, SE_FL_ORPHAN);
Willy Tarreau0adb2812022-05-27 10:02:48 +0200184 return sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100185}
186
Willy Tarreau4596fe22022-05-17 19:07:51 +0200187/* Creates a new stream connector from an stream. There is no endpoint here, thus it
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200188 * will be created by sc_new(). So the SE_FL_DETACHED flag is set. It returns
Willy Tarreau4596fe22022-05-17 19:07:51 +0200189 * NULL on error. On success, the new stream connector is returned.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200190 */
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200191struct stconn *sc_new_from_strm(struct stream *strm, unsigned int flags)
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100192{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200193 struct stconn *sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100194
Willy Tarreau0adb2812022-05-27 10:02:48 +0200195 sc = sc_new(NULL);
196 if (unlikely(!sc))
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100197 return NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200198 sc->flags |= flags;
199 sc_ep_set(sc, SE_FL_DETACHED);
200 sc->app = &strm->obj_type;
201 sc->app_ops = &sc_app_embedded_ops;
202 return sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100203}
204
Willy Tarreau4596fe22022-05-17 19:07:51 +0200205/* Creates a new stream connector from an health-check. There is no endpoint here,
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200206 * thus it will be created by sc_new(). So the SE_FL_DETACHED flag is set. It
Willy Tarreau4596fe22022-05-17 19:07:51 +0200207 * returns NULL on error. On success, the new stream connector is returned.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200208 */
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200209struct stconn *sc_new_from_check(struct check *check, unsigned int flags)
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100210{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200211 struct stconn *sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100212
Willy Tarreau0adb2812022-05-27 10:02:48 +0200213 sc = sc_new(NULL);
214 if (unlikely(!sc))
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100215 return NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200216 sc->flags |= flags;
217 sc_ep_set(sc, SE_FL_DETACHED);
218 sc->app = &check->obj_type;
219 sc->app_ops = &sc_app_check_ops;
220 return sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100221}
222
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200223/* Releases a stconn previously allocated by sc_new(), as well as its
Christopher Faulet9ed77422022-04-12 08:51:15 +0200224 * endpoint, if it exists. This function is called internally or on error path.
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100225 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200226void sc_free(struct stconn *sc)
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100227{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200228 sockaddr_free(&sc->src);
229 sockaddr_free(&sc->dst);
230 if (sc->sedesc) {
231 BUG_ON(!sc_ep_test(sc, SE_FL_DETACHED));
232 sedesc_free(sc->sedesc);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100233 }
Tim Duesterhusb1ec21d2023-04-22 17:47:32 +0200234 tasklet_free(sc->wait_event.tasklet);
Willy Tarreau0adb2812022-05-27 10:02:48 +0200235 pool_free(pool_head_connstream, sc);
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100236}
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100237
Willy Tarreau4596fe22022-05-17 19:07:51 +0200238/* Conditionally removes a stream connector if it is detached and if there is no app
Christopher Fauleteb50c012022-04-21 14:22:53 +0200239 * layer defined. Except on error path, this one must be used. if release, the
Willy Tarreaue68bc612022-05-27 11:23:05 +0200240 * pointer on the SC is set to NULL.
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200241 */
Willy Tarreaue68bc612022-05-27 11:23:05 +0200242static void sc_free_cond(struct stconn **scp)
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200243{
Willy Tarreaue68bc612022-05-27 11:23:05 +0200244 struct stconn *sc = *scp;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200245
Willy Tarreau0adb2812022-05-27 10:02:48 +0200246 if (!sc->app && (!sc->sedesc || sc_ep_test(sc, SE_FL_DETACHED))) {
247 sc_free(sc);
Willy Tarreaue68bc612022-05-27 11:23:05 +0200248 *scp = NULL;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200249 }
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200250}
251
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100252
Willy Tarreau4596fe22022-05-17 19:07:51 +0200253/* Attaches a stconn to a mux endpoint and sets the endpoint ctx. Returns
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500254 * -1 on error and 0 on success. SE_FL_DETACHED flag is removed. This function is
Christopher Faulet9ed77422022-04-12 08:51:15 +0200255 * called from a mux when it is attached to a stream or a health-check.
256 */
Willy Tarreau31219282022-05-27 16:21:33 +0200257int sc_attach_mux(struct stconn *sc, void *sd, void *ctx)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100258{
Christopher Faulet93882042022-01-19 14:56:50 +0100259 struct connection *conn = ctx;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200260 struct sedesc *sedesc = sc->sedesc;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100261
Willy Tarreau0adb2812022-05-27 10:02:48 +0200262 if (sc_strm(sc)) {
263 if (!sc->wait_event.tasklet) {
264 sc->wait_event.tasklet = tasklet_new();
265 if (!sc->wait_event.tasklet)
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200266 return -1;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200267 sc->wait_event.tasklet->process = sc_conn_io_cb;
268 sc->wait_event.tasklet->context = sc;
269 sc->wait_event.events = 0;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200270 }
271
Willy Tarreau0adb2812022-05-27 10:02:48 +0200272 sc->app_ops = &sc_app_conn_ops;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100273 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200274 else if (sc_check(sc)) {
275 if (!sc->wait_event.tasklet) {
276 sc->wait_event.tasklet = tasklet_new();
277 if (!sc->wait_event.tasklet)
Christopher Fauletc95eaef2022-05-18 15:57:15 +0200278 return -1;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200279 sc->wait_event.tasklet->process = srv_chk_io_cb;
280 sc->wait_event.tasklet->context = sc;
281 sc->wait_event.events = 0;
Christopher Fauletc95eaef2022-05-18 15:57:15 +0200282 }
283
Willy Tarreau0adb2812022-05-27 10:02:48 +0200284 sc->app_ops = &sc_app_check_ops;
Christopher Fauletc95eaef2022-05-18 15:57:15 +0200285 }
Willy Tarreaue2f79462023-03-20 19:45:41 +0100286
287 sedesc->se = sd;
288 sedesc->conn = ctx;
289 se_fl_set(sedesc, SE_FL_T_MUX);
290 se_fl_clr(sedesc, SE_FL_DETACHED);
291 if (!conn->ctx)
292 conn->ctx = sc;
Christopher Faulet070b91b2022-03-31 19:27:18 +0200293 return 0;
Christopher Faulet93882042022-01-19 14:56:50 +0100294}
295
Willy Tarreau4596fe22022-05-17 19:07:51 +0200296/* Attaches a stconn to an applet endpoint and sets the endpoint
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500297 * ctx. Returns -1 on error and 0 on success. SE_FL_DETACHED flag is
Christopher Faulet9ed77422022-04-12 08:51:15 +0200298 * removed. This function is called by a stream when a backend applet is
299 * registered.
300 */
Willy Tarreau31219282022-05-27 16:21:33 +0200301static void sc_attach_applet(struct stconn *sc, void *sd)
Christopher Faulet93882042022-01-19 14:56:50 +0100302{
Willy Tarreau31219282022-05-27 16:21:33 +0200303 sc->sedesc->se = sd;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200304 sc_ep_set(sc, SE_FL_T_APPLET);
305 sc_ep_clr(sc, SE_FL_DETACHED);
306 if (sc_strm(sc))
307 sc->app_ops = &sc_app_applet_ops;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100308}
309
Willy Tarreau4596fe22022-05-17 19:07:51 +0200310/* Attaches a stconn to a app layer and sets the relevant
Willy Tarreaub605c422022-05-17 17:04:55 +0200311 * callbacks. Returns -1 on error and 0 on success. SE_FL_ORPHAN flag is
Christopher Faulet9ed77422022-04-12 08:51:15 +0200312 * removed. This function is called by a stream when it is created to attach it
Willy Tarreau4596fe22022-05-17 19:07:51 +0200313 * on the stream connector on the client side.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200314 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200315int sc_attach_strm(struct stconn *sc, struct stream *strm)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100316{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200317 sc->app = &strm->obj_type;
318 sc_ep_clr(sc, SE_FL_ORPHAN);
319 if (sc_ep_test(sc, SE_FL_T_MUX)) {
320 sc->wait_event.tasklet = tasklet_new();
321 if (!sc->wait_event.tasklet)
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200322 return -1;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200323 sc->wait_event.tasklet->process = sc_conn_io_cb;
324 sc->wait_event.tasklet->context = sc;
325 sc->wait_event.events = 0;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200326
Willy Tarreau0adb2812022-05-27 10:02:48 +0200327 sc->app_ops = &sc_app_conn_ops;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100328 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200329 else if (sc_ep_test(sc, SE_FL_T_APPLET)) {
330 sc->app_ops = &sc_app_applet_ops;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100331 }
332 else {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200333 sc->app_ops = &sc_app_embedded_ops;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100334 }
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100335 return 0;
336}
337
Willy Tarreau4596fe22022-05-17 19:07:51 +0200338/* Detaches the stconn from the endpoint, if any. For a connecrion, if a
Christopher Faulet9ed77422022-04-12 08:51:15 +0200339 * mux owns the connection ->detach() callback is called. Otherwise, it means
Willy Tarreau4596fe22022-05-17 19:07:51 +0200340 * the stream connector owns the connection. In this case the connection is closed
Christopher Faulet9ed77422022-04-12 08:51:15 +0200341 * and released. For an applet, the appctx is released. If still allocated, the
342 * endpoint is reset and flag as detached. If the app layer is also detached,
Willy Tarreau4596fe22022-05-17 19:07:51 +0200343 * the stream connector is released.
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100344 */
Willy Tarreaue68bc612022-05-27 11:23:05 +0200345static void sc_detach_endp(struct stconn **scp)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100346{
Willy Tarreaue68bc612022-05-27 11:23:05 +0200347 struct stconn *sc = *scp;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200348
Willy Tarreau0adb2812022-05-27 10:02:48 +0200349 if (!sc)
Christopher Fauleteb50c012022-04-21 14:22:53 +0200350 return;
351
Willy Tarreau0adb2812022-05-27 10:02:48 +0200352 if (sc_ep_test(sc, SE_FL_T_MUX)) {
353 struct connection *conn = __sc_conn(sc);
354 struct sedesc *sedesc = sc->sedesc;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100355
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100356 if (conn->mux) {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200357 if (sc->wait_event.events != 0)
358 conn->mux->unsubscribe(sc, sc->wait_event.events, &sc->wait_event);
Willy Tarreau798465b2022-05-17 18:20:02 +0200359 se_fl_set(sedesc, SE_FL_ORPHAN);
Willy Tarreauc1054922022-05-18 07:43:52 +0200360 sedesc->sc = NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200361 sc->sedesc = NULL;
Willy Tarreau798465b2022-05-17 18:20:02 +0200362 conn->mux->detach(sedesc);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100363 }
364 else {
365 /* It's too early to have a mux, let's just destroy
366 * the connection
367 */
368 conn_stop_tracking(conn);
369 conn_full_close(conn);
370 if (conn->destroy_cb)
371 conn->destroy_cb(conn);
372 conn_free(conn);
373 }
374 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200375 else if (sc_ep_test(sc, SE_FL_T_APPLET)) {
376 struct appctx *appctx = __sc_appctx(sc);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100377
Willy Tarreau0adb2812022-05-27 10:02:48 +0200378 sc_ep_set(sc, SE_FL_ORPHAN);
379 sc->sedesc->sc = NULL;
380 sc->sedesc = NULL;
Willy Tarreau1c3ead42022-05-10 19:42:22 +0200381 appctx_shut(appctx);
382 appctx_free(appctx);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100383 }
384
Willy Tarreau0adb2812022-05-27 10:02:48 +0200385 if (sc->sedesc) {
Willy Tarreauda59c892022-05-27 17:03:34 +0200386 /* the SD wasn't used and can be recycled */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200387 sc->sedesc->se = NULL;
388 sc->sedesc->conn = NULL;
Willy Tarreauda59c892022-05-27 17:03:34 +0200389 sc->sedesc->flags = 0;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200390 sc_ep_set(sc, SE_FL_DETACHED);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100391 }
392
Willy Tarreaue68bc612022-05-27 11:23:05 +0200393 /* FIXME: Rest SC for now but must be reviewed. SC flags are only
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100394 * connection related for now but this will evolved
395 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200396 sc->flags &= SC_FL_ISBACK;
397 if (sc_strm(sc))
398 sc->app_ops = &sc_app_embedded_ops;
Willy Tarreau2f2318d2022-05-18 10:17:16 +0200399 else
Willy Tarreau0adb2812022-05-27 10:02:48 +0200400 sc->app_ops = NULL;
Willy Tarreaue68bc612022-05-27 11:23:05 +0200401 sc_free_cond(scp);
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100402}
403
Willy Tarreau4596fe22022-05-17 19:07:51 +0200404/* Detaches the stconn from the app layer. If there is no endpoint attached
405 * to the stconn
Christopher Faulet9ed77422022-04-12 08:51:15 +0200406 */
Willy Tarreaue68bc612022-05-27 11:23:05 +0200407static void sc_detach_app(struct stconn **scp)
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100408{
Willy Tarreaue68bc612022-05-27 11:23:05 +0200409 struct stconn *sc = *scp;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200410
Willy Tarreau0adb2812022-05-27 10:02:48 +0200411 if (!sc)
Christopher Fauleteb50c012022-04-21 14:22:53 +0200412 return;
413
Willy Tarreau0adb2812022-05-27 10:02:48 +0200414 sc->app = NULL;
415 sc->app_ops = NULL;
416 sockaddr_free(&sc->src);
417 sockaddr_free(&sc->dst);
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200418
Tim Duesterhusb1ec21d2023-04-22 17:47:32 +0200419 tasklet_free(sc->wait_event.tasklet);
Willy Tarreau0adb2812022-05-27 10:02:48 +0200420 sc->wait_event.tasklet = NULL;
421 sc->wait_event.events = 0;
Willy Tarreaue68bc612022-05-27 11:23:05 +0200422 sc_free_cond(scp);
Christopher Fauleteb50c012022-04-21 14:22:53 +0200423}
424
Willy Tarreau4596fe22022-05-17 19:07:51 +0200425/* Destroy the stconn. It is detached from its endpoint and its
426 * application. After this call, the stconn must be considered as released.
Christopher Fauleteb50c012022-04-21 14:22:53 +0200427 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200428void sc_destroy(struct stconn *sc)
Christopher Fauleteb50c012022-04-21 14:22:53 +0200429{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200430 sc_detach_endp(&sc);
431 sc_detach_app(&sc);
432 BUG_ON_HOT(sc);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100433}
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100434
Willy Tarreau4596fe22022-05-17 19:07:51 +0200435/* Resets the stream connector endpoint. It happens when the app layer want to renew
Christopher Faulet9ed77422022-04-12 08:51:15 +0200436 * its endpoint. For a connection retry for instance. If a mux or an applet is
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500437 * attached, a new endpoint is created. Returns -1 on error and 0 on success.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200438 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200439int sc_reset_endp(struct stconn *sc)
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100440{
Willy Tarreau31219282022-05-27 16:21:33 +0200441 struct sedesc *new_sd;
Christopher Fauletb041b232022-03-24 10:27:02 +0100442
Willy Tarreau0adb2812022-05-27 10:02:48 +0200443 BUG_ON(!sc->app);
Christopher Fauleta6c4a482022-04-28 18:25:24 +0200444
Willy Tarreau0adb2812022-05-27 10:02:48 +0200445 if (!__sc_endp(sc)) {
Christopher Fauletb041b232022-03-24 10:27:02 +0100446 /* endpoint not attached or attached to a mux with no
447 * target. Thus the endpoint will not be release but just
Willy Tarreau0adb2812022-05-27 10:02:48 +0200448 * reset. The app is still attached, the sc will not be
Christopher Fauleteb50c012022-04-21 14:22:53 +0200449 * released.
Christopher Fauletb041b232022-03-24 10:27:02 +0100450 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200451 sc_detach_endp(&sc);
Christopher Fauletb041b232022-03-24 10:27:02 +0100452 return 0;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100453 }
Christopher Fauletb041b232022-03-24 10:27:02 +0100454
455 /* allocate the new endpoint first to be able to set error if it
456 * fails */
Willy Tarreau31219282022-05-27 16:21:33 +0200457 new_sd = sedesc_new();
Christopher Faulet638fe6a2023-04-14 10:28:28 +0200458 if (!unlikely(new_sd))
Christopher Fauletb041b232022-03-24 10:27:02 +0100459 return -1;
Christopher Fauletb041b232022-03-24 10:27:02 +0100460
Willy Tarreau0adb2812022-05-27 10:02:48 +0200461 /* The app is still attached, the sc will not be released */
462 sc_detach_endp(&sc);
Willy Tarreau6a378d12022-08-11 13:56:42 +0200463 BUG_ON(!sc);
Willy Tarreau0adb2812022-05-27 10:02:48 +0200464 BUG_ON(sc->sedesc);
Willy Tarreau31219282022-05-27 16:21:33 +0200465 sc->sedesc = new_sd;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200466 sc->sedesc->sc = sc;
467 sc_ep_set(sc, SE_FL_DETACHED);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100468 return 0;
469}
Christopher Faulet37046632022-04-01 11:36:58 +0200470
471
Willy Tarreaue68bc612022-05-27 11:23:05 +0200472/* Create an applet to handle a stream connector as a new appctx. The SC will
Christopher Faulet37046632022-04-01 11:36:58 +0200473 * wake it up every time it is solicited. The appctx must be deleted by the task
Willy Tarreau19c65a92022-05-27 08:49:24 +0200474 * handler using sc_detach_endp(), possibly from within the function itself.
Christopher Faulet37046632022-04-01 11:36:58 +0200475 * It also pre-initializes the applet's context and returns it (or NULL in case
476 * it could not be allocated).
477 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200478struct appctx *sc_applet_create(struct stconn *sc, struct applet *app)
Christopher Faulet37046632022-04-01 11:36:58 +0200479{
480 struct appctx *appctx;
481
Willy Tarreau0adb2812022-05-27 10:02:48 +0200482 appctx = appctx_new_here(app, sc->sedesc);
Christopher Faulet37046632022-04-01 11:36:58 +0200483 if (!appctx)
484 return NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200485 sc_attach_applet(sc, appctx);
486 appctx->t->nice = __sc_strm(sc)->task->nice;
Willy Tarreau90e8b452022-05-25 18:21:43 +0200487 applet_need_more_data(appctx);
Christopher Faulet37046632022-04-01 11:36:58 +0200488 appctx_wakeup(appctx);
Christopher Fauleta33ff7a2022-04-21 11:52:07 +0200489
Willy Tarreau0adb2812022-05-27 10:02:48 +0200490 sc->state = SC_ST_RDY;
Christopher Faulet37046632022-04-01 11:36:58 +0200491 return appctx;
492}
493
Ilya Shipitsin07be66d2023-04-01 12:26:42 +0200494/* Conditionally forward the close to the write side. It return 1 if it can be
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100495 * forwarded. It is the caller responsibility to forward the close to the write
Christopher Faulete38534c2023-04-13 15:45:24 +0200496 * side. Otherwise, 0 is returned. In this case, SC_FL_SHUT_WANTED flag may be set on
Christopher Faulet87633c32023-04-03 18:32:50 +0200497 * the consumer SC if we are only waiting for the outgoing data to be flushed.
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100498 */
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +0200499static inline int sc_cond_forward_shut(struct stconn *sc)
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100500{
501 /* The close must not be forwarded */
Christopher Fauletca5309a2023-04-17 16:17:32 +0200502 if (!(sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)) || !(sc->flags & SC_FL_NOHALF))
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100503 return 0;
504
505 if (!channel_is_empty(sc_ic(sc))) {
Christopher Fauletdf7cd712023-04-13 15:56:26 +0200506 /* the shutdown cannot be forwarded now because
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100507 * we should flush outgoing data first. But instruct the output
508 * channel it should be done ASAP.
509 */
Christopher Fauletdf7cd712023-04-13 15:56:26 +0200510 sc_schedule_shutdown(sc);
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100511 return 0;
512 }
513
514 /* the close can be immediately forwarded to the write side */
515 return 1;
516}
517
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200518/*
Willy Tarreau4596fe22022-05-17 19:07:51 +0200519 * This function performs a shutdown-read on a detached stream connector in a
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200520 * connected or init state (it does nothing for other states). It either shuts
521 * the read side or marks itself as closed. The buffer flags are updated to
Willy Tarreaucb041662022-05-17 19:44:42 +0200522 * reflect the new state. If the stream connector has SC_FL_NOHALF, we also
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200523 * forward the close to the write side. The owner task is woken up if it exists.
524 */
Christopher Fauletcfc11c02023-04-13 16:10:23 +0200525static void sc_app_abort(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200526{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200527 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200528
Christopher Fauletca5309a2023-04-17 16:17:32 +0200529 if (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))
Christopher Fauletc665bb52023-04-04 10:06:57 +0200530 return;
Christopher Faulet87633c32023-04-03 18:32:50 +0200531
Christopher Faulet0c370ee2023-04-13 16:05:13 +0200532 sc->flags |= SC_FL_ABRT_DONE;
Christopher Faulet87633c32023-04-03 18:32:50 +0200533 ic->flags |= CF_READ_EVENT;
Christopher Faulet4c135682023-02-16 11:09:31 +0100534 sc_ep_report_read_activity(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200535
Willy Tarreau0adb2812022-05-27 10:02:48 +0200536 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200537 return;
538
Christopher Faulet208c7122023-04-13 16:16:15 +0200539 if (sc->flags & SC_FL_SHUT_DONE) {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200540 sc->state = SC_ST_DIS;
Christopher Fauletca679922022-07-20 13:24:04 +0200541 if (sc->flags & SC_FL_ISBACK)
542 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200543 }
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +0200544 else if (sc_cond_forward_shut(sc))
545 return sc_app_shut(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200546
547 /* note that if the task exists, it must unregister itself once it runs */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200548 if (!(sc->flags & SC_FL_DONT_WAKE))
549 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200550}
551
552/*
Willy Tarreau4596fe22022-05-17 19:07:51 +0200553 * This function performs a shutdown-write on a detached stream connector in a
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200554 * connected or init state (it does nothing for other states). It either shuts
555 * the write side or marks itself as closed. The buffer flags are updated to
Willy Tarreaue68bc612022-05-27 11:23:05 +0200556 * reflect the new state. It does also close everything if the SC was marked as
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200557 * being in error state. The owner task is woken up if it exists.
558 */
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +0200559static void sc_app_shut(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200560{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200561 struct channel *ic = sc_ic(sc);
562 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200563
Christopher Faulete38534c2023-04-13 15:45:24 +0200564 sc->flags &= ~SC_FL_SHUT_WANTED;
Christopher Faulet208c7122023-04-13 16:16:15 +0200565 if (sc->flags & SC_FL_SHUT_DONE)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200566 return;
Christopher Faulet208c7122023-04-13 16:16:15 +0200567 sc->flags |= SC_FL_SHUT_DONE;
Christopher Faulet87633c32023-04-03 18:32:50 +0200568 oc->flags |= CF_WRITE_EVENT;
Christopher Fauletbcdcfad2023-02-20 08:36:53 +0100569 sc_set_hcto(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200570
Willy Tarreau0adb2812022-05-27 10:02:48 +0200571 switch (sc->state) {
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200572 case SC_ST_RDY:
573 case SC_ST_EST:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200574 /* we have to shut before closing, otherwise some short messages
575 * may never leave the system, especially when there are remaining
576 * unread data in the socket input buffer, or when nolinger is set.
Willy Tarreaucb041662022-05-17 19:44:42 +0200577 * However, if SC_FL_NOLINGER is explicitly set, we know there is
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200578 * no risk so we close both sides immediately.
579 */
Christopher Fauletca5309a2023-04-17 16:17:32 +0200580 if (!(sc->flags & (SC_FL_ERROR|SC_FL_NOLINGER|SC_FL_EOS|SC_FL_ABRT_DONE)) &&
Christopher Fauletad46e522023-04-14 11:59:15 +0200581 !(ic->flags & CF_DONT_READ))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200582 return;
583
Willy Tarreau476c2802022-11-14 07:36:42 +0100584 __fallthrough;
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200585 case SC_ST_CON:
586 case SC_ST_CER:
587 case SC_ST_QUE:
588 case SC_ST_TAR:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200589 /* Note that none of these states may happen with applets */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200590 sc->state = SC_ST_DIS;
Willy Tarreau476c2802022-11-14 07:36:42 +0100591 __fallthrough;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200592 default:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200593 sc->flags &= ~SC_FL_NOLINGER;
Christopher Faulet0c370ee2023-04-13 16:05:13 +0200594 sc->flags |= SC_FL_ABRT_DONE;
Christopher Fauletca679922022-07-20 13:24:04 +0200595 if (sc->flags & SC_FL_ISBACK)
596 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200597 }
598
599 /* note that if the task exists, it must unregister itself once it runs */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200600 if (!(sc->flags & SC_FL_DONT_WAKE))
601 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200602}
603
604/* default chk_rcv function for scheduled tasks */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200605static void sc_app_chk_rcv(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200606{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200607 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200608
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200609 if (ic->pipe) {
610 /* stop reading */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200611 sc_need_room(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200612 }
613 else {
614 /* (re)start reading */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200615 if (!(sc->flags & SC_FL_DONT_WAKE))
616 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200617 }
618}
619
620/* default chk_snd function for scheduled tasks */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200621static void sc_app_chk_snd(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200622{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200623 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200624
Christopher Faulet208c7122023-04-13 16:16:15 +0200625 if (unlikely(sc->state != SC_ST_EST || (sc->flags & SC_FL_SHUT_DONE)))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200626 return;
627
Willy Tarreau0adb2812022-05-27 10:02:48 +0200628 if (!sc_ep_test(sc, SE_FL_WAIT_DATA) || /* not waiting for data */
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200629 channel_is_empty(oc)) /* called with nothing to send ! */
630 return;
631
632 /* Otherwise there are remaining data to be sent in the buffer,
633 * so we tell the handler.
634 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200635 sc_ep_clr(sc, SE_FL_WAIT_DATA);
Willy Tarreau0adb2812022-05-27 10:02:48 +0200636 if (!(sc->flags & SC_FL_DONT_WAKE))
637 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200638}
639
640/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200641 * This function performs a shutdown-read on a stream connector attached to
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200642 * a connection in a connected or init state (it does nothing for other
643 * states). It either shuts the read side or marks itself as closed. The buffer
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200644 * flags are updated to reflect the new state. If the stream connector has
Willy Tarreaucb041662022-05-17 19:44:42 +0200645 * SC_FL_NOHALF, we also forward the close to the write side. If a control
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200646 * layer is defined, then it is supposed to be a socket layer and file
647 * descriptors are then shutdown or closed accordingly. The function
648 * automatically disables polling if needed.
649 */
Christopher Fauletcfc11c02023-04-13 16:10:23 +0200650static void sc_app_abort_conn(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200651{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200652 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200653
Willy Tarreau0adb2812022-05-27 10:02:48 +0200654 BUG_ON(!sc_conn(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200655
Christopher Fauletca5309a2023-04-17 16:17:32 +0200656 if (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200657 return;
Christopher Faulet0c370ee2023-04-13 16:05:13 +0200658 sc->flags |= SC_FL_ABRT_DONE;
Christopher Faulet87633c32023-04-03 18:32:50 +0200659 ic->flags |= CF_READ_EVENT;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200660
Willy Tarreau0adb2812022-05-27 10:02:48 +0200661 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200662 return;
663
Christopher Faulet208c7122023-04-13 16:16:15 +0200664 if (sc->flags & SC_FL_SHUT_DONE) {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200665 sc_conn_shut(sc);
666 sc->state = SC_ST_DIS;
Christopher Fauletca679922022-07-20 13:24:04 +0200667 if (sc->flags & SC_FL_ISBACK)
668 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200669 }
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +0200670 else if (sc_cond_forward_shut(sc))
671 return sc_app_shut_conn(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200672}
673
674/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200675 * This function performs a shutdown-write on a stream connector attached to
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200676 * a connection in a connected or init state (it does nothing for other
677 * states). It either shuts the write side or marks itself as closed. The
678 * buffer flags are updated to reflect the new state. It does also close
Willy Tarreaue68bc612022-05-27 11:23:05 +0200679 * everything if the SC was marked as being in error state. If there is a
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200680 * data-layer shutdown, it is called.
681 */
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +0200682static void sc_app_shut_conn(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200683{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200684 struct channel *ic = sc_ic(sc);
685 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200686
Willy Tarreau0adb2812022-05-27 10:02:48 +0200687 BUG_ON(!sc_conn(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200688
Christopher Faulete38534c2023-04-13 15:45:24 +0200689 sc->flags &= ~SC_FL_SHUT_WANTED;
Christopher Faulet208c7122023-04-13 16:16:15 +0200690 if (sc->flags & SC_FL_SHUT_DONE)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200691 return;
Christopher Faulet208c7122023-04-13 16:16:15 +0200692 sc->flags |= SC_FL_SHUT_DONE;
Christopher Faulet87633c32023-04-03 18:32:50 +0200693 oc->flags |= CF_WRITE_EVENT;
Christopher Fauletbcdcfad2023-02-20 08:36:53 +0100694 sc_set_hcto(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200695
Willy Tarreau0adb2812022-05-27 10:02:48 +0200696 switch (sc->state) {
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200697 case SC_ST_RDY:
698 case SC_ST_EST:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200699 /* we have to shut before closing, otherwise some short messages
700 * may never leave the system, especially when there are remaining
701 * unread data in the socket input buffer, or when nolinger is set.
Willy Tarreaucb041662022-05-17 19:44:42 +0200702 * However, if SC_FL_NOLINGER is explicitly set, we know there is
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200703 * no risk so we close both sides immediately.
704 */
705
Christopher Faulet25d9fe52023-04-14 12:09:35 +0200706 if (sc->flags & SC_FL_ERROR) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200707 /* quick close, the socket is already shut anyway */
708 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200709 else if (sc->flags & SC_FL_NOLINGER) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200710 /* unclean data-layer shutdown, typically an aborted request
711 * or a forwarded shutdown from a client to a server due to
712 * option abortonclose. No need for the TLS layer to try to
713 * emit a shutdown message.
714 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200715 sc_conn_shutw(sc, CO_SHW_SILENT);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200716 }
717 else {
718 /* clean data-layer shutdown. This only happens on the
719 * frontend side, or on the backend side when forwarding
720 * a client close in TCP mode or in HTTP TUNNEL mode
721 * while option abortonclose is set. We want the TLS
722 * layer to try to signal it to the peer before we close.
723 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200724 sc_conn_shutw(sc, CO_SHW_NORMAL);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200725
Christopher Fauletca5309a2023-04-17 16:17:32 +0200726 if (!(sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)) && !(ic->flags & CF_DONT_READ))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200727 return;
728 }
729
Willy Tarreau476c2802022-11-14 07:36:42 +0100730 __fallthrough;
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200731 case SC_ST_CON:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200732 /* we may have to close a pending connection, and mark the
Christopher Fauletcfc11c02023-04-13 16:10:23 +0200733 * response buffer as abort
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200734 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200735 sc_conn_shut(sc);
Willy Tarreau476c2802022-11-14 07:36:42 +0100736 __fallthrough;
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200737 case SC_ST_CER:
738 case SC_ST_QUE:
739 case SC_ST_TAR:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200740 sc->state = SC_ST_DIS;
Willy Tarreau476c2802022-11-14 07:36:42 +0100741 __fallthrough;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200742 default:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200743 sc->flags &= ~SC_FL_NOLINGER;
Christopher Faulet0c370ee2023-04-13 16:05:13 +0200744 sc->flags |= SC_FL_ABRT_DONE;
Christopher Fauletca679922022-07-20 13:24:04 +0200745 if (sc->flags & SC_FL_ISBACK)
746 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200747 }
748}
749
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200750/* This function is used for inter-stream connector calls. It is called by the
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200751 * consumer to inform the producer side that it may be interested in checking
752 * for free space in the buffer. Note that it intentionally does not update
753 * timeouts, so that we can still check them later at wake-up. This function is
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200754 * dedicated to connection-based stream connectors.
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200755 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200756static void sc_app_chk_rcv_conn(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200757{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200758 BUG_ON(!sc_conn(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200759
760 /* (re)start reading */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200761 if (sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
762 tasklet_wakeup(sc->wait_event.tasklet);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200763}
764
765
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200766/* This function is used for inter-stream connector calls. It is called by the
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200767 * producer to inform the consumer side that it may be interested in checking
768 * for data in the buffer. Note that it intentionally does not update timeouts,
769 * so that we can still check them later at wake-up.
770 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200771static void sc_app_chk_snd_conn(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200772{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200773 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200774
Willy Tarreau0adb2812022-05-27 10:02:48 +0200775 BUG_ON(!sc_conn(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200776
Willy Tarreau0adb2812022-05-27 10:02:48 +0200777 if (unlikely(!sc_state_in(sc->state, SC_SB_RDY|SC_SB_EST) ||
Christopher Faulet208c7122023-04-13 16:16:15 +0200778 (sc->flags & SC_FL_SHUT_DONE)))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200779 return;
780
781 if (unlikely(channel_is_empty(oc))) /* called with nothing to send ! */
782 return;
783
784 if (!oc->pipe && /* spliced data wants to be forwarded ASAP */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200785 !sc_ep_test(sc, SE_FL_WAIT_DATA)) /* not waiting for data */
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200786 return;
787
Willy Tarreau0adb2812022-05-27 10:02:48 +0200788 if (!(sc->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(sc_oc(sc)))
789 sc_conn_send(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200790
Willy Tarreau0adb2812022-05-27 10:02:48 +0200791 if (sc_ep_test(sc, SE_FL_ERROR | SE_FL_ERR_PENDING) || sc_is_conn_error(sc)) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200792 /* Write error on the file descriptor */
Christopher Faulet56a2b602023-04-14 09:42:59 +0200793 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 +0200794 goto out_wakeup;
795 }
796
797 /* OK, so now we know that some data might have been sent, and that we may
798 * have to poll first. We have to do that too if the buffer is not empty.
799 */
800 if (channel_is_empty(oc)) {
801 /* the connection is established but we can't write. Either the
802 * buffer is empty, or we just refrain from sending because the
803 * ->o limit was reached. Maybe we just wrote the last
804 * chunk and need to close.
805 */
Christopher Faulet87633c32023-04-03 18:32:50 +0200806 if ((oc->flags & CF_AUTO_CLOSE) &&
Christopher Faulet208c7122023-04-13 16:16:15 +0200807 ((sc->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED)) == SC_FL_SHUT_WANTED) &&
Willy Tarreau0adb2812022-05-27 10:02:48 +0200808 sc_state_in(sc->state, SC_SB_RDY|SC_SB_EST)) {
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +0200809 sc_shutdown(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200810 goto out_wakeup;
811 }
812
Christopher Faulet208c7122023-04-13 16:16:15 +0200813 if ((sc->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED)) == 0)
Willy Tarreau0adb2812022-05-27 10:02:48 +0200814 sc_ep_set(sc, SE_FL_WAIT_DATA);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200815 }
816 else {
817 /* Otherwise there are remaining data to be sent in the buffer,
818 * which means we have to poll before doing so.
819 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200820 sc_ep_clr(sc, SE_FL_WAIT_DATA);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200821 }
822
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200823 /* in case of special condition (error, shutdown, end of write...), we
824 * have to notify the task.
825 */
Christopher Faulet208c7122023-04-13 16:16:15 +0200826 if (likely((sc->flags & SC_FL_SHUT_DONE) ||
Christopher Faulet71c486b2023-02-09 14:14:38 +0100827 ((oc->flags & CF_WRITE_EVENT) && sc->state < SC_ST_EST) ||
828 ((oc->flags & CF_WAKE_WRITE) &&
829 ((channel_is_empty(oc) && !oc->to_forward) ||
830 !sc_state_in(sc->state, SC_SB_EST))))) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200831 out_wakeup:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200832 if (!(sc->flags & SC_FL_DONT_WAKE))
833 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200834 }
835}
836
837/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200838 * This function performs a shutdown-read on a stream connector attached to an
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200839 * applet in a connected or init state (it does nothing for other states). It
840 * either shuts the read side or marks itself as closed. The buffer flags are
Willy Tarreaucb041662022-05-17 19:44:42 +0200841 * updated to reflect the new state. If the stream connector has SC_FL_NOHALF,
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200842 * we also forward the close to the write side. The owner task is woken up if
843 * it exists.
844 */
Christopher Fauletcfc11c02023-04-13 16:10:23 +0200845static void sc_app_abort_applet(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200846{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200847 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200848
Willy Tarreau0adb2812022-05-27 10:02:48 +0200849 BUG_ON(!sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200850
Christopher Fauletca5309a2023-04-17 16:17:32 +0200851 if (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200852 return;
Christopher Faulet0c370ee2023-04-13 16:05:13 +0200853 sc->flags |= SC_FL_ABRT_DONE;
Christopher Faulet87633c32023-04-03 18:32:50 +0200854 ic->flags |= CF_READ_EVENT;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200855
Christopher Fauletcfc11c02023-04-13 16:10:23 +0200856 /* Note: on abort, we don't call the applet */
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200857
Willy Tarreau0adb2812022-05-27 10:02:48 +0200858 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200859 return;
860
Christopher Faulet208c7122023-04-13 16:16:15 +0200861 if (sc->flags & SC_FL_SHUT_DONE) {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200862 appctx_shut(__sc_appctx(sc));
863 sc->state = SC_ST_DIS;
Christopher Fauletca679922022-07-20 13:24:04 +0200864 if (sc->flags & SC_FL_ISBACK)
865 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200866 }
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +0200867 else if (sc_cond_forward_shut(sc))
868 return sc_app_shut_applet(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200869}
870
871/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200872 * This function performs a shutdown-write on a stream connector attached to an
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200873 * applet in a connected or init state (it does nothing for other states). It
874 * either shuts the write side or marks itself as closed. The buffer flags are
875 * updated to reflect the new state. It does also close everything if the SI
876 * was marked as being in error state. The owner task is woken up if it exists.
877 */
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +0200878static void sc_app_shut_applet(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200879{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200880 struct channel *ic = sc_ic(sc);
881 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200882
Willy Tarreau0adb2812022-05-27 10:02:48 +0200883 BUG_ON(!sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200884
Christopher Faulete38534c2023-04-13 15:45:24 +0200885 sc->flags &= ~SC_FL_SHUT_WANTED;
Christopher Faulet208c7122023-04-13 16:16:15 +0200886 if (sc->flags & SC_FL_SHUT_DONE)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200887 return;
Christopher Faulet208c7122023-04-13 16:16:15 +0200888 sc->flags |= SC_FL_SHUT_DONE;
Christopher Faulet87633c32023-04-03 18:32:50 +0200889 oc->flags |= CF_WRITE_EVENT;
Christopher Fauletbcdcfad2023-02-20 08:36:53 +0100890 sc_set_hcto(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200891
892 /* on shutw we always wake the applet up */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200893 appctx_wakeup(__sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200894
Willy Tarreau0adb2812022-05-27 10:02:48 +0200895 switch (sc->state) {
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200896 case SC_ST_RDY:
897 case SC_ST_EST:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200898 /* we have to shut before closing, otherwise some short messages
899 * may never leave the system, especially when there are remaining
900 * unread data in the socket input buffer, or when nolinger is set.
Willy Tarreaucb041662022-05-17 19:44:42 +0200901 * However, if SC_FL_NOLINGER is explicitly set, we know there is
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200902 * no risk so we close both sides immediately.
903 */
Christopher Fauletca5309a2023-04-17 16:17:32 +0200904 if (!(sc->flags & (SC_FL_ERROR|SC_FL_NOLINGER|SC_FL_EOS|SC_FL_ABRT_DONE)) &&
Christopher Faulet87633c32023-04-03 18:32:50 +0200905 !(ic->flags & CF_DONT_READ))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200906 return;
907
Willy Tarreau476c2802022-11-14 07:36:42 +0100908 __fallthrough;
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200909 case SC_ST_CON:
910 case SC_ST_CER:
911 case SC_ST_QUE:
912 case SC_ST_TAR:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200913 /* Note that none of these states may happen with applets */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200914 appctx_shut(__sc_appctx(sc));
915 sc->state = SC_ST_DIS;
Willy Tarreau476c2802022-11-14 07:36:42 +0100916 __fallthrough;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200917 default:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200918 sc->flags &= ~SC_FL_NOLINGER;
Christopher Faulet0c370ee2023-04-13 16:05:13 +0200919 sc->flags |= SC_FL_ABRT_DONE;
Christopher Fauletca679922022-07-20 13:24:04 +0200920 if (sc->flags & SC_FL_ISBACK)
921 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200922 }
923}
924
925/* chk_rcv function for applets */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200926static void sc_app_chk_rcv_applet(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200927{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200928 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200929
Willy Tarreau0adb2812022-05-27 10:02:48 +0200930 BUG_ON(!sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200931
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200932 if (!ic->pipe) {
933 /* (re)start reading */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200934 appctx_wakeup(__sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200935 }
936}
937
938/* chk_snd function for applets */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200939static void sc_app_chk_snd_applet(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200940{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200941 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200942
Willy Tarreau0adb2812022-05-27 10:02:48 +0200943 BUG_ON(!sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200944
Christopher Faulet208c7122023-04-13 16:16:15 +0200945 if (unlikely(sc->state != SC_ST_EST || (sc->flags & SC_FL_SHUT_DONE)))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200946 return;
947
Christopher Faulet04f03e12022-06-01 17:35:34 +0200948 /* we only wake the applet up if it was waiting for some data and is ready to consume it */
949 if (!sc_ep_test(sc, SE_FL_WAIT_DATA) || sc_ep_test(sc, SE_FL_WONT_CONSUME))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200950 return;
951
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200952 if (!channel_is_empty(oc)) {
953 /* (re)start sending */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200954 appctx_wakeup(__sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200955 }
956}
Christopher Faulet13045f02022-04-01 14:23:38 +0200957
958
959/* This function is designed to be called from within the stream handler to
Willy Tarreau4596fe22022-05-17 19:07:51 +0200960 * update the input channel's expiration timer and the stream connector's
Christopher Faulet13045f02022-04-01 14:23:38 +0200961 * Rx flags based on the channel's flags. It needs to be called only once
962 * after the channel's flags have settled down, and before they are cleared,
963 * though it doesn't harm to call it as often as desired (it just slightly
964 * hurts performance). It must not be called from outside of the stream
965 * handler, as what it does will be used to compute the stream task's
966 * expiration.
967 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200968void sc_update_rx(struct stconn *sc)
Christopher Faulet13045f02022-04-01 14:23:38 +0200969{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200970 struct channel *ic = sc_ic(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +0200971
Christopher Fauletca5309a2023-04-17 16:17:32 +0200972 if (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))
Christopher Faulet13045f02022-04-01 14:23:38 +0200973 return;
Christopher Faulet13045f02022-04-01 14:23:38 +0200974
975 /* Read not closed, update FD status and timeout for reads */
976 if (ic->flags & CF_DONT_READ)
Willy Tarreau0adb2812022-05-27 10:02:48 +0200977 sc_wont_read(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +0200978 else
Willy Tarreau0adb2812022-05-27 10:02:48 +0200979 sc_will_read(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +0200980
Willy Tarreau0adb2812022-05-27 10:02:48 +0200981 sc_chk_rcv(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +0200982}
983
984/* This function is designed to be called from within the stream handler to
Willy Tarreau4596fe22022-05-17 19:07:51 +0200985 * update the output channel's expiration timer and the stream connector's
Christopher Faulet13045f02022-04-01 14:23:38 +0200986 * Tx flags based on the channel's flags. It needs to be called only once
987 * after the channel's flags have settled down, and before they are cleared,
988 * though it doesn't harm to call it as often as desired (it just slightly
989 * hurts performance). It must not be called from outside of the stream
990 * handler, as what it does will be used to compute the stream task's
991 * expiration.
992 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200993void sc_update_tx(struct stconn *sc)
Christopher Faulet13045f02022-04-01 14:23:38 +0200994{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200995 struct channel *oc = sc_oc(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +0200996
Christopher Faulet208c7122023-04-13 16:16:15 +0200997 if (sc->flags & SC_FL_SHUT_DONE)
Christopher Faulet13045f02022-04-01 14:23:38 +0200998 return;
999
1000 /* Write not closed, update FD status and timeout for writes */
1001 if (channel_is_empty(oc)) {
1002 /* stop writing */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001003 if (!sc_ep_test(sc, SE_FL_WAIT_DATA)) {
Christopher Faulete38534c2023-04-13 15:45:24 +02001004 if ((sc->flags & SC_FL_SHUT_WANTED) == 0)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001005 sc_ep_set(sc, SE_FL_WAIT_DATA);
Christopher Faulet13045f02022-04-01 14:23:38 +02001006 }
1007 return;
1008 }
1009
Christopher Faulet15315d62023-02-20 08:23:51 +01001010 /* (re)start writing */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001011 sc_ep_clr(sc, SE_FL_WAIT_DATA);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001012}
1013
Willy Tarreau19c65a92022-05-27 08:49:24 +02001014/* This function is the equivalent to sc_update() except that it's
Christopher Faulet5e29b762022-04-04 08:58:34 +02001015 * designed to be called from outside the stream handlers, typically the lower
1016 * layers (applets, connections) after I/O completion. After updating the stream
1017 * interface and timeouts, it will try to forward what can be forwarded, then to
1018 * wake the associated task up if an important event requires special handling.
Willy Tarreau15252cd2022-05-25 16:36:21 +02001019 * It may update SE_FL_WAIT_DATA and/or SC_FL_NEED_ROOM, that the callers are
Christopher Faulet5e29b762022-04-04 08:58:34 +02001020 * encouraged to watch to take appropriate action.
Willy Tarreau19c65a92022-05-27 08:49:24 +02001021 * It should not be called from within the stream itself, sc_update()
Christopher Faulet5e29b762022-04-04 08:58:34 +02001022 * is designed for this.
1023 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001024static void sc_notify(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001025{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001026 struct channel *ic = sc_ic(sc);
1027 struct channel *oc = sc_oc(sc);
Willy Tarreaue68bc612022-05-27 11:23:05 +02001028 struct stconn *sco = sc_opposite(sc);
Willy Tarreau0adb2812022-05-27 10:02:48 +02001029 struct task *task = sc_strm_task(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001030
1031 /* process consumer side */
1032 if (channel_is_empty(oc)) {
Willy Tarreau0adb2812022-05-27 10:02:48 +02001033 struct connection *conn = sc_conn(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001034
Christopher Faulet208c7122023-04-13 16:16:15 +02001035 if (((sc->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED)) == SC_FL_SHUT_WANTED) &&
Willy Tarreau0adb2812022-05-27 10:02:48 +02001036 (sc->state == SC_ST_EST) && (!conn || !(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS))))
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +02001037 sc_shutdown(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001038 }
1039
1040 /* indicate that we may be waiting for data from the output channel or
Christopher Faulete38534c2023-04-13 15:45:24 +02001041 * 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 +02001042 */
Christopher Faulet208c7122023-04-13 16:16:15 +02001043 if (!(sc->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED)))
Willy Tarreau0adb2812022-05-27 10:02:48 +02001044 sc_ep_set(sc, SE_FL_WAIT_DATA);
Christopher Faulet208c7122023-04-13 16:16:15 +02001045 else if ((sc->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED)) == SC_FL_SHUT_WANTED)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001046 sc_ep_clr(sc, SE_FL_WAIT_DATA);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001047
Christopher Faulet5e29b762022-04-04 08:58:34 +02001048 if (oc->flags & CF_DONT_READ)
Willy Tarreaue68bc612022-05-27 11:23:05 +02001049 sc_wont_read(sco);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001050 else
Willy Tarreaue68bc612022-05-27 11:23:05 +02001051 sc_will_read(sco);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001052
1053 /* Notify the other side when we've injected data into the IC that
1054 * needs to be forwarded. We can do fast-forwarding as soon as there
1055 * are output data, but we avoid doing this if some of the data are
1056 * not yet scheduled for being forwarded, because it is very likely
1057 * that it will be done again immediately afterwards once the following
Willy Tarreau15252cd2022-05-25 16:36:21 +02001058 * data are parsed (eg: HTTP chunking). We only clear SC_FL_NEED_ROOM
1059 * once we've emptied *some* of the output buffer, and not just when
1060 * there is available room, because applets are often forced to stop
1061 * before the buffer is full. We must not stop based on input data
1062 * alone because an HTTP parser might need more data to complete the
1063 * parsing.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001064 */
1065 if (!channel_is_empty(ic) &&
Willy Tarreaue68bc612022-05-27 11:23:05 +02001066 sc_ep_test(sco, SE_FL_WAIT_DATA) &&
Christopher Faulet84d3ef92023-03-17 15:45:58 +01001067 (!(sc->flags & SC_FL_SND_EXP_MORE) || c_full(ic) || ci_data(ic) == 0 || ic->pipe)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001068 int new_len, last_len;
1069
1070 last_len = co_data(ic);
1071 if (ic->pipe)
1072 last_len += ic->pipe->data;
1073
Willy Tarreaue68bc612022-05-27 11:23:05 +02001074 sc_chk_snd(sco);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001075
1076 new_len = co_data(ic);
1077 if (ic->pipe)
1078 new_len += ic->pipe->data;
1079
1080 /* check if the consumer has freed some space either in the
1081 * buffer or in the pipe.
1082 */
1083 if (new_len < last_len)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001084 sc_have_room(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001085 }
1086
1087 if (!(ic->flags & CF_DONT_READ))
Willy Tarreau0adb2812022-05-27 10:02:48 +02001088 sc_will_read(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001089
Willy Tarreau0adb2812022-05-27 10:02:48 +02001090 sc_chk_rcv(sc);
Willy Tarreaue68bc612022-05-27 11:23:05 +02001091 sc_chk_rcv(sco);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001092
Christopher Faulet5e29b762022-04-04 08:58:34 +02001093 /* wake the task up only when needed */
Christopher Faulet285f7612022-12-12 08:28:55 +01001094 if (/* changes on the production side that must be handled:
Christopher Fauletad46e522023-04-14 11:59:15 +02001095 * - An error on receipt: SC_FL_ERROR
Christopher Fauletca5309a2023-04-17 16:17:32 +02001096 * - A read event: shutdown for reads (CF_READ_EVENT + EOS/ABRT_DONE)
Christopher Faulet904763f2023-03-22 14:53:11 +01001097 * end of input (CF_READ_EVENT + SC_FL_EOI)
Christopher Faulet285f7612022-12-12 08:28:55 +01001098 * data received and no fast-forwarding (CF_READ_EVENT + !to_forward)
1099 * read event while consumer side is not established (CF_READ_EVENT + sco->state != SC_ST_EST)
1100 */
Christopher Fauletca5309a2023-04-17 16:17:32 +02001101 ((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 +02001102 (sc->flags & SC_FL_ERROR) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001103
1104 /* changes on the consumption side */
Christopher Faulet2e56a732023-01-26 16:18:09 +01001105 sc_ep_test(sc, SE_FL_ERR_PENDING) ||
Christopher Fauletd8988412022-12-20 18:10:04 +01001106 ((oc->flags & CF_WRITE_EVENT) &&
1107 ((sc->state < SC_ST_EST) ||
Christopher Faulet208c7122023-04-13 16:16:15 +02001108 (sc->flags & SC_FL_SHUT_DONE) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001109 (((oc->flags & CF_WAKE_WRITE) ||
Christopher Faulet87633c32023-04-03 18:32:50 +02001110 (!(oc->flags & CF_AUTO_CLOSE) &&
Christopher Faulet208c7122023-04-13 16:16:15 +02001111 !(sc->flags & (SC_FL_SHUT_WANTED|SC_FL_SHUT_DONE)))) &&
Christopher Faulet87633c32023-04-03 18:32:50 +02001112 (sco->state != SC_ST_EST ||
1113 (channel_is_empty(oc) && !oc->to_forward)))))) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001114 task_wakeup(task, TASK_WOKEN_IO);
1115 }
Christopher Faulet5e29b762022-04-04 08:58:34 +02001116
Christopher Faulet2e56a732023-01-26 16:18:09 +01001117 if (ic->flags & CF_READ_EVENT)
Christopher Faulet9a790f62023-03-16 14:40:03 +01001118 sc->flags &= ~SC_FL_RCV_ONCE;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001119}
1120
1121/*
Christopher Faulet1aec6c92023-04-17 17:29:29 +02001122 * This function propagates an end-of-stream received on a socket-based connection.
Willy Tarreaucb041662022-05-17 19:44:42 +02001123 * It updates the stream connector. If the stream connector has SC_FL_NOHALF,
Christopher Faulet5e29b762022-04-04 08:58:34 +02001124 * the close is also forwarded to the write side as an abort.
1125 */
Christopher Faulet1aec6c92023-04-17 17:29:29 +02001126static void sc_conn_eos(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001127{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001128 struct channel *ic = sc_ic(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001129
Willy Tarreau0adb2812022-05-27 10:02:48 +02001130 BUG_ON(!sc_conn(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001131
Christopher Fauletca5309a2023-04-17 16:17:32 +02001132 if (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001133 return;
Christopher Faulet1aec6c92023-04-17 17:29:29 +02001134 sc->flags |= SC_FL_EOS;
Christopher Faulet87633c32023-04-03 18:32:50 +02001135 ic->flags |= CF_READ_EVENT;
Christopher Faulet4c135682023-02-16 11:09:31 +01001136 sc_ep_report_read_activity(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001137
Willy Tarreau0adb2812022-05-27 10:02:48 +02001138 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001139 return;
1140
Christopher Faulet208c7122023-04-13 16:16:15 +02001141 if (sc->flags & SC_FL_SHUT_DONE)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001142 goto do_close;
1143
Christopher Fauletb2b1c3a2023-04-13 16:23:48 +02001144 if (sc_cond_forward_shut(sc)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001145 /* we want to immediately forward this close to the write side */
1146 /* force flag on ssl to keep stream in cache */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001147 sc_conn_shutw(sc, CO_SHW_SILENT);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001148 goto do_close;
1149 }
1150
1151 /* otherwise that's just a normal read shutdown */
1152 return;
1153
1154 do_close:
Willy Tarreauf61dd192022-05-27 09:00:19 +02001155 /* OK we completely close the socket here just as if we went through sc_shut[rw]() */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001156 sc_conn_shut(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001157
Christopher Faulete38534c2023-04-13 15:45:24 +02001158 sc->flags &= ~SC_FL_SHUT_WANTED;
Christopher Faulet208c7122023-04-13 16:16:15 +02001159 sc->flags |= SC_FL_SHUT_DONE;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001160
Willy Tarreau0adb2812022-05-27 10:02:48 +02001161 sc->state = SC_ST_DIS;
Christopher Fauletca679922022-07-20 13:24:04 +02001162 if (sc->flags & SC_FL_ISBACK)
1163 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001164 return;
1165}
1166
1167/*
1168 * This is the callback which is called by the connection layer to receive data
1169 * into the buffer from the connection. It iterates over the mux layer's
1170 * rcv_buf function.
1171 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001172static int sc_conn_recv(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001173{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001174 struct connection *conn = __sc_conn(sc);
1175 struct channel *ic = sc_ic(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001176 int ret, max, cur_read = 0;
1177 int read_poll = MAX_READ_POLL_LOOPS;
1178 int flags = 0;
1179
1180 /* If not established yet, do nothing. */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001181 if (sc->state != SC_ST_EST)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001182 return 0;
1183
Willy Tarreau462b9892022-05-18 18:06:53 +02001184 /* If another call to sc_conn_recv() failed, and we subscribed to
Christopher Faulet5e29b762022-04-04 08:58:34 +02001185 * recv events already, give up now.
1186 */
Christopher Faulet95125882023-04-12 18:35:18 +02001187 if ((sc->wait_event.events & SUB_RETRY_RECV) || sc_waiting_room(sc))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001188 return 0;
1189
Christopher Fauletcfc11c02023-04-13 16:10:23 +02001190 /* maybe we were called immediately after an asynchronous abort */
Christopher Fauletca5309a2023-04-17 16:17:32 +02001191 if (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001192 return 1;
1193
1194 /* we must wait because the mux is not installed yet */
1195 if (!conn->mux)
1196 return 0;
1197
Christopher Faulet5e29b762022-04-04 08:58:34 +02001198 /* stop immediately on errors. Note that we DON'T want to stop on
1199 * POLL_ERR, as the poller might report a write error while there
1200 * are still data available in the recv buffer. This typically
1201 * happens when we send too large a request to a backend server
1202 * which rejects it before reading it all.
1203 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001204 if (!sc_ep_test(sc, SE_FL_RCV_MORE)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001205 if (!conn_xprt_ready(conn))
1206 return 0;
Willy Tarreau0adb2812022-05-27 10:02:48 +02001207 if (sc_ep_test(sc, SE_FL_ERROR))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001208 goto end_recv;
1209 }
1210
1211 /* prepare to detect if the mux needs more room */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001212 sc_ep_clr(sc, SE_FL_WANT_ROOM);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001213
1214 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) && !co_data(ic) &&
1215 global.tune.idle_timer &&
1216 (unsigned short)(now_ms - ic->last_read) >= global.tune.idle_timer) {
1217 /* The buffer was empty and nothing was transferred for more
1218 * than one second. This was caused by a pause and not by
1219 * congestion. Reset any streaming mode to reduce latency.
1220 */
1221 ic->xfer_small = 0;
1222 ic->xfer_large = 0;
1223 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
1224 }
1225
1226 /* First, let's see if we may splice data across the channel without
1227 * using a buffer.
1228 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001229 if (sc_ep_test(sc, SE_FL_MAY_SPLICE) &&
Christopher Faulet5e29b762022-04-04 08:58:34 +02001230 (ic->pipe || ic->to_forward >= MIN_SPLICE_FORWARD) &&
1231 ic->flags & CF_KERN_SPLICING) {
1232 if (c_data(ic)) {
1233 /* We're embarrassed, there are already data pending in
1234 * the buffer and we don't want to have them at two
1235 * locations at a time. Let's indicate we need some
1236 * place and ask the consumer to hurry.
1237 */
1238 flags |= CO_RFL_BUF_FLUSH;
1239 goto abort_splice;
1240 }
1241
1242 if (unlikely(ic->pipe == NULL)) {
1243 if (pipes_used >= global.maxpipes || !(ic->pipe = get_pipe())) {
1244 ic->flags &= ~CF_KERN_SPLICING;
1245 goto abort_splice;
1246 }
1247 }
1248
Willy Tarreau0adb2812022-05-27 10:02:48 +02001249 ret = conn->mux->rcv_pipe(sc, ic->pipe, ic->to_forward);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001250 if (ret < 0) {
1251 /* splice not supported on this end, let's disable it */
1252 ic->flags &= ~CF_KERN_SPLICING;
1253 goto abort_splice;
1254 }
1255
1256 if (ret > 0) {
1257 if (ic->to_forward != CHN_INFINITE_FORWARD)
1258 ic->to_forward -= ret;
1259 ic->total += ret;
1260 cur_read += ret;
Christopher Faulet285f7612022-12-12 08:28:55 +01001261 ic->flags |= CF_READ_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001262 }
1263
Willy Tarreau0adb2812022-05-27 10:02:48 +02001264 if (sc_ep_test(sc, SE_FL_EOS | SE_FL_ERROR))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001265 goto end_recv;
1266
1267 if (conn->flags & CO_FL_WAIT_ROOM) {
1268 /* the pipe is full or we have read enough data that it
1269 * could soon be full. Let's stop before needing to poll.
1270 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001271 sc_need_room(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001272 goto done_recv;
1273 }
1274
1275 /* splice not possible (anymore), let's go on on standard copy */
1276 }
1277
1278 abort_splice:
1279 if (ic->pipe && unlikely(!ic->pipe->data)) {
1280 put_pipe(ic->pipe);
1281 ic->pipe = NULL;
1282 }
1283
Willy Tarreau0adb2812022-05-27 10:02:48 +02001284 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 +02001285 /* don't break splicing by reading, but still call rcv_buf()
1286 * to pass the flag.
1287 */
1288 goto done_recv;
1289 }
1290
1291 /* now we'll need a input buffer for the stream */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001292 if (!sc_alloc_ibuf(sc, &(__sc_strm(sc)->buffer_wait)))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001293 goto end_recv;
1294
1295 /* For an HTX stream, if the buffer is stuck (no output data with some
1296 * input data) and if the HTX message is fragmented or if its free space
1297 * wraps, we force an HTX deframentation. It is a way to have a
1298 * contiguous free space nad to let the mux to copy as much data as
1299 * possible.
1300 *
1301 * NOTE: A possible optim may be to let the mux decides if defrag is
1302 * required or not, depending on amount of data to be xferred.
1303 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001304 if (IS_HTX_STRM(__sc_strm(sc)) && !co_data(ic)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001305 struct htx *htx = htxbuf(&ic->buf);
1306
1307 if (htx_is_not_empty(htx) && ((htx->flags & HTX_FL_FRAGMENTED) || htx_space_wraps(htx)))
1308 htx_defrag(htx, NULL, 0);
1309 }
1310
1311 /* Instruct the mux it must subscribed for read events */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001312 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 +02001313
1314 /* Important note : if we're called with POLL_IN|POLL_HUP, it means the read polling
1315 * was enabled, which implies that the recv buffer was not full. So we have a guarantee
1316 * that if such an event is not handled above in splice, it will be handled here by
1317 * recv().
1318 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001319 while (sc_ep_test(sc, SE_FL_RCV_MORE) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001320 (!(conn->flags & CO_FL_HANDSHAKE) &&
Christopher Fauletca5309a2023-04-17 16:17:32 +02001321 (!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 +02001322 int cur_flags = flags;
1323
1324 /* Compute transient CO_RFL_* flags */
1325 if (co_data(ic)) {
1326 cur_flags |= (CO_RFL_BUF_WET | CO_RFL_BUF_NOT_STUCK);
1327 }
1328
1329 /* <max> may be null. This is the mux responsibility to set
Willy Tarreaue68bc612022-05-27 11:23:05 +02001330 * SE_FL_RCV_MORE on the SC if more space is needed.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001331 */
1332 max = channel_recv_max(ic);
Willy Tarreau0adb2812022-05-27 10:02:48 +02001333 ret = conn->mux->rcv_buf(sc, &ic->buf, max, cur_flags);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001334
Willy Tarreau0adb2812022-05-27 10:02:48 +02001335 if (sc_ep_test(sc, SE_FL_WANT_ROOM)) {
Willy Tarreaub605c422022-05-17 17:04:55 +02001336 /* SE_FL_WANT_ROOM must not be reported if the channel's
Christopher Faulet5e29b762022-04-04 08:58:34 +02001337 * buffer is empty.
1338 */
1339 BUG_ON(c_empty(ic));
1340
Willy Tarreau0adb2812022-05-27 10:02:48 +02001341 sc_need_room(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001342 /* Add READ_PARTIAL because some data are pending but
1343 * cannot be xferred to the channel
1344 */
Christopher Faulet285f7612022-12-12 08:28:55 +01001345 ic->flags |= CF_READ_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001346 }
1347
1348 if (ret <= 0) {
1349 /* if we refrained from reading because we asked for a
1350 * flush to satisfy rcv_pipe(), we must not subscribe
1351 * and instead report that there's not enough room
1352 * here to proceed.
1353 */
1354 if (flags & CO_RFL_BUF_FLUSH)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001355 sc_need_room(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001356 break;
1357 }
1358
1359 cur_read += ret;
1360
1361 /* if we're allowed to directly forward data, we must update ->o */
Christopher Faulet64350bb2023-04-13 16:37:37 +02001362 if (ic->to_forward && !(sc_opposite(sc)->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED))) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001363 unsigned long fwd = ret;
1364 if (ic->to_forward != CHN_INFINITE_FORWARD) {
1365 if (fwd > ic->to_forward)
1366 fwd = ic->to_forward;
1367 ic->to_forward -= fwd;
1368 }
1369 c_adv(ic, fwd);
1370 }
1371
Christopher Faulet285f7612022-12-12 08:28:55 +01001372 ic->flags |= CF_READ_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001373 ic->total += ret;
1374
1375 /* End-of-input reached, we can leave. In this case, it is
Willy Tarreaue68bc612022-05-27 11:23:05 +02001376 * important to break the loop to not block the SC because of
Christopher Faulet5e29b762022-04-04 08:58:34 +02001377 * the channel's policies.This way, we are still able to receive
1378 * shutdowns.
1379 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001380 if (sc_ep_test(sc, SE_FL_EOI))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001381 break;
1382
Christopher Faulet9a790f62023-03-16 14:40:03 +01001383 if ((sc->flags & SC_FL_RCV_ONCE) || --read_poll <= 0) {
1384 /* we don't expect to read more data */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001385 sc_wont_read(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001386 break;
1387 }
1388
1389 /* if too many bytes were missing from last read, it means that
1390 * it's pointless trying to read again because the system does
1391 * not have them in buffers.
1392 */
1393 if (ret < max) {
1394 /* if a streamer has read few data, it may be because we
1395 * have exhausted system buffers. It's not worth trying
1396 * again.
1397 */
1398 if (ic->flags & CF_STREAMER) {
1399 /* we're stopped by the channel's policy */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001400 sc_wont_read(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001401 break;
1402 }
1403
1404 /* if we read a large block smaller than what we requested,
1405 * it's almost certain we'll never get anything more.
1406 */
1407 if (ret >= global.tune.recv_enough) {
1408 /* we're stopped by the channel's policy */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001409 sc_wont_read(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001410 break;
1411 }
1412 }
1413
1414 /* if we are waiting for more space, don't try to read more data
1415 * right now.
1416 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001417 if (sc->flags & (SC_FL_WONT_READ|SC_FL_NEED_BUFF|SC_FL_NEED_ROOM))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001418 break;
1419 } /* while !flags */
1420
1421 done_recv:
1422 if (cur_read) {
1423 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) &&
1424 (cur_read <= ic->buf.size / 2)) {
1425 ic->xfer_large = 0;
1426 ic->xfer_small++;
1427 if (ic->xfer_small >= 3) {
1428 /* we have read less than half of the buffer in
1429 * one pass, and this happened at least 3 times.
1430 * This is definitely not a streamer.
1431 */
1432 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
1433 }
1434 else if (ic->xfer_small >= 2) {
1435 /* if the buffer has been at least half full twice,
1436 * we receive faster than we send, so at least it
1437 * is not a "fast streamer".
1438 */
1439 ic->flags &= ~CF_STREAMER_FAST;
1440 }
1441 }
1442 else if (!(ic->flags & CF_STREAMER_FAST) &&
1443 (cur_read >= ic->buf.size - global.tune.maxrewrite)) {
1444 /* we read a full buffer at once */
1445 ic->xfer_small = 0;
1446 ic->xfer_large++;
1447 if (ic->xfer_large >= 3) {
1448 /* we call this buffer a fast streamer if it manages
1449 * to be filled in one call 3 consecutive times.
1450 */
1451 ic->flags |= (CF_STREAMER | CF_STREAMER_FAST);
1452 }
1453 }
1454 else {
1455 ic->xfer_small = 0;
1456 ic->xfer_large = 0;
1457 }
1458 ic->last_read = now_ms;
Christopher Faulet4c135682023-02-16 11:09:31 +01001459 sc_ep_report_read_activity(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001460 }
1461
1462 end_recv:
1463 ret = (cur_read != 0);
1464
1465 /* Report EOI on the channel if it was reached from the mux point of
1466 * view. */
Christopher Faulet904763f2023-03-22 14:53:11 +01001467 if (sc_ep_test(sc, SE_FL_EOI) && !(sc->flags & SC_FL_EOI)) {
Christopher Faulet4c135682023-02-16 11:09:31 +01001468 sc_ep_report_read_activity(sc);
Christopher Faulet904763f2023-03-22 14:53:11 +01001469 sc->flags |= SC_FL_EOI;
1470 ic->flags |= CF_READ_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001471 ret = 1;
1472 }
1473
Christopher Fauletb208d8c2023-03-21 11:25:21 +01001474 if (sc_ep_test(sc, SE_FL_EOS)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001475 /* we received a shutdown */
Christopher Faulet5e29b762022-04-04 08:58:34 +02001476 if (ic->flags & CF_AUTO_CLOSE)
Christopher Fauletdf7cd712023-04-13 15:56:26 +02001477 sc_schedule_shutdown(sc_opposite(sc));
Christopher Faulet1aec6c92023-04-17 17:29:29 +02001478 sc_conn_eos(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001479 ret = 1;
1480 }
Christopher Fauletb208d8c2023-03-21 11:25:21 +01001481
Christopher Fauleta1d14a72023-04-14 10:42:08 +02001482 if (sc_ep_test(sc, SE_FL_ERROR)) {
1483 sc->flags |= SC_FL_ERROR;
Christopher Fauletb208d8c2023-03-21 11:25:21 +01001484 ret = 1;
Christopher Fauleta1d14a72023-04-14 10:42:08 +02001485 }
Willy Tarreau0adb2812022-05-27 10:02:48 +02001486 else if (!(sc->flags & (SC_FL_WONT_READ|SC_FL_NEED_BUFF|SC_FL_NEED_ROOM)) &&
Christopher Fauletca5309a2023-04-17 16:17:32 +02001487 !(sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001488 /* Subscribe to receive events if we're blocking on I/O */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001489 conn->mux->subscribe(sc, SUB_RETRY_RECV, &sc->wait_event);
1490 se_have_no_more_data(sc->sedesc);
Christopher Fauletb208d8c2023-03-21 11:25:21 +01001491 }
1492 else {
Willy Tarreau0adb2812022-05-27 10:02:48 +02001493 se_have_more_data(sc->sedesc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001494 ret = 1;
1495 }
Christopher Faulet8019f782023-03-23 17:30:29 +01001496
1497 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 +02001498 return ret;
1499}
1500
Willy Tarreau4596fe22022-05-17 19:07:51 +02001501/* This tries to perform a synchronous receive on the stream connector to
Christopher Faulet5e29b762022-04-04 08:58:34 +02001502 * try to collect last arrived data. In practice it's only implemented on
Willy Tarreau4596fe22022-05-17 19:07:51 +02001503 * stconns. Returns 0 if nothing was done, non-zero if new data or a
Christopher Faulet5e29b762022-04-04 08:58:34 +02001504 * shutdown were collected. This may result on some delayed receive calls
1505 * to be programmed and performed later, though it doesn't provide any
1506 * such guarantee.
1507 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001508int sc_conn_sync_recv(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001509{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001510 if (!sc_state_in(sc->state, SC_SB_RDY|SC_SB_EST))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001511 return 0;
1512
Willy Tarreau0adb2812022-05-27 10:02:48 +02001513 if (!sc_mux_ops(sc))
Willy Tarreau4596fe22022-05-17 19:07:51 +02001514 return 0; // only stconns are supported
Christopher Faulet5e29b762022-04-04 08:58:34 +02001515
Willy Tarreau0adb2812022-05-27 10:02:48 +02001516 if (sc->wait_event.events & SUB_RETRY_RECV)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001517 return 0; // already subscribed
1518
Willy Tarreau0adb2812022-05-27 10:02:48 +02001519 if (!sc_is_recv_allowed(sc))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001520 return 0; // already failed
1521
Willy Tarreau0adb2812022-05-27 10:02:48 +02001522 return sc_conn_recv(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001523}
1524
1525/*
1526 * This function is called to send buffer data to a stream socket.
1527 * It calls the mux layer's snd_buf function. It relies on the
1528 * caller to commit polling changes. The caller should check conn->flags
1529 * for errors.
1530 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001531static int sc_conn_send(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001532{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001533 struct connection *conn = __sc_conn(sc);
Christopher Faulet904763f2023-03-22 14:53:11 +01001534 struct stconn *sco = sc_opposite(sc);
Willy Tarreau0adb2812022-05-27 10:02:48 +02001535 struct stream *s = __sc_strm(sc);
1536 struct channel *oc = sc_oc(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001537 int ret;
1538 int did_send = 0;
1539
Willy Tarreau0adb2812022-05-27 10:02:48 +02001540 if (sc_ep_test(sc, SE_FL_ERROR | SE_FL_ERR_PENDING) || sc_is_conn_error(sc)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001541 /* We're probably there because the tasklet was woken up,
1542 * but process_stream() ran before, detected there were an
Willy Tarreaue68bc612022-05-27 11:23:05 +02001543 * error and put the SC back to SC_ST_TAR. There's still
Christopher Faulet5e29b762022-04-04 08:58:34 +02001544 * CO_FL_ERROR on the connection but we don't want to add
Willy Tarreaub605c422022-05-17 17:04:55 +02001545 * SE_FL_ERROR back, so give up
Christopher Faulet5e29b762022-04-04 08:58:34 +02001546 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001547 if (sc->state < SC_ST_CON)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001548 return 0;
Christopher Faulet56a2b602023-04-14 09:42:59 +02001549 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 +02001550 return 1;
1551 }
1552
1553 /* We're already waiting to be able to send, give up */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001554 if (sc->wait_event.events & SUB_RETRY_SEND)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001555 return 0;
1556
1557 /* we might have been called just after an asynchronous shutw */
Christopher Faulet208c7122023-04-13 16:16:15 +02001558 if (sc->flags & SC_FL_SHUT_DONE)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001559 return 1;
1560
1561 /* we must wait because the mux is not installed yet */
1562 if (!conn->mux)
1563 return 0;
1564
1565 if (oc->pipe && conn->xprt->snd_pipe && conn->mux->snd_pipe) {
Willy Tarreau0adb2812022-05-27 10:02:48 +02001566 ret = conn->mux->snd_pipe(sc, oc->pipe);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001567 if (ret > 0)
1568 did_send = 1;
1569
1570 if (!oc->pipe->data) {
1571 put_pipe(oc->pipe);
1572 oc->pipe = NULL;
1573 }
1574
1575 if (oc->pipe)
1576 goto end;
1577 }
1578
1579 /* At this point, the pipe is empty, but we may still have data pending
1580 * in the normal buffer.
1581 */
1582 if (co_data(oc)) {
1583 /* when we're here, we already know that there is no spliced
1584 * data left, and that there are sendable buffered data.
1585 */
1586
1587 /* check if we want to inform the kernel that we're interested in
1588 * sending more data after this call. We want this if :
1589 * - we're about to close after this last send and want to merge
1590 * the ongoing FIN with the last segment.
1591 * - we know we can't send everything at once and must get back
1592 * here because of unaligned data
1593 * - there is still a finite amount of data to forward
1594 * The test is arranged so that the most common case does only 2
1595 * tests.
1596 */
1597 unsigned int send_flag = 0;
1598
Christopher Faulet68ef2182023-03-17 15:38:18 +01001599 if ((!(sc->flags & (SC_FL_SND_ASAP|SC_FL_SND_NEVERWAIT)) &&
Christopher Faulet5e29b762022-04-04 08:58:34 +02001600 ((oc->to_forward && oc->to_forward != CHN_INFINITE_FORWARD) ||
Christopher Faulet84d3ef92023-03-17 15:45:58 +01001601 (sc->flags & SC_FL_SND_EXP_MORE) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001602 (IS_HTX_STRM(s) &&
Christopher Fauletca5309a2023-04-17 16:17:32 +02001603 (!(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 +02001604 ((oc->flags & CF_ISRESP) &&
Christopher Faulet87633c32023-04-03 18:32:50 +02001605 (oc->flags & CF_AUTO_CLOSE) &&
Christopher Faulete38534c2023-04-13 15:45:24 +02001606 (sc->flags & SC_FL_SHUT_WANTED)))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001607 send_flag |= CO_SFL_MSG_MORE;
1608
1609 if (oc->flags & CF_STREAMER)
1610 send_flag |= CO_SFL_STREAMER;
1611
1612 if (s->txn && s->txn->flags & TX_L7_RETRY && !b_data(&s->txn->l7_buffer)) {
1613 /* If we want to be able to do L7 retries, copy
1614 * the data we're about to send, so that we are able
1615 * to resend them if needed
1616 */
1617 /* Try to allocate a buffer if we had none.
1618 * If it fails, the next test will just
1619 * disable the l7 retries by setting
1620 * l7_conn_retries to 0.
1621 */
1622 if (s->txn->req.msg_state != HTTP_MSG_DONE)
1623 s->txn->flags &= ~TX_L7_RETRY;
1624 else {
1625 if (b_alloc(&s->txn->l7_buffer) == NULL)
1626 s->txn->flags &= ~TX_L7_RETRY;
1627 else {
1628 memcpy(b_orig(&s->txn->l7_buffer),
1629 b_orig(&oc->buf),
1630 b_size(&oc->buf));
1631 s->txn->l7_buffer.head = co_data(oc);
1632 b_add(&s->txn->l7_buffer, co_data(oc));
1633 }
1634
1635 }
1636 }
1637
Willy Tarreau0adb2812022-05-27 10:02:48 +02001638 ret = conn->mux->snd_buf(sc, &oc->buf, co_data(oc), send_flag);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001639 if (ret > 0) {
1640 did_send = 1;
1641 c_rew(oc, ret);
1642 c_realign_if_empty(oc);
1643
1644 if (!co_data(oc)) {
1645 /* Always clear both flags once everything has been sent, they're one-shot */
Christopher Faulet84d3ef92023-03-17 15:45:58 +01001646 sc->flags &= ~(SC_FL_SND_ASAP|SC_FL_SND_EXP_MORE);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001647 }
1648 /* if some data remain in the buffer, it's only because the
1649 * system buffers are full, we will try next time.
1650 */
Christopher Faulet13045f02022-04-01 14:23:38 +02001651 }
1652 }
Christopher Faulet5e29b762022-04-04 08:58:34 +02001653
1654 end:
1655 if (did_send) {
Christopher Fauletd8988412022-12-20 18:10:04 +01001656 oc->flags |= CF_WRITE_EVENT | CF_WROTE_DATA;
Willy Tarreau0adb2812022-05-27 10:02:48 +02001657 if (sc->state == SC_ST_CON)
1658 sc->state = SC_ST_RDY;
Willy Tarreau0adb2812022-05-27 10:02:48 +02001659 sc_have_room(sc_opposite(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001660 }
1661
Willy Tarreau0adb2812022-05-27 10:02:48 +02001662 if (sc_ep_test(sc, SE_FL_ERROR | SE_FL_ERR_PENDING)) {
Christopher Faulet2e56a732023-01-26 16:18:09 +01001663 oc->flags |= CF_WRITE_EVENT;
Christopher Faulet56a2b602023-04-14 09:42:59 +02001664 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 +02001665 if (sc_ep_test(sc, SE_FL_ERROR))
1666 sc->flags |= SC_FL_ERROR;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001667 return 1;
1668 }
1669
Christopher Faulet59b240c2023-02-27 16:38:12 +01001670 if (channel_is_empty(oc))
1671 sc_ep_report_send_activity(sc);
1672 else {
1673 /* 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 +02001674 conn->mux->subscribe(sc, SUB_RETRY_SEND, &sc->wait_event);
Christopher Faulet59b240c2023-02-27 16:38:12 +01001675 sc_ep_report_blocked_send(sc);
1676 }
1677
Christopher Faulet5e29b762022-04-04 08:58:34 +02001678 return did_send;
1679}
1680
Christopher Fauletd8988412022-12-20 18:10:04 +01001681/* perform a synchronous send() for the stream connector. The CF_WRITE_EVENT
1682 * flag are cleared prior to the attempt, and will possibly be updated in case
1683 * of success.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001684 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001685void sc_conn_sync_send(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001686{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001687 struct channel *oc = sc_oc(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001688
Christopher Fauletd8988412022-12-20 18:10:04 +01001689 oc->flags &= ~CF_WRITE_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001690
Christopher Faulet208c7122023-04-13 16:16:15 +02001691 if (sc->flags & SC_FL_SHUT_DONE)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001692 return;
1693
1694 if (channel_is_empty(oc))
1695 return;
1696
Willy Tarreau0adb2812022-05-27 10:02:48 +02001697 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001698 return;
1699
Willy Tarreau0adb2812022-05-27 10:02:48 +02001700 if (!sc_mux_ops(sc))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001701 return;
1702
Willy Tarreau0adb2812022-05-27 10:02:48 +02001703 sc_conn_send(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001704}
1705
1706/* Called by I/O handlers after completion.. It propagates
Willy Tarreau4596fe22022-05-17 19:07:51 +02001707 * connection flags to the stream connector, updates the stream (which may or
Christopher Faulet5e29b762022-04-04 08:58:34 +02001708 * may not take this opportunity to try to forward data), then update the
Willy Tarreau4596fe22022-05-17 19:07:51 +02001709 * connection's polling based on the channels and stream connector's final
Christopher Faulet5e29b762022-04-04 08:58:34 +02001710 * states. The function always returns 0.
1711 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001712static int sc_conn_process(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001713{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001714 struct connection *conn = __sc_conn(sc);
1715 struct channel *ic = sc_ic(sc);
1716 struct channel *oc = sc_oc(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001717
1718 BUG_ON(!conn);
1719
1720 /* If we have data to send, try it now */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001721 if (!channel_is_empty(oc) && !(sc->wait_event.events & SUB_RETRY_SEND))
1722 sc_conn_send(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001723
Willy Tarreau4596fe22022-05-17 19:07:51 +02001724 /* First step, report to the stream connector what was detected at the
Christopher Faulet5e29b762022-04-04 08:58:34 +02001725 * connection layer : errors and connection establishment.
Christopher Faulet88d05a02023-04-14 12:03:50 +02001726 * Only add SC_FL_ERROR if we're connected, or we're attempting to
Christopher Faulet5e29b762022-04-04 08:58:34 +02001727 * connect, we may get there because we got woken up, but only run
1728 * after process_stream() noticed there were an error, and decided
1729 * to retry to connect, the connection may still have CO_FL_ERROR,
Christopher Faulet88d05a02023-04-14 12:03:50 +02001730 * and we don't want to add SC_FL_ERROR back
Christopher Faulet5e29b762022-04-04 08:58:34 +02001731 *
Willy Tarreau462b9892022-05-18 18:06:53 +02001732 * Note: This test is only required because sc_conn_process is also the SI
1733 * wake callback. Otherwise sc_conn_recv()/sc_conn_send() already take
Christopher Faulet5e29b762022-04-04 08:58:34 +02001734 * care of it.
1735 */
1736
Willy Tarreau0adb2812022-05-27 10:02:48 +02001737 if (sc->state >= SC_ST_CON) {
Christopher Faulet88d05a02023-04-14 12:03:50 +02001738 if (sc_is_conn_error(sc))
Christopher Fauleta1d14a72023-04-14 10:42:08 +02001739 sc->flags |= SC_FL_ERROR;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001740 }
1741
1742 /* If we had early data, and the handshake ended, then
1743 * we can remove the flag, and attempt to wake the task up,
1744 * in the event there's an analyser waiting for the end of
1745 * the handshake.
1746 */
1747 if (!(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS)) &&
Willy Tarreau0adb2812022-05-27 10:02:48 +02001748 sc_ep_test(sc, SE_FL_WAIT_FOR_HS)) {
1749 sc_ep_clr(sc, SE_FL_WAIT_FOR_HS);
1750 task_wakeup(sc_strm_task(sc), TASK_WOKEN_MSG);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001751 }
1752
Willy Tarreau0adb2812022-05-27 10:02:48 +02001753 if (!sc_state_in(sc->state, SC_SB_EST|SC_SB_DIS|SC_SB_CLO) &&
Christopher Faulet5e29b762022-04-04 08:58:34 +02001754 (conn->flags & CO_FL_WAIT_XPRT) == 0) {
Christopher Fauletca679922022-07-20 13:24:04 +02001755 if (sc->flags & SC_FL_ISBACK)
1756 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Fauletb96f2aa2022-12-12 08:11:36 +01001757 oc->flags |= CF_WRITE_EVENT;
Willy Tarreau0adb2812022-05-27 10:02:48 +02001758 if (sc->state == SC_ST_CON)
1759 sc->state = SC_ST_RDY;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001760 }
1761
1762 /* Report EOS on the channel if it was reached from the mux point of
1763 * view.
1764 *
Willy Tarreau462b9892022-05-18 18:06:53 +02001765 * Note: This test is only required because sc_conn_process is also the SI
1766 * wake callback. Otherwise sc_conn_recv()/sc_conn_send() already take
Christopher Faulet5e29b762022-04-04 08:58:34 +02001767 * care of it.
1768 */
Christopher Faulet1aec6c92023-04-17 17:29:29 +02001769 if (sc_ep_test(sc, SE_FL_EOS) && !(sc->flags & SC_FL_EOS)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001770 /* we received a shutdown */
Christopher Faulet5e29b762022-04-04 08:58:34 +02001771 if (ic->flags & CF_AUTO_CLOSE)
Christopher Fauletdf7cd712023-04-13 15:56:26 +02001772 sc_schedule_shutdown(sc_opposite(sc));
Christopher Faulet1aec6c92023-04-17 17:29:29 +02001773 sc_conn_eos(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001774 }
1775
1776 /* Report EOI on the channel if it was reached from the mux point of
1777 * view.
1778 *
Willy Tarreau462b9892022-05-18 18:06:53 +02001779 * Note: This test is only required because sc_conn_process is also the SI
1780 * wake callback. Otherwise sc_conn_recv()/sc_conn_send() already take
Christopher Faulet5e29b762022-04-04 08:58:34 +02001781 * care of it.
1782 */
Christopher Faulet904763f2023-03-22 14:53:11 +01001783 if (sc_ep_test(sc, SE_FL_EOI) && !(sc->flags & SC_FL_EOI)) {
1784 sc->flags |= SC_FL_EOI;
1785 ic->flags |= CF_READ_EVENT;
1786 }
Christopher Faulet5e29b762022-04-04 08:58:34 +02001787
Christopher Fauleta1d14a72023-04-14 10:42:08 +02001788 if (sc_ep_test(sc, SE_FL_ERROR))
1789 sc->flags |= SC_FL_ERROR;
1790
Willy Tarreau4596fe22022-05-17 19:07:51 +02001791 /* Second step : update the stream connector and channels, try to forward any
Christopher Faulet5e29b762022-04-04 08:58:34 +02001792 * pending data, then possibly wake the stream up based on the new
Willy Tarreau4596fe22022-05-17 19:07:51 +02001793 * stream connector status.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001794 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001795 sc_notify(sc);
1796 stream_release_buffers(__sc_strm(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001797 return 0;
1798}
1799
Willy Tarreau4596fe22022-05-17 19:07:51 +02001800/* This is the ->process() function for any stream connector's wait_event task.
1801 * It's assigned during the stream connector's initialization, for any type of
1802 * stream connector. Thus it is always safe to perform a tasklet_wakeup() on a
Willy Tarreaue68bc612022-05-27 11:23:05 +02001803 * stream connector, as the presence of the SC is checked there.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001804 */
Willy Tarreau462b9892022-05-18 18:06:53 +02001805struct task *sc_conn_io_cb(struct task *t, void *ctx, unsigned int state)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001806{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001807 struct stconn *sc = ctx;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001808 int ret = 0;
1809
Willy Tarreau0adb2812022-05-27 10:02:48 +02001810 if (!sc_conn(sc))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001811 return t;
1812
Willy Tarreau0adb2812022-05-27 10:02:48 +02001813 if (!(sc->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(sc_oc(sc)))
1814 ret = sc_conn_send(sc);
1815 if (!(sc->wait_event.events & SUB_RETRY_RECV))
1816 ret |= sc_conn_recv(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001817 if (ret != 0)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001818 sc_conn_process(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001819
Willy Tarreau0adb2812022-05-27 10:02:48 +02001820 stream_release_buffers(__sc_strm(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001821 return t;
1822}
1823
Christopher Fauletb36e5122023-04-17 17:32:43 +02001824/*
1825 * This function propagates an end-of-stream received from an applet. It
1826 * updates the stream connector. If it is is already shut, the applet is
1827 * released. Otherwise, we try to forward the shutdown, immediately or ASAP.
1828 */
1829static void sc_applet_eos(struct stconn *sc)
1830{
1831 struct channel *ic = sc_ic(sc);
1832
1833 BUG_ON(!sc_appctx(sc));
1834
1835 if (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))
1836 return;
1837 sc->flags |= SC_FL_EOS;
1838 ic->flags |= CF_READ_EVENT;
1839
1840 /* Note: on abort, we don't call the applet */
1841
1842 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
1843 return;
1844
1845 if (sc->flags & SC_FL_SHUT_DONE) {
1846 appctx_shut(__sc_appctx(sc));
1847 sc->state = SC_ST_DIS;
1848 if (sc->flags & SC_FL_ISBACK)
1849 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
1850 }
1851 else if (sc_cond_forward_shut(sc))
1852 return sc_app_shut_applet(sc);
1853}
1854
Christopher Faulet5e29b762022-04-04 08:58:34 +02001855/* Callback to be used by applet handlers upon completion. It updates the stream
1856 * (which may or may not take this opportunity to try to forward data), then
Willy Tarreau4596fe22022-05-17 19:07:51 +02001857 * may re-enable the applet's based on the channels and stream connector's final
Christopher Faulet5e29b762022-04-04 08:58:34 +02001858 * states.
1859 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001860static int sc_applet_process(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001861{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001862 struct channel *ic = sc_ic(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001863
Willy Tarreau0adb2812022-05-27 10:02:48 +02001864 BUG_ON(!sc_appctx(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001865
Christopher Fauletf8fbb6d2023-03-21 11:49:21 +01001866 /* Report EOI on the channel if it was reached from the applet point of
1867 * view. */
Christopher Faulet904763f2023-03-22 14:53:11 +01001868 if (sc_ep_test(sc, SE_FL_EOI) && !(sc->flags & SC_FL_EOI)) {
Christopher Fauletf8fbb6d2023-03-21 11:49:21 +01001869 sc_ep_report_read_activity(sc);
Christopher Faulet904763f2023-03-22 14:53:11 +01001870 sc->flags |= SC_FL_EOI;
1871 ic->flags |= CF_READ_EVENT;
Christopher Fauletf8fbb6d2023-03-21 11:49:21 +01001872 }
1873
Christopher Fauleta1d14a72023-04-14 10:42:08 +02001874 if (sc_ep_test(sc, SE_FL_ERROR))
1875 sc->flags |= SC_FL_ERROR;
1876
Christopher Faulet0ffc9d72023-03-21 14:19:08 +01001877 if (sc_ep_test(sc, SE_FL_EOS)) {
1878 /* we received a shutdown */
Christopher Fauletb36e5122023-04-17 17:32:43 +02001879 sc_applet_eos(sc);
Christopher Faulet0ffc9d72023-03-21 14:19:08 +01001880 }
1881
Christopher Faulete8bcef52023-04-14 09:45:41 +02001882 BUG_ON(sc_ep_test(sc, SE_FL_HAVE_NO_DATA|SE_FL_EOI) == SE_FL_EOI);
1883
Christopher Faulet5e29b762022-04-04 08:58:34 +02001884 /* If the applet wants to write and the channel is closed, it's a
1885 * broken pipe and it must be reported.
1886 */
Christopher Fauletca5309a2023-04-17 16:17:32 +02001887 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 +02001888 sc_ep_set(sc, SE_FL_ERROR);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001889
1890 /* automatically mark the applet having data available if it reported
1891 * begin blocked by the channel.
1892 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001893 if ((sc->flags & (SC_FL_WONT_READ|SC_FL_NEED_BUFF|SC_FL_NEED_ROOM)) ||
1894 sc_ep_test(sc, SE_FL_APPLET_NEED_CONN))
1895 applet_have_more_data(__sc_appctx(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001896
Willy Tarreau4596fe22022-05-17 19:07:51 +02001897 /* update the stream connector, channels, and possibly wake the stream up */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001898 sc_notify(sc);
1899 stream_release_buffers(__sc_strm(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001900
Willy Tarreau19c65a92022-05-27 08:49:24 +02001901 /* sc_notify may have passed through chk_snd and released some blocking
Willy Tarreau15252cd2022-05-25 16:36:21 +02001902 * flags. Process_stream will consider those flags to wake up the
Christopher Faulet5e29b762022-04-04 08:58:34 +02001903 * appctx but in the case the task is not in runqueue we may have to
1904 * wakeup the appctx immediately.
1905 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001906 if (sc_is_recv_allowed(sc) || sc_is_send_allowed(sc))
1907 appctx_wakeup(__sc_appctx(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001908 return 0;
Christopher Faulet13045f02022-04-01 14:23:38 +02001909}
Christopher Fauletb68f77d2022-06-16 16:24:16 +02001910
1911
1912/* Prepares an endpoint upgrade. We don't now at this stage if the upgrade will
1913 * succeed or not and if the stconn will be reused by the new endpoint. Thus,
1914 * for now, only pretend the stconn is detached.
1915 */
1916void sc_conn_prepare_endp_upgrade(struct stconn *sc)
1917{
1918 BUG_ON(!sc_conn(sc) || !sc->app);
1919 sc_ep_clr(sc, SE_FL_T_MUX);
1920 sc_ep_set(sc, SE_FL_DETACHED);
1921}
1922
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001923/* Endpoint upgrade failed. Restore the stconn state. */
Christopher Fauletb68f77d2022-06-16 16:24:16 +02001924void sc_conn_abort_endp_upgrade(struct stconn *sc)
1925{
1926 sc_ep_set(sc, SE_FL_T_MUX);
1927 sc_ep_clr(sc, SE_FL_DETACHED);
1928}
1929
1930/* Commit the endpoint upgrade. If stconn is attached, it means the new endpoint
1931 * use it. So we do nothing. Otherwise, the stconn will be destroy with the
1932 * overlying stream. So, it means we must commit the detach.
1933*/
1934void sc_conn_commit_endp_upgrade(struct stconn *sc)
1935{
1936 if (!sc_ep_test(sc, SE_FL_DETACHED))
1937 return;
1938 sc_detach_endp(&sc);
1939 /* Because it was already set as detached, the sedesc must be preserved */
Willy Tarreau6a378d12022-08-11 13:56:42 +02001940 BUG_ON(!sc);
Christopher Fauletb68f77d2022-06-16 16:24:16 +02001941 BUG_ON(!sc->sedesc);
1942}