blob: 819f2cb4a54209fca38117ddaa69de7f01409d3d [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
Christopher Fauletca6c9bb2022-04-21 08:44:09 +0200391 cs_applet_shut(cs);
Willy Tarreauefb46182022-05-10 09:04:18 +0200392 cs->endp->flags |= CS_EP_ORPHAN;
393 cs->endp->cs = NULL;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100394 appctx_free(appctx);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100395 cs->endp = NULL;
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
517/* call the applet's release function if any. Needs to be called upon close() */
Christopher Fauletca6c9bb2022-04-21 08:44:09 +0200518void cs_applet_shut(struct conn_stream *cs)
Christopher Faulet37046632022-04-01 11:36:58 +0200519{
520 struct appctx *appctx = __cs_appctx(cs);
521
Christopher Faulet02ef0ff2022-04-21 08:50:00 +0200522 if (cs->endp->flags & (CS_EP_SHR|CS_EP_SHW))
523 return;
524
525 if (appctx->applet->release)
Christopher Faulet37046632022-04-01 11:36:58 +0200526 appctx->applet->release(appctx);
Christopher Faulet02ef0ff2022-04-21 08:50:00 +0200527
528 cs->endp->flags |= CS_EP_SHRR | CS_EP_SHWN;
Christopher Faulet37046632022-04-01 11:36:58 +0200529}
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200530
531/*
532 * This function performs a shutdown-read on a detached conn-stream in a
533 * connected or init state (it does nothing for other states). It either shuts
534 * the read side or marks itself as closed. The buffer flags are updated to
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +0200535 * reflect the new state. If the conn-stream has CS_FL_NOHALF, we also
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200536 * forward the close to the write side. The owner task is woken up if it exists.
537 */
538static void cs_app_shutr(struct conn_stream *cs)
539{
540 struct channel *ic = cs_ic(cs);
541
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200542 cs_rx_shut_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200543 if (ic->flags & CF_SHUTR)
544 return;
545 ic->flags |= CF_SHUTR;
546 ic->rex = TICK_ETERNITY;
547
548 if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
549 return;
550
551 if (cs_oc(cs)->flags & CF_SHUTW) {
552 cs->state = CS_ST_DIS;
553 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
554 }
555 else if (cs->flags & CS_FL_NOHALF) {
556 /* we want to immediately forward this close to the write side */
557 return cs_app_shutw(cs);
558 }
559
560 /* note that if the task exists, it must unregister itself once it runs */
561 if (!(cs->flags & CS_FL_DONT_WAKE))
562 task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO);
563}
564
565/*
566 * This function performs a shutdown-write on a detached conn-stream in a
567 * connected or init state (it does nothing for other states). It either shuts
568 * the write side or marks itself as closed. The buffer flags are updated to
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +0200569 * reflect the new state. It does also close everything if the CS was marked as
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200570 * being in error state. The owner task is woken up if it exists.
571 */
572static void cs_app_shutw(struct conn_stream *cs)
573{
574 struct channel *ic = cs_ic(cs);
575 struct channel *oc = cs_oc(cs);
576
577 oc->flags &= ~CF_SHUTW_NOW;
578 if (oc->flags & CF_SHUTW)
579 return;
580 oc->flags |= CF_SHUTW;
581 oc->wex = TICK_ETERNITY;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200582 cs_done_get(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200583
584 if (tick_isset(cs->hcto)) {
585 ic->rto = cs->hcto;
586 ic->rex = tick_add(now_ms, ic->rto);
587 }
588
589 switch (cs->state) {
590 case CS_ST_RDY:
591 case CS_ST_EST:
592 /* we have to shut before closing, otherwise some short messages
593 * may never leave the system, especially when there are remaining
594 * unread data in the socket input buffer, or when nolinger is set.
595 * However, if CS_FL_NOLINGER is explicitly set, we know there is
596 * no risk so we close both sides immediately.
597 */
598 if (!(cs->endp->flags & CS_EP_ERROR) && !(cs->flags & CS_FL_NOLINGER) &&
599 !(ic->flags & (CF_SHUTR|CF_DONT_READ)))
600 return;
601
602 /* fall through */
603 case CS_ST_CON:
604 case CS_ST_CER:
605 case CS_ST_QUE:
606 case CS_ST_TAR:
607 /* Note that none of these states may happen with applets */
608 cs->state = CS_ST_DIS;
609 /* fall through */
610 default:
611 cs->flags &= ~CS_FL_NOLINGER;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200612 cs_rx_shut_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200613 ic->flags |= CF_SHUTR;
614 ic->rex = TICK_ETERNITY;
615 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
616 }
617
618 /* note that if the task exists, it must unregister itself once it runs */
619 if (!(cs->flags & CS_FL_DONT_WAKE))
620 task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO);
621}
622
623/* default chk_rcv function for scheduled tasks */
624static void cs_app_chk_rcv(struct conn_stream *cs)
625{
626 struct channel *ic = cs_ic(cs);
627
628 DPRINTF(stderr, "%s: cs=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n",
629 __FUNCTION__,
630 cs, cs->state, ic->flags, cs_oc(cs)->flags);
631
632 if (ic->pipe) {
633 /* stop reading */
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200634 cs_rx_room_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200635 }
636 else {
637 /* (re)start reading */
638 if (!(cs->flags & CS_FL_DONT_WAKE))
639 task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO);
640 }
641}
642
643/* default chk_snd function for scheduled tasks */
644static void cs_app_chk_snd(struct conn_stream *cs)
645{
646 struct channel *oc = cs_oc(cs);
647
648 DPRINTF(stderr, "%s: cs=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n",
649 __FUNCTION__,
650 cs, cs->state, cs_ic(cs)->flags, oc->flags);
651
652 if (unlikely(cs->state != CS_ST_EST || (oc->flags & CF_SHUTW)))
653 return;
654
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200655 if (!(cs->endp->flags & CS_EP_WAIT_DATA) || /* not waiting for data */
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200656 channel_is_empty(oc)) /* called with nothing to send ! */
657 return;
658
659 /* Otherwise there are remaining data to be sent in the buffer,
660 * so we tell the handler.
661 */
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200662 cs->endp->flags &= ~CS_EP_WAIT_DATA;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200663 if (!tick_isset(oc->wex))
664 oc->wex = tick_add_ifset(now_ms, oc->wto);
665
666 if (!(cs->flags & CS_FL_DONT_WAKE))
667 task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO);
668}
669
670/*
671 * This function performs a shutdown-read on a conn-stream attached to
672 * a connection in a connected or init state (it does nothing for other
673 * states). It either shuts the read side or marks itself as closed. The buffer
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +0200674 * flags are updated to reflect the new state. If the conn-stream has
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200675 * CS_FL_NOHALF, we also forward the close to the write side. If a control
676 * layer is defined, then it is supposed to be a socket layer and file
677 * descriptors are then shutdown or closed accordingly. The function
678 * automatically disables polling if needed.
679 */
680static void cs_app_shutr_conn(struct conn_stream *cs)
681{
682 struct channel *ic = cs_ic(cs);
683
684 BUG_ON(!cs_conn(cs));
685
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200686 cs_rx_shut_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200687 if (ic->flags & CF_SHUTR)
688 return;
689 ic->flags |= CF_SHUTR;
690 ic->rex = TICK_ETERNITY;
691
692 if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
693 return;
694
695 if (cs_oc(cs)->flags & CF_SHUTW) {
Christopher Fauletff022a22022-04-21 08:38:54 +0200696 cs_conn_shut(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200697 cs->state = CS_ST_DIS;
698 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
699 }
700 else if (cs->flags & CS_FL_NOHALF) {
701 /* we want to immediately forward this close to the write side */
702 return cs_app_shutw_conn(cs);
703 }
704}
705
706/*
707 * This function performs a shutdown-write on a conn-stream attached to
708 * a connection in a connected or init state (it does nothing for other
709 * states). It either shuts the write side or marks itself as closed. The
710 * buffer flags are updated to reflect the new state. It does also close
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +0200711 * everything if the CS was marked as being in error state. If there is a
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200712 * data-layer shutdown, it is called.
713 */
714static void cs_app_shutw_conn(struct conn_stream *cs)
715{
716 struct channel *ic = cs_ic(cs);
717 struct channel *oc = cs_oc(cs);
718
719 BUG_ON(!cs_conn(cs));
720
721 oc->flags &= ~CF_SHUTW_NOW;
722 if (oc->flags & CF_SHUTW)
723 return;
724 oc->flags |= CF_SHUTW;
725 oc->wex = TICK_ETERNITY;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200726 cs_done_get(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200727
728 if (tick_isset(cs->hcto)) {
729 ic->rto = cs->hcto;
730 ic->rex = tick_add(now_ms, ic->rto);
731 }
732
733 switch (cs->state) {
734 case CS_ST_RDY:
735 case CS_ST_EST:
736 /* we have to shut before closing, otherwise some short messages
737 * may never leave the system, especially when there are remaining
738 * unread data in the socket input buffer, or when nolinger is set.
739 * However, if CS_FL_NOLINGER is explicitly set, we know there is
740 * no risk so we close both sides immediately.
741 */
742
743 if (cs->endp->flags & CS_EP_ERROR) {
744 /* quick close, the socket is already shut anyway */
745 }
746 else if (cs->flags & CS_FL_NOLINGER) {
747 /* unclean data-layer shutdown, typically an aborted request
748 * or a forwarded shutdown from a client to a server due to
749 * option abortonclose. No need for the TLS layer to try to
750 * emit a shutdown message.
751 */
752 cs_conn_shutw(cs, CO_SHW_SILENT);
753 }
754 else {
755 /* clean data-layer shutdown. This only happens on the
756 * frontend side, or on the backend side when forwarding
757 * a client close in TCP mode or in HTTP TUNNEL mode
758 * while option abortonclose is set. We want the TLS
759 * layer to try to signal it to the peer before we close.
760 */
761 cs_conn_shutw(cs, CO_SHW_NORMAL);
762
763 if (!(ic->flags & (CF_SHUTR|CF_DONT_READ)))
764 return;
765 }
766
767 /* fall through */
768 case CS_ST_CON:
769 /* we may have to close a pending connection, and mark the
770 * response buffer as shutr
771 */
Christopher Fauletff022a22022-04-21 08:38:54 +0200772 cs_conn_shut(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200773 /* fall through */
774 case CS_ST_CER:
775 case CS_ST_QUE:
776 case CS_ST_TAR:
777 cs->state = CS_ST_DIS;
778 /* fall through */
779 default:
780 cs->flags &= ~CS_FL_NOLINGER;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200781 cs_rx_shut_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200782 ic->flags |= CF_SHUTR;
783 ic->rex = TICK_ETERNITY;
784 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
785 }
786}
787
788/* This function is used for inter-conn-stream calls. It is called by the
789 * consumer to inform the producer side that it may be interested in checking
790 * for free space in the buffer. Note that it intentionally does not update
791 * timeouts, so that we can still check them later at wake-up. This function is
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +0200792 * dedicated to connection-based conn-streams.
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200793 */
794static void cs_app_chk_rcv_conn(struct conn_stream *cs)
795{
796 BUG_ON(!cs_conn(cs));
797
798 /* (re)start reading */
799 if (cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
800 tasklet_wakeup(cs->wait_event.tasklet);
801}
802
803
804/* This function is used for inter-conn-stream calls. It is called by the
805 * producer to inform the consumer side that it may be interested in checking
806 * for data in the buffer. Note that it intentionally does not update timeouts,
807 * so that we can still check them later at wake-up.
808 */
809static void cs_app_chk_snd_conn(struct conn_stream *cs)
810{
811 struct channel *oc = cs_oc(cs);
812
813 BUG_ON(!cs_conn(cs));
814
Willy Tarreau4173f4e2022-04-29 15:04:41 +0200815 if (unlikely(!cs_state_in(cs->state, CS_SB_RDY|CS_SB_EST) ||
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200816 (oc->flags & CF_SHUTW)))
817 return;
818
819 if (unlikely(channel_is_empty(oc))) /* called with nothing to send ! */
820 return;
821
822 if (!oc->pipe && /* spliced data wants to be forwarded ASAP */
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200823 !(cs->endp->flags & CS_EP_WAIT_DATA)) /* not waiting for data */
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200824 return;
825
826 if (!(cs->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(cs_oc(cs)))
Christopher Faulet000ba3e2022-04-01 17:06:32 +0200827 cs_conn_send(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200828
Christopher Faulet158f3362022-04-01 17:15:10 +0200829 if (cs->endp->flags & (CS_EP_ERROR|CS_EP_ERR_PENDING) || cs_is_conn_error(cs)) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200830 /* Write error on the file descriptor */
831 if (cs->state >= CS_ST_CON)
832 cs->endp->flags |= CS_EP_ERROR;
833 goto out_wakeup;
834 }
835
836 /* OK, so now we know that some data might have been sent, and that we may
837 * have to poll first. We have to do that too if the buffer is not empty.
838 */
839 if (channel_is_empty(oc)) {
840 /* the connection is established but we can't write. Either the
841 * buffer is empty, or we just refrain from sending because the
842 * ->o limit was reached. Maybe we just wrote the last
843 * chunk and need to close.
844 */
845 if (((oc->flags & (CF_SHUTW|CF_AUTO_CLOSE|CF_SHUTW_NOW)) ==
846 (CF_AUTO_CLOSE|CF_SHUTW_NOW)) &&
847 cs_state_in(cs->state, CS_SB_RDY|CS_SB_EST)) {
848 cs_shutw(cs);
849 goto out_wakeup;
850 }
851
852 if ((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == 0)
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200853 cs->endp->flags |= CS_EP_WAIT_DATA;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200854 oc->wex = TICK_ETERNITY;
855 }
856 else {
857 /* Otherwise there are remaining data to be sent in the buffer,
858 * which means we have to poll before doing so.
859 */
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200860 cs->endp->flags &= ~CS_EP_WAIT_DATA;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200861 if (!tick_isset(oc->wex))
862 oc->wex = tick_add_ifset(now_ms, oc->wto);
863 }
864
865 if (likely(oc->flags & CF_WRITE_ACTIVITY)) {
866 struct channel *ic = cs_ic(cs);
867
868 /* update timeout if we have written something */
869 if ((oc->flags & (CF_SHUTW|CF_WRITE_PARTIAL)) == CF_WRITE_PARTIAL &&
870 !channel_is_empty(oc))
871 oc->wex = tick_add_ifset(now_ms, oc->wto);
872
873 if (tick_isset(ic->rex) && !(cs->flags & CS_FL_INDEP_STR)) {
874 /* Note: to prevent the client from expiring read timeouts
875 * during writes, we refresh it. We only do this if the
876 * interface is not configured for "independent streams",
877 * because for some applications it's better not to do this,
878 * for instance when continuously exchanging small amounts
879 * of data which can full the socket buffers long before a
880 * write timeout is detected.
881 */
882 ic->rex = tick_add_ifset(now_ms, ic->rto);
883 }
884 }
885
886 /* in case of special condition (error, shutdown, end of write...), we
887 * have to notify the task.
888 */
889 if (likely((oc->flags & (CF_WRITE_NULL|CF_WRITE_ERROR|CF_SHUTW)) ||
890 ((oc->flags & CF_WAKE_WRITE) &&
891 ((channel_is_empty(oc) && !oc->to_forward) ||
892 !cs_state_in(cs->state, CS_SB_EST))))) {
893 out_wakeup:
894 if (!(cs->flags & CS_FL_DONT_WAKE))
895 task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO);
896 }
897}
898
899/*
900 * This function performs a shutdown-read on a conn-stream attached to an
901 * applet in a connected or init state (it does nothing for other states). It
902 * either shuts the read side or marks itself as closed. The buffer flags are
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +0200903 * updated to reflect the new state. If the conn-stream has CS_FL_NOHALF,
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200904 * we also forward the close to the write side. The owner task is woken up if
905 * it exists.
906 */
907static void cs_app_shutr_applet(struct conn_stream *cs)
908{
909 struct channel *ic = cs_ic(cs);
910
911 BUG_ON(!cs_appctx(cs));
912
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200913 cs_rx_shut_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200914 if (ic->flags & CF_SHUTR)
915 return;
916 ic->flags |= CF_SHUTR;
917 ic->rex = TICK_ETERNITY;
918
919 /* Note: on shutr, we don't call the applet */
920
921 if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
922 return;
923
924 if (cs_oc(cs)->flags & CF_SHUTW) {
Christopher Fauletca6c9bb2022-04-21 08:44:09 +0200925 cs_applet_shut(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200926 cs->state = CS_ST_DIS;
927 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
928 }
929 else if (cs->flags & CS_FL_NOHALF) {
930 /* we want to immediately forward this close to the write side */
931 return cs_app_shutw_applet(cs);
932 }
933}
934
935/*
936 * This function performs a shutdown-write on a conn-stream attached to an
937 * applet in a connected or init state (it does nothing for other states). It
938 * either shuts the write side or marks itself as closed. The buffer flags are
939 * updated to reflect the new state. It does also close everything if the SI
940 * was marked as being in error state. The owner task is woken up if it exists.
941 */
942static void cs_app_shutw_applet(struct conn_stream *cs)
943{
944 struct channel *ic = cs_ic(cs);
945 struct channel *oc = cs_oc(cs);
946
947 BUG_ON(!cs_appctx(cs));
948
949 oc->flags &= ~CF_SHUTW_NOW;
950 if (oc->flags & CF_SHUTW)
951 return;
952 oc->flags |= CF_SHUTW;
953 oc->wex = TICK_ETERNITY;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200954 cs_done_get(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200955
956 if (tick_isset(cs->hcto)) {
957 ic->rto = cs->hcto;
958 ic->rex = tick_add(now_ms, ic->rto);
959 }
960
961 /* on shutw we always wake the applet up */
962 appctx_wakeup(__cs_appctx(cs));
963
964 switch (cs->state) {
965 case CS_ST_RDY:
966 case CS_ST_EST:
967 /* we have to shut before closing, otherwise some short messages
968 * may never leave the system, especially when there are remaining
969 * unread data in the socket input buffer, or when nolinger is set.
970 * However, if CS_FL_NOLINGER is explicitly set, we know there is
971 * no risk so we close both sides immediately.
972 */
973 if (!(cs->endp->flags & CS_EP_ERROR) && !(cs->flags & CS_FL_NOLINGER) &&
974 !(ic->flags & (CF_SHUTR|CF_DONT_READ)))
975 return;
976
977 /* fall through */
978 case CS_ST_CON:
979 case CS_ST_CER:
980 case CS_ST_QUE:
981 case CS_ST_TAR:
982 /* Note that none of these states may happen with applets */
Christopher Fauletca6c9bb2022-04-21 08:44:09 +0200983 cs_applet_shut(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200984 cs->state = CS_ST_DIS;
985 /* fall through */
986 default:
987 cs->flags &= ~CS_FL_NOLINGER;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200988 cs_rx_shut_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200989 ic->flags |= CF_SHUTR;
990 ic->rex = TICK_ETERNITY;
991 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
992 }
993}
994
995/* chk_rcv function for applets */
996static void cs_app_chk_rcv_applet(struct conn_stream *cs)
997{
998 struct channel *ic = cs_ic(cs);
999
1000 BUG_ON(!cs_appctx(cs));
1001
1002 DPRINTF(stderr, "%s: cs=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n",
1003 __FUNCTION__,
1004 cs, cs->state, ic->flags, cs_oc(cs)->flags);
1005
1006 if (!ic->pipe) {
1007 /* (re)start reading */
1008 appctx_wakeup(__cs_appctx(cs));
1009 }
1010}
1011
1012/* chk_snd function for applets */
1013static void cs_app_chk_snd_applet(struct conn_stream *cs)
1014{
1015 struct channel *oc = cs_oc(cs);
1016
1017 BUG_ON(!cs_appctx(cs));
1018
1019 DPRINTF(stderr, "%s: cs=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n",
1020 __FUNCTION__,
1021 cs, cs->state, cs_ic(cs)->flags, oc->flags);
1022
1023 if (unlikely(cs->state != CS_ST_EST || (oc->flags & CF_SHUTW)))
1024 return;
1025
1026 /* we only wake the applet up if it was waiting for some data */
1027
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001028 if (!(cs->endp->flags & CS_EP_WAIT_DATA))
Christopher Faulet9ffddd52022-04-01 14:04:29 +02001029 return;
1030
1031 if (!tick_isset(oc->wex))
1032 oc->wex = tick_add_ifset(now_ms, oc->wto);
1033
1034 if (!channel_is_empty(oc)) {
1035 /* (re)start sending */
1036 appctx_wakeup(__cs_appctx(cs));
1037 }
1038}
Christopher Faulet13045f02022-04-01 14:23:38 +02001039
1040
1041/* This function is designed to be called from within the stream handler to
1042 * update the input channel's expiration timer and the conn-stream's
1043 * Rx flags based on the channel's flags. It needs to be called only once
1044 * after the channel's flags have settled down, and before they are cleared,
1045 * though it doesn't harm to call it as often as desired (it just slightly
1046 * hurts performance). It must not be called from outside of the stream
1047 * handler, as what it does will be used to compute the stream task's
1048 * expiration.
1049 */
1050void cs_update_rx(struct conn_stream *cs)
1051{
1052 struct channel *ic = cs_ic(cs);
1053
1054 if (ic->flags & CF_SHUTR) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001055 cs_rx_shut_blk(cs);
Christopher Faulet13045f02022-04-01 14:23:38 +02001056 return;
1057 }
1058
1059 /* Read not closed, update FD status and timeout for reads */
1060 if (ic->flags & CF_DONT_READ)
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001061 cs_rx_chan_blk(cs);
Christopher Faulet13045f02022-04-01 14:23:38 +02001062 else
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001063 cs_rx_chan_rdy(cs);
Christopher Faulet13045f02022-04-01 14:23:38 +02001064
1065 if (!channel_is_empty(ic) || !channel_may_recv(ic)) {
1066 /* stop reading, imposed by channel's policy or contents */
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001067 cs_rx_room_blk(cs);
Christopher Faulet13045f02022-04-01 14:23:38 +02001068 }
1069 else {
1070 /* (re)start reading and update timeout. Note: we don't recompute the timeout
1071 * every time we get here, otherwise it would risk never to expire. We only
1072 * update it if is was not yet set. The stream socket handler will already
1073 * have updated it if there has been a completed I/O.
1074 */
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001075 cs_rx_room_rdy(cs);
Christopher Faulet13045f02022-04-01 14:23:38 +02001076 }
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001077 if (cs->endp->flags & CS_EP_RXBLK_ANY & ~CS_EP_RX_WAIT_EP)
Christopher Faulet13045f02022-04-01 14:23:38 +02001078 ic->rex = TICK_ETERNITY;
1079 else if (!(ic->flags & CF_READ_NOEXP) && !tick_isset(ic->rex))
1080 ic->rex = tick_add_ifset(now_ms, ic->rto);
1081
1082 cs_chk_rcv(cs);
1083}
1084
1085/* This function is designed to be called from within the stream handler to
1086 * update the output channel's expiration timer and the conn-stream's
1087 * Tx flags based on the channel's flags. It needs to be called only once
1088 * after the channel's flags have settled down, and before they are cleared,
1089 * though it doesn't harm to call it as often as desired (it just slightly
1090 * hurts performance). It must not be called from outside of the stream
1091 * handler, as what it does will be used to compute the stream task's
1092 * expiration.
1093 */
1094void cs_update_tx(struct conn_stream *cs)
1095{
1096 struct channel *oc = cs_oc(cs);
1097 struct channel *ic = cs_ic(cs);
1098
1099 if (oc->flags & CF_SHUTW)
1100 return;
1101
1102 /* Write not closed, update FD status and timeout for writes */
1103 if (channel_is_empty(oc)) {
1104 /* stop writing */
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001105 if (!(cs->endp->flags & CS_EP_WAIT_DATA)) {
Christopher Faulet13045f02022-04-01 14:23:38 +02001106 if ((oc->flags & CF_SHUTW_NOW) == 0)
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001107 cs->endp->flags |= CS_EP_WAIT_DATA;
Christopher Faulet13045f02022-04-01 14:23:38 +02001108 oc->wex = TICK_ETERNITY;
1109 }
1110 return;
1111 }
1112
1113 /* (re)start writing and update timeout. Note: we don't recompute the timeout
1114 * every time we get here, otherwise it would risk never to expire. We only
1115 * update it if is was not yet set. The stream socket handler will already
1116 * have updated it if there has been a completed I/O.
1117 */
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001118 cs->endp->flags &= ~CS_EP_WAIT_DATA;
Christopher Faulet13045f02022-04-01 14:23:38 +02001119 if (!tick_isset(oc->wex)) {
1120 oc->wex = tick_add_ifset(now_ms, oc->wto);
1121 if (tick_isset(ic->rex) && !(cs->flags & CS_FL_INDEP_STR)) {
1122 /* Note: depending on the protocol, we don't know if we're waiting
1123 * for incoming data or not. So in order to prevent the socket from
1124 * expiring read timeouts during writes, we refresh the read timeout,
1125 * except if it was already infinite or if we have explicitly setup
1126 * independent streams.
1127 */
1128 ic->rex = tick_add_ifset(now_ms, ic->rto);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001129 }
1130 }
1131}
1132
1133/* This function is the equivalent to cs_update() except that it's
1134 * designed to be called from outside the stream handlers, typically the lower
1135 * layers (applets, connections) after I/O completion. After updating the stream
1136 * interface and timeouts, it will try to forward what can be forwarded, then to
1137 * wake the associated task up if an important event requires special handling.
1138 * It may update CS_EP_WAIT_DATA and/or CS_EP_RXBLK_ROOM, that the callers are
1139 * encouraged to watch to take appropriate action.
1140 * It should not be called from within the stream itself, cs_update()
1141 * is designed for this.
1142 */
1143static void cs_notify(struct conn_stream *cs)
1144{
1145 struct channel *ic = cs_ic(cs);
1146 struct channel *oc = cs_oc(cs);
1147 struct conn_stream *cso = cs_opposite(cs);
1148 struct task *task = cs_strm_task(cs);
1149
1150 /* process consumer side */
1151 if (channel_is_empty(oc)) {
1152 struct connection *conn = cs_conn(cs);
1153
1154 if (((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW) &&
1155 (cs->state == CS_ST_EST) && (!conn || !(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS))))
1156 cs_shutw(cs);
1157 oc->wex = TICK_ETERNITY;
1158 }
1159
1160 /* indicate that we may be waiting for data from the output channel or
1161 * we're about to close and can't expect more data if SHUTW_NOW is there.
1162 */
1163 if (!(oc->flags & (CF_SHUTW|CF_SHUTW_NOW)))
1164 cs->endp->flags |= CS_EP_WAIT_DATA;
1165 else if ((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW)
1166 cs->endp->flags &= ~CS_EP_WAIT_DATA;
1167
1168 /* update OC timeouts and wake the other side up if it's waiting for room */
1169 if (oc->flags & CF_WRITE_ACTIVITY) {
1170 if ((oc->flags & (CF_SHUTW|CF_WRITE_PARTIAL)) == CF_WRITE_PARTIAL &&
1171 !channel_is_empty(oc))
1172 if (tick_isset(oc->wex))
1173 oc->wex = tick_add_ifset(now_ms, oc->wto);
1174
1175 if (!(cs->flags & CS_FL_INDEP_STR))
1176 if (tick_isset(ic->rex))
1177 ic->rex = tick_add_ifset(now_ms, ic->rto);
1178 }
1179
1180 if (oc->flags & CF_DONT_READ)
1181 cs_rx_chan_blk(cso);
1182 else
1183 cs_rx_chan_rdy(cso);
1184
1185 /* Notify the other side when we've injected data into the IC that
1186 * needs to be forwarded. We can do fast-forwarding as soon as there
1187 * are output data, but we avoid doing this if some of the data are
1188 * not yet scheduled for being forwarded, because it is very likely
1189 * that it will be done again immediately afterwards once the following
1190 * data are parsed (eg: HTTP chunking). We only CS_EP_RXBLK_ROOM once
1191 * we've emptied *some* of the output buffer, and not just when there
1192 * is available room, because applets are often forced to stop before
1193 * the buffer is full. We must not stop based on input data alone because
1194 * an HTTP parser might need more data to complete the parsing.
1195 */
1196 if (!channel_is_empty(ic) &&
1197 (cso->endp->flags & CS_EP_WAIT_DATA) &&
1198 (!(ic->flags & CF_EXPECT_MORE) || c_full(ic) || ci_data(ic) == 0 || ic->pipe)) {
1199 int new_len, last_len;
1200
1201 last_len = co_data(ic);
1202 if (ic->pipe)
1203 last_len += ic->pipe->data;
1204
1205 cs_chk_snd(cso);
1206
1207 new_len = co_data(ic);
1208 if (ic->pipe)
1209 new_len += ic->pipe->data;
1210
1211 /* check if the consumer has freed some space either in the
1212 * buffer or in the pipe.
1213 */
1214 if (new_len < last_len)
1215 cs_rx_room_rdy(cs);
1216 }
1217
1218 if (!(ic->flags & CF_DONT_READ))
1219 cs_rx_chan_rdy(cs);
1220
1221 cs_chk_rcv(cs);
1222 cs_chk_rcv(cso);
1223
1224 if (cs_rx_blocked(cs)) {
1225 ic->rex = TICK_ETERNITY;
1226 }
1227 else if ((ic->flags & (CF_SHUTR|CF_READ_PARTIAL)) == CF_READ_PARTIAL) {
1228 /* we must re-enable reading if cs_chk_snd() has freed some space */
1229 if (!(ic->flags & CF_READ_NOEXP) && tick_isset(ic->rex))
1230 ic->rex = tick_add_ifset(now_ms, ic->rto);
1231 }
1232
1233 /* wake the task up only when needed */
1234 if (/* changes on the production side */
1235 (ic->flags & (CF_READ_NULL|CF_READ_ERROR)) ||
1236 !cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST) ||
1237 (cs->endp->flags & CS_EP_ERROR) ||
1238 ((ic->flags & CF_READ_PARTIAL) &&
1239 ((ic->flags & CF_EOI) || !ic->to_forward || cso->state != CS_ST_EST)) ||
1240
1241 /* changes on the consumption side */
1242 (oc->flags & (CF_WRITE_NULL|CF_WRITE_ERROR)) ||
1243 ((oc->flags & CF_WRITE_ACTIVITY) &&
1244 ((oc->flags & CF_SHUTW) ||
1245 (((oc->flags & CF_WAKE_WRITE) ||
1246 !(oc->flags & (CF_AUTO_CLOSE|CF_SHUTW_NOW|CF_SHUTW))) &&
1247 (cso->state != CS_ST_EST ||
1248 (channel_is_empty(oc) && !oc->to_forward)))))) {
1249 task_wakeup(task, TASK_WOKEN_IO);
1250 }
1251 else {
1252 /* Update expiration date for the task and requeue it */
1253 task->expire = tick_first((tick_is_expired(task->expire, now_ms) ? 0 : task->expire),
1254 tick_first(tick_first(ic->rex, ic->wex),
1255 tick_first(oc->rex, oc->wex)));
1256
1257 task->expire = tick_first(task->expire, ic->analyse_exp);
1258 task->expire = tick_first(task->expire, oc->analyse_exp);
1259 task->expire = tick_first(task->expire, __cs_strm(cs)->conn_exp);
1260
1261 task_queue(task);
1262 }
1263 if (ic->flags & CF_READ_ACTIVITY)
1264 ic->flags &= ~CF_READ_DONTWAIT;
1265}
1266
1267/*
1268 * This function propagates a null read received on a socket-based connection.
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001269 * It updates the conn-stream. If the conn-stream has CS_FL_NOHALF,
Christopher Faulet5e29b762022-04-04 08:58:34 +02001270 * the close is also forwarded to the write side as an abort.
1271 */
1272static void cs_conn_read0(struct conn_stream *cs)
1273{
1274 struct channel *ic = cs_ic(cs);
1275 struct channel *oc = cs_oc(cs);
1276
1277 BUG_ON(!cs_conn(cs));
1278
1279 cs_rx_shut_blk(cs);
1280 if (ic->flags & CF_SHUTR)
1281 return;
1282 ic->flags |= CF_SHUTR;
1283 ic->rex = TICK_ETERNITY;
1284
1285 if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
1286 return;
1287
1288 if (oc->flags & CF_SHUTW)
1289 goto do_close;
1290
1291 if (cs->flags & CS_FL_NOHALF) {
1292 /* we want to immediately forward this close to the write side */
1293 /* force flag on ssl to keep stream in cache */
1294 cs_conn_shutw(cs, CO_SHW_SILENT);
1295 goto do_close;
1296 }
1297
1298 /* otherwise that's just a normal read shutdown */
1299 return;
1300
1301 do_close:
1302 /* OK we completely close the socket here just as if we went through cs_shut[rw]() */
Christopher Fauletff022a22022-04-21 08:38:54 +02001303 cs_conn_shut(cs);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001304
1305 oc->flags &= ~CF_SHUTW_NOW;
1306 oc->flags |= CF_SHUTW;
1307 oc->wex = TICK_ETERNITY;
1308
1309 cs_done_get(cs);
1310
1311 cs->state = CS_ST_DIS;
1312 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
1313 return;
1314}
1315
1316/*
1317 * This is the callback which is called by the connection layer to receive data
1318 * into the buffer from the connection. It iterates over the mux layer's
1319 * rcv_buf function.
1320 */
1321static int cs_conn_recv(struct conn_stream *cs)
1322{
1323 struct connection *conn = __cs_conn(cs);
1324 struct channel *ic = cs_ic(cs);
1325 int ret, max, cur_read = 0;
1326 int read_poll = MAX_READ_POLL_LOOPS;
1327 int flags = 0;
1328
1329 /* If not established yet, do nothing. */
1330 if (cs->state != CS_ST_EST)
1331 return 0;
1332
1333 /* If another call to cs_conn_recv() failed, and we subscribed to
1334 * recv events already, give up now.
1335 */
1336 if (cs->wait_event.events & SUB_RETRY_RECV)
1337 return 0;
1338
1339 /* maybe we were called immediately after an asynchronous shutr */
1340 if (ic->flags & CF_SHUTR)
1341 return 1;
1342
1343 /* we must wait because the mux is not installed yet */
1344 if (!conn->mux)
1345 return 0;
1346
1347 /* stop here if we reached the end of data */
1348 if (cs->endp->flags & CS_EP_EOS)
1349 goto end_recv;
1350
1351 /* stop immediately on errors. Note that we DON'T want to stop on
1352 * POLL_ERR, as the poller might report a write error while there
1353 * are still data available in the recv buffer. This typically
1354 * happens when we send too large a request to a backend server
1355 * which rejects it before reading it all.
1356 */
1357 if (!(cs->endp->flags & CS_EP_RCV_MORE)) {
1358 if (!conn_xprt_ready(conn))
1359 return 0;
1360 if (cs->endp->flags & CS_EP_ERROR)
1361 goto end_recv;
1362 }
1363
1364 /* prepare to detect if the mux needs more room */
1365 cs->endp->flags &= ~CS_EP_WANT_ROOM;
1366
1367 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) && !co_data(ic) &&
1368 global.tune.idle_timer &&
1369 (unsigned short)(now_ms - ic->last_read) >= global.tune.idle_timer) {
1370 /* The buffer was empty and nothing was transferred for more
1371 * than one second. This was caused by a pause and not by
1372 * congestion. Reset any streaming mode to reduce latency.
1373 */
1374 ic->xfer_small = 0;
1375 ic->xfer_large = 0;
1376 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
1377 }
1378
1379 /* First, let's see if we may splice data across the channel without
1380 * using a buffer.
1381 */
1382 if (cs->endp->flags & CS_EP_MAY_SPLICE &&
1383 (ic->pipe || ic->to_forward >= MIN_SPLICE_FORWARD) &&
1384 ic->flags & CF_KERN_SPLICING) {
1385 if (c_data(ic)) {
1386 /* We're embarrassed, there are already data pending in
1387 * the buffer and we don't want to have them at two
1388 * locations at a time. Let's indicate we need some
1389 * place and ask the consumer to hurry.
1390 */
1391 flags |= CO_RFL_BUF_FLUSH;
1392 goto abort_splice;
1393 }
1394
1395 if (unlikely(ic->pipe == NULL)) {
1396 if (pipes_used >= global.maxpipes || !(ic->pipe = get_pipe())) {
1397 ic->flags &= ~CF_KERN_SPLICING;
1398 goto abort_splice;
1399 }
1400 }
1401
1402 ret = conn->mux->rcv_pipe(cs, ic->pipe, ic->to_forward);
1403 if (ret < 0) {
1404 /* splice not supported on this end, let's disable it */
1405 ic->flags &= ~CF_KERN_SPLICING;
1406 goto abort_splice;
1407 }
1408
1409 if (ret > 0) {
1410 if (ic->to_forward != CHN_INFINITE_FORWARD)
1411 ic->to_forward -= ret;
1412 ic->total += ret;
1413 cur_read += ret;
1414 ic->flags |= CF_READ_PARTIAL;
1415 }
1416
1417 if (cs->endp->flags & (CS_EP_EOS|CS_EP_ERROR))
1418 goto end_recv;
1419
1420 if (conn->flags & CO_FL_WAIT_ROOM) {
1421 /* the pipe is full or we have read enough data that it
1422 * could soon be full. Let's stop before needing to poll.
1423 */
1424 cs_rx_room_blk(cs);
1425 goto done_recv;
1426 }
1427
1428 /* splice not possible (anymore), let's go on on standard copy */
1429 }
1430
1431 abort_splice:
1432 if (ic->pipe && unlikely(!ic->pipe->data)) {
1433 put_pipe(ic->pipe);
1434 ic->pipe = NULL;
1435 }
1436
1437 if (ic->pipe && ic->to_forward && !(flags & CO_RFL_BUF_FLUSH) && cs->endp->flags & CS_EP_MAY_SPLICE) {
1438 /* don't break splicing by reading, but still call rcv_buf()
1439 * to pass the flag.
1440 */
1441 goto done_recv;
1442 }
1443
1444 /* now we'll need a input buffer for the stream */
1445 if (!cs_alloc_ibuf(cs, &(__cs_strm(cs)->buffer_wait)))
1446 goto end_recv;
1447
1448 /* For an HTX stream, if the buffer is stuck (no output data with some
1449 * input data) and if the HTX message is fragmented or if its free space
1450 * wraps, we force an HTX deframentation. It is a way to have a
1451 * contiguous free space nad to let the mux to copy as much data as
1452 * possible.
1453 *
1454 * NOTE: A possible optim may be to let the mux decides if defrag is
1455 * required or not, depending on amount of data to be xferred.
1456 */
1457 if (IS_HTX_STRM(__cs_strm(cs)) && !co_data(ic)) {
1458 struct htx *htx = htxbuf(&ic->buf);
1459
1460 if (htx_is_not_empty(htx) && ((htx->flags & HTX_FL_FRAGMENTED) || htx_space_wraps(htx)))
1461 htx_defrag(htx, NULL, 0);
1462 }
1463
1464 /* Instruct the mux it must subscribed for read events */
1465 flags |= ((!conn_is_back(conn) && (__cs_strm(cs)->be->options & PR_O_ABRT_CLOSE)) ? CO_RFL_KEEP_RECV : 0);
1466
1467 /* Important note : if we're called with POLL_IN|POLL_HUP, it means the read polling
1468 * was enabled, which implies that the recv buffer was not full. So we have a guarantee
1469 * that if such an event is not handled above in splice, it will be handled here by
1470 * recv().
1471 */
1472 while ((cs->endp->flags & CS_EP_RCV_MORE) ||
1473 (!(conn->flags & CO_FL_HANDSHAKE) &&
1474 (!(cs->endp->flags & (CS_EP_ERROR|CS_EP_EOS))) && !(ic->flags & CF_SHUTR))) {
1475 int cur_flags = flags;
1476
1477 /* Compute transient CO_RFL_* flags */
1478 if (co_data(ic)) {
1479 cur_flags |= (CO_RFL_BUF_WET | CO_RFL_BUF_NOT_STUCK);
1480 }
1481
1482 /* <max> may be null. This is the mux responsibility to set
1483 * CS_EP_RCV_MORE on the CS if more space is needed.
1484 */
1485 max = channel_recv_max(ic);
1486 ret = conn->mux->rcv_buf(cs, &ic->buf, max, cur_flags);
1487
1488 if (cs->endp->flags & CS_EP_WANT_ROOM) {
1489 /* CS_EP_WANT_ROOM must not be reported if the channel's
1490 * buffer is empty.
1491 */
1492 BUG_ON(c_empty(ic));
1493
1494 cs_rx_room_blk(cs);
1495 /* Add READ_PARTIAL because some data are pending but
1496 * cannot be xferred to the channel
1497 */
1498 ic->flags |= CF_READ_PARTIAL;
1499 }
1500
1501 if (ret <= 0) {
1502 /* if we refrained from reading because we asked for a
1503 * flush to satisfy rcv_pipe(), we must not subscribe
1504 * and instead report that there's not enough room
1505 * here to proceed.
1506 */
1507 if (flags & CO_RFL_BUF_FLUSH)
1508 cs_rx_room_blk(cs);
1509 break;
1510 }
1511
1512 cur_read += ret;
1513
1514 /* if we're allowed to directly forward data, we must update ->o */
1515 if (ic->to_forward && !(ic->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
1516 unsigned long fwd = ret;
1517 if (ic->to_forward != CHN_INFINITE_FORWARD) {
1518 if (fwd > ic->to_forward)
1519 fwd = ic->to_forward;
1520 ic->to_forward -= fwd;
1521 }
1522 c_adv(ic, fwd);
1523 }
1524
1525 ic->flags |= CF_READ_PARTIAL;
1526 ic->total += ret;
1527
1528 /* End-of-input reached, we can leave. In this case, it is
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001529 * important to break the loop to not block the CS because of
Christopher Faulet5e29b762022-04-04 08:58:34 +02001530 * the channel's policies.This way, we are still able to receive
1531 * shutdowns.
1532 */
1533 if (cs->endp->flags & CS_EP_EOI)
1534 break;
1535
1536 if ((ic->flags & CF_READ_DONTWAIT) || --read_poll <= 0) {
1537 /* we're stopped by the channel's policy */
1538 cs_rx_chan_blk(cs);
1539 break;
1540 }
1541
1542 /* if too many bytes were missing from last read, it means that
1543 * it's pointless trying to read again because the system does
1544 * not have them in buffers.
1545 */
1546 if (ret < max) {
1547 /* if a streamer has read few data, it may be because we
1548 * have exhausted system buffers. It's not worth trying
1549 * again.
1550 */
1551 if (ic->flags & CF_STREAMER) {
1552 /* we're stopped by the channel's policy */
1553 cs_rx_chan_blk(cs);
1554 break;
1555 }
1556
1557 /* if we read a large block smaller than what we requested,
1558 * it's almost certain we'll never get anything more.
1559 */
1560 if (ret >= global.tune.recv_enough) {
1561 /* we're stopped by the channel's policy */
1562 cs_rx_chan_blk(cs);
1563 break;
1564 }
1565 }
1566
1567 /* if we are waiting for more space, don't try to read more data
1568 * right now.
1569 */
1570 if (cs_rx_blocked(cs))
1571 break;
1572 } /* while !flags */
1573
1574 done_recv:
1575 if (cur_read) {
1576 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) &&
1577 (cur_read <= ic->buf.size / 2)) {
1578 ic->xfer_large = 0;
1579 ic->xfer_small++;
1580 if (ic->xfer_small >= 3) {
1581 /* we have read less than half of the buffer in
1582 * one pass, and this happened at least 3 times.
1583 * This is definitely not a streamer.
1584 */
1585 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
1586 }
1587 else if (ic->xfer_small >= 2) {
1588 /* if the buffer has been at least half full twice,
1589 * we receive faster than we send, so at least it
1590 * is not a "fast streamer".
1591 */
1592 ic->flags &= ~CF_STREAMER_FAST;
1593 }
1594 }
1595 else if (!(ic->flags & CF_STREAMER_FAST) &&
1596 (cur_read >= ic->buf.size - global.tune.maxrewrite)) {
1597 /* we read a full buffer at once */
1598 ic->xfer_small = 0;
1599 ic->xfer_large++;
1600 if (ic->xfer_large >= 3) {
1601 /* we call this buffer a fast streamer if it manages
1602 * to be filled in one call 3 consecutive times.
1603 */
1604 ic->flags |= (CF_STREAMER | CF_STREAMER_FAST);
1605 }
1606 }
1607 else {
1608 ic->xfer_small = 0;
1609 ic->xfer_large = 0;
1610 }
1611 ic->last_read = now_ms;
1612 }
1613
1614 end_recv:
1615 ret = (cur_read != 0);
1616
1617 /* Report EOI on the channel if it was reached from the mux point of
1618 * view. */
1619 if ((cs->endp->flags & CS_EP_EOI) && !(ic->flags & CF_EOI)) {
1620 ic->flags |= (CF_EOI|CF_READ_PARTIAL);
1621 ret = 1;
1622 }
1623
1624 if (cs->endp->flags & CS_EP_ERROR)
1625 ret = 1;
1626 else if (cs->endp->flags & CS_EP_EOS) {
1627 /* we received a shutdown */
1628 ic->flags |= CF_READ_NULL;
1629 if (ic->flags & CF_AUTO_CLOSE)
1630 channel_shutw_now(ic);
1631 cs_conn_read0(cs);
1632 ret = 1;
1633 }
1634 else if (!cs_rx_blocked(cs)) {
1635 /* Subscribe to receive events if we're blocking on I/O */
1636 conn->mux->subscribe(cs, SUB_RETRY_RECV, &cs->wait_event);
1637 cs_rx_endp_done(cs);
1638 } else {
1639 cs_rx_endp_more(cs);
1640 ret = 1;
1641 }
1642 return ret;
1643}
1644
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001645/* This tries to perform a synchronous receive on the conn-stream to
Christopher Faulet5e29b762022-04-04 08:58:34 +02001646 * try to collect last arrived data. In practice it's only implemented on
1647 * conn_streams. Returns 0 if nothing was done, non-zero if new data or a
1648 * shutdown were collected. This may result on some delayed receive calls
1649 * to be programmed and performed later, though it doesn't provide any
1650 * such guarantee.
1651 */
1652int cs_conn_sync_recv(struct conn_stream *cs)
1653{
1654 if (!cs_state_in(cs->state, CS_SB_RDY|CS_SB_EST))
1655 return 0;
1656
1657 if (!cs_conn_mux(cs))
1658 return 0; // only conn_streams are supported
1659
1660 if (cs->wait_event.events & SUB_RETRY_RECV)
1661 return 0; // already subscribed
1662
1663 if (!cs_rx_endp_ready(cs) || cs_rx_blocked(cs))
1664 return 0; // already failed
1665
1666 return cs_conn_recv(cs);
1667}
1668
1669/*
1670 * This function is called to send buffer data to a stream socket.
1671 * It calls the mux layer's snd_buf function. It relies on the
1672 * caller to commit polling changes. The caller should check conn->flags
1673 * for errors.
1674 */
1675static int cs_conn_send(struct conn_stream *cs)
1676{
1677 struct connection *conn = __cs_conn(cs);
1678 struct stream *s = __cs_strm(cs);
1679 struct channel *oc = cs_oc(cs);
1680 int ret;
1681 int did_send = 0;
1682
1683 if (cs->endp->flags & (CS_EP_ERROR|CS_EP_ERR_PENDING) || cs_is_conn_error(cs)) {
1684 /* We're probably there because the tasklet was woken up,
1685 * but process_stream() ran before, detected there were an
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001686 * error and put the CS back to CS_ST_TAR. There's still
Christopher Faulet5e29b762022-04-04 08:58:34 +02001687 * CO_FL_ERROR on the connection but we don't want to add
1688 * CS_EP_ERROR back, so give up
1689 */
1690 if (cs->state < CS_ST_CON)
1691 return 0;
1692 cs->endp->flags |= CS_EP_ERROR;
1693 return 1;
1694 }
1695
1696 /* We're already waiting to be able to send, give up */
1697 if (cs->wait_event.events & SUB_RETRY_SEND)
1698 return 0;
1699
1700 /* we might have been called just after an asynchronous shutw */
1701 if (oc->flags & CF_SHUTW)
1702 return 1;
1703
1704 /* we must wait because the mux is not installed yet */
1705 if (!conn->mux)
1706 return 0;
1707
1708 if (oc->pipe && conn->xprt->snd_pipe && conn->mux->snd_pipe) {
1709 ret = conn->mux->snd_pipe(cs, oc->pipe);
1710 if (ret > 0)
1711 did_send = 1;
1712
1713 if (!oc->pipe->data) {
1714 put_pipe(oc->pipe);
1715 oc->pipe = NULL;
1716 }
1717
1718 if (oc->pipe)
1719 goto end;
1720 }
1721
1722 /* At this point, the pipe is empty, but we may still have data pending
1723 * in the normal buffer.
1724 */
1725 if (co_data(oc)) {
1726 /* when we're here, we already know that there is no spliced
1727 * data left, and that there are sendable buffered data.
1728 */
1729
1730 /* check if we want to inform the kernel that we're interested in
1731 * sending more data after this call. We want this if :
1732 * - we're about to close after this last send and want to merge
1733 * the ongoing FIN with the last segment.
1734 * - we know we can't send everything at once and must get back
1735 * here because of unaligned data
1736 * - there is still a finite amount of data to forward
1737 * The test is arranged so that the most common case does only 2
1738 * tests.
1739 */
1740 unsigned int send_flag = 0;
1741
1742 if ((!(oc->flags & (CF_NEVER_WAIT|CF_SEND_DONTWAIT)) &&
1743 ((oc->to_forward && oc->to_forward != CHN_INFINITE_FORWARD) ||
1744 (oc->flags & CF_EXPECT_MORE) ||
1745 (IS_HTX_STRM(s) &&
1746 (!(oc->flags & (CF_EOI|CF_SHUTR)) && htx_expect_more(htxbuf(&oc->buf)))))) ||
1747 ((oc->flags & CF_ISRESP) &&
1748 ((oc->flags & (CF_AUTO_CLOSE|CF_SHUTW_NOW)) == (CF_AUTO_CLOSE|CF_SHUTW_NOW))))
1749 send_flag |= CO_SFL_MSG_MORE;
1750
1751 if (oc->flags & CF_STREAMER)
1752 send_flag |= CO_SFL_STREAMER;
1753
1754 if (s->txn && s->txn->flags & TX_L7_RETRY && !b_data(&s->txn->l7_buffer)) {
1755 /* If we want to be able to do L7 retries, copy
1756 * the data we're about to send, so that we are able
1757 * to resend them if needed
1758 */
1759 /* Try to allocate a buffer if we had none.
1760 * If it fails, the next test will just
1761 * disable the l7 retries by setting
1762 * l7_conn_retries to 0.
1763 */
1764 if (s->txn->req.msg_state != HTTP_MSG_DONE)
1765 s->txn->flags &= ~TX_L7_RETRY;
1766 else {
1767 if (b_alloc(&s->txn->l7_buffer) == NULL)
1768 s->txn->flags &= ~TX_L7_RETRY;
1769 else {
1770 memcpy(b_orig(&s->txn->l7_buffer),
1771 b_orig(&oc->buf),
1772 b_size(&oc->buf));
1773 s->txn->l7_buffer.head = co_data(oc);
1774 b_add(&s->txn->l7_buffer, co_data(oc));
1775 }
1776
1777 }
1778 }
1779
1780 ret = conn->mux->snd_buf(cs, &oc->buf, co_data(oc), send_flag);
1781 if (ret > 0) {
1782 did_send = 1;
1783 c_rew(oc, ret);
1784 c_realign_if_empty(oc);
1785
1786 if (!co_data(oc)) {
1787 /* Always clear both flags once everything has been sent, they're one-shot */
1788 oc->flags &= ~(CF_EXPECT_MORE | CF_SEND_DONTWAIT);
1789 }
1790 /* if some data remain in the buffer, it's only because the
1791 * system buffers are full, we will try next time.
1792 */
Christopher Faulet13045f02022-04-01 14:23:38 +02001793 }
1794 }
Christopher Faulet5e29b762022-04-04 08:58:34 +02001795
1796 end:
1797 if (did_send) {
1798 oc->flags |= CF_WRITE_PARTIAL | CF_WROTE_DATA;
1799 if (cs->state == CS_ST_CON)
1800 cs->state = CS_ST_RDY;
1801
1802 cs_rx_room_rdy(cs_opposite(cs));
1803 }
1804
1805 if (cs->endp->flags & (CS_EP_ERROR|CS_EP_ERR_PENDING)) {
1806 cs->endp->flags |= CS_EP_ERROR;
1807 return 1;
1808 }
1809
1810 /* We couldn't send all of our data, let the mux know we'd like to send more */
1811 if (!channel_is_empty(oc))
1812 conn->mux->subscribe(cs, SUB_RETRY_SEND, &cs->wait_event);
1813 return did_send;
1814}
1815
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001816/* perform a synchronous send() for the conn-stream. The CF_WRITE_NULL and
Christopher Faulet5e29b762022-04-04 08:58:34 +02001817 * CF_WRITE_PARTIAL flags are cleared prior to the attempt, and will possibly
1818 * be updated in case of success.
1819 */
1820void cs_conn_sync_send(struct conn_stream *cs)
1821{
1822 struct channel *oc = cs_oc(cs);
1823
1824 oc->flags &= ~(CF_WRITE_NULL|CF_WRITE_PARTIAL);
1825
1826 if (oc->flags & CF_SHUTW)
1827 return;
1828
1829 if (channel_is_empty(oc))
1830 return;
1831
1832 if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
1833 return;
1834
1835 if (!cs_conn_mux(cs))
1836 return;
1837
1838 cs_conn_send(cs);
1839}
1840
1841/* Called by I/O handlers after completion.. It propagates
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001842 * connection flags to the conn-stream, updates the stream (which may or
Christopher Faulet5e29b762022-04-04 08:58:34 +02001843 * may not take this opportunity to try to forward data), then update the
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001844 * connection's polling based on the channels and conn-stream's final
Christopher Faulet5e29b762022-04-04 08:58:34 +02001845 * states. The function always returns 0.
1846 */
1847static int cs_conn_process(struct conn_stream *cs)
1848{
1849 struct connection *conn = __cs_conn(cs);
1850 struct channel *ic = cs_ic(cs);
1851 struct channel *oc = cs_oc(cs);
1852
1853 BUG_ON(!conn);
1854
1855 /* If we have data to send, try it now */
1856 if (!channel_is_empty(oc) && !(cs->wait_event.events & SUB_RETRY_SEND))
1857 cs_conn_send(cs);
1858
1859 /* First step, report to the conn-stream what was detected at the
1860 * connection layer : errors and connection establishment.
1861 * Only add CS_EP_ERROR if we're connected, or we're attempting to
1862 * connect, we may get there because we got woken up, but only run
1863 * after process_stream() noticed there were an error, and decided
1864 * to retry to connect, the connection may still have CO_FL_ERROR,
1865 * and we don't want to add CS_EP_ERROR back
1866 *
1867 * Note: This test is only required because cs_conn_process is also the SI
1868 * wake callback. Otherwise cs_conn_recv()/cs_conn_send() already take
1869 * care of it.
1870 */
1871
1872 if (cs->state >= CS_ST_CON) {
1873 if (cs_is_conn_error(cs))
1874 cs->endp->flags |= CS_EP_ERROR;
1875 }
1876
1877 /* If we had early data, and the handshake ended, then
1878 * we can remove the flag, and attempt to wake the task up,
1879 * in the event there's an analyser waiting for the end of
1880 * the handshake.
1881 */
1882 if (!(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS)) &&
1883 (cs->endp->flags & CS_EP_WAIT_FOR_HS)) {
1884 cs->endp->flags &= ~CS_EP_WAIT_FOR_HS;
1885 task_wakeup(cs_strm_task(cs), TASK_WOKEN_MSG);
1886 }
1887
1888 if (!cs_state_in(cs->state, CS_SB_EST|CS_SB_DIS|CS_SB_CLO) &&
1889 (conn->flags & CO_FL_WAIT_XPRT) == 0) {
1890 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
1891 oc->flags |= CF_WRITE_NULL;
1892 if (cs->state == CS_ST_CON)
1893 cs->state = CS_ST_RDY;
1894 }
1895
1896 /* Report EOS on the channel if it was reached from the mux point of
1897 * view.
1898 *
1899 * Note: This test is only required because cs_conn_process is also the SI
1900 * wake callback. Otherwise cs_conn_recv()/cs_conn_send() already take
1901 * care of it.
1902 */
1903 if (cs->endp->flags & CS_EP_EOS && !(ic->flags & CF_SHUTR)) {
1904 /* we received a shutdown */
1905 ic->flags |= CF_READ_NULL;
1906 if (ic->flags & CF_AUTO_CLOSE)
1907 channel_shutw_now(ic);
1908 cs_conn_read0(cs);
1909 }
1910
1911 /* Report EOI on the channel if it was reached from the mux point of
1912 * view.
1913 *
1914 * Note: This test is only required because cs_conn_process is also the SI
1915 * wake callback. Otherwise cs_conn_recv()/cs_conn_send() already take
1916 * care of it.
1917 */
1918 if ((cs->endp->flags & CS_EP_EOI) && !(ic->flags & CF_EOI))
1919 ic->flags |= (CF_EOI|CF_READ_PARTIAL);
1920
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001921 /* Second step : update the conn-stream and channels, try to forward any
Christopher Faulet5e29b762022-04-04 08:58:34 +02001922 * pending data, then possibly wake the stream up based on the new
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001923 * conn-stream status.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001924 */
1925 cs_notify(cs);
1926 stream_release_buffers(__cs_strm(cs));
1927 return 0;
1928}
1929
1930/* This is the ->process() function for any conn-stream's wait_event task.
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001931 * It's assigned during the conn-stream's initialization, for any type of
1932 * conn-stream. Thus it is always safe to perform a tasklet_wakeup() on a
1933 * conn-stream, as the presence of the CS is checked there.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001934 */
1935struct task *cs_conn_io_cb(struct task *t, void *ctx, unsigned int state)
1936{
1937 struct conn_stream *cs = ctx;
1938 int ret = 0;
1939
1940 if (!cs_conn(cs))
1941 return t;
1942
1943 if (!(cs->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(cs_oc(cs)))
1944 ret = cs_conn_send(cs);
1945 if (!(cs->wait_event.events & SUB_RETRY_RECV))
1946 ret |= cs_conn_recv(cs);
1947 if (ret != 0)
1948 cs_conn_process(cs);
1949
1950 stream_release_buffers(__cs_strm(cs));
1951 return t;
1952}
1953
1954/* Callback to be used by applet handlers upon completion. It updates the stream
1955 * (which may or may not take this opportunity to try to forward data), then
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001956 * may re-enable the applet's based on the channels and conn-stream's final
Christopher Faulet5e29b762022-04-04 08:58:34 +02001957 * states.
1958 */
1959static int cs_applet_process(struct conn_stream *cs)
1960{
1961 struct channel *ic = cs_ic(cs);
1962
1963 BUG_ON(!cs_appctx(cs));
1964
1965 /* If the applet wants to write and the channel is closed, it's a
1966 * broken pipe and it must be reported.
1967 */
1968 if (!(cs->endp->flags & CS_EP_RX_WAIT_EP) && (ic->flags & CF_SHUTR))
1969 cs->endp->flags |= CS_EP_ERROR;
1970
1971 /* automatically mark the applet having data available if it reported
1972 * begin blocked by the channel.
1973 */
1974 if (cs_rx_blocked(cs))
1975 cs_rx_endp_more(cs);
1976
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001977 /* update the conn-stream, channels, and possibly wake the stream up */
Christopher Faulet5e29b762022-04-04 08:58:34 +02001978 cs_notify(cs);
1979 stream_release_buffers(__cs_strm(cs));
1980
1981 /* cs_notify may have passed through chk_snd and released some
1982 * RXBLK flags. Process_stream will consider those flags to wake up the
1983 * appctx but in the case the task is not in runqueue we may have to
1984 * wakeup the appctx immediately.
1985 */
1986 if ((cs_rx_endp_ready(cs) && !cs_rx_blocked(cs)) ||
1987 (cs_tx_endp_ready(cs) && !cs_tx_blocked(cs)))
1988 appctx_wakeup(__cs_appctx(cs));
1989 return 0;
Christopher Faulet13045f02022-04-01 14:23:38 +02001990}