blob: e19bb56300daab4522229d2e246d99ea6865ba24 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Server management functions.
3 *
Willy Tarreau7c669d72008-06-20 15:04:11 +02004 * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
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 Tarreaue3ba5f02006-06-29 18:54:54 +020014
15#include <common/config.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020016#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020017#include <common/memory.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020018
Willy Tarreaubaaee002006-06-26 02:48:02 +020019#include <types/capture.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010020#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010022#include <proto/backend.h>
Willy Tarreau7341d942007-05-13 19:56:02 +020023#include <proto/buffers.h>
Willy Tarreau8d5d7f22007-01-21 19:16:41 +010024#include <proto/hdr_idx.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020025#include <proto/log.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020026#include <proto/session.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010027#include <proto/proto_http.h>
28#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020029#include <proto/queue.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010030#include <proto/stream_interface.h>
31#include <proto/stream_sock.h>
32#include <proto/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020033
34
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020035struct pool_head *pool2_session;
Willy Tarreauf54f8bd2008-11-23 19:53:55 +010036struct list sessions;
Willy Tarreaubaaee002006-06-26 02:48:02 +020037
38/*
39 * frees the context associated to a session. It must have been removed first.
40 */
41void session_free(struct session *s)
42{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +010043 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +020044 struct proxy *fe = s->fe;
Willy Tarreau0f7562b2007-01-07 15:46:13 +010045
Willy Tarreaubaaee002006-06-26 02:48:02 +020046 if (s->pend_pos)
47 pendconn_free(s->pend_pos);
Willy Tarreau1e62de62008-11-11 20:20:02 +010048 if (s->srv) { /* there may be requests left pending in queue */
49 if (s->flags & SN_CURR_SESS) {
50 s->flags &= ~SN_CURR_SESS;
51 s->srv->cur_sess--;
52 }
Willy Tarreau7c669d72008-06-20 15:04:11 +020053 process_srv_queue(s->srv);
Willy Tarreau1e62de62008-11-11 20:20:02 +010054 }
Willy Tarreau7c669d72008-06-20 15:04:11 +020055 if (unlikely(s->srv_conn)) {
56 /* the session still has a reserved slot on a server, but
57 * it should normally be only the same as the one above,
58 * so this should not happen in fact.
59 */
60 sess_change_server(s, NULL);
61 }
62
Willy Tarreau48d63db2008-08-03 17:41:33 +020063 pool_free2(pool2_buffer, s->req);
64 pool_free2(pool2_buffer, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +020065
Willy Tarreau92fb9832007-10-16 17:34:28 +020066 if (fe) {
Willy Tarreau48d63db2008-08-03 17:41:33 +020067 pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau41dff822007-01-01 23:32:30 +010068
Willy Tarreau92fb9832007-10-16 17:34:28 +020069 if (txn->rsp.cap != NULL) {
70 struct cap_hdr *h;
Willy Tarreau48d63db2008-08-03 17:41:33 +020071 for (h = fe->rsp_cap; h; h = h->next)
72 pool_free2(h->pool, txn->rsp.cap[h->index]);
Willy Tarreau92fb9832007-10-16 17:34:28 +020073 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +020074 }
Willy Tarreau92fb9832007-10-16 17:34:28 +020075 if (txn->req.cap != NULL) {
76 struct cap_hdr *h;
Willy Tarreau48d63db2008-08-03 17:41:33 +020077 for (h = fe->req_cap; h; h = h->next)
78 pool_free2(h->pool, txn->req.cap[h->index]);
Willy Tarreau92fb9832007-10-16 17:34:28 +020079 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +020080 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020081 }
Willy Tarreau48d63db2008-08-03 17:41:33 +020082 pool_free2(pool2_requri, txn->uri);
83 pool_free2(pool2_capture, txn->cli_cookie);
84 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreauf54f8bd2008-11-23 19:53:55 +010085 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020086 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +020087
88 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +020089 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +020090 pool_flush2(pool2_buffer);
91 pool_flush2(fe->hdr_idx_pool);
92 pool_flush2(pool2_requri);
93 pool_flush2(pool2_capture);
94 pool_flush2(pool2_session);
95 pool_flush2(fe->req_cap_pool);
96 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +020097 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020098}
99
100
101/* perform minimal intializations, report 0 in case of error, 1 if OK. */
102int init_session()
103{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100104 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200105 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
106 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200107}
108
Willy Tarreau30e71012007-11-26 20:15:35 +0100109void session_process_counters(struct session *s)
110{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100111 unsigned long long bytes;
112
Willy Tarreau30e71012007-11-26 20:15:35 +0100113 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100114 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100115 s->logs.bytes_in = s->req->total;
116 if (bytes) {
117 s->fe->bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100118
Willy Tarreau30e71012007-11-26 20:15:35 +0100119 if (s->be != s->fe)
120 s->be->bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100121
Willy Tarreau30e71012007-11-26 20:15:35 +0100122 if (s->srv)
123 s->srv->bytes_in += bytes;
124 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100125 }
126
Willy Tarreau30e71012007-11-26 20:15:35 +0100127 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100128 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100129 s->logs.bytes_out = s->rep->total;
130 if (bytes) {
131 s->fe->bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100132
Willy Tarreau30e71012007-11-26 20:15:35 +0100133 if (s->be != s->fe)
134 s->be->bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100135
Willy Tarreau30e71012007-11-26 20:15:35 +0100136 if (s->srv)
137 s->srv->bytes_out += bytes;
138 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100139 }
140}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200141
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100142/* This function is called with (si->state == SI_ST_CON) meaning that a
143 * connection was attempted and that the file descriptor is already allocated.
144 * We must check for establishment, error and abort. Possible output states
145 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
146 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
147 * otherwise 1.
148 */
149int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
150{
151 struct buffer *req = si->ob;
152 struct buffer *rep = si->ib;
153
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100154 /* If we got an error, or if nothing happened and the connection timed
155 * out, we must give up. The CER state handler will take care of retry
156 * attempts and error reports.
157 */
158 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
159 si->state = SI_ST_CER;
160 fd_delete(si->fd);
161
162 if (si->err_type)
163 return 0;
164
165 si->err_loc = s->srv;
166 if (si->flags & SI_FL_ERR)
167 si->err_type = SI_ET_CONN_ERR;
168 else
169 si->err_type = SI_ET_CONN_TO;
170 return 0;
171 }
172
173 /* OK, maybe we want to abort */
174 if (unlikely((req->flags & BF_SHUTW_NOW) ||
175 (rep->flags & BF_SHUTW) ||
176 ((req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
177 ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_ACTIVITY)) ||
178 s->be->options & PR_O_ABRT_CLOSE)))) {
179 /* give up */
180 si->shutw(si);
181 si->err_type |= SI_ET_CONN_ABRT;
182 si->err_loc = s->srv;
183 return 1;
184 }
185
186 /* we need to wait a bit more if there was no activity either */
187 if (!(req->flags & BF_WRITE_ACTIVITY))
188 return 1;
189
190 /* OK, this means that a connection succeeded. The caller will be
191 * responsible for handling the transition from CON to EST.
192 */
193 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
194 si->state = SI_ST_EST;
195 si->err_type = SI_ET_NONE;
196 si->err_loc = NULL;
197 return 1;
198}
199
200/* This function is called with (si->state == SI_ST_CER) meaning that a
201 * previous connection attempt has failed and that the file descriptor
202 * has already been released. Possible causes include asynchronous error
203 * notification and time out. Possible output states are SI_ST_CLO when
204 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
205 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
206 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
207 * marked as in error state. It returns 0.
208 */
209int sess_update_st_cer(struct session *s, struct stream_interface *si)
210{
211 /* we probably have to release last session from the server */
212 if (s->srv) {
213 if (s->flags & SN_CURR_SESS) {
214 s->flags &= ~SN_CURR_SESS;
215 s->srv->cur_sess--;
216 }
217 sess_change_server(s, NULL);
218 }
219
220 /* ensure that we have enough retries left */
221 s->conn_retries--;
222 if (s->conn_retries < 0) {
223 if (!si->err_type) {
224 si->err_type = SI_ET_CONN_ERR;
225 si->err_loc = s->srv;
226 }
227
228 if (s->srv)
229 s->srv->failed_conns++;
230 s->be->failed_conns++;
231 if (may_dequeue_tasks(s->srv, s->be))
232 process_srv_queue(s->srv);
233
234 /* shutw is enough so stop a connecting socket */
235 si->shutw(si);
236 si->ob->flags |= BF_WRITE_ERROR;
237 si->ib->flags |= BF_READ_ERROR;
238
239 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100240 if (s->srv_error)
241 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100242 return 0;
243 }
244
245 /* If the "redispatch" option is set on the backend, we are allowed to
246 * retry on another server for the last retry. In order to achieve this,
247 * we must mark the session unassigned, and eventually clear the DIRECT
248 * bit to ignore any persistence cookie. We won't count a retry nor a
249 * redispatch yet, because this will depend on what server is selected.
250 */
251 if (s->srv && s->conn_retries == 0 && s->be->options & PR_O_REDISP) {
252 if (may_dequeue_tasks(s->srv, s->be))
253 process_srv_queue(s->srv);
254
255 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
256 s->prev_srv = s->srv;
257 si->state = SI_ST_REQ;
258 } else {
259 if (s->srv)
260 s->srv->retries++;
261 s->be->retries++;
262 si->state = SI_ST_ASS;
263 }
264
265 if (si->flags & SI_FL_ERR) {
266 /* The error was an asynchronous connection error, and we will
267 * likely have to retry connecting to the same server, most
268 * likely leading to the same result. To avoid this, we wait
269 * one second before retrying.
270 */
271
272 if (!si->err_type)
273 si->err_type = SI_ET_CONN_ERR;
274
275 si->state = SI_ST_TAR;
276 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
277 return 0;
278 }
279 return 0;
280}
281
282/*
283 * This function handles the transition between the SI_ST_CON state and the
284 * SI_ST_EST state. It must only be called after switching from SI_ST_CON to
285 * SI_ST_EST.
286 */
287void sess_establish(struct session *s, struct stream_interface *si)
288{
289 struct buffer *req = si->ob;
290 struct buffer *rep = si->ib;
291
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100292 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100293 buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */
294
295 /* if the user wants to log as soon as possible, without counting
296 * bytes from the server, then this is the right moment. */
297 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
298 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100299 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100300 }
301#ifdef CONFIG_HAP_TCPSPLICE
302 if ((s->fe->options & s->be->options) & PR_O_TCPSPLICE) {
303 /* TCP splicing supported by both FE and BE */
304 tcp_splice_splicefd(req->prod->fd, si->fd, 0);
305 }
306#endif
307 }
308 else {
309 rep->analysers |= AN_RTR_HTTP_HDR;
310 buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */
311 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
312 /* reset hdr_idx which was already initialized by the request.
313 * right now, the http parser does it.
314 * hdr_idx_init(&s->txn.hdr_idx);
315 */
316 }
317
318 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
319 req->wex = TICK_ETERNITY;
320}
321
322/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
323 * Other input states are simply ignored.
324 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
325 * Flags must have previously been updated for timeouts and other conditions.
326 */
327void sess_update_stream_int(struct session *s, struct stream_interface *si)
328{
329 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",
330 now_ms, __FUNCTION__,
331 s,
332 s->req, s->rep,
333 s->req->rex, s->rep->wex,
334 s->req->flags, s->rep->flags,
335 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
336
337 if (si->state == SI_ST_ASS) {
338 /* Server assigned to connection request, we have to try to connect now */
339 int conn_err;
340
341 conn_err = connect_server(s);
342 if (conn_err == SN_ERR_NONE) {
343 /* state = SI_ST_CON now */
Willy Tarreau8f6457c2008-12-01 00:08:28 +0100344 if (s->srv)
345 s->srv->cum_sess++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100346 return;
347 }
348
349 /* We have received a synchronous error. We might have to
350 * abort, retry immediately or redispatch.
351 */
352 if (conn_err == SN_ERR_INTERNAL) {
353 if (!si->err_type) {
354 si->err_type = SI_ET_CONN_OTHER;
355 si->err_loc = s->srv;
356 }
357
358 if (s->srv)
359 s->srv->cum_sess++;
360 if (s->srv)
361 s->srv->failed_conns++;
362 s->be->failed_conns++;
363
364 /* release other sessions waiting for this server */
365 if (may_dequeue_tasks(s->srv, s->be))
366 process_srv_queue(s->srv);
367
368 /* Failed and not retryable. */
369 si->shutr(si);
370 si->shutw(si);
371 si->ob->flags |= BF_WRITE_ERROR;
372
373 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
374
375 /* no session was ever accounted for this server */
376 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100377 if (s->srv_error)
378 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100379 return;
380 }
381
382 /* We are facing a retryable error, but we don't want to run a
383 * turn-around now, as the problem is likely a source port
384 * allocation problem, so we want to retry now.
385 */
386 si->state = SI_ST_CER;
387 si->flags &= ~SI_FL_ERR;
388 sess_update_st_cer(s, si);
389 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
390 return;
391 }
392 else if (si->state == SI_ST_QUE) {
393 /* connection request was queued, check for any update */
394 if (!s->pend_pos) {
395 /* The connection is not in the queue anymore. Either
396 * we have a server connection slot available and we
397 * go directly to the assigned state, or we need to
398 * load-balance first and go to the INI state.
399 */
400 si->exp = TICK_ETERNITY;
401 if (unlikely(!(s->flags & SN_ASSIGNED)))
402 si->state = SI_ST_REQ;
403 else {
404 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
405 si->state = SI_ST_ASS;
406 }
407 return;
408 }
409
410 /* Connection request still in queue... */
411 if (si->flags & SI_FL_EXP) {
412 /* ... and timeout expired */
413 si->exp = TICK_ETERNITY;
414 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
415 if (s->srv)
416 s->srv->failed_conns++;
417 s->be->failed_conns++;
418 si->shutr(si);
419 si->shutw(si);
420 si->ob->flags |= BF_WRITE_TIMEOUT;
421 if (!si->err_type)
422 si->err_type = SI_ET_QUEUE_TO;
423 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100424 if (s->srv_error)
425 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100426 return;
427 }
428
429 /* Connection remains in queue, check if we have to abort it */
430 if ((si->ob->flags & (BF_READ_ERROR|BF_SHUTW_NOW)) || /* abort requested */
431 ((si->ob->flags & BF_SHUTR) && /* empty and client stopped */
432 (si->ob->flags & BF_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
433 /* give up */
434 si->exp = TICK_ETERNITY;
435 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
436 si->shutr(si);
437 si->shutw(si);
438 si->err_type |= SI_ET_QUEUE_ABRT;
439 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100440 if (s->srv_error)
441 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100442 return;
443 }
444
445 /* Nothing changed */
446 return;
447 }
448 else if (si->state == SI_ST_TAR) {
449 /* Connection request might be aborted */
450 if ((si->ob->flags & (BF_READ_ERROR|BF_SHUTW_NOW)) || /* abort requested */
451 ((si->ob->flags & BF_SHUTR) && /* empty and client stopped */
452 (si->ob->flags & BF_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
453 /* give up */
454 si->exp = TICK_ETERNITY;
455 si->shutr(si);
456 si->shutw(si);
457 si->err_type |= SI_ET_CONN_ABRT;
458 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100459 if (s->srv_error)
460 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100461 return;
462 }
463
464 if (!(si->flags & SI_FL_EXP))
465 return; /* still in turn-around */
466
467 si->exp = TICK_ETERNITY;
468
469 /* we keep trying on the same server as long as the session is
470 * marked "assigned".
471 * FIXME: Should we force a redispatch attempt when the server is down ?
472 */
473 if (s->flags & SN_ASSIGNED)
474 si->state = SI_ST_ASS;
475 else
476 si->state = SI_ST_REQ;
477 return;
478 }
479}
480
481/* This function initiates a server connection request on a stream interface
482 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
483 * indicating that a server has been assigned. It may also return SI_ST_QUE,
484 * or SI_ST_CLO upon error.
485 */
486static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
487 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",
488 now_ms, __FUNCTION__,
489 s,
490 s->req, s->rep,
491 s->req->rex, s->rep->wex,
492 s->req->flags, s->rep->flags,
493 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
494
495 if (si->state != SI_ST_REQ)
496 return;
497
498 /* Try to assign a server */
499 if (srv_redispatch_connect(s) != 0) {
500 /* We did not get a server. Either we queued the
501 * connection request, or we encountered an error.
502 */
503 if (si->state == SI_ST_QUE)
504 return;
505
506 /* we did not get any server, let's check the cause */
507 si->shutr(si);
508 si->shutw(si);
509 si->ob->flags |= BF_WRITE_ERROR;
510 if (!si->err_type)
511 si->err_type = SI_ET_CONN_OTHER;
512 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100513 if (s->srv_error)
514 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100515 return;
516 }
517
518 /* The server is assigned */
519 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
520 si->state = SI_ST_ASS;
521}
522
523/* Processes the client, server, request and response jobs of a session task,
524 * then puts it back to the wait queue in a clean state, or cleans up its
525 * resources if it must be deleted. Returns in <next> the date the task wants
526 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
527 * nothing too many times, the request and response buffers flags are monitored
528 * and each function is called only if at least another function has changed at
529 * least one flag it is interested in.
530 */
531void process_session(struct task *t, int *next)
532{
533 struct session *s = t->context;
534 int resync;
535 unsigned int rqf_last, rpf_last;
536
537 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
538 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
539
540 /* 1a: Check for low level timeouts if needed. We just set a flag on
541 * stream interfaces when their timeouts have expired.
542 */
543 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
544 stream_int_check_timeouts(&s->si[0]);
545 stream_int_check_timeouts(&s->si[1]);
546 buffer_check_timeouts(s->req);
547 buffer_check_timeouts(s->rep);
548 }
549
550 /* copy req/rep flags so that we can detect shutdowns */
551 rqf_last = s->req->flags;
552 rpf_last = s->rep->flags;
553
554 /* 1b: check for low-level errors reported at the stream interface.
555 * First we check if it's a retryable error (in which case we don't
556 * want to tell the buffer). Otherwise we report the error one level
557 * upper by setting flags into the buffers. Note that the side towards
558 * the client cannot have connect (hence retryable) errors. Also, the
559 * connection setup code must be able to deal with any type of abort.
560 */
561 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
562 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
563 s->si[0].shutr(&s->si[0]);
564 s->si[0].shutw(&s->si[0]);
565 stream_int_report_error(&s->si[0]);
566 }
567 }
568
569 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
570 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
571 s->si[1].shutr(&s->si[1]);
572 s->si[1].shutw(&s->si[1]);
573 stream_int_report_error(&s->si[1]);
574 s->be->failed_resp++;
575 if (s->srv)
576 s->srv->failed_resp++;
577 }
578 /* note: maybe we should process connection errors here ? */
579 }
580
581 if (s->si[1].state == SI_ST_CON) {
582 /* we were trying to establish a connection on the server side,
583 * maybe it succeeded, maybe it failed, maybe we timed out, ...
584 */
585 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
586 sess_update_st_cer(s, &s->si[1]);
587 else if (s->si[1].state == SI_ST_EST)
588 sess_establish(s, &s->si[1]);
589
590 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
591 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
592 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
593 */
594 }
595
596 /* check buffer timeouts, and close the corresponding stream interfaces
597 * for future reads or writes. Note: this will also concern upper layers
598 * but we do not touch any other flag. We must be careful and correctly
599 * detect state changes when calling them.
600 */
601 if (unlikely(s->req->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) {
602 if (s->req->flags & BF_READ_TIMEOUT)
603 s->req->prod->shutr(s->req->prod);
604 if (s->req->flags & BF_WRITE_TIMEOUT)
605 s->req->cons->shutw(s->req->cons);
606 DPRINTF(stderr,
607 "[%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",
608 now_ms, __FUNCTION__, __LINE__,
609 t,
610 s, s->flags,
611 s->req, s->rep,
612 s->req->rex, s->rep->wex,
613 s->req->flags, s->rep->flags,
614 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
615 s->rep->cons->err_type, s->req->cons->err_type,
616 s->conn_retries);
617 }
618
619 if (unlikely(s->rep->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) {
620 if (s->rep->flags & BF_READ_TIMEOUT)
621 s->rep->prod->shutr(s->rep->prod);
622 if (s->rep->flags & BF_WRITE_TIMEOUT)
623 s->rep->cons->shutw(s->rep->cons);
624 DPRINTF(stderr,
625 "[%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",
626 now_ms, __FUNCTION__, __LINE__,
627 t,
628 s, s->flags,
629 s->req, s->rep,
630 s->req->rex, s->rep->wex,
631 s->req->flags, s->rep->flags,
632 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
633 s->rep->cons->err_type, s->req->cons->err_type,
634 s->conn_retries);
635 }
636
637 /* Check for connection closure */
638
639resync_stream_interface:
640 DPRINTF(stderr,
641 "[%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",
642 now_ms, __FUNCTION__, __LINE__,
643 t,
644 s, s->flags,
645 s->req, s->rep,
646 s->req->rex, s->rep->wex,
647 s->req->flags, s->rep->flags,
648 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
649 s->rep->cons->err_type, s->req->cons->err_type,
650 s->conn_retries);
651
652 /* nothing special to be done on client side */
653 if (unlikely(s->req->prod->state == SI_ST_DIS))
654 s->req->prod->state = SI_ST_CLO;
655
656 /* When a server-side connection is released, we have to count it and
657 * check for pending connections on this server.
658 */
659 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
660 s->req->cons->state = SI_ST_CLO;
661 if (s->srv) {
662 if (s->flags & SN_CURR_SESS) {
663 s->flags &= ~SN_CURR_SESS;
664 s->srv->cur_sess--;
665 }
666 sess_change_server(s, NULL);
667 if (may_dequeue_tasks(s->srv, s->be))
668 process_srv_queue(s->srv);
669 }
670 }
671
672 /*
673 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
674 * at this point.
675 */
676
677 /**** Process layer 7 below ****/
678
679 resync = 0;
680
681 /* Analyse request */
682 if ((s->req->flags & BF_MASK_ANALYSER) ||
683 (s->req->flags ^ rqf_last) & BF_MASK_STATIC) {
684 unsigned int flags = s->req->flags;
685
686 if (s->req->prod->state >= SI_ST_EST) {
687 /* it's up to the analysers to reset write_ena */
688 buffer_write_ena(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100689
690 /* We will call all analysers for which a bit is set in
691 * s->req->analysers, following the bit order from LSB
692 * to MSB. The analysers must remove themselves from
693 * the list when not needed. This while() loop is in
694 * fact a cleaner if().
695 */
696 while (s->req->analysers) {
697 if (s->req->analysers & AN_REQ_INSPECT)
698 if (!tcp_inspect_request(s, s->req))
699 break;
700
Willy Tarreau59234e92008-11-30 23:51:27 +0100701 if (s->req->analysers & AN_REQ_HTTP_HDR)
702 if (!http_process_request(s, s->req))
Willy Tarreauedcf6682008-11-30 23:15:34 +0100703 break;
704
Willy Tarreau60b85b02008-11-30 23:28:40 +0100705 if (s->req->analysers & AN_REQ_HTTP_TARPIT)
706 if (!http_process_tarpit(s, s->req))
707 break;
708
Willy Tarreaud34af782008-11-30 23:36:37 +0100709 if (s->req->analysers & AN_REQ_HTTP_BODY)
710 if (!http_process_request_body(s, s->req))
711 break;
712
Willy Tarreauedcf6682008-11-30 23:15:34 +0100713 /* Just make sure that nobody set a wrong flag causing an endless loop */
714 s->req->analysers &= AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_TARPIT | AN_REQ_HTTP_BODY;
715
716 /* we don't want to loop anyway */
717 break;
718 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100719 }
720 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
721 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
722 if (s->req->flags != flags)
723 resync = 1;
724 }
725
726 /* reflect what the L7 analysers have seen last */
727 rqf_last = s->req->flags;
728
729 /*
730 * Now forward all shutdown requests between both sides of the buffer
731 */
732
733 /* first, let's check if the request buffer needs to shutdown(write) */
734 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
735 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)))
736 buffer_shutw_now(s->req);
737 else if ((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA) &&
738 (s->req->cons->state == SI_ST_EST) &&
739 s->be->options & PR_O_FORCE_CLO &&
740 s->rep->flags & BF_READ_ACTIVITY) {
741 /* We want to force the connection to the server to close,
742 * and the server has begun to respond. That's the right
743 * time.
744 */
745 buffer_shutw_now(s->req);
746 }
747
748 /* shutdown(write) pending */
749 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW))
750 s->req->cons->shutw(s->req->cons);
751
752 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +0100753 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
754 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100755 buffer_shutr_now(s->req);
756
757 /* shutdown(read) pending */
758 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
759 s->req->prod->shutr(s->req->prod);
760
761 /* it's possible that an upper layer has requested a connection setup */
762 if (s->req->cons->state == SI_ST_INI &&
763 (s->req->flags & (BF_WRITE_ENA|BF_SHUTW|BF_SHUTW_NOW)) == BF_WRITE_ENA)
764 s->req->cons->state = SI_ST_REQ;
765
766 /* we may have a pending connection request, or a connection waiting
767 * for completion.
768 */
769 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
770 do {
771 /* nb: step 1 might switch from QUE to ASS, but we first want
772 * to give a chance to step 2 to perform a redirect if needed.
773 */
774 if (s->si[1].state != SI_ST_REQ)
775 sess_update_stream_int(s, &s->si[1]);
776 if (s->si[1].state == SI_ST_REQ)
777 sess_prepare_conn_req(s, &s->si[1]);
778
779 if (s->si[1].state == SI_ST_ASS && s->srv &&
780 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
781 perform_http_redirect(s, &s->si[1]);
782 } while (s->si[1].state == SI_ST_ASS);
783 }
784
785 /*
786 * Here we want to check if we need to resync or not.
787 */
788 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
789 resync = 1;
790
791 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
792
793 /* according to benchmarks, it makes sense to resync now */
794 if (resync)
795 goto resync_stream_interface;
796
797
798 /* Analyse response */
799
800 if (unlikely(s->rep->flags & BF_HIJACK)) {
801 /* In inject mode, we wake up everytime something has
802 * happened on the write side of the buffer.
803 */
804 unsigned int flags = s->rep->flags;
805
806 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
807 !(s->rep->flags & BF_FULL)) {
808 produce_content(s);
809 }
810 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
811 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
812 if (s->rep->flags != flags)
813 resync = 1;
814 }
815 else if ((s->rep->flags & BF_MASK_ANALYSER) ||
816 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC) {
817 unsigned int flags = s->rep->flags;
818
819 if (s->rep->prod->state >= SI_ST_EST) {
820 /* it's up to the analysers to reset write_ena */
821 buffer_write_ena(s->rep);
822 if (s->rep->analysers)
823 process_response(s);
824 }
825 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
826 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
827 if (s->rep->flags != flags)
828 resync = 1;
829 }
830
831 /* reflect what the L7 analysers have seen last */
832 rpf_last = s->rep->flags;
833
834 /*
835 * Now forward all shutdown requests between both sides of the buffer
836 */
837
838 /*
839 * FIXME: this is probably where we should produce error responses.
840 */
841
842 /* first, let's check if the request buffer needs to shutdown(write) */
843 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
844 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)))
845 buffer_shutw_now(s->rep);
846
847 /* shutdown(write) pending */
848 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW))
849 s->rep->cons->shutw(s->rep->cons);
850
851 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +0100852 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
853 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100854 buffer_shutr_now(s->rep);
855
856 /* shutdown(read) pending */
857 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
858 s->rep->prod->shutr(s->rep->prod);
859
860 /*
861 * Here we want to check if we need to resync or not.
862 */
863 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
864 resync = 1;
865
866 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
867
868 if (resync)
869 goto resync_stream_interface;
870
871
872 /* This is needed only when debugging is enabled, to indicate
873 * client-side or server-side close. Please note that in the unlikely
874 * event where both sides would close at once, the sequence is reported
875 * on the server side first.
876 */
877 if (unlikely((global.mode & MODE_DEBUG) &&
878 (!(global.mode & MODE_QUIET) ||
879 (global.mode & MODE_VERBOSE)))) {
880 int len;
881
882 if (s->si[1].state == SI_ST_CLO &&
883 s->si[1].prev_state == SI_ST_EST) {
884 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
885 s->uniq_id, s->be->id,
886 (unsigned short)s->si[0].fd,
887 (unsigned short)s->si[1].fd);
888 write(1, trash, len);
889 }
890
891 if (s->si[0].state == SI_ST_CLO &&
892 s->si[0].prev_state == SI_ST_EST) {
893 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
894 s->uniq_id, s->be->id,
895 (unsigned short)s->si[0].fd,
896 (unsigned short)s->si[1].fd);
897 write(1, trash, len);
898 }
899 }
900
901 if (likely((s->rep->cons->state != SI_ST_CLO) ||
902 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
903
904 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
905 session_process_counters(s);
906
907 if (s->rep->cons->state == SI_ST_EST)
Willy Tarreaub0253252008-11-30 21:37:12 +0100908 stream_sock_data_finish(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100909
910 if (s->req->cons->state == SI_ST_EST)
Willy Tarreaub0253252008-11-30 21:37:12 +0100911 stream_sock_data_finish(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100912
913 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
914 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
915 s->si[0].prev_state = s->si[0].state;
916 s->si[1].prev_state = s->si[1].state;
917 s->si[0].flags = s->si[1].flags = SI_FL_NONE;
918
919 /* Trick: if a request is being waiting for the server to respond,
920 * and if we know the server can timeout, we don't want the timeout
921 * to expire on the client side first, but we're still interested
922 * in passing data from the client to the server (eg: POST). Thus,
923 * we can cancel the client's request timeout if the server's
924 * request timeout is set and the server has not yet sent a response.
925 */
926
927 if ((s->rep->flags & (BF_WRITE_ENA|BF_SHUTR)) == 0 &&
928 (tick_isset(s->req->wex) || tick_isset(s->rep->rex)))
929 s->req->rex = TICK_ETERNITY;
930
931 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
932 tick_first(s->rep->rex, s->rep->wex));
933 if (s->req->analysers)
934 t->expire = tick_first(t->expire, s->req->analyse_exp);
935
936 if (s->si[0].exp)
937 t->expire = tick_first(t->expire, s->si[0].exp);
938
939 if (s->si[1].exp)
940 t->expire = tick_first(t->expire, s->si[1].exp);
941
942#ifdef DEBUG_FULL
943 fprintf(stderr, "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u rep->rex=%u rep->wex=%u, cs=%d, ss=%d\n",
944 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp, s->rep->rex, s->rep->wex, s->si[0].state, s->si[1].state);
945#endif
946 /* restore t to its place in the task list */
947 task_queue(t);
948
949#ifdef DEBUG_DEV
950 /* this may only happen when no timeout is set or in case of an FSM bug */
951 if (!t->expire)
952 ABORT_NOW();
953#endif
954 *next = t->expire;
955 return; /* nothing more to do */
956 }
957
958 s->fe->feconn--;
959 if (s->flags & SN_BE_ASSIGNED)
960 s->be->beconn--;
961 actconn--;
962
963 if (unlikely((global.mode & MODE_DEBUG) &&
964 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
965 int len;
966 len = sprintf(trash, "%08x:%s.closed[%04x:%04x] (term_trace=0x%08x)\n",
967 s->uniq_id, s->be->id,
968 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd,
969 s->term_trace);
970 write(1, trash, len);
971 }
972
973 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
974 session_process_counters(s);
975
976 /* let's do a final log if we need it */
977 if (s->logs.logwait &&
978 !(s->flags & SN_MONITOR) &&
979 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100980 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100981 }
982
983 /* the task MUST not be in the run queue anymore */
984 task_delete(t);
985 session_free(s);
986 task_free(t);
987 *next = TICK_ETERNITY;
988}
989
Willy Tarreau7c669d72008-06-20 15:04:11 +0200990/*
991 * This function adjusts sess->srv_conn and maintains the previous and new
992 * server's served session counts. Setting newsrv to NULL is enough to release
993 * current connection slot. This function also notifies any LB algo which might
994 * expect to be informed about any change in the number of active sessions on a
995 * server.
996 */
997void sess_change_server(struct session *sess, struct server *newsrv)
998{
999 if (sess->srv_conn == newsrv)
1000 return;
1001
1002 if (sess->srv_conn) {
1003 sess->srv_conn->served--;
1004 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
1005 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
1006 sess->srv_conn = NULL;
1007 }
1008
1009 if (newsrv) {
1010 newsrv->served++;
1011 if (newsrv->proxy->lbprm.server_take_conn)
1012 newsrv->proxy->lbprm.server_take_conn(newsrv);
1013 sess->srv_conn = newsrv;
1014 }
1015}
1016
1017
Willy Tarreaubaaee002006-06-26 02:48:02 +02001018/*
1019 * Local variables:
1020 * c-indent-level: 8
1021 * c-basic-offset: 8
1022 * End:
1023 */