blob: d8e7f8b9cf3338ed63e2803c90f3c51c84050999 [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;
Willy Tarreauea59b022022-05-17 17:53:22 +020095 se_fl_setall(sedesc, SE_FL_NONE);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +010096}
97
Christopher Faulet9ed77422022-04-12 08:51:15 +020098/* Tries to alloc an endpoint and initialize it. Returns NULL on failure. */
Willy Tarreauea59b022022-05-17 17:53:22 +020099struct sedesc *sedesc_new()
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100100{
Willy Tarreauea59b022022-05-17 17:53:22 +0200101 struct sedesc *sedesc;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100102
Willy Tarreauea59b022022-05-17 17:53:22 +0200103 sedesc = pool_alloc(pool_head_sedesc);
104 if (unlikely(!sedesc))
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100105 return NULL;
106
Willy Tarreauea59b022022-05-17 17:53:22 +0200107 sedesc_init(sedesc);
108 return sedesc;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100109}
110
Christopher Faulet9ed77422022-04-12 08:51:15 +0200111/* Releases an endpoint. It is the caller responsibility to be sure it is safe
112 * and it is not shared with another entity
113 */
Willy Tarreauea59b022022-05-17 17:53:22 +0200114void sedesc_free(struct sedesc *sedesc)
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100115{
Willy Tarreauea59b022022-05-17 17:53:22 +0200116 pool_free(pool_head_sedesc, sedesc);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100117}
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100118
Willy Tarreau4596fe22022-05-17 19:07:51 +0200119/* Tries to allocate a new stconn and initialize its main fields. On
Christopher Faulet9ed77422022-04-12 08:51:15 +0200120 * failure, nothing is allocated and NULL is returned. It is an internal
Willy Tarreaub605c422022-05-17 17:04:55 +0200121 * function. The caller must, at least, set the SE_FL_ORPHAN or SE_FL_DETACHED
Christopher Faulet9ed77422022-04-12 08:51:15 +0200122 * flag.
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100123 */
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200124static struct stconn *sc_new(struct sedesc *sedesc)
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100125{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200126 struct stconn *sc;
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100127
Willy Tarreau0adb2812022-05-27 10:02:48 +0200128 sc = pool_alloc(pool_head_connstream);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100129
Willy Tarreau0adb2812022-05-27 10:02:48 +0200130 if (unlikely(!sc))
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100131 goto alloc_error;
Christopher Fauletbb772d02022-03-22 15:28:36 +0100132
Willy Tarreau1d2c79a2022-05-27 11:15:19 +0200133 sc->obj_type = OBJ_TYPE_SC;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200134 sc->flags = SC_FL_NONE;
135 sc->state = SC_ST_INI;
136 sc->hcto = TICK_ETERNITY;
137 sc->app = NULL;
138 sc->app_ops = NULL;
139 sc->src = NULL;
140 sc->dst = NULL;
141 sc->wait_event.tasklet = NULL;
142 sc->wait_event.events = 0;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200143
Christopher Faulet9ed77422022-04-12 08:51:15 +0200144 /* If there is no endpoint, allocate a new one now */
Willy Tarreauea59b022022-05-17 17:53:22 +0200145 if (!sedesc) {
146 sedesc = sedesc_new();
147 if (unlikely(!sedesc))
Christopher Fauletb669d682022-03-22 18:37:19 +0100148 goto alloc_error;
149 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200150 sc->sedesc = sedesc;
151 sedesc->sc = sc;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100152
Willy Tarreau0adb2812022-05-27 10:02:48 +0200153 return sc;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100154
155 alloc_error:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200156 pool_free(pool_head_connstream, sc);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100157 return NULL;
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100158}
159
Willy Tarreau31219282022-05-27 16:21:33 +0200160/* Creates a new stream connector and its associated stream from a mux. <sd> must
161 * be defined. It returns NULL on error. On success, the new stream connector is
Willy Tarreaub605c422022-05-17 17:04:55 +0200162 * returned. In this case, SE_FL_ORPHAN flag is removed.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200163 */
Willy Tarreau31219282022-05-27 16:21:33 +0200164struct stconn *sc_new_from_endp(struct sedesc *sd, struct session *sess, struct buffer *input)
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100165{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200166 struct stconn *sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100167
Willy Tarreau31219282022-05-27 16:21:33 +0200168 sc = sc_new(sd);
Willy Tarreau0adb2812022-05-27 10:02:48 +0200169 if (unlikely(!sc))
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100170 return NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200171 if (unlikely(!stream_new(sess, sc, input))) {
172 pool_free(pool_head_connstream, sc);
Christopher Faulet3ab72c62022-09-27 09:18:20 +0200173 sd->sc = NULL;
174 se_fl_set(sd, SE_FL_ORPHAN);
175 return NULL;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100176 }
Willy Tarreau31219282022-05-27 16:21:33 +0200177 se_fl_clr(sd, SE_FL_ORPHAN);
Willy Tarreau0adb2812022-05-27 10:02:48 +0200178 return sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100179}
180
Willy Tarreau4596fe22022-05-17 19:07:51 +0200181/* Creates a new stream connector from an stream. There is no endpoint here, thus it
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200182 * will be created by sc_new(). So the SE_FL_DETACHED flag is set. It returns
Willy Tarreau4596fe22022-05-17 19:07:51 +0200183 * NULL on error. On success, the new stream connector is returned.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200184 */
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200185struct stconn *sc_new_from_strm(struct stream *strm, unsigned int flags)
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100186{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200187 struct stconn *sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100188
Willy Tarreau0adb2812022-05-27 10:02:48 +0200189 sc = sc_new(NULL);
190 if (unlikely(!sc))
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100191 return NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200192 sc->flags |= flags;
193 sc_ep_set(sc, SE_FL_DETACHED);
194 sc->app = &strm->obj_type;
195 sc->app_ops = &sc_app_embedded_ops;
196 return sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100197}
198
Willy Tarreau4596fe22022-05-17 19:07:51 +0200199/* Creates a new stream connector from an health-check. There is no endpoint here,
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200200 * thus it will be created by sc_new(). So the SE_FL_DETACHED flag is set. It
Willy Tarreau4596fe22022-05-17 19:07:51 +0200201 * returns NULL on error. On success, the new stream connector is returned.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200202 */
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200203struct stconn *sc_new_from_check(struct check *check, unsigned int flags)
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100204{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200205 struct stconn *sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100206
Willy Tarreau0adb2812022-05-27 10:02:48 +0200207 sc = sc_new(NULL);
208 if (unlikely(!sc))
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100209 return NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200210 sc->flags |= flags;
211 sc_ep_set(sc, SE_FL_DETACHED);
212 sc->app = &check->obj_type;
213 sc->app_ops = &sc_app_check_ops;
214 return sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100215}
216
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200217/* Releases a stconn previously allocated by sc_new(), as well as its
Christopher Faulet9ed77422022-04-12 08:51:15 +0200218 * endpoint, if it exists. This function is called internally or on error path.
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100219 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200220void sc_free(struct stconn *sc)
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100221{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200222 sockaddr_free(&sc->src);
223 sockaddr_free(&sc->dst);
224 if (sc->sedesc) {
225 BUG_ON(!sc_ep_test(sc, SE_FL_DETACHED));
226 sedesc_free(sc->sedesc);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100227 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200228 if (sc->wait_event.tasklet)
229 tasklet_free(sc->wait_event.tasklet);
230 pool_free(pool_head_connstream, sc);
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100231}
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100232
Willy Tarreau4596fe22022-05-17 19:07:51 +0200233/* Conditionally removes a stream connector if it is detached and if there is no app
Christopher Fauleteb50c012022-04-21 14:22:53 +0200234 * layer defined. Except on error path, this one must be used. if release, the
Willy Tarreaue68bc612022-05-27 11:23:05 +0200235 * pointer on the SC is set to NULL.
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200236 */
Willy Tarreaue68bc612022-05-27 11:23:05 +0200237static void sc_free_cond(struct stconn **scp)
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200238{
Willy Tarreaue68bc612022-05-27 11:23:05 +0200239 struct stconn *sc = *scp;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200240
Willy Tarreau0adb2812022-05-27 10:02:48 +0200241 if (!sc->app && (!sc->sedesc || sc_ep_test(sc, SE_FL_DETACHED))) {
242 sc_free(sc);
Willy Tarreaue68bc612022-05-27 11:23:05 +0200243 *scp = NULL;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200244 }
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200245}
246
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100247
Willy Tarreau4596fe22022-05-17 19:07:51 +0200248/* Attaches a stconn to a mux endpoint and sets the endpoint ctx. Returns
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500249 * -1 on error and 0 on success. SE_FL_DETACHED flag is removed. This function is
Christopher Faulet9ed77422022-04-12 08:51:15 +0200250 * called from a mux when it is attached to a stream or a health-check.
251 */
Willy Tarreau31219282022-05-27 16:21:33 +0200252int sc_attach_mux(struct stconn *sc, void *sd, void *ctx)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100253{
Christopher Faulet93882042022-01-19 14:56:50 +0100254 struct connection *conn = ctx;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200255 struct sedesc *sedesc = sc->sedesc;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100256
Willy Tarreau31219282022-05-27 16:21:33 +0200257 sedesc->se = sd;
Willy Tarreau798465b2022-05-17 18:20:02 +0200258 sedesc->conn = ctx;
259 se_fl_set(sedesc, SE_FL_T_MUX);
260 se_fl_clr(sedesc, SE_FL_DETACHED);
Christopher Faulet93882042022-01-19 14:56:50 +0100261 if (!conn->ctx)
Willy Tarreau0adb2812022-05-27 10:02:48 +0200262 conn->ctx = sc;
263 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 }
Christopher Faulet070b91b2022-03-31 19:27:18 +0200287 return 0;
Christopher Faulet93882042022-01-19 14:56:50 +0100288}
289
Willy Tarreau4596fe22022-05-17 19:07:51 +0200290/* Attaches a stconn to an applet endpoint and sets the endpoint
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500291 * ctx. Returns -1 on error and 0 on success. SE_FL_DETACHED flag is
Christopher Faulet9ed77422022-04-12 08:51:15 +0200292 * removed. This function is called by a stream when a backend applet is
293 * registered.
294 */
Willy Tarreau31219282022-05-27 16:21:33 +0200295static void sc_attach_applet(struct stconn *sc, void *sd)
Christopher Faulet93882042022-01-19 14:56:50 +0100296{
Willy Tarreau31219282022-05-27 16:21:33 +0200297 sc->sedesc->se = sd;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200298 sc_ep_set(sc, SE_FL_T_APPLET);
299 sc_ep_clr(sc, SE_FL_DETACHED);
300 if (sc_strm(sc))
301 sc->app_ops = &sc_app_applet_ops;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100302}
303
Willy Tarreau4596fe22022-05-17 19:07:51 +0200304/* Attaches a stconn to a app layer and sets the relevant
Willy Tarreaub605c422022-05-17 17:04:55 +0200305 * callbacks. Returns -1 on error and 0 on success. SE_FL_ORPHAN flag is
Christopher Faulet9ed77422022-04-12 08:51:15 +0200306 * removed. This function is called by a stream when it is created to attach it
Willy Tarreau4596fe22022-05-17 19:07:51 +0200307 * on the stream connector on the client side.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200308 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200309int sc_attach_strm(struct stconn *sc, struct stream *strm)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100310{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200311 sc->app = &strm->obj_type;
312 sc_ep_clr(sc, SE_FL_ORPHAN);
313 if (sc_ep_test(sc, SE_FL_T_MUX)) {
314 sc->wait_event.tasklet = tasklet_new();
315 if (!sc->wait_event.tasklet)
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200316 return -1;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200317 sc->wait_event.tasklet->process = sc_conn_io_cb;
318 sc->wait_event.tasklet->context = sc;
319 sc->wait_event.events = 0;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200320
Willy Tarreau0adb2812022-05-27 10:02:48 +0200321 sc->app_ops = &sc_app_conn_ops;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100322 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200323 else if (sc_ep_test(sc, SE_FL_T_APPLET)) {
324 sc->app_ops = &sc_app_applet_ops;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100325 }
326 else {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200327 sc->app_ops = &sc_app_embedded_ops;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100328 }
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100329 return 0;
330}
331
Willy Tarreau4596fe22022-05-17 19:07:51 +0200332/* Detaches the stconn from the endpoint, if any. For a connecrion, if a
Christopher Faulet9ed77422022-04-12 08:51:15 +0200333 * mux owns the connection ->detach() callback is called. Otherwise, it means
Willy Tarreau4596fe22022-05-17 19:07:51 +0200334 * the stream connector owns the connection. In this case the connection is closed
Christopher Faulet9ed77422022-04-12 08:51:15 +0200335 * and released. For an applet, the appctx is released. If still allocated, the
336 * endpoint is reset and flag as detached. If the app layer is also detached,
Willy Tarreau4596fe22022-05-17 19:07:51 +0200337 * the stream connector is released.
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100338 */
Willy Tarreaue68bc612022-05-27 11:23:05 +0200339static void sc_detach_endp(struct stconn **scp)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100340{
Willy Tarreaue68bc612022-05-27 11:23:05 +0200341 struct stconn *sc = *scp;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200342
Willy Tarreau0adb2812022-05-27 10:02:48 +0200343 if (!sc)
Christopher Fauleteb50c012022-04-21 14:22:53 +0200344 return;
345
Willy Tarreau0adb2812022-05-27 10:02:48 +0200346 if (sc_ep_test(sc, SE_FL_T_MUX)) {
347 struct connection *conn = __sc_conn(sc);
348 struct sedesc *sedesc = sc->sedesc;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100349
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100350 if (conn->mux) {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200351 if (sc->wait_event.events != 0)
352 conn->mux->unsubscribe(sc, sc->wait_event.events, &sc->wait_event);
Willy Tarreau798465b2022-05-17 18:20:02 +0200353 se_fl_set(sedesc, SE_FL_ORPHAN);
Willy Tarreauc1054922022-05-18 07:43:52 +0200354 sedesc->sc = NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200355 sc->sedesc = NULL;
Willy Tarreau798465b2022-05-17 18:20:02 +0200356 conn->mux->detach(sedesc);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100357 }
358 else {
359 /* It's too early to have a mux, let's just destroy
360 * the connection
361 */
362 conn_stop_tracking(conn);
363 conn_full_close(conn);
364 if (conn->destroy_cb)
365 conn->destroy_cb(conn);
366 conn_free(conn);
367 }
368 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200369 else if (sc_ep_test(sc, SE_FL_T_APPLET)) {
370 struct appctx *appctx = __sc_appctx(sc);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100371
Willy Tarreau0adb2812022-05-27 10:02:48 +0200372 sc_ep_set(sc, SE_FL_ORPHAN);
373 sc->sedesc->sc = NULL;
374 sc->sedesc = NULL;
Willy Tarreau1c3ead42022-05-10 19:42:22 +0200375 appctx_shut(appctx);
376 appctx_free(appctx);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100377 }
378
Willy Tarreau0adb2812022-05-27 10:02:48 +0200379 if (sc->sedesc) {
Willy Tarreauda59c892022-05-27 17:03:34 +0200380 /* the SD wasn't used and can be recycled */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200381 sc->sedesc->se = NULL;
382 sc->sedesc->conn = NULL;
Willy Tarreauda59c892022-05-27 17:03:34 +0200383 sc->sedesc->flags = 0;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200384 sc_ep_set(sc, SE_FL_DETACHED);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100385 }
386
Willy Tarreaue68bc612022-05-27 11:23:05 +0200387 /* FIXME: Rest SC for now but must be reviewed. SC flags are only
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100388 * connection related for now but this will evolved
389 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200390 sc->flags &= SC_FL_ISBACK;
391 if (sc_strm(sc))
392 sc->app_ops = &sc_app_embedded_ops;
Willy Tarreau2f2318d2022-05-18 10:17:16 +0200393 else
Willy Tarreau0adb2812022-05-27 10:02:48 +0200394 sc->app_ops = NULL;
Willy Tarreaue68bc612022-05-27 11:23:05 +0200395 sc_free_cond(scp);
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100396}
397
Willy Tarreau4596fe22022-05-17 19:07:51 +0200398/* Detaches the stconn from the app layer. If there is no endpoint attached
399 * to the stconn
Christopher Faulet9ed77422022-04-12 08:51:15 +0200400 */
Willy Tarreaue68bc612022-05-27 11:23:05 +0200401static void sc_detach_app(struct stconn **scp)
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100402{
Willy Tarreaue68bc612022-05-27 11:23:05 +0200403 struct stconn *sc = *scp;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200404
Willy Tarreau0adb2812022-05-27 10:02:48 +0200405 if (!sc)
Christopher Fauleteb50c012022-04-21 14:22:53 +0200406 return;
407
Willy Tarreau0adb2812022-05-27 10:02:48 +0200408 sc->app = NULL;
409 sc->app_ops = NULL;
410 sockaddr_free(&sc->src);
411 sockaddr_free(&sc->dst);
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200412
Willy Tarreau0adb2812022-05-27 10:02:48 +0200413 if (sc->wait_event.tasklet)
414 tasklet_free(sc->wait_event.tasklet);
415 sc->wait_event.tasklet = NULL;
416 sc->wait_event.events = 0;
Willy Tarreaue68bc612022-05-27 11:23:05 +0200417 sc_free_cond(scp);
Christopher Fauleteb50c012022-04-21 14:22:53 +0200418}
419
Willy Tarreau4596fe22022-05-17 19:07:51 +0200420/* Destroy the stconn. It is detached from its endpoint and its
421 * application. After this call, the stconn must be considered as released.
Christopher Fauleteb50c012022-04-21 14:22:53 +0200422 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200423void sc_destroy(struct stconn *sc)
Christopher Fauleteb50c012022-04-21 14:22:53 +0200424{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200425 sc_detach_endp(&sc);
426 sc_detach_app(&sc);
427 BUG_ON_HOT(sc);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100428}
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100429
Willy Tarreau4596fe22022-05-17 19:07:51 +0200430/* Resets the stream connector endpoint. It happens when the app layer want to renew
Christopher Faulet9ed77422022-04-12 08:51:15 +0200431 * its endpoint. For a connection retry for instance. If a mux or an applet is
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500432 * attached, a new endpoint is created. Returns -1 on error and 0 on success.
Christopher Fauleta6c4a482022-04-28 18:25:24 +0200433 *
Willy Tarreaub605c422022-05-17 17:04:55 +0200434 * Only SE_FL_ERROR flag is removed on the endpoint. Orther flags are preserved.
Christopher Fauleta6c4a482022-04-28 18:25:24 +0200435 * It is the caller responsibility to remove other flags if needed.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200436 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200437int sc_reset_endp(struct stconn *sc)
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100438{
Willy Tarreau31219282022-05-27 16:21:33 +0200439 struct sedesc *new_sd;
Christopher Fauletb041b232022-03-24 10:27:02 +0100440
Willy Tarreau0adb2812022-05-27 10:02:48 +0200441 BUG_ON(!sc->app);
Christopher Fauleta6c4a482022-04-28 18:25:24 +0200442
Willy Tarreau0adb2812022-05-27 10:02:48 +0200443 sc_ep_clr(sc, SE_FL_ERROR);
444 if (!__sc_endp(sc)) {
Christopher Fauletb041b232022-03-24 10:27:02 +0100445 /* endpoint not attached or attached to a mux with no
446 * target. Thus the endpoint will not be release but just
Willy Tarreau0adb2812022-05-27 10:02:48 +0200447 * reset. The app is still attached, the sc will not be
Christopher Fauleteb50c012022-04-21 14:22:53 +0200448 * released.
Christopher Fauletb041b232022-03-24 10:27:02 +0100449 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200450 sc_detach_endp(&sc);
Christopher Fauletb041b232022-03-24 10:27:02 +0100451 return 0;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100452 }
Christopher Fauletb041b232022-03-24 10:27:02 +0100453
454 /* allocate the new endpoint first to be able to set error if it
455 * fails */
Willy Tarreau31219282022-05-27 16:21:33 +0200456 new_sd = sedesc_new();
457 if (!unlikely(new_sd)) {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200458 sc_ep_set(sc, SE_FL_ERROR);
Christopher Fauletb041b232022-03-24 10:27:02 +0100459 return -1;
460 }
461
Willy Tarreau0adb2812022-05-27 10:02:48 +0200462 /* The app is still attached, the sc will not be released */
463 sc_detach_endp(&sc);
Willy Tarreau6a378d12022-08-11 13:56:42 +0200464 BUG_ON(!sc);
Willy Tarreau0adb2812022-05-27 10:02:48 +0200465 BUG_ON(sc->sedesc);
Willy Tarreau31219282022-05-27 16:21:33 +0200466 sc->sedesc = new_sd;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200467 sc->sedesc->sc = sc;
468 sc_ep_set(sc, SE_FL_DETACHED);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100469 return 0;
470}
Christopher Faulet37046632022-04-01 11:36:58 +0200471
472
Willy Tarreaue68bc612022-05-27 11:23:05 +0200473/* Create an applet to handle a stream connector as a new appctx. The SC will
Christopher Faulet37046632022-04-01 11:36:58 +0200474 * wake it up every time it is solicited. The appctx must be deleted by the task
Willy Tarreau19c65a92022-05-27 08:49:24 +0200475 * handler using sc_detach_endp(), possibly from within the function itself.
Christopher Faulet37046632022-04-01 11:36:58 +0200476 * It also pre-initializes the applet's context and returns it (or NULL in case
477 * it could not be allocated).
478 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200479struct appctx *sc_applet_create(struct stconn *sc, struct applet *app)
Christopher Faulet37046632022-04-01 11:36:58 +0200480{
481 struct appctx *appctx;
482
Willy Tarreau0adb2812022-05-27 10:02:48 +0200483 DPRINTF(stderr, "registering handler %p for sc %p (was %p)\n", app, sc, sc_strm_task(sc));
Christopher Faulet37046632022-04-01 11:36:58 +0200484
Willy Tarreau0adb2812022-05-27 10:02:48 +0200485 appctx = appctx_new_here(app, sc->sedesc);
Christopher Faulet37046632022-04-01 11:36:58 +0200486 if (!appctx)
487 return NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200488 sc_attach_applet(sc, appctx);
489 appctx->t->nice = __sc_strm(sc)->task->nice;
Willy Tarreau90e8b452022-05-25 18:21:43 +0200490 applet_need_more_data(appctx);
Christopher Faulet37046632022-04-01 11:36:58 +0200491 appctx_wakeup(appctx);
Christopher Fauleta33ff7a2022-04-21 11:52:07 +0200492
Willy Tarreau0adb2812022-05-27 10:02:48 +0200493 sc->state = SC_ST_RDY;
Christopher Faulet37046632022-04-01 11:36:58 +0200494 return appctx;
495}
496
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100497/* Conditionnaly forward the close to the wirte side. It return 1 if it can be
498 * forwarded. It is the caller responsibility to forward the close to the write
499 * side. Otherwise, 0 is returned. In this case, CF_SHUTW_NOW flag may be set on
500 * the channel if we are only waiting for the outgoing data to be flushed.
501 */
502static inline int sc_cond_forward_shutw(struct stconn *sc)
503{
504 /* The close must not be forwarded */
505 if (!(sc_ic(sc)->flags & CF_SHUTR) || !(sc->flags & SC_FL_NOHALF))
506 return 0;
507
508 if (!channel_is_empty(sc_ic(sc))) {
509 /* the close to the write side cannot be forwarded now because
510 * we should flush outgoing data first. But instruct the output
511 * channel it should be done ASAP.
512 */
513 channel_shutw_now(sc_oc(sc));
514 return 0;
515 }
516
517 /* the close can be immediately forwarded to the write side */
518 return 1;
519}
520
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200521/*
Willy Tarreau4596fe22022-05-17 19:07:51 +0200522 * This function performs a shutdown-read on a detached stream connector in a
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200523 * connected or init state (it does nothing for other states). It either shuts
524 * the read side or marks itself as closed. The buffer flags are updated to
Willy Tarreaucb041662022-05-17 19:44:42 +0200525 * reflect the new state. If the stream connector has SC_FL_NOHALF, we also
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200526 * forward the close to the write side. The owner task is woken up if it exists.
527 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200528static void sc_app_shutr(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200529{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200530 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200531
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200532 if (ic->flags & CF_SHUTR)
533 return;
534 ic->flags |= CF_SHUTR;
535 ic->rex = TICK_ETERNITY;
536
Willy Tarreau0adb2812022-05-27 10:02:48 +0200537 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200538 return;
539
Willy Tarreau0adb2812022-05-27 10:02:48 +0200540 if (sc_oc(sc)->flags & CF_SHUTW) {
541 sc->state = SC_ST_DIS;
Christopher Fauletca679922022-07-20 13:24:04 +0200542 if (sc->flags & SC_FL_ISBACK)
543 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200544 }
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100545 else if (sc_cond_forward_shutw(sc))
Willy Tarreau0adb2812022-05-27 10:02:48 +0200546 return sc_app_shutw(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200547
548 /* note that if the task exists, it must unregister itself once it runs */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200549 if (!(sc->flags & SC_FL_DONT_WAKE))
550 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200551}
552
553/*
Willy Tarreau4596fe22022-05-17 19:07:51 +0200554 * This function performs a shutdown-write on a detached stream connector in a
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200555 * connected or init state (it does nothing for other states). It either shuts
556 * the write side or marks itself as closed. The buffer flags are updated to
Willy Tarreaue68bc612022-05-27 11:23:05 +0200557 * reflect the new state. It does also close everything if the SC was marked as
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200558 * being in error state. The owner task is woken up if it exists.
559 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200560static void sc_app_shutw(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200561{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200562 struct channel *ic = sc_ic(sc);
563 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200564
565 oc->flags &= ~CF_SHUTW_NOW;
566 if (oc->flags & CF_SHUTW)
567 return;
568 oc->flags |= CF_SHUTW;
569 oc->wex = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200570
Willy Tarreau0adb2812022-05-27 10:02:48 +0200571 if (tick_isset(sc->hcto)) {
572 ic->rto = sc->hcto;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200573 ic->rex = tick_add(now_ms, ic->rto);
574 }
575
Willy Tarreau0adb2812022-05-27 10:02:48 +0200576 switch (sc->state) {
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200577 case SC_ST_RDY:
578 case SC_ST_EST:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200579 /* we have to shut before closing, otherwise some short messages
580 * may never leave the system, especially when there are remaining
581 * unread data in the socket input buffer, or when nolinger is set.
Willy Tarreaucb041662022-05-17 19:44:42 +0200582 * However, if SC_FL_NOLINGER is explicitly set, we know there is
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200583 * no risk so we close both sides immediately.
584 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200585 if (!sc_ep_test(sc, SE_FL_ERROR) && !(sc->flags & SC_FL_NOLINGER) &&
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200586 !(ic->flags & (CF_SHUTR|CF_DONT_READ)))
587 return;
588
Willy Tarreau476c2802022-11-14 07:36:42 +0100589 __fallthrough;
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200590 case SC_ST_CON:
591 case SC_ST_CER:
592 case SC_ST_QUE:
593 case SC_ST_TAR:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200594 /* Note that none of these states may happen with applets */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200595 sc->state = SC_ST_DIS;
Willy Tarreau476c2802022-11-14 07:36:42 +0100596 __fallthrough;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200597 default:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200598 sc->flags &= ~SC_FL_NOLINGER;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200599 ic->flags |= CF_SHUTR;
600 ic->rex = TICK_ETERNITY;
Christopher Fauletca679922022-07-20 13:24:04 +0200601 if (sc->flags & SC_FL_ISBACK)
602 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200603 }
604
605 /* note that if the task exists, it must unregister itself once it runs */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200606 if (!(sc->flags & SC_FL_DONT_WAKE))
607 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200608}
609
610/* default chk_rcv function for scheduled tasks */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200611static void sc_app_chk_rcv(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200612{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200613 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200614
Willy Tarreau0adb2812022-05-27 10:02:48 +0200615 DPRINTF(stderr, "%s: sc=%p, sc->state=%d ic->flags=%08x oc->flags=%08x\n",
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200616 __FUNCTION__,
Willy Tarreau0adb2812022-05-27 10:02:48 +0200617 sc, sc->state, ic->flags, sc_oc(sc)->flags);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200618
619 if (ic->pipe) {
620 /* stop reading */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200621 sc_need_room(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200622 }
623 else {
624 /* (re)start reading */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200625 if (!(sc->flags & SC_FL_DONT_WAKE))
626 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200627 }
628}
629
630/* default chk_snd function for scheduled tasks */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200631static void sc_app_chk_snd(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200632{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200633 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200634
Willy Tarreau0adb2812022-05-27 10:02:48 +0200635 DPRINTF(stderr, "%s: sc=%p, sc->state=%d ic->flags=%08x oc->flags=%08x\n",
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200636 __FUNCTION__,
Willy Tarreau0adb2812022-05-27 10:02:48 +0200637 sc, sc->state, sc_ic(sc)->flags, oc->flags);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200638
Willy Tarreau0adb2812022-05-27 10:02:48 +0200639 if (unlikely(sc->state != SC_ST_EST || (oc->flags & CF_SHUTW)))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200640 return;
641
Willy Tarreau0adb2812022-05-27 10:02:48 +0200642 if (!sc_ep_test(sc, SE_FL_WAIT_DATA) || /* not waiting for data */
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200643 channel_is_empty(oc)) /* called with nothing to send ! */
644 return;
645
646 /* Otherwise there are remaining data to be sent in the buffer,
647 * so we tell the handler.
648 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200649 sc_ep_clr(sc, SE_FL_WAIT_DATA);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200650 if (!tick_isset(oc->wex))
651 oc->wex = tick_add_ifset(now_ms, oc->wto);
652
Willy Tarreau0adb2812022-05-27 10:02:48 +0200653 if (!(sc->flags & SC_FL_DONT_WAKE))
654 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200655}
656
657/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200658 * This function performs a shutdown-read on a stream connector attached to
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200659 * a connection in a connected or init state (it does nothing for other
660 * states). It either shuts the read side or marks itself as closed. The buffer
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200661 * flags are updated to reflect the new state. If the stream connector has
Willy Tarreaucb041662022-05-17 19:44:42 +0200662 * SC_FL_NOHALF, we also forward the close to the write side. If a control
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200663 * layer is defined, then it is supposed to be a socket layer and file
664 * descriptors are then shutdown or closed accordingly. The function
665 * automatically disables polling if needed.
666 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200667static void sc_app_shutr_conn(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200668{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200669 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200670
Willy Tarreau0adb2812022-05-27 10:02:48 +0200671 BUG_ON(!sc_conn(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200672
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200673 if (ic->flags & CF_SHUTR)
674 return;
675 ic->flags |= CF_SHUTR;
676 ic->rex = TICK_ETERNITY;
677
Willy Tarreau0adb2812022-05-27 10:02:48 +0200678 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200679 return;
680
Willy Tarreau0adb2812022-05-27 10:02:48 +0200681 if (sc_oc(sc)->flags & CF_SHUTW) {
682 sc_conn_shut(sc);
683 sc->state = SC_ST_DIS;
Christopher Fauletca679922022-07-20 13:24:04 +0200684 if (sc->flags & SC_FL_ISBACK)
685 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200686 }
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100687 else if (sc_cond_forward_shutw(sc))
Willy Tarreau0adb2812022-05-27 10:02:48 +0200688 return sc_app_shutw_conn(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200689}
690
691/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200692 * This function performs a shutdown-write on a stream connector attached to
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200693 * a connection in a connected or init state (it does nothing for other
694 * states). It either shuts the write side or marks itself as closed. The
695 * buffer flags are updated to reflect the new state. It does also close
Willy Tarreaue68bc612022-05-27 11:23:05 +0200696 * everything if the SC was marked as being in error state. If there is a
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200697 * data-layer shutdown, it is called.
698 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200699static void sc_app_shutw_conn(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200700{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200701 struct channel *ic = sc_ic(sc);
702 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200703
Willy Tarreau0adb2812022-05-27 10:02:48 +0200704 BUG_ON(!sc_conn(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200705
706 oc->flags &= ~CF_SHUTW_NOW;
707 if (oc->flags & CF_SHUTW)
708 return;
709 oc->flags |= CF_SHUTW;
710 oc->wex = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200711
Willy Tarreau0adb2812022-05-27 10:02:48 +0200712 if (tick_isset(sc->hcto)) {
713 ic->rto = sc->hcto;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200714 ic->rex = tick_add(now_ms, ic->rto);
715 }
716
Willy Tarreau0adb2812022-05-27 10:02:48 +0200717 switch (sc->state) {
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200718 case SC_ST_RDY:
719 case SC_ST_EST:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200720 /* we have to shut before closing, otherwise some short messages
721 * may never leave the system, especially when there are remaining
722 * unread data in the socket input buffer, or when nolinger is set.
Willy Tarreaucb041662022-05-17 19:44:42 +0200723 * However, if SC_FL_NOLINGER is explicitly set, we know there is
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200724 * no risk so we close both sides immediately.
725 */
726
Willy Tarreau0adb2812022-05-27 10:02:48 +0200727 if (sc_ep_test(sc, SE_FL_ERROR)) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200728 /* quick close, the socket is already shut anyway */
729 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200730 else if (sc->flags & SC_FL_NOLINGER) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200731 /* unclean data-layer shutdown, typically an aborted request
732 * or a forwarded shutdown from a client to a server due to
733 * option abortonclose. No need for the TLS layer to try to
734 * emit a shutdown message.
735 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200736 sc_conn_shutw(sc, CO_SHW_SILENT);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200737 }
738 else {
739 /* clean data-layer shutdown. This only happens on the
740 * frontend side, or on the backend side when forwarding
741 * a client close in TCP mode or in HTTP TUNNEL mode
742 * while option abortonclose is set. We want the TLS
743 * layer to try to signal it to the peer before we close.
744 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200745 sc_conn_shutw(sc, CO_SHW_NORMAL);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200746
747 if (!(ic->flags & (CF_SHUTR|CF_DONT_READ)))
748 return;
749 }
750
Willy Tarreau476c2802022-11-14 07:36:42 +0100751 __fallthrough;
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200752 case SC_ST_CON:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200753 /* we may have to close a pending connection, and mark the
754 * response buffer as shutr
755 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200756 sc_conn_shut(sc);
Willy Tarreau476c2802022-11-14 07:36:42 +0100757 __fallthrough;
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200758 case SC_ST_CER:
759 case SC_ST_QUE:
760 case SC_ST_TAR:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200761 sc->state = SC_ST_DIS;
Willy Tarreau476c2802022-11-14 07:36:42 +0100762 __fallthrough;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200763 default:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200764 sc->flags &= ~SC_FL_NOLINGER;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200765 ic->flags |= CF_SHUTR;
766 ic->rex = TICK_ETERNITY;
Christopher Fauletca679922022-07-20 13:24:04 +0200767 if (sc->flags & SC_FL_ISBACK)
768 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200769 }
770}
771
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200772/* This function is used for inter-stream connector calls. It is called by the
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200773 * consumer to inform the producer side that it may be interested in checking
774 * for free space in the buffer. Note that it intentionally does not update
775 * timeouts, so that we can still check them later at wake-up. This function is
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200776 * dedicated to connection-based stream connectors.
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200777 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200778static void sc_app_chk_rcv_conn(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200779{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200780 BUG_ON(!sc_conn(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200781
782 /* (re)start reading */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200783 if (sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
784 tasklet_wakeup(sc->wait_event.tasklet);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200785}
786
787
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200788/* This function is used for inter-stream connector calls. It is called by the
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200789 * producer to inform the consumer side that it may be interested in checking
790 * for data in the buffer. Note that it intentionally does not update timeouts,
791 * so that we can still check them later at wake-up.
792 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200793static void sc_app_chk_snd_conn(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200794{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200795 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200796
Willy Tarreau0adb2812022-05-27 10:02:48 +0200797 BUG_ON(!sc_conn(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200798
Willy Tarreau0adb2812022-05-27 10:02:48 +0200799 if (unlikely(!sc_state_in(sc->state, SC_SB_RDY|SC_SB_EST) ||
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200800 (oc->flags & CF_SHUTW)))
801 return;
802
803 if (unlikely(channel_is_empty(oc))) /* called with nothing to send ! */
804 return;
805
806 if (!oc->pipe && /* spliced data wants to be forwarded ASAP */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200807 !sc_ep_test(sc, SE_FL_WAIT_DATA)) /* not waiting for data */
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200808 return;
809
Willy Tarreau0adb2812022-05-27 10:02:48 +0200810 if (!(sc->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(sc_oc(sc)))
811 sc_conn_send(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200812
Willy Tarreau0adb2812022-05-27 10:02:48 +0200813 if (sc_ep_test(sc, SE_FL_ERROR | SE_FL_ERR_PENDING) || sc_is_conn_error(sc)) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200814 /* Write error on the file descriptor */
Christopher Faulet7f6aa562022-10-17 10:21:19 +0200815 if (sc->state >= SC_ST_CON && sc_ep_test(sc, SE_FL_EOS))
Willy Tarreau0adb2812022-05-27 10:02:48 +0200816 sc_ep_set(sc, SE_FL_ERROR);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200817 goto out_wakeup;
818 }
819
820 /* OK, so now we know that some data might have been sent, and that we may
821 * have to poll first. We have to do that too if the buffer is not empty.
822 */
823 if (channel_is_empty(oc)) {
824 /* the connection is established but we can't write. Either the
825 * buffer is empty, or we just refrain from sending because the
826 * ->o limit was reached. Maybe we just wrote the last
827 * chunk and need to close.
828 */
829 if (((oc->flags & (CF_SHUTW|CF_AUTO_CLOSE|CF_SHUTW_NOW)) ==
830 (CF_AUTO_CLOSE|CF_SHUTW_NOW)) &&
Willy Tarreau0adb2812022-05-27 10:02:48 +0200831 sc_state_in(sc->state, SC_SB_RDY|SC_SB_EST)) {
832 sc_shutw(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200833 goto out_wakeup;
834 }
835
836 if ((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == 0)
Willy Tarreau0adb2812022-05-27 10:02:48 +0200837 sc_ep_set(sc, SE_FL_WAIT_DATA);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200838 oc->wex = TICK_ETERNITY;
839 }
840 else {
841 /* Otherwise there are remaining data to be sent in the buffer,
842 * which means we have to poll before doing so.
843 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200844 sc_ep_clr(sc, SE_FL_WAIT_DATA);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200845 if (!tick_isset(oc->wex))
846 oc->wex = tick_add_ifset(now_ms, oc->wto);
847 }
848
Christopher Fauleta63f8f32022-12-20 18:18:00 +0100849 if (likely(oc->flags & (CF_WRITE_EVENT|CF_WRITE_ERROR))) {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200850 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200851
852 /* update timeout if we have written something */
Christopher Fauletd8988412022-12-20 18:10:04 +0100853 if ((oc->flags & (CF_SHUTW|CF_WRITE_EVENT)) == CF_WRITE_EVENT &&
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200854 !channel_is_empty(oc))
855 oc->wex = tick_add_ifset(now_ms, oc->wto);
856
Willy Tarreau0adb2812022-05-27 10:02:48 +0200857 if (tick_isset(ic->rex) && !(sc->flags & SC_FL_INDEP_STR)) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200858 /* Note: to prevent the client from expiring read timeouts
859 * during writes, we refresh it. We only do this if the
860 * interface is not configured for "independent streams",
861 * because for some applications it's better not to do this,
862 * for instance when continuously exchanging small amounts
863 * of data which can full the socket buffers long before a
864 * write timeout is detected.
865 */
866 ic->rex = tick_add_ifset(now_ms, ic->rto);
867 }
868 }
869
870 /* in case of special condition (error, shutdown, end of write...), we
871 * have to notify the task.
872 */
Christopher Faulet71c486b2023-02-09 14:14:38 +0100873 if (likely((oc->flags & CF_SHUTW) ||
874 ((oc->flags & CF_WRITE_EVENT) && sc->state < SC_ST_EST) ||
875 ((oc->flags & CF_WAKE_WRITE) &&
876 ((channel_is_empty(oc) && !oc->to_forward) ||
877 !sc_state_in(sc->state, SC_SB_EST))))) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200878 out_wakeup:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200879 if (!(sc->flags & SC_FL_DONT_WAKE))
880 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200881 }
882}
883
884/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200885 * This function performs a shutdown-read 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 read side or marks itself as closed. The buffer flags are
Willy Tarreaucb041662022-05-17 19:44:42 +0200888 * updated to reflect the new state. If the stream connector has SC_FL_NOHALF,
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200889 * we also forward the close to the write side. The owner task is woken up if
890 * it exists.
891 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200892static void sc_app_shutr_applet(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200893{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200894 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200895
Willy Tarreau0adb2812022-05-27 10:02:48 +0200896 BUG_ON(!sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200897
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200898 if (ic->flags & CF_SHUTR)
899 return;
900 ic->flags |= CF_SHUTR;
901 ic->rex = TICK_ETERNITY;
902
903 /* Note: on shutr, we don't call the applet */
904
Willy Tarreau0adb2812022-05-27 10:02:48 +0200905 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200906 return;
907
Willy Tarreau0adb2812022-05-27 10:02:48 +0200908 if (sc_oc(sc)->flags & CF_SHUTW) {
909 appctx_shut(__sc_appctx(sc));
910 sc->state = SC_ST_DIS;
Christopher Fauletca679922022-07-20 13:24:04 +0200911 if (sc->flags & SC_FL_ISBACK)
912 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200913 }
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100914 else if (sc_cond_forward_shutw(sc))
Willy Tarreau0adb2812022-05-27 10:02:48 +0200915 return sc_app_shutw_applet(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200916}
917
918/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200919 * This function performs a shutdown-write on a stream connector attached to an
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200920 * applet in a connected or init state (it does nothing for other states). It
921 * either shuts the write side or marks itself as closed. The buffer flags are
922 * updated to reflect the new state. It does also close everything if the SI
923 * was marked as being in error state. The owner task is woken up if it exists.
924 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200925static void sc_app_shutw_applet(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200926{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200927 struct channel *ic = sc_ic(sc);
928 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200929
Willy Tarreau0adb2812022-05-27 10:02:48 +0200930 BUG_ON(!sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200931
932 oc->flags &= ~CF_SHUTW_NOW;
933 if (oc->flags & CF_SHUTW)
934 return;
935 oc->flags |= CF_SHUTW;
936 oc->wex = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200937
Willy Tarreau0adb2812022-05-27 10:02:48 +0200938 if (tick_isset(sc->hcto)) {
939 ic->rto = sc->hcto;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200940 ic->rex = tick_add(now_ms, ic->rto);
941 }
942
943 /* on shutw we always wake the applet up */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200944 appctx_wakeup(__sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200945
Willy Tarreau0adb2812022-05-27 10:02:48 +0200946 switch (sc->state) {
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200947 case SC_ST_RDY:
948 case SC_ST_EST:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200949 /* we have to shut before closing, otherwise some short messages
950 * may never leave the system, especially when there are remaining
951 * unread data in the socket input buffer, or when nolinger is set.
Willy Tarreaucb041662022-05-17 19:44:42 +0200952 * However, if SC_FL_NOLINGER is explicitly set, we know there is
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200953 * no risk so we close both sides immediately.
954 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200955 if (!sc_ep_test(sc, SE_FL_ERROR) && !(sc->flags & SC_FL_NOLINGER) &&
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200956 !(ic->flags & (CF_SHUTR|CF_DONT_READ)))
957 return;
958
Willy Tarreau476c2802022-11-14 07:36:42 +0100959 __fallthrough;
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200960 case SC_ST_CON:
961 case SC_ST_CER:
962 case SC_ST_QUE:
963 case SC_ST_TAR:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200964 /* Note that none of these states may happen with applets */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200965 appctx_shut(__sc_appctx(sc));
966 sc->state = SC_ST_DIS;
Willy Tarreau476c2802022-11-14 07:36:42 +0100967 __fallthrough;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200968 default:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200969 sc->flags &= ~SC_FL_NOLINGER;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200970 ic->flags |= CF_SHUTR;
971 ic->rex = TICK_ETERNITY;
Christopher Fauletca679922022-07-20 13:24:04 +0200972 if (sc->flags & SC_FL_ISBACK)
973 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200974 }
975}
976
977/* chk_rcv function for applets */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200978static void sc_app_chk_rcv_applet(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200979{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200980 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200981
Willy Tarreau0adb2812022-05-27 10:02:48 +0200982 BUG_ON(!sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200983
Willy Tarreau0adb2812022-05-27 10:02:48 +0200984 DPRINTF(stderr, "%s: sc=%p, sc->state=%d ic->flags=%08x oc->flags=%08x\n",
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200985 __FUNCTION__,
Willy Tarreau0adb2812022-05-27 10:02:48 +0200986 sc, sc->state, ic->flags, sc_oc(sc)->flags);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200987
988 if (!ic->pipe) {
989 /* (re)start reading */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200990 appctx_wakeup(__sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200991 }
992}
993
994/* chk_snd function for applets */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200995static void sc_app_chk_snd_applet(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200996{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200997 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200998
Willy Tarreau0adb2812022-05-27 10:02:48 +0200999 BUG_ON(!sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +02001000
Willy Tarreau0adb2812022-05-27 10:02:48 +02001001 DPRINTF(stderr, "%s: sc=%p, sc->state=%d ic->flags=%08x oc->flags=%08x\n",
Christopher Faulet9ffddd52022-04-01 14:04:29 +02001002 __FUNCTION__,
Willy Tarreau0adb2812022-05-27 10:02:48 +02001003 sc, sc->state, sc_ic(sc)->flags, oc->flags);
Christopher Faulet9ffddd52022-04-01 14:04:29 +02001004
Willy Tarreau0adb2812022-05-27 10:02:48 +02001005 if (unlikely(sc->state != SC_ST_EST || (oc->flags & CF_SHUTW)))
Christopher Faulet9ffddd52022-04-01 14:04:29 +02001006 return;
1007
Christopher Faulet04f03e12022-06-01 17:35:34 +02001008 /* we only wake the applet up if it was waiting for some data and is ready to consume it */
1009 if (!sc_ep_test(sc, SE_FL_WAIT_DATA) || sc_ep_test(sc, SE_FL_WONT_CONSUME))
Christopher Faulet9ffddd52022-04-01 14:04:29 +02001010 return;
1011
1012 if (!tick_isset(oc->wex))
1013 oc->wex = tick_add_ifset(now_ms, oc->wto);
1014
1015 if (!channel_is_empty(oc)) {
1016 /* (re)start sending */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001017 appctx_wakeup(__sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +02001018 }
1019}
Christopher Faulet13045f02022-04-01 14:23:38 +02001020
1021
1022/* This function is designed to be called from within the stream handler to
Willy Tarreau4596fe22022-05-17 19:07:51 +02001023 * update the input channel's expiration timer and the stream connector's
Christopher Faulet13045f02022-04-01 14:23:38 +02001024 * Rx flags based on the channel's flags. It needs to be called only once
1025 * after the channel's flags have settled down, and before they are cleared,
1026 * though it doesn't harm to call it as often as desired (it just slightly
1027 * hurts performance). It must not be called from outside of the stream
1028 * handler, as what it does will be used to compute the stream task's
1029 * expiration.
1030 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001031void sc_update_rx(struct stconn *sc)
Christopher Faulet13045f02022-04-01 14:23:38 +02001032{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001033 struct channel *ic = sc_ic(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +02001034
Willy Tarreau676c8db2022-05-24 16:22:24 +02001035 if (ic->flags & CF_SHUTR)
Christopher Faulet13045f02022-04-01 14:23:38 +02001036 return;
Christopher Faulet13045f02022-04-01 14:23:38 +02001037
1038 /* Read not closed, update FD status and timeout for reads */
1039 if (ic->flags & CF_DONT_READ)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001040 sc_wont_read(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +02001041 else
Willy Tarreau0adb2812022-05-27 10:02:48 +02001042 sc_will_read(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +02001043
Willy Tarreau0adb2812022-05-27 10:02:48 +02001044 if (sc->flags & (SC_FL_WONT_READ|SC_FL_NEED_BUFF|SC_FL_NEED_ROOM))
Christopher Faulet13045f02022-04-01 14:23:38 +02001045 ic->rex = TICK_ETERNITY;
1046 else if (!(ic->flags & CF_READ_NOEXP) && !tick_isset(ic->rex))
1047 ic->rex = tick_add_ifset(now_ms, ic->rto);
1048
Willy Tarreau0adb2812022-05-27 10:02:48 +02001049 sc_chk_rcv(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +02001050}
1051
1052/* This function is designed to be called from within the stream handler to
Willy Tarreau4596fe22022-05-17 19:07:51 +02001053 * update the output channel's expiration timer and the stream connector's
Christopher Faulet13045f02022-04-01 14:23:38 +02001054 * Tx flags based on the channel's flags. It needs to be called only once
1055 * after the channel's flags have settled down, and before they are cleared,
1056 * though it doesn't harm to call it as often as desired (it just slightly
1057 * hurts performance). It must not be called from outside of the stream
1058 * handler, as what it does will be used to compute the stream task's
1059 * expiration.
1060 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001061void sc_update_tx(struct stconn *sc)
Christopher Faulet13045f02022-04-01 14:23:38 +02001062{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001063 struct channel *oc = sc_oc(sc);
1064 struct channel *ic = sc_ic(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +02001065
1066 if (oc->flags & CF_SHUTW)
1067 return;
1068
1069 /* Write not closed, update FD status and timeout for writes */
1070 if (channel_is_empty(oc)) {
1071 /* stop writing */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001072 if (!sc_ep_test(sc, SE_FL_WAIT_DATA)) {
Christopher Faulet13045f02022-04-01 14:23:38 +02001073 if ((oc->flags & CF_SHUTW_NOW) == 0)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001074 sc_ep_set(sc, SE_FL_WAIT_DATA);
Christopher Faulet13045f02022-04-01 14:23:38 +02001075 oc->wex = TICK_ETERNITY;
1076 }
1077 return;
1078 }
1079
1080 /* (re)start writing and update timeout. Note: we don't recompute the timeout
1081 * every time we get here, otherwise it would risk never to expire. We only
1082 * update it if is was not yet set. The stream socket handler will already
1083 * have updated it if there has been a completed I/O.
1084 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001085 sc_ep_clr(sc, SE_FL_WAIT_DATA);
Christopher Faulet13045f02022-04-01 14:23:38 +02001086 if (!tick_isset(oc->wex)) {
1087 oc->wex = tick_add_ifset(now_ms, oc->wto);
Willy Tarreau0adb2812022-05-27 10:02:48 +02001088 if (tick_isset(ic->rex) && !(sc->flags & SC_FL_INDEP_STR)) {
Christopher Faulet13045f02022-04-01 14:23:38 +02001089 /* Note: depending on the protocol, we don't know if we're waiting
1090 * for incoming data or not. So in order to prevent the socket from
1091 * expiring read timeouts during writes, we refresh the read timeout,
1092 * except if it was already infinite or if we have explicitly setup
1093 * independent streams.
1094 */
1095 ic->rex = tick_add_ifset(now_ms, ic->rto);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001096 }
1097 }
1098}
1099
Willy Tarreau19c65a92022-05-27 08:49:24 +02001100/* This function is the equivalent to sc_update() except that it's
Christopher Faulet5e29b762022-04-04 08:58:34 +02001101 * designed to be called from outside the stream handlers, typically the lower
1102 * layers (applets, connections) after I/O completion. After updating the stream
1103 * interface and timeouts, it will try to forward what can be forwarded, then to
1104 * wake the associated task up if an important event requires special handling.
Willy Tarreau15252cd2022-05-25 16:36:21 +02001105 * It may update SE_FL_WAIT_DATA and/or SC_FL_NEED_ROOM, that the callers are
Christopher Faulet5e29b762022-04-04 08:58:34 +02001106 * encouraged to watch to take appropriate action.
Willy Tarreau19c65a92022-05-27 08:49:24 +02001107 * It should not be called from within the stream itself, sc_update()
Christopher Faulet5e29b762022-04-04 08:58:34 +02001108 * is designed for this.
1109 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001110static void sc_notify(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001111{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001112 struct channel *ic = sc_ic(sc);
1113 struct channel *oc = sc_oc(sc);
Willy Tarreaue68bc612022-05-27 11:23:05 +02001114 struct stconn *sco = sc_opposite(sc);
Willy Tarreau0adb2812022-05-27 10:02:48 +02001115 struct task *task = sc_strm_task(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001116
1117 /* process consumer side */
1118 if (channel_is_empty(oc)) {
Willy Tarreau0adb2812022-05-27 10:02:48 +02001119 struct connection *conn = sc_conn(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001120
1121 if (((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW) &&
Willy Tarreau0adb2812022-05-27 10:02:48 +02001122 (sc->state == SC_ST_EST) && (!conn || !(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS))))
1123 sc_shutw(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001124 oc->wex = TICK_ETERNITY;
1125 }
1126
1127 /* indicate that we may be waiting for data from the output channel or
1128 * we're about to close and can't expect more data if SHUTW_NOW is there.
1129 */
1130 if (!(oc->flags & (CF_SHUTW|CF_SHUTW_NOW)))
Willy Tarreau0adb2812022-05-27 10:02:48 +02001131 sc_ep_set(sc, SE_FL_WAIT_DATA);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001132 else if ((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001133 sc_ep_clr(sc, SE_FL_WAIT_DATA);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001134
1135 /* update OC timeouts and wake the other side up if it's waiting for room */
Christopher Fauleta63f8f32022-12-20 18:18:00 +01001136 if (oc->flags & (CF_WRITE_EVENT|CF_WRITE_ERROR)) {
Christopher Fauletda89e9b2023-01-04 14:11:10 +01001137 if (!(oc->flags & CF_WRITE_ERROR) &&
Christopher Faulet5e29b762022-04-04 08:58:34 +02001138 !channel_is_empty(oc))
1139 if (tick_isset(oc->wex))
1140 oc->wex = tick_add_ifset(now_ms, oc->wto);
1141
Willy Tarreau0adb2812022-05-27 10:02:48 +02001142 if (!(sc->flags & SC_FL_INDEP_STR))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001143 if (tick_isset(ic->rex))
1144 ic->rex = tick_add_ifset(now_ms, ic->rto);
1145 }
1146
1147 if (oc->flags & CF_DONT_READ)
Willy Tarreaue68bc612022-05-27 11:23:05 +02001148 sc_wont_read(sco);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001149 else
Willy Tarreaue68bc612022-05-27 11:23:05 +02001150 sc_will_read(sco);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001151
1152 /* Notify the other side when we've injected data into the IC that
1153 * needs to be forwarded. We can do fast-forwarding as soon as there
1154 * are output data, but we avoid doing this if some of the data are
1155 * not yet scheduled for being forwarded, because it is very likely
1156 * that it will be done again immediately afterwards once the following
Willy Tarreau15252cd2022-05-25 16:36:21 +02001157 * data are parsed (eg: HTTP chunking). We only clear SC_FL_NEED_ROOM
1158 * once we've emptied *some* of the output buffer, and not just when
1159 * there is available room, because applets are often forced to stop
1160 * before the buffer is full. We must not stop based on input data
1161 * alone because an HTTP parser might need more data to complete the
1162 * parsing.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001163 */
1164 if (!channel_is_empty(ic) &&
Willy Tarreaue68bc612022-05-27 11:23:05 +02001165 sc_ep_test(sco, SE_FL_WAIT_DATA) &&
Christopher Faulet5e29b762022-04-04 08:58:34 +02001166 (!(ic->flags & CF_EXPECT_MORE) || c_full(ic) || ci_data(ic) == 0 || ic->pipe)) {
1167 int new_len, last_len;
1168
1169 last_len = co_data(ic);
1170 if (ic->pipe)
1171 last_len += ic->pipe->data;
1172
Willy Tarreaue68bc612022-05-27 11:23:05 +02001173 sc_chk_snd(sco);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001174
1175 new_len = co_data(ic);
1176 if (ic->pipe)
1177 new_len += ic->pipe->data;
1178
1179 /* check if the consumer has freed some space either in the
1180 * buffer or in the pipe.
1181 */
1182 if (new_len < last_len)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001183 sc_have_room(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001184 }
1185
1186 if (!(ic->flags & CF_DONT_READ))
Willy Tarreau0adb2812022-05-27 10:02:48 +02001187 sc_will_read(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001188
Willy Tarreau0adb2812022-05-27 10:02:48 +02001189 sc_chk_rcv(sc);
Willy Tarreaue68bc612022-05-27 11:23:05 +02001190 sc_chk_rcv(sco);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001191
Willy Tarreau0adb2812022-05-27 10:02:48 +02001192 if (ic->flags & CF_SHUTR || sc_ep_test(sc, SE_FL_APPLET_NEED_CONN) ||
1193 (sc->flags & (SC_FL_WONT_READ|SC_FL_NEED_BUFF|SC_FL_NEED_ROOM))) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001194 ic->rex = TICK_ETERNITY;
1195 }
Christopher Faulet285f7612022-12-12 08:28:55 +01001196 else if ((ic->flags & (CF_SHUTR|CF_READ_EVENT)) == CF_READ_EVENT) {
Willy Tarreauf61dd192022-05-27 09:00:19 +02001197 /* we must re-enable reading if sc_chk_snd() has freed some space */
Christopher Faulet5e29b762022-04-04 08:58:34 +02001198 if (!(ic->flags & CF_READ_NOEXP) && tick_isset(ic->rex))
1199 ic->rex = tick_add_ifset(now_ms, ic->rto);
1200 }
1201
1202 /* wake the task up only when needed */
Christopher Faulet285f7612022-12-12 08:28:55 +01001203 if (/* changes on the production side that must be handled:
1204 * - An error on receipt: CF_READ_ERROR or SE_FL_ERROR
1205 * - A read event: shutdown for reads (CF_READ_EVENT + SHUTR)
1206 * end of input (CF_READ_EVENT + CF_EOI)
1207 * data received and no fast-forwarding (CF_READ_EVENT + !to_forward)
1208 * read event while consumer side is not established (CF_READ_EVENT + sco->state != SC_ST_EST)
1209 */
1210 ((ic->flags & CF_READ_EVENT) && ((ic->flags & (CF_SHUTR|CF_EOI)) || !ic->to_forward || sco->state != SC_ST_EST)) ||
1211 (ic->flags & CF_READ_ERROR) || sc_ep_test(sc, SE_FL_ERROR) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001212
1213 /* changes on the consumption side */
Christopher Fauletd8988412022-12-20 18:10:04 +01001214 (oc->flags & CF_WRITE_ERROR) ||
1215 ((oc->flags & CF_WRITE_EVENT) &&
1216 ((sc->state < SC_ST_EST) ||
1217 (oc->flags & CF_SHUTW) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001218 (((oc->flags & CF_WAKE_WRITE) ||
Christopher Fauletd8988412022-12-20 18:10:04 +01001219 !(oc->flags & (CF_AUTO_CLOSE|CF_SHUTW_NOW|CF_SHUTW))) &&
1220 (sco->state != SC_ST_EST ||
1221 (channel_is_empty(oc) && !oc->to_forward)))))) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001222 task_wakeup(task, TASK_WOKEN_IO);
1223 }
1224 else {
1225 /* Update expiration date for the task and requeue it */
1226 task->expire = tick_first((tick_is_expired(task->expire, now_ms) ? 0 : task->expire),
1227 tick_first(tick_first(ic->rex, ic->wex),
1228 tick_first(oc->rex, oc->wex)));
1229
1230 task->expire = tick_first(task->expire, ic->analyse_exp);
1231 task->expire = tick_first(task->expire, oc->analyse_exp);
Willy Tarreau0adb2812022-05-27 10:02:48 +02001232 task->expire = tick_first(task->expire, __sc_strm(sc)->conn_exp);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001233
1234 task_queue(task);
1235 }
Christopher Faulet33e03ce2022-12-20 18:14:56 +01001236 if (ic->flags & (CF_READ_EVENT|CF_READ_ERROR))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001237 ic->flags &= ~CF_READ_DONTWAIT;
1238}
1239
1240/*
1241 * This function propagates a null read received on a socket-based connection.
Willy Tarreaucb041662022-05-17 19:44:42 +02001242 * It updates the stream connector. If the stream connector has SC_FL_NOHALF,
Christopher Faulet5e29b762022-04-04 08:58:34 +02001243 * the close is also forwarded to the write side as an abort.
1244 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001245static void sc_conn_read0(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001246{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001247 struct channel *ic = sc_ic(sc);
1248 struct channel *oc = sc_oc(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001249
Willy Tarreau0adb2812022-05-27 10:02:48 +02001250 BUG_ON(!sc_conn(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001251
Christopher Faulet5e29b762022-04-04 08:58:34 +02001252 if (ic->flags & CF_SHUTR)
1253 return;
1254 ic->flags |= CF_SHUTR;
1255 ic->rex = TICK_ETERNITY;
1256
Willy Tarreau0adb2812022-05-27 10:02:48 +02001257 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001258 return;
1259
1260 if (oc->flags & CF_SHUTW)
1261 goto do_close;
1262
Christopher Fauleteb3f26d2023-02-08 16:18:48 +01001263 if (sc_cond_forward_shutw(sc)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001264 /* we want to immediately forward this close to the write side */
1265 /* force flag on ssl to keep stream in cache */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001266 sc_conn_shutw(sc, CO_SHW_SILENT);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001267 goto do_close;
1268 }
1269
1270 /* otherwise that's just a normal read shutdown */
1271 return;
1272
1273 do_close:
Willy Tarreauf61dd192022-05-27 09:00:19 +02001274 /* OK we completely close the socket here just as if we went through sc_shut[rw]() */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001275 sc_conn_shut(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001276
1277 oc->flags &= ~CF_SHUTW_NOW;
1278 oc->flags |= CF_SHUTW;
1279 oc->wex = TICK_ETERNITY;
1280
Willy Tarreau0adb2812022-05-27 10:02:48 +02001281 sc->state = SC_ST_DIS;
Christopher Fauletca679922022-07-20 13:24:04 +02001282 if (sc->flags & SC_FL_ISBACK)
1283 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001284 return;
1285}
1286
1287/*
1288 * This is the callback which is called by the connection layer to receive data
1289 * into the buffer from the connection. It iterates over the mux layer's
1290 * rcv_buf function.
1291 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001292static int sc_conn_recv(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001293{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001294 struct connection *conn = __sc_conn(sc);
1295 struct channel *ic = sc_ic(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001296 int ret, max, cur_read = 0;
1297 int read_poll = MAX_READ_POLL_LOOPS;
1298 int flags = 0;
1299
1300 /* If not established yet, do nothing. */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001301 if (sc->state != SC_ST_EST)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001302 return 0;
1303
Willy Tarreau462b9892022-05-18 18:06:53 +02001304 /* If another call to sc_conn_recv() failed, and we subscribed to
Christopher Faulet5e29b762022-04-04 08:58:34 +02001305 * recv events already, give up now.
1306 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001307 if (sc->wait_event.events & SUB_RETRY_RECV)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001308 return 0;
1309
1310 /* maybe we were called immediately after an asynchronous shutr */
1311 if (ic->flags & CF_SHUTR)
1312 return 1;
1313
1314 /* we must wait because the mux is not installed yet */
1315 if (!conn->mux)
1316 return 0;
1317
1318 /* stop here if we reached the end of data */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001319 if (sc_ep_test(sc, SE_FL_EOS))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001320 goto end_recv;
1321
1322 /* stop immediately on errors. Note that we DON'T want to stop on
1323 * POLL_ERR, as the poller might report a write error while there
1324 * are still data available in the recv buffer. This typically
1325 * happens when we send too large a request to a backend server
1326 * which rejects it before reading it all.
1327 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001328 if (!sc_ep_test(sc, SE_FL_RCV_MORE)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001329 if (!conn_xprt_ready(conn))
1330 return 0;
Willy Tarreau0adb2812022-05-27 10:02:48 +02001331 if (sc_ep_test(sc, SE_FL_ERROR))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001332 goto end_recv;
1333 }
1334
1335 /* prepare to detect if the mux needs more room */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001336 sc_ep_clr(sc, SE_FL_WANT_ROOM);
Christopher Faulet341a5782023-02-10 17:37:11 +01001337 BUG_ON(sc_waiting_room(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001338
1339 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) && !co_data(ic) &&
1340 global.tune.idle_timer &&
1341 (unsigned short)(now_ms - ic->last_read) >= global.tune.idle_timer) {
1342 /* The buffer was empty and nothing was transferred for more
1343 * than one second. This was caused by a pause and not by
1344 * congestion. Reset any streaming mode to reduce latency.
1345 */
1346 ic->xfer_small = 0;
1347 ic->xfer_large = 0;
1348 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
1349 }
1350
1351 /* First, let's see if we may splice data across the channel without
1352 * using a buffer.
1353 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001354 if (sc_ep_test(sc, SE_FL_MAY_SPLICE) &&
Christopher Faulet5e29b762022-04-04 08:58:34 +02001355 (ic->pipe || ic->to_forward >= MIN_SPLICE_FORWARD) &&
1356 ic->flags & CF_KERN_SPLICING) {
1357 if (c_data(ic)) {
1358 /* We're embarrassed, there are already data pending in
1359 * the buffer and we don't want to have them at two
1360 * locations at a time. Let's indicate we need some
1361 * place and ask the consumer to hurry.
1362 */
1363 flags |= CO_RFL_BUF_FLUSH;
1364 goto abort_splice;
1365 }
1366
1367 if (unlikely(ic->pipe == NULL)) {
1368 if (pipes_used >= global.maxpipes || !(ic->pipe = get_pipe())) {
1369 ic->flags &= ~CF_KERN_SPLICING;
1370 goto abort_splice;
1371 }
1372 }
1373
Willy Tarreau0adb2812022-05-27 10:02:48 +02001374 ret = conn->mux->rcv_pipe(sc, ic->pipe, ic->to_forward);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001375 if (ret < 0) {
1376 /* splice not supported on this end, let's disable it */
1377 ic->flags &= ~CF_KERN_SPLICING;
1378 goto abort_splice;
1379 }
1380
1381 if (ret > 0) {
1382 if (ic->to_forward != CHN_INFINITE_FORWARD)
1383 ic->to_forward -= ret;
1384 ic->total += ret;
1385 cur_read += ret;
Christopher Faulet285f7612022-12-12 08:28:55 +01001386 ic->flags |= CF_READ_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001387 }
1388
Willy Tarreau0adb2812022-05-27 10:02:48 +02001389 if (sc_ep_test(sc, SE_FL_EOS | SE_FL_ERROR))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001390 goto end_recv;
1391
1392 if (conn->flags & CO_FL_WAIT_ROOM) {
1393 /* the pipe is full or we have read enough data that it
1394 * could soon be full. Let's stop before needing to poll.
1395 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001396 sc_need_room(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001397 goto done_recv;
1398 }
1399
1400 /* splice not possible (anymore), let's go on on standard copy */
1401 }
1402
1403 abort_splice:
1404 if (ic->pipe && unlikely(!ic->pipe->data)) {
1405 put_pipe(ic->pipe);
1406 ic->pipe = NULL;
1407 }
1408
Willy Tarreau0adb2812022-05-27 10:02:48 +02001409 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 +02001410 /* don't break splicing by reading, but still call rcv_buf()
1411 * to pass the flag.
1412 */
1413 goto done_recv;
1414 }
1415
1416 /* now we'll need a input buffer for the stream */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001417 if (!sc_alloc_ibuf(sc, &(__sc_strm(sc)->buffer_wait)))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001418 goto end_recv;
1419
1420 /* For an HTX stream, if the buffer is stuck (no output data with some
1421 * input data) and if the HTX message is fragmented or if its free space
1422 * wraps, we force an HTX deframentation. It is a way to have a
1423 * contiguous free space nad to let the mux to copy as much data as
1424 * possible.
1425 *
1426 * NOTE: A possible optim may be to let the mux decides if defrag is
1427 * required or not, depending on amount of data to be xferred.
1428 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001429 if (IS_HTX_STRM(__sc_strm(sc)) && !co_data(ic)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001430 struct htx *htx = htxbuf(&ic->buf);
1431
1432 if (htx_is_not_empty(htx) && ((htx->flags & HTX_FL_FRAGMENTED) || htx_space_wraps(htx)))
1433 htx_defrag(htx, NULL, 0);
1434 }
1435
1436 /* Instruct the mux it must subscribed for read events */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001437 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 +02001438
1439 /* Important note : if we're called with POLL_IN|POLL_HUP, it means the read polling
1440 * was enabled, which implies that the recv buffer was not full. So we have a guarantee
1441 * that if such an event is not handled above in splice, it will be handled here by
1442 * recv().
1443 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001444 while (sc_ep_test(sc, SE_FL_RCV_MORE) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001445 (!(conn->flags & CO_FL_HANDSHAKE) &&
Willy Tarreau0adb2812022-05-27 10:02:48 +02001446 (!sc_ep_test(sc, SE_FL_ERROR | SE_FL_EOS)) && !(ic->flags & CF_SHUTR))) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001447 int cur_flags = flags;
1448
1449 /* Compute transient CO_RFL_* flags */
1450 if (co_data(ic)) {
1451 cur_flags |= (CO_RFL_BUF_WET | CO_RFL_BUF_NOT_STUCK);
1452 }
1453
1454 /* <max> may be null. This is the mux responsibility to set
Willy Tarreaue68bc612022-05-27 11:23:05 +02001455 * SE_FL_RCV_MORE on the SC if more space is needed.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001456 */
1457 max = channel_recv_max(ic);
Willy Tarreau0adb2812022-05-27 10:02:48 +02001458 ret = conn->mux->rcv_buf(sc, &ic->buf, max, cur_flags);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001459
Willy Tarreau0adb2812022-05-27 10:02:48 +02001460 if (sc_ep_test(sc, SE_FL_WANT_ROOM)) {
Willy Tarreaub605c422022-05-17 17:04:55 +02001461 /* SE_FL_WANT_ROOM must not be reported if the channel's
Christopher Faulet5e29b762022-04-04 08:58:34 +02001462 * buffer is empty.
1463 */
1464 BUG_ON(c_empty(ic));
1465
Willy Tarreau0adb2812022-05-27 10:02:48 +02001466 sc_need_room(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001467 /* Add READ_PARTIAL because some data are pending but
1468 * cannot be xferred to the channel
1469 */
Christopher Faulet285f7612022-12-12 08:28:55 +01001470 ic->flags |= CF_READ_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001471 }
1472
1473 if (ret <= 0) {
1474 /* if we refrained from reading because we asked for a
1475 * flush to satisfy rcv_pipe(), we must not subscribe
1476 * and instead report that there's not enough room
1477 * here to proceed.
1478 */
1479 if (flags & CO_RFL_BUF_FLUSH)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001480 sc_need_room(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001481 break;
1482 }
1483
1484 cur_read += ret;
1485
1486 /* if we're allowed to directly forward data, we must update ->o */
1487 if (ic->to_forward && !(ic->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
1488 unsigned long fwd = ret;
1489 if (ic->to_forward != CHN_INFINITE_FORWARD) {
1490 if (fwd > ic->to_forward)
1491 fwd = ic->to_forward;
1492 ic->to_forward -= fwd;
1493 }
1494 c_adv(ic, fwd);
1495 }
1496
Christopher Faulet285f7612022-12-12 08:28:55 +01001497 ic->flags |= CF_READ_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001498 ic->total += ret;
1499
1500 /* End-of-input reached, we can leave. In this case, it is
Willy Tarreaue68bc612022-05-27 11:23:05 +02001501 * important to break the loop to not block the SC because of
Christopher Faulet5e29b762022-04-04 08:58:34 +02001502 * the channel's policies.This way, we are still able to receive
1503 * shutdowns.
1504 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001505 if (sc_ep_test(sc, SE_FL_EOI))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001506 break;
1507
1508 if ((ic->flags & CF_READ_DONTWAIT) || --read_poll <= 0) {
1509 /* we're stopped by the channel's policy */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001510 sc_wont_read(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001511 break;
1512 }
1513
1514 /* if too many bytes were missing from last read, it means that
1515 * it's pointless trying to read again because the system does
1516 * not have them in buffers.
1517 */
1518 if (ret < max) {
1519 /* if a streamer has read few data, it may be because we
1520 * have exhausted system buffers. It's not worth trying
1521 * again.
1522 */
1523 if (ic->flags & CF_STREAMER) {
1524 /* we're stopped by the channel's policy */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001525 sc_wont_read(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001526 break;
1527 }
1528
1529 /* if we read a large block smaller than what we requested,
1530 * it's almost certain we'll never get anything more.
1531 */
1532 if (ret >= global.tune.recv_enough) {
1533 /* we're stopped by the channel's policy */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001534 sc_wont_read(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001535 break;
1536 }
1537 }
1538
1539 /* if we are waiting for more space, don't try to read more data
1540 * right now.
1541 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001542 if (sc->flags & (SC_FL_WONT_READ|SC_FL_NEED_BUFF|SC_FL_NEED_ROOM))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001543 break;
1544 } /* while !flags */
1545
1546 done_recv:
1547 if (cur_read) {
1548 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) &&
1549 (cur_read <= ic->buf.size / 2)) {
1550 ic->xfer_large = 0;
1551 ic->xfer_small++;
1552 if (ic->xfer_small >= 3) {
1553 /* we have read less than half of the buffer in
1554 * one pass, and this happened at least 3 times.
1555 * This is definitely not a streamer.
1556 */
1557 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
1558 }
1559 else if (ic->xfer_small >= 2) {
1560 /* if the buffer has been at least half full twice,
1561 * we receive faster than we send, so at least it
1562 * is not a "fast streamer".
1563 */
1564 ic->flags &= ~CF_STREAMER_FAST;
1565 }
1566 }
1567 else if (!(ic->flags & CF_STREAMER_FAST) &&
1568 (cur_read >= ic->buf.size - global.tune.maxrewrite)) {
1569 /* we read a full buffer at once */
1570 ic->xfer_small = 0;
1571 ic->xfer_large++;
1572 if (ic->xfer_large >= 3) {
1573 /* we call this buffer a fast streamer if it manages
1574 * to be filled in one call 3 consecutive times.
1575 */
1576 ic->flags |= (CF_STREAMER | CF_STREAMER_FAST);
1577 }
1578 }
1579 else {
1580 ic->xfer_small = 0;
1581 ic->xfer_large = 0;
1582 }
1583 ic->last_read = now_ms;
1584 }
1585
1586 end_recv:
1587 ret = (cur_read != 0);
1588
1589 /* Report EOI on the channel if it was reached from the mux point of
1590 * view. */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001591 if (sc_ep_test(sc, SE_FL_EOI) && !(ic->flags & CF_EOI)) {
Christopher Faulet285f7612022-12-12 08:28:55 +01001592 ic->flags |= (CF_EOI|CF_READ_EVENT);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001593 ret = 1;
1594 }
1595
Willy Tarreau0adb2812022-05-27 10:02:48 +02001596 if (sc_ep_test(sc, SE_FL_ERROR))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001597 ret = 1;
Willy Tarreau0adb2812022-05-27 10:02:48 +02001598 else if (sc_ep_test(sc, SE_FL_EOS)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001599 /* we received a shutdown */
Christopher Faulet6e1bbc42022-12-12 08:08:15 +01001600 ic->flags |= CF_READ_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001601 if (ic->flags & CF_AUTO_CLOSE)
1602 channel_shutw_now(ic);
Willy Tarreau0adb2812022-05-27 10:02:48 +02001603 sc_conn_read0(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001604 ret = 1;
1605 }
Willy Tarreau0adb2812022-05-27 10:02:48 +02001606 else if (!(sc->flags & (SC_FL_WONT_READ|SC_FL_NEED_BUFF|SC_FL_NEED_ROOM)) &&
Willy Tarreau15252cd2022-05-25 16:36:21 +02001607 !(ic->flags & CF_SHUTR)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001608 /* Subscribe to receive events if we're blocking on I/O */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001609 conn->mux->subscribe(sc, SUB_RETRY_RECV, &sc->wait_event);
1610 se_have_no_more_data(sc->sedesc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001611 } else {
Willy Tarreau0adb2812022-05-27 10:02:48 +02001612 se_have_more_data(sc->sedesc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001613 ret = 1;
1614 }
1615 return ret;
1616}
1617
Willy Tarreau4596fe22022-05-17 19:07:51 +02001618/* This tries to perform a synchronous receive on the stream connector to
Christopher Faulet5e29b762022-04-04 08:58:34 +02001619 * try to collect last arrived data. In practice it's only implemented on
Willy Tarreau4596fe22022-05-17 19:07:51 +02001620 * stconns. Returns 0 if nothing was done, non-zero if new data or a
Christopher Faulet5e29b762022-04-04 08:58:34 +02001621 * shutdown were collected. This may result on some delayed receive calls
1622 * to be programmed and performed later, though it doesn't provide any
1623 * such guarantee.
1624 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001625int sc_conn_sync_recv(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001626{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001627 if (!sc_state_in(sc->state, SC_SB_RDY|SC_SB_EST))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001628 return 0;
1629
Willy Tarreau0adb2812022-05-27 10:02:48 +02001630 if (!sc_mux_ops(sc))
Willy Tarreau4596fe22022-05-17 19:07:51 +02001631 return 0; // only stconns are supported
Christopher Faulet5e29b762022-04-04 08:58:34 +02001632
Willy Tarreau0adb2812022-05-27 10:02:48 +02001633 if (sc->wait_event.events & SUB_RETRY_RECV)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001634 return 0; // already subscribed
1635
Willy Tarreau0adb2812022-05-27 10:02:48 +02001636 if (!sc_is_recv_allowed(sc))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001637 return 0; // already failed
1638
Willy Tarreau0adb2812022-05-27 10:02:48 +02001639 return sc_conn_recv(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001640}
1641
1642/*
1643 * This function is called to send buffer data to a stream socket.
1644 * It calls the mux layer's snd_buf function. It relies on the
1645 * caller to commit polling changes. The caller should check conn->flags
1646 * for errors.
1647 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001648static int sc_conn_send(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001649{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001650 struct connection *conn = __sc_conn(sc);
1651 struct stream *s = __sc_strm(sc);
1652 struct channel *oc = sc_oc(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001653 int ret;
1654 int did_send = 0;
1655
Willy Tarreau0adb2812022-05-27 10:02:48 +02001656 if (sc_ep_test(sc, SE_FL_ERROR | SE_FL_ERR_PENDING) || sc_is_conn_error(sc)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001657 /* We're probably there because the tasklet was woken up,
1658 * but process_stream() ran before, detected there were an
Willy Tarreaue68bc612022-05-27 11:23:05 +02001659 * error and put the SC back to SC_ST_TAR. There's still
Christopher Faulet5e29b762022-04-04 08:58:34 +02001660 * CO_FL_ERROR on the connection but we don't want to add
Willy Tarreaub605c422022-05-17 17:04:55 +02001661 * SE_FL_ERROR back, so give up
Christopher Faulet5e29b762022-04-04 08:58:34 +02001662 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001663 if (sc->state < SC_ST_CON)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001664 return 0;
Christopher Faulet7f6aa562022-10-17 10:21:19 +02001665 if (sc_ep_test(sc, SE_FL_EOS))
1666 sc_ep_set(sc, SE_FL_ERROR);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001667 return 1;
1668 }
1669
1670 /* We're already waiting to be able to send, give up */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001671 if (sc->wait_event.events & SUB_RETRY_SEND)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001672 return 0;
1673
1674 /* we might have been called just after an asynchronous shutw */
1675 if (oc->flags & CF_SHUTW)
1676 return 1;
1677
1678 /* we must wait because the mux is not installed yet */
1679 if (!conn->mux)
1680 return 0;
1681
1682 if (oc->pipe && conn->xprt->snd_pipe && conn->mux->snd_pipe) {
Willy Tarreau0adb2812022-05-27 10:02:48 +02001683 ret = conn->mux->snd_pipe(sc, oc->pipe);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001684 if (ret > 0)
1685 did_send = 1;
1686
1687 if (!oc->pipe->data) {
1688 put_pipe(oc->pipe);
1689 oc->pipe = NULL;
1690 }
1691
1692 if (oc->pipe)
1693 goto end;
1694 }
1695
1696 /* At this point, the pipe is empty, but we may still have data pending
1697 * in the normal buffer.
1698 */
1699 if (co_data(oc)) {
1700 /* when we're here, we already know that there is no spliced
1701 * data left, and that there are sendable buffered data.
1702 */
1703
1704 /* check if we want to inform the kernel that we're interested in
1705 * sending more data after this call. We want this if :
1706 * - we're about to close after this last send and want to merge
1707 * the ongoing FIN with the last segment.
1708 * - we know we can't send everything at once and must get back
1709 * here because of unaligned data
1710 * - there is still a finite amount of data to forward
1711 * The test is arranged so that the most common case does only 2
1712 * tests.
1713 */
1714 unsigned int send_flag = 0;
1715
1716 if ((!(oc->flags & (CF_NEVER_WAIT|CF_SEND_DONTWAIT)) &&
1717 ((oc->to_forward && oc->to_forward != CHN_INFINITE_FORWARD) ||
1718 (oc->flags & CF_EXPECT_MORE) ||
1719 (IS_HTX_STRM(s) &&
1720 (!(oc->flags & (CF_EOI|CF_SHUTR)) && htx_expect_more(htxbuf(&oc->buf)))))) ||
1721 ((oc->flags & CF_ISRESP) &&
1722 ((oc->flags & (CF_AUTO_CLOSE|CF_SHUTW_NOW)) == (CF_AUTO_CLOSE|CF_SHUTW_NOW))))
1723 send_flag |= CO_SFL_MSG_MORE;
1724
1725 if (oc->flags & CF_STREAMER)
1726 send_flag |= CO_SFL_STREAMER;
1727
1728 if (s->txn && s->txn->flags & TX_L7_RETRY && !b_data(&s->txn->l7_buffer)) {
1729 /* If we want to be able to do L7 retries, copy
1730 * the data we're about to send, so that we are able
1731 * to resend them if needed
1732 */
1733 /* Try to allocate a buffer if we had none.
1734 * If it fails, the next test will just
1735 * disable the l7 retries by setting
1736 * l7_conn_retries to 0.
1737 */
1738 if (s->txn->req.msg_state != HTTP_MSG_DONE)
1739 s->txn->flags &= ~TX_L7_RETRY;
1740 else {
1741 if (b_alloc(&s->txn->l7_buffer) == NULL)
1742 s->txn->flags &= ~TX_L7_RETRY;
1743 else {
1744 memcpy(b_orig(&s->txn->l7_buffer),
1745 b_orig(&oc->buf),
1746 b_size(&oc->buf));
1747 s->txn->l7_buffer.head = co_data(oc);
1748 b_add(&s->txn->l7_buffer, co_data(oc));
1749 }
1750
1751 }
1752 }
1753
Willy Tarreau0adb2812022-05-27 10:02:48 +02001754 ret = conn->mux->snd_buf(sc, &oc->buf, co_data(oc), send_flag);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001755 if (ret > 0) {
1756 did_send = 1;
1757 c_rew(oc, ret);
1758 c_realign_if_empty(oc);
1759
1760 if (!co_data(oc)) {
1761 /* Always clear both flags once everything has been sent, they're one-shot */
1762 oc->flags &= ~(CF_EXPECT_MORE | CF_SEND_DONTWAIT);
1763 }
1764 /* if some data remain in the buffer, it's only because the
1765 * system buffers are full, we will try next time.
1766 */
Christopher Faulet13045f02022-04-01 14:23:38 +02001767 }
1768 }
Christopher Faulet5e29b762022-04-04 08:58:34 +02001769
1770 end:
1771 if (did_send) {
Christopher Fauletd8988412022-12-20 18:10:04 +01001772 oc->flags |= CF_WRITE_EVENT | CF_WROTE_DATA;
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
Willy Tarreau0adb2812022-05-27 10:02:48 +02001776 sc_have_room(sc_opposite(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001777 }
1778
Willy Tarreau0adb2812022-05-27 10:02:48 +02001779 if (sc_ep_test(sc, SE_FL_ERROR | SE_FL_ERR_PENDING)) {
Christopher Faulet7f6aa562022-10-17 10:21:19 +02001780 if (sc_ep_test(sc, SE_FL_EOS))
1781 sc_ep_set(sc, SE_FL_ERROR);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001782 return 1;
1783 }
1784
1785 /* We couldn't send all of our data, let the mux know we'd like to send more */
1786 if (!channel_is_empty(oc))
Willy Tarreau0adb2812022-05-27 10:02:48 +02001787 conn->mux->subscribe(sc, SUB_RETRY_SEND, &sc->wait_event);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001788 return did_send;
1789}
1790
Christopher Fauletd8988412022-12-20 18:10:04 +01001791/* perform a synchronous send() for the stream connector. The CF_WRITE_EVENT
1792 * flag are cleared prior to the attempt, and will possibly be updated in case
1793 * of success.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001794 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001795void sc_conn_sync_send(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001796{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001797 struct channel *oc = sc_oc(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001798
Christopher Fauletd8988412022-12-20 18:10:04 +01001799 oc->flags &= ~CF_WRITE_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001800
1801 if (oc->flags & CF_SHUTW)
1802 return;
1803
1804 if (channel_is_empty(oc))
1805 return;
1806
Willy Tarreau0adb2812022-05-27 10:02:48 +02001807 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001808 return;
1809
Willy Tarreau0adb2812022-05-27 10:02:48 +02001810 if (!sc_mux_ops(sc))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001811 return;
1812
Willy Tarreau0adb2812022-05-27 10:02:48 +02001813 sc_conn_send(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001814}
1815
1816/* Called by I/O handlers after completion.. It propagates
Willy Tarreau4596fe22022-05-17 19:07:51 +02001817 * connection flags to the stream connector, updates the stream (which may or
Christopher Faulet5e29b762022-04-04 08:58:34 +02001818 * may not take this opportunity to try to forward data), then update the
Willy Tarreau4596fe22022-05-17 19:07:51 +02001819 * connection's polling based on the channels and stream connector's final
Christopher Faulet5e29b762022-04-04 08:58:34 +02001820 * states. The function always returns 0.
1821 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001822static int sc_conn_process(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001823{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001824 struct connection *conn = __sc_conn(sc);
1825 struct channel *ic = sc_ic(sc);
1826 struct channel *oc = sc_oc(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001827
1828 BUG_ON(!conn);
1829
1830 /* If we have data to send, try it now */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001831 if (!channel_is_empty(oc) && !(sc->wait_event.events & SUB_RETRY_SEND))
1832 sc_conn_send(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001833
Willy Tarreau4596fe22022-05-17 19:07:51 +02001834 /* First step, report to the stream connector what was detected at the
Christopher Faulet5e29b762022-04-04 08:58:34 +02001835 * connection layer : errors and connection establishment.
Willy Tarreaub605c422022-05-17 17:04:55 +02001836 * Only add SE_FL_ERROR if we're connected, or we're attempting to
Christopher Faulet5e29b762022-04-04 08:58:34 +02001837 * connect, we may get there because we got woken up, but only run
1838 * after process_stream() noticed there were an error, and decided
1839 * to retry to connect, the connection may still have CO_FL_ERROR,
Willy Tarreaub605c422022-05-17 17:04:55 +02001840 * and we don't want to add SE_FL_ERROR back
Christopher Faulet5e29b762022-04-04 08:58:34 +02001841 *
Willy Tarreau462b9892022-05-18 18:06:53 +02001842 * Note: This test is only required because sc_conn_process is also the SI
1843 * wake callback. Otherwise sc_conn_recv()/sc_conn_send() already take
Christopher Faulet5e29b762022-04-04 08:58:34 +02001844 * care of it.
1845 */
1846
Willy Tarreau0adb2812022-05-27 10:02:48 +02001847 if (sc->state >= SC_ST_CON) {
1848 if (sc_is_conn_error(sc))
1849 sc_ep_set(sc, SE_FL_ERROR);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001850 }
1851
1852 /* If we had early data, and the handshake ended, then
1853 * we can remove the flag, and attempt to wake the task up,
1854 * in the event there's an analyser waiting for the end of
1855 * the handshake.
1856 */
1857 if (!(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS)) &&
Willy Tarreau0adb2812022-05-27 10:02:48 +02001858 sc_ep_test(sc, SE_FL_WAIT_FOR_HS)) {
1859 sc_ep_clr(sc, SE_FL_WAIT_FOR_HS);
1860 task_wakeup(sc_strm_task(sc), TASK_WOKEN_MSG);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001861 }
1862
Willy Tarreau0adb2812022-05-27 10:02:48 +02001863 if (!sc_state_in(sc->state, SC_SB_EST|SC_SB_DIS|SC_SB_CLO) &&
Christopher Faulet5e29b762022-04-04 08:58:34 +02001864 (conn->flags & CO_FL_WAIT_XPRT) == 0) {
Christopher Fauletca679922022-07-20 13:24:04 +02001865 if (sc->flags & SC_FL_ISBACK)
1866 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Fauletb96f2aa2022-12-12 08:11:36 +01001867 oc->flags |= CF_WRITE_EVENT;
Willy Tarreau0adb2812022-05-27 10:02:48 +02001868 if (sc->state == SC_ST_CON)
1869 sc->state = SC_ST_RDY;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001870 }
1871
1872 /* Report EOS on the channel if it was reached from the mux point of
1873 * view.
1874 *
Willy Tarreau462b9892022-05-18 18:06:53 +02001875 * Note: This test is only required because sc_conn_process is also the SI
1876 * wake callback. Otherwise sc_conn_recv()/sc_conn_send() already take
Christopher Faulet5e29b762022-04-04 08:58:34 +02001877 * care of it.
1878 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001879 if (sc_ep_test(sc, SE_FL_EOS) && !(ic->flags & CF_SHUTR)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001880 /* we received a shutdown */
Christopher Faulet6e1bbc42022-12-12 08:08:15 +01001881 ic->flags |= CF_READ_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001882 if (ic->flags & CF_AUTO_CLOSE)
1883 channel_shutw_now(ic);
Willy Tarreau0adb2812022-05-27 10:02:48 +02001884 sc_conn_read0(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001885 }
1886
1887 /* Report EOI on the channel if it was reached from the mux point of
1888 * view.
1889 *
Willy Tarreau462b9892022-05-18 18:06:53 +02001890 * Note: This test is only required because sc_conn_process is also the SI
1891 * wake callback. Otherwise sc_conn_recv()/sc_conn_send() already take
Christopher Faulet5e29b762022-04-04 08:58:34 +02001892 * care of it.
1893 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001894 if (sc_ep_test(sc, SE_FL_EOI) && !(ic->flags & CF_EOI))
Christopher Faulet285f7612022-12-12 08:28:55 +01001895 ic->flags |= (CF_EOI|CF_READ_EVENT);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001896
Willy Tarreau4596fe22022-05-17 19:07:51 +02001897 /* Second step : update the stream connector and channels, try to forward any
Christopher Faulet5e29b762022-04-04 08:58:34 +02001898 * pending data, then possibly wake the stream up based on the new
Willy Tarreau4596fe22022-05-17 19:07:51 +02001899 * stream connector status.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001900 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001901 sc_notify(sc);
1902 stream_release_buffers(__sc_strm(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001903 return 0;
1904}
1905
Willy Tarreau4596fe22022-05-17 19:07:51 +02001906/* This is the ->process() function for any stream connector's wait_event task.
1907 * It's assigned during the stream connector's initialization, for any type of
1908 * stream connector. Thus it is always safe to perform a tasklet_wakeup() on a
Willy Tarreaue68bc612022-05-27 11:23:05 +02001909 * stream connector, as the presence of the SC is checked there.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001910 */
Willy Tarreau462b9892022-05-18 18:06:53 +02001911struct task *sc_conn_io_cb(struct task *t, void *ctx, unsigned int state)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001912{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001913 struct stconn *sc = ctx;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001914 int ret = 0;
1915
Willy Tarreau0adb2812022-05-27 10:02:48 +02001916 if (!sc_conn(sc))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001917 return t;
1918
Willy Tarreau0adb2812022-05-27 10:02:48 +02001919 if (!(sc->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(sc_oc(sc)))
1920 ret = sc_conn_send(sc);
1921 if (!(sc->wait_event.events & SUB_RETRY_RECV))
1922 ret |= sc_conn_recv(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001923 if (ret != 0)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001924 sc_conn_process(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001925
Willy Tarreau0adb2812022-05-27 10:02:48 +02001926 stream_release_buffers(__sc_strm(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001927 return t;
1928}
1929
1930/* Callback to be used by applet handlers upon completion. It updates the stream
1931 * (which may or may not take this opportunity to try to forward data), then
Willy Tarreau4596fe22022-05-17 19:07:51 +02001932 * may re-enable the applet's based on the channels and stream connector's final
Christopher Faulet5e29b762022-04-04 08:58:34 +02001933 * states.
1934 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001935static int sc_applet_process(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001936{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001937 struct channel *ic = sc_ic(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001938
Willy Tarreau0adb2812022-05-27 10:02:48 +02001939 BUG_ON(!sc_appctx(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001940
1941 /* If the applet wants to write and the channel is closed, it's a
1942 * broken pipe and it must be reported.
1943 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001944 if (!sc_ep_test(sc, SE_FL_HAVE_NO_DATA) && (ic->flags & CF_SHUTR))
1945 sc_ep_set(sc, SE_FL_ERROR);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001946
1947 /* automatically mark the applet having data available if it reported
1948 * begin blocked by the channel.
1949 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001950 if ((sc->flags & (SC_FL_WONT_READ|SC_FL_NEED_BUFF|SC_FL_NEED_ROOM)) ||
1951 sc_ep_test(sc, SE_FL_APPLET_NEED_CONN))
1952 applet_have_more_data(__sc_appctx(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001953
Willy Tarreau4596fe22022-05-17 19:07:51 +02001954 /* update the stream connector, channels, and possibly wake the stream up */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001955 sc_notify(sc);
1956 stream_release_buffers(__sc_strm(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001957
Willy Tarreau19c65a92022-05-27 08:49:24 +02001958 /* sc_notify may have passed through chk_snd and released some blocking
Willy Tarreau15252cd2022-05-25 16:36:21 +02001959 * flags. Process_stream will consider those flags to wake up the
Christopher Faulet5e29b762022-04-04 08:58:34 +02001960 * appctx but in the case the task is not in runqueue we may have to
1961 * wakeup the appctx immediately.
1962 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001963 if (sc_is_recv_allowed(sc) || sc_is_send_allowed(sc))
1964 appctx_wakeup(__sc_appctx(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001965 return 0;
Christopher Faulet13045f02022-04-01 14:23:38 +02001966}
Christopher Fauletb68f77d2022-06-16 16:24:16 +02001967
1968
1969/* Prepares an endpoint upgrade. We don't now at this stage if the upgrade will
1970 * succeed or not and if the stconn will be reused by the new endpoint. Thus,
1971 * for now, only pretend the stconn is detached.
1972 */
1973void sc_conn_prepare_endp_upgrade(struct stconn *sc)
1974{
1975 BUG_ON(!sc_conn(sc) || !sc->app);
1976 sc_ep_clr(sc, SE_FL_T_MUX);
1977 sc_ep_set(sc, SE_FL_DETACHED);
1978}
1979
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001980/* Endpoint upgrade failed. Restore the stconn state. */
Christopher Fauletb68f77d2022-06-16 16:24:16 +02001981void sc_conn_abort_endp_upgrade(struct stconn *sc)
1982{
1983 sc_ep_set(sc, SE_FL_T_MUX);
1984 sc_ep_clr(sc, SE_FL_DETACHED);
1985}
1986
1987/* Commit the endpoint upgrade. If stconn is attached, it means the new endpoint
1988 * use it. So we do nothing. Otherwise, the stconn will be destroy with the
1989 * overlying stream. So, it means we must commit the detach.
1990*/
1991void sc_conn_commit_endp_upgrade(struct stconn *sc)
1992{
1993 if (!sc_ep_test(sc, SE_FL_DETACHED))
1994 return;
1995 sc_detach_endp(&sc);
1996 /* Because it was already set as detached, the sedesc must be preserved */
Willy Tarreau6a378d12022-08-11 13:56:42 +02001997 BUG_ON(!sc);
Christopher Fauletb68f77d2022-06-16 16:24:16 +02001998 BUG_ON(!sc->sedesc);
1999}