blob: 95ece53fa6d77733592c0a332fa4b1ebe6ce98b0 [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 Tarreau62e4f1d2008-12-07 20:16:23 +010045 struct bref *bref, *back;
Willy Tarreau0f7562b2007-01-07 15:46:13 +010046
Willy Tarreaubaaee002006-06-26 02:48:02 +020047 if (s->pend_pos)
48 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +010049
Willy Tarreau1e62de62008-11-11 20:20:02 +010050 if (s->srv) { /* there may be requests left pending in queue */
51 if (s->flags & SN_CURR_SESS) {
52 s->flags &= ~SN_CURR_SESS;
53 s->srv->cur_sess--;
54 }
Willy Tarreau922a8062008-12-04 09:33:58 +010055 if (may_dequeue_tasks(s->srv, s->be))
56 process_srv_queue(s->srv);
Willy Tarreau1e62de62008-11-11 20:20:02 +010057 }
Willy Tarreau922a8062008-12-04 09:33:58 +010058
Willy Tarreau7c669d72008-06-20 15:04:11 +020059 if (unlikely(s->srv_conn)) {
60 /* the session still has a reserved slot on a server, but
61 * it should normally be only the same as the one above,
62 * so this should not happen in fact.
63 */
64 sess_change_server(s, NULL);
65 }
66
Willy Tarreau48d63db2008-08-03 17:41:33 +020067 pool_free2(pool2_buffer, s->req);
68 pool_free2(pool2_buffer, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +020069
Willy Tarreau92fb9832007-10-16 17:34:28 +020070 if (fe) {
Willy Tarreau48d63db2008-08-03 17:41:33 +020071 pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau41dff822007-01-01 23:32:30 +010072
Willy Tarreau92fb9832007-10-16 17:34:28 +020073 if (txn->rsp.cap != NULL) {
74 struct cap_hdr *h;
Willy Tarreau48d63db2008-08-03 17:41:33 +020075 for (h = fe->rsp_cap; h; h = h->next)
76 pool_free2(h->pool, txn->rsp.cap[h->index]);
Willy Tarreau92fb9832007-10-16 17:34:28 +020077 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +020078 }
Willy Tarreau92fb9832007-10-16 17:34:28 +020079 if (txn->req.cap != NULL) {
80 struct cap_hdr *h;
Willy Tarreau48d63db2008-08-03 17:41:33 +020081 for (h = fe->req_cap; h; h = h->next)
82 pool_free2(h->pool, txn->req.cap[h->index]);
Willy Tarreau92fb9832007-10-16 17:34:28 +020083 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +020084 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020085 }
Willy Tarreau48d63db2008-08-03 17:41:33 +020086 pool_free2(pool2_requri, txn->uri);
87 pool_free2(pool2_capture, txn->cli_cookie);
88 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +010089
90 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
91 LIST_DEL(&bref->users);
92 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
93 bref->ref = s->list.n;
94 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +010095 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020096 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +020097
98 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +020099 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200100 pool_flush2(pool2_buffer);
101 pool_flush2(fe->hdr_idx_pool);
102 pool_flush2(pool2_requri);
103 pool_flush2(pool2_capture);
104 pool_flush2(pool2_session);
105 pool_flush2(fe->req_cap_pool);
106 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200107 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200108}
109
110
111/* perform minimal intializations, report 0 in case of error, 1 if OK. */
112int init_session()
113{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100114 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200115 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
116 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200117}
118
Willy Tarreau30e71012007-11-26 20:15:35 +0100119void session_process_counters(struct session *s)
120{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100121 unsigned long long bytes;
122
Willy Tarreau30e71012007-11-26 20:15:35 +0100123 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100124 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100125 s->logs.bytes_in = s->req->total;
126 if (bytes) {
127 s->fe->bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100128
Willy Tarreau30e71012007-11-26 20:15:35 +0100129 if (s->be != s->fe)
130 s->be->bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100131
Willy Tarreau30e71012007-11-26 20:15:35 +0100132 if (s->srv)
133 s->srv->bytes_in += bytes;
134 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100135 }
136
Willy Tarreau30e71012007-11-26 20:15:35 +0100137 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100138 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100139 s->logs.bytes_out = s->rep->total;
140 if (bytes) {
141 s->fe->bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100142
Willy Tarreau30e71012007-11-26 20:15:35 +0100143 if (s->be != s->fe)
144 s->be->bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100145
Willy Tarreau30e71012007-11-26 20:15:35 +0100146 if (s->srv)
147 s->srv->bytes_out += bytes;
148 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100149 }
150}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200151
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100152/* This function is called with (si->state == SI_ST_CON) meaning that a
153 * connection was attempted and that the file descriptor is already allocated.
154 * We must check for establishment, error and abort. Possible output states
155 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
156 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
157 * otherwise 1.
158 */
159int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
160{
161 struct buffer *req = si->ob;
162 struct buffer *rep = si->ib;
163
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100164 /* If we got an error, or if nothing happened and the connection timed
165 * out, we must give up. The CER state handler will take care of retry
166 * attempts and error reports.
167 */
168 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
169 si->state = SI_ST_CER;
170 fd_delete(si->fd);
171
172 if (si->err_type)
173 return 0;
174
175 si->err_loc = s->srv;
176 if (si->flags & SI_FL_ERR)
177 si->err_type = SI_ET_CONN_ERR;
178 else
179 si->err_type = SI_ET_CONN_TO;
180 return 0;
181 }
182
183 /* OK, maybe we want to abort */
184 if (unlikely((req->flags & BF_SHUTW_NOW) ||
185 (rep->flags & BF_SHUTW) ||
186 ((req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
187 ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_ACTIVITY)) ||
188 s->be->options & PR_O_ABRT_CLOSE)))) {
189 /* give up */
190 si->shutw(si);
191 si->err_type |= SI_ET_CONN_ABRT;
192 si->err_loc = s->srv;
193 return 1;
194 }
195
196 /* we need to wait a bit more if there was no activity either */
197 if (!(req->flags & BF_WRITE_ACTIVITY))
198 return 1;
199
200 /* OK, this means that a connection succeeded. The caller will be
201 * responsible for handling the transition from CON to EST.
202 */
203 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
204 si->state = SI_ST_EST;
205 si->err_type = SI_ET_NONE;
206 si->err_loc = NULL;
207 return 1;
208}
209
210/* This function is called with (si->state == SI_ST_CER) meaning that a
211 * previous connection attempt has failed and that the file descriptor
212 * has already been released. Possible causes include asynchronous error
213 * notification and time out. Possible output states are SI_ST_CLO when
214 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
215 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
216 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
217 * marked as in error state. It returns 0.
218 */
219int sess_update_st_cer(struct session *s, struct stream_interface *si)
220{
221 /* we probably have to release last session from the server */
222 if (s->srv) {
223 if (s->flags & SN_CURR_SESS) {
224 s->flags &= ~SN_CURR_SESS;
225 s->srv->cur_sess--;
226 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100227 }
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;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100249 if (s->srv_error)
250 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100251 return 0;
252 }
253
254 /* If the "redispatch" option is set on the backend, we are allowed to
255 * retry on another server for the last retry. In order to achieve this,
256 * we must mark the session unassigned, and eventually clear the DIRECT
257 * bit to ignore any persistence cookie. We won't count a retry nor a
258 * redispatch yet, because this will depend on what server is selected.
259 */
260 if (s->srv && s->conn_retries == 0 && s->be->options & PR_O_REDISP) {
261 if (may_dequeue_tasks(s->srv, s->be))
262 process_srv_queue(s->srv);
263
264 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
265 s->prev_srv = s->srv;
266 si->state = SI_ST_REQ;
267 } else {
268 if (s->srv)
269 s->srv->retries++;
270 s->be->retries++;
271 si->state = SI_ST_ASS;
272 }
273
274 if (si->flags & SI_FL_ERR) {
275 /* The error was an asynchronous connection error, and we will
276 * likely have to retry connecting to the same server, most
277 * likely leading to the same result. To avoid this, we wait
278 * one second before retrying.
279 */
280
281 if (!si->err_type)
282 si->err_type = SI_ET_CONN_ERR;
283
284 si->state = SI_ST_TAR;
285 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
286 return 0;
287 }
288 return 0;
289}
290
291/*
292 * This function handles the transition between the SI_ST_CON state and the
293 * SI_ST_EST state. It must only be called after switching from SI_ST_CON to
294 * SI_ST_EST.
295 */
296void sess_establish(struct session *s, struct stream_interface *si)
297{
298 struct buffer *req = si->ob;
299 struct buffer *rep = si->ib;
300
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100301 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100302 buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */
303
304 /* if the user wants to log as soon as possible, without counting
305 * bytes from the server, then this is the right moment. */
306 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
307 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100308 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100309 }
310#ifdef CONFIG_HAP_TCPSPLICE
311 if ((s->fe->options & s->be->options) & PR_O_TCPSPLICE) {
312 /* TCP splicing supported by both FE and BE */
313 tcp_splice_splicefd(req->prod->fd, si->fd, 0);
314 }
315#endif
316 }
317 else {
318 rep->analysers |= AN_RTR_HTTP_HDR;
319 buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */
320 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
321 /* reset hdr_idx which was already initialized by the request.
322 * right now, the http parser does it.
323 * hdr_idx_init(&s->txn.hdr_idx);
324 */
325 }
326
327 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
328 req->wex = TICK_ETERNITY;
329}
330
331/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
332 * Other input states are simply ignored.
333 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
334 * Flags must have previously been updated for timeouts and other conditions.
335 */
336void sess_update_stream_int(struct session *s, struct stream_interface *si)
337{
338 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",
339 now_ms, __FUNCTION__,
340 s,
341 s->req, s->rep,
342 s->req->rex, s->rep->wex,
343 s->req->flags, s->rep->flags,
344 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
345
346 if (si->state == SI_ST_ASS) {
347 /* Server assigned to connection request, we have to try to connect now */
348 int conn_err;
349
350 conn_err = connect_server(s);
351 if (conn_err == SN_ERR_NONE) {
352 /* state = SI_ST_CON now */
Willy Tarreau8f6457c2008-12-01 00:08:28 +0100353 if (s->srv)
354 s->srv->cum_sess++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100355 return;
356 }
357
358 /* We have received a synchronous error. We might have to
359 * abort, retry immediately or redispatch.
360 */
361 if (conn_err == SN_ERR_INTERNAL) {
362 if (!si->err_type) {
363 si->err_type = SI_ET_CONN_OTHER;
364 si->err_loc = s->srv;
365 }
366
367 if (s->srv)
368 s->srv->cum_sess++;
369 if (s->srv)
370 s->srv->failed_conns++;
371 s->be->failed_conns++;
372
373 /* release other sessions waiting for this server */
374 if (may_dequeue_tasks(s->srv, s->be))
375 process_srv_queue(s->srv);
376
377 /* Failed and not retryable. */
378 si->shutr(si);
379 si->shutw(si);
380 si->ob->flags |= BF_WRITE_ERROR;
381
382 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
383
384 /* no session was ever accounted for this server */
385 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100386 if (s->srv_error)
387 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100388 return;
389 }
390
391 /* We are facing a retryable error, but we don't want to run a
392 * turn-around now, as the problem is likely a source port
393 * allocation problem, so we want to retry now.
394 */
395 si->state = SI_ST_CER;
396 si->flags &= ~SI_FL_ERR;
397 sess_update_st_cer(s, si);
398 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
399 return;
400 }
401 else if (si->state == SI_ST_QUE) {
402 /* connection request was queued, check for any update */
403 if (!s->pend_pos) {
404 /* The connection is not in the queue anymore. Either
405 * we have a server connection slot available and we
406 * go directly to the assigned state, or we need to
407 * load-balance first and go to the INI state.
408 */
409 si->exp = TICK_ETERNITY;
410 if (unlikely(!(s->flags & SN_ASSIGNED)))
411 si->state = SI_ST_REQ;
412 else {
413 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
414 si->state = SI_ST_ASS;
415 }
416 return;
417 }
418
419 /* Connection request still in queue... */
420 if (si->flags & SI_FL_EXP) {
421 /* ... and timeout expired */
422 si->exp = TICK_ETERNITY;
423 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
424 if (s->srv)
425 s->srv->failed_conns++;
426 s->be->failed_conns++;
427 si->shutr(si);
428 si->shutw(si);
429 si->ob->flags |= BF_WRITE_TIMEOUT;
430 if (!si->err_type)
431 si->err_type = SI_ET_QUEUE_TO;
432 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100433 if (s->srv_error)
434 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100435 return;
436 }
437
438 /* Connection remains in queue, check if we have to abort it */
439 if ((si->ob->flags & (BF_READ_ERROR|BF_SHUTW_NOW)) || /* abort requested */
440 ((si->ob->flags & BF_SHUTR) && /* empty and client stopped */
441 (si->ob->flags & BF_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
442 /* give up */
443 si->exp = TICK_ETERNITY;
444 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
445 si->shutr(si);
446 si->shutw(si);
447 si->err_type |= SI_ET_QUEUE_ABRT;
448 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100449 if (s->srv_error)
450 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100451 return;
452 }
453
454 /* Nothing changed */
455 return;
456 }
457 else if (si->state == SI_ST_TAR) {
458 /* Connection request might be aborted */
459 if ((si->ob->flags & (BF_READ_ERROR|BF_SHUTW_NOW)) || /* abort requested */
460 ((si->ob->flags & BF_SHUTR) && /* empty and client stopped */
461 (si->ob->flags & BF_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
462 /* give up */
463 si->exp = TICK_ETERNITY;
464 si->shutr(si);
465 si->shutw(si);
466 si->err_type |= SI_ET_CONN_ABRT;
467 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100468 if (s->srv_error)
469 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100470 return;
471 }
472
473 if (!(si->flags & SI_FL_EXP))
474 return; /* still in turn-around */
475
476 si->exp = TICK_ETERNITY;
477
478 /* we keep trying on the same server as long as the session is
479 * marked "assigned".
480 * FIXME: Should we force a redispatch attempt when the server is down ?
481 */
482 if (s->flags & SN_ASSIGNED)
483 si->state = SI_ST_ASS;
484 else
485 si->state = SI_ST_REQ;
486 return;
487 }
488}
489
490/* This function initiates a server connection request on a stream interface
491 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
492 * indicating that a server has been assigned. It may also return SI_ST_QUE,
493 * or SI_ST_CLO upon error.
494 */
495static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
496 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",
497 now_ms, __FUNCTION__,
498 s,
499 s->req, s->rep,
500 s->req->rex, s->rep->wex,
501 s->req->flags, s->rep->flags,
502 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
503
504 if (si->state != SI_ST_REQ)
505 return;
506
507 /* Try to assign a server */
508 if (srv_redispatch_connect(s) != 0) {
509 /* We did not get a server. Either we queued the
510 * connection request, or we encountered an error.
511 */
512 if (si->state == SI_ST_QUE)
513 return;
514
515 /* we did not get any server, let's check the cause */
516 si->shutr(si);
517 si->shutw(si);
518 si->ob->flags |= BF_WRITE_ERROR;
519 if (!si->err_type)
520 si->err_type = SI_ET_CONN_OTHER;
521 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100522 if (s->srv_error)
523 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100524 return;
525 }
526
527 /* The server is assigned */
528 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
529 si->state = SI_ST_ASS;
530}
531
532/* Processes the client, server, request and response jobs of a session task,
533 * then puts it back to the wait queue in a clean state, or cleans up its
534 * resources if it must be deleted. Returns in <next> the date the task wants
535 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
536 * nothing too many times, the request and response buffers flags are monitored
537 * and each function is called only if at least another function has changed at
538 * least one flag it is interested in.
539 */
540void process_session(struct task *t, int *next)
541{
542 struct session *s = t->context;
543 int resync;
544 unsigned int rqf_last, rpf_last;
545
546 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
547 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
548
549 /* 1a: Check for low level timeouts if needed. We just set a flag on
550 * stream interfaces when their timeouts have expired.
551 */
552 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
553 stream_int_check_timeouts(&s->si[0]);
554 stream_int_check_timeouts(&s->si[1]);
555 buffer_check_timeouts(s->req);
556 buffer_check_timeouts(s->rep);
557 }
558
559 /* copy req/rep flags so that we can detect shutdowns */
560 rqf_last = s->req->flags;
561 rpf_last = s->rep->flags;
562
563 /* 1b: check for low-level errors reported at the stream interface.
564 * First we check if it's a retryable error (in which case we don't
565 * want to tell the buffer). Otherwise we report the error one level
566 * upper by setting flags into the buffers. Note that the side towards
567 * the client cannot have connect (hence retryable) errors. Also, the
568 * connection setup code must be able to deal with any type of abort.
569 */
570 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
571 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
572 s->si[0].shutr(&s->si[0]);
573 s->si[0].shutw(&s->si[0]);
574 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +0100575 if (!(s->req->analysers) && !(s->rep->analysers)) {
576 if (!(s->flags & SN_ERR_MASK))
577 s->flags |= SN_ERR_CLICL;
578 if (!(s->flags & SN_FINST_MASK))
579 s->flags |= SN_FINST_D;
580 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100581 }
582 }
583
584 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
585 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
586 s->si[1].shutr(&s->si[1]);
587 s->si[1].shutw(&s->si[1]);
588 stream_int_report_error(&s->si[1]);
589 s->be->failed_resp++;
590 if (s->srv)
591 s->srv->failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +0100592 if (!(s->req->analysers) && !(s->rep->analysers)) {
593 if (!(s->flags & SN_ERR_MASK))
594 s->flags |= SN_ERR_SRVCL;
595 if (!(s->flags & SN_FINST_MASK))
596 s->flags |= SN_FINST_D;
597 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100598 }
599 /* note: maybe we should process connection errors here ? */
600 }
601
602 if (s->si[1].state == SI_ST_CON) {
603 /* we were trying to establish a connection on the server side,
604 * maybe it succeeded, maybe it failed, maybe we timed out, ...
605 */
606 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
607 sess_update_st_cer(s, &s->si[1]);
608 else if (s->si[1].state == SI_ST_EST)
609 sess_establish(s, &s->si[1]);
610
611 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
612 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
613 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
614 */
615 }
616
617 /* check buffer timeouts, and close the corresponding stream interfaces
618 * for future reads or writes. Note: this will also concern upper layers
619 * but we do not touch any other flag. We must be careful and correctly
620 * detect state changes when calling them.
621 */
622 if (unlikely(s->req->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) {
623 if (s->req->flags & BF_READ_TIMEOUT)
624 s->req->prod->shutr(s->req->prod);
625 if (s->req->flags & BF_WRITE_TIMEOUT)
626 s->req->cons->shutw(s->req->cons);
627 DPRINTF(stderr,
628 "[%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",
629 now_ms, __FUNCTION__, __LINE__,
630 t,
631 s, s->flags,
632 s->req, s->rep,
633 s->req->rex, s->rep->wex,
634 s->req->flags, s->rep->flags,
635 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
636 s->rep->cons->err_type, s->req->cons->err_type,
637 s->conn_retries);
638 }
639
640 if (unlikely(s->rep->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) {
641 if (s->rep->flags & BF_READ_TIMEOUT)
642 s->rep->prod->shutr(s->rep->prod);
643 if (s->rep->flags & BF_WRITE_TIMEOUT)
644 s->rep->cons->shutw(s->rep->cons);
645 DPRINTF(stderr,
646 "[%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",
647 now_ms, __FUNCTION__, __LINE__,
648 t,
649 s, s->flags,
650 s->req, s->rep,
651 s->req->rex, s->rep->wex,
652 s->req->flags, s->rep->flags,
653 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
654 s->rep->cons->err_type, s->req->cons->err_type,
655 s->conn_retries);
656 }
657
658 /* Check for connection closure */
659
660resync_stream_interface:
661 DPRINTF(stderr,
662 "[%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",
663 now_ms, __FUNCTION__, __LINE__,
664 t,
665 s, s->flags,
666 s->req, s->rep,
667 s->req->rex, s->rep->wex,
668 s->req->flags, s->rep->flags,
669 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
670 s->rep->cons->err_type, s->req->cons->err_type,
671 s->conn_retries);
672
673 /* nothing special to be done on client side */
674 if (unlikely(s->req->prod->state == SI_ST_DIS))
675 s->req->prod->state = SI_ST_CLO;
676
677 /* When a server-side connection is released, we have to count it and
678 * check for pending connections on this server.
679 */
680 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
681 s->req->cons->state = SI_ST_CLO;
682 if (s->srv) {
683 if (s->flags & SN_CURR_SESS) {
684 s->flags &= ~SN_CURR_SESS;
685 s->srv->cur_sess--;
686 }
687 sess_change_server(s, NULL);
688 if (may_dequeue_tasks(s->srv, s->be))
689 process_srv_queue(s->srv);
690 }
691 }
692
693 /*
694 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
695 * at this point.
696 */
697
698 /**** Process layer 7 below ****/
699
700 resync = 0;
701
702 /* Analyse request */
703 if ((s->req->flags & BF_MASK_ANALYSER) ||
704 (s->req->flags ^ rqf_last) & BF_MASK_STATIC) {
705 unsigned int flags = s->req->flags;
706
707 if (s->req->prod->state >= SI_ST_EST) {
708 /* it's up to the analysers to reset write_ena */
709 buffer_write_ena(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100710
711 /* We will call all analysers for which a bit is set in
712 * s->req->analysers, following the bit order from LSB
713 * to MSB. The analysers must remove themselves from
714 * the list when not needed. This while() loop is in
715 * fact a cleaner if().
716 */
717 while (s->req->analysers) {
718 if (s->req->analysers & AN_REQ_INSPECT)
719 if (!tcp_inspect_request(s, s->req))
720 break;
721
Willy Tarreau59234e92008-11-30 23:51:27 +0100722 if (s->req->analysers & AN_REQ_HTTP_HDR)
723 if (!http_process_request(s, s->req))
Willy Tarreauedcf6682008-11-30 23:15:34 +0100724 break;
725
Willy Tarreau60b85b02008-11-30 23:28:40 +0100726 if (s->req->analysers & AN_REQ_HTTP_TARPIT)
727 if (!http_process_tarpit(s, s->req))
728 break;
729
Willy Tarreaud34af782008-11-30 23:36:37 +0100730 if (s->req->analysers & AN_REQ_HTTP_BODY)
731 if (!http_process_request_body(s, s->req))
732 break;
733
Willy Tarreauedcf6682008-11-30 23:15:34 +0100734 /* Just make sure that nobody set a wrong flag causing an endless loop */
735 s->req->analysers &= AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_TARPIT | AN_REQ_HTTP_BODY;
736
737 /* we don't want to loop anyway */
738 break;
739 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100740 }
741 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
742 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
743 if (s->req->flags != flags)
744 resync = 1;
745 }
746
Willy Tarreauf890dc92008-12-13 21:12:26 +0100747 /* if noone is interested in analysing data, let's forward everything */
748 if (!s->req->analysers && !(s->req->flags & BF_HIJACK))
749 s->req->send_max = s->req->l;
750
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100751 /* reflect what the L7 analysers have seen last */
752 rqf_last = s->req->flags;
753
754 /*
755 * Now forward all shutdown requests between both sides of the buffer
756 */
757
758 /* first, let's check if the request buffer needs to shutdown(write) */
759 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
760 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)))
761 buffer_shutw_now(s->req);
762 else if ((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA) &&
763 (s->req->cons->state == SI_ST_EST) &&
764 s->be->options & PR_O_FORCE_CLO &&
765 s->rep->flags & BF_READ_ACTIVITY) {
766 /* We want to force the connection to the server to close,
767 * and the server has begun to respond. That's the right
768 * time.
769 */
770 buffer_shutw_now(s->req);
771 }
772
773 /* shutdown(write) pending */
774 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW))
775 s->req->cons->shutw(s->req->cons);
776
777 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +0100778 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
779 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100780 buffer_shutr_now(s->req);
781
782 /* shutdown(read) pending */
783 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
784 s->req->prod->shutr(s->req->prod);
785
786 /* it's possible that an upper layer has requested a connection setup */
787 if (s->req->cons->state == SI_ST_INI &&
788 (s->req->flags & (BF_WRITE_ENA|BF_SHUTW|BF_SHUTW_NOW)) == BF_WRITE_ENA)
789 s->req->cons->state = SI_ST_REQ;
790
791 /* we may have a pending connection request, or a connection waiting
792 * for completion.
793 */
794 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
795 do {
796 /* nb: step 1 might switch from QUE to ASS, but we first want
797 * to give a chance to step 2 to perform a redirect if needed.
798 */
799 if (s->si[1].state != SI_ST_REQ)
800 sess_update_stream_int(s, &s->si[1]);
801 if (s->si[1].state == SI_ST_REQ)
802 sess_prepare_conn_req(s, &s->si[1]);
803
804 if (s->si[1].state == SI_ST_ASS && s->srv &&
805 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
806 perform_http_redirect(s, &s->si[1]);
807 } while (s->si[1].state == SI_ST_ASS);
808 }
809
810 /*
811 * Here we want to check if we need to resync or not.
812 */
813 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
814 resync = 1;
815
816 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
817
818 /* according to benchmarks, it makes sense to resync now */
819 if (resync)
820 goto resync_stream_interface;
821
822
823 /* Analyse response */
824
825 if (unlikely(s->rep->flags & BF_HIJACK)) {
826 /* In inject mode, we wake up everytime something has
827 * happened on the write side of the buffer.
828 */
829 unsigned int flags = s->rep->flags;
830
831 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
832 !(s->rep->flags & BF_FULL)) {
Willy Tarreau01bf8672008-12-07 18:03:29 +0100833 s->rep->hijacker(s, s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100834 }
835 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
836 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
837 if (s->rep->flags != flags)
838 resync = 1;
839 }
840 else if ((s->rep->flags & BF_MASK_ANALYSER) ||
841 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC) {
842 unsigned int flags = s->rep->flags;
843
844 if (s->rep->prod->state >= SI_ST_EST) {
845 /* it's up to the analysers to reset write_ena */
846 buffer_write_ena(s->rep);
847 if (s->rep->analysers)
848 process_response(s);
849 }
850 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
851 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
852 if (s->rep->flags != flags)
853 resync = 1;
854 }
855
Willy Tarreauf890dc92008-12-13 21:12:26 +0100856 /* if noone is interested in analysing data, let's forward everything */
857 if (!s->rep->analysers && !(s->rep->flags & BF_HIJACK))
858 s->rep->send_max = s->rep->l;
859
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100860 /* reflect what the L7 analysers have seen last */
861 rpf_last = s->rep->flags;
862
863 /*
864 * Now forward all shutdown requests between both sides of the buffer
865 */
866
867 /*
868 * FIXME: this is probably where we should produce error responses.
869 */
870
871 /* first, let's check if the request buffer needs to shutdown(write) */
872 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
873 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)))
874 buffer_shutw_now(s->rep);
875
876 /* shutdown(write) pending */
877 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW))
878 s->rep->cons->shutw(s->rep->cons);
879
880 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +0100881 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
882 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100883 buffer_shutr_now(s->rep);
884
885 /* shutdown(read) pending */
886 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
887 s->rep->prod->shutr(s->rep->prod);
888
889 /*
890 * Here we want to check if we need to resync or not.
891 */
892 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
893 resync = 1;
894
895 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
896
897 if (resync)
898 goto resync_stream_interface;
899
900
901 /* This is needed only when debugging is enabled, to indicate
902 * client-side or server-side close. Please note that in the unlikely
903 * event where both sides would close at once, the sequence is reported
904 * on the server side first.
905 */
906 if (unlikely((global.mode & MODE_DEBUG) &&
907 (!(global.mode & MODE_QUIET) ||
908 (global.mode & MODE_VERBOSE)))) {
909 int len;
910
911 if (s->si[1].state == SI_ST_CLO &&
912 s->si[1].prev_state == SI_ST_EST) {
913 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
914 s->uniq_id, s->be->id,
915 (unsigned short)s->si[0].fd,
916 (unsigned short)s->si[1].fd);
917 write(1, trash, len);
918 }
919
920 if (s->si[0].state == SI_ST_CLO &&
921 s->si[0].prev_state == SI_ST_EST) {
922 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
923 s->uniq_id, s->be->id,
924 (unsigned short)s->si[0].fd,
925 (unsigned short)s->si[1].fd);
926 write(1, trash, len);
927 }
928 }
929
930 if (likely((s->rep->cons->state != SI_ST_CLO) ||
931 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
932
933 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
934 session_process_counters(s);
935
936 if (s->rep->cons->state == SI_ST_EST)
Willy Tarreaub0253252008-11-30 21:37:12 +0100937 stream_sock_data_finish(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100938
939 if (s->req->cons->state == SI_ST_EST)
Willy Tarreaub0253252008-11-30 21:37:12 +0100940 stream_sock_data_finish(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100941
942 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
943 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
944 s->si[0].prev_state = s->si[0].state;
945 s->si[1].prev_state = s->si[1].state;
946 s->si[0].flags = s->si[1].flags = SI_FL_NONE;
947
948 /* Trick: if a request is being waiting for the server to respond,
949 * and if we know the server can timeout, we don't want the timeout
950 * to expire on the client side first, but we're still interested
951 * in passing data from the client to the server (eg: POST). Thus,
952 * we can cancel the client's request timeout if the server's
953 * request timeout is set and the server has not yet sent a response.
954 */
955
956 if ((s->rep->flags & (BF_WRITE_ENA|BF_SHUTR)) == 0 &&
957 (tick_isset(s->req->wex) || tick_isset(s->rep->rex)))
958 s->req->rex = TICK_ETERNITY;
959
960 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
961 tick_first(s->rep->rex, s->rep->wex));
962 if (s->req->analysers)
963 t->expire = tick_first(t->expire, s->req->analyse_exp);
964
965 if (s->si[0].exp)
966 t->expire = tick_first(t->expire, s->si[0].exp);
967
968 if (s->si[1].exp)
969 t->expire = tick_first(t->expire, s->si[1].exp);
970
971#ifdef DEBUG_FULL
972 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",
973 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);
974#endif
975 /* restore t to its place in the task list */
976 task_queue(t);
977
978#ifdef DEBUG_DEV
979 /* this may only happen when no timeout is set or in case of an FSM bug */
980 if (!t->expire)
981 ABORT_NOW();
982#endif
983 *next = t->expire;
984 return; /* nothing more to do */
985 }
986
987 s->fe->feconn--;
988 if (s->flags & SN_BE_ASSIGNED)
989 s->be->beconn--;
990 actconn--;
991
992 if (unlikely((global.mode & MODE_DEBUG) &&
993 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
994 int len;
995 len = sprintf(trash, "%08x:%s.closed[%04x:%04x] (term_trace=0x%08x)\n",
996 s->uniq_id, s->be->id,
997 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd,
998 s->term_trace);
999 write(1, trash, len);
1000 }
1001
1002 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
1003 session_process_counters(s);
1004
1005 /* let's do a final log if we need it */
1006 if (s->logs.logwait &&
1007 !(s->flags & SN_MONITOR) &&
1008 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001009 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001010 }
1011
1012 /* the task MUST not be in the run queue anymore */
1013 task_delete(t);
1014 session_free(s);
1015 task_free(t);
1016 *next = TICK_ETERNITY;
1017}
1018
Willy Tarreau7c669d72008-06-20 15:04:11 +02001019/*
1020 * This function adjusts sess->srv_conn and maintains the previous and new
1021 * server's served session counts. Setting newsrv to NULL is enough to release
1022 * current connection slot. This function also notifies any LB algo which might
1023 * expect to be informed about any change in the number of active sessions on a
1024 * server.
1025 */
1026void sess_change_server(struct session *sess, struct server *newsrv)
1027{
1028 if (sess->srv_conn == newsrv)
1029 return;
1030
1031 if (sess->srv_conn) {
1032 sess->srv_conn->served--;
1033 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
1034 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
1035 sess->srv_conn = NULL;
1036 }
1037
1038 if (newsrv) {
1039 newsrv->served++;
1040 if (newsrv->proxy->lbprm.server_take_conn)
1041 newsrv->proxy->lbprm.server_take_conn(newsrv);
1042 sess->srv_conn = newsrv;
1043 }
1044}
1045
1046
Willy Tarreaubaaee002006-06-26 02:48:02 +02001047/*
1048 * Local variables:
1049 * c-indent-level: 8
1050 * c-basic-offset: 8
1051 * End:
1052 */