blob: a36bee0640507f44c23cf6b57458e9b98979185a [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;
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 */
353 return;
354 }
355
356 /* We have received a synchronous error. We might have to
357 * abort, retry immediately or redispatch.
358 */
359 if (conn_err == SN_ERR_INTERNAL) {
360 if (!si->err_type) {
361 si->err_type = SI_ET_CONN_OTHER;
362 si->err_loc = s->srv;
363 }
364
365 if (s->srv)
366 s->srv->cum_sess++;
367 if (s->srv)
368 s->srv->failed_conns++;
369 s->be->failed_conns++;
370
371 /* release other sessions waiting for this server */
372 if (may_dequeue_tasks(s->srv, s->be))
373 process_srv_queue(s->srv);
374
375 /* Failed and not retryable. */
376 si->shutr(si);
377 si->shutw(si);
378 si->ob->flags |= BF_WRITE_ERROR;
379
380 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
381
382 /* no session was ever accounted for this server */
383 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100384 if (s->srv_error)
385 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100386 return;
387 }
388
389 /* We are facing a retryable error, but we don't want to run a
390 * turn-around now, as the problem is likely a source port
391 * allocation problem, so we want to retry now.
392 */
393 si->state = SI_ST_CER;
394 si->flags &= ~SI_FL_ERR;
395 sess_update_st_cer(s, si);
396 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
397 return;
398 }
399 else if (si->state == SI_ST_QUE) {
400 /* connection request was queued, check for any update */
401 if (!s->pend_pos) {
402 /* The connection is not in the queue anymore. Either
403 * we have a server connection slot available and we
404 * go directly to the assigned state, or we need to
405 * load-balance first and go to the INI state.
406 */
407 si->exp = TICK_ETERNITY;
408 if (unlikely(!(s->flags & SN_ASSIGNED)))
409 si->state = SI_ST_REQ;
410 else {
411 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
412 si->state = SI_ST_ASS;
413 }
414 return;
415 }
416
417 /* Connection request still in queue... */
418 if (si->flags & SI_FL_EXP) {
419 /* ... and timeout expired */
420 si->exp = TICK_ETERNITY;
421 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
422 if (s->srv)
423 s->srv->failed_conns++;
424 s->be->failed_conns++;
425 si->shutr(si);
426 si->shutw(si);
427 si->ob->flags |= BF_WRITE_TIMEOUT;
428 if (!si->err_type)
429 si->err_type = SI_ET_QUEUE_TO;
430 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100431 if (s->srv_error)
432 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100433 return;
434 }
435
436 /* Connection remains in queue, check if we have to abort it */
437 if ((si->ob->flags & (BF_READ_ERROR|BF_SHUTW_NOW)) || /* abort requested */
438 ((si->ob->flags & BF_SHUTR) && /* empty and client stopped */
439 (si->ob->flags & BF_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
440 /* give up */
441 si->exp = TICK_ETERNITY;
442 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
443 si->shutr(si);
444 si->shutw(si);
445 si->err_type |= SI_ET_QUEUE_ABRT;
446 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100447 if (s->srv_error)
448 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100449 return;
450 }
451
452 /* Nothing changed */
453 return;
454 }
455 else if (si->state == SI_ST_TAR) {
456 /* Connection request might be aborted */
457 if ((si->ob->flags & (BF_READ_ERROR|BF_SHUTW_NOW)) || /* abort requested */
458 ((si->ob->flags & BF_SHUTR) && /* empty and client stopped */
459 (si->ob->flags & BF_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
460 /* give up */
461 si->exp = TICK_ETERNITY;
462 si->shutr(si);
463 si->shutw(si);
464 si->err_type |= SI_ET_CONN_ABRT;
465 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100466 if (s->srv_error)
467 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100468 return;
469 }
470
471 if (!(si->flags & SI_FL_EXP))
472 return; /* still in turn-around */
473
474 si->exp = TICK_ETERNITY;
475
476 /* we keep trying on the same server as long as the session is
477 * marked "assigned".
478 * FIXME: Should we force a redispatch attempt when the server is down ?
479 */
480 if (s->flags & SN_ASSIGNED)
481 si->state = SI_ST_ASS;
482 else
483 si->state = SI_ST_REQ;
484 return;
485 }
486}
487
488/* This function initiates a server connection request on a stream interface
489 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
490 * indicating that a server has been assigned. It may also return SI_ST_QUE,
491 * or SI_ST_CLO upon error.
492 */
493static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
494 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",
495 now_ms, __FUNCTION__,
496 s,
497 s->req, s->rep,
498 s->req->rex, s->rep->wex,
499 s->req->flags, s->rep->flags,
500 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
501
502 if (si->state != SI_ST_REQ)
503 return;
504
505 /* Try to assign a server */
506 if (srv_redispatch_connect(s) != 0) {
507 /* We did not get a server. Either we queued the
508 * connection request, or we encountered an error.
509 */
510 if (si->state == SI_ST_QUE)
511 return;
512
513 /* we did not get any server, let's check the cause */
514 si->shutr(si);
515 si->shutw(si);
516 si->ob->flags |= BF_WRITE_ERROR;
517 if (!si->err_type)
518 si->err_type = SI_ET_CONN_OTHER;
519 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100520 if (s->srv_error)
521 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100522 return;
523 }
524
525 /* The server is assigned */
526 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
527 si->state = SI_ST_ASS;
528}
529
530/* Processes the client, server, request and response jobs of a session task,
531 * then puts it back to the wait queue in a clean state, or cleans up its
532 * resources if it must be deleted. Returns in <next> the date the task wants
533 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
534 * nothing too many times, the request and response buffers flags are monitored
535 * and each function is called only if at least another function has changed at
536 * least one flag it is interested in.
537 */
538void process_session(struct task *t, int *next)
539{
540 struct session *s = t->context;
541 int resync;
542 unsigned int rqf_last, rpf_last;
543
544 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
545 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
546
547 /* 1a: Check for low level timeouts if needed. We just set a flag on
548 * stream interfaces when their timeouts have expired.
549 */
550 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
551 stream_int_check_timeouts(&s->si[0]);
552 stream_int_check_timeouts(&s->si[1]);
553 buffer_check_timeouts(s->req);
554 buffer_check_timeouts(s->rep);
555 }
556
557 /* copy req/rep flags so that we can detect shutdowns */
558 rqf_last = s->req->flags;
559 rpf_last = s->rep->flags;
560
561 /* 1b: check for low-level errors reported at the stream interface.
562 * First we check if it's a retryable error (in which case we don't
563 * want to tell the buffer). Otherwise we report the error one level
564 * upper by setting flags into the buffers. Note that the side towards
565 * the client cannot have connect (hence retryable) errors. Also, the
566 * connection setup code must be able to deal with any type of abort.
567 */
568 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
569 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
570 s->si[0].shutr(&s->si[0]);
571 s->si[0].shutw(&s->si[0]);
572 stream_int_report_error(&s->si[0]);
573 }
574 }
575
576 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
577 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
578 s->si[1].shutr(&s->si[1]);
579 s->si[1].shutw(&s->si[1]);
580 stream_int_report_error(&s->si[1]);
581 s->be->failed_resp++;
582 if (s->srv)
583 s->srv->failed_resp++;
584 }
585 /* note: maybe we should process connection errors here ? */
586 }
587
588 if (s->si[1].state == SI_ST_CON) {
589 /* we were trying to establish a connection on the server side,
590 * maybe it succeeded, maybe it failed, maybe we timed out, ...
591 */
592 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
593 sess_update_st_cer(s, &s->si[1]);
594 else if (s->si[1].state == SI_ST_EST)
595 sess_establish(s, &s->si[1]);
596
597 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
598 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
599 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
600 */
601 }
602
603 /* check buffer timeouts, and close the corresponding stream interfaces
604 * for future reads or writes. Note: this will also concern upper layers
605 * but we do not touch any other flag. We must be careful and correctly
606 * detect state changes when calling them.
607 */
608 if (unlikely(s->req->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) {
609 if (s->req->flags & BF_READ_TIMEOUT)
610 s->req->prod->shutr(s->req->prod);
611 if (s->req->flags & BF_WRITE_TIMEOUT)
612 s->req->cons->shutw(s->req->cons);
613 DPRINTF(stderr,
614 "[%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",
615 now_ms, __FUNCTION__, __LINE__,
616 t,
617 s, s->flags,
618 s->req, s->rep,
619 s->req->rex, s->rep->wex,
620 s->req->flags, s->rep->flags,
621 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
622 s->rep->cons->err_type, s->req->cons->err_type,
623 s->conn_retries);
624 }
625
626 if (unlikely(s->rep->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) {
627 if (s->rep->flags & BF_READ_TIMEOUT)
628 s->rep->prod->shutr(s->rep->prod);
629 if (s->rep->flags & BF_WRITE_TIMEOUT)
630 s->rep->cons->shutw(s->rep->cons);
631 DPRINTF(stderr,
632 "[%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",
633 now_ms, __FUNCTION__, __LINE__,
634 t,
635 s, s->flags,
636 s->req, s->rep,
637 s->req->rex, s->rep->wex,
638 s->req->flags, s->rep->flags,
639 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
640 s->rep->cons->err_type, s->req->cons->err_type,
641 s->conn_retries);
642 }
643
644 /* Check for connection closure */
645
646resync_stream_interface:
647 DPRINTF(stderr,
648 "[%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",
649 now_ms, __FUNCTION__, __LINE__,
650 t,
651 s, s->flags,
652 s->req, s->rep,
653 s->req->rex, s->rep->wex,
654 s->req->flags, s->rep->flags,
655 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
656 s->rep->cons->err_type, s->req->cons->err_type,
657 s->conn_retries);
658
659 /* nothing special to be done on client side */
660 if (unlikely(s->req->prod->state == SI_ST_DIS))
661 s->req->prod->state = SI_ST_CLO;
662
663 /* When a server-side connection is released, we have to count it and
664 * check for pending connections on this server.
665 */
666 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
667 s->req->cons->state = SI_ST_CLO;
668 if (s->srv) {
669 if (s->flags & SN_CURR_SESS) {
670 s->flags &= ~SN_CURR_SESS;
671 s->srv->cur_sess--;
672 }
673 sess_change_server(s, NULL);
674 if (may_dequeue_tasks(s->srv, s->be))
675 process_srv_queue(s->srv);
676 }
677 }
678
679 /*
680 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
681 * at this point.
682 */
683
684 /**** Process layer 7 below ****/
685
686 resync = 0;
687
688 /* Analyse request */
689 if ((s->req->flags & BF_MASK_ANALYSER) ||
690 (s->req->flags ^ rqf_last) & BF_MASK_STATIC) {
691 unsigned int flags = s->req->flags;
692
693 if (s->req->prod->state >= SI_ST_EST) {
694 /* it's up to the analysers to reset write_ena */
695 buffer_write_ena(s->req);
696 if (s->req->analysers)
697 process_request(s);
698 }
699 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
700 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
701 if (s->req->flags != flags)
702 resync = 1;
703 }
704
705 /* reflect what the L7 analysers have seen last */
706 rqf_last = s->req->flags;
707
708 /*
709 * Now forward all shutdown requests between both sides of the buffer
710 */
711
712 /* first, let's check if the request buffer needs to shutdown(write) */
713 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
714 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)))
715 buffer_shutw_now(s->req);
716 else if ((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA) &&
717 (s->req->cons->state == SI_ST_EST) &&
718 s->be->options & PR_O_FORCE_CLO &&
719 s->rep->flags & BF_READ_ACTIVITY) {
720 /* We want to force the connection to the server to close,
721 * and the server has begun to respond. That's the right
722 * time.
723 */
724 buffer_shutw_now(s->req);
725 }
726
727 /* shutdown(write) pending */
728 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW))
729 s->req->cons->shutw(s->req->cons);
730
731 /* shutdown(write) done on server side, we must stop the client too */
732 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW))
733 buffer_shutr_now(s->req);
734
735 /* shutdown(read) pending */
736 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
737 s->req->prod->shutr(s->req->prod);
738
739 /* it's possible that an upper layer has requested a connection setup */
740 if (s->req->cons->state == SI_ST_INI &&
741 (s->req->flags & (BF_WRITE_ENA|BF_SHUTW|BF_SHUTW_NOW)) == BF_WRITE_ENA)
742 s->req->cons->state = SI_ST_REQ;
743
744 /* we may have a pending connection request, or a connection waiting
745 * for completion.
746 */
747 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
748 do {
749 /* nb: step 1 might switch from QUE to ASS, but we first want
750 * to give a chance to step 2 to perform a redirect if needed.
751 */
752 if (s->si[1].state != SI_ST_REQ)
753 sess_update_stream_int(s, &s->si[1]);
754 if (s->si[1].state == SI_ST_REQ)
755 sess_prepare_conn_req(s, &s->si[1]);
756
757 if (s->si[1].state == SI_ST_ASS && s->srv &&
758 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
759 perform_http_redirect(s, &s->si[1]);
760 } while (s->si[1].state == SI_ST_ASS);
761 }
762
763 /*
764 * Here we want to check if we need to resync or not.
765 */
766 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
767 resync = 1;
768
769 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
770
771 /* according to benchmarks, it makes sense to resync now */
772 if (resync)
773 goto resync_stream_interface;
774
775
776 /* Analyse response */
777
778 if (unlikely(s->rep->flags & BF_HIJACK)) {
779 /* In inject mode, we wake up everytime something has
780 * happened on the write side of the buffer.
781 */
782 unsigned int flags = s->rep->flags;
783
784 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
785 !(s->rep->flags & BF_FULL)) {
786 produce_content(s);
787 }
788 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
789 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
790 if (s->rep->flags != flags)
791 resync = 1;
792 }
793 else if ((s->rep->flags & BF_MASK_ANALYSER) ||
794 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC) {
795 unsigned int flags = s->rep->flags;
796
797 if (s->rep->prod->state >= SI_ST_EST) {
798 /* it's up to the analysers to reset write_ena */
799 buffer_write_ena(s->rep);
800 if (s->rep->analysers)
801 process_response(s);
802 }
803 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
804 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
805 if (s->rep->flags != flags)
806 resync = 1;
807 }
808
809 /* reflect what the L7 analysers have seen last */
810 rpf_last = s->rep->flags;
811
812 /*
813 * Now forward all shutdown requests between both sides of the buffer
814 */
815
816 /*
817 * FIXME: this is probably where we should produce error responses.
818 */
819
820 /* first, let's check if the request buffer needs to shutdown(write) */
821 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
822 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)))
823 buffer_shutw_now(s->rep);
824
825 /* shutdown(write) pending */
826 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW))
827 s->rep->cons->shutw(s->rep->cons);
828
829 /* shutdown(write) done on the client side, we must stop the server too */
830 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW))
831 buffer_shutr_now(s->rep);
832
833 /* shutdown(read) pending */
834 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
835 s->rep->prod->shutr(s->rep->prod);
836
837 /*
838 * Here we want to check if we need to resync or not.
839 */
840 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
841 resync = 1;
842
843 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
844
845 if (resync)
846 goto resync_stream_interface;
847
848
849 /* This is needed only when debugging is enabled, to indicate
850 * client-side or server-side close. Please note that in the unlikely
851 * event where both sides would close at once, the sequence is reported
852 * on the server side first.
853 */
854 if (unlikely((global.mode & MODE_DEBUG) &&
855 (!(global.mode & MODE_QUIET) ||
856 (global.mode & MODE_VERBOSE)))) {
857 int len;
858
859 if (s->si[1].state == SI_ST_CLO &&
860 s->si[1].prev_state == SI_ST_EST) {
861 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
862 s->uniq_id, s->be->id,
863 (unsigned short)s->si[0].fd,
864 (unsigned short)s->si[1].fd);
865 write(1, trash, len);
866 }
867
868 if (s->si[0].state == SI_ST_CLO &&
869 s->si[0].prev_state == SI_ST_EST) {
870 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
871 s->uniq_id, s->be->id,
872 (unsigned short)s->si[0].fd,
873 (unsigned short)s->si[1].fd);
874 write(1, trash, len);
875 }
876 }
877
878 if (likely((s->rep->cons->state != SI_ST_CLO) ||
879 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
880
881 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
882 session_process_counters(s);
883
884 if (s->rep->cons->state == SI_ST_EST)
885 stream_sock_data_finish(s->rep->cons->fd);
886
887 if (s->req->cons->state == SI_ST_EST)
888 stream_sock_data_finish(s->req->cons->fd);
889
890 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
891 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
892 s->si[0].prev_state = s->si[0].state;
893 s->si[1].prev_state = s->si[1].state;
894 s->si[0].flags = s->si[1].flags = SI_FL_NONE;
895
896 /* Trick: if a request is being waiting for the server to respond,
897 * and if we know the server can timeout, we don't want the timeout
898 * to expire on the client side first, but we're still interested
899 * in passing data from the client to the server (eg: POST). Thus,
900 * we can cancel the client's request timeout if the server's
901 * request timeout is set and the server has not yet sent a response.
902 */
903
904 if ((s->rep->flags & (BF_WRITE_ENA|BF_SHUTR)) == 0 &&
905 (tick_isset(s->req->wex) || tick_isset(s->rep->rex)))
906 s->req->rex = TICK_ETERNITY;
907
908 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
909 tick_first(s->rep->rex, s->rep->wex));
910 if (s->req->analysers)
911 t->expire = tick_first(t->expire, s->req->analyse_exp);
912
913 if (s->si[0].exp)
914 t->expire = tick_first(t->expire, s->si[0].exp);
915
916 if (s->si[1].exp)
917 t->expire = tick_first(t->expire, s->si[1].exp);
918
919#ifdef DEBUG_FULL
920 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",
921 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);
922#endif
923 /* restore t to its place in the task list */
924 task_queue(t);
925
926#ifdef DEBUG_DEV
927 /* this may only happen when no timeout is set or in case of an FSM bug */
928 if (!t->expire)
929 ABORT_NOW();
930#endif
931 *next = t->expire;
932 return; /* nothing more to do */
933 }
934
935 s->fe->feconn--;
936 if (s->flags & SN_BE_ASSIGNED)
937 s->be->beconn--;
938 actconn--;
939
940 if (unlikely((global.mode & MODE_DEBUG) &&
941 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
942 int len;
943 len = sprintf(trash, "%08x:%s.closed[%04x:%04x] (term_trace=0x%08x)\n",
944 s->uniq_id, s->be->id,
945 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd,
946 s->term_trace);
947 write(1, trash, len);
948 }
949
950 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
951 session_process_counters(s);
952
953 /* let's do a final log if we need it */
954 if (s->logs.logwait &&
955 !(s->flags & SN_MONITOR) &&
956 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100957 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100958 }
959
960 /* the task MUST not be in the run queue anymore */
961 task_delete(t);
962 session_free(s);
963 task_free(t);
964 *next = TICK_ETERNITY;
965}
966
Willy Tarreau7c669d72008-06-20 15:04:11 +0200967/*
968 * This function adjusts sess->srv_conn and maintains the previous and new
969 * server's served session counts. Setting newsrv to NULL is enough to release
970 * current connection slot. This function also notifies any LB algo which might
971 * expect to be informed about any change in the number of active sessions on a
972 * server.
973 */
974void sess_change_server(struct session *sess, struct server *newsrv)
975{
976 if (sess->srv_conn == newsrv)
977 return;
978
979 if (sess->srv_conn) {
980 sess->srv_conn->served--;
981 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
982 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
983 sess->srv_conn = NULL;
984 }
985
986 if (newsrv) {
987 newsrv->served++;
988 if (newsrv->proxy->lbprm.server_take_conn)
989 newsrv->proxy->lbprm.server_take_conn(newsrv);
990 sess->srv_conn = newsrv;
991 }
992}
993
994
Willy Tarreaubaaee002006-06-26 02:48:02 +0200995/*
996 * Local variables:
997 * c-indent-level: 8
998 * c-basic-offset: 8
999 * End:
1000 */