blob: dd130e0258910a2d4fb74e594105ec245d39a3bb [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 Tarreau8d5d7f22007-01-21 19:16:41 +010030#include <proto/hdr_idx.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020031#include <proto/log.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020032#include <proto/session.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010033#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010034#include <proto/proto_http.h>
35#include <proto/proto_tcp.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020036#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010038#include <proto/server.h>
Emeric Brun1d33b292010-01-04 15:47:17 +010039#include <proto/stick_table.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010040#include <proto/stream_interface.h>
41#include <proto/stream_sock.h>
42#include <proto/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020044struct pool_head *pool2_session;
Willy Tarreauf54f8bd2008-11-23 19:53:55 +010045struct list sessions;
Willy Tarreaubaaee002006-06-26 02:48:02 +020046
Willy Tarreau81f9aa32010-06-01 17:45:26 +020047/* This function is called from the protocol layer accept() in order to instanciate
48 * a new session on behalf of a given listener and frontend. It returns a positive
49 * value upon success, 0 if the connection needs to be closed and ignored, or a
50 * negative value upon critical failure.
51 */
52int session_accept(struct listener *l, int cfd, struct sockaddr_storage *addr)
53{
54 struct proxy *p = l->frontend;
55 struct session *s;
56 struct http_txn *txn;
57 struct task *t;
58
59 if (unlikely((s = pool_alloc2(pool2_session)) == NULL)) {
60 Alert("out of memory in event_accept().\n");
61 goto out_close;
62 }
63
64 /* minimum session initialization required for monitor mode below */
65 s->flags = 0;
66 s->logs.logwait = p->to_log;
Willy Tarreau56123282010-08-06 19:06:56 +020067 s->stkctr1_entry = NULL;
68 s->stkctr2_entry = NULL;
69 s->stkctr1_table = NULL;
70 s->stkctr2_table = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020071
72 /* if this session comes from a known monitoring system, we want to ignore
73 * it as soon as possible, which means closing it immediately for TCP, but
74 * cleanly.
75 */
76 if (unlikely((l->options & LI_O_CHK_MONNET) &&
77 addr->ss_family == AF_INET &&
78 (((struct sockaddr_in *)addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr)) {
79 if (p->mode == PR_MODE_TCP) {
80 pool_free2(pool2_session, s);
81 return 0;
82 }
83 s->flags |= SN_MONITOR;
84 s->logs.logwait = 0;
85 }
86
87 /* OK, we're keeping the session, so let's properly initialize the session */
88 LIST_ADDQ(&sessions, &s->list);
89 LIST_INIT(&s->back_refs);
90
91 if (unlikely((t = task_new()) == NULL)) { /* disable this proxy for a while */
92 Alert("out of memory in event_accept().\n");
93 goto out_free_session;
94 }
95
96 s->term_trace = 0;
97 s->cli_addr = *addr;
98 s->logs.accept_date = date; /* user-visible date for logging */
99 s->logs.tv_accept = now; /* corrected date for internal use */
100 s->uniq_id = totalconn;
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200101 p->feconn++; /* beconn will be increased once assigned */
102
Willy Tarreaub36b4242010-06-04 20:59:39 +0200103 proxy_inc_fe_conn_ctr(l, p); /* note: cum_beconn will be increased once assigned */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200104
105 t->process = l->handler;
106 t->context = s;
107 t->nice = l->nice;
108 t->expire = TICK_ETERNITY;
109
110 s->task = t;
111 s->listener = l;
112
113 /* Note: initially, the session's backend points to the frontend.
114 * This changes later when switching rules are executed or
115 * when the default backend is assigned.
116 */
117 s->be = s->fe = p;
118 s->req = s->rep = NULL; /* will be allocated later */
119
120 /* now evaluate the tcp-request layer4 rules. Since we expect to be able
121 * to abort right here as soon as possible, we check the rules before
122 * even initializing the stream interfaces.
123 */
124 if ((l->options & LI_O_TCP_RULES) && !tcp_exec_req_rules(s)) {
Willy Tarreau56123282010-08-06 19:06:56 +0200125 if (s->stkctr1_entry || s->stkctr2_entry)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200126 session_store_counters(s);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200127 task_free(t);
128 LIST_DEL(&s->list);
129 pool_free2(pool2_session, s);
130 /* let's do a no-linger now to close with a single RST. */
131 setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200132 p->feconn--;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200133 return 0;
134 }
135
Willy Tarreaub36b4242010-06-04 20:59:39 +0200136 /* This session was accepted, count it now */
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200137 if (p->feconn > p->counters.feconn_max)
138 p->counters.feconn_max = p->feconn;
Willy Tarreaub36b4242010-06-04 20:59:39 +0200139 proxy_inc_fe_sess_ctr(l, p);
Willy Tarreau56123282010-08-06 19:06:56 +0200140 if (s->stkctr1_entry) {
Willy Tarreau91c43d72010-06-20 11:19:22 +0200141 void *ptr;
142
Willy Tarreau56123282010-08-06 19:06:56 +0200143 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200144 if (ptr)
145 stktable_data_cast(ptr, sess_cnt)++;
Willy Tarreau91c43d72010-06-20 11:19:22 +0200146
Willy Tarreau56123282010-08-06 19:06:56 +0200147 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +0200148 if (ptr)
149 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200150 s->stkctr1_table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200151 }
Willy Tarreaub36b4242010-06-04 20:59:39 +0200152
Willy Tarreau56123282010-08-06 19:06:56 +0200153 if (s->stkctr2_entry) {
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200154 void *ptr;
155
Willy Tarreau56123282010-08-06 19:06:56 +0200156 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_SESS_CNT);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200157 if (ptr)
158 stktable_data_cast(ptr, sess_cnt)++;
159
Willy Tarreau56123282010-08-06 19:06:56 +0200160 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_SESS_RATE);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200161 if (ptr)
162 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200163 s->stkctr2_table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200164 }
165
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200166 /* this part should be common with other protocols */
167 s->si[0].fd = cfd;
168 s->si[0].owner = t;
169 s->si[0].state = s->si[0].prev_state = SI_ST_EST;
170 s->si[0].err_type = SI_ET_NONE;
171 s->si[0].err_loc = NULL;
172 s->si[0].connect = NULL;
173 s->si[0].iohandler = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200174 s->si[0].release = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200175 s->si[0].exp = TICK_ETERNITY;
176 s->si[0].flags = SI_FL_NONE;
177
178 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
179 s->si[0].flags |= SI_FL_INDEP_STR;
180
181 if (addr->ss_family == AF_INET || addr->ss_family == AF_INET6)
182 s->si[0].flags = SI_FL_CAP_SPLTCP; /* TCP/TCPv6 splicing possible */
183
184 /* add the various callbacks */
185 stream_sock_prepare_interface(&s->si[0]);
186
187 /* pre-initialize the other side's stream interface to an INIT state. The
188 * callbacks will be initialized before attempting to connect.
189 */
190 s->si[1].fd = -1; /* just to help with debugging */
191 s->si[1].owner = t;
192 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
193 s->si[1].err_type = SI_ET_NONE;
194 s->si[1].err_loc = NULL;
195 s->si[1].connect = NULL;
196 s->si[1].iohandler = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200197 s->si[1].release = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200198 s->si[1].shutr = stream_int_shutr;
199 s->si[1].shutw = stream_int_shutw;
200 s->si[1].exp = TICK_ETERNITY;
201 s->si[1].flags = SI_FL_NONE;
202
203 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
204 s->si[1].flags |= SI_FL_INDEP_STR;
205
206 s->srv = s->prev_srv = s->srv_conn = NULL;
207 s->pend_pos = NULL;
208
209 /* init store persistence */
210 s->store_count = 0;
211
212 /* Adjust some socket options */
213 if (unlikely(fcntl(cfd, F_SETFL, O_NONBLOCK) == -1)) {
214 Alert("accept(): cannot set the socket in non blocking mode. Giving up\n");
215 goto out_free_task;
216 }
217
218 txn = &s->txn;
219 /* Those variables will be checked and freed if non-NULL in
220 * session.c:session_free(). It is important that they are
221 * properly initialized.
222 */
223 txn->sessid = NULL;
224 txn->srv_cookie = NULL;
225 txn->cli_cookie = NULL;
226 txn->uri = NULL;
227 txn->req.cap = NULL;
228 txn->rsp.cap = NULL;
229 txn->hdr_idx.v = NULL;
230 txn->hdr_idx.size = txn->hdr_idx.used = 0;
231
232 if (unlikely((s->req = pool_alloc2(pool2_buffer)) == NULL))
233 goto out_free_task; /* no memory */
234
235 if (unlikely((s->rep = pool_alloc2(pool2_buffer)) == NULL))
236 goto out_free_req; /* no memory */
237
238 /* initialize the request buffer */
239 s->req->size = global.tune.bufsize;
240 buffer_init(s->req);
241 s->req->prod = &s->si[0];
242 s->req->cons = &s->si[1];
243 s->si[0].ib = s->si[1].ob = s->req;
244 s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */
245
246 /* activate default analysers enabled for this listener */
247 s->req->analysers = l->analysers;
248
249 s->req->wto = TICK_ETERNITY;
250 s->req->rto = TICK_ETERNITY;
251 s->req->rex = TICK_ETERNITY;
252 s->req->wex = TICK_ETERNITY;
253 s->req->analyse_exp = TICK_ETERNITY;
254
255 /* initialize response buffer */
256 s->rep->size = global.tune.bufsize;
257 buffer_init(s->rep);
258 s->rep->prod = &s->si[1];
259 s->rep->cons = &s->si[0];
260 s->si[0].ob = s->si[1].ib = s->rep;
261 s->rep->analysers = 0;
262
263 s->rep->rto = TICK_ETERNITY;
264 s->rep->wto = TICK_ETERNITY;
265 s->rep->rex = TICK_ETERNITY;
266 s->rep->wex = TICK_ETERNITY;
267 s->rep->analyse_exp = TICK_ETERNITY;
268
269 /* finish initialization of the accepted file descriptor */
270 fd_insert(cfd);
271 fdtab[cfd].owner = &s->si[0];
272 fdtab[cfd].state = FD_STREADY;
273 fdtab[cfd].flags = 0;
274 fdtab[cfd].cb[DIR_RD].f = l->proto->read;
275 fdtab[cfd].cb[DIR_RD].b = s->req;
276 fdtab[cfd].cb[DIR_WR].f = l->proto->write;
277 fdtab[cfd].cb[DIR_WR].b = s->rep;
278 fdinfo[cfd].peeraddr = (struct sockaddr *)&s->cli_addr;
279 fdinfo[cfd].peerlen = sizeof(s->cli_addr);
280 EV_FD_SET(cfd, DIR_RD);
281
282 if (p->accept) {
283 int ret = p->accept(s);
284 if (unlikely(ret < 0))
285 goto out_free_rep;
286
287 if (unlikely(ret == 0)) {
288 /* work is finished, we can release everything (eg: monitoring) */
289 pool_free2(pool2_buffer, s->rep);
290 pool_free2(pool2_buffer, s->req);
Willy Tarreau56123282010-08-06 19:06:56 +0200291 if (s->stkctr1_entry || s->stkctr2_entry)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200292 session_store_counters(s);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200293 task_free(t);
294 LIST_DEL(&s->list);
295 pool_free2(pool2_session, s);
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200296 p->feconn--;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200297 return 0;
298 }
299 }
300
301 /* it is important not to call the wakeup function directly but to
302 * pass through task_wakeup(), because this one knows how to apply
303 * priorities to tasks.
304 */
305 task_wakeup(t, TASK_WOKEN_INIT);
306 return 1;
307
308 /* Error unrolling */
309 out_free_rep:
310 pool_free2(pool2_buffer, s->rep);
311 out_free_req:
312 pool_free2(pool2_buffer, s->req);
313 out_free_task:
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200314 p->feconn--;
Willy Tarreau56123282010-08-06 19:06:56 +0200315 if (s->stkctr1_entry || s->stkctr2_entry)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200316 session_store_counters(s);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200317 task_free(t);
318 out_free_session:
319 LIST_DEL(&s->list);
320 pool_free2(pool2_session, s);
321 out_close:
322 return -1;
323}
324
Willy Tarreaubaaee002006-06-26 02:48:02 +0200325/*
326 * frees the context associated to a session. It must have been removed first.
327 */
328void session_free(struct session *s)
329{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +0100330 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +0200331 struct proxy *fe = s->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100332 struct bref *bref, *back;
Willy Tarreaua4cda672010-06-06 18:28:49 +0200333 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100334
Willy Tarreaubaaee002006-06-26 02:48:02 +0200335 if (s->pend_pos)
336 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100337
Willy Tarreau1e62de62008-11-11 20:20:02 +0100338 if (s->srv) { /* there may be requests left pending in queue */
339 if (s->flags & SN_CURR_SESS) {
340 s->flags &= ~SN_CURR_SESS;
341 s->srv->cur_sess--;
342 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100343 if (may_dequeue_tasks(s->srv, s->be))
344 process_srv_queue(s->srv);
Willy Tarreau1e62de62008-11-11 20:20:02 +0100345 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100346
Willy Tarreau7c669d72008-06-20 15:04:11 +0200347 if (unlikely(s->srv_conn)) {
348 /* the session still has a reserved slot on a server, but
349 * it should normally be only the same as the one above,
350 * so this should not happen in fact.
351 */
352 sess_change_server(s, NULL);
353 }
354
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100355 if (s->req->pipe)
356 put_pipe(s->req->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100357
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100358 if (s->rep->pipe)
359 put_pipe(s->rep->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100360
Willy Tarreau48d63db2008-08-03 17:41:33 +0200361 pool_free2(pool2_buffer, s->req);
362 pool_free2(pool2_buffer, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200363
Willy Tarreau46023632010-01-07 22:51:47 +0100364 http_end_txn(s);
365
Willy Tarreaua4cda672010-06-06 18:28:49 +0200366 for (i = 0; i < s->store_count; i++) {
367 if (!s->store[i].ts)
368 continue;
369 stksess_free(s->store[i].table, s->store[i].ts);
370 s->store[i].ts = NULL;
371 }
372
Willy Tarreau92fb9832007-10-16 17:34:28 +0200373 if (fe) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200374 pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau46023632010-01-07 22:51:47 +0100375 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
376 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200377 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100378
Willy Tarreau56123282010-08-06 19:06:56 +0200379 if (s->stkctr1_entry || s->stkctr2_entry)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200380 session_store_counters(s);
381
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100382 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100383 /* we have to unlink all watchers. We must not relink them if
384 * this session was the last one in the list.
385 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100386 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100387 LIST_INIT(&bref->users);
388 if (s->list.n != &sessions)
389 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100390 bref->ref = s->list.n;
391 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100392 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200393 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200394
395 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200396 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200397 pool_flush2(pool2_buffer);
398 pool_flush2(fe->hdr_idx_pool);
399 pool_flush2(pool2_requri);
400 pool_flush2(pool2_capture);
401 pool_flush2(pool2_session);
402 pool_flush2(fe->req_cap_pool);
403 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200404 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200405}
406
407
408/* perform minimal intializations, report 0 in case of error, 1 if OK. */
409int init_session()
410{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100411 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200412 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
413 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200414}
415
Willy Tarreau30e71012007-11-26 20:15:35 +0100416void session_process_counters(struct session *s)
417{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100418 unsigned long long bytes;
419
Willy Tarreau30e71012007-11-26 20:15:35 +0100420 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100421 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100422 s->logs.bytes_in = s->req->total;
423 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200424 s->fe->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100425
Willy Tarreau30e71012007-11-26 20:15:35 +0100426 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200427 s->be->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100428
Willy Tarreau30e71012007-11-26 20:15:35 +0100429 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200430 s->srv->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200431
432 if (s->listener->counters)
433 s->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200434
Willy Tarreau56123282010-08-06 19:06:56 +0200435 if (s->stkctr2_entry) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200436 void *ptr;
437
Willy Tarreau56123282010-08-06 19:06:56 +0200438 ptr = stktable_data_ptr(s->stkctr2_table,
439 s->stkctr2_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200440 STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200441 if (ptr)
442 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200443
Willy Tarreau56123282010-08-06 19:06:56 +0200444 ptr = stktable_data_ptr(s->stkctr2_table,
445 s->stkctr2_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200446 STKTABLE_DT_BYTES_IN_RATE);
447 if (ptr)
448 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200449 s->stkctr2_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200450 }
451
Willy Tarreau56123282010-08-06 19:06:56 +0200452 if (s->stkctr1_entry) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200453 void *ptr;
454
Willy Tarreau56123282010-08-06 19:06:56 +0200455 ptr = stktable_data_ptr(s->stkctr1_table,
456 s->stkctr1_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200457 STKTABLE_DT_BYTES_IN_CNT);
458 if (ptr)
459 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
460
Willy Tarreau56123282010-08-06 19:06:56 +0200461 ptr = stktable_data_ptr(s->stkctr1_table,
462 s->stkctr1_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200463 STKTABLE_DT_BYTES_IN_RATE);
464 if (ptr)
465 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200466 s->stkctr1_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200467 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100468 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100469 }
470
Willy Tarreau30e71012007-11-26 20:15:35 +0100471 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100472 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100473 s->logs.bytes_out = s->rep->total;
474 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200475 s->fe->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100476
Willy Tarreau30e71012007-11-26 20:15:35 +0100477 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200478 s->be->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100479
Willy Tarreau30e71012007-11-26 20:15:35 +0100480 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200481 s->srv->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200482
483 if (s->listener->counters)
484 s->listener->counters->bytes_out += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200485
Willy Tarreau56123282010-08-06 19:06:56 +0200486 if (s->stkctr2_entry) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200487 void *ptr;
488
Willy Tarreau56123282010-08-06 19:06:56 +0200489 ptr = stktable_data_ptr(s->stkctr2_table,
490 s->stkctr2_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200491 STKTABLE_DT_BYTES_OUT_CNT);
492 if (ptr)
493 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
494
Willy Tarreau56123282010-08-06 19:06:56 +0200495 ptr = stktable_data_ptr(s->stkctr2_table,
496 s->stkctr2_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200497 STKTABLE_DT_BYTES_OUT_RATE);
498 if (ptr)
499 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200500 s->stkctr2_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200501 }
502
Willy Tarreau56123282010-08-06 19:06:56 +0200503 if (s->stkctr1_entry) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200504 void *ptr;
505
Willy Tarreau56123282010-08-06 19:06:56 +0200506 ptr = stktable_data_ptr(s->stkctr1_table,
507 s->stkctr1_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200508 STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200509 if (ptr)
510 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200511
Willy Tarreau56123282010-08-06 19:06:56 +0200512 ptr = stktable_data_ptr(s->stkctr1_table,
513 s->stkctr1_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200514 STKTABLE_DT_BYTES_OUT_RATE);
515 if (ptr)
516 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200517 s->stkctr1_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200518 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100519 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100520 }
521}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200522
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100523/* This function is called with (si->state == SI_ST_CON) meaning that a
524 * connection was attempted and that the file descriptor is already allocated.
525 * We must check for establishment, error and abort. Possible output states
526 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
527 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
528 * otherwise 1.
529 */
530int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
531{
532 struct buffer *req = si->ob;
533 struct buffer *rep = si->ib;
534
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100535 /* If we got an error, or if nothing happened and the connection timed
536 * out, we must give up. The CER state handler will take care of retry
537 * attempts and error reports.
538 */
539 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau127334e2009-03-28 10:47:26 +0100540 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100541 si->state = SI_ST_CER;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200542 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100543 fd_delete(si->fd);
544
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200545 if (si->release)
546 si->release(si);
547
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100548 if (si->err_type)
549 return 0;
550
551 si->err_loc = s->srv;
552 if (si->flags & SI_FL_ERR)
553 si->err_type = SI_ET_CONN_ERR;
554 else
555 si->err_type = SI_ET_CONN_TO;
556 return 0;
557 }
558
559 /* OK, maybe we want to abort */
Willy Tarreau418fd472009-09-06 21:37:23 +0200560 if (unlikely((rep->flags & BF_SHUTW) ||
561 ((req->flags & BF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200562 (((req->flags & (BF_OUT_EMPTY|BF_WRITE_ACTIVITY)) == BF_OUT_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100563 s->be->options & PR_O_ABRT_CLOSE)))) {
564 /* give up */
565 si->shutw(si);
566 si->err_type |= SI_ET_CONN_ABRT;
567 si->err_loc = s->srv;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200568 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau84455332009-03-15 22:34:05 +0100569 if (s->srv_error)
570 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100571 return 1;
572 }
573
574 /* we need to wait a bit more if there was no activity either */
575 if (!(req->flags & BF_WRITE_ACTIVITY))
576 return 1;
577
578 /* OK, this means that a connection succeeded. The caller will be
579 * responsible for handling the transition from CON to EST.
580 */
581 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100582 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100583 si->state = SI_ST_EST;
584 si->err_type = SI_ET_NONE;
585 si->err_loc = NULL;
586 return 1;
587}
588
589/* This function is called with (si->state == SI_ST_CER) meaning that a
590 * previous connection attempt has failed and that the file descriptor
591 * has already been released. Possible causes include asynchronous error
592 * notification and time out. Possible output states are SI_ST_CLO when
593 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
594 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
595 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
596 * marked as in error state. It returns 0.
597 */
598int sess_update_st_cer(struct session *s, struct stream_interface *si)
599{
600 /* we probably have to release last session from the server */
601 if (s->srv) {
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100602 health_adjust(s->srv, HANA_STATUS_L4_ERR);
603
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100604 if (s->flags & SN_CURR_SESS) {
605 s->flags &= ~SN_CURR_SESS;
606 s->srv->cur_sess--;
607 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100608 }
609
610 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200611 si->conn_retries--;
612 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100613 if (!si->err_type) {
614 si->err_type = SI_ET_CONN_ERR;
615 si->err_loc = s->srv;
616 }
617
618 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200619 s->srv->counters.failed_conns++;
620 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100621 if (may_dequeue_tasks(s->srv, s->be))
622 process_srv_queue(s->srv);
623
624 /* shutw is enough so stop a connecting socket */
625 si->shutw(si);
626 si->ob->flags |= BF_WRITE_ERROR;
627 si->ib->flags |= BF_READ_ERROR;
628
629 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100630 if (s->srv_error)
631 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100632 return 0;
633 }
634
635 /* If the "redispatch" option is set on the backend, we are allowed to
636 * retry on another server for the last retry. In order to achieve this,
637 * we must mark the session unassigned, and eventually clear the DIRECT
638 * bit to ignore any persistence cookie. We won't count a retry nor a
639 * redispatch yet, because this will depend on what server is selected.
640 */
Willy Tarreauee28de02010-06-01 09:51:00 +0200641 if (s->srv && si->conn_retries == 0 &&
Willy Tarreau4de91492010-01-22 19:10:05 +0100642 s->be->options & PR_O_REDISP && !(s->flags & SN_FORCE_PRST)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100643 if (may_dequeue_tasks(s->srv, s->be))
644 process_srv_queue(s->srv);
645
646 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
647 s->prev_srv = s->srv;
648 si->state = SI_ST_REQ;
649 } else {
650 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200651 s->srv->counters.retries++;
652 s->be->counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100653 si->state = SI_ST_ASS;
654 }
655
656 if (si->flags & SI_FL_ERR) {
657 /* The error was an asynchronous connection error, and we will
658 * likely have to retry connecting to the same server, most
659 * likely leading to the same result. To avoid this, we wait
660 * one second before retrying.
661 */
662
663 if (!si->err_type)
664 si->err_type = SI_ET_CONN_ERR;
665
666 si->state = SI_ST_TAR;
667 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
668 return 0;
669 }
670 return 0;
671}
672
673/*
674 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200675 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
676 * SI_ST_INI) to SI_ST_EST, but only when a ->connect function is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100677 */
678void sess_establish(struct session *s, struct stream_interface *si)
679{
680 struct buffer *req = si->ob;
681 struct buffer *rep = si->ib;
682
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100683 if (s->srv)
684 health_adjust(s->srv, HANA_STATUS_L4_OK);
685
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100686 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100687 /* if the user wants to log as soon as possible, without counting
688 * bytes from the server, then this is the right moment. */
689 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
690 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100691 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100692 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100693 }
694 else {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100695 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
696 /* reset hdr_idx which was already initialized by the request.
697 * right now, the http parser does it.
698 * hdr_idx_init(&s->txn.hdr_idx);
699 */
700 }
701
Willy Tarreau4e5b8282009-08-16 22:57:50 +0200702 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100703 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
Willy Tarreaud04e8582010-05-31 12:31:35 +0200704 if (si->connect) {
705 /* real connections have timeouts */
706 req->wto = s->be->timeout.server;
707 rep->rto = s->be->timeout.server;
708 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100709 req->wex = TICK_ETERNITY;
710}
711
712/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
713 * Other input states are simply ignored.
714 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
715 * Flags must have previously been updated for timeouts and other conditions.
716 */
717void sess_update_stream_int(struct session *s, struct stream_interface *si)
718{
719 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",
720 now_ms, __FUNCTION__,
721 s,
722 s->req, s->rep,
723 s->req->rex, s->rep->wex,
724 s->req->flags, s->rep->flags,
725 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
726
727 if (si->state == SI_ST_ASS) {
728 /* Server assigned to connection request, we have to try to connect now */
729 int conn_err;
730
731 conn_err = connect_server(s);
732 if (conn_err == SN_ERR_NONE) {
733 /* state = SI_ST_CON now */
Willy Tarreau8f6457c2008-12-01 00:08:28 +0100734 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100735 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100736 return;
737 }
738
739 /* We have received a synchronous error. We might have to
740 * abort, retry immediately or redispatch.
741 */
742 if (conn_err == SN_ERR_INTERNAL) {
743 if (!si->err_type) {
744 si->err_type = SI_ET_CONN_OTHER;
745 si->err_loc = s->srv;
746 }
747
748 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100749 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100750 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200751 s->srv->counters.failed_conns++;
752 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100753
754 /* release other sessions waiting for this server */
755 if (may_dequeue_tasks(s->srv, s->be))
756 process_srv_queue(s->srv);
757
758 /* Failed and not retryable. */
759 si->shutr(si);
760 si->shutw(si);
761 si->ob->flags |= BF_WRITE_ERROR;
762
763 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
764
765 /* no session was ever accounted for this server */
766 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100767 if (s->srv_error)
768 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100769 return;
770 }
771
772 /* We are facing a retryable error, but we don't want to run a
773 * turn-around now, as the problem is likely a source port
774 * allocation problem, so we want to retry now.
775 */
776 si->state = SI_ST_CER;
777 si->flags &= ~SI_FL_ERR;
778 sess_update_st_cer(s, si);
779 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
780 return;
781 }
782 else if (si->state == SI_ST_QUE) {
783 /* connection request was queued, check for any update */
784 if (!s->pend_pos) {
785 /* The connection is not in the queue anymore. Either
786 * we have a server connection slot available and we
787 * go directly to the assigned state, or we need to
788 * load-balance first and go to the INI state.
789 */
790 si->exp = TICK_ETERNITY;
791 if (unlikely(!(s->flags & SN_ASSIGNED)))
792 si->state = SI_ST_REQ;
793 else {
794 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
795 si->state = SI_ST_ASS;
796 }
797 return;
798 }
799
800 /* Connection request still in queue... */
801 if (si->flags & SI_FL_EXP) {
802 /* ... and timeout expired */
803 si->exp = TICK_ETERNITY;
804 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
805 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200806 s->srv->counters.failed_conns++;
807 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100808 si->shutr(si);
809 si->shutw(si);
810 si->ob->flags |= BF_WRITE_TIMEOUT;
811 if (!si->err_type)
812 si->err_type = SI_ET_QUEUE_TO;
813 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100814 if (s->srv_error)
815 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100816 return;
817 }
818
819 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau418fd472009-09-06 21:37:23 +0200820 if ((si->ob->flags & (BF_READ_ERROR)) ||
821 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200822 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100823 /* give up */
824 si->exp = TICK_ETERNITY;
825 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
826 si->shutr(si);
827 si->shutw(si);
828 si->err_type |= SI_ET_QUEUE_ABRT;
829 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100830 if (s->srv_error)
831 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100832 return;
833 }
834
835 /* Nothing changed */
836 return;
837 }
838 else if (si->state == SI_ST_TAR) {
839 /* Connection request might be aborted */
Willy Tarreau418fd472009-09-06 21:37:23 +0200840 if ((si->ob->flags & (BF_READ_ERROR)) ||
841 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200842 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100843 /* give up */
844 si->exp = TICK_ETERNITY;
845 si->shutr(si);
846 si->shutw(si);
847 si->err_type |= SI_ET_CONN_ABRT;
848 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100849 if (s->srv_error)
850 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100851 return;
852 }
853
854 if (!(si->flags & SI_FL_EXP))
855 return; /* still in turn-around */
856
857 si->exp = TICK_ETERNITY;
858
859 /* we keep trying on the same server as long as the session is
860 * marked "assigned".
861 * FIXME: Should we force a redispatch attempt when the server is down ?
862 */
863 if (s->flags & SN_ASSIGNED)
864 si->state = SI_ST_ASS;
865 else
866 si->state = SI_ST_REQ;
867 return;
868 }
869}
870
871/* This function initiates a server connection request on a stream interface
872 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
873 * indicating that a server has been assigned. It may also return SI_ST_QUE,
874 * or SI_ST_CLO upon error.
875 */
876static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
877 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",
878 now_ms, __FUNCTION__,
879 s,
880 s->req, s->rep,
881 s->req->rex, s->rep->wex,
882 s->req->flags, s->rep->flags,
883 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
884
885 if (si->state != SI_ST_REQ)
886 return;
887
888 /* Try to assign a server */
889 if (srv_redispatch_connect(s) != 0) {
890 /* We did not get a server. Either we queued the
891 * connection request, or we encountered an error.
892 */
893 if (si->state == SI_ST_QUE)
894 return;
895
896 /* we did not get any server, let's check the cause */
897 si->shutr(si);
898 si->shutw(si);
899 si->ob->flags |= BF_WRITE_ERROR;
900 if (!si->err_type)
901 si->err_type = SI_ET_CONN_OTHER;
902 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100903 if (s->srv_error)
904 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100905 return;
906 }
907
908 /* The server is assigned */
909 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
910 si->state = SI_ST_ASS;
911}
912
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200913/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +0100914 * if appropriate. The default_backend rule is also considered, then the
915 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200916 * It returns 1 if the processing can continue on next analysers, or zero if it
917 * either needs more data or wants to immediately abort the request.
918 */
919int process_switching_rules(struct session *s, struct buffer *req, int an_bit)
920{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200921 struct persist_rule *prst_rule;
Willy Tarreau4de91492010-01-22 19:10:05 +0100922
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200923 req->analysers &= ~an_bit;
924 req->analyse_exp = TICK_ETERNITY;
925
926 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
927 now_ms, __FUNCTION__,
928 s,
929 req,
930 req->rex, req->wex,
931 req->flags,
932 req->l,
933 req->analysers);
934
935 /* now check whether we have some switching rules for this request */
936 if (!(s->flags & SN_BE_ASSIGNED)) {
937 struct switching_rule *rule;
938
939 list_for_each_entry(rule, &s->fe->switching_rules, list) {
940 int ret;
941
942 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ);
943 ret = acl_pass(ret);
944 if (rule->cond->pol == ACL_COND_UNLESS)
945 ret = !ret;
946
947 if (ret) {
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200948 if (!session_set_backend(s, rule->be.backend))
949 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200950 break;
951 }
952 }
953
954 /* To ensure correct connection accounting on the backend, we
955 * have to assign one if it was not set (eg: a listen). This
956 * measure also takes care of correctly setting the default
957 * backend if any.
958 */
959 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200960 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
961 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200962 }
963
Willy Tarreaufb356202010-08-03 14:02:05 +0200964 /* we don't want to run the TCP or HTTP filters again if the backend has not changed */
965 if (s->fe == s->be) {
966 s->req->analysers &= ~AN_REQ_INSPECT_BE;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200967 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +0200968 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200969
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200970 /* 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 +0100971 * persistence rule, and report that in the session.
972 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200973 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +0100974 int ret = 1;
975
976 if (prst_rule->cond) {
977 ret = acl_exec_cond(prst_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
978 ret = acl_pass(ret);
979 if (prst_rule->cond->pol == ACL_COND_UNLESS)
980 ret = !ret;
981 }
982
983 if (ret) {
984 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200985 if (prst_rule->type == PERSIST_TYPE_FORCE) {
986 s->flags |= SN_FORCE_PRST;
987 } else {
988 s->flags |= SN_IGNORE_PRST;
989 }
Willy Tarreau4de91492010-01-22 19:10:05 +0100990 break;
991 }
992 }
993
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200994 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200995
996 sw_failed:
997 /* immediately abort this request in case of allocation failure */
998 buffer_abort(s->req);
999 buffer_abort(s->rep);
1000
1001 if (!(s->flags & SN_ERR_MASK))
1002 s->flags |= SN_ERR_RESOURCE;
1003 if (!(s->flags & SN_FINST_MASK))
1004 s->flags |= SN_FINST_R;
1005
1006 s->txn.status = 500;
1007 s->req->analysers = 0;
1008 s->req->analyse_exp = TICK_ETERNITY;
1009 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001010}
1011
Emeric Brun1d33b292010-01-04 15:47:17 +01001012/* This stream analyser works on a request. It applies all sticking rules on
1013 * it then returns 1. The data must already be present in the buffer otherwise
1014 * they won't match. It always returns 1.
1015 */
1016int process_sticking_rules(struct session *s, struct buffer *req, int an_bit)
1017{
1018 struct proxy *px = s->be;
1019 struct sticking_rule *rule;
1020
1021 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1022 now_ms, __FUNCTION__,
1023 s,
1024 req,
1025 req->rex, req->wex,
1026 req->flags,
1027 req->l,
1028 req->analysers);
1029
1030 list_for_each_entry(rule, &px->sticking_rules, list) {
1031 int ret = 1 ;
1032 int i;
1033
1034 for (i = 0; i < s->store_count; i++) {
1035 if (rule->table.t == s->store[i].table)
1036 break;
1037 }
1038
1039 if (i != s->store_count)
1040 continue;
1041
1042 if (rule->cond) {
1043 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_REQ);
1044 ret = acl_pass(ret);
1045 if (rule->cond->pol == ACL_COND_UNLESS)
1046 ret = !ret;
1047 }
1048
1049 if (ret) {
1050 struct stktable_key *key;
1051
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001052 key = stktable_fetch_key(px, s, &s->txn, PATTERN_FETCH_REQ, rule->expr, rule->table.t->type);
Emeric Brun1d33b292010-01-04 15:47:17 +01001053 if (!key)
1054 continue;
1055
1056 if (rule->flags & STK_IS_MATCH) {
1057 struct stksess *ts;
1058
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001059 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001060 if (!(s->flags & SN_ASSIGNED)) {
1061 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001062 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001063
1064 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001065 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1066 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001067 if (node) {
1068 struct server *srv;
1069
1070 srv = container_of(node, struct server, conf.id);
Willy Tarreau4de91492010-01-22 19:10:05 +01001071 if ((srv->state & SRV_RUNNING) ||
1072 (px->options & PR_O_PERSIST) ||
1073 (s->flags & SN_FORCE_PRST)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001074 s->flags |= SN_DIRECT | SN_ASSIGNED;
1075 s->srv = srv;
1076 }
1077 }
1078 }
Willy Tarreaue8f63382010-07-13 15:20:24 +02001079 stktable_touch(rule->table.t, ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001080 }
1081 }
1082 if (rule->flags & STK_IS_STORE) {
1083 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1084 struct stksess *ts;
1085
1086 ts = stksess_new(rule->table.t, key);
1087 if (ts) {
1088 s->store[s->store_count].table = rule->table.t;
1089 s->store[s->store_count++].ts = ts;
1090 }
1091 }
1092 }
1093 }
1094 }
1095
1096 req->analysers &= ~an_bit;
1097 req->analyse_exp = TICK_ETERNITY;
1098 return 1;
1099}
1100
1101/* This stream analyser works on a response. It applies all store rules on it
1102 * then returns 1. The data must already be present in the buffer otherwise
1103 * they won't match. It always returns 1.
1104 */
1105int process_store_rules(struct session *s, struct buffer *rep, int an_bit)
1106{
1107 struct proxy *px = s->be;
1108 struct sticking_rule *rule;
1109 int i;
1110
1111 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1112 now_ms, __FUNCTION__,
1113 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001114 rep,
1115 rep->rex, rep->wex,
1116 rep->flags,
1117 rep->l,
1118 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001119
1120 list_for_each_entry(rule, &px->storersp_rules, list) {
1121 int ret = 1 ;
1122 int storereqidx = -1;
1123
1124 for (i = 0; i < s->store_count; i++) {
1125 if (rule->table.t == s->store[i].table) {
1126 if (!(s->store[i].flags))
1127 storereqidx = i;
1128 break;
1129 }
1130 }
1131
1132 if ((i != s->store_count) && (storereqidx == -1))
1133 continue;
1134
1135 if (rule->cond) {
1136 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_RTR);
1137 ret = acl_pass(ret);
1138 if (rule->cond->pol == ACL_COND_UNLESS)
1139 ret = !ret;
1140 }
1141
1142 if (ret) {
1143 struct stktable_key *key;
1144
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001145 key = stktable_fetch_key(px, s, &s->txn, PATTERN_FETCH_RTR, rule->expr, rule->table.t->type);
Emeric Brun1d33b292010-01-04 15:47:17 +01001146 if (!key)
1147 continue;
1148
1149 if (storereqidx != -1) {
Willy Tarreau393379c2010-06-06 12:11:37 +02001150 stksess_setkey(s->store[storereqidx].table, s->store[storereqidx].ts, key);
Emeric Brun1d33b292010-01-04 15:47:17 +01001151 s->store[storereqidx].flags = 1;
1152 }
1153 else if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1154 struct stksess *ts;
1155
1156 ts = stksess_new(rule->table.t, key);
1157 if (ts) {
1158 s->store[s->store_count].table = rule->table.t;
1159 s->store[s->store_count].flags = 1;
1160 s->store[s->store_count++].ts = ts;
1161 }
1162 }
1163 }
1164 }
1165
1166 /* process store request and store response */
1167 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001168 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001169 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001170
1171 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1172 if (ts) {
1173 /* the entry already existed, we can free ours */
Willy Tarreaue8f63382010-07-13 15:20:24 +02001174 stktable_touch(s->store[i].table, ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001175 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001176 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001177 else
1178 ts = stktable_store(s->store[i].table, s->store[i].ts);
1179
1180 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001181 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
1182 stktable_data_cast(ptr, server_id) = s->srv->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001183 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001184 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001185
1186 rep->analysers &= ~an_bit;
1187 rep->analyse_exp = TICK_ETERNITY;
1188 return 1;
1189}
1190
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001191/* This macro is very specific to the function below. See the comments in
1192 * process_session() below to understand the logic and the tests.
1193 */
1194#define UPDATE_ANALYSERS(real, list, back, flag) { \
1195 list = (((list) & ~(flag)) | ~(back)) & (real); \
1196 back = real; \
1197 if (!(list)) \
1198 break; \
1199 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1200 continue; \
1201}
1202
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001203/* Processes the client, server, request and response jobs of a session task,
1204 * then puts it back to the wait queue in a clean state, or cleans up its
1205 * resources if it must be deleted. Returns in <next> the date the task wants
1206 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1207 * nothing too many times, the request and response buffers flags are monitored
1208 * and each function is called only if at least another function has changed at
1209 * least one flag it is interested in.
1210 */
Willy Tarreau26c25062009-03-08 09:38:41 +01001211struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001212{
1213 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001214 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001215 unsigned int rq_prod_last, rq_cons_last;
1216 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001217 unsigned int req_ana_back;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001218
1219 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
1220 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
1221
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001222 /* this data may be no longer valid, clear it */
1223 memset(&s->txn.auth, 0, sizeof(s->txn.auth));
1224
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001225 /* This flag must explicitly be set every time */
1226 s->req->flags &= ~BF_READ_NOEXP;
1227
1228 /* Keep a copy of req/rep flags so that we can detect shutdowns */
1229 rqf_last = s->req->flags;
1230 rpf_last = s->rep->flags;
1231
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001232 /* we don't want the stream interface functions to recursively wake us up */
1233 if (s->req->prod->owner == t)
1234 s->req->prod->flags |= SI_FL_DONT_WAKE;
1235 if (s->req->cons->owner == t)
1236 s->req->cons->flags |= SI_FL_DONT_WAKE;
1237
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001238 /* 1a: Check for low level timeouts if needed. We just set a flag on
1239 * stream interfaces when their timeouts have expired.
1240 */
1241 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
1242 stream_int_check_timeouts(&s->si[0]);
1243 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001244
1245 /* check buffer timeouts, and close the corresponding stream interfaces
1246 * for future reads or writes. Note: this will also concern upper layers
1247 * but we do not touch any other flag. We must be careful and correctly
1248 * detect state changes when calling them.
1249 */
1250
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001251 buffer_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001252
Willy Tarreau14641402009-12-29 14:49:56 +01001253 if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1254 s->req->cons->flags |= SI_FL_NOLINGER;
1255 s->req->cons->shutw(s->req->cons);
1256 }
1257
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001258 if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1259 s->req->prod->shutr(s->req->prod);
1260
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001261 buffer_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001262
Willy Tarreau14641402009-12-29 14:49:56 +01001263 if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1264 s->rep->cons->flags |= SI_FL_NOLINGER;
1265 s->rep->cons->shutw(s->rep->cons);
1266 }
1267
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001268 if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1269 s->rep->prod->shutr(s->rep->prod);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001270 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001271
1272 /* 1b: check for low-level errors reported at the stream interface.
1273 * First we check if it's a retryable error (in which case we don't
1274 * want to tell the buffer). Otherwise we report the error one level
1275 * upper by setting flags into the buffers. Note that the side towards
1276 * the client cannot have connect (hence retryable) errors. Also, the
1277 * connection setup code must be able to deal with any type of abort.
1278 */
1279 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
1280 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
1281 s->si[0].shutr(&s->si[0]);
1282 s->si[0].shutw(&s->si[0]);
1283 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001284 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreauae526782010-03-04 20:34:23 +01001285 s->be->counters.cli_aborts++;
1286 if (s->srv)
1287 s->srv->counters.cli_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001288 if (!(s->flags & SN_ERR_MASK))
1289 s->flags |= SN_ERR_CLICL;
1290 if (!(s->flags & SN_FINST_MASK))
1291 s->flags |= SN_FINST_D;
1292 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001293 }
1294 }
1295
1296 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
1297 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
1298 s->si[1].shutr(&s->si[1]);
1299 s->si[1].shutw(&s->si[1]);
1300 stream_int_report_error(&s->si[1]);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001301 s->be->counters.failed_resp++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001302 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001303 s->srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001304 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreauae526782010-03-04 20:34:23 +01001305 s->be->counters.srv_aborts++;
1306 if (s->srv)
1307 s->srv->counters.srv_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001308 if (!(s->flags & SN_ERR_MASK))
1309 s->flags |= SN_ERR_SRVCL;
1310 if (!(s->flags & SN_FINST_MASK))
1311 s->flags |= SN_FINST_D;
1312 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001313 }
1314 /* note: maybe we should process connection errors here ? */
1315 }
1316
1317 if (s->si[1].state == SI_ST_CON) {
1318 /* we were trying to establish a connection on the server side,
1319 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1320 */
1321 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
1322 sess_update_st_cer(s, &s->si[1]);
1323 else if (s->si[1].state == SI_ST_EST)
1324 sess_establish(s, &s->si[1]);
1325
1326 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1327 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1328 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1329 */
1330 }
1331
Willy Tarreau815a9b22010-07-27 17:15:12 +02001332 rq_prod_last = s->si[0].state;
1333 rq_cons_last = s->si[1].state;
1334 rp_cons_last = s->si[0].state;
1335 rp_prod_last = s->si[1].state;
1336
1337 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001338 /* Check for connection closure */
1339
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001340 DPRINTF(stderr,
1341 "[%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",
1342 now_ms, __FUNCTION__, __LINE__,
1343 t,
1344 s, s->flags,
1345 s->req, s->rep,
1346 s->req->rex, s->rep->wex,
1347 s->req->flags, s->rep->flags,
1348 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
1349 s->rep->cons->err_type, s->req->cons->err_type,
Willy Tarreauee28de02010-06-01 09:51:00 +02001350 s->req->cons->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001351
1352 /* nothing special to be done on client side */
1353 if (unlikely(s->req->prod->state == SI_ST_DIS))
1354 s->req->prod->state = SI_ST_CLO;
1355
1356 /* When a server-side connection is released, we have to count it and
1357 * check for pending connections on this server.
1358 */
1359 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
1360 s->req->cons->state = SI_ST_CLO;
1361 if (s->srv) {
1362 if (s->flags & SN_CURR_SESS) {
1363 s->flags &= ~SN_CURR_SESS;
1364 s->srv->cur_sess--;
1365 }
1366 sess_change_server(s, NULL);
1367 if (may_dequeue_tasks(s->srv, s->be))
1368 process_srv_queue(s->srv);
1369 }
1370 }
1371
1372 /*
1373 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1374 * at this point.
1375 */
1376
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001377 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001378 /* Analyse request */
1379 if ((s->req->flags & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001380 ((s->req->flags ^ rqf_last) & BF_MASK_STATIC) ||
1381 s->si[0].state != rq_prod_last ||
1382 s->si[1].state != rq_cons_last) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001383 unsigned int flags = s->req->flags;
1384
1385 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001386 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001387 unsigned int ana_list;
1388 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001389
Willy Tarreau90deb182010-01-07 00:20:41 +01001390 /* it's up to the analysers to stop new connections,
1391 * disable reading or closing. Note: if an analyser
1392 * disables any of these bits, it is responsible for
1393 * enabling them again when it disables itself, so
1394 * that other analysers are called in similar conditions.
1395 */
1396 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001397 buffer_auto_connect(s->req);
1398 buffer_auto_close(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001399
1400 /* We will call all analysers for which a bit is set in
1401 * s->req->analysers, following the bit order from LSB
1402 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001403 * the list when not needed. Any analyser may return 0
1404 * to break out of the loop, either because of missing
1405 * data to take a decision, or because it decides to
1406 * kill the session. We loop at least once through each
1407 * analyser, and we may loop again if other analysers
1408 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001409 *
1410 * We build a list of analysers to run. We evaluate all
1411 * of these analysers in the order of the lower bit to
1412 * the higher bit. This ordering is very important.
1413 * An analyser will often add/remove other analysers,
1414 * including itself. Any changes to itself have no effect
1415 * on the loop. If it removes any other analysers, we
1416 * want those analysers not to be called anymore during
1417 * this loop. If it adds an analyser that is located
1418 * after itself, we want it to be scheduled for being
1419 * processed during the loop. If it adds an analyser
1420 * which is located before it, we want it to switch to
1421 * it immediately, even if it has already been called
1422 * once but removed since.
1423 *
1424 * In order to achieve this, we compare the analyser
1425 * list after the call with a copy of it before the
1426 * call. The work list is fed with analyser bits that
1427 * appeared during the call. Then we compare previous
1428 * work list with the new one, and check the bits that
1429 * appeared. If the lowest of these bits is lower than
1430 * the current bit, it means we have enabled a previous
1431 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001432 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001433
1434 ana_list = ana_back = s->req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001435 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001436 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001437
Willy Tarreaufb356202010-08-03 14:02:05 +02001438 if (ana_list & AN_REQ_INSPECT_FE) {
1439 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001440 break;
Willy Tarreaufb356202010-08-03 14:02:05 +02001441 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001442 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001443
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001444 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001445 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001446 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001447 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001448 }
1449
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001450 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001451 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
1452 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001453 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001454 }
1455
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001456 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001457 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
1458 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001459 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001460 }
1461
Willy Tarreaufb356202010-08-03 14:02:05 +02001462 if (ana_list & AN_REQ_INSPECT_BE) {
1463 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_BE))
1464 break;
1465 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
1466 }
1467
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001468 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001469 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
1470 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001471 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001472 }
1473
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001474 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001475 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001476 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001477 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001478 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001479
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001480 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreauc465fd72009-08-31 00:17:18 +02001481 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
1482 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001483 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001484 }
1485
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001486 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001487 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001488 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001489 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001490 }
Emeric Brun647caf12009-06-30 17:57:00 +02001491
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001492 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Emeric Brun647caf12009-06-30 17:57:00 +02001493 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
1494 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001495 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001496 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001497
Emeric Brun1d33b292010-01-04 15:47:17 +01001498 if (ana_list & AN_REQ_STICKING_RULES) {
1499 if (!process_sticking_rules(s, s->req, AN_REQ_STICKING_RULES))
1500 break;
1501 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
1502 }
1503
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001504 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001505 if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY))
1506 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001507 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001508 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001509 break;
1510 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001511 }
Willy Tarreau84455332009-03-15 22:34:05 +01001512
Willy Tarreau815a9b22010-07-27 17:15:12 +02001513 rq_prod_last = s->si[0].state;
1514 rq_cons_last = s->si[1].state;
1515 rqf_last = s->req->flags;
1516
1517 if ((s->req->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001518 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001519 }
1520
Willy Tarreau576507f2010-01-07 00:09:04 +01001521 /* we'll monitor the request analysers while parsing the response,
1522 * because some response analysers may indirectly enable new request
1523 * analysers (eg: HTTP keep-alive).
1524 */
1525 req_ana_back = s->req->analysers;
1526
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001527 resync_response:
1528 /* Analyse response */
1529
1530 if (unlikely(s->rep->flags & BF_HIJACK)) {
1531 /* In inject mode, we wake up everytime something has
1532 * happened on the write side of the buffer.
1533 */
1534 unsigned int flags = s->rep->flags;
1535
1536 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
1537 !(s->rep->flags & BF_FULL)) {
1538 s->rep->hijacker(s, s->rep);
1539 }
1540
1541 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
1542 rpf_last = s->rep->flags;
1543 goto resync_response;
1544 }
1545 }
1546 else if ((s->rep->flags & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001547 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC ||
1548 s->si[0].state != rp_cons_last ||
1549 s->si[1].state != rp_prod_last) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001550 unsigned int flags = s->rep->flags;
1551
1552 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001553 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001554 unsigned int ana_list;
1555 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001556
Willy Tarreau90deb182010-01-07 00:20:41 +01001557 /* it's up to the analysers to stop disable reading or
1558 * closing. Note: if an analyser disables any of these
1559 * bits, it is responsible for enabling them again when
1560 * it disables itself, so that other analysers are called
1561 * in similar conditions.
1562 */
1563 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001564 buffer_auto_close(s->rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001565
1566 /* We will call all analysers for which a bit is set in
1567 * s->rep->analysers, following the bit order from LSB
1568 * to MSB. The analysers must remove themselves from
1569 * the list when not needed. Any analyser may return 0
1570 * to break out of the loop, either because of missing
1571 * data to take a decision, or because it decides to
1572 * kill the session. We loop at least once through each
1573 * analyser, and we may loop again if other analysers
1574 * are added in the middle.
1575 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001576
1577 ana_list = ana_back = s->rep->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001578 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001579 /* Warning! ensure that analysers are always placed in ascending order! */
1580
1581 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001582 if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP))
1583 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001584 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001585 }
1586
Emeric Brun1d33b292010-01-04 15:47:17 +01001587 if (ana_list & AN_RES_STORE_RULES) {
1588 if (!process_store_rules(s, s->rep, AN_RES_STORE_RULES))
1589 break;
1590 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
1591 }
1592
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001593 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001594 if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be))
1595 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001596 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001597 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001598
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001599 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001600 if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY))
1601 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001602 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001603 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001604 break;
1605 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001606 }
1607
Willy Tarreau815a9b22010-07-27 17:15:12 +02001608 rp_cons_last = s->si[0].state;
1609 rp_prod_last = s->si[1].state;
1610 rpf_last = s->rep->flags;
1611
1612 if ((s->rep->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001613 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001614 }
1615
Willy Tarreau576507f2010-01-07 00:09:04 +01001616 /* maybe someone has added some request analysers, so we must check and loop */
1617 if (s->req->analysers & ~req_ana_back)
1618 goto resync_request;
1619
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001620 /* FIXME: here we should call protocol handlers which rely on
1621 * both buffers.
1622 */
1623
1624
1625 /*
Willy Tarreauae526782010-03-04 20:34:23 +01001626 * Now we propagate unhandled errors to the session. Normally
1627 * we're just in a data phase here since it means we have not
1628 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001629 */
1630 if (!(s->flags & SN_ERR_MASK)) {
1631 if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1632 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +01001633 s->req->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001634 if (s->req->flags & BF_READ_ERROR) {
1635 s->be->counters.cli_aborts++;
1636 if (s->srv)
1637 s->srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001638 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001639 }
1640 else if (s->req->flags & BF_READ_TIMEOUT) {
1641 s->be->counters.cli_aborts++;
1642 if (s->srv)
1643 s->srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001644 s->flags |= SN_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001645 }
1646 else if (s->req->flags & BF_WRITE_ERROR) {
1647 s->be->counters.srv_aborts++;
1648 if (s->srv)
1649 s->srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001650 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001651 }
1652 else {
1653 s->be->counters.srv_aborts++;
1654 if (s->srv)
1655 s->srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001656 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001657 }
Willy Tarreau84455332009-03-15 22:34:05 +01001658 sess_set_term_flags(s);
1659 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001660 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1661 /* Report it if the server got an error or a read timeout expired */
1662 s->rep->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001663 if (s->rep->flags & BF_READ_ERROR) {
1664 s->be->counters.srv_aborts++;
1665 if (s->srv)
1666 s->srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001667 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001668 }
1669 else if (s->rep->flags & BF_READ_TIMEOUT) {
1670 s->be->counters.srv_aborts++;
1671 if (s->srv)
1672 s->srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001673 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001674 }
1675 else if (s->rep->flags & BF_WRITE_ERROR) {
1676 s->be->counters.cli_aborts++;
1677 if (s->srv)
1678 s->srv->counters.cli_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001679 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001680 }
1681 else {
1682 s->be->counters.cli_aborts++;
1683 if (s->srv)
1684 s->srv->counters.cli_aborts++;
1685 s->flags |= SN_ERR_CLITO;
1686 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001687 sess_set_term_flags(s);
1688 }
Willy Tarreau84455332009-03-15 22:34:05 +01001689 }
1690
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001691 /*
1692 * Here we take care of forwarding unhandled data. This also includes
1693 * connection establishments and shutdown requests.
1694 */
1695
1696
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001697 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001698 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001699 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001700 if (!s->req->analysers &&
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001701 !(s->req->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTW_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001702 (s->req->prod->state >= SI_ST_EST) &&
1703 (s->req->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001704 /* This buffer is freewheeling, there's no analyser nor hijacker
1705 * attached to it. If any data are left in, we'll permit them to
1706 * move.
1707 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001708 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001709 buffer_auto_connect(s->req);
1710 buffer_auto_close(s->req);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001711 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001712
Willy Tarreau31971e52009-09-20 12:07:52 +02001713 /* If the producer is still connected, we'll enable data to flow
1714 * from the producer to the consumer (which might possibly not be
1715 * connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001716 */
Willy Tarreau31971e52009-09-20 12:07:52 +02001717 if (!(s->req->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW)))
1718 buffer_forward(s->req, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001719 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001720
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001721 /* check if it is wise to enable kernel splicing to forward request data */
1722 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1723 s->req->to_forward &&
1724 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001725 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001726 (pipes_used < global.maxpipes) &&
1727 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
1728 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1729 (s->req->flags & BF_STREAMER_FAST)))) {
1730 s->req->flags |= BF_KERN_SPLICING;
1731 }
1732
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001733 /* reflect what the L7 analysers have seen last */
1734 rqf_last = s->req->flags;
1735
1736 /*
1737 * Now forward all shutdown requests between both sides of the buffer
1738 */
1739
Willy Tarreau520d95e2009-09-19 21:04:57 +02001740 /* first, let's check if the request buffer needs to shutdown(write), which may
1741 * happen either because the input is closed or because we want to force a close
Willy Tarreaue4599762010-03-21 23:25:09 +01001742 * once the server has begun to respond.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001743 */
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001744 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
Willy Tarreaue4599762010-03-21 23:25:09 +01001745 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001746 buffer_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001747
1748 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001749 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 +01001750 s->req->cons->shutw(s->req->cons);
1751
1752 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001753 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
1754 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001755 buffer_shutr_now(s->req);
1756
1757 /* shutdown(read) pending */
1758 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1759 s->req->prod->shutr(s->req->prod);
1760
Willy Tarreau520d95e2009-09-19 21:04:57 +02001761 /* it's possible that an upper layer has requested a connection setup or abort.
1762 * There are 2 situations where we decide to establish a new connection :
1763 * - there are data scheduled for emission in the buffer
1764 * - the BF_AUTO_CONNECT flag is set (active connection)
1765 */
1766 if (s->req->cons->state == SI_ST_INI) {
Willy Tarreaue4599762010-03-21 23:25:09 +01001767 if (!(s->req->flags & BF_SHUTW)) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001768 if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) {
Willy Tarreau85e7d002010-05-31 11:57:51 +02001769 /* If we have an iohandler without a connect method, we immediately
1770 * switch to the connected state, otherwise we perform a connection
1771 * request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001772 */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001773 s->req->cons->state = SI_ST_REQ; /* new connection requested */
Willy Tarreau070ceb62010-06-01 10:36:43 +02001774 s->req->cons->conn_retries = s->be->conn_retries;
Willy Tarreau85e7d002010-05-31 11:57:51 +02001775 if (unlikely(s->req->cons->iohandler && !s->req->cons->connect)) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02001776 s->req->cons->state = SI_ST_EST; /* connection established */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001777 s->rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
1778 s->req->wex = TICK_ETERNITY;
1779 }
Willy Tarreau520d95e2009-09-19 21:04:57 +02001780 }
Willy Tarreau73201222009-08-16 18:27:24 +02001781 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001782 else {
Willy Tarreau92795622009-03-06 12:51:23 +01001783 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001784 buffer_shutw_now(s->req); /* fix buffer flags upon abort */
1785 buffer_shutr_now(s->rep);
1786 }
Willy Tarreau92795622009-03-06 12:51:23 +01001787 }
1788
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001789
1790 /* we may have a pending connection request, or a connection waiting
1791 * for completion.
1792 */
1793 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
1794 do {
1795 /* nb: step 1 might switch from QUE to ASS, but we first want
1796 * to give a chance to step 2 to perform a redirect if needed.
1797 */
1798 if (s->si[1].state != SI_ST_REQ)
1799 sess_update_stream_int(s, &s->si[1]);
1800 if (s->si[1].state == SI_ST_REQ)
1801 sess_prepare_conn_req(s, &s->si[1]);
1802
1803 if (s->si[1].state == SI_ST_ASS && s->srv &&
1804 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
1805 perform_http_redirect(s, &s->si[1]);
1806 } while (s->si[1].state == SI_ST_ASS);
1807 }
1808
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001809 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001810 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001811 goto resync_stream_interface;
1812
Willy Tarreau815a9b22010-07-27 17:15:12 +02001813 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001814 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001815 goto resync_request;
1816
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001817 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01001818
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001819 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001820 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001821 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001822 if (!s->rep->analysers &&
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001823 !(s->rep->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTW_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001824 (s->rep->prod->state >= SI_ST_EST) &&
1825 (s->rep->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001826 /* This buffer is freewheeling, there's no analyser nor hijacker
1827 * attached to it. If any data are left in, we'll permit them to
1828 * move.
1829 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001830 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001831 buffer_auto_close(s->rep);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001832 buffer_flush(s->rep);
Willy Tarreau31971e52009-09-20 12:07:52 +02001833 if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW)))
1834 buffer_forward(s->rep, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001835 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001836
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001837 /* check if it is wise to enable kernel splicing to forward response data */
1838 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1839 s->rep->to_forward &&
1840 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001841 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001842 (pipes_used < global.maxpipes) &&
1843 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
1844 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1845 (s->rep->flags & BF_STREAMER_FAST)))) {
1846 s->rep->flags |= BF_KERN_SPLICING;
1847 }
1848
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001849 /* reflect what the L7 analysers have seen last */
1850 rpf_last = s->rep->flags;
1851
1852 /*
1853 * Now forward all shutdown requests between both sides of the buffer
1854 */
1855
1856 /*
1857 * FIXME: this is probably where we should produce error responses.
1858 */
1859
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001860 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001861 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
1862 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001863 buffer_shutw_now(s->rep);
1864
1865 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001866 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 +01001867 s->rep->cons->shutw(s->rep->cons);
1868
1869 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001870 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
1871 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001872 buffer_shutr_now(s->rep);
1873
1874 /* shutdown(read) pending */
1875 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1876 s->rep->prod->shutr(s->rep->prod);
1877
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001878 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001879 goto resync_stream_interface;
1880
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001881 if (s->req->flags != rqf_last)
1882 goto resync_request;
1883
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001884 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001885 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001886
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001887 /* we're interested in getting wakeups again */
1888 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
1889 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
1890
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001891 /* This is needed only when debugging is enabled, to indicate
1892 * client-side or server-side close. Please note that in the unlikely
1893 * event where both sides would close at once, the sequence is reported
1894 * on the server side first.
1895 */
1896 if (unlikely((global.mode & MODE_DEBUG) &&
1897 (!(global.mode & MODE_QUIET) ||
1898 (global.mode & MODE_VERBOSE)))) {
1899 int len;
1900
1901 if (s->si[1].state == SI_ST_CLO &&
1902 s->si[1].prev_state == SI_ST_EST) {
1903 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1904 s->uniq_id, s->be->id,
1905 (unsigned short)s->si[0].fd,
1906 (unsigned short)s->si[1].fd);
1907 write(1, trash, len);
1908 }
1909
1910 if (s->si[0].state == SI_ST_CLO &&
1911 s->si[0].prev_state == SI_ST_EST) {
1912 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1913 s->uniq_id, s->be->id,
1914 (unsigned short)s->si[0].fd,
1915 (unsigned short)s->si[1].fd);
1916 write(1, trash, len);
1917 }
1918 }
1919
1920 if (likely((s->rep->cons->state != SI_ST_CLO) ||
1921 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
1922
1923 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1924 session_process_counters(s);
1925
Willy Tarreau1accfc02009-09-05 20:57:35 +02001926 if (s->rep->cons->state == SI_ST_EST && !s->rep->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001927 s->rep->cons->update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001928
Willy Tarreau1accfc02009-09-05 20:57:35 +02001929 if (s->req->cons->state == SI_ST_EST && !s->req->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001930 s->req->cons->update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001931
Willy Tarreaua6eebb32010-06-04 11:40:20 +02001932 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL|BF_READ_ATTACHED);
1933 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 +01001934 s->si[0].prev_state = s->si[0].state;
1935 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01001936 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
1937 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001938
1939 /* Trick: if a request is being waiting for the server to respond,
1940 * and if we know the server can timeout, we don't want the timeout
1941 * to expire on the client side first, but we're still interested
1942 * in passing data from the client to the server (eg: POST). Thus,
1943 * we can cancel the client's request timeout if the server's
1944 * request timeout is set and the server has not yet sent a response.
1945 */
1946
Willy Tarreau520d95e2009-09-19 21:04:57 +02001947 if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01001948 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
1949 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001950 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01001951 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001952
Willy Tarreau7a20aa62010-07-13 16:30:45 +02001953 /* Call the stream interfaces' I/O handlers when embedded.
Willy Tarreau1accfc02009-09-05 20:57:35 +02001954 * Note that this one may wake the task up again.
1955 */
Willy Tarreau7a20aa62010-07-13 16:30:45 +02001956 if (s->req->cons->iohandler || s->rep->cons->iohandler) {
1957 if (s->req->cons->iohandler)
1958 s->req->cons->iohandler(s->req->cons);
1959 if (s->rep->cons->iohandler)
1960 s->rep->cons->iohandler(s->rep->cons);
Willy Tarreau1accfc02009-09-05 20:57:35 +02001961 if (task_in_rq(t)) {
1962 /* If we woke up, we don't want to requeue the
1963 * task to the wait queue, but rather requeue
1964 * it into the runqueue ASAP.
1965 */
1966 t->expire = TICK_ETERNITY;
1967 return t;
1968 }
1969 }
1970
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001971 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
1972 tick_first(s->rep->rex, s->rep->wex));
1973 if (s->req->analysers)
1974 t->expire = tick_first(t->expire, s->req->analyse_exp);
1975
1976 if (s->si[0].exp)
1977 t->expire = tick_first(t->expire, s->si[0].exp);
1978
1979 if (s->si[1].exp)
1980 t->expire = tick_first(t->expire, s->si[1].exp);
1981
1982#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01001983 fprintf(stderr,
1984 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
1985 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
1986 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
1987 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 +01001988#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001989
1990#ifdef DEBUG_DEV
1991 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01001992 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001993 ABORT_NOW();
1994#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01001995 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001996 }
1997
1998 s->fe->feconn--;
1999 if (s->flags & SN_BE_ASSIGNED)
2000 s->be->beconn--;
2001 actconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002002 jobs--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02002003 s->listener->nbconn--;
2004 if (s->listener->state == LI_FULL &&
2005 s->listener->nbconn < s->listener->maxconn) {
2006 /* we should reactivate the listener */
2007 EV_FD_SET(s->listener->fd, DIR_RD);
2008 s->listener->state = LI_READY;
2009 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002010
2011 if (unlikely((global.mode & MODE_DEBUG) &&
2012 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
2013 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002014 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002015 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002016 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002017 write(1, trash, len);
2018 }
2019
2020 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
2021 session_process_counters(s);
2022
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002023 if (s->txn.status) {
2024 int n;
2025
2026 n = s->txn.status / 100;
2027 if (n < 1 || n > 5)
2028 n = 0;
2029
2030 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau24657792010-02-26 10:30:28 +01002031 s->fe->counters.fe.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002032
Willy Tarreau24657792010-02-26 10:30:28 +01002033 if ((s->flags & SN_BE_ASSIGNED) &&
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002034 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau24657792010-02-26 10:30:28 +01002035 s->be->counters.be.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002036 }
2037
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002038 /* let's do a final log if we need it */
2039 if (s->logs.logwait &&
2040 !(s->flags & SN_MONITOR) &&
2041 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002042 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002043 }
2044
2045 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002046 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002047 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002048 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002049 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002050}
2051
Willy Tarreau7c669d72008-06-20 15:04:11 +02002052/*
2053 * This function adjusts sess->srv_conn and maintains the previous and new
2054 * server's served session counts. Setting newsrv to NULL is enough to release
2055 * current connection slot. This function also notifies any LB algo which might
2056 * expect to be informed about any change in the number of active sessions on a
2057 * server.
2058 */
2059void sess_change_server(struct session *sess, struct server *newsrv)
2060{
2061 if (sess->srv_conn == newsrv)
2062 return;
2063
2064 if (sess->srv_conn) {
2065 sess->srv_conn->served--;
2066 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2067 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
2068 sess->srv_conn = NULL;
2069 }
2070
2071 if (newsrv) {
2072 newsrv->served++;
2073 if (newsrv->proxy->lbprm.server_take_conn)
2074 newsrv->proxy->lbprm.server_take_conn(newsrv);
2075 sess->srv_conn = newsrv;
2076 }
2077}
2078
Willy Tarreau84455332009-03-15 22:34:05 +01002079/* Set correct session termination flags in case no analyser has done it. It
2080 * also counts a failed request if the server state has not reached the request
2081 * stage.
2082 */
2083void sess_set_term_flags(struct session *s)
2084{
2085 if (!(s->flags & SN_FINST_MASK)) {
2086 if (s->si[1].state < SI_ST_REQ) {
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002087
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002088 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002089 if (s->listener->counters)
2090 s->listener->counters->failed_req++;
2091
Willy Tarreau84455332009-03-15 22:34:05 +01002092 s->flags |= SN_FINST_R;
2093 }
2094 else if (s->si[1].state == SI_ST_QUE)
2095 s->flags |= SN_FINST_Q;
2096 else if (s->si[1].state < SI_ST_EST)
2097 s->flags |= SN_FINST_C;
Willy Tarreau033b2db2010-03-04 17:54:21 +01002098 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreau84455332009-03-15 22:34:05 +01002099 s->flags |= SN_FINST_D;
2100 else
2101 s->flags |= SN_FINST_L;
2102 }
2103}
2104
2105/* Handle server-side errors for default protocols. It is called whenever a a
2106 * connection setup is aborted or a request is aborted in queue. It sets the
2107 * session termination flags so that the caller does not have to worry about
2108 * them. It's installed as ->srv_error for the server-side stream_interface.
2109 */
2110void default_srv_error(struct session *s, struct stream_interface *si)
2111{
2112 int err_type = si->err_type;
2113 int err = 0, fin = 0;
2114
2115 if (err_type & SI_ET_QUEUE_ABRT) {
2116 err = SN_ERR_CLICL;
2117 fin = SN_FINST_Q;
2118 }
2119 else if (err_type & SI_ET_CONN_ABRT) {
2120 err = SN_ERR_CLICL;
2121 fin = SN_FINST_C;
2122 }
2123 else if (err_type & SI_ET_QUEUE_TO) {
2124 err = SN_ERR_SRVTO;
2125 fin = SN_FINST_Q;
2126 }
2127 else if (err_type & SI_ET_QUEUE_ERR) {
2128 err = SN_ERR_SRVCL;
2129 fin = SN_FINST_Q;
2130 }
2131 else if (err_type & SI_ET_CONN_TO) {
2132 err = SN_ERR_SRVTO;
2133 fin = SN_FINST_C;
2134 }
2135 else if (err_type & SI_ET_CONN_ERR) {
2136 err = SN_ERR_SRVCL;
2137 fin = SN_FINST_C;
2138 }
2139 else /* SI_ET_CONN_OTHER and others */ {
2140 err = SN_ERR_INTERNAL;
2141 fin = SN_FINST_C;
2142 }
2143
2144 if (!(s->flags & SN_ERR_MASK))
2145 s->flags |= err;
2146 if (!(s->flags & SN_FINST_MASK))
2147 s->flags |= fin;
2148}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002149
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002150
Willy Tarreau8b22a712010-06-18 17:46:06 +02002151/************************************************************************/
2152/* All supported ACL keywords must be declared here. */
2153/************************************************************************/
2154
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002155/* set test->i to the General Purpose Counter 0 value in the stksess entry <ts> */
2156static int
2157acl_fetch_get_gpc0(struct stktable *table, struct acl_test *test, struct stksess *ts)
2158{
2159 test->flags = ACL_TEST_F_VOL_TEST;
2160 test->i = 0;
2161 if (ts != NULL) {
2162 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2163 if (!ptr)
2164 return 0; /* parameter not stored */
2165 test->i = stktable_data_cast(ptr, gpc0);
2166 }
2167 return 1;
2168}
2169
2170/* set test->i to the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002171 * frontend counters.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002172 */
2173static int
Willy Tarreau56123282010-08-06 19:06:56 +02002174acl_fetch_sc1_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2175 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002176{
Willy Tarreau56123282010-08-06 19:06:56 +02002177 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002178 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002179 return acl_fetch_get_gpc0(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002180}
2181
2182/* set test->i to the General Purpose Counter 0 value from the session's tracked
2183 * backend counters.
2184 */
2185static int
Willy Tarreau56123282010-08-06 19:06:56 +02002186acl_fetch_sc2_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2187 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002188{
Willy Tarreau56123282010-08-06 19:06:56 +02002189 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002190 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002191 return acl_fetch_get_gpc0(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002192}
2193
2194/* set test->i to the General Purpose Counter 0 value from the session's source
2195 * address in the table pointed to by expr.
2196 */
2197static int
2198acl_fetch_src_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2199 struct acl_expr *expr, struct acl_test *test)
2200{
2201 struct stktable_key *key;
2202
2203 key = tcpv4_src_to_stktable_key(l4);
2204 if (!key)
2205 return 0; /* only TCPv4 is supported right now */
2206
2207 if (expr->arg_len)
2208 px = find_stktable(expr->arg.str);
2209
2210 if (!px)
2211 return 0; /* table not found */
2212
2213 return acl_fetch_get_gpc0(&px->table, test, stktable_lookup_key(&px->table, key));
2214}
2215
2216/* Increment the General Purpose Counter 0 value in the stksess entry <ts> and
2217 * return it into test->i.
2218 */
2219static int
2220acl_fetch_inc_gpc0(struct stktable *table, struct acl_test *test, struct stksess *ts)
2221{
2222 test->flags = ACL_TEST_F_VOL_TEST;
2223 test->i = 0;
2224 if (ts != NULL) {
2225 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2226 if (!ptr)
2227 return 0; /* parameter not stored */
2228 test->i = ++stktable_data_cast(ptr, gpc0);
2229 }
2230 return 1;
2231}
2232
2233/* Increment the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002234 * frontend counters and return it into test->i.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002235 */
2236static int
Willy Tarreau56123282010-08-06 19:06:56 +02002237acl_fetch_sc1_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2238 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002239{
Willy Tarreau56123282010-08-06 19:06:56 +02002240 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002241 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002242 return acl_fetch_inc_gpc0(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002243}
2244
2245/* Increment the General Purpose Counter 0 value from the session's tracked
2246 * backend counters and return it into test->i.
2247 */
2248static int
Willy Tarreau56123282010-08-06 19:06:56 +02002249acl_fetch_sc2_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2250 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002251{
Willy Tarreau56123282010-08-06 19:06:56 +02002252 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002253 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002254 return acl_fetch_inc_gpc0(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002255}
2256
2257/* Increment the General Purpose Counter 0 value from the session's source
2258 * address in the table pointed to by expr, and return it into test->i.
2259 */
2260static int
2261acl_fetch_src_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2262 struct acl_expr *expr, struct acl_test *test)
2263{
2264 struct stktable_key *key;
2265
2266 key = tcpv4_src_to_stktable_key(l4);
2267 if (!key)
2268 return 0; /* only TCPv4 is supported right now */
2269
2270 if (expr->arg_len)
2271 px = find_stktable(expr->arg.str);
2272
2273 if (!px)
2274 return 0; /* table not found */
2275
2276 return acl_fetch_inc_gpc0(&px->table, test, stktable_update_key(&px->table, key));
2277}
2278
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002279/* set test->i to the cumulated number of connections in the stksess entry <ts> */
2280static int
2281acl_fetch_conn_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2282{
2283 test->flags = ACL_TEST_F_VOL_TEST;
2284 test->i = 0;
2285 if (ts != NULL) {
2286 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CNT);
2287 if (!ptr)
2288 return 0; /* parameter not stored */
2289 test->i = stktable_data_cast(ptr, conn_cnt);
2290 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002291 return 1;
2292}
2293
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002294/* set test->i to the cumulated number of connections from the session's tracked FE counters */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002295static int
Willy Tarreau56123282010-08-06 19:06:56 +02002296acl_fetch_sc1_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2297 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002298{
Willy Tarreau56123282010-08-06 19:06:56 +02002299 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002300 return 0;
2301
Willy Tarreau56123282010-08-06 19:06:56 +02002302 return acl_fetch_conn_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002303}
2304
2305/* set test->i to the cumulated number of connections from the session's tracked BE counters */
2306static int
Willy Tarreau56123282010-08-06 19:06:56 +02002307acl_fetch_sc2_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2308 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002309{
Willy Tarreau56123282010-08-06 19:06:56 +02002310 if (!l4->stkctr2_entry)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002311 return 0;
2312
Willy Tarreau56123282010-08-06 19:06:56 +02002313 return acl_fetch_conn_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002314}
2315
2316/* set test->i to the cumulated number of connections from the session's source
2317 * address in the table pointed to by expr.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002318 */
2319static int
2320acl_fetch_src_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2321 struct acl_expr *expr, struct acl_test *test)
2322{
Willy Tarreau8b22a712010-06-18 17:46:06 +02002323 struct stktable_key *key;
2324
2325 key = tcpv4_src_to_stktable_key(l4);
2326 if (!key)
2327 return 0; /* only TCPv4 is supported right now */
2328
2329 if (expr->arg_len)
2330 px = find_stktable(expr->arg.str);
2331
2332 if (!px)
2333 return 0; /* table not found */
2334
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002335 return acl_fetch_conn_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau8b22a712010-06-18 17:46:06 +02002336}
2337
Willy Tarreau91c43d72010-06-20 11:19:22 +02002338/* set test->i to the connection rate in the stksess entry <ts> over the configured period */
2339static int
2340acl_fetch_conn_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2341{
2342 test->flags = ACL_TEST_F_VOL_TEST;
2343 test->i = 0;
2344 if (ts != NULL) {
2345 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_RATE);
2346 if (!ptr)
2347 return 0; /* parameter not stored */
2348 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
2349 table->data_arg[STKTABLE_DT_CONN_RATE].u);
2350 }
2351 return 1;
2352}
2353
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002354/* set test->i to the connection rate from the session's tracked FE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002355 * the configured period.
2356 */
2357static int
Willy Tarreau56123282010-08-06 19:06:56 +02002358acl_fetch_sc1_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2359 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002360{
Willy Tarreau56123282010-08-06 19:06:56 +02002361 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002362 return 0;
2363
Willy Tarreau56123282010-08-06 19:06:56 +02002364 return acl_fetch_conn_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002365}
2366
2367/* set test->i to the connection rate from the session's tracked BE counters over
2368 * the configured period.
2369 */
2370static int
Willy Tarreau56123282010-08-06 19:06:56 +02002371acl_fetch_sc2_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2372 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002373{
Willy Tarreau56123282010-08-06 19:06:56 +02002374 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002375 return 0;
2376
Willy Tarreau56123282010-08-06 19:06:56 +02002377 return acl_fetch_conn_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002378}
2379
2380/* set test->i to the connection rate from the session's source address in the
2381 * table pointed to by expr, over the configured period.
2382 */
2383static int
2384acl_fetch_src_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2385 struct acl_expr *expr, struct acl_test *test)
2386{
2387 struct stktable_key *key;
2388
2389 key = tcpv4_src_to_stktable_key(l4);
2390 if (!key)
2391 return 0; /* only TCPv4 is supported right now */
2392
2393 if (expr->arg_len)
2394 px = find_stktable(expr->arg.str);
2395
2396 if (!px)
2397 return 0; /* table not found */
2398
2399 return acl_fetch_conn_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2400}
2401
Willy Tarreau8b22a712010-06-18 17:46:06 +02002402/* set test->i to the number of connections from the session's source address
2403 * in the table pointed to by expr, after updating it.
2404 */
2405static int
2406acl_fetch_src_updt_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2407 struct acl_expr *expr, struct acl_test *test)
2408{
2409 struct stksess *ts;
2410 struct stktable_key *key;
2411 void *ptr;
2412
2413 key = tcpv4_src_to_stktable_key(l4);
2414 if (!key)
2415 return 0; /* only TCPv4 is supported right now */
2416
2417 if (expr->arg_len)
2418 px = find_stktable(expr->arg.str);
2419
2420 if (!px)
2421 return 0; /* table not found */
2422
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002423 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2424 /* entry does not exist and could not be created */
2425 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002426
2427 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2428 if (!ptr)
2429 return 0; /* parameter not stored in this table */
2430
2431 test->i = ++stktable_data_cast(ptr, conn_cnt);
2432 test->flags = ACL_TEST_F_VOL_TEST;
2433 return 1;
2434}
2435
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002436/* set test->i to the number of concurrent connections in the stksess entry <ts> */
2437static int
2438acl_fetch_conn_cur(struct stktable *table, struct acl_test *test, struct stksess *ts)
2439{
2440 test->flags = ACL_TEST_F_VOL_TEST;
2441 test->i = 0;
2442
2443 if (ts != NULL) {
2444 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CUR);
2445 if (!ptr)
2446 return 0; /* parameter not stored */
2447 test->i = stktable_data_cast(ptr, conn_cur);
2448 }
2449 return 1;
2450}
2451
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002452/* set test->i to the number of concurrent connections from the session's tracked FE counters */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002453static int
Willy Tarreau56123282010-08-06 19:06:56 +02002454acl_fetch_sc1_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2455 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002456{
Willy Tarreau56123282010-08-06 19:06:56 +02002457 if (!l4->stkctr1_entry)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002458 return 0;
2459
Willy Tarreau56123282010-08-06 19:06:56 +02002460 return acl_fetch_conn_cur(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002461}
2462
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002463/* set test->i to the number of concurrent connections from the session's tracked BE counters */
2464static int
Willy Tarreau56123282010-08-06 19:06:56 +02002465acl_fetch_sc2_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2466 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002467{
Willy Tarreau56123282010-08-06 19:06:56 +02002468 if (!l4->stkctr2_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002469 return 0;
2470
Willy Tarreau56123282010-08-06 19:06:56 +02002471 return acl_fetch_conn_cur(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002472}
2473
Willy Tarreau38285c12010-06-18 16:35:43 +02002474/* set test->i to the number of concurrent connections from the session's source
2475 * address in the table pointed to by expr.
2476 */
2477static int
2478acl_fetch_src_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2479 struct acl_expr *expr, struct acl_test *test)
2480{
Willy Tarreau38285c12010-06-18 16:35:43 +02002481 struct stktable_key *key;
2482
2483 key = tcpv4_src_to_stktable_key(l4);
2484 if (!key)
2485 return 0; /* only TCPv4 is supported right now */
2486
2487 if (expr->arg_len)
2488 px = find_stktable(expr->arg.str);
2489
2490 if (!px)
2491 return 0; /* table not found */
2492
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002493 return acl_fetch_conn_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau38285c12010-06-18 16:35:43 +02002494}
2495
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002496/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2497static int
2498acl_fetch_sess_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2499{
2500 test->flags = ACL_TEST_F_VOL_TEST;
2501 test->i = 0;
2502 if (ts != NULL) {
2503 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_CNT);
2504 if (!ptr)
2505 return 0; /* parameter not stored */
2506 test->i = stktable_data_cast(ptr, sess_cnt);
2507 }
2508 return 1;
2509}
2510
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002511/* set test->i to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002512static int
Willy Tarreau56123282010-08-06 19:06:56 +02002513acl_fetch_sc1_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2514 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002515{
Willy Tarreau56123282010-08-06 19:06:56 +02002516 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002517 return 0;
2518
Willy Tarreau56123282010-08-06 19:06:56 +02002519 return acl_fetch_sess_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002520}
2521
2522/* set test->i to the cumulated number of sessions from the session's tracked BE counters */
2523static int
Willy Tarreau56123282010-08-06 19:06:56 +02002524acl_fetch_sc2_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2525 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002526{
Willy Tarreau56123282010-08-06 19:06:56 +02002527 if (!l4->stkctr2_entry)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002528 return 0;
2529
Willy Tarreau56123282010-08-06 19:06:56 +02002530 return acl_fetch_sess_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002531}
2532
2533/* set test->i to the cumulated number of session from the session's source
2534 * address in the table pointed to by expr.
2535 */
2536static int
2537acl_fetch_src_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2538 struct acl_expr *expr, struct acl_test *test)
2539{
2540 struct stktable_key *key;
2541
2542 key = tcpv4_src_to_stktable_key(l4);
2543 if (!key)
2544 return 0; /* only TCPv4 is supported right now */
2545
2546 if (expr->arg_len)
2547 px = find_stktable(expr->arg.str);
2548
2549 if (!px)
2550 return 0; /* table not found */
2551
2552 return acl_fetch_sess_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2553}
2554
Willy Tarreau91c43d72010-06-20 11:19:22 +02002555/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2556static int
2557acl_fetch_sess_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2558{
2559 test->flags = ACL_TEST_F_VOL_TEST;
2560 test->i = 0;
2561 if (ts != NULL) {
2562 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_RATE);
2563 if (!ptr)
2564 return 0; /* parameter not stored */
2565 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
2566 table->data_arg[STKTABLE_DT_SESS_RATE].u);
2567 }
2568 return 1;
2569}
2570
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002571/* set test->i to the session rate from the session's tracked FE counters over
2572 * the configured period.
2573 */
2574static int
Willy Tarreau56123282010-08-06 19:06:56 +02002575acl_fetch_sc1_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2576 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002577{
Willy Tarreau56123282010-08-06 19:06:56 +02002578 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002579 return 0;
2580
Willy Tarreau56123282010-08-06 19:06:56 +02002581 return acl_fetch_sess_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002582}
2583
2584/* set test->i to the session rate from the session's tracked BE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002585 * the configured period.
2586 */
2587static int
Willy Tarreau56123282010-08-06 19:06:56 +02002588acl_fetch_sc2_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2589 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002590{
Willy Tarreau56123282010-08-06 19:06:56 +02002591 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002592 return 0;
2593
Willy Tarreau56123282010-08-06 19:06:56 +02002594 return acl_fetch_sess_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002595}
2596
2597/* set test->i to the session rate from the session's source address in the
2598 * table pointed to by expr, over the configured period.
2599 */
2600static int
2601acl_fetch_src_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2602 struct acl_expr *expr, struct acl_test *test)
2603{
2604 struct stktable_key *key;
2605
2606 key = tcpv4_src_to_stktable_key(l4);
2607 if (!key)
2608 return 0; /* only TCPv4 is supported right now */
2609
2610 if (expr->arg_len)
2611 px = find_stktable(expr->arg.str);
2612
2613 if (!px)
2614 return 0; /* table not found */
2615
2616 return acl_fetch_sess_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2617}
2618
Willy Tarreauda7ff642010-06-23 11:44:09 +02002619/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2620static int
2621acl_fetch_http_req_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2622{
2623 test->flags = ACL_TEST_F_VOL_TEST;
2624 test->i = 0;
2625 if (ts != NULL) {
2626 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_CNT);
2627 if (!ptr)
2628 return 0; /* parameter not stored */
2629 test->i = stktable_data_cast(ptr, http_req_cnt);
2630 }
2631 return 1;
2632}
2633
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002634/* set test->i to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002635static int
Willy Tarreau56123282010-08-06 19:06:56 +02002636acl_fetch_sc1_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2637 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002638{
Willy Tarreau56123282010-08-06 19:06:56 +02002639 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002640 return 0;
2641
Willy Tarreau56123282010-08-06 19:06:56 +02002642 return acl_fetch_http_req_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002643}
2644
2645/* set test->i to the cumulated number of sessions from the session's tracked BE counters */
2646static int
Willy Tarreau56123282010-08-06 19:06:56 +02002647acl_fetch_sc2_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2648 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002649{
Willy Tarreau56123282010-08-06 19:06:56 +02002650 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002651 return 0;
2652
Willy Tarreau56123282010-08-06 19:06:56 +02002653 return acl_fetch_http_req_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002654}
2655
2656/* set test->i to the cumulated number of session from the session's source
2657 * address in the table pointed to by expr.
2658 */
2659static int
2660acl_fetch_src_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002661 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002662{
2663 struct stktable_key *key;
2664
2665 key = tcpv4_src_to_stktable_key(l4);
2666 if (!key)
2667 return 0; /* only TCPv4 is supported right now */
2668
2669 if (expr->arg_len)
2670 px = find_stktable(expr->arg.str);
2671
2672 if (!px)
2673 return 0; /* table not found */
2674
2675 return acl_fetch_http_req_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2676}
2677
2678/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2679static int
2680acl_fetch_http_req_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2681{
2682 test->flags = ACL_TEST_F_VOL_TEST;
2683 test->i = 0;
2684 if (ts != NULL) {
2685 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_RATE);
2686 if (!ptr)
2687 return 0; /* parameter not stored */
2688 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2689 table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
2690 }
2691 return 1;
2692}
2693
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002694/* set test->i to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02002695 * the configured period.
2696 */
2697static int
Willy Tarreau56123282010-08-06 19:06:56 +02002698acl_fetch_sc1_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2699 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002700{
Willy Tarreau56123282010-08-06 19:06:56 +02002701 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002702 return 0;
2703
Willy Tarreau56123282010-08-06 19:06:56 +02002704 return acl_fetch_http_req_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002705}
2706
2707/* set test->i to the session rate from the session's tracked BE counters over
2708 * the configured period.
2709 */
2710static int
Willy Tarreau56123282010-08-06 19:06:56 +02002711acl_fetch_sc2_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2712 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002713{
Willy Tarreau56123282010-08-06 19:06:56 +02002714 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002715 return 0;
2716
Willy Tarreau56123282010-08-06 19:06:56 +02002717 return acl_fetch_http_req_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002718}
2719
2720/* set test->i to the session rate from the session's source address in the
2721 * table pointed to by expr, over the configured period.
2722 */
2723static int
2724acl_fetch_src_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002725 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002726{
2727 struct stktable_key *key;
2728
2729 key = tcpv4_src_to_stktable_key(l4);
2730 if (!key)
2731 return 0; /* only TCPv4 is supported right now */
2732
2733 if (expr->arg_len)
2734 px = find_stktable(expr->arg.str);
2735
2736 if (!px)
2737 return 0; /* table not found */
2738
2739 return acl_fetch_http_req_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2740}
2741
2742/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2743static int
2744acl_fetch_http_err_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2745{
2746 test->flags = ACL_TEST_F_VOL_TEST;
2747 test->i = 0;
2748 if (ts != NULL) {
2749 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_CNT);
2750 if (!ptr)
2751 return 0; /* parameter not stored */
2752 test->i = stktable_data_cast(ptr, http_err_cnt);
2753 }
2754 return 1;
2755}
2756
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002757/* set test->i to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002758static int
Willy Tarreau56123282010-08-06 19:06:56 +02002759acl_fetch_sc1_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2760 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002761{
Willy Tarreau56123282010-08-06 19:06:56 +02002762 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002763 return 0;
2764
Willy Tarreau56123282010-08-06 19:06:56 +02002765 return acl_fetch_http_err_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002766}
2767
2768/* set test->i to the cumulated number of sessions from the session's tracked BE counters */
2769static int
Willy Tarreau56123282010-08-06 19:06:56 +02002770acl_fetch_sc2_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2771 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002772{
Willy Tarreau56123282010-08-06 19:06:56 +02002773 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002774 return 0;
2775
Willy Tarreau56123282010-08-06 19:06:56 +02002776 return acl_fetch_http_err_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002777}
2778
2779/* set test->i to the cumulated number of session from the session's source
2780 * address in the table pointed to by expr.
2781 */
2782static int
2783acl_fetch_src_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002784 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002785{
2786 struct stktable_key *key;
2787
2788 key = tcpv4_src_to_stktable_key(l4);
2789 if (!key)
2790 return 0; /* only TCPv4 is supported right now */
2791
2792 if (expr->arg_len)
2793 px = find_stktable(expr->arg.str);
2794
2795 if (!px)
2796 return 0; /* table not found */
2797
2798 return acl_fetch_http_err_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2799}
2800
2801/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2802static int
2803acl_fetch_http_err_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2804{
2805 test->flags = ACL_TEST_F_VOL_TEST;
2806 test->i = 0;
2807 if (ts != NULL) {
2808 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_RATE);
2809 if (!ptr)
2810 return 0; /* parameter not stored */
2811 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2812 table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
2813 }
2814 return 1;
2815}
2816
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002817/* set test->i to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02002818 * the configured period.
2819 */
2820static int
Willy Tarreau56123282010-08-06 19:06:56 +02002821acl_fetch_sc1_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2822 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002823{
Willy Tarreau56123282010-08-06 19:06:56 +02002824 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002825 return 0;
2826
Willy Tarreau56123282010-08-06 19:06:56 +02002827 return acl_fetch_http_err_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002828}
2829
2830/* set test->i to the session rate from the session's tracked BE counters over
2831 * the configured period.
2832 */
2833static int
Willy Tarreau56123282010-08-06 19:06:56 +02002834acl_fetch_sc2_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2835 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002836{
Willy Tarreau56123282010-08-06 19:06:56 +02002837 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002838 return 0;
2839
Willy Tarreau56123282010-08-06 19:06:56 +02002840 return acl_fetch_http_err_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002841}
2842
2843/* set test->i to the session rate from the session's source address in the
2844 * table pointed to by expr, over the configured period.
2845 */
2846static int
2847acl_fetch_src_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2848 struct acl_expr *expr, struct acl_test *test)
2849{
2850 struct stktable_key *key;
2851
2852 key = tcpv4_src_to_stktable_key(l4);
2853 if (!key)
2854 return 0; /* only TCPv4 is supported right now */
2855
2856 if (expr->arg_len)
2857 px = find_stktable(expr->arg.str);
2858
2859 if (!px)
2860 return 0; /* table not found */
2861
2862 return acl_fetch_http_err_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2863}
2864
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002865/* set test->i to the number of kbytes received from clients matching the stksess entry <ts> */
2866static int
2867acl_fetch_kbytes_in(struct stktable *table, struct acl_test *test, struct stksess *ts)
2868{
2869 test->flags = ACL_TEST_F_VOL_TEST;
2870 test->i = 0;
2871
2872 if (ts != NULL) {
2873 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_CNT);
2874 if (!ptr)
2875 return 0; /* parameter not stored */
2876 test->i = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
2877 }
2878 return 1;
2879}
2880
2881/* set test->i to the number of kbytes received from clients according to the
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002882 * session's tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002883 */
2884static int
Willy Tarreau56123282010-08-06 19:06:56 +02002885acl_fetch_sc1_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2886 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002887{
Willy Tarreau56123282010-08-06 19:06:56 +02002888 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002889 return 0;
2890
Willy Tarreau56123282010-08-06 19:06:56 +02002891 return acl_fetch_kbytes_in(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002892}
2893
2894/* set test->i to the number of kbytes received from clients according to the
2895 * session's tracked BE counters.
2896 */
2897static int
Willy Tarreau56123282010-08-06 19:06:56 +02002898acl_fetch_sc2_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2899 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002900{
Willy Tarreau56123282010-08-06 19:06:56 +02002901 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002902 return 0;
2903
Willy Tarreau56123282010-08-06 19:06:56 +02002904 return acl_fetch_kbytes_in(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002905}
2906
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002907/* set test->i to the number of kbytes received from the session's source
2908 * address in the table pointed to by expr.
2909 */
2910static int
2911acl_fetch_src_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2912 struct acl_expr *expr, struct acl_test *test)
2913{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002914 struct stktable_key *key;
2915
2916 key = tcpv4_src_to_stktable_key(l4);
2917 if (!key)
2918 return 0; /* only TCPv4 is supported right now */
2919
2920 if (expr->arg_len)
2921 px = find_stktable(expr->arg.str);
2922
2923 if (!px)
2924 return 0; /* table not found */
2925
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002926 return acl_fetch_kbytes_in(&px->table, test, stktable_lookup_key(&px->table, key));
2927}
2928
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002929/* set test->i to the bytes rate from clients in the stksess entry <ts> over the
2930 * configured period.
2931 */
2932static int
2933acl_fetch_bytes_in_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2934{
2935 test->flags = ACL_TEST_F_VOL_TEST;
2936 test->i = 0;
2937 if (ts != NULL) {
2938 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_RATE);
2939 if (!ptr)
2940 return 0; /* parameter not stored */
2941 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
2942 table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
2943 }
2944 return 1;
2945}
2946
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002947/* set test->i to the bytes rate from clients from the session's tracked FE
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002948 * counters over the configured period.
2949 */
2950static int
Willy Tarreau56123282010-08-06 19:06:56 +02002951acl_fetch_sc1_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2952 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002953{
Willy Tarreau56123282010-08-06 19:06:56 +02002954 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002955 return 0;
2956
Willy Tarreau56123282010-08-06 19:06:56 +02002957 return acl_fetch_bytes_in_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002958}
2959
2960/* set test->i to the bytes rate from clients from the session's tracked BE
2961 * counters over the configured period.
2962 */
2963static int
Willy Tarreau56123282010-08-06 19:06:56 +02002964acl_fetch_sc2_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2965 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002966{
Willy Tarreau56123282010-08-06 19:06:56 +02002967 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002968 return 0;
2969
Willy Tarreau56123282010-08-06 19:06:56 +02002970 return acl_fetch_bytes_in_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002971}
2972
2973/* set test->i to the bytes rate from clients from the session's source address
2974 * in the table pointed to by expr, over the configured period.
2975 */
2976static int
2977acl_fetch_src_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002978 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002979{
2980 struct stktable_key *key;
2981
2982 key = tcpv4_src_to_stktable_key(l4);
2983 if (!key)
2984 return 0; /* only TCPv4 is supported right now */
2985
2986 if (expr->arg_len)
2987 px = find_stktable(expr->arg.str);
2988
2989 if (!px)
2990 return 0; /* table not found */
2991
2992 return acl_fetch_bytes_in_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2993}
2994
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002995/* set test->i to the number of kbytes sent to clients matching the stksess entry <ts> */
2996static int
2997acl_fetch_kbytes_out(struct stktable *table, struct acl_test *test, struct stksess *ts)
2998{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002999 test->flags = ACL_TEST_F_VOL_TEST;
3000 test->i = 0;
3001
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003002 if (ts != NULL) {
3003 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003004 if (!ptr)
3005 return 0; /* parameter not stored */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003006 test->i = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003007 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003008 return 1;
3009}
3010
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003011/* set test->i to the number of kbytes sent to clients according to the session's
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003012 * tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003013 */
3014static int
Willy Tarreau56123282010-08-06 19:06:56 +02003015acl_fetch_sc1_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3016 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003017{
Willy Tarreau56123282010-08-06 19:06:56 +02003018 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003019 return 0;
3020
Willy Tarreau56123282010-08-06 19:06:56 +02003021 return acl_fetch_kbytes_out(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003022}
3023
3024/* set test->i to the number of kbytes sent to clients according to the session's
3025 * tracked BE counters.
3026 */
3027static int
Willy Tarreau56123282010-08-06 19:06:56 +02003028acl_fetch_sc2_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3029 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003030{
Willy Tarreau56123282010-08-06 19:06:56 +02003031 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003032 return 0;
3033
Willy Tarreau56123282010-08-06 19:06:56 +02003034 return acl_fetch_kbytes_out(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003035}
3036
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003037/* set test->i to the number of kbytes sent to the session's source address in
3038 * the table pointed to by expr.
3039 */
3040static int
3041acl_fetch_src_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3042 struct acl_expr *expr, struct acl_test *test)
3043{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003044 struct stktable_key *key;
3045
3046 key = tcpv4_src_to_stktable_key(l4);
3047 if (!key)
3048 return 0; /* only TCPv4 is supported right now */
3049
3050 if (expr->arg_len)
3051 px = find_stktable(expr->arg.str);
3052
3053 if (!px)
3054 return 0; /* table not found */
3055
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003056 return acl_fetch_kbytes_out(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003057}
3058
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003059/* set test->i to the bytes rate to clients in the stksess entry <ts> over the
3060 * configured period.
3061 */
3062static int
3063acl_fetch_bytes_out_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
3064{
3065 test->flags = ACL_TEST_F_VOL_TEST;
3066 test->i = 0;
3067 if (ts != NULL) {
3068 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_RATE);
3069 if (!ptr)
3070 return 0; /* parameter not stored */
3071 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
3072 table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
3073 }
3074 return 1;
3075}
3076
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003077/* set test->i to the bytes rate to clients from the session's tracked FE counters
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003078 * over the configured period.
3079 */
3080static int
Willy Tarreau56123282010-08-06 19:06:56 +02003081acl_fetch_sc1_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3082 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003083{
Willy Tarreau56123282010-08-06 19:06:56 +02003084 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003085 return 0;
3086
Willy Tarreau56123282010-08-06 19:06:56 +02003087 return acl_fetch_bytes_out_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003088}
3089
3090/* set test->i to the bytes rate to clients from the session's tracked BE counters
3091 * over the configured period.
3092 */
3093static int
Willy Tarreau56123282010-08-06 19:06:56 +02003094acl_fetch_sc2_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3095 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003096{
Willy Tarreau56123282010-08-06 19:06:56 +02003097 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003098 return 0;
3099
Willy Tarreau56123282010-08-06 19:06:56 +02003100 return acl_fetch_bytes_out_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003101}
3102
3103/* set test->i to the bytes rate to client from the session's source address in
3104 * the table pointed to by expr, over the configured period.
3105 */
3106static int
3107acl_fetch_src_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02003108 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003109{
3110 struct stktable_key *key;
3111
3112 key = tcpv4_src_to_stktable_key(l4);
3113 if (!key)
3114 return 0; /* only TCPv4 is supported right now */
3115
3116 if (expr->arg_len)
3117 px = find_stktable(expr->arg.str);
3118
3119 if (!px)
3120 return 0; /* table not found */
3121
3122 return acl_fetch_bytes_out_rate(&px->table, test, stktable_lookup_key(&px->table, key));
3123}
3124
Willy Tarreau8b22a712010-06-18 17:46:06 +02003125
3126/* Note: must not be declared <const> as its list will be overwritten */
3127static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau56123282010-08-06 19:06:56 +02003128 { "sc1_get_gpc0", acl_parse_int, acl_fetch_sc1_get_gpc0, acl_match_int, ACL_USE_NOTHING },
3129 { "sc2_get_gpc0", acl_parse_int, acl_fetch_sc2_get_gpc0, acl_match_int, ACL_USE_NOTHING },
3130 { "src_get_gpc0", acl_parse_int, acl_fetch_src_get_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE },
3131 { "sc1_inc_gpc0", acl_parse_int, acl_fetch_sc1_inc_gpc0, acl_match_int, ACL_USE_NOTHING },
3132 { "sc2_inc_gpc0", acl_parse_int, acl_fetch_sc2_inc_gpc0, acl_match_int, ACL_USE_NOTHING },
3133 { "src_inc_gpc0", acl_parse_int, acl_fetch_src_inc_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE },
3134 { "sc1_conn_cnt", acl_parse_int, acl_fetch_sc1_conn_cnt, acl_match_int, ACL_USE_NOTHING },
3135 { "sc2_conn_cnt", acl_parse_int, acl_fetch_sc2_conn_cnt, acl_match_int, ACL_USE_NOTHING },
3136 { "src_conn_cnt", acl_parse_int, acl_fetch_src_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3137 { "sc1_conn_rate", acl_parse_int, acl_fetch_sc1_conn_rate, acl_match_int, ACL_USE_NOTHING },
3138 { "sc2_conn_rate", acl_parse_int, acl_fetch_sc2_conn_rate, acl_match_int, ACL_USE_NOTHING },
3139 { "src_conn_rate", acl_parse_int, acl_fetch_src_conn_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3140 { "src_updt_conn_cnt", acl_parse_int, acl_fetch_src_updt_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3141 { "sc1_conn_cur", acl_parse_int, acl_fetch_sc1_conn_cur, acl_match_int, ACL_USE_NOTHING },
3142 { "sc2_conn_cur", acl_parse_int, acl_fetch_sc2_conn_cur, acl_match_int, ACL_USE_NOTHING },
3143 { "src_conn_cur", acl_parse_int, acl_fetch_src_conn_cur, acl_match_int, ACL_USE_TCP4_VOLATILE },
3144 { "sc1_sess_cnt", acl_parse_int, acl_fetch_sc1_sess_cnt, acl_match_int, ACL_USE_NOTHING },
3145 { "sc2_sess_cnt", acl_parse_int, acl_fetch_sc2_sess_cnt, acl_match_int, ACL_USE_NOTHING },
3146 { "src_sess_cnt", acl_parse_int, acl_fetch_src_sess_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3147 { "sc1_sess_rate", acl_parse_int, acl_fetch_sc1_sess_rate, acl_match_int, ACL_USE_NOTHING },
3148 { "sc2_sess_rate", acl_parse_int, acl_fetch_sc2_sess_rate, acl_match_int, ACL_USE_NOTHING },
3149 { "src_sess_rate", acl_parse_int, acl_fetch_src_sess_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3150 { "sc1_http_req_cnt", acl_parse_int, acl_fetch_sc1_http_req_cnt, acl_match_int, ACL_USE_NOTHING },
3151 { "sc2_http_req_cnt", acl_parse_int, acl_fetch_sc2_http_req_cnt, acl_match_int, ACL_USE_NOTHING },
3152 { "src_http_req_cnt", acl_parse_int, acl_fetch_src_http_req_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3153 { "sc1_http_req_rate", acl_parse_int, acl_fetch_sc1_http_req_rate, acl_match_int, ACL_USE_NOTHING },
3154 { "sc2_http_req_rate", acl_parse_int, acl_fetch_sc2_http_req_rate, acl_match_int, ACL_USE_NOTHING },
3155 { "src_http_req_rate", acl_parse_int, acl_fetch_src_http_req_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3156 { "sc1_http_err_cnt", acl_parse_int, acl_fetch_sc1_http_err_cnt, acl_match_int, ACL_USE_NOTHING },
3157 { "sc2_http_err_cnt", acl_parse_int, acl_fetch_sc2_http_err_cnt, acl_match_int, ACL_USE_NOTHING },
3158 { "src_http_err_cnt", acl_parse_int, acl_fetch_src_http_err_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3159 { "sc1_http_err_rate", acl_parse_int, acl_fetch_sc1_http_err_rate, acl_match_int, ACL_USE_NOTHING },
3160 { "sc2_http_err_rate", acl_parse_int, acl_fetch_sc2_http_err_rate, acl_match_int, ACL_USE_NOTHING },
3161 { "src_http_err_rate", acl_parse_int, acl_fetch_src_http_err_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3162 { "sc1_kbytes_in", acl_parse_int, acl_fetch_sc1_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3163 { "sc2_kbytes_in", acl_parse_int, acl_fetch_sc2_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3164 { "src_kbytes_in", acl_parse_int, acl_fetch_src_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3165 { "sc1_bytes_in_rate", acl_parse_int, acl_fetch_sc1_bytes_in_rate, acl_match_int, ACL_USE_NOTHING },
3166 { "sc2_bytes_in_rate", acl_parse_int, acl_fetch_sc2_bytes_in_rate, acl_match_int, ACL_USE_NOTHING },
3167 { "src_bytes_in_rate", acl_parse_int, acl_fetch_src_bytes_in_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3168 { "sc1_kbytes_out", acl_parse_int, acl_fetch_sc1_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3169 { "sc2_kbytes_out", acl_parse_int, acl_fetch_sc2_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3170 { "src_kbytes_out", acl_parse_int, acl_fetch_src_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3171 { "sc1_bytes_out_rate", acl_parse_int, acl_fetch_sc1_bytes_out_rate, acl_match_int, ACL_USE_NOTHING },
3172 { "sc2_bytes_out_rate", acl_parse_int, acl_fetch_sc2_bytes_out_rate, acl_match_int, ACL_USE_NOTHING },
3173 { "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 +02003174 { NULL, NULL, NULL, NULL },
3175}};
3176
3177
Willy Tarreau56123282010-08-06 19:06:56 +02003178/* Parse a "track-sc[12]" line starting with "track-sc[12]" in args[arg-1].
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003179 * Returns the number of warnings emitted, or -1 in case of fatal errors. The
3180 * <prm> struct is fed with the table name if any. If unspecified, the caller
3181 * will assume that the current proxy's table is used.
3182 */
3183int parse_track_counters(char **args, int *arg,
3184 int section_type, struct proxy *curpx,
3185 struct track_ctr_prm *prm,
3186 struct proxy *defpx, char *err, int errlen)
3187{
3188 int pattern_type = 0;
Willy Tarreau56123282010-08-06 19:06:56 +02003189 char *kw = args[*arg - 1];
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003190
Willy Tarreau56123282010-08-06 19:06:56 +02003191 /* parse the arguments of "track-sc[12]" before the condition in the
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003192 * following form :
Willy Tarreau56123282010-08-06 19:06:56 +02003193 * track-sc[12] src [ table xxx ] [ if|unless ... ]
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003194 */
3195 while (args[*arg]) {
3196 if (strcmp(args[*arg], "src") == 0) {
3197 prm->type = STKTABLE_TYPE_IP;
3198 pattern_type = 1;
3199 }
3200 else if (strcmp(args[*arg], "table") == 0) {
3201 if (!args[*arg + 1]) {
3202 snprintf(err, errlen,
Willy Tarreau56123282010-08-06 19:06:56 +02003203 "missing table for %s in %s '%s'.",
3204 kw, proxy_type_str(curpx), curpx->id);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003205 return -1;
3206 }
3207 /* we copy the table name for now, it will be resolved later */
3208 prm->table.n = strdup(args[*arg + 1]);
3209 (*arg)++;
3210 }
3211 else {
3212 /* unhandled keywords are handled by the caller */
3213 break;
3214 }
3215 (*arg)++;
3216 }
3217
3218 if (!pattern_type) {
3219 snprintf(err, errlen,
Willy Tarreau56123282010-08-06 19:06:56 +02003220 "%s key not specified in %s '%s' (found %s, only 'src' is supported).",
3221 kw, proxy_type_str(curpx), curpx->id, quote_arg(args[*arg]));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003222 return -1;
3223 }
3224
3225 return 0;
3226}
3227
Willy Tarreau8b22a712010-06-18 17:46:06 +02003228__attribute__((constructor))
3229static void __session_init(void)
3230{
3231 acl_register_keywords(&acl_kws);
3232}
3233
Willy Tarreaubaaee002006-06-26 02:48:02 +02003234/*
3235 * Local variables:
3236 * c-indent-level: 8
3237 * c-basic-offset: 8
3238 * End:
3239 */