blob: c4dd8ab8db63482eda74c609074f578067f4ffab [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 Fauletcda94ac2021-12-23 17:28:17 +010022#include <haproxy/stream_interface.h>
Christopher Faulet1329f2a2021-12-16 17:32:56 +010023
24DECLARE_POOL(pool_head_connstream, "conn_stream", sizeof(struct conn_stream));
Christopher Fauletdb90f2a2022-03-22 16:06:25 +010025DECLARE_POOL(pool_head_cs_endpoint, "cs_endpoint", sizeof(struct cs_endpoint));
Christopher Faulet1329f2a2021-12-16 17:32:56 +010026
Christopher Faulet9ffddd52022-04-01 14:04:29 +020027/* functions used by default on a detached conn-stream */
28static void cs_app_shutr(struct conn_stream *cs);
29static void cs_app_shutw(struct conn_stream *cs);
30static void cs_app_chk_rcv(struct conn_stream *cs);
31static void cs_app_chk_snd(struct conn_stream *cs);
32
33/* functions used on a mux-based conn-stream */
34static void cs_app_shutr_conn(struct conn_stream *cs);
35static void cs_app_shutw_conn(struct conn_stream *cs);
36static void cs_app_chk_rcv_conn(struct conn_stream *cs);
37static void cs_app_chk_snd_conn(struct conn_stream *cs);
38
39/* functions used on an applet-based conn-stream */
40static void cs_app_shutr_applet(struct conn_stream *cs);
41static void cs_app_shutw_applet(struct conn_stream *cs);
42static void cs_app_chk_rcv_applet(struct conn_stream *cs);
43static void cs_app_chk_snd_applet(struct conn_stream *cs);
44
45/* conn-stream operations for connections */
46struct cs_app_ops cs_app_conn_ops = {
47 .chk_rcv = cs_app_chk_rcv_conn,
48 .chk_snd = cs_app_chk_snd_conn,
49 .shutr = cs_app_shutr_conn,
50 .shutw = cs_app_shutw_conn,
51};
52
53/* conn-stream operations for embedded tasks */
54struct cs_app_ops cs_app_embedded_ops = {
55 .chk_rcv = cs_app_chk_rcv,
56 .chk_snd = cs_app_chk_snd,
57 .shutr = cs_app_shutr,
58 .shutw = cs_app_shutw,
59};
60
61/* conn-stream operations for connections */
62struct cs_app_ops cs_app_applet_ops = {
63 .chk_rcv = cs_app_chk_rcv_applet,
64 .chk_snd = cs_app_chk_snd_applet,
65 .shutr = cs_app_shutr_applet,
66 .shutw = cs_app_shutw_applet,
67};
68
Christopher Faulet5e29b762022-04-04 08:58:34 +020069static int cs_conn_process(struct conn_stream *cs);
70static int cs_conn_recv(struct conn_stream *cs);
71static int cs_conn_send(struct conn_stream *cs);
72static int cs_applet_process(struct conn_stream *cs);
73
74struct data_cb cs_data_conn_cb = {
75 .wake = cs_conn_process,
76 .name = "STRM",
77};
78
79struct data_cb cs_data_applet_cb = {
80 .wake = cs_applet_process,
81 .name = "STRM",
82};
83
84
Christopher Faulet9ffddd52022-04-01 14:04:29 +020085
Christopher Fauletdb90f2a2022-03-22 16:06:25 +010086void cs_endpoint_init(struct cs_endpoint *endp)
87{
88 endp->target = NULL;
89 endp->ctx = NULL;
90 endp->flags = CS_EP_NONE;
91}
92
93struct cs_endpoint *cs_endpoint_new()
94{
95 struct cs_endpoint *endp;
96
97 endp = pool_alloc(pool_head_cs_endpoint);
98 if (unlikely(!endp))
99 return NULL;
100
101 cs_endpoint_init(endp);
102 return endp;
103}
104
105void cs_endpoint_free(struct cs_endpoint *endp)
106{
107 pool_free(pool_head_cs_endpoint, endp);
108}
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100109
Christopher Fauletdd2d0d82021-12-20 09:34:32 +0100110/* Tries to allocate a new conn_stream and initialize its main fields. On
111 * failure, nothing is allocated and NULL is returned.
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100112 */
Christopher Fauletb669d682022-03-22 18:37:19 +0100113struct conn_stream *cs_new(struct cs_endpoint *endp)
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100114{
115 struct conn_stream *cs;
116
117 cs = pool_alloc(pool_head_connstream);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100118
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100119 if (unlikely(!cs))
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100120 goto alloc_error;
Christopher Fauletbb772d02022-03-22 15:28:36 +0100121
122 cs->obj_type = OBJ_TYPE_CS;
123 cs->flags = CS_FL_NONE;
Christopher Faulet62e75742022-03-31 09:16:34 +0200124 cs->state = CS_ST_INI;
Christopher Faulet1d987772022-03-29 18:03:35 +0200125 cs->hcto = TICK_ETERNITY;
Christopher Fauletbb772d02022-03-22 15:28:36 +0100126 cs->app = NULL;
Christopher Fauletbb772d02022-03-22 15:28:36 +0100127 cs->si = NULL;
128 cs->data_cb = NULL;
Christopher Faulet8da67aa2022-03-29 17:53:09 +0200129 cs->src = NULL;
130 cs->dst = NULL;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200131 cs->wait_event.tasklet = NULL;
132 cs->wait_event.events = 0;
133
Christopher Fauletb669d682022-03-22 18:37:19 +0100134 if (!endp) {
135 endp = cs_endpoint_new();
136 if (unlikely(!endp))
137 goto alloc_error;
138 }
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100139 cs->endp = endp;
140
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100141 return cs;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100142
143 alloc_error:
144 pool_free(pool_head_connstream, cs);
145 return NULL;
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100146}
147
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100148struct conn_stream *cs_new_from_mux(struct cs_endpoint *endp, struct session *sess, struct buffer *input)
149{
150 struct conn_stream *cs;
151
152 cs = cs_new(endp);
153 if (unlikely(!cs))
154 return NULL;
155 if (unlikely(!stream_new(sess, cs, input))) {
156 pool_free(pool_head_connstream, cs);
157 cs = NULL;
158 }
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100159 endp->flags &= ~CS_EP_ORPHAN;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100160 return cs;
161}
162
163struct conn_stream *cs_new_from_applet(struct cs_endpoint *endp, struct session *sess, struct buffer *input)
164{
165 struct conn_stream *cs;
166 struct appctx *appctx = endp->ctx;
167
168 cs = cs_new(endp);
169 if (unlikely(!cs))
170 return NULL;
171 appctx->owner = cs;
172 if (unlikely(!stream_new(sess, cs, input))) {
173 pool_free(pool_head_connstream, cs);
174 cs = NULL;
175 }
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100176 endp->flags &= ~CS_EP_ORPHAN;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100177 return cs;
178}
179
180struct conn_stream *cs_new_from_strm(struct stream *strm, unsigned int flags)
181{
182 struct conn_stream *cs;
183
184 cs = cs_new(NULL);
185 if (unlikely(!cs))
186 return NULL;
187 cs->flags |= flags;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100188 cs->endp->flags |= CS_EP_DETACHED;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100189 cs->si = si_new(cs);
190 if (unlikely(!cs->si)) {
191 cs_free(cs);
192 return NULL;
193 }
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200194
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100195 cs->app = &strm->obj_type;
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200196 cs->ops = &cs_app_embedded_ops;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100197 cs->data_cb = NULL;
198 return cs;
199}
200
201struct conn_stream *cs_new_from_check(struct check *check, unsigned int flags)
202{
203 struct conn_stream *cs;
204
205 cs = cs_new(NULL);
206 if (unlikely(!cs))
207 return NULL;
208 cs->flags |= flags;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100209 cs->endp->flags |= CS_EP_DETACHED;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100210 cs->app = &check->obj_type;
211 cs->data_cb = &check_conn_cb;
212 return cs;
213}
214
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100215/* Releases a conn_stream previously allocated by cs_new(), as well as any
216 * buffer it would still hold.
217 */
218void cs_free(struct conn_stream *cs)
219{
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100220 si_free(cs->si);
Christopher Faulet8da67aa2022-03-29 17:53:09 +0200221 sockaddr_free(&cs->src);
222 sockaddr_free(&cs->dst);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100223 if (cs->endp) {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100224 BUG_ON(!(cs->endp->flags & CS_EP_DETACHED));
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100225 cs_endpoint_free(cs->endp);
226 }
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200227 if (cs->wait_event.tasklet)
228 tasklet_free(cs->wait_event.tasklet);
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100229 pool_free(pool_head_connstream, cs);
230}
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100231
232
Christopher Faulet93882042022-01-19 14:56:50 +0100233/* Attaches a conn_stream to an mux endpoint and sets the endpoint ctx */
Christopher Faulet070b91b2022-03-31 19:27:18 +0200234int cs_attach_mux(struct conn_stream *cs, void *target, void *ctx)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100235{
Christopher Faulet93882042022-01-19 14:56:50 +0100236 struct connection *conn = ctx;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100237
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100238 cs->endp->target = target;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100239 cs->endp->ctx = ctx;
240 cs->endp->flags |= CS_EP_T_MUX;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100241 cs->endp->flags &= ~CS_EP_DETACHED;
Christopher Faulet93882042022-01-19 14:56:50 +0100242 if (!conn->ctx)
243 conn->ctx = cs;
244 if (cs_strm(cs)) {
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200245 if (!cs->wait_event.tasklet) {
246 cs->wait_event.tasklet = tasklet_new();
247 if (!cs->wait_event.tasklet)
248 return -1;
Christopher Faulet4a7764a2022-04-01 16:58:52 +0200249 cs->wait_event.tasklet->process = cs_conn_io_cb;
250 cs->wait_event.tasklet->context = cs;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200251 cs->wait_event.events = 0;
252 }
253
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200254 cs->ops = &cs_app_conn_ops;
Christopher Faulet000ba3e2022-04-01 17:06:32 +0200255 cs->data_cb = &cs_data_conn_cb;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100256 }
Christopher Faulet93882042022-01-19 14:56:50 +0100257 else if (cs_check(cs))
258 cs->data_cb = &check_conn_cb;
Christopher Faulet070b91b2022-03-31 19:27:18 +0200259 return 0;
Christopher Faulet93882042022-01-19 14:56:50 +0100260}
261
262/* Attaches a conn_stream to an applet endpoint and sets the endpoint ctx */
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100263void cs_attach_applet(struct conn_stream *cs, void *target, void *ctx)
Christopher Faulet93882042022-01-19 14:56:50 +0100264{
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100265 struct appctx *appctx = target;
Christopher Faulet93882042022-01-19 14:56:50 +0100266
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100267 cs->endp->target = target;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100268 cs->endp->ctx = ctx;
269 cs->endp->flags |= CS_EP_T_APPLET;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100270 cs->endp->flags &= ~CS_EP_DETACHED;
Christopher Faulet93882042022-01-19 14:56:50 +0100271 appctx->owner = cs;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100272 if (cs_strm(cs)) {
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200273 cs->ops = &cs_app_applet_ops;
Christopher Faulet6059ba42022-04-01 16:34:53 +0200274 cs->data_cb = &cs_data_applet_cb;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100275 }
276}
277
278/* Attaches a conn_stream to a app layer and sets the relevant callbacks */
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100279int cs_attach_strm(struct conn_stream *cs, struct stream *strm)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100280{
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100281 cs->app = &strm->obj_type;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100282
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100283 cs->si = si_new(cs);
284 if (unlikely(!cs->si))
285 return -1;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200286
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100287 cs->endp->flags &= ~CS_EP_ORPHAN;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100288 if (cs->endp->flags & CS_EP_T_MUX) {
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200289 cs->wait_event.tasklet = tasklet_new();
290 if (!cs->wait_event.tasklet) {
291 si_free(cs->si);
292 cs->si = NULL;
293 return -1;
294 }
Christopher Faulet4a7764a2022-04-01 16:58:52 +0200295 cs->wait_event.tasklet->process = cs_conn_io_cb;
296 cs->wait_event.tasklet->context = cs;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200297 cs->wait_event.events = 0;
298
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200299 cs->ops = &cs_app_conn_ops;
Christopher Faulet000ba3e2022-04-01 17:06:32 +0200300 cs->data_cb = &cs_data_conn_cb;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100301 }
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100302 else if (cs->endp->flags & CS_EP_T_APPLET) {
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200303 cs->ops = &cs_app_applet_ops;
Christopher Faulet6059ba42022-04-01 16:34:53 +0200304 cs->data_cb = &cs_data_applet_cb;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100305 }
306 else {
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200307 cs->ops = &cs_app_embedded_ops;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100308 cs->data_cb = NULL;
309 }
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100310 return 0;
311}
312
313/* Detach the conn_stream from the endpoint, if any. For a connecrion, if a mux
314 * owns the connection ->detach() callback is called. Otherwise, it means the
315 * conn-stream owns the connection. In this case the connection is closed and
316 * released. For an applet, the appctx is released. At the end, the conn-stream
317 * is not released but some fields a reset.
318 */
319void cs_detach_endp(struct conn_stream *cs)
320{
Christopher Fauletb041b232022-03-24 10:27:02 +0100321 if (!cs->endp)
322 goto reset_cs;
323
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100324 if (cs->endp->flags & CS_EP_T_MUX) {
325 struct connection *conn = cs_conn(cs);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100326
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100327 if (conn->mux) {
Christopher Faulet54e85cb2022-01-06 08:46:56 +0100328 /* TODO: handle unsubscribe for healthchecks too */
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100329 cs->endp->flags |= CS_EP_ORPHAN;
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200330 if (cs->wait_event.events != 0)
331 conn->mux->unsubscribe(cs, cs->wait_event.events, &cs->wait_event);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100332 conn->mux->detach(cs);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100333 cs->endp = NULL;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100334 }
335 else {
336 /* It's too early to have a mux, let's just destroy
337 * the connection
338 */
339 conn_stop_tracking(conn);
340 conn_full_close(conn);
341 if (conn->destroy_cb)
342 conn->destroy_cb(conn);
343 conn_free(conn);
344 }
345 }
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100346 else if (cs->endp->flags & CS_EP_T_APPLET) {
347 struct appctx *appctx = cs_appctx(cs);
348
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100349 cs->endp->flags |= CS_EP_ORPHAN;
Christopher Faulet37046632022-04-01 11:36:58 +0200350 cs_applet_release(cs);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100351 appctx_free(appctx);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100352 cs->endp = NULL;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100353 }
354
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100355 if (cs->endp) {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100356 /* the cs is the only one one the endpoint */
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100357 cs_endpoint_init(cs->endp);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100358 cs->endp->flags |= CS_EP_DETACHED;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100359 }
360
Christopher Fauletb041b232022-03-24 10:27:02 +0100361 reset_cs:
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100362 /* FIXME: Rest CS for now but must be reviewed. CS flags are only
363 * connection related for now but this will evolved
364 */
Christopher Faulet30995112022-03-25 15:32:38 +0100365 cs->flags &= CS_FL_ISBACK;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100366 if (cs->si)
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200367 cs->ops = &cs_app_embedded_ops;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100368 cs->data_cb = NULL;
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100369
370 if (cs->app == NULL)
371 cs_free(cs);
372}
373
374void cs_detach_app(struct conn_stream *cs)
375{
376 si_free(cs->si);
377 cs->app = NULL;
378 cs->si = NULL;
379 cs->data_cb = NULL;
Christopher Faulet8da67aa2022-03-29 17:53:09 +0200380 sockaddr_free(&cs->src);
381 sockaddr_free(&cs->dst);
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200382
383 if (cs->wait_event.tasklet)
384 tasklet_free(cs->wait_event.tasklet);
385 cs->wait_event.tasklet = NULL;
386 cs->wait_event.events = 0;
387
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100388 if (!cs->endp || (cs->endp->flags & CS_EP_DETACHED))
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100389 cs_free(cs);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100390}
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100391
392int cs_reset_endp(struct conn_stream *cs)
393{
Christopher Fauletb041b232022-03-24 10:27:02 +0100394 struct cs_endpoint *new_endp;
395
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100396 BUG_ON(!cs->app);
Christopher Fauletb041b232022-03-24 10:27:02 +0100397 if (!__cs_endp_target(cs)) {
398 /* endpoint not attached or attached to a mux with no
399 * target. Thus the endpoint will not be release but just
400 * reset
401 */
402 cs_detach_endp(cs);
403 return 0;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100404 }
Christopher Fauletb041b232022-03-24 10:27:02 +0100405
406 /* allocate the new endpoint first to be able to set error if it
407 * fails */
408 new_endp = cs_endpoint_new();
409 if (!unlikely(new_endp)) {
410 cs->endp->flags |= CS_EP_ERROR;
411 return -1;
412 }
413
414 cs_detach_endp(cs);
415 BUG_ON(cs->endp);
416 cs->endp = new_endp;
417 cs->endp->flags |= CS_EP_DETACHED;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100418 return 0;
419}
Christopher Faulet37046632022-04-01 11:36:58 +0200420
421
422/* Register an applet to handle a conn-stream as a new appctx. The CS will
423 * wake it up every time it is solicited. The appctx must be deleted by the task
424 * handler using cs_detach_endp(), possibly from within the function itself.
425 * It also pre-initializes the applet's context and returns it (or NULL in case
426 * it could not be allocated).
427 */
428struct appctx *cs_register_applet(struct conn_stream *cs, struct applet *app)
429{
430 struct appctx *appctx;
431
432 DPRINTF(stderr, "registering handler %p for cs %p (was %p)\n", app, cs, cs_strm_task(cs));
433
434 appctx = appctx_new(app, cs->endp);
435 if (!appctx)
436 return NULL;
437 cs_attach_applet(cs, appctx, appctx);
438 appctx->owner = cs;
439 appctx->t->nice = __cs_strm(cs)->task->nice;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200440 cs_cant_get(cs);
Christopher Faulet37046632022-04-01 11:36:58 +0200441 appctx_wakeup(appctx);
442 return appctx;
443}
444
445/* call the applet's release function if any. Needs to be called upon close() */
446void cs_applet_release(struct conn_stream *cs)
447{
448 struct appctx *appctx = __cs_appctx(cs);
449
450 if (appctx->applet->release && !cs_state_in(cs->state, CS_SB_DIS|CS_SB_CLO))
451 appctx->applet->release(appctx);
452}
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200453
454/*
455 * This function performs a shutdown-read on a detached conn-stream in a
456 * connected or init state (it does nothing for other states). It either shuts
457 * the read side or marks itself as closed. The buffer flags are updated to
458 * reflect the new state. If the stream interface has CS_FL_NOHALF, we also
459 * forward the close to the write side. The owner task is woken up if it exists.
460 */
461static void cs_app_shutr(struct conn_stream *cs)
462{
463 struct channel *ic = cs_ic(cs);
464
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200465 cs_rx_shut_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200466 if (ic->flags & CF_SHUTR)
467 return;
468 ic->flags |= CF_SHUTR;
469 ic->rex = TICK_ETERNITY;
470
471 if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
472 return;
473
474 if (cs_oc(cs)->flags & CF_SHUTW) {
475 cs->state = CS_ST_DIS;
476 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
477 }
478 else if (cs->flags & CS_FL_NOHALF) {
479 /* we want to immediately forward this close to the write side */
480 return cs_app_shutw(cs);
481 }
482
483 /* note that if the task exists, it must unregister itself once it runs */
484 if (!(cs->flags & CS_FL_DONT_WAKE))
485 task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO);
486}
487
488/*
489 * This function performs a shutdown-write on a detached conn-stream in a
490 * connected or init state (it does nothing for other states). It either shuts
491 * the write side or marks itself as closed. The buffer flags are updated to
492 * reflect the new state. It does also close everything if the SI was marked as
493 * being in error state. The owner task is woken up if it exists.
494 */
495static void cs_app_shutw(struct conn_stream *cs)
496{
497 struct channel *ic = cs_ic(cs);
498 struct channel *oc = cs_oc(cs);
499
500 oc->flags &= ~CF_SHUTW_NOW;
501 if (oc->flags & CF_SHUTW)
502 return;
503 oc->flags |= CF_SHUTW;
504 oc->wex = TICK_ETERNITY;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200505 cs_done_get(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200506
507 if (tick_isset(cs->hcto)) {
508 ic->rto = cs->hcto;
509 ic->rex = tick_add(now_ms, ic->rto);
510 }
511
512 switch (cs->state) {
513 case CS_ST_RDY:
514 case CS_ST_EST:
515 /* we have to shut before closing, otherwise some short messages
516 * may never leave the system, especially when there are remaining
517 * unread data in the socket input buffer, or when nolinger is set.
518 * However, if CS_FL_NOLINGER is explicitly set, we know there is
519 * no risk so we close both sides immediately.
520 */
521 if (!(cs->endp->flags & CS_EP_ERROR) && !(cs->flags & CS_FL_NOLINGER) &&
522 !(ic->flags & (CF_SHUTR|CF_DONT_READ)))
523 return;
524
525 /* fall through */
526 case CS_ST_CON:
527 case CS_ST_CER:
528 case CS_ST_QUE:
529 case CS_ST_TAR:
530 /* Note that none of these states may happen with applets */
531 cs->state = CS_ST_DIS;
532 /* fall through */
533 default:
534 cs->flags &= ~CS_FL_NOLINGER;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200535 cs_rx_shut_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200536 ic->flags |= CF_SHUTR;
537 ic->rex = TICK_ETERNITY;
538 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
539 }
540
541 /* note that if the task exists, it must unregister itself once it runs */
542 if (!(cs->flags & CS_FL_DONT_WAKE))
543 task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO);
544}
545
546/* default chk_rcv function for scheduled tasks */
547static void cs_app_chk_rcv(struct conn_stream *cs)
548{
549 struct channel *ic = cs_ic(cs);
550
551 DPRINTF(stderr, "%s: cs=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n",
552 __FUNCTION__,
553 cs, cs->state, ic->flags, cs_oc(cs)->flags);
554
555 if (ic->pipe) {
556 /* stop reading */
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200557 cs_rx_room_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200558 }
559 else {
560 /* (re)start reading */
561 if (!(cs->flags & CS_FL_DONT_WAKE))
562 task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO);
563 }
564}
565
566/* default chk_snd function for scheduled tasks */
567static void cs_app_chk_snd(struct conn_stream *cs)
568{
569 struct channel *oc = cs_oc(cs);
570
571 DPRINTF(stderr, "%s: cs=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n",
572 __FUNCTION__,
573 cs, cs->state, cs_ic(cs)->flags, oc->flags);
574
575 if (unlikely(cs->state != CS_ST_EST || (oc->flags & CF_SHUTW)))
576 return;
577
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200578 if (!(cs->endp->flags & CS_EP_WAIT_DATA) || /* not waiting for data */
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200579 channel_is_empty(oc)) /* called with nothing to send ! */
580 return;
581
582 /* Otherwise there are remaining data to be sent in the buffer,
583 * so we tell the handler.
584 */
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200585 cs->endp->flags &= ~CS_EP_WAIT_DATA;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200586 if (!tick_isset(oc->wex))
587 oc->wex = tick_add_ifset(now_ms, oc->wto);
588
589 if (!(cs->flags & CS_FL_DONT_WAKE))
590 task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO);
591}
592
593/*
594 * This function performs a shutdown-read on a conn-stream attached to
595 * a connection in a connected or init state (it does nothing for other
596 * states). It either shuts the read side or marks itself as closed. The buffer
597 * flags are updated to reflect the new state. If the stream interface has
598 * CS_FL_NOHALF, we also forward the close to the write side. If a control
599 * layer is defined, then it is supposed to be a socket layer and file
600 * descriptors are then shutdown or closed accordingly. The function
601 * automatically disables polling if needed.
602 */
603static void cs_app_shutr_conn(struct conn_stream *cs)
604{
605 struct channel *ic = cs_ic(cs);
606
607 BUG_ON(!cs_conn(cs));
608
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200609 cs_rx_shut_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200610 if (ic->flags & CF_SHUTR)
611 return;
612 ic->flags |= CF_SHUTR;
613 ic->rex = TICK_ETERNITY;
614
615 if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
616 return;
617
618 if (cs_oc(cs)->flags & CF_SHUTW) {
619 cs_conn_close(cs);
620 cs->state = CS_ST_DIS;
621 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
622 }
623 else if (cs->flags & CS_FL_NOHALF) {
624 /* we want to immediately forward this close to the write side */
625 return cs_app_shutw_conn(cs);
626 }
627}
628
629/*
630 * This function performs a shutdown-write 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 write side or marks itself as closed. The
633 * buffer flags are updated to reflect the new state. It does also close
634 * everything if the SI was marked as being in error state. If there is a
635 * data-layer shutdown, it is called.
636 */
637static void cs_app_shutw_conn(struct conn_stream *cs)
638{
639 struct channel *ic = cs_ic(cs);
640 struct channel *oc = cs_oc(cs);
641
642 BUG_ON(!cs_conn(cs));
643
644 oc->flags &= ~CF_SHUTW_NOW;
645 if (oc->flags & CF_SHUTW)
646 return;
647 oc->flags |= CF_SHUTW;
648 oc->wex = TICK_ETERNITY;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200649 cs_done_get(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200650
651 if (tick_isset(cs->hcto)) {
652 ic->rto = cs->hcto;
653 ic->rex = tick_add(now_ms, ic->rto);
654 }
655
656 switch (cs->state) {
657 case CS_ST_RDY:
658 case CS_ST_EST:
659 /* we have to shut before closing, otherwise some short messages
660 * may never leave the system, especially when there are remaining
661 * unread data in the socket input buffer, or when nolinger is set.
662 * However, if CS_FL_NOLINGER is explicitly set, we know there is
663 * no risk so we close both sides immediately.
664 */
665
666 if (cs->endp->flags & CS_EP_ERROR) {
667 /* quick close, the socket is already shut anyway */
668 }
669 else if (cs->flags & CS_FL_NOLINGER) {
670 /* unclean data-layer shutdown, typically an aborted request
671 * or a forwarded shutdown from a client to a server due to
672 * option abortonclose. No need for the TLS layer to try to
673 * emit a shutdown message.
674 */
675 cs_conn_shutw(cs, CO_SHW_SILENT);
676 }
677 else {
678 /* clean data-layer shutdown. This only happens on the
679 * frontend side, or on the backend side when forwarding
680 * a client close in TCP mode or in HTTP TUNNEL mode
681 * while option abortonclose is set. We want the TLS
682 * layer to try to signal it to the peer before we close.
683 */
684 cs_conn_shutw(cs, CO_SHW_NORMAL);
685
686 if (!(ic->flags & (CF_SHUTR|CF_DONT_READ)))
687 return;
688 }
689
690 /* fall through */
691 case CS_ST_CON:
692 /* we may have to close a pending connection, and mark the
693 * response buffer as shutr
694 */
695 cs_conn_close(cs);
696 /* fall through */
697 case CS_ST_CER:
698 case CS_ST_QUE:
699 case CS_ST_TAR:
700 cs->state = CS_ST_DIS;
701 /* fall through */
702 default:
703 cs->flags &= ~CS_FL_NOLINGER;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200704 cs_rx_shut_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200705 ic->flags |= CF_SHUTR;
706 ic->rex = TICK_ETERNITY;
707 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
708 }
709}
710
711/* This function is used for inter-conn-stream calls. It is called by the
712 * consumer to inform the producer side that it may be interested in checking
713 * for free space in the buffer. Note that it intentionally does not update
714 * timeouts, so that we can still check them later at wake-up. This function is
715 * dedicated to connection-based stream interfaces.
716 */
717static void cs_app_chk_rcv_conn(struct conn_stream *cs)
718{
719 BUG_ON(!cs_conn(cs));
720
721 /* (re)start reading */
722 if (cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
723 tasklet_wakeup(cs->wait_event.tasklet);
724}
725
726
727/* This function is used for inter-conn-stream calls. It is called by the
728 * producer to inform the consumer side that it may be interested in checking
729 * for data in the buffer. Note that it intentionally does not update timeouts,
730 * so that we can still check them later at wake-up.
731 */
732static void cs_app_chk_snd_conn(struct conn_stream *cs)
733{
734 struct channel *oc = cs_oc(cs);
735
736 BUG_ON(!cs_conn(cs));
737
738 if (unlikely(!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST) ||
739 (oc->flags & CF_SHUTW)))
740 return;
741
742 if (unlikely(channel_is_empty(oc))) /* called with nothing to send ! */
743 return;
744
745 if (!oc->pipe && /* spliced data wants to be forwarded ASAP */
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200746 !(cs->endp->flags & CS_EP_WAIT_DATA)) /* not waiting for data */
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200747 return;
748
749 if (!(cs->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(cs_oc(cs)))
Christopher Faulet000ba3e2022-04-01 17:06:32 +0200750 cs_conn_send(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200751
Christopher Faulet158f3362022-04-01 17:15:10 +0200752 if (cs->endp->flags & (CS_EP_ERROR|CS_EP_ERR_PENDING) || cs_is_conn_error(cs)) {
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200753 /* Write error on the file descriptor */
754 if (cs->state >= CS_ST_CON)
755 cs->endp->flags |= CS_EP_ERROR;
756 goto out_wakeup;
757 }
758
759 /* OK, so now we know that some data might have been sent, and that we may
760 * have to poll first. We have to do that too if the buffer is not empty.
761 */
762 if (channel_is_empty(oc)) {
763 /* the connection is established but we can't write. Either the
764 * buffer is empty, or we just refrain from sending because the
765 * ->o limit was reached. Maybe we just wrote the last
766 * chunk and need to close.
767 */
768 if (((oc->flags & (CF_SHUTW|CF_AUTO_CLOSE|CF_SHUTW_NOW)) ==
769 (CF_AUTO_CLOSE|CF_SHUTW_NOW)) &&
770 cs_state_in(cs->state, CS_SB_RDY|CS_SB_EST)) {
771 cs_shutw(cs);
772 goto out_wakeup;
773 }
774
775 if ((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == 0)
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200776 cs->endp->flags |= CS_EP_WAIT_DATA;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200777 oc->wex = TICK_ETERNITY;
778 }
779 else {
780 /* Otherwise there are remaining data to be sent in the buffer,
781 * which means we have to poll before doing so.
782 */
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200783 cs->endp->flags &= ~CS_EP_WAIT_DATA;
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200784 if (!tick_isset(oc->wex))
785 oc->wex = tick_add_ifset(now_ms, oc->wto);
786 }
787
788 if (likely(oc->flags & CF_WRITE_ACTIVITY)) {
789 struct channel *ic = cs_ic(cs);
790
791 /* update timeout if we have written something */
792 if ((oc->flags & (CF_SHUTW|CF_WRITE_PARTIAL)) == CF_WRITE_PARTIAL &&
793 !channel_is_empty(oc))
794 oc->wex = tick_add_ifset(now_ms, oc->wto);
795
796 if (tick_isset(ic->rex) && !(cs->flags & CS_FL_INDEP_STR)) {
797 /* Note: to prevent the client from expiring read timeouts
798 * during writes, we refresh it. We only do this if the
799 * interface is not configured for "independent streams",
800 * because for some applications it's better not to do this,
801 * for instance when continuously exchanging small amounts
802 * of data which can full the socket buffers long before a
803 * write timeout is detected.
804 */
805 ic->rex = tick_add_ifset(now_ms, ic->rto);
806 }
807 }
808
809 /* in case of special condition (error, shutdown, end of write...), we
810 * have to notify the task.
811 */
812 if (likely((oc->flags & (CF_WRITE_NULL|CF_WRITE_ERROR|CF_SHUTW)) ||
813 ((oc->flags & CF_WAKE_WRITE) &&
814 ((channel_is_empty(oc) && !oc->to_forward) ||
815 !cs_state_in(cs->state, CS_SB_EST))))) {
816 out_wakeup:
817 if (!(cs->flags & CS_FL_DONT_WAKE))
818 task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO);
819 }
820}
821
822/*
823 * This function performs a shutdown-read on a conn-stream attached to an
824 * applet in a connected or init state (it does nothing for other states). It
825 * either shuts the read side or marks itself as closed. The buffer flags are
826 * updated to reflect the new state. If the stream interface has CS_FL_NOHALF,
827 * we also forward the close to the write side. The owner task is woken up if
828 * it exists.
829 */
830static void cs_app_shutr_applet(struct conn_stream *cs)
831{
832 struct channel *ic = cs_ic(cs);
833
834 BUG_ON(!cs_appctx(cs));
835
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200836 cs_rx_shut_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200837 if (ic->flags & CF_SHUTR)
838 return;
839 ic->flags |= CF_SHUTR;
840 ic->rex = TICK_ETERNITY;
841
842 /* Note: on shutr, we don't call the applet */
843
844 if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
845 return;
846
847 if (cs_oc(cs)->flags & CF_SHUTW) {
848 cs_applet_release(cs);
849 cs->state = CS_ST_DIS;
850 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
851 }
852 else if (cs->flags & CS_FL_NOHALF) {
853 /* we want to immediately forward this close to the write side */
854 return cs_app_shutw_applet(cs);
855 }
856}
857
858/*
859 * This function performs a shutdown-write 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 write side or marks itself as closed. The buffer flags are
862 * updated to reflect the new state. It does also close everything if the SI
863 * was marked as being in error state. The owner task is woken up if it exists.
864 */
865static void cs_app_shutw_applet(struct conn_stream *cs)
866{
867 struct channel *ic = cs_ic(cs);
868 struct channel *oc = cs_oc(cs);
869
870 BUG_ON(!cs_appctx(cs));
871
872 oc->flags &= ~CF_SHUTW_NOW;
873 if (oc->flags & CF_SHUTW)
874 return;
875 oc->flags |= CF_SHUTW;
876 oc->wex = TICK_ETERNITY;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200877 cs_done_get(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200878
879 if (tick_isset(cs->hcto)) {
880 ic->rto = cs->hcto;
881 ic->rex = tick_add(now_ms, ic->rto);
882 }
883
884 /* on shutw we always wake the applet up */
885 appctx_wakeup(__cs_appctx(cs));
886
887 switch (cs->state) {
888 case CS_ST_RDY:
889 case CS_ST_EST:
890 /* we have to shut before closing, otherwise some short messages
891 * may never leave the system, especially when there are remaining
892 * unread data in the socket input buffer, or when nolinger is set.
893 * However, if CS_FL_NOLINGER is explicitly set, we know there is
894 * no risk so we close both sides immediately.
895 */
896 if (!(cs->endp->flags & CS_EP_ERROR) && !(cs->flags & CS_FL_NOLINGER) &&
897 !(ic->flags & (CF_SHUTR|CF_DONT_READ)))
898 return;
899
900 /* fall through */
901 case CS_ST_CON:
902 case CS_ST_CER:
903 case CS_ST_QUE:
904 case CS_ST_TAR:
905 /* Note that none of these states may happen with applets */
906 cs_applet_release(cs);
907 cs->state = CS_ST_DIS;
908 /* fall through */
909 default:
910 cs->flags &= ~CS_FL_NOLINGER;
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200911 cs_rx_shut_blk(cs);
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200912 ic->flags |= CF_SHUTR;
913 ic->rex = TICK_ETERNITY;
914 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
915 }
916}
917
918/* chk_rcv function for applets */
919static void cs_app_chk_rcv_applet(struct conn_stream *cs)
920{
921 struct channel *ic = cs_ic(cs);
922
923 BUG_ON(!cs_appctx(cs));
924
925 DPRINTF(stderr, "%s: cs=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n",
926 __FUNCTION__,
927 cs, cs->state, ic->flags, cs_oc(cs)->flags);
928
929 if (!ic->pipe) {
930 /* (re)start reading */
931 appctx_wakeup(__cs_appctx(cs));
932 }
933}
934
935/* chk_snd function for applets */
936static void cs_app_chk_snd_applet(struct conn_stream *cs)
937{
938 struct channel *oc = cs_oc(cs);
939
940 BUG_ON(!cs_appctx(cs));
941
942 DPRINTF(stderr, "%s: cs=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n",
943 __FUNCTION__,
944 cs, cs->state, cs_ic(cs)->flags, oc->flags);
945
946 if (unlikely(cs->state != CS_ST_EST || (oc->flags & CF_SHUTW)))
947 return;
948
949 /* we only wake the applet up if it was waiting for some data */
950
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200951 if (!(cs->endp->flags & CS_EP_WAIT_DATA))
Christopher Faulet9ffddd52022-04-01 14:04:29 +0200952 return;
953
954 if (!tick_isset(oc->wex))
955 oc->wex = tick_add_ifset(now_ms, oc->wto);
956
957 if (!channel_is_empty(oc)) {
958 /* (re)start sending */
959 appctx_wakeup(__cs_appctx(cs));
960 }
961}
Christopher Faulet13045f02022-04-01 14:23:38 +0200962
963
964/* This function is designed to be called from within the stream handler to
965 * update the input channel's expiration timer and the conn-stream's
966 * Rx flags based on the channel's flags. It needs to be called only once
967 * after the channel's flags have settled down, and before they are cleared,
968 * though it doesn't harm to call it as often as desired (it just slightly
969 * hurts performance). It must not be called from outside of the stream
970 * handler, as what it does will be used to compute the stream task's
971 * expiration.
972 */
973void cs_update_rx(struct conn_stream *cs)
974{
975 struct channel *ic = cs_ic(cs);
976
977 if (ic->flags & CF_SHUTR) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200978 cs_rx_shut_blk(cs);
Christopher Faulet13045f02022-04-01 14:23:38 +0200979 return;
980 }
981
982 /* Read not closed, update FD status and timeout for reads */
983 if (ic->flags & CF_DONT_READ)
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200984 cs_rx_chan_blk(cs);
Christopher Faulet13045f02022-04-01 14:23:38 +0200985 else
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200986 cs_rx_chan_rdy(cs);
Christopher Faulet13045f02022-04-01 14:23:38 +0200987
988 if (!channel_is_empty(ic) || !channel_may_recv(ic)) {
989 /* stop reading, imposed by channel's policy or contents */
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200990 cs_rx_room_blk(cs);
Christopher Faulet13045f02022-04-01 14:23:38 +0200991 }
992 else {
993 /* (re)start reading and update timeout. Note: we don't recompute the timeout
994 * every time we get here, otherwise it would risk never to expire. We only
995 * update it if is was not yet set. The stream socket handler will already
996 * have updated it if there has been a completed I/O.
997 */
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200998 cs_rx_room_rdy(cs);
Christopher Faulet13045f02022-04-01 14:23:38 +0200999 }
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001000 if (cs->endp->flags & CS_EP_RXBLK_ANY & ~CS_EP_RX_WAIT_EP)
Christopher Faulet13045f02022-04-01 14:23:38 +02001001 ic->rex = TICK_ETERNITY;
1002 else if (!(ic->flags & CF_READ_NOEXP) && !tick_isset(ic->rex))
1003 ic->rex = tick_add_ifset(now_ms, ic->rto);
1004
1005 cs_chk_rcv(cs);
1006}
1007
1008/* This function is designed to be called from within the stream handler to
1009 * update the output channel's expiration timer and the conn-stream's
1010 * Tx flags based on the channel's flags. It needs to be called only once
1011 * after the channel's flags have settled down, and before they are cleared,
1012 * though it doesn't harm to call it as often as desired (it just slightly
1013 * hurts performance). It must not be called from outside of the stream
1014 * handler, as what it does will be used to compute the stream task's
1015 * expiration.
1016 */
1017void cs_update_tx(struct conn_stream *cs)
1018{
1019 struct channel *oc = cs_oc(cs);
1020 struct channel *ic = cs_ic(cs);
1021
1022 if (oc->flags & CF_SHUTW)
1023 return;
1024
1025 /* Write not closed, update FD status and timeout for writes */
1026 if (channel_is_empty(oc)) {
1027 /* stop writing */
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001028 if (!(cs->endp->flags & CS_EP_WAIT_DATA)) {
Christopher Faulet13045f02022-04-01 14:23:38 +02001029 if ((oc->flags & CF_SHUTW_NOW) == 0)
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001030 cs->endp->flags |= CS_EP_WAIT_DATA;
Christopher Faulet13045f02022-04-01 14:23:38 +02001031 oc->wex = TICK_ETERNITY;
1032 }
1033 return;
1034 }
1035
1036 /* (re)start writing and update timeout. Note: we don't recompute the timeout
1037 * every time we get here, otherwise it would risk never to expire. We only
1038 * update it if is was not yet set. The stream socket handler will already
1039 * have updated it if there has been a completed I/O.
1040 */
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001041 cs->endp->flags &= ~CS_EP_WAIT_DATA;
Christopher Faulet13045f02022-04-01 14:23:38 +02001042 if (!tick_isset(oc->wex)) {
1043 oc->wex = tick_add_ifset(now_ms, oc->wto);
1044 if (tick_isset(ic->rex) && !(cs->flags & CS_FL_INDEP_STR)) {
1045 /* Note: depending on the protocol, we don't know if we're waiting
1046 * for incoming data or not. So in order to prevent the socket from
1047 * expiring read timeouts during writes, we refresh the read timeout,
1048 * except if it was already infinite or if we have explicitly setup
1049 * independent streams.
1050 */
1051 ic->rex = tick_add_ifset(now_ms, ic->rto);
Christopher Faulet5e29b762022-04-04 08:58:34 +02001052 }
1053 }
1054}
1055
1056/* This function is the equivalent to cs_update() except that it's
1057 * designed to be called from outside the stream handlers, typically the lower
1058 * layers (applets, connections) after I/O completion. After updating the stream
1059 * interface and timeouts, it will try to forward what can be forwarded, then to
1060 * wake the associated task up if an important event requires special handling.
1061 * It may update CS_EP_WAIT_DATA and/or CS_EP_RXBLK_ROOM, that the callers are
1062 * encouraged to watch to take appropriate action.
1063 * It should not be called from within the stream itself, cs_update()
1064 * is designed for this.
1065 */
1066static void cs_notify(struct conn_stream *cs)
1067{
1068 struct channel *ic = cs_ic(cs);
1069 struct channel *oc = cs_oc(cs);
1070 struct conn_stream *cso = cs_opposite(cs);
1071 struct task *task = cs_strm_task(cs);
1072
1073 /* process consumer side */
1074 if (channel_is_empty(oc)) {
1075 struct connection *conn = cs_conn(cs);
1076
1077 if (((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW) &&
1078 (cs->state == CS_ST_EST) && (!conn || !(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS))))
1079 cs_shutw(cs);
1080 oc->wex = TICK_ETERNITY;
1081 }
1082
1083 /* indicate that we may be waiting for data from the output channel or
1084 * we're about to close and can't expect more data if SHUTW_NOW is there.
1085 */
1086 if (!(oc->flags & (CF_SHUTW|CF_SHUTW_NOW)))
1087 cs->endp->flags |= CS_EP_WAIT_DATA;
1088 else if ((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW)
1089 cs->endp->flags &= ~CS_EP_WAIT_DATA;
1090
1091 /* update OC timeouts and wake the other side up if it's waiting for room */
1092 if (oc->flags & CF_WRITE_ACTIVITY) {
1093 if ((oc->flags & (CF_SHUTW|CF_WRITE_PARTIAL)) == CF_WRITE_PARTIAL &&
1094 !channel_is_empty(oc))
1095 if (tick_isset(oc->wex))
1096 oc->wex = tick_add_ifset(now_ms, oc->wto);
1097
1098 if (!(cs->flags & CS_FL_INDEP_STR))
1099 if (tick_isset(ic->rex))
1100 ic->rex = tick_add_ifset(now_ms, ic->rto);
1101 }
1102
1103 if (oc->flags & CF_DONT_READ)
1104 cs_rx_chan_blk(cso);
1105 else
1106 cs_rx_chan_rdy(cso);
1107
1108 /* Notify the other side when we've injected data into the IC that
1109 * needs to be forwarded. We can do fast-forwarding as soon as there
1110 * are output data, but we avoid doing this if some of the data are
1111 * not yet scheduled for being forwarded, because it is very likely
1112 * that it will be done again immediately afterwards once the following
1113 * data are parsed (eg: HTTP chunking). We only CS_EP_RXBLK_ROOM once
1114 * we've emptied *some* of the output buffer, and not just when there
1115 * is available room, because applets are often forced to stop before
1116 * the buffer is full. We must not stop based on input data alone because
1117 * an HTTP parser might need more data to complete the parsing.
1118 */
1119 if (!channel_is_empty(ic) &&
1120 (cso->endp->flags & CS_EP_WAIT_DATA) &&
1121 (!(ic->flags & CF_EXPECT_MORE) || c_full(ic) || ci_data(ic) == 0 || ic->pipe)) {
1122 int new_len, last_len;
1123
1124 last_len = co_data(ic);
1125 if (ic->pipe)
1126 last_len += ic->pipe->data;
1127
1128 cs_chk_snd(cso);
1129
1130 new_len = co_data(ic);
1131 if (ic->pipe)
1132 new_len += ic->pipe->data;
1133
1134 /* check if the consumer has freed some space either in the
1135 * buffer or in the pipe.
1136 */
1137 if (new_len < last_len)
1138 cs_rx_room_rdy(cs);
1139 }
1140
1141 if (!(ic->flags & CF_DONT_READ))
1142 cs_rx_chan_rdy(cs);
1143
1144 cs_chk_rcv(cs);
1145 cs_chk_rcv(cso);
1146
1147 if (cs_rx_blocked(cs)) {
1148 ic->rex = TICK_ETERNITY;
1149 }
1150 else if ((ic->flags & (CF_SHUTR|CF_READ_PARTIAL)) == CF_READ_PARTIAL) {
1151 /* we must re-enable reading if cs_chk_snd() has freed some space */
1152 if (!(ic->flags & CF_READ_NOEXP) && tick_isset(ic->rex))
1153 ic->rex = tick_add_ifset(now_ms, ic->rto);
1154 }
1155
1156 /* wake the task up only when needed */
1157 if (/* changes on the production side */
1158 (ic->flags & (CF_READ_NULL|CF_READ_ERROR)) ||
1159 !cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST) ||
1160 (cs->endp->flags & CS_EP_ERROR) ||
1161 ((ic->flags & CF_READ_PARTIAL) &&
1162 ((ic->flags & CF_EOI) || !ic->to_forward || cso->state != CS_ST_EST)) ||
1163
1164 /* changes on the consumption side */
1165 (oc->flags & (CF_WRITE_NULL|CF_WRITE_ERROR)) ||
1166 ((oc->flags & CF_WRITE_ACTIVITY) &&
1167 ((oc->flags & CF_SHUTW) ||
1168 (((oc->flags & CF_WAKE_WRITE) ||
1169 !(oc->flags & (CF_AUTO_CLOSE|CF_SHUTW_NOW|CF_SHUTW))) &&
1170 (cso->state != CS_ST_EST ||
1171 (channel_is_empty(oc) && !oc->to_forward)))))) {
1172 task_wakeup(task, TASK_WOKEN_IO);
1173 }
1174 else {
1175 /* Update expiration date for the task and requeue it */
1176 task->expire = tick_first((tick_is_expired(task->expire, now_ms) ? 0 : task->expire),
1177 tick_first(tick_first(ic->rex, ic->wex),
1178 tick_first(oc->rex, oc->wex)));
1179
1180 task->expire = tick_first(task->expire, ic->analyse_exp);
1181 task->expire = tick_first(task->expire, oc->analyse_exp);
1182 task->expire = tick_first(task->expire, __cs_strm(cs)->conn_exp);
1183
1184 task_queue(task);
1185 }
1186 if (ic->flags & CF_READ_ACTIVITY)
1187 ic->flags &= ~CF_READ_DONTWAIT;
1188}
1189
1190/*
1191 * This function propagates a null read received on a socket-based connection.
1192 * It updates the stream interface. If the stream interface has CS_FL_NOHALF,
1193 * the close is also forwarded to the write side as an abort.
1194 */
1195static void cs_conn_read0(struct conn_stream *cs)
1196{
1197 struct channel *ic = cs_ic(cs);
1198 struct channel *oc = cs_oc(cs);
1199
1200 BUG_ON(!cs_conn(cs));
1201
1202 cs_rx_shut_blk(cs);
1203 if (ic->flags & CF_SHUTR)
1204 return;
1205 ic->flags |= CF_SHUTR;
1206 ic->rex = TICK_ETERNITY;
1207
1208 if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
1209 return;
1210
1211 if (oc->flags & CF_SHUTW)
1212 goto do_close;
1213
1214 if (cs->flags & CS_FL_NOHALF) {
1215 /* we want to immediately forward this close to the write side */
1216 /* force flag on ssl to keep stream in cache */
1217 cs_conn_shutw(cs, CO_SHW_SILENT);
1218 goto do_close;
1219 }
1220
1221 /* otherwise that's just a normal read shutdown */
1222 return;
1223
1224 do_close:
1225 /* OK we completely close the socket here just as if we went through cs_shut[rw]() */
1226 cs_conn_close(cs);
1227
1228 oc->flags &= ~CF_SHUTW_NOW;
1229 oc->flags |= CF_SHUTW;
1230 oc->wex = TICK_ETERNITY;
1231
1232 cs_done_get(cs);
1233
1234 cs->state = CS_ST_DIS;
1235 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
1236 return;
1237}
1238
1239/*
1240 * This is the callback which is called by the connection layer to receive data
1241 * into the buffer from the connection. It iterates over the mux layer's
1242 * rcv_buf function.
1243 */
1244static int cs_conn_recv(struct conn_stream *cs)
1245{
1246 struct connection *conn = __cs_conn(cs);
1247 struct channel *ic = cs_ic(cs);
1248 int ret, max, cur_read = 0;
1249 int read_poll = MAX_READ_POLL_LOOPS;
1250 int flags = 0;
1251
1252 /* If not established yet, do nothing. */
1253 if (cs->state != CS_ST_EST)
1254 return 0;
1255
1256 /* If another call to cs_conn_recv() failed, and we subscribed to
1257 * recv events already, give up now.
1258 */
1259 if (cs->wait_event.events & SUB_RETRY_RECV)
1260 return 0;
1261
1262 /* maybe we were called immediately after an asynchronous shutr */
1263 if (ic->flags & CF_SHUTR)
1264 return 1;
1265
1266 /* we must wait because the mux is not installed yet */
1267 if (!conn->mux)
1268 return 0;
1269
1270 /* stop here if we reached the end of data */
1271 if (cs->endp->flags & CS_EP_EOS)
1272 goto end_recv;
1273
1274 /* stop immediately on errors. Note that we DON'T want to stop on
1275 * POLL_ERR, as the poller might report a write error while there
1276 * are still data available in the recv buffer. This typically
1277 * happens when we send too large a request to a backend server
1278 * which rejects it before reading it all.
1279 */
1280 if (!(cs->endp->flags & CS_EP_RCV_MORE)) {
1281 if (!conn_xprt_ready(conn))
1282 return 0;
1283 if (cs->endp->flags & CS_EP_ERROR)
1284 goto end_recv;
1285 }
1286
1287 /* prepare to detect if the mux needs more room */
1288 cs->endp->flags &= ~CS_EP_WANT_ROOM;
1289
1290 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) && !co_data(ic) &&
1291 global.tune.idle_timer &&
1292 (unsigned short)(now_ms - ic->last_read) >= global.tune.idle_timer) {
1293 /* The buffer was empty and nothing was transferred for more
1294 * than one second. This was caused by a pause and not by
1295 * congestion. Reset any streaming mode to reduce latency.
1296 */
1297 ic->xfer_small = 0;
1298 ic->xfer_large = 0;
1299 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
1300 }
1301
1302 /* First, let's see if we may splice data across the channel without
1303 * using a buffer.
1304 */
1305 if (cs->endp->flags & CS_EP_MAY_SPLICE &&
1306 (ic->pipe || ic->to_forward >= MIN_SPLICE_FORWARD) &&
1307 ic->flags & CF_KERN_SPLICING) {
1308 if (c_data(ic)) {
1309 /* We're embarrassed, there are already data pending in
1310 * the buffer and we don't want to have them at two
1311 * locations at a time. Let's indicate we need some
1312 * place and ask the consumer to hurry.
1313 */
1314 flags |= CO_RFL_BUF_FLUSH;
1315 goto abort_splice;
1316 }
1317
1318 if (unlikely(ic->pipe == NULL)) {
1319 if (pipes_used >= global.maxpipes || !(ic->pipe = get_pipe())) {
1320 ic->flags &= ~CF_KERN_SPLICING;
1321 goto abort_splice;
1322 }
1323 }
1324
1325 ret = conn->mux->rcv_pipe(cs, ic->pipe, ic->to_forward);
1326 if (ret < 0) {
1327 /* splice not supported on this end, let's disable it */
1328 ic->flags &= ~CF_KERN_SPLICING;
1329 goto abort_splice;
1330 }
1331
1332 if (ret > 0) {
1333 if (ic->to_forward != CHN_INFINITE_FORWARD)
1334 ic->to_forward -= ret;
1335 ic->total += ret;
1336 cur_read += ret;
1337 ic->flags |= CF_READ_PARTIAL;
1338 }
1339
1340 if (cs->endp->flags & (CS_EP_EOS|CS_EP_ERROR))
1341 goto end_recv;
1342
1343 if (conn->flags & CO_FL_WAIT_ROOM) {
1344 /* the pipe is full or we have read enough data that it
1345 * could soon be full. Let's stop before needing to poll.
1346 */
1347 cs_rx_room_blk(cs);
1348 goto done_recv;
1349 }
1350
1351 /* splice not possible (anymore), let's go on on standard copy */
1352 }
1353
1354 abort_splice:
1355 if (ic->pipe && unlikely(!ic->pipe->data)) {
1356 put_pipe(ic->pipe);
1357 ic->pipe = NULL;
1358 }
1359
1360 if (ic->pipe && ic->to_forward && !(flags & CO_RFL_BUF_FLUSH) && cs->endp->flags & CS_EP_MAY_SPLICE) {
1361 /* don't break splicing by reading, but still call rcv_buf()
1362 * to pass the flag.
1363 */
1364 goto done_recv;
1365 }
1366
1367 /* now we'll need a input buffer for the stream */
1368 if (!cs_alloc_ibuf(cs, &(__cs_strm(cs)->buffer_wait)))
1369 goto end_recv;
1370
1371 /* For an HTX stream, if the buffer is stuck (no output data with some
1372 * input data) and if the HTX message is fragmented or if its free space
1373 * wraps, we force an HTX deframentation. It is a way to have a
1374 * contiguous free space nad to let the mux to copy as much data as
1375 * possible.
1376 *
1377 * NOTE: A possible optim may be to let the mux decides if defrag is
1378 * required or not, depending on amount of data to be xferred.
1379 */
1380 if (IS_HTX_STRM(__cs_strm(cs)) && !co_data(ic)) {
1381 struct htx *htx = htxbuf(&ic->buf);
1382
1383 if (htx_is_not_empty(htx) && ((htx->flags & HTX_FL_FRAGMENTED) || htx_space_wraps(htx)))
1384 htx_defrag(htx, NULL, 0);
1385 }
1386
1387 /* Instruct the mux it must subscribed for read events */
1388 flags |= ((!conn_is_back(conn) && (__cs_strm(cs)->be->options & PR_O_ABRT_CLOSE)) ? CO_RFL_KEEP_RECV : 0);
1389
1390 /* Important note : if we're called with POLL_IN|POLL_HUP, it means the read polling
1391 * was enabled, which implies that the recv buffer was not full. So we have a guarantee
1392 * that if such an event is not handled above in splice, it will be handled here by
1393 * recv().
1394 */
1395 while ((cs->endp->flags & CS_EP_RCV_MORE) ||
1396 (!(conn->flags & CO_FL_HANDSHAKE) &&
1397 (!(cs->endp->flags & (CS_EP_ERROR|CS_EP_EOS))) && !(ic->flags & CF_SHUTR))) {
1398 int cur_flags = flags;
1399
1400 /* Compute transient CO_RFL_* flags */
1401 if (co_data(ic)) {
1402 cur_flags |= (CO_RFL_BUF_WET | CO_RFL_BUF_NOT_STUCK);
1403 }
1404
1405 /* <max> may be null. This is the mux responsibility to set
1406 * CS_EP_RCV_MORE on the CS if more space is needed.
1407 */
1408 max = channel_recv_max(ic);
1409 ret = conn->mux->rcv_buf(cs, &ic->buf, max, cur_flags);
1410
1411 if (cs->endp->flags & CS_EP_WANT_ROOM) {
1412 /* CS_EP_WANT_ROOM must not be reported if the channel's
1413 * buffer is empty.
1414 */
1415 BUG_ON(c_empty(ic));
1416
1417 cs_rx_room_blk(cs);
1418 /* Add READ_PARTIAL because some data are pending but
1419 * cannot be xferred to the channel
1420 */
1421 ic->flags |= CF_READ_PARTIAL;
1422 }
1423
1424 if (ret <= 0) {
1425 /* if we refrained from reading because we asked for a
1426 * flush to satisfy rcv_pipe(), we must not subscribe
1427 * and instead report that there's not enough room
1428 * here to proceed.
1429 */
1430 if (flags & CO_RFL_BUF_FLUSH)
1431 cs_rx_room_blk(cs);
1432 break;
1433 }
1434
1435 cur_read += ret;
1436
1437 /* if we're allowed to directly forward data, we must update ->o */
1438 if (ic->to_forward && !(ic->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
1439 unsigned long fwd = ret;
1440 if (ic->to_forward != CHN_INFINITE_FORWARD) {
1441 if (fwd > ic->to_forward)
1442 fwd = ic->to_forward;
1443 ic->to_forward -= fwd;
1444 }
1445 c_adv(ic, fwd);
1446 }
1447
1448 ic->flags |= CF_READ_PARTIAL;
1449 ic->total += ret;
1450
1451 /* End-of-input reached, we can leave. In this case, it is
1452 * important to break the loop to not block the SI because of
1453 * the channel's policies.This way, we are still able to receive
1454 * shutdowns.
1455 */
1456 if (cs->endp->flags & CS_EP_EOI)
1457 break;
1458
1459 if ((ic->flags & CF_READ_DONTWAIT) || --read_poll <= 0) {
1460 /* we're stopped by the channel's policy */
1461 cs_rx_chan_blk(cs);
1462 break;
1463 }
1464
1465 /* if too many bytes were missing from last read, it means that
1466 * it's pointless trying to read again because the system does
1467 * not have them in buffers.
1468 */
1469 if (ret < max) {
1470 /* if a streamer has read few data, it may be because we
1471 * have exhausted system buffers. It's not worth trying
1472 * again.
1473 */
1474 if (ic->flags & CF_STREAMER) {
1475 /* we're stopped by the channel's policy */
1476 cs_rx_chan_blk(cs);
1477 break;
1478 }
1479
1480 /* if we read a large block smaller than what we requested,
1481 * it's almost certain we'll never get anything more.
1482 */
1483 if (ret >= global.tune.recv_enough) {
1484 /* we're stopped by the channel's policy */
1485 cs_rx_chan_blk(cs);
1486 break;
1487 }
1488 }
1489
1490 /* if we are waiting for more space, don't try to read more data
1491 * right now.
1492 */
1493 if (cs_rx_blocked(cs))
1494 break;
1495 } /* while !flags */
1496
1497 done_recv:
1498 if (cur_read) {
1499 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) &&
1500 (cur_read <= ic->buf.size / 2)) {
1501 ic->xfer_large = 0;
1502 ic->xfer_small++;
1503 if (ic->xfer_small >= 3) {
1504 /* we have read less than half of the buffer in
1505 * one pass, and this happened at least 3 times.
1506 * This is definitely not a streamer.
1507 */
1508 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
1509 }
1510 else if (ic->xfer_small >= 2) {
1511 /* if the buffer has been at least half full twice,
1512 * we receive faster than we send, so at least it
1513 * is not a "fast streamer".
1514 */
1515 ic->flags &= ~CF_STREAMER_FAST;
1516 }
1517 }
1518 else if (!(ic->flags & CF_STREAMER_FAST) &&
1519 (cur_read >= ic->buf.size - global.tune.maxrewrite)) {
1520 /* we read a full buffer at once */
1521 ic->xfer_small = 0;
1522 ic->xfer_large++;
1523 if (ic->xfer_large >= 3) {
1524 /* we call this buffer a fast streamer if it manages
1525 * to be filled in one call 3 consecutive times.
1526 */
1527 ic->flags |= (CF_STREAMER | CF_STREAMER_FAST);
1528 }
1529 }
1530 else {
1531 ic->xfer_small = 0;
1532 ic->xfer_large = 0;
1533 }
1534 ic->last_read = now_ms;
1535 }
1536
1537 end_recv:
1538 ret = (cur_read != 0);
1539
1540 /* Report EOI on the channel if it was reached from the mux point of
1541 * view. */
1542 if ((cs->endp->flags & CS_EP_EOI) && !(ic->flags & CF_EOI)) {
1543 ic->flags |= (CF_EOI|CF_READ_PARTIAL);
1544 ret = 1;
1545 }
1546
1547 if (cs->endp->flags & CS_EP_ERROR)
1548 ret = 1;
1549 else if (cs->endp->flags & CS_EP_EOS) {
1550 /* we received a shutdown */
1551 ic->flags |= CF_READ_NULL;
1552 if (ic->flags & CF_AUTO_CLOSE)
1553 channel_shutw_now(ic);
1554 cs_conn_read0(cs);
1555 ret = 1;
1556 }
1557 else if (!cs_rx_blocked(cs)) {
1558 /* Subscribe to receive events if we're blocking on I/O */
1559 conn->mux->subscribe(cs, SUB_RETRY_RECV, &cs->wait_event);
1560 cs_rx_endp_done(cs);
1561 } else {
1562 cs_rx_endp_more(cs);
1563 ret = 1;
1564 }
1565 return ret;
1566}
1567
1568/* This tries to perform a synchronous receive on the stream interface to
1569 * try to collect last arrived data. In practice it's only implemented on
1570 * conn_streams. Returns 0 if nothing was done, non-zero if new data or a
1571 * shutdown were collected. This may result on some delayed receive calls
1572 * to be programmed and performed later, though it doesn't provide any
1573 * such guarantee.
1574 */
1575int cs_conn_sync_recv(struct conn_stream *cs)
1576{
1577 if (!cs_state_in(cs->state, CS_SB_RDY|CS_SB_EST))
1578 return 0;
1579
1580 if (!cs_conn_mux(cs))
1581 return 0; // only conn_streams are supported
1582
1583 if (cs->wait_event.events & SUB_RETRY_RECV)
1584 return 0; // already subscribed
1585
1586 if (!cs_rx_endp_ready(cs) || cs_rx_blocked(cs))
1587 return 0; // already failed
1588
1589 return cs_conn_recv(cs);
1590}
1591
1592/*
1593 * This function is called to send buffer data to a stream socket.
1594 * It calls the mux layer's snd_buf function. It relies on the
1595 * caller to commit polling changes. The caller should check conn->flags
1596 * for errors.
1597 */
1598static int cs_conn_send(struct conn_stream *cs)
1599{
1600 struct connection *conn = __cs_conn(cs);
1601 struct stream *s = __cs_strm(cs);
1602 struct channel *oc = cs_oc(cs);
1603 int ret;
1604 int did_send = 0;
1605
1606 if (cs->endp->flags & (CS_EP_ERROR|CS_EP_ERR_PENDING) || cs_is_conn_error(cs)) {
1607 /* We're probably there because the tasklet was woken up,
1608 * but process_stream() ran before, detected there were an
1609 * error and put the si back to CS_ST_TAR. There's still
1610 * CO_FL_ERROR on the connection but we don't want to add
1611 * CS_EP_ERROR back, so give up
1612 */
1613 if (cs->state < CS_ST_CON)
1614 return 0;
1615 cs->endp->flags |= CS_EP_ERROR;
1616 return 1;
1617 }
1618
1619 /* We're already waiting to be able to send, give up */
1620 if (cs->wait_event.events & SUB_RETRY_SEND)
1621 return 0;
1622
1623 /* we might have been called just after an asynchronous shutw */
1624 if (oc->flags & CF_SHUTW)
1625 return 1;
1626
1627 /* we must wait because the mux is not installed yet */
1628 if (!conn->mux)
1629 return 0;
1630
1631 if (oc->pipe && conn->xprt->snd_pipe && conn->mux->snd_pipe) {
1632 ret = conn->mux->snd_pipe(cs, oc->pipe);
1633 if (ret > 0)
1634 did_send = 1;
1635
1636 if (!oc->pipe->data) {
1637 put_pipe(oc->pipe);
1638 oc->pipe = NULL;
1639 }
1640
1641 if (oc->pipe)
1642 goto end;
1643 }
1644
1645 /* At this point, the pipe is empty, but we may still have data pending
1646 * in the normal buffer.
1647 */
1648 if (co_data(oc)) {
1649 /* when we're here, we already know that there is no spliced
1650 * data left, and that there are sendable buffered data.
1651 */
1652
1653 /* check if we want to inform the kernel that we're interested in
1654 * sending more data after this call. We want this if :
1655 * - we're about to close after this last send and want to merge
1656 * the ongoing FIN with the last segment.
1657 * - we know we can't send everything at once and must get back
1658 * here because of unaligned data
1659 * - there is still a finite amount of data to forward
1660 * The test is arranged so that the most common case does only 2
1661 * tests.
1662 */
1663 unsigned int send_flag = 0;
1664
1665 if ((!(oc->flags & (CF_NEVER_WAIT|CF_SEND_DONTWAIT)) &&
1666 ((oc->to_forward && oc->to_forward != CHN_INFINITE_FORWARD) ||
1667 (oc->flags & CF_EXPECT_MORE) ||
1668 (IS_HTX_STRM(s) &&
1669 (!(oc->flags & (CF_EOI|CF_SHUTR)) && htx_expect_more(htxbuf(&oc->buf)))))) ||
1670 ((oc->flags & CF_ISRESP) &&
1671 ((oc->flags & (CF_AUTO_CLOSE|CF_SHUTW_NOW)) == (CF_AUTO_CLOSE|CF_SHUTW_NOW))))
1672 send_flag |= CO_SFL_MSG_MORE;
1673
1674 if (oc->flags & CF_STREAMER)
1675 send_flag |= CO_SFL_STREAMER;
1676
1677 if (s->txn && s->txn->flags & TX_L7_RETRY && !b_data(&s->txn->l7_buffer)) {
1678 /* If we want to be able to do L7 retries, copy
1679 * the data we're about to send, so that we are able
1680 * to resend them if needed
1681 */
1682 /* Try to allocate a buffer if we had none.
1683 * If it fails, the next test will just
1684 * disable the l7 retries by setting
1685 * l7_conn_retries to 0.
1686 */
1687 if (s->txn->req.msg_state != HTTP_MSG_DONE)
1688 s->txn->flags &= ~TX_L7_RETRY;
1689 else {
1690 if (b_alloc(&s->txn->l7_buffer) == NULL)
1691 s->txn->flags &= ~TX_L7_RETRY;
1692 else {
1693 memcpy(b_orig(&s->txn->l7_buffer),
1694 b_orig(&oc->buf),
1695 b_size(&oc->buf));
1696 s->txn->l7_buffer.head = co_data(oc);
1697 b_add(&s->txn->l7_buffer, co_data(oc));
1698 }
1699
1700 }
1701 }
1702
1703 ret = conn->mux->snd_buf(cs, &oc->buf, co_data(oc), send_flag);
1704 if (ret > 0) {
1705 did_send = 1;
1706 c_rew(oc, ret);
1707 c_realign_if_empty(oc);
1708
1709 if (!co_data(oc)) {
1710 /* Always clear both flags once everything has been sent, they're one-shot */
1711 oc->flags &= ~(CF_EXPECT_MORE | CF_SEND_DONTWAIT);
1712 }
1713 /* if some data remain in the buffer, it's only because the
1714 * system buffers are full, we will try next time.
1715 */
Christopher Faulet13045f02022-04-01 14:23:38 +02001716 }
1717 }
Christopher Faulet5e29b762022-04-04 08:58:34 +02001718
1719 end:
1720 if (did_send) {
1721 oc->flags |= CF_WRITE_PARTIAL | CF_WROTE_DATA;
1722 if (cs->state == CS_ST_CON)
1723 cs->state = CS_ST_RDY;
1724
1725 cs_rx_room_rdy(cs_opposite(cs));
1726 }
1727
1728 if (cs->endp->flags & (CS_EP_ERROR|CS_EP_ERR_PENDING)) {
1729 cs->endp->flags |= CS_EP_ERROR;
1730 return 1;
1731 }
1732
1733 /* We couldn't send all of our data, let the mux know we'd like to send more */
1734 if (!channel_is_empty(oc))
1735 conn->mux->subscribe(cs, SUB_RETRY_SEND, &cs->wait_event);
1736 return did_send;
1737}
1738
1739/* perform a synchronous send() for the stream interface. The CF_WRITE_NULL and
1740 * CF_WRITE_PARTIAL flags are cleared prior to the attempt, and will possibly
1741 * be updated in case of success.
1742 */
1743void cs_conn_sync_send(struct conn_stream *cs)
1744{
1745 struct channel *oc = cs_oc(cs);
1746
1747 oc->flags &= ~(CF_WRITE_NULL|CF_WRITE_PARTIAL);
1748
1749 if (oc->flags & CF_SHUTW)
1750 return;
1751
1752 if (channel_is_empty(oc))
1753 return;
1754
1755 if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
1756 return;
1757
1758 if (!cs_conn_mux(cs))
1759 return;
1760
1761 cs_conn_send(cs);
1762}
1763
1764/* Called by I/O handlers after completion.. It propagates
1765 * connection flags to the stream interface, updates the stream (which may or
1766 * may not take this opportunity to try to forward data), then update the
1767 * connection's polling based on the channels and stream interface's final
1768 * states. The function always returns 0.
1769 */
1770static int cs_conn_process(struct conn_stream *cs)
1771{
1772 struct connection *conn = __cs_conn(cs);
1773 struct channel *ic = cs_ic(cs);
1774 struct channel *oc = cs_oc(cs);
1775
1776 BUG_ON(!conn);
1777
1778 /* If we have data to send, try it now */
1779 if (!channel_is_empty(oc) && !(cs->wait_event.events & SUB_RETRY_SEND))
1780 cs_conn_send(cs);
1781
1782 /* First step, report to the conn-stream what was detected at the
1783 * connection layer : errors and connection establishment.
1784 * Only add CS_EP_ERROR if we're connected, or we're attempting to
1785 * connect, we may get there because we got woken up, but only run
1786 * after process_stream() noticed there were an error, and decided
1787 * to retry to connect, the connection may still have CO_FL_ERROR,
1788 * and we don't want to add CS_EP_ERROR back
1789 *
1790 * Note: This test is only required because cs_conn_process is also the SI
1791 * wake callback. Otherwise cs_conn_recv()/cs_conn_send() already take
1792 * care of it.
1793 */
1794
1795 if (cs->state >= CS_ST_CON) {
1796 if (cs_is_conn_error(cs))
1797 cs->endp->flags |= CS_EP_ERROR;
1798 }
1799
1800 /* If we had early data, and the handshake ended, then
1801 * we can remove the flag, and attempt to wake the task up,
1802 * in the event there's an analyser waiting for the end of
1803 * the handshake.
1804 */
1805 if (!(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS)) &&
1806 (cs->endp->flags & CS_EP_WAIT_FOR_HS)) {
1807 cs->endp->flags &= ~CS_EP_WAIT_FOR_HS;
1808 task_wakeup(cs_strm_task(cs), TASK_WOKEN_MSG);
1809 }
1810
1811 if (!cs_state_in(cs->state, CS_SB_EST|CS_SB_DIS|CS_SB_CLO) &&
1812 (conn->flags & CO_FL_WAIT_XPRT) == 0) {
1813 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
1814 oc->flags |= CF_WRITE_NULL;
1815 if (cs->state == CS_ST_CON)
1816 cs->state = CS_ST_RDY;
1817 }
1818
1819 /* Report EOS on the channel if it was reached from the mux point of
1820 * view.
1821 *
1822 * Note: This test is only required because cs_conn_process is also the SI
1823 * wake callback. Otherwise cs_conn_recv()/cs_conn_send() already take
1824 * care of it.
1825 */
1826 if (cs->endp->flags & CS_EP_EOS && !(ic->flags & CF_SHUTR)) {
1827 /* we received a shutdown */
1828 ic->flags |= CF_READ_NULL;
1829 if (ic->flags & CF_AUTO_CLOSE)
1830 channel_shutw_now(ic);
1831 cs_conn_read0(cs);
1832 }
1833
1834 /* Report EOI on the channel if it was reached from the mux point of
1835 * view.
1836 *
1837 * Note: This test is only required because cs_conn_process is also the SI
1838 * wake callback. Otherwise cs_conn_recv()/cs_conn_send() already take
1839 * care of it.
1840 */
1841 if ((cs->endp->flags & CS_EP_EOI) && !(ic->flags & CF_EOI))
1842 ic->flags |= (CF_EOI|CF_READ_PARTIAL);
1843
1844 /* Second step : update the stream-int and channels, try to forward any
1845 * pending data, then possibly wake the stream up based on the new
1846 * stream-int status.
1847 */
1848 cs_notify(cs);
1849 stream_release_buffers(__cs_strm(cs));
1850 return 0;
1851}
1852
1853/* This is the ->process() function for any conn-stream's wait_event task.
1854 * It's assigned during the stream-interface's initialization, for any type of
1855 * stream interface. Thus it is always safe to perform a tasklet_wakeup() on a
1856 * stream interface, as the presence of the CS is checked there.
1857 */
1858struct task *cs_conn_io_cb(struct task *t, void *ctx, unsigned int state)
1859{
1860 struct conn_stream *cs = ctx;
1861 int ret = 0;
1862
1863 if (!cs_conn(cs))
1864 return t;
1865
1866 if (!(cs->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(cs_oc(cs)))
1867 ret = cs_conn_send(cs);
1868 if (!(cs->wait_event.events & SUB_RETRY_RECV))
1869 ret |= cs_conn_recv(cs);
1870 if (ret != 0)
1871 cs_conn_process(cs);
1872
1873 stream_release_buffers(__cs_strm(cs));
1874 return t;
1875}
1876
1877/* Callback to be used by applet handlers upon completion. It updates the stream
1878 * (which may or may not take this opportunity to try to forward data), then
1879 * may re-enable the applet's based on the channels and stream interface's final
1880 * states.
1881 */
1882static int cs_applet_process(struct conn_stream *cs)
1883{
1884 struct channel *ic = cs_ic(cs);
1885
1886 BUG_ON(!cs_appctx(cs));
1887
1888 /* If the applet wants to write and the channel is closed, it's a
1889 * broken pipe and it must be reported.
1890 */
1891 if (!(cs->endp->flags & CS_EP_RX_WAIT_EP) && (ic->flags & CF_SHUTR))
1892 cs->endp->flags |= CS_EP_ERROR;
1893
1894 /* automatically mark the applet having data available if it reported
1895 * begin blocked by the channel.
1896 */
1897 if (cs_rx_blocked(cs))
1898 cs_rx_endp_more(cs);
1899
1900 /* update the stream-int, channels, and possibly wake the stream up */
1901 cs_notify(cs);
1902 stream_release_buffers(__cs_strm(cs));
1903
1904 /* cs_notify may have passed through chk_snd and released some
1905 * RXBLK flags. Process_stream will consider those flags to wake up the
1906 * appctx but in the case the task is not in runqueue we may have to
1907 * wakeup the appctx immediately.
1908 */
1909 if ((cs_rx_endp_ready(cs) && !cs_rx_blocked(cs)) ||
1910 (cs_tx_endp_ready(cs) && !cs_tx_blocked(cs)))
1911 appctx_wakeup(__cs_appctx(cs));
1912 return 0;
Christopher Faulet13045f02022-04-01 14:23:38 +02001913}