blob: caf33e4386ca2cac70c01d8a000b866a1d7bb349 [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>
16#include <haproxy/conn_stream.h>
Christopher Faulet19bd7282022-04-01 13:58:09 +020017#include <haproxy/cs_utils.h>
Christopher Faulet5e29b762022-04-04 08:58:34 +020018#include <haproxy/check.h>
19#include <haproxy/http_ana.h>
20#include <haproxy/pipe.h>
Christopher Faulet1329f2a2021-12-16 17:32:56 +010021#include <haproxy/pool.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 Tarreau4596fe22022-05-17 19:07:51 +020027static void sc_app_shutr(struct stconn *cs);
28static void sc_app_shutw(struct stconn *cs);
29static void sc_app_chk_rcv(struct stconn *cs);
30static void sc_app_chk_snd(struct stconn *cs);
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 Tarreau4596fe22022-05-17 19:07:51 +020033static void sc_app_shutr_conn(struct stconn *cs);
34static void sc_app_shutw_conn(struct stconn *cs);
35static void sc_app_chk_rcv_conn(struct stconn *cs);
36static void sc_app_chk_snd_conn(struct stconn *cs);
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 Tarreau4596fe22022-05-17 19:07:51 +020039static void sc_app_shutr_applet(struct stconn *cs);
40static void sc_app_shutw_applet(struct stconn *cs);
41static void sc_app_chk_rcv_applet(struct stconn *cs);
42static void sc_app_chk_snd_applet(struct stconn *cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +020043
Willy Tarreau462b9892022-05-18 18:06:53 +020044static int sc_conn_process(struct stconn *cs);
45static int sc_conn_recv(struct stconn *cs);
46static int sc_conn_send(struct stconn *cs);
Willy Tarreau2f2318d2022-05-18 10:17:16 +020047static int cs_applet_process(struct stconn *cs);
48
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,
Christopher Faulet5e29b762022-04-04 08:58:34 +020075 .wake = cs_applet_process,
76 .name = "STRM",
77};
78
Willy Tarreau2f2318d2022-05-18 10:17:16 +020079/* stream connector for health checks on connections */
80struct sc_app_ops sc_app_check_ops = {
81 .chk_rcv = NULL,
82 .chk_snd = NULL,
83 .shutr = NULL,
84 .shutw = NULL,
85 .wake = wake_srv_chk,
86 .name = "CHCK",
87};
Christopher Faulet5e29b762022-04-04 08:58:34 +020088
Christopher Faulet9ed77422022-04-12 08:51:15 +020089/* Initializes an endpoint */
Willy Tarreauea59b022022-05-17 17:53:22 +020090void sedesc_init(struct sedesc *sedesc)
Christopher Fauletdb90f2a2022-03-22 16:06:25 +010091{
Willy Tarreauea59b022022-05-17 17:53:22 +020092 sedesc->se = NULL;
93 sedesc->conn = NULL;
Willy Tarreauc1054922022-05-18 07:43:52 +020094 sedesc->sc = NULL;
Willy Tarreauea59b022022-05-17 17:53:22 +020095 se_fl_setall(sedesc, SE_FL_NONE);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +010096}
97
Christopher Faulet9ed77422022-04-12 08:51:15 +020098/* Tries to alloc an endpoint and initialize it. Returns NULL on failure. */
Willy Tarreauea59b022022-05-17 17:53:22 +020099struct sedesc *sedesc_new()
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100100{
Willy Tarreauea59b022022-05-17 17:53:22 +0200101 struct sedesc *sedesc;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100102
Willy Tarreauea59b022022-05-17 17:53:22 +0200103 sedesc = pool_alloc(pool_head_sedesc);
104 if (unlikely(!sedesc))
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100105 return NULL;
106
Willy Tarreauea59b022022-05-17 17:53:22 +0200107 sedesc_init(sedesc);
108 return sedesc;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100109}
110
Christopher Faulet9ed77422022-04-12 08:51:15 +0200111/* Releases an endpoint. It is the caller responsibility to be sure it is safe
112 * and it is not shared with another entity
113 */
Willy Tarreauea59b022022-05-17 17:53:22 +0200114void sedesc_free(struct sedesc *sedesc)
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100115{
Willy Tarreauea59b022022-05-17 17:53:22 +0200116 pool_free(pool_head_sedesc, sedesc);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100117}
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100118
Willy Tarreau4596fe22022-05-17 19:07:51 +0200119/* Tries to allocate a new stconn and initialize its main fields. On
Christopher Faulet9ed77422022-04-12 08:51:15 +0200120 * failure, nothing is allocated and NULL is returned. It is an internal
Willy Tarreaub605c422022-05-17 17:04:55 +0200121 * function. The caller must, at least, set the SE_FL_ORPHAN or SE_FL_DETACHED
Christopher Faulet9ed77422022-04-12 08:51:15 +0200122 * flag.
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100123 */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200124static struct stconn *cs_new(struct sedesc *sedesc)
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100125{
Willy Tarreau4596fe22022-05-17 19:07:51 +0200126 struct stconn *cs;
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100127
128 cs = pool_alloc(pool_head_connstream);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100129
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100130 if (unlikely(!cs))
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100131 goto alloc_error;
Christopher Fauletbb772d02022-03-22 15:28:36 +0100132
133 cs->obj_type = OBJ_TYPE_CS;
Willy Tarreaucb041662022-05-17 19:44:42 +0200134 cs->flags = SC_FL_NONE;
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200135 cs->state = SC_ST_INI;
Christopher Faulet1d987772022-03-29 18:03:35 +0200136 cs->hcto = TICK_ETERNITY;
Christopher Fauletbb772d02022-03-22 15:28:36 +0100137 cs->app = NULL;
Willy Tarreau2f2318d2022-05-18 10:17:16 +0200138 cs->app_ops = NULL;
Christopher Faulet8da67aa2022-03-29 17:53:09 +0200139 cs->src = NULL;
140 cs->dst = NULL;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200141 cs->wait_event.tasklet = NULL;
142 cs->wait_event.events = 0;
143
Christopher Faulet9ed77422022-04-12 08:51:15 +0200144 /* If there is no endpoint, allocate a new one now */
Willy Tarreauea59b022022-05-17 17:53:22 +0200145 if (!sedesc) {
146 sedesc = sedesc_new();
147 if (unlikely(!sedesc))
Christopher Fauletb669d682022-03-22 18:37:19 +0100148 goto alloc_error;
149 }
Willy Tarreau798465b2022-05-17 18:20:02 +0200150 cs->sedesc = sedesc;
Willy Tarreauc1054922022-05-18 07:43:52 +0200151 sedesc->sc = cs;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100152
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100153 return cs;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100154
155 alloc_error:
156 pool_free(pool_head_connstream, cs);
157 return NULL;
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100158}
159
Willy Tarreau4596fe22022-05-17 19:07:51 +0200160/* Creates a new stream connector and its associated stream from a mux. <endp> must be
161 * defined. It returns NULL on error. On success, the new stream connector is
Willy Tarreaub605c422022-05-17 17:04:55 +0200162 * returned. In this case, SE_FL_ORPHAN flag is removed.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200163 */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200164struct stconn *cs_new_from_endp(struct sedesc *sedesc, struct session *sess, struct buffer *input)
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100165{
Willy Tarreau4596fe22022-05-17 19:07:51 +0200166 struct stconn *cs;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100167
Willy Tarreauea59b022022-05-17 17:53:22 +0200168 cs = cs_new(sedesc);
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100169 if (unlikely(!cs))
170 return NULL;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100171 if (unlikely(!stream_new(sess, cs, input))) {
172 pool_free(pool_head_connstream, cs);
173 cs = NULL;
174 }
Willy Tarreauea59b022022-05-17 17:53:22 +0200175 se_fl_clr(sedesc, SE_FL_ORPHAN);
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100176 return cs;
177}
178
Willy Tarreau4596fe22022-05-17 19:07:51 +0200179/* Creates a new stream connector from an stream. There is no endpoint here, thus it
Willy Tarreaub605c422022-05-17 17:04:55 +0200180 * will be created by cs_new(). So the SE_FL_DETACHED flag is set. It returns
Willy Tarreau4596fe22022-05-17 19:07:51 +0200181 * NULL on error. On success, the new stream connector is returned.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200182 */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200183struct stconn *cs_new_from_strm(struct stream *strm, unsigned int flags)
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100184{
Willy Tarreau4596fe22022-05-17 19:07:51 +0200185 struct stconn *cs;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100186
187 cs = cs_new(NULL);
188 if (unlikely(!cs))
189 return NULL;
190 cs->flags |= flags;
Willy Tarreaub605c422022-05-17 17:04:55 +0200191 sc_ep_set(cs, SE_FL_DETACHED);
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100192 cs->app = &strm->obj_type;
Willy Tarreau2f2318d2022-05-18 10:17:16 +0200193 cs->app_ops = &sc_app_embedded_ops;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100194 return cs;
195}
196
Willy Tarreau4596fe22022-05-17 19:07:51 +0200197/* Creates a new stream connector from an health-check. There is no endpoint here,
Willy Tarreaub605c422022-05-17 17:04:55 +0200198 * thus it will be created by cs_new(). So the SE_FL_DETACHED flag is set. It
Willy Tarreau4596fe22022-05-17 19:07:51 +0200199 * returns NULL on error. On success, the new stream connector is returned.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200200 */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200201struct stconn *cs_new_from_check(struct check *check, unsigned int flags)
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100202{
Willy Tarreau4596fe22022-05-17 19:07:51 +0200203 struct stconn *cs;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100204
205 cs = cs_new(NULL);
206 if (unlikely(!cs))
207 return NULL;
208 cs->flags |= flags;
Willy Tarreaub605c422022-05-17 17:04:55 +0200209 sc_ep_set(cs, SE_FL_DETACHED);
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100210 cs->app = &check->obj_type;
Willy Tarreau2f2318d2022-05-18 10:17:16 +0200211 cs->app_ops = &sc_app_check_ops;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100212 return cs;
213}
214
Willy Tarreau4596fe22022-05-17 19:07:51 +0200215/* Releases a stconn previously allocated by cs_new(), as well as its
Christopher Faulet9ed77422022-04-12 08:51:15 +0200216 * endpoint, if it exists. This function is called internally or on error path.
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100217 */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200218void cs_free(struct stconn *cs)
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100219{
Christopher Faulet8da67aa2022-03-29 17:53:09 +0200220 sockaddr_free(&cs->src);
221 sockaddr_free(&cs->dst);
Willy Tarreau798465b2022-05-17 18:20:02 +0200222 if (cs->sedesc) {
Willy Tarreaub605c422022-05-17 17:04:55 +0200223 BUG_ON(!sc_ep_test(cs, SE_FL_DETACHED));
Willy Tarreau798465b2022-05-17 18:20:02 +0200224 sedesc_free(cs->sedesc);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100225 }
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200226 if (cs->wait_event.tasklet)
227 tasklet_free(cs->wait_event.tasklet);
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100228 pool_free(pool_head_connstream, cs);
229}
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100230
Willy Tarreau4596fe22022-05-17 19:07:51 +0200231/* Conditionally removes a stream connector if it is detached and if there is no app
Christopher Fauleteb50c012022-04-21 14:22:53 +0200232 * layer defined. Except on error path, this one must be used. if release, the
233 * pointer on the CS is set to NULL.
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200234 */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200235static void cs_free_cond(struct stconn **csp)
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200236{
Willy Tarreau4596fe22022-05-17 19:07:51 +0200237 struct stconn *cs = *csp;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200238
Willy Tarreau798465b2022-05-17 18:20:02 +0200239 if (!cs->app && (!cs->sedesc || sc_ep_test(cs, SE_FL_DETACHED))) {
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200240 cs_free(cs);
Christopher Fauleteb50c012022-04-21 14:22:53 +0200241 *csp = NULL;
242 }
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200243}
244
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100245
Willy Tarreau4596fe22022-05-17 19:07:51 +0200246/* Attaches a stconn to a mux endpoint and sets the endpoint ctx. Returns
Willy Tarreaub605c422022-05-17 17:04:55 +0200247 * -1 on error and 0 on sucess. SE_FL_DETACHED flag is removed. This function is
Christopher Faulet9ed77422022-04-12 08:51:15 +0200248 * called from a mux when it is attached to a stream or a health-check.
249 */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200250int cs_attach_mux(struct stconn *cs, void *endp, void *ctx)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100251{
Christopher Faulet93882042022-01-19 14:56:50 +0100252 struct connection *conn = ctx;
Willy Tarreau798465b2022-05-17 18:20:02 +0200253 struct sedesc *sedesc = cs->sedesc;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100254
Willy Tarreau798465b2022-05-17 18:20:02 +0200255 sedesc->se = endp;
256 sedesc->conn = ctx;
257 se_fl_set(sedesc, SE_FL_T_MUX);
258 se_fl_clr(sedesc, SE_FL_DETACHED);
Christopher Faulet93882042022-01-19 14:56:50 +0100259 if (!conn->ctx)
260 conn->ctx = cs;
Willy Tarreauea27f482022-05-18 16:10:52 +0200261 if (sc_strm(cs)) {
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200262 if (!cs->wait_event.tasklet) {
263 cs->wait_event.tasklet = tasklet_new();
264 if (!cs->wait_event.tasklet)
265 return -1;
Willy Tarreau462b9892022-05-18 18:06:53 +0200266 cs->wait_event.tasklet->process = sc_conn_io_cb;
Christopher Faulet4a7764a2022-04-01 16:58:52 +0200267 cs->wait_event.tasklet->context = cs;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200268 cs->wait_event.events = 0;
269 }
270
Willy Tarreau2f2318d2022-05-18 10:17:16 +0200271 cs->app_ops = &sc_app_conn_ops;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100272 }
Willy Tarreauea27f482022-05-18 16:10:52 +0200273 else if (sc_check(cs)) {
Christopher Fauletc95eaef2022-05-18 15:57:15 +0200274 if (!cs->wait_event.tasklet) {
275 cs->wait_event.tasklet = tasklet_new();
276 if (!cs->wait_event.tasklet)
277 return -1;
278 cs->wait_event.tasklet->process = srv_chk_io_cb;
279 cs->wait_event.tasklet->context = cs;
280 cs->wait_event.events = 0;
281 }
282
Willy Tarreau2f2318d2022-05-18 10:17:16 +0200283 cs->app_ops = &sc_app_check_ops;
Christopher Fauletc95eaef2022-05-18 15:57:15 +0200284 }
Christopher Faulet070b91b2022-03-31 19:27:18 +0200285 return 0;
Christopher Faulet93882042022-01-19 14:56:50 +0100286}
287
Willy Tarreau4596fe22022-05-17 19:07:51 +0200288/* Attaches a stconn to an applet endpoint and sets the endpoint
Willy Tarreaub605c422022-05-17 17:04:55 +0200289 * ctx. Returns -1 on error and 0 on sucess. SE_FL_DETACHED flag is
Christopher Faulet9ed77422022-04-12 08:51:15 +0200290 * removed. This function is called by a stream when a backend applet is
291 * registered.
292 */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200293static void cs_attach_applet(struct stconn *cs, void *endp)
Christopher Faulet93882042022-01-19 14:56:50 +0100294{
Willy Tarreau798465b2022-05-17 18:20:02 +0200295 cs->sedesc->se = endp;
Willy Tarreaub605c422022-05-17 17:04:55 +0200296 sc_ep_set(cs, SE_FL_T_APPLET);
297 sc_ep_clr(cs, SE_FL_DETACHED);
Willy Tarreauea27f482022-05-18 16:10:52 +0200298 if (sc_strm(cs))
Willy Tarreau2f2318d2022-05-18 10:17:16 +0200299 cs->app_ops = &sc_app_applet_ops;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100300}
301
Willy Tarreau4596fe22022-05-17 19:07:51 +0200302/* Attaches a stconn to a app layer and sets the relevant
Willy Tarreaub605c422022-05-17 17:04:55 +0200303 * callbacks. Returns -1 on error and 0 on success. SE_FL_ORPHAN flag is
Christopher Faulet9ed77422022-04-12 08:51:15 +0200304 * removed. This function is called by a stream when it is created to attach it
Willy Tarreau4596fe22022-05-17 19:07:51 +0200305 * on the stream connector on the client side.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200306 */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200307int cs_attach_strm(struct stconn *cs, struct stream *strm)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100308{
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100309 cs->app = &strm->obj_type;
Willy Tarreaub605c422022-05-17 17:04:55 +0200310 sc_ep_clr(cs, SE_FL_ORPHAN);
311 if (sc_ep_test(cs, SE_FL_T_MUX)) {
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200312 cs->wait_event.tasklet = tasklet_new();
Christopher Faulet582a2262022-04-04 11:25:59 +0200313 if (!cs->wait_event.tasklet)
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200314 return -1;
Willy Tarreau462b9892022-05-18 18:06:53 +0200315 cs->wait_event.tasklet->process = sc_conn_io_cb;
Christopher Faulet4a7764a2022-04-01 16:58:52 +0200316 cs->wait_event.tasklet->context = cs;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200317 cs->wait_event.events = 0;
318
Willy Tarreau2f2318d2022-05-18 10:17:16 +0200319 cs->app_ops = &sc_app_conn_ops;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100320 }
Willy Tarreaub605c422022-05-17 17:04:55 +0200321 else if (sc_ep_test(cs, SE_FL_T_APPLET)) {
Willy Tarreau2f2318d2022-05-18 10:17:16 +0200322 cs->app_ops = &sc_app_applet_ops;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100323 }
324 else {
Willy Tarreau2f2318d2022-05-18 10:17:16 +0200325 cs->app_ops = &sc_app_embedded_ops;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100326 }
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100327 return 0;
328}
329
Willy Tarreau4596fe22022-05-17 19:07:51 +0200330/* Detaches the stconn from the endpoint, if any. For a connecrion, if a
Christopher Faulet9ed77422022-04-12 08:51:15 +0200331 * mux owns the connection ->detach() callback is called. Otherwise, it means
Willy Tarreau4596fe22022-05-17 19:07:51 +0200332 * the stream connector owns the connection. In this case the connection is closed
Christopher Faulet9ed77422022-04-12 08:51:15 +0200333 * and released. For an applet, the appctx is released. If still allocated, the
334 * endpoint is reset and flag as detached. If the app layer is also detached,
Willy Tarreau4596fe22022-05-17 19:07:51 +0200335 * the stream connector is released.
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100336 */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200337static void cs_detach_endp(struct stconn **csp)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100338{
Willy Tarreau4596fe22022-05-17 19:07:51 +0200339 struct stconn *cs = *csp;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200340
341 if (!cs)
342 return;
343
Willy Tarreau798465b2022-05-17 18:20:02 +0200344 if (!cs->sedesc)
Christopher Fauletb041b232022-03-24 10:27:02 +0100345 goto reset_cs;
346
Willy Tarreaub605c422022-05-17 17:04:55 +0200347 if (sc_ep_test(cs, SE_FL_T_MUX)) {
Willy Tarreaufd9417b2022-05-18 16:23:22 +0200348 struct connection *conn = __sc_conn(cs);
Willy Tarreau798465b2022-05-17 18:20:02 +0200349 struct sedesc *sedesc = cs->sedesc;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100350
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100351 if (conn->mux) {
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200352 if (cs->wait_event.events != 0)
353 conn->mux->unsubscribe(cs, cs->wait_event.events, &cs->wait_event);
Willy Tarreau798465b2022-05-17 18:20:02 +0200354 se_fl_set(sedesc, SE_FL_ORPHAN);
Willy Tarreauc1054922022-05-18 07:43:52 +0200355 sedesc->sc = NULL;
Willy Tarreau798465b2022-05-17 18:20:02 +0200356 cs->sedesc = NULL;
357 conn->mux->detach(sedesc);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100358 }
359 else {
360 /* It's too early to have a mux, let's just destroy
361 * the connection
362 */
363 conn_stop_tracking(conn);
364 conn_full_close(conn);
365 if (conn->destroy_cb)
366 conn->destroy_cb(conn);
367 conn_free(conn);
368 }
369 }
Willy Tarreaub605c422022-05-17 17:04:55 +0200370 else if (sc_ep_test(cs, SE_FL_T_APPLET)) {
Willy Tarreau8e7c6e62022-05-18 17:58:02 +0200371 struct appctx *appctx = __sc_appctx(cs);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100372
Willy Tarreaub605c422022-05-17 17:04:55 +0200373 sc_ep_set(cs, SE_FL_ORPHAN);
Willy Tarreauc1054922022-05-18 07:43:52 +0200374 cs->sedesc->sc = NULL;
Willy Tarreau798465b2022-05-17 18:20:02 +0200375 cs->sedesc = NULL;
Willy Tarreau1c3ead42022-05-10 19:42:22 +0200376 appctx_shut(appctx);
377 appctx_free(appctx);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100378 }
379
Willy Tarreau798465b2022-05-17 18:20:02 +0200380 if (cs->sedesc) {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100381 /* the cs is the only one one the endpoint */
Willy Tarreau798465b2022-05-17 18:20:02 +0200382 cs->sedesc->se = NULL;
383 cs->sedesc->conn = NULL;
Willy Tarreaub605c422022-05-17 17:04:55 +0200384 sc_ep_clr(cs, ~SE_FL_APP_MASK);
385 sc_ep_set(cs, SE_FL_DETACHED);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100386 }
387
Christopher Fauletb041b232022-03-24 10:27:02 +0100388 reset_cs:
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100389 /* FIXME: Rest CS for now but must be reviewed. CS flags are only
390 * connection related for now but this will evolved
391 */
Willy Tarreaucb041662022-05-17 19:44:42 +0200392 cs->flags &= SC_FL_ISBACK;
Willy Tarreauea27f482022-05-18 16:10:52 +0200393 if (sc_strm(cs))
Willy Tarreau2f2318d2022-05-18 10:17:16 +0200394 cs->app_ops = &sc_app_embedded_ops;
395 else
396 cs->app_ops = NULL;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200397 cs_free_cond(csp);
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100398}
399
Willy Tarreau4596fe22022-05-17 19:07:51 +0200400/* Detaches the stconn from the app layer. If there is no endpoint attached
401 * to the stconn
Christopher Faulet9ed77422022-04-12 08:51:15 +0200402 */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200403static void cs_detach_app(struct stconn **csp)
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100404{
Willy Tarreau4596fe22022-05-17 19:07:51 +0200405 struct stconn *cs = *csp;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200406
407 if (!cs)
408 return;
409
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100410 cs->app = NULL;
Willy Tarreau2f2318d2022-05-18 10:17:16 +0200411 cs->app_ops = NULL;
Christopher Faulet8da67aa2022-03-29 17:53:09 +0200412 sockaddr_free(&cs->src);
413 sockaddr_free(&cs->dst);
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200414
415 if (cs->wait_event.tasklet)
416 tasklet_free(cs->wait_event.tasklet);
417 cs->wait_event.tasklet = NULL;
418 cs->wait_event.events = 0;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200419 cs_free_cond(csp);
420}
421
Willy Tarreau4596fe22022-05-17 19:07:51 +0200422/* Destroy the stconn. It is detached from its endpoint and its
423 * application. After this call, the stconn must be considered as released.
Christopher Fauleteb50c012022-04-21 14:22:53 +0200424 */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200425void cs_destroy(struct stconn *cs)
Christopher Fauleteb50c012022-04-21 14:22:53 +0200426{
427 cs_detach_endp(&cs);
428 cs_detach_app(&cs);
429 BUG_ON_HOT(cs);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100430}
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100431
Willy Tarreau4596fe22022-05-17 19:07:51 +0200432/* Resets the stream connector endpoint. It happens when the app layer want to renew
Christopher Faulet9ed77422022-04-12 08:51:15 +0200433 * its endpoint. For a connection retry for instance. If a mux or an applet is
434 * attached, a new endpoint is created. Returns -1 on error and 0 on sucess.
Christopher Fauleta6c4a482022-04-28 18:25:24 +0200435 *
Willy Tarreaub605c422022-05-17 17:04:55 +0200436 * Only SE_FL_ERROR flag is removed on the endpoint. Orther flags are preserved.
Christopher Fauleta6c4a482022-04-28 18:25:24 +0200437 * It is the caller responsibility to remove other flags if needed.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200438 */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200439int cs_reset_endp(struct stconn *cs)
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100440{
Willy Tarreauea59b022022-05-17 17:53:22 +0200441 struct sedesc *new_endp;
Christopher Fauletb041b232022-03-24 10:27:02 +0100442
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100443 BUG_ON(!cs->app);
Christopher Fauleta6c4a482022-04-28 18:25:24 +0200444
Willy Tarreaub605c422022-05-17 17:04:55 +0200445 sc_ep_clr(cs, SE_FL_ERROR);
Willy Tarreaufa57cc72022-05-18 17:56:13 +0200446 if (!__sc_endp(cs)) {
Christopher Fauletb041b232022-03-24 10:27:02 +0100447 /* endpoint not attached or attached to a mux with no
448 * target. Thus the endpoint will not be release but just
Christopher Fauleteb50c012022-04-21 14:22:53 +0200449 * reset. The app is still attached, the cs will not be
450 * released.
Christopher Fauletb041b232022-03-24 10:27:02 +0100451 */
Christopher Fauleteb50c012022-04-21 14:22:53 +0200452 cs_detach_endp(&cs);
Christopher Fauletb041b232022-03-24 10:27:02 +0100453 return 0;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100454 }
Christopher Fauletb041b232022-03-24 10:27:02 +0100455
456 /* allocate the new endpoint first to be able to set error if it
457 * fails */
Willy Tarreauea59b022022-05-17 17:53:22 +0200458 new_endp = sedesc_new();
Christopher Fauletb041b232022-03-24 10:27:02 +0100459 if (!unlikely(new_endp)) {
Willy Tarreaub605c422022-05-17 17:04:55 +0200460 sc_ep_set(cs, SE_FL_ERROR);
Christopher Fauletb041b232022-03-24 10:27:02 +0100461 return -1;
462 }
Willy Tarreaub605c422022-05-17 17:04:55 +0200463 se_fl_setall(new_endp, sc_ep_get(cs) & SE_FL_APP_MASK);
Christopher Fauletb041b232022-03-24 10:27:02 +0100464
Christopher Fauleteb50c012022-04-21 14:22:53 +0200465 /* The app is still attached, the cs will not be released */
466 cs_detach_endp(&cs);
Willy Tarreau798465b2022-05-17 18:20:02 +0200467 BUG_ON(cs->sedesc);
468 cs->sedesc = new_endp;
Willy Tarreauc1054922022-05-18 07:43:52 +0200469 cs->sedesc->sc = cs;
Willy Tarreaub605c422022-05-17 17:04:55 +0200470 sc_ep_set(cs, SE_FL_DETACHED);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100471 return 0;
472}
Christopher Faulet37046632022-04-01 11:36:58 +0200473
474
Willy Tarreau4596fe22022-05-17 19:07:51 +0200475/* Create an applet to handle a stream connector as a new appctx. The CS will
Christopher Faulet37046632022-04-01 11:36:58 +0200476 * wake it up every time it is solicited. The appctx must be deleted by the task
477 * handler using cs_detach_endp(), possibly from within the function itself.
478 * It also pre-initializes the applet's context and returns it (or NULL in case
479 * it could not be allocated).
480 */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200481struct appctx *cs_applet_create(struct stconn *cs, struct applet *app)
Christopher Faulet37046632022-04-01 11:36:58 +0200482{
483 struct appctx *appctx;
484
Willy Tarreauea27f482022-05-18 16:10:52 +0200485 DPRINTF(stderr, "registering handler %p for cs %p (was %p)\n", app, cs, sc_strm_task(cs));
Christopher Faulet37046632022-04-01 11:36:58 +0200486
Willy Tarreau798465b2022-05-17 18:20:02 +0200487 appctx = appctx_new_here(app, cs->sedesc);
Christopher Faulet37046632022-04-01 11:36:58 +0200488 if (!appctx)
489 return NULL;
Christopher Faulet2d9cc852022-05-16 17:29:37 +0200490 cs_attach_applet(cs, appctx);
Willy Tarreauea27f482022-05-18 16:10:52 +0200491 appctx->t->nice = __sc_strm(cs)->task->nice;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200492 cs_cant_get(cs);
Christopher Faulet37046632022-04-01 11:36:58 +0200493 appctx_wakeup(appctx);
Christopher Fauleta33ff7a2022-04-21 11:52:07 +0200494
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200495 cs->state = SC_ST_RDY;
Christopher Faulet37046632022-04-01 11:36:58 +0200496 return appctx;
497}
498
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200499/*
Willy Tarreau4596fe22022-05-17 19:07:51 +0200500 * This function performs a shutdown-read on a detached stream connector in a
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200501 * connected or init state (it does nothing for other states). It either shuts
502 * the read side or marks itself as closed. The buffer flags are updated to
Willy Tarreaucb041662022-05-17 19:44:42 +0200503 * reflect the new state. If the stream connector has SC_FL_NOHALF, we also
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200504 * forward the close to the write side. The owner task is woken up if it exists.
505 */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200506static void sc_app_shutr(struct stconn *cs)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200507{
Willy Tarreau40a9c322022-05-18 15:55:18 +0200508 struct channel *ic = sc_ic(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200509
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200510 if (ic->flags & CF_SHUTR)
511 return;
512 ic->flags |= CF_SHUTR;
513 ic->rex = TICK_ETERNITY;
514
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200515 if (!cs_state_in(cs->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200516 return;
517
Willy Tarreau40a9c322022-05-18 15:55:18 +0200518 if (sc_oc(cs)->flags & CF_SHUTW) {
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200519 cs->state = SC_ST_DIS;
Willy Tarreauea27f482022-05-18 16:10:52 +0200520 __sc_strm(cs)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200521 }
Willy Tarreaucb041662022-05-17 19:44:42 +0200522 else if (cs->flags & SC_FL_NOHALF) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200523 /* we want to immediately forward this close to the write side */
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200524 return sc_app_shutw(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200525 }
526
527 /* note that if the task exists, it must unregister itself once it runs */
Willy Tarreaucb041662022-05-17 19:44:42 +0200528 if (!(cs->flags & SC_FL_DONT_WAKE))
Willy Tarreauea27f482022-05-18 16:10:52 +0200529 task_wakeup(sc_strm_task(cs), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200530}
531
532/*
Willy Tarreau4596fe22022-05-17 19:07:51 +0200533 * This function performs a shutdown-write on a detached stream connector in a
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200534 * connected or init state (it does nothing for other states). It either shuts
535 * the write side or marks itself as closed. The buffer flags are updated to
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +0200536 * reflect the new state. It does also close everything if the CS was marked as
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200537 * being in error state. The owner task is woken up if it exists.
538 */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200539static void sc_app_shutw(struct stconn *cs)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200540{
Willy Tarreau40a9c322022-05-18 15:55:18 +0200541 struct channel *ic = sc_ic(cs);
542 struct channel *oc = sc_oc(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200543
544 oc->flags &= ~CF_SHUTW_NOW;
545 if (oc->flags & CF_SHUTW)
546 return;
547 oc->flags |= CF_SHUTW;
548 oc->wex = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200549
550 if (tick_isset(cs->hcto)) {
551 ic->rto = cs->hcto;
552 ic->rex = tick_add(now_ms, ic->rto);
553 }
554
555 switch (cs->state) {
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200556 case SC_ST_RDY:
557 case SC_ST_EST:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200558 /* we have to shut before closing, otherwise some short messages
559 * may never leave the system, especially when there are remaining
560 * unread data in the socket input buffer, or when nolinger is set.
Willy Tarreaucb041662022-05-17 19:44:42 +0200561 * However, if SC_FL_NOLINGER is explicitly set, we know there is
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200562 * no risk so we close both sides immediately.
563 */
Willy Tarreaucb041662022-05-17 19:44:42 +0200564 if (!sc_ep_test(cs, SE_FL_ERROR) && !(cs->flags & SC_FL_NOLINGER) &&
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200565 !(ic->flags & (CF_SHUTR|CF_DONT_READ)))
566 return;
567
568 /* fall through */
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200569 case SC_ST_CON:
570 case SC_ST_CER:
571 case SC_ST_QUE:
572 case SC_ST_TAR:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200573 /* Note that none of these states may happen with applets */
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200574 cs->state = SC_ST_DIS;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200575 /* fall through */
576 default:
Willy Tarreaucb041662022-05-17 19:44:42 +0200577 cs->flags &= ~SC_FL_NOLINGER;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200578 ic->flags |= CF_SHUTR;
579 ic->rex = TICK_ETERNITY;
Willy Tarreauea27f482022-05-18 16:10:52 +0200580 __sc_strm(cs)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200581 }
582
583 /* note that if the task exists, it must unregister itself once it runs */
Willy Tarreaucb041662022-05-17 19:44:42 +0200584 if (!(cs->flags & SC_FL_DONT_WAKE))
Willy Tarreauea27f482022-05-18 16:10:52 +0200585 task_wakeup(sc_strm_task(cs), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200586}
587
588/* default chk_rcv function for scheduled tasks */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200589static void sc_app_chk_rcv(struct stconn *cs)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200590{
Willy Tarreau40a9c322022-05-18 15:55:18 +0200591 struct channel *ic = sc_ic(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200592
593 DPRINTF(stderr, "%s: cs=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n",
594 __FUNCTION__,
Willy Tarreau40a9c322022-05-18 15:55:18 +0200595 cs, cs->state, ic->flags, sc_oc(cs)->flags);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200596
597 if (ic->pipe) {
598 /* stop reading */
Willy Tarreau99615ed2022-05-25 07:29:36 +0200599 sc_need_room(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200600 }
601 else {
602 /* (re)start reading */
Willy Tarreaucb041662022-05-17 19:44:42 +0200603 if (!(cs->flags & SC_FL_DONT_WAKE))
Willy Tarreauea27f482022-05-18 16:10:52 +0200604 task_wakeup(sc_strm_task(cs), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200605 }
606}
607
608/* default chk_snd function for scheduled tasks */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200609static void sc_app_chk_snd(struct stconn *cs)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200610{
Willy Tarreau40a9c322022-05-18 15:55:18 +0200611 struct channel *oc = sc_oc(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200612
613 DPRINTF(stderr, "%s: cs=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n",
614 __FUNCTION__,
Willy Tarreau40a9c322022-05-18 15:55:18 +0200615 cs, cs->state, sc_ic(cs)->flags, oc->flags);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200616
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200617 if (unlikely(cs->state != SC_ST_EST || (oc->flags & CF_SHUTW)))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200618 return;
619
Willy Tarreaub605c422022-05-17 17:04:55 +0200620 if (!sc_ep_test(cs, SE_FL_WAIT_DATA) || /* not waiting for data */
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200621 channel_is_empty(oc)) /* called with nothing to send ! */
622 return;
623
624 /* Otherwise there are remaining data to be sent in the buffer,
625 * so we tell the handler.
626 */
Willy Tarreaub605c422022-05-17 17:04:55 +0200627 sc_ep_clr(cs, SE_FL_WAIT_DATA);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200628 if (!tick_isset(oc->wex))
629 oc->wex = tick_add_ifset(now_ms, oc->wto);
630
Willy Tarreaucb041662022-05-17 19:44:42 +0200631 if (!(cs->flags & SC_FL_DONT_WAKE))
Willy Tarreauea27f482022-05-18 16:10:52 +0200632 task_wakeup(sc_strm_task(cs), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200633}
634
635/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200636 * This function performs a shutdown-read on a stream connector attached to
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200637 * a connection in a connected or init state (it does nothing for other
638 * states). It either shuts the read side or marks itself as closed. The buffer
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200639 * flags are updated to reflect the new state. If the stream connector has
Willy Tarreaucb041662022-05-17 19:44:42 +0200640 * SC_FL_NOHALF, we also forward the close to the write side. If a control
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200641 * layer is defined, then it is supposed to be a socket layer and file
642 * descriptors are then shutdown or closed accordingly. The function
643 * automatically disables polling if needed.
644 */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200645static void sc_app_shutr_conn(struct stconn *cs)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200646{
Willy Tarreau40a9c322022-05-18 15:55:18 +0200647 struct channel *ic = sc_ic(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200648
Willy Tarreaufd9417b2022-05-18 16:23:22 +0200649 BUG_ON(!sc_conn(cs));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200650
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200651 if (ic->flags & CF_SHUTR)
652 return;
653 ic->flags |= CF_SHUTR;
654 ic->rex = TICK_ETERNITY;
655
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200656 if (!cs_state_in(cs->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200657 return;
658
Willy Tarreau40a9c322022-05-18 15:55:18 +0200659 if (sc_oc(cs)->flags & CF_SHUTW) {
Willy Tarreau462b9892022-05-18 18:06:53 +0200660 sc_conn_shut(cs);
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200661 cs->state = SC_ST_DIS;
Willy Tarreauea27f482022-05-18 16:10:52 +0200662 __sc_strm(cs)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200663 }
Willy Tarreaucb041662022-05-17 19:44:42 +0200664 else if (cs->flags & SC_FL_NOHALF) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200665 /* we want to immediately forward this close to the write side */
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200666 return sc_app_shutw_conn(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200667 }
668}
669
670/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200671 * This function performs a shutdown-write on a stream connector attached to
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200672 * a connection in a connected or init state (it does nothing for other
673 * states). It either shuts the write side or marks itself as closed. The
674 * buffer flags are updated to reflect the new state. It does also close
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +0200675 * everything if the CS was marked as being in error state. If there is a
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200676 * data-layer shutdown, it is called.
677 */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200678static void sc_app_shutw_conn(struct stconn *cs)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200679{
Willy Tarreau40a9c322022-05-18 15:55:18 +0200680 struct channel *ic = sc_ic(cs);
681 struct channel *oc = sc_oc(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200682
Willy Tarreaufd9417b2022-05-18 16:23:22 +0200683 BUG_ON(!sc_conn(cs));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200684
685 oc->flags &= ~CF_SHUTW_NOW;
686 if (oc->flags & CF_SHUTW)
687 return;
688 oc->flags |= CF_SHUTW;
689 oc->wex = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200690
691 if (tick_isset(cs->hcto)) {
692 ic->rto = cs->hcto;
693 ic->rex = tick_add(now_ms, ic->rto);
694 }
695
696 switch (cs->state) {
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200697 case SC_ST_RDY:
698 case SC_ST_EST:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200699 /* we have to shut before closing, otherwise some short messages
700 * may never leave the system, especially when there are remaining
701 * unread data in the socket input buffer, or when nolinger is set.
Willy Tarreaucb041662022-05-17 19:44:42 +0200702 * However, if SC_FL_NOLINGER is explicitly set, we know there is
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200703 * no risk so we close both sides immediately.
704 */
705
Willy Tarreaub605c422022-05-17 17:04:55 +0200706 if (sc_ep_test(cs, SE_FL_ERROR)) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200707 /* quick close, the socket is already shut anyway */
708 }
Willy Tarreaucb041662022-05-17 19:44:42 +0200709 else if (cs->flags & SC_FL_NOLINGER) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200710 /* unclean data-layer shutdown, typically an aborted request
711 * or a forwarded shutdown from a client to a server due to
712 * option abortonclose. No need for the TLS layer to try to
713 * emit a shutdown message.
714 */
Willy Tarreau462b9892022-05-18 18:06:53 +0200715 sc_conn_shutw(cs, CO_SHW_SILENT);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200716 }
717 else {
718 /* clean data-layer shutdown. This only happens on the
719 * frontend side, or on the backend side when forwarding
720 * a client close in TCP mode or in HTTP TUNNEL mode
721 * while option abortonclose is set. We want the TLS
722 * layer to try to signal it to the peer before we close.
723 */
Willy Tarreau462b9892022-05-18 18:06:53 +0200724 sc_conn_shutw(cs, CO_SHW_NORMAL);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200725
726 if (!(ic->flags & (CF_SHUTR|CF_DONT_READ)))
727 return;
728 }
729
730 /* fall through */
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200731 case SC_ST_CON:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200732 /* we may have to close a pending connection, and mark the
733 * response buffer as shutr
734 */
Willy Tarreau462b9892022-05-18 18:06:53 +0200735 sc_conn_shut(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200736 /* fall through */
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200737 case SC_ST_CER:
738 case SC_ST_QUE:
739 case SC_ST_TAR:
740 cs->state = SC_ST_DIS;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200741 /* fall through */
742 default:
Willy Tarreaucb041662022-05-17 19:44:42 +0200743 cs->flags &= ~SC_FL_NOLINGER;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200744 ic->flags |= CF_SHUTR;
745 ic->rex = TICK_ETERNITY;
Willy Tarreauea27f482022-05-18 16:10:52 +0200746 __sc_strm(cs)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200747 }
748}
749
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200750/* This function is used for inter-stream connector calls. It is called by the
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200751 * consumer to inform the producer side that it may be interested in checking
752 * for free space in the buffer. Note that it intentionally does not update
753 * timeouts, so that we can still check them later at wake-up. This function is
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200754 * dedicated to connection-based stream connectors.
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200755 */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200756static void sc_app_chk_rcv_conn(struct stconn *cs)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200757{
Willy Tarreaufd9417b2022-05-18 16:23:22 +0200758 BUG_ON(!sc_conn(cs));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200759
760 /* (re)start reading */
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200761 if (cs_state_in(cs->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200762 tasklet_wakeup(cs->wait_event.tasklet);
763}
764
765
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200766/* This function is used for inter-stream connector calls. It is called by the
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200767 * producer to inform the consumer side that it may be interested in checking
768 * for data in the buffer. Note that it intentionally does not update timeouts,
769 * so that we can still check them later at wake-up.
770 */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200771static void sc_app_chk_snd_conn(struct stconn *cs)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200772{
Willy Tarreau40a9c322022-05-18 15:55:18 +0200773 struct channel *oc = sc_oc(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200774
Willy Tarreaufd9417b2022-05-18 16:23:22 +0200775 BUG_ON(!sc_conn(cs));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200776
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200777 if (unlikely(!cs_state_in(cs->state, SC_SB_RDY|SC_SB_EST) ||
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200778 (oc->flags & CF_SHUTW)))
779 return;
780
781 if (unlikely(channel_is_empty(oc))) /* called with nothing to send ! */
782 return;
783
784 if (!oc->pipe && /* spliced data wants to be forwarded ASAP */
Willy Tarreaub605c422022-05-17 17:04:55 +0200785 !sc_ep_test(cs, SE_FL_WAIT_DATA)) /* not waiting for data */
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200786 return;
787
Willy Tarreau40a9c322022-05-18 15:55:18 +0200788 if (!(cs->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(sc_oc(cs)))
Willy Tarreau462b9892022-05-18 18:06:53 +0200789 sc_conn_send(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200790
Willy Tarreaub605c422022-05-17 17:04:55 +0200791 if (sc_ep_test(cs, SE_FL_ERROR | SE_FL_ERR_PENDING) || cs_is_conn_error(cs)) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200792 /* Write error on the file descriptor */
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200793 if (cs->state >= SC_ST_CON)
Willy Tarreaub605c422022-05-17 17:04:55 +0200794 sc_ep_set(cs, SE_FL_ERROR);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200795 goto out_wakeup;
796 }
797
798 /* OK, so now we know that some data might have been sent, and that we may
799 * have to poll first. We have to do that too if the buffer is not empty.
800 */
801 if (channel_is_empty(oc)) {
802 /* the connection is established but we can't write. Either the
803 * buffer is empty, or we just refrain from sending because the
804 * ->o limit was reached. Maybe we just wrote the last
805 * chunk and need to close.
806 */
807 if (((oc->flags & (CF_SHUTW|CF_AUTO_CLOSE|CF_SHUTW_NOW)) ==
808 (CF_AUTO_CLOSE|CF_SHUTW_NOW)) &&
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200809 cs_state_in(cs->state, SC_SB_RDY|SC_SB_EST)) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200810 cs_shutw(cs);
811 goto out_wakeup;
812 }
813
814 if ((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == 0)
Willy Tarreaub605c422022-05-17 17:04:55 +0200815 sc_ep_set(cs, SE_FL_WAIT_DATA);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200816 oc->wex = TICK_ETERNITY;
817 }
818 else {
819 /* Otherwise there are remaining data to be sent in the buffer,
820 * which means we have to poll before doing so.
821 */
Willy Tarreaub605c422022-05-17 17:04:55 +0200822 sc_ep_clr(cs, SE_FL_WAIT_DATA);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200823 if (!tick_isset(oc->wex))
824 oc->wex = tick_add_ifset(now_ms, oc->wto);
825 }
826
827 if (likely(oc->flags & CF_WRITE_ACTIVITY)) {
Willy Tarreau40a9c322022-05-18 15:55:18 +0200828 struct channel *ic = sc_ic(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200829
830 /* update timeout if we have written something */
831 if ((oc->flags & (CF_SHUTW|CF_WRITE_PARTIAL)) == CF_WRITE_PARTIAL &&
832 !channel_is_empty(oc))
833 oc->wex = tick_add_ifset(now_ms, oc->wto);
834
Willy Tarreaucb041662022-05-17 19:44:42 +0200835 if (tick_isset(ic->rex) && !(cs->flags & SC_FL_INDEP_STR)) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200836 /* Note: to prevent the client from expiring read timeouts
837 * during writes, we refresh it. We only do this if the
838 * interface is not configured for "independent streams",
839 * because for some applications it's better not to do this,
840 * for instance when continuously exchanging small amounts
841 * of data which can full the socket buffers long before a
842 * write timeout is detected.
843 */
844 ic->rex = tick_add_ifset(now_ms, ic->rto);
845 }
846 }
847
848 /* in case of special condition (error, shutdown, end of write...), we
849 * have to notify the task.
850 */
851 if (likely((oc->flags & (CF_WRITE_NULL|CF_WRITE_ERROR|CF_SHUTW)) ||
852 ((oc->flags & CF_WAKE_WRITE) &&
853 ((channel_is_empty(oc) && !oc->to_forward) ||
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200854 !cs_state_in(cs->state, SC_SB_EST))))) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200855 out_wakeup:
Willy Tarreaucb041662022-05-17 19:44:42 +0200856 if (!(cs->flags & SC_FL_DONT_WAKE))
Willy Tarreauea27f482022-05-18 16:10:52 +0200857 task_wakeup(sc_strm_task(cs), TASK_WOKEN_IO);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200858 }
859}
860
861/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200862 * This function performs a shutdown-read on a stream connector attached to an
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200863 * applet in a connected or init state (it does nothing for other states). It
864 * either shuts the read side or marks itself as closed. The buffer flags are
Willy Tarreaucb041662022-05-17 19:44:42 +0200865 * updated to reflect the new state. If the stream connector has SC_FL_NOHALF,
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200866 * we also forward the close to the write side. The owner task is woken up if
867 * it exists.
868 */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200869static void sc_app_shutr_applet(struct stconn *cs)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200870{
Willy Tarreau40a9c322022-05-18 15:55:18 +0200871 struct channel *ic = sc_ic(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200872
Willy Tarreau8e7c6e62022-05-18 17:58:02 +0200873 BUG_ON(!sc_appctx(cs));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200874
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200875 if (ic->flags & CF_SHUTR)
876 return;
877 ic->flags |= CF_SHUTR;
878 ic->rex = TICK_ETERNITY;
879
880 /* Note: on shutr, we don't call the applet */
881
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200882 if (!cs_state_in(cs->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200883 return;
884
Willy Tarreau40a9c322022-05-18 15:55:18 +0200885 if (sc_oc(cs)->flags & CF_SHUTW) {
Willy Tarreau8e7c6e62022-05-18 17:58:02 +0200886 appctx_shut(__sc_appctx(cs));
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200887 cs->state = SC_ST_DIS;
Willy Tarreauea27f482022-05-18 16:10:52 +0200888 __sc_strm(cs)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200889 }
Willy Tarreaucb041662022-05-17 19:44:42 +0200890 else if (cs->flags & SC_FL_NOHALF) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200891 /* we want to immediately forward this close to the write side */
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200892 return sc_app_shutw_applet(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200893 }
894}
895
896/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200897 * This function performs a shutdown-write on a stream connector attached to an
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200898 * applet in a connected or init state (it does nothing for other states). It
899 * either shuts the write side or marks itself as closed. The buffer flags are
900 * updated to reflect the new state. It does also close everything if the SI
901 * was marked as being in error state. The owner task is woken up if it exists.
902 */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200903static void sc_app_shutw_applet(struct stconn *cs)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200904{
Willy Tarreau40a9c322022-05-18 15:55:18 +0200905 struct channel *ic = sc_ic(cs);
906 struct channel *oc = sc_oc(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200907
Willy Tarreau8e7c6e62022-05-18 17:58:02 +0200908 BUG_ON(!sc_appctx(cs));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200909
910 oc->flags &= ~CF_SHUTW_NOW;
911 if (oc->flags & CF_SHUTW)
912 return;
913 oc->flags |= CF_SHUTW;
914 oc->wex = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200915
916 if (tick_isset(cs->hcto)) {
917 ic->rto = cs->hcto;
918 ic->rex = tick_add(now_ms, ic->rto);
919 }
920
921 /* on shutw we always wake the applet up */
Willy Tarreau8e7c6e62022-05-18 17:58:02 +0200922 appctx_wakeup(__sc_appctx(cs));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200923
924 switch (cs->state) {
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200925 case SC_ST_RDY:
926 case SC_ST_EST:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200927 /* we have to shut before closing, otherwise some short messages
928 * may never leave the system, especially when there are remaining
929 * unread data in the socket input buffer, or when nolinger is set.
Willy Tarreaucb041662022-05-17 19:44:42 +0200930 * However, if SC_FL_NOLINGER is explicitly set, we know there is
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200931 * no risk so we close both sides immediately.
932 */
Willy Tarreaucb041662022-05-17 19:44:42 +0200933 if (!sc_ep_test(cs, SE_FL_ERROR) && !(cs->flags & SC_FL_NOLINGER) &&
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200934 !(ic->flags & (CF_SHUTR|CF_DONT_READ)))
935 return;
936
937 /* fall through */
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200938 case SC_ST_CON:
939 case SC_ST_CER:
940 case SC_ST_QUE:
941 case SC_ST_TAR:
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200942 /* Note that none of these states may happen with applets */
Willy Tarreau8e7c6e62022-05-18 17:58:02 +0200943 appctx_shut(__sc_appctx(cs));
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200944 cs->state = SC_ST_DIS;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200945 /* fall through */
946 default:
Willy Tarreaucb041662022-05-17 19:44:42 +0200947 cs->flags &= ~SC_FL_NOLINGER;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200948 ic->flags |= CF_SHUTR;
949 ic->rex = TICK_ETERNITY;
Willy Tarreauea27f482022-05-18 16:10:52 +0200950 __sc_strm(cs)->conn_exp = TICK_ETERNITY;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200951 }
952}
953
954/* chk_rcv function for applets */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200955static void sc_app_chk_rcv_applet(struct stconn *cs)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200956{
Willy Tarreau40a9c322022-05-18 15:55:18 +0200957 struct channel *ic = sc_ic(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200958
Willy Tarreau8e7c6e62022-05-18 17:58:02 +0200959 BUG_ON(!sc_appctx(cs));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200960
961 DPRINTF(stderr, "%s: cs=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n",
962 __FUNCTION__,
Willy Tarreau40a9c322022-05-18 15:55:18 +0200963 cs, cs->state, ic->flags, sc_oc(cs)->flags);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200964
965 if (!ic->pipe) {
966 /* (re)start reading */
Willy Tarreau8e7c6e62022-05-18 17:58:02 +0200967 appctx_wakeup(__sc_appctx(cs));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200968 }
969}
970
971/* chk_snd function for applets */
Willy Tarreau4596fe22022-05-17 19:07:51 +0200972static void sc_app_chk_snd_applet(struct stconn *cs)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200973{
Willy Tarreau40a9c322022-05-18 15:55:18 +0200974 struct channel *oc = sc_oc(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200975
Willy Tarreau8e7c6e62022-05-18 17:58:02 +0200976 BUG_ON(!sc_appctx(cs));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200977
978 DPRINTF(stderr, "%s: cs=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n",
979 __FUNCTION__,
Willy Tarreau40a9c322022-05-18 15:55:18 +0200980 cs, cs->state, sc_ic(cs)->flags, oc->flags);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200981
Willy Tarreau026e8fb2022-05-17 19:47:17 +0200982 if (unlikely(cs->state != SC_ST_EST || (oc->flags & CF_SHUTW)))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200983 return;
984
985 /* we only wake the applet up if it was waiting for some data */
986
Willy Tarreaub605c422022-05-17 17:04:55 +0200987 if (!sc_ep_test(cs, SE_FL_WAIT_DATA))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200988 return;
989
990 if (!tick_isset(oc->wex))
991 oc->wex = tick_add_ifset(now_ms, oc->wto);
992
993 if (!channel_is_empty(oc)) {
994 /* (re)start sending */
Willy Tarreau8e7c6e62022-05-18 17:58:02 +0200995 appctx_wakeup(__sc_appctx(cs));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200996 }
997}
Christopher Faulet13045f02022-04-01 14:23:38 +0200998
999
1000/* This function is designed to be called from within the stream handler to
Willy Tarreau4596fe22022-05-17 19:07:51 +02001001 * update the input channel's expiration timer and the stream connector's
Christopher Faulet13045f02022-04-01 14:23:38 +02001002 * Rx flags based on the channel's flags. It needs to be called only once
1003 * after the channel's flags have settled down, and before they are cleared,
1004 * though it doesn't harm to call it as often as desired (it just slightly
1005 * hurts performance). It must not be called from outside of the stream
1006 * handler, as what it does will be used to compute the stream task's
1007 * expiration.
1008 */
Willy Tarreau4596fe22022-05-17 19:07:51 +02001009void cs_update_rx(struct stconn *cs)
Christopher Faulet13045f02022-04-01 14:23:38 +02001010{
Willy Tarreau40a9c322022-05-18 15:55:18 +02001011 struct channel *ic = sc_ic(cs);
Christopher Faulet13045f02022-04-01 14:23:38 +02001012
Willy Tarreau676c8db2022-05-24 16:22:24 +02001013 if (ic->flags & CF_SHUTR)
Christopher Faulet13045f02022-04-01 14:23:38 +02001014 return;
Christopher Faulet13045f02022-04-01 14:23:38 +02001015
1016 /* Read not closed, update FD status and timeout for reads */
1017 if (ic->flags & CF_DONT_READ)
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001018 cs_rx_chan_blk(cs);
Christopher Faulet13045f02022-04-01 14:23:38 +02001019 else
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001020 cs_rx_chan_rdy(cs);
Christopher Faulet13045f02022-04-01 14:23:38 +02001021
1022 if (!channel_is_empty(ic) || !channel_may_recv(ic)) {
1023 /* stop reading, imposed by channel's policy or contents */
Willy Tarreau99615ed2022-05-25 07:29:36 +02001024 sc_need_room(cs);
Christopher Faulet13045f02022-04-01 14:23:38 +02001025 }
1026 else {
1027 /* (re)start reading and update timeout. Note: we don't recompute the timeout
1028 * every time we get here, otherwise it would risk never to expire. We only
1029 * update it if is was not yet set. The stream socket handler will already
1030 * have updated it if there has been a completed I/O.
1031 */
Willy Tarreau99615ed2022-05-25 07:29:36 +02001032 sc_have_room(cs);
Christopher Faulet13045f02022-04-01 14:23:38 +02001033 }
Willy Tarreaub605c422022-05-17 17:04:55 +02001034 if (sc_ep_test(cs, SE_FL_RXBLK_ANY))
Christopher Faulet13045f02022-04-01 14:23:38 +02001035 ic->rex = TICK_ETERNITY;
1036 else if (!(ic->flags & CF_READ_NOEXP) && !tick_isset(ic->rex))
1037 ic->rex = tick_add_ifset(now_ms, ic->rto);
1038
1039 cs_chk_rcv(cs);
1040}
1041
1042/* This function is designed to be called from within the stream handler to
Willy Tarreau4596fe22022-05-17 19:07:51 +02001043 * update the output channel's expiration timer and the stream connector's
Christopher Faulet13045f02022-04-01 14:23:38 +02001044 * Tx flags based on the channel's flags. It needs to be called only once
1045 * after the channel's flags have settled down, and before they are cleared,
1046 * though it doesn't harm to call it as often as desired (it just slightly
1047 * hurts performance). It must not be called from outside of the stream
1048 * handler, as what it does will be used to compute the stream task's
1049 * expiration.
1050 */
Willy Tarreau4596fe22022-05-17 19:07:51 +02001051void cs_update_tx(struct stconn *cs)
Christopher Faulet13045f02022-04-01 14:23:38 +02001052{
Willy Tarreau40a9c322022-05-18 15:55:18 +02001053 struct channel *oc = sc_oc(cs);
1054 struct channel *ic = sc_ic(cs);
Christopher Faulet13045f02022-04-01 14:23:38 +02001055
1056 if (oc->flags & CF_SHUTW)
1057 return;
1058
1059 /* Write not closed, update FD status and timeout for writes */
1060 if (channel_is_empty(oc)) {
1061 /* stop writing */
Willy Tarreaub605c422022-05-17 17:04:55 +02001062 if (!sc_ep_test(cs, SE_FL_WAIT_DATA)) {
Christopher Faulet13045f02022-04-01 14:23:38 +02001063 if ((oc->flags & CF_SHUTW_NOW) == 0)
Willy Tarreaub605c422022-05-17 17:04:55 +02001064 sc_ep_set(cs, SE_FL_WAIT_DATA);
Christopher Faulet13045f02022-04-01 14:23:38 +02001065 oc->wex = TICK_ETERNITY;
1066 }
1067 return;
1068 }
1069
1070 /* (re)start writing and update timeout. Note: we don't recompute the timeout
1071 * every time we get here, otherwise it would risk never to expire. We only
1072 * update it if is was not yet set. The stream socket handler will already
1073 * have updated it if there has been a completed I/O.
1074 */
Willy Tarreaub605c422022-05-17 17:04:55 +02001075 sc_ep_clr(cs, SE_FL_WAIT_DATA);
Christopher Faulet13045f02022-04-01 14:23:38 +02001076 if (!tick_isset(oc->wex)) {
1077 oc->wex = tick_add_ifset(now_ms, oc->wto);
Willy Tarreaucb041662022-05-17 19:44:42 +02001078 if (tick_isset(ic->rex) && !(cs->flags & SC_FL_INDEP_STR)) {
Christopher Faulet13045f02022-04-01 14:23:38 +02001079 /* Note: depending on the protocol, we don't know if we're waiting
1080 * for incoming data or not. So in order to prevent the socket from
1081 * expiring read timeouts during writes, we refresh the read timeout,
1082 * except if it was already infinite or if we have explicitly setup
1083 * independent streams.
1084 */
1085 ic->rex = tick_add_ifset(now_ms, ic->rto);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001086 }
1087 }
1088}
1089
1090/* This function is the equivalent to cs_update() except that it's
1091 * designed to be called from outside the stream handlers, typically the lower
1092 * layers (applets, connections) after I/O completion. After updating the stream
1093 * interface and timeouts, it will try to forward what can be forwarded, then to
1094 * wake the associated task up if an important event requires special handling.
Willy Tarreaub605c422022-05-17 17:04:55 +02001095 * It may update SE_FL_WAIT_DATA and/or SE_FL_RXBLK_ROOM, that the callers are
Christopher Faulet5e29b762022-04-04 08:58:34 +02001096 * encouraged to watch to take appropriate action.
1097 * It should not be called from within the stream itself, cs_update()
1098 * is designed for this.
1099 */
Willy Tarreau4596fe22022-05-17 19:07:51 +02001100static void cs_notify(struct stconn *cs)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001101{
Willy Tarreau40a9c322022-05-18 15:55:18 +02001102 struct channel *ic = sc_ic(cs);
1103 struct channel *oc = sc_oc(cs);
Willy Tarreau4596fe22022-05-17 19:07:51 +02001104 struct stconn *cso = cs_opposite(cs);
Willy Tarreauea27f482022-05-18 16:10:52 +02001105 struct task *task = sc_strm_task(cs);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001106
1107 /* process consumer side */
1108 if (channel_is_empty(oc)) {
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001109 struct connection *conn = sc_conn(cs);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001110
1111 if (((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW) &&
Willy Tarreau026e8fb2022-05-17 19:47:17 +02001112 (cs->state == SC_ST_EST) && (!conn || !(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS))))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001113 cs_shutw(cs);
1114 oc->wex = TICK_ETERNITY;
1115 }
1116
1117 /* indicate that we may be waiting for data from the output channel or
1118 * we're about to close and can't expect more data if SHUTW_NOW is there.
1119 */
1120 if (!(oc->flags & (CF_SHUTW|CF_SHUTW_NOW)))
Willy Tarreaub605c422022-05-17 17:04:55 +02001121 sc_ep_set(cs, SE_FL_WAIT_DATA);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001122 else if ((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW)
Willy Tarreaub605c422022-05-17 17:04:55 +02001123 sc_ep_clr(cs, SE_FL_WAIT_DATA);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001124
1125 /* update OC timeouts and wake the other side up if it's waiting for room */
1126 if (oc->flags & CF_WRITE_ACTIVITY) {
1127 if ((oc->flags & (CF_SHUTW|CF_WRITE_PARTIAL)) == CF_WRITE_PARTIAL &&
1128 !channel_is_empty(oc))
1129 if (tick_isset(oc->wex))
1130 oc->wex = tick_add_ifset(now_ms, oc->wto);
1131
Willy Tarreaucb041662022-05-17 19:44:42 +02001132 if (!(cs->flags & SC_FL_INDEP_STR))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001133 if (tick_isset(ic->rex))
1134 ic->rex = tick_add_ifset(now_ms, ic->rto);
1135 }
1136
1137 if (oc->flags & CF_DONT_READ)
1138 cs_rx_chan_blk(cso);
1139 else
1140 cs_rx_chan_rdy(cso);
1141
1142 /* Notify the other side when we've injected data into the IC that
1143 * needs to be forwarded. We can do fast-forwarding as soon as there
1144 * are output data, but we avoid doing this if some of the data are
1145 * not yet scheduled for being forwarded, because it is very likely
1146 * that it will be done again immediately afterwards once the following
Willy Tarreaub605c422022-05-17 17:04:55 +02001147 * data are parsed (eg: HTTP chunking). We only SE_FL_RXBLK_ROOM once
Christopher Faulet5e29b762022-04-04 08:58:34 +02001148 * we've emptied *some* of the output buffer, and not just when there
1149 * is available room, because applets are often forced to stop before
1150 * the buffer is full. We must not stop based on input data alone because
1151 * an HTTP parser might need more data to complete the parsing.
1152 */
1153 if (!channel_is_empty(ic) &&
Willy Tarreaub605c422022-05-17 17:04:55 +02001154 sc_ep_test(cso, SE_FL_WAIT_DATA) &&
Christopher Faulet5e29b762022-04-04 08:58:34 +02001155 (!(ic->flags & CF_EXPECT_MORE) || c_full(ic) || ci_data(ic) == 0 || ic->pipe)) {
1156 int new_len, last_len;
1157
1158 last_len = co_data(ic);
1159 if (ic->pipe)
1160 last_len += ic->pipe->data;
1161
1162 cs_chk_snd(cso);
1163
1164 new_len = co_data(ic);
1165 if (ic->pipe)
1166 new_len += ic->pipe->data;
1167
1168 /* check if the consumer has freed some space either in the
1169 * buffer or in the pipe.
1170 */
1171 if (new_len < last_len)
Willy Tarreau99615ed2022-05-25 07:29:36 +02001172 sc_have_room(cs);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001173 }
1174
1175 if (!(ic->flags & CF_DONT_READ))
1176 cs_rx_chan_rdy(cs);
1177
1178 cs_chk_rcv(cs);
1179 cs_chk_rcv(cso);
1180
Willy Tarreaub73262f2022-05-24 16:56:55 +02001181 if (ic->flags & CF_SHUTR || sc_ep_test(cs, SE_FL_APPLET_NEED_CONN) || cs_rx_blocked(cs)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001182 ic->rex = TICK_ETERNITY;
1183 }
1184 else if ((ic->flags & (CF_SHUTR|CF_READ_PARTIAL)) == CF_READ_PARTIAL) {
1185 /* we must re-enable reading if cs_chk_snd() has freed some space */
1186 if (!(ic->flags & CF_READ_NOEXP) && tick_isset(ic->rex))
1187 ic->rex = tick_add_ifset(now_ms, ic->rto);
1188 }
1189
1190 /* wake the task up only when needed */
1191 if (/* changes on the production side */
1192 (ic->flags & (CF_READ_NULL|CF_READ_ERROR)) ||
Willy Tarreau026e8fb2022-05-17 19:47:17 +02001193 !cs_state_in(cs->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST) ||
Willy Tarreaub605c422022-05-17 17:04:55 +02001194 sc_ep_test(cs, SE_FL_ERROR) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001195 ((ic->flags & CF_READ_PARTIAL) &&
Willy Tarreau026e8fb2022-05-17 19:47:17 +02001196 ((ic->flags & CF_EOI) || !ic->to_forward || cso->state != SC_ST_EST)) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001197
1198 /* changes on the consumption side */
1199 (oc->flags & (CF_WRITE_NULL|CF_WRITE_ERROR)) ||
1200 ((oc->flags & CF_WRITE_ACTIVITY) &&
1201 ((oc->flags & CF_SHUTW) ||
1202 (((oc->flags & CF_WAKE_WRITE) ||
1203 !(oc->flags & (CF_AUTO_CLOSE|CF_SHUTW_NOW|CF_SHUTW))) &&
Willy Tarreau026e8fb2022-05-17 19:47:17 +02001204 (cso->state != SC_ST_EST ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001205 (channel_is_empty(oc) && !oc->to_forward)))))) {
1206 task_wakeup(task, TASK_WOKEN_IO);
1207 }
1208 else {
1209 /* Update expiration date for the task and requeue it */
1210 task->expire = tick_first((tick_is_expired(task->expire, now_ms) ? 0 : task->expire),
1211 tick_first(tick_first(ic->rex, ic->wex),
1212 tick_first(oc->rex, oc->wex)));
1213
1214 task->expire = tick_first(task->expire, ic->analyse_exp);
1215 task->expire = tick_first(task->expire, oc->analyse_exp);
Willy Tarreauea27f482022-05-18 16:10:52 +02001216 task->expire = tick_first(task->expire, __sc_strm(cs)->conn_exp);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001217
1218 task_queue(task);
1219 }
1220 if (ic->flags & CF_READ_ACTIVITY)
1221 ic->flags &= ~CF_READ_DONTWAIT;
1222}
1223
1224/*
1225 * This function propagates a null read received on a socket-based connection.
Willy Tarreaucb041662022-05-17 19:44:42 +02001226 * It updates the stream connector. If the stream connector has SC_FL_NOHALF,
Christopher Faulet5e29b762022-04-04 08:58:34 +02001227 * the close is also forwarded to the write side as an abort.
1228 */
Willy Tarreau462b9892022-05-18 18:06:53 +02001229static void sc_conn_read0(struct stconn *cs)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001230{
Willy Tarreau40a9c322022-05-18 15:55:18 +02001231 struct channel *ic = sc_ic(cs);
1232 struct channel *oc = sc_oc(cs);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001233
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001234 BUG_ON(!sc_conn(cs));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001235
Christopher Faulet5e29b762022-04-04 08:58:34 +02001236 if (ic->flags & CF_SHUTR)
1237 return;
1238 ic->flags |= CF_SHUTR;
1239 ic->rex = TICK_ETERNITY;
1240
Willy Tarreau026e8fb2022-05-17 19:47:17 +02001241 if (!cs_state_in(cs->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001242 return;
1243
1244 if (oc->flags & CF_SHUTW)
1245 goto do_close;
1246
Willy Tarreaucb041662022-05-17 19:44:42 +02001247 if (cs->flags & SC_FL_NOHALF) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001248 /* we want to immediately forward this close to the write side */
1249 /* force flag on ssl to keep stream in cache */
Willy Tarreau462b9892022-05-18 18:06:53 +02001250 sc_conn_shutw(cs, CO_SHW_SILENT);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001251 goto do_close;
1252 }
1253
1254 /* otherwise that's just a normal read shutdown */
1255 return;
1256
1257 do_close:
1258 /* OK we completely close the socket here just as if we went through cs_shut[rw]() */
Willy Tarreau462b9892022-05-18 18:06:53 +02001259 sc_conn_shut(cs);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001260
1261 oc->flags &= ~CF_SHUTW_NOW;
1262 oc->flags |= CF_SHUTW;
1263 oc->wex = TICK_ETERNITY;
1264
Willy Tarreau026e8fb2022-05-17 19:47:17 +02001265 cs->state = SC_ST_DIS;
Willy Tarreauea27f482022-05-18 16:10:52 +02001266 __sc_strm(cs)->conn_exp = TICK_ETERNITY;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001267 return;
1268}
1269
1270/*
1271 * This is the callback which is called by the connection layer to receive data
1272 * into the buffer from the connection. It iterates over the mux layer's
1273 * rcv_buf function.
1274 */
Willy Tarreau462b9892022-05-18 18:06:53 +02001275static int sc_conn_recv(struct stconn *cs)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001276{
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001277 struct connection *conn = __sc_conn(cs);
Willy Tarreau40a9c322022-05-18 15:55:18 +02001278 struct channel *ic = sc_ic(cs);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001279 int ret, max, cur_read = 0;
1280 int read_poll = MAX_READ_POLL_LOOPS;
1281 int flags = 0;
1282
1283 /* If not established yet, do nothing. */
Willy Tarreau026e8fb2022-05-17 19:47:17 +02001284 if (cs->state != SC_ST_EST)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001285 return 0;
1286
Willy Tarreau462b9892022-05-18 18:06:53 +02001287 /* If another call to sc_conn_recv() failed, and we subscribed to
Christopher Faulet5e29b762022-04-04 08:58:34 +02001288 * recv events already, give up now.
1289 */
1290 if (cs->wait_event.events & SUB_RETRY_RECV)
1291 return 0;
1292
1293 /* maybe we were called immediately after an asynchronous shutr */
1294 if (ic->flags & CF_SHUTR)
1295 return 1;
1296
1297 /* we must wait because the mux is not installed yet */
1298 if (!conn->mux)
1299 return 0;
1300
1301 /* stop here if we reached the end of data */
Willy Tarreaub605c422022-05-17 17:04:55 +02001302 if (sc_ep_test(cs, SE_FL_EOS))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001303 goto end_recv;
1304
1305 /* stop immediately on errors. Note that we DON'T want to stop on
1306 * POLL_ERR, as the poller might report a write error while there
1307 * are still data available in the recv buffer. This typically
1308 * happens when we send too large a request to a backend server
1309 * which rejects it before reading it all.
1310 */
Willy Tarreaub605c422022-05-17 17:04:55 +02001311 if (!sc_ep_test(cs, SE_FL_RCV_MORE)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001312 if (!conn_xprt_ready(conn))
1313 return 0;
Willy Tarreaub605c422022-05-17 17:04:55 +02001314 if (sc_ep_test(cs, SE_FL_ERROR))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001315 goto end_recv;
1316 }
1317
1318 /* prepare to detect if the mux needs more room */
Willy Tarreaub605c422022-05-17 17:04:55 +02001319 sc_ep_clr(cs, SE_FL_WANT_ROOM);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001320
1321 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) && !co_data(ic) &&
1322 global.tune.idle_timer &&
1323 (unsigned short)(now_ms - ic->last_read) >= global.tune.idle_timer) {
1324 /* The buffer was empty and nothing was transferred for more
1325 * than one second. This was caused by a pause and not by
1326 * congestion. Reset any streaming mode to reduce latency.
1327 */
1328 ic->xfer_small = 0;
1329 ic->xfer_large = 0;
1330 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
1331 }
1332
1333 /* First, let's see if we may splice data across the channel without
1334 * using a buffer.
1335 */
Willy Tarreaub605c422022-05-17 17:04:55 +02001336 if (sc_ep_test(cs, SE_FL_MAY_SPLICE) &&
Christopher Faulet5e29b762022-04-04 08:58:34 +02001337 (ic->pipe || ic->to_forward >= MIN_SPLICE_FORWARD) &&
1338 ic->flags & CF_KERN_SPLICING) {
1339 if (c_data(ic)) {
1340 /* We're embarrassed, there are already data pending in
1341 * the buffer and we don't want to have them at two
1342 * locations at a time. Let's indicate we need some
1343 * place and ask the consumer to hurry.
1344 */
1345 flags |= CO_RFL_BUF_FLUSH;
1346 goto abort_splice;
1347 }
1348
1349 if (unlikely(ic->pipe == NULL)) {
1350 if (pipes_used >= global.maxpipes || !(ic->pipe = get_pipe())) {
1351 ic->flags &= ~CF_KERN_SPLICING;
1352 goto abort_splice;
1353 }
1354 }
1355
1356 ret = conn->mux->rcv_pipe(cs, ic->pipe, ic->to_forward);
1357 if (ret < 0) {
1358 /* splice not supported on this end, let's disable it */
1359 ic->flags &= ~CF_KERN_SPLICING;
1360 goto abort_splice;
1361 }
1362
1363 if (ret > 0) {
1364 if (ic->to_forward != CHN_INFINITE_FORWARD)
1365 ic->to_forward -= ret;
1366 ic->total += ret;
1367 cur_read += ret;
1368 ic->flags |= CF_READ_PARTIAL;
1369 }
1370
Willy Tarreaub605c422022-05-17 17:04:55 +02001371 if (sc_ep_test(cs, SE_FL_EOS | SE_FL_ERROR))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001372 goto end_recv;
1373
1374 if (conn->flags & CO_FL_WAIT_ROOM) {
1375 /* the pipe is full or we have read enough data that it
1376 * could soon be full. Let's stop before needing to poll.
1377 */
Willy Tarreau99615ed2022-05-25 07:29:36 +02001378 sc_need_room(cs);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001379 goto done_recv;
1380 }
1381
1382 /* splice not possible (anymore), let's go on on standard copy */
1383 }
1384
1385 abort_splice:
1386 if (ic->pipe && unlikely(!ic->pipe->data)) {
1387 put_pipe(ic->pipe);
1388 ic->pipe = NULL;
1389 }
1390
Willy Tarreaub605c422022-05-17 17:04:55 +02001391 if (ic->pipe && ic->to_forward && !(flags & CO_RFL_BUF_FLUSH) && sc_ep_test(cs, SE_FL_MAY_SPLICE)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001392 /* don't break splicing by reading, but still call rcv_buf()
1393 * to pass the flag.
1394 */
1395 goto done_recv;
1396 }
1397
1398 /* now we'll need a input buffer for the stream */
Willy Tarreauea27f482022-05-18 16:10:52 +02001399 if (!cs_alloc_ibuf(cs, &(__sc_strm(cs)->buffer_wait)))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001400 goto end_recv;
1401
1402 /* For an HTX stream, if the buffer is stuck (no output data with some
1403 * input data) and if the HTX message is fragmented or if its free space
1404 * wraps, we force an HTX deframentation. It is a way to have a
1405 * contiguous free space nad to let the mux to copy as much data as
1406 * possible.
1407 *
1408 * NOTE: A possible optim may be to let the mux decides if defrag is
1409 * required or not, depending on amount of data to be xferred.
1410 */
Willy Tarreauea27f482022-05-18 16:10:52 +02001411 if (IS_HTX_STRM(__sc_strm(cs)) && !co_data(ic)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001412 struct htx *htx = htxbuf(&ic->buf);
1413
1414 if (htx_is_not_empty(htx) && ((htx->flags & HTX_FL_FRAGMENTED) || htx_space_wraps(htx)))
1415 htx_defrag(htx, NULL, 0);
1416 }
1417
1418 /* Instruct the mux it must subscribed for read events */
Willy Tarreauea27f482022-05-18 16:10:52 +02001419 flags |= ((!conn_is_back(conn) && (__sc_strm(cs)->be->options & PR_O_ABRT_CLOSE)) ? CO_RFL_KEEP_RECV : 0);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001420
1421 /* Important note : if we're called with POLL_IN|POLL_HUP, it means the read polling
1422 * was enabled, which implies that the recv buffer was not full. So we have a guarantee
1423 * that if such an event is not handled above in splice, it will be handled here by
1424 * recv().
1425 */
Willy Tarreaub605c422022-05-17 17:04:55 +02001426 while (sc_ep_test(cs, SE_FL_RCV_MORE) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001427 (!(conn->flags & CO_FL_HANDSHAKE) &&
Willy Tarreaub605c422022-05-17 17:04:55 +02001428 (!sc_ep_test(cs, SE_FL_ERROR | SE_FL_EOS)) && !(ic->flags & CF_SHUTR))) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001429 int cur_flags = flags;
1430
1431 /* Compute transient CO_RFL_* flags */
1432 if (co_data(ic)) {
1433 cur_flags |= (CO_RFL_BUF_WET | CO_RFL_BUF_NOT_STUCK);
1434 }
1435
1436 /* <max> may be null. This is the mux responsibility to set
Willy Tarreaub605c422022-05-17 17:04:55 +02001437 * SE_FL_RCV_MORE on the CS if more space is needed.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001438 */
1439 max = channel_recv_max(ic);
1440 ret = conn->mux->rcv_buf(cs, &ic->buf, max, cur_flags);
1441
Willy Tarreaub605c422022-05-17 17:04:55 +02001442 if (sc_ep_test(cs, SE_FL_WANT_ROOM)) {
1443 /* SE_FL_WANT_ROOM must not be reported if the channel's
Christopher Faulet5e29b762022-04-04 08:58:34 +02001444 * buffer is empty.
1445 */
1446 BUG_ON(c_empty(ic));
1447
Willy Tarreau99615ed2022-05-25 07:29:36 +02001448 sc_need_room(cs);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001449 /* Add READ_PARTIAL because some data are pending but
1450 * cannot be xferred to the channel
1451 */
1452 ic->flags |= CF_READ_PARTIAL;
1453 }
1454
1455 if (ret <= 0) {
1456 /* if we refrained from reading because we asked for a
1457 * flush to satisfy rcv_pipe(), we must not subscribe
1458 * and instead report that there's not enough room
1459 * here to proceed.
1460 */
1461 if (flags & CO_RFL_BUF_FLUSH)
Willy Tarreau99615ed2022-05-25 07:29:36 +02001462 sc_need_room(cs);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001463 break;
1464 }
1465
1466 cur_read += ret;
1467
1468 /* if we're allowed to directly forward data, we must update ->o */
1469 if (ic->to_forward && !(ic->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
1470 unsigned long fwd = ret;
1471 if (ic->to_forward != CHN_INFINITE_FORWARD) {
1472 if (fwd > ic->to_forward)
1473 fwd = ic->to_forward;
1474 ic->to_forward -= fwd;
1475 }
1476 c_adv(ic, fwd);
1477 }
1478
1479 ic->flags |= CF_READ_PARTIAL;
1480 ic->total += ret;
1481
1482 /* End-of-input reached, we can leave. In this case, it is
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001483 * important to break the loop to not block the CS because of
Christopher Faulet5e29b762022-04-04 08:58:34 +02001484 * the channel's policies.This way, we are still able to receive
1485 * shutdowns.
1486 */
Willy Tarreaub605c422022-05-17 17:04:55 +02001487 if (sc_ep_test(cs, SE_FL_EOI))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001488 break;
1489
1490 if ((ic->flags & CF_READ_DONTWAIT) || --read_poll <= 0) {
1491 /* we're stopped by the channel's policy */
1492 cs_rx_chan_blk(cs);
1493 break;
1494 }
1495
1496 /* if too many bytes were missing from last read, it means that
1497 * it's pointless trying to read again because the system does
1498 * not have them in buffers.
1499 */
1500 if (ret < max) {
1501 /* if a streamer has read few data, it may be because we
1502 * have exhausted system buffers. It's not worth trying
1503 * again.
1504 */
1505 if (ic->flags & CF_STREAMER) {
1506 /* we're stopped by the channel's policy */
1507 cs_rx_chan_blk(cs);
1508 break;
1509 }
1510
1511 /* if we read a large block smaller than what we requested,
1512 * it's almost certain we'll never get anything more.
1513 */
1514 if (ret >= global.tune.recv_enough) {
1515 /* we're stopped by the channel's policy */
1516 cs_rx_chan_blk(cs);
1517 break;
1518 }
1519 }
1520
1521 /* if we are waiting for more space, don't try to read more data
1522 * right now.
1523 */
1524 if (cs_rx_blocked(cs))
1525 break;
1526 } /* while !flags */
1527
1528 done_recv:
1529 if (cur_read) {
1530 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) &&
1531 (cur_read <= ic->buf.size / 2)) {
1532 ic->xfer_large = 0;
1533 ic->xfer_small++;
1534 if (ic->xfer_small >= 3) {
1535 /* we have read less than half of the buffer in
1536 * one pass, and this happened at least 3 times.
1537 * This is definitely not a streamer.
1538 */
1539 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
1540 }
1541 else if (ic->xfer_small >= 2) {
1542 /* if the buffer has been at least half full twice,
1543 * we receive faster than we send, so at least it
1544 * is not a "fast streamer".
1545 */
1546 ic->flags &= ~CF_STREAMER_FAST;
1547 }
1548 }
1549 else if (!(ic->flags & CF_STREAMER_FAST) &&
1550 (cur_read >= ic->buf.size - global.tune.maxrewrite)) {
1551 /* we read a full buffer at once */
1552 ic->xfer_small = 0;
1553 ic->xfer_large++;
1554 if (ic->xfer_large >= 3) {
1555 /* we call this buffer a fast streamer if it manages
1556 * to be filled in one call 3 consecutive times.
1557 */
1558 ic->flags |= (CF_STREAMER | CF_STREAMER_FAST);
1559 }
1560 }
1561 else {
1562 ic->xfer_small = 0;
1563 ic->xfer_large = 0;
1564 }
1565 ic->last_read = now_ms;
1566 }
1567
1568 end_recv:
1569 ret = (cur_read != 0);
1570
1571 /* Report EOI on the channel if it was reached from the mux point of
1572 * view. */
Willy Tarreaub605c422022-05-17 17:04:55 +02001573 if (sc_ep_test(cs, SE_FL_EOI) && !(ic->flags & CF_EOI)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001574 ic->flags |= (CF_EOI|CF_READ_PARTIAL);
1575 ret = 1;
1576 }
1577
Willy Tarreaub605c422022-05-17 17:04:55 +02001578 if (sc_ep_test(cs, SE_FL_ERROR))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001579 ret = 1;
Willy Tarreaub605c422022-05-17 17:04:55 +02001580 else if (sc_ep_test(cs, SE_FL_EOS)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001581 /* we received a shutdown */
1582 ic->flags |= CF_READ_NULL;
1583 if (ic->flags & CF_AUTO_CLOSE)
1584 channel_shutw_now(ic);
Willy Tarreau462b9892022-05-18 18:06:53 +02001585 sc_conn_read0(cs);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001586 ret = 1;
1587 }
Willy Tarreaue7866b12022-05-24 16:18:11 +02001588 else if (!cs_rx_blocked(cs) && !(ic->flags & CF_SHUTR)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001589 /* Subscribe to receive events if we're blocking on I/O */
1590 conn->mux->subscribe(cs, SUB_RETRY_RECV, &cs->wait_event);
1591 cs_rx_endp_done(cs);
1592 } else {
1593 cs_rx_endp_more(cs);
1594 ret = 1;
1595 }
1596 return ret;
1597}
1598
Willy Tarreau4596fe22022-05-17 19:07:51 +02001599/* This tries to perform a synchronous receive on the stream connector to
Christopher Faulet5e29b762022-04-04 08:58:34 +02001600 * try to collect last arrived data. In practice it's only implemented on
Willy Tarreau4596fe22022-05-17 19:07:51 +02001601 * stconns. Returns 0 if nothing was done, non-zero if new data or a
Christopher Faulet5e29b762022-04-04 08:58:34 +02001602 * shutdown were collected. This may result on some delayed receive calls
1603 * to be programmed and performed later, though it doesn't provide any
1604 * such guarantee.
1605 */
Willy Tarreau462b9892022-05-18 18:06:53 +02001606int sc_conn_sync_recv(struct stconn *cs)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001607{
Willy Tarreau026e8fb2022-05-17 19:47:17 +02001608 if (!cs_state_in(cs->state, SC_SB_RDY|SC_SB_EST))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001609 return 0;
1610
Willy Tarreau417a31b2022-05-18 17:51:19 +02001611 if (!sc_mux_ops(cs))
Willy Tarreau4596fe22022-05-17 19:07:51 +02001612 return 0; // only stconns are supported
Christopher Faulet5e29b762022-04-04 08:58:34 +02001613
1614 if (cs->wait_event.events & SUB_RETRY_RECV)
1615 return 0; // already subscribed
1616
1617 if (!cs_rx_endp_ready(cs) || cs_rx_blocked(cs))
1618 return 0; // already failed
1619
Willy Tarreau462b9892022-05-18 18:06:53 +02001620 return sc_conn_recv(cs);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001621}
1622
1623/*
1624 * This function is called to send buffer data to a stream socket.
1625 * It calls the mux layer's snd_buf function. It relies on the
1626 * caller to commit polling changes. The caller should check conn->flags
1627 * for errors.
1628 */
Willy Tarreau462b9892022-05-18 18:06:53 +02001629static int sc_conn_send(struct stconn *cs)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001630{
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001631 struct connection *conn = __sc_conn(cs);
Willy Tarreauea27f482022-05-18 16:10:52 +02001632 struct stream *s = __sc_strm(cs);
Willy Tarreau40a9c322022-05-18 15:55:18 +02001633 struct channel *oc = sc_oc(cs);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001634 int ret;
1635 int did_send = 0;
1636
Willy Tarreaub605c422022-05-17 17:04:55 +02001637 if (sc_ep_test(cs, SE_FL_ERROR | SE_FL_ERR_PENDING) || cs_is_conn_error(cs)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001638 /* We're probably there because the tasklet was woken up,
1639 * but process_stream() ran before, detected there were an
Willy Tarreau026e8fb2022-05-17 19:47:17 +02001640 * error and put the CS back to SC_ST_TAR. There's still
Christopher Faulet5e29b762022-04-04 08:58:34 +02001641 * CO_FL_ERROR on the connection but we don't want to add
Willy Tarreaub605c422022-05-17 17:04:55 +02001642 * SE_FL_ERROR back, so give up
Christopher Faulet5e29b762022-04-04 08:58:34 +02001643 */
Willy Tarreau026e8fb2022-05-17 19:47:17 +02001644 if (cs->state < SC_ST_CON)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001645 return 0;
Willy Tarreaub605c422022-05-17 17:04:55 +02001646 sc_ep_set(cs, SE_FL_ERROR);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001647 return 1;
1648 }
1649
1650 /* We're already waiting to be able to send, give up */
1651 if (cs->wait_event.events & SUB_RETRY_SEND)
1652 return 0;
1653
1654 /* we might have been called just after an asynchronous shutw */
1655 if (oc->flags & CF_SHUTW)
1656 return 1;
1657
1658 /* we must wait because the mux is not installed yet */
1659 if (!conn->mux)
1660 return 0;
1661
1662 if (oc->pipe && conn->xprt->snd_pipe && conn->mux->snd_pipe) {
1663 ret = conn->mux->snd_pipe(cs, oc->pipe);
1664 if (ret > 0)
1665 did_send = 1;
1666
1667 if (!oc->pipe->data) {
1668 put_pipe(oc->pipe);
1669 oc->pipe = NULL;
1670 }
1671
1672 if (oc->pipe)
1673 goto end;
1674 }
1675
1676 /* At this point, the pipe is empty, but we may still have data pending
1677 * in the normal buffer.
1678 */
1679 if (co_data(oc)) {
1680 /* when we're here, we already know that there is no spliced
1681 * data left, and that there are sendable buffered data.
1682 */
1683
1684 /* check if we want to inform the kernel that we're interested in
1685 * sending more data after this call. We want this if :
1686 * - we're about to close after this last send and want to merge
1687 * the ongoing FIN with the last segment.
1688 * - we know we can't send everything at once and must get back
1689 * here because of unaligned data
1690 * - there is still a finite amount of data to forward
1691 * The test is arranged so that the most common case does only 2
1692 * tests.
1693 */
1694 unsigned int send_flag = 0;
1695
1696 if ((!(oc->flags & (CF_NEVER_WAIT|CF_SEND_DONTWAIT)) &&
1697 ((oc->to_forward && oc->to_forward != CHN_INFINITE_FORWARD) ||
1698 (oc->flags & CF_EXPECT_MORE) ||
1699 (IS_HTX_STRM(s) &&
1700 (!(oc->flags & (CF_EOI|CF_SHUTR)) && htx_expect_more(htxbuf(&oc->buf)))))) ||
1701 ((oc->flags & CF_ISRESP) &&
1702 ((oc->flags & (CF_AUTO_CLOSE|CF_SHUTW_NOW)) == (CF_AUTO_CLOSE|CF_SHUTW_NOW))))
1703 send_flag |= CO_SFL_MSG_MORE;
1704
1705 if (oc->flags & CF_STREAMER)
1706 send_flag |= CO_SFL_STREAMER;
1707
1708 if (s->txn && s->txn->flags & TX_L7_RETRY && !b_data(&s->txn->l7_buffer)) {
1709 /* If we want to be able to do L7 retries, copy
1710 * the data we're about to send, so that we are able
1711 * to resend them if needed
1712 */
1713 /* Try to allocate a buffer if we had none.
1714 * If it fails, the next test will just
1715 * disable the l7 retries by setting
1716 * l7_conn_retries to 0.
1717 */
1718 if (s->txn->req.msg_state != HTTP_MSG_DONE)
1719 s->txn->flags &= ~TX_L7_RETRY;
1720 else {
1721 if (b_alloc(&s->txn->l7_buffer) == NULL)
1722 s->txn->flags &= ~TX_L7_RETRY;
1723 else {
1724 memcpy(b_orig(&s->txn->l7_buffer),
1725 b_orig(&oc->buf),
1726 b_size(&oc->buf));
1727 s->txn->l7_buffer.head = co_data(oc);
1728 b_add(&s->txn->l7_buffer, co_data(oc));
1729 }
1730
1731 }
1732 }
1733
1734 ret = conn->mux->snd_buf(cs, &oc->buf, co_data(oc), send_flag);
1735 if (ret > 0) {
1736 did_send = 1;
1737 c_rew(oc, ret);
1738 c_realign_if_empty(oc);
1739
1740 if (!co_data(oc)) {
1741 /* Always clear both flags once everything has been sent, they're one-shot */
1742 oc->flags &= ~(CF_EXPECT_MORE | CF_SEND_DONTWAIT);
1743 }
1744 /* if some data remain in the buffer, it's only because the
1745 * system buffers are full, we will try next time.
1746 */
Christopher Faulet13045f02022-04-01 14:23:38 +02001747 }
1748 }
Christopher Faulet5e29b762022-04-04 08:58:34 +02001749
1750 end:
1751 if (did_send) {
1752 oc->flags |= CF_WRITE_PARTIAL | CF_WROTE_DATA;
Willy Tarreau026e8fb2022-05-17 19:47:17 +02001753 if (cs->state == SC_ST_CON)
1754 cs->state = SC_ST_RDY;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001755
Willy Tarreau99615ed2022-05-25 07:29:36 +02001756 sc_have_room(cs_opposite(cs));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001757 }
1758
Willy Tarreaub605c422022-05-17 17:04:55 +02001759 if (sc_ep_test(cs, SE_FL_ERROR | SE_FL_ERR_PENDING)) {
1760 sc_ep_set(cs, SE_FL_ERROR);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001761 return 1;
1762 }
1763
1764 /* We couldn't send all of our data, let the mux know we'd like to send more */
1765 if (!channel_is_empty(oc))
1766 conn->mux->subscribe(cs, SUB_RETRY_SEND, &cs->wait_event);
1767 return did_send;
1768}
1769
Willy Tarreau4596fe22022-05-17 19:07:51 +02001770/* perform a synchronous send() for the stream connector. The CF_WRITE_NULL and
Christopher Faulet5e29b762022-04-04 08:58:34 +02001771 * CF_WRITE_PARTIAL flags are cleared prior to the attempt, and will possibly
1772 * be updated in case of success.
1773 */
Willy Tarreau462b9892022-05-18 18:06:53 +02001774void sc_conn_sync_send(struct stconn *cs)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001775{
Willy Tarreau40a9c322022-05-18 15:55:18 +02001776 struct channel *oc = sc_oc(cs);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001777
1778 oc->flags &= ~(CF_WRITE_NULL|CF_WRITE_PARTIAL);
1779
1780 if (oc->flags & CF_SHUTW)
1781 return;
1782
1783 if (channel_is_empty(oc))
1784 return;
1785
Willy Tarreau026e8fb2022-05-17 19:47:17 +02001786 if (!cs_state_in(cs->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001787 return;
1788
Willy Tarreau417a31b2022-05-18 17:51:19 +02001789 if (!sc_mux_ops(cs))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001790 return;
1791
Willy Tarreau462b9892022-05-18 18:06:53 +02001792 sc_conn_send(cs);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001793}
1794
1795/* Called by I/O handlers after completion.. It propagates
Willy Tarreau4596fe22022-05-17 19:07:51 +02001796 * connection flags to the stream connector, updates the stream (which may or
Christopher Faulet5e29b762022-04-04 08:58:34 +02001797 * may not take this opportunity to try to forward data), then update the
Willy Tarreau4596fe22022-05-17 19:07:51 +02001798 * connection's polling based on the channels and stream connector's final
Christopher Faulet5e29b762022-04-04 08:58:34 +02001799 * states. The function always returns 0.
1800 */
Willy Tarreau462b9892022-05-18 18:06:53 +02001801static int sc_conn_process(struct stconn *cs)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001802{
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001803 struct connection *conn = __sc_conn(cs);
Willy Tarreau40a9c322022-05-18 15:55:18 +02001804 struct channel *ic = sc_ic(cs);
1805 struct channel *oc = sc_oc(cs);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001806
1807 BUG_ON(!conn);
1808
1809 /* If we have data to send, try it now */
1810 if (!channel_is_empty(oc) && !(cs->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau462b9892022-05-18 18:06:53 +02001811 sc_conn_send(cs);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001812
Willy Tarreau4596fe22022-05-17 19:07:51 +02001813 /* First step, report to the stream connector what was detected at the
Christopher Faulet5e29b762022-04-04 08:58:34 +02001814 * connection layer : errors and connection establishment.
Willy Tarreaub605c422022-05-17 17:04:55 +02001815 * Only add SE_FL_ERROR if we're connected, or we're attempting to
Christopher Faulet5e29b762022-04-04 08:58:34 +02001816 * connect, we may get there because we got woken up, but only run
1817 * after process_stream() noticed there were an error, and decided
1818 * to retry to connect, the connection may still have CO_FL_ERROR,
Willy Tarreaub605c422022-05-17 17:04:55 +02001819 * and we don't want to add SE_FL_ERROR back
Christopher Faulet5e29b762022-04-04 08:58:34 +02001820 *
Willy Tarreau462b9892022-05-18 18:06:53 +02001821 * Note: This test is only required because sc_conn_process is also the SI
1822 * wake callback. Otherwise sc_conn_recv()/sc_conn_send() already take
Christopher Faulet5e29b762022-04-04 08:58:34 +02001823 * care of it.
1824 */
1825
Willy Tarreau026e8fb2022-05-17 19:47:17 +02001826 if (cs->state >= SC_ST_CON) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001827 if (cs_is_conn_error(cs))
Willy Tarreaub605c422022-05-17 17:04:55 +02001828 sc_ep_set(cs, SE_FL_ERROR);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001829 }
1830
1831 /* If we had early data, and the handshake ended, then
1832 * we can remove the flag, and attempt to wake the task up,
1833 * in the event there's an analyser waiting for the end of
1834 * the handshake.
1835 */
1836 if (!(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS)) &&
Willy Tarreaub605c422022-05-17 17:04:55 +02001837 sc_ep_test(cs, SE_FL_WAIT_FOR_HS)) {
1838 sc_ep_clr(cs, SE_FL_WAIT_FOR_HS);
Willy Tarreauea27f482022-05-18 16:10:52 +02001839 task_wakeup(sc_strm_task(cs), TASK_WOKEN_MSG);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001840 }
1841
Willy Tarreau026e8fb2022-05-17 19:47:17 +02001842 if (!cs_state_in(cs->state, SC_SB_EST|SC_SB_DIS|SC_SB_CLO) &&
Christopher Faulet5e29b762022-04-04 08:58:34 +02001843 (conn->flags & CO_FL_WAIT_XPRT) == 0) {
Willy Tarreauea27f482022-05-18 16:10:52 +02001844 __sc_strm(cs)->conn_exp = TICK_ETERNITY;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001845 oc->flags |= CF_WRITE_NULL;
Willy Tarreau026e8fb2022-05-17 19:47:17 +02001846 if (cs->state == SC_ST_CON)
1847 cs->state = SC_ST_RDY;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001848 }
1849
1850 /* Report EOS on the channel if it was reached from the mux point of
1851 * view.
1852 *
Willy Tarreau462b9892022-05-18 18:06:53 +02001853 * Note: This test is only required because sc_conn_process is also the SI
1854 * wake callback. Otherwise sc_conn_recv()/sc_conn_send() already take
Christopher Faulet5e29b762022-04-04 08:58:34 +02001855 * care of it.
1856 */
Willy Tarreaub605c422022-05-17 17:04:55 +02001857 if (sc_ep_test(cs, SE_FL_EOS) && !(ic->flags & CF_SHUTR)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001858 /* we received a shutdown */
1859 ic->flags |= CF_READ_NULL;
1860 if (ic->flags & CF_AUTO_CLOSE)
1861 channel_shutw_now(ic);
Willy Tarreau462b9892022-05-18 18:06:53 +02001862 sc_conn_read0(cs);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001863 }
1864
1865 /* Report EOI on the channel if it was reached from the mux point of
1866 * view.
1867 *
Willy Tarreau462b9892022-05-18 18:06:53 +02001868 * Note: This test is only required because sc_conn_process is also the SI
1869 * wake callback. Otherwise sc_conn_recv()/sc_conn_send() already take
Christopher Faulet5e29b762022-04-04 08:58:34 +02001870 * care of it.
1871 */
Willy Tarreaub605c422022-05-17 17:04:55 +02001872 if (sc_ep_test(cs, SE_FL_EOI) && !(ic->flags & CF_EOI))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001873 ic->flags |= (CF_EOI|CF_READ_PARTIAL);
1874
Willy Tarreau4596fe22022-05-17 19:07:51 +02001875 /* Second step : update the stream connector and channels, try to forward any
Christopher Faulet5e29b762022-04-04 08:58:34 +02001876 * pending data, then possibly wake the stream up based on the new
Willy Tarreau4596fe22022-05-17 19:07:51 +02001877 * stream connector status.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001878 */
1879 cs_notify(cs);
Willy Tarreauea27f482022-05-18 16:10:52 +02001880 stream_release_buffers(__sc_strm(cs));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001881 return 0;
1882}
1883
Willy Tarreau4596fe22022-05-17 19:07:51 +02001884/* This is the ->process() function for any stream connector's wait_event task.
1885 * It's assigned during the stream connector's initialization, for any type of
1886 * stream connector. Thus it is always safe to perform a tasklet_wakeup() on a
1887 * stream connector, as the presence of the CS is checked there.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001888 */
Willy Tarreau462b9892022-05-18 18:06:53 +02001889struct task *sc_conn_io_cb(struct task *t, void *ctx, unsigned int state)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001890{
Willy Tarreau4596fe22022-05-17 19:07:51 +02001891 struct stconn *cs = ctx;
Christopher Faulet5e29b762022-04-04 08:58:34 +02001892 int ret = 0;
1893
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001894 if (!sc_conn(cs))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001895 return t;
1896
Willy Tarreau40a9c322022-05-18 15:55:18 +02001897 if (!(cs->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(sc_oc(cs)))
Willy Tarreau462b9892022-05-18 18:06:53 +02001898 ret = sc_conn_send(cs);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001899 if (!(cs->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau462b9892022-05-18 18:06:53 +02001900 ret |= sc_conn_recv(cs);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001901 if (ret != 0)
Willy Tarreau462b9892022-05-18 18:06:53 +02001902 sc_conn_process(cs);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001903
Willy Tarreauea27f482022-05-18 16:10:52 +02001904 stream_release_buffers(__sc_strm(cs));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001905 return t;
1906}
1907
1908/* Callback to be used by applet handlers upon completion. It updates the stream
1909 * (which may or may not take this opportunity to try to forward data), then
Willy Tarreau4596fe22022-05-17 19:07:51 +02001910 * may re-enable the applet's based on the channels and stream connector's final
Christopher Faulet5e29b762022-04-04 08:58:34 +02001911 * states.
1912 */
Willy Tarreau4596fe22022-05-17 19:07:51 +02001913static int cs_applet_process(struct stconn *cs)
Christopher Faulet5e29b762022-04-04 08:58:34 +02001914{
Willy Tarreau40a9c322022-05-18 15:55:18 +02001915 struct channel *ic = sc_ic(cs);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001916
Willy Tarreau8e7c6e62022-05-18 17:58:02 +02001917 BUG_ON(!sc_appctx(cs));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001918
1919 /* If the applet wants to write and the channel is closed, it's a
1920 * broken pipe and it must be reported.
1921 */
Willy Tarreaub605c422022-05-17 17:04:55 +02001922 if (!sc_ep_test(cs, SE_FL_RX_WAIT_EP) && (ic->flags & CF_SHUTR))
1923 sc_ep_set(cs, SE_FL_ERROR);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001924
1925 /* automatically mark the applet having data available if it reported
1926 * begin blocked by the channel.
1927 */
Willy Tarreaub73262f2022-05-24 16:56:55 +02001928 if (cs_rx_blocked(cs) || sc_ep_test(cs, SE_FL_APPLET_NEED_CONN))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001929 cs_rx_endp_more(cs);
1930
Willy Tarreau4596fe22022-05-17 19:07:51 +02001931 /* update the stream connector, channels, and possibly wake the stream up */
Christopher Faulet5e29b762022-04-04 08:58:34 +02001932 cs_notify(cs);
Willy Tarreauea27f482022-05-18 16:10:52 +02001933 stream_release_buffers(__sc_strm(cs));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001934
1935 /* cs_notify may have passed through chk_snd and released some
1936 * RXBLK flags. Process_stream will consider those flags to wake up the
1937 * appctx but in the case the task is not in runqueue we may have to
1938 * wakeup the appctx immediately.
1939 */
Willy Tarreaub73262f2022-05-24 16:56:55 +02001940 if ((cs_rx_endp_ready(cs) && !cs_rx_blocked(cs) &&
1941 !sc_ep_test(cs, SE_FL_APPLET_NEED_CONN) && !(ic->flags & CF_SHUTR)) ||
Willy Tarreaue7866b12022-05-24 16:18:11 +02001942 sc_is_send_allowed(cs))
Willy Tarreau8e7c6e62022-05-18 17:58:02 +02001943 appctx_wakeup(__sc_appctx(cs));
Christopher Faulet5e29b762022-04-04 08:58:34 +02001944 return 0;
Christopher Faulet13045f02022-04-01 14:23:38 +02001945}