blob: b5147905b36affcfa6d519d8367ac3c53bbdc728 [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));
Christopher Fauletdb90f2a2022-03-22 16:06:25 +010024DECLARE_POOL(pool_head_cs_endpoint, "cs_endpoint", sizeof(struct cs_endpoint));
Christopher Faulet1329f2a2021-12-16 17:32:56 +010025
Christopher Faulet9ffddd52022-04-01 14:04:29 +020026/* functions used by default on a detached conn-stream */
27static void cs_app_shutr(struct conn_stream *cs);
28static void cs_app_shutw(struct conn_stream *cs);
29static void cs_app_chk_rcv(struct conn_stream *cs);
30static void cs_app_chk_snd(struct conn_stream *cs);
31
32/* functions used on a mux-based conn-stream */
33static void cs_app_shutr_conn(struct conn_stream *cs);
34static void cs_app_shutw_conn(struct conn_stream *cs);
35static void cs_app_chk_rcv_conn(struct conn_stream *cs);
36static void cs_app_chk_snd_conn(struct conn_stream *cs);
37
38/* functions used on an applet-based conn-stream */
39static void cs_app_shutr_applet(struct conn_stream *cs);
40static void cs_app_shutw_applet(struct conn_stream *cs);
41static void cs_app_chk_rcv_applet(struct conn_stream *cs);
42static void cs_app_chk_snd_applet(struct conn_stream *cs);
43
44/* conn-stream operations for connections */
45struct cs_app_ops cs_app_conn_ops = {
46 .chk_rcv = cs_app_chk_rcv_conn,
47 .chk_snd = cs_app_chk_snd_conn,
48 .shutr = cs_app_shutr_conn,
49 .shutw = cs_app_shutw_conn,
50};
51
52/* conn-stream operations for embedded tasks */
53struct cs_app_ops cs_app_embedded_ops = {
54 .chk_rcv = cs_app_chk_rcv,
55 .chk_snd = cs_app_chk_snd,
56 .shutr = cs_app_shutr,
57 .shutw = cs_app_shutw,
58};
59
60/* conn-stream operations for connections */
61struct cs_app_ops cs_app_applet_ops = {
62 .chk_rcv = cs_app_chk_rcv_applet,
63 .chk_snd = cs_app_chk_snd_applet,
64 .shutr = cs_app_shutr_applet,
65 .shutw = cs_app_shutw_applet,
66};
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 */
Christopher Fauletdb90f2a2022-03-22 16:06:25 +010085void cs_endpoint_init(struct cs_endpoint *endp)
86{
87 endp->target = NULL;
88 endp->ctx = NULL;
89 endp->flags = CS_EP_NONE;
90}
91
Christopher Faulet9ed77422022-04-12 08:51:15 +020092/* Tries to alloc an endpoint and initialize it. Returns NULL on failure. */
Christopher Fauletdb90f2a2022-03-22 16:06:25 +010093struct cs_endpoint *cs_endpoint_new()
94{
95 struct cs_endpoint *endp;
96
97 endp = pool_alloc(pool_head_cs_endpoint);
98 if (unlikely(!endp))
99 return NULL;
100
101 cs_endpoint_init(endp);
102 return endp;
103}
104
Christopher Faulet9ed77422022-04-12 08:51:15 +0200105/* Releases an endpoint. It is the caller responsibility to be sure it is safe
106 * and it is not shared with another entity
107 */
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100108void cs_endpoint_free(struct cs_endpoint *endp)
109{
110 pool_free(pool_head_cs_endpoint, endp);
111}
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100112
Christopher Fauletdd2d0d82021-12-20 09:34:32 +0100113/* Tries to allocate a new conn_stream and initialize its main fields. On
Christopher Faulet9ed77422022-04-12 08:51:15 +0200114 * failure, nothing is allocated and NULL is returned. It is an internal
115 * function. The caller must, at least, set the CS_EP_ORPHAN or CS_EP_DETACĀ§HED
116 * flag.
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100117 */
Christopher Faulet265e1652022-04-12 08:49:27 +0200118static struct conn_stream *cs_new(struct cs_endpoint *endp)
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100119{
120 struct conn_stream *cs;
121
122 cs = pool_alloc(pool_head_connstream);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100123
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100124 if (unlikely(!cs))
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100125 goto alloc_error;
Christopher Fauletbb772d02022-03-22 15:28:36 +0100126
127 cs->obj_type = OBJ_TYPE_CS;
128 cs->flags = CS_FL_NONE;
Christopher Faulet62e75742022-03-31 09:16:34 +0200129 cs->state = CS_ST_INI;
Christopher Faulet1d987772022-03-29 18:03:35 +0200130 cs->hcto = TICK_ETERNITY;
Christopher Fauletbb772d02022-03-22 15:28:36 +0100131 cs->app = NULL;
Christopher Fauletbb772d02022-03-22 15:28:36 +0100132 cs->data_cb = NULL;
Christopher Faulet8da67aa2022-03-29 17:53:09 +0200133 cs->src = NULL;
134 cs->dst = NULL;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200135 cs->wait_event.tasklet = NULL;
136 cs->wait_event.events = 0;
137
Christopher Faulet9ed77422022-04-12 08:51:15 +0200138 /* If there is no endpoint, allocate a new one now */
Christopher Fauletb669d682022-03-22 18:37:19 +0100139 if (!endp) {
140 endp = cs_endpoint_new();
141 if (unlikely(!endp))
142 goto alloc_error;
143 }
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100144 cs->endp = endp;
145
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100146 return cs;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100147
148 alloc_error:
149 pool_free(pool_head_connstream, cs);
150 return NULL;
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100151}
152
Christopher Faulet9ed77422022-04-12 08:51:15 +0200153/* Creates a new conn-stream and its associated stream from a mux. <endp> must be
154 * defined. It returns NULL on error. On success, the new conn-stream is
155 * returned. In this case, CS_EP_ORPHAN flag is removed.
156 */
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100157struct conn_stream *cs_new_from_mux(struct cs_endpoint *endp, struct session *sess, struct buffer *input)
158{
159 struct conn_stream *cs;
160
161 cs = cs_new(endp);
162 if (unlikely(!cs))
163 return NULL;
164 if (unlikely(!stream_new(sess, cs, input))) {
165 pool_free(pool_head_connstream, cs);
166 cs = NULL;
167 }
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100168 endp->flags &= ~CS_EP_ORPHAN;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100169 return cs;
170}
171
Christopher Faulet9ed77422022-04-12 08:51:15 +0200172/* Creates a new conn-stream and its associated stream from an applet. <endp>
173 * must be defined. It returns NULL on error. On success, the new conn-stream is
174 * returned. In this case, CS_EP_ORPHAN flag is removed. The created CS is used
175 * to set the appctx owner.
176 */
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100177struct conn_stream *cs_new_from_applet(struct cs_endpoint *endp, struct session *sess, struct buffer *input)
178{
179 struct conn_stream *cs;
180 struct appctx *appctx = endp->ctx;
181
182 cs = cs_new(endp);
183 if (unlikely(!cs))
184 return NULL;
185 appctx->owner = cs;
186 if (unlikely(!stream_new(sess, cs, input))) {
187 pool_free(pool_head_connstream, cs);
188 cs = NULL;
189 }
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100190 endp->flags &= ~CS_EP_ORPHAN;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100191 return cs;
192}
193
Christopher Faulet9ed77422022-04-12 08:51:15 +0200194/* Creates a new conn-stream from an stream. There is no endpoint here, thus it
195 * will be created by cs_new(). So the CS_EP_DETACHED flag is set. It returns
196 * NULL on error. On success, the new conn-stream is returned.
197 */
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100198struct conn_stream *cs_new_from_strm(struct stream *strm, unsigned int flags)
199{
200 struct conn_stream *cs;
201
202 cs = cs_new(NULL);
203 if (unlikely(!cs))
204 return NULL;
205 cs->flags |= flags;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100206 cs->endp->flags |= CS_EP_DETACHED;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100207 cs->app = &strm->obj_type;
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200208 cs->ops = &cs_app_embedded_ops;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100209 cs->data_cb = NULL;
210 return cs;
211}
212
Christopher Faulet9ed77422022-04-12 08:51:15 +0200213/* Creates a new conn-stream from an health-check. There is no endpoint here,
214 * thus it will be created by cs_new(). So the CS_EP_DETACHED flag is set. It
215 * returns NULL on error. On success, the new conn-stream is returned.
216 */
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100217struct conn_stream *cs_new_from_check(struct check *check, unsigned int flags)
218{
219 struct conn_stream *cs;
220
221 cs = cs_new(NULL);
222 if (unlikely(!cs))
223 return NULL;
224 cs->flags |= flags;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100225 cs->endp->flags |= CS_EP_DETACHED;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100226 cs->app = &check->obj_type;
227 cs->data_cb = &check_conn_cb;
228 return cs;
229}
230
Christopher Faulet9ed77422022-04-12 08:51:15 +0200231/* Releases a conn_stream previously allocated by cs_new(), as well as its
232 * endpoint, if it exists. This function is called internally or on error path.
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100233 */
234void cs_free(struct conn_stream *cs)
235{
Christopher Faulet8da67aa2022-03-29 17:53:09 +0200236 sockaddr_free(&cs->src);
237 sockaddr_free(&cs->dst);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100238 if (cs->endp) {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100239 BUG_ON(!(cs->endp->flags & CS_EP_DETACHED));
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100240 cs_endpoint_free(cs->endp);
241 }
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200242 if (cs->wait_event.tasklet)
243 tasklet_free(cs->wait_event.tasklet);
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100244 pool_free(pool_head_connstream, cs);
245}
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100246
Christopher Fauleteb50c012022-04-21 14:22:53 +0200247/* Conditionally removes a conn-stream if it is detached and if there is no app
248 * layer defined. Except on error path, this one must be used. if release, the
249 * pointer on the CS is set to NULL.
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200250 */
Christopher Fauleteb50c012022-04-21 14:22:53 +0200251static void cs_free_cond(struct conn_stream **csp)
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200252{
Christopher Fauleteb50c012022-04-21 14:22:53 +0200253 struct conn_stream *cs = *csp;
254
255 if (!cs->app && (!cs->endp || (cs->endp->flags & CS_EP_DETACHED))) {
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200256 cs_free(cs);
Christopher Fauleteb50c012022-04-21 14:22:53 +0200257 *csp = NULL;
258 }
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200259}
260
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100261
Christopher Faulet9ed77422022-04-12 08:51:15 +0200262/* Attaches a conn_stream to a mux endpoint and sets the endpoint ctx. Returns
263 * -1 on error and 0 on sucess. CS_EP_DETACHED flag is removed. This function is
264 * called from a mux when it is attached to a stream or a health-check.
265 */
Christopher Faulet070b91b2022-03-31 19:27:18 +0200266int cs_attach_mux(struct conn_stream *cs, void *target, void *ctx)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100267{
Christopher Faulet93882042022-01-19 14:56:50 +0100268 struct connection *conn = ctx;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100269
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100270 cs->endp->target = target;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100271 cs->endp->ctx = ctx;
272 cs->endp->flags |= CS_EP_T_MUX;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100273 cs->endp->flags &= ~CS_EP_DETACHED;
Christopher Faulet93882042022-01-19 14:56:50 +0100274 if (!conn->ctx)
275 conn->ctx = cs;
276 if (cs_strm(cs)) {
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200277 if (!cs->wait_event.tasklet) {
278 cs->wait_event.tasklet = tasklet_new();
279 if (!cs->wait_event.tasklet)
280 return -1;
Christopher Faulet4a7764a2022-04-01 16:58:52 +0200281 cs->wait_event.tasklet->process = cs_conn_io_cb;
282 cs->wait_event.tasklet->context = cs;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200283 cs->wait_event.events = 0;
284 }
285
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200286 cs->ops = &cs_app_conn_ops;
Christopher Faulet000ba3e2022-04-01 17:06:32 +0200287 cs->data_cb = &cs_data_conn_cb;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100288 }
Christopher Faulet93882042022-01-19 14:56:50 +0100289 else if (cs_check(cs))
290 cs->data_cb = &check_conn_cb;
Christopher Faulet070b91b2022-03-31 19:27:18 +0200291 return 0;
Christopher Faulet93882042022-01-19 14:56:50 +0100292}
293
Christopher Faulet9ed77422022-04-12 08:51:15 +0200294/* Attaches a conn_stream to an applet endpoint and sets the endpoint
295 * ctx. Returns -1 on error and 0 on sucess. CS_EP_DETACHED flag is
296 * removed. This function is called by a stream when a backend applet is
297 * registered.
298 */
Christopher Faulet265e1652022-04-12 08:49:27 +0200299static void cs_attach_applet(struct conn_stream *cs, void *target, void *ctx)
Christopher Faulet93882042022-01-19 14:56:50 +0100300{
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100301 struct appctx *appctx = target;
Christopher Faulet93882042022-01-19 14:56:50 +0100302
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100303 cs->endp->target = target;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100304 cs->endp->ctx = ctx;
305 cs->endp->flags |= CS_EP_T_APPLET;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100306 cs->endp->flags &= ~CS_EP_DETACHED;
Christopher Faulet93882042022-01-19 14:56:50 +0100307 appctx->owner = cs;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100308 if (cs_strm(cs)) {
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200309 cs->ops = &cs_app_applet_ops;
Christopher Faulet6059ba42022-04-01 16:34:53 +0200310 cs->data_cb = &cs_data_applet_cb;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100311 }
312}
313
Christopher Faulet9ed77422022-04-12 08:51:15 +0200314/* Attaches a conn_stream to a app layer and sets the relevant
315 * callbacks. Returns -1 on error and 0 on success. CS_EP_ORPHAN flag is
316 * removed. This function is called by a stream when it is created to attach it
317 * on the conn-stream on the client side.
318 */
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100319int cs_attach_strm(struct conn_stream *cs, struct stream *strm)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100320{
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100321 cs->app = &strm->obj_type;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100322 cs->endp->flags &= ~CS_EP_ORPHAN;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100323 if (cs->endp->flags & CS_EP_T_MUX) {
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200324 cs->wait_event.tasklet = tasklet_new();
Christopher Faulet582a2262022-04-04 11:25:59 +0200325 if (!cs->wait_event.tasklet)
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200326 return -1;
Christopher Faulet4a7764a2022-04-01 16:58:52 +0200327 cs->wait_event.tasklet->process = cs_conn_io_cb;
328 cs->wait_event.tasklet->context = cs;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200329 cs->wait_event.events = 0;
330
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200331 cs->ops = &cs_app_conn_ops;
Christopher Faulet000ba3e2022-04-01 17:06:32 +0200332 cs->data_cb = &cs_data_conn_cb;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100333 }
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100334 else if (cs->endp->flags & CS_EP_T_APPLET) {
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200335 cs->ops = &cs_app_applet_ops;
Christopher Faulet6059ba42022-04-01 16:34:53 +0200336 cs->data_cb = &cs_data_applet_cb;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100337 }
338 else {
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200339 cs->ops = &cs_app_embedded_ops;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100340 cs->data_cb = NULL;
341 }
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100342 return 0;
343}
344
Christopher Faulet9ed77422022-04-12 08:51:15 +0200345/* Detaches the conn_stream from the endpoint, if any. For a connecrion, if a
346 * mux owns the connection ->detach() callback is called. Otherwise, it means
347 * the conn-stream owns the connection. In this case the connection is closed
348 * and released. For an applet, the appctx is released. If still allocated, the
349 * endpoint is reset and flag as detached. If the app layer is also detached,
350 * the conn-stream is released.
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100351 */
Christopher Fauleteb50c012022-04-21 14:22:53 +0200352static void cs_detach_endp(struct conn_stream **csp)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100353{
Christopher Fauleteb50c012022-04-21 14:22:53 +0200354 struct conn_stream *cs = *csp;
355
356 if (!cs)
357 return;
358
Christopher Fauletb041b232022-03-24 10:27:02 +0100359 if (!cs->endp)
360 goto reset_cs;
361
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100362 if (cs->endp->flags & CS_EP_T_MUX) {
Christopher Fauletcea05432022-04-14 11:40:12 +0200363 struct connection *conn = __cs_conn(cs);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100364
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100365 if (conn->mux) {
Christopher Faulet54e85cb2022-01-06 08:46:56 +0100366 /* TODO: handle unsubscribe for healthchecks too */
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100367 cs->endp->flags |= CS_EP_ORPHAN;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200368 if (cs->wait_event.events != 0)
369 conn->mux->unsubscribe(cs, cs->wait_event.events, &cs->wait_event);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100370 conn->mux->detach(cs);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100371 cs->endp = NULL;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100372 }
373 else {
374 /* It's too early to have a mux, let's just destroy
375 * the connection
376 */
377 conn_stop_tracking(conn);
378 conn_full_close(conn);
379 if (conn->destroy_cb)
380 conn->destroy_cb(conn);
381 conn_free(conn);
382 }
383 }
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100384 else if (cs->endp->flags & CS_EP_T_APPLET) {
Christopher Fauletcea05432022-04-14 11:40:12 +0200385 struct appctx *appctx = __cs_appctx(cs);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100386
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100387 cs->endp->flags |= CS_EP_ORPHAN;
Christopher Fauletca6c9bb2022-04-21 08:44:09 +0200388 cs_applet_shut(cs);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100389 appctx_free(appctx);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100390 cs->endp = NULL;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100391 }
392
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100393 if (cs->endp) {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100394 /* the cs is the only one one the endpoint */
Christopher Fauleta6c4a482022-04-28 18:25:24 +0200395 cs->endp->target = NULL;
396 cs->endp->ctx = NULL;
Christopher Fauletc41f93c2022-05-04 09:52:48 +0200397 cs->endp->flags &= CS_EP_APP_MASK;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100398 cs->endp->flags |= CS_EP_DETACHED;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100399 }
400
Christopher Fauletb041b232022-03-24 10:27:02 +0100401 reset_cs:
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100402 /* FIXME: Rest CS for now but must be reviewed. CS flags are only
403 * connection related for now but this will evolved
404 */
Christopher Faulet30995112022-03-25 15:32:38 +0100405 cs->flags &= CS_FL_ISBACK;
Christopher Faulet582a2262022-04-04 11:25:59 +0200406 if (cs_strm(cs))
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200407 cs->ops = &cs_app_embedded_ops;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100408 cs->data_cb = NULL;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200409 cs_free_cond(csp);
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100410}
411
Christopher Faulet9ed77422022-04-12 08:51:15 +0200412/* Detaches the conn_stream from the app layer. If there is no endpoint attached
413 * to the conn_stream
414 */
Christopher Fauleteb50c012022-04-21 14:22:53 +0200415static void cs_detach_app(struct conn_stream **csp)
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100416{
Christopher Fauleteb50c012022-04-21 14:22:53 +0200417 struct conn_stream *cs = *csp;
418
419 if (!cs)
420 return;
421
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100422 cs->app = NULL;
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100423 cs->data_cb = NULL;
Christopher Faulet8da67aa2022-03-29 17:53:09 +0200424 sockaddr_free(&cs->src);
425 sockaddr_free(&cs->dst);
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200426
427 if (cs->wait_event.tasklet)
428 tasklet_free(cs->wait_event.tasklet);
429 cs->wait_event.tasklet = NULL;
430 cs->wait_event.events = 0;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200431 cs_free_cond(csp);
432}
433
434/* Destroy the conn_stream. It is detached from its endpoint and its
435 * application. After this call, the conn_stream must be considered as released.
436 */
437void cs_destroy(struct conn_stream *cs)
438{
439 cs_detach_endp(&cs);
440 cs_detach_app(&cs);
441 BUG_ON_HOT(cs);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100442}
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100443
Christopher Faulet9ed77422022-04-12 08:51:15 +0200444/* Resets the conn-stream endpoint. It happens when the app layer want to renew
445 * its endpoint. For a connection retry for instance. If a mux or an applet is
446 * attached, a new endpoint is created. Returns -1 on error and 0 on sucess.
Christopher Fauleta6c4a482022-04-28 18:25:24 +0200447 *
448 * Only CS_EP_ERROR flag is removed on the endpoint. Orther flags are preserved.
449 * It is the caller responsibility to remove other flags if needed.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200450 */
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100451int cs_reset_endp(struct conn_stream *cs)
452{
Christopher Fauletb041b232022-03-24 10:27:02 +0100453 struct cs_endpoint *new_endp;
454
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100455 BUG_ON(!cs->app);
Christopher Fauleta6c4a482022-04-28 18:25:24 +0200456
457 cs->endp->flags &= ~CS_EP_ERROR;
Christopher Fauletb041b232022-03-24 10:27:02 +0100458 if (!__cs_endp_target(cs)) {
459 /* endpoint not attached or attached to a mux with no
460 * target. Thus the endpoint will not be release but just
Christopher Fauleteb50c012022-04-21 14:22:53 +0200461 * reset. The app is still attached, the cs will not be
462 * released.
Christopher Fauletb041b232022-03-24 10:27:02 +0100463 */
Christopher Fauleteb50c012022-04-21 14:22:53 +0200464 cs_detach_endp(&cs);
Christopher Fauletb041b232022-03-24 10:27:02 +0100465 return 0;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100466 }
Christopher Fauletb041b232022-03-24 10:27:02 +0100467
468 /* allocate the new endpoint first to be able to set error if it
469 * fails */
470 new_endp = cs_endpoint_new();
471 if (!unlikely(new_endp)) {
472 cs->endp->flags |= CS_EP_ERROR;
473 return -1;
474 }
Christopher Fauletc41f93c2022-05-04 09:52:48 +0200475 new_endp->flags = (cs->endp->flags & CS_EP_APP_MASK);
Christopher Fauletb041b232022-03-24 10:27:02 +0100476
Christopher Fauleteb50c012022-04-21 14:22:53 +0200477 /* The app is still attached, the cs will not be released */
478 cs_detach_endp(&cs);
Christopher Fauletb041b232022-03-24 10:27:02 +0100479 BUG_ON(cs->endp);
480 cs->endp = new_endp;
481 cs->endp->flags |= CS_EP_DETACHED;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100482 return 0;
483}
Christopher Faulet37046632022-04-01 11:36:58 +0200484
485
Christopher Faulet1336ccf2022-04-12 18:15:16 +0200486/* Create an applet to handle a conn-stream as a new appctx. The CS will
Christopher Faulet37046632022-04-01 11:36:58 +0200487 * wake it up every time it is solicited. The appctx must be deleted by the task
488 * handler using cs_detach_endp(), possibly from within the function itself.
489 * It also pre-initializes the applet's context and returns it (or NULL in case
490 * it could not be allocated).
491 */
Christopher Faulet1336ccf2022-04-12 18:15:16 +0200492struct appctx *cs_applet_create(struct conn_stream *cs, struct applet *app)
Christopher Faulet37046632022-04-01 11:36:58 +0200493{
494 struct appctx *appctx;
495
496 DPRINTF(stderr, "registering handler %p for cs %p (was %p)\n", app, cs, cs_strm_task(cs));
497
498 appctx = appctx_new(app, cs->endp);
499 if (!appctx)
500 return NULL;
501 cs_attach_applet(cs, appctx, appctx);
502 appctx->owner = cs;
503 appctx->t->nice = __cs_strm(cs)->task->nice;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200504 cs_cant_get(cs);
Christopher Faulet37046632022-04-01 11:36:58 +0200505 appctx_wakeup(appctx);
Christopher Fauleta33ff7a2022-04-21 11:52:07 +0200506
507 cs->state = CS_ST_RDY;
Christopher Faulet37046632022-04-01 11:36:58 +0200508 return appctx;
509}
510
511/* call the applet's release function if any. Needs to be called upon close() */
Christopher Fauletca6c9bb2022-04-21 08:44:09 +0200512void cs_applet_shut(struct conn_stream *cs)
Christopher Faulet37046632022-04-01 11:36:58 +0200513{
514 struct appctx *appctx = __cs_appctx(cs);
515
Christopher Faulet02ef0ff2022-04-21 08:50:00 +0200516 if (cs->endp->flags & (CS_EP_SHR|CS_EP_SHW))
517 return;
518
519 if (appctx->applet->release)
Christopher Faulet37046632022-04-01 11:36:58 +0200520 appctx->applet->release(appctx);
Christopher Faulet02ef0ff2022-04-21 08:50:00 +0200521
522 cs->endp->flags |= CS_EP_SHRR | CS_EP_SHWN;
Christopher Faulet37046632022-04-01 11:36:58 +0200523}
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200524
525/*
526 * This function performs a shutdown-read on a detached conn-stream in a
527 * connected or init state (it does nothing for other states). It either shuts
528 * the read side or marks itself as closed. The buffer flags are updated to
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +0200529 * reflect the new state. If the conn-stream has CS_FL_NOHALF, we also
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200530 * forward the close to the write side. The owner task is woken up if it exists.
531 */
532static void cs_app_shutr(struct conn_stream *cs)
533{
534 struct channel *ic = cs_ic(cs);
535
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200536 cs_rx_shut_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200537 if (ic->flags & CF_SHUTR)
538 return;
539 ic->flags |= CF_SHUTR;
540 ic->rex = TICK_ETERNITY;
541
542 if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
543 return;
544
545 if (cs_oc(cs)->flags & CF_SHUTW) {
546 cs->state = CS_ST_DIS;
547 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
548 }
549 else if (cs->flags & CS_FL_NOHALF) {
550 /* we want to immediately forward this close to the write side */
551 return cs_app_shutw(cs);
552 }
553
554 /* note that if the task exists, it must unregister itself once it runs */
555 if (!(cs->flags & CS_FL_DONT_WAKE))
556 task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO);
557}
558
559/*
560 * This function performs a shutdown-write on a detached conn-stream in a
561 * connected or init state (it does nothing for other states). It either shuts
562 * the write side or marks itself as closed. The buffer flags are updated to
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +0200563 * reflect the new state. It does also close everything if the CS was marked as
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200564 * being in error state. The owner task is woken up if it exists.
565 */
566static void cs_app_shutw(struct conn_stream *cs)
567{
568 struct channel *ic = cs_ic(cs);
569 struct channel *oc = cs_oc(cs);
570
571 oc->flags &= ~CF_SHUTW_NOW;
572 if (oc->flags & CF_SHUTW)
573 return;
574 oc->flags |= CF_SHUTW;
575 oc->wex = TICK_ETERNITY;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200576 cs_done_get(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200577
578 if (tick_isset(cs->hcto)) {
579 ic->rto = cs->hcto;
580 ic->rex = tick_add(now_ms, ic->rto);
581 }
582
583 switch (cs->state) {
584 case CS_ST_RDY:
585 case CS_ST_EST:
586 /* we have to shut before closing, otherwise some short messages
587 * may never leave the system, especially when there are remaining
588 * unread data in the socket input buffer, or when nolinger is set.
589 * However, if CS_FL_NOLINGER is explicitly set, we know there is
590 * no risk so we close both sides immediately.
591 */
592 if (!(cs->endp->flags & CS_EP_ERROR) && !(cs->flags & CS_FL_NOLINGER) &&
593 !(ic->flags & (CF_SHUTR|CF_DONT_READ)))
594 return;
595
596 /* fall through */
597 case CS_ST_CON:
598 case CS_ST_CER:
599 case CS_ST_QUE:
600 case CS_ST_TAR:
601 /* Note that none of these states may happen with applets */
602 cs->state = CS_ST_DIS;
603 /* fall through */
604 default:
605 cs->flags &= ~CS_FL_NOLINGER;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200606 cs_rx_shut_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200607 ic->flags |= CF_SHUTR;
608 ic->rex = TICK_ETERNITY;
609 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
610 }
611
612 /* note that if the task exists, it must unregister itself once it runs */
613 if (!(cs->flags & CS_FL_DONT_WAKE))
614 task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO);
615}
616
617/* default chk_rcv function for scheduled tasks */
618static void cs_app_chk_rcv(struct conn_stream *cs)
619{
620 struct channel *ic = cs_ic(cs);
621
622 DPRINTF(stderr, "%s: cs=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n",
623 __FUNCTION__,
624 cs, cs->state, ic->flags, cs_oc(cs)->flags);
625
626 if (ic->pipe) {
627 /* stop reading */
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200628 cs_rx_room_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200629 }
630 else {
631 /* (re)start reading */
632 if (!(cs->flags & CS_FL_DONT_WAKE))
633 task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO);
634 }
635}
636
637/* default chk_snd function for scheduled tasks */
638static void cs_app_chk_snd(struct conn_stream *cs)
639{
640 struct channel *oc = cs_oc(cs);
641
642 DPRINTF(stderr, "%s: cs=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n",
643 __FUNCTION__,
644 cs, cs->state, cs_ic(cs)->flags, oc->flags);
645
646 if (unlikely(cs->state != CS_ST_EST || (oc->flags & CF_SHUTW)))
647 return;
648
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200649 if (!(cs->endp->flags & CS_EP_WAIT_DATA) || /* not waiting for data */
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200650 channel_is_empty(oc)) /* called with nothing to send ! */
651 return;
652
653 /* Otherwise there are remaining data to be sent in the buffer,
654 * so we tell the handler.
655 */
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200656 cs->endp->flags &= ~CS_EP_WAIT_DATA;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200657 if (!tick_isset(oc->wex))
658 oc->wex = tick_add_ifset(now_ms, oc->wto);
659
660 if (!(cs->flags & CS_FL_DONT_WAKE))
661 task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO);
662}
663
664/*
665 * This function performs a shutdown-read on a conn-stream attached to
666 * a connection in a connected or init state (it does nothing for other
667 * states). It either shuts the read side or marks itself as closed. The buffer
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +0200668 * flags are updated to reflect the new state. If the conn-stream has
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200669 * CS_FL_NOHALF, we also forward the close to the write side. If a control
670 * layer is defined, then it is supposed to be a socket layer and file
671 * descriptors are then shutdown or closed accordingly. The function
672 * automatically disables polling if needed.
673 */
674static void cs_app_shutr_conn(struct conn_stream *cs)
675{
676 struct channel *ic = cs_ic(cs);
677
678 BUG_ON(!cs_conn(cs));
679
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200680 cs_rx_shut_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200681 if (ic->flags & CF_SHUTR)
682 return;
683 ic->flags |= CF_SHUTR;
684 ic->rex = TICK_ETERNITY;
685
686 if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
687 return;
688
689 if (cs_oc(cs)->flags & CF_SHUTW) {
Christopher Fauletff022a22022-04-21 08:38:54 +0200690 cs_conn_shut(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200691 cs->state = CS_ST_DIS;
692 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
693 }
694 else if (cs->flags & CS_FL_NOHALF) {
695 /* we want to immediately forward this close to the write side */
696 return cs_app_shutw_conn(cs);
697 }
698}
699
700/*
701 * This function performs a shutdown-write on a conn-stream attached to
702 * a connection in a connected or init state (it does nothing for other
703 * states). It either shuts the write side or marks itself as closed. The
704 * buffer flags are updated to reflect the new state. It does also close
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +0200705 * everything if the CS was marked as being in error state. If there is a
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200706 * data-layer shutdown, it is called.
707 */
708static void cs_app_shutw_conn(struct conn_stream *cs)
709{
710 struct channel *ic = cs_ic(cs);
711 struct channel *oc = cs_oc(cs);
712
713 BUG_ON(!cs_conn(cs));
714
715 oc->flags &= ~CF_SHUTW_NOW;
716 if (oc->flags & CF_SHUTW)
717 return;
718 oc->flags |= CF_SHUTW;
719 oc->wex = TICK_ETERNITY;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200720 cs_done_get(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200721
722 if (tick_isset(cs->hcto)) {
723 ic->rto = cs->hcto;
724 ic->rex = tick_add(now_ms, ic->rto);
725 }
726
727 switch (cs->state) {
728 case CS_ST_RDY:
729 case CS_ST_EST:
730 /* we have to shut before closing, otherwise some short messages
731 * may never leave the system, especially when there are remaining
732 * unread data in the socket input buffer, or when nolinger is set.
733 * However, if CS_FL_NOLINGER is explicitly set, we know there is
734 * no risk so we close both sides immediately.
735 */
736
737 if (cs->endp->flags & CS_EP_ERROR) {
738 /* quick close, the socket is already shut anyway */
739 }
740 else if (cs->flags & CS_FL_NOLINGER) {
741 /* unclean data-layer shutdown, typically an aborted request
742 * or a forwarded shutdown from a client to a server due to
743 * option abortonclose. No need for the TLS layer to try to
744 * emit a shutdown message.
745 */
746 cs_conn_shutw(cs, CO_SHW_SILENT);
747 }
748 else {
749 /* clean data-layer shutdown. This only happens on the
750 * frontend side, or on the backend side when forwarding
751 * a client close in TCP mode or in HTTP TUNNEL mode
752 * while option abortonclose is set. We want the TLS
753 * layer to try to signal it to the peer before we close.
754 */
755 cs_conn_shutw(cs, CO_SHW_NORMAL);
756
757 if (!(ic->flags & (CF_SHUTR|CF_DONT_READ)))
758 return;
759 }
760
761 /* fall through */
762 case CS_ST_CON:
763 /* we may have to close a pending connection, and mark the
764 * response buffer as shutr
765 */
Christopher Fauletff022a22022-04-21 08:38:54 +0200766 cs_conn_shut(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200767 /* fall through */
768 case CS_ST_CER:
769 case CS_ST_QUE:
770 case CS_ST_TAR:
771 cs->state = CS_ST_DIS;
772 /* fall through */
773 default:
774 cs->flags &= ~CS_FL_NOLINGER;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200775 cs_rx_shut_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200776 ic->flags |= CF_SHUTR;
777 ic->rex = TICK_ETERNITY;
778 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
779 }
780}
781
782/* This function is used for inter-conn-stream calls. It is called by the
783 * consumer to inform the producer side that it may be interested in checking
784 * for free space in the buffer. Note that it intentionally does not update
785 * timeouts, so that we can still check them later at wake-up. This function is
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +0200786 * dedicated to connection-based conn-streams.
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200787 */
788static void cs_app_chk_rcv_conn(struct conn_stream *cs)
789{
790 BUG_ON(!cs_conn(cs));
791
792 /* (re)start reading */
793 if (cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
794 tasklet_wakeup(cs->wait_event.tasklet);
795}
796
797
798/* This function is used for inter-conn-stream calls. It is called by the
799 * producer to inform the consumer side that it may be interested in checking
800 * for data in the buffer. Note that it intentionally does not update timeouts,
801 * so that we can still check them later at wake-up.
802 */
803static void cs_app_chk_snd_conn(struct conn_stream *cs)
804{
805 struct channel *oc = cs_oc(cs);
806
807 BUG_ON(!cs_conn(cs));
808
Willy Tarreau4173f4e2022-04-29 15:04:41 +0200809 if (unlikely(!cs_state_in(cs->state, CS_SB_RDY|CS_SB_EST) ||
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200810 (oc->flags & CF_SHUTW)))
811 return;
812
813 if (unlikely(channel_is_empty(oc))) /* called with nothing to send ! */
814 return;
815
816 if (!oc->pipe && /* spliced data wants to be forwarded ASAP */
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200817 !(cs->endp->flags & CS_EP_WAIT_DATA)) /* not waiting for data */
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200818 return;
819
820 if (!(cs->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(cs_oc(cs)))
Christopher Faulet000ba3e2022-04-01 17:06:32 +0200821 cs_conn_send(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200822
Christopher Faulet158f3362022-04-01 17:15:10 +0200823 if (cs->endp->flags & (CS_EP_ERROR|CS_EP_ERR_PENDING) || cs_is_conn_error(cs)) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200824 /* Write error on the file descriptor */
825 if (cs->state >= CS_ST_CON)
826 cs->endp->flags |= CS_EP_ERROR;
827 goto out_wakeup;
828 }
829
830 /* OK, so now we know that some data might have been sent, and that we may
831 * have to poll first. We have to do that too if the buffer is not empty.
832 */
833 if (channel_is_empty(oc)) {
834 /* the connection is established but we can't write. Either the
835 * buffer is empty, or we just refrain from sending because the
836 * ->o limit was reached. Maybe we just wrote the last
837 * chunk and need to close.
838 */
839 if (((oc->flags & (CF_SHUTW|CF_AUTO_CLOSE|CF_SHUTW_NOW)) ==
840 (CF_AUTO_CLOSE|CF_SHUTW_NOW)) &&
841 cs_state_in(cs->state, CS_SB_RDY|CS_SB_EST)) {
842 cs_shutw(cs);
843 goto out_wakeup;
844 }
845
846 if ((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == 0)
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200847 cs->endp->flags |= CS_EP_WAIT_DATA;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200848 oc->wex = TICK_ETERNITY;
849 }
850 else {
851 /* Otherwise there are remaining data to be sent in the buffer,
852 * which means we have to poll before doing so.
853 */
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200854 cs->endp->flags &= ~CS_EP_WAIT_DATA;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200855 if (!tick_isset(oc->wex))
856 oc->wex = tick_add_ifset(now_ms, oc->wto);
857 }
858
859 if (likely(oc->flags & CF_WRITE_ACTIVITY)) {
860 struct channel *ic = cs_ic(cs);
861
862 /* update timeout if we have written something */
863 if ((oc->flags & (CF_SHUTW|CF_WRITE_PARTIAL)) == CF_WRITE_PARTIAL &&
864 !channel_is_empty(oc))
865 oc->wex = tick_add_ifset(now_ms, oc->wto);
866
867 if (tick_isset(ic->rex) && !(cs->flags & CS_FL_INDEP_STR)) {
868 /* Note: to prevent the client from expiring read timeouts
869 * during writes, we refresh it. We only do this if the
870 * interface is not configured for "independent streams",
871 * because for some applications it's better not to do this,
872 * for instance when continuously exchanging small amounts
873 * of data which can full the socket buffers long before a
874 * write timeout is detected.
875 */
876 ic->rex = tick_add_ifset(now_ms, ic->rto);
877 }
878 }
879
880 /* in case of special condition (error, shutdown, end of write...), we
881 * have to notify the task.
882 */
883 if (likely((oc->flags & (CF_WRITE_NULL|CF_WRITE_ERROR|CF_SHUTW)) ||
884 ((oc->flags & CF_WAKE_WRITE) &&
885 ((channel_is_empty(oc) && !oc->to_forward) ||
886 !cs_state_in(cs->state, CS_SB_EST))))) {
887 out_wakeup:
888 if (!(cs->flags & CS_FL_DONT_WAKE))
889 task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO);
890 }
891}
892
893/*
894 * This function performs a shutdown-read on a conn-stream attached to an
895 * applet in a connected or init state (it does nothing for other states). It
896 * either shuts the read side or marks itself as closed. The buffer flags are
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +0200897 * updated to reflect the new state. If the conn-stream has CS_FL_NOHALF,
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200898 * we also forward the close to the write side. The owner task is woken up if
899 * it exists.
900 */
901static void cs_app_shutr_applet(struct conn_stream *cs)
902{
903 struct channel *ic = cs_ic(cs);
904
905 BUG_ON(!cs_appctx(cs));
906
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200907 cs_rx_shut_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200908 if (ic->flags & CF_SHUTR)
909 return;
910 ic->flags |= CF_SHUTR;
911 ic->rex = TICK_ETERNITY;
912
913 /* Note: on shutr, we don't call the applet */
914
915 if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
916 return;
917
918 if (cs_oc(cs)->flags & CF_SHUTW) {
Christopher Fauletca6c9bb2022-04-21 08:44:09 +0200919 cs_applet_shut(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200920 cs->state = CS_ST_DIS;
921 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
922 }
923 else if (cs->flags & CS_FL_NOHALF) {
924 /* we want to immediately forward this close to the write side */
925 return cs_app_shutw_applet(cs);
926 }
927}
928
929/*
930 * This function performs a shutdown-write on a conn-stream attached to an
931 * applet in a connected or init state (it does nothing for other states). It
932 * either shuts the write side or marks itself as closed. The buffer flags are
933 * updated to reflect the new state. It does also close everything if the SI
934 * was marked as being in error state. The owner task is woken up if it exists.
935 */
936static void cs_app_shutw_applet(struct conn_stream *cs)
937{
938 struct channel *ic = cs_ic(cs);
939 struct channel *oc = cs_oc(cs);
940
941 BUG_ON(!cs_appctx(cs));
942
943 oc->flags &= ~CF_SHUTW_NOW;
944 if (oc->flags & CF_SHUTW)
945 return;
946 oc->flags |= CF_SHUTW;
947 oc->wex = TICK_ETERNITY;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200948 cs_done_get(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200949
950 if (tick_isset(cs->hcto)) {
951 ic->rto = cs->hcto;
952 ic->rex = tick_add(now_ms, ic->rto);
953 }
954
955 /* on shutw we always wake the applet up */
956 appctx_wakeup(__cs_appctx(cs));
957
958 switch (cs->state) {
959 case CS_ST_RDY:
960 case CS_ST_EST:
961 /* we have to shut before closing, otherwise some short messages
962 * may never leave the system, especially when there are remaining
963 * unread data in the socket input buffer, or when nolinger is set.
964 * However, if CS_FL_NOLINGER is explicitly set, we know there is
965 * no risk so we close both sides immediately.
966 */
967 if (!(cs->endp->flags & CS_EP_ERROR) && !(cs->flags & CS_FL_NOLINGER) &&
968 !(ic->flags & (CF_SHUTR|CF_DONT_READ)))
969 return;
970
971 /* fall through */
972 case CS_ST_CON:
973 case CS_ST_CER:
974 case CS_ST_QUE:
975 case CS_ST_TAR:
976 /* Note that none of these states may happen with applets */
Christopher Fauletca6c9bb2022-04-21 08:44:09 +0200977 cs_applet_shut(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200978 cs->state = CS_ST_DIS;
979 /* fall through */
980 default:
981 cs->flags &= ~CS_FL_NOLINGER;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200982 cs_rx_shut_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200983 ic->flags |= CF_SHUTR;
984 ic->rex = TICK_ETERNITY;
985 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
986 }
987}
988
989/* chk_rcv function for applets */
990static void cs_app_chk_rcv_applet(struct conn_stream *cs)
991{
992 struct channel *ic = cs_ic(cs);
993
994 BUG_ON(!cs_appctx(cs));
995
996 DPRINTF(stderr, "%s: cs=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n",
997 __FUNCTION__,
998 cs, cs->state, ic->flags, cs_oc(cs)->flags);
999
1000 if (!ic->pipe) {
1001 /* (re)start reading */
1002 appctx_wakeup(__cs_appctx(cs));
1003 }
1004}
1005
1006/* chk_snd function for applets */
1007static void cs_app_chk_snd_applet(struct conn_stream *cs)
1008{
1009 struct channel *oc = cs_oc(cs);
1010
1011 BUG_ON(!cs_appctx(cs));
1012
1013 DPRINTF(stderr, "%s: cs=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n",
1014 __FUNCTION__,
1015 cs, cs->state, cs_ic(cs)->flags, oc->flags);
1016
1017 if (unlikely(cs->state != CS_ST_EST || (oc->flags & CF_SHUTW)))
1018 return;
1019
1020 /* we only wake the applet up if it was waiting for some data */
1021
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001022 if (!(cs->endp->flags & CS_EP_WAIT_DATA))
Christopher Faulet9ffddd52022-04-01 14:04:29 +02001023 return;
1024
1025 if (!tick_isset(oc->wex))
1026 oc->wex = tick_add_ifset(now_ms, oc->wto);
1027
1028 if (!channel_is_empty(oc)) {
1029 /* (re)start sending */
1030 appctx_wakeup(__cs_appctx(cs));
1031 }
1032}
Christopher Faulet13045f02022-04-01 14:23:38 +02001033
1034
1035/* This function is designed to be called from within the stream handler to
1036 * update the input channel's expiration timer and the conn-stream's
1037 * Rx flags based on the channel's flags. It needs to be called only once
1038 * after the channel's flags have settled down, and before they are cleared,
1039 * though it doesn't harm to call it as often as desired (it just slightly
1040 * hurts performance). It must not be called from outside of the stream
1041 * handler, as what it does will be used to compute the stream task's
1042 * expiration.
1043 */
1044void cs_update_rx(struct conn_stream *cs)
1045{
1046 struct channel *ic = cs_ic(cs);
1047
1048 if (ic->flags & CF_SHUTR) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001049 cs_rx_shut_blk(cs);
Christopher Faulet13045f02022-04-01 14:23:38 +02001050 return;
1051 }
1052
1053 /* Read not closed, update FD status and timeout for reads */
1054 if (ic->flags & CF_DONT_READ)
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001055 cs_rx_chan_blk(cs);
Christopher Faulet13045f02022-04-01 14:23:38 +02001056 else
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001057 cs_rx_chan_rdy(cs);
Christopher Faulet13045f02022-04-01 14:23:38 +02001058
1059 if (!channel_is_empty(ic) || !channel_may_recv(ic)) {
1060 /* stop reading, imposed by channel's policy or contents */
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001061 cs_rx_room_blk(cs);
Christopher Faulet13045f02022-04-01 14:23:38 +02001062 }
1063 else {
1064 /* (re)start reading and update timeout. Note: we don't recompute the timeout
1065 * every time we get here, otherwise it would risk never to expire. We only
1066 * update it if is was not yet set. The stream socket handler will already
1067 * have updated it if there has been a completed I/O.
1068 */
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001069 cs_rx_room_rdy(cs);
Christopher Faulet13045f02022-04-01 14:23:38 +02001070 }
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001071 if (cs->endp->flags & CS_EP_RXBLK_ANY & ~CS_EP_RX_WAIT_EP)
Christopher Faulet13045f02022-04-01 14:23:38 +02001072 ic->rex = TICK_ETERNITY;
1073 else if (!(ic->flags & CF_READ_NOEXP) && !tick_isset(ic->rex))
1074 ic->rex = tick_add_ifset(now_ms, ic->rto);
1075
1076 cs_chk_rcv(cs);
1077}
1078
1079/* This function is designed to be called from within the stream handler to
1080 * update the output channel's expiration timer and the conn-stream's
1081 * Tx flags based on the channel's flags. It needs to be called only once
1082 * after the channel's flags have settled down, and before they are cleared,
1083 * though it doesn't harm to call it as often as desired (it just slightly
1084 * hurts performance). It must not be called from outside of the stream
1085 * handler, as what it does will be used to compute the stream task's
1086 * expiration.
1087 */
1088void cs_update_tx(struct conn_stream *cs)
1089{
1090 struct channel *oc = cs_oc(cs);
1091 struct channel *ic = cs_ic(cs);
1092
1093 if (oc->flags & CF_SHUTW)
1094 return;
1095
1096 /* Write not closed, update FD status and timeout for writes */
1097 if (channel_is_empty(oc)) {
1098 /* stop writing */
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001099 if (!(cs->endp->flags & CS_EP_WAIT_DATA)) {
Christopher Faulet13045f02022-04-01 14:23:38 +02001100 if ((oc->flags & CF_SHUTW_NOW) == 0)
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001101 cs->endp->flags |= CS_EP_WAIT_DATA;
Christopher Faulet13045f02022-04-01 14:23:38 +02001102 oc->wex = TICK_ETERNITY;
1103 }
1104 return;
1105 }
1106
1107 /* (re)start writing and update timeout. Note: we don't recompute the timeout
1108 * every time we get here, otherwise it would risk never to expire. We only
1109 * update it if is was not yet set. The stream socket handler will already
1110 * have updated it if there has been a completed I/O.
1111 */
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001112 cs->endp->flags &= ~CS_EP_WAIT_DATA;
Christopher Faulet13045f02022-04-01 14:23:38 +02001113 if (!tick_isset(oc->wex)) {
1114 oc->wex = tick_add_ifset(now_ms, oc->wto);
1115 if (tick_isset(ic->rex) && !(cs->flags & CS_FL_INDEP_STR)) {
1116 /* Note: depending on the protocol, we don't know if we're waiting
1117 * for incoming data or not. So in order to prevent the socket from
1118 * expiring read timeouts during writes, we refresh the read timeout,
1119 * except if it was already infinite or if we have explicitly setup
1120 * independent streams.
1121 */
1122 ic->rex = tick_add_ifset(now_ms, ic->rto);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001123 }
1124 }
1125}
1126
1127/* This function is the equivalent to cs_update() except that it's
1128 * designed to be called from outside the stream handlers, typically the lower
1129 * layers (applets, connections) after I/O completion. After updating the stream
1130 * interface and timeouts, it will try to forward what can be forwarded, then to
1131 * wake the associated task up if an important event requires special handling.
1132 * It may update CS_EP_WAIT_DATA and/or CS_EP_RXBLK_ROOM, that the callers are
1133 * encouraged to watch to take appropriate action.
1134 * It should not be called from within the stream itself, cs_update()
1135 * is designed for this.
1136 */
1137static void cs_notify(struct conn_stream *cs)
1138{
1139 struct channel *ic = cs_ic(cs);
1140 struct channel *oc = cs_oc(cs);
1141 struct conn_stream *cso = cs_opposite(cs);
1142 struct task *task = cs_strm_task(cs);
1143
1144 /* process consumer side */
1145 if (channel_is_empty(oc)) {
1146 struct connection *conn = cs_conn(cs);
1147
1148 if (((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW) &&
1149 (cs->state == CS_ST_EST) && (!conn || !(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS))))
1150 cs_shutw(cs);
1151 oc->wex = TICK_ETERNITY;
1152 }
1153
1154 /* indicate that we may be waiting for data from the output channel or
1155 * we're about to close and can't expect more data if SHUTW_NOW is there.
1156 */
1157 if (!(oc->flags & (CF_SHUTW|CF_SHUTW_NOW)))
1158 cs->endp->flags |= CS_EP_WAIT_DATA;
1159 else if ((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW)
1160 cs->endp->flags &= ~CS_EP_WAIT_DATA;
1161
1162 /* update OC timeouts and wake the other side up if it's waiting for room */
1163 if (oc->flags & CF_WRITE_ACTIVITY) {
1164 if ((oc->flags & (CF_SHUTW|CF_WRITE_PARTIAL)) == CF_WRITE_PARTIAL &&
1165 !channel_is_empty(oc))
1166 if (tick_isset(oc->wex))
1167 oc->wex = tick_add_ifset(now_ms, oc->wto);
1168
1169 if (!(cs->flags & CS_FL_INDEP_STR))
1170 if (tick_isset(ic->rex))
1171 ic->rex = tick_add_ifset(now_ms, ic->rto);
1172 }
1173
1174 if (oc->flags & CF_DONT_READ)
1175 cs_rx_chan_blk(cso);
1176 else
1177 cs_rx_chan_rdy(cso);
1178
1179 /* Notify the other side when we've injected data into the IC that
1180 * needs to be forwarded. We can do fast-forwarding as soon as there
1181 * are output data, but we avoid doing this if some of the data are
1182 * not yet scheduled for being forwarded, because it is very likely
1183 * that it will be done again immediately afterwards once the following
1184 * data are parsed (eg: HTTP chunking). We only CS_EP_RXBLK_ROOM once
1185 * we've emptied *some* of the output buffer, and not just when there
1186 * is available room, because applets are often forced to stop before
1187 * the buffer is full. We must not stop based on input data alone because
1188 * an HTTP parser might need more data to complete the parsing.
1189 */
1190 if (!channel_is_empty(ic) &&
1191 (cso->endp->flags & CS_EP_WAIT_DATA) &&
1192 (!(ic->flags & CF_EXPECT_MORE) || c_full(ic) || ci_data(ic) == 0 || ic->pipe)) {
1193 int new_len, last_len;
1194
1195 last_len = co_data(ic);
1196 if (ic->pipe)
1197 last_len += ic->pipe->data;
1198
1199 cs_chk_snd(cso);
1200
1201 new_len = co_data(ic);
1202 if (ic->pipe)
1203 new_len += ic->pipe->data;
1204
1205 /* check if the consumer has freed some space either in the
1206 * buffer or in the pipe.
1207 */
1208 if (new_len < last_len)
1209 cs_rx_room_rdy(cs);
1210 }
1211
1212 if (!(ic->flags & CF_DONT_READ))
1213 cs_rx_chan_rdy(cs);
1214
1215 cs_chk_rcv(cs);
1216 cs_chk_rcv(cso);
1217
1218 if (cs_rx_blocked(cs)) {
1219 ic->rex = TICK_ETERNITY;
1220 }
1221 else if ((ic->flags & (CF_SHUTR|CF_READ_PARTIAL)) == CF_READ_PARTIAL) {
1222 /* we must re-enable reading if cs_chk_snd() has freed some space */
1223 if (!(ic->flags & CF_READ_NOEXP) && tick_isset(ic->rex))
1224 ic->rex = tick_add_ifset(now_ms, ic->rto);
1225 }
1226
1227 /* wake the task up only when needed */
1228 if (/* changes on the production side */
1229 (ic->flags & (CF_READ_NULL|CF_READ_ERROR)) ||
1230 !cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST) ||
1231 (cs->endp->flags & CS_EP_ERROR) ||
1232 ((ic->flags & CF_READ_PARTIAL) &&
1233 ((ic->flags & CF_EOI) || !ic->to_forward || cso->state != CS_ST_EST)) ||
1234
1235 /* changes on the consumption side */
1236 (oc->flags & (CF_WRITE_NULL|CF_WRITE_ERROR)) ||
1237 ((oc->flags & CF_WRITE_ACTIVITY) &&
1238 ((oc->flags & CF_SHUTW) ||
1239 (((oc->flags & CF_WAKE_WRITE) ||
1240 !(oc->flags & (CF_AUTO_CLOSE|CF_SHUTW_NOW|CF_SHUTW))) &&
1241 (cso->state != CS_ST_EST ||
1242 (channel_is_empty(oc) && !oc->to_forward)))))) {
1243 task_wakeup(task, TASK_WOKEN_IO);
1244 }
1245 else {
1246 /* Update expiration date for the task and requeue it */
1247 task->expire = tick_first((tick_is_expired(task->expire, now_ms) ? 0 : task->expire),
1248 tick_first(tick_first(ic->rex, ic->wex),
1249 tick_first(oc->rex, oc->wex)));
1250
1251 task->expire = tick_first(task->expire, ic->analyse_exp);
1252 task->expire = tick_first(task->expire, oc->analyse_exp);
1253 task->expire = tick_first(task->expire, __cs_strm(cs)->conn_exp);
1254
1255 task_queue(task);
1256 }
1257 if (ic->flags & CF_READ_ACTIVITY)
1258 ic->flags &= ~CF_READ_DONTWAIT;
1259}
1260
1261/*
1262 * This function propagates a null read received on a socket-based connection.
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001263 * It updates the conn-stream. If the conn-stream has CS_FL_NOHALF,
Christopher Faulet5e29b762022-04-04 08:58:34 +02001264 * the close is also forwarded to the write side as an abort.
1265 */
1266static void cs_conn_read0(struct conn_stream *cs)
1267{
1268 struct channel *ic = cs_ic(cs);
1269 struct channel *oc = cs_oc(cs);
1270
1271 BUG_ON(!cs_conn(cs));
1272
1273 cs_rx_shut_blk(cs);
1274 if (ic->flags & CF_SHUTR)
1275 return;
1276 ic->flags |= CF_SHUTR;
1277 ic->rex = TICK_ETERNITY;
1278
1279 if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
1280 return;
1281
1282 if (oc->flags & CF_SHUTW)
1283 goto do_close;
1284
1285 if (cs->flags & CS_FL_NOHALF) {
1286 /* we want to immediately forward this close to the write side */
1287 /* force flag on ssl to keep stream in cache */
1288 cs_conn_shutw(cs, CO_SHW_SILENT);
1289 goto do_close;
1290 }
1291
1292 /* otherwise that's just a normal read shutdown */
1293 return;
1294
1295 do_close:
1296 /* OK we completely close the socket here just as if we went through cs_shut[rw]() */
Christopher Fauletff022a22022-04-21 08:38:54 +02001297 cs_conn_shut(cs);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001298
1299 oc->flags &= ~CF_SHUTW_NOW;
1300 oc->flags |= CF_SHUTW;
1301 oc->wex = TICK_ETERNITY;
1302
1303 cs_done_get(cs);
1304
1305 cs->state = CS_ST_DIS;
1306 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
1307 return;
1308}
1309
1310/*
1311 * This is the callback which is called by the connection layer to receive data
1312 * into the buffer from the connection. It iterates over the mux layer's
1313 * rcv_buf function.
1314 */
1315static int cs_conn_recv(struct conn_stream *cs)
1316{
1317 struct connection *conn = __cs_conn(cs);
1318 struct channel *ic = cs_ic(cs);
1319 int ret, max, cur_read = 0;
1320 int read_poll = MAX_READ_POLL_LOOPS;
1321 int flags = 0;
1322
1323 /* If not established yet, do nothing. */
1324 if (cs->state != CS_ST_EST)
1325 return 0;
1326
1327 /* If another call to cs_conn_recv() failed, and we subscribed to
1328 * recv events already, give up now.
1329 */
1330 if (cs->wait_event.events & SUB_RETRY_RECV)
1331 return 0;
1332
1333 /* maybe we were called immediately after an asynchronous shutr */
1334 if (ic->flags & CF_SHUTR)
1335 return 1;
1336
1337 /* we must wait because the mux is not installed yet */
1338 if (!conn->mux)
1339 return 0;
1340
1341 /* stop here if we reached the end of data */
1342 if (cs->endp->flags & CS_EP_EOS)
1343 goto end_recv;
1344
1345 /* stop immediately on errors. Note that we DON'T want to stop on
1346 * POLL_ERR, as the poller might report a write error while there
1347 * are still data available in the recv buffer. This typically
1348 * happens when we send too large a request to a backend server
1349 * which rejects it before reading it all.
1350 */
1351 if (!(cs->endp->flags & CS_EP_RCV_MORE)) {
1352 if (!conn_xprt_ready(conn))
1353 return 0;
1354 if (cs->endp->flags & CS_EP_ERROR)
1355 goto end_recv;
1356 }
1357
1358 /* prepare to detect if the mux needs more room */
1359 cs->endp->flags &= ~CS_EP_WANT_ROOM;
1360
1361 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) && !co_data(ic) &&
1362 global.tune.idle_timer &&
1363 (unsigned short)(now_ms - ic->last_read) >= global.tune.idle_timer) {
1364 /* The buffer was empty and nothing was transferred for more
1365 * than one second. This was caused by a pause and not by
1366 * congestion. Reset any streaming mode to reduce latency.
1367 */
1368 ic->xfer_small = 0;
1369 ic->xfer_large = 0;
1370 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
1371 }
1372
1373 /* First, let's see if we may splice data across the channel without
1374 * using a buffer.
1375 */
1376 if (cs->endp->flags & CS_EP_MAY_SPLICE &&
1377 (ic->pipe || ic->to_forward >= MIN_SPLICE_FORWARD) &&
1378 ic->flags & CF_KERN_SPLICING) {
1379 if (c_data(ic)) {
1380 /* We're embarrassed, there are already data pending in
1381 * the buffer and we don't want to have them at two
1382 * locations at a time. Let's indicate we need some
1383 * place and ask the consumer to hurry.
1384 */
1385 flags |= CO_RFL_BUF_FLUSH;
1386 goto abort_splice;
1387 }
1388
1389 if (unlikely(ic->pipe == NULL)) {
1390 if (pipes_used >= global.maxpipes || !(ic->pipe = get_pipe())) {
1391 ic->flags &= ~CF_KERN_SPLICING;
1392 goto abort_splice;
1393 }
1394 }
1395
1396 ret = conn->mux->rcv_pipe(cs, ic->pipe, ic->to_forward);
1397 if (ret < 0) {
1398 /* splice not supported on this end, let's disable it */
1399 ic->flags &= ~CF_KERN_SPLICING;
1400 goto abort_splice;
1401 }
1402
1403 if (ret > 0) {
1404 if (ic->to_forward != CHN_INFINITE_FORWARD)
1405 ic->to_forward -= ret;
1406 ic->total += ret;
1407 cur_read += ret;
1408 ic->flags |= CF_READ_PARTIAL;
1409 }
1410
1411 if (cs->endp->flags & (CS_EP_EOS|CS_EP_ERROR))
1412 goto end_recv;
1413
1414 if (conn->flags & CO_FL_WAIT_ROOM) {
1415 /* the pipe is full or we have read enough data that it
1416 * could soon be full. Let's stop before needing to poll.
1417 */
1418 cs_rx_room_blk(cs);
1419 goto done_recv;
1420 }
1421
1422 /* splice not possible (anymore), let's go on on standard copy */
1423 }
1424
1425 abort_splice:
1426 if (ic->pipe && unlikely(!ic->pipe->data)) {
1427 put_pipe(ic->pipe);
1428 ic->pipe = NULL;
1429 }
1430
1431 if (ic->pipe && ic->to_forward && !(flags & CO_RFL_BUF_FLUSH) && cs->endp->flags & CS_EP_MAY_SPLICE) {
1432 /* don't break splicing by reading, but still call rcv_buf()
1433 * to pass the flag.
1434 */
1435 goto done_recv;
1436 }
1437
1438 /* now we'll need a input buffer for the stream */
1439 if (!cs_alloc_ibuf(cs, &(__cs_strm(cs)->buffer_wait)))
1440 goto end_recv;
1441
1442 /* For an HTX stream, if the buffer is stuck (no output data with some
1443 * input data) and if the HTX message is fragmented or if its free space
1444 * wraps, we force an HTX deframentation. It is a way to have a
1445 * contiguous free space nad to let the mux to copy as much data as
1446 * possible.
1447 *
1448 * NOTE: A possible optim may be to let the mux decides if defrag is
1449 * required or not, depending on amount of data to be xferred.
1450 */
1451 if (IS_HTX_STRM(__cs_strm(cs)) && !co_data(ic)) {
1452 struct htx *htx = htxbuf(&ic->buf);
1453
1454 if (htx_is_not_empty(htx) && ((htx->flags & HTX_FL_FRAGMENTED) || htx_space_wraps(htx)))
1455 htx_defrag(htx, NULL, 0);
1456 }
1457
1458 /* Instruct the mux it must subscribed for read events */
1459 flags |= ((!conn_is_back(conn) && (__cs_strm(cs)->be->options & PR_O_ABRT_CLOSE)) ? CO_RFL_KEEP_RECV : 0);
1460
1461 /* Important note : if we're called with POLL_IN|POLL_HUP, it means the read polling
1462 * was enabled, which implies that the recv buffer was not full. So we have a guarantee
1463 * that if such an event is not handled above in splice, it will be handled here by
1464 * recv().
1465 */
1466 while ((cs->endp->flags & CS_EP_RCV_MORE) ||
1467 (!(conn->flags & CO_FL_HANDSHAKE) &&
1468 (!(cs->endp->flags & (CS_EP_ERROR|CS_EP_EOS))) && !(ic->flags & CF_SHUTR))) {
1469 int cur_flags = flags;
1470
1471 /* Compute transient CO_RFL_* flags */
1472 if (co_data(ic)) {
1473 cur_flags |= (CO_RFL_BUF_WET | CO_RFL_BUF_NOT_STUCK);
1474 }
1475
1476 /* <max> may be null. This is the mux responsibility to set
1477 * CS_EP_RCV_MORE on the CS if more space is needed.
1478 */
1479 max = channel_recv_max(ic);
1480 ret = conn->mux->rcv_buf(cs, &ic->buf, max, cur_flags);
1481
1482 if (cs->endp->flags & CS_EP_WANT_ROOM) {
1483 /* CS_EP_WANT_ROOM must not be reported if the channel's
1484 * buffer is empty.
1485 */
1486 BUG_ON(c_empty(ic));
1487
1488 cs_rx_room_blk(cs);
1489 /* Add READ_PARTIAL because some data are pending but
1490 * cannot be xferred to the channel
1491 */
1492 ic->flags |= CF_READ_PARTIAL;
1493 }
1494
1495 if (ret <= 0) {
1496 /* if we refrained from reading because we asked for a
1497 * flush to satisfy rcv_pipe(), we must not subscribe
1498 * and instead report that there's not enough room
1499 * here to proceed.
1500 */
1501 if (flags & CO_RFL_BUF_FLUSH)
1502 cs_rx_room_blk(cs);
1503 break;
1504 }
1505
1506 cur_read += ret;
1507
1508 /* if we're allowed to directly forward data, we must update ->o */
1509 if (ic->to_forward && !(ic->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
1510 unsigned long fwd = ret;
1511 if (ic->to_forward != CHN_INFINITE_FORWARD) {
1512 if (fwd > ic->to_forward)
1513 fwd = ic->to_forward;
1514 ic->to_forward -= fwd;
1515 }
1516 c_adv(ic, fwd);
1517 }
1518
1519 ic->flags |= CF_READ_PARTIAL;
1520 ic->total += ret;
1521
1522 /* End-of-input reached, we can leave. In this case, it is
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001523 * important to break the loop to not block the CS because of
Christopher Faulet5e29b762022-04-04 08:58:34 +02001524 * the channel's policies.This way, we are still able to receive
1525 * shutdowns.
1526 */
1527 if (cs->endp->flags & CS_EP_EOI)
1528 break;
1529
1530 if ((ic->flags & CF_READ_DONTWAIT) || --read_poll <= 0) {
1531 /* we're stopped by the channel's policy */
1532 cs_rx_chan_blk(cs);
1533 break;
1534 }
1535
1536 /* if too many bytes were missing from last read, it means that
1537 * it's pointless trying to read again because the system does
1538 * not have them in buffers.
1539 */
1540 if (ret < max) {
1541 /* if a streamer has read few data, it may be because we
1542 * have exhausted system buffers. It's not worth trying
1543 * again.
1544 */
1545 if (ic->flags & CF_STREAMER) {
1546 /* we're stopped by the channel's policy */
1547 cs_rx_chan_blk(cs);
1548 break;
1549 }
1550
1551 /* if we read a large block smaller than what we requested,
1552 * it's almost certain we'll never get anything more.
1553 */
1554 if (ret >= global.tune.recv_enough) {
1555 /* we're stopped by the channel's policy */
1556 cs_rx_chan_blk(cs);
1557 break;
1558 }
1559 }
1560
1561 /* if we are waiting for more space, don't try to read more data
1562 * right now.
1563 */
1564 if (cs_rx_blocked(cs))
1565 break;
1566 } /* while !flags */
1567
1568 done_recv:
1569 if (cur_read) {
1570 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) &&
1571 (cur_read <= ic->buf.size / 2)) {
1572 ic->xfer_large = 0;
1573 ic->xfer_small++;
1574 if (ic->xfer_small >= 3) {
1575 /* we have read less than half of the buffer in
1576 * one pass, and this happened at least 3 times.
1577 * This is definitely not a streamer.
1578 */
1579 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
1580 }
1581 else if (ic->xfer_small >= 2) {
1582 /* if the buffer has been at least half full twice,
1583 * we receive faster than we send, so at least it
1584 * is not a "fast streamer".
1585 */
1586 ic->flags &= ~CF_STREAMER_FAST;
1587 }
1588 }
1589 else if (!(ic->flags & CF_STREAMER_FAST) &&
1590 (cur_read >= ic->buf.size - global.tune.maxrewrite)) {
1591 /* we read a full buffer at once */
1592 ic->xfer_small = 0;
1593 ic->xfer_large++;
1594 if (ic->xfer_large >= 3) {
1595 /* we call this buffer a fast streamer if it manages
1596 * to be filled in one call 3 consecutive times.
1597 */
1598 ic->flags |= (CF_STREAMER | CF_STREAMER_FAST);
1599 }
1600 }
1601 else {
1602 ic->xfer_small = 0;
1603 ic->xfer_large = 0;
1604 }
1605 ic->last_read = now_ms;
1606 }
1607
1608 end_recv:
1609 ret = (cur_read != 0);
1610
1611 /* Report EOI on the channel if it was reached from the mux point of
1612 * view. */
1613 if ((cs->endp->flags & CS_EP_EOI) && !(ic->flags & CF_EOI)) {
1614 ic->flags |= (CF_EOI|CF_READ_PARTIAL);
1615 ret = 1;
1616 }
1617
1618 if (cs->endp->flags & CS_EP_ERROR)
1619 ret = 1;
1620 else if (cs->endp->flags & CS_EP_EOS) {
1621 /* we received a shutdown */
1622 ic->flags |= CF_READ_NULL;
1623 if (ic->flags & CF_AUTO_CLOSE)
1624 channel_shutw_now(ic);
1625 cs_conn_read0(cs);
1626 ret = 1;
1627 }
1628 else if (!cs_rx_blocked(cs)) {
1629 /* Subscribe to receive events if we're blocking on I/O */
1630 conn->mux->subscribe(cs, SUB_RETRY_RECV, &cs->wait_event);
1631 cs_rx_endp_done(cs);
1632 } else {
1633 cs_rx_endp_more(cs);
1634 ret = 1;
1635 }
1636 return ret;
1637}
1638
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001639/* This tries to perform a synchronous receive on the conn-stream to
Christopher Faulet5e29b762022-04-04 08:58:34 +02001640 * try to collect last arrived data. In practice it's only implemented on
1641 * conn_streams. Returns 0 if nothing was done, non-zero if new data or a
1642 * shutdown were collected. This may result on some delayed receive calls
1643 * to be programmed and performed later, though it doesn't provide any
1644 * such guarantee.
1645 */
1646int cs_conn_sync_recv(struct conn_stream *cs)
1647{
1648 if (!cs_state_in(cs->state, CS_SB_RDY|CS_SB_EST))
1649 return 0;
1650
1651 if (!cs_conn_mux(cs))
1652 return 0; // only conn_streams are supported
1653
1654 if (cs->wait_event.events & SUB_RETRY_RECV)
1655 return 0; // already subscribed
1656
1657 if (!cs_rx_endp_ready(cs) || cs_rx_blocked(cs))
1658 return 0; // already failed
1659
1660 return cs_conn_recv(cs);
1661}
1662
1663/*
1664 * This function is called to send buffer data to a stream socket.
1665 * It calls the mux layer's snd_buf function. It relies on the
1666 * caller to commit polling changes. The caller should check conn->flags
1667 * for errors.
1668 */
1669static int cs_conn_send(struct conn_stream *cs)
1670{
1671 struct connection *conn = __cs_conn(cs);
1672 struct stream *s = __cs_strm(cs);
1673 struct channel *oc = cs_oc(cs);
1674 int ret;
1675 int did_send = 0;
1676
1677 if (cs->endp->flags & (CS_EP_ERROR|CS_EP_ERR_PENDING) || cs_is_conn_error(cs)) {
1678 /* We're probably there because the tasklet was woken up,
1679 * but process_stream() ran before, detected there were an
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001680 * error and put the CS back to CS_ST_TAR. There's still
Christopher Faulet5e29b762022-04-04 08:58:34 +02001681 * CO_FL_ERROR on the connection but we don't want to add
1682 * CS_EP_ERROR back, so give up
1683 */
1684 if (cs->state < CS_ST_CON)
1685 return 0;
1686 cs->endp->flags |= CS_EP_ERROR;
1687 return 1;
1688 }
1689
1690 /* We're already waiting to be able to send, give up */
1691 if (cs->wait_event.events & SUB_RETRY_SEND)
1692 return 0;
1693
1694 /* we might have been called just after an asynchronous shutw */
1695 if (oc->flags & CF_SHUTW)
1696 return 1;
1697
1698 /* we must wait because the mux is not installed yet */
1699 if (!conn->mux)
1700 return 0;
1701
1702 if (oc->pipe && conn->xprt->snd_pipe && conn->mux->snd_pipe) {
1703 ret = conn->mux->snd_pipe(cs, oc->pipe);
1704 if (ret > 0)
1705 did_send = 1;
1706
1707 if (!oc->pipe->data) {
1708 put_pipe(oc->pipe);
1709 oc->pipe = NULL;
1710 }
1711
1712 if (oc->pipe)
1713 goto end;
1714 }
1715
1716 /* At this point, the pipe is empty, but we may still have data pending
1717 * in the normal buffer.
1718 */
1719 if (co_data(oc)) {
1720 /* when we're here, we already know that there is no spliced
1721 * data left, and that there are sendable buffered data.
1722 */
1723
1724 /* check if we want to inform the kernel that we're interested in
1725 * sending more data after this call. We want this if :
1726 * - we're about to close after this last send and want to merge
1727 * the ongoing FIN with the last segment.
1728 * - we know we can't send everything at once and must get back
1729 * here because of unaligned data
1730 * - there is still a finite amount of data to forward
1731 * The test is arranged so that the most common case does only 2
1732 * tests.
1733 */
1734 unsigned int send_flag = 0;
1735
1736 if ((!(oc->flags & (CF_NEVER_WAIT|CF_SEND_DONTWAIT)) &&
1737 ((oc->to_forward && oc->to_forward != CHN_INFINITE_FORWARD) ||
1738 (oc->flags & CF_EXPECT_MORE) ||
1739 (IS_HTX_STRM(s) &&
1740 (!(oc->flags & (CF_EOI|CF_SHUTR)) && htx_expect_more(htxbuf(&oc->buf)))))) ||
1741 ((oc->flags & CF_ISRESP) &&
1742 ((oc->flags & (CF_AUTO_CLOSE|CF_SHUTW_NOW)) == (CF_AUTO_CLOSE|CF_SHUTW_NOW))))
1743 send_flag |= CO_SFL_MSG_MORE;
1744
1745 if (oc->flags & CF_STREAMER)
1746 send_flag |= CO_SFL_STREAMER;
1747
1748 if (s->txn && s->txn->flags & TX_L7_RETRY && !b_data(&s->txn->l7_buffer)) {
1749 /* If we want to be able to do L7 retries, copy
1750 * the data we're about to send, so that we are able
1751 * to resend them if needed
1752 */
1753 /* Try to allocate a buffer if we had none.
1754 * If it fails, the next test will just
1755 * disable the l7 retries by setting
1756 * l7_conn_retries to 0.
1757 */
1758 if (s->txn->req.msg_state != HTTP_MSG_DONE)
1759 s->txn->flags &= ~TX_L7_RETRY;
1760 else {
1761 if (b_alloc(&s->txn->l7_buffer) == NULL)
1762 s->txn->flags &= ~TX_L7_RETRY;
1763 else {
1764 memcpy(b_orig(&s->txn->l7_buffer),
1765 b_orig(&oc->buf),
1766 b_size(&oc->buf));
1767 s->txn->l7_buffer.head = co_data(oc);
1768 b_add(&s->txn->l7_buffer, co_data(oc));
1769 }
1770
1771 }
1772 }
1773
1774 ret = conn->mux->snd_buf(cs, &oc->buf, co_data(oc), send_flag);
1775 if (ret > 0) {
1776 did_send = 1;
1777 c_rew(oc, ret);
1778 c_realign_if_empty(oc);
1779
1780 if (!co_data(oc)) {
1781 /* Always clear both flags once everything has been sent, they're one-shot */
1782 oc->flags &= ~(CF_EXPECT_MORE | CF_SEND_DONTWAIT);
1783 }
1784 /* if some data remain in the buffer, it's only because the
1785 * system buffers are full, we will try next time.
1786 */
Christopher Faulet13045f02022-04-01 14:23:38 +02001787 }
1788 }
Christopher Faulet5e29b762022-04-04 08:58:34 +02001789
1790 end:
1791 if (did_send) {
1792 oc->flags |= CF_WRITE_PARTIAL | CF_WROTE_DATA;
1793 if (cs->state == CS_ST_CON)
1794 cs->state = CS_ST_RDY;
1795
1796 cs_rx_room_rdy(cs_opposite(cs));
1797 }
1798
1799 if (cs->endp->flags & (CS_EP_ERROR|CS_EP_ERR_PENDING)) {
1800 cs->endp->flags |= CS_EP_ERROR;
1801 return 1;
1802 }
1803
1804 /* We couldn't send all of our data, let the mux know we'd like to send more */
1805 if (!channel_is_empty(oc))
1806 conn->mux->subscribe(cs, SUB_RETRY_SEND, &cs->wait_event);
1807 return did_send;
1808}
1809
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001810/* perform a synchronous send() for the conn-stream. The CF_WRITE_NULL and
Christopher Faulet5e29b762022-04-04 08:58:34 +02001811 * CF_WRITE_PARTIAL flags are cleared prior to the attempt, and will possibly
1812 * be updated in case of success.
1813 */
1814void cs_conn_sync_send(struct conn_stream *cs)
1815{
1816 struct channel *oc = cs_oc(cs);
1817
1818 oc->flags &= ~(CF_WRITE_NULL|CF_WRITE_PARTIAL);
1819
1820 if (oc->flags & CF_SHUTW)
1821 return;
1822
1823 if (channel_is_empty(oc))
1824 return;
1825
1826 if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
1827 return;
1828
1829 if (!cs_conn_mux(cs))
1830 return;
1831
1832 cs_conn_send(cs);
1833}
1834
1835/* Called by I/O handlers after completion.. It propagates
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001836 * connection flags to the conn-stream, updates the stream (which may or
Christopher Faulet5e29b762022-04-04 08:58:34 +02001837 * may not take this opportunity to try to forward data), then update the
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001838 * connection's polling based on the channels and conn-stream's final
Christopher Faulet5e29b762022-04-04 08:58:34 +02001839 * states. The function always returns 0.
1840 */
1841static int cs_conn_process(struct conn_stream *cs)
1842{
1843 struct connection *conn = __cs_conn(cs);
1844 struct channel *ic = cs_ic(cs);
1845 struct channel *oc = cs_oc(cs);
1846
1847 BUG_ON(!conn);
1848
1849 /* If we have data to send, try it now */
1850 if (!channel_is_empty(oc) && !(cs->wait_event.events & SUB_RETRY_SEND))
1851 cs_conn_send(cs);
1852
1853 /* First step, report to the conn-stream what was detected at the
1854 * connection layer : errors and connection establishment.
1855 * Only add CS_EP_ERROR if we're connected, or we're attempting to
1856 * connect, we may get there because we got woken up, but only run
1857 * after process_stream() noticed there were an error, and decided
1858 * to retry to connect, the connection may still have CO_FL_ERROR,
1859 * and we don't want to add CS_EP_ERROR back
1860 *
1861 * Note: This test is only required because cs_conn_process is also the SI
1862 * wake callback. Otherwise cs_conn_recv()/cs_conn_send() already take
1863 * care of it.
1864 */
1865
1866 if (cs->state >= CS_ST_CON) {
1867 if (cs_is_conn_error(cs))
1868 cs->endp->flags |= CS_EP_ERROR;
1869 }
1870
1871 /* If we had early data, and the handshake ended, then
1872 * we can remove the flag, and attempt to wake the task up,
1873 * in the event there's an analyser waiting for the end of
1874 * the handshake.
1875 */
1876 if (!(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS)) &&
1877 (cs->endp->flags & CS_EP_WAIT_FOR_HS)) {
1878 cs->endp->flags &= ~CS_EP_WAIT_FOR_HS;
1879 task_wakeup(cs_strm_task(cs), TASK_WOKEN_MSG);
1880 }
1881
1882 if (!cs_state_in(cs->state, CS_SB_EST|CS_SB_DIS|CS_SB_CLO) &&
1883 (conn->flags & CO_FL_WAIT_XPRT) == 0) {
1884 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
1885 oc->flags |= CF_WRITE_NULL;
1886 if (cs->state == CS_ST_CON)
1887 cs->state = CS_ST_RDY;
1888 }
1889
1890 /* Report EOS on the channel if it was reached from the mux point of
1891 * view.
1892 *
1893 * Note: This test is only required because cs_conn_process is also the SI
1894 * wake callback. Otherwise cs_conn_recv()/cs_conn_send() already take
1895 * care of it.
1896 */
1897 if (cs->endp->flags & CS_EP_EOS && !(ic->flags & CF_SHUTR)) {
1898 /* we received a shutdown */
1899 ic->flags |= CF_READ_NULL;
1900 if (ic->flags & CF_AUTO_CLOSE)
1901 channel_shutw_now(ic);
1902 cs_conn_read0(cs);
1903 }
1904
1905 /* Report EOI on the channel if it was reached from the mux point of
1906 * view.
1907 *
1908 * Note: This test is only required because cs_conn_process is also the SI
1909 * wake callback. Otherwise cs_conn_recv()/cs_conn_send() already take
1910 * care of it.
1911 */
1912 if ((cs->endp->flags & CS_EP_EOI) && !(ic->flags & CF_EOI))
1913 ic->flags |= (CF_EOI|CF_READ_PARTIAL);
1914
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001915 /* Second step : update the conn-stream and channels, try to forward any
Christopher Faulet5e29b762022-04-04 08:58:34 +02001916 * pending data, then possibly wake the stream up based on the new
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001917 * conn-stream status.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001918 */
1919 cs_notify(cs);
1920 stream_release_buffers(__cs_strm(cs));
1921 return 0;
1922}
1923
1924/* This is the ->process() function for any conn-stream's wait_event task.
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001925 * It's assigned during the conn-stream's initialization, for any type of
1926 * conn-stream. Thus it is always safe to perform a tasklet_wakeup() on a
1927 * conn-stream, as the presence of the CS is checked there.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001928 */
1929struct task *cs_conn_io_cb(struct task *t, void *ctx, unsigned int state)
1930{
1931 struct conn_stream *cs = ctx;
1932 int ret = 0;
1933
1934 if (!cs_conn(cs))
1935 return t;
1936
1937 if (!(cs->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(cs_oc(cs)))
1938 ret = cs_conn_send(cs);
1939 if (!(cs->wait_event.events & SUB_RETRY_RECV))
1940 ret |= cs_conn_recv(cs);
1941 if (ret != 0)
1942 cs_conn_process(cs);
1943
1944 stream_release_buffers(__cs_strm(cs));
1945 return t;
1946}
1947
1948/* Callback to be used by applet handlers upon completion. It updates the stream
1949 * (which may or may not take this opportunity to try to forward data), then
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001950 * may re-enable the applet's based on the channels and conn-stream's final
Christopher Faulet5e29b762022-04-04 08:58:34 +02001951 * states.
1952 */
1953static int cs_applet_process(struct conn_stream *cs)
1954{
1955 struct channel *ic = cs_ic(cs);
1956
1957 BUG_ON(!cs_appctx(cs));
1958
1959 /* If the applet wants to write and the channel is closed, it's a
1960 * broken pipe and it must be reported.
1961 */
1962 if (!(cs->endp->flags & CS_EP_RX_WAIT_EP) && (ic->flags & CF_SHUTR))
1963 cs->endp->flags |= CS_EP_ERROR;
1964
1965 /* automatically mark the applet having data available if it reported
1966 * begin blocked by the channel.
1967 */
1968 if (cs_rx_blocked(cs))
1969 cs_rx_endp_more(cs);
1970
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001971 /* update the conn-stream, channels, and possibly wake the stream up */
Christopher Faulet5e29b762022-04-04 08:58:34 +02001972 cs_notify(cs);
1973 stream_release_buffers(__cs_strm(cs));
1974
1975 /* cs_notify may have passed through chk_snd and released some
1976 * RXBLK flags. Process_stream will consider those flags to wake up the
1977 * appctx but in the case the task is not in runqueue we may have to
1978 * wakeup the appctx immediately.
1979 */
1980 if ((cs_rx_endp_ready(cs) && !cs_rx_blocked(cs)) ||
1981 (cs_tx_endp_ready(cs) && !cs_tx_blocked(cs)))
1982 appctx_wakeup(__cs_appctx(cs));
1983 return 0;
Christopher Faulet13045f02022-04-01 14:23:38 +02001984}