blob: 0df174a1c961239a50eb23563573e53426dc9edb [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 */
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]);
575 }
576 }
577
578 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
579 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
580 s->si[1].shutr(&s->si[1]);
581 s->si[1].shutw(&s->si[1]);
582 stream_int_report_error(&s->si[1]);
583 s->be->failed_resp++;
584 if (s->srv)
585 s->srv->failed_resp++;
586 }
587 /* note: maybe we should process connection errors here ? */
588 }
589
590 if (s->si[1].state == SI_ST_CON) {
591 /* we were trying to establish a connection on the server side,
592 * maybe it succeeded, maybe it failed, maybe we timed out, ...
593 */
594 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
595 sess_update_st_cer(s, &s->si[1]);
596 else if (s->si[1].state == SI_ST_EST)
597 sess_establish(s, &s->si[1]);
598
599 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
600 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
601 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
602 */
603 }
604
605 /* check buffer timeouts, and close the corresponding stream interfaces
606 * for future reads or writes. Note: this will also concern upper layers
607 * but we do not touch any other flag. We must be careful and correctly
608 * detect state changes when calling them.
609 */
610 if (unlikely(s->req->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) {
611 if (s->req->flags & BF_READ_TIMEOUT)
612 s->req->prod->shutr(s->req->prod);
613 if (s->req->flags & BF_WRITE_TIMEOUT)
614 s->req->cons->shutw(s->req->cons);
615 DPRINTF(stderr,
616 "[%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",
617 now_ms, __FUNCTION__, __LINE__,
618 t,
619 s, s->flags,
620 s->req, s->rep,
621 s->req->rex, s->rep->wex,
622 s->req->flags, s->rep->flags,
623 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
624 s->rep->cons->err_type, s->req->cons->err_type,
625 s->conn_retries);
626 }
627
628 if (unlikely(s->rep->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) {
629 if (s->rep->flags & BF_READ_TIMEOUT)
630 s->rep->prod->shutr(s->rep->prod);
631 if (s->rep->flags & BF_WRITE_TIMEOUT)
632 s->rep->cons->shutw(s->rep->cons);
633 DPRINTF(stderr,
634 "[%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",
635 now_ms, __FUNCTION__, __LINE__,
636 t,
637 s, s->flags,
638 s->req, s->rep,
639 s->req->rex, s->rep->wex,
640 s->req->flags, s->rep->flags,
641 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
642 s->rep->cons->err_type, s->req->cons->err_type,
643 s->conn_retries);
644 }
645
646 /* Check for connection closure */
647
648resync_stream_interface:
649 DPRINTF(stderr,
650 "[%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",
651 now_ms, __FUNCTION__, __LINE__,
652 t,
653 s, s->flags,
654 s->req, s->rep,
655 s->req->rex, s->rep->wex,
656 s->req->flags, s->rep->flags,
657 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
658 s->rep->cons->err_type, s->req->cons->err_type,
659 s->conn_retries);
660
661 /* nothing special to be done on client side */
662 if (unlikely(s->req->prod->state == SI_ST_DIS))
663 s->req->prod->state = SI_ST_CLO;
664
665 /* When a server-side connection is released, we have to count it and
666 * check for pending connections on this server.
667 */
668 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
669 s->req->cons->state = SI_ST_CLO;
670 if (s->srv) {
671 if (s->flags & SN_CURR_SESS) {
672 s->flags &= ~SN_CURR_SESS;
673 s->srv->cur_sess--;
674 }
675 sess_change_server(s, NULL);
676 if (may_dequeue_tasks(s->srv, s->be))
677 process_srv_queue(s->srv);
678 }
679 }
680
681 /*
682 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
683 * at this point.
684 */
685
686 /**** Process layer 7 below ****/
687
688 resync = 0;
689
690 /* Analyse request */
691 if ((s->req->flags & BF_MASK_ANALYSER) ||
692 (s->req->flags ^ rqf_last) & BF_MASK_STATIC) {
693 unsigned int flags = s->req->flags;
694
695 if (s->req->prod->state >= SI_ST_EST) {
696 /* it's up to the analysers to reset write_ena */
697 buffer_write_ena(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +0100698
699 /* We will call all analysers for which a bit is set in
700 * s->req->analysers, following the bit order from LSB
701 * to MSB. The analysers must remove themselves from
702 * the list when not needed. This while() loop is in
703 * fact a cleaner if().
704 */
705 while (s->req->analysers) {
706 if (s->req->analysers & AN_REQ_INSPECT)
707 if (!tcp_inspect_request(s, s->req))
708 break;
709
Willy Tarreau59234e92008-11-30 23:51:27 +0100710 if (s->req->analysers & AN_REQ_HTTP_HDR)
711 if (!http_process_request(s, s->req))
Willy Tarreauedcf6682008-11-30 23:15:34 +0100712 break;
713
Willy Tarreau60b85b02008-11-30 23:28:40 +0100714 if (s->req->analysers & AN_REQ_HTTP_TARPIT)
715 if (!http_process_tarpit(s, s->req))
716 break;
717
Willy Tarreaud34af782008-11-30 23:36:37 +0100718 if (s->req->analysers & AN_REQ_HTTP_BODY)
719 if (!http_process_request_body(s, s->req))
720 break;
721
Willy Tarreauedcf6682008-11-30 23:15:34 +0100722 /* Just make sure that nobody set a wrong flag causing an endless loop */
723 s->req->analysers &= AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_TARPIT | AN_REQ_HTTP_BODY;
724
725 /* we don't want to loop anyway */
726 break;
727 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100728 }
729 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
730 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
731 if (s->req->flags != flags)
732 resync = 1;
733 }
734
735 /* reflect what the L7 analysers have seen last */
736 rqf_last = s->req->flags;
737
738 /*
739 * Now forward all shutdown requests between both sides of the buffer
740 */
741
742 /* first, let's check if the request buffer needs to shutdown(write) */
743 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
744 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)))
745 buffer_shutw_now(s->req);
746 else if ((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA) &&
747 (s->req->cons->state == SI_ST_EST) &&
748 s->be->options & PR_O_FORCE_CLO &&
749 s->rep->flags & BF_READ_ACTIVITY) {
750 /* We want to force the connection to the server to close,
751 * and the server has begun to respond. That's the right
752 * time.
753 */
754 buffer_shutw_now(s->req);
755 }
756
757 /* shutdown(write) pending */
758 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW))
759 s->req->cons->shutw(s->req->cons);
760
761 /* shutdown(write) done on server side, we must stop the client too */
762 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW))
763 buffer_shutr_now(s->req);
764
765 /* shutdown(read) pending */
766 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
767 s->req->prod->shutr(s->req->prod);
768
769 /* it's possible that an upper layer has requested a connection setup */
770 if (s->req->cons->state == SI_ST_INI &&
771 (s->req->flags & (BF_WRITE_ENA|BF_SHUTW|BF_SHUTW_NOW)) == BF_WRITE_ENA)
772 s->req->cons->state = SI_ST_REQ;
773
774 /* we may have a pending connection request, or a connection waiting
775 * for completion.
776 */
777 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
778 do {
779 /* nb: step 1 might switch from QUE to ASS, but we first want
780 * to give a chance to step 2 to perform a redirect if needed.
781 */
782 if (s->si[1].state != SI_ST_REQ)
783 sess_update_stream_int(s, &s->si[1]);
784 if (s->si[1].state == SI_ST_REQ)
785 sess_prepare_conn_req(s, &s->si[1]);
786
787 if (s->si[1].state == SI_ST_ASS && s->srv &&
788 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
789 perform_http_redirect(s, &s->si[1]);
790 } while (s->si[1].state == SI_ST_ASS);
791 }
792
793 /*
794 * Here we want to check if we need to resync or not.
795 */
796 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
797 resync = 1;
798
799 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
800
801 /* according to benchmarks, it makes sense to resync now */
802 if (resync)
803 goto resync_stream_interface;
804
805
806 /* Analyse response */
807
808 if (unlikely(s->rep->flags & BF_HIJACK)) {
809 /* In inject mode, we wake up everytime something has
810 * happened on the write side of the buffer.
811 */
812 unsigned int flags = s->rep->flags;
813
814 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
815 !(s->rep->flags & BF_FULL)) {
816 produce_content(s);
817 }
818 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
819 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
820 if (s->rep->flags != flags)
821 resync = 1;
822 }
823 else if ((s->rep->flags & BF_MASK_ANALYSER) ||
824 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC) {
825 unsigned int flags = s->rep->flags;
826
827 if (s->rep->prod->state >= SI_ST_EST) {
828 /* it's up to the analysers to reset write_ena */
829 buffer_write_ena(s->rep);
830 if (s->rep->analysers)
831 process_response(s);
832 }
833 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
834 flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
835 if (s->rep->flags != flags)
836 resync = 1;
837 }
838
839 /* reflect what the L7 analysers have seen last */
840 rpf_last = s->rep->flags;
841
842 /*
843 * Now forward all shutdown requests between both sides of the buffer
844 */
845
846 /*
847 * FIXME: this is probably where we should produce error responses.
848 */
849
850 /* first, let's check if the request buffer needs to shutdown(write) */
851 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
852 (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)))
853 buffer_shutw_now(s->rep);
854
855 /* shutdown(write) pending */
856 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW)) == BF_SHUTW_NOW))
857 s->rep->cons->shutw(s->rep->cons);
858
859 /* shutdown(write) done on the client side, we must stop the server too */
860 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW))
861 buffer_shutr_now(s->rep);
862
863 /* shutdown(read) pending */
864 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
865 s->rep->prod->shutr(s->rep->prod);
866
867 /*
868 * Here we want to check if we need to resync or not.
869 */
870 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
871 resync = 1;
872
873 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
874
875 if (resync)
876 goto resync_stream_interface;
877
878
879 /* This is needed only when debugging is enabled, to indicate
880 * client-side or server-side close. Please note that in the unlikely
881 * event where both sides would close at once, the sequence is reported
882 * on the server side first.
883 */
884 if (unlikely((global.mode & MODE_DEBUG) &&
885 (!(global.mode & MODE_QUIET) ||
886 (global.mode & MODE_VERBOSE)))) {
887 int len;
888
889 if (s->si[1].state == SI_ST_CLO &&
890 s->si[1].prev_state == SI_ST_EST) {
891 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
892 s->uniq_id, s->be->id,
893 (unsigned short)s->si[0].fd,
894 (unsigned short)s->si[1].fd);
895 write(1, trash, len);
896 }
897
898 if (s->si[0].state == SI_ST_CLO &&
899 s->si[0].prev_state == SI_ST_EST) {
900 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
901 s->uniq_id, s->be->id,
902 (unsigned short)s->si[0].fd,
903 (unsigned short)s->si[1].fd);
904 write(1, trash, len);
905 }
906 }
907
908 if (likely((s->rep->cons->state != SI_ST_CLO) ||
909 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
910
911 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
912 session_process_counters(s);
913
914 if (s->rep->cons->state == SI_ST_EST)
Willy Tarreaub0253252008-11-30 21:37:12 +0100915 stream_sock_data_finish(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100916
917 if (s->req->cons->state == SI_ST_EST)
Willy Tarreaub0253252008-11-30 21:37:12 +0100918 stream_sock_data_finish(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100919
920 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
921 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT;
922 s->si[0].prev_state = s->si[0].state;
923 s->si[1].prev_state = s->si[1].state;
924 s->si[0].flags = s->si[1].flags = SI_FL_NONE;
925
926 /* Trick: if a request is being waiting for the server to respond,
927 * and if we know the server can timeout, we don't want the timeout
928 * to expire on the client side first, but we're still interested
929 * in passing data from the client to the server (eg: POST). Thus,
930 * we can cancel the client's request timeout if the server's
931 * request timeout is set and the server has not yet sent a response.
932 */
933
934 if ((s->rep->flags & (BF_WRITE_ENA|BF_SHUTR)) == 0 &&
935 (tick_isset(s->req->wex) || tick_isset(s->rep->rex)))
936 s->req->rex = TICK_ETERNITY;
937
938 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
939 tick_first(s->rep->rex, s->rep->wex));
940 if (s->req->analysers)
941 t->expire = tick_first(t->expire, s->req->analyse_exp);
942
943 if (s->si[0].exp)
944 t->expire = tick_first(t->expire, s->si[0].exp);
945
946 if (s->si[1].exp)
947 t->expire = tick_first(t->expire, s->si[1].exp);
948
949#ifdef DEBUG_FULL
950 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",
951 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);
952#endif
953 /* restore t to its place in the task list */
954 task_queue(t);
955
956#ifdef DEBUG_DEV
957 /* this may only happen when no timeout is set or in case of an FSM bug */
958 if (!t->expire)
959 ABORT_NOW();
960#endif
961 *next = t->expire;
962 return; /* nothing more to do */
963 }
964
965 s->fe->feconn--;
966 if (s->flags & SN_BE_ASSIGNED)
967 s->be->beconn--;
968 actconn--;
969
970 if (unlikely((global.mode & MODE_DEBUG) &&
971 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
972 int len;
973 len = sprintf(trash, "%08x:%s.closed[%04x:%04x] (term_trace=0x%08x)\n",
974 s->uniq_id, s->be->id,
975 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd,
976 s->term_trace);
977 write(1, trash, len);
978 }
979
980 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
981 session_process_counters(s);
982
983 /* let's do a final log if we need it */
984 if (s->logs.logwait &&
985 !(s->flags & SN_MONITOR) &&
986 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100987 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100988 }
989
990 /* the task MUST not be in the run queue anymore */
991 task_delete(t);
992 session_free(s);
993 task_free(t);
994 *next = TICK_ETERNITY;
995}
996
Willy Tarreau7c669d72008-06-20 15:04:11 +0200997/*
998 * This function adjusts sess->srv_conn and maintains the previous and new
999 * server's served session counts. Setting newsrv to NULL is enough to release
1000 * current connection slot. This function also notifies any LB algo which might
1001 * expect to be informed about any change in the number of active sessions on a
1002 * server.
1003 */
1004void sess_change_server(struct session *sess, struct server *newsrv)
1005{
1006 if (sess->srv_conn == newsrv)
1007 return;
1008
1009 if (sess->srv_conn) {
1010 sess->srv_conn->served--;
1011 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
1012 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
1013 sess->srv_conn = NULL;
1014 }
1015
1016 if (newsrv) {
1017 newsrv->served++;
1018 if (newsrv->proxy->lbprm.server_take_conn)
1019 newsrv->proxy->lbprm.server_take_conn(newsrv);
1020 sess->srv_conn = newsrv;
1021 }
1022}
1023
1024
Willy Tarreaubaaee002006-06-26 02:48:02 +02001025/*
1026 * Local variables:
1027 * c-indent-level: 8
1028 * c-basic-offset: 8
1029 * End:
1030 */