blob: ff8704f5f305f1257e88160faaf3669b3b264433 [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;
Willy Tarreauefb46182022-05-10 09:04:18 +020089 endp->cs = NULL;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +010090 endp->flags = CS_EP_NONE;
91}
92
Christopher Faulet9ed77422022-04-12 08:51:15 +020093/* Tries to alloc an endpoint and initialize it. Returns NULL on failure. */
Christopher Fauletdb90f2a2022-03-22 16:06:25 +010094struct cs_endpoint *cs_endpoint_new()
95{
96 struct cs_endpoint *endp;
97
98 endp = pool_alloc(pool_head_cs_endpoint);
99 if (unlikely(!endp))
100 return NULL;
101
102 cs_endpoint_init(endp);
103 return endp;
104}
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 */
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100109void cs_endpoint_free(struct cs_endpoint *endp)
110{
111 pool_free(pool_head_cs_endpoint, endp);
112}
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 Tarreauefb46182022-05-10 09:04:18 +0200116 * function. The caller must, at least, set the CS_EP_ORPHAN or CS_EP_DETACHED
Christopher Faulet9ed77422022-04-12 08:51:15 +0200117 * flag.
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100118 */
Christopher Faulet265e1652022-04-12 08:49:27 +0200119static struct conn_stream *cs_new(struct cs_endpoint *endp)
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 */
Christopher Fauletb669d682022-03-22 18:37:19 +0100140 if (!endp) {
141 endp = cs_endpoint_new();
142 if (unlikely(!endp))
143 goto alloc_error;
144 }
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100145 cs->endp = endp;
Willy Tarreauefb46182022-05-10 09:04:18 +0200146 endp->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
157 * returned. In this case, CS_EP_ORPHAN flag is removed.
158 */
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100159struct conn_stream *cs_new_from_mux(struct cs_endpoint *endp, struct session *sess, struct buffer *input)
160{
161 struct conn_stream *cs;
162
163 cs = cs_new(endp);
164 if (unlikely(!cs))
165 return NULL;
166 if (unlikely(!stream_new(sess, cs, input))) {
167 pool_free(pool_head_connstream, cs);
168 cs = NULL;
169 }
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100170 endp->flags &= ~CS_EP_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 and its associated stream from an applet. <endp>
175 * must be defined. It returns NULL on error. On success, the new conn-stream is
176 * returned. In this case, CS_EP_ORPHAN flag is removed. The created CS is used
177 * to set the appctx owner.
178 */
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100179struct conn_stream *cs_new_from_applet(struct cs_endpoint *endp, struct session *sess, struct buffer *input)
180{
181 struct conn_stream *cs;
182 struct appctx *appctx = endp->ctx;
183
184 cs = cs_new(endp);
185 if (unlikely(!cs))
186 return NULL;
187 appctx->owner = cs;
188 if (unlikely(!stream_new(sess, cs, input))) {
189 pool_free(pool_head_connstream, cs);
190 cs = NULL;
191 }
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100192 endp->flags &= ~CS_EP_ORPHAN;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100193 return cs;
194}
195
Christopher Faulet9ed77422022-04-12 08:51:15 +0200196/* Creates a new conn-stream from an stream. There is no endpoint here, thus it
197 * will be created by cs_new(). So the CS_EP_DETACHED flag is set. It returns
198 * NULL on error. On success, the new conn-stream is returned.
199 */
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100200struct conn_stream *cs_new_from_strm(struct stream *strm, unsigned int flags)
201{
202 struct conn_stream *cs;
203
204 cs = cs_new(NULL);
205 if (unlikely(!cs))
206 return NULL;
207 cs->flags |= flags;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100208 cs->endp->flags |= CS_EP_DETACHED;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100209 cs->app = &strm->obj_type;
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200210 cs->ops = &cs_app_embedded_ops;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100211 cs->data_cb = NULL;
212 return cs;
213}
214
Christopher Faulet9ed77422022-04-12 08:51:15 +0200215/* Creates a new conn-stream from an health-check. There is no endpoint here,
216 * thus it will be created by cs_new(). So the CS_EP_DETACHED flag is set. It
217 * returns NULL on error. On success, the new conn-stream is returned.
218 */
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100219struct conn_stream *cs_new_from_check(struct check *check, unsigned int flags)
220{
221 struct conn_stream *cs;
222
223 cs = cs_new(NULL);
224 if (unlikely(!cs))
225 return NULL;
226 cs->flags |= flags;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100227 cs->endp->flags |= CS_EP_DETACHED;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100228 cs->app = &check->obj_type;
229 cs->data_cb = &check_conn_cb;
230 return cs;
231}
232
Christopher Faulet9ed77422022-04-12 08:51:15 +0200233/* Releases a conn_stream previously allocated by cs_new(), as well as its
234 * endpoint, if it exists. This function is called internally or on error path.
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100235 */
236void cs_free(struct conn_stream *cs)
237{
Christopher Faulet8da67aa2022-03-29 17:53:09 +0200238 sockaddr_free(&cs->src);
239 sockaddr_free(&cs->dst);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100240 if (cs->endp) {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100241 BUG_ON(!(cs->endp->flags & CS_EP_DETACHED));
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100242 cs_endpoint_free(cs->endp);
243 }
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200244 if (cs->wait_event.tasklet)
245 tasklet_free(cs->wait_event.tasklet);
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100246 pool_free(pool_head_connstream, cs);
247}
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100248
Christopher Fauleteb50c012022-04-21 14:22:53 +0200249/* Conditionally removes a conn-stream if it is detached and if there is no app
250 * layer defined. Except on error path, this one must be used. if release, the
251 * pointer on the CS is set to NULL.
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200252 */
Christopher Fauleteb50c012022-04-21 14:22:53 +0200253static void cs_free_cond(struct conn_stream **csp)
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200254{
Christopher Fauleteb50c012022-04-21 14:22:53 +0200255 struct conn_stream *cs = *csp;
256
257 if (!cs->app && (!cs->endp || (cs->endp->flags & CS_EP_DETACHED))) {
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200258 cs_free(cs);
Christopher Fauleteb50c012022-04-21 14:22:53 +0200259 *csp = NULL;
260 }
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200261}
262
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100263
Christopher Faulet9ed77422022-04-12 08:51:15 +0200264/* Attaches a conn_stream to a mux endpoint and sets the endpoint ctx. Returns
265 * -1 on error and 0 on sucess. CS_EP_DETACHED flag is removed. This function is
266 * called from a mux when it is attached to a stream or a health-check.
267 */
Christopher Faulet070b91b2022-03-31 19:27:18 +0200268int cs_attach_mux(struct conn_stream *cs, void *target, void *ctx)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100269{
Christopher Faulet93882042022-01-19 14:56:50 +0100270 struct connection *conn = ctx;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100271
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100272 cs->endp->target = target;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100273 cs->endp->ctx = ctx;
274 cs->endp->flags |= CS_EP_T_MUX;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100275 cs->endp->flags &= ~CS_EP_DETACHED;
Christopher Faulet93882042022-01-19 14:56:50 +0100276 if (!conn->ctx)
277 conn->ctx = cs;
278 if (cs_strm(cs)) {
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200279 if (!cs->wait_event.tasklet) {
280 cs->wait_event.tasklet = tasklet_new();
281 if (!cs->wait_event.tasklet)
282 return -1;
Christopher Faulet4a7764a2022-04-01 16:58:52 +0200283 cs->wait_event.tasklet->process = cs_conn_io_cb;
284 cs->wait_event.tasklet->context = cs;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200285 cs->wait_event.events = 0;
286 }
287
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200288 cs->ops = &cs_app_conn_ops;
Christopher Faulet000ba3e2022-04-01 17:06:32 +0200289 cs->data_cb = &cs_data_conn_cb;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100290 }
Christopher Faulet93882042022-01-19 14:56:50 +0100291 else if (cs_check(cs))
292 cs->data_cb = &check_conn_cb;
Christopher Faulet070b91b2022-03-31 19:27:18 +0200293 return 0;
Christopher Faulet93882042022-01-19 14:56:50 +0100294}
295
Christopher Faulet9ed77422022-04-12 08:51:15 +0200296/* Attaches a conn_stream to an applet endpoint and sets the endpoint
297 * ctx. Returns -1 on error and 0 on sucess. CS_EP_DETACHED flag is
298 * removed. This function is called by a stream when a backend applet is
299 * registered.
300 */
Christopher Faulet265e1652022-04-12 08:49:27 +0200301static void cs_attach_applet(struct conn_stream *cs, void *target, void *ctx)
Christopher Faulet93882042022-01-19 14:56:50 +0100302{
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100303 struct appctx *appctx = target;
Christopher Faulet93882042022-01-19 14:56:50 +0100304
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100305 cs->endp->target = target;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100306 cs->endp->ctx = ctx;
307 cs->endp->flags |= CS_EP_T_APPLET;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100308 cs->endp->flags &= ~CS_EP_DETACHED;
Christopher Faulet93882042022-01-19 14:56:50 +0100309 appctx->owner = cs;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100310 if (cs_strm(cs)) {
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200311 cs->ops = &cs_app_applet_ops;
Christopher Faulet6059ba42022-04-01 16:34:53 +0200312 cs->data_cb = &cs_data_applet_cb;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100313 }
314}
315
Christopher Faulet9ed77422022-04-12 08:51:15 +0200316/* Attaches a conn_stream to a app layer and sets the relevant
317 * callbacks. Returns -1 on error and 0 on success. CS_EP_ORPHAN flag is
318 * removed. This function is called by a stream when it is created to attach it
319 * on the conn-stream on the client side.
320 */
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100321int cs_attach_strm(struct conn_stream *cs, struct stream *strm)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100322{
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100323 cs->app = &strm->obj_type;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100324 cs->endp->flags &= ~CS_EP_ORPHAN;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100325 if (cs->endp->flags & CS_EP_T_MUX) {
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200326 cs->wait_event.tasklet = tasklet_new();
Christopher Faulet582a2262022-04-04 11:25:59 +0200327 if (!cs->wait_event.tasklet)
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200328 return -1;
Christopher Faulet4a7764a2022-04-01 16:58:52 +0200329 cs->wait_event.tasklet->process = cs_conn_io_cb;
330 cs->wait_event.tasklet->context = cs;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200331 cs->wait_event.events = 0;
332
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200333 cs->ops = &cs_app_conn_ops;
Christopher Faulet000ba3e2022-04-01 17:06:32 +0200334 cs->data_cb = &cs_data_conn_cb;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100335 }
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100336 else if (cs->endp->flags & CS_EP_T_APPLET) {
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200337 cs->ops = &cs_app_applet_ops;
Christopher Faulet6059ba42022-04-01 16:34:53 +0200338 cs->data_cb = &cs_data_applet_cb;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100339 }
340 else {
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200341 cs->ops = &cs_app_embedded_ops;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100342 cs->data_cb = NULL;
343 }
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100344 return 0;
345}
346
Christopher Faulet9ed77422022-04-12 08:51:15 +0200347/* Detaches the conn_stream from the endpoint, if any. For a connecrion, if a
348 * mux owns the connection ->detach() callback is called. Otherwise, it means
349 * the conn-stream owns the connection. In this case the connection is closed
350 * and released. For an applet, the appctx is released. If still allocated, the
351 * endpoint is reset and flag as detached. If the app layer is also detached,
352 * the conn-stream is released.
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100353 */
Christopher Fauleteb50c012022-04-21 14:22:53 +0200354static void cs_detach_endp(struct conn_stream **csp)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100355{
Christopher Fauleteb50c012022-04-21 14:22:53 +0200356 struct conn_stream *cs = *csp;
357
358 if (!cs)
359 return;
360
Christopher Fauletb041b232022-03-24 10:27:02 +0100361 if (!cs->endp)
362 goto reset_cs;
363
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100364 if (cs->endp->flags & CS_EP_T_MUX) {
Christopher Fauletcea05432022-04-14 11:40:12 +0200365 struct connection *conn = __cs_conn(cs);
Willy Tarreau4201ab72022-05-10 19:18:52 +0200366 struct cs_endpoint *endp = cs->endp;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100367
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100368 if (conn->mux) {
Christopher Faulet54e85cb2022-01-06 08:46:56 +0100369 /* TODO: handle unsubscribe for healthchecks too */
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200370 if (cs->wait_event.events != 0)
371 conn->mux->unsubscribe(cs, cs->wait_event.events, &cs->wait_event);
Willy Tarreau4201ab72022-05-10 19:18:52 +0200372 endp->flags |= CS_EP_ORPHAN;
373 endp->cs = NULL;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100374 cs->endp = NULL;
Willy Tarreau4201ab72022-05-10 19:18:52 +0200375 conn->mux->detach(endp);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100376 }
377 else {
378 /* It's too early to have a mux, let's just destroy
379 * the connection
380 */
381 conn_stop_tracking(conn);
382 conn_full_close(conn);
383 if (conn->destroy_cb)
384 conn->destroy_cb(conn);
385 conn_free(conn);
386 }
387 }
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100388 else if (cs->endp->flags & CS_EP_T_APPLET) {
Christopher Fauletcea05432022-04-14 11:40:12 +0200389 struct appctx *appctx = __cs_appctx(cs);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100390
Willy Tarreauefb46182022-05-10 09:04:18 +0200391 cs->endp->flags |= CS_EP_ORPHAN;
392 cs->endp->cs = NULL;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100393 cs->endp = NULL;
Willy Tarreau1c3ead42022-05-10 19:42:22 +0200394 appctx_shut(appctx);
395 appctx_free(appctx);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100396 }
397
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100398 if (cs->endp) {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100399 /* the cs is the only one one the endpoint */
Christopher Fauleta6c4a482022-04-28 18:25:24 +0200400 cs->endp->target = NULL;
401 cs->endp->ctx = NULL;
Christopher Fauletc41f93c2022-05-04 09:52:48 +0200402 cs->endp->flags &= CS_EP_APP_MASK;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100403 cs->endp->flags |= CS_EP_DETACHED;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100404 }
405
Christopher Fauletb041b232022-03-24 10:27:02 +0100406 reset_cs:
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100407 /* FIXME: Rest CS for now but must be reviewed. CS flags are only
408 * connection related for now but this will evolved
409 */
Christopher Faulet30995112022-03-25 15:32:38 +0100410 cs->flags &= CS_FL_ISBACK;
Christopher Faulet582a2262022-04-04 11:25:59 +0200411 if (cs_strm(cs))
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200412 cs->ops = &cs_app_embedded_ops;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100413 cs->data_cb = NULL;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200414 cs_free_cond(csp);
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100415}
416
Christopher Faulet9ed77422022-04-12 08:51:15 +0200417/* Detaches the conn_stream from the app layer. If there is no endpoint attached
418 * to the conn_stream
419 */
Christopher Fauleteb50c012022-04-21 14:22:53 +0200420static void cs_detach_app(struct conn_stream **csp)
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100421{
Christopher Fauleteb50c012022-04-21 14:22:53 +0200422 struct conn_stream *cs = *csp;
423
424 if (!cs)
425 return;
426
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100427 cs->app = NULL;
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100428 cs->data_cb = NULL;
Christopher Faulet8da67aa2022-03-29 17:53:09 +0200429 sockaddr_free(&cs->src);
430 sockaddr_free(&cs->dst);
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200431
432 if (cs->wait_event.tasklet)
433 tasklet_free(cs->wait_event.tasklet);
434 cs->wait_event.tasklet = NULL;
435 cs->wait_event.events = 0;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200436 cs_free_cond(csp);
437}
438
439/* Destroy the conn_stream. It is detached from its endpoint and its
440 * application. After this call, the conn_stream must be considered as released.
441 */
442void cs_destroy(struct conn_stream *cs)
443{
444 cs_detach_endp(&cs);
445 cs_detach_app(&cs);
446 BUG_ON_HOT(cs);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100447}
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100448
Christopher Faulet9ed77422022-04-12 08:51:15 +0200449/* Resets the conn-stream endpoint. It happens when the app layer want to renew
450 * its endpoint. For a connection retry for instance. If a mux or an applet is
451 * attached, a new endpoint is created. Returns -1 on error and 0 on sucess.
Christopher Fauleta6c4a482022-04-28 18:25:24 +0200452 *
453 * Only CS_EP_ERROR flag is removed on the endpoint. Orther flags are preserved.
454 * It is the caller responsibility to remove other flags if needed.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200455 */
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100456int cs_reset_endp(struct conn_stream *cs)
457{
Christopher Fauletb041b232022-03-24 10:27:02 +0100458 struct cs_endpoint *new_endp;
459
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100460 BUG_ON(!cs->app);
Christopher Fauleta6c4a482022-04-28 18:25:24 +0200461
462 cs->endp->flags &= ~CS_EP_ERROR;
Christopher Fauletb041b232022-03-24 10:27:02 +0100463 if (!__cs_endp_target(cs)) {
464 /* endpoint not attached or attached to a mux with no
465 * target. Thus the endpoint will not be release but just
Christopher Fauleteb50c012022-04-21 14:22:53 +0200466 * reset. The app is still attached, the cs will not be
467 * released.
Christopher Fauletb041b232022-03-24 10:27:02 +0100468 */
Christopher Fauleteb50c012022-04-21 14:22:53 +0200469 cs_detach_endp(&cs);
Christopher Fauletb041b232022-03-24 10:27:02 +0100470 return 0;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100471 }
Christopher Fauletb041b232022-03-24 10:27:02 +0100472
473 /* allocate the new endpoint first to be able to set error if it
474 * fails */
475 new_endp = cs_endpoint_new();
476 if (!unlikely(new_endp)) {
477 cs->endp->flags |= CS_EP_ERROR;
478 return -1;
479 }
Christopher Fauletc41f93c2022-05-04 09:52:48 +0200480 new_endp->flags = (cs->endp->flags & CS_EP_APP_MASK);
Christopher Fauletb041b232022-03-24 10:27:02 +0100481
Christopher Fauleteb50c012022-04-21 14:22:53 +0200482 /* The app is still attached, the cs will not be released */
483 cs_detach_endp(&cs);
Christopher Fauletb041b232022-03-24 10:27:02 +0100484 BUG_ON(cs->endp);
485 cs->endp = new_endp;
Willy Tarreauefb46182022-05-10 09:04:18 +0200486 cs->endp->cs = cs;
Christopher Fauletb041b232022-03-24 10:27:02 +0100487 cs->endp->flags |= CS_EP_DETACHED;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100488 return 0;
489}
Christopher Faulet37046632022-04-01 11:36:58 +0200490
491
Christopher Faulet1336ccf2022-04-12 18:15:16 +0200492/* Create an applet to handle a conn-stream as a new appctx. The CS will
Christopher Faulet37046632022-04-01 11:36:58 +0200493 * wake it up every time it is solicited. The appctx must be deleted by the task
494 * handler using cs_detach_endp(), possibly from within the function itself.
495 * It also pre-initializes the applet's context and returns it (or NULL in case
496 * it could not be allocated).
497 */
Christopher Faulet1336ccf2022-04-12 18:15:16 +0200498struct appctx *cs_applet_create(struct conn_stream *cs, struct applet *app)
Christopher Faulet37046632022-04-01 11:36:58 +0200499{
500 struct appctx *appctx;
501
502 DPRINTF(stderr, "registering handler %p for cs %p (was %p)\n", app, cs, cs_strm_task(cs));
503
504 appctx = appctx_new(app, cs->endp);
505 if (!appctx)
506 return NULL;
507 cs_attach_applet(cs, appctx, appctx);
508 appctx->owner = cs;
509 appctx->t->nice = __cs_strm(cs)->task->nice;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200510 cs_cant_get(cs);
Christopher Faulet37046632022-04-01 11:36:58 +0200511 appctx_wakeup(appctx);
Christopher Fauleta33ff7a2022-04-21 11:52:07 +0200512
513 cs->state = CS_ST_RDY;
Christopher Faulet37046632022-04-01 11:36:58 +0200514 return appctx;
515}
516
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200517/*
518 * This function performs a shutdown-read on a detached conn-stream in a
519 * connected or init state (it does nothing for other states). It either shuts
520 * the read side or marks itself as closed. The buffer flags are updated to
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +0200521 * reflect the new state. If the conn-stream has CS_FL_NOHALF, we also
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200522 * forward the close to the write side. The owner task is woken up if it exists.
523 */
524static void cs_app_shutr(struct conn_stream *cs)
525{
526 struct channel *ic = cs_ic(cs);
527
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200528 cs_rx_shut_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200529 if (ic->flags & CF_SHUTR)
530 return;
531 ic->flags |= CF_SHUTR;
532 ic->rex = TICK_ETERNITY;
533
534 if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
535 return;
536
537 if (cs_oc(cs)->flags & CF_SHUTW) {
538 cs->state = CS_ST_DIS;
539 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
540 }
541 else if (cs->flags & CS_FL_NOHALF) {
542 /* we want to immediately forward this close to the write side */
543 return cs_app_shutw(cs);
544 }
545
546 /* note that if the task exists, it must unregister itself once it runs */
547 if (!(cs->flags & CS_FL_DONT_WAKE))
548 task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO);
549}
550
551/*
552 * This function performs a shutdown-write on a detached conn-stream in a
553 * connected or init state (it does nothing for other states). It either shuts
554 * the write side or marks itself as closed. The buffer flags are updated to
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +0200555 * reflect the new state. It does also close everything if the CS was marked as
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200556 * being in error state. The owner task is woken up if it exists.
557 */
558static void cs_app_shutw(struct conn_stream *cs)
559{
560 struct channel *ic = cs_ic(cs);
561 struct channel *oc = cs_oc(cs);
562
563 oc->flags &= ~CF_SHUTW_NOW;
564 if (oc->flags & CF_SHUTW)
565 return;
566 oc->flags |= CF_SHUTW;
567 oc->wex = TICK_ETERNITY;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200568 cs_done_get(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200569
570 if (tick_isset(cs->hcto)) {
571 ic->rto = cs->hcto;
572 ic->rex = tick_add(now_ms, ic->rto);
573 }
574
575 switch (cs->state) {
576 case CS_ST_RDY:
577 case CS_ST_EST:
578 /* we have to shut before closing, otherwise some short messages
579 * may never leave the system, especially when there are remaining
580 * unread data in the socket input buffer, or when nolinger is set.
581 * However, if CS_FL_NOLINGER is explicitly set, we know there is
582 * no risk so we close both sides immediately.
583 */
584 if (!(cs->endp->flags & CS_EP_ERROR) && !(cs->flags & CS_FL_NOLINGER) &&
585 !(ic->flags & (CF_SHUTR|CF_DONT_READ)))
586 return;
587
588 /* fall through */
589 case CS_ST_CON:
590 case CS_ST_CER:
591 case CS_ST_QUE:
592 case CS_ST_TAR:
593 /* Note that none of these states may happen with applets */
594 cs->state = CS_ST_DIS;
595 /* fall through */
596 default:
597 cs->flags &= ~CS_FL_NOLINGER;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200598 cs_rx_shut_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200599 ic->flags |= CF_SHUTR;
600 ic->rex = TICK_ETERNITY;
601 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
602 }
603
604 /* note that if the task exists, it must unregister itself once it runs */
605 if (!(cs->flags & CS_FL_DONT_WAKE))
606 task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO);
607}
608
609/* default chk_rcv function for scheduled tasks */
610static void cs_app_chk_rcv(struct conn_stream *cs)
611{
612 struct channel *ic = cs_ic(cs);
613
614 DPRINTF(stderr, "%s: cs=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n",
615 __FUNCTION__,
616 cs, cs->state, ic->flags, cs_oc(cs)->flags);
617
618 if (ic->pipe) {
619 /* stop reading */
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200620 cs_rx_room_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200621 }
622 else {
623 /* (re)start reading */
624 if (!(cs->flags & CS_FL_DONT_WAKE))
625 task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO);
626 }
627}
628
629/* default chk_snd function for scheduled tasks */
630static void cs_app_chk_snd(struct conn_stream *cs)
631{
632 struct channel *oc = cs_oc(cs);
633
634 DPRINTF(stderr, "%s: cs=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n",
635 __FUNCTION__,
636 cs, cs->state, cs_ic(cs)->flags, oc->flags);
637
638 if (unlikely(cs->state != CS_ST_EST || (oc->flags & CF_SHUTW)))
639 return;
640
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200641 if (!(cs->endp->flags & CS_EP_WAIT_DATA) || /* not waiting for data */
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200642 channel_is_empty(oc)) /* called with nothing to send ! */
643 return;
644
645 /* Otherwise there are remaining data to be sent in the buffer,
646 * so we tell the handler.
647 */
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200648 cs->endp->flags &= ~CS_EP_WAIT_DATA;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200649 if (!tick_isset(oc->wex))
650 oc->wex = tick_add_ifset(now_ms, oc->wto);
651
652 if (!(cs->flags & CS_FL_DONT_WAKE))
653 task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO);
654}
655
656/*
657 * This function performs a shutdown-read on a conn-stream attached to
658 * a connection in a connected or init state (it does nothing for other
659 * states). It either shuts the read side or marks itself as closed. The buffer
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +0200660 * flags are updated to reflect the new state. If the conn-stream has
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200661 * CS_FL_NOHALF, we also forward the close to the write side. If a control
662 * layer is defined, then it is supposed to be a socket layer and file
663 * descriptors are then shutdown or closed accordingly. The function
664 * automatically disables polling if needed.
665 */
666static void cs_app_shutr_conn(struct conn_stream *cs)
667{
668 struct channel *ic = cs_ic(cs);
669
670 BUG_ON(!cs_conn(cs));
671
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200672 cs_rx_shut_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200673 if (ic->flags & CF_SHUTR)
674 return;
675 ic->flags |= CF_SHUTR;
676 ic->rex = TICK_ETERNITY;
677
678 if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
679 return;
680
681 if (cs_oc(cs)->flags & CF_SHUTW) {
Christopher Fauletff022a22022-04-21 08:38:54 +0200682 cs_conn_shut(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200683 cs->state = CS_ST_DIS;
684 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
685 }
686 else if (cs->flags & CS_FL_NOHALF) {
687 /* we want to immediately forward this close to the write side */
688 return cs_app_shutw_conn(cs);
689 }
690}
691
692/*
693 * This function performs a shutdown-write on a conn-stream attached to
694 * a connection in a connected or init state (it does nothing for other
695 * states). It either shuts the write side or marks itself as closed. The
696 * buffer flags are updated to reflect the new state. It does also close
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +0200697 * everything if the CS was marked as being in error state. If there is a
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200698 * data-layer shutdown, it is called.
699 */
700static void cs_app_shutw_conn(struct conn_stream *cs)
701{
702 struct channel *ic = cs_ic(cs);
703 struct channel *oc = cs_oc(cs);
704
705 BUG_ON(!cs_conn(cs));
706
707 oc->flags &= ~CF_SHUTW_NOW;
708 if (oc->flags & CF_SHUTW)
709 return;
710 oc->flags |= CF_SHUTW;
711 oc->wex = TICK_ETERNITY;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200712 cs_done_get(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200713
714 if (tick_isset(cs->hcto)) {
715 ic->rto = cs->hcto;
716 ic->rex = tick_add(now_ms, ic->rto);
717 }
718
719 switch (cs->state) {
720 case CS_ST_RDY:
721 case CS_ST_EST:
722 /* we have to shut before closing, otherwise some short messages
723 * may never leave the system, especially when there are remaining
724 * unread data in the socket input buffer, or when nolinger is set.
725 * However, if CS_FL_NOLINGER is explicitly set, we know there is
726 * no risk so we close both sides immediately.
727 */
728
729 if (cs->endp->flags & CS_EP_ERROR) {
730 /* quick close, the socket is already shut anyway */
731 }
732 else if (cs->flags & CS_FL_NOLINGER) {
733 /* unclean data-layer shutdown, typically an aborted request
734 * or a forwarded shutdown from a client to a server due to
735 * option abortonclose. No need for the TLS layer to try to
736 * emit a shutdown message.
737 */
738 cs_conn_shutw(cs, CO_SHW_SILENT);
739 }
740 else {
741 /* clean data-layer shutdown. This only happens on the
742 * frontend side, or on the backend side when forwarding
743 * a client close in TCP mode or in HTTP TUNNEL mode
744 * while option abortonclose is set. We want the TLS
745 * layer to try to signal it to the peer before we close.
746 */
747 cs_conn_shutw(cs, CO_SHW_NORMAL);
748
749 if (!(ic->flags & (CF_SHUTR|CF_DONT_READ)))
750 return;
751 }
752
753 /* fall through */
754 case CS_ST_CON:
755 /* we may have to close a pending connection, and mark the
756 * response buffer as shutr
757 */
Christopher Fauletff022a22022-04-21 08:38:54 +0200758 cs_conn_shut(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200759 /* fall through */
760 case CS_ST_CER:
761 case CS_ST_QUE:
762 case CS_ST_TAR:
763 cs->state = CS_ST_DIS;
764 /* fall through */
765 default:
766 cs->flags &= ~CS_FL_NOLINGER;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200767 cs_rx_shut_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200768 ic->flags |= CF_SHUTR;
769 ic->rex = TICK_ETERNITY;
770 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
771 }
772}
773
774/* This function is used for inter-conn-stream calls. It is called by the
775 * consumer to inform the producer side that it may be interested in checking
776 * for free space in the buffer. Note that it intentionally does not update
777 * timeouts, so that we can still check them later at wake-up. This function is
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +0200778 * dedicated to connection-based conn-streams.
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200779 */
780static void cs_app_chk_rcv_conn(struct conn_stream *cs)
781{
782 BUG_ON(!cs_conn(cs));
783
784 /* (re)start reading */
785 if (cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
786 tasklet_wakeup(cs->wait_event.tasklet);
787}
788
789
790/* This function is used for inter-conn-stream calls. It is called by the
791 * producer to inform the consumer side that it may be interested in checking
792 * for data in the buffer. Note that it intentionally does not update timeouts,
793 * so that we can still check them later at wake-up.
794 */
795static void cs_app_chk_snd_conn(struct conn_stream *cs)
796{
797 struct channel *oc = cs_oc(cs);
798
799 BUG_ON(!cs_conn(cs));
800
Willy Tarreau4173f4e2022-04-29 15:04:41 +0200801 if (unlikely(!cs_state_in(cs->state, CS_SB_RDY|CS_SB_EST) ||
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200802 (oc->flags & CF_SHUTW)))
803 return;
804
805 if (unlikely(channel_is_empty(oc))) /* called with nothing to send ! */
806 return;
807
808 if (!oc->pipe && /* spliced data wants to be forwarded ASAP */
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200809 !(cs->endp->flags & CS_EP_WAIT_DATA)) /* not waiting for data */
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200810 return;
811
812 if (!(cs->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(cs_oc(cs)))
Christopher Faulet000ba3e2022-04-01 17:06:32 +0200813 cs_conn_send(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200814
Christopher Faulet158f3362022-04-01 17:15:10 +0200815 if (cs->endp->flags & (CS_EP_ERROR|CS_EP_ERR_PENDING) || cs_is_conn_error(cs)) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200816 /* Write error on the file descriptor */
817 if (cs->state >= CS_ST_CON)
818 cs->endp->flags |= CS_EP_ERROR;
819 goto out_wakeup;
820 }
821
822 /* OK, so now we know that some data might have been sent, and that we may
823 * have to poll first. We have to do that too if the buffer is not empty.
824 */
825 if (channel_is_empty(oc)) {
826 /* the connection is established but we can't write. Either the
827 * buffer is empty, or we just refrain from sending because the
828 * ->o limit was reached. Maybe we just wrote the last
829 * chunk and need to close.
830 */
831 if (((oc->flags & (CF_SHUTW|CF_AUTO_CLOSE|CF_SHUTW_NOW)) ==
832 (CF_AUTO_CLOSE|CF_SHUTW_NOW)) &&
833 cs_state_in(cs->state, CS_SB_RDY|CS_SB_EST)) {
834 cs_shutw(cs);
835 goto out_wakeup;
836 }
837
838 if ((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == 0)
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200839 cs->endp->flags |= CS_EP_WAIT_DATA;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200840 oc->wex = TICK_ETERNITY;
841 }
842 else {
843 /* Otherwise there are remaining data to be sent in the buffer,
844 * which means we have to poll before doing so.
845 */
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200846 cs->endp->flags &= ~CS_EP_WAIT_DATA;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200847 if (!tick_isset(oc->wex))
848 oc->wex = tick_add_ifset(now_ms, oc->wto);
849 }
850
851 if (likely(oc->flags & CF_WRITE_ACTIVITY)) {
852 struct channel *ic = cs_ic(cs);
853
854 /* update timeout if we have written something */
855 if ((oc->flags & (CF_SHUTW|CF_WRITE_PARTIAL)) == CF_WRITE_PARTIAL &&
856 !channel_is_empty(oc))
857 oc->wex = tick_add_ifset(now_ms, oc->wto);
858
859 if (tick_isset(ic->rex) && !(cs->flags & CS_FL_INDEP_STR)) {
860 /* Note: to prevent the client from expiring read timeouts
861 * during writes, we refresh it. We only do this if the
862 * interface is not configured for "independent streams",
863 * because for some applications it's better not to do this,
864 * for instance when continuously exchanging small amounts
865 * of data which can full the socket buffers long before a
866 * write timeout is detected.
867 */
868 ic->rex = tick_add_ifset(now_ms, ic->rto);
869 }
870 }
871
872 /* in case of special condition (error, shutdown, end of write...), we
873 * have to notify the task.
874 */
875 if (likely((oc->flags & (CF_WRITE_NULL|CF_WRITE_ERROR|CF_SHUTW)) ||
876 ((oc->flags & CF_WAKE_WRITE) &&
877 ((channel_is_empty(oc) && !oc->to_forward) ||
878 !cs_state_in(cs->state, CS_SB_EST))))) {
879 out_wakeup:
880 if (!(cs->flags & CS_FL_DONT_WAKE))
881 task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO);
882 }
883}
884
885/*
886 * This function performs a shutdown-read on a conn-stream attached to an
887 * applet in a connected or init state (it does nothing for other states). It
888 * either shuts the read side or marks itself as closed. The buffer flags are
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +0200889 * updated to reflect the new state. If the conn-stream has CS_FL_NOHALF,
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200890 * we also forward the close to the write side. The owner task is woken up if
891 * it exists.
892 */
893static void cs_app_shutr_applet(struct conn_stream *cs)
894{
895 struct channel *ic = cs_ic(cs);
896
897 BUG_ON(!cs_appctx(cs));
898
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200899 cs_rx_shut_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200900 if (ic->flags & CF_SHUTR)
901 return;
902 ic->flags |= CF_SHUTR;
903 ic->rex = TICK_ETERNITY;
904
905 /* Note: on shutr, we don't call the applet */
906
907 if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
908 return;
909
910 if (cs_oc(cs)->flags & CF_SHUTW) {
Willy Tarreau1c3ead42022-05-10 19:42:22 +0200911 appctx_shut(__cs_appctx(cs));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200912 cs->state = CS_ST_DIS;
913 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
914 }
915 else if (cs->flags & CS_FL_NOHALF) {
916 /* we want to immediately forward this close to the write side */
917 return cs_app_shutw_applet(cs);
918 }
919}
920
921/*
922 * This function performs a shutdown-write on a conn-stream attached to an
923 * applet in a connected or init state (it does nothing for other states). It
924 * either shuts the write side or marks itself as closed. The buffer flags are
925 * updated to reflect the new state. It does also close everything if the SI
926 * was marked as being in error state. The owner task is woken up if it exists.
927 */
928static void cs_app_shutw_applet(struct conn_stream *cs)
929{
930 struct channel *ic = cs_ic(cs);
931 struct channel *oc = cs_oc(cs);
932
933 BUG_ON(!cs_appctx(cs));
934
935 oc->flags &= ~CF_SHUTW_NOW;
936 if (oc->flags & CF_SHUTW)
937 return;
938 oc->flags |= CF_SHUTW;
939 oc->wex = TICK_ETERNITY;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200940 cs_done_get(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200941
942 if (tick_isset(cs->hcto)) {
943 ic->rto = cs->hcto;
944 ic->rex = tick_add(now_ms, ic->rto);
945 }
946
947 /* on shutw we always wake the applet up */
948 appctx_wakeup(__cs_appctx(cs));
949
950 switch (cs->state) {
951 case CS_ST_RDY:
952 case CS_ST_EST:
953 /* we have to shut before closing, otherwise some short messages
954 * may never leave the system, especially when there are remaining
955 * unread data in the socket input buffer, or when nolinger is set.
956 * However, if CS_FL_NOLINGER is explicitly set, we know there is
957 * no risk so we close both sides immediately.
958 */
959 if (!(cs->endp->flags & CS_EP_ERROR) && !(cs->flags & CS_FL_NOLINGER) &&
960 !(ic->flags & (CF_SHUTR|CF_DONT_READ)))
961 return;
962
963 /* fall through */
964 case CS_ST_CON:
965 case CS_ST_CER:
966 case CS_ST_QUE:
967 case CS_ST_TAR:
968 /* Note that none of these states may happen with applets */
Willy Tarreau1c3ead42022-05-10 19:42:22 +0200969 appctx_shut(__cs_appctx(cs));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200970 cs->state = CS_ST_DIS;
971 /* fall through */
972 default:
973 cs->flags &= ~CS_FL_NOLINGER;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200974 cs_rx_shut_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200975 ic->flags |= CF_SHUTR;
976 ic->rex = TICK_ETERNITY;
977 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
978 }
979}
980
981/* chk_rcv function for applets */
982static void cs_app_chk_rcv_applet(struct conn_stream *cs)
983{
984 struct channel *ic = cs_ic(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, ic->flags, cs_oc(cs)->flags);
991
992 if (!ic->pipe) {
993 /* (re)start reading */
994 appctx_wakeup(__cs_appctx(cs));
995 }
996}
997
998/* chk_snd function for applets */
999static void cs_app_chk_snd_applet(struct conn_stream *cs)
1000{
1001 struct channel *oc = cs_oc(cs);
1002
1003 BUG_ON(!cs_appctx(cs));
1004
1005 DPRINTF(stderr, "%s: cs=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n",
1006 __FUNCTION__,
1007 cs, cs->state, cs_ic(cs)->flags, oc->flags);
1008
1009 if (unlikely(cs->state != CS_ST_EST || (oc->flags & CF_SHUTW)))
1010 return;
1011
1012 /* we only wake the applet up if it was waiting for some data */
1013
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001014 if (!(cs->endp->flags & CS_EP_WAIT_DATA))
Christopher Faulet9ffddd52022-04-01 14:04:29 +02001015 return;
1016
1017 if (!tick_isset(oc->wex))
1018 oc->wex = tick_add_ifset(now_ms, oc->wto);
1019
1020 if (!channel_is_empty(oc)) {
1021 /* (re)start sending */
1022 appctx_wakeup(__cs_appctx(cs));
1023 }
1024}
Christopher Faulet13045f02022-04-01 14:23:38 +02001025
1026
1027/* This function is designed to be called from within the stream handler to
1028 * update the input channel's expiration timer and the conn-stream's
1029 * Rx flags based on the channel's flags. It needs to be called only once
1030 * after the channel's flags have settled down, and before they are cleared,
1031 * though it doesn't harm to call it as often as desired (it just slightly
1032 * hurts performance). It must not be called from outside of the stream
1033 * handler, as what it does will be used to compute the stream task's
1034 * expiration.
1035 */
1036void cs_update_rx(struct conn_stream *cs)
1037{
1038 struct channel *ic = cs_ic(cs);
1039
1040 if (ic->flags & CF_SHUTR) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001041 cs_rx_shut_blk(cs);
Christopher Faulet13045f02022-04-01 14:23:38 +02001042 return;
1043 }
1044
1045 /* Read not closed, update FD status and timeout for reads */
1046 if (ic->flags & CF_DONT_READ)
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001047 cs_rx_chan_blk(cs);
Christopher Faulet13045f02022-04-01 14:23:38 +02001048 else
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001049 cs_rx_chan_rdy(cs);
Christopher Faulet13045f02022-04-01 14:23:38 +02001050
1051 if (!channel_is_empty(ic) || !channel_may_recv(ic)) {
1052 /* stop reading, imposed by channel's policy or contents */
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001053 cs_rx_room_blk(cs);
Christopher Faulet13045f02022-04-01 14:23:38 +02001054 }
1055 else {
1056 /* (re)start reading and update timeout. Note: we don't recompute the timeout
1057 * every time we get here, otherwise it would risk never to expire. We only
1058 * update it if is was not yet set. The stream socket handler will already
1059 * have updated it if there has been a completed I/O.
1060 */
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001061 cs_rx_room_rdy(cs);
Christopher Faulet13045f02022-04-01 14:23:38 +02001062 }
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001063 if (cs->endp->flags & CS_EP_RXBLK_ANY & ~CS_EP_RX_WAIT_EP)
Christopher Faulet13045f02022-04-01 14:23:38 +02001064 ic->rex = TICK_ETERNITY;
1065 else if (!(ic->flags & CF_READ_NOEXP) && !tick_isset(ic->rex))
1066 ic->rex = tick_add_ifset(now_ms, ic->rto);
1067
1068 cs_chk_rcv(cs);
1069}
1070
1071/* This function is designed to be called from within the stream handler to
1072 * update the output channel's expiration timer and the conn-stream's
1073 * Tx flags based on the channel's flags. It needs to be called only once
1074 * after the channel's flags have settled down, and before they are cleared,
1075 * though it doesn't harm to call it as often as desired (it just slightly
1076 * hurts performance). It must not be called from outside of the stream
1077 * handler, as what it does will be used to compute the stream task's
1078 * expiration.
1079 */
1080void cs_update_tx(struct conn_stream *cs)
1081{
1082 struct channel *oc = cs_oc(cs);
1083 struct channel *ic = cs_ic(cs);
1084
1085 if (oc->flags & CF_SHUTW)
1086 return;
1087
1088 /* Write not closed, update FD status and timeout for writes */
1089 if (channel_is_empty(oc)) {
1090 /* stop writing */
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001091 if (!(cs->endp->flags & CS_EP_WAIT_DATA)) {
Christopher Faulet13045f02022-04-01 14:23:38 +02001092 if ((oc->flags & CF_SHUTW_NOW) == 0)
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001093 cs->endp->flags |= CS_EP_WAIT_DATA;
Christopher Faulet13045f02022-04-01 14:23:38 +02001094 oc->wex = TICK_ETERNITY;
1095 }
1096 return;
1097 }
1098
1099 /* (re)start writing and update timeout. Note: we don't recompute the timeout
1100 * every time we get here, otherwise it would risk never to expire. We only
1101 * update it if is was not yet set. The stream socket handler will already
1102 * have updated it if there has been a completed I/O.
1103 */
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001104 cs->endp->flags &= ~CS_EP_WAIT_DATA;
Christopher Faulet13045f02022-04-01 14:23:38 +02001105 if (!tick_isset(oc->wex)) {
1106 oc->wex = tick_add_ifset(now_ms, oc->wto);
1107 if (tick_isset(ic->rex) && !(cs->flags & CS_FL_INDEP_STR)) {
1108 /* Note: depending on the protocol, we don't know if we're waiting
1109 * for incoming data or not. So in order to prevent the socket from
1110 * expiring read timeouts during writes, we refresh the read timeout,
1111 * except if it was already infinite or if we have explicitly setup
1112 * independent streams.
1113 */
1114 ic->rex = tick_add_ifset(now_ms, ic->rto);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001115 }
1116 }
1117}
1118
1119/* This function is the equivalent to cs_update() except that it's
1120 * designed to be called from outside the stream handlers, typically the lower
1121 * layers (applets, connections) after I/O completion. After updating the stream
1122 * interface and timeouts, it will try to forward what can be forwarded, then to
1123 * wake the associated task up if an important event requires special handling.
1124 * It may update CS_EP_WAIT_DATA and/or CS_EP_RXBLK_ROOM, that the callers are
1125 * encouraged to watch to take appropriate action.
1126 * It should not be called from within the stream itself, cs_update()
1127 * is designed for this.
1128 */
1129static void cs_notify(struct conn_stream *cs)
1130{
1131 struct channel *ic = cs_ic(cs);
1132 struct channel *oc = cs_oc(cs);
1133 struct conn_stream *cso = cs_opposite(cs);
1134 struct task *task = cs_strm_task(cs);
1135
1136 /* process consumer side */
1137 if (channel_is_empty(oc)) {
1138 struct connection *conn = cs_conn(cs);
1139
1140 if (((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW) &&
1141 (cs->state == CS_ST_EST) && (!conn || !(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS))))
1142 cs_shutw(cs);
1143 oc->wex = TICK_ETERNITY;
1144 }
1145
1146 /* indicate that we may be waiting for data from the output channel or
1147 * we're about to close and can't expect more data if SHUTW_NOW is there.
1148 */
1149 if (!(oc->flags & (CF_SHUTW|CF_SHUTW_NOW)))
1150 cs->endp->flags |= CS_EP_WAIT_DATA;
1151 else if ((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW)
1152 cs->endp->flags &= ~CS_EP_WAIT_DATA;
1153
1154 /* update OC timeouts and wake the other side up if it's waiting for room */
1155 if (oc->flags & CF_WRITE_ACTIVITY) {
1156 if ((oc->flags & (CF_SHUTW|CF_WRITE_PARTIAL)) == CF_WRITE_PARTIAL &&
1157 !channel_is_empty(oc))
1158 if (tick_isset(oc->wex))
1159 oc->wex = tick_add_ifset(now_ms, oc->wto);
1160
1161 if (!(cs->flags & CS_FL_INDEP_STR))
1162 if (tick_isset(ic->rex))
1163 ic->rex = tick_add_ifset(now_ms, ic->rto);
1164 }
1165
1166 if (oc->flags & CF_DONT_READ)
1167 cs_rx_chan_blk(cso);
1168 else
1169 cs_rx_chan_rdy(cso);
1170
1171 /* Notify the other side when we've injected data into the IC that
1172 * needs to be forwarded. We can do fast-forwarding as soon as there
1173 * are output data, but we avoid doing this if some of the data are
1174 * not yet scheduled for being forwarded, because it is very likely
1175 * that it will be done again immediately afterwards once the following
1176 * data are parsed (eg: HTTP chunking). We only CS_EP_RXBLK_ROOM once
1177 * we've emptied *some* of the output buffer, and not just when there
1178 * is available room, because applets are often forced to stop before
1179 * the buffer is full. We must not stop based on input data alone because
1180 * an HTTP parser might need more data to complete the parsing.
1181 */
1182 if (!channel_is_empty(ic) &&
1183 (cso->endp->flags & CS_EP_WAIT_DATA) &&
1184 (!(ic->flags & CF_EXPECT_MORE) || c_full(ic) || ci_data(ic) == 0 || ic->pipe)) {
1185 int new_len, last_len;
1186
1187 last_len = co_data(ic);
1188 if (ic->pipe)
1189 last_len += ic->pipe->data;
1190
1191 cs_chk_snd(cso);
1192
1193 new_len = co_data(ic);
1194 if (ic->pipe)
1195 new_len += ic->pipe->data;
1196
1197 /* check if the consumer has freed some space either in the
1198 * buffer or in the pipe.
1199 */
1200 if (new_len < last_len)
1201 cs_rx_room_rdy(cs);
1202 }
1203
1204 if (!(ic->flags & CF_DONT_READ))
1205 cs_rx_chan_rdy(cs);
1206
1207 cs_chk_rcv(cs);
1208 cs_chk_rcv(cso);
1209
1210 if (cs_rx_blocked(cs)) {
1211 ic->rex = TICK_ETERNITY;
1212 }
1213 else if ((ic->flags & (CF_SHUTR|CF_READ_PARTIAL)) == CF_READ_PARTIAL) {
1214 /* we must re-enable reading if cs_chk_snd() has freed some space */
1215 if (!(ic->flags & CF_READ_NOEXP) && tick_isset(ic->rex))
1216 ic->rex = tick_add_ifset(now_ms, ic->rto);
1217 }
1218
1219 /* wake the task up only when needed */
1220 if (/* changes on the production side */
1221 (ic->flags & (CF_READ_NULL|CF_READ_ERROR)) ||
1222 !cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST) ||
1223 (cs->endp->flags & CS_EP_ERROR) ||
1224 ((ic->flags & CF_READ_PARTIAL) &&
1225 ((ic->flags & CF_EOI) || !ic->to_forward || cso->state != CS_ST_EST)) ||
1226
1227 /* changes on the consumption side */
1228 (oc->flags & (CF_WRITE_NULL|CF_WRITE_ERROR)) ||
1229 ((oc->flags & CF_WRITE_ACTIVITY) &&
1230 ((oc->flags & CF_SHUTW) ||
1231 (((oc->flags & CF_WAKE_WRITE) ||
1232 !(oc->flags & (CF_AUTO_CLOSE|CF_SHUTW_NOW|CF_SHUTW))) &&
1233 (cso->state != CS_ST_EST ||
1234 (channel_is_empty(oc) && !oc->to_forward)))))) {
1235 task_wakeup(task, TASK_WOKEN_IO);
1236 }
1237 else {
1238 /* Update expiration date for the task and requeue it */
1239 task->expire = tick_first((tick_is_expired(task->expire, now_ms) ? 0 : task->expire),
1240 tick_first(tick_first(ic->rex, ic->wex),
1241 tick_first(oc->rex, oc->wex)));
1242
1243 task->expire = tick_first(task->expire, ic->analyse_exp);
1244 task->expire = tick_first(task->expire, oc->analyse_exp);
1245 task->expire = tick_first(task->expire, __cs_strm(cs)->conn_exp);
1246
1247 task_queue(task);
1248 }
1249 if (ic->flags & CF_READ_ACTIVITY)
1250 ic->flags &= ~CF_READ_DONTWAIT;
1251}
1252
1253/*
1254 * This function propagates a null read received on a socket-based connection.
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001255 * It updates the conn-stream. If the conn-stream has CS_FL_NOHALF,
Christopher Faulet5e29b762022-04-04 08:58:34 +02001256 * the close is also forwarded to the write side as an abort.
1257 */
1258static void cs_conn_read0(struct conn_stream *cs)
1259{
1260 struct channel *ic = cs_ic(cs);
1261 struct channel *oc = cs_oc(cs);
1262
1263 BUG_ON(!cs_conn(cs));
1264
1265 cs_rx_shut_blk(cs);
1266 if (ic->flags & CF_SHUTR)
1267 return;
1268 ic->flags |= CF_SHUTR;
1269 ic->rex = TICK_ETERNITY;
1270
1271 if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
1272 return;
1273
1274 if (oc->flags & CF_SHUTW)
1275 goto do_close;
1276
1277 if (cs->flags & CS_FL_NOHALF) {
1278 /* we want to immediately forward this close to the write side */
1279 /* force flag on ssl to keep stream in cache */
1280 cs_conn_shutw(cs, CO_SHW_SILENT);
1281 goto do_close;
1282 }
1283
1284 /* otherwise that's just a normal read shutdown */
1285 return;
1286
1287 do_close:
1288 /* OK we completely close the socket here just as if we went through cs_shut[rw]() */
Christopher Fauletff022a22022-04-21 08:38:54 +02001289 cs_conn_shut(cs);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001290
1291 oc->flags &= ~CF_SHUTW_NOW;
1292 oc->flags |= CF_SHUTW;
1293 oc->wex = TICK_ETERNITY;
1294
1295 cs_done_get(cs);
1296
1297 cs->state = CS_ST_DIS;
1298 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
1299 return;
1300}
1301
1302/*
1303 * This is the callback which is called by the connection layer to receive data
1304 * into the buffer from the connection. It iterates over the mux layer's
1305 * rcv_buf function.
1306 */
1307static int cs_conn_recv(struct conn_stream *cs)
1308{
1309 struct connection *conn = __cs_conn(cs);
1310 struct channel *ic = cs_ic(cs);
1311 int ret, max, cur_read = 0;
1312 int read_poll = MAX_READ_POLL_LOOPS;
1313 int flags = 0;
1314
1315 /* If not established yet, do nothing. */
1316 if (cs->state != CS_ST_EST)
1317 return 0;
1318
1319 /* If another call to cs_conn_recv() failed, and we subscribed to
1320 * recv events already, give up now.
1321 */
1322 if (cs->wait_event.events & SUB_RETRY_RECV)
1323 return 0;
1324
1325 /* maybe we were called immediately after an asynchronous shutr */
1326 if (ic->flags & CF_SHUTR)
1327 return 1;
1328
1329 /* we must wait because the mux is not installed yet */
1330 if (!conn->mux)
1331 return 0;
1332
1333 /* stop here if we reached the end of data */
1334 if (cs->endp->flags & CS_EP_EOS)
1335 goto end_recv;
1336
1337 /* stop immediately on errors. Note that we DON'T want to stop on
1338 * POLL_ERR, as the poller might report a write error while there
1339 * are still data available in the recv buffer. This typically
1340 * happens when we send too large a request to a backend server
1341 * which rejects it before reading it all.
1342 */
1343 if (!(cs->endp->flags & CS_EP_RCV_MORE)) {
1344 if (!conn_xprt_ready(conn))
1345 return 0;
1346 if (cs->endp->flags & CS_EP_ERROR)
1347 goto end_recv;
1348 }
1349
1350 /* prepare to detect if the mux needs more room */
1351 cs->endp->flags &= ~CS_EP_WANT_ROOM;
1352
1353 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) && !co_data(ic) &&
1354 global.tune.idle_timer &&
1355 (unsigned short)(now_ms - ic->last_read) >= global.tune.idle_timer) {
1356 /* The buffer was empty and nothing was transferred for more
1357 * than one second. This was caused by a pause and not by
1358 * congestion. Reset any streaming mode to reduce latency.
1359 */
1360 ic->xfer_small = 0;
1361 ic->xfer_large = 0;
1362 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
1363 }
1364
1365 /* First, let's see if we may splice data across the channel without
1366 * using a buffer.
1367 */
1368 if (cs->endp->flags & CS_EP_MAY_SPLICE &&
1369 (ic->pipe || ic->to_forward >= MIN_SPLICE_FORWARD) &&
1370 ic->flags & CF_KERN_SPLICING) {
1371 if (c_data(ic)) {
1372 /* We're embarrassed, there are already data pending in
1373 * the buffer and we don't want to have them at two
1374 * locations at a time. Let's indicate we need some
1375 * place and ask the consumer to hurry.
1376 */
1377 flags |= CO_RFL_BUF_FLUSH;
1378 goto abort_splice;
1379 }
1380
1381 if (unlikely(ic->pipe == NULL)) {
1382 if (pipes_used >= global.maxpipes || !(ic->pipe = get_pipe())) {
1383 ic->flags &= ~CF_KERN_SPLICING;
1384 goto abort_splice;
1385 }
1386 }
1387
1388 ret = conn->mux->rcv_pipe(cs, ic->pipe, ic->to_forward);
1389 if (ret < 0) {
1390 /* splice not supported on this end, let's disable it */
1391 ic->flags &= ~CF_KERN_SPLICING;
1392 goto abort_splice;
1393 }
1394
1395 if (ret > 0) {
1396 if (ic->to_forward != CHN_INFINITE_FORWARD)
1397 ic->to_forward -= ret;
1398 ic->total += ret;
1399 cur_read += ret;
1400 ic->flags |= CF_READ_PARTIAL;
1401 }
1402
1403 if (cs->endp->flags & (CS_EP_EOS|CS_EP_ERROR))
1404 goto end_recv;
1405
1406 if (conn->flags & CO_FL_WAIT_ROOM) {
1407 /* the pipe is full or we have read enough data that it
1408 * could soon be full. Let's stop before needing to poll.
1409 */
1410 cs_rx_room_blk(cs);
1411 goto done_recv;
1412 }
1413
1414 /* splice not possible (anymore), let's go on on standard copy */
1415 }
1416
1417 abort_splice:
1418 if (ic->pipe && unlikely(!ic->pipe->data)) {
1419 put_pipe(ic->pipe);
1420 ic->pipe = NULL;
1421 }
1422
1423 if (ic->pipe && ic->to_forward && !(flags & CO_RFL_BUF_FLUSH) && cs->endp->flags & CS_EP_MAY_SPLICE) {
1424 /* don't break splicing by reading, but still call rcv_buf()
1425 * to pass the flag.
1426 */
1427 goto done_recv;
1428 }
1429
1430 /* now we'll need a input buffer for the stream */
1431 if (!cs_alloc_ibuf(cs, &(__cs_strm(cs)->buffer_wait)))
1432 goto end_recv;
1433
1434 /* For an HTX stream, if the buffer is stuck (no output data with some
1435 * input data) and if the HTX message is fragmented or if its free space
1436 * wraps, we force an HTX deframentation. It is a way to have a
1437 * contiguous free space nad to let the mux to copy as much data as
1438 * possible.
1439 *
1440 * NOTE: A possible optim may be to let the mux decides if defrag is
1441 * required or not, depending on amount of data to be xferred.
1442 */
1443 if (IS_HTX_STRM(__cs_strm(cs)) && !co_data(ic)) {
1444 struct htx *htx = htxbuf(&ic->buf);
1445
1446 if (htx_is_not_empty(htx) && ((htx->flags & HTX_FL_FRAGMENTED) || htx_space_wraps(htx)))
1447 htx_defrag(htx, NULL, 0);
1448 }
1449
1450 /* Instruct the mux it must subscribed for read events */
1451 flags |= ((!conn_is_back(conn) && (__cs_strm(cs)->be->options & PR_O_ABRT_CLOSE)) ? CO_RFL_KEEP_RECV : 0);
1452
1453 /* Important note : if we're called with POLL_IN|POLL_HUP, it means the read polling
1454 * was enabled, which implies that the recv buffer was not full. So we have a guarantee
1455 * that if such an event is not handled above in splice, it will be handled here by
1456 * recv().
1457 */
1458 while ((cs->endp->flags & CS_EP_RCV_MORE) ||
1459 (!(conn->flags & CO_FL_HANDSHAKE) &&
1460 (!(cs->endp->flags & (CS_EP_ERROR|CS_EP_EOS))) && !(ic->flags & CF_SHUTR))) {
1461 int cur_flags = flags;
1462
1463 /* Compute transient CO_RFL_* flags */
1464 if (co_data(ic)) {
1465 cur_flags |= (CO_RFL_BUF_WET | CO_RFL_BUF_NOT_STUCK);
1466 }
1467
1468 /* <max> may be null. This is the mux responsibility to set
1469 * CS_EP_RCV_MORE on the CS if more space is needed.
1470 */
1471 max = channel_recv_max(ic);
1472 ret = conn->mux->rcv_buf(cs, &ic->buf, max, cur_flags);
1473
1474 if (cs->endp->flags & CS_EP_WANT_ROOM) {
1475 /* CS_EP_WANT_ROOM must not be reported if the channel's
1476 * buffer is empty.
1477 */
1478 BUG_ON(c_empty(ic));
1479
1480 cs_rx_room_blk(cs);
1481 /* Add READ_PARTIAL because some data are pending but
1482 * cannot be xferred to the channel
1483 */
1484 ic->flags |= CF_READ_PARTIAL;
1485 }
1486
1487 if (ret <= 0) {
1488 /* if we refrained from reading because we asked for a
1489 * flush to satisfy rcv_pipe(), we must not subscribe
1490 * and instead report that there's not enough room
1491 * here to proceed.
1492 */
1493 if (flags & CO_RFL_BUF_FLUSH)
1494 cs_rx_room_blk(cs);
1495 break;
1496 }
1497
1498 cur_read += ret;
1499
1500 /* if we're allowed to directly forward data, we must update ->o */
1501 if (ic->to_forward && !(ic->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
1502 unsigned long fwd = ret;
1503 if (ic->to_forward != CHN_INFINITE_FORWARD) {
1504 if (fwd > ic->to_forward)
1505 fwd = ic->to_forward;
1506 ic->to_forward -= fwd;
1507 }
1508 c_adv(ic, fwd);
1509 }
1510
1511 ic->flags |= CF_READ_PARTIAL;
1512 ic->total += ret;
1513
1514 /* End-of-input reached, we can leave. In this case, it is
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001515 * important to break the loop to not block the CS because of
Christopher Faulet5e29b762022-04-04 08:58:34 +02001516 * the channel's policies.This way, we are still able to receive
1517 * shutdowns.
1518 */
1519 if (cs->endp->flags & CS_EP_EOI)
1520 break;
1521
1522 if ((ic->flags & CF_READ_DONTWAIT) || --read_poll <= 0) {
1523 /* we're stopped by the channel's policy */
1524 cs_rx_chan_blk(cs);
1525 break;
1526 }
1527
1528 /* if too many bytes were missing from last read, it means that
1529 * it's pointless trying to read again because the system does
1530 * not have them in buffers.
1531 */
1532 if (ret < max) {
1533 /* if a streamer has read few data, it may be because we
1534 * have exhausted system buffers. It's not worth trying
1535 * again.
1536 */
1537 if (ic->flags & CF_STREAMER) {
1538 /* we're stopped by the channel's policy */
1539 cs_rx_chan_blk(cs);
1540 break;
1541 }
1542
1543 /* if we read a large block smaller than what we requested,
1544 * it's almost certain we'll never get anything more.
1545 */
1546 if (ret >= global.tune.recv_enough) {
1547 /* we're stopped by the channel's policy */
1548 cs_rx_chan_blk(cs);
1549 break;
1550 }
1551 }
1552
1553 /* if we are waiting for more space, don't try to read more data
1554 * right now.
1555 */
1556 if (cs_rx_blocked(cs))
1557 break;
1558 } /* while !flags */
1559
1560 done_recv:
1561 if (cur_read) {
1562 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) &&
1563 (cur_read <= ic->buf.size / 2)) {
1564 ic->xfer_large = 0;
1565 ic->xfer_small++;
1566 if (ic->xfer_small >= 3) {
1567 /* we have read less than half of the buffer in
1568 * one pass, and this happened at least 3 times.
1569 * This is definitely not a streamer.
1570 */
1571 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
1572 }
1573 else if (ic->xfer_small >= 2) {
1574 /* if the buffer has been at least half full twice,
1575 * we receive faster than we send, so at least it
1576 * is not a "fast streamer".
1577 */
1578 ic->flags &= ~CF_STREAMER_FAST;
1579 }
1580 }
1581 else if (!(ic->flags & CF_STREAMER_FAST) &&
1582 (cur_read >= ic->buf.size - global.tune.maxrewrite)) {
1583 /* we read a full buffer at once */
1584 ic->xfer_small = 0;
1585 ic->xfer_large++;
1586 if (ic->xfer_large >= 3) {
1587 /* we call this buffer a fast streamer if it manages
1588 * to be filled in one call 3 consecutive times.
1589 */
1590 ic->flags |= (CF_STREAMER | CF_STREAMER_FAST);
1591 }
1592 }
1593 else {
1594 ic->xfer_small = 0;
1595 ic->xfer_large = 0;
1596 }
1597 ic->last_read = now_ms;
1598 }
1599
1600 end_recv:
1601 ret = (cur_read != 0);
1602
1603 /* Report EOI on the channel if it was reached from the mux point of
1604 * view. */
1605 if ((cs->endp->flags & CS_EP_EOI) && !(ic->flags & CF_EOI)) {
1606 ic->flags |= (CF_EOI|CF_READ_PARTIAL);
1607 ret = 1;
1608 }
1609
1610 if (cs->endp->flags & CS_EP_ERROR)
1611 ret = 1;
1612 else if (cs->endp->flags & CS_EP_EOS) {
1613 /* we received a shutdown */
1614 ic->flags |= CF_READ_NULL;
1615 if (ic->flags & CF_AUTO_CLOSE)
1616 channel_shutw_now(ic);
1617 cs_conn_read0(cs);
1618 ret = 1;
1619 }
1620 else if (!cs_rx_blocked(cs)) {
1621 /* Subscribe to receive events if we're blocking on I/O */
1622 conn->mux->subscribe(cs, SUB_RETRY_RECV, &cs->wait_event);
1623 cs_rx_endp_done(cs);
1624 } else {
1625 cs_rx_endp_more(cs);
1626 ret = 1;
1627 }
1628 return ret;
1629}
1630
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001631/* This tries to perform a synchronous receive on the conn-stream to
Christopher Faulet5e29b762022-04-04 08:58:34 +02001632 * try to collect last arrived data. In practice it's only implemented on
1633 * conn_streams. Returns 0 if nothing was done, non-zero if new data or a
1634 * shutdown were collected. This may result on some delayed receive calls
1635 * to be programmed and performed later, though it doesn't provide any
1636 * such guarantee.
1637 */
1638int cs_conn_sync_recv(struct conn_stream *cs)
1639{
1640 if (!cs_state_in(cs->state, CS_SB_RDY|CS_SB_EST))
1641 return 0;
1642
1643 if (!cs_conn_mux(cs))
1644 return 0; // only conn_streams are supported
1645
1646 if (cs->wait_event.events & SUB_RETRY_RECV)
1647 return 0; // already subscribed
1648
1649 if (!cs_rx_endp_ready(cs) || cs_rx_blocked(cs))
1650 return 0; // already failed
1651
1652 return cs_conn_recv(cs);
1653}
1654
1655/*
1656 * This function is called to send buffer data to a stream socket.
1657 * It calls the mux layer's snd_buf function. It relies on the
1658 * caller to commit polling changes. The caller should check conn->flags
1659 * for errors.
1660 */
1661static int cs_conn_send(struct conn_stream *cs)
1662{
1663 struct connection *conn = __cs_conn(cs);
1664 struct stream *s = __cs_strm(cs);
1665 struct channel *oc = cs_oc(cs);
1666 int ret;
1667 int did_send = 0;
1668
1669 if (cs->endp->flags & (CS_EP_ERROR|CS_EP_ERR_PENDING) || cs_is_conn_error(cs)) {
1670 /* We're probably there because the tasklet was woken up,
1671 * but process_stream() ran before, detected there were an
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001672 * error and put the CS back to CS_ST_TAR. There's still
Christopher Faulet5e29b762022-04-04 08:58:34 +02001673 * CO_FL_ERROR on the connection but we don't want to add
1674 * CS_EP_ERROR back, so give up
1675 */
1676 if (cs->state < CS_ST_CON)
1677 return 0;
1678 cs->endp->flags |= CS_EP_ERROR;
1679 return 1;
1680 }
1681
1682 /* We're already waiting to be able to send, give up */
1683 if (cs->wait_event.events & SUB_RETRY_SEND)
1684 return 0;
1685
1686 /* we might have been called just after an asynchronous shutw */
1687 if (oc->flags & CF_SHUTW)
1688 return 1;
1689
1690 /* we must wait because the mux is not installed yet */
1691 if (!conn->mux)
1692 return 0;
1693
1694 if (oc->pipe && conn->xprt->snd_pipe && conn->mux->snd_pipe) {
1695 ret = conn->mux->snd_pipe(cs, oc->pipe);
1696 if (ret > 0)
1697 did_send = 1;
1698
1699 if (!oc->pipe->data) {
1700 put_pipe(oc->pipe);
1701 oc->pipe = NULL;
1702 }
1703
1704 if (oc->pipe)
1705 goto end;
1706 }
1707
1708 /* At this point, the pipe is empty, but we may still have data pending
1709 * in the normal buffer.
1710 */
1711 if (co_data(oc)) {
1712 /* when we're here, we already know that there is no spliced
1713 * data left, and that there are sendable buffered data.
1714 */
1715
1716 /* check if we want to inform the kernel that we're interested in
1717 * sending more data after this call. We want this if :
1718 * - we're about to close after this last send and want to merge
1719 * the ongoing FIN with the last segment.
1720 * - we know we can't send everything at once and must get back
1721 * here because of unaligned data
1722 * - there is still a finite amount of data to forward
1723 * The test is arranged so that the most common case does only 2
1724 * tests.
1725 */
1726 unsigned int send_flag = 0;
1727
1728 if ((!(oc->flags & (CF_NEVER_WAIT|CF_SEND_DONTWAIT)) &&
1729 ((oc->to_forward && oc->to_forward != CHN_INFINITE_FORWARD) ||
1730 (oc->flags & CF_EXPECT_MORE) ||
1731 (IS_HTX_STRM(s) &&
1732 (!(oc->flags & (CF_EOI|CF_SHUTR)) && htx_expect_more(htxbuf(&oc->buf)))))) ||
1733 ((oc->flags & CF_ISRESP) &&
1734 ((oc->flags & (CF_AUTO_CLOSE|CF_SHUTW_NOW)) == (CF_AUTO_CLOSE|CF_SHUTW_NOW))))
1735 send_flag |= CO_SFL_MSG_MORE;
1736
1737 if (oc->flags & CF_STREAMER)
1738 send_flag |= CO_SFL_STREAMER;
1739
1740 if (s->txn && s->txn->flags & TX_L7_RETRY && !b_data(&s->txn->l7_buffer)) {
1741 /* If we want to be able to do L7 retries, copy
1742 * the data we're about to send, so that we are able
1743 * to resend them if needed
1744 */
1745 /* Try to allocate a buffer if we had none.
1746 * If it fails, the next test will just
1747 * disable the l7 retries by setting
1748 * l7_conn_retries to 0.
1749 */
1750 if (s->txn->req.msg_state != HTTP_MSG_DONE)
1751 s->txn->flags &= ~TX_L7_RETRY;
1752 else {
1753 if (b_alloc(&s->txn->l7_buffer) == NULL)
1754 s->txn->flags &= ~TX_L7_RETRY;
1755 else {
1756 memcpy(b_orig(&s->txn->l7_buffer),
1757 b_orig(&oc->buf),
1758 b_size(&oc->buf));
1759 s->txn->l7_buffer.head = co_data(oc);
1760 b_add(&s->txn->l7_buffer, co_data(oc));
1761 }
1762
1763 }
1764 }
1765
1766 ret = conn->mux->snd_buf(cs, &oc->buf, co_data(oc), send_flag);
1767 if (ret > 0) {
1768 did_send = 1;
1769 c_rew(oc, ret);
1770 c_realign_if_empty(oc);
1771
1772 if (!co_data(oc)) {
1773 /* Always clear both flags once everything has been sent, they're one-shot */
1774 oc->flags &= ~(CF_EXPECT_MORE | CF_SEND_DONTWAIT);
1775 }
1776 /* if some data remain in the buffer, it's only because the
1777 * system buffers are full, we will try next time.
1778 */
Christopher Faulet13045f02022-04-01 14:23:38 +02001779 }
1780 }
Christopher Faulet5e29b762022-04-04 08:58:34 +02001781
1782 end:
1783 if (did_send) {
1784 oc->flags |= CF_WRITE_PARTIAL | CF_WROTE_DATA;
1785 if (cs->state == CS_ST_CON)
1786 cs->state = CS_ST_RDY;
1787
1788 cs_rx_room_rdy(cs_opposite(cs));
1789 }
1790
1791 if (cs->endp->flags & (CS_EP_ERROR|CS_EP_ERR_PENDING)) {
1792 cs->endp->flags |= CS_EP_ERROR;
1793 return 1;
1794 }
1795
1796 /* We couldn't send all of our data, let the mux know we'd like to send more */
1797 if (!channel_is_empty(oc))
1798 conn->mux->subscribe(cs, SUB_RETRY_SEND, &cs->wait_event);
1799 return did_send;
1800}
1801
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001802/* perform a synchronous send() for the conn-stream. The CF_WRITE_NULL and
Christopher Faulet5e29b762022-04-04 08:58:34 +02001803 * CF_WRITE_PARTIAL flags are cleared prior to the attempt, and will possibly
1804 * be updated in case of success.
1805 */
1806void cs_conn_sync_send(struct conn_stream *cs)
1807{
1808 struct channel *oc = cs_oc(cs);
1809
1810 oc->flags &= ~(CF_WRITE_NULL|CF_WRITE_PARTIAL);
1811
1812 if (oc->flags & CF_SHUTW)
1813 return;
1814
1815 if (channel_is_empty(oc))
1816 return;
1817
1818 if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
1819 return;
1820
1821 if (!cs_conn_mux(cs))
1822 return;
1823
1824 cs_conn_send(cs);
1825}
1826
1827/* Called by I/O handlers after completion.. It propagates
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001828 * connection flags to the conn-stream, updates the stream (which may or
Christopher Faulet5e29b762022-04-04 08:58:34 +02001829 * may not take this opportunity to try to forward data), then update the
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001830 * connection's polling based on the channels and conn-stream's final
Christopher Faulet5e29b762022-04-04 08:58:34 +02001831 * states. The function always returns 0.
1832 */
1833static int cs_conn_process(struct conn_stream *cs)
1834{
1835 struct connection *conn = __cs_conn(cs);
1836 struct channel *ic = cs_ic(cs);
1837 struct channel *oc = cs_oc(cs);
1838
1839 BUG_ON(!conn);
1840
1841 /* If we have data to send, try it now */
1842 if (!channel_is_empty(oc) && !(cs->wait_event.events & SUB_RETRY_SEND))
1843 cs_conn_send(cs);
1844
1845 /* First step, report to the conn-stream what was detected at the
1846 * connection layer : errors and connection establishment.
1847 * Only add CS_EP_ERROR if we're connected, or we're attempting to
1848 * connect, we may get there because we got woken up, but only run
1849 * after process_stream() noticed there were an error, and decided
1850 * to retry to connect, the connection may still have CO_FL_ERROR,
1851 * and we don't want to add CS_EP_ERROR back
1852 *
1853 * Note: This test is only required because cs_conn_process is also the SI
1854 * wake callback. Otherwise cs_conn_recv()/cs_conn_send() already take
1855 * care of it.
1856 */
1857
1858 if (cs->state >= CS_ST_CON) {
1859 if (cs_is_conn_error(cs))
1860 cs->endp->flags |= CS_EP_ERROR;
1861 }
1862
1863 /* If we had early data, and the handshake ended, then
1864 * we can remove the flag, and attempt to wake the task up,
1865 * in the event there's an analyser waiting for the end of
1866 * the handshake.
1867 */
1868 if (!(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS)) &&
1869 (cs->endp->flags & CS_EP_WAIT_FOR_HS)) {
1870 cs->endp->flags &= ~CS_EP_WAIT_FOR_HS;
1871 task_wakeup(cs_strm_task(cs), TASK_WOKEN_MSG);
1872 }
1873
1874 if (!cs_state_in(cs->state, CS_SB_EST|CS_SB_DIS|CS_SB_CLO) &&
1875 (conn->flags & CO_FL_WAIT_XPRT) == 0) {
1876 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
1877 oc->flags |= CF_WRITE_NULL;
1878 if (cs->state == CS_ST_CON)
1879 cs->state = CS_ST_RDY;
1880 }
1881
1882 /* Report EOS on the channel if it was reached from the mux point of
1883 * view.
1884 *
1885 * Note: This test is only required because cs_conn_process is also the SI
1886 * wake callback. Otherwise cs_conn_recv()/cs_conn_send() already take
1887 * care of it.
1888 */
1889 if (cs->endp->flags & CS_EP_EOS && !(ic->flags & CF_SHUTR)) {
1890 /* we received a shutdown */
1891 ic->flags |= CF_READ_NULL;
1892 if (ic->flags & CF_AUTO_CLOSE)
1893 channel_shutw_now(ic);
1894 cs_conn_read0(cs);
1895 }
1896
1897 /* Report EOI on the channel if it was reached from the mux point of
1898 * view.
1899 *
1900 * Note: This test is only required because cs_conn_process is also the SI
1901 * wake callback. Otherwise cs_conn_recv()/cs_conn_send() already take
1902 * care of it.
1903 */
1904 if ((cs->endp->flags & CS_EP_EOI) && !(ic->flags & CF_EOI))
1905 ic->flags |= (CF_EOI|CF_READ_PARTIAL);
1906
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001907 /* Second step : update the conn-stream and channels, try to forward any
Christopher Faulet5e29b762022-04-04 08:58:34 +02001908 * pending data, then possibly wake the stream up based on the new
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001909 * conn-stream status.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001910 */
1911 cs_notify(cs);
1912 stream_release_buffers(__cs_strm(cs));
1913 return 0;
1914}
1915
1916/* This is the ->process() function for any conn-stream's wait_event task.
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001917 * It's assigned during the conn-stream's initialization, for any type of
1918 * conn-stream. Thus it is always safe to perform a tasklet_wakeup() on a
1919 * conn-stream, as the presence of the CS is checked there.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001920 */
1921struct task *cs_conn_io_cb(struct task *t, void *ctx, unsigned int state)
1922{
1923 struct conn_stream *cs = ctx;
1924 int ret = 0;
1925
1926 if (!cs_conn(cs))
1927 return t;
1928
1929 if (!(cs->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(cs_oc(cs)))
1930 ret = cs_conn_send(cs);
1931 if (!(cs->wait_event.events & SUB_RETRY_RECV))
1932 ret |= cs_conn_recv(cs);
1933 if (ret != 0)
1934 cs_conn_process(cs);
1935
1936 stream_release_buffers(__cs_strm(cs));
1937 return t;
1938}
1939
1940/* Callback to be used by applet handlers upon completion. It updates the stream
1941 * (which may or may not take this opportunity to try to forward data), then
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001942 * may re-enable the applet's based on the channels and conn-stream's final
Christopher Faulet5e29b762022-04-04 08:58:34 +02001943 * states.
1944 */
1945static int cs_applet_process(struct conn_stream *cs)
1946{
1947 struct channel *ic = cs_ic(cs);
1948
1949 BUG_ON(!cs_appctx(cs));
1950
1951 /* If the applet wants to write and the channel is closed, it's a
1952 * broken pipe and it must be reported.
1953 */
1954 if (!(cs->endp->flags & CS_EP_RX_WAIT_EP) && (ic->flags & CF_SHUTR))
1955 cs->endp->flags |= CS_EP_ERROR;
1956
1957 /* automatically mark the applet having data available if it reported
1958 * begin blocked by the channel.
1959 */
1960 if (cs_rx_blocked(cs))
1961 cs_rx_endp_more(cs);
1962
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001963 /* update the conn-stream, channels, and possibly wake the stream up */
Christopher Faulet5e29b762022-04-04 08:58:34 +02001964 cs_notify(cs);
1965 stream_release_buffers(__cs_strm(cs));
1966
1967 /* cs_notify may have passed through chk_snd and released some
1968 * RXBLK flags. Process_stream will consider those flags to wake up the
1969 * appctx but in the case the task is not in runqueue we may have to
1970 * wakeup the appctx immediately.
1971 */
1972 if ((cs_rx_endp_ready(cs) && !cs_rx_blocked(cs)) ||
1973 (cs_tx_endp_ready(cs) && !cs_tx_blocked(cs)))
1974 appctx_wakeup(__cs_appctx(cs));
1975 return 0;
Christopher Faulet13045f02022-04-01 14:23:38 +02001976}