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