blob: e1266a4ebce94f368d517255ca8c85763129467f [file] [log] [blame]
Christopher Faulet1329f2a2021-12-16 17:32:56 +01001/*
Willy Tarreau4596fe22022-05-17 19:07:51 +02002 * stream connector management functions
Christopher Faulet1329f2a2021-12-16 17:32:56 +01003 *
4 * Copyright 2021 Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <haproxy/api.h>
Christopher Faulet37046632022-04-01 11:36:58 +020014#include <haproxy/applet.h>
Christopher Faulet1329f2a2021-12-16 17:32:56 +010015#include <haproxy/connection.h>
Christopher Faulet5e29b762022-04-04 08:58:34 +020016#include <haproxy/check.h>
17#include <haproxy/http_ana.h>
18#include <haproxy/pipe.h>
Christopher Faulet1329f2a2021-12-16 17:32:56 +010019#include <haproxy/pool.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020020#include <haproxy/sc_strm.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020021#include <haproxy/stconn.h>
Christopher Faulet1329f2a2021-12-16 17:32:56 +010022
Willy Tarreau4596fe22022-05-17 19:07:51 +020023DECLARE_POOL(pool_head_connstream, "stconn", sizeof(struct stconn));
Willy Tarreauea59b022022-05-17 17:53:22 +020024DECLARE_POOL(pool_head_sedesc, "sedesc", sizeof(struct sedesc));
Christopher Faulet1329f2a2021-12-16 17:32:56 +010025
Willy Tarreau3a3f4802022-05-17 18:28:19 +020026/* functions used by default on a detached stream connector */
Willy Tarreau0adb2812022-05-27 10:02:48 +020027static void sc_app_shutr(struct stconn *sc);
28static void sc_app_shutw(struct stconn *sc);
29static void sc_app_chk_rcv(struct stconn *sc);
30static void sc_app_chk_snd(struct stconn *sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +020031
Willy Tarreau3a3f4802022-05-17 18:28:19 +020032/* functions used on a mux-based stream connector */
Willy Tarreau0adb2812022-05-27 10:02:48 +020033static void sc_app_shutr_conn(struct stconn *sc);
34static void sc_app_shutw_conn(struct stconn *sc);
35static void sc_app_chk_rcv_conn(struct stconn *sc);
36static void sc_app_chk_snd_conn(struct stconn *sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +020037
Willy Tarreau3a3f4802022-05-17 18:28:19 +020038/* functions used on an applet-based stream connector */
Willy Tarreau0adb2812022-05-27 10:02:48 +020039static void sc_app_shutr_applet(struct stconn *sc);
40static void sc_app_shutw_applet(struct stconn *sc);
41static void sc_app_chk_rcv_applet(struct stconn *sc);
42static void sc_app_chk_snd_applet(struct stconn *sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +020043
Willy Tarreau0adb2812022-05-27 10:02:48 +020044static int sc_conn_process(struct stconn *sc);
45static int sc_conn_recv(struct stconn *sc);
46static int sc_conn_send(struct stconn *sc);
47static int sc_applet_process(struct stconn *sc);
Willy Tarreau2f2318d2022-05-18 10:17:16 +020048
Willy Tarreau3a3f4802022-05-17 18:28:19 +020049/* stream connector operations for connections */
50struct sc_app_ops sc_app_conn_ops = {
51 .chk_rcv = sc_app_chk_rcv_conn,
52 .chk_snd = sc_app_chk_snd_conn,
53 .shutr = sc_app_shutr_conn,
54 .shutw = sc_app_shutw_conn,
Willy Tarreau462b9892022-05-18 18:06:53 +020055 .wake = sc_conn_process,
Willy Tarreau2f2318d2022-05-18 10:17:16 +020056 .name = "STRM",
Christopher Faulet9ffddd52022-04-01 14:04:29 +020057};
58
Willy Tarreau3a3f4802022-05-17 18:28:19 +020059/* stream connector operations for embedded tasks */
60struct sc_app_ops sc_app_embedded_ops = {
61 .chk_rcv = sc_app_chk_rcv,
62 .chk_snd = sc_app_chk_snd,
63 .shutr = sc_app_shutr,
64 .shutw = sc_app_shutw,
Willy Tarreau2f2318d2022-05-18 10:17:16 +020065 .wake = NULL, /* may never be used */
66 .name = "NONE", /* may never be used */
Christopher Faulet9ffddd52022-04-01 14:04:29 +020067};
68
Willy Tarreau2f2318d2022-05-18 10:17:16 +020069/* stream connector operations for applets */
Willy Tarreau3a3f4802022-05-17 18:28:19 +020070struct sc_app_ops sc_app_applet_ops = {
71 .chk_rcv = sc_app_chk_rcv_applet,
72 .chk_snd = sc_app_chk_snd_applet,
73 .shutr = sc_app_shutr_applet,
74 .shutw = sc_app_shutw_applet,
Willy Tarreau19c65a92022-05-27 08:49:24 +020075 .wake = sc_applet_process,
Christopher Faulet5e29b762022-04-04 08:58:34 +020076 .name = "STRM",
77};
78
Willy Tarreau2f2318d2022-05-18 10:17:16 +020079/* stream connector for health checks on connections */
80struct sc_app_ops sc_app_check_ops = {
81 .chk_rcv = NULL,
82 .chk_snd = NULL,
83 .shutr = NULL,
84 .shutw = NULL,
85 .wake = wake_srv_chk,
86 .name = "CHCK",
87};
Christopher Faulet5e29b762022-04-04 08:58:34 +020088
Christopher Faulet9ed77422022-04-12 08:51:15 +020089/* Initializes an endpoint */
Willy Tarreauea59b022022-05-17 17:53:22 +020090void sedesc_init(struct sedesc *sedesc)
Christopher Fauletdb90f2a2022-03-22 16:06:25 +010091{
Willy Tarreauea59b022022-05-17 17:53:22 +020092 sedesc->se = NULL;
93 sedesc->conn = NULL;
Willy Tarreauc1054922022-05-18 07:43:52 +020094 sedesc->sc = NULL;
Christopher Faulet4c135682023-02-16 11:09:31 +010095 sedesc->lra = TICK_ETERNITY;
96 sedesc->fsb = TICK_ETERNITY;
Willy Tarreauea59b022022-05-17 17:53:22 +020097 se_fl_setall(sedesc, SE_FL_NONE);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +010098}
99
Christopher Faulet9ed77422022-04-12 08:51:15 +0200100/* Tries to alloc an endpoint and initialize it. Returns NULL on failure. */
Willy Tarreauea59b022022-05-17 17:53:22 +0200101struct sedesc *sedesc_new()
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100102{
Willy Tarreauea59b022022-05-17 17:53:22 +0200103 struct sedesc *sedesc;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100104
Willy Tarreauea59b022022-05-17 17:53:22 +0200105 sedesc = pool_alloc(pool_head_sedesc);
106 if (unlikely(!sedesc))
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100107 return NULL;
108
Willy Tarreauea59b022022-05-17 17:53:22 +0200109 sedesc_init(sedesc);
110 return sedesc;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100111}
112
Christopher Faulet9ed77422022-04-12 08:51:15 +0200113/* Releases an endpoint. It is the caller responsibility to be sure it is safe
114 * and it is not shared with another entity
115 */
Willy Tarreauea59b022022-05-17 17:53:22 +0200116void sedesc_free(struct sedesc *sedesc)
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100117{
Willy Tarreauea59b022022-05-17 17:53:22 +0200118 pool_free(pool_head_sedesc, sedesc);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100119}
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100120
Willy Tarreau4596fe22022-05-17 19:07:51 +0200121/* Tries to allocate a new stconn and initialize its main fields. On
Christopher Faulet9ed77422022-04-12 08:51:15 +0200122 * failure, nothing is allocated and NULL is returned. It is an internal
Willy Tarreaub605c422022-05-17 17:04:55 +0200123 * function. The caller must, at least, set the SE_FL_ORPHAN or SE_FL_DETACHED
Christopher Faulet9ed77422022-04-12 08:51:15 +0200124 * flag.
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100125 */
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200126static struct stconn *sc_new(struct sedesc *sedesc)
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100127{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200128 struct stconn *sc;
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100129
Willy Tarreau0adb2812022-05-27 10:02:48 +0200130 sc = pool_alloc(pool_head_connstream);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100131
Willy Tarreau0adb2812022-05-27 10:02:48 +0200132 if (unlikely(!sc))
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100133 goto alloc_error;
Christopher Fauletbb772d02022-03-22 15:28:36 +0100134
Willy Tarreau1d2c79a2022-05-27 11:15:19 +0200135 sc->obj_type = OBJ_TYPE_SC;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200136 sc->flags = SC_FL_NONE;
137 sc->state = SC_ST_INI;
Christopher Fauletbe5cc762023-02-20 08:41:55 +0100138 sc->ioto = TICK_ETERNITY;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200139 sc->app = NULL;
140 sc->app_ops = NULL;
141 sc->src = NULL;
142 sc->dst = NULL;
143 sc->wait_event.tasklet = NULL;
144 sc->wait_event.events = 0;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200145
Christopher Faulet9ed77422022-04-12 08:51:15 +0200146 /* If there is no endpoint, allocate a new one now */
Willy Tarreauea59b022022-05-17 17:53:22 +0200147 if (!sedesc) {
148 sedesc = sedesc_new();
149 if (unlikely(!sedesc))
Christopher Fauletb669d682022-03-22 18:37:19 +0100150 goto alloc_error;
151 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200152 sc->sedesc = sedesc;
153 sedesc->sc = sc;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100154
Willy Tarreau0adb2812022-05-27 10:02:48 +0200155 return sc;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100156
157 alloc_error:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200158 pool_free(pool_head_connstream, sc);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100159 return NULL;
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100160}
161
Willy Tarreau31219282022-05-27 16:21:33 +0200162/* Creates a new stream connector and its associated stream from a mux. <sd> must
163 * be defined. It returns NULL on error. On success, the new stream connector is
Willy Tarreaub605c422022-05-17 17:04:55 +0200164 * returned. In this case, SE_FL_ORPHAN flag is removed.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200165 */
Willy Tarreau31219282022-05-27 16:21:33 +0200166struct stconn *sc_new_from_endp(struct sedesc *sd, struct session *sess, struct buffer *input)
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100167{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200168 struct stconn *sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100169
Willy Tarreau31219282022-05-27 16:21:33 +0200170 sc = sc_new(sd);
Willy Tarreau0adb2812022-05-27 10:02:48 +0200171 if (unlikely(!sc))
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100172 return NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200173 if (unlikely(!stream_new(sess, sc, input))) {
174 pool_free(pool_head_connstream, sc);
Christopher Faulet3ab72c62022-09-27 09:18:20 +0200175 sd->sc = NULL;
176 se_fl_set(sd, SE_FL_ORPHAN);
177 return NULL;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100178 }
Willy Tarreau31219282022-05-27 16:21:33 +0200179 se_fl_clr(sd, SE_FL_ORPHAN);
Willy Tarreau0adb2812022-05-27 10:02:48 +0200180 return sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100181}
182
Willy Tarreau4596fe22022-05-17 19:07:51 +0200183/* Creates a new stream connector from an stream. There is no endpoint here, thus it
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200184 * will be created by sc_new(). So the SE_FL_DETACHED flag is set. It returns
Willy Tarreau4596fe22022-05-17 19:07:51 +0200185 * NULL on error. On success, the new stream connector is returned.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200186 */
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200187struct stconn *sc_new_from_strm(struct stream *strm, unsigned int flags)
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100188{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200189 struct stconn *sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100190
Willy Tarreau0adb2812022-05-27 10:02:48 +0200191 sc = sc_new(NULL);
192 if (unlikely(!sc))
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100193 return NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200194 sc->flags |= flags;
195 sc_ep_set(sc, SE_FL_DETACHED);
196 sc->app = &strm->obj_type;
197 sc->app_ops = &sc_app_embedded_ops;
198 return sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100199}
200
Willy Tarreau4596fe22022-05-17 19:07:51 +0200201/* Creates a new stream connector from an health-check. There is no endpoint here,
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200202 * thus it will be created by sc_new(). So the SE_FL_DETACHED flag is set. It
Willy Tarreau4596fe22022-05-17 19:07:51 +0200203 * returns NULL on error. On success, the new stream connector is returned.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200204 */
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200205struct stconn *sc_new_from_check(struct check *check, unsigned int flags)
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100206{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200207 struct stconn *sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100208
Willy Tarreau0adb2812022-05-27 10:02:48 +0200209 sc = sc_new(NULL);
210 if (unlikely(!sc))
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100211 return NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200212 sc->flags |= flags;
213 sc_ep_set(sc, SE_FL_DETACHED);
214 sc->app = &check->obj_type;
215 sc->app_ops = &sc_app_check_ops;
216 return sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100217}
218
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200219/* Releases a stconn previously allocated by sc_new(), as well as its
Christopher Faulet9ed77422022-04-12 08:51:15 +0200220 * endpoint, if it exists. This function is called internally or on error path.
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100221 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200222void sc_free(struct stconn *sc)
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100223{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200224 sockaddr_free(&sc->src);
225 sockaddr_free(&sc->dst);
226 if (sc->sedesc) {
227 BUG_ON(!sc_ep_test(sc, SE_FL_DETACHED));
228 sedesc_free(sc->sedesc);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100229 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200230 if (sc->wait_event.tasklet)
231 tasklet_free(sc->wait_event.tasklet);
232 pool_free(pool_head_connstream, sc);
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100233}
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100234
Willy Tarreau4596fe22022-05-17 19:07:51 +0200235/* Conditionally removes a stream connector if it is detached and if there is no app
Christopher Fauleteb50c012022-04-21 14:22:53 +0200236 * layer defined. Except on error path, this one must be used. if release, the
Willy Tarreaue68bc612022-05-27 11:23:05 +0200237 * pointer on the SC is set to NULL.
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200238 */
Willy Tarreaue68bc612022-05-27 11:23:05 +0200239static void sc_free_cond(struct stconn **scp)
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200240{
Willy Tarreaue68bc612022-05-27 11:23:05 +0200241 struct stconn *sc = *scp;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200242
Willy Tarreau0adb2812022-05-27 10:02:48 +0200243 if (!sc->app && (!sc->sedesc || sc_ep_test(sc, SE_FL_DETACHED))) {
244 sc_free(sc);
Willy Tarreaue68bc612022-05-27 11:23:05 +0200245 *scp = NULL;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200246 }
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200247}
248
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100249
Willy Tarreau4596fe22022-05-17 19:07:51 +0200250/* Attaches a stconn to a mux endpoint and sets the endpoint ctx. Returns
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500251 * -1 on error and 0 on success. SE_FL_DETACHED flag is removed. This function is
Christopher Faulet9ed77422022-04-12 08:51:15 +0200252 * called from a mux when it is attached to a stream or a health-check.
253 */
Willy Tarreau31219282022-05-27 16:21:33 +0200254int sc_attach_mux(struct stconn *sc, void *sd, void *ctx)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100255{
Christopher Faulet93882042022-01-19 14:56:50 +0100256 struct connection *conn = ctx;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200257 struct sedesc *sedesc = sc->sedesc;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100258
Willy Tarreau0adb2812022-05-27 10:02:48 +0200259 if (sc_strm(sc)) {
260 if (!sc->wait_event.tasklet) {
261 sc->wait_event.tasklet = tasklet_new();
262 if (!sc->wait_event.tasklet)
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200263 return -1;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200264 sc->wait_event.tasklet->process = sc_conn_io_cb;
265 sc->wait_event.tasklet->context = sc;
266 sc->wait_event.events = 0;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200267 }
268
Willy Tarreau0adb2812022-05-27 10:02:48 +0200269 sc->app_ops = &sc_app_conn_ops;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100270 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200271 else if (sc_check(sc)) {
272 if (!sc->wait_event.tasklet) {
273 sc->wait_event.tasklet = tasklet_new();
274 if (!sc->wait_event.tasklet)
Christopher Fauletc95eaef2022-05-18 15:57:15 +0200275 return -1;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200276 sc->wait_event.tasklet->process = srv_chk_io_cb;
277 sc->wait_event.tasklet->context = sc;
278 sc->wait_event.events = 0;
Christopher Fauletc95eaef2022-05-18 15:57:15 +0200279 }
280
Willy Tarreau0adb2812022-05-27 10:02:48 +0200281 sc->app_ops = &sc_app_check_ops;
Christopher Fauletc95eaef2022-05-18 15:57:15 +0200282 }
Willy Tarreaue2f79462023-03-20 19:45:41 +0100283
284 sedesc->se = sd;
285 sedesc->conn = ctx;
286 se_fl_set(sedesc, SE_FL_T_MUX);
287 se_fl_clr(sedesc, SE_FL_DETACHED);
288 if (!conn->ctx)
289 conn->ctx = sc;
Christopher Faulet070b91b2022-03-31 19:27:18 +0200290 return 0;
Christopher Faulet93882042022-01-19 14:56:50 +0100291}
292
Willy Tarreau4596fe22022-05-17 19:07:51 +0200293/* Attaches a stconn to an applet endpoint and sets the endpoint
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500294 * ctx. Returns -1 on error and 0 on success. SE_FL_DETACHED flag is
Christopher Faulet9ed77422022-04-12 08:51:15 +0200295 * removed. This function is called by a stream when a backend applet is
296 * registered.
297 */
Willy Tarreau31219282022-05-27 16:21:33 +0200298static void sc_attach_applet(struct stconn *sc, void *sd)
Christopher Faulet93882042022-01-19 14:56:50 +0100299{
Willy Tarreau31219282022-05-27 16:21:33 +0200300 sc->sedesc->se = sd;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200301 sc_ep_set(sc, SE_FL_T_APPLET);
302 sc_ep_clr(sc, SE_FL_DETACHED);
303 if (sc_strm(sc))
304 sc->app_ops = &sc_app_applet_ops;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100305}
306
Willy Tarreau4596fe22022-05-17 19:07:51 +0200307/* Attaches a stconn to a app layer and sets the relevant
Willy Tarreaub605c422022-05-17 17:04:55 +0200308 * callbacks. Returns -1 on error and 0 on success. SE_FL_ORPHAN flag is
Christopher Faulet9ed77422022-04-12 08:51:15 +0200309 * removed. This function is called by a stream when it is created to attach it
Willy Tarreau4596fe22022-05-17 19:07:51 +0200310 * on the stream connector on the client side.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200311 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200312int sc_attach_strm(struct stconn *sc, struct stream *strm)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100313{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200314 sc->app = &strm->obj_type;
315 sc_ep_clr(sc, SE_FL_ORPHAN);
316 if (sc_ep_test(sc, SE_FL_T_MUX)) {
317 sc->wait_event.tasklet = tasklet_new();
318 if (!sc->wait_event.tasklet)
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200319 return -1;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200320 sc->wait_event.tasklet->process = sc_conn_io_cb;
321 sc->wait_event.tasklet->context = sc;
322 sc->wait_event.events = 0;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200323
Willy Tarreau0adb2812022-05-27 10:02:48 +0200324 sc->app_ops = &sc_app_conn_ops;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100325 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200326 else if (sc_ep_test(sc, SE_FL_T_APPLET)) {
327 sc->app_ops = &sc_app_applet_ops;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100328 }
329 else {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200330 sc->app_ops = &sc_app_embedded_ops;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100331 }
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100332 return 0;
333}
334
Willy Tarreau4596fe22022-05-17 19:07:51 +0200335/* Detaches the stconn from the endpoint, if any. For a connecrion, if a
Christopher Faulet9ed77422022-04-12 08:51:15 +0200336 * mux owns the connection ->detach() callback is called. Otherwise, it means
Willy Tarreau4596fe22022-05-17 19:07:51 +0200337 * the stream connector owns the connection. In this case the connection is closed
Christopher Faulet9ed77422022-04-12 08:51:15 +0200338 * and released. For an applet, the appctx is released. If still allocated, the
339 * endpoint is reset and flag as detached. If the app layer is also detached,
Willy Tarreau4596fe22022-05-17 19:07:51 +0200340 * the stream connector is released.
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100341 */
Willy Tarreaue68bc612022-05-27 11:23:05 +0200342static void sc_detach_endp(struct stconn **scp)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100343{
Willy Tarreaue68bc612022-05-27 11:23:05 +0200344 struct stconn *sc = *scp;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200345
Willy Tarreau0adb2812022-05-27 10:02:48 +0200346 if (!sc)
Christopher Fauleteb50c012022-04-21 14:22:53 +0200347 return;
348
Willy Tarreau0adb2812022-05-27 10:02:48 +0200349 if (sc_ep_test(sc, SE_FL_T_MUX)) {
350 struct connection *conn = __sc_conn(sc);
351 struct sedesc *sedesc = sc->sedesc;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100352
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100353 if (conn->mux) {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200354 if (sc->wait_event.events != 0)
355 conn->mux->unsubscribe(sc, sc->wait_event.events, &sc->wait_event);
Willy Tarreau798465b2022-05-17 18:20:02 +0200356 se_fl_set(sedesc, SE_FL_ORPHAN);
Willy Tarreauc1054922022-05-18 07:43:52 +0200357 sedesc->sc = NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200358 sc->sedesc = NULL;
Willy Tarreau798465b2022-05-17 18:20:02 +0200359 conn->mux->detach(sedesc);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100360 }
361 else {
362 /* It's too early to have a mux, let's just destroy
363 * the connection
364 */
365 conn_stop_tracking(conn);
366 conn_full_close(conn);
367 if (conn->destroy_cb)
368 conn->destroy_cb(conn);
369 conn_free(conn);
370 }
371 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200372 else if (sc_ep_test(sc, SE_FL_T_APPLET)) {
373 struct appctx *appctx = __sc_appctx(sc);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100374
Willy Tarreau0adb2812022-05-27 10:02:48 +0200375 sc_ep_set(sc, SE_FL_ORPHAN);
376 sc->sedesc->sc = NULL;
377 sc->sedesc = NULL;
Willy Tarreau1c3ead42022-05-10 19:42:22 +0200378 appctx_shut(appctx);
379 appctx_free(appctx);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100380 }
381
Willy Tarreau0adb2812022-05-27 10:02:48 +0200382 if (sc->sedesc) {
Willy Tarreauda59c892022-05-27 17:03:34 +0200383 /* the SD wasn't used and can be recycled */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200384 sc->sedesc->se = NULL;
385 sc->sedesc->conn = NULL;
Willy Tarreauda59c892022-05-27 17:03:34 +0200386 sc->sedesc->flags = 0;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200387 sc_ep_set(sc, SE_FL_DETACHED);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100388 }
389
Willy Tarreaue68bc612022-05-27 11:23:05 +0200390 /* FIXME: Rest SC for now but must be reviewed. SC flags are only
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100391 * connection related for now but this will evolved
392 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200393 sc->flags &= SC_FL_ISBACK;
394 if (sc_strm(sc))
395 sc->app_ops = &sc_app_embedded_ops;
Willy Tarreau2f2318d2022-05-18 10:17:16 +0200396 else
Willy Tarreau0adb2812022-05-27 10:02:48 +0200397 sc->app_ops = NULL;
Willy Tarreaue68bc612022-05-27 11:23:05 +0200398 sc_free_cond(scp);
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100399}
400
Willy Tarreau4596fe22022-05-17 19:07:51 +0200401/* Detaches the stconn from the app layer. If there is no endpoint attached
402 * to the stconn
Christopher Faulet9ed77422022-04-12 08:51:15 +0200403 */
Willy Tarreaue68bc612022-05-27 11:23:05 +0200404static void sc_detach_app(struct stconn **scp)
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100405{
Willy Tarreaue68bc612022-05-27 11:23:05 +0200406 struct stconn *sc = *scp;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200407
Willy Tarreau0adb2812022-05-27 10:02:48 +0200408 if (!sc)
Christopher Fauleteb50c012022-04-21 14:22:53 +0200409 return;
410
Willy Tarreau0adb2812022-05-27 10:02:48 +0200411 sc->app = NULL;
412 sc->app_ops = NULL;
413 sockaddr_free(&sc->src);
414 sockaddr_free(&sc->dst);
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200415
Willy Tarreau0adb2812022-05-27 10:02:48 +0200416 if (sc->wait_event.tasklet)
417 tasklet_free(sc->wait_event.tasklet);
418 sc->wait_event.tasklet = NULL;
419 sc->wait_event.events = 0;
Willy Tarreaue68bc612022-05-27 11:23:05 +0200420 sc_free_cond(scp);
Christopher Fauleteb50c012022-04-21 14:22:53 +0200421}
422
Willy Tarreau4596fe22022-05-17 19:07:51 +0200423/* Destroy the stconn. It is detached from its endpoint and its
424 * application. After this call, the stconn must be considered as released.
Christopher Fauleteb50c012022-04-21 14:22:53 +0200425 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200426void sc_destroy(struct stconn *sc)
Christopher Fauleteb50c012022-04-21 14:22:53 +0200427{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200428 sc_detach_endp(&sc);
429 sc_detach_app(&sc);
430 BUG_ON_HOT(sc);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100431}
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100432
Willy Tarreau4596fe22022-05-17 19:07:51 +0200433/* Resets the stream connector endpoint. It happens when the app layer want to renew
Christopher Faulet9ed77422022-04-12 08:51:15 +0200434 * its endpoint. For a connection retry for instance. If a mux or an applet is
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500435 * attached, a new endpoint is created. Returns -1 on error and 0 on success.
Christopher Fauleta6c4a482022-04-28 18:25:24 +0200436 *
Willy Tarreaub605c422022-05-17 17:04:55 +0200437 * Only SE_FL_ERROR flag is removed on the endpoint. Orther flags are preserved.
Christopher Fauleta6c4a482022-04-28 18:25:24 +0200438 * It is the caller responsibility to remove other flags if needed.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200439 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200440int sc_reset_endp(struct stconn *sc)
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100441{
Willy Tarreau31219282022-05-27 16:21:33 +0200442 struct sedesc *new_sd;
Christopher Fauletb041b232022-03-24 10:27:02 +0100443
Willy Tarreau0adb2812022-05-27 10:02:48 +0200444 BUG_ON(!sc->app);
Christopher Fauleta6c4a482022-04-28 18:25:24 +0200445
Willy Tarreau0adb2812022-05-27 10:02:48 +0200446 sc_ep_clr(sc, SE_FL_ERROR);
447 if (!__sc_endp(sc)) {
Christopher Fauletb041b232022-03-24 10:27:02 +0100448 /* endpoint not attached or attached to a mux with no
449 * target. Thus the endpoint will not be release but just
Willy Tarreau0adb2812022-05-27 10:02:48 +0200450 * reset. The app is still attached, the sc will not be
Christopher Fauleteb50c012022-04-21 14:22:53 +0200451 * released.
Christopher Fauletb041b232022-03-24 10:27:02 +0100452 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200453 sc_detach_endp(&sc);
Christopher Fauletb041b232022-03-24 10:27:02 +0100454 return 0;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100455 }
Christopher Fauletb041b232022-03-24 10:27:02 +0100456
457 /* allocate the new endpoint first to be able to set error if it
458 * fails */
Willy Tarreau31219282022-05-27 16:21:33 +0200459 new_sd = sedesc_new();
460 if (!unlikely(new_sd)) {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200461 sc_ep_set(sc, SE_FL_ERROR);
Christopher Fauletb041b232022-03-24 10:27:02 +0100462 return -1;
463 }
464
Willy Tarreau0adb2812022-05-27 10:02:48 +0200465 /* The app is still attached, the sc will not be released */
466 sc_detach_endp(&sc);
Willy Tarreau6a378d12022-08-11 13:56:42 +0200467 BUG_ON(!sc);
Willy Tarreau0adb2812022-05-27 10:02:48 +0200468 BUG_ON(sc->sedesc);
Willy Tarreau31219282022-05-27 16:21:33 +0200469 sc->sedesc = new_sd;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200470 sc->sedesc->sc = sc;
471 sc_ep_set(sc, SE_FL_DETACHED);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100472 return 0;
473}
Christopher Faulet37046632022-04-01 11:36:58 +0200474
475
Willy Tarreaue68bc612022-05-27 11:23:05 +0200476/* Create an applet to handle a stream connector as a new appctx. The SC will
Christopher Faulet37046632022-04-01 11:36:58 +0200477 * wake it up every time it is solicited. The appctx must be deleted by the task
Willy Tarreau19c65a92022-05-27 08:49:24 +0200478 * handler using sc_detach_endp(), possibly from within the function itself.
Christopher Faulet37046632022-04-01 11:36:58 +0200479 * It also pre-initializes the applet's context and returns it (or NULL in case
480 * it could not be allocated).
481 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200482struct appctx *sc_applet_create(struct stconn *sc, struct applet *app)
Christopher Faulet37046632022-04-01 11:36:58 +0200483{
484 struct appctx *appctx;
485
Willy Tarreau0adb2812022-05-27 10:02:48 +0200486 DPRINTF(stderr, "registering handler %p for sc %p (was %p)\n", app, sc, sc_strm_task(sc));
Christopher Faulet37046632022-04-01 11:36:58 +0200487
Willy Tarreau0adb2812022-05-27 10:02:48 +0200488 appctx = appctx_new_here(app, sc->sedesc);
Christopher Faulet37046632022-04-01 11:36:58 +0200489 if (!appctx)
490 return NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200491 sc_attach_applet(sc, appctx);
492 appctx->t->nice = __sc_strm(sc)->task->nice;
Willy Tarreau90e8b452022-05-25 18:21:43 +0200493 applet_need_more_data(appctx);
Christopher Faulet37046632022-04-01 11:36:58 +0200494 appctx_wakeup(appctx);
Christopher Fauleta33ff7a2022-04-21 11:52:07 +0200495
Willy Tarreau0adb2812022-05-27 10:02:48 +0200496 sc->state = SC_ST_RDY;
Christopher Faulet37046632022-04-01 11:36:58 +0200497 return appctx;
498}
499
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100500/* Conditionnaly forward the close to the wirte side. It return 1 if it can be
501 * forwarded. It is the caller responsibility to forward the close to the write
502 * side. Otherwise, 0 is returned. In this case, CF_SHUTW_NOW flag may be set on
503 * the channel if we are only waiting for the outgoing data to be flushed.
504 */
505static inline int sc_cond_forward_shutw(struct stconn *sc)
506{
507 /* The close must not be forwarded */
508 if (!(sc_ic(sc)->flags & CF_SHUTR) || !(sc->flags & SC_FL_NOHALF))
509 return 0;
510
511 if (!channel_is_empty(sc_ic(sc))) {
512 /* the close to the write side cannot be forwarded now because
513 * we should flush outgoing data first. But instruct the output
514 * channel it should be done ASAP.
515 */
516 channel_shutw_now(sc_oc(sc));
517 return 0;
518 }
519
520 /* the close can be immediately forwarded to the write side */
521 return 1;
522}
523
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200524/*
Willy Tarreau4596fe22022-05-17 19:07:51 +0200525 * This function performs a shutdown-read on a detached stream connector in a
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200526 * connected or init state (it does nothing for other states). It either shuts
527 * the read side or marks itself as closed. The buffer flags are updated to
Willy Tarreaucb041662022-05-17 19:44:42 +0200528 * reflect the new state. If the stream connector has SC_FL_NOHALF, we also
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200529 * forward the close to the write side. The owner task is woken up if it exists.
530 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200531static void sc_app_shutr(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200532{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200533 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200534
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200535 if (ic->flags & CF_SHUTR)
Christopher Fauletb08c5252023-02-20 07:55:19 +0100536
537 ic->flags |= CF_SHUTR|CF_READ_EVENT;
Christopher Faulet4c135682023-02-16 11:09:31 +0100538 sc_ep_report_read_activity(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200539
Willy Tarreau0adb2812022-05-27 10:02:48 +0200540 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200541 return;
542
Willy Tarreau0adb2812022-05-27 10:02:48 +0200543 if (sc_oc(sc)->flags & CF_SHUTW) {
544 sc->state = SC_ST_DIS;
Christopher Fauletca679922022-07-20 13:24:04 +0200545 if (sc->flags & SC_FL_ISBACK)
546 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200547 }
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100548 else if (sc_cond_forward_shutw(sc))
Willy Tarreau0adb2812022-05-27 10:02:48 +0200549 return sc_app_shutw(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200550
551 /* note that if the task exists, it must unregister itself once it runs */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200552 if (!(sc->flags & SC_FL_DONT_WAKE))
553 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200554}
555
556/*
Willy Tarreau4596fe22022-05-17 19:07:51 +0200557 * This function performs a shutdown-write on a detached stream connector in a
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200558 * connected or init state (it does nothing for other states). It either shuts
559 * the write side or marks itself as closed. The buffer flags are updated to
Willy Tarreaue68bc612022-05-27 11:23:05 +0200560 * reflect the new state. It does also close everything if the SC was marked as
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200561 * being in error state. The owner task is woken up if it exists.
562 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200563static void sc_app_shutw(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200564{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200565 struct channel *ic = sc_ic(sc);
566 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200567
568 oc->flags &= ~CF_SHUTW_NOW;
569 if (oc->flags & CF_SHUTW)
570 return;
Christopher Fauletb08c5252023-02-20 07:55:19 +0100571 oc->flags |= CF_SHUTW|CF_WRITE_EVENT;
Christopher Fauletbcdcfad2023-02-20 08:36:53 +0100572 sc_set_hcto(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200573
Willy Tarreau0adb2812022-05-27 10:02:48 +0200574 switch (sc->state) {
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200575 case SC_ST_RDY:
576 case SC_ST_EST:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200577 /* we have to shut before closing, otherwise some short messages
578 * may never leave the system, especially when there are remaining
579 * unread data in the socket input buffer, or when nolinger is set.
Willy Tarreaucb041662022-05-17 19:44:42 +0200580 * However, if SC_FL_NOLINGER is explicitly set, we know there is
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200581 * no risk so we close both sides immediately.
582 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200583 if (!sc_ep_test(sc, SE_FL_ERROR) && !(sc->flags & SC_FL_NOLINGER) &&
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200584 !(ic->flags & (CF_SHUTR|CF_DONT_READ)))
585 return;
586
Willy Tarreau476c2802022-11-14 07:36:42 +0100587 __fallthrough;
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200588 case SC_ST_CON:
589 case SC_ST_CER:
590 case SC_ST_QUE:
591 case SC_ST_TAR:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200592 /* Note that none of these states may happen with applets */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200593 sc->state = SC_ST_DIS;
Willy Tarreau476c2802022-11-14 07:36:42 +0100594 __fallthrough;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200595 default:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200596 sc->flags &= ~SC_FL_NOLINGER;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200597 ic->flags |= CF_SHUTR;
Christopher Fauletca679922022-07-20 13:24:04 +0200598 if (sc->flags & SC_FL_ISBACK)
599 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200600 }
601
602 /* note that if the task exists, it must unregister itself once it runs */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200603 if (!(sc->flags & SC_FL_DONT_WAKE))
604 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200605}
606
607/* default chk_rcv function for scheduled tasks */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200608static void sc_app_chk_rcv(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200609{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200610 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200611
Willy Tarreau0adb2812022-05-27 10:02:48 +0200612 DPRINTF(stderr, "%s: sc=%p, sc->state=%d ic->flags=%08x oc->flags=%08x\n",
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200613 __FUNCTION__,
Willy Tarreau0adb2812022-05-27 10:02:48 +0200614 sc, sc->state, ic->flags, sc_oc(sc)->flags);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200615
616 if (ic->pipe) {
617 /* stop reading */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200618 sc_need_room(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200619 }
620 else {
621 /* (re)start reading */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200622 if (!(sc->flags & SC_FL_DONT_WAKE))
623 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200624 }
625}
626
627/* default chk_snd function for scheduled tasks */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200628static void sc_app_chk_snd(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200629{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200630 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200631
Willy Tarreau0adb2812022-05-27 10:02:48 +0200632 DPRINTF(stderr, "%s: sc=%p, sc->state=%d ic->flags=%08x oc->flags=%08x\n",
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200633 __FUNCTION__,
Willy Tarreau0adb2812022-05-27 10:02:48 +0200634 sc, sc->state, sc_ic(sc)->flags, oc->flags);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200635
Willy Tarreau0adb2812022-05-27 10:02:48 +0200636 if (unlikely(sc->state != SC_ST_EST || (oc->flags & CF_SHUTW)))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200637 return;
638
Willy Tarreau0adb2812022-05-27 10:02:48 +0200639 if (!sc_ep_test(sc, SE_FL_WAIT_DATA) || /* not waiting for data */
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200640 channel_is_empty(oc)) /* called with nothing to send ! */
641 return;
642
643 /* Otherwise there are remaining data to be sent in the buffer,
644 * so we tell the handler.
645 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200646 sc_ep_clr(sc, SE_FL_WAIT_DATA);
Willy Tarreau0adb2812022-05-27 10:02:48 +0200647 if (!(sc->flags & SC_FL_DONT_WAKE))
648 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200649}
650
651/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200652 * This function performs a shutdown-read on a stream connector attached to
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200653 * a connection in a connected or init state (it does nothing for other
654 * states). It either shuts the read side or marks itself as closed. The buffer
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200655 * flags are updated to reflect the new state. If the stream connector has
Willy Tarreaucb041662022-05-17 19:44:42 +0200656 * SC_FL_NOHALF, we also forward the close to the write side. If a control
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200657 * layer is defined, then it is supposed to be a socket layer and file
658 * descriptors are then shutdown or closed accordingly. The function
659 * automatically disables polling if needed.
660 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200661static void sc_app_shutr_conn(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200662{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200663 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200664
Willy Tarreau0adb2812022-05-27 10:02:48 +0200665 BUG_ON(!sc_conn(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200666
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200667 if (ic->flags & CF_SHUTR)
668 return;
Christopher Fauletb08c5252023-02-20 07:55:19 +0100669 ic->flags |= CF_SHUTR|CF_READ_EVENT;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200670
Willy Tarreau0adb2812022-05-27 10:02:48 +0200671 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200672 return;
673
Willy Tarreau0adb2812022-05-27 10:02:48 +0200674 if (sc_oc(sc)->flags & CF_SHUTW) {
675 sc_conn_shut(sc);
676 sc->state = SC_ST_DIS;
Christopher Fauletca679922022-07-20 13:24:04 +0200677 if (sc->flags & SC_FL_ISBACK)
678 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200679 }
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100680 else if (sc_cond_forward_shutw(sc))
Willy Tarreau0adb2812022-05-27 10:02:48 +0200681 return sc_app_shutw_conn(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200682}
683
684/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200685 * This function performs a shutdown-write on a stream connector attached to
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200686 * a connection in a connected or init state (it does nothing for other
687 * states). It either shuts the write side or marks itself as closed. The
688 * buffer flags are updated to reflect the new state. It does also close
Willy Tarreaue68bc612022-05-27 11:23:05 +0200689 * everything if the SC was marked as being in error state. If there is a
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200690 * data-layer shutdown, it is called.
691 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200692static void sc_app_shutw_conn(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200693{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200694 struct channel *ic = sc_ic(sc);
695 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200696
Willy Tarreau0adb2812022-05-27 10:02:48 +0200697 BUG_ON(!sc_conn(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200698
699 oc->flags &= ~CF_SHUTW_NOW;
700 if (oc->flags & CF_SHUTW)
701 return;
Christopher Fauletb08c5252023-02-20 07:55:19 +0100702 oc->flags |= CF_SHUTW|CF_WRITE_EVENT;
Christopher Fauletbcdcfad2023-02-20 08:36:53 +0100703 sc_set_hcto(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200704
Willy Tarreau0adb2812022-05-27 10:02:48 +0200705 switch (sc->state) {
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200706 case SC_ST_RDY:
707 case SC_ST_EST:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200708 /* we have to shut before closing, otherwise some short messages
709 * may never leave the system, especially when there are remaining
710 * unread data in the socket input buffer, or when nolinger is set.
Willy Tarreaucb041662022-05-17 19:44:42 +0200711 * However, if SC_FL_NOLINGER is explicitly set, we know there is
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200712 * no risk so we close both sides immediately.
713 */
714
Willy Tarreau0adb2812022-05-27 10:02:48 +0200715 if (sc_ep_test(sc, SE_FL_ERROR)) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200716 /* quick close, the socket is already shut anyway */
717 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200718 else if (sc->flags & SC_FL_NOLINGER) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200719 /* unclean data-layer shutdown, typically an aborted request
720 * or a forwarded shutdown from a client to a server due to
721 * option abortonclose. No need for the TLS layer to try to
722 * emit a shutdown message.
723 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200724 sc_conn_shutw(sc, CO_SHW_SILENT);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200725 }
726 else {
727 /* clean data-layer shutdown. This only happens on the
728 * frontend side, or on the backend side when forwarding
729 * a client close in TCP mode or in HTTP TUNNEL mode
730 * while option abortonclose is set. We want the TLS
731 * layer to try to signal it to the peer before we close.
732 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200733 sc_conn_shutw(sc, CO_SHW_NORMAL);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200734
735 if (!(ic->flags & (CF_SHUTR|CF_DONT_READ)))
736 return;
737 }
738
Willy Tarreau476c2802022-11-14 07:36:42 +0100739 __fallthrough;
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200740 case SC_ST_CON:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200741 /* we may have to close a pending connection, and mark the
742 * response buffer as shutr
743 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200744 sc_conn_shut(sc);
Willy Tarreau476c2802022-11-14 07:36:42 +0100745 __fallthrough;
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200746 case SC_ST_CER:
747 case SC_ST_QUE:
748 case SC_ST_TAR:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200749 sc->state = SC_ST_DIS;
Willy Tarreau476c2802022-11-14 07:36:42 +0100750 __fallthrough;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200751 default:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200752 sc->flags &= ~SC_FL_NOLINGER;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200753 ic->flags |= CF_SHUTR;
Christopher Fauletca679922022-07-20 13:24:04 +0200754 if (sc->flags & SC_FL_ISBACK)
755 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200756 }
757}
758
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200759/* This function is used for inter-stream connector calls. It is called by the
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200760 * consumer to inform the producer side that it may be interested in checking
761 * for free space in the buffer. Note that it intentionally does not update
762 * timeouts, so that we can still check them later at wake-up. This function is
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200763 * dedicated to connection-based stream connectors.
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200764 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200765static void sc_app_chk_rcv_conn(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200766{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200767 BUG_ON(!sc_conn(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200768
769 /* (re)start reading */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200770 if (sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
771 tasklet_wakeup(sc->wait_event.tasklet);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200772}
773
774
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200775/* This function is used for inter-stream connector calls. It is called by the
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200776 * producer to inform the consumer side that it may be interested in checking
777 * for data in the buffer. Note that it intentionally does not update timeouts,
778 * so that we can still check them later at wake-up.
779 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200780static void sc_app_chk_snd_conn(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200781{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200782 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200783
Willy Tarreau0adb2812022-05-27 10:02:48 +0200784 BUG_ON(!sc_conn(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200785
Willy Tarreau0adb2812022-05-27 10:02:48 +0200786 if (unlikely(!sc_state_in(sc->state, SC_SB_RDY|SC_SB_EST) ||
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200787 (oc->flags & CF_SHUTW)))
788 return;
789
790 if (unlikely(channel_is_empty(oc))) /* called with nothing to send ! */
791 return;
792
793 if (!oc->pipe && /* spliced data wants to be forwarded ASAP */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200794 !sc_ep_test(sc, SE_FL_WAIT_DATA)) /* not waiting for data */
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200795 return;
796
Willy Tarreau0adb2812022-05-27 10:02:48 +0200797 if (!(sc->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(sc_oc(sc)))
798 sc_conn_send(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200799
Willy Tarreau0adb2812022-05-27 10:02:48 +0200800 if (sc_ep_test(sc, SE_FL_ERROR | SE_FL_ERR_PENDING) || sc_is_conn_error(sc)) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200801 /* Write error on the file descriptor */
Christopher Faulet7f6aa562022-10-17 10:21:19 +0200802 if (sc->state >= SC_ST_CON && sc_ep_test(sc, SE_FL_EOS))
Willy Tarreau0adb2812022-05-27 10:02:48 +0200803 sc_ep_set(sc, SE_FL_ERROR);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200804 goto out_wakeup;
805 }
806
807 /* OK, so now we know that some data might have been sent, and that we may
808 * have to poll first. We have to do that too if the buffer is not empty.
809 */
810 if (channel_is_empty(oc)) {
811 /* the connection is established but we can't write. Either the
812 * buffer is empty, or we just refrain from sending because the
813 * ->o limit was reached. Maybe we just wrote the last
814 * chunk and need to close.
815 */
816 if (((oc->flags & (CF_SHUTW|CF_AUTO_CLOSE|CF_SHUTW_NOW)) ==
817 (CF_AUTO_CLOSE|CF_SHUTW_NOW)) &&
Willy Tarreau0adb2812022-05-27 10:02:48 +0200818 sc_state_in(sc->state, SC_SB_RDY|SC_SB_EST)) {
819 sc_shutw(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200820 goto out_wakeup;
821 }
822
823 if ((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == 0)
Willy Tarreau0adb2812022-05-27 10:02:48 +0200824 sc_ep_set(sc, SE_FL_WAIT_DATA);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200825 }
826 else {
827 /* Otherwise there are remaining data to be sent in the buffer,
828 * which means we have to poll before doing so.
829 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200830 sc_ep_clr(sc, SE_FL_WAIT_DATA);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200831 }
832
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200833 /* in case of special condition (error, shutdown, end of write...), we
834 * have to notify the task.
835 */
Christopher Faulet71c486b2023-02-09 14:14:38 +0100836 if (likely((oc->flags & CF_SHUTW) ||
837 ((oc->flags & CF_WRITE_EVENT) && sc->state < SC_ST_EST) ||
838 ((oc->flags & CF_WAKE_WRITE) &&
839 ((channel_is_empty(oc) && !oc->to_forward) ||
840 !sc_state_in(sc->state, SC_SB_EST))))) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200841 out_wakeup:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200842 if (!(sc->flags & SC_FL_DONT_WAKE))
843 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200844 }
845}
846
847/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200848 * This function performs a shutdown-read on a stream connector attached to an
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200849 * applet in a connected or init state (it does nothing for other states). It
850 * either shuts the read side or marks itself as closed. The buffer flags are
Willy Tarreaucb041662022-05-17 19:44:42 +0200851 * updated to reflect the new state. If the stream connector has SC_FL_NOHALF,
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200852 * we also forward the close to the write side. The owner task is woken up if
853 * it exists.
854 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200855static void sc_app_shutr_applet(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200856{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200857 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200858
Willy Tarreau0adb2812022-05-27 10:02:48 +0200859 BUG_ON(!sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200860
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200861 if (ic->flags & CF_SHUTR)
862 return;
Christopher Fauletb08c5252023-02-20 07:55:19 +0100863 ic->flags |= CF_SHUTR|CF_READ_EVENT;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200864
865 /* Note: on shutr, we don't call the applet */
866
Willy Tarreau0adb2812022-05-27 10:02:48 +0200867 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200868 return;
869
Willy Tarreau0adb2812022-05-27 10:02:48 +0200870 if (sc_oc(sc)->flags & CF_SHUTW) {
871 appctx_shut(__sc_appctx(sc));
872 sc->state = SC_ST_DIS;
Christopher Fauletca679922022-07-20 13:24:04 +0200873 if (sc->flags & SC_FL_ISBACK)
874 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200875 }
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100876 else if (sc_cond_forward_shutw(sc))
Willy Tarreau0adb2812022-05-27 10:02:48 +0200877 return sc_app_shutw_applet(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200878}
879
880/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200881 * This function performs a shutdown-write on a stream connector attached to an
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200882 * applet in a connected or init state (it does nothing for other states). It
883 * either shuts the write side or marks itself as closed. The buffer flags are
884 * updated to reflect the new state. It does also close everything if the SI
885 * was marked as being in error state. The owner task is woken up if it exists.
886 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200887static void sc_app_shutw_applet(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200888{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200889 struct channel *ic = sc_ic(sc);
890 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200891
Willy Tarreau0adb2812022-05-27 10:02:48 +0200892 BUG_ON(!sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200893
894 oc->flags &= ~CF_SHUTW_NOW;
895 if (oc->flags & CF_SHUTW)
896 return;
Christopher Fauletb08c5252023-02-20 07:55:19 +0100897 oc->flags |= CF_SHUTW|CF_WRITE_EVENT;
Christopher Fauletbcdcfad2023-02-20 08:36:53 +0100898 sc_set_hcto(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200899
900 /* on shutw we always wake the applet up */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200901 appctx_wakeup(__sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200902
Willy Tarreau0adb2812022-05-27 10:02:48 +0200903 switch (sc->state) {
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200904 case SC_ST_RDY:
905 case SC_ST_EST:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200906 /* we have to shut before closing, otherwise some short messages
907 * may never leave the system, especially when there are remaining
908 * unread data in the socket input buffer, or when nolinger is set.
Willy Tarreaucb041662022-05-17 19:44:42 +0200909 * However, if SC_FL_NOLINGER is explicitly set, we know there is
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200910 * no risk so we close both sides immediately.
911 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200912 if (!sc_ep_test(sc, SE_FL_ERROR) && !(sc->flags & SC_FL_NOLINGER) &&
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200913 !(ic->flags & (CF_SHUTR|CF_DONT_READ)))
914 return;
915
Willy Tarreau476c2802022-11-14 07:36:42 +0100916 __fallthrough;
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200917 case SC_ST_CON:
918 case SC_ST_CER:
919 case SC_ST_QUE:
920 case SC_ST_TAR:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200921 /* Note that none of these states may happen with applets */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200922 appctx_shut(__sc_appctx(sc));
923 sc->state = SC_ST_DIS;
Willy Tarreau476c2802022-11-14 07:36:42 +0100924 __fallthrough;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200925 default:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200926 sc->flags &= ~SC_FL_NOLINGER;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200927 ic->flags |= CF_SHUTR;
Christopher Fauletca679922022-07-20 13:24:04 +0200928 if (sc->flags & SC_FL_ISBACK)
929 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200930 }
931}
932
933/* chk_rcv function for applets */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200934static void sc_app_chk_rcv_applet(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200935{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200936 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200937
Willy Tarreau0adb2812022-05-27 10:02:48 +0200938 BUG_ON(!sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200939
Willy Tarreau0adb2812022-05-27 10:02:48 +0200940 DPRINTF(stderr, "%s: sc=%p, sc->state=%d ic->flags=%08x oc->flags=%08x\n",
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200941 __FUNCTION__,
Willy Tarreau0adb2812022-05-27 10:02:48 +0200942 sc, sc->state, ic->flags, sc_oc(sc)->flags);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200943
944 if (!ic->pipe) {
945 /* (re)start reading */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200946 appctx_wakeup(__sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200947 }
948}
949
950/* chk_snd function for applets */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200951static void sc_app_chk_snd_applet(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200952{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200953 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200954
Willy Tarreau0adb2812022-05-27 10:02:48 +0200955 BUG_ON(!sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200956
Willy Tarreau0adb2812022-05-27 10:02:48 +0200957 DPRINTF(stderr, "%s: sc=%p, sc->state=%d ic->flags=%08x oc->flags=%08x\n",
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200958 __FUNCTION__,
Willy Tarreau0adb2812022-05-27 10:02:48 +0200959 sc, sc->state, sc_ic(sc)->flags, oc->flags);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200960
Willy Tarreau0adb2812022-05-27 10:02:48 +0200961 if (unlikely(sc->state != SC_ST_EST || (oc->flags & CF_SHUTW)))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200962 return;
963
Christopher Faulet04f03e12022-06-01 17:35:34 +0200964 /* we only wake the applet up if it was waiting for some data and is ready to consume it */
965 if (!sc_ep_test(sc, SE_FL_WAIT_DATA) || sc_ep_test(sc, SE_FL_WONT_CONSUME))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200966 return;
967
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200968 if (!channel_is_empty(oc)) {
969 /* (re)start sending */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200970 appctx_wakeup(__sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200971 }
972}
Christopher Faulet13045f02022-04-01 14:23:38 +0200973
974
975/* This function is designed to be called from within the stream handler to
Willy Tarreau4596fe22022-05-17 19:07:51 +0200976 * update the input channel's expiration timer and the stream connector's
Christopher Faulet13045f02022-04-01 14:23:38 +0200977 * Rx flags based on the channel's flags. It needs to be called only once
978 * after the channel's flags have settled down, and before they are cleared,
979 * though it doesn't harm to call it as often as desired (it just slightly
980 * hurts performance). It must not be called from outside of the stream
981 * handler, as what it does will be used to compute the stream task's
982 * expiration.
983 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200984void sc_update_rx(struct stconn *sc)
Christopher Faulet13045f02022-04-01 14:23:38 +0200985{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200986 struct channel *ic = sc_ic(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +0200987
Willy Tarreau676c8db2022-05-24 16:22:24 +0200988 if (ic->flags & CF_SHUTR)
Christopher Faulet13045f02022-04-01 14:23:38 +0200989 return;
Christopher Faulet13045f02022-04-01 14:23:38 +0200990
991 /* Read not closed, update FD status and timeout for reads */
992 if (ic->flags & CF_DONT_READ)
Willy Tarreau0adb2812022-05-27 10:02:48 +0200993 sc_wont_read(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +0200994 else
Willy Tarreau0adb2812022-05-27 10:02:48 +0200995 sc_will_read(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +0200996
Willy Tarreau0adb2812022-05-27 10:02:48 +0200997 sc_chk_rcv(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +0200998}
999
1000/* This function is designed to be called from within the stream handler to
Willy Tarreau4596fe22022-05-17 19:07:51 +02001001 * update the output channel's expiration timer and the stream connector's
Christopher Faulet13045f02022-04-01 14:23:38 +02001002 * Tx flags based on the channel's flags. It needs to be called only once
1003 * after the channel's flags have settled down, and before they are cleared,
1004 * though it doesn't harm to call it as often as desired (it just slightly
1005 * hurts performance). It must not be called from outside of the stream
1006 * handler, as what it does will be used to compute the stream task's
1007 * expiration.
1008 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001009void sc_update_tx(struct stconn *sc)
Christopher Faulet13045f02022-04-01 14:23:38 +02001010{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001011 struct channel *oc = sc_oc(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +02001012
1013 if (oc->flags & CF_SHUTW)
1014 return;
1015
1016 /* Write not closed, update FD status and timeout for writes */
1017 if (channel_is_empty(oc)) {
1018 /* stop writing */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001019 if (!sc_ep_test(sc, SE_FL_WAIT_DATA)) {
Christopher Faulet13045f02022-04-01 14:23:38 +02001020 if ((oc->flags & CF_SHUTW_NOW) == 0)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001021 sc_ep_set(sc, SE_FL_WAIT_DATA);
Christopher Faulet13045f02022-04-01 14:23:38 +02001022 }
1023 return;
1024 }
1025
Christopher Faulet15315d62023-02-20 08:23:51 +01001026 /* (re)start writing */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001027 sc_ep_clr(sc, SE_FL_WAIT_DATA);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001028}
1029
Willy Tarreau19c65a92022-05-27 08:49:24 +02001030/* This function is the equivalent to sc_update() except that it's
Christopher Faulet5e29b762022-04-04 08:58:34 +02001031 * designed to be called from outside the stream handlers, typically the lower
1032 * layers (applets, connections) after I/O completion. After updating the stream
1033 * interface and timeouts, it will try to forward what can be forwarded, then to
1034 * wake the associated task up if an important event requires special handling.
Willy Tarreau15252cd2022-05-25 16:36:21 +02001035 * It may update SE_FL_WAIT_DATA and/or SC_FL_NEED_ROOM, that the callers are
Christopher Faulet5e29b762022-04-04 08:58:34 +02001036 * encouraged to watch to take appropriate action.
Willy Tarreau19c65a92022-05-27 08:49:24 +02001037 * It should not be called from within the stream itself, sc_update()
Christopher Faulet5e29b762022-04-04 08:58:34 +02001038 * is designed for this.
1039 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001040static void sc_notify(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001041{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001042 struct channel *ic = sc_ic(sc);
1043 struct channel *oc = sc_oc(sc);
Willy Tarreaue68bc612022-05-27 11:23:05 +02001044 struct stconn *sco = sc_opposite(sc);
Willy Tarreau0adb2812022-05-27 10:02:48 +02001045 struct task *task = sc_strm_task(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001046
1047 /* process consumer side */
1048 if (channel_is_empty(oc)) {
Willy Tarreau0adb2812022-05-27 10:02:48 +02001049 struct connection *conn = sc_conn(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001050
1051 if (((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW) &&
Willy Tarreau0adb2812022-05-27 10:02:48 +02001052 (sc->state == SC_ST_EST) && (!conn || !(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS))))
1053 sc_shutw(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001054 }
1055
1056 /* indicate that we may be waiting for data from the output channel or
1057 * we're about to close and can't expect more data if SHUTW_NOW is there.
1058 */
1059 if (!(oc->flags & (CF_SHUTW|CF_SHUTW_NOW)))
Willy Tarreau0adb2812022-05-27 10:02:48 +02001060 sc_ep_set(sc, SE_FL_WAIT_DATA);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001061 else if ((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001062 sc_ep_clr(sc, SE_FL_WAIT_DATA);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001063
Christopher Faulet5e29b762022-04-04 08:58:34 +02001064 if (oc->flags & CF_DONT_READ)
Willy Tarreaue68bc612022-05-27 11:23:05 +02001065 sc_wont_read(sco);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001066 else
Willy Tarreaue68bc612022-05-27 11:23:05 +02001067 sc_will_read(sco);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001068
1069 /* Notify the other side when we've injected data into the IC that
1070 * needs to be forwarded. We can do fast-forwarding as soon as there
1071 * are output data, but we avoid doing this if some of the data are
1072 * not yet scheduled for being forwarded, because it is very likely
1073 * that it will be done again immediately afterwards once the following
Willy Tarreau15252cd2022-05-25 16:36:21 +02001074 * data are parsed (eg: HTTP chunking). We only clear SC_FL_NEED_ROOM
1075 * once we've emptied *some* of the output buffer, and not just when
1076 * there is available room, because applets are often forced to stop
1077 * before the buffer is full. We must not stop based on input data
1078 * alone because an HTTP parser might need more data to complete the
1079 * parsing.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001080 */
1081 if (!channel_is_empty(ic) &&
Willy Tarreaue68bc612022-05-27 11:23:05 +02001082 sc_ep_test(sco, SE_FL_WAIT_DATA) &&
Christopher Faulet5e29b762022-04-04 08:58:34 +02001083 (!(ic->flags & CF_EXPECT_MORE) || c_full(ic) || ci_data(ic) == 0 || ic->pipe)) {
1084 int new_len, last_len;
1085
1086 last_len = co_data(ic);
1087 if (ic->pipe)
1088 last_len += ic->pipe->data;
1089
Willy Tarreaue68bc612022-05-27 11:23:05 +02001090 sc_chk_snd(sco);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001091
1092 new_len = co_data(ic);
1093 if (ic->pipe)
1094 new_len += ic->pipe->data;
1095
1096 /* check if the consumer has freed some space either in the
1097 * buffer or in the pipe.
1098 */
1099 if (new_len < last_len)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001100 sc_have_room(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001101 }
1102
1103 if (!(ic->flags & CF_DONT_READ))
Willy Tarreau0adb2812022-05-27 10:02:48 +02001104 sc_will_read(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001105
Willy Tarreau0adb2812022-05-27 10:02:48 +02001106 sc_chk_rcv(sc);
Willy Tarreaue68bc612022-05-27 11:23:05 +02001107 sc_chk_rcv(sco);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001108
Christopher Faulet5e29b762022-04-04 08:58:34 +02001109 /* wake the task up only when needed */
Christopher Faulet285f7612022-12-12 08:28:55 +01001110 if (/* changes on the production side that must be handled:
Christopher Faulet2e56a732023-01-26 16:18:09 +01001111 * - An error on receipt: SE_FL_ERROR
Christopher Faulet285f7612022-12-12 08:28:55 +01001112 * - A read event: shutdown for reads (CF_READ_EVENT + SHUTR)
1113 * end of input (CF_READ_EVENT + CF_EOI)
1114 * data received and no fast-forwarding (CF_READ_EVENT + !to_forward)
1115 * read event while consumer side is not established (CF_READ_EVENT + sco->state != SC_ST_EST)
1116 */
1117 ((ic->flags & CF_READ_EVENT) && ((ic->flags & (CF_SHUTR|CF_EOI)) || !ic->to_forward || sco->state != SC_ST_EST)) ||
Christopher Faulet2e56a732023-01-26 16:18:09 +01001118 sc_ep_test(sc, SE_FL_ERROR) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001119
1120 /* changes on the consumption side */
Christopher Faulet2e56a732023-01-26 16:18:09 +01001121 sc_ep_test(sc, SE_FL_ERR_PENDING) ||
Christopher Fauletd8988412022-12-20 18:10:04 +01001122 ((oc->flags & CF_WRITE_EVENT) &&
1123 ((sc->state < SC_ST_EST) ||
1124 (oc->flags & CF_SHUTW) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001125 (((oc->flags & CF_WAKE_WRITE) ||
Christopher Fauletd8988412022-12-20 18:10:04 +01001126 !(oc->flags & (CF_AUTO_CLOSE|CF_SHUTW_NOW|CF_SHUTW))) &&
1127 (sco->state != SC_ST_EST ||
1128 (channel_is_empty(oc) && !oc->to_forward)))))) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001129 task_wakeup(task, TASK_WOKEN_IO);
1130 }
Christopher Faulet5e29b762022-04-04 08:58:34 +02001131
Christopher Faulet2e56a732023-01-26 16:18:09 +01001132 if (ic->flags & CF_READ_EVENT)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001133 ic->flags &= ~CF_READ_DONTWAIT;
1134}
1135
1136/*
1137 * This function propagates a null read received on a socket-based connection.
Willy Tarreaucb041662022-05-17 19:44:42 +02001138 * It updates the stream connector. If the stream connector has SC_FL_NOHALF,
Christopher Faulet5e29b762022-04-04 08:58:34 +02001139 * the close is also forwarded to the write side as an abort.
1140 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001141static void sc_conn_read0(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001142{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001143 struct channel *ic = sc_ic(sc);
1144 struct channel *oc = sc_oc(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001145
Willy Tarreau0adb2812022-05-27 10:02:48 +02001146 BUG_ON(!sc_conn(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001147
Christopher Faulet5e29b762022-04-04 08:58:34 +02001148 if (ic->flags & CF_SHUTR)
1149 return;
Christopher Fauletb08c5252023-02-20 07:55:19 +01001150 ic->flags |= CF_SHUTR|CF_READ_EVENT;
Christopher Faulet4c135682023-02-16 11:09:31 +01001151 sc_ep_report_read_activity(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001152
Willy Tarreau0adb2812022-05-27 10:02:48 +02001153 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001154 return;
1155
1156 if (oc->flags & CF_SHUTW)
1157 goto do_close;
1158
Christopher Fauleteb3f26d2023-02-08 16:18:48 +01001159 if (sc_cond_forward_shutw(sc)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001160 /* we want to immediately forward this close to the write side */
1161 /* force flag on ssl to keep stream in cache */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001162 sc_conn_shutw(sc, CO_SHW_SILENT);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001163 goto do_close;
1164 }
1165
1166 /* otherwise that's just a normal read shutdown */
1167 return;
1168
1169 do_close:
Willy Tarreauf61dd192022-05-27 09:00:19 +02001170 /* OK we completely close the socket here just as if we went through sc_shut[rw]() */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001171 sc_conn_shut(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001172
1173 oc->flags &= ~CF_SHUTW_NOW;
1174 oc->flags |= CF_SHUTW;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001175
Willy Tarreau0adb2812022-05-27 10:02:48 +02001176 sc->state = SC_ST_DIS;
Christopher Fauletca679922022-07-20 13:24:04 +02001177 if (sc->flags & SC_FL_ISBACK)
1178 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001179 return;
1180}
1181
1182/*
1183 * This is the callback which is called by the connection layer to receive data
1184 * into the buffer from the connection. It iterates over the mux layer's
1185 * rcv_buf function.
1186 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001187static int sc_conn_recv(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001188{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001189 struct connection *conn = __sc_conn(sc);
1190 struct channel *ic = sc_ic(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001191 int ret, max, cur_read = 0;
1192 int read_poll = MAX_READ_POLL_LOOPS;
1193 int flags = 0;
1194
1195 /* If not established yet, do nothing. */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001196 if (sc->state != SC_ST_EST)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001197 return 0;
1198
Willy Tarreau462b9892022-05-18 18:06:53 +02001199 /* If another call to sc_conn_recv() failed, and we subscribed to
Christopher Faulet5e29b762022-04-04 08:58:34 +02001200 * recv events already, give up now.
1201 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001202 if (sc->wait_event.events & SUB_RETRY_RECV)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001203 return 0;
1204
1205 /* maybe we were called immediately after an asynchronous shutr */
1206 if (ic->flags & CF_SHUTR)
1207 return 1;
1208
1209 /* we must wait because the mux is not installed yet */
1210 if (!conn->mux)
1211 return 0;
1212
1213 /* stop here if we reached the end of data */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001214 if (sc_ep_test(sc, SE_FL_EOS))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001215 goto end_recv;
1216
1217 /* stop immediately on errors. Note that we DON'T want to stop on
1218 * POLL_ERR, as the poller might report a write error while there
1219 * are still data available in the recv buffer. This typically
1220 * happens when we send too large a request to a backend server
1221 * which rejects it before reading it all.
1222 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001223 if (!sc_ep_test(sc, SE_FL_RCV_MORE)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001224 if (!conn_xprt_ready(conn))
1225 return 0;
Willy Tarreau0adb2812022-05-27 10:02:48 +02001226 if (sc_ep_test(sc, SE_FL_ERROR))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001227 goto end_recv;
1228 }
1229
1230 /* prepare to detect if the mux needs more room */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001231 sc_ep_clr(sc, SE_FL_WANT_ROOM);
Christopher Faulet341a5782023-02-10 17:37:11 +01001232 BUG_ON(sc_waiting_room(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001233
1234 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) && !co_data(ic) &&
1235 global.tune.idle_timer &&
1236 (unsigned short)(now_ms - ic->last_read) >= global.tune.idle_timer) {
1237 /* The buffer was empty and nothing was transferred for more
1238 * than one second. This was caused by a pause and not by
1239 * congestion. Reset any streaming mode to reduce latency.
1240 */
1241 ic->xfer_small = 0;
1242 ic->xfer_large = 0;
1243 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
1244 }
1245
1246 /* First, let's see if we may splice data across the channel without
1247 * using a buffer.
1248 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001249 if (sc_ep_test(sc, SE_FL_MAY_SPLICE) &&
Christopher Faulet5e29b762022-04-04 08:58:34 +02001250 (ic->pipe || ic->to_forward >= MIN_SPLICE_FORWARD) &&
1251 ic->flags & CF_KERN_SPLICING) {
1252 if (c_data(ic)) {
1253 /* We're embarrassed, there are already data pending in
1254 * the buffer and we don't want to have them at two
1255 * locations at a time. Let's indicate we need some
1256 * place and ask the consumer to hurry.
1257 */
1258 flags |= CO_RFL_BUF_FLUSH;
1259 goto abort_splice;
1260 }
1261
1262 if (unlikely(ic->pipe == NULL)) {
1263 if (pipes_used >= global.maxpipes || !(ic->pipe = get_pipe())) {
1264 ic->flags &= ~CF_KERN_SPLICING;
1265 goto abort_splice;
1266 }
1267 }
1268
Willy Tarreau0adb2812022-05-27 10:02:48 +02001269 ret = conn->mux->rcv_pipe(sc, ic->pipe, ic->to_forward);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001270 if (ret < 0) {
1271 /* splice not supported on this end, let's disable it */
1272 ic->flags &= ~CF_KERN_SPLICING;
1273 goto abort_splice;
1274 }
1275
1276 if (ret > 0) {
1277 if (ic->to_forward != CHN_INFINITE_FORWARD)
1278 ic->to_forward -= ret;
1279 ic->total += ret;
1280 cur_read += ret;
Christopher Faulet285f7612022-12-12 08:28:55 +01001281 ic->flags |= CF_READ_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001282 }
1283
Willy Tarreau0adb2812022-05-27 10:02:48 +02001284 if (sc_ep_test(sc, SE_FL_EOS | SE_FL_ERROR))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001285 goto end_recv;
1286
1287 if (conn->flags & CO_FL_WAIT_ROOM) {
1288 /* the pipe is full or we have read enough data that it
1289 * could soon be full. Let's stop before needing to poll.
1290 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001291 sc_need_room(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001292 goto done_recv;
1293 }
1294
1295 /* splice not possible (anymore), let's go on on standard copy */
1296 }
1297
1298 abort_splice:
1299 if (ic->pipe && unlikely(!ic->pipe->data)) {
1300 put_pipe(ic->pipe);
1301 ic->pipe = NULL;
1302 }
1303
Willy Tarreau0adb2812022-05-27 10:02:48 +02001304 if (ic->pipe && ic->to_forward && !(flags & CO_RFL_BUF_FLUSH) && sc_ep_test(sc, SE_FL_MAY_SPLICE)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001305 /* don't break splicing by reading, but still call rcv_buf()
1306 * to pass the flag.
1307 */
1308 goto done_recv;
1309 }
1310
1311 /* now we'll need a input buffer for the stream */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001312 if (!sc_alloc_ibuf(sc, &(__sc_strm(sc)->buffer_wait)))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001313 goto end_recv;
1314
1315 /* For an HTX stream, if the buffer is stuck (no output data with some
1316 * input data) and if the HTX message is fragmented or if its free space
1317 * wraps, we force an HTX deframentation. It is a way to have a
1318 * contiguous free space nad to let the mux to copy as much data as
1319 * possible.
1320 *
1321 * NOTE: A possible optim may be to let the mux decides if defrag is
1322 * required or not, depending on amount of data to be xferred.
1323 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001324 if (IS_HTX_STRM(__sc_strm(sc)) && !co_data(ic)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001325 struct htx *htx = htxbuf(&ic->buf);
1326
1327 if (htx_is_not_empty(htx) && ((htx->flags & HTX_FL_FRAGMENTED) || htx_space_wraps(htx)))
1328 htx_defrag(htx, NULL, 0);
1329 }
1330
1331 /* Instruct the mux it must subscribed for read events */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001332 flags |= ((!conn_is_back(conn) && (__sc_strm(sc)->be->options & PR_O_ABRT_CLOSE)) ? CO_RFL_KEEP_RECV : 0);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001333
1334 /* Important note : if we're called with POLL_IN|POLL_HUP, it means the read polling
1335 * was enabled, which implies that the recv buffer was not full. So we have a guarantee
1336 * that if such an event is not handled above in splice, it will be handled here by
1337 * recv().
1338 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001339 while (sc_ep_test(sc, SE_FL_RCV_MORE) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001340 (!(conn->flags & CO_FL_HANDSHAKE) &&
Willy Tarreau0adb2812022-05-27 10:02:48 +02001341 (!sc_ep_test(sc, SE_FL_ERROR | SE_FL_EOS)) && !(ic->flags & CF_SHUTR))) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001342 int cur_flags = flags;
1343
1344 /* Compute transient CO_RFL_* flags */
1345 if (co_data(ic)) {
1346 cur_flags |= (CO_RFL_BUF_WET | CO_RFL_BUF_NOT_STUCK);
1347 }
1348
1349 /* <max> may be null. This is the mux responsibility to set
Willy Tarreaue68bc612022-05-27 11:23:05 +02001350 * SE_FL_RCV_MORE on the SC if more space is needed.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001351 */
1352 max = channel_recv_max(ic);
Willy Tarreau0adb2812022-05-27 10:02:48 +02001353 ret = conn->mux->rcv_buf(sc, &ic->buf, max, cur_flags);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001354
Willy Tarreau0adb2812022-05-27 10:02:48 +02001355 if (sc_ep_test(sc, SE_FL_WANT_ROOM)) {
Willy Tarreaub605c422022-05-17 17:04:55 +02001356 /* SE_FL_WANT_ROOM must not be reported if the channel's
Christopher Faulet5e29b762022-04-04 08:58:34 +02001357 * buffer is empty.
1358 */
1359 BUG_ON(c_empty(ic));
1360
Willy Tarreau0adb2812022-05-27 10:02:48 +02001361 sc_need_room(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001362 /* Add READ_PARTIAL because some data are pending but
1363 * cannot be xferred to the channel
1364 */
Christopher Faulet285f7612022-12-12 08:28:55 +01001365 ic->flags |= CF_READ_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001366 }
1367
1368 if (ret <= 0) {
1369 /* if we refrained from reading because we asked for a
1370 * flush to satisfy rcv_pipe(), we must not subscribe
1371 * and instead report that there's not enough room
1372 * here to proceed.
1373 */
1374 if (flags & CO_RFL_BUF_FLUSH)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001375 sc_need_room(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001376 break;
1377 }
1378
1379 cur_read += ret;
1380
1381 /* if we're allowed to directly forward data, we must update ->o */
1382 if (ic->to_forward && !(ic->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
1383 unsigned long fwd = ret;
1384 if (ic->to_forward != CHN_INFINITE_FORWARD) {
1385 if (fwd > ic->to_forward)
1386 fwd = ic->to_forward;
1387 ic->to_forward -= fwd;
1388 }
1389 c_adv(ic, fwd);
1390 }
1391
Christopher Faulet285f7612022-12-12 08:28:55 +01001392 ic->flags |= CF_READ_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001393 ic->total += ret;
1394
1395 /* End-of-input reached, we can leave. In this case, it is
Willy Tarreaue68bc612022-05-27 11:23:05 +02001396 * important to break the loop to not block the SC because of
Christopher Faulet5e29b762022-04-04 08:58:34 +02001397 * the channel's policies.This way, we are still able to receive
1398 * shutdowns.
1399 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001400 if (sc_ep_test(sc, SE_FL_EOI))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001401 break;
1402
1403 if ((ic->flags & CF_READ_DONTWAIT) || --read_poll <= 0) {
1404 /* we're stopped by the channel's policy */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001405 sc_wont_read(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001406 break;
1407 }
1408
1409 /* if too many bytes were missing from last read, it means that
1410 * it's pointless trying to read again because the system does
1411 * not have them in buffers.
1412 */
1413 if (ret < max) {
1414 /* if a streamer has read few data, it may be because we
1415 * have exhausted system buffers. It's not worth trying
1416 * again.
1417 */
1418 if (ic->flags & CF_STREAMER) {
1419 /* we're stopped by the channel's policy */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001420 sc_wont_read(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001421 break;
1422 }
1423
1424 /* if we read a large block smaller than what we requested,
1425 * it's almost certain we'll never get anything more.
1426 */
1427 if (ret >= global.tune.recv_enough) {
1428 /* we're stopped by the channel's policy */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001429 sc_wont_read(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001430 break;
1431 }
1432 }
1433
1434 /* if we are waiting for more space, don't try to read more data
1435 * right now.
1436 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001437 if (sc->flags & (SC_FL_WONT_READ|SC_FL_NEED_BUFF|SC_FL_NEED_ROOM))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001438 break;
1439 } /* while !flags */
1440
1441 done_recv:
1442 if (cur_read) {
1443 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) &&
1444 (cur_read <= ic->buf.size / 2)) {
1445 ic->xfer_large = 0;
1446 ic->xfer_small++;
1447 if (ic->xfer_small >= 3) {
1448 /* we have read less than half of the buffer in
1449 * one pass, and this happened at least 3 times.
1450 * This is definitely not a streamer.
1451 */
1452 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
1453 }
1454 else if (ic->xfer_small >= 2) {
1455 /* if the buffer has been at least half full twice,
1456 * we receive faster than we send, so at least it
1457 * is not a "fast streamer".
1458 */
1459 ic->flags &= ~CF_STREAMER_FAST;
1460 }
1461 }
1462 else if (!(ic->flags & CF_STREAMER_FAST) &&
1463 (cur_read >= ic->buf.size - global.tune.maxrewrite)) {
1464 /* we read a full buffer at once */
1465 ic->xfer_small = 0;
1466 ic->xfer_large++;
1467 if (ic->xfer_large >= 3) {
1468 /* we call this buffer a fast streamer if it manages
1469 * to be filled in one call 3 consecutive times.
1470 */
1471 ic->flags |= (CF_STREAMER | CF_STREAMER_FAST);
1472 }
1473 }
1474 else {
1475 ic->xfer_small = 0;
1476 ic->xfer_large = 0;
1477 }
1478 ic->last_read = now_ms;
Christopher Faulet4c135682023-02-16 11:09:31 +01001479 sc_ep_report_read_activity(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001480 }
1481
1482 end_recv:
1483 ret = (cur_read != 0);
1484
1485 /* Report EOI on the channel if it was reached from the mux point of
1486 * view. */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001487 if (sc_ep_test(sc, SE_FL_EOI) && !(ic->flags & CF_EOI)) {
Christopher Faulet4c135682023-02-16 11:09:31 +01001488 sc_ep_report_read_activity(sc);
Christopher Faulet285f7612022-12-12 08:28:55 +01001489 ic->flags |= (CF_EOI|CF_READ_EVENT);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001490 ret = 1;
1491 }
1492
Willy Tarreau0adb2812022-05-27 10:02:48 +02001493 if (sc_ep_test(sc, SE_FL_ERROR))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001494 ret = 1;
Willy Tarreau0adb2812022-05-27 10:02:48 +02001495 else if (sc_ep_test(sc, SE_FL_EOS)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001496 /* we received a shutdown */
Christopher Faulet5e29b762022-04-04 08:58:34 +02001497 if (ic->flags & CF_AUTO_CLOSE)
1498 channel_shutw_now(ic);
Willy Tarreau0adb2812022-05-27 10:02:48 +02001499 sc_conn_read0(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001500 ret = 1;
1501 }
Willy Tarreau0adb2812022-05-27 10:02:48 +02001502 else if (!(sc->flags & (SC_FL_WONT_READ|SC_FL_NEED_BUFF|SC_FL_NEED_ROOM)) &&
Willy Tarreau15252cd2022-05-25 16:36:21 +02001503 !(ic->flags & CF_SHUTR)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001504 /* Subscribe to receive events if we're blocking on I/O */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001505 conn->mux->subscribe(sc, SUB_RETRY_RECV, &sc->wait_event);
1506 se_have_no_more_data(sc->sedesc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001507 } else {
Willy Tarreau0adb2812022-05-27 10:02:48 +02001508 se_have_more_data(sc->sedesc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001509 ret = 1;
1510 }
1511 return ret;
1512}
1513
Willy Tarreau4596fe22022-05-17 19:07:51 +02001514/* This tries to perform a synchronous receive on the stream connector to
Christopher Faulet5e29b762022-04-04 08:58:34 +02001515 * try to collect last arrived data. In practice it's only implemented on
Willy Tarreau4596fe22022-05-17 19:07:51 +02001516 * stconns. Returns 0 if nothing was done, non-zero if new data or a
Christopher Faulet5e29b762022-04-04 08:58:34 +02001517 * shutdown were collected. This may result on some delayed receive calls
1518 * to be programmed and performed later, though it doesn't provide any
1519 * such guarantee.
1520 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001521int sc_conn_sync_recv(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001522{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001523 if (!sc_state_in(sc->state, SC_SB_RDY|SC_SB_EST))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001524 return 0;
1525
Willy Tarreau0adb2812022-05-27 10:02:48 +02001526 if (!sc_mux_ops(sc))
Willy Tarreau4596fe22022-05-17 19:07:51 +02001527 return 0; // only stconns are supported
Christopher Faulet5e29b762022-04-04 08:58:34 +02001528
Willy Tarreau0adb2812022-05-27 10:02:48 +02001529 if (sc->wait_event.events & SUB_RETRY_RECV)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001530 return 0; // already subscribed
1531
Willy Tarreau0adb2812022-05-27 10:02:48 +02001532 if (!sc_is_recv_allowed(sc))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001533 return 0; // already failed
1534
Willy Tarreau0adb2812022-05-27 10:02:48 +02001535 return sc_conn_recv(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001536}
1537
1538/*
1539 * This function is called to send buffer data to a stream socket.
1540 * It calls the mux layer's snd_buf function. It relies on the
1541 * caller to commit polling changes. The caller should check conn->flags
1542 * for errors.
1543 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001544static int sc_conn_send(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001545{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001546 struct connection *conn = __sc_conn(sc);
1547 struct stream *s = __sc_strm(sc);
1548 struct channel *oc = sc_oc(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001549 int ret;
1550 int did_send = 0;
1551
Willy Tarreau0adb2812022-05-27 10:02:48 +02001552 if (sc_ep_test(sc, SE_FL_ERROR | SE_FL_ERR_PENDING) || sc_is_conn_error(sc)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001553 /* We're probably there because the tasklet was woken up,
1554 * but process_stream() ran before, detected there were an
Willy Tarreaue68bc612022-05-27 11:23:05 +02001555 * error and put the SC back to SC_ST_TAR. There's still
Christopher Faulet5e29b762022-04-04 08:58:34 +02001556 * CO_FL_ERROR on the connection but we don't want to add
Willy Tarreaub605c422022-05-17 17:04:55 +02001557 * SE_FL_ERROR back, so give up
Christopher Faulet5e29b762022-04-04 08:58:34 +02001558 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001559 if (sc->state < SC_ST_CON)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001560 return 0;
Christopher Faulet7f6aa562022-10-17 10:21:19 +02001561 if (sc_ep_test(sc, SE_FL_EOS))
1562 sc_ep_set(sc, SE_FL_ERROR);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001563 return 1;
1564 }
1565
1566 /* We're already waiting to be able to send, give up */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001567 if (sc->wait_event.events & SUB_RETRY_SEND)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001568 return 0;
1569
1570 /* we might have been called just after an asynchronous shutw */
1571 if (oc->flags & CF_SHUTW)
1572 return 1;
1573
1574 /* we must wait because the mux is not installed yet */
1575 if (!conn->mux)
1576 return 0;
1577
1578 if (oc->pipe && conn->xprt->snd_pipe && conn->mux->snd_pipe) {
Willy Tarreau0adb2812022-05-27 10:02:48 +02001579 ret = conn->mux->snd_pipe(sc, oc->pipe);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001580 if (ret > 0)
1581 did_send = 1;
1582
1583 if (!oc->pipe->data) {
1584 put_pipe(oc->pipe);
1585 oc->pipe = NULL;
1586 }
1587
1588 if (oc->pipe)
1589 goto end;
1590 }
1591
1592 /* At this point, the pipe is empty, but we may still have data pending
1593 * in the normal buffer.
1594 */
1595 if (co_data(oc)) {
1596 /* when we're here, we already know that there is no spliced
1597 * data left, and that there are sendable buffered data.
1598 */
1599
1600 /* check if we want to inform the kernel that we're interested in
1601 * sending more data after this call. We want this if :
1602 * - we're about to close after this last send and want to merge
1603 * the ongoing FIN with the last segment.
1604 * - we know we can't send everything at once and must get back
1605 * here because of unaligned data
1606 * - there is still a finite amount of data to forward
1607 * The test is arranged so that the most common case does only 2
1608 * tests.
1609 */
1610 unsigned int send_flag = 0;
1611
1612 if ((!(oc->flags & (CF_NEVER_WAIT|CF_SEND_DONTWAIT)) &&
1613 ((oc->to_forward && oc->to_forward != CHN_INFINITE_FORWARD) ||
1614 (oc->flags & CF_EXPECT_MORE) ||
1615 (IS_HTX_STRM(s) &&
1616 (!(oc->flags & (CF_EOI|CF_SHUTR)) && htx_expect_more(htxbuf(&oc->buf)))))) ||
1617 ((oc->flags & CF_ISRESP) &&
1618 ((oc->flags & (CF_AUTO_CLOSE|CF_SHUTW_NOW)) == (CF_AUTO_CLOSE|CF_SHUTW_NOW))))
1619 send_flag |= CO_SFL_MSG_MORE;
1620
1621 if (oc->flags & CF_STREAMER)
1622 send_flag |= CO_SFL_STREAMER;
1623
1624 if (s->txn && s->txn->flags & TX_L7_RETRY && !b_data(&s->txn->l7_buffer)) {
1625 /* If we want to be able to do L7 retries, copy
1626 * the data we're about to send, so that we are able
1627 * to resend them if needed
1628 */
1629 /* Try to allocate a buffer if we had none.
1630 * If it fails, the next test will just
1631 * disable the l7 retries by setting
1632 * l7_conn_retries to 0.
1633 */
1634 if (s->txn->req.msg_state != HTTP_MSG_DONE)
1635 s->txn->flags &= ~TX_L7_RETRY;
1636 else {
1637 if (b_alloc(&s->txn->l7_buffer) == NULL)
1638 s->txn->flags &= ~TX_L7_RETRY;
1639 else {
1640 memcpy(b_orig(&s->txn->l7_buffer),
1641 b_orig(&oc->buf),
1642 b_size(&oc->buf));
1643 s->txn->l7_buffer.head = co_data(oc);
1644 b_add(&s->txn->l7_buffer, co_data(oc));
1645 }
1646
1647 }
1648 }
1649
Willy Tarreau0adb2812022-05-27 10:02:48 +02001650 ret = conn->mux->snd_buf(sc, &oc->buf, co_data(oc), send_flag);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001651 if (ret > 0) {
1652 did_send = 1;
1653 c_rew(oc, ret);
1654 c_realign_if_empty(oc);
1655
1656 if (!co_data(oc)) {
1657 /* Always clear both flags once everything has been sent, they're one-shot */
1658 oc->flags &= ~(CF_EXPECT_MORE | CF_SEND_DONTWAIT);
1659 }
1660 /* if some data remain in the buffer, it's only because the
1661 * system buffers are full, we will try next time.
1662 */
Christopher Faulet13045f02022-04-01 14:23:38 +02001663 }
1664 }
Christopher Faulet5e29b762022-04-04 08:58:34 +02001665
1666 end:
1667 if (did_send) {
Christopher Fauletd8988412022-12-20 18:10:04 +01001668 oc->flags |= CF_WRITE_EVENT | CF_WROTE_DATA;
Willy Tarreau0adb2812022-05-27 10:02:48 +02001669 if (sc->state == SC_ST_CON)
1670 sc->state = SC_ST_RDY;
Willy Tarreau0adb2812022-05-27 10:02:48 +02001671 sc_have_room(sc_opposite(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001672 }
1673
Willy Tarreau0adb2812022-05-27 10:02:48 +02001674 if (sc_ep_test(sc, SE_FL_ERROR | SE_FL_ERR_PENDING)) {
Christopher Faulet2e56a732023-01-26 16:18:09 +01001675 oc->flags |= CF_WRITE_EVENT;
Christopher Faulet7f6aa562022-10-17 10:21:19 +02001676 if (sc_ep_test(sc, SE_FL_EOS))
Christopher Faulet2e56a732023-01-26 16:18:09 +01001677 sc_ep_set(sc, SE_FL_ERROR);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001678 return 1;
1679 }
1680
Christopher Faulet59b240c2023-02-27 16:38:12 +01001681 if (channel_is_empty(oc))
1682 sc_ep_report_send_activity(sc);
1683 else {
1684 /* We couldn't send all of our data, let the mux know we'd like to send more */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001685 conn->mux->subscribe(sc, SUB_RETRY_SEND, &sc->wait_event);
Christopher Faulet59b240c2023-02-27 16:38:12 +01001686 sc_ep_report_blocked_send(sc);
1687 }
1688
Christopher Faulet5e29b762022-04-04 08:58:34 +02001689 return did_send;
1690}
1691
Christopher Fauletd8988412022-12-20 18:10:04 +01001692/* perform a synchronous send() for the stream connector. The CF_WRITE_EVENT
1693 * flag are cleared prior to the attempt, and will possibly be updated in case
1694 * of success.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001695 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001696void sc_conn_sync_send(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001697{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001698 struct channel *oc = sc_oc(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001699
Christopher Fauletd8988412022-12-20 18:10:04 +01001700 oc->flags &= ~CF_WRITE_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001701
1702 if (oc->flags & CF_SHUTW)
1703 return;
1704
1705 if (channel_is_empty(oc))
1706 return;
1707
Willy Tarreau0adb2812022-05-27 10:02:48 +02001708 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001709 return;
1710
Willy Tarreau0adb2812022-05-27 10:02:48 +02001711 if (!sc_mux_ops(sc))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001712 return;
1713
Willy Tarreau0adb2812022-05-27 10:02:48 +02001714 sc_conn_send(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001715}
1716
1717/* Called by I/O handlers after completion.. It propagates
Willy Tarreau4596fe22022-05-17 19:07:51 +02001718 * connection flags to the stream connector, updates the stream (which may or
Christopher Faulet5e29b762022-04-04 08:58:34 +02001719 * may not take this opportunity to try to forward data), then update the
Willy Tarreau4596fe22022-05-17 19:07:51 +02001720 * connection's polling based on the channels and stream connector's final
Christopher Faulet5e29b762022-04-04 08:58:34 +02001721 * states. The function always returns 0.
1722 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001723static int sc_conn_process(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001724{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001725 struct connection *conn = __sc_conn(sc);
1726 struct channel *ic = sc_ic(sc);
1727 struct channel *oc = sc_oc(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001728
1729 BUG_ON(!conn);
1730
1731 /* If we have data to send, try it now */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001732 if (!channel_is_empty(oc) && !(sc->wait_event.events & SUB_RETRY_SEND))
1733 sc_conn_send(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001734
Willy Tarreau4596fe22022-05-17 19:07:51 +02001735 /* First step, report to the stream connector what was detected at the
Christopher Faulet5e29b762022-04-04 08:58:34 +02001736 * connection layer : errors and connection establishment.
Willy Tarreaub605c422022-05-17 17:04:55 +02001737 * Only add SE_FL_ERROR if we're connected, or we're attempting to
Christopher Faulet5e29b762022-04-04 08:58:34 +02001738 * connect, we may get there because we got woken up, but only run
1739 * after process_stream() noticed there were an error, and decided
1740 * to retry to connect, the connection may still have CO_FL_ERROR,
Willy Tarreaub605c422022-05-17 17:04:55 +02001741 * and we don't want to add SE_FL_ERROR back
Christopher Faulet5e29b762022-04-04 08:58:34 +02001742 *
Willy Tarreau462b9892022-05-18 18:06:53 +02001743 * Note: This test is only required because sc_conn_process is also the SI
1744 * wake callback. Otherwise sc_conn_recv()/sc_conn_send() already take
Christopher Faulet5e29b762022-04-04 08:58:34 +02001745 * care of it.
1746 */
1747
Willy Tarreau0adb2812022-05-27 10:02:48 +02001748 if (sc->state >= SC_ST_CON) {
1749 if (sc_is_conn_error(sc))
1750 sc_ep_set(sc, SE_FL_ERROR);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001751 }
1752
1753 /* If we had early data, and the handshake ended, then
1754 * we can remove the flag, and attempt to wake the task up,
1755 * in the event there's an analyser waiting for the end of
1756 * the handshake.
1757 */
1758 if (!(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS)) &&
Willy Tarreau0adb2812022-05-27 10:02:48 +02001759 sc_ep_test(sc, SE_FL_WAIT_FOR_HS)) {
1760 sc_ep_clr(sc, SE_FL_WAIT_FOR_HS);
1761 task_wakeup(sc_strm_task(sc), TASK_WOKEN_MSG);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001762 }
1763
Willy Tarreau0adb2812022-05-27 10:02:48 +02001764 if (!sc_state_in(sc->state, SC_SB_EST|SC_SB_DIS|SC_SB_CLO) &&
Christopher Faulet5e29b762022-04-04 08:58:34 +02001765 (conn->flags & CO_FL_WAIT_XPRT) == 0) {
Christopher Fauletca679922022-07-20 13:24:04 +02001766 if (sc->flags & SC_FL_ISBACK)
1767 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Fauletb96f2aa2022-12-12 08:11:36 +01001768 oc->flags |= CF_WRITE_EVENT;
Willy Tarreau0adb2812022-05-27 10:02:48 +02001769 if (sc->state == SC_ST_CON)
1770 sc->state = SC_ST_RDY;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001771 }
1772
1773 /* Report EOS on the channel if it was reached from the mux point of
1774 * view.
1775 *
Willy Tarreau462b9892022-05-18 18:06:53 +02001776 * Note: This test is only required because sc_conn_process is also the SI
1777 * wake callback. Otherwise sc_conn_recv()/sc_conn_send() already take
Christopher Faulet5e29b762022-04-04 08:58:34 +02001778 * care of it.
1779 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001780 if (sc_ep_test(sc, SE_FL_EOS) && !(ic->flags & CF_SHUTR)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001781 /* we received a shutdown */
Christopher Faulet5e29b762022-04-04 08:58:34 +02001782 if (ic->flags & CF_AUTO_CLOSE)
1783 channel_shutw_now(ic);
Willy Tarreau0adb2812022-05-27 10:02:48 +02001784 sc_conn_read0(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001785 }
1786
1787 /* Report EOI on the channel if it was reached from the mux point of
1788 * view.
1789 *
Willy Tarreau462b9892022-05-18 18:06:53 +02001790 * Note: This test is only required because sc_conn_process is also the SI
1791 * wake callback. Otherwise sc_conn_recv()/sc_conn_send() already take
Christopher Faulet5e29b762022-04-04 08:58:34 +02001792 * care of it.
1793 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001794 if (sc_ep_test(sc, SE_FL_EOI) && !(ic->flags & CF_EOI))
Christopher Faulet285f7612022-12-12 08:28:55 +01001795 ic->flags |= (CF_EOI|CF_READ_EVENT);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001796
Willy Tarreau4596fe22022-05-17 19:07:51 +02001797 /* Second step : update the stream connector and channels, try to forward any
Christopher Faulet5e29b762022-04-04 08:58:34 +02001798 * pending data, then possibly wake the stream up based on the new
Willy Tarreau4596fe22022-05-17 19:07:51 +02001799 * stream connector status.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001800 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001801 sc_notify(sc);
1802 stream_release_buffers(__sc_strm(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001803 return 0;
1804}
1805
Willy Tarreau4596fe22022-05-17 19:07:51 +02001806/* This is the ->process() function for any stream connector's wait_event task.
1807 * It's assigned during the stream connector's initialization, for any type of
1808 * stream connector. Thus it is always safe to perform a tasklet_wakeup() on a
Willy Tarreaue68bc612022-05-27 11:23:05 +02001809 * stream connector, as the presence of the SC is checked there.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001810 */
Willy Tarreau462b9892022-05-18 18:06:53 +02001811struct task *sc_conn_io_cb(struct task *t, void *ctx, unsigned int state)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001812{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001813 struct stconn *sc = ctx;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001814 int ret = 0;
1815
Willy Tarreau0adb2812022-05-27 10:02:48 +02001816 if (!sc_conn(sc))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001817 return t;
1818
Willy Tarreau0adb2812022-05-27 10:02:48 +02001819 if (!(sc->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(sc_oc(sc)))
1820 ret = sc_conn_send(sc);
1821 if (!(sc->wait_event.events & SUB_RETRY_RECV))
1822 ret |= sc_conn_recv(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001823 if (ret != 0)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001824 sc_conn_process(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001825
Willy Tarreau0adb2812022-05-27 10:02:48 +02001826 stream_release_buffers(__sc_strm(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001827 return t;
1828}
1829
1830/* Callback to be used by applet handlers upon completion. It updates the stream
1831 * (which may or may not take this opportunity to try to forward data), then
Willy Tarreau4596fe22022-05-17 19:07:51 +02001832 * may re-enable the applet's based on the channels and stream connector's final
Christopher Faulet5e29b762022-04-04 08:58:34 +02001833 * states.
1834 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001835static int sc_applet_process(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001836{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001837 struct channel *ic = sc_ic(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001838
Willy Tarreau0adb2812022-05-27 10:02:48 +02001839 BUG_ON(!sc_appctx(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001840
1841 /* If the applet wants to write and the channel is closed, it's a
1842 * broken pipe and it must be reported.
1843 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001844 if (!sc_ep_test(sc, SE_FL_HAVE_NO_DATA) && (ic->flags & CF_SHUTR))
1845 sc_ep_set(sc, SE_FL_ERROR);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001846
1847 /* automatically mark the applet having data available if it reported
1848 * begin blocked by the channel.
1849 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001850 if ((sc->flags & (SC_FL_WONT_READ|SC_FL_NEED_BUFF|SC_FL_NEED_ROOM)) ||
1851 sc_ep_test(sc, SE_FL_APPLET_NEED_CONN))
1852 applet_have_more_data(__sc_appctx(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001853
Willy Tarreau4596fe22022-05-17 19:07:51 +02001854 /* update the stream connector, channels, and possibly wake the stream up */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001855 sc_notify(sc);
1856 stream_release_buffers(__sc_strm(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001857
Willy Tarreau19c65a92022-05-27 08:49:24 +02001858 /* sc_notify may have passed through chk_snd and released some blocking
Willy Tarreau15252cd2022-05-25 16:36:21 +02001859 * flags. Process_stream will consider those flags to wake up the
Christopher Faulet5e29b762022-04-04 08:58:34 +02001860 * appctx but in the case the task is not in runqueue we may have to
1861 * wakeup the appctx immediately.
1862 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001863 if (sc_is_recv_allowed(sc) || sc_is_send_allowed(sc))
1864 appctx_wakeup(__sc_appctx(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001865 return 0;
Christopher Faulet13045f02022-04-01 14:23:38 +02001866}
Christopher Fauletb68f77d2022-06-16 16:24:16 +02001867
1868
1869/* Prepares an endpoint upgrade. We don't now at this stage if the upgrade will
1870 * succeed or not and if the stconn will be reused by the new endpoint. Thus,
1871 * for now, only pretend the stconn is detached.
1872 */
1873void sc_conn_prepare_endp_upgrade(struct stconn *sc)
1874{
1875 BUG_ON(!sc_conn(sc) || !sc->app);
1876 sc_ep_clr(sc, SE_FL_T_MUX);
1877 sc_ep_set(sc, SE_FL_DETACHED);
1878}
1879
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001880/* Endpoint upgrade failed. Restore the stconn state. */
Christopher Fauletb68f77d2022-06-16 16:24:16 +02001881void sc_conn_abort_endp_upgrade(struct stconn *sc)
1882{
1883 sc_ep_set(sc, SE_FL_T_MUX);
1884 sc_ep_clr(sc, SE_FL_DETACHED);
1885}
1886
1887/* Commit the endpoint upgrade. If stconn is attached, it means the new endpoint
1888 * use it. So we do nothing. Otherwise, the stconn will be destroy with the
1889 * overlying stream. So, it means we must commit the detach.
1890*/
1891void sc_conn_commit_endp_upgrade(struct stconn *sc)
1892{
1893 if (!sc_ep_test(sc, SE_FL_DETACHED))
1894 return;
1895 sc_detach_endp(&sc);
1896 /* Because it was already set as detached, the sedesc must be preserved */
Willy Tarreau6a378d12022-08-11 13:56:42 +02001897 BUG_ON(!sc);
Christopher Fauletb68f77d2022-06-16 16:24:16 +02001898 BUG_ON(!sc->sedesc);
1899}