blob: ca36e4958e6bcb772db1617a81604b40149d853e [file] [log] [blame]
Christopher Faulet1329f2a2021-12-16 17:32:56 +01001/*
Willy Tarreau4596fe22022-05-17 19:07:51 +02002 * stream connector management functions
Christopher Faulet1329f2a2021-12-16 17:32:56 +01003 *
4 * Copyright 2021 Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <haproxy/api.h>
Christopher Faulet37046632022-04-01 11:36:58 +020014#include <haproxy/applet.h>
Christopher Faulet1329f2a2021-12-16 17:32:56 +010015#include <haproxy/connection.h>
Christopher Faulet5e29b762022-04-04 08:58:34 +020016#include <haproxy/check.h>
17#include <haproxy/http_ana.h>
18#include <haproxy/pipe.h>
Christopher Faulet1329f2a2021-12-16 17:32:56 +010019#include <haproxy/pool.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020020#include <haproxy/sc_strm.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020021#include <haproxy/stconn.h>
Christopher Faulet1329f2a2021-12-16 17:32:56 +010022
Willy Tarreau4596fe22022-05-17 19:07:51 +020023DECLARE_POOL(pool_head_connstream, "stconn", sizeof(struct stconn));
Willy Tarreauea59b022022-05-17 17:53:22 +020024DECLARE_POOL(pool_head_sedesc, "sedesc", sizeof(struct sedesc));
Christopher Faulet1329f2a2021-12-16 17:32:56 +010025
Willy Tarreau3a3f4802022-05-17 18:28:19 +020026/* functions used by default on a detached stream connector */
Willy Tarreau0adb2812022-05-27 10:02:48 +020027static void sc_app_shutr(struct stconn *sc);
28static void sc_app_shutw(struct stconn *sc);
29static void sc_app_chk_rcv(struct stconn *sc);
30static void sc_app_chk_snd(struct stconn *sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +020031
Willy Tarreau3a3f4802022-05-17 18:28:19 +020032/* functions used on a mux-based stream connector */
Willy Tarreau0adb2812022-05-27 10:02:48 +020033static void sc_app_shutr_conn(struct stconn *sc);
34static void sc_app_shutw_conn(struct stconn *sc);
35static void sc_app_chk_rcv_conn(struct stconn *sc);
36static void sc_app_chk_snd_conn(struct stconn *sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +020037
Willy Tarreau3a3f4802022-05-17 18:28:19 +020038/* functions used on an applet-based stream connector */
Willy Tarreau0adb2812022-05-27 10:02:48 +020039static void sc_app_shutr_applet(struct stconn *sc);
40static void sc_app_shutw_applet(struct stconn *sc);
41static void sc_app_chk_rcv_applet(struct stconn *sc);
42static void sc_app_chk_snd_applet(struct stconn *sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +020043
Willy Tarreau0adb2812022-05-27 10:02:48 +020044static int sc_conn_process(struct stconn *sc);
45static int sc_conn_recv(struct stconn *sc);
46static int sc_conn_send(struct stconn *sc);
47static int sc_applet_process(struct stconn *sc);
Willy Tarreau2f2318d2022-05-18 10:17:16 +020048
Willy Tarreau3a3f4802022-05-17 18:28:19 +020049/* stream connector operations for connections */
50struct sc_app_ops sc_app_conn_ops = {
51 .chk_rcv = sc_app_chk_rcv_conn,
52 .chk_snd = sc_app_chk_snd_conn,
53 .shutr = sc_app_shutr_conn,
54 .shutw = sc_app_shutw_conn,
Willy Tarreau462b9892022-05-18 18:06:53 +020055 .wake = sc_conn_process,
Willy Tarreau2f2318d2022-05-18 10:17:16 +020056 .name = "STRM",
Christopher Faulet9ffddd52022-04-01 14:04:29 +020057};
58
Willy Tarreau3a3f4802022-05-17 18:28:19 +020059/* stream connector operations for embedded tasks */
60struct sc_app_ops sc_app_embedded_ops = {
61 .chk_rcv = sc_app_chk_rcv,
62 .chk_snd = sc_app_chk_snd,
63 .shutr = sc_app_shutr,
64 .shutw = sc_app_shutw,
Willy Tarreau2f2318d2022-05-18 10:17:16 +020065 .wake = NULL, /* may never be used */
66 .name = "NONE", /* may never be used */
Christopher Faulet9ffddd52022-04-01 14:04:29 +020067};
68
Willy Tarreau2f2318d2022-05-18 10:17:16 +020069/* stream connector operations for applets */
Willy Tarreau3a3f4802022-05-17 18:28:19 +020070struct sc_app_ops sc_app_applet_ops = {
71 .chk_rcv = sc_app_chk_rcv_applet,
72 .chk_snd = sc_app_chk_snd_applet,
73 .shutr = sc_app_shutr_applet,
74 .shutw = sc_app_shutw_applet,
Willy Tarreau19c65a92022-05-27 08:49:24 +020075 .wake = sc_applet_process,
Christopher Faulet5e29b762022-04-04 08:58:34 +020076 .name = "STRM",
77};
78
Willy Tarreau2f2318d2022-05-18 10:17:16 +020079/* stream connector for health checks on connections */
80struct sc_app_ops sc_app_check_ops = {
81 .chk_rcv = NULL,
82 .chk_snd = NULL,
83 .shutr = NULL,
84 .shutw = NULL,
85 .wake = wake_srv_chk,
86 .name = "CHCK",
87};
Christopher Faulet5e29b762022-04-04 08:58:34 +020088
Christopher Faulet9ed77422022-04-12 08:51:15 +020089/* Initializes an endpoint */
Willy Tarreauea59b022022-05-17 17:53:22 +020090void sedesc_init(struct sedesc *sedesc)
Christopher Fauletdb90f2a2022-03-22 16:06:25 +010091{
Willy Tarreauea59b022022-05-17 17:53:22 +020092 sedesc->se = NULL;
93 sedesc->conn = NULL;
Willy Tarreauc1054922022-05-18 07:43:52 +020094 sedesc->sc = NULL;
Willy Tarreauea59b022022-05-17 17:53:22 +020095 se_fl_setall(sedesc, SE_FL_NONE);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +010096}
97
Christopher Faulet9ed77422022-04-12 08:51:15 +020098/* Tries to alloc an endpoint and initialize it. Returns NULL on failure. */
Willy Tarreauea59b022022-05-17 17:53:22 +020099struct sedesc *sedesc_new()
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100100{
Willy Tarreauea59b022022-05-17 17:53:22 +0200101 struct sedesc *sedesc;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100102
Willy Tarreauea59b022022-05-17 17:53:22 +0200103 sedesc = pool_alloc(pool_head_sedesc);
104 if (unlikely(!sedesc))
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100105 return NULL;
106
Willy Tarreauea59b022022-05-17 17:53:22 +0200107 sedesc_init(sedesc);
108 return sedesc;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100109}
110
Christopher Faulet9ed77422022-04-12 08:51:15 +0200111/* Releases an endpoint. It is the caller responsibility to be sure it is safe
112 * and it is not shared with another entity
113 */
Willy Tarreauea59b022022-05-17 17:53:22 +0200114void sedesc_free(struct sedesc *sedesc)
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100115{
Willy Tarreauea59b022022-05-17 17:53:22 +0200116 pool_free(pool_head_sedesc, sedesc);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100117}
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100118
Willy Tarreau4596fe22022-05-17 19:07:51 +0200119/* Tries to allocate a new stconn and initialize its main fields. On
Christopher Faulet9ed77422022-04-12 08:51:15 +0200120 * failure, nothing is allocated and NULL is returned. It is an internal
Willy Tarreaub605c422022-05-17 17:04:55 +0200121 * function. The caller must, at least, set the SE_FL_ORPHAN or SE_FL_DETACHED
Christopher Faulet9ed77422022-04-12 08:51:15 +0200122 * flag.
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100123 */
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200124static struct stconn *sc_new(struct sedesc *sedesc)
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100125{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200126 struct stconn *sc;
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100127
Willy Tarreau0adb2812022-05-27 10:02:48 +0200128 sc = pool_alloc(pool_head_connstream);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100129
Willy Tarreau0adb2812022-05-27 10:02:48 +0200130 if (unlikely(!sc))
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100131 goto alloc_error;
Christopher Fauletbb772d02022-03-22 15:28:36 +0100132
Willy Tarreau1d2c79a2022-05-27 11:15:19 +0200133 sc->obj_type = OBJ_TYPE_SC;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200134 sc->flags = SC_FL_NONE;
135 sc->state = SC_ST_INI;
136 sc->hcto = TICK_ETERNITY;
137 sc->app = NULL;
138 sc->app_ops = NULL;
139 sc->src = NULL;
140 sc->dst = NULL;
141 sc->wait_event.tasklet = NULL;
142 sc->wait_event.events = 0;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200143
Christopher Faulet9ed77422022-04-12 08:51:15 +0200144 /* If there is no endpoint, allocate a new one now */
Willy Tarreauea59b022022-05-17 17:53:22 +0200145 if (!sedesc) {
146 sedesc = sedesc_new();
147 if (unlikely(!sedesc))
Christopher Fauletb669d682022-03-22 18:37:19 +0100148 goto alloc_error;
149 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200150 sc->sedesc = sedesc;
151 sedesc->sc = sc;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100152
Willy Tarreau0adb2812022-05-27 10:02:48 +0200153 return sc;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100154
155 alloc_error:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200156 pool_free(pool_head_connstream, sc);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100157 return NULL;
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100158}
159
Willy Tarreau31219282022-05-27 16:21:33 +0200160/* Creates a new stream connector and its associated stream from a mux. <sd> must
161 * be defined. It returns NULL on error. On success, the new stream connector is
Willy Tarreaub605c422022-05-17 17:04:55 +0200162 * returned. In this case, SE_FL_ORPHAN flag is removed.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200163 */
Willy Tarreau31219282022-05-27 16:21:33 +0200164struct stconn *sc_new_from_endp(struct sedesc *sd, struct session *sess, struct buffer *input)
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100165{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200166 struct stconn *sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100167
Willy Tarreau31219282022-05-27 16:21:33 +0200168 sc = sc_new(sd);
Willy Tarreau0adb2812022-05-27 10:02:48 +0200169 if (unlikely(!sc))
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100170 return NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200171 if (unlikely(!stream_new(sess, sc, input))) {
172 pool_free(pool_head_connstream, sc);
173 sc = NULL;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100174 }
Willy Tarreau31219282022-05-27 16:21:33 +0200175 se_fl_clr(sd, SE_FL_ORPHAN);
Willy Tarreau0adb2812022-05-27 10:02:48 +0200176 return sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100177}
178
Willy Tarreau4596fe22022-05-17 19:07:51 +0200179/* Creates a new stream connector from an stream. There is no endpoint here, thus it
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200180 * will be created by sc_new(). So the SE_FL_DETACHED flag is set. It returns
Willy Tarreau4596fe22022-05-17 19:07:51 +0200181 * NULL on error. On success, the new stream connector is returned.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200182 */
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200183struct stconn *sc_new_from_strm(struct stream *strm, unsigned int flags)
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100184{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200185 struct stconn *sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100186
Willy Tarreau0adb2812022-05-27 10:02:48 +0200187 sc = sc_new(NULL);
188 if (unlikely(!sc))
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100189 return NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200190 sc->flags |= flags;
191 sc_ep_set(sc, SE_FL_DETACHED);
192 sc->app = &strm->obj_type;
193 sc->app_ops = &sc_app_embedded_ops;
194 return sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100195}
196
Willy Tarreau4596fe22022-05-17 19:07:51 +0200197/* Creates a new stream connector from an health-check. There is no endpoint here,
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200198 * thus it will be created by sc_new(). So the SE_FL_DETACHED flag is set. It
Willy Tarreau4596fe22022-05-17 19:07:51 +0200199 * returns NULL on error. On success, the new stream connector is returned.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200200 */
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200201struct stconn *sc_new_from_check(struct check *check, unsigned int flags)
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100202{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200203 struct stconn *sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100204
Willy Tarreau0adb2812022-05-27 10:02:48 +0200205 sc = sc_new(NULL);
206 if (unlikely(!sc))
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100207 return NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200208 sc->flags |= flags;
209 sc_ep_set(sc, SE_FL_DETACHED);
210 sc->app = &check->obj_type;
211 sc->app_ops = &sc_app_check_ops;
212 return sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100213}
214
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200215/* Releases a stconn previously allocated by sc_new(), as well as its
Christopher Faulet9ed77422022-04-12 08:51:15 +0200216 * endpoint, if it exists. This function is called internally or on error path.
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100217 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200218void sc_free(struct stconn *sc)
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100219{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200220 sockaddr_free(&sc->src);
221 sockaddr_free(&sc->dst);
222 if (sc->sedesc) {
223 BUG_ON(!sc_ep_test(sc, SE_FL_DETACHED));
224 sedesc_free(sc->sedesc);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100225 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200226 if (sc->wait_event.tasklet)
227 tasklet_free(sc->wait_event.tasklet);
228 pool_free(pool_head_connstream, sc);
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100229}
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100230
Willy Tarreau4596fe22022-05-17 19:07:51 +0200231/* Conditionally removes a stream connector if it is detached and if there is no app
Christopher Fauleteb50c012022-04-21 14:22:53 +0200232 * layer defined. Except on error path, this one must be used. if release, the
Willy Tarreaue68bc612022-05-27 11:23:05 +0200233 * pointer on the SC is set to NULL.
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200234 */
Willy Tarreaue68bc612022-05-27 11:23:05 +0200235static void sc_free_cond(struct stconn **scp)
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200236{
Willy Tarreaue68bc612022-05-27 11:23:05 +0200237 struct stconn *sc = *scp;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200238
Willy Tarreau0adb2812022-05-27 10:02:48 +0200239 if (!sc->app && (!sc->sedesc || sc_ep_test(sc, SE_FL_DETACHED))) {
240 sc_free(sc);
Willy Tarreaue68bc612022-05-27 11:23:05 +0200241 *scp = NULL;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200242 }
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200243}
244
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100245
Willy Tarreau4596fe22022-05-17 19:07:51 +0200246/* Attaches a stconn to a mux endpoint and sets the endpoint ctx. Returns
Willy Tarreaub605c422022-05-17 17:04:55 +0200247 * -1 on error and 0 on sucess. SE_FL_DETACHED flag is removed. This function is
Christopher Faulet9ed77422022-04-12 08:51:15 +0200248 * called from a mux when it is attached to a stream or a health-check.
249 */
Willy Tarreau31219282022-05-27 16:21:33 +0200250int sc_attach_mux(struct stconn *sc, void *sd, void *ctx)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100251{
Christopher Faulet93882042022-01-19 14:56:50 +0100252 struct connection *conn = ctx;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200253 struct sedesc *sedesc = sc->sedesc;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100254
Willy Tarreau31219282022-05-27 16:21:33 +0200255 sedesc->se = sd;
Willy Tarreau798465b2022-05-17 18:20:02 +0200256 sedesc->conn = ctx;
257 se_fl_set(sedesc, SE_FL_T_MUX);
258 se_fl_clr(sedesc, SE_FL_DETACHED);
Christopher Faulet93882042022-01-19 14:56:50 +0100259 if (!conn->ctx)
Willy Tarreau0adb2812022-05-27 10:02:48 +0200260 conn->ctx = sc;
261 if (sc_strm(sc)) {
262 if (!sc->wait_event.tasklet) {
263 sc->wait_event.tasklet = tasklet_new();
264 if (!sc->wait_event.tasklet)
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200265 return -1;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200266 sc->wait_event.tasklet->process = sc_conn_io_cb;
267 sc->wait_event.tasklet->context = sc;
268 sc->wait_event.events = 0;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200269 }
270
Willy Tarreau0adb2812022-05-27 10:02:48 +0200271 sc->app_ops = &sc_app_conn_ops;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100272 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200273 else if (sc_check(sc)) {
274 if (!sc->wait_event.tasklet) {
275 sc->wait_event.tasklet = tasklet_new();
276 if (!sc->wait_event.tasklet)
Christopher Fauletc95eaef2022-05-18 15:57:15 +0200277 return -1;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200278 sc->wait_event.tasklet->process = srv_chk_io_cb;
279 sc->wait_event.tasklet->context = sc;
280 sc->wait_event.events = 0;
Christopher Fauletc95eaef2022-05-18 15:57:15 +0200281 }
282
Willy Tarreau0adb2812022-05-27 10:02:48 +0200283 sc->app_ops = &sc_app_check_ops;
Christopher Fauletc95eaef2022-05-18 15:57:15 +0200284 }
Christopher Faulet070b91b2022-03-31 19:27:18 +0200285 return 0;
Christopher Faulet93882042022-01-19 14:56:50 +0100286}
287
Willy Tarreau4596fe22022-05-17 19:07:51 +0200288/* Attaches a stconn to an applet endpoint and sets the endpoint
Willy Tarreaub605c422022-05-17 17:04:55 +0200289 * ctx. Returns -1 on error and 0 on sucess. SE_FL_DETACHED flag is
Christopher Faulet9ed77422022-04-12 08:51:15 +0200290 * removed. This function is called by a stream when a backend applet is
291 * registered.
292 */
Willy Tarreau31219282022-05-27 16:21:33 +0200293static void sc_attach_applet(struct stconn *sc, void *sd)
Christopher Faulet93882042022-01-19 14:56:50 +0100294{
Willy Tarreau31219282022-05-27 16:21:33 +0200295 sc->sedesc->se = sd;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200296 sc_ep_set(sc, SE_FL_T_APPLET);
297 sc_ep_clr(sc, SE_FL_DETACHED);
298 if (sc_strm(sc))
299 sc->app_ops = &sc_app_applet_ops;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100300}
301
Willy Tarreau4596fe22022-05-17 19:07:51 +0200302/* Attaches a stconn to a app layer and sets the relevant
Willy Tarreaub605c422022-05-17 17:04:55 +0200303 * callbacks. Returns -1 on error and 0 on success. SE_FL_ORPHAN flag is
Christopher Faulet9ed77422022-04-12 08:51:15 +0200304 * removed. This function is called by a stream when it is created to attach it
Willy Tarreau4596fe22022-05-17 19:07:51 +0200305 * on the stream connector on the client side.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200306 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200307int sc_attach_strm(struct stconn *sc, struct stream *strm)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100308{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200309 sc->app = &strm->obj_type;
310 sc_ep_clr(sc, SE_FL_ORPHAN);
311 if (sc_ep_test(sc, SE_FL_T_MUX)) {
312 sc->wait_event.tasklet = tasklet_new();
313 if (!sc->wait_event.tasklet)
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200314 return -1;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200315 sc->wait_event.tasklet->process = sc_conn_io_cb;
316 sc->wait_event.tasklet->context = sc;
317 sc->wait_event.events = 0;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200318
Willy Tarreau0adb2812022-05-27 10:02:48 +0200319 sc->app_ops = &sc_app_conn_ops;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100320 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200321 else if (sc_ep_test(sc, SE_FL_T_APPLET)) {
322 sc->app_ops = &sc_app_applet_ops;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100323 }
324 else {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200325 sc->app_ops = &sc_app_embedded_ops;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100326 }
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100327 return 0;
328}
329
Willy Tarreau4596fe22022-05-17 19:07:51 +0200330/* Detaches the stconn from the endpoint, if any. For a connecrion, if a
Christopher Faulet9ed77422022-04-12 08:51:15 +0200331 * mux owns the connection ->detach() callback is called. Otherwise, it means
Willy Tarreau4596fe22022-05-17 19:07:51 +0200332 * the stream connector owns the connection. In this case the connection is closed
Christopher Faulet9ed77422022-04-12 08:51:15 +0200333 * and released. For an applet, the appctx is released. If still allocated, the
334 * endpoint is reset and flag as detached. If the app layer is also detached,
Willy Tarreau4596fe22022-05-17 19:07:51 +0200335 * the stream connector is released.
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100336 */
Willy Tarreaue68bc612022-05-27 11:23:05 +0200337static void sc_detach_endp(struct stconn **scp)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100338{
Willy Tarreaue68bc612022-05-27 11:23:05 +0200339 struct stconn *sc = *scp;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200340
Willy Tarreau0adb2812022-05-27 10:02:48 +0200341 if (!sc)
Christopher Fauleteb50c012022-04-21 14:22:53 +0200342 return;
343
Willy Tarreau0adb2812022-05-27 10:02:48 +0200344 if (sc_ep_test(sc, SE_FL_T_MUX)) {
345 struct connection *conn = __sc_conn(sc);
346 struct sedesc *sedesc = sc->sedesc;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100347
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100348 if (conn->mux) {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200349 if (sc->wait_event.events != 0)
350 conn->mux->unsubscribe(sc, sc->wait_event.events, &sc->wait_event);
Willy Tarreau798465b2022-05-17 18:20:02 +0200351 se_fl_set(sedesc, SE_FL_ORPHAN);
Willy Tarreauc1054922022-05-18 07:43:52 +0200352 sedesc->sc = NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200353 sc->sedesc = NULL;
Willy Tarreau798465b2022-05-17 18:20:02 +0200354 conn->mux->detach(sedesc);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100355 }
356 else {
357 /* It's too early to have a mux, let's just destroy
358 * the connection
359 */
360 conn_stop_tracking(conn);
361 conn_full_close(conn);
362 if (conn->destroy_cb)
363 conn->destroy_cb(conn);
364 conn_free(conn);
365 }
366 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200367 else if (sc_ep_test(sc, SE_FL_T_APPLET)) {
368 struct appctx *appctx = __sc_appctx(sc);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100369
Willy Tarreau0adb2812022-05-27 10:02:48 +0200370 sc_ep_set(sc, SE_FL_ORPHAN);
371 sc->sedesc->sc = NULL;
372 sc->sedesc = NULL;
Willy Tarreau1c3ead42022-05-10 19:42:22 +0200373 appctx_shut(appctx);
374 appctx_free(appctx);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100375 }
376
Willy Tarreau0adb2812022-05-27 10:02:48 +0200377 if (sc->sedesc) {
Willy Tarreauda59c892022-05-27 17:03:34 +0200378 /* the SD wasn't used and can be recycled */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200379 sc->sedesc->se = NULL;
380 sc->sedesc->conn = NULL;
Willy Tarreauda59c892022-05-27 17:03:34 +0200381 sc->sedesc->flags = 0;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200382 sc_ep_set(sc, SE_FL_DETACHED);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100383 }
384
Willy Tarreaue68bc612022-05-27 11:23:05 +0200385 /* FIXME: Rest SC for now but must be reviewed. SC flags are only
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100386 * connection related for now but this will evolved
387 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200388 sc->flags &= SC_FL_ISBACK;
389 if (sc_strm(sc))
390 sc->app_ops = &sc_app_embedded_ops;
Willy Tarreau2f2318d2022-05-18 10:17:16 +0200391 else
Willy Tarreau0adb2812022-05-27 10:02:48 +0200392 sc->app_ops = NULL;
Willy Tarreaue68bc612022-05-27 11:23:05 +0200393 sc_free_cond(scp);
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100394}
395
Willy Tarreau4596fe22022-05-17 19:07:51 +0200396/* Detaches the stconn from the app layer. If there is no endpoint attached
397 * to the stconn
Christopher Faulet9ed77422022-04-12 08:51:15 +0200398 */
Willy Tarreaue68bc612022-05-27 11:23:05 +0200399static void sc_detach_app(struct stconn **scp)
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100400{
Willy Tarreaue68bc612022-05-27 11:23:05 +0200401 struct stconn *sc = *scp;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200402
Willy Tarreau0adb2812022-05-27 10:02:48 +0200403 if (!sc)
Christopher Fauleteb50c012022-04-21 14:22:53 +0200404 return;
405
Willy Tarreau0adb2812022-05-27 10:02:48 +0200406 sc->app = NULL;
407 sc->app_ops = NULL;
408 sockaddr_free(&sc->src);
409 sockaddr_free(&sc->dst);
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200410
Willy Tarreau0adb2812022-05-27 10:02:48 +0200411 if (sc->wait_event.tasklet)
412 tasklet_free(sc->wait_event.tasklet);
413 sc->wait_event.tasklet = NULL;
414 sc->wait_event.events = 0;
Willy Tarreaue68bc612022-05-27 11:23:05 +0200415 sc_free_cond(scp);
Christopher Fauleteb50c012022-04-21 14:22:53 +0200416}
417
Willy Tarreau4596fe22022-05-17 19:07:51 +0200418/* Destroy the stconn. It is detached from its endpoint and its
419 * application. After this call, the stconn must be considered as released.
Christopher Fauleteb50c012022-04-21 14:22:53 +0200420 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200421void sc_destroy(struct stconn *sc)
Christopher Fauleteb50c012022-04-21 14:22:53 +0200422{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200423 sc_detach_endp(&sc);
424 sc_detach_app(&sc);
425 BUG_ON_HOT(sc);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100426}
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100427
Willy Tarreau4596fe22022-05-17 19:07:51 +0200428/* Resets the stream connector endpoint. It happens when the app layer want to renew
Christopher Faulet9ed77422022-04-12 08:51:15 +0200429 * its endpoint. For a connection retry for instance. If a mux or an applet is
430 * attached, a new endpoint is created. Returns -1 on error and 0 on sucess.
Christopher Fauleta6c4a482022-04-28 18:25:24 +0200431 *
Willy Tarreaub605c422022-05-17 17:04:55 +0200432 * Only SE_FL_ERROR flag is removed on the endpoint. Orther flags are preserved.
Christopher Fauleta6c4a482022-04-28 18:25:24 +0200433 * It is the caller responsibility to remove other flags if needed.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200434 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200435int sc_reset_endp(struct stconn *sc)
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100436{
Willy Tarreau31219282022-05-27 16:21:33 +0200437 struct sedesc *new_sd;
Christopher Fauletb041b232022-03-24 10:27:02 +0100438
Willy Tarreau0adb2812022-05-27 10:02:48 +0200439 BUG_ON(!sc->app);
Christopher Fauleta6c4a482022-04-28 18:25:24 +0200440
Willy Tarreau0adb2812022-05-27 10:02:48 +0200441 sc_ep_clr(sc, SE_FL_ERROR);
442 if (!__sc_endp(sc)) {
Christopher Fauletb041b232022-03-24 10:27:02 +0100443 /* endpoint not attached or attached to a mux with no
444 * target. Thus the endpoint will not be release but just
Willy Tarreau0adb2812022-05-27 10:02:48 +0200445 * reset. The app is still attached, the sc will not be
Christopher Fauleteb50c012022-04-21 14:22:53 +0200446 * released.
Christopher Fauletb041b232022-03-24 10:27:02 +0100447 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200448 sc_detach_endp(&sc);
Christopher Fauletb041b232022-03-24 10:27:02 +0100449 return 0;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100450 }
Christopher Fauletb041b232022-03-24 10:27:02 +0100451
452 /* allocate the new endpoint first to be able to set error if it
453 * fails */
Willy Tarreau31219282022-05-27 16:21:33 +0200454 new_sd = sedesc_new();
455 if (!unlikely(new_sd)) {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200456 sc_ep_set(sc, SE_FL_ERROR);
Christopher Fauletb041b232022-03-24 10:27:02 +0100457 return -1;
458 }
459
Willy Tarreau0adb2812022-05-27 10:02:48 +0200460 /* The app is still attached, the sc will not be released */
461 sc_detach_endp(&sc);
462 BUG_ON(sc->sedesc);
Willy Tarreau31219282022-05-27 16:21:33 +0200463 sc->sedesc = new_sd;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200464 sc->sedesc->sc = sc;
465 sc_ep_set(sc, SE_FL_DETACHED);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100466 return 0;
467}
Christopher Faulet37046632022-04-01 11:36:58 +0200468
469
Willy Tarreaue68bc612022-05-27 11:23:05 +0200470/* Create an applet to handle a stream connector as a new appctx. The SC will
Christopher Faulet37046632022-04-01 11:36:58 +0200471 * wake it up every time it is solicited. The appctx must be deleted by the task
Willy Tarreau19c65a92022-05-27 08:49:24 +0200472 * handler using sc_detach_endp(), possibly from within the function itself.
Christopher Faulet37046632022-04-01 11:36:58 +0200473 * It also pre-initializes the applet's context and returns it (or NULL in case
474 * it could not be allocated).
475 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200476struct appctx *sc_applet_create(struct stconn *sc, struct applet *app)
Christopher Faulet37046632022-04-01 11:36:58 +0200477{
478 struct appctx *appctx;
479
Willy Tarreau0adb2812022-05-27 10:02:48 +0200480 DPRINTF(stderr, "registering handler %p for sc %p (was %p)\n", app, sc, sc_strm_task(sc));
Christopher Faulet37046632022-04-01 11:36:58 +0200481
Willy Tarreau0adb2812022-05-27 10:02:48 +0200482 appctx = appctx_new_here(app, sc->sedesc);
Christopher Faulet37046632022-04-01 11:36:58 +0200483 if (!appctx)
484 return NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200485 sc_attach_applet(sc, appctx);
486 appctx->t->nice = __sc_strm(sc)->task->nice;
Willy Tarreau90e8b452022-05-25 18:21:43 +0200487 applet_need_more_data(appctx);
Christopher Faulet37046632022-04-01 11:36:58 +0200488 appctx_wakeup(appctx);
Christopher Fauleta33ff7a2022-04-21 11:52:07 +0200489
Willy Tarreau0adb2812022-05-27 10:02:48 +0200490 sc->state = SC_ST_RDY;
Christopher Faulet37046632022-04-01 11:36:58 +0200491 return appctx;
492}
493
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200494/*
Willy Tarreau4596fe22022-05-17 19:07:51 +0200495 * This function performs a shutdown-read on a detached stream connector in a
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200496 * connected or init state (it does nothing for other states). It either shuts
497 * the read side or marks itself as closed. The buffer flags are updated to
Willy Tarreaucb041662022-05-17 19:44:42 +0200498 * reflect the new state. If the stream connector has SC_FL_NOHALF, we also
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200499 * forward the close to the write side. The owner task is woken up if it exists.
500 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200501static void sc_app_shutr(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200502{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200503 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200504
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200505 if (ic->flags & CF_SHUTR)
506 return;
507 ic->flags |= CF_SHUTR;
508 ic->rex = TICK_ETERNITY;
509
Willy Tarreau0adb2812022-05-27 10:02:48 +0200510 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200511 return;
512
Willy Tarreau0adb2812022-05-27 10:02:48 +0200513 if (sc_oc(sc)->flags & CF_SHUTW) {
514 sc->state = SC_ST_DIS;
Christopher Fauletca679922022-07-20 13:24:04 +0200515 if (sc->flags & SC_FL_ISBACK)
516 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200517 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200518 else if (sc->flags & SC_FL_NOHALF) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200519 /* we want to immediately forward this close to the write side */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200520 return sc_app_shutw(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200521 }
522
523 /* note that if the task exists, it must unregister itself once it runs */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200524 if (!(sc->flags & SC_FL_DONT_WAKE))
525 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200526}
527
528/*
Willy Tarreau4596fe22022-05-17 19:07:51 +0200529 * This function performs a shutdown-write on a detached stream connector in a
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200530 * connected or init state (it does nothing for other states). It either shuts
531 * the write side or marks itself as closed. The buffer flags are updated to
Willy Tarreaue68bc612022-05-27 11:23:05 +0200532 * reflect the new state. It does also close everything if the SC was marked as
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200533 * being in error state. The owner task is woken up if it exists.
534 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200535static void sc_app_shutw(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200536{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200537 struct channel *ic = sc_ic(sc);
538 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200539
540 oc->flags &= ~CF_SHUTW_NOW;
541 if (oc->flags & CF_SHUTW)
542 return;
543 oc->flags |= CF_SHUTW;
544 oc->wex = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200545
Willy Tarreau0adb2812022-05-27 10:02:48 +0200546 if (tick_isset(sc->hcto)) {
547 ic->rto = sc->hcto;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200548 ic->rex = tick_add(now_ms, ic->rto);
549 }
550
Willy Tarreau0adb2812022-05-27 10:02:48 +0200551 switch (sc->state) {
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200552 case SC_ST_RDY:
553 case SC_ST_EST:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200554 /* we have to shut before closing, otherwise some short messages
555 * may never leave the system, especially when there are remaining
556 * unread data in the socket input buffer, or when nolinger is set.
Willy Tarreaucb041662022-05-17 19:44:42 +0200557 * However, if SC_FL_NOLINGER is explicitly set, we know there is
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200558 * no risk so we close both sides immediately.
559 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200560 if (!sc_ep_test(sc, SE_FL_ERROR) && !(sc->flags & SC_FL_NOLINGER) &&
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200561 !(ic->flags & (CF_SHUTR|CF_DONT_READ)))
562 return;
563
564 /* fall through */
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200565 case SC_ST_CON:
566 case SC_ST_CER:
567 case SC_ST_QUE:
568 case SC_ST_TAR:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200569 /* Note that none of these states may happen with applets */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200570 sc->state = SC_ST_DIS;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200571 /* fall through */
572 default:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200573 sc->flags &= ~SC_FL_NOLINGER;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200574 ic->flags |= CF_SHUTR;
575 ic->rex = TICK_ETERNITY;
Christopher Fauletca679922022-07-20 13:24:04 +0200576 if (sc->flags & SC_FL_ISBACK)
577 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200578 }
579
580 /* note that if the task exists, it must unregister itself once it runs */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200581 if (!(sc->flags & SC_FL_DONT_WAKE))
582 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200583}
584
585/* default chk_rcv function for scheduled tasks */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200586static void sc_app_chk_rcv(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200587{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200588 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200589
Willy Tarreau0adb2812022-05-27 10:02:48 +0200590 DPRINTF(stderr, "%s: sc=%p, sc->state=%d ic->flags=%08x oc->flags=%08x\n",
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200591 __FUNCTION__,
Willy Tarreau0adb2812022-05-27 10:02:48 +0200592 sc, sc->state, ic->flags, sc_oc(sc)->flags);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200593
594 if (ic->pipe) {
595 /* stop reading */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200596 sc_need_room(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200597 }
598 else {
599 /* (re)start reading */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200600 if (!(sc->flags & SC_FL_DONT_WAKE))
601 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200602 }
603}
604
605/* default chk_snd function for scheduled tasks */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200606static void sc_app_chk_snd(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200607{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200608 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200609
Willy Tarreau0adb2812022-05-27 10:02:48 +0200610 DPRINTF(stderr, "%s: sc=%p, sc->state=%d ic->flags=%08x oc->flags=%08x\n",
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200611 __FUNCTION__,
Willy Tarreau0adb2812022-05-27 10:02:48 +0200612 sc, sc->state, sc_ic(sc)->flags, oc->flags);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200613
Willy Tarreau0adb2812022-05-27 10:02:48 +0200614 if (unlikely(sc->state != SC_ST_EST || (oc->flags & CF_SHUTW)))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200615 return;
616
Willy Tarreau0adb2812022-05-27 10:02:48 +0200617 if (!sc_ep_test(sc, SE_FL_WAIT_DATA) || /* not waiting for data */
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200618 channel_is_empty(oc)) /* called with nothing to send ! */
619 return;
620
621 /* Otherwise there are remaining data to be sent in the buffer,
622 * so we tell the handler.
623 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200624 sc_ep_clr(sc, SE_FL_WAIT_DATA);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200625 if (!tick_isset(oc->wex))
626 oc->wex = tick_add_ifset(now_ms, oc->wto);
627
Willy Tarreau0adb2812022-05-27 10:02:48 +0200628 if (!(sc->flags & SC_FL_DONT_WAKE))
629 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200630}
631
632/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200633 * This function performs a shutdown-read on a stream connector attached to
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200634 * a connection in a connected or init state (it does nothing for other
635 * states). It either shuts the read side or marks itself as closed. The buffer
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200636 * flags are updated to reflect the new state. If the stream connector has
Willy Tarreaucb041662022-05-17 19:44:42 +0200637 * SC_FL_NOHALF, we also forward the close to the write side. If a control
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200638 * layer is defined, then it is supposed to be a socket layer and file
639 * descriptors are then shutdown or closed accordingly. The function
640 * automatically disables polling if needed.
641 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200642static void sc_app_shutr_conn(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200643{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200644 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200645
Willy Tarreau0adb2812022-05-27 10:02:48 +0200646 BUG_ON(!sc_conn(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200647
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200648 if (ic->flags & CF_SHUTR)
649 return;
650 ic->flags |= CF_SHUTR;
651 ic->rex = TICK_ETERNITY;
652
Willy Tarreau0adb2812022-05-27 10:02:48 +0200653 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200654 return;
655
Willy Tarreau0adb2812022-05-27 10:02:48 +0200656 if (sc_oc(sc)->flags & CF_SHUTW) {
657 sc_conn_shut(sc);
658 sc->state = SC_ST_DIS;
Christopher Fauletca679922022-07-20 13:24:04 +0200659 if (sc->flags & SC_FL_ISBACK)
660 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200661 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200662 else if (sc->flags & SC_FL_NOHALF) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200663 /* we want to immediately forward this close to the write side */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200664 return sc_app_shutw_conn(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200665 }
666}
667
668/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200669 * This function performs a shutdown-write on a stream connector attached to
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200670 * a connection in a connected or init state (it does nothing for other
671 * states). It either shuts the write side or marks itself as closed. The
672 * buffer flags are updated to reflect the new state. It does also close
Willy Tarreaue68bc612022-05-27 11:23:05 +0200673 * everything if the SC was marked as being in error state. If there is a
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200674 * data-layer shutdown, it is called.
675 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200676static void sc_app_shutw_conn(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200677{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200678 struct channel *ic = sc_ic(sc);
679 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200680
Willy Tarreau0adb2812022-05-27 10:02:48 +0200681 BUG_ON(!sc_conn(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200682
683 oc->flags &= ~CF_SHUTW_NOW;
684 if (oc->flags & CF_SHUTW)
685 return;
686 oc->flags |= CF_SHUTW;
687 oc->wex = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200688
Willy Tarreau0adb2812022-05-27 10:02:48 +0200689 if (tick_isset(sc->hcto)) {
690 ic->rto = sc->hcto;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200691 ic->rex = tick_add(now_ms, ic->rto);
692 }
693
Willy Tarreau0adb2812022-05-27 10:02:48 +0200694 switch (sc->state) {
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200695 case SC_ST_RDY:
696 case SC_ST_EST:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200697 /* we have to shut before closing, otherwise some short messages
698 * may never leave the system, especially when there are remaining
699 * unread data in the socket input buffer, or when nolinger is set.
Willy Tarreaucb041662022-05-17 19:44:42 +0200700 * However, if SC_FL_NOLINGER is explicitly set, we know there is
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200701 * no risk so we close both sides immediately.
702 */
703
Willy Tarreau0adb2812022-05-27 10:02:48 +0200704 if (sc_ep_test(sc, SE_FL_ERROR)) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200705 /* quick close, the socket is already shut anyway */
706 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200707 else if (sc->flags & SC_FL_NOLINGER) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200708 /* unclean data-layer shutdown, typically an aborted request
709 * or a forwarded shutdown from a client to a server due to
710 * option abortonclose. No need for the TLS layer to try to
711 * emit a shutdown message.
712 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200713 sc_conn_shutw(sc, CO_SHW_SILENT);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200714 }
715 else {
716 /* clean data-layer shutdown. This only happens on the
717 * frontend side, or on the backend side when forwarding
718 * a client close in TCP mode or in HTTP TUNNEL mode
719 * while option abortonclose is set. We want the TLS
720 * layer to try to signal it to the peer before we close.
721 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200722 sc_conn_shutw(sc, CO_SHW_NORMAL);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200723
724 if (!(ic->flags & (CF_SHUTR|CF_DONT_READ)))
725 return;
726 }
727
728 /* fall through */
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200729 case SC_ST_CON:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200730 /* we may have to close a pending connection, and mark the
731 * response buffer as shutr
732 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200733 sc_conn_shut(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200734 /* fall through */
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200735 case SC_ST_CER:
736 case SC_ST_QUE:
737 case SC_ST_TAR:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200738 sc->state = SC_ST_DIS;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200739 /* fall through */
740 default:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200741 sc->flags &= ~SC_FL_NOLINGER;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200742 ic->flags |= CF_SHUTR;
743 ic->rex = TICK_ETERNITY;
Christopher Fauletca679922022-07-20 13:24:04 +0200744 if (sc->flags & SC_FL_ISBACK)
745 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200746 }
747}
748
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200749/* This function is used for inter-stream connector calls. It is called by the
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200750 * consumer to inform the producer side that it may be interested in checking
751 * for free space in the buffer. Note that it intentionally does not update
752 * timeouts, so that we can still check them later at wake-up. This function is
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200753 * dedicated to connection-based stream connectors.
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200754 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200755static void sc_app_chk_rcv_conn(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200756{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200757 BUG_ON(!sc_conn(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200758
759 /* (re)start reading */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200760 if (sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
761 tasklet_wakeup(sc->wait_event.tasklet);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200762}
763
764
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200765/* This function is used for inter-stream connector calls. It is called by the
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200766 * producer to inform the consumer side that it may be interested in checking
767 * for data in the buffer. Note that it intentionally does not update timeouts,
768 * so that we can still check them later at wake-up.
769 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200770static void sc_app_chk_snd_conn(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200771{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200772 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200773
Willy Tarreau0adb2812022-05-27 10:02:48 +0200774 BUG_ON(!sc_conn(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200775
Willy Tarreau0adb2812022-05-27 10:02:48 +0200776 if (unlikely(!sc_state_in(sc->state, SC_SB_RDY|SC_SB_EST) ||
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200777 (oc->flags & CF_SHUTW)))
778 return;
779
780 if (unlikely(channel_is_empty(oc))) /* called with nothing to send ! */
781 return;
782
783 if (!oc->pipe && /* spliced data wants to be forwarded ASAP */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200784 !sc_ep_test(sc, SE_FL_WAIT_DATA)) /* not waiting for data */
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200785 return;
786
Willy Tarreau0adb2812022-05-27 10:02:48 +0200787 if (!(sc->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(sc_oc(sc)))
788 sc_conn_send(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200789
Willy Tarreau0adb2812022-05-27 10:02:48 +0200790 if (sc_ep_test(sc, SE_FL_ERROR | SE_FL_ERR_PENDING) || sc_is_conn_error(sc)) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200791 /* Write error on the file descriptor */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200792 if (sc->state >= SC_ST_CON)
793 sc_ep_set(sc, SE_FL_ERROR);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200794 goto out_wakeup;
795 }
796
797 /* OK, so now we know that some data might have been sent, and that we may
798 * have to poll first. We have to do that too if the buffer is not empty.
799 */
800 if (channel_is_empty(oc)) {
801 /* the connection is established but we can't write. Either the
802 * buffer is empty, or we just refrain from sending because the
803 * ->o limit was reached. Maybe we just wrote the last
804 * chunk and need to close.
805 */
806 if (((oc->flags & (CF_SHUTW|CF_AUTO_CLOSE|CF_SHUTW_NOW)) ==
807 (CF_AUTO_CLOSE|CF_SHUTW_NOW)) &&
Willy Tarreau0adb2812022-05-27 10:02:48 +0200808 sc_state_in(sc->state, SC_SB_RDY|SC_SB_EST)) {
809 sc_shutw(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200810 goto out_wakeup;
811 }
812
813 if ((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == 0)
Willy Tarreau0adb2812022-05-27 10:02:48 +0200814 sc_ep_set(sc, SE_FL_WAIT_DATA);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200815 oc->wex = TICK_ETERNITY;
816 }
817 else {
818 /* Otherwise there are remaining data to be sent in the buffer,
819 * which means we have to poll before doing so.
820 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200821 sc_ep_clr(sc, SE_FL_WAIT_DATA);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200822 if (!tick_isset(oc->wex))
823 oc->wex = tick_add_ifset(now_ms, oc->wto);
824 }
825
826 if (likely(oc->flags & CF_WRITE_ACTIVITY)) {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200827 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200828
829 /* update timeout if we have written something */
830 if ((oc->flags & (CF_SHUTW|CF_WRITE_PARTIAL)) == CF_WRITE_PARTIAL &&
831 !channel_is_empty(oc))
832 oc->wex = tick_add_ifset(now_ms, oc->wto);
833
Willy Tarreau0adb2812022-05-27 10:02:48 +0200834 if (tick_isset(ic->rex) && !(sc->flags & SC_FL_INDEP_STR)) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200835 /* Note: to prevent the client from expiring read timeouts
836 * during writes, we refresh it. We only do this if the
837 * interface is not configured for "independent streams",
838 * because for some applications it's better not to do this,
839 * for instance when continuously exchanging small amounts
840 * of data which can full the socket buffers long before a
841 * write timeout is detected.
842 */
843 ic->rex = tick_add_ifset(now_ms, ic->rto);
844 }
845 }
846
847 /* in case of special condition (error, shutdown, end of write...), we
848 * have to notify the task.
849 */
850 if (likely((oc->flags & (CF_WRITE_NULL|CF_WRITE_ERROR|CF_SHUTW)) ||
851 ((oc->flags & CF_WAKE_WRITE) &&
852 ((channel_is_empty(oc) && !oc->to_forward) ||
Willy Tarreau0adb2812022-05-27 10:02:48 +0200853 !sc_state_in(sc->state, SC_SB_EST))))) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200854 out_wakeup:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200855 if (!(sc->flags & SC_FL_DONT_WAKE))
856 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200857 }
858}
859
860/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200861 * This function performs a shutdown-read on a stream connector attached to an
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200862 * applet in a connected or init state (it does nothing for other states). It
863 * either shuts the read side or marks itself as closed. The buffer flags are
Willy Tarreaucb041662022-05-17 19:44:42 +0200864 * updated to reflect the new state. If the stream connector has SC_FL_NOHALF,
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200865 * we also forward the close to the write side. The owner task is woken up if
866 * it exists.
867 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200868static void sc_app_shutr_applet(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200869{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200870 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200871
Willy Tarreau0adb2812022-05-27 10:02:48 +0200872 BUG_ON(!sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200873
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200874 if (ic->flags & CF_SHUTR)
875 return;
876 ic->flags |= CF_SHUTR;
877 ic->rex = TICK_ETERNITY;
878
879 /* Note: on shutr, we don't call the applet */
880
Willy Tarreau0adb2812022-05-27 10:02:48 +0200881 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200882 return;
883
Willy Tarreau0adb2812022-05-27 10:02:48 +0200884 if (sc_oc(sc)->flags & CF_SHUTW) {
885 appctx_shut(__sc_appctx(sc));
886 sc->state = SC_ST_DIS;
Christopher Fauletca679922022-07-20 13:24:04 +0200887 if (sc->flags & SC_FL_ISBACK)
888 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200889 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200890 else if (sc->flags & SC_FL_NOHALF) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200891 /* we want to immediately forward this close to the write side */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200892 return sc_app_shutw_applet(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200893 }
894}
895
896/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200897 * This function performs a shutdown-write on a stream connector attached to an
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200898 * applet in a connected or init state (it does nothing for other states). It
899 * either shuts the write side or marks itself as closed. The buffer flags are
900 * updated to reflect the new state. It does also close everything if the SI
901 * was marked as being in error state. The owner task is woken up if it exists.
902 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200903static void sc_app_shutw_applet(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200904{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200905 struct channel *ic = sc_ic(sc);
906 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200907
Willy Tarreau0adb2812022-05-27 10:02:48 +0200908 BUG_ON(!sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200909
910 oc->flags &= ~CF_SHUTW_NOW;
911 if (oc->flags & CF_SHUTW)
912 return;
913 oc->flags |= CF_SHUTW;
914 oc->wex = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200915
Willy Tarreau0adb2812022-05-27 10:02:48 +0200916 if (tick_isset(sc->hcto)) {
917 ic->rto = sc->hcto;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200918 ic->rex = tick_add(now_ms, ic->rto);
919 }
920
921 /* on shutw we always wake the applet up */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200922 appctx_wakeup(__sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200923
Willy Tarreau0adb2812022-05-27 10:02:48 +0200924 switch (sc->state) {
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200925 case SC_ST_RDY:
926 case SC_ST_EST:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200927 /* we have to shut before closing, otherwise some short messages
928 * may never leave the system, especially when there are remaining
929 * unread data in the socket input buffer, or when nolinger is set.
Willy Tarreaucb041662022-05-17 19:44:42 +0200930 * However, if SC_FL_NOLINGER is explicitly set, we know there is
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200931 * no risk so we close both sides immediately.
932 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200933 if (!sc_ep_test(sc, SE_FL_ERROR) && !(sc->flags & SC_FL_NOLINGER) &&
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200934 !(ic->flags & (CF_SHUTR|CF_DONT_READ)))
935 return;
936
937 /* fall through */
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200938 case SC_ST_CON:
939 case SC_ST_CER:
940 case SC_ST_QUE:
941 case SC_ST_TAR:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200942 /* Note that none of these states may happen with applets */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200943 appctx_shut(__sc_appctx(sc));
944 sc->state = SC_ST_DIS;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200945 /* fall through */
946 default:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200947 sc->flags &= ~SC_FL_NOLINGER;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200948 ic->flags |= CF_SHUTR;
949 ic->rex = TICK_ETERNITY;
Christopher Fauletca679922022-07-20 13:24:04 +0200950 if (sc->flags & SC_FL_ISBACK)
951 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200952 }
953}
954
955/* chk_rcv function for applets */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200956static void sc_app_chk_rcv_applet(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200957{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200958 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200959
Willy Tarreau0adb2812022-05-27 10:02:48 +0200960 BUG_ON(!sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200961
Willy Tarreau0adb2812022-05-27 10:02:48 +0200962 DPRINTF(stderr, "%s: sc=%p, sc->state=%d ic->flags=%08x oc->flags=%08x\n",
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200963 __FUNCTION__,
Willy Tarreau0adb2812022-05-27 10:02:48 +0200964 sc, sc->state, ic->flags, sc_oc(sc)->flags);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200965
966 if (!ic->pipe) {
967 /* (re)start reading */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200968 appctx_wakeup(__sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200969 }
970}
971
972/* chk_snd function for applets */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200973static void sc_app_chk_snd_applet(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200974{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200975 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200976
Willy Tarreau0adb2812022-05-27 10:02:48 +0200977 BUG_ON(!sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200978
Willy Tarreau0adb2812022-05-27 10:02:48 +0200979 DPRINTF(stderr, "%s: sc=%p, sc->state=%d ic->flags=%08x oc->flags=%08x\n",
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200980 __FUNCTION__,
Willy Tarreau0adb2812022-05-27 10:02:48 +0200981 sc, sc->state, sc_ic(sc)->flags, oc->flags);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200982
Willy Tarreau0adb2812022-05-27 10:02:48 +0200983 if (unlikely(sc->state != SC_ST_EST || (oc->flags & CF_SHUTW)))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200984 return;
985
Christopher Faulet04f03e12022-06-01 17:35:34 +0200986 /* we only wake the applet up if it was waiting for some data and is ready to consume it */
987 if (!sc_ep_test(sc, SE_FL_WAIT_DATA) || sc_ep_test(sc, SE_FL_WONT_CONSUME))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200988 return;
989
990 if (!tick_isset(oc->wex))
991 oc->wex = tick_add_ifset(now_ms, oc->wto);
992
993 if (!channel_is_empty(oc)) {
994 /* (re)start sending */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200995 appctx_wakeup(__sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200996 }
997}
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 input channel's expiration timer and the stream connector's
Christopher Faulet13045f02022-04-01 14:23:38 +02001002 * Rx 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_rx(struct stconn *sc)
Christopher Faulet13045f02022-04-01 14:23:38 +02001010{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001011 struct channel *ic = sc_ic(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +02001012
Willy Tarreau676c8db2022-05-24 16:22:24 +02001013 if (ic->flags & CF_SHUTR)
Christopher Faulet13045f02022-04-01 14:23:38 +02001014 return;
Christopher Faulet13045f02022-04-01 14:23:38 +02001015
1016 /* Read not closed, update FD status and timeout for reads */
1017 if (ic->flags & CF_DONT_READ)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001018 sc_wont_read(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +02001019 else
Willy Tarreau0adb2812022-05-27 10:02:48 +02001020 sc_will_read(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +02001021
1022 if (!channel_is_empty(ic) || !channel_may_recv(ic)) {
1023 /* stop reading, imposed by channel's policy or contents */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001024 sc_need_room(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +02001025 }
1026 else {
1027 /* (re)start reading and update timeout. Note: we don't recompute the timeout
1028 * every time we get here, otherwise it would risk never to expire. We only
1029 * update it if is was not yet set. The stream socket handler will already
1030 * have updated it if there has been a completed I/O.
1031 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001032 sc_have_room(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +02001033 }
Willy Tarreau0adb2812022-05-27 10:02:48 +02001034 if (sc->flags & (SC_FL_WONT_READ|SC_FL_NEED_BUFF|SC_FL_NEED_ROOM))
Christopher Faulet13045f02022-04-01 14:23:38 +02001035 ic->rex = TICK_ETERNITY;
1036 else if (!(ic->flags & CF_READ_NOEXP) && !tick_isset(ic->rex))
1037 ic->rex = tick_add_ifset(now_ms, ic->rto);
1038
Willy Tarreau0adb2812022-05-27 10:02:48 +02001039 sc_chk_rcv(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +02001040}
1041
1042/* This function is designed to be called from within the stream handler to
Willy Tarreau4596fe22022-05-17 19:07:51 +02001043 * update the output channel's expiration timer and the stream connector's
Christopher Faulet13045f02022-04-01 14:23:38 +02001044 * Tx flags based on the channel's flags. It needs to be called only once
1045 * after the channel's flags have settled down, and before they are cleared,
1046 * though it doesn't harm to call it as often as desired (it just slightly
1047 * hurts performance). It must not be called from outside of the stream
1048 * handler, as what it does will be used to compute the stream task's
1049 * expiration.
1050 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001051void sc_update_tx(struct stconn *sc)
Christopher Faulet13045f02022-04-01 14:23:38 +02001052{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001053 struct channel *oc = sc_oc(sc);
1054 struct channel *ic = sc_ic(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +02001055
1056 if (oc->flags & CF_SHUTW)
1057 return;
1058
1059 /* Write not closed, update FD status and timeout for writes */
1060 if (channel_is_empty(oc)) {
1061 /* stop writing */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001062 if (!sc_ep_test(sc, SE_FL_WAIT_DATA)) {
Christopher Faulet13045f02022-04-01 14:23:38 +02001063 if ((oc->flags & CF_SHUTW_NOW) == 0)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001064 sc_ep_set(sc, SE_FL_WAIT_DATA);
Christopher Faulet13045f02022-04-01 14:23:38 +02001065 oc->wex = TICK_ETERNITY;
1066 }
1067 return;
1068 }
1069
1070 /* (re)start writing and update timeout. Note: we don't recompute the timeout
1071 * every time we get here, otherwise it would risk never to expire. We only
1072 * update it if is was not yet set. The stream socket handler will already
1073 * have updated it if there has been a completed I/O.
1074 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001075 sc_ep_clr(sc, SE_FL_WAIT_DATA);
Christopher Faulet13045f02022-04-01 14:23:38 +02001076 if (!tick_isset(oc->wex)) {
1077 oc->wex = tick_add_ifset(now_ms, oc->wto);
Willy Tarreau0adb2812022-05-27 10:02:48 +02001078 if (tick_isset(ic->rex) && !(sc->flags & SC_FL_INDEP_STR)) {
Christopher Faulet13045f02022-04-01 14:23:38 +02001079 /* Note: depending on the protocol, we don't know if we're waiting
1080 * for incoming data or not. So in order to prevent the socket from
1081 * expiring read timeouts during writes, we refresh the read timeout,
1082 * except if it was already infinite or if we have explicitly setup
1083 * independent streams.
1084 */
1085 ic->rex = tick_add_ifset(now_ms, ic->rto);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001086 }
1087 }
1088}
1089
Willy Tarreau19c65a92022-05-27 08:49:24 +02001090/* This function is the equivalent to sc_update() except that it's
Christopher Faulet5e29b762022-04-04 08:58:34 +02001091 * designed to be called from outside the stream handlers, typically the lower
1092 * layers (applets, connections) after I/O completion. After updating the stream
1093 * interface and timeouts, it will try to forward what can be forwarded, then to
1094 * wake the associated task up if an important event requires special handling.
Willy Tarreau15252cd2022-05-25 16:36:21 +02001095 * It may update SE_FL_WAIT_DATA and/or SC_FL_NEED_ROOM, that the callers are
Christopher Faulet5e29b762022-04-04 08:58:34 +02001096 * encouraged to watch to take appropriate action.
Willy Tarreau19c65a92022-05-27 08:49:24 +02001097 * It should not be called from within the stream itself, sc_update()
Christopher Faulet5e29b762022-04-04 08:58:34 +02001098 * is designed for this.
1099 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001100static void sc_notify(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001101{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001102 struct channel *ic = sc_ic(sc);
1103 struct channel *oc = sc_oc(sc);
Willy Tarreaue68bc612022-05-27 11:23:05 +02001104 struct stconn *sco = sc_opposite(sc);
Willy Tarreau0adb2812022-05-27 10:02:48 +02001105 struct task *task = sc_strm_task(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001106
1107 /* process consumer side */
1108 if (channel_is_empty(oc)) {
Willy Tarreau0adb2812022-05-27 10:02:48 +02001109 struct connection *conn = sc_conn(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001110
1111 if (((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW) &&
Willy Tarreau0adb2812022-05-27 10:02:48 +02001112 (sc->state == SC_ST_EST) && (!conn || !(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS))))
1113 sc_shutw(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001114 oc->wex = TICK_ETERNITY;
1115 }
1116
1117 /* indicate that we may be waiting for data from the output channel or
1118 * we're about to close and can't expect more data if SHUTW_NOW is there.
1119 */
1120 if (!(oc->flags & (CF_SHUTW|CF_SHUTW_NOW)))
Willy Tarreau0adb2812022-05-27 10:02:48 +02001121 sc_ep_set(sc, SE_FL_WAIT_DATA);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001122 else if ((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001123 sc_ep_clr(sc, SE_FL_WAIT_DATA);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001124
1125 /* update OC timeouts and wake the other side up if it's waiting for room */
1126 if (oc->flags & CF_WRITE_ACTIVITY) {
1127 if ((oc->flags & (CF_SHUTW|CF_WRITE_PARTIAL)) == CF_WRITE_PARTIAL &&
1128 !channel_is_empty(oc))
1129 if (tick_isset(oc->wex))
1130 oc->wex = tick_add_ifset(now_ms, oc->wto);
1131
Willy Tarreau0adb2812022-05-27 10:02:48 +02001132 if (!(sc->flags & SC_FL_INDEP_STR))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001133 if (tick_isset(ic->rex))
1134 ic->rex = tick_add_ifset(now_ms, ic->rto);
1135 }
1136
1137 if (oc->flags & CF_DONT_READ)
Willy Tarreaue68bc612022-05-27 11:23:05 +02001138 sc_wont_read(sco);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001139 else
Willy Tarreaue68bc612022-05-27 11:23:05 +02001140 sc_will_read(sco);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001141
1142 /* Notify the other side when we've injected data into the IC that
1143 * needs to be forwarded. We can do fast-forwarding as soon as there
1144 * are output data, but we avoid doing this if some of the data are
1145 * not yet scheduled for being forwarded, because it is very likely
1146 * that it will be done again immediately afterwards once the following
Willy Tarreau15252cd2022-05-25 16:36:21 +02001147 * data are parsed (eg: HTTP chunking). We only clear SC_FL_NEED_ROOM
1148 * once we've emptied *some* of the output buffer, and not just when
1149 * there is available room, because applets are often forced to stop
1150 * before the buffer is full. We must not stop based on input data
1151 * alone because an HTTP parser might need more data to complete the
1152 * parsing.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001153 */
1154 if (!channel_is_empty(ic) &&
Willy Tarreaue68bc612022-05-27 11:23:05 +02001155 sc_ep_test(sco, SE_FL_WAIT_DATA) &&
Christopher Faulet5e29b762022-04-04 08:58:34 +02001156 (!(ic->flags & CF_EXPECT_MORE) || c_full(ic) || ci_data(ic) == 0 || ic->pipe)) {
1157 int new_len, last_len;
1158
1159 last_len = co_data(ic);
1160 if (ic->pipe)
1161 last_len += ic->pipe->data;
1162
Willy Tarreaue68bc612022-05-27 11:23:05 +02001163 sc_chk_snd(sco);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001164
1165 new_len = co_data(ic);
1166 if (ic->pipe)
1167 new_len += ic->pipe->data;
1168
1169 /* check if the consumer has freed some space either in the
1170 * buffer or in the pipe.
1171 */
1172 if (new_len < last_len)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001173 sc_have_room(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001174 }
1175
1176 if (!(ic->flags & CF_DONT_READ))
Willy Tarreau0adb2812022-05-27 10:02:48 +02001177 sc_will_read(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001178
Willy Tarreau0adb2812022-05-27 10:02:48 +02001179 sc_chk_rcv(sc);
Willy Tarreaue68bc612022-05-27 11:23:05 +02001180 sc_chk_rcv(sco);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001181
Willy Tarreau0adb2812022-05-27 10:02:48 +02001182 if (ic->flags & CF_SHUTR || sc_ep_test(sc, SE_FL_APPLET_NEED_CONN) ||
1183 (sc->flags & (SC_FL_WONT_READ|SC_FL_NEED_BUFF|SC_FL_NEED_ROOM))) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001184 ic->rex = TICK_ETERNITY;
1185 }
1186 else if ((ic->flags & (CF_SHUTR|CF_READ_PARTIAL)) == CF_READ_PARTIAL) {
Willy Tarreauf61dd192022-05-27 09:00:19 +02001187 /* we must re-enable reading if sc_chk_snd() has freed some space */
Christopher Faulet5e29b762022-04-04 08:58:34 +02001188 if (!(ic->flags & CF_READ_NOEXP) && tick_isset(ic->rex))
1189 ic->rex = tick_add_ifset(now_ms, ic->rto);
1190 }
1191
1192 /* wake the task up only when needed */
1193 if (/* changes on the production side */
1194 (ic->flags & (CF_READ_NULL|CF_READ_ERROR)) ||
Willy Tarreau0adb2812022-05-27 10:02:48 +02001195 !sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST) ||
1196 sc_ep_test(sc, SE_FL_ERROR) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001197 ((ic->flags & CF_READ_PARTIAL) &&
Willy Tarreaue68bc612022-05-27 11:23:05 +02001198 ((ic->flags & CF_EOI) || !ic->to_forward || sco->state != SC_ST_EST)) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001199
1200 /* changes on the consumption side */
1201 (oc->flags & (CF_WRITE_NULL|CF_WRITE_ERROR)) ||
1202 ((oc->flags & CF_WRITE_ACTIVITY) &&
1203 ((oc->flags & CF_SHUTW) ||
1204 (((oc->flags & CF_WAKE_WRITE) ||
1205 !(oc->flags & (CF_AUTO_CLOSE|CF_SHUTW_NOW|CF_SHUTW))) &&
Willy Tarreaue68bc612022-05-27 11:23:05 +02001206 (sco->state != SC_ST_EST ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001207 (channel_is_empty(oc) && !oc->to_forward)))))) {
1208 task_wakeup(task, TASK_WOKEN_IO);
1209 }
1210 else {
1211 /* Update expiration date for the task and requeue it */
1212 task->expire = tick_first((tick_is_expired(task->expire, now_ms) ? 0 : task->expire),
1213 tick_first(tick_first(ic->rex, ic->wex),
1214 tick_first(oc->rex, oc->wex)));
1215
1216 task->expire = tick_first(task->expire, ic->analyse_exp);
1217 task->expire = tick_first(task->expire, oc->analyse_exp);
Willy Tarreau0adb2812022-05-27 10:02:48 +02001218 task->expire = tick_first(task->expire, __sc_strm(sc)->conn_exp);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001219
1220 task_queue(task);
1221 }
1222 if (ic->flags & CF_READ_ACTIVITY)
1223 ic->flags &= ~CF_READ_DONTWAIT;
1224}
1225
1226/*
1227 * This function propagates a null read received on a socket-based connection.
Willy Tarreaucb041662022-05-17 19:44:42 +02001228 * It updates the stream connector. If the stream connector has SC_FL_NOHALF,
Christopher Faulet5e29b762022-04-04 08:58:34 +02001229 * the close is also forwarded to the write side as an abort.
1230 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001231static void sc_conn_read0(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001232{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001233 struct channel *ic = sc_ic(sc);
1234 struct channel *oc = sc_oc(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001235
Willy Tarreau0adb2812022-05-27 10:02:48 +02001236 BUG_ON(!sc_conn(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001237
Christopher Faulet5e29b762022-04-04 08:58:34 +02001238 if (ic->flags & CF_SHUTR)
1239 return;
1240 ic->flags |= CF_SHUTR;
1241 ic->rex = TICK_ETERNITY;
1242
Willy Tarreau0adb2812022-05-27 10:02:48 +02001243 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001244 return;
1245
1246 if (oc->flags & CF_SHUTW)
1247 goto do_close;
1248
Willy Tarreau0adb2812022-05-27 10:02:48 +02001249 if (sc->flags & SC_FL_NOHALF) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001250 /* we want to immediately forward this close to the write side */
1251 /* force flag on ssl to keep stream in cache */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001252 sc_conn_shutw(sc, CO_SHW_SILENT);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001253 goto do_close;
1254 }
1255
1256 /* otherwise that's just a normal read shutdown */
1257 return;
1258
1259 do_close:
Willy Tarreauf61dd192022-05-27 09:00:19 +02001260 /* OK we completely close the socket here just as if we went through sc_shut[rw]() */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001261 sc_conn_shut(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001262
1263 oc->flags &= ~CF_SHUTW_NOW;
1264 oc->flags |= CF_SHUTW;
1265 oc->wex = TICK_ETERNITY;
1266
Willy Tarreau0adb2812022-05-27 10:02:48 +02001267 sc->state = SC_ST_DIS;
Christopher Fauletca679922022-07-20 13:24:04 +02001268 if (sc->flags & SC_FL_ISBACK)
1269 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001270 return;
1271}
1272
1273/*
1274 * This is the callback which is called by the connection layer to receive data
1275 * into the buffer from the connection. It iterates over the mux layer's
1276 * rcv_buf function.
1277 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001278static int sc_conn_recv(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001279{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001280 struct connection *conn = __sc_conn(sc);
1281 struct channel *ic = sc_ic(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001282 int ret, max, cur_read = 0;
1283 int read_poll = MAX_READ_POLL_LOOPS;
1284 int flags = 0;
1285
1286 /* If not established yet, do nothing. */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001287 if (sc->state != SC_ST_EST)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001288 return 0;
1289
Willy Tarreau462b9892022-05-18 18:06:53 +02001290 /* If another call to sc_conn_recv() failed, and we subscribed to
Christopher Faulet5e29b762022-04-04 08:58:34 +02001291 * recv events already, give up now.
1292 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001293 if (sc->wait_event.events & SUB_RETRY_RECV)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001294 return 0;
1295
1296 /* maybe we were called immediately after an asynchronous shutr */
1297 if (ic->flags & CF_SHUTR)
1298 return 1;
1299
1300 /* we must wait because the mux is not installed yet */
1301 if (!conn->mux)
1302 return 0;
1303
1304 /* stop here if we reached the end of data */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001305 if (sc_ep_test(sc, SE_FL_EOS))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001306 goto end_recv;
1307
1308 /* stop immediately on errors. Note that we DON'T want to stop on
1309 * POLL_ERR, as the poller might report a write error while there
1310 * are still data available in the recv buffer. This typically
1311 * happens when we send too large a request to a backend server
1312 * which rejects it before reading it all.
1313 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001314 if (!sc_ep_test(sc, SE_FL_RCV_MORE)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001315 if (!conn_xprt_ready(conn))
1316 return 0;
Willy Tarreau0adb2812022-05-27 10:02:48 +02001317 if (sc_ep_test(sc, SE_FL_ERROR))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001318 goto end_recv;
1319 }
1320
1321 /* prepare to detect if the mux needs more room */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001322 sc_ep_clr(sc, SE_FL_WANT_ROOM);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001323
1324 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) && !co_data(ic) &&
1325 global.tune.idle_timer &&
1326 (unsigned short)(now_ms - ic->last_read) >= global.tune.idle_timer) {
1327 /* The buffer was empty and nothing was transferred for more
1328 * than one second. This was caused by a pause and not by
1329 * congestion. Reset any streaming mode to reduce latency.
1330 */
1331 ic->xfer_small = 0;
1332 ic->xfer_large = 0;
1333 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
1334 }
1335
1336 /* First, let's see if we may splice data across the channel without
1337 * using a buffer.
1338 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001339 if (sc_ep_test(sc, SE_FL_MAY_SPLICE) &&
Christopher Faulet5e29b762022-04-04 08:58:34 +02001340 (ic->pipe || ic->to_forward >= MIN_SPLICE_FORWARD) &&
1341 ic->flags & CF_KERN_SPLICING) {
1342 if (c_data(ic)) {
1343 /* We're embarrassed, there are already data pending in
1344 * the buffer and we don't want to have them at two
1345 * locations at a time. Let's indicate we need some
1346 * place and ask the consumer to hurry.
1347 */
1348 flags |= CO_RFL_BUF_FLUSH;
1349 goto abort_splice;
1350 }
1351
1352 if (unlikely(ic->pipe == NULL)) {
1353 if (pipes_used >= global.maxpipes || !(ic->pipe = get_pipe())) {
1354 ic->flags &= ~CF_KERN_SPLICING;
1355 goto abort_splice;
1356 }
1357 }
1358
Willy Tarreau0adb2812022-05-27 10:02:48 +02001359 ret = conn->mux->rcv_pipe(sc, ic->pipe, ic->to_forward);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001360 if (ret < 0) {
1361 /* splice not supported on this end, let's disable it */
1362 ic->flags &= ~CF_KERN_SPLICING;
1363 goto abort_splice;
1364 }
1365
1366 if (ret > 0) {
1367 if (ic->to_forward != CHN_INFINITE_FORWARD)
1368 ic->to_forward -= ret;
1369 ic->total += ret;
1370 cur_read += ret;
1371 ic->flags |= CF_READ_PARTIAL;
1372 }
1373
Willy Tarreau0adb2812022-05-27 10:02:48 +02001374 if (sc_ep_test(sc, SE_FL_EOS | SE_FL_ERROR))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001375 goto end_recv;
1376
1377 if (conn->flags & CO_FL_WAIT_ROOM) {
1378 /* the pipe is full or we have read enough data that it
1379 * could soon be full. Let's stop before needing to poll.
1380 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001381 sc_need_room(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001382 goto done_recv;
1383 }
1384
1385 /* splice not possible (anymore), let's go on on standard copy */
1386 }
1387
1388 abort_splice:
1389 if (ic->pipe && unlikely(!ic->pipe->data)) {
1390 put_pipe(ic->pipe);
1391 ic->pipe = NULL;
1392 }
1393
Willy Tarreau0adb2812022-05-27 10:02:48 +02001394 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 +02001395 /* don't break splicing by reading, but still call rcv_buf()
1396 * to pass the flag.
1397 */
1398 goto done_recv;
1399 }
1400
1401 /* now we'll need a input buffer for the stream */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001402 if (!sc_alloc_ibuf(sc, &(__sc_strm(sc)->buffer_wait)))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001403 goto end_recv;
1404
1405 /* For an HTX stream, if the buffer is stuck (no output data with some
1406 * input data) and if the HTX message is fragmented or if its free space
1407 * wraps, we force an HTX deframentation. It is a way to have a
1408 * contiguous free space nad to let the mux to copy as much data as
1409 * possible.
1410 *
1411 * NOTE: A possible optim may be to let the mux decides if defrag is
1412 * required or not, depending on amount of data to be xferred.
1413 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001414 if (IS_HTX_STRM(__sc_strm(sc)) && !co_data(ic)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001415 struct htx *htx = htxbuf(&ic->buf);
1416
1417 if (htx_is_not_empty(htx) && ((htx->flags & HTX_FL_FRAGMENTED) || htx_space_wraps(htx)))
1418 htx_defrag(htx, NULL, 0);
1419 }
1420
1421 /* Instruct the mux it must subscribed for read events */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001422 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 +02001423
1424 /* Important note : if we're called with POLL_IN|POLL_HUP, it means the read polling
1425 * was enabled, which implies that the recv buffer was not full. So we have a guarantee
1426 * that if such an event is not handled above in splice, it will be handled here by
1427 * recv().
1428 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001429 while (sc_ep_test(sc, SE_FL_RCV_MORE) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001430 (!(conn->flags & CO_FL_HANDSHAKE) &&
Willy Tarreau0adb2812022-05-27 10:02:48 +02001431 (!sc_ep_test(sc, SE_FL_ERROR | SE_FL_EOS)) && !(ic->flags & CF_SHUTR))) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001432 int cur_flags = flags;
1433
1434 /* Compute transient CO_RFL_* flags */
1435 if (co_data(ic)) {
1436 cur_flags |= (CO_RFL_BUF_WET | CO_RFL_BUF_NOT_STUCK);
1437 }
1438
1439 /* <max> may be null. This is the mux responsibility to set
Willy Tarreaue68bc612022-05-27 11:23:05 +02001440 * SE_FL_RCV_MORE on the SC if more space is needed.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001441 */
1442 max = channel_recv_max(ic);
Willy Tarreau0adb2812022-05-27 10:02:48 +02001443 ret = conn->mux->rcv_buf(sc, &ic->buf, max, cur_flags);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001444
Willy Tarreau0adb2812022-05-27 10:02:48 +02001445 if (sc_ep_test(sc, SE_FL_WANT_ROOM)) {
Willy Tarreaub605c422022-05-17 17:04:55 +02001446 /* SE_FL_WANT_ROOM must not be reported if the channel's
Christopher Faulet5e29b762022-04-04 08:58:34 +02001447 * buffer is empty.
1448 */
1449 BUG_ON(c_empty(ic));
1450
Willy Tarreau0adb2812022-05-27 10:02:48 +02001451 sc_need_room(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001452 /* Add READ_PARTIAL because some data are pending but
1453 * cannot be xferred to the channel
1454 */
1455 ic->flags |= CF_READ_PARTIAL;
1456 }
1457
1458 if (ret <= 0) {
1459 /* if we refrained from reading because we asked for a
1460 * flush to satisfy rcv_pipe(), we must not subscribe
1461 * and instead report that there's not enough room
1462 * here to proceed.
1463 */
1464 if (flags & CO_RFL_BUF_FLUSH)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001465 sc_need_room(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001466 break;
1467 }
1468
1469 cur_read += ret;
1470
1471 /* if we're allowed to directly forward data, we must update ->o */
1472 if (ic->to_forward && !(ic->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
1473 unsigned long fwd = ret;
1474 if (ic->to_forward != CHN_INFINITE_FORWARD) {
1475 if (fwd > ic->to_forward)
1476 fwd = ic->to_forward;
1477 ic->to_forward -= fwd;
1478 }
1479 c_adv(ic, fwd);
1480 }
1481
1482 ic->flags |= CF_READ_PARTIAL;
1483 ic->total += ret;
1484
1485 /* End-of-input reached, we can leave. In this case, it is
Willy Tarreaue68bc612022-05-27 11:23:05 +02001486 * important to break the loop to not block the SC because of
Christopher Faulet5e29b762022-04-04 08:58:34 +02001487 * the channel's policies.This way, we are still able to receive
1488 * shutdowns.
1489 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001490 if (sc_ep_test(sc, SE_FL_EOI))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001491 break;
1492
1493 if ((ic->flags & CF_READ_DONTWAIT) || --read_poll <= 0) {
1494 /* we're stopped by the channel's policy */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001495 sc_wont_read(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001496 break;
1497 }
1498
1499 /* if too many bytes were missing from last read, it means that
1500 * it's pointless trying to read again because the system does
1501 * not have them in buffers.
1502 */
1503 if (ret < max) {
1504 /* if a streamer has read few data, it may be because we
1505 * have exhausted system buffers. It's not worth trying
1506 * again.
1507 */
1508 if (ic->flags & CF_STREAMER) {
1509 /* we're stopped by the channel's policy */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001510 sc_wont_read(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001511 break;
1512 }
1513
1514 /* if we read a large block smaller than what we requested,
1515 * it's almost certain we'll never get anything more.
1516 */
1517 if (ret >= global.tune.recv_enough) {
1518 /* we're stopped by the channel's policy */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001519 sc_wont_read(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001520 break;
1521 }
1522 }
1523
1524 /* if we are waiting for more space, don't try to read more data
1525 * right now.
1526 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001527 if (sc->flags & (SC_FL_WONT_READ|SC_FL_NEED_BUFF|SC_FL_NEED_ROOM))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001528 break;
1529 } /* while !flags */
1530
1531 done_recv:
1532 if (cur_read) {
1533 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) &&
1534 (cur_read <= ic->buf.size / 2)) {
1535 ic->xfer_large = 0;
1536 ic->xfer_small++;
1537 if (ic->xfer_small >= 3) {
1538 /* we have read less than half of the buffer in
1539 * one pass, and this happened at least 3 times.
1540 * This is definitely not a streamer.
1541 */
1542 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
1543 }
1544 else if (ic->xfer_small >= 2) {
1545 /* if the buffer has been at least half full twice,
1546 * we receive faster than we send, so at least it
1547 * is not a "fast streamer".
1548 */
1549 ic->flags &= ~CF_STREAMER_FAST;
1550 }
1551 }
1552 else if (!(ic->flags & CF_STREAMER_FAST) &&
1553 (cur_read >= ic->buf.size - global.tune.maxrewrite)) {
1554 /* we read a full buffer at once */
1555 ic->xfer_small = 0;
1556 ic->xfer_large++;
1557 if (ic->xfer_large >= 3) {
1558 /* we call this buffer a fast streamer if it manages
1559 * to be filled in one call 3 consecutive times.
1560 */
1561 ic->flags |= (CF_STREAMER | CF_STREAMER_FAST);
1562 }
1563 }
1564 else {
1565 ic->xfer_small = 0;
1566 ic->xfer_large = 0;
1567 }
1568 ic->last_read = now_ms;
1569 }
1570
1571 end_recv:
1572 ret = (cur_read != 0);
1573
1574 /* Report EOI on the channel if it was reached from the mux point of
1575 * view. */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001576 if (sc_ep_test(sc, SE_FL_EOI) && !(ic->flags & CF_EOI)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001577 ic->flags |= (CF_EOI|CF_READ_PARTIAL);
1578 ret = 1;
1579 }
1580
Willy Tarreau0adb2812022-05-27 10:02:48 +02001581 if (sc_ep_test(sc, SE_FL_ERROR))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001582 ret = 1;
Willy Tarreau0adb2812022-05-27 10:02:48 +02001583 else if (sc_ep_test(sc, SE_FL_EOS)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001584 /* we received a shutdown */
1585 ic->flags |= CF_READ_NULL;
1586 if (ic->flags & CF_AUTO_CLOSE)
1587 channel_shutw_now(ic);
Willy Tarreau0adb2812022-05-27 10:02:48 +02001588 sc_conn_read0(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001589 ret = 1;
1590 }
Willy Tarreau0adb2812022-05-27 10:02:48 +02001591 else if (!(sc->flags & (SC_FL_WONT_READ|SC_FL_NEED_BUFF|SC_FL_NEED_ROOM)) &&
Willy Tarreau15252cd2022-05-25 16:36:21 +02001592 !(ic->flags & CF_SHUTR)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001593 /* Subscribe to receive events if we're blocking on I/O */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001594 conn->mux->subscribe(sc, SUB_RETRY_RECV, &sc->wait_event);
1595 se_have_no_more_data(sc->sedesc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001596 } else {
Willy Tarreau0adb2812022-05-27 10:02:48 +02001597 se_have_more_data(sc->sedesc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001598 ret = 1;
1599 }
1600 return ret;
1601}
1602
Willy Tarreau4596fe22022-05-17 19:07:51 +02001603/* This tries to perform a synchronous receive on the stream connector to
Christopher Faulet5e29b762022-04-04 08:58:34 +02001604 * try to collect last arrived data. In practice it's only implemented on
Willy Tarreau4596fe22022-05-17 19:07:51 +02001605 * stconns. Returns 0 if nothing was done, non-zero if new data or a
Christopher Faulet5e29b762022-04-04 08:58:34 +02001606 * shutdown were collected. This may result on some delayed receive calls
1607 * to be programmed and performed later, though it doesn't provide any
1608 * such guarantee.
1609 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001610int sc_conn_sync_recv(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001611{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001612 if (!sc_state_in(sc->state, SC_SB_RDY|SC_SB_EST))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001613 return 0;
1614
Willy Tarreau0adb2812022-05-27 10:02:48 +02001615 if (!sc_mux_ops(sc))
Willy Tarreau4596fe22022-05-17 19:07:51 +02001616 return 0; // only stconns are supported
Christopher Faulet5e29b762022-04-04 08:58:34 +02001617
Willy Tarreau0adb2812022-05-27 10:02:48 +02001618 if (sc->wait_event.events & SUB_RETRY_RECV)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001619 return 0; // already subscribed
1620
Willy Tarreau0adb2812022-05-27 10:02:48 +02001621 if (!sc_is_recv_allowed(sc))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001622 return 0; // already failed
1623
Willy Tarreau0adb2812022-05-27 10:02:48 +02001624 return sc_conn_recv(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001625}
1626
1627/*
1628 * This function is called to send buffer data to a stream socket.
1629 * It calls the mux layer's snd_buf function. It relies on the
1630 * caller to commit polling changes. The caller should check conn->flags
1631 * for errors.
1632 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001633static int sc_conn_send(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001634{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001635 struct connection *conn = __sc_conn(sc);
1636 struct stream *s = __sc_strm(sc);
1637 struct channel *oc = sc_oc(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001638 int ret;
1639 int did_send = 0;
1640
Willy Tarreau0adb2812022-05-27 10:02:48 +02001641 if (sc_ep_test(sc, SE_FL_ERROR | SE_FL_ERR_PENDING) || sc_is_conn_error(sc)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001642 /* We're probably there because the tasklet was woken up,
1643 * but process_stream() ran before, detected there were an
Willy Tarreaue68bc612022-05-27 11:23:05 +02001644 * error and put the SC back to SC_ST_TAR. There's still
Christopher Faulet5e29b762022-04-04 08:58:34 +02001645 * CO_FL_ERROR on the connection but we don't want to add
Willy Tarreaub605c422022-05-17 17:04:55 +02001646 * SE_FL_ERROR back, so give up
Christopher Faulet5e29b762022-04-04 08:58:34 +02001647 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001648 if (sc->state < SC_ST_CON)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001649 return 0;
Willy Tarreau0adb2812022-05-27 10:02:48 +02001650 sc_ep_set(sc, SE_FL_ERROR);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001651 return 1;
1652 }
1653
1654 /* We're already waiting to be able to send, give up */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001655 if (sc->wait_event.events & SUB_RETRY_SEND)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001656 return 0;
1657
1658 /* we might have been called just after an asynchronous shutw */
1659 if (oc->flags & CF_SHUTW)
1660 return 1;
1661
1662 /* we must wait because the mux is not installed yet */
1663 if (!conn->mux)
1664 return 0;
1665
1666 if (oc->pipe && conn->xprt->snd_pipe && conn->mux->snd_pipe) {
Willy Tarreau0adb2812022-05-27 10:02:48 +02001667 ret = conn->mux->snd_pipe(sc, oc->pipe);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001668 if (ret > 0)
1669 did_send = 1;
1670
1671 if (!oc->pipe->data) {
1672 put_pipe(oc->pipe);
1673 oc->pipe = NULL;
1674 }
1675
1676 if (oc->pipe)
1677 goto end;
1678 }
1679
1680 /* At this point, the pipe is empty, but we may still have data pending
1681 * in the normal buffer.
1682 */
1683 if (co_data(oc)) {
1684 /* when we're here, we already know that there is no spliced
1685 * data left, and that there are sendable buffered data.
1686 */
1687
1688 /* check if we want to inform the kernel that we're interested in
1689 * sending more data after this call. We want this if :
1690 * - we're about to close after this last send and want to merge
1691 * the ongoing FIN with the last segment.
1692 * - we know we can't send everything at once and must get back
1693 * here because of unaligned data
1694 * - there is still a finite amount of data to forward
1695 * The test is arranged so that the most common case does only 2
1696 * tests.
1697 */
1698 unsigned int send_flag = 0;
1699
1700 if ((!(oc->flags & (CF_NEVER_WAIT|CF_SEND_DONTWAIT)) &&
1701 ((oc->to_forward && oc->to_forward != CHN_INFINITE_FORWARD) ||
1702 (oc->flags & CF_EXPECT_MORE) ||
1703 (IS_HTX_STRM(s) &&
1704 (!(oc->flags & (CF_EOI|CF_SHUTR)) && htx_expect_more(htxbuf(&oc->buf)))))) ||
1705 ((oc->flags & CF_ISRESP) &&
1706 ((oc->flags & (CF_AUTO_CLOSE|CF_SHUTW_NOW)) == (CF_AUTO_CLOSE|CF_SHUTW_NOW))))
1707 send_flag |= CO_SFL_MSG_MORE;
1708
1709 if (oc->flags & CF_STREAMER)
1710 send_flag |= CO_SFL_STREAMER;
1711
1712 if (s->txn && s->txn->flags & TX_L7_RETRY && !b_data(&s->txn->l7_buffer)) {
1713 /* If we want to be able to do L7 retries, copy
1714 * the data we're about to send, so that we are able
1715 * to resend them if needed
1716 */
1717 /* Try to allocate a buffer if we had none.
1718 * If it fails, the next test will just
1719 * disable the l7 retries by setting
1720 * l7_conn_retries to 0.
1721 */
1722 if (s->txn->req.msg_state != HTTP_MSG_DONE)
1723 s->txn->flags &= ~TX_L7_RETRY;
1724 else {
1725 if (b_alloc(&s->txn->l7_buffer) == NULL)
1726 s->txn->flags &= ~TX_L7_RETRY;
1727 else {
1728 memcpy(b_orig(&s->txn->l7_buffer),
1729 b_orig(&oc->buf),
1730 b_size(&oc->buf));
1731 s->txn->l7_buffer.head = co_data(oc);
1732 b_add(&s->txn->l7_buffer, co_data(oc));
1733 }
1734
1735 }
1736 }
1737
Willy Tarreau0adb2812022-05-27 10:02:48 +02001738 ret = conn->mux->snd_buf(sc, &oc->buf, co_data(oc), send_flag);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001739 if (ret > 0) {
1740 did_send = 1;
1741 c_rew(oc, ret);
1742 c_realign_if_empty(oc);
1743
1744 if (!co_data(oc)) {
1745 /* Always clear both flags once everything has been sent, they're one-shot */
1746 oc->flags &= ~(CF_EXPECT_MORE | CF_SEND_DONTWAIT);
1747 }
1748 /* if some data remain in the buffer, it's only because the
1749 * system buffers are full, we will try next time.
1750 */
Christopher Faulet13045f02022-04-01 14:23:38 +02001751 }
1752 }
Christopher Faulet5e29b762022-04-04 08:58:34 +02001753
1754 end:
1755 if (did_send) {
1756 oc->flags |= CF_WRITE_PARTIAL | CF_WROTE_DATA;
Willy Tarreau0adb2812022-05-27 10:02:48 +02001757 if (sc->state == SC_ST_CON)
1758 sc->state = SC_ST_RDY;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001759
Willy Tarreau0adb2812022-05-27 10:02:48 +02001760 sc_have_room(sc_opposite(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001761 }
1762
Willy Tarreau0adb2812022-05-27 10:02:48 +02001763 if (sc_ep_test(sc, SE_FL_ERROR | SE_FL_ERR_PENDING)) {
1764 sc_ep_set(sc, SE_FL_ERROR);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001765 return 1;
1766 }
1767
1768 /* We couldn't send all of our data, let the mux know we'd like to send more */
1769 if (!channel_is_empty(oc))
Willy Tarreau0adb2812022-05-27 10:02:48 +02001770 conn->mux->subscribe(sc, SUB_RETRY_SEND, &sc->wait_event);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001771 return did_send;
1772}
1773
Willy Tarreau4596fe22022-05-17 19:07:51 +02001774/* perform a synchronous send() for the stream connector. The CF_WRITE_NULL and
Christopher Faulet5e29b762022-04-04 08:58:34 +02001775 * CF_WRITE_PARTIAL flags are cleared prior to the attempt, and will possibly
1776 * be updated in case of success.
1777 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001778void sc_conn_sync_send(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001779{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001780 struct channel *oc = sc_oc(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001781
1782 oc->flags &= ~(CF_WRITE_NULL|CF_WRITE_PARTIAL);
1783
1784 if (oc->flags & CF_SHUTW)
1785 return;
1786
1787 if (channel_is_empty(oc))
1788 return;
1789
Willy Tarreau0adb2812022-05-27 10:02:48 +02001790 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001791 return;
1792
Willy Tarreau0adb2812022-05-27 10:02:48 +02001793 if (!sc_mux_ops(sc))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001794 return;
1795
Willy Tarreau0adb2812022-05-27 10:02:48 +02001796 sc_conn_send(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001797}
1798
1799/* Called by I/O handlers after completion.. It propagates
Willy Tarreau4596fe22022-05-17 19:07:51 +02001800 * connection flags to the stream connector, updates the stream (which may or
Christopher Faulet5e29b762022-04-04 08:58:34 +02001801 * may not take this opportunity to try to forward data), then update the
Willy Tarreau4596fe22022-05-17 19:07:51 +02001802 * connection's polling based on the channels and stream connector's final
Christopher Faulet5e29b762022-04-04 08:58:34 +02001803 * states. The function always returns 0.
1804 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001805static int sc_conn_process(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001806{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001807 struct connection *conn = __sc_conn(sc);
1808 struct channel *ic = sc_ic(sc);
1809 struct channel *oc = sc_oc(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001810
1811 BUG_ON(!conn);
1812
1813 /* If we have data to send, try it now */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001814 if (!channel_is_empty(oc) && !(sc->wait_event.events & SUB_RETRY_SEND))
1815 sc_conn_send(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001816
Willy Tarreau4596fe22022-05-17 19:07:51 +02001817 /* First step, report to the stream connector what was detected at the
Christopher Faulet5e29b762022-04-04 08:58:34 +02001818 * connection layer : errors and connection establishment.
Willy Tarreaub605c422022-05-17 17:04:55 +02001819 * Only add SE_FL_ERROR if we're connected, or we're attempting to
Christopher Faulet5e29b762022-04-04 08:58:34 +02001820 * connect, we may get there because we got woken up, but only run
1821 * after process_stream() noticed there were an error, and decided
1822 * to retry to connect, the connection may still have CO_FL_ERROR,
Willy Tarreaub605c422022-05-17 17:04:55 +02001823 * and we don't want to add SE_FL_ERROR back
Christopher Faulet5e29b762022-04-04 08:58:34 +02001824 *
Willy Tarreau462b9892022-05-18 18:06:53 +02001825 * Note: This test is only required because sc_conn_process is also the SI
1826 * wake callback. Otherwise sc_conn_recv()/sc_conn_send() already take
Christopher Faulet5e29b762022-04-04 08:58:34 +02001827 * care of it.
1828 */
1829
Willy Tarreau0adb2812022-05-27 10:02:48 +02001830 if (sc->state >= SC_ST_CON) {
1831 if (sc_is_conn_error(sc))
1832 sc_ep_set(sc, SE_FL_ERROR);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001833 }
1834
1835 /* If we had early data, and the handshake ended, then
1836 * we can remove the flag, and attempt to wake the task up,
1837 * in the event there's an analyser waiting for the end of
1838 * the handshake.
1839 */
1840 if (!(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS)) &&
Willy Tarreau0adb2812022-05-27 10:02:48 +02001841 sc_ep_test(sc, SE_FL_WAIT_FOR_HS)) {
1842 sc_ep_clr(sc, SE_FL_WAIT_FOR_HS);
1843 task_wakeup(sc_strm_task(sc), TASK_WOKEN_MSG);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001844 }
1845
Willy Tarreau0adb2812022-05-27 10:02:48 +02001846 if (!sc_state_in(sc->state, SC_SB_EST|SC_SB_DIS|SC_SB_CLO) &&
Christopher Faulet5e29b762022-04-04 08:58:34 +02001847 (conn->flags & CO_FL_WAIT_XPRT) == 0) {
Christopher Fauletca679922022-07-20 13:24:04 +02001848 if (sc->flags & SC_FL_ISBACK)
1849 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001850 oc->flags |= CF_WRITE_NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +02001851 if (sc->state == SC_ST_CON)
1852 sc->state = SC_ST_RDY;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001853 }
1854
1855 /* Report EOS on the channel if it was reached from the mux point of
1856 * view.
1857 *
Willy Tarreau462b9892022-05-18 18:06:53 +02001858 * Note: This test is only required because sc_conn_process is also the SI
1859 * wake callback. Otherwise sc_conn_recv()/sc_conn_send() already take
Christopher Faulet5e29b762022-04-04 08:58:34 +02001860 * care of it.
1861 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001862 if (sc_ep_test(sc, SE_FL_EOS) && !(ic->flags & CF_SHUTR)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001863 /* we received a shutdown */
1864 ic->flags |= CF_READ_NULL;
1865 if (ic->flags & CF_AUTO_CLOSE)
1866 channel_shutw_now(ic);
Willy Tarreau0adb2812022-05-27 10:02:48 +02001867 sc_conn_read0(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001868 }
1869
1870 /* Report EOI on the channel if it was reached from the mux point of
1871 * view.
1872 *
Willy Tarreau462b9892022-05-18 18:06:53 +02001873 * Note: This test is only required because sc_conn_process is also the SI
1874 * wake callback. Otherwise sc_conn_recv()/sc_conn_send() already take
Christopher Faulet5e29b762022-04-04 08:58:34 +02001875 * care of it.
1876 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001877 if (sc_ep_test(sc, SE_FL_EOI) && !(ic->flags & CF_EOI))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001878 ic->flags |= (CF_EOI|CF_READ_PARTIAL);
1879
Willy Tarreau4596fe22022-05-17 19:07:51 +02001880 /* Second step : update the stream connector and channels, try to forward any
Christopher Faulet5e29b762022-04-04 08:58:34 +02001881 * pending data, then possibly wake the stream up based on the new
Willy Tarreau4596fe22022-05-17 19:07:51 +02001882 * stream connector status.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001883 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001884 sc_notify(sc);
1885 stream_release_buffers(__sc_strm(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001886 return 0;
1887}
1888
Willy Tarreau4596fe22022-05-17 19:07:51 +02001889/* This is the ->process() function for any stream connector's wait_event task.
1890 * It's assigned during the stream connector's initialization, for any type of
1891 * stream connector. Thus it is always safe to perform a tasklet_wakeup() on a
Willy Tarreaue68bc612022-05-27 11:23:05 +02001892 * stream connector, as the presence of the SC is checked there.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001893 */
Willy Tarreau462b9892022-05-18 18:06:53 +02001894struct task *sc_conn_io_cb(struct task *t, void *ctx, unsigned int state)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001895{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001896 struct stconn *sc = ctx;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001897 int ret = 0;
1898
Willy Tarreau0adb2812022-05-27 10:02:48 +02001899 if (!sc_conn(sc))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001900 return t;
1901
Willy Tarreau0adb2812022-05-27 10:02:48 +02001902 if (!(sc->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(sc_oc(sc)))
1903 ret = sc_conn_send(sc);
1904 if (!(sc->wait_event.events & SUB_RETRY_RECV))
1905 ret |= sc_conn_recv(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001906 if (ret != 0)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001907 sc_conn_process(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001908
Willy Tarreau0adb2812022-05-27 10:02:48 +02001909 stream_release_buffers(__sc_strm(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001910 return t;
1911}
1912
1913/* Callback to be used by applet handlers upon completion. It updates the stream
1914 * (which may or may not take this opportunity to try to forward data), then
Willy Tarreau4596fe22022-05-17 19:07:51 +02001915 * may re-enable the applet's based on the channels and stream connector's final
Christopher Faulet5e29b762022-04-04 08:58:34 +02001916 * states.
1917 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001918static int sc_applet_process(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001919{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001920 struct channel *ic = sc_ic(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001921
Willy Tarreau0adb2812022-05-27 10:02:48 +02001922 BUG_ON(!sc_appctx(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001923
1924 /* If the applet wants to write and the channel is closed, it's a
1925 * broken pipe and it must be reported.
1926 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001927 if (!sc_ep_test(sc, SE_FL_HAVE_NO_DATA) && (ic->flags & CF_SHUTR))
1928 sc_ep_set(sc, SE_FL_ERROR);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001929
1930 /* automatically mark the applet having data available if it reported
1931 * begin blocked by the channel.
1932 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001933 if ((sc->flags & (SC_FL_WONT_READ|SC_FL_NEED_BUFF|SC_FL_NEED_ROOM)) ||
1934 sc_ep_test(sc, SE_FL_APPLET_NEED_CONN))
1935 applet_have_more_data(__sc_appctx(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001936
Willy Tarreau4596fe22022-05-17 19:07:51 +02001937 /* update the stream connector, channels, and possibly wake the stream up */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001938 sc_notify(sc);
1939 stream_release_buffers(__sc_strm(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001940
Willy Tarreau19c65a92022-05-27 08:49:24 +02001941 /* sc_notify may have passed through chk_snd and released some blocking
Willy Tarreau15252cd2022-05-25 16:36:21 +02001942 * flags. Process_stream will consider those flags to wake up the
Christopher Faulet5e29b762022-04-04 08:58:34 +02001943 * appctx but in the case the task is not in runqueue we may have to
1944 * wakeup the appctx immediately.
1945 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001946 if (sc_is_recv_allowed(sc) || sc_is_send_allowed(sc))
1947 appctx_wakeup(__sc_appctx(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001948 return 0;
Christopher Faulet13045f02022-04-01 14:23:38 +02001949}
Christopher Fauletb68f77d2022-06-16 16:24:16 +02001950
1951
1952/* Prepares an endpoint upgrade. We don't now at this stage if the upgrade will
1953 * succeed or not and if the stconn will be reused by the new endpoint. Thus,
1954 * for now, only pretend the stconn is detached.
1955 */
1956void sc_conn_prepare_endp_upgrade(struct stconn *sc)
1957{
1958 BUG_ON(!sc_conn(sc) || !sc->app);
1959 sc_ep_clr(sc, SE_FL_T_MUX);
1960 sc_ep_set(sc, SE_FL_DETACHED);
1961}
1962
1963/* Endpoint upgrade failed. Retore the stconn state. */
1964void sc_conn_abort_endp_upgrade(struct stconn *sc)
1965{
1966 sc_ep_set(sc, SE_FL_T_MUX);
1967 sc_ep_clr(sc, SE_FL_DETACHED);
1968}
1969
1970/* Commit the endpoint upgrade. If stconn is attached, it means the new endpoint
1971 * use it. So we do nothing. Otherwise, the stconn will be destroy with the
1972 * overlying stream. So, it means we must commit the detach.
1973*/
1974void sc_conn_commit_endp_upgrade(struct stconn *sc)
1975{
1976 if (!sc_ep_test(sc, SE_FL_DETACHED))
1977 return;
1978 sc_detach_endp(&sc);
1979 /* Because it was already set as detached, the sedesc must be preserved */
1980 BUG_ON(!sc->sedesc);
1981}