blob: b119cff82002311ef436dd986c2bb47b23bb346f [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>
Willy Tarreau2741c8c2020-06-02 11:28:02 +020026#include <haproxy/dynbuf.h>
Willy Tarreaub7fc4c42021-10-06 18:56:42 +020027#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020028#include <haproxy/http_htx.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020029#include <haproxy/pipe-t.h>
30#include <haproxy/pipe.h>
Christopher Fauletcda94ac2021-12-23 17:28:17 +010031#include <haproxy/pool.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020032#include <haproxy/proxy.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020033#include <haproxy/stream-t.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020034#include <haproxy/stream_interface.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020035#include <haproxy/task.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020036#include <haproxy/ticks.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020037#include <haproxy/tools.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010038
Willy Tarreaufd31e532012-07-23 18:24:25 +020039
Christopher Fauletcda94ac2021-12-23 17:28:17 +010040DECLARE_POOL(pool_head_streaminterface, "stream_interface", sizeof(struct stream_interface));
41
42
Willy Tarreau14bfe9a2018-12-19 15:19:27 +010043/* functions used by default on a detached stream-interface */
Willy Tarreau6fe15412013-09-29 15:16:03 +020044static void stream_int_shutr(struct stream_interface *si);
45static void stream_int_shutw(struct stream_interface *si);
Willy Tarreauf873d752012-05-11 17:47:17 +020046static void stream_int_chk_rcv(struct stream_interface *si);
47static void stream_int_chk_snd(struct stream_interface *si);
Willy Tarreau14bfe9a2018-12-19 15:19:27 +010048
49/* functions used on a conn_stream-based stream-interface */
Willy Tarreau6fe15412013-09-29 15:16:03 +020050static void stream_int_shutr_conn(struct stream_interface *si);
51static void stream_int_shutw_conn(struct stream_interface *si);
Willy Tarreauc5788912012-08-24 18:12:41 +020052static void stream_int_chk_rcv_conn(struct stream_interface *si);
53static void stream_int_chk_snd_conn(struct stream_interface *si);
Willy Tarreau14bfe9a2018-12-19 15:19:27 +010054
55/* functions used on an applet-based stream-interface */
Willy Tarreaud45b9f82015-04-13 16:30:14 +020056static void stream_int_shutr_applet(struct stream_interface *si);
57static void stream_int_shutw_applet(struct stream_interface *si);
58static void stream_int_chk_rcv_applet(struct stream_interface *si);
59static void stream_int_chk_snd_applet(struct stream_interface *si);
Willy Tarreau14bfe9a2018-12-19 15:19:27 +010060
61/* last read notification */
62static void stream_int_read0(struct stream_interface *si);
63
64/* post-IO notification callback */
65static void stream_int_notify(struct stream_interface *si);
Willy Tarreauf873d752012-05-11 17:47:17 +020066
Willy Tarreauc5788912012-08-24 18:12:41 +020067/* stream-interface operations for embedded tasks */
68struct si_ops si_embedded_ops = {
Willy Tarreau5c979a92012-05-07 17:15:39 +020069 .chk_rcv = stream_int_chk_rcv,
70 .chk_snd = stream_int_chk_snd,
Willy Tarreau8b3d7df2013-09-29 14:51:58 +020071 .shutr = stream_int_shutr,
72 .shutw = stream_int_shutw,
Willy Tarreau5c979a92012-05-07 17:15:39 +020073};
74
Willy Tarreauc5788912012-08-24 18:12:41 +020075/* stream-interface operations for connections */
76struct si_ops si_conn_ops = {
Willy Tarreauc5788912012-08-24 18:12:41 +020077 .chk_rcv = stream_int_chk_rcv_conn,
78 .chk_snd = stream_int_chk_snd_conn,
Willy Tarreau8b3d7df2013-09-29 14:51:58 +020079 .shutr = stream_int_shutr_conn,
80 .shutw = stream_int_shutw_conn,
Willy Tarreauc5788912012-08-24 18:12:41 +020081};
82
Willy Tarreaud45b9f82015-04-13 16:30:14 +020083/* stream-interface operations for connections */
84struct si_ops si_applet_ops = {
Willy Tarreaud45b9f82015-04-13 16:30:14 +020085 .chk_rcv = stream_int_chk_rcv_applet,
86 .chk_snd = stream_int_chk_snd_applet,
87 .shutr = stream_int_shutr_applet,
88 .shutw = stream_int_shutw_applet,
89};
90
Willy Tarreau14bfe9a2018-12-19 15:19:27 +010091
92/* Functions used to communicate with a conn_stream. The first two may be used
93 * directly, the last one is mostly a wake callback.
94 */
Christopher Faulet9936dc62022-02-28 09:21:58 +010095static int si_cs_recv(struct conn_stream *cs);
Christopher Faulet49416232022-02-28 09:14:46 +010096static int si_cs_send(struct conn_stream *cs);
Willy Tarreau14bfe9a2018-12-19 15:19:27 +010097static int si_cs_process(struct conn_stream *cs);
98
99
Willy Tarreau74beec32012-10-03 00:41:04 +0200100struct data_cb si_conn_cb = {
Olivier Houchard21df6cc2018-09-14 23:21:44 +0200101 .wake = si_cs_process,
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +0100102 .name = "STRM",
Willy Tarreauc5788912012-08-24 18:12:41 +0200103};
104
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100105
106struct stream_interface *si_new(struct conn_stream *cs)
107{
108 struct stream_interface *si;
109
110 si = pool_alloc(pool_head_streaminterface);
111 if (unlikely(!si))
112 return NULL;
113 si->flags = SI_FL_NONE;
Christopher Faulet014ac352022-01-06 08:13:46 +0100114 if (si_init(si) < 0) {
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100115 pool_free(pool_head_streaminterface, si);
116 return NULL;
117 }
118 si->cs = cs;
119 return si;
120}
121
122void si_free(struct stream_interface *si)
123{
124 if (!si)
125 return;
126
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100127 pool_free(pool_head_streaminterface, si);
128}
129
Willy Tarreaucff64112008-11-03 06:26:53 +0100130/*
Willy Tarreaud45b9f82015-04-13 16:30:14 +0200131 * This function performs a shutdown-read on a detached stream interface in a
132 * connected or init state (it does nothing for other states). It either shuts
133 * the read side or marks itself as closed. The buffer flags are updated to
Christopher Faulet8abe7122022-03-30 15:10:18 +0200134 * reflect the new state. If the stream interface has CS_FL_NOHALF, we also
Willy Tarreaud45b9f82015-04-13 16:30:14 +0200135 * forward the close to the write side. The owner task is woken up if it exists.
Willy Tarreau4a36b562012-08-06 19:31:45 +0200136 */
Willy Tarreau6fe15412013-09-29 15:16:03 +0200137static void stream_int_shutr(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200138{
Willy Tarreauafc8a222014-11-28 15:46:27 +0100139 struct channel *ic = si_ic(si);
140
Willy Tarreauabb5d422018-11-14 16:58:52 +0100141 si_rx_shut_blk(si);
Willy Tarreauafc8a222014-11-28 15:46:27 +0100142 if (ic->flags & CF_SHUTR)
Willy Tarreau6fe15412013-09-29 15:16:03 +0200143 return;
Willy Tarreauafc8a222014-11-28 15:46:27 +0100144 ic->flags |= CF_SHUTR;
145 ic->rex = TICK_ETERNITY;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200146
Christopher Faulet62e75742022-03-31 09:16:34 +0200147 if (!cs_state_in(si->cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
Willy Tarreau6fe15412013-09-29 15:16:03 +0200148 return;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200149
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100150 if (si_oc(si)->flags & CF_SHUTW) {
Christopher Faulet62e75742022-03-31 09:16:34 +0200151 si->cs->state = CS_ST_DIS;
Christopher Fauletae024ce2022-03-29 19:02:31 +0200152 __cs_strm(si->cs)->conn_exp = TICK_ETERNITY;
Willy Tarreaud8ccffe2010-09-07 16:16:50 +0200153 }
Christopher Faulet8abe7122022-03-30 15:10:18 +0200154 else if (si->cs->flags & CS_FL_NOHALF) {
Willy Tarreau4a36b562012-08-06 19:31:45 +0200155 /* we want to immediately forward this close to the write side */
156 return stream_int_shutw(si);
157 }
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200158
Willy Tarreau4a36b562012-08-06 19:31:45 +0200159 /* note that if the task exists, it must unregister itself once it runs */
Christopher Faulet974da9f2022-03-30 15:30:03 +0200160 if (!(si->cs->flags & CS_FL_DONT_WAKE))
Willy Tarreau07373b82014-11-28 12:08:47 +0100161 task_wakeup(si_task(si), TASK_WOKEN_IO);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200162}
163
Willy Tarreau4a36b562012-08-06 19:31:45 +0200164/*
Willy Tarreaud45b9f82015-04-13 16:30:14 +0200165 * This function performs a shutdown-write on a detached stream interface in a
166 * connected or init state (it does nothing for other states). It either shuts
167 * the write side or marks itself as closed. The buffer flags are updated to
168 * reflect the new state. It does also close everything if the SI was marked as
169 * being in error state. The owner task is woken up if it exists.
Willy Tarreau4a36b562012-08-06 19:31:45 +0200170 */
Willy Tarreau6fe15412013-09-29 15:16:03 +0200171static void stream_int_shutw(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200172{
Willy Tarreauafc8a222014-11-28 15:46:27 +0100173 struct channel *ic = si_ic(si);
174 struct channel *oc = si_oc(si);
175
176 oc->flags &= ~CF_SHUTW_NOW;
177 if (oc->flags & CF_SHUTW)
Willy Tarreau6fe15412013-09-29 15:16:03 +0200178 return;
Willy Tarreauafc8a222014-11-28 15:46:27 +0100179 oc->flags |= CF_SHUTW;
180 oc->wex = TICK_ETERNITY;
Willy Tarreau43e69dc2018-11-06 19:23:03 +0100181 si_done_get(si);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200182
Christopher Faulet1d987772022-03-29 18:03:35 +0200183 if (tick_isset(si->cs->hcto)) {
184 ic->rto = si->cs->hcto;
Hongbo Longe39683c2017-03-10 18:41:51 +0100185 ic->rex = tick_add(now_ms, ic->rto);
186 }
187
Christopher Faulet62e75742022-03-31 09:16:34 +0200188 switch (si->cs->state) {
189 case CS_ST_RDY:
190 case CS_ST_EST:
Willy Tarreau4a36b562012-08-06 19:31:45 +0200191 /* we have to shut before closing, otherwise some short messages
192 * may never leave the system, especially when there are remaining
193 * unread data in the socket input buffer, or when nolinger is set.
Christopher Faulet8abe7122022-03-30 15:10:18 +0200194 * However, if CS_FL_NOLINGER is explicitly set, we know there is
Willy Tarreau4a36b562012-08-06 19:31:45 +0200195 * no risk so we close both sides immediately.
196 */
Christopher Faulet8abe7122022-03-30 15:10:18 +0200197 if (!(si->cs->endp->flags & CS_EP_ERROR) && !(si->cs->flags & CS_FL_NOLINGER) &&
Willy Tarreauafc8a222014-11-28 15:46:27 +0100198 !(ic->flags & (CF_SHUTR|CF_DONT_READ)))
Willy Tarreau6fe15412013-09-29 15:16:03 +0200199 return;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200200
201 /* fall through */
Christopher Faulet62e75742022-03-31 09:16:34 +0200202 case CS_ST_CON:
203 case CS_ST_CER:
204 case CS_ST_QUE:
205 case CS_ST_TAR:
Willy Tarreau8b3d7df2013-09-29 14:51:58 +0200206 /* Note that none of these states may happen with applets */
Christopher Faulet62e75742022-03-31 09:16:34 +0200207 si->cs->state = CS_ST_DIS;
Tim Duesterhus588b3142020-05-29 14:35:51 +0200208 /* fall through */
Willy Tarreaufb90d942009-09-05 20:57:35 +0200209 default:
Christopher Faulet8abe7122022-03-30 15:10:18 +0200210 si->cs->flags &= ~CS_FL_NOLINGER;
Willy Tarreauabb5d422018-11-14 16:58:52 +0100211 si_rx_shut_blk(si);
Willy Tarreauafc8a222014-11-28 15:46:27 +0100212 ic->flags |= CF_SHUTR;
213 ic->rex = TICK_ETERNITY;
Christopher Fauletae024ce2022-03-29 19:02:31 +0200214 __cs_strm(si->cs)->conn_exp = TICK_ETERNITY;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200215 }
216
Willy Tarreau4a36b562012-08-06 19:31:45 +0200217 /* note that if the task exists, it must unregister itself once it runs */
Christopher Faulet974da9f2022-03-30 15:30:03 +0200218 if (!(si->cs->flags & CS_FL_DONT_WAKE))
Willy Tarreau07373b82014-11-28 12:08:47 +0100219 task_wakeup(si_task(si), TASK_WOKEN_IO);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200220}
221
222/* default chk_rcv function for scheduled tasks */
Willy Tarreauf873d752012-05-11 17:47:17 +0200223static void stream_int_chk_rcv(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200224{
Willy Tarreauafc8a222014-11-28 15:46:27 +0100225 struct channel *ic = si_ic(si);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200226
Christopher Faulet62e75742022-03-31 09:16:34 +0200227 DPRINTF(stderr, "%s: si=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n",
Willy Tarreaufb90d942009-09-05 20:57:35 +0200228 __FUNCTION__,
Christopher Faulet62e75742022-03-31 09:16:34 +0200229 si, si->cs->state, ic->flags, si_oc(si)->flags);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200230
Christopher Fauletb3e0de42018-10-11 13:54:13 +0200231 if (ic->pipe) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200232 /* stop reading */
Willy Tarreaudb398432018-11-15 11:08:52 +0100233 si_rx_room_blk(si);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200234 }
235 else {
236 /* (re)start reading */
Christopher Faulet974da9f2022-03-30 15:30:03 +0200237 if (!(si->cs->flags & CS_FL_DONT_WAKE))
Willy Tarreau07373b82014-11-28 12:08:47 +0100238 task_wakeup(si_task(si), TASK_WOKEN_IO);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200239 }
240}
241
242/* default chk_snd function for scheduled tasks */
Willy Tarreauf873d752012-05-11 17:47:17 +0200243static void stream_int_chk_snd(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200244{
Willy Tarreauafc8a222014-11-28 15:46:27 +0100245 struct channel *oc = si_oc(si);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200246
Christopher Faulet62e75742022-03-31 09:16:34 +0200247 DPRINTF(stderr, "%s: si=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n",
Willy Tarreaufb90d942009-09-05 20:57:35 +0200248 __FUNCTION__,
Christopher Faulet62e75742022-03-31 09:16:34 +0200249 si, si->cs->state, si_ic(si)->flags, oc->flags);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200250
Christopher Faulet62e75742022-03-31 09:16:34 +0200251 if (unlikely(si->cs->state != CS_ST_EST || (oc->flags & CF_SHUTW)))
Willy Tarreaufb90d942009-09-05 20:57:35 +0200252 return;
253
254 if (!(si->flags & SI_FL_WAIT_DATA) || /* not waiting for data */
Willy Tarreauafc8a222014-11-28 15:46:27 +0100255 channel_is_empty(oc)) /* called with nothing to send ! */
Willy Tarreaufb90d942009-09-05 20:57:35 +0200256 return;
257
258 /* Otherwise there are remaining data to be sent in the buffer,
259 * so we tell the handler.
260 */
261 si->flags &= ~SI_FL_WAIT_DATA;
Willy Tarreauafc8a222014-11-28 15:46:27 +0100262 if (!tick_isset(oc->wex))
263 oc->wex = tick_add_ifset(now_ms, oc->wto);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200264
Christopher Faulet974da9f2022-03-30 15:30:03 +0200265 if (!(si->cs->flags & CS_FL_DONT_WAKE))
Willy Tarreau07373b82014-11-28 12:08:47 +0100266 task_wakeup(si_task(si), TASK_WOKEN_IO);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200267}
268
Willy Tarreaua9ff5e62015-07-19 18:46:30 +0200269/* Register an applet to handle a stream_interface as a new appctx. The SI will
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -0700270 * wake it up every time it is solicited. The appctx must be deleted by the task
Willy Tarreaua9ff5e62015-07-19 18:46:30 +0200271 * handler using si_release_endpoint(), possibly from within the function itself.
272 * It also pre-initializes the applet's context and returns it (or NULL in case
273 * it could not be allocated).
Willy Tarreaufb90d942009-09-05 20:57:35 +0200274 */
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100275struct appctx *si_register_handler(struct stream_interface *si, struct applet *app)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200276{
Willy Tarreau0a23bcb2013-12-01 11:31:38 +0100277 struct appctx *appctx;
278
Willy Tarreau07373b82014-11-28 12:08:47 +0100279 DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", app, si, si_task(si));
Willy Tarreaufb90d942009-09-05 20:57:35 +0200280
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100281 appctx = appctx_new(app, si->cs->endp);
Willy Tarreaua69fc9f2014-12-22 19:34:00 +0100282 if (!appctx)
Willy Tarreau0a23bcb2013-12-01 11:31:38 +0100283 return NULL;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100284 cs_attach_applet(si->cs, appctx, appctx);
285 appctx->owner = si->cs;
Christopher Faulet13a35e52021-12-20 15:34:16 +0100286 appctx->t->nice = si_strm(si)->task->nice;
Willy Tarreau0cd3bd62018-11-06 18:46:37 +0100287 si_cant_get(si);
Willy Tarreau828824a2015-04-19 17:20:03 +0200288 appctx_wakeup(appctx);
Christopher Faulet13a35e52021-12-20 15:34:16 +0100289 return appctx;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200290}
291
Willy Tarreau2c6be842012-07-06 17:12:34 +0200292/* This callback is used to send a valid PROXY protocol line to a socket being
Willy Tarreauafad0e02012-08-09 14:45:22 +0200293 * established. It returns 0 if it fails in a fatal way or needs to poll to go
294 * further, otherwise it returns non-zero and removes itself from the connection's
Willy Tarreaua1a74742012-08-24 12:14:49 +0200295 * flags (the bit is provided in <flag> by the caller). It is designed to be
296 * called by the connection handler and relies on it to commit polling changes.
Willy Tarreau57cd3e42013-10-24 22:01:26 +0200297 * Note that it can emit a PROXY line by relying on the other end's address
298 * when the connection is attached to a stream interface, or by resolving the
299 * local address otherwise (also called a LOCAL line).
Willy Tarreau2c6be842012-07-06 17:12:34 +0200300 */
301int conn_si_send_proxy(struct connection *conn, unsigned int flag)
302{
Willy Tarreaud02cdd22013-12-15 10:23:20 +0100303 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200304 goto out_error;
305
Willy Tarreau2c6be842012-07-06 17:12:34 +0200306 /* If we have a PROXY line to send, we'll use this to validate the
307 * connection, in which case the connection is validated only once
308 * we've sent the whole proxy line. Otherwise we use connect().
309 */
Tim Duesterhus36839dc2019-02-26 17:09:51 +0100310 if (conn->send_proxy_ofs) {
Willy Tarreau6b1379f2018-11-18 21:38:19 +0100311 const struct conn_stream *cs;
Willy Tarreau2c6be842012-07-06 17:12:34 +0200312 int ret;
313
Christopher Fauletd82056c2020-05-26 16:08:49 +0200314 /* If there is no mux attached to the connection, it means the
315 * connection context is a conn-stream.
316 */
Christopher Faulet69ef6c92022-03-31 14:20:00 +0200317 cs = (conn->mux ? cs_conn_get_first(conn) : conn->ctx);
Christopher Fauletd82056c2020-05-26 16:08:49 +0200318
Willy Tarreau2c6be842012-07-06 17:12:34 +0200319 /* The target server expects a PROXY line to be sent first.
320 * If the send_proxy_ofs is negative, it corresponds to the
321 * offset to start sending from then end of the proxy string
322 * (which is recomputed every time since it's constant). If
323 * it is positive, it means we have to send from the start.
Willy Tarreau57cd3e42013-10-24 22:01:26 +0200324 * We can only send a "normal" PROXY line when the connection
325 * is attached to a stream interface. Otherwise we can only
326 * send a LOCAL line (eg: for use with health checks).
Willy Tarreau2c6be842012-07-06 17:12:34 +0200327 */
Willy Tarreau6b1379f2018-11-18 21:38:19 +0100328
329 if (cs && cs->data_cb == &si_conn_cb) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200330 ret = make_proxy_line(trash.area, trash.size,
331 objt_server(conn->target),
Christopher Fauletf835dea2021-12-21 14:35:17 +0100332 cs_conn(si_opposite(cs_si(cs))->cs),
Christopher Faulet693b23b2022-02-28 09:09:05 +0100333 cs_strm(cs));
Willy Tarreau57cd3e42013-10-24 22:01:26 +0200334 }
335 else {
336 /* The target server expects a LOCAL line to be sent first. Retrieving
337 * local or remote addresses may fail until the connection is established.
338 */
Willy Tarreau7bb447c2019-07-17 11:40:51 +0200339 if (!conn_get_src(conn) || !conn_get_dst(conn))
Willy Tarreau57cd3e42013-10-24 22:01:26 +0200340 goto out_wait;
341
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200342 ret = make_proxy_line(trash.area, trash.size,
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +0100343 objt_server(conn->target), conn,
344 NULL);
Willy Tarreau57cd3e42013-10-24 22:01:26 +0200345 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200346
Willy Tarreau2c6be842012-07-06 17:12:34 +0200347 if (!ret)
348 goto out_error;
349
Willy Tarreaub8020ce2013-10-24 21:10:08 +0200350 if (conn->send_proxy_ofs > 0)
351 conn->send_proxy_ofs = -ret; /* first call */
Willy Tarreau2c6be842012-07-06 17:12:34 +0200352
Willy Tarreaua1a74742012-08-24 12:14:49 +0200353 /* we have to send trash from (ret+sp for -sp bytes). If the
354 * data layer has a pending write, we'll also set MSG_MORE.
355 */
Willy Tarreau827fee72020-12-11 15:26:55 +0100356 ret = conn_ctrl_send(conn,
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200357 trash.area + ret + conn->send_proxy_ofs,
358 -conn->send_proxy_ofs,
Willy Tarreau827fee72020-12-11 15:26:55 +0100359 (conn->subs && conn->subs->events & SUB_RETRY_SEND) ? CO_SFL_MSG_MORE : 0);
Willy Tarreau2c6be842012-07-06 17:12:34 +0200360
Willy Tarreau0a03c0f2015-03-13 00:05:28 +0100361 if (ret < 0)
Willy Tarreau2c6be842012-07-06 17:12:34 +0200362 goto out_error;
Willy Tarreau2c6be842012-07-06 17:12:34 +0200363
Willy Tarreaub8020ce2013-10-24 21:10:08 +0200364 conn->send_proxy_ofs += ret; /* becomes zero once complete */
365 if (conn->send_proxy_ofs != 0)
Willy Tarreau2c6be842012-07-06 17:12:34 +0200366 goto out_wait;
367
368 /* OK we've sent the whole line, we're connected */
369 }
370
Willy Tarreaua1a74742012-08-24 12:14:49 +0200371 /* The connection is ready now, simply return and let the connection
372 * handler notify upper layers if needed.
Willy Tarreau2c6be842012-07-06 17:12:34 +0200373 */
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100374 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau2c6be842012-07-06 17:12:34 +0200375 conn->flags &= ~flag;
Willy Tarreauafad0e02012-08-09 14:45:22 +0200376 return 1;
Willy Tarreau2c6be842012-07-06 17:12:34 +0200377
378 out_error:
Willy Tarreauafad0e02012-08-09 14:45:22 +0200379 /* Write error on the file descriptor */
Willy Tarreau2c6be842012-07-06 17:12:34 +0200380 conn->flags |= CO_FL_ERROR;
Willy Tarreauafad0e02012-08-09 14:45:22 +0200381 return 0;
Willy Tarreau2c6be842012-07-06 17:12:34 +0200382
383 out_wait:
Willy Tarreauafad0e02012-08-09 14:45:22 +0200384 return 0;
Willy Tarreau2c6be842012-07-06 17:12:34 +0200385}
386
Willy Tarreau27375622013-12-17 00:00:28 +0100387
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100388/* This function is the equivalent to si_update() except that it's
Willy Tarreau615f28b2015-09-23 18:40:09 +0200389 * designed to be called from outside the stream handlers, typically the lower
390 * layers (applets, connections) after I/O completion. After updating the stream
391 * interface and timeouts, it will try to forward what can be forwarded, then to
392 * wake the associated task up if an important event requires special handling.
Willy Tarreaud0f5bbc2018-11-14 11:10:26 +0100393 * It may update SI_FL_WAIT_DATA and/or SI_FL_RXBLK_ROOM, that the callers are
Willy Tarreau0dfccb22018-10-25 13:55:20 +0200394 * encouraged to watch to take appropriate action.
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100395 * It should not be called from within the stream itself, si_update()
Willy Tarreau615f28b2015-09-23 18:40:09 +0200396 * is designed for this.
397 */
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100398static void stream_int_notify(struct stream_interface *si)
Willy Tarreau615f28b2015-09-23 18:40:09 +0200399{
400 struct channel *ic = si_ic(si);
401 struct channel *oc = si_oc(si);
Willy Tarreau47baeb82018-11-15 07:46:57 +0100402 struct stream_interface *sio = si_opposite(si);
Christopher Fauletd7607de2019-01-03 16:24:54 +0100403 struct task *task = si_task(si);
Willy Tarreau615f28b2015-09-23 18:40:09 +0200404
405 /* process consumer side */
406 if (channel_is_empty(oc)) {
Christopher Faulet13a35e52021-12-20 15:34:16 +0100407 struct connection *conn = cs_conn(si->cs);
Olivier Houcharde9bed532017-11-16 17:49:25 +0100408
Willy Tarreau615f28b2015-09-23 18:40:09 +0200409 if (((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW) &&
Christopher Faulet62e75742022-03-31 09:16:34 +0200410 (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 +0200411 cs_shutw(si->cs);
Willy Tarreau615f28b2015-09-23 18:40:09 +0200412 oc->wex = TICK_ETERNITY;
413 }
414
Willy Tarreau8cf9c8e2016-12-13 15:21:25 +0100415 /* indicate that we may be waiting for data from the output channel or
416 * we're about to close and can't expect more data if SHUTW_NOW is there.
417 */
Christopher Fauletb3e0de42018-10-11 13:54:13 +0200418 if (!(oc->flags & (CF_SHUTW|CF_SHUTW_NOW)))
Willy Tarreau615f28b2015-09-23 18:40:09 +0200419 si->flags |= SI_FL_WAIT_DATA;
Willy Tarreau8cf9c8e2016-12-13 15:21:25 +0100420 else if ((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW)
421 si->flags &= ~SI_FL_WAIT_DATA;
Willy Tarreau615f28b2015-09-23 18:40:09 +0200422
423 /* update OC timeouts and wake the other side up if it's waiting for room */
424 if (oc->flags & CF_WRITE_ACTIVITY) {
425 if ((oc->flags & (CF_SHUTW|CF_WRITE_PARTIAL)) == CF_WRITE_PARTIAL &&
426 !channel_is_empty(oc))
427 if (tick_isset(oc->wex))
428 oc->wex = tick_add_ifset(now_ms, oc->wto);
429
Christopher Fauleta7285182022-03-30 15:43:23 +0200430 if (!(si->cs->flags & CS_FL_INDEP_STR))
Willy Tarreau615f28b2015-09-23 18:40:09 +0200431 if (tick_isset(ic->rex))
432 ic->rex = tick_add_ifset(now_ms, ic->rto);
Willy Tarreauf26c26c2018-11-12 16:11:08 +0100433 }
Willy Tarreau615f28b2015-09-23 18:40:09 +0200434
Willy Tarreaub26a6f92018-11-14 17:10:36 +0100435 if (oc->flags & CF_DONT_READ)
436 si_rx_chan_blk(sio);
437 else
438 si_rx_chan_rdy(sio);
Willy Tarreau615f28b2015-09-23 18:40:09 +0200439
440 /* Notify the other side when we've injected data into the IC that
441 * needs to be forwarded. We can do fast-forwarding as soon as there
442 * are output data, but we avoid doing this if some of the data are
443 * not yet scheduled for being forwarded, because it is very likely
444 * that it will be done again immediately afterwards once the following
Willy Tarreaud0f5bbc2018-11-14 11:10:26 +0100445 * data are parsed (eg: HTTP chunking). We only SI_FL_RXBLK_ROOM once
Willy Tarreau615f28b2015-09-23 18:40:09 +0200446 * we've emptied *some* of the output buffer, and not just when there
447 * is available room, because applets are often forced to stop before
448 * the buffer is full. We must not stop based on input data alone because
449 * an HTTP parser might need more data to complete the parsing.
450 */
451 if (!channel_is_empty(ic) &&
Willy Tarreau47baeb82018-11-15 07:46:57 +0100452 (sio->flags & SI_FL_WAIT_DATA) &&
Willy Tarreau89b6a2b2018-11-18 15:46:10 +0100453 (!(ic->flags & CF_EXPECT_MORE) || c_full(ic) || ci_data(ic) == 0 || ic->pipe)) {
Willy Tarreau615f28b2015-09-23 18:40:09 +0200454 int new_len, last_len;
455
Willy Tarreau77e478c2018-06-19 07:03:14 +0200456 last_len = co_data(ic);
Willy Tarreau615f28b2015-09-23 18:40:09 +0200457 if (ic->pipe)
458 last_len += ic->pipe->data;
459
Christopher Fauletda098e62022-03-31 17:44:45 +0200460 cs_chk_snd(sio->cs);
Willy Tarreau615f28b2015-09-23 18:40:09 +0200461
Willy Tarreau77e478c2018-06-19 07:03:14 +0200462 new_len = co_data(ic);
Willy Tarreau615f28b2015-09-23 18:40:09 +0200463 if (ic->pipe)
464 new_len += ic->pipe->data;
465
466 /* check if the consumer has freed some space either in the
467 * buffer or in the pipe.
468 */
Willy Tarreau47baeb82018-11-15 07:46:57 +0100469 if (new_len < last_len)
Willy Tarreaudb398432018-11-15 11:08:52 +0100470 si_rx_room_rdy(si);
Willy Tarreau615f28b2015-09-23 18:40:09 +0200471 }
472
Willy Tarreaub26a6f92018-11-14 17:10:36 +0100473 if (!(ic->flags & CF_DONT_READ))
474 si_rx_chan_rdy(si);
475
Christopher Fauletda098e62022-03-31 17:44:45 +0200476 cs_chk_rcv(si->cs);
477 cs_chk_rcv(sio->cs);
Willy Tarreau47baeb82018-11-15 07:46:57 +0100478
Willy Tarreaub26a6f92018-11-14 17:10:36 +0100479 if (si_rx_blocked(si)) {
Willy Tarreau615f28b2015-09-23 18:40:09 +0200480 ic->rex = TICK_ETERNITY;
481 }
Willy Tarreaub26a6f92018-11-14 17:10:36 +0100482 else if ((ic->flags & (CF_SHUTR|CF_READ_PARTIAL)) == CF_READ_PARTIAL) {
Christopher Fauletda098e62022-03-31 17:44:45 +0200483 /* we must re-enable reading if cs_chk_snd() has freed some space */
Willy Tarreau615f28b2015-09-23 18:40:09 +0200484 if (!(ic->flags & CF_READ_NOEXP) && tick_isset(ic->rex))
485 ic->rex = tick_add_ifset(now_ms, ic->rto);
486 }
487
488 /* wake the task up only when needed */
489 if (/* changes on the production side */
490 (ic->flags & (CF_READ_NULL|CF_READ_ERROR)) ||
Christopher Faulet62e75742022-03-31 09:16:34 +0200491 !cs_state_in(si->cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST) ||
Christopher Faulet6cd56d52022-03-30 10:47:32 +0200492 (si->cs->endp->flags & CS_EP_ERROR) ||
Willy Tarreau615f28b2015-09-23 18:40:09 +0200493 ((ic->flags & CF_READ_PARTIAL) &&
Christopher Faulet62e75742022-03-31 09:16:34 +0200494 ((ic->flags & CF_EOI) || !ic->to_forward || sio->cs->state != CS_ST_EST)) ||
Willy Tarreau615f28b2015-09-23 18:40:09 +0200495
496 /* changes on the consumption side */
497 (oc->flags & (CF_WRITE_NULL|CF_WRITE_ERROR)) ||
Willy Tarreauede3d882018-10-24 17:17:56 +0200498 ((oc->flags & CF_WRITE_ACTIVITY) &&
Willy Tarreau615f28b2015-09-23 18:40:09 +0200499 ((oc->flags & CF_SHUTW) ||
Willy Tarreau78f5ff82018-12-19 11:00:00 +0100500 (((oc->flags & CF_WAKE_WRITE) ||
501 !(oc->flags & (CF_AUTO_CLOSE|CF_SHUTW_NOW|CF_SHUTW))) &&
Christopher Faulet62e75742022-03-31 09:16:34 +0200502 (sio->cs->state != CS_ST_EST ||
Willy Tarreau615f28b2015-09-23 18:40:09 +0200503 (channel_is_empty(oc) && !oc->to_forward)))))) {
Christopher Fauletd7607de2019-01-03 16:24:54 +0100504 task_wakeup(task, TASK_WOKEN_IO);
505 }
506 else {
507 /* Update expiration date for the task and requeue it */
508 task->expire = tick_first((tick_is_expired(task->expire, now_ms) ? 0 : task->expire),
509 tick_first(tick_first(ic->rex, ic->wex),
510 tick_first(oc->rex, oc->wex)));
Willy Tarreau45bcb372019-08-01 18:51:38 +0200511
512 task->expire = tick_first(task->expire, ic->analyse_exp);
513 task->expire = tick_first(task->expire, oc->analyse_exp);
Christopher Fauletae024ce2022-03-29 19:02:31 +0200514 task->expire = tick_first(task->expire, __cs_strm(si->cs)->conn_exp);
Willy Tarreau45bcb372019-08-01 18:51:38 +0200515
Christopher Fauletd7607de2019-01-03 16:24:54 +0100516 task_queue(task);
Willy Tarreau615f28b2015-09-23 18:40:09 +0200517 }
518 if (ic->flags & CF_READ_ACTIVITY)
519 ic->flags &= ~CF_READ_DONTWAIT;
Willy Tarreau615f28b2015-09-23 18:40:09 +0200520}
521
Willy Tarreaud1480cc2022-03-17 16:19:09 +0100522/* The stream interface is only responsible for the connection during the early
523 * states, before plugging a mux. Thus it should only care about CO_FL_ERROR
Christopher Faulet62e75742022-03-31 09:16:34 +0200524 * before CS_ST_EST, and after that it must absolutely ignore it since the mux
Willy Tarreaud1480cc2022-03-17 16:19:09 +0100525 * may hold pending data. This function returns true if such an error was
526 * reported. Both the CS and the CONN must be valid.
527 */
528static inline int si_is_conn_error(const struct stream_interface *si)
529{
530 struct connection *conn;
531
Christopher Faulet62e75742022-03-31 09:16:34 +0200532 if (si->cs->state >= CS_ST_EST)
Willy Tarreaud1480cc2022-03-17 16:19:09 +0100533 return 0;
534
535 conn = __cs_conn(si->cs);
536 BUG_ON(!conn);
537 return !!(conn->flags & CO_FL_ERROR);
538}
Willy Tarreau615f28b2015-09-23 18:40:09 +0200539
Olivier Houchardc2aa7112018-09-11 18:27:21 +0200540/* Called by I/O handlers after completion.. It propagates
Willy Tarreau651e1822015-09-23 20:06:13 +0200541 * connection flags to the stream interface, updates the stream (which may or
542 * may not take this opportunity to try to forward data), then update the
543 * connection's polling based on the channels and stream interface's final
544 * states. The function always returns 0.
Willy Tarreau100c4672012-08-20 12:06:26 +0200545 */
Olivier Houchardc2aa7112018-09-11 18:27:21 +0200546static int si_cs_process(struct conn_stream *cs)
Willy Tarreaufd31e532012-07-23 18:24:25 +0200547{
Christopher Faulet693b23b2022-02-28 09:09:05 +0100548 struct connection *conn = __cs_conn(cs);
Christopher Fauletf835dea2021-12-21 14:35:17 +0100549 struct stream_interface *si = cs_si(cs);
Willy Tarreauafc8a222014-11-28 15:46:27 +0100550 struct channel *ic = si_ic(si);
551 struct channel *oc = si_oc(si);
Willy Tarreaufd31e532012-07-23 18:24:25 +0200552
Christopher Faulet897d6122021-12-17 17:28:35 +0100553 BUG_ON(!conn);
554
Olivier Houchardc7ffa912018-08-28 19:37:41 +0200555 /* If we have data to send, try it now */
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200556 if (!channel_is_empty(oc) && !(si->cs->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau908d26f2018-10-25 14:02:47 +0200557 si_cs_send(cs);
558
Christopher Fauletaf642df2022-03-30 10:06:11 +0200559 /* First step, report to the conn-stream what was detected at the
Willy Tarreau651e1822015-09-23 20:06:13 +0200560 * connection layer : errors and connection establishment.
Christopher Faulet6cd56d52022-03-30 10:47:32 +0200561 * Only add CS_EP_ERROR if we're connected, or we're attempting to
Olivier Houchardc31e2cb2019-06-24 16:08:08 +0200562 * connect, we may get there because we got woken up, but only run
563 * after process_stream() noticed there were an error, and decided
564 * to retry to connect, the connection may still have CO_FL_ERROR,
Christopher Faulet6cd56d52022-03-30 10:47:32 +0200565 * and we don't want to add CS_EP_ERROR back
Christopher Faulet36b536d2019-11-20 11:56:33 +0100566 *
567 * Note: This test is only required because si_cs_process is also the SI
568 * wake callback. Otherwise si_cs_recv()/si_cs_send() already take
569 * care of it.
Willy Tarreau651e1822015-09-23 20:06:13 +0200570 */
Willy Tarreaud1480cc2022-03-17 16:19:09 +0100571
Christopher Faulet62e75742022-03-31 09:16:34 +0200572 if (si->cs->state >= CS_ST_CON) {
Christopher Faulet6cd56d52022-03-30 10:47:32 +0200573 if (si_is_conn_error(si))
574 cs->endp->flags |= CS_EP_ERROR;
Willy Tarreaud1480cc2022-03-17 16:19:09 +0100575 }
Willy Tarreau3c55ec22012-07-23 19:19:51 +0200576
Olivier Houchardccaa7de2017-10-02 11:51:03 +0200577 /* If we had early data, and the handshake ended, then
578 * we can remove the flag, and attempt to wake the task up,
579 * in the event there's an analyser waiting for the end of
580 * the handshake.
581 */
Willy Tarreau911db9b2020-01-23 16:27:54 +0100582 if (!(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS)) &&
Christopher Faulete9e48202022-03-22 18:13:29 +0100583 (cs->endp->flags & CS_EP_WAIT_FOR_HS)) {
584 cs->endp->flags &= ~CS_EP_WAIT_FOR_HS;
Olivier Houchardccaa7de2017-10-02 11:51:03 +0200585 task_wakeup(si_task(si), TASK_WOKEN_MSG);
586 }
587
Christopher Faulet62e75742022-03-31 09:16:34 +0200588 if (!cs_state_in(si->cs->state, CS_SB_EST|CS_SB_DIS|CS_SB_CLO) &&
Willy Tarreau911db9b2020-01-23 16:27:54 +0100589 (conn->flags & CO_FL_WAIT_XPRT) == 0) {
Christopher Fauletae024ce2022-03-29 19:02:31 +0200590 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
Willy Tarreauafc8a222014-11-28 15:46:27 +0100591 oc->flags |= CF_WRITE_NULL;
Christopher Faulet62e75742022-03-31 09:16:34 +0200592 if (si->cs->state == CS_ST_CON)
593 si->cs->state = CS_ST_RDY;
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200594 }
595
Christopher Faulet89e34c22021-01-21 16:22:01 +0100596 /* Report EOS on the channel if it was reached from the mux point of
597 * view.
598 *
599 * Note: This test is only required because si_cs_process is also the SI
600 * wake callback. Otherwise si_cs_recv()/si_cs_send() already take
601 * care of it.
602 */
Christopher Fauletb041b232022-03-24 10:27:02 +0100603 if (cs->endp->flags & CS_EP_EOS && !(ic->flags & CF_SHUTR)) {
Christopher Faulet89e34c22021-01-21 16:22:01 +0100604 /* we received a shutdown */
605 ic->flags |= CF_READ_NULL;
606 if (ic->flags & CF_AUTO_CLOSE)
607 channel_shutw_now(ic);
608 stream_int_read0(si);
609 }
610
Christopher Faulet297d3e22019-03-22 14:16:14 +0100611 /* Report EOI on the channel if it was reached from the mux point of
Christopher Faulet36b536d2019-11-20 11:56:33 +0100612 * view.
613 *
614 * Note: This test is only required because si_cs_process is also the SI
615 * wake callback. Otherwise si_cs_recv()/si_cs_send() already take
616 * care of it.
617 */
Christopher Fauletb041b232022-03-24 10:27:02 +0100618 if ((cs->endp->flags & CS_EP_EOI) && !(ic->flags & CF_EOI))
Christopher Faulet8e9e3ef2019-05-17 09:14:10 +0200619 ic->flags |= (CF_EOI|CF_READ_PARTIAL);
Christopher Faulet203b2b02019-03-08 09:23:46 +0100620
Willy Tarreau651e1822015-09-23 20:06:13 +0200621 /* Second step : update the stream-int and channels, try to forward any
622 * pending data, then possibly wake the stream up based on the new
623 * stream-int status.
Willy Tarreau44b5dc62012-08-24 12:12:53 +0200624 */
Willy Tarreau651e1822015-09-23 20:06:13 +0200625 stream_int_notify(si);
Willy Tarreaua64c7032019-08-01 14:17:02 +0200626 stream_release_buffers(si_strm(si));
Willy Tarreau2396c1c2012-10-03 21:12:16 +0200627 return 0;
Willy Tarreaufd31e532012-07-23 18:24:25 +0200628}
Willy Tarreau2c6be842012-07-06 17:12:34 +0200629
Willy Tarreau5368d802012-08-21 18:22:06 +0200630/*
631 * This function is called to send buffer data to a stream socket.
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200632 * It calls the mux layer's snd_buf function. It relies on the
Godbach4f489902013-12-04 17:24:06 +0800633 * caller to commit polling changes. The caller should check conn->flags
634 * for errors.
Willy Tarreau5368d802012-08-21 18:22:06 +0200635 */
Christopher Faulet49416232022-02-28 09:14:46 +0100636static int si_cs_send(struct conn_stream *cs)
Willy Tarreau5368d802012-08-21 18:22:06 +0200637{
Christopher Faulet693b23b2022-02-28 09:09:05 +0100638 struct connection *conn = __cs_conn(cs);
Christopher Fauletf835dea2021-12-21 14:35:17 +0100639 struct stream_interface *si = cs_si(cs);
Christopher Faulete05bf9e2022-03-29 15:23:40 +0200640 struct stream *s = si_strm(si);
Willy Tarreauafc8a222014-11-28 15:46:27 +0100641 struct channel *oc = si_oc(si);
Willy Tarreau5368d802012-08-21 18:22:06 +0200642 int ret;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200643 int did_send = 0;
644
Christopher Fauletb041b232022-03-24 10:27:02 +0100645 if (cs->endp->flags & (CS_EP_ERROR|CS_EP_ERR_PENDING) || si_is_conn_error(si)) {
Olivier Houchardc31e2cb2019-06-24 16:08:08 +0200646 /* We're probably there because the tasklet was woken up,
647 * but process_stream() ran before, detected there were an
Christopher Faulet62e75742022-03-31 09:16:34 +0200648 * error and put the si back to CS_ST_TAR. There's still
Olivier Houchardc31e2cb2019-06-24 16:08:08 +0200649 * CO_FL_ERROR on the connection but we don't want to add
Christopher Faulet6cd56d52022-03-30 10:47:32 +0200650 * CS_EP_ERROR back, so give up
Olivier Houchardc31e2cb2019-06-24 16:08:08 +0200651 */
Christopher Faulet62e75742022-03-31 09:16:34 +0200652 if (si->cs->state < CS_ST_CON)
Olivier Houchardc31e2cb2019-06-24 16:08:08 +0200653 return 0;
Christopher Faulet6cd56d52022-03-30 10:47:32 +0200654 cs->endp->flags |= CS_EP_ERROR;
Olivier Houchardc2aa7112018-09-11 18:27:21 +0200655 return 1;
Willy Tarreaubddf7fc2018-12-19 17:17:10 +0100656 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200657
Christopher Faulet328ed222019-09-23 15:57:29 +0200658 /* We're already waiting to be able to send, give up */
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200659 if (si->cs->wait_event.events & SUB_RETRY_SEND)
Christopher Faulet328ed222019-09-23 15:57:29 +0200660 return 0;
661
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200662 /* we might have been called just after an asynchronous shutw */
Willy Tarreauf22758d2020-01-23 18:25:23 +0100663 if (oc->flags & CF_SHUTW)
Olivier Houchardc2aa7112018-09-11 18:27:21 +0200664 return 1;
Willy Tarreau5368d802012-08-21 18:22:06 +0200665
Christopher Faulete96993b2020-07-30 09:26:46 +0200666 /* we must wait because the mux is not installed yet */
667 if (!conn->mux)
668 return 0;
669
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200670 if (oc->pipe && conn->xprt->snd_pipe && conn->mux->snd_pipe) {
671 ret = conn->mux->snd_pipe(cs, oc->pipe);
Christopher Faulet86162db2019-07-05 11:49:11 +0200672 if (ret > 0)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200673 did_send = 1;
Willy Tarreau5368d802012-08-21 18:22:06 +0200674
Willy Tarreauafc8a222014-11-28 15:46:27 +0100675 if (!oc->pipe->data) {
676 put_pipe(oc->pipe);
677 oc->pipe = NULL;
Willy Tarreau5368d802012-08-21 18:22:06 +0200678 }
679
Christopher Faulet3f76f4c2018-11-20 10:21:08 +0100680 if (oc->pipe)
681 goto end;
Willy Tarreau5368d802012-08-21 18:22:06 +0200682 }
683
684 /* At this point, the pipe is empty, but we may still have data pending
685 * in the normal buffer.
686 */
Christopher Faulet55dec0d2018-11-20 10:30:02 +0100687 if (co_data(oc)) {
688 /* when we're here, we already know that there is no spliced
689 * data left, and that there are sendable buffered data.
690 */
Willy Tarreau5368d802012-08-21 18:22:06 +0200691
Willy Tarreau5368d802012-08-21 18:22:06 +0200692 /* check if we want to inform the kernel that we're interested in
693 * sending more data after this call. We want this if :
694 * - we're about to close after this last send and want to merge
695 * the ongoing FIN with the last segment.
696 * - we know we can't send everything at once and must get back
697 * here because of unaligned data
698 * - there is still a finite amount of data to forward
699 * The test is arranged so that the most common case does only 2
700 * tests.
701 */
Willy Tarreau1049b1f2014-02-02 01:51:17 +0100702 unsigned int send_flag = 0;
Willy Tarreau5368d802012-08-21 18:22:06 +0200703
Willy Tarreauafc8a222014-11-28 15:46:27 +0100704 if ((!(oc->flags & (CF_NEVER_WAIT|CF_SEND_DONTWAIT)) &&
705 ((oc->to_forward && oc->to_forward != CHN_INFINITE_FORWARD) ||
Willy Tarreau8945bb62020-06-19 17:07:06 +0200706 (oc->flags & CF_EXPECT_MORE) ||
Christopher Faulet9e3dc832020-07-22 16:28:44 +0200707 (IS_HTX_STRM(si_strm(si)) &&
708 (!(oc->flags & (CF_EOI|CF_SHUTR)) && htx_expect_more(htxbuf(&oc->buf)))))) ||
Willy Tarreauecd2e152017-11-07 15:07:25 +0100709 ((oc->flags & CF_ISRESP) &&
710 ((oc->flags & (CF_AUTO_CLOSE|CF_SHUTW_NOW)) == (CF_AUTO_CLOSE|CF_SHUTW_NOW))))
Willy Tarreau1049b1f2014-02-02 01:51:17 +0100711 send_flag |= CO_SFL_MSG_MORE;
Willy Tarreau5368d802012-08-21 18:22:06 +0200712
Willy Tarreauafc8a222014-11-28 15:46:27 +0100713 if (oc->flags & CF_STREAMER)
Willy Tarreau7bed9452014-02-02 02:00:24 +0100714 send_flag |= CO_SFL_STREAMER;
715
Christopher Faulete05bf9e2022-03-29 15:23:40 +0200716 if (s->txn && s->txn->flags & TX_L7_RETRY && !b_data(&s->txn->l7_buffer)) {
Christopher Faulet9f5382e2021-05-21 13:46:14 +0200717 /* If we want to be able to do L7 retries, copy
718 * the data we're about to send, so that we are able
719 * to resend them if needed
720 */
721 /* Try to allocate a buffer if we had none.
722 * If it fails, the next test will just
723 * disable the l7 retries by setting
724 * l7_conn_retries to 0.
725 */
Christopher Faulete05bf9e2022-03-29 15:23:40 +0200726 if (s->txn->req.msg_state != HTTP_MSG_DONE)
727 s->txn->flags &= ~TX_L7_RETRY;
Christopher Faulet9f5382e2021-05-21 13:46:14 +0200728 else {
Christopher Faulete05bf9e2022-03-29 15:23:40 +0200729 if (b_alloc(&s->txn->l7_buffer) == NULL)
730 s->txn->flags &= ~TX_L7_RETRY;
Christopher Faulet9f5382e2021-05-21 13:46:14 +0200731 else {
Christopher Faulete05bf9e2022-03-29 15:23:40 +0200732 memcpy(b_orig(&s->txn->l7_buffer),
Christopher Faulet9f5382e2021-05-21 13:46:14 +0200733 b_orig(&oc->buf),
734 b_size(&oc->buf));
Christopher Faulete05bf9e2022-03-29 15:23:40 +0200735 s->txn->l7_buffer.head = co_data(oc);
736 b_add(&s->txn->l7_buffer, co_data(oc));
Christopher Faulet9f5382e2021-05-21 13:46:14 +0200737 }
738
739 }
740 }
741
Christopher Faulet897d6122021-12-17 17:28:35 +0100742 ret = conn->mux->snd_buf(cs, &oc->buf, co_data(oc), send_flag);
Godbache68e02d2013-10-11 15:48:29 +0800743 if (ret > 0) {
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200744 did_send = 1;
Willy Tarreau84240042022-02-28 16:51:23 +0100745 c_rew(oc, ret);
Willy Tarreaudeccd112018-06-14 18:38:55 +0200746 c_realign_if_empty(oc);
747
748 if (!co_data(oc)) {
Godbache68e02d2013-10-11 15:48:29 +0800749 /* Always clear both flags once everything has been sent, they're one-shot */
Willy Tarreauafc8a222014-11-28 15:46:27 +0100750 oc->flags &= ~(CF_EXPECT_MORE | CF_SEND_DONTWAIT);
Godbache68e02d2013-10-11 15:48:29 +0800751 }
Godbache68e02d2013-10-11 15:48:29 +0800752 /* if some data remain in the buffer, it's only because the
753 * system buffers are full, we will try next time.
754 */
Willy Tarreau5368d802012-08-21 18:22:06 +0200755 }
Godbache68e02d2013-10-11 15:48:29 +0800756 }
Christopher Faulet55dec0d2018-11-20 10:30:02 +0100757
Willy Tarreauf6975aa2018-11-15 14:33:05 +0100758 end:
Christopher Faulet86162db2019-07-05 11:49:11 +0200759 if (did_send) {
760 oc->flags |= CF_WRITE_PARTIAL | CF_WROTE_DATA;
Christopher Faulet62e75742022-03-31 09:16:34 +0200761 if (si->cs->state == CS_ST_CON)
762 si->cs->state = CS_ST_RDY;
Christopher Faulet037b3eb2019-07-05 13:44:29 +0200763
764 si_rx_room_rdy(si_opposite(si));
Christopher Faulet86162db2019-07-05 11:49:11 +0200765 }
766
Christopher Fauletb041b232022-03-24 10:27:02 +0100767 if (cs->endp->flags & (CS_EP_ERROR|CS_EP_ERR_PENDING)) {
Christopher Faulet6cd56d52022-03-30 10:47:32 +0200768 cs->endp->flags |= CS_EP_ERROR;
Christopher Faulet86162db2019-07-05 11:49:11 +0200769 return 1;
770 }
771
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200772 /* 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 +0100773 if (!channel_is_empty(oc))
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200774 conn->mux->subscribe(cs, SUB_RETRY_SEND, &si->cs->wait_event);
Olivier Houchardf6535282018-08-31 17:29:12 +0200775 return did_send;
Willy Tarreau5368d802012-08-21 18:22:06 +0200776}
777
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200778/* This is the ->process() function for any conn-stream's wait_event task.
Willy Tarreau8ccd2082018-11-07 07:47:52 +0100779 * It's assigned during the stream-interface's initialization, for any type of
780 * stream interface. Thus it is always safe to perform a tasklet_wakeup() on a
781 * stream interface, as the presence of the CS is checked there.
782 */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100783struct task *si_cs_io_cb(struct task *t, void *ctx, unsigned int state)
Olivier Houchard91894cb2018-08-02 18:06:28 +0200784{
Olivier Houchard8f0b4c62018-08-02 18:21:38 +0200785 struct stream_interface *si = ctx;
Christopher Faulet13a35e52021-12-20 15:34:16 +0100786 struct conn_stream *cs = si->cs;
Olivier Houchardf6535282018-08-31 17:29:12 +0200787 int ret = 0;
Olivier Houcharda6ff0352018-08-21 15:59:43 +0200788
Christopher Faulet0256da12021-12-15 09:50:17 +0100789 if (!cs_conn(cs))
Willy Tarreau74163142021-03-13 11:30:19 +0100790 return t;
Willy Tarreau8ccd2082018-11-07 07:47:52 +0100791
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200792 if (!(cs->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(si_oc(si)))
Olivier Houchardf6535282018-08-31 17:29:12 +0200793 ret = si_cs_send(cs);
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200794 if (!(cs->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardf6535282018-08-31 17:29:12 +0200795 ret |= si_cs_recv(cs);
796 if (ret != 0)
Olivier Houchardc2aa7112018-09-11 18:27:21 +0200797 si_cs_process(cs);
Olivier Houchardf6535282018-08-31 17:29:12 +0200798
Willy Tarreaua64c7032019-08-01 14:17:02 +0200799 stream_release_buffers(si_strm(si));
Willy Tarreau74163142021-03-13 11:30:19 +0100800 return t;
Olivier Houchard91894cb2018-08-02 18:06:28 +0200801}
802
Willy Tarreau25f13102015-09-24 11:32:22 +0200803/* This function is designed to be called from within the stream handler to
Willy Tarreau236c4292019-06-06 08:19:20 +0200804 * update the input channel's expiration timer and the stream interface's
805 * Rx flags based on the channel's flags. It needs to be called only once
806 * after the channel's flags have settled down, and before they are cleared,
807 * though it doesn't harm to call it as often as desired (it just slightly
808 * hurts performance). It must not be called from outside of the stream
809 * handler, as what it does will be used to compute the stream task's
810 * expiration.
Willy Tarreau25f13102015-09-24 11:32:22 +0200811 */
Willy Tarreau236c4292019-06-06 08:19:20 +0200812void si_update_rx(struct stream_interface *si)
Willy Tarreau25f13102015-09-24 11:32:22 +0200813{
814 struct channel *ic = si_ic(si);
Willy Tarreau25f13102015-09-24 11:32:22 +0200815
Willy Tarreau236c4292019-06-06 08:19:20 +0200816 if (ic->flags & CF_SHUTR) {
817 si_rx_shut_blk(si);
818 return;
819 }
Willy Tarreaub26a6f92018-11-14 17:10:36 +0100820
Willy Tarreau236c4292019-06-06 08:19:20 +0200821 /* Read not closed, update FD status and timeout for reads */
822 if (ic->flags & CF_DONT_READ)
823 si_rx_chan_blk(si);
824 else
825 si_rx_chan_rdy(si);
Willy Tarreaub26a6f92018-11-14 17:10:36 +0100826
Christopher Faulet69fad002021-10-29 14:55:59 +0200827 if (!channel_is_empty(ic) || !channel_may_recv(ic)) {
Willy Tarreau236c4292019-06-06 08:19:20 +0200828 /* stop reading, imposed by channel's policy or contents */
829 si_rx_room_blk(si);
Willy Tarreau25f13102015-09-24 11:32:22 +0200830 }
Willy Tarreau236c4292019-06-06 08:19:20 +0200831 else {
832 /* (re)start reading and update timeout. Note: we don't recompute the timeout
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -0700833 * every time we get here, otherwise it would risk never to expire. We only
Willy Tarreau236c4292019-06-06 08:19:20 +0200834 * update it if is was not yet set. The stream socket handler will already
835 * have updated it if there has been a completed I/O.
836 */
837 si_rx_room_rdy(si);
838 }
839 if (si->flags & SI_FL_RXBLK_ANY & ~SI_FL_RX_WAIT_EP)
840 ic->rex = TICK_ETERNITY;
841 else if (!(ic->flags & CF_READ_NOEXP) && !tick_isset(ic->rex))
842 ic->rex = tick_add_ifset(now_ms, ic->rto);
Willy Tarreau25f13102015-09-24 11:32:22 +0200843
Christopher Fauletda098e62022-03-31 17:44:45 +0200844 cs_chk_rcv(si->cs);
Willy Tarreau236c4292019-06-06 08:19:20 +0200845}
846
847/* This function is designed to be called from within the stream handler to
848 * update the output channel's expiration timer and the stream interface's
849 * Tx flags based on the channel's flags. It needs to be called only once
850 * after the channel's flags have settled down, and before they are cleared,
851 * though it doesn't harm to call it as often as desired (it just slightly
852 * hurts performance). It must not be called from outside of the stream
853 * handler, as what it does will be used to compute the stream task's
854 * expiration.
855 */
856void si_update_tx(struct stream_interface *si)
857{
858 struct channel *oc = si_oc(si);
859 struct channel *ic = si_ic(si);
860
861 if (oc->flags & CF_SHUTW)
862 return;
863
864 /* Write not closed, update FD status and timeout for writes */
865 if (channel_is_empty(oc)) {
866 /* stop writing */
867 if (!(si->flags & SI_FL_WAIT_DATA)) {
868 if ((oc->flags & CF_SHUTW_NOW) == 0)
869 si->flags |= SI_FL_WAIT_DATA;
870 oc->wex = TICK_ETERNITY;
Willy Tarreau25f13102015-09-24 11:32:22 +0200871 }
Willy Tarreau236c4292019-06-06 08:19:20 +0200872 return;
873 }
874
875 /* (re)start writing and update timeout. Note: we don't recompute the timeout
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -0700876 * every time we get here, otherwise it would risk never to expire. We only
Willy Tarreau236c4292019-06-06 08:19:20 +0200877 * update it if is was not yet set. The stream socket handler will already
878 * have updated it if there has been a completed I/O.
879 */
880 si->flags &= ~SI_FL_WAIT_DATA;
881 if (!tick_isset(oc->wex)) {
882 oc->wex = tick_add_ifset(now_ms, oc->wto);
Christopher Fauleta7285182022-03-30 15:43:23 +0200883 if (tick_isset(ic->rex) && !(si->cs->flags & CS_FL_INDEP_STR)) {
Willy Tarreau236c4292019-06-06 08:19:20 +0200884 /* Note: depending on the protocol, we don't know if we're waiting
885 * for incoming data or not. So in order to prevent the socket from
886 * expiring read timeouts during writes, we refresh the read timeout,
887 * except if it was already infinite or if we have explicitly setup
888 * independent streams.
Willy Tarreau25f13102015-09-24 11:32:22 +0200889 */
Willy Tarreau236c4292019-06-06 08:19:20 +0200890 ic->rex = tick_add_ifset(now_ms, ic->rto);
Willy Tarreau25f13102015-09-24 11:32:22 +0200891 }
892 }
893}
894
Christopher Faulet9936dc62022-02-28 09:21:58 +0100895/* This tries to perform a synchronous receive on the stream interface to
896 * try to collect last arrived data. In practice it's only implemented on
897 * conn_streams. Returns 0 if nothing was done, non-zero if new data or a
898 * shutdown were collected. This may result on some delayed receive calls
899 * to be programmed and performed later, though it doesn't provide any
900 * such guarantee.
901 */
902int si_sync_recv(struct stream_interface *si)
903{
Christopher Faulet62e75742022-03-31 09:16:34 +0200904 if (!cs_state_in(si->cs->state, CS_SB_RDY|CS_SB_EST))
Christopher Faulet9936dc62022-02-28 09:21:58 +0100905 return 0;
906
907 if (!cs_conn_mux(si->cs))
908 return 0; // only conn_streams are supported
909
Christopher Faulet2f35e7b2022-03-31 11:09:28 +0200910 if (si->cs->wait_event.events & SUB_RETRY_RECV)
Christopher Faulet9936dc62022-02-28 09:21:58 +0100911 return 0; // already subscribed
912
913 if (!si_rx_endp_ready(si) || si_rx_blocked(si))
914 return 0; // already failed
915
916 return si_cs_recv(si->cs);
917}
918
Willy Tarreau3b285d72019-06-06 08:20:17 +0200919/* perform a synchronous send() for the stream interface. The CF_WRITE_NULL and
920 * CF_WRITE_PARTIAL flags are cleared prior to the attempt, and will possibly
921 * be updated in case of success.
922 */
923void si_sync_send(struct stream_interface *si)
924{
925 struct channel *oc = si_oc(si);
Willy Tarreau3b285d72019-06-06 08:20:17 +0200926
927 oc->flags &= ~(CF_WRITE_NULL|CF_WRITE_PARTIAL);
928
929 if (oc->flags & CF_SHUTW)
930 return;
931
932 if (channel_is_empty(oc))
933 return;
934
Christopher Faulet62e75742022-03-31 09:16:34 +0200935 if (!cs_state_in(si->cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
Willy Tarreau3b285d72019-06-06 08:20:17 +0200936 return;
937
Christopher Faulet13a35e52021-12-20 15:34:16 +0100938 if (!cs_conn_mux(si->cs))
Willy Tarreau3b285d72019-06-06 08:20:17 +0200939 return;
940
Christopher Faulet13a35e52021-12-20 15:34:16 +0100941 si_cs_send(si->cs);
Willy Tarreau3b285d72019-06-06 08:20:17 +0200942}
943
Willy Tarreaud14844a2018-11-08 18:15:29 +0100944/* Updates at once the channel flags, and timers of both stream interfaces of a
945 * same stream, to complete the work after the analysers, then updates the data
946 * layer below. This will ensure that any synchronous update performed at the
947 * data layer will be reflected in the channel flags and/or stream-interface.
Willy Tarreau829bd472019-06-06 09:17:23 +0200948 * Note that this does not change the stream interface's current state, though
949 * it updates the previous state to the current one.
Willy Tarreaud14844a2018-11-08 18:15:29 +0100950 */
951void si_update_both(struct stream_interface *si_f, struct stream_interface *si_b)
952{
953 struct channel *req = si_ic(si_f);
954 struct channel *res = si_oc(si_f);
Willy Tarreaud14844a2018-11-08 18:15:29 +0100955
956 req->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_READ_ATTACHED|CF_WRITE_NULL|CF_WRITE_PARTIAL);
957 res->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_READ_ATTACHED|CF_WRITE_NULL|CF_WRITE_PARTIAL);
958
Christopher Faulet62e75742022-03-31 09:16:34 +0200959 si_strm(si_b)->prev_conn_state = si_b->cs->state;
Willy Tarreaud14844a2018-11-08 18:15:29 +0100960
Willy Tarreaubf89ff32018-11-09 14:59:25 +0100961 /* let's recompute both sides states */
Christopher Faulet62e75742022-03-31 09:16:34 +0200962 if (cs_state_in(si_f->cs->state, CS_SB_RDY|CS_SB_EST))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100963 si_update(si_f);
Willy Tarreaubf89ff32018-11-09 14:59:25 +0100964
Christopher Faulet62e75742022-03-31 09:16:34 +0200965 if (cs_state_in(si_b->cs->state, CS_SB_RDY|CS_SB_EST))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100966 si_update(si_b);
Willy Tarreaubf89ff32018-11-09 14:59:25 +0100967
968 /* stream ints are processed outside of process_stream() and must be
969 * handled at the latest moment.
970 */
Christopher Faulet13a35e52021-12-20 15:34:16 +0100971 if (cs_appctx(si_f->cs) &&
Willy Tarreau32742fd2018-11-14 14:07:59 +0100972 ((si_rx_endp_ready(si_f) && !si_rx_blocked(si_f)) ||
973 (si_tx_endp_ready(si_f) && !si_tx_blocked(si_f))))
Christopher Faulet693b23b2022-02-28 09:09:05 +0100974 appctx_wakeup(__cs_appctx(si_f->cs));
Willy Tarreaud14844a2018-11-08 18:15:29 +0100975
Christopher Faulet13a35e52021-12-20 15:34:16 +0100976 if (cs_appctx(si_b->cs) &&
Willy Tarreau32742fd2018-11-14 14:07:59 +0100977 ((si_rx_endp_ready(si_b) && !si_rx_blocked(si_b)) ||
978 (si_tx_endp_ready(si_b) && !si_tx_blocked(si_b))))
Christopher Faulet693b23b2022-02-28 09:09:05 +0100979 appctx_wakeup(__cs_appctx(si_b->cs));
Willy Tarreaud14844a2018-11-08 18:15:29 +0100980}
981
Willy Tarreau8b3d7df2013-09-29 14:51:58 +0200982/*
983 * This function performs a shutdown-read on a stream interface attached to
984 * a connection in a connected or init state (it does nothing for other
985 * states). It either shuts the read side or marks itself as closed. The buffer
986 * flags are updated to reflect the new state. If the stream interface has
Christopher Faulet8abe7122022-03-30 15:10:18 +0200987 * CS_FL_NOHALF, we also forward the close to the write side. If a control
Willy Tarreau8b3d7df2013-09-29 14:51:58 +0200988 * layer is defined, then it is supposed to be a socket layer and file
Willy Tarreau6fe15412013-09-29 15:16:03 +0200989 * descriptors are then shutdown or closed accordingly. The function
990 * automatically disables polling if needed.
Willy Tarreau8b3d7df2013-09-29 14:51:58 +0200991 */
Willy Tarreau6fe15412013-09-29 15:16:03 +0200992static void stream_int_shutr_conn(struct stream_interface *si)
Willy Tarreau8b3d7df2013-09-29 14:51:58 +0200993{
Christopher Faulet13a35e52021-12-20 15:34:16 +0100994 struct conn_stream *cs = si->cs;
Willy Tarreauafc8a222014-11-28 15:46:27 +0100995 struct channel *ic = si_ic(si);
Willy Tarreau8b3d7df2013-09-29 14:51:58 +0200996
Christopher Faulet13a35e52021-12-20 15:34:16 +0100997 BUG_ON(!cs_conn(cs));
998
Willy Tarreauabb5d422018-11-14 16:58:52 +0100999 si_rx_shut_blk(si);
Willy Tarreauafc8a222014-11-28 15:46:27 +01001000 if (ic->flags & CF_SHUTR)
Willy Tarreau6fe15412013-09-29 15:16:03 +02001001 return;
Willy Tarreauafc8a222014-11-28 15:46:27 +01001002 ic->flags |= CF_SHUTR;
1003 ic->rex = TICK_ETERNITY;
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001004
Christopher Faulet62e75742022-03-31 09:16:34 +02001005 if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
Willy Tarreau6fe15412013-09-29 15:16:03 +02001006 return;
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001007
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001008 if (si_oc(si)->flags & CF_SHUTW) {
Christopher Faulet69ef6c92022-03-31 14:20:00 +02001009 cs_conn_close(cs);
Christopher Faulet62e75742022-03-31 09:16:34 +02001010 cs->state = CS_ST_DIS;
Christopher Fauletae024ce2022-03-29 19:02:31 +02001011 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001012 }
Christopher Faulet8abe7122022-03-30 15:10:18 +02001013 else if (si->cs->flags & CS_FL_NOHALF) {
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001014 /* we want to immediately forward this close to the write side */
1015 return stream_int_shutw_conn(si);
1016 }
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001017}
1018
1019/*
1020 * This function performs a shutdown-write on a stream interface attached to
1021 * a connection in a connected or init state (it does nothing for other
1022 * states). It either shuts the write side or marks itself as closed. The
1023 * buffer flags are updated to reflect the new state. It does also close
1024 * everything if the SI was marked as being in error state. If there is a
Willy Tarreau1398aa12015-03-12 23:04:07 +01001025 * data-layer shutdown, it is called.
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001026 */
Willy Tarreau6fe15412013-09-29 15:16:03 +02001027static void stream_int_shutw_conn(struct stream_interface *si)
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001028{
Christopher Faulet13a35e52021-12-20 15:34:16 +01001029 struct conn_stream *cs = si->cs;
Willy Tarreauafc8a222014-11-28 15:46:27 +01001030 struct channel *ic = si_ic(si);
1031 struct channel *oc = si_oc(si);
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001032
Christopher Faulet13a35e52021-12-20 15:34:16 +01001033 BUG_ON(!cs_conn(cs));
1034
Willy Tarreauafc8a222014-11-28 15:46:27 +01001035 oc->flags &= ~CF_SHUTW_NOW;
1036 if (oc->flags & CF_SHUTW)
Willy Tarreau6fe15412013-09-29 15:16:03 +02001037 return;
Willy Tarreauafc8a222014-11-28 15:46:27 +01001038 oc->flags |= CF_SHUTW;
1039 oc->wex = TICK_ETERNITY;
Willy Tarreau43e69dc2018-11-06 19:23:03 +01001040 si_done_get(si);
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001041
Christopher Faulet1d987772022-03-29 18:03:35 +02001042 if (tick_isset(si->cs->hcto)) {
1043 ic->rto = si->cs->hcto;
Hongbo Longe39683c2017-03-10 18:41:51 +01001044 ic->rex = tick_add(now_ms, ic->rto);
1045 }
1046
Christopher Faulet62e75742022-03-31 09:16:34 +02001047 switch (cs->state) {
1048 case CS_ST_RDY:
1049 case CS_ST_EST:
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001050 /* we have to shut before closing, otherwise some short messages
1051 * may never leave the system, especially when there are remaining
1052 * unread data in the socket input buffer, or when nolinger is set.
Christopher Faulet8abe7122022-03-30 15:10:18 +02001053 * However, if CS_FL_NOLINGER is explicitly set, we know there is
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001054 * no risk so we close both sides immediately.
1055 */
Willy Tarreau51d0a7e2019-01-31 19:09:59 +01001056
Christopher Faulet6cd56d52022-03-30 10:47:32 +02001057 if (cs->endp->flags & CS_EP_ERROR) {
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001058 /* quick close, the socket is already shut anyway */
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001059 }
Christopher Faulet8abe7122022-03-30 15:10:18 +02001060 else if (cs->flags & CS_FL_NOLINGER) {
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001061 /* unclean data-layer shutdown, typically an aborted request
1062 * or a forwarded shutdown from a client to a server due to
1063 * option abortonclose. No need for the TLS layer to try to
1064 * emit a shutdown message.
1065 */
Christopher Faulet69ef6c92022-03-31 14:20:00 +02001066 cs_conn_shutw(cs, CO_SHW_SILENT);
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001067 }
1068 else {
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001069 /* clean data-layer shutdown. This only happens on the
1070 * frontend side, or on the backend side when forwarding
1071 * a client close in TCP mode or in HTTP TUNNEL mode
1072 * while option abortonclose is set. We want the TLS
1073 * layer to try to signal it to the peer before we close.
1074 */
Christopher Faulet69ef6c92022-03-31 14:20:00 +02001075 cs_conn_shutw(cs, CO_SHW_NORMAL);
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001076
Willy Tarreaua5ea7512020-12-11 10:24:05 +01001077 if (!(ic->flags & (CF_SHUTR|CF_DONT_READ)))
Willy Tarreaua553ae92017-10-05 18:52:17 +02001078 return;
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001079 }
1080
1081 /* fall through */
Christopher Faulet62e75742022-03-31 09:16:34 +02001082 case CS_ST_CON:
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001083 /* we may have to close a pending connection, and mark the
1084 * response buffer as shutr
1085 */
Christopher Faulet69ef6c92022-03-31 14:20:00 +02001086 cs_conn_close(cs);
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001087 /* fall through */
Christopher Faulet62e75742022-03-31 09:16:34 +02001088 case CS_ST_CER:
1089 case CS_ST_QUE:
1090 case CS_ST_TAR:
1091 cs->state = CS_ST_DIS;
Willy Tarreau4a59f2f2013-10-24 20:10:45 +02001092 /* fall through */
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001093 default:
Christopher Faulet8abe7122022-03-30 15:10:18 +02001094 cs->flags &= ~CS_FL_NOLINGER;
Willy Tarreauabb5d422018-11-14 16:58:52 +01001095 si_rx_shut_blk(si);
Willy Tarreauafc8a222014-11-28 15:46:27 +01001096 ic->flags |= CF_SHUTR;
1097 ic->rex = TICK_ETERNITY;
Christopher Fauletae024ce2022-03-29 19:02:31 +02001098 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
Willy Tarreau100c4672012-08-20 12:06:26 +02001099 }
1100}
1101
Willy Tarreau46a8d922012-08-20 12:38:36 +02001102/* This function is used for inter-stream-interface calls. It is called by the
1103 * consumer to inform the producer side that it may be interested in checking
1104 * for free space in the buffer. Note that it intentionally does not update
1105 * timeouts, so that we can still check them later at wake-up. This function is
1106 * dedicated to connection-based stream interfaces.
1107 */
Willy Tarreauc5788912012-08-24 18:12:41 +02001108static void stream_int_chk_rcv_conn(struct stream_interface *si)
Willy Tarreau46a8d922012-08-20 12:38:36 +02001109{
Christopher Fauletb3e0de42018-10-11 13:54:13 +02001110 /* (re)start reading */
Christopher Faulet62e75742022-03-31 09:16:34 +02001111 if (cs_state_in(si->cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
Christopher Faulet2f35e7b2022-03-31 11:09:28 +02001112 tasklet_wakeup(si->cs->wait_event.tasklet);
Willy Tarreau46a8d922012-08-20 12:38:36 +02001113}
1114
1115
Willy Tarreaude5722c2012-08-20 15:01:10 +02001116/* This function is used for inter-stream-interface calls. It is called by the
1117 * producer to inform the consumer side that it may be interested in checking
1118 * for data in the buffer. Note that it intentionally does not update timeouts,
1119 * so that we can still check them later at wake-up.
1120 */
Willy Tarreauc5788912012-08-24 18:12:41 +02001121static void stream_int_chk_snd_conn(struct stream_interface *si)
Willy Tarreaude5722c2012-08-20 15:01:10 +02001122{
Willy Tarreauafc8a222014-11-28 15:46:27 +01001123 struct channel *oc = si_oc(si);
Christopher Faulet13a35e52021-12-20 15:34:16 +01001124 struct conn_stream *cs = si->cs;
Christopher Faulet897d6122021-12-17 17:28:35 +01001125
Willy Tarreau0c3205a2022-03-23 11:11:31 +01001126 BUG_ON(!cs_conn(cs));
Willy Tarreaude5722c2012-08-20 15:01:10 +02001127
Christopher Faulet62e75742022-03-31 09:16:34 +02001128 if (unlikely(!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST) ||
Olivier Houchardb2fc04e2019-04-11 13:56:26 +02001129 (oc->flags & CF_SHUTW)))
Willy Tarreaude5722c2012-08-20 15:01:10 +02001130 return;
Willy Tarreaude5722c2012-08-20 15:01:10 +02001131
Willy Tarreauafc8a222014-11-28 15:46:27 +01001132 if (unlikely(channel_is_empty(oc))) /* called with nothing to send ! */
Willy Tarreaude5722c2012-08-20 15:01:10 +02001133 return;
1134
Willy Tarreauafc8a222014-11-28 15:46:27 +01001135 if (!oc->pipe && /* spliced data wants to be forwarded ASAP */
Willy Tarreaub0165872012-12-15 10:12:39 +01001136 !(si->flags & SI_FL_WAIT_DATA)) /* not waiting for data */
Willy Tarreaude5722c2012-08-20 15:01:10 +02001137 return;
1138
Christopher Faulet2f35e7b2022-03-31 11:09:28 +02001139 if (!(si->cs->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(si_oc(si)))
Olivier Houchard31f04e42018-10-22 16:01:09 +02001140 si_cs_send(cs);
Willy Tarreau33a09a52018-10-25 13:49:49 +02001141
Christopher Fauletb041b232022-03-24 10:27:02 +01001142 if (cs->endp->flags & (CS_EP_ERROR|CS_EP_ERR_PENDING) || si_is_conn_error(si)) {
Willy Tarreau3b9c8502017-10-25 14:22:28 +02001143 /* Write error on the file descriptor */
Christopher Faulet62e75742022-03-31 09:16:34 +02001144 if (cs->state >= CS_ST_CON)
Christopher Faulet6cd56d52022-03-30 10:47:32 +02001145 cs->endp->flags |= CS_EP_ERROR;
Willy Tarreau3b9c8502017-10-25 14:22:28 +02001146 goto out_wakeup;
Willy Tarreaude5722c2012-08-20 15:01:10 +02001147 }
1148
1149 /* OK, so now we know that some data might have been sent, and that we may
1150 * have to poll first. We have to do that too if the buffer is not empty.
1151 */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001152 if (channel_is_empty(oc)) {
Willy Tarreaude5722c2012-08-20 15:01:10 +02001153 /* the connection is established but we can't write. Either the
1154 * buffer is empty, or we just refrain from sending because the
1155 * ->o limit was reached. Maybe we just wrote the last
1156 * chunk and need to close.
1157 */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001158 if (((oc->flags & (CF_SHUTW|CF_AUTO_CLOSE|CF_SHUTW_NOW)) ==
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001159 (CF_AUTO_CLOSE|CF_SHUTW_NOW)) &&
Christopher Faulet62e75742022-03-31 09:16:34 +02001160 cs_state_in(cs->state, CS_SB_RDY|CS_SB_EST)) {
Christopher Fauletda098e62022-03-31 17:44:45 +02001161 cs_shutw(cs);
Willy Tarreaude5722c2012-08-20 15:01:10 +02001162 goto out_wakeup;
1163 }
1164
Willy Tarreauafc8a222014-11-28 15:46:27 +01001165 if ((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == 0)
Christopher Fauletda098e62022-03-31 17:44:45 +02001166 cs->si->flags |= SI_FL_WAIT_DATA;
Willy Tarreauafc8a222014-11-28 15:46:27 +01001167 oc->wex = TICK_ETERNITY;
Willy Tarreaude5722c2012-08-20 15:01:10 +02001168 }
1169 else {
1170 /* Otherwise there are remaining data to be sent in the buffer,
1171 * which means we have to poll before doing so.
1172 */
Christopher Fauletda098e62022-03-31 17:44:45 +02001173 cs->si->flags &= ~SI_FL_WAIT_DATA;
Willy Tarreauafc8a222014-11-28 15:46:27 +01001174 if (!tick_isset(oc->wex))
1175 oc->wex = tick_add_ifset(now_ms, oc->wto);
Willy Tarreaude5722c2012-08-20 15:01:10 +02001176 }
1177
Willy Tarreauafc8a222014-11-28 15:46:27 +01001178 if (likely(oc->flags & CF_WRITE_ACTIVITY)) {
Christopher Fauletda098e62022-03-31 17:44:45 +02001179 struct channel *ic = cs_ic(cs);
Willy Tarreauafc8a222014-11-28 15:46:27 +01001180
Willy Tarreaude5722c2012-08-20 15:01:10 +02001181 /* update timeout if we have written something */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001182 if ((oc->flags & (CF_SHUTW|CF_WRITE_PARTIAL)) == CF_WRITE_PARTIAL &&
1183 !channel_is_empty(oc))
1184 oc->wex = tick_add_ifset(now_ms, oc->wto);
Willy Tarreaude5722c2012-08-20 15:01:10 +02001185
Christopher Fauleta7285182022-03-30 15:43:23 +02001186 if (tick_isset(ic->rex) && !(cs->flags & CS_FL_INDEP_STR)) {
Willy Tarreaude5722c2012-08-20 15:01:10 +02001187 /* Note: to prevent the client from expiring read timeouts
1188 * during writes, we refresh it. We only do this if the
1189 * interface is not configured for "independent streams",
1190 * because for some applications it's better not to do this,
1191 * for instance when continuously exchanging small amounts
1192 * of data which can full the socket buffers long before a
1193 * write timeout is detected.
1194 */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001195 ic->rex = tick_add_ifset(now_ms, ic->rto);
Willy Tarreaude5722c2012-08-20 15:01:10 +02001196 }
1197 }
1198
1199 /* in case of special condition (error, shutdown, end of write...), we
1200 * have to notify the task.
1201 */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001202 if (likely((oc->flags & (CF_WRITE_NULL|CF_WRITE_ERROR|CF_SHUTW)) ||
1203 ((oc->flags & CF_WAKE_WRITE) &&
1204 ((channel_is_empty(oc) && !oc->to_forward) ||
Christopher Faulet62e75742022-03-31 09:16:34 +02001205 !cs_state_in(cs->state, CS_SB_EST))))) {
Willy Tarreaude5722c2012-08-20 15:01:10 +02001206 out_wakeup:
Christopher Fauletda098e62022-03-31 17:44:45 +02001207 if (!(cs->flags & CS_FL_DONT_WAKE))
1208 task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO);
Willy Tarreaude5722c2012-08-20 15:01:10 +02001209 }
1210}
1211
Willy Tarreaueecf6ca2012-08-20 15:09:53 +02001212/*
Willy Tarreauce323de2012-08-20 21:41:06 +02001213 * This is the callback which is called by the connection layer to receive data
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001214 * into the buffer from the connection. It iterates over the mux layer's
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001215 * rcv_buf function.
Willy Tarreauce323de2012-08-20 21:41:06 +02001216 */
Christopher Faulet9936dc62022-02-28 09:21:58 +01001217static int si_cs_recv(struct conn_stream *cs)
Willy Tarreauce323de2012-08-20 21:41:06 +02001218{
Christopher Faulet693b23b2022-02-28 09:09:05 +01001219 struct connection *conn = __cs_conn(cs);
Christopher Fauletf835dea2021-12-21 14:35:17 +01001220 struct stream_interface *si = cs_si(cs);
Willy Tarreauafc8a222014-11-28 15:46:27 +01001221 struct channel *ic = si_ic(si);
Olivier Houchardf6535282018-08-31 17:29:12 +02001222 int ret, max, cur_read = 0;
Willy Tarreauce323de2012-08-20 21:41:06 +02001223 int read_poll = MAX_READ_POLL_LOOPS;
Christopher Fauletc6618d62018-10-11 15:56:04 +02001224 int flags = 0;
Willy Tarreauce323de2012-08-20 21:41:06 +02001225
Christopher Faulet04400bc2019-10-25 10:21:01 +02001226 /* If not established yet, do nothing. */
Christopher Faulet62e75742022-03-31 09:16:34 +02001227 if (cs->state != CS_ST_EST)
Christopher Faulet04400bc2019-10-25 10:21:01 +02001228 return 0;
1229
Olivier Houchardf6535282018-08-31 17:29:12 +02001230 /* If another call to si_cs_recv() failed, and we subscribed to
1231 * recv events already, give up now.
1232 */
Christopher Faulet2f35e7b2022-03-31 11:09:28 +02001233 if (si->cs->wait_event.events & SUB_RETRY_RECV)
Olivier Houchardf6535282018-08-31 17:29:12 +02001234 return 0;
Willy Tarreauce323de2012-08-20 21:41:06 +02001235
Willy Tarreauce323de2012-08-20 21:41:06 +02001236 /* maybe we were called immediately after an asynchronous shutr */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001237 if (ic->flags & CF_SHUTR)
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001238 return 1;
Willy Tarreauce323de2012-08-20 21:41:06 +02001239
Christopher Faulete96993b2020-07-30 09:26:46 +02001240 /* we must wait because the mux is not installed yet */
1241 if (!conn->mux)
1242 return 0;
1243
Willy Tarreau54e917c2017-08-30 07:35:35 +02001244 /* stop here if we reached the end of data */
Christopher Fauletb041b232022-03-24 10:27:02 +01001245 if (cs->endp->flags & CS_EP_EOS)
Christopher Faulet36b536d2019-11-20 11:56:33 +01001246 goto end_recv;
Willy Tarreau54e917c2017-08-30 07:35:35 +02001247
Christopher Fauletf061e422018-12-07 14:51:20 +01001248 /* stop immediately on errors. Note that we DON'T want to stop on
1249 * POLL_ERR, as the poller might report a write error while there
1250 * are still data available in the recv buffer. This typically
1251 * happens when we send too large a request to a backend server
1252 * which rejects it before reading it all.
1253 */
Christopher Fauletb041b232022-03-24 10:27:02 +01001254 if (!(cs->endp->flags & CS_EP_RCV_MORE)) {
Christopher Fauletf061e422018-12-07 14:51:20 +01001255 if (!conn_xprt_ready(conn))
1256 return 0;
Christopher Fauletb041b232022-03-24 10:27:02 +01001257 if (cs->endp->flags & CS_EP_ERROR)
Christopher Faulet36b536d2019-11-20 11:56:33 +01001258 goto end_recv;
Christopher Fauletf061e422018-12-07 14:51:20 +01001259 }
Willy Tarreauf26c26c2018-11-12 16:11:08 +01001260
Willy Tarreau7ab99a32018-12-18 09:15:43 +01001261 /* prepare to detect if the mux needs more room */
Christopher Fauletb041b232022-03-24 10:27:02 +01001262 cs->endp->flags &= ~CS_EP_WANT_ROOM;
Willy Tarreau7ab99a32018-12-18 09:15:43 +01001263
Willy Tarreau77e478c2018-06-19 07:03:14 +02001264 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) && !co_data(ic) &&
Willy Tarreau7e312732014-02-12 16:35:14 +01001265 global.tune.idle_timer &&
Willy Tarreauafc8a222014-11-28 15:46:27 +01001266 (unsigned short)(now_ms - ic->last_read) >= global.tune.idle_timer) {
Willy Tarreauc5890e62014-02-09 17:47:01 +01001267 /* The buffer was empty and nothing was transferred for more
1268 * than one second. This was caused by a pause and not by
1269 * congestion. Reset any streaming mode to reduce latency.
1270 */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001271 ic->xfer_small = 0;
1272 ic->xfer_large = 0;
1273 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
Willy Tarreauc5890e62014-02-09 17:47:01 +01001274 }
1275
Willy Tarreau96199b12012-08-24 00:46:52 +02001276 /* First, let's see if we may splice data across the channel without
1277 * using a buffer.
1278 */
Christopher Faulete9e48202022-03-22 18:13:29 +01001279 if (cs->endp->flags & CS_EP_MAY_SPLICE &&
Willy Tarreauafc8a222014-11-28 15:46:27 +01001280 (ic->pipe || ic->to_forward >= MIN_SPLICE_FORWARD) &&
1281 ic->flags & CF_KERN_SPLICING) {
Willy Tarreaud760eec2018-07-10 09:50:25 +02001282 if (c_data(ic)) {
Willy Tarreau96199b12012-08-24 00:46:52 +02001283 /* We're embarrassed, there are already data pending in
1284 * the buffer and we don't want to have them at two
1285 * locations at a time. Let's indicate we need some
1286 * place and ask the consumer to hurry.
1287 */
Christopher Fauletc6618d62018-10-11 15:56:04 +02001288 flags |= CO_RFL_BUF_FLUSH;
Willy Tarreau96199b12012-08-24 00:46:52 +02001289 goto abort_splice;
1290 }
Willy Tarreauce323de2012-08-20 21:41:06 +02001291
Willy Tarreauafc8a222014-11-28 15:46:27 +01001292 if (unlikely(ic->pipe == NULL)) {
1293 if (pipes_used >= global.maxpipes || !(ic->pipe = get_pipe())) {
1294 ic->flags &= ~CF_KERN_SPLICING;
Willy Tarreau96199b12012-08-24 00:46:52 +02001295 goto abort_splice;
1296 }
1297 }
1298
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001299 ret = conn->mux->rcv_pipe(cs, ic->pipe, ic->to_forward);
Willy Tarreau96199b12012-08-24 00:46:52 +02001300 if (ret < 0) {
1301 /* splice not supported on this end, let's disable it */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001302 ic->flags &= ~CF_KERN_SPLICING;
Willy Tarreau96199b12012-08-24 00:46:52 +02001303 goto abort_splice;
1304 }
Willy Tarreauce323de2012-08-20 21:41:06 +02001305
Willy Tarreau96199b12012-08-24 00:46:52 +02001306 if (ret > 0) {
Willy Tarreauafc8a222014-11-28 15:46:27 +01001307 if (ic->to_forward != CHN_INFINITE_FORWARD)
1308 ic->to_forward -= ret;
1309 ic->total += ret;
Willy Tarreau96199b12012-08-24 00:46:52 +02001310 cur_read += ret;
Willy Tarreauafc8a222014-11-28 15:46:27 +01001311 ic->flags |= CF_READ_PARTIAL;
Willy Tarreauce323de2012-08-20 21:41:06 +02001312 }
Willy Tarreau96199b12012-08-24 00:46:52 +02001313
Christopher Fauletb041b232022-03-24 10:27:02 +01001314 if (cs->endp->flags & (CS_EP_EOS|CS_EP_ERROR))
Christopher Faulet36b536d2019-11-20 11:56:33 +01001315 goto end_recv;
Willy Tarreau96199b12012-08-24 00:46:52 +02001316
Willy Tarreau61d39a02013-07-18 21:49:32 +02001317 if (conn->flags & CO_FL_WAIT_ROOM) {
1318 /* the pipe is full or we have read enough data that it
1319 * could soon be full. Let's stop before needing to poll.
1320 */
Willy Tarreaudb398432018-11-15 11:08:52 +01001321 si_rx_room_blk(si);
Willy Tarreauffb12052018-11-15 16:06:02 +01001322 goto done_recv;
Willy Tarreau61d39a02013-07-18 21:49:32 +02001323 }
Willy Tarreau56a77e52012-09-02 18:34:44 +02001324
Willy Tarreauce323de2012-08-20 21:41:06 +02001325 /* splice not possible (anymore), let's go on on standard copy */
1326 }
Willy Tarreau96199b12012-08-24 00:46:52 +02001327
1328 abort_splice:
Willy Tarreauafc8a222014-11-28 15:46:27 +01001329 if (ic->pipe && unlikely(!ic->pipe->data)) {
1330 put_pipe(ic->pipe);
1331 ic->pipe = NULL;
Willy Tarreau96199b12012-08-24 00:46:52 +02001332 }
1333
Christopher Faulete9e48202022-03-22 18:13:29 +01001334 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 +01001335 /* don't break splicing by reading, but still call rcv_buf()
1336 * to pass the flag.
1337 */
1338 goto done_recv;
1339 }
1340
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001341 /* now we'll need a input buffer for the stream */
Willy Tarreau581abd32018-10-25 10:21:41 +02001342 if (!si_alloc_ibuf(si, &(si_strm(si)->buffer_wait)))
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001343 goto end_recv;
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001344
Christopher Faulet2bc364c2021-09-21 15:22:12 +02001345 /* For an HTX stream, if the buffer is stuck (no output data with some
1346 * input data) and if the HTX message is fragmented or if its free space
1347 * wraps, we force an HTX deframentation. It is a way to have a
1348 * contiguous free space nad to let the mux to copy as much data as
1349 * possible.
1350 *
1351 * NOTE: A possible optim may be to let the mux decides if defrag is
1352 * required or not, depending on amount of data to be xferred.
1353 */
1354 if (IS_HTX_STRM(si_strm(si)) && !co_data(ic)) {
1355 struct htx *htx = htxbuf(&ic->buf);
1356
1357 if (htx_is_not_empty(htx) && ((htx->flags & HTX_FL_FRAGMENTED) || htx_space_wraps(htx)))
1358 htx_defrag(htxbuf(&ic->buf), NULL, 0);
1359 }
Christopher Faulet68a14db2021-09-21 15:14:57 +02001360
1361 /* Instruct the mux it must subscribed for read events */
1362 flags |= ((!conn_is_back(conn) && (si_strm(si)->be->options & PR_O_ABRT_CLOSE)) ? CO_RFL_KEEP_RECV : 0);
1363
Willy Tarreau61d39a02013-07-18 21:49:32 +02001364 /* Important note : if we're called with POLL_IN|POLL_HUP, it means the read polling
1365 * was enabled, which implies that the recv buffer was not full. So we have a guarantee
1366 * that if such an event is not handled above in splice, it will be handled here by
1367 * recv().
1368 */
Christopher Fauletb041b232022-03-24 10:27:02 +01001369 while ((cs->endp->flags & CS_EP_RCV_MORE) ||
Willy Tarreaud1480cc2022-03-17 16:19:09 +01001370 (!(conn->flags & CO_FL_HANDSHAKE) &&
Christopher Fauletb041b232022-03-24 10:27:02 +01001371 (!(cs->endp->flags & (CS_EP_ERROR|CS_EP_EOS))) && !(ic->flags & CF_SHUTR))) {
Christopher Faulet68a14db2021-09-21 15:14:57 +02001372 int cur_flags = flags;
1373
1374 /* Compute transient CO_RFL_* flags */
Christopher Faulet564e39c2021-09-21 15:50:55 +02001375 if (co_data(ic)) {
1376 cur_flags |= (CO_RFL_BUF_WET | CO_RFL_BUF_NOT_STUCK);
1377 }
Christopher Faulet68a14db2021-09-21 15:14:57 +02001378
Christopher Faulet4eb7d742018-10-11 15:29:21 +02001379 /* <max> may be null. This is the mux responsibility to set
Christopher Fauletb041b232022-03-24 10:27:02 +01001380 * CS_EP_RCV_MORE on the CS if more space is needed.
Christopher Faulet4eb7d742018-10-11 15:29:21 +02001381 */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001382 max = channel_recv_max(ic);
Christopher Faulet897d6122021-12-17 17:28:35 +01001383 ret = conn->mux->rcv_buf(cs, &ic->buf, max, cur_flags);
Willy Tarreau674e0ad2018-12-05 13:45:41 +01001384
Christopher Fauletb041b232022-03-24 10:27:02 +01001385 if (cs->endp->flags & CS_EP_WANT_ROOM) {
1386 /* CS_EP_WANT_ROOM must not be reported if the channel's
Christopher Fauletae179252022-02-21 16:12:00 +01001387 * buffer is empty.
1388 */
1389 BUG_ON(c_empty(ic));
1390
Willy Tarreaudb398432018-11-15 11:08:52 +01001391 si_rx_room_blk(si);
Christopher Fauletdf994082021-09-23 14:17:20 +02001392 /* Add READ_PARTIAL because some data are pending but
1393 * cannot be xferred to the channel
1394 */
1395 ic->flags |= CF_READ_PARTIAL;
1396 }
Willy Tarreau6577b482017-12-10 21:19:33 +01001397
Willy Tarreauf26c26c2018-11-12 16:11:08 +01001398 if (ret <= 0) {
Willy Tarreau1ac5f202019-12-03 18:08:45 +01001399 /* if we refrained from reading because we asked for a
1400 * flush to satisfy rcv_pipe(), we must not subscribe
1401 * and instead report that there's not enough room
1402 * here to proceed.
1403 */
1404 if (flags & CO_RFL_BUF_FLUSH)
1405 si_rx_room_blk(si);
Willy Tarreauce323de2012-08-20 21:41:06 +02001406 break;
Willy Tarreauf26c26c2018-11-12 16:11:08 +01001407 }
Willy Tarreauce323de2012-08-20 21:41:06 +02001408
1409 cur_read += ret;
1410
1411 /* if we're allowed to directly forward data, we must update ->o */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001412 if (ic->to_forward && !(ic->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreauce323de2012-08-20 21:41:06 +02001413 unsigned long fwd = ret;
Willy Tarreauafc8a222014-11-28 15:46:27 +01001414 if (ic->to_forward != CHN_INFINITE_FORWARD) {
1415 if (fwd > ic->to_forward)
1416 fwd = ic->to_forward;
1417 ic->to_forward -= fwd;
Willy Tarreauce323de2012-08-20 21:41:06 +02001418 }
Willy Tarreaubcbd3932018-06-06 07:13:22 +02001419 c_adv(ic, fwd);
Willy Tarreauce323de2012-08-20 21:41:06 +02001420 }
1421
Willy Tarreauafc8a222014-11-28 15:46:27 +01001422 ic->flags |= CF_READ_PARTIAL;
1423 ic->total += ret;
Willy Tarreauce323de2012-08-20 21:41:06 +02001424
Christopher Faulet883d83e2021-09-09 10:17:59 +02001425 /* End-of-input reached, we can leave. In this case, it is
1426 * important to break the loop to not block the SI because of
1427 * the channel's policies.This way, we are still able to receive
1428 * shutdowns.
1429 */
Christopher Fauletb041b232022-03-24 10:27:02 +01001430 if (cs->endp->flags & CS_EP_EOI)
Christopher Faulet883d83e2021-09-09 10:17:59 +02001431 break;
1432
Willy Tarreauf26c26c2018-11-12 16:11:08 +01001433 if ((ic->flags & CF_READ_DONTWAIT) || --read_poll <= 0) {
1434 /* we're stopped by the channel's policy */
Willy Tarreaub26a6f92018-11-14 17:10:36 +01001435 si_rx_chan_blk(si);
Willy Tarreau62dd6982017-11-18 11:26:20 +01001436 break;
Willy Tarreauf26c26c2018-11-12 16:11:08 +01001437 }
Willy Tarreauce323de2012-08-20 21:41:06 +02001438
1439 /* if too many bytes were missing from last read, it means that
1440 * it's pointless trying to read again because the system does
1441 * not have them in buffers.
1442 */
1443 if (ret < max) {
Willy Tarreauce323de2012-08-20 21:41:06 +02001444 /* if a streamer has read few data, it may be because we
1445 * have exhausted system buffers. It's not worth trying
1446 * again.
1447 */
Willy Tarreauf26c26c2018-11-12 16:11:08 +01001448 if (ic->flags & CF_STREAMER) {
1449 /* we're stopped by the channel's policy */
Willy Tarreaub26a6f92018-11-14 17:10:36 +01001450 si_rx_chan_blk(si);
Willy Tarreauce323de2012-08-20 21:41:06 +02001451 break;
Willy Tarreauf26c26c2018-11-12 16:11:08 +01001452 }
Willy Tarreauce323de2012-08-20 21:41:06 +02001453
1454 /* if we read a large block smaller than what we requested,
1455 * it's almost certain we'll never get anything more.
1456 */
Willy Tarreauf26c26c2018-11-12 16:11:08 +01001457 if (ret >= global.tune.recv_enough) {
1458 /* we're stopped by the channel's policy */
Willy Tarreaub26a6f92018-11-14 17:10:36 +01001459 si_rx_chan_blk(si);
Willy Tarreauce323de2012-08-20 21:41:06 +02001460 break;
Willy Tarreauf26c26c2018-11-12 16:11:08 +01001461 }
Willy Tarreauce323de2012-08-20 21:41:06 +02001462 }
Christopher Fauletb3e0de42018-10-11 13:54:13 +02001463
1464 /* if we are waiting for more space, don't try to read more data
1465 * right now.
1466 */
Willy Tarreaub26a6f92018-11-14 17:10:36 +01001467 if (si_rx_blocked(si))
Christopher Fauletb3e0de42018-10-11 13:54:13 +02001468 break;
Willy Tarreauce323de2012-08-20 21:41:06 +02001469 } /* while !flags */
1470
Willy Tarreauffb12052018-11-15 16:06:02 +01001471 done_recv:
Willy Tarreauc5890e62014-02-09 17:47:01 +01001472 if (cur_read) {
Willy Tarreauafc8a222014-11-28 15:46:27 +01001473 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) &&
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001474 (cur_read <= ic->buf.size / 2)) {
Willy Tarreauafc8a222014-11-28 15:46:27 +01001475 ic->xfer_large = 0;
1476 ic->xfer_small++;
1477 if (ic->xfer_small >= 3) {
Willy Tarreauc5890e62014-02-09 17:47:01 +01001478 /* we have read less than half of the buffer in
1479 * one pass, and this happened at least 3 times.
1480 * This is definitely not a streamer.
1481 */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001482 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
Willy Tarreauc5890e62014-02-09 17:47:01 +01001483 }
Willy Tarreauafc8a222014-11-28 15:46:27 +01001484 else if (ic->xfer_small >= 2) {
Willy Tarreauc5890e62014-02-09 17:47:01 +01001485 /* if the buffer has been at least half full twice,
1486 * we receive faster than we send, so at least it
1487 * is not a "fast streamer".
1488 */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001489 ic->flags &= ~CF_STREAMER_FAST;
Willy Tarreauc5890e62014-02-09 17:47:01 +01001490 }
1491 }
Willy Tarreauafc8a222014-11-28 15:46:27 +01001492 else if (!(ic->flags & CF_STREAMER_FAST) &&
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001493 (cur_read >= ic->buf.size - global.tune.maxrewrite)) {
Willy Tarreauc5890e62014-02-09 17:47:01 +01001494 /* we read a full buffer at once */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001495 ic->xfer_small = 0;
1496 ic->xfer_large++;
1497 if (ic->xfer_large >= 3) {
Willy Tarreauc5890e62014-02-09 17:47:01 +01001498 /* we call this buffer a fast streamer if it manages
1499 * to be filled in one call 3 consecutive times.
1500 */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001501 ic->flags |= (CF_STREAMER | CF_STREAMER_FAST);
Willy Tarreauc5890e62014-02-09 17:47:01 +01001502 }
1503 }
1504 else {
Willy Tarreauafc8a222014-11-28 15:46:27 +01001505 ic->xfer_small = 0;
1506 ic->xfer_large = 0;
Willy Tarreauc5890e62014-02-09 17:47:01 +01001507 }
Willy Tarreauafc8a222014-11-28 15:46:27 +01001508 ic->last_read = now_ms;
Willy Tarreauc5890e62014-02-09 17:47:01 +01001509 }
1510
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001511 end_recv:
Christopher Faulete6d8cb12019-11-20 16:42:00 +01001512 ret = (cur_read != 0);
1513
Christopher Faulet36b536d2019-11-20 11:56:33 +01001514 /* Report EOI on the channel if it was reached from the mux point of
1515 * view. */
Christopher Fauletb041b232022-03-24 10:27:02 +01001516 if ((cs->endp->flags & CS_EP_EOI) && !(ic->flags & CF_EOI)) {
Christopher Faulet36b536d2019-11-20 11:56:33 +01001517 ic->flags |= (CF_EOI|CF_READ_PARTIAL);
Christopher Faulete6d8cb12019-11-20 16:42:00 +01001518 ret = 1;
1519 }
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001520
Christopher Faulet6cd56d52022-03-30 10:47:32 +02001521 if (cs->endp->flags & CS_EP_ERROR)
Christopher Faulete6d8cb12019-11-20 16:42:00 +01001522 ret = 1;
Christopher Fauletb041b232022-03-24 10:27:02 +01001523 else if (cs->endp->flags & CS_EP_EOS) {
Willy Tarreau18955db2020-01-23 16:32:24 +01001524 /* we received a shutdown */
1525 ic->flags |= CF_READ_NULL;
1526 if (ic->flags & CF_AUTO_CLOSE)
1527 channel_shutw_now(ic);
1528 stream_int_read0(si);
Christopher Faulete6d8cb12019-11-20 16:42:00 +01001529 ret = 1;
Christopher Faulet36b536d2019-11-20 11:56:33 +01001530 }
1531 else if (!si_rx_blocked(si)) {
1532 /* Subscribe to receive events if we're blocking on I/O */
Christopher Faulet2f35e7b2022-03-31 11:09:28 +02001533 conn->mux->subscribe(cs, SUB_RETRY_RECV, &si->cs->wait_event);
Willy Tarreaudd5621a2018-11-15 16:55:14 +01001534 si_rx_endp_done(si);
1535 } else {
1536 si_rx_endp_more(si);
Christopher Faulete6d8cb12019-11-20 16:42:00 +01001537 ret = 1;
Willy Tarreaudd5621a2018-11-15 16:55:14 +01001538 }
Christopher Faulete6d8cb12019-11-20 16:42:00 +01001539 return ret;
Willy Tarreauce323de2012-08-20 21:41:06 +02001540}
1541
1542/*
Willy Tarreau9bf9c142012-08-20 15:38:41 +02001543 * This function propagates a null read received on a socket-based connection.
Christopher Faulet8abe7122022-03-30 15:10:18 +02001544 * It updates the stream interface. If the stream interface has CS_FL_NOHALF,
Willy Tarreau11405122015-03-12 22:32:27 +01001545 * the close is also forwarded to the write side as an abort.
Willy Tarreau9bf9c142012-08-20 15:38:41 +02001546 */
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001547static void stream_int_read0(struct stream_interface *si)
Willy Tarreau9bf9c142012-08-20 15:38:41 +02001548{
Christopher Faulet13a35e52021-12-20 15:34:16 +01001549 struct conn_stream *cs = si->cs;
Willy Tarreauafc8a222014-11-28 15:46:27 +01001550 struct channel *ic = si_ic(si);
1551 struct channel *oc = si_oc(si);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001552
Christopher Faulet13a35e52021-12-20 15:34:16 +01001553 BUG_ON(!cs_conn(cs));
1554
Willy Tarreauabb5d422018-11-14 16:58:52 +01001555 si_rx_shut_blk(si);
Willy Tarreauafc8a222014-11-28 15:46:27 +01001556 if (ic->flags & CF_SHUTR)
Willy Tarreau9bf9c142012-08-20 15:38:41 +02001557 return;
Willy Tarreauafc8a222014-11-28 15:46:27 +01001558 ic->flags |= CF_SHUTR;
1559 ic->rex = TICK_ETERNITY;
Willy Tarreau9bf9c142012-08-20 15:38:41 +02001560
Christopher Faulet62e75742022-03-31 09:16:34 +02001561 if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
Willy Tarreau9bf9c142012-08-20 15:38:41 +02001562 return;
1563
Willy Tarreauafc8a222014-11-28 15:46:27 +01001564 if (oc->flags & CF_SHUTW)
Willy Tarreau9bf9c142012-08-20 15:38:41 +02001565 goto do_close;
1566
Christopher Faulet8abe7122022-03-30 15:10:18 +02001567 if (cs->flags & CS_FL_NOHALF) {
Willy Tarreau9bf9c142012-08-20 15:38:41 +02001568 /* we want to immediately forward this close to the write side */
Willy Tarreau87b09662015-04-03 00:22:06 +02001569 /* force flag on ssl to keep stream in cache */
Christopher Faulet69ef6c92022-03-31 14:20:00 +02001570 cs_conn_shutw(cs, CO_SHW_SILENT);
Willy Tarreau9bf9c142012-08-20 15:38:41 +02001571 goto do_close;
1572 }
1573
1574 /* otherwise that's just a normal read shutdown */
Willy Tarreau9bf9c142012-08-20 15:38:41 +02001575 return;
1576
1577 do_close:
Christopher Fauletda098e62022-03-31 17:44:45 +02001578 /* OK we completely close the socket here just as if we went through cs_shut[rw]() */
Christopher Faulet69ef6c92022-03-31 14:20:00 +02001579 cs_conn_close(cs);
Willy Tarreauf9fbfe82012-11-21 21:51:53 +01001580
Willy Tarreauafc8a222014-11-28 15:46:27 +01001581 oc->flags &= ~CF_SHUTW_NOW;
1582 oc->flags |= CF_SHUTW;
1583 oc->wex = TICK_ETERNITY;
Willy Tarreauf9fbfe82012-11-21 21:51:53 +01001584
Willy Tarreau43e69dc2018-11-06 19:23:03 +01001585 si_done_get(si);
Willy Tarreauf9fbfe82012-11-21 21:51:53 +01001586
Christopher Faulet62e75742022-03-31 09:16:34 +02001587 cs->state = CS_ST_DIS;
Christopher Fauletae024ce2022-03-29 19:02:31 +02001588 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
Willy Tarreau9bf9c142012-08-20 15:38:41 +02001589 return;
1590}
1591
Willy Tarreau651e1822015-09-23 20:06:13 +02001592/* Callback to be used by applet handlers upon completion. It updates the stream
1593 * (which may or may not take this opportunity to try to forward data), then
Emeric Brun2802b072017-06-30 14:11:56 +02001594 * may re-enable the applet's based on the channels and stream interface's final
Willy Tarreau651e1822015-09-23 20:06:13 +02001595 * states.
1596 */
Willy Tarreauaa977ba2015-09-25 11:45:06 +02001597void si_applet_wake_cb(struct stream_interface *si)
Willy Tarreaue5f86492015-04-19 15:16:35 +02001598{
Willy Tarreaueca572f2015-09-25 19:11:55 +02001599 struct channel *ic = si_ic(si);
1600
Christopher Faulet8bc17592022-02-28 17:27:09 +01001601 BUG_ON(!cs_appctx(si->cs));
Christopher Faulet693b23b2022-02-28 09:09:05 +01001602
Willy Tarreaueca572f2015-09-25 19:11:55 +02001603 /* If the applet wants to write and the channel is closed, it's a
1604 * broken pipe and it must be reported.
1605 */
Willy Tarreau05b9b642018-11-14 13:43:35 +01001606 if (!(si->flags & SI_FL_RX_WAIT_EP) && (ic->flags & CF_SHUTR))
Christopher Faulet6cd56d52022-03-30 10:47:32 +02001607 si->cs->endp->flags |= CS_EP_ERROR;
Willy Tarreaueca572f2015-09-25 19:11:55 +02001608
Willy Tarreau186dcdd2018-11-16 16:18:34 +01001609 /* automatically mark the applet having data available if it reported
1610 * begin blocked by the channel.
1611 */
1612 if (si_rx_blocked(si))
1613 si_rx_endp_more(si);
1614
Willy Tarreau651e1822015-09-23 20:06:13 +02001615 /* update the stream-int, channels, and possibly wake the stream up */
1616 stream_int_notify(si);
Willy Tarreaua64c7032019-08-01 14:17:02 +02001617 stream_release_buffers(si_strm(si));
Willy Tarreaue5f86492015-04-19 15:16:35 +02001618
Willy Tarreau32742fd2018-11-14 14:07:59 +01001619 /* stream_int_notify may have passed through chk_snd and released some
1620 * RXBLK flags. Process_stream will consider those flags to wake up the
1621 * appctx but in the case the task is not in runqueue we may have to
1622 * wakeup the appctx immediately.
Emeric Brun2802b072017-06-30 14:11:56 +02001623 */
Olivier Houchard51205a12019-04-17 19:29:35 +02001624 if ((si_rx_endp_ready(si) && !si_rx_blocked(si)) ||
1625 (si_tx_endp_ready(si) && !si_tx_blocked(si)))
Christopher Faulet693b23b2022-02-28 09:09:05 +01001626 appctx_wakeup(__cs_appctx(si->cs));
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001627}
1628
1629/*
1630 * This function performs a shutdown-read on a stream interface attached to an
1631 * applet in a connected or init state (it does nothing for other states). It
1632 * either shuts the read side or marks itself as closed. The buffer flags are
Christopher Faulet8abe7122022-03-30 15:10:18 +02001633 * updated to reflect the new state. If the stream interface has CS_FL_NOHALF,
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001634 * we also forward the close to the write side. The owner task is woken up if
1635 * it exists.
1636 */
1637static void stream_int_shutr_applet(struct stream_interface *si)
1638{
1639 struct channel *ic = si_ic(si);
1640
Christopher Faulet8bc17592022-02-28 17:27:09 +01001641 BUG_ON(!cs_appctx(si->cs));
Christopher Faulet693b23b2022-02-28 09:09:05 +01001642
Willy Tarreauabb5d422018-11-14 16:58:52 +01001643 si_rx_shut_blk(si);
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001644 if (ic->flags & CF_SHUTR)
1645 return;
1646 ic->flags |= CF_SHUTR;
1647 ic->rex = TICK_ETERNITY;
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001648
Willy Tarreau828824a2015-04-19 17:20:03 +02001649 /* Note: on shutr, we don't call the applet */
1650
Christopher Faulet62e75742022-03-31 09:16:34 +02001651 if (!cs_state_in(si->cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST))
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001652 return;
1653
1654 if (si_oc(si)->flags & CF_SHUTW) {
Willy Tarreau958f0742015-09-25 20:24:26 +02001655 si_applet_release(si);
Christopher Faulet62e75742022-03-31 09:16:34 +02001656 si->cs->state = CS_ST_DIS;
Christopher Fauletae024ce2022-03-29 19:02:31 +02001657 __cs_strm(si->cs)->conn_exp = TICK_ETERNITY;
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001658 }
Christopher Faulet8abe7122022-03-30 15:10:18 +02001659 else if (si->cs->flags & CS_FL_NOHALF) {
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001660 /* we want to immediately forward this close to the write side */
1661 return stream_int_shutw_applet(si);
1662 }
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001663}
1664
1665/*
1666 * This function performs a shutdown-write on a stream interface attached to an
1667 * applet in a connected or init state (it does nothing for other states). It
1668 * either shuts the write side or marks itself as closed. The buffer flags are
1669 * updated to reflect the new state. It does also close everything if the SI
1670 * was marked as being in error state. The owner task is woken up if it exists.
1671 */
1672static void stream_int_shutw_applet(struct stream_interface *si)
1673{
1674 struct channel *ic = si_ic(si);
1675 struct channel *oc = si_oc(si);
1676
Christopher Faulet8bc17592022-02-28 17:27:09 +01001677 BUG_ON(!cs_appctx(si->cs));
Christopher Faulet693b23b2022-02-28 09:09:05 +01001678
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001679 oc->flags &= ~CF_SHUTW_NOW;
1680 if (oc->flags & CF_SHUTW)
1681 return;
1682 oc->flags |= CF_SHUTW;
1683 oc->wex = TICK_ETERNITY;
Willy Tarreau43e69dc2018-11-06 19:23:03 +01001684 si_done_get(si);
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001685
Christopher Faulet1d987772022-03-29 18:03:35 +02001686 if (tick_isset(si->cs->hcto)) {
1687 ic->rto = si->cs->hcto;
Hongbo Longe39683c2017-03-10 18:41:51 +01001688 ic->rex = tick_add(now_ms, ic->rto);
1689 }
1690
Willy Tarreau828824a2015-04-19 17:20:03 +02001691 /* on shutw we always wake the applet up */
Christopher Faulet693b23b2022-02-28 09:09:05 +01001692 appctx_wakeup(__cs_appctx(si->cs));
Willy Tarreau828824a2015-04-19 17:20:03 +02001693
Christopher Faulet62e75742022-03-31 09:16:34 +02001694 switch (si->cs->state) {
1695 case CS_ST_RDY:
1696 case CS_ST_EST:
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001697 /* we have to shut before closing, otherwise some short messages
1698 * may never leave the system, especially when there are remaining
1699 * unread data in the socket input buffer, or when nolinger is set.
Christopher Faulet8abe7122022-03-30 15:10:18 +02001700 * However, if CS_FL_NOLINGER is explicitly set, we know there is
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001701 * no risk so we close both sides immediately.
1702 */
Christopher Faulet8abe7122022-03-30 15:10:18 +02001703 if (!(si->cs->endp->flags & CS_EP_ERROR) && !(si->cs->flags & CS_FL_NOLINGER) &&
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001704 !(ic->flags & (CF_SHUTR|CF_DONT_READ)))
1705 return;
1706
1707 /* fall through */
Christopher Faulet62e75742022-03-31 09:16:34 +02001708 case CS_ST_CON:
1709 case CS_ST_CER:
1710 case CS_ST_QUE:
1711 case CS_ST_TAR:
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001712 /* Note that none of these states may happen with applets */
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001713 si_applet_release(si);
Christopher Faulet62e75742022-03-31 09:16:34 +02001714 si->cs->state = CS_ST_DIS;
Tim Duesterhus588b3142020-05-29 14:35:51 +02001715 /* fall through */
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001716 default:
Christopher Faulet8abe7122022-03-30 15:10:18 +02001717 si->cs->flags &= ~CS_FL_NOLINGER;
Willy Tarreauabb5d422018-11-14 16:58:52 +01001718 si_rx_shut_blk(si);
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001719 ic->flags |= CF_SHUTR;
1720 ic->rex = TICK_ETERNITY;
Christopher Fauletae024ce2022-03-29 19:02:31 +02001721 __cs_strm(si->cs)->conn_exp = TICK_ETERNITY;
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001722 }
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001723}
1724
1725/* chk_rcv function for applets */
1726static void stream_int_chk_rcv_applet(struct stream_interface *si)
1727{
1728 struct channel *ic = si_ic(si);
1729
Christopher Faulet8bc17592022-02-28 17:27:09 +01001730 BUG_ON(!cs_appctx(si->cs));
Christopher Faulet693b23b2022-02-28 09:09:05 +01001731
Christopher Faulet62e75742022-03-31 09:16:34 +02001732 DPRINTF(stderr, "%s: si=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n",
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001733 __FUNCTION__,
Christopher Faulet62e75742022-03-31 09:16:34 +02001734 si, si->cs->state, ic->flags, si_oc(si)->flags);
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001735
Christopher Fauletb3e0de42018-10-11 13:54:13 +02001736 if (!ic->pipe) {
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001737 /* (re)start reading */
Christopher Faulet693b23b2022-02-28 09:09:05 +01001738 appctx_wakeup(__cs_appctx(si->cs));
Thierry FOURNIER5bc2cbf2015-09-04 18:40:36 +02001739 }
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001740}
1741
1742/* chk_snd function for applets */
1743static void stream_int_chk_snd_applet(struct stream_interface *si)
1744{
1745 struct channel *oc = si_oc(si);
1746
Christopher Faulet8bc17592022-02-28 17:27:09 +01001747 BUG_ON(!cs_appctx(si->cs));
Christopher Faulet693b23b2022-02-28 09:09:05 +01001748
Christopher Faulet62e75742022-03-31 09:16:34 +02001749 DPRINTF(stderr, "%s: si=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n",
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001750 __FUNCTION__,
Christopher Faulet62e75742022-03-31 09:16:34 +02001751 si, si->cs->state, si_ic(si)->flags, oc->flags);
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001752
Christopher Faulet62e75742022-03-31 09:16:34 +02001753 if (unlikely(si->cs->state != CS_ST_EST || (oc->flags & CF_SHUTW)))
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001754 return;
1755
Willy Tarreau828824a2015-04-19 17:20:03 +02001756 /* we only wake the applet up if it was waiting for some data */
1757
1758 if (!(si->flags & SI_FL_WAIT_DATA))
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001759 return;
1760
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001761 if (!tick_isset(oc->wex))
1762 oc->wex = tick_add_ifset(now_ms, oc->wto);
1763
Willy Tarreau828824a2015-04-19 17:20:03 +02001764 if (!channel_is_empty(oc)) {
1765 /* (re)start sending */
Christopher Faulet693b23b2022-02-28 09:09:05 +01001766 appctx_wakeup(__cs_appctx(si->cs));
Willy Tarreau828824a2015-04-19 17:20:03 +02001767 }
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001768}
1769
Willy Tarreaudded32d2008-11-30 19:48:07 +01001770/*
Willy Tarreaucff64112008-11-03 06:26:53 +01001771 * Local variables:
1772 * c-indent-level: 8
1773 * c-basic-offset: 8
1774 * End:
1775 */