blob: 1eb7b69fc5e43d0655273f6ad0ef7f6a8f9f7663 [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
154 DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d, fds=%d\n",
155 now_ms, __FUNCTION__,
156 cli_stnames[s->cli_state],
157 rep->rex, req->wex,
158 req->flags, rep->flags,
159 req->l, rep->l,
160 fdtab[si->fd].state);
161
162
163 /* If we got an error, or if nothing happened and the connection timed
164 * out, we must give up. The CER state handler will take care of retry
165 * attempts and error reports.
166 */
167 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
168 si->state = SI_ST_CER;
169 fd_delete(si->fd);
170
171 if (si->err_type)
172 return 0;
173
174 si->err_loc = s->srv;
175 if (si->flags & SI_FL_ERR)
176 si->err_type = SI_ET_CONN_ERR;
177 else
178 si->err_type = SI_ET_CONN_TO;
179 return 0;
180 }
181
182 /* OK, maybe we want to abort */
183 if (unlikely((req->flags & BF_SHUTW_NOW) ||
184 (rep->flags & BF_SHUTW) ||
185 ((req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
186 ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_ACTIVITY)) ||
187 s->be->options & PR_O_ABRT_CLOSE)))) {
188 /* give up */
189 si->shutw(si);
190 si->err_type |= SI_ET_CONN_ABRT;
191 si->err_loc = s->srv;
192 return 1;
193 }
194
195 /* we need to wait a bit more if there was no activity either */
196 if (!(req->flags & BF_WRITE_ACTIVITY))
197 return 1;
198
199 /* OK, this means that a connection succeeded. The caller will be
200 * responsible for handling the transition from CON to EST.
201 */
202 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
203 si->state = SI_ST_EST;
204 si->err_type = SI_ET_NONE;
205 si->err_loc = NULL;
206 return 1;
207}
208
209/* This function is called with (si->state == SI_ST_CER) meaning that a
210 * previous connection attempt has failed and that the file descriptor
211 * has already been released. Possible causes include asynchronous error
212 * notification and time out. Possible output states are SI_ST_CLO when
213 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
214 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
215 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
216 * marked as in error state. It returns 0.
217 */
218int sess_update_st_cer(struct session *s, struct stream_interface *si)
219{
220 /* we probably have to release last session from the server */
221 if (s->srv) {
222 if (s->flags & SN_CURR_SESS) {
223 s->flags &= ~SN_CURR_SESS;
224 s->srv->cur_sess--;
225 }
226 sess_change_server(s, NULL);
227 }
228
229 /* ensure that we have enough retries left */
230 s->conn_retries--;
231 if (s->conn_retries < 0) {
232 if (!si->err_type) {
233 si->err_type = SI_ET_CONN_ERR;
234 si->err_loc = s->srv;
235 }
236
237 if (s->srv)
238 s->srv->failed_conns++;
239 s->be->failed_conns++;
240 if (may_dequeue_tasks(s->srv, s->be))
241 process_srv_queue(s->srv);
242
243 /* shutw is enough so stop a connecting socket */
244 si->shutw(si);
245 si->ob->flags |= BF_WRITE_ERROR;
246 si->ib->flags |= BF_READ_ERROR;
247
248 si->state = SI_ST_CLO;
249 return_srv_error(s, si->err_type);
250 return 0;
251 }
252
253 /* If the "redispatch" option is set on the backend, we are allowed to
254 * retry on another server for the last retry. In order to achieve this,
255 * we must mark the session unassigned, and eventually clear the DIRECT
256 * bit to ignore any persistence cookie. We won't count a retry nor a
257 * redispatch yet, because this will depend on what server is selected.
258 */
259 if (s->srv && s->conn_retries == 0 && s->be->options & PR_O_REDISP) {
260 if (may_dequeue_tasks(s->srv, s->be))
261 process_srv_queue(s->srv);
262
263 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
264 s->prev_srv = s->srv;
265 si->state = SI_ST_REQ;
266 } else {
267 if (s->srv)
268 s->srv->retries++;
269 s->be->retries++;
270 si->state = SI_ST_ASS;
271 }
272
273 if (si->flags & SI_FL_ERR) {
274 /* The error was an asynchronous connection error, and we will
275 * likely have to retry connecting to the same server, most
276 * likely leading to the same result. To avoid this, we wait
277 * one second before retrying.
278 */
279
280 if (!si->err_type)
281 si->err_type = SI_ET_CONN_ERR;
282
283 si->state = SI_ST_TAR;
284 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
285 return 0;
286 }
287 return 0;
288}
289
290/*
291 * This function handles the transition between the SI_ST_CON state and the
292 * SI_ST_EST state. It must only be called after switching from SI_ST_CON to
293 * SI_ST_EST.
294 */
295void sess_establish(struct session *s, struct stream_interface *si)
296{
297 struct buffer *req = si->ob;
298 struct buffer *rep = si->ib;
299
300 if (req->flags & BF_EMPTY) {
301 EV_FD_CLR(si->fd, DIR_WR);
302 req->wex = TICK_ETERNITY;
303 } else {
304 EV_FD_SET(si->fd, DIR_WR);
305 req->wex = tick_add_ifset(now_ms, s->be->timeout.server);
306 if (tick_isset(req->wex)) {
307 /* FIXME: to prevent the server from expiring read
308 * timeouts during writes, we refresh it. */
309 rep->rex = req->wex;
310 }
311 }
312
313 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
314 if (!(rep->flags & BF_HIJACK)) {
315 EV_FD_SET(si->fd, DIR_RD);
316 rep->rex = tick_add_ifset(now_ms, s->be->timeout.server);
317 }
318 buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */
319
320 /* if the user wants to log as soon as possible, without counting
321 * bytes from the server, then this is the right moment. */
322 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
323 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100324 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100325 }
326#ifdef CONFIG_HAP_TCPSPLICE
327 if ((s->fe->options & s->be->options) & PR_O_TCPSPLICE) {
328 /* TCP splicing supported by both FE and BE */
329 tcp_splice_splicefd(req->prod->fd, si->fd, 0);
330 }
331#endif
332 }
333 else {
334 rep->analysers |= AN_RTR_HTTP_HDR;
335 buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */
336 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
337 /* reset hdr_idx which was already initialized by the request.
338 * right now, the http parser does it.
339 * hdr_idx_init(&s->txn.hdr_idx);
340 */
341 }
342
343 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
344 req->wex = TICK_ETERNITY;
345}
346
347/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
348 * Other input states are simply ignored.
349 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
350 * Flags must have previously been updated for timeouts and other conditions.
351 */
352void sess_update_stream_int(struct session *s, struct stream_interface *si)
353{
354 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",
355 now_ms, __FUNCTION__,
356 s,
357 s->req, s->rep,
358 s->req->rex, s->rep->wex,
359 s->req->flags, s->rep->flags,
360 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
361
362 if (si->state == SI_ST_ASS) {
363 /* Server assigned to connection request, we have to try to connect now */
364 int conn_err;
365
366 conn_err = connect_server(s);
367 if (conn_err == SN_ERR_NONE) {
368 /* state = SI_ST_CON now */
369 return;
370 }
371
372 /* We have received a synchronous error. We might have to
373 * abort, retry immediately or redispatch.
374 */
375 if (conn_err == SN_ERR_INTERNAL) {
376 if (!si->err_type) {
377 si->err_type = SI_ET_CONN_OTHER;
378 si->err_loc = s->srv;
379 }
380
381 if (s->srv)
382 s->srv->cum_sess++;
383 if (s->srv)
384 s->srv->failed_conns++;
385 s->be->failed_conns++;
386
387 /* release other sessions waiting for this server */
388 if (may_dequeue_tasks(s->srv, s->be))
389 process_srv_queue(s->srv);
390
391 /* Failed and not retryable. */
392 si->shutr(si);
393 si->shutw(si);
394 si->ob->flags |= BF_WRITE_ERROR;
395
396 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
397
398 /* no session was ever accounted for this server */
399 si->state = SI_ST_CLO;
400 return_srv_error(s, si->err_type);
401 return;
402 }
403
404 /* We are facing a retryable error, but we don't want to run a
405 * turn-around now, as the problem is likely a source port
406 * allocation problem, so we want to retry now.
407 */
408 si->state = SI_ST_CER;
409 si->flags &= ~SI_FL_ERR;
410 sess_update_st_cer(s, si);
411 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
412 return;
413 }
414 else if (si->state == SI_ST_QUE) {
415 /* connection request was queued, check for any update */
416 if (!s->pend_pos) {
417 /* The connection is not in the queue anymore. Either
418 * we have a server connection slot available and we
419 * go directly to the assigned state, or we need to
420 * load-balance first and go to the INI state.
421 */
422 si->exp = TICK_ETERNITY;
423 if (unlikely(!(s->flags & SN_ASSIGNED)))
424 si->state = SI_ST_REQ;
425 else {
426 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
427 si->state = SI_ST_ASS;
428 }
429 return;
430 }
431
432 /* Connection request still in queue... */
433 if (si->flags & SI_FL_EXP) {
434 /* ... and timeout expired */
435 si->exp = TICK_ETERNITY;
436 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
437 if (s->srv)
438 s->srv->failed_conns++;
439 s->be->failed_conns++;
440 si->shutr(si);
441 si->shutw(si);
442 si->ob->flags |= BF_WRITE_TIMEOUT;
443 if (!si->err_type)
444 si->err_type = SI_ET_QUEUE_TO;
445 si->state = SI_ST_CLO;
446 return_srv_error(s, si->err_type);
447 return;
448 }
449
450 /* Connection remains in queue, check if we have to abort it */
451 if ((si->ob->flags & (BF_READ_ERROR|BF_SHUTW_NOW)) || /* abort requested */
452 ((si->ob->flags & BF_SHUTR) && /* empty and client stopped */
453 (si->ob->flags & BF_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
454 /* give up */
455 si->exp = TICK_ETERNITY;
456 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
457 si->shutr(si);
458 si->shutw(si);
459 si->err_type |= SI_ET_QUEUE_ABRT;
460 si->state = SI_ST_CLO;
461 return_srv_error(s, si->err_type);
462 return;
463 }
464
465 /* Nothing changed */
466 return;
467 }
468 else if (si->state == SI_ST_TAR) {
469 /* Connection request might be aborted */
470 if ((si->ob->flags & (BF_READ_ERROR|BF_SHUTW_NOW)) || /* abort requested */
471 ((si->ob->flags & BF_SHUTR) && /* empty and client stopped */
472 (si->ob->flags & BF_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
473 /* give up */
474 si->exp = TICK_ETERNITY;
475 si->shutr(si);
476 si->shutw(si);
477 si->err_type |= SI_ET_CONN_ABRT;
478 si->state = SI_ST_CLO;
479 return_srv_error(s, si->err_type);
480 return;
481 }
482
483 if (!(si->flags & SI_FL_EXP))
484 return; /* still in turn-around */
485
486 si->exp = TICK_ETERNITY;
487
488 /* we keep trying on the same server as long as the session is
489 * marked "assigned".
490 * FIXME: Should we force a redispatch attempt when the server is down ?
491 */
492 if (s->flags & SN_ASSIGNED)
493 si->state = SI_ST_ASS;
494 else
495 si->state = SI_ST_REQ;
496 return;
497 }
498}
499
500/* This function initiates a server connection request on a stream interface
501 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
502 * indicating that a server has been assigned. It may also return SI_ST_QUE,
503 * or SI_ST_CLO upon error.
504 */
505static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
506 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",
507 now_ms, __FUNCTION__,
508 s,
509 s->req, s->rep,
510 s->req->rex, s->rep->wex,
511 s->req->flags, s->rep->flags,
512 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
513
514 if (si->state != SI_ST_REQ)
515 return;
516
517 /* Try to assign a server */
518 if (srv_redispatch_connect(s) != 0) {
519 /* We did not get a server. Either we queued the
520 * connection request, or we encountered an error.
521 */
522 if (si->state == SI_ST_QUE)
523 return;
524
525 /* we did not get any server, let's check the cause */
526 si->shutr(si);
527 si->shutw(si);
528 si->ob->flags |= BF_WRITE_ERROR;
529 if (!si->err_type)
530 si->err_type = SI_ET_CONN_OTHER;
531 si->state = SI_ST_CLO;
532 return_srv_error(s, si->err_type);
533 return;
534 }
535
536 /* The server is assigned */
537 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
538 si->state = SI_ST_ASS;
539}
540
541/* Processes the client, server, request and response jobs of a session task,
542 * then puts it back to the wait queue in a clean state, or cleans up its
543 * resources if it must be deleted. Returns in <next> the date the task wants
544 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
545 * nothing too many times, the request and response buffers flags are monitored
546 * and each function is called only if at least another function has changed at
547 * least one flag it is interested in.
548 */
549void process_session(struct task *t, int *next)
550{
551 struct session *s = t->context;
552 int resync;
553 unsigned int rqf_last, rpf_last;
554
555 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
556 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
557
558 /* 1a: Check for low level timeouts if needed. We just set a flag on
559 * stream interfaces when their timeouts have expired.
560 */
561 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
562 stream_int_check_timeouts(&s->si[0]);
563 stream_int_check_timeouts(&s->si[1]);
564 buffer_check_timeouts(s->req);
565 buffer_check_timeouts(s->rep);
566 }
567
568 /* copy req/rep flags so that we can detect shutdowns */
569 rqf_last = s->req->flags;
570 rpf_last = s->rep->flags;
571
572 /* 1b: check for low-level errors reported at the stream interface.
573 * First we check if it's a retryable error (in which case we don't
574 * want to tell the buffer). Otherwise we report the error one level
575 * upper by setting flags into the buffers. Note that the side towards
576 * the client cannot have connect (hence retryable) errors. Also, the
577 * connection setup code must be able to deal with any type of abort.
578 */
579 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
580 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
581 s->si[0].shutr(&s->si[0]);
582 s->si[0].shutw(&s->si[0]);
583 stream_int_report_error(&s->si[0]);
584 }
585 }
586
587 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
588 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
589 s->si[1].shutr(&s->si[1]);
590 s->si[1].shutw(&s->si[1]);
591 stream_int_report_error(&s->si[1]);
592 s->be->failed_resp++;
593 if (s->srv)
594 s->srv->failed_resp++;
595 }
596 /* note: maybe we should process connection errors here ? */
597 }
598
599 if (s->si[1].state == SI_ST_CON) {
600 /* we were trying to establish a connection on the server side,
601 * maybe it succeeded, maybe it failed, maybe we timed out, ...
602 */
603 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
604 sess_update_st_cer(s, &s->si[1]);
605 else if (s->si[1].state == SI_ST_EST)
606 sess_establish(s, &s->si[1]);
607
608 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
609 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
610 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
611 */
612 }
613
614 /* check buffer timeouts, and close the corresponding stream interfaces
615 * for future reads or writes. Note: this will also concern upper layers
616 * but we do not touch any other flag. We must be careful and correctly
617 * detect state changes when calling them.
618 */
619 if (unlikely(s->req->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) {
620 if (s->req->flags & BF_READ_TIMEOUT)
621 s->req->prod->shutr(s->req->prod);
622 if (s->req->flags & BF_WRITE_TIMEOUT)
623 s->req->cons->shutw(s->req->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 if (unlikely(s->rep->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) {
638 if (s->rep->flags & BF_READ_TIMEOUT)
639 s->rep->prod->shutr(s->rep->prod);
640 if (s->rep->flags & BF_WRITE_TIMEOUT)
641 s->rep->cons->shutw(s->rep->cons);
642 DPRINTF(stderr,
643 "[%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",
644 now_ms, __FUNCTION__, __LINE__,
645 t,
646 s, s->flags,
647 s->req, s->rep,
648 s->req->rex, s->rep->wex,
649 s->req->flags, s->rep->flags,
650 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
651 s->rep->cons->err_type, s->req->cons->err_type,
652 s->conn_retries);
653 }
654
655 /* Check for connection closure */
656
657resync_stream_interface:
658 DPRINTF(stderr,
659 "[%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",
660 now_ms, __FUNCTION__, __LINE__,
661 t,
662 s, s->flags,
663 s->req, s->rep,
664 s->req->rex, s->rep->wex,
665 s->req->flags, s->rep->flags,
666 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
667 s->rep->cons->err_type, s->req->cons->err_type,
668 s->conn_retries);
669
670 /* nothing special to be done on client side */
671 if (unlikely(s->req->prod->state == SI_ST_DIS))
672 s->req->prod->state = SI_ST_CLO;
673
674 /* When a server-side connection is released, we have to count it and
675 * check for pending connections on this server.
676 */
677 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
678 s->req->cons->state = SI_ST_CLO;
679 if (s->srv) {
680 if (s->flags & SN_CURR_SESS) {
681 s->flags &= ~SN_CURR_SESS;
682 s->srv->cur_sess--;
683 }
684 sess_change_server(s, NULL);
685 if (may_dequeue_tasks(s->srv, s->be))
686 process_srv_queue(s->srv);
687 }
688 }
689
690 /*
691 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
692 * at this point.
693 */
694
695 /**** Process layer 7 below ****/
696
697 resync = 0;
698
699 /* Analyse request */
700 if ((s->req->flags & BF_MASK_ANALYSER) ||
701 (s->req->flags ^ rqf_last) & BF_MASK_STATIC) {
702 unsigned int flags = s->req->flags;
703
704 if (s->req->prod->state >= SI_ST_EST) {
705 /* it's up to the analysers to reset write_ena */
706 buffer_write_ena(s->req);
707 if (s->req->analysers)
708 process_request(s);
709 }
710 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
711 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
712 if (s->req->flags != flags)
713 resync = 1;
714 }
715
716 /* reflect what the L7 analysers have seen last */
717 rqf_last = s->req->flags;
718
719 /*
720 * Now forward all shutdown requests between both sides of the buffer
721 */
722
723 /* first, let's check if the request buffer needs to shutdown(write) */
724 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
725 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)))
726 buffer_shutw_now(s->req);
727 else if ((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA) &&
728 (s->req->cons->state == SI_ST_EST) &&
729 s->be->options & PR_O_FORCE_CLO &&
730 s->rep->flags & BF_READ_ACTIVITY) {
731 /* We want to force the connection to the server to close,
732 * and the server has begun to respond. That's the right
733 * time.
734 */
735 buffer_shutw_now(s->req);
736 }
737
738 /* shutdown(write) pending */
739 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW))
740 s->req->cons->shutw(s->req->cons);
741
742 /* shutdown(write) done on server side, we must stop the client too */
743 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW))
744 buffer_shutr_now(s->req);
745
746 /* shutdown(read) pending */
747 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
748 s->req->prod->shutr(s->req->prod);
749
750 /* it's possible that an upper layer has requested a connection setup */
751 if (s->req->cons->state == SI_ST_INI &&
752 (s->req->flags & (BF_WRITE_ENA|BF_SHUTW|BF_SHUTW_NOW)) == BF_WRITE_ENA)
753 s->req->cons->state = SI_ST_REQ;
754
755 /* we may have a pending connection request, or a connection waiting
756 * for completion.
757 */
758 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
759 do {
760 /* nb: step 1 might switch from QUE to ASS, but we first want
761 * to give a chance to step 2 to perform a redirect if needed.
762 */
763 if (s->si[1].state != SI_ST_REQ)
764 sess_update_stream_int(s, &s->si[1]);
765 if (s->si[1].state == SI_ST_REQ)
766 sess_prepare_conn_req(s, &s->si[1]);
767
768 if (s->si[1].state == SI_ST_ASS && s->srv &&
769 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
770 perform_http_redirect(s, &s->si[1]);
771 } while (s->si[1].state == SI_ST_ASS);
772 }
773
774 /*
775 * Here we want to check if we need to resync or not.
776 */
777 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
778 resync = 1;
779
780 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
781
782 /* according to benchmarks, it makes sense to resync now */
783 if (resync)
784 goto resync_stream_interface;
785
786
787 /* Analyse response */
788
789 if (unlikely(s->rep->flags & BF_HIJACK)) {
790 /* In inject mode, we wake up everytime something has
791 * happened on the write side of the buffer.
792 */
793 unsigned int flags = s->rep->flags;
794
795 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
796 !(s->rep->flags & BF_FULL)) {
797 produce_content(s);
798 }
799 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
800 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
801 if (s->rep->flags != flags)
802 resync = 1;
803 }
804 else if ((s->rep->flags & BF_MASK_ANALYSER) ||
805 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC) {
806 unsigned int flags = s->rep->flags;
807
808 if (s->rep->prod->state >= SI_ST_EST) {
809 /* it's up to the analysers to reset write_ena */
810 buffer_write_ena(s->rep);
811 if (s->rep->analysers)
812 process_response(s);
813 }
814 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
815 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
816 if (s->rep->flags != flags)
817 resync = 1;
818 }
819
820 /* reflect what the L7 analysers have seen last */
821 rpf_last = s->rep->flags;
822
823 /*
824 * Now forward all shutdown requests between both sides of the buffer
825 */
826
827 /*
828 * FIXME: this is probably where we should produce error responses.
829 */
830
831 /* first, let's check if the request buffer needs to shutdown(write) */
832 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
833 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)))
834 buffer_shutw_now(s->rep);
835
836 /* shutdown(write) pending */
837 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW))
838 s->rep->cons->shutw(s->rep->cons);
839
840 /* shutdown(write) done on the client side, we must stop the server too */
841 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW))
842 buffer_shutr_now(s->rep);
843
844 /* shutdown(read) pending */
845 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
846 s->rep->prod->shutr(s->rep->prod);
847
848 /*
849 * Here we want to check if we need to resync or not.
850 */
851 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
852 resync = 1;
853
854 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
855
856 if (resync)
857 goto resync_stream_interface;
858
859
860 /* This is needed only when debugging is enabled, to indicate
861 * client-side or server-side close. Please note that in the unlikely
862 * event where both sides would close at once, the sequence is reported
863 * on the server side first.
864 */
865 if (unlikely((global.mode & MODE_DEBUG) &&
866 (!(global.mode & MODE_QUIET) ||
867 (global.mode & MODE_VERBOSE)))) {
868 int len;
869
870 if (s->si[1].state == SI_ST_CLO &&
871 s->si[1].prev_state == SI_ST_EST) {
872 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
873 s->uniq_id, s->be->id,
874 (unsigned short)s->si[0].fd,
875 (unsigned short)s->si[1].fd);
876 write(1, trash, len);
877 }
878
879 if (s->si[0].state == SI_ST_CLO &&
880 s->si[0].prev_state == SI_ST_EST) {
881 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
882 s->uniq_id, s->be->id,
883 (unsigned short)s->si[0].fd,
884 (unsigned short)s->si[1].fd);
885 write(1, trash, len);
886 }
887 }
888
889 if (likely((s->rep->cons->state != SI_ST_CLO) ||
890 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
891
892 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
893 session_process_counters(s);
894
895 if (s->rep->cons->state == SI_ST_EST)
896 stream_sock_data_finish(s->rep->cons->fd);
897
898 if (s->req->cons->state == SI_ST_EST)
899 stream_sock_data_finish(s->req->cons->fd);
900
901 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
902 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
903 s->si[0].prev_state = s->si[0].state;
904 s->si[1].prev_state = s->si[1].state;
905 s->si[0].flags = s->si[1].flags = SI_FL_NONE;
906
907 /* Trick: if a request is being waiting for the server to respond,
908 * and if we know the server can timeout, we don't want the timeout
909 * to expire on the client side first, but we're still interested
910 * in passing data from the client to the server (eg: POST). Thus,
911 * we can cancel the client's request timeout if the server's
912 * request timeout is set and the server has not yet sent a response.
913 */
914
915 if ((s->rep->flags & (BF_WRITE_ENA|BF_SHUTR)) == 0 &&
916 (tick_isset(s->req->wex) || tick_isset(s->rep->rex)))
917 s->req->rex = TICK_ETERNITY;
918
919 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
920 tick_first(s->rep->rex, s->rep->wex));
921 if (s->req->analysers)
922 t->expire = tick_first(t->expire, s->req->analyse_exp);
923
924 if (s->si[0].exp)
925 t->expire = tick_first(t->expire, s->si[0].exp);
926
927 if (s->si[1].exp)
928 t->expire = tick_first(t->expire, s->si[1].exp);
929
930#ifdef DEBUG_FULL
931 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",
932 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);
933#endif
934 /* restore t to its place in the task list */
935 task_queue(t);
936
937#ifdef DEBUG_DEV
938 /* this may only happen when no timeout is set or in case of an FSM bug */
939 if (!t->expire)
940 ABORT_NOW();
941#endif
942 *next = t->expire;
943 return; /* nothing more to do */
944 }
945
946 s->fe->feconn--;
947 if (s->flags & SN_BE_ASSIGNED)
948 s->be->beconn--;
949 actconn--;
950
951 if (unlikely((global.mode & MODE_DEBUG) &&
952 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
953 int len;
954 len = sprintf(trash, "%08x:%s.closed[%04x:%04x] (term_trace=0x%08x)\n",
955 s->uniq_id, s->be->id,
956 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd,
957 s->term_trace);
958 write(1, trash, len);
959 }
960
961 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
962 session_process_counters(s);
963
964 /* let's do a final log if we need it */
965 if (s->logs.logwait &&
966 !(s->flags & SN_MONITOR) &&
967 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100968 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100969 }
970
971 /* the task MUST not be in the run queue anymore */
972 task_delete(t);
973 session_free(s);
974 task_free(t);
975 *next = TICK_ETERNITY;
976}
977
Willy Tarreau7c669d72008-06-20 15:04:11 +0200978/*
979 * This function adjusts sess->srv_conn and maintains the previous and new
980 * server's served session counts. Setting newsrv to NULL is enough to release
981 * current connection slot. This function also notifies any LB algo which might
982 * expect to be informed about any change in the number of active sessions on a
983 * server.
984 */
985void sess_change_server(struct session *sess, struct server *newsrv)
986{
987 if (sess->srv_conn == newsrv)
988 return;
989
990 if (sess->srv_conn) {
991 sess->srv_conn->served--;
992 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
993 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
994 sess->srv_conn = NULL;
995 }
996
997 if (newsrv) {
998 newsrv->served++;
999 if (newsrv->proxy->lbprm.server_take_conn)
1000 newsrv->proxy->lbprm.server_take_conn(newsrv);
1001 sess->srv_conn = newsrv;
1002 }
1003}
1004
1005
Willy Tarreaubaaee002006-06-26 02:48:02 +02001006/*
1007 * Local variables:
1008 * c-indent-level: 8
1009 * c-basic-offset: 8
1010 * End:
1011 */