blob: ed854d981acf60a465d5b58d78c85b34836c6941 [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);
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200137 if (s->tracked_counters) {
138 void *ptr = stktable_data_ptr(s->tracked_table, s->tracked_counters, STKTABLE_DT_SESS_CNT);
139 if (ptr)
140 stktable_data_cast(ptr, sess_cnt)++;
141 }
Willy Tarreaub36b4242010-06-04 20:59:39 +0200142
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200143 /* this part should be common with other protocols */
144 s->si[0].fd = cfd;
145 s->si[0].owner = t;
146 s->si[0].state = s->si[0].prev_state = SI_ST_EST;
147 s->si[0].err_type = SI_ET_NONE;
148 s->si[0].err_loc = NULL;
149 s->si[0].connect = NULL;
150 s->si[0].iohandler = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200151 s->si[0].release = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200152 s->si[0].exp = TICK_ETERNITY;
153 s->si[0].flags = SI_FL_NONE;
154
155 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
156 s->si[0].flags |= SI_FL_INDEP_STR;
157
158 if (addr->ss_family == AF_INET || addr->ss_family == AF_INET6)
159 s->si[0].flags = SI_FL_CAP_SPLTCP; /* TCP/TCPv6 splicing possible */
160
161 /* add the various callbacks */
162 stream_sock_prepare_interface(&s->si[0]);
163
164 /* pre-initialize the other side's stream interface to an INIT state. The
165 * callbacks will be initialized before attempting to connect.
166 */
167 s->si[1].fd = -1; /* just to help with debugging */
168 s->si[1].owner = t;
169 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
170 s->si[1].err_type = SI_ET_NONE;
171 s->si[1].err_loc = NULL;
172 s->si[1].connect = NULL;
173 s->si[1].iohandler = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200174 s->si[1].release = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200175 s->si[1].shutr = stream_int_shutr;
176 s->si[1].shutw = stream_int_shutw;
177 s->si[1].exp = TICK_ETERNITY;
178 s->si[1].flags = SI_FL_NONE;
179
180 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
181 s->si[1].flags |= SI_FL_INDEP_STR;
182
183 s->srv = s->prev_srv = s->srv_conn = NULL;
184 s->pend_pos = NULL;
185
186 /* init store persistence */
187 s->store_count = 0;
188
189 /* Adjust some socket options */
190 if (unlikely(fcntl(cfd, F_SETFL, O_NONBLOCK) == -1)) {
191 Alert("accept(): cannot set the socket in non blocking mode. Giving up\n");
192 goto out_free_task;
193 }
194
195 txn = &s->txn;
196 /* Those variables will be checked and freed if non-NULL in
197 * session.c:session_free(). It is important that they are
198 * properly initialized.
199 */
200 txn->sessid = NULL;
201 txn->srv_cookie = NULL;
202 txn->cli_cookie = NULL;
203 txn->uri = NULL;
204 txn->req.cap = NULL;
205 txn->rsp.cap = NULL;
206 txn->hdr_idx.v = NULL;
207 txn->hdr_idx.size = txn->hdr_idx.used = 0;
208
209 if (unlikely((s->req = pool_alloc2(pool2_buffer)) == NULL))
210 goto out_free_task; /* no memory */
211
212 if (unlikely((s->rep = pool_alloc2(pool2_buffer)) == NULL))
213 goto out_free_req; /* no memory */
214
215 /* initialize the request buffer */
216 s->req->size = global.tune.bufsize;
217 buffer_init(s->req);
218 s->req->prod = &s->si[0];
219 s->req->cons = &s->si[1];
220 s->si[0].ib = s->si[1].ob = s->req;
221 s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */
222
223 /* activate default analysers enabled for this listener */
224 s->req->analysers = l->analysers;
225
226 s->req->wto = TICK_ETERNITY;
227 s->req->rto = TICK_ETERNITY;
228 s->req->rex = TICK_ETERNITY;
229 s->req->wex = TICK_ETERNITY;
230 s->req->analyse_exp = TICK_ETERNITY;
231
232 /* initialize response buffer */
233 s->rep->size = global.tune.bufsize;
234 buffer_init(s->rep);
235 s->rep->prod = &s->si[1];
236 s->rep->cons = &s->si[0];
237 s->si[0].ob = s->si[1].ib = s->rep;
238 s->rep->analysers = 0;
239
240 s->rep->rto = TICK_ETERNITY;
241 s->rep->wto = TICK_ETERNITY;
242 s->rep->rex = TICK_ETERNITY;
243 s->rep->wex = TICK_ETERNITY;
244 s->rep->analyse_exp = TICK_ETERNITY;
245
246 /* finish initialization of the accepted file descriptor */
247 fd_insert(cfd);
248 fdtab[cfd].owner = &s->si[0];
249 fdtab[cfd].state = FD_STREADY;
250 fdtab[cfd].flags = 0;
251 fdtab[cfd].cb[DIR_RD].f = l->proto->read;
252 fdtab[cfd].cb[DIR_RD].b = s->req;
253 fdtab[cfd].cb[DIR_WR].f = l->proto->write;
254 fdtab[cfd].cb[DIR_WR].b = s->rep;
255 fdinfo[cfd].peeraddr = (struct sockaddr *)&s->cli_addr;
256 fdinfo[cfd].peerlen = sizeof(s->cli_addr);
257 EV_FD_SET(cfd, DIR_RD);
258
259 if (p->accept) {
260 int ret = p->accept(s);
261 if (unlikely(ret < 0))
262 goto out_free_rep;
263
264 if (unlikely(ret == 0)) {
265 /* work is finished, we can release everything (eg: monitoring) */
266 pool_free2(pool2_buffer, s->rep);
267 pool_free2(pool2_buffer, s->req);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200268 if (s->tracked_counters)
269 session_store_counters(s);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200270 task_free(t);
271 LIST_DEL(&s->list);
272 pool_free2(pool2_session, s);
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200273 p->feconn--;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200274 return 0;
275 }
276 }
277
278 /* it is important not to call the wakeup function directly but to
279 * pass through task_wakeup(), because this one knows how to apply
280 * priorities to tasks.
281 */
282 task_wakeup(t, TASK_WOKEN_INIT);
283 return 1;
284
285 /* Error unrolling */
286 out_free_rep:
287 pool_free2(pool2_buffer, s->rep);
288 out_free_req:
289 pool_free2(pool2_buffer, s->req);
290 out_free_task:
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200291 p->feconn--;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200292 if (s->tracked_counters)
293 session_store_counters(s);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200294 task_free(t);
295 out_free_session:
296 LIST_DEL(&s->list);
297 pool_free2(pool2_session, s);
298 out_close:
299 return -1;
300}
301
Willy Tarreaubaaee002006-06-26 02:48:02 +0200302/*
303 * frees the context associated to a session. It must have been removed first.
304 */
305void session_free(struct session *s)
306{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +0100307 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +0200308 struct proxy *fe = s->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100309 struct bref *bref, *back;
Willy Tarreaua4cda672010-06-06 18:28:49 +0200310 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100311
Willy Tarreaubaaee002006-06-26 02:48:02 +0200312 if (s->pend_pos)
313 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100314
Willy Tarreau1e62de62008-11-11 20:20:02 +0100315 if (s->srv) { /* there may be requests left pending in queue */
316 if (s->flags & SN_CURR_SESS) {
317 s->flags &= ~SN_CURR_SESS;
318 s->srv->cur_sess--;
319 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100320 if (may_dequeue_tasks(s->srv, s->be))
321 process_srv_queue(s->srv);
Willy Tarreau1e62de62008-11-11 20:20:02 +0100322 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100323
Willy Tarreau7c669d72008-06-20 15:04:11 +0200324 if (unlikely(s->srv_conn)) {
325 /* the session still has a reserved slot on a server, but
326 * it should normally be only the same as the one above,
327 * so this should not happen in fact.
328 */
329 sess_change_server(s, NULL);
330 }
331
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100332 if (s->req->pipe)
333 put_pipe(s->req->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100334
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100335 if (s->rep->pipe)
336 put_pipe(s->rep->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100337
Willy Tarreau48d63db2008-08-03 17:41:33 +0200338 pool_free2(pool2_buffer, s->req);
339 pool_free2(pool2_buffer, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200340
Willy Tarreau46023632010-01-07 22:51:47 +0100341 http_end_txn(s);
342
Willy Tarreaua4cda672010-06-06 18:28:49 +0200343 for (i = 0; i < s->store_count; i++) {
344 if (!s->store[i].ts)
345 continue;
346 stksess_free(s->store[i].table, s->store[i].ts);
347 s->store[i].ts = NULL;
348 }
349
Willy Tarreau92fb9832007-10-16 17:34:28 +0200350 if (fe) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200351 pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau46023632010-01-07 22:51:47 +0100352 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
353 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200354 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100355
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200356 if (s->tracked_counters)
357 session_store_counters(s);
358
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100359 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100360 /* we have to unlink all watchers. We must not relink them if
361 * this session was the last one in the list.
362 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100363 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100364 LIST_INIT(&bref->users);
365 if (s->list.n != &sessions)
366 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100367 bref->ref = s->list.n;
368 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100369 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200370 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200371
372 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200373 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200374 pool_flush2(pool2_buffer);
375 pool_flush2(fe->hdr_idx_pool);
376 pool_flush2(pool2_requri);
377 pool_flush2(pool2_capture);
378 pool_flush2(pool2_session);
379 pool_flush2(fe->req_cap_pool);
380 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200381 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200382}
383
384
385/* perform minimal intializations, report 0 in case of error, 1 if OK. */
386int init_session()
387{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100388 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200389 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
390 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200391}
392
Willy Tarreau30e71012007-11-26 20:15:35 +0100393void session_process_counters(struct session *s)
394{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100395 unsigned long long bytes;
396
Willy Tarreau30e71012007-11-26 20:15:35 +0100397 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100398 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100399 s->logs.bytes_in = s->req->total;
400 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200401 s->fe->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100402
Willy Tarreau30e71012007-11-26 20:15:35 +0100403 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200404 s->be->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100405
Willy Tarreau30e71012007-11-26 20:15:35 +0100406 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200407 s->srv->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200408
409 if (s->listener->counters)
410 s->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200411
412 if (s->tracked_counters) {
413 void *ptr = stktable_data_ptr(s->tracked_table,
414 s->tracked_counters,
415 STKTABLE_DT_BYTES_IN_CNT);
416 if (ptr)
417 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
418 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100419 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100420 }
421
Willy Tarreau30e71012007-11-26 20:15:35 +0100422 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100423 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100424 s->logs.bytes_out = s->rep->total;
425 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200426 s->fe->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100427
Willy Tarreau30e71012007-11-26 20:15:35 +0100428 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200429 s->be->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100430
Willy Tarreau30e71012007-11-26 20:15:35 +0100431 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200432 s->srv->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200433
434 if (s->listener->counters)
435 s->listener->counters->bytes_out += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200436
437 if (s->tracked_counters) {
438 void *ptr = stktable_data_ptr(s->tracked_table,
439 s->tracked_counters,
440 STKTABLE_DT_BYTES_OUT_CNT);
441 if (ptr)
442 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
443 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100444 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100445 }
446}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200447
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100448/* This function is called with (si->state == SI_ST_CON) meaning that a
449 * connection was attempted and that the file descriptor is already allocated.
450 * We must check for establishment, error and abort. Possible output states
451 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
452 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
453 * otherwise 1.
454 */
455int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
456{
457 struct buffer *req = si->ob;
458 struct buffer *rep = si->ib;
459
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100460 /* If we got an error, or if nothing happened and the connection timed
461 * out, we must give up. The CER state handler will take care of retry
462 * attempts and error reports.
463 */
464 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau127334e2009-03-28 10:47:26 +0100465 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100466 si->state = SI_ST_CER;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200467 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100468 fd_delete(si->fd);
469
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200470 if (si->release)
471 si->release(si);
472
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100473 if (si->err_type)
474 return 0;
475
476 si->err_loc = s->srv;
477 if (si->flags & SI_FL_ERR)
478 si->err_type = SI_ET_CONN_ERR;
479 else
480 si->err_type = SI_ET_CONN_TO;
481 return 0;
482 }
483
484 /* OK, maybe we want to abort */
Willy Tarreau418fd472009-09-06 21:37:23 +0200485 if (unlikely((rep->flags & BF_SHUTW) ||
486 ((req->flags & BF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200487 (((req->flags & (BF_OUT_EMPTY|BF_WRITE_ACTIVITY)) == BF_OUT_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100488 s->be->options & PR_O_ABRT_CLOSE)))) {
489 /* give up */
490 si->shutw(si);
491 si->err_type |= SI_ET_CONN_ABRT;
492 si->err_loc = s->srv;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200493 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau84455332009-03-15 22:34:05 +0100494 if (s->srv_error)
495 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100496 return 1;
497 }
498
499 /* we need to wait a bit more if there was no activity either */
500 if (!(req->flags & BF_WRITE_ACTIVITY))
501 return 1;
502
503 /* OK, this means that a connection succeeded. The caller will be
504 * responsible for handling the transition from CON to EST.
505 */
506 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100507 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100508 si->state = SI_ST_EST;
509 si->err_type = SI_ET_NONE;
510 si->err_loc = NULL;
511 return 1;
512}
513
514/* This function is called with (si->state == SI_ST_CER) meaning that a
515 * previous connection attempt has failed and that the file descriptor
516 * has already been released. Possible causes include asynchronous error
517 * notification and time out. Possible output states are SI_ST_CLO when
518 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
519 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
520 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
521 * marked as in error state. It returns 0.
522 */
523int sess_update_st_cer(struct session *s, struct stream_interface *si)
524{
525 /* we probably have to release last session from the server */
526 if (s->srv) {
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100527 health_adjust(s->srv, HANA_STATUS_L4_ERR);
528
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100529 if (s->flags & SN_CURR_SESS) {
530 s->flags &= ~SN_CURR_SESS;
531 s->srv->cur_sess--;
532 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100533 }
534
535 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200536 si->conn_retries--;
537 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100538 if (!si->err_type) {
539 si->err_type = SI_ET_CONN_ERR;
540 si->err_loc = s->srv;
541 }
542
543 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200544 s->srv->counters.failed_conns++;
545 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100546 if (may_dequeue_tasks(s->srv, s->be))
547 process_srv_queue(s->srv);
548
549 /* shutw is enough so stop a connecting socket */
550 si->shutw(si);
551 si->ob->flags |= BF_WRITE_ERROR;
552 si->ib->flags |= BF_READ_ERROR;
553
554 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100555 if (s->srv_error)
556 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100557 return 0;
558 }
559
560 /* If the "redispatch" option is set on the backend, we are allowed to
561 * retry on another server for the last retry. In order to achieve this,
562 * we must mark the session unassigned, and eventually clear the DIRECT
563 * bit to ignore any persistence cookie. We won't count a retry nor a
564 * redispatch yet, because this will depend on what server is selected.
565 */
Willy Tarreauee28de02010-06-01 09:51:00 +0200566 if (s->srv && si->conn_retries == 0 &&
Willy Tarreau4de91492010-01-22 19:10:05 +0100567 s->be->options & PR_O_REDISP && !(s->flags & SN_FORCE_PRST)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100568 if (may_dequeue_tasks(s->srv, s->be))
569 process_srv_queue(s->srv);
570
571 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
572 s->prev_srv = s->srv;
573 si->state = SI_ST_REQ;
574 } else {
575 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200576 s->srv->counters.retries++;
577 s->be->counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100578 si->state = SI_ST_ASS;
579 }
580
581 if (si->flags & SI_FL_ERR) {
582 /* The error was an asynchronous connection error, and we will
583 * likely have to retry connecting to the same server, most
584 * likely leading to the same result. To avoid this, we wait
585 * one second before retrying.
586 */
587
588 if (!si->err_type)
589 si->err_type = SI_ET_CONN_ERR;
590
591 si->state = SI_ST_TAR;
592 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
593 return 0;
594 }
595 return 0;
596}
597
598/*
599 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200600 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
601 * SI_ST_INI) to SI_ST_EST, but only when a ->connect function is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100602 */
603void sess_establish(struct session *s, struct stream_interface *si)
604{
605 struct buffer *req = si->ob;
606 struct buffer *rep = si->ib;
607
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100608 if (s->srv)
609 health_adjust(s->srv, HANA_STATUS_L4_OK);
610
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100611 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100612 /* if the user wants to log as soon as possible, without counting
613 * bytes from the server, then this is the right moment. */
614 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
615 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100616 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100617 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100618 }
619 else {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100620 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
621 /* reset hdr_idx which was already initialized by the request.
622 * right now, the http parser does it.
623 * hdr_idx_init(&s->txn.hdr_idx);
624 */
625 }
626
Willy Tarreau4e5b8282009-08-16 22:57:50 +0200627 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100628 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
Willy Tarreaud04e8582010-05-31 12:31:35 +0200629 if (si->connect) {
630 /* real connections have timeouts */
631 req->wto = s->be->timeout.server;
632 rep->rto = s->be->timeout.server;
633 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100634 req->wex = TICK_ETERNITY;
635}
636
637/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
638 * Other input states are simply ignored.
639 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
640 * Flags must have previously been updated for timeouts and other conditions.
641 */
642void sess_update_stream_int(struct session *s, struct stream_interface *si)
643{
644 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",
645 now_ms, __FUNCTION__,
646 s,
647 s->req, s->rep,
648 s->req->rex, s->rep->wex,
649 s->req->flags, s->rep->flags,
650 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
651
652 if (si->state == SI_ST_ASS) {
653 /* Server assigned to connection request, we have to try to connect now */
654 int conn_err;
655
656 conn_err = connect_server(s);
657 if (conn_err == SN_ERR_NONE) {
658 /* state = SI_ST_CON now */
Willy Tarreau8f6457c2008-12-01 00:08:28 +0100659 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100660 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100661 return;
662 }
663
664 /* We have received a synchronous error. We might have to
665 * abort, retry immediately or redispatch.
666 */
667 if (conn_err == SN_ERR_INTERNAL) {
668 if (!si->err_type) {
669 si->err_type = SI_ET_CONN_OTHER;
670 si->err_loc = s->srv;
671 }
672
673 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100674 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100675 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200676 s->srv->counters.failed_conns++;
677 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100678
679 /* release other sessions waiting for this server */
680 if (may_dequeue_tasks(s->srv, s->be))
681 process_srv_queue(s->srv);
682
683 /* Failed and not retryable. */
684 si->shutr(si);
685 si->shutw(si);
686 si->ob->flags |= BF_WRITE_ERROR;
687
688 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
689
690 /* no session was ever accounted for this server */
691 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100692 if (s->srv_error)
693 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100694 return;
695 }
696
697 /* We are facing a retryable error, but we don't want to run a
698 * turn-around now, as the problem is likely a source port
699 * allocation problem, so we want to retry now.
700 */
701 si->state = SI_ST_CER;
702 si->flags &= ~SI_FL_ERR;
703 sess_update_st_cer(s, si);
704 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
705 return;
706 }
707 else if (si->state == SI_ST_QUE) {
708 /* connection request was queued, check for any update */
709 if (!s->pend_pos) {
710 /* The connection is not in the queue anymore. Either
711 * we have a server connection slot available and we
712 * go directly to the assigned state, or we need to
713 * load-balance first and go to the INI state.
714 */
715 si->exp = TICK_ETERNITY;
716 if (unlikely(!(s->flags & SN_ASSIGNED)))
717 si->state = SI_ST_REQ;
718 else {
719 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
720 si->state = SI_ST_ASS;
721 }
722 return;
723 }
724
725 /* Connection request still in queue... */
726 if (si->flags & SI_FL_EXP) {
727 /* ... and timeout expired */
728 si->exp = TICK_ETERNITY;
729 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
730 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200731 s->srv->counters.failed_conns++;
732 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100733 si->shutr(si);
734 si->shutw(si);
735 si->ob->flags |= BF_WRITE_TIMEOUT;
736 if (!si->err_type)
737 si->err_type = SI_ET_QUEUE_TO;
738 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100739 if (s->srv_error)
740 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100741 return;
742 }
743
744 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau418fd472009-09-06 21:37:23 +0200745 if ((si->ob->flags & (BF_READ_ERROR)) ||
746 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200747 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100748 /* give up */
749 si->exp = TICK_ETERNITY;
750 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
751 si->shutr(si);
752 si->shutw(si);
753 si->err_type |= SI_ET_QUEUE_ABRT;
754 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100755 if (s->srv_error)
756 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100757 return;
758 }
759
760 /* Nothing changed */
761 return;
762 }
763 else if (si->state == SI_ST_TAR) {
764 /* Connection request might be aborted */
Willy Tarreau418fd472009-09-06 21:37:23 +0200765 if ((si->ob->flags & (BF_READ_ERROR)) ||
766 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200767 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100768 /* give up */
769 si->exp = TICK_ETERNITY;
770 si->shutr(si);
771 si->shutw(si);
772 si->err_type |= SI_ET_CONN_ABRT;
773 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100774 if (s->srv_error)
775 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100776 return;
777 }
778
779 if (!(si->flags & SI_FL_EXP))
780 return; /* still in turn-around */
781
782 si->exp = TICK_ETERNITY;
783
784 /* we keep trying on the same server as long as the session is
785 * marked "assigned".
786 * FIXME: Should we force a redispatch attempt when the server is down ?
787 */
788 if (s->flags & SN_ASSIGNED)
789 si->state = SI_ST_ASS;
790 else
791 si->state = SI_ST_REQ;
792 return;
793 }
794}
795
796/* This function initiates a server connection request on a stream interface
797 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
798 * indicating that a server has been assigned. It may also return SI_ST_QUE,
799 * or SI_ST_CLO upon error.
800 */
801static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
802 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",
803 now_ms, __FUNCTION__,
804 s,
805 s->req, s->rep,
806 s->req->rex, s->rep->wex,
807 s->req->flags, s->rep->flags,
808 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
809
810 if (si->state != SI_ST_REQ)
811 return;
812
813 /* Try to assign a server */
814 if (srv_redispatch_connect(s) != 0) {
815 /* We did not get a server. Either we queued the
816 * connection request, or we encountered an error.
817 */
818 if (si->state == SI_ST_QUE)
819 return;
820
821 /* we did not get any server, let's check the cause */
822 si->shutr(si);
823 si->shutw(si);
824 si->ob->flags |= BF_WRITE_ERROR;
825 if (!si->err_type)
826 si->err_type = SI_ET_CONN_OTHER;
827 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100828 if (s->srv_error)
829 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100830 return;
831 }
832
833 /* The server is assigned */
834 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
835 si->state = SI_ST_ASS;
836}
837
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200838/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +0100839 * if appropriate. The default_backend rule is also considered, then the
840 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200841 * It returns 1 if the processing can continue on next analysers, or zero if it
842 * either needs more data or wants to immediately abort the request.
843 */
844int process_switching_rules(struct session *s, struct buffer *req, int an_bit)
845{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200846 struct persist_rule *prst_rule;
Willy Tarreau4de91492010-01-22 19:10:05 +0100847
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200848 req->analysers &= ~an_bit;
849 req->analyse_exp = TICK_ETERNITY;
850
851 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
852 now_ms, __FUNCTION__,
853 s,
854 req,
855 req->rex, req->wex,
856 req->flags,
857 req->l,
858 req->analysers);
859
860 /* now check whether we have some switching rules for this request */
861 if (!(s->flags & SN_BE_ASSIGNED)) {
862 struct switching_rule *rule;
863
864 list_for_each_entry(rule, &s->fe->switching_rules, list) {
865 int ret;
866
867 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ);
868 ret = acl_pass(ret);
869 if (rule->cond->pol == ACL_COND_UNLESS)
870 ret = !ret;
871
872 if (ret) {
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200873 if (!session_set_backend(s, rule->be.backend))
874 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200875 break;
876 }
877 }
878
879 /* To ensure correct connection accounting on the backend, we
880 * have to assign one if it was not set (eg: a listen). This
881 * measure also takes care of correctly setting the default
882 * backend if any.
883 */
884 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200885 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
886 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200887 }
888
Willy Tarreaufb356202010-08-03 14:02:05 +0200889 /* we don't want to run the TCP or HTTP filters again if the backend has not changed */
890 if (s->fe == s->be) {
891 s->req->analysers &= ~AN_REQ_INSPECT_BE;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200892 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +0200893 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200894
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200895 /* 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 +0100896 * persistence rule, and report that in the session.
897 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200898 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +0100899 int ret = 1;
900
901 if (prst_rule->cond) {
902 ret = acl_exec_cond(prst_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
903 ret = acl_pass(ret);
904 if (prst_rule->cond->pol == ACL_COND_UNLESS)
905 ret = !ret;
906 }
907
908 if (ret) {
909 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200910 if (prst_rule->type == PERSIST_TYPE_FORCE) {
911 s->flags |= SN_FORCE_PRST;
912 } else {
913 s->flags |= SN_IGNORE_PRST;
914 }
Willy Tarreau4de91492010-01-22 19:10:05 +0100915 break;
916 }
917 }
918
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200919 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200920
921 sw_failed:
922 /* immediately abort this request in case of allocation failure */
923 buffer_abort(s->req);
924 buffer_abort(s->rep);
925
926 if (!(s->flags & SN_ERR_MASK))
927 s->flags |= SN_ERR_RESOURCE;
928 if (!(s->flags & SN_FINST_MASK))
929 s->flags |= SN_FINST_R;
930
931 s->txn.status = 500;
932 s->req->analysers = 0;
933 s->req->analyse_exp = TICK_ETERNITY;
934 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200935}
936
Emeric Brun1d33b292010-01-04 15:47:17 +0100937/* This stream analyser works on a request. It applies all sticking rules on
938 * it then returns 1. The data must already be present in the buffer otherwise
939 * they won't match. It always returns 1.
940 */
941int process_sticking_rules(struct session *s, struct buffer *req, int an_bit)
942{
943 struct proxy *px = s->be;
944 struct sticking_rule *rule;
945
946 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
947 now_ms, __FUNCTION__,
948 s,
949 req,
950 req->rex, req->wex,
951 req->flags,
952 req->l,
953 req->analysers);
954
955 list_for_each_entry(rule, &px->sticking_rules, list) {
956 int ret = 1 ;
957 int i;
958
959 for (i = 0; i < s->store_count; i++) {
960 if (rule->table.t == s->store[i].table)
961 break;
962 }
963
964 if (i != s->store_count)
965 continue;
966
967 if (rule->cond) {
968 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_REQ);
969 ret = acl_pass(ret);
970 if (rule->cond->pol == ACL_COND_UNLESS)
971 ret = !ret;
972 }
973
974 if (ret) {
975 struct stktable_key *key;
976
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200977 key = stktable_fetch_key(px, s, &s->txn, PATTERN_FETCH_REQ, rule->expr, rule->table.t->type);
Emeric Brun1d33b292010-01-04 15:47:17 +0100978 if (!key)
979 continue;
980
981 if (rule->flags & STK_IS_MATCH) {
982 struct stksess *ts;
983
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200984 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Emeric Brun1d33b292010-01-04 15:47:17 +0100985 if (!(s->flags & SN_ASSIGNED)) {
986 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +0200987 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +0100988
989 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +0200990 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
991 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +0100992 if (node) {
993 struct server *srv;
994
995 srv = container_of(node, struct server, conf.id);
Willy Tarreau4de91492010-01-22 19:10:05 +0100996 if ((srv->state & SRV_RUNNING) ||
997 (px->options & PR_O_PERSIST) ||
998 (s->flags & SN_FORCE_PRST)) {
Emeric Brun1d33b292010-01-04 15:47:17 +0100999 s->flags |= SN_DIRECT | SN_ASSIGNED;
1000 s->srv = srv;
1001 }
1002 }
1003 }
Willy Tarreaue8f63382010-07-13 15:20:24 +02001004 stktable_touch(rule->table.t, ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001005 }
1006 }
1007 if (rule->flags & STK_IS_STORE) {
1008 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1009 struct stksess *ts;
1010
1011 ts = stksess_new(rule->table.t, key);
1012 if (ts) {
1013 s->store[s->store_count].table = rule->table.t;
1014 s->store[s->store_count++].ts = ts;
1015 }
1016 }
1017 }
1018 }
1019 }
1020
1021 req->analysers &= ~an_bit;
1022 req->analyse_exp = TICK_ETERNITY;
1023 return 1;
1024}
1025
1026/* This stream analyser works on a response. It applies all store rules on it
1027 * then returns 1. The data must already be present in the buffer otherwise
1028 * they won't match. It always returns 1.
1029 */
1030int process_store_rules(struct session *s, struct buffer *rep, int an_bit)
1031{
1032 struct proxy *px = s->be;
1033 struct sticking_rule *rule;
1034 int i;
1035
1036 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1037 now_ms, __FUNCTION__,
1038 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001039 rep,
1040 rep->rex, rep->wex,
1041 rep->flags,
1042 rep->l,
1043 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001044
1045 list_for_each_entry(rule, &px->storersp_rules, list) {
1046 int ret = 1 ;
1047 int storereqidx = -1;
1048
1049 for (i = 0; i < s->store_count; i++) {
1050 if (rule->table.t == s->store[i].table) {
1051 if (!(s->store[i].flags))
1052 storereqidx = i;
1053 break;
1054 }
1055 }
1056
1057 if ((i != s->store_count) && (storereqidx == -1))
1058 continue;
1059
1060 if (rule->cond) {
1061 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_RTR);
1062 ret = acl_pass(ret);
1063 if (rule->cond->pol == ACL_COND_UNLESS)
1064 ret = !ret;
1065 }
1066
1067 if (ret) {
1068 struct stktable_key *key;
1069
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001070 key = stktable_fetch_key(px, s, &s->txn, PATTERN_FETCH_RTR, rule->expr, rule->table.t->type);
Emeric Brun1d33b292010-01-04 15:47:17 +01001071 if (!key)
1072 continue;
1073
1074 if (storereqidx != -1) {
Willy Tarreau393379c2010-06-06 12:11:37 +02001075 stksess_setkey(s->store[storereqidx].table, s->store[storereqidx].ts, key);
Emeric Brun1d33b292010-01-04 15:47:17 +01001076 s->store[storereqidx].flags = 1;
1077 }
1078 else if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1079 struct stksess *ts;
1080
1081 ts = stksess_new(rule->table.t, key);
1082 if (ts) {
1083 s->store[s->store_count].table = rule->table.t;
1084 s->store[s->store_count].flags = 1;
1085 s->store[s->store_count++].ts = ts;
1086 }
1087 }
1088 }
1089 }
1090
1091 /* process store request and store response */
1092 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001093 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001094 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001095
1096 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1097 if (ts) {
1098 /* the entry already existed, we can free ours */
Willy Tarreaue8f63382010-07-13 15:20:24 +02001099 stktable_touch(s->store[i].table, ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001100 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001101 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001102 else
1103 ts = stktable_store(s->store[i].table, s->store[i].ts);
1104
1105 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001106 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
1107 stktable_data_cast(ptr, server_id) = s->srv->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001108 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001109 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001110
1111 rep->analysers &= ~an_bit;
1112 rep->analyse_exp = TICK_ETERNITY;
1113 return 1;
1114}
1115
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001116/* This macro is very specific to the function below. See the comments in
1117 * process_session() below to understand the logic and the tests.
1118 */
1119#define UPDATE_ANALYSERS(real, list, back, flag) { \
1120 list = (((list) & ~(flag)) | ~(back)) & (real); \
1121 back = real; \
1122 if (!(list)) \
1123 break; \
1124 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1125 continue; \
1126}
1127
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001128/* Processes the client, server, request and response jobs of a session task,
1129 * then puts it back to the wait queue in a clean state, or cleans up its
1130 * resources if it must be deleted. Returns in <next> the date the task wants
1131 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1132 * nothing too many times, the request and response buffers flags are monitored
1133 * and each function is called only if at least another function has changed at
1134 * least one flag it is interested in.
1135 */
Willy Tarreau26c25062009-03-08 09:38:41 +01001136struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001137{
1138 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001139 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001140 unsigned int rq_prod_last, rq_cons_last;
1141 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001142 unsigned int req_ana_back;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001143
1144 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
1145 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
1146
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001147 /* this data may be no longer valid, clear it */
1148 memset(&s->txn.auth, 0, sizeof(s->txn.auth));
1149
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001150 /* This flag must explicitly be set every time */
1151 s->req->flags &= ~BF_READ_NOEXP;
1152
1153 /* Keep a copy of req/rep flags so that we can detect shutdowns */
1154 rqf_last = s->req->flags;
1155 rpf_last = s->rep->flags;
1156
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001157 /* we don't want the stream interface functions to recursively wake us up */
1158 if (s->req->prod->owner == t)
1159 s->req->prod->flags |= SI_FL_DONT_WAKE;
1160 if (s->req->cons->owner == t)
1161 s->req->cons->flags |= SI_FL_DONT_WAKE;
1162
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001163 /* 1a: Check for low level timeouts if needed. We just set a flag on
1164 * stream interfaces when their timeouts have expired.
1165 */
1166 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
1167 stream_int_check_timeouts(&s->si[0]);
1168 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001169
1170 /* check buffer timeouts, and close the corresponding stream interfaces
1171 * for future reads or writes. Note: this will also concern upper layers
1172 * but we do not touch any other flag. We must be careful and correctly
1173 * detect state changes when calling them.
1174 */
1175
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001176 buffer_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001177
Willy Tarreau14641402009-12-29 14:49:56 +01001178 if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1179 s->req->cons->flags |= SI_FL_NOLINGER;
1180 s->req->cons->shutw(s->req->cons);
1181 }
1182
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001183 if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1184 s->req->prod->shutr(s->req->prod);
1185
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001186 buffer_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001187
Willy Tarreau14641402009-12-29 14:49:56 +01001188 if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1189 s->rep->cons->flags |= SI_FL_NOLINGER;
1190 s->rep->cons->shutw(s->rep->cons);
1191 }
1192
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001193 if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1194 s->rep->prod->shutr(s->rep->prod);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001195 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001196
1197 /* 1b: check for low-level errors reported at the stream interface.
1198 * First we check if it's a retryable error (in which case we don't
1199 * want to tell the buffer). Otherwise we report the error one level
1200 * upper by setting flags into the buffers. Note that the side towards
1201 * the client cannot have connect (hence retryable) errors. Also, the
1202 * connection setup code must be able to deal with any type of abort.
1203 */
1204 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
1205 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
1206 s->si[0].shutr(&s->si[0]);
1207 s->si[0].shutw(&s->si[0]);
1208 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001209 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreauae526782010-03-04 20:34:23 +01001210 s->be->counters.cli_aborts++;
1211 if (s->srv)
1212 s->srv->counters.cli_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001213 if (!(s->flags & SN_ERR_MASK))
1214 s->flags |= SN_ERR_CLICL;
1215 if (!(s->flags & SN_FINST_MASK))
1216 s->flags |= SN_FINST_D;
1217 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001218 }
1219 }
1220
1221 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
1222 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
1223 s->si[1].shutr(&s->si[1]);
1224 s->si[1].shutw(&s->si[1]);
1225 stream_int_report_error(&s->si[1]);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001226 s->be->counters.failed_resp++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001227 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001228 s->srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001229 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreauae526782010-03-04 20:34:23 +01001230 s->be->counters.srv_aborts++;
1231 if (s->srv)
1232 s->srv->counters.srv_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001233 if (!(s->flags & SN_ERR_MASK))
1234 s->flags |= SN_ERR_SRVCL;
1235 if (!(s->flags & SN_FINST_MASK))
1236 s->flags |= SN_FINST_D;
1237 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001238 }
1239 /* note: maybe we should process connection errors here ? */
1240 }
1241
1242 if (s->si[1].state == SI_ST_CON) {
1243 /* we were trying to establish a connection on the server side,
1244 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1245 */
1246 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
1247 sess_update_st_cer(s, &s->si[1]);
1248 else if (s->si[1].state == SI_ST_EST)
1249 sess_establish(s, &s->si[1]);
1250
1251 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1252 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1253 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1254 */
1255 }
1256
Willy Tarreau815a9b22010-07-27 17:15:12 +02001257 rq_prod_last = s->si[0].state;
1258 rq_cons_last = s->si[1].state;
1259 rp_cons_last = s->si[0].state;
1260 rp_prod_last = s->si[1].state;
1261
1262 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001263 /* Check for connection closure */
1264
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001265 DPRINTF(stderr,
1266 "[%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",
1267 now_ms, __FUNCTION__, __LINE__,
1268 t,
1269 s, s->flags,
1270 s->req, s->rep,
1271 s->req->rex, s->rep->wex,
1272 s->req->flags, s->rep->flags,
1273 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
1274 s->rep->cons->err_type, s->req->cons->err_type,
Willy Tarreauee28de02010-06-01 09:51:00 +02001275 s->req->cons->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001276
1277 /* nothing special to be done on client side */
1278 if (unlikely(s->req->prod->state == SI_ST_DIS))
1279 s->req->prod->state = SI_ST_CLO;
1280
1281 /* When a server-side connection is released, we have to count it and
1282 * check for pending connections on this server.
1283 */
1284 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
1285 s->req->cons->state = SI_ST_CLO;
1286 if (s->srv) {
1287 if (s->flags & SN_CURR_SESS) {
1288 s->flags &= ~SN_CURR_SESS;
1289 s->srv->cur_sess--;
1290 }
1291 sess_change_server(s, NULL);
1292 if (may_dequeue_tasks(s->srv, s->be))
1293 process_srv_queue(s->srv);
1294 }
1295 }
1296
1297 /*
1298 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1299 * at this point.
1300 */
1301
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001302 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001303 /* Analyse request */
1304 if ((s->req->flags & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001305 ((s->req->flags ^ rqf_last) & BF_MASK_STATIC) ||
1306 s->si[0].state != rq_prod_last ||
1307 s->si[1].state != rq_cons_last) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001308 unsigned int flags = s->req->flags;
1309
1310 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001311 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001312 unsigned int ana_list;
1313 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001314
Willy Tarreau90deb182010-01-07 00:20:41 +01001315 /* it's up to the analysers to stop new connections,
1316 * disable reading or closing. Note: if an analyser
1317 * disables any of these bits, it is responsible for
1318 * enabling them again when it disables itself, so
1319 * that other analysers are called in similar conditions.
1320 */
1321 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001322 buffer_auto_connect(s->req);
1323 buffer_auto_close(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001324
1325 /* We will call all analysers for which a bit is set in
1326 * s->req->analysers, following the bit order from LSB
1327 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001328 * the list when not needed. Any analyser may return 0
1329 * to break out of the loop, either because of missing
1330 * data to take a decision, or because it decides to
1331 * kill the session. We loop at least once through each
1332 * analyser, and we may loop again if other analysers
1333 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001334 *
1335 * We build a list of analysers to run. We evaluate all
1336 * of these analysers in the order of the lower bit to
1337 * the higher bit. This ordering is very important.
1338 * An analyser will often add/remove other analysers,
1339 * including itself. Any changes to itself have no effect
1340 * on the loop. If it removes any other analysers, we
1341 * want those analysers not to be called anymore during
1342 * this loop. If it adds an analyser that is located
1343 * after itself, we want it to be scheduled for being
1344 * processed during the loop. If it adds an analyser
1345 * which is located before it, we want it to switch to
1346 * it immediately, even if it has already been called
1347 * once but removed since.
1348 *
1349 * In order to achieve this, we compare the analyser
1350 * list after the call with a copy of it before the
1351 * call. The work list is fed with analyser bits that
1352 * appeared during the call. Then we compare previous
1353 * work list with the new one, and check the bits that
1354 * appeared. If the lowest of these bits is lower than
1355 * the current bit, it means we have enabled a previous
1356 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001357 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001358
1359 ana_list = ana_back = s->req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001360 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001361 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001362
Willy Tarreaufb356202010-08-03 14:02:05 +02001363 if (ana_list & AN_REQ_INSPECT_FE) {
1364 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001365 break;
Willy Tarreaufb356202010-08-03 14:02:05 +02001366 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001367 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001368
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001369 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001370 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001371 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001372 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001373 }
1374
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001375 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001376 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
1377 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001378 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001379 }
1380
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001381 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001382 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
1383 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001384 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001385 }
1386
Willy Tarreaufb356202010-08-03 14:02:05 +02001387 if (ana_list & AN_REQ_INSPECT_BE) {
1388 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_BE))
1389 break;
1390 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
1391 }
1392
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001393 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001394 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
1395 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001396 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001397 }
1398
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001399 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001400 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001401 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001402 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001403 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001404
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001405 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreauc465fd72009-08-31 00:17:18 +02001406 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
1407 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001408 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001409 }
1410
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001411 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001412 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001413 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001414 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001415 }
Emeric Brun647caf12009-06-30 17:57:00 +02001416
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001417 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Emeric Brun647caf12009-06-30 17:57:00 +02001418 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
1419 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001420 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001421 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001422
Emeric Brun1d33b292010-01-04 15:47:17 +01001423 if (ana_list & AN_REQ_STICKING_RULES) {
1424 if (!process_sticking_rules(s, s->req, AN_REQ_STICKING_RULES))
1425 break;
1426 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
1427 }
1428
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001429 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001430 if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY))
1431 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001432 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001433 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001434 break;
1435 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001436 }
Willy Tarreau84455332009-03-15 22:34:05 +01001437
Willy Tarreau815a9b22010-07-27 17:15:12 +02001438 rq_prod_last = s->si[0].state;
1439 rq_cons_last = s->si[1].state;
1440 rqf_last = s->req->flags;
1441
1442 if ((s->req->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001443 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001444 }
1445
Willy Tarreau576507f2010-01-07 00:09:04 +01001446 /* we'll monitor the request analysers while parsing the response,
1447 * because some response analysers may indirectly enable new request
1448 * analysers (eg: HTTP keep-alive).
1449 */
1450 req_ana_back = s->req->analysers;
1451
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001452 resync_response:
1453 /* Analyse response */
1454
1455 if (unlikely(s->rep->flags & BF_HIJACK)) {
1456 /* In inject mode, we wake up everytime something has
1457 * happened on the write side of the buffer.
1458 */
1459 unsigned int flags = s->rep->flags;
1460
1461 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
1462 !(s->rep->flags & BF_FULL)) {
1463 s->rep->hijacker(s, s->rep);
1464 }
1465
1466 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
1467 rpf_last = s->rep->flags;
1468 goto resync_response;
1469 }
1470 }
1471 else if ((s->rep->flags & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001472 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC ||
1473 s->si[0].state != rp_cons_last ||
1474 s->si[1].state != rp_prod_last) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001475 unsigned int flags = s->rep->flags;
1476
1477 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001478 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001479 unsigned int ana_list;
1480 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001481
Willy Tarreau90deb182010-01-07 00:20:41 +01001482 /* it's up to the analysers to stop disable reading or
1483 * closing. Note: if an analyser disables any of these
1484 * bits, it is responsible for enabling them again when
1485 * it disables itself, so that other analysers are called
1486 * in similar conditions.
1487 */
1488 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001489 buffer_auto_close(s->rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001490
1491 /* We will call all analysers for which a bit is set in
1492 * s->rep->analysers, following the bit order from LSB
1493 * to MSB. The analysers must remove themselves from
1494 * the list when not needed. Any analyser may return 0
1495 * to break out of the loop, either because of missing
1496 * data to take a decision, or because it decides to
1497 * kill the session. We loop at least once through each
1498 * analyser, and we may loop again if other analysers
1499 * are added in the middle.
1500 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001501
1502 ana_list = ana_back = s->rep->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001503 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001504 /* Warning! ensure that analysers are always placed in ascending order! */
1505
1506 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001507 if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP))
1508 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001509 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001510 }
1511
Emeric Brun1d33b292010-01-04 15:47:17 +01001512 if (ana_list & AN_RES_STORE_RULES) {
1513 if (!process_store_rules(s, s->rep, AN_RES_STORE_RULES))
1514 break;
1515 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
1516 }
1517
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001518 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001519 if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be))
1520 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001521 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001522 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001523
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001524 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001525 if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY))
1526 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001527 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001528 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001529 break;
1530 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001531 }
1532
Willy Tarreau815a9b22010-07-27 17:15:12 +02001533 rp_cons_last = s->si[0].state;
1534 rp_prod_last = s->si[1].state;
1535 rpf_last = s->rep->flags;
1536
1537 if ((s->rep->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001538 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001539 }
1540
Willy Tarreau576507f2010-01-07 00:09:04 +01001541 /* maybe someone has added some request analysers, so we must check and loop */
1542 if (s->req->analysers & ~req_ana_back)
1543 goto resync_request;
1544
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001545 /* FIXME: here we should call protocol handlers which rely on
1546 * both buffers.
1547 */
1548
1549
1550 /*
Willy Tarreauae526782010-03-04 20:34:23 +01001551 * Now we propagate unhandled errors to the session. Normally
1552 * we're just in a data phase here since it means we have not
1553 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001554 */
1555 if (!(s->flags & SN_ERR_MASK)) {
1556 if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1557 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +01001558 s->req->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001559 if (s->req->flags & BF_READ_ERROR) {
1560 s->be->counters.cli_aborts++;
1561 if (s->srv)
1562 s->srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001563 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001564 }
1565 else if (s->req->flags & BF_READ_TIMEOUT) {
1566 s->be->counters.cli_aborts++;
1567 if (s->srv)
1568 s->srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001569 s->flags |= SN_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001570 }
1571 else if (s->req->flags & BF_WRITE_ERROR) {
1572 s->be->counters.srv_aborts++;
1573 if (s->srv)
1574 s->srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001575 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001576 }
1577 else {
1578 s->be->counters.srv_aborts++;
1579 if (s->srv)
1580 s->srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001581 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001582 }
Willy Tarreau84455332009-03-15 22:34:05 +01001583 sess_set_term_flags(s);
1584 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001585 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1586 /* Report it if the server got an error or a read timeout expired */
1587 s->rep->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001588 if (s->rep->flags & BF_READ_ERROR) {
1589 s->be->counters.srv_aborts++;
1590 if (s->srv)
1591 s->srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001592 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001593 }
1594 else if (s->rep->flags & BF_READ_TIMEOUT) {
1595 s->be->counters.srv_aborts++;
1596 if (s->srv)
1597 s->srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001598 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001599 }
1600 else if (s->rep->flags & BF_WRITE_ERROR) {
1601 s->be->counters.cli_aborts++;
1602 if (s->srv)
1603 s->srv->counters.cli_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001604 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001605 }
1606 else {
1607 s->be->counters.cli_aborts++;
1608 if (s->srv)
1609 s->srv->counters.cli_aborts++;
1610 s->flags |= SN_ERR_CLITO;
1611 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001612 sess_set_term_flags(s);
1613 }
Willy Tarreau84455332009-03-15 22:34:05 +01001614 }
1615
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001616 /*
1617 * Here we take care of forwarding unhandled data. This also includes
1618 * connection establishments and shutdown requests.
1619 */
1620
1621
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001622 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001623 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001624 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001625 if (!s->req->analysers &&
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001626 !(s->req->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTW_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001627 (s->req->prod->state >= SI_ST_EST) &&
1628 (s->req->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001629 /* This buffer is freewheeling, there's no analyser nor hijacker
1630 * attached to it. If any data are left in, we'll permit them to
1631 * move.
1632 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001633 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001634 buffer_auto_connect(s->req);
1635 buffer_auto_close(s->req);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001636 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001637
Willy Tarreau31971e52009-09-20 12:07:52 +02001638 /* If the producer is still connected, we'll enable data to flow
1639 * from the producer to the consumer (which might possibly not be
1640 * connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001641 */
Willy Tarreau31971e52009-09-20 12:07:52 +02001642 if (!(s->req->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW)))
1643 buffer_forward(s->req, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001644 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001645
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001646 /* check if it is wise to enable kernel splicing to forward request data */
1647 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1648 s->req->to_forward &&
1649 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001650 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001651 (pipes_used < global.maxpipes) &&
1652 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
1653 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1654 (s->req->flags & BF_STREAMER_FAST)))) {
1655 s->req->flags |= BF_KERN_SPLICING;
1656 }
1657
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001658 /* reflect what the L7 analysers have seen last */
1659 rqf_last = s->req->flags;
1660
1661 /*
1662 * Now forward all shutdown requests between both sides of the buffer
1663 */
1664
Willy Tarreau520d95e2009-09-19 21:04:57 +02001665 /* first, let's check if the request buffer needs to shutdown(write), which may
1666 * happen either because the input is closed or because we want to force a close
Willy Tarreaue4599762010-03-21 23:25:09 +01001667 * once the server has begun to respond.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001668 */
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001669 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
Willy Tarreaue4599762010-03-21 23:25:09 +01001670 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001671 buffer_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001672
1673 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001674 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 +01001675 s->req->cons->shutw(s->req->cons);
1676
1677 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001678 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
1679 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001680 buffer_shutr_now(s->req);
1681
1682 /* shutdown(read) pending */
1683 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1684 s->req->prod->shutr(s->req->prod);
1685
Willy Tarreau520d95e2009-09-19 21:04:57 +02001686 /* it's possible that an upper layer has requested a connection setup or abort.
1687 * There are 2 situations where we decide to establish a new connection :
1688 * - there are data scheduled for emission in the buffer
1689 * - the BF_AUTO_CONNECT flag is set (active connection)
1690 */
1691 if (s->req->cons->state == SI_ST_INI) {
Willy Tarreaue4599762010-03-21 23:25:09 +01001692 if (!(s->req->flags & BF_SHUTW)) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001693 if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) {
Willy Tarreau85e7d002010-05-31 11:57:51 +02001694 /* If we have an iohandler without a connect method, we immediately
1695 * switch to the connected state, otherwise we perform a connection
1696 * request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001697 */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001698 s->req->cons->state = SI_ST_REQ; /* new connection requested */
Willy Tarreau070ceb62010-06-01 10:36:43 +02001699 s->req->cons->conn_retries = s->be->conn_retries;
Willy Tarreau85e7d002010-05-31 11:57:51 +02001700 if (unlikely(s->req->cons->iohandler && !s->req->cons->connect)) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02001701 s->req->cons->state = SI_ST_EST; /* connection established */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001702 s->rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
1703 s->req->wex = TICK_ETERNITY;
1704 }
Willy Tarreau520d95e2009-09-19 21:04:57 +02001705 }
Willy Tarreau73201222009-08-16 18:27:24 +02001706 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001707 else {
Willy Tarreau92795622009-03-06 12:51:23 +01001708 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001709 buffer_shutw_now(s->req); /* fix buffer flags upon abort */
1710 buffer_shutr_now(s->rep);
1711 }
Willy Tarreau92795622009-03-06 12:51:23 +01001712 }
1713
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001714
1715 /* we may have a pending connection request, or a connection waiting
1716 * for completion.
1717 */
1718 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
1719 do {
1720 /* nb: step 1 might switch from QUE to ASS, but we first want
1721 * to give a chance to step 2 to perform a redirect if needed.
1722 */
1723 if (s->si[1].state != SI_ST_REQ)
1724 sess_update_stream_int(s, &s->si[1]);
1725 if (s->si[1].state == SI_ST_REQ)
1726 sess_prepare_conn_req(s, &s->si[1]);
1727
1728 if (s->si[1].state == SI_ST_ASS && s->srv &&
1729 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
1730 perform_http_redirect(s, &s->si[1]);
1731 } while (s->si[1].state == SI_ST_ASS);
1732 }
1733
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001734 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001735 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001736 goto resync_stream_interface;
1737
Willy Tarreau815a9b22010-07-27 17:15:12 +02001738 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001739 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001740 goto resync_request;
1741
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001742 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01001743
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001744 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001745 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001746 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001747 if (!s->rep->analysers &&
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001748 !(s->rep->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTW_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001749 (s->rep->prod->state >= SI_ST_EST) &&
1750 (s->rep->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001751 /* This buffer is freewheeling, there's no analyser nor hijacker
1752 * attached to it. If any data are left in, we'll permit them to
1753 * move.
1754 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001755 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001756 buffer_auto_close(s->rep);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001757 buffer_flush(s->rep);
Willy Tarreau31971e52009-09-20 12:07:52 +02001758 if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW|BF_SHUTW_NOW)))
1759 buffer_forward(s->rep, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001760 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001761
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001762 /* check if it is wise to enable kernel splicing to forward response data */
1763 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1764 s->rep->to_forward &&
1765 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001766 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001767 (pipes_used < global.maxpipes) &&
1768 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
1769 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1770 (s->rep->flags & BF_STREAMER_FAST)))) {
1771 s->rep->flags |= BF_KERN_SPLICING;
1772 }
1773
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001774 /* reflect what the L7 analysers have seen last */
1775 rpf_last = s->rep->flags;
1776
1777 /*
1778 * Now forward all shutdown requests between both sides of the buffer
1779 */
1780
1781 /*
1782 * FIXME: this is probably where we should produce error responses.
1783 */
1784
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001785 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001786 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
1787 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001788 buffer_shutw_now(s->rep);
1789
1790 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001791 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 +01001792 s->rep->cons->shutw(s->rep->cons);
1793
1794 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001795 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
1796 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001797 buffer_shutr_now(s->rep);
1798
1799 /* shutdown(read) pending */
1800 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1801 s->rep->prod->shutr(s->rep->prod);
1802
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001803 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001804 goto resync_stream_interface;
1805
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001806 if (s->req->flags != rqf_last)
1807 goto resync_request;
1808
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001809 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001810 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001811
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001812 /* we're interested in getting wakeups again */
1813 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
1814 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
1815
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001816 /* This is needed only when debugging is enabled, to indicate
1817 * client-side or server-side close. Please note that in the unlikely
1818 * event where both sides would close at once, the sequence is reported
1819 * on the server side first.
1820 */
1821 if (unlikely((global.mode & MODE_DEBUG) &&
1822 (!(global.mode & MODE_QUIET) ||
1823 (global.mode & MODE_VERBOSE)))) {
1824 int len;
1825
1826 if (s->si[1].state == SI_ST_CLO &&
1827 s->si[1].prev_state == SI_ST_EST) {
1828 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1829 s->uniq_id, s->be->id,
1830 (unsigned short)s->si[0].fd,
1831 (unsigned short)s->si[1].fd);
1832 write(1, trash, len);
1833 }
1834
1835 if (s->si[0].state == SI_ST_CLO &&
1836 s->si[0].prev_state == SI_ST_EST) {
1837 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1838 s->uniq_id, s->be->id,
1839 (unsigned short)s->si[0].fd,
1840 (unsigned short)s->si[1].fd);
1841 write(1, trash, len);
1842 }
1843 }
1844
1845 if (likely((s->rep->cons->state != SI_ST_CLO) ||
1846 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
1847
1848 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1849 session_process_counters(s);
1850
Willy Tarreau1accfc02009-09-05 20:57:35 +02001851 if (s->rep->cons->state == SI_ST_EST && !s->rep->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001852 s->rep->cons->update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001853
Willy Tarreau1accfc02009-09-05 20:57:35 +02001854 if (s->req->cons->state == SI_ST_EST && !s->req->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001855 s->req->cons->update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001856
Willy Tarreaua6eebb32010-06-04 11:40:20 +02001857 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL|BF_READ_ATTACHED);
1858 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 +01001859 s->si[0].prev_state = s->si[0].state;
1860 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01001861 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
1862 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001863
1864 /* Trick: if a request is being waiting for the server to respond,
1865 * and if we know the server can timeout, we don't want the timeout
1866 * to expire on the client side first, but we're still interested
1867 * in passing data from the client to the server (eg: POST). Thus,
1868 * we can cancel the client's request timeout if the server's
1869 * request timeout is set and the server has not yet sent a response.
1870 */
1871
Willy Tarreau520d95e2009-09-19 21:04:57 +02001872 if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01001873 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
1874 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001875 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01001876 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001877
Willy Tarreau7a20aa62010-07-13 16:30:45 +02001878 /* Call the stream interfaces' I/O handlers when embedded.
Willy Tarreau1accfc02009-09-05 20:57:35 +02001879 * Note that this one may wake the task up again.
1880 */
Willy Tarreau7a20aa62010-07-13 16:30:45 +02001881 if (s->req->cons->iohandler || s->rep->cons->iohandler) {
1882 if (s->req->cons->iohandler)
1883 s->req->cons->iohandler(s->req->cons);
1884 if (s->rep->cons->iohandler)
1885 s->rep->cons->iohandler(s->rep->cons);
Willy Tarreau1accfc02009-09-05 20:57:35 +02001886 if (task_in_rq(t)) {
1887 /* If we woke up, we don't want to requeue the
1888 * task to the wait queue, but rather requeue
1889 * it into the runqueue ASAP.
1890 */
1891 t->expire = TICK_ETERNITY;
1892 return t;
1893 }
1894 }
1895
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001896 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
1897 tick_first(s->rep->rex, s->rep->wex));
1898 if (s->req->analysers)
1899 t->expire = tick_first(t->expire, s->req->analyse_exp);
1900
1901 if (s->si[0].exp)
1902 t->expire = tick_first(t->expire, s->si[0].exp);
1903
1904 if (s->si[1].exp)
1905 t->expire = tick_first(t->expire, s->si[1].exp);
1906
1907#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01001908 fprintf(stderr,
1909 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
1910 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
1911 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
1912 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 +01001913#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001914
1915#ifdef DEBUG_DEV
1916 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01001917 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001918 ABORT_NOW();
1919#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01001920 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001921 }
1922
1923 s->fe->feconn--;
1924 if (s->flags & SN_BE_ASSIGNED)
1925 s->be->beconn--;
1926 actconn--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02001927 s->listener->nbconn--;
1928 if (s->listener->state == LI_FULL &&
1929 s->listener->nbconn < s->listener->maxconn) {
1930 /* we should reactivate the listener */
1931 EV_FD_SET(s->listener->fd, DIR_RD);
1932 s->listener->state = LI_READY;
1933 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001934
1935 if (unlikely((global.mode & MODE_DEBUG) &&
1936 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1937 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001938 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001939 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01001940 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001941 write(1, trash, len);
1942 }
1943
1944 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
1945 session_process_counters(s);
1946
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001947 if (s->txn.status) {
1948 int n;
1949
1950 n = s->txn.status / 100;
1951 if (n < 1 || n > 5)
1952 n = 0;
1953
1954 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau24657792010-02-26 10:30:28 +01001955 s->fe->counters.fe.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001956
Willy Tarreau24657792010-02-26 10:30:28 +01001957 if ((s->flags & SN_BE_ASSIGNED) &&
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001958 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau24657792010-02-26 10:30:28 +01001959 s->be->counters.be.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001960 }
1961
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001962 /* let's do a final log if we need it */
1963 if (s->logs.logwait &&
1964 !(s->flags & SN_MONITOR) &&
1965 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001966 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001967 }
1968
1969 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001970 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01001971 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001972 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01001973 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001974}
1975
Willy Tarreau7c669d72008-06-20 15:04:11 +02001976/*
1977 * This function adjusts sess->srv_conn and maintains the previous and new
1978 * server's served session counts. Setting newsrv to NULL is enough to release
1979 * current connection slot. This function also notifies any LB algo which might
1980 * expect to be informed about any change in the number of active sessions on a
1981 * server.
1982 */
1983void sess_change_server(struct session *sess, struct server *newsrv)
1984{
1985 if (sess->srv_conn == newsrv)
1986 return;
1987
1988 if (sess->srv_conn) {
1989 sess->srv_conn->served--;
1990 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
1991 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
1992 sess->srv_conn = NULL;
1993 }
1994
1995 if (newsrv) {
1996 newsrv->served++;
1997 if (newsrv->proxy->lbprm.server_take_conn)
1998 newsrv->proxy->lbprm.server_take_conn(newsrv);
1999 sess->srv_conn = newsrv;
2000 }
2001}
2002
Willy Tarreau84455332009-03-15 22:34:05 +01002003/* Set correct session termination flags in case no analyser has done it. It
2004 * also counts a failed request if the server state has not reached the request
2005 * stage.
2006 */
2007void sess_set_term_flags(struct session *s)
2008{
2009 if (!(s->flags & SN_FINST_MASK)) {
2010 if (s->si[1].state < SI_ST_REQ) {
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002011
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002012 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002013 if (s->listener->counters)
2014 s->listener->counters->failed_req++;
2015
Willy Tarreau84455332009-03-15 22:34:05 +01002016 s->flags |= SN_FINST_R;
2017 }
2018 else if (s->si[1].state == SI_ST_QUE)
2019 s->flags |= SN_FINST_Q;
2020 else if (s->si[1].state < SI_ST_EST)
2021 s->flags |= SN_FINST_C;
Willy Tarreau033b2db2010-03-04 17:54:21 +01002022 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreau84455332009-03-15 22:34:05 +01002023 s->flags |= SN_FINST_D;
2024 else
2025 s->flags |= SN_FINST_L;
2026 }
2027}
2028
2029/* Handle server-side errors for default protocols. It is called whenever a a
2030 * connection setup is aborted or a request is aborted in queue. It sets the
2031 * session termination flags so that the caller does not have to worry about
2032 * them. It's installed as ->srv_error for the server-side stream_interface.
2033 */
2034void default_srv_error(struct session *s, struct stream_interface *si)
2035{
2036 int err_type = si->err_type;
2037 int err = 0, fin = 0;
2038
2039 if (err_type & SI_ET_QUEUE_ABRT) {
2040 err = SN_ERR_CLICL;
2041 fin = SN_FINST_Q;
2042 }
2043 else if (err_type & SI_ET_CONN_ABRT) {
2044 err = SN_ERR_CLICL;
2045 fin = SN_FINST_C;
2046 }
2047 else if (err_type & SI_ET_QUEUE_TO) {
2048 err = SN_ERR_SRVTO;
2049 fin = SN_FINST_Q;
2050 }
2051 else if (err_type & SI_ET_QUEUE_ERR) {
2052 err = SN_ERR_SRVCL;
2053 fin = SN_FINST_Q;
2054 }
2055 else if (err_type & SI_ET_CONN_TO) {
2056 err = SN_ERR_SRVTO;
2057 fin = SN_FINST_C;
2058 }
2059 else if (err_type & SI_ET_CONN_ERR) {
2060 err = SN_ERR_SRVCL;
2061 fin = SN_FINST_C;
2062 }
2063 else /* SI_ET_CONN_OTHER and others */ {
2064 err = SN_ERR_INTERNAL;
2065 fin = SN_FINST_C;
2066 }
2067
2068 if (!(s->flags & SN_ERR_MASK))
2069 s->flags |= err;
2070 if (!(s->flags & SN_FINST_MASK))
2071 s->flags |= fin;
2072}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002073
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002074
Willy Tarreau8b22a712010-06-18 17:46:06 +02002075/************************************************************************/
2076/* All supported ACL keywords must be declared here. */
2077/************************************************************************/
2078
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002079/* set test->i to the cumulated number of connections in the stksess entry <ts> */
2080static int
2081acl_fetch_conn_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2082{
2083 test->flags = ACL_TEST_F_VOL_TEST;
2084 test->i = 0;
2085 if (ts != NULL) {
2086 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CNT);
2087 if (!ptr)
2088 return 0; /* parameter not stored */
2089 test->i = stktable_data_cast(ptr, conn_cnt);
2090 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002091 return 1;
2092}
2093
2094/* set test->i to the cumulated number of connections from the session's tracked counters */
2095static int
2096acl_fetch_trk_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2097 struct acl_expr *expr, struct acl_test *test)
2098{
2099 if (!l4->tracked_counters)
2100 return 0;
2101
2102 return acl_fetch_conn_cnt(l4->tracked_table, test, l4->tracked_counters);
2103}
2104
2105/* set test->i to the cumulated number of connections from the session's source
2106 * address in the table pointed to by expr.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002107 */
2108static int
2109acl_fetch_src_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2110 struct acl_expr *expr, struct acl_test *test)
2111{
Willy Tarreau8b22a712010-06-18 17:46:06 +02002112 struct stktable_key *key;
2113
2114 key = tcpv4_src_to_stktable_key(l4);
2115 if (!key)
2116 return 0; /* only TCPv4 is supported right now */
2117
2118 if (expr->arg_len)
2119 px = find_stktable(expr->arg.str);
2120
2121 if (!px)
2122 return 0; /* table not found */
2123
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002124 return acl_fetch_conn_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau8b22a712010-06-18 17:46:06 +02002125}
2126
2127/* set test->i to the number of connections from the session's source address
2128 * in the table pointed to by expr, after updating it.
2129 */
2130static int
2131acl_fetch_src_updt_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2132 struct acl_expr *expr, struct acl_test *test)
2133{
2134 struct stksess *ts;
2135 struct stktable_key *key;
2136 void *ptr;
2137
2138 key = tcpv4_src_to_stktable_key(l4);
2139 if (!key)
2140 return 0; /* only TCPv4 is supported right now */
2141
2142 if (expr->arg_len)
2143 px = find_stktable(expr->arg.str);
2144
2145 if (!px)
2146 return 0; /* table not found */
2147
2148 if ((ts = stktable_lookup_key(&px->table, key)) == NULL) {
2149 /* entry does not exist, initialize a new one */
2150 ts = stksess_new(&px->table, key);
2151 if (!ts)
2152 return 0;
2153 stktable_store(&px->table, ts);
2154 }
2155 else
2156 stktable_touch(&px->table, ts);
2157
2158 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2159 if (!ptr)
2160 return 0; /* parameter not stored in this table */
2161
2162 test->i = ++stktable_data_cast(ptr, conn_cnt);
2163 test->flags = ACL_TEST_F_VOL_TEST;
2164 return 1;
2165}
2166
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002167/* set test->i to the number of concurrent connections in the stksess entry <ts> */
2168static int
2169acl_fetch_conn_cur(struct stktable *table, struct acl_test *test, struct stksess *ts)
2170{
2171 test->flags = ACL_TEST_F_VOL_TEST;
2172 test->i = 0;
2173
2174 if (ts != NULL) {
2175 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CUR);
2176 if (!ptr)
2177 return 0; /* parameter not stored */
2178 test->i = stktable_data_cast(ptr, conn_cur);
2179 }
2180 return 1;
2181}
2182
2183/* set test->i to the number of concurrent connections from the session's tracked counters */
2184static int
2185acl_fetch_trk_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2186 struct acl_expr *expr, struct acl_test *test)
2187{
2188 if (!l4->tracked_counters)
2189 return 0;
2190
2191 return acl_fetch_conn_cur(l4->tracked_table, test, l4->tracked_counters);
2192}
2193
Willy Tarreau38285c12010-06-18 16:35:43 +02002194/* set test->i to the number of concurrent connections from the session's source
2195 * address in the table pointed to by expr.
2196 */
2197static int
2198acl_fetch_src_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2199 struct acl_expr *expr, struct acl_test *test)
2200{
Willy Tarreau38285c12010-06-18 16:35:43 +02002201 struct stktable_key *key;
2202
2203 key = tcpv4_src_to_stktable_key(l4);
2204 if (!key)
2205 return 0; /* only TCPv4 is supported right now */
2206
2207 if (expr->arg_len)
2208 px = find_stktable(expr->arg.str);
2209
2210 if (!px)
2211 return 0; /* table not found */
2212
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002213 return acl_fetch_conn_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau38285c12010-06-18 16:35:43 +02002214}
2215
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002216/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2217static int
2218acl_fetch_sess_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2219{
2220 test->flags = ACL_TEST_F_VOL_TEST;
2221 test->i = 0;
2222 if (ts != NULL) {
2223 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_CNT);
2224 if (!ptr)
2225 return 0; /* parameter not stored */
2226 test->i = stktable_data_cast(ptr, sess_cnt);
2227 }
2228 return 1;
2229}
2230
2231/* set test->i to the cumulated number of sessions from the session's tracked counters */
2232static int
2233acl_fetch_trk_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2234 struct acl_expr *expr, struct acl_test *test)
2235{
2236 if (!l4->tracked_counters)
2237 return 0;
2238
2239 return acl_fetch_sess_cnt(l4->tracked_table, test, l4->tracked_counters);
2240}
2241
2242/* set test->i to the cumulated number of session from the session's source
2243 * address in the table pointed to by expr.
2244 */
2245static int
2246acl_fetch_src_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2247 struct acl_expr *expr, struct acl_test *test)
2248{
2249 struct stktable_key *key;
2250
2251 key = tcpv4_src_to_stktable_key(l4);
2252 if (!key)
2253 return 0; /* only TCPv4 is supported right now */
2254
2255 if (expr->arg_len)
2256 px = find_stktable(expr->arg.str);
2257
2258 if (!px)
2259 return 0; /* table not found */
2260
2261 return acl_fetch_sess_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2262}
2263
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002264/* set test->i to the number of kbytes received from clients matching the stksess entry <ts> */
2265static int
2266acl_fetch_kbytes_in(struct stktable *table, struct acl_test *test, struct stksess *ts)
2267{
2268 test->flags = ACL_TEST_F_VOL_TEST;
2269 test->i = 0;
2270
2271 if (ts != NULL) {
2272 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_CNT);
2273 if (!ptr)
2274 return 0; /* parameter not stored */
2275 test->i = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
2276 }
2277 return 1;
2278}
2279
2280/* set test->i to the number of kbytes received from clients according to the
2281 * session's tracked counters.
2282 */
2283static int
2284acl_fetch_trk_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2285 struct acl_expr *expr, struct acl_test *test)
2286{
2287 if (!l4->tracked_counters)
2288 return 0;
2289
2290 return acl_fetch_kbytes_in(l4->tracked_table, test, l4->tracked_counters);
2291}
2292
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002293/* set test->i to the number of kbytes received from the session's source
2294 * address in the table pointed to by expr.
2295 */
2296static int
2297acl_fetch_src_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2298 struct acl_expr *expr, struct acl_test *test)
2299{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002300 struct stktable_key *key;
2301
2302 key = tcpv4_src_to_stktable_key(l4);
2303 if (!key)
2304 return 0; /* only TCPv4 is supported right now */
2305
2306 if (expr->arg_len)
2307 px = find_stktable(expr->arg.str);
2308
2309 if (!px)
2310 return 0; /* table not found */
2311
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002312 return acl_fetch_kbytes_in(&px->table, test, stktable_lookup_key(&px->table, key));
2313}
2314
2315/* set test->i to the number of kbytes sent to clients matching the stksess entry <ts> */
2316static int
2317acl_fetch_kbytes_out(struct stktable *table, struct acl_test *test, struct stksess *ts)
2318{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002319 test->flags = ACL_TEST_F_VOL_TEST;
2320 test->i = 0;
2321
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002322 if (ts != NULL) {
2323 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002324 if (!ptr)
2325 return 0; /* parameter not stored */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002326 test->i = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002327 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002328 return 1;
2329}
2330
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002331/* set test->i to the number of kbytes sent to clients according to the session's
2332 * tracked counters.
2333 */
2334static int
2335acl_fetch_trk_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
2336 struct acl_expr *expr, struct acl_test *test)
2337{
2338 if (!l4->tracked_counters)
2339 return 0;
2340
2341 return acl_fetch_kbytes_out(l4->tracked_table, test, l4->tracked_counters);
2342}
2343
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002344/* set test->i to the number of kbytes sent to the session's source address in
2345 * the table pointed to by expr.
2346 */
2347static int
2348acl_fetch_src_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
2349 struct acl_expr *expr, struct acl_test *test)
2350{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002351 struct stktable_key *key;
2352
2353 key = tcpv4_src_to_stktable_key(l4);
2354 if (!key)
2355 return 0; /* only TCPv4 is supported right now */
2356
2357 if (expr->arg_len)
2358 px = find_stktable(expr->arg.str);
2359
2360 if (!px)
2361 return 0; /* table not found */
2362
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002363 return acl_fetch_kbytes_out(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002364}
2365
Willy Tarreau8b22a712010-06-18 17:46:06 +02002366
2367/* Note: must not be declared <const> as its list will be overwritten */
2368static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002369 { "trk_conn_cnt", acl_parse_int, acl_fetch_trk_conn_cnt, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau8b22a712010-06-18 17:46:06 +02002370 { "src_conn_cnt", acl_parse_int, acl_fetch_src_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
2371 { "src_updt_conn_cnt", acl_parse_int, acl_fetch_src_updt_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002372 { "trk_conn_cur", acl_parse_int, acl_fetch_trk_conn_cur, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau38285c12010-06-18 16:35:43 +02002373 { "src_conn_cur", acl_parse_int, acl_fetch_src_conn_cur, acl_match_int, ACL_USE_TCP4_VOLATILE },
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002374 { "trk_sess_cnt", acl_parse_int, acl_fetch_trk_sess_cnt, acl_match_int, ACL_USE_NOTHING },
2375 { "src_sess_cnt", acl_parse_int, acl_fetch_src_sess_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002376 { "trk_kbytes_in", acl_parse_int, acl_fetch_trk_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002377 { "src_kbytes_in", acl_parse_int, acl_fetch_src_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002378 { "trk_kbytes_out", acl_parse_int, acl_fetch_trk_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002379 { "src_kbytes_out", acl_parse_int, acl_fetch_src_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
Willy Tarreau8b22a712010-06-18 17:46:06 +02002380 { NULL, NULL, NULL, NULL },
2381}};
2382
2383
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002384/* Parse a "track-counters" line starting with "track-counters" in args[arg-1].
2385 * Returns the number of warnings emitted, or -1 in case of fatal errors. The
2386 * <prm> struct is fed with the table name if any. If unspecified, the caller
2387 * will assume that the current proxy's table is used.
2388 */
2389int parse_track_counters(char **args, int *arg,
2390 int section_type, struct proxy *curpx,
2391 struct track_ctr_prm *prm,
2392 struct proxy *defpx, char *err, int errlen)
2393{
2394 int pattern_type = 0;
2395
2396 /* parse the arguments of "track-counters" before the condition in the
2397 * following form :
2398 * track-counters src [ table xxx ] [ if|unless ... ]
2399 */
2400 while (args[*arg]) {
2401 if (strcmp(args[*arg], "src") == 0) {
2402 prm->type = STKTABLE_TYPE_IP;
2403 pattern_type = 1;
2404 }
2405 else if (strcmp(args[*arg], "table") == 0) {
2406 if (!args[*arg + 1]) {
2407 snprintf(err, errlen,
2408 "missing table for track-counter in %s '%s'.",
2409 proxy_type_str(curpx), curpx->id);
2410 return -1;
2411 }
2412 /* we copy the table name for now, it will be resolved later */
2413 prm->table.n = strdup(args[*arg + 1]);
2414 (*arg)++;
2415 }
2416 else {
2417 /* unhandled keywords are handled by the caller */
2418 break;
2419 }
2420 (*arg)++;
2421 }
2422
2423 if (!pattern_type) {
2424 snprintf(err, errlen,
2425 "track-counter key not specified in %s '%s' (found %s, only 'src' is supported).",
2426 proxy_type_str(curpx), curpx->id, quote_arg(args[*arg]));
2427 return -1;
2428 }
2429
2430 return 0;
2431}
2432
Willy Tarreau8b22a712010-06-18 17:46:06 +02002433__attribute__((constructor))
2434static void __session_init(void)
2435{
2436 acl_register_keywords(&acl_kws);
2437}
2438
Willy Tarreaubaaee002006-06-26 02:48:02 +02002439/*
2440 * Local variables:
2441 * c-indent-level: 8
2442 * c-basic-offset: 8
2443 * End:
2444 */