blob: 7b94812673cfccb512f2efc58987afacadb003eb [file] [log] [blame]
Willy Tarreaucff64112008-11-03 06:26:53 +01001/*
2 * Functions managing stream_interface structures
3 *
Willy Tarreauf873d752012-05-11 17:47:17 +02004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Willy Tarreaucff64112008-11-03 06:26:53 +01005 *
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 <errno.h>
14#include <fcntl.h>
15#include <stdio.h>
16#include <stdlib.h>
17
18#include <sys/socket.h>
19#include <sys/stat.h>
20#include <sys/types.h>
21
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020022#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020023#include <haproxy/applet.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020024#include <haproxy/channel.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020025#include <haproxy/connection.h>
Christopher Faulet0c6a64c2022-04-01 08:58:29 +020026#include <haproxy/conn_stream.h>
27#include <haproxy/cs_utils.h>
Willy Tarreau2741c8c2020-06-02 11:28:02 +020028#include <haproxy/dynbuf.h>
Willy Tarreaub7fc4c42021-10-06 18:56:42 +020029#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020030#include <haproxy/http_htx.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020031#include <haproxy/pipe-t.h>
32#include <haproxy/pipe.h>
Christopher Fauletcda94ac2021-12-23 17:28:17 +010033#include <haproxy/pool.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020034#include <haproxy/proxy.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020035#include <haproxy/stream-t.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020036#include <haproxy/stream_interface.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020037#include <haproxy/task.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020038#include <haproxy/ticks.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020039#include <haproxy/tools.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010040
Willy Tarreaufd31e532012-07-23 18:24:25 +020041
Christopher Fauletcda94ac2021-12-23 17:28:17 +010042DECLARE_POOL(pool_head_streaminterface, "stream_interface", sizeof(struct stream_interface));
43
44
Christopher Faulet0c6a64c2022-04-01 08:58:29 +020045/* functions used by default on a detached conn-stream */
46static void cs_app_shutr(struct conn_stream *cs);
47static void cs_app_shutw(struct conn_stream *cs);
48static void cs_app_chk_rcv(struct conn_stream *cs);
49static void cs_app_chk_snd(struct conn_stream *cs);
Willy Tarreau14bfe9a2018-12-19 15:19:27 +010050
Christopher Faulet0c6a64c2022-04-01 08:58:29 +020051/* functions used on a mux-based conn-stream */
52static void cs_app_shutr_conn(struct conn_stream *cs);
53static void cs_app_shutw_conn(struct conn_stream *cs);
54static void cs_app_chk_rcv_conn(struct conn_stream *cs);
55static void cs_app_chk_snd_conn(struct conn_stream *cs);
Willy Tarreau14bfe9a2018-12-19 15:19:27 +010056
Christopher Faulet0c6a64c2022-04-01 08:58:29 +020057/* functions used on an applet-based conn-stream */
58static void cs_app_shutr_applet(struct conn_stream *cs);
59static void cs_app_shutw_applet(struct conn_stream *cs);
60static void cs_app_chk_rcv_applet(struct conn_stream *cs);
61static void cs_app_chk_snd_applet(struct conn_stream *cs);
Willy Tarreau14bfe9a2018-12-19 15:19:27 +010062
63/* last read notification */
64static void stream_int_read0(struct stream_interface *si);
65
66/* post-IO notification callback */
67static void stream_int_notify(struct stream_interface *si);
Willy Tarreauf873d752012-05-11 17:47:17 +020068
Willy Tarreau5c979a92012-05-07 17:15:39 +020069
Christopher Faulet0c6a64c2022-04-01 08:58:29 +020070/* conn-stream operations for connections */
71struct cs_app_ops cs_app_conn_ops = {
72 .chk_rcv = cs_app_chk_rcv_conn,
73 .chk_snd = cs_app_chk_snd_conn,
74 .shutr = cs_app_shutr_conn,
75 .shutw = cs_app_shutw_conn,
Willy Tarreauc5788912012-08-24 18:12:41 +020076};
77
Christopher Faulet0c6a64c2022-04-01 08:58:29 +020078/* conn-stream operations for embedded tasks */
79struct cs_app_ops cs_app_embedded_ops = {
80 .chk_rcv = cs_app_chk_rcv,
81 .chk_snd = cs_app_chk_snd,
82 .shutr = cs_app_shutr,
83 .shutw = cs_app_shutw,
Willy Tarreaud45b9f82015-04-13 16:30:14 +020084};
85
Christopher Faulet0c6a64c2022-04-01 08:58:29 +020086/* conn-stream operations for connections */
87struct cs_app_ops cs_app_applet_ops = {
88 .chk_rcv = cs_app_chk_rcv_applet,
89 .chk_snd = cs_app_chk_snd_applet,
90 .shutr = cs_app_shutr_applet,
91 .shutw = cs_app_shutw_applet,
92};
Willy Tarreau14bfe9a2018-12-19 15:19:27 +010093
94/* Functions used to communicate with a conn_stream. The first two may be used
95 * directly, the last one is mostly a wake callback.
96 */
Christopher Faulet9936dc62022-02-28 09:21:58 +010097static int si_cs_recv(struct conn_stream *cs);
Christopher Faulet49416232022-02-28 09:14:46 +010098static int si_cs_send(struct conn_stream *cs);
Willy Tarreau14bfe9a2018-12-19 15:19:27 +010099static int si_cs_process(struct conn_stream *cs);
100
101
Willy Tarreau74beec32012-10-03 00:41:04 +0200102struct data_cb si_conn_cb = {
Olivier Houchard21df6cc2018-09-14 23:21:44 +0200103 .wake = si_cs_process,
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +0100104 .name = "STRM",
Willy Tarreauc5788912012-08-24 18:12:41 +0200105};
106
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100107
108struct stream_interface *si_new(struct conn_stream *cs)
109{
110 struct stream_interface *si;
111
112 si = pool_alloc(pool_head_streaminterface);
113 if (unlikely(!si))
114 return NULL;
115 si->flags = SI_FL_NONE;
Christopher Faulet014ac352022-01-06 08:13:46 +0100116 if (si_init(si) < 0) {
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100117 pool_free(pool_head_streaminterface, si);
118 return NULL;
119 }
120 si->cs = cs;
121 return si;
122}
123
124void si_free(struct stream_interface *si)
125{
126 if (!si)
127 return;
128
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100129 pool_free(pool_head_streaminterface, si);
130}
131
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200132
Willy Tarreaucff64112008-11-03 06:26:53 +0100133/*
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200134 * This function performs a shutdown-read on a detached conn-stream in a
Willy Tarreaud45b9f82015-04-13 16:30:14 +0200135 * connected or init state (it does nothing for other states). It either shuts
136 * the read side or marks itself as closed. The buffer flags are updated to
Christopher Faulet8abe7122022-03-30 15:10:18 +0200137 * reflect the new state. If the stream interface has CS_FL_NOHALF, we also
Willy Tarreaud45b9f82015-04-13 16:30:14 +0200138 * forward the close to the write side. The owner task is woken up if it exists.
Willy Tarreau4a36b562012-08-06 19:31:45 +0200139 */
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200140static void cs_app_shutr(struct conn_stream *cs)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200141{
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200142 struct channel *ic = cs_ic(cs);
Willy Tarreauafc8a222014-11-28 15:46:27 +0100143
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200144 si_rx_shut_blk(cs->si);
Willy Tarreauafc8a222014-11-28 15:46:27 +0100145 if (ic->flags & CF_SHUTR)
Willy Tarreau6fe15412013-09-29 15:16:03 +0200146 return;
Willy Tarreauafc8a222014-11-28 15:46:27 +0100147 ic->flags |= CF_SHUTR;
148 ic->rex = TICK_ETERNITY;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200149
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200150 if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
Willy Tarreau6fe15412013-09-29 15:16:03 +0200151 return;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200152
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200153 if (cs_oc(cs)->flags & CF_SHUTW) {
154 cs->state = CS_ST_DIS;
155 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
Willy Tarreaud8ccffe2010-09-07 16:16:50 +0200156 }
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200157 else if (cs->flags & CS_FL_NOHALF) {
Willy Tarreau4a36b562012-08-06 19:31:45 +0200158 /* we want to immediately forward this close to the write side */
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200159 return cs_app_shutw(cs);
Willy Tarreau4a36b562012-08-06 19:31:45 +0200160 }
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200161
Willy Tarreau4a36b562012-08-06 19:31:45 +0200162 /* note that if the task exists, it must unregister itself once it runs */
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200163 if (!(cs->flags & CS_FL_DONT_WAKE))
164 task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200165}
166
Willy Tarreau4a36b562012-08-06 19:31:45 +0200167/*
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200168 * This function performs a shutdown-write on a detached conn-stream in a
Willy Tarreaud45b9f82015-04-13 16:30:14 +0200169 * connected or init state (it does nothing for other states). It either shuts
170 * the write side or marks itself as closed. The buffer flags are updated to
171 * reflect the new state. It does also close everything if the SI was marked as
172 * being in error state. The owner task is woken up if it exists.
Willy Tarreau4a36b562012-08-06 19:31:45 +0200173 */
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200174static void cs_app_shutw(struct conn_stream *cs)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200175{
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200176 struct channel *ic = cs_ic(cs);
177 struct channel *oc = cs_oc(cs);
Willy Tarreauafc8a222014-11-28 15:46:27 +0100178
179 oc->flags &= ~CF_SHUTW_NOW;
180 if (oc->flags & CF_SHUTW)
Willy Tarreau6fe15412013-09-29 15:16:03 +0200181 return;
Willy Tarreauafc8a222014-11-28 15:46:27 +0100182 oc->flags |= CF_SHUTW;
183 oc->wex = TICK_ETERNITY;
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200184 si_done_get(cs->si);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200185
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200186 if (tick_isset(cs->hcto)) {
187 ic->rto = cs->hcto;
Hongbo Longe39683c2017-03-10 18:41:51 +0100188 ic->rex = tick_add(now_ms, ic->rto);
189 }
190
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200191 switch (cs->state) {
Christopher Faulet62e75742022-03-31 09:16:34 +0200192 case CS_ST_RDY:
193 case CS_ST_EST:
Willy Tarreau4a36b562012-08-06 19:31:45 +0200194 /* we have to shut before closing, otherwise some short messages
195 * may never leave the system, especially when there are remaining
196 * unread data in the socket input buffer, or when nolinger is set.
Christopher Faulet8abe7122022-03-30 15:10:18 +0200197 * However, if CS_FL_NOLINGER is explicitly set, we know there is
Willy Tarreau4a36b562012-08-06 19:31:45 +0200198 * no risk so we close both sides immediately.
199 */
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200200 if (!(cs->endp->flags & CS_EP_ERROR) && !(cs->flags & CS_FL_NOLINGER) &&
Willy Tarreauafc8a222014-11-28 15:46:27 +0100201 !(ic->flags & (CF_SHUTR|CF_DONT_READ)))
Willy Tarreau6fe15412013-09-29 15:16:03 +0200202 return;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200203
204 /* fall through */
Christopher Faulet62e75742022-03-31 09:16:34 +0200205 case CS_ST_CON:
206 case CS_ST_CER:
207 case CS_ST_QUE:
208 case CS_ST_TAR:
Willy Tarreau8b3d7df2013-09-29 14:51:58 +0200209 /* Note that none of these states may happen with applets */
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200210 cs->state = CS_ST_DIS;
Tim Duesterhus588b3142020-05-29 14:35:51 +0200211 /* fall through */
Willy Tarreaufb90d942009-09-05 20:57:35 +0200212 default:
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200213 cs->flags &= ~CS_FL_NOLINGER;
214 si_rx_shut_blk(cs->si);
Willy Tarreauafc8a222014-11-28 15:46:27 +0100215 ic->flags |= CF_SHUTR;
216 ic->rex = TICK_ETERNITY;
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200217 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200218 }
219
Willy Tarreau4a36b562012-08-06 19:31:45 +0200220 /* note that if the task exists, it must unregister itself once it runs */
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200221 if (!(cs->flags & CS_FL_DONT_WAKE))
222 task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200223}
224
225/* default chk_rcv function for scheduled tasks */
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200226static void cs_app_chk_rcv(struct conn_stream *cs)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200227{
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200228 struct channel *ic = cs_ic(cs);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200229
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200230 DPRINTF(stderr, "%s: cs=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n",
Willy Tarreaufb90d942009-09-05 20:57:35 +0200231 __FUNCTION__,
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200232 cs, cs->state, ic->flags, cs_oc(cs)->flags);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200233
Christopher Fauletb3e0de42018-10-11 13:54:13 +0200234 if (ic->pipe) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200235 /* stop reading */
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200236 si_rx_room_blk(cs->si);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200237 }
238 else {
239 /* (re)start reading */
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200240 if (!(cs->flags & CS_FL_DONT_WAKE))
241 task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200242 }
243}
244
245/* default chk_snd function for scheduled tasks */
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200246static void cs_app_chk_snd(struct conn_stream *cs)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200247{
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200248 struct channel *oc = cs_oc(cs);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200249
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200250 DPRINTF(stderr, "%s: cs=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n",
Willy Tarreaufb90d942009-09-05 20:57:35 +0200251 __FUNCTION__,
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200252 cs, cs->state, cs_ic(cs)->flags, oc->flags);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200253
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200254 if (unlikely(cs->state != CS_ST_EST || (oc->flags & CF_SHUTW)))
Willy Tarreaufb90d942009-09-05 20:57:35 +0200255 return;
256
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200257 if (!(cs->si->flags & SI_FL_WAIT_DATA) || /* not waiting for data */
258 channel_is_empty(oc)) /* called with nothing to send ! */
Willy Tarreaufb90d942009-09-05 20:57:35 +0200259 return;
260
261 /* Otherwise there are remaining data to be sent in the buffer,
262 * so we tell the handler.
263 */
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200264 cs->si->flags &= ~SI_FL_WAIT_DATA;
Willy Tarreauafc8a222014-11-28 15:46:27 +0100265 if (!tick_isset(oc->wex))
266 oc->wex = tick_add_ifset(now_ms, oc->wto);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200267
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200268 if (!(cs->flags & CS_FL_DONT_WAKE))
269 task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200270}
271
Willy Tarreaua9ff5e62015-07-19 18:46:30 +0200272/* Register an applet to handle a stream_interface as a new appctx. The SI will
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -0700273 * wake it up every time it is solicited. The appctx must be deleted by the task
Willy Tarreaua9ff5e62015-07-19 18:46:30 +0200274 * handler using si_release_endpoint(), possibly from within the function itself.
275 * It also pre-initializes the applet's context and returns it (or NULL in case
276 * it could not be allocated).
Willy Tarreaufb90d942009-09-05 20:57:35 +0200277 */
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100278struct appctx *si_register_handler(struct stream_interface *si, struct applet *app)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200279{
Willy Tarreau0a23bcb2013-12-01 11:31:38 +0100280 struct appctx *appctx;
281
Willy Tarreau07373b82014-11-28 12:08:47 +0100282 DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", app, si, si_task(si));
Willy Tarreaufb90d942009-09-05 20:57:35 +0200283
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100284 appctx = appctx_new(app, si->cs->endp);
Willy Tarreaua69fc9f2014-12-22 19:34:00 +0100285 if (!appctx)
Willy Tarreau0a23bcb2013-12-01 11:31:38 +0100286 return NULL;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100287 cs_attach_applet(si->cs, appctx, appctx);
288 appctx->owner = si->cs;
Christopher Faulet13a35e52021-12-20 15:34:16 +0100289 appctx->t->nice = si_strm(si)->task->nice;
Willy Tarreau0cd3bd62018-11-06 18:46:37 +0100290 si_cant_get(si);
Willy Tarreau828824a2015-04-19 17:20:03 +0200291 appctx_wakeup(appctx);
Christopher Faulet13a35e52021-12-20 15:34:16 +0100292 return appctx;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200293}
294
Willy Tarreau2c6be842012-07-06 17:12:34 +0200295/* This callback is used to send a valid PROXY protocol line to a socket being
Willy Tarreauafad0e02012-08-09 14:45:22 +0200296 * established. It returns 0 if it fails in a fatal way or needs to poll to go
297 * further, otherwise it returns non-zero and removes itself from the connection's
Willy Tarreaua1a74742012-08-24 12:14:49 +0200298 * flags (the bit is provided in <flag> by the caller). It is designed to be
299 * called by the connection handler and relies on it to commit polling changes.
Willy Tarreau57cd3e42013-10-24 22:01:26 +0200300 * Note that it can emit a PROXY line by relying on the other end's address
301 * when the connection is attached to a stream interface, or by resolving the
302 * local address otherwise (also called a LOCAL line).
Willy Tarreau2c6be842012-07-06 17:12:34 +0200303 */
304int conn_si_send_proxy(struct connection *conn, unsigned int flag)
305{
Willy Tarreaud02cdd22013-12-15 10:23:20 +0100306 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200307 goto out_error;
308
Willy Tarreau2c6be842012-07-06 17:12:34 +0200309 /* If we have a PROXY line to send, we'll use this to validate the
310 * connection, in which case the connection is validated only once
311 * we've sent the whole proxy line. Otherwise we use connect().
312 */
Tim Duesterhus36839dc2019-02-26 17:09:51 +0100313 if (conn->send_proxy_ofs) {
Willy Tarreau6b1379f2018-11-18 21:38:19 +0100314 const struct conn_stream *cs;
Willy Tarreau2c6be842012-07-06 17:12:34 +0200315 int ret;
316
Christopher Fauletd82056c2020-05-26 16:08:49 +0200317 /* If there is no mux attached to the connection, it means the
318 * connection context is a conn-stream.
319 */
Christopher Faulet69ef6c92022-03-31 14:20:00 +0200320 cs = (conn->mux ? cs_conn_get_first(conn) : conn->ctx);
Christopher Fauletd82056c2020-05-26 16:08:49 +0200321
Willy Tarreau2c6be842012-07-06 17:12:34 +0200322 /* The target server expects a PROXY line to be sent first.
323 * If the send_proxy_ofs is negative, it corresponds to the
324 * offset to start sending from then end of the proxy string
325 * (which is recomputed every time since it's constant). If
326 * it is positive, it means we have to send from the start.
Willy Tarreau57cd3e42013-10-24 22:01:26 +0200327 * We can only send a "normal" PROXY line when the connection
328 * is attached to a stream interface. Otherwise we can only
329 * send a LOCAL line (eg: for use with health checks).
Willy Tarreau2c6be842012-07-06 17:12:34 +0200330 */
Willy Tarreau6b1379f2018-11-18 21:38:19 +0100331
332 if (cs && cs->data_cb == &si_conn_cb) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200333 ret = make_proxy_line(trash.area, trash.size,
334 objt_server(conn->target),
Christopher Fauletf835dea2021-12-21 14:35:17 +0100335 cs_conn(si_opposite(cs_si(cs))->cs),
Christopher Faulet693b23b2022-02-28 09:09:05 +0100336 cs_strm(cs));
Willy Tarreau57cd3e42013-10-24 22:01:26 +0200337 }
338 else {
339 /* The target server expects a LOCAL line to be sent first. Retrieving
340 * local or remote addresses may fail until the connection is established.
341 */
Willy Tarreau7bb447c2019-07-17 11:40:51 +0200342 if (!conn_get_src(conn) || !conn_get_dst(conn))
Willy Tarreau57cd3e42013-10-24 22:01:26 +0200343 goto out_wait;
344
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200345 ret = make_proxy_line(trash.area, trash.size,
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +0100346 objt_server(conn->target), conn,
347 NULL);
Willy Tarreau57cd3e42013-10-24 22:01:26 +0200348 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200349
Willy Tarreau2c6be842012-07-06 17:12:34 +0200350 if (!ret)
351 goto out_error;
352
Willy Tarreaub8020ce2013-10-24 21:10:08 +0200353 if (conn->send_proxy_ofs > 0)
354 conn->send_proxy_ofs = -ret; /* first call */
Willy Tarreau2c6be842012-07-06 17:12:34 +0200355
Willy Tarreaua1a74742012-08-24 12:14:49 +0200356 /* we have to send trash from (ret+sp for -sp bytes). If the
357 * data layer has a pending write, we'll also set MSG_MORE.
358 */
Willy Tarreau827fee72020-12-11 15:26:55 +0100359 ret = conn_ctrl_send(conn,
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200360 trash.area + ret + conn->send_proxy_ofs,
361 -conn->send_proxy_ofs,
Willy Tarreau827fee72020-12-11 15:26:55 +0100362 (conn->subs && conn->subs->events & SUB_RETRY_SEND) ? CO_SFL_MSG_MORE : 0);
Willy Tarreau2c6be842012-07-06 17:12:34 +0200363
Willy Tarreau0a03c0f2015-03-13 00:05:28 +0100364 if (ret < 0)
Willy Tarreau2c6be842012-07-06 17:12:34 +0200365 goto out_error;
Willy Tarreau2c6be842012-07-06 17:12:34 +0200366
Willy Tarreaub8020ce2013-10-24 21:10:08 +0200367 conn->send_proxy_ofs += ret; /* becomes zero once complete */
368 if (conn->send_proxy_ofs != 0)
Willy Tarreau2c6be842012-07-06 17:12:34 +0200369 goto out_wait;
370
371 /* OK we've sent the whole line, we're connected */
372 }
373
Willy Tarreaua1a74742012-08-24 12:14:49 +0200374 /* The connection is ready now, simply return and let the connection
375 * handler notify upper layers if needed.
Willy Tarreau2c6be842012-07-06 17:12:34 +0200376 */
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100377 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau2c6be842012-07-06 17:12:34 +0200378 conn->flags &= ~flag;
Willy Tarreauafad0e02012-08-09 14:45:22 +0200379 return 1;
Willy Tarreau2c6be842012-07-06 17:12:34 +0200380
381 out_error:
Willy Tarreauafad0e02012-08-09 14:45:22 +0200382 /* Write error on the file descriptor */
Willy Tarreau2c6be842012-07-06 17:12:34 +0200383 conn->flags |= CO_FL_ERROR;
Willy Tarreauafad0e02012-08-09 14:45:22 +0200384 return 0;
Willy Tarreau2c6be842012-07-06 17:12:34 +0200385
386 out_wait:
Willy Tarreauafad0e02012-08-09 14:45:22 +0200387 return 0;
Willy Tarreau2c6be842012-07-06 17:12:34 +0200388}
389
Willy Tarreau27375622013-12-17 00:00:28 +0100390
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100391/* This function is the equivalent to si_update() except that it's
Willy Tarreau615f28b2015-09-23 18:40:09 +0200392 * designed to be called from outside the stream handlers, typically the lower
393 * layers (applets, connections) after I/O completion. After updating the stream
394 * interface and timeouts, it will try to forward what can be forwarded, then to
395 * wake the associated task up if an important event requires special handling.
Willy Tarreaud0f5bbc2018-11-14 11:10:26 +0100396 * It may update SI_FL_WAIT_DATA and/or SI_FL_RXBLK_ROOM, that the callers are
Willy Tarreau0dfccb22018-10-25 13:55:20 +0200397 * encouraged to watch to take appropriate action.
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100398 * It should not be called from within the stream itself, si_update()
Willy Tarreau615f28b2015-09-23 18:40:09 +0200399 * is designed for this.
400 */
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100401static void stream_int_notify(struct stream_interface *si)
Willy Tarreau615f28b2015-09-23 18:40:09 +0200402{
403 struct channel *ic = si_ic(si);
404 struct channel *oc = si_oc(si);
Willy Tarreau47baeb82018-11-15 07:46:57 +0100405 struct stream_interface *sio = si_opposite(si);
Christopher Fauletd7607de2019-01-03 16:24:54 +0100406 struct task *task = si_task(si);
Willy Tarreau615f28b2015-09-23 18:40:09 +0200407
408 /* process consumer side */
409 if (channel_is_empty(oc)) {
Christopher Faulet13a35e52021-12-20 15:34:16 +0100410 struct connection *conn = cs_conn(si->cs);
Olivier Houcharde9bed532017-11-16 17:49:25 +0100411
Willy Tarreau615f28b2015-09-23 18:40:09 +0200412 if (((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW) &&
Christopher Faulet62e75742022-03-31 09:16:34 +0200413 (si->cs->state == CS_ST_EST) && (!conn || !(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS))))
Christopher Fauletda098e62022-03-31 17:44:45 +0200414 cs_shutw(si->cs);
Willy Tarreau615f28b2015-09-23 18:40:09 +0200415 oc->wex = TICK_ETERNITY;
416 }
417
Willy Tarreau8cf9c8e2016-12-13 15:21:25 +0100418 /* indicate that we may be waiting for data from the output channel or
419 * we're about to close and can't expect more data if SHUTW_NOW is there.
420 */
Christopher Fauletb3e0de42018-10-11 13:54:13 +0200421 if (!(oc->flags & (CF_SHUTW|CF_SHUTW_NOW)))
Willy Tarreau615f28b2015-09-23 18:40:09 +0200422 si->flags |= SI_FL_WAIT_DATA;
Willy Tarreau8cf9c8e2016-12-13 15:21:25 +0100423 else if ((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW)
424 si->flags &= ~SI_FL_WAIT_DATA;
Willy Tarreau615f28b2015-09-23 18:40:09 +0200425
426 /* update OC timeouts and wake the other side up if it's waiting for room */
427 if (oc->flags & CF_WRITE_ACTIVITY) {
428 if ((oc->flags & (CF_SHUTW|CF_WRITE_PARTIAL)) == CF_WRITE_PARTIAL &&
429 !channel_is_empty(oc))
430 if (tick_isset(oc->wex))
431 oc->wex = tick_add_ifset(now_ms, oc->wto);
432
Christopher Fauleta7285182022-03-30 15:43:23 +0200433 if (!(si->cs->flags & CS_FL_INDEP_STR))
Willy Tarreau615f28b2015-09-23 18:40:09 +0200434 if (tick_isset(ic->rex))
435 ic->rex = tick_add_ifset(now_ms, ic->rto);
Willy Tarreauf26c26c2018-11-12 16:11:08 +0100436 }
Willy Tarreau615f28b2015-09-23 18:40:09 +0200437
Willy Tarreaub26a6f92018-11-14 17:10:36 +0100438 if (oc->flags & CF_DONT_READ)
439 si_rx_chan_blk(sio);
440 else
441 si_rx_chan_rdy(sio);
Willy Tarreau615f28b2015-09-23 18:40:09 +0200442
443 /* Notify the other side when we've injected data into the IC that
444 * needs to be forwarded. We can do fast-forwarding as soon as there
445 * are output data, but we avoid doing this if some of the data are
446 * not yet scheduled for being forwarded, because it is very likely
447 * that it will be done again immediately afterwards once the following
Willy Tarreaud0f5bbc2018-11-14 11:10:26 +0100448 * data are parsed (eg: HTTP chunking). We only SI_FL_RXBLK_ROOM once
Willy Tarreau615f28b2015-09-23 18:40:09 +0200449 * we've emptied *some* of the output buffer, and not just when there
450 * is available room, because applets are often forced to stop before
451 * the buffer is full. We must not stop based on input data alone because
452 * an HTTP parser might need more data to complete the parsing.
453 */
454 if (!channel_is_empty(ic) &&
Willy Tarreau47baeb82018-11-15 07:46:57 +0100455 (sio->flags & SI_FL_WAIT_DATA) &&
Willy Tarreau89b6a2b2018-11-18 15:46:10 +0100456 (!(ic->flags & CF_EXPECT_MORE) || c_full(ic) || ci_data(ic) == 0 || ic->pipe)) {
Willy Tarreau615f28b2015-09-23 18:40:09 +0200457 int new_len, last_len;
458
Willy Tarreau77e478c2018-06-19 07:03:14 +0200459 last_len = co_data(ic);
Willy Tarreau615f28b2015-09-23 18:40:09 +0200460 if (ic->pipe)
461 last_len += ic->pipe->data;
462
Christopher Fauletda098e62022-03-31 17:44:45 +0200463 cs_chk_snd(sio->cs);
Willy Tarreau615f28b2015-09-23 18:40:09 +0200464
Willy Tarreau77e478c2018-06-19 07:03:14 +0200465 new_len = co_data(ic);
Willy Tarreau615f28b2015-09-23 18:40:09 +0200466 if (ic->pipe)
467 new_len += ic->pipe->data;
468
469 /* check if the consumer has freed some space either in the
470 * buffer or in the pipe.
471 */
Willy Tarreau47baeb82018-11-15 07:46:57 +0100472 if (new_len < last_len)
Willy Tarreaudb398432018-11-15 11:08:52 +0100473 si_rx_room_rdy(si);
Willy Tarreau615f28b2015-09-23 18:40:09 +0200474 }
475
Willy Tarreaub26a6f92018-11-14 17:10:36 +0100476 if (!(ic->flags & CF_DONT_READ))
477 si_rx_chan_rdy(si);
478
Christopher Fauletda098e62022-03-31 17:44:45 +0200479 cs_chk_rcv(si->cs);
480 cs_chk_rcv(sio->cs);
Willy Tarreau47baeb82018-11-15 07:46:57 +0100481
Willy Tarreaub26a6f92018-11-14 17:10:36 +0100482 if (si_rx_blocked(si)) {
Willy Tarreau615f28b2015-09-23 18:40:09 +0200483 ic->rex = TICK_ETERNITY;
484 }
Willy Tarreaub26a6f92018-11-14 17:10:36 +0100485 else if ((ic->flags & (CF_SHUTR|CF_READ_PARTIAL)) == CF_READ_PARTIAL) {
Christopher Fauletda098e62022-03-31 17:44:45 +0200486 /* we must re-enable reading if cs_chk_snd() has freed some space */
Willy Tarreau615f28b2015-09-23 18:40:09 +0200487 if (!(ic->flags & CF_READ_NOEXP) && tick_isset(ic->rex))
488 ic->rex = tick_add_ifset(now_ms, ic->rto);
489 }
490
491 /* wake the task up only when needed */
492 if (/* changes on the production side */
493 (ic->flags & (CF_READ_NULL|CF_READ_ERROR)) ||
Christopher Faulet62e75742022-03-31 09:16:34 +0200494 !cs_state_in(si->cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST) ||
Christopher Faulet6cd56d52022-03-30 10:47:32 +0200495 (si->cs->endp->flags & CS_EP_ERROR) ||
Willy Tarreau615f28b2015-09-23 18:40:09 +0200496 ((ic->flags & CF_READ_PARTIAL) &&
Christopher Faulet62e75742022-03-31 09:16:34 +0200497 ((ic->flags & CF_EOI) || !ic->to_forward || sio->cs->state != CS_ST_EST)) ||
Willy Tarreau615f28b2015-09-23 18:40:09 +0200498
499 /* changes on the consumption side */
500 (oc->flags & (CF_WRITE_NULL|CF_WRITE_ERROR)) ||
Willy Tarreauede3d882018-10-24 17:17:56 +0200501 ((oc->flags & CF_WRITE_ACTIVITY) &&
Willy Tarreau615f28b2015-09-23 18:40:09 +0200502 ((oc->flags & CF_SHUTW) ||
Willy Tarreau78f5ff82018-12-19 11:00:00 +0100503 (((oc->flags & CF_WAKE_WRITE) ||
504 !(oc->flags & (CF_AUTO_CLOSE|CF_SHUTW_NOW|CF_SHUTW))) &&
Christopher Faulet62e75742022-03-31 09:16:34 +0200505 (sio->cs->state != CS_ST_EST ||
Willy Tarreau615f28b2015-09-23 18:40:09 +0200506 (channel_is_empty(oc) && !oc->to_forward)))))) {
Christopher Fauletd7607de2019-01-03 16:24:54 +0100507 task_wakeup(task, TASK_WOKEN_IO);
508 }
509 else {
510 /* Update expiration date for the task and requeue it */
511 task->expire = tick_first((tick_is_expired(task->expire, now_ms) ? 0 : task->expire),
512 tick_first(tick_first(ic->rex, ic->wex),
513 tick_first(oc->rex, oc->wex)));
Willy Tarreau45bcb372019-08-01 18:51:38 +0200514
515 task->expire = tick_first(task->expire, ic->analyse_exp);
516 task->expire = tick_first(task->expire, oc->analyse_exp);
Christopher Fauletae024ce2022-03-29 19:02:31 +0200517 task->expire = tick_first(task->expire, __cs_strm(si->cs)->conn_exp);
Willy Tarreau45bcb372019-08-01 18:51:38 +0200518
Christopher Fauletd7607de2019-01-03 16:24:54 +0100519 task_queue(task);
Willy Tarreau615f28b2015-09-23 18:40:09 +0200520 }
521 if (ic->flags & CF_READ_ACTIVITY)
522 ic->flags &= ~CF_READ_DONTWAIT;
Willy Tarreau615f28b2015-09-23 18:40:09 +0200523}
524
Willy Tarreaud1480cc2022-03-17 16:19:09 +0100525/* The stream interface is only responsible for the connection during the early
526 * states, before plugging a mux. Thus it should only care about CO_FL_ERROR
Christopher Faulet62e75742022-03-31 09:16:34 +0200527 * before CS_ST_EST, and after that it must absolutely ignore it since the mux
Willy Tarreaud1480cc2022-03-17 16:19:09 +0100528 * may hold pending data. This function returns true if such an error was
529 * reported. Both the CS and the CONN must be valid.
530 */
531static inline int si_is_conn_error(const struct stream_interface *si)
532{
533 struct connection *conn;
534
Christopher Faulet62e75742022-03-31 09:16:34 +0200535 if (si->cs->state >= CS_ST_EST)
Willy Tarreaud1480cc2022-03-17 16:19:09 +0100536 return 0;
537
538 conn = __cs_conn(si->cs);
539 BUG_ON(!conn);
540 return !!(conn->flags & CO_FL_ERROR);
541}
Willy Tarreau615f28b2015-09-23 18:40:09 +0200542
Olivier Houchardc2aa7112018-09-11 18:27:21 +0200543/* Called by I/O handlers after completion.. It propagates
Willy Tarreau651e1822015-09-23 20:06:13 +0200544 * connection flags to the stream interface, updates the stream (which may or
545 * may not take this opportunity to try to forward data), then update the
546 * connection's polling based on the channels and stream interface's final
547 * states. The function always returns 0.
Willy Tarreau100c4672012-08-20 12:06:26 +0200548 */
Olivier Houchardc2aa7112018-09-11 18:27:21 +0200549static int si_cs_process(struct conn_stream *cs)
Willy Tarreaufd31e532012-07-23 18:24:25 +0200550{
Christopher Faulet693b23b2022-02-28 09:09:05 +0100551 struct connection *conn = __cs_conn(cs);
Christopher Fauletf835dea2021-12-21 14:35:17 +0100552 struct stream_interface *si = cs_si(cs);
Willy Tarreauafc8a222014-11-28 15:46:27 +0100553 struct channel *ic = si_ic(si);
554 struct channel *oc = si_oc(si);
Willy Tarreaufd31e532012-07-23 18:24:25 +0200555
Christopher Faulet897d6122021-12-17 17:28:35 +0100556 BUG_ON(!conn);
557
Olivier Houchardc7ffa912018-08-28 19:37:41 +0200558 /* If we have data to send, try it now */
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200559 if (!channel_is_empty(oc) && !(si->cs->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau908d26f2018-10-25 14:02:47 +0200560 si_cs_send(cs);
561
Christopher Fauletaf642df2022-03-30 10:06:11 +0200562 /* First step, report to the conn-stream what was detected at the
Willy Tarreau651e1822015-09-23 20:06:13 +0200563 * connection layer : errors and connection establishment.
Christopher Faulet6cd56d52022-03-30 10:47:32 +0200564 * Only add CS_EP_ERROR if we're connected, or we're attempting to
Olivier Houchardc31e2cb2019-06-24 16:08:08 +0200565 * connect, we may get there because we got woken up, but only run
566 * after process_stream() noticed there were an error, and decided
567 * to retry to connect, the connection may still have CO_FL_ERROR,
Christopher Faulet6cd56d52022-03-30 10:47:32 +0200568 * and we don't want to add CS_EP_ERROR back
Christopher Faulet36b536d2019-11-20 11:56:33 +0100569 *
570 * Note: This test is only required because si_cs_process is also the SI
571 * wake callback. Otherwise si_cs_recv()/si_cs_send() already take
572 * care of it.
Willy Tarreau651e1822015-09-23 20:06:13 +0200573 */
Willy Tarreaud1480cc2022-03-17 16:19:09 +0100574
Christopher Faulet62e75742022-03-31 09:16:34 +0200575 if (si->cs->state >= CS_ST_CON) {
Christopher Faulet6cd56d52022-03-30 10:47:32 +0200576 if (si_is_conn_error(si))
577 cs->endp->flags |= CS_EP_ERROR;
Willy Tarreaud1480cc2022-03-17 16:19:09 +0100578 }
Willy Tarreau3c55ec22012-07-23 19:19:51 +0200579
Olivier Houchardccaa7de2017-10-02 11:51:03 +0200580 /* If we had early data, and the handshake ended, then
581 * we can remove the flag, and attempt to wake the task up,
582 * in the event there's an analyser waiting for the end of
583 * the handshake.
584 */
Willy Tarreau911db9b2020-01-23 16:27:54 +0100585 if (!(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS)) &&
Christopher Faulete9e48202022-03-22 18:13:29 +0100586 (cs->endp->flags & CS_EP_WAIT_FOR_HS)) {
587 cs->endp->flags &= ~CS_EP_WAIT_FOR_HS;
Olivier Houchardccaa7de2017-10-02 11:51:03 +0200588 task_wakeup(si_task(si), TASK_WOKEN_MSG);
589 }
590
Christopher Faulet62e75742022-03-31 09:16:34 +0200591 if (!cs_state_in(si->cs->state, CS_SB_EST|CS_SB_DIS|CS_SB_CLO) &&
Willy Tarreau911db9b2020-01-23 16:27:54 +0100592 (conn->flags & CO_FL_WAIT_XPRT) == 0) {
Christopher Fauletae024ce2022-03-29 19:02:31 +0200593 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
Willy Tarreauafc8a222014-11-28 15:46:27 +0100594 oc->flags |= CF_WRITE_NULL;
Christopher Faulet62e75742022-03-31 09:16:34 +0200595 if (si->cs->state == CS_ST_CON)
596 si->cs->state = CS_ST_RDY;
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200597 }
598
Christopher Faulet89e34c22021-01-21 16:22:01 +0100599 /* Report EOS on the channel if it was reached from the mux point of
600 * view.
601 *
602 * Note: This test is only required because si_cs_process is also the SI
603 * wake callback. Otherwise si_cs_recv()/si_cs_send() already take
604 * care of it.
605 */
Christopher Fauletb041b232022-03-24 10:27:02 +0100606 if (cs->endp->flags & CS_EP_EOS && !(ic->flags & CF_SHUTR)) {
Christopher Faulet89e34c22021-01-21 16:22:01 +0100607 /* we received a shutdown */
608 ic->flags |= CF_READ_NULL;
609 if (ic->flags & CF_AUTO_CLOSE)
610 channel_shutw_now(ic);
611 stream_int_read0(si);
612 }
613
Christopher Faulet297d3e22019-03-22 14:16:14 +0100614 /* Report EOI on the channel if it was reached from the mux point of
Christopher Faulet36b536d2019-11-20 11:56:33 +0100615 * view.
616 *
617 * Note: This test is only required because si_cs_process is also the SI
618 * wake callback. Otherwise si_cs_recv()/si_cs_send() already take
619 * care of it.
620 */
Christopher Fauletb041b232022-03-24 10:27:02 +0100621 if ((cs->endp->flags & CS_EP_EOI) && !(ic->flags & CF_EOI))
Christopher Faulet8e9e3ef2019-05-17 09:14:10 +0200622 ic->flags |= (CF_EOI|CF_READ_PARTIAL);
Christopher Faulet203b2b02019-03-08 09:23:46 +0100623
Willy Tarreau651e1822015-09-23 20:06:13 +0200624 /* Second step : update the stream-int and channels, try to forward any
625 * pending data, then possibly wake the stream up based on the new
626 * stream-int status.
Willy Tarreau44b5dc62012-08-24 12:12:53 +0200627 */
Willy Tarreau651e1822015-09-23 20:06:13 +0200628 stream_int_notify(si);
Willy Tarreaua64c7032019-08-01 14:17:02 +0200629 stream_release_buffers(si_strm(si));
Willy Tarreau2396c1c2012-10-03 21:12:16 +0200630 return 0;
Willy Tarreaufd31e532012-07-23 18:24:25 +0200631}
Willy Tarreau2c6be842012-07-06 17:12:34 +0200632
Willy Tarreau5368d802012-08-21 18:22:06 +0200633/*
634 * This function is called to send buffer data to a stream socket.
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200635 * It calls the mux layer's snd_buf function. It relies on the
Godbach4f489902013-12-04 17:24:06 +0800636 * caller to commit polling changes. The caller should check conn->flags
637 * for errors.
Willy Tarreau5368d802012-08-21 18:22:06 +0200638 */
Christopher Faulet49416232022-02-28 09:14:46 +0100639static int si_cs_send(struct conn_stream *cs)
Willy Tarreau5368d802012-08-21 18:22:06 +0200640{
Christopher Faulet693b23b2022-02-28 09:09:05 +0100641 struct connection *conn = __cs_conn(cs);
Christopher Fauletf835dea2021-12-21 14:35:17 +0100642 struct stream_interface *si = cs_si(cs);
Christopher Faulete05bf9e2022-03-29 15:23:40 +0200643 struct stream *s = si_strm(si);
Willy Tarreauafc8a222014-11-28 15:46:27 +0100644 struct channel *oc = si_oc(si);
Willy Tarreau5368d802012-08-21 18:22:06 +0200645 int ret;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200646 int did_send = 0;
647
Christopher Fauletb041b232022-03-24 10:27:02 +0100648 if (cs->endp->flags & (CS_EP_ERROR|CS_EP_ERR_PENDING) || si_is_conn_error(si)) {
Olivier Houchardc31e2cb2019-06-24 16:08:08 +0200649 /* We're probably there because the tasklet was woken up,
650 * but process_stream() ran before, detected there were an
Christopher Faulet62e75742022-03-31 09:16:34 +0200651 * error and put the si back to CS_ST_TAR. There's still
Olivier Houchardc31e2cb2019-06-24 16:08:08 +0200652 * CO_FL_ERROR on the connection but we don't want to add
Christopher Faulet6cd56d52022-03-30 10:47:32 +0200653 * CS_EP_ERROR back, so give up
Olivier Houchardc31e2cb2019-06-24 16:08:08 +0200654 */
Christopher Faulet62e75742022-03-31 09:16:34 +0200655 if (si->cs->state < CS_ST_CON)
Olivier Houchardc31e2cb2019-06-24 16:08:08 +0200656 return 0;
Christopher Faulet6cd56d52022-03-30 10:47:32 +0200657 cs->endp->flags |= CS_EP_ERROR;
Olivier Houchardc2aa7112018-09-11 18:27:21 +0200658 return 1;
Willy Tarreaubddf7fc2018-12-19 17:17:10 +0100659 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200660
Christopher Faulet328ed222019-09-23 15:57:29 +0200661 /* We're already waiting to be able to send, give up */
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200662 if (si->cs->wait_event.events & SUB_RETRY_SEND)
Christopher Faulet328ed222019-09-23 15:57:29 +0200663 return 0;
664
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200665 /* we might have been called just after an asynchronous shutw */
Willy Tarreauf22758d2020-01-23 18:25:23 +0100666 if (oc->flags & CF_SHUTW)
Olivier Houchardc2aa7112018-09-11 18:27:21 +0200667 return 1;
Willy Tarreau5368d802012-08-21 18:22:06 +0200668
Christopher Faulete96993b2020-07-30 09:26:46 +0200669 /* we must wait because the mux is not installed yet */
670 if (!conn->mux)
671 return 0;
672
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200673 if (oc->pipe && conn->xprt->snd_pipe && conn->mux->snd_pipe) {
674 ret = conn->mux->snd_pipe(cs, oc->pipe);
Christopher Faulet86162db2019-07-05 11:49:11 +0200675 if (ret > 0)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200676 did_send = 1;
Willy Tarreau5368d802012-08-21 18:22:06 +0200677
Willy Tarreauafc8a222014-11-28 15:46:27 +0100678 if (!oc->pipe->data) {
679 put_pipe(oc->pipe);
680 oc->pipe = NULL;
Willy Tarreau5368d802012-08-21 18:22:06 +0200681 }
682
Christopher Faulet3f76f4c2018-11-20 10:21:08 +0100683 if (oc->pipe)
684 goto end;
Willy Tarreau5368d802012-08-21 18:22:06 +0200685 }
686
687 /* At this point, the pipe is empty, but we may still have data pending
688 * in the normal buffer.
689 */
Christopher Faulet55dec0d2018-11-20 10:30:02 +0100690 if (co_data(oc)) {
691 /* when we're here, we already know that there is no spliced
692 * data left, and that there are sendable buffered data.
693 */
Willy Tarreau5368d802012-08-21 18:22:06 +0200694
Willy Tarreau5368d802012-08-21 18:22:06 +0200695 /* check if we want to inform the kernel that we're interested in
696 * sending more data after this call. We want this if :
697 * - we're about to close after this last send and want to merge
698 * the ongoing FIN with the last segment.
699 * - we know we can't send everything at once and must get back
700 * here because of unaligned data
701 * - there is still a finite amount of data to forward
702 * The test is arranged so that the most common case does only 2
703 * tests.
704 */
Willy Tarreau1049b1f2014-02-02 01:51:17 +0100705 unsigned int send_flag = 0;
Willy Tarreau5368d802012-08-21 18:22:06 +0200706
Willy Tarreauafc8a222014-11-28 15:46:27 +0100707 if ((!(oc->flags & (CF_NEVER_WAIT|CF_SEND_DONTWAIT)) &&
708 ((oc->to_forward && oc->to_forward != CHN_INFINITE_FORWARD) ||
Willy Tarreau8945bb62020-06-19 17:07:06 +0200709 (oc->flags & CF_EXPECT_MORE) ||
Christopher Faulet9e3dc832020-07-22 16:28:44 +0200710 (IS_HTX_STRM(si_strm(si)) &&
711 (!(oc->flags & (CF_EOI|CF_SHUTR)) && htx_expect_more(htxbuf(&oc->buf)))))) ||
Willy Tarreauecd2e152017-11-07 15:07:25 +0100712 ((oc->flags & CF_ISRESP) &&
713 ((oc->flags & (CF_AUTO_CLOSE|CF_SHUTW_NOW)) == (CF_AUTO_CLOSE|CF_SHUTW_NOW))))
Willy Tarreau1049b1f2014-02-02 01:51:17 +0100714 send_flag |= CO_SFL_MSG_MORE;
Willy Tarreau5368d802012-08-21 18:22:06 +0200715
Willy Tarreauafc8a222014-11-28 15:46:27 +0100716 if (oc->flags & CF_STREAMER)
Willy Tarreau7bed9452014-02-02 02:00:24 +0100717 send_flag |= CO_SFL_STREAMER;
718
Christopher Faulete05bf9e2022-03-29 15:23:40 +0200719 if (s->txn && s->txn->flags & TX_L7_RETRY && !b_data(&s->txn->l7_buffer)) {
Christopher Faulet9f5382e2021-05-21 13:46:14 +0200720 /* If we want to be able to do L7 retries, copy
721 * the data we're about to send, so that we are able
722 * to resend them if needed
723 */
724 /* Try to allocate a buffer if we had none.
725 * If it fails, the next test will just
726 * disable the l7 retries by setting
727 * l7_conn_retries to 0.
728 */
Christopher Faulete05bf9e2022-03-29 15:23:40 +0200729 if (s->txn->req.msg_state != HTTP_MSG_DONE)
730 s->txn->flags &= ~TX_L7_RETRY;
Christopher Faulet9f5382e2021-05-21 13:46:14 +0200731 else {
Christopher Faulete05bf9e2022-03-29 15:23:40 +0200732 if (b_alloc(&s->txn->l7_buffer) == NULL)
733 s->txn->flags &= ~TX_L7_RETRY;
Christopher Faulet9f5382e2021-05-21 13:46:14 +0200734 else {
Christopher Faulete05bf9e2022-03-29 15:23:40 +0200735 memcpy(b_orig(&s->txn->l7_buffer),
Christopher Faulet9f5382e2021-05-21 13:46:14 +0200736 b_orig(&oc->buf),
737 b_size(&oc->buf));
Christopher Faulete05bf9e2022-03-29 15:23:40 +0200738 s->txn->l7_buffer.head = co_data(oc);
739 b_add(&s->txn->l7_buffer, co_data(oc));
Christopher Faulet9f5382e2021-05-21 13:46:14 +0200740 }
741
742 }
743 }
744
Christopher Faulet897d6122021-12-17 17:28:35 +0100745 ret = conn->mux->snd_buf(cs, &oc->buf, co_data(oc), send_flag);
Godbache68e02d2013-10-11 15:48:29 +0800746 if (ret > 0) {
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200747 did_send = 1;
Willy Tarreau84240042022-02-28 16:51:23 +0100748 c_rew(oc, ret);
Willy Tarreaudeccd112018-06-14 18:38:55 +0200749 c_realign_if_empty(oc);
750
751 if (!co_data(oc)) {
Godbache68e02d2013-10-11 15:48:29 +0800752 /* Always clear both flags once everything has been sent, they're one-shot */
Willy Tarreauafc8a222014-11-28 15:46:27 +0100753 oc->flags &= ~(CF_EXPECT_MORE | CF_SEND_DONTWAIT);
Godbache68e02d2013-10-11 15:48:29 +0800754 }
Godbache68e02d2013-10-11 15:48:29 +0800755 /* if some data remain in the buffer, it's only because the
756 * system buffers are full, we will try next time.
757 */
Willy Tarreau5368d802012-08-21 18:22:06 +0200758 }
Godbache68e02d2013-10-11 15:48:29 +0800759 }
Christopher Faulet55dec0d2018-11-20 10:30:02 +0100760
Willy Tarreauf6975aa2018-11-15 14:33:05 +0100761 end:
Christopher Faulet86162db2019-07-05 11:49:11 +0200762 if (did_send) {
763 oc->flags |= CF_WRITE_PARTIAL | CF_WROTE_DATA;
Christopher Faulet62e75742022-03-31 09:16:34 +0200764 if (si->cs->state == CS_ST_CON)
765 si->cs->state = CS_ST_RDY;
Christopher Faulet037b3eb2019-07-05 13:44:29 +0200766
767 si_rx_room_rdy(si_opposite(si));
Christopher Faulet86162db2019-07-05 11:49:11 +0200768 }
769
Christopher Fauletb041b232022-03-24 10:27:02 +0100770 if (cs->endp->flags & (CS_EP_ERROR|CS_EP_ERR_PENDING)) {
Christopher Faulet6cd56d52022-03-30 10:47:32 +0200771 cs->endp->flags |= CS_EP_ERROR;
Christopher Faulet86162db2019-07-05 11:49:11 +0200772 return 1;
773 }
774
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200775 /* We couldn't send all of our data, let the mux know we'd like to send more */
Willy Tarreau691fe392018-11-12 18:48:52 +0100776 if (!channel_is_empty(oc))
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200777 conn->mux->subscribe(cs, SUB_RETRY_SEND, &si->cs->wait_event);
Olivier Houchardf6535282018-08-31 17:29:12 +0200778 return did_send;
Willy Tarreau5368d802012-08-21 18:22:06 +0200779}
780
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200781/* This is the ->process() function for any conn-stream's wait_event task.
Willy Tarreau8ccd2082018-11-07 07:47:52 +0100782 * It's assigned during the stream-interface's initialization, for any type of
783 * stream interface. Thus it is always safe to perform a tasklet_wakeup() on a
784 * stream interface, as the presence of the CS is checked there.
785 */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100786struct task *si_cs_io_cb(struct task *t, void *ctx, unsigned int state)
Olivier Houchard91894cb2018-08-02 18:06:28 +0200787{
Olivier Houchard8f0b4c62018-08-02 18:21:38 +0200788 struct stream_interface *si = ctx;
Christopher Faulet13a35e52021-12-20 15:34:16 +0100789 struct conn_stream *cs = si->cs;
Olivier Houchardf6535282018-08-31 17:29:12 +0200790 int ret = 0;
Olivier Houcharda6ff0352018-08-21 15:59:43 +0200791
Christopher Faulet0256da12021-12-15 09:50:17 +0100792 if (!cs_conn(cs))
Willy Tarreau74163142021-03-13 11:30:19 +0100793 return t;
Willy Tarreau8ccd2082018-11-07 07:47:52 +0100794
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200795 if (!(cs->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(si_oc(si)))
Olivier Houchardf6535282018-08-31 17:29:12 +0200796 ret = si_cs_send(cs);
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200797 if (!(cs->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardf6535282018-08-31 17:29:12 +0200798 ret |= si_cs_recv(cs);
799 if (ret != 0)
Olivier Houchardc2aa7112018-09-11 18:27:21 +0200800 si_cs_process(cs);
Olivier Houchardf6535282018-08-31 17:29:12 +0200801
Willy Tarreaua64c7032019-08-01 14:17:02 +0200802 stream_release_buffers(si_strm(si));
Willy Tarreau74163142021-03-13 11:30:19 +0100803 return t;
Olivier Houchard91894cb2018-08-02 18:06:28 +0200804}
805
Willy Tarreau25f13102015-09-24 11:32:22 +0200806/* This function is designed to be called from within the stream handler to
Willy Tarreau236c4292019-06-06 08:19:20 +0200807 * update the input channel's expiration timer and the stream interface's
808 * Rx flags based on the channel's flags. It needs to be called only once
809 * after the channel's flags have settled down, and before they are cleared,
810 * though it doesn't harm to call it as often as desired (it just slightly
811 * hurts performance). It must not be called from outside of the stream
812 * handler, as what it does will be used to compute the stream task's
813 * expiration.
Willy Tarreau25f13102015-09-24 11:32:22 +0200814 */
Willy Tarreau236c4292019-06-06 08:19:20 +0200815void si_update_rx(struct stream_interface *si)
Willy Tarreau25f13102015-09-24 11:32:22 +0200816{
817 struct channel *ic = si_ic(si);
Willy Tarreau25f13102015-09-24 11:32:22 +0200818
Willy Tarreau236c4292019-06-06 08:19:20 +0200819 if (ic->flags & CF_SHUTR) {
820 si_rx_shut_blk(si);
821 return;
822 }
Willy Tarreaub26a6f92018-11-14 17:10:36 +0100823
Willy Tarreau236c4292019-06-06 08:19:20 +0200824 /* Read not closed, update FD status and timeout for reads */
825 if (ic->flags & CF_DONT_READ)
826 si_rx_chan_blk(si);
827 else
828 si_rx_chan_rdy(si);
Willy Tarreaub26a6f92018-11-14 17:10:36 +0100829
Christopher Faulet69fad002021-10-29 14:55:59 +0200830 if (!channel_is_empty(ic) || !channel_may_recv(ic)) {
Willy Tarreau236c4292019-06-06 08:19:20 +0200831 /* stop reading, imposed by channel's policy or contents */
832 si_rx_room_blk(si);
Willy Tarreau25f13102015-09-24 11:32:22 +0200833 }
Willy Tarreau236c4292019-06-06 08:19:20 +0200834 else {
835 /* (re)start reading and update timeout. Note: we don't recompute the timeout
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -0700836 * every time we get here, otherwise it would risk never to expire. We only
Willy Tarreau236c4292019-06-06 08:19:20 +0200837 * update it if is was not yet set. The stream socket handler will already
838 * have updated it if there has been a completed I/O.
839 */
840 si_rx_room_rdy(si);
841 }
842 if (si->flags & SI_FL_RXBLK_ANY & ~SI_FL_RX_WAIT_EP)
843 ic->rex = TICK_ETERNITY;
844 else if (!(ic->flags & CF_READ_NOEXP) && !tick_isset(ic->rex))
845 ic->rex = tick_add_ifset(now_ms, ic->rto);
Willy Tarreau25f13102015-09-24 11:32:22 +0200846
Christopher Fauletda098e62022-03-31 17:44:45 +0200847 cs_chk_rcv(si->cs);
Willy Tarreau236c4292019-06-06 08:19:20 +0200848}
849
850/* This function is designed to be called from within the stream handler to
851 * update the output channel's expiration timer and the stream interface's
852 * Tx flags based on the channel's flags. It needs to be called only once
853 * after the channel's flags have settled down, and before they are cleared,
854 * though it doesn't harm to call it as often as desired (it just slightly
855 * hurts performance). It must not be called from outside of the stream
856 * handler, as what it does will be used to compute the stream task's
857 * expiration.
858 */
859void si_update_tx(struct stream_interface *si)
860{
861 struct channel *oc = si_oc(si);
862 struct channel *ic = si_ic(si);
863
864 if (oc->flags & CF_SHUTW)
865 return;
866
867 /* Write not closed, update FD status and timeout for writes */
868 if (channel_is_empty(oc)) {
869 /* stop writing */
870 if (!(si->flags & SI_FL_WAIT_DATA)) {
871 if ((oc->flags & CF_SHUTW_NOW) == 0)
872 si->flags |= SI_FL_WAIT_DATA;
873 oc->wex = TICK_ETERNITY;
Willy Tarreau25f13102015-09-24 11:32:22 +0200874 }
Willy Tarreau236c4292019-06-06 08:19:20 +0200875 return;
876 }
877
878 /* (re)start writing and update timeout. Note: we don't recompute the timeout
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -0700879 * every time we get here, otherwise it would risk never to expire. We only
Willy Tarreau236c4292019-06-06 08:19:20 +0200880 * update it if is was not yet set. The stream socket handler will already
881 * have updated it if there has been a completed I/O.
882 */
883 si->flags &= ~SI_FL_WAIT_DATA;
884 if (!tick_isset(oc->wex)) {
885 oc->wex = tick_add_ifset(now_ms, oc->wto);
Christopher Fauleta7285182022-03-30 15:43:23 +0200886 if (tick_isset(ic->rex) && !(si->cs->flags & CS_FL_INDEP_STR)) {
Willy Tarreau236c4292019-06-06 08:19:20 +0200887 /* Note: depending on the protocol, we don't know if we're waiting
888 * for incoming data or not. So in order to prevent the socket from
889 * expiring read timeouts during writes, we refresh the read timeout,
890 * except if it was already infinite or if we have explicitly setup
891 * independent streams.
Willy Tarreau25f13102015-09-24 11:32:22 +0200892 */
Willy Tarreau236c4292019-06-06 08:19:20 +0200893 ic->rex = tick_add_ifset(now_ms, ic->rto);
Willy Tarreau25f13102015-09-24 11:32:22 +0200894 }
895 }
896}
897
Christopher Faulet9936dc62022-02-28 09:21:58 +0100898/* This tries to perform a synchronous receive on the stream interface to
899 * try to collect last arrived data. In practice it's only implemented on
900 * conn_streams. Returns 0 if nothing was done, non-zero if new data or a
901 * shutdown were collected. This may result on some delayed receive calls
902 * to be programmed and performed later, though it doesn't provide any
903 * such guarantee.
904 */
905int si_sync_recv(struct stream_interface *si)
906{
Christopher Faulet62e75742022-03-31 09:16:34 +0200907 if (!cs_state_in(si->cs->state, CS_SB_RDY|CS_SB_EST))
Christopher Faulet9936dc62022-02-28 09:21:58 +0100908 return 0;
909
910 if (!cs_conn_mux(si->cs))
911 return 0; // only conn_streams are supported
912
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200913 if (si->cs->wait_event.events & SUB_RETRY_RECV)
Christopher Faulet9936dc62022-02-28 09:21:58 +0100914 return 0; // already subscribed
915
916 if (!si_rx_endp_ready(si) || si_rx_blocked(si))
917 return 0; // already failed
918
919 return si_cs_recv(si->cs);
920}
921
Willy Tarreau3b285d72019-06-06 08:20:17 +0200922/* perform a synchronous send() for the stream interface. The CF_WRITE_NULL and
923 * CF_WRITE_PARTIAL flags are cleared prior to the attempt, and will possibly
924 * be updated in case of success.
925 */
926void si_sync_send(struct stream_interface *si)
927{
928 struct channel *oc = si_oc(si);
Willy Tarreau3b285d72019-06-06 08:20:17 +0200929
930 oc->flags &= ~(CF_WRITE_NULL|CF_WRITE_PARTIAL);
931
932 if (oc->flags & CF_SHUTW)
933 return;
934
935 if (channel_is_empty(oc))
936 return;
937
Christopher Faulet62e75742022-03-31 09:16:34 +0200938 if (!cs_state_in(si->cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
Willy Tarreau3b285d72019-06-06 08:20:17 +0200939 return;
940
Christopher Faulet13a35e52021-12-20 15:34:16 +0100941 if (!cs_conn_mux(si->cs))
Willy Tarreau3b285d72019-06-06 08:20:17 +0200942 return;
943
Christopher Faulet13a35e52021-12-20 15:34:16 +0100944 si_cs_send(si->cs);
Willy Tarreau3b285d72019-06-06 08:20:17 +0200945}
946
Willy Tarreaud14844a2018-11-08 18:15:29 +0100947/* Updates at once the channel flags, and timers of both stream interfaces of a
948 * same stream, to complete the work after the analysers, then updates the data
949 * layer below. This will ensure that any synchronous update performed at the
950 * data layer will be reflected in the channel flags and/or stream-interface.
Willy Tarreau829bd472019-06-06 09:17:23 +0200951 * Note that this does not change the stream interface's current state, though
952 * it updates the previous state to the current one.
Willy Tarreaud14844a2018-11-08 18:15:29 +0100953 */
954void si_update_both(struct stream_interface *si_f, struct stream_interface *si_b)
955{
956 struct channel *req = si_ic(si_f);
957 struct channel *res = si_oc(si_f);
Willy Tarreaud14844a2018-11-08 18:15:29 +0100958
959 req->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_READ_ATTACHED|CF_WRITE_NULL|CF_WRITE_PARTIAL);
960 res->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_READ_ATTACHED|CF_WRITE_NULL|CF_WRITE_PARTIAL);
961
Christopher Faulet62e75742022-03-31 09:16:34 +0200962 si_strm(si_b)->prev_conn_state = si_b->cs->state;
Willy Tarreaud14844a2018-11-08 18:15:29 +0100963
Willy Tarreaubf89ff32018-11-09 14:59:25 +0100964 /* let's recompute both sides states */
Christopher Faulet62e75742022-03-31 09:16:34 +0200965 if (cs_state_in(si_f->cs->state, CS_SB_RDY|CS_SB_EST))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100966 si_update(si_f);
Willy Tarreaubf89ff32018-11-09 14:59:25 +0100967
Christopher Faulet62e75742022-03-31 09:16:34 +0200968 if (cs_state_in(si_b->cs->state, CS_SB_RDY|CS_SB_EST))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100969 si_update(si_b);
Willy Tarreaubf89ff32018-11-09 14:59:25 +0100970
971 /* stream ints are processed outside of process_stream() and must be
972 * handled at the latest moment.
973 */
Christopher Faulet13a35e52021-12-20 15:34:16 +0100974 if (cs_appctx(si_f->cs) &&
Willy Tarreau32742fd2018-11-14 14:07:59 +0100975 ((si_rx_endp_ready(si_f) && !si_rx_blocked(si_f)) ||
976 (si_tx_endp_ready(si_f) && !si_tx_blocked(si_f))))
Christopher Faulet693b23b2022-02-28 09:09:05 +0100977 appctx_wakeup(__cs_appctx(si_f->cs));
Willy Tarreaud14844a2018-11-08 18:15:29 +0100978
Christopher Faulet13a35e52021-12-20 15:34:16 +0100979 if (cs_appctx(si_b->cs) &&
Willy Tarreau32742fd2018-11-14 14:07:59 +0100980 ((si_rx_endp_ready(si_b) && !si_rx_blocked(si_b)) ||
981 (si_tx_endp_ready(si_b) && !si_tx_blocked(si_b))))
Christopher Faulet693b23b2022-02-28 09:09:05 +0100982 appctx_wakeup(__cs_appctx(si_b->cs));
Willy Tarreaud14844a2018-11-08 18:15:29 +0100983}
984
Willy Tarreau8b3d7df2013-09-29 14:51:58 +0200985/*
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200986 * This function performs a shutdown-read on a conn-stream attached to
Willy Tarreau8b3d7df2013-09-29 14:51:58 +0200987 * a connection in a connected or init state (it does nothing for other
988 * states). It either shuts the read side or marks itself as closed. The buffer
989 * flags are updated to reflect the new state. If the stream interface has
Christopher Faulet8abe7122022-03-30 15:10:18 +0200990 * CS_FL_NOHALF, we also forward the close to the write side. If a control
Willy Tarreau8b3d7df2013-09-29 14:51:58 +0200991 * layer is defined, then it is supposed to be a socket layer and file
Willy Tarreau6fe15412013-09-29 15:16:03 +0200992 * descriptors are then shutdown or closed accordingly. The function
993 * automatically disables polling if needed.
Willy Tarreau8b3d7df2013-09-29 14:51:58 +0200994 */
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200995static void cs_app_shutr_conn(struct conn_stream *cs)
Willy Tarreau8b3d7df2013-09-29 14:51:58 +0200996{
Christopher Faulet0c6a64c2022-04-01 08:58:29 +0200997 struct channel *ic = cs_ic(cs);
Willy Tarreau8b3d7df2013-09-29 14:51:58 +0200998
Christopher Faulet13a35e52021-12-20 15:34:16 +0100999 BUG_ON(!cs_conn(cs));
1000
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001001 si_rx_shut_blk(cs->si);
Willy Tarreauafc8a222014-11-28 15:46:27 +01001002 if (ic->flags & CF_SHUTR)
Willy Tarreau6fe15412013-09-29 15:16:03 +02001003 return;
Willy Tarreauafc8a222014-11-28 15:46:27 +01001004 ic->flags |= CF_SHUTR;
1005 ic->rex = TICK_ETERNITY;
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001006
Christopher Faulet62e75742022-03-31 09:16:34 +02001007 if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
Willy Tarreau6fe15412013-09-29 15:16:03 +02001008 return;
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001009
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001010 if (cs_oc(cs)->flags & CF_SHUTW) {
Christopher Faulet69ef6c92022-03-31 14:20:00 +02001011 cs_conn_close(cs);
Christopher Faulet62e75742022-03-31 09:16:34 +02001012 cs->state = CS_ST_DIS;
Christopher Fauletae024ce2022-03-29 19:02:31 +02001013 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001014 }
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001015 else if (cs->flags & CS_FL_NOHALF) {
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001016 /* we want to immediately forward this close to the write side */
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001017 return cs_app_shutw_conn(cs);
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001018 }
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001019}
1020
1021/*
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001022 * This function performs a shutdown-write on a conn-stream attached to
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001023 * a connection in a connected or init state (it does nothing for other
1024 * states). It either shuts the write side or marks itself as closed. The
1025 * buffer flags are updated to reflect the new state. It does also close
1026 * everything if the SI was marked as being in error state. If there is a
Willy Tarreau1398aa12015-03-12 23:04:07 +01001027 * data-layer shutdown, it is called.
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001028 */
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001029static void cs_app_shutw_conn(struct conn_stream *cs)
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001030{
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001031 struct channel *ic = cs_ic(cs);
1032 struct channel *oc = cs_oc(cs);
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001033
Christopher Faulet13a35e52021-12-20 15:34:16 +01001034 BUG_ON(!cs_conn(cs));
1035
Willy Tarreauafc8a222014-11-28 15:46:27 +01001036 oc->flags &= ~CF_SHUTW_NOW;
1037 if (oc->flags & CF_SHUTW)
Willy Tarreau6fe15412013-09-29 15:16:03 +02001038 return;
Willy Tarreauafc8a222014-11-28 15:46:27 +01001039 oc->flags |= CF_SHUTW;
1040 oc->wex = TICK_ETERNITY;
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001041 si_done_get(cs->si);
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001042
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001043 if (tick_isset(cs->hcto)) {
1044 ic->rto = cs->hcto;
Hongbo Longe39683c2017-03-10 18:41:51 +01001045 ic->rex = tick_add(now_ms, ic->rto);
1046 }
1047
Christopher Faulet62e75742022-03-31 09:16:34 +02001048 switch (cs->state) {
1049 case CS_ST_RDY:
1050 case CS_ST_EST:
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001051 /* we have to shut before closing, otherwise some short messages
1052 * may never leave the system, especially when there are remaining
1053 * unread data in the socket input buffer, or when nolinger is set.
Christopher Faulet8abe7122022-03-30 15:10:18 +02001054 * However, if CS_FL_NOLINGER is explicitly set, we know there is
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001055 * no risk so we close both sides immediately.
1056 */
Willy Tarreau51d0a7e2019-01-31 19:09:59 +01001057
Christopher Faulet6cd56d52022-03-30 10:47:32 +02001058 if (cs->endp->flags & CS_EP_ERROR) {
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001059 /* quick close, the socket is already shut anyway */
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001060 }
Christopher Faulet8abe7122022-03-30 15:10:18 +02001061 else if (cs->flags & CS_FL_NOLINGER) {
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001062 /* unclean data-layer shutdown, typically an aborted request
1063 * or a forwarded shutdown from a client to a server due to
1064 * option abortonclose. No need for the TLS layer to try to
1065 * emit a shutdown message.
1066 */
Christopher Faulet69ef6c92022-03-31 14:20:00 +02001067 cs_conn_shutw(cs, CO_SHW_SILENT);
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001068 }
1069 else {
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001070 /* clean data-layer shutdown. This only happens on the
1071 * frontend side, or on the backend side when forwarding
1072 * a client close in TCP mode or in HTTP TUNNEL mode
1073 * while option abortonclose is set. We want the TLS
1074 * layer to try to signal it to the peer before we close.
1075 */
Christopher Faulet69ef6c92022-03-31 14:20:00 +02001076 cs_conn_shutw(cs, CO_SHW_NORMAL);
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001077
Willy Tarreaua5ea7512020-12-11 10:24:05 +01001078 if (!(ic->flags & (CF_SHUTR|CF_DONT_READ)))
Willy Tarreaua553ae92017-10-05 18:52:17 +02001079 return;
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001080 }
1081
1082 /* fall through */
Christopher Faulet62e75742022-03-31 09:16:34 +02001083 case CS_ST_CON:
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001084 /* we may have to close a pending connection, and mark the
1085 * response buffer as shutr
1086 */
Christopher Faulet69ef6c92022-03-31 14:20:00 +02001087 cs_conn_close(cs);
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001088 /* fall through */
Christopher Faulet62e75742022-03-31 09:16:34 +02001089 case CS_ST_CER:
1090 case CS_ST_QUE:
1091 case CS_ST_TAR:
1092 cs->state = CS_ST_DIS;
Willy Tarreau4a59f2f2013-10-24 20:10:45 +02001093 /* fall through */
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001094 default:
Christopher Faulet8abe7122022-03-30 15:10:18 +02001095 cs->flags &= ~CS_FL_NOLINGER;
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001096 si_rx_shut_blk(cs->si);
Willy Tarreauafc8a222014-11-28 15:46:27 +01001097 ic->flags |= CF_SHUTR;
1098 ic->rex = TICK_ETERNITY;
Christopher Fauletae024ce2022-03-29 19:02:31 +02001099 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
Willy Tarreau100c4672012-08-20 12:06:26 +02001100 }
1101}
1102
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001103/* This function is used for inter-conn-stream calls. It is called by the
Willy Tarreau46a8d922012-08-20 12:38:36 +02001104 * consumer to inform the producer side that it may be interested in checking
1105 * for free space in the buffer. Note that it intentionally does not update
1106 * timeouts, so that we can still check them later at wake-up. This function is
1107 * dedicated to connection-based stream interfaces.
1108 */
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001109static void cs_app_chk_rcv_conn(struct conn_stream *cs)
Willy Tarreau46a8d922012-08-20 12:38:36 +02001110{
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001111 BUG_ON(!cs_conn(cs));
1112
Christopher Fauletb3e0de42018-10-11 13:54:13 +02001113 /* (re)start reading */
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001114 if (cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
1115 tasklet_wakeup(cs->wait_event.tasklet);
Willy Tarreau46a8d922012-08-20 12:38:36 +02001116}
1117
1118
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001119/* This function is used for inter-conn-stream calls. It is called by the
Willy Tarreaude5722c2012-08-20 15:01:10 +02001120 * producer to inform the consumer side that it may be interested in checking
1121 * for data in the buffer. Note that it intentionally does not update timeouts,
1122 * so that we can still check them later at wake-up.
1123 */
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001124static void cs_app_chk_snd_conn(struct conn_stream *cs)
Willy Tarreaude5722c2012-08-20 15:01:10 +02001125{
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001126 struct channel *oc = cs_oc(cs);
Christopher Faulet897d6122021-12-17 17:28:35 +01001127
Willy Tarreau0c3205a2022-03-23 11:11:31 +01001128 BUG_ON(!cs_conn(cs));
Willy Tarreaude5722c2012-08-20 15:01:10 +02001129
Christopher Faulet62e75742022-03-31 09:16:34 +02001130 if (unlikely(!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST) ||
Olivier Houchardb2fc04e2019-04-11 13:56:26 +02001131 (oc->flags & CF_SHUTW)))
Willy Tarreaude5722c2012-08-20 15:01:10 +02001132 return;
Willy Tarreaude5722c2012-08-20 15:01:10 +02001133
Willy Tarreauafc8a222014-11-28 15:46:27 +01001134 if (unlikely(channel_is_empty(oc))) /* called with nothing to send ! */
Willy Tarreaude5722c2012-08-20 15:01:10 +02001135 return;
1136
Willy Tarreauafc8a222014-11-28 15:46:27 +01001137 if (!oc->pipe && /* spliced data wants to be forwarded ASAP */
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001138 !(cs->si->flags & SI_FL_WAIT_DATA)) /* not waiting for data */
Willy Tarreaude5722c2012-08-20 15:01:10 +02001139 return;
1140
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001141 if (!(cs->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(cs_oc(cs)))
Olivier Houchard31f04e42018-10-22 16:01:09 +02001142 si_cs_send(cs);
Willy Tarreau33a09a52018-10-25 13:49:49 +02001143
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001144 if (cs->endp->flags & (CS_EP_ERROR|CS_EP_ERR_PENDING) || si_is_conn_error(cs->si)) {
Willy Tarreau3b9c8502017-10-25 14:22:28 +02001145 /* Write error on the file descriptor */
Christopher Faulet62e75742022-03-31 09:16:34 +02001146 if (cs->state >= CS_ST_CON)
Christopher Faulet6cd56d52022-03-30 10:47:32 +02001147 cs->endp->flags |= CS_EP_ERROR;
Willy Tarreau3b9c8502017-10-25 14:22:28 +02001148 goto out_wakeup;
Willy Tarreaude5722c2012-08-20 15:01:10 +02001149 }
1150
1151 /* OK, so now we know that some data might have been sent, and that we may
1152 * have to poll first. We have to do that too if the buffer is not empty.
1153 */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001154 if (channel_is_empty(oc)) {
Willy Tarreaude5722c2012-08-20 15:01:10 +02001155 /* the connection is established but we can't write. Either the
1156 * buffer is empty, or we just refrain from sending because the
1157 * ->o limit was reached. Maybe we just wrote the last
1158 * chunk and need to close.
1159 */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001160 if (((oc->flags & (CF_SHUTW|CF_AUTO_CLOSE|CF_SHUTW_NOW)) ==
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001161 (CF_AUTO_CLOSE|CF_SHUTW_NOW)) &&
Christopher Faulet62e75742022-03-31 09:16:34 +02001162 cs_state_in(cs->state, CS_SB_RDY|CS_SB_EST)) {
Christopher Fauletda098e62022-03-31 17:44:45 +02001163 cs_shutw(cs);
Willy Tarreaude5722c2012-08-20 15:01:10 +02001164 goto out_wakeup;
1165 }
1166
Willy Tarreauafc8a222014-11-28 15:46:27 +01001167 if ((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == 0)
Christopher Fauletda098e62022-03-31 17:44:45 +02001168 cs->si->flags |= SI_FL_WAIT_DATA;
Willy Tarreauafc8a222014-11-28 15:46:27 +01001169 oc->wex = TICK_ETERNITY;
Willy Tarreaude5722c2012-08-20 15:01:10 +02001170 }
1171 else {
1172 /* Otherwise there are remaining data to be sent in the buffer,
1173 * which means we have to poll before doing so.
1174 */
Christopher Fauletda098e62022-03-31 17:44:45 +02001175 cs->si->flags &= ~SI_FL_WAIT_DATA;
Willy Tarreauafc8a222014-11-28 15:46:27 +01001176 if (!tick_isset(oc->wex))
1177 oc->wex = tick_add_ifset(now_ms, oc->wto);
Willy Tarreaude5722c2012-08-20 15:01:10 +02001178 }
1179
Willy Tarreauafc8a222014-11-28 15:46:27 +01001180 if (likely(oc->flags & CF_WRITE_ACTIVITY)) {
Christopher Fauletda098e62022-03-31 17:44:45 +02001181 struct channel *ic = cs_ic(cs);
Willy Tarreauafc8a222014-11-28 15:46:27 +01001182
Willy Tarreaude5722c2012-08-20 15:01:10 +02001183 /* update timeout if we have written something */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001184 if ((oc->flags & (CF_SHUTW|CF_WRITE_PARTIAL)) == CF_WRITE_PARTIAL &&
1185 !channel_is_empty(oc))
1186 oc->wex = tick_add_ifset(now_ms, oc->wto);
Willy Tarreaude5722c2012-08-20 15:01:10 +02001187
Christopher Fauleta7285182022-03-30 15:43:23 +02001188 if (tick_isset(ic->rex) && !(cs->flags & CS_FL_INDEP_STR)) {
Willy Tarreaude5722c2012-08-20 15:01:10 +02001189 /* Note: to prevent the client from expiring read timeouts
1190 * during writes, we refresh it. We only do this if the
1191 * interface is not configured for "independent streams",
1192 * because for some applications it's better not to do this,
1193 * for instance when continuously exchanging small amounts
1194 * of data which can full the socket buffers long before a
1195 * write timeout is detected.
1196 */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001197 ic->rex = tick_add_ifset(now_ms, ic->rto);
Willy Tarreaude5722c2012-08-20 15:01:10 +02001198 }
1199 }
1200
1201 /* in case of special condition (error, shutdown, end of write...), we
1202 * have to notify the task.
1203 */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001204 if (likely((oc->flags & (CF_WRITE_NULL|CF_WRITE_ERROR|CF_SHUTW)) ||
1205 ((oc->flags & CF_WAKE_WRITE) &&
1206 ((channel_is_empty(oc) && !oc->to_forward) ||
Christopher Faulet62e75742022-03-31 09:16:34 +02001207 !cs_state_in(cs->state, CS_SB_EST))))) {
Willy Tarreaude5722c2012-08-20 15:01:10 +02001208 out_wakeup:
Christopher Fauletda098e62022-03-31 17:44:45 +02001209 if (!(cs->flags & CS_FL_DONT_WAKE))
1210 task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO);
Willy Tarreaude5722c2012-08-20 15:01:10 +02001211 }
1212}
1213
Willy Tarreaueecf6ca2012-08-20 15:09:53 +02001214/*
Willy Tarreauce323de2012-08-20 21:41:06 +02001215 * This is the callback which is called by the connection layer to receive data
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001216 * into the buffer from the connection. It iterates over the mux layer's
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001217 * rcv_buf function.
Willy Tarreauce323de2012-08-20 21:41:06 +02001218 */
Christopher Faulet9936dc62022-02-28 09:21:58 +01001219static int si_cs_recv(struct conn_stream *cs)
Willy Tarreauce323de2012-08-20 21:41:06 +02001220{
Christopher Faulet693b23b2022-02-28 09:09:05 +01001221 struct connection *conn = __cs_conn(cs);
Christopher Fauletf835dea2021-12-21 14:35:17 +01001222 struct stream_interface *si = cs_si(cs);
Willy Tarreauafc8a222014-11-28 15:46:27 +01001223 struct channel *ic = si_ic(si);
Olivier Houchardf6535282018-08-31 17:29:12 +02001224 int ret, max, cur_read = 0;
Willy Tarreauce323de2012-08-20 21:41:06 +02001225 int read_poll = MAX_READ_POLL_LOOPS;
Christopher Fauletc6618d62018-10-11 15:56:04 +02001226 int flags = 0;
Willy Tarreauce323de2012-08-20 21:41:06 +02001227
Christopher Faulet04400bc2019-10-25 10:21:01 +02001228 /* If not established yet, do nothing. */
Christopher Faulet62e75742022-03-31 09:16:34 +02001229 if (cs->state != CS_ST_EST)
Christopher Faulet04400bc2019-10-25 10:21:01 +02001230 return 0;
1231
Olivier Houchardf6535282018-08-31 17:29:12 +02001232 /* If another call to si_cs_recv() failed, and we subscribed to
1233 * recv events already, give up now.
1234 */
Christopher Faulet2f35e7b2022-03-31 11:09:28 +02001235 if (si->cs->wait_event.events & SUB_RETRY_RECV)
Olivier Houchardf6535282018-08-31 17:29:12 +02001236 return 0;
Willy Tarreauce323de2012-08-20 21:41:06 +02001237
Willy Tarreauce323de2012-08-20 21:41:06 +02001238 /* maybe we were called immediately after an asynchronous shutr */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001239 if (ic->flags & CF_SHUTR)
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001240 return 1;
Willy Tarreauce323de2012-08-20 21:41:06 +02001241
Christopher Faulete96993b2020-07-30 09:26:46 +02001242 /* we must wait because the mux is not installed yet */
1243 if (!conn->mux)
1244 return 0;
1245
Willy Tarreau54e917c2017-08-30 07:35:35 +02001246 /* stop here if we reached the end of data */
Christopher Fauletb041b232022-03-24 10:27:02 +01001247 if (cs->endp->flags & CS_EP_EOS)
Christopher Faulet36b536d2019-11-20 11:56:33 +01001248 goto end_recv;
Willy Tarreau54e917c2017-08-30 07:35:35 +02001249
Christopher Fauletf061e422018-12-07 14:51:20 +01001250 /* stop immediately on errors. Note that we DON'T want to stop on
1251 * POLL_ERR, as the poller might report a write error while there
1252 * are still data available in the recv buffer. This typically
1253 * happens when we send too large a request to a backend server
1254 * which rejects it before reading it all.
1255 */
Christopher Fauletb041b232022-03-24 10:27:02 +01001256 if (!(cs->endp->flags & CS_EP_RCV_MORE)) {
Christopher Fauletf061e422018-12-07 14:51:20 +01001257 if (!conn_xprt_ready(conn))
1258 return 0;
Christopher Fauletb041b232022-03-24 10:27:02 +01001259 if (cs->endp->flags & CS_EP_ERROR)
Christopher Faulet36b536d2019-11-20 11:56:33 +01001260 goto end_recv;
Christopher Fauletf061e422018-12-07 14:51:20 +01001261 }
Willy Tarreauf26c26c2018-11-12 16:11:08 +01001262
Willy Tarreau7ab99a32018-12-18 09:15:43 +01001263 /* prepare to detect if the mux needs more room */
Christopher Fauletb041b232022-03-24 10:27:02 +01001264 cs->endp->flags &= ~CS_EP_WANT_ROOM;
Willy Tarreau7ab99a32018-12-18 09:15:43 +01001265
Willy Tarreau77e478c2018-06-19 07:03:14 +02001266 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) && !co_data(ic) &&
Willy Tarreau7e312732014-02-12 16:35:14 +01001267 global.tune.idle_timer &&
Willy Tarreauafc8a222014-11-28 15:46:27 +01001268 (unsigned short)(now_ms - ic->last_read) >= global.tune.idle_timer) {
Willy Tarreauc5890e62014-02-09 17:47:01 +01001269 /* The buffer was empty and nothing was transferred for more
1270 * than one second. This was caused by a pause and not by
1271 * congestion. Reset any streaming mode to reduce latency.
1272 */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001273 ic->xfer_small = 0;
1274 ic->xfer_large = 0;
1275 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
Willy Tarreauc5890e62014-02-09 17:47:01 +01001276 }
1277
Willy Tarreau96199b12012-08-24 00:46:52 +02001278 /* First, let's see if we may splice data across the channel without
1279 * using a buffer.
1280 */
Christopher Faulete9e48202022-03-22 18:13:29 +01001281 if (cs->endp->flags & CS_EP_MAY_SPLICE &&
Willy Tarreauafc8a222014-11-28 15:46:27 +01001282 (ic->pipe || ic->to_forward >= MIN_SPLICE_FORWARD) &&
1283 ic->flags & CF_KERN_SPLICING) {
Willy Tarreaud760eec2018-07-10 09:50:25 +02001284 if (c_data(ic)) {
Willy Tarreau96199b12012-08-24 00:46:52 +02001285 /* We're embarrassed, there are already data pending in
1286 * the buffer and we don't want to have them at two
1287 * locations at a time. Let's indicate we need some
1288 * place and ask the consumer to hurry.
1289 */
Christopher Fauletc6618d62018-10-11 15:56:04 +02001290 flags |= CO_RFL_BUF_FLUSH;
Willy Tarreau96199b12012-08-24 00:46:52 +02001291 goto abort_splice;
1292 }
Willy Tarreauce323de2012-08-20 21:41:06 +02001293
Willy Tarreauafc8a222014-11-28 15:46:27 +01001294 if (unlikely(ic->pipe == NULL)) {
1295 if (pipes_used >= global.maxpipes || !(ic->pipe = get_pipe())) {
1296 ic->flags &= ~CF_KERN_SPLICING;
Willy Tarreau96199b12012-08-24 00:46:52 +02001297 goto abort_splice;
1298 }
1299 }
1300
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001301 ret = conn->mux->rcv_pipe(cs, ic->pipe, ic->to_forward);
Willy Tarreau96199b12012-08-24 00:46:52 +02001302 if (ret < 0) {
1303 /* splice not supported on this end, let's disable it */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001304 ic->flags &= ~CF_KERN_SPLICING;
Willy Tarreau96199b12012-08-24 00:46:52 +02001305 goto abort_splice;
1306 }
Willy Tarreauce323de2012-08-20 21:41:06 +02001307
Willy Tarreau96199b12012-08-24 00:46:52 +02001308 if (ret > 0) {
Willy Tarreauafc8a222014-11-28 15:46:27 +01001309 if (ic->to_forward != CHN_INFINITE_FORWARD)
1310 ic->to_forward -= ret;
1311 ic->total += ret;
Willy Tarreau96199b12012-08-24 00:46:52 +02001312 cur_read += ret;
Willy Tarreauafc8a222014-11-28 15:46:27 +01001313 ic->flags |= CF_READ_PARTIAL;
Willy Tarreauce323de2012-08-20 21:41:06 +02001314 }
Willy Tarreau96199b12012-08-24 00:46:52 +02001315
Christopher Fauletb041b232022-03-24 10:27:02 +01001316 if (cs->endp->flags & (CS_EP_EOS|CS_EP_ERROR))
Christopher Faulet36b536d2019-11-20 11:56:33 +01001317 goto end_recv;
Willy Tarreau96199b12012-08-24 00:46:52 +02001318
Willy Tarreau61d39a02013-07-18 21:49:32 +02001319 if (conn->flags & CO_FL_WAIT_ROOM) {
1320 /* the pipe is full or we have read enough data that it
1321 * could soon be full. Let's stop before needing to poll.
1322 */
Willy Tarreaudb398432018-11-15 11:08:52 +01001323 si_rx_room_blk(si);
Willy Tarreauffb12052018-11-15 16:06:02 +01001324 goto done_recv;
Willy Tarreau61d39a02013-07-18 21:49:32 +02001325 }
Willy Tarreau56a77e52012-09-02 18:34:44 +02001326
Willy Tarreauce323de2012-08-20 21:41:06 +02001327 /* splice not possible (anymore), let's go on on standard copy */
1328 }
Willy Tarreau96199b12012-08-24 00:46:52 +02001329
1330 abort_splice:
Willy Tarreauafc8a222014-11-28 15:46:27 +01001331 if (ic->pipe && unlikely(!ic->pipe->data)) {
1332 put_pipe(ic->pipe);
1333 ic->pipe = NULL;
Willy Tarreau96199b12012-08-24 00:46:52 +02001334 }
1335
Christopher Faulete9e48202022-03-22 18:13:29 +01001336 if (ic->pipe && ic->to_forward && !(flags & CO_RFL_BUF_FLUSH) && cs->endp->flags & CS_EP_MAY_SPLICE) {
Willy Tarreauc640ef12019-12-03 18:13:04 +01001337 /* don't break splicing by reading, but still call rcv_buf()
1338 * to pass the flag.
1339 */
1340 goto done_recv;
1341 }
1342
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001343 /* now we'll need a input buffer for the stream */
Willy Tarreau581abd32018-10-25 10:21:41 +02001344 if (!si_alloc_ibuf(si, &(si_strm(si)->buffer_wait)))
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001345 goto end_recv;
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001346
Christopher Faulet2bc364c2021-09-21 15:22:12 +02001347 /* For an HTX stream, if the buffer is stuck (no output data with some
1348 * input data) and if the HTX message is fragmented or if its free space
1349 * wraps, we force an HTX deframentation. It is a way to have a
1350 * contiguous free space nad to let the mux to copy as much data as
1351 * possible.
1352 *
1353 * NOTE: A possible optim may be to let the mux decides if defrag is
1354 * required or not, depending on amount of data to be xferred.
1355 */
1356 if (IS_HTX_STRM(si_strm(si)) && !co_data(ic)) {
1357 struct htx *htx = htxbuf(&ic->buf);
1358
1359 if (htx_is_not_empty(htx) && ((htx->flags & HTX_FL_FRAGMENTED) || htx_space_wraps(htx)))
1360 htx_defrag(htxbuf(&ic->buf), NULL, 0);
1361 }
Christopher Faulet68a14db2021-09-21 15:14:57 +02001362
1363 /* Instruct the mux it must subscribed for read events */
1364 flags |= ((!conn_is_back(conn) && (si_strm(si)->be->options & PR_O_ABRT_CLOSE)) ? CO_RFL_KEEP_RECV : 0);
1365
Willy Tarreau61d39a02013-07-18 21:49:32 +02001366 /* Important note : if we're called with POLL_IN|POLL_HUP, it means the read polling
1367 * was enabled, which implies that the recv buffer was not full. So we have a guarantee
1368 * that if such an event is not handled above in splice, it will be handled here by
1369 * recv().
1370 */
Christopher Fauletb041b232022-03-24 10:27:02 +01001371 while ((cs->endp->flags & CS_EP_RCV_MORE) ||
Willy Tarreaud1480cc2022-03-17 16:19:09 +01001372 (!(conn->flags & CO_FL_HANDSHAKE) &&
Christopher Fauletb041b232022-03-24 10:27:02 +01001373 (!(cs->endp->flags & (CS_EP_ERROR|CS_EP_EOS))) && !(ic->flags & CF_SHUTR))) {
Christopher Faulet68a14db2021-09-21 15:14:57 +02001374 int cur_flags = flags;
1375
1376 /* Compute transient CO_RFL_* flags */
Christopher Faulet564e39c2021-09-21 15:50:55 +02001377 if (co_data(ic)) {
1378 cur_flags |= (CO_RFL_BUF_WET | CO_RFL_BUF_NOT_STUCK);
1379 }
Christopher Faulet68a14db2021-09-21 15:14:57 +02001380
Christopher Faulet4eb7d742018-10-11 15:29:21 +02001381 /* <max> may be null. This is the mux responsibility to set
Christopher Fauletb041b232022-03-24 10:27:02 +01001382 * CS_EP_RCV_MORE on the CS if more space is needed.
Christopher Faulet4eb7d742018-10-11 15:29:21 +02001383 */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001384 max = channel_recv_max(ic);
Christopher Faulet897d6122021-12-17 17:28:35 +01001385 ret = conn->mux->rcv_buf(cs, &ic->buf, max, cur_flags);
Willy Tarreau674e0ad2018-12-05 13:45:41 +01001386
Christopher Fauletb041b232022-03-24 10:27:02 +01001387 if (cs->endp->flags & CS_EP_WANT_ROOM) {
1388 /* CS_EP_WANT_ROOM must not be reported if the channel's
Christopher Fauletae179252022-02-21 16:12:00 +01001389 * buffer is empty.
1390 */
1391 BUG_ON(c_empty(ic));
1392
Willy Tarreaudb398432018-11-15 11:08:52 +01001393 si_rx_room_blk(si);
Christopher Fauletdf994082021-09-23 14:17:20 +02001394 /* Add READ_PARTIAL because some data are pending but
1395 * cannot be xferred to the channel
1396 */
1397 ic->flags |= CF_READ_PARTIAL;
1398 }
Willy Tarreau6577b482017-12-10 21:19:33 +01001399
Willy Tarreauf26c26c2018-11-12 16:11:08 +01001400 if (ret <= 0) {
Willy Tarreau1ac5f202019-12-03 18:08:45 +01001401 /* if we refrained from reading because we asked for a
1402 * flush to satisfy rcv_pipe(), we must not subscribe
1403 * and instead report that there's not enough room
1404 * here to proceed.
1405 */
1406 if (flags & CO_RFL_BUF_FLUSH)
1407 si_rx_room_blk(si);
Willy Tarreauce323de2012-08-20 21:41:06 +02001408 break;
Willy Tarreauf26c26c2018-11-12 16:11:08 +01001409 }
Willy Tarreauce323de2012-08-20 21:41:06 +02001410
1411 cur_read += ret;
1412
1413 /* if we're allowed to directly forward data, we must update ->o */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001414 if (ic->to_forward && !(ic->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreauce323de2012-08-20 21:41:06 +02001415 unsigned long fwd = ret;
Willy Tarreauafc8a222014-11-28 15:46:27 +01001416 if (ic->to_forward != CHN_INFINITE_FORWARD) {
1417 if (fwd > ic->to_forward)
1418 fwd = ic->to_forward;
1419 ic->to_forward -= fwd;
Willy Tarreauce323de2012-08-20 21:41:06 +02001420 }
Willy Tarreaubcbd3932018-06-06 07:13:22 +02001421 c_adv(ic, fwd);
Willy Tarreauce323de2012-08-20 21:41:06 +02001422 }
1423
Willy Tarreauafc8a222014-11-28 15:46:27 +01001424 ic->flags |= CF_READ_PARTIAL;
1425 ic->total += ret;
Willy Tarreauce323de2012-08-20 21:41:06 +02001426
Christopher Faulet883d83e2021-09-09 10:17:59 +02001427 /* End-of-input reached, we can leave. In this case, it is
1428 * important to break the loop to not block the SI because of
1429 * the channel's policies.This way, we are still able to receive
1430 * shutdowns.
1431 */
Christopher Fauletb041b232022-03-24 10:27:02 +01001432 if (cs->endp->flags & CS_EP_EOI)
Christopher Faulet883d83e2021-09-09 10:17:59 +02001433 break;
1434
Willy Tarreauf26c26c2018-11-12 16:11:08 +01001435 if ((ic->flags & CF_READ_DONTWAIT) || --read_poll <= 0) {
1436 /* we're stopped by the channel's policy */
Willy Tarreaub26a6f92018-11-14 17:10:36 +01001437 si_rx_chan_blk(si);
Willy Tarreau62dd6982017-11-18 11:26:20 +01001438 break;
Willy Tarreauf26c26c2018-11-12 16:11:08 +01001439 }
Willy Tarreauce323de2012-08-20 21:41:06 +02001440
1441 /* if too many bytes were missing from last read, it means that
1442 * it's pointless trying to read again because the system does
1443 * not have them in buffers.
1444 */
1445 if (ret < max) {
Willy Tarreauce323de2012-08-20 21:41:06 +02001446 /* if a streamer has read few data, it may be because we
1447 * have exhausted system buffers. It's not worth trying
1448 * again.
1449 */
Willy Tarreauf26c26c2018-11-12 16:11:08 +01001450 if (ic->flags & CF_STREAMER) {
1451 /* we're stopped by the channel's policy */
Willy Tarreaub26a6f92018-11-14 17:10:36 +01001452 si_rx_chan_blk(si);
Willy Tarreauce323de2012-08-20 21:41:06 +02001453 break;
Willy Tarreauf26c26c2018-11-12 16:11:08 +01001454 }
Willy Tarreauce323de2012-08-20 21:41:06 +02001455
1456 /* if we read a large block smaller than what we requested,
1457 * it's almost certain we'll never get anything more.
1458 */
Willy Tarreauf26c26c2018-11-12 16:11:08 +01001459 if (ret >= global.tune.recv_enough) {
1460 /* we're stopped by the channel's policy */
Willy Tarreaub26a6f92018-11-14 17:10:36 +01001461 si_rx_chan_blk(si);
Willy Tarreauce323de2012-08-20 21:41:06 +02001462 break;
Willy Tarreauf26c26c2018-11-12 16:11:08 +01001463 }
Willy Tarreauce323de2012-08-20 21:41:06 +02001464 }
Christopher Fauletb3e0de42018-10-11 13:54:13 +02001465
1466 /* if we are waiting for more space, don't try to read more data
1467 * right now.
1468 */
Willy Tarreaub26a6f92018-11-14 17:10:36 +01001469 if (si_rx_blocked(si))
Christopher Fauletb3e0de42018-10-11 13:54:13 +02001470 break;
Willy Tarreauce323de2012-08-20 21:41:06 +02001471 } /* while !flags */
1472
Willy Tarreauffb12052018-11-15 16:06:02 +01001473 done_recv:
Willy Tarreauc5890e62014-02-09 17:47:01 +01001474 if (cur_read) {
Willy Tarreauafc8a222014-11-28 15:46:27 +01001475 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) &&
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001476 (cur_read <= ic->buf.size / 2)) {
Willy Tarreauafc8a222014-11-28 15:46:27 +01001477 ic->xfer_large = 0;
1478 ic->xfer_small++;
1479 if (ic->xfer_small >= 3) {
Willy Tarreauc5890e62014-02-09 17:47:01 +01001480 /* we have read less than half of the buffer in
1481 * one pass, and this happened at least 3 times.
1482 * This is definitely not a streamer.
1483 */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001484 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
Willy Tarreauc5890e62014-02-09 17:47:01 +01001485 }
Willy Tarreauafc8a222014-11-28 15:46:27 +01001486 else if (ic->xfer_small >= 2) {
Willy Tarreauc5890e62014-02-09 17:47:01 +01001487 /* if the buffer has been at least half full twice,
1488 * we receive faster than we send, so at least it
1489 * is not a "fast streamer".
1490 */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001491 ic->flags &= ~CF_STREAMER_FAST;
Willy Tarreauc5890e62014-02-09 17:47:01 +01001492 }
1493 }
Willy Tarreauafc8a222014-11-28 15:46:27 +01001494 else if (!(ic->flags & CF_STREAMER_FAST) &&
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001495 (cur_read >= ic->buf.size - global.tune.maxrewrite)) {
Willy Tarreauc5890e62014-02-09 17:47:01 +01001496 /* we read a full buffer at once */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001497 ic->xfer_small = 0;
1498 ic->xfer_large++;
1499 if (ic->xfer_large >= 3) {
Willy Tarreauc5890e62014-02-09 17:47:01 +01001500 /* we call this buffer a fast streamer if it manages
1501 * to be filled in one call 3 consecutive times.
1502 */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001503 ic->flags |= (CF_STREAMER | CF_STREAMER_FAST);
Willy Tarreauc5890e62014-02-09 17:47:01 +01001504 }
1505 }
1506 else {
Willy Tarreauafc8a222014-11-28 15:46:27 +01001507 ic->xfer_small = 0;
1508 ic->xfer_large = 0;
Willy Tarreauc5890e62014-02-09 17:47:01 +01001509 }
Willy Tarreauafc8a222014-11-28 15:46:27 +01001510 ic->last_read = now_ms;
Willy Tarreauc5890e62014-02-09 17:47:01 +01001511 }
1512
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001513 end_recv:
Christopher Faulete6d8cb12019-11-20 16:42:00 +01001514 ret = (cur_read != 0);
1515
Christopher Faulet36b536d2019-11-20 11:56:33 +01001516 /* Report EOI on the channel if it was reached from the mux point of
1517 * view. */
Christopher Fauletb041b232022-03-24 10:27:02 +01001518 if ((cs->endp->flags & CS_EP_EOI) && !(ic->flags & CF_EOI)) {
Christopher Faulet36b536d2019-11-20 11:56:33 +01001519 ic->flags |= (CF_EOI|CF_READ_PARTIAL);
Christopher Faulete6d8cb12019-11-20 16:42:00 +01001520 ret = 1;
1521 }
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001522
Christopher Faulet6cd56d52022-03-30 10:47:32 +02001523 if (cs->endp->flags & CS_EP_ERROR)
Christopher Faulete6d8cb12019-11-20 16:42:00 +01001524 ret = 1;
Christopher Fauletb041b232022-03-24 10:27:02 +01001525 else if (cs->endp->flags & CS_EP_EOS) {
Willy Tarreau18955db2020-01-23 16:32:24 +01001526 /* we received a shutdown */
1527 ic->flags |= CF_READ_NULL;
1528 if (ic->flags & CF_AUTO_CLOSE)
1529 channel_shutw_now(ic);
1530 stream_int_read0(si);
Christopher Faulete6d8cb12019-11-20 16:42:00 +01001531 ret = 1;
Christopher Faulet36b536d2019-11-20 11:56:33 +01001532 }
1533 else if (!si_rx_blocked(si)) {
1534 /* Subscribe to receive events if we're blocking on I/O */
Christopher Faulet2f35e7b2022-03-31 11:09:28 +02001535 conn->mux->subscribe(cs, SUB_RETRY_RECV, &si->cs->wait_event);
Willy Tarreaudd5621a2018-11-15 16:55:14 +01001536 si_rx_endp_done(si);
1537 } else {
1538 si_rx_endp_more(si);
Christopher Faulete6d8cb12019-11-20 16:42:00 +01001539 ret = 1;
Willy Tarreaudd5621a2018-11-15 16:55:14 +01001540 }
Christopher Faulete6d8cb12019-11-20 16:42:00 +01001541 return ret;
Willy Tarreauce323de2012-08-20 21:41:06 +02001542}
1543
1544/*
Willy Tarreau9bf9c142012-08-20 15:38:41 +02001545 * This function propagates a null read received on a socket-based connection.
Christopher Faulet8abe7122022-03-30 15:10:18 +02001546 * It updates the stream interface. If the stream interface has CS_FL_NOHALF,
Willy Tarreau11405122015-03-12 22:32:27 +01001547 * the close is also forwarded to the write side as an abort.
Willy Tarreau9bf9c142012-08-20 15:38:41 +02001548 */
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001549static void stream_int_read0(struct stream_interface *si)
Willy Tarreau9bf9c142012-08-20 15:38:41 +02001550{
Christopher Faulet13a35e52021-12-20 15:34:16 +01001551 struct conn_stream *cs = si->cs;
Willy Tarreauafc8a222014-11-28 15:46:27 +01001552 struct channel *ic = si_ic(si);
1553 struct channel *oc = si_oc(si);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001554
Christopher Faulet13a35e52021-12-20 15:34:16 +01001555 BUG_ON(!cs_conn(cs));
1556
Willy Tarreauabb5d422018-11-14 16:58:52 +01001557 si_rx_shut_blk(si);
Willy Tarreauafc8a222014-11-28 15:46:27 +01001558 if (ic->flags & CF_SHUTR)
Willy Tarreau9bf9c142012-08-20 15:38:41 +02001559 return;
Willy Tarreauafc8a222014-11-28 15:46:27 +01001560 ic->flags |= CF_SHUTR;
1561 ic->rex = TICK_ETERNITY;
Willy Tarreau9bf9c142012-08-20 15:38:41 +02001562
Christopher Faulet62e75742022-03-31 09:16:34 +02001563 if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
Willy Tarreau9bf9c142012-08-20 15:38:41 +02001564 return;
1565
Willy Tarreauafc8a222014-11-28 15:46:27 +01001566 if (oc->flags & CF_SHUTW)
Willy Tarreau9bf9c142012-08-20 15:38:41 +02001567 goto do_close;
1568
Christopher Faulet8abe7122022-03-30 15:10:18 +02001569 if (cs->flags & CS_FL_NOHALF) {
Willy Tarreau9bf9c142012-08-20 15:38:41 +02001570 /* we want to immediately forward this close to the write side */
Willy Tarreau87b09662015-04-03 00:22:06 +02001571 /* force flag on ssl to keep stream in cache */
Christopher Faulet69ef6c92022-03-31 14:20:00 +02001572 cs_conn_shutw(cs, CO_SHW_SILENT);
Willy Tarreau9bf9c142012-08-20 15:38:41 +02001573 goto do_close;
1574 }
1575
1576 /* otherwise that's just a normal read shutdown */
Willy Tarreau9bf9c142012-08-20 15:38:41 +02001577 return;
1578
1579 do_close:
Christopher Fauletda098e62022-03-31 17:44:45 +02001580 /* OK we completely close the socket here just as if we went through cs_shut[rw]() */
Christopher Faulet69ef6c92022-03-31 14:20:00 +02001581 cs_conn_close(cs);
Willy Tarreauf9fbfe82012-11-21 21:51:53 +01001582
Willy Tarreauafc8a222014-11-28 15:46:27 +01001583 oc->flags &= ~CF_SHUTW_NOW;
1584 oc->flags |= CF_SHUTW;
1585 oc->wex = TICK_ETERNITY;
Willy Tarreauf9fbfe82012-11-21 21:51:53 +01001586
Willy Tarreau43e69dc2018-11-06 19:23:03 +01001587 si_done_get(si);
Willy Tarreauf9fbfe82012-11-21 21:51:53 +01001588
Christopher Faulet62e75742022-03-31 09:16:34 +02001589 cs->state = CS_ST_DIS;
Christopher Fauletae024ce2022-03-29 19:02:31 +02001590 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
Willy Tarreau9bf9c142012-08-20 15:38:41 +02001591 return;
1592}
1593
Willy Tarreau651e1822015-09-23 20:06:13 +02001594/* Callback to be used by applet handlers upon completion. It updates the stream
1595 * (which may or may not take this opportunity to try to forward data), then
Emeric Brun2802b072017-06-30 14:11:56 +02001596 * may re-enable the applet's based on the channels and stream interface's final
Willy Tarreau651e1822015-09-23 20:06:13 +02001597 * states.
1598 */
Willy Tarreauaa977ba2015-09-25 11:45:06 +02001599void si_applet_wake_cb(struct stream_interface *si)
Willy Tarreaue5f86492015-04-19 15:16:35 +02001600{
Willy Tarreaueca572f2015-09-25 19:11:55 +02001601 struct channel *ic = si_ic(si);
1602
Christopher Faulet8bc17592022-02-28 17:27:09 +01001603 BUG_ON(!cs_appctx(si->cs));
Christopher Faulet693b23b2022-02-28 09:09:05 +01001604
Willy Tarreaueca572f2015-09-25 19:11:55 +02001605 /* If the applet wants to write and the channel is closed, it's a
1606 * broken pipe and it must be reported.
1607 */
Willy Tarreau05b9b642018-11-14 13:43:35 +01001608 if (!(si->flags & SI_FL_RX_WAIT_EP) && (ic->flags & CF_SHUTR))
Christopher Faulet6cd56d52022-03-30 10:47:32 +02001609 si->cs->endp->flags |= CS_EP_ERROR;
Willy Tarreaueca572f2015-09-25 19:11:55 +02001610
Willy Tarreau186dcdd2018-11-16 16:18:34 +01001611 /* automatically mark the applet having data available if it reported
1612 * begin blocked by the channel.
1613 */
1614 if (si_rx_blocked(si))
1615 si_rx_endp_more(si);
1616
Willy Tarreau651e1822015-09-23 20:06:13 +02001617 /* update the stream-int, channels, and possibly wake the stream up */
1618 stream_int_notify(si);
Willy Tarreaua64c7032019-08-01 14:17:02 +02001619 stream_release_buffers(si_strm(si));
Willy Tarreaue5f86492015-04-19 15:16:35 +02001620
Willy Tarreau32742fd2018-11-14 14:07:59 +01001621 /* stream_int_notify may have passed through chk_snd and released some
1622 * RXBLK flags. Process_stream will consider those flags to wake up the
1623 * appctx but in the case the task is not in runqueue we may have to
1624 * wakeup the appctx immediately.
Emeric Brun2802b072017-06-30 14:11:56 +02001625 */
Olivier Houchard51205a12019-04-17 19:29:35 +02001626 if ((si_rx_endp_ready(si) && !si_rx_blocked(si)) ||
1627 (si_tx_endp_ready(si) && !si_tx_blocked(si)))
Christopher Faulet693b23b2022-02-28 09:09:05 +01001628 appctx_wakeup(__cs_appctx(si->cs));
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001629}
1630
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001631
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001632/*
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001633 * This function performs a shutdown-read on a conn-stream attached to an
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001634 * applet in a connected or init state (it does nothing for other states). It
1635 * either shuts the read side or marks itself as closed. The buffer flags are
Christopher Faulet8abe7122022-03-30 15:10:18 +02001636 * updated to reflect the new state. If the stream interface has CS_FL_NOHALF,
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001637 * we also forward the close to the write side. The owner task is woken up if
1638 * it exists.
1639 */
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001640static void cs_app_shutr_applet(struct conn_stream *cs)
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001641{
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001642 struct channel *ic = cs_ic(cs);
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001643
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001644 BUG_ON(!cs_appctx(cs));
Christopher Faulet693b23b2022-02-28 09:09:05 +01001645
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001646 si_rx_shut_blk(cs->si);
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001647 if (ic->flags & CF_SHUTR)
1648 return;
1649 ic->flags |= CF_SHUTR;
1650 ic->rex = TICK_ETERNITY;
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001651
Willy Tarreau828824a2015-04-19 17:20:03 +02001652 /* Note: on shutr, we don't call the applet */
1653
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001654 if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001655 return;
1656
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001657 if (cs_oc(cs)->flags & CF_SHUTW) {
1658 si_applet_release(cs->si);
1659 cs->state = CS_ST_DIS;
1660 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001661 }
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001662 else if (cs->flags & CS_FL_NOHALF) {
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001663 /* we want to immediately forward this close to the write side */
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001664 return cs_app_shutw_applet(cs);
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001665 }
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001666}
1667
1668/*
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001669 * This function performs a shutdown-write on a conn-stream attached to an
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001670 * applet in a connected or init state (it does nothing for other states). It
1671 * either shuts the write side or marks itself as closed. The buffer flags are
1672 * updated to reflect the new state. It does also close everything if the SI
1673 * was marked as being in error state. The owner task is woken up if it exists.
1674 */
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001675static void cs_app_shutw_applet(struct conn_stream *cs)
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001676{
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001677 struct channel *ic = cs_ic(cs);
1678 struct channel *oc = cs_oc(cs);
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001679
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001680 BUG_ON(!cs_appctx(cs));
Christopher Faulet693b23b2022-02-28 09:09:05 +01001681
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001682 oc->flags &= ~CF_SHUTW_NOW;
1683 if (oc->flags & CF_SHUTW)
1684 return;
1685 oc->flags |= CF_SHUTW;
1686 oc->wex = TICK_ETERNITY;
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001687 si_done_get(cs->si);
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001688
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001689 if (tick_isset(cs->hcto)) {
1690 ic->rto = cs->hcto;
Hongbo Longe39683c2017-03-10 18:41:51 +01001691 ic->rex = tick_add(now_ms, ic->rto);
1692 }
1693
Willy Tarreau828824a2015-04-19 17:20:03 +02001694 /* on shutw we always wake the applet up */
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001695 appctx_wakeup(__cs_appctx(cs));
Willy Tarreau828824a2015-04-19 17:20:03 +02001696
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001697 switch (cs->state) {
Christopher Faulet62e75742022-03-31 09:16:34 +02001698 case CS_ST_RDY:
1699 case CS_ST_EST:
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001700 /* we have to shut before closing, otherwise some short messages
1701 * may never leave the system, especially when there are remaining
1702 * unread data in the socket input buffer, or when nolinger is set.
Christopher Faulet8abe7122022-03-30 15:10:18 +02001703 * However, if CS_FL_NOLINGER is explicitly set, we know there is
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001704 * no risk so we close both sides immediately.
1705 */
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001706 if (!(cs->endp->flags & CS_EP_ERROR) && !(cs->flags & CS_FL_NOLINGER) &&
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001707 !(ic->flags & (CF_SHUTR|CF_DONT_READ)))
1708 return;
1709
1710 /* fall through */
Christopher Faulet62e75742022-03-31 09:16:34 +02001711 case CS_ST_CON:
1712 case CS_ST_CER:
1713 case CS_ST_QUE:
1714 case CS_ST_TAR:
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001715 /* Note that none of these states may happen with applets */
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001716 si_applet_release(cs->si);
1717 cs->state = CS_ST_DIS;
Tim Duesterhus588b3142020-05-29 14:35:51 +02001718 /* fall through */
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001719 default:
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001720 cs->flags &= ~CS_FL_NOLINGER;
1721 si_rx_shut_blk(cs->si);
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001722 ic->flags |= CF_SHUTR;
1723 ic->rex = TICK_ETERNITY;
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001724 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001725 }
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001726}
1727
1728/* chk_rcv function for applets */
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001729static void cs_app_chk_rcv_applet(struct conn_stream *cs)
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001730{
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001731 struct channel *ic = cs_ic(cs);
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001732
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001733 BUG_ON(!cs_appctx(cs));
Christopher Faulet693b23b2022-02-28 09:09:05 +01001734
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001735 DPRINTF(stderr, "%s: cs=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n",
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001736 __FUNCTION__,
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001737 cs, cs->state, ic->flags, cs_oc(cs)->flags);
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001738
Christopher Fauletb3e0de42018-10-11 13:54:13 +02001739 if (!ic->pipe) {
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001740 /* (re)start reading */
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001741 appctx_wakeup(__cs_appctx(cs));
Thierry FOURNIER5bc2cbf2015-09-04 18:40:36 +02001742 }
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001743}
1744
1745/* chk_snd function for applets */
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001746static void cs_app_chk_snd_applet(struct conn_stream *cs)
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001747{
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001748 struct channel *oc = cs_oc(cs);
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001749
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001750 BUG_ON(!cs_appctx(cs));
Christopher Faulet693b23b2022-02-28 09:09:05 +01001751
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001752 DPRINTF(stderr, "%s: cs=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n",
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001753 __FUNCTION__,
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001754 cs, cs->state, cs_ic(cs)->flags, oc->flags);
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001755
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001756 if (unlikely(cs->state != CS_ST_EST || (oc->flags & CF_SHUTW)))
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001757 return;
1758
Willy Tarreau828824a2015-04-19 17:20:03 +02001759 /* we only wake the applet up if it was waiting for some data */
1760
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001761 if (!(cs->si->flags & SI_FL_WAIT_DATA))
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001762 return;
1763
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001764 if (!tick_isset(oc->wex))
1765 oc->wex = tick_add_ifset(now_ms, oc->wto);
1766
Willy Tarreau828824a2015-04-19 17:20:03 +02001767 if (!channel_is_empty(oc)) {
1768 /* (re)start sending */
Christopher Faulet0c6a64c2022-04-01 08:58:29 +02001769 appctx_wakeup(__cs_appctx(cs));
Willy Tarreau828824a2015-04-19 17:20:03 +02001770 }
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001771}
1772
Willy Tarreaudded32d2008-11-30 19:48:07 +01001773/*
Willy Tarreaucff64112008-11-03 06:26:53 +01001774 * Local variables:
1775 * c-indent-level: 8
1776 * c-basic-offset: 8
1777 * End:
1778 */