Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Server management functions. |
| 3 | * |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 4 | * Copyright 2000-2008 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 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 <stdlib.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 14 | |
| 15 | #include <common/config.h> |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 16 | #include <common/debug.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 17 | #include <common/memory.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 18 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 19 | #include <types/capture.h> |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 20 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 22 | #include <proto/acl.h> |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 23 | #include <proto/backend.h> |
Willy Tarreau | 7341d94 | 2007-05-13 19:56:02 +0200 | [diff] [blame] | 24 | #include <proto/buffers.h> |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 25 | #include <proto/checks.h> |
Willy Tarreau | 5ca791d | 2009-08-16 19:06:42 +0200 | [diff] [blame] | 26 | #include <proto/dumpstats.h> |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 27 | #include <proto/hdr_idx.h> |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 28 | #include <proto/log.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 29 | #include <proto/session.h> |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 30 | #include <proto/pipe.h> |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 31 | #include <proto/proto_http.h> |
| 32 | #include <proto/proto_tcp.h> |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 33 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 34 | #include <proto/queue.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 35 | #include <proto/server.h> |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 36 | #include <proto/stream_interface.h> |
| 37 | #include <proto/stream_sock.h> |
| 38 | #include <proto/task.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 39 | |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 40 | struct pool_head *pool2_session; |
Willy Tarreau | f54f8bd | 2008-11-23 19:53:55 +0100 | [diff] [blame] | 41 | struct list sessions; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 42 | |
| 43 | /* |
| 44 | * frees the context associated to a session. It must have been removed first. |
| 45 | */ |
| 46 | void session_free(struct session *s) |
| 47 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 48 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 632f5a7 | 2007-07-11 10:42:35 +0200 | [diff] [blame] | 49 | struct proxy *fe = s->fe; |
Willy Tarreau | 62e4f1d | 2008-12-07 20:16:23 +0100 | [diff] [blame] | 50 | struct bref *bref, *back; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 51 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 52 | if (s->pend_pos) |
| 53 | pendconn_free(s->pend_pos); |
Willy Tarreau | 922a806 | 2008-12-04 09:33:58 +0100 | [diff] [blame] | 54 | |
Willy Tarreau | 1e62de6 | 2008-11-11 20:20:02 +0100 | [diff] [blame] | 55 | if (s->srv) { /* there may be requests left pending in queue */ |
| 56 | if (s->flags & SN_CURR_SESS) { |
| 57 | s->flags &= ~SN_CURR_SESS; |
| 58 | s->srv->cur_sess--; |
| 59 | } |
Willy Tarreau | 922a806 | 2008-12-04 09:33:58 +0100 | [diff] [blame] | 60 | if (may_dequeue_tasks(s->srv, s->be)) |
| 61 | process_srv_queue(s->srv); |
Willy Tarreau | 1e62de6 | 2008-11-11 20:20:02 +0100 | [diff] [blame] | 62 | } |
Willy Tarreau | 922a806 | 2008-12-04 09:33:58 +0100 | [diff] [blame] | 63 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 64 | if (unlikely(s->srv_conn)) { |
| 65 | /* the session still has a reserved slot on a server, but |
| 66 | * it should normally be only the same as the one above, |
| 67 | * so this should not happen in fact. |
| 68 | */ |
| 69 | sess_change_server(s, NULL); |
| 70 | } |
| 71 | |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 72 | if (s->req->pipe) |
| 73 | put_pipe(s->req->pipe); |
Willy Tarreau | 259de1b | 2009-01-18 21:56:21 +0100 | [diff] [blame] | 74 | |
Willy Tarreau | 3eba98a | 2009-01-25 13:56:13 +0100 | [diff] [blame] | 75 | if (s->rep->pipe) |
| 76 | put_pipe(s->rep->pipe); |
Willy Tarreau | 259de1b | 2009-01-18 21:56:21 +0100 | [diff] [blame] | 77 | |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 78 | pool_free2(pool2_buffer, s->req); |
| 79 | pool_free2(pool2_buffer, s->rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 80 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 81 | if (s->sessid) |
| 82 | pool_free2(apools.sessid, s->sessid); |
| 83 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 84 | if (fe) { |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 85 | pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v); |
Willy Tarreau | 41dff82 | 2007-01-01 23:32:30 +0100 | [diff] [blame] | 86 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 87 | if (txn->rsp.cap != NULL) { |
| 88 | struct cap_hdr *h; |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 89 | for (h = fe->rsp_cap; h; h = h->next) |
| 90 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 91 | pool_free2(fe->rsp_cap_pool, txn->rsp.cap); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 92 | } |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 93 | if (txn->req.cap != NULL) { |
| 94 | struct cap_hdr *h; |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 95 | for (h = fe->req_cap; h; h = h->next) |
| 96 | pool_free2(h->pool, txn->req.cap[h->index]); |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 97 | pool_free2(fe->req_cap_pool, txn->req.cap); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 98 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 99 | } |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 100 | |
| 101 | http_end_txn(s); |
Willy Tarreau | 62e4f1d | 2008-12-07 20:16:23 +0100 | [diff] [blame] | 102 | |
| 103 | list_for_each_entry_safe(bref, back, &s->back_refs, users) { |
Willy Tarreau | fd3828e | 2009-02-22 15:17:24 +0100 | [diff] [blame] | 104 | /* we have to unlink all watchers. We must not relink them if |
| 105 | * this session was the last one in the list. |
| 106 | */ |
Willy Tarreau | 62e4f1d | 2008-12-07 20:16:23 +0100 | [diff] [blame] | 107 | LIST_DEL(&bref->users); |
Willy Tarreau | fd3828e | 2009-02-22 15:17:24 +0100 | [diff] [blame] | 108 | LIST_INIT(&bref->users); |
| 109 | if (s->list.n != &sessions) |
| 110 | LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users); |
Willy Tarreau | 62e4f1d | 2008-12-07 20:16:23 +0100 | [diff] [blame] | 111 | bref->ref = s->list.n; |
| 112 | } |
Willy Tarreau | f54f8bd | 2008-11-23 19:53:55 +0100 | [diff] [blame] | 113 | LIST_DEL(&s->list); |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 114 | pool_free2(pool2_session, s); |
Willy Tarreau | 632f5a7 | 2007-07-11 10:42:35 +0200 | [diff] [blame] | 115 | |
| 116 | /* We may want to free the maximum amount of pools if the proxy is stopping */ |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 117 | if (fe && unlikely(fe->state == PR_STSTOPPED)) { |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 118 | pool_flush2(pool2_buffer); |
| 119 | pool_flush2(fe->hdr_idx_pool); |
| 120 | pool_flush2(pool2_requri); |
| 121 | pool_flush2(pool2_capture); |
| 122 | pool_flush2(pool2_session); |
| 123 | pool_flush2(fe->req_cap_pool); |
| 124 | pool_flush2(fe->rsp_cap_pool); |
Willy Tarreau | 632f5a7 | 2007-07-11 10:42:35 +0200 | [diff] [blame] | 125 | } |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | |
| 129 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 130 | int init_session() |
| 131 | { |
Willy Tarreau | f54f8bd | 2008-11-23 19:53:55 +0100 | [diff] [blame] | 132 | LIST_INIT(&sessions); |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 133 | pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED); |
| 134 | return pool2_session != NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 135 | } |
| 136 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 137 | void session_process_counters(struct session *s) |
| 138 | { |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 139 | unsigned long long bytes; |
| 140 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 141 | if (s->req) { |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 142 | bytes = s->req->total - s->logs.bytes_in; |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 143 | s->logs.bytes_in = s->req->total; |
| 144 | if (bytes) { |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 145 | s->fe->counters.bytes_in += bytes; |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 146 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 147 | if (s->be != s->fe) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 148 | s->be->counters.bytes_in += bytes; |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 149 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 150 | if (s->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 151 | s->srv->counters.bytes_in += bytes; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 152 | |
| 153 | if (s->listener->counters) |
| 154 | s->listener->counters->bytes_in += bytes; |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 155 | } |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 156 | } |
| 157 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 158 | if (s->rep) { |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 159 | bytes = s->rep->total - s->logs.bytes_out; |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 160 | s->logs.bytes_out = s->rep->total; |
| 161 | if (bytes) { |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 162 | s->fe->counters.bytes_out += bytes; |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 163 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 164 | if (s->be != s->fe) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 165 | s->be->counters.bytes_out += bytes; |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 166 | |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 167 | if (s->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 168 | s->srv->counters.bytes_out += bytes; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 169 | |
| 170 | if (s->listener->counters) |
| 171 | s->listener->counters->bytes_out += bytes; |
Willy Tarreau | 30e7101 | 2007-11-26 20:15:35 +0100 | [diff] [blame] | 172 | } |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 173 | } |
| 174 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 175 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 176 | /* This function is called with (si->state == SI_ST_CON) meaning that a |
| 177 | * connection was attempted and that the file descriptor is already allocated. |
| 178 | * We must check for establishment, error and abort. Possible output states |
| 179 | * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and |
| 180 | * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER, |
| 181 | * otherwise 1. |
| 182 | */ |
| 183 | int sess_update_st_con_tcp(struct session *s, struct stream_interface *si) |
| 184 | { |
| 185 | struct buffer *req = si->ob; |
| 186 | struct buffer *rep = si->ib; |
| 187 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 188 | /* If we got an error, or if nothing happened and the connection timed |
| 189 | * out, we must give up. The CER state handler will take care of retry |
| 190 | * attempts and error reports. |
| 191 | */ |
| 192 | if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) { |
Willy Tarreau | 127334e | 2009-03-28 10:47:26 +0100 | [diff] [blame] | 193 | si->exp = TICK_ETERNITY; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 194 | si->state = SI_ST_CER; |
Willy Tarreau | dc340a9 | 2009-06-28 23:10:19 +0200 | [diff] [blame] | 195 | si->flags &= ~SI_FL_CAP_SPLICE; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 196 | fd_delete(si->fd); |
| 197 | |
| 198 | if (si->err_type) |
| 199 | return 0; |
| 200 | |
| 201 | si->err_loc = s->srv; |
| 202 | if (si->flags & SI_FL_ERR) |
| 203 | si->err_type = SI_ET_CONN_ERR; |
| 204 | else |
| 205 | si->err_type = SI_ET_CONN_TO; |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | /* OK, maybe we want to abort */ |
Willy Tarreau | 418fd47 | 2009-09-06 21:37:23 +0200 | [diff] [blame] | 210 | if (unlikely((rep->flags & BF_SHUTW) || |
| 211 | ((req->flags & BF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */ |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 212 | (((req->flags & (BF_OUT_EMPTY|BF_WRITE_ACTIVITY)) == BF_OUT_EMPTY) || |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 213 | s->be->options & PR_O_ABRT_CLOSE)))) { |
| 214 | /* give up */ |
| 215 | si->shutw(si); |
| 216 | si->err_type |= SI_ET_CONN_ABRT; |
| 217 | si->err_loc = s->srv; |
Willy Tarreau | dc340a9 | 2009-06-28 23:10:19 +0200 | [diff] [blame] | 218 | si->flags &= ~SI_FL_CAP_SPLICE; |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 219 | if (s->srv_error) |
| 220 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 221 | return 1; |
| 222 | } |
| 223 | |
| 224 | /* we need to wait a bit more if there was no activity either */ |
| 225 | if (!(req->flags & BF_WRITE_ACTIVITY)) |
| 226 | return 1; |
| 227 | |
| 228 | /* OK, this means that a connection succeeded. The caller will be |
| 229 | * responsible for handling the transition from CON to EST. |
| 230 | */ |
| 231 | s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 127334e | 2009-03-28 10:47:26 +0100 | [diff] [blame] | 232 | si->exp = TICK_ETERNITY; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 233 | si->state = SI_ST_EST; |
| 234 | si->err_type = SI_ET_NONE; |
| 235 | si->err_loc = NULL; |
| 236 | return 1; |
| 237 | } |
| 238 | |
| 239 | /* This function is called with (si->state == SI_ST_CER) meaning that a |
| 240 | * previous connection attempt has failed and that the file descriptor |
| 241 | * has already been released. Possible causes include asynchronous error |
| 242 | * notification and time out. Possible output states are SI_ST_CLO when |
| 243 | * retries are exhausted, SI_ST_TAR when a delay is wanted before a new |
| 244 | * connection attempt, SI_ST_ASS when it's wise to retry on the same server, |
| 245 | * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are |
| 246 | * marked as in error state. It returns 0. |
| 247 | */ |
| 248 | int sess_update_st_cer(struct session *s, struct stream_interface *si) |
| 249 | { |
| 250 | /* we probably have to release last session from the server */ |
| 251 | if (s->srv) { |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 252 | health_adjust(s->srv, HANA_STATUS_L4_ERR); |
| 253 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 254 | if (s->flags & SN_CURR_SESS) { |
| 255 | s->flags &= ~SN_CURR_SESS; |
| 256 | s->srv->cur_sess--; |
| 257 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /* ensure that we have enough retries left */ |
| 261 | s->conn_retries--; |
| 262 | if (s->conn_retries < 0) { |
| 263 | if (!si->err_type) { |
| 264 | si->err_type = SI_ET_CONN_ERR; |
| 265 | si->err_loc = s->srv; |
| 266 | } |
| 267 | |
| 268 | if (s->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 269 | s->srv->counters.failed_conns++; |
| 270 | s->be->counters.failed_conns++; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 271 | if (may_dequeue_tasks(s->srv, s->be)) |
| 272 | process_srv_queue(s->srv); |
| 273 | |
| 274 | /* shutw is enough so stop a connecting socket */ |
| 275 | si->shutw(si); |
| 276 | si->ob->flags |= BF_WRITE_ERROR; |
| 277 | si->ib->flags |= BF_READ_ERROR; |
| 278 | |
| 279 | si->state = SI_ST_CLO; |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 280 | if (s->srv_error) |
| 281 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | /* If the "redispatch" option is set on the backend, we are allowed to |
| 286 | * retry on another server for the last retry. In order to achieve this, |
| 287 | * we must mark the session unassigned, and eventually clear the DIRECT |
| 288 | * bit to ignore any persistence cookie. We won't count a retry nor a |
| 289 | * redispatch yet, because this will depend on what server is selected. |
| 290 | */ |
| 291 | if (s->srv && s->conn_retries == 0 && s->be->options & PR_O_REDISP) { |
| 292 | if (may_dequeue_tasks(s->srv, s->be)) |
| 293 | process_srv_queue(s->srv); |
| 294 | |
| 295 | s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 296 | s->prev_srv = s->srv; |
| 297 | si->state = SI_ST_REQ; |
| 298 | } else { |
| 299 | if (s->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 300 | s->srv->counters.retries++; |
| 301 | s->be->counters.retries++; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 302 | si->state = SI_ST_ASS; |
| 303 | } |
| 304 | |
| 305 | if (si->flags & SI_FL_ERR) { |
| 306 | /* The error was an asynchronous connection error, and we will |
| 307 | * likely have to retry connecting to the same server, most |
| 308 | * likely leading to the same result. To avoid this, we wait |
| 309 | * one second before retrying. |
| 310 | */ |
| 311 | |
| 312 | if (!si->err_type) |
| 313 | si->err_type = SI_ET_CONN_ERR; |
| 314 | |
| 315 | si->state = SI_ST_TAR; |
| 316 | si->exp = tick_add(now_ms, MS_TO_TICKS(1000)); |
| 317 | return 0; |
| 318 | } |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | /* |
| 323 | * This function handles the transition between the SI_ST_CON state and the |
| 324 | * SI_ST_EST state. It must only be called after switching from SI_ST_CON to |
| 325 | * SI_ST_EST. |
| 326 | */ |
| 327 | void sess_establish(struct session *s, struct stream_interface *si) |
| 328 | { |
| 329 | struct buffer *req = si->ob; |
| 330 | struct buffer *rep = si->ib; |
| 331 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 332 | if (s->srv) |
| 333 | health_adjust(s->srv, HANA_STATUS_L4_OK); |
| 334 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 335 | if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 336 | /* if the user wants to log as soon as possible, without counting |
| 337 | * bytes from the server, then this is the right moment. */ |
| 338 | if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) { |
| 339 | s->logs.t_close = s->logs.t_connect; /* to get a valid end date */ |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 340 | s->do_log(s); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 341 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 342 | } |
| 343 | else { |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 344 | s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE; |
| 345 | /* reset hdr_idx which was already initialized by the request. |
| 346 | * right now, the http parser does it. |
| 347 | * hdr_idx_init(&s->txn.hdr_idx); |
| 348 | */ |
| 349 | } |
| 350 | |
Willy Tarreau | 4e5b828 | 2009-08-16 22:57:50 +0200 | [diff] [blame] | 351 | rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 352 | rep->flags |= BF_READ_ATTACHED; /* producer is now attached */ |
| 353 | req->wex = TICK_ETERNITY; |
| 354 | } |
| 355 | |
| 356 | /* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR. |
| 357 | * Other input states are simply ignored. |
| 358 | * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON. |
| 359 | * Flags must have previously been updated for timeouts and other conditions. |
| 360 | */ |
| 361 | void sess_update_stream_int(struct session *s, struct stream_interface *si) |
| 362 | { |
| 363 | DPRINTF(stderr,"[%u] %s: sess=%p rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rql=%d rpl=%d cs=%d ss=%d\n", |
| 364 | now_ms, __FUNCTION__, |
| 365 | s, |
| 366 | s->req, s->rep, |
| 367 | s->req->rex, s->rep->wex, |
| 368 | s->req->flags, s->rep->flags, |
| 369 | s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state); |
| 370 | |
| 371 | if (si->state == SI_ST_ASS) { |
| 372 | /* Server assigned to connection request, we have to try to connect now */ |
| 373 | int conn_err; |
| 374 | |
| 375 | conn_err = connect_server(s); |
| 376 | if (conn_err == SN_ERR_NONE) { |
| 377 | /* state = SI_ST_CON now */ |
Willy Tarreau | 8f6457c | 2008-12-01 00:08:28 +0100 | [diff] [blame] | 378 | if (s->srv) |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 379 | srv_inc_sess_ctr(s->srv); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 380 | return; |
| 381 | } |
| 382 | |
| 383 | /* We have received a synchronous error. We might have to |
| 384 | * abort, retry immediately or redispatch. |
| 385 | */ |
| 386 | if (conn_err == SN_ERR_INTERNAL) { |
| 387 | if (!si->err_type) { |
| 388 | si->err_type = SI_ET_CONN_OTHER; |
| 389 | si->err_loc = s->srv; |
| 390 | } |
| 391 | |
| 392 | if (s->srv) |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 393 | srv_inc_sess_ctr(s->srv); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 394 | if (s->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 395 | s->srv->counters.failed_conns++; |
| 396 | s->be->counters.failed_conns++; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 397 | |
| 398 | /* release other sessions waiting for this server */ |
| 399 | if (may_dequeue_tasks(s->srv, s->be)) |
| 400 | process_srv_queue(s->srv); |
| 401 | |
| 402 | /* Failed and not retryable. */ |
| 403 | si->shutr(si); |
| 404 | si->shutw(si); |
| 405 | si->ob->flags |= BF_WRITE_ERROR; |
| 406 | |
| 407 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 408 | |
| 409 | /* no session was ever accounted for this server */ |
| 410 | si->state = SI_ST_CLO; |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 411 | if (s->srv_error) |
| 412 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 413 | return; |
| 414 | } |
| 415 | |
| 416 | /* We are facing a retryable error, but we don't want to run a |
| 417 | * turn-around now, as the problem is likely a source port |
| 418 | * allocation problem, so we want to retry now. |
| 419 | */ |
| 420 | si->state = SI_ST_CER; |
| 421 | si->flags &= ~SI_FL_ERR; |
| 422 | sess_update_st_cer(s, si); |
| 423 | /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */ |
| 424 | return; |
| 425 | } |
| 426 | else if (si->state == SI_ST_QUE) { |
| 427 | /* connection request was queued, check for any update */ |
| 428 | if (!s->pend_pos) { |
| 429 | /* The connection is not in the queue anymore. Either |
| 430 | * we have a server connection slot available and we |
| 431 | * go directly to the assigned state, or we need to |
| 432 | * load-balance first and go to the INI state. |
| 433 | */ |
| 434 | si->exp = TICK_ETERNITY; |
| 435 | if (unlikely(!(s->flags & SN_ASSIGNED))) |
| 436 | si->state = SI_ST_REQ; |
| 437 | else { |
| 438 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 439 | si->state = SI_ST_ASS; |
| 440 | } |
| 441 | return; |
| 442 | } |
| 443 | |
| 444 | /* Connection request still in queue... */ |
| 445 | if (si->flags & SI_FL_EXP) { |
| 446 | /* ... and timeout expired */ |
| 447 | si->exp = TICK_ETERNITY; |
| 448 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 449 | if (s->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 450 | s->srv->counters.failed_conns++; |
| 451 | s->be->counters.failed_conns++; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 452 | si->shutr(si); |
| 453 | si->shutw(si); |
| 454 | si->ob->flags |= BF_WRITE_TIMEOUT; |
| 455 | if (!si->err_type) |
| 456 | si->err_type = SI_ET_QUEUE_TO; |
| 457 | si->state = SI_ST_CLO; |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 458 | if (s->srv_error) |
| 459 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 460 | return; |
| 461 | } |
| 462 | |
| 463 | /* Connection remains in queue, check if we have to abort it */ |
Willy Tarreau | 418fd47 | 2009-09-06 21:37:23 +0200 | [diff] [blame] | 464 | if ((si->ob->flags & (BF_READ_ERROR)) || |
| 465 | ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */ |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 466 | (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) { |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 467 | /* give up */ |
| 468 | si->exp = TICK_ETERNITY; |
| 469 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 470 | si->shutr(si); |
| 471 | si->shutw(si); |
| 472 | si->err_type |= SI_ET_QUEUE_ABRT; |
| 473 | si->state = SI_ST_CLO; |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 474 | if (s->srv_error) |
| 475 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 476 | return; |
| 477 | } |
| 478 | |
| 479 | /* Nothing changed */ |
| 480 | return; |
| 481 | } |
| 482 | else if (si->state == SI_ST_TAR) { |
| 483 | /* Connection request might be aborted */ |
Willy Tarreau | 418fd47 | 2009-09-06 21:37:23 +0200 | [diff] [blame] | 484 | if ((si->ob->flags & (BF_READ_ERROR)) || |
| 485 | ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */ |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 486 | (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) { |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 487 | /* give up */ |
| 488 | si->exp = TICK_ETERNITY; |
| 489 | si->shutr(si); |
| 490 | si->shutw(si); |
| 491 | si->err_type |= SI_ET_CONN_ABRT; |
| 492 | si->state = SI_ST_CLO; |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 493 | if (s->srv_error) |
| 494 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 495 | return; |
| 496 | } |
| 497 | |
| 498 | if (!(si->flags & SI_FL_EXP)) |
| 499 | return; /* still in turn-around */ |
| 500 | |
| 501 | si->exp = TICK_ETERNITY; |
| 502 | |
| 503 | /* we keep trying on the same server as long as the session is |
| 504 | * marked "assigned". |
| 505 | * FIXME: Should we force a redispatch attempt when the server is down ? |
| 506 | */ |
| 507 | if (s->flags & SN_ASSIGNED) |
| 508 | si->state = SI_ST_ASS; |
| 509 | else |
| 510 | si->state = SI_ST_REQ; |
| 511 | return; |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | /* This function initiates a server connection request on a stream interface |
| 516 | * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS, |
| 517 | * indicating that a server has been assigned. It may also return SI_ST_QUE, |
| 518 | * or SI_ST_CLO upon error. |
| 519 | */ |
| 520 | static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) { |
| 521 | DPRINTF(stderr,"[%u] %s: sess=%p rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rql=%d rpl=%d cs=%d ss=%d\n", |
| 522 | now_ms, __FUNCTION__, |
| 523 | s, |
| 524 | s->req, s->rep, |
| 525 | s->req->rex, s->rep->wex, |
| 526 | s->req->flags, s->rep->flags, |
| 527 | s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state); |
| 528 | |
| 529 | if (si->state != SI_ST_REQ) |
| 530 | return; |
| 531 | |
| 532 | /* Try to assign a server */ |
| 533 | if (srv_redispatch_connect(s) != 0) { |
| 534 | /* We did not get a server. Either we queued the |
| 535 | * connection request, or we encountered an error. |
| 536 | */ |
| 537 | if (si->state == SI_ST_QUE) |
| 538 | return; |
| 539 | |
| 540 | /* we did not get any server, let's check the cause */ |
| 541 | si->shutr(si); |
| 542 | si->shutw(si); |
| 543 | si->ob->flags |= BF_WRITE_ERROR; |
| 544 | if (!si->err_type) |
| 545 | si->err_type = SI_ET_CONN_OTHER; |
| 546 | si->state = SI_ST_CLO; |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 547 | if (s->srv_error) |
| 548 | s->srv_error(s, si); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 549 | return; |
| 550 | } |
| 551 | |
| 552 | /* The server is assigned */ |
| 553 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 554 | si->state = SI_ST_ASS; |
| 555 | } |
| 556 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 557 | /* This stream analyser checks the switching rules and changes the backend |
| 558 | * if appropriate. The default_backend rule is also considered. |
| 559 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 560 | * either needs more data or wants to immediately abort the request. |
| 561 | */ |
| 562 | int process_switching_rules(struct session *s, struct buffer *req, int an_bit) |
| 563 | { |
| 564 | req->analysers &= ~an_bit; |
| 565 | req->analyse_exp = TICK_ETERNITY; |
| 566 | |
| 567 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 568 | now_ms, __FUNCTION__, |
| 569 | s, |
| 570 | req, |
| 571 | req->rex, req->wex, |
| 572 | req->flags, |
| 573 | req->l, |
| 574 | req->analysers); |
| 575 | |
| 576 | /* now check whether we have some switching rules for this request */ |
| 577 | if (!(s->flags & SN_BE_ASSIGNED)) { |
| 578 | struct switching_rule *rule; |
| 579 | |
| 580 | list_for_each_entry(rule, &s->fe->switching_rules, list) { |
| 581 | int ret; |
| 582 | |
| 583 | ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ); |
| 584 | ret = acl_pass(ret); |
| 585 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 586 | ret = !ret; |
| 587 | |
| 588 | if (ret) { |
Willy Tarreau | bedb9ba | 2009-07-12 08:27:39 +0200 | [diff] [blame] | 589 | if (!session_set_backend(s, rule->be.backend)) |
| 590 | goto sw_failed; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 591 | break; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | /* To ensure correct connection accounting on the backend, we |
| 596 | * have to assign one if it was not set (eg: a listen). This |
| 597 | * measure also takes care of correctly setting the default |
| 598 | * backend if any. |
| 599 | */ |
| 600 | if (!(s->flags & SN_BE_ASSIGNED)) |
Willy Tarreau | bedb9ba | 2009-07-12 08:27:39 +0200 | [diff] [blame] | 601 | if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be)) |
| 602 | goto sw_failed; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | /* we don't want to run the HTTP filters again if the backend has not changed */ |
| 606 | if (s->fe == s->be) |
| 607 | s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE; |
| 608 | |
| 609 | return 1; |
Willy Tarreau | bedb9ba | 2009-07-12 08:27:39 +0200 | [diff] [blame] | 610 | |
| 611 | sw_failed: |
| 612 | /* immediately abort this request in case of allocation failure */ |
| 613 | buffer_abort(s->req); |
| 614 | buffer_abort(s->rep); |
| 615 | |
| 616 | if (!(s->flags & SN_ERR_MASK)) |
| 617 | s->flags |= SN_ERR_RESOURCE; |
| 618 | if (!(s->flags & SN_FINST_MASK)) |
| 619 | s->flags |= SN_FINST_R; |
| 620 | |
| 621 | s->txn.status = 500; |
| 622 | s->req->analysers = 0; |
| 623 | s->req->analyse_exp = TICK_ETERNITY; |
| 624 | return 0; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 625 | } |
| 626 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 627 | /* Processes the client, server, request and response jobs of a session task, |
| 628 | * then puts it back to the wait queue in a clean state, or cleans up its |
| 629 | * resources if it must be deleted. Returns in <next> the date the task wants |
| 630 | * to be woken up, or TICK_ETERNITY. In order not to call all functions for |
| 631 | * nothing too many times, the request and response buffers flags are monitored |
| 632 | * and each function is called only if at least another function has changed at |
| 633 | * least one flag it is interested in. |
| 634 | */ |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 635 | struct task *process_session(struct task *t) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 636 | { |
| 637 | struct session *s = t->context; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 638 | unsigned int rqf_last, rpf_last; |
| 639 | |
| 640 | //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__, |
| 641 | // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags); |
| 642 | |
Willy Tarreau | b67a9b8 | 2009-06-21 22:03:51 +0200 | [diff] [blame] | 643 | /* This flag must explicitly be set every time */ |
| 644 | s->req->flags &= ~BF_READ_NOEXP; |
| 645 | |
| 646 | /* Keep a copy of req/rep flags so that we can detect shutdowns */ |
| 647 | rqf_last = s->req->flags; |
| 648 | rpf_last = s->rep->flags; |
| 649 | |
Willy Tarreau | 89f7ef2 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 650 | /* we don't want the stream interface functions to recursively wake us up */ |
| 651 | if (s->req->prod->owner == t) |
| 652 | s->req->prod->flags |= SI_FL_DONT_WAKE; |
| 653 | if (s->req->cons->owner == t) |
| 654 | s->req->cons->flags |= SI_FL_DONT_WAKE; |
| 655 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 656 | /* 1a: Check for low level timeouts if needed. We just set a flag on |
| 657 | * stream interfaces when their timeouts have expired. |
| 658 | */ |
| 659 | if (unlikely(t->state & TASK_WOKEN_TIMER)) { |
| 660 | stream_int_check_timeouts(&s->si[0]); |
| 661 | stream_int_check_timeouts(&s->si[1]); |
Willy Tarreau | b67a9b8 | 2009-06-21 22:03:51 +0200 | [diff] [blame] | 662 | |
| 663 | /* check buffer timeouts, and close the corresponding stream interfaces |
| 664 | * for future reads or writes. Note: this will also concern upper layers |
| 665 | * but we do not touch any other flag. We must be careful and correctly |
| 666 | * detect state changes when calling them. |
| 667 | */ |
| 668 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 669 | buffer_check_timeouts(s->req); |
Willy Tarreau | b67a9b8 | 2009-06-21 22:03:51 +0200 | [diff] [blame] | 670 | |
| 671 | if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT)) |
| 672 | s->req->prod->shutr(s->req->prod); |
| 673 | |
| 674 | if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) |
| 675 | s->req->cons->shutw(s->req->cons); |
| 676 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 677 | buffer_check_timeouts(s->rep); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 678 | |
Willy Tarreau | b67a9b8 | 2009-06-21 22:03:51 +0200 | [diff] [blame] | 679 | if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT)) |
| 680 | s->rep->prod->shutr(s->rep->prod); |
Willy Tarreau | 86491c3 | 2008-12-14 09:04:47 +0100 | [diff] [blame] | 681 | |
Willy Tarreau | b67a9b8 | 2009-06-21 22:03:51 +0200 | [diff] [blame] | 682 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) |
| 683 | s->rep->cons->shutw(s->rep->cons); |
| 684 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 685 | |
| 686 | /* 1b: check for low-level errors reported at the stream interface. |
| 687 | * First we check if it's a retryable error (in which case we don't |
| 688 | * want to tell the buffer). Otherwise we report the error one level |
| 689 | * upper by setting flags into the buffers. Note that the side towards |
| 690 | * the client cannot have connect (hence retryable) errors. Also, the |
| 691 | * connection setup code must be able to deal with any type of abort. |
| 692 | */ |
| 693 | if (unlikely(s->si[0].flags & SI_FL_ERR)) { |
| 694 | if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) { |
| 695 | s->si[0].shutr(&s->si[0]); |
| 696 | s->si[0].shutw(&s->si[0]); |
| 697 | stream_int_report_error(&s->si[0]); |
Willy Tarreau | 05cb29b | 2008-12-14 11:44:04 +0100 | [diff] [blame] | 698 | if (!(s->req->analysers) && !(s->rep->analysers)) { |
| 699 | if (!(s->flags & SN_ERR_MASK)) |
| 700 | s->flags |= SN_ERR_CLICL; |
| 701 | if (!(s->flags & SN_FINST_MASK)) |
| 702 | s->flags |= SN_FINST_D; |
| 703 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 704 | } |
| 705 | } |
| 706 | |
| 707 | if (unlikely(s->si[1].flags & SI_FL_ERR)) { |
| 708 | if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) { |
| 709 | s->si[1].shutr(&s->si[1]); |
| 710 | s->si[1].shutw(&s->si[1]); |
| 711 | stream_int_report_error(&s->si[1]); |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 712 | s->be->counters.failed_resp++; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 713 | if (s->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 714 | s->srv->counters.failed_resp++; |
Willy Tarreau | 05cb29b | 2008-12-14 11:44:04 +0100 | [diff] [blame] | 715 | if (!(s->req->analysers) && !(s->rep->analysers)) { |
| 716 | if (!(s->flags & SN_ERR_MASK)) |
| 717 | s->flags |= SN_ERR_SRVCL; |
| 718 | if (!(s->flags & SN_FINST_MASK)) |
| 719 | s->flags |= SN_FINST_D; |
| 720 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 721 | } |
| 722 | /* note: maybe we should process connection errors here ? */ |
| 723 | } |
| 724 | |
| 725 | if (s->si[1].state == SI_ST_CON) { |
| 726 | /* we were trying to establish a connection on the server side, |
| 727 | * maybe it succeeded, maybe it failed, maybe we timed out, ... |
| 728 | */ |
| 729 | if (unlikely(!sess_update_st_con_tcp(s, &s->si[1]))) |
| 730 | sess_update_st_cer(s, &s->si[1]); |
| 731 | else if (s->si[1].state == SI_ST_EST) |
| 732 | sess_establish(s, &s->si[1]); |
| 733 | |
| 734 | /* state is now one of SI_ST_CON (still in progress), SI_ST_EST |
| 735 | * (established), SI_ST_DIS (abort), SI_ST_CLO (last error), |
| 736 | * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors. |
| 737 | */ |
| 738 | } |
| 739 | |
Willy Tarreau | b67a9b8 | 2009-06-21 22:03:51 +0200 | [diff] [blame] | 740 | resync_stream_interface: |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 741 | /* Check for connection closure */ |
| 742 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 743 | DPRINTF(stderr, |
| 744 | "[%u] %s:%d: task=%p s=%p, sfl=0x%08x, rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rql=%d rpl=%d cs=%d ss=%d, cet=0x%x set=0x%x retr=%d\n", |
| 745 | now_ms, __FUNCTION__, __LINE__, |
| 746 | t, |
| 747 | s, s->flags, |
| 748 | s->req, s->rep, |
| 749 | s->req->rex, s->rep->wex, |
| 750 | s->req->flags, s->rep->flags, |
| 751 | s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state, |
| 752 | s->rep->cons->err_type, s->req->cons->err_type, |
| 753 | s->conn_retries); |
| 754 | |
| 755 | /* nothing special to be done on client side */ |
| 756 | if (unlikely(s->req->prod->state == SI_ST_DIS)) |
| 757 | s->req->prod->state = SI_ST_CLO; |
| 758 | |
| 759 | /* When a server-side connection is released, we have to count it and |
| 760 | * check for pending connections on this server. |
| 761 | */ |
| 762 | if (unlikely(s->req->cons->state == SI_ST_DIS)) { |
| 763 | s->req->cons->state = SI_ST_CLO; |
| 764 | if (s->srv) { |
| 765 | if (s->flags & SN_CURR_SESS) { |
| 766 | s->flags &= ~SN_CURR_SESS; |
| 767 | s->srv->cur_sess--; |
| 768 | } |
| 769 | sess_change_server(s, NULL); |
| 770 | if (may_dequeue_tasks(s->srv, s->be)) |
| 771 | process_srv_queue(s->srv); |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | /* |
| 776 | * Note: of the transient states (REQ, CER, DIS), only REQ may remain |
| 777 | * at this point. |
| 778 | */ |
| 779 | |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 780 | resync_request: |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 781 | /* Analyse request */ |
| 782 | if ((s->req->flags & BF_MASK_ANALYSER) || |
| 783 | (s->req->flags ^ rqf_last) & BF_MASK_STATIC) { |
| 784 | unsigned int flags = s->req->flags; |
| 785 | |
| 786 | if (s->req->prod->state >= SI_ST_EST) { |
Willy Tarreau | 1a52dbd | 2009-06-28 19:37:53 +0200 | [diff] [blame] | 787 | unsigned int last_ana = 0; |
| 788 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 789 | /* it's up to the analysers to stop new connections */ |
| 790 | buffer_auto_connect(s->req); |
| 791 | buffer_auto_close(s->req); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 792 | |
| 793 | /* We will call all analysers for which a bit is set in |
| 794 | * s->req->analysers, following the bit order from LSB |
| 795 | * to MSB. The analysers must remove themselves from |
Willy Tarreau | 1a52dbd | 2009-06-28 19:37:53 +0200 | [diff] [blame] | 796 | * the list when not needed. Any analyser may return 0 |
| 797 | * to break out of the loop, either because of missing |
| 798 | * data to take a decision, or because it decides to |
| 799 | * kill the session. We loop at least once through each |
| 800 | * analyser, and we may loop again if other analysers |
| 801 | * are added in the middle. |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 802 | */ |
Willy Tarreau | 1a52dbd | 2009-06-28 19:37:53 +0200 | [diff] [blame] | 803 | while (s->req->analysers & ~last_ana) { |
| 804 | last_ana = s->req->analysers; |
| 805 | |
| 806 | if (s->req->analysers & AN_REQ_INSPECT) { |
| 807 | last_ana |= AN_REQ_INSPECT; |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 808 | if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT)) |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 809 | break; |
Willy Tarreau | 1a52dbd | 2009-06-28 19:37:53 +0200 | [diff] [blame] | 810 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 811 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 812 | if (s->req->analysers & AN_REQ_WAIT_HTTP) { |
| 813 | last_ana |= AN_REQ_WAIT_HTTP; |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 814 | if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP)) |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 815 | break; |
| 816 | } |
| 817 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 818 | if (s->req->analysers & AN_REQ_HTTP_PROCESS_FE) { |
| 819 | last_ana |= AN_REQ_HTTP_PROCESS_FE; |
| 820 | if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe)) |
| 821 | break; |
| 822 | } |
| 823 | |
| 824 | if (s->req->analysers & AN_REQ_SWITCHING_RULES) { |
| 825 | last_ana |= AN_REQ_SWITCHING_RULES; |
| 826 | if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES)) |
| 827 | break; |
Willy Tarreau | d88bb6f | 2009-07-12 09:55:41 +0200 | [diff] [blame] | 828 | /* FIXME: we mait switch from TCP to HTTP and want to |
| 829 | * immediately loop back to the top. This is a dirty way |
| 830 | * of doing it, and we should find a cleaner method relying |
| 831 | * on a circular list of function pointers. |
| 832 | */ |
| 833 | if ((s->req->analysers & ~last_ana) & AN_REQ_WAIT_HTTP) |
| 834 | continue; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | if (s->req->analysers & AN_REQ_HTTP_PROCESS_BE) { |
| 838 | last_ana |= AN_REQ_HTTP_PROCESS_BE; |
| 839 | if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be)) |
| 840 | break; |
| 841 | } |
| 842 | |
Willy Tarreau | 1a52dbd | 2009-06-28 19:37:53 +0200 | [diff] [blame] | 843 | if (s->req->analysers & AN_REQ_HTTP_TARPIT) { |
| 844 | last_ana |= AN_REQ_HTTP_TARPIT; |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 845 | if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT)) |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 846 | break; |
Willy Tarreau | 1a52dbd | 2009-06-28 19:37:53 +0200 | [diff] [blame] | 847 | } |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 848 | |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 849 | if (s->req->analysers & AN_REQ_HTTP_INNER) { |
| 850 | last_ana |= AN_REQ_HTTP_INNER; |
| 851 | if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER)) |
| 852 | break; |
| 853 | } |
| 854 | |
Willy Tarreau | 1a52dbd | 2009-06-28 19:37:53 +0200 | [diff] [blame] | 855 | if (s->req->analysers & AN_REQ_HTTP_BODY) { |
| 856 | last_ana |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 857 | if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY)) |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 858 | break; |
Willy Tarreau | 1a52dbd | 2009-06-28 19:37:53 +0200 | [diff] [blame] | 859 | } |
Emeric Brun | 647caf1 | 2009-06-30 17:57:00 +0200 | [diff] [blame] | 860 | |
| 861 | if (s->req->analysers & AN_REQ_PRST_RDP_COOKIE) { |
| 862 | last_ana |= AN_REQ_PRST_RDP_COOKIE; |
| 863 | if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE)) |
| 864 | break; |
| 865 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 866 | |
| 867 | if (s->req->analysers & AN_REQ_HTTP_XFER_BODY) { |
| 868 | last_ana |= AN_REQ_HTTP_XFER_BODY; |
| 869 | if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY)) |
| 870 | break; |
| 871 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 872 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 873 | } |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 874 | |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 875 | if ((s->req->flags ^ flags) & BF_MASK_STATIC) { |
| 876 | rqf_last = s->req->flags; |
| 877 | goto resync_request; |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | resync_response: |
| 882 | /* Analyse response */ |
| 883 | |
| 884 | if (unlikely(s->rep->flags & BF_HIJACK)) { |
| 885 | /* In inject mode, we wake up everytime something has |
| 886 | * happened on the write side of the buffer. |
| 887 | */ |
| 888 | unsigned int flags = s->rep->flags; |
| 889 | |
| 890 | if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) && |
| 891 | !(s->rep->flags & BF_FULL)) { |
| 892 | s->rep->hijacker(s, s->rep); |
| 893 | } |
| 894 | |
| 895 | if ((s->rep->flags ^ flags) & BF_MASK_STATIC) { |
| 896 | rpf_last = s->rep->flags; |
| 897 | goto resync_response; |
| 898 | } |
| 899 | } |
| 900 | else if ((s->rep->flags & BF_MASK_ANALYSER) || |
| 901 | (s->rep->flags ^ rpf_last) & BF_MASK_STATIC) { |
| 902 | unsigned int flags = s->rep->flags; |
| 903 | |
| 904 | if (s->rep->prod->state >= SI_ST_EST) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 905 | unsigned int last_ana = 0; |
| 906 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 907 | /* it's up to the analysers to reset auto_close */ |
| 908 | buffer_auto_close(s->rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 909 | |
| 910 | /* We will call all analysers for which a bit is set in |
| 911 | * s->rep->analysers, following the bit order from LSB |
| 912 | * to MSB. The analysers must remove themselves from |
| 913 | * the list when not needed. Any analyser may return 0 |
| 914 | * to break out of the loop, either because of missing |
| 915 | * data to take a decision, or because it decides to |
| 916 | * kill the session. We loop at least once through each |
| 917 | * analyser, and we may loop again if other analysers |
| 918 | * are added in the middle. |
| 919 | */ |
| 920 | while (s->rep->analysers & ~last_ana) { |
| 921 | last_ana = s->rep->analysers; |
| 922 | |
| 923 | if (s->rep->analysers & AN_RES_WAIT_HTTP) { |
| 924 | last_ana |= AN_RES_WAIT_HTTP; |
| 925 | if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP)) |
| 926 | break; |
| 927 | } |
| 928 | |
| 929 | if (s->rep->analysers & AN_RES_HTTP_PROCESS_BE) { |
| 930 | last_ana |= AN_RES_HTTP_PROCESS_BE; |
| 931 | if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be)) |
| 932 | break; |
| 933 | /* FIXME: we may wait for a second response in case of a status 1xx |
| 934 | * and want to immediately loop back to the top. This is a dirty way |
| 935 | * of doing it, and we should find a cleaner method relying on a |
| 936 | * circular list of function pointers. |
| 937 | */ |
| 938 | if ((s->rep->analysers & ~last_ana) & AN_RES_WAIT_HTTP) |
| 939 | continue; |
| 940 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 941 | |
| 942 | if (s->rep->analysers & AN_RES_HTTP_XFER_BODY) { |
| 943 | last_ana |= AN_RES_HTTP_XFER_BODY; |
| 944 | if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY)) |
| 945 | break; |
| 946 | } |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 947 | } |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | if ((s->rep->flags ^ flags) & BF_MASK_STATIC) { |
| 951 | rpf_last = s->rep->flags; |
| 952 | goto resync_response; |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | /* FIXME: here we should call protocol handlers which rely on |
| 957 | * both buffers. |
| 958 | */ |
| 959 | |
| 960 | |
| 961 | /* |
| 962 | * Now we propagate unhandled errors to the session |
| 963 | */ |
| 964 | if (!(s->flags & SN_ERR_MASK)) { |
| 965 | if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) { |
| 966 | /* Report it if the client got an error or a read timeout expired */ |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 967 | s->req->analysers = 0; |
| 968 | if (s->req->flags & BF_READ_ERROR) |
| 969 | s->flags |= SN_ERR_CLICL; |
| 970 | else if (s->req->flags & BF_READ_TIMEOUT) |
| 971 | s->flags |= SN_ERR_CLITO; |
| 972 | else if (s->req->flags & BF_WRITE_ERROR) |
| 973 | s->flags |= SN_ERR_SRVCL; |
| 974 | else |
| 975 | s->flags |= SN_ERR_SRVTO; |
| 976 | sess_set_term_flags(s); |
| 977 | } |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 978 | else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) { |
| 979 | /* Report it if the server got an error or a read timeout expired */ |
| 980 | s->rep->analysers = 0; |
| 981 | if (s->rep->flags & BF_READ_ERROR) |
| 982 | s->flags |= SN_ERR_SRVCL; |
| 983 | else if (s->rep->flags & BF_READ_TIMEOUT) |
| 984 | s->flags |= SN_ERR_SRVTO; |
| 985 | else if (s->rep->flags & BF_WRITE_ERROR) |
| 986 | s->flags |= SN_ERR_CLICL; |
| 987 | else |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 988 | s->flags |= SN_ERR_CLITO; |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 989 | sess_set_term_flags(s); |
| 990 | } |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 991 | } |
| 992 | |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 993 | /* |
| 994 | * Here we take care of forwarding unhandled data. This also includes |
| 995 | * connection establishments and shutdown requests. |
| 996 | */ |
| 997 | |
| 998 | |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 999 | /* If noone is interested in analysing data, it's time to forward |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 1000 | * everything. We configure the buffer to forward indefinitely. |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 1001 | */ |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1002 | if (!s->req->analysers && |
Willy Tarreau | 82eeaf2 | 2009-12-29 12:09:05 +0100 | [diff] [blame^] | 1003 | !(s->req->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTW_NOW)) && |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 1004 | (s->req->prod->state >= SI_ST_EST) && |
| 1005 | (s->req->to_forward != BUF_INFINITE_FORWARD)) { |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1006 | /* This buffer is freewheeling, there's no analyser nor hijacker |
| 1007 | * attached to it. If any data are left in, we'll permit them to |
| 1008 | * move. |
| 1009 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1010 | buffer_auto_connect(s->req); |
| 1011 | buffer_auto_close(s->req); |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1012 | buffer_flush(s->req); |
Willy Tarreau | 5bd8c37 | 2009-01-19 00:32:22 +0100 | [diff] [blame] | 1013 | |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 1014 | /* If the producer is still connected, we'll enable data to flow |
| 1015 | * from the producer to the consumer (which might possibly not be |
| 1016 | * connected yet). |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1017 | */ |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 1018 | if (!(s->req->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW))) |
| 1019 | buffer_forward(s->req, BUF_INFINITE_FORWARD); |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 1020 | } |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 1021 | |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1022 | /* check if it is wise to enable kernel splicing to forward request data */ |
| 1023 | if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) && |
| 1024 | s->req->to_forward && |
| 1025 | (global.tune.options & GTUNE_USE_SPLICE) && |
Willy Tarreau | dc340a9 | 2009-06-28 23:10:19 +0200 | [diff] [blame] | 1026 | (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) && |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1027 | (pipes_used < global.maxpipes) && |
| 1028 | (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) || |
| 1029 | (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) && |
| 1030 | (s->req->flags & BF_STREAMER_FAST)))) { |
| 1031 | s->req->flags |= BF_KERN_SPLICING; |
| 1032 | } |
| 1033 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1034 | /* reflect what the L7 analysers have seen last */ |
| 1035 | rqf_last = s->req->flags; |
| 1036 | |
| 1037 | /* |
| 1038 | * Now forward all shutdown requests between both sides of the buffer |
| 1039 | */ |
| 1040 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1041 | /* first, let's check if the request buffer needs to shutdown(write), which may |
| 1042 | * happen either because the input is closed or because we want to force a close |
| 1043 | * once the server has begun to respond. |
| 1044 | */ |
Willy Tarreau | 82eeaf2 | 2009-12-29 12:09:05 +0100 | [diff] [blame^] | 1045 | if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) == |
| 1046 | (BF_AUTO_CLOSE|BF_SHUTR))) |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 1047 | buffer_shutw_now(s->req); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1048 | |
| 1049 | /* shutdown(write) pending */ |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 1050 | if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_OUT_EMPTY)) == (BF_SHUTW_NOW|BF_OUT_EMPTY))) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1051 | s->req->cons->shutw(s->req->cons); |
| 1052 | |
| 1053 | /* shutdown(write) done on server side, we must stop the client too */ |
Willy Tarreau | 3dbc694 | 2008-12-07 13:05:04 +0100 | [diff] [blame] | 1054 | if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW && |
| 1055 | !s->req->analysers)) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1056 | buffer_shutr_now(s->req); |
| 1057 | |
| 1058 | /* shutdown(read) pending */ |
| 1059 | if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW)) |
| 1060 | s->req->prod->shutr(s->req->prod); |
| 1061 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1062 | /* it's possible that an upper layer has requested a connection setup or abort. |
| 1063 | * There are 2 situations where we decide to establish a new connection : |
| 1064 | * - there are data scheduled for emission in the buffer |
| 1065 | * - the BF_AUTO_CONNECT flag is set (active connection) |
| 1066 | */ |
| 1067 | if (s->req->cons->state == SI_ST_INI) { |
| 1068 | if (!(s->req->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 1069 | if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) { |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1070 | /* If we have a ->connect method, we need to perform a connection request, |
| 1071 | * otherwise we immediately switch to the connected state. |
| 1072 | */ |
| 1073 | if (s->req->cons->connect) |
| 1074 | s->req->cons->state = SI_ST_REQ; /* new connection requested */ |
| 1075 | else |
| 1076 | s->req->cons->state = SI_ST_EST; /* connection established */ |
| 1077 | } |
Willy Tarreau | 7320122 | 2009-08-16 18:27:24 +0200 | [diff] [blame] | 1078 | } |
Willy Tarreau | f41ffdc | 2009-09-20 08:19:25 +0200 | [diff] [blame] | 1079 | else { |
Willy Tarreau | 9279562 | 2009-03-06 12:51:23 +0100 | [diff] [blame] | 1080 | s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */ |
Willy Tarreau | f41ffdc | 2009-09-20 08:19:25 +0200 | [diff] [blame] | 1081 | buffer_shutw_now(s->req); /* fix buffer flags upon abort */ |
| 1082 | buffer_shutr_now(s->rep); |
| 1083 | } |
Willy Tarreau | 9279562 | 2009-03-06 12:51:23 +0100 | [diff] [blame] | 1084 | } |
| 1085 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1086 | |
| 1087 | /* we may have a pending connection request, or a connection waiting |
| 1088 | * for completion. |
| 1089 | */ |
| 1090 | if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) { |
| 1091 | do { |
| 1092 | /* nb: step 1 might switch from QUE to ASS, but we first want |
| 1093 | * to give a chance to step 2 to perform a redirect if needed. |
| 1094 | */ |
| 1095 | if (s->si[1].state != SI_ST_REQ) |
| 1096 | sess_update_stream_int(s, &s->si[1]); |
| 1097 | if (s->si[1].state == SI_ST_REQ) |
| 1098 | sess_prepare_conn_req(s, &s->si[1]); |
| 1099 | |
| 1100 | if (s->si[1].state == SI_ST_ASS && s->srv && |
| 1101 | s->srv->rdr_len && (s->flags & SN_REDIRECTABLE)) |
| 1102 | perform_http_redirect(s, &s->si[1]); |
| 1103 | } while (s->si[1].state == SI_ST_ASS); |
| 1104 | } |
| 1105 | |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1106 | /* Benchmarks have shown that it's optimal to do a full resync now */ |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 1107 | if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1108 | goto resync_stream_interface; |
| 1109 | |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1110 | /* otherwise wewant to check if we need to resync the req buffer or not */ |
| 1111 | if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC) |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 1112 | goto resync_request; |
| 1113 | |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1114 | /* perform output updates to the response buffer */ |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 1115 | |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1116 | /* If noone is interested in analysing data, it's time to forward |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 1117 | * everything. We configure the buffer to forward indefinitely. |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 1118 | */ |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1119 | if (!s->rep->analysers && |
Willy Tarreau | 82eeaf2 | 2009-12-29 12:09:05 +0100 | [diff] [blame^] | 1120 | !(s->rep->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTW_NOW)) && |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 1121 | (s->rep->prod->state >= SI_ST_EST) && |
| 1122 | (s->rep->to_forward != BUF_INFINITE_FORWARD)) { |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1123 | /* This buffer is freewheeling, there's no analyser nor hijacker |
| 1124 | * attached to it. If any data are left in, we'll permit them to |
| 1125 | * move. |
| 1126 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1127 | buffer_auto_close(s->rep); |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1128 | buffer_flush(s->rep); |
Willy Tarreau | 31971e5 | 2009-09-20 12:07:52 +0200 | [diff] [blame] | 1129 | if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW))) |
| 1130 | buffer_forward(s->rep, BUF_INFINITE_FORWARD); |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 1131 | } |
Willy Tarreau | f890dc9 | 2008-12-13 21:12:26 +0100 | [diff] [blame] | 1132 | |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1133 | /* check if it is wise to enable kernel splicing to forward response data */ |
| 1134 | if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) && |
| 1135 | s->rep->to_forward && |
| 1136 | (global.tune.options & GTUNE_USE_SPLICE) && |
Willy Tarreau | dc340a9 | 2009-06-28 23:10:19 +0200 | [diff] [blame] | 1137 | (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) && |
Willy Tarreau | 7c84bab | 2009-03-08 21:38:23 +0100 | [diff] [blame] | 1138 | (pipes_used < global.maxpipes) && |
| 1139 | (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) || |
| 1140 | (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) && |
| 1141 | (s->rep->flags & BF_STREAMER_FAST)))) { |
| 1142 | s->rep->flags |= BF_KERN_SPLICING; |
| 1143 | } |
| 1144 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1145 | /* reflect what the L7 analysers have seen last */ |
| 1146 | rpf_last = s->rep->flags; |
| 1147 | |
| 1148 | /* |
| 1149 | * Now forward all shutdown requests between both sides of the buffer |
| 1150 | */ |
| 1151 | |
| 1152 | /* |
| 1153 | * FIXME: this is probably where we should produce error responses. |
| 1154 | */ |
| 1155 | |
Willy Tarreau | 6b66f3e | 2008-12-14 17:31:54 +0100 | [diff] [blame] | 1156 | /* first, let's check if the response buffer needs to shutdown(write) */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1157 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) == |
| 1158 | (BF_AUTO_CLOSE|BF_SHUTR))) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1159 | buffer_shutw_now(s->rep); |
| 1160 | |
| 1161 | /* shutdown(write) pending */ |
Willy Tarreau | ba0b63d | 2009-09-20 08:09:44 +0200 | [diff] [blame] | 1162 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_OUT_EMPTY|BF_SHUTW_NOW)) == (BF_OUT_EMPTY|BF_SHUTW_NOW))) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1163 | s->rep->cons->shutw(s->rep->cons); |
| 1164 | |
| 1165 | /* shutdown(write) done on the client side, we must stop the server too */ |
Willy Tarreau | 3dbc694 | 2008-12-07 13:05:04 +0100 | [diff] [blame] | 1166 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) && |
| 1167 | !s->rep->analysers) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1168 | buffer_shutr_now(s->rep); |
| 1169 | |
| 1170 | /* shutdown(read) pending */ |
| 1171 | if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW)) |
| 1172 | s->rep->prod->shutr(s->rep->prod); |
| 1173 | |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 1174 | if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1175 | goto resync_stream_interface; |
| 1176 | |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 1177 | if (s->req->flags != rqf_last) |
| 1178 | goto resync_request; |
| 1179 | |
Willy Tarreau | 3deb3d0 | 2009-06-21 22:43:05 +0200 | [diff] [blame] | 1180 | if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC) |
Willy Tarreau | 0be0ef9 | 2009-03-08 19:20:25 +0100 | [diff] [blame] | 1181 | goto resync_response; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1182 | |
Willy Tarreau | 89f7ef2 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 1183 | /* we're interested in getting wakeups again */ |
| 1184 | s->req->prod->flags &= ~SI_FL_DONT_WAKE; |
| 1185 | s->req->cons->flags &= ~SI_FL_DONT_WAKE; |
| 1186 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1187 | /* This is needed only when debugging is enabled, to indicate |
| 1188 | * client-side or server-side close. Please note that in the unlikely |
| 1189 | * event where both sides would close at once, the sequence is reported |
| 1190 | * on the server side first. |
| 1191 | */ |
| 1192 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1193 | (!(global.mode & MODE_QUIET) || |
| 1194 | (global.mode & MODE_VERBOSE)))) { |
| 1195 | int len; |
| 1196 | |
| 1197 | if (s->si[1].state == SI_ST_CLO && |
| 1198 | s->si[1].prev_state == SI_ST_EST) { |
| 1199 | len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", |
| 1200 | s->uniq_id, s->be->id, |
| 1201 | (unsigned short)s->si[0].fd, |
| 1202 | (unsigned short)s->si[1].fd); |
| 1203 | write(1, trash, len); |
| 1204 | } |
| 1205 | |
| 1206 | if (s->si[0].state == SI_ST_CLO && |
| 1207 | s->si[0].prev_state == SI_ST_EST) { |
| 1208 | len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", |
| 1209 | s->uniq_id, s->be->id, |
| 1210 | (unsigned short)s->si[0].fd, |
| 1211 | (unsigned short)s->si[1].fd); |
| 1212 | write(1, trash, len); |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | if (likely((s->rep->cons->state != SI_ST_CLO) || |
| 1217 | (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) { |
| 1218 | |
| 1219 | if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED)) |
| 1220 | session_process_counters(s); |
| 1221 | |
Willy Tarreau | 1accfc0 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 1222 | if (s->rep->cons->state == SI_ST_EST && !s->rep->cons->iohandler) |
Willy Tarreau | dc85b39 | 2009-08-18 07:38:19 +0200 | [diff] [blame] | 1223 | s->rep->cons->update(s->rep->cons); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1224 | |
Willy Tarreau | 1accfc0 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 1225 | if (s->req->cons->state == SI_ST_EST && !s->req->cons->iohandler) |
Willy Tarreau | dc85b39 | 2009-08-18 07:38:19 +0200 | [diff] [blame] | 1226 | s->req->cons->update(s->req->cons); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1227 | |
Willy Tarreau | ea38854 | 2009-06-21 21:45:58 +0200 | [diff] [blame] | 1228 | s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL); |
| 1229 | s->rep->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1230 | s->si[0].prev_state = s->si[0].state; |
| 1231 | s->si[1].prev_state = s->si[1].state; |
Willy Tarreau | b0ef735 | 2008-12-14 13:26:20 +0100 | [diff] [blame] | 1232 | s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP); |
| 1233 | s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1234 | |
| 1235 | /* Trick: if a request is being waiting for the server to respond, |
| 1236 | * and if we know the server can timeout, we don't want the timeout |
| 1237 | * to expire on the client side first, but we're still interested |
| 1238 | * in passing data from the client to the server (eg: POST). Thus, |
| 1239 | * we can cancel the client's request timeout if the server's |
| 1240 | * request timeout is set and the server has not yet sent a response. |
| 1241 | */ |
| 1242 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1243 | if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 && |
Willy Tarreau | 86491c3 | 2008-12-14 09:04:47 +0100 | [diff] [blame] | 1244 | (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) { |
| 1245 | s->req->flags |= BF_READ_NOEXP; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1246 | s->req->rex = TICK_ETERNITY; |
Willy Tarreau | 86491c3 | 2008-12-14 09:04:47 +0100 | [diff] [blame] | 1247 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1248 | |
Willy Tarreau | 1accfc0 | 2009-09-05 20:57:35 +0200 | [diff] [blame] | 1249 | /* Call the second stream interface's I/O handler if it's embedded. |
| 1250 | * Note that this one may wake the task up again. |
| 1251 | */ |
| 1252 | if (s->req->cons->iohandler) { |
| 1253 | s->req->cons->iohandler(s->req->cons); |
| 1254 | if (task_in_rq(t)) { |
| 1255 | /* If we woke up, we don't want to requeue the |
| 1256 | * task to the wait queue, but rather requeue |
| 1257 | * it into the runqueue ASAP. |
| 1258 | */ |
| 1259 | t->expire = TICK_ETERNITY; |
| 1260 | return t; |
| 1261 | } |
| 1262 | } |
| 1263 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1264 | t->expire = tick_first(tick_first(s->req->rex, s->req->wex), |
| 1265 | tick_first(s->rep->rex, s->rep->wex)); |
| 1266 | if (s->req->analysers) |
| 1267 | t->expire = tick_first(t->expire, s->req->analyse_exp); |
| 1268 | |
| 1269 | if (s->si[0].exp) |
| 1270 | t->expire = tick_first(t->expire, s->si[0].exp); |
| 1271 | |
| 1272 | if (s->si[1].exp) |
| 1273 | t->expire = tick_first(t->expire, s->si[1].exp); |
| 1274 | |
| 1275 | #ifdef DEBUG_FULL |
Willy Tarreau | 127334e | 2009-03-28 10:47:26 +0100 | [diff] [blame] | 1276 | fprintf(stderr, |
| 1277 | "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u" |
| 1278 | " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n", |
| 1279 | now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp, |
| 1280 | s->rep->rex, s->rep->wex, s->si[0].exp, s->si[1].exp, s->si[0].state, s->si[1].state); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1281 | #endif |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1282 | |
| 1283 | #ifdef DEBUG_DEV |
| 1284 | /* this may only happen when no timeout is set or in case of an FSM bug */ |
Willy Tarreau | d0a201b | 2009-03-08 15:53:06 +0100 | [diff] [blame] | 1285 | if (!tick_isset(t->expire)) |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1286 | ABORT_NOW(); |
| 1287 | #endif |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1288 | return t; /* nothing more to do */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | s->fe->feconn--; |
| 1292 | if (s->flags & SN_BE_ASSIGNED) |
| 1293 | s->be->beconn--; |
| 1294 | actconn--; |
Willy Tarreau | 6e6fb2b | 2009-08-16 18:20:44 +0200 | [diff] [blame] | 1295 | s->listener->nbconn--; |
| 1296 | if (s->listener->state == LI_FULL && |
| 1297 | s->listener->nbconn < s->listener->maxconn) { |
| 1298 | /* we should reactivate the listener */ |
| 1299 | EV_FD_SET(s->listener->fd, DIR_RD); |
| 1300 | s->listener->state = LI_READY; |
| 1301 | } |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1302 | |
| 1303 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1304 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 1305 | int len; |
Willy Tarreau | ec22b2c | 2009-03-06 13:07:40 +0100 | [diff] [blame] | 1306 | len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n", |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1307 | s->uniq_id, s->be->id, |
Willy Tarreau | ec22b2c | 2009-03-06 13:07:40 +0100 | [diff] [blame] | 1308 | (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1309 | write(1, trash, len); |
| 1310 | } |
| 1311 | |
| 1312 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 1313 | session_process_counters(s); |
| 1314 | |
Krzysztof Piotr Oledzki | de71d16 | 2009-10-24 15:36:15 +0200 | [diff] [blame] | 1315 | if (s->txn.status) { |
| 1316 | int n; |
| 1317 | |
| 1318 | n = s->txn.status / 100; |
| 1319 | if (n < 1 || n > 5) |
| 1320 | n = 0; |
| 1321 | |
| 1322 | if (s->fe->mode == PR_MODE_HTTP) |
| 1323 | s->fe->counters.p.http.rsp[n]++; |
| 1324 | |
| 1325 | if ((s->flags & SN_BE_ASSIGNED) && (s->fe != s->be) && |
| 1326 | (s->be->mode == PR_MODE_HTTP)) |
| 1327 | s->be->counters.p.http.rsp[n]++; |
| 1328 | } |
| 1329 | |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1330 | /* let's do a final log if we need it */ |
| 1331 | if (s->logs.logwait && |
| 1332 | !(s->flags & SN_MONITOR) && |
| 1333 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 1334 | s->do_log(s); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | /* the task MUST not be in the run queue anymore */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1338 | session_free(s); |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1339 | task_delete(t); |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1340 | task_free(t); |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 1341 | return NULL; |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1342 | } |
| 1343 | |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1344 | /* |
| 1345 | * This function adjusts sess->srv_conn and maintains the previous and new |
| 1346 | * server's served session counts. Setting newsrv to NULL is enough to release |
| 1347 | * current connection slot. This function also notifies any LB algo which might |
| 1348 | * expect to be informed about any change in the number of active sessions on a |
| 1349 | * server. |
| 1350 | */ |
| 1351 | void sess_change_server(struct session *sess, struct server *newsrv) |
| 1352 | { |
| 1353 | if (sess->srv_conn == newsrv) |
| 1354 | return; |
| 1355 | |
| 1356 | if (sess->srv_conn) { |
| 1357 | sess->srv_conn->served--; |
| 1358 | if (sess->srv_conn->proxy->lbprm.server_drop_conn) |
| 1359 | sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn); |
| 1360 | sess->srv_conn = NULL; |
| 1361 | } |
| 1362 | |
| 1363 | if (newsrv) { |
| 1364 | newsrv->served++; |
| 1365 | if (newsrv->proxy->lbprm.server_take_conn) |
| 1366 | newsrv->proxy->lbprm.server_take_conn(newsrv); |
| 1367 | sess->srv_conn = newsrv; |
| 1368 | } |
| 1369 | } |
| 1370 | |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 1371 | /* Set correct session termination flags in case no analyser has done it. It |
| 1372 | * also counts a failed request if the server state has not reached the request |
| 1373 | * stage. |
| 1374 | */ |
| 1375 | void sess_set_term_flags(struct session *s) |
| 1376 | { |
| 1377 | if (!(s->flags & SN_FINST_MASK)) { |
| 1378 | if (s->si[1].state < SI_ST_REQ) { |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1379 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 1380 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1381 | if (s->listener->counters) |
| 1382 | s->listener->counters->failed_req++; |
| 1383 | |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 1384 | s->flags |= SN_FINST_R; |
| 1385 | } |
| 1386 | else if (s->si[1].state == SI_ST_QUE) |
| 1387 | s->flags |= SN_FINST_Q; |
| 1388 | else if (s->si[1].state < SI_ST_EST) |
| 1389 | s->flags |= SN_FINST_C; |
| 1390 | else if (s->si[1].state == SI_ST_EST) |
| 1391 | s->flags |= SN_FINST_D; |
| 1392 | else |
| 1393 | s->flags |= SN_FINST_L; |
| 1394 | } |
| 1395 | } |
| 1396 | |
| 1397 | /* Handle server-side errors for default protocols. It is called whenever a a |
| 1398 | * connection setup is aborted or a request is aborted in queue. It sets the |
| 1399 | * session termination flags so that the caller does not have to worry about |
| 1400 | * them. It's installed as ->srv_error for the server-side stream_interface. |
| 1401 | */ |
| 1402 | void default_srv_error(struct session *s, struct stream_interface *si) |
| 1403 | { |
| 1404 | int err_type = si->err_type; |
| 1405 | int err = 0, fin = 0; |
| 1406 | |
| 1407 | if (err_type & SI_ET_QUEUE_ABRT) { |
| 1408 | err = SN_ERR_CLICL; |
| 1409 | fin = SN_FINST_Q; |
| 1410 | } |
| 1411 | else if (err_type & SI_ET_CONN_ABRT) { |
| 1412 | err = SN_ERR_CLICL; |
| 1413 | fin = SN_FINST_C; |
| 1414 | } |
| 1415 | else if (err_type & SI_ET_QUEUE_TO) { |
| 1416 | err = SN_ERR_SRVTO; |
| 1417 | fin = SN_FINST_Q; |
| 1418 | } |
| 1419 | else if (err_type & SI_ET_QUEUE_ERR) { |
| 1420 | err = SN_ERR_SRVCL; |
| 1421 | fin = SN_FINST_Q; |
| 1422 | } |
| 1423 | else if (err_type & SI_ET_CONN_TO) { |
| 1424 | err = SN_ERR_SRVTO; |
| 1425 | fin = SN_FINST_C; |
| 1426 | } |
| 1427 | else if (err_type & SI_ET_CONN_ERR) { |
| 1428 | err = SN_ERR_SRVCL; |
| 1429 | fin = SN_FINST_C; |
| 1430 | } |
| 1431 | else /* SI_ET_CONN_OTHER and others */ { |
| 1432 | err = SN_ERR_INTERNAL; |
| 1433 | fin = SN_FINST_C; |
| 1434 | } |
| 1435 | |
| 1436 | if (!(s->flags & SN_ERR_MASK)) |
| 1437 | s->flags |= err; |
| 1438 | if (!(s->flags & SN_FINST_MASK)) |
| 1439 | s->flags |= fin; |
| 1440 | } |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 1441 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1442 | /* |
| 1443 | * Local variables: |
| 1444 | * c-indent-level: 8 |
| 1445 | * c-basic-offset: 8 |
| 1446 | * End: |
| 1447 | */ |