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