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