blob: 1e796c2642847a719e1503044e212c70c341ecc9 [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 */
Willy Tarreau0adb2812022-05-27 10:02:48 +020027static void sc_app_shutr(struct stconn *sc);
28static void sc_app_shutw(struct stconn *sc);
29static 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 */
Willy Tarreau0adb2812022-05-27 10:02:48 +020033static void sc_app_shutr_conn(struct stconn *sc);
34static void sc_app_shutw_conn(struct stconn *sc);
35static 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 */
Willy Tarreau0adb2812022-05-27 10:02:48 +020039static void sc_app_shutr_applet(struct stconn *sc);
40static void sc_app_shutw_applet(struct stconn *sc);
41static 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,
53 .shutr = sc_app_shutr_conn,
54 .shutw = sc_app_shutw_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,
63 .shutr = sc_app_shutr,
64 .shutw = sc_app_shutw,
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,
73 .shutr = sc_app_shutr_applet,
74 .shutw = sc_app_shutw_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,
83 .shutr = NULL,
84 .shutw = NULL,
85 .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 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200234 if (sc->wait_event.tasklet)
235 tasklet_free(sc->wait_event.tasklet);
236 pool_free(pool_head_connstream, sc);
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100237}
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100238
Willy Tarreau4596fe22022-05-17 19:07:51 +0200239/* Conditionally removes a stream connector if it is detached and if there is no app
Christopher Fauleteb50c012022-04-21 14:22:53 +0200240 * layer defined. Except on error path, this one must be used. if release, the
Willy Tarreaue68bc612022-05-27 11:23:05 +0200241 * pointer on the SC is set to NULL.
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200242 */
Willy Tarreaue68bc612022-05-27 11:23:05 +0200243static void sc_free_cond(struct stconn **scp)
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200244{
Willy Tarreaue68bc612022-05-27 11:23:05 +0200245 struct stconn *sc = *scp;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200246
Willy Tarreau0adb2812022-05-27 10:02:48 +0200247 if (!sc->app && (!sc->sedesc || sc_ep_test(sc, SE_FL_DETACHED))) {
248 sc_free(sc);
Willy Tarreaue68bc612022-05-27 11:23:05 +0200249 *scp = NULL;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200250 }
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200251}
252
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100253
Willy Tarreau4596fe22022-05-17 19:07:51 +0200254/* Attaches a stconn to a mux endpoint and sets the endpoint ctx. Returns
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500255 * -1 on error and 0 on success. SE_FL_DETACHED flag is removed. This function is
Christopher Faulet9ed77422022-04-12 08:51:15 +0200256 * called from a mux when it is attached to a stream or a health-check.
257 */
Willy Tarreau31219282022-05-27 16:21:33 +0200258int sc_attach_mux(struct stconn *sc, void *sd, void *ctx)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100259{
Christopher Faulet93882042022-01-19 14:56:50 +0100260 struct connection *conn = ctx;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200261 struct sedesc *sedesc = sc->sedesc;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100262
Willy Tarreau0adb2812022-05-27 10:02:48 +0200263 if (sc_strm(sc)) {
264 if (!sc->wait_event.tasklet) {
265 sc->wait_event.tasklet = tasklet_new();
266 if (!sc->wait_event.tasklet)
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200267 return -1;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200268 sc->wait_event.tasklet->process = sc_conn_io_cb;
269 sc->wait_event.tasklet->context = sc;
270 sc->wait_event.events = 0;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200271 }
272
Willy Tarreau0adb2812022-05-27 10:02:48 +0200273 sc->app_ops = &sc_app_conn_ops;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100274 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200275 else if (sc_check(sc)) {
276 if (!sc->wait_event.tasklet) {
277 sc->wait_event.tasklet = tasklet_new();
278 if (!sc->wait_event.tasklet)
Christopher Fauletc95eaef2022-05-18 15:57:15 +0200279 return -1;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200280 sc->wait_event.tasklet->process = srv_chk_io_cb;
281 sc->wait_event.tasklet->context = sc;
282 sc->wait_event.events = 0;
Christopher Fauletc95eaef2022-05-18 15:57:15 +0200283 }
284
Willy Tarreau0adb2812022-05-27 10:02:48 +0200285 sc->app_ops = &sc_app_check_ops;
Christopher Fauletc95eaef2022-05-18 15:57:15 +0200286 }
Willy Tarreaue2f79462023-03-20 19:45:41 +0100287
288 sedesc->se = sd;
289 sedesc->conn = ctx;
290 se_fl_set(sedesc, SE_FL_T_MUX);
291 se_fl_clr(sedesc, SE_FL_DETACHED);
292 if (!conn->ctx)
293 conn->ctx = sc;
Christopher Faulet070b91b2022-03-31 19:27:18 +0200294 return 0;
Christopher Faulet93882042022-01-19 14:56:50 +0100295}
296
Willy Tarreau4596fe22022-05-17 19:07:51 +0200297/* Attaches a stconn to an applet endpoint and sets the endpoint
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500298 * ctx. Returns -1 on error and 0 on success. SE_FL_DETACHED flag is
Christopher Faulet9ed77422022-04-12 08:51:15 +0200299 * removed. This function is called by a stream when a backend applet is
300 * registered.
301 */
Willy Tarreau31219282022-05-27 16:21:33 +0200302static void sc_attach_applet(struct stconn *sc, void *sd)
Christopher Faulet93882042022-01-19 14:56:50 +0100303{
Willy Tarreau31219282022-05-27 16:21:33 +0200304 sc->sedesc->se = sd;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200305 sc_ep_set(sc, SE_FL_T_APPLET);
306 sc_ep_clr(sc, SE_FL_DETACHED);
307 if (sc_strm(sc))
308 sc->app_ops = &sc_app_applet_ops;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100309}
310
Willy Tarreau4596fe22022-05-17 19:07:51 +0200311/* Attaches a stconn to a app layer and sets the relevant
Willy Tarreaub605c422022-05-17 17:04:55 +0200312 * callbacks. Returns -1 on error and 0 on success. SE_FL_ORPHAN flag is
Christopher Faulet9ed77422022-04-12 08:51:15 +0200313 * removed. This function is called by a stream when it is created to attach it
Willy Tarreau4596fe22022-05-17 19:07:51 +0200314 * on the stream connector on the client side.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200315 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200316int sc_attach_strm(struct stconn *sc, struct stream *strm)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100317{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200318 sc->app = &strm->obj_type;
319 sc_ep_clr(sc, SE_FL_ORPHAN);
320 if (sc_ep_test(sc, SE_FL_T_MUX)) {
321 sc->wait_event.tasklet = tasklet_new();
322 if (!sc->wait_event.tasklet)
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200323 return -1;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200324 sc->wait_event.tasklet->process = sc_conn_io_cb;
325 sc->wait_event.tasklet->context = sc;
326 sc->wait_event.events = 0;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200327
Willy Tarreau0adb2812022-05-27 10:02:48 +0200328 sc->app_ops = &sc_app_conn_ops;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100329 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200330 else if (sc_ep_test(sc, SE_FL_T_APPLET)) {
331 sc->app_ops = &sc_app_applet_ops;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100332 }
333 else {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200334 sc->app_ops = &sc_app_embedded_ops;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100335 }
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100336 return 0;
337}
338
Willy Tarreau4596fe22022-05-17 19:07:51 +0200339/* Detaches the stconn from the endpoint, if any. For a connecrion, if a
Christopher Faulet9ed77422022-04-12 08:51:15 +0200340 * mux owns the connection ->detach() callback is called. Otherwise, it means
Willy Tarreau4596fe22022-05-17 19:07:51 +0200341 * the stream connector owns the connection. In this case the connection is closed
Christopher Faulet9ed77422022-04-12 08:51:15 +0200342 * and released. For an applet, the appctx is released. If still allocated, the
343 * endpoint is reset and flag as detached. If the app layer is also detached,
Willy Tarreau4596fe22022-05-17 19:07:51 +0200344 * the stream connector is released.
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100345 */
Willy Tarreaue68bc612022-05-27 11:23:05 +0200346static void sc_detach_endp(struct stconn **scp)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100347{
Willy Tarreaue68bc612022-05-27 11:23:05 +0200348 struct stconn *sc = *scp;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200349
Willy Tarreau0adb2812022-05-27 10:02:48 +0200350 if (!sc)
Christopher Fauleteb50c012022-04-21 14:22:53 +0200351 return;
352
Willy Tarreau0adb2812022-05-27 10:02:48 +0200353 if (sc_ep_test(sc, SE_FL_T_MUX)) {
354 struct connection *conn = __sc_conn(sc);
355 struct sedesc *sedesc = sc->sedesc;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100356
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100357 if (conn->mux) {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200358 if (sc->wait_event.events != 0)
359 conn->mux->unsubscribe(sc, sc->wait_event.events, &sc->wait_event);
Willy Tarreau798465b2022-05-17 18:20:02 +0200360 se_fl_set(sedesc, SE_FL_ORPHAN);
Willy Tarreauc1054922022-05-18 07:43:52 +0200361 sedesc->sc = NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200362 sc->sedesc = NULL;
Willy Tarreau798465b2022-05-17 18:20:02 +0200363 conn->mux->detach(sedesc);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100364 }
365 else {
366 /* It's too early to have a mux, let's just destroy
367 * the connection
368 */
369 conn_stop_tracking(conn);
370 conn_full_close(conn);
371 if (conn->destroy_cb)
372 conn->destroy_cb(conn);
373 conn_free(conn);
374 }
375 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200376 else if (sc_ep_test(sc, SE_FL_T_APPLET)) {
377 struct appctx *appctx = __sc_appctx(sc);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100378
Willy Tarreau0adb2812022-05-27 10:02:48 +0200379 sc_ep_set(sc, SE_FL_ORPHAN);
380 sc->sedesc->sc = NULL;
381 sc->sedesc = NULL;
Willy Tarreau1c3ead42022-05-10 19:42:22 +0200382 appctx_shut(appctx);
383 appctx_free(appctx);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100384 }
385
Willy Tarreau0adb2812022-05-27 10:02:48 +0200386 if (sc->sedesc) {
Willy Tarreauda59c892022-05-27 17:03:34 +0200387 /* the SD wasn't used and can be recycled */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200388 sc->sedesc->se = NULL;
389 sc->sedesc->conn = NULL;
Willy Tarreauda59c892022-05-27 17:03:34 +0200390 sc->sedesc->flags = 0;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200391 sc_ep_set(sc, SE_FL_DETACHED);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100392 }
393
Willy Tarreaue68bc612022-05-27 11:23:05 +0200394 /* FIXME: Rest SC for now but must be reviewed. SC flags are only
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100395 * connection related for now but this will evolved
396 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200397 sc->flags &= SC_FL_ISBACK;
398 if (sc_strm(sc))
399 sc->app_ops = &sc_app_embedded_ops;
Willy Tarreau2f2318d2022-05-18 10:17:16 +0200400 else
Willy Tarreau0adb2812022-05-27 10:02:48 +0200401 sc->app_ops = NULL;
Willy Tarreaue68bc612022-05-27 11:23:05 +0200402 sc_free_cond(scp);
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100403}
404
Willy Tarreau4596fe22022-05-17 19:07:51 +0200405/* Detaches the stconn from the app layer. If there is no endpoint attached
406 * to the stconn
Christopher Faulet9ed77422022-04-12 08:51:15 +0200407 */
Willy Tarreaue68bc612022-05-27 11:23:05 +0200408static void sc_detach_app(struct stconn **scp)
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100409{
Willy Tarreaue68bc612022-05-27 11:23:05 +0200410 struct stconn *sc = *scp;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200411
Willy Tarreau0adb2812022-05-27 10:02:48 +0200412 if (!sc)
Christopher Fauleteb50c012022-04-21 14:22:53 +0200413 return;
414
Willy Tarreau0adb2812022-05-27 10:02:48 +0200415 sc->app = NULL;
416 sc->app_ops = NULL;
417 sockaddr_free(&sc->src);
418 sockaddr_free(&sc->dst);
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200419
Willy Tarreau0adb2812022-05-27 10:02:48 +0200420 if (sc->wait_event.tasklet)
421 tasklet_free(sc->wait_event.tasklet);
422 sc->wait_event.tasklet = NULL;
423 sc->wait_event.events = 0;
Willy Tarreaue68bc612022-05-27 11:23:05 +0200424 sc_free_cond(scp);
Christopher Fauleteb50c012022-04-21 14:22:53 +0200425}
426
Willy Tarreau4596fe22022-05-17 19:07:51 +0200427/* Destroy the stconn. It is detached from its endpoint and its
428 * application. After this call, the stconn must be considered as released.
Christopher Fauleteb50c012022-04-21 14:22:53 +0200429 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200430void sc_destroy(struct stconn *sc)
Christopher Fauleteb50c012022-04-21 14:22:53 +0200431{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200432 sc_detach_endp(&sc);
433 sc_detach_app(&sc);
434 BUG_ON_HOT(sc);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100435}
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100436
Willy Tarreau4596fe22022-05-17 19:07:51 +0200437/* Resets the stream connector endpoint. It happens when the app layer want to renew
Christopher Faulet9ed77422022-04-12 08:51:15 +0200438 * its endpoint. For a connection retry for instance. If a mux or an applet is
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500439 * attached, a new endpoint is created. Returns -1 on error and 0 on success.
Christopher Fauleta6c4a482022-04-28 18:25:24 +0200440 *
Willy Tarreaub605c422022-05-17 17:04:55 +0200441 * Only SE_FL_ERROR flag is removed on the endpoint. Orther flags are preserved.
Christopher Fauleta6c4a482022-04-28 18:25:24 +0200442 * It is the caller responsibility to remove other flags if needed.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200443 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200444int sc_reset_endp(struct stconn *sc)
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100445{
Willy Tarreau31219282022-05-27 16:21:33 +0200446 struct sedesc *new_sd;
Christopher Fauletb041b232022-03-24 10:27:02 +0100447
Willy Tarreau0adb2812022-05-27 10:02:48 +0200448 BUG_ON(!sc->app);
Christopher Fauleta6c4a482022-04-28 18:25:24 +0200449
Willy Tarreau0adb2812022-05-27 10:02:48 +0200450 sc_ep_clr(sc, SE_FL_ERROR);
451 if (!__sc_endp(sc)) {
Christopher Fauletb041b232022-03-24 10:27:02 +0100452 /* endpoint not attached or attached to a mux with no
453 * target. Thus the endpoint will not be release but just
Willy Tarreau0adb2812022-05-27 10:02:48 +0200454 * reset. The app is still attached, the sc will not be
Christopher Fauleteb50c012022-04-21 14:22:53 +0200455 * released.
Christopher Fauletb041b232022-03-24 10:27:02 +0100456 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200457 sc_detach_endp(&sc);
Christopher Fauletb041b232022-03-24 10:27:02 +0100458 return 0;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100459 }
Christopher Fauletb041b232022-03-24 10:27:02 +0100460
461 /* allocate the new endpoint first to be able to set error if it
462 * fails */
Willy Tarreau31219282022-05-27 16:21:33 +0200463 new_sd = sedesc_new();
464 if (!unlikely(new_sd)) {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200465 sc_ep_set(sc, SE_FL_ERROR);
Christopher Fauletb041b232022-03-24 10:27:02 +0100466 return -1;
467 }
468
Willy Tarreau0adb2812022-05-27 10:02:48 +0200469 /* The app is still attached, the sc will not be released */
470 sc_detach_endp(&sc);
Willy Tarreau6a378d12022-08-11 13:56:42 +0200471 BUG_ON(!sc);
Willy Tarreau0adb2812022-05-27 10:02:48 +0200472 BUG_ON(sc->sedesc);
Willy Tarreau31219282022-05-27 16:21:33 +0200473 sc->sedesc = new_sd;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200474 sc->sedesc->sc = sc;
475 sc_ep_set(sc, SE_FL_DETACHED);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100476 return 0;
477}
Christopher Faulet37046632022-04-01 11:36:58 +0200478
479
Willy Tarreaue68bc612022-05-27 11:23:05 +0200480/* Create an applet to handle a stream connector as a new appctx. The SC will
Christopher Faulet37046632022-04-01 11:36:58 +0200481 * wake it up every time it is solicited. The appctx must be deleted by the task
Willy Tarreau19c65a92022-05-27 08:49:24 +0200482 * handler using sc_detach_endp(), possibly from within the function itself.
Christopher Faulet37046632022-04-01 11:36:58 +0200483 * It also pre-initializes the applet's context and returns it (or NULL in case
484 * it could not be allocated).
485 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200486struct appctx *sc_applet_create(struct stconn *sc, struct applet *app)
Christopher Faulet37046632022-04-01 11:36:58 +0200487{
488 struct appctx *appctx;
489
Willy Tarreau0adb2812022-05-27 10:02:48 +0200490 DPRINTF(stderr, "registering handler %p for sc %p (was %p)\n", app, sc, sc_strm_task(sc));
Christopher Faulet37046632022-04-01 11:36:58 +0200491
Willy Tarreau0adb2812022-05-27 10:02:48 +0200492 appctx = appctx_new_here(app, sc->sedesc);
Christopher Faulet37046632022-04-01 11:36:58 +0200493 if (!appctx)
494 return NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200495 sc_attach_applet(sc, appctx);
496 appctx->t->nice = __sc_strm(sc)->task->nice;
Willy Tarreau90e8b452022-05-25 18:21:43 +0200497 applet_need_more_data(appctx);
Christopher Faulet37046632022-04-01 11:36:58 +0200498 appctx_wakeup(appctx);
Christopher Fauleta33ff7a2022-04-21 11:52:07 +0200499
Willy Tarreau0adb2812022-05-27 10:02:48 +0200500 sc->state = SC_ST_RDY;
Christopher Faulet37046632022-04-01 11:36:58 +0200501 return appctx;
502}
503
Ilya Shipitsin07be66d2023-04-01 12:26:42 +0200504/* Conditionally forward the close to the write side. It return 1 if it can be
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100505 * forwarded. It is the caller responsibility to forward the close to the write
506 * side. Otherwise, 0 is returned. In this case, CF_SHUTW_NOW flag may be set on
507 * the channel if we are only waiting for the outgoing data to be flushed.
508 */
509static inline int sc_cond_forward_shutw(struct stconn *sc)
510{
511 /* The close must not be forwarded */
512 if (!(sc_ic(sc)->flags & CF_SHUTR) || !(sc->flags & SC_FL_NOHALF))
513 return 0;
514
515 if (!channel_is_empty(sc_ic(sc))) {
516 /* the close to the write side cannot be forwarded now because
517 * we should flush outgoing data first. But instruct the output
518 * channel it should be done ASAP.
519 */
520 channel_shutw_now(sc_oc(sc));
521 return 0;
522 }
523
524 /* the close can be immediately forwarded to the write side */
525 return 1;
526}
527
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200528/*
Willy Tarreau4596fe22022-05-17 19:07:51 +0200529 * This function performs a shutdown-read on a detached stream connector in a
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200530 * connected or init state (it does nothing for other states). It either shuts
531 * the read side or marks itself as closed. The buffer flags are updated to
Willy Tarreaucb041662022-05-17 19:44:42 +0200532 * reflect the new state. If the stream connector has SC_FL_NOHALF, we also
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200533 * forward the close to the write side. The owner task is woken up if it exists.
534 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200535static void sc_app_shutr(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200536{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200537 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200538
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200539 if (ic->flags & CF_SHUTR)
Christopher Fauletb08c5252023-02-20 07:55:19 +0100540
541 ic->flags |= CF_SHUTR|CF_READ_EVENT;
Christopher Faulet4c135682023-02-16 11:09:31 +0100542 sc_ep_report_read_activity(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200543
Willy Tarreau0adb2812022-05-27 10:02:48 +0200544 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200545 return;
546
Willy Tarreau0adb2812022-05-27 10:02:48 +0200547 if (sc_oc(sc)->flags & CF_SHUTW) {
548 sc->state = SC_ST_DIS;
Christopher Fauletca679922022-07-20 13:24:04 +0200549 if (sc->flags & SC_FL_ISBACK)
550 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200551 }
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100552 else if (sc_cond_forward_shutw(sc))
Willy Tarreau0adb2812022-05-27 10:02:48 +0200553 return sc_app_shutw(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200554
555 /* note that if the task exists, it must unregister itself once it runs */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200556 if (!(sc->flags & SC_FL_DONT_WAKE))
557 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200558}
559
560/*
Willy Tarreau4596fe22022-05-17 19:07:51 +0200561 * This function performs a shutdown-write on a detached stream connector in a
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200562 * connected or init state (it does nothing for other states). It either shuts
563 * the write side or marks itself as closed. The buffer flags are updated to
Willy Tarreaue68bc612022-05-27 11:23:05 +0200564 * reflect the new state. It does also close everything if the SC was marked as
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200565 * being in error state. The owner task is woken up if it exists.
566 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200567static void sc_app_shutw(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200568{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200569 struct channel *ic = sc_ic(sc);
570 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200571
572 oc->flags &= ~CF_SHUTW_NOW;
573 if (oc->flags & CF_SHUTW)
574 return;
Christopher Fauletb08c5252023-02-20 07:55:19 +0100575 oc->flags |= CF_SHUTW|CF_WRITE_EVENT;
Christopher Fauletbcdcfad2023-02-20 08:36:53 +0100576 sc_set_hcto(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200577
Willy Tarreau0adb2812022-05-27 10:02:48 +0200578 switch (sc->state) {
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200579 case SC_ST_RDY:
580 case SC_ST_EST:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200581 /* we have to shut before closing, otherwise some short messages
582 * may never leave the system, especially when there are remaining
583 * unread data in the socket input buffer, or when nolinger is set.
Willy Tarreaucb041662022-05-17 19:44:42 +0200584 * However, if SC_FL_NOLINGER is explicitly set, we know there is
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200585 * no risk so we close both sides immediately.
586 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200587 if (!sc_ep_test(sc, SE_FL_ERROR) && !(sc->flags & SC_FL_NOLINGER) &&
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200588 !(ic->flags & (CF_SHUTR|CF_DONT_READ)))
589 return;
590
Willy Tarreau476c2802022-11-14 07:36:42 +0100591 __fallthrough;
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200592 case SC_ST_CON:
593 case SC_ST_CER:
594 case SC_ST_QUE:
595 case SC_ST_TAR:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200596 /* Note that none of these states may happen with applets */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200597 sc->state = SC_ST_DIS;
Willy Tarreau476c2802022-11-14 07:36:42 +0100598 __fallthrough;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200599 default:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200600 sc->flags &= ~SC_FL_NOLINGER;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200601 ic->flags |= CF_SHUTR;
Christopher Fauletca679922022-07-20 13:24:04 +0200602 if (sc->flags & SC_FL_ISBACK)
603 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200604 }
605
606 /* note that if the task exists, it must unregister itself once it runs */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200607 if (!(sc->flags & SC_FL_DONT_WAKE))
608 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200609}
610
611/* default chk_rcv function for scheduled tasks */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200612static void sc_app_chk_rcv(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200613{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200614 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200615
Willy Tarreau0adb2812022-05-27 10:02:48 +0200616 DPRINTF(stderr, "%s: sc=%p, sc->state=%d ic->flags=%08x oc->flags=%08x\n",
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200617 __FUNCTION__,
Willy Tarreau0adb2812022-05-27 10:02:48 +0200618 sc, sc->state, ic->flags, sc_oc(sc)->flags);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200619
620 if (ic->pipe) {
621 /* stop reading */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200622 sc_need_room(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200623 }
624 else {
625 /* (re)start reading */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200626 if (!(sc->flags & SC_FL_DONT_WAKE))
627 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200628 }
629}
630
631/* default chk_snd function for scheduled tasks */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200632static void sc_app_chk_snd(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200633{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200634 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200635
Willy Tarreau0adb2812022-05-27 10:02:48 +0200636 DPRINTF(stderr, "%s: sc=%p, sc->state=%d ic->flags=%08x oc->flags=%08x\n",
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200637 __FUNCTION__,
Willy Tarreau0adb2812022-05-27 10:02:48 +0200638 sc, sc->state, sc_ic(sc)->flags, oc->flags);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200639
Willy Tarreau0adb2812022-05-27 10:02:48 +0200640 if (unlikely(sc->state != SC_ST_EST || (oc->flags & CF_SHUTW)))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200641 return;
642
Willy Tarreau0adb2812022-05-27 10:02:48 +0200643 if (!sc_ep_test(sc, SE_FL_WAIT_DATA) || /* not waiting for data */
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200644 channel_is_empty(oc)) /* called with nothing to send ! */
645 return;
646
647 /* Otherwise there are remaining data to be sent in the buffer,
648 * so we tell the handler.
649 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200650 sc_ep_clr(sc, SE_FL_WAIT_DATA);
Willy Tarreau0adb2812022-05-27 10:02:48 +0200651 if (!(sc->flags & SC_FL_DONT_WAKE))
652 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200653}
654
655/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200656 * This function performs a shutdown-read on a stream connector attached to
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200657 * a connection in a connected or init state (it does nothing for other
658 * states). It either shuts the read side or marks itself as closed. The buffer
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200659 * flags are updated to reflect the new state. If the stream connector has
Willy Tarreaucb041662022-05-17 19:44:42 +0200660 * SC_FL_NOHALF, we also forward the close to the write side. If a control
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200661 * layer is defined, then it is supposed to be a socket layer and file
662 * descriptors are then shutdown or closed accordingly. The function
663 * automatically disables polling if needed.
664 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200665static void sc_app_shutr_conn(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200666{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200667 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200668
Willy Tarreau0adb2812022-05-27 10:02:48 +0200669 BUG_ON(!sc_conn(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200670
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200671 if (ic->flags & CF_SHUTR)
672 return;
Christopher Fauletb08c5252023-02-20 07:55:19 +0100673 ic->flags |= CF_SHUTR|CF_READ_EVENT;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200674
Willy Tarreau0adb2812022-05-27 10:02:48 +0200675 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200676 return;
677
Willy Tarreau0adb2812022-05-27 10:02:48 +0200678 if (sc_oc(sc)->flags & CF_SHUTW) {
679 sc_conn_shut(sc);
680 sc->state = SC_ST_DIS;
Christopher Fauletca679922022-07-20 13:24:04 +0200681 if (sc->flags & SC_FL_ISBACK)
682 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200683 }
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100684 else if (sc_cond_forward_shutw(sc))
Willy Tarreau0adb2812022-05-27 10:02:48 +0200685 return sc_app_shutw_conn(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200686}
687
688/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200689 * This function performs a shutdown-write on a stream connector attached to
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200690 * a connection in a connected or init state (it does nothing for other
691 * states). It either shuts the write side or marks itself as closed. The
692 * buffer flags are updated to reflect the new state. It does also close
Willy Tarreaue68bc612022-05-27 11:23:05 +0200693 * everything if the SC was marked as being in error state. If there is a
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200694 * data-layer shutdown, it is called.
695 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200696static void sc_app_shutw_conn(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200697{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200698 struct channel *ic = sc_ic(sc);
699 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200700
Willy Tarreau0adb2812022-05-27 10:02:48 +0200701 BUG_ON(!sc_conn(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200702
703 oc->flags &= ~CF_SHUTW_NOW;
704 if (oc->flags & CF_SHUTW)
705 return;
Christopher Fauletb08c5252023-02-20 07:55:19 +0100706 oc->flags |= CF_SHUTW|CF_WRITE_EVENT;
Christopher Fauletbcdcfad2023-02-20 08:36:53 +0100707 sc_set_hcto(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200708
Willy Tarreau0adb2812022-05-27 10:02:48 +0200709 switch (sc->state) {
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200710 case SC_ST_RDY:
711 case SC_ST_EST:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200712 /* we have to shut before closing, otherwise some short messages
713 * may never leave the system, especially when there are remaining
714 * unread data in the socket input buffer, or when nolinger is set.
Willy Tarreaucb041662022-05-17 19:44:42 +0200715 * However, if SC_FL_NOLINGER is explicitly set, we know there is
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200716 * no risk so we close both sides immediately.
717 */
718
Willy Tarreau0adb2812022-05-27 10:02:48 +0200719 if (sc_ep_test(sc, SE_FL_ERROR)) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200720 /* quick close, the socket is already shut anyway */
721 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200722 else if (sc->flags & SC_FL_NOLINGER) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200723 /* unclean data-layer shutdown, typically an aborted request
724 * or a forwarded shutdown from a client to a server due to
725 * option abortonclose. No need for the TLS layer to try to
726 * emit a shutdown message.
727 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200728 sc_conn_shutw(sc, CO_SHW_SILENT);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200729 }
730 else {
731 /* clean data-layer shutdown. This only happens on the
732 * frontend side, or on the backend side when forwarding
733 * a client close in TCP mode or in HTTP TUNNEL mode
734 * while option abortonclose is set. We want the TLS
735 * layer to try to signal it to the peer before we close.
736 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200737 sc_conn_shutw(sc, CO_SHW_NORMAL);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200738
739 if (!(ic->flags & (CF_SHUTR|CF_DONT_READ)))
740 return;
741 }
742
Willy Tarreau476c2802022-11-14 07:36:42 +0100743 __fallthrough;
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200744 case SC_ST_CON:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200745 /* we may have to close a pending connection, and mark the
746 * response buffer as shutr
747 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200748 sc_conn_shut(sc);
Willy Tarreau476c2802022-11-14 07:36:42 +0100749 __fallthrough;
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200750 case SC_ST_CER:
751 case SC_ST_QUE:
752 case SC_ST_TAR:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200753 sc->state = SC_ST_DIS;
Willy Tarreau476c2802022-11-14 07:36:42 +0100754 __fallthrough;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200755 default:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200756 sc->flags &= ~SC_FL_NOLINGER;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200757 ic->flags |= CF_SHUTR;
Christopher Fauletca679922022-07-20 13:24:04 +0200758 if (sc->flags & SC_FL_ISBACK)
759 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200760 }
761}
762
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200763/* This function is used for inter-stream connector calls. It is called by the
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200764 * consumer to inform the producer side that it may be interested in checking
765 * for free space in the buffer. Note that it intentionally does not update
766 * timeouts, so that we can still check them later at wake-up. This function is
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200767 * dedicated to connection-based stream connectors.
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200768 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200769static void sc_app_chk_rcv_conn(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200770{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200771 BUG_ON(!sc_conn(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200772
773 /* (re)start reading */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200774 if (sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
775 tasklet_wakeup(sc->wait_event.tasklet);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200776}
777
778
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200779/* This function is used for inter-stream connector calls. It is called by the
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200780 * producer to inform the consumer side that it may be interested in checking
781 * for data in the buffer. Note that it intentionally does not update timeouts,
782 * so that we can still check them later at wake-up.
783 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200784static void sc_app_chk_snd_conn(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200785{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200786 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200787
Willy Tarreau0adb2812022-05-27 10:02:48 +0200788 BUG_ON(!sc_conn(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200789
Willy Tarreau0adb2812022-05-27 10:02:48 +0200790 if (unlikely(!sc_state_in(sc->state, SC_SB_RDY|SC_SB_EST) ||
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200791 (oc->flags & CF_SHUTW)))
792 return;
793
794 if (unlikely(channel_is_empty(oc))) /* called with nothing to send ! */
795 return;
796
797 if (!oc->pipe && /* spliced data wants to be forwarded ASAP */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200798 !sc_ep_test(sc, SE_FL_WAIT_DATA)) /* not waiting for data */
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200799 return;
800
Willy Tarreau0adb2812022-05-27 10:02:48 +0200801 if (!(sc->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(sc_oc(sc)))
802 sc_conn_send(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200803
Willy Tarreau0adb2812022-05-27 10:02:48 +0200804 if (sc_ep_test(sc, SE_FL_ERROR | SE_FL_ERR_PENDING) || sc_is_conn_error(sc)) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200805 /* Write error on the file descriptor */
Christopher Faulet7f6aa562022-10-17 10:21:19 +0200806 if (sc->state >= SC_ST_CON && sc_ep_test(sc, SE_FL_EOS))
Willy Tarreau0adb2812022-05-27 10:02:48 +0200807 sc_ep_set(sc, SE_FL_ERROR);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200808 goto out_wakeup;
809 }
810
811 /* OK, so now we know that some data might have been sent, and that we may
812 * have to poll first. We have to do that too if the buffer is not empty.
813 */
814 if (channel_is_empty(oc)) {
815 /* the connection is established but we can't write. Either the
816 * buffer is empty, or we just refrain from sending because the
817 * ->o limit was reached. Maybe we just wrote the last
818 * chunk and need to close.
819 */
820 if (((oc->flags & (CF_SHUTW|CF_AUTO_CLOSE|CF_SHUTW_NOW)) ==
821 (CF_AUTO_CLOSE|CF_SHUTW_NOW)) &&
Willy Tarreau0adb2812022-05-27 10:02:48 +0200822 sc_state_in(sc->state, SC_SB_RDY|SC_SB_EST)) {
823 sc_shutw(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200824 goto out_wakeup;
825 }
826
827 if ((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == 0)
Willy Tarreau0adb2812022-05-27 10:02:48 +0200828 sc_ep_set(sc, SE_FL_WAIT_DATA);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200829 }
830 else {
831 /* Otherwise there are remaining data to be sent in the buffer,
832 * which means we have to poll before doing so.
833 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200834 sc_ep_clr(sc, SE_FL_WAIT_DATA);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200835 }
836
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200837 /* in case of special condition (error, shutdown, end of write...), we
838 * have to notify the task.
839 */
Christopher Faulet71c486b2023-02-09 14:14:38 +0100840 if (likely((oc->flags & CF_SHUTW) ||
841 ((oc->flags & CF_WRITE_EVENT) && sc->state < SC_ST_EST) ||
842 ((oc->flags & CF_WAKE_WRITE) &&
843 ((channel_is_empty(oc) && !oc->to_forward) ||
844 !sc_state_in(sc->state, SC_SB_EST))))) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200845 out_wakeup:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200846 if (!(sc->flags & SC_FL_DONT_WAKE))
847 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200848 }
849}
850
851/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200852 * This function performs a shutdown-read on a stream connector attached to an
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200853 * applet in a connected or init state (it does nothing for other states). It
854 * either shuts the read side or marks itself as closed. The buffer flags are
Willy Tarreaucb041662022-05-17 19:44:42 +0200855 * updated to reflect the new state. If the stream connector has SC_FL_NOHALF,
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200856 * we also forward the close to the write side. The owner task is woken up if
857 * it exists.
858 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200859static void sc_app_shutr_applet(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200860{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200861 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200862
Willy Tarreau0adb2812022-05-27 10:02:48 +0200863 BUG_ON(!sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200864
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200865 if (ic->flags & CF_SHUTR)
866 return;
Christopher Fauletb08c5252023-02-20 07:55:19 +0100867 ic->flags |= CF_SHUTR|CF_READ_EVENT;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200868
869 /* Note: on shutr, we don't call the applet */
870
Willy Tarreau0adb2812022-05-27 10:02:48 +0200871 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200872 return;
873
Willy Tarreau0adb2812022-05-27 10:02:48 +0200874 if (sc_oc(sc)->flags & CF_SHUTW) {
875 appctx_shut(__sc_appctx(sc));
876 sc->state = SC_ST_DIS;
Christopher Fauletca679922022-07-20 13:24:04 +0200877 if (sc->flags & SC_FL_ISBACK)
878 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200879 }
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100880 else if (sc_cond_forward_shutw(sc))
Willy Tarreau0adb2812022-05-27 10:02:48 +0200881 return sc_app_shutw_applet(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200882}
883
884/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200885 * This function performs a shutdown-write on a stream connector attached to an
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200886 * applet in a connected or init state (it does nothing for other states). It
887 * either shuts the write side or marks itself as closed. The buffer flags are
888 * updated to reflect the new state. It does also close everything if the SI
889 * was marked as being in error state. The owner task is woken up if it exists.
890 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200891static void sc_app_shutw_applet(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200892{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200893 struct channel *ic = sc_ic(sc);
894 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200895
Willy Tarreau0adb2812022-05-27 10:02:48 +0200896 BUG_ON(!sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200897
898 oc->flags &= ~CF_SHUTW_NOW;
899 if (oc->flags & CF_SHUTW)
900 return;
Christopher Fauletb08c5252023-02-20 07:55:19 +0100901 oc->flags |= CF_SHUTW|CF_WRITE_EVENT;
Christopher Fauletbcdcfad2023-02-20 08:36:53 +0100902 sc_set_hcto(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200903
904 /* on shutw we always wake the applet up */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200905 appctx_wakeup(__sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200906
Willy Tarreau0adb2812022-05-27 10:02:48 +0200907 switch (sc->state) {
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200908 case SC_ST_RDY:
909 case SC_ST_EST:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200910 /* we have to shut before closing, otherwise some short messages
911 * may never leave the system, especially when there are remaining
912 * unread data in the socket input buffer, or when nolinger is set.
Willy Tarreaucb041662022-05-17 19:44:42 +0200913 * However, if SC_FL_NOLINGER is explicitly set, we know there is
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200914 * no risk so we close both sides immediately.
915 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200916 if (!sc_ep_test(sc, SE_FL_ERROR) && !(sc->flags & SC_FL_NOLINGER) &&
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200917 !(ic->flags & (CF_SHUTR|CF_DONT_READ)))
918 return;
919
Willy Tarreau476c2802022-11-14 07:36:42 +0100920 __fallthrough;
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200921 case SC_ST_CON:
922 case SC_ST_CER:
923 case SC_ST_QUE:
924 case SC_ST_TAR:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200925 /* Note that none of these states may happen with applets */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200926 appctx_shut(__sc_appctx(sc));
927 sc->state = SC_ST_DIS;
Willy Tarreau476c2802022-11-14 07:36:42 +0100928 __fallthrough;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200929 default:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200930 sc->flags &= ~SC_FL_NOLINGER;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200931 ic->flags |= CF_SHUTR;
Christopher Fauletca679922022-07-20 13:24:04 +0200932 if (sc->flags & SC_FL_ISBACK)
933 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200934 }
935}
936
937/* chk_rcv function for applets */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200938static void sc_app_chk_rcv_applet(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200939{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200940 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200941
Willy Tarreau0adb2812022-05-27 10:02:48 +0200942 BUG_ON(!sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200943
Willy Tarreau0adb2812022-05-27 10:02:48 +0200944 DPRINTF(stderr, "%s: sc=%p, sc->state=%d ic->flags=%08x oc->flags=%08x\n",
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200945 __FUNCTION__,
Willy Tarreau0adb2812022-05-27 10:02:48 +0200946 sc, sc->state, ic->flags, sc_oc(sc)->flags);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200947
948 if (!ic->pipe) {
949 /* (re)start reading */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200950 appctx_wakeup(__sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200951 }
952}
953
954/* chk_snd function for applets */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200955static void sc_app_chk_snd_applet(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200956{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200957 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200958
Willy Tarreau0adb2812022-05-27 10:02:48 +0200959 BUG_ON(!sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200960
Willy Tarreau0adb2812022-05-27 10:02:48 +0200961 DPRINTF(stderr, "%s: sc=%p, sc->state=%d ic->flags=%08x oc->flags=%08x\n",
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200962 __FUNCTION__,
Willy Tarreau0adb2812022-05-27 10:02:48 +0200963 sc, sc->state, sc_ic(sc)->flags, oc->flags);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200964
Willy Tarreau0adb2812022-05-27 10:02:48 +0200965 if (unlikely(sc->state != SC_ST_EST || (oc->flags & CF_SHUTW)))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200966 return;
967
Christopher Faulet04f03e12022-06-01 17:35:34 +0200968 /* we only wake the applet up if it was waiting for some data and is ready to consume it */
969 if (!sc_ep_test(sc, SE_FL_WAIT_DATA) || sc_ep_test(sc, SE_FL_WONT_CONSUME))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200970 return;
971
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200972 if (!channel_is_empty(oc)) {
973 /* (re)start sending */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200974 appctx_wakeup(__sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200975 }
976}
Christopher Faulet13045f02022-04-01 14:23:38 +0200977
978
979/* This function is designed to be called from within the stream handler to
Willy Tarreau4596fe22022-05-17 19:07:51 +0200980 * update the input channel's expiration timer and the stream connector's
Christopher Faulet13045f02022-04-01 14:23:38 +0200981 * Rx flags based on the channel's flags. It needs to be called only once
982 * after the channel's flags have settled down, and before they are cleared,
983 * though it doesn't harm to call it as often as desired (it just slightly
984 * hurts performance). It must not be called from outside of the stream
985 * handler, as what it does will be used to compute the stream task's
986 * expiration.
987 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200988void sc_update_rx(struct stconn *sc)
Christopher Faulet13045f02022-04-01 14:23:38 +0200989{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200990 struct channel *ic = sc_ic(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +0200991
Willy Tarreau676c8db2022-05-24 16:22:24 +0200992 if (ic->flags & CF_SHUTR)
Christopher Faulet13045f02022-04-01 14:23:38 +0200993 return;
Christopher Faulet13045f02022-04-01 14:23:38 +0200994
995 /* Read not closed, update FD status and timeout for reads */
996 if (ic->flags & CF_DONT_READ)
Willy Tarreau0adb2812022-05-27 10:02:48 +0200997 sc_wont_read(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +0200998 else
Willy Tarreau0adb2812022-05-27 10:02:48 +0200999 sc_will_read(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +02001000
Willy Tarreau0adb2812022-05-27 10:02:48 +02001001 sc_chk_rcv(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +02001002}
1003
1004/* This function is designed to be called from within the stream handler to
Willy Tarreau4596fe22022-05-17 19:07:51 +02001005 * update the output channel's expiration timer and the stream connector's
Christopher Faulet13045f02022-04-01 14:23:38 +02001006 * Tx flags based on the channel's flags. It needs to be called only once
1007 * after the channel's flags have settled down, and before they are cleared,
1008 * though it doesn't harm to call it as often as desired (it just slightly
1009 * hurts performance). It must not be called from outside of the stream
1010 * handler, as what it does will be used to compute the stream task's
1011 * expiration.
1012 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001013void sc_update_tx(struct stconn *sc)
Christopher Faulet13045f02022-04-01 14:23:38 +02001014{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001015 struct channel *oc = sc_oc(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +02001016
1017 if (oc->flags & CF_SHUTW)
1018 return;
1019
1020 /* Write not closed, update FD status and timeout for writes */
1021 if (channel_is_empty(oc)) {
1022 /* stop writing */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001023 if (!sc_ep_test(sc, SE_FL_WAIT_DATA)) {
Christopher Faulet13045f02022-04-01 14:23:38 +02001024 if ((oc->flags & CF_SHUTW_NOW) == 0)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001025 sc_ep_set(sc, SE_FL_WAIT_DATA);
Christopher Faulet13045f02022-04-01 14:23:38 +02001026 }
1027 return;
1028 }
1029
Christopher Faulet15315d62023-02-20 08:23:51 +01001030 /* (re)start writing */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001031 sc_ep_clr(sc, SE_FL_WAIT_DATA);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001032}
1033
Willy Tarreau19c65a92022-05-27 08:49:24 +02001034/* This function is the equivalent to sc_update() except that it's
Christopher Faulet5e29b762022-04-04 08:58:34 +02001035 * designed to be called from outside the stream handlers, typically the lower
1036 * layers (applets, connections) after I/O completion. After updating the stream
1037 * interface and timeouts, it will try to forward what can be forwarded, then to
1038 * wake the associated task up if an important event requires special handling.
Willy Tarreau15252cd2022-05-25 16:36:21 +02001039 * It may update SE_FL_WAIT_DATA and/or SC_FL_NEED_ROOM, that the callers are
Christopher Faulet5e29b762022-04-04 08:58:34 +02001040 * encouraged to watch to take appropriate action.
Willy Tarreau19c65a92022-05-27 08:49:24 +02001041 * It should not be called from within the stream itself, sc_update()
Christopher Faulet5e29b762022-04-04 08:58:34 +02001042 * is designed for this.
1043 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001044static void sc_notify(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001045{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001046 struct channel *ic = sc_ic(sc);
1047 struct channel *oc = sc_oc(sc);
Willy Tarreaue68bc612022-05-27 11:23:05 +02001048 struct stconn *sco = sc_opposite(sc);
Willy Tarreau0adb2812022-05-27 10:02:48 +02001049 struct task *task = sc_strm_task(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001050
1051 /* process consumer side */
1052 if (channel_is_empty(oc)) {
Willy Tarreau0adb2812022-05-27 10:02:48 +02001053 struct connection *conn = sc_conn(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001054
1055 if (((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW) &&
Willy Tarreau0adb2812022-05-27 10:02:48 +02001056 (sc->state == SC_ST_EST) && (!conn || !(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS))))
1057 sc_shutw(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001058 }
1059
1060 /* indicate that we may be waiting for data from the output channel or
1061 * we're about to close and can't expect more data if SHUTW_NOW is there.
1062 */
1063 if (!(oc->flags & (CF_SHUTW|CF_SHUTW_NOW)))
Willy Tarreau0adb2812022-05-27 10:02:48 +02001064 sc_ep_set(sc, SE_FL_WAIT_DATA);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001065 else if ((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001066 sc_ep_clr(sc, SE_FL_WAIT_DATA);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001067
Christopher Faulet5e29b762022-04-04 08:58:34 +02001068 if (oc->flags & CF_DONT_READ)
Willy Tarreaue68bc612022-05-27 11:23:05 +02001069 sc_wont_read(sco);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001070 else
Willy Tarreaue68bc612022-05-27 11:23:05 +02001071 sc_will_read(sco);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001072
1073 /* Notify the other side when we've injected data into the IC that
1074 * needs to be forwarded. We can do fast-forwarding as soon as there
1075 * are output data, but we avoid doing this if some of the data are
1076 * not yet scheduled for being forwarded, because it is very likely
1077 * that it will be done again immediately afterwards once the following
Willy Tarreau15252cd2022-05-25 16:36:21 +02001078 * data are parsed (eg: HTTP chunking). We only clear SC_FL_NEED_ROOM
1079 * once we've emptied *some* of the output buffer, and not just when
1080 * there is available room, because applets are often forced to stop
1081 * before the buffer is full. We must not stop based on input data
1082 * alone because an HTTP parser might need more data to complete the
1083 * parsing.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001084 */
1085 if (!channel_is_empty(ic) &&
Willy Tarreaue68bc612022-05-27 11:23:05 +02001086 sc_ep_test(sco, SE_FL_WAIT_DATA) &&
Christopher Faulet5e29b762022-04-04 08:58:34 +02001087 (!(ic->flags & CF_EXPECT_MORE) || c_full(ic) || ci_data(ic) == 0 || ic->pipe)) {
1088 int new_len, last_len;
1089
1090 last_len = co_data(ic);
1091 if (ic->pipe)
1092 last_len += ic->pipe->data;
1093
Willy Tarreaue68bc612022-05-27 11:23:05 +02001094 sc_chk_snd(sco);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001095
1096 new_len = co_data(ic);
1097 if (ic->pipe)
1098 new_len += ic->pipe->data;
1099
1100 /* check if the consumer has freed some space either in the
1101 * buffer or in the pipe.
1102 */
1103 if (new_len < last_len)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001104 sc_have_room(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001105 }
1106
1107 if (!(ic->flags & CF_DONT_READ))
Willy Tarreau0adb2812022-05-27 10:02:48 +02001108 sc_will_read(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001109
Willy Tarreau0adb2812022-05-27 10:02:48 +02001110 sc_chk_rcv(sc);
Willy Tarreaue68bc612022-05-27 11:23:05 +02001111 sc_chk_rcv(sco);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001112
Christopher Faulet5e29b762022-04-04 08:58:34 +02001113 /* wake the task up only when needed */
Christopher Faulet285f7612022-12-12 08:28:55 +01001114 if (/* changes on the production side that must be handled:
Christopher Faulet2e56a732023-01-26 16:18:09 +01001115 * - An error on receipt: SE_FL_ERROR
Christopher Faulet285f7612022-12-12 08:28:55 +01001116 * - A read event: shutdown for reads (CF_READ_EVENT + SHUTR)
1117 * end of input (CF_READ_EVENT + CF_EOI)
1118 * data received and no fast-forwarding (CF_READ_EVENT + !to_forward)
1119 * read event while consumer side is not established (CF_READ_EVENT + sco->state != SC_ST_EST)
1120 */
1121 ((ic->flags & CF_READ_EVENT) && ((ic->flags & (CF_SHUTR|CF_EOI)) || !ic->to_forward || sco->state != SC_ST_EST)) ||
Christopher Faulet2e56a732023-01-26 16:18:09 +01001122 sc_ep_test(sc, SE_FL_ERROR) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001123
1124 /* changes on the consumption side */
Christopher Faulet2e56a732023-01-26 16:18:09 +01001125 sc_ep_test(sc, SE_FL_ERR_PENDING) ||
Christopher Fauletd8988412022-12-20 18:10:04 +01001126 ((oc->flags & CF_WRITE_EVENT) &&
1127 ((sc->state < SC_ST_EST) ||
1128 (oc->flags & CF_SHUTW) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001129 (((oc->flags & CF_WAKE_WRITE) ||
Christopher Fauletd8988412022-12-20 18:10:04 +01001130 !(oc->flags & (CF_AUTO_CLOSE|CF_SHUTW_NOW|CF_SHUTW))) &&
1131 (sco->state != SC_ST_EST ||
1132 (channel_is_empty(oc) && !oc->to_forward)))))) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001133 task_wakeup(task, TASK_WOKEN_IO);
1134 }
Christopher Faulet5e29b762022-04-04 08:58:34 +02001135
Christopher Faulet2e56a732023-01-26 16:18:09 +01001136 if (ic->flags & CF_READ_EVENT)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001137 ic->flags &= ~CF_READ_DONTWAIT;
1138}
1139
1140/*
1141 * This function propagates a null read received on a socket-based connection.
Willy Tarreaucb041662022-05-17 19:44:42 +02001142 * It updates the stream connector. If the stream connector has SC_FL_NOHALF,
Christopher Faulet5e29b762022-04-04 08:58:34 +02001143 * the close is also forwarded to the write side as an abort.
1144 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001145static void sc_conn_read0(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001146{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001147 struct channel *ic = sc_ic(sc);
1148 struct channel *oc = sc_oc(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001149
Willy Tarreau0adb2812022-05-27 10:02:48 +02001150 BUG_ON(!sc_conn(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001151
Christopher Faulet5e29b762022-04-04 08:58:34 +02001152 if (ic->flags & CF_SHUTR)
1153 return;
Christopher Fauletb08c5252023-02-20 07:55:19 +01001154 ic->flags |= CF_SHUTR|CF_READ_EVENT;
Christopher Faulet4c135682023-02-16 11:09:31 +01001155 sc_ep_report_read_activity(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001156
Willy Tarreau0adb2812022-05-27 10:02:48 +02001157 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001158 return;
1159
1160 if (oc->flags & CF_SHUTW)
1161 goto do_close;
1162
Christopher Fauleteb3f26d2023-02-08 16:18:48 +01001163 if (sc_cond_forward_shutw(sc)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001164 /* we want to immediately forward this close to the write side */
1165 /* force flag on ssl to keep stream in cache */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001166 sc_conn_shutw(sc, CO_SHW_SILENT);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001167 goto do_close;
1168 }
1169
1170 /* otherwise that's just a normal read shutdown */
1171 return;
1172
1173 do_close:
Willy Tarreauf61dd192022-05-27 09:00:19 +02001174 /* OK we completely close the socket here just as if we went through sc_shut[rw]() */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001175 sc_conn_shut(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001176
1177 oc->flags &= ~CF_SHUTW_NOW;
1178 oc->flags |= CF_SHUTW;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001179
Willy Tarreau0adb2812022-05-27 10:02:48 +02001180 sc->state = SC_ST_DIS;
Christopher Fauletca679922022-07-20 13:24:04 +02001181 if (sc->flags & SC_FL_ISBACK)
1182 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001183 return;
1184}
1185
1186/*
1187 * This is the callback which is called by the connection layer to receive data
1188 * into the buffer from the connection. It iterates over the mux layer's
1189 * rcv_buf function.
1190 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001191static int sc_conn_recv(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001192{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001193 struct connection *conn = __sc_conn(sc);
1194 struct channel *ic = sc_ic(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001195 int ret, max, cur_read = 0;
1196 int read_poll = MAX_READ_POLL_LOOPS;
1197 int flags = 0;
1198
1199 /* If not established yet, do nothing. */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001200 if (sc->state != SC_ST_EST)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001201 return 0;
1202
Willy Tarreau462b9892022-05-18 18:06:53 +02001203 /* If another call to sc_conn_recv() failed, and we subscribed to
Christopher Faulet5e29b762022-04-04 08:58:34 +02001204 * recv events already, give up now.
1205 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001206 if (sc->wait_event.events & SUB_RETRY_RECV)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001207 return 0;
1208
1209 /* maybe we were called immediately after an asynchronous shutr */
1210 if (ic->flags & CF_SHUTR)
1211 return 1;
1212
1213 /* we must wait because the mux is not installed yet */
1214 if (!conn->mux)
1215 return 0;
1216
1217 /* stop here if we reached the end of data */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001218 if (sc_ep_test(sc, SE_FL_EOS))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001219 goto end_recv;
1220
1221 /* stop immediately on errors. Note that we DON'T want to stop on
1222 * POLL_ERR, as the poller might report a write error while there
1223 * are still data available in the recv buffer. This typically
1224 * happens when we send too large a request to a backend server
1225 * which rejects it before reading it all.
1226 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001227 if (!sc_ep_test(sc, SE_FL_RCV_MORE)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001228 if (!conn_xprt_ready(conn))
1229 return 0;
Willy Tarreau0adb2812022-05-27 10:02:48 +02001230 if (sc_ep_test(sc, SE_FL_ERROR))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001231 goto end_recv;
1232 }
1233
1234 /* prepare to detect if the mux needs more room */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001235 sc_ep_clr(sc, SE_FL_WANT_ROOM);
Christopher Faulet341a5782023-02-10 17:37:11 +01001236 BUG_ON(sc_waiting_room(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001237
1238 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) && !co_data(ic) &&
1239 global.tune.idle_timer &&
1240 (unsigned short)(now_ms - ic->last_read) >= global.tune.idle_timer) {
1241 /* The buffer was empty and nothing was transferred for more
1242 * than one second. This was caused by a pause and not by
1243 * congestion. Reset any streaming mode to reduce latency.
1244 */
1245 ic->xfer_small = 0;
1246 ic->xfer_large = 0;
1247 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
1248 }
1249
1250 /* First, let's see if we may splice data across the channel without
1251 * using a buffer.
1252 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001253 if (sc_ep_test(sc, SE_FL_MAY_SPLICE) &&
Christopher Faulet5e29b762022-04-04 08:58:34 +02001254 (ic->pipe || ic->to_forward >= MIN_SPLICE_FORWARD) &&
1255 ic->flags & CF_KERN_SPLICING) {
1256 if (c_data(ic)) {
1257 /* We're embarrassed, there are already data pending in
1258 * the buffer and we don't want to have them at two
1259 * locations at a time. Let's indicate we need some
1260 * place and ask the consumer to hurry.
1261 */
1262 flags |= CO_RFL_BUF_FLUSH;
1263 goto abort_splice;
1264 }
1265
1266 if (unlikely(ic->pipe == NULL)) {
1267 if (pipes_used >= global.maxpipes || !(ic->pipe = get_pipe())) {
1268 ic->flags &= ~CF_KERN_SPLICING;
1269 goto abort_splice;
1270 }
1271 }
1272
Willy Tarreau0adb2812022-05-27 10:02:48 +02001273 ret = conn->mux->rcv_pipe(sc, ic->pipe, ic->to_forward);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001274 if (ret < 0) {
1275 /* splice not supported on this end, let's disable it */
1276 ic->flags &= ~CF_KERN_SPLICING;
1277 goto abort_splice;
1278 }
1279
1280 if (ret > 0) {
1281 if (ic->to_forward != CHN_INFINITE_FORWARD)
1282 ic->to_forward -= ret;
1283 ic->total += ret;
1284 cur_read += ret;
Christopher Faulet285f7612022-12-12 08:28:55 +01001285 ic->flags |= CF_READ_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001286 }
1287
Willy Tarreau0adb2812022-05-27 10:02:48 +02001288 if (sc_ep_test(sc, SE_FL_EOS | SE_FL_ERROR))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001289 goto end_recv;
1290
1291 if (conn->flags & CO_FL_WAIT_ROOM) {
1292 /* the pipe is full or we have read enough data that it
1293 * could soon be full. Let's stop before needing to poll.
1294 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001295 sc_need_room(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001296 goto done_recv;
1297 }
1298
1299 /* splice not possible (anymore), let's go on on standard copy */
1300 }
1301
1302 abort_splice:
1303 if (ic->pipe && unlikely(!ic->pipe->data)) {
1304 put_pipe(ic->pipe);
1305 ic->pipe = NULL;
1306 }
1307
Willy Tarreau0adb2812022-05-27 10:02:48 +02001308 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 +02001309 /* don't break splicing by reading, but still call rcv_buf()
1310 * to pass the flag.
1311 */
1312 goto done_recv;
1313 }
1314
1315 /* now we'll need a input buffer for the stream */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001316 if (!sc_alloc_ibuf(sc, &(__sc_strm(sc)->buffer_wait)))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001317 goto end_recv;
1318
1319 /* For an HTX stream, if the buffer is stuck (no output data with some
1320 * input data) and if the HTX message is fragmented or if its free space
1321 * wraps, we force an HTX deframentation. It is a way to have a
1322 * contiguous free space nad to let the mux to copy as much data as
1323 * possible.
1324 *
1325 * NOTE: A possible optim may be to let the mux decides if defrag is
1326 * required or not, depending on amount of data to be xferred.
1327 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001328 if (IS_HTX_STRM(__sc_strm(sc)) && !co_data(ic)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001329 struct htx *htx = htxbuf(&ic->buf);
1330
1331 if (htx_is_not_empty(htx) && ((htx->flags & HTX_FL_FRAGMENTED) || htx_space_wraps(htx)))
1332 htx_defrag(htx, NULL, 0);
1333 }
1334
1335 /* Instruct the mux it must subscribed for read events */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001336 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 +02001337
1338 /* Important note : if we're called with POLL_IN|POLL_HUP, it means the read polling
1339 * was enabled, which implies that the recv buffer was not full. So we have a guarantee
1340 * that if such an event is not handled above in splice, it will be handled here by
1341 * recv().
1342 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001343 while (sc_ep_test(sc, SE_FL_RCV_MORE) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001344 (!(conn->flags & CO_FL_HANDSHAKE) &&
Willy Tarreau0adb2812022-05-27 10:02:48 +02001345 (!sc_ep_test(sc, SE_FL_ERROR | SE_FL_EOS)) && !(ic->flags & CF_SHUTR))) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001346 int cur_flags = flags;
1347
1348 /* Compute transient CO_RFL_* flags */
1349 if (co_data(ic)) {
1350 cur_flags |= (CO_RFL_BUF_WET | CO_RFL_BUF_NOT_STUCK);
1351 }
1352
1353 /* <max> may be null. This is the mux responsibility to set
Willy Tarreaue68bc612022-05-27 11:23:05 +02001354 * SE_FL_RCV_MORE on the SC if more space is needed.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001355 */
1356 max = channel_recv_max(ic);
Willy Tarreau0adb2812022-05-27 10:02:48 +02001357 ret = conn->mux->rcv_buf(sc, &ic->buf, max, cur_flags);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001358
Willy Tarreau0adb2812022-05-27 10:02:48 +02001359 if (sc_ep_test(sc, SE_FL_WANT_ROOM)) {
Willy Tarreaub605c422022-05-17 17:04:55 +02001360 /* SE_FL_WANT_ROOM must not be reported if the channel's
Christopher Faulet5e29b762022-04-04 08:58:34 +02001361 * buffer is empty.
1362 */
1363 BUG_ON(c_empty(ic));
1364
Willy Tarreau0adb2812022-05-27 10:02:48 +02001365 sc_need_room(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001366 /* Add READ_PARTIAL because some data are pending but
1367 * cannot be xferred to the channel
1368 */
Christopher Faulet285f7612022-12-12 08:28:55 +01001369 ic->flags |= CF_READ_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001370 }
1371
1372 if (ret <= 0) {
1373 /* if we refrained from reading because we asked for a
1374 * flush to satisfy rcv_pipe(), we must not subscribe
1375 * and instead report that there's not enough room
1376 * here to proceed.
1377 */
1378 if (flags & CO_RFL_BUF_FLUSH)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001379 sc_need_room(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001380 break;
1381 }
1382
1383 cur_read += ret;
1384
1385 /* if we're allowed to directly forward data, we must update ->o */
1386 if (ic->to_forward && !(ic->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
1387 unsigned long fwd = ret;
1388 if (ic->to_forward != CHN_INFINITE_FORWARD) {
1389 if (fwd > ic->to_forward)
1390 fwd = ic->to_forward;
1391 ic->to_forward -= fwd;
1392 }
1393 c_adv(ic, fwd);
1394 }
1395
Christopher Faulet285f7612022-12-12 08:28:55 +01001396 ic->flags |= CF_READ_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001397 ic->total += ret;
1398
1399 /* End-of-input reached, we can leave. In this case, it is
Willy Tarreaue68bc612022-05-27 11:23:05 +02001400 * important to break the loop to not block the SC because of
Christopher Faulet5e29b762022-04-04 08:58:34 +02001401 * the channel's policies.This way, we are still able to receive
1402 * shutdowns.
1403 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001404 if (sc_ep_test(sc, SE_FL_EOI))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001405 break;
1406
1407 if ((ic->flags & CF_READ_DONTWAIT) || --read_poll <= 0) {
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 /* if too many bytes were missing from last read, it means that
1414 * it's pointless trying to read again because the system does
1415 * not have them in buffers.
1416 */
1417 if (ret < max) {
1418 /* if a streamer has read few data, it may be because we
1419 * have exhausted system buffers. It's not worth trying
1420 * again.
1421 */
1422 if (ic->flags & CF_STREAMER) {
1423 /* we're stopped by the channel's policy */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001424 sc_wont_read(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001425 break;
1426 }
1427
1428 /* if we read a large block smaller than what we requested,
1429 * it's almost certain we'll never get anything more.
1430 */
1431 if (ret >= global.tune.recv_enough) {
1432 /* we're stopped by the channel's policy */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001433 sc_wont_read(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001434 break;
1435 }
1436 }
1437
1438 /* if we are waiting for more space, don't try to read more data
1439 * right now.
1440 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001441 if (sc->flags & (SC_FL_WONT_READ|SC_FL_NEED_BUFF|SC_FL_NEED_ROOM))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001442 break;
1443 } /* while !flags */
1444
1445 done_recv:
1446 if (cur_read) {
1447 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) &&
1448 (cur_read <= ic->buf.size / 2)) {
1449 ic->xfer_large = 0;
1450 ic->xfer_small++;
1451 if (ic->xfer_small >= 3) {
1452 /* we have read less than half of the buffer in
1453 * one pass, and this happened at least 3 times.
1454 * This is definitely not a streamer.
1455 */
1456 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
1457 }
1458 else if (ic->xfer_small >= 2) {
1459 /* if the buffer has been at least half full twice,
1460 * we receive faster than we send, so at least it
1461 * is not a "fast streamer".
1462 */
1463 ic->flags &= ~CF_STREAMER_FAST;
1464 }
1465 }
1466 else if (!(ic->flags & CF_STREAMER_FAST) &&
1467 (cur_read >= ic->buf.size - global.tune.maxrewrite)) {
1468 /* we read a full buffer at once */
1469 ic->xfer_small = 0;
1470 ic->xfer_large++;
1471 if (ic->xfer_large >= 3) {
1472 /* we call this buffer a fast streamer if it manages
1473 * to be filled in one call 3 consecutive times.
1474 */
1475 ic->flags |= (CF_STREAMER | CF_STREAMER_FAST);
1476 }
1477 }
1478 else {
1479 ic->xfer_small = 0;
1480 ic->xfer_large = 0;
1481 }
1482 ic->last_read = now_ms;
Christopher Faulet4c135682023-02-16 11:09:31 +01001483 sc_ep_report_read_activity(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001484 }
1485
1486 end_recv:
1487 ret = (cur_read != 0);
1488
1489 /* Report EOI on the channel if it was reached from the mux point of
1490 * view. */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001491 if (sc_ep_test(sc, SE_FL_EOI) && !(ic->flags & CF_EOI)) {
Christopher Faulet4c135682023-02-16 11:09:31 +01001492 sc_ep_report_read_activity(sc);
Christopher Faulet285f7612022-12-12 08:28:55 +01001493 ic->flags |= (CF_EOI|CF_READ_EVENT);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001494 ret = 1;
1495 }
1496
Willy Tarreau0adb2812022-05-27 10:02:48 +02001497 if (sc_ep_test(sc, SE_FL_ERROR))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001498 ret = 1;
Willy Tarreau0adb2812022-05-27 10:02:48 +02001499 else if (sc_ep_test(sc, SE_FL_EOS)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001500 /* we received a shutdown */
Christopher Faulet5e29b762022-04-04 08:58:34 +02001501 if (ic->flags & CF_AUTO_CLOSE)
1502 channel_shutw_now(ic);
Willy Tarreau0adb2812022-05-27 10:02:48 +02001503 sc_conn_read0(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001504 ret = 1;
1505 }
Willy Tarreau0adb2812022-05-27 10:02:48 +02001506 else if (!(sc->flags & (SC_FL_WONT_READ|SC_FL_NEED_BUFF|SC_FL_NEED_ROOM)) &&
Willy Tarreau15252cd2022-05-25 16:36:21 +02001507 !(ic->flags & CF_SHUTR)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001508 /* Subscribe to receive events if we're blocking on I/O */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001509 conn->mux->subscribe(sc, SUB_RETRY_RECV, &sc->wait_event);
1510 se_have_no_more_data(sc->sedesc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001511 } else {
Willy Tarreau0adb2812022-05-27 10:02:48 +02001512 se_have_more_data(sc->sedesc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001513 ret = 1;
1514 }
1515 return ret;
1516}
1517
Willy Tarreau4596fe22022-05-17 19:07:51 +02001518/* This tries to perform a synchronous receive on the stream connector to
Christopher Faulet5e29b762022-04-04 08:58:34 +02001519 * try to collect last arrived data. In practice it's only implemented on
Willy Tarreau4596fe22022-05-17 19:07:51 +02001520 * stconns. Returns 0 if nothing was done, non-zero if new data or a
Christopher Faulet5e29b762022-04-04 08:58:34 +02001521 * shutdown were collected. This may result on some delayed receive calls
1522 * to be programmed and performed later, though it doesn't provide any
1523 * such guarantee.
1524 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001525int sc_conn_sync_recv(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001526{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001527 if (!sc_state_in(sc->state, SC_SB_RDY|SC_SB_EST))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001528 return 0;
1529
Willy Tarreau0adb2812022-05-27 10:02:48 +02001530 if (!sc_mux_ops(sc))
Willy Tarreau4596fe22022-05-17 19:07:51 +02001531 return 0; // only stconns are supported
Christopher Faulet5e29b762022-04-04 08:58:34 +02001532
Willy Tarreau0adb2812022-05-27 10:02:48 +02001533 if (sc->wait_event.events & SUB_RETRY_RECV)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001534 return 0; // already subscribed
1535
Willy Tarreau0adb2812022-05-27 10:02:48 +02001536 if (!sc_is_recv_allowed(sc))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001537 return 0; // already failed
1538
Willy Tarreau0adb2812022-05-27 10:02:48 +02001539 return sc_conn_recv(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001540}
1541
1542/*
1543 * This function is called to send buffer data to a stream socket.
1544 * It calls the mux layer's snd_buf function. It relies on the
1545 * caller to commit polling changes. The caller should check conn->flags
1546 * for errors.
1547 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001548static int sc_conn_send(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001549{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001550 struct connection *conn = __sc_conn(sc);
1551 struct stream *s = __sc_strm(sc);
1552 struct channel *oc = sc_oc(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001553 int ret;
1554 int did_send = 0;
1555
Willy Tarreau0adb2812022-05-27 10:02:48 +02001556 if (sc_ep_test(sc, SE_FL_ERROR | SE_FL_ERR_PENDING) || sc_is_conn_error(sc)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001557 /* We're probably there because the tasklet was woken up,
1558 * but process_stream() ran before, detected there were an
Willy Tarreaue68bc612022-05-27 11:23:05 +02001559 * error and put the SC back to SC_ST_TAR. There's still
Christopher Faulet5e29b762022-04-04 08:58:34 +02001560 * CO_FL_ERROR on the connection but we don't want to add
Willy Tarreaub605c422022-05-17 17:04:55 +02001561 * SE_FL_ERROR back, so give up
Christopher Faulet5e29b762022-04-04 08:58:34 +02001562 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001563 if (sc->state < SC_ST_CON)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001564 return 0;
Christopher Faulet7f6aa562022-10-17 10:21:19 +02001565 if (sc_ep_test(sc, SE_FL_EOS))
1566 sc_ep_set(sc, SE_FL_ERROR);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001567 return 1;
1568 }
1569
1570 /* We're already waiting to be able to send, give up */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001571 if (sc->wait_event.events & SUB_RETRY_SEND)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001572 return 0;
1573
1574 /* we might have been called just after an asynchronous shutw */
1575 if (oc->flags & CF_SHUTW)
1576 return 1;
1577
1578 /* we must wait because the mux is not installed yet */
1579 if (!conn->mux)
1580 return 0;
1581
1582 if (oc->pipe && conn->xprt->snd_pipe && conn->mux->snd_pipe) {
Willy Tarreau0adb2812022-05-27 10:02:48 +02001583 ret = conn->mux->snd_pipe(sc, oc->pipe);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001584 if (ret > 0)
1585 did_send = 1;
1586
1587 if (!oc->pipe->data) {
1588 put_pipe(oc->pipe);
1589 oc->pipe = NULL;
1590 }
1591
1592 if (oc->pipe)
1593 goto end;
1594 }
1595
1596 /* At this point, the pipe is empty, but we may still have data pending
1597 * in the normal buffer.
1598 */
1599 if (co_data(oc)) {
1600 /* when we're here, we already know that there is no spliced
1601 * data left, and that there are sendable buffered data.
1602 */
1603
1604 /* check if we want to inform the kernel that we're interested in
1605 * sending more data after this call. We want this if :
1606 * - we're about to close after this last send and want to merge
1607 * the ongoing FIN with the last segment.
1608 * - we know we can't send everything at once and must get back
1609 * here because of unaligned data
1610 * - there is still a finite amount of data to forward
1611 * The test is arranged so that the most common case does only 2
1612 * tests.
1613 */
1614 unsigned int send_flag = 0;
1615
1616 if ((!(oc->flags & (CF_NEVER_WAIT|CF_SEND_DONTWAIT)) &&
1617 ((oc->to_forward && oc->to_forward != CHN_INFINITE_FORWARD) ||
1618 (oc->flags & CF_EXPECT_MORE) ||
1619 (IS_HTX_STRM(s) &&
1620 (!(oc->flags & (CF_EOI|CF_SHUTR)) && htx_expect_more(htxbuf(&oc->buf)))))) ||
1621 ((oc->flags & CF_ISRESP) &&
1622 ((oc->flags & (CF_AUTO_CLOSE|CF_SHUTW_NOW)) == (CF_AUTO_CLOSE|CF_SHUTW_NOW))))
1623 send_flag |= CO_SFL_MSG_MORE;
1624
1625 if (oc->flags & CF_STREAMER)
1626 send_flag |= CO_SFL_STREAMER;
1627
1628 if (s->txn && s->txn->flags & TX_L7_RETRY && !b_data(&s->txn->l7_buffer)) {
1629 /* If we want to be able to do L7 retries, copy
1630 * the data we're about to send, so that we are able
1631 * to resend them if needed
1632 */
1633 /* Try to allocate a buffer if we had none.
1634 * If it fails, the next test will just
1635 * disable the l7 retries by setting
1636 * l7_conn_retries to 0.
1637 */
1638 if (s->txn->req.msg_state != HTTP_MSG_DONE)
1639 s->txn->flags &= ~TX_L7_RETRY;
1640 else {
1641 if (b_alloc(&s->txn->l7_buffer) == NULL)
1642 s->txn->flags &= ~TX_L7_RETRY;
1643 else {
1644 memcpy(b_orig(&s->txn->l7_buffer),
1645 b_orig(&oc->buf),
1646 b_size(&oc->buf));
1647 s->txn->l7_buffer.head = co_data(oc);
1648 b_add(&s->txn->l7_buffer, co_data(oc));
1649 }
1650
1651 }
1652 }
1653
Willy Tarreau0adb2812022-05-27 10:02:48 +02001654 ret = conn->mux->snd_buf(sc, &oc->buf, co_data(oc), send_flag);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001655 if (ret > 0) {
1656 did_send = 1;
1657 c_rew(oc, ret);
1658 c_realign_if_empty(oc);
1659
1660 if (!co_data(oc)) {
1661 /* Always clear both flags once everything has been sent, they're one-shot */
1662 oc->flags &= ~(CF_EXPECT_MORE | CF_SEND_DONTWAIT);
1663 }
1664 /* if some data remain in the buffer, it's only because the
1665 * system buffers are full, we will try next time.
1666 */
Christopher Faulet13045f02022-04-01 14:23:38 +02001667 }
1668 }
Christopher Faulet5e29b762022-04-04 08:58:34 +02001669
1670 end:
1671 if (did_send) {
Christopher Fauletd8988412022-12-20 18:10:04 +01001672 oc->flags |= CF_WRITE_EVENT | CF_WROTE_DATA;
Willy Tarreau0adb2812022-05-27 10:02:48 +02001673 if (sc->state == SC_ST_CON)
1674 sc->state = SC_ST_RDY;
Willy Tarreau0adb2812022-05-27 10:02:48 +02001675 sc_have_room(sc_opposite(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001676 }
1677
Willy Tarreau0adb2812022-05-27 10:02:48 +02001678 if (sc_ep_test(sc, SE_FL_ERROR | SE_FL_ERR_PENDING)) {
Christopher Faulet2e56a732023-01-26 16:18:09 +01001679 oc->flags |= CF_WRITE_EVENT;
Christopher Faulet7f6aa562022-10-17 10:21:19 +02001680 if (sc_ep_test(sc, SE_FL_EOS))
Christopher Faulet2e56a732023-01-26 16:18:09 +01001681 sc_ep_set(sc, SE_FL_ERROR);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001682 return 1;
1683 }
1684
Christopher Faulet59b240c2023-02-27 16:38:12 +01001685 if (channel_is_empty(oc))
1686 sc_ep_report_send_activity(sc);
1687 else {
1688 /* 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 +02001689 conn->mux->subscribe(sc, SUB_RETRY_SEND, &sc->wait_event);
Christopher Faulet59b240c2023-02-27 16:38:12 +01001690 sc_ep_report_blocked_send(sc);
1691 }
1692
Christopher Faulet5e29b762022-04-04 08:58:34 +02001693 return did_send;
1694}
1695
Christopher Fauletd8988412022-12-20 18:10:04 +01001696/* perform a synchronous send() for the stream connector. The CF_WRITE_EVENT
1697 * flag are cleared prior to the attempt, and will possibly be updated in case
1698 * of success.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001699 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001700void sc_conn_sync_send(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001701{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001702 struct channel *oc = sc_oc(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001703
Christopher Fauletd8988412022-12-20 18:10:04 +01001704 oc->flags &= ~CF_WRITE_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001705
1706 if (oc->flags & CF_SHUTW)
1707 return;
1708
1709 if (channel_is_empty(oc))
1710 return;
1711
Willy Tarreau0adb2812022-05-27 10:02:48 +02001712 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001713 return;
1714
Willy Tarreau0adb2812022-05-27 10:02:48 +02001715 if (!sc_mux_ops(sc))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001716 return;
1717
Willy Tarreau0adb2812022-05-27 10:02:48 +02001718 sc_conn_send(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001719}
1720
1721/* Called by I/O handlers after completion.. It propagates
Willy Tarreau4596fe22022-05-17 19:07:51 +02001722 * connection flags to the stream connector, updates the stream (which may or
Christopher Faulet5e29b762022-04-04 08:58:34 +02001723 * may not take this opportunity to try to forward data), then update the
Willy Tarreau4596fe22022-05-17 19:07:51 +02001724 * connection's polling based on the channels and stream connector's final
Christopher Faulet5e29b762022-04-04 08:58:34 +02001725 * states. The function always returns 0.
1726 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001727static int sc_conn_process(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001728{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001729 struct connection *conn = __sc_conn(sc);
1730 struct channel *ic = sc_ic(sc);
1731 struct channel *oc = sc_oc(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001732
1733 BUG_ON(!conn);
1734
1735 /* If we have data to send, try it now */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001736 if (!channel_is_empty(oc) && !(sc->wait_event.events & SUB_RETRY_SEND))
1737 sc_conn_send(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001738
Willy Tarreau4596fe22022-05-17 19:07:51 +02001739 /* First step, report to the stream connector what was detected at the
Christopher Faulet5e29b762022-04-04 08:58:34 +02001740 * connection layer : errors and connection establishment.
Willy Tarreaub605c422022-05-17 17:04:55 +02001741 * Only add SE_FL_ERROR if we're connected, or we're attempting to
Christopher Faulet5e29b762022-04-04 08:58:34 +02001742 * connect, we may get there because we got woken up, but only run
1743 * after process_stream() noticed there were an error, and decided
1744 * to retry to connect, the connection may still have CO_FL_ERROR,
Willy Tarreaub605c422022-05-17 17:04:55 +02001745 * and we don't want to add SE_FL_ERROR back
Christopher Faulet5e29b762022-04-04 08:58:34 +02001746 *
Willy Tarreau462b9892022-05-18 18:06:53 +02001747 * Note: This test is only required because sc_conn_process is also the SI
1748 * wake callback. Otherwise sc_conn_recv()/sc_conn_send() already take
Christopher Faulet5e29b762022-04-04 08:58:34 +02001749 * care of it.
1750 */
1751
Willy Tarreau0adb2812022-05-27 10:02:48 +02001752 if (sc->state >= SC_ST_CON) {
1753 if (sc_is_conn_error(sc))
1754 sc_ep_set(sc, SE_FL_ERROR);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001755 }
1756
1757 /* If we had early data, and the handshake ended, then
1758 * we can remove the flag, and attempt to wake the task up,
1759 * in the event there's an analyser waiting for the end of
1760 * the handshake.
1761 */
1762 if (!(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS)) &&
Willy Tarreau0adb2812022-05-27 10:02:48 +02001763 sc_ep_test(sc, SE_FL_WAIT_FOR_HS)) {
1764 sc_ep_clr(sc, SE_FL_WAIT_FOR_HS);
1765 task_wakeup(sc_strm_task(sc), TASK_WOKEN_MSG);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001766 }
1767
Willy Tarreau0adb2812022-05-27 10:02:48 +02001768 if (!sc_state_in(sc->state, SC_SB_EST|SC_SB_DIS|SC_SB_CLO) &&
Christopher Faulet5e29b762022-04-04 08:58:34 +02001769 (conn->flags & CO_FL_WAIT_XPRT) == 0) {
Christopher Fauletca679922022-07-20 13:24:04 +02001770 if (sc->flags & SC_FL_ISBACK)
1771 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Fauletb96f2aa2022-12-12 08:11:36 +01001772 oc->flags |= CF_WRITE_EVENT;
Willy Tarreau0adb2812022-05-27 10:02:48 +02001773 if (sc->state == SC_ST_CON)
1774 sc->state = SC_ST_RDY;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001775 }
1776
1777 /* Report EOS on the channel if it was reached from the mux point of
1778 * view.
1779 *
Willy Tarreau462b9892022-05-18 18:06:53 +02001780 * Note: This test is only required because sc_conn_process is also the SI
1781 * wake callback. Otherwise sc_conn_recv()/sc_conn_send() already take
Christopher Faulet5e29b762022-04-04 08:58:34 +02001782 * care of it.
1783 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001784 if (sc_ep_test(sc, SE_FL_EOS) && !(ic->flags & CF_SHUTR)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001785 /* we received a shutdown */
Christopher Faulet5e29b762022-04-04 08:58:34 +02001786 if (ic->flags & CF_AUTO_CLOSE)
1787 channel_shutw_now(ic);
Willy Tarreau0adb2812022-05-27 10:02:48 +02001788 sc_conn_read0(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001789 }
1790
1791 /* Report EOI on the channel if it was reached from the mux point of
1792 * view.
1793 *
Willy Tarreau462b9892022-05-18 18:06:53 +02001794 * Note: This test is only required because sc_conn_process is also the SI
1795 * wake callback. Otherwise sc_conn_recv()/sc_conn_send() already take
Christopher Faulet5e29b762022-04-04 08:58:34 +02001796 * care of it.
1797 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001798 if (sc_ep_test(sc, SE_FL_EOI) && !(ic->flags & CF_EOI))
Christopher Faulet285f7612022-12-12 08:28:55 +01001799 ic->flags |= (CF_EOI|CF_READ_EVENT);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001800
Willy Tarreau4596fe22022-05-17 19:07:51 +02001801 /* Second step : update the stream connector and channels, try to forward any
Christopher Faulet5e29b762022-04-04 08:58:34 +02001802 * pending data, then possibly wake the stream up based on the new
Willy Tarreau4596fe22022-05-17 19:07:51 +02001803 * stream connector status.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001804 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001805 sc_notify(sc);
1806 stream_release_buffers(__sc_strm(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001807 return 0;
1808}
1809
Willy Tarreau4596fe22022-05-17 19:07:51 +02001810/* This is the ->process() function for any stream connector's wait_event task.
1811 * It's assigned during the stream connector's initialization, for any type of
1812 * stream connector. Thus it is always safe to perform a tasklet_wakeup() on a
Willy Tarreaue68bc612022-05-27 11:23:05 +02001813 * stream connector, as the presence of the SC is checked there.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001814 */
Willy Tarreau462b9892022-05-18 18:06:53 +02001815struct task *sc_conn_io_cb(struct task *t, void *ctx, unsigned int state)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001816{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001817 struct stconn *sc = ctx;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001818 int ret = 0;
1819
Willy Tarreau0adb2812022-05-27 10:02:48 +02001820 if (!sc_conn(sc))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001821 return t;
1822
Willy Tarreau0adb2812022-05-27 10:02:48 +02001823 if (!(sc->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(sc_oc(sc)))
1824 ret = sc_conn_send(sc);
1825 if (!(sc->wait_event.events & SUB_RETRY_RECV))
1826 ret |= sc_conn_recv(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001827 if (ret != 0)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001828 sc_conn_process(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001829
Willy Tarreau0adb2812022-05-27 10:02:48 +02001830 stream_release_buffers(__sc_strm(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001831 return t;
1832}
1833
1834/* Callback to be used by applet handlers upon completion. It updates the stream
1835 * (which may or may not take this opportunity to try to forward data), then
Willy Tarreau4596fe22022-05-17 19:07:51 +02001836 * may re-enable the applet's based on the channels and stream connector's final
Christopher Faulet5e29b762022-04-04 08:58:34 +02001837 * states.
1838 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001839static int sc_applet_process(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001840{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001841 struct channel *ic = sc_ic(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001842
Willy Tarreau0adb2812022-05-27 10:02:48 +02001843 BUG_ON(!sc_appctx(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001844
1845 /* If the applet wants to write and the channel is closed, it's a
1846 * broken pipe and it must be reported.
1847 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001848 if (!sc_ep_test(sc, SE_FL_HAVE_NO_DATA) && (ic->flags & CF_SHUTR))
1849 sc_ep_set(sc, SE_FL_ERROR);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001850
1851 /* automatically mark the applet having data available if it reported
1852 * begin blocked by the channel.
1853 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001854 if ((sc->flags & (SC_FL_WONT_READ|SC_FL_NEED_BUFF|SC_FL_NEED_ROOM)) ||
1855 sc_ep_test(sc, SE_FL_APPLET_NEED_CONN))
1856 applet_have_more_data(__sc_appctx(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001857
Willy Tarreau4596fe22022-05-17 19:07:51 +02001858 /* update the stream connector, channels, and possibly wake the stream up */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001859 sc_notify(sc);
1860 stream_release_buffers(__sc_strm(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001861
Willy Tarreau19c65a92022-05-27 08:49:24 +02001862 /* sc_notify may have passed through chk_snd and released some blocking
Willy Tarreau15252cd2022-05-25 16:36:21 +02001863 * flags. Process_stream will consider those flags to wake up the
Christopher Faulet5e29b762022-04-04 08:58:34 +02001864 * appctx but in the case the task is not in runqueue we may have to
1865 * wakeup the appctx immediately.
1866 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001867 if (sc_is_recv_allowed(sc) || sc_is_send_allowed(sc))
1868 appctx_wakeup(__sc_appctx(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001869 return 0;
Christopher Faulet13045f02022-04-01 14:23:38 +02001870}
Christopher Fauletb68f77d2022-06-16 16:24:16 +02001871
1872
1873/* Prepares an endpoint upgrade. We don't now at this stage if the upgrade will
1874 * succeed or not and if the stconn will be reused by the new endpoint. Thus,
1875 * for now, only pretend the stconn is detached.
1876 */
1877void sc_conn_prepare_endp_upgrade(struct stconn *sc)
1878{
1879 BUG_ON(!sc_conn(sc) || !sc->app);
1880 sc_ep_clr(sc, SE_FL_T_MUX);
1881 sc_ep_set(sc, SE_FL_DETACHED);
1882}
1883
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001884/* Endpoint upgrade failed. Restore the stconn state. */
Christopher Fauletb68f77d2022-06-16 16:24:16 +02001885void sc_conn_abort_endp_upgrade(struct stconn *sc)
1886{
1887 sc_ep_set(sc, SE_FL_T_MUX);
1888 sc_ep_clr(sc, SE_FL_DETACHED);
1889}
1890
1891/* Commit the endpoint upgrade. If stconn is attached, it means the new endpoint
1892 * use it. So we do nothing. Otherwise, the stconn will be destroy with the
1893 * overlying stream. So, it means we must commit the detach.
1894*/
1895void sc_conn_commit_endp_upgrade(struct stconn *sc)
1896{
1897 if (!sc_ep_test(sc, SE_FL_DETACHED))
1898 return;
1899 sc_detach_endp(&sc);
1900 /* Because it was already set as detached, the sedesc must be preserved */
Willy Tarreau6a378d12022-08-11 13:56:42 +02001901 BUG_ON(!sc);
Christopher Fauletb68f77d2022-06-16 16:24:16 +02001902 BUG_ON(!sc->sedesc);
1903}