blob: 53ee397390c9b857fa1c5d8161a6b7527dca0a5e [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau81f9aa32010-06-01 17:45:26 +02002 * Session management functions.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003 *
Willy Tarreau81f9aa32010-06-01 17:45:26 +02004 * Copyright 2000-2010 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 Tarreau81f9aa32010-06-01 17:45:26 +020014#include <unistd.h>
15#include <fcntl.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020016
17#include <common/config.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020018#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020019#include <common/memory.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020020
Willy Tarreaubaaee002006-06-26 02:48:02 +020021#include <types/capture.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010022#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020023
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020024#include <proto/acl.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010025#include <proto/backend.h>
Willy Tarreau7341d942007-05-13 19:56:02 +020026#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010027#include <proto/checks.h>
Willy Tarreau5ca791d2009-08-16 19:06:42 +020028#include <proto/dumpstats.h>
Willy Tarreau91c43d72010-06-20 11:19:22 +020029#include <proto/freq_ctr.h>
Willy Tarreau3041b9f2010-10-15 23:25:20 +020030#include <proto/frontend.h>
Willy Tarreau8d5d7f22007-01-21 19:16:41 +010031#include <proto/hdr_idx.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020032#include <proto/log.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020033#include <proto/session.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010034#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010035#include <proto/proto_http.h>
36#include <proto/proto_tcp.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020037#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010039#include <proto/server.h>
Emeric Brun1d33b292010-01-04 15:47:17 +010040#include <proto/stick_table.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010041#include <proto/stream_interface.h>
42#include <proto/stream_sock.h>
43#include <proto/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020045struct pool_head *pool2_session;
Willy Tarreauf54f8bd2008-11-23 19:53:55 +010046struct list sessions;
Willy Tarreaubaaee002006-06-26 02:48:02 +020047
Willy Tarreau81f9aa32010-06-01 17:45:26 +020048/* This function is called from the protocol layer accept() in order to instanciate
49 * a new session on behalf of a given listener and frontend. It returns a positive
50 * value upon success, 0 if the connection needs to be closed and ignored, or a
51 * negative value upon critical failure.
52 */
53int session_accept(struct listener *l, int cfd, struct sockaddr_storage *addr)
54{
55 struct proxy *p = l->frontend;
56 struct session *s;
57 struct http_txn *txn;
58 struct task *t;
59
60 if (unlikely((s = pool_alloc2(pool2_session)) == NULL)) {
61 Alert("out of memory in event_accept().\n");
62 goto out_close;
63 }
64
65 /* minimum session initialization required for monitor mode below */
66 s->flags = 0;
67 s->logs.logwait = p->to_log;
Willy Tarreau56123282010-08-06 19:06:56 +020068 s->stkctr1_entry = NULL;
69 s->stkctr2_entry = NULL;
70 s->stkctr1_table = NULL;
71 s->stkctr2_table = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020072
73 /* if this session comes from a known monitoring system, we want to ignore
74 * it as soon as possible, which means closing it immediately for TCP, but
75 * cleanly.
76 */
77 if (unlikely((l->options & LI_O_CHK_MONNET) &&
78 addr->ss_family == AF_INET &&
79 (((struct sockaddr_in *)addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr)) {
80 if (p->mode == PR_MODE_TCP) {
81 pool_free2(pool2_session, s);
82 return 0;
83 }
84 s->flags |= SN_MONITOR;
85 s->logs.logwait = 0;
86 }
87
88 /* OK, we're keeping the session, so let's properly initialize the session */
89 LIST_ADDQ(&sessions, &s->list);
90 LIST_INIT(&s->back_refs);
91
92 if (unlikely((t = task_new()) == NULL)) { /* disable this proxy for a while */
93 Alert("out of memory in event_accept().\n");
94 goto out_free_session;
95 }
96
97 s->term_trace = 0;
98 s->cli_addr = *addr;
99 s->logs.accept_date = date; /* user-visible date for logging */
100 s->logs.tv_accept = now; /* corrected date for internal use */
101 s->uniq_id = totalconn;
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200102 p->feconn++; /* beconn will be increased once assigned */
103
Willy Tarreaub36b4242010-06-04 20:59:39 +0200104 proxy_inc_fe_conn_ctr(l, p); /* note: cum_beconn will be increased once assigned */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200105
106 t->process = l->handler;
107 t->context = s;
108 t->nice = l->nice;
109 t->expire = TICK_ETERNITY;
110
111 s->task = t;
112 s->listener = l;
113
114 /* Note: initially, the session's backend points to the frontend.
115 * This changes later when switching rules are executed or
116 * when the default backend is assigned.
117 */
118 s->be = s->fe = p;
119 s->req = s->rep = NULL; /* will be allocated later */
120
121 /* now evaluate the tcp-request layer4 rules. Since we expect to be able
122 * to abort right here as soon as possible, we check the rules before
123 * even initializing the stream interfaces.
124 */
125 if ((l->options & LI_O_TCP_RULES) && !tcp_exec_req_rules(s)) {
Willy Tarreau56123282010-08-06 19:06:56 +0200126 if (s->stkctr1_entry || s->stkctr2_entry)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200127 session_store_counters(s);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200128 task_free(t);
129 LIST_DEL(&s->list);
130 pool_free2(pool2_session, s);
131 /* let's do a no-linger now to close with a single RST. */
132 setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200133 p->feconn--;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200134 return 0;
135 }
136
Willy Tarreaub36b4242010-06-04 20:59:39 +0200137 /* This session was accepted, count it now */
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200138 if (p->feconn > p->counters.feconn_max)
139 p->counters.feconn_max = p->feconn;
Willy Tarreaub36b4242010-06-04 20:59:39 +0200140 proxy_inc_fe_sess_ctr(l, p);
Willy Tarreau56123282010-08-06 19:06:56 +0200141 if (s->stkctr1_entry) {
Willy Tarreau91c43d72010-06-20 11:19:22 +0200142 void *ptr;
143
Willy Tarreau56123282010-08-06 19:06:56 +0200144 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200145 if (ptr)
146 stktable_data_cast(ptr, sess_cnt)++;
Willy Tarreau91c43d72010-06-20 11:19:22 +0200147
Willy Tarreau56123282010-08-06 19:06:56 +0200148 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +0200149 if (ptr)
150 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200151 s->stkctr1_table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200152 }
Willy Tarreaub36b4242010-06-04 20:59:39 +0200153
Willy Tarreau56123282010-08-06 19:06:56 +0200154 if (s->stkctr2_entry) {
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200155 void *ptr;
156
Willy Tarreau56123282010-08-06 19:06:56 +0200157 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_SESS_CNT);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200158 if (ptr)
159 stktable_data_cast(ptr, sess_cnt)++;
160
Willy Tarreau56123282010-08-06 19:06:56 +0200161 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_SESS_RATE);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200162 if (ptr)
163 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200164 s->stkctr2_table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200165 }
166
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200167 /* this part should be common with other protocols */
168 s->si[0].fd = cfd;
169 s->si[0].owner = t;
170 s->si[0].state = s->si[0].prev_state = SI_ST_EST;
171 s->si[0].err_type = SI_ET_NONE;
172 s->si[0].err_loc = NULL;
173 s->si[0].connect = NULL;
174 s->si[0].iohandler = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200175 s->si[0].release = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200176 s->si[0].exp = TICK_ETERNITY;
177 s->si[0].flags = SI_FL_NONE;
178
179 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
180 s->si[0].flags |= SI_FL_INDEP_STR;
181
182 if (addr->ss_family == AF_INET || addr->ss_family == AF_INET6)
183 s->si[0].flags = SI_FL_CAP_SPLTCP; /* TCP/TCPv6 splicing possible */
184
185 /* add the various callbacks */
186 stream_sock_prepare_interface(&s->si[0]);
187
188 /* pre-initialize the other side's stream interface to an INIT state. The
189 * callbacks will be initialized before attempting to connect.
190 */
191 s->si[1].fd = -1; /* just to help with debugging */
192 s->si[1].owner = t;
193 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
194 s->si[1].err_type = SI_ET_NONE;
195 s->si[1].err_loc = NULL;
196 s->si[1].connect = NULL;
197 s->si[1].iohandler = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200198 s->si[1].release = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200199 s->si[1].shutr = stream_int_shutr;
200 s->si[1].shutw = stream_int_shutw;
201 s->si[1].exp = TICK_ETERNITY;
202 s->si[1].flags = SI_FL_NONE;
203
204 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
205 s->si[1].flags |= SI_FL_INDEP_STR;
206
207 s->srv = s->prev_srv = s->srv_conn = NULL;
208 s->pend_pos = NULL;
209
210 /* init store persistence */
211 s->store_count = 0;
212
213 /* Adjust some socket options */
214 if (unlikely(fcntl(cfd, F_SETFL, O_NONBLOCK) == -1)) {
215 Alert("accept(): cannot set the socket in non blocking mode. Giving up\n");
216 goto out_free_task;
217 }
218
219 txn = &s->txn;
220 /* Those variables will be checked and freed if non-NULL in
221 * session.c:session_free(). It is important that they are
222 * properly initialized.
223 */
224 txn->sessid = NULL;
225 txn->srv_cookie = NULL;
226 txn->cli_cookie = NULL;
227 txn->uri = NULL;
228 txn->req.cap = NULL;
229 txn->rsp.cap = NULL;
230 txn->hdr_idx.v = NULL;
231 txn->hdr_idx.size = txn->hdr_idx.used = 0;
232
233 if (unlikely((s->req = pool_alloc2(pool2_buffer)) == NULL))
234 goto out_free_task; /* no memory */
235
236 if (unlikely((s->rep = pool_alloc2(pool2_buffer)) == NULL))
237 goto out_free_req; /* no memory */
238
239 /* initialize the request buffer */
240 s->req->size = global.tune.bufsize;
241 buffer_init(s->req);
242 s->req->prod = &s->si[0];
243 s->req->cons = &s->si[1];
244 s->si[0].ib = s->si[1].ob = s->req;
245 s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */
246
247 /* activate default analysers enabled for this listener */
248 s->req->analysers = l->analysers;
249
250 s->req->wto = TICK_ETERNITY;
251 s->req->rto = TICK_ETERNITY;
252 s->req->rex = TICK_ETERNITY;
253 s->req->wex = TICK_ETERNITY;
254 s->req->analyse_exp = TICK_ETERNITY;
255
256 /* initialize response buffer */
257 s->rep->size = global.tune.bufsize;
258 buffer_init(s->rep);
259 s->rep->prod = &s->si[1];
260 s->rep->cons = &s->si[0];
261 s->si[0].ob = s->si[1].ib = s->rep;
262 s->rep->analysers = 0;
263
264 s->rep->rto = TICK_ETERNITY;
265 s->rep->wto = TICK_ETERNITY;
266 s->rep->rex = TICK_ETERNITY;
267 s->rep->wex = TICK_ETERNITY;
268 s->rep->analyse_exp = TICK_ETERNITY;
269
270 /* finish initialization of the accepted file descriptor */
271 fd_insert(cfd);
272 fdtab[cfd].owner = &s->si[0];
273 fdtab[cfd].state = FD_STREADY;
274 fdtab[cfd].flags = 0;
275 fdtab[cfd].cb[DIR_RD].f = l->proto->read;
276 fdtab[cfd].cb[DIR_RD].b = s->req;
277 fdtab[cfd].cb[DIR_WR].f = l->proto->write;
278 fdtab[cfd].cb[DIR_WR].b = s->rep;
279 fdinfo[cfd].peeraddr = (struct sockaddr *)&s->cli_addr;
280 fdinfo[cfd].peerlen = sizeof(s->cli_addr);
281 EV_FD_SET(cfd, DIR_RD);
282
283 if (p->accept) {
284 int ret = p->accept(s);
285 if (unlikely(ret < 0))
286 goto out_free_rep;
287
288 if (unlikely(ret == 0)) {
289 /* work is finished, we can release everything (eg: monitoring) */
290 pool_free2(pool2_buffer, s->rep);
291 pool_free2(pool2_buffer, s->req);
Willy Tarreau56123282010-08-06 19:06:56 +0200292 if (s->stkctr1_entry || s->stkctr2_entry)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200293 session_store_counters(s);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200294 task_free(t);
295 LIST_DEL(&s->list);
296 pool_free2(pool2_session, s);
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200297 p->feconn--;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200298 return 0;
299 }
300 }
301
302 /* it is important not to call the wakeup function directly but to
303 * pass through task_wakeup(), because this one knows how to apply
304 * priorities to tasks.
305 */
306 task_wakeup(t, TASK_WOKEN_INIT);
307 return 1;
308
309 /* Error unrolling */
310 out_free_rep:
311 pool_free2(pool2_buffer, s->rep);
312 out_free_req:
313 pool_free2(pool2_buffer, s->req);
314 out_free_task:
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200315 p->feconn--;
Willy Tarreau56123282010-08-06 19:06:56 +0200316 if (s->stkctr1_entry || s->stkctr2_entry)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200317 session_store_counters(s);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200318 task_free(t);
319 out_free_session:
320 LIST_DEL(&s->list);
321 pool_free2(pool2_session, s);
322 out_close:
323 return -1;
324}
325
Willy Tarreaubaaee002006-06-26 02:48:02 +0200326/*
327 * frees the context associated to a session. It must have been removed first.
328 */
329void session_free(struct session *s)
330{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +0100331 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +0200332 struct proxy *fe = s->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100333 struct bref *bref, *back;
Willy Tarreaua4cda672010-06-06 18:28:49 +0200334 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100335
Willy Tarreaubaaee002006-06-26 02:48:02 +0200336 if (s->pend_pos)
337 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100338
Willy Tarreau1e62de62008-11-11 20:20:02 +0100339 if (s->srv) { /* there may be requests left pending in queue */
340 if (s->flags & SN_CURR_SESS) {
341 s->flags &= ~SN_CURR_SESS;
342 s->srv->cur_sess--;
343 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100344 if (may_dequeue_tasks(s->srv, s->be))
345 process_srv_queue(s->srv);
Willy Tarreau1e62de62008-11-11 20:20:02 +0100346 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100347
Willy Tarreau7c669d72008-06-20 15:04:11 +0200348 if (unlikely(s->srv_conn)) {
349 /* the session still has a reserved slot on a server, but
350 * it should normally be only the same as the one above,
351 * so this should not happen in fact.
352 */
353 sess_change_server(s, NULL);
354 }
355
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100356 if (s->req->pipe)
357 put_pipe(s->req->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100358
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100359 if (s->rep->pipe)
360 put_pipe(s->rep->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100361
Willy Tarreau48d63db2008-08-03 17:41:33 +0200362 pool_free2(pool2_buffer, s->req);
363 pool_free2(pool2_buffer, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200364
Willy Tarreau46023632010-01-07 22:51:47 +0100365 http_end_txn(s);
366
Willy Tarreaua4cda672010-06-06 18:28:49 +0200367 for (i = 0; i < s->store_count; i++) {
368 if (!s->store[i].ts)
369 continue;
370 stksess_free(s->store[i].table, s->store[i].ts);
371 s->store[i].ts = NULL;
372 }
373
Willy Tarreau92fb9832007-10-16 17:34:28 +0200374 if (fe) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200375 pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau46023632010-01-07 22:51:47 +0100376 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
377 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200378 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100379
Willy Tarreau56123282010-08-06 19:06:56 +0200380 if (s->stkctr1_entry || s->stkctr2_entry)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200381 session_store_counters(s);
382
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100383 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100384 /* we have to unlink all watchers. We must not relink them if
385 * this session was the last one in the list.
386 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100387 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100388 LIST_INIT(&bref->users);
389 if (s->list.n != &sessions)
390 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100391 bref->ref = s->list.n;
392 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100393 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200394 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200395
396 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200397 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200398 pool_flush2(pool2_buffer);
399 pool_flush2(fe->hdr_idx_pool);
400 pool_flush2(pool2_requri);
401 pool_flush2(pool2_capture);
402 pool_flush2(pool2_session);
403 pool_flush2(fe->req_cap_pool);
404 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200405 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200406}
407
408
409/* perform minimal intializations, report 0 in case of error, 1 if OK. */
410int init_session()
411{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100412 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200413 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
414 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200415}
416
Willy Tarreau30e71012007-11-26 20:15:35 +0100417void session_process_counters(struct session *s)
418{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100419 unsigned long long bytes;
420
Willy Tarreau30e71012007-11-26 20:15:35 +0100421 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100422 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100423 s->logs.bytes_in = s->req->total;
424 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200425 s->fe->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100426
Willy Tarreau30e71012007-11-26 20:15:35 +0100427 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200428 s->be->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100429
Willy Tarreau30e71012007-11-26 20:15:35 +0100430 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200431 s->srv->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200432
433 if (s->listener->counters)
434 s->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200435
Willy Tarreau56123282010-08-06 19:06:56 +0200436 if (s->stkctr2_entry) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200437 void *ptr;
438
Willy Tarreau56123282010-08-06 19:06:56 +0200439 ptr = stktable_data_ptr(s->stkctr2_table,
440 s->stkctr2_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200441 STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200442 if (ptr)
443 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200444
Willy Tarreau56123282010-08-06 19:06:56 +0200445 ptr = stktable_data_ptr(s->stkctr2_table,
446 s->stkctr2_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200447 STKTABLE_DT_BYTES_IN_RATE);
448 if (ptr)
449 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200450 s->stkctr2_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200451 }
452
Willy Tarreau56123282010-08-06 19:06:56 +0200453 if (s->stkctr1_entry) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200454 void *ptr;
455
Willy Tarreau56123282010-08-06 19:06:56 +0200456 ptr = stktable_data_ptr(s->stkctr1_table,
457 s->stkctr1_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200458 STKTABLE_DT_BYTES_IN_CNT);
459 if (ptr)
460 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
461
Willy Tarreau56123282010-08-06 19:06:56 +0200462 ptr = stktable_data_ptr(s->stkctr1_table,
463 s->stkctr1_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200464 STKTABLE_DT_BYTES_IN_RATE);
465 if (ptr)
466 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200467 s->stkctr1_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200468 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100469 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100470 }
471
Willy Tarreau30e71012007-11-26 20:15:35 +0100472 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100473 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100474 s->logs.bytes_out = s->rep->total;
475 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200476 s->fe->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100477
Willy Tarreau30e71012007-11-26 20:15:35 +0100478 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200479 s->be->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100480
Willy Tarreau30e71012007-11-26 20:15:35 +0100481 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200482 s->srv->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200483
484 if (s->listener->counters)
485 s->listener->counters->bytes_out += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200486
Willy Tarreau56123282010-08-06 19:06:56 +0200487 if (s->stkctr2_entry) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200488 void *ptr;
489
Willy Tarreau56123282010-08-06 19:06:56 +0200490 ptr = stktable_data_ptr(s->stkctr2_table,
491 s->stkctr2_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200492 STKTABLE_DT_BYTES_OUT_CNT);
493 if (ptr)
494 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
495
Willy Tarreau56123282010-08-06 19:06:56 +0200496 ptr = stktable_data_ptr(s->stkctr2_table,
497 s->stkctr2_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200498 STKTABLE_DT_BYTES_OUT_RATE);
499 if (ptr)
500 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200501 s->stkctr2_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200502 }
503
Willy Tarreau56123282010-08-06 19:06:56 +0200504 if (s->stkctr1_entry) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200505 void *ptr;
506
Willy Tarreau56123282010-08-06 19:06:56 +0200507 ptr = stktable_data_ptr(s->stkctr1_table,
508 s->stkctr1_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200509 STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200510 if (ptr)
511 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200512
Willy Tarreau56123282010-08-06 19:06:56 +0200513 ptr = stktable_data_ptr(s->stkctr1_table,
514 s->stkctr1_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200515 STKTABLE_DT_BYTES_OUT_RATE);
516 if (ptr)
517 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200518 s->stkctr1_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200519 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100520 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100521 }
522}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200523
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100524/* This function is called with (si->state == SI_ST_CON) meaning that a
525 * connection was attempted and that the file descriptor is already allocated.
526 * We must check for establishment, error and abort. Possible output states
527 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
528 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
529 * otherwise 1.
530 */
531int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
532{
533 struct buffer *req = si->ob;
534 struct buffer *rep = si->ib;
535
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100536 /* If we got an error, or if nothing happened and the connection timed
537 * out, we must give up. The CER state handler will take care of retry
538 * attempts and error reports.
539 */
540 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau127334e2009-03-28 10:47:26 +0100541 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100542 si->state = SI_ST_CER;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200543 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100544 fd_delete(si->fd);
545
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200546 if (si->release)
547 si->release(si);
548
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100549 if (si->err_type)
550 return 0;
551
552 si->err_loc = s->srv;
553 if (si->flags & SI_FL_ERR)
554 si->err_type = SI_ET_CONN_ERR;
555 else
556 si->err_type = SI_ET_CONN_TO;
557 return 0;
558 }
559
560 /* OK, maybe we want to abort */
Willy Tarreau418fd472009-09-06 21:37:23 +0200561 if (unlikely((rep->flags & BF_SHUTW) ||
562 ((req->flags & BF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200563 (((req->flags & (BF_OUT_EMPTY|BF_WRITE_ACTIVITY)) == BF_OUT_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100564 s->be->options & PR_O_ABRT_CLOSE)))) {
565 /* give up */
566 si->shutw(si);
567 si->err_type |= SI_ET_CONN_ABRT;
568 si->err_loc = s->srv;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200569 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau84455332009-03-15 22:34:05 +0100570 if (s->srv_error)
571 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100572 return 1;
573 }
574
575 /* we need to wait a bit more if there was no activity either */
576 if (!(req->flags & BF_WRITE_ACTIVITY))
577 return 1;
578
579 /* OK, this means that a connection succeeded. The caller will be
580 * responsible for handling the transition from CON to EST.
581 */
582 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100583 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100584 si->state = SI_ST_EST;
585 si->err_type = SI_ET_NONE;
586 si->err_loc = NULL;
587 return 1;
588}
589
590/* This function is called with (si->state == SI_ST_CER) meaning that a
591 * previous connection attempt has failed and that the file descriptor
592 * has already been released. Possible causes include asynchronous error
593 * notification and time out. Possible output states are SI_ST_CLO when
594 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
595 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
596 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
597 * marked as in error state. It returns 0.
598 */
599int sess_update_st_cer(struct session *s, struct stream_interface *si)
600{
601 /* we probably have to release last session from the server */
602 if (s->srv) {
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100603 health_adjust(s->srv, HANA_STATUS_L4_ERR);
604
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100605 if (s->flags & SN_CURR_SESS) {
606 s->flags &= ~SN_CURR_SESS;
607 s->srv->cur_sess--;
608 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100609 }
610
611 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200612 si->conn_retries--;
613 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100614 if (!si->err_type) {
615 si->err_type = SI_ET_CONN_ERR;
616 si->err_loc = s->srv;
617 }
618
619 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200620 s->srv->counters.failed_conns++;
621 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100622 if (may_dequeue_tasks(s->srv, s->be))
623 process_srv_queue(s->srv);
624
625 /* shutw is enough so stop a connecting socket */
626 si->shutw(si);
627 si->ob->flags |= BF_WRITE_ERROR;
628 si->ib->flags |= BF_READ_ERROR;
629
630 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100631 if (s->srv_error)
632 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100633 return 0;
634 }
635
636 /* If the "redispatch" option is set on the backend, we are allowed to
637 * retry on another server for the last retry. In order to achieve this,
638 * we must mark the session unassigned, and eventually clear the DIRECT
639 * bit to ignore any persistence cookie. We won't count a retry nor a
640 * redispatch yet, because this will depend on what server is selected.
641 */
Willy Tarreauee28de02010-06-01 09:51:00 +0200642 if (s->srv && si->conn_retries == 0 &&
Willy Tarreau4de91492010-01-22 19:10:05 +0100643 s->be->options & PR_O_REDISP && !(s->flags & SN_FORCE_PRST)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100644 if (may_dequeue_tasks(s->srv, s->be))
645 process_srv_queue(s->srv);
646
647 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
648 s->prev_srv = s->srv;
649 si->state = SI_ST_REQ;
650 } else {
651 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200652 s->srv->counters.retries++;
653 s->be->counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100654 si->state = SI_ST_ASS;
655 }
656
657 if (si->flags & SI_FL_ERR) {
658 /* The error was an asynchronous connection error, and we will
659 * likely have to retry connecting to the same server, most
660 * likely leading to the same result. To avoid this, we wait
661 * one second before retrying.
662 */
663
664 if (!si->err_type)
665 si->err_type = SI_ET_CONN_ERR;
666
667 si->state = SI_ST_TAR;
668 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
669 return 0;
670 }
671 return 0;
672}
673
674/*
675 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200676 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
677 * SI_ST_INI) to SI_ST_EST, but only when a ->connect function is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100678 */
679void sess_establish(struct session *s, struct stream_interface *si)
680{
681 struct buffer *req = si->ob;
682 struct buffer *rep = si->ib;
683
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100684 if (s->srv)
685 health_adjust(s->srv, HANA_STATUS_L4_OK);
686
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100687 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100688 /* if the user wants to log as soon as possible, without counting
689 * bytes from the server, then this is the right moment. */
690 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
691 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100692 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100693 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100694 }
695 else {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100696 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
697 /* reset hdr_idx which was already initialized by the request.
698 * right now, the http parser does it.
699 * hdr_idx_init(&s->txn.hdr_idx);
700 */
701 }
702
Willy Tarreau4e5b8282009-08-16 22:57:50 +0200703 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100704 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
Willy Tarreaud04e8582010-05-31 12:31:35 +0200705 if (si->connect) {
706 /* real connections have timeouts */
707 req->wto = s->be->timeout.server;
708 rep->rto = s->be->timeout.server;
709 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100710 req->wex = TICK_ETERNITY;
711}
712
713/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
714 * Other input states are simply ignored.
715 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
716 * Flags must have previously been updated for timeouts and other conditions.
717 */
718void sess_update_stream_int(struct session *s, struct stream_interface *si)
719{
720 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",
721 now_ms, __FUNCTION__,
722 s,
723 s->req, s->rep,
724 s->req->rex, s->rep->wex,
725 s->req->flags, s->rep->flags,
726 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
727
728 if (si->state == SI_ST_ASS) {
729 /* Server assigned to connection request, we have to try to connect now */
730 int conn_err;
731
732 conn_err = connect_server(s);
733 if (conn_err == SN_ERR_NONE) {
734 /* state = SI_ST_CON now */
Willy Tarreau8f6457c2008-12-01 00:08:28 +0100735 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100736 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100737 return;
738 }
739
740 /* We have received a synchronous error. We might have to
741 * abort, retry immediately or redispatch.
742 */
743 if (conn_err == SN_ERR_INTERNAL) {
744 if (!si->err_type) {
745 si->err_type = SI_ET_CONN_OTHER;
746 si->err_loc = s->srv;
747 }
748
749 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100750 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100751 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200752 s->srv->counters.failed_conns++;
753 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100754
755 /* release other sessions waiting for this server */
756 if (may_dequeue_tasks(s->srv, s->be))
757 process_srv_queue(s->srv);
758
759 /* Failed and not retryable. */
760 si->shutr(si);
761 si->shutw(si);
762 si->ob->flags |= BF_WRITE_ERROR;
763
764 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
765
766 /* no session was ever accounted for this server */
767 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100768 if (s->srv_error)
769 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100770 return;
771 }
772
773 /* We are facing a retryable error, but we don't want to run a
774 * turn-around now, as the problem is likely a source port
775 * allocation problem, so we want to retry now.
776 */
777 si->state = SI_ST_CER;
778 si->flags &= ~SI_FL_ERR;
779 sess_update_st_cer(s, si);
780 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
781 return;
782 }
783 else if (si->state == SI_ST_QUE) {
784 /* connection request was queued, check for any update */
785 if (!s->pend_pos) {
786 /* The connection is not in the queue anymore. Either
787 * we have a server connection slot available and we
788 * go directly to the assigned state, or we need to
789 * load-balance first and go to the INI state.
790 */
791 si->exp = TICK_ETERNITY;
792 if (unlikely(!(s->flags & SN_ASSIGNED)))
793 si->state = SI_ST_REQ;
794 else {
795 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
796 si->state = SI_ST_ASS;
797 }
798 return;
799 }
800
801 /* Connection request still in queue... */
802 if (si->flags & SI_FL_EXP) {
803 /* ... and timeout expired */
804 si->exp = TICK_ETERNITY;
805 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
806 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200807 s->srv->counters.failed_conns++;
808 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100809 si->shutr(si);
810 si->shutw(si);
811 si->ob->flags |= BF_WRITE_TIMEOUT;
812 if (!si->err_type)
813 si->err_type = SI_ET_QUEUE_TO;
814 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100815 if (s->srv_error)
816 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100817 return;
818 }
819
820 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau418fd472009-09-06 21:37:23 +0200821 if ((si->ob->flags & (BF_READ_ERROR)) ||
822 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200823 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100824 /* give up */
825 si->exp = TICK_ETERNITY;
826 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
827 si->shutr(si);
828 si->shutw(si);
829 si->err_type |= SI_ET_QUEUE_ABRT;
830 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100831 if (s->srv_error)
832 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100833 return;
834 }
835
836 /* Nothing changed */
837 return;
838 }
839 else if (si->state == SI_ST_TAR) {
840 /* Connection request might be aborted */
Willy Tarreau418fd472009-09-06 21:37:23 +0200841 if ((si->ob->flags & (BF_READ_ERROR)) ||
842 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200843 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100844 /* give up */
845 si->exp = TICK_ETERNITY;
846 si->shutr(si);
847 si->shutw(si);
848 si->err_type |= SI_ET_CONN_ABRT;
849 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100850 if (s->srv_error)
851 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100852 return;
853 }
854
855 if (!(si->flags & SI_FL_EXP))
856 return; /* still in turn-around */
857
858 si->exp = TICK_ETERNITY;
859
860 /* we keep trying on the same server as long as the session is
861 * marked "assigned".
862 * FIXME: Should we force a redispatch attempt when the server is down ?
863 */
864 if (s->flags & SN_ASSIGNED)
865 si->state = SI_ST_ASS;
866 else
867 si->state = SI_ST_REQ;
868 return;
869 }
870}
871
872/* This function initiates a server connection request on a stream interface
873 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
874 * indicating that a server has been assigned. It may also return SI_ST_QUE,
875 * or SI_ST_CLO upon error.
876 */
877static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
878 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",
879 now_ms, __FUNCTION__,
880 s,
881 s->req, s->rep,
882 s->req->rex, s->rep->wex,
883 s->req->flags, s->rep->flags,
884 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
885
886 if (si->state != SI_ST_REQ)
887 return;
888
889 /* Try to assign a server */
890 if (srv_redispatch_connect(s) != 0) {
891 /* We did not get a server. Either we queued the
892 * connection request, or we encountered an error.
893 */
894 if (si->state == SI_ST_QUE)
895 return;
896
897 /* we did not get any server, let's check the cause */
898 si->shutr(si);
899 si->shutw(si);
900 si->ob->flags |= BF_WRITE_ERROR;
901 if (!si->err_type)
902 si->err_type = SI_ET_CONN_OTHER;
903 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100904 if (s->srv_error)
905 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100906 return;
907 }
908
909 /* The server is assigned */
910 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
911 si->state = SI_ST_ASS;
912}
913
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200914/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +0100915 * if appropriate. The default_backend rule is also considered, then the
916 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200917 * It returns 1 if the processing can continue on next analysers, or zero if it
918 * either needs more data or wants to immediately abort the request.
919 */
920int process_switching_rules(struct session *s, struct buffer *req, int an_bit)
921{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200922 struct persist_rule *prst_rule;
Willy Tarreau4de91492010-01-22 19:10:05 +0100923
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200924 req->analysers &= ~an_bit;
925 req->analyse_exp = TICK_ETERNITY;
926
927 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
928 now_ms, __FUNCTION__,
929 s,
930 req,
931 req->rex, req->wex,
932 req->flags,
933 req->l,
934 req->analysers);
935
936 /* now check whether we have some switching rules for this request */
937 if (!(s->flags & SN_BE_ASSIGNED)) {
938 struct switching_rule *rule;
939
940 list_for_each_entry(rule, &s->fe->switching_rules, list) {
941 int ret;
942
943 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ);
944 ret = acl_pass(ret);
945 if (rule->cond->pol == ACL_COND_UNLESS)
946 ret = !ret;
947
948 if (ret) {
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200949 if (!session_set_backend(s, rule->be.backend))
950 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200951 break;
952 }
953 }
954
955 /* To ensure correct connection accounting on the backend, we
956 * have to assign one if it was not set (eg: a listen). This
957 * measure also takes care of correctly setting the default
958 * backend if any.
959 */
960 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200961 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
962 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200963 }
964
Willy Tarreaufb356202010-08-03 14:02:05 +0200965 /* we don't want to run the TCP or HTTP filters again if the backend has not changed */
966 if (s->fe == s->be) {
967 s->req->analysers &= ~AN_REQ_INSPECT_BE;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200968 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +0200969 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200970
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200971 /* as soon as we know the backend, we must check if we have a matching forced or ignored
Willy Tarreau4de91492010-01-22 19:10:05 +0100972 * persistence rule, and report that in the session.
973 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200974 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +0100975 int ret = 1;
976
977 if (prst_rule->cond) {
978 ret = acl_exec_cond(prst_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
979 ret = acl_pass(ret);
980 if (prst_rule->cond->pol == ACL_COND_UNLESS)
981 ret = !ret;
982 }
983
984 if (ret) {
985 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200986 if (prst_rule->type == PERSIST_TYPE_FORCE) {
987 s->flags |= SN_FORCE_PRST;
988 } else {
989 s->flags |= SN_IGNORE_PRST;
990 }
Willy Tarreau4de91492010-01-22 19:10:05 +0100991 break;
992 }
993 }
994
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200995 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200996
997 sw_failed:
998 /* immediately abort this request in case of allocation failure */
999 buffer_abort(s->req);
1000 buffer_abort(s->rep);
1001
1002 if (!(s->flags & SN_ERR_MASK))
1003 s->flags |= SN_ERR_RESOURCE;
1004 if (!(s->flags & SN_FINST_MASK))
1005 s->flags |= SN_FINST_R;
1006
1007 s->txn.status = 500;
1008 s->req->analysers = 0;
1009 s->req->analyse_exp = TICK_ETERNITY;
1010 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001011}
1012
Emeric Brun1d33b292010-01-04 15:47:17 +01001013/* This stream analyser works on a request. It applies all sticking rules on
1014 * it then returns 1. The data must already be present in the buffer otherwise
1015 * they won't match. It always returns 1.
1016 */
1017int process_sticking_rules(struct session *s, struct buffer *req, int an_bit)
1018{
1019 struct proxy *px = s->be;
1020 struct sticking_rule *rule;
1021
1022 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1023 now_ms, __FUNCTION__,
1024 s,
1025 req,
1026 req->rex, req->wex,
1027 req->flags,
1028 req->l,
1029 req->analysers);
1030
1031 list_for_each_entry(rule, &px->sticking_rules, list) {
1032 int ret = 1 ;
1033 int i;
1034
1035 for (i = 0; i < s->store_count; i++) {
1036 if (rule->table.t == s->store[i].table)
1037 break;
1038 }
1039
1040 if (i != s->store_count)
1041 continue;
1042
1043 if (rule->cond) {
1044 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_REQ);
1045 ret = acl_pass(ret);
1046 if (rule->cond->pol == ACL_COND_UNLESS)
1047 ret = !ret;
1048 }
1049
1050 if (ret) {
1051 struct stktable_key *key;
1052
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001053 key = stktable_fetch_key(px, s, &s->txn, PATTERN_FETCH_REQ, rule->expr, rule->table.t->type);
Emeric Brun1d33b292010-01-04 15:47:17 +01001054 if (!key)
1055 continue;
1056
1057 if (rule->flags & STK_IS_MATCH) {
1058 struct stksess *ts;
1059
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001060 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001061 if (!(s->flags & SN_ASSIGNED)) {
1062 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001063 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001064
1065 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001066 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1067 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001068 if (node) {
1069 struct server *srv;
1070
1071 srv = container_of(node, struct server, conf.id);
Willy Tarreau4de91492010-01-22 19:10:05 +01001072 if ((srv->state & SRV_RUNNING) ||
1073 (px->options & PR_O_PERSIST) ||
1074 (s->flags & SN_FORCE_PRST)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001075 s->flags |= SN_DIRECT | SN_ASSIGNED;
1076 s->srv = srv;
1077 }
1078 }
1079 }
Willy Tarreaue8f63382010-07-13 15:20:24 +02001080 stktable_touch(rule->table.t, ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001081 }
1082 }
1083 if (rule->flags & STK_IS_STORE) {
1084 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1085 struct stksess *ts;
1086
1087 ts = stksess_new(rule->table.t, key);
1088 if (ts) {
1089 s->store[s->store_count].table = rule->table.t;
1090 s->store[s->store_count++].ts = ts;
1091 }
1092 }
1093 }
1094 }
1095 }
1096
1097 req->analysers &= ~an_bit;
1098 req->analyse_exp = TICK_ETERNITY;
1099 return 1;
1100}
1101
1102/* This stream analyser works on a response. It applies all store rules on it
1103 * then returns 1. The data must already be present in the buffer otherwise
1104 * they won't match. It always returns 1.
1105 */
1106int process_store_rules(struct session *s, struct buffer *rep, int an_bit)
1107{
1108 struct proxy *px = s->be;
1109 struct sticking_rule *rule;
1110 int i;
1111
1112 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1113 now_ms, __FUNCTION__,
1114 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001115 rep,
1116 rep->rex, rep->wex,
1117 rep->flags,
1118 rep->l,
1119 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001120
1121 list_for_each_entry(rule, &px->storersp_rules, list) {
1122 int ret = 1 ;
1123 int storereqidx = -1;
1124
1125 for (i = 0; i < s->store_count; i++) {
1126 if (rule->table.t == s->store[i].table) {
1127 if (!(s->store[i].flags))
1128 storereqidx = i;
1129 break;
1130 }
1131 }
1132
1133 if ((i != s->store_count) && (storereqidx == -1))
1134 continue;
1135
1136 if (rule->cond) {
1137 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_RTR);
1138 ret = acl_pass(ret);
1139 if (rule->cond->pol == ACL_COND_UNLESS)
1140 ret = !ret;
1141 }
1142
1143 if (ret) {
1144 struct stktable_key *key;
1145
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001146 key = stktable_fetch_key(px, s, &s->txn, PATTERN_FETCH_RTR, rule->expr, rule->table.t->type);
Emeric Brun1d33b292010-01-04 15:47:17 +01001147 if (!key)
1148 continue;
1149
1150 if (storereqidx != -1) {
Willy Tarreau393379c2010-06-06 12:11:37 +02001151 stksess_setkey(s->store[storereqidx].table, s->store[storereqidx].ts, key);
Emeric Brun1d33b292010-01-04 15:47:17 +01001152 s->store[storereqidx].flags = 1;
1153 }
1154 else if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1155 struct stksess *ts;
1156
1157 ts = stksess_new(rule->table.t, key);
1158 if (ts) {
1159 s->store[s->store_count].table = rule->table.t;
1160 s->store[s->store_count].flags = 1;
1161 s->store[s->store_count++].ts = ts;
1162 }
1163 }
1164 }
1165 }
1166
1167 /* process store request and store response */
1168 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001169 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001170 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001171
1172 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1173 if (ts) {
1174 /* the entry already existed, we can free ours */
Willy Tarreaue8f63382010-07-13 15:20:24 +02001175 stktable_touch(s->store[i].table, ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001176 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001177 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001178 else
1179 ts = stktable_store(s->store[i].table, s->store[i].ts);
1180
1181 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001182 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
1183 stktable_data_cast(ptr, server_id) = s->srv->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001184 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001185 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001186
1187 rep->analysers &= ~an_bit;
1188 rep->analyse_exp = TICK_ETERNITY;
1189 return 1;
1190}
1191
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001192/* This macro is very specific to the function below. See the comments in
1193 * process_session() below to understand the logic and the tests.
1194 */
1195#define UPDATE_ANALYSERS(real, list, back, flag) { \
1196 list = (((list) & ~(flag)) | ~(back)) & (real); \
1197 back = real; \
1198 if (!(list)) \
1199 break; \
1200 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1201 continue; \
1202}
1203
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001204/* Processes the client, server, request and response jobs of a session task,
1205 * then puts it back to the wait queue in a clean state, or cleans up its
1206 * resources if it must be deleted. Returns in <next> the date the task wants
1207 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1208 * nothing too many times, the request and response buffers flags are monitored
1209 * and each function is called only if at least another function has changed at
1210 * least one flag it is interested in.
1211 */
Willy Tarreau26c25062009-03-08 09:38:41 +01001212struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001213{
1214 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001215 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001216 unsigned int rq_prod_last, rq_cons_last;
1217 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001218 unsigned int req_ana_back;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001219
1220 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
1221 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
1222
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001223 /* this data may be no longer valid, clear it */
1224 memset(&s->txn.auth, 0, sizeof(s->txn.auth));
1225
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001226 /* This flag must explicitly be set every time */
1227 s->req->flags &= ~BF_READ_NOEXP;
1228
1229 /* Keep a copy of req/rep flags so that we can detect shutdowns */
1230 rqf_last = s->req->flags;
1231 rpf_last = s->rep->flags;
1232
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001233 /* we don't want the stream interface functions to recursively wake us up */
1234 if (s->req->prod->owner == t)
1235 s->req->prod->flags |= SI_FL_DONT_WAKE;
1236 if (s->req->cons->owner == t)
1237 s->req->cons->flags |= SI_FL_DONT_WAKE;
1238
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001239 /* 1a: Check for low level timeouts if needed. We just set a flag on
1240 * stream interfaces when their timeouts have expired.
1241 */
1242 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
1243 stream_int_check_timeouts(&s->si[0]);
1244 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001245
1246 /* check buffer timeouts, and close the corresponding stream interfaces
1247 * for future reads or writes. Note: this will also concern upper layers
1248 * but we do not touch any other flag. We must be careful and correctly
1249 * detect state changes when calling them.
1250 */
1251
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001252 buffer_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001253
Willy Tarreau14641402009-12-29 14:49:56 +01001254 if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1255 s->req->cons->flags |= SI_FL_NOLINGER;
1256 s->req->cons->shutw(s->req->cons);
1257 }
1258
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001259 if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1260 s->req->prod->shutr(s->req->prod);
1261
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001262 buffer_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001263
Willy Tarreau14641402009-12-29 14:49:56 +01001264 if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1265 s->rep->cons->flags |= SI_FL_NOLINGER;
1266 s->rep->cons->shutw(s->rep->cons);
1267 }
1268
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001269 if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1270 s->rep->prod->shutr(s->rep->prod);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001271 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001272
1273 /* 1b: check for low-level errors reported at the stream interface.
1274 * First we check if it's a retryable error (in which case we don't
1275 * want to tell the buffer). Otherwise we report the error one level
1276 * upper by setting flags into the buffers. Note that the side towards
1277 * the client cannot have connect (hence retryable) errors. Also, the
1278 * connection setup code must be able to deal with any type of abort.
1279 */
1280 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
1281 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
1282 s->si[0].shutr(&s->si[0]);
1283 s->si[0].shutw(&s->si[0]);
1284 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001285 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreauae526782010-03-04 20:34:23 +01001286 s->be->counters.cli_aborts++;
1287 if (s->srv)
1288 s->srv->counters.cli_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001289 if (!(s->flags & SN_ERR_MASK))
1290 s->flags |= SN_ERR_CLICL;
1291 if (!(s->flags & SN_FINST_MASK))
1292 s->flags |= SN_FINST_D;
1293 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001294 }
1295 }
1296
1297 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
1298 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
1299 s->si[1].shutr(&s->si[1]);
1300 s->si[1].shutw(&s->si[1]);
1301 stream_int_report_error(&s->si[1]);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001302 s->be->counters.failed_resp++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001303 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001304 s->srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001305 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreauae526782010-03-04 20:34:23 +01001306 s->be->counters.srv_aborts++;
1307 if (s->srv)
1308 s->srv->counters.srv_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001309 if (!(s->flags & SN_ERR_MASK))
1310 s->flags |= SN_ERR_SRVCL;
1311 if (!(s->flags & SN_FINST_MASK))
1312 s->flags |= SN_FINST_D;
1313 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001314 }
1315 /* note: maybe we should process connection errors here ? */
1316 }
1317
1318 if (s->si[1].state == SI_ST_CON) {
1319 /* we were trying to establish a connection on the server side,
1320 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1321 */
1322 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
1323 sess_update_st_cer(s, &s->si[1]);
1324 else if (s->si[1].state == SI_ST_EST)
1325 sess_establish(s, &s->si[1]);
1326
1327 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1328 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1329 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1330 */
1331 }
1332
Willy Tarreau815a9b22010-07-27 17:15:12 +02001333 rq_prod_last = s->si[0].state;
1334 rq_cons_last = s->si[1].state;
1335 rp_cons_last = s->si[0].state;
1336 rp_prod_last = s->si[1].state;
1337
1338 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001339 /* Check for connection closure */
1340
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001341 DPRINTF(stderr,
1342 "[%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",
1343 now_ms, __FUNCTION__, __LINE__,
1344 t,
1345 s, s->flags,
1346 s->req, s->rep,
1347 s->req->rex, s->rep->wex,
1348 s->req->flags, s->rep->flags,
1349 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
1350 s->rep->cons->err_type, s->req->cons->err_type,
Willy Tarreauee28de02010-06-01 09:51:00 +02001351 s->req->cons->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001352
1353 /* nothing special to be done on client side */
1354 if (unlikely(s->req->prod->state == SI_ST_DIS))
1355 s->req->prod->state = SI_ST_CLO;
1356
1357 /* When a server-side connection is released, we have to count it and
1358 * check for pending connections on this server.
1359 */
1360 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
1361 s->req->cons->state = SI_ST_CLO;
1362 if (s->srv) {
1363 if (s->flags & SN_CURR_SESS) {
1364 s->flags &= ~SN_CURR_SESS;
1365 s->srv->cur_sess--;
1366 }
1367 sess_change_server(s, NULL);
1368 if (may_dequeue_tasks(s->srv, s->be))
1369 process_srv_queue(s->srv);
1370 }
1371 }
1372
1373 /*
1374 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1375 * at this point.
1376 */
1377
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001378 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001379 /* Analyse request */
1380 if ((s->req->flags & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001381 ((s->req->flags ^ rqf_last) & BF_MASK_STATIC) ||
1382 s->si[0].state != rq_prod_last ||
1383 s->si[1].state != rq_cons_last) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001384 unsigned int flags = s->req->flags;
1385
1386 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001387 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001388 unsigned int ana_list;
1389 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001390
Willy Tarreau90deb182010-01-07 00:20:41 +01001391 /* it's up to the analysers to stop new connections,
1392 * disable reading or closing. Note: if an analyser
1393 * disables any of these bits, it is responsible for
1394 * enabling them again when it disables itself, so
1395 * that other analysers are called in similar conditions.
1396 */
1397 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001398 buffer_auto_connect(s->req);
1399 buffer_auto_close(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001400
1401 /* We will call all analysers for which a bit is set in
1402 * s->req->analysers, following the bit order from LSB
1403 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001404 * the list when not needed. Any analyser may return 0
1405 * to break out of the loop, either because of missing
1406 * data to take a decision, or because it decides to
1407 * kill the session. We loop at least once through each
1408 * analyser, and we may loop again if other analysers
1409 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001410 *
1411 * We build a list of analysers to run. We evaluate all
1412 * of these analysers in the order of the lower bit to
1413 * the higher bit. This ordering is very important.
1414 * An analyser will often add/remove other analysers,
1415 * including itself. Any changes to itself have no effect
1416 * on the loop. If it removes any other analysers, we
1417 * want those analysers not to be called anymore during
1418 * this loop. If it adds an analyser that is located
1419 * after itself, we want it to be scheduled for being
1420 * processed during the loop. If it adds an analyser
1421 * which is located before it, we want it to switch to
1422 * it immediately, even if it has already been called
1423 * once but removed since.
1424 *
1425 * In order to achieve this, we compare the analyser
1426 * list after the call with a copy of it before the
1427 * call. The work list is fed with analyser bits that
1428 * appeared during the call. Then we compare previous
1429 * work list with the new one, and check the bits that
1430 * appeared. If the lowest of these bits is lower than
1431 * the current bit, it means we have enabled a previous
1432 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001433 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001434
1435 ana_list = ana_back = s->req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001436 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001437 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001438
Willy Tarreau3041b9f2010-10-15 23:25:20 +02001439 if (ana_list & AN_REQ_DECODE_PROXY) {
1440 if (!frontend_decode_proxy_request(s, s->req, AN_REQ_DECODE_PROXY))
1441 break;
1442 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_DECODE_PROXY);
1443 }
1444
Willy Tarreaufb356202010-08-03 14:02:05 +02001445 if (ana_list & AN_REQ_INSPECT_FE) {
1446 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001447 break;
Willy Tarreaufb356202010-08-03 14:02:05 +02001448 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001449 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001450
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001451 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001452 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001453 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001454 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001455 }
1456
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001457 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001458 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
1459 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001460 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001461 }
1462
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001463 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001464 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
1465 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001466 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001467 }
1468
Willy Tarreaufb356202010-08-03 14:02:05 +02001469 if (ana_list & AN_REQ_INSPECT_BE) {
1470 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_BE))
1471 break;
1472 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
1473 }
1474
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001475 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001476 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
1477 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001478 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001479 }
1480
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001481 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001482 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001483 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001484 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001485 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001486
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001487 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreauc465fd72009-08-31 00:17:18 +02001488 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
1489 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001490 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001491 }
1492
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001493 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001494 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001495 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001496 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001497 }
Emeric Brun647caf12009-06-30 17:57:00 +02001498
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001499 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Emeric Brun647caf12009-06-30 17:57:00 +02001500 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
1501 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001502 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001503 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001504
Emeric Brun1d33b292010-01-04 15:47:17 +01001505 if (ana_list & AN_REQ_STICKING_RULES) {
1506 if (!process_sticking_rules(s, s->req, AN_REQ_STICKING_RULES))
1507 break;
1508 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
1509 }
1510
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001511 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001512 if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY))
1513 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001514 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001515 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001516 break;
1517 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001518 }
Willy Tarreau84455332009-03-15 22:34:05 +01001519
Willy Tarreau815a9b22010-07-27 17:15:12 +02001520 rq_prod_last = s->si[0].state;
1521 rq_cons_last = s->si[1].state;
1522 rqf_last = s->req->flags;
1523
1524 if ((s->req->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001525 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001526 }
1527
Willy Tarreau576507f2010-01-07 00:09:04 +01001528 /* we'll monitor the request analysers while parsing the response,
1529 * because some response analysers may indirectly enable new request
1530 * analysers (eg: HTTP keep-alive).
1531 */
1532 req_ana_back = s->req->analysers;
1533
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001534 resync_response:
1535 /* Analyse response */
1536
1537 if (unlikely(s->rep->flags & BF_HIJACK)) {
1538 /* In inject mode, we wake up everytime something has
1539 * happened on the write side of the buffer.
1540 */
1541 unsigned int flags = s->rep->flags;
1542
1543 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
1544 !(s->rep->flags & BF_FULL)) {
1545 s->rep->hijacker(s, s->rep);
1546 }
1547
1548 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
1549 rpf_last = s->rep->flags;
1550 goto resync_response;
1551 }
1552 }
1553 else if ((s->rep->flags & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001554 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC ||
1555 s->si[0].state != rp_cons_last ||
1556 s->si[1].state != rp_prod_last) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001557 unsigned int flags = s->rep->flags;
1558
1559 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001560 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001561 unsigned int ana_list;
1562 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001563
Willy Tarreau90deb182010-01-07 00:20:41 +01001564 /* it's up to the analysers to stop disable reading or
1565 * closing. Note: if an analyser disables any of these
1566 * bits, it is responsible for enabling them again when
1567 * it disables itself, so that other analysers are called
1568 * in similar conditions.
1569 */
1570 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001571 buffer_auto_close(s->rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001572
1573 /* We will call all analysers for which a bit is set in
1574 * s->rep->analysers, following the bit order from LSB
1575 * to MSB. The analysers must remove themselves from
1576 * the list when not needed. Any analyser may return 0
1577 * to break out of the loop, either because of missing
1578 * data to take a decision, or because it decides to
1579 * kill the session. We loop at least once through each
1580 * analyser, and we may loop again if other analysers
1581 * are added in the middle.
1582 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001583
1584 ana_list = ana_back = s->rep->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001585 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001586 /* Warning! ensure that analysers are always placed in ascending order! */
1587
Emeric Brun97679e72010-09-23 17:56:44 +02001588 if (ana_list & AN_RES_INSPECT) {
1589 if (!tcp_inspect_response(s, s->rep, AN_RES_INSPECT))
1590 break;
1591 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_INSPECT);
1592 }
1593
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001594 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001595 if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP))
1596 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001597 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001598 }
1599
Emeric Brun1d33b292010-01-04 15:47:17 +01001600 if (ana_list & AN_RES_STORE_RULES) {
1601 if (!process_store_rules(s, s->rep, AN_RES_STORE_RULES))
1602 break;
1603 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
1604 }
1605
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001606 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001607 if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be))
1608 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001609 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001610 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001611
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001612 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001613 if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY))
1614 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001615 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001616 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001617 break;
1618 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001619 }
1620
Willy Tarreau815a9b22010-07-27 17:15:12 +02001621 rp_cons_last = s->si[0].state;
1622 rp_prod_last = s->si[1].state;
1623 rpf_last = s->rep->flags;
1624
1625 if ((s->rep->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001626 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001627 }
1628
Willy Tarreau576507f2010-01-07 00:09:04 +01001629 /* maybe someone has added some request analysers, so we must check and loop */
1630 if (s->req->analysers & ~req_ana_back)
1631 goto resync_request;
1632
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001633 /* FIXME: here we should call protocol handlers which rely on
1634 * both buffers.
1635 */
1636
1637
1638 /*
Willy Tarreauae526782010-03-04 20:34:23 +01001639 * Now we propagate unhandled errors to the session. Normally
1640 * we're just in a data phase here since it means we have not
1641 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001642 */
1643 if (!(s->flags & SN_ERR_MASK)) {
1644 if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1645 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +01001646 s->req->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001647 if (s->req->flags & BF_READ_ERROR) {
1648 s->be->counters.cli_aborts++;
1649 if (s->srv)
1650 s->srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001651 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001652 }
1653 else if (s->req->flags & BF_READ_TIMEOUT) {
1654 s->be->counters.cli_aborts++;
1655 if (s->srv)
1656 s->srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001657 s->flags |= SN_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001658 }
1659 else if (s->req->flags & BF_WRITE_ERROR) {
1660 s->be->counters.srv_aborts++;
1661 if (s->srv)
1662 s->srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001663 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001664 }
1665 else {
1666 s->be->counters.srv_aborts++;
1667 if (s->srv)
1668 s->srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001669 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001670 }
Willy Tarreau84455332009-03-15 22:34:05 +01001671 sess_set_term_flags(s);
1672 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001673 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1674 /* Report it if the server got an error or a read timeout expired */
1675 s->rep->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001676 if (s->rep->flags & BF_READ_ERROR) {
1677 s->be->counters.srv_aborts++;
1678 if (s->srv)
1679 s->srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001680 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001681 }
1682 else if (s->rep->flags & BF_READ_TIMEOUT) {
1683 s->be->counters.srv_aborts++;
1684 if (s->srv)
1685 s->srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001686 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001687 }
1688 else if (s->rep->flags & BF_WRITE_ERROR) {
1689 s->be->counters.cli_aborts++;
1690 if (s->srv)
1691 s->srv->counters.cli_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001692 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001693 }
1694 else {
1695 s->be->counters.cli_aborts++;
1696 if (s->srv)
1697 s->srv->counters.cli_aborts++;
1698 s->flags |= SN_ERR_CLITO;
1699 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001700 sess_set_term_flags(s);
1701 }
Willy Tarreau84455332009-03-15 22:34:05 +01001702 }
1703
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001704 /*
1705 * Here we take care of forwarding unhandled data. This also includes
1706 * connection establishments and shutdown requests.
1707 */
1708
1709
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001710 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001711 * everything. We configure the buffer to forward indefinitely.
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001712 * Note that we're checking BF_SHUTR_NOW as an indication of a possible
1713 * recent call to buffer_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001714 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001715 if (!s->req->analysers &&
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001716 !(s->req->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001717 (s->req->prod->state >= SI_ST_EST) &&
1718 (s->req->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001719 /* This buffer is freewheeling, there's no analyser nor hijacker
1720 * attached to it. If any data are left in, we'll permit them to
1721 * move.
1722 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001723 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001724 buffer_auto_connect(s->req);
1725 buffer_auto_close(s->req);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001726 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001727
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001728 /* We'll let data flow between the producer (if still connected)
1729 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001730 */
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001731 if (!(s->req->flags & (BF_SHUTR|BF_SHUTW_NOW)))
Willy Tarreau31971e52009-09-20 12:07:52 +02001732 buffer_forward(s->req, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001733 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001734
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001735 /* check if it is wise to enable kernel splicing to forward request data */
1736 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1737 s->req->to_forward &&
1738 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001739 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001740 (pipes_used < global.maxpipes) &&
1741 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
1742 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1743 (s->req->flags & BF_STREAMER_FAST)))) {
1744 s->req->flags |= BF_KERN_SPLICING;
1745 }
1746
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001747 /* reflect what the L7 analysers have seen last */
1748 rqf_last = s->req->flags;
1749
1750 /*
1751 * Now forward all shutdown requests between both sides of the buffer
1752 */
1753
Willy Tarreau520d95e2009-09-19 21:04:57 +02001754 /* first, let's check if the request buffer needs to shutdown(write), which may
1755 * happen either because the input is closed or because we want to force a close
Willy Tarreaue4599762010-03-21 23:25:09 +01001756 * once the server has begun to respond.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001757 */
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001758 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
Willy Tarreaue4599762010-03-21 23:25:09 +01001759 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001760 buffer_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001761
1762 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001763 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_OUT_EMPTY)) == (BF_SHUTW_NOW|BF_OUT_EMPTY)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001764 s->req->cons->shutw(s->req->cons);
1765
1766 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001767 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
1768 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001769 buffer_shutr_now(s->req);
1770
1771 /* shutdown(read) pending */
1772 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1773 s->req->prod->shutr(s->req->prod);
1774
Willy Tarreau520d95e2009-09-19 21:04:57 +02001775 /* it's possible that an upper layer has requested a connection setup or abort.
1776 * There are 2 situations where we decide to establish a new connection :
1777 * - there are data scheduled for emission in the buffer
1778 * - the BF_AUTO_CONNECT flag is set (active connection)
1779 */
1780 if (s->req->cons->state == SI_ST_INI) {
Willy Tarreaue4599762010-03-21 23:25:09 +01001781 if (!(s->req->flags & BF_SHUTW)) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001782 if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) {
Willy Tarreau85e7d002010-05-31 11:57:51 +02001783 /* If we have an iohandler without a connect method, we immediately
1784 * switch to the connected state, otherwise we perform a connection
1785 * request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001786 */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001787 s->req->cons->state = SI_ST_REQ; /* new connection requested */
Willy Tarreau070ceb62010-06-01 10:36:43 +02001788 s->req->cons->conn_retries = s->be->conn_retries;
Willy Tarreau85e7d002010-05-31 11:57:51 +02001789 if (unlikely(s->req->cons->iohandler && !s->req->cons->connect)) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02001790 s->req->cons->state = SI_ST_EST; /* connection established */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001791 s->rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
1792 s->req->wex = TICK_ETERNITY;
1793 }
Willy Tarreau520d95e2009-09-19 21:04:57 +02001794 }
Willy Tarreau73201222009-08-16 18:27:24 +02001795 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001796 else {
Willy Tarreau92795622009-03-06 12:51:23 +01001797 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001798 buffer_shutw_now(s->req); /* fix buffer flags upon abort */
1799 buffer_shutr_now(s->rep);
1800 }
Willy Tarreau92795622009-03-06 12:51:23 +01001801 }
1802
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001803
1804 /* we may have a pending connection request, or a connection waiting
1805 * for completion.
1806 */
1807 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
1808 do {
1809 /* nb: step 1 might switch from QUE to ASS, but we first want
1810 * to give a chance to step 2 to perform a redirect if needed.
1811 */
1812 if (s->si[1].state != SI_ST_REQ)
1813 sess_update_stream_int(s, &s->si[1]);
1814 if (s->si[1].state == SI_ST_REQ)
1815 sess_prepare_conn_req(s, &s->si[1]);
1816
1817 if (s->si[1].state == SI_ST_ASS && s->srv &&
1818 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
1819 perform_http_redirect(s, &s->si[1]);
1820 } while (s->si[1].state == SI_ST_ASS);
1821 }
1822
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001823 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001824 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001825 goto resync_stream_interface;
1826
Willy Tarreau815a9b22010-07-27 17:15:12 +02001827 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001828 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001829 goto resync_request;
1830
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001831 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01001832
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001833 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001834 * everything. We configure the buffer to forward indefinitely.
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001835 * Note that we're checking BF_SHUTR_NOW as an indication of a possible
1836 * recent call to buffer_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001837 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001838 if (!s->rep->analysers &&
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001839 !(s->rep->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001840 (s->rep->prod->state >= SI_ST_EST) &&
1841 (s->rep->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001842 /* This buffer is freewheeling, there's no analyser nor hijacker
1843 * attached to it. If any data are left in, we'll permit them to
1844 * move.
1845 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001846 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001847 buffer_auto_close(s->rep);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001848 buffer_flush(s->rep);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001849
1850 /* We'll let data flow between the producer (if still connected)
1851 * to the consumer.
1852 */
1853 if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW_NOW)))
Willy Tarreau31971e52009-09-20 12:07:52 +02001854 buffer_forward(s->rep, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001855 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001856
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001857 /* check if it is wise to enable kernel splicing to forward response data */
1858 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1859 s->rep->to_forward &&
1860 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001861 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001862 (pipes_used < global.maxpipes) &&
1863 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
1864 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1865 (s->rep->flags & BF_STREAMER_FAST)))) {
1866 s->rep->flags |= BF_KERN_SPLICING;
1867 }
1868
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001869 /* reflect what the L7 analysers have seen last */
1870 rpf_last = s->rep->flags;
1871
1872 /*
1873 * Now forward all shutdown requests between both sides of the buffer
1874 */
1875
1876 /*
1877 * FIXME: this is probably where we should produce error responses.
1878 */
1879
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001880 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001881 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
1882 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001883 buffer_shutw_now(s->rep);
1884
1885 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001886 if (unlikely((s->rep->flags & (BF_SHUTW|BF_OUT_EMPTY|BF_SHUTW_NOW)) == (BF_OUT_EMPTY|BF_SHUTW_NOW)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001887 s->rep->cons->shutw(s->rep->cons);
1888
1889 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001890 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
1891 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001892 buffer_shutr_now(s->rep);
1893
1894 /* shutdown(read) pending */
1895 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1896 s->rep->prod->shutr(s->rep->prod);
1897
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001898 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001899 goto resync_stream_interface;
1900
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001901 if (s->req->flags != rqf_last)
1902 goto resync_request;
1903
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001904 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001905 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001906
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001907 /* we're interested in getting wakeups again */
1908 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
1909 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
1910
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001911 /* This is needed only when debugging is enabled, to indicate
1912 * client-side or server-side close. Please note that in the unlikely
1913 * event where both sides would close at once, the sequence is reported
1914 * on the server side first.
1915 */
1916 if (unlikely((global.mode & MODE_DEBUG) &&
1917 (!(global.mode & MODE_QUIET) ||
1918 (global.mode & MODE_VERBOSE)))) {
1919 int len;
1920
1921 if (s->si[1].state == SI_ST_CLO &&
1922 s->si[1].prev_state == SI_ST_EST) {
1923 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1924 s->uniq_id, s->be->id,
1925 (unsigned short)s->si[0].fd,
1926 (unsigned short)s->si[1].fd);
1927 write(1, trash, len);
1928 }
1929
1930 if (s->si[0].state == SI_ST_CLO &&
1931 s->si[0].prev_state == SI_ST_EST) {
1932 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1933 s->uniq_id, s->be->id,
1934 (unsigned short)s->si[0].fd,
1935 (unsigned short)s->si[1].fd);
1936 write(1, trash, len);
1937 }
1938 }
1939
1940 if (likely((s->rep->cons->state != SI_ST_CLO) ||
1941 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
1942
1943 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1944 session_process_counters(s);
1945
Willy Tarreau1accfc02009-09-05 20:57:35 +02001946 if (s->rep->cons->state == SI_ST_EST && !s->rep->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001947 s->rep->cons->update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001948
Willy Tarreau1accfc02009-09-05 20:57:35 +02001949 if (s->req->cons->state == SI_ST_EST && !s->req->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001950 s->req->cons->update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001951
Willy Tarreaua6eebb32010-06-04 11:40:20 +02001952 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL|BF_READ_ATTACHED);
1953 s->rep->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL|BF_READ_ATTACHED);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001954 s->si[0].prev_state = s->si[0].state;
1955 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01001956 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
1957 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001958
1959 /* Trick: if a request is being waiting for the server to respond,
1960 * and if we know the server can timeout, we don't want the timeout
1961 * to expire on the client side first, but we're still interested
1962 * in passing data from the client to the server (eg: POST). Thus,
1963 * we can cancel the client's request timeout if the server's
1964 * request timeout is set and the server has not yet sent a response.
1965 */
1966
Willy Tarreau520d95e2009-09-19 21:04:57 +02001967 if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01001968 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
1969 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001970 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01001971 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001972
Willy Tarreau7a20aa62010-07-13 16:30:45 +02001973 /* Call the stream interfaces' I/O handlers when embedded.
Willy Tarreau1accfc02009-09-05 20:57:35 +02001974 * Note that this one may wake the task up again.
1975 */
Willy Tarreau7a20aa62010-07-13 16:30:45 +02001976 if (s->req->cons->iohandler || s->rep->cons->iohandler) {
1977 if (s->req->cons->iohandler)
1978 s->req->cons->iohandler(s->req->cons);
1979 if (s->rep->cons->iohandler)
1980 s->rep->cons->iohandler(s->rep->cons);
Willy Tarreau1accfc02009-09-05 20:57:35 +02001981 if (task_in_rq(t)) {
1982 /* If we woke up, we don't want to requeue the
1983 * task to the wait queue, but rather requeue
1984 * it into the runqueue ASAP.
1985 */
1986 t->expire = TICK_ETERNITY;
1987 return t;
1988 }
1989 }
1990
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001991 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
1992 tick_first(s->rep->rex, s->rep->wex));
1993 if (s->req->analysers)
1994 t->expire = tick_first(t->expire, s->req->analyse_exp);
1995
1996 if (s->si[0].exp)
1997 t->expire = tick_first(t->expire, s->si[0].exp);
1998
1999 if (s->si[1].exp)
2000 t->expire = tick_first(t->expire, s->si[1].exp);
2001
2002#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002003 fprintf(stderr,
2004 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2005 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
2006 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
2007 s->rep->rex, s->rep->wex, s->si[0].exp, s->si[1].exp, s->si[0].state, s->si[1].state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002008#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002009
2010#ifdef DEBUG_DEV
2011 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002012 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002013 ABORT_NOW();
2014#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01002015 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002016 }
2017
2018 s->fe->feconn--;
2019 if (s->flags & SN_BE_ASSIGNED)
2020 s->be->beconn--;
2021 actconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002022 jobs--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02002023 s->listener->nbconn--;
2024 if (s->listener->state == LI_FULL &&
2025 s->listener->nbconn < s->listener->maxconn) {
2026 /* we should reactivate the listener */
2027 EV_FD_SET(s->listener->fd, DIR_RD);
2028 s->listener->state = LI_READY;
2029 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002030
2031 if (unlikely((global.mode & MODE_DEBUG) &&
2032 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
2033 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002034 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002035 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002036 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002037 write(1, trash, len);
2038 }
2039
2040 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
2041 session_process_counters(s);
2042
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002043 if (s->txn.status) {
2044 int n;
2045
2046 n = s->txn.status / 100;
2047 if (n < 1 || n > 5)
2048 n = 0;
2049
2050 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau24657792010-02-26 10:30:28 +01002051 s->fe->counters.fe.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002052
Willy Tarreau24657792010-02-26 10:30:28 +01002053 if ((s->flags & SN_BE_ASSIGNED) &&
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002054 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau24657792010-02-26 10:30:28 +01002055 s->be->counters.be.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002056 }
2057
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002058 /* let's do a final log if we need it */
2059 if (s->logs.logwait &&
2060 !(s->flags & SN_MONITOR) &&
2061 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002062 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002063 }
2064
2065 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002066 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002067 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002068 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002069 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002070}
2071
Willy Tarreau7c669d72008-06-20 15:04:11 +02002072/*
2073 * This function adjusts sess->srv_conn and maintains the previous and new
2074 * server's served session counts. Setting newsrv to NULL is enough to release
2075 * current connection slot. This function also notifies any LB algo which might
2076 * expect to be informed about any change in the number of active sessions on a
2077 * server.
2078 */
2079void sess_change_server(struct session *sess, struct server *newsrv)
2080{
2081 if (sess->srv_conn == newsrv)
2082 return;
2083
2084 if (sess->srv_conn) {
2085 sess->srv_conn->served--;
2086 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2087 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
2088 sess->srv_conn = NULL;
2089 }
2090
2091 if (newsrv) {
2092 newsrv->served++;
2093 if (newsrv->proxy->lbprm.server_take_conn)
2094 newsrv->proxy->lbprm.server_take_conn(newsrv);
2095 sess->srv_conn = newsrv;
2096 }
2097}
2098
Willy Tarreau84455332009-03-15 22:34:05 +01002099/* Set correct session termination flags in case no analyser has done it. It
2100 * also counts a failed request if the server state has not reached the request
2101 * stage.
2102 */
2103void sess_set_term_flags(struct session *s)
2104{
2105 if (!(s->flags & SN_FINST_MASK)) {
2106 if (s->si[1].state < SI_ST_REQ) {
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002107
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002108 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002109 if (s->listener->counters)
2110 s->listener->counters->failed_req++;
2111
Willy Tarreau84455332009-03-15 22:34:05 +01002112 s->flags |= SN_FINST_R;
2113 }
2114 else if (s->si[1].state == SI_ST_QUE)
2115 s->flags |= SN_FINST_Q;
2116 else if (s->si[1].state < SI_ST_EST)
2117 s->flags |= SN_FINST_C;
Willy Tarreau033b2db2010-03-04 17:54:21 +01002118 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreau84455332009-03-15 22:34:05 +01002119 s->flags |= SN_FINST_D;
2120 else
2121 s->flags |= SN_FINST_L;
2122 }
2123}
2124
2125/* Handle server-side errors for default protocols. It is called whenever a a
2126 * connection setup is aborted or a request is aborted in queue. It sets the
2127 * session termination flags so that the caller does not have to worry about
2128 * them. It's installed as ->srv_error for the server-side stream_interface.
2129 */
2130void default_srv_error(struct session *s, struct stream_interface *si)
2131{
2132 int err_type = si->err_type;
2133 int err = 0, fin = 0;
2134
2135 if (err_type & SI_ET_QUEUE_ABRT) {
2136 err = SN_ERR_CLICL;
2137 fin = SN_FINST_Q;
2138 }
2139 else if (err_type & SI_ET_CONN_ABRT) {
2140 err = SN_ERR_CLICL;
2141 fin = SN_FINST_C;
2142 }
2143 else if (err_type & SI_ET_QUEUE_TO) {
2144 err = SN_ERR_SRVTO;
2145 fin = SN_FINST_Q;
2146 }
2147 else if (err_type & SI_ET_QUEUE_ERR) {
2148 err = SN_ERR_SRVCL;
2149 fin = SN_FINST_Q;
2150 }
2151 else if (err_type & SI_ET_CONN_TO) {
2152 err = SN_ERR_SRVTO;
2153 fin = SN_FINST_C;
2154 }
2155 else if (err_type & SI_ET_CONN_ERR) {
2156 err = SN_ERR_SRVCL;
2157 fin = SN_FINST_C;
2158 }
2159 else /* SI_ET_CONN_OTHER and others */ {
2160 err = SN_ERR_INTERNAL;
2161 fin = SN_FINST_C;
2162 }
2163
2164 if (!(s->flags & SN_ERR_MASK))
2165 s->flags |= err;
2166 if (!(s->flags & SN_FINST_MASK))
2167 s->flags |= fin;
2168}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002169
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002170
Willy Tarreau8b22a712010-06-18 17:46:06 +02002171/************************************************************************/
2172/* All supported ACL keywords must be declared here. */
2173/************************************************************************/
2174
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002175/* set test->i to the General Purpose Counter 0 value in the stksess entry <ts> */
2176static int
2177acl_fetch_get_gpc0(struct stktable *table, struct acl_test *test, struct stksess *ts)
2178{
2179 test->flags = ACL_TEST_F_VOL_TEST;
2180 test->i = 0;
2181 if (ts != NULL) {
2182 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2183 if (!ptr)
2184 return 0; /* parameter not stored */
2185 test->i = stktable_data_cast(ptr, gpc0);
2186 }
2187 return 1;
2188}
2189
2190/* set test->i to the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002191 * frontend counters.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002192 */
2193static int
Willy Tarreau56123282010-08-06 19:06:56 +02002194acl_fetch_sc1_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2195 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002196{
Willy Tarreau56123282010-08-06 19:06:56 +02002197 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002198 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002199 return acl_fetch_get_gpc0(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002200}
2201
2202/* set test->i to the General Purpose Counter 0 value from the session's tracked
2203 * backend counters.
2204 */
2205static int
Willy Tarreau56123282010-08-06 19:06:56 +02002206acl_fetch_sc2_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2207 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002208{
Willy Tarreau56123282010-08-06 19:06:56 +02002209 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002210 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002211 return acl_fetch_get_gpc0(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002212}
2213
2214/* set test->i to the General Purpose Counter 0 value from the session's source
2215 * address in the table pointed to by expr.
2216 */
2217static int
2218acl_fetch_src_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2219 struct acl_expr *expr, struct acl_test *test)
2220{
2221 struct stktable_key *key;
2222
2223 key = tcpv4_src_to_stktable_key(l4);
2224 if (!key)
2225 return 0; /* only TCPv4 is supported right now */
2226
2227 if (expr->arg_len)
2228 px = find_stktable(expr->arg.str);
2229
2230 if (!px)
2231 return 0; /* table not found */
2232
2233 return acl_fetch_get_gpc0(&px->table, test, stktable_lookup_key(&px->table, key));
2234}
2235
2236/* Increment the General Purpose Counter 0 value in the stksess entry <ts> and
2237 * return it into test->i.
2238 */
2239static int
2240acl_fetch_inc_gpc0(struct stktable *table, struct acl_test *test, struct stksess *ts)
2241{
2242 test->flags = ACL_TEST_F_VOL_TEST;
2243 test->i = 0;
2244 if (ts != NULL) {
2245 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2246 if (!ptr)
2247 return 0; /* parameter not stored */
2248 test->i = ++stktable_data_cast(ptr, gpc0);
2249 }
2250 return 1;
2251}
2252
2253/* Increment the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002254 * frontend counters and return it into test->i.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002255 */
2256static int
Willy Tarreau56123282010-08-06 19:06:56 +02002257acl_fetch_sc1_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2258 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002259{
Willy Tarreau56123282010-08-06 19:06:56 +02002260 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002261 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002262 return acl_fetch_inc_gpc0(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002263}
2264
2265/* Increment the General Purpose Counter 0 value from the session's tracked
2266 * backend counters and return it into test->i.
2267 */
2268static int
Willy Tarreau56123282010-08-06 19:06:56 +02002269acl_fetch_sc2_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2270 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002271{
Willy Tarreau56123282010-08-06 19:06:56 +02002272 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002273 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002274 return acl_fetch_inc_gpc0(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002275}
2276
2277/* Increment the General Purpose Counter 0 value from the session's source
2278 * address in the table pointed to by expr, and return it into test->i.
2279 */
2280static int
2281acl_fetch_src_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2282 struct acl_expr *expr, struct acl_test *test)
2283{
2284 struct stktable_key *key;
2285
2286 key = tcpv4_src_to_stktable_key(l4);
2287 if (!key)
2288 return 0; /* only TCPv4 is supported right now */
2289
2290 if (expr->arg_len)
2291 px = find_stktable(expr->arg.str);
2292
2293 if (!px)
2294 return 0; /* table not found */
2295
2296 return acl_fetch_inc_gpc0(&px->table, test, stktable_update_key(&px->table, key));
2297}
2298
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002299/* set test->i to the cumulated number of connections in the stksess entry <ts> */
2300static int
2301acl_fetch_conn_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2302{
2303 test->flags = ACL_TEST_F_VOL_TEST;
2304 test->i = 0;
2305 if (ts != NULL) {
2306 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CNT);
2307 if (!ptr)
2308 return 0; /* parameter not stored */
2309 test->i = stktable_data_cast(ptr, conn_cnt);
2310 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002311 return 1;
2312}
2313
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002314/* set test->i to the cumulated number of connections from the session's tracked FE counters */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002315static int
Willy Tarreau56123282010-08-06 19:06:56 +02002316acl_fetch_sc1_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2317 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002318{
Willy Tarreau56123282010-08-06 19:06:56 +02002319 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002320 return 0;
2321
Willy Tarreau56123282010-08-06 19:06:56 +02002322 return acl_fetch_conn_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002323}
2324
2325/* set test->i to the cumulated number of connections from the session's tracked BE counters */
2326static int
Willy Tarreau56123282010-08-06 19:06:56 +02002327acl_fetch_sc2_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2328 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002329{
Willy Tarreau56123282010-08-06 19:06:56 +02002330 if (!l4->stkctr2_entry)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002331 return 0;
2332
Willy Tarreau56123282010-08-06 19:06:56 +02002333 return acl_fetch_conn_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002334}
2335
2336/* set test->i to the cumulated number of connections from the session's source
2337 * address in the table pointed to by expr.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002338 */
2339static int
2340acl_fetch_src_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2341 struct acl_expr *expr, struct acl_test *test)
2342{
Willy Tarreau8b22a712010-06-18 17:46:06 +02002343 struct stktable_key *key;
2344
2345 key = tcpv4_src_to_stktable_key(l4);
2346 if (!key)
2347 return 0; /* only TCPv4 is supported right now */
2348
2349 if (expr->arg_len)
2350 px = find_stktable(expr->arg.str);
2351
2352 if (!px)
2353 return 0; /* table not found */
2354
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002355 return acl_fetch_conn_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau8b22a712010-06-18 17:46:06 +02002356}
2357
Willy Tarreau91c43d72010-06-20 11:19:22 +02002358/* set test->i to the connection rate in the stksess entry <ts> over the configured period */
2359static int
2360acl_fetch_conn_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2361{
2362 test->flags = ACL_TEST_F_VOL_TEST;
2363 test->i = 0;
2364 if (ts != NULL) {
2365 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_RATE);
2366 if (!ptr)
2367 return 0; /* parameter not stored */
2368 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
2369 table->data_arg[STKTABLE_DT_CONN_RATE].u);
2370 }
2371 return 1;
2372}
2373
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002374/* set test->i to the connection rate from the session's tracked FE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002375 * the configured period.
2376 */
2377static int
Willy Tarreau56123282010-08-06 19:06:56 +02002378acl_fetch_sc1_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2379 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002380{
Willy Tarreau56123282010-08-06 19:06:56 +02002381 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002382 return 0;
2383
Willy Tarreau56123282010-08-06 19:06:56 +02002384 return acl_fetch_conn_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002385}
2386
2387/* set test->i to the connection rate from the session's tracked BE counters over
2388 * the configured period.
2389 */
2390static int
Willy Tarreau56123282010-08-06 19:06:56 +02002391acl_fetch_sc2_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2392 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002393{
Willy Tarreau56123282010-08-06 19:06:56 +02002394 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002395 return 0;
2396
Willy Tarreau56123282010-08-06 19:06:56 +02002397 return acl_fetch_conn_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002398}
2399
2400/* set test->i to the connection rate from the session's source address in the
2401 * table pointed to by expr, over the configured period.
2402 */
2403static int
2404acl_fetch_src_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2405 struct acl_expr *expr, struct acl_test *test)
2406{
2407 struct stktable_key *key;
2408
2409 key = tcpv4_src_to_stktable_key(l4);
2410 if (!key)
2411 return 0; /* only TCPv4 is supported right now */
2412
2413 if (expr->arg_len)
2414 px = find_stktable(expr->arg.str);
2415
2416 if (!px)
2417 return 0; /* table not found */
2418
2419 return acl_fetch_conn_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2420}
2421
Willy Tarreau8b22a712010-06-18 17:46:06 +02002422/* set test->i to the number of connections from the session's source address
2423 * in the table pointed to by expr, after updating it.
2424 */
2425static int
2426acl_fetch_src_updt_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2427 struct acl_expr *expr, struct acl_test *test)
2428{
2429 struct stksess *ts;
2430 struct stktable_key *key;
2431 void *ptr;
2432
2433 key = tcpv4_src_to_stktable_key(l4);
2434 if (!key)
2435 return 0; /* only TCPv4 is supported right now */
2436
2437 if (expr->arg_len)
2438 px = find_stktable(expr->arg.str);
2439
2440 if (!px)
2441 return 0; /* table not found */
2442
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002443 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2444 /* entry does not exist and could not be created */
2445 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002446
2447 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2448 if (!ptr)
2449 return 0; /* parameter not stored in this table */
2450
2451 test->i = ++stktable_data_cast(ptr, conn_cnt);
2452 test->flags = ACL_TEST_F_VOL_TEST;
2453 return 1;
2454}
2455
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002456/* set test->i to the number of concurrent connections in the stksess entry <ts> */
2457static int
2458acl_fetch_conn_cur(struct stktable *table, struct acl_test *test, struct stksess *ts)
2459{
2460 test->flags = ACL_TEST_F_VOL_TEST;
2461 test->i = 0;
2462
2463 if (ts != NULL) {
2464 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CUR);
2465 if (!ptr)
2466 return 0; /* parameter not stored */
2467 test->i = stktable_data_cast(ptr, conn_cur);
2468 }
2469 return 1;
2470}
2471
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002472/* set test->i to the number of concurrent connections from the session's tracked FE counters */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002473static int
Willy Tarreau56123282010-08-06 19:06:56 +02002474acl_fetch_sc1_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2475 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002476{
Willy Tarreau56123282010-08-06 19:06:56 +02002477 if (!l4->stkctr1_entry)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002478 return 0;
2479
Willy Tarreau56123282010-08-06 19:06:56 +02002480 return acl_fetch_conn_cur(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002481}
2482
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002483/* set test->i to the number of concurrent connections from the session's tracked BE counters */
2484static int
Willy Tarreau56123282010-08-06 19:06:56 +02002485acl_fetch_sc2_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2486 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002487{
Willy Tarreau56123282010-08-06 19:06:56 +02002488 if (!l4->stkctr2_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002489 return 0;
2490
Willy Tarreau56123282010-08-06 19:06:56 +02002491 return acl_fetch_conn_cur(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002492}
2493
Willy Tarreau38285c12010-06-18 16:35:43 +02002494/* set test->i to the number of concurrent connections from the session's source
2495 * address in the table pointed to by expr.
2496 */
2497static int
2498acl_fetch_src_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2499 struct acl_expr *expr, struct acl_test *test)
2500{
Willy Tarreau38285c12010-06-18 16:35:43 +02002501 struct stktable_key *key;
2502
2503 key = tcpv4_src_to_stktable_key(l4);
2504 if (!key)
2505 return 0; /* only TCPv4 is supported right now */
2506
2507 if (expr->arg_len)
2508 px = find_stktable(expr->arg.str);
2509
2510 if (!px)
2511 return 0; /* table not found */
2512
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002513 return acl_fetch_conn_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau38285c12010-06-18 16:35:43 +02002514}
2515
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002516/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2517static int
2518acl_fetch_sess_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2519{
2520 test->flags = ACL_TEST_F_VOL_TEST;
2521 test->i = 0;
2522 if (ts != NULL) {
2523 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_CNT);
2524 if (!ptr)
2525 return 0; /* parameter not stored */
2526 test->i = stktable_data_cast(ptr, sess_cnt);
2527 }
2528 return 1;
2529}
2530
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002531/* set test->i to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002532static int
Willy Tarreau56123282010-08-06 19:06:56 +02002533acl_fetch_sc1_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2534 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002535{
Willy Tarreau56123282010-08-06 19:06:56 +02002536 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002537 return 0;
2538
Willy Tarreau56123282010-08-06 19:06:56 +02002539 return acl_fetch_sess_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002540}
2541
2542/* set test->i to the cumulated number of sessions from the session's tracked BE counters */
2543static int
Willy Tarreau56123282010-08-06 19:06:56 +02002544acl_fetch_sc2_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2545 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002546{
Willy Tarreau56123282010-08-06 19:06:56 +02002547 if (!l4->stkctr2_entry)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002548 return 0;
2549
Willy Tarreau56123282010-08-06 19:06:56 +02002550 return acl_fetch_sess_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002551}
2552
2553/* set test->i to the cumulated number of session from the session's source
2554 * address in the table pointed to by expr.
2555 */
2556static int
2557acl_fetch_src_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2558 struct acl_expr *expr, struct acl_test *test)
2559{
2560 struct stktable_key *key;
2561
2562 key = tcpv4_src_to_stktable_key(l4);
2563 if (!key)
2564 return 0; /* only TCPv4 is supported right now */
2565
2566 if (expr->arg_len)
2567 px = find_stktable(expr->arg.str);
2568
2569 if (!px)
2570 return 0; /* table not found */
2571
2572 return acl_fetch_sess_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2573}
2574
Willy Tarreau91c43d72010-06-20 11:19:22 +02002575/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2576static int
2577acl_fetch_sess_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2578{
2579 test->flags = ACL_TEST_F_VOL_TEST;
2580 test->i = 0;
2581 if (ts != NULL) {
2582 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_RATE);
2583 if (!ptr)
2584 return 0; /* parameter not stored */
2585 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
2586 table->data_arg[STKTABLE_DT_SESS_RATE].u);
2587 }
2588 return 1;
2589}
2590
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002591/* set test->i to the session rate from the session's tracked FE counters over
2592 * the configured period.
2593 */
2594static int
Willy Tarreau56123282010-08-06 19:06:56 +02002595acl_fetch_sc1_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2596 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002597{
Willy Tarreau56123282010-08-06 19:06:56 +02002598 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002599 return 0;
2600
Willy Tarreau56123282010-08-06 19:06:56 +02002601 return acl_fetch_sess_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002602}
2603
2604/* set test->i to the session rate from the session's tracked BE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002605 * the configured period.
2606 */
2607static int
Willy Tarreau56123282010-08-06 19:06:56 +02002608acl_fetch_sc2_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2609 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002610{
Willy Tarreau56123282010-08-06 19:06:56 +02002611 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002612 return 0;
2613
Willy Tarreau56123282010-08-06 19:06:56 +02002614 return acl_fetch_sess_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002615}
2616
2617/* set test->i to the session rate from the session's source address in the
2618 * table pointed to by expr, over the configured period.
2619 */
2620static int
2621acl_fetch_src_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2622 struct acl_expr *expr, struct acl_test *test)
2623{
2624 struct stktable_key *key;
2625
2626 key = tcpv4_src_to_stktable_key(l4);
2627 if (!key)
2628 return 0; /* only TCPv4 is supported right now */
2629
2630 if (expr->arg_len)
2631 px = find_stktable(expr->arg.str);
2632
2633 if (!px)
2634 return 0; /* table not found */
2635
2636 return acl_fetch_sess_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2637}
2638
Willy Tarreauda7ff642010-06-23 11:44:09 +02002639/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2640static int
2641acl_fetch_http_req_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2642{
2643 test->flags = ACL_TEST_F_VOL_TEST;
2644 test->i = 0;
2645 if (ts != NULL) {
2646 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_CNT);
2647 if (!ptr)
2648 return 0; /* parameter not stored */
2649 test->i = stktable_data_cast(ptr, http_req_cnt);
2650 }
2651 return 1;
2652}
2653
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002654/* set test->i to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002655static int
Willy Tarreau56123282010-08-06 19:06:56 +02002656acl_fetch_sc1_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2657 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002658{
Willy Tarreau56123282010-08-06 19:06:56 +02002659 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002660 return 0;
2661
Willy Tarreau56123282010-08-06 19:06:56 +02002662 return acl_fetch_http_req_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002663}
2664
2665/* set test->i to the cumulated number of sessions from the session's tracked BE counters */
2666static int
Willy Tarreau56123282010-08-06 19:06:56 +02002667acl_fetch_sc2_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2668 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002669{
Willy Tarreau56123282010-08-06 19:06:56 +02002670 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002671 return 0;
2672
Willy Tarreau56123282010-08-06 19:06:56 +02002673 return acl_fetch_http_req_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002674}
2675
2676/* set test->i to the cumulated number of session from the session's source
2677 * address in the table pointed to by expr.
2678 */
2679static int
2680acl_fetch_src_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002681 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002682{
2683 struct stktable_key *key;
2684
2685 key = tcpv4_src_to_stktable_key(l4);
2686 if (!key)
2687 return 0; /* only TCPv4 is supported right now */
2688
2689 if (expr->arg_len)
2690 px = find_stktable(expr->arg.str);
2691
2692 if (!px)
2693 return 0; /* table not found */
2694
2695 return acl_fetch_http_req_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2696}
2697
2698/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2699static int
2700acl_fetch_http_req_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2701{
2702 test->flags = ACL_TEST_F_VOL_TEST;
2703 test->i = 0;
2704 if (ts != NULL) {
2705 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_RATE);
2706 if (!ptr)
2707 return 0; /* parameter not stored */
2708 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2709 table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
2710 }
2711 return 1;
2712}
2713
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002714/* set test->i to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02002715 * the configured period.
2716 */
2717static int
Willy Tarreau56123282010-08-06 19:06:56 +02002718acl_fetch_sc1_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2719 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002720{
Willy Tarreau56123282010-08-06 19:06:56 +02002721 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002722 return 0;
2723
Willy Tarreau56123282010-08-06 19:06:56 +02002724 return acl_fetch_http_req_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002725}
2726
2727/* set test->i to the session rate from the session's tracked BE counters over
2728 * the configured period.
2729 */
2730static int
Willy Tarreau56123282010-08-06 19:06:56 +02002731acl_fetch_sc2_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2732 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002733{
Willy Tarreau56123282010-08-06 19:06:56 +02002734 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002735 return 0;
2736
Willy Tarreau56123282010-08-06 19:06:56 +02002737 return acl_fetch_http_req_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002738}
2739
2740/* set test->i to the session rate from the session's source address in the
2741 * table pointed to by expr, over the configured period.
2742 */
2743static int
2744acl_fetch_src_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002745 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002746{
2747 struct stktable_key *key;
2748
2749 key = tcpv4_src_to_stktable_key(l4);
2750 if (!key)
2751 return 0; /* only TCPv4 is supported right now */
2752
2753 if (expr->arg_len)
2754 px = find_stktable(expr->arg.str);
2755
2756 if (!px)
2757 return 0; /* table not found */
2758
2759 return acl_fetch_http_req_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2760}
2761
2762/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2763static int
2764acl_fetch_http_err_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2765{
2766 test->flags = ACL_TEST_F_VOL_TEST;
2767 test->i = 0;
2768 if (ts != NULL) {
2769 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_CNT);
2770 if (!ptr)
2771 return 0; /* parameter not stored */
2772 test->i = stktable_data_cast(ptr, http_err_cnt);
2773 }
2774 return 1;
2775}
2776
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002777/* set test->i to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002778static int
Willy Tarreau56123282010-08-06 19:06:56 +02002779acl_fetch_sc1_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2780 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002781{
Willy Tarreau56123282010-08-06 19:06:56 +02002782 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002783 return 0;
2784
Willy Tarreau56123282010-08-06 19:06:56 +02002785 return acl_fetch_http_err_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002786}
2787
2788/* set test->i to the cumulated number of sessions from the session's tracked BE counters */
2789static int
Willy Tarreau56123282010-08-06 19:06:56 +02002790acl_fetch_sc2_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2791 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002792{
Willy Tarreau56123282010-08-06 19:06:56 +02002793 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002794 return 0;
2795
Willy Tarreau56123282010-08-06 19:06:56 +02002796 return acl_fetch_http_err_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002797}
2798
2799/* set test->i to the cumulated number of session from the session's source
2800 * address in the table pointed to by expr.
2801 */
2802static int
2803acl_fetch_src_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002804 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002805{
2806 struct stktable_key *key;
2807
2808 key = tcpv4_src_to_stktable_key(l4);
2809 if (!key)
2810 return 0; /* only TCPv4 is supported right now */
2811
2812 if (expr->arg_len)
2813 px = find_stktable(expr->arg.str);
2814
2815 if (!px)
2816 return 0; /* table not found */
2817
2818 return acl_fetch_http_err_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2819}
2820
2821/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2822static int
2823acl_fetch_http_err_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2824{
2825 test->flags = ACL_TEST_F_VOL_TEST;
2826 test->i = 0;
2827 if (ts != NULL) {
2828 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_RATE);
2829 if (!ptr)
2830 return 0; /* parameter not stored */
2831 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2832 table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
2833 }
2834 return 1;
2835}
2836
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002837/* set test->i to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02002838 * the configured period.
2839 */
2840static int
Willy Tarreau56123282010-08-06 19:06:56 +02002841acl_fetch_sc1_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2842 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002843{
Willy Tarreau56123282010-08-06 19:06:56 +02002844 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002845 return 0;
2846
Willy Tarreau56123282010-08-06 19:06:56 +02002847 return acl_fetch_http_err_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002848}
2849
2850/* set test->i to the session rate from the session's tracked BE counters over
2851 * the configured period.
2852 */
2853static int
Willy Tarreau56123282010-08-06 19:06:56 +02002854acl_fetch_sc2_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2855 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002856{
Willy Tarreau56123282010-08-06 19:06:56 +02002857 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002858 return 0;
2859
Willy Tarreau56123282010-08-06 19:06:56 +02002860 return acl_fetch_http_err_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002861}
2862
2863/* set test->i to the session rate from the session's source address in the
2864 * table pointed to by expr, over the configured period.
2865 */
2866static int
2867acl_fetch_src_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2868 struct acl_expr *expr, struct acl_test *test)
2869{
2870 struct stktable_key *key;
2871
2872 key = tcpv4_src_to_stktable_key(l4);
2873 if (!key)
2874 return 0; /* only TCPv4 is supported right now */
2875
2876 if (expr->arg_len)
2877 px = find_stktable(expr->arg.str);
2878
2879 if (!px)
2880 return 0; /* table not found */
2881
2882 return acl_fetch_http_err_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2883}
2884
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002885/* set test->i to the number of kbytes received from clients matching the stksess entry <ts> */
2886static int
2887acl_fetch_kbytes_in(struct stktable *table, struct acl_test *test, struct stksess *ts)
2888{
2889 test->flags = ACL_TEST_F_VOL_TEST;
2890 test->i = 0;
2891
2892 if (ts != NULL) {
2893 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_CNT);
2894 if (!ptr)
2895 return 0; /* parameter not stored */
2896 test->i = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
2897 }
2898 return 1;
2899}
2900
2901/* set test->i to the number of kbytes received from clients according to the
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002902 * session's tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002903 */
2904static int
Willy Tarreau56123282010-08-06 19:06:56 +02002905acl_fetch_sc1_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2906 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002907{
Willy Tarreau56123282010-08-06 19:06:56 +02002908 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002909 return 0;
2910
Willy Tarreau56123282010-08-06 19:06:56 +02002911 return acl_fetch_kbytes_in(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002912}
2913
2914/* set test->i to the number of kbytes received from clients according to the
2915 * session's tracked BE counters.
2916 */
2917static int
Willy Tarreau56123282010-08-06 19:06:56 +02002918acl_fetch_sc2_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2919 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002920{
Willy Tarreau56123282010-08-06 19:06:56 +02002921 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002922 return 0;
2923
Willy Tarreau56123282010-08-06 19:06:56 +02002924 return acl_fetch_kbytes_in(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002925}
2926
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002927/* set test->i to the number of kbytes received from the session's source
2928 * address in the table pointed to by expr.
2929 */
2930static int
2931acl_fetch_src_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2932 struct acl_expr *expr, struct acl_test *test)
2933{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002934 struct stktable_key *key;
2935
2936 key = tcpv4_src_to_stktable_key(l4);
2937 if (!key)
2938 return 0; /* only TCPv4 is supported right now */
2939
2940 if (expr->arg_len)
2941 px = find_stktable(expr->arg.str);
2942
2943 if (!px)
2944 return 0; /* table not found */
2945
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002946 return acl_fetch_kbytes_in(&px->table, test, stktable_lookup_key(&px->table, key));
2947}
2948
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002949/* set test->i to the bytes rate from clients in the stksess entry <ts> over the
2950 * configured period.
2951 */
2952static int
2953acl_fetch_bytes_in_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2954{
2955 test->flags = ACL_TEST_F_VOL_TEST;
2956 test->i = 0;
2957 if (ts != NULL) {
2958 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_RATE);
2959 if (!ptr)
2960 return 0; /* parameter not stored */
2961 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
2962 table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
2963 }
2964 return 1;
2965}
2966
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002967/* set test->i to the bytes rate from clients from the session's tracked FE
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002968 * counters over the configured period.
2969 */
2970static int
Willy Tarreau56123282010-08-06 19:06:56 +02002971acl_fetch_sc1_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2972 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002973{
Willy Tarreau56123282010-08-06 19:06:56 +02002974 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002975 return 0;
2976
Willy Tarreau56123282010-08-06 19:06:56 +02002977 return acl_fetch_bytes_in_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002978}
2979
2980/* set test->i to the bytes rate from clients from the session's tracked BE
2981 * counters over the configured period.
2982 */
2983static int
Willy Tarreau56123282010-08-06 19:06:56 +02002984acl_fetch_sc2_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2985 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002986{
Willy Tarreau56123282010-08-06 19:06:56 +02002987 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002988 return 0;
2989
Willy Tarreau56123282010-08-06 19:06:56 +02002990 return acl_fetch_bytes_in_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002991}
2992
2993/* set test->i to the bytes rate from clients from the session's source address
2994 * in the table pointed to by expr, over the configured period.
2995 */
2996static int
2997acl_fetch_src_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002998 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002999{
3000 struct stktable_key *key;
3001
3002 key = tcpv4_src_to_stktable_key(l4);
3003 if (!key)
3004 return 0; /* only TCPv4 is supported right now */
3005
3006 if (expr->arg_len)
3007 px = find_stktable(expr->arg.str);
3008
3009 if (!px)
3010 return 0; /* table not found */
3011
3012 return acl_fetch_bytes_in_rate(&px->table, test, stktable_lookup_key(&px->table, key));
3013}
3014
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003015/* set test->i to the number of kbytes sent to clients matching the stksess entry <ts> */
3016static int
3017acl_fetch_kbytes_out(struct stktable *table, struct acl_test *test, struct stksess *ts)
3018{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003019 test->flags = ACL_TEST_F_VOL_TEST;
3020 test->i = 0;
3021
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003022 if (ts != NULL) {
3023 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003024 if (!ptr)
3025 return 0; /* parameter not stored */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003026 test->i = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003027 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003028 return 1;
3029}
3030
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003031/* set test->i to the number of kbytes sent to clients according to the session's
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003032 * tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003033 */
3034static int
Willy Tarreau56123282010-08-06 19:06:56 +02003035acl_fetch_sc1_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3036 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003037{
Willy Tarreau56123282010-08-06 19:06:56 +02003038 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003039 return 0;
3040
Willy Tarreau56123282010-08-06 19:06:56 +02003041 return acl_fetch_kbytes_out(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003042}
3043
3044/* set test->i to the number of kbytes sent to clients according to the session's
3045 * tracked BE counters.
3046 */
3047static int
Willy Tarreau56123282010-08-06 19:06:56 +02003048acl_fetch_sc2_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3049 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003050{
Willy Tarreau56123282010-08-06 19:06:56 +02003051 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003052 return 0;
3053
Willy Tarreau56123282010-08-06 19:06:56 +02003054 return acl_fetch_kbytes_out(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003055}
3056
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003057/* set test->i to the number of kbytes sent to the session's source address in
3058 * the table pointed to by expr.
3059 */
3060static int
3061acl_fetch_src_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3062 struct acl_expr *expr, struct acl_test *test)
3063{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003064 struct stktable_key *key;
3065
3066 key = tcpv4_src_to_stktable_key(l4);
3067 if (!key)
3068 return 0; /* only TCPv4 is supported right now */
3069
3070 if (expr->arg_len)
3071 px = find_stktable(expr->arg.str);
3072
3073 if (!px)
3074 return 0; /* table not found */
3075
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003076 return acl_fetch_kbytes_out(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003077}
3078
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003079/* set test->i to the bytes rate to clients in the stksess entry <ts> over the
3080 * configured period.
3081 */
3082static int
3083acl_fetch_bytes_out_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
3084{
3085 test->flags = ACL_TEST_F_VOL_TEST;
3086 test->i = 0;
3087 if (ts != NULL) {
3088 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_RATE);
3089 if (!ptr)
3090 return 0; /* parameter not stored */
3091 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
3092 table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
3093 }
3094 return 1;
3095}
3096
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003097/* set test->i to the bytes rate to clients from the session's tracked FE counters
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003098 * over the configured period.
3099 */
3100static int
Willy Tarreau56123282010-08-06 19:06:56 +02003101acl_fetch_sc1_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3102 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003103{
Willy Tarreau56123282010-08-06 19:06:56 +02003104 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003105 return 0;
3106
Willy Tarreau56123282010-08-06 19:06:56 +02003107 return acl_fetch_bytes_out_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003108}
3109
3110/* set test->i to the bytes rate to clients from the session's tracked BE counters
3111 * over the configured period.
3112 */
3113static int
Willy Tarreau56123282010-08-06 19:06:56 +02003114acl_fetch_sc2_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3115 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003116{
Willy Tarreau56123282010-08-06 19:06:56 +02003117 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003118 return 0;
3119
Willy Tarreau56123282010-08-06 19:06:56 +02003120 return acl_fetch_bytes_out_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003121}
3122
3123/* set test->i to the bytes rate to client from the session's source address in
3124 * the table pointed to by expr, over the configured period.
3125 */
3126static int
3127acl_fetch_src_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02003128 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003129{
3130 struct stktable_key *key;
3131
3132 key = tcpv4_src_to_stktable_key(l4);
3133 if (!key)
3134 return 0; /* only TCPv4 is supported right now */
3135
3136 if (expr->arg_len)
3137 px = find_stktable(expr->arg.str);
3138
3139 if (!px)
3140 return 0; /* table not found */
3141
3142 return acl_fetch_bytes_out_rate(&px->table, test, stktable_lookup_key(&px->table, key));
3143}
3144
Willy Tarreau8b22a712010-06-18 17:46:06 +02003145
3146/* Note: must not be declared <const> as its list will be overwritten */
3147static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau56123282010-08-06 19:06:56 +02003148 { "sc1_get_gpc0", acl_parse_int, acl_fetch_sc1_get_gpc0, acl_match_int, ACL_USE_NOTHING },
3149 { "sc2_get_gpc0", acl_parse_int, acl_fetch_sc2_get_gpc0, acl_match_int, ACL_USE_NOTHING },
3150 { "src_get_gpc0", acl_parse_int, acl_fetch_src_get_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE },
3151 { "sc1_inc_gpc0", acl_parse_int, acl_fetch_sc1_inc_gpc0, acl_match_int, ACL_USE_NOTHING },
3152 { "sc2_inc_gpc0", acl_parse_int, acl_fetch_sc2_inc_gpc0, acl_match_int, ACL_USE_NOTHING },
3153 { "src_inc_gpc0", acl_parse_int, acl_fetch_src_inc_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE },
3154 { "sc1_conn_cnt", acl_parse_int, acl_fetch_sc1_conn_cnt, acl_match_int, ACL_USE_NOTHING },
3155 { "sc2_conn_cnt", acl_parse_int, acl_fetch_sc2_conn_cnt, acl_match_int, ACL_USE_NOTHING },
3156 { "src_conn_cnt", acl_parse_int, acl_fetch_src_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3157 { "sc1_conn_rate", acl_parse_int, acl_fetch_sc1_conn_rate, acl_match_int, ACL_USE_NOTHING },
3158 { "sc2_conn_rate", acl_parse_int, acl_fetch_sc2_conn_rate, acl_match_int, ACL_USE_NOTHING },
3159 { "src_conn_rate", acl_parse_int, acl_fetch_src_conn_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3160 { "src_updt_conn_cnt", acl_parse_int, acl_fetch_src_updt_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3161 { "sc1_conn_cur", acl_parse_int, acl_fetch_sc1_conn_cur, acl_match_int, ACL_USE_NOTHING },
3162 { "sc2_conn_cur", acl_parse_int, acl_fetch_sc2_conn_cur, acl_match_int, ACL_USE_NOTHING },
3163 { "src_conn_cur", acl_parse_int, acl_fetch_src_conn_cur, acl_match_int, ACL_USE_TCP4_VOLATILE },
3164 { "sc1_sess_cnt", acl_parse_int, acl_fetch_sc1_sess_cnt, acl_match_int, ACL_USE_NOTHING },
3165 { "sc2_sess_cnt", acl_parse_int, acl_fetch_sc2_sess_cnt, acl_match_int, ACL_USE_NOTHING },
3166 { "src_sess_cnt", acl_parse_int, acl_fetch_src_sess_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3167 { "sc1_sess_rate", acl_parse_int, acl_fetch_sc1_sess_rate, acl_match_int, ACL_USE_NOTHING },
3168 { "sc2_sess_rate", acl_parse_int, acl_fetch_sc2_sess_rate, acl_match_int, ACL_USE_NOTHING },
3169 { "src_sess_rate", acl_parse_int, acl_fetch_src_sess_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3170 { "sc1_http_req_cnt", acl_parse_int, acl_fetch_sc1_http_req_cnt, acl_match_int, ACL_USE_NOTHING },
3171 { "sc2_http_req_cnt", acl_parse_int, acl_fetch_sc2_http_req_cnt, acl_match_int, ACL_USE_NOTHING },
3172 { "src_http_req_cnt", acl_parse_int, acl_fetch_src_http_req_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3173 { "sc1_http_req_rate", acl_parse_int, acl_fetch_sc1_http_req_rate, acl_match_int, ACL_USE_NOTHING },
3174 { "sc2_http_req_rate", acl_parse_int, acl_fetch_sc2_http_req_rate, acl_match_int, ACL_USE_NOTHING },
3175 { "src_http_req_rate", acl_parse_int, acl_fetch_src_http_req_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3176 { "sc1_http_err_cnt", acl_parse_int, acl_fetch_sc1_http_err_cnt, acl_match_int, ACL_USE_NOTHING },
3177 { "sc2_http_err_cnt", acl_parse_int, acl_fetch_sc2_http_err_cnt, acl_match_int, ACL_USE_NOTHING },
3178 { "src_http_err_cnt", acl_parse_int, acl_fetch_src_http_err_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3179 { "sc1_http_err_rate", acl_parse_int, acl_fetch_sc1_http_err_rate, acl_match_int, ACL_USE_NOTHING },
3180 { "sc2_http_err_rate", acl_parse_int, acl_fetch_sc2_http_err_rate, acl_match_int, ACL_USE_NOTHING },
3181 { "src_http_err_rate", acl_parse_int, acl_fetch_src_http_err_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3182 { "sc1_kbytes_in", acl_parse_int, acl_fetch_sc1_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3183 { "sc2_kbytes_in", acl_parse_int, acl_fetch_sc2_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3184 { "src_kbytes_in", acl_parse_int, acl_fetch_src_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3185 { "sc1_bytes_in_rate", acl_parse_int, acl_fetch_sc1_bytes_in_rate, acl_match_int, ACL_USE_NOTHING },
3186 { "sc2_bytes_in_rate", acl_parse_int, acl_fetch_sc2_bytes_in_rate, acl_match_int, ACL_USE_NOTHING },
3187 { "src_bytes_in_rate", acl_parse_int, acl_fetch_src_bytes_in_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3188 { "sc1_kbytes_out", acl_parse_int, acl_fetch_sc1_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3189 { "sc2_kbytes_out", acl_parse_int, acl_fetch_sc2_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3190 { "src_kbytes_out", acl_parse_int, acl_fetch_src_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3191 { "sc1_bytes_out_rate", acl_parse_int, acl_fetch_sc1_bytes_out_rate, acl_match_int, ACL_USE_NOTHING },
3192 { "sc2_bytes_out_rate", acl_parse_int, acl_fetch_sc2_bytes_out_rate, acl_match_int, ACL_USE_NOTHING },
3193 { "src_bytes_out_rate", acl_parse_int, acl_fetch_src_bytes_out_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003194 { NULL, NULL, NULL, NULL },
3195}};
3196
3197
Willy Tarreau56123282010-08-06 19:06:56 +02003198/* Parse a "track-sc[12]" line starting with "track-sc[12]" in args[arg-1].
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003199 * Returns the number of warnings emitted, or -1 in case of fatal errors. The
3200 * <prm> struct is fed with the table name if any. If unspecified, the caller
3201 * will assume that the current proxy's table is used.
3202 */
3203int parse_track_counters(char **args, int *arg,
3204 int section_type, struct proxy *curpx,
3205 struct track_ctr_prm *prm,
3206 struct proxy *defpx, char *err, int errlen)
3207{
3208 int pattern_type = 0;
Willy Tarreau56123282010-08-06 19:06:56 +02003209 char *kw = args[*arg - 1];
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003210
Willy Tarreau56123282010-08-06 19:06:56 +02003211 /* parse the arguments of "track-sc[12]" before the condition in the
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003212 * following form :
Willy Tarreau56123282010-08-06 19:06:56 +02003213 * track-sc[12] src [ table xxx ] [ if|unless ... ]
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003214 */
3215 while (args[*arg]) {
3216 if (strcmp(args[*arg], "src") == 0) {
3217 prm->type = STKTABLE_TYPE_IP;
3218 pattern_type = 1;
3219 }
3220 else if (strcmp(args[*arg], "table") == 0) {
3221 if (!args[*arg + 1]) {
3222 snprintf(err, errlen,
Willy Tarreau56123282010-08-06 19:06:56 +02003223 "missing table for %s in %s '%s'.",
3224 kw, proxy_type_str(curpx), curpx->id);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003225 return -1;
3226 }
3227 /* we copy the table name for now, it will be resolved later */
3228 prm->table.n = strdup(args[*arg + 1]);
3229 (*arg)++;
3230 }
3231 else {
3232 /* unhandled keywords are handled by the caller */
3233 break;
3234 }
3235 (*arg)++;
3236 }
3237
3238 if (!pattern_type) {
3239 snprintf(err, errlen,
Willy Tarreau56123282010-08-06 19:06:56 +02003240 "%s key not specified in %s '%s' (found %s, only 'src' is supported).",
3241 kw, proxy_type_str(curpx), curpx->id, quote_arg(args[*arg]));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003242 return -1;
3243 }
3244
3245 return 0;
3246}
3247
Willy Tarreau8b22a712010-06-18 17:46:06 +02003248__attribute__((constructor))
3249static void __session_init(void)
3250{
3251 acl_register_keywords(&acl_kws);
3252}
3253
Willy Tarreaubaaee002006-06-26 02:48:02 +02003254/*
3255 * Local variables:
3256 * c-indent-level: 8
3257 * c-basic-offset: 8
3258 * End:
3259 */