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