blob: 6cb702dd1fa2fc70a768e0817e466cba56fc21e6 [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 Tarreau8d5d7f22007-01-21 19:16:41 +010029#include <proto/hdr_idx.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020030#include <proto/log.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031#include <proto/session.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010032#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010033#include <proto/proto_http.h>
34#include <proto/proto_tcp.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020035#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020036#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010037#include <proto/server.h>
Emeric Brun1d33b292010-01-04 15:47:17 +010038#include <proto/stick_table.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010039#include <proto/stream_interface.h>
40#include <proto/stream_sock.h>
41#include <proto/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020043struct pool_head *pool2_session;
Willy Tarreauf54f8bd2008-11-23 19:53:55 +010044struct list sessions;
Willy Tarreaubaaee002006-06-26 02:48:02 +020045
Willy Tarreau81f9aa32010-06-01 17:45:26 +020046/* This function is called from the protocol layer accept() in order to instanciate
47 * a new session on behalf of a given listener and frontend. It returns a positive
48 * value upon success, 0 if the connection needs to be closed and ignored, or a
49 * negative value upon critical failure.
50 */
51int session_accept(struct listener *l, int cfd, struct sockaddr_storage *addr)
52{
53 struct proxy *p = l->frontend;
54 struct session *s;
55 struct http_txn *txn;
56 struct task *t;
57
58 if (unlikely((s = pool_alloc2(pool2_session)) == NULL)) {
59 Alert("out of memory in event_accept().\n");
60 goto out_close;
61 }
62
63 /* minimum session initialization required for monitor mode below */
64 s->flags = 0;
65 s->logs.logwait = p->to_log;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020066 s->tracked_counters = NULL;
67 s->tracked_table = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020068
69 /* if this session comes from a known monitoring system, we want to ignore
70 * it as soon as possible, which means closing it immediately for TCP, but
71 * cleanly.
72 */
73 if (unlikely((l->options & LI_O_CHK_MONNET) &&
74 addr->ss_family == AF_INET &&
75 (((struct sockaddr_in *)addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr)) {
76 if (p->mode == PR_MODE_TCP) {
77 pool_free2(pool2_session, s);
78 return 0;
79 }
80 s->flags |= SN_MONITOR;
81 s->logs.logwait = 0;
82 }
83
84 /* OK, we're keeping the session, so let's properly initialize the session */
85 LIST_ADDQ(&sessions, &s->list);
86 LIST_INIT(&s->back_refs);
87
88 if (unlikely((t = task_new()) == NULL)) { /* disable this proxy for a while */
89 Alert("out of memory in event_accept().\n");
90 goto out_free_session;
91 }
92
93 s->term_trace = 0;
94 s->cli_addr = *addr;
95 s->logs.accept_date = date; /* user-visible date for logging */
96 s->logs.tv_accept = now; /* corrected date for internal use */
97 s->uniq_id = totalconn;
Willy Tarreau24dcaf32010-06-05 10:49:41 +020098 p->feconn++; /* beconn will be increased once assigned */
99
Willy Tarreaub36b4242010-06-04 20:59:39 +0200100 proxy_inc_fe_conn_ctr(l, p); /* note: cum_beconn will be increased once assigned */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200101
102 t->process = l->handler;
103 t->context = s;
104 t->nice = l->nice;
105 t->expire = TICK_ETERNITY;
106
107 s->task = t;
108 s->listener = l;
109
110 /* Note: initially, the session's backend points to the frontend.
111 * This changes later when switching rules are executed or
112 * when the default backend is assigned.
113 */
114 s->be = s->fe = p;
115 s->req = s->rep = NULL; /* will be allocated later */
116
117 /* now evaluate the tcp-request layer4 rules. Since we expect to be able
118 * to abort right here as soon as possible, we check the rules before
119 * even initializing the stream interfaces.
120 */
121 if ((l->options & LI_O_TCP_RULES) && !tcp_exec_req_rules(s)) {
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200122 if (s->tracked_counters)
123 session_store_counters(s);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200124 task_free(t);
125 LIST_DEL(&s->list);
126 pool_free2(pool2_session, s);
127 /* let's do a no-linger now to close with a single RST. */
128 setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200129 p->feconn--;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200130 return 0;
131 }
132
Willy Tarreaub36b4242010-06-04 20:59:39 +0200133 /* This session was accepted, count it now */
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200134 if (p->feconn > p->counters.feconn_max)
135 p->counters.feconn_max = p->feconn;
Willy Tarreaub36b4242010-06-04 20:59:39 +0200136 proxy_inc_fe_sess_ctr(l, p);
137
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200138 /* this part should be common with other protocols */
139 s->si[0].fd = cfd;
140 s->si[0].owner = t;
141 s->si[0].state = s->si[0].prev_state = SI_ST_EST;
142 s->si[0].err_type = SI_ET_NONE;
143 s->si[0].err_loc = NULL;
144 s->si[0].connect = NULL;
145 s->si[0].iohandler = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200146 s->si[0].release = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200147 s->si[0].exp = TICK_ETERNITY;
148 s->si[0].flags = SI_FL_NONE;
149
150 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
151 s->si[0].flags |= SI_FL_INDEP_STR;
152
153 if (addr->ss_family == AF_INET || addr->ss_family == AF_INET6)
154 s->si[0].flags = SI_FL_CAP_SPLTCP; /* TCP/TCPv6 splicing possible */
155
156 /* add the various callbacks */
157 stream_sock_prepare_interface(&s->si[0]);
158
159 /* pre-initialize the other side's stream interface to an INIT state. The
160 * callbacks will be initialized before attempting to connect.
161 */
162 s->si[1].fd = -1; /* just to help with debugging */
163 s->si[1].owner = t;
164 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
165 s->si[1].err_type = SI_ET_NONE;
166 s->si[1].err_loc = NULL;
167 s->si[1].connect = NULL;
168 s->si[1].iohandler = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200169 s->si[1].release = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200170 s->si[1].shutr = stream_int_shutr;
171 s->si[1].shutw = stream_int_shutw;
172 s->si[1].exp = TICK_ETERNITY;
173 s->si[1].flags = SI_FL_NONE;
174
175 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
176 s->si[1].flags |= SI_FL_INDEP_STR;
177
178 s->srv = s->prev_srv = s->srv_conn = NULL;
179 s->pend_pos = NULL;
180
181 /* init store persistence */
182 s->store_count = 0;
183
184 /* Adjust some socket options */
185 if (unlikely(fcntl(cfd, F_SETFL, O_NONBLOCK) == -1)) {
186 Alert("accept(): cannot set the socket in non blocking mode. Giving up\n");
187 goto out_free_task;
188 }
189
190 txn = &s->txn;
191 /* Those variables will be checked and freed if non-NULL in
192 * session.c:session_free(). It is important that they are
193 * properly initialized.
194 */
195 txn->sessid = NULL;
196 txn->srv_cookie = NULL;
197 txn->cli_cookie = NULL;
198 txn->uri = NULL;
199 txn->req.cap = NULL;
200 txn->rsp.cap = NULL;
201 txn->hdr_idx.v = NULL;
202 txn->hdr_idx.size = txn->hdr_idx.used = 0;
203
204 if (unlikely((s->req = pool_alloc2(pool2_buffer)) == NULL))
205 goto out_free_task; /* no memory */
206
207 if (unlikely((s->rep = pool_alloc2(pool2_buffer)) == NULL))
208 goto out_free_req; /* no memory */
209
210 /* initialize the request buffer */
211 s->req->size = global.tune.bufsize;
212 buffer_init(s->req);
213 s->req->prod = &s->si[0];
214 s->req->cons = &s->si[1];
215 s->si[0].ib = s->si[1].ob = s->req;
216 s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */
217
218 /* activate default analysers enabled for this listener */
219 s->req->analysers = l->analysers;
220
221 s->req->wto = TICK_ETERNITY;
222 s->req->rto = TICK_ETERNITY;
223 s->req->rex = TICK_ETERNITY;
224 s->req->wex = TICK_ETERNITY;
225 s->req->analyse_exp = TICK_ETERNITY;
226
227 /* initialize response buffer */
228 s->rep->size = global.tune.bufsize;
229 buffer_init(s->rep);
230 s->rep->prod = &s->si[1];
231 s->rep->cons = &s->si[0];
232 s->si[0].ob = s->si[1].ib = s->rep;
233 s->rep->analysers = 0;
234
235 s->rep->rto = TICK_ETERNITY;
236 s->rep->wto = TICK_ETERNITY;
237 s->rep->rex = TICK_ETERNITY;
238 s->rep->wex = TICK_ETERNITY;
239 s->rep->analyse_exp = TICK_ETERNITY;
240
241 /* finish initialization of the accepted file descriptor */
242 fd_insert(cfd);
243 fdtab[cfd].owner = &s->si[0];
244 fdtab[cfd].state = FD_STREADY;
245 fdtab[cfd].flags = 0;
246 fdtab[cfd].cb[DIR_RD].f = l->proto->read;
247 fdtab[cfd].cb[DIR_RD].b = s->req;
248 fdtab[cfd].cb[DIR_WR].f = l->proto->write;
249 fdtab[cfd].cb[DIR_WR].b = s->rep;
250 fdinfo[cfd].peeraddr = (struct sockaddr *)&s->cli_addr;
251 fdinfo[cfd].peerlen = sizeof(s->cli_addr);
252 EV_FD_SET(cfd, DIR_RD);
253
254 if (p->accept) {
255 int ret = p->accept(s);
256 if (unlikely(ret < 0))
257 goto out_free_rep;
258
259 if (unlikely(ret == 0)) {
260 /* work is finished, we can release everything (eg: monitoring) */
261 pool_free2(pool2_buffer, s->rep);
262 pool_free2(pool2_buffer, s->req);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200263 if (s->tracked_counters)
264 session_store_counters(s);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200265 task_free(t);
266 LIST_DEL(&s->list);
267 pool_free2(pool2_session, s);
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200268 p->feconn--;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200269 return 0;
270 }
271 }
272
273 /* it is important not to call the wakeup function directly but to
274 * pass through task_wakeup(), because this one knows how to apply
275 * priorities to tasks.
276 */
277 task_wakeup(t, TASK_WOKEN_INIT);
278 return 1;
279
280 /* Error unrolling */
281 out_free_rep:
282 pool_free2(pool2_buffer, s->rep);
283 out_free_req:
284 pool_free2(pool2_buffer, s->req);
285 out_free_task:
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200286 p->feconn--;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200287 if (s->tracked_counters)
288 session_store_counters(s);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200289 task_free(t);
290 out_free_session:
291 LIST_DEL(&s->list);
292 pool_free2(pool2_session, s);
293 out_close:
294 return -1;
295}
296
Willy Tarreaubaaee002006-06-26 02:48:02 +0200297/*
298 * frees the context associated to a session. It must have been removed first.
299 */
300void session_free(struct session *s)
301{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +0100302 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +0200303 struct proxy *fe = s->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100304 struct bref *bref, *back;
Willy Tarreaua4cda672010-06-06 18:28:49 +0200305 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100306
Willy Tarreaubaaee002006-06-26 02:48:02 +0200307 if (s->pend_pos)
308 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100309
Willy Tarreau1e62de62008-11-11 20:20:02 +0100310 if (s->srv) { /* there may be requests left pending in queue */
311 if (s->flags & SN_CURR_SESS) {
312 s->flags &= ~SN_CURR_SESS;
313 s->srv->cur_sess--;
314 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100315 if (may_dequeue_tasks(s->srv, s->be))
316 process_srv_queue(s->srv);
Willy Tarreau1e62de62008-11-11 20:20:02 +0100317 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100318
Willy Tarreau7c669d72008-06-20 15:04:11 +0200319 if (unlikely(s->srv_conn)) {
320 /* the session still has a reserved slot on a server, but
321 * it should normally be only the same as the one above,
322 * so this should not happen in fact.
323 */
324 sess_change_server(s, NULL);
325 }
326
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100327 if (s->req->pipe)
328 put_pipe(s->req->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100329
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100330 if (s->rep->pipe)
331 put_pipe(s->rep->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100332
Willy Tarreau48d63db2008-08-03 17:41:33 +0200333 pool_free2(pool2_buffer, s->req);
334 pool_free2(pool2_buffer, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200335
Willy Tarreau46023632010-01-07 22:51:47 +0100336 http_end_txn(s);
337
Willy Tarreaua4cda672010-06-06 18:28:49 +0200338 for (i = 0; i < s->store_count; i++) {
339 if (!s->store[i].ts)
340 continue;
341 stksess_free(s->store[i].table, s->store[i].ts);
342 s->store[i].ts = NULL;
343 }
344
Willy Tarreau92fb9832007-10-16 17:34:28 +0200345 if (fe) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200346 pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau46023632010-01-07 22:51:47 +0100347 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
348 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200349 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100350
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200351 if (s->tracked_counters)
352 session_store_counters(s);
353
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100354 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100355 /* we have to unlink all watchers. We must not relink them if
356 * this session was the last one in the list.
357 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100358 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100359 LIST_INIT(&bref->users);
360 if (s->list.n != &sessions)
361 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100362 bref->ref = s->list.n;
363 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100364 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200365 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200366
367 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200368 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200369 pool_flush2(pool2_buffer);
370 pool_flush2(fe->hdr_idx_pool);
371 pool_flush2(pool2_requri);
372 pool_flush2(pool2_capture);
373 pool_flush2(pool2_session);
374 pool_flush2(fe->req_cap_pool);
375 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200376 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200377}
378
379
380/* perform minimal intializations, report 0 in case of error, 1 if OK. */
381int init_session()
382{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100383 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200384 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
385 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200386}
387
Willy Tarreau30e71012007-11-26 20:15:35 +0100388void session_process_counters(struct session *s)
389{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100390 unsigned long long bytes;
391
Willy Tarreau30e71012007-11-26 20:15:35 +0100392 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100393 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100394 s->logs.bytes_in = s->req->total;
395 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200396 s->fe->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100397
Willy Tarreau30e71012007-11-26 20:15:35 +0100398 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200399 s->be->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100400
Willy Tarreau30e71012007-11-26 20:15:35 +0100401 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200402 s->srv->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200403
404 if (s->listener->counters)
405 s->listener->counters->bytes_in += bytes;
Willy Tarreau30e71012007-11-26 20:15:35 +0100406 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100407 }
408
Willy Tarreau30e71012007-11-26 20:15:35 +0100409 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100410 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100411 s->logs.bytes_out = s->rep->total;
412 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200413 s->fe->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100414
Willy Tarreau30e71012007-11-26 20:15:35 +0100415 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200416 s->be->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100417
Willy Tarreau30e71012007-11-26 20:15:35 +0100418 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200419 s->srv->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200420
421 if (s->listener->counters)
422 s->listener->counters->bytes_out += bytes;
Willy Tarreau30e71012007-11-26 20:15:35 +0100423 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100424 }
425}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200426
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100427/* This function is called with (si->state == SI_ST_CON) meaning that a
428 * connection was attempted and that the file descriptor is already allocated.
429 * We must check for establishment, error and abort. Possible output states
430 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
431 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
432 * otherwise 1.
433 */
434int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
435{
436 struct buffer *req = si->ob;
437 struct buffer *rep = si->ib;
438
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100439 /* If we got an error, or if nothing happened and the connection timed
440 * out, we must give up. The CER state handler will take care of retry
441 * attempts and error reports.
442 */
443 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau127334e2009-03-28 10:47:26 +0100444 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100445 si->state = SI_ST_CER;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200446 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100447 fd_delete(si->fd);
448
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200449 if (si->release)
450 si->release(si);
451
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100452 if (si->err_type)
453 return 0;
454
455 si->err_loc = s->srv;
456 if (si->flags & SI_FL_ERR)
457 si->err_type = SI_ET_CONN_ERR;
458 else
459 si->err_type = SI_ET_CONN_TO;
460 return 0;
461 }
462
463 /* OK, maybe we want to abort */
Willy Tarreau418fd472009-09-06 21:37:23 +0200464 if (unlikely((rep->flags & BF_SHUTW) ||
465 ((req->flags & BF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200466 (((req->flags & (BF_OUT_EMPTY|BF_WRITE_ACTIVITY)) == BF_OUT_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100467 s->be->options & PR_O_ABRT_CLOSE)))) {
468 /* give up */
469 si->shutw(si);
470 si->err_type |= SI_ET_CONN_ABRT;
471 si->err_loc = s->srv;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200472 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau84455332009-03-15 22:34:05 +0100473 if (s->srv_error)
474 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100475 return 1;
476 }
477
478 /* we need to wait a bit more if there was no activity either */
479 if (!(req->flags & BF_WRITE_ACTIVITY))
480 return 1;
481
482 /* OK, this means that a connection succeeded. The caller will be
483 * responsible for handling the transition from CON to EST.
484 */
485 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100486 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100487 si->state = SI_ST_EST;
488 si->err_type = SI_ET_NONE;
489 si->err_loc = NULL;
490 return 1;
491}
492
493/* This function is called with (si->state == SI_ST_CER) meaning that a
494 * previous connection attempt has failed and that the file descriptor
495 * has already been released. Possible causes include asynchronous error
496 * notification and time out. Possible output states are SI_ST_CLO when
497 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
498 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
499 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
500 * marked as in error state. It returns 0.
501 */
502int sess_update_st_cer(struct session *s, struct stream_interface *si)
503{
504 /* we probably have to release last session from the server */
505 if (s->srv) {
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100506 health_adjust(s->srv, HANA_STATUS_L4_ERR);
507
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100508 if (s->flags & SN_CURR_SESS) {
509 s->flags &= ~SN_CURR_SESS;
510 s->srv->cur_sess--;
511 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100512 }
513
514 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200515 si->conn_retries--;
516 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100517 if (!si->err_type) {
518 si->err_type = SI_ET_CONN_ERR;
519 si->err_loc = s->srv;
520 }
521
522 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200523 s->srv->counters.failed_conns++;
524 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100525 if (may_dequeue_tasks(s->srv, s->be))
526 process_srv_queue(s->srv);
527
528 /* shutw is enough so stop a connecting socket */
529 si->shutw(si);
530 si->ob->flags |= BF_WRITE_ERROR;
531 si->ib->flags |= BF_READ_ERROR;
532
533 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100534 if (s->srv_error)
535 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100536 return 0;
537 }
538
539 /* If the "redispatch" option is set on the backend, we are allowed to
540 * retry on another server for the last retry. In order to achieve this,
541 * we must mark the session unassigned, and eventually clear the DIRECT
542 * bit to ignore any persistence cookie. We won't count a retry nor a
543 * redispatch yet, because this will depend on what server is selected.
544 */
Willy Tarreauee28de02010-06-01 09:51:00 +0200545 if (s->srv && si->conn_retries == 0 &&
Willy Tarreau4de91492010-01-22 19:10:05 +0100546 s->be->options & PR_O_REDISP && !(s->flags & SN_FORCE_PRST)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100547 if (may_dequeue_tasks(s->srv, s->be))
548 process_srv_queue(s->srv);
549
550 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
551 s->prev_srv = s->srv;
552 si->state = SI_ST_REQ;
553 } else {
554 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200555 s->srv->counters.retries++;
556 s->be->counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100557 si->state = SI_ST_ASS;
558 }
559
560 if (si->flags & SI_FL_ERR) {
561 /* The error was an asynchronous connection error, and we will
562 * likely have to retry connecting to the same server, most
563 * likely leading to the same result. To avoid this, we wait
564 * one second before retrying.
565 */
566
567 if (!si->err_type)
568 si->err_type = SI_ET_CONN_ERR;
569
570 si->state = SI_ST_TAR;
571 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
572 return 0;
573 }
574 return 0;
575}
576
577/*
578 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200579 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
580 * SI_ST_INI) to SI_ST_EST, but only when a ->connect function is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100581 */
582void sess_establish(struct session *s, struct stream_interface *si)
583{
584 struct buffer *req = si->ob;
585 struct buffer *rep = si->ib;
586
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100587 if (s->srv)
588 health_adjust(s->srv, HANA_STATUS_L4_OK);
589
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100590 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100591 /* if the user wants to log as soon as possible, without counting
592 * bytes from the server, then this is the right moment. */
593 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
594 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100595 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100596 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100597 }
598 else {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100599 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
600 /* reset hdr_idx which was already initialized by the request.
601 * right now, the http parser does it.
602 * hdr_idx_init(&s->txn.hdr_idx);
603 */
604 }
605
Willy Tarreau4e5b8282009-08-16 22:57:50 +0200606 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100607 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
Willy Tarreaud04e8582010-05-31 12:31:35 +0200608 if (si->connect) {
609 /* real connections have timeouts */
610 req->wto = s->be->timeout.server;
611 rep->rto = s->be->timeout.server;
612 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100613 req->wex = TICK_ETERNITY;
614}
615
616/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
617 * Other input states are simply ignored.
618 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
619 * Flags must have previously been updated for timeouts and other conditions.
620 */
621void sess_update_stream_int(struct session *s, struct stream_interface *si)
622{
623 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",
624 now_ms, __FUNCTION__,
625 s,
626 s->req, s->rep,
627 s->req->rex, s->rep->wex,
628 s->req->flags, s->rep->flags,
629 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
630
631 if (si->state == SI_ST_ASS) {
632 /* Server assigned to connection request, we have to try to connect now */
633 int conn_err;
634
635 conn_err = connect_server(s);
636 if (conn_err == SN_ERR_NONE) {
637 /* state = SI_ST_CON now */
Willy Tarreau8f6457c2008-12-01 00:08:28 +0100638 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100639 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100640 return;
641 }
642
643 /* We have received a synchronous error. We might have to
644 * abort, retry immediately or redispatch.
645 */
646 if (conn_err == SN_ERR_INTERNAL) {
647 if (!si->err_type) {
648 si->err_type = SI_ET_CONN_OTHER;
649 si->err_loc = s->srv;
650 }
651
652 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100653 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100654 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200655 s->srv->counters.failed_conns++;
656 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100657
658 /* release other sessions waiting for this server */
659 if (may_dequeue_tasks(s->srv, s->be))
660 process_srv_queue(s->srv);
661
662 /* Failed and not retryable. */
663 si->shutr(si);
664 si->shutw(si);
665 si->ob->flags |= BF_WRITE_ERROR;
666
667 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
668
669 /* no session was ever accounted for this server */
670 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100671 if (s->srv_error)
672 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100673 return;
674 }
675
676 /* We are facing a retryable error, but we don't want to run a
677 * turn-around now, as the problem is likely a source port
678 * allocation problem, so we want to retry now.
679 */
680 si->state = SI_ST_CER;
681 si->flags &= ~SI_FL_ERR;
682 sess_update_st_cer(s, si);
683 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
684 return;
685 }
686 else if (si->state == SI_ST_QUE) {
687 /* connection request was queued, check for any update */
688 if (!s->pend_pos) {
689 /* The connection is not in the queue anymore. Either
690 * we have a server connection slot available and we
691 * go directly to the assigned state, or we need to
692 * load-balance first and go to the INI state.
693 */
694 si->exp = TICK_ETERNITY;
695 if (unlikely(!(s->flags & SN_ASSIGNED)))
696 si->state = SI_ST_REQ;
697 else {
698 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
699 si->state = SI_ST_ASS;
700 }
701 return;
702 }
703
704 /* Connection request still in queue... */
705 if (si->flags & SI_FL_EXP) {
706 /* ... and timeout expired */
707 si->exp = TICK_ETERNITY;
708 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
709 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200710 s->srv->counters.failed_conns++;
711 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100712 si->shutr(si);
713 si->shutw(si);
714 si->ob->flags |= BF_WRITE_TIMEOUT;
715 if (!si->err_type)
716 si->err_type = SI_ET_QUEUE_TO;
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 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau418fd472009-09-06 21:37:23 +0200724 if ((si->ob->flags & (BF_READ_ERROR)) ||
725 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200726 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100727 /* give up */
728 si->exp = TICK_ETERNITY;
729 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
730 si->shutr(si);
731 si->shutw(si);
732 si->err_type |= SI_ET_QUEUE_ABRT;
733 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100734 if (s->srv_error)
735 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100736 return;
737 }
738
739 /* Nothing changed */
740 return;
741 }
742 else if (si->state == SI_ST_TAR) {
743 /* Connection request might be aborted */
Willy Tarreau418fd472009-09-06 21:37:23 +0200744 if ((si->ob->flags & (BF_READ_ERROR)) ||
745 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200746 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100747 /* give up */
748 si->exp = TICK_ETERNITY;
749 si->shutr(si);
750 si->shutw(si);
751 si->err_type |= SI_ET_CONN_ABRT;
752 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100753 if (s->srv_error)
754 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100755 return;
756 }
757
758 if (!(si->flags & SI_FL_EXP))
759 return; /* still in turn-around */
760
761 si->exp = TICK_ETERNITY;
762
763 /* we keep trying on the same server as long as the session is
764 * marked "assigned".
765 * FIXME: Should we force a redispatch attempt when the server is down ?
766 */
767 if (s->flags & SN_ASSIGNED)
768 si->state = SI_ST_ASS;
769 else
770 si->state = SI_ST_REQ;
771 return;
772 }
773}
774
775/* This function initiates a server connection request on a stream interface
776 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
777 * indicating that a server has been assigned. It may also return SI_ST_QUE,
778 * or SI_ST_CLO upon error.
779 */
780static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
781 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",
782 now_ms, __FUNCTION__,
783 s,
784 s->req, s->rep,
785 s->req->rex, s->rep->wex,
786 s->req->flags, s->rep->flags,
787 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
788
789 if (si->state != SI_ST_REQ)
790 return;
791
792 /* Try to assign a server */
793 if (srv_redispatch_connect(s) != 0) {
794 /* We did not get a server. Either we queued the
795 * connection request, or we encountered an error.
796 */
797 if (si->state == SI_ST_QUE)
798 return;
799
800 /* we did not get any server, let's check the cause */
801 si->shutr(si);
802 si->shutw(si);
803 si->ob->flags |= BF_WRITE_ERROR;
804 if (!si->err_type)
805 si->err_type = SI_ET_CONN_OTHER;
806 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100807 if (s->srv_error)
808 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100809 return;
810 }
811
812 /* The server is assigned */
813 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
814 si->state = SI_ST_ASS;
815}
816
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200817/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +0100818 * if appropriate. The default_backend rule is also considered, then the
819 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200820 * It returns 1 if the processing can continue on next analysers, or zero if it
821 * either needs more data or wants to immediately abort the request.
822 */
823int process_switching_rules(struct session *s, struct buffer *req, int an_bit)
824{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200825 struct persist_rule *prst_rule;
Willy Tarreau4de91492010-01-22 19:10:05 +0100826
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200827 req->analysers &= ~an_bit;
828 req->analyse_exp = TICK_ETERNITY;
829
830 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
831 now_ms, __FUNCTION__,
832 s,
833 req,
834 req->rex, req->wex,
835 req->flags,
836 req->l,
837 req->analysers);
838
839 /* now check whether we have some switching rules for this request */
840 if (!(s->flags & SN_BE_ASSIGNED)) {
841 struct switching_rule *rule;
842
843 list_for_each_entry(rule, &s->fe->switching_rules, list) {
844 int ret;
845
846 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ);
847 ret = acl_pass(ret);
848 if (rule->cond->pol == ACL_COND_UNLESS)
849 ret = !ret;
850
851 if (ret) {
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200852 if (!session_set_backend(s, rule->be.backend))
853 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200854 break;
855 }
856 }
857
858 /* To ensure correct connection accounting on the backend, we
859 * have to assign one if it was not set (eg: a listen). This
860 * measure also takes care of correctly setting the default
861 * backend if any.
862 */
863 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200864 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
865 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200866 }
867
Willy Tarreaufb356202010-08-03 14:02:05 +0200868 /* we don't want to run the TCP or HTTP filters again if the backend has not changed */
869 if (s->fe == s->be) {
870 s->req->analysers &= ~AN_REQ_INSPECT_BE;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200871 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +0200872 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200873
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200874 /* 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 +0100875 * persistence rule, and report that in the session.
876 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200877 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +0100878 int ret = 1;
879
880 if (prst_rule->cond) {
881 ret = acl_exec_cond(prst_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
882 ret = acl_pass(ret);
883 if (prst_rule->cond->pol == ACL_COND_UNLESS)
884 ret = !ret;
885 }
886
887 if (ret) {
888 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200889 if (prst_rule->type == PERSIST_TYPE_FORCE) {
890 s->flags |= SN_FORCE_PRST;
891 } else {
892 s->flags |= SN_IGNORE_PRST;
893 }
Willy Tarreau4de91492010-01-22 19:10:05 +0100894 break;
895 }
896 }
897
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200898 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200899
900 sw_failed:
901 /* immediately abort this request in case of allocation failure */
902 buffer_abort(s->req);
903 buffer_abort(s->rep);
904
905 if (!(s->flags & SN_ERR_MASK))
906 s->flags |= SN_ERR_RESOURCE;
907 if (!(s->flags & SN_FINST_MASK))
908 s->flags |= SN_FINST_R;
909
910 s->txn.status = 500;
911 s->req->analysers = 0;
912 s->req->analyse_exp = TICK_ETERNITY;
913 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200914}
915
Emeric Brun1d33b292010-01-04 15:47:17 +0100916/* This stream analyser works on a request. It applies all sticking rules on
917 * it then returns 1. The data must already be present in the buffer otherwise
918 * they won't match. It always returns 1.
919 */
920int process_sticking_rules(struct session *s, struct buffer *req, int an_bit)
921{
922 struct proxy *px = s->be;
923 struct sticking_rule *rule;
924
925 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
926 now_ms, __FUNCTION__,
927 s,
928 req,
929 req->rex, req->wex,
930 req->flags,
931 req->l,
932 req->analysers);
933
934 list_for_each_entry(rule, &px->sticking_rules, list) {
935 int ret = 1 ;
936 int i;
937
938 for (i = 0; i < s->store_count; i++) {
939 if (rule->table.t == s->store[i].table)
940 break;
941 }
942
943 if (i != s->store_count)
944 continue;
945
946 if (rule->cond) {
947 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_REQ);
948 ret = acl_pass(ret);
949 if (rule->cond->pol == ACL_COND_UNLESS)
950 ret = !ret;
951 }
952
953 if (ret) {
954 struct stktable_key *key;
955
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200956 key = stktable_fetch_key(px, s, &s->txn, PATTERN_FETCH_REQ, rule->expr, rule->table.t->type);
Emeric Brun1d33b292010-01-04 15:47:17 +0100957 if (!key)
958 continue;
959
960 if (rule->flags & STK_IS_MATCH) {
961 struct stksess *ts;
962
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200963 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Emeric Brun1d33b292010-01-04 15:47:17 +0100964 if (!(s->flags & SN_ASSIGNED)) {
965 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +0200966 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +0100967
968 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +0200969 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
970 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +0100971 if (node) {
972 struct server *srv;
973
974 srv = container_of(node, struct server, conf.id);
Willy Tarreau4de91492010-01-22 19:10:05 +0100975 if ((srv->state & SRV_RUNNING) ||
976 (px->options & PR_O_PERSIST) ||
977 (s->flags & SN_FORCE_PRST)) {
Emeric Brun1d33b292010-01-04 15:47:17 +0100978 s->flags |= SN_DIRECT | SN_ASSIGNED;
979 s->srv = srv;
980 }
981 }
982 }
Willy Tarreaue8f63382010-07-13 15:20:24 +0200983 stktable_touch(rule->table.t, ts);
Emeric Brun1d33b292010-01-04 15:47:17 +0100984 }
985 }
986 if (rule->flags & STK_IS_STORE) {
987 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
988 struct stksess *ts;
989
990 ts = stksess_new(rule->table.t, key);
991 if (ts) {
992 s->store[s->store_count].table = rule->table.t;
993 s->store[s->store_count++].ts = ts;
994 }
995 }
996 }
997 }
998 }
999
1000 req->analysers &= ~an_bit;
1001 req->analyse_exp = TICK_ETERNITY;
1002 return 1;
1003}
1004
1005/* This stream analyser works on a response. It applies all store rules on it
1006 * then returns 1. The data must already be present in the buffer otherwise
1007 * they won't match. It always returns 1.
1008 */
1009int process_store_rules(struct session *s, struct buffer *rep, int an_bit)
1010{
1011 struct proxy *px = s->be;
1012 struct sticking_rule *rule;
1013 int i;
1014
1015 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1016 now_ms, __FUNCTION__,
1017 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001018 rep,
1019 rep->rex, rep->wex,
1020 rep->flags,
1021 rep->l,
1022 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001023
1024 list_for_each_entry(rule, &px->storersp_rules, list) {
1025 int ret = 1 ;
1026 int storereqidx = -1;
1027
1028 for (i = 0; i < s->store_count; i++) {
1029 if (rule->table.t == s->store[i].table) {
1030 if (!(s->store[i].flags))
1031 storereqidx = i;
1032 break;
1033 }
1034 }
1035
1036 if ((i != s->store_count) && (storereqidx == -1))
1037 continue;
1038
1039 if (rule->cond) {
1040 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_RTR);
1041 ret = acl_pass(ret);
1042 if (rule->cond->pol == ACL_COND_UNLESS)
1043 ret = !ret;
1044 }
1045
1046 if (ret) {
1047 struct stktable_key *key;
1048
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001049 key = stktable_fetch_key(px, s, &s->txn, PATTERN_FETCH_RTR, rule->expr, rule->table.t->type);
Emeric Brun1d33b292010-01-04 15:47:17 +01001050 if (!key)
1051 continue;
1052
1053 if (storereqidx != -1) {
Willy Tarreau393379c2010-06-06 12:11:37 +02001054 stksess_setkey(s->store[storereqidx].table, s->store[storereqidx].ts, key);
Emeric Brun1d33b292010-01-04 15:47:17 +01001055 s->store[storereqidx].flags = 1;
1056 }
1057 else if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1058 struct stksess *ts;
1059
1060 ts = stksess_new(rule->table.t, key);
1061 if (ts) {
1062 s->store[s->store_count].table = rule->table.t;
1063 s->store[s->store_count].flags = 1;
1064 s->store[s->store_count++].ts = ts;
1065 }
1066 }
1067 }
1068 }
1069
1070 /* process store request and store response */
1071 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001072 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001073 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001074
1075 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1076 if (ts) {
1077 /* the entry already existed, we can free ours */
Willy Tarreaue8f63382010-07-13 15:20:24 +02001078 stktable_touch(s->store[i].table, ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001079 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001080 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001081 else
1082 ts = stktable_store(s->store[i].table, s->store[i].ts);
1083
1084 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001085 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
1086 stktable_data_cast(ptr, server_id) = s->srv->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001087 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001088 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001089
1090 rep->analysers &= ~an_bit;
1091 rep->analyse_exp = TICK_ETERNITY;
1092 return 1;
1093}
1094
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001095/* This macro is very specific to the function below. See the comments in
1096 * process_session() below to understand the logic and the tests.
1097 */
1098#define UPDATE_ANALYSERS(real, list, back, flag) { \
1099 list = (((list) & ~(flag)) | ~(back)) & (real); \
1100 back = real; \
1101 if (!(list)) \
1102 break; \
1103 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1104 continue; \
1105}
1106
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001107/* Processes the client, server, request and response jobs of a session task,
1108 * then puts it back to the wait queue in a clean state, or cleans up its
1109 * resources if it must be deleted. Returns in <next> the date the task wants
1110 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1111 * nothing too many times, the request and response buffers flags are monitored
1112 * and each function is called only if at least another function has changed at
1113 * least one flag it is interested in.
1114 */
Willy Tarreau26c25062009-03-08 09:38:41 +01001115struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001116{
1117 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001118 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001119 unsigned int rq_prod_last, rq_cons_last;
1120 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001121 unsigned int req_ana_back;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001122
1123 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
1124 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
1125
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001126 /* this data may be no longer valid, clear it */
1127 memset(&s->txn.auth, 0, sizeof(s->txn.auth));
1128
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001129 /* This flag must explicitly be set every time */
1130 s->req->flags &= ~BF_READ_NOEXP;
1131
1132 /* Keep a copy of req/rep flags so that we can detect shutdowns */
1133 rqf_last = s->req->flags;
1134 rpf_last = s->rep->flags;
1135
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001136 /* we don't want the stream interface functions to recursively wake us up */
1137 if (s->req->prod->owner == t)
1138 s->req->prod->flags |= SI_FL_DONT_WAKE;
1139 if (s->req->cons->owner == t)
1140 s->req->cons->flags |= SI_FL_DONT_WAKE;
1141
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001142 /* 1a: Check for low level timeouts if needed. We just set a flag on
1143 * stream interfaces when their timeouts have expired.
1144 */
1145 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
1146 stream_int_check_timeouts(&s->si[0]);
1147 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001148
1149 /* check buffer timeouts, and close the corresponding stream interfaces
1150 * for future reads or writes. Note: this will also concern upper layers
1151 * but we do not touch any other flag. We must be careful and correctly
1152 * detect state changes when calling them.
1153 */
1154
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001155 buffer_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001156
Willy Tarreau14641402009-12-29 14:49:56 +01001157 if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1158 s->req->cons->flags |= SI_FL_NOLINGER;
1159 s->req->cons->shutw(s->req->cons);
1160 }
1161
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001162 if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1163 s->req->prod->shutr(s->req->prod);
1164
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001165 buffer_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001166
Willy Tarreau14641402009-12-29 14:49:56 +01001167 if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1168 s->rep->cons->flags |= SI_FL_NOLINGER;
1169 s->rep->cons->shutw(s->rep->cons);
1170 }
1171
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001172 if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1173 s->rep->prod->shutr(s->rep->prod);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001174 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001175
1176 /* 1b: check for low-level errors reported at the stream interface.
1177 * First we check if it's a retryable error (in which case we don't
1178 * want to tell the buffer). Otherwise we report the error one level
1179 * upper by setting flags into the buffers. Note that the side towards
1180 * the client cannot have connect (hence retryable) errors. Also, the
1181 * connection setup code must be able to deal with any type of abort.
1182 */
1183 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
1184 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
1185 s->si[0].shutr(&s->si[0]);
1186 s->si[0].shutw(&s->si[0]);
1187 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001188 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreauae526782010-03-04 20:34:23 +01001189 s->be->counters.cli_aborts++;
1190 if (s->srv)
1191 s->srv->counters.cli_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001192 if (!(s->flags & SN_ERR_MASK))
1193 s->flags |= SN_ERR_CLICL;
1194 if (!(s->flags & SN_FINST_MASK))
1195 s->flags |= SN_FINST_D;
1196 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001197 }
1198 }
1199
1200 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
1201 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
1202 s->si[1].shutr(&s->si[1]);
1203 s->si[1].shutw(&s->si[1]);
1204 stream_int_report_error(&s->si[1]);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001205 s->be->counters.failed_resp++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001206 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001207 s->srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001208 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreauae526782010-03-04 20:34:23 +01001209 s->be->counters.srv_aborts++;
1210 if (s->srv)
1211 s->srv->counters.srv_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001212 if (!(s->flags & SN_ERR_MASK))
1213 s->flags |= SN_ERR_SRVCL;
1214 if (!(s->flags & SN_FINST_MASK))
1215 s->flags |= SN_FINST_D;
1216 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001217 }
1218 /* note: maybe we should process connection errors here ? */
1219 }
1220
1221 if (s->si[1].state == SI_ST_CON) {
1222 /* we were trying to establish a connection on the server side,
1223 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1224 */
1225 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
1226 sess_update_st_cer(s, &s->si[1]);
1227 else if (s->si[1].state == SI_ST_EST)
1228 sess_establish(s, &s->si[1]);
1229
1230 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1231 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1232 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1233 */
1234 }
1235
Willy Tarreau815a9b22010-07-27 17:15:12 +02001236 rq_prod_last = s->si[0].state;
1237 rq_cons_last = s->si[1].state;
1238 rp_cons_last = s->si[0].state;
1239 rp_prod_last = s->si[1].state;
1240
1241 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001242 /* Check for connection closure */
1243
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001244 DPRINTF(stderr,
1245 "[%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",
1246 now_ms, __FUNCTION__, __LINE__,
1247 t,
1248 s, s->flags,
1249 s->req, s->rep,
1250 s->req->rex, s->rep->wex,
1251 s->req->flags, s->rep->flags,
1252 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
1253 s->rep->cons->err_type, s->req->cons->err_type,
Willy Tarreauee28de02010-06-01 09:51:00 +02001254 s->req->cons->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001255
1256 /* nothing special to be done on client side */
1257 if (unlikely(s->req->prod->state == SI_ST_DIS))
1258 s->req->prod->state = SI_ST_CLO;
1259
1260 /* When a server-side connection is released, we have to count it and
1261 * check for pending connections on this server.
1262 */
1263 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
1264 s->req->cons->state = SI_ST_CLO;
1265 if (s->srv) {
1266 if (s->flags & SN_CURR_SESS) {
1267 s->flags &= ~SN_CURR_SESS;
1268 s->srv->cur_sess--;
1269 }
1270 sess_change_server(s, NULL);
1271 if (may_dequeue_tasks(s->srv, s->be))
1272 process_srv_queue(s->srv);
1273 }
1274 }
1275
1276 /*
1277 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1278 * at this point.
1279 */
1280
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001281 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001282 /* Analyse request */
1283 if ((s->req->flags & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001284 ((s->req->flags ^ rqf_last) & BF_MASK_STATIC) ||
1285 s->si[0].state != rq_prod_last ||
1286 s->si[1].state != rq_cons_last) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001287 unsigned int flags = s->req->flags;
1288
1289 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001290 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001291 unsigned int ana_list;
1292 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001293
Willy Tarreau90deb182010-01-07 00:20:41 +01001294 /* it's up to the analysers to stop new connections,
1295 * disable reading or closing. Note: if an analyser
1296 * disables any of these bits, it is responsible for
1297 * enabling them again when it disables itself, so
1298 * that other analysers are called in similar conditions.
1299 */
1300 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001301 buffer_auto_connect(s->req);
1302 buffer_auto_close(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001303
1304 /* We will call all analysers for which a bit is set in
1305 * s->req->analysers, following the bit order from LSB
1306 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001307 * the list when not needed. Any analyser may return 0
1308 * to break out of the loop, either because of missing
1309 * data to take a decision, or because it decides to
1310 * kill the session. We loop at least once through each
1311 * analyser, and we may loop again if other analysers
1312 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001313 *
1314 * We build a list of analysers to run. We evaluate all
1315 * of these analysers in the order of the lower bit to
1316 * the higher bit. This ordering is very important.
1317 * An analyser will often add/remove other analysers,
1318 * including itself. Any changes to itself have no effect
1319 * on the loop. If it removes any other analysers, we
1320 * want those analysers not to be called anymore during
1321 * this loop. If it adds an analyser that is located
1322 * after itself, we want it to be scheduled for being
1323 * processed during the loop. If it adds an analyser
1324 * which is located before it, we want it to switch to
1325 * it immediately, even if it has already been called
1326 * once but removed since.
1327 *
1328 * In order to achieve this, we compare the analyser
1329 * list after the call with a copy of it before the
1330 * call. The work list is fed with analyser bits that
1331 * appeared during the call. Then we compare previous
1332 * work list with the new one, and check the bits that
1333 * appeared. If the lowest of these bits is lower than
1334 * the current bit, it means we have enabled a previous
1335 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001336 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001337
1338 ana_list = ana_back = s->req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001339 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001340 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001341
Willy Tarreaufb356202010-08-03 14:02:05 +02001342 if (ana_list & AN_REQ_INSPECT_FE) {
1343 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001344 break;
Willy Tarreaufb356202010-08-03 14:02:05 +02001345 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001346 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001347
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001348 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001349 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001350 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001351 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001352 }
1353
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001354 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001355 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
1356 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001357 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001358 }
1359
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001360 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001361 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
1362 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001363 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001364 }
1365
Willy Tarreaufb356202010-08-03 14:02:05 +02001366 if (ana_list & AN_REQ_INSPECT_BE) {
1367 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_BE))
1368 break;
1369 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
1370 }
1371
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001372 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001373 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
1374 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001375 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001376 }
1377
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001378 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001379 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001380 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001381 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001382 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001383
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001384 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreauc465fd72009-08-31 00:17:18 +02001385 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
1386 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001387 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001388 }
1389
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001390 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001391 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001392 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001393 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001394 }
Emeric Brun647caf12009-06-30 17:57:00 +02001395
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001396 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Emeric Brun647caf12009-06-30 17:57:00 +02001397 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
1398 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001399 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001400 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001401
Emeric Brun1d33b292010-01-04 15:47:17 +01001402 if (ana_list & AN_REQ_STICKING_RULES) {
1403 if (!process_sticking_rules(s, s->req, AN_REQ_STICKING_RULES))
1404 break;
1405 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
1406 }
1407
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001408 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001409 if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY))
1410 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001411 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001412 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001413 break;
1414 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001415 }
Willy Tarreau84455332009-03-15 22:34:05 +01001416
Willy Tarreau815a9b22010-07-27 17:15:12 +02001417 rq_prod_last = s->si[0].state;
1418 rq_cons_last = s->si[1].state;
1419 rqf_last = s->req->flags;
1420
1421 if ((s->req->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001422 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001423 }
1424
Willy Tarreau576507f2010-01-07 00:09:04 +01001425 /* we'll monitor the request analysers while parsing the response,
1426 * because some response analysers may indirectly enable new request
1427 * analysers (eg: HTTP keep-alive).
1428 */
1429 req_ana_back = s->req->analysers;
1430
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001431 resync_response:
1432 /* Analyse response */
1433
1434 if (unlikely(s->rep->flags & BF_HIJACK)) {
1435 /* In inject mode, we wake up everytime something has
1436 * happened on the write side of the buffer.
1437 */
1438 unsigned int flags = s->rep->flags;
1439
1440 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
1441 !(s->rep->flags & BF_FULL)) {
1442 s->rep->hijacker(s, s->rep);
1443 }
1444
1445 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
1446 rpf_last = s->rep->flags;
1447 goto resync_response;
1448 }
1449 }
1450 else if ((s->rep->flags & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001451 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC ||
1452 s->si[0].state != rp_cons_last ||
1453 s->si[1].state != rp_prod_last) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001454 unsigned int flags = s->rep->flags;
1455
1456 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001457 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001458 unsigned int ana_list;
1459 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001460
Willy Tarreau90deb182010-01-07 00:20:41 +01001461 /* it's up to the analysers to stop disable reading or
1462 * closing. Note: if an analyser disables any of these
1463 * bits, it is responsible for enabling them again when
1464 * it disables itself, so that other analysers are called
1465 * in similar conditions.
1466 */
1467 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001468 buffer_auto_close(s->rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001469
1470 /* We will call all analysers for which a bit is set in
1471 * s->rep->analysers, following the bit order from LSB
1472 * to MSB. The analysers must remove themselves from
1473 * the list when not needed. Any analyser may return 0
1474 * to break out of the loop, either because of missing
1475 * data to take a decision, or because it decides to
1476 * kill the session. We loop at least once through each
1477 * analyser, and we may loop again if other analysers
1478 * are added in the middle.
1479 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001480
1481 ana_list = ana_back = s->rep->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001482 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001483 /* Warning! ensure that analysers are always placed in ascending order! */
1484
1485 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001486 if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP))
1487 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001488 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001489 }
1490
Emeric Brun1d33b292010-01-04 15:47:17 +01001491 if (ana_list & AN_RES_STORE_RULES) {
1492 if (!process_store_rules(s, s->rep, AN_RES_STORE_RULES))
1493 break;
1494 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
1495 }
1496
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001497 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001498 if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be))
1499 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001500 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001501 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001502
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001503 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001504 if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY))
1505 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001506 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001507 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001508 break;
1509 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001510 }
1511
Willy Tarreau815a9b22010-07-27 17:15:12 +02001512 rp_cons_last = s->si[0].state;
1513 rp_prod_last = s->si[1].state;
1514 rpf_last = s->rep->flags;
1515
1516 if ((s->rep->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001517 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001518 }
1519
Willy Tarreau576507f2010-01-07 00:09:04 +01001520 /* maybe someone has added some request analysers, so we must check and loop */
1521 if (s->req->analysers & ~req_ana_back)
1522 goto resync_request;
1523
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001524 /* FIXME: here we should call protocol handlers which rely on
1525 * both buffers.
1526 */
1527
1528
1529 /*
Willy Tarreauae526782010-03-04 20:34:23 +01001530 * Now we propagate unhandled errors to the session. Normally
1531 * we're just in a data phase here since it means we have not
1532 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001533 */
1534 if (!(s->flags & SN_ERR_MASK)) {
1535 if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1536 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +01001537 s->req->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001538 if (s->req->flags & BF_READ_ERROR) {
1539 s->be->counters.cli_aborts++;
1540 if (s->srv)
1541 s->srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001542 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001543 }
1544 else if (s->req->flags & BF_READ_TIMEOUT) {
1545 s->be->counters.cli_aborts++;
1546 if (s->srv)
1547 s->srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001548 s->flags |= SN_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001549 }
1550 else if (s->req->flags & BF_WRITE_ERROR) {
1551 s->be->counters.srv_aborts++;
1552 if (s->srv)
1553 s->srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001554 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001555 }
1556 else {
1557 s->be->counters.srv_aborts++;
1558 if (s->srv)
1559 s->srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001560 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001561 }
Willy Tarreau84455332009-03-15 22:34:05 +01001562 sess_set_term_flags(s);
1563 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001564 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1565 /* Report it if the server got an error or a read timeout expired */
1566 s->rep->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001567 if (s->rep->flags & BF_READ_ERROR) {
1568 s->be->counters.srv_aborts++;
1569 if (s->srv)
1570 s->srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001571 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001572 }
1573 else if (s->rep->flags & BF_READ_TIMEOUT) {
1574 s->be->counters.srv_aborts++;
1575 if (s->srv)
1576 s->srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001577 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001578 }
1579 else if (s->rep->flags & BF_WRITE_ERROR) {
1580 s->be->counters.cli_aborts++;
1581 if (s->srv)
1582 s->srv->counters.cli_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001583 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001584 }
1585 else {
1586 s->be->counters.cli_aborts++;
1587 if (s->srv)
1588 s->srv->counters.cli_aborts++;
1589 s->flags |= SN_ERR_CLITO;
1590 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001591 sess_set_term_flags(s);
1592 }
Willy Tarreau84455332009-03-15 22:34:05 +01001593 }
1594
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001595 /*
1596 * Here we take care of forwarding unhandled data. This also includes
1597 * connection establishments and shutdown requests.
1598 */
1599
1600
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001601 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001602 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001603 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001604 if (!s->req->analysers &&
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001605 !(s->req->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTW_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001606 (s->req->prod->state >= SI_ST_EST) &&
1607 (s->req->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001608 /* This buffer is freewheeling, there's no analyser nor hijacker
1609 * attached to it. If any data are left in, we'll permit them to
1610 * move.
1611 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001612 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001613 buffer_auto_connect(s->req);
1614 buffer_auto_close(s->req);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001615 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001616
Willy Tarreau31971e52009-09-20 12:07:52 +02001617 /* If the producer is still connected, we'll enable data to flow
1618 * from the producer to the consumer (which might possibly not be
1619 * connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001620 */
Willy Tarreau31971e52009-09-20 12:07:52 +02001621 if (!(s->req->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW)))
1622 buffer_forward(s->req, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001623 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001624
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001625 /* check if it is wise to enable kernel splicing to forward request data */
1626 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1627 s->req->to_forward &&
1628 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001629 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001630 (pipes_used < global.maxpipes) &&
1631 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
1632 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1633 (s->req->flags & BF_STREAMER_FAST)))) {
1634 s->req->flags |= BF_KERN_SPLICING;
1635 }
1636
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001637 /* reflect what the L7 analysers have seen last */
1638 rqf_last = s->req->flags;
1639
1640 /*
1641 * Now forward all shutdown requests between both sides of the buffer
1642 */
1643
Willy Tarreau520d95e2009-09-19 21:04:57 +02001644 /* first, let's check if the request buffer needs to shutdown(write), which may
1645 * happen either because the input is closed or because we want to force a close
Willy Tarreaue4599762010-03-21 23:25:09 +01001646 * once the server has begun to respond.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001647 */
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001648 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
Willy Tarreaue4599762010-03-21 23:25:09 +01001649 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001650 buffer_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001651
1652 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001653 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 +01001654 s->req->cons->shutw(s->req->cons);
1655
1656 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001657 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
1658 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001659 buffer_shutr_now(s->req);
1660
1661 /* shutdown(read) pending */
1662 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1663 s->req->prod->shutr(s->req->prod);
1664
Willy Tarreau520d95e2009-09-19 21:04:57 +02001665 /* it's possible that an upper layer has requested a connection setup or abort.
1666 * There are 2 situations where we decide to establish a new connection :
1667 * - there are data scheduled for emission in the buffer
1668 * - the BF_AUTO_CONNECT flag is set (active connection)
1669 */
1670 if (s->req->cons->state == SI_ST_INI) {
Willy Tarreaue4599762010-03-21 23:25:09 +01001671 if (!(s->req->flags & BF_SHUTW)) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001672 if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) {
Willy Tarreau85e7d002010-05-31 11:57:51 +02001673 /* If we have an iohandler without a connect method, we immediately
1674 * switch to the connected state, otherwise we perform a connection
1675 * request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001676 */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001677 s->req->cons->state = SI_ST_REQ; /* new connection requested */
Willy Tarreau070ceb62010-06-01 10:36:43 +02001678 s->req->cons->conn_retries = s->be->conn_retries;
Willy Tarreau85e7d002010-05-31 11:57:51 +02001679 if (unlikely(s->req->cons->iohandler && !s->req->cons->connect)) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02001680 s->req->cons->state = SI_ST_EST; /* connection established */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001681 s->rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
1682 s->req->wex = TICK_ETERNITY;
1683 }
Willy Tarreau520d95e2009-09-19 21:04:57 +02001684 }
Willy Tarreau73201222009-08-16 18:27:24 +02001685 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001686 else {
Willy Tarreau92795622009-03-06 12:51:23 +01001687 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001688 buffer_shutw_now(s->req); /* fix buffer flags upon abort */
1689 buffer_shutr_now(s->rep);
1690 }
Willy Tarreau92795622009-03-06 12:51:23 +01001691 }
1692
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001693
1694 /* we may have a pending connection request, or a connection waiting
1695 * for completion.
1696 */
1697 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
1698 do {
1699 /* nb: step 1 might switch from QUE to ASS, but we first want
1700 * to give a chance to step 2 to perform a redirect if needed.
1701 */
1702 if (s->si[1].state != SI_ST_REQ)
1703 sess_update_stream_int(s, &s->si[1]);
1704 if (s->si[1].state == SI_ST_REQ)
1705 sess_prepare_conn_req(s, &s->si[1]);
1706
1707 if (s->si[1].state == SI_ST_ASS && s->srv &&
1708 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
1709 perform_http_redirect(s, &s->si[1]);
1710 } while (s->si[1].state == SI_ST_ASS);
1711 }
1712
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001713 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001714 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001715 goto resync_stream_interface;
1716
Willy Tarreau815a9b22010-07-27 17:15:12 +02001717 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001718 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001719 goto resync_request;
1720
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001721 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01001722
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001723 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001724 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001725 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001726 if (!s->rep->analysers &&
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001727 !(s->rep->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTW_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001728 (s->rep->prod->state >= SI_ST_EST) &&
1729 (s->rep->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001730 /* This buffer is freewheeling, there's no analyser nor hijacker
1731 * attached to it. If any data are left in, we'll permit them to
1732 * move.
1733 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001734 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001735 buffer_auto_close(s->rep);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001736 buffer_flush(s->rep);
Willy Tarreau31971e52009-09-20 12:07:52 +02001737 if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW)))
1738 buffer_forward(s->rep, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001739 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001740
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001741 /* check if it is wise to enable kernel splicing to forward response data */
1742 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1743 s->rep->to_forward &&
1744 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001745 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001746 (pipes_used < global.maxpipes) &&
1747 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
1748 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1749 (s->rep->flags & BF_STREAMER_FAST)))) {
1750 s->rep->flags |= BF_KERN_SPLICING;
1751 }
1752
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001753 /* reflect what the L7 analysers have seen last */
1754 rpf_last = s->rep->flags;
1755
1756 /*
1757 * Now forward all shutdown requests between both sides of the buffer
1758 */
1759
1760 /*
1761 * FIXME: this is probably where we should produce error responses.
1762 */
1763
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001764 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001765 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
1766 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001767 buffer_shutw_now(s->rep);
1768
1769 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001770 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 +01001771 s->rep->cons->shutw(s->rep->cons);
1772
1773 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001774 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
1775 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001776 buffer_shutr_now(s->rep);
1777
1778 /* shutdown(read) pending */
1779 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1780 s->rep->prod->shutr(s->rep->prod);
1781
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001782 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001783 goto resync_stream_interface;
1784
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001785 if (s->req->flags != rqf_last)
1786 goto resync_request;
1787
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001788 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001789 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001790
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001791 /* we're interested in getting wakeups again */
1792 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
1793 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
1794
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001795 /* This is needed only when debugging is enabled, to indicate
1796 * client-side or server-side close. Please note that in the unlikely
1797 * event where both sides would close at once, the sequence is reported
1798 * on the server side first.
1799 */
1800 if (unlikely((global.mode & MODE_DEBUG) &&
1801 (!(global.mode & MODE_QUIET) ||
1802 (global.mode & MODE_VERBOSE)))) {
1803 int len;
1804
1805 if (s->si[1].state == SI_ST_CLO &&
1806 s->si[1].prev_state == SI_ST_EST) {
1807 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1808 s->uniq_id, s->be->id,
1809 (unsigned short)s->si[0].fd,
1810 (unsigned short)s->si[1].fd);
1811 write(1, trash, len);
1812 }
1813
1814 if (s->si[0].state == SI_ST_CLO &&
1815 s->si[0].prev_state == SI_ST_EST) {
1816 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1817 s->uniq_id, s->be->id,
1818 (unsigned short)s->si[0].fd,
1819 (unsigned short)s->si[1].fd);
1820 write(1, trash, len);
1821 }
1822 }
1823
1824 if (likely((s->rep->cons->state != SI_ST_CLO) ||
1825 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
1826
1827 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1828 session_process_counters(s);
1829
Willy Tarreau1accfc02009-09-05 20:57:35 +02001830 if (s->rep->cons->state == SI_ST_EST && !s->rep->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001831 s->rep->cons->update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001832
Willy Tarreau1accfc02009-09-05 20:57:35 +02001833 if (s->req->cons->state == SI_ST_EST && !s->req->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001834 s->req->cons->update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001835
Willy Tarreaua6eebb32010-06-04 11:40:20 +02001836 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL|BF_READ_ATTACHED);
1837 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 +01001838 s->si[0].prev_state = s->si[0].state;
1839 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01001840 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
1841 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001842
1843 /* Trick: if a request is being waiting for the server to respond,
1844 * and if we know the server can timeout, we don't want the timeout
1845 * to expire on the client side first, but we're still interested
1846 * in passing data from the client to the server (eg: POST). Thus,
1847 * we can cancel the client's request timeout if the server's
1848 * request timeout is set and the server has not yet sent a response.
1849 */
1850
Willy Tarreau520d95e2009-09-19 21:04:57 +02001851 if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01001852 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
1853 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001854 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01001855 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001856
Willy Tarreau7a20aa62010-07-13 16:30:45 +02001857 /* Call the stream interfaces' I/O handlers when embedded.
Willy Tarreau1accfc02009-09-05 20:57:35 +02001858 * Note that this one may wake the task up again.
1859 */
Willy Tarreau7a20aa62010-07-13 16:30:45 +02001860 if (s->req->cons->iohandler || s->rep->cons->iohandler) {
1861 if (s->req->cons->iohandler)
1862 s->req->cons->iohandler(s->req->cons);
1863 if (s->rep->cons->iohandler)
1864 s->rep->cons->iohandler(s->rep->cons);
Willy Tarreau1accfc02009-09-05 20:57:35 +02001865 if (task_in_rq(t)) {
1866 /* If we woke up, we don't want to requeue the
1867 * task to the wait queue, but rather requeue
1868 * it into the runqueue ASAP.
1869 */
1870 t->expire = TICK_ETERNITY;
1871 return t;
1872 }
1873 }
1874
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001875 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
1876 tick_first(s->rep->rex, s->rep->wex));
1877 if (s->req->analysers)
1878 t->expire = tick_first(t->expire, s->req->analyse_exp);
1879
1880 if (s->si[0].exp)
1881 t->expire = tick_first(t->expire, s->si[0].exp);
1882
1883 if (s->si[1].exp)
1884 t->expire = tick_first(t->expire, s->si[1].exp);
1885
1886#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01001887 fprintf(stderr,
1888 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
1889 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
1890 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
1891 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 +01001892#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001893
1894#ifdef DEBUG_DEV
1895 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01001896 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001897 ABORT_NOW();
1898#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01001899 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001900 }
1901
1902 s->fe->feconn--;
1903 if (s->flags & SN_BE_ASSIGNED)
1904 s->be->beconn--;
1905 actconn--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02001906 s->listener->nbconn--;
1907 if (s->listener->state == LI_FULL &&
1908 s->listener->nbconn < s->listener->maxconn) {
1909 /* we should reactivate the listener */
1910 EV_FD_SET(s->listener->fd, DIR_RD);
1911 s->listener->state = LI_READY;
1912 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001913
1914 if (unlikely((global.mode & MODE_DEBUG) &&
1915 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1916 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001917 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001918 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001919 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001920 write(1, trash, len);
1921 }
1922
1923 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
1924 session_process_counters(s);
1925
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001926 if (s->txn.status) {
1927 int n;
1928
1929 n = s->txn.status / 100;
1930 if (n < 1 || n > 5)
1931 n = 0;
1932
1933 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau24657792010-02-26 10:30:28 +01001934 s->fe->counters.fe.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001935
Willy Tarreau24657792010-02-26 10:30:28 +01001936 if ((s->flags & SN_BE_ASSIGNED) &&
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001937 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau24657792010-02-26 10:30:28 +01001938 s->be->counters.be.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001939 }
1940
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001941 /* let's do a final log if we need it */
1942 if (s->logs.logwait &&
1943 !(s->flags & SN_MONITOR) &&
1944 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001945 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001946 }
1947
1948 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001949 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01001950 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001951 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01001952 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001953}
1954
Willy Tarreau7c669d72008-06-20 15:04:11 +02001955/*
1956 * This function adjusts sess->srv_conn and maintains the previous and new
1957 * server's served session counts. Setting newsrv to NULL is enough to release
1958 * current connection slot. This function also notifies any LB algo which might
1959 * expect to be informed about any change in the number of active sessions on a
1960 * server.
1961 */
1962void sess_change_server(struct session *sess, struct server *newsrv)
1963{
1964 if (sess->srv_conn == newsrv)
1965 return;
1966
1967 if (sess->srv_conn) {
1968 sess->srv_conn->served--;
1969 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
1970 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
1971 sess->srv_conn = NULL;
1972 }
1973
1974 if (newsrv) {
1975 newsrv->served++;
1976 if (newsrv->proxy->lbprm.server_take_conn)
1977 newsrv->proxy->lbprm.server_take_conn(newsrv);
1978 sess->srv_conn = newsrv;
1979 }
1980}
1981
Willy Tarreau84455332009-03-15 22:34:05 +01001982/* Set correct session termination flags in case no analyser has done it. It
1983 * also counts a failed request if the server state has not reached the request
1984 * stage.
1985 */
1986void sess_set_term_flags(struct session *s)
1987{
1988 if (!(s->flags & SN_FINST_MASK)) {
1989 if (s->si[1].state < SI_ST_REQ) {
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001990
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001991 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001992 if (s->listener->counters)
1993 s->listener->counters->failed_req++;
1994
Willy Tarreau84455332009-03-15 22:34:05 +01001995 s->flags |= SN_FINST_R;
1996 }
1997 else if (s->si[1].state == SI_ST_QUE)
1998 s->flags |= SN_FINST_Q;
1999 else if (s->si[1].state < SI_ST_EST)
2000 s->flags |= SN_FINST_C;
Willy Tarreau033b2db2010-03-04 17:54:21 +01002001 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreau84455332009-03-15 22:34:05 +01002002 s->flags |= SN_FINST_D;
2003 else
2004 s->flags |= SN_FINST_L;
2005 }
2006}
2007
2008/* Handle server-side errors for default protocols. It is called whenever a a
2009 * connection setup is aborted or a request is aborted in queue. It sets the
2010 * session termination flags so that the caller does not have to worry about
2011 * them. It's installed as ->srv_error for the server-side stream_interface.
2012 */
2013void default_srv_error(struct session *s, struct stream_interface *si)
2014{
2015 int err_type = si->err_type;
2016 int err = 0, fin = 0;
2017
2018 if (err_type & SI_ET_QUEUE_ABRT) {
2019 err = SN_ERR_CLICL;
2020 fin = SN_FINST_Q;
2021 }
2022 else if (err_type & SI_ET_CONN_ABRT) {
2023 err = SN_ERR_CLICL;
2024 fin = SN_FINST_C;
2025 }
2026 else if (err_type & SI_ET_QUEUE_TO) {
2027 err = SN_ERR_SRVTO;
2028 fin = SN_FINST_Q;
2029 }
2030 else if (err_type & SI_ET_QUEUE_ERR) {
2031 err = SN_ERR_SRVCL;
2032 fin = SN_FINST_Q;
2033 }
2034 else if (err_type & SI_ET_CONN_TO) {
2035 err = SN_ERR_SRVTO;
2036 fin = SN_FINST_C;
2037 }
2038 else if (err_type & SI_ET_CONN_ERR) {
2039 err = SN_ERR_SRVCL;
2040 fin = SN_FINST_C;
2041 }
2042 else /* SI_ET_CONN_OTHER and others */ {
2043 err = SN_ERR_INTERNAL;
2044 fin = SN_FINST_C;
2045 }
2046
2047 if (!(s->flags & SN_ERR_MASK))
2048 s->flags |= err;
2049 if (!(s->flags & SN_FINST_MASK))
2050 s->flags |= fin;
2051}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002052
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002053
Willy Tarreau8b22a712010-06-18 17:46:06 +02002054/************************************************************************/
2055/* All supported ACL keywords must be declared here. */
2056/************************************************************************/
2057
2058/* set test->i to the number of connections from the session's source address
2059 * in the table pointed to by expr.
2060 */
2061static int
2062acl_fetch_src_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2063 struct acl_expr *expr, struct acl_test *test)
2064{
2065 struct stksess *ts;
2066 struct stktable_key *key;
2067
2068 key = tcpv4_src_to_stktable_key(l4);
2069 if (!key)
2070 return 0; /* only TCPv4 is supported right now */
2071
2072 if (expr->arg_len)
2073 px = find_stktable(expr->arg.str);
2074
2075 if (!px)
2076 return 0; /* table not found */
2077
2078 test->flags = ACL_TEST_F_VOL_TEST;
2079 test->i = 0;
2080 if ((ts = stktable_lookup_key(&px->table, key)) != NULL) {
2081 void *ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2082 if (!ptr)
2083 return 0; /* parameter not stored */
2084 test->i = stktable_data_cast(ptr, conn_cnt);
2085 }
2086
2087 return 1;
2088}
2089
2090/* set test->i to the number of connections from the session's source address
2091 * in the table pointed to by expr, after updating it.
2092 */
2093static int
2094acl_fetch_src_updt_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2095 struct acl_expr *expr, struct acl_test *test)
2096{
2097 struct stksess *ts;
2098 struct stktable_key *key;
2099 void *ptr;
2100
2101 key = tcpv4_src_to_stktable_key(l4);
2102 if (!key)
2103 return 0; /* only TCPv4 is supported right now */
2104
2105 if (expr->arg_len)
2106 px = find_stktable(expr->arg.str);
2107
2108 if (!px)
2109 return 0; /* table not found */
2110
2111 if ((ts = stktable_lookup_key(&px->table, key)) == NULL) {
2112 /* entry does not exist, initialize a new one */
2113 ts = stksess_new(&px->table, key);
2114 if (!ts)
2115 return 0;
2116 stktable_store(&px->table, ts);
2117 }
2118 else
2119 stktable_touch(&px->table, ts);
2120
2121 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2122 if (!ptr)
2123 return 0; /* parameter not stored in this table */
2124
2125 test->i = ++stktable_data_cast(ptr, conn_cnt);
2126 test->flags = ACL_TEST_F_VOL_TEST;
2127 return 1;
2128}
2129
Willy Tarreau38285c12010-06-18 16:35:43 +02002130/* set test->i to the number of concurrent connections from the session's source
2131 * address in the table pointed to by expr.
2132 */
2133static int
2134acl_fetch_src_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2135 struct acl_expr *expr, struct acl_test *test)
2136{
2137 struct stksess *ts;
2138 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
2150 test->flags = ACL_TEST_F_VOL_TEST;
2151 test->i = 0;
2152
2153 if ((ts = stktable_lookup_key(&px->table, key)) != NULL) {
2154 void *ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CUR);
2155 if (!ptr)
2156 return 0; /* parameter not stored */
2157 test->i = stktable_data_cast(ptr, conn_cur);
2158 }
2159
2160 return 1;
2161}
2162
Willy Tarreau8b22a712010-06-18 17:46:06 +02002163
2164/* Note: must not be declared <const> as its list will be overwritten */
2165static struct acl_kw_list acl_kws = {{ },{
2166 { "src_conn_cnt", acl_parse_int, acl_fetch_src_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
2167 { "src_updt_conn_cnt", acl_parse_int, acl_fetch_src_updt_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
Willy Tarreau38285c12010-06-18 16:35:43 +02002168 { "src_conn_cur", acl_parse_int, acl_fetch_src_conn_cur, acl_match_int, ACL_USE_TCP4_VOLATILE },
Willy Tarreau8b22a712010-06-18 17:46:06 +02002169 { NULL, NULL, NULL, NULL },
2170}};
2171
2172
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002173/* Parse a "track-counters" line starting with "track-counters" in args[arg-1].
2174 * Returns the number of warnings emitted, or -1 in case of fatal errors. The
2175 * <prm> struct is fed with the table name if any. If unspecified, the caller
2176 * will assume that the current proxy's table is used.
2177 */
2178int parse_track_counters(char **args, int *arg,
2179 int section_type, struct proxy *curpx,
2180 struct track_ctr_prm *prm,
2181 struct proxy *defpx, char *err, int errlen)
2182{
2183 int pattern_type = 0;
2184
2185 /* parse the arguments of "track-counters" before the condition in the
2186 * following form :
2187 * track-counters src [ table xxx ] [ if|unless ... ]
2188 */
2189 while (args[*arg]) {
2190 if (strcmp(args[*arg], "src") == 0) {
2191 prm->type = STKTABLE_TYPE_IP;
2192 pattern_type = 1;
2193 }
2194 else if (strcmp(args[*arg], "table") == 0) {
2195 if (!args[*arg + 1]) {
2196 snprintf(err, errlen,
2197 "missing table for track-counter in %s '%s'.",
2198 proxy_type_str(curpx), curpx->id);
2199 return -1;
2200 }
2201 /* we copy the table name for now, it will be resolved later */
2202 prm->table.n = strdup(args[*arg + 1]);
2203 (*arg)++;
2204 }
2205 else {
2206 /* unhandled keywords are handled by the caller */
2207 break;
2208 }
2209 (*arg)++;
2210 }
2211
2212 if (!pattern_type) {
2213 snprintf(err, errlen,
2214 "track-counter key not specified in %s '%s' (found %s, only 'src' is supported).",
2215 proxy_type_str(curpx), curpx->id, quote_arg(args[*arg]));
2216 return -1;
2217 }
2218
2219 return 0;
2220}
2221
Willy Tarreau8b22a712010-06-18 17:46:06 +02002222__attribute__((constructor))
2223static void __session_init(void)
2224{
2225 acl_register_keywords(&acl_kws);
2226}
2227
Willy Tarreaubaaee002006-06-26 02:48:02 +02002228/*
2229 * Local variables:
2230 * c-indent-level: 8
2231 * c-basic-offset: 8
2232 * End:
2233 */