blob: db703d2da93649d463bc0a77f0c7b1f8593b6c36 [file] [log] [blame]
Christopher Faulet1329f2a2021-12-16 17:32:56 +01001/*
2 * Conn-stream management functions
3 *
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
23DECLARE_POOL(pool_head_connstream, "conn_stream", sizeof(struct conn_stream));
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 */
27static void sc_app_shutr(struct conn_stream *cs);
28static void sc_app_shutw(struct conn_stream *cs);
29static void sc_app_chk_rcv(struct conn_stream *cs);
30static void sc_app_chk_snd(struct conn_stream *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 */
33static void sc_app_shutr_conn(struct conn_stream *cs);
34static void sc_app_shutw_conn(struct conn_stream *cs);
35static void sc_app_chk_rcv_conn(struct conn_stream *cs);
36static void sc_app_chk_snd_conn(struct conn_stream *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 */
39static void sc_app_shutr_applet(struct conn_stream *cs);
40static void sc_app_shutw_applet(struct conn_stream *cs);
41static void sc_app_chk_rcv_applet(struct conn_stream *cs);
42static void sc_app_chk_snd_applet(struct conn_stream *cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +020043
Willy Tarreau3a3f4802022-05-17 18:28:19 +020044/* stream connector operations for connections */
45struct sc_app_ops sc_app_conn_ops = {
46 .chk_rcv = sc_app_chk_rcv_conn,
47 .chk_snd = sc_app_chk_snd_conn,
48 .shutr = sc_app_shutr_conn,
49 .shutw = sc_app_shutw_conn,
Christopher Faulet9ffddd52022-04-01 14:04:29 +020050};
51
Willy Tarreau3a3f4802022-05-17 18:28:19 +020052/* stream connector operations for embedded tasks */
53struct sc_app_ops sc_app_embedded_ops = {
54 .chk_rcv = sc_app_chk_rcv,
55 .chk_snd = sc_app_chk_snd,
56 .shutr = sc_app_shutr,
57 .shutw = sc_app_shutw,
Christopher Faulet9ffddd52022-04-01 14:04:29 +020058};
59
Willy Tarreau3a3f4802022-05-17 18:28:19 +020060/* stream connector operations for connections */
61struct sc_app_ops sc_app_applet_ops = {
62 .chk_rcv = sc_app_chk_rcv_applet,
63 .chk_snd = sc_app_chk_snd_applet,
64 .shutr = sc_app_shutr_applet,
65 .shutw = sc_app_shutw_applet,
Christopher Faulet9ffddd52022-04-01 14:04:29 +020066};
67
Christopher Faulet5e29b762022-04-04 08:58:34 +020068static int cs_conn_process(struct conn_stream *cs);
69static int cs_conn_recv(struct conn_stream *cs);
70static int cs_conn_send(struct conn_stream *cs);
71static int cs_applet_process(struct conn_stream *cs);
72
73struct data_cb cs_data_conn_cb = {
74 .wake = cs_conn_process,
75 .name = "STRM",
76};
77
78struct data_cb cs_data_applet_cb = {
79 .wake = cs_applet_process,
80 .name = "STRM",
81};
82
83
Christopher Faulet9ed77422022-04-12 08:51:15 +020084/* Initializes an endpoint */
Willy Tarreauea59b022022-05-17 17:53:22 +020085void sedesc_init(struct sedesc *sedesc)
Christopher Fauletdb90f2a2022-03-22 16:06:25 +010086{
Willy Tarreauea59b022022-05-17 17:53:22 +020087 sedesc->se = NULL;
88 sedesc->conn = NULL;
89 sedesc->cs = NULL;
90 se_fl_setall(sedesc, SE_FL_NONE);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +010091}
92
Christopher Faulet9ed77422022-04-12 08:51:15 +020093/* Tries to alloc an endpoint and initialize it. Returns NULL on failure. */
Willy Tarreauea59b022022-05-17 17:53:22 +020094struct sedesc *sedesc_new()
Christopher Fauletdb90f2a2022-03-22 16:06:25 +010095{
Willy Tarreauea59b022022-05-17 17:53:22 +020096 struct sedesc *sedesc;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +010097
Willy Tarreauea59b022022-05-17 17:53:22 +020098 sedesc = pool_alloc(pool_head_sedesc);
99 if (unlikely(!sedesc))
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100100 return NULL;
101
Willy Tarreauea59b022022-05-17 17:53:22 +0200102 sedesc_init(sedesc);
103 return sedesc;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100104}
105
Christopher Faulet9ed77422022-04-12 08:51:15 +0200106/* Releases an endpoint. It is the caller responsibility to be sure it is safe
107 * and it is not shared with another entity
108 */
Willy Tarreauea59b022022-05-17 17:53:22 +0200109void sedesc_free(struct sedesc *sedesc)
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100110{
Willy Tarreauea59b022022-05-17 17:53:22 +0200111 pool_free(pool_head_sedesc, sedesc);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100112}
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100113
Christopher Fauletdd2d0d82021-12-20 09:34:32 +0100114/* Tries to allocate a new conn_stream and initialize its main fields. On
Christopher Faulet9ed77422022-04-12 08:51:15 +0200115 * failure, nothing is allocated and NULL is returned. It is an internal
Willy Tarreaub605c422022-05-17 17:04:55 +0200116 * function. The caller must, at least, set the SE_FL_ORPHAN or SE_FL_DETACHED
Christopher Faulet9ed77422022-04-12 08:51:15 +0200117 * flag.
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100118 */
Willy Tarreauea59b022022-05-17 17:53:22 +0200119static struct conn_stream *cs_new(struct sedesc *sedesc)
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100120{
121 struct conn_stream *cs;
122
123 cs = pool_alloc(pool_head_connstream);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100124
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100125 if (unlikely(!cs))
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100126 goto alloc_error;
Christopher Fauletbb772d02022-03-22 15:28:36 +0100127
128 cs->obj_type = OBJ_TYPE_CS;
129 cs->flags = CS_FL_NONE;
Christopher Faulet62e75742022-03-31 09:16:34 +0200130 cs->state = CS_ST_INI;
Christopher Faulet1d987772022-03-29 18:03:35 +0200131 cs->hcto = TICK_ETERNITY;
Christopher Fauletbb772d02022-03-22 15:28:36 +0100132 cs->app = NULL;
Christopher Fauletbb772d02022-03-22 15:28:36 +0100133 cs->data_cb = NULL;
Christopher Faulet8da67aa2022-03-29 17:53:09 +0200134 cs->src = NULL;
135 cs->dst = NULL;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200136 cs->wait_event.tasklet = NULL;
137 cs->wait_event.events = 0;
138
Christopher Faulet9ed77422022-04-12 08:51:15 +0200139 /* If there is no endpoint, allocate a new one now */
Willy Tarreauea59b022022-05-17 17:53:22 +0200140 if (!sedesc) {
141 sedesc = sedesc_new();
142 if (unlikely(!sedesc))
Christopher Fauletb669d682022-03-22 18:37:19 +0100143 goto alloc_error;
144 }
Willy Tarreau798465b2022-05-17 18:20:02 +0200145 cs->sedesc = sedesc;
Willy Tarreauea59b022022-05-17 17:53:22 +0200146 sedesc->cs = cs;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100147
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100148 return cs;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100149
150 alloc_error:
151 pool_free(pool_head_connstream, cs);
152 return NULL;
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100153}
154
Christopher Faulet9ed77422022-04-12 08:51:15 +0200155/* Creates a new conn-stream and its associated stream from a mux. <endp> must be
156 * defined. It returns NULL on error. On success, the new conn-stream is
Willy Tarreaub605c422022-05-17 17:04:55 +0200157 * returned. In this case, SE_FL_ORPHAN flag is removed.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200158 */
Willy Tarreauea59b022022-05-17 17:53:22 +0200159struct conn_stream *cs_new_from_endp(struct sedesc *sedesc, struct session *sess, struct buffer *input)
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100160{
161 struct conn_stream *cs;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100162
Willy Tarreauea59b022022-05-17 17:53:22 +0200163 cs = cs_new(sedesc);
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100164 if (unlikely(!cs))
165 return NULL;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100166 if (unlikely(!stream_new(sess, cs, input))) {
167 pool_free(pool_head_connstream, cs);
168 cs = NULL;
169 }
Willy Tarreauea59b022022-05-17 17:53:22 +0200170 se_fl_clr(sedesc, SE_FL_ORPHAN);
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100171 return cs;
172}
173
Christopher Faulet9ed77422022-04-12 08:51:15 +0200174/* Creates a new conn-stream from an stream. There is no endpoint here, thus it
Willy Tarreaub605c422022-05-17 17:04:55 +0200175 * will be created by cs_new(). So the SE_FL_DETACHED flag is set. It returns
Christopher Faulet9ed77422022-04-12 08:51:15 +0200176 * NULL on error. On success, the new conn-stream is returned.
177 */
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100178struct conn_stream *cs_new_from_strm(struct stream *strm, unsigned int flags)
179{
180 struct conn_stream *cs;
181
182 cs = cs_new(NULL);
183 if (unlikely(!cs))
184 return NULL;
185 cs->flags |= flags;
Willy Tarreaub605c422022-05-17 17:04:55 +0200186 sc_ep_set(cs, SE_FL_DETACHED);
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100187 cs->app = &strm->obj_type;
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200188 cs->ops = &sc_app_embedded_ops;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100189 cs->data_cb = NULL;
190 return cs;
191}
192
Christopher Faulet9ed77422022-04-12 08:51:15 +0200193/* Creates a new conn-stream from an health-check. There is no endpoint here,
Willy Tarreaub605c422022-05-17 17:04:55 +0200194 * thus it will be created by cs_new(). So the SE_FL_DETACHED flag is set. It
Christopher Faulet9ed77422022-04-12 08:51:15 +0200195 * returns NULL on error. On success, the new conn-stream is returned.
196 */
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100197struct conn_stream *cs_new_from_check(struct check *check, unsigned int flags)
198{
199 struct conn_stream *cs;
200
201 cs = cs_new(NULL);
202 if (unlikely(!cs))
203 return NULL;
204 cs->flags |= flags;
Willy Tarreaub605c422022-05-17 17:04:55 +0200205 sc_ep_set(cs, SE_FL_DETACHED);
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100206 cs->app = &check->obj_type;
207 cs->data_cb = &check_conn_cb;
208 return cs;
209}
210
Christopher Faulet9ed77422022-04-12 08:51:15 +0200211/* Releases a conn_stream previously allocated by cs_new(), as well as its
212 * endpoint, if it exists. This function is called internally or on error path.
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100213 */
214void cs_free(struct conn_stream *cs)
215{
Christopher Faulet8da67aa2022-03-29 17:53:09 +0200216 sockaddr_free(&cs->src);
217 sockaddr_free(&cs->dst);
Willy Tarreau798465b2022-05-17 18:20:02 +0200218 if (cs->sedesc) {
Willy Tarreaub605c422022-05-17 17:04:55 +0200219 BUG_ON(!sc_ep_test(cs, SE_FL_DETACHED));
Willy Tarreau798465b2022-05-17 18:20:02 +0200220 sedesc_free(cs->sedesc);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100221 }
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200222 if (cs->wait_event.tasklet)
223 tasklet_free(cs->wait_event.tasklet);
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100224 pool_free(pool_head_connstream, cs);
225}
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100226
Christopher Fauleteb50c012022-04-21 14:22:53 +0200227/* Conditionally removes a conn-stream if it is detached and if there is no app
228 * layer defined. Except on error path, this one must be used. if release, the
229 * pointer on the CS is set to NULL.
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200230 */
Christopher Fauleteb50c012022-04-21 14:22:53 +0200231static void cs_free_cond(struct conn_stream **csp)
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200232{
Christopher Fauleteb50c012022-04-21 14:22:53 +0200233 struct conn_stream *cs = *csp;
234
Willy Tarreau798465b2022-05-17 18:20:02 +0200235 if (!cs->app && (!cs->sedesc || sc_ep_test(cs, SE_FL_DETACHED))) {
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200236 cs_free(cs);
Christopher Fauleteb50c012022-04-21 14:22:53 +0200237 *csp = NULL;
238 }
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200239}
240
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100241
Christopher Faulet9ed77422022-04-12 08:51:15 +0200242/* Attaches a conn_stream to a mux endpoint and sets the endpoint ctx. Returns
Willy Tarreaub605c422022-05-17 17:04:55 +0200243 * -1 on error and 0 on sucess. SE_FL_DETACHED flag is removed. This function is
Christopher Faulet9ed77422022-04-12 08:51:15 +0200244 * called from a mux when it is attached to a stream or a health-check.
245 */
Willy Tarreau65d05972022-05-16 17:29:42 +0200246int cs_attach_mux(struct conn_stream *cs, void *endp, void *ctx)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100247{
Christopher Faulet93882042022-01-19 14:56:50 +0100248 struct connection *conn = ctx;
Willy Tarreau798465b2022-05-17 18:20:02 +0200249 struct sedesc *sedesc = cs->sedesc;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100250
Willy Tarreau798465b2022-05-17 18:20:02 +0200251 sedesc->se = endp;
252 sedesc->conn = ctx;
253 se_fl_set(sedesc, SE_FL_T_MUX);
254 se_fl_clr(sedesc, SE_FL_DETACHED);
Christopher Faulet93882042022-01-19 14:56:50 +0100255 if (!conn->ctx)
256 conn->ctx = cs;
257 if (cs_strm(cs)) {
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200258 if (!cs->wait_event.tasklet) {
259 cs->wait_event.tasklet = tasklet_new();
260 if (!cs->wait_event.tasklet)
261 return -1;
Christopher Faulet4a7764a2022-04-01 16:58:52 +0200262 cs->wait_event.tasklet->process = cs_conn_io_cb;
263 cs->wait_event.tasklet->context = cs;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200264 cs->wait_event.events = 0;
265 }
266
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200267 cs->ops = &sc_app_conn_ops;
Christopher Faulet000ba3e2022-04-01 17:06:32 +0200268 cs->data_cb = &cs_data_conn_cb;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100269 }
Christopher Fauletc95eaef2022-05-18 15:57:15 +0200270 else if (cs_check(cs)) {
271 if (!cs->wait_event.tasklet) {
272 cs->wait_event.tasklet = tasklet_new();
273 if (!cs->wait_event.tasklet)
274 return -1;
275 cs->wait_event.tasklet->process = srv_chk_io_cb;
276 cs->wait_event.tasklet->context = cs;
277 cs->wait_event.events = 0;
278 }
279
Christopher Faulet93882042022-01-19 14:56:50 +0100280 cs->data_cb = &check_conn_cb;
Christopher Fauletc95eaef2022-05-18 15:57:15 +0200281 }
Christopher Faulet070b91b2022-03-31 19:27:18 +0200282 return 0;
Christopher Faulet93882042022-01-19 14:56:50 +0100283}
284
Christopher Faulet9ed77422022-04-12 08:51:15 +0200285/* Attaches a conn_stream to an applet endpoint and sets the endpoint
Willy Tarreaub605c422022-05-17 17:04:55 +0200286 * ctx. Returns -1 on error and 0 on sucess. SE_FL_DETACHED flag is
Christopher Faulet9ed77422022-04-12 08:51:15 +0200287 * removed. This function is called by a stream when a backend applet is
288 * registered.
289 */
Willy Tarreau65d05972022-05-16 17:29:42 +0200290static void cs_attach_applet(struct conn_stream *cs, void *endp)
Christopher Faulet93882042022-01-19 14:56:50 +0100291{
Willy Tarreau798465b2022-05-17 18:20:02 +0200292 cs->sedesc->se = endp;
Willy Tarreaub605c422022-05-17 17:04:55 +0200293 sc_ep_set(cs, SE_FL_T_APPLET);
294 sc_ep_clr(cs, SE_FL_DETACHED);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100295 if (cs_strm(cs)) {
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200296 cs->ops = &sc_app_applet_ops;
Christopher Faulet6059ba42022-04-01 16:34:53 +0200297 cs->data_cb = &cs_data_applet_cb;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100298 }
299}
300
Christopher Faulet9ed77422022-04-12 08:51:15 +0200301/* Attaches a conn_stream to a app layer and sets the relevant
Willy Tarreaub605c422022-05-17 17:04:55 +0200302 * callbacks. Returns -1 on error and 0 on success. SE_FL_ORPHAN flag is
Christopher Faulet9ed77422022-04-12 08:51:15 +0200303 * removed. This function is called by a stream when it is created to attach it
304 * on the conn-stream on the client side.
305 */
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100306int cs_attach_strm(struct conn_stream *cs, struct stream *strm)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100307{
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100308 cs->app = &strm->obj_type;
Willy Tarreaub605c422022-05-17 17:04:55 +0200309 sc_ep_clr(cs, SE_FL_ORPHAN);
310 if (sc_ep_test(cs, SE_FL_T_MUX)) {
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200311 cs->wait_event.tasklet = tasklet_new();
Christopher Faulet582a2262022-04-04 11:25:59 +0200312 if (!cs->wait_event.tasklet)
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200313 return -1;
Christopher Faulet4a7764a2022-04-01 16:58:52 +0200314 cs->wait_event.tasklet->process = cs_conn_io_cb;
315 cs->wait_event.tasklet->context = cs;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200316 cs->wait_event.events = 0;
317
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200318 cs->ops = &sc_app_conn_ops;
Christopher Faulet000ba3e2022-04-01 17:06:32 +0200319 cs->data_cb = &cs_data_conn_cb;
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 Tarreau3a3f4802022-05-17 18:28:19 +0200322 cs->ops = &sc_app_applet_ops;
Christopher Faulet6059ba42022-04-01 16:34:53 +0200323 cs->data_cb = &cs_data_applet_cb;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100324 }
325 else {
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200326 cs->ops = &sc_app_embedded_ops;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100327 cs->data_cb = NULL;
328 }
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100329 return 0;
330}
331
Christopher Faulet9ed77422022-04-12 08:51:15 +0200332/* Detaches the conn_stream from the endpoint, if any. For a connecrion, if a
333 * mux owns the connection ->detach() callback is called. Otherwise, it means
334 * the conn-stream owns the connection. In this case the connection is closed
335 * and released. For an applet, the appctx is released. If still allocated, the
336 * endpoint is reset and flag as detached. If the app layer is also detached,
337 * the conn-stream is released.
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100338 */
Christopher Fauleteb50c012022-04-21 14:22:53 +0200339static void cs_detach_endp(struct conn_stream **csp)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100340{
Christopher Fauleteb50c012022-04-21 14:22:53 +0200341 struct conn_stream *cs = *csp;
342
343 if (!cs)
344 return;
345
Willy Tarreau798465b2022-05-17 18:20:02 +0200346 if (!cs->sedesc)
Christopher Fauletb041b232022-03-24 10:27:02 +0100347 goto reset_cs;
348
Willy Tarreaub605c422022-05-17 17:04:55 +0200349 if (sc_ep_test(cs, SE_FL_T_MUX)) {
Christopher Fauletcea05432022-04-14 11:40:12 +0200350 struct connection *conn = __cs_conn(cs);
Willy Tarreau798465b2022-05-17 18:20:02 +0200351 struct sedesc *sedesc = cs->sedesc;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100352
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100353 if (conn->mux) {
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200354 if (cs->wait_event.events != 0)
355 conn->mux->unsubscribe(cs, cs->wait_event.events, &cs->wait_event);
Willy Tarreau798465b2022-05-17 18:20:02 +0200356 se_fl_set(sedesc, SE_FL_ORPHAN);
357 sedesc->cs = NULL;
358 cs->sedesc = NULL;
359 conn->mux->detach(sedesc);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100360 }
361 else {
362 /* It's too early to have a mux, let's just destroy
363 * the connection
364 */
365 conn_stop_tracking(conn);
366 conn_full_close(conn);
367 if (conn->destroy_cb)
368 conn->destroy_cb(conn);
369 conn_free(conn);
370 }
371 }
Willy Tarreaub605c422022-05-17 17:04:55 +0200372 else if (sc_ep_test(cs, SE_FL_T_APPLET)) {
Christopher Fauletcea05432022-04-14 11:40:12 +0200373 struct appctx *appctx = __cs_appctx(cs);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100374
Willy Tarreaub605c422022-05-17 17:04:55 +0200375 sc_ep_set(cs, SE_FL_ORPHAN);
Willy Tarreau798465b2022-05-17 18:20:02 +0200376 cs->sedesc->cs = NULL;
377 cs->sedesc = NULL;
Willy Tarreau1c3ead42022-05-10 19:42:22 +0200378 appctx_shut(appctx);
379 appctx_free(appctx);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100380 }
381
Willy Tarreau798465b2022-05-17 18:20:02 +0200382 if (cs->sedesc) {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100383 /* the cs is the only one one the endpoint */
Willy Tarreau798465b2022-05-17 18:20:02 +0200384 cs->sedesc->se = NULL;
385 cs->sedesc->conn = NULL;
Willy Tarreaub605c422022-05-17 17:04:55 +0200386 sc_ep_clr(cs, ~SE_FL_APP_MASK);
387 sc_ep_set(cs, SE_FL_DETACHED);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100388 }
389
Christopher Fauletb041b232022-03-24 10:27:02 +0100390 reset_cs:
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100391 /* FIXME: Rest CS for now but must be reviewed. CS flags are only
392 * connection related for now but this will evolved
393 */
Christopher Faulet30995112022-03-25 15:32:38 +0100394 cs->flags &= CS_FL_ISBACK;
Christopher Faulet582a2262022-04-04 11:25:59 +0200395 if (cs_strm(cs))
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200396 cs->ops = &sc_app_embedded_ops;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100397 cs->data_cb = NULL;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200398 cs_free_cond(csp);
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100399}
400
Christopher Faulet9ed77422022-04-12 08:51:15 +0200401/* Detaches the conn_stream from the app layer. If there is no endpoint attached
402 * to the conn_stream
403 */
Christopher Fauleteb50c012022-04-21 14:22:53 +0200404static void cs_detach_app(struct conn_stream **csp)
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100405{
Christopher Fauleteb50c012022-04-21 14:22:53 +0200406 struct conn_stream *cs = *csp;
407
408 if (!cs)
409 return;
410
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100411 cs->app = NULL;
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100412 cs->data_cb = NULL;
Christopher Faulet8da67aa2022-03-29 17:53:09 +0200413 sockaddr_free(&cs->src);
414 sockaddr_free(&cs->dst);
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200415
416 if (cs->wait_event.tasklet)
417 tasklet_free(cs->wait_event.tasklet);
418 cs->wait_event.tasklet = NULL;
419 cs->wait_event.events = 0;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200420 cs_free_cond(csp);
421}
422
423/* Destroy the conn_stream. It is detached from its endpoint and its
424 * application. After this call, the conn_stream must be considered as released.
425 */
426void cs_destroy(struct conn_stream *cs)
427{
428 cs_detach_endp(&cs);
429 cs_detach_app(&cs);
430 BUG_ON_HOT(cs);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100431}
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100432
Christopher Faulet9ed77422022-04-12 08:51:15 +0200433/* Resets the conn-stream endpoint. It happens when the app layer want to renew
434 * its endpoint. For a connection retry for instance. If a mux or an applet is
435 * attached, a new endpoint is created. Returns -1 on error and 0 on sucess.
Christopher Fauleta6c4a482022-04-28 18:25:24 +0200436 *
Willy Tarreaub605c422022-05-17 17:04:55 +0200437 * Only SE_FL_ERROR flag is removed on the endpoint. Orther flags are preserved.
Christopher Fauleta6c4a482022-04-28 18:25:24 +0200438 * It is the caller responsibility to remove other flags if needed.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200439 */
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100440int cs_reset_endp(struct conn_stream *cs)
441{
Willy Tarreauea59b022022-05-17 17:53:22 +0200442 struct sedesc *new_endp;
Christopher Fauletb041b232022-03-24 10:27:02 +0100443
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100444 BUG_ON(!cs->app);
Christopher Fauleta6c4a482022-04-28 18:25:24 +0200445
Willy Tarreaub605c422022-05-17 17:04:55 +0200446 sc_ep_clr(cs, SE_FL_ERROR);
Christopher Fauletb041b232022-03-24 10:27:02 +0100447 if (!__cs_endp_target(cs)) {
448 /* endpoint not attached or attached to a mux with no
449 * target. Thus the endpoint will not be release but just
Christopher Fauleteb50c012022-04-21 14:22:53 +0200450 * reset. The app is still attached, the cs will not be
451 * released.
Christopher Fauletb041b232022-03-24 10:27:02 +0100452 */
Christopher Fauleteb50c012022-04-21 14:22:53 +0200453 cs_detach_endp(&cs);
Christopher Fauletb041b232022-03-24 10:27:02 +0100454 return 0;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100455 }
Christopher Fauletb041b232022-03-24 10:27:02 +0100456
457 /* allocate the new endpoint first to be able to set error if it
458 * fails */
Willy Tarreauea59b022022-05-17 17:53:22 +0200459 new_endp = sedesc_new();
Christopher Fauletb041b232022-03-24 10:27:02 +0100460 if (!unlikely(new_endp)) {
Willy Tarreaub605c422022-05-17 17:04:55 +0200461 sc_ep_set(cs, SE_FL_ERROR);
Christopher Fauletb041b232022-03-24 10:27:02 +0100462 return -1;
463 }
Willy Tarreaub605c422022-05-17 17:04:55 +0200464 se_fl_setall(new_endp, sc_ep_get(cs) & SE_FL_APP_MASK);
Christopher Fauletb041b232022-03-24 10:27:02 +0100465
Christopher Fauleteb50c012022-04-21 14:22:53 +0200466 /* The app is still attached, the cs will not be released */
467 cs_detach_endp(&cs);
Willy Tarreau798465b2022-05-17 18:20:02 +0200468 BUG_ON(cs->sedesc);
469 cs->sedesc = new_endp;
470 cs->sedesc->cs = cs;
Willy Tarreaub605c422022-05-17 17:04:55 +0200471 sc_ep_set(cs, SE_FL_DETACHED);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100472 return 0;
473}
Christopher Faulet37046632022-04-01 11:36:58 +0200474
475
Christopher Faulet1336ccf2022-04-12 18:15:16 +0200476/* Create an applet to handle a conn-stream as a new appctx. The CS will
Christopher Faulet37046632022-04-01 11:36:58 +0200477 * wake it up every time it is solicited. The appctx must be deleted by the task
478 * handler using cs_detach_endp(), possibly from within the function itself.
479 * It also pre-initializes the applet's context and returns it (or NULL in case
480 * it could not be allocated).
481 */
Christopher Faulet1336ccf2022-04-12 18:15:16 +0200482struct appctx *cs_applet_create(struct conn_stream *cs, struct applet *app)
Christopher Faulet37046632022-04-01 11:36:58 +0200483{
484 struct appctx *appctx;
485
486 DPRINTF(stderr, "registering handler %p for cs %p (was %p)\n", app, cs, cs_strm_task(cs));
487
Willy Tarreau798465b2022-05-17 18:20:02 +0200488 appctx = appctx_new_here(app, cs->sedesc);
Christopher Faulet37046632022-04-01 11:36:58 +0200489 if (!appctx)
490 return NULL;
Christopher Faulet2d9cc852022-05-16 17:29:37 +0200491 cs_attach_applet(cs, appctx);
Christopher Faulet37046632022-04-01 11:36:58 +0200492 appctx->t->nice = __cs_strm(cs)->task->nice;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200493 cs_cant_get(cs);
Christopher Faulet37046632022-04-01 11:36:58 +0200494 appctx_wakeup(appctx);
Christopher Fauleta33ff7a2022-04-21 11:52:07 +0200495
496 cs->state = CS_ST_RDY;
Christopher Faulet37046632022-04-01 11:36:58 +0200497 return appctx;
498}
499
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200500/*
501 * This function performs a shutdown-read on a detached conn-stream in a
502 * connected or init state (it does nothing for other states). It either shuts
503 * the read side or marks itself as closed. The buffer flags are updated to
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +0200504 * reflect the new state. If the conn-stream has CS_FL_NOHALF, we also
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200505 * forward the close to the write side. The owner task is woken up if it exists.
506 */
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200507static void sc_app_shutr(struct conn_stream *cs)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200508{
509 struct channel *ic = cs_ic(cs);
510
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200511 cs_rx_shut_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200512 if (ic->flags & CF_SHUTR)
513 return;
514 ic->flags |= CF_SHUTR;
515 ic->rex = TICK_ETERNITY;
516
517 if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
518 return;
519
520 if (cs_oc(cs)->flags & CF_SHUTW) {
521 cs->state = CS_ST_DIS;
522 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
523 }
524 else if (cs->flags & CS_FL_NOHALF) {
525 /* we want to immediately forward this close to the write side */
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200526 return sc_app_shutw(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200527 }
528
529 /* note that if the task exists, it must unregister itself once it runs */
530 if (!(cs->flags & CS_FL_DONT_WAKE))
531 task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO);
532}
533
534/*
535 * This function performs a shutdown-write on a detached conn-stream in a
536 * connected or init state (it does nothing for other states). It either shuts
537 * the write side or marks itself as closed. The buffer flags are updated to
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +0200538 * reflect the new state. It does also close everything if the CS was marked as
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200539 * being in error state. The owner task is woken up if it exists.
540 */
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200541static void sc_app_shutw(struct conn_stream *cs)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200542{
543 struct channel *ic = cs_ic(cs);
544 struct channel *oc = cs_oc(cs);
545
546 oc->flags &= ~CF_SHUTW_NOW;
547 if (oc->flags & CF_SHUTW)
548 return;
549 oc->flags |= CF_SHUTW;
550 oc->wex = TICK_ETERNITY;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200551 cs_done_get(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200552
553 if (tick_isset(cs->hcto)) {
554 ic->rto = cs->hcto;
555 ic->rex = tick_add(now_ms, ic->rto);
556 }
557
558 switch (cs->state) {
559 case CS_ST_RDY:
560 case CS_ST_EST:
561 /* we have to shut before closing, otherwise some short messages
562 * may never leave the system, especially when there are remaining
563 * unread data in the socket input buffer, or when nolinger is set.
564 * However, if CS_FL_NOLINGER is explicitly set, we know there is
565 * no risk so we close both sides immediately.
566 */
Willy Tarreaub605c422022-05-17 17:04:55 +0200567 if (!sc_ep_test(cs, SE_FL_ERROR) && !(cs->flags & CS_FL_NOLINGER) &&
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200568 !(ic->flags & (CF_SHUTR|CF_DONT_READ)))
569 return;
570
571 /* fall through */
572 case CS_ST_CON:
573 case CS_ST_CER:
574 case CS_ST_QUE:
575 case CS_ST_TAR:
576 /* Note that none of these states may happen with applets */
577 cs->state = CS_ST_DIS;
578 /* fall through */
579 default:
580 cs->flags &= ~CS_FL_NOLINGER;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200581 cs_rx_shut_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200582 ic->flags |= CF_SHUTR;
583 ic->rex = TICK_ETERNITY;
584 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
585 }
586
587 /* note that if the task exists, it must unregister itself once it runs */
588 if (!(cs->flags & CS_FL_DONT_WAKE))
589 task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO);
590}
591
592/* default chk_rcv function for scheduled tasks */
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200593static void sc_app_chk_rcv(struct conn_stream *cs)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200594{
595 struct channel *ic = cs_ic(cs);
596
597 DPRINTF(stderr, "%s: cs=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n",
598 __FUNCTION__,
599 cs, cs->state, ic->flags, cs_oc(cs)->flags);
600
601 if (ic->pipe) {
602 /* stop reading */
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200603 cs_rx_room_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200604 }
605 else {
606 /* (re)start reading */
607 if (!(cs->flags & CS_FL_DONT_WAKE))
608 task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO);
609 }
610}
611
612/* default chk_snd function for scheduled tasks */
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200613static void sc_app_chk_snd(struct conn_stream *cs)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200614{
615 struct channel *oc = cs_oc(cs);
616
617 DPRINTF(stderr, "%s: cs=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n",
618 __FUNCTION__,
619 cs, cs->state, cs_ic(cs)->flags, oc->flags);
620
621 if (unlikely(cs->state != CS_ST_EST || (oc->flags & CF_SHUTW)))
622 return;
623
Willy Tarreaub605c422022-05-17 17:04:55 +0200624 if (!sc_ep_test(cs, SE_FL_WAIT_DATA) || /* not waiting for data */
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200625 channel_is_empty(oc)) /* called with nothing to send ! */
626 return;
627
628 /* Otherwise there are remaining data to be sent in the buffer,
629 * so we tell the handler.
630 */
Willy Tarreaub605c422022-05-17 17:04:55 +0200631 sc_ep_clr(cs, SE_FL_WAIT_DATA);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200632 if (!tick_isset(oc->wex))
633 oc->wex = tick_add_ifset(now_ms, oc->wto);
634
635 if (!(cs->flags & CS_FL_DONT_WAKE))
636 task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO);
637}
638
639/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200640 * This function performs a shutdown-read on a stream connector attached to
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200641 * a connection in a connected or init state (it does nothing for other
642 * states). It either shuts the read side or marks itself as closed. The buffer
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200643 * flags are updated to reflect the new state. If the stream connector has
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200644 * CS_FL_NOHALF, we also forward the close to the write side. If a control
645 * layer is defined, then it is supposed to be a socket layer and file
646 * descriptors are then shutdown or closed accordingly. The function
647 * automatically disables polling if needed.
648 */
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200649static void sc_app_shutr_conn(struct conn_stream *cs)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200650{
651 struct channel *ic = cs_ic(cs);
652
653 BUG_ON(!cs_conn(cs));
654
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200655 cs_rx_shut_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200656 if (ic->flags & CF_SHUTR)
657 return;
658 ic->flags |= CF_SHUTR;
659 ic->rex = TICK_ETERNITY;
660
661 if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
662 return;
663
664 if (cs_oc(cs)->flags & CF_SHUTW) {
Christopher Fauletff022a22022-04-21 08:38:54 +0200665 cs_conn_shut(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200666 cs->state = CS_ST_DIS;
667 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
668 }
669 else if (cs->flags & CS_FL_NOHALF) {
670 /* we want to immediately forward this close to the write side */
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200671 return sc_app_shutw_conn(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200672 }
673}
674
675/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200676 * This function performs a shutdown-write on a stream connector attached to
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200677 * a connection in a connected or init state (it does nothing for other
678 * states). It either shuts the write side or marks itself as closed. The
679 * buffer flags are updated to reflect the new state. It does also close
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +0200680 * everything if the CS was marked as being in error state. If there is a
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200681 * data-layer shutdown, it is called.
682 */
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200683static void sc_app_shutw_conn(struct conn_stream *cs)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200684{
685 struct channel *ic = cs_ic(cs);
686 struct channel *oc = cs_oc(cs);
687
688 BUG_ON(!cs_conn(cs));
689
690 oc->flags &= ~CF_SHUTW_NOW;
691 if (oc->flags & CF_SHUTW)
692 return;
693 oc->flags |= CF_SHUTW;
694 oc->wex = TICK_ETERNITY;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200695 cs_done_get(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200696
697 if (tick_isset(cs->hcto)) {
698 ic->rto = cs->hcto;
699 ic->rex = tick_add(now_ms, ic->rto);
700 }
701
702 switch (cs->state) {
703 case CS_ST_RDY:
704 case CS_ST_EST:
705 /* we have to shut before closing, otherwise some short messages
706 * may never leave the system, especially when there are remaining
707 * unread data in the socket input buffer, or when nolinger is set.
708 * However, if CS_FL_NOLINGER is explicitly set, we know there is
709 * no risk so we close both sides immediately.
710 */
711
Willy Tarreaub605c422022-05-17 17:04:55 +0200712 if (sc_ep_test(cs, SE_FL_ERROR)) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200713 /* quick close, the socket is already shut anyway */
714 }
715 else if (cs->flags & CS_FL_NOLINGER) {
716 /* unclean data-layer shutdown, typically an aborted request
717 * or a forwarded shutdown from a client to a server due to
718 * option abortonclose. No need for the TLS layer to try to
719 * emit a shutdown message.
720 */
721 cs_conn_shutw(cs, CO_SHW_SILENT);
722 }
723 else {
724 /* clean data-layer shutdown. This only happens on the
725 * frontend side, or on the backend side when forwarding
726 * a client close in TCP mode or in HTTP TUNNEL mode
727 * while option abortonclose is set. We want the TLS
728 * layer to try to signal it to the peer before we close.
729 */
730 cs_conn_shutw(cs, CO_SHW_NORMAL);
731
732 if (!(ic->flags & (CF_SHUTR|CF_DONT_READ)))
733 return;
734 }
735
736 /* fall through */
737 case CS_ST_CON:
738 /* we may have to close a pending connection, and mark the
739 * response buffer as shutr
740 */
Christopher Fauletff022a22022-04-21 08:38:54 +0200741 cs_conn_shut(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200742 /* fall through */
743 case CS_ST_CER:
744 case CS_ST_QUE:
745 case CS_ST_TAR:
746 cs->state = CS_ST_DIS;
747 /* fall through */
748 default:
749 cs->flags &= ~CS_FL_NOLINGER;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200750 cs_rx_shut_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200751 ic->flags |= CF_SHUTR;
752 ic->rex = TICK_ETERNITY;
753 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
754 }
755}
756
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200757/* This function is used for inter-stream connector calls. It is called by the
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200758 * consumer to inform the producer side that it may be interested in checking
759 * for free space in the buffer. Note that it intentionally does not update
760 * timeouts, so that we can still check them later at wake-up. This function is
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200761 * dedicated to connection-based stream connectors.
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200762 */
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200763static void sc_app_chk_rcv_conn(struct conn_stream *cs)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200764{
765 BUG_ON(!cs_conn(cs));
766
767 /* (re)start reading */
768 if (cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
769 tasklet_wakeup(cs->wait_event.tasklet);
770}
771
772
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200773/* This function is used for inter-stream connector calls. It is called by the
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200774 * producer to inform the consumer side that it may be interested in checking
775 * for data in the buffer. Note that it intentionally does not update timeouts,
776 * so that we can still check them later at wake-up.
777 */
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200778static void sc_app_chk_snd_conn(struct conn_stream *cs)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200779{
780 struct channel *oc = cs_oc(cs);
781
782 BUG_ON(!cs_conn(cs));
783
Willy Tarreau4173f4e2022-04-29 15:04:41 +0200784 if (unlikely(!cs_state_in(cs->state, CS_SB_RDY|CS_SB_EST) ||
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200785 (oc->flags & CF_SHUTW)))
786 return;
787
788 if (unlikely(channel_is_empty(oc))) /* called with nothing to send ! */
789 return;
790
791 if (!oc->pipe && /* spliced data wants to be forwarded ASAP */
Willy Tarreaub605c422022-05-17 17:04:55 +0200792 !sc_ep_test(cs, SE_FL_WAIT_DATA)) /* not waiting for data */
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200793 return;
794
795 if (!(cs->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(cs_oc(cs)))
Christopher Faulet000ba3e2022-04-01 17:06:32 +0200796 cs_conn_send(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200797
Willy Tarreaub605c422022-05-17 17:04:55 +0200798 if (sc_ep_test(cs, SE_FL_ERROR | SE_FL_ERR_PENDING) || cs_is_conn_error(cs)) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200799 /* Write error on the file descriptor */
800 if (cs->state >= CS_ST_CON)
Willy Tarreaub605c422022-05-17 17:04:55 +0200801 sc_ep_set(cs, SE_FL_ERROR);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200802 goto out_wakeup;
803 }
804
805 /* OK, so now we know that some data might have been sent, and that we may
806 * have to poll first. We have to do that too if the buffer is not empty.
807 */
808 if (channel_is_empty(oc)) {
809 /* the connection is established but we can't write. Either the
810 * buffer is empty, or we just refrain from sending because the
811 * ->o limit was reached. Maybe we just wrote the last
812 * chunk and need to close.
813 */
814 if (((oc->flags & (CF_SHUTW|CF_AUTO_CLOSE|CF_SHUTW_NOW)) ==
815 (CF_AUTO_CLOSE|CF_SHUTW_NOW)) &&
816 cs_state_in(cs->state, CS_SB_RDY|CS_SB_EST)) {
817 cs_shutw(cs);
818 goto out_wakeup;
819 }
820
821 if ((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == 0)
Willy Tarreaub605c422022-05-17 17:04:55 +0200822 sc_ep_set(cs, SE_FL_WAIT_DATA);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200823 oc->wex = TICK_ETERNITY;
824 }
825 else {
826 /* Otherwise there are remaining data to be sent in the buffer,
827 * which means we have to poll before doing so.
828 */
Willy Tarreaub605c422022-05-17 17:04:55 +0200829 sc_ep_clr(cs, SE_FL_WAIT_DATA);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200830 if (!tick_isset(oc->wex))
831 oc->wex = tick_add_ifset(now_ms, oc->wto);
832 }
833
834 if (likely(oc->flags & CF_WRITE_ACTIVITY)) {
835 struct channel *ic = cs_ic(cs);
836
837 /* update timeout if we have written something */
838 if ((oc->flags & (CF_SHUTW|CF_WRITE_PARTIAL)) == CF_WRITE_PARTIAL &&
839 !channel_is_empty(oc))
840 oc->wex = tick_add_ifset(now_ms, oc->wto);
841
842 if (tick_isset(ic->rex) && !(cs->flags & CS_FL_INDEP_STR)) {
843 /* Note: to prevent the client from expiring read timeouts
844 * during writes, we refresh it. We only do this if the
845 * interface is not configured for "independent streams",
846 * because for some applications it's better not to do this,
847 * for instance when continuously exchanging small amounts
848 * of data which can full the socket buffers long before a
849 * write timeout is detected.
850 */
851 ic->rex = tick_add_ifset(now_ms, ic->rto);
852 }
853 }
854
855 /* in case of special condition (error, shutdown, end of write...), we
856 * have to notify the task.
857 */
858 if (likely((oc->flags & (CF_WRITE_NULL|CF_WRITE_ERROR|CF_SHUTW)) ||
859 ((oc->flags & CF_WAKE_WRITE) &&
860 ((channel_is_empty(oc) && !oc->to_forward) ||
861 !cs_state_in(cs->state, CS_SB_EST))))) {
862 out_wakeup:
863 if (!(cs->flags & CS_FL_DONT_WAKE))
864 task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO);
865 }
866}
867
868/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200869 * This function performs a shutdown-read on a stream connector attached to an
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200870 * applet in a connected or init state (it does nothing for other states). It
871 * either shuts the read side or marks itself as closed. The buffer flags are
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200872 * updated to reflect the new state. If the stream connector has CS_FL_NOHALF,
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200873 * we also forward the close to the write side. The owner task is woken up if
874 * it exists.
875 */
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200876static void sc_app_shutr_applet(struct conn_stream *cs)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200877{
878 struct channel *ic = cs_ic(cs);
879
880 BUG_ON(!cs_appctx(cs));
881
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200882 cs_rx_shut_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200883 if (ic->flags & CF_SHUTR)
884 return;
885 ic->flags |= CF_SHUTR;
886 ic->rex = TICK_ETERNITY;
887
888 /* Note: on shutr, we don't call the applet */
889
890 if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
891 return;
892
893 if (cs_oc(cs)->flags & CF_SHUTW) {
Willy Tarreau1c3ead42022-05-10 19:42:22 +0200894 appctx_shut(__cs_appctx(cs));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200895 cs->state = CS_ST_DIS;
896 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
897 }
898 else if (cs->flags & CS_FL_NOHALF) {
899 /* we want to immediately forward this close to the write side */
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200900 return sc_app_shutw_applet(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200901 }
902}
903
904/*
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200905 * This function performs a shutdown-write on a stream connector attached to an
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200906 * applet in a connected or init state (it does nothing for other states). It
907 * either shuts the write side or marks itself as closed. The buffer flags are
908 * updated to reflect the new state. It does also close everything if the SI
909 * was marked as being in error state. The owner task is woken up if it exists.
910 */
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200911static void sc_app_shutw_applet(struct conn_stream *cs)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200912{
913 struct channel *ic = cs_ic(cs);
914 struct channel *oc = cs_oc(cs);
915
916 BUG_ON(!cs_appctx(cs));
917
918 oc->flags &= ~CF_SHUTW_NOW;
919 if (oc->flags & CF_SHUTW)
920 return;
921 oc->flags |= CF_SHUTW;
922 oc->wex = TICK_ETERNITY;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200923 cs_done_get(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200924
925 if (tick_isset(cs->hcto)) {
926 ic->rto = cs->hcto;
927 ic->rex = tick_add(now_ms, ic->rto);
928 }
929
930 /* on shutw we always wake the applet up */
931 appctx_wakeup(__cs_appctx(cs));
932
933 switch (cs->state) {
934 case CS_ST_RDY:
935 case CS_ST_EST:
936 /* we have to shut before closing, otherwise some short messages
937 * may never leave the system, especially when there are remaining
938 * unread data in the socket input buffer, or when nolinger is set.
939 * However, if CS_FL_NOLINGER is explicitly set, we know there is
940 * no risk so we close both sides immediately.
941 */
Willy Tarreaub605c422022-05-17 17:04:55 +0200942 if (!sc_ep_test(cs, SE_FL_ERROR) && !(cs->flags & CS_FL_NOLINGER) &&
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200943 !(ic->flags & (CF_SHUTR|CF_DONT_READ)))
944 return;
945
946 /* fall through */
947 case CS_ST_CON:
948 case CS_ST_CER:
949 case CS_ST_QUE:
950 case CS_ST_TAR:
951 /* Note that none of these states may happen with applets */
Willy Tarreau1c3ead42022-05-10 19:42:22 +0200952 appctx_shut(__cs_appctx(cs));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200953 cs->state = CS_ST_DIS;
954 /* fall through */
955 default:
956 cs->flags &= ~CS_FL_NOLINGER;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200957 cs_rx_shut_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200958 ic->flags |= CF_SHUTR;
959 ic->rex = TICK_ETERNITY;
960 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
961 }
962}
963
964/* chk_rcv function for applets */
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200965static void sc_app_chk_rcv_applet(struct conn_stream *cs)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200966{
967 struct channel *ic = cs_ic(cs);
968
969 BUG_ON(!cs_appctx(cs));
970
971 DPRINTF(stderr, "%s: cs=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n",
972 __FUNCTION__,
973 cs, cs->state, ic->flags, cs_oc(cs)->flags);
974
975 if (!ic->pipe) {
976 /* (re)start reading */
977 appctx_wakeup(__cs_appctx(cs));
978 }
979}
980
981/* chk_snd function for applets */
Willy Tarreau3a3f4802022-05-17 18:28:19 +0200982static void sc_app_chk_snd_applet(struct conn_stream *cs)
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200983{
984 struct channel *oc = cs_oc(cs);
985
986 BUG_ON(!cs_appctx(cs));
987
988 DPRINTF(stderr, "%s: cs=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n",
989 __FUNCTION__,
990 cs, cs->state, cs_ic(cs)->flags, oc->flags);
991
992 if (unlikely(cs->state != CS_ST_EST || (oc->flags & CF_SHUTW)))
993 return;
994
995 /* we only wake the applet up if it was waiting for some data */
996
Willy Tarreaub605c422022-05-17 17:04:55 +0200997 if (!sc_ep_test(cs, SE_FL_WAIT_DATA))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200998 return;
999
1000 if (!tick_isset(oc->wex))
1001 oc->wex = tick_add_ifset(now_ms, oc->wto);
1002
1003 if (!channel_is_empty(oc)) {
1004 /* (re)start sending */
1005 appctx_wakeup(__cs_appctx(cs));
1006 }
1007}
Christopher Faulet13045f02022-04-01 14:23:38 +02001008
1009
1010/* This function is designed to be called from within the stream handler to
1011 * update the input channel's expiration timer and the conn-stream's
1012 * Rx flags based on the channel's flags. It needs to be called only once
1013 * after the channel's flags have settled down, and before they are cleared,
1014 * though it doesn't harm to call it as often as desired (it just slightly
1015 * hurts performance). It must not be called from outside of the stream
1016 * handler, as what it does will be used to compute the stream task's
1017 * expiration.
1018 */
1019void cs_update_rx(struct conn_stream *cs)
1020{
1021 struct channel *ic = cs_ic(cs);
1022
1023 if (ic->flags & CF_SHUTR) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001024 cs_rx_shut_blk(cs);
Christopher Faulet13045f02022-04-01 14:23:38 +02001025 return;
1026 }
1027
1028 /* Read not closed, update FD status and timeout for reads */
1029 if (ic->flags & CF_DONT_READ)
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001030 cs_rx_chan_blk(cs);
Christopher Faulet13045f02022-04-01 14:23:38 +02001031 else
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001032 cs_rx_chan_rdy(cs);
Christopher Faulet13045f02022-04-01 14:23:38 +02001033
1034 if (!channel_is_empty(ic) || !channel_may_recv(ic)) {
1035 /* stop reading, imposed by channel's policy or contents */
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001036 cs_rx_room_blk(cs);
Christopher Faulet13045f02022-04-01 14:23:38 +02001037 }
1038 else {
1039 /* (re)start reading and update timeout. Note: we don't recompute the timeout
1040 * every time we get here, otherwise it would risk never to expire. We only
1041 * update it if is was not yet set. The stream socket handler will already
1042 * have updated it if there has been a completed I/O.
1043 */
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001044 cs_rx_room_rdy(cs);
Christopher Faulet13045f02022-04-01 14:23:38 +02001045 }
Willy Tarreaub605c422022-05-17 17:04:55 +02001046 if (sc_ep_test(cs, SE_FL_RXBLK_ANY))
Christopher Faulet13045f02022-04-01 14:23:38 +02001047 ic->rex = TICK_ETERNITY;
1048 else if (!(ic->flags & CF_READ_NOEXP) && !tick_isset(ic->rex))
1049 ic->rex = tick_add_ifset(now_ms, ic->rto);
1050
1051 cs_chk_rcv(cs);
1052}
1053
1054/* This function is designed to be called from within the stream handler to
1055 * update the output channel's expiration timer and the conn-stream's
1056 * Tx flags based on the channel's flags. It needs to be called only once
1057 * after the channel's flags have settled down, and before they are cleared,
1058 * though it doesn't harm to call it as often as desired (it just slightly
1059 * hurts performance). It must not be called from outside of the stream
1060 * handler, as what it does will be used to compute the stream task's
1061 * expiration.
1062 */
1063void cs_update_tx(struct conn_stream *cs)
1064{
1065 struct channel *oc = cs_oc(cs);
1066 struct channel *ic = cs_ic(cs);
1067
1068 if (oc->flags & CF_SHUTW)
1069 return;
1070
1071 /* Write not closed, update FD status and timeout for writes */
1072 if (channel_is_empty(oc)) {
1073 /* stop writing */
Willy Tarreaub605c422022-05-17 17:04:55 +02001074 if (!sc_ep_test(cs, SE_FL_WAIT_DATA)) {
Christopher Faulet13045f02022-04-01 14:23:38 +02001075 if ((oc->flags & CF_SHUTW_NOW) == 0)
Willy Tarreaub605c422022-05-17 17:04:55 +02001076 sc_ep_set(cs, SE_FL_WAIT_DATA);
Christopher Faulet13045f02022-04-01 14:23:38 +02001077 oc->wex = TICK_ETERNITY;
1078 }
1079 return;
1080 }
1081
1082 /* (re)start writing and update timeout. Note: we don't recompute the timeout
1083 * every time we get here, otherwise it would risk never to expire. We only
1084 * update it if is was not yet set. The stream socket handler will already
1085 * have updated it if there has been a completed I/O.
1086 */
Willy Tarreaub605c422022-05-17 17:04:55 +02001087 sc_ep_clr(cs, SE_FL_WAIT_DATA);
Christopher Faulet13045f02022-04-01 14:23:38 +02001088 if (!tick_isset(oc->wex)) {
1089 oc->wex = tick_add_ifset(now_ms, oc->wto);
1090 if (tick_isset(ic->rex) && !(cs->flags & CS_FL_INDEP_STR)) {
1091 /* Note: depending on the protocol, we don't know if we're waiting
1092 * for incoming data or not. So in order to prevent the socket from
1093 * expiring read timeouts during writes, we refresh the read timeout,
1094 * except if it was already infinite or if we have explicitly setup
1095 * independent streams.
1096 */
1097 ic->rex = tick_add_ifset(now_ms, ic->rto);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001098 }
1099 }
1100}
1101
1102/* This function is the equivalent to cs_update() except that it's
1103 * designed to be called from outside the stream handlers, typically the lower
1104 * layers (applets, connections) after I/O completion. After updating the stream
1105 * interface and timeouts, it will try to forward what can be forwarded, then to
1106 * wake the associated task up if an important event requires special handling.
Willy Tarreaub605c422022-05-17 17:04:55 +02001107 * It may update SE_FL_WAIT_DATA and/or SE_FL_RXBLK_ROOM, that the callers are
Christopher Faulet5e29b762022-04-04 08:58:34 +02001108 * encouraged to watch to take appropriate action.
1109 * It should not be called from within the stream itself, cs_update()
1110 * is designed for this.
1111 */
1112static void cs_notify(struct conn_stream *cs)
1113{
1114 struct channel *ic = cs_ic(cs);
1115 struct channel *oc = cs_oc(cs);
1116 struct conn_stream *cso = cs_opposite(cs);
1117 struct task *task = cs_strm_task(cs);
1118
1119 /* process consumer side */
1120 if (channel_is_empty(oc)) {
1121 struct connection *conn = cs_conn(cs);
1122
1123 if (((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW) &&
1124 (cs->state == CS_ST_EST) && (!conn || !(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS))))
1125 cs_shutw(cs);
1126 oc->wex = TICK_ETERNITY;
1127 }
1128
1129 /* indicate that we may be waiting for data from the output channel or
1130 * we're about to close and can't expect more data if SHUTW_NOW is there.
1131 */
1132 if (!(oc->flags & (CF_SHUTW|CF_SHUTW_NOW)))
Willy Tarreaub605c422022-05-17 17:04:55 +02001133 sc_ep_set(cs, SE_FL_WAIT_DATA);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001134 else if ((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW)
Willy Tarreaub605c422022-05-17 17:04:55 +02001135 sc_ep_clr(cs, SE_FL_WAIT_DATA);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001136
1137 /* update OC timeouts and wake the other side up if it's waiting for room */
1138 if (oc->flags & CF_WRITE_ACTIVITY) {
1139 if ((oc->flags & (CF_SHUTW|CF_WRITE_PARTIAL)) == CF_WRITE_PARTIAL &&
1140 !channel_is_empty(oc))
1141 if (tick_isset(oc->wex))
1142 oc->wex = tick_add_ifset(now_ms, oc->wto);
1143
1144 if (!(cs->flags & CS_FL_INDEP_STR))
1145 if (tick_isset(ic->rex))
1146 ic->rex = tick_add_ifset(now_ms, ic->rto);
1147 }
1148
1149 if (oc->flags & CF_DONT_READ)
1150 cs_rx_chan_blk(cso);
1151 else
1152 cs_rx_chan_rdy(cso);
1153
1154 /* Notify the other side when we've injected data into the IC that
1155 * needs to be forwarded. We can do fast-forwarding as soon as there
1156 * are output data, but we avoid doing this if some of the data are
1157 * not yet scheduled for being forwarded, because it is very likely
1158 * that it will be done again immediately afterwards once the following
Willy Tarreaub605c422022-05-17 17:04:55 +02001159 * data are parsed (eg: HTTP chunking). We only SE_FL_RXBLK_ROOM once
Christopher Faulet5e29b762022-04-04 08:58:34 +02001160 * we've emptied *some* of the output buffer, and not just when there
1161 * is available room, because applets are often forced to stop before
1162 * the buffer is full. We must not stop based on input data alone because
1163 * an HTTP parser might need more data to complete the parsing.
1164 */
1165 if (!channel_is_empty(ic) &&
Willy Tarreaub605c422022-05-17 17:04:55 +02001166 sc_ep_test(cso, SE_FL_WAIT_DATA) &&
Christopher Faulet5e29b762022-04-04 08:58:34 +02001167 (!(ic->flags & CF_EXPECT_MORE) || c_full(ic) || ci_data(ic) == 0 || ic->pipe)) {
1168 int new_len, last_len;
1169
1170 last_len = co_data(ic);
1171 if (ic->pipe)
1172 last_len += ic->pipe->data;
1173
1174 cs_chk_snd(cso);
1175
1176 new_len = co_data(ic);
1177 if (ic->pipe)
1178 new_len += ic->pipe->data;
1179
1180 /* check if the consumer has freed some space either in the
1181 * buffer or in the pipe.
1182 */
1183 if (new_len < last_len)
1184 cs_rx_room_rdy(cs);
1185 }
1186
1187 if (!(ic->flags & CF_DONT_READ))
1188 cs_rx_chan_rdy(cs);
1189
1190 cs_chk_rcv(cs);
1191 cs_chk_rcv(cso);
1192
1193 if (cs_rx_blocked(cs)) {
1194 ic->rex = TICK_ETERNITY;
1195 }
1196 else if ((ic->flags & (CF_SHUTR|CF_READ_PARTIAL)) == CF_READ_PARTIAL) {
1197 /* we must re-enable reading if cs_chk_snd() has freed some space */
1198 if (!(ic->flags & CF_READ_NOEXP) && tick_isset(ic->rex))
1199 ic->rex = tick_add_ifset(now_ms, ic->rto);
1200 }
1201
1202 /* wake the task up only when needed */
1203 if (/* changes on the production side */
1204 (ic->flags & (CF_READ_NULL|CF_READ_ERROR)) ||
1205 !cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST) ||
Willy Tarreaub605c422022-05-17 17:04:55 +02001206 sc_ep_test(cs, SE_FL_ERROR) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001207 ((ic->flags & CF_READ_PARTIAL) &&
1208 ((ic->flags & CF_EOI) || !ic->to_forward || cso->state != CS_ST_EST)) ||
1209
1210 /* changes on the consumption side */
1211 (oc->flags & (CF_WRITE_NULL|CF_WRITE_ERROR)) ||
1212 ((oc->flags & CF_WRITE_ACTIVITY) &&
1213 ((oc->flags & CF_SHUTW) ||
1214 (((oc->flags & CF_WAKE_WRITE) ||
1215 !(oc->flags & (CF_AUTO_CLOSE|CF_SHUTW_NOW|CF_SHUTW))) &&
1216 (cso->state != CS_ST_EST ||
1217 (channel_is_empty(oc) && !oc->to_forward)))))) {
1218 task_wakeup(task, TASK_WOKEN_IO);
1219 }
1220 else {
1221 /* Update expiration date for the task and requeue it */
1222 task->expire = tick_first((tick_is_expired(task->expire, now_ms) ? 0 : task->expire),
1223 tick_first(tick_first(ic->rex, ic->wex),
1224 tick_first(oc->rex, oc->wex)));
1225
1226 task->expire = tick_first(task->expire, ic->analyse_exp);
1227 task->expire = tick_first(task->expire, oc->analyse_exp);
1228 task->expire = tick_first(task->expire, __cs_strm(cs)->conn_exp);
1229
1230 task_queue(task);
1231 }
1232 if (ic->flags & CF_READ_ACTIVITY)
1233 ic->flags &= ~CF_READ_DONTWAIT;
1234}
1235
1236/*
1237 * This function propagates a null read received on a socket-based connection.
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001238 * It updates the conn-stream. If the conn-stream has CS_FL_NOHALF,
Christopher Faulet5e29b762022-04-04 08:58:34 +02001239 * the close is also forwarded to the write side as an abort.
1240 */
1241static void cs_conn_read0(struct conn_stream *cs)
1242{
1243 struct channel *ic = cs_ic(cs);
1244 struct channel *oc = cs_oc(cs);
1245
1246 BUG_ON(!cs_conn(cs));
1247
1248 cs_rx_shut_blk(cs);
1249 if (ic->flags & CF_SHUTR)
1250 return;
1251 ic->flags |= CF_SHUTR;
1252 ic->rex = TICK_ETERNITY;
1253
1254 if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
1255 return;
1256
1257 if (oc->flags & CF_SHUTW)
1258 goto do_close;
1259
1260 if (cs->flags & CS_FL_NOHALF) {
1261 /* we want to immediately forward this close to the write side */
1262 /* force flag on ssl to keep stream in cache */
1263 cs_conn_shutw(cs, CO_SHW_SILENT);
1264 goto do_close;
1265 }
1266
1267 /* otherwise that's just a normal read shutdown */
1268 return;
1269
1270 do_close:
1271 /* OK we completely close the socket here just as if we went through cs_shut[rw]() */
Christopher Fauletff022a22022-04-21 08:38:54 +02001272 cs_conn_shut(cs);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001273
1274 oc->flags &= ~CF_SHUTW_NOW;
1275 oc->flags |= CF_SHUTW;
1276 oc->wex = TICK_ETERNITY;
1277
1278 cs_done_get(cs);
1279
1280 cs->state = CS_ST_DIS;
1281 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
1282 return;
1283}
1284
1285/*
1286 * This is the callback which is called by the connection layer to receive data
1287 * into the buffer from the connection. It iterates over the mux layer's
1288 * rcv_buf function.
1289 */
1290static int cs_conn_recv(struct conn_stream *cs)
1291{
1292 struct connection *conn = __cs_conn(cs);
1293 struct channel *ic = cs_ic(cs);
1294 int ret, max, cur_read = 0;
1295 int read_poll = MAX_READ_POLL_LOOPS;
1296 int flags = 0;
1297
1298 /* If not established yet, do nothing. */
1299 if (cs->state != CS_ST_EST)
1300 return 0;
1301
1302 /* If another call to cs_conn_recv() failed, and we subscribed to
1303 * recv events already, give up now.
1304 */
1305 if (cs->wait_event.events & SUB_RETRY_RECV)
1306 return 0;
1307
1308 /* maybe we were called immediately after an asynchronous shutr */
1309 if (ic->flags & CF_SHUTR)
1310 return 1;
1311
1312 /* we must wait because the mux is not installed yet */
1313 if (!conn->mux)
1314 return 0;
1315
1316 /* stop here if we reached the end of data */
Willy Tarreaub605c422022-05-17 17:04:55 +02001317 if (sc_ep_test(cs, SE_FL_EOS))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001318 goto end_recv;
1319
1320 /* stop immediately on errors. Note that we DON'T want to stop on
1321 * POLL_ERR, as the poller might report a write error while there
1322 * are still data available in the recv buffer. This typically
1323 * happens when we send too large a request to a backend server
1324 * which rejects it before reading it all.
1325 */
Willy Tarreaub605c422022-05-17 17:04:55 +02001326 if (!sc_ep_test(cs, SE_FL_RCV_MORE)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001327 if (!conn_xprt_ready(conn))
1328 return 0;
Willy Tarreaub605c422022-05-17 17:04:55 +02001329 if (sc_ep_test(cs, SE_FL_ERROR))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001330 goto end_recv;
1331 }
1332
1333 /* prepare to detect if the mux needs more room */
Willy Tarreaub605c422022-05-17 17:04:55 +02001334 sc_ep_clr(cs, SE_FL_WANT_ROOM);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001335
1336 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) && !co_data(ic) &&
1337 global.tune.idle_timer &&
1338 (unsigned short)(now_ms - ic->last_read) >= global.tune.idle_timer) {
1339 /* The buffer was empty and nothing was transferred for more
1340 * than one second. This was caused by a pause and not by
1341 * congestion. Reset any streaming mode to reduce latency.
1342 */
1343 ic->xfer_small = 0;
1344 ic->xfer_large = 0;
1345 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
1346 }
1347
1348 /* First, let's see if we may splice data across the channel without
1349 * using a buffer.
1350 */
Willy Tarreaub605c422022-05-17 17:04:55 +02001351 if (sc_ep_test(cs, SE_FL_MAY_SPLICE) &&
Christopher Faulet5e29b762022-04-04 08:58:34 +02001352 (ic->pipe || ic->to_forward >= MIN_SPLICE_FORWARD) &&
1353 ic->flags & CF_KERN_SPLICING) {
1354 if (c_data(ic)) {
1355 /* We're embarrassed, there are already data pending in
1356 * the buffer and we don't want to have them at two
1357 * locations at a time. Let's indicate we need some
1358 * place and ask the consumer to hurry.
1359 */
1360 flags |= CO_RFL_BUF_FLUSH;
1361 goto abort_splice;
1362 }
1363
1364 if (unlikely(ic->pipe == NULL)) {
1365 if (pipes_used >= global.maxpipes || !(ic->pipe = get_pipe())) {
1366 ic->flags &= ~CF_KERN_SPLICING;
1367 goto abort_splice;
1368 }
1369 }
1370
1371 ret = conn->mux->rcv_pipe(cs, ic->pipe, ic->to_forward);
1372 if (ret < 0) {
1373 /* splice not supported on this end, let's disable it */
1374 ic->flags &= ~CF_KERN_SPLICING;
1375 goto abort_splice;
1376 }
1377
1378 if (ret > 0) {
1379 if (ic->to_forward != CHN_INFINITE_FORWARD)
1380 ic->to_forward -= ret;
1381 ic->total += ret;
1382 cur_read += ret;
1383 ic->flags |= CF_READ_PARTIAL;
1384 }
1385
Willy Tarreaub605c422022-05-17 17:04:55 +02001386 if (sc_ep_test(cs, SE_FL_EOS | SE_FL_ERROR))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001387 goto end_recv;
1388
1389 if (conn->flags & CO_FL_WAIT_ROOM) {
1390 /* the pipe is full or we have read enough data that it
1391 * could soon be full. Let's stop before needing to poll.
1392 */
1393 cs_rx_room_blk(cs);
1394 goto done_recv;
1395 }
1396
1397 /* splice not possible (anymore), let's go on on standard copy */
1398 }
1399
1400 abort_splice:
1401 if (ic->pipe && unlikely(!ic->pipe->data)) {
1402 put_pipe(ic->pipe);
1403 ic->pipe = NULL;
1404 }
1405
Willy Tarreaub605c422022-05-17 17:04:55 +02001406 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 +02001407 /* don't break splicing by reading, but still call rcv_buf()
1408 * to pass the flag.
1409 */
1410 goto done_recv;
1411 }
1412
1413 /* now we'll need a input buffer for the stream */
1414 if (!cs_alloc_ibuf(cs, &(__cs_strm(cs)->buffer_wait)))
1415 goto end_recv;
1416
1417 /* For an HTX stream, if the buffer is stuck (no output data with some
1418 * input data) and if the HTX message is fragmented or if its free space
1419 * wraps, we force an HTX deframentation. It is a way to have a
1420 * contiguous free space nad to let the mux to copy as much data as
1421 * possible.
1422 *
1423 * NOTE: A possible optim may be to let the mux decides if defrag is
1424 * required or not, depending on amount of data to be xferred.
1425 */
1426 if (IS_HTX_STRM(__cs_strm(cs)) && !co_data(ic)) {
1427 struct htx *htx = htxbuf(&ic->buf);
1428
1429 if (htx_is_not_empty(htx) && ((htx->flags & HTX_FL_FRAGMENTED) || htx_space_wraps(htx)))
1430 htx_defrag(htx, NULL, 0);
1431 }
1432
1433 /* Instruct the mux it must subscribed for read events */
1434 flags |= ((!conn_is_back(conn) && (__cs_strm(cs)->be->options & PR_O_ABRT_CLOSE)) ? CO_RFL_KEEP_RECV : 0);
1435
1436 /* Important note : if we're called with POLL_IN|POLL_HUP, it means the read polling
1437 * was enabled, which implies that the recv buffer was not full. So we have a guarantee
1438 * that if such an event is not handled above in splice, it will be handled here by
1439 * recv().
1440 */
Willy Tarreaub605c422022-05-17 17:04:55 +02001441 while (sc_ep_test(cs, SE_FL_RCV_MORE) ||
Christopher Faulet5e29b762022-04-04 08:58:34 +02001442 (!(conn->flags & CO_FL_HANDSHAKE) &&
Willy Tarreaub605c422022-05-17 17:04:55 +02001443 (!sc_ep_test(cs, SE_FL_ERROR | SE_FL_EOS)) && !(ic->flags & CF_SHUTR))) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001444 int cur_flags = flags;
1445
1446 /* Compute transient CO_RFL_* flags */
1447 if (co_data(ic)) {
1448 cur_flags |= (CO_RFL_BUF_WET | CO_RFL_BUF_NOT_STUCK);
1449 }
1450
1451 /* <max> may be null. This is the mux responsibility to set
Willy Tarreaub605c422022-05-17 17:04:55 +02001452 * SE_FL_RCV_MORE on the CS if more space is needed.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001453 */
1454 max = channel_recv_max(ic);
1455 ret = conn->mux->rcv_buf(cs, &ic->buf, max, cur_flags);
1456
Willy Tarreaub605c422022-05-17 17:04:55 +02001457 if (sc_ep_test(cs, SE_FL_WANT_ROOM)) {
1458 /* SE_FL_WANT_ROOM must not be reported if the channel's
Christopher Faulet5e29b762022-04-04 08:58:34 +02001459 * buffer is empty.
1460 */
1461 BUG_ON(c_empty(ic));
1462
1463 cs_rx_room_blk(cs);
1464 /* Add READ_PARTIAL because some data are pending but
1465 * cannot be xferred to the channel
1466 */
1467 ic->flags |= CF_READ_PARTIAL;
1468 }
1469
1470 if (ret <= 0) {
1471 /* if we refrained from reading because we asked for a
1472 * flush to satisfy rcv_pipe(), we must not subscribe
1473 * and instead report that there's not enough room
1474 * here to proceed.
1475 */
1476 if (flags & CO_RFL_BUF_FLUSH)
1477 cs_rx_room_blk(cs);
1478 break;
1479 }
1480
1481 cur_read += ret;
1482
1483 /* if we're allowed to directly forward data, we must update ->o */
1484 if (ic->to_forward && !(ic->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
1485 unsigned long fwd = ret;
1486 if (ic->to_forward != CHN_INFINITE_FORWARD) {
1487 if (fwd > ic->to_forward)
1488 fwd = ic->to_forward;
1489 ic->to_forward -= fwd;
1490 }
1491 c_adv(ic, fwd);
1492 }
1493
1494 ic->flags |= CF_READ_PARTIAL;
1495 ic->total += ret;
1496
1497 /* End-of-input reached, we can leave. In this case, it is
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001498 * important to break the loop to not block the CS because of
Christopher Faulet5e29b762022-04-04 08:58:34 +02001499 * the channel's policies.This way, we are still able to receive
1500 * shutdowns.
1501 */
Willy Tarreaub605c422022-05-17 17:04:55 +02001502 if (sc_ep_test(cs, SE_FL_EOI))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001503 break;
1504
1505 if ((ic->flags & CF_READ_DONTWAIT) || --read_poll <= 0) {
1506 /* we're stopped by the channel's policy */
1507 cs_rx_chan_blk(cs);
1508 break;
1509 }
1510
1511 /* if too many bytes were missing from last read, it means that
1512 * it's pointless trying to read again because the system does
1513 * not have them in buffers.
1514 */
1515 if (ret < max) {
1516 /* if a streamer has read few data, it may be because we
1517 * have exhausted system buffers. It's not worth trying
1518 * again.
1519 */
1520 if (ic->flags & CF_STREAMER) {
1521 /* we're stopped by the channel's policy */
1522 cs_rx_chan_blk(cs);
1523 break;
1524 }
1525
1526 /* if we read a large block smaller than what we requested,
1527 * it's almost certain we'll never get anything more.
1528 */
1529 if (ret >= global.tune.recv_enough) {
1530 /* we're stopped by the channel's policy */
1531 cs_rx_chan_blk(cs);
1532 break;
1533 }
1534 }
1535
1536 /* if we are waiting for more space, don't try to read more data
1537 * right now.
1538 */
1539 if (cs_rx_blocked(cs))
1540 break;
1541 } /* while !flags */
1542
1543 done_recv:
1544 if (cur_read) {
1545 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) &&
1546 (cur_read <= ic->buf.size / 2)) {
1547 ic->xfer_large = 0;
1548 ic->xfer_small++;
1549 if (ic->xfer_small >= 3) {
1550 /* we have read less than half of the buffer in
1551 * one pass, and this happened at least 3 times.
1552 * This is definitely not a streamer.
1553 */
1554 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
1555 }
1556 else if (ic->xfer_small >= 2) {
1557 /* if the buffer has been at least half full twice,
1558 * we receive faster than we send, so at least it
1559 * is not a "fast streamer".
1560 */
1561 ic->flags &= ~CF_STREAMER_FAST;
1562 }
1563 }
1564 else if (!(ic->flags & CF_STREAMER_FAST) &&
1565 (cur_read >= ic->buf.size - global.tune.maxrewrite)) {
1566 /* we read a full buffer at once */
1567 ic->xfer_small = 0;
1568 ic->xfer_large++;
1569 if (ic->xfer_large >= 3) {
1570 /* we call this buffer a fast streamer if it manages
1571 * to be filled in one call 3 consecutive times.
1572 */
1573 ic->flags |= (CF_STREAMER | CF_STREAMER_FAST);
1574 }
1575 }
1576 else {
1577 ic->xfer_small = 0;
1578 ic->xfer_large = 0;
1579 }
1580 ic->last_read = now_ms;
1581 }
1582
1583 end_recv:
1584 ret = (cur_read != 0);
1585
1586 /* Report EOI on the channel if it was reached from the mux point of
1587 * view. */
Willy Tarreaub605c422022-05-17 17:04:55 +02001588 if (sc_ep_test(cs, SE_FL_EOI) && !(ic->flags & CF_EOI)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001589 ic->flags |= (CF_EOI|CF_READ_PARTIAL);
1590 ret = 1;
1591 }
1592
Willy Tarreaub605c422022-05-17 17:04:55 +02001593 if (sc_ep_test(cs, SE_FL_ERROR))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001594 ret = 1;
Willy Tarreaub605c422022-05-17 17:04:55 +02001595 else if (sc_ep_test(cs, SE_FL_EOS)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001596 /* we received a shutdown */
1597 ic->flags |= CF_READ_NULL;
1598 if (ic->flags & CF_AUTO_CLOSE)
1599 channel_shutw_now(ic);
1600 cs_conn_read0(cs);
1601 ret = 1;
1602 }
1603 else if (!cs_rx_blocked(cs)) {
1604 /* Subscribe to receive events if we're blocking on I/O */
1605 conn->mux->subscribe(cs, SUB_RETRY_RECV, &cs->wait_event);
1606 cs_rx_endp_done(cs);
1607 } else {
1608 cs_rx_endp_more(cs);
1609 ret = 1;
1610 }
1611 return ret;
1612}
1613
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001614/* This tries to perform a synchronous receive on the conn-stream to
Christopher Faulet5e29b762022-04-04 08:58:34 +02001615 * try to collect last arrived data. In practice it's only implemented on
1616 * conn_streams. Returns 0 if nothing was done, non-zero if new data or a
1617 * shutdown were collected. This may result on some delayed receive calls
1618 * to be programmed and performed later, though it doesn't provide any
1619 * such guarantee.
1620 */
1621int cs_conn_sync_recv(struct conn_stream *cs)
1622{
1623 if (!cs_state_in(cs->state, CS_SB_RDY|CS_SB_EST))
1624 return 0;
1625
1626 if (!cs_conn_mux(cs))
1627 return 0; // only conn_streams are supported
1628
1629 if (cs->wait_event.events & SUB_RETRY_RECV)
1630 return 0; // already subscribed
1631
1632 if (!cs_rx_endp_ready(cs) || cs_rx_blocked(cs))
1633 return 0; // already failed
1634
1635 return cs_conn_recv(cs);
1636}
1637
1638/*
1639 * This function is called to send buffer data to a stream socket.
1640 * It calls the mux layer's snd_buf function. It relies on the
1641 * caller to commit polling changes. The caller should check conn->flags
1642 * for errors.
1643 */
1644static int cs_conn_send(struct conn_stream *cs)
1645{
1646 struct connection *conn = __cs_conn(cs);
1647 struct stream *s = __cs_strm(cs);
1648 struct channel *oc = cs_oc(cs);
1649 int ret;
1650 int did_send = 0;
1651
Willy Tarreaub605c422022-05-17 17:04:55 +02001652 if (sc_ep_test(cs, SE_FL_ERROR | SE_FL_ERR_PENDING) || cs_is_conn_error(cs)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001653 /* We're probably there because the tasklet was woken up,
1654 * but process_stream() ran before, detected there were an
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001655 * error and put the CS back to CS_ST_TAR. There's still
Christopher Faulet5e29b762022-04-04 08:58:34 +02001656 * CO_FL_ERROR on the connection but we don't want to add
Willy Tarreaub605c422022-05-17 17:04:55 +02001657 * SE_FL_ERROR back, so give up
Christopher Faulet5e29b762022-04-04 08:58:34 +02001658 */
1659 if (cs->state < CS_ST_CON)
1660 return 0;
Willy Tarreaub605c422022-05-17 17:04:55 +02001661 sc_ep_set(cs, SE_FL_ERROR);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001662 return 1;
1663 }
1664
1665 /* We're already waiting to be able to send, give up */
1666 if (cs->wait_event.events & SUB_RETRY_SEND)
1667 return 0;
1668
1669 /* we might have been called just after an asynchronous shutw */
1670 if (oc->flags & CF_SHUTW)
1671 return 1;
1672
1673 /* we must wait because the mux is not installed yet */
1674 if (!conn->mux)
1675 return 0;
1676
1677 if (oc->pipe && conn->xprt->snd_pipe && conn->mux->snd_pipe) {
1678 ret = conn->mux->snd_pipe(cs, oc->pipe);
1679 if (ret > 0)
1680 did_send = 1;
1681
1682 if (!oc->pipe->data) {
1683 put_pipe(oc->pipe);
1684 oc->pipe = NULL;
1685 }
1686
1687 if (oc->pipe)
1688 goto end;
1689 }
1690
1691 /* At this point, the pipe is empty, but we may still have data pending
1692 * in the normal buffer.
1693 */
1694 if (co_data(oc)) {
1695 /* when we're here, we already know that there is no spliced
1696 * data left, and that there are sendable buffered data.
1697 */
1698
1699 /* check if we want to inform the kernel that we're interested in
1700 * sending more data after this call. We want this if :
1701 * - we're about to close after this last send and want to merge
1702 * the ongoing FIN with the last segment.
1703 * - we know we can't send everything at once and must get back
1704 * here because of unaligned data
1705 * - there is still a finite amount of data to forward
1706 * The test is arranged so that the most common case does only 2
1707 * tests.
1708 */
1709 unsigned int send_flag = 0;
1710
1711 if ((!(oc->flags & (CF_NEVER_WAIT|CF_SEND_DONTWAIT)) &&
1712 ((oc->to_forward && oc->to_forward != CHN_INFINITE_FORWARD) ||
1713 (oc->flags & CF_EXPECT_MORE) ||
1714 (IS_HTX_STRM(s) &&
1715 (!(oc->flags & (CF_EOI|CF_SHUTR)) && htx_expect_more(htxbuf(&oc->buf)))))) ||
1716 ((oc->flags & CF_ISRESP) &&
1717 ((oc->flags & (CF_AUTO_CLOSE|CF_SHUTW_NOW)) == (CF_AUTO_CLOSE|CF_SHUTW_NOW))))
1718 send_flag |= CO_SFL_MSG_MORE;
1719
1720 if (oc->flags & CF_STREAMER)
1721 send_flag |= CO_SFL_STREAMER;
1722
1723 if (s->txn && s->txn->flags & TX_L7_RETRY && !b_data(&s->txn->l7_buffer)) {
1724 /* If we want to be able to do L7 retries, copy
1725 * the data we're about to send, so that we are able
1726 * to resend them if needed
1727 */
1728 /* Try to allocate a buffer if we had none.
1729 * If it fails, the next test will just
1730 * disable the l7 retries by setting
1731 * l7_conn_retries to 0.
1732 */
1733 if (s->txn->req.msg_state != HTTP_MSG_DONE)
1734 s->txn->flags &= ~TX_L7_RETRY;
1735 else {
1736 if (b_alloc(&s->txn->l7_buffer) == NULL)
1737 s->txn->flags &= ~TX_L7_RETRY;
1738 else {
1739 memcpy(b_orig(&s->txn->l7_buffer),
1740 b_orig(&oc->buf),
1741 b_size(&oc->buf));
1742 s->txn->l7_buffer.head = co_data(oc);
1743 b_add(&s->txn->l7_buffer, co_data(oc));
1744 }
1745
1746 }
1747 }
1748
1749 ret = conn->mux->snd_buf(cs, &oc->buf, co_data(oc), send_flag);
1750 if (ret > 0) {
1751 did_send = 1;
1752 c_rew(oc, ret);
1753 c_realign_if_empty(oc);
1754
1755 if (!co_data(oc)) {
1756 /* Always clear both flags once everything has been sent, they're one-shot */
1757 oc->flags &= ~(CF_EXPECT_MORE | CF_SEND_DONTWAIT);
1758 }
1759 /* if some data remain in the buffer, it's only because the
1760 * system buffers are full, we will try next time.
1761 */
Christopher Faulet13045f02022-04-01 14:23:38 +02001762 }
1763 }
Christopher Faulet5e29b762022-04-04 08:58:34 +02001764
1765 end:
1766 if (did_send) {
1767 oc->flags |= CF_WRITE_PARTIAL | CF_WROTE_DATA;
1768 if (cs->state == CS_ST_CON)
1769 cs->state = CS_ST_RDY;
1770
1771 cs_rx_room_rdy(cs_opposite(cs));
1772 }
1773
Willy Tarreaub605c422022-05-17 17:04:55 +02001774 if (sc_ep_test(cs, SE_FL_ERROR | SE_FL_ERR_PENDING)) {
1775 sc_ep_set(cs, SE_FL_ERROR);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001776 return 1;
1777 }
1778
1779 /* We couldn't send all of our data, let the mux know we'd like to send more */
1780 if (!channel_is_empty(oc))
1781 conn->mux->subscribe(cs, SUB_RETRY_SEND, &cs->wait_event);
1782 return did_send;
1783}
1784
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001785/* perform a synchronous send() for the conn-stream. The CF_WRITE_NULL and
Christopher Faulet5e29b762022-04-04 08:58:34 +02001786 * CF_WRITE_PARTIAL flags are cleared prior to the attempt, and will possibly
1787 * be updated in case of success.
1788 */
1789void cs_conn_sync_send(struct conn_stream *cs)
1790{
1791 struct channel *oc = cs_oc(cs);
1792
1793 oc->flags &= ~(CF_WRITE_NULL|CF_WRITE_PARTIAL);
1794
1795 if (oc->flags & CF_SHUTW)
1796 return;
1797
1798 if (channel_is_empty(oc))
1799 return;
1800
1801 if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
1802 return;
1803
1804 if (!cs_conn_mux(cs))
1805 return;
1806
1807 cs_conn_send(cs);
1808}
1809
1810/* Called by I/O handlers after completion.. It propagates
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001811 * connection flags to the conn-stream, updates the stream (which may or
Christopher Faulet5e29b762022-04-04 08:58:34 +02001812 * may not take this opportunity to try to forward data), then update the
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001813 * connection's polling based on the channels and conn-stream's final
Christopher Faulet5e29b762022-04-04 08:58:34 +02001814 * states. The function always returns 0.
1815 */
1816static int cs_conn_process(struct conn_stream *cs)
1817{
1818 struct connection *conn = __cs_conn(cs);
1819 struct channel *ic = cs_ic(cs);
1820 struct channel *oc = cs_oc(cs);
1821
1822 BUG_ON(!conn);
1823
1824 /* If we have data to send, try it now */
1825 if (!channel_is_empty(oc) && !(cs->wait_event.events & SUB_RETRY_SEND))
1826 cs_conn_send(cs);
1827
1828 /* First step, report to the conn-stream what was detected at the
1829 * connection layer : errors and connection establishment.
Willy Tarreaub605c422022-05-17 17:04:55 +02001830 * Only add SE_FL_ERROR if we're connected, or we're attempting to
Christopher Faulet5e29b762022-04-04 08:58:34 +02001831 * connect, we may get there because we got woken up, but only run
1832 * after process_stream() noticed there were an error, and decided
1833 * to retry to connect, the connection may still have CO_FL_ERROR,
Willy Tarreaub605c422022-05-17 17:04:55 +02001834 * and we don't want to add SE_FL_ERROR back
Christopher Faulet5e29b762022-04-04 08:58:34 +02001835 *
1836 * Note: This test is only required because cs_conn_process is also the SI
1837 * wake callback. Otherwise cs_conn_recv()/cs_conn_send() already take
1838 * care of it.
1839 */
1840
1841 if (cs->state >= CS_ST_CON) {
1842 if (cs_is_conn_error(cs))
Willy Tarreaub605c422022-05-17 17:04:55 +02001843 sc_ep_set(cs, SE_FL_ERROR);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001844 }
1845
1846 /* If we had early data, and the handshake ended, then
1847 * we can remove the flag, and attempt to wake the task up,
1848 * in the event there's an analyser waiting for the end of
1849 * the handshake.
1850 */
1851 if (!(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS)) &&
Willy Tarreaub605c422022-05-17 17:04:55 +02001852 sc_ep_test(cs, SE_FL_WAIT_FOR_HS)) {
1853 sc_ep_clr(cs, SE_FL_WAIT_FOR_HS);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001854 task_wakeup(cs_strm_task(cs), TASK_WOKEN_MSG);
1855 }
1856
1857 if (!cs_state_in(cs->state, CS_SB_EST|CS_SB_DIS|CS_SB_CLO) &&
1858 (conn->flags & CO_FL_WAIT_XPRT) == 0) {
1859 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
1860 oc->flags |= CF_WRITE_NULL;
1861 if (cs->state == CS_ST_CON)
1862 cs->state = CS_ST_RDY;
1863 }
1864
1865 /* Report EOS on the channel if it was reached from the mux point of
1866 * view.
1867 *
1868 * Note: This test is only required because cs_conn_process is also the SI
1869 * wake callback. Otherwise cs_conn_recv()/cs_conn_send() already take
1870 * care of it.
1871 */
Willy Tarreaub605c422022-05-17 17:04:55 +02001872 if (sc_ep_test(cs, SE_FL_EOS) && !(ic->flags & CF_SHUTR)) {
Christopher Faulet5e29b762022-04-04 08:58:34 +02001873 /* we received a shutdown */
1874 ic->flags |= CF_READ_NULL;
1875 if (ic->flags & CF_AUTO_CLOSE)
1876 channel_shutw_now(ic);
1877 cs_conn_read0(cs);
1878 }
1879
1880 /* Report EOI on the channel if it was reached from the mux point of
1881 * view.
1882 *
1883 * Note: This test is only required because cs_conn_process is also the SI
1884 * wake callback. Otherwise cs_conn_recv()/cs_conn_send() already take
1885 * care of it.
1886 */
Willy Tarreaub605c422022-05-17 17:04:55 +02001887 if (sc_ep_test(cs, SE_FL_EOI) && !(ic->flags & CF_EOI))
Christopher Faulet5e29b762022-04-04 08:58:34 +02001888 ic->flags |= (CF_EOI|CF_READ_PARTIAL);
1889
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001890 /* Second step : update the conn-stream and channels, try to forward any
Christopher Faulet5e29b762022-04-04 08:58:34 +02001891 * pending data, then possibly wake the stream up based on the new
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001892 * conn-stream status.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001893 */
1894 cs_notify(cs);
1895 stream_release_buffers(__cs_strm(cs));
1896 return 0;
1897}
1898
1899/* This is the ->process() function for any conn-stream's wait_event task.
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001900 * It's assigned during the conn-stream's initialization, for any type of
1901 * conn-stream. Thus it is always safe to perform a tasklet_wakeup() on a
1902 * conn-stream, as the presence of the CS is checked there.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001903 */
1904struct task *cs_conn_io_cb(struct task *t, void *ctx, unsigned int state)
1905{
1906 struct conn_stream *cs = ctx;
1907 int ret = 0;
1908
1909 if (!cs_conn(cs))
1910 return t;
1911
1912 if (!(cs->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(cs_oc(cs)))
1913 ret = cs_conn_send(cs);
1914 if (!(cs->wait_event.events & SUB_RETRY_RECV))
1915 ret |= cs_conn_recv(cs);
1916 if (ret != 0)
1917 cs_conn_process(cs);
1918
1919 stream_release_buffers(__cs_strm(cs));
1920 return t;
1921}
1922
1923/* Callback to be used by applet handlers upon completion. It updates the stream
1924 * (which may or may not take this opportunity to try to forward data), then
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001925 * may re-enable the applet's based on the channels and conn-stream's final
Christopher Faulet5e29b762022-04-04 08:58:34 +02001926 * states.
1927 */
1928static int cs_applet_process(struct conn_stream *cs)
1929{
1930 struct channel *ic = cs_ic(cs);
1931
1932 BUG_ON(!cs_appctx(cs));
1933
1934 /* If the applet wants to write and the channel is closed, it's a
1935 * broken pipe and it must be reported.
1936 */
Willy Tarreaub605c422022-05-17 17:04:55 +02001937 if (!sc_ep_test(cs, SE_FL_RX_WAIT_EP) && (ic->flags & CF_SHUTR))
1938 sc_ep_set(cs, SE_FL_ERROR);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001939
1940 /* automatically mark the applet having data available if it reported
1941 * begin blocked by the channel.
1942 */
1943 if (cs_rx_blocked(cs))
1944 cs_rx_endp_more(cs);
1945
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001946 /* update the conn-stream, channels, and possibly wake the stream up */
Christopher Faulet5e29b762022-04-04 08:58:34 +02001947 cs_notify(cs);
1948 stream_release_buffers(__cs_strm(cs));
1949
1950 /* cs_notify may have passed through chk_snd and released some
1951 * RXBLK flags. Process_stream will consider those flags to wake up the
1952 * appctx but in the case the task is not in runqueue we may have to
1953 * wakeup the appctx immediately.
1954 */
1955 if ((cs_rx_endp_ready(cs) && !cs_rx_blocked(cs)) ||
1956 (cs_tx_endp_ready(cs) && !cs_tx_blocked(cs)))
1957 appctx_wakeup(__cs_appctx(cs));
1958 return 0;
Christopher Faulet13045f02022-04-01 14:23:38 +02001959}