blob: 2c115767cd1870be2786ae21edc9acde79541f9c [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 Tarreau9ba2dcc2010-06-14 21:04:55 +020067 s->tracked_counters = NULL;
68 s->tracked_table = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020069
70 /* if this session comes from a known monitoring system, we want to ignore
71 * it as soon as possible, which means closing it immediately for TCP, but
72 * cleanly.
73 */
74 if (unlikely((l->options & LI_O_CHK_MONNET) &&
75 addr->ss_family == AF_INET &&
76 (((struct sockaddr_in *)addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr)) {
77 if (p->mode == PR_MODE_TCP) {
78 pool_free2(pool2_session, s);
79 return 0;
80 }
81 s->flags |= SN_MONITOR;
82 s->logs.logwait = 0;
83 }
84
85 /* OK, we're keeping the session, so let's properly initialize the session */
86 LIST_ADDQ(&sessions, &s->list);
87 LIST_INIT(&s->back_refs);
88
89 if (unlikely((t = task_new()) == NULL)) { /* disable this proxy for a while */
90 Alert("out of memory in event_accept().\n");
91 goto out_free_session;
92 }
93
94 s->term_trace = 0;
95 s->cli_addr = *addr;
96 s->logs.accept_date = date; /* user-visible date for logging */
97 s->logs.tv_accept = now; /* corrected date for internal use */
98 s->uniq_id = totalconn;
Willy Tarreau24dcaf32010-06-05 10:49:41 +020099 p->feconn++; /* beconn will be increased once assigned */
100
Willy Tarreaub36b4242010-06-04 20:59:39 +0200101 proxy_inc_fe_conn_ctr(l, p); /* note: cum_beconn will be increased once assigned */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200102
103 t->process = l->handler;
104 t->context = s;
105 t->nice = l->nice;
106 t->expire = TICK_ETERNITY;
107
108 s->task = t;
109 s->listener = l;
110
111 /* Note: initially, the session's backend points to the frontend.
112 * This changes later when switching rules are executed or
113 * when the default backend is assigned.
114 */
115 s->be = s->fe = p;
116 s->req = s->rep = NULL; /* will be allocated later */
117
118 /* now evaluate the tcp-request layer4 rules. Since we expect to be able
119 * to abort right here as soon as possible, we check the rules before
120 * even initializing the stream interfaces.
121 */
122 if ((l->options & LI_O_TCP_RULES) && !tcp_exec_req_rules(s)) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200123 if (s->tracked_counters)
124 session_store_counters(s);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200125 task_free(t);
126 LIST_DEL(&s->list);
127 pool_free2(pool2_session, s);
128 /* let's do a no-linger now to close with a single RST. */
129 setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200130 p->feconn--;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200131 return 0;
132 }
133
Willy Tarreaub36b4242010-06-04 20:59:39 +0200134 /* This session was accepted, count it now */
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200135 if (p->feconn > p->counters.feconn_max)
136 p->counters.feconn_max = p->feconn;
Willy Tarreaub36b4242010-06-04 20:59:39 +0200137 proxy_inc_fe_sess_ctr(l, p);
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200138 if (s->tracked_counters) {
Willy Tarreau91c43d72010-06-20 11:19:22 +0200139 void *ptr;
140
141 ptr = stktable_data_ptr(s->tracked_table, s->tracked_counters, STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200142 if (ptr)
143 stktable_data_cast(ptr, sess_cnt)++;
Willy Tarreau91c43d72010-06-20 11:19:22 +0200144
145 ptr = stktable_data_ptr(s->tracked_table, s->tracked_counters, STKTABLE_DT_SESS_RATE);
146 if (ptr)
147 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
148 s->tracked_table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200149 }
Willy Tarreaub36b4242010-06-04 20:59:39 +0200150
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200151 /* this part should be common with other protocols */
152 s->si[0].fd = cfd;
153 s->si[0].owner = t;
154 s->si[0].state = s->si[0].prev_state = SI_ST_EST;
155 s->si[0].err_type = SI_ET_NONE;
156 s->si[0].err_loc = NULL;
157 s->si[0].connect = NULL;
158 s->si[0].iohandler = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200159 s->si[0].release = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200160 s->si[0].exp = TICK_ETERNITY;
161 s->si[0].flags = SI_FL_NONE;
162
163 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
164 s->si[0].flags |= SI_FL_INDEP_STR;
165
166 if (addr->ss_family == AF_INET || addr->ss_family == AF_INET6)
167 s->si[0].flags = SI_FL_CAP_SPLTCP; /* TCP/TCPv6 splicing possible */
168
169 /* add the various callbacks */
170 stream_sock_prepare_interface(&s->si[0]);
171
172 /* pre-initialize the other side's stream interface to an INIT state. The
173 * callbacks will be initialized before attempting to connect.
174 */
175 s->si[1].fd = -1; /* just to help with debugging */
176 s->si[1].owner = t;
177 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
178 s->si[1].err_type = SI_ET_NONE;
179 s->si[1].err_loc = NULL;
180 s->si[1].connect = NULL;
181 s->si[1].iohandler = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200182 s->si[1].release = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200183 s->si[1].shutr = stream_int_shutr;
184 s->si[1].shutw = stream_int_shutw;
185 s->si[1].exp = TICK_ETERNITY;
186 s->si[1].flags = SI_FL_NONE;
187
188 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
189 s->si[1].flags |= SI_FL_INDEP_STR;
190
191 s->srv = s->prev_srv = s->srv_conn = NULL;
192 s->pend_pos = NULL;
193
194 /* init store persistence */
195 s->store_count = 0;
196
197 /* Adjust some socket options */
198 if (unlikely(fcntl(cfd, F_SETFL, O_NONBLOCK) == -1)) {
199 Alert("accept(): cannot set the socket in non blocking mode. Giving up\n");
200 goto out_free_task;
201 }
202
203 txn = &s->txn;
204 /* Those variables will be checked and freed if non-NULL in
205 * session.c:session_free(). It is important that they are
206 * properly initialized.
207 */
208 txn->sessid = NULL;
209 txn->srv_cookie = NULL;
210 txn->cli_cookie = NULL;
211 txn->uri = NULL;
212 txn->req.cap = NULL;
213 txn->rsp.cap = NULL;
214 txn->hdr_idx.v = NULL;
215 txn->hdr_idx.size = txn->hdr_idx.used = 0;
216
217 if (unlikely((s->req = pool_alloc2(pool2_buffer)) == NULL))
218 goto out_free_task; /* no memory */
219
220 if (unlikely((s->rep = pool_alloc2(pool2_buffer)) == NULL))
221 goto out_free_req; /* no memory */
222
223 /* initialize the request buffer */
224 s->req->size = global.tune.bufsize;
225 buffer_init(s->req);
226 s->req->prod = &s->si[0];
227 s->req->cons = &s->si[1];
228 s->si[0].ib = s->si[1].ob = s->req;
229 s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */
230
231 /* activate default analysers enabled for this listener */
232 s->req->analysers = l->analysers;
233
234 s->req->wto = TICK_ETERNITY;
235 s->req->rto = TICK_ETERNITY;
236 s->req->rex = TICK_ETERNITY;
237 s->req->wex = TICK_ETERNITY;
238 s->req->analyse_exp = TICK_ETERNITY;
239
240 /* initialize response buffer */
241 s->rep->size = global.tune.bufsize;
242 buffer_init(s->rep);
243 s->rep->prod = &s->si[1];
244 s->rep->cons = &s->si[0];
245 s->si[0].ob = s->si[1].ib = s->rep;
246 s->rep->analysers = 0;
247
248 s->rep->rto = TICK_ETERNITY;
249 s->rep->wto = TICK_ETERNITY;
250 s->rep->rex = TICK_ETERNITY;
251 s->rep->wex = TICK_ETERNITY;
252 s->rep->analyse_exp = TICK_ETERNITY;
253
254 /* finish initialization of the accepted file descriptor */
255 fd_insert(cfd);
256 fdtab[cfd].owner = &s->si[0];
257 fdtab[cfd].state = FD_STREADY;
258 fdtab[cfd].flags = 0;
259 fdtab[cfd].cb[DIR_RD].f = l->proto->read;
260 fdtab[cfd].cb[DIR_RD].b = s->req;
261 fdtab[cfd].cb[DIR_WR].f = l->proto->write;
262 fdtab[cfd].cb[DIR_WR].b = s->rep;
263 fdinfo[cfd].peeraddr = (struct sockaddr *)&s->cli_addr;
264 fdinfo[cfd].peerlen = sizeof(s->cli_addr);
265 EV_FD_SET(cfd, DIR_RD);
266
267 if (p->accept) {
268 int ret = p->accept(s);
269 if (unlikely(ret < 0))
270 goto out_free_rep;
271
272 if (unlikely(ret == 0)) {
273 /* work is finished, we can release everything (eg: monitoring) */
274 pool_free2(pool2_buffer, s->rep);
275 pool_free2(pool2_buffer, s->req);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200276 if (s->tracked_counters)
277 session_store_counters(s);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200278 task_free(t);
279 LIST_DEL(&s->list);
280 pool_free2(pool2_session, s);
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200281 p->feconn--;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200282 return 0;
283 }
284 }
285
286 /* it is important not to call the wakeup function directly but to
287 * pass through task_wakeup(), because this one knows how to apply
288 * priorities to tasks.
289 */
290 task_wakeup(t, TASK_WOKEN_INIT);
291 return 1;
292
293 /* Error unrolling */
294 out_free_rep:
295 pool_free2(pool2_buffer, s->rep);
296 out_free_req:
297 pool_free2(pool2_buffer, s->req);
298 out_free_task:
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200299 p->feconn--;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200300 if (s->tracked_counters)
301 session_store_counters(s);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200302 task_free(t);
303 out_free_session:
304 LIST_DEL(&s->list);
305 pool_free2(pool2_session, s);
306 out_close:
307 return -1;
308}
309
Willy Tarreaubaaee002006-06-26 02:48:02 +0200310/*
311 * frees the context associated to a session. It must have been removed first.
312 */
313void session_free(struct session *s)
314{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +0100315 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +0200316 struct proxy *fe = s->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100317 struct bref *bref, *back;
Willy Tarreaua4cda672010-06-06 18:28:49 +0200318 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100319
Willy Tarreaubaaee002006-06-26 02:48:02 +0200320 if (s->pend_pos)
321 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100322
Willy Tarreau1e62de62008-11-11 20:20:02 +0100323 if (s->srv) { /* there may be requests left pending in queue */
324 if (s->flags & SN_CURR_SESS) {
325 s->flags &= ~SN_CURR_SESS;
326 s->srv->cur_sess--;
327 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100328 if (may_dequeue_tasks(s->srv, s->be))
329 process_srv_queue(s->srv);
Willy Tarreau1e62de62008-11-11 20:20:02 +0100330 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100331
Willy Tarreau7c669d72008-06-20 15:04:11 +0200332 if (unlikely(s->srv_conn)) {
333 /* the session still has a reserved slot on a server, but
334 * it should normally be only the same as the one above,
335 * so this should not happen in fact.
336 */
337 sess_change_server(s, NULL);
338 }
339
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100340 if (s->req->pipe)
341 put_pipe(s->req->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100342
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100343 if (s->rep->pipe)
344 put_pipe(s->rep->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100345
Willy Tarreau48d63db2008-08-03 17:41:33 +0200346 pool_free2(pool2_buffer, s->req);
347 pool_free2(pool2_buffer, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200348
Willy Tarreau46023632010-01-07 22:51:47 +0100349 http_end_txn(s);
350
Willy Tarreaua4cda672010-06-06 18:28:49 +0200351 for (i = 0; i < s->store_count; i++) {
352 if (!s->store[i].ts)
353 continue;
354 stksess_free(s->store[i].table, s->store[i].ts);
355 s->store[i].ts = NULL;
356 }
357
Willy Tarreau92fb9832007-10-16 17:34:28 +0200358 if (fe) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200359 pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau46023632010-01-07 22:51:47 +0100360 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
361 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200362 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100363
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200364 if (s->tracked_counters)
365 session_store_counters(s);
366
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100367 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100368 /* we have to unlink all watchers. We must not relink them if
369 * this session was the last one in the list.
370 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100371 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100372 LIST_INIT(&bref->users);
373 if (s->list.n != &sessions)
374 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100375 bref->ref = s->list.n;
376 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100377 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200378 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200379
380 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200381 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200382 pool_flush2(pool2_buffer);
383 pool_flush2(fe->hdr_idx_pool);
384 pool_flush2(pool2_requri);
385 pool_flush2(pool2_capture);
386 pool_flush2(pool2_session);
387 pool_flush2(fe->req_cap_pool);
388 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200389 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200390}
391
392
393/* perform minimal intializations, report 0 in case of error, 1 if OK. */
394int init_session()
395{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100396 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200397 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
398 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200399}
400
Willy Tarreau30e71012007-11-26 20:15:35 +0100401void session_process_counters(struct session *s)
402{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100403 unsigned long long bytes;
404
Willy Tarreau30e71012007-11-26 20:15:35 +0100405 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100406 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100407 s->logs.bytes_in = s->req->total;
408 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200409 s->fe->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100410
Willy Tarreau30e71012007-11-26 20:15:35 +0100411 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200412 s->be->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100413
Willy Tarreau30e71012007-11-26 20:15:35 +0100414 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200415 s->srv->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200416
417 if (s->listener->counters)
418 s->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200419
420 if (s->tracked_counters) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200421 void *ptr;
422
423 ptr = stktable_data_ptr(s->tracked_table,
424 s->tracked_counters,
425 STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200426 if (ptr)
427 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200428
429 ptr = stktable_data_ptr(s->tracked_table,
430 s->tracked_counters,
431 STKTABLE_DT_BYTES_IN_RATE);
432 if (ptr)
433 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
434 s->tracked_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200435 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100436 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100437 }
438
Willy Tarreau30e71012007-11-26 20:15:35 +0100439 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100440 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100441 s->logs.bytes_out = s->rep->total;
442 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200443 s->fe->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100444
Willy Tarreau30e71012007-11-26 20:15:35 +0100445 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200446 s->be->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100447
Willy Tarreau30e71012007-11-26 20:15:35 +0100448 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200449 s->srv->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200450
451 if (s->listener->counters)
452 s->listener->counters->bytes_out += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200453
454 if (s->tracked_counters) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200455 void *ptr;
456
457 ptr = stktable_data_ptr(s->tracked_table,
458 s->tracked_counters,
459 STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200460 if (ptr)
461 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200462
463 ptr = stktable_data_ptr(s->tracked_table,
464 s->tracked_counters,
465 STKTABLE_DT_BYTES_OUT_RATE);
466 if (ptr)
467 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
468 s->tracked_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200469 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100470 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100471 }
472}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200473
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100474/* This function is called with (si->state == SI_ST_CON) meaning that a
475 * connection was attempted and that the file descriptor is already allocated.
476 * We must check for establishment, error and abort. Possible output states
477 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
478 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
479 * otherwise 1.
480 */
481int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
482{
483 struct buffer *req = si->ob;
484 struct buffer *rep = si->ib;
485
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100486 /* If we got an error, or if nothing happened and the connection timed
487 * out, we must give up. The CER state handler will take care of retry
488 * attempts and error reports.
489 */
490 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau127334e2009-03-28 10:47:26 +0100491 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100492 si->state = SI_ST_CER;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200493 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100494 fd_delete(si->fd);
495
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200496 if (si->release)
497 si->release(si);
498
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100499 if (si->err_type)
500 return 0;
501
502 si->err_loc = s->srv;
503 if (si->flags & SI_FL_ERR)
504 si->err_type = SI_ET_CONN_ERR;
505 else
506 si->err_type = SI_ET_CONN_TO;
507 return 0;
508 }
509
510 /* OK, maybe we want to abort */
Willy Tarreau418fd472009-09-06 21:37:23 +0200511 if (unlikely((rep->flags & BF_SHUTW) ||
512 ((req->flags & BF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200513 (((req->flags & (BF_OUT_EMPTY|BF_WRITE_ACTIVITY)) == BF_OUT_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100514 s->be->options & PR_O_ABRT_CLOSE)))) {
515 /* give up */
516 si->shutw(si);
517 si->err_type |= SI_ET_CONN_ABRT;
518 si->err_loc = s->srv;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200519 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau84455332009-03-15 22:34:05 +0100520 if (s->srv_error)
521 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100522 return 1;
523 }
524
525 /* we need to wait a bit more if there was no activity either */
526 if (!(req->flags & BF_WRITE_ACTIVITY))
527 return 1;
528
529 /* OK, this means that a connection succeeded. The caller will be
530 * responsible for handling the transition from CON to EST.
531 */
532 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100533 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100534 si->state = SI_ST_EST;
535 si->err_type = SI_ET_NONE;
536 si->err_loc = NULL;
537 return 1;
538}
539
540/* This function is called with (si->state == SI_ST_CER) meaning that a
541 * previous connection attempt has failed and that the file descriptor
542 * has already been released. Possible causes include asynchronous error
543 * notification and time out. Possible output states are SI_ST_CLO when
544 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
545 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
546 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
547 * marked as in error state. It returns 0.
548 */
549int sess_update_st_cer(struct session *s, struct stream_interface *si)
550{
551 /* we probably have to release last session from the server */
552 if (s->srv) {
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100553 health_adjust(s->srv, HANA_STATUS_L4_ERR);
554
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100555 if (s->flags & SN_CURR_SESS) {
556 s->flags &= ~SN_CURR_SESS;
557 s->srv->cur_sess--;
558 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100559 }
560
561 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200562 si->conn_retries--;
563 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100564 if (!si->err_type) {
565 si->err_type = SI_ET_CONN_ERR;
566 si->err_loc = s->srv;
567 }
568
569 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200570 s->srv->counters.failed_conns++;
571 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100572 if (may_dequeue_tasks(s->srv, s->be))
573 process_srv_queue(s->srv);
574
575 /* shutw is enough so stop a connecting socket */
576 si->shutw(si);
577 si->ob->flags |= BF_WRITE_ERROR;
578 si->ib->flags |= BF_READ_ERROR;
579
580 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100581 if (s->srv_error)
582 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100583 return 0;
584 }
585
586 /* If the "redispatch" option is set on the backend, we are allowed to
587 * retry on another server for the last retry. In order to achieve this,
588 * we must mark the session unassigned, and eventually clear the DIRECT
589 * bit to ignore any persistence cookie. We won't count a retry nor a
590 * redispatch yet, because this will depend on what server is selected.
591 */
Willy Tarreauee28de02010-06-01 09:51:00 +0200592 if (s->srv && si->conn_retries == 0 &&
Willy Tarreau4de91492010-01-22 19:10:05 +0100593 s->be->options & PR_O_REDISP && !(s->flags & SN_FORCE_PRST)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100594 if (may_dequeue_tasks(s->srv, s->be))
595 process_srv_queue(s->srv);
596
597 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
598 s->prev_srv = s->srv;
599 si->state = SI_ST_REQ;
600 } else {
601 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200602 s->srv->counters.retries++;
603 s->be->counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100604 si->state = SI_ST_ASS;
605 }
606
607 if (si->flags & SI_FL_ERR) {
608 /* The error was an asynchronous connection error, and we will
609 * likely have to retry connecting to the same server, most
610 * likely leading to the same result. To avoid this, we wait
611 * one second before retrying.
612 */
613
614 if (!si->err_type)
615 si->err_type = SI_ET_CONN_ERR;
616
617 si->state = SI_ST_TAR;
618 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
619 return 0;
620 }
621 return 0;
622}
623
624/*
625 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200626 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
627 * SI_ST_INI) to SI_ST_EST, but only when a ->connect function is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100628 */
629void sess_establish(struct session *s, struct stream_interface *si)
630{
631 struct buffer *req = si->ob;
632 struct buffer *rep = si->ib;
633
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100634 if (s->srv)
635 health_adjust(s->srv, HANA_STATUS_L4_OK);
636
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100637 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100638 /* if the user wants to log as soon as possible, without counting
639 * bytes from the server, then this is the right moment. */
640 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
641 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100642 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100643 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100644 }
645 else {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100646 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
647 /* reset hdr_idx which was already initialized by the request.
648 * right now, the http parser does it.
649 * hdr_idx_init(&s->txn.hdr_idx);
650 */
651 }
652
Willy Tarreau4e5b8282009-08-16 22:57:50 +0200653 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100654 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
Willy Tarreaud04e8582010-05-31 12:31:35 +0200655 if (si->connect) {
656 /* real connections have timeouts */
657 req->wto = s->be->timeout.server;
658 rep->rto = s->be->timeout.server;
659 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100660 req->wex = TICK_ETERNITY;
661}
662
663/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
664 * Other input states are simply ignored.
665 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
666 * Flags must have previously been updated for timeouts and other conditions.
667 */
668void sess_update_stream_int(struct session *s, struct stream_interface *si)
669{
670 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",
671 now_ms, __FUNCTION__,
672 s,
673 s->req, s->rep,
674 s->req->rex, s->rep->wex,
675 s->req->flags, s->rep->flags,
676 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
677
678 if (si->state == SI_ST_ASS) {
679 /* Server assigned to connection request, we have to try to connect now */
680 int conn_err;
681
682 conn_err = connect_server(s);
683 if (conn_err == SN_ERR_NONE) {
684 /* state = SI_ST_CON now */
Willy Tarreau8f6457c2008-12-01 00:08:28 +0100685 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100686 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100687 return;
688 }
689
690 /* We have received a synchronous error. We might have to
691 * abort, retry immediately or redispatch.
692 */
693 if (conn_err == SN_ERR_INTERNAL) {
694 if (!si->err_type) {
695 si->err_type = SI_ET_CONN_OTHER;
696 si->err_loc = s->srv;
697 }
698
699 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100700 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100701 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200702 s->srv->counters.failed_conns++;
703 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100704
705 /* release other sessions waiting for this server */
706 if (may_dequeue_tasks(s->srv, s->be))
707 process_srv_queue(s->srv);
708
709 /* Failed and not retryable. */
710 si->shutr(si);
711 si->shutw(si);
712 si->ob->flags |= BF_WRITE_ERROR;
713
714 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
715
716 /* no session was ever accounted for this server */
717 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100718 if (s->srv_error)
719 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100720 return;
721 }
722
723 /* We are facing a retryable error, but we don't want to run a
724 * turn-around now, as the problem is likely a source port
725 * allocation problem, so we want to retry now.
726 */
727 si->state = SI_ST_CER;
728 si->flags &= ~SI_FL_ERR;
729 sess_update_st_cer(s, si);
730 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
731 return;
732 }
733 else if (si->state == SI_ST_QUE) {
734 /* connection request was queued, check for any update */
735 if (!s->pend_pos) {
736 /* The connection is not in the queue anymore. Either
737 * we have a server connection slot available and we
738 * go directly to the assigned state, or we need to
739 * load-balance first and go to the INI state.
740 */
741 si->exp = TICK_ETERNITY;
742 if (unlikely(!(s->flags & SN_ASSIGNED)))
743 si->state = SI_ST_REQ;
744 else {
745 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
746 si->state = SI_ST_ASS;
747 }
748 return;
749 }
750
751 /* Connection request still in queue... */
752 if (si->flags & SI_FL_EXP) {
753 /* ... and timeout expired */
754 si->exp = TICK_ETERNITY;
755 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
756 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200757 s->srv->counters.failed_conns++;
758 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100759 si->shutr(si);
760 si->shutw(si);
761 si->ob->flags |= BF_WRITE_TIMEOUT;
762 if (!si->err_type)
763 si->err_type = SI_ET_QUEUE_TO;
764 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100765 if (s->srv_error)
766 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100767 return;
768 }
769
770 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau418fd472009-09-06 21:37:23 +0200771 if ((si->ob->flags & (BF_READ_ERROR)) ||
772 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200773 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100774 /* give up */
775 si->exp = TICK_ETERNITY;
776 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
777 si->shutr(si);
778 si->shutw(si);
779 si->err_type |= SI_ET_QUEUE_ABRT;
780 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100781 if (s->srv_error)
782 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100783 return;
784 }
785
786 /* Nothing changed */
787 return;
788 }
789 else if (si->state == SI_ST_TAR) {
790 /* Connection request might be aborted */
Willy Tarreau418fd472009-09-06 21:37:23 +0200791 if ((si->ob->flags & (BF_READ_ERROR)) ||
792 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200793 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100794 /* give up */
795 si->exp = TICK_ETERNITY;
796 si->shutr(si);
797 si->shutw(si);
798 si->err_type |= SI_ET_CONN_ABRT;
799 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100800 if (s->srv_error)
801 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100802 return;
803 }
804
805 if (!(si->flags & SI_FL_EXP))
806 return; /* still in turn-around */
807
808 si->exp = TICK_ETERNITY;
809
810 /* we keep trying on the same server as long as the session is
811 * marked "assigned".
812 * FIXME: Should we force a redispatch attempt when the server is down ?
813 */
814 if (s->flags & SN_ASSIGNED)
815 si->state = SI_ST_ASS;
816 else
817 si->state = SI_ST_REQ;
818 return;
819 }
820}
821
822/* This function initiates a server connection request on a stream interface
823 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
824 * indicating that a server has been assigned. It may also return SI_ST_QUE,
825 * or SI_ST_CLO upon error.
826 */
827static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
828 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",
829 now_ms, __FUNCTION__,
830 s,
831 s->req, s->rep,
832 s->req->rex, s->rep->wex,
833 s->req->flags, s->rep->flags,
834 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
835
836 if (si->state != SI_ST_REQ)
837 return;
838
839 /* Try to assign a server */
840 if (srv_redispatch_connect(s) != 0) {
841 /* We did not get a server. Either we queued the
842 * connection request, or we encountered an error.
843 */
844 if (si->state == SI_ST_QUE)
845 return;
846
847 /* we did not get any server, let's check the cause */
848 si->shutr(si);
849 si->shutw(si);
850 si->ob->flags |= BF_WRITE_ERROR;
851 if (!si->err_type)
852 si->err_type = SI_ET_CONN_OTHER;
853 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100854 if (s->srv_error)
855 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100856 return;
857 }
858
859 /* The server is assigned */
860 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
861 si->state = SI_ST_ASS;
862}
863
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200864/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +0100865 * if appropriate. The default_backend rule is also considered, then the
866 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200867 * It returns 1 if the processing can continue on next analysers, or zero if it
868 * either needs more data or wants to immediately abort the request.
869 */
870int process_switching_rules(struct session *s, struct buffer *req, int an_bit)
871{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200872 struct persist_rule *prst_rule;
Willy Tarreau4de91492010-01-22 19:10:05 +0100873
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200874 req->analysers &= ~an_bit;
875 req->analyse_exp = TICK_ETERNITY;
876
877 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
878 now_ms, __FUNCTION__,
879 s,
880 req,
881 req->rex, req->wex,
882 req->flags,
883 req->l,
884 req->analysers);
885
886 /* now check whether we have some switching rules for this request */
887 if (!(s->flags & SN_BE_ASSIGNED)) {
888 struct switching_rule *rule;
889
890 list_for_each_entry(rule, &s->fe->switching_rules, list) {
891 int ret;
892
893 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ);
894 ret = acl_pass(ret);
895 if (rule->cond->pol == ACL_COND_UNLESS)
896 ret = !ret;
897
898 if (ret) {
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200899 if (!session_set_backend(s, rule->be.backend))
900 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200901 break;
902 }
903 }
904
905 /* To ensure correct connection accounting on the backend, we
906 * have to assign one if it was not set (eg: a listen). This
907 * measure also takes care of correctly setting the default
908 * backend if any.
909 */
910 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200911 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
912 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200913 }
914
Willy Tarreaufb356202010-08-03 14:02:05 +0200915 /* we don't want to run the TCP or HTTP filters again if the backend has not changed */
916 if (s->fe == s->be) {
917 s->req->analysers &= ~AN_REQ_INSPECT_BE;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200918 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +0200919 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200920
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200921 /* 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 +0100922 * persistence rule, and report that in the session.
923 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200924 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +0100925 int ret = 1;
926
927 if (prst_rule->cond) {
928 ret = acl_exec_cond(prst_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
929 ret = acl_pass(ret);
930 if (prst_rule->cond->pol == ACL_COND_UNLESS)
931 ret = !ret;
932 }
933
934 if (ret) {
935 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200936 if (prst_rule->type == PERSIST_TYPE_FORCE) {
937 s->flags |= SN_FORCE_PRST;
938 } else {
939 s->flags |= SN_IGNORE_PRST;
940 }
Willy Tarreau4de91492010-01-22 19:10:05 +0100941 break;
942 }
943 }
944
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200945 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200946
947 sw_failed:
948 /* immediately abort this request in case of allocation failure */
949 buffer_abort(s->req);
950 buffer_abort(s->rep);
951
952 if (!(s->flags & SN_ERR_MASK))
953 s->flags |= SN_ERR_RESOURCE;
954 if (!(s->flags & SN_FINST_MASK))
955 s->flags |= SN_FINST_R;
956
957 s->txn.status = 500;
958 s->req->analysers = 0;
959 s->req->analyse_exp = TICK_ETERNITY;
960 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200961}
962
Emeric Brun1d33b292010-01-04 15:47:17 +0100963/* This stream analyser works on a request. It applies all sticking rules on
964 * it then returns 1. The data must already be present in the buffer otherwise
965 * they won't match. It always returns 1.
966 */
967int process_sticking_rules(struct session *s, struct buffer *req, int an_bit)
968{
969 struct proxy *px = s->be;
970 struct sticking_rule *rule;
971
972 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
973 now_ms, __FUNCTION__,
974 s,
975 req,
976 req->rex, req->wex,
977 req->flags,
978 req->l,
979 req->analysers);
980
981 list_for_each_entry(rule, &px->sticking_rules, list) {
982 int ret = 1 ;
983 int i;
984
985 for (i = 0; i < s->store_count; i++) {
986 if (rule->table.t == s->store[i].table)
987 break;
988 }
989
990 if (i != s->store_count)
991 continue;
992
993 if (rule->cond) {
994 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_REQ);
995 ret = acl_pass(ret);
996 if (rule->cond->pol == ACL_COND_UNLESS)
997 ret = !ret;
998 }
999
1000 if (ret) {
1001 struct stktable_key *key;
1002
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001003 key = stktable_fetch_key(px, s, &s->txn, PATTERN_FETCH_REQ, rule->expr, rule->table.t->type);
Emeric Brun1d33b292010-01-04 15:47:17 +01001004 if (!key)
1005 continue;
1006
1007 if (rule->flags & STK_IS_MATCH) {
1008 struct stksess *ts;
1009
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001010 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001011 if (!(s->flags & SN_ASSIGNED)) {
1012 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001013 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001014
1015 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001016 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1017 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001018 if (node) {
1019 struct server *srv;
1020
1021 srv = container_of(node, struct server, conf.id);
Willy Tarreau4de91492010-01-22 19:10:05 +01001022 if ((srv->state & SRV_RUNNING) ||
1023 (px->options & PR_O_PERSIST) ||
1024 (s->flags & SN_FORCE_PRST)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001025 s->flags |= SN_DIRECT | SN_ASSIGNED;
1026 s->srv = srv;
1027 }
1028 }
1029 }
Willy Tarreaue8f63382010-07-13 15:20:24 +02001030 stktable_touch(rule->table.t, ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001031 }
1032 }
1033 if (rule->flags & STK_IS_STORE) {
1034 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1035 struct stksess *ts;
1036
1037 ts = stksess_new(rule->table.t, key);
1038 if (ts) {
1039 s->store[s->store_count].table = rule->table.t;
1040 s->store[s->store_count++].ts = ts;
1041 }
1042 }
1043 }
1044 }
1045 }
1046
1047 req->analysers &= ~an_bit;
1048 req->analyse_exp = TICK_ETERNITY;
1049 return 1;
1050}
1051
1052/* This stream analyser works on a response. It applies all store rules on it
1053 * then returns 1. The data must already be present in the buffer otherwise
1054 * they won't match. It always returns 1.
1055 */
1056int process_store_rules(struct session *s, struct buffer *rep, int an_bit)
1057{
1058 struct proxy *px = s->be;
1059 struct sticking_rule *rule;
1060 int i;
1061
1062 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1063 now_ms, __FUNCTION__,
1064 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001065 rep,
1066 rep->rex, rep->wex,
1067 rep->flags,
1068 rep->l,
1069 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001070
1071 list_for_each_entry(rule, &px->storersp_rules, list) {
1072 int ret = 1 ;
1073 int storereqidx = -1;
1074
1075 for (i = 0; i < s->store_count; i++) {
1076 if (rule->table.t == s->store[i].table) {
1077 if (!(s->store[i].flags))
1078 storereqidx = i;
1079 break;
1080 }
1081 }
1082
1083 if ((i != s->store_count) && (storereqidx == -1))
1084 continue;
1085
1086 if (rule->cond) {
1087 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_RTR);
1088 ret = acl_pass(ret);
1089 if (rule->cond->pol == ACL_COND_UNLESS)
1090 ret = !ret;
1091 }
1092
1093 if (ret) {
1094 struct stktable_key *key;
1095
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001096 key = stktable_fetch_key(px, s, &s->txn, PATTERN_FETCH_RTR, rule->expr, rule->table.t->type);
Emeric Brun1d33b292010-01-04 15:47:17 +01001097 if (!key)
1098 continue;
1099
1100 if (storereqidx != -1) {
Willy Tarreau393379c2010-06-06 12:11:37 +02001101 stksess_setkey(s->store[storereqidx].table, s->store[storereqidx].ts, key);
Emeric Brun1d33b292010-01-04 15:47:17 +01001102 s->store[storereqidx].flags = 1;
1103 }
1104 else if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1105 struct stksess *ts;
1106
1107 ts = stksess_new(rule->table.t, key);
1108 if (ts) {
1109 s->store[s->store_count].table = rule->table.t;
1110 s->store[s->store_count].flags = 1;
1111 s->store[s->store_count++].ts = ts;
1112 }
1113 }
1114 }
1115 }
1116
1117 /* process store request and store response */
1118 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001119 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001120 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001121
1122 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1123 if (ts) {
1124 /* the entry already existed, we can free ours */
Willy Tarreaue8f63382010-07-13 15:20:24 +02001125 stktable_touch(s->store[i].table, ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001126 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001127 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001128 else
1129 ts = stktable_store(s->store[i].table, s->store[i].ts);
1130
1131 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001132 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
1133 stktable_data_cast(ptr, server_id) = s->srv->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001134 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001135 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001136
1137 rep->analysers &= ~an_bit;
1138 rep->analyse_exp = TICK_ETERNITY;
1139 return 1;
1140}
1141
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001142/* This macro is very specific to the function below. See the comments in
1143 * process_session() below to understand the logic and the tests.
1144 */
1145#define UPDATE_ANALYSERS(real, list, back, flag) { \
1146 list = (((list) & ~(flag)) | ~(back)) & (real); \
1147 back = real; \
1148 if (!(list)) \
1149 break; \
1150 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1151 continue; \
1152}
1153
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001154/* Processes the client, server, request and response jobs of a session task,
1155 * then puts it back to the wait queue in a clean state, or cleans up its
1156 * resources if it must be deleted. Returns in <next> the date the task wants
1157 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1158 * nothing too many times, the request and response buffers flags are monitored
1159 * and each function is called only if at least another function has changed at
1160 * least one flag it is interested in.
1161 */
Willy Tarreau26c25062009-03-08 09:38:41 +01001162struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001163{
1164 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001165 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001166 unsigned int rq_prod_last, rq_cons_last;
1167 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001168 unsigned int req_ana_back;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001169
1170 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
1171 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
1172
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001173 /* this data may be no longer valid, clear it */
1174 memset(&s->txn.auth, 0, sizeof(s->txn.auth));
1175
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001176 /* This flag must explicitly be set every time */
1177 s->req->flags &= ~BF_READ_NOEXP;
1178
1179 /* Keep a copy of req/rep flags so that we can detect shutdowns */
1180 rqf_last = s->req->flags;
1181 rpf_last = s->rep->flags;
1182
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001183 /* we don't want the stream interface functions to recursively wake us up */
1184 if (s->req->prod->owner == t)
1185 s->req->prod->flags |= SI_FL_DONT_WAKE;
1186 if (s->req->cons->owner == t)
1187 s->req->cons->flags |= SI_FL_DONT_WAKE;
1188
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001189 /* 1a: Check for low level timeouts if needed. We just set a flag on
1190 * stream interfaces when their timeouts have expired.
1191 */
1192 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
1193 stream_int_check_timeouts(&s->si[0]);
1194 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001195
1196 /* check buffer timeouts, and close the corresponding stream interfaces
1197 * for future reads or writes. Note: this will also concern upper layers
1198 * but we do not touch any other flag. We must be careful and correctly
1199 * detect state changes when calling them.
1200 */
1201
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001202 buffer_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001203
Willy Tarreau14641402009-12-29 14:49:56 +01001204 if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1205 s->req->cons->flags |= SI_FL_NOLINGER;
1206 s->req->cons->shutw(s->req->cons);
1207 }
1208
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001209 if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1210 s->req->prod->shutr(s->req->prod);
1211
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001212 buffer_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001213
Willy Tarreau14641402009-12-29 14:49:56 +01001214 if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1215 s->rep->cons->flags |= SI_FL_NOLINGER;
1216 s->rep->cons->shutw(s->rep->cons);
1217 }
1218
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001219 if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1220 s->rep->prod->shutr(s->rep->prod);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001221 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001222
1223 /* 1b: check for low-level errors reported at the stream interface.
1224 * First we check if it's a retryable error (in which case we don't
1225 * want to tell the buffer). Otherwise we report the error one level
1226 * upper by setting flags into the buffers. Note that the side towards
1227 * the client cannot have connect (hence retryable) errors. Also, the
1228 * connection setup code must be able to deal with any type of abort.
1229 */
1230 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
1231 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
1232 s->si[0].shutr(&s->si[0]);
1233 s->si[0].shutw(&s->si[0]);
1234 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001235 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreauae526782010-03-04 20:34:23 +01001236 s->be->counters.cli_aborts++;
1237 if (s->srv)
1238 s->srv->counters.cli_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001239 if (!(s->flags & SN_ERR_MASK))
1240 s->flags |= SN_ERR_CLICL;
1241 if (!(s->flags & SN_FINST_MASK))
1242 s->flags |= SN_FINST_D;
1243 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001244 }
1245 }
1246
1247 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
1248 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
1249 s->si[1].shutr(&s->si[1]);
1250 s->si[1].shutw(&s->si[1]);
1251 stream_int_report_error(&s->si[1]);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001252 s->be->counters.failed_resp++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001253 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001254 s->srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001255 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreauae526782010-03-04 20:34:23 +01001256 s->be->counters.srv_aborts++;
1257 if (s->srv)
1258 s->srv->counters.srv_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001259 if (!(s->flags & SN_ERR_MASK))
1260 s->flags |= SN_ERR_SRVCL;
1261 if (!(s->flags & SN_FINST_MASK))
1262 s->flags |= SN_FINST_D;
1263 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001264 }
1265 /* note: maybe we should process connection errors here ? */
1266 }
1267
1268 if (s->si[1].state == SI_ST_CON) {
1269 /* we were trying to establish a connection on the server side,
1270 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1271 */
1272 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
1273 sess_update_st_cer(s, &s->si[1]);
1274 else if (s->si[1].state == SI_ST_EST)
1275 sess_establish(s, &s->si[1]);
1276
1277 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1278 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1279 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1280 */
1281 }
1282
Willy Tarreau815a9b22010-07-27 17:15:12 +02001283 rq_prod_last = s->si[0].state;
1284 rq_cons_last = s->si[1].state;
1285 rp_cons_last = s->si[0].state;
1286 rp_prod_last = s->si[1].state;
1287
1288 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001289 /* Check for connection closure */
1290
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001291 DPRINTF(stderr,
1292 "[%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",
1293 now_ms, __FUNCTION__, __LINE__,
1294 t,
1295 s, s->flags,
1296 s->req, s->rep,
1297 s->req->rex, s->rep->wex,
1298 s->req->flags, s->rep->flags,
1299 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
1300 s->rep->cons->err_type, s->req->cons->err_type,
Willy Tarreauee28de02010-06-01 09:51:00 +02001301 s->req->cons->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001302
1303 /* nothing special to be done on client side */
1304 if (unlikely(s->req->prod->state == SI_ST_DIS))
1305 s->req->prod->state = SI_ST_CLO;
1306
1307 /* When a server-side connection is released, we have to count it and
1308 * check for pending connections on this server.
1309 */
1310 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
1311 s->req->cons->state = SI_ST_CLO;
1312 if (s->srv) {
1313 if (s->flags & SN_CURR_SESS) {
1314 s->flags &= ~SN_CURR_SESS;
1315 s->srv->cur_sess--;
1316 }
1317 sess_change_server(s, NULL);
1318 if (may_dequeue_tasks(s->srv, s->be))
1319 process_srv_queue(s->srv);
1320 }
1321 }
1322
1323 /*
1324 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1325 * at this point.
1326 */
1327
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001328 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001329 /* Analyse request */
1330 if ((s->req->flags & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001331 ((s->req->flags ^ rqf_last) & BF_MASK_STATIC) ||
1332 s->si[0].state != rq_prod_last ||
1333 s->si[1].state != rq_cons_last) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001334 unsigned int flags = s->req->flags;
1335
1336 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001337 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001338 unsigned int ana_list;
1339 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001340
Willy Tarreau90deb182010-01-07 00:20:41 +01001341 /* it's up to the analysers to stop new connections,
1342 * disable reading or closing. Note: if an analyser
1343 * disables any of these bits, it is responsible for
1344 * enabling them again when it disables itself, so
1345 * that other analysers are called in similar conditions.
1346 */
1347 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001348 buffer_auto_connect(s->req);
1349 buffer_auto_close(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001350
1351 /* We will call all analysers for which a bit is set in
1352 * s->req->analysers, following the bit order from LSB
1353 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001354 * the list when not needed. Any analyser may return 0
1355 * to break out of the loop, either because of missing
1356 * data to take a decision, or because it decides to
1357 * kill the session. We loop at least once through each
1358 * analyser, and we may loop again if other analysers
1359 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001360 *
1361 * We build a list of analysers to run. We evaluate all
1362 * of these analysers in the order of the lower bit to
1363 * the higher bit. This ordering is very important.
1364 * An analyser will often add/remove other analysers,
1365 * including itself. Any changes to itself have no effect
1366 * on the loop. If it removes any other analysers, we
1367 * want those analysers not to be called anymore during
1368 * this loop. If it adds an analyser that is located
1369 * after itself, we want it to be scheduled for being
1370 * processed during the loop. If it adds an analyser
1371 * which is located before it, we want it to switch to
1372 * it immediately, even if it has already been called
1373 * once but removed since.
1374 *
1375 * In order to achieve this, we compare the analyser
1376 * list after the call with a copy of it before the
1377 * call. The work list is fed with analyser bits that
1378 * appeared during the call. Then we compare previous
1379 * work list with the new one, and check the bits that
1380 * appeared. If the lowest of these bits is lower than
1381 * the current bit, it means we have enabled a previous
1382 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001383 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001384
1385 ana_list = ana_back = s->req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001386 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001387 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001388
Willy Tarreaufb356202010-08-03 14:02:05 +02001389 if (ana_list & AN_REQ_INSPECT_FE) {
1390 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001391 break;
Willy Tarreaufb356202010-08-03 14:02:05 +02001392 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001393 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001394
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001395 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001396 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001397 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001398 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001399 }
1400
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001401 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001402 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
1403 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001404 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001405 }
1406
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001407 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001408 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
1409 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001410 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001411 }
1412
Willy Tarreaufb356202010-08-03 14:02:05 +02001413 if (ana_list & AN_REQ_INSPECT_BE) {
1414 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_BE))
1415 break;
1416 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
1417 }
1418
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001419 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001420 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
1421 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001422 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001423 }
1424
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001425 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001426 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001427 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001428 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001429 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001430
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001431 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreauc465fd72009-08-31 00:17:18 +02001432 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
1433 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001434 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001435 }
1436
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001437 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001438 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001439 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001440 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001441 }
Emeric Brun647caf12009-06-30 17:57:00 +02001442
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001443 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Emeric Brun647caf12009-06-30 17:57:00 +02001444 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
1445 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001446 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001447 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001448
Emeric Brun1d33b292010-01-04 15:47:17 +01001449 if (ana_list & AN_REQ_STICKING_RULES) {
1450 if (!process_sticking_rules(s, s->req, AN_REQ_STICKING_RULES))
1451 break;
1452 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
1453 }
1454
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001455 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001456 if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY))
1457 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001458 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001459 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001460 break;
1461 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001462 }
Willy Tarreau84455332009-03-15 22:34:05 +01001463
Willy Tarreau815a9b22010-07-27 17:15:12 +02001464 rq_prod_last = s->si[0].state;
1465 rq_cons_last = s->si[1].state;
1466 rqf_last = s->req->flags;
1467
1468 if ((s->req->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001469 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001470 }
1471
Willy Tarreau576507f2010-01-07 00:09:04 +01001472 /* we'll monitor the request analysers while parsing the response,
1473 * because some response analysers may indirectly enable new request
1474 * analysers (eg: HTTP keep-alive).
1475 */
1476 req_ana_back = s->req->analysers;
1477
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001478 resync_response:
1479 /* Analyse response */
1480
1481 if (unlikely(s->rep->flags & BF_HIJACK)) {
1482 /* In inject mode, we wake up everytime something has
1483 * happened on the write side of the buffer.
1484 */
1485 unsigned int flags = s->rep->flags;
1486
1487 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
1488 !(s->rep->flags & BF_FULL)) {
1489 s->rep->hijacker(s, s->rep);
1490 }
1491
1492 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
1493 rpf_last = s->rep->flags;
1494 goto resync_response;
1495 }
1496 }
1497 else if ((s->rep->flags & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001498 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC ||
1499 s->si[0].state != rp_cons_last ||
1500 s->si[1].state != rp_prod_last) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001501 unsigned int flags = s->rep->flags;
1502
1503 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001504 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001505 unsigned int ana_list;
1506 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001507
Willy Tarreau90deb182010-01-07 00:20:41 +01001508 /* it's up to the analysers to stop disable reading or
1509 * closing. Note: if an analyser disables any of these
1510 * bits, it is responsible for enabling them again when
1511 * it disables itself, so that other analysers are called
1512 * in similar conditions.
1513 */
1514 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001515 buffer_auto_close(s->rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001516
1517 /* We will call all analysers for which a bit is set in
1518 * s->rep->analysers, following the bit order from LSB
1519 * to MSB. The analysers must remove themselves from
1520 * the list when not needed. Any analyser may return 0
1521 * to break out of the loop, either because of missing
1522 * data to take a decision, or because it decides to
1523 * kill the session. We loop at least once through each
1524 * analyser, and we may loop again if other analysers
1525 * are added in the middle.
1526 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001527
1528 ana_list = ana_back = s->rep->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001529 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001530 /* Warning! ensure that analysers are always placed in ascending order! */
1531
1532 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001533 if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP))
1534 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001535 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001536 }
1537
Emeric Brun1d33b292010-01-04 15:47:17 +01001538 if (ana_list & AN_RES_STORE_RULES) {
1539 if (!process_store_rules(s, s->rep, AN_RES_STORE_RULES))
1540 break;
1541 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
1542 }
1543
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001544 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001545 if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be))
1546 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001547 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001548 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001549
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001550 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001551 if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY))
1552 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001553 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001554 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001555 break;
1556 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001557 }
1558
Willy Tarreau815a9b22010-07-27 17:15:12 +02001559 rp_cons_last = s->si[0].state;
1560 rp_prod_last = s->si[1].state;
1561 rpf_last = s->rep->flags;
1562
1563 if ((s->rep->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001564 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001565 }
1566
Willy Tarreau576507f2010-01-07 00:09:04 +01001567 /* maybe someone has added some request analysers, so we must check and loop */
1568 if (s->req->analysers & ~req_ana_back)
1569 goto resync_request;
1570
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001571 /* FIXME: here we should call protocol handlers which rely on
1572 * both buffers.
1573 */
1574
1575
1576 /*
Willy Tarreauae526782010-03-04 20:34:23 +01001577 * Now we propagate unhandled errors to the session. Normally
1578 * we're just in a data phase here since it means we have not
1579 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001580 */
1581 if (!(s->flags & SN_ERR_MASK)) {
1582 if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1583 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +01001584 s->req->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001585 if (s->req->flags & BF_READ_ERROR) {
1586 s->be->counters.cli_aborts++;
1587 if (s->srv)
1588 s->srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001589 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001590 }
1591 else if (s->req->flags & BF_READ_TIMEOUT) {
1592 s->be->counters.cli_aborts++;
1593 if (s->srv)
1594 s->srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001595 s->flags |= SN_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001596 }
1597 else if (s->req->flags & BF_WRITE_ERROR) {
1598 s->be->counters.srv_aborts++;
1599 if (s->srv)
1600 s->srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001601 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001602 }
1603 else {
1604 s->be->counters.srv_aborts++;
1605 if (s->srv)
1606 s->srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001607 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001608 }
Willy Tarreau84455332009-03-15 22:34:05 +01001609 sess_set_term_flags(s);
1610 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001611 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1612 /* Report it if the server got an error or a read timeout expired */
1613 s->rep->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001614 if (s->rep->flags & BF_READ_ERROR) {
1615 s->be->counters.srv_aborts++;
1616 if (s->srv)
1617 s->srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001618 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001619 }
1620 else if (s->rep->flags & BF_READ_TIMEOUT) {
1621 s->be->counters.srv_aborts++;
1622 if (s->srv)
1623 s->srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001624 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001625 }
1626 else if (s->rep->flags & BF_WRITE_ERROR) {
1627 s->be->counters.cli_aborts++;
1628 if (s->srv)
1629 s->srv->counters.cli_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001630 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001631 }
1632 else {
1633 s->be->counters.cli_aborts++;
1634 if (s->srv)
1635 s->srv->counters.cli_aborts++;
1636 s->flags |= SN_ERR_CLITO;
1637 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001638 sess_set_term_flags(s);
1639 }
Willy Tarreau84455332009-03-15 22:34:05 +01001640 }
1641
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001642 /*
1643 * Here we take care of forwarding unhandled data. This also includes
1644 * connection establishments and shutdown requests.
1645 */
1646
1647
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001648 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001649 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001650 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001651 if (!s->req->analysers &&
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001652 !(s->req->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTW_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001653 (s->req->prod->state >= SI_ST_EST) &&
1654 (s->req->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001655 /* This buffer is freewheeling, there's no analyser nor hijacker
1656 * attached to it. If any data are left in, we'll permit them to
1657 * move.
1658 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001659 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001660 buffer_auto_connect(s->req);
1661 buffer_auto_close(s->req);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001662 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001663
Willy Tarreau31971e52009-09-20 12:07:52 +02001664 /* If the producer is still connected, we'll enable data to flow
1665 * from the producer to the consumer (which might possibly not be
1666 * connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001667 */
Willy Tarreau31971e52009-09-20 12:07:52 +02001668 if (!(s->req->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW)))
1669 buffer_forward(s->req, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001670 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001671
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001672 /* check if it is wise to enable kernel splicing to forward request data */
1673 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1674 s->req->to_forward &&
1675 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001676 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001677 (pipes_used < global.maxpipes) &&
1678 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
1679 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1680 (s->req->flags & BF_STREAMER_FAST)))) {
1681 s->req->flags |= BF_KERN_SPLICING;
1682 }
1683
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001684 /* reflect what the L7 analysers have seen last */
1685 rqf_last = s->req->flags;
1686
1687 /*
1688 * Now forward all shutdown requests between both sides of the buffer
1689 */
1690
Willy Tarreau520d95e2009-09-19 21:04:57 +02001691 /* first, let's check if the request buffer needs to shutdown(write), which may
1692 * happen either because the input is closed or because we want to force a close
Willy Tarreaue4599762010-03-21 23:25:09 +01001693 * once the server has begun to respond.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001694 */
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001695 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
Willy Tarreaue4599762010-03-21 23:25:09 +01001696 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001697 buffer_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001698
1699 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001700 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 +01001701 s->req->cons->shutw(s->req->cons);
1702
1703 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001704 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
1705 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001706 buffer_shutr_now(s->req);
1707
1708 /* shutdown(read) pending */
1709 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1710 s->req->prod->shutr(s->req->prod);
1711
Willy Tarreau520d95e2009-09-19 21:04:57 +02001712 /* it's possible that an upper layer has requested a connection setup or abort.
1713 * There are 2 situations where we decide to establish a new connection :
1714 * - there are data scheduled for emission in the buffer
1715 * - the BF_AUTO_CONNECT flag is set (active connection)
1716 */
1717 if (s->req->cons->state == SI_ST_INI) {
Willy Tarreaue4599762010-03-21 23:25:09 +01001718 if (!(s->req->flags & BF_SHUTW)) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001719 if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) {
Willy Tarreau85e7d002010-05-31 11:57:51 +02001720 /* If we have an iohandler without a connect method, we immediately
1721 * switch to the connected state, otherwise we perform a connection
1722 * request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001723 */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001724 s->req->cons->state = SI_ST_REQ; /* new connection requested */
Willy Tarreau070ceb62010-06-01 10:36:43 +02001725 s->req->cons->conn_retries = s->be->conn_retries;
Willy Tarreau85e7d002010-05-31 11:57:51 +02001726 if (unlikely(s->req->cons->iohandler && !s->req->cons->connect)) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02001727 s->req->cons->state = SI_ST_EST; /* connection established */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001728 s->rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
1729 s->req->wex = TICK_ETERNITY;
1730 }
Willy Tarreau520d95e2009-09-19 21:04:57 +02001731 }
Willy Tarreau73201222009-08-16 18:27:24 +02001732 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001733 else {
Willy Tarreau92795622009-03-06 12:51:23 +01001734 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001735 buffer_shutw_now(s->req); /* fix buffer flags upon abort */
1736 buffer_shutr_now(s->rep);
1737 }
Willy Tarreau92795622009-03-06 12:51:23 +01001738 }
1739
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001740
1741 /* we may have a pending connection request, or a connection waiting
1742 * for completion.
1743 */
1744 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
1745 do {
1746 /* nb: step 1 might switch from QUE to ASS, but we first want
1747 * to give a chance to step 2 to perform a redirect if needed.
1748 */
1749 if (s->si[1].state != SI_ST_REQ)
1750 sess_update_stream_int(s, &s->si[1]);
1751 if (s->si[1].state == SI_ST_REQ)
1752 sess_prepare_conn_req(s, &s->si[1]);
1753
1754 if (s->si[1].state == SI_ST_ASS && s->srv &&
1755 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
1756 perform_http_redirect(s, &s->si[1]);
1757 } while (s->si[1].state == SI_ST_ASS);
1758 }
1759
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001760 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001761 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001762 goto resync_stream_interface;
1763
Willy Tarreau815a9b22010-07-27 17:15:12 +02001764 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001765 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001766 goto resync_request;
1767
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001768 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01001769
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001770 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001771 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001772 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001773 if (!s->rep->analysers &&
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001774 !(s->rep->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTW_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001775 (s->rep->prod->state >= SI_ST_EST) &&
1776 (s->rep->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001777 /* This buffer is freewheeling, there's no analyser nor hijacker
1778 * attached to it. If any data are left in, we'll permit them to
1779 * move.
1780 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001781 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001782 buffer_auto_close(s->rep);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001783 buffer_flush(s->rep);
Willy Tarreau31971e52009-09-20 12:07:52 +02001784 if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW)))
1785 buffer_forward(s->rep, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001786 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001787
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001788 /* check if it is wise to enable kernel splicing to forward response data */
1789 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1790 s->rep->to_forward &&
1791 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001792 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001793 (pipes_used < global.maxpipes) &&
1794 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
1795 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1796 (s->rep->flags & BF_STREAMER_FAST)))) {
1797 s->rep->flags |= BF_KERN_SPLICING;
1798 }
1799
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001800 /* reflect what the L7 analysers have seen last */
1801 rpf_last = s->rep->flags;
1802
1803 /*
1804 * Now forward all shutdown requests between both sides of the buffer
1805 */
1806
1807 /*
1808 * FIXME: this is probably where we should produce error responses.
1809 */
1810
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001811 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001812 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
1813 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001814 buffer_shutw_now(s->rep);
1815
1816 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001817 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 +01001818 s->rep->cons->shutw(s->rep->cons);
1819
1820 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001821 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
1822 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001823 buffer_shutr_now(s->rep);
1824
1825 /* shutdown(read) pending */
1826 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1827 s->rep->prod->shutr(s->rep->prod);
1828
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001829 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001830 goto resync_stream_interface;
1831
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001832 if (s->req->flags != rqf_last)
1833 goto resync_request;
1834
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001835 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001836 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001837
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001838 /* we're interested in getting wakeups again */
1839 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
1840 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
1841
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001842 /* This is needed only when debugging is enabled, to indicate
1843 * client-side or server-side close. Please note that in the unlikely
1844 * event where both sides would close at once, the sequence is reported
1845 * on the server side first.
1846 */
1847 if (unlikely((global.mode & MODE_DEBUG) &&
1848 (!(global.mode & MODE_QUIET) ||
1849 (global.mode & MODE_VERBOSE)))) {
1850 int len;
1851
1852 if (s->si[1].state == SI_ST_CLO &&
1853 s->si[1].prev_state == SI_ST_EST) {
1854 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1855 s->uniq_id, s->be->id,
1856 (unsigned short)s->si[0].fd,
1857 (unsigned short)s->si[1].fd);
1858 write(1, trash, len);
1859 }
1860
1861 if (s->si[0].state == SI_ST_CLO &&
1862 s->si[0].prev_state == SI_ST_EST) {
1863 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1864 s->uniq_id, s->be->id,
1865 (unsigned short)s->si[0].fd,
1866 (unsigned short)s->si[1].fd);
1867 write(1, trash, len);
1868 }
1869 }
1870
1871 if (likely((s->rep->cons->state != SI_ST_CLO) ||
1872 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
1873
1874 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1875 session_process_counters(s);
1876
Willy Tarreau1accfc02009-09-05 20:57:35 +02001877 if (s->rep->cons->state == SI_ST_EST && !s->rep->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001878 s->rep->cons->update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001879
Willy Tarreau1accfc02009-09-05 20:57:35 +02001880 if (s->req->cons->state == SI_ST_EST && !s->req->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001881 s->req->cons->update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001882
Willy Tarreaua6eebb32010-06-04 11:40:20 +02001883 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL|BF_READ_ATTACHED);
1884 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 +01001885 s->si[0].prev_state = s->si[0].state;
1886 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01001887 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
1888 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001889
1890 /* Trick: if a request is being waiting for the server to respond,
1891 * and if we know the server can timeout, we don't want the timeout
1892 * to expire on the client side first, but we're still interested
1893 * in passing data from the client to the server (eg: POST). Thus,
1894 * we can cancel the client's request timeout if the server's
1895 * request timeout is set and the server has not yet sent a response.
1896 */
1897
Willy Tarreau520d95e2009-09-19 21:04:57 +02001898 if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01001899 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
1900 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001901 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01001902 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001903
Willy Tarreau7a20aa62010-07-13 16:30:45 +02001904 /* Call the stream interfaces' I/O handlers when embedded.
Willy Tarreau1accfc02009-09-05 20:57:35 +02001905 * Note that this one may wake the task up again.
1906 */
Willy Tarreau7a20aa62010-07-13 16:30:45 +02001907 if (s->req->cons->iohandler || s->rep->cons->iohandler) {
1908 if (s->req->cons->iohandler)
1909 s->req->cons->iohandler(s->req->cons);
1910 if (s->rep->cons->iohandler)
1911 s->rep->cons->iohandler(s->rep->cons);
Willy Tarreau1accfc02009-09-05 20:57:35 +02001912 if (task_in_rq(t)) {
1913 /* If we woke up, we don't want to requeue the
1914 * task to the wait queue, but rather requeue
1915 * it into the runqueue ASAP.
1916 */
1917 t->expire = TICK_ETERNITY;
1918 return t;
1919 }
1920 }
1921
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001922 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
1923 tick_first(s->rep->rex, s->rep->wex));
1924 if (s->req->analysers)
1925 t->expire = tick_first(t->expire, s->req->analyse_exp);
1926
1927 if (s->si[0].exp)
1928 t->expire = tick_first(t->expire, s->si[0].exp);
1929
1930 if (s->si[1].exp)
1931 t->expire = tick_first(t->expire, s->si[1].exp);
1932
1933#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01001934 fprintf(stderr,
1935 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
1936 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
1937 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
1938 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 +01001939#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001940
1941#ifdef DEBUG_DEV
1942 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01001943 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001944 ABORT_NOW();
1945#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01001946 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001947 }
1948
1949 s->fe->feconn--;
1950 if (s->flags & SN_BE_ASSIGNED)
1951 s->be->beconn--;
1952 actconn--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02001953 s->listener->nbconn--;
1954 if (s->listener->state == LI_FULL &&
1955 s->listener->nbconn < s->listener->maxconn) {
1956 /* we should reactivate the listener */
1957 EV_FD_SET(s->listener->fd, DIR_RD);
1958 s->listener->state = LI_READY;
1959 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001960
1961 if (unlikely((global.mode & MODE_DEBUG) &&
1962 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1963 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001964 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001965 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001966 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001967 write(1, trash, len);
1968 }
1969
1970 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
1971 session_process_counters(s);
1972
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001973 if (s->txn.status) {
1974 int n;
1975
1976 n = s->txn.status / 100;
1977 if (n < 1 || n > 5)
1978 n = 0;
1979
1980 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau24657792010-02-26 10:30:28 +01001981 s->fe->counters.fe.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001982
Willy Tarreau24657792010-02-26 10:30:28 +01001983 if ((s->flags & SN_BE_ASSIGNED) &&
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001984 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau24657792010-02-26 10:30:28 +01001985 s->be->counters.be.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001986 }
1987
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001988 /* let's do a final log if we need it */
1989 if (s->logs.logwait &&
1990 !(s->flags & SN_MONITOR) &&
1991 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001992 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001993 }
1994
1995 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001996 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01001997 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001998 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01001999 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002000}
2001
Willy Tarreau7c669d72008-06-20 15:04:11 +02002002/*
2003 * This function adjusts sess->srv_conn and maintains the previous and new
2004 * server's served session counts. Setting newsrv to NULL is enough to release
2005 * current connection slot. This function also notifies any LB algo which might
2006 * expect to be informed about any change in the number of active sessions on a
2007 * server.
2008 */
2009void sess_change_server(struct session *sess, struct server *newsrv)
2010{
2011 if (sess->srv_conn == newsrv)
2012 return;
2013
2014 if (sess->srv_conn) {
2015 sess->srv_conn->served--;
2016 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2017 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
2018 sess->srv_conn = NULL;
2019 }
2020
2021 if (newsrv) {
2022 newsrv->served++;
2023 if (newsrv->proxy->lbprm.server_take_conn)
2024 newsrv->proxy->lbprm.server_take_conn(newsrv);
2025 sess->srv_conn = newsrv;
2026 }
2027}
2028
Willy Tarreau84455332009-03-15 22:34:05 +01002029/* Set correct session termination flags in case no analyser has done it. It
2030 * also counts a failed request if the server state has not reached the request
2031 * stage.
2032 */
2033void sess_set_term_flags(struct session *s)
2034{
2035 if (!(s->flags & SN_FINST_MASK)) {
2036 if (s->si[1].state < SI_ST_REQ) {
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002037
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002038 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002039 if (s->listener->counters)
2040 s->listener->counters->failed_req++;
2041
Willy Tarreau84455332009-03-15 22:34:05 +01002042 s->flags |= SN_FINST_R;
2043 }
2044 else if (s->si[1].state == SI_ST_QUE)
2045 s->flags |= SN_FINST_Q;
2046 else if (s->si[1].state < SI_ST_EST)
2047 s->flags |= SN_FINST_C;
Willy Tarreau033b2db2010-03-04 17:54:21 +01002048 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreau84455332009-03-15 22:34:05 +01002049 s->flags |= SN_FINST_D;
2050 else
2051 s->flags |= SN_FINST_L;
2052 }
2053}
2054
2055/* Handle server-side errors for default protocols. It is called whenever a a
2056 * connection setup is aborted or a request is aborted in queue. It sets the
2057 * session termination flags so that the caller does not have to worry about
2058 * them. It's installed as ->srv_error for the server-side stream_interface.
2059 */
2060void default_srv_error(struct session *s, struct stream_interface *si)
2061{
2062 int err_type = si->err_type;
2063 int err = 0, fin = 0;
2064
2065 if (err_type & SI_ET_QUEUE_ABRT) {
2066 err = SN_ERR_CLICL;
2067 fin = SN_FINST_Q;
2068 }
2069 else if (err_type & SI_ET_CONN_ABRT) {
2070 err = SN_ERR_CLICL;
2071 fin = SN_FINST_C;
2072 }
2073 else if (err_type & SI_ET_QUEUE_TO) {
2074 err = SN_ERR_SRVTO;
2075 fin = SN_FINST_Q;
2076 }
2077 else if (err_type & SI_ET_QUEUE_ERR) {
2078 err = SN_ERR_SRVCL;
2079 fin = SN_FINST_Q;
2080 }
2081 else if (err_type & SI_ET_CONN_TO) {
2082 err = SN_ERR_SRVTO;
2083 fin = SN_FINST_C;
2084 }
2085 else if (err_type & SI_ET_CONN_ERR) {
2086 err = SN_ERR_SRVCL;
2087 fin = SN_FINST_C;
2088 }
2089 else /* SI_ET_CONN_OTHER and others */ {
2090 err = SN_ERR_INTERNAL;
2091 fin = SN_FINST_C;
2092 }
2093
2094 if (!(s->flags & SN_ERR_MASK))
2095 s->flags |= err;
2096 if (!(s->flags & SN_FINST_MASK))
2097 s->flags |= fin;
2098}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002099
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002100
Willy Tarreau8b22a712010-06-18 17:46:06 +02002101/************************************************************************/
2102/* All supported ACL keywords must be declared here. */
2103/************************************************************************/
2104
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002105/* set test->i to the cumulated number of connections in the stksess entry <ts> */
2106static int
2107acl_fetch_conn_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2108{
2109 test->flags = ACL_TEST_F_VOL_TEST;
2110 test->i = 0;
2111 if (ts != NULL) {
2112 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CNT);
2113 if (!ptr)
2114 return 0; /* parameter not stored */
2115 test->i = stktable_data_cast(ptr, conn_cnt);
2116 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002117 return 1;
2118}
2119
2120/* set test->i to the cumulated number of connections from the session's tracked counters */
2121static int
2122acl_fetch_trk_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2123 struct acl_expr *expr, struct acl_test *test)
2124{
2125 if (!l4->tracked_counters)
2126 return 0;
2127
2128 return acl_fetch_conn_cnt(l4->tracked_table, test, l4->tracked_counters);
2129}
2130
2131/* set test->i to the cumulated number of connections from the session's source
2132 * address in the table pointed to by expr.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002133 */
2134static int
2135acl_fetch_src_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2136 struct acl_expr *expr, struct acl_test *test)
2137{
Willy Tarreau8b22a712010-06-18 17:46:06 +02002138 struct stktable_key *key;
2139
2140 key = tcpv4_src_to_stktable_key(l4);
2141 if (!key)
2142 return 0; /* only TCPv4 is supported right now */
2143
2144 if (expr->arg_len)
2145 px = find_stktable(expr->arg.str);
2146
2147 if (!px)
2148 return 0; /* table not found */
2149
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002150 return acl_fetch_conn_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau8b22a712010-06-18 17:46:06 +02002151}
2152
Willy Tarreau91c43d72010-06-20 11:19:22 +02002153/* set test->i to the connection rate in the stksess entry <ts> over the configured period */
2154static int
2155acl_fetch_conn_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2156{
2157 test->flags = ACL_TEST_F_VOL_TEST;
2158 test->i = 0;
2159 if (ts != NULL) {
2160 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_RATE);
2161 if (!ptr)
2162 return 0; /* parameter not stored */
2163 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
2164 table->data_arg[STKTABLE_DT_CONN_RATE].u);
2165 }
2166 return 1;
2167}
2168
2169/* set test->i to the connection rate from the session's tracked counters over
2170 * the configured period.
2171 */
2172static int
2173acl_fetch_trk_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2174 struct acl_expr *expr, struct acl_test *test)
2175{
2176 if (!l4->tracked_counters)
2177 return 0;
2178
2179 return acl_fetch_conn_rate(l4->tracked_table, test, l4->tracked_counters);
2180}
2181
2182/* set test->i to the connection rate from the session's source address in the
2183 * table pointed to by expr, over the configured period.
2184 */
2185static int
2186acl_fetch_src_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2187 struct acl_expr *expr, struct acl_test *test)
2188{
2189 struct stktable_key *key;
2190
2191 key = tcpv4_src_to_stktable_key(l4);
2192 if (!key)
2193 return 0; /* only TCPv4 is supported right now */
2194
2195 if (expr->arg_len)
2196 px = find_stktable(expr->arg.str);
2197
2198 if (!px)
2199 return 0; /* table not found */
2200
2201 return acl_fetch_conn_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2202}
2203
Willy Tarreau8b22a712010-06-18 17:46:06 +02002204/* set test->i to the number of connections from the session's source address
2205 * in the table pointed to by expr, after updating it.
2206 */
2207static int
2208acl_fetch_src_updt_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2209 struct acl_expr *expr, struct acl_test *test)
2210{
2211 struct stksess *ts;
2212 struct stktable_key *key;
2213 void *ptr;
2214
2215 key = tcpv4_src_to_stktable_key(l4);
2216 if (!key)
2217 return 0; /* only TCPv4 is supported right now */
2218
2219 if (expr->arg_len)
2220 px = find_stktable(expr->arg.str);
2221
2222 if (!px)
2223 return 0; /* table not found */
2224
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002225 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2226 /* entry does not exist and could not be created */
2227 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002228
2229 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2230 if (!ptr)
2231 return 0; /* parameter not stored in this table */
2232
2233 test->i = ++stktable_data_cast(ptr, conn_cnt);
2234 test->flags = ACL_TEST_F_VOL_TEST;
2235 return 1;
2236}
2237
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002238/* set test->i to the number of concurrent connections in the stksess entry <ts> */
2239static int
2240acl_fetch_conn_cur(struct stktable *table, struct acl_test *test, struct stksess *ts)
2241{
2242 test->flags = ACL_TEST_F_VOL_TEST;
2243 test->i = 0;
2244
2245 if (ts != NULL) {
2246 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CUR);
2247 if (!ptr)
2248 return 0; /* parameter not stored */
2249 test->i = stktable_data_cast(ptr, conn_cur);
2250 }
2251 return 1;
2252}
2253
2254/* set test->i to the number of concurrent connections from the session's tracked counters */
2255static int
2256acl_fetch_trk_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2257 struct acl_expr *expr, struct acl_test *test)
2258{
2259 if (!l4->tracked_counters)
2260 return 0;
2261
2262 return acl_fetch_conn_cur(l4->tracked_table, test, l4->tracked_counters);
2263}
2264
Willy Tarreau38285c12010-06-18 16:35:43 +02002265/* set test->i to the number of concurrent connections from the session's source
2266 * address in the table pointed to by expr.
2267 */
2268static int
2269acl_fetch_src_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2270 struct acl_expr *expr, struct acl_test *test)
2271{
Willy Tarreau38285c12010-06-18 16:35:43 +02002272 struct stktable_key *key;
2273
2274 key = tcpv4_src_to_stktable_key(l4);
2275 if (!key)
2276 return 0; /* only TCPv4 is supported right now */
2277
2278 if (expr->arg_len)
2279 px = find_stktable(expr->arg.str);
2280
2281 if (!px)
2282 return 0; /* table not found */
2283
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002284 return acl_fetch_conn_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau38285c12010-06-18 16:35:43 +02002285}
2286
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002287/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2288static int
2289acl_fetch_sess_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2290{
2291 test->flags = ACL_TEST_F_VOL_TEST;
2292 test->i = 0;
2293 if (ts != NULL) {
2294 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_CNT);
2295 if (!ptr)
2296 return 0; /* parameter not stored */
2297 test->i = stktable_data_cast(ptr, sess_cnt);
2298 }
2299 return 1;
2300}
2301
2302/* set test->i to the cumulated number of sessions from the session's tracked counters */
2303static int
2304acl_fetch_trk_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2305 struct acl_expr *expr, struct acl_test *test)
2306{
2307 if (!l4->tracked_counters)
2308 return 0;
2309
2310 return acl_fetch_sess_cnt(l4->tracked_table, test, l4->tracked_counters);
2311}
2312
2313/* set test->i to the cumulated number of session from the session's source
2314 * address in the table pointed to by expr.
2315 */
2316static int
2317acl_fetch_src_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2318 struct acl_expr *expr, struct acl_test *test)
2319{
2320 struct stktable_key *key;
2321
2322 key = tcpv4_src_to_stktable_key(l4);
2323 if (!key)
2324 return 0; /* only TCPv4 is supported right now */
2325
2326 if (expr->arg_len)
2327 px = find_stktable(expr->arg.str);
2328
2329 if (!px)
2330 return 0; /* table not found */
2331
2332 return acl_fetch_sess_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2333}
2334
Willy Tarreau91c43d72010-06-20 11:19:22 +02002335/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2336static int
2337acl_fetch_sess_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2338{
2339 test->flags = ACL_TEST_F_VOL_TEST;
2340 test->i = 0;
2341 if (ts != NULL) {
2342 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_RATE);
2343 if (!ptr)
2344 return 0; /* parameter not stored */
2345 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
2346 table->data_arg[STKTABLE_DT_SESS_RATE].u);
2347 }
2348 return 1;
2349}
2350
2351/* set test->i to the session rate from the session's tracked counters over
2352 * the configured period.
2353 */
2354static int
2355acl_fetch_trk_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2356 struct acl_expr *expr, struct acl_test *test)
2357{
2358 if (!l4->tracked_counters)
2359 return 0;
2360
2361 return acl_fetch_sess_rate(l4->tracked_table, test, l4->tracked_counters);
2362}
2363
2364/* set test->i to the session rate from the session's source address in the
2365 * table pointed to by expr, over the configured period.
2366 */
2367static int
2368acl_fetch_src_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2369 struct acl_expr *expr, struct acl_test *test)
2370{
2371 struct stktable_key *key;
2372
2373 key = tcpv4_src_to_stktable_key(l4);
2374 if (!key)
2375 return 0; /* only TCPv4 is supported right now */
2376
2377 if (expr->arg_len)
2378 px = find_stktable(expr->arg.str);
2379
2380 if (!px)
2381 return 0; /* table not found */
2382
2383 return acl_fetch_sess_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2384}
2385
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002386/* set test->i to the number of kbytes received from clients matching the stksess entry <ts> */
2387static int
2388acl_fetch_kbytes_in(struct stktable *table, struct acl_test *test, struct stksess *ts)
2389{
2390 test->flags = ACL_TEST_F_VOL_TEST;
2391 test->i = 0;
2392
2393 if (ts != NULL) {
2394 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_CNT);
2395 if (!ptr)
2396 return 0; /* parameter not stored */
2397 test->i = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
2398 }
2399 return 1;
2400}
2401
2402/* set test->i to the number of kbytes received from clients according to the
2403 * session's tracked counters.
2404 */
2405static int
2406acl_fetch_trk_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2407 struct acl_expr *expr, struct acl_test *test)
2408{
2409 if (!l4->tracked_counters)
2410 return 0;
2411
2412 return acl_fetch_kbytes_in(l4->tracked_table, test, l4->tracked_counters);
2413}
2414
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002415/* set test->i to the number of kbytes received from the session's source
2416 * address in the table pointed to by expr.
2417 */
2418static int
2419acl_fetch_src_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2420 struct acl_expr *expr, struct acl_test *test)
2421{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002422 struct stktable_key *key;
2423
2424 key = tcpv4_src_to_stktable_key(l4);
2425 if (!key)
2426 return 0; /* only TCPv4 is supported right now */
2427
2428 if (expr->arg_len)
2429 px = find_stktable(expr->arg.str);
2430
2431 if (!px)
2432 return 0; /* table not found */
2433
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002434 return acl_fetch_kbytes_in(&px->table, test, stktable_lookup_key(&px->table, key));
2435}
2436
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002437/* set test->i to the bytes rate from clients in the stksess entry <ts> over the
2438 * configured period.
2439 */
2440static int
2441acl_fetch_bytes_in_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2442{
2443 test->flags = ACL_TEST_F_VOL_TEST;
2444 test->i = 0;
2445 if (ts != NULL) {
2446 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_RATE);
2447 if (!ptr)
2448 return 0; /* parameter not stored */
2449 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
2450 table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
2451 }
2452 return 1;
2453}
2454
2455/* set test->i to the bytes rate from clients from the session's tracked
2456 * counters over the configured period.
2457 */
2458static int
2459acl_fetch_trk_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2460 struct acl_expr *expr, struct acl_test *test)
2461{
2462 if (!l4->tracked_counters)
2463 return 0;
2464
2465 return acl_fetch_bytes_in_rate(l4->tracked_table, test, l4->tracked_counters);
2466}
2467
2468/* set test->i to the bytes rate from clients from the session's source address
2469 * in the table pointed to by expr, over the configured period.
2470 */
2471static int
2472acl_fetch_src_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2473 struct acl_expr *expr, struct acl_test *test)
2474{
2475 struct stktable_key *key;
2476
2477 key = tcpv4_src_to_stktable_key(l4);
2478 if (!key)
2479 return 0; /* only TCPv4 is supported right now */
2480
2481 if (expr->arg_len)
2482 px = find_stktable(expr->arg.str);
2483
2484 if (!px)
2485 return 0; /* table not found */
2486
2487 return acl_fetch_bytes_in_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2488}
2489
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002490/* set test->i to the number of kbytes sent to clients matching the stksess entry <ts> */
2491static int
2492acl_fetch_kbytes_out(struct stktable *table, struct acl_test *test, struct stksess *ts)
2493{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002494 test->flags = ACL_TEST_F_VOL_TEST;
2495 test->i = 0;
2496
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002497 if (ts != NULL) {
2498 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002499 if (!ptr)
2500 return 0; /* parameter not stored */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002501 test->i = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002502 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002503 return 1;
2504}
2505
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002506/* set test->i to the number of kbytes sent to clients according to the session's
2507 * tracked counters.
2508 */
2509static int
2510acl_fetch_trk_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
2511 struct acl_expr *expr, struct acl_test *test)
2512{
2513 if (!l4->tracked_counters)
2514 return 0;
2515
2516 return acl_fetch_kbytes_out(l4->tracked_table, test, l4->tracked_counters);
2517}
2518
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002519/* set test->i to the number of kbytes sent to the session's source address in
2520 * the table pointed to by expr.
2521 */
2522static int
2523acl_fetch_src_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
2524 struct acl_expr *expr, struct acl_test *test)
2525{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002526 struct stktable_key *key;
2527
2528 key = tcpv4_src_to_stktable_key(l4);
2529 if (!key)
2530 return 0; /* only TCPv4 is supported right now */
2531
2532 if (expr->arg_len)
2533 px = find_stktable(expr->arg.str);
2534
2535 if (!px)
2536 return 0; /* table not found */
2537
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002538 return acl_fetch_kbytes_out(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002539}
2540
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002541/* set test->i to the bytes rate to clients in the stksess entry <ts> over the
2542 * configured period.
2543 */
2544static int
2545acl_fetch_bytes_out_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2546{
2547 test->flags = ACL_TEST_F_VOL_TEST;
2548 test->i = 0;
2549 if (ts != NULL) {
2550 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_RATE);
2551 if (!ptr)
2552 return 0; /* parameter not stored */
2553 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
2554 table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
2555 }
2556 return 1;
2557}
2558
2559/* set test->i to the bytes rate to clients from the session's tracked counters
2560 * over the configured period.
2561 */
2562static int
2563acl_fetch_trk_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2564 struct acl_expr *expr, struct acl_test *test)
2565{
2566 if (!l4->tracked_counters)
2567 return 0;
2568
2569 return acl_fetch_bytes_out_rate(l4->tracked_table, test, l4->tracked_counters);
2570}
2571
2572/* set test->i to the bytes rate to client from the session's source address in
2573 * the table pointed to by expr, over the configured period.
2574 */
2575static int
2576acl_fetch_src_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2577 struct acl_expr *expr, struct acl_test *test)
2578{
2579 struct stktable_key *key;
2580
2581 key = tcpv4_src_to_stktable_key(l4);
2582 if (!key)
2583 return 0; /* only TCPv4 is supported right now */
2584
2585 if (expr->arg_len)
2586 px = find_stktable(expr->arg.str);
2587
2588 if (!px)
2589 return 0; /* table not found */
2590
2591 return acl_fetch_bytes_out_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2592}
2593
Willy Tarreau8b22a712010-06-18 17:46:06 +02002594
2595/* Note: must not be declared <const> as its list will be overwritten */
2596static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002597 { "trk_conn_cnt", acl_parse_int, acl_fetch_trk_conn_cnt, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau8b22a712010-06-18 17:46:06 +02002598 { "src_conn_cnt", acl_parse_int, acl_fetch_src_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
Willy Tarreau91c43d72010-06-20 11:19:22 +02002599 { "trk_conn_rate", acl_parse_int, acl_fetch_trk_conn_rate, acl_match_int, ACL_USE_NOTHING },
2600 { "src_conn_rate", acl_parse_int, acl_fetch_src_conn_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
Willy Tarreau8b22a712010-06-18 17:46:06 +02002601 { "src_updt_conn_cnt", acl_parse_int, acl_fetch_src_updt_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002602 { "trk_conn_cur", acl_parse_int, acl_fetch_trk_conn_cur, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau38285c12010-06-18 16:35:43 +02002603 { "src_conn_cur", acl_parse_int, acl_fetch_src_conn_cur, acl_match_int, ACL_USE_TCP4_VOLATILE },
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002604 { "trk_sess_cnt", acl_parse_int, acl_fetch_trk_sess_cnt, acl_match_int, ACL_USE_NOTHING },
2605 { "src_sess_cnt", acl_parse_int, acl_fetch_src_sess_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
Willy Tarreau91c43d72010-06-20 11:19:22 +02002606 { "trk_sess_rate", acl_parse_int, acl_fetch_trk_sess_rate, acl_match_int, ACL_USE_NOTHING },
2607 { "src_sess_rate", acl_parse_int, acl_fetch_src_sess_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002608 { "trk_kbytes_in", acl_parse_int, acl_fetch_trk_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002609 { "src_kbytes_in", acl_parse_int, acl_fetch_src_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002610 { "trk_bytes_in_rate", acl_parse_int, acl_fetch_trk_bytes_in_rate, acl_match_int, ACL_USE_NOTHING },
2611 { "src_bytes_in_rate", acl_parse_int, acl_fetch_src_bytes_in_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002612 { "trk_kbytes_out", acl_parse_int, acl_fetch_trk_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002613 { "src_kbytes_out", acl_parse_int, acl_fetch_src_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002614 { "trk_bytes_out_rate", acl_parse_int, acl_fetch_trk_bytes_out_rate,acl_match_int, ACL_USE_NOTHING },
2615 { "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 +02002616 { NULL, NULL, NULL, NULL },
2617}};
2618
2619
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002620/* Parse a "track-counters" line starting with "track-counters" in args[arg-1].
2621 * Returns the number of warnings emitted, or -1 in case of fatal errors. The
2622 * <prm> struct is fed with the table name if any. If unspecified, the caller
2623 * will assume that the current proxy's table is used.
2624 */
2625int parse_track_counters(char **args, int *arg,
2626 int section_type, struct proxy *curpx,
2627 struct track_ctr_prm *prm,
2628 struct proxy *defpx, char *err, int errlen)
2629{
2630 int pattern_type = 0;
2631
2632 /* parse the arguments of "track-counters" before the condition in the
2633 * following form :
2634 * track-counters src [ table xxx ] [ if|unless ... ]
2635 */
2636 while (args[*arg]) {
2637 if (strcmp(args[*arg], "src") == 0) {
2638 prm->type = STKTABLE_TYPE_IP;
2639 pattern_type = 1;
2640 }
2641 else if (strcmp(args[*arg], "table") == 0) {
2642 if (!args[*arg + 1]) {
2643 snprintf(err, errlen,
2644 "missing table for track-counter in %s '%s'.",
2645 proxy_type_str(curpx), curpx->id);
2646 return -1;
2647 }
2648 /* we copy the table name for now, it will be resolved later */
2649 prm->table.n = strdup(args[*arg + 1]);
2650 (*arg)++;
2651 }
2652 else {
2653 /* unhandled keywords are handled by the caller */
2654 break;
2655 }
2656 (*arg)++;
2657 }
2658
2659 if (!pattern_type) {
2660 snprintf(err, errlen,
2661 "track-counter key not specified in %s '%s' (found %s, only 'src' is supported).",
2662 proxy_type_str(curpx), curpx->id, quote_arg(args[*arg]));
2663 return -1;
2664 }
2665
2666 return 0;
2667}
2668
Willy Tarreau8b22a712010-06-18 17:46:06 +02002669__attribute__((constructor))
2670static void __session_init(void)
2671{
2672 acl_register_keywords(&acl_kws);
2673}
2674
Willy Tarreaubaaee002006-06-26 02:48:02 +02002675/*
2676 * Local variables:
2677 * c-indent-level: 8
2678 * c-basic-offset: 8
2679 * End:
2680 */