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