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