blob: 2c196ce4416e9fdecd954b15dc5184781ca19091 [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 tasklet_free(si->wait_event.tasklet);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100128 pool_free(pool_head_streaminterface, si);
129}
130
Willy Tarreaucff64112008-11-03 06:26:53 +0100131/*
Willy Tarreaudded32d2008-11-30 19:48:07 +0100132 * Returns a message to the client ; the connection is shut down for read,
133 * and the request is cleared so that no server connection can be initiated.
134 * The buffer is marked for read shutdown on the other side to protect the
135 * message, and the buffer write is enabled. The message is contained in a
Willy Tarreau148d0992010-01-10 10:21:21 +0100136 * "chunk". If it is null, then an empty message is used. The reply buffer does
137 * not need to be empty before this, and its contents will not be overwritten.
138 * The primary goal of this function is to return error messages to a client.
Willy Tarreaudded32d2008-11-30 19:48:07 +0100139 */
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100140void si_retnclose(struct stream_interface *si,
Willy Tarreau83061a82018-07-13 11:56:34 +0200141 const struct buffer *msg)
Willy Tarreaudded32d2008-11-30 19:48:07 +0100142{
Willy Tarreauafc8a222014-11-28 15:46:27 +0100143 struct channel *ic = si_ic(si);
144 struct channel *oc = si_oc(si);
145
146 channel_auto_read(ic);
147 channel_abort(ic);
148 channel_auto_close(ic);
149 channel_erase(ic);
150 channel_truncate(oc);
Willy Tarreau798e1282010-12-12 13:06:00 +0100151
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200152 if (likely(msg && msg->data))
153 co_inject(oc, msg->area, msg->data);
Willy Tarreaudded32d2008-11-30 19:48:07 +0100154
Willy Tarreauafc8a222014-11-28 15:46:27 +0100155 oc->wex = tick_add_ifset(now_ms, oc->wto);
156 channel_auto_read(oc);
157 channel_auto_close(oc);
158 channel_shutr_now(oc);
Willy Tarreau5d881d02009-12-27 22:51:06 +0100159}
160
Willy Tarreau4a36b562012-08-06 19:31:45 +0200161/*
Willy Tarreaud45b9f82015-04-13 16:30:14 +0200162 * This function performs a shutdown-read on a detached stream interface in a
163 * connected or init state (it does nothing for other states). It either shuts
164 * the read side or marks itself as closed. The buffer flags are updated to
Christopher Faulet8abe7122022-03-30 15:10:18 +0200165 * reflect the new state. If the stream interface has CS_FL_NOHALF, we also
Willy Tarreaud45b9f82015-04-13 16:30:14 +0200166 * forward the close to the write side. The owner task is woken up if it exists.
Willy Tarreau4a36b562012-08-06 19:31:45 +0200167 */
Willy Tarreau6fe15412013-09-29 15:16:03 +0200168static void stream_int_shutr(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200169{
Willy Tarreauafc8a222014-11-28 15:46:27 +0100170 struct channel *ic = si_ic(si);
171
Willy Tarreauabb5d422018-11-14 16:58:52 +0100172 si_rx_shut_blk(si);
Willy Tarreauafc8a222014-11-28 15:46:27 +0100173 if (ic->flags & CF_SHUTR)
Willy Tarreau6fe15412013-09-29 15:16:03 +0200174 return;
Willy Tarreauafc8a222014-11-28 15:46:27 +0100175 ic->flags |= CF_SHUTR;
176 ic->rex = TICK_ETERNITY;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200177
Willy Tarreau4f283fa2019-06-05 14:34:03 +0200178 if (!si_state_in(si->state, SI_SB_CON|SI_SB_RDY|SI_SB_EST))
Willy Tarreau6fe15412013-09-29 15:16:03 +0200179 return;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200180
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100181 if (si_oc(si)->flags & CF_SHUTW) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200182 si->state = SI_ST_DIS;
Christopher Fauletae024ce2022-03-29 19:02:31 +0200183 __cs_strm(si->cs)->conn_exp = TICK_ETERNITY;
Willy Tarreaud8ccffe2010-09-07 16:16:50 +0200184 }
Christopher Faulet8abe7122022-03-30 15:10:18 +0200185 else if (si->cs->flags & CS_FL_NOHALF) {
Willy Tarreau4a36b562012-08-06 19:31:45 +0200186 /* we want to immediately forward this close to the write side */
187 return stream_int_shutw(si);
188 }
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200189
Willy Tarreau4a36b562012-08-06 19:31:45 +0200190 /* note that if the task exists, it must unregister itself once it runs */
Willy Tarreau07373b82014-11-28 12:08:47 +0100191 if (!(si->flags & SI_FL_DONT_WAKE))
192 task_wakeup(si_task(si), TASK_WOKEN_IO);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200193}
194
Willy Tarreau4a36b562012-08-06 19:31:45 +0200195/*
Willy Tarreaud45b9f82015-04-13 16:30:14 +0200196 * This function performs a shutdown-write on a detached stream interface in a
197 * connected or init state (it does nothing for other states). It either shuts
198 * the write side or marks itself as closed. The buffer flags are updated to
199 * reflect the new state. It does also close everything if the SI was marked as
200 * being in error state. The owner task is woken up if it exists.
Willy Tarreau4a36b562012-08-06 19:31:45 +0200201 */
Willy Tarreau6fe15412013-09-29 15:16:03 +0200202static void stream_int_shutw(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200203{
Willy Tarreauafc8a222014-11-28 15:46:27 +0100204 struct channel *ic = si_ic(si);
205 struct channel *oc = si_oc(si);
206
207 oc->flags &= ~CF_SHUTW_NOW;
208 if (oc->flags & CF_SHUTW)
Willy Tarreau6fe15412013-09-29 15:16:03 +0200209 return;
Willy Tarreauafc8a222014-11-28 15:46:27 +0100210 oc->flags |= CF_SHUTW;
211 oc->wex = TICK_ETERNITY;
Willy Tarreau43e69dc2018-11-06 19:23:03 +0100212 si_done_get(si);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200213
Christopher Faulet1d987772022-03-29 18:03:35 +0200214 if (tick_isset(si->cs->hcto)) {
215 ic->rto = si->cs->hcto;
Hongbo Longe39683c2017-03-10 18:41:51 +0100216 ic->rex = tick_add(now_ms, ic->rto);
217 }
218
Willy Tarreaufb90d942009-09-05 20:57:35 +0200219 switch (si->state) {
Willy Tarreau4f283fa2019-06-05 14:34:03 +0200220 case SI_ST_RDY:
Willy Tarreaufb90d942009-09-05 20:57:35 +0200221 case SI_ST_EST:
Willy Tarreau4a36b562012-08-06 19:31:45 +0200222 /* we have to shut before closing, otherwise some short messages
223 * may never leave the system, especially when there are remaining
224 * unread data in the socket input buffer, or when nolinger is set.
Christopher Faulet8abe7122022-03-30 15:10:18 +0200225 * However, if CS_FL_NOLINGER is explicitly set, we know there is
Willy Tarreau4a36b562012-08-06 19:31:45 +0200226 * no risk so we close both sides immediately.
227 */
Christopher Faulet8abe7122022-03-30 15:10:18 +0200228 if (!(si->cs->endp->flags & CS_EP_ERROR) && !(si->cs->flags & CS_FL_NOLINGER) &&
Willy Tarreauafc8a222014-11-28 15:46:27 +0100229 !(ic->flags & (CF_SHUTR|CF_DONT_READ)))
Willy Tarreau6fe15412013-09-29 15:16:03 +0200230 return;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200231
232 /* fall through */
233 case SI_ST_CON:
234 case SI_ST_CER:
Willy Tarreau32d3ee92010-12-29 14:03:02 +0100235 case SI_ST_QUE:
236 case SI_ST_TAR:
Willy Tarreau8b3d7df2013-09-29 14:51:58 +0200237 /* Note that none of these states may happen with applets */
Willy Tarreaufb90d942009-09-05 20:57:35 +0200238 si->state = SI_ST_DIS;
Tim Duesterhus588b3142020-05-29 14:35:51 +0200239 /* fall through */
Willy Tarreaufb90d942009-09-05 20:57:35 +0200240 default:
Christopher Faulet8abe7122022-03-30 15:10:18 +0200241 si->cs->flags &= ~CS_FL_NOLINGER;
Willy Tarreauabb5d422018-11-14 16:58:52 +0100242 si_rx_shut_blk(si);
Willy Tarreauafc8a222014-11-28 15:46:27 +0100243 ic->flags |= CF_SHUTR;
244 ic->rex = TICK_ETERNITY;
Christopher Fauletae024ce2022-03-29 19:02:31 +0200245 __cs_strm(si->cs)->conn_exp = TICK_ETERNITY;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200246 }
247
Willy Tarreau4a36b562012-08-06 19:31:45 +0200248 /* note that if the task exists, it must unregister itself once it runs */
Willy Tarreau07373b82014-11-28 12:08:47 +0100249 if (!(si->flags & SI_FL_DONT_WAKE))
250 task_wakeup(si_task(si), TASK_WOKEN_IO);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200251}
252
253/* default chk_rcv function for scheduled tasks */
Willy Tarreauf873d752012-05-11 17:47:17 +0200254static void stream_int_chk_rcv(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200255{
Willy Tarreauafc8a222014-11-28 15:46:27 +0100256 struct channel *ic = si_ic(si);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200257
Willy Tarreauafc8a222014-11-28 15:46:27 +0100258 DPRINTF(stderr, "%s: si=%p, si->state=%d ic->flags=%08x oc->flags=%08x\n",
Willy Tarreaufb90d942009-09-05 20:57:35 +0200259 __FUNCTION__,
Willy Tarreauafc8a222014-11-28 15:46:27 +0100260 si, si->state, ic->flags, si_oc(si)->flags);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200261
Christopher Fauletb3e0de42018-10-11 13:54:13 +0200262 if (ic->pipe) {
Willy Tarreaufb90d942009-09-05 20:57:35 +0200263 /* stop reading */
Willy Tarreaudb398432018-11-15 11:08:52 +0100264 si_rx_room_blk(si);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200265 }
266 else {
267 /* (re)start reading */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200268 tasklet_wakeup(si->wait_event.tasklet);
Willy Tarreau07373b82014-11-28 12:08:47 +0100269 if (!(si->flags & SI_FL_DONT_WAKE))
270 task_wakeup(si_task(si), TASK_WOKEN_IO);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200271 }
272}
273
274/* default chk_snd function for scheduled tasks */
Willy Tarreauf873d752012-05-11 17:47:17 +0200275static void stream_int_chk_snd(struct stream_interface *si)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200276{
Willy Tarreauafc8a222014-11-28 15:46:27 +0100277 struct channel *oc = si_oc(si);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200278
Willy Tarreauafc8a222014-11-28 15:46:27 +0100279 DPRINTF(stderr, "%s: si=%p, si->state=%d ic->flags=%08x oc->flags=%08x\n",
Willy Tarreaufb90d942009-09-05 20:57:35 +0200280 __FUNCTION__,
Willy Tarreauafc8a222014-11-28 15:46:27 +0100281 si, si->state, si_ic(si)->flags, oc->flags);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200282
Willy Tarreauafc8a222014-11-28 15:46:27 +0100283 if (unlikely(si->state != SI_ST_EST || (oc->flags & CF_SHUTW)))
Willy Tarreaufb90d942009-09-05 20:57:35 +0200284 return;
285
286 if (!(si->flags & SI_FL_WAIT_DATA) || /* not waiting for data */
Willy Tarreauafc8a222014-11-28 15:46:27 +0100287 channel_is_empty(oc)) /* called with nothing to send ! */
Willy Tarreaufb90d942009-09-05 20:57:35 +0200288 return;
289
290 /* Otherwise there are remaining data to be sent in the buffer,
291 * so we tell the handler.
292 */
293 si->flags &= ~SI_FL_WAIT_DATA;
Willy Tarreauafc8a222014-11-28 15:46:27 +0100294 if (!tick_isset(oc->wex))
295 oc->wex = tick_add_ifset(now_ms, oc->wto);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200296
Willy Tarreau07373b82014-11-28 12:08:47 +0100297 if (!(si->flags & SI_FL_DONT_WAKE))
298 task_wakeup(si_task(si), TASK_WOKEN_IO);
Willy Tarreaufb90d942009-09-05 20:57:35 +0200299}
300
Willy Tarreaua9ff5e62015-07-19 18:46:30 +0200301/* Register an applet to handle a stream_interface as a new appctx. The SI will
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -0700302 * wake it up every time it is solicited. The appctx must be deleted by the task
Willy Tarreaua9ff5e62015-07-19 18:46:30 +0200303 * handler using si_release_endpoint(), possibly from within the function itself.
304 * It also pre-initializes the applet's context and returns it (or NULL in case
305 * it could not be allocated).
Willy Tarreaufb90d942009-09-05 20:57:35 +0200306 */
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100307struct appctx *si_register_handler(struct stream_interface *si, struct applet *app)
Willy Tarreaufb90d942009-09-05 20:57:35 +0200308{
Willy Tarreau0a23bcb2013-12-01 11:31:38 +0100309 struct appctx *appctx;
310
Willy Tarreau07373b82014-11-28 12:08:47 +0100311 DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", app, si, si_task(si));
Willy Tarreaufb90d942009-09-05 20:57:35 +0200312
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100313 appctx = appctx_new(app, si->cs->endp);
Willy Tarreaua69fc9f2014-12-22 19:34:00 +0100314 if (!appctx)
Willy Tarreau0a23bcb2013-12-01 11:31:38 +0100315 return NULL;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100316 cs_attach_applet(si->cs, appctx, appctx);
317 appctx->owner = si->cs;
Christopher Faulet13a35e52021-12-20 15:34:16 +0100318 appctx->t->nice = si_strm(si)->task->nice;
Willy Tarreau0cd3bd62018-11-06 18:46:37 +0100319 si_cant_get(si);
Willy Tarreau828824a2015-04-19 17:20:03 +0200320 appctx_wakeup(appctx);
Christopher Faulet13a35e52021-12-20 15:34:16 +0100321 return appctx;
Willy Tarreaufb90d942009-09-05 20:57:35 +0200322}
323
Willy Tarreau2c6be842012-07-06 17:12:34 +0200324/* This callback is used to send a valid PROXY protocol line to a socket being
Willy Tarreauafad0e02012-08-09 14:45:22 +0200325 * established. It returns 0 if it fails in a fatal way or needs to poll to go
326 * further, otherwise it returns non-zero and removes itself from the connection's
Willy Tarreaua1a74742012-08-24 12:14:49 +0200327 * flags (the bit is provided in <flag> by the caller). It is designed to be
328 * called by the connection handler and relies on it to commit polling changes.
Willy Tarreau57cd3e42013-10-24 22:01:26 +0200329 * Note that it can emit a PROXY line by relying on the other end's address
330 * when the connection is attached to a stream interface, or by resolving the
331 * local address otherwise (also called a LOCAL line).
Willy Tarreau2c6be842012-07-06 17:12:34 +0200332 */
333int conn_si_send_proxy(struct connection *conn, unsigned int flag)
334{
Willy Tarreaud02cdd22013-12-15 10:23:20 +0100335 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200336 goto out_error;
337
Willy Tarreau2c6be842012-07-06 17:12:34 +0200338 /* If we have a PROXY line to send, we'll use this to validate the
339 * connection, in which case the connection is validated only once
340 * we've sent the whole proxy line. Otherwise we use connect().
341 */
Tim Duesterhus36839dc2019-02-26 17:09:51 +0100342 if (conn->send_proxy_ofs) {
Willy Tarreau6b1379f2018-11-18 21:38:19 +0100343 const struct conn_stream *cs;
Willy Tarreau2c6be842012-07-06 17:12:34 +0200344 int ret;
345
Christopher Fauletd82056c2020-05-26 16:08:49 +0200346 /* If there is no mux attached to the connection, it means the
347 * connection context is a conn-stream.
348 */
349 cs = (conn->mux ? cs_get_first(conn) : conn->ctx);
350
Willy Tarreau2c6be842012-07-06 17:12:34 +0200351 /* The target server expects a PROXY line to be sent first.
352 * If the send_proxy_ofs is negative, it corresponds to the
353 * offset to start sending from then end of the proxy string
354 * (which is recomputed every time since it's constant). If
355 * it is positive, it means we have to send from the start.
Willy Tarreau57cd3e42013-10-24 22:01:26 +0200356 * We can only send a "normal" PROXY line when the connection
357 * is attached to a stream interface. Otherwise we can only
358 * send a LOCAL line (eg: for use with health checks).
Willy Tarreau2c6be842012-07-06 17:12:34 +0200359 */
Willy Tarreau6b1379f2018-11-18 21:38:19 +0100360
361 if (cs && cs->data_cb == &si_conn_cb) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200362 ret = make_proxy_line(trash.area, trash.size,
363 objt_server(conn->target),
Christopher Fauletf835dea2021-12-21 14:35:17 +0100364 cs_conn(si_opposite(cs_si(cs))->cs),
Christopher Faulet693b23b2022-02-28 09:09:05 +0100365 cs_strm(cs));
Willy Tarreau57cd3e42013-10-24 22:01:26 +0200366 }
367 else {
368 /* The target server expects a LOCAL line to be sent first. Retrieving
369 * local or remote addresses may fail until the connection is established.
370 */
Willy Tarreau7bb447c2019-07-17 11:40:51 +0200371 if (!conn_get_src(conn) || !conn_get_dst(conn))
Willy Tarreau57cd3e42013-10-24 22:01:26 +0200372 goto out_wait;
373
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200374 ret = make_proxy_line(trash.area, trash.size,
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +0100375 objt_server(conn->target), conn,
376 NULL);
Willy Tarreau57cd3e42013-10-24 22:01:26 +0200377 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200378
Willy Tarreau2c6be842012-07-06 17:12:34 +0200379 if (!ret)
380 goto out_error;
381
Willy Tarreaub8020ce2013-10-24 21:10:08 +0200382 if (conn->send_proxy_ofs > 0)
383 conn->send_proxy_ofs = -ret; /* first call */
Willy Tarreau2c6be842012-07-06 17:12:34 +0200384
Willy Tarreaua1a74742012-08-24 12:14:49 +0200385 /* we have to send trash from (ret+sp for -sp bytes). If the
386 * data layer has a pending write, we'll also set MSG_MORE.
387 */
Willy Tarreau827fee72020-12-11 15:26:55 +0100388 ret = conn_ctrl_send(conn,
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200389 trash.area + ret + conn->send_proxy_ofs,
390 -conn->send_proxy_ofs,
Willy Tarreau827fee72020-12-11 15:26:55 +0100391 (conn->subs && conn->subs->events & SUB_RETRY_SEND) ? CO_SFL_MSG_MORE : 0);
Willy Tarreau2c6be842012-07-06 17:12:34 +0200392
Willy Tarreau0a03c0f2015-03-13 00:05:28 +0100393 if (ret < 0)
Willy Tarreau2c6be842012-07-06 17:12:34 +0200394 goto out_error;
Willy Tarreau2c6be842012-07-06 17:12:34 +0200395
Willy Tarreaub8020ce2013-10-24 21:10:08 +0200396 conn->send_proxy_ofs += ret; /* becomes zero once complete */
397 if (conn->send_proxy_ofs != 0)
Willy Tarreau2c6be842012-07-06 17:12:34 +0200398 goto out_wait;
399
400 /* OK we've sent the whole line, we're connected */
401 }
402
Willy Tarreaua1a74742012-08-24 12:14:49 +0200403 /* The connection is ready now, simply return and let the connection
404 * handler notify upper layers if needed.
Willy Tarreau2c6be842012-07-06 17:12:34 +0200405 */
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100406 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau2c6be842012-07-06 17:12:34 +0200407 conn->flags &= ~flag;
Willy Tarreauafad0e02012-08-09 14:45:22 +0200408 return 1;
Willy Tarreau2c6be842012-07-06 17:12:34 +0200409
410 out_error:
Willy Tarreauafad0e02012-08-09 14:45:22 +0200411 /* Write error on the file descriptor */
Willy Tarreau2c6be842012-07-06 17:12:34 +0200412 conn->flags |= CO_FL_ERROR;
Willy Tarreauafad0e02012-08-09 14:45:22 +0200413 return 0;
Willy Tarreau2c6be842012-07-06 17:12:34 +0200414
415 out_wait:
Willy Tarreauafad0e02012-08-09 14:45:22 +0200416 return 0;
Willy Tarreau2c6be842012-07-06 17:12:34 +0200417}
418
Willy Tarreau27375622013-12-17 00:00:28 +0100419
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100420/* This function is the equivalent to si_update() except that it's
Willy Tarreau615f28b2015-09-23 18:40:09 +0200421 * designed to be called from outside the stream handlers, typically the lower
422 * layers (applets, connections) after I/O completion. After updating the stream
423 * interface and timeouts, it will try to forward what can be forwarded, then to
424 * wake the associated task up if an important event requires special handling.
Willy Tarreaud0f5bbc2018-11-14 11:10:26 +0100425 * It may update SI_FL_WAIT_DATA and/or SI_FL_RXBLK_ROOM, that the callers are
Willy Tarreau0dfccb22018-10-25 13:55:20 +0200426 * encouraged to watch to take appropriate action.
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100427 * It should not be called from within the stream itself, si_update()
Willy Tarreau615f28b2015-09-23 18:40:09 +0200428 * is designed for this.
429 */
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100430static void stream_int_notify(struct stream_interface *si)
Willy Tarreau615f28b2015-09-23 18:40:09 +0200431{
432 struct channel *ic = si_ic(si);
433 struct channel *oc = si_oc(si);
Willy Tarreau47baeb82018-11-15 07:46:57 +0100434 struct stream_interface *sio = si_opposite(si);
Christopher Fauletd7607de2019-01-03 16:24:54 +0100435 struct task *task = si_task(si);
Willy Tarreau615f28b2015-09-23 18:40:09 +0200436
437 /* process consumer side */
438 if (channel_is_empty(oc)) {
Christopher Faulet13a35e52021-12-20 15:34:16 +0100439 struct connection *conn = cs_conn(si->cs);
Olivier Houcharde9bed532017-11-16 17:49:25 +0100440
Willy Tarreau615f28b2015-09-23 18:40:09 +0200441 if (((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW) &&
Willy Tarreau911db9b2020-01-23 16:27:54 +0100442 (si->state == SI_ST_EST) && (!conn || !(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS))))
Willy Tarreau615f28b2015-09-23 18:40:09 +0200443 si_shutw(si);
444 oc->wex = TICK_ETERNITY;
445 }
446
Willy Tarreau8cf9c8e2016-12-13 15:21:25 +0100447 /* indicate that we may be waiting for data from the output channel or
448 * we're about to close and can't expect more data if SHUTW_NOW is there.
449 */
Christopher Fauletb3e0de42018-10-11 13:54:13 +0200450 if (!(oc->flags & (CF_SHUTW|CF_SHUTW_NOW)))
Willy Tarreau615f28b2015-09-23 18:40:09 +0200451 si->flags |= SI_FL_WAIT_DATA;
Willy Tarreau8cf9c8e2016-12-13 15:21:25 +0100452 else if ((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW)
453 si->flags &= ~SI_FL_WAIT_DATA;
Willy Tarreau615f28b2015-09-23 18:40:09 +0200454
455 /* update OC timeouts and wake the other side up if it's waiting for room */
456 if (oc->flags & CF_WRITE_ACTIVITY) {
457 if ((oc->flags & (CF_SHUTW|CF_WRITE_PARTIAL)) == CF_WRITE_PARTIAL &&
458 !channel_is_empty(oc))
459 if (tick_isset(oc->wex))
460 oc->wex = tick_add_ifset(now_ms, oc->wto);
461
462 if (!(si->flags & SI_FL_INDEP_STR))
463 if (tick_isset(ic->rex))
464 ic->rex = tick_add_ifset(now_ms, ic->rto);
Willy Tarreauf26c26c2018-11-12 16:11:08 +0100465 }
Willy Tarreau615f28b2015-09-23 18:40:09 +0200466
Willy Tarreaub26a6f92018-11-14 17:10:36 +0100467 if (oc->flags & CF_DONT_READ)
468 si_rx_chan_blk(sio);
469 else
470 si_rx_chan_rdy(sio);
Willy Tarreau615f28b2015-09-23 18:40:09 +0200471
472 /* Notify the other side when we've injected data into the IC that
473 * needs to be forwarded. We can do fast-forwarding as soon as there
474 * are output data, but we avoid doing this if some of the data are
475 * not yet scheduled for being forwarded, because it is very likely
476 * that it will be done again immediately afterwards once the following
Willy Tarreaud0f5bbc2018-11-14 11:10:26 +0100477 * data are parsed (eg: HTTP chunking). We only SI_FL_RXBLK_ROOM once
Willy Tarreau615f28b2015-09-23 18:40:09 +0200478 * we've emptied *some* of the output buffer, and not just when there
479 * is available room, because applets are often forced to stop before
480 * the buffer is full. We must not stop based on input data alone because
481 * an HTTP parser might need more data to complete the parsing.
482 */
483 if (!channel_is_empty(ic) &&
Willy Tarreau47baeb82018-11-15 07:46:57 +0100484 (sio->flags & SI_FL_WAIT_DATA) &&
Willy Tarreau89b6a2b2018-11-18 15:46:10 +0100485 (!(ic->flags & CF_EXPECT_MORE) || c_full(ic) || ci_data(ic) == 0 || ic->pipe)) {
Willy Tarreau615f28b2015-09-23 18:40:09 +0200486 int new_len, last_len;
487
Willy Tarreau77e478c2018-06-19 07:03:14 +0200488 last_len = co_data(ic);
Willy Tarreau615f28b2015-09-23 18:40:09 +0200489 if (ic->pipe)
490 last_len += ic->pipe->data;
491
Willy Tarreau47baeb82018-11-15 07:46:57 +0100492 si_chk_snd(sio);
Willy Tarreau615f28b2015-09-23 18:40:09 +0200493
Willy Tarreau77e478c2018-06-19 07:03:14 +0200494 new_len = co_data(ic);
Willy Tarreau615f28b2015-09-23 18:40:09 +0200495 if (ic->pipe)
496 new_len += ic->pipe->data;
497
498 /* check if the consumer has freed some space either in the
499 * buffer or in the pipe.
500 */
Willy Tarreau47baeb82018-11-15 07:46:57 +0100501 if (new_len < last_len)
Willy Tarreaudb398432018-11-15 11:08:52 +0100502 si_rx_room_rdy(si);
Willy Tarreau615f28b2015-09-23 18:40:09 +0200503 }
504
Willy Tarreaub26a6f92018-11-14 17:10:36 +0100505 if (!(ic->flags & CF_DONT_READ))
506 si_rx_chan_rdy(si);
507
Willy Tarreau47baeb82018-11-15 07:46:57 +0100508 si_chk_rcv(si);
509 si_chk_rcv(sio);
510
Willy Tarreaub26a6f92018-11-14 17:10:36 +0100511 if (si_rx_blocked(si)) {
Willy Tarreau615f28b2015-09-23 18:40:09 +0200512 ic->rex = TICK_ETERNITY;
513 }
Willy Tarreaub26a6f92018-11-14 17:10:36 +0100514 else if ((ic->flags & (CF_SHUTR|CF_READ_PARTIAL)) == CF_READ_PARTIAL) {
Willy Tarreau615f28b2015-09-23 18:40:09 +0200515 /* we must re-enable reading if si_chk_snd() has freed some space */
516 if (!(ic->flags & CF_READ_NOEXP) && tick_isset(ic->rex))
517 ic->rex = tick_add_ifset(now_ms, ic->rto);
518 }
519
520 /* wake the task up only when needed */
521 if (/* changes on the production side */
522 (ic->flags & (CF_READ_NULL|CF_READ_ERROR)) ||
Willy Tarreau4f283fa2019-06-05 14:34:03 +0200523 !si_state_in(si->state, SI_SB_CON|SI_SB_RDY|SI_SB_EST) ||
Christopher Faulet6cd56d52022-03-30 10:47:32 +0200524 (si->cs->endp->flags & CS_EP_ERROR) ||
Willy Tarreau615f28b2015-09-23 18:40:09 +0200525 ((ic->flags & CF_READ_PARTIAL) &&
Christopher Faulet297d3e22019-03-22 14:16:14 +0100526 ((ic->flags & CF_EOI) || !ic->to_forward || sio->state != SI_ST_EST)) ||
Willy Tarreau615f28b2015-09-23 18:40:09 +0200527
528 /* changes on the consumption side */
529 (oc->flags & (CF_WRITE_NULL|CF_WRITE_ERROR)) ||
Willy Tarreauede3d882018-10-24 17:17:56 +0200530 ((oc->flags & CF_WRITE_ACTIVITY) &&
Willy Tarreau615f28b2015-09-23 18:40:09 +0200531 ((oc->flags & CF_SHUTW) ||
Willy Tarreau78f5ff82018-12-19 11:00:00 +0100532 (((oc->flags & CF_WAKE_WRITE) ||
533 !(oc->flags & (CF_AUTO_CLOSE|CF_SHUTW_NOW|CF_SHUTW))) &&
Willy Tarreau47baeb82018-11-15 07:46:57 +0100534 (sio->state != SI_ST_EST ||
Willy Tarreau615f28b2015-09-23 18:40:09 +0200535 (channel_is_empty(oc) && !oc->to_forward)))))) {
Christopher Fauletd7607de2019-01-03 16:24:54 +0100536 task_wakeup(task, TASK_WOKEN_IO);
537 }
538 else {
539 /* Update expiration date for the task and requeue it */
540 task->expire = tick_first((tick_is_expired(task->expire, now_ms) ? 0 : task->expire),
541 tick_first(tick_first(ic->rex, ic->wex),
542 tick_first(oc->rex, oc->wex)));
Willy Tarreau45bcb372019-08-01 18:51:38 +0200543
544 task->expire = tick_first(task->expire, ic->analyse_exp);
545 task->expire = tick_first(task->expire, oc->analyse_exp);
Christopher Fauletae024ce2022-03-29 19:02:31 +0200546 task->expire = tick_first(task->expire, __cs_strm(si->cs)->conn_exp);
Willy Tarreau45bcb372019-08-01 18:51:38 +0200547
Christopher Fauletd7607de2019-01-03 16:24:54 +0100548 task_queue(task);
Willy Tarreau615f28b2015-09-23 18:40:09 +0200549 }
550 if (ic->flags & CF_READ_ACTIVITY)
551 ic->flags &= ~CF_READ_DONTWAIT;
Willy Tarreau615f28b2015-09-23 18:40:09 +0200552}
553
Willy Tarreaud1480cc2022-03-17 16:19:09 +0100554/* The stream interface is only responsible for the connection during the early
555 * states, before plugging a mux. Thus it should only care about CO_FL_ERROR
556 * before SI_ST_EST, and after that it must absolutely ignore it since the mux
557 * may hold pending data. This function returns true if such an error was
558 * reported. Both the CS and the CONN must be valid.
559 */
560static inline int si_is_conn_error(const struct stream_interface *si)
561{
562 struct connection *conn;
563
564 if (si->state >= SI_ST_EST)
565 return 0;
566
567 conn = __cs_conn(si->cs);
568 BUG_ON(!conn);
569 return !!(conn->flags & CO_FL_ERROR);
570}
Willy Tarreau615f28b2015-09-23 18:40:09 +0200571
Olivier Houchardc2aa7112018-09-11 18:27:21 +0200572/* Called by I/O handlers after completion.. It propagates
Willy Tarreau651e1822015-09-23 20:06:13 +0200573 * connection flags to the stream interface, updates the stream (which may or
574 * may not take this opportunity to try to forward data), then update the
575 * connection's polling based on the channels and stream interface's final
576 * states. The function always returns 0.
Willy Tarreau100c4672012-08-20 12:06:26 +0200577 */
Olivier Houchardc2aa7112018-09-11 18:27:21 +0200578static int si_cs_process(struct conn_stream *cs)
Willy Tarreaufd31e532012-07-23 18:24:25 +0200579{
Christopher Faulet693b23b2022-02-28 09:09:05 +0100580 struct connection *conn = __cs_conn(cs);
Christopher Fauletf835dea2021-12-21 14:35:17 +0100581 struct stream_interface *si = cs_si(cs);
Willy Tarreauafc8a222014-11-28 15:46:27 +0100582 struct channel *ic = si_ic(si);
583 struct channel *oc = si_oc(si);
Willy Tarreaufd31e532012-07-23 18:24:25 +0200584
Christopher Faulet897d6122021-12-17 17:28:35 +0100585 BUG_ON(!conn);
586
Olivier Houchardc7ffa912018-08-28 19:37:41 +0200587 /* If we have data to send, try it now */
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100588 if (!channel_is_empty(oc) && !(si->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau908d26f2018-10-25 14:02:47 +0200589 si_cs_send(cs);
590
Christopher Fauletaf642df2022-03-30 10:06:11 +0200591 /* First step, report to the conn-stream what was detected at the
Willy Tarreau651e1822015-09-23 20:06:13 +0200592 * connection layer : errors and connection establishment.
Christopher Faulet6cd56d52022-03-30 10:47:32 +0200593 * Only add CS_EP_ERROR if we're connected, or we're attempting to
Olivier Houchardc31e2cb2019-06-24 16:08:08 +0200594 * connect, we may get there because we got woken up, but only run
595 * after process_stream() noticed there were an error, and decided
596 * to retry to connect, the connection may still have CO_FL_ERROR,
Christopher Faulet6cd56d52022-03-30 10:47:32 +0200597 * and we don't want to add CS_EP_ERROR back
Christopher Faulet36b536d2019-11-20 11:56:33 +0100598 *
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.
Willy Tarreau651e1822015-09-23 20:06:13 +0200602 */
Willy Tarreaud1480cc2022-03-17 16:19:09 +0100603
604 if (si->state >= SI_ST_CON) {
Christopher Faulet6cd56d52022-03-30 10:47:32 +0200605 if (si_is_conn_error(si))
606 cs->endp->flags |= CS_EP_ERROR;
Willy Tarreaud1480cc2022-03-17 16:19:09 +0100607 }
Willy Tarreau3c55ec22012-07-23 19:19:51 +0200608
Olivier Houchardccaa7de2017-10-02 11:51:03 +0200609 /* If we had early data, and the handshake ended, then
610 * we can remove the flag, and attempt to wake the task up,
611 * in the event there's an analyser waiting for the end of
612 * the handshake.
613 */
Willy Tarreau911db9b2020-01-23 16:27:54 +0100614 if (!(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS)) &&
Christopher Faulete9e48202022-03-22 18:13:29 +0100615 (cs->endp->flags & CS_EP_WAIT_FOR_HS)) {
616 cs->endp->flags &= ~CS_EP_WAIT_FOR_HS;
Olivier Houchardccaa7de2017-10-02 11:51:03 +0200617 task_wakeup(si_task(si), TASK_WOKEN_MSG);
618 }
619
Willy Tarreau7ab22adb2019-06-05 14:53:22 +0200620 if (!si_state_in(si->state, SI_SB_EST|SI_SB_DIS|SI_SB_CLO) &&
Willy Tarreau911db9b2020-01-23 16:27:54 +0100621 (conn->flags & CO_FL_WAIT_XPRT) == 0) {
Christopher Fauletae024ce2022-03-29 19:02:31 +0200622 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
Willy Tarreauafc8a222014-11-28 15:46:27 +0100623 oc->flags |= CF_WRITE_NULL;
Willy Tarreaub27f54a2019-06-05 16:43:44 +0200624 if (si->state == SI_ST_CON)
625 si->state = SI_ST_RDY;
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200626 }
627
Christopher Faulet89e34c22021-01-21 16:22:01 +0100628 /* Report EOS on the channel if it was reached from the mux point of
629 * view.
630 *
631 * Note: This test is only required because si_cs_process is also the SI
632 * wake callback. Otherwise si_cs_recv()/si_cs_send() already take
633 * care of it.
634 */
Christopher Fauletb041b232022-03-24 10:27:02 +0100635 if (cs->endp->flags & CS_EP_EOS && !(ic->flags & CF_SHUTR)) {
Christopher Faulet89e34c22021-01-21 16:22:01 +0100636 /* we received a shutdown */
637 ic->flags |= CF_READ_NULL;
638 if (ic->flags & CF_AUTO_CLOSE)
639 channel_shutw_now(ic);
640 stream_int_read0(si);
641 }
642
Christopher Faulet297d3e22019-03-22 14:16:14 +0100643 /* Report EOI on the channel if it was reached from the mux point of
Christopher Faulet36b536d2019-11-20 11:56:33 +0100644 * view.
645 *
646 * Note: This test is only required because si_cs_process is also the SI
647 * wake callback. Otherwise si_cs_recv()/si_cs_send() already take
648 * care of it.
649 */
Christopher Fauletb041b232022-03-24 10:27:02 +0100650 if ((cs->endp->flags & CS_EP_EOI) && !(ic->flags & CF_EOI))
Christopher Faulet8e9e3ef2019-05-17 09:14:10 +0200651 ic->flags |= (CF_EOI|CF_READ_PARTIAL);
Christopher Faulet203b2b02019-03-08 09:23:46 +0100652
Willy Tarreau651e1822015-09-23 20:06:13 +0200653 /* Second step : update the stream-int and channels, try to forward any
654 * pending data, then possibly wake the stream up based on the new
655 * stream-int status.
Willy Tarreau44b5dc62012-08-24 12:12:53 +0200656 */
Willy Tarreau651e1822015-09-23 20:06:13 +0200657 stream_int_notify(si);
Willy Tarreaua64c7032019-08-01 14:17:02 +0200658 stream_release_buffers(si_strm(si));
Willy Tarreau2396c1c2012-10-03 21:12:16 +0200659 return 0;
Willy Tarreaufd31e532012-07-23 18:24:25 +0200660}
Willy Tarreau2c6be842012-07-06 17:12:34 +0200661
Willy Tarreau5368d802012-08-21 18:22:06 +0200662/*
663 * This function is called to send buffer data to a stream socket.
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200664 * It calls the mux layer's snd_buf function. It relies on the
Godbach4f489902013-12-04 17:24:06 +0800665 * caller to commit polling changes. The caller should check conn->flags
666 * for errors.
Willy Tarreau5368d802012-08-21 18:22:06 +0200667 */
Christopher Faulet49416232022-02-28 09:14:46 +0100668static int si_cs_send(struct conn_stream *cs)
Willy Tarreau5368d802012-08-21 18:22:06 +0200669{
Christopher Faulet693b23b2022-02-28 09:09:05 +0100670 struct connection *conn = __cs_conn(cs);
Christopher Fauletf835dea2021-12-21 14:35:17 +0100671 struct stream_interface *si = cs_si(cs);
Christopher Faulete05bf9e2022-03-29 15:23:40 +0200672 struct stream *s = si_strm(si);
Willy Tarreauafc8a222014-11-28 15:46:27 +0100673 struct channel *oc = si_oc(si);
Willy Tarreau5368d802012-08-21 18:22:06 +0200674 int ret;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200675 int did_send = 0;
676
Christopher Fauletb041b232022-03-24 10:27:02 +0100677 if (cs->endp->flags & (CS_EP_ERROR|CS_EP_ERR_PENDING) || si_is_conn_error(si)) {
Olivier Houchardc31e2cb2019-06-24 16:08:08 +0200678 /* We're probably there because the tasklet was woken up,
679 * but process_stream() ran before, detected there were an
680 * error and put the si back to SI_ST_TAR. There's still
681 * CO_FL_ERROR on the connection but we don't want to add
Christopher Faulet6cd56d52022-03-30 10:47:32 +0200682 * CS_EP_ERROR back, so give up
Olivier Houchardc31e2cb2019-06-24 16:08:08 +0200683 */
684 if (si->state < SI_ST_CON)
685 return 0;
Christopher Faulet6cd56d52022-03-30 10:47:32 +0200686 cs->endp->flags |= CS_EP_ERROR;
Olivier Houchardc2aa7112018-09-11 18:27:21 +0200687 return 1;
Willy Tarreaubddf7fc2018-12-19 17:17:10 +0100688 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200689
Christopher Faulet328ed222019-09-23 15:57:29 +0200690 /* We're already waiting to be able to send, give up */
691 if (si->wait_event.events & SUB_RETRY_SEND)
692 return 0;
693
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200694 /* we might have been called just after an asynchronous shutw */
Willy Tarreauf22758d2020-01-23 18:25:23 +0100695 if (oc->flags & CF_SHUTW)
Olivier Houchardc2aa7112018-09-11 18:27:21 +0200696 return 1;
Willy Tarreau5368d802012-08-21 18:22:06 +0200697
Christopher Faulete96993b2020-07-30 09:26:46 +0200698 /* we must wait because the mux is not installed yet */
699 if (!conn->mux)
700 return 0;
701
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200702 if (oc->pipe && conn->xprt->snd_pipe && conn->mux->snd_pipe) {
703 ret = conn->mux->snd_pipe(cs, oc->pipe);
Christopher Faulet86162db2019-07-05 11:49:11 +0200704 if (ret > 0)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200705 did_send = 1;
Willy Tarreau5368d802012-08-21 18:22:06 +0200706
Willy Tarreauafc8a222014-11-28 15:46:27 +0100707 if (!oc->pipe->data) {
708 put_pipe(oc->pipe);
709 oc->pipe = NULL;
Willy Tarreau5368d802012-08-21 18:22:06 +0200710 }
711
Christopher Faulet3f76f4c2018-11-20 10:21:08 +0100712 if (oc->pipe)
713 goto end;
Willy Tarreau5368d802012-08-21 18:22:06 +0200714 }
715
716 /* At this point, the pipe is empty, but we may still have data pending
717 * in the normal buffer.
718 */
Christopher Faulet55dec0d2018-11-20 10:30:02 +0100719 if (co_data(oc)) {
720 /* when we're here, we already know that there is no spliced
721 * data left, and that there are sendable buffered data.
722 */
Willy Tarreau5368d802012-08-21 18:22:06 +0200723
Willy Tarreau5368d802012-08-21 18:22:06 +0200724 /* check if we want to inform the kernel that we're interested in
725 * sending more data after this call. We want this if :
726 * - we're about to close after this last send and want to merge
727 * the ongoing FIN with the last segment.
728 * - we know we can't send everything at once and must get back
729 * here because of unaligned data
730 * - there is still a finite amount of data to forward
731 * The test is arranged so that the most common case does only 2
732 * tests.
733 */
Willy Tarreau1049b1f2014-02-02 01:51:17 +0100734 unsigned int send_flag = 0;
Willy Tarreau5368d802012-08-21 18:22:06 +0200735
Willy Tarreauafc8a222014-11-28 15:46:27 +0100736 if ((!(oc->flags & (CF_NEVER_WAIT|CF_SEND_DONTWAIT)) &&
737 ((oc->to_forward && oc->to_forward != CHN_INFINITE_FORWARD) ||
Willy Tarreau8945bb62020-06-19 17:07:06 +0200738 (oc->flags & CF_EXPECT_MORE) ||
Christopher Faulet9e3dc832020-07-22 16:28:44 +0200739 (IS_HTX_STRM(si_strm(si)) &&
740 (!(oc->flags & (CF_EOI|CF_SHUTR)) && htx_expect_more(htxbuf(&oc->buf)))))) ||
Willy Tarreauecd2e152017-11-07 15:07:25 +0100741 ((oc->flags & CF_ISRESP) &&
742 ((oc->flags & (CF_AUTO_CLOSE|CF_SHUTW_NOW)) == (CF_AUTO_CLOSE|CF_SHUTW_NOW))))
Willy Tarreau1049b1f2014-02-02 01:51:17 +0100743 send_flag |= CO_SFL_MSG_MORE;
Willy Tarreau5368d802012-08-21 18:22:06 +0200744
Willy Tarreauafc8a222014-11-28 15:46:27 +0100745 if (oc->flags & CF_STREAMER)
Willy Tarreau7bed9452014-02-02 02:00:24 +0100746 send_flag |= CO_SFL_STREAMER;
747
Christopher Faulete05bf9e2022-03-29 15:23:40 +0200748 if (s->txn && s->txn->flags & TX_L7_RETRY && !b_data(&s->txn->l7_buffer)) {
Christopher Faulet9f5382e2021-05-21 13:46:14 +0200749 /* If we want to be able to do L7 retries, copy
750 * the data we're about to send, so that we are able
751 * to resend them if needed
752 */
753 /* Try to allocate a buffer if we had none.
754 * If it fails, the next test will just
755 * disable the l7 retries by setting
756 * l7_conn_retries to 0.
757 */
Christopher Faulete05bf9e2022-03-29 15:23:40 +0200758 if (s->txn->req.msg_state != HTTP_MSG_DONE)
759 s->txn->flags &= ~TX_L7_RETRY;
Christopher Faulet9f5382e2021-05-21 13:46:14 +0200760 else {
Christopher Faulete05bf9e2022-03-29 15:23:40 +0200761 if (b_alloc(&s->txn->l7_buffer) == NULL)
762 s->txn->flags &= ~TX_L7_RETRY;
Christopher Faulet9f5382e2021-05-21 13:46:14 +0200763 else {
Christopher Faulete05bf9e2022-03-29 15:23:40 +0200764 memcpy(b_orig(&s->txn->l7_buffer),
Christopher Faulet9f5382e2021-05-21 13:46:14 +0200765 b_orig(&oc->buf),
766 b_size(&oc->buf));
Christopher Faulete05bf9e2022-03-29 15:23:40 +0200767 s->txn->l7_buffer.head = co_data(oc);
768 b_add(&s->txn->l7_buffer, co_data(oc));
Christopher Faulet9f5382e2021-05-21 13:46:14 +0200769 }
770
771 }
772 }
773
Christopher Faulet897d6122021-12-17 17:28:35 +0100774 ret = conn->mux->snd_buf(cs, &oc->buf, co_data(oc), send_flag);
Godbache68e02d2013-10-11 15:48:29 +0800775 if (ret > 0) {
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200776 did_send = 1;
Willy Tarreau84240042022-02-28 16:51:23 +0100777 c_rew(oc, ret);
Willy Tarreaudeccd112018-06-14 18:38:55 +0200778 c_realign_if_empty(oc);
779
780 if (!co_data(oc)) {
Godbache68e02d2013-10-11 15:48:29 +0800781 /* Always clear both flags once everything has been sent, they're one-shot */
Willy Tarreauafc8a222014-11-28 15:46:27 +0100782 oc->flags &= ~(CF_EXPECT_MORE | CF_SEND_DONTWAIT);
Godbache68e02d2013-10-11 15:48:29 +0800783 }
Godbache68e02d2013-10-11 15:48:29 +0800784 /* if some data remain in the buffer, it's only because the
785 * system buffers are full, we will try next time.
786 */
Willy Tarreau5368d802012-08-21 18:22:06 +0200787 }
Godbache68e02d2013-10-11 15:48:29 +0800788 }
Christopher Faulet55dec0d2018-11-20 10:30:02 +0100789
Willy Tarreauf6975aa2018-11-15 14:33:05 +0100790 end:
Christopher Faulet86162db2019-07-05 11:49:11 +0200791 if (did_send) {
792 oc->flags |= CF_WRITE_PARTIAL | CF_WROTE_DATA;
793 if (si->state == SI_ST_CON)
794 si->state = SI_ST_RDY;
Christopher Faulet037b3eb2019-07-05 13:44:29 +0200795
796 si_rx_room_rdy(si_opposite(si));
Christopher Faulet86162db2019-07-05 11:49:11 +0200797 }
798
Christopher Fauletb041b232022-03-24 10:27:02 +0100799 if (cs->endp->flags & (CS_EP_ERROR|CS_EP_ERR_PENDING)) {
Christopher Faulet6cd56d52022-03-30 10:47:32 +0200800 cs->endp->flags |= CS_EP_ERROR;
Christopher Faulet86162db2019-07-05 11:49:11 +0200801 return 1;
802 }
803
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200804 /* 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 +0100805 if (!channel_is_empty(oc))
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100806 conn->mux->subscribe(cs, SUB_RETRY_SEND, &si->wait_event);
Olivier Houchardf6535282018-08-31 17:29:12 +0200807 return did_send;
Willy Tarreau5368d802012-08-21 18:22:06 +0200808}
809
Willy Tarreau8ccd2082018-11-07 07:47:52 +0100810/* This is the ->process() function for any stream-interface's wait_event task.
811 * It's assigned during the stream-interface's initialization, for any type of
812 * stream interface. Thus it is always safe to perform a tasklet_wakeup() on a
813 * stream interface, as the presence of the CS is checked there.
814 */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100815struct task *si_cs_io_cb(struct task *t, void *ctx, unsigned int state)
Olivier Houchard91894cb2018-08-02 18:06:28 +0200816{
Olivier Houchard8f0b4c62018-08-02 18:21:38 +0200817 struct stream_interface *si = ctx;
Christopher Faulet13a35e52021-12-20 15:34:16 +0100818 struct conn_stream *cs = si->cs;
Olivier Houchardf6535282018-08-31 17:29:12 +0200819 int ret = 0;
Olivier Houcharda6ff0352018-08-21 15:59:43 +0200820
Christopher Faulet0256da12021-12-15 09:50:17 +0100821 if (!cs_conn(cs))
Willy Tarreau74163142021-03-13 11:30:19 +0100822 return t;
Willy Tarreau8ccd2082018-11-07 07:47:52 +0100823
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100824 if (!(si->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(si_oc(si)))
Olivier Houchardf6535282018-08-31 17:29:12 +0200825 ret = si_cs_send(cs);
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100826 if (!(si->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardf6535282018-08-31 17:29:12 +0200827 ret |= si_cs_recv(cs);
828 if (ret != 0)
Olivier Houchardc2aa7112018-09-11 18:27:21 +0200829 si_cs_process(cs);
Olivier Houchardf6535282018-08-31 17:29:12 +0200830
Willy Tarreaua64c7032019-08-01 14:17:02 +0200831 stream_release_buffers(si_strm(si));
Willy Tarreau74163142021-03-13 11:30:19 +0100832 return t;
Olivier Houchard91894cb2018-08-02 18:06:28 +0200833}
834
Willy Tarreau25f13102015-09-24 11:32:22 +0200835/* This function is designed to be called from within the stream handler to
Willy Tarreau236c4292019-06-06 08:19:20 +0200836 * update the input channel's expiration timer and the stream interface's
837 * Rx flags based on the channel's flags. It needs to be called only once
838 * after the channel's flags have settled down, and before they are cleared,
839 * though it doesn't harm to call it as often as desired (it just slightly
840 * hurts performance). It must not be called from outside of the stream
841 * handler, as what it does will be used to compute the stream task's
842 * expiration.
Willy Tarreau25f13102015-09-24 11:32:22 +0200843 */
Willy Tarreau236c4292019-06-06 08:19:20 +0200844void si_update_rx(struct stream_interface *si)
Willy Tarreau25f13102015-09-24 11:32:22 +0200845{
846 struct channel *ic = si_ic(si);
Willy Tarreau25f13102015-09-24 11:32:22 +0200847
Willy Tarreau236c4292019-06-06 08:19:20 +0200848 if (ic->flags & CF_SHUTR) {
849 si_rx_shut_blk(si);
850 return;
851 }
Willy Tarreaub26a6f92018-11-14 17:10:36 +0100852
Willy Tarreau236c4292019-06-06 08:19:20 +0200853 /* Read not closed, update FD status and timeout for reads */
854 if (ic->flags & CF_DONT_READ)
855 si_rx_chan_blk(si);
856 else
857 si_rx_chan_rdy(si);
Willy Tarreaub26a6f92018-11-14 17:10:36 +0100858
Christopher Faulet69fad002021-10-29 14:55:59 +0200859 if (!channel_is_empty(ic) || !channel_may_recv(ic)) {
Willy Tarreau236c4292019-06-06 08:19:20 +0200860 /* stop reading, imposed by channel's policy or contents */
861 si_rx_room_blk(si);
Willy Tarreau25f13102015-09-24 11:32:22 +0200862 }
Willy Tarreau236c4292019-06-06 08:19:20 +0200863 else {
864 /* (re)start reading and update timeout. Note: we don't recompute the timeout
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -0700865 * every time we get here, otherwise it would risk never to expire. We only
Willy Tarreau236c4292019-06-06 08:19:20 +0200866 * update it if is was not yet set. The stream socket handler will already
867 * have updated it if there has been a completed I/O.
868 */
869 si_rx_room_rdy(si);
870 }
871 if (si->flags & SI_FL_RXBLK_ANY & ~SI_FL_RX_WAIT_EP)
872 ic->rex = TICK_ETERNITY;
873 else if (!(ic->flags & CF_READ_NOEXP) && !tick_isset(ic->rex))
874 ic->rex = tick_add_ifset(now_ms, ic->rto);
Willy Tarreau25f13102015-09-24 11:32:22 +0200875
Willy Tarreau236c4292019-06-06 08:19:20 +0200876 si_chk_rcv(si);
877}
878
879/* This function is designed to be called from within the stream handler to
880 * update the output channel's expiration timer and the stream interface's
881 * Tx flags based on the channel's flags. It needs to be called only once
882 * after the channel's flags have settled down, and before they are cleared,
883 * though it doesn't harm to call it as often as desired (it just slightly
884 * hurts performance). It must not be called from outside of the stream
885 * handler, as what it does will be used to compute the stream task's
886 * expiration.
887 */
888void si_update_tx(struct stream_interface *si)
889{
890 struct channel *oc = si_oc(si);
891 struct channel *ic = si_ic(si);
892
893 if (oc->flags & CF_SHUTW)
894 return;
895
896 /* Write not closed, update FD status and timeout for writes */
897 if (channel_is_empty(oc)) {
898 /* stop writing */
899 if (!(si->flags & SI_FL_WAIT_DATA)) {
900 if ((oc->flags & CF_SHUTW_NOW) == 0)
901 si->flags |= SI_FL_WAIT_DATA;
902 oc->wex = TICK_ETERNITY;
Willy Tarreau25f13102015-09-24 11:32:22 +0200903 }
Willy Tarreau236c4292019-06-06 08:19:20 +0200904 return;
905 }
906
907 /* (re)start writing and update timeout. Note: we don't recompute the timeout
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -0700908 * every time we get here, otherwise it would risk never to expire. We only
Willy Tarreau236c4292019-06-06 08:19:20 +0200909 * update it if is was not yet set. The stream socket handler will already
910 * have updated it if there has been a completed I/O.
911 */
912 si->flags &= ~SI_FL_WAIT_DATA;
913 if (!tick_isset(oc->wex)) {
914 oc->wex = tick_add_ifset(now_ms, oc->wto);
915 if (tick_isset(ic->rex) && !(si->flags & SI_FL_INDEP_STR)) {
916 /* Note: depending on the protocol, we don't know if we're waiting
917 * for incoming data or not. So in order to prevent the socket from
918 * expiring read timeouts during writes, we refresh the read timeout,
919 * except if it was already infinite or if we have explicitly setup
920 * independent streams.
Willy Tarreau25f13102015-09-24 11:32:22 +0200921 */
Willy Tarreau236c4292019-06-06 08:19:20 +0200922 ic->rex = tick_add_ifset(now_ms, ic->rto);
Willy Tarreau25f13102015-09-24 11:32:22 +0200923 }
924 }
925}
926
Christopher Faulet9936dc62022-02-28 09:21:58 +0100927/* This tries to perform a synchronous receive on the stream interface to
928 * try to collect last arrived data. In practice it's only implemented on
929 * conn_streams. Returns 0 if nothing was done, non-zero if new data or a
930 * shutdown were collected. This may result on some delayed receive calls
931 * to be programmed and performed later, though it doesn't provide any
932 * such guarantee.
933 */
934int si_sync_recv(struct stream_interface *si)
935{
936 if (!si_state_in(si->state, SI_SB_RDY|SI_SB_EST))
937 return 0;
938
939 if (!cs_conn_mux(si->cs))
940 return 0; // only conn_streams are supported
941
942 if (si->wait_event.events & SUB_RETRY_RECV)
943 return 0; // already subscribed
944
945 if (!si_rx_endp_ready(si) || si_rx_blocked(si))
946 return 0; // already failed
947
948 return si_cs_recv(si->cs);
949}
950
Willy Tarreau3b285d72019-06-06 08:20:17 +0200951/* perform a synchronous send() for the stream interface. The CF_WRITE_NULL and
952 * CF_WRITE_PARTIAL flags are cleared prior to the attempt, and will possibly
953 * be updated in case of success.
954 */
955void si_sync_send(struct stream_interface *si)
956{
957 struct channel *oc = si_oc(si);
Willy Tarreau3b285d72019-06-06 08:20:17 +0200958
959 oc->flags &= ~(CF_WRITE_NULL|CF_WRITE_PARTIAL);
960
961 if (oc->flags & CF_SHUTW)
962 return;
963
964 if (channel_is_empty(oc))
965 return;
966
967 if (!si_state_in(si->state, SI_SB_CON|SI_SB_RDY|SI_SB_EST))
968 return;
969
Christopher Faulet13a35e52021-12-20 15:34:16 +0100970 if (!cs_conn_mux(si->cs))
Willy Tarreau3b285d72019-06-06 08:20:17 +0200971 return;
972
Christopher Faulet13a35e52021-12-20 15:34:16 +0100973 si_cs_send(si->cs);
Willy Tarreau3b285d72019-06-06 08:20:17 +0200974}
975
Willy Tarreaud14844a2018-11-08 18:15:29 +0100976/* Updates at once the channel flags, and timers of both stream interfaces of a
977 * same stream, to complete the work after the analysers, then updates the data
978 * layer below. This will ensure that any synchronous update performed at the
979 * data layer will be reflected in the channel flags and/or stream-interface.
Willy Tarreau829bd472019-06-06 09:17:23 +0200980 * Note that this does not change the stream interface's current state, though
981 * it updates the previous state to the current one.
Willy Tarreaud14844a2018-11-08 18:15:29 +0100982 */
983void si_update_both(struct stream_interface *si_f, struct stream_interface *si_b)
984{
985 struct channel *req = si_ic(si_f);
986 struct channel *res = si_oc(si_f);
Willy Tarreaud14844a2018-11-08 18:15:29 +0100987
988 req->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_READ_ATTACHED|CF_WRITE_NULL|CF_WRITE_PARTIAL);
989 res->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_READ_ATTACHED|CF_WRITE_NULL|CF_WRITE_PARTIAL);
990
Willy Tarreaud14844a2018-11-08 18:15:29 +0100991 si_f->prev_state = si_f->state;
992 si_b->prev_state = si_b->state;
993
Willy Tarreaubf89ff32018-11-09 14:59:25 +0100994 /* let's recompute both sides states */
Willy Tarreau4f283fa2019-06-05 14:34:03 +0200995 if (si_state_in(si_f->state, SI_SB_RDY|SI_SB_EST))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100996 si_update(si_f);
Willy Tarreaubf89ff32018-11-09 14:59:25 +0100997
Willy Tarreau4f283fa2019-06-05 14:34:03 +0200998 if (si_state_in(si_b->state, SI_SB_RDY|SI_SB_EST))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100999 si_update(si_b);
Willy Tarreaubf89ff32018-11-09 14:59:25 +01001000
1001 /* stream ints are processed outside of process_stream() and must be
1002 * handled at the latest moment.
1003 */
Christopher Faulet13a35e52021-12-20 15:34:16 +01001004 if (cs_appctx(si_f->cs) &&
Willy Tarreau32742fd2018-11-14 14:07:59 +01001005 ((si_rx_endp_ready(si_f) && !si_rx_blocked(si_f)) ||
1006 (si_tx_endp_ready(si_f) && !si_tx_blocked(si_f))))
Christopher Faulet693b23b2022-02-28 09:09:05 +01001007 appctx_wakeup(__cs_appctx(si_f->cs));
Willy Tarreaud14844a2018-11-08 18:15:29 +01001008
Christopher Faulet13a35e52021-12-20 15:34:16 +01001009 if (cs_appctx(si_b->cs) &&
Willy Tarreau32742fd2018-11-14 14:07:59 +01001010 ((si_rx_endp_ready(si_b) && !si_rx_blocked(si_b)) ||
1011 (si_tx_endp_ready(si_b) && !si_tx_blocked(si_b))))
Christopher Faulet693b23b2022-02-28 09:09:05 +01001012 appctx_wakeup(__cs_appctx(si_b->cs));
Willy Tarreaud14844a2018-11-08 18:15:29 +01001013}
1014
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001015/*
1016 * This function performs a shutdown-read on a stream interface attached to
1017 * a connection in a connected or init state (it does nothing for other
1018 * states). It either shuts the read side or marks itself as closed. The buffer
1019 * flags are updated to reflect the new state. If the stream interface has
Christopher Faulet8abe7122022-03-30 15:10:18 +02001020 * CS_FL_NOHALF, we also forward the close to the write side. If a control
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001021 * layer is defined, then it is supposed to be a socket layer and file
Willy Tarreau6fe15412013-09-29 15:16:03 +02001022 * descriptors are then shutdown or closed accordingly. The function
1023 * automatically disables polling if needed.
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001024 */
Willy Tarreau6fe15412013-09-29 15:16:03 +02001025static void stream_int_shutr_conn(struct stream_interface *si)
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001026{
Christopher Faulet13a35e52021-12-20 15:34:16 +01001027 struct conn_stream *cs = si->cs;
Willy Tarreauafc8a222014-11-28 15:46:27 +01001028 struct channel *ic = si_ic(si);
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001029
Christopher Faulet13a35e52021-12-20 15:34:16 +01001030 BUG_ON(!cs_conn(cs));
1031
Willy Tarreauabb5d422018-11-14 16:58:52 +01001032 si_rx_shut_blk(si);
Willy Tarreauafc8a222014-11-28 15:46:27 +01001033 if (ic->flags & CF_SHUTR)
Willy Tarreau6fe15412013-09-29 15:16:03 +02001034 return;
Willy Tarreauafc8a222014-11-28 15:46:27 +01001035 ic->flags |= CF_SHUTR;
1036 ic->rex = TICK_ETERNITY;
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001037
Willy Tarreau4f283fa2019-06-05 14:34:03 +02001038 if (!si_state_in(si->state, SI_SB_CON|SI_SB_RDY|SI_SB_EST))
Willy Tarreau6fe15412013-09-29 15:16:03 +02001039 return;
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001040
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001041 if (si_oc(si)->flags & CF_SHUTW) {
Willy Tarreaua553ae92017-10-05 18:52:17 +02001042 cs_close(cs);
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001043 si->state = SI_ST_DIS;
Christopher Fauletae024ce2022-03-29 19:02:31 +02001044 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001045 }
Christopher Faulet8abe7122022-03-30 15:10:18 +02001046 else if (si->cs->flags & CS_FL_NOHALF) {
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001047 /* we want to immediately forward this close to the write side */
1048 return stream_int_shutw_conn(si);
1049 }
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001050}
1051
1052/*
1053 * This function performs a shutdown-write on a stream interface attached to
1054 * a connection in a connected or init state (it does nothing for other
1055 * states). It either shuts the write side or marks itself as closed. The
1056 * buffer flags are updated to reflect the new state. It does also close
1057 * everything if the SI was marked as being in error state. If there is a
Willy Tarreau1398aa12015-03-12 23:04:07 +01001058 * data-layer shutdown, it is called.
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001059 */
Willy Tarreau6fe15412013-09-29 15:16:03 +02001060static void stream_int_shutw_conn(struct stream_interface *si)
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001061{
Christopher Faulet13a35e52021-12-20 15:34:16 +01001062 struct conn_stream *cs = si->cs;
Willy Tarreauafc8a222014-11-28 15:46:27 +01001063 struct channel *ic = si_ic(si);
1064 struct channel *oc = si_oc(si);
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001065
Christopher Faulet13a35e52021-12-20 15:34:16 +01001066 BUG_ON(!cs_conn(cs));
1067
Willy Tarreauafc8a222014-11-28 15:46:27 +01001068 oc->flags &= ~CF_SHUTW_NOW;
1069 if (oc->flags & CF_SHUTW)
Willy Tarreau6fe15412013-09-29 15:16:03 +02001070 return;
Willy Tarreauafc8a222014-11-28 15:46:27 +01001071 oc->flags |= CF_SHUTW;
1072 oc->wex = TICK_ETERNITY;
Willy Tarreau43e69dc2018-11-06 19:23:03 +01001073 si_done_get(si);
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001074
Christopher Faulet1d987772022-03-29 18:03:35 +02001075 if (tick_isset(si->cs->hcto)) {
1076 ic->rto = si->cs->hcto;
Hongbo Longe39683c2017-03-10 18:41:51 +01001077 ic->rex = tick_add(now_ms, ic->rto);
1078 }
1079
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001080 switch (si->state) {
Willy Tarreau4f283fa2019-06-05 14:34:03 +02001081 case SI_ST_RDY:
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001082 case SI_ST_EST:
1083 /* we have to shut before closing, otherwise some short messages
1084 * may never leave the system, especially when there are remaining
1085 * unread data in the socket input buffer, or when nolinger is set.
Christopher Faulet8abe7122022-03-30 15:10:18 +02001086 * However, if CS_FL_NOLINGER is explicitly set, we know there is
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001087 * no risk so we close both sides immediately.
1088 */
Willy Tarreau51d0a7e2019-01-31 19:09:59 +01001089
Christopher Faulet6cd56d52022-03-30 10:47:32 +02001090 if (cs->endp->flags & CS_EP_ERROR) {
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001091 /* quick close, the socket is already shut anyway */
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001092 }
Christopher Faulet8abe7122022-03-30 15:10:18 +02001093 else if (cs->flags & CS_FL_NOLINGER) {
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001094 /* unclean data-layer shutdown, typically an aborted request
1095 * or a forwarded shutdown from a client to a server due to
1096 * option abortonclose. No need for the TLS layer to try to
1097 * emit a shutdown message.
1098 */
Willy Tarreauecdb3fe2017-10-05 15:25:48 +02001099 cs_shutw(cs, CS_SHW_SILENT);
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001100 }
1101 else {
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001102 /* clean data-layer shutdown. This only happens on the
1103 * frontend side, or on the backend side when forwarding
1104 * a client close in TCP mode or in HTTP TUNNEL mode
1105 * while option abortonclose is set. We want the TLS
1106 * layer to try to signal it to the peer before we close.
1107 */
Willy Tarreauecdb3fe2017-10-05 15:25:48 +02001108 cs_shutw(cs, CS_SHW_NORMAL);
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001109
Willy Tarreaua5ea7512020-12-11 10:24:05 +01001110 if (!(ic->flags & (CF_SHUTR|CF_DONT_READ)))
Willy Tarreaua553ae92017-10-05 18:52:17 +02001111 return;
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001112 }
1113
1114 /* fall through */
1115 case SI_ST_CON:
1116 /* we may have to close a pending connection, and mark the
1117 * response buffer as shutr
1118 */
Willy Tarreaua553ae92017-10-05 18:52:17 +02001119 cs_close(cs);
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001120 /* fall through */
1121 case SI_ST_CER:
1122 case SI_ST_QUE:
1123 case SI_ST_TAR:
1124 si->state = SI_ST_DIS;
Willy Tarreau4a59f2f2013-10-24 20:10:45 +02001125 /* fall through */
Willy Tarreau8b3d7df2013-09-29 14:51:58 +02001126 default:
Christopher Faulet8abe7122022-03-30 15:10:18 +02001127 cs->flags &= ~CS_FL_NOLINGER;
Willy Tarreauabb5d422018-11-14 16:58:52 +01001128 si_rx_shut_blk(si);
Willy Tarreauafc8a222014-11-28 15:46:27 +01001129 ic->flags |= CF_SHUTR;
1130 ic->rex = TICK_ETERNITY;
Christopher Fauletae024ce2022-03-29 19:02:31 +02001131 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
Willy Tarreau100c4672012-08-20 12:06:26 +02001132 }
1133}
1134
Willy Tarreau46a8d922012-08-20 12:38:36 +02001135/* This function is used for inter-stream-interface calls. It is called by the
1136 * consumer to inform the producer side that it may be interested in checking
1137 * for free space in the buffer. Note that it intentionally does not update
1138 * timeouts, so that we can still check them later at wake-up. This function is
1139 * dedicated to connection-based stream interfaces.
1140 */
Willy Tarreauc5788912012-08-24 18:12:41 +02001141static void stream_int_chk_rcv_conn(struct stream_interface *si)
Willy Tarreau46a8d922012-08-20 12:38:36 +02001142{
Christopher Fauletb3e0de42018-10-11 13:54:13 +02001143 /* (re)start reading */
Willy Tarreau4f283fa2019-06-05 14:34:03 +02001144 if (si_state_in(si->state, SI_SB_CON|SI_SB_RDY|SI_SB_EST))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001145 tasklet_wakeup(si->wait_event.tasklet);
Willy Tarreau46a8d922012-08-20 12:38:36 +02001146}
1147
1148
Willy Tarreaude5722c2012-08-20 15:01:10 +02001149/* This function is used for inter-stream-interface calls. It is called by the
1150 * producer to inform the consumer side that it may be interested in checking
1151 * for data in the buffer. Note that it intentionally does not update timeouts,
1152 * so that we can still check them later at wake-up.
1153 */
Willy Tarreauc5788912012-08-24 18:12:41 +02001154static void stream_int_chk_snd_conn(struct stream_interface *si)
Willy Tarreaude5722c2012-08-20 15:01:10 +02001155{
Willy Tarreauafc8a222014-11-28 15:46:27 +01001156 struct channel *oc = si_oc(si);
Christopher Faulet13a35e52021-12-20 15:34:16 +01001157 struct conn_stream *cs = si->cs;
Christopher Faulet897d6122021-12-17 17:28:35 +01001158
Willy Tarreau0c3205a2022-03-23 11:11:31 +01001159 BUG_ON(!cs_conn(cs));
Willy Tarreaude5722c2012-08-20 15:01:10 +02001160
Willy Tarreau4f283fa2019-06-05 14:34:03 +02001161 if (unlikely(!si_state_in(si->state, SI_SB_CON|SI_SB_RDY|SI_SB_EST) ||
Olivier Houchardb2fc04e2019-04-11 13:56:26 +02001162 (oc->flags & CF_SHUTW)))
Willy Tarreaude5722c2012-08-20 15:01:10 +02001163 return;
Willy Tarreaude5722c2012-08-20 15:01:10 +02001164
Willy Tarreauafc8a222014-11-28 15:46:27 +01001165 if (unlikely(channel_is_empty(oc))) /* called with nothing to send ! */
Willy Tarreaude5722c2012-08-20 15:01:10 +02001166 return;
1167
Willy Tarreauafc8a222014-11-28 15:46:27 +01001168 if (!oc->pipe && /* spliced data wants to be forwarded ASAP */
Willy Tarreaub0165872012-12-15 10:12:39 +01001169 !(si->flags & SI_FL_WAIT_DATA)) /* not waiting for data */
Willy Tarreaude5722c2012-08-20 15:01:10 +02001170 return;
1171
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001172 if (!(si->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(si_oc(si)))
Olivier Houchard31f04e42018-10-22 16:01:09 +02001173 si_cs_send(cs);
Willy Tarreau33a09a52018-10-25 13:49:49 +02001174
Christopher Fauletb041b232022-03-24 10:27:02 +01001175 if (cs->endp->flags & (CS_EP_ERROR|CS_EP_ERR_PENDING) || si_is_conn_error(si)) {
Willy Tarreau3b9c8502017-10-25 14:22:28 +02001176 /* Write error on the file descriptor */
Olivier Houchardc31e2cb2019-06-24 16:08:08 +02001177 if (si->state >= SI_ST_CON)
Christopher Faulet6cd56d52022-03-30 10:47:32 +02001178 cs->endp->flags |= CS_EP_ERROR;
Willy Tarreau3b9c8502017-10-25 14:22:28 +02001179 goto out_wakeup;
Willy Tarreaude5722c2012-08-20 15:01:10 +02001180 }
1181
1182 /* OK, so now we know that some data might have been sent, and that we may
1183 * have to poll first. We have to do that too if the buffer is not empty.
1184 */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001185 if (channel_is_empty(oc)) {
Willy Tarreaude5722c2012-08-20 15:01:10 +02001186 /* the connection is established but we can't write. Either the
1187 * buffer is empty, or we just refrain from sending because the
1188 * ->o limit was reached. Maybe we just wrote the last
1189 * chunk and need to close.
1190 */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001191 if (((oc->flags & (CF_SHUTW|CF_AUTO_CLOSE|CF_SHUTW_NOW)) ==
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001192 (CF_AUTO_CLOSE|CF_SHUTW_NOW)) &&
Willy Tarreau4f283fa2019-06-05 14:34:03 +02001193 si_state_in(si->state, SI_SB_RDY|SI_SB_EST)) {
Willy Tarreaude5722c2012-08-20 15:01:10 +02001194 si_shutw(si);
1195 goto out_wakeup;
1196 }
1197
Willy Tarreauafc8a222014-11-28 15:46:27 +01001198 if ((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == 0)
Willy Tarreaude5722c2012-08-20 15:01:10 +02001199 si->flags |= SI_FL_WAIT_DATA;
Willy Tarreauafc8a222014-11-28 15:46:27 +01001200 oc->wex = TICK_ETERNITY;
Willy Tarreaude5722c2012-08-20 15:01:10 +02001201 }
1202 else {
1203 /* Otherwise there are remaining data to be sent in the buffer,
1204 * which means we have to poll before doing so.
1205 */
Willy Tarreaude5722c2012-08-20 15:01:10 +02001206 si->flags &= ~SI_FL_WAIT_DATA;
Willy Tarreauafc8a222014-11-28 15:46:27 +01001207 if (!tick_isset(oc->wex))
1208 oc->wex = tick_add_ifset(now_ms, oc->wto);
Willy Tarreaude5722c2012-08-20 15:01:10 +02001209 }
1210
Willy Tarreauafc8a222014-11-28 15:46:27 +01001211 if (likely(oc->flags & CF_WRITE_ACTIVITY)) {
1212 struct channel *ic = si_ic(si);
1213
Willy Tarreaude5722c2012-08-20 15:01:10 +02001214 /* update timeout if we have written something */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001215 if ((oc->flags & (CF_SHUTW|CF_WRITE_PARTIAL)) == CF_WRITE_PARTIAL &&
1216 !channel_is_empty(oc))
1217 oc->wex = tick_add_ifset(now_ms, oc->wto);
Willy Tarreaude5722c2012-08-20 15:01:10 +02001218
Willy Tarreauafc8a222014-11-28 15:46:27 +01001219 if (tick_isset(ic->rex) && !(si->flags & SI_FL_INDEP_STR)) {
Willy Tarreaude5722c2012-08-20 15:01:10 +02001220 /* Note: to prevent the client from expiring read timeouts
1221 * during writes, we refresh it. We only do this if the
1222 * interface is not configured for "independent streams",
1223 * because for some applications it's better not to do this,
1224 * for instance when continuously exchanging small amounts
1225 * of data which can full the socket buffers long before a
1226 * write timeout is detected.
1227 */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001228 ic->rex = tick_add_ifset(now_ms, ic->rto);
Willy Tarreaude5722c2012-08-20 15:01:10 +02001229 }
1230 }
1231
1232 /* in case of special condition (error, shutdown, end of write...), we
1233 * have to notify the task.
1234 */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001235 if (likely((oc->flags & (CF_WRITE_NULL|CF_WRITE_ERROR|CF_SHUTW)) ||
1236 ((oc->flags & CF_WAKE_WRITE) &&
1237 ((channel_is_empty(oc) && !oc->to_forward) ||
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02001238 !si_state_in(si->state, SI_SB_EST))))) {
Willy Tarreaude5722c2012-08-20 15:01:10 +02001239 out_wakeup:
Willy Tarreau07373b82014-11-28 12:08:47 +01001240 if (!(si->flags & SI_FL_DONT_WAKE))
1241 task_wakeup(si_task(si), TASK_WOKEN_IO);
Willy Tarreaude5722c2012-08-20 15:01:10 +02001242 }
1243}
1244
Willy Tarreaueecf6ca2012-08-20 15:09:53 +02001245/*
Willy Tarreauce323de2012-08-20 21:41:06 +02001246 * This is the callback which is called by the connection layer to receive data
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001247 * into the buffer from the connection. It iterates over the mux layer's
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001248 * rcv_buf function.
Willy Tarreauce323de2012-08-20 21:41:06 +02001249 */
Christopher Faulet9936dc62022-02-28 09:21:58 +01001250static int si_cs_recv(struct conn_stream *cs)
Willy Tarreauce323de2012-08-20 21:41:06 +02001251{
Christopher Faulet693b23b2022-02-28 09:09:05 +01001252 struct connection *conn = __cs_conn(cs);
Christopher Fauletf835dea2021-12-21 14:35:17 +01001253 struct stream_interface *si = cs_si(cs);
Willy Tarreauafc8a222014-11-28 15:46:27 +01001254 struct channel *ic = si_ic(si);
Olivier Houchardf6535282018-08-31 17:29:12 +02001255 int ret, max, cur_read = 0;
Willy Tarreauce323de2012-08-20 21:41:06 +02001256 int read_poll = MAX_READ_POLL_LOOPS;
Christopher Fauletc6618d62018-10-11 15:56:04 +02001257 int flags = 0;
Willy Tarreauce323de2012-08-20 21:41:06 +02001258
Christopher Faulet04400bc2019-10-25 10:21:01 +02001259 /* If not established yet, do nothing. */
1260 if (si->state != SI_ST_EST)
1261 return 0;
1262
Olivier Houchardf6535282018-08-31 17:29:12 +02001263 /* If another call to si_cs_recv() failed, and we subscribed to
1264 * recv events already, give up now.
1265 */
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001266 if (si->wait_event.events & SUB_RETRY_RECV)
Olivier Houchardf6535282018-08-31 17:29:12 +02001267 return 0;
Willy Tarreauce323de2012-08-20 21:41:06 +02001268
Willy Tarreauce323de2012-08-20 21:41:06 +02001269 /* maybe we were called immediately after an asynchronous shutr */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001270 if (ic->flags & CF_SHUTR)
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001271 return 1;
Willy Tarreauce323de2012-08-20 21:41:06 +02001272
Christopher Faulete96993b2020-07-30 09:26:46 +02001273 /* we must wait because the mux is not installed yet */
1274 if (!conn->mux)
1275 return 0;
1276
Willy Tarreau54e917c2017-08-30 07:35:35 +02001277 /* stop here if we reached the end of data */
Christopher Fauletb041b232022-03-24 10:27:02 +01001278 if (cs->endp->flags & CS_EP_EOS)
Christopher Faulet36b536d2019-11-20 11:56:33 +01001279 goto end_recv;
Willy Tarreau54e917c2017-08-30 07:35:35 +02001280
Christopher Fauletf061e422018-12-07 14:51:20 +01001281 /* stop immediately on errors. Note that we DON'T want to stop on
1282 * POLL_ERR, as the poller might report a write error while there
1283 * are still data available in the recv buffer. This typically
1284 * happens when we send too large a request to a backend server
1285 * which rejects it before reading it all.
1286 */
Christopher Fauletb041b232022-03-24 10:27:02 +01001287 if (!(cs->endp->flags & CS_EP_RCV_MORE)) {
Christopher Fauletf061e422018-12-07 14:51:20 +01001288 if (!conn_xprt_ready(conn))
1289 return 0;
Christopher Fauletb041b232022-03-24 10:27:02 +01001290 if (cs->endp->flags & CS_EP_ERROR)
Christopher Faulet36b536d2019-11-20 11:56:33 +01001291 goto end_recv;
Christopher Fauletf061e422018-12-07 14:51:20 +01001292 }
Willy Tarreauf26c26c2018-11-12 16:11:08 +01001293
Willy Tarreau7ab99a32018-12-18 09:15:43 +01001294 /* prepare to detect if the mux needs more room */
Christopher Fauletb041b232022-03-24 10:27:02 +01001295 cs->endp->flags &= ~CS_EP_WANT_ROOM;
Willy Tarreau7ab99a32018-12-18 09:15:43 +01001296
Willy Tarreau77e478c2018-06-19 07:03:14 +02001297 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) && !co_data(ic) &&
Willy Tarreau7e312732014-02-12 16:35:14 +01001298 global.tune.idle_timer &&
Willy Tarreauafc8a222014-11-28 15:46:27 +01001299 (unsigned short)(now_ms - ic->last_read) >= global.tune.idle_timer) {
Willy Tarreauc5890e62014-02-09 17:47:01 +01001300 /* The buffer was empty and nothing was transferred for more
1301 * than one second. This was caused by a pause and not by
1302 * congestion. Reset any streaming mode to reduce latency.
1303 */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001304 ic->xfer_small = 0;
1305 ic->xfer_large = 0;
1306 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
Willy Tarreauc5890e62014-02-09 17:47:01 +01001307 }
1308
Willy Tarreau96199b12012-08-24 00:46:52 +02001309 /* First, let's see if we may splice data across the channel without
1310 * using a buffer.
1311 */
Christopher Faulete9e48202022-03-22 18:13:29 +01001312 if (cs->endp->flags & CS_EP_MAY_SPLICE &&
Willy Tarreauafc8a222014-11-28 15:46:27 +01001313 (ic->pipe || ic->to_forward >= MIN_SPLICE_FORWARD) &&
1314 ic->flags & CF_KERN_SPLICING) {
Willy Tarreaud760eec2018-07-10 09:50:25 +02001315 if (c_data(ic)) {
Willy Tarreau96199b12012-08-24 00:46:52 +02001316 /* We're embarrassed, there are already data pending in
1317 * the buffer and we don't want to have them at two
1318 * locations at a time. Let's indicate we need some
1319 * place and ask the consumer to hurry.
1320 */
Christopher Fauletc6618d62018-10-11 15:56:04 +02001321 flags |= CO_RFL_BUF_FLUSH;
Willy Tarreau96199b12012-08-24 00:46:52 +02001322 goto abort_splice;
1323 }
Willy Tarreauce323de2012-08-20 21:41:06 +02001324
Willy Tarreauafc8a222014-11-28 15:46:27 +01001325 if (unlikely(ic->pipe == NULL)) {
1326 if (pipes_used >= global.maxpipes || !(ic->pipe = get_pipe())) {
1327 ic->flags &= ~CF_KERN_SPLICING;
Willy Tarreau96199b12012-08-24 00:46:52 +02001328 goto abort_splice;
1329 }
1330 }
1331
Olivier Houchard9aaf7782017-09-13 18:30:23 +02001332 ret = conn->mux->rcv_pipe(cs, ic->pipe, ic->to_forward);
Willy Tarreau96199b12012-08-24 00:46:52 +02001333 if (ret < 0) {
1334 /* splice not supported on this end, let's disable it */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001335 ic->flags &= ~CF_KERN_SPLICING;
Willy Tarreau96199b12012-08-24 00:46:52 +02001336 goto abort_splice;
1337 }
Willy Tarreauce323de2012-08-20 21:41:06 +02001338
Willy Tarreau96199b12012-08-24 00:46:52 +02001339 if (ret > 0) {
Willy Tarreauafc8a222014-11-28 15:46:27 +01001340 if (ic->to_forward != CHN_INFINITE_FORWARD)
1341 ic->to_forward -= ret;
1342 ic->total += ret;
Willy Tarreau96199b12012-08-24 00:46:52 +02001343 cur_read += ret;
Willy Tarreauafc8a222014-11-28 15:46:27 +01001344 ic->flags |= CF_READ_PARTIAL;
Willy Tarreauce323de2012-08-20 21:41:06 +02001345 }
Willy Tarreau96199b12012-08-24 00:46:52 +02001346
Christopher Fauletb041b232022-03-24 10:27:02 +01001347 if (cs->endp->flags & (CS_EP_EOS|CS_EP_ERROR))
Christopher Faulet36b536d2019-11-20 11:56:33 +01001348 goto end_recv;
Willy Tarreau96199b12012-08-24 00:46:52 +02001349
Willy Tarreau61d39a02013-07-18 21:49:32 +02001350 if (conn->flags & CO_FL_WAIT_ROOM) {
1351 /* the pipe is full or we have read enough data that it
1352 * could soon be full. Let's stop before needing to poll.
1353 */
Willy Tarreaudb398432018-11-15 11:08:52 +01001354 si_rx_room_blk(si);
Willy Tarreauffb12052018-11-15 16:06:02 +01001355 goto done_recv;
Willy Tarreau61d39a02013-07-18 21:49:32 +02001356 }
Willy Tarreau56a77e52012-09-02 18:34:44 +02001357
Willy Tarreauce323de2012-08-20 21:41:06 +02001358 /* splice not possible (anymore), let's go on on standard copy */
1359 }
Willy Tarreau96199b12012-08-24 00:46:52 +02001360
1361 abort_splice:
Willy Tarreauafc8a222014-11-28 15:46:27 +01001362 if (ic->pipe && unlikely(!ic->pipe->data)) {
1363 put_pipe(ic->pipe);
1364 ic->pipe = NULL;
Willy Tarreau96199b12012-08-24 00:46:52 +02001365 }
1366
Christopher Faulete9e48202022-03-22 18:13:29 +01001367 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 +01001368 /* don't break splicing by reading, but still call rcv_buf()
1369 * to pass the flag.
1370 */
1371 goto done_recv;
1372 }
1373
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001374 /* now we'll need a input buffer for the stream */
Willy Tarreau581abd32018-10-25 10:21:41 +02001375 if (!si_alloc_ibuf(si, &(si_strm(si)->buffer_wait)))
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001376 goto end_recv;
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001377
Christopher Faulet2bc364c2021-09-21 15:22:12 +02001378 /* For an HTX stream, if the buffer is stuck (no output data with some
1379 * input data) and if the HTX message is fragmented or if its free space
1380 * wraps, we force an HTX deframentation. It is a way to have a
1381 * contiguous free space nad to let the mux to copy as much data as
1382 * possible.
1383 *
1384 * NOTE: A possible optim may be to let the mux decides if defrag is
1385 * required or not, depending on amount of data to be xferred.
1386 */
1387 if (IS_HTX_STRM(si_strm(si)) && !co_data(ic)) {
1388 struct htx *htx = htxbuf(&ic->buf);
1389
1390 if (htx_is_not_empty(htx) && ((htx->flags & HTX_FL_FRAGMENTED) || htx_space_wraps(htx)))
1391 htx_defrag(htxbuf(&ic->buf), NULL, 0);
1392 }
Christopher Faulet68a14db2021-09-21 15:14:57 +02001393
1394 /* Instruct the mux it must subscribed for read events */
1395 flags |= ((!conn_is_back(conn) && (si_strm(si)->be->options & PR_O_ABRT_CLOSE)) ? CO_RFL_KEEP_RECV : 0);
1396
Willy Tarreau61d39a02013-07-18 21:49:32 +02001397 /* Important note : if we're called with POLL_IN|POLL_HUP, it means the read polling
1398 * was enabled, which implies that the recv buffer was not full. So we have a guarantee
1399 * that if such an event is not handled above in splice, it will be handled here by
1400 * recv().
1401 */
Christopher Fauletb041b232022-03-24 10:27:02 +01001402 while ((cs->endp->flags & CS_EP_RCV_MORE) ||
Willy Tarreaud1480cc2022-03-17 16:19:09 +01001403 (!(conn->flags & CO_FL_HANDSHAKE) &&
Christopher Fauletb041b232022-03-24 10:27:02 +01001404 (!(cs->endp->flags & (CS_EP_ERROR|CS_EP_EOS))) && !(ic->flags & CF_SHUTR))) {
Christopher Faulet68a14db2021-09-21 15:14:57 +02001405 int cur_flags = flags;
1406
1407 /* Compute transient CO_RFL_* flags */
Christopher Faulet564e39c2021-09-21 15:50:55 +02001408 if (co_data(ic)) {
1409 cur_flags |= (CO_RFL_BUF_WET | CO_RFL_BUF_NOT_STUCK);
1410 }
Christopher Faulet68a14db2021-09-21 15:14:57 +02001411
Christopher Faulet4eb7d742018-10-11 15:29:21 +02001412 /* <max> may be null. This is the mux responsibility to set
Christopher Fauletb041b232022-03-24 10:27:02 +01001413 * CS_EP_RCV_MORE on the CS if more space is needed.
Christopher Faulet4eb7d742018-10-11 15:29:21 +02001414 */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001415 max = channel_recv_max(ic);
Christopher Faulet897d6122021-12-17 17:28:35 +01001416 ret = conn->mux->rcv_buf(cs, &ic->buf, max, cur_flags);
Willy Tarreau674e0ad2018-12-05 13:45:41 +01001417
Christopher Fauletb041b232022-03-24 10:27:02 +01001418 if (cs->endp->flags & CS_EP_WANT_ROOM) {
1419 /* CS_EP_WANT_ROOM must not be reported if the channel's
Christopher Fauletae179252022-02-21 16:12:00 +01001420 * buffer is empty.
1421 */
1422 BUG_ON(c_empty(ic));
1423
Willy Tarreaudb398432018-11-15 11:08:52 +01001424 si_rx_room_blk(si);
Christopher Fauletdf994082021-09-23 14:17:20 +02001425 /* Add READ_PARTIAL because some data are pending but
1426 * cannot be xferred to the channel
1427 */
1428 ic->flags |= CF_READ_PARTIAL;
1429 }
Willy Tarreau6577b482017-12-10 21:19:33 +01001430
Willy Tarreauf26c26c2018-11-12 16:11:08 +01001431 if (ret <= 0) {
Willy Tarreau1ac5f202019-12-03 18:08:45 +01001432 /* if we refrained from reading because we asked for a
1433 * flush to satisfy rcv_pipe(), we must not subscribe
1434 * and instead report that there's not enough room
1435 * here to proceed.
1436 */
1437 if (flags & CO_RFL_BUF_FLUSH)
1438 si_rx_room_blk(si);
Willy Tarreauce323de2012-08-20 21:41:06 +02001439 break;
Willy Tarreauf26c26c2018-11-12 16:11:08 +01001440 }
Willy Tarreauce323de2012-08-20 21:41:06 +02001441
1442 cur_read += ret;
1443
1444 /* if we're allowed to directly forward data, we must update ->o */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001445 if (ic->to_forward && !(ic->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreauce323de2012-08-20 21:41:06 +02001446 unsigned long fwd = ret;
Willy Tarreauafc8a222014-11-28 15:46:27 +01001447 if (ic->to_forward != CHN_INFINITE_FORWARD) {
1448 if (fwd > ic->to_forward)
1449 fwd = ic->to_forward;
1450 ic->to_forward -= fwd;
Willy Tarreauce323de2012-08-20 21:41:06 +02001451 }
Willy Tarreaubcbd3932018-06-06 07:13:22 +02001452 c_adv(ic, fwd);
Willy Tarreauce323de2012-08-20 21:41:06 +02001453 }
1454
Willy Tarreauafc8a222014-11-28 15:46:27 +01001455 ic->flags |= CF_READ_PARTIAL;
1456 ic->total += ret;
Willy Tarreauce323de2012-08-20 21:41:06 +02001457
Christopher Faulet883d83e2021-09-09 10:17:59 +02001458 /* End-of-input reached, we can leave. In this case, it is
1459 * important to break the loop to not block the SI because of
1460 * the channel's policies.This way, we are still able to receive
1461 * shutdowns.
1462 */
Christopher Fauletb041b232022-03-24 10:27:02 +01001463 if (cs->endp->flags & CS_EP_EOI)
Christopher Faulet883d83e2021-09-09 10:17:59 +02001464 break;
1465
Willy Tarreauf26c26c2018-11-12 16:11:08 +01001466 if ((ic->flags & CF_READ_DONTWAIT) || --read_poll <= 0) {
1467 /* we're stopped by the channel's policy */
Willy Tarreaub26a6f92018-11-14 17:10:36 +01001468 si_rx_chan_blk(si);
Willy Tarreau62dd6982017-11-18 11:26:20 +01001469 break;
Willy Tarreauf26c26c2018-11-12 16:11:08 +01001470 }
Willy Tarreauce323de2012-08-20 21:41:06 +02001471
1472 /* if too many bytes were missing from last read, it means that
1473 * it's pointless trying to read again because the system does
1474 * not have them in buffers.
1475 */
1476 if (ret < max) {
Willy Tarreauce323de2012-08-20 21:41:06 +02001477 /* if a streamer has read few data, it may be because we
1478 * have exhausted system buffers. It's not worth trying
1479 * again.
1480 */
Willy Tarreauf26c26c2018-11-12 16:11:08 +01001481 if (ic->flags & CF_STREAMER) {
1482 /* we're stopped by the channel's policy */
Willy Tarreaub26a6f92018-11-14 17:10:36 +01001483 si_rx_chan_blk(si);
Willy Tarreauce323de2012-08-20 21:41:06 +02001484 break;
Willy Tarreauf26c26c2018-11-12 16:11:08 +01001485 }
Willy Tarreauce323de2012-08-20 21:41:06 +02001486
1487 /* if we read a large block smaller than what we requested,
1488 * it's almost certain we'll never get anything more.
1489 */
Willy Tarreauf26c26c2018-11-12 16:11:08 +01001490 if (ret >= global.tune.recv_enough) {
1491 /* we're stopped by the channel's policy */
Willy Tarreaub26a6f92018-11-14 17:10:36 +01001492 si_rx_chan_blk(si);
Willy Tarreauce323de2012-08-20 21:41:06 +02001493 break;
Willy Tarreauf26c26c2018-11-12 16:11:08 +01001494 }
Willy Tarreauce323de2012-08-20 21:41:06 +02001495 }
Christopher Fauletb3e0de42018-10-11 13:54:13 +02001496
1497 /* if we are waiting for more space, don't try to read more data
1498 * right now.
1499 */
Willy Tarreaub26a6f92018-11-14 17:10:36 +01001500 if (si_rx_blocked(si))
Christopher Fauletb3e0de42018-10-11 13:54:13 +02001501 break;
Willy Tarreauce323de2012-08-20 21:41:06 +02001502 } /* while !flags */
1503
Willy Tarreauffb12052018-11-15 16:06:02 +01001504 done_recv:
Willy Tarreauc5890e62014-02-09 17:47:01 +01001505 if (cur_read) {
Willy Tarreauafc8a222014-11-28 15:46:27 +01001506 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) &&
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001507 (cur_read <= ic->buf.size / 2)) {
Willy Tarreauafc8a222014-11-28 15:46:27 +01001508 ic->xfer_large = 0;
1509 ic->xfer_small++;
1510 if (ic->xfer_small >= 3) {
Willy Tarreauc5890e62014-02-09 17:47:01 +01001511 /* we have read less than half of the buffer in
1512 * one pass, and this happened at least 3 times.
1513 * This is definitely not a streamer.
1514 */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001515 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
Willy Tarreauc5890e62014-02-09 17:47:01 +01001516 }
Willy Tarreauafc8a222014-11-28 15:46:27 +01001517 else if (ic->xfer_small >= 2) {
Willy Tarreauc5890e62014-02-09 17:47:01 +01001518 /* if the buffer has been at least half full twice,
1519 * we receive faster than we send, so at least it
1520 * is not a "fast streamer".
1521 */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001522 ic->flags &= ~CF_STREAMER_FAST;
Willy Tarreauc5890e62014-02-09 17:47:01 +01001523 }
1524 }
Willy Tarreauafc8a222014-11-28 15:46:27 +01001525 else if (!(ic->flags & CF_STREAMER_FAST) &&
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001526 (cur_read >= ic->buf.size - global.tune.maxrewrite)) {
Willy Tarreauc5890e62014-02-09 17:47:01 +01001527 /* we read a full buffer at once */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001528 ic->xfer_small = 0;
1529 ic->xfer_large++;
1530 if (ic->xfer_large >= 3) {
Willy Tarreauc5890e62014-02-09 17:47:01 +01001531 /* we call this buffer a fast streamer if it manages
1532 * to be filled in one call 3 consecutive times.
1533 */
Willy Tarreauafc8a222014-11-28 15:46:27 +01001534 ic->flags |= (CF_STREAMER | CF_STREAMER_FAST);
Willy Tarreauc5890e62014-02-09 17:47:01 +01001535 }
1536 }
1537 else {
Willy Tarreauafc8a222014-11-28 15:46:27 +01001538 ic->xfer_small = 0;
1539 ic->xfer_large = 0;
Willy Tarreauc5890e62014-02-09 17:47:01 +01001540 }
Willy Tarreauafc8a222014-11-28 15:46:27 +01001541 ic->last_read = now_ms;
Willy Tarreauc5890e62014-02-09 17:47:01 +01001542 }
1543
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001544 end_recv:
Christopher Faulete6d8cb12019-11-20 16:42:00 +01001545 ret = (cur_read != 0);
1546
Christopher Faulet36b536d2019-11-20 11:56:33 +01001547 /* Report EOI on the channel if it was reached from the mux point of
1548 * view. */
Christopher Fauletb041b232022-03-24 10:27:02 +01001549 if ((cs->endp->flags & CS_EP_EOI) && !(ic->flags & CF_EOI)) {
Christopher Faulet36b536d2019-11-20 11:56:33 +01001550 ic->flags |= (CF_EOI|CF_READ_PARTIAL);
Christopher Faulete6d8cb12019-11-20 16:42:00 +01001551 ret = 1;
1552 }
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001553
Christopher Faulet6cd56d52022-03-30 10:47:32 +02001554 if (cs->endp->flags & CS_EP_ERROR)
Christopher Faulete6d8cb12019-11-20 16:42:00 +01001555 ret = 1;
Christopher Fauletb041b232022-03-24 10:27:02 +01001556 else if (cs->endp->flags & CS_EP_EOS) {
Willy Tarreau18955db2020-01-23 16:32:24 +01001557 /* we received a shutdown */
1558 ic->flags |= CF_READ_NULL;
1559 if (ic->flags & CF_AUTO_CLOSE)
1560 channel_shutw_now(ic);
1561 stream_int_read0(si);
Christopher Faulete6d8cb12019-11-20 16:42:00 +01001562 ret = 1;
Christopher Faulet36b536d2019-11-20 11:56:33 +01001563 }
1564 else if (!si_rx_blocked(si)) {
1565 /* Subscribe to receive events if we're blocking on I/O */
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001566 conn->mux->subscribe(cs, SUB_RETRY_RECV, &si->wait_event);
Willy Tarreaudd5621a2018-11-15 16:55:14 +01001567 si_rx_endp_done(si);
1568 } else {
1569 si_rx_endp_more(si);
Christopher Faulete6d8cb12019-11-20 16:42:00 +01001570 ret = 1;
Willy Tarreaudd5621a2018-11-15 16:55:14 +01001571 }
Christopher Faulete6d8cb12019-11-20 16:42:00 +01001572 return ret;
Willy Tarreauce323de2012-08-20 21:41:06 +02001573}
1574
1575/*
Willy Tarreau9bf9c142012-08-20 15:38:41 +02001576 * This function propagates a null read received on a socket-based connection.
Christopher Faulet8abe7122022-03-30 15:10:18 +02001577 * It updates the stream interface. If the stream interface has CS_FL_NOHALF,
Willy Tarreau11405122015-03-12 22:32:27 +01001578 * the close is also forwarded to the write side as an abort.
Willy Tarreau9bf9c142012-08-20 15:38:41 +02001579 */
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001580static void stream_int_read0(struct stream_interface *si)
Willy Tarreau9bf9c142012-08-20 15:38:41 +02001581{
Christopher Faulet13a35e52021-12-20 15:34:16 +01001582 struct conn_stream *cs = si->cs;
Willy Tarreauafc8a222014-11-28 15:46:27 +01001583 struct channel *ic = si_ic(si);
1584 struct channel *oc = si_oc(si);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001585
Christopher Faulet13a35e52021-12-20 15:34:16 +01001586 BUG_ON(!cs_conn(cs));
1587
Willy Tarreauabb5d422018-11-14 16:58:52 +01001588 si_rx_shut_blk(si);
Willy Tarreauafc8a222014-11-28 15:46:27 +01001589 if (ic->flags & CF_SHUTR)
Willy Tarreau9bf9c142012-08-20 15:38:41 +02001590 return;
Willy Tarreauafc8a222014-11-28 15:46:27 +01001591 ic->flags |= CF_SHUTR;
1592 ic->rex = TICK_ETERNITY;
Willy Tarreau9bf9c142012-08-20 15:38:41 +02001593
Willy Tarreau4f283fa2019-06-05 14:34:03 +02001594 if (!si_state_in(si->state, SI_SB_CON|SI_SB_RDY|SI_SB_EST))
Willy Tarreau9bf9c142012-08-20 15:38:41 +02001595 return;
1596
Willy Tarreauafc8a222014-11-28 15:46:27 +01001597 if (oc->flags & CF_SHUTW)
Willy Tarreau9bf9c142012-08-20 15:38:41 +02001598 goto do_close;
1599
Christopher Faulet8abe7122022-03-30 15:10:18 +02001600 if (cs->flags & CS_FL_NOHALF) {
Willy Tarreau9bf9c142012-08-20 15:38:41 +02001601 /* we want to immediately forward this close to the write side */
Willy Tarreau87b09662015-04-03 00:22:06 +02001602 /* force flag on ssl to keep stream in cache */
Willy Tarreauecdb3fe2017-10-05 15:25:48 +02001603 cs_shutw(cs, CS_SHW_SILENT);
Willy Tarreau9bf9c142012-08-20 15:38:41 +02001604 goto do_close;
1605 }
1606
1607 /* otherwise that's just a normal read shutdown */
Willy Tarreau9bf9c142012-08-20 15:38:41 +02001608 return;
1609
1610 do_close:
Willy Tarreauf9fbfe82012-11-21 21:51:53 +01001611 /* OK we completely close the socket here just as if we went through si_shut[rw]() */
Willy Tarreaua553ae92017-10-05 18:52:17 +02001612 cs_close(cs);
Willy Tarreauf9fbfe82012-11-21 21:51:53 +01001613
Willy Tarreauafc8a222014-11-28 15:46:27 +01001614 oc->flags &= ~CF_SHUTW_NOW;
1615 oc->flags |= CF_SHUTW;
1616 oc->wex = TICK_ETERNITY;
Willy Tarreauf9fbfe82012-11-21 21:51:53 +01001617
Willy Tarreau43e69dc2018-11-06 19:23:03 +01001618 si_done_get(si);
Willy Tarreauf9fbfe82012-11-21 21:51:53 +01001619
Christopher Faulet04400bc2019-10-25 10:21:01 +02001620 si->state = SI_ST_DIS;
Christopher Fauletae024ce2022-03-29 19:02:31 +02001621 __cs_strm(cs)->conn_exp = TICK_ETERNITY;
Willy Tarreau9bf9c142012-08-20 15:38:41 +02001622 return;
1623}
1624
Willy Tarreau651e1822015-09-23 20:06:13 +02001625/* Callback to be used by applet handlers upon completion. It updates the stream
1626 * (which may or may not take this opportunity to try to forward data), then
Emeric Brun2802b072017-06-30 14:11:56 +02001627 * may re-enable the applet's based on the channels and stream interface's final
Willy Tarreau651e1822015-09-23 20:06:13 +02001628 * states.
1629 */
Willy Tarreauaa977ba2015-09-25 11:45:06 +02001630void si_applet_wake_cb(struct stream_interface *si)
Willy Tarreaue5f86492015-04-19 15:16:35 +02001631{
Willy Tarreaueca572f2015-09-25 19:11:55 +02001632 struct channel *ic = si_ic(si);
1633
Christopher Faulet8bc17592022-02-28 17:27:09 +01001634 BUG_ON(!cs_appctx(si->cs));
Christopher Faulet693b23b2022-02-28 09:09:05 +01001635
Willy Tarreaueca572f2015-09-25 19:11:55 +02001636 /* If the applet wants to write and the channel is closed, it's a
1637 * broken pipe and it must be reported.
1638 */
Willy Tarreau05b9b642018-11-14 13:43:35 +01001639 if (!(si->flags & SI_FL_RX_WAIT_EP) && (ic->flags & CF_SHUTR))
Christopher Faulet6cd56d52022-03-30 10:47:32 +02001640 si->cs->endp->flags |= CS_EP_ERROR;
Willy Tarreaueca572f2015-09-25 19:11:55 +02001641
Willy Tarreau186dcdd2018-11-16 16:18:34 +01001642 /* automatically mark the applet having data available if it reported
1643 * begin blocked by the channel.
1644 */
1645 if (si_rx_blocked(si))
1646 si_rx_endp_more(si);
1647
Willy Tarreau651e1822015-09-23 20:06:13 +02001648 /* update the stream-int, channels, and possibly wake the stream up */
1649 stream_int_notify(si);
Willy Tarreaua64c7032019-08-01 14:17:02 +02001650 stream_release_buffers(si_strm(si));
Willy Tarreaue5f86492015-04-19 15:16:35 +02001651
Willy Tarreau32742fd2018-11-14 14:07:59 +01001652 /* stream_int_notify may have passed through chk_snd and released some
1653 * RXBLK flags. Process_stream will consider those flags to wake up the
1654 * appctx but in the case the task is not in runqueue we may have to
1655 * wakeup the appctx immediately.
Emeric Brun2802b072017-06-30 14:11:56 +02001656 */
Olivier Houchard51205a12019-04-17 19:29:35 +02001657 if ((si_rx_endp_ready(si) && !si_rx_blocked(si)) ||
1658 (si_tx_endp_ready(si) && !si_tx_blocked(si)))
Christopher Faulet693b23b2022-02-28 09:09:05 +01001659 appctx_wakeup(__cs_appctx(si->cs));
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001660}
1661
1662/*
1663 * This function performs a shutdown-read on a stream interface attached to an
1664 * applet in a connected or init state (it does nothing for other states). It
1665 * either shuts the read side or marks itself as closed. The buffer flags are
Christopher Faulet8abe7122022-03-30 15:10:18 +02001666 * updated to reflect the new state. If the stream interface has CS_FL_NOHALF,
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001667 * we also forward the close to the write side. The owner task is woken up if
1668 * it exists.
1669 */
1670static void stream_int_shutr_applet(struct stream_interface *si)
1671{
1672 struct channel *ic = si_ic(si);
1673
Christopher Faulet8bc17592022-02-28 17:27:09 +01001674 BUG_ON(!cs_appctx(si->cs));
Christopher Faulet693b23b2022-02-28 09:09:05 +01001675
Willy Tarreauabb5d422018-11-14 16:58:52 +01001676 si_rx_shut_blk(si);
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001677 if (ic->flags & CF_SHUTR)
1678 return;
1679 ic->flags |= CF_SHUTR;
1680 ic->rex = TICK_ETERNITY;
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001681
Willy Tarreau828824a2015-04-19 17:20:03 +02001682 /* Note: on shutr, we don't call the applet */
1683
Willy Tarreau4f283fa2019-06-05 14:34:03 +02001684 if (!si_state_in(si->state, SI_SB_CON|SI_SB_RDY|SI_SB_EST))
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001685 return;
1686
1687 if (si_oc(si)->flags & CF_SHUTW) {
Willy Tarreau958f0742015-09-25 20:24:26 +02001688 si_applet_release(si);
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001689 si->state = SI_ST_DIS;
Christopher Fauletae024ce2022-03-29 19:02:31 +02001690 __cs_strm(si->cs)->conn_exp = TICK_ETERNITY;
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001691 }
Christopher Faulet8abe7122022-03-30 15:10:18 +02001692 else if (si->cs->flags & CS_FL_NOHALF) {
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001693 /* we want to immediately forward this close to the write side */
1694 return stream_int_shutw_applet(si);
1695 }
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001696}
1697
1698/*
1699 * This function performs a shutdown-write on a stream interface attached to an
1700 * applet in a connected or init state (it does nothing for other states). It
1701 * either shuts the write side or marks itself as closed. The buffer flags are
1702 * updated to reflect the new state. It does also close everything if the SI
1703 * was marked as being in error state. The owner task is woken up if it exists.
1704 */
1705static void stream_int_shutw_applet(struct stream_interface *si)
1706{
1707 struct channel *ic = si_ic(si);
1708 struct channel *oc = si_oc(si);
1709
Christopher Faulet8bc17592022-02-28 17:27:09 +01001710 BUG_ON(!cs_appctx(si->cs));
Christopher Faulet693b23b2022-02-28 09:09:05 +01001711
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001712 oc->flags &= ~CF_SHUTW_NOW;
1713 if (oc->flags & CF_SHUTW)
1714 return;
1715 oc->flags |= CF_SHUTW;
1716 oc->wex = TICK_ETERNITY;
Willy Tarreau43e69dc2018-11-06 19:23:03 +01001717 si_done_get(si);
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001718
Christopher Faulet1d987772022-03-29 18:03:35 +02001719 if (tick_isset(si->cs->hcto)) {
1720 ic->rto = si->cs->hcto;
Hongbo Longe39683c2017-03-10 18:41:51 +01001721 ic->rex = tick_add(now_ms, ic->rto);
1722 }
1723
Willy Tarreau828824a2015-04-19 17:20:03 +02001724 /* on shutw we always wake the applet up */
Christopher Faulet693b23b2022-02-28 09:09:05 +01001725 appctx_wakeup(__cs_appctx(si->cs));
Willy Tarreau828824a2015-04-19 17:20:03 +02001726
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001727 switch (si->state) {
Willy Tarreau4f283fa2019-06-05 14:34:03 +02001728 case SI_ST_RDY:
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001729 case SI_ST_EST:
1730 /* we have to shut before closing, otherwise some short messages
1731 * may never leave the system, especially when there are remaining
1732 * unread data in the socket input buffer, or when nolinger is set.
Christopher Faulet8abe7122022-03-30 15:10:18 +02001733 * However, if CS_FL_NOLINGER is explicitly set, we know there is
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001734 * no risk so we close both sides immediately.
1735 */
Christopher Faulet8abe7122022-03-30 15:10:18 +02001736 if (!(si->cs->endp->flags & CS_EP_ERROR) && !(si->cs->flags & CS_FL_NOLINGER) &&
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001737 !(ic->flags & (CF_SHUTR|CF_DONT_READ)))
1738 return;
1739
1740 /* fall through */
1741 case SI_ST_CON:
1742 case SI_ST_CER:
1743 case SI_ST_QUE:
1744 case SI_ST_TAR:
1745 /* Note that none of these states may happen with applets */
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001746 si_applet_release(si);
Willy Tarreau958f0742015-09-25 20:24:26 +02001747 si->state = SI_ST_DIS;
Tim Duesterhus588b3142020-05-29 14:35:51 +02001748 /* fall through */
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001749 default:
Christopher Faulet8abe7122022-03-30 15:10:18 +02001750 si->cs->flags &= ~CS_FL_NOLINGER;
Willy Tarreauabb5d422018-11-14 16:58:52 +01001751 si_rx_shut_blk(si);
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001752 ic->flags |= CF_SHUTR;
1753 ic->rex = TICK_ETERNITY;
Christopher Fauletae024ce2022-03-29 19:02:31 +02001754 __cs_strm(si->cs)->conn_exp = TICK_ETERNITY;
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001755 }
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001756}
1757
1758/* chk_rcv function for applets */
1759static void stream_int_chk_rcv_applet(struct stream_interface *si)
1760{
1761 struct channel *ic = si_ic(si);
1762
Christopher Faulet8bc17592022-02-28 17:27:09 +01001763 BUG_ON(!cs_appctx(si->cs));
Christopher Faulet693b23b2022-02-28 09:09:05 +01001764
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001765 DPRINTF(stderr, "%s: si=%p, si->state=%d ic->flags=%08x oc->flags=%08x\n",
1766 __FUNCTION__,
1767 si, si->state, ic->flags, si_oc(si)->flags);
1768
Christopher Fauletb3e0de42018-10-11 13:54:13 +02001769 if (!ic->pipe) {
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001770 /* (re)start reading */
Christopher Faulet693b23b2022-02-28 09:09:05 +01001771 appctx_wakeup(__cs_appctx(si->cs));
Thierry FOURNIER5bc2cbf2015-09-04 18:40:36 +02001772 }
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001773}
1774
1775/* chk_snd function for applets */
1776static void stream_int_chk_snd_applet(struct stream_interface *si)
1777{
1778 struct channel *oc = si_oc(si);
1779
Christopher Faulet8bc17592022-02-28 17:27:09 +01001780 BUG_ON(!cs_appctx(si->cs));
Christopher Faulet693b23b2022-02-28 09:09:05 +01001781
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001782 DPRINTF(stderr, "%s: si=%p, si->state=%d ic->flags=%08x oc->flags=%08x\n",
1783 __FUNCTION__,
1784 si, si->state, si_ic(si)->flags, oc->flags);
1785
1786 if (unlikely(si->state != SI_ST_EST || (oc->flags & CF_SHUTW)))
1787 return;
1788
Willy Tarreau828824a2015-04-19 17:20:03 +02001789 /* we only wake the applet up if it was waiting for some data */
1790
1791 if (!(si->flags & SI_FL_WAIT_DATA))
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001792 return;
1793
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001794 if (!tick_isset(oc->wex))
1795 oc->wex = tick_add_ifset(now_ms, oc->wto);
1796
Willy Tarreau828824a2015-04-19 17:20:03 +02001797 if (!channel_is_empty(oc)) {
1798 /* (re)start sending */
Christopher Faulet693b23b2022-02-28 09:09:05 +01001799 appctx_wakeup(__cs_appctx(si->cs));
Willy Tarreau828824a2015-04-19 17:20:03 +02001800 }
Willy Tarreaud45b9f82015-04-13 16:30:14 +02001801}
1802
Willy Tarreaudded32d2008-11-30 19:48:07 +01001803/*
Willy Tarreaucff64112008-11-03 06:26:53 +01001804 * Local variables:
1805 * c-indent-level: 8
1806 * c-basic-offset: 8
1807 * End:
1808 */