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