Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Functions managing stream_interface structures |
| 3 | * |
Willy Tarreau | f873d75 | 2012-05-11 17:47:17 +0200 | [diff] [blame] | 4 | * Copyright 2000-2012 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <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 Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 22 | #include <haproxy/api.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 23 | #include <haproxy/applet.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 24 | #include <haproxy/channel.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 25 | #include <haproxy/connection.h> |
Willy Tarreau | 2741c8c | 2020-06-02 11:28:02 +0200 | [diff] [blame] | 26 | #include <haproxy/dynbuf.h> |
Willy Tarreau | b7fc4c4 | 2021-10-06 18:56:42 +0200 | [diff] [blame] | 27 | #include <haproxy/http_ana.h> |
Willy Tarreau | 8773533 | 2020-06-04 09:08:41 +0200 | [diff] [blame] | 28 | #include <haproxy/http_htx.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 29 | #include <haproxy/pipe-t.h> |
| 30 | #include <haproxy/pipe.h> |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 31 | #include <haproxy/pool.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 32 | #include <haproxy/proxy.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 33 | #include <haproxy/stream-t.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 34 | #include <haproxy/stream_interface.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 35 | #include <haproxy/task.h> |
Willy Tarreau | c2f7c58 | 2020-06-02 18:15:32 +0200 | [diff] [blame] | 36 | #include <haproxy/ticks.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 37 | #include <haproxy/tools.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 38 | |
Willy Tarreau | fd31e53 | 2012-07-23 18:24:25 +0200 | [diff] [blame] | 39 | |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 40 | DECLARE_POOL(pool_head_streaminterface, "stream_interface", sizeof(struct stream_interface)); |
| 41 | |
| 42 | |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 43 | /* functions used by default on a detached stream-interface */ |
Willy Tarreau | 6fe1541 | 2013-09-29 15:16:03 +0200 | [diff] [blame] | 44 | static void stream_int_shutr(struct stream_interface *si); |
| 45 | static void stream_int_shutw(struct stream_interface *si); |
Willy Tarreau | f873d75 | 2012-05-11 17:47:17 +0200 | [diff] [blame] | 46 | static void stream_int_chk_rcv(struct stream_interface *si); |
| 47 | static void stream_int_chk_snd(struct stream_interface *si); |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 48 | |
| 49 | /* functions used on a conn_stream-based stream-interface */ |
Willy Tarreau | 6fe1541 | 2013-09-29 15:16:03 +0200 | [diff] [blame] | 50 | static void stream_int_shutr_conn(struct stream_interface *si); |
| 51 | static void stream_int_shutw_conn(struct stream_interface *si); |
Willy Tarreau | c578891 | 2012-08-24 18:12:41 +0200 | [diff] [blame] | 52 | static void stream_int_chk_rcv_conn(struct stream_interface *si); |
| 53 | static void stream_int_chk_snd_conn(struct stream_interface *si); |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 54 | |
| 55 | /* functions used on an applet-based stream-interface */ |
Willy Tarreau | d45b9f8 | 2015-04-13 16:30:14 +0200 | [diff] [blame] | 56 | static void stream_int_shutr_applet(struct stream_interface *si); |
| 57 | static void stream_int_shutw_applet(struct stream_interface *si); |
| 58 | static void stream_int_chk_rcv_applet(struct stream_interface *si); |
| 59 | static void stream_int_chk_snd_applet(struct stream_interface *si); |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 60 | |
| 61 | /* last read notification */ |
| 62 | static void stream_int_read0(struct stream_interface *si); |
| 63 | |
| 64 | /* post-IO notification callback */ |
| 65 | static void stream_int_notify(struct stream_interface *si); |
Willy Tarreau | f873d75 | 2012-05-11 17:47:17 +0200 | [diff] [blame] | 66 | |
Willy Tarreau | c578891 | 2012-08-24 18:12:41 +0200 | [diff] [blame] | 67 | /* stream-interface operations for embedded tasks */ |
| 68 | struct si_ops si_embedded_ops = { |
Willy Tarreau | 5c979a9 | 2012-05-07 17:15:39 +0200 | [diff] [blame] | 69 | .chk_rcv = stream_int_chk_rcv, |
| 70 | .chk_snd = stream_int_chk_snd, |
Willy Tarreau | 8b3d7df | 2013-09-29 14:51:58 +0200 | [diff] [blame] | 71 | .shutr = stream_int_shutr, |
| 72 | .shutw = stream_int_shutw, |
Willy Tarreau | 5c979a9 | 2012-05-07 17:15:39 +0200 | [diff] [blame] | 73 | }; |
| 74 | |
Willy Tarreau | c578891 | 2012-08-24 18:12:41 +0200 | [diff] [blame] | 75 | /* stream-interface operations for connections */ |
| 76 | struct si_ops si_conn_ops = { |
Willy Tarreau | c578891 | 2012-08-24 18:12:41 +0200 | [diff] [blame] | 77 | .chk_rcv = stream_int_chk_rcv_conn, |
| 78 | .chk_snd = stream_int_chk_snd_conn, |
Willy Tarreau | 8b3d7df | 2013-09-29 14:51:58 +0200 | [diff] [blame] | 79 | .shutr = stream_int_shutr_conn, |
| 80 | .shutw = stream_int_shutw_conn, |
Willy Tarreau | c578891 | 2012-08-24 18:12:41 +0200 | [diff] [blame] | 81 | }; |
| 82 | |
Willy Tarreau | d45b9f8 | 2015-04-13 16:30:14 +0200 | [diff] [blame] | 83 | /* stream-interface operations for connections */ |
| 84 | struct si_ops si_applet_ops = { |
Willy Tarreau | d45b9f8 | 2015-04-13 16:30:14 +0200 | [diff] [blame] | 85 | .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 Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 91 | |
| 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 Faulet | 9936dc6 | 2022-02-28 09:21:58 +0100 | [diff] [blame] | 95 | static int si_cs_recv(struct conn_stream *cs); |
Christopher Faulet | 4941623 | 2022-02-28 09:14:46 +0100 | [diff] [blame] | 96 | static int si_cs_send(struct conn_stream *cs); |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 97 | static int si_cs_process(struct conn_stream *cs); |
| 98 | |
| 99 | |
Willy Tarreau | 74beec3 | 2012-10-03 00:41:04 +0200 | [diff] [blame] | 100 | struct data_cb si_conn_cb = { |
Olivier Houchard | 21df6cc | 2018-09-14 23:21:44 +0200 | [diff] [blame] | 101 | .wake = si_cs_process, |
Willy Tarreau | 8e0bb0a | 2016-11-24 16:58:12 +0100 | [diff] [blame] | 102 | .name = "STRM", |
Willy Tarreau | c578891 | 2012-08-24 18:12:41 +0200 | [diff] [blame] | 103 | }; |
| 104 | |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 105 | |
| 106 | struct 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 Faulet | 014ac35 | 2022-01-06 08:13:46 +0100 | [diff] [blame] | 114 | if (si_init(si) < 0) { |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 115 | pool_free(pool_head_streaminterface, si); |
| 116 | return NULL; |
| 117 | } |
| 118 | si->cs = cs; |
| 119 | return si; |
| 120 | } |
| 121 | |
| 122 | void si_free(struct stream_interface *si) |
| 123 | { |
| 124 | if (!si) |
| 125 | return; |
| 126 | |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 127 | pool_free(pool_head_streaminterface, si); |
| 128 | } |
| 129 | |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 130 | /* |
Willy Tarreau | d45b9f8 | 2015-04-13 16:30:14 +0200 | [diff] [blame] | 131 | * This function performs a shutdown-read on a detached stream interface in a |
| 132 | * connected or init state (it does nothing for other states). It either shuts |
| 133 | * the read side or marks itself as closed. The buffer flags are updated to |
Christopher Faulet | 8abe712 | 2022-03-30 15:10:18 +0200 | [diff] [blame] | 134 | * reflect the new state. If the stream interface has CS_FL_NOHALF, we also |
Willy Tarreau | d45b9f8 | 2015-04-13 16:30:14 +0200 | [diff] [blame] | 135 | * forward the close to the write side. The owner task is woken up if it exists. |
Willy Tarreau | 4a36b56 | 2012-08-06 19:31:45 +0200 | [diff] [blame] | 136 | */ |
Willy Tarreau | 6fe1541 | 2013-09-29 15:16:03 +0200 | [diff] [blame] | 137 | static void stream_int_shutr(struct stream_interface *si) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 138 | { |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 139 | struct channel *ic = si_ic(si); |
| 140 | |
Willy Tarreau | abb5d42 | 2018-11-14 16:58:52 +0100 | [diff] [blame] | 141 | si_rx_shut_blk(si); |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 142 | if (ic->flags & CF_SHUTR) |
Willy Tarreau | 6fe1541 | 2013-09-29 15:16:03 +0200 | [diff] [blame] | 143 | return; |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 144 | ic->flags |= CF_SHUTR; |
| 145 | ic->rex = TICK_ETERNITY; |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 146 | |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 147 | if (!cs_state_in(si->cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST)) |
Willy Tarreau | 6fe1541 | 2013-09-29 15:16:03 +0200 | [diff] [blame] | 148 | return; |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 149 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 150 | if (si_oc(si)->flags & CF_SHUTW) { |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 151 | si->cs->state = CS_ST_DIS; |
Christopher Faulet | ae024ce | 2022-03-29 19:02:31 +0200 | [diff] [blame] | 152 | __cs_strm(si->cs)->conn_exp = TICK_ETERNITY; |
Willy Tarreau | d8ccffe | 2010-09-07 16:16:50 +0200 | [diff] [blame] | 153 | } |
Christopher Faulet | 8abe712 | 2022-03-30 15:10:18 +0200 | [diff] [blame] | 154 | else if (si->cs->flags & CS_FL_NOHALF) { |
Willy Tarreau | 4a36b56 | 2012-08-06 19:31:45 +0200 | [diff] [blame] | 155 | /* we want to immediately forward this close to the write side */ |
| 156 | return stream_int_shutw(si); |
| 157 | } |
Willy Tarreau | 0bd05ea | 2010-07-02 11:18:03 +0200 | [diff] [blame] | 158 | |
Willy Tarreau | 4a36b56 | 2012-08-06 19:31:45 +0200 | [diff] [blame] | 159 | /* note that if the task exists, it must unregister itself once it runs */ |
Christopher Faulet | 974da9f | 2022-03-30 15:30:03 +0200 | [diff] [blame] | 160 | if (!(si->cs->flags & CS_FL_DONT_WAKE)) |
Willy Tarreau | 07373b8 | 2014-11-28 12:08:47 +0100 | [diff] [blame] | 161 | task_wakeup(si_task(si), TASK_WOKEN_IO); |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 162 | } |
| 163 | |
Willy Tarreau | 4a36b56 | 2012-08-06 19:31:45 +0200 | [diff] [blame] | 164 | /* |
Willy Tarreau | d45b9f8 | 2015-04-13 16:30:14 +0200 | [diff] [blame] | 165 | * This function performs a shutdown-write on a detached stream interface in a |
| 166 | * connected or init state (it does nothing for other states). It either shuts |
| 167 | * the write side or marks itself as closed. The buffer flags are updated to |
| 168 | * reflect the new state. It does also close everything if the SI was marked as |
| 169 | * being in error state. The owner task is woken up if it exists. |
Willy Tarreau | 4a36b56 | 2012-08-06 19:31:45 +0200 | [diff] [blame] | 170 | */ |
Willy Tarreau | 6fe1541 | 2013-09-29 15:16:03 +0200 | [diff] [blame] | 171 | static void stream_int_shutw(struct stream_interface *si) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 172 | { |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 173 | struct channel *ic = si_ic(si); |
| 174 | struct channel *oc = si_oc(si); |
| 175 | |
| 176 | oc->flags &= ~CF_SHUTW_NOW; |
| 177 | if (oc->flags & CF_SHUTW) |
Willy Tarreau | 6fe1541 | 2013-09-29 15:16:03 +0200 | [diff] [blame] | 178 | return; |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 179 | oc->flags |= CF_SHUTW; |
| 180 | oc->wex = TICK_ETERNITY; |
Willy Tarreau | 43e69dc | 2018-11-06 19:23:03 +0100 | [diff] [blame] | 181 | si_done_get(si); |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 182 | |
Christopher Faulet | 1d98777 | 2022-03-29 18:03:35 +0200 | [diff] [blame] | 183 | if (tick_isset(si->cs->hcto)) { |
| 184 | ic->rto = si->cs->hcto; |
Hongbo Long | e39683c | 2017-03-10 18:41:51 +0100 | [diff] [blame] | 185 | ic->rex = tick_add(now_ms, ic->rto); |
| 186 | } |
| 187 | |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 188 | switch (si->cs->state) { |
| 189 | case CS_ST_RDY: |
| 190 | case CS_ST_EST: |
Willy Tarreau | 4a36b56 | 2012-08-06 19:31:45 +0200 | [diff] [blame] | 191 | /* we have to shut before closing, otherwise some short messages |
| 192 | * may never leave the system, especially when there are remaining |
| 193 | * unread data in the socket input buffer, or when nolinger is set. |
Christopher Faulet | 8abe712 | 2022-03-30 15:10:18 +0200 | [diff] [blame] | 194 | * However, if CS_FL_NOLINGER is explicitly set, we know there is |
Willy Tarreau | 4a36b56 | 2012-08-06 19:31:45 +0200 | [diff] [blame] | 195 | * no risk so we close both sides immediately. |
| 196 | */ |
Christopher Faulet | 8abe712 | 2022-03-30 15:10:18 +0200 | [diff] [blame] | 197 | if (!(si->cs->endp->flags & CS_EP_ERROR) && !(si->cs->flags & CS_FL_NOLINGER) && |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 198 | !(ic->flags & (CF_SHUTR|CF_DONT_READ))) |
Willy Tarreau | 6fe1541 | 2013-09-29 15:16:03 +0200 | [diff] [blame] | 199 | return; |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 200 | |
| 201 | /* fall through */ |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 202 | case CS_ST_CON: |
| 203 | case CS_ST_CER: |
| 204 | case CS_ST_QUE: |
| 205 | case CS_ST_TAR: |
Willy Tarreau | 8b3d7df | 2013-09-29 14:51:58 +0200 | [diff] [blame] | 206 | /* Note that none of these states may happen with applets */ |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 207 | si->cs->state = CS_ST_DIS; |
Tim Duesterhus | 588b314 | 2020-05-29 14:35:51 +0200 | [diff] [blame] | 208 | /* fall through */ |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 209 | default: |
Christopher Faulet | 8abe712 | 2022-03-30 15:10:18 +0200 | [diff] [blame] | 210 | si->cs->flags &= ~CS_FL_NOLINGER; |
Willy Tarreau | abb5d42 | 2018-11-14 16:58:52 +0100 | [diff] [blame] | 211 | si_rx_shut_blk(si); |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 212 | ic->flags |= CF_SHUTR; |
| 213 | ic->rex = TICK_ETERNITY; |
Christopher Faulet | ae024ce | 2022-03-29 19:02:31 +0200 | [diff] [blame] | 214 | __cs_strm(si->cs)->conn_exp = TICK_ETERNITY; |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 215 | } |
| 216 | |
Willy Tarreau | 4a36b56 | 2012-08-06 19:31:45 +0200 | [diff] [blame] | 217 | /* note that if the task exists, it must unregister itself once it runs */ |
Christopher Faulet | 974da9f | 2022-03-30 15:30:03 +0200 | [diff] [blame] | 218 | if (!(si->cs->flags & CS_FL_DONT_WAKE)) |
Willy Tarreau | 07373b8 | 2014-11-28 12:08:47 +0100 | [diff] [blame] | 219 | task_wakeup(si_task(si), TASK_WOKEN_IO); |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /* default chk_rcv function for scheduled tasks */ |
Willy Tarreau | f873d75 | 2012-05-11 17:47:17 +0200 | [diff] [blame] | 223 | static void stream_int_chk_rcv(struct stream_interface *si) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 224 | { |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 225 | struct channel *ic = si_ic(si); |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 226 | |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 227 | DPRINTF(stderr, "%s: si=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n", |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 228 | __FUNCTION__, |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 229 | si, si->cs->state, ic->flags, si_oc(si)->flags); |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 230 | |
Christopher Faulet | b3e0de4 | 2018-10-11 13:54:13 +0200 | [diff] [blame] | 231 | if (ic->pipe) { |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 232 | /* stop reading */ |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 233 | si_rx_room_blk(si); |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 234 | } |
| 235 | else { |
| 236 | /* (re)start reading */ |
Christopher Faulet | 974da9f | 2022-03-30 15:30:03 +0200 | [diff] [blame] | 237 | if (!(si->cs->flags & CS_FL_DONT_WAKE)) |
Willy Tarreau | 07373b8 | 2014-11-28 12:08:47 +0100 | [diff] [blame] | 238 | task_wakeup(si_task(si), TASK_WOKEN_IO); |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
| 242 | /* default chk_snd function for scheduled tasks */ |
Willy Tarreau | f873d75 | 2012-05-11 17:47:17 +0200 | [diff] [blame] | 243 | static void stream_int_chk_snd(struct stream_interface *si) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 244 | { |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 245 | struct channel *oc = si_oc(si); |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 246 | |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 247 | DPRINTF(stderr, "%s: si=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n", |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 248 | __FUNCTION__, |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 249 | si, si->cs->state, si_ic(si)->flags, oc->flags); |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 250 | |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 251 | if (unlikely(si->cs->state != CS_ST_EST || (oc->flags & CF_SHUTW))) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 252 | return; |
| 253 | |
| 254 | if (!(si->flags & SI_FL_WAIT_DATA) || /* not waiting for data */ |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 255 | channel_is_empty(oc)) /* called with nothing to send ! */ |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 256 | return; |
| 257 | |
| 258 | /* Otherwise there are remaining data to be sent in the buffer, |
| 259 | * so we tell the handler. |
| 260 | */ |
| 261 | si->flags &= ~SI_FL_WAIT_DATA; |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 262 | if (!tick_isset(oc->wex)) |
| 263 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 264 | |
Christopher Faulet | 974da9f | 2022-03-30 15:30:03 +0200 | [diff] [blame] | 265 | if (!(si->cs->flags & CS_FL_DONT_WAKE)) |
Willy Tarreau | 07373b8 | 2014-11-28 12:08:47 +0100 | [diff] [blame] | 266 | task_wakeup(si_task(si), TASK_WOKEN_IO); |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 267 | } |
| 268 | |
Willy Tarreau | a9ff5e6 | 2015-07-19 18:46:30 +0200 | [diff] [blame] | 269 | /* Register an applet to handle a stream_interface as a new appctx. The SI will |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 270 | * wake it up every time it is solicited. The appctx must be deleted by the task |
Willy Tarreau | a9ff5e6 | 2015-07-19 18:46:30 +0200 | [diff] [blame] | 271 | * handler using si_release_endpoint(), possibly from within the function itself. |
| 272 | * It also pre-initializes the applet's context and returns it (or NULL in case |
| 273 | * it could not be allocated). |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 274 | */ |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 275 | struct appctx *si_register_handler(struct stream_interface *si, struct applet *app) |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 276 | { |
Willy Tarreau | 0a23bcb | 2013-12-01 11:31:38 +0100 | [diff] [blame] | 277 | struct appctx *appctx; |
| 278 | |
Willy Tarreau | 07373b8 | 2014-11-28 12:08:47 +0100 | [diff] [blame] | 279 | DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", app, si, si_task(si)); |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 280 | |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 281 | appctx = appctx_new(app, si->cs->endp); |
Willy Tarreau | a69fc9f | 2014-12-22 19:34:00 +0100 | [diff] [blame] | 282 | if (!appctx) |
Willy Tarreau | 0a23bcb | 2013-12-01 11:31:38 +0100 | [diff] [blame] | 283 | return NULL; |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 284 | cs_attach_applet(si->cs, appctx, appctx); |
| 285 | appctx->owner = si->cs; |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 286 | appctx->t->nice = si_strm(si)->task->nice; |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 287 | si_cant_get(si); |
Willy Tarreau | 828824a | 2015-04-19 17:20:03 +0200 | [diff] [blame] | 288 | appctx_wakeup(appctx); |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 289 | return appctx; |
Willy Tarreau | fb90d94 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 290 | } |
| 291 | |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 292 | /* This callback is used to send a valid PROXY protocol line to a socket being |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 293 | * established. It returns 0 if it fails in a fatal way or needs to poll to go |
| 294 | * further, otherwise it returns non-zero and removes itself from the connection's |
Willy Tarreau | a1a7474 | 2012-08-24 12:14:49 +0200 | [diff] [blame] | 295 | * flags (the bit is provided in <flag> by the caller). It is designed to be |
| 296 | * called by the connection handler and relies on it to commit polling changes. |
Willy Tarreau | 57cd3e4 | 2013-10-24 22:01:26 +0200 | [diff] [blame] | 297 | * Note that it can emit a PROXY line by relying on the other end's address |
| 298 | * when the connection is attached to a stream interface, or by resolving the |
| 299 | * local address otherwise (also called a LOCAL line). |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 300 | */ |
| 301 | int conn_si_send_proxy(struct connection *conn, unsigned int flag) |
| 302 | { |
Willy Tarreau | d02cdd2 | 2013-12-15 10:23:20 +0100 | [diff] [blame] | 303 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 304 | goto out_error; |
| 305 | |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 306 | /* If we have a PROXY line to send, we'll use this to validate the |
| 307 | * connection, in which case the connection is validated only once |
| 308 | * we've sent the whole proxy line. Otherwise we use connect(). |
| 309 | */ |
Tim Duesterhus | 36839dc | 2019-02-26 17:09:51 +0100 | [diff] [blame] | 310 | if (conn->send_proxy_ofs) { |
Willy Tarreau | 6b1379f | 2018-11-18 21:38:19 +0100 | [diff] [blame] | 311 | const struct conn_stream *cs; |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 312 | int ret; |
| 313 | |
Christopher Faulet | d82056c | 2020-05-26 16:08:49 +0200 | [diff] [blame] | 314 | /* If there is no mux attached to the connection, it means the |
| 315 | * connection context is a conn-stream. |
| 316 | */ |
Christopher Faulet | 69ef6c9 | 2022-03-31 14:20:00 +0200 | [diff] [blame] | 317 | cs = (conn->mux ? cs_conn_get_first(conn) : conn->ctx); |
Christopher Faulet | d82056c | 2020-05-26 16:08:49 +0200 | [diff] [blame] | 318 | |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 319 | /* The target server expects a PROXY line to be sent first. |
| 320 | * If the send_proxy_ofs is negative, it corresponds to the |
| 321 | * offset to start sending from then end of the proxy string |
| 322 | * (which is recomputed every time since it's constant). If |
| 323 | * it is positive, it means we have to send from the start. |
Willy Tarreau | 57cd3e4 | 2013-10-24 22:01:26 +0200 | [diff] [blame] | 324 | * We can only send a "normal" PROXY line when the connection |
| 325 | * is attached to a stream interface. Otherwise we can only |
| 326 | * send a LOCAL line (eg: for use with health checks). |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 327 | */ |
Willy Tarreau | 6b1379f | 2018-11-18 21:38:19 +0100 | [diff] [blame] | 328 | |
| 329 | if (cs && cs->data_cb == &si_conn_cb) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 330 | ret = make_proxy_line(trash.area, trash.size, |
| 331 | objt_server(conn->target), |
Christopher Faulet | f835dea | 2021-12-21 14:35:17 +0100 | [diff] [blame] | 332 | cs_conn(si_opposite(cs_si(cs))->cs), |
Christopher Faulet | 693b23b | 2022-02-28 09:09:05 +0100 | [diff] [blame] | 333 | cs_strm(cs)); |
Willy Tarreau | 57cd3e4 | 2013-10-24 22:01:26 +0200 | [diff] [blame] | 334 | } |
| 335 | else { |
| 336 | /* The target server expects a LOCAL line to be sent first. Retrieving |
| 337 | * local or remote addresses may fail until the connection is established. |
| 338 | */ |
Willy Tarreau | 7bb447c | 2019-07-17 11:40:51 +0200 | [diff] [blame] | 339 | if (!conn_get_src(conn) || !conn_get_dst(conn)) |
Willy Tarreau | 57cd3e4 | 2013-10-24 22:01:26 +0200 | [diff] [blame] | 340 | goto out_wait; |
| 341 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 342 | ret = make_proxy_line(trash.area, trash.size, |
Tim Duesterhus | cf6e0c8 | 2020-03-13 12:34:24 +0100 | [diff] [blame] | 343 | objt_server(conn->target), conn, |
| 344 | NULL); |
Willy Tarreau | 57cd3e4 | 2013-10-24 22:01:26 +0200 | [diff] [blame] | 345 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 346 | |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 347 | if (!ret) |
| 348 | goto out_error; |
| 349 | |
Willy Tarreau | b8020ce | 2013-10-24 21:10:08 +0200 | [diff] [blame] | 350 | if (conn->send_proxy_ofs > 0) |
| 351 | conn->send_proxy_ofs = -ret; /* first call */ |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 352 | |
Willy Tarreau | a1a7474 | 2012-08-24 12:14:49 +0200 | [diff] [blame] | 353 | /* we have to send trash from (ret+sp for -sp bytes). If the |
| 354 | * data layer has a pending write, we'll also set MSG_MORE. |
| 355 | */ |
Willy Tarreau | 827fee7 | 2020-12-11 15:26:55 +0100 | [diff] [blame] | 356 | ret = conn_ctrl_send(conn, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 357 | trash.area + ret + conn->send_proxy_ofs, |
| 358 | -conn->send_proxy_ofs, |
Willy Tarreau | 827fee7 | 2020-12-11 15:26:55 +0100 | [diff] [blame] | 359 | (conn->subs && conn->subs->events & SUB_RETRY_SEND) ? CO_SFL_MSG_MORE : 0); |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 360 | |
Willy Tarreau | 0a03c0f | 2015-03-13 00:05:28 +0100 | [diff] [blame] | 361 | if (ret < 0) |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 362 | goto out_error; |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 363 | |
Willy Tarreau | b8020ce | 2013-10-24 21:10:08 +0200 | [diff] [blame] | 364 | conn->send_proxy_ofs += ret; /* becomes zero once complete */ |
| 365 | if (conn->send_proxy_ofs != 0) |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 366 | goto out_wait; |
| 367 | |
| 368 | /* OK we've sent the whole line, we're connected */ |
| 369 | } |
| 370 | |
Willy Tarreau | a1a7474 | 2012-08-24 12:14:49 +0200 | [diff] [blame] | 371 | /* The connection is ready now, simply return and let the connection |
| 372 | * handler notify upper layers if needed. |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 373 | */ |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 374 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 375 | conn->flags &= ~flag; |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 376 | return 1; |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 377 | |
| 378 | out_error: |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 379 | /* Write error on the file descriptor */ |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 380 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 381 | return 0; |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 382 | |
| 383 | out_wait: |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 384 | return 0; |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 385 | } |
| 386 | |
Willy Tarreau | 2737562 | 2013-12-17 00:00:28 +0100 | [diff] [blame] | 387 | |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 388 | /* This function is the equivalent to si_update() except that it's |
Willy Tarreau | 615f28b | 2015-09-23 18:40:09 +0200 | [diff] [blame] | 389 | * designed to be called from outside the stream handlers, typically the lower |
| 390 | * layers (applets, connections) after I/O completion. After updating the stream |
| 391 | * interface and timeouts, it will try to forward what can be forwarded, then to |
| 392 | * wake the associated task up if an important event requires special handling. |
Willy Tarreau | d0f5bbc | 2018-11-14 11:10:26 +0100 | [diff] [blame] | 393 | * It may update SI_FL_WAIT_DATA and/or SI_FL_RXBLK_ROOM, that the callers are |
Willy Tarreau | 0dfccb2 | 2018-10-25 13:55:20 +0200 | [diff] [blame] | 394 | * encouraged to watch to take appropriate action. |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 395 | * It should not be called from within the stream itself, si_update() |
Willy Tarreau | 615f28b | 2015-09-23 18:40:09 +0200 | [diff] [blame] | 396 | * is designed for this. |
| 397 | */ |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 398 | static void stream_int_notify(struct stream_interface *si) |
Willy Tarreau | 615f28b | 2015-09-23 18:40:09 +0200 | [diff] [blame] | 399 | { |
| 400 | struct channel *ic = si_ic(si); |
| 401 | struct channel *oc = si_oc(si); |
Willy Tarreau | 47baeb8 | 2018-11-15 07:46:57 +0100 | [diff] [blame] | 402 | struct stream_interface *sio = si_opposite(si); |
Christopher Faulet | d7607de | 2019-01-03 16:24:54 +0100 | [diff] [blame] | 403 | struct task *task = si_task(si); |
Willy Tarreau | 615f28b | 2015-09-23 18:40:09 +0200 | [diff] [blame] | 404 | |
| 405 | /* process consumer side */ |
| 406 | if (channel_is_empty(oc)) { |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 407 | struct connection *conn = cs_conn(si->cs); |
Olivier Houchard | e9bed53 | 2017-11-16 17:49:25 +0100 | [diff] [blame] | 408 | |
Willy Tarreau | 615f28b | 2015-09-23 18:40:09 +0200 | [diff] [blame] | 409 | if (((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW) && |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 410 | (si->cs->state == CS_ST_EST) && (!conn || !(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS)))) |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame^] | 411 | cs_shutw(si->cs); |
Willy Tarreau | 615f28b | 2015-09-23 18:40:09 +0200 | [diff] [blame] | 412 | oc->wex = TICK_ETERNITY; |
| 413 | } |
| 414 | |
Willy Tarreau | 8cf9c8e | 2016-12-13 15:21:25 +0100 | [diff] [blame] | 415 | /* indicate that we may be waiting for data from the output channel or |
| 416 | * we're about to close and can't expect more data if SHUTW_NOW is there. |
| 417 | */ |
Christopher Faulet | b3e0de4 | 2018-10-11 13:54:13 +0200 | [diff] [blame] | 418 | if (!(oc->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
Willy Tarreau | 615f28b | 2015-09-23 18:40:09 +0200 | [diff] [blame] | 419 | si->flags |= SI_FL_WAIT_DATA; |
Willy Tarreau | 8cf9c8e | 2016-12-13 15:21:25 +0100 | [diff] [blame] | 420 | else if ((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW) |
| 421 | si->flags &= ~SI_FL_WAIT_DATA; |
Willy Tarreau | 615f28b | 2015-09-23 18:40:09 +0200 | [diff] [blame] | 422 | |
| 423 | /* update OC timeouts and wake the other side up if it's waiting for room */ |
| 424 | if (oc->flags & CF_WRITE_ACTIVITY) { |
| 425 | if ((oc->flags & (CF_SHUTW|CF_WRITE_PARTIAL)) == CF_WRITE_PARTIAL && |
| 426 | !channel_is_empty(oc)) |
| 427 | if (tick_isset(oc->wex)) |
| 428 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
| 429 | |
Christopher Faulet | a728518 | 2022-03-30 15:43:23 +0200 | [diff] [blame] | 430 | if (!(si->cs->flags & CS_FL_INDEP_STR)) |
Willy Tarreau | 615f28b | 2015-09-23 18:40:09 +0200 | [diff] [blame] | 431 | if (tick_isset(ic->rex)) |
| 432 | ic->rex = tick_add_ifset(now_ms, ic->rto); |
Willy Tarreau | f26c26c | 2018-11-12 16:11:08 +0100 | [diff] [blame] | 433 | } |
Willy Tarreau | 615f28b | 2015-09-23 18:40:09 +0200 | [diff] [blame] | 434 | |
Willy Tarreau | b26a6f9 | 2018-11-14 17:10:36 +0100 | [diff] [blame] | 435 | if (oc->flags & CF_DONT_READ) |
| 436 | si_rx_chan_blk(sio); |
| 437 | else |
| 438 | si_rx_chan_rdy(sio); |
Willy Tarreau | 615f28b | 2015-09-23 18:40:09 +0200 | [diff] [blame] | 439 | |
| 440 | /* Notify the other side when we've injected data into the IC that |
| 441 | * needs to be forwarded. We can do fast-forwarding as soon as there |
| 442 | * are output data, but we avoid doing this if some of the data are |
| 443 | * not yet scheduled for being forwarded, because it is very likely |
| 444 | * that it will be done again immediately afterwards once the following |
Willy Tarreau | d0f5bbc | 2018-11-14 11:10:26 +0100 | [diff] [blame] | 445 | * data are parsed (eg: HTTP chunking). We only SI_FL_RXBLK_ROOM once |
Willy Tarreau | 615f28b | 2015-09-23 18:40:09 +0200 | [diff] [blame] | 446 | * we've emptied *some* of the output buffer, and not just when there |
| 447 | * is available room, because applets are often forced to stop before |
| 448 | * the buffer is full. We must not stop based on input data alone because |
| 449 | * an HTTP parser might need more data to complete the parsing. |
| 450 | */ |
| 451 | if (!channel_is_empty(ic) && |
Willy Tarreau | 47baeb8 | 2018-11-15 07:46:57 +0100 | [diff] [blame] | 452 | (sio->flags & SI_FL_WAIT_DATA) && |
Willy Tarreau | 89b6a2b | 2018-11-18 15:46:10 +0100 | [diff] [blame] | 453 | (!(ic->flags & CF_EXPECT_MORE) || c_full(ic) || ci_data(ic) == 0 || ic->pipe)) { |
Willy Tarreau | 615f28b | 2015-09-23 18:40:09 +0200 | [diff] [blame] | 454 | int new_len, last_len; |
| 455 | |
Willy Tarreau | 77e478c | 2018-06-19 07:03:14 +0200 | [diff] [blame] | 456 | last_len = co_data(ic); |
Willy Tarreau | 615f28b | 2015-09-23 18:40:09 +0200 | [diff] [blame] | 457 | if (ic->pipe) |
| 458 | last_len += ic->pipe->data; |
| 459 | |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame^] | 460 | cs_chk_snd(sio->cs); |
Willy Tarreau | 615f28b | 2015-09-23 18:40:09 +0200 | [diff] [blame] | 461 | |
Willy Tarreau | 77e478c | 2018-06-19 07:03:14 +0200 | [diff] [blame] | 462 | new_len = co_data(ic); |
Willy Tarreau | 615f28b | 2015-09-23 18:40:09 +0200 | [diff] [blame] | 463 | if (ic->pipe) |
| 464 | new_len += ic->pipe->data; |
| 465 | |
| 466 | /* check if the consumer has freed some space either in the |
| 467 | * buffer or in the pipe. |
| 468 | */ |
Willy Tarreau | 47baeb8 | 2018-11-15 07:46:57 +0100 | [diff] [blame] | 469 | if (new_len < last_len) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 470 | si_rx_room_rdy(si); |
Willy Tarreau | 615f28b | 2015-09-23 18:40:09 +0200 | [diff] [blame] | 471 | } |
| 472 | |
Willy Tarreau | b26a6f9 | 2018-11-14 17:10:36 +0100 | [diff] [blame] | 473 | if (!(ic->flags & CF_DONT_READ)) |
| 474 | si_rx_chan_rdy(si); |
| 475 | |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame^] | 476 | cs_chk_rcv(si->cs); |
| 477 | cs_chk_rcv(sio->cs); |
Willy Tarreau | 47baeb8 | 2018-11-15 07:46:57 +0100 | [diff] [blame] | 478 | |
Willy Tarreau | b26a6f9 | 2018-11-14 17:10:36 +0100 | [diff] [blame] | 479 | if (si_rx_blocked(si)) { |
Willy Tarreau | 615f28b | 2015-09-23 18:40:09 +0200 | [diff] [blame] | 480 | ic->rex = TICK_ETERNITY; |
| 481 | } |
Willy Tarreau | b26a6f9 | 2018-11-14 17:10:36 +0100 | [diff] [blame] | 482 | else if ((ic->flags & (CF_SHUTR|CF_READ_PARTIAL)) == CF_READ_PARTIAL) { |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame^] | 483 | /* we must re-enable reading if cs_chk_snd() has freed some space */ |
Willy Tarreau | 615f28b | 2015-09-23 18:40:09 +0200 | [diff] [blame] | 484 | if (!(ic->flags & CF_READ_NOEXP) && tick_isset(ic->rex)) |
| 485 | ic->rex = tick_add_ifset(now_ms, ic->rto); |
| 486 | } |
| 487 | |
| 488 | /* wake the task up only when needed */ |
| 489 | if (/* changes on the production side */ |
| 490 | (ic->flags & (CF_READ_NULL|CF_READ_ERROR)) || |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 491 | !cs_state_in(si->cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST) || |
Christopher Faulet | 6cd56d5 | 2022-03-30 10:47:32 +0200 | [diff] [blame] | 492 | (si->cs->endp->flags & CS_EP_ERROR) || |
Willy Tarreau | 615f28b | 2015-09-23 18:40:09 +0200 | [diff] [blame] | 493 | ((ic->flags & CF_READ_PARTIAL) && |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 494 | ((ic->flags & CF_EOI) || !ic->to_forward || sio->cs->state != CS_ST_EST)) || |
Willy Tarreau | 615f28b | 2015-09-23 18:40:09 +0200 | [diff] [blame] | 495 | |
| 496 | /* changes on the consumption side */ |
| 497 | (oc->flags & (CF_WRITE_NULL|CF_WRITE_ERROR)) || |
Willy Tarreau | ede3d88 | 2018-10-24 17:17:56 +0200 | [diff] [blame] | 498 | ((oc->flags & CF_WRITE_ACTIVITY) && |
Willy Tarreau | 615f28b | 2015-09-23 18:40:09 +0200 | [diff] [blame] | 499 | ((oc->flags & CF_SHUTW) || |
Willy Tarreau | 78f5ff8 | 2018-12-19 11:00:00 +0100 | [diff] [blame] | 500 | (((oc->flags & CF_WAKE_WRITE) || |
| 501 | !(oc->flags & (CF_AUTO_CLOSE|CF_SHUTW_NOW|CF_SHUTW))) && |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 502 | (sio->cs->state != CS_ST_EST || |
Willy Tarreau | 615f28b | 2015-09-23 18:40:09 +0200 | [diff] [blame] | 503 | (channel_is_empty(oc) && !oc->to_forward)))))) { |
Christopher Faulet | d7607de | 2019-01-03 16:24:54 +0100 | [diff] [blame] | 504 | task_wakeup(task, TASK_WOKEN_IO); |
| 505 | } |
| 506 | else { |
| 507 | /* Update expiration date for the task and requeue it */ |
| 508 | task->expire = tick_first((tick_is_expired(task->expire, now_ms) ? 0 : task->expire), |
| 509 | tick_first(tick_first(ic->rex, ic->wex), |
| 510 | tick_first(oc->rex, oc->wex))); |
Willy Tarreau | 45bcb37 | 2019-08-01 18:51:38 +0200 | [diff] [blame] | 511 | |
| 512 | task->expire = tick_first(task->expire, ic->analyse_exp); |
| 513 | task->expire = tick_first(task->expire, oc->analyse_exp); |
Christopher Faulet | ae024ce | 2022-03-29 19:02:31 +0200 | [diff] [blame] | 514 | task->expire = tick_first(task->expire, __cs_strm(si->cs)->conn_exp); |
Willy Tarreau | 45bcb37 | 2019-08-01 18:51:38 +0200 | [diff] [blame] | 515 | |
Christopher Faulet | d7607de | 2019-01-03 16:24:54 +0100 | [diff] [blame] | 516 | task_queue(task); |
Willy Tarreau | 615f28b | 2015-09-23 18:40:09 +0200 | [diff] [blame] | 517 | } |
| 518 | if (ic->flags & CF_READ_ACTIVITY) |
| 519 | ic->flags &= ~CF_READ_DONTWAIT; |
Willy Tarreau | 615f28b | 2015-09-23 18:40:09 +0200 | [diff] [blame] | 520 | } |
| 521 | |
Willy Tarreau | d1480cc | 2022-03-17 16:19:09 +0100 | [diff] [blame] | 522 | /* The stream interface is only responsible for the connection during the early |
| 523 | * states, before plugging a mux. Thus it should only care about CO_FL_ERROR |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 524 | * before CS_ST_EST, and after that it must absolutely ignore it since the mux |
Willy Tarreau | d1480cc | 2022-03-17 16:19:09 +0100 | [diff] [blame] | 525 | * may hold pending data. This function returns true if such an error was |
| 526 | * reported. Both the CS and the CONN must be valid. |
| 527 | */ |
| 528 | static inline int si_is_conn_error(const struct stream_interface *si) |
| 529 | { |
| 530 | struct connection *conn; |
| 531 | |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 532 | if (si->cs->state >= CS_ST_EST) |
Willy Tarreau | d1480cc | 2022-03-17 16:19:09 +0100 | [diff] [blame] | 533 | return 0; |
| 534 | |
| 535 | conn = __cs_conn(si->cs); |
| 536 | BUG_ON(!conn); |
| 537 | return !!(conn->flags & CO_FL_ERROR); |
| 538 | } |
Willy Tarreau | 615f28b | 2015-09-23 18:40:09 +0200 | [diff] [blame] | 539 | |
Olivier Houchard | c2aa711 | 2018-09-11 18:27:21 +0200 | [diff] [blame] | 540 | /* Called by I/O handlers after completion.. It propagates |
Willy Tarreau | 651e182 | 2015-09-23 20:06:13 +0200 | [diff] [blame] | 541 | * connection flags to the stream interface, updates the stream (which may or |
| 542 | * may not take this opportunity to try to forward data), then update the |
| 543 | * connection's polling based on the channels and stream interface's final |
| 544 | * states. The function always returns 0. |
Willy Tarreau | 100c467 | 2012-08-20 12:06:26 +0200 | [diff] [blame] | 545 | */ |
Olivier Houchard | c2aa711 | 2018-09-11 18:27:21 +0200 | [diff] [blame] | 546 | static int si_cs_process(struct conn_stream *cs) |
Willy Tarreau | fd31e53 | 2012-07-23 18:24:25 +0200 | [diff] [blame] | 547 | { |
Christopher Faulet | 693b23b | 2022-02-28 09:09:05 +0100 | [diff] [blame] | 548 | struct connection *conn = __cs_conn(cs); |
Christopher Faulet | f835dea | 2021-12-21 14:35:17 +0100 | [diff] [blame] | 549 | struct stream_interface *si = cs_si(cs); |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 550 | struct channel *ic = si_ic(si); |
| 551 | struct channel *oc = si_oc(si); |
Willy Tarreau | fd31e53 | 2012-07-23 18:24:25 +0200 | [diff] [blame] | 552 | |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 553 | BUG_ON(!conn); |
| 554 | |
Olivier Houchard | c7ffa91 | 2018-08-28 19:37:41 +0200 | [diff] [blame] | 555 | /* If we have data to send, try it now */ |
Christopher Faulet | 2f35e7b | 2022-03-31 11:09:28 +0200 | [diff] [blame] | 556 | if (!channel_is_empty(oc) && !(si->cs->wait_event.events & SUB_RETRY_SEND)) |
Willy Tarreau | 908d26f | 2018-10-25 14:02:47 +0200 | [diff] [blame] | 557 | si_cs_send(cs); |
| 558 | |
Christopher Faulet | af642df | 2022-03-30 10:06:11 +0200 | [diff] [blame] | 559 | /* First step, report to the conn-stream what was detected at the |
Willy Tarreau | 651e182 | 2015-09-23 20:06:13 +0200 | [diff] [blame] | 560 | * connection layer : errors and connection establishment. |
Christopher Faulet | 6cd56d5 | 2022-03-30 10:47:32 +0200 | [diff] [blame] | 561 | * Only add CS_EP_ERROR if we're connected, or we're attempting to |
Olivier Houchard | c31e2cb | 2019-06-24 16:08:08 +0200 | [diff] [blame] | 562 | * connect, we may get there because we got woken up, but only run |
| 563 | * after process_stream() noticed there were an error, and decided |
| 564 | * to retry to connect, the connection may still have CO_FL_ERROR, |
Christopher Faulet | 6cd56d5 | 2022-03-30 10:47:32 +0200 | [diff] [blame] | 565 | * and we don't want to add CS_EP_ERROR back |
Christopher Faulet | 36b536d | 2019-11-20 11:56:33 +0100 | [diff] [blame] | 566 | * |
| 567 | * Note: This test is only required because si_cs_process is also the SI |
| 568 | * wake callback. Otherwise si_cs_recv()/si_cs_send() already take |
| 569 | * care of it. |
Willy Tarreau | 651e182 | 2015-09-23 20:06:13 +0200 | [diff] [blame] | 570 | */ |
Willy Tarreau | d1480cc | 2022-03-17 16:19:09 +0100 | [diff] [blame] | 571 | |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 572 | if (si->cs->state >= CS_ST_CON) { |
Christopher Faulet | 6cd56d5 | 2022-03-30 10:47:32 +0200 | [diff] [blame] | 573 | if (si_is_conn_error(si)) |
| 574 | cs->endp->flags |= CS_EP_ERROR; |
Willy Tarreau | d1480cc | 2022-03-17 16:19:09 +0100 | [diff] [blame] | 575 | } |
Willy Tarreau | 3c55ec2 | 2012-07-23 19:19:51 +0200 | [diff] [blame] | 576 | |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 577 | /* If we had early data, and the handshake ended, then |
| 578 | * we can remove the flag, and attempt to wake the task up, |
| 579 | * in the event there's an analyser waiting for the end of |
| 580 | * the handshake. |
| 581 | */ |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 582 | if (!(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS)) && |
Christopher Faulet | e9e4820 | 2022-03-22 18:13:29 +0100 | [diff] [blame] | 583 | (cs->endp->flags & CS_EP_WAIT_FOR_HS)) { |
| 584 | cs->endp->flags &= ~CS_EP_WAIT_FOR_HS; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 585 | task_wakeup(si_task(si), TASK_WOKEN_MSG); |
| 586 | } |
| 587 | |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 588 | if (!cs_state_in(si->cs->state, CS_SB_EST|CS_SB_DIS|CS_SB_CLO) && |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 589 | (conn->flags & CO_FL_WAIT_XPRT) == 0) { |
Christopher Faulet | ae024ce | 2022-03-29 19:02:31 +0200 | [diff] [blame] | 590 | __cs_strm(cs)->conn_exp = TICK_ETERNITY; |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 591 | oc->flags |= CF_WRITE_NULL; |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 592 | if (si->cs->state == CS_ST_CON) |
| 593 | si->cs->state = CS_ST_RDY; |
Willy Tarreau | 8f8c92f | 2012-07-23 19:45:44 +0200 | [diff] [blame] | 594 | } |
| 595 | |
Christopher Faulet | 89e34c2 | 2021-01-21 16:22:01 +0100 | [diff] [blame] | 596 | /* Report EOS on the channel if it was reached from the mux point of |
| 597 | * view. |
| 598 | * |
| 599 | * Note: This test is only required because si_cs_process is also the SI |
| 600 | * wake callback. Otherwise si_cs_recv()/si_cs_send() already take |
| 601 | * care of it. |
| 602 | */ |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 603 | if (cs->endp->flags & CS_EP_EOS && !(ic->flags & CF_SHUTR)) { |
Christopher Faulet | 89e34c2 | 2021-01-21 16:22:01 +0100 | [diff] [blame] | 604 | /* we received a shutdown */ |
| 605 | ic->flags |= CF_READ_NULL; |
| 606 | if (ic->flags & CF_AUTO_CLOSE) |
| 607 | channel_shutw_now(ic); |
| 608 | stream_int_read0(si); |
| 609 | } |
| 610 | |
Christopher Faulet | 297d3e2 | 2019-03-22 14:16:14 +0100 | [diff] [blame] | 611 | /* Report EOI on the channel if it was reached from the mux point of |
Christopher Faulet | 36b536d | 2019-11-20 11:56:33 +0100 | [diff] [blame] | 612 | * view. |
| 613 | * |
| 614 | * Note: This test is only required because si_cs_process is also the SI |
| 615 | * wake callback. Otherwise si_cs_recv()/si_cs_send() already take |
| 616 | * care of it. |
| 617 | */ |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 618 | if ((cs->endp->flags & CS_EP_EOI) && !(ic->flags & CF_EOI)) |
Christopher Faulet | 8e9e3ef | 2019-05-17 09:14:10 +0200 | [diff] [blame] | 619 | ic->flags |= (CF_EOI|CF_READ_PARTIAL); |
Christopher Faulet | 203b2b0 | 2019-03-08 09:23:46 +0100 | [diff] [blame] | 620 | |
Willy Tarreau | 651e182 | 2015-09-23 20:06:13 +0200 | [diff] [blame] | 621 | /* Second step : update the stream-int and channels, try to forward any |
| 622 | * pending data, then possibly wake the stream up based on the new |
| 623 | * stream-int status. |
Willy Tarreau | 44b5dc6 | 2012-08-24 12:12:53 +0200 | [diff] [blame] | 624 | */ |
Willy Tarreau | 651e182 | 2015-09-23 20:06:13 +0200 | [diff] [blame] | 625 | stream_int_notify(si); |
Willy Tarreau | a64c703 | 2019-08-01 14:17:02 +0200 | [diff] [blame] | 626 | stream_release_buffers(si_strm(si)); |
Willy Tarreau | 2396c1c | 2012-10-03 21:12:16 +0200 | [diff] [blame] | 627 | return 0; |
Willy Tarreau | fd31e53 | 2012-07-23 18:24:25 +0200 | [diff] [blame] | 628 | } |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 629 | |
Willy Tarreau | 5368d80 | 2012-08-21 18:22:06 +0200 | [diff] [blame] | 630 | /* |
| 631 | * This function is called to send buffer data to a stream socket. |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 632 | * It calls the mux layer's snd_buf function. It relies on the |
Godbach | 4f48990 | 2013-12-04 17:24:06 +0800 | [diff] [blame] | 633 | * caller to commit polling changes. The caller should check conn->flags |
| 634 | * for errors. |
Willy Tarreau | 5368d80 | 2012-08-21 18:22:06 +0200 | [diff] [blame] | 635 | */ |
Christopher Faulet | 4941623 | 2022-02-28 09:14:46 +0100 | [diff] [blame] | 636 | static int si_cs_send(struct conn_stream *cs) |
Willy Tarreau | 5368d80 | 2012-08-21 18:22:06 +0200 | [diff] [blame] | 637 | { |
Christopher Faulet | 693b23b | 2022-02-28 09:09:05 +0100 | [diff] [blame] | 638 | struct connection *conn = __cs_conn(cs); |
Christopher Faulet | f835dea | 2021-12-21 14:35:17 +0100 | [diff] [blame] | 639 | struct stream_interface *si = cs_si(cs); |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 640 | struct stream *s = si_strm(si); |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 641 | struct channel *oc = si_oc(si); |
Willy Tarreau | 5368d80 | 2012-08-21 18:22:06 +0200 | [diff] [blame] | 642 | int ret; |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 643 | int did_send = 0; |
| 644 | |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 645 | if (cs->endp->flags & (CS_EP_ERROR|CS_EP_ERR_PENDING) || si_is_conn_error(si)) { |
Olivier Houchard | c31e2cb | 2019-06-24 16:08:08 +0200 | [diff] [blame] | 646 | /* We're probably there because the tasklet was woken up, |
| 647 | * but process_stream() ran before, detected there were an |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 648 | * error and put the si back to CS_ST_TAR. There's still |
Olivier Houchard | c31e2cb | 2019-06-24 16:08:08 +0200 | [diff] [blame] | 649 | * CO_FL_ERROR on the connection but we don't want to add |
Christopher Faulet | 6cd56d5 | 2022-03-30 10:47:32 +0200 | [diff] [blame] | 650 | * CS_EP_ERROR back, so give up |
Olivier Houchard | c31e2cb | 2019-06-24 16:08:08 +0200 | [diff] [blame] | 651 | */ |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 652 | if (si->cs->state < CS_ST_CON) |
Olivier Houchard | c31e2cb | 2019-06-24 16:08:08 +0200 | [diff] [blame] | 653 | return 0; |
Christopher Faulet | 6cd56d5 | 2022-03-30 10:47:32 +0200 | [diff] [blame] | 654 | cs->endp->flags |= CS_EP_ERROR; |
Olivier Houchard | c2aa711 | 2018-09-11 18:27:21 +0200 | [diff] [blame] | 655 | return 1; |
Willy Tarreau | bddf7fc | 2018-12-19 17:17:10 +0100 | [diff] [blame] | 656 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 657 | |
Christopher Faulet | 328ed22 | 2019-09-23 15:57:29 +0200 | [diff] [blame] | 658 | /* We're already waiting to be able to send, give up */ |
Christopher Faulet | 2f35e7b | 2022-03-31 11:09:28 +0200 | [diff] [blame] | 659 | if (si->cs->wait_event.events & SUB_RETRY_SEND) |
Christopher Faulet | 328ed22 | 2019-09-23 15:57:29 +0200 | [diff] [blame] | 660 | return 0; |
| 661 | |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 662 | /* we might have been called just after an asynchronous shutw */ |
Willy Tarreau | f22758d | 2020-01-23 18:25:23 +0100 | [diff] [blame] | 663 | if (oc->flags & CF_SHUTW) |
Olivier Houchard | c2aa711 | 2018-09-11 18:27:21 +0200 | [diff] [blame] | 664 | return 1; |
Willy Tarreau | 5368d80 | 2012-08-21 18:22:06 +0200 | [diff] [blame] | 665 | |
Christopher Faulet | e96993b | 2020-07-30 09:26:46 +0200 | [diff] [blame] | 666 | /* we must wait because the mux is not installed yet */ |
| 667 | if (!conn->mux) |
| 668 | return 0; |
| 669 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 670 | if (oc->pipe && conn->xprt->snd_pipe && conn->mux->snd_pipe) { |
| 671 | ret = conn->mux->snd_pipe(cs, oc->pipe); |
Christopher Faulet | 86162db | 2019-07-05 11:49:11 +0200 | [diff] [blame] | 672 | if (ret > 0) |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 673 | did_send = 1; |
Willy Tarreau | 5368d80 | 2012-08-21 18:22:06 +0200 | [diff] [blame] | 674 | |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 675 | if (!oc->pipe->data) { |
| 676 | put_pipe(oc->pipe); |
| 677 | oc->pipe = NULL; |
Willy Tarreau | 5368d80 | 2012-08-21 18:22:06 +0200 | [diff] [blame] | 678 | } |
| 679 | |
Christopher Faulet | 3f76f4c | 2018-11-20 10:21:08 +0100 | [diff] [blame] | 680 | if (oc->pipe) |
| 681 | goto end; |
Willy Tarreau | 5368d80 | 2012-08-21 18:22:06 +0200 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | /* At this point, the pipe is empty, but we may still have data pending |
| 685 | * in the normal buffer. |
| 686 | */ |
Christopher Faulet | 55dec0d | 2018-11-20 10:30:02 +0100 | [diff] [blame] | 687 | if (co_data(oc)) { |
| 688 | /* when we're here, we already know that there is no spliced |
| 689 | * data left, and that there are sendable buffered data. |
| 690 | */ |
Willy Tarreau | 5368d80 | 2012-08-21 18:22:06 +0200 | [diff] [blame] | 691 | |
Willy Tarreau | 5368d80 | 2012-08-21 18:22:06 +0200 | [diff] [blame] | 692 | /* check if we want to inform the kernel that we're interested in |
| 693 | * sending more data after this call. We want this if : |
| 694 | * - we're about to close after this last send and want to merge |
| 695 | * the ongoing FIN with the last segment. |
| 696 | * - we know we can't send everything at once and must get back |
| 697 | * here because of unaligned data |
| 698 | * - there is still a finite amount of data to forward |
| 699 | * The test is arranged so that the most common case does only 2 |
| 700 | * tests. |
| 701 | */ |
Willy Tarreau | 1049b1f | 2014-02-02 01:51:17 +0100 | [diff] [blame] | 702 | unsigned int send_flag = 0; |
Willy Tarreau | 5368d80 | 2012-08-21 18:22:06 +0200 | [diff] [blame] | 703 | |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 704 | if ((!(oc->flags & (CF_NEVER_WAIT|CF_SEND_DONTWAIT)) && |
| 705 | ((oc->to_forward && oc->to_forward != CHN_INFINITE_FORWARD) || |
Willy Tarreau | 8945bb6 | 2020-06-19 17:07:06 +0200 | [diff] [blame] | 706 | (oc->flags & CF_EXPECT_MORE) || |
Christopher Faulet | 9e3dc83 | 2020-07-22 16:28:44 +0200 | [diff] [blame] | 707 | (IS_HTX_STRM(si_strm(si)) && |
| 708 | (!(oc->flags & (CF_EOI|CF_SHUTR)) && htx_expect_more(htxbuf(&oc->buf)))))) || |
Willy Tarreau | ecd2e15 | 2017-11-07 15:07:25 +0100 | [diff] [blame] | 709 | ((oc->flags & CF_ISRESP) && |
| 710 | ((oc->flags & (CF_AUTO_CLOSE|CF_SHUTW_NOW)) == (CF_AUTO_CLOSE|CF_SHUTW_NOW)))) |
Willy Tarreau | 1049b1f | 2014-02-02 01:51:17 +0100 | [diff] [blame] | 711 | send_flag |= CO_SFL_MSG_MORE; |
Willy Tarreau | 5368d80 | 2012-08-21 18:22:06 +0200 | [diff] [blame] | 712 | |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 713 | if (oc->flags & CF_STREAMER) |
Willy Tarreau | 7bed945 | 2014-02-02 02:00:24 +0100 | [diff] [blame] | 714 | send_flag |= CO_SFL_STREAMER; |
| 715 | |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 716 | if (s->txn && s->txn->flags & TX_L7_RETRY && !b_data(&s->txn->l7_buffer)) { |
Christopher Faulet | 9f5382e | 2021-05-21 13:46:14 +0200 | [diff] [blame] | 717 | /* If we want to be able to do L7 retries, copy |
| 718 | * the data we're about to send, so that we are able |
| 719 | * to resend them if needed |
| 720 | */ |
| 721 | /* Try to allocate a buffer if we had none. |
| 722 | * If it fails, the next test will just |
| 723 | * disable the l7 retries by setting |
| 724 | * l7_conn_retries to 0. |
| 725 | */ |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 726 | if (s->txn->req.msg_state != HTTP_MSG_DONE) |
| 727 | s->txn->flags &= ~TX_L7_RETRY; |
Christopher Faulet | 9f5382e | 2021-05-21 13:46:14 +0200 | [diff] [blame] | 728 | else { |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 729 | if (b_alloc(&s->txn->l7_buffer) == NULL) |
| 730 | s->txn->flags &= ~TX_L7_RETRY; |
Christopher Faulet | 9f5382e | 2021-05-21 13:46:14 +0200 | [diff] [blame] | 731 | else { |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 732 | memcpy(b_orig(&s->txn->l7_buffer), |
Christopher Faulet | 9f5382e | 2021-05-21 13:46:14 +0200 | [diff] [blame] | 733 | b_orig(&oc->buf), |
| 734 | b_size(&oc->buf)); |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 735 | s->txn->l7_buffer.head = co_data(oc); |
| 736 | b_add(&s->txn->l7_buffer, co_data(oc)); |
Christopher Faulet | 9f5382e | 2021-05-21 13:46:14 +0200 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | } |
| 740 | } |
| 741 | |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 742 | ret = conn->mux->snd_buf(cs, &oc->buf, co_data(oc), send_flag); |
Godbach | e68e02d | 2013-10-11 15:48:29 +0800 | [diff] [blame] | 743 | if (ret > 0) { |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 744 | did_send = 1; |
Willy Tarreau | 8424004 | 2022-02-28 16:51:23 +0100 | [diff] [blame] | 745 | c_rew(oc, ret); |
Willy Tarreau | deccd11 | 2018-06-14 18:38:55 +0200 | [diff] [blame] | 746 | c_realign_if_empty(oc); |
| 747 | |
| 748 | if (!co_data(oc)) { |
Godbach | e68e02d | 2013-10-11 15:48:29 +0800 | [diff] [blame] | 749 | /* Always clear both flags once everything has been sent, they're one-shot */ |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 750 | oc->flags &= ~(CF_EXPECT_MORE | CF_SEND_DONTWAIT); |
Godbach | e68e02d | 2013-10-11 15:48:29 +0800 | [diff] [blame] | 751 | } |
Godbach | e68e02d | 2013-10-11 15:48:29 +0800 | [diff] [blame] | 752 | /* if some data remain in the buffer, it's only because the |
| 753 | * system buffers are full, we will try next time. |
| 754 | */ |
Willy Tarreau | 5368d80 | 2012-08-21 18:22:06 +0200 | [diff] [blame] | 755 | } |
Godbach | e68e02d | 2013-10-11 15:48:29 +0800 | [diff] [blame] | 756 | } |
Christopher Faulet | 55dec0d | 2018-11-20 10:30:02 +0100 | [diff] [blame] | 757 | |
Willy Tarreau | f6975aa | 2018-11-15 14:33:05 +0100 | [diff] [blame] | 758 | end: |
Christopher Faulet | 86162db | 2019-07-05 11:49:11 +0200 | [diff] [blame] | 759 | if (did_send) { |
| 760 | oc->flags |= CF_WRITE_PARTIAL | CF_WROTE_DATA; |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 761 | if (si->cs->state == CS_ST_CON) |
| 762 | si->cs->state = CS_ST_RDY; |
Christopher Faulet | 037b3eb | 2019-07-05 13:44:29 +0200 | [diff] [blame] | 763 | |
| 764 | si_rx_room_rdy(si_opposite(si)); |
Christopher Faulet | 86162db | 2019-07-05 11:49:11 +0200 | [diff] [blame] | 765 | } |
| 766 | |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 767 | if (cs->endp->flags & (CS_EP_ERROR|CS_EP_ERR_PENDING)) { |
Christopher Faulet | 6cd56d5 | 2022-03-30 10:47:32 +0200 | [diff] [blame] | 768 | cs->endp->flags |= CS_EP_ERROR; |
Christopher Faulet | 86162db | 2019-07-05 11:49:11 +0200 | [diff] [blame] | 769 | return 1; |
| 770 | } |
| 771 | |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 772 | /* We couldn't send all of our data, let the mux know we'd like to send more */ |
Willy Tarreau | 691fe39 | 2018-11-12 18:48:52 +0100 | [diff] [blame] | 773 | if (!channel_is_empty(oc)) |
Christopher Faulet | 2f35e7b | 2022-03-31 11:09:28 +0200 | [diff] [blame] | 774 | conn->mux->subscribe(cs, SUB_RETRY_SEND, &si->cs->wait_event); |
Olivier Houchard | f653528 | 2018-08-31 17:29:12 +0200 | [diff] [blame] | 775 | return did_send; |
Willy Tarreau | 5368d80 | 2012-08-21 18:22:06 +0200 | [diff] [blame] | 776 | } |
| 777 | |
Christopher Faulet | 2f35e7b | 2022-03-31 11:09:28 +0200 | [diff] [blame] | 778 | /* This is the ->process() function for any conn-stream's wait_event task. |
Willy Tarreau | 8ccd208 | 2018-11-07 07:47:52 +0100 | [diff] [blame] | 779 | * It's assigned during the stream-interface's initialization, for any type of |
| 780 | * stream interface. Thus it is always safe to perform a tasklet_wakeup() on a |
| 781 | * stream interface, as the presence of the CS is checked there. |
| 782 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 783 | struct task *si_cs_io_cb(struct task *t, void *ctx, unsigned int state) |
Olivier Houchard | 91894cb | 2018-08-02 18:06:28 +0200 | [diff] [blame] | 784 | { |
Olivier Houchard | 8f0b4c6 | 2018-08-02 18:21:38 +0200 | [diff] [blame] | 785 | struct stream_interface *si = ctx; |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 786 | struct conn_stream *cs = si->cs; |
Olivier Houchard | f653528 | 2018-08-31 17:29:12 +0200 | [diff] [blame] | 787 | int ret = 0; |
Olivier Houchard | a6ff035 | 2018-08-21 15:59:43 +0200 | [diff] [blame] | 788 | |
Christopher Faulet | 0256da1 | 2021-12-15 09:50:17 +0100 | [diff] [blame] | 789 | if (!cs_conn(cs)) |
Willy Tarreau | 7416314 | 2021-03-13 11:30:19 +0100 | [diff] [blame] | 790 | return t; |
Willy Tarreau | 8ccd208 | 2018-11-07 07:47:52 +0100 | [diff] [blame] | 791 | |
Christopher Faulet | 2f35e7b | 2022-03-31 11:09:28 +0200 | [diff] [blame] | 792 | if (!(cs->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(si_oc(si))) |
Olivier Houchard | f653528 | 2018-08-31 17:29:12 +0200 | [diff] [blame] | 793 | ret = si_cs_send(cs); |
Christopher Faulet | 2f35e7b | 2022-03-31 11:09:28 +0200 | [diff] [blame] | 794 | if (!(cs->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | f653528 | 2018-08-31 17:29:12 +0200 | [diff] [blame] | 795 | ret |= si_cs_recv(cs); |
| 796 | if (ret != 0) |
Olivier Houchard | c2aa711 | 2018-09-11 18:27:21 +0200 | [diff] [blame] | 797 | si_cs_process(cs); |
Olivier Houchard | f653528 | 2018-08-31 17:29:12 +0200 | [diff] [blame] | 798 | |
Willy Tarreau | a64c703 | 2019-08-01 14:17:02 +0200 | [diff] [blame] | 799 | stream_release_buffers(si_strm(si)); |
Willy Tarreau | 7416314 | 2021-03-13 11:30:19 +0100 | [diff] [blame] | 800 | return t; |
Olivier Houchard | 91894cb | 2018-08-02 18:06:28 +0200 | [diff] [blame] | 801 | } |
| 802 | |
Willy Tarreau | 25f1310 | 2015-09-24 11:32:22 +0200 | [diff] [blame] | 803 | /* This function is designed to be called from within the stream handler to |
Willy Tarreau | 236c429 | 2019-06-06 08:19:20 +0200 | [diff] [blame] | 804 | * update the input channel's expiration timer and the stream interface's |
| 805 | * Rx flags based on the channel's flags. It needs to be called only once |
| 806 | * after the channel's flags have settled down, and before they are cleared, |
| 807 | * though it doesn't harm to call it as often as desired (it just slightly |
| 808 | * hurts performance). It must not be called from outside of the stream |
| 809 | * handler, as what it does will be used to compute the stream task's |
| 810 | * expiration. |
Willy Tarreau | 25f1310 | 2015-09-24 11:32:22 +0200 | [diff] [blame] | 811 | */ |
Willy Tarreau | 236c429 | 2019-06-06 08:19:20 +0200 | [diff] [blame] | 812 | void si_update_rx(struct stream_interface *si) |
Willy Tarreau | 25f1310 | 2015-09-24 11:32:22 +0200 | [diff] [blame] | 813 | { |
| 814 | struct channel *ic = si_ic(si); |
Willy Tarreau | 25f1310 | 2015-09-24 11:32:22 +0200 | [diff] [blame] | 815 | |
Willy Tarreau | 236c429 | 2019-06-06 08:19:20 +0200 | [diff] [blame] | 816 | if (ic->flags & CF_SHUTR) { |
| 817 | si_rx_shut_blk(si); |
| 818 | return; |
| 819 | } |
Willy Tarreau | b26a6f9 | 2018-11-14 17:10:36 +0100 | [diff] [blame] | 820 | |
Willy Tarreau | 236c429 | 2019-06-06 08:19:20 +0200 | [diff] [blame] | 821 | /* Read not closed, update FD status and timeout for reads */ |
| 822 | if (ic->flags & CF_DONT_READ) |
| 823 | si_rx_chan_blk(si); |
| 824 | else |
| 825 | si_rx_chan_rdy(si); |
Willy Tarreau | b26a6f9 | 2018-11-14 17:10:36 +0100 | [diff] [blame] | 826 | |
Christopher Faulet | 69fad00 | 2021-10-29 14:55:59 +0200 | [diff] [blame] | 827 | if (!channel_is_empty(ic) || !channel_may_recv(ic)) { |
Willy Tarreau | 236c429 | 2019-06-06 08:19:20 +0200 | [diff] [blame] | 828 | /* stop reading, imposed by channel's policy or contents */ |
| 829 | si_rx_room_blk(si); |
Willy Tarreau | 25f1310 | 2015-09-24 11:32:22 +0200 | [diff] [blame] | 830 | } |
Willy Tarreau | 236c429 | 2019-06-06 08:19:20 +0200 | [diff] [blame] | 831 | else { |
| 832 | /* (re)start reading and update timeout. Note: we don't recompute the timeout |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 833 | * every time we get here, otherwise it would risk never to expire. We only |
Willy Tarreau | 236c429 | 2019-06-06 08:19:20 +0200 | [diff] [blame] | 834 | * update it if is was not yet set. The stream socket handler will already |
| 835 | * have updated it if there has been a completed I/O. |
| 836 | */ |
| 837 | si_rx_room_rdy(si); |
| 838 | } |
| 839 | if (si->flags & SI_FL_RXBLK_ANY & ~SI_FL_RX_WAIT_EP) |
| 840 | ic->rex = TICK_ETERNITY; |
| 841 | else if (!(ic->flags & CF_READ_NOEXP) && !tick_isset(ic->rex)) |
| 842 | ic->rex = tick_add_ifset(now_ms, ic->rto); |
Willy Tarreau | 25f1310 | 2015-09-24 11:32:22 +0200 | [diff] [blame] | 843 | |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame^] | 844 | cs_chk_rcv(si->cs); |
Willy Tarreau | 236c429 | 2019-06-06 08:19:20 +0200 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | /* This function is designed to be called from within the stream handler to |
| 848 | * update the output channel's expiration timer and the stream interface's |
| 849 | * Tx flags based on the channel's flags. It needs to be called only once |
| 850 | * after the channel's flags have settled down, and before they are cleared, |
| 851 | * though it doesn't harm to call it as often as desired (it just slightly |
| 852 | * hurts performance). It must not be called from outside of the stream |
| 853 | * handler, as what it does will be used to compute the stream task's |
| 854 | * expiration. |
| 855 | */ |
| 856 | void si_update_tx(struct stream_interface *si) |
| 857 | { |
| 858 | struct channel *oc = si_oc(si); |
| 859 | struct channel *ic = si_ic(si); |
| 860 | |
| 861 | if (oc->flags & CF_SHUTW) |
| 862 | return; |
| 863 | |
| 864 | /* Write not closed, update FD status and timeout for writes */ |
| 865 | if (channel_is_empty(oc)) { |
| 866 | /* stop writing */ |
| 867 | if (!(si->flags & SI_FL_WAIT_DATA)) { |
| 868 | if ((oc->flags & CF_SHUTW_NOW) == 0) |
| 869 | si->flags |= SI_FL_WAIT_DATA; |
| 870 | oc->wex = TICK_ETERNITY; |
Willy Tarreau | 25f1310 | 2015-09-24 11:32:22 +0200 | [diff] [blame] | 871 | } |
Willy Tarreau | 236c429 | 2019-06-06 08:19:20 +0200 | [diff] [blame] | 872 | return; |
| 873 | } |
| 874 | |
| 875 | /* (re)start writing and update timeout. Note: we don't recompute the timeout |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 876 | * every time we get here, otherwise it would risk never to expire. We only |
Willy Tarreau | 236c429 | 2019-06-06 08:19:20 +0200 | [diff] [blame] | 877 | * update it if is was not yet set. The stream socket handler will already |
| 878 | * have updated it if there has been a completed I/O. |
| 879 | */ |
| 880 | si->flags &= ~SI_FL_WAIT_DATA; |
| 881 | if (!tick_isset(oc->wex)) { |
| 882 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
Christopher Faulet | a728518 | 2022-03-30 15:43:23 +0200 | [diff] [blame] | 883 | if (tick_isset(ic->rex) && !(si->cs->flags & CS_FL_INDEP_STR)) { |
Willy Tarreau | 236c429 | 2019-06-06 08:19:20 +0200 | [diff] [blame] | 884 | /* Note: depending on the protocol, we don't know if we're waiting |
| 885 | * for incoming data or not. So in order to prevent the socket from |
| 886 | * expiring read timeouts during writes, we refresh the read timeout, |
| 887 | * except if it was already infinite or if we have explicitly setup |
| 888 | * independent streams. |
Willy Tarreau | 25f1310 | 2015-09-24 11:32:22 +0200 | [diff] [blame] | 889 | */ |
Willy Tarreau | 236c429 | 2019-06-06 08:19:20 +0200 | [diff] [blame] | 890 | ic->rex = tick_add_ifset(now_ms, ic->rto); |
Willy Tarreau | 25f1310 | 2015-09-24 11:32:22 +0200 | [diff] [blame] | 891 | } |
| 892 | } |
| 893 | } |
| 894 | |
Christopher Faulet | 9936dc6 | 2022-02-28 09:21:58 +0100 | [diff] [blame] | 895 | /* This tries to perform a synchronous receive on the stream interface to |
| 896 | * try to collect last arrived data. In practice it's only implemented on |
| 897 | * conn_streams. Returns 0 if nothing was done, non-zero if new data or a |
| 898 | * shutdown were collected. This may result on some delayed receive calls |
| 899 | * to be programmed and performed later, though it doesn't provide any |
| 900 | * such guarantee. |
| 901 | */ |
| 902 | int si_sync_recv(struct stream_interface *si) |
| 903 | { |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 904 | if (!cs_state_in(si->cs->state, CS_SB_RDY|CS_SB_EST)) |
Christopher Faulet | 9936dc6 | 2022-02-28 09:21:58 +0100 | [diff] [blame] | 905 | return 0; |
| 906 | |
| 907 | if (!cs_conn_mux(si->cs)) |
| 908 | return 0; // only conn_streams are supported |
| 909 | |
Christopher Faulet | 2f35e7b | 2022-03-31 11:09:28 +0200 | [diff] [blame] | 910 | if (si->cs->wait_event.events & SUB_RETRY_RECV) |
Christopher Faulet | 9936dc6 | 2022-02-28 09:21:58 +0100 | [diff] [blame] | 911 | return 0; // already subscribed |
| 912 | |
| 913 | if (!si_rx_endp_ready(si) || si_rx_blocked(si)) |
| 914 | return 0; // already failed |
| 915 | |
| 916 | return si_cs_recv(si->cs); |
| 917 | } |
| 918 | |
Willy Tarreau | 3b285d7 | 2019-06-06 08:20:17 +0200 | [diff] [blame] | 919 | /* perform a synchronous send() for the stream interface. The CF_WRITE_NULL and |
| 920 | * CF_WRITE_PARTIAL flags are cleared prior to the attempt, and will possibly |
| 921 | * be updated in case of success. |
| 922 | */ |
| 923 | void si_sync_send(struct stream_interface *si) |
| 924 | { |
| 925 | struct channel *oc = si_oc(si); |
Willy Tarreau | 3b285d7 | 2019-06-06 08:20:17 +0200 | [diff] [blame] | 926 | |
| 927 | oc->flags &= ~(CF_WRITE_NULL|CF_WRITE_PARTIAL); |
| 928 | |
| 929 | if (oc->flags & CF_SHUTW) |
| 930 | return; |
| 931 | |
| 932 | if (channel_is_empty(oc)) |
| 933 | return; |
| 934 | |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 935 | if (!cs_state_in(si->cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST)) |
Willy Tarreau | 3b285d7 | 2019-06-06 08:20:17 +0200 | [diff] [blame] | 936 | return; |
| 937 | |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 938 | if (!cs_conn_mux(si->cs)) |
Willy Tarreau | 3b285d7 | 2019-06-06 08:20:17 +0200 | [diff] [blame] | 939 | return; |
| 940 | |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 941 | si_cs_send(si->cs); |
Willy Tarreau | 3b285d7 | 2019-06-06 08:20:17 +0200 | [diff] [blame] | 942 | } |
| 943 | |
Willy Tarreau | d14844a | 2018-11-08 18:15:29 +0100 | [diff] [blame] | 944 | /* Updates at once the channel flags, and timers of both stream interfaces of a |
| 945 | * same stream, to complete the work after the analysers, then updates the data |
| 946 | * layer below. This will ensure that any synchronous update performed at the |
| 947 | * data layer will be reflected in the channel flags and/or stream-interface. |
Willy Tarreau | 829bd47 | 2019-06-06 09:17:23 +0200 | [diff] [blame] | 948 | * Note that this does not change the stream interface's current state, though |
| 949 | * it updates the previous state to the current one. |
Willy Tarreau | d14844a | 2018-11-08 18:15:29 +0100 | [diff] [blame] | 950 | */ |
| 951 | void si_update_both(struct stream_interface *si_f, struct stream_interface *si_b) |
| 952 | { |
| 953 | struct channel *req = si_ic(si_f); |
| 954 | struct channel *res = si_oc(si_f); |
Willy Tarreau | d14844a | 2018-11-08 18:15:29 +0100 | [diff] [blame] | 955 | |
| 956 | req->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_READ_ATTACHED|CF_WRITE_NULL|CF_WRITE_PARTIAL); |
| 957 | res->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_READ_ATTACHED|CF_WRITE_NULL|CF_WRITE_PARTIAL); |
| 958 | |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 959 | si_strm(si_b)->prev_conn_state = si_b->cs->state; |
Willy Tarreau | d14844a | 2018-11-08 18:15:29 +0100 | [diff] [blame] | 960 | |
Willy Tarreau | bf89ff3 | 2018-11-09 14:59:25 +0100 | [diff] [blame] | 961 | /* let's recompute both sides states */ |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 962 | if (cs_state_in(si_f->cs->state, CS_SB_RDY|CS_SB_EST)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 963 | si_update(si_f); |
Willy Tarreau | bf89ff3 | 2018-11-09 14:59:25 +0100 | [diff] [blame] | 964 | |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 965 | if (cs_state_in(si_b->cs->state, CS_SB_RDY|CS_SB_EST)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 966 | si_update(si_b); |
Willy Tarreau | bf89ff3 | 2018-11-09 14:59:25 +0100 | [diff] [blame] | 967 | |
| 968 | /* stream ints are processed outside of process_stream() and must be |
| 969 | * handled at the latest moment. |
| 970 | */ |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 971 | if (cs_appctx(si_f->cs) && |
Willy Tarreau | 32742fd | 2018-11-14 14:07:59 +0100 | [diff] [blame] | 972 | ((si_rx_endp_ready(si_f) && !si_rx_blocked(si_f)) || |
| 973 | (si_tx_endp_ready(si_f) && !si_tx_blocked(si_f)))) |
Christopher Faulet | 693b23b | 2022-02-28 09:09:05 +0100 | [diff] [blame] | 974 | appctx_wakeup(__cs_appctx(si_f->cs)); |
Willy Tarreau | d14844a | 2018-11-08 18:15:29 +0100 | [diff] [blame] | 975 | |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 976 | if (cs_appctx(si_b->cs) && |
Willy Tarreau | 32742fd | 2018-11-14 14:07:59 +0100 | [diff] [blame] | 977 | ((si_rx_endp_ready(si_b) && !si_rx_blocked(si_b)) || |
| 978 | (si_tx_endp_ready(si_b) && !si_tx_blocked(si_b)))) |
Christopher Faulet | 693b23b | 2022-02-28 09:09:05 +0100 | [diff] [blame] | 979 | appctx_wakeup(__cs_appctx(si_b->cs)); |
Willy Tarreau | d14844a | 2018-11-08 18:15:29 +0100 | [diff] [blame] | 980 | } |
| 981 | |
Willy Tarreau | 8b3d7df | 2013-09-29 14:51:58 +0200 | [diff] [blame] | 982 | /* |
| 983 | * This function performs a shutdown-read on a stream interface attached to |
| 984 | * a connection in a connected or init state (it does nothing for other |
| 985 | * states). It either shuts the read side or marks itself as closed. The buffer |
| 986 | * flags are updated to reflect the new state. If the stream interface has |
Christopher Faulet | 8abe712 | 2022-03-30 15:10:18 +0200 | [diff] [blame] | 987 | * CS_FL_NOHALF, we also forward the close to the write side. If a control |
Willy Tarreau | 8b3d7df | 2013-09-29 14:51:58 +0200 | [diff] [blame] | 988 | * layer is defined, then it is supposed to be a socket layer and file |
Willy Tarreau | 6fe1541 | 2013-09-29 15:16:03 +0200 | [diff] [blame] | 989 | * descriptors are then shutdown or closed accordingly. The function |
| 990 | * automatically disables polling if needed. |
Willy Tarreau | 8b3d7df | 2013-09-29 14:51:58 +0200 | [diff] [blame] | 991 | */ |
Willy Tarreau | 6fe1541 | 2013-09-29 15:16:03 +0200 | [diff] [blame] | 992 | static void stream_int_shutr_conn(struct stream_interface *si) |
Willy Tarreau | 8b3d7df | 2013-09-29 14:51:58 +0200 | [diff] [blame] | 993 | { |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 994 | struct conn_stream *cs = si->cs; |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 995 | struct channel *ic = si_ic(si); |
Willy Tarreau | 8b3d7df | 2013-09-29 14:51:58 +0200 | [diff] [blame] | 996 | |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 997 | BUG_ON(!cs_conn(cs)); |
| 998 | |
Willy Tarreau | abb5d42 | 2018-11-14 16:58:52 +0100 | [diff] [blame] | 999 | si_rx_shut_blk(si); |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1000 | if (ic->flags & CF_SHUTR) |
Willy Tarreau | 6fe1541 | 2013-09-29 15:16:03 +0200 | [diff] [blame] | 1001 | return; |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1002 | ic->flags |= CF_SHUTR; |
| 1003 | ic->rex = TICK_ETERNITY; |
Willy Tarreau | 8b3d7df | 2013-09-29 14:51:58 +0200 | [diff] [blame] | 1004 | |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 1005 | if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST)) |
Willy Tarreau | 6fe1541 | 2013-09-29 15:16:03 +0200 | [diff] [blame] | 1006 | return; |
Willy Tarreau | 8b3d7df | 2013-09-29 14:51:58 +0200 | [diff] [blame] | 1007 | |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1008 | if (si_oc(si)->flags & CF_SHUTW) { |
Christopher Faulet | 69ef6c9 | 2022-03-31 14:20:00 +0200 | [diff] [blame] | 1009 | cs_conn_close(cs); |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 1010 | cs->state = CS_ST_DIS; |
Christopher Faulet | ae024ce | 2022-03-29 19:02:31 +0200 | [diff] [blame] | 1011 | __cs_strm(cs)->conn_exp = TICK_ETERNITY; |
Willy Tarreau | 8b3d7df | 2013-09-29 14:51:58 +0200 | [diff] [blame] | 1012 | } |
Christopher Faulet | 8abe712 | 2022-03-30 15:10:18 +0200 | [diff] [blame] | 1013 | else if (si->cs->flags & CS_FL_NOHALF) { |
Willy Tarreau | 8b3d7df | 2013-09-29 14:51:58 +0200 | [diff] [blame] | 1014 | /* we want to immediately forward this close to the write side */ |
| 1015 | return stream_int_shutw_conn(si); |
| 1016 | } |
Willy Tarreau | 8b3d7df | 2013-09-29 14:51:58 +0200 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | /* |
| 1020 | * This function performs a shutdown-write on a stream interface attached to |
| 1021 | * a connection in a connected or init state (it does nothing for other |
| 1022 | * states). It either shuts the write side or marks itself as closed. The |
| 1023 | * buffer flags are updated to reflect the new state. It does also close |
| 1024 | * everything if the SI was marked as being in error state. If there is a |
Willy Tarreau | 1398aa1 | 2015-03-12 23:04:07 +0100 | [diff] [blame] | 1025 | * data-layer shutdown, it is called. |
Willy Tarreau | 8b3d7df | 2013-09-29 14:51:58 +0200 | [diff] [blame] | 1026 | */ |
Willy Tarreau | 6fe1541 | 2013-09-29 15:16:03 +0200 | [diff] [blame] | 1027 | static void stream_int_shutw_conn(struct stream_interface *si) |
Willy Tarreau | 8b3d7df | 2013-09-29 14:51:58 +0200 | [diff] [blame] | 1028 | { |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 1029 | struct conn_stream *cs = si->cs; |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1030 | struct channel *ic = si_ic(si); |
| 1031 | struct channel *oc = si_oc(si); |
Willy Tarreau | 8b3d7df | 2013-09-29 14:51:58 +0200 | [diff] [blame] | 1032 | |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 1033 | BUG_ON(!cs_conn(cs)); |
| 1034 | |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1035 | oc->flags &= ~CF_SHUTW_NOW; |
| 1036 | if (oc->flags & CF_SHUTW) |
Willy Tarreau | 6fe1541 | 2013-09-29 15:16:03 +0200 | [diff] [blame] | 1037 | return; |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1038 | oc->flags |= CF_SHUTW; |
| 1039 | oc->wex = TICK_ETERNITY; |
Willy Tarreau | 43e69dc | 2018-11-06 19:23:03 +0100 | [diff] [blame] | 1040 | si_done_get(si); |
Willy Tarreau | 8b3d7df | 2013-09-29 14:51:58 +0200 | [diff] [blame] | 1041 | |
Christopher Faulet | 1d98777 | 2022-03-29 18:03:35 +0200 | [diff] [blame] | 1042 | if (tick_isset(si->cs->hcto)) { |
| 1043 | ic->rto = si->cs->hcto; |
Hongbo Long | e39683c | 2017-03-10 18:41:51 +0100 | [diff] [blame] | 1044 | ic->rex = tick_add(now_ms, ic->rto); |
| 1045 | } |
| 1046 | |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 1047 | switch (cs->state) { |
| 1048 | case CS_ST_RDY: |
| 1049 | case CS_ST_EST: |
Willy Tarreau | 8b3d7df | 2013-09-29 14:51:58 +0200 | [diff] [blame] | 1050 | /* we have to shut before closing, otherwise some short messages |
| 1051 | * may never leave the system, especially when there are remaining |
| 1052 | * unread data in the socket input buffer, or when nolinger is set. |
Christopher Faulet | 8abe712 | 2022-03-30 15:10:18 +0200 | [diff] [blame] | 1053 | * However, if CS_FL_NOLINGER is explicitly set, we know there is |
Willy Tarreau | 8b3d7df | 2013-09-29 14:51:58 +0200 | [diff] [blame] | 1054 | * no risk so we close both sides immediately. |
| 1055 | */ |
Willy Tarreau | 51d0a7e | 2019-01-31 19:09:59 +0100 | [diff] [blame] | 1056 | |
Christopher Faulet | 6cd56d5 | 2022-03-30 10:47:32 +0200 | [diff] [blame] | 1057 | if (cs->endp->flags & CS_EP_ERROR) { |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 1058 | /* quick close, the socket is already shut anyway */ |
Willy Tarreau | 8b3d7df | 2013-09-29 14:51:58 +0200 | [diff] [blame] | 1059 | } |
Christopher Faulet | 8abe712 | 2022-03-30 15:10:18 +0200 | [diff] [blame] | 1060 | else if (cs->flags & CS_FL_NOLINGER) { |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 1061 | /* unclean data-layer shutdown, typically an aborted request |
| 1062 | * or a forwarded shutdown from a client to a server due to |
| 1063 | * option abortonclose. No need for the TLS layer to try to |
| 1064 | * emit a shutdown message. |
| 1065 | */ |
Christopher Faulet | 69ef6c9 | 2022-03-31 14:20:00 +0200 | [diff] [blame] | 1066 | cs_conn_shutw(cs, CO_SHW_SILENT); |
Willy Tarreau | 8b3d7df | 2013-09-29 14:51:58 +0200 | [diff] [blame] | 1067 | } |
| 1068 | else { |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 1069 | /* clean data-layer shutdown. This only happens on the |
| 1070 | * frontend side, or on the backend side when forwarding |
| 1071 | * a client close in TCP mode or in HTTP TUNNEL mode |
| 1072 | * while option abortonclose is set. We want the TLS |
| 1073 | * layer to try to signal it to the peer before we close. |
| 1074 | */ |
Christopher Faulet | 69ef6c9 | 2022-03-31 14:20:00 +0200 | [diff] [blame] | 1075 | cs_conn_shutw(cs, CO_SHW_NORMAL); |
Willy Tarreau | 8b3d7df | 2013-09-29 14:51:58 +0200 | [diff] [blame] | 1076 | |
Willy Tarreau | a5ea751 | 2020-12-11 10:24:05 +0100 | [diff] [blame] | 1077 | if (!(ic->flags & (CF_SHUTR|CF_DONT_READ))) |
Willy Tarreau | a553ae9 | 2017-10-05 18:52:17 +0200 | [diff] [blame] | 1078 | return; |
Willy Tarreau | 8b3d7df | 2013-09-29 14:51:58 +0200 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | /* fall through */ |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 1082 | case CS_ST_CON: |
Willy Tarreau | 8b3d7df | 2013-09-29 14:51:58 +0200 | [diff] [blame] | 1083 | /* we may have to close a pending connection, and mark the |
| 1084 | * response buffer as shutr |
| 1085 | */ |
Christopher Faulet | 69ef6c9 | 2022-03-31 14:20:00 +0200 | [diff] [blame] | 1086 | cs_conn_close(cs); |
Willy Tarreau | 8b3d7df | 2013-09-29 14:51:58 +0200 | [diff] [blame] | 1087 | /* fall through */ |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 1088 | case CS_ST_CER: |
| 1089 | case CS_ST_QUE: |
| 1090 | case CS_ST_TAR: |
| 1091 | cs->state = CS_ST_DIS; |
Willy Tarreau | 4a59f2f | 2013-10-24 20:10:45 +0200 | [diff] [blame] | 1092 | /* fall through */ |
Willy Tarreau | 8b3d7df | 2013-09-29 14:51:58 +0200 | [diff] [blame] | 1093 | default: |
Christopher Faulet | 8abe712 | 2022-03-30 15:10:18 +0200 | [diff] [blame] | 1094 | cs->flags &= ~CS_FL_NOLINGER; |
Willy Tarreau | abb5d42 | 2018-11-14 16:58:52 +0100 | [diff] [blame] | 1095 | si_rx_shut_blk(si); |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1096 | ic->flags |= CF_SHUTR; |
| 1097 | ic->rex = TICK_ETERNITY; |
Christopher Faulet | ae024ce | 2022-03-29 19:02:31 +0200 | [diff] [blame] | 1098 | __cs_strm(cs)->conn_exp = TICK_ETERNITY; |
Willy Tarreau | 100c467 | 2012-08-20 12:06:26 +0200 | [diff] [blame] | 1099 | } |
| 1100 | } |
| 1101 | |
Willy Tarreau | 46a8d92 | 2012-08-20 12:38:36 +0200 | [diff] [blame] | 1102 | /* This function is used for inter-stream-interface calls. It is called by the |
| 1103 | * consumer to inform the producer side that it may be interested in checking |
| 1104 | * for free space in the buffer. Note that it intentionally does not update |
| 1105 | * timeouts, so that we can still check them later at wake-up. This function is |
| 1106 | * dedicated to connection-based stream interfaces. |
| 1107 | */ |
Willy Tarreau | c578891 | 2012-08-24 18:12:41 +0200 | [diff] [blame] | 1108 | static void stream_int_chk_rcv_conn(struct stream_interface *si) |
Willy Tarreau | 46a8d92 | 2012-08-20 12:38:36 +0200 | [diff] [blame] | 1109 | { |
Christopher Faulet | b3e0de4 | 2018-10-11 13:54:13 +0200 | [diff] [blame] | 1110 | /* (re)start reading */ |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 1111 | if (cs_state_in(si->cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST)) |
Christopher Faulet | 2f35e7b | 2022-03-31 11:09:28 +0200 | [diff] [blame] | 1112 | tasklet_wakeup(si->cs->wait_event.tasklet); |
Willy Tarreau | 46a8d92 | 2012-08-20 12:38:36 +0200 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | |
Willy Tarreau | de5722c | 2012-08-20 15:01:10 +0200 | [diff] [blame] | 1116 | /* This function is used for inter-stream-interface calls. It is called by the |
| 1117 | * producer to inform the consumer side that it may be interested in checking |
| 1118 | * for data in the buffer. Note that it intentionally does not update timeouts, |
| 1119 | * so that we can still check them later at wake-up. |
| 1120 | */ |
Willy Tarreau | c578891 | 2012-08-24 18:12:41 +0200 | [diff] [blame] | 1121 | static void stream_int_chk_snd_conn(struct stream_interface *si) |
Willy Tarreau | de5722c | 2012-08-20 15:01:10 +0200 | [diff] [blame] | 1122 | { |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1123 | struct channel *oc = si_oc(si); |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 1124 | struct conn_stream *cs = si->cs; |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 1125 | |
Willy Tarreau | 0c3205a | 2022-03-23 11:11:31 +0100 | [diff] [blame] | 1126 | BUG_ON(!cs_conn(cs)); |
Willy Tarreau | de5722c | 2012-08-20 15:01:10 +0200 | [diff] [blame] | 1127 | |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 1128 | if (unlikely(!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST) || |
Olivier Houchard | b2fc04e | 2019-04-11 13:56:26 +0200 | [diff] [blame] | 1129 | (oc->flags & CF_SHUTW))) |
Willy Tarreau | de5722c | 2012-08-20 15:01:10 +0200 | [diff] [blame] | 1130 | return; |
Willy Tarreau | de5722c | 2012-08-20 15:01:10 +0200 | [diff] [blame] | 1131 | |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1132 | if (unlikely(channel_is_empty(oc))) /* called with nothing to send ! */ |
Willy Tarreau | de5722c | 2012-08-20 15:01:10 +0200 | [diff] [blame] | 1133 | return; |
| 1134 | |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1135 | if (!oc->pipe && /* spliced data wants to be forwarded ASAP */ |
Willy Tarreau | b016587 | 2012-12-15 10:12:39 +0100 | [diff] [blame] | 1136 | !(si->flags & SI_FL_WAIT_DATA)) /* not waiting for data */ |
Willy Tarreau | de5722c | 2012-08-20 15:01:10 +0200 | [diff] [blame] | 1137 | return; |
| 1138 | |
Christopher Faulet | 2f35e7b | 2022-03-31 11:09:28 +0200 | [diff] [blame] | 1139 | if (!(si->cs->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(si_oc(si))) |
Olivier Houchard | 31f04e4 | 2018-10-22 16:01:09 +0200 | [diff] [blame] | 1140 | si_cs_send(cs); |
Willy Tarreau | 33a09a5 | 2018-10-25 13:49:49 +0200 | [diff] [blame] | 1141 | |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 1142 | if (cs->endp->flags & (CS_EP_ERROR|CS_EP_ERR_PENDING) || si_is_conn_error(si)) { |
Willy Tarreau | 3b9c850 | 2017-10-25 14:22:28 +0200 | [diff] [blame] | 1143 | /* Write error on the file descriptor */ |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 1144 | if (cs->state >= CS_ST_CON) |
Christopher Faulet | 6cd56d5 | 2022-03-30 10:47:32 +0200 | [diff] [blame] | 1145 | cs->endp->flags |= CS_EP_ERROR; |
Willy Tarreau | 3b9c850 | 2017-10-25 14:22:28 +0200 | [diff] [blame] | 1146 | goto out_wakeup; |
Willy Tarreau | de5722c | 2012-08-20 15:01:10 +0200 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | /* OK, so now we know that some data might have been sent, and that we may |
| 1150 | * have to poll first. We have to do that too if the buffer is not empty. |
| 1151 | */ |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1152 | if (channel_is_empty(oc)) { |
Willy Tarreau | de5722c | 2012-08-20 15:01:10 +0200 | [diff] [blame] | 1153 | /* the connection is established but we can't write. Either the |
| 1154 | * buffer is empty, or we just refrain from sending because the |
| 1155 | * ->o limit was reached. Maybe we just wrote the last |
| 1156 | * chunk and need to close. |
| 1157 | */ |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1158 | if (((oc->flags & (CF_SHUTW|CF_AUTO_CLOSE|CF_SHUTW_NOW)) == |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1159 | (CF_AUTO_CLOSE|CF_SHUTW_NOW)) && |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 1160 | cs_state_in(cs->state, CS_SB_RDY|CS_SB_EST)) { |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame^] | 1161 | cs_shutw(cs); |
Willy Tarreau | de5722c | 2012-08-20 15:01:10 +0200 | [diff] [blame] | 1162 | goto out_wakeup; |
| 1163 | } |
| 1164 | |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1165 | if ((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == 0) |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame^] | 1166 | cs->si->flags |= SI_FL_WAIT_DATA; |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1167 | oc->wex = TICK_ETERNITY; |
Willy Tarreau | de5722c | 2012-08-20 15:01:10 +0200 | [diff] [blame] | 1168 | } |
| 1169 | else { |
| 1170 | /* Otherwise there are remaining data to be sent in the buffer, |
| 1171 | * which means we have to poll before doing so. |
| 1172 | */ |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame^] | 1173 | cs->si->flags &= ~SI_FL_WAIT_DATA; |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1174 | if (!tick_isset(oc->wex)) |
| 1175 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
Willy Tarreau | de5722c | 2012-08-20 15:01:10 +0200 | [diff] [blame] | 1176 | } |
| 1177 | |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1178 | if (likely(oc->flags & CF_WRITE_ACTIVITY)) { |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame^] | 1179 | struct channel *ic = cs_ic(cs); |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1180 | |
Willy Tarreau | de5722c | 2012-08-20 15:01:10 +0200 | [diff] [blame] | 1181 | /* update timeout if we have written something */ |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1182 | if ((oc->flags & (CF_SHUTW|CF_WRITE_PARTIAL)) == CF_WRITE_PARTIAL && |
| 1183 | !channel_is_empty(oc)) |
| 1184 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
Willy Tarreau | de5722c | 2012-08-20 15:01:10 +0200 | [diff] [blame] | 1185 | |
Christopher Faulet | a728518 | 2022-03-30 15:43:23 +0200 | [diff] [blame] | 1186 | if (tick_isset(ic->rex) && !(cs->flags & CS_FL_INDEP_STR)) { |
Willy Tarreau | de5722c | 2012-08-20 15:01:10 +0200 | [diff] [blame] | 1187 | /* Note: to prevent the client from expiring read timeouts |
| 1188 | * during writes, we refresh it. We only do this if the |
| 1189 | * interface is not configured for "independent streams", |
| 1190 | * because for some applications it's better not to do this, |
| 1191 | * for instance when continuously exchanging small amounts |
| 1192 | * of data which can full the socket buffers long before a |
| 1193 | * write timeout is detected. |
| 1194 | */ |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1195 | ic->rex = tick_add_ifset(now_ms, ic->rto); |
Willy Tarreau | de5722c | 2012-08-20 15:01:10 +0200 | [diff] [blame] | 1196 | } |
| 1197 | } |
| 1198 | |
| 1199 | /* in case of special condition (error, shutdown, end of write...), we |
| 1200 | * have to notify the task. |
| 1201 | */ |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1202 | if (likely((oc->flags & (CF_WRITE_NULL|CF_WRITE_ERROR|CF_SHUTW)) || |
| 1203 | ((oc->flags & CF_WAKE_WRITE) && |
| 1204 | ((channel_is_empty(oc) && !oc->to_forward) || |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 1205 | !cs_state_in(cs->state, CS_SB_EST))))) { |
Willy Tarreau | de5722c | 2012-08-20 15:01:10 +0200 | [diff] [blame] | 1206 | out_wakeup: |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame^] | 1207 | if (!(cs->flags & CS_FL_DONT_WAKE)) |
| 1208 | task_wakeup(cs_strm_task(cs), TASK_WOKEN_IO); |
Willy Tarreau | de5722c | 2012-08-20 15:01:10 +0200 | [diff] [blame] | 1209 | } |
| 1210 | } |
| 1211 | |
Willy Tarreau | eecf6ca | 2012-08-20 15:09:53 +0200 | [diff] [blame] | 1212 | /* |
Willy Tarreau | ce323de | 2012-08-20 21:41:06 +0200 | [diff] [blame] | 1213 | * This is the callback which is called by the connection layer to receive data |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 1214 | * into the buffer from the connection. It iterates over the mux layer's |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 1215 | * rcv_buf function. |
Willy Tarreau | ce323de | 2012-08-20 21:41:06 +0200 | [diff] [blame] | 1216 | */ |
Christopher Faulet | 9936dc6 | 2022-02-28 09:21:58 +0100 | [diff] [blame] | 1217 | static int si_cs_recv(struct conn_stream *cs) |
Willy Tarreau | ce323de | 2012-08-20 21:41:06 +0200 | [diff] [blame] | 1218 | { |
Christopher Faulet | 693b23b | 2022-02-28 09:09:05 +0100 | [diff] [blame] | 1219 | struct connection *conn = __cs_conn(cs); |
Christopher Faulet | f835dea | 2021-12-21 14:35:17 +0100 | [diff] [blame] | 1220 | struct stream_interface *si = cs_si(cs); |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1221 | struct channel *ic = si_ic(si); |
Olivier Houchard | f653528 | 2018-08-31 17:29:12 +0200 | [diff] [blame] | 1222 | int ret, max, cur_read = 0; |
Willy Tarreau | ce323de | 2012-08-20 21:41:06 +0200 | [diff] [blame] | 1223 | int read_poll = MAX_READ_POLL_LOOPS; |
Christopher Faulet | c6618d6 | 2018-10-11 15:56:04 +0200 | [diff] [blame] | 1224 | int flags = 0; |
Willy Tarreau | ce323de | 2012-08-20 21:41:06 +0200 | [diff] [blame] | 1225 | |
Christopher Faulet | 04400bc | 2019-10-25 10:21:01 +0200 | [diff] [blame] | 1226 | /* If not established yet, do nothing. */ |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 1227 | if (cs->state != CS_ST_EST) |
Christopher Faulet | 04400bc | 2019-10-25 10:21:01 +0200 | [diff] [blame] | 1228 | return 0; |
| 1229 | |
Olivier Houchard | f653528 | 2018-08-31 17:29:12 +0200 | [diff] [blame] | 1230 | /* If another call to si_cs_recv() failed, and we subscribed to |
| 1231 | * recv events already, give up now. |
| 1232 | */ |
Christopher Faulet | 2f35e7b | 2022-03-31 11:09:28 +0200 | [diff] [blame] | 1233 | if (si->cs->wait_event.events & SUB_RETRY_RECV) |
Olivier Houchard | f653528 | 2018-08-31 17:29:12 +0200 | [diff] [blame] | 1234 | return 0; |
Willy Tarreau | ce323de | 2012-08-20 21:41:06 +0200 | [diff] [blame] | 1235 | |
Willy Tarreau | ce323de | 2012-08-20 21:41:06 +0200 | [diff] [blame] | 1236 | /* maybe we were called immediately after an asynchronous shutr */ |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1237 | if (ic->flags & CF_SHUTR) |
Olivier Houchard | c2aa711 | 2018-09-11 18:27:21 +0200 | [diff] [blame] | 1238 | return 1; |
Willy Tarreau | ce323de | 2012-08-20 21:41:06 +0200 | [diff] [blame] | 1239 | |
Christopher Faulet | e96993b | 2020-07-30 09:26:46 +0200 | [diff] [blame] | 1240 | /* we must wait because the mux is not installed yet */ |
| 1241 | if (!conn->mux) |
| 1242 | return 0; |
| 1243 | |
Willy Tarreau | 54e917c | 2017-08-30 07:35:35 +0200 | [diff] [blame] | 1244 | /* stop here if we reached the end of data */ |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 1245 | if (cs->endp->flags & CS_EP_EOS) |
Christopher Faulet | 36b536d | 2019-11-20 11:56:33 +0100 | [diff] [blame] | 1246 | goto end_recv; |
Willy Tarreau | 54e917c | 2017-08-30 07:35:35 +0200 | [diff] [blame] | 1247 | |
Christopher Faulet | f061e42 | 2018-12-07 14:51:20 +0100 | [diff] [blame] | 1248 | /* stop immediately on errors. Note that we DON'T want to stop on |
| 1249 | * POLL_ERR, as the poller might report a write error while there |
| 1250 | * are still data available in the recv buffer. This typically |
| 1251 | * happens when we send too large a request to a backend server |
| 1252 | * which rejects it before reading it all. |
| 1253 | */ |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 1254 | if (!(cs->endp->flags & CS_EP_RCV_MORE)) { |
Christopher Faulet | f061e42 | 2018-12-07 14:51:20 +0100 | [diff] [blame] | 1255 | if (!conn_xprt_ready(conn)) |
| 1256 | return 0; |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 1257 | if (cs->endp->flags & CS_EP_ERROR) |
Christopher Faulet | 36b536d | 2019-11-20 11:56:33 +0100 | [diff] [blame] | 1258 | goto end_recv; |
Christopher Faulet | f061e42 | 2018-12-07 14:51:20 +0100 | [diff] [blame] | 1259 | } |
Willy Tarreau | f26c26c | 2018-11-12 16:11:08 +0100 | [diff] [blame] | 1260 | |
Willy Tarreau | 7ab99a3 | 2018-12-18 09:15:43 +0100 | [diff] [blame] | 1261 | /* prepare to detect if the mux needs more room */ |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 1262 | cs->endp->flags &= ~CS_EP_WANT_ROOM; |
Willy Tarreau | 7ab99a3 | 2018-12-18 09:15:43 +0100 | [diff] [blame] | 1263 | |
Willy Tarreau | 77e478c | 2018-06-19 07:03:14 +0200 | [diff] [blame] | 1264 | if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) && !co_data(ic) && |
Willy Tarreau | 7e31273 | 2014-02-12 16:35:14 +0100 | [diff] [blame] | 1265 | global.tune.idle_timer && |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1266 | (unsigned short)(now_ms - ic->last_read) >= global.tune.idle_timer) { |
Willy Tarreau | c5890e6 | 2014-02-09 17:47:01 +0100 | [diff] [blame] | 1267 | /* The buffer was empty and nothing was transferred for more |
| 1268 | * than one second. This was caused by a pause and not by |
| 1269 | * congestion. Reset any streaming mode to reduce latency. |
| 1270 | */ |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1271 | ic->xfer_small = 0; |
| 1272 | ic->xfer_large = 0; |
| 1273 | ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST); |
Willy Tarreau | c5890e6 | 2014-02-09 17:47:01 +0100 | [diff] [blame] | 1274 | } |
| 1275 | |
Willy Tarreau | 96199b1 | 2012-08-24 00:46:52 +0200 | [diff] [blame] | 1276 | /* First, let's see if we may splice data across the channel without |
| 1277 | * using a buffer. |
| 1278 | */ |
Christopher Faulet | e9e4820 | 2022-03-22 18:13:29 +0100 | [diff] [blame] | 1279 | if (cs->endp->flags & CS_EP_MAY_SPLICE && |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1280 | (ic->pipe || ic->to_forward >= MIN_SPLICE_FORWARD) && |
| 1281 | ic->flags & CF_KERN_SPLICING) { |
Willy Tarreau | d760eec | 2018-07-10 09:50:25 +0200 | [diff] [blame] | 1282 | if (c_data(ic)) { |
Willy Tarreau | 96199b1 | 2012-08-24 00:46:52 +0200 | [diff] [blame] | 1283 | /* We're embarrassed, there are already data pending in |
| 1284 | * the buffer and we don't want to have them at two |
| 1285 | * locations at a time. Let's indicate we need some |
| 1286 | * place and ask the consumer to hurry. |
| 1287 | */ |
Christopher Faulet | c6618d6 | 2018-10-11 15:56:04 +0200 | [diff] [blame] | 1288 | flags |= CO_RFL_BUF_FLUSH; |
Willy Tarreau | 96199b1 | 2012-08-24 00:46:52 +0200 | [diff] [blame] | 1289 | goto abort_splice; |
| 1290 | } |
Willy Tarreau | ce323de | 2012-08-20 21:41:06 +0200 | [diff] [blame] | 1291 | |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1292 | if (unlikely(ic->pipe == NULL)) { |
| 1293 | if (pipes_used >= global.maxpipes || !(ic->pipe = get_pipe())) { |
| 1294 | ic->flags &= ~CF_KERN_SPLICING; |
Willy Tarreau | 96199b1 | 2012-08-24 00:46:52 +0200 | [diff] [blame] | 1295 | goto abort_splice; |
| 1296 | } |
| 1297 | } |
| 1298 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 1299 | ret = conn->mux->rcv_pipe(cs, ic->pipe, ic->to_forward); |
Willy Tarreau | 96199b1 | 2012-08-24 00:46:52 +0200 | [diff] [blame] | 1300 | if (ret < 0) { |
| 1301 | /* splice not supported on this end, let's disable it */ |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1302 | ic->flags &= ~CF_KERN_SPLICING; |
Willy Tarreau | 96199b1 | 2012-08-24 00:46:52 +0200 | [diff] [blame] | 1303 | goto abort_splice; |
| 1304 | } |
Willy Tarreau | ce323de | 2012-08-20 21:41:06 +0200 | [diff] [blame] | 1305 | |
Willy Tarreau | 96199b1 | 2012-08-24 00:46:52 +0200 | [diff] [blame] | 1306 | if (ret > 0) { |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1307 | if (ic->to_forward != CHN_INFINITE_FORWARD) |
| 1308 | ic->to_forward -= ret; |
| 1309 | ic->total += ret; |
Willy Tarreau | 96199b1 | 2012-08-24 00:46:52 +0200 | [diff] [blame] | 1310 | cur_read += ret; |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1311 | ic->flags |= CF_READ_PARTIAL; |
Willy Tarreau | ce323de | 2012-08-20 21:41:06 +0200 | [diff] [blame] | 1312 | } |
Willy Tarreau | 96199b1 | 2012-08-24 00:46:52 +0200 | [diff] [blame] | 1313 | |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 1314 | if (cs->endp->flags & (CS_EP_EOS|CS_EP_ERROR)) |
Christopher Faulet | 36b536d | 2019-11-20 11:56:33 +0100 | [diff] [blame] | 1315 | goto end_recv; |
Willy Tarreau | 96199b1 | 2012-08-24 00:46:52 +0200 | [diff] [blame] | 1316 | |
Willy Tarreau | 61d39a0 | 2013-07-18 21:49:32 +0200 | [diff] [blame] | 1317 | if (conn->flags & CO_FL_WAIT_ROOM) { |
| 1318 | /* the pipe is full or we have read enough data that it |
| 1319 | * could soon be full. Let's stop before needing to poll. |
| 1320 | */ |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 1321 | si_rx_room_blk(si); |
Willy Tarreau | ffb1205 | 2018-11-15 16:06:02 +0100 | [diff] [blame] | 1322 | goto done_recv; |
Willy Tarreau | 61d39a0 | 2013-07-18 21:49:32 +0200 | [diff] [blame] | 1323 | } |
Willy Tarreau | 56a77e5 | 2012-09-02 18:34:44 +0200 | [diff] [blame] | 1324 | |
Willy Tarreau | ce323de | 2012-08-20 21:41:06 +0200 | [diff] [blame] | 1325 | /* splice not possible (anymore), let's go on on standard copy */ |
| 1326 | } |
Willy Tarreau | 96199b1 | 2012-08-24 00:46:52 +0200 | [diff] [blame] | 1327 | |
| 1328 | abort_splice: |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1329 | if (ic->pipe && unlikely(!ic->pipe->data)) { |
| 1330 | put_pipe(ic->pipe); |
| 1331 | ic->pipe = NULL; |
Willy Tarreau | 96199b1 | 2012-08-24 00:46:52 +0200 | [diff] [blame] | 1332 | } |
| 1333 | |
Christopher Faulet | e9e4820 | 2022-03-22 18:13:29 +0100 | [diff] [blame] | 1334 | if (ic->pipe && ic->to_forward && !(flags & CO_RFL_BUF_FLUSH) && cs->endp->flags & CS_EP_MAY_SPLICE) { |
Willy Tarreau | c640ef1 | 2019-12-03 18:13:04 +0100 | [diff] [blame] | 1335 | /* don't break splicing by reading, but still call rcv_buf() |
| 1336 | * to pass the flag. |
| 1337 | */ |
| 1338 | goto done_recv; |
| 1339 | } |
| 1340 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 1341 | /* now we'll need a input buffer for the stream */ |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 1342 | if (!si_alloc_ibuf(si, &(si_strm(si)->buffer_wait))) |
Willy Tarreau | 10fc09e | 2014-11-25 19:46:36 +0100 | [diff] [blame] | 1343 | goto end_recv; |
Willy Tarreau | 10fc09e | 2014-11-25 19:46:36 +0100 | [diff] [blame] | 1344 | |
Christopher Faulet | 2bc364c | 2021-09-21 15:22:12 +0200 | [diff] [blame] | 1345 | /* For an HTX stream, if the buffer is stuck (no output data with some |
| 1346 | * input data) and if the HTX message is fragmented or if its free space |
| 1347 | * wraps, we force an HTX deframentation. It is a way to have a |
| 1348 | * contiguous free space nad to let the mux to copy as much data as |
| 1349 | * possible. |
| 1350 | * |
| 1351 | * NOTE: A possible optim may be to let the mux decides if defrag is |
| 1352 | * required or not, depending on amount of data to be xferred. |
| 1353 | */ |
| 1354 | if (IS_HTX_STRM(si_strm(si)) && !co_data(ic)) { |
| 1355 | struct htx *htx = htxbuf(&ic->buf); |
| 1356 | |
| 1357 | if (htx_is_not_empty(htx) && ((htx->flags & HTX_FL_FRAGMENTED) || htx_space_wraps(htx))) |
| 1358 | htx_defrag(htxbuf(&ic->buf), NULL, 0); |
| 1359 | } |
Christopher Faulet | 68a14db | 2021-09-21 15:14:57 +0200 | [diff] [blame] | 1360 | |
| 1361 | /* Instruct the mux it must subscribed for read events */ |
| 1362 | flags |= ((!conn_is_back(conn) && (si_strm(si)->be->options & PR_O_ABRT_CLOSE)) ? CO_RFL_KEEP_RECV : 0); |
| 1363 | |
Willy Tarreau | 61d39a0 | 2013-07-18 21:49:32 +0200 | [diff] [blame] | 1364 | /* Important note : if we're called with POLL_IN|POLL_HUP, it means the read polling |
| 1365 | * was enabled, which implies that the recv buffer was not full. So we have a guarantee |
| 1366 | * that if such an event is not handled above in splice, it will be handled here by |
| 1367 | * recv(). |
| 1368 | */ |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 1369 | while ((cs->endp->flags & CS_EP_RCV_MORE) || |
Willy Tarreau | d1480cc | 2022-03-17 16:19:09 +0100 | [diff] [blame] | 1370 | (!(conn->flags & CO_FL_HANDSHAKE) && |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 1371 | (!(cs->endp->flags & (CS_EP_ERROR|CS_EP_EOS))) && !(ic->flags & CF_SHUTR))) { |
Christopher Faulet | 68a14db | 2021-09-21 15:14:57 +0200 | [diff] [blame] | 1372 | int cur_flags = flags; |
| 1373 | |
| 1374 | /* Compute transient CO_RFL_* flags */ |
Christopher Faulet | 564e39c | 2021-09-21 15:50:55 +0200 | [diff] [blame] | 1375 | if (co_data(ic)) { |
| 1376 | cur_flags |= (CO_RFL_BUF_WET | CO_RFL_BUF_NOT_STUCK); |
| 1377 | } |
Christopher Faulet | 68a14db | 2021-09-21 15:14:57 +0200 | [diff] [blame] | 1378 | |
Christopher Faulet | 4eb7d74 | 2018-10-11 15:29:21 +0200 | [diff] [blame] | 1379 | /* <max> may be null. This is the mux responsibility to set |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 1380 | * CS_EP_RCV_MORE on the CS if more space is needed. |
Christopher Faulet | 4eb7d74 | 2018-10-11 15:29:21 +0200 | [diff] [blame] | 1381 | */ |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1382 | max = channel_recv_max(ic); |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 1383 | ret = conn->mux->rcv_buf(cs, &ic->buf, max, cur_flags); |
Willy Tarreau | 674e0ad | 2018-12-05 13:45:41 +0100 | [diff] [blame] | 1384 | |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 1385 | if (cs->endp->flags & CS_EP_WANT_ROOM) { |
| 1386 | /* CS_EP_WANT_ROOM must not be reported if the channel's |
Christopher Faulet | ae17925 | 2022-02-21 16:12:00 +0100 | [diff] [blame] | 1387 | * buffer is empty. |
| 1388 | */ |
| 1389 | BUG_ON(c_empty(ic)); |
| 1390 | |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 1391 | si_rx_room_blk(si); |
Christopher Faulet | df99408 | 2021-09-23 14:17:20 +0200 | [diff] [blame] | 1392 | /* Add READ_PARTIAL because some data are pending but |
| 1393 | * cannot be xferred to the channel |
| 1394 | */ |
| 1395 | ic->flags |= CF_READ_PARTIAL; |
| 1396 | } |
Willy Tarreau | 6577b48 | 2017-12-10 21:19:33 +0100 | [diff] [blame] | 1397 | |
Willy Tarreau | f26c26c | 2018-11-12 16:11:08 +0100 | [diff] [blame] | 1398 | if (ret <= 0) { |
Willy Tarreau | 1ac5f20 | 2019-12-03 18:08:45 +0100 | [diff] [blame] | 1399 | /* if we refrained from reading because we asked for a |
| 1400 | * flush to satisfy rcv_pipe(), we must not subscribe |
| 1401 | * and instead report that there's not enough room |
| 1402 | * here to proceed. |
| 1403 | */ |
| 1404 | if (flags & CO_RFL_BUF_FLUSH) |
| 1405 | si_rx_room_blk(si); |
Willy Tarreau | ce323de | 2012-08-20 21:41:06 +0200 | [diff] [blame] | 1406 | break; |
Willy Tarreau | f26c26c | 2018-11-12 16:11:08 +0100 | [diff] [blame] | 1407 | } |
Willy Tarreau | ce323de | 2012-08-20 21:41:06 +0200 | [diff] [blame] | 1408 | |
| 1409 | cur_read += ret; |
| 1410 | |
| 1411 | /* if we're allowed to directly forward data, we must update ->o */ |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1412 | if (ic->to_forward && !(ic->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
Willy Tarreau | ce323de | 2012-08-20 21:41:06 +0200 | [diff] [blame] | 1413 | unsigned long fwd = ret; |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1414 | if (ic->to_forward != CHN_INFINITE_FORWARD) { |
| 1415 | if (fwd > ic->to_forward) |
| 1416 | fwd = ic->to_forward; |
| 1417 | ic->to_forward -= fwd; |
Willy Tarreau | ce323de | 2012-08-20 21:41:06 +0200 | [diff] [blame] | 1418 | } |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 1419 | c_adv(ic, fwd); |
Willy Tarreau | ce323de | 2012-08-20 21:41:06 +0200 | [diff] [blame] | 1420 | } |
| 1421 | |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1422 | ic->flags |= CF_READ_PARTIAL; |
| 1423 | ic->total += ret; |
Willy Tarreau | ce323de | 2012-08-20 21:41:06 +0200 | [diff] [blame] | 1424 | |
Christopher Faulet | 883d83e | 2021-09-09 10:17:59 +0200 | [diff] [blame] | 1425 | /* End-of-input reached, we can leave. In this case, it is |
| 1426 | * important to break the loop to not block the SI because of |
| 1427 | * the channel's policies.This way, we are still able to receive |
| 1428 | * shutdowns. |
| 1429 | */ |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 1430 | if (cs->endp->flags & CS_EP_EOI) |
Christopher Faulet | 883d83e | 2021-09-09 10:17:59 +0200 | [diff] [blame] | 1431 | break; |
| 1432 | |
Willy Tarreau | f26c26c | 2018-11-12 16:11:08 +0100 | [diff] [blame] | 1433 | if ((ic->flags & CF_READ_DONTWAIT) || --read_poll <= 0) { |
| 1434 | /* we're stopped by the channel's policy */ |
Willy Tarreau | b26a6f9 | 2018-11-14 17:10:36 +0100 | [diff] [blame] | 1435 | si_rx_chan_blk(si); |
Willy Tarreau | 62dd698 | 2017-11-18 11:26:20 +0100 | [diff] [blame] | 1436 | break; |
Willy Tarreau | f26c26c | 2018-11-12 16:11:08 +0100 | [diff] [blame] | 1437 | } |
Willy Tarreau | ce323de | 2012-08-20 21:41:06 +0200 | [diff] [blame] | 1438 | |
| 1439 | /* if too many bytes were missing from last read, it means that |
| 1440 | * it's pointless trying to read again because the system does |
| 1441 | * not have them in buffers. |
| 1442 | */ |
| 1443 | if (ret < max) { |
Willy Tarreau | ce323de | 2012-08-20 21:41:06 +0200 | [diff] [blame] | 1444 | /* if a streamer has read few data, it may be because we |
| 1445 | * have exhausted system buffers. It's not worth trying |
| 1446 | * again. |
| 1447 | */ |
Willy Tarreau | f26c26c | 2018-11-12 16:11:08 +0100 | [diff] [blame] | 1448 | if (ic->flags & CF_STREAMER) { |
| 1449 | /* we're stopped by the channel's policy */ |
Willy Tarreau | b26a6f9 | 2018-11-14 17:10:36 +0100 | [diff] [blame] | 1450 | si_rx_chan_blk(si); |
Willy Tarreau | ce323de | 2012-08-20 21:41:06 +0200 | [diff] [blame] | 1451 | break; |
Willy Tarreau | f26c26c | 2018-11-12 16:11:08 +0100 | [diff] [blame] | 1452 | } |
Willy Tarreau | ce323de | 2012-08-20 21:41:06 +0200 | [diff] [blame] | 1453 | |
| 1454 | /* if we read a large block smaller than what we requested, |
| 1455 | * it's almost certain we'll never get anything more. |
| 1456 | */ |
Willy Tarreau | f26c26c | 2018-11-12 16:11:08 +0100 | [diff] [blame] | 1457 | if (ret >= global.tune.recv_enough) { |
| 1458 | /* we're stopped by the channel's policy */ |
Willy Tarreau | b26a6f9 | 2018-11-14 17:10:36 +0100 | [diff] [blame] | 1459 | si_rx_chan_blk(si); |
Willy Tarreau | ce323de | 2012-08-20 21:41:06 +0200 | [diff] [blame] | 1460 | break; |
Willy Tarreau | f26c26c | 2018-11-12 16:11:08 +0100 | [diff] [blame] | 1461 | } |
Willy Tarreau | ce323de | 2012-08-20 21:41:06 +0200 | [diff] [blame] | 1462 | } |
Christopher Faulet | b3e0de4 | 2018-10-11 13:54:13 +0200 | [diff] [blame] | 1463 | |
| 1464 | /* if we are waiting for more space, don't try to read more data |
| 1465 | * right now. |
| 1466 | */ |
Willy Tarreau | b26a6f9 | 2018-11-14 17:10:36 +0100 | [diff] [blame] | 1467 | if (si_rx_blocked(si)) |
Christopher Faulet | b3e0de4 | 2018-10-11 13:54:13 +0200 | [diff] [blame] | 1468 | break; |
Willy Tarreau | ce323de | 2012-08-20 21:41:06 +0200 | [diff] [blame] | 1469 | } /* while !flags */ |
| 1470 | |
Willy Tarreau | ffb1205 | 2018-11-15 16:06:02 +0100 | [diff] [blame] | 1471 | done_recv: |
Willy Tarreau | c5890e6 | 2014-02-09 17:47:01 +0100 | [diff] [blame] | 1472 | if (cur_read) { |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1473 | if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) && |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1474 | (cur_read <= ic->buf.size / 2)) { |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1475 | ic->xfer_large = 0; |
| 1476 | ic->xfer_small++; |
| 1477 | if (ic->xfer_small >= 3) { |
Willy Tarreau | c5890e6 | 2014-02-09 17:47:01 +0100 | [diff] [blame] | 1478 | /* we have read less than half of the buffer in |
| 1479 | * one pass, and this happened at least 3 times. |
| 1480 | * This is definitely not a streamer. |
| 1481 | */ |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1482 | ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST); |
Willy Tarreau | c5890e6 | 2014-02-09 17:47:01 +0100 | [diff] [blame] | 1483 | } |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1484 | else if (ic->xfer_small >= 2) { |
Willy Tarreau | c5890e6 | 2014-02-09 17:47:01 +0100 | [diff] [blame] | 1485 | /* if the buffer has been at least half full twice, |
| 1486 | * we receive faster than we send, so at least it |
| 1487 | * is not a "fast streamer". |
| 1488 | */ |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1489 | ic->flags &= ~CF_STREAMER_FAST; |
Willy Tarreau | c5890e6 | 2014-02-09 17:47:01 +0100 | [diff] [blame] | 1490 | } |
| 1491 | } |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1492 | else if (!(ic->flags & CF_STREAMER_FAST) && |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1493 | (cur_read >= ic->buf.size - global.tune.maxrewrite)) { |
Willy Tarreau | c5890e6 | 2014-02-09 17:47:01 +0100 | [diff] [blame] | 1494 | /* we read a full buffer at once */ |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1495 | ic->xfer_small = 0; |
| 1496 | ic->xfer_large++; |
| 1497 | if (ic->xfer_large >= 3) { |
Willy Tarreau | c5890e6 | 2014-02-09 17:47:01 +0100 | [diff] [blame] | 1498 | /* we call this buffer a fast streamer if it manages |
| 1499 | * to be filled in one call 3 consecutive times. |
| 1500 | */ |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1501 | ic->flags |= (CF_STREAMER | CF_STREAMER_FAST); |
Willy Tarreau | c5890e6 | 2014-02-09 17:47:01 +0100 | [diff] [blame] | 1502 | } |
| 1503 | } |
| 1504 | else { |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1505 | ic->xfer_small = 0; |
| 1506 | ic->xfer_large = 0; |
Willy Tarreau | c5890e6 | 2014-02-09 17:47:01 +0100 | [diff] [blame] | 1507 | } |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1508 | ic->last_read = now_ms; |
Willy Tarreau | c5890e6 | 2014-02-09 17:47:01 +0100 | [diff] [blame] | 1509 | } |
| 1510 | |
Willy Tarreau | 10fc09e | 2014-11-25 19:46:36 +0100 | [diff] [blame] | 1511 | end_recv: |
Christopher Faulet | e6d8cb1 | 2019-11-20 16:42:00 +0100 | [diff] [blame] | 1512 | ret = (cur_read != 0); |
| 1513 | |
Christopher Faulet | 36b536d | 2019-11-20 11:56:33 +0100 | [diff] [blame] | 1514 | /* Report EOI on the channel if it was reached from the mux point of |
| 1515 | * view. */ |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 1516 | if ((cs->endp->flags & CS_EP_EOI) && !(ic->flags & CF_EOI)) { |
Christopher Faulet | 36b536d | 2019-11-20 11:56:33 +0100 | [diff] [blame] | 1517 | ic->flags |= (CF_EOI|CF_READ_PARTIAL); |
Christopher Faulet | e6d8cb1 | 2019-11-20 16:42:00 +0100 | [diff] [blame] | 1518 | ret = 1; |
| 1519 | } |
Willy Tarreau | 10fc09e | 2014-11-25 19:46:36 +0100 | [diff] [blame] | 1520 | |
Christopher Faulet | 6cd56d5 | 2022-03-30 10:47:32 +0200 | [diff] [blame] | 1521 | if (cs->endp->flags & CS_EP_ERROR) |
Christopher Faulet | e6d8cb1 | 2019-11-20 16:42:00 +0100 | [diff] [blame] | 1522 | ret = 1; |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 1523 | else if (cs->endp->flags & CS_EP_EOS) { |
Willy Tarreau | 18955db | 2020-01-23 16:32:24 +0100 | [diff] [blame] | 1524 | /* we received a shutdown */ |
| 1525 | ic->flags |= CF_READ_NULL; |
| 1526 | if (ic->flags & CF_AUTO_CLOSE) |
| 1527 | channel_shutw_now(ic); |
| 1528 | stream_int_read0(si); |
Christopher Faulet | e6d8cb1 | 2019-11-20 16:42:00 +0100 | [diff] [blame] | 1529 | ret = 1; |
Christopher Faulet | 36b536d | 2019-11-20 11:56:33 +0100 | [diff] [blame] | 1530 | } |
| 1531 | else if (!si_rx_blocked(si)) { |
| 1532 | /* Subscribe to receive events if we're blocking on I/O */ |
Christopher Faulet | 2f35e7b | 2022-03-31 11:09:28 +0200 | [diff] [blame] | 1533 | conn->mux->subscribe(cs, SUB_RETRY_RECV, &si->cs->wait_event); |
Willy Tarreau | dd5621a | 2018-11-15 16:55:14 +0100 | [diff] [blame] | 1534 | si_rx_endp_done(si); |
| 1535 | } else { |
| 1536 | si_rx_endp_more(si); |
Christopher Faulet | e6d8cb1 | 2019-11-20 16:42:00 +0100 | [diff] [blame] | 1537 | ret = 1; |
Willy Tarreau | dd5621a | 2018-11-15 16:55:14 +0100 | [diff] [blame] | 1538 | } |
Christopher Faulet | e6d8cb1 | 2019-11-20 16:42:00 +0100 | [diff] [blame] | 1539 | return ret; |
Willy Tarreau | ce323de | 2012-08-20 21:41:06 +0200 | [diff] [blame] | 1540 | } |
| 1541 | |
| 1542 | /* |
Willy Tarreau | 9bf9c14 | 2012-08-20 15:38:41 +0200 | [diff] [blame] | 1543 | * This function propagates a null read received on a socket-based connection. |
Christopher Faulet | 8abe712 | 2022-03-30 15:10:18 +0200 | [diff] [blame] | 1544 | * It updates the stream interface. If the stream interface has CS_FL_NOHALF, |
Willy Tarreau | 1140512 | 2015-03-12 22:32:27 +0100 | [diff] [blame] | 1545 | * the close is also forwarded to the write side as an abort. |
Willy Tarreau | 9bf9c14 | 2012-08-20 15:38:41 +0200 | [diff] [blame] | 1546 | */ |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1547 | static void stream_int_read0(struct stream_interface *si) |
Willy Tarreau | 9bf9c14 | 2012-08-20 15:38:41 +0200 | [diff] [blame] | 1548 | { |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 1549 | struct conn_stream *cs = si->cs; |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1550 | struct channel *ic = si_ic(si); |
| 1551 | struct channel *oc = si_oc(si); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1552 | |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 1553 | BUG_ON(!cs_conn(cs)); |
| 1554 | |
Willy Tarreau | abb5d42 | 2018-11-14 16:58:52 +0100 | [diff] [blame] | 1555 | si_rx_shut_blk(si); |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1556 | if (ic->flags & CF_SHUTR) |
Willy Tarreau | 9bf9c14 | 2012-08-20 15:38:41 +0200 | [diff] [blame] | 1557 | return; |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1558 | ic->flags |= CF_SHUTR; |
| 1559 | ic->rex = TICK_ETERNITY; |
Willy Tarreau | 9bf9c14 | 2012-08-20 15:38:41 +0200 | [diff] [blame] | 1560 | |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 1561 | if (!cs_state_in(cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST)) |
Willy Tarreau | 9bf9c14 | 2012-08-20 15:38:41 +0200 | [diff] [blame] | 1562 | return; |
| 1563 | |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1564 | if (oc->flags & CF_SHUTW) |
Willy Tarreau | 9bf9c14 | 2012-08-20 15:38:41 +0200 | [diff] [blame] | 1565 | goto do_close; |
| 1566 | |
Christopher Faulet | 8abe712 | 2022-03-30 15:10:18 +0200 | [diff] [blame] | 1567 | if (cs->flags & CS_FL_NOHALF) { |
Willy Tarreau | 9bf9c14 | 2012-08-20 15:38:41 +0200 | [diff] [blame] | 1568 | /* we want to immediately forward this close to the write side */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1569 | /* force flag on ssl to keep stream in cache */ |
Christopher Faulet | 69ef6c9 | 2022-03-31 14:20:00 +0200 | [diff] [blame] | 1570 | cs_conn_shutw(cs, CO_SHW_SILENT); |
Willy Tarreau | 9bf9c14 | 2012-08-20 15:38:41 +0200 | [diff] [blame] | 1571 | goto do_close; |
| 1572 | } |
| 1573 | |
| 1574 | /* otherwise that's just a normal read shutdown */ |
Willy Tarreau | 9bf9c14 | 2012-08-20 15:38:41 +0200 | [diff] [blame] | 1575 | return; |
| 1576 | |
| 1577 | do_close: |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame^] | 1578 | /* OK we completely close the socket here just as if we went through cs_shut[rw]() */ |
Christopher Faulet | 69ef6c9 | 2022-03-31 14:20:00 +0200 | [diff] [blame] | 1579 | cs_conn_close(cs); |
Willy Tarreau | f9fbfe8 | 2012-11-21 21:51:53 +0100 | [diff] [blame] | 1580 | |
Willy Tarreau | afc8a22 | 2014-11-28 15:46:27 +0100 | [diff] [blame] | 1581 | oc->flags &= ~CF_SHUTW_NOW; |
| 1582 | oc->flags |= CF_SHUTW; |
| 1583 | oc->wex = TICK_ETERNITY; |
Willy Tarreau | f9fbfe8 | 2012-11-21 21:51:53 +0100 | [diff] [blame] | 1584 | |
Willy Tarreau | 43e69dc | 2018-11-06 19:23:03 +0100 | [diff] [blame] | 1585 | si_done_get(si); |
Willy Tarreau | f9fbfe8 | 2012-11-21 21:51:53 +0100 | [diff] [blame] | 1586 | |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 1587 | cs->state = CS_ST_DIS; |
Christopher Faulet | ae024ce | 2022-03-29 19:02:31 +0200 | [diff] [blame] | 1588 | __cs_strm(cs)->conn_exp = TICK_ETERNITY; |
Willy Tarreau | 9bf9c14 | 2012-08-20 15:38:41 +0200 | [diff] [blame] | 1589 | return; |
| 1590 | } |
| 1591 | |
Willy Tarreau | 651e182 | 2015-09-23 20:06:13 +0200 | [diff] [blame] | 1592 | /* Callback to be used by applet handlers upon completion. It updates the stream |
| 1593 | * (which may or may not take this opportunity to try to forward data), then |
Emeric Brun | 2802b07 | 2017-06-30 14:11:56 +0200 | [diff] [blame] | 1594 | * may re-enable the applet's based on the channels and stream interface's final |
Willy Tarreau | 651e182 | 2015-09-23 20:06:13 +0200 | [diff] [blame] | 1595 | * states. |
| 1596 | */ |
Willy Tarreau | aa977ba | 2015-09-25 11:45:06 +0200 | [diff] [blame] | 1597 | void si_applet_wake_cb(struct stream_interface *si) |
Willy Tarreau | e5f8649 | 2015-04-19 15:16:35 +0200 | [diff] [blame] | 1598 | { |
Willy Tarreau | eca572f | 2015-09-25 19:11:55 +0200 | [diff] [blame] | 1599 | struct channel *ic = si_ic(si); |
| 1600 | |
Christopher Faulet | 8bc1759 | 2022-02-28 17:27:09 +0100 | [diff] [blame] | 1601 | BUG_ON(!cs_appctx(si->cs)); |
Christopher Faulet | 693b23b | 2022-02-28 09:09:05 +0100 | [diff] [blame] | 1602 | |
Willy Tarreau | eca572f | 2015-09-25 19:11:55 +0200 | [diff] [blame] | 1603 | /* If the applet wants to write and the channel is closed, it's a |
| 1604 | * broken pipe and it must be reported. |
| 1605 | */ |
Willy Tarreau | 05b9b64 | 2018-11-14 13:43:35 +0100 | [diff] [blame] | 1606 | if (!(si->flags & SI_FL_RX_WAIT_EP) && (ic->flags & CF_SHUTR)) |
Christopher Faulet | 6cd56d5 | 2022-03-30 10:47:32 +0200 | [diff] [blame] | 1607 | si->cs->endp->flags |= CS_EP_ERROR; |
Willy Tarreau | eca572f | 2015-09-25 19:11:55 +0200 | [diff] [blame] | 1608 | |
Willy Tarreau | 186dcdd | 2018-11-16 16:18:34 +0100 | [diff] [blame] | 1609 | /* automatically mark the applet having data available if it reported |
| 1610 | * begin blocked by the channel. |
| 1611 | */ |
| 1612 | if (si_rx_blocked(si)) |
| 1613 | si_rx_endp_more(si); |
| 1614 | |
Willy Tarreau | 651e182 | 2015-09-23 20:06:13 +0200 | [diff] [blame] | 1615 | /* update the stream-int, channels, and possibly wake the stream up */ |
| 1616 | stream_int_notify(si); |
Willy Tarreau | a64c703 | 2019-08-01 14:17:02 +0200 | [diff] [blame] | 1617 | stream_release_buffers(si_strm(si)); |
Willy Tarreau | e5f8649 | 2015-04-19 15:16:35 +0200 | [diff] [blame] | 1618 | |
Willy Tarreau | 32742fd | 2018-11-14 14:07:59 +0100 | [diff] [blame] | 1619 | /* stream_int_notify may have passed through chk_snd and released some |
| 1620 | * RXBLK flags. Process_stream will consider those flags to wake up the |
| 1621 | * appctx but in the case the task is not in runqueue we may have to |
| 1622 | * wakeup the appctx immediately. |
Emeric Brun | 2802b07 | 2017-06-30 14:11:56 +0200 | [diff] [blame] | 1623 | */ |
Olivier Houchard | 51205a1 | 2019-04-17 19:29:35 +0200 | [diff] [blame] | 1624 | if ((si_rx_endp_ready(si) && !si_rx_blocked(si)) || |
| 1625 | (si_tx_endp_ready(si) && !si_tx_blocked(si))) |
Christopher Faulet | 693b23b | 2022-02-28 09:09:05 +0100 | [diff] [blame] | 1626 | appctx_wakeup(__cs_appctx(si->cs)); |
Willy Tarreau | d45b9f8 | 2015-04-13 16:30:14 +0200 | [diff] [blame] | 1627 | } |
| 1628 | |
| 1629 | /* |
| 1630 | * This function performs a shutdown-read on a stream interface attached to an |
| 1631 | * applet in a connected or init state (it does nothing for other states). It |
| 1632 | * either shuts the read side or marks itself as closed. The buffer flags are |
Christopher Faulet | 8abe712 | 2022-03-30 15:10:18 +0200 | [diff] [blame] | 1633 | * updated to reflect the new state. If the stream interface has CS_FL_NOHALF, |
Willy Tarreau | d45b9f8 | 2015-04-13 16:30:14 +0200 | [diff] [blame] | 1634 | * we also forward the close to the write side. The owner task is woken up if |
| 1635 | * it exists. |
| 1636 | */ |
| 1637 | static void stream_int_shutr_applet(struct stream_interface *si) |
| 1638 | { |
| 1639 | struct channel *ic = si_ic(si); |
| 1640 | |
Christopher Faulet | 8bc1759 | 2022-02-28 17:27:09 +0100 | [diff] [blame] | 1641 | BUG_ON(!cs_appctx(si->cs)); |
Christopher Faulet | 693b23b | 2022-02-28 09:09:05 +0100 | [diff] [blame] | 1642 | |
Willy Tarreau | abb5d42 | 2018-11-14 16:58:52 +0100 | [diff] [blame] | 1643 | si_rx_shut_blk(si); |
Willy Tarreau | d45b9f8 | 2015-04-13 16:30:14 +0200 | [diff] [blame] | 1644 | if (ic->flags & CF_SHUTR) |
| 1645 | return; |
| 1646 | ic->flags |= CF_SHUTR; |
| 1647 | ic->rex = TICK_ETERNITY; |
Willy Tarreau | d45b9f8 | 2015-04-13 16:30:14 +0200 | [diff] [blame] | 1648 | |
Willy Tarreau | 828824a | 2015-04-19 17:20:03 +0200 | [diff] [blame] | 1649 | /* Note: on shutr, we don't call the applet */ |
| 1650 | |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 1651 | if (!cs_state_in(si->cs->state, CS_SB_CON|CS_SB_RDY|CS_SB_EST)) |
Willy Tarreau | d45b9f8 | 2015-04-13 16:30:14 +0200 | [diff] [blame] | 1652 | return; |
| 1653 | |
| 1654 | if (si_oc(si)->flags & CF_SHUTW) { |
Willy Tarreau | 958f074 | 2015-09-25 20:24:26 +0200 | [diff] [blame] | 1655 | si_applet_release(si); |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 1656 | si->cs->state = CS_ST_DIS; |
Christopher Faulet | ae024ce | 2022-03-29 19:02:31 +0200 | [diff] [blame] | 1657 | __cs_strm(si->cs)->conn_exp = TICK_ETERNITY; |
Willy Tarreau | d45b9f8 | 2015-04-13 16:30:14 +0200 | [diff] [blame] | 1658 | } |
Christopher Faulet | 8abe712 | 2022-03-30 15:10:18 +0200 | [diff] [blame] | 1659 | else if (si->cs->flags & CS_FL_NOHALF) { |
Willy Tarreau | d45b9f8 | 2015-04-13 16:30:14 +0200 | [diff] [blame] | 1660 | /* we want to immediately forward this close to the write side */ |
| 1661 | return stream_int_shutw_applet(si); |
| 1662 | } |
Willy Tarreau | d45b9f8 | 2015-04-13 16:30:14 +0200 | [diff] [blame] | 1663 | } |
| 1664 | |
| 1665 | /* |
| 1666 | * This function performs a shutdown-write on a stream interface attached to an |
| 1667 | * applet in a connected or init state (it does nothing for other states). It |
| 1668 | * either shuts the write side or marks itself as closed. The buffer flags are |
| 1669 | * updated to reflect the new state. It does also close everything if the SI |
| 1670 | * was marked as being in error state. The owner task is woken up if it exists. |
| 1671 | */ |
| 1672 | static void stream_int_shutw_applet(struct stream_interface *si) |
| 1673 | { |
| 1674 | struct channel *ic = si_ic(si); |
| 1675 | struct channel *oc = si_oc(si); |
| 1676 | |
Christopher Faulet | 8bc1759 | 2022-02-28 17:27:09 +0100 | [diff] [blame] | 1677 | BUG_ON(!cs_appctx(si->cs)); |
Christopher Faulet | 693b23b | 2022-02-28 09:09:05 +0100 | [diff] [blame] | 1678 | |
Willy Tarreau | d45b9f8 | 2015-04-13 16:30:14 +0200 | [diff] [blame] | 1679 | oc->flags &= ~CF_SHUTW_NOW; |
| 1680 | if (oc->flags & CF_SHUTW) |
| 1681 | return; |
| 1682 | oc->flags |= CF_SHUTW; |
| 1683 | oc->wex = TICK_ETERNITY; |
Willy Tarreau | 43e69dc | 2018-11-06 19:23:03 +0100 | [diff] [blame] | 1684 | si_done_get(si); |
Willy Tarreau | d45b9f8 | 2015-04-13 16:30:14 +0200 | [diff] [blame] | 1685 | |
Christopher Faulet | 1d98777 | 2022-03-29 18:03:35 +0200 | [diff] [blame] | 1686 | if (tick_isset(si->cs->hcto)) { |
| 1687 | ic->rto = si->cs->hcto; |
Hongbo Long | e39683c | 2017-03-10 18:41:51 +0100 | [diff] [blame] | 1688 | ic->rex = tick_add(now_ms, ic->rto); |
| 1689 | } |
| 1690 | |
Willy Tarreau | 828824a | 2015-04-19 17:20:03 +0200 | [diff] [blame] | 1691 | /* on shutw we always wake the applet up */ |
Christopher Faulet | 693b23b | 2022-02-28 09:09:05 +0100 | [diff] [blame] | 1692 | appctx_wakeup(__cs_appctx(si->cs)); |
Willy Tarreau | 828824a | 2015-04-19 17:20:03 +0200 | [diff] [blame] | 1693 | |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 1694 | switch (si->cs->state) { |
| 1695 | case CS_ST_RDY: |
| 1696 | case CS_ST_EST: |
Willy Tarreau | d45b9f8 | 2015-04-13 16:30:14 +0200 | [diff] [blame] | 1697 | /* we have to shut before closing, otherwise some short messages |
| 1698 | * may never leave the system, especially when there are remaining |
| 1699 | * unread data in the socket input buffer, or when nolinger is set. |
Christopher Faulet | 8abe712 | 2022-03-30 15:10:18 +0200 | [diff] [blame] | 1700 | * However, if CS_FL_NOLINGER is explicitly set, we know there is |
Willy Tarreau | d45b9f8 | 2015-04-13 16:30:14 +0200 | [diff] [blame] | 1701 | * no risk so we close both sides immediately. |
| 1702 | */ |
Christopher Faulet | 8abe712 | 2022-03-30 15:10:18 +0200 | [diff] [blame] | 1703 | if (!(si->cs->endp->flags & CS_EP_ERROR) && !(si->cs->flags & CS_FL_NOLINGER) && |
Willy Tarreau | d45b9f8 | 2015-04-13 16:30:14 +0200 | [diff] [blame] | 1704 | !(ic->flags & (CF_SHUTR|CF_DONT_READ))) |
| 1705 | return; |
| 1706 | |
| 1707 | /* fall through */ |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 1708 | case CS_ST_CON: |
| 1709 | case CS_ST_CER: |
| 1710 | case CS_ST_QUE: |
| 1711 | case CS_ST_TAR: |
Willy Tarreau | d45b9f8 | 2015-04-13 16:30:14 +0200 | [diff] [blame] | 1712 | /* Note that none of these states may happen with applets */ |
Willy Tarreau | d45b9f8 | 2015-04-13 16:30:14 +0200 | [diff] [blame] | 1713 | si_applet_release(si); |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 1714 | si->cs->state = CS_ST_DIS; |
Tim Duesterhus | 588b314 | 2020-05-29 14:35:51 +0200 | [diff] [blame] | 1715 | /* fall through */ |
Willy Tarreau | d45b9f8 | 2015-04-13 16:30:14 +0200 | [diff] [blame] | 1716 | default: |
Christopher Faulet | 8abe712 | 2022-03-30 15:10:18 +0200 | [diff] [blame] | 1717 | si->cs->flags &= ~CS_FL_NOLINGER; |
Willy Tarreau | abb5d42 | 2018-11-14 16:58:52 +0100 | [diff] [blame] | 1718 | si_rx_shut_blk(si); |
Willy Tarreau | d45b9f8 | 2015-04-13 16:30:14 +0200 | [diff] [blame] | 1719 | ic->flags |= CF_SHUTR; |
| 1720 | ic->rex = TICK_ETERNITY; |
Christopher Faulet | ae024ce | 2022-03-29 19:02:31 +0200 | [diff] [blame] | 1721 | __cs_strm(si->cs)->conn_exp = TICK_ETERNITY; |
Willy Tarreau | d45b9f8 | 2015-04-13 16:30:14 +0200 | [diff] [blame] | 1722 | } |
Willy Tarreau | d45b9f8 | 2015-04-13 16:30:14 +0200 | [diff] [blame] | 1723 | } |
| 1724 | |
| 1725 | /* chk_rcv function for applets */ |
| 1726 | static void stream_int_chk_rcv_applet(struct stream_interface *si) |
| 1727 | { |
| 1728 | struct channel *ic = si_ic(si); |
| 1729 | |
Christopher Faulet | 8bc1759 | 2022-02-28 17:27:09 +0100 | [diff] [blame] | 1730 | BUG_ON(!cs_appctx(si->cs)); |
Christopher Faulet | 693b23b | 2022-02-28 09:09:05 +0100 | [diff] [blame] | 1731 | |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 1732 | DPRINTF(stderr, "%s: si=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n", |
Willy Tarreau | d45b9f8 | 2015-04-13 16:30:14 +0200 | [diff] [blame] | 1733 | __FUNCTION__, |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 1734 | si, si->cs->state, ic->flags, si_oc(si)->flags); |
Willy Tarreau | d45b9f8 | 2015-04-13 16:30:14 +0200 | [diff] [blame] | 1735 | |
Christopher Faulet | b3e0de4 | 2018-10-11 13:54:13 +0200 | [diff] [blame] | 1736 | if (!ic->pipe) { |
Willy Tarreau | d45b9f8 | 2015-04-13 16:30:14 +0200 | [diff] [blame] | 1737 | /* (re)start reading */ |
Christopher Faulet | 693b23b | 2022-02-28 09:09:05 +0100 | [diff] [blame] | 1738 | appctx_wakeup(__cs_appctx(si->cs)); |
Thierry FOURNIER | 5bc2cbf | 2015-09-04 18:40:36 +0200 | [diff] [blame] | 1739 | } |
Willy Tarreau | d45b9f8 | 2015-04-13 16:30:14 +0200 | [diff] [blame] | 1740 | } |
| 1741 | |
| 1742 | /* chk_snd function for applets */ |
| 1743 | static void stream_int_chk_snd_applet(struct stream_interface *si) |
| 1744 | { |
| 1745 | struct channel *oc = si_oc(si); |
| 1746 | |
Christopher Faulet | 8bc1759 | 2022-02-28 17:27:09 +0100 | [diff] [blame] | 1747 | BUG_ON(!cs_appctx(si->cs)); |
Christopher Faulet | 693b23b | 2022-02-28 09:09:05 +0100 | [diff] [blame] | 1748 | |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 1749 | DPRINTF(stderr, "%s: si=%p, cs->state=%d ic->flags=%08x oc->flags=%08x\n", |
Willy Tarreau | d45b9f8 | 2015-04-13 16:30:14 +0200 | [diff] [blame] | 1750 | __FUNCTION__, |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 1751 | si, si->cs->state, si_ic(si)->flags, oc->flags); |
Willy Tarreau | d45b9f8 | 2015-04-13 16:30:14 +0200 | [diff] [blame] | 1752 | |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 1753 | if (unlikely(si->cs->state != CS_ST_EST || (oc->flags & CF_SHUTW))) |
Willy Tarreau | d45b9f8 | 2015-04-13 16:30:14 +0200 | [diff] [blame] | 1754 | return; |
| 1755 | |
Willy Tarreau | 828824a | 2015-04-19 17:20:03 +0200 | [diff] [blame] | 1756 | /* we only wake the applet up if it was waiting for some data */ |
| 1757 | |
| 1758 | if (!(si->flags & SI_FL_WAIT_DATA)) |
Willy Tarreau | d45b9f8 | 2015-04-13 16:30:14 +0200 | [diff] [blame] | 1759 | return; |
| 1760 | |
Willy Tarreau | d45b9f8 | 2015-04-13 16:30:14 +0200 | [diff] [blame] | 1761 | if (!tick_isset(oc->wex)) |
| 1762 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
| 1763 | |
Willy Tarreau | 828824a | 2015-04-19 17:20:03 +0200 | [diff] [blame] | 1764 | if (!channel_is_empty(oc)) { |
| 1765 | /* (re)start sending */ |
Christopher Faulet | 693b23b | 2022-02-28 09:09:05 +0100 | [diff] [blame] | 1766 | appctx_wakeup(__cs_appctx(si->cs)); |
Willy Tarreau | 828824a | 2015-04-19 17:20:03 +0200 | [diff] [blame] | 1767 | } |
Willy Tarreau | d45b9f8 | 2015-04-13 16:30:14 +0200 | [diff] [blame] | 1768 | } |
| 1769 | |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 1770 | /* |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 1771 | * Local variables: |
| 1772 | * c-indent-level: 8 |
| 1773 | * c-basic-offset: 8 |
| 1774 | * End: |
| 1775 | */ |