blob: 7fc3a493552fb3d7c890f9bfd5b068fef572fbf4 [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 */
Willy Tarreau6796a062022-05-11 16:11:24 +0200159struct conn_stream *cs_new_from_endp(struct cs_endpoint *endp, struct session *sess, struct buffer *input)
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100160{
161 struct conn_stream *cs;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100162
163 cs = cs_new(endp);
164 if (unlikely(!cs))
165 return NULL;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100166 if (unlikely(!stream_new(sess, cs, input))) {
167 pool_free(pool_head_connstream, cs);
168 cs = NULL;
169 }
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 from an stream. There is no endpoint here, thus it
175 * will be created by cs_new(). So the CS_EP_DETACHED flag is set. It returns
176 * NULL on error. On success, the new conn-stream is returned.
177 */
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100178struct conn_stream *cs_new_from_strm(struct stream *strm, unsigned int flags)
179{
180 struct conn_stream *cs;
181
182 cs = cs_new(NULL);
183 if (unlikely(!cs))
184 return NULL;
185 cs->flags |= flags;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100186 cs->endp->flags |= CS_EP_DETACHED;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100187 cs->app = &strm->obj_type;
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200188 cs->ops = &cs_app_embedded_ops;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100189 cs->data_cb = NULL;
190 return cs;
191}
192
Christopher Faulet9ed77422022-04-12 08:51:15 +0200193/* Creates a new conn-stream from an health-check. There is no endpoint here,
194 * thus it will be created by cs_new(). So the CS_EP_DETACHED flag is set. It
195 * returns NULL on error. On success, the new conn-stream is returned.
196 */
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100197struct conn_stream *cs_new_from_check(struct check *check, unsigned int flags)
198{
199 struct conn_stream *cs;
200
201 cs = cs_new(NULL);
202 if (unlikely(!cs))
203 return NULL;
204 cs->flags |= flags;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100205 cs->endp->flags |= CS_EP_DETACHED;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100206 cs->app = &check->obj_type;
207 cs->data_cb = &check_conn_cb;
208 return cs;
209}
210
Christopher Faulet9ed77422022-04-12 08:51:15 +0200211/* Releases a conn_stream previously allocated by cs_new(), as well as its
212 * endpoint, if it exists. This function is called internally or on error path.
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100213 */
214void cs_free(struct conn_stream *cs)
215{
Christopher Faulet8da67aa2022-03-29 17:53:09 +0200216 sockaddr_free(&cs->src);
217 sockaddr_free(&cs->dst);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100218 if (cs->endp) {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100219 BUG_ON(!(cs->endp->flags & CS_EP_DETACHED));
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100220 cs_endpoint_free(cs->endp);
221 }
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200222 if (cs->wait_event.tasklet)
223 tasklet_free(cs->wait_event.tasklet);
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100224 pool_free(pool_head_connstream, cs);
225}
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100226
Christopher Fauleteb50c012022-04-21 14:22:53 +0200227/* Conditionally removes a conn-stream if it is detached and if there is no app
228 * layer defined. Except on error path, this one must be used. if release, the
229 * pointer on the CS is set to NULL.
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200230 */
Christopher Fauleteb50c012022-04-21 14:22:53 +0200231static void cs_free_cond(struct conn_stream **csp)
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200232{
Christopher Fauleteb50c012022-04-21 14:22:53 +0200233 struct conn_stream *cs = *csp;
234
235 if (!cs->app && (!cs->endp || (cs->endp->flags & CS_EP_DETACHED))) {
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200236 cs_free(cs);
Christopher Fauleteb50c012022-04-21 14:22:53 +0200237 *csp = NULL;
238 }
Christopher Fauletaa69d8f2022-04-12 18:09:48 +0200239}
240
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100241
Christopher Faulet9ed77422022-04-12 08:51:15 +0200242/* Attaches a conn_stream to a mux endpoint and sets the endpoint ctx. Returns
243 * -1 on error and 0 on sucess. CS_EP_DETACHED flag is removed. This function is
244 * called from a mux when it is attached to a stream or a health-check.
245 */
Christopher Faulet070b91b2022-03-31 19:27:18 +0200246int cs_attach_mux(struct conn_stream *cs, void *target, void *ctx)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100247{
Christopher Faulet93882042022-01-19 14:56:50 +0100248 struct connection *conn = ctx;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100249
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100250 cs->endp->target = target;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100251 cs->endp->ctx = ctx;
252 cs->endp->flags |= CS_EP_T_MUX;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100253 cs->endp->flags &= ~CS_EP_DETACHED;
Christopher Faulet93882042022-01-19 14:56:50 +0100254 if (!conn->ctx)
255 conn->ctx = cs;
256 if (cs_strm(cs)) {
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200257 if (!cs->wait_event.tasklet) {
258 cs->wait_event.tasklet = tasklet_new();
259 if (!cs->wait_event.tasklet)
260 return -1;
Christopher Faulet4a7764a2022-04-01 16:58:52 +0200261 cs->wait_event.tasklet->process = cs_conn_io_cb;
262 cs->wait_event.tasklet->context = cs;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200263 cs->wait_event.events = 0;
264 }
265
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200266 cs->ops = &cs_app_conn_ops;
Christopher Faulet000ba3e2022-04-01 17:06:32 +0200267 cs->data_cb = &cs_data_conn_cb;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100268 }
Christopher Faulet93882042022-01-19 14:56:50 +0100269 else if (cs_check(cs))
270 cs->data_cb = &check_conn_cb;
Christopher Faulet070b91b2022-03-31 19:27:18 +0200271 return 0;
Christopher Faulet93882042022-01-19 14:56:50 +0100272}
273
Christopher Faulet9ed77422022-04-12 08:51:15 +0200274/* Attaches a conn_stream to an applet endpoint and sets the endpoint
275 * ctx. Returns -1 on error and 0 on sucess. CS_EP_DETACHED flag is
276 * removed. This function is called by a stream when a backend applet is
277 * registered.
278 */
Christopher Faulet2d9cc852022-05-16 17:29:37 +0200279static void cs_attach_applet(struct conn_stream *cs, void *target)
Christopher Faulet93882042022-01-19 14:56:50 +0100280{
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100281 cs->endp->target = target;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100282 cs->endp->flags |= CS_EP_T_APPLET;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100283 cs->endp->flags &= ~CS_EP_DETACHED;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100284 if (cs_strm(cs)) {
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200285 cs->ops = &cs_app_applet_ops;
Christopher Faulet6059ba42022-04-01 16:34:53 +0200286 cs->data_cb = &cs_data_applet_cb;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100287 }
288}
289
Christopher Faulet9ed77422022-04-12 08:51:15 +0200290/* Attaches a conn_stream to a app layer and sets the relevant
291 * callbacks. Returns -1 on error and 0 on success. CS_EP_ORPHAN flag is
292 * removed. This function is called by a stream when it is created to attach it
293 * on the conn-stream on the client side.
294 */
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100295int cs_attach_strm(struct conn_stream *cs, struct stream *strm)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100296{
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100297 cs->app = &strm->obj_type;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100298 cs->endp->flags &= ~CS_EP_ORPHAN;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100299 if (cs->endp->flags & CS_EP_T_MUX) {
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200300 cs->wait_event.tasklet = tasklet_new();
Christopher Faulet582a2262022-04-04 11:25:59 +0200301 if (!cs->wait_event.tasklet)
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200302 return -1;
Christopher Faulet4a7764a2022-04-01 16:58:52 +0200303 cs->wait_event.tasklet->process = cs_conn_io_cb;
304 cs->wait_event.tasklet->context = cs;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200305 cs->wait_event.events = 0;
306
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200307 cs->ops = &cs_app_conn_ops;
Christopher Faulet000ba3e2022-04-01 17:06:32 +0200308 cs->data_cb = &cs_data_conn_cb;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100309 }
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100310 else if (cs->endp->flags & CS_EP_T_APPLET) {
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 Fauleta9e8b392022-03-23 11:01:09 +0100313 }
314 else {
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200315 cs->ops = &cs_app_embedded_ops;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100316 cs->data_cb = NULL;
317 }
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100318 return 0;
319}
320
Christopher Faulet9ed77422022-04-12 08:51:15 +0200321/* Detaches the conn_stream from the endpoint, if any. For a connecrion, if a
322 * mux owns the connection ->detach() callback is called. Otherwise, it means
323 * the conn-stream owns the connection. In this case the connection is closed
324 * and released. For an applet, the appctx is released. If still allocated, the
325 * endpoint is reset and flag as detached. If the app layer is also detached,
326 * the conn-stream is released.
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100327 */
Christopher Fauleteb50c012022-04-21 14:22:53 +0200328static void cs_detach_endp(struct conn_stream **csp)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100329{
Christopher Fauleteb50c012022-04-21 14:22:53 +0200330 struct conn_stream *cs = *csp;
331
332 if (!cs)
333 return;
334
Christopher Fauletb041b232022-03-24 10:27:02 +0100335 if (!cs->endp)
336 goto reset_cs;
337
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100338 if (cs->endp->flags & CS_EP_T_MUX) {
Christopher Fauletcea05432022-04-14 11:40:12 +0200339 struct connection *conn = __cs_conn(cs);
Willy Tarreau4201ab72022-05-10 19:18:52 +0200340 struct cs_endpoint *endp = cs->endp;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100341
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100342 if (conn->mux) {
Christopher Faulet54e85cb2022-01-06 08:46:56 +0100343 /* TODO: handle unsubscribe for healthchecks too */
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200344 if (cs->wait_event.events != 0)
345 conn->mux->unsubscribe(cs, cs->wait_event.events, &cs->wait_event);
Willy Tarreau4201ab72022-05-10 19:18:52 +0200346 endp->flags |= CS_EP_ORPHAN;
347 endp->cs = NULL;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100348 cs->endp = NULL;
Willy Tarreau4201ab72022-05-10 19:18:52 +0200349 conn->mux->detach(endp);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100350 }
351 else {
352 /* It's too early to have a mux, let's just destroy
353 * the connection
354 */
355 conn_stop_tracking(conn);
356 conn_full_close(conn);
357 if (conn->destroy_cb)
358 conn->destroy_cb(conn);
359 conn_free(conn);
360 }
361 }
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100362 else if (cs->endp->flags & CS_EP_T_APPLET) {
Christopher Fauletcea05432022-04-14 11:40:12 +0200363 struct appctx *appctx = __cs_appctx(cs);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100364
Willy Tarreauefb46182022-05-10 09:04:18 +0200365 cs->endp->flags |= CS_EP_ORPHAN;
366 cs->endp->cs = NULL;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100367 cs->endp = NULL;
Willy Tarreau1c3ead42022-05-10 19:42:22 +0200368 appctx_shut(appctx);
369 appctx_free(appctx);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100370 }
371
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100372 if (cs->endp) {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100373 /* the cs is the only one one the endpoint */
Christopher Fauleta6c4a482022-04-28 18:25:24 +0200374 cs->endp->target = NULL;
375 cs->endp->ctx = NULL;
Christopher Fauletc41f93c2022-05-04 09:52:48 +0200376 cs->endp->flags &= CS_EP_APP_MASK;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100377 cs->endp->flags |= CS_EP_DETACHED;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100378 }
379
Christopher Fauletb041b232022-03-24 10:27:02 +0100380 reset_cs:
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100381 /* FIXME: Rest CS for now but must be reviewed. CS flags are only
382 * connection related for now but this will evolved
383 */
Christopher Faulet30995112022-03-25 15:32:38 +0100384 cs->flags &= CS_FL_ISBACK;
Christopher Faulet582a2262022-04-04 11:25:59 +0200385 if (cs_strm(cs))
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200386 cs->ops = &cs_app_embedded_ops;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100387 cs->data_cb = NULL;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200388 cs_free_cond(csp);
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100389}
390
Christopher Faulet9ed77422022-04-12 08:51:15 +0200391/* Detaches the conn_stream from the app layer. If there is no endpoint attached
392 * to the conn_stream
393 */
Christopher Fauleteb50c012022-04-21 14:22:53 +0200394static void cs_detach_app(struct conn_stream **csp)
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100395{
Christopher Fauleteb50c012022-04-21 14:22:53 +0200396 struct conn_stream *cs = *csp;
397
398 if (!cs)
399 return;
400
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100401 cs->app = NULL;
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100402 cs->data_cb = NULL;
Christopher Faulet8da67aa2022-03-29 17:53:09 +0200403 sockaddr_free(&cs->src);
404 sockaddr_free(&cs->dst);
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200405
406 if (cs->wait_event.tasklet)
407 tasklet_free(cs->wait_event.tasklet);
408 cs->wait_event.tasklet = NULL;
409 cs->wait_event.events = 0;
Christopher Fauleteb50c012022-04-21 14:22:53 +0200410 cs_free_cond(csp);
411}
412
413/* Destroy the conn_stream. It is detached from its endpoint and its
414 * application. After this call, the conn_stream must be considered as released.
415 */
416void cs_destroy(struct conn_stream *cs)
417{
418 cs_detach_endp(&cs);
419 cs_detach_app(&cs);
420 BUG_ON_HOT(cs);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100421}
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100422
Christopher Faulet9ed77422022-04-12 08:51:15 +0200423/* Resets the conn-stream endpoint. It happens when the app layer want to renew
424 * its endpoint. For a connection retry for instance. If a mux or an applet is
425 * attached, a new endpoint is created. Returns -1 on error and 0 on sucess.
Christopher Fauleta6c4a482022-04-28 18:25:24 +0200426 *
427 * Only CS_EP_ERROR flag is removed on the endpoint. Orther flags are preserved.
428 * It is the caller responsibility to remove other flags if needed.
Christopher Faulet9ed77422022-04-12 08:51:15 +0200429 */
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100430int cs_reset_endp(struct conn_stream *cs)
431{
Christopher Fauletb041b232022-03-24 10:27:02 +0100432 struct cs_endpoint *new_endp;
433
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100434 BUG_ON(!cs->app);
Christopher Fauleta6c4a482022-04-28 18:25:24 +0200435
436 cs->endp->flags &= ~CS_EP_ERROR;
Christopher Fauletb041b232022-03-24 10:27:02 +0100437 if (!__cs_endp_target(cs)) {
438 /* endpoint not attached or attached to a mux with no
439 * target. Thus the endpoint will not be release but just
Christopher Fauleteb50c012022-04-21 14:22:53 +0200440 * reset. The app is still attached, the cs will not be
441 * released.
Christopher Fauletb041b232022-03-24 10:27:02 +0100442 */
Christopher Fauleteb50c012022-04-21 14:22:53 +0200443 cs_detach_endp(&cs);
Christopher Fauletb041b232022-03-24 10:27:02 +0100444 return 0;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100445 }
Christopher Fauletb041b232022-03-24 10:27:02 +0100446
447 /* allocate the new endpoint first to be able to set error if it
448 * fails */
449 new_endp = cs_endpoint_new();
450 if (!unlikely(new_endp)) {
451 cs->endp->flags |= CS_EP_ERROR;
452 return -1;
453 }
Christopher Fauletc41f93c2022-05-04 09:52:48 +0200454 new_endp->flags = (cs->endp->flags & CS_EP_APP_MASK);
Christopher Fauletb041b232022-03-24 10:27:02 +0100455
Christopher Fauleteb50c012022-04-21 14:22:53 +0200456 /* The app is still attached, the cs will not be released */
457 cs_detach_endp(&cs);
Christopher Fauletb041b232022-03-24 10:27:02 +0100458 BUG_ON(cs->endp);
459 cs->endp = new_endp;
Willy Tarreauefb46182022-05-10 09:04:18 +0200460 cs->endp->cs = cs;
Christopher Fauletb041b232022-03-24 10:27:02 +0100461 cs->endp->flags |= CS_EP_DETACHED;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100462 return 0;
463}
Christopher Faulet37046632022-04-01 11:36:58 +0200464
465
Christopher Faulet1336ccf2022-04-12 18:15:16 +0200466/* Create an applet to handle a conn-stream as a new appctx. The CS will
Christopher Faulet37046632022-04-01 11:36:58 +0200467 * wake it up every time it is solicited. The appctx must be deleted by the task
468 * handler using cs_detach_endp(), possibly from within the function itself.
469 * It also pre-initializes the applet's context and returns it (or NULL in case
470 * it could not be allocated).
471 */
Christopher Faulet1336ccf2022-04-12 18:15:16 +0200472struct appctx *cs_applet_create(struct conn_stream *cs, struct applet *app)
Christopher Faulet37046632022-04-01 11:36:58 +0200473{
474 struct appctx *appctx;
475
476 DPRINTF(stderr, "registering handler %p for cs %p (was %p)\n", app, cs, cs_strm_task(cs));
477
Christopher Faulet6095d572022-05-16 17:09:48 +0200478 appctx = appctx_new_here(app, cs->endp);
Christopher Faulet37046632022-04-01 11:36:58 +0200479 if (!appctx)
480 return NULL;
Christopher Faulet2d9cc852022-05-16 17:29:37 +0200481 cs_attach_applet(cs, appctx);
Christopher Faulet37046632022-04-01 11:36:58 +0200482 appctx->t->nice = __cs_strm(cs)->task->nice;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200483 cs_cant_get(cs);
Christopher Faulet37046632022-04-01 11:36:58 +0200484 appctx_wakeup(appctx);
Christopher Fauleta33ff7a2022-04-21 11:52:07 +0200485
486 cs->state = CS_ST_RDY;
Christopher Faulet37046632022-04-01 11:36:58 +0200487 return appctx;
488}
489
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200490/*
491 * This function performs a shutdown-read on a detached conn-stream in a
492 * connected or init state (it does nothing for other states). It either shuts
493 * the read side or marks itself as closed. The buffer flags are updated to
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +0200494 * reflect the new state. If the conn-stream has CS_FL_NOHALF, we also
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200495 * forward the close to the write side. The owner task is woken up if it exists.
496 */
497static void cs_app_shutr(struct conn_stream *cs)
498{
499 struct channel *ic = cs_ic(cs);
500
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200501 cs_rx_shut_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200502 if (ic->flags & CF_SHUTR)
503 return;
504 ic->flags |= CF_SHUTR;
505 ic->rex = TICK_ETERNITY;
506
507 if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
508 return;
509
510 if (cs_oc(cs)->flags & CF_SHUTW) {
511 cs->state = CS_ST_DIS;
512 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
513 }
514 else if (cs->flags & CS_FL_NOHALF) {
515 /* we want to immediately forward this close to the write side */
516 return cs_app_shutw(cs);
517 }
518
519 /* note that if the task exists, it must unregister itself once it runs */
520 if (!(cs->flags & CS_FL_DONT_WAKE))
521 task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO);
522}
523
524/*
525 * This function performs a shutdown-write on a detached conn-stream in a
526 * connected or init state (it does nothing for other states). It either shuts
527 * the write side or marks itself as closed. The buffer flags are updated to
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +0200528 * reflect the new state. It does also close everything if the CS was marked as
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200529 * being in error state. The owner task is woken up if it exists.
530 */
531static void cs_app_shutw(struct conn_stream *cs)
532{
533 struct channel *ic = cs_ic(cs);
534 struct channel *oc = cs_oc(cs);
535
536 oc->flags &= ~CF_SHUTW_NOW;
537 if (oc->flags & CF_SHUTW)
538 return;
539 oc->flags |= CF_SHUTW;
540 oc->wex = TICK_ETERNITY;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200541 cs_done_get(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200542
543 if (tick_isset(cs->hcto)) {
544 ic->rto = cs->hcto;
545 ic->rex = tick_add(now_ms, ic->rto);
546 }
547
548 switch (cs->state) {
549 case CS_ST_RDY:
550 case CS_ST_EST:
551 /* we have to shut before closing, otherwise some short messages
552 * may never leave the system, especially when there are remaining
553 * unread data in the socket input buffer, or when nolinger is set.
554 * However, if CS_FL_NOLINGER is explicitly set, we know there is
555 * no risk so we close both sides immediately.
556 */
557 if (!(cs->endp->flags & CS_EP_ERROR) && !(cs->flags & CS_FL_NOLINGER) &&
558 !(ic->flags & (CF_SHUTR|CF_DONT_READ)))
559 return;
560
561 /* fall through */
562 case CS_ST_CON:
563 case CS_ST_CER:
564 case CS_ST_QUE:
565 case CS_ST_TAR:
566 /* Note that none of these states may happen with applets */
567 cs->state = CS_ST_DIS;
568 /* fall through */
569 default:
570 cs->flags &= ~CS_FL_NOLINGER;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200571 cs_rx_shut_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200572 ic->flags |= CF_SHUTR;
573 ic->rex = TICK_ETERNITY;
574 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
575 }
576
577 /* note that if the task exists, it must unregister itself once it runs */
578 if (!(cs->flags & CS_FL_DONT_WAKE))
579 task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO);
580}
581
582/* default chk_rcv function for scheduled tasks */
583static void cs_app_chk_rcv(struct conn_stream *cs)
584{
585 struct channel *ic = cs_ic(cs);
586
587 DPRINTF(stderr, "%s: cs=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n",
588 __FUNCTION__,
589 cs, cs->state, ic->flags, cs_oc(cs)->flags);
590
591 if (ic->pipe) {
592 /* stop reading */
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200593 cs_rx_room_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200594 }
595 else {
596 /* (re)start reading */
597 if (!(cs->flags & CS_FL_DONT_WAKE))
598 task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO);
599 }
600}
601
602/* default chk_snd function for scheduled tasks */
603static void cs_app_chk_snd(struct conn_stream *cs)
604{
605 struct channel *oc = cs_oc(cs);
606
607 DPRINTF(stderr, "%s: cs=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n",
608 __FUNCTION__,
609 cs, cs->state, cs_ic(cs)->flags, oc->flags);
610
611 if (unlikely(cs->state != CS_ST_EST || (oc->flags & CF_SHUTW)))
612 return;
613
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200614 if (!(cs->endp->flags & CS_EP_WAIT_DATA) || /* not waiting for data */
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200615 channel_is_empty(oc)) /* called with nothing to send ! */
616 return;
617
618 /* Otherwise there are remaining data to be sent in the buffer,
619 * so we tell the handler.
620 */
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200621 cs->endp->flags &= ~CS_EP_WAIT_DATA;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200622 if (!tick_isset(oc->wex))
623 oc->wex = tick_add_ifset(now_ms, oc->wto);
624
625 if (!(cs->flags & CS_FL_DONT_WAKE))
626 task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO);
627}
628
629/*
630 * This function performs a shutdown-read on a conn-stream attached to
631 * a connection in a connected or init state (it does nothing for other
632 * states). It either shuts the read side or marks itself as closed. The buffer
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +0200633 * flags are updated to reflect the new state. If the conn-stream has
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200634 * CS_FL_NOHALF, we also forward the close to the write side. If a control
635 * layer is defined, then it is supposed to be a socket layer and file
636 * descriptors are then shutdown or closed accordingly. The function
637 * automatically disables polling if needed.
638 */
639static void cs_app_shutr_conn(struct conn_stream *cs)
640{
641 struct channel *ic = cs_ic(cs);
642
643 BUG_ON(!cs_conn(cs));
644
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200645 cs_rx_shut_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200646 if (ic->flags & CF_SHUTR)
647 return;
648 ic->flags |= CF_SHUTR;
649 ic->rex = TICK_ETERNITY;
650
651 if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
652 return;
653
654 if (cs_oc(cs)->flags & CF_SHUTW) {
Christopher Fauletff022a22022-04-21 08:38:54 +0200655 cs_conn_shut(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200656 cs->state = CS_ST_DIS;
657 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
658 }
659 else if (cs->flags & CS_FL_NOHALF) {
660 /* we want to immediately forward this close to the write side */
661 return cs_app_shutw_conn(cs);
662 }
663}
664
665/*
666 * This function performs a shutdown-write on a conn-stream attached to
667 * a connection in a connected or init state (it does nothing for other
668 * states). It either shuts the write side or marks itself as closed. The
669 * buffer flags are updated to reflect the new state. It does also close
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +0200670 * everything if the CS was marked as being in error state. If there is a
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200671 * data-layer shutdown, it is called.
672 */
673static void cs_app_shutw_conn(struct conn_stream *cs)
674{
675 struct channel *ic = cs_ic(cs);
676 struct channel *oc = cs_oc(cs);
677
678 BUG_ON(!cs_conn(cs));
679
680 oc->flags &= ~CF_SHUTW_NOW;
681 if (oc->flags & CF_SHUTW)
682 return;
683 oc->flags |= CF_SHUTW;
684 oc->wex = TICK_ETERNITY;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200685 cs_done_get(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200686
687 if (tick_isset(cs->hcto)) {
688 ic->rto = cs->hcto;
689 ic->rex = tick_add(now_ms, ic->rto);
690 }
691
692 switch (cs->state) {
693 case CS_ST_RDY:
694 case CS_ST_EST:
695 /* we have to shut before closing, otherwise some short messages
696 * may never leave the system, especially when there are remaining
697 * unread data in the socket input buffer, or when nolinger is set.
698 * However, if CS_FL_NOLINGER is explicitly set, we know there is
699 * no risk so we close both sides immediately.
700 */
701
702 if (cs->endp->flags & CS_EP_ERROR) {
703 /* quick close, the socket is already shut anyway */
704 }
705 else if (cs->flags & CS_FL_NOLINGER) {
706 /* unclean data-layer shutdown, typically an aborted request
707 * or a forwarded shutdown from a client to a server due to
708 * option abortonclose. No need for the TLS layer to try to
709 * emit a shutdown message.
710 */
711 cs_conn_shutw(cs, CO_SHW_SILENT);
712 }
713 else {
714 /* clean data-layer shutdown. This only happens on the
715 * frontend side, or on the backend side when forwarding
716 * a client close in TCP mode or in HTTP TUNNEL mode
717 * while option abortonclose is set. We want the TLS
718 * layer to try to signal it to the peer before we close.
719 */
720 cs_conn_shutw(cs, CO_SHW_NORMAL);
721
722 if (!(ic->flags & (CF_SHUTR|CF_DONT_READ)))
723 return;
724 }
725
726 /* fall through */
727 case CS_ST_CON:
728 /* we may have to close a pending connection, and mark the
729 * response buffer as shutr
730 */
Christopher Fauletff022a22022-04-21 08:38:54 +0200731 cs_conn_shut(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200732 /* fall through */
733 case CS_ST_CER:
734 case CS_ST_QUE:
735 case CS_ST_TAR:
736 cs->state = CS_ST_DIS;
737 /* fall through */
738 default:
739 cs->flags &= ~CS_FL_NOLINGER;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200740 cs_rx_shut_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200741 ic->flags |= CF_SHUTR;
742 ic->rex = TICK_ETERNITY;
743 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
744 }
745}
746
747/* This function is used for inter-conn-stream calls. It is called by the
748 * consumer to inform the producer side that it may be interested in checking
749 * for free space in the buffer. Note that it intentionally does not update
750 * timeouts, so that we can still check them later at wake-up. This function is
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +0200751 * dedicated to connection-based conn-streams.
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200752 */
753static void cs_app_chk_rcv_conn(struct conn_stream *cs)
754{
755 BUG_ON(!cs_conn(cs));
756
757 /* (re)start reading */
758 if (cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
759 tasklet_wakeup(cs->wait_event.tasklet);
760}
761
762
763/* This function is used for inter-conn-stream calls. It is called by the
764 * producer to inform the consumer side that it may be interested in checking
765 * for data in the buffer. Note that it intentionally does not update timeouts,
766 * so that we can still check them later at wake-up.
767 */
768static void cs_app_chk_snd_conn(struct conn_stream *cs)
769{
770 struct channel *oc = cs_oc(cs);
771
772 BUG_ON(!cs_conn(cs));
773
Willy Tarreau4173f4e2022-04-29 15:04:41 +0200774 if (unlikely(!cs_state_in(cs->state, CS_SB_RDY|CS_SB_EST) ||
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200775 (oc->flags & CF_SHUTW)))
776 return;
777
778 if (unlikely(channel_is_empty(oc))) /* called with nothing to send ! */
779 return;
780
781 if (!oc->pipe && /* spliced data wants to be forwarded ASAP */
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200782 !(cs->endp->flags & CS_EP_WAIT_DATA)) /* not waiting for data */
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200783 return;
784
785 if (!(cs->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(cs_oc(cs)))
Christopher Faulet000ba3e2022-04-01 17:06:32 +0200786 cs_conn_send(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200787
Christopher Faulet158f3362022-04-01 17:15:10 +0200788 if (cs->endp->flags & (CS_EP_ERROR|CS_EP_ERR_PENDING) || cs_is_conn_error(cs)) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200789 /* Write error on the file descriptor */
790 if (cs->state >= CS_ST_CON)
791 cs->endp->flags |= CS_EP_ERROR;
792 goto out_wakeup;
793 }
794
795 /* OK, so now we know that some data might have been sent, and that we may
796 * have to poll first. We have to do that too if the buffer is not empty.
797 */
798 if (channel_is_empty(oc)) {
799 /* the connection is established but we can't write. Either the
800 * buffer is empty, or we just refrain from sending because the
801 * ->o limit was reached. Maybe we just wrote the last
802 * chunk and need to close.
803 */
804 if (((oc->flags & (CF_SHUTW|CF_AUTO_CLOSE|CF_SHUTW_NOW)) ==
805 (CF_AUTO_CLOSE|CF_SHUTW_NOW)) &&
806 cs_state_in(cs->state, CS_SB_RDY|CS_SB_EST)) {
807 cs_shutw(cs);
808 goto out_wakeup;
809 }
810
811 if ((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == 0)
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200812 cs->endp->flags |= CS_EP_WAIT_DATA;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200813 oc->wex = TICK_ETERNITY;
814 }
815 else {
816 /* Otherwise there are remaining data to be sent in the buffer,
817 * which means we have to poll before doing so.
818 */
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200819 cs->endp->flags &= ~CS_EP_WAIT_DATA;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200820 if (!tick_isset(oc->wex))
821 oc->wex = tick_add_ifset(now_ms, oc->wto);
822 }
823
824 if (likely(oc->flags & CF_WRITE_ACTIVITY)) {
825 struct channel *ic = cs_ic(cs);
826
827 /* update timeout if we have written something */
828 if ((oc->flags & (CF_SHUTW|CF_WRITE_PARTIAL)) == CF_WRITE_PARTIAL &&
829 !channel_is_empty(oc))
830 oc->wex = tick_add_ifset(now_ms, oc->wto);
831
832 if (tick_isset(ic->rex) && !(cs->flags & CS_FL_INDEP_STR)) {
833 /* Note: to prevent the client from expiring read timeouts
834 * during writes, we refresh it. We only do this if the
835 * interface is not configured for "independent streams",
836 * because for some applications it's better not to do this,
837 * for instance when continuously exchanging small amounts
838 * of data which can full the socket buffers long before a
839 * write timeout is detected.
840 */
841 ic->rex = tick_add_ifset(now_ms, ic->rto);
842 }
843 }
844
845 /* in case of special condition (error, shutdown, end of write...), we
846 * have to notify the task.
847 */
848 if (likely((oc->flags & (CF_WRITE_NULL|CF_WRITE_ERROR|CF_SHUTW)) ||
849 ((oc->flags & CF_WAKE_WRITE) &&
850 ((channel_is_empty(oc) && !oc->to_forward) ||
851 !cs_state_in(cs->state, CS_SB_EST))))) {
852 out_wakeup:
853 if (!(cs->flags & CS_FL_DONT_WAKE))
854 task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO);
855 }
856}
857
858/*
859 * This function performs a shutdown-read on a conn-stream attached to an
860 * applet in a connected or init state (it does nothing for other states). It
861 * either shuts the read side or marks itself as closed. The buffer flags are
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +0200862 * updated to reflect the new state. If the conn-stream has CS_FL_NOHALF,
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200863 * we also forward the close to the write side. The owner task is woken up if
864 * it exists.
865 */
866static void cs_app_shutr_applet(struct conn_stream *cs)
867{
868 struct channel *ic = cs_ic(cs);
869
870 BUG_ON(!cs_appctx(cs));
871
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200872 cs_rx_shut_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200873 if (ic->flags & CF_SHUTR)
874 return;
875 ic->flags |= CF_SHUTR;
876 ic->rex = TICK_ETERNITY;
877
878 /* Note: on shutr, we don't call the applet */
879
880 if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
881 return;
882
883 if (cs_oc(cs)->flags & CF_SHUTW) {
Willy Tarreau1c3ead42022-05-10 19:42:22 +0200884 appctx_shut(__cs_appctx(cs));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200885 cs->state = CS_ST_DIS;
886 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
887 }
888 else if (cs->flags & CS_FL_NOHALF) {
889 /* we want to immediately forward this close to the write side */
890 return cs_app_shutw_applet(cs);
891 }
892}
893
894/*
895 * This function performs a shutdown-write on a conn-stream attached to an
896 * applet in a connected or init state (it does nothing for other states). It
897 * either shuts the write side or marks itself as closed. The buffer flags are
898 * updated to reflect the new state. It does also close everything if the SI
899 * was marked as being in error state. The owner task is woken up if it exists.
900 */
901static void cs_app_shutw_applet(struct conn_stream *cs)
902{
903 struct channel *ic = cs_ic(cs);
904 struct channel *oc = cs_oc(cs);
905
906 BUG_ON(!cs_appctx(cs));
907
908 oc->flags &= ~CF_SHUTW_NOW;
909 if (oc->flags & CF_SHUTW)
910 return;
911 oc->flags |= CF_SHUTW;
912 oc->wex = TICK_ETERNITY;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200913 cs_done_get(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200914
915 if (tick_isset(cs->hcto)) {
916 ic->rto = cs->hcto;
917 ic->rex = tick_add(now_ms, ic->rto);
918 }
919
920 /* on shutw we always wake the applet up */
921 appctx_wakeup(__cs_appctx(cs));
922
923 switch (cs->state) {
924 case CS_ST_RDY:
925 case CS_ST_EST:
926 /* we have to shut before closing, otherwise some short messages
927 * may never leave the system, especially when there are remaining
928 * unread data in the socket input buffer, or when nolinger is set.
929 * However, if CS_FL_NOLINGER is explicitly set, we know there is
930 * no risk so we close both sides immediately.
931 */
932 if (!(cs->endp->flags & CS_EP_ERROR) && !(cs->flags & CS_FL_NOLINGER) &&
933 !(ic->flags & (CF_SHUTR|CF_DONT_READ)))
934 return;
935
936 /* fall through */
937 case CS_ST_CON:
938 case CS_ST_CER:
939 case CS_ST_QUE:
940 case CS_ST_TAR:
941 /* Note that none of these states may happen with applets */
Willy Tarreau1c3ead42022-05-10 19:42:22 +0200942 appctx_shut(__cs_appctx(cs));
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200943 cs->state = CS_ST_DIS;
944 /* fall through */
945 default:
946 cs->flags &= ~CS_FL_NOLINGER;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200947 cs_rx_shut_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200948 ic->flags |= CF_SHUTR;
949 ic->rex = TICK_ETERNITY;
950 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
951 }
952}
953
954/* chk_rcv function for applets */
955static void cs_app_chk_rcv_applet(struct conn_stream *cs)
956{
957 struct channel *ic = cs_ic(cs);
958
959 BUG_ON(!cs_appctx(cs));
960
961 DPRINTF(stderr, "%s: cs=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n",
962 __FUNCTION__,
963 cs, cs->state, ic->flags, cs_oc(cs)->flags);
964
965 if (!ic->pipe) {
966 /* (re)start reading */
967 appctx_wakeup(__cs_appctx(cs));
968 }
969}
970
971/* chk_snd function for applets */
972static void cs_app_chk_snd_applet(struct conn_stream *cs)
973{
974 struct channel *oc = cs_oc(cs);
975
976 BUG_ON(!cs_appctx(cs));
977
978 DPRINTF(stderr, "%s: cs=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n",
979 __FUNCTION__,
980 cs, cs->state, cs_ic(cs)->flags, oc->flags);
981
982 if (unlikely(cs->state != CS_ST_EST || (oc->flags & CF_SHUTW)))
983 return;
984
985 /* we only wake the applet up if it was waiting for some data */
986
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200987 if (!(cs->endp->flags & CS_EP_WAIT_DATA))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200988 return;
989
990 if (!tick_isset(oc->wex))
991 oc->wex = tick_add_ifset(now_ms, oc->wto);
992
993 if (!channel_is_empty(oc)) {
994 /* (re)start sending */
995 appctx_wakeup(__cs_appctx(cs));
996 }
997}
Christopher Faulet13045f02022-04-01 14:23:38 +0200998
999
1000/* This function is designed to be called from within the stream handler to
1001 * update the input channel's expiration timer and the conn-stream's
1002 * Rx flags based on the channel's flags. It needs to be called only once
1003 * after the channel's flags have settled down, and before they are cleared,
1004 * though it doesn't harm to call it as often as desired (it just slightly
1005 * hurts performance). It must not be called from outside of the stream
1006 * handler, as what it does will be used to compute the stream task's
1007 * expiration.
1008 */
1009void cs_update_rx(struct conn_stream *cs)
1010{
1011 struct channel *ic = cs_ic(cs);
1012
1013 if (ic->flags & CF_SHUTR) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001014 cs_rx_shut_blk(cs);
Christopher Faulet13045f02022-04-01 14:23:38 +02001015 return;
1016 }
1017
1018 /* Read not closed, update FD status and timeout for reads */
1019 if (ic->flags & CF_DONT_READ)
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001020 cs_rx_chan_blk(cs);
Christopher Faulet13045f02022-04-01 14:23:38 +02001021 else
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001022 cs_rx_chan_rdy(cs);
Christopher Faulet13045f02022-04-01 14:23:38 +02001023
1024 if (!channel_is_empty(ic) || !channel_may_recv(ic)) {
1025 /* stop reading, imposed by channel's policy or contents */
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001026 cs_rx_room_blk(cs);
Christopher Faulet13045f02022-04-01 14:23:38 +02001027 }
1028 else {
1029 /* (re)start reading and update timeout. Note: we don't recompute the timeout
1030 * every time we get here, otherwise it would risk never to expire. We only
1031 * update it if is was not yet set. The stream socket handler will already
1032 * have updated it if there has been a completed I/O.
1033 */
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001034 cs_rx_room_rdy(cs);
Christopher Faulet13045f02022-04-01 14:23:38 +02001035 }
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001036 if (cs->endp->flags & CS_EP_RXBLK_ANY & ~CS_EP_RX_WAIT_EP)
Christopher Faulet13045f02022-04-01 14:23:38 +02001037 ic->rex = TICK_ETERNITY;
1038 else if (!(ic->flags & CF_READ_NOEXP) && !tick_isset(ic->rex))
1039 ic->rex = tick_add_ifset(now_ms, ic->rto);
1040
1041 cs_chk_rcv(cs);
1042}
1043
1044/* This function is designed to be called from within the stream handler to
1045 * update the output channel's expiration timer and the conn-stream's
1046 * Tx flags based on the channel's flags. It needs to be called only once
1047 * after the channel's flags have settled down, and before they are cleared,
1048 * though it doesn't harm to call it as often as desired (it just slightly
1049 * hurts performance). It must not be called from outside of the stream
1050 * handler, as what it does will be used to compute the stream task's
1051 * expiration.
1052 */
1053void cs_update_tx(struct conn_stream *cs)
1054{
1055 struct channel *oc = cs_oc(cs);
1056 struct channel *ic = cs_ic(cs);
1057
1058 if (oc->flags & CF_SHUTW)
1059 return;
1060
1061 /* Write not closed, update FD status and timeout for writes */
1062 if (channel_is_empty(oc)) {
1063 /* stop writing */
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001064 if (!(cs->endp->flags & CS_EP_WAIT_DATA)) {
Christopher Faulet13045f02022-04-01 14:23:38 +02001065 if ((oc->flags & CF_SHUTW_NOW) == 0)
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001066 cs->endp->flags |= CS_EP_WAIT_DATA;
Christopher Faulet13045f02022-04-01 14:23:38 +02001067 oc->wex = TICK_ETERNITY;
1068 }
1069 return;
1070 }
1071
1072 /* (re)start writing and update timeout. Note: we don't recompute the timeout
1073 * every time we get here, otherwise it would risk never to expire. We only
1074 * update it if is was not yet set. The stream socket handler will already
1075 * have updated it if there has been a completed I/O.
1076 */
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001077 cs->endp->flags &= ~CS_EP_WAIT_DATA;
Christopher Faulet13045f02022-04-01 14:23:38 +02001078 if (!tick_isset(oc->wex)) {
1079 oc->wex = tick_add_ifset(now_ms, oc->wto);
1080 if (tick_isset(ic->rex) && !(cs->flags & CS_FL_INDEP_STR)) {
1081 /* Note: depending on the protocol, we don't know if we're waiting
1082 * for incoming data or not. So in order to prevent the socket from
1083 * expiring read timeouts during writes, we refresh the read timeout,
1084 * except if it was already infinite or if we have explicitly setup
1085 * independent streams.
1086 */
1087 ic->rex = tick_add_ifset(now_ms, ic->rto);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001088 }
1089 }
1090}
1091
1092/* This function is the equivalent to cs_update() except that it's
1093 * designed to be called from outside the stream handlers, typically the lower
1094 * layers (applets, connections) after I/O completion. After updating the stream
1095 * interface and timeouts, it will try to forward what can be forwarded, then to
1096 * wake the associated task up if an important event requires special handling.
1097 * It may update CS_EP_WAIT_DATA and/or CS_EP_RXBLK_ROOM, that the callers are
1098 * encouraged to watch to take appropriate action.
1099 * It should not be called from within the stream itself, cs_update()
1100 * is designed for this.
1101 */
1102static void cs_notify(struct conn_stream *cs)
1103{
1104 struct channel *ic = cs_ic(cs);
1105 struct channel *oc = cs_oc(cs);
1106 struct conn_stream *cso = cs_opposite(cs);
1107 struct task *task = cs_strm_task(cs);
1108
1109 /* process consumer side */
1110 if (channel_is_empty(oc)) {
1111 struct connection *conn = cs_conn(cs);
1112
1113 if (((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW) &&
1114 (cs->state == CS_ST_EST) && (!conn || !(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS))))
1115 cs_shutw(cs);
1116 oc->wex = TICK_ETERNITY;
1117 }
1118
1119 /* indicate that we may be waiting for data from the output channel or
1120 * we're about to close and can't expect more data if SHUTW_NOW is there.
1121 */
1122 if (!(oc->flags & (CF_SHUTW|CF_SHUTW_NOW)))
1123 cs->endp->flags |= CS_EP_WAIT_DATA;
1124 else if ((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW)
1125 cs->endp->flags &= ~CS_EP_WAIT_DATA;
1126
1127 /* update OC timeouts and wake the other side up if it's waiting for room */
1128 if (oc->flags & CF_WRITE_ACTIVITY) {
1129 if ((oc->flags & (CF_SHUTW|CF_WRITE_PARTIAL)) == CF_WRITE_PARTIAL &&
1130 !channel_is_empty(oc))
1131 if (tick_isset(oc->wex))
1132 oc->wex = tick_add_ifset(now_ms, oc->wto);
1133
1134 if (!(cs->flags & CS_FL_INDEP_STR))
1135 if (tick_isset(ic->rex))
1136 ic->rex = tick_add_ifset(now_ms, ic->rto);
1137 }
1138
1139 if (oc->flags & CF_DONT_READ)
1140 cs_rx_chan_blk(cso);
1141 else
1142 cs_rx_chan_rdy(cso);
1143
1144 /* Notify the other side when we've injected data into the IC that
1145 * needs to be forwarded. We can do fast-forwarding as soon as there
1146 * are output data, but we avoid doing this if some of the data are
1147 * not yet scheduled for being forwarded, because it is very likely
1148 * that it will be done again immediately afterwards once the following
1149 * data are parsed (eg: HTTP chunking). We only CS_EP_RXBLK_ROOM once
1150 * we've emptied *some* of the output buffer, and not just when there
1151 * is available room, because applets are often forced to stop before
1152 * the buffer is full. We must not stop based on input data alone because
1153 * an HTTP parser might need more data to complete the parsing.
1154 */
1155 if (!channel_is_empty(ic) &&
1156 (cso->endp->flags & CS_EP_WAIT_DATA) &&
1157 (!(ic->flags & CF_EXPECT_MORE) || c_full(ic) || ci_data(ic) == 0 || ic->pipe)) {
1158 int new_len, last_len;
1159
1160 last_len = co_data(ic);
1161 if (ic->pipe)
1162 last_len += ic->pipe->data;
1163
1164 cs_chk_snd(cso);
1165
1166 new_len = co_data(ic);
1167 if (ic->pipe)
1168 new_len += ic->pipe->data;
1169
1170 /* check if the consumer has freed some space either in the
1171 * buffer or in the pipe.
1172 */
1173 if (new_len < last_len)
1174 cs_rx_room_rdy(cs);
1175 }
1176
1177 if (!(ic->flags & CF_DONT_READ))
1178 cs_rx_chan_rdy(cs);
1179
1180 cs_chk_rcv(cs);
1181 cs_chk_rcv(cso);
1182
1183 if (cs_rx_blocked(cs)) {
1184 ic->rex = TICK_ETERNITY;
1185 }
1186 else if ((ic->flags & (CF_SHUTR|CF_READ_PARTIAL)) == CF_READ_PARTIAL) {
1187 /* we must re-enable reading if cs_chk_snd() has freed some space */
1188 if (!(ic->flags & CF_READ_NOEXP) && tick_isset(ic->rex))
1189 ic->rex = tick_add_ifset(now_ms, ic->rto);
1190 }
1191
1192 /* wake the task up only when needed */
1193 if (/* changes on the production side */
1194 (ic->flags & (CF_READ_NULL|CF_READ_ERROR)) ||
1195 !cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST) ||
1196 (cs->endp->flags & CS_EP_ERROR) ||
1197 ((ic->flags & CF_READ_PARTIAL) &&
1198 ((ic->flags & CF_EOI) || !ic->to_forward || cso->state != CS_ST_EST)) ||
1199
1200 /* changes on the consumption side */
1201 (oc->flags & (CF_WRITE_NULL|CF_WRITE_ERROR)) ||
1202 ((oc->flags & CF_WRITE_ACTIVITY) &&
1203 ((oc->flags & CF_SHUTW) ||
1204 (((oc->flags & CF_WAKE_WRITE) ||
1205 !(oc->flags & (CF_AUTO_CLOSE|CF_SHUTW_NOW|CF_SHUTW))) &&
1206 (cso->state != CS_ST_EST ||
1207 (channel_is_empty(oc) && !oc->to_forward)))))) {
1208 task_wakeup(task, TASK_WOKEN_IO);
1209 }
1210 else {
1211 /* Update expiration date for the task and requeue it */
1212 task->expire = tick_first((tick_is_expired(task->expire, now_ms) ? 0 : task->expire),
1213 tick_first(tick_first(ic->rex, ic->wex),
1214 tick_first(oc->rex, oc->wex)));
1215
1216 task->expire = tick_first(task->expire, ic->analyse_exp);
1217 task->expire = tick_first(task->expire, oc->analyse_exp);
1218 task->expire = tick_first(task->expire, __cs_strm(cs)->conn_exp);
1219
1220 task_queue(task);
1221 }
1222 if (ic->flags & CF_READ_ACTIVITY)
1223 ic->flags &= ~CF_READ_DONTWAIT;
1224}
1225
1226/*
1227 * This function propagates a null read received on a socket-based connection.
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001228 * It updates the conn-stream. If the conn-stream has CS_FL_NOHALF,
Christopher Faulet5e29b762022-04-04 08:58:34 +02001229 * the close is also forwarded to the write side as an abort.
1230 */
1231static void cs_conn_read0(struct conn_stream *cs)
1232{
1233 struct channel *ic = cs_ic(cs);
1234 struct channel *oc = cs_oc(cs);
1235
1236 BUG_ON(!cs_conn(cs));
1237
1238 cs_rx_shut_blk(cs);
1239 if (ic->flags & CF_SHUTR)
1240 return;
1241 ic->flags |= CF_SHUTR;
1242 ic->rex = TICK_ETERNITY;
1243
1244 if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
1245 return;
1246
1247 if (oc->flags & CF_SHUTW)
1248 goto do_close;
1249
1250 if (cs->flags & CS_FL_NOHALF) {
1251 /* we want to immediately forward this close to the write side */
1252 /* force flag on ssl to keep stream in cache */
1253 cs_conn_shutw(cs, CO_SHW_SILENT);
1254 goto do_close;
1255 }
1256
1257 /* otherwise that's just a normal read shutdown */
1258 return;
1259
1260 do_close:
1261 /* OK we completely close the socket here just as if we went through cs_shut[rw]() */
Christopher Fauletff022a22022-04-21 08:38:54 +02001262 cs_conn_shut(cs);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001263
1264 oc->flags &= ~CF_SHUTW_NOW;
1265 oc->flags |= CF_SHUTW;
1266 oc->wex = TICK_ETERNITY;
1267
1268 cs_done_get(cs);
1269
1270 cs->state = CS_ST_DIS;
1271 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
1272 return;
1273}
1274
1275/*
1276 * This is the callback which is called by the connection layer to receive data
1277 * into the buffer from the connection. It iterates over the mux layer's
1278 * rcv_buf function.
1279 */
1280static int cs_conn_recv(struct conn_stream *cs)
1281{
1282 struct connection *conn = __cs_conn(cs);
1283 struct channel *ic = cs_ic(cs);
1284 int ret, max, cur_read = 0;
1285 int read_poll = MAX_READ_POLL_LOOPS;
1286 int flags = 0;
1287
1288 /* If not established yet, do nothing. */
1289 if (cs->state != CS_ST_EST)
1290 return 0;
1291
1292 /* If another call to cs_conn_recv() failed, and we subscribed to
1293 * recv events already, give up now.
1294 */
1295 if (cs->wait_event.events & SUB_RETRY_RECV)
1296 return 0;
1297
1298 /* maybe we were called immediately after an asynchronous shutr */
1299 if (ic->flags & CF_SHUTR)
1300 return 1;
1301
1302 /* we must wait because the mux is not installed yet */
1303 if (!conn->mux)
1304 return 0;
1305
1306 /* stop here if we reached the end of data */
1307 if (cs->endp->flags & CS_EP_EOS)
1308 goto end_recv;
1309
1310 /* stop immediately on errors. Note that we DON'T want to stop on
1311 * POLL_ERR, as the poller might report a write error while there
1312 * are still data available in the recv buffer. This typically
1313 * happens when we send too large a request to a backend server
1314 * which rejects it before reading it all.
1315 */
1316 if (!(cs->endp->flags & CS_EP_RCV_MORE)) {
1317 if (!conn_xprt_ready(conn))
1318 return 0;
1319 if (cs->endp->flags & CS_EP_ERROR)
1320 goto end_recv;
1321 }
1322
1323 /* prepare to detect if the mux needs more room */
1324 cs->endp->flags &= ~CS_EP_WANT_ROOM;
1325
1326 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) && !co_data(ic) &&
1327 global.tune.idle_timer &&
1328 (unsigned short)(now_ms - ic->last_read) >= global.tune.idle_timer) {
1329 /* The buffer was empty and nothing was transferred for more
1330 * than one second. This was caused by a pause and not by
1331 * congestion. Reset any streaming mode to reduce latency.
1332 */
1333 ic->xfer_small = 0;
1334 ic->xfer_large = 0;
1335 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
1336 }
1337
1338 /* First, let's see if we may splice data across the channel without
1339 * using a buffer.
1340 */
1341 if (cs->endp->flags & CS_EP_MAY_SPLICE &&
1342 (ic->pipe || ic->to_forward >= MIN_SPLICE_FORWARD) &&
1343 ic->flags & CF_KERN_SPLICING) {
1344 if (c_data(ic)) {
1345 /* We're embarrassed, there are already data pending in
1346 * the buffer and we don't want to have them at two
1347 * locations at a time. Let's indicate we need some
1348 * place and ask the consumer to hurry.
1349 */
1350 flags |= CO_RFL_BUF_FLUSH;
1351 goto abort_splice;
1352 }
1353
1354 if (unlikely(ic->pipe == NULL)) {
1355 if (pipes_used >= global.maxpipes || !(ic->pipe = get_pipe())) {
1356 ic->flags &= ~CF_KERN_SPLICING;
1357 goto abort_splice;
1358 }
1359 }
1360
1361 ret = conn->mux->rcv_pipe(cs, ic->pipe, ic->to_forward);
1362 if (ret < 0) {
1363 /* splice not supported on this end, let's disable it */
1364 ic->flags &= ~CF_KERN_SPLICING;
1365 goto abort_splice;
1366 }
1367
1368 if (ret > 0) {
1369 if (ic->to_forward != CHN_INFINITE_FORWARD)
1370 ic->to_forward -= ret;
1371 ic->total += ret;
1372 cur_read += ret;
1373 ic->flags |= CF_READ_PARTIAL;
1374 }
1375
1376 if (cs->endp->flags & (CS_EP_EOS|CS_EP_ERROR))
1377 goto end_recv;
1378
1379 if (conn->flags & CO_FL_WAIT_ROOM) {
1380 /* the pipe is full or we have read enough data that it
1381 * could soon be full. Let's stop before needing to poll.
1382 */
1383 cs_rx_room_blk(cs);
1384 goto done_recv;
1385 }
1386
1387 /* splice not possible (anymore), let's go on on standard copy */
1388 }
1389
1390 abort_splice:
1391 if (ic->pipe && unlikely(!ic->pipe->data)) {
1392 put_pipe(ic->pipe);
1393 ic->pipe = NULL;
1394 }
1395
1396 if (ic->pipe && ic->to_forward && !(flags & CO_RFL_BUF_FLUSH) && cs->endp->flags & CS_EP_MAY_SPLICE) {
1397 /* don't break splicing by reading, but still call rcv_buf()
1398 * to pass the flag.
1399 */
1400 goto done_recv;
1401 }
1402
1403 /* now we'll need a input buffer for the stream */
1404 if (!cs_alloc_ibuf(cs, &(__cs_strm(cs)->buffer_wait)))
1405 goto end_recv;
1406
1407 /* For an HTX stream, if the buffer is stuck (no output data with some
1408 * input data) and if the HTX message is fragmented or if its free space
1409 * wraps, we force an HTX deframentation. It is a way to have a
1410 * contiguous free space nad to let the mux to copy as much data as
1411 * possible.
1412 *
1413 * NOTE: A possible optim may be to let the mux decides if defrag is
1414 * required or not, depending on amount of data to be xferred.
1415 */
1416 if (IS_HTX_STRM(__cs_strm(cs)) && !co_data(ic)) {
1417 struct htx *htx = htxbuf(&ic->buf);
1418
1419 if (htx_is_not_empty(htx) && ((htx->flags & HTX_FL_FRAGMENTED) || htx_space_wraps(htx)))
1420 htx_defrag(htx, NULL, 0);
1421 }
1422
1423 /* Instruct the mux it must subscribed for read events */
1424 flags |= ((!conn_is_back(conn) && (__cs_strm(cs)->be->options & PR_O_ABRT_CLOSE)) ? CO_RFL_KEEP_RECV : 0);
1425
1426 /* Important note : if we're called with POLL_IN|POLL_HUP, it means the read polling
1427 * was enabled, which implies that the recv buffer was not full. So we have a guarantee
1428 * that if such an event is not handled above in splice, it will be handled here by
1429 * recv().
1430 */
1431 while ((cs->endp->flags & CS_EP_RCV_MORE) ||
1432 (!(conn->flags & CO_FL_HANDSHAKE) &&
1433 (!(cs->endp->flags & (CS_EP_ERROR|CS_EP_EOS))) && !(ic->flags & CF_SHUTR))) {
1434 int cur_flags = flags;
1435
1436 /* Compute transient CO_RFL_* flags */
1437 if (co_data(ic)) {
1438 cur_flags |= (CO_RFL_BUF_WET | CO_RFL_BUF_NOT_STUCK);
1439 }
1440
1441 /* <max> may be null. This is the mux responsibility to set
1442 * CS_EP_RCV_MORE on the CS if more space is needed.
1443 */
1444 max = channel_recv_max(ic);
1445 ret = conn->mux->rcv_buf(cs, &ic->buf, max, cur_flags);
1446
1447 if (cs->endp->flags & CS_EP_WANT_ROOM) {
1448 /* CS_EP_WANT_ROOM must not be reported if the channel's
1449 * buffer is empty.
1450 */
1451 BUG_ON(c_empty(ic));
1452
1453 cs_rx_room_blk(cs);
1454 /* Add READ_PARTIAL because some data are pending but
1455 * cannot be xferred to the channel
1456 */
1457 ic->flags |= CF_READ_PARTIAL;
1458 }
1459
1460 if (ret <= 0) {
1461 /* if we refrained from reading because we asked for a
1462 * flush to satisfy rcv_pipe(), we must not subscribe
1463 * and instead report that there's not enough room
1464 * here to proceed.
1465 */
1466 if (flags & CO_RFL_BUF_FLUSH)
1467 cs_rx_room_blk(cs);
1468 break;
1469 }
1470
1471 cur_read += ret;
1472
1473 /* if we're allowed to directly forward data, we must update ->o */
1474 if (ic->to_forward && !(ic->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
1475 unsigned long fwd = ret;
1476 if (ic->to_forward != CHN_INFINITE_FORWARD) {
1477 if (fwd > ic->to_forward)
1478 fwd = ic->to_forward;
1479 ic->to_forward -= fwd;
1480 }
1481 c_adv(ic, fwd);
1482 }
1483
1484 ic->flags |= CF_READ_PARTIAL;
1485 ic->total += ret;
1486
1487 /* End-of-input reached, we can leave. In this case, it is
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001488 * important to break the loop to not block the CS because of
Christopher Faulet5e29b762022-04-04 08:58:34 +02001489 * the channel's policies.This way, we are still able to receive
1490 * shutdowns.
1491 */
1492 if (cs->endp->flags & CS_EP_EOI)
1493 break;
1494
1495 if ((ic->flags & CF_READ_DONTWAIT) || --read_poll <= 0) {
1496 /* we're stopped by the channel's policy */
1497 cs_rx_chan_blk(cs);
1498 break;
1499 }
1500
1501 /* if too many bytes were missing from last read, it means that
1502 * it's pointless trying to read again because the system does
1503 * not have them in buffers.
1504 */
1505 if (ret < max) {
1506 /* if a streamer has read few data, it may be because we
1507 * have exhausted system buffers. It's not worth trying
1508 * again.
1509 */
1510 if (ic->flags & CF_STREAMER) {
1511 /* we're stopped by the channel's policy */
1512 cs_rx_chan_blk(cs);
1513 break;
1514 }
1515
1516 /* if we read a large block smaller than what we requested,
1517 * it's almost certain we'll never get anything more.
1518 */
1519 if (ret >= global.tune.recv_enough) {
1520 /* we're stopped by the channel's policy */
1521 cs_rx_chan_blk(cs);
1522 break;
1523 }
1524 }
1525
1526 /* if we are waiting for more space, don't try to read more data
1527 * right now.
1528 */
1529 if (cs_rx_blocked(cs))
1530 break;
1531 } /* while !flags */
1532
1533 done_recv:
1534 if (cur_read) {
1535 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) &&
1536 (cur_read <= ic->buf.size / 2)) {
1537 ic->xfer_large = 0;
1538 ic->xfer_small++;
1539 if (ic->xfer_small >= 3) {
1540 /* we have read less than half of the buffer in
1541 * one pass, and this happened at least 3 times.
1542 * This is definitely not a streamer.
1543 */
1544 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
1545 }
1546 else if (ic->xfer_small >= 2) {
1547 /* if the buffer has been at least half full twice,
1548 * we receive faster than we send, so at least it
1549 * is not a "fast streamer".
1550 */
1551 ic->flags &= ~CF_STREAMER_FAST;
1552 }
1553 }
1554 else if (!(ic->flags & CF_STREAMER_FAST) &&
1555 (cur_read >= ic->buf.size - global.tune.maxrewrite)) {
1556 /* we read a full buffer at once */
1557 ic->xfer_small = 0;
1558 ic->xfer_large++;
1559 if (ic->xfer_large >= 3) {
1560 /* we call this buffer a fast streamer if it manages
1561 * to be filled in one call 3 consecutive times.
1562 */
1563 ic->flags |= (CF_STREAMER | CF_STREAMER_FAST);
1564 }
1565 }
1566 else {
1567 ic->xfer_small = 0;
1568 ic->xfer_large = 0;
1569 }
1570 ic->last_read = now_ms;
1571 }
1572
1573 end_recv:
1574 ret = (cur_read != 0);
1575
1576 /* Report EOI on the channel if it was reached from the mux point of
1577 * view. */
1578 if ((cs->endp->flags & CS_EP_EOI) && !(ic->flags & CF_EOI)) {
1579 ic->flags |= (CF_EOI|CF_READ_PARTIAL);
1580 ret = 1;
1581 }
1582
1583 if (cs->endp->flags & CS_EP_ERROR)
1584 ret = 1;
1585 else if (cs->endp->flags & CS_EP_EOS) {
1586 /* we received a shutdown */
1587 ic->flags |= CF_READ_NULL;
1588 if (ic->flags & CF_AUTO_CLOSE)
1589 channel_shutw_now(ic);
1590 cs_conn_read0(cs);
1591 ret = 1;
1592 }
1593 else if (!cs_rx_blocked(cs)) {
1594 /* Subscribe to receive events if we're blocking on I/O */
1595 conn->mux->subscribe(cs, SUB_RETRY_RECV, &cs->wait_event);
1596 cs_rx_endp_done(cs);
1597 } else {
1598 cs_rx_endp_more(cs);
1599 ret = 1;
1600 }
1601 return ret;
1602}
1603
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001604/* This tries to perform a synchronous receive on the conn-stream to
Christopher Faulet5e29b762022-04-04 08:58:34 +02001605 * try to collect last arrived data. In practice it's only implemented on
1606 * conn_streams. Returns 0 if nothing was done, non-zero if new data or a
1607 * shutdown were collected. This may result on some delayed receive calls
1608 * to be programmed and performed later, though it doesn't provide any
1609 * such guarantee.
1610 */
1611int cs_conn_sync_recv(struct conn_stream *cs)
1612{
1613 if (!cs_state_in(cs->state, CS_SB_RDY|CS_SB_EST))
1614 return 0;
1615
1616 if (!cs_conn_mux(cs))
1617 return 0; // only conn_streams are supported
1618
1619 if (cs->wait_event.events & SUB_RETRY_RECV)
1620 return 0; // already subscribed
1621
1622 if (!cs_rx_endp_ready(cs) || cs_rx_blocked(cs))
1623 return 0; // already failed
1624
1625 return cs_conn_recv(cs);
1626}
1627
1628/*
1629 * This function is called to send buffer data to a stream socket.
1630 * It calls the mux layer's snd_buf function. It relies on the
1631 * caller to commit polling changes. The caller should check conn->flags
1632 * for errors.
1633 */
1634static int cs_conn_send(struct conn_stream *cs)
1635{
1636 struct connection *conn = __cs_conn(cs);
1637 struct stream *s = __cs_strm(cs);
1638 struct channel *oc = cs_oc(cs);
1639 int ret;
1640 int did_send = 0;
1641
1642 if (cs->endp->flags & (CS_EP_ERROR|CS_EP_ERR_PENDING) || cs_is_conn_error(cs)) {
1643 /* We're probably there because the tasklet was woken up,
1644 * but process_stream() ran before, detected there were an
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001645 * error and put the CS back to CS_ST_TAR. There's still
Christopher Faulet5e29b762022-04-04 08:58:34 +02001646 * CO_FL_ERROR on the connection but we don't want to add
1647 * CS_EP_ERROR back, so give up
1648 */
1649 if (cs->state < CS_ST_CON)
1650 return 0;
1651 cs->endp->flags |= CS_EP_ERROR;
1652 return 1;
1653 }
1654
1655 /* We're already waiting to be able to send, give up */
1656 if (cs->wait_event.events & SUB_RETRY_SEND)
1657 return 0;
1658
1659 /* we might have been called just after an asynchronous shutw */
1660 if (oc->flags & CF_SHUTW)
1661 return 1;
1662
1663 /* we must wait because the mux is not installed yet */
1664 if (!conn->mux)
1665 return 0;
1666
1667 if (oc->pipe && conn->xprt->snd_pipe && conn->mux->snd_pipe) {
1668 ret = conn->mux->snd_pipe(cs, oc->pipe);
1669 if (ret > 0)
1670 did_send = 1;
1671
1672 if (!oc->pipe->data) {
1673 put_pipe(oc->pipe);
1674 oc->pipe = NULL;
1675 }
1676
1677 if (oc->pipe)
1678 goto end;
1679 }
1680
1681 /* At this point, the pipe is empty, but we may still have data pending
1682 * in the normal buffer.
1683 */
1684 if (co_data(oc)) {
1685 /* when we're here, we already know that there is no spliced
1686 * data left, and that there are sendable buffered data.
1687 */
1688
1689 /* check if we want to inform the kernel that we're interested in
1690 * sending more data after this call. We want this if :
1691 * - we're about to close after this last send and want to merge
1692 * the ongoing FIN with the last segment.
1693 * - we know we can't send everything at once and must get back
1694 * here because of unaligned data
1695 * - there is still a finite amount of data to forward
1696 * The test is arranged so that the most common case does only 2
1697 * tests.
1698 */
1699 unsigned int send_flag = 0;
1700
1701 if ((!(oc->flags & (CF_NEVER_WAIT|CF_SEND_DONTWAIT)) &&
1702 ((oc->to_forward && oc->to_forward != CHN_INFINITE_FORWARD) ||
1703 (oc->flags & CF_EXPECT_MORE) ||
1704 (IS_HTX_STRM(s) &&
1705 (!(oc->flags & (CF_EOI|CF_SHUTR)) && htx_expect_more(htxbuf(&oc->buf)))))) ||
1706 ((oc->flags & CF_ISRESP) &&
1707 ((oc->flags & (CF_AUTO_CLOSE|CF_SHUTW_NOW)) == (CF_AUTO_CLOSE|CF_SHUTW_NOW))))
1708 send_flag |= CO_SFL_MSG_MORE;
1709
1710 if (oc->flags & CF_STREAMER)
1711 send_flag |= CO_SFL_STREAMER;
1712
1713 if (s->txn && s->txn->flags & TX_L7_RETRY && !b_data(&s->txn->l7_buffer)) {
1714 /* If we want to be able to do L7 retries, copy
1715 * the data we're about to send, so that we are able
1716 * to resend them if needed
1717 */
1718 /* Try to allocate a buffer if we had none.
1719 * If it fails, the next test will just
1720 * disable the l7 retries by setting
1721 * l7_conn_retries to 0.
1722 */
1723 if (s->txn->req.msg_state != HTTP_MSG_DONE)
1724 s->txn->flags &= ~TX_L7_RETRY;
1725 else {
1726 if (b_alloc(&s->txn->l7_buffer) == NULL)
1727 s->txn->flags &= ~TX_L7_RETRY;
1728 else {
1729 memcpy(b_orig(&s->txn->l7_buffer),
1730 b_orig(&oc->buf),
1731 b_size(&oc->buf));
1732 s->txn->l7_buffer.head = co_data(oc);
1733 b_add(&s->txn->l7_buffer, co_data(oc));
1734 }
1735
1736 }
1737 }
1738
1739 ret = conn->mux->snd_buf(cs, &oc->buf, co_data(oc), send_flag);
1740 if (ret > 0) {
1741 did_send = 1;
1742 c_rew(oc, ret);
1743 c_realign_if_empty(oc);
1744
1745 if (!co_data(oc)) {
1746 /* Always clear both flags once everything has been sent, they're one-shot */
1747 oc->flags &= ~(CF_EXPECT_MORE | CF_SEND_DONTWAIT);
1748 }
1749 /* if some data remain in the buffer, it's only because the
1750 * system buffers are full, we will try next time.
1751 */
Christopher Faulet13045f02022-04-01 14:23:38 +02001752 }
1753 }
Christopher Faulet5e29b762022-04-04 08:58:34 +02001754
1755 end:
1756 if (did_send) {
1757 oc->flags |= CF_WRITE_PARTIAL | CF_WROTE_DATA;
1758 if (cs->state == CS_ST_CON)
1759 cs->state = CS_ST_RDY;
1760
1761 cs_rx_room_rdy(cs_opposite(cs));
1762 }
1763
1764 if (cs->endp->flags & (CS_EP_ERROR|CS_EP_ERR_PENDING)) {
1765 cs->endp->flags |= CS_EP_ERROR;
1766 return 1;
1767 }
1768
1769 /* We couldn't send all of our data, let the mux know we'd like to send more */
1770 if (!channel_is_empty(oc))
1771 conn->mux->subscribe(cs, SUB_RETRY_SEND, &cs->wait_event);
1772 return did_send;
1773}
1774
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001775/* perform a synchronous send() for the conn-stream. The CF_WRITE_NULL and
Christopher Faulet5e29b762022-04-04 08:58:34 +02001776 * CF_WRITE_PARTIAL flags are cleared prior to the attempt, and will possibly
1777 * be updated in case of success.
1778 */
1779void cs_conn_sync_send(struct conn_stream *cs)
1780{
1781 struct channel *oc = cs_oc(cs);
1782
1783 oc->flags &= ~(CF_WRITE_NULL|CF_WRITE_PARTIAL);
1784
1785 if (oc->flags & CF_SHUTW)
1786 return;
1787
1788 if (channel_is_empty(oc))
1789 return;
1790
1791 if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
1792 return;
1793
1794 if (!cs_conn_mux(cs))
1795 return;
1796
1797 cs_conn_send(cs);
1798}
1799
1800/* Called by I/O handlers after completion.. It propagates
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001801 * connection flags to the conn-stream, updates the stream (which may or
Christopher Faulet5e29b762022-04-04 08:58:34 +02001802 * may not take this opportunity to try to forward data), then update the
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001803 * connection's polling based on the channels and conn-stream's final
Christopher Faulet5e29b762022-04-04 08:58:34 +02001804 * states. The function always returns 0.
1805 */
1806static int cs_conn_process(struct conn_stream *cs)
1807{
1808 struct connection *conn = __cs_conn(cs);
1809 struct channel *ic = cs_ic(cs);
1810 struct channel *oc = cs_oc(cs);
1811
1812 BUG_ON(!conn);
1813
1814 /* If we have data to send, try it now */
1815 if (!channel_is_empty(oc) && !(cs->wait_event.events & SUB_RETRY_SEND))
1816 cs_conn_send(cs);
1817
1818 /* First step, report to the conn-stream what was detected at the
1819 * connection layer : errors and connection establishment.
1820 * Only add CS_EP_ERROR if we're connected, or we're attempting to
1821 * connect, we may get there because we got woken up, but only run
1822 * after process_stream() noticed there were an error, and decided
1823 * to retry to connect, the connection may still have CO_FL_ERROR,
1824 * and we don't want to add CS_EP_ERROR back
1825 *
1826 * Note: This test is only required because cs_conn_process is also the SI
1827 * wake callback. Otherwise cs_conn_recv()/cs_conn_send() already take
1828 * care of it.
1829 */
1830
1831 if (cs->state >= CS_ST_CON) {
1832 if (cs_is_conn_error(cs))
1833 cs->endp->flags |= CS_EP_ERROR;
1834 }
1835
1836 /* If we had early data, and the handshake ended, then
1837 * we can remove the flag, and attempt to wake the task up,
1838 * in the event there's an analyser waiting for the end of
1839 * the handshake.
1840 */
1841 if (!(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS)) &&
1842 (cs->endp->flags & CS_EP_WAIT_FOR_HS)) {
1843 cs->endp->flags &= ~CS_EP_WAIT_FOR_HS;
1844 task_wakeup(cs_strm_task(cs), TASK_WOKEN_MSG);
1845 }
1846
1847 if (!cs_state_in(cs->state, CS_SB_EST|CS_SB_DIS|CS_SB_CLO) &&
1848 (conn->flags & CO_FL_WAIT_XPRT) == 0) {
1849 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
1850 oc->flags |= CF_WRITE_NULL;
1851 if (cs->state == CS_ST_CON)
1852 cs->state = CS_ST_RDY;
1853 }
1854
1855 /* Report EOS on the channel if it was reached from the mux point of
1856 * view.
1857 *
1858 * Note: This test is only required because cs_conn_process is also the SI
1859 * wake callback. Otherwise cs_conn_recv()/cs_conn_send() already take
1860 * care of it.
1861 */
1862 if (cs->endp->flags & CS_EP_EOS && !(ic->flags & CF_SHUTR)) {
1863 /* we received a shutdown */
1864 ic->flags |= CF_READ_NULL;
1865 if (ic->flags & CF_AUTO_CLOSE)
1866 channel_shutw_now(ic);
1867 cs_conn_read0(cs);
1868 }
1869
1870 /* Report EOI on the channel if it was reached from the mux point of
1871 * view.
1872 *
1873 * Note: This test is only required because cs_conn_process is also the SI
1874 * wake callback. Otherwise cs_conn_recv()/cs_conn_send() already take
1875 * care of it.
1876 */
1877 if ((cs->endp->flags & CS_EP_EOI) && !(ic->flags & CF_EOI))
1878 ic->flags |= (CF_EOI|CF_READ_PARTIAL);
1879
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001880 /* Second step : update the conn-stream and channels, try to forward any
Christopher Faulet5e29b762022-04-04 08:58:34 +02001881 * pending data, then possibly wake the stream up based on the new
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001882 * conn-stream status.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001883 */
1884 cs_notify(cs);
1885 stream_release_buffers(__cs_strm(cs));
1886 return 0;
1887}
1888
1889/* This is the ->process() function for any conn-stream's wait_event task.
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001890 * It's assigned during the conn-stream's initialization, for any type of
1891 * conn-stream. Thus it is always safe to perform a tasklet_wakeup() on a
1892 * conn-stream, as the presence of the CS is checked there.
Christopher Faulet5e29b762022-04-04 08:58:34 +02001893 */
1894struct task *cs_conn_io_cb(struct task *t, void *ctx, unsigned int state)
1895{
1896 struct conn_stream *cs = ctx;
1897 int ret = 0;
1898
1899 if (!cs_conn(cs))
1900 return t;
1901
1902 if (!(cs->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(cs_oc(cs)))
1903 ret = cs_conn_send(cs);
1904 if (!(cs->wait_event.events & SUB_RETRY_RECV))
1905 ret |= cs_conn_recv(cs);
1906 if (ret != 0)
1907 cs_conn_process(cs);
1908
1909 stream_release_buffers(__cs_strm(cs));
1910 return t;
1911}
1912
1913/* Callback to be used by applet handlers upon completion. It updates the stream
1914 * (which may or may not take this opportunity to try to forward data), then
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001915 * may re-enable the applet's based on the channels and conn-stream's final
Christopher Faulet5e29b762022-04-04 08:58:34 +02001916 * states.
1917 */
1918static int cs_applet_process(struct conn_stream *cs)
1919{
1920 struct channel *ic = cs_ic(cs);
1921
1922 BUG_ON(!cs_appctx(cs));
1923
1924 /* If the applet wants to write and the channel is closed, it's a
1925 * broken pipe and it must be reported.
1926 */
1927 if (!(cs->endp->flags & CS_EP_RX_WAIT_EP) && (ic->flags & CF_SHUTR))
1928 cs->endp->flags |= CS_EP_ERROR;
1929
1930 /* automatically mark the applet having data available if it reported
1931 * begin blocked by the channel.
1932 */
1933 if (cs_rx_blocked(cs))
1934 cs_rx_endp_more(cs);
1935
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +02001936 /* update the conn-stream, channels, and possibly wake the stream up */
Christopher Faulet5e29b762022-04-04 08:58:34 +02001937 cs_notify(cs);
1938 stream_release_buffers(__cs_strm(cs));
1939
1940 /* cs_notify may have passed through chk_snd and released some
1941 * RXBLK flags. Process_stream will consider those flags to wake up the
1942 * appctx but in the case the task is not in runqueue we may have to
1943 * wakeup the appctx immediately.
1944 */
1945 if ((cs_rx_endp_ready(cs) && !cs_rx_blocked(cs)) ||
1946 (cs_tx_endp_ready(cs) && !cs_tx_blocked(cs)))
1947 appctx_wakeup(__cs_appctx(cs));
1948 return 0;
Christopher Faulet13045f02022-04-01 14:23:38 +02001949}