Christopher Faulet | 1329f2a | 2021-12-16 17:32:56 +0100 | [diff] [blame] | 1 | /* |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 2 | * stream connector management functions |
Christopher Faulet | 1329f2a | 2021-12-16 17:32:56 +0100 | [diff] [blame] | 3 | * |
| 4 | * Copyright 2021 Christopher Faulet <cfaulet@haproxy.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <haproxy/api.h> |
Christopher Faulet | 3704663 | 2022-04-01 11:36:58 +0200 | [diff] [blame] | 14 | #include <haproxy/applet.h> |
Christopher Faulet | 1329f2a | 2021-12-16 17:32:56 +0100 | [diff] [blame] | 15 | #include <haproxy/connection.h> |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 16 | #include <haproxy/check.h> |
| 17 | #include <haproxy/http_ana.h> |
| 18 | #include <haproxy/pipe.h> |
Christopher Faulet | 1329f2a | 2021-12-16 17:32:56 +0100 | [diff] [blame] | 19 | #include <haproxy/pool.h> |
Willy Tarreau | 5edca2f | 2022-05-27 09:25:10 +0200 | [diff] [blame] | 20 | #include <haproxy/sc_strm.h> |
Willy Tarreau | cb086c6 | 2022-05-27 09:47:12 +0200 | [diff] [blame] | 21 | #include <haproxy/stconn.h> |
Christopher Faulet | 7542fb4 | 2023-05-11 14:40:27 +0200 | [diff] [blame] | 22 | #include <haproxy/xref.h> |
Christopher Faulet | 1329f2a | 2021-12-16 17:32:56 +0100 | [diff] [blame] | 23 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 24 | DECLARE_POOL(pool_head_connstream, "stconn", sizeof(struct stconn)); |
Willy Tarreau | ea59b02 | 2022-05-17 17:53:22 +0200 | [diff] [blame] | 25 | DECLARE_POOL(pool_head_sedesc, "sedesc", sizeof(struct sedesc)); |
Christopher Faulet | 1329f2a | 2021-12-16 17:32:56 +0100 | [diff] [blame] | 26 | |
Willy Tarreau | 3a3f480 | 2022-05-17 18:28:19 +0200 | [diff] [blame] | 27 | /* functions used by default on a detached stream connector */ |
Christopher Faulet | cfc11c0 | 2023-04-13 16:10:23 +0200 | [diff] [blame] | 28 | static void sc_app_abort(struct stconn *sc); |
Christopher Faulet | b2b1c3a | 2023-04-13 16:23:48 +0200 | [diff] [blame] | 29 | static void sc_app_shut(struct stconn *sc); |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 30 | static void sc_app_chk_rcv(struct stconn *sc); |
| 31 | static void sc_app_chk_snd(struct stconn *sc); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 32 | |
Willy Tarreau | 3a3f480 | 2022-05-17 18:28:19 +0200 | [diff] [blame] | 33 | /* functions used on a mux-based stream connector */ |
Christopher Faulet | cfc11c0 | 2023-04-13 16:10:23 +0200 | [diff] [blame] | 34 | static void sc_app_abort_conn(struct stconn *sc); |
Christopher Faulet | b2b1c3a | 2023-04-13 16:23:48 +0200 | [diff] [blame] | 35 | static void sc_app_shut_conn(struct stconn *sc); |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 36 | static void sc_app_chk_rcv_conn(struct stconn *sc); |
| 37 | static void sc_app_chk_snd_conn(struct stconn *sc); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 38 | |
Willy Tarreau | 3a3f480 | 2022-05-17 18:28:19 +0200 | [diff] [blame] | 39 | /* functions used on an applet-based stream connector */ |
Christopher Faulet | cfc11c0 | 2023-04-13 16:10:23 +0200 | [diff] [blame] | 40 | static void sc_app_abort_applet(struct stconn *sc); |
Christopher Faulet | b2b1c3a | 2023-04-13 16:23:48 +0200 | [diff] [blame] | 41 | static void sc_app_shut_applet(struct stconn *sc); |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 42 | static void sc_app_chk_rcv_applet(struct stconn *sc); |
| 43 | static void sc_app_chk_snd_applet(struct stconn *sc); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 44 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 45 | static int sc_conn_process(struct stconn *sc); |
| 46 | static int sc_conn_recv(struct stconn *sc); |
| 47 | static int sc_conn_send(struct stconn *sc); |
| 48 | static int sc_applet_process(struct stconn *sc); |
Willy Tarreau | 2f2318d | 2022-05-18 10:17:16 +0200 | [diff] [blame] | 49 | |
Willy Tarreau | 3a3f480 | 2022-05-17 18:28:19 +0200 | [diff] [blame] | 50 | /* stream connector operations for connections */ |
| 51 | struct sc_app_ops sc_app_conn_ops = { |
| 52 | .chk_rcv = sc_app_chk_rcv_conn, |
| 53 | .chk_snd = sc_app_chk_snd_conn, |
Christopher Faulet | cfc11c0 | 2023-04-13 16:10:23 +0200 | [diff] [blame] | 54 | .abort = sc_app_abort_conn, |
Christopher Faulet | b2b1c3a | 2023-04-13 16:23:48 +0200 | [diff] [blame] | 55 | .shutdown= sc_app_shut_conn, |
Willy Tarreau | 462b989 | 2022-05-18 18:06:53 +0200 | [diff] [blame] | 56 | .wake = sc_conn_process, |
Willy Tarreau | 2f2318d | 2022-05-18 10:17:16 +0200 | [diff] [blame] | 57 | .name = "STRM", |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 58 | }; |
| 59 | |
Willy Tarreau | 3a3f480 | 2022-05-17 18:28:19 +0200 | [diff] [blame] | 60 | /* stream connector operations for embedded tasks */ |
| 61 | struct sc_app_ops sc_app_embedded_ops = { |
| 62 | .chk_rcv = sc_app_chk_rcv, |
| 63 | .chk_snd = sc_app_chk_snd, |
Christopher Faulet | cfc11c0 | 2023-04-13 16:10:23 +0200 | [diff] [blame] | 64 | .abort = sc_app_abort, |
Christopher Faulet | b2b1c3a | 2023-04-13 16:23:48 +0200 | [diff] [blame] | 65 | .shutdown= sc_app_shut, |
Willy Tarreau | 2f2318d | 2022-05-18 10:17:16 +0200 | [diff] [blame] | 66 | .wake = NULL, /* may never be used */ |
| 67 | .name = "NONE", /* may never be used */ |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 68 | }; |
| 69 | |
Willy Tarreau | 2f2318d | 2022-05-18 10:17:16 +0200 | [diff] [blame] | 70 | /* stream connector operations for applets */ |
Willy Tarreau | 3a3f480 | 2022-05-17 18:28:19 +0200 | [diff] [blame] | 71 | struct sc_app_ops sc_app_applet_ops = { |
| 72 | .chk_rcv = sc_app_chk_rcv_applet, |
| 73 | .chk_snd = sc_app_chk_snd_applet, |
Christopher Faulet | cfc11c0 | 2023-04-13 16:10:23 +0200 | [diff] [blame] | 74 | .abort = sc_app_abort_applet, |
Christopher Faulet | b2b1c3a | 2023-04-13 16:23:48 +0200 | [diff] [blame] | 75 | .shutdown= sc_app_shut_applet, |
Willy Tarreau | 19c65a9 | 2022-05-27 08:49:24 +0200 | [diff] [blame] | 76 | .wake = sc_applet_process, |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 77 | .name = "STRM", |
| 78 | }; |
| 79 | |
Willy Tarreau | 2f2318d | 2022-05-18 10:17:16 +0200 | [diff] [blame] | 80 | /* stream connector for health checks on connections */ |
| 81 | struct sc_app_ops sc_app_check_ops = { |
| 82 | .chk_rcv = NULL, |
| 83 | .chk_snd = NULL, |
Christopher Faulet | cfc11c0 | 2023-04-13 16:10:23 +0200 | [diff] [blame] | 84 | .abort = NULL, |
Christopher Faulet | b2b1c3a | 2023-04-13 16:23:48 +0200 | [diff] [blame] | 85 | .shutdown= NULL, |
Willy Tarreau | 2f2318d | 2022-05-18 10:17:16 +0200 | [diff] [blame] | 86 | .wake = wake_srv_chk, |
| 87 | .name = "CHCK", |
| 88 | }; |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 89 | |
Christopher Faulet | 9ed7742 | 2022-04-12 08:51:15 +0200 | [diff] [blame] | 90 | /* Initializes an endpoint */ |
Willy Tarreau | ea59b02 | 2022-05-17 17:53:22 +0200 | [diff] [blame] | 91 | void sedesc_init(struct sedesc *sedesc) |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 92 | { |
Willy Tarreau | ea59b02 | 2022-05-17 17:53:22 +0200 | [diff] [blame] | 93 | sedesc->se = NULL; |
| 94 | sedesc->conn = NULL; |
Willy Tarreau | c105492 | 2022-05-18 07:43:52 +0200 | [diff] [blame] | 95 | sedesc->sc = NULL; |
Christopher Faulet | 4c13568 | 2023-02-16 11:09:31 +0100 | [diff] [blame] | 96 | sedesc->lra = TICK_ETERNITY; |
| 97 | sedesc->fsb = TICK_ETERNITY; |
Christopher Faulet | 7542fb4 | 2023-05-11 14:40:27 +0200 | [diff] [blame] | 98 | sedesc->xref.peer = NULL; |
Willy Tarreau | ea59b02 | 2022-05-17 17:53:22 +0200 | [diff] [blame] | 99 | se_fl_setall(sedesc, SE_FL_NONE); |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 100 | } |
| 101 | |
Christopher Faulet | 9ed7742 | 2022-04-12 08:51:15 +0200 | [diff] [blame] | 102 | /* Tries to alloc an endpoint and initialize it. Returns NULL on failure. */ |
Willy Tarreau | ea59b02 | 2022-05-17 17:53:22 +0200 | [diff] [blame] | 103 | struct sedesc *sedesc_new() |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 104 | { |
Willy Tarreau | ea59b02 | 2022-05-17 17:53:22 +0200 | [diff] [blame] | 105 | struct sedesc *sedesc; |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 106 | |
Willy Tarreau | ea59b02 | 2022-05-17 17:53:22 +0200 | [diff] [blame] | 107 | sedesc = pool_alloc(pool_head_sedesc); |
| 108 | if (unlikely(!sedesc)) |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 109 | return NULL; |
| 110 | |
Willy Tarreau | ea59b02 | 2022-05-17 17:53:22 +0200 | [diff] [blame] | 111 | sedesc_init(sedesc); |
| 112 | return sedesc; |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 113 | } |
| 114 | |
Christopher Faulet | 9ed7742 | 2022-04-12 08:51:15 +0200 | [diff] [blame] | 115 | /* Releases an endpoint. It is the caller responsibility to be sure it is safe |
| 116 | * and it is not shared with another entity |
| 117 | */ |
Willy Tarreau | ea59b02 | 2022-05-17 17:53:22 +0200 | [diff] [blame] | 118 | void sedesc_free(struct sedesc *sedesc) |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 119 | { |
Willy Tarreau | ea59b02 | 2022-05-17 17:53:22 +0200 | [diff] [blame] | 120 | pool_free(pool_head_sedesc, sedesc); |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 121 | } |
Christopher Faulet | 1329f2a | 2021-12-16 17:32:56 +0100 | [diff] [blame] | 122 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 123 | /* Tries to allocate a new stconn and initialize its main fields. On |
Christopher Faulet | 9ed7742 | 2022-04-12 08:51:15 +0200 | [diff] [blame] | 124 | * failure, nothing is allocated and NULL is returned. It is an internal |
Willy Tarreau | b605c42 | 2022-05-17 17:04:55 +0200 | [diff] [blame] | 125 | * function. The caller must, at least, set the SE_FL_ORPHAN or SE_FL_DETACHED |
Christopher Faulet | 9ed7742 | 2022-04-12 08:51:15 +0200 | [diff] [blame] | 126 | * flag. |
Christopher Faulet | 1329f2a | 2021-12-16 17:32:56 +0100 | [diff] [blame] | 127 | */ |
Willy Tarreau | a0b58b5 | 2022-05-27 08:33:53 +0200 | [diff] [blame] | 128 | static struct stconn *sc_new(struct sedesc *sedesc) |
Christopher Faulet | 1329f2a | 2021-12-16 17:32:56 +0100 | [diff] [blame] | 129 | { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 130 | struct stconn *sc; |
Christopher Faulet | 1329f2a | 2021-12-16 17:32:56 +0100 | [diff] [blame] | 131 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 132 | sc = pool_alloc(pool_head_connstream); |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 133 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 134 | if (unlikely(!sc)) |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 135 | goto alloc_error; |
Christopher Faulet | bb772d0 | 2022-03-22 15:28:36 +0100 | [diff] [blame] | 136 | |
Willy Tarreau | 1d2c79a | 2022-05-27 11:15:19 +0200 | [diff] [blame] | 137 | sc->obj_type = OBJ_TYPE_SC; |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 138 | sc->flags = SC_FL_NONE; |
| 139 | sc->state = SC_ST_INI; |
Christopher Faulet | be5cc76 | 2023-02-20 08:41:55 +0100 | [diff] [blame] | 140 | sc->ioto = TICK_ETERNITY; |
Christopher Faulet | 9aed112 | 2023-05-05 11:25:19 +0200 | [diff] [blame] | 141 | sc->room_needed = 0; |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 142 | sc->app = NULL; |
| 143 | sc->app_ops = NULL; |
| 144 | sc->src = NULL; |
| 145 | sc->dst = NULL; |
| 146 | sc->wait_event.tasklet = NULL; |
| 147 | sc->wait_event.events = 0; |
Christopher Faulet | 2f35e7b | 2022-03-31 11:09:28 +0200 | [diff] [blame] | 148 | |
Christopher Faulet | 9ed7742 | 2022-04-12 08:51:15 +0200 | [diff] [blame] | 149 | /* If there is no endpoint, allocate a new one now */ |
Willy Tarreau | ea59b02 | 2022-05-17 17:53:22 +0200 | [diff] [blame] | 150 | if (!sedesc) { |
| 151 | sedesc = sedesc_new(); |
| 152 | if (unlikely(!sedesc)) |
Christopher Faulet | b669d68 | 2022-03-22 18:37:19 +0100 | [diff] [blame] | 153 | goto alloc_error; |
| 154 | } |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 155 | sc->sedesc = sedesc; |
| 156 | sedesc->sc = sc; |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 157 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 158 | return sc; |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 159 | |
| 160 | alloc_error: |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 161 | pool_free(pool_head_connstream, sc); |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 162 | return NULL; |
Christopher Faulet | 1329f2a | 2021-12-16 17:32:56 +0100 | [diff] [blame] | 163 | } |
| 164 | |
Willy Tarreau | 3121928 | 2022-05-27 16:21:33 +0200 | [diff] [blame] | 165 | /* Creates a new stream connector and its associated stream from a mux. <sd> must |
| 166 | * be defined. It returns NULL on error. On success, the new stream connector is |
Willy Tarreau | b605c42 | 2022-05-17 17:04:55 +0200 | [diff] [blame] | 167 | * returned. In this case, SE_FL_ORPHAN flag is removed. |
Christopher Faulet | 9ed7742 | 2022-04-12 08:51:15 +0200 | [diff] [blame] | 168 | */ |
Willy Tarreau | 3121928 | 2022-05-27 16:21:33 +0200 | [diff] [blame] | 169 | struct stconn *sc_new_from_endp(struct sedesc *sd, struct session *sess, struct buffer *input) |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 170 | { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 171 | struct stconn *sc; |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 172 | |
Willy Tarreau | 3121928 | 2022-05-27 16:21:33 +0200 | [diff] [blame] | 173 | sc = sc_new(sd); |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 174 | if (unlikely(!sc)) |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 175 | return NULL; |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 176 | if (unlikely(!stream_new(sess, sc, input))) { |
Christopher Faulet | 3ab72c6 | 2022-09-27 09:18:20 +0200 | [diff] [blame] | 177 | sd->sc = NULL; |
Willy Tarreau | 7a8ca0a | 2023-03-20 19:53:14 +0100 | [diff] [blame] | 178 | if (sc->sedesc != sd) { |
| 179 | /* none was provided so sc_new() allocated one */ |
| 180 | sedesc_free(sc->sedesc); |
| 181 | } |
| 182 | pool_free(pool_head_connstream, sc); |
Christopher Faulet | 3ab72c6 | 2022-09-27 09:18:20 +0200 | [diff] [blame] | 183 | se_fl_set(sd, SE_FL_ORPHAN); |
| 184 | return NULL; |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 185 | } |
Willy Tarreau | 3121928 | 2022-05-27 16:21:33 +0200 | [diff] [blame] | 186 | se_fl_clr(sd, SE_FL_ORPHAN); |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 187 | return sc; |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 188 | } |
| 189 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 190 | /* Creates a new stream connector from an stream. There is no endpoint here, thus it |
Willy Tarreau | a0b58b5 | 2022-05-27 08:33:53 +0200 | [diff] [blame] | 191 | * will be created by sc_new(). So the SE_FL_DETACHED flag is set. It returns |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 192 | * NULL on error. On success, the new stream connector is returned. |
Christopher Faulet | 9ed7742 | 2022-04-12 08:51:15 +0200 | [diff] [blame] | 193 | */ |
Willy Tarreau | a0b58b5 | 2022-05-27 08:33:53 +0200 | [diff] [blame] | 194 | struct stconn *sc_new_from_strm(struct stream *strm, unsigned int flags) |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 195 | { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 196 | struct stconn *sc; |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 197 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 198 | sc = sc_new(NULL); |
| 199 | if (unlikely(!sc)) |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 200 | return NULL; |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 201 | sc->flags |= flags; |
| 202 | sc_ep_set(sc, SE_FL_DETACHED); |
| 203 | sc->app = &strm->obj_type; |
| 204 | sc->app_ops = &sc_app_embedded_ops; |
| 205 | return sc; |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 206 | } |
| 207 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 208 | /* Creates a new stream connector from an health-check. There is no endpoint here, |
Willy Tarreau | a0b58b5 | 2022-05-27 08:33:53 +0200 | [diff] [blame] | 209 | * thus it will be created by sc_new(). So the SE_FL_DETACHED flag is set. It |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 210 | * returns NULL on error. On success, the new stream connector is returned. |
Christopher Faulet | 9ed7742 | 2022-04-12 08:51:15 +0200 | [diff] [blame] | 211 | */ |
Willy Tarreau | a0b58b5 | 2022-05-27 08:33:53 +0200 | [diff] [blame] | 212 | struct stconn *sc_new_from_check(struct check *check, unsigned int flags) |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 213 | { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 214 | struct stconn *sc; |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 215 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 216 | sc = sc_new(NULL); |
| 217 | if (unlikely(!sc)) |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 218 | return NULL; |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 219 | sc->flags |= flags; |
| 220 | sc_ep_set(sc, SE_FL_DETACHED); |
| 221 | sc->app = &check->obj_type; |
| 222 | sc->app_ops = &sc_app_check_ops; |
| 223 | return sc; |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 224 | } |
| 225 | |
Willy Tarreau | a0b58b5 | 2022-05-27 08:33:53 +0200 | [diff] [blame] | 226 | /* Releases a stconn previously allocated by sc_new(), as well as its |
Christopher Faulet | 9ed7742 | 2022-04-12 08:51:15 +0200 | [diff] [blame] | 227 | * endpoint, if it exists. This function is called internally or on error path. |
Christopher Faulet | 1329f2a | 2021-12-16 17:32:56 +0100 | [diff] [blame] | 228 | */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 229 | void sc_free(struct stconn *sc) |
Christopher Faulet | 1329f2a | 2021-12-16 17:32:56 +0100 | [diff] [blame] | 230 | { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 231 | sockaddr_free(&sc->src); |
| 232 | sockaddr_free(&sc->dst); |
| 233 | if (sc->sedesc) { |
| 234 | BUG_ON(!sc_ep_test(sc, SE_FL_DETACHED)); |
| 235 | sedesc_free(sc->sedesc); |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 236 | } |
Tim Duesterhus | b1ec21d | 2023-04-22 17:47:32 +0200 | [diff] [blame] | 237 | tasklet_free(sc->wait_event.tasklet); |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 238 | pool_free(pool_head_connstream, sc); |
Christopher Faulet | 1329f2a | 2021-12-16 17:32:56 +0100 | [diff] [blame] | 239 | } |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 240 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 241 | /* Conditionally removes a stream connector if it is detached and if there is no app |
Christopher Faulet | eb50c01 | 2022-04-21 14:22:53 +0200 | [diff] [blame] | 242 | * layer defined. Except on error path, this one must be used. if release, the |
Willy Tarreau | e68bc61 | 2022-05-27 11:23:05 +0200 | [diff] [blame] | 243 | * pointer on the SC is set to NULL. |
Christopher Faulet | aa69d8f | 2022-04-12 18:09:48 +0200 | [diff] [blame] | 244 | */ |
Willy Tarreau | e68bc61 | 2022-05-27 11:23:05 +0200 | [diff] [blame] | 245 | static void sc_free_cond(struct stconn **scp) |
Christopher Faulet | aa69d8f | 2022-04-12 18:09:48 +0200 | [diff] [blame] | 246 | { |
Willy Tarreau | e68bc61 | 2022-05-27 11:23:05 +0200 | [diff] [blame] | 247 | struct stconn *sc = *scp; |
Christopher Faulet | eb50c01 | 2022-04-21 14:22:53 +0200 | [diff] [blame] | 248 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 249 | if (!sc->app && (!sc->sedesc || sc_ep_test(sc, SE_FL_DETACHED))) { |
| 250 | sc_free(sc); |
Willy Tarreau | e68bc61 | 2022-05-27 11:23:05 +0200 | [diff] [blame] | 251 | *scp = NULL; |
Christopher Faulet | eb50c01 | 2022-04-21 14:22:53 +0200 | [diff] [blame] | 252 | } |
Christopher Faulet | aa69d8f | 2022-04-12 18:09:48 +0200 | [diff] [blame] | 253 | } |
| 254 | |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 255 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 256 | /* Attaches a stconn to a mux endpoint and sets the endpoint ctx. Returns |
Ilya Shipitsin | 3b64a28 | 2022-07-29 22:26:53 +0500 | [diff] [blame] | 257 | * -1 on error and 0 on success. SE_FL_DETACHED flag is removed. This function is |
Christopher Faulet | 9ed7742 | 2022-04-12 08:51:15 +0200 | [diff] [blame] | 258 | * called from a mux when it is attached to a stream or a health-check. |
| 259 | */ |
Willy Tarreau | 3121928 | 2022-05-27 16:21:33 +0200 | [diff] [blame] | 260 | int sc_attach_mux(struct stconn *sc, void *sd, void *ctx) |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 261 | { |
Christopher Faulet | 9388204 | 2022-01-19 14:56:50 +0100 | [diff] [blame] | 262 | struct connection *conn = ctx; |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 263 | struct sedesc *sedesc = sc->sedesc; |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 264 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 265 | if (sc_strm(sc)) { |
| 266 | if (!sc->wait_event.tasklet) { |
| 267 | sc->wait_event.tasklet = tasklet_new(); |
| 268 | if (!sc->wait_event.tasklet) |
Christopher Faulet | 2f35e7b | 2022-03-31 11:09:28 +0200 | [diff] [blame] | 269 | return -1; |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 270 | sc->wait_event.tasklet->process = sc_conn_io_cb; |
| 271 | sc->wait_event.tasklet->context = sc; |
| 272 | sc->wait_event.events = 0; |
Christopher Faulet | 2f35e7b | 2022-03-31 11:09:28 +0200 | [diff] [blame] | 273 | } |
| 274 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 275 | sc->app_ops = &sc_app_conn_ops; |
Christopher Faulet | 7542fb4 | 2023-05-11 14:40:27 +0200 | [diff] [blame] | 276 | xref_create(&sc->sedesc->xref, &sc_opposite(sc)->sedesc->xref); |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 277 | } |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 278 | else if (sc_check(sc)) { |
| 279 | if (!sc->wait_event.tasklet) { |
| 280 | sc->wait_event.tasklet = tasklet_new(); |
| 281 | if (!sc->wait_event.tasklet) |
Christopher Faulet | c95eaef | 2022-05-18 15:57:15 +0200 | [diff] [blame] | 282 | return -1; |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 283 | sc->wait_event.tasklet->process = srv_chk_io_cb; |
| 284 | sc->wait_event.tasklet->context = sc; |
| 285 | sc->wait_event.events = 0; |
Christopher Faulet | c95eaef | 2022-05-18 15:57:15 +0200 | [diff] [blame] | 286 | } |
| 287 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 288 | sc->app_ops = &sc_app_check_ops; |
Christopher Faulet | c95eaef | 2022-05-18 15:57:15 +0200 | [diff] [blame] | 289 | } |
Willy Tarreau | e2f7946 | 2023-03-20 19:45:41 +0100 | [diff] [blame] | 290 | |
| 291 | sedesc->se = sd; |
| 292 | sedesc->conn = ctx; |
| 293 | se_fl_set(sedesc, SE_FL_T_MUX); |
| 294 | se_fl_clr(sedesc, SE_FL_DETACHED); |
| 295 | if (!conn->ctx) |
| 296 | conn->ctx = sc; |
Christopher Faulet | 070b91b | 2022-03-31 19:27:18 +0200 | [diff] [blame] | 297 | return 0; |
Christopher Faulet | 9388204 | 2022-01-19 14:56:50 +0100 | [diff] [blame] | 298 | } |
| 299 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 300 | /* Attaches a stconn to an applet endpoint and sets the endpoint |
Ilya Shipitsin | 3b64a28 | 2022-07-29 22:26:53 +0500 | [diff] [blame] | 301 | * ctx. Returns -1 on error and 0 on success. SE_FL_DETACHED flag is |
Christopher Faulet | 9ed7742 | 2022-04-12 08:51:15 +0200 | [diff] [blame] | 302 | * removed. This function is called by a stream when a backend applet is |
| 303 | * registered. |
| 304 | */ |
Willy Tarreau | 3121928 | 2022-05-27 16:21:33 +0200 | [diff] [blame] | 305 | static void sc_attach_applet(struct stconn *sc, void *sd) |
Christopher Faulet | 9388204 | 2022-01-19 14:56:50 +0100 | [diff] [blame] | 306 | { |
Willy Tarreau | 3121928 | 2022-05-27 16:21:33 +0200 | [diff] [blame] | 307 | sc->sedesc->se = sd; |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 308 | sc_ep_set(sc, SE_FL_T_APPLET); |
| 309 | sc_ep_clr(sc, SE_FL_DETACHED); |
Christopher Faulet | 7542fb4 | 2023-05-11 14:40:27 +0200 | [diff] [blame] | 310 | if (sc_strm(sc)) { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 311 | sc->app_ops = &sc_app_applet_ops; |
Christopher Faulet | 7542fb4 | 2023-05-11 14:40:27 +0200 | [diff] [blame] | 312 | xref_create(&sc->sedesc->xref, &sc_opposite(sc)->sedesc->xref); |
| 313 | } |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 314 | } |
| 315 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 316 | /* Attaches a stconn to a app layer and sets the relevant |
Willy Tarreau | b605c42 | 2022-05-17 17:04:55 +0200 | [diff] [blame] | 317 | * callbacks. Returns -1 on error and 0 on success. SE_FL_ORPHAN flag is |
Christopher Faulet | 9ed7742 | 2022-04-12 08:51:15 +0200 | [diff] [blame] | 318 | * removed. This function is called by a stream when it is created to attach it |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 319 | * on the stream connector on the client side. |
Christopher Faulet | 9ed7742 | 2022-04-12 08:51:15 +0200 | [diff] [blame] | 320 | */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 321 | int sc_attach_strm(struct stconn *sc, struct stream *strm) |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 322 | { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 323 | sc->app = &strm->obj_type; |
| 324 | sc_ep_clr(sc, SE_FL_ORPHAN); |
Christopher Faulet | f4ef3d9 | 2023-09-06 08:46:15 +0200 | [diff] [blame] | 325 | sc_ep_report_read_activity(sc); |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 326 | if (sc_ep_test(sc, SE_FL_T_MUX)) { |
| 327 | sc->wait_event.tasklet = tasklet_new(); |
| 328 | if (!sc->wait_event.tasklet) |
Christopher Faulet | 2f35e7b | 2022-03-31 11:09:28 +0200 | [diff] [blame] | 329 | return -1; |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 330 | sc->wait_event.tasklet->process = sc_conn_io_cb; |
| 331 | sc->wait_event.tasklet->context = sc; |
| 332 | sc->wait_event.events = 0; |
Christopher Faulet | 2f35e7b | 2022-03-31 11:09:28 +0200 | [diff] [blame] | 333 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 334 | sc->app_ops = &sc_app_conn_ops; |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 335 | } |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 336 | else if (sc_ep_test(sc, SE_FL_T_APPLET)) { |
| 337 | sc->app_ops = &sc_app_applet_ops; |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 338 | } |
| 339 | else { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 340 | sc->app_ops = &sc_app_embedded_ops; |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 341 | } |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 342 | return 0; |
| 343 | } |
| 344 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 345 | /* Detaches the stconn from the endpoint, if any. For a connecrion, if a |
Christopher Faulet | 9ed7742 | 2022-04-12 08:51:15 +0200 | [diff] [blame] | 346 | * mux owns the connection ->detach() callback is called. Otherwise, it means |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 347 | * the stream connector owns the connection. In this case the connection is closed |
Christopher Faulet | 9ed7742 | 2022-04-12 08:51:15 +0200 | [diff] [blame] | 348 | * and released. For an applet, the appctx is released. If still allocated, the |
| 349 | * endpoint is reset and flag as detached. If the app layer is also detached, |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 350 | * the stream connector is released. |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 351 | */ |
Willy Tarreau | e68bc61 | 2022-05-27 11:23:05 +0200 | [diff] [blame] | 352 | static void sc_detach_endp(struct stconn **scp) |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 353 | { |
Willy Tarreau | e68bc61 | 2022-05-27 11:23:05 +0200 | [diff] [blame] | 354 | struct stconn *sc = *scp; |
Christopher Faulet | 6eb53b1 | 2023-05-15 09:53:29 +0200 | [diff] [blame] | 355 | struct xref *peer; |
Christopher Faulet | eb50c01 | 2022-04-21 14:22:53 +0200 | [diff] [blame] | 356 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 357 | if (!sc) |
Christopher Faulet | eb50c01 | 2022-04-21 14:22:53 +0200 | [diff] [blame] | 358 | return; |
| 359 | |
Christopher Faulet | 7542fb4 | 2023-05-11 14:40:27 +0200 | [diff] [blame] | 360 | |
Christopher Faulet | 6eb53b1 | 2023-05-15 09:53:29 +0200 | [diff] [blame] | 361 | /* Remove my link in the original objects. */ |
| 362 | peer = xref_get_peer_and_lock(&sc->sedesc->xref); |
| 363 | if (peer) |
| 364 | xref_disconnect(&sc->sedesc->xref, peer); |
Christopher Faulet | 7542fb4 | 2023-05-11 14:40:27 +0200 | [diff] [blame] | 365 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 366 | if (sc_ep_test(sc, SE_FL_T_MUX)) { |
| 367 | struct connection *conn = __sc_conn(sc); |
| 368 | struct sedesc *sedesc = sc->sedesc; |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 369 | |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 370 | if (conn->mux) { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 371 | if (sc->wait_event.events != 0) |
| 372 | conn->mux->unsubscribe(sc, sc->wait_event.events, &sc->wait_event); |
Willy Tarreau | 798465b | 2022-05-17 18:20:02 +0200 | [diff] [blame] | 373 | se_fl_set(sedesc, SE_FL_ORPHAN); |
Willy Tarreau | c105492 | 2022-05-18 07:43:52 +0200 | [diff] [blame] | 374 | sedesc->sc = NULL; |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 375 | sc->sedesc = NULL; |
Willy Tarreau | 798465b | 2022-05-17 18:20:02 +0200 | [diff] [blame] | 376 | conn->mux->detach(sedesc); |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 377 | } |
| 378 | else { |
| 379 | /* It's too early to have a mux, let's just destroy |
| 380 | * the connection |
| 381 | */ |
| 382 | conn_stop_tracking(conn); |
| 383 | conn_full_close(conn); |
| 384 | if (conn->destroy_cb) |
| 385 | conn->destroy_cb(conn); |
| 386 | conn_free(conn); |
| 387 | } |
| 388 | } |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 389 | else if (sc_ep_test(sc, SE_FL_T_APPLET)) { |
| 390 | struct appctx *appctx = __sc_appctx(sc); |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 391 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 392 | sc_ep_set(sc, SE_FL_ORPHAN); |
| 393 | sc->sedesc->sc = NULL; |
| 394 | sc->sedesc = NULL; |
Willy Tarreau | 1c3ead4 | 2022-05-10 19:42:22 +0200 | [diff] [blame] | 395 | appctx_shut(appctx); |
| 396 | appctx_free(appctx); |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 397 | } |
| 398 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 399 | if (sc->sedesc) { |
Willy Tarreau | da59c89 | 2022-05-27 17:03:34 +0200 | [diff] [blame] | 400 | /* the SD wasn't used and can be recycled */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 401 | sc->sedesc->se = NULL; |
| 402 | sc->sedesc->conn = NULL; |
Willy Tarreau | da59c89 | 2022-05-27 17:03:34 +0200 | [diff] [blame] | 403 | sc->sedesc->flags = 0; |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 404 | sc_ep_set(sc, SE_FL_DETACHED); |
Christopher Faulet | db90f2a | 2022-03-22 16:06:25 +0100 | [diff] [blame] | 405 | } |
| 406 | |
Willy Tarreau | e68bc61 | 2022-05-27 11:23:05 +0200 | [diff] [blame] | 407 | /* FIXME: Rest SC for now but must be reviewed. SC flags are only |
Christopher Faulet | c36de9d | 2022-01-06 08:44:58 +0100 | [diff] [blame] | 408 | * connection related for now but this will evolved |
| 409 | */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 410 | sc->flags &= SC_FL_ISBACK; |
| 411 | if (sc_strm(sc)) |
| 412 | sc->app_ops = &sc_app_embedded_ops; |
Willy Tarreau | 2f2318d | 2022-05-18 10:17:16 +0200 | [diff] [blame] | 413 | else |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 414 | sc->app_ops = NULL; |
Willy Tarreau | e68bc61 | 2022-05-27 11:23:05 +0200 | [diff] [blame] | 415 | sc_free_cond(scp); |
Christopher Faulet | c36de9d | 2022-01-06 08:44:58 +0100 | [diff] [blame] | 416 | } |
| 417 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 418 | /* Detaches the stconn from the app layer. If there is no endpoint attached |
| 419 | * to the stconn |
Christopher Faulet | 9ed7742 | 2022-04-12 08:51:15 +0200 | [diff] [blame] | 420 | */ |
Willy Tarreau | e68bc61 | 2022-05-27 11:23:05 +0200 | [diff] [blame] | 421 | static void sc_detach_app(struct stconn **scp) |
Christopher Faulet | c36de9d | 2022-01-06 08:44:58 +0100 | [diff] [blame] | 422 | { |
Willy Tarreau | e68bc61 | 2022-05-27 11:23:05 +0200 | [diff] [blame] | 423 | struct stconn *sc = *scp; |
Christopher Faulet | eb50c01 | 2022-04-21 14:22:53 +0200 | [diff] [blame] | 424 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 425 | if (!sc) |
Christopher Faulet | eb50c01 | 2022-04-21 14:22:53 +0200 | [diff] [blame] | 426 | return; |
| 427 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 428 | sc->app = NULL; |
| 429 | sc->app_ops = NULL; |
| 430 | sockaddr_free(&sc->src); |
| 431 | sockaddr_free(&sc->dst); |
Christopher Faulet | 2f35e7b | 2022-03-31 11:09:28 +0200 | [diff] [blame] | 432 | |
Tim Duesterhus | b1ec21d | 2023-04-22 17:47:32 +0200 | [diff] [blame] | 433 | tasklet_free(sc->wait_event.tasklet); |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 434 | sc->wait_event.tasklet = NULL; |
| 435 | sc->wait_event.events = 0; |
Willy Tarreau | e68bc61 | 2022-05-27 11:23:05 +0200 | [diff] [blame] | 436 | sc_free_cond(scp); |
Christopher Faulet | eb50c01 | 2022-04-21 14:22:53 +0200 | [diff] [blame] | 437 | } |
| 438 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 439 | /* Destroy the stconn. It is detached from its endpoint and its |
| 440 | * application. After this call, the stconn must be considered as released. |
Christopher Faulet | eb50c01 | 2022-04-21 14:22:53 +0200 | [diff] [blame] | 441 | */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 442 | void sc_destroy(struct stconn *sc) |
Christopher Faulet | eb50c01 | 2022-04-21 14:22:53 +0200 | [diff] [blame] | 443 | { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 444 | sc_detach_endp(&sc); |
| 445 | sc_detach_app(&sc); |
| 446 | BUG_ON_HOT(sc); |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 447 | } |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 448 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 449 | /* Resets the stream connector endpoint. It happens when the app layer want to renew |
Christopher Faulet | 9ed7742 | 2022-04-12 08:51:15 +0200 | [diff] [blame] | 450 | * its endpoint. For a connection retry for instance. If a mux or an applet is |
Ilya Shipitsin | 3b64a28 | 2022-07-29 22:26:53 +0500 | [diff] [blame] | 451 | * attached, a new endpoint is created. Returns -1 on error and 0 on success. |
Christopher Faulet | 9ed7742 | 2022-04-12 08:51:15 +0200 | [diff] [blame] | 452 | */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 453 | int sc_reset_endp(struct stconn *sc) |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 454 | { |
Willy Tarreau | 3121928 | 2022-05-27 16:21:33 +0200 | [diff] [blame] | 455 | struct sedesc *new_sd; |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 456 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 457 | BUG_ON(!sc->app); |
Christopher Faulet | a6c4a48 | 2022-04-28 18:25:24 +0200 | [diff] [blame] | 458 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 459 | if (!__sc_endp(sc)) { |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 460 | /* endpoint not attached or attached to a mux with no |
| 461 | * target. Thus the endpoint will not be release but just |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 462 | * reset. The app is still attached, the sc will not be |
Christopher Faulet | eb50c01 | 2022-04-21 14:22:53 +0200 | [diff] [blame] | 463 | * released. |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 464 | */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 465 | sc_detach_endp(&sc); |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 466 | return 0; |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 467 | } |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 468 | |
| 469 | /* allocate the new endpoint first to be able to set error if it |
| 470 | * fails */ |
Willy Tarreau | 3121928 | 2022-05-27 16:21:33 +0200 | [diff] [blame] | 471 | new_sd = sedesc_new(); |
Christopher Faulet | 638fe6a | 2023-04-14 10:28:28 +0200 | [diff] [blame] | 472 | if (!unlikely(new_sd)) |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 473 | return -1; |
Christopher Faulet | b041b23 | 2022-03-24 10:27:02 +0100 | [diff] [blame] | 474 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 475 | /* The app is still attached, the sc will not be released */ |
| 476 | sc_detach_endp(&sc); |
Willy Tarreau | 6a378d1 | 2022-08-11 13:56:42 +0200 | [diff] [blame] | 477 | BUG_ON(!sc); |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 478 | BUG_ON(sc->sedesc); |
Willy Tarreau | 3121928 | 2022-05-27 16:21:33 +0200 | [diff] [blame] | 479 | sc->sedesc = new_sd; |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 480 | sc->sedesc->sc = sc; |
| 481 | sc_ep_set(sc, SE_FL_DETACHED); |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 482 | return 0; |
| 483 | } |
Christopher Faulet | 3704663 | 2022-04-01 11:36:58 +0200 | [diff] [blame] | 484 | |
| 485 | |
Willy Tarreau | e68bc61 | 2022-05-27 11:23:05 +0200 | [diff] [blame] | 486 | /* Create an applet to handle a stream connector as a new appctx. The SC will |
Christopher Faulet | 3704663 | 2022-04-01 11:36:58 +0200 | [diff] [blame] | 487 | * wake it up every time it is solicited. The appctx must be deleted by the task |
Willy Tarreau | 19c65a9 | 2022-05-27 08:49:24 +0200 | [diff] [blame] | 488 | * handler using sc_detach_endp(), possibly from within the function itself. |
Christopher Faulet | 3704663 | 2022-04-01 11:36:58 +0200 | [diff] [blame] | 489 | * It also pre-initializes the applet's context and returns it (or NULL in case |
| 490 | * it could not be allocated). |
| 491 | */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 492 | struct appctx *sc_applet_create(struct stconn *sc, struct applet *app) |
Christopher Faulet | 3704663 | 2022-04-01 11:36:58 +0200 | [diff] [blame] | 493 | { |
| 494 | struct appctx *appctx; |
| 495 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 496 | appctx = appctx_new_here(app, sc->sedesc); |
Christopher Faulet | 3704663 | 2022-04-01 11:36:58 +0200 | [diff] [blame] | 497 | if (!appctx) |
| 498 | return NULL; |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 499 | sc_attach_applet(sc, appctx); |
| 500 | appctx->t->nice = __sc_strm(sc)->task->nice; |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 501 | applet_need_more_data(appctx); |
Christopher Faulet | 3704663 | 2022-04-01 11:36:58 +0200 | [diff] [blame] | 502 | appctx_wakeup(appctx); |
Christopher Faulet | a33ff7a | 2022-04-21 11:52:07 +0200 | [diff] [blame] | 503 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 504 | sc->state = SC_ST_RDY; |
Christopher Faulet | 3704663 | 2022-04-01 11:36:58 +0200 | [diff] [blame] | 505 | return appctx; |
| 506 | } |
| 507 | |
Ilya Shipitsin | 07be66d | 2023-04-01 12:26:42 +0200 | [diff] [blame] | 508 | /* Conditionally forward the close to the write side. It return 1 if it can be |
Christopher Faulet | eb3f26d | 2023-02-08 16:18:48 +0100 | [diff] [blame] | 509 | * forwarded. It is the caller responsibility to forward the close to the write |
Christopher Faulet | e38534c | 2023-04-13 15:45:24 +0200 | [diff] [blame] | 510 | * side. Otherwise, 0 is returned. In this case, SC_FL_SHUT_WANTED flag may be set on |
Christopher Faulet | 87633c3 | 2023-04-03 18:32:50 +0200 | [diff] [blame] | 511 | * the consumer SC if we are only waiting for the outgoing data to be flushed. |
Christopher Faulet | eb3f26d | 2023-02-08 16:18:48 +0100 | [diff] [blame] | 512 | */ |
Christopher Faulet | b2b1c3a | 2023-04-13 16:23:48 +0200 | [diff] [blame] | 513 | static inline int sc_cond_forward_shut(struct stconn *sc) |
Christopher Faulet | eb3f26d | 2023-02-08 16:18:48 +0100 | [diff] [blame] | 514 | { |
Christopher Faulet | 406b81c | 2023-09-06 08:59:33 +0200 | [diff] [blame] | 515 | |
Christopher Faulet | eb3f26d | 2023-02-08 16:18:48 +0100 | [diff] [blame] | 516 | /* The close must not be forwarded */ |
Christopher Faulet | ca5309a | 2023-04-17 16:17:32 +0200 | [diff] [blame] | 517 | if (!(sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)) || !(sc->flags & SC_FL_NOHALF)) |
Christopher Faulet | eb3f26d | 2023-02-08 16:18:48 +0100 | [diff] [blame] | 518 | return 0; |
| 519 | |
Christopher Faulet | c978327 | 2024-01-05 16:48:40 +0100 | [diff] [blame] | 520 | if (!channel_is_empty(sc_ic(sc)) && !(sc_ic(sc)->flags & CF_WRITE_TIMEOUT)) { |
Christopher Faulet | df7cd71 | 2023-04-13 15:56:26 +0200 | [diff] [blame] | 521 | /* the shutdown cannot be forwarded now because |
Christopher Faulet | eb3f26d | 2023-02-08 16:18:48 +0100 | [diff] [blame] | 522 | * we should flush outgoing data first. But instruct the output |
| 523 | * channel it should be done ASAP. |
| 524 | */ |
Christopher Faulet | df7cd71 | 2023-04-13 15:56:26 +0200 | [diff] [blame] | 525 | sc_schedule_shutdown(sc); |
Christopher Faulet | eb3f26d | 2023-02-08 16:18:48 +0100 | [diff] [blame] | 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | /* the close can be immediately forwarded to the write side */ |
| 530 | return 1; |
| 531 | } |
| 532 | |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 533 | /* |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 534 | * This function performs a shutdown-read on a detached stream connector in a |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 535 | * connected or init state (it does nothing for other states). It either shuts |
| 536 | * the read side or marks itself as closed. The buffer flags are updated to |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 537 | * reflect the new state. If the stream connector has SC_FL_NOHALF, we also |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 538 | * forward the close to the write side. The owner task is woken up if it exists. |
| 539 | */ |
Christopher Faulet | cfc11c0 | 2023-04-13 16:10:23 +0200 | [diff] [blame] | 540 | static void sc_app_abort(struct stconn *sc) |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 541 | { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 542 | struct channel *ic = sc_ic(sc); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 543 | |
Christopher Faulet | ca5309a | 2023-04-17 16:17:32 +0200 | [diff] [blame] | 544 | if (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)) |
Christopher Faulet | c665bb5 | 2023-04-04 10:06:57 +0200 | [diff] [blame] | 545 | return; |
Christopher Faulet | 87633c3 | 2023-04-03 18:32:50 +0200 | [diff] [blame] | 546 | |
Christopher Faulet | 0c370ee | 2023-04-13 16:05:13 +0200 | [diff] [blame] | 547 | sc->flags |= SC_FL_ABRT_DONE; |
Christopher Faulet | 87633c3 | 2023-04-03 18:32:50 +0200 | [diff] [blame] | 548 | ic->flags |= CF_READ_EVENT; |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 549 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 550 | if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST)) |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 551 | return; |
| 552 | |
Christopher Faulet | 208c712 | 2023-04-13 16:16:15 +0200 | [diff] [blame] | 553 | if (sc->flags & SC_FL_SHUT_DONE) { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 554 | sc->state = SC_ST_DIS; |
Christopher Faulet | ca67992 | 2022-07-20 13:24:04 +0200 | [diff] [blame] | 555 | if (sc->flags & SC_FL_ISBACK) |
| 556 | __sc_strm(sc)->conn_exp = TICK_ETERNITY; |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 557 | } |
Christopher Faulet | b2b1c3a | 2023-04-13 16:23:48 +0200 | [diff] [blame] | 558 | else if (sc_cond_forward_shut(sc)) |
| 559 | return sc_app_shut(sc); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 560 | |
| 561 | /* note that if the task exists, it must unregister itself once it runs */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 562 | if (!(sc->flags & SC_FL_DONT_WAKE)) |
| 563 | task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | /* |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 567 | * This function performs a shutdown-write on a detached stream connector in a |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 568 | * connected or init state (it does nothing for other states). It either shuts |
| 569 | * the write side or marks itself as closed. The buffer flags are updated to |
Willy Tarreau | e68bc61 | 2022-05-27 11:23:05 +0200 | [diff] [blame] | 570 | * reflect the new state. It does also close everything if the SC was marked as |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 571 | * being in error state. The owner task is woken up if it exists. |
| 572 | */ |
Christopher Faulet | b2b1c3a | 2023-04-13 16:23:48 +0200 | [diff] [blame] | 573 | static void sc_app_shut(struct stconn *sc) |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 574 | { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 575 | struct channel *ic = sc_ic(sc); |
| 576 | struct channel *oc = sc_oc(sc); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 577 | |
Christopher Faulet | e38534c | 2023-04-13 15:45:24 +0200 | [diff] [blame] | 578 | sc->flags &= ~SC_FL_SHUT_WANTED; |
Christopher Faulet | 208c712 | 2023-04-13 16:16:15 +0200 | [diff] [blame] | 579 | if (sc->flags & SC_FL_SHUT_DONE) |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 580 | return; |
Christopher Faulet | 208c712 | 2023-04-13 16:16:15 +0200 | [diff] [blame] | 581 | sc->flags |= SC_FL_SHUT_DONE; |
Christopher Faulet | 87633c3 | 2023-04-03 18:32:50 +0200 | [diff] [blame] | 582 | oc->flags |= CF_WRITE_EVENT; |
Christopher Faulet | bcdcfad | 2023-02-20 08:36:53 +0100 | [diff] [blame] | 583 | sc_set_hcto(sc); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 584 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 585 | switch (sc->state) { |
Willy Tarreau | 026e8fb | 2022-05-17 19:47:17 +0200 | [diff] [blame] | 586 | case SC_ST_RDY: |
| 587 | case SC_ST_EST: |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 588 | /* we have to shut before closing, otherwise some short messages |
| 589 | * may never leave the system, especially when there are remaining |
| 590 | * unread data in the socket input buffer, or when nolinger is set. |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 591 | * However, if SC_FL_NOLINGER is explicitly set, we know there is |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 592 | * no risk so we close both sides immediately. |
| 593 | */ |
Christopher Faulet | ca5309a | 2023-04-17 16:17:32 +0200 | [diff] [blame] | 594 | if (!(sc->flags & (SC_FL_ERROR|SC_FL_NOLINGER|SC_FL_EOS|SC_FL_ABRT_DONE)) && |
Christopher Faulet | ad46e52 | 2023-04-14 11:59:15 +0200 | [diff] [blame] | 595 | !(ic->flags & CF_DONT_READ)) |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 596 | return; |
| 597 | |
Willy Tarreau | 476c280 | 2022-11-14 07:36:42 +0100 | [diff] [blame] | 598 | __fallthrough; |
Willy Tarreau | 026e8fb | 2022-05-17 19:47:17 +0200 | [diff] [blame] | 599 | case SC_ST_CON: |
| 600 | case SC_ST_CER: |
| 601 | case SC_ST_QUE: |
| 602 | case SC_ST_TAR: |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 603 | /* Note that none of these states may happen with applets */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 604 | sc->state = SC_ST_DIS; |
Willy Tarreau | 476c280 | 2022-11-14 07:36:42 +0100 | [diff] [blame] | 605 | __fallthrough; |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 606 | default: |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 607 | sc->flags &= ~SC_FL_NOLINGER; |
Christopher Faulet | 0c370ee | 2023-04-13 16:05:13 +0200 | [diff] [blame] | 608 | sc->flags |= SC_FL_ABRT_DONE; |
Christopher Faulet | ca67992 | 2022-07-20 13:24:04 +0200 | [diff] [blame] | 609 | if (sc->flags & SC_FL_ISBACK) |
| 610 | __sc_strm(sc)->conn_exp = TICK_ETERNITY; |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | /* note that if the task exists, it must unregister itself once it runs */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 614 | if (!(sc->flags & SC_FL_DONT_WAKE)) |
| 615 | task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | /* default chk_rcv function for scheduled tasks */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 619 | static void sc_app_chk_rcv(struct stconn *sc) |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 620 | { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 621 | struct channel *ic = sc_ic(sc); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 622 | |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 623 | if (ic->pipe) { |
| 624 | /* stop reading */ |
Christopher Faulet | 7b3d38a | 2023-05-05 11:28:45 +0200 | [diff] [blame] | 625 | sc_need_room(sc, -1); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 626 | } |
| 627 | else { |
| 628 | /* (re)start reading */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 629 | if (!(sc->flags & SC_FL_DONT_WAKE)) |
| 630 | task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 631 | } |
| 632 | } |
| 633 | |
| 634 | /* default chk_snd function for scheduled tasks */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 635 | static void sc_app_chk_snd(struct stconn *sc) |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 636 | { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 637 | struct channel *oc = sc_oc(sc); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 638 | |
Christopher Faulet | 208c712 | 2023-04-13 16:16:15 +0200 | [diff] [blame] | 639 | if (unlikely(sc->state != SC_ST_EST || (sc->flags & SC_FL_SHUT_DONE))) |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 640 | return; |
| 641 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 642 | if (!sc_ep_test(sc, SE_FL_WAIT_DATA) || /* not waiting for data */ |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 643 | channel_is_empty(oc)) /* called with nothing to send ! */ |
| 644 | return; |
| 645 | |
| 646 | /* Otherwise there are remaining data to be sent in the buffer, |
| 647 | * so we tell the handler. |
| 648 | */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 649 | sc_ep_clr(sc, SE_FL_WAIT_DATA); |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 650 | if (!(sc->flags & SC_FL_DONT_WAKE)) |
| 651 | task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | /* |
Willy Tarreau | 3a3f480 | 2022-05-17 18:28:19 +0200 | [diff] [blame] | 655 | * This function performs a shutdown-read on a stream connector attached to |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 656 | * a connection in a connected or init state (it does nothing for other |
| 657 | * states). It either shuts the read side or marks itself as closed. The buffer |
Willy Tarreau | 3a3f480 | 2022-05-17 18:28:19 +0200 | [diff] [blame] | 658 | * flags are updated to reflect the new state. If the stream connector has |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 659 | * SC_FL_NOHALF, we also forward the close to the write side. If a control |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 660 | * layer is defined, then it is supposed to be a socket layer and file |
| 661 | * descriptors are then shutdown or closed accordingly. The function |
| 662 | * automatically disables polling if needed. |
| 663 | */ |
Christopher Faulet | cfc11c0 | 2023-04-13 16:10:23 +0200 | [diff] [blame] | 664 | static void sc_app_abort_conn(struct stconn *sc) |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 665 | { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 666 | struct channel *ic = sc_ic(sc); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 667 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 668 | BUG_ON(!sc_conn(sc)); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 669 | |
Christopher Faulet | ca5309a | 2023-04-17 16:17:32 +0200 | [diff] [blame] | 670 | if (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)) |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 671 | return; |
Christopher Faulet | 0c370ee | 2023-04-13 16:05:13 +0200 | [diff] [blame] | 672 | sc->flags |= SC_FL_ABRT_DONE; |
Christopher Faulet | 87633c3 | 2023-04-03 18:32:50 +0200 | [diff] [blame] | 673 | ic->flags |= CF_READ_EVENT; |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 674 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 675 | if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST)) |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 676 | return; |
| 677 | |
Christopher Faulet | 208c712 | 2023-04-13 16:16:15 +0200 | [diff] [blame] | 678 | if (sc->flags & SC_FL_SHUT_DONE) { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 679 | sc_conn_shut(sc); |
| 680 | sc->state = SC_ST_DIS; |
Christopher Faulet | ca67992 | 2022-07-20 13:24:04 +0200 | [diff] [blame] | 681 | if (sc->flags & SC_FL_ISBACK) |
| 682 | __sc_strm(sc)->conn_exp = TICK_ETERNITY; |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 683 | } |
Christopher Faulet | b2b1c3a | 2023-04-13 16:23:48 +0200 | [diff] [blame] | 684 | else if (sc_cond_forward_shut(sc)) |
| 685 | return sc_app_shut_conn(sc); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | /* |
Willy Tarreau | 3a3f480 | 2022-05-17 18:28:19 +0200 | [diff] [blame] | 689 | * This function performs a shutdown-write on a stream connector attached to |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 690 | * a connection in a connected or init state (it does nothing for other |
| 691 | * states). It either shuts the write side or marks itself as closed. The |
| 692 | * buffer flags are updated to reflect the new state. It does also close |
Willy Tarreau | e68bc61 | 2022-05-27 11:23:05 +0200 | [diff] [blame] | 693 | * everything if the SC was marked as being in error state. If there is a |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 694 | * data-layer shutdown, it is called. |
| 695 | */ |
Christopher Faulet | b2b1c3a | 2023-04-13 16:23:48 +0200 | [diff] [blame] | 696 | static void sc_app_shut_conn(struct stconn *sc) |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 697 | { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 698 | struct channel *ic = sc_ic(sc); |
| 699 | struct channel *oc = sc_oc(sc); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 700 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 701 | BUG_ON(!sc_conn(sc)); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 702 | |
Christopher Faulet | e38534c | 2023-04-13 15:45:24 +0200 | [diff] [blame] | 703 | sc->flags &= ~SC_FL_SHUT_WANTED; |
Christopher Faulet | 208c712 | 2023-04-13 16:16:15 +0200 | [diff] [blame] | 704 | if (sc->flags & SC_FL_SHUT_DONE) |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 705 | return; |
Christopher Faulet | 208c712 | 2023-04-13 16:16:15 +0200 | [diff] [blame] | 706 | sc->flags |= SC_FL_SHUT_DONE; |
Christopher Faulet | 87633c3 | 2023-04-03 18:32:50 +0200 | [diff] [blame] | 707 | oc->flags |= CF_WRITE_EVENT; |
Christopher Faulet | bcdcfad | 2023-02-20 08:36:53 +0100 | [diff] [blame] | 708 | sc_set_hcto(sc); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 709 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 710 | switch (sc->state) { |
Willy Tarreau | 026e8fb | 2022-05-17 19:47:17 +0200 | [diff] [blame] | 711 | case SC_ST_RDY: |
| 712 | case SC_ST_EST: |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 713 | /* we have to shut before closing, otherwise some short messages |
| 714 | * may never leave the system, especially when there are remaining |
| 715 | * unread data in the socket input buffer, or when nolinger is set. |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 716 | * However, if SC_FL_NOLINGER is explicitly set, we know there is |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 717 | * no risk so we close both sides immediately. |
| 718 | */ |
| 719 | |
Christopher Faulet | 25d9fe5 | 2023-04-14 12:09:35 +0200 | [diff] [blame] | 720 | if (sc->flags & SC_FL_ERROR) { |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 721 | /* quick close, the socket is already shut anyway */ |
| 722 | } |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 723 | else if (sc->flags & SC_FL_NOLINGER) { |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 724 | /* unclean data-layer shutdown, typically an aborted request |
| 725 | * or a forwarded shutdown from a client to a server due to |
| 726 | * option abortonclose. No need for the TLS layer to try to |
| 727 | * emit a shutdown message. |
| 728 | */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 729 | sc_conn_shutw(sc, CO_SHW_SILENT); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 730 | } |
| 731 | else { |
| 732 | /* clean data-layer shutdown. This only happens on the |
| 733 | * frontend side, or on the backend side when forwarding |
| 734 | * a client close in TCP mode or in HTTP TUNNEL mode |
| 735 | * while option abortonclose is set. We want the TLS |
| 736 | * layer to try to signal it to the peer before we close. |
| 737 | */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 738 | sc_conn_shutw(sc, CO_SHW_NORMAL); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 739 | |
Christopher Faulet | ca5309a | 2023-04-17 16:17:32 +0200 | [diff] [blame] | 740 | if (!(sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)) && !(ic->flags & CF_DONT_READ)) |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 741 | return; |
| 742 | } |
| 743 | |
Willy Tarreau | 476c280 | 2022-11-14 07:36:42 +0100 | [diff] [blame] | 744 | __fallthrough; |
Willy Tarreau | 026e8fb | 2022-05-17 19:47:17 +0200 | [diff] [blame] | 745 | case SC_ST_CON: |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 746 | /* we may have to close a pending connection, and mark the |
Christopher Faulet | cfc11c0 | 2023-04-13 16:10:23 +0200 | [diff] [blame] | 747 | * response buffer as abort |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 748 | */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 749 | sc_conn_shut(sc); |
Willy Tarreau | 476c280 | 2022-11-14 07:36:42 +0100 | [diff] [blame] | 750 | __fallthrough; |
Willy Tarreau | 026e8fb | 2022-05-17 19:47:17 +0200 | [diff] [blame] | 751 | case SC_ST_CER: |
| 752 | case SC_ST_QUE: |
| 753 | case SC_ST_TAR: |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 754 | sc->state = SC_ST_DIS; |
Willy Tarreau | 476c280 | 2022-11-14 07:36:42 +0100 | [diff] [blame] | 755 | __fallthrough; |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 756 | default: |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 757 | sc->flags &= ~SC_FL_NOLINGER; |
Christopher Faulet | 0c370ee | 2023-04-13 16:05:13 +0200 | [diff] [blame] | 758 | sc->flags |= SC_FL_ABRT_DONE; |
Christopher Faulet | ca67992 | 2022-07-20 13:24:04 +0200 | [diff] [blame] | 759 | if (sc->flags & SC_FL_ISBACK) |
| 760 | __sc_strm(sc)->conn_exp = TICK_ETERNITY; |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 761 | } |
| 762 | } |
| 763 | |
Willy Tarreau | 3a3f480 | 2022-05-17 18:28:19 +0200 | [diff] [blame] | 764 | /* This function is used for inter-stream connector calls. It is called by the |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 765 | * consumer to inform the producer side that it may be interested in checking |
| 766 | * for free space in the buffer. Note that it intentionally does not update |
| 767 | * timeouts, so that we can still check them later at wake-up. This function is |
Willy Tarreau | 3a3f480 | 2022-05-17 18:28:19 +0200 | [diff] [blame] | 768 | * dedicated to connection-based stream connectors. |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 769 | */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 770 | static void sc_app_chk_rcv_conn(struct stconn *sc) |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 771 | { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 772 | BUG_ON(!sc_conn(sc)); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 773 | |
| 774 | /* (re)start reading */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 775 | if (sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST)) |
| 776 | tasklet_wakeup(sc->wait_event.tasklet); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | |
Willy Tarreau | 3a3f480 | 2022-05-17 18:28:19 +0200 | [diff] [blame] | 780 | /* This function is used for inter-stream connector calls. It is called by the |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 781 | * producer to inform the consumer side that it may be interested in checking |
| 782 | * for data in the buffer. Note that it intentionally does not update timeouts, |
| 783 | * so that we can still check them later at wake-up. |
| 784 | */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 785 | static void sc_app_chk_snd_conn(struct stconn *sc) |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 786 | { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 787 | struct channel *oc = sc_oc(sc); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 788 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 789 | BUG_ON(!sc_conn(sc)); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 790 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 791 | if (unlikely(!sc_state_in(sc->state, SC_SB_RDY|SC_SB_EST) || |
Christopher Faulet | 208c712 | 2023-04-13 16:16:15 +0200 | [diff] [blame] | 792 | (sc->flags & SC_FL_SHUT_DONE))) |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 793 | return; |
| 794 | |
| 795 | if (unlikely(channel_is_empty(oc))) /* called with nothing to send ! */ |
| 796 | return; |
| 797 | |
| 798 | if (!oc->pipe && /* spliced data wants to be forwarded ASAP */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 799 | !sc_ep_test(sc, SE_FL_WAIT_DATA)) /* not waiting for data */ |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 800 | return; |
| 801 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 802 | if (!(sc->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(sc_oc(sc))) |
| 803 | sc_conn_send(sc); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 804 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 805 | if (sc_ep_test(sc, SE_FL_ERROR | SE_FL_ERR_PENDING) || sc_is_conn_error(sc)) { |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 806 | /* Write error on the file descriptor */ |
Christopher Faulet | 56a2b60 | 2023-04-14 09:42:59 +0200 | [diff] [blame] | 807 | BUG_ON(sc_ep_test(sc, SE_FL_EOS|SE_FL_ERROR|SE_FL_ERR_PENDING) == (SE_FL_EOS|SE_FL_ERR_PENDING)); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 808 | goto out_wakeup; |
| 809 | } |
| 810 | |
| 811 | /* OK, so now we know that some data might have been sent, and that we may |
| 812 | * have to poll first. We have to do that too if the buffer is not empty. |
| 813 | */ |
| 814 | if (channel_is_empty(oc)) { |
| 815 | /* the connection is established but we can't write. Either the |
| 816 | * buffer is empty, or we just refrain from sending because the |
| 817 | * ->o limit was reached. Maybe we just wrote the last |
| 818 | * chunk and need to close. |
| 819 | */ |
Christopher Faulet | 87633c3 | 2023-04-03 18:32:50 +0200 | [diff] [blame] | 820 | if ((oc->flags & CF_AUTO_CLOSE) && |
Christopher Faulet | 208c712 | 2023-04-13 16:16:15 +0200 | [diff] [blame] | 821 | ((sc->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED)) == SC_FL_SHUT_WANTED) && |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 822 | sc_state_in(sc->state, SC_SB_RDY|SC_SB_EST)) { |
Christopher Faulet | b2b1c3a | 2023-04-13 16:23:48 +0200 | [diff] [blame] | 823 | sc_shutdown(sc); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 824 | goto out_wakeup; |
| 825 | } |
| 826 | |
Christopher Faulet | 208c712 | 2023-04-13 16:16:15 +0200 | [diff] [blame] | 827 | if ((sc->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED)) == 0) |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 828 | sc_ep_set(sc, SE_FL_WAIT_DATA); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 829 | } |
| 830 | else { |
| 831 | /* Otherwise there are remaining data to be sent in the buffer, |
| 832 | * which means we have to poll before doing so. |
| 833 | */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 834 | sc_ep_clr(sc, SE_FL_WAIT_DATA); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 835 | } |
| 836 | |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 837 | /* in case of special condition (error, shutdown, end of write...), we |
| 838 | * have to notify the task. |
| 839 | */ |
Christopher Faulet | 208c712 | 2023-04-13 16:16:15 +0200 | [diff] [blame] | 840 | if (likely((sc->flags & SC_FL_SHUT_DONE) || |
Christopher Faulet | 71c486b | 2023-02-09 14:14:38 +0100 | [diff] [blame] | 841 | ((oc->flags & CF_WRITE_EVENT) && sc->state < SC_ST_EST) || |
| 842 | ((oc->flags & CF_WAKE_WRITE) && |
| 843 | ((channel_is_empty(oc) && !oc->to_forward) || |
| 844 | !sc_state_in(sc->state, SC_SB_EST))))) { |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 845 | out_wakeup: |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 846 | if (!(sc->flags & SC_FL_DONT_WAKE)) |
| 847 | task_wakeup(sc_strm_task(sc), TASK_WOKEN_IO); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 848 | } |
| 849 | } |
| 850 | |
| 851 | /* |
Willy Tarreau | 3a3f480 | 2022-05-17 18:28:19 +0200 | [diff] [blame] | 852 | * This function performs a shutdown-read on a stream connector attached to an |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 853 | * applet in a connected or init state (it does nothing for other states). It |
| 854 | * either shuts the read side or marks itself as closed. The buffer flags are |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 855 | * updated to reflect the new state. If the stream connector has SC_FL_NOHALF, |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 856 | * we also forward the close to the write side. The owner task is woken up if |
| 857 | * it exists. |
| 858 | */ |
Christopher Faulet | cfc11c0 | 2023-04-13 16:10:23 +0200 | [diff] [blame] | 859 | static void sc_app_abort_applet(struct stconn *sc) |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 860 | { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 861 | struct channel *ic = sc_ic(sc); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 862 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 863 | BUG_ON(!sc_appctx(sc)); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 864 | |
Christopher Faulet | ca5309a | 2023-04-17 16:17:32 +0200 | [diff] [blame] | 865 | if (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)) |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 866 | return; |
Christopher Faulet | 0c370ee | 2023-04-13 16:05:13 +0200 | [diff] [blame] | 867 | sc->flags |= SC_FL_ABRT_DONE; |
Christopher Faulet | 87633c3 | 2023-04-03 18:32:50 +0200 | [diff] [blame] | 868 | ic->flags |= CF_READ_EVENT; |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 869 | |
Christopher Faulet | cfc11c0 | 2023-04-13 16:10:23 +0200 | [diff] [blame] | 870 | /* Note: on abort, we don't call the applet */ |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 871 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 872 | if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST)) |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 873 | return; |
| 874 | |
Christopher Faulet | 208c712 | 2023-04-13 16:16:15 +0200 | [diff] [blame] | 875 | if (sc->flags & SC_FL_SHUT_DONE) { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 876 | appctx_shut(__sc_appctx(sc)); |
| 877 | sc->state = SC_ST_DIS; |
Christopher Faulet | ca67992 | 2022-07-20 13:24:04 +0200 | [diff] [blame] | 878 | if (sc->flags & SC_FL_ISBACK) |
| 879 | __sc_strm(sc)->conn_exp = TICK_ETERNITY; |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 880 | } |
Christopher Faulet | b2b1c3a | 2023-04-13 16:23:48 +0200 | [diff] [blame] | 881 | else if (sc_cond_forward_shut(sc)) |
| 882 | return sc_app_shut_applet(sc); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | /* |
Willy Tarreau | 3a3f480 | 2022-05-17 18:28:19 +0200 | [diff] [blame] | 886 | * This function performs a shutdown-write on a stream connector attached to an |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 887 | * applet in a connected or init state (it does nothing for other states). It |
| 888 | * either shuts the write side or marks itself as closed. The buffer flags are |
| 889 | * updated to reflect the new state. It does also close everything if the SI |
| 890 | * was marked as being in error state. The owner task is woken up if it exists. |
| 891 | */ |
Christopher Faulet | b2b1c3a | 2023-04-13 16:23:48 +0200 | [diff] [blame] | 892 | static void sc_app_shut_applet(struct stconn *sc) |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 893 | { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 894 | struct channel *ic = sc_ic(sc); |
| 895 | struct channel *oc = sc_oc(sc); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 896 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 897 | BUG_ON(!sc_appctx(sc)); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 898 | |
Christopher Faulet | e38534c | 2023-04-13 15:45:24 +0200 | [diff] [blame] | 899 | sc->flags &= ~SC_FL_SHUT_WANTED; |
Christopher Faulet | 208c712 | 2023-04-13 16:16:15 +0200 | [diff] [blame] | 900 | if (sc->flags & SC_FL_SHUT_DONE) |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 901 | return; |
Christopher Faulet | 208c712 | 2023-04-13 16:16:15 +0200 | [diff] [blame] | 902 | sc->flags |= SC_FL_SHUT_DONE; |
Christopher Faulet | 87633c3 | 2023-04-03 18:32:50 +0200 | [diff] [blame] | 903 | oc->flags |= CF_WRITE_EVENT; |
Christopher Faulet | bcdcfad | 2023-02-20 08:36:53 +0100 | [diff] [blame] | 904 | sc_set_hcto(sc); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 905 | |
| 906 | /* on shutw we always wake the applet up */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 907 | appctx_wakeup(__sc_appctx(sc)); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 908 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 909 | switch (sc->state) { |
Willy Tarreau | 026e8fb | 2022-05-17 19:47:17 +0200 | [diff] [blame] | 910 | case SC_ST_RDY: |
| 911 | case SC_ST_EST: |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 912 | /* we have to shut before closing, otherwise some short messages |
| 913 | * may never leave the system, especially when there are remaining |
| 914 | * unread data in the socket input buffer, or when nolinger is set. |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 915 | * However, if SC_FL_NOLINGER is explicitly set, we know there is |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 916 | * no risk so we close both sides immediately. |
| 917 | */ |
Christopher Faulet | ca5309a | 2023-04-17 16:17:32 +0200 | [diff] [blame] | 918 | if (!(sc->flags & (SC_FL_ERROR|SC_FL_NOLINGER|SC_FL_EOS|SC_FL_ABRT_DONE)) && |
Christopher Faulet | 87633c3 | 2023-04-03 18:32:50 +0200 | [diff] [blame] | 919 | !(ic->flags & CF_DONT_READ)) |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 920 | return; |
| 921 | |
Willy Tarreau | 476c280 | 2022-11-14 07:36:42 +0100 | [diff] [blame] | 922 | __fallthrough; |
Willy Tarreau | 026e8fb | 2022-05-17 19:47:17 +0200 | [diff] [blame] | 923 | case SC_ST_CON: |
| 924 | case SC_ST_CER: |
| 925 | case SC_ST_QUE: |
| 926 | case SC_ST_TAR: |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 927 | /* Note that none of these states may happen with applets */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 928 | appctx_shut(__sc_appctx(sc)); |
| 929 | sc->state = SC_ST_DIS; |
Willy Tarreau | 476c280 | 2022-11-14 07:36:42 +0100 | [diff] [blame] | 930 | __fallthrough; |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 931 | default: |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 932 | sc->flags &= ~SC_FL_NOLINGER; |
Christopher Faulet | 0c370ee | 2023-04-13 16:05:13 +0200 | [diff] [blame] | 933 | sc->flags |= SC_FL_ABRT_DONE; |
Christopher Faulet | ca67992 | 2022-07-20 13:24:04 +0200 | [diff] [blame] | 934 | if (sc->flags & SC_FL_ISBACK) |
| 935 | __sc_strm(sc)->conn_exp = TICK_ETERNITY; |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 936 | } |
| 937 | } |
| 938 | |
| 939 | /* chk_rcv function for applets */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 940 | static void sc_app_chk_rcv_applet(struct stconn *sc) |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 941 | { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 942 | struct channel *ic = sc_ic(sc); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 943 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 944 | BUG_ON(!sc_appctx(sc)); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 945 | |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 946 | if (!ic->pipe) { |
| 947 | /* (re)start reading */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 948 | appctx_wakeup(__sc_appctx(sc)); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 949 | } |
| 950 | } |
| 951 | |
| 952 | /* chk_snd function for applets */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 953 | static void sc_app_chk_snd_applet(struct stconn *sc) |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 954 | { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 955 | struct channel *oc = sc_oc(sc); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 956 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 957 | BUG_ON(!sc_appctx(sc)); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 958 | |
Christopher Faulet | 208c712 | 2023-04-13 16:16:15 +0200 | [diff] [blame] | 959 | if (unlikely(sc->state != SC_ST_EST || (sc->flags & SC_FL_SHUT_DONE))) |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 960 | return; |
| 961 | |
Christopher Faulet | df8c149 | 2024-02-12 18:30:33 +0100 | [diff] [blame^] | 962 | /* we only wake the applet up if it was waiting for some data and is ready to consume it */ |
| 963 | if (!sc_ep_test(sc, SE_FL_WAIT_DATA|SE_FL_WONT_CONSUME)) |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 964 | return; |
| 965 | |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 966 | if (!channel_is_empty(oc)) { |
| 967 | /* (re)start sending */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 968 | appctx_wakeup(__sc_appctx(sc)); |
Christopher Faulet | 9ffddd5 | 2022-04-01 14:04:29 +0200 | [diff] [blame] | 969 | } |
| 970 | } |
Christopher Faulet | 13045f0 | 2022-04-01 14:23:38 +0200 | [diff] [blame] | 971 | |
| 972 | |
| 973 | /* This function is designed to be called from within the stream handler to |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 974 | * update the input channel's expiration timer and the stream connector's |
Christopher Faulet | 13045f0 | 2022-04-01 14:23:38 +0200 | [diff] [blame] | 975 | * Rx flags based on the channel's flags. It needs to be called only once |
| 976 | * after the channel's flags have settled down, and before they are cleared, |
| 977 | * though it doesn't harm to call it as often as desired (it just slightly |
| 978 | * hurts performance). It must not be called from outside of the stream |
| 979 | * handler, as what it does will be used to compute the stream task's |
| 980 | * expiration. |
| 981 | */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 982 | void sc_update_rx(struct stconn *sc) |
Christopher Faulet | 13045f0 | 2022-04-01 14:23:38 +0200 | [diff] [blame] | 983 | { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 984 | struct channel *ic = sc_ic(sc); |
Christopher Faulet | 13045f0 | 2022-04-01 14:23:38 +0200 | [diff] [blame] | 985 | |
Christopher Faulet | ca5309a | 2023-04-17 16:17:32 +0200 | [diff] [blame] | 986 | if (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)) |
Christopher Faulet | 13045f0 | 2022-04-01 14:23:38 +0200 | [diff] [blame] | 987 | return; |
Christopher Faulet | 13045f0 | 2022-04-01 14:23:38 +0200 | [diff] [blame] | 988 | |
Christopher Faulet | fab82bf | 2023-05-05 11:30:16 +0200 | [diff] [blame] | 989 | /* Unblock the SC if it needs room and the free space is large enough (0 |
| 990 | * means it can always be unblocked). Do not unblock it if -1 was |
| 991 | * specified. |
| 992 | */ |
| 993 | if (!sc->room_needed || (sc->room_needed > 0 && channel_recv_max(ic) >= sc->room_needed)) |
| 994 | sc_have_room(sc); |
| 995 | |
Christopher Faulet | 13045f0 | 2022-04-01 14:23:38 +0200 | [diff] [blame] | 996 | /* Read not closed, update FD status and timeout for reads */ |
| 997 | if (ic->flags & CF_DONT_READ) |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 998 | sc_wont_read(sc); |
Christopher Faulet | 13045f0 | 2022-04-01 14:23:38 +0200 | [diff] [blame] | 999 | else |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1000 | sc_will_read(sc); |
Christopher Faulet | 13045f0 | 2022-04-01 14:23:38 +0200 | [diff] [blame] | 1001 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1002 | sc_chk_rcv(sc); |
Christopher Faulet | 13045f0 | 2022-04-01 14:23:38 +0200 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | /* This function is designed to be called from within the stream handler to |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 1006 | * update the output channel's expiration timer and the stream connector's |
Christopher Faulet | 13045f0 | 2022-04-01 14:23:38 +0200 | [diff] [blame] | 1007 | * Tx flags based on the channel's flags. It needs to be called only once |
| 1008 | * after the channel's flags have settled down, and before they are cleared, |
| 1009 | * though it doesn't harm to call it as often as desired (it just slightly |
| 1010 | * hurts performance). It must not be called from outside of the stream |
| 1011 | * handler, as what it does will be used to compute the stream task's |
| 1012 | * expiration. |
| 1013 | */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1014 | void sc_update_tx(struct stconn *sc) |
Christopher Faulet | 13045f0 | 2022-04-01 14:23:38 +0200 | [diff] [blame] | 1015 | { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1016 | struct channel *oc = sc_oc(sc); |
Christopher Faulet | 13045f0 | 2022-04-01 14:23:38 +0200 | [diff] [blame] | 1017 | |
Christopher Faulet | 208c712 | 2023-04-13 16:16:15 +0200 | [diff] [blame] | 1018 | if (sc->flags & SC_FL_SHUT_DONE) |
Christopher Faulet | 13045f0 | 2022-04-01 14:23:38 +0200 | [diff] [blame] | 1019 | return; |
| 1020 | |
| 1021 | /* Write not closed, update FD status and timeout for writes */ |
| 1022 | if (channel_is_empty(oc)) { |
| 1023 | /* stop writing */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1024 | if (!sc_ep_test(sc, SE_FL_WAIT_DATA)) { |
Christopher Faulet | e38534c | 2023-04-13 15:45:24 +0200 | [diff] [blame] | 1025 | if ((sc->flags & SC_FL_SHUT_WANTED) == 0) |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1026 | sc_ep_set(sc, SE_FL_WAIT_DATA); |
Christopher Faulet | 13045f0 | 2022-04-01 14:23:38 +0200 | [diff] [blame] | 1027 | } |
| 1028 | return; |
| 1029 | } |
| 1030 | |
Christopher Faulet | 15315d6 | 2023-02-20 08:23:51 +0100 | [diff] [blame] | 1031 | /* (re)start writing */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1032 | sc_ep_clr(sc, SE_FL_WAIT_DATA); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1033 | } |
| 1034 | |
Willy Tarreau | 19c65a9 | 2022-05-27 08:49:24 +0200 | [diff] [blame] | 1035 | /* This function is the equivalent to sc_update() except that it's |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1036 | * designed to be called from outside the stream handlers, typically the lower |
| 1037 | * layers (applets, connections) after I/O completion. After updating the stream |
| 1038 | * interface and timeouts, it will try to forward what can be forwarded, then to |
| 1039 | * wake the associated task up if an important event requires special handling. |
Willy Tarreau | 15252cd | 2022-05-25 16:36:21 +0200 | [diff] [blame] | 1040 | * It may update SE_FL_WAIT_DATA and/or SC_FL_NEED_ROOM, that the callers are |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1041 | * encouraged to watch to take appropriate action. |
Willy Tarreau | 19c65a9 | 2022-05-27 08:49:24 +0200 | [diff] [blame] | 1042 | * It should not be called from within the stream itself, sc_update() |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1043 | * is designed for this. |
| 1044 | */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1045 | static void sc_notify(struct stconn *sc) |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1046 | { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1047 | struct channel *ic = sc_ic(sc); |
| 1048 | struct channel *oc = sc_oc(sc); |
Willy Tarreau | e68bc61 | 2022-05-27 11:23:05 +0200 | [diff] [blame] | 1049 | struct stconn *sco = sc_opposite(sc); |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1050 | struct task *task = sc_strm_task(sc); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1051 | |
| 1052 | /* process consumer side */ |
| 1053 | if (channel_is_empty(oc)) { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1054 | struct connection *conn = sc_conn(sc); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1055 | |
Christopher Faulet | 208c712 | 2023-04-13 16:16:15 +0200 | [diff] [blame] | 1056 | if (((sc->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED)) == SC_FL_SHUT_WANTED) && |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1057 | (sc->state == SC_ST_EST) && (!conn || !(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS)))) |
Christopher Faulet | b2b1c3a | 2023-04-13 16:23:48 +0200 | [diff] [blame] | 1058 | sc_shutdown(sc); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | /* indicate that we may be waiting for data from the output channel or |
Christopher Faulet | e38534c | 2023-04-13 15:45:24 +0200 | [diff] [blame] | 1062 | * we're about to close and can't expect more data if SC_FL_SHUT_WANTED is there. |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1063 | */ |
Christopher Faulet | 208c712 | 2023-04-13 16:16:15 +0200 | [diff] [blame] | 1064 | if (!(sc->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED))) |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1065 | sc_ep_set(sc, SE_FL_WAIT_DATA); |
Christopher Faulet | 208c712 | 2023-04-13 16:16:15 +0200 | [diff] [blame] | 1066 | else if ((sc->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED)) == SC_FL_SHUT_WANTED) |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1067 | sc_ep_clr(sc, SE_FL_WAIT_DATA); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1068 | |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1069 | if (oc->flags & CF_DONT_READ) |
Willy Tarreau | e68bc61 | 2022-05-27 11:23:05 +0200 | [diff] [blame] | 1070 | sc_wont_read(sco); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1071 | else |
Willy Tarreau | e68bc61 | 2022-05-27 11:23:05 +0200 | [diff] [blame] | 1072 | sc_will_read(sco); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1073 | |
| 1074 | /* Notify the other side when we've injected data into the IC that |
| 1075 | * needs to be forwarded. We can do fast-forwarding as soon as there |
| 1076 | * are output data, but we avoid doing this if some of the data are |
| 1077 | * not yet scheduled for being forwarded, because it is very likely |
| 1078 | * that it will be done again immediately afterwards once the following |
Willy Tarreau | 15252cd | 2022-05-25 16:36:21 +0200 | [diff] [blame] | 1079 | * data are parsed (eg: HTTP chunking). We only clear SC_FL_NEED_ROOM |
| 1080 | * once we've emptied *some* of the output buffer, and not just when |
| 1081 | * there is available room, because applets are often forced to stop |
| 1082 | * before the buffer is full. We must not stop based on input data |
| 1083 | * alone because an HTTP parser might need more data to complete the |
| 1084 | * parsing. |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1085 | */ |
| 1086 | if (!channel_is_empty(ic) && |
Willy Tarreau | e68bc61 | 2022-05-27 11:23:05 +0200 | [diff] [blame] | 1087 | sc_ep_test(sco, SE_FL_WAIT_DATA) && |
Christopher Faulet | d114208 | 2023-11-17 11:59:33 +0100 | [diff] [blame] | 1088 | (!(sc->flags & SC_FL_SND_EXP_MORE) || channel_full(ic, co_data(ic)) || channel_input_data(ic) == 0)) { |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1089 | int new_len, last_len; |
| 1090 | |
| 1091 | last_len = co_data(ic); |
| 1092 | if (ic->pipe) |
| 1093 | last_len += ic->pipe->data; |
| 1094 | |
Willy Tarreau | e68bc61 | 2022-05-27 11:23:05 +0200 | [diff] [blame] | 1095 | sc_chk_snd(sco); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1096 | |
| 1097 | new_len = co_data(ic); |
| 1098 | if (ic->pipe) |
| 1099 | new_len += ic->pipe->data; |
| 1100 | |
| 1101 | /* check if the consumer has freed some space either in the |
| 1102 | * buffer or in the pipe. |
| 1103 | */ |
Christopher Faulet | 18b3309 | 2023-05-05 11:40:07 +0200 | [diff] [blame] | 1104 | if (!sc->room_needed || (new_len < last_len && (sc->room_needed < 0 || channel_recv_max(ic) >= sc->room_needed))) |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1105 | sc_have_room(sc); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | if (!(ic->flags & CF_DONT_READ)) |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1109 | sc_will_read(sc); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1110 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1111 | sc_chk_rcv(sc); |
Willy Tarreau | e68bc61 | 2022-05-27 11:23:05 +0200 | [diff] [blame] | 1112 | sc_chk_rcv(sco); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1113 | |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1114 | /* wake the task up only when needed */ |
Christopher Faulet | 285f761 | 2022-12-12 08:28:55 +0100 | [diff] [blame] | 1115 | if (/* changes on the production side that must be handled: |
Christopher Faulet | ad46e52 | 2023-04-14 11:59:15 +0200 | [diff] [blame] | 1116 | * - An error on receipt: SC_FL_ERROR |
Christopher Faulet | ca5309a | 2023-04-17 16:17:32 +0200 | [diff] [blame] | 1117 | * - A read event: shutdown for reads (CF_READ_EVENT + EOS/ABRT_DONE) |
Christopher Faulet | 904763f | 2023-03-22 14:53:11 +0100 | [diff] [blame] | 1118 | * end of input (CF_READ_EVENT + SC_FL_EOI) |
Christopher Faulet | 285f761 | 2022-12-12 08:28:55 +0100 | [diff] [blame] | 1119 | * data received and no fast-forwarding (CF_READ_EVENT + !to_forward) |
| 1120 | * read event while consumer side is not established (CF_READ_EVENT + sco->state != SC_ST_EST) |
| 1121 | */ |
Christopher Faulet | ca5309a | 2023-04-17 16:17:32 +0200 | [diff] [blame] | 1122 | ((ic->flags & CF_READ_EVENT) && ((sc->flags & SC_FL_EOI) || (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)) || !ic->to_forward || sco->state != SC_ST_EST)) || |
Christopher Faulet | 25d9fe5 | 2023-04-14 12:09:35 +0200 | [diff] [blame] | 1123 | (sc->flags & SC_FL_ERROR) || |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1124 | |
| 1125 | /* changes on the consumption side */ |
Christopher Faulet | 2e56a73 | 2023-01-26 16:18:09 +0100 | [diff] [blame] | 1126 | sc_ep_test(sc, SE_FL_ERR_PENDING) || |
Christopher Faulet | d898841 | 2022-12-20 18:10:04 +0100 | [diff] [blame] | 1127 | ((oc->flags & CF_WRITE_EVENT) && |
| 1128 | ((sc->state < SC_ST_EST) || |
Christopher Faulet | 208c712 | 2023-04-13 16:16:15 +0200 | [diff] [blame] | 1129 | (sc->flags & SC_FL_SHUT_DONE) || |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1130 | (((oc->flags & CF_WAKE_WRITE) || |
Christopher Faulet | 87633c3 | 2023-04-03 18:32:50 +0200 | [diff] [blame] | 1131 | (!(oc->flags & CF_AUTO_CLOSE) && |
Christopher Faulet | 208c712 | 2023-04-13 16:16:15 +0200 | [diff] [blame] | 1132 | !(sc->flags & (SC_FL_SHUT_WANTED|SC_FL_SHUT_DONE)))) && |
Christopher Faulet | 87633c3 | 2023-04-03 18:32:50 +0200 | [diff] [blame] | 1133 | (sco->state != SC_ST_EST || |
| 1134 | (channel_is_empty(oc) && !oc->to_forward)))))) { |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1135 | task_wakeup(task, TASK_WOKEN_IO); |
| 1136 | } |
Christopher Faulet | 13a8a59 | 2023-09-06 09:09:10 +0200 | [diff] [blame] | 1137 | else { |
Christopher Faulet | a93d8d2 | 2023-11-09 09:54:51 +0100 | [diff] [blame] | 1138 | /* Update expiration date for the task and requeue it if not already expired */ |
| 1139 | if (!tick_is_expired(task->expire, now_ms)) { |
| 1140 | task->expire = tick_first(task->expire, sc_ep_rcv_ex(sc)); |
| 1141 | task->expire = tick_first(task->expire, sc_ep_snd_ex(sc)); |
| 1142 | task->expire = tick_first(task->expire, sc_ep_rcv_ex(sco)); |
| 1143 | task->expire = tick_first(task->expire, sc_ep_snd_ex(sco)); |
| 1144 | task->expire = tick_first(task->expire, ic->analyse_exp); |
| 1145 | task->expire = tick_first(task->expire, oc->analyse_exp); |
| 1146 | task->expire = tick_first(task->expire, __sc_strm(sc)->conn_exp); |
Christopher Faulet | 13a8a59 | 2023-09-06 09:09:10 +0200 | [diff] [blame] | 1147 | |
Christopher Faulet | a93d8d2 | 2023-11-09 09:54:51 +0100 | [diff] [blame] | 1148 | task_queue(task); |
| 1149 | } |
Christopher Faulet | 13a8a59 | 2023-09-06 09:09:10 +0200 | [diff] [blame] | 1150 | } |
| 1151 | |
Christopher Faulet | 2e56a73 | 2023-01-26 16:18:09 +0100 | [diff] [blame] | 1152 | if (ic->flags & CF_READ_EVENT) |
Christopher Faulet | 9a790f6 | 2023-03-16 14:40:03 +0100 | [diff] [blame] | 1153 | sc->flags &= ~SC_FL_RCV_ONCE; |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | /* |
Christopher Faulet | 1aec6c9 | 2023-04-17 17:29:29 +0200 | [diff] [blame] | 1157 | * This function propagates an end-of-stream received on a socket-based connection. |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 1158 | * It updates the stream connector. If the stream connector has SC_FL_NOHALF, |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1159 | * the close is also forwarded to the write side as an abort. |
| 1160 | */ |
Christopher Faulet | 1aec6c9 | 2023-04-17 17:29:29 +0200 | [diff] [blame] | 1161 | static void sc_conn_eos(struct stconn *sc) |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1162 | { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1163 | struct channel *ic = sc_ic(sc); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1164 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1165 | BUG_ON(!sc_conn(sc)); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1166 | |
Christopher Faulet | ca5309a | 2023-04-17 16:17:32 +0200 | [diff] [blame] | 1167 | if (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)) |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1168 | return; |
Christopher Faulet | 1aec6c9 | 2023-04-17 17:29:29 +0200 | [diff] [blame] | 1169 | sc->flags |= SC_FL_EOS; |
Christopher Faulet | 87633c3 | 2023-04-03 18:32:50 +0200 | [diff] [blame] | 1170 | ic->flags |= CF_READ_EVENT; |
Christopher Faulet | 4c13568 | 2023-02-16 11:09:31 +0100 | [diff] [blame] | 1171 | sc_ep_report_read_activity(sc); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1172 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1173 | if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST)) |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1174 | return; |
| 1175 | |
Christopher Faulet | 208c712 | 2023-04-13 16:16:15 +0200 | [diff] [blame] | 1176 | if (sc->flags & SC_FL_SHUT_DONE) |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1177 | goto do_close; |
| 1178 | |
Christopher Faulet | b2b1c3a | 2023-04-13 16:23:48 +0200 | [diff] [blame] | 1179 | if (sc_cond_forward_shut(sc)) { |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1180 | /* we want to immediately forward this close to the write side */ |
| 1181 | /* force flag on ssl to keep stream in cache */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1182 | sc_conn_shutw(sc, CO_SHW_SILENT); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1183 | goto do_close; |
| 1184 | } |
| 1185 | |
| 1186 | /* otherwise that's just a normal read shutdown */ |
| 1187 | return; |
| 1188 | |
| 1189 | do_close: |
Willy Tarreau | f61dd19 | 2022-05-27 09:00:19 +0200 | [diff] [blame] | 1190 | /* OK we completely close the socket here just as if we went through sc_shut[rw]() */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1191 | sc_conn_shut(sc); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1192 | |
Christopher Faulet | e38534c | 2023-04-13 15:45:24 +0200 | [diff] [blame] | 1193 | sc->flags &= ~SC_FL_SHUT_WANTED; |
Christopher Faulet | 208c712 | 2023-04-13 16:16:15 +0200 | [diff] [blame] | 1194 | sc->flags |= SC_FL_SHUT_DONE; |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1195 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1196 | sc->state = SC_ST_DIS; |
Christopher Faulet | ca67992 | 2022-07-20 13:24:04 +0200 | [diff] [blame] | 1197 | if (sc->flags & SC_FL_ISBACK) |
| 1198 | __sc_strm(sc)->conn_exp = TICK_ETERNITY; |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1199 | return; |
| 1200 | } |
| 1201 | |
| 1202 | /* |
| 1203 | * This is the callback which is called by the connection layer to receive data |
| 1204 | * into the buffer from the connection. It iterates over the mux layer's |
| 1205 | * rcv_buf function. |
| 1206 | */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1207 | static int sc_conn_recv(struct stconn *sc) |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1208 | { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1209 | struct connection *conn = __sc_conn(sc); |
| 1210 | struct channel *ic = sc_ic(sc); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1211 | int ret, max, cur_read = 0; |
| 1212 | int read_poll = MAX_READ_POLL_LOOPS; |
| 1213 | int flags = 0; |
| 1214 | |
| 1215 | /* If not established yet, do nothing. */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1216 | if (sc->state != SC_ST_EST) |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1217 | return 0; |
| 1218 | |
Willy Tarreau | 462b989 | 2022-05-18 18:06:53 +0200 | [diff] [blame] | 1219 | /* If another call to sc_conn_recv() failed, and we subscribed to |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1220 | * recv events already, give up now. |
| 1221 | */ |
Christopher Faulet | 9512588 | 2023-04-12 18:35:18 +0200 | [diff] [blame] | 1222 | if ((sc->wait_event.events & SUB_RETRY_RECV) || sc_waiting_room(sc)) |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1223 | return 0; |
| 1224 | |
Christopher Faulet | cfc11c0 | 2023-04-13 16:10:23 +0200 | [diff] [blame] | 1225 | /* maybe we were called immediately after an asynchronous abort */ |
Christopher Faulet | ca5309a | 2023-04-17 16:17:32 +0200 | [diff] [blame] | 1226 | if (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)) |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1227 | return 1; |
| 1228 | |
| 1229 | /* we must wait because the mux is not installed yet */ |
| 1230 | if (!conn->mux) |
| 1231 | return 0; |
| 1232 | |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1233 | /* stop immediately on errors. Note that we DON'T want to stop on |
| 1234 | * POLL_ERR, as the poller might report a write error while there |
| 1235 | * are still data available in the recv buffer. This typically |
| 1236 | * happens when we send too large a request to a backend server |
| 1237 | * which rejects it before reading it all. |
| 1238 | */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1239 | if (!sc_ep_test(sc, SE_FL_RCV_MORE)) { |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1240 | if (!conn_xprt_ready(conn)) |
| 1241 | return 0; |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1242 | if (sc_ep_test(sc, SE_FL_ERROR)) |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1243 | goto end_recv; |
| 1244 | } |
| 1245 | |
| 1246 | /* prepare to detect if the mux needs more room */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1247 | sc_ep_clr(sc, SE_FL_WANT_ROOM); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1248 | |
| 1249 | if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) && !co_data(ic) && |
| 1250 | global.tune.idle_timer && |
| 1251 | (unsigned short)(now_ms - ic->last_read) >= global.tune.idle_timer) { |
| 1252 | /* The buffer was empty and nothing was transferred for more |
| 1253 | * than one second. This was caused by a pause and not by |
| 1254 | * congestion. Reset any streaming mode to reduce latency. |
| 1255 | */ |
| 1256 | ic->xfer_small = 0; |
| 1257 | ic->xfer_large = 0; |
| 1258 | ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST); |
| 1259 | } |
| 1260 | |
| 1261 | /* First, let's see if we may splice data across the channel without |
| 1262 | * using a buffer. |
| 1263 | */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1264 | if (sc_ep_test(sc, SE_FL_MAY_SPLICE) && |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1265 | (ic->pipe || ic->to_forward >= MIN_SPLICE_FORWARD) && |
| 1266 | ic->flags & CF_KERN_SPLICING) { |
Christopher Faulet | d114208 | 2023-11-17 11:59:33 +0100 | [diff] [blame] | 1267 | if (channel_data(ic)) { |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1268 | /* We're embarrassed, there are already data pending in |
| 1269 | * the buffer and we don't want to have them at two |
| 1270 | * locations at a time. Let's indicate we need some |
| 1271 | * place and ask the consumer to hurry. |
| 1272 | */ |
| 1273 | flags |= CO_RFL_BUF_FLUSH; |
| 1274 | goto abort_splice; |
| 1275 | } |
| 1276 | |
| 1277 | if (unlikely(ic->pipe == NULL)) { |
| 1278 | if (pipes_used >= global.maxpipes || !(ic->pipe = get_pipe())) { |
| 1279 | ic->flags &= ~CF_KERN_SPLICING; |
| 1280 | goto abort_splice; |
| 1281 | } |
| 1282 | } |
| 1283 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1284 | ret = conn->mux->rcv_pipe(sc, ic->pipe, ic->to_forward); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1285 | if (ret < 0) { |
| 1286 | /* splice not supported on this end, let's disable it */ |
| 1287 | ic->flags &= ~CF_KERN_SPLICING; |
| 1288 | goto abort_splice; |
| 1289 | } |
| 1290 | |
| 1291 | if (ret > 0) { |
| 1292 | if (ic->to_forward != CHN_INFINITE_FORWARD) |
| 1293 | ic->to_forward -= ret; |
| 1294 | ic->total += ret; |
| 1295 | cur_read += ret; |
Christopher Faulet | 285f761 | 2022-12-12 08:28:55 +0100 | [diff] [blame] | 1296 | ic->flags |= CF_READ_EVENT; |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1297 | } |
| 1298 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1299 | if (sc_ep_test(sc, SE_FL_EOS | SE_FL_ERROR)) |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1300 | goto end_recv; |
| 1301 | |
| 1302 | if (conn->flags & CO_FL_WAIT_ROOM) { |
| 1303 | /* the pipe is full or we have read enough data that it |
| 1304 | * could soon be full. Let's stop before needing to poll. |
| 1305 | */ |
Christopher Faulet | 7b3d38a | 2023-05-05 11:28:45 +0200 | [diff] [blame] | 1306 | sc_need_room(sc, 0); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1307 | goto done_recv; |
| 1308 | } |
| 1309 | |
| 1310 | /* splice not possible (anymore), let's go on on standard copy */ |
| 1311 | } |
| 1312 | |
| 1313 | abort_splice: |
| 1314 | if (ic->pipe && unlikely(!ic->pipe->data)) { |
| 1315 | put_pipe(ic->pipe); |
| 1316 | ic->pipe = NULL; |
| 1317 | } |
| 1318 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1319 | if (ic->pipe && ic->to_forward && !(flags & CO_RFL_BUF_FLUSH) && sc_ep_test(sc, SE_FL_MAY_SPLICE)) { |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1320 | /* don't break splicing by reading, but still call rcv_buf() |
| 1321 | * to pass the flag. |
| 1322 | */ |
| 1323 | goto done_recv; |
| 1324 | } |
| 1325 | |
| 1326 | /* now we'll need a input buffer for the stream */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1327 | if (!sc_alloc_ibuf(sc, &(__sc_strm(sc)->buffer_wait))) |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1328 | goto end_recv; |
| 1329 | |
| 1330 | /* For an HTX stream, if the buffer is stuck (no output data with some |
| 1331 | * input data) and if the HTX message is fragmented or if its free space |
| 1332 | * wraps, we force an HTX deframentation. It is a way to have a |
| 1333 | * contiguous free space nad to let the mux to copy as much data as |
| 1334 | * possible. |
| 1335 | * |
| 1336 | * NOTE: A possible optim may be to let the mux decides if defrag is |
| 1337 | * required or not, depending on amount of data to be xferred. |
| 1338 | */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1339 | if (IS_HTX_STRM(__sc_strm(sc)) && !co_data(ic)) { |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1340 | struct htx *htx = htxbuf(&ic->buf); |
| 1341 | |
| 1342 | if (htx_is_not_empty(htx) && ((htx->flags & HTX_FL_FRAGMENTED) || htx_space_wraps(htx))) |
| 1343 | htx_defrag(htx, NULL, 0); |
| 1344 | } |
| 1345 | |
| 1346 | /* Instruct the mux it must subscribed for read events */ |
Christopher Faulet | d24b51a | 2023-11-13 19:16:09 +0100 | [diff] [blame] | 1347 | if (!conn_is_back(conn) && /* for frontend conns only */ |
| 1348 | (sc_opposite(sc)->state != SC_ST_INI) && /* before backend connection setup */ |
| 1349 | (__sc_strm(sc)->be->options & PR_O_ABRT_CLOSE)) /* if abortonclose option is set for the current backend */ |
| 1350 | flags |= CO_RFL_KEEP_RECV; |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1351 | |
| 1352 | /* Important note : if we're called with POLL_IN|POLL_HUP, it means the read polling |
| 1353 | * was enabled, which implies that the recv buffer was not full. So we have a guarantee |
| 1354 | * that if such an event is not handled above in splice, it will be handled here by |
| 1355 | * recv(). |
| 1356 | */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1357 | while (sc_ep_test(sc, SE_FL_RCV_MORE) || |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1358 | (!(conn->flags & CO_FL_HANDSHAKE) && |
Christopher Faulet | ca5309a | 2023-04-17 16:17:32 +0200 | [diff] [blame] | 1359 | (!sc_ep_test(sc, SE_FL_ERROR | SE_FL_EOS)) && !(sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)))) { |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1360 | int cur_flags = flags; |
| 1361 | |
| 1362 | /* Compute transient CO_RFL_* flags */ |
| 1363 | if (co_data(ic)) { |
| 1364 | cur_flags |= (CO_RFL_BUF_WET | CO_RFL_BUF_NOT_STUCK); |
| 1365 | } |
| 1366 | |
| 1367 | /* <max> may be null. This is the mux responsibility to set |
Willy Tarreau | e68bc61 | 2022-05-27 11:23:05 +0200 | [diff] [blame] | 1368 | * SE_FL_RCV_MORE on the SC if more space is needed. |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1369 | */ |
| 1370 | max = channel_recv_max(ic); |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1371 | ret = conn->mux->rcv_buf(sc, &ic->buf, max, cur_flags); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1372 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1373 | if (sc_ep_test(sc, SE_FL_WANT_ROOM)) { |
Willy Tarreau | b605c42 | 2022-05-17 17:04:55 +0200 | [diff] [blame] | 1374 | /* SE_FL_WANT_ROOM must not be reported if the channel's |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1375 | * buffer is empty. |
| 1376 | */ |
| 1377 | BUG_ON(c_empty(ic)); |
| 1378 | |
Christopher Faulet | 7b3d38a | 2023-05-05 11:28:45 +0200 | [diff] [blame] | 1379 | sc_need_room(sc, channel_recv_max(ic) + 1); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1380 | /* Add READ_PARTIAL because some data are pending but |
| 1381 | * cannot be xferred to the channel |
| 1382 | */ |
Christopher Faulet | 285f761 | 2022-12-12 08:28:55 +0100 | [diff] [blame] | 1383 | ic->flags |= CF_READ_EVENT; |
Christopher Faulet | 2bb2ee2 | 2023-11-07 07:45:43 +0100 | [diff] [blame] | 1384 | sc_ep_report_read_activity(sc); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | if (ret <= 0) { |
| 1388 | /* if we refrained from reading because we asked for a |
| 1389 | * flush to satisfy rcv_pipe(), we must not subscribe |
| 1390 | * and instead report that there's not enough room |
| 1391 | * here to proceed. |
| 1392 | */ |
| 1393 | if (flags & CO_RFL_BUF_FLUSH) |
Christopher Faulet | 7b3d38a | 2023-05-05 11:28:45 +0200 | [diff] [blame] | 1394 | sc_need_room(sc, -1); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1395 | break; |
| 1396 | } |
| 1397 | |
| 1398 | cur_read += ret; |
| 1399 | |
| 1400 | /* if we're allowed to directly forward data, we must update ->o */ |
Christopher Faulet | 64350bb | 2023-04-13 16:37:37 +0200 | [diff] [blame] | 1401 | if (ic->to_forward && !(sc_opposite(sc)->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED))) { |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1402 | unsigned long fwd = ret; |
| 1403 | if (ic->to_forward != CHN_INFINITE_FORWARD) { |
| 1404 | if (fwd > ic->to_forward) |
| 1405 | fwd = ic->to_forward; |
| 1406 | ic->to_forward -= fwd; |
| 1407 | } |
| 1408 | c_adv(ic, fwd); |
| 1409 | } |
| 1410 | |
Christopher Faulet | 285f761 | 2022-12-12 08:28:55 +0100 | [diff] [blame] | 1411 | ic->flags |= CF_READ_EVENT; |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1412 | ic->total += ret; |
| 1413 | |
| 1414 | /* End-of-input reached, we can leave. In this case, it is |
Willy Tarreau | e68bc61 | 2022-05-27 11:23:05 +0200 | [diff] [blame] | 1415 | * important to break the loop to not block the SC because of |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1416 | * the channel's policies.This way, we are still able to receive |
| 1417 | * shutdowns. |
| 1418 | */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1419 | if (sc_ep_test(sc, SE_FL_EOI)) |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1420 | break; |
| 1421 | |
Christopher Faulet | 9a790f6 | 2023-03-16 14:40:03 +0100 | [diff] [blame] | 1422 | if ((sc->flags & SC_FL_RCV_ONCE) || --read_poll <= 0) { |
| 1423 | /* we don't expect to read more data */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1424 | sc_wont_read(sc); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1425 | break; |
| 1426 | } |
| 1427 | |
| 1428 | /* if too many bytes were missing from last read, it means that |
| 1429 | * it's pointless trying to read again because the system does |
| 1430 | * not have them in buffers. |
| 1431 | */ |
| 1432 | if (ret < max) { |
| 1433 | /* if a streamer has read few data, it may be because we |
| 1434 | * have exhausted system buffers. It's not worth trying |
| 1435 | * again. |
| 1436 | */ |
| 1437 | if (ic->flags & CF_STREAMER) { |
| 1438 | /* we're stopped by the channel's policy */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1439 | sc_wont_read(sc); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1440 | break; |
| 1441 | } |
| 1442 | |
| 1443 | /* if we read a large block smaller than what we requested, |
| 1444 | * it's almost certain we'll never get anything more. |
| 1445 | */ |
| 1446 | if (ret >= global.tune.recv_enough) { |
| 1447 | /* we're stopped by the channel's policy */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1448 | sc_wont_read(sc); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1449 | break; |
| 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | /* if we are waiting for more space, don't try to read more data |
| 1454 | * right now. |
| 1455 | */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1456 | if (sc->flags & (SC_FL_WONT_READ|SC_FL_NEED_BUFF|SC_FL_NEED_ROOM)) |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1457 | break; |
| 1458 | } /* while !flags */ |
| 1459 | |
| 1460 | done_recv: |
| 1461 | if (cur_read) { |
| 1462 | if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) && |
| 1463 | (cur_read <= ic->buf.size / 2)) { |
| 1464 | ic->xfer_large = 0; |
| 1465 | ic->xfer_small++; |
| 1466 | if (ic->xfer_small >= 3) { |
| 1467 | /* we have read less than half of the buffer in |
| 1468 | * one pass, and this happened at least 3 times. |
| 1469 | * This is definitely not a streamer. |
| 1470 | */ |
| 1471 | ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST); |
| 1472 | } |
| 1473 | else if (ic->xfer_small >= 2) { |
| 1474 | /* if the buffer has been at least half full twice, |
| 1475 | * we receive faster than we send, so at least it |
| 1476 | * is not a "fast streamer". |
| 1477 | */ |
| 1478 | ic->flags &= ~CF_STREAMER_FAST; |
| 1479 | } |
| 1480 | } |
Christopher Faulet | 0b25b6a | 2023-11-17 11:23:11 +0100 | [diff] [blame] | 1481 | else if (!(ic->flags & CF_STREAMER_FAST) && (cur_read >= channel_data_limit(ic))) { |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1482 | /* we read a full buffer at once */ |
| 1483 | ic->xfer_small = 0; |
| 1484 | ic->xfer_large++; |
| 1485 | if (ic->xfer_large >= 3) { |
| 1486 | /* we call this buffer a fast streamer if it manages |
| 1487 | * to be filled in one call 3 consecutive times. |
| 1488 | */ |
| 1489 | ic->flags |= (CF_STREAMER | CF_STREAMER_FAST); |
| 1490 | } |
| 1491 | } |
| 1492 | else { |
| 1493 | ic->xfer_small = 0; |
| 1494 | ic->xfer_large = 0; |
| 1495 | } |
| 1496 | ic->last_read = now_ms; |
Christopher Faulet | 4c13568 | 2023-02-16 11:09:31 +0100 | [diff] [blame] | 1497 | sc_ep_report_read_activity(sc); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | end_recv: |
| 1501 | ret = (cur_read != 0); |
| 1502 | |
| 1503 | /* Report EOI on the channel if it was reached from the mux point of |
| 1504 | * view. */ |
Christopher Faulet | 904763f | 2023-03-22 14:53:11 +0100 | [diff] [blame] | 1505 | if (sc_ep_test(sc, SE_FL_EOI) && !(sc->flags & SC_FL_EOI)) { |
Christopher Faulet | 4c13568 | 2023-02-16 11:09:31 +0100 | [diff] [blame] | 1506 | sc_ep_report_read_activity(sc); |
Christopher Faulet | 904763f | 2023-03-22 14:53:11 +0100 | [diff] [blame] | 1507 | sc->flags |= SC_FL_EOI; |
| 1508 | ic->flags |= CF_READ_EVENT; |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1509 | ret = 1; |
| 1510 | } |
| 1511 | |
Christopher Faulet | b208d8c | 2023-03-21 11:25:21 +0100 | [diff] [blame] | 1512 | if (sc_ep_test(sc, SE_FL_EOS)) { |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1513 | /* we received a shutdown */ |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1514 | if (ic->flags & CF_AUTO_CLOSE) |
Christopher Faulet | df7cd71 | 2023-04-13 15:56:26 +0200 | [diff] [blame] | 1515 | sc_schedule_shutdown(sc_opposite(sc)); |
Christopher Faulet | 1aec6c9 | 2023-04-17 17:29:29 +0200 | [diff] [blame] | 1516 | sc_conn_eos(sc); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1517 | ret = 1; |
| 1518 | } |
Christopher Faulet | b208d8c | 2023-03-21 11:25:21 +0100 | [diff] [blame] | 1519 | |
Christopher Faulet | a1d14a7 | 2023-04-14 10:42:08 +0200 | [diff] [blame] | 1520 | if (sc_ep_test(sc, SE_FL_ERROR)) { |
| 1521 | sc->flags |= SC_FL_ERROR; |
Christopher Faulet | b208d8c | 2023-03-21 11:25:21 +0100 | [diff] [blame] | 1522 | ret = 1; |
Christopher Faulet | a1d14a7 | 2023-04-14 10:42:08 +0200 | [diff] [blame] | 1523 | } |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1524 | else if (!(sc->flags & (SC_FL_WONT_READ|SC_FL_NEED_BUFF|SC_FL_NEED_ROOM)) && |
Christopher Faulet | ca5309a | 2023-04-17 16:17:32 +0200 | [diff] [blame] | 1525 | !(sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))) { |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1526 | /* Subscribe to receive events if we're blocking on I/O */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1527 | conn->mux->subscribe(sc, SUB_RETRY_RECV, &sc->wait_event); |
| 1528 | se_have_no_more_data(sc->sedesc); |
Christopher Faulet | b208d8c | 2023-03-21 11:25:21 +0100 | [diff] [blame] | 1529 | } |
| 1530 | else { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1531 | se_have_more_data(sc->sedesc); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1532 | ret = 1; |
| 1533 | } |
Christopher Faulet | 8019f78 | 2023-03-23 17:30:29 +0100 | [diff] [blame] | 1534 | |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1535 | return ret; |
| 1536 | } |
| 1537 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 1538 | /* This tries to perform a synchronous receive on the stream connector to |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1539 | * try to collect last arrived data. In practice it's only implemented on |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 1540 | * stconns. Returns 0 if nothing was done, non-zero if new data or a |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1541 | * shutdown were collected. This may result on some delayed receive calls |
| 1542 | * to be programmed and performed later, though it doesn't provide any |
| 1543 | * such guarantee. |
| 1544 | */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1545 | int sc_conn_sync_recv(struct stconn *sc) |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1546 | { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1547 | if (!sc_state_in(sc->state, SC_SB_RDY|SC_SB_EST)) |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1548 | return 0; |
| 1549 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1550 | if (!sc_mux_ops(sc)) |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 1551 | return 0; // only stconns are supported |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1552 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1553 | if (sc->wait_event.events & SUB_RETRY_RECV) |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1554 | return 0; // already subscribed |
| 1555 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1556 | if (!sc_is_recv_allowed(sc)) |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1557 | return 0; // already failed |
| 1558 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1559 | return sc_conn_recv(sc); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | /* |
| 1563 | * This function is called to send buffer data to a stream socket. |
| 1564 | * It calls the mux layer's snd_buf function. It relies on the |
| 1565 | * caller to commit polling changes. The caller should check conn->flags |
| 1566 | * for errors. |
| 1567 | */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1568 | static int sc_conn_send(struct stconn *sc) |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1569 | { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1570 | struct connection *conn = __sc_conn(sc); |
Christopher Faulet | 904763f | 2023-03-22 14:53:11 +0100 | [diff] [blame] | 1571 | struct stconn *sco = sc_opposite(sc); |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1572 | struct stream *s = __sc_strm(sc); |
| 1573 | struct channel *oc = sc_oc(sc); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1574 | int ret; |
| 1575 | int did_send = 0; |
| 1576 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1577 | if (sc_ep_test(sc, SE_FL_ERROR | SE_FL_ERR_PENDING) || sc_is_conn_error(sc)) { |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1578 | /* We're probably there because the tasklet was woken up, |
| 1579 | * but process_stream() ran before, detected there were an |
Willy Tarreau | e68bc61 | 2022-05-27 11:23:05 +0200 | [diff] [blame] | 1580 | * error and put the SC back to SC_ST_TAR. There's still |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1581 | * CO_FL_ERROR on the connection but we don't want to add |
Willy Tarreau | b605c42 | 2022-05-17 17:04:55 +0200 | [diff] [blame] | 1582 | * SE_FL_ERROR back, so give up |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1583 | */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1584 | if (sc->state < SC_ST_CON) |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1585 | return 0; |
Christopher Faulet | 56a2b60 | 2023-04-14 09:42:59 +0200 | [diff] [blame] | 1586 | BUG_ON(sc_ep_test(sc, SE_FL_EOS|SE_FL_ERROR|SE_FL_ERR_PENDING) == (SE_FL_EOS|SE_FL_ERR_PENDING)); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1587 | return 1; |
| 1588 | } |
| 1589 | |
| 1590 | /* We're already waiting to be able to send, give up */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1591 | if (sc->wait_event.events & SUB_RETRY_SEND) |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1592 | return 0; |
| 1593 | |
| 1594 | /* we might have been called just after an asynchronous shutw */ |
Christopher Faulet | 208c712 | 2023-04-13 16:16:15 +0200 | [diff] [blame] | 1595 | if (sc->flags & SC_FL_SHUT_DONE) |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1596 | return 1; |
| 1597 | |
| 1598 | /* we must wait because the mux is not installed yet */ |
| 1599 | if (!conn->mux) |
| 1600 | return 0; |
| 1601 | |
| 1602 | if (oc->pipe && conn->xprt->snd_pipe && conn->mux->snd_pipe) { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1603 | ret = conn->mux->snd_pipe(sc, oc->pipe); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1604 | if (ret > 0) |
| 1605 | did_send = 1; |
| 1606 | |
| 1607 | if (!oc->pipe->data) { |
| 1608 | put_pipe(oc->pipe); |
| 1609 | oc->pipe = NULL; |
| 1610 | } |
| 1611 | |
| 1612 | if (oc->pipe) |
| 1613 | goto end; |
| 1614 | } |
| 1615 | |
| 1616 | /* At this point, the pipe is empty, but we may still have data pending |
| 1617 | * in the normal buffer. |
| 1618 | */ |
| 1619 | if (co_data(oc)) { |
| 1620 | /* when we're here, we already know that there is no spliced |
| 1621 | * data left, and that there are sendable buffered data. |
| 1622 | */ |
| 1623 | |
| 1624 | /* check if we want to inform the kernel that we're interested in |
| 1625 | * sending more data after this call. We want this if : |
| 1626 | * - we're about to close after this last send and want to merge |
| 1627 | * the ongoing FIN with the last segment. |
| 1628 | * - we know we can't send everything at once and must get back |
| 1629 | * here because of unaligned data |
| 1630 | * - there is still a finite amount of data to forward |
| 1631 | * The test is arranged so that the most common case does only 2 |
| 1632 | * tests. |
| 1633 | */ |
| 1634 | unsigned int send_flag = 0; |
| 1635 | |
Christopher Faulet | 68ef218 | 2023-03-17 15:38:18 +0100 | [diff] [blame] | 1636 | if ((!(sc->flags & (SC_FL_SND_ASAP|SC_FL_SND_NEVERWAIT)) && |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1637 | ((oc->to_forward && oc->to_forward != CHN_INFINITE_FORWARD) || |
Christopher Faulet | 84d3ef9 | 2023-03-17 15:45:58 +0100 | [diff] [blame] | 1638 | (sc->flags & SC_FL_SND_EXP_MORE) || |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1639 | (IS_HTX_STRM(s) && |
Christopher Faulet | ca5309a | 2023-04-17 16:17:32 +0200 | [diff] [blame] | 1640 | (!(sco->flags & (SC_FL_EOI|SC_FL_EOS|SC_FL_ABRT_DONE)) && htx_expect_more(htxbuf(&oc->buf)))))) || |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1641 | ((oc->flags & CF_ISRESP) && |
Christopher Faulet | 87633c3 | 2023-04-03 18:32:50 +0200 | [diff] [blame] | 1642 | (oc->flags & CF_AUTO_CLOSE) && |
Christopher Faulet | e38534c | 2023-04-13 15:45:24 +0200 | [diff] [blame] | 1643 | (sc->flags & SC_FL_SHUT_WANTED))) |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1644 | send_flag |= CO_SFL_MSG_MORE; |
| 1645 | |
| 1646 | if (oc->flags & CF_STREAMER) |
| 1647 | send_flag |= CO_SFL_STREAMER; |
| 1648 | |
| 1649 | if (s->txn && s->txn->flags & TX_L7_RETRY && !b_data(&s->txn->l7_buffer)) { |
| 1650 | /* If we want to be able to do L7 retries, copy |
| 1651 | * the data we're about to send, so that we are able |
| 1652 | * to resend them if needed |
| 1653 | */ |
| 1654 | /* Try to allocate a buffer if we had none. |
| 1655 | * If it fails, the next test will just |
| 1656 | * disable the l7 retries by setting |
| 1657 | * l7_conn_retries to 0. |
| 1658 | */ |
| 1659 | if (s->txn->req.msg_state != HTTP_MSG_DONE) |
| 1660 | s->txn->flags &= ~TX_L7_RETRY; |
| 1661 | else { |
| 1662 | if (b_alloc(&s->txn->l7_buffer) == NULL) |
| 1663 | s->txn->flags &= ~TX_L7_RETRY; |
| 1664 | else { |
| 1665 | memcpy(b_orig(&s->txn->l7_buffer), |
| 1666 | b_orig(&oc->buf), |
| 1667 | b_size(&oc->buf)); |
| 1668 | s->txn->l7_buffer.head = co_data(oc); |
| 1669 | b_add(&s->txn->l7_buffer, co_data(oc)); |
| 1670 | } |
| 1671 | |
| 1672 | } |
| 1673 | } |
| 1674 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1675 | ret = conn->mux->snd_buf(sc, &oc->buf, co_data(oc), send_flag); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1676 | if (ret > 0) { |
| 1677 | did_send = 1; |
| 1678 | c_rew(oc, ret); |
| 1679 | c_realign_if_empty(oc); |
| 1680 | |
| 1681 | if (!co_data(oc)) { |
| 1682 | /* Always clear both flags once everything has been sent, they're one-shot */ |
Christopher Faulet | 84d3ef9 | 2023-03-17 15:45:58 +0100 | [diff] [blame] | 1683 | sc->flags &= ~(SC_FL_SND_ASAP|SC_FL_SND_EXP_MORE); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1684 | } |
| 1685 | /* if some data remain in the buffer, it's only because the |
| 1686 | * system buffers are full, we will try next time. |
| 1687 | */ |
Christopher Faulet | 13045f0 | 2022-04-01 14:23:38 +0200 | [diff] [blame] | 1688 | } |
| 1689 | } |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1690 | |
| 1691 | end: |
| 1692 | if (did_send) { |
Christopher Faulet | d898841 | 2022-12-20 18:10:04 +0100 | [diff] [blame] | 1693 | oc->flags |= CF_WRITE_EVENT | CF_WROTE_DATA; |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1694 | if (sc->state == SC_ST_CON) |
| 1695 | sc->state = SC_ST_RDY; |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1696 | } |
| 1697 | |
Christopher Faulet | e7405d4 | 2023-05-05 11:40:30 +0200 | [diff] [blame] | 1698 | if (!sco->room_needed || (did_send && (sco->room_needed < 0 || channel_recv_max(sc_oc(sc)) >= sco->room_needed))) |
| 1699 | sc_have_room(sco); |
| 1700 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1701 | if (sc_ep_test(sc, SE_FL_ERROR | SE_FL_ERR_PENDING)) { |
Christopher Faulet | 2e56a73 | 2023-01-26 16:18:09 +0100 | [diff] [blame] | 1702 | oc->flags |= CF_WRITE_EVENT; |
Christopher Faulet | 56a2b60 | 2023-04-14 09:42:59 +0200 | [diff] [blame] | 1703 | BUG_ON(sc_ep_test(sc, SE_FL_EOS|SE_FL_ERROR|SE_FL_ERR_PENDING) == (SE_FL_EOS|SE_FL_ERR_PENDING)); |
Christopher Faulet | d0c57d3 | 2023-04-18 18:38:32 +0200 | [diff] [blame] | 1704 | if (sc_ep_test(sc, SE_FL_ERROR)) |
| 1705 | sc->flags |= SC_FL_ERROR; |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1706 | return 1; |
| 1707 | } |
| 1708 | |
Christopher Faulet | e65daba | 2023-11-15 17:33:06 +0100 | [diff] [blame] | 1709 | if (channel_is_empty(oc)) { |
Christopher Faulet | 7fb330b | 2023-11-17 15:26:57 +0100 | [diff] [blame] | 1710 | if (did_send) |
| 1711 | sc_ep_report_send_activity(sc); |
Christopher Faulet | e65daba | 2023-11-15 17:33:06 +0100 | [diff] [blame] | 1712 | } |
| 1713 | else { |
Christopher Faulet | 59b240c | 2023-02-27 16:38:12 +0100 | [diff] [blame] | 1714 | /* We couldn't send all of our data, let the mux know we'd like to send more */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1715 | conn->mux->subscribe(sc, SUB_RETRY_SEND, &sc->wait_event); |
Christopher Faulet | e65daba | 2023-11-15 17:33:06 +0100 | [diff] [blame] | 1716 | if (sc_state_in(sc->state, SC_SB_EST|SC_SB_DIS|SC_SB_CLO)) |
| 1717 | sc_ep_report_blocked_send(sc, did_send); |
Christopher Faulet | 59b240c | 2023-02-27 16:38:12 +0100 | [diff] [blame] | 1718 | } |
| 1719 | |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1720 | return did_send; |
| 1721 | } |
| 1722 | |
Christopher Faulet | d898841 | 2022-12-20 18:10:04 +0100 | [diff] [blame] | 1723 | /* perform a synchronous send() for the stream connector. The CF_WRITE_EVENT |
| 1724 | * flag are cleared prior to the attempt, and will possibly be updated in case |
| 1725 | * of success. |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1726 | */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1727 | void sc_conn_sync_send(struct stconn *sc) |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1728 | { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1729 | struct channel *oc = sc_oc(sc); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1730 | |
Christopher Faulet | d898841 | 2022-12-20 18:10:04 +0100 | [diff] [blame] | 1731 | oc->flags &= ~CF_WRITE_EVENT; |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1732 | |
Christopher Faulet | 208c712 | 2023-04-13 16:16:15 +0200 | [diff] [blame] | 1733 | if (sc->flags & SC_FL_SHUT_DONE) |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1734 | return; |
| 1735 | |
| 1736 | if (channel_is_empty(oc)) |
| 1737 | return; |
| 1738 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1739 | if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST)) |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1740 | return; |
| 1741 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1742 | if (!sc_mux_ops(sc)) |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1743 | return; |
| 1744 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1745 | sc_conn_send(sc); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1746 | } |
| 1747 | |
| 1748 | /* Called by I/O handlers after completion.. It propagates |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 1749 | * connection flags to the stream connector, updates the stream (which may or |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1750 | * may not take this opportunity to try to forward data), then update the |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 1751 | * connection's polling based on the channels and stream connector's final |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1752 | * states. The function always returns 0. |
| 1753 | */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1754 | static int sc_conn_process(struct stconn *sc) |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1755 | { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1756 | struct connection *conn = __sc_conn(sc); |
| 1757 | struct channel *ic = sc_ic(sc); |
| 1758 | struct channel *oc = sc_oc(sc); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1759 | |
| 1760 | BUG_ON(!conn); |
| 1761 | |
| 1762 | /* If we have data to send, try it now */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1763 | if (!channel_is_empty(oc) && !(sc->wait_event.events & SUB_RETRY_SEND)) |
| 1764 | sc_conn_send(sc); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1765 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 1766 | /* First step, report to the stream connector what was detected at the |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1767 | * connection layer : errors and connection establishment. |
Christopher Faulet | 88d05a0 | 2023-04-14 12:03:50 +0200 | [diff] [blame] | 1768 | * Only add SC_FL_ERROR if we're connected, or we're attempting to |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1769 | * connect, we may get there because we got woken up, but only run |
| 1770 | * after process_stream() noticed there were an error, and decided |
| 1771 | * to retry to connect, the connection may still have CO_FL_ERROR, |
Christopher Faulet | 88d05a0 | 2023-04-14 12:03:50 +0200 | [diff] [blame] | 1772 | * and we don't want to add SC_FL_ERROR back |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1773 | * |
Willy Tarreau | 462b989 | 2022-05-18 18:06:53 +0200 | [diff] [blame] | 1774 | * Note: This test is only required because sc_conn_process is also the SI |
| 1775 | * wake callback. Otherwise sc_conn_recv()/sc_conn_send() already take |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1776 | * care of it. |
| 1777 | */ |
| 1778 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1779 | if (sc->state >= SC_ST_CON) { |
Christopher Faulet | 88d05a0 | 2023-04-14 12:03:50 +0200 | [diff] [blame] | 1780 | if (sc_is_conn_error(sc)) |
Christopher Faulet | a1d14a7 | 2023-04-14 10:42:08 +0200 | [diff] [blame] | 1781 | sc->flags |= SC_FL_ERROR; |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1782 | } |
| 1783 | |
| 1784 | /* If we had early data, and the handshake ended, then |
| 1785 | * we can remove the flag, and attempt to wake the task up, |
| 1786 | * in the event there's an analyser waiting for the end of |
| 1787 | * the handshake. |
| 1788 | */ |
| 1789 | if (!(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS)) && |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1790 | sc_ep_test(sc, SE_FL_WAIT_FOR_HS)) { |
| 1791 | sc_ep_clr(sc, SE_FL_WAIT_FOR_HS); |
| 1792 | task_wakeup(sc_strm_task(sc), TASK_WOKEN_MSG); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1793 | } |
| 1794 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1795 | if (!sc_state_in(sc->state, SC_SB_EST|SC_SB_DIS|SC_SB_CLO) && |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1796 | (conn->flags & CO_FL_WAIT_XPRT) == 0) { |
Christopher Faulet | ca67992 | 2022-07-20 13:24:04 +0200 | [diff] [blame] | 1797 | if (sc->flags & SC_FL_ISBACK) |
| 1798 | __sc_strm(sc)->conn_exp = TICK_ETERNITY; |
Christopher Faulet | b96f2aa | 2022-12-12 08:11:36 +0100 | [diff] [blame] | 1799 | oc->flags |= CF_WRITE_EVENT; |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1800 | if (sc->state == SC_ST_CON) |
| 1801 | sc->state = SC_ST_RDY; |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1802 | } |
| 1803 | |
| 1804 | /* Report EOS on the channel if it was reached from the mux point of |
| 1805 | * view. |
| 1806 | * |
Willy Tarreau | 462b989 | 2022-05-18 18:06:53 +0200 | [diff] [blame] | 1807 | * Note: This test is only required because sc_conn_process is also the SI |
| 1808 | * wake callback. Otherwise sc_conn_recv()/sc_conn_send() already take |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1809 | * care of it. |
| 1810 | */ |
Christopher Faulet | 1aec6c9 | 2023-04-17 17:29:29 +0200 | [diff] [blame] | 1811 | if (sc_ep_test(sc, SE_FL_EOS) && !(sc->flags & SC_FL_EOS)) { |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1812 | /* we received a shutdown */ |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1813 | if (ic->flags & CF_AUTO_CLOSE) |
Christopher Faulet | df7cd71 | 2023-04-13 15:56:26 +0200 | [diff] [blame] | 1814 | sc_schedule_shutdown(sc_opposite(sc)); |
Christopher Faulet | 1aec6c9 | 2023-04-17 17:29:29 +0200 | [diff] [blame] | 1815 | sc_conn_eos(sc); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1816 | } |
| 1817 | |
| 1818 | /* Report EOI on the channel if it was reached from the mux point of |
| 1819 | * view. |
| 1820 | * |
Willy Tarreau | 462b989 | 2022-05-18 18:06:53 +0200 | [diff] [blame] | 1821 | * Note: This test is only required because sc_conn_process is also the SI |
| 1822 | * wake callback. Otherwise sc_conn_recv()/sc_conn_send() already take |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1823 | * care of it. |
| 1824 | */ |
Christopher Faulet | 904763f | 2023-03-22 14:53:11 +0100 | [diff] [blame] | 1825 | if (sc_ep_test(sc, SE_FL_EOI) && !(sc->flags & SC_FL_EOI)) { |
| 1826 | sc->flags |= SC_FL_EOI; |
| 1827 | ic->flags |= CF_READ_EVENT; |
Christopher Faulet | 2bb2ee2 | 2023-11-07 07:45:43 +0100 | [diff] [blame] | 1828 | sc_ep_report_read_activity(sc); |
Christopher Faulet | 904763f | 2023-03-22 14:53:11 +0100 | [diff] [blame] | 1829 | } |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1830 | |
Christopher Faulet | a1d14a7 | 2023-04-14 10:42:08 +0200 | [diff] [blame] | 1831 | if (sc_ep_test(sc, SE_FL_ERROR)) |
| 1832 | sc->flags |= SC_FL_ERROR; |
| 1833 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 1834 | /* Second step : update the stream connector and channels, try to forward any |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1835 | * pending data, then possibly wake the stream up based on the new |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 1836 | * stream connector status. |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1837 | */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1838 | sc_notify(sc); |
| 1839 | stream_release_buffers(__sc_strm(sc)); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1840 | return 0; |
| 1841 | } |
| 1842 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 1843 | /* This is the ->process() function for any stream connector's wait_event task. |
| 1844 | * It's assigned during the stream connector's initialization, for any type of |
| 1845 | * stream connector. Thus it is always safe to perform a tasklet_wakeup() on a |
Willy Tarreau | e68bc61 | 2022-05-27 11:23:05 +0200 | [diff] [blame] | 1846 | * stream connector, as the presence of the SC is checked there. |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1847 | */ |
Willy Tarreau | 462b989 | 2022-05-18 18:06:53 +0200 | [diff] [blame] | 1848 | struct task *sc_conn_io_cb(struct task *t, void *ctx, unsigned int state) |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1849 | { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1850 | struct stconn *sc = ctx; |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1851 | int ret = 0; |
| 1852 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1853 | if (!sc_conn(sc)) |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1854 | return t; |
| 1855 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1856 | if (!(sc->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(sc_oc(sc))) |
| 1857 | ret = sc_conn_send(sc); |
| 1858 | if (!(sc->wait_event.events & SUB_RETRY_RECV)) |
| 1859 | ret |= sc_conn_recv(sc); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1860 | if (ret != 0) |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1861 | sc_conn_process(sc); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1862 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1863 | stream_release_buffers(__sc_strm(sc)); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1864 | return t; |
| 1865 | } |
| 1866 | |
Christopher Faulet | b36e512 | 2023-04-17 17:32:43 +0200 | [diff] [blame] | 1867 | /* |
| 1868 | * This function propagates an end-of-stream received from an applet. It |
| 1869 | * updates the stream connector. If it is is already shut, the applet is |
| 1870 | * released. Otherwise, we try to forward the shutdown, immediately or ASAP. |
| 1871 | */ |
| 1872 | static void sc_applet_eos(struct stconn *sc) |
| 1873 | { |
| 1874 | struct channel *ic = sc_ic(sc); |
| 1875 | |
| 1876 | BUG_ON(!sc_appctx(sc)); |
| 1877 | |
| 1878 | if (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)) |
| 1879 | return; |
| 1880 | sc->flags |= SC_FL_EOS; |
| 1881 | ic->flags |= CF_READ_EVENT; |
Christopher Faulet | 2bb2ee2 | 2023-11-07 07:45:43 +0100 | [diff] [blame] | 1882 | sc_ep_report_read_activity(sc); |
Christopher Faulet | b36e512 | 2023-04-17 17:32:43 +0200 | [diff] [blame] | 1883 | |
| 1884 | /* Note: on abort, we don't call the applet */ |
| 1885 | |
| 1886 | if (!sc_state_in(sc->state, SC_SB_CON|SC_SB_RDY|SC_SB_EST)) |
| 1887 | return; |
| 1888 | |
| 1889 | if (sc->flags & SC_FL_SHUT_DONE) { |
| 1890 | appctx_shut(__sc_appctx(sc)); |
| 1891 | sc->state = SC_ST_DIS; |
| 1892 | if (sc->flags & SC_FL_ISBACK) |
| 1893 | __sc_strm(sc)->conn_exp = TICK_ETERNITY; |
| 1894 | } |
| 1895 | else if (sc_cond_forward_shut(sc)) |
| 1896 | return sc_app_shut_applet(sc); |
| 1897 | } |
| 1898 | |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1899 | /* Callback to be used by applet handlers upon completion. It updates the stream |
| 1900 | * (which may or may not take this opportunity to try to forward data), then |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 1901 | * may re-enable the applet's based on the channels and stream connector's final |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1902 | * states. |
| 1903 | */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1904 | static int sc_applet_process(struct stconn *sc) |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1905 | { |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1906 | struct channel *ic = sc_ic(sc); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1907 | |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1908 | BUG_ON(!sc_appctx(sc)); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1909 | |
Christopher Faulet | f8fbb6d | 2023-03-21 11:49:21 +0100 | [diff] [blame] | 1910 | /* Report EOI on the channel if it was reached from the applet point of |
| 1911 | * view. */ |
Christopher Faulet | 904763f | 2023-03-22 14:53:11 +0100 | [diff] [blame] | 1912 | if (sc_ep_test(sc, SE_FL_EOI) && !(sc->flags & SC_FL_EOI)) { |
Christopher Faulet | f8fbb6d | 2023-03-21 11:49:21 +0100 | [diff] [blame] | 1913 | sc_ep_report_read_activity(sc); |
Christopher Faulet | 904763f | 2023-03-22 14:53:11 +0100 | [diff] [blame] | 1914 | sc->flags |= SC_FL_EOI; |
| 1915 | ic->flags |= CF_READ_EVENT; |
Christopher Faulet | f8fbb6d | 2023-03-21 11:49:21 +0100 | [diff] [blame] | 1916 | } |
| 1917 | |
Christopher Faulet | a1d14a7 | 2023-04-14 10:42:08 +0200 | [diff] [blame] | 1918 | if (sc_ep_test(sc, SE_FL_ERROR)) |
| 1919 | sc->flags |= SC_FL_ERROR; |
| 1920 | |
Christopher Faulet | 0ffc9d7 | 2023-03-21 14:19:08 +0100 | [diff] [blame] | 1921 | if (sc_ep_test(sc, SE_FL_EOS)) { |
| 1922 | /* we received a shutdown */ |
Christopher Faulet | b36e512 | 2023-04-17 17:32:43 +0200 | [diff] [blame] | 1923 | sc_applet_eos(sc); |
Christopher Faulet | 0ffc9d7 | 2023-03-21 14:19:08 +0100 | [diff] [blame] | 1924 | } |
| 1925 | |
Christopher Faulet | e8bcef5 | 2023-04-14 09:45:41 +0200 | [diff] [blame] | 1926 | BUG_ON(sc_ep_test(sc, SE_FL_HAVE_NO_DATA|SE_FL_EOI) == SE_FL_EOI); |
| 1927 | |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1928 | /* If the applet wants to write and the channel is closed, it's a |
| 1929 | * broken pipe and it must be reported. |
| 1930 | */ |
Christopher Faulet | ca5309a | 2023-04-17 16:17:32 +0200 | [diff] [blame] | 1931 | if (!sc_ep_test(sc, SE_FL_HAVE_NO_DATA) && (sc->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))) |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1932 | sc_ep_set(sc, SE_FL_ERROR); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1933 | |
| 1934 | /* automatically mark the applet having data available if it reported |
| 1935 | * begin blocked by the channel. |
| 1936 | */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1937 | if ((sc->flags & (SC_FL_WONT_READ|SC_FL_NEED_BUFF|SC_FL_NEED_ROOM)) || |
| 1938 | sc_ep_test(sc, SE_FL_APPLET_NEED_CONN)) |
| 1939 | applet_have_more_data(__sc_appctx(sc)); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1940 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 1941 | /* update the stream connector, channels, and possibly wake the stream up */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1942 | sc_notify(sc); |
| 1943 | stream_release_buffers(__sc_strm(sc)); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1944 | |
Willy Tarreau | 19c65a9 | 2022-05-27 08:49:24 +0200 | [diff] [blame] | 1945 | /* sc_notify may have passed through chk_snd and released some blocking |
Willy Tarreau | 15252cd | 2022-05-25 16:36:21 +0200 | [diff] [blame] | 1946 | * flags. Process_stream will consider those flags to wake up the |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1947 | * appctx but in the case the task is not in runqueue we may have to |
| 1948 | * wakeup the appctx immediately. |
| 1949 | */ |
Willy Tarreau | 0adb281 | 2022-05-27 10:02:48 +0200 | [diff] [blame] | 1950 | if (sc_is_recv_allowed(sc) || sc_is_send_allowed(sc)) |
| 1951 | appctx_wakeup(__sc_appctx(sc)); |
Christopher Faulet | 5e29b76 | 2022-04-04 08:58:34 +0200 | [diff] [blame] | 1952 | return 0; |
Christopher Faulet | 13045f0 | 2022-04-01 14:23:38 +0200 | [diff] [blame] | 1953 | } |
Christopher Faulet | b68f77d | 2022-06-16 16:24:16 +0200 | [diff] [blame] | 1954 | |
| 1955 | |
| 1956 | /* Prepares an endpoint upgrade. We don't now at this stage if the upgrade will |
| 1957 | * succeed or not and if the stconn will be reused by the new endpoint. Thus, |
| 1958 | * for now, only pretend the stconn is detached. |
| 1959 | */ |
| 1960 | void sc_conn_prepare_endp_upgrade(struct stconn *sc) |
| 1961 | { |
| 1962 | BUG_ON(!sc_conn(sc) || !sc->app); |
| 1963 | sc_ep_clr(sc, SE_FL_T_MUX); |
| 1964 | sc_ep_set(sc, SE_FL_DETACHED); |
| 1965 | } |
| 1966 | |
Ilya Shipitsin | 3b64a28 | 2022-07-29 22:26:53 +0500 | [diff] [blame] | 1967 | /* Endpoint upgrade failed. Restore the stconn state. */ |
Christopher Faulet | b68f77d | 2022-06-16 16:24:16 +0200 | [diff] [blame] | 1968 | void sc_conn_abort_endp_upgrade(struct stconn *sc) |
| 1969 | { |
| 1970 | sc_ep_set(sc, SE_FL_T_MUX); |
| 1971 | sc_ep_clr(sc, SE_FL_DETACHED); |
| 1972 | } |
| 1973 | |
| 1974 | /* Commit the endpoint upgrade. If stconn is attached, it means the new endpoint |
| 1975 | * use it. So we do nothing. Otherwise, the stconn will be destroy with the |
| 1976 | * overlying stream. So, it means we must commit the detach. |
| 1977 | */ |
| 1978 | void sc_conn_commit_endp_upgrade(struct stconn *sc) |
| 1979 | { |
| 1980 | if (!sc_ep_test(sc, SE_FL_DETACHED)) |
| 1981 | return; |
| 1982 | sc_detach_endp(&sc); |
| 1983 | /* Because it was already set as detached, the sedesc must be preserved */ |
Willy Tarreau | 6a378d1 | 2022-08-11 13:56:42 +0200 | [diff] [blame] | 1984 | BUG_ON(!sc); |
Christopher Faulet | b68f77d | 2022-06-16 16:24:16 +0200 | [diff] [blame] | 1985 | BUG_ON(!sc->sedesc); |
| 1986 | } |