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