blob: 07b9767b73ff1c4524bb429272ca2916413bd6b6 [file] [log] [blame]
Christopher Faulet1329f2a2021-12-16 17:32:56 +01001/*
Willy Tarreau4596fe22022-05-17 19:07:51 +02002 * stream connector management functions
Christopher Faulet1329f2a2021-12-16 17:32:56 +01003 *
4 * Copyright 2021 Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <haproxy/api.h>
Christopher Faulet37046632022-04-01 11:36:58 +020014#include <haproxy/applet.h>
Christopher Faulet1329f2a2021-12-16 17:32:56 +010015#include <haproxy/connection.h>
Christopher Faulet5e29b762022-04-04 08:58:34 +020016#include <haproxy/check.h>
17#include <haproxy/http_ana.h>
18#include <haproxy/pipe.h>
Christopher Faulet1329f2a2021-12-16 17:32:56 +010019#include <haproxy/pool.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020020#include <haproxy/sc_strm.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020021#include <haproxy/stconn.h>
Christopher Faulet1329f2a2021-12-16 17:32:56 +010022
Willy Tarreau4596fe22022-05-17 19:07:51 +020023DECLARE_POOL(pool_head_connstream, "stconn", sizeof(struct stconn));
Willy Tarreauea59b022022-05-17 17:53:22 +020024DECLARE_POOL(pool_head_sedesc, "sedesc", sizeof(struct sedesc));
Christopher Faulet1329f2a2021-12-16 17:32:56 +010025
Willy Tarreau3a3f4802022-05-17 18:28:19 +020026/* functions used by default on a detached stream connector */
Willy Tarreau0adb2812022-05-27 10:02:48 +020027static void sc_app_shutr(struct stconn *sc);
28static void sc_app_shutw(struct stconn *sc);
29static void sc_app_chk_rcv(struct stconn *sc);
30static void sc_app_chk_snd(struct stconn *sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +020031
Willy Tarreau3a3f4802022-05-17 18:28:19 +020032/* functions used on a mux-based stream connector */
Willy Tarreau0adb2812022-05-27 10:02:48 +020033static void sc_app_shutr_conn(struct stconn *sc);
34static void sc_app_shutw_conn(struct stconn *sc);
35static void sc_app_chk_rcv_conn(struct stconn *sc);
36static void sc_app_chk_snd_conn(struct stconn *sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +020037
Willy Tarreau3a3f4802022-05-17 18:28:19 +020038/* functions used on an applet-based stream connector */
Willy Tarreau0adb2812022-05-27 10:02:48 +020039static void sc_app_shutr_applet(struct stconn *sc);
40static void sc_app_shutw_applet(struct stconn *sc);
41static void sc_app_chk_rcv_applet(struct stconn *sc);
42static void sc_app_chk_snd_applet(struct stconn *sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +020043
Willy Tarreau0adb2812022-05-27 10:02:48 +020044static int sc_conn_process(struct stconn *sc);
45static int sc_conn_recv(struct stconn *sc);
46static int sc_conn_send(struct stconn *sc);
47static int sc_applet_process(struct stconn *sc);
Willy Tarreau2f2318d2022-05-18 10:17:16 +020048
Willy Tarreau3a3f4802022-05-17 18:28:19 +020049/* stream connector operations for connections */
50struct sc_app_ops sc_app_conn_ops = {
51 .chk_rcv = sc_app_chk_rcv_conn,
52 .chk_snd = sc_app_chk_snd_conn,
53 .shutr = sc_app_shutr_conn,
54 .shutw = sc_app_shutw_conn,
Willy Tarreau462b9892022-05-18 18:06:53 +020055 .wake = sc_conn_process,
Willy Tarreau2f2318d2022-05-18 10:17:16 +020056 .name = "STRM",
Christopher Faulet9ffddd52022-04-01 14:04:29 +020057};
58
Willy Tarreau3a3f4802022-05-17 18:28:19 +020059/* stream connector operations for embedded tasks */
60struct sc_app_ops sc_app_embedded_ops = {
61 .chk_rcv = sc_app_chk_rcv,
62 .chk_snd = sc_app_chk_snd,
63 .shutr = sc_app_shutr,
64 .shutw = sc_app_shutw,
Willy Tarreau2f2318d2022-05-18 10:17:16 +020065 .wake = NULL, /* may never be used */
66 .name = "NONE", /* may never be used */
Christopher Faulet9ffddd52022-04-01 14:04:29 +020067};
68
Willy Tarreau2f2318d2022-05-18 10:17:16 +020069/* stream connector operations for applets */
Willy Tarreau3a3f4802022-05-17 18:28:19 +020070struct sc_app_ops sc_app_applet_ops = {
71 .chk_rcv = sc_app_chk_rcv_applet,
72 .chk_snd = sc_app_chk_snd_applet,
73 .shutr = sc_app_shutr_applet,
74 .shutw = sc_app_shutw_applet,
Willy Tarreau19c65a92022-05-27 08:49:24 +020075 .wake = sc_applet_process,
Christopher Faulet5e29b762022-04-04 08:58:34 +020076 .name = "STRM",
77};
78
Willy Tarreau2f2318d2022-05-18 10:17:16 +020079/* stream connector for health checks on connections */
80struct sc_app_ops sc_app_check_ops = {
81 .chk_rcv = NULL,
82 .chk_snd = NULL,
83 .shutr = NULL,
84 .shutw = NULL,
85 .wake = wake_srv_chk,
86 .name = "CHCK",
87};
Christopher Faulet5e29b762022-04-04 08:58:34 +020088
Christopher Faulet9ed77422022-04-12 08:51:15 +020089/* Initializes an endpoint */
Willy Tarreauea59b022022-05-17 17:53:22 +020090void sedesc_init(struct sedesc *sedesc)
Christopher Fauletdb90f2a2022-03-22 16:06:25 +010091{
Willy Tarreauea59b022022-05-17 17:53:22 +020092 sedesc->se = NULL;
93 sedesc->conn = NULL;
Willy Tarreauc1054922022-05-18 07:43:52 +020094 sedesc->sc = NULL;
Christopher Faulet4c135682023-02-16 11:09:31 +010095 sedesc->lra = TICK_ETERNITY;
96 sedesc->fsb = TICK_ETERNITY;
Christopher Fauletf8413cb2023-02-07 16:06:14 +010097 sedesc->rex = sedesc->wex = TICK_ETERNITY;
Willy Tarreauea59b022022-05-17 17:53:22 +020098 se_fl_setall(sedesc, SE_FL_NONE);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +010099}
100
Christopher Faulet9ed77422022-04-12 08:51:15 +0200101/* Tries to alloc an endpoint and initialize it. Returns NULL on failure. */
Willy Tarreauea59b022022-05-17 17:53:22 +0200102struct sedesc *sedesc_new()
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100103{
Willy Tarreauea59b022022-05-17 17:53:22 +0200104 struct sedesc *sedesc;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100105
Willy Tarreauea59b022022-05-17 17:53:22 +0200106 sedesc = pool_alloc(pool_head_sedesc);
107 if (unlikely(!sedesc))
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100108 return NULL;
109
Willy Tarreauea59b022022-05-17 17:53:22 +0200110 sedesc_init(sedesc);
111 return sedesc;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100112}
113
Christopher Faulet9ed77422022-04-12 08:51:15 +0200114/* Releases an endpoint. It is the caller responsibility to be sure it is safe
115 * and it is not shared with another entity
116 */
Willy Tarreauea59b022022-05-17 17:53:22 +0200117void sedesc_free(struct sedesc *sedesc)
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100118{
Willy Tarreauea59b022022-05-17 17:53:22 +0200119 pool_free(pool_head_sedesc, sedesc);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100120}
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100121
Willy Tarreau4596fe22022-05-17 19:07:51 +0200122/* Tries to allocate a new stconn and initialize its main fields. On
Christopher Faulet9ed77422022-04-12 08:51:15 +0200123 * failure, nothing is allocated and NULL is returned. It is an internal
Willy Tarreaub605c422022-05-17 17:04:55 +0200124 * function. The caller must, at least, set the SE_FL_ORPHAN or SE_FL_DETACHED
Christopher Faulet9ed77422022-04-12 08:51:15 +0200125 * flag.
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100126 */
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200127static struct stconn *sc_new(struct sedesc *sedesc)
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100128{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200129 struct stconn *sc;
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100130
Willy Tarreau0adb2812022-05-27 10:02:48 +0200131 sc = pool_alloc(pool_head_connstream);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100132
Willy Tarreau0adb2812022-05-27 10:02:48 +0200133 if (unlikely(!sc))
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100134 goto alloc_error;
Christopher Fauletbb772d02022-03-22 15:28:36 +0100135
Willy Tarreau1d2c79a2022-05-27 11:15:19 +0200136 sc->obj_type = OBJ_TYPE_SC;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200137 sc->flags = SC_FL_NONE;
138 sc->state = SC_ST_INI;
Christopher Faulet5aaacfb2023-02-15 08:13:33 +0100139 sc->ioto = sc->hcto = TICK_ETERNITY;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200140 sc->app = NULL;
141 sc->app_ops = NULL;
142 sc->src = NULL;
143 sc->dst = NULL;
144 sc->wait_event.tasklet = NULL;
145 sc->wait_event.events = 0;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200146
Christopher Faulet9ed77422022-04-12 08:51:15 +0200147 /* If there is no endpoint, allocate a new one now */
Willy Tarreauea59b022022-05-17 17:53:22 +0200148 if (!sedesc) {
149 sedesc = sedesc_new();
150 if (unlikely(!sedesc))
Christopher Fauletb669d682022-03-22 18:37:19 +0100151 goto alloc_error;
152 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200153 sc->sedesc = sedesc;
154 sedesc->sc = sc;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100155
Willy Tarreau0adb2812022-05-27 10:02:48 +0200156 return sc;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100157
158 alloc_error:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200159 pool_free(pool_head_connstream, sc);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100160 return NULL;
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100161}
162
Willy Tarreau31219282022-05-27 16:21:33 +0200163/* Creates a new stream connector and its associated stream from a mux. <sd> must
164 * be defined. It returns NULL on error. On success, the new stream connector is
Willy Tarreaub605c422022-05-17 17:04:55 +0200165 * returned. In this case, SE_FL_ORPHAN flag is removed.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200166 */
Willy Tarreau31219282022-05-27 16:21:33 +0200167struct stconn *sc_new_from_endp(struct sedesc *sd, struct session *sess, struct buffer *input)
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100168{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200169 struct stconn *sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100170
Willy Tarreau31219282022-05-27 16:21:33 +0200171 sc = sc_new(sd);
Willy Tarreau0adb2812022-05-27 10:02:48 +0200172 if (unlikely(!sc))
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100173 return NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200174 if (unlikely(!stream_new(sess, sc, input))) {
175 pool_free(pool_head_connstream, sc);
Christopher Faulet3ab72c62022-09-27 09:18:20 +0200176 sd->sc = NULL;
177 se_fl_set(sd, SE_FL_ORPHAN);
178 return NULL;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100179 }
Willy Tarreau31219282022-05-27 16:21:33 +0200180 se_fl_clr(sd, SE_FL_ORPHAN);
Willy Tarreau0adb2812022-05-27 10:02:48 +0200181 return sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100182}
183
Willy Tarreau4596fe22022-05-17 19:07:51 +0200184/* Creates a new stream connector from an stream. There is no endpoint here, thus it
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200185 * will be created by sc_new(). So the SE_FL_DETACHED flag is set. It returns
Willy Tarreau4596fe22022-05-17 19:07:51 +0200186 * NULL on error. On success, the new stream connector is returned.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200187 */
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200188struct stconn *sc_new_from_strm(struct stream *strm, unsigned int flags)
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100189{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200190 struct stconn *sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100191
Willy Tarreau0adb2812022-05-27 10:02:48 +0200192 sc = sc_new(NULL);
193 if (unlikely(!sc))
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100194 return NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200195 sc->flags |= flags;
196 sc_ep_set(sc, SE_FL_DETACHED);
197 sc->app = &strm->obj_type;
198 sc->app_ops = &sc_app_embedded_ops;
199 return sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100200}
201
Willy Tarreau4596fe22022-05-17 19:07:51 +0200202/* Creates a new stream connector from an health-check. There is no endpoint here,
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200203 * thus it will be created by sc_new(). So the SE_FL_DETACHED flag is set. It
Willy Tarreau4596fe22022-05-17 19:07:51 +0200204 * returns NULL on error. On success, the new stream connector is returned.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200205 */
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200206struct stconn *sc_new_from_check(struct check *check, unsigned int flags)
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100207{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200208 struct stconn *sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100209
Willy Tarreau0adb2812022-05-27 10:02:48 +0200210 sc = sc_new(NULL);
211 if (unlikely(!sc))
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100212 return NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200213 sc->flags |= flags;
214 sc_ep_set(sc, SE_FL_DETACHED);
215 sc->app = &check->obj_type;
216 sc->app_ops = &sc_app_check_ops;
217 return sc;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100218}
219
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200220/* Releases a stconn previously allocated by sc_new(), as well as its
Christopher Faulet9ed77422022-04-12 08:51:15 +0200221 * endpoint, if it exists. This function is called internally or on error path.
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100222 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200223void sc_free(struct stconn *sc)
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100224{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200225 sockaddr_free(&sc->src);
226 sockaddr_free(&sc->dst);
227 if (sc->sedesc) {
228 BUG_ON(!sc_ep_test(sc, SE_FL_DETACHED));
229 sedesc_free(sc->sedesc);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100230 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200231 if (sc->wait_event.tasklet)
232 tasklet_free(sc->wait_event.tasklet);
233 pool_free(pool_head_connstream, sc);
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100234}
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100235
Willy Tarreau4596fe22022-05-17 19:07:51 +0200236/* Conditionally removes a stream connector if it is detached and if there is no app
Christopher Fauleteb50c012022-04-21 14:22:53 +0200237 * layer defined. Except on error path, this one must be used. if release, the
Willy Tarreaue68bc612022-05-27 11:23:05 +0200238 * pointer on the SC is set to NULL.
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200239 */
Willy Tarreaue68bc612022-05-27 11:23:05 +0200240static void sc_free_cond(struct stconn **scp)
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200241{
Willy Tarreaue68bc612022-05-27 11:23:05 +0200242 struct stconn *sc = *scp;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200243
Willy Tarreau0adb2812022-05-27 10:02:48 +0200244 if (!sc->app && (!sc->sedesc || sc_ep_test(sc, SE_FL_DETACHED))) {
245 sc_free(sc);
Willy Tarreaue68bc612022-05-27 11:23:05 +0200246 *scp = NULL;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200247 }
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200248}
249
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100250
Willy Tarreau4596fe22022-05-17 19:07:51 +0200251/* Attaches a stconn to a mux endpoint and sets the endpoint ctx. Returns
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500252 * -1 on error and 0 on success. SE_FL_DETACHED flag is removed. This function is
Christopher Faulet9ed77422022-04-12 08:51:15 +0200253 * called from a mux when it is attached to a stream or a health-check.
254 */
Willy Tarreau31219282022-05-27 16:21:33 +0200255int sc_attach_mux(struct stconn *sc, void *sd, void *ctx)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100256{
Christopher Faulet93882042022-01-19 14:56:50 +0100257 struct connection *conn = ctx;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200258 struct sedesc *sedesc = sc->sedesc;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100259
Willy Tarreau31219282022-05-27 16:21:33 +0200260 sedesc->se = sd;
Willy Tarreau798465b2022-05-17 18:20:02 +0200261 sedesc->conn = ctx;
262 se_fl_set(sedesc, SE_FL_T_MUX);
263 se_fl_clr(sedesc, SE_FL_DETACHED);
Christopher Faulet93882042022-01-19 14:56:50 +0100264 if (!conn->ctx)
Willy Tarreau0adb2812022-05-27 10:02:48 +0200265 conn->ctx = sc;
266 if (sc_strm(sc)) {
267 if (!sc->wait_event.tasklet) {
268 sc->wait_event.tasklet = tasklet_new();
269 if (!sc->wait_event.tasklet)
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200270 return -1;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200271 sc->wait_event.tasklet->process = sc_conn_io_cb;
272 sc->wait_event.tasklet->context = sc;
273 sc->wait_event.events = 0;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200274 }
275
Willy Tarreau0adb2812022-05-27 10:02:48 +0200276 sc->app_ops = &sc_app_conn_ops;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100277 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200278 else if (sc_check(sc)) {
279 if (!sc->wait_event.tasklet) {
280 sc->wait_event.tasklet = tasklet_new();
281 if (!sc->wait_event.tasklet)
Christopher Fauletc95eaef2022-05-18 15:57:15 +0200282 return -1;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200283 sc->wait_event.tasklet->process = srv_chk_io_cb;
284 sc->wait_event.tasklet->context = sc;
285 sc->wait_event.events = 0;
Christopher Fauletc95eaef2022-05-18 15:57:15 +0200286 }
287
Willy Tarreau0adb2812022-05-27 10:02:48 +0200288 sc->app_ops = &sc_app_check_ops;
Christopher Fauletc95eaef2022-05-18 15:57:15 +0200289 }
Christopher Faulet070b91b2022-03-31 19:27:18 +0200290 return 0;
Christopher Faulet93882042022-01-19 14:56:50 +0100291}
292
Willy Tarreau4596fe22022-05-17 19:07:51 +0200293/* Attaches a stconn to an applet endpoint and sets the endpoint
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500294 * ctx. Returns -1 on error and 0 on success. SE_FL_DETACHED flag is
Christopher Faulet9ed77422022-04-12 08:51:15 +0200295 * removed. This function is called by a stream when a backend applet is
296 * registered.
297 */
Willy Tarreau31219282022-05-27 16:21:33 +0200298static void sc_attach_applet(struct stconn *sc, void *sd)
Christopher Faulet93882042022-01-19 14:56:50 +0100299{
Willy Tarreau31219282022-05-27 16:21:33 +0200300 sc->sedesc->se = sd;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200301 sc_ep_set(sc, SE_FL_T_APPLET);
302 sc_ep_clr(sc, SE_FL_DETACHED);
303 if (sc_strm(sc))
304 sc->app_ops = &sc_app_applet_ops;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100305}
306
Willy Tarreau4596fe22022-05-17 19:07:51 +0200307/* Attaches a stconn to a app layer and sets the relevant
Willy Tarreaub605c422022-05-17 17:04:55 +0200308 * callbacks. Returns -1 on error and 0 on success. SE_FL_ORPHAN flag is
Christopher Faulet9ed77422022-04-12 08:51:15 +0200309 * removed. This function is called by a stream when it is created to attach it
Willy Tarreau4596fe22022-05-17 19:07:51 +0200310 * on the stream connector on the client side.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200311 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200312int sc_attach_strm(struct stconn *sc, struct stream *strm)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100313{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200314 sc->app = &strm->obj_type;
315 sc_ep_clr(sc, SE_FL_ORPHAN);
316 if (sc_ep_test(sc, SE_FL_T_MUX)) {
317 sc->wait_event.tasklet = tasklet_new();
318 if (!sc->wait_event.tasklet)
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200319 return -1;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200320 sc->wait_event.tasklet->process = sc_conn_io_cb;
321 sc->wait_event.tasklet->context = sc;
322 sc->wait_event.events = 0;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200323
Willy Tarreau0adb2812022-05-27 10:02:48 +0200324 sc->app_ops = &sc_app_conn_ops;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100325 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200326 else if (sc_ep_test(sc, SE_FL_T_APPLET)) {
327 sc->app_ops = &sc_app_applet_ops;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100328 }
329 else {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200330 sc->app_ops = &sc_app_embedded_ops;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100331 }
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100332 return 0;
333}
334
Willy Tarreau4596fe22022-05-17 19:07:51 +0200335/* Detaches the stconn from the endpoint, if any. For a connecrion, if a
Christopher Faulet9ed77422022-04-12 08:51:15 +0200336 * mux owns the connection ->detach() callback is called. Otherwise, it means
Willy Tarreau4596fe22022-05-17 19:07:51 +0200337 * the stream connector owns the connection. In this case the connection is closed
Christopher Faulet9ed77422022-04-12 08:51:15 +0200338 * and released. For an applet, the appctx is released. If still allocated, the
339 * endpoint is reset and flag as detached. If the app layer is also detached,
Willy Tarreau4596fe22022-05-17 19:07:51 +0200340 * the stream connector is released.
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100341 */
Willy Tarreaue68bc612022-05-27 11:23:05 +0200342static void sc_detach_endp(struct stconn **scp)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100343{
Willy Tarreaue68bc612022-05-27 11:23:05 +0200344 struct stconn *sc = *scp;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200345
Willy Tarreau0adb2812022-05-27 10:02:48 +0200346 if (!sc)
Christopher Fauleteb50c012022-04-21 14:22:53 +0200347 return;
348
Willy Tarreau0adb2812022-05-27 10:02:48 +0200349 if (sc_ep_test(sc, SE_FL_T_MUX)) {
350 struct connection *conn = __sc_conn(sc);
351 struct sedesc *sedesc = sc->sedesc;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100352
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100353 if (conn->mux) {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200354 if (sc->wait_event.events != 0)
355 conn->mux->unsubscribe(sc, sc->wait_event.events, &sc->wait_event);
Willy Tarreau798465b2022-05-17 18:20:02 +0200356 se_fl_set(sedesc, SE_FL_ORPHAN);
Willy Tarreauc1054922022-05-18 07:43:52 +0200357 sedesc->sc = NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200358 sc->sedesc = NULL;
Willy Tarreau798465b2022-05-17 18:20:02 +0200359 conn->mux->detach(sedesc);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100360 }
361 else {
362 /* It's too early to have a mux, let's just destroy
363 * the connection
364 */
365 conn_stop_tracking(conn);
366 conn_full_close(conn);
367 if (conn->destroy_cb)
368 conn->destroy_cb(conn);
369 conn_free(conn);
370 }
371 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200372 else if (sc_ep_test(sc, SE_FL_T_APPLET)) {
373 struct appctx *appctx = __sc_appctx(sc);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100374
Willy Tarreau0adb2812022-05-27 10:02:48 +0200375 sc_ep_set(sc, SE_FL_ORPHAN);
376 sc->sedesc->sc = NULL;
377 sc->sedesc = NULL;
Willy Tarreau1c3ead42022-05-10 19:42:22 +0200378 appctx_shut(appctx);
379 appctx_free(appctx);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100380 }
381
Willy Tarreau0adb2812022-05-27 10:02:48 +0200382 if (sc->sedesc) {
Willy Tarreauda59c892022-05-27 17:03:34 +0200383 /* the SD wasn't used and can be recycled */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200384 sc->sedesc->se = NULL;
385 sc->sedesc->conn = NULL;
Willy Tarreauda59c892022-05-27 17:03:34 +0200386 sc->sedesc->flags = 0;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200387 sc_ep_set(sc, SE_FL_DETACHED);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100388 }
389
Willy Tarreaue68bc612022-05-27 11:23:05 +0200390 /* FIXME: Rest SC for now but must be reviewed. SC flags are only
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100391 * connection related for now but this will evolved
392 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200393 sc->flags &= SC_FL_ISBACK;
394 if (sc_strm(sc))
395 sc->app_ops = &sc_app_embedded_ops;
Willy Tarreau2f2318d2022-05-18 10:17:16 +0200396 else
Willy Tarreau0adb2812022-05-27 10:02:48 +0200397 sc->app_ops = NULL;
Willy Tarreaue68bc612022-05-27 11:23:05 +0200398 sc_free_cond(scp);
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100399}
400
Willy Tarreau4596fe22022-05-17 19:07:51 +0200401/* Detaches the stconn from the app layer. If there is no endpoint attached
402 * to the stconn
Christopher Faulet9ed77422022-04-12 08:51:15 +0200403 */
Willy Tarreaue68bc612022-05-27 11:23:05 +0200404static void sc_detach_app(struct stconn **scp)
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100405{
Willy Tarreaue68bc612022-05-27 11:23:05 +0200406 struct stconn *sc = *scp;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200407
Willy Tarreau0adb2812022-05-27 10:02:48 +0200408 if (!sc)
Christopher Fauleteb50c012022-04-21 14:22:53 +0200409 return;
410
Willy Tarreau0adb2812022-05-27 10:02:48 +0200411 sc->app = NULL;
412 sc->app_ops = NULL;
413 sockaddr_free(&sc->src);
414 sockaddr_free(&sc->dst);
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200415
Willy Tarreau0adb2812022-05-27 10:02:48 +0200416 if (sc->wait_event.tasklet)
417 tasklet_free(sc->wait_event.tasklet);
418 sc->wait_event.tasklet = NULL;
419 sc->wait_event.events = 0;
Willy Tarreaue68bc612022-05-27 11:23:05 +0200420 sc_free_cond(scp);
Christopher Fauleteb50c012022-04-21 14:22:53 +0200421}
422
Willy Tarreau4596fe22022-05-17 19:07:51 +0200423/* Destroy the stconn. It is detached from its endpoint and its
424 * application. After this call, the stconn must be considered as released.
Christopher Fauleteb50c012022-04-21 14:22:53 +0200425 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200426void sc_destroy(struct stconn *sc)
Christopher Fauleteb50c012022-04-21 14:22:53 +0200427{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200428 sc_detach_endp(&sc);
429 sc_detach_app(&sc);
430 BUG_ON_HOT(sc);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100431}
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100432
Willy Tarreau4596fe22022-05-17 19:07:51 +0200433/* Resets the stream connector endpoint. It happens when the app layer want to renew
Christopher Faulet9ed77422022-04-12 08:51:15 +0200434 * its endpoint. For a connection retry for instance. If a mux or an applet is
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500435 * attached, a new endpoint is created. Returns -1 on error and 0 on success.
Christopher Fauleta6c4a482022-04-28 18:25:24 +0200436 *
Willy Tarreaub605c422022-05-17 17:04:55 +0200437 * Only SE_FL_ERROR flag is removed on the endpoint. Orther flags are preserved.
Christopher Fauleta6c4a482022-04-28 18:25:24 +0200438 * It is the caller responsibility to remove other flags if needed.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200439 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200440int sc_reset_endp(struct stconn *sc)
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100441{
Willy Tarreau31219282022-05-27 16:21:33 +0200442 struct sedesc *new_sd;
Christopher Fauletb041b232022-03-24 10:27:02 +0100443
Willy Tarreau0adb2812022-05-27 10:02:48 +0200444 BUG_ON(!sc->app);
Christopher Fauleta6c4a482022-04-28 18:25:24 +0200445
Willy Tarreau0adb2812022-05-27 10:02:48 +0200446 sc_ep_clr(sc, SE_FL_ERROR);
447 if (!__sc_endp(sc)) {
Christopher Fauletb041b232022-03-24 10:27:02 +0100448 /* endpoint not attached or attached to a mux with no
449 * target. Thus the endpoint will not be release but just
Willy Tarreau0adb2812022-05-27 10:02:48 +0200450 * reset. The app is still attached, the sc will not be
Christopher Fauleteb50c012022-04-21 14:22:53 +0200451 * released.
Christopher Fauletb041b232022-03-24 10:27:02 +0100452 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200453 sc_detach_endp(&sc);
Christopher Fauletb041b232022-03-24 10:27:02 +0100454 return 0;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100455 }
Christopher Fauletb041b232022-03-24 10:27:02 +0100456
457 /* allocate the new endpoint first to be able to set error if it
458 * fails */
Willy Tarreau31219282022-05-27 16:21:33 +0200459 new_sd = sedesc_new();
460 if (!unlikely(new_sd)) {
Willy Tarreau0adb2812022-05-27 10:02:48 +0200461 sc_ep_set(sc, SE_FL_ERROR);
Christopher Fauletb041b232022-03-24 10:27:02 +0100462 return -1;
463 }
464
Willy Tarreau0adb2812022-05-27 10:02:48 +0200465 /* The app is still attached, the sc will not be released */
466 sc_detach_endp(&sc);
Willy Tarreau6a378d12022-08-11 13:56:42 +0200467 BUG_ON(!sc);
Willy Tarreau0adb2812022-05-27 10:02:48 +0200468 BUG_ON(sc->sedesc);
Willy Tarreau31219282022-05-27 16:21:33 +0200469 sc->sedesc = new_sd;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200470 sc->sedesc->sc = sc;
471 sc_ep_set(sc, SE_FL_DETACHED);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100472 return 0;
473}
Christopher Faulet37046632022-04-01 11:36:58 +0200474
475
Willy Tarreaue68bc612022-05-27 11:23:05 +0200476/* Create an applet to handle a stream connector as a new appctx. The SC will
Christopher Faulet37046632022-04-01 11:36:58 +0200477 * wake it up every time it is solicited. The appctx must be deleted by the task
Willy Tarreau19c65a92022-05-27 08:49:24 +0200478 * handler using sc_detach_endp(), possibly from within the function itself.
Christopher Faulet37046632022-04-01 11:36:58 +0200479 * It also pre-initializes the applet's context and returns it (or NULL in case
480 * it could not be allocated).
481 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200482struct appctx *sc_applet_create(struct stconn *sc, struct applet *app)
Christopher Faulet37046632022-04-01 11:36:58 +0200483{
484 struct appctx *appctx;
485
Willy Tarreau0adb2812022-05-27 10:02:48 +0200486 DPRINTF(stderr, "registering handler %p for sc %p (was %p)\n", app, sc, sc_strm_task(sc));
Christopher Faulet37046632022-04-01 11:36:58 +0200487
Willy Tarreau0adb2812022-05-27 10:02:48 +0200488 appctx = appctx_new_here(app, sc->sedesc);
Christopher Faulet37046632022-04-01 11:36:58 +0200489 if (!appctx)
490 return NULL;
Willy Tarreau0adb2812022-05-27 10:02:48 +0200491 sc_attach_applet(sc, appctx);
492 appctx->t->nice = __sc_strm(sc)->task->nice;
Willy Tarreau90e8b452022-05-25 18:21:43 +0200493 applet_need_more_data(appctx);
Christopher Faulet37046632022-04-01 11:36:58 +0200494 appctx_wakeup(appctx);
Christopher Fauleta33ff7a2022-04-21 11:52:07 +0200495
Willy Tarreau0adb2812022-05-27 10:02:48 +0200496 sc->state = SC_ST_RDY;
Christopher Faulet37046632022-04-01 11:36:58 +0200497 return appctx;
498}
499
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100500/* Conditionnaly forward the close to the wirte side. It return 1 if it can be
501 * forwarded. It is the caller responsibility to forward the close to the write
502 * side. Otherwise, 0 is returned. In this case, CF_SHUTW_NOW flag may be set on
503 * the channel if we are only waiting for the outgoing data to be flushed.
504 */
505static inline int sc_cond_forward_shutw(struct stconn *sc)
506{
507 /* The close must not be forwarded */
508 if (!(sc_ic(sc)->flags & CF_SHUTR) || !(sc->flags & SC_FL_NOHALF))
509 return 0;
510
511 if (!channel_is_empty(sc_ic(sc))) {
512 /* the close to the write side cannot be forwarded now because
513 * we should flush outgoing data first. But instruct the output
514 * channel it should be done ASAP.
515 */
516 channel_shutw_now(sc_oc(sc));
517 return 0;
518 }
519
520 /* the close can be immediately forwarded to the write side */
521 return 1;
522}
523
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200524/*
Willy Tarreau4596fe22022-05-17 19:07:51 +0200525 * This function performs a shutdown-read on a detached stream connector in a
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200526 * connected or init state (it does nothing for other states). It either shuts
527 * the read side or marks itself as closed. The buffer flags are updated to
Willy Tarreaucb041662022-05-17 19:44:42 +0200528 * reflect the new state. If the stream connector has SC_FL_NOHALF, we also
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200529 * forward the close to the write side. The owner task is woken up if it exists.
530 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200531static void sc_app_shutr(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200532{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200533 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200534
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200535 if (ic->flags & CF_SHUTR)
536 return;
537 ic->flags |= CF_SHUTR;
Christopher Faulet4c135682023-02-16 11:09:31 +0100538 sc_ep_report_read_activity(sc);
Christopher Fauletf8413cb2023-02-07 16:06:14 +0100539 sc_ep_reset_rex(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200540
Willy Tarreau0adb2812022-05-27 10:02:48 +0200541 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200542 return;
543
Willy Tarreau0adb2812022-05-27 10:02:48 +0200544 if (sc_oc(sc)->flags & CF_SHUTW) {
545 sc->state = SC_ST_DIS;
Christopher Fauletca679922022-07-20 13:24:04 +0200546 if (sc->flags & SC_FL_ISBACK)
547 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200548 }
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100549 else if (sc_cond_forward_shutw(sc))
Willy Tarreau0adb2812022-05-27 10:02:48 +0200550 return sc_app_shutw(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200551
552 /* note that if the task exists, it must unregister itself once it runs */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200553 if (!(sc->flags & SC_FL_DONT_WAKE))
554 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200555}
556
557/*
Willy Tarreau4596fe22022-05-17 19:07:51 +0200558 * This function performs a shutdown-write on a detached stream connector in a
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200559 * connected or init state (it does nothing for other states). It either shuts
560 * the write side or marks itself as closed. The buffer flags are updated to
Willy Tarreaue68bc612022-05-27 11:23:05 +0200561 * reflect the new state. It does also close everything if the SC was marked as
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200562 * being in error state. The owner task is woken up if it exists.
563 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200564static void sc_app_shutw(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200565{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200566 struct channel *ic = sc_ic(sc);
567 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200568
569 oc->flags &= ~CF_SHUTW_NOW;
570 if (oc->flags & CF_SHUTW)
571 return;
572 oc->flags |= CF_SHUTW;
Christopher Fauletf8413cb2023-02-07 16:06:14 +0100573 sc_ep_reset_wex(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200574
Willy Tarreau0adb2812022-05-27 10:02:48 +0200575 if (tick_isset(sc->hcto)) {
Christopher Faulet5aaacfb2023-02-15 08:13:33 +0100576 sc->ioto = sc->hcto;
577 sc_ep_set_rex(sc, sc->ioto);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200578 }
579
Willy Tarreau0adb2812022-05-27 10:02:48 +0200580 switch (sc->state) {
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200581 case SC_ST_RDY:
582 case SC_ST_EST:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200583 /* we have to shut before closing, otherwise some short messages
584 * may never leave the system, especially when there are remaining
585 * unread data in the socket input buffer, or when nolinger is set.
Willy Tarreaucb041662022-05-17 19:44:42 +0200586 * However, if SC_FL_NOLINGER is explicitly set, we know there is
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200587 * no risk so we close both sides immediately.
588 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200589 if (!sc_ep_test(sc, SE_FL_ERROR) && !(sc->flags & SC_FL_NOLINGER) &&
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200590 !(ic->flags & (CF_SHUTR|CF_DONT_READ)))
591 return;
592
Willy Tarreau476c2802022-11-14 07:36:42 +0100593 __fallthrough;
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200594 case SC_ST_CON:
595 case SC_ST_CER:
596 case SC_ST_QUE:
597 case SC_ST_TAR:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200598 /* Note that none of these states may happen with applets */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200599 sc->state = SC_ST_DIS;
Willy Tarreau476c2802022-11-14 07:36:42 +0100600 __fallthrough;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200601 default:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200602 sc->flags &= ~SC_FL_NOLINGER;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200603 ic->flags |= CF_SHUTR;
Christopher Fauletf8413cb2023-02-07 16:06:14 +0100604 sc_ep_reset_rex(sc);
Christopher Fauletca679922022-07-20 13:24:04 +0200605 if (sc->flags & SC_FL_ISBACK)
606 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200607 }
608
609 /* note that if the task exists, it must unregister itself once it runs */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200610 if (!(sc->flags & SC_FL_DONT_WAKE))
611 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200612}
613
614/* default chk_rcv function for scheduled tasks */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200615static void sc_app_chk_rcv(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200616{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200617 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200618
Willy Tarreau0adb2812022-05-27 10:02:48 +0200619 DPRINTF(stderr, "%s: sc=%p, sc->state=%d ic->flags=%08x oc->flags=%08x\n",
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200620 __FUNCTION__,
Willy Tarreau0adb2812022-05-27 10:02:48 +0200621 sc, sc->state, ic->flags, sc_oc(sc)->flags);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200622
623 if (ic->pipe) {
624 /* stop reading */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200625 sc_need_room(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200626 }
627 else {
628 /* (re)start reading */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200629 if (!(sc->flags & SC_FL_DONT_WAKE))
630 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200631 }
632}
633
634/* default chk_snd function for scheduled tasks */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200635static void sc_app_chk_snd(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200636{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200637 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200638
Willy Tarreau0adb2812022-05-27 10:02:48 +0200639 DPRINTF(stderr, "%s: sc=%p, sc->state=%d ic->flags=%08x oc->flags=%08x\n",
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200640 __FUNCTION__,
Willy Tarreau0adb2812022-05-27 10:02:48 +0200641 sc, sc->state, sc_ic(sc)->flags, oc->flags);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200642
Willy Tarreau0adb2812022-05-27 10:02:48 +0200643 if (unlikely(sc->state != SC_ST_EST || (oc->flags & CF_SHUTW)))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200644 return;
645
Willy Tarreau0adb2812022-05-27 10:02:48 +0200646 if (!sc_ep_test(sc, SE_FL_WAIT_DATA) || /* not waiting for data */
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200647 channel_is_empty(oc)) /* called with nothing to send ! */
648 return;
649
650 /* Otherwise there are remaining data to be sent in the buffer,
651 * so we tell the handler.
652 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200653 sc_ep_clr(sc, SE_FL_WAIT_DATA);
Christopher Fauletf8413cb2023-02-07 16:06:14 +0100654 if (!tick_isset(sc_ep_wex(sc)))
Christopher Faulet5aaacfb2023-02-15 08:13:33 +0100655 sc_ep_set_wex(sc, sc->ioto);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200656
Willy Tarreau0adb2812022-05-27 10:02:48 +0200657 if (!(sc->flags & SC_FL_DONT_WAKE))
658 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200659}
660
661/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200662 * This function performs a shutdown-read on a stream connector attached to
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200663 * a connection in a connected or init state (it does nothing for other
664 * states). It either shuts the read side or marks itself as closed. The buffer
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200665 * flags are updated to reflect the new state. If the stream connector has
Willy Tarreaucb041662022-05-17 19:44:42 +0200666 * SC_FL_NOHALF, we also forward the close to the write side. If a control
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200667 * layer is defined, then it is supposed to be a socket layer and file
668 * descriptors are then shutdown or closed accordingly. The function
669 * automatically disables polling if needed.
670 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200671static void sc_app_shutr_conn(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200672{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200673 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200674
Willy Tarreau0adb2812022-05-27 10:02:48 +0200675 BUG_ON(!sc_conn(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200676
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200677 if (ic->flags & CF_SHUTR)
678 return;
679 ic->flags |= CF_SHUTR;
Christopher Fauletf8413cb2023-02-07 16:06:14 +0100680 sc_ep_reset_rex(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200681
Willy Tarreau0adb2812022-05-27 10:02:48 +0200682 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200683 return;
684
Willy Tarreau0adb2812022-05-27 10:02:48 +0200685 if (sc_oc(sc)->flags & CF_SHUTW) {
686 sc_conn_shut(sc);
687 sc->state = SC_ST_DIS;
Christopher Fauletca679922022-07-20 13:24:04 +0200688 if (sc->flags & SC_FL_ISBACK)
689 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200690 }
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100691 else if (sc_cond_forward_shutw(sc))
Willy Tarreau0adb2812022-05-27 10:02:48 +0200692 return sc_app_shutw_conn(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200693}
694
695/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200696 * This function performs a shutdown-write on a stream connector attached to
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200697 * a connection in a connected or init state (it does nothing for other
698 * states). It either shuts the write side or marks itself as closed. The
699 * buffer flags are updated to reflect the new state. It does also close
Willy Tarreaue68bc612022-05-27 11:23:05 +0200700 * everything if the SC was marked as being in error state. If there is a
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200701 * data-layer shutdown, it is called.
702 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200703static void sc_app_shutw_conn(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200704{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200705 struct channel *ic = sc_ic(sc);
706 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200707
Willy Tarreau0adb2812022-05-27 10:02:48 +0200708 BUG_ON(!sc_conn(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200709
710 oc->flags &= ~CF_SHUTW_NOW;
711 if (oc->flags & CF_SHUTW)
712 return;
713 oc->flags |= CF_SHUTW;
Christopher Fauletf8413cb2023-02-07 16:06:14 +0100714 sc_ep_reset_wex(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200715
Willy Tarreau0adb2812022-05-27 10:02:48 +0200716 if (tick_isset(sc->hcto)) {
Christopher Faulet5aaacfb2023-02-15 08:13:33 +0100717 sc->ioto = sc->hcto;
718 sc_ep_set_rex(sc, sc->ioto);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200719 }
720
Willy Tarreau0adb2812022-05-27 10:02:48 +0200721 switch (sc->state) {
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200722 case SC_ST_RDY:
723 case SC_ST_EST:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200724 /* we have to shut before closing, otherwise some short messages
725 * may never leave the system, especially when there are remaining
726 * unread data in the socket input buffer, or when nolinger is set.
Willy Tarreaucb041662022-05-17 19:44:42 +0200727 * However, if SC_FL_NOLINGER is explicitly set, we know there is
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200728 * no risk so we close both sides immediately.
729 */
730
Willy Tarreau0adb2812022-05-27 10:02:48 +0200731 if (sc_ep_test(sc, SE_FL_ERROR)) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200732 /* quick close, the socket is already shut anyway */
733 }
Willy Tarreau0adb2812022-05-27 10:02:48 +0200734 else if (sc->flags & SC_FL_NOLINGER) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200735 /* unclean data-layer shutdown, typically an aborted request
736 * or a forwarded shutdown from a client to a server due to
737 * option abortonclose. No need for the TLS layer to try to
738 * emit a shutdown message.
739 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200740 sc_conn_shutw(sc, CO_SHW_SILENT);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200741 }
742 else {
743 /* clean data-layer shutdown. This only happens on the
744 * frontend side, or on the backend side when forwarding
745 * a client close in TCP mode or in HTTP TUNNEL mode
746 * while option abortonclose is set. We want the TLS
747 * layer to try to signal it to the peer before we close.
748 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200749 sc_conn_shutw(sc, CO_SHW_NORMAL);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200750
751 if (!(ic->flags & (CF_SHUTR|CF_DONT_READ)))
752 return;
753 }
754
Willy Tarreau476c2802022-11-14 07:36:42 +0100755 __fallthrough;
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200756 case SC_ST_CON:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200757 /* we may have to close a pending connection, and mark the
758 * response buffer as shutr
759 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200760 sc_conn_shut(sc);
Willy Tarreau476c2802022-11-14 07:36:42 +0100761 __fallthrough;
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200762 case SC_ST_CER:
763 case SC_ST_QUE:
764 case SC_ST_TAR:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200765 sc->state = SC_ST_DIS;
Willy Tarreau476c2802022-11-14 07:36:42 +0100766 __fallthrough;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200767 default:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200768 sc->flags &= ~SC_FL_NOLINGER;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200769 ic->flags |= CF_SHUTR;
Christopher Fauletf8413cb2023-02-07 16:06:14 +0100770 sc_ep_reset_rex(sc);
Christopher Fauletca679922022-07-20 13:24:04 +0200771 if (sc->flags & SC_FL_ISBACK)
772 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200773 }
774}
775
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200776/* This function is used for inter-stream connector calls. It is called by the
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200777 * consumer to inform the producer side that it may be interested in checking
778 * for free space in the buffer. Note that it intentionally does not update
779 * timeouts, so that we can still check them later at wake-up. This function is
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200780 * dedicated to connection-based stream connectors.
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200781 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200782static void sc_app_chk_rcv_conn(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200783{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200784 BUG_ON(!sc_conn(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200785
786 /* (re)start reading */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200787 if (sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
788 tasklet_wakeup(sc->wait_event.tasklet);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200789}
790
791
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200792/* This function is used for inter-stream connector calls. It is called by the
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200793 * producer to inform the consumer side that it may be interested in checking
794 * for data in the buffer. Note that it intentionally does not update timeouts,
795 * so that we can still check them later at wake-up.
796 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200797static void sc_app_chk_snd_conn(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200798{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200799 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200800
Willy Tarreau0adb2812022-05-27 10:02:48 +0200801 BUG_ON(!sc_conn(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200802
Willy Tarreau0adb2812022-05-27 10:02:48 +0200803 if (unlikely(!sc_state_in(sc->state, SC_SB_RDY|SC_SB_EST) ||
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200804 (oc->flags & CF_SHUTW)))
805 return;
806
807 if (unlikely(channel_is_empty(oc))) /* called with nothing to send ! */
808 return;
809
810 if (!oc->pipe && /* spliced data wants to be forwarded ASAP */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200811 !sc_ep_test(sc, SE_FL_WAIT_DATA)) /* not waiting for data */
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200812 return;
813
Willy Tarreau0adb2812022-05-27 10:02:48 +0200814 if (!(sc->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(sc_oc(sc)))
815 sc_conn_send(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200816
Willy Tarreau0adb2812022-05-27 10:02:48 +0200817 if (sc_ep_test(sc, SE_FL_ERROR | SE_FL_ERR_PENDING) || sc_is_conn_error(sc)) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200818 /* Write error on the file descriptor */
Christopher Faulet7f6aa562022-10-17 10:21:19 +0200819 if (sc->state >= SC_ST_CON && sc_ep_test(sc, SE_FL_EOS))
Willy Tarreau0adb2812022-05-27 10:02:48 +0200820 sc_ep_set(sc, SE_FL_ERROR);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200821 goto out_wakeup;
822 }
823
824 /* OK, so now we know that some data might have been sent, and that we may
825 * have to poll first. We have to do that too if the buffer is not empty.
826 */
827 if (channel_is_empty(oc)) {
828 /* the connection is established but we can't write. Either the
829 * buffer is empty, or we just refrain from sending because the
830 * ->o limit was reached. Maybe we just wrote the last
831 * chunk and need to close.
832 */
833 if (((oc->flags & (CF_SHUTW|CF_AUTO_CLOSE|CF_SHUTW_NOW)) ==
834 (CF_AUTO_CLOSE|CF_SHUTW_NOW)) &&
Willy Tarreau0adb2812022-05-27 10:02:48 +0200835 sc_state_in(sc->state, SC_SB_RDY|SC_SB_EST)) {
836 sc_shutw(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200837 goto out_wakeup;
838 }
839
840 if ((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == 0)
Willy Tarreau0adb2812022-05-27 10:02:48 +0200841 sc_ep_set(sc, SE_FL_WAIT_DATA);
Christopher Fauletf8413cb2023-02-07 16:06:14 +0100842 sc_ep_reset_wex(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200843 }
844 else {
845 /* Otherwise there are remaining data to be sent in the buffer,
846 * which means we have to poll before doing so.
847 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200848 sc_ep_clr(sc, SE_FL_WAIT_DATA);
Christopher Fauletf8413cb2023-02-07 16:06:14 +0100849 if (!tick_isset(sc_ep_wex(sc)))
Christopher Faulet5aaacfb2023-02-15 08:13:33 +0100850 sc_ep_set_wex(sc, sc->ioto);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200851 }
852
Christopher Faulet2e56a732023-01-26 16:18:09 +0100853 if (likely(oc->flags & CF_WRITE_EVENT)) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200854 /* update timeout if we have written something */
Christopher Faulet2e56a732023-01-26 16:18:09 +0100855 if (!(oc->flags & CF_SHUTW) && !channel_is_empty(oc))
Christopher Faulet5aaacfb2023-02-15 08:13:33 +0100856 sc_ep_set_wex(sc, sc->ioto);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200857
Christopher Fauletf8413cb2023-02-07 16:06:14 +0100858 if (tick_isset(sc_ep_rex(sc)) && !(sc->flags & SC_FL_INDEP_STR)) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200859 /* Note: to prevent the client from expiring read timeouts
860 * during writes, we refresh it. We only do this if the
861 * interface is not configured for "independent streams",
862 * because for some applications it's better not to do this,
863 * for instance when continuously exchanging small amounts
864 * of data which can full the socket buffers long before a
865 * write timeout is detected.
866 */
Christopher Faulet5aaacfb2023-02-15 08:13:33 +0100867 sc_ep_set_rex(sc, sc->ioto);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200868 }
869 }
870
871 /* in case of special condition (error, shutdown, end of write...), we
872 * have to notify the task.
873 */
Christopher Faulet71c486b2023-02-09 14:14:38 +0100874 if (likely((oc->flags & CF_SHUTW) ||
875 ((oc->flags & CF_WRITE_EVENT) && sc->state < SC_ST_EST) ||
876 ((oc->flags & CF_WAKE_WRITE) &&
877 ((channel_is_empty(oc) && !oc->to_forward) ||
878 !sc_state_in(sc->state, SC_SB_EST))))) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200879 out_wakeup:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200880 if (!(sc->flags & SC_FL_DONT_WAKE))
881 task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200882 }
883}
884
885/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200886 * This function performs a shutdown-read on a stream connector attached to an
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200887 * applet in a connected or init state (it does nothing for other states). It
888 * either shuts the read side or marks itself as closed. The buffer flags are
Willy Tarreaucb041662022-05-17 19:44:42 +0200889 * updated to reflect the new state. If the stream connector has SC_FL_NOHALF,
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200890 * we also forward the close to the write side. The owner task is woken up if
891 * it exists.
892 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200893static void sc_app_shutr_applet(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200894{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200895 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200896
Willy Tarreau0adb2812022-05-27 10:02:48 +0200897 BUG_ON(!sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200898
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200899 if (ic->flags & CF_SHUTR)
900 return;
901 ic->flags |= CF_SHUTR;
Christopher Fauletf8413cb2023-02-07 16:06:14 +0100902 sc_ep_reset_rex(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200903
904 /* Note: on shutr, we don't call the applet */
905
Willy Tarreau0adb2812022-05-27 10:02:48 +0200906 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200907 return;
908
Willy Tarreau0adb2812022-05-27 10:02:48 +0200909 if (sc_oc(sc)->flags & CF_SHUTW) {
910 appctx_shut(__sc_appctx(sc));
911 sc->state = SC_ST_DIS;
Christopher Fauletca679922022-07-20 13:24:04 +0200912 if (sc->flags & SC_FL_ISBACK)
913 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200914 }
Christopher Fauleteb3f26d2023-02-08 16:18:48 +0100915 else if (sc_cond_forward_shutw(sc))
Willy Tarreau0adb2812022-05-27 10:02:48 +0200916 return sc_app_shutw_applet(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200917}
918
919/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200920 * This function performs a shutdown-write on a stream connector attached to an
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200921 * applet in a connected or init state (it does nothing for other states). It
922 * either shuts the write side or marks itself as closed. The buffer flags are
923 * updated to reflect the new state. It does also close everything if the SI
924 * was marked as being in error state. The owner task is woken up if it exists.
925 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200926static void sc_app_shutw_applet(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200927{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200928 struct channel *ic = sc_ic(sc);
929 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200930
Willy Tarreau0adb2812022-05-27 10:02:48 +0200931 BUG_ON(!sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200932
933 oc->flags &= ~CF_SHUTW_NOW;
934 if (oc->flags & CF_SHUTW)
935 return;
936 oc->flags |= CF_SHUTW;
Christopher Fauletf8413cb2023-02-07 16:06:14 +0100937 sc_ep_reset_wex(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200938
Willy Tarreau0adb2812022-05-27 10:02:48 +0200939 if (tick_isset(sc->hcto)) {
Christopher Faulet5aaacfb2023-02-15 08:13:33 +0100940 sc->ioto = sc->hcto;
941 sc_ep_set_rex(sc, sc->ioto);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200942 }
943
944 /* on shutw we always wake the applet up */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200945 appctx_wakeup(__sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200946
Willy Tarreau0adb2812022-05-27 10:02:48 +0200947 switch (sc->state) {
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200948 case SC_ST_RDY:
949 case SC_ST_EST:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200950 /* we have to shut before closing, otherwise some short messages
951 * may never leave the system, especially when there are remaining
952 * unread data in the socket input buffer, or when nolinger is set.
Willy Tarreaucb041662022-05-17 19:44:42 +0200953 * However, if SC_FL_NOLINGER is explicitly set, we know there is
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200954 * no risk so we close both sides immediately.
955 */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200956 if (!sc_ep_test(sc, SE_FL_ERROR) && !(sc->flags & SC_FL_NOLINGER) &&
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200957 !(ic->flags & (CF_SHUTR|CF_DONT_READ)))
958 return;
959
Willy Tarreau476c2802022-11-14 07:36:42 +0100960 __fallthrough;
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200961 case SC_ST_CON:
962 case SC_ST_CER:
963 case SC_ST_QUE:
964 case SC_ST_TAR:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200965 /* Note that none of these states may happen with applets */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200966 appctx_shut(__sc_appctx(sc));
967 sc->state = SC_ST_DIS;
Willy Tarreau476c2802022-11-14 07:36:42 +0100968 __fallthrough;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200969 default:
Willy Tarreau0adb2812022-05-27 10:02:48 +0200970 sc->flags &= ~SC_FL_NOLINGER;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200971 ic->flags |= CF_SHUTR;
Christopher Fauletf8413cb2023-02-07 16:06:14 +0100972 sc_ep_reset_rex(sc);
Christopher Fauletca679922022-07-20 13:24:04 +0200973 if (sc->flags & SC_FL_ISBACK)
974 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200975 }
976}
977
978/* chk_rcv function for applets */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200979static void sc_app_chk_rcv_applet(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200980{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200981 struct channel *ic = sc_ic(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200982
Willy Tarreau0adb2812022-05-27 10:02:48 +0200983 BUG_ON(!sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200984
Willy Tarreau0adb2812022-05-27 10:02:48 +0200985 DPRINTF(stderr, "%s: sc=%p, sc->state=%d ic->flags=%08x oc->flags=%08x\n",
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200986 __FUNCTION__,
Willy Tarreau0adb2812022-05-27 10:02:48 +0200987 sc, sc->state, ic->flags, sc_oc(sc)->flags);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200988
989 if (!ic->pipe) {
990 /* (re)start reading */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200991 appctx_wakeup(__sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200992 }
993}
994
995/* chk_snd function for applets */
Willy Tarreau0adb2812022-05-27 10:02:48 +0200996static void sc_app_chk_snd_applet(struct stconn *sc)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200997{
Willy Tarreau0adb2812022-05-27 10:02:48 +0200998 struct channel *oc = sc_oc(sc);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200999
Willy Tarreau0adb2812022-05-27 10:02:48 +02001000 BUG_ON(!sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +02001001
Willy Tarreau0adb2812022-05-27 10:02:48 +02001002 DPRINTF(stderr, "%s: sc=%p, sc->state=%d ic->flags=%08x oc->flags=%08x\n",
Christopher Faulet9ffddd52022-04-01 14:04:29 +02001003 __FUNCTION__,
Willy Tarreau0adb2812022-05-27 10:02:48 +02001004 sc, sc->state, sc_ic(sc)->flags, oc->flags);
Christopher Faulet9ffddd52022-04-01 14:04:29 +02001005
Willy Tarreau0adb2812022-05-27 10:02:48 +02001006 if (unlikely(sc->state != SC_ST_EST || (oc->flags & CF_SHUTW)))
Christopher Faulet9ffddd52022-04-01 14:04:29 +02001007 return;
1008
Christopher Faulet04f03e12022-06-01 17:35:34 +02001009 /* we only wake the applet up if it was waiting for some data and is ready to consume it */
1010 if (!sc_ep_test(sc, SE_FL_WAIT_DATA) || sc_ep_test(sc, SE_FL_WONT_CONSUME))
Christopher Faulet9ffddd52022-04-01 14:04:29 +02001011 return;
1012
Christopher Fauletf8413cb2023-02-07 16:06:14 +01001013 if (!tick_isset(sc_ep_wex(sc)))
Christopher Faulet5aaacfb2023-02-15 08:13:33 +01001014 sc_ep_set_wex(sc, sc->ioto);
Christopher Faulet9ffddd52022-04-01 14:04:29 +02001015
1016 if (!channel_is_empty(oc)) {
1017 /* (re)start sending */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001018 appctx_wakeup(__sc_appctx(sc));
Christopher Faulet9ffddd52022-04-01 14:04:29 +02001019 }
1020}
Christopher Faulet13045f02022-04-01 14:23:38 +02001021
1022
1023/* This function is designed to be called from within the stream handler to
Willy Tarreau4596fe22022-05-17 19:07:51 +02001024 * update the input channel's expiration timer and the stream connector's
Christopher Faulet13045f02022-04-01 14:23:38 +02001025 * Rx flags based on the channel's flags. It needs to be called only once
1026 * after the channel's flags have settled down, and before they are cleared,
1027 * though it doesn't harm to call it as often as desired (it just slightly
1028 * hurts performance). It must not be called from outside of the stream
1029 * handler, as what it does will be used to compute the stream task's
1030 * expiration.
1031 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001032void sc_update_rx(struct stconn *sc)
Christopher Faulet13045f02022-04-01 14:23:38 +02001033{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001034 struct channel *ic = sc_ic(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +02001035
Willy Tarreau676c8db2022-05-24 16:22:24 +02001036 if (ic->flags & CF_SHUTR)
Christopher Faulet13045f02022-04-01 14:23:38 +02001037 return;
Christopher Faulet13045f02022-04-01 14:23:38 +02001038
1039 /* Read not closed, update FD status and timeout for reads */
1040 if (ic->flags & CF_DONT_READ)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001041 sc_wont_read(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +02001042 else
Willy Tarreau0adb2812022-05-27 10:02:48 +02001043 sc_will_read(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +02001044
Christopher Faulet407210a2023-02-14 11:01:51 +01001045 if ((ic->flags & CF_EOI) || sc->flags & (SC_FL_WONT_READ|SC_FL_NEED_BUFF|SC_FL_NEED_ROOM))
Christopher Fauletf8413cb2023-02-07 16:06:14 +01001046 sc_ep_reset_rex(sc);
1047 else if (!tick_isset(sc_ep_rex(sc)))
Christopher Faulet5aaacfb2023-02-15 08:13:33 +01001048 sc_ep_set_rex(sc, sc->ioto);
Christopher Faulet13045f02022-04-01 14:23:38 +02001049
Willy Tarreau0adb2812022-05-27 10:02:48 +02001050 sc_chk_rcv(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +02001051}
1052
1053/* This function is designed to be called from within the stream handler to
Willy Tarreau4596fe22022-05-17 19:07:51 +02001054 * update the output channel's expiration timer and the stream connector's
Christopher Faulet13045f02022-04-01 14:23:38 +02001055 * Tx flags based on the channel's flags. It needs to be called only once
1056 * after the channel's flags have settled down, and before they are cleared,
1057 * though it doesn't harm to call it as often as desired (it just slightly
1058 * hurts performance). It must not be called from outside of the stream
1059 * handler, as what it does will be used to compute the stream task's
1060 * expiration.
1061 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001062void sc_update_tx(struct stconn *sc)
Christopher Faulet13045f02022-04-01 14:23:38 +02001063{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001064 struct channel *oc = sc_oc(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +02001065
1066 if (oc->flags & CF_SHUTW)
1067 return;
1068
1069 /* Write not closed, update FD status and timeout for writes */
1070 if (channel_is_empty(oc)) {
1071 /* stop writing */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001072 if (!sc_ep_test(sc, SE_FL_WAIT_DATA)) {
Christopher Faulet13045f02022-04-01 14:23:38 +02001073 if ((oc->flags & CF_SHUTW_NOW) == 0)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001074 sc_ep_set(sc, SE_FL_WAIT_DATA);
Christopher Fauletf8413cb2023-02-07 16:06:14 +01001075 sc_ep_reset_wex(sc);
Christopher Faulet13045f02022-04-01 14:23:38 +02001076 }
1077 return;
1078 }
1079
1080 /* (re)start writing and update timeout. Note: we don't recompute the timeout
1081 * every time we get here, otherwise it would risk never to expire. We only
1082 * update it if is was not yet set. The stream socket handler will already
1083 * have updated it if there has been a completed I/O.
1084 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001085 sc_ep_clr(sc, SE_FL_WAIT_DATA);
Christopher Fauletf8413cb2023-02-07 16:06:14 +01001086 if (!tick_isset(sc_ep_wex(sc))) {
Christopher Faulet5aaacfb2023-02-15 08:13:33 +01001087 sc_ep_set_wex(sc, sc->ioto);
Christopher Fauletf8413cb2023-02-07 16:06:14 +01001088 if (tick_isset(sc_ep_rex(sc)) && !(sc->flags & SC_FL_INDEP_STR)) {
Christopher Faulet13045f02022-04-01 14:23:38 +02001089 /* Note: depending on the protocol, we don't know if we're waiting
1090 * for incoming data or not. So in order to prevent the socket from
1091 * expiring read timeouts during writes, we refresh the read timeout,
1092 * except if it was already infinite or if we have explicitly setup
1093 * independent streams.
1094 */
Christopher Faulet5aaacfb2023-02-15 08:13:33 +01001095 sc_ep_set_rex(sc, sc->ioto);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001096 }
1097 }
1098}
1099
Willy Tarreau19c65a92022-05-27 08:49:24 +02001100/* This function is the equivalent to sc_update() except that it's
Christopher Faulet5e29b762022-04-04 08:58:34 +02001101 * designed to be called from outside the stream handlers, typically the lower
1102 * layers (applets, connections) after I/O completion. After updating the stream
1103 * interface and timeouts, it will try to forward what can be forwarded, then to
1104 * wake the associated task up if an important event requires special handling.
Willy Tarreau15252cd2022-05-25 16:36:21 +02001105 * It may update SE_FL_WAIT_DATA and/or SC_FL_NEED_ROOM, that the callers are
Christopher Faulet5e29b762022-04-04 08:58:34 +02001106 * encouraged to watch to take appropriate action.
Willy Tarreau19c65a92022-05-27 08:49:24 +02001107 * It should not be called from within the stream itself, sc_update()
Christopher Faulet5e29b762022-04-04 08:58:34 +02001108 * is designed for this.
1109 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001110static void sc_notify(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001111{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001112 struct channel *ic = sc_ic(sc);
1113 struct channel *oc = sc_oc(sc);
Willy Tarreaue68bc612022-05-27 11:23:05 +02001114 struct stconn *sco = sc_opposite(sc);
Willy Tarreau0adb2812022-05-27 10:02:48 +02001115 struct task *task = sc_strm_task(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001116
1117 /* process consumer side */
1118 if (channel_is_empty(oc)) {
Willy Tarreau0adb2812022-05-27 10:02:48 +02001119 struct connection *conn = sc_conn(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001120
1121 if (((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW) &&
Willy Tarreau0adb2812022-05-27 10:02:48 +02001122 (sc->state == SC_ST_EST) && (!conn || !(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS))))
1123 sc_shutw(sc);
Christopher Fauletf8413cb2023-02-07 16:06:14 +01001124 sc_ep_reset_wex(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001125 }
1126
1127 /* indicate that we may be waiting for data from the output channel or
1128 * we're about to close and can't expect more data if SHUTW_NOW is there.
1129 */
1130 if (!(oc->flags & (CF_SHUTW|CF_SHUTW_NOW)))
Willy Tarreau0adb2812022-05-27 10:02:48 +02001131 sc_ep_set(sc, SE_FL_WAIT_DATA);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001132 else if ((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001133 sc_ep_clr(sc, SE_FL_WAIT_DATA);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001134
1135 /* update OC timeouts and wake the other side up if it's waiting for room */
Christopher Faulet2e56a732023-01-26 16:18:09 +01001136 if (oc->flags & (CF_WRITE_EVENT)) {
1137 if (sc_ep_test(sc, SE_FL_ERR_PENDING|SE_FL_ERROR) &&
Christopher Faulet5e29b762022-04-04 08:58:34 +02001138 !channel_is_empty(oc))
Christopher Fauletf8413cb2023-02-07 16:06:14 +01001139 if (tick_isset(sc_ep_wex(sc)))
Christopher Faulet5aaacfb2023-02-15 08:13:33 +01001140 sc_ep_set_wex(sc, sc->ioto);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001141
Willy Tarreau0adb2812022-05-27 10:02:48 +02001142 if (!(sc->flags & SC_FL_INDEP_STR))
Christopher Fauletf8413cb2023-02-07 16:06:14 +01001143 if (tick_isset(sc_ep_rex(sc)))
Christopher Faulet5aaacfb2023-02-15 08:13:33 +01001144 sc_ep_set_rex(sc, sc->ioto);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001145 }
1146
1147 if (oc->flags & CF_DONT_READ)
Willy Tarreaue68bc612022-05-27 11:23:05 +02001148 sc_wont_read(sco);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001149 else
Willy Tarreaue68bc612022-05-27 11:23:05 +02001150 sc_will_read(sco);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001151
1152 /* Notify the other side when we've injected data into the IC that
1153 * needs to be forwarded. We can do fast-forwarding as soon as there
1154 * are output data, but we avoid doing this if some of the data are
1155 * not yet scheduled for being forwarded, because it is very likely
1156 * that it will be done again immediately afterwards once the following
Willy Tarreau15252cd2022-05-25 16:36:21 +02001157 * data are parsed (eg: HTTP chunking). We only clear SC_FL_NEED_ROOM
1158 * once we've emptied *some* of the output buffer, and not just when
1159 * there is available room, because applets are often forced to stop
1160 * before the buffer is full. We must not stop based on input data
1161 * alone because an HTTP parser might need more data to complete the
1162 * parsing.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001163 */
1164 if (!channel_is_empty(ic) &&
Willy Tarreaue68bc612022-05-27 11:23:05 +02001165 sc_ep_test(sco, SE_FL_WAIT_DATA) &&
Christopher Faulet5e29b762022-04-04 08:58:34 +02001166 (!(ic->flags & CF_EXPECT_MORE) || c_full(ic) || ci_data(ic) == 0 || ic->pipe)) {
1167 int new_len, last_len;
1168
1169 last_len = co_data(ic);
1170 if (ic->pipe)
1171 last_len += ic->pipe->data;
1172
Willy Tarreaue68bc612022-05-27 11:23:05 +02001173 sc_chk_snd(sco);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001174
1175 new_len = co_data(ic);
1176 if (ic->pipe)
1177 new_len += ic->pipe->data;
1178
1179 /* check if the consumer has freed some space either in the
1180 * buffer or in the pipe.
1181 */
1182 if (new_len < last_len)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001183 sc_have_room(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001184 }
1185
1186 if (!(ic->flags & CF_DONT_READ))
Willy Tarreau0adb2812022-05-27 10:02:48 +02001187 sc_will_read(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001188
Willy Tarreau0adb2812022-05-27 10:02:48 +02001189 sc_chk_rcv(sc);
Willy Tarreaue68bc612022-05-27 11:23:05 +02001190 sc_chk_rcv(sco);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001191
Christopher Faulet407210a2023-02-14 11:01:51 +01001192 if (ic->flags & (CF_EOI|CF_SHUTR) || sc_ep_test(sc, SE_FL_APPLET_NEED_CONN) ||
Willy Tarreau0adb2812022-05-27 10:02:48 +02001193 (sc->flags & (SC_FL_WONT_READ|SC_FL_NEED_BUFF|SC_FL_NEED_ROOM))) {
Christopher Fauletf8413cb2023-02-07 16:06:14 +01001194 sc_ep_reset_rex(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001195 }
Christopher Faulet285f7612022-12-12 08:28:55 +01001196 else if ((ic->flags & (CF_SHUTR|CF_READ_EVENT)) == CF_READ_EVENT) {
Willy Tarreauf61dd192022-05-27 09:00:19 +02001197 /* we must re-enable reading if sc_chk_snd() has freed some space */
Christopher Fauletf8413cb2023-02-07 16:06:14 +01001198 if (tick_isset(sc_ep_rex(sc)))
Christopher Faulet5aaacfb2023-02-15 08:13:33 +01001199 sc_ep_set_rex(sc, sc->ioto);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001200 }
1201
1202 /* wake the task up only when needed */
Christopher Faulet285f7612022-12-12 08:28:55 +01001203 if (/* changes on the production side that must be handled:
Christopher Faulet2e56a732023-01-26 16:18:09 +01001204 * - An error on receipt: SE_FL_ERROR
Christopher Faulet285f7612022-12-12 08:28:55 +01001205 * - A read event: shutdown for reads (CF_READ_EVENT + SHUTR)
1206 * end of input (CF_READ_EVENT + CF_EOI)
1207 * data received and no fast-forwarding (CF_READ_EVENT + !to_forward)
1208 * read event while consumer side is not established (CF_READ_EVENT + sco->state != SC_ST_EST)
1209 */
1210 ((ic->flags & CF_READ_EVENT) && ((ic->flags & (CF_SHUTR|CF_EOI)) || !ic->to_forward || sco->state != SC_ST_EST)) ||
Christopher Faulet2e56a732023-01-26 16:18:09 +01001211 sc_ep_test(sc, SE_FL_ERROR) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001212
1213 /* changes on the consumption side */
Christopher Faulet2e56a732023-01-26 16:18:09 +01001214 sc_ep_test(sc, SE_FL_ERR_PENDING) ||
Christopher Fauletd8988412022-12-20 18:10:04 +01001215 ((oc->flags & CF_WRITE_EVENT) &&
1216 ((sc->state < SC_ST_EST) ||
1217 (oc->flags & CF_SHUTW) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001218 (((oc->flags & CF_WAKE_WRITE) ||
Christopher Fauletd8988412022-12-20 18:10:04 +01001219 !(oc->flags & (CF_AUTO_CLOSE|CF_SHUTW_NOW|CF_SHUTW))) &&
1220 (sco->state != SC_ST_EST ||
1221 (channel_is_empty(oc) && !oc->to_forward)))))) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001222 task_wakeup(task, TASK_WOKEN_IO);
1223 }
Christopher Faulet5e29b762022-04-04 08:58:34 +02001224
Christopher Faulet2e56a732023-01-26 16:18:09 +01001225 if (ic->flags & CF_READ_EVENT)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001226 ic->flags &= ~CF_READ_DONTWAIT;
1227}
1228
1229/*
1230 * This function propagates a null read received on a socket-based connection.
Willy Tarreaucb041662022-05-17 19:44:42 +02001231 * It updates the stream connector. If the stream connector has SC_FL_NOHALF,
Christopher Faulet5e29b762022-04-04 08:58:34 +02001232 * the close is also forwarded to the write side as an abort.
1233 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001234static void sc_conn_read0(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001235{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001236 struct channel *ic = sc_ic(sc);
1237 struct channel *oc = sc_oc(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001238
Willy Tarreau0adb2812022-05-27 10:02:48 +02001239 BUG_ON(!sc_conn(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001240
Christopher Faulet5e29b762022-04-04 08:58:34 +02001241 if (ic->flags & CF_SHUTR)
1242 return;
1243 ic->flags |= CF_SHUTR;
Christopher Faulet4c135682023-02-16 11:09:31 +01001244 sc_ep_report_read_activity(sc);
Christopher Fauletf8413cb2023-02-07 16:06:14 +01001245 sc_ep_reset_rex(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001246
Willy Tarreau0adb2812022-05-27 10:02:48 +02001247 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001248 return;
1249
1250 if (oc->flags & CF_SHUTW)
1251 goto do_close;
1252
Christopher Fauleteb3f26d2023-02-08 16:18:48 +01001253 if (sc_cond_forward_shutw(sc)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001254 /* we want to immediately forward this close to the write side */
1255 /* force flag on ssl to keep stream in cache */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001256 sc_conn_shutw(sc, CO_SHW_SILENT);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001257 goto do_close;
1258 }
1259
1260 /* otherwise that's just a normal read shutdown */
1261 return;
1262
1263 do_close:
Willy Tarreauf61dd192022-05-27 09:00:19 +02001264 /* OK we completely close the socket here just as if we went through sc_shut[rw]() */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001265 sc_conn_shut(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001266
1267 oc->flags &= ~CF_SHUTW_NOW;
1268 oc->flags |= CF_SHUTW;
Christopher Fauletf8413cb2023-02-07 16:06:14 +01001269 sc_ep_reset_wex(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001270
Willy Tarreau0adb2812022-05-27 10:02:48 +02001271 sc->state = SC_ST_DIS;
Christopher Fauletca679922022-07-20 13:24:04 +02001272 if (sc->flags & SC_FL_ISBACK)
1273 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001274 return;
1275}
1276
1277/*
1278 * This is the callback which is called by the connection layer to receive data
1279 * into the buffer from the connection. It iterates over the mux layer's
1280 * rcv_buf function.
1281 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001282static int sc_conn_recv(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001283{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001284 struct connection *conn = __sc_conn(sc);
1285 struct channel *ic = sc_ic(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001286 int ret, max, cur_read = 0;
1287 int read_poll = MAX_READ_POLL_LOOPS;
1288 int flags = 0;
1289
1290 /* If not established yet, do nothing. */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001291 if (sc->state != SC_ST_EST)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001292 return 0;
1293
Willy Tarreau462b9892022-05-18 18:06:53 +02001294 /* If another call to sc_conn_recv() failed, and we subscribed to
Christopher Faulet5e29b762022-04-04 08:58:34 +02001295 * recv events already, give up now.
1296 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001297 if (sc->wait_event.events & SUB_RETRY_RECV)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001298 return 0;
1299
1300 /* maybe we were called immediately after an asynchronous shutr */
1301 if (ic->flags & CF_SHUTR)
1302 return 1;
1303
1304 /* we must wait because the mux is not installed yet */
1305 if (!conn->mux)
1306 return 0;
1307
1308 /* stop here if we reached the end of data */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001309 if (sc_ep_test(sc, SE_FL_EOS))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001310 goto end_recv;
1311
1312 /* stop immediately on errors. Note that we DON'T want to stop on
1313 * POLL_ERR, as the poller might report a write error while there
1314 * are still data available in the recv buffer. This typically
1315 * happens when we send too large a request to a backend server
1316 * which rejects it before reading it all.
1317 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001318 if (!sc_ep_test(sc, SE_FL_RCV_MORE)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001319 if (!conn_xprt_ready(conn))
1320 return 0;
Willy Tarreau0adb2812022-05-27 10:02:48 +02001321 if (sc_ep_test(sc, SE_FL_ERROR))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001322 goto end_recv;
1323 }
1324
1325 /* prepare to detect if the mux needs more room */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001326 sc_ep_clr(sc, SE_FL_WANT_ROOM);
Christopher Faulet341a5782023-02-10 17:37:11 +01001327 BUG_ON(sc_waiting_room(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001328
1329 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) && !co_data(ic) &&
1330 global.tune.idle_timer &&
1331 (unsigned short)(now_ms - ic->last_read) >= global.tune.idle_timer) {
1332 /* The buffer was empty and nothing was transferred for more
1333 * than one second. This was caused by a pause and not by
1334 * congestion. Reset any streaming mode to reduce latency.
1335 */
1336 ic->xfer_small = 0;
1337 ic->xfer_large = 0;
1338 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
1339 }
1340
1341 /* First, let's see if we may splice data across the channel without
1342 * using a buffer.
1343 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001344 if (sc_ep_test(sc, SE_FL_MAY_SPLICE) &&
Christopher Faulet5e29b762022-04-04 08:58:34 +02001345 (ic->pipe || ic->to_forward >= MIN_SPLICE_FORWARD) &&
1346 ic->flags & CF_KERN_SPLICING) {
1347 if (c_data(ic)) {
1348 /* We're embarrassed, there are already data pending in
1349 * the buffer and we don't want to have them at two
1350 * locations at a time. Let's indicate we need some
1351 * place and ask the consumer to hurry.
1352 */
1353 flags |= CO_RFL_BUF_FLUSH;
1354 goto abort_splice;
1355 }
1356
1357 if (unlikely(ic->pipe == NULL)) {
1358 if (pipes_used >= global.maxpipes || !(ic->pipe = get_pipe())) {
1359 ic->flags &= ~CF_KERN_SPLICING;
1360 goto abort_splice;
1361 }
1362 }
1363
Willy Tarreau0adb2812022-05-27 10:02:48 +02001364 ret = conn->mux->rcv_pipe(sc, ic->pipe, ic->to_forward);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001365 if (ret < 0) {
1366 /* splice not supported on this end, let's disable it */
1367 ic->flags &= ~CF_KERN_SPLICING;
1368 goto abort_splice;
1369 }
1370
1371 if (ret > 0) {
1372 if (ic->to_forward != CHN_INFINITE_FORWARD)
1373 ic->to_forward -= ret;
1374 ic->total += ret;
1375 cur_read += ret;
Christopher Faulet285f7612022-12-12 08:28:55 +01001376 ic->flags |= CF_READ_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001377 }
1378
Willy Tarreau0adb2812022-05-27 10:02:48 +02001379 if (sc_ep_test(sc, SE_FL_EOS | SE_FL_ERROR))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001380 goto end_recv;
1381
1382 if (conn->flags & CO_FL_WAIT_ROOM) {
1383 /* the pipe is full or we have read enough data that it
1384 * could soon be full. Let's stop before needing to poll.
1385 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001386 sc_need_room(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001387 goto done_recv;
1388 }
1389
1390 /* splice not possible (anymore), let's go on on standard copy */
1391 }
1392
1393 abort_splice:
1394 if (ic->pipe && unlikely(!ic->pipe->data)) {
1395 put_pipe(ic->pipe);
1396 ic->pipe = NULL;
1397 }
1398
Willy Tarreau0adb2812022-05-27 10:02:48 +02001399 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 +02001400 /* don't break splicing by reading, but still call rcv_buf()
1401 * to pass the flag.
1402 */
1403 goto done_recv;
1404 }
1405
1406 /* now we'll need a input buffer for the stream */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001407 if (!sc_alloc_ibuf(sc, &(__sc_strm(sc)->buffer_wait)))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001408 goto end_recv;
1409
1410 /* For an HTX stream, if the buffer is stuck (no output data with some
1411 * input data) and if the HTX message is fragmented or if its free space
1412 * wraps, we force an HTX deframentation. It is a way to have a
1413 * contiguous free space nad to let the mux to copy as much data as
1414 * possible.
1415 *
1416 * NOTE: A possible optim may be to let the mux decides if defrag is
1417 * required or not, depending on amount of data to be xferred.
1418 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001419 if (IS_HTX_STRM(__sc_strm(sc)) && !co_data(ic)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001420 struct htx *htx = htxbuf(&ic->buf);
1421
1422 if (htx_is_not_empty(htx) && ((htx->flags & HTX_FL_FRAGMENTED) || htx_space_wraps(htx)))
1423 htx_defrag(htx, NULL, 0);
1424 }
1425
1426 /* Instruct the mux it must subscribed for read events */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001427 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 +02001428
1429 /* Important note : if we're called with POLL_IN|POLL_HUP, it means the read polling
1430 * was enabled, which implies that the recv buffer was not full. So we have a guarantee
1431 * that if such an event is not handled above in splice, it will be handled here by
1432 * recv().
1433 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001434 while (sc_ep_test(sc, SE_FL_RCV_MORE) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001435 (!(conn->flags & CO_FL_HANDSHAKE) &&
Willy Tarreau0adb2812022-05-27 10:02:48 +02001436 (!sc_ep_test(sc, SE_FL_ERROR | SE_FL_EOS)) && !(ic->flags & CF_SHUTR))) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001437 int cur_flags = flags;
1438
1439 /* Compute transient CO_RFL_* flags */
1440 if (co_data(ic)) {
1441 cur_flags |= (CO_RFL_BUF_WET | CO_RFL_BUF_NOT_STUCK);
1442 }
1443
1444 /* <max> may be null. This is the mux responsibility to set
Willy Tarreaue68bc612022-05-27 11:23:05 +02001445 * SE_FL_RCV_MORE on the SC if more space is needed.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001446 */
1447 max = channel_recv_max(ic);
Willy Tarreau0adb2812022-05-27 10:02:48 +02001448 ret = conn->mux->rcv_buf(sc, &ic->buf, max, cur_flags);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001449
Willy Tarreau0adb2812022-05-27 10:02:48 +02001450 if (sc_ep_test(sc, SE_FL_WANT_ROOM)) {
Willy Tarreaub605c422022-05-17 17:04:55 +02001451 /* SE_FL_WANT_ROOM must not be reported if the channel's
Christopher Faulet5e29b762022-04-04 08:58:34 +02001452 * buffer is empty.
1453 */
1454 BUG_ON(c_empty(ic));
1455
Willy Tarreau0adb2812022-05-27 10:02:48 +02001456 sc_need_room(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001457 /* Add READ_PARTIAL because some data are pending but
1458 * cannot be xferred to the channel
1459 */
Christopher Faulet285f7612022-12-12 08:28:55 +01001460 ic->flags |= CF_READ_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001461 }
1462
1463 if (ret <= 0) {
1464 /* if we refrained from reading because we asked for a
1465 * flush to satisfy rcv_pipe(), we must not subscribe
1466 * and instead report that there's not enough room
1467 * here to proceed.
1468 */
1469 if (flags & CO_RFL_BUF_FLUSH)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001470 sc_need_room(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001471 break;
1472 }
1473
1474 cur_read += ret;
1475
1476 /* if we're allowed to directly forward data, we must update ->o */
1477 if (ic->to_forward && !(ic->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
1478 unsigned long fwd = ret;
1479 if (ic->to_forward != CHN_INFINITE_FORWARD) {
1480 if (fwd > ic->to_forward)
1481 fwd = ic->to_forward;
1482 ic->to_forward -= fwd;
1483 }
1484 c_adv(ic, fwd);
1485 }
1486
Christopher Faulet285f7612022-12-12 08:28:55 +01001487 ic->flags |= CF_READ_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001488 ic->total += ret;
1489
1490 /* End-of-input reached, we can leave. In this case, it is
Willy Tarreaue68bc612022-05-27 11:23:05 +02001491 * important to break the loop to not block the SC because of
Christopher Faulet5e29b762022-04-04 08:58:34 +02001492 * the channel's policies.This way, we are still able to receive
1493 * shutdowns.
1494 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001495 if (sc_ep_test(sc, SE_FL_EOI))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001496 break;
1497
1498 if ((ic->flags & CF_READ_DONTWAIT) || --read_poll <= 0) {
1499 /* we're stopped by the channel's policy */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001500 sc_wont_read(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001501 break;
1502 }
1503
1504 /* if too many bytes were missing from last read, it means that
1505 * it's pointless trying to read again because the system does
1506 * not have them in buffers.
1507 */
1508 if (ret < max) {
1509 /* if a streamer has read few data, it may be because we
1510 * have exhausted system buffers. It's not worth trying
1511 * again.
1512 */
1513 if (ic->flags & CF_STREAMER) {
1514 /* we're stopped by the channel's policy */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001515 sc_wont_read(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001516 break;
1517 }
1518
1519 /* if we read a large block smaller than what we requested,
1520 * it's almost certain we'll never get anything more.
1521 */
1522 if (ret >= global.tune.recv_enough) {
1523 /* we're stopped by the channel's policy */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001524 sc_wont_read(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001525 break;
1526 }
1527 }
1528
1529 /* if we are waiting for more space, don't try to read more data
1530 * right now.
1531 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001532 if (sc->flags & (SC_FL_WONT_READ|SC_FL_NEED_BUFF|SC_FL_NEED_ROOM))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001533 break;
1534 } /* while !flags */
1535
1536 done_recv:
1537 if (cur_read) {
1538 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) &&
1539 (cur_read <= ic->buf.size / 2)) {
1540 ic->xfer_large = 0;
1541 ic->xfer_small++;
1542 if (ic->xfer_small >= 3) {
1543 /* we have read less than half of the buffer in
1544 * one pass, and this happened at least 3 times.
1545 * This is definitely not a streamer.
1546 */
1547 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
1548 }
1549 else if (ic->xfer_small >= 2) {
1550 /* if the buffer has been at least half full twice,
1551 * we receive faster than we send, so at least it
1552 * is not a "fast streamer".
1553 */
1554 ic->flags &= ~CF_STREAMER_FAST;
1555 }
1556 }
1557 else if (!(ic->flags & CF_STREAMER_FAST) &&
1558 (cur_read >= ic->buf.size - global.tune.maxrewrite)) {
1559 /* we read a full buffer at once */
1560 ic->xfer_small = 0;
1561 ic->xfer_large++;
1562 if (ic->xfer_large >= 3) {
1563 /* we call this buffer a fast streamer if it manages
1564 * to be filled in one call 3 consecutive times.
1565 */
1566 ic->flags |= (CF_STREAMER | CF_STREAMER_FAST);
1567 }
1568 }
1569 else {
1570 ic->xfer_small = 0;
1571 ic->xfer_large = 0;
1572 }
1573 ic->last_read = now_ms;
Christopher Faulet4c135682023-02-16 11:09:31 +01001574 sc_ep_report_read_activity(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001575 }
1576
1577 end_recv:
1578 ret = (cur_read != 0);
1579
1580 /* Report EOI on the channel if it was reached from the mux point of
1581 * view. */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001582 if (sc_ep_test(sc, SE_FL_EOI) && !(ic->flags & CF_EOI)) {
Christopher Faulet4c135682023-02-16 11:09:31 +01001583 sc_ep_report_read_activity(sc);
Christopher Faulet285f7612022-12-12 08:28:55 +01001584 ic->flags |= (CF_EOI|CF_READ_EVENT);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001585 ret = 1;
1586 }
1587
Willy Tarreau0adb2812022-05-27 10:02:48 +02001588 if (sc_ep_test(sc, SE_FL_ERROR))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001589 ret = 1;
Willy Tarreau0adb2812022-05-27 10:02:48 +02001590 else if (sc_ep_test(sc, SE_FL_EOS)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001591 /* we received a shutdown */
Christopher Faulet6e1bbc42022-12-12 08:08:15 +01001592 ic->flags |= CF_READ_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001593 if (ic->flags & CF_AUTO_CLOSE)
1594 channel_shutw_now(ic);
Willy Tarreau0adb2812022-05-27 10:02:48 +02001595 sc_conn_read0(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001596 ret = 1;
1597 }
Willy Tarreau0adb2812022-05-27 10:02:48 +02001598 else if (!(sc->flags & (SC_FL_WONT_READ|SC_FL_NEED_BUFF|SC_FL_NEED_ROOM)) &&
Willy Tarreau15252cd2022-05-25 16:36:21 +02001599 !(ic->flags & CF_SHUTR)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001600 /* Subscribe to receive events if we're blocking on I/O */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001601 conn->mux->subscribe(sc, SUB_RETRY_RECV, &sc->wait_event);
1602 se_have_no_more_data(sc->sedesc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001603 } else {
Willy Tarreau0adb2812022-05-27 10:02:48 +02001604 se_have_more_data(sc->sedesc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001605 ret = 1;
1606 }
1607 return ret;
1608}
1609
Willy Tarreau4596fe22022-05-17 19:07:51 +02001610/* This tries to perform a synchronous receive on the stream connector to
Christopher Faulet5e29b762022-04-04 08:58:34 +02001611 * try to collect last arrived data. In practice it's only implemented on
Willy Tarreau4596fe22022-05-17 19:07:51 +02001612 * stconns. Returns 0 if nothing was done, non-zero if new data or a
Christopher Faulet5e29b762022-04-04 08:58:34 +02001613 * shutdown were collected. This may result on some delayed receive calls
1614 * to be programmed and performed later, though it doesn't provide any
1615 * such guarantee.
1616 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001617int sc_conn_sync_recv(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001618{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001619 if (!sc_state_in(sc->state, SC_SB_RDY|SC_SB_EST))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001620 return 0;
1621
Willy Tarreau0adb2812022-05-27 10:02:48 +02001622 if (!sc_mux_ops(sc))
Willy Tarreau4596fe22022-05-17 19:07:51 +02001623 return 0; // only stconns are supported
Christopher Faulet5e29b762022-04-04 08:58:34 +02001624
Willy Tarreau0adb2812022-05-27 10:02:48 +02001625 if (sc->wait_event.events & SUB_RETRY_RECV)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001626 return 0; // already subscribed
1627
Willy Tarreau0adb2812022-05-27 10:02:48 +02001628 if (!sc_is_recv_allowed(sc))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001629 return 0; // already failed
1630
Willy Tarreau0adb2812022-05-27 10:02:48 +02001631 return sc_conn_recv(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001632}
1633
1634/*
1635 * This function is called to send buffer data to a stream socket.
1636 * It calls the mux layer's snd_buf function. It relies on the
1637 * caller to commit polling changes. The caller should check conn->flags
1638 * for errors.
1639 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001640static int sc_conn_send(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001641{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001642 struct connection *conn = __sc_conn(sc);
1643 struct stream *s = __sc_strm(sc);
1644 struct channel *oc = sc_oc(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001645 int ret;
1646 int did_send = 0;
1647
Willy Tarreau0adb2812022-05-27 10:02:48 +02001648 if (sc_ep_test(sc, SE_FL_ERROR | SE_FL_ERR_PENDING) || sc_is_conn_error(sc)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001649 /* We're probably there because the tasklet was woken up,
1650 * but process_stream() ran before, detected there were an
Willy Tarreaue68bc612022-05-27 11:23:05 +02001651 * error and put the SC back to SC_ST_TAR. There's still
Christopher Faulet5e29b762022-04-04 08:58:34 +02001652 * CO_FL_ERROR on the connection but we don't want to add
Willy Tarreaub605c422022-05-17 17:04:55 +02001653 * SE_FL_ERROR back, so give up
Christopher Faulet5e29b762022-04-04 08:58:34 +02001654 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001655 if (sc->state < SC_ST_CON)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001656 return 0;
Christopher Faulet7f6aa562022-10-17 10:21:19 +02001657 if (sc_ep_test(sc, SE_FL_EOS))
1658 sc_ep_set(sc, SE_FL_ERROR);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001659 return 1;
1660 }
1661
1662 /* We're already waiting to be able to send, give up */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001663 if (sc->wait_event.events & SUB_RETRY_SEND)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001664 return 0;
1665
1666 /* we might have been called just after an asynchronous shutw */
1667 if (oc->flags & CF_SHUTW)
1668 return 1;
1669
1670 /* we must wait because the mux is not installed yet */
1671 if (!conn->mux)
1672 return 0;
1673
1674 if (oc->pipe && conn->xprt->snd_pipe && conn->mux->snd_pipe) {
Willy Tarreau0adb2812022-05-27 10:02:48 +02001675 ret = conn->mux->snd_pipe(sc, oc->pipe);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001676 if (ret > 0)
1677 did_send = 1;
1678
1679 if (!oc->pipe->data) {
1680 put_pipe(oc->pipe);
1681 oc->pipe = NULL;
1682 }
1683
1684 if (oc->pipe)
1685 goto end;
1686 }
1687
1688 /* At this point, the pipe is empty, but we may still have data pending
1689 * in the normal buffer.
1690 */
1691 if (co_data(oc)) {
1692 /* when we're here, we already know that there is no spliced
1693 * data left, and that there are sendable buffered data.
1694 */
1695
1696 /* check if we want to inform the kernel that we're interested in
1697 * sending more data after this call. We want this if :
1698 * - we're about to close after this last send and want to merge
1699 * the ongoing FIN with the last segment.
1700 * - we know we can't send everything at once and must get back
1701 * here because of unaligned data
1702 * - there is still a finite amount of data to forward
1703 * The test is arranged so that the most common case does only 2
1704 * tests.
1705 */
1706 unsigned int send_flag = 0;
1707
1708 if ((!(oc->flags & (CF_NEVER_WAIT|CF_SEND_DONTWAIT)) &&
1709 ((oc->to_forward && oc->to_forward != CHN_INFINITE_FORWARD) ||
1710 (oc->flags & CF_EXPECT_MORE) ||
1711 (IS_HTX_STRM(s) &&
1712 (!(oc->flags & (CF_EOI|CF_SHUTR)) && htx_expect_more(htxbuf(&oc->buf)))))) ||
1713 ((oc->flags & CF_ISRESP) &&
1714 ((oc->flags & (CF_AUTO_CLOSE|CF_SHUTW_NOW)) == (CF_AUTO_CLOSE|CF_SHUTW_NOW))))
1715 send_flag |= CO_SFL_MSG_MORE;
1716
1717 if (oc->flags & CF_STREAMER)
1718 send_flag |= CO_SFL_STREAMER;
1719
1720 if (s->txn && s->txn->flags & TX_L7_RETRY && !b_data(&s->txn->l7_buffer)) {
1721 /* If we want to be able to do L7 retries, copy
1722 * the data we're about to send, so that we are able
1723 * to resend them if needed
1724 */
1725 /* Try to allocate a buffer if we had none.
1726 * If it fails, the next test will just
1727 * disable the l7 retries by setting
1728 * l7_conn_retries to 0.
1729 */
1730 if (s->txn->req.msg_state != HTTP_MSG_DONE)
1731 s->txn->flags &= ~TX_L7_RETRY;
1732 else {
1733 if (b_alloc(&s->txn->l7_buffer) == NULL)
1734 s->txn->flags &= ~TX_L7_RETRY;
1735 else {
1736 memcpy(b_orig(&s->txn->l7_buffer),
1737 b_orig(&oc->buf),
1738 b_size(&oc->buf));
1739 s->txn->l7_buffer.head = co_data(oc);
1740 b_add(&s->txn->l7_buffer, co_data(oc));
1741 }
1742
1743 }
1744 }
1745
Willy Tarreau0adb2812022-05-27 10:02:48 +02001746 ret = conn->mux->snd_buf(sc, &oc->buf, co_data(oc), send_flag);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001747 if (ret > 0) {
1748 did_send = 1;
1749 c_rew(oc, ret);
1750 c_realign_if_empty(oc);
1751
1752 if (!co_data(oc)) {
1753 /* Always clear both flags once everything has been sent, they're one-shot */
1754 oc->flags &= ~(CF_EXPECT_MORE | CF_SEND_DONTWAIT);
1755 }
1756 /* if some data remain in the buffer, it's only because the
1757 * system buffers are full, we will try next time.
1758 */
Christopher Faulet13045f02022-04-01 14:23:38 +02001759 }
1760 }
Christopher Faulet5e29b762022-04-04 08:58:34 +02001761
1762 end:
1763 if (did_send) {
Christopher Fauletd8988412022-12-20 18:10:04 +01001764 oc->flags |= CF_WRITE_EVENT | CF_WROTE_DATA;
Willy Tarreau0adb2812022-05-27 10:02:48 +02001765 if (sc->state == SC_ST_CON)
1766 sc->state = SC_ST_RDY;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001767
Willy Tarreau0adb2812022-05-27 10:02:48 +02001768 sc_have_room(sc_opposite(sc));
Christopher Faulet4c135682023-02-16 11:09:31 +01001769 sc_ep_report_send_activity(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001770 }
Christopher Faulet4c135682023-02-16 11:09:31 +01001771 else
1772 sc_ep_report_blocked_send(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001773
Willy Tarreau0adb2812022-05-27 10:02:48 +02001774 if (sc_ep_test(sc, SE_FL_ERROR | SE_FL_ERR_PENDING)) {
Christopher Faulet2e56a732023-01-26 16:18:09 +01001775 oc->flags |= CF_WRITE_EVENT;
Christopher Faulet7f6aa562022-10-17 10:21:19 +02001776 if (sc_ep_test(sc, SE_FL_EOS))
Christopher Faulet2e56a732023-01-26 16:18:09 +01001777 sc_ep_set(sc, SE_FL_ERROR);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001778 return 1;
1779 }
1780
1781 /* We couldn't send all of our data, let the mux know we'd like to send more */
1782 if (!channel_is_empty(oc))
Willy Tarreau0adb2812022-05-27 10:02:48 +02001783 conn->mux->subscribe(sc, SUB_RETRY_SEND, &sc->wait_event);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001784 return did_send;
1785}
1786
Christopher Fauletd8988412022-12-20 18:10:04 +01001787/* perform a synchronous send() for the stream connector. The CF_WRITE_EVENT
1788 * flag are cleared prior to the attempt, and will possibly be updated in case
1789 * of success.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001790 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001791void sc_conn_sync_send(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001792{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001793 struct channel *oc = sc_oc(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001794
Christopher Fauletd8988412022-12-20 18:10:04 +01001795 oc->flags &= ~CF_WRITE_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001796
1797 if (oc->flags & CF_SHUTW)
1798 return;
1799
1800 if (channel_is_empty(oc))
1801 return;
1802
Willy Tarreau0adb2812022-05-27 10:02:48 +02001803 if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001804 return;
1805
Willy Tarreau0adb2812022-05-27 10:02:48 +02001806 if (!sc_mux_ops(sc))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001807 return;
1808
Willy Tarreau0adb2812022-05-27 10:02:48 +02001809 sc_conn_send(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001810}
1811
1812/* Called by I/O handlers after completion.. It propagates
Willy Tarreau4596fe22022-05-17 19:07:51 +02001813 * connection flags to the stream connector, updates the stream (which may or
Christopher Faulet5e29b762022-04-04 08:58:34 +02001814 * may not take this opportunity to try to forward data), then update the
Willy Tarreau4596fe22022-05-17 19:07:51 +02001815 * connection's polling based on the channels and stream connector's final
Christopher Faulet5e29b762022-04-04 08:58:34 +02001816 * states. The function always returns 0.
1817 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001818static int sc_conn_process(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001819{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001820 struct connection *conn = __sc_conn(sc);
1821 struct channel *ic = sc_ic(sc);
1822 struct channel *oc = sc_oc(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001823
1824 BUG_ON(!conn);
1825
1826 /* If we have data to send, try it now */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001827 if (!channel_is_empty(oc) && !(sc->wait_event.events & SUB_RETRY_SEND))
1828 sc_conn_send(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001829
Willy Tarreau4596fe22022-05-17 19:07:51 +02001830 /* First step, report to the stream connector what was detected at the
Christopher Faulet5e29b762022-04-04 08:58:34 +02001831 * connection layer : errors and connection establishment.
Willy Tarreaub605c422022-05-17 17:04:55 +02001832 * Only add SE_FL_ERROR if we're connected, or we're attempting to
Christopher Faulet5e29b762022-04-04 08:58:34 +02001833 * connect, we may get there because we got woken up, but only run
1834 * after process_stream() noticed there were an error, and decided
1835 * to retry to connect, the connection may still have CO_FL_ERROR,
Willy Tarreaub605c422022-05-17 17:04:55 +02001836 * and we don't want to add SE_FL_ERROR back
Christopher Faulet5e29b762022-04-04 08:58:34 +02001837 *
Willy Tarreau462b9892022-05-18 18:06:53 +02001838 * Note: This test is only required because sc_conn_process is also the SI
1839 * wake callback. Otherwise sc_conn_recv()/sc_conn_send() already take
Christopher Faulet5e29b762022-04-04 08:58:34 +02001840 * care of it.
1841 */
1842
Willy Tarreau0adb2812022-05-27 10:02:48 +02001843 if (sc->state >= SC_ST_CON) {
1844 if (sc_is_conn_error(sc))
1845 sc_ep_set(sc, SE_FL_ERROR);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001846 }
1847
1848 /* If we had early data, and the handshake ended, then
1849 * we can remove the flag, and attempt to wake the task up,
1850 * in the event there's an analyser waiting for the end of
1851 * the handshake.
1852 */
1853 if (!(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS)) &&
Willy Tarreau0adb2812022-05-27 10:02:48 +02001854 sc_ep_test(sc, SE_FL_WAIT_FOR_HS)) {
1855 sc_ep_clr(sc, SE_FL_WAIT_FOR_HS);
1856 task_wakeup(sc_strm_task(sc), TASK_WOKEN_MSG);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001857 }
1858
Willy Tarreau0adb2812022-05-27 10:02:48 +02001859 if (!sc_state_in(sc->state, SC_SB_EST|SC_SB_DIS|SC_SB_CLO) &&
Christopher Faulet5e29b762022-04-04 08:58:34 +02001860 (conn->flags & CO_FL_WAIT_XPRT) == 0) {
Christopher Fauletca679922022-07-20 13:24:04 +02001861 if (sc->flags & SC_FL_ISBACK)
1862 __sc_strm(sc)->conn_exp = TICK_ETERNITY;
Christopher Fauletb96f2aa2022-12-12 08:11:36 +01001863 oc->flags |= CF_WRITE_EVENT;
Willy Tarreau0adb2812022-05-27 10:02:48 +02001864 if (sc->state == SC_ST_CON)
1865 sc->state = SC_ST_RDY;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001866 }
1867
1868 /* Report EOS on the channel if it was reached from the mux point of
1869 * view.
1870 *
Willy Tarreau462b9892022-05-18 18:06:53 +02001871 * Note: This test is only required because sc_conn_process is also the SI
1872 * wake callback. Otherwise sc_conn_recv()/sc_conn_send() already take
Christopher Faulet5e29b762022-04-04 08:58:34 +02001873 * care of it.
1874 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001875 if (sc_ep_test(sc, SE_FL_EOS) && !(ic->flags & CF_SHUTR)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001876 /* we received a shutdown */
Christopher Faulet6e1bbc42022-12-12 08:08:15 +01001877 ic->flags |= CF_READ_EVENT;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001878 if (ic->flags & CF_AUTO_CLOSE)
1879 channel_shutw_now(ic);
Willy Tarreau0adb2812022-05-27 10:02:48 +02001880 sc_conn_read0(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001881 }
1882
1883 /* Report EOI on the channel if it was reached from the mux point of
1884 * view.
1885 *
Willy Tarreau462b9892022-05-18 18:06:53 +02001886 * Note: This test is only required because sc_conn_process is also the SI
1887 * wake callback. Otherwise sc_conn_recv()/sc_conn_send() already take
Christopher Faulet5e29b762022-04-04 08:58:34 +02001888 * care of it.
1889 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001890 if (sc_ep_test(sc, SE_FL_EOI) && !(ic->flags & CF_EOI))
Christopher Faulet285f7612022-12-12 08:28:55 +01001891 ic->flags |= (CF_EOI|CF_READ_EVENT);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001892
Willy Tarreau4596fe22022-05-17 19:07:51 +02001893 /* Second step : update the stream connector and channels, try to forward any
Christopher Faulet5e29b762022-04-04 08:58:34 +02001894 * pending data, then possibly wake the stream up based on the new
Willy Tarreau4596fe22022-05-17 19:07:51 +02001895 * stream connector status.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001896 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001897 sc_notify(sc);
1898 stream_release_buffers(__sc_strm(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001899 return 0;
1900}
1901
Willy Tarreau4596fe22022-05-17 19:07:51 +02001902/* This is the ->process() function for any stream connector's wait_event task.
1903 * It's assigned during the stream connector's initialization, for any type of
1904 * stream connector. Thus it is always safe to perform a tasklet_wakeup() on a
Willy Tarreaue68bc612022-05-27 11:23:05 +02001905 * stream connector, as the presence of the SC is checked there.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001906 */
Willy Tarreau462b9892022-05-18 18:06:53 +02001907struct task *sc_conn_io_cb(struct task *t, void *ctx, unsigned int state)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001908{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001909 struct stconn *sc = ctx;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001910 int ret = 0;
1911
Willy Tarreau0adb2812022-05-27 10:02:48 +02001912 if (!sc_conn(sc))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001913 return t;
1914
Willy Tarreau0adb2812022-05-27 10:02:48 +02001915 if (!(sc->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(sc_oc(sc)))
1916 ret = sc_conn_send(sc);
1917 if (!(sc->wait_event.events & SUB_RETRY_RECV))
1918 ret |= sc_conn_recv(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001919 if (ret != 0)
Willy Tarreau0adb2812022-05-27 10:02:48 +02001920 sc_conn_process(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001921
Willy Tarreau0adb2812022-05-27 10:02:48 +02001922 stream_release_buffers(__sc_strm(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001923 return t;
1924}
1925
1926/* Callback to be used by applet handlers upon completion. It updates the stream
1927 * (which may or may not take this opportunity to try to forward data), then
Willy Tarreau4596fe22022-05-17 19:07:51 +02001928 * may re-enable the applet's based on the channels and stream connector's final
Christopher Faulet5e29b762022-04-04 08:58:34 +02001929 * states.
1930 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001931static int sc_applet_process(struct stconn *sc)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001932{
Willy Tarreau0adb2812022-05-27 10:02:48 +02001933 struct channel *ic = sc_ic(sc);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001934
Willy Tarreau0adb2812022-05-27 10:02:48 +02001935 BUG_ON(!sc_appctx(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001936
1937 /* If the applet wants to write and the channel is closed, it's a
1938 * broken pipe and it must be reported.
1939 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001940 if (!sc_ep_test(sc, SE_FL_HAVE_NO_DATA) && (ic->flags & CF_SHUTR))
1941 sc_ep_set(sc, SE_FL_ERROR);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001942
1943 /* automatically mark the applet having data available if it reported
1944 * begin blocked by the channel.
1945 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001946 if ((sc->flags & (SC_FL_WONT_READ|SC_FL_NEED_BUFF|SC_FL_NEED_ROOM)) ||
1947 sc_ep_test(sc, SE_FL_APPLET_NEED_CONN))
1948 applet_have_more_data(__sc_appctx(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001949
Willy Tarreau4596fe22022-05-17 19:07:51 +02001950 /* update the stream connector, channels, and possibly wake the stream up */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001951 sc_notify(sc);
1952 stream_release_buffers(__sc_strm(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001953
Willy Tarreau19c65a92022-05-27 08:49:24 +02001954 /* sc_notify may have passed through chk_snd and released some blocking
Willy Tarreau15252cd2022-05-25 16:36:21 +02001955 * flags. Process_stream will consider those flags to wake up the
Christopher Faulet5e29b762022-04-04 08:58:34 +02001956 * appctx but in the case the task is not in runqueue we may have to
1957 * wakeup the appctx immediately.
1958 */
Willy Tarreau0adb2812022-05-27 10:02:48 +02001959 if (sc_is_recv_allowed(sc) || sc_is_send_allowed(sc))
1960 appctx_wakeup(__sc_appctx(sc));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001961 return 0;
Christopher Faulet13045f02022-04-01 14:23:38 +02001962}
Christopher Fauletb68f77d2022-06-16 16:24:16 +02001963
1964
1965/* Prepares an endpoint upgrade. We don't now at this stage if the upgrade will
1966 * succeed or not and if the stconn will be reused by the new endpoint. Thus,
1967 * for now, only pretend the stconn is detached.
1968 */
1969void sc_conn_prepare_endp_upgrade(struct stconn *sc)
1970{
1971 BUG_ON(!sc_conn(sc) || !sc->app);
1972 sc_ep_clr(sc, SE_FL_T_MUX);
1973 sc_ep_set(sc, SE_FL_DETACHED);
1974}
1975
Ilya Shipitsin3b64a282022-07-29 22:26:53 +05001976/* Endpoint upgrade failed. Restore the stconn state. */
Christopher Fauletb68f77d2022-06-16 16:24:16 +02001977void sc_conn_abort_endp_upgrade(struct stconn *sc)
1978{
1979 sc_ep_set(sc, SE_FL_T_MUX);
1980 sc_ep_clr(sc, SE_FL_DETACHED);
1981}
1982
1983/* Commit the endpoint upgrade. If stconn is attached, it means the new endpoint
1984 * use it. So we do nothing. Otherwise, the stconn will be destroy with the
1985 * overlying stream. So, it means we must commit the detach.
1986*/
1987void sc_conn_commit_endp_upgrade(struct stconn *sc)
1988{
1989 if (!sc_ep_test(sc, SE_FL_DETACHED))
1990 return;
1991 sc_detach_endp(&sc);
1992 /* Because it was already set as detached, the sedesc must be preserved */
Willy Tarreau6a378d12022-08-11 13:56:42 +02001993 BUG_ON(!sc);
Christopher Fauletb68f77d2022-06-16 16:24:16 +02001994 BUG_ON(!sc->sedesc);
1995}