blob: 120f670f7ada88f1d72c85a9ff5f737af56f092c [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau81f9aa32010-06-01 17:45:26 +02002 * Session management functions.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003 *
Willy Tarreau81f9aa32010-06-01 17:45:26 +02004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <stdlib.h>
Willy Tarreau81f9aa32010-06-01 17:45:26 +020014#include <unistd.h>
15#include <fcntl.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020016
17#include <common/config.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020018#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020019#include <common/memory.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020020
Willy Tarreaubaaee002006-06-26 02:48:02 +020021#include <types/capture.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010022#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020023
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020024#include <proto/acl.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010025#include <proto/backend.h>
Willy Tarreau7341d942007-05-13 19:56:02 +020026#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010027#include <proto/checks.h>
Willy Tarreau5ca791d2009-08-16 19:06:42 +020028#include <proto/dumpstats.h>
Willy Tarreau91c43d72010-06-20 11:19:22 +020029#include <proto/freq_ctr.h>
Willy Tarreau3041b9f2010-10-15 23:25:20 +020030#include <proto/frontend.h>
Willy Tarreau8d5d7f22007-01-21 19:16:41 +010031#include <proto/hdr_idx.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020032#include <proto/log.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020033#include <proto/session.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010034#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010035#include <proto/proto_http.h>
36#include <proto/proto_tcp.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020037#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010039#include <proto/server.h>
Emeric Brun1d33b292010-01-04 15:47:17 +010040#include <proto/stick_table.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010041#include <proto/stream_interface.h>
42#include <proto/stream_sock.h>
43#include <proto/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020045struct pool_head *pool2_session;
Willy Tarreauf54f8bd2008-11-23 19:53:55 +010046struct list sessions;
Willy Tarreaubaaee002006-06-26 02:48:02 +020047
Willy Tarreau81f9aa32010-06-01 17:45:26 +020048/* This function is called from the protocol layer accept() in order to instanciate
49 * a new session on behalf of a given listener and frontend. It returns a positive
Willy Tarreauabe8ea52010-11-11 10:56:04 +010050 * value upon success, 0 if the connection can be ignored, or a negative value upon
51 * critical failure. The accepted file descriptor is closed if we return <= 0.
Willy Tarreau81f9aa32010-06-01 17:45:26 +020052 */
53int session_accept(struct listener *l, int cfd, struct sockaddr_storage *addr)
54{
55 struct proxy *p = l->frontend;
56 struct session *s;
57 struct http_txn *txn;
58 struct task *t;
Willy Tarreauabe8ea52010-11-11 10:56:04 +010059 int ret;
60
61
62 ret = -1; /* assume unrecoverable error by default */
Willy Tarreau81f9aa32010-06-01 17:45:26 +020063
Willy Tarreaufffe1322010-11-11 09:48:16 +010064 if (unlikely((s = pool_alloc2(pool2_session)) == NULL))
Willy Tarreau81f9aa32010-06-01 17:45:26 +020065 goto out_close;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020066
67 /* minimum session initialization required for monitor mode below */
68 s->flags = 0;
69 s->logs.logwait = p->to_log;
Willy Tarreau56123282010-08-06 19:06:56 +020070 s->stkctr1_entry = NULL;
71 s->stkctr2_entry = NULL;
72 s->stkctr1_table = NULL;
73 s->stkctr2_table = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020074
75 /* if this session comes from a known monitoring system, we want to ignore
76 * it as soon as possible, which means closing it immediately for TCP, but
77 * cleanly.
78 */
79 if (unlikely((l->options & LI_O_CHK_MONNET) &&
80 addr->ss_family == AF_INET &&
81 (((struct sockaddr_in *)addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr)) {
82 if (p->mode == PR_MODE_TCP) {
Willy Tarreauabe8ea52010-11-11 10:56:04 +010083 ret = 0; /* successful termination */
84 goto out_free_session;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020085 }
86 s->flags |= SN_MONITOR;
87 s->logs.logwait = 0;
88 }
89
Willy Tarreauabe8ea52010-11-11 10:56:04 +010090 if (unlikely((t = task_new()) == NULL))
91 goto out_free_session;
92
Willy Tarreau81f9aa32010-06-01 17:45:26 +020093 /* OK, we're keeping the session, so let's properly initialize the session */
94 LIST_ADDQ(&sessions, &s->list);
95 LIST_INIT(&s->back_refs);
96
Willy Tarreau81f9aa32010-06-01 17:45:26 +020097 s->term_trace = 0;
98 s->cli_addr = *addr;
99 s->logs.accept_date = date; /* user-visible date for logging */
100 s->logs.tv_accept = now; /* corrected date for internal use */
101 s->uniq_id = totalconn;
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200102 p->feconn++; /* beconn will be increased once assigned */
103
Willy Tarreaub36b4242010-06-04 20:59:39 +0200104 proxy_inc_fe_conn_ctr(l, p); /* note: cum_beconn will be increased once assigned */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200105
106 t->process = l->handler;
107 t->context = s;
108 t->nice = l->nice;
109 t->expire = TICK_ETERNITY;
110
111 s->task = t;
112 s->listener = l;
113
114 /* Note: initially, the session's backend points to the frontend.
115 * This changes later when switching rules are executed or
116 * when the default backend is assigned.
117 */
118 s->be = s->fe = p;
119 s->req = s->rep = NULL; /* will be allocated later */
120
121 /* now evaluate the tcp-request layer4 rules. Since we expect to be able
122 * to abort right here as soon as possible, we check the rules before
123 * even initializing the stream interfaces.
124 */
125 if ((l->options & LI_O_TCP_RULES) && !tcp_exec_req_rules(s)) {
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200126 /* let's do a no-linger now to close with a single RST. */
127 setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100128 ret = 0; /* successful termination */
129 goto out_free_task;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200130 }
131
Willy Tarreaub36b4242010-06-04 20:59:39 +0200132 /* This session was accepted, count it now */
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200133 if (p->feconn > p->counters.feconn_max)
134 p->counters.feconn_max = p->feconn;
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100135
Willy Tarreaub36b4242010-06-04 20:59:39 +0200136 proxy_inc_fe_sess_ctr(l, p);
Willy Tarreau56123282010-08-06 19:06:56 +0200137 if (s->stkctr1_entry) {
Willy Tarreau91c43d72010-06-20 11:19:22 +0200138 void *ptr;
139
Willy Tarreau56123282010-08-06 19:06:56 +0200140 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200141 if (ptr)
142 stktable_data_cast(ptr, sess_cnt)++;
Willy Tarreau91c43d72010-06-20 11:19:22 +0200143
Willy Tarreau56123282010-08-06 19:06:56 +0200144 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +0200145 if (ptr)
146 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200147 s->stkctr1_table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200148 }
Willy Tarreaub36b4242010-06-04 20:59:39 +0200149
Willy Tarreau56123282010-08-06 19:06:56 +0200150 if (s->stkctr2_entry) {
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200151 void *ptr;
152
Willy Tarreau56123282010-08-06 19:06:56 +0200153 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_SESS_CNT);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200154 if (ptr)
155 stktable_data_cast(ptr, sess_cnt)++;
156
Willy Tarreau56123282010-08-06 19:06:56 +0200157 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_SESS_RATE);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200158 if (ptr)
159 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200160 s->stkctr2_table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200161 }
162
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200163 /* this part should be common with other protocols */
164 s->si[0].fd = cfd;
165 s->si[0].owner = t;
166 s->si[0].state = s->si[0].prev_state = SI_ST_EST;
167 s->si[0].err_type = SI_ET_NONE;
168 s->si[0].err_loc = NULL;
169 s->si[0].connect = NULL;
170 s->si[0].iohandler = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200171 s->si[0].release = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200172 s->si[0].exp = TICK_ETERNITY;
173 s->si[0].flags = SI_FL_NONE;
174
175 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
176 s->si[0].flags |= SI_FL_INDEP_STR;
177
178 if (addr->ss_family == AF_INET || addr->ss_family == AF_INET6)
179 s->si[0].flags = SI_FL_CAP_SPLTCP; /* TCP/TCPv6 splicing possible */
180
181 /* add the various callbacks */
182 stream_sock_prepare_interface(&s->si[0]);
183
184 /* pre-initialize the other side's stream interface to an INIT state. The
185 * callbacks will be initialized before attempting to connect.
186 */
187 s->si[1].fd = -1; /* just to help with debugging */
188 s->si[1].owner = t;
189 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
190 s->si[1].err_type = SI_ET_NONE;
191 s->si[1].err_loc = NULL;
192 s->si[1].connect = NULL;
193 s->si[1].iohandler = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200194 s->si[1].release = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200195 s->si[1].shutr = stream_int_shutr;
196 s->si[1].shutw = stream_int_shutw;
197 s->si[1].exp = TICK_ETERNITY;
198 s->si[1].flags = SI_FL_NONE;
199
200 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
201 s->si[1].flags |= SI_FL_INDEP_STR;
202
203 s->srv = s->prev_srv = s->srv_conn = NULL;
204 s->pend_pos = NULL;
205
206 /* init store persistence */
207 s->store_count = 0;
208
209 /* Adjust some socket options */
Willy Tarreaufffe1322010-11-11 09:48:16 +0100210 if (unlikely(fcntl(cfd, F_SETFL, O_NONBLOCK) == -1))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200211 goto out_free_task;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200212
213 txn = &s->txn;
214 /* Those variables will be checked and freed if non-NULL in
215 * session.c:session_free(). It is important that they are
216 * properly initialized.
217 */
218 txn->sessid = NULL;
219 txn->srv_cookie = NULL;
220 txn->cli_cookie = NULL;
221 txn->uri = NULL;
222 txn->req.cap = NULL;
223 txn->rsp.cap = NULL;
224 txn->hdr_idx.v = NULL;
225 txn->hdr_idx.size = txn->hdr_idx.used = 0;
226
227 if (unlikely((s->req = pool_alloc2(pool2_buffer)) == NULL))
228 goto out_free_task; /* no memory */
229
230 if (unlikely((s->rep = pool_alloc2(pool2_buffer)) == NULL))
231 goto out_free_req; /* no memory */
232
233 /* initialize the request buffer */
234 s->req->size = global.tune.bufsize;
235 buffer_init(s->req);
236 s->req->prod = &s->si[0];
237 s->req->cons = &s->si[1];
238 s->si[0].ib = s->si[1].ob = s->req;
239 s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */
240
241 /* activate default analysers enabled for this listener */
242 s->req->analysers = l->analysers;
243
244 s->req->wto = TICK_ETERNITY;
245 s->req->rto = TICK_ETERNITY;
246 s->req->rex = TICK_ETERNITY;
247 s->req->wex = TICK_ETERNITY;
248 s->req->analyse_exp = TICK_ETERNITY;
249
250 /* initialize response buffer */
251 s->rep->size = global.tune.bufsize;
252 buffer_init(s->rep);
253 s->rep->prod = &s->si[1];
254 s->rep->cons = &s->si[0];
255 s->si[0].ob = s->si[1].ib = s->rep;
256 s->rep->analysers = 0;
257
258 s->rep->rto = TICK_ETERNITY;
259 s->rep->wto = TICK_ETERNITY;
260 s->rep->rex = TICK_ETERNITY;
261 s->rep->wex = TICK_ETERNITY;
262 s->rep->analyse_exp = TICK_ETERNITY;
263
264 /* finish initialization of the accepted file descriptor */
265 fd_insert(cfd);
266 fdtab[cfd].owner = &s->si[0];
267 fdtab[cfd].state = FD_STREADY;
268 fdtab[cfd].flags = 0;
269 fdtab[cfd].cb[DIR_RD].f = l->proto->read;
270 fdtab[cfd].cb[DIR_RD].b = s->req;
271 fdtab[cfd].cb[DIR_WR].f = l->proto->write;
272 fdtab[cfd].cb[DIR_WR].b = s->rep;
273 fdinfo[cfd].peeraddr = (struct sockaddr *)&s->cli_addr;
274 fdinfo[cfd].peerlen = sizeof(s->cli_addr);
275 EV_FD_SET(cfd, DIR_RD);
276
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100277 if (p->accept && (ret = p->accept(s)) <= 0) {
278 /* Either we had an unrecoverable error (<0) or work is
279 * finished (=0, eg: monitoring), in both situations,
280 * we can release everything and close.
281 */
282 goto out_free_rep;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200283 }
284
285 /* it is important not to call the wakeup function directly but to
286 * pass through task_wakeup(), because this one knows how to apply
287 * priorities to tasks.
288 */
289 task_wakeup(t, TASK_WOKEN_INIT);
290 return 1;
291
292 /* Error unrolling */
293 out_free_rep:
294 pool_free2(pool2_buffer, s->rep);
295 out_free_req:
296 pool_free2(pool2_buffer, s->req);
297 out_free_task:
Willy Tarreau24dcaf32010-06-05 10:49:41 +0200298 p->feconn--;
Willy Tarreau56123282010-08-06 19:06:56 +0200299 if (s->stkctr1_entry || s->stkctr2_entry)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200300 session_store_counters(s);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200301 task_free(t);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200302 LIST_DEL(&s->list);
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100303 out_free_session:
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200304 pool_free2(pool2_session, s);
305 out_close:
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100306 if (fdtab[cfd].state != FD_STCLOSE)
307 fd_delete(cfd);
308 else
309 close(cfd);
310 return ret;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200311}
312
Willy Tarreaubaaee002006-06-26 02:48:02 +0200313/*
314 * frees the context associated to a session. It must have been removed first.
315 */
316void session_free(struct session *s)
317{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +0100318 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +0200319 struct proxy *fe = s->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100320 struct bref *bref, *back;
Willy Tarreaua4cda672010-06-06 18:28:49 +0200321 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100322
Willy Tarreaubaaee002006-06-26 02:48:02 +0200323 if (s->pend_pos)
324 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100325
Willy Tarreau1e62de62008-11-11 20:20:02 +0100326 if (s->srv) { /* there may be requests left pending in queue */
327 if (s->flags & SN_CURR_SESS) {
328 s->flags &= ~SN_CURR_SESS;
329 s->srv->cur_sess--;
330 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100331 if (may_dequeue_tasks(s->srv, s->be))
332 process_srv_queue(s->srv);
Willy Tarreau1e62de62008-11-11 20:20:02 +0100333 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100334
Willy Tarreau7c669d72008-06-20 15:04:11 +0200335 if (unlikely(s->srv_conn)) {
336 /* the session still has a reserved slot on a server, but
337 * it should normally be only the same as the one above,
338 * so this should not happen in fact.
339 */
340 sess_change_server(s, NULL);
341 }
342
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100343 if (s->req->pipe)
344 put_pipe(s->req->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100345
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100346 if (s->rep->pipe)
347 put_pipe(s->rep->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100348
Willy Tarreau48d63db2008-08-03 17:41:33 +0200349 pool_free2(pool2_buffer, s->req);
350 pool_free2(pool2_buffer, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200351
Willy Tarreau46023632010-01-07 22:51:47 +0100352 http_end_txn(s);
353
Willy Tarreaua4cda672010-06-06 18:28:49 +0200354 for (i = 0; i < s->store_count; i++) {
355 if (!s->store[i].ts)
356 continue;
357 stksess_free(s->store[i].table, s->store[i].ts);
358 s->store[i].ts = NULL;
359 }
360
Willy Tarreau92fb9832007-10-16 17:34:28 +0200361 if (fe) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200362 pool_free2(fe->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau46023632010-01-07 22:51:47 +0100363 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
364 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200365 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100366
Willy Tarreau56123282010-08-06 19:06:56 +0200367 if (s->stkctr1_entry || s->stkctr2_entry)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200368 session_store_counters(s);
369
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100370 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100371 /* we have to unlink all watchers. We must not relink them if
372 * this session was the last one in the list.
373 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100374 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100375 LIST_INIT(&bref->users);
376 if (s->list.n != &sessions)
377 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100378 bref->ref = s->list.n;
379 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100380 LIST_DEL(&s->list);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200381 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200382
383 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200384 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau48d63db2008-08-03 17:41:33 +0200385 pool_flush2(pool2_buffer);
386 pool_flush2(fe->hdr_idx_pool);
387 pool_flush2(pool2_requri);
388 pool_flush2(pool2_capture);
389 pool_flush2(pool2_session);
390 pool_flush2(fe->req_cap_pool);
391 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200392 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200393}
394
395
396/* perform minimal intializations, report 0 in case of error, 1 if OK. */
397int init_session()
398{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100399 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200400 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
401 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200402}
403
Willy Tarreau30e71012007-11-26 20:15:35 +0100404void session_process_counters(struct session *s)
405{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100406 unsigned long long bytes;
407
Willy Tarreau30e71012007-11-26 20:15:35 +0100408 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100409 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100410 s->logs.bytes_in = s->req->total;
411 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200412 s->fe->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100413
Willy Tarreau30e71012007-11-26 20:15:35 +0100414 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200415 s->be->counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100416
Willy Tarreau30e71012007-11-26 20:15:35 +0100417 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200418 s->srv->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200419
420 if (s->listener->counters)
421 s->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200422
Willy Tarreau56123282010-08-06 19:06:56 +0200423 if (s->stkctr2_entry) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200424 void *ptr;
425
Willy Tarreau56123282010-08-06 19:06:56 +0200426 ptr = stktable_data_ptr(s->stkctr2_table,
427 s->stkctr2_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200428 STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200429 if (ptr)
430 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200431
Willy Tarreau56123282010-08-06 19:06:56 +0200432 ptr = stktable_data_ptr(s->stkctr2_table,
433 s->stkctr2_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200434 STKTABLE_DT_BYTES_IN_RATE);
435 if (ptr)
436 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200437 s->stkctr2_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200438 }
439
Willy Tarreau56123282010-08-06 19:06:56 +0200440 if (s->stkctr1_entry) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200441 void *ptr;
442
Willy Tarreau56123282010-08-06 19:06:56 +0200443 ptr = stktable_data_ptr(s->stkctr1_table,
444 s->stkctr1_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200445 STKTABLE_DT_BYTES_IN_CNT);
446 if (ptr)
447 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
448
Willy Tarreau56123282010-08-06 19:06:56 +0200449 ptr = stktable_data_ptr(s->stkctr1_table,
450 s->stkctr1_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200451 STKTABLE_DT_BYTES_IN_RATE);
452 if (ptr)
453 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200454 s->stkctr1_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200455 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100456 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100457 }
458
Willy Tarreau30e71012007-11-26 20:15:35 +0100459 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100460 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100461 s->logs.bytes_out = s->rep->total;
462 if (bytes) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200463 s->fe->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100464
Willy Tarreau30e71012007-11-26 20:15:35 +0100465 if (s->be != s->fe)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200466 s->be->counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100467
Willy Tarreau30e71012007-11-26 20:15:35 +0100468 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200469 s->srv->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200470
471 if (s->listener->counters)
472 s->listener->counters->bytes_out += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200473
Willy Tarreau56123282010-08-06 19:06:56 +0200474 if (s->stkctr2_entry) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200475 void *ptr;
476
Willy Tarreau56123282010-08-06 19:06:56 +0200477 ptr = stktable_data_ptr(s->stkctr2_table,
478 s->stkctr2_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200479 STKTABLE_DT_BYTES_OUT_CNT);
480 if (ptr)
481 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
482
Willy Tarreau56123282010-08-06 19:06:56 +0200483 ptr = stktable_data_ptr(s->stkctr2_table,
484 s->stkctr2_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200485 STKTABLE_DT_BYTES_OUT_RATE);
486 if (ptr)
487 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200488 s->stkctr2_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200489 }
490
Willy Tarreau56123282010-08-06 19:06:56 +0200491 if (s->stkctr1_entry) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200492 void *ptr;
493
Willy Tarreau56123282010-08-06 19:06:56 +0200494 ptr = stktable_data_ptr(s->stkctr1_table,
495 s->stkctr1_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200496 STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200497 if (ptr)
498 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200499
Willy Tarreau56123282010-08-06 19:06:56 +0200500 ptr = stktable_data_ptr(s->stkctr1_table,
501 s->stkctr1_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200502 STKTABLE_DT_BYTES_OUT_RATE);
503 if (ptr)
504 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200505 s->stkctr1_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200506 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100507 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100508 }
509}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200510
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100511/* This function is called with (si->state == SI_ST_CON) meaning that a
512 * connection was attempted and that the file descriptor is already allocated.
513 * We must check for establishment, error and abort. Possible output states
514 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
515 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
516 * otherwise 1.
517 */
518int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
519{
520 struct buffer *req = si->ob;
521 struct buffer *rep = si->ib;
522
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100523 /* If we got an error, or if nothing happened and the connection timed
524 * out, we must give up. The CER state handler will take care of retry
525 * attempts and error reports.
526 */
527 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau127334e2009-03-28 10:47:26 +0100528 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100529 si->state = SI_ST_CER;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200530 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100531 fd_delete(si->fd);
532
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200533 if (si->release)
534 si->release(si);
535
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100536 if (si->err_type)
537 return 0;
538
539 si->err_loc = s->srv;
540 if (si->flags & SI_FL_ERR)
541 si->err_type = SI_ET_CONN_ERR;
542 else
543 si->err_type = SI_ET_CONN_TO;
544 return 0;
545 }
546
547 /* OK, maybe we want to abort */
Willy Tarreau418fd472009-09-06 21:37:23 +0200548 if (unlikely((rep->flags & BF_SHUTW) ||
549 ((req->flags & BF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200550 (((req->flags & (BF_OUT_EMPTY|BF_WRITE_ACTIVITY)) == BF_OUT_EMPTY) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100551 s->be->options & PR_O_ABRT_CLOSE)))) {
552 /* give up */
553 si->shutw(si);
554 si->err_type |= SI_ET_CONN_ABRT;
555 si->err_loc = s->srv;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200556 si->flags &= ~SI_FL_CAP_SPLICE;
Willy Tarreau84455332009-03-15 22:34:05 +0100557 if (s->srv_error)
558 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100559 return 1;
560 }
561
562 /* we need to wait a bit more if there was no activity either */
563 if (!(req->flags & BF_WRITE_ACTIVITY))
564 return 1;
565
566 /* OK, this means that a connection succeeded. The caller will be
567 * responsible for handling the transition from CON to EST.
568 */
569 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100570 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100571 si->state = SI_ST_EST;
572 si->err_type = SI_ET_NONE;
573 si->err_loc = NULL;
574 return 1;
575}
576
577/* This function is called with (si->state == SI_ST_CER) meaning that a
578 * previous connection attempt has failed and that the file descriptor
579 * has already been released. Possible causes include asynchronous error
580 * notification and time out. Possible output states are SI_ST_CLO when
581 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
582 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
583 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
584 * marked as in error state. It returns 0.
585 */
586int sess_update_st_cer(struct session *s, struct stream_interface *si)
587{
588 /* we probably have to release last session from the server */
589 if (s->srv) {
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100590 health_adjust(s->srv, HANA_STATUS_L4_ERR);
591
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100592 if (s->flags & SN_CURR_SESS) {
593 s->flags &= ~SN_CURR_SESS;
594 s->srv->cur_sess--;
595 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100596 }
597
598 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200599 si->conn_retries--;
600 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100601 if (!si->err_type) {
602 si->err_type = SI_ET_CONN_ERR;
603 si->err_loc = s->srv;
604 }
605
606 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200607 s->srv->counters.failed_conns++;
608 s->be->counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100609 sess_change_server(s, NULL);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100610 if (may_dequeue_tasks(s->srv, s->be))
611 process_srv_queue(s->srv);
612
613 /* shutw is enough so stop a connecting socket */
614 si->shutw(si);
615 si->ob->flags |= BF_WRITE_ERROR;
616 si->ib->flags |= BF_READ_ERROR;
617
618 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100619 if (s->srv_error)
620 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100621 return 0;
622 }
623
624 /* If the "redispatch" option is set on the backend, we are allowed to
625 * retry on another server for the last retry. In order to achieve this,
626 * we must mark the session unassigned, and eventually clear the DIRECT
627 * bit to ignore any persistence cookie. We won't count a retry nor a
628 * redispatch yet, because this will depend on what server is selected.
629 */
Willy Tarreauee28de02010-06-01 09:51:00 +0200630 if (s->srv && si->conn_retries == 0 &&
Willy Tarreau4de91492010-01-22 19:10:05 +0100631 s->be->options & PR_O_REDISP && !(s->flags & SN_FORCE_PRST)) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100632 sess_change_server(s, NULL);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100633 if (may_dequeue_tasks(s->srv, s->be))
634 process_srv_queue(s->srv);
635
636 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
637 s->prev_srv = s->srv;
638 si->state = SI_ST_REQ;
639 } else {
640 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200641 s->srv->counters.retries++;
642 s->be->counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100643 si->state = SI_ST_ASS;
644 }
645
646 if (si->flags & SI_FL_ERR) {
647 /* The error was an asynchronous connection error, and we will
648 * likely have to retry connecting to the same server, most
649 * likely leading to the same result. To avoid this, we wait
650 * one second before retrying.
651 */
652
653 if (!si->err_type)
654 si->err_type = SI_ET_CONN_ERR;
655
656 si->state = SI_ST_TAR;
657 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
658 return 0;
659 }
660 return 0;
661}
662
663/*
664 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200665 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
666 * SI_ST_INI) to SI_ST_EST, but only when a ->connect function is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100667 */
668void sess_establish(struct session *s, struct stream_interface *si)
669{
670 struct buffer *req = si->ob;
671 struct buffer *rep = si->ib;
672
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100673 if (s->srv)
674 health_adjust(s->srv, HANA_STATUS_L4_OK);
675
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100676 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100677 /* if the user wants to log as soon as possible, without counting
678 * bytes from the server, then this is the right moment. */
679 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
680 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100681 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100682 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100683 }
684 else {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100685 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
686 /* reset hdr_idx which was already initialized by the request.
687 * right now, the http parser does it.
688 * hdr_idx_init(&s->txn.hdr_idx);
689 */
690 }
691
Willy Tarreau4e5b8282009-08-16 22:57:50 +0200692 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100693 rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
Willy Tarreaud04e8582010-05-31 12:31:35 +0200694 if (si->connect) {
695 /* real connections have timeouts */
696 req->wto = s->be->timeout.server;
697 rep->rto = s->be->timeout.server;
698 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100699 req->wex = TICK_ETERNITY;
700}
701
702/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
703 * Other input states are simply ignored.
704 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
705 * Flags must have previously been updated for timeouts and other conditions.
706 */
707void sess_update_stream_int(struct session *s, struct stream_interface *si)
708{
709 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",
710 now_ms, __FUNCTION__,
711 s,
712 s->req, s->rep,
713 s->req->rex, s->rep->wex,
714 s->req->flags, s->rep->flags,
715 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
716
717 if (si->state == SI_ST_ASS) {
718 /* Server assigned to connection request, we have to try to connect now */
719 int conn_err;
720
721 conn_err = connect_server(s);
722 if (conn_err == SN_ERR_NONE) {
723 /* state = SI_ST_CON now */
Willy Tarreau8f6457c2008-12-01 00:08:28 +0100724 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100725 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100726 return;
727 }
728
729 /* We have received a synchronous error. We might have to
730 * abort, retry immediately or redispatch.
731 */
732 if (conn_err == SN_ERR_INTERNAL) {
733 if (!si->err_type) {
734 si->err_type = SI_ET_CONN_OTHER;
735 si->err_loc = s->srv;
736 }
737
738 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100739 srv_inc_sess_ctr(s->srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100740 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200741 s->srv->counters.failed_conns++;
742 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100743
744 /* release other sessions waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100745 sess_change_server(s, NULL);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100746 if (may_dequeue_tasks(s->srv, s->be))
747 process_srv_queue(s->srv);
748
749 /* Failed and not retryable. */
750 si->shutr(si);
751 si->shutw(si);
752 si->ob->flags |= BF_WRITE_ERROR;
753
754 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
755
756 /* no session was ever accounted for this server */
757 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100758 if (s->srv_error)
759 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100760 return;
761 }
762
763 /* We are facing a retryable error, but we don't want to run a
764 * turn-around now, as the problem is likely a source port
765 * allocation problem, so we want to retry now.
766 */
767 si->state = SI_ST_CER;
768 si->flags &= ~SI_FL_ERR;
769 sess_update_st_cer(s, si);
770 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
771 return;
772 }
773 else if (si->state == SI_ST_QUE) {
774 /* connection request was queued, check for any update */
775 if (!s->pend_pos) {
776 /* The connection is not in the queue anymore. Either
777 * we have a server connection slot available and we
778 * go directly to the assigned state, or we need to
779 * load-balance first and go to the INI state.
780 */
781 si->exp = TICK_ETERNITY;
782 if (unlikely(!(s->flags & SN_ASSIGNED)))
783 si->state = SI_ST_REQ;
784 else {
785 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
786 si->state = SI_ST_ASS;
787 }
788 return;
789 }
790
791 /* Connection request still in queue... */
792 if (si->flags & SI_FL_EXP) {
793 /* ... and timeout expired */
794 si->exp = TICK_ETERNITY;
795 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
796 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +0200797 s->srv->counters.failed_conns++;
798 s->be->counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100799 si->shutr(si);
800 si->shutw(si);
801 si->ob->flags |= BF_WRITE_TIMEOUT;
802 if (!si->err_type)
803 si->err_type = SI_ET_QUEUE_TO;
804 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100805 if (s->srv_error)
806 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100807 return;
808 }
809
810 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau418fd472009-09-06 21:37:23 +0200811 if ((si->ob->flags & (BF_READ_ERROR)) ||
812 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200813 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100814 /* give up */
815 si->exp = TICK_ETERNITY;
816 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
817 si->shutr(si);
818 si->shutw(si);
819 si->err_type |= SI_ET_QUEUE_ABRT;
820 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100821 if (s->srv_error)
822 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100823 return;
824 }
825
826 /* Nothing changed */
827 return;
828 }
829 else if (si->state == SI_ST_TAR) {
830 /* Connection request might be aborted */
Willy Tarreau418fd472009-09-06 21:37:23 +0200831 if ((si->ob->flags & (BF_READ_ERROR)) ||
832 ((si->ob->flags & BF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreauba0b63d2009-09-20 08:09:44 +0200833 (si->ob->flags & BF_OUT_EMPTY || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100834 /* give up */
835 si->exp = TICK_ETERNITY;
836 si->shutr(si);
837 si->shutw(si);
838 si->err_type |= SI_ET_CONN_ABRT;
839 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100840 if (s->srv_error)
841 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100842 return;
843 }
844
845 if (!(si->flags & SI_FL_EXP))
846 return; /* still in turn-around */
847
848 si->exp = TICK_ETERNITY;
849
850 /* we keep trying on the same server as long as the session is
851 * marked "assigned".
852 * FIXME: Should we force a redispatch attempt when the server is down ?
853 */
854 if (s->flags & SN_ASSIGNED)
855 si->state = SI_ST_ASS;
856 else
857 si->state = SI_ST_REQ;
858 return;
859 }
860}
861
862/* This function initiates a server connection request on a stream interface
863 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
864 * indicating that a server has been assigned. It may also return SI_ST_QUE,
865 * or SI_ST_CLO upon error.
866 */
867static void sess_prepare_conn_req(struct session *s, struct stream_interface *si) {
868 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",
869 now_ms, __FUNCTION__,
870 s,
871 s->req, s->rep,
872 s->req->rex, s->rep->wex,
873 s->req->flags, s->rep->flags,
874 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state);
875
876 if (si->state != SI_ST_REQ)
877 return;
878
879 /* Try to assign a server */
880 if (srv_redispatch_connect(s) != 0) {
881 /* We did not get a server. Either we queued the
882 * connection request, or we encountered an error.
883 */
884 if (si->state == SI_ST_QUE)
885 return;
886
887 /* we did not get any server, let's check the cause */
888 si->shutr(si);
889 si->shutw(si);
890 si->ob->flags |= BF_WRITE_ERROR;
891 if (!si->err_type)
892 si->err_type = SI_ET_CONN_OTHER;
893 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100894 if (s->srv_error)
895 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100896 return;
897 }
898
899 /* The server is assigned */
900 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
901 si->state = SI_ST_ASS;
902}
903
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200904/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +0100905 * if appropriate. The default_backend rule is also considered, then the
906 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200907 * It returns 1 if the processing can continue on next analysers, or zero if it
908 * either needs more data or wants to immediately abort the request.
909 */
910int process_switching_rules(struct session *s, struct buffer *req, int an_bit)
911{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200912 struct persist_rule *prst_rule;
Willy Tarreau4de91492010-01-22 19:10:05 +0100913
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200914 req->analysers &= ~an_bit;
915 req->analyse_exp = TICK_ETERNITY;
916
917 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
918 now_ms, __FUNCTION__,
919 s,
920 req,
921 req->rex, req->wex,
922 req->flags,
923 req->l,
924 req->analysers);
925
926 /* now check whether we have some switching rules for this request */
927 if (!(s->flags & SN_BE_ASSIGNED)) {
928 struct switching_rule *rule;
929
930 list_for_each_entry(rule, &s->fe->switching_rules, list) {
931 int ret;
932
933 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ);
934 ret = acl_pass(ret);
935 if (rule->cond->pol == ACL_COND_UNLESS)
936 ret = !ret;
937
938 if (ret) {
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200939 if (!session_set_backend(s, rule->be.backend))
940 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200941 break;
942 }
943 }
944
945 /* To ensure correct connection accounting on the backend, we
946 * have to assign one if it was not set (eg: a listen). This
947 * measure also takes care of correctly setting the default
948 * backend if any.
949 */
950 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200951 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
952 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200953 }
954
Willy Tarreaufb356202010-08-03 14:02:05 +0200955 /* we don't want to run the TCP or HTTP filters again if the backend has not changed */
956 if (s->fe == s->be) {
957 s->req->analysers &= ~AN_REQ_INSPECT_BE;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200958 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +0200959 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200960
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200961 /* 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 +0100962 * persistence rule, and report that in the session.
963 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200964 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +0100965 int ret = 1;
966
967 if (prst_rule->cond) {
968 ret = acl_exec_cond(prst_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
969 ret = acl_pass(ret);
970 if (prst_rule->cond->pol == ACL_COND_UNLESS)
971 ret = !ret;
972 }
973
974 if (ret) {
975 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +0200976 if (prst_rule->type == PERSIST_TYPE_FORCE) {
977 s->flags |= SN_FORCE_PRST;
978 } else {
979 s->flags |= SN_IGNORE_PRST;
980 }
Willy Tarreau4de91492010-01-22 19:10:05 +0100981 break;
982 }
983 }
984
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200985 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +0200986
987 sw_failed:
988 /* immediately abort this request in case of allocation failure */
989 buffer_abort(s->req);
990 buffer_abort(s->rep);
991
992 if (!(s->flags & SN_ERR_MASK))
993 s->flags |= SN_ERR_RESOURCE;
994 if (!(s->flags & SN_FINST_MASK))
995 s->flags |= SN_FINST_R;
996
997 s->txn.status = 500;
998 s->req->analysers = 0;
999 s->req->analyse_exp = TICK_ETERNITY;
1000 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001001}
1002
Emeric Brun1d33b292010-01-04 15:47:17 +01001003/* This stream analyser works on a request. It applies all sticking rules on
1004 * it then returns 1. The data must already be present in the buffer otherwise
1005 * they won't match. It always returns 1.
1006 */
1007int process_sticking_rules(struct session *s, struct buffer *req, int an_bit)
1008{
1009 struct proxy *px = s->be;
1010 struct sticking_rule *rule;
1011
1012 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1013 now_ms, __FUNCTION__,
1014 s,
1015 req,
1016 req->rex, req->wex,
1017 req->flags,
1018 req->l,
1019 req->analysers);
1020
1021 list_for_each_entry(rule, &px->sticking_rules, list) {
1022 int ret = 1 ;
1023 int i;
1024
1025 for (i = 0; i < s->store_count; i++) {
1026 if (rule->table.t == s->store[i].table)
1027 break;
1028 }
1029
1030 if (i != s->store_count)
1031 continue;
1032
1033 if (rule->cond) {
1034 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_REQ);
1035 ret = acl_pass(ret);
1036 if (rule->cond->pol == ACL_COND_UNLESS)
1037 ret = !ret;
1038 }
1039
1040 if (ret) {
1041 struct stktable_key *key;
1042
Emeric Brun485479d2010-09-23 18:02:19 +02001043 key = stktable_fetch_key(rule->table.t, px, s, &s->txn, PATTERN_FETCH_REQ, rule->expr);
Emeric Brun1d33b292010-01-04 15:47:17 +01001044 if (!key)
1045 continue;
1046
1047 if (rule->flags & STK_IS_MATCH) {
1048 struct stksess *ts;
1049
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001050 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001051 if (!(s->flags & SN_ASSIGNED)) {
1052 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001053 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001054
1055 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001056 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1057 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001058 if (node) {
1059 struct server *srv;
1060
1061 srv = container_of(node, struct server, conf.id);
Willy Tarreau4de91492010-01-22 19:10:05 +01001062 if ((srv->state & SRV_RUNNING) ||
1063 (px->options & PR_O_PERSIST) ||
1064 (s->flags & SN_FORCE_PRST)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001065 s->flags |= SN_DIRECT | SN_ASSIGNED;
1066 s->srv = srv;
1067 }
1068 }
1069 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001070 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001071 }
1072 }
1073 if (rule->flags & STK_IS_STORE) {
1074 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1075 struct stksess *ts;
1076
1077 ts = stksess_new(rule->table.t, key);
1078 if (ts) {
1079 s->store[s->store_count].table = rule->table.t;
1080 s->store[s->store_count++].ts = ts;
1081 }
1082 }
1083 }
1084 }
1085 }
1086
1087 req->analysers &= ~an_bit;
1088 req->analyse_exp = TICK_ETERNITY;
1089 return 1;
1090}
1091
1092/* This stream analyser works on a response. It applies all store rules on it
1093 * then returns 1. The data must already be present in the buffer otherwise
1094 * they won't match. It always returns 1.
1095 */
1096int process_store_rules(struct session *s, struct buffer *rep, int an_bit)
1097{
1098 struct proxy *px = s->be;
1099 struct sticking_rule *rule;
1100 int i;
1101
1102 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1103 now_ms, __FUNCTION__,
1104 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001105 rep,
1106 rep->rex, rep->wex,
1107 rep->flags,
1108 rep->l,
1109 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001110
1111 list_for_each_entry(rule, &px->storersp_rules, list) {
1112 int ret = 1 ;
1113 int storereqidx = -1;
1114
1115 for (i = 0; i < s->store_count; i++) {
1116 if (rule->table.t == s->store[i].table) {
1117 if (!(s->store[i].flags))
1118 storereqidx = i;
1119 break;
1120 }
1121 }
1122
1123 if ((i != s->store_count) && (storereqidx == -1))
1124 continue;
1125
1126 if (rule->cond) {
1127 ret = acl_exec_cond(rule->cond, px, s, &s->txn, ACL_DIR_RTR);
1128 ret = acl_pass(ret);
1129 if (rule->cond->pol == ACL_COND_UNLESS)
1130 ret = !ret;
1131 }
1132
1133 if (ret) {
1134 struct stktable_key *key;
1135
Emeric Brun485479d2010-09-23 18:02:19 +02001136 key = stktable_fetch_key(rule->table.t, px, s, &s->txn, PATTERN_FETCH_RTR, rule->expr);
Emeric Brun1d33b292010-01-04 15:47:17 +01001137 if (!key)
1138 continue;
1139
1140 if (storereqidx != -1) {
Willy Tarreau393379c2010-06-06 12:11:37 +02001141 stksess_setkey(s->store[storereqidx].table, s->store[storereqidx].ts, key);
Emeric Brun1d33b292010-01-04 15:47:17 +01001142 s->store[storereqidx].flags = 1;
1143 }
1144 else if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1145 struct stksess *ts;
1146
1147 ts = stksess_new(rule->table.t, key);
1148 if (ts) {
1149 s->store[s->store_count].table = rule->table.t;
1150 s->store[s->store_count].flags = 1;
1151 s->store[s->store_count++].ts = ts;
1152 }
1153 }
1154 }
1155 }
1156
1157 /* process store request and store response */
1158 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001159 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001160 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001161
1162 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1163 if (ts) {
1164 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001165 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001166 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001167 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001168 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001169 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001170
1171 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001172 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
1173 stktable_data_cast(ptr, server_id) = s->srv->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001174 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001175 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001176
1177 rep->analysers &= ~an_bit;
1178 rep->analyse_exp = TICK_ETERNITY;
1179 return 1;
1180}
1181
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001182/* This macro is very specific to the function below. See the comments in
1183 * process_session() below to understand the logic and the tests.
1184 */
1185#define UPDATE_ANALYSERS(real, list, back, flag) { \
1186 list = (((list) & ~(flag)) | ~(back)) & (real); \
1187 back = real; \
1188 if (!(list)) \
1189 break; \
1190 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1191 continue; \
1192}
1193
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001194/* Processes the client, server, request and response jobs of a session task,
1195 * then puts it back to the wait queue in a clean state, or cleans up its
1196 * resources if it must be deleted. Returns in <next> the date the task wants
1197 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1198 * nothing too many times, the request and response buffers flags are monitored
1199 * and each function is called only if at least another function has changed at
1200 * least one flag it is interested in.
1201 */
Willy Tarreau26c25062009-03-08 09:38:41 +01001202struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001203{
1204 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001205 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001206 unsigned int rq_prod_last, rq_cons_last;
1207 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001208 unsigned int req_ana_back;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001209
1210 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
1211 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
1212
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001213 /* this data may be no longer valid, clear it */
1214 memset(&s->txn.auth, 0, sizeof(s->txn.auth));
1215
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001216 /* This flag must explicitly be set every time */
1217 s->req->flags &= ~BF_READ_NOEXP;
1218
1219 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau2f976e12010-11-11 14:28:47 +01001220 rqf_last = s->req->flags & ~BF_MASK_ANALYSER;
1221 rpf_last = s->rep->flags & ~BF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001222
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001223 /* we don't want the stream interface functions to recursively wake us up */
1224 if (s->req->prod->owner == t)
1225 s->req->prod->flags |= SI_FL_DONT_WAKE;
1226 if (s->req->cons->owner == t)
1227 s->req->cons->flags |= SI_FL_DONT_WAKE;
1228
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001229 /* 1a: Check for low level timeouts if needed. We just set a flag on
1230 * stream interfaces when their timeouts have expired.
1231 */
1232 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
1233 stream_int_check_timeouts(&s->si[0]);
1234 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001235
1236 /* check buffer timeouts, and close the corresponding stream interfaces
1237 * for future reads or writes. Note: this will also concern upper layers
1238 * but we do not touch any other flag. We must be careful and correctly
1239 * detect state changes when calling them.
1240 */
1241
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001242 buffer_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001243
Willy Tarreau14641402009-12-29 14:49:56 +01001244 if (unlikely((s->req->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1245 s->req->cons->flags |= SI_FL_NOLINGER;
1246 s->req->cons->shutw(s->req->cons);
1247 }
1248
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001249 if (unlikely((s->req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1250 s->req->prod->shutr(s->req->prod);
1251
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001252 buffer_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001253
Willy Tarreau14641402009-12-29 14:49:56 +01001254 if (unlikely((s->rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT)) {
1255 s->rep->cons->flags |= SI_FL_NOLINGER;
1256 s->rep->cons->shutw(s->rep->cons);
1257 }
1258
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001259 if (unlikely((s->rep->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT))
1260 s->rep->prod->shutr(s->rep->prod);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001261 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001262
1263 /* 1b: check for low-level errors reported at the stream interface.
1264 * First we check if it's a retryable error (in which case we don't
1265 * want to tell the buffer). Otherwise we report the error one level
1266 * upper by setting flags into the buffers. Note that the side towards
1267 * the client cannot have connect (hence retryable) errors. Also, the
1268 * connection setup code must be able to deal with any type of abort.
1269 */
1270 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
1271 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
1272 s->si[0].shutr(&s->si[0]);
1273 s->si[0].shutw(&s->si[0]);
1274 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001275 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreauae526782010-03-04 20:34:23 +01001276 s->be->counters.cli_aborts++;
1277 if (s->srv)
1278 s->srv->counters.cli_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001279 if (!(s->flags & SN_ERR_MASK))
1280 s->flags |= SN_ERR_CLICL;
1281 if (!(s->flags & SN_FINST_MASK))
1282 s->flags |= SN_FINST_D;
1283 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001284 }
1285 }
1286
1287 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
1288 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
1289 s->si[1].shutr(&s->si[1]);
1290 s->si[1].shutw(&s->si[1]);
1291 stream_int_report_error(&s->si[1]);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001292 s->be->counters.failed_resp++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001293 if (s->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001294 s->srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001295 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreauae526782010-03-04 20:34:23 +01001296 s->be->counters.srv_aborts++;
1297 if (s->srv)
1298 s->srv->counters.srv_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001299 if (!(s->flags & SN_ERR_MASK))
1300 s->flags |= SN_ERR_SRVCL;
1301 if (!(s->flags & SN_FINST_MASK))
1302 s->flags |= SN_FINST_D;
1303 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001304 }
1305 /* note: maybe we should process connection errors here ? */
1306 }
1307
1308 if (s->si[1].state == SI_ST_CON) {
1309 /* we were trying to establish a connection on the server side,
1310 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1311 */
1312 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
1313 sess_update_st_cer(s, &s->si[1]);
1314 else if (s->si[1].state == SI_ST_EST)
1315 sess_establish(s, &s->si[1]);
1316
1317 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1318 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1319 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1320 */
1321 }
1322
Willy Tarreau815a9b22010-07-27 17:15:12 +02001323 rq_prod_last = s->si[0].state;
1324 rq_cons_last = s->si[1].state;
1325 rp_cons_last = s->si[0].state;
1326 rp_prod_last = s->si[1].state;
1327
1328 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001329 /* Check for connection closure */
1330
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001331 DPRINTF(stderr,
1332 "[%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",
1333 now_ms, __FUNCTION__, __LINE__,
1334 t,
1335 s, s->flags,
1336 s->req, s->rep,
1337 s->req->rex, s->rep->wex,
1338 s->req->flags, s->rep->flags,
1339 s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state,
1340 s->rep->cons->err_type, s->req->cons->err_type,
Willy Tarreauee28de02010-06-01 09:51:00 +02001341 s->req->cons->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001342
1343 /* nothing special to be done on client side */
1344 if (unlikely(s->req->prod->state == SI_ST_DIS))
1345 s->req->prod->state = SI_ST_CLO;
1346
1347 /* When a server-side connection is released, we have to count it and
1348 * check for pending connections on this server.
1349 */
1350 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
1351 s->req->cons->state = SI_ST_CLO;
1352 if (s->srv) {
1353 if (s->flags & SN_CURR_SESS) {
1354 s->flags &= ~SN_CURR_SESS;
1355 s->srv->cur_sess--;
1356 }
1357 sess_change_server(s, NULL);
1358 if (may_dequeue_tasks(s->srv, s->be))
1359 process_srv_queue(s->srv);
1360 }
1361 }
1362
1363 /*
1364 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1365 * at this point.
1366 */
1367
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001368 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001369 /* Analyse request */
Willy Tarreau2f976e12010-11-11 14:28:47 +01001370 if (((s->req->flags & ~rqf_last) & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001371 ((s->req->flags ^ rqf_last) & BF_MASK_STATIC) ||
1372 s->si[0].state != rq_prod_last ||
1373 s->si[1].state != rq_cons_last) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001374 unsigned int flags = s->req->flags;
1375
1376 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001377 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001378 unsigned int ana_list;
1379 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001380
Willy Tarreau90deb182010-01-07 00:20:41 +01001381 /* it's up to the analysers to stop new connections,
1382 * disable reading or closing. Note: if an analyser
1383 * disables any of these bits, it is responsible for
1384 * enabling them again when it disables itself, so
1385 * that other analysers are called in similar conditions.
1386 */
1387 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001388 buffer_auto_connect(s->req);
1389 buffer_auto_close(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001390
1391 /* We will call all analysers for which a bit is set in
1392 * s->req->analysers, following the bit order from LSB
1393 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001394 * the list when not needed. Any analyser may return 0
1395 * to break out of the loop, either because of missing
1396 * data to take a decision, or because it decides to
1397 * kill the session. We loop at least once through each
1398 * analyser, and we may loop again if other analysers
1399 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001400 *
1401 * We build a list of analysers to run. We evaluate all
1402 * of these analysers in the order of the lower bit to
1403 * the higher bit. This ordering is very important.
1404 * An analyser will often add/remove other analysers,
1405 * including itself. Any changes to itself have no effect
1406 * on the loop. If it removes any other analysers, we
1407 * want those analysers not to be called anymore during
1408 * this loop. If it adds an analyser that is located
1409 * after itself, we want it to be scheduled for being
1410 * processed during the loop. If it adds an analyser
1411 * which is located before it, we want it to switch to
1412 * it immediately, even if it has already been called
1413 * once but removed since.
1414 *
1415 * In order to achieve this, we compare the analyser
1416 * list after the call with a copy of it before the
1417 * call. The work list is fed with analyser bits that
1418 * appeared during the call. Then we compare previous
1419 * work list with the new one, and check the bits that
1420 * appeared. If the lowest of these bits is lower than
1421 * the current bit, it means we have enabled a previous
1422 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001423 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001424
1425 ana_list = ana_back = s->req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001426 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001427 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001428
Willy Tarreau3041b9f2010-10-15 23:25:20 +02001429 if (ana_list & AN_REQ_DECODE_PROXY) {
1430 if (!frontend_decode_proxy_request(s, s->req, AN_REQ_DECODE_PROXY))
1431 break;
1432 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_DECODE_PROXY);
1433 }
1434
Willy Tarreaufb356202010-08-03 14:02:05 +02001435 if (ana_list & AN_REQ_INSPECT_FE) {
1436 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001437 break;
Willy Tarreaufb356202010-08-03 14:02:05 +02001438 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001439 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001440
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001441 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001442 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001443 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001444 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001445 }
1446
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001447 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001448 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
1449 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001450 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001451 }
1452
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001453 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001454 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
1455 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001456 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001457 }
1458
Willy Tarreaufb356202010-08-03 14:02:05 +02001459 if (ana_list & AN_REQ_INSPECT_BE) {
1460 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_BE))
1461 break;
1462 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
1463 }
1464
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001465 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001466 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
1467 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001468 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001469 }
1470
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001471 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001472 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001473 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001474 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001475 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001476
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001477 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreauc465fd72009-08-31 00:17:18 +02001478 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
1479 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001480 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001481 }
1482
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001483 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001484 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001485 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001486 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001487 }
Emeric Brun647caf12009-06-30 17:57:00 +02001488
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001489 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Emeric Brun647caf12009-06-30 17:57:00 +02001490 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
1491 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001492 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001493 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001494
Emeric Brun1d33b292010-01-04 15:47:17 +01001495 if (ana_list & AN_REQ_STICKING_RULES) {
1496 if (!process_sticking_rules(s, s->req, AN_REQ_STICKING_RULES))
1497 break;
1498 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
1499 }
1500
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001501 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001502 if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY))
1503 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001504 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001505 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001506 break;
1507 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001508 }
Willy Tarreau84455332009-03-15 22:34:05 +01001509
Willy Tarreau815a9b22010-07-27 17:15:12 +02001510 rq_prod_last = s->si[0].state;
1511 rq_cons_last = s->si[1].state;
Willy Tarreau0499e352010-12-17 07:13:42 +01001512 s->req->flags &= ~BF_WAKE_ONCE;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001513 rqf_last = s->req->flags;
1514
1515 if ((s->req->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001516 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001517 }
1518
Willy Tarreau576507f2010-01-07 00:09:04 +01001519 /* we'll monitor the request analysers while parsing the response,
1520 * because some response analysers may indirectly enable new request
1521 * analysers (eg: HTTP keep-alive).
1522 */
1523 req_ana_back = s->req->analysers;
1524
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001525 resync_response:
1526 /* Analyse response */
1527
1528 if (unlikely(s->rep->flags & BF_HIJACK)) {
1529 /* In inject mode, we wake up everytime something has
1530 * happened on the write side of the buffer.
1531 */
1532 unsigned int flags = s->rep->flags;
1533
1534 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
1535 !(s->rep->flags & BF_FULL)) {
1536 s->rep->hijacker(s, s->rep);
1537 }
1538
1539 if ((s->rep->flags ^ flags) & BF_MASK_STATIC) {
1540 rpf_last = s->rep->flags;
1541 goto resync_response;
1542 }
1543 }
Willy Tarreau2f976e12010-11-11 14:28:47 +01001544 else if (((s->rep->flags & ~rpf_last) & BF_MASK_ANALYSER) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001545 (s->rep->flags ^ rpf_last) & BF_MASK_STATIC ||
1546 s->si[0].state != rp_cons_last ||
1547 s->si[1].state != rp_prod_last) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001548 unsigned int flags = s->rep->flags;
1549
Willy Tarreau0499e352010-12-17 07:13:42 +01001550 if ((s->rep->flags & BF_MASK_ANALYSER) &&
1551 (s->rep->analysers & AN_REQ_WAIT_HTTP)) {
1552 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1553 * for some free space in the response buffer, so we might need to call
1554 * it when something changes in the response buffer, but still we pass
1555 * it the request buffer. Note that the SI state might very well still
1556 * be zero due to us returning a flow of redirects!
1557 */
1558 s->rep->analysers &= ~AN_REQ_WAIT_HTTP;
1559 s->req->flags |= BF_WAKE_ONCE;
1560 }
1561
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001562 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001563 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001564 unsigned int ana_list;
1565 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001566
Willy Tarreau90deb182010-01-07 00:20:41 +01001567 /* it's up to the analysers to stop disable reading or
1568 * closing. Note: if an analyser disables any of these
1569 * bits, it is responsible for enabling them again when
1570 * it disables itself, so that other analysers are called
1571 * in similar conditions.
1572 */
1573 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001574 buffer_auto_close(s->rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001575
1576 /* We will call all analysers for which a bit is set in
1577 * s->rep->analysers, following the bit order from LSB
1578 * to MSB. The analysers must remove themselves from
1579 * the list when not needed. Any analyser may return 0
1580 * to break out of the loop, either because of missing
1581 * data to take a decision, or because it decides to
1582 * kill the session. We loop at least once through each
1583 * analyser, and we may loop again if other analysers
1584 * are added in the middle.
1585 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001586
1587 ana_list = ana_back = s->rep->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001588 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001589 /* Warning! ensure that analysers are always placed in ascending order! */
1590
Emeric Brun97679e72010-09-23 17:56:44 +02001591 if (ana_list & AN_RES_INSPECT) {
1592 if (!tcp_inspect_response(s, s->rep, AN_RES_INSPECT))
1593 break;
1594 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_INSPECT);
1595 }
1596
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001597 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001598 if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP))
1599 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001600 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001601 }
1602
Emeric Brun1d33b292010-01-04 15:47:17 +01001603 if (ana_list & AN_RES_STORE_RULES) {
1604 if (!process_store_rules(s, s->rep, AN_RES_STORE_RULES))
1605 break;
1606 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
1607 }
1608
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001609 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001610 if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be))
1611 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001612 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001613 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001614
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001615 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001616 if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY))
1617 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001618 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001619 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001620 break;
1621 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001622 }
1623
Willy Tarreau815a9b22010-07-27 17:15:12 +02001624 rp_cons_last = s->si[0].state;
1625 rp_prod_last = s->si[1].state;
1626 rpf_last = s->rep->flags;
1627
1628 if ((s->rep->flags ^ flags) & BF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001629 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001630 }
1631
Willy Tarreau576507f2010-01-07 00:09:04 +01001632 /* maybe someone has added some request analysers, so we must check and loop */
1633 if (s->req->analysers & ~req_ana_back)
1634 goto resync_request;
1635
Willy Tarreau0499e352010-12-17 07:13:42 +01001636 if ((s->req->flags & ~rqf_last) & BF_MASK_ANALYSER)
1637 goto resync_request;
1638
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001639 /* FIXME: here we should call protocol handlers which rely on
1640 * both buffers.
1641 */
1642
1643
1644 /*
Willy Tarreauae526782010-03-04 20:34:23 +01001645 * Now we propagate unhandled errors to the session. Normally
1646 * we're just in a data phase here since it means we have not
1647 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001648 */
Willy Tarreau2f976e12010-11-11 14:28:47 +01001649 if (unlikely(!(s->flags & SN_ERR_MASK))) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001650 if (s->req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1651 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +01001652 s->req->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001653 if (s->req->flags & BF_READ_ERROR) {
1654 s->be->counters.cli_aborts++;
1655 if (s->srv)
1656 s->srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001657 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001658 }
1659 else if (s->req->flags & BF_READ_TIMEOUT) {
1660 s->be->counters.cli_aborts++;
1661 if (s->srv)
1662 s->srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001663 s->flags |= SN_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001664 }
1665 else if (s->req->flags & BF_WRITE_ERROR) {
1666 s->be->counters.srv_aborts++;
1667 if (s->srv)
1668 s->srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001669 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001670 }
1671 else {
1672 s->be->counters.srv_aborts++;
1673 if (s->srv)
1674 s->srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01001675 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001676 }
Willy Tarreau84455332009-03-15 22:34:05 +01001677 sess_set_term_flags(s);
1678 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001679 else if (s->rep->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
1680 /* Report it if the server got an error or a read timeout expired */
1681 s->rep->analysers = 0;
Willy Tarreauae526782010-03-04 20:34:23 +01001682 if (s->rep->flags & BF_READ_ERROR) {
1683 s->be->counters.srv_aborts++;
1684 if (s->srv)
1685 s->srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001686 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001687 }
1688 else if (s->rep->flags & BF_READ_TIMEOUT) {
1689 s->be->counters.srv_aborts++;
1690 if (s->srv)
1691 s->srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001692 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001693 }
1694 else if (s->rep->flags & BF_WRITE_ERROR) {
1695 s->be->counters.cli_aborts++;
1696 if (s->srv)
1697 s->srv->counters.cli_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001698 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001699 }
1700 else {
1701 s->be->counters.cli_aborts++;
1702 if (s->srv)
1703 s->srv->counters.cli_aborts++;
1704 s->flags |= SN_ERR_CLITO;
1705 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001706 sess_set_term_flags(s);
1707 }
Willy Tarreau84455332009-03-15 22:34:05 +01001708 }
1709
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001710 /*
1711 * Here we take care of forwarding unhandled data. This also includes
1712 * connection establishments and shutdown requests.
1713 */
1714
1715
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001716 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001717 * everything. We configure the buffer to forward indefinitely.
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001718 * Note that we're checking BF_SHUTR_NOW as an indication of a possible
1719 * recent call to buffer_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001720 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001721 if (!s->req->analysers &&
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001722 !(s->req->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001723 (s->req->prod->state >= SI_ST_EST) &&
1724 (s->req->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001725 /* This buffer is freewheeling, there's no analyser nor hijacker
1726 * attached to it. If any data are left in, we'll permit them to
1727 * move.
1728 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001729 buffer_auto_read(s->req);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001730 buffer_auto_connect(s->req);
1731 buffer_auto_close(s->req);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001732 buffer_flush(s->req);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001733
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001734 /* We'll let data flow between the producer (if still connected)
1735 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001736 */
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001737 if (!(s->req->flags & (BF_SHUTR|BF_SHUTW_NOW)))
Willy Tarreau31971e52009-09-20 12:07:52 +02001738 buffer_forward(s->req, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001739 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001740
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001741 /* check if it is wise to enable kernel splicing to forward request data */
1742 if (!(s->req->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1743 s->req->to_forward &&
1744 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001745 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001746 (pipes_used < global.maxpipes) &&
1747 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
1748 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1749 (s->req->flags & BF_STREAMER_FAST)))) {
1750 s->req->flags |= BF_KERN_SPLICING;
1751 }
1752
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001753 /* reflect what the L7 analysers have seen last */
1754 rqf_last = s->req->flags;
1755
1756 /*
1757 * Now forward all shutdown requests between both sides of the buffer
1758 */
1759
Willy Tarreau520d95e2009-09-19 21:04:57 +02001760 /* first, let's check if the request buffer needs to shutdown(write), which may
1761 * happen either because the input is closed or because we want to force a close
Willy Tarreaue4599762010-03-21 23:25:09 +01001762 * once the server has begun to respond.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001763 */
Willy Tarreau82eeaf22009-12-29 12:09:05 +01001764 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
Willy Tarreaue4599762010-03-21 23:25:09 +01001765 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001766 buffer_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001767
1768 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001769 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 +01001770 s->req->cons->shutw(s->req->cons);
1771
1772 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001773 if (unlikely((s->req->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW &&
1774 !s->req->analysers))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001775 buffer_shutr_now(s->req);
1776
1777 /* shutdown(read) pending */
1778 if (unlikely((s->req->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1779 s->req->prod->shutr(s->req->prod);
1780
Willy Tarreau520d95e2009-09-19 21:04:57 +02001781 /* it's possible that an upper layer has requested a connection setup or abort.
1782 * There are 2 situations where we decide to establish a new connection :
1783 * - there are data scheduled for emission in the buffer
1784 * - the BF_AUTO_CONNECT flag is set (active connection)
1785 */
1786 if (s->req->cons->state == SI_ST_INI) {
Willy Tarreaue4599762010-03-21 23:25:09 +01001787 if (!(s->req->flags & BF_SHUTW)) {
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001788 if ((s->req->flags & (BF_AUTO_CONNECT|BF_OUT_EMPTY)) != BF_OUT_EMPTY) {
Willy Tarreau85e7d002010-05-31 11:57:51 +02001789 /* If we have an iohandler without a connect method, we immediately
1790 * switch to the connected state, otherwise we perform a connection
1791 * request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001792 */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001793 s->req->cons->state = SI_ST_REQ; /* new connection requested */
Willy Tarreau070ceb62010-06-01 10:36:43 +02001794 s->req->cons->conn_retries = s->be->conn_retries;
Willy Tarreau85e7d002010-05-31 11:57:51 +02001795 if (unlikely(s->req->cons->iohandler && !s->req->cons->connect)) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02001796 s->req->cons->state = SI_ST_EST; /* connection established */
Willy Tarreau85e7d002010-05-31 11:57:51 +02001797 s->rep->flags |= BF_READ_ATTACHED; /* producer is now attached */
1798 s->req->wex = TICK_ETERNITY;
1799 }
Willy Tarreau520d95e2009-09-19 21:04:57 +02001800 }
Willy Tarreau73201222009-08-16 18:27:24 +02001801 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001802 else {
Willy Tarreau92795622009-03-06 12:51:23 +01001803 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02001804 buffer_shutw_now(s->req); /* fix buffer flags upon abort */
1805 buffer_shutr_now(s->rep);
1806 }
Willy Tarreau92795622009-03-06 12:51:23 +01001807 }
1808
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001809
1810 /* we may have a pending connection request, or a connection waiting
1811 * for completion.
1812 */
1813 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
1814 do {
1815 /* nb: step 1 might switch from QUE to ASS, but we first want
1816 * to give a chance to step 2 to perform a redirect if needed.
1817 */
1818 if (s->si[1].state != SI_ST_REQ)
1819 sess_update_stream_int(s, &s->si[1]);
1820 if (s->si[1].state == SI_ST_REQ)
1821 sess_prepare_conn_req(s, &s->si[1]);
1822
1823 if (s->si[1].state == SI_ST_ASS && s->srv &&
1824 s->srv->rdr_len && (s->flags & SN_REDIRECTABLE))
1825 perform_http_redirect(s, &s->si[1]);
1826 } while (s->si[1].state == SI_ST_ASS);
1827 }
1828
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001829 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001830 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001831 goto resync_stream_interface;
1832
Willy Tarreau815a9b22010-07-27 17:15:12 +02001833 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001834 if ((s->req->flags ^ rqf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001835 goto resync_request;
1836
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001837 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01001838
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001839 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001840 * everything. We configure the buffer to forward indefinitely.
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001841 * Note that we're checking BF_SHUTR_NOW as an indication of a possible
1842 * recent call to buffer_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001843 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001844 if (!s->rep->analysers &&
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001845 !(s->rep->flags & (BF_HIJACK|BF_SHUTW|BF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02001846 (s->rep->prod->state >= SI_ST_EST) &&
1847 (s->rep->to_forward != BUF_INFINITE_FORWARD)) {
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001848 /* This buffer is freewheeling, there's no analyser nor hijacker
1849 * attached to it. If any data are left in, we'll permit them to
1850 * move.
1851 */
Willy Tarreau90deb182010-01-07 00:20:41 +01001852 buffer_auto_read(s->rep);
Willy Tarreau520d95e2009-09-19 21:04:57 +02001853 buffer_auto_close(s->rep);
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001854 buffer_flush(s->rep);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001855
1856 /* We'll let data flow between the producer (if still connected)
1857 * to the consumer.
1858 */
1859 if (!(s->rep->flags & (BF_SHUTR|BF_SHUTW_NOW)))
Willy Tarreau31971e52009-09-20 12:07:52 +02001860 buffer_forward(s->rep, BUF_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001861 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001862
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001863 /* check if it is wise to enable kernel splicing to forward response data */
1864 if (!(s->rep->flags & (BF_KERN_SPLICING|BF_SHUTR)) &&
1865 s->rep->to_forward &&
1866 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaudc340a92009-06-28 23:10:19 +02001867 (s->si[0].flags & s->si[1].flags & SI_FL_CAP_SPLICE) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001868 (pipes_used < global.maxpipes) &&
1869 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
1870 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
1871 (s->rep->flags & BF_STREAMER_FAST)))) {
1872 s->rep->flags |= BF_KERN_SPLICING;
1873 }
1874
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001875 /* reflect what the L7 analysers have seen last */
1876 rpf_last = s->rep->flags;
1877
1878 /*
1879 * Now forward all shutdown requests between both sides of the buffer
1880 */
1881
1882 /*
1883 * FIXME: this is probably where we should produce error responses.
1884 */
1885
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001886 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau520d95e2009-09-19 21:04:57 +02001887 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_HIJACK|BF_AUTO_CLOSE|BF_SHUTR)) ==
1888 (BF_AUTO_CLOSE|BF_SHUTR)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001889 buffer_shutw_now(s->rep);
1890
1891 /* shutdown(write) pending */
Willy Tarreauba0b63d2009-09-20 08:09:44 +02001892 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 +01001893 s->rep->cons->shutw(s->rep->cons);
1894
1895 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau3dbc6942008-12-07 13:05:04 +01001896 if (unlikely((s->rep->flags & (BF_SHUTW|BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTW) &&
1897 !s->rep->analysers)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001898 buffer_shutr_now(s->rep);
1899
1900 /* shutdown(read) pending */
1901 if (unlikely((s->rep->flags & (BF_SHUTR|BF_SHUTR_NOW)) == BF_SHUTR_NOW))
1902 s->rep->prod->shutr(s->rep->prod);
1903
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001904 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001905 goto resync_stream_interface;
1906
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001907 if (s->req->flags != rqf_last)
1908 goto resync_request;
1909
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001910 if ((s->rep->flags ^ rpf_last) & BF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001911 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001912
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001913 /* we're interested in getting wakeups again */
1914 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
1915 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
1916
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001917 /* This is needed only when debugging is enabled, to indicate
1918 * client-side or server-side close. Please note that in the unlikely
1919 * event where both sides would close at once, the sequence is reported
1920 * on the server side first.
1921 */
1922 if (unlikely((global.mode & MODE_DEBUG) &&
1923 (!(global.mode & MODE_QUIET) ||
1924 (global.mode & MODE_VERBOSE)))) {
1925 int len;
1926
1927 if (s->si[1].state == SI_ST_CLO &&
1928 s->si[1].prev_state == SI_ST_EST) {
1929 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
1930 s->uniq_id, s->be->id,
1931 (unsigned short)s->si[0].fd,
1932 (unsigned short)s->si[1].fd);
1933 write(1, trash, len);
1934 }
1935
1936 if (s->si[0].state == SI_ST_CLO &&
1937 s->si[0].prev_state == SI_ST_EST) {
1938 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
1939 s->uniq_id, s->be->id,
1940 (unsigned short)s->si[0].fd,
1941 (unsigned short)s->si[1].fd);
1942 write(1, trash, len);
1943 }
1944 }
1945
1946 if (likely((s->rep->cons->state != SI_ST_CLO) ||
1947 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
1948
1949 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
1950 session_process_counters(s);
1951
Willy Tarreau1accfc02009-09-05 20:57:35 +02001952 if (s->rep->cons->state == SI_ST_EST && !s->rep->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001953 s->rep->cons->update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001954
Willy Tarreau1accfc02009-09-05 20:57:35 +02001955 if (s->req->cons->state == SI_ST_EST && !s->req->cons->iohandler)
Willy Tarreaudc85b392009-08-18 07:38:19 +02001956 s->req->cons->update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001957
Willy Tarreaua6eebb32010-06-04 11:40:20 +02001958 s->req->flags &= ~(BF_READ_NULL|BF_READ_PARTIAL|BF_WRITE_NULL|BF_WRITE_PARTIAL|BF_READ_ATTACHED);
1959 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 +01001960 s->si[0].prev_state = s->si[0].state;
1961 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01001962 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
1963 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001964
1965 /* Trick: if a request is being waiting for the server to respond,
1966 * and if we know the server can timeout, we don't want the timeout
1967 * to expire on the client side first, but we're still interested
1968 * in passing data from the client to the server (eg: POST). Thus,
1969 * we can cancel the client's request timeout if the server's
1970 * request timeout is set and the server has not yet sent a response.
1971 */
1972
Willy Tarreau520d95e2009-09-19 21:04:57 +02001973 if ((s->rep->flags & (BF_AUTO_CLOSE|BF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01001974 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
1975 s->req->flags |= BF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001976 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01001977 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001978
Willy Tarreau7a20aa62010-07-13 16:30:45 +02001979 /* Call the stream interfaces' I/O handlers when embedded.
Willy Tarreau1accfc02009-09-05 20:57:35 +02001980 * Note that this one may wake the task up again.
1981 */
Willy Tarreau7a20aa62010-07-13 16:30:45 +02001982 if (s->req->cons->iohandler || s->rep->cons->iohandler) {
1983 if (s->req->cons->iohandler)
1984 s->req->cons->iohandler(s->req->cons);
1985 if (s->rep->cons->iohandler)
1986 s->rep->cons->iohandler(s->rep->cons);
Willy Tarreau1accfc02009-09-05 20:57:35 +02001987 if (task_in_rq(t)) {
1988 /* If we woke up, we don't want to requeue the
1989 * task to the wait queue, but rather requeue
1990 * it into the runqueue ASAP.
1991 */
1992 t->expire = TICK_ETERNITY;
1993 return t;
1994 }
1995 }
1996
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001997 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
1998 tick_first(s->rep->rex, s->rep->wex));
1999 if (s->req->analysers)
2000 t->expire = tick_first(t->expire, s->req->analyse_exp);
2001
2002 if (s->si[0].exp)
2003 t->expire = tick_first(t->expire, s->si[0].exp);
2004
2005 if (s->si[1].exp)
2006 t->expire = tick_first(t->expire, s->si[1].exp);
2007
2008#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002009 fprintf(stderr,
2010 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2011 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
2012 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
2013 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 +01002014#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002015
2016#ifdef DEBUG_DEV
2017 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002018 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002019 ABORT_NOW();
2020#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01002021 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002022 }
2023
2024 s->fe->feconn--;
2025 if (s->flags & SN_BE_ASSIGNED)
2026 s->be->beconn--;
2027 actconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002028 jobs--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02002029 s->listener->nbconn--;
2030 if (s->listener->state == LI_FULL &&
2031 s->listener->nbconn < s->listener->maxconn) {
2032 /* we should reactivate the listener */
2033 EV_FD_SET(s->listener->fd, DIR_RD);
2034 s->listener->state = LI_READY;
2035 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002036
2037 if (unlikely((global.mode & MODE_DEBUG) &&
2038 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
2039 int len;
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002040 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002041 s->uniq_id, s->be->id,
Willy Tarreauec22b2c2009-03-06 13:07:40 +01002042 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002043 write(1, trash, len);
2044 }
2045
2046 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
2047 session_process_counters(s);
2048
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002049 if (s->txn.status) {
2050 int n;
2051
2052 n = s->txn.status / 100;
2053 if (n < 1 || n > 5)
2054 n = 0;
2055
2056 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau24657792010-02-26 10:30:28 +01002057 s->fe->counters.fe.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002058
Willy Tarreau24657792010-02-26 10:30:28 +01002059 if ((s->flags & SN_BE_ASSIGNED) &&
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002060 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau24657792010-02-26 10:30:28 +01002061 s->be->counters.be.http.rsp[n]++;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002062 }
2063
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002064 /* let's do a final log if we need it */
2065 if (s->logs.logwait &&
2066 !(s->flags & SN_MONITOR) &&
2067 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002068 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002069 }
2070
2071 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002072 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002073 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002074 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002075 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002076}
2077
Willy Tarreau7c669d72008-06-20 15:04:11 +02002078/*
2079 * This function adjusts sess->srv_conn and maintains the previous and new
2080 * server's served session counts. Setting newsrv to NULL is enough to release
2081 * current connection slot. This function also notifies any LB algo which might
2082 * expect to be informed about any change in the number of active sessions on a
2083 * server.
2084 */
2085void sess_change_server(struct session *sess, struct server *newsrv)
2086{
2087 if (sess->srv_conn == newsrv)
2088 return;
2089
2090 if (sess->srv_conn) {
2091 sess->srv_conn->served--;
2092 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2093 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
2094 sess->srv_conn = NULL;
2095 }
2096
2097 if (newsrv) {
2098 newsrv->served++;
2099 if (newsrv->proxy->lbprm.server_take_conn)
2100 newsrv->proxy->lbprm.server_take_conn(newsrv);
2101 sess->srv_conn = newsrv;
2102 }
2103}
2104
Willy Tarreau84455332009-03-15 22:34:05 +01002105/* Set correct session termination flags in case no analyser has done it. It
2106 * also counts a failed request if the server state has not reached the request
2107 * stage.
2108 */
2109void sess_set_term_flags(struct session *s)
2110{
2111 if (!(s->flags & SN_FINST_MASK)) {
2112 if (s->si[1].state < SI_ST_REQ) {
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002113
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002114 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002115 if (s->listener->counters)
2116 s->listener->counters->failed_req++;
2117
Willy Tarreau84455332009-03-15 22:34:05 +01002118 s->flags |= SN_FINST_R;
2119 }
2120 else if (s->si[1].state == SI_ST_QUE)
2121 s->flags |= SN_FINST_Q;
2122 else if (s->si[1].state < SI_ST_EST)
2123 s->flags |= SN_FINST_C;
Willy Tarreau033b2db2010-03-04 17:54:21 +01002124 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreau84455332009-03-15 22:34:05 +01002125 s->flags |= SN_FINST_D;
2126 else
2127 s->flags |= SN_FINST_L;
2128 }
2129}
2130
2131/* Handle server-side errors for default protocols. It is called whenever a a
2132 * connection setup is aborted or a request is aborted in queue. It sets the
2133 * session termination flags so that the caller does not have to worry about
2134 * them. It's installed as ->srv_error for the server-side stream_interface.
2135 */
2136void default_srv_error(struct session *s, struct stream_interface *si)
2137{
2138 int err_type = si->err_type;
2139 int err = 0, fin = 0;
2140
2141 if (err_type & SI_ET_QUEUE_ABRT) {
2142 err = SN_ERR_CLICL;
2143 fin = SN_FINST_Q;
2144 }
2145 else if (err_type & SI_ET_CONN_ABRT) {
2146 err = SN_ERR_CLICL;
2147 fin = SN_FINST_C;
2148 }
2149 else if (err_type & SI_ET_QUEUE_TO) {
2150 err = SN_ERR_SRVTO;
2151 fin = SN_FINST_Q;
2152 }
2153 else if (err_type & SI_ET_QUEUE_ERR) {
2154 err = SN_ERR_SRVCL;
2155 fin = SN_FINST_Q;
2156 }
2157 else if (err_type & SI_ET_CONN_TO) {
2158 err = SN_ERR_SRVTO;
2159 fin = SN_FINST_C;
2160 }
2161 else if (err_type & SI_ET_CONN_ERR) {
2162 err = SN_ERR_SRVCL;
2163 fin = SN_FINST_C;
2164 }
2165 else /* SI_ET_CONN_OTHER and others */ {
2166 err = SN_ERR_INTERNAL;
2167 fin = SN_FINST_C;
2168 }
2169
2170 if (!(s->flags & SN_ERR_MASK))
2171 s->flags |= err;
2172 if (!(s->flags & SN_FINST_MASK))
2173 s->flags |= fin;
2174}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002175
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002176
Willy Tarreau8b22a712010-06-18 17:46:06 +02002177/************************************************************************/
2178/* All supported ACL keywords must be declared here. */
2179/************************************************************************/
2180
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002181/* set test->i to the General Purpose Counter 0 value in the stksess entry <ts> */
2182static int
2183acl_fetch_get_gpc0(struct stktable *table, struct acl_test *test, struct stksess *ts)
2184{
2185 test->flags = ACL_TEST_F_VOL_TEST;
2186 test->i = 0;
2187 if (ts != NULL) {
2188 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2189 if (!ptr)
2190 return 0; /* parameter not stored */
2191 test->i = stktable_data_cast(ptr, gpc0);
2192 }
2193 return 1;
2194}
2195
2196/* set test->i to the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002197 * frontend counters.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002198 */
2199static int
Willy Tarreau56123282010-08-06 19:06:56 +02002200acl_fetch_sc1_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2201 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002202{
Willy Tarreau56123282010-08-06 19:06:56 +02002203 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002204 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002205 return acl_fetch_get_gpc0(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002206}
2207
2208/* set test->i to the General Purpose Counter 0 value from the session's tracked
2209 * backend counters.
2210 */
2211static int
Willy Tarreau56123282010-08-06 19:06:56 +02002212acl_fetch_sc2_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2213 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002214{
Willy Tarreau56123282010-08-06 19:06:56 +02002215 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002216 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002217 return acl_fetch_get_gpc0(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002218}
2219
2220/* set test->i to the General Purpose Counter 0 value from the session's source
2221 * address in the table pointed to by expr.
2222 */
2223static int
2224acl_fetch_src_get_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2225 struct acl_expr *expr, struct acl_test *test)
2226{
2227 struct stktable_key *key;
2228
2229 key = tcpv4_src_to_stktable_key(l4);
2230 if (!key)
2231 return 0; /* only TCPv4 is supported right now */
2232
2233 if (expr->arg_len)
2234 px = find_stktable(expr->arg.str);
2235
2236 if (!px)
2237 return 0; /* table not found */
2238
2239 return acl_fetch_get_gpc0(&px->table, test, stktable_lookup_key(&px->table, key));
2240}
2241
2242/* Increment the General Purpose Counter 0 value in the stksess entry <ts> and
2243 * return it into test->i.
2244 */
2245static int
2246acl_fetch_inc_gpc0(struct stktable *table, struct acl_test *test, struct stksess *ts)
2247{
2248 test->flags = ACL_TEST_F_VOL_TEST;
2249 test->i = 0;
2250 if (ts != NULL) {
2251 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2252 if (!ptr)
2253 return 0; /* parameter not stored */
2254 test->i = ++stktable_data_cast(ptr, gpc0);
2255 }
2256 return 1;
2257}
2258
2259/* Increment the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002260 * frontend counters and return it into test->i.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002261 */
2262static int
Willy Tarreau56123282010-08-06 19:06:56 +02002263acl_fetch_sc1_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2264 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002265{
Willy Tarreau56123282010-08-06 19:06:56 +02002266 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002267 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002268 return acl_fetch_inc_gpc0(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002269}
2270
2271/* Increment the General Purpose Counter 0 value from the session's tracked
2272 * backend counters and return it into test->i.
2273 */
2274static int
Willy Tarreau56123282010-08-06 19:06:56 +02002275acl_fetch_sc2_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2276 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002277{
Willy Tarreau56123282010-08-06 19:06:56 +02002278 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002279 return 0;
Willy Tarreau56123282010-08-06 19:06:56 +02002280 return acl_fetch_inc_gpc0(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002281}
2282
2283/* Increment the General Purpose Counter 0 value from the session's source
2284 * address in the table pointed to by expr, and return it into test->i.
2285 */
2286static int
2287acl_fetch_src_inc_gpc0(struct proxy *px, struct session *l4, void *l7, int dir,
2288 struct acl_expr *expr, struct acl_test *test)
2289{
2290 struct stktable_key *key;
2291
2292 key = tcpv4_src_to_stktable_key(l4);
2293 if (!key)
2294 return 0; /* only TCPv4 is supported right now */
2295
2296 if (expr->arg_len)
2297 px = find_stktable(expr->arg.str);
2298
2299 if (!px)
2300 return 0; /* table not found */
2301
2302 return acl_fetch_inc_gpc0(&px->table, test, stktable_update_key(&px->table, key));
2303}
2304
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002305/* set test->i to the cumulated number of connections in the stksess entry <ts> */
2306static int
2307acl_fetch_conn_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2308{
2309 test->flags = ACL_TEST_F_VOL_TEST;
2310 test->i = 0;
2311 if (ts != NULL) {
2312 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CNT);
2313 if (!ptr)
2314 return 0; /* parameter not stored */
2315 test->i = stktable_data_cast(ptr, conn_cnt);
2316 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002317 return 1;
2318}
2319
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002320/* set test->i to the cumulated number of connections from the session's tracked FE counters */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002321static int
Willy Tarreau56123282010-08-06 19:06:56 +02002322acl_fetch_sc1_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2323 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002324{
Willy Tarreau56123282010-08-06 19:06:56 +02002325 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002326 return 0;
2327
Willy Tarreau56123282010-08-06 19:06:56 +02002328 return acl_fetch_conn_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002329}
2330
2331/* set test->i to the cumulated number of connections from the session's tracked BE counters */
2332static int
Willy Tarreau56123282010-08-06 19:06:56 +02002333acl_fetch_sc2_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2334 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002335{
Willy Tarreau56123282010-08-06 19:06:56 +02002336 if (!l4->stkctr2_entry)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002337 return 0;
2338
Willy Tarreau56123282010-08-06 19:06:56 +02002339 return acl_fetch_conn_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002340}
2341
2342/* set test->i to the cumulated number of connections from the session's source
2343 * address in the table pointed to by expr.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002344 */
2345static int
2346acl_fetch_src_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2347 struct acl_expr *expr, struct acl_test *test)
2348{
Willy Tarreau8b22a712010-06-18 17:46:06 +02002349 struct stktable_key *key;
2350
2351 key = tcpv4_src_to_stktable_key(l4);
2352 if (!key)
2353 return 0; /* only TCPv4 is supported right now */
2354
2355 if (expr->arg_len)
2356 px = find_stktable(expr->arg.str);
2357
2358 if (!px)
2359 return 0; /* table not found */
2360
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002361 return acl_fetch_conn_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau8b22a712010-06-18 17:46:06 +02002362}
2363
Willy Tarreau91c43d72010-06-20 11:19:22 +02002364/* set test->i to the connection rate in the stksess entry <ts> over the configured period */
2365static int
2366acl_fetch_conn_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2367{
2368 test->flags = ACL_TEST_F_VOL_TEST;
2369 test->i = 0;
2370 if (ts != NULL) {
2371 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_RATE);
2372 if (!ptr)
2373 return 0; /* parameter not stored */
2374 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
2375 table->data_arg[STKTABLE_DT_CONN_RATE].u);
2376 }
2377 return 1;
2378}
2379
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002380/* set test->i to the connection rate from the session's tracked FE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002381 * the configured period.
2382 */
2383static int
Willy Tarreau56123282010-08-06 19:06:56 +02002384acl_fetch_sc1_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2385 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002386{
Willy Tarreau56123282010-08-06 19:06:56 +02002387 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002388 return 0;
2389
Willy Tarreau56123282010-08-06 19:06:56 +02002390 return acl_fetch_conn_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002391}
2392
2393/* set test->i to the connection rate from the session's tracked BE counters over
2394 * the configured period.
2395 */
2396static int
Willy Tarreau56123282010-08-06 19:06:56 +02002397acl_fetch_sc2_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2398 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002399{
Willy Tarreau56123282010-08-06 19:06:56 +02002400 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002401 return 0;
2402
Willy Tarreau56123282010-08-06 19:06:56 +02002403 return acl_fetch_conn_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002404}
2405
2406/* set test->i to the connection rate from the session's source address in the
2407 * table pointed to by expr, over the configured period.
2408 */
2409static int
2410acl_fetch_src_conn_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2411 struct acl_expr *expr, struct acl_test *test)
2412{
2413 struct stktable_key *key;
2414
2415 key = tcpv4_src_to_stktable_key(l4);
2416 if (!key)
2417 return 0; /* only TCPv4 is supported right now */
2418
2419 if (expr->arg_len)
2420 px = find_stktable(expr->arg.str);
2421
2422 if (!px)
2423 return 0; /* table not found */
2424
2425 return acl_fetch_conn_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2426}
2427
Willy Tarreau8b22a712010-06-18 17:46:06 +02002428/* set test->i to the number of connections from the session's source address
2429 * in the table pointed to by expr, after updating it.
2430 */
2431static int
2432acl_fetch_src_updt_conn_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2433 struct acl_expr *expr, struct acl_test *test)
2434{
2435 struct stksess *ts;
2436 struct stktable_key *key;
2437 void *ptr;
2438
2439 key = tcpv4_src_to_stktable_key(l4);
2440 if (!key)
2441 return 0; /* only TCPv4 is supported right now */
2442
2443 if (expr->arg_len)
2444 px = find_stktable(expr->arg.str);
2445
2446 if (!px)
2447 return 0; /* table not found */
2448
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002449 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2450 /* entry does not exist and could not be created */
2451 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002452
2453 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2454 if (!ptr)
2455 return 0; /* parameter not stored in this table */
2456
2457 test->i = ++stktable_data_cast(ptr, conn_cnt);
2458 test->flags = ACL_TEST_F_VOL_TEST;
2459 return 1;
2460}
2461
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002462/* set test->i to the number of concurrent connections in the stksess entry <ts> */
2463static int
2464acl_fetch_conn_cur(struct stktable *table, struct acl_test *test, struct stksess *ts)
2465{
2466 test->flags = ACL_TEST_F_VOL_TEST;
2467 test->i = 0;
2468
2469 if (ts != NULL) {
2470 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CUR);
2471 if (!ptr)
2472 return 0; /* parameter not stored */
2473 test->i = stktable_data_cast(ptr, conn_cur);
2474 }
2475 return 1;
2476}
2477
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002478/* set test->i to the number of concurrent connections from the session's tracked FE counters */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002479static int
Willy Tarreau56123282010-08-06 19:06:56 +02002480acl_fetch_sc1_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2481 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002482{
Willy Tarreau56123282010-08-06 19:06:56 +02002483 if (!l4->stkctr1_entry)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002484 return 0;
2485
Willy Tarreau56123282010-08-06 19:06:56 +02002486 return acl_fetch_conn_cur(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002487}
2488
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002489/* set test->i to the number of concurrent connections from the session's tracked BE counters */
2490static int
Willy Tarreau56123282010-08-06 19:06:56 +02002491acl_fetch_sc2_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2492 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002493{
Willy Tarreau56123282010-08-06 19:06:56 +02002494 if (!l4->stkctr2_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002495 return 0;
2496
Willy Tarreau56123282010-08-06 19:06:56 +02002497 return acl_fetch_conn_cur(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002498}
2499
Willy Tarreau38285c12010-06-18 16:35:43 +02002500/* set test->i to the number of concurrent connections from the session's source
2501 * address in the table pointed to by expr.
2502 */
2503static int
2504acl_fetch_src_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
2505 struct acl_expr *expr, struct acl_test *test)
2506{
Willy Tarreau38285c12010-06-18 16:35:43 +02002507 struct stktable_key *key;
2508
2509 key = tcpv4_src_to_stktable_key(l4);
2510 if (!key)
2511 return 0; /* only TCPv4 is supported right now */
2512
2513 if (expr->arg_len)
2514 px = find_stktable(expr->arg.str);
2515
2516 if (!px)
2517 return 0; /* table not found */
2518
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002519 return acl_fetch_conn_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau38285c12010-06-18 16:35:43 +02002520}
2521
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002522/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2523static int
2524acl_fetch_sess_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2525{
2526 test->flags = ACL_TEST_F_VOL_TEST;
2527 test->i = 0;
2528 if (ts != NULL) {
2529 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_CNT);
2530 if (!ptr)
2531 return 0; /* parameter not stored */
2532 test->i = stktable_data_cast(ptr, sess_cnt);
2533 }
2534 return 1;
2535}
2536
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002537/* set test->i to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002538static int
Willy Tarreau56123282010-08-06 19:06:56 +02002539acl_fetch_sc1_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2540 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002541{
Willy Tarreau56123282010-08-06 19:06:56 +02002542 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002543 return 0;
2544
Willy Tarreau56123282010-08-06 19:06:56 +02002545 return acl_fetch_sess_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002546}
2547
2548/* set test->i to the cumulated number of sessions from the session's tracked BE counters */
2549static int
Willy Tarreau56123282010-08-06 19:06:56 +02002550acl_fetch_sc2_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2551 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002552{
Willy Tarreau56123282010-08-06 19:06:56 +02002553 if (!l4->stkctr2_entry)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002554 return 0;
2555
Willy Tarreau56123282010-08-06 19:06:56 +02002556 return acl_fetch_sess_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002557}
2558
2559/* set test->i to the cumulated number of session from the session's source
2560 * address in the table pointed to by expr.
2561 */
2562static int
2563acl_fetch_src_sess_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2564 struct acl_expr *expr, struct acl_test *test)
2565{
2566 struct stktable_key *key;
2567
2568 key = tcpv4_src_to_stktable_key(l4);
2569 if (!key)
2570 return 0; /* only TCPv4 is supported right now */
2571
2572 if (expr->arg_len)
2573 px = find_stktable(expr->arg.str);
2574
2575 if (!px)
2576 return 0; /* table not found */
2577
2578 return acl_fetch_sess_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2579}
2580
Willy Tarreau91c43d72010-06-20 11:19:22 +02002581/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2582static int
2583acl_fetch_sess_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2584{
2585 test->flags = ACL_TEST_F_VOL_TEST;
2586 test->i = 0;
2587 if (ts != NULL) {
2588 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_RATE);
2589 if (!ptr)
2590 return 0; /* parameter not stored */
2591 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
2592 table->data_arg[STKTABLE_DT_SESS_RATE].u);
2593 }
2594 return 1;
2595}
2596
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002597/* set test->i to the session rate from the session's tracked FE counters over
2598 * the configured period.
2599 */
2600static int
Willy Tarreau56123282010-08-06 19:06:56 +02002601acl_fetch_sc1_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2602 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002603{
Willy Tarreau56123282010-08-06 19:06:56 +02002604 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002605 return 0;
2606
Willy Tarreau56123282010-08-06 19:06:56 +02002607 return acl_fetch_sess_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002608}
2609
2610/* set test->i to the session rate from the session's tracked BE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002611 * the configured period.
2612 */
2613static int
Willy Tarreau56123282010-08-06 19:06:56 +02002614acl_fetch_sc2_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2615 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002616{
Willy Tarreau56123282010-08-06 19:06:56 +02002617 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002618 return 0;
2619
Willy Tarreau56123282010-08-06 19:06:56 +02002620 return acl_fetch_sess_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002621}
2622
2623/* set test->i to the session rate from the session's source address in the
2624 * table pointed to by expr, over the configured period.
2625 */
2626static int
2627acl_fetch_src_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2628 struct acl_expr *expr, struct acl_test *test)
2629{
2630 struct stktable_key *key;
2631
2632 key = tcpv4_src_to_stktable_key(l4);
2633 if (!key)
2634 return 0; /* only TCPv4 is supported right now */
2635
2636 if (expr->arg_len)
2637 px = find_stktable(expr->arg.str);
2638
2639 if (!px)
2640 return 0; /* table not found */
2641
2642 return acl_fetch_sess_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2643}
2644
Willy Tarreauda7ff642010-06-23 11:44:09 +02002645/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2646static int
2647acl_fetch_http_req_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2648{
2649 test->flags = ACL_TEST_F_VOL_TEST;
2650 test->i = 0;
2651 if (ts != NULL) {
2652 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_CNT);
2653 if (!ptr)
2654 return 0; /* parameter not stored */
2655 test->i = stktable_data_cast(ptr, http_req_cnt);
2656 }
2657 return 1;
2658}
2659
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002660/* set test->i to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002661static int
Willy Tarreau56123282010-08-06 19:06:56 +02002662acl_fetch_sc1_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2663 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002664{
Willy Tarreau56123282010-08-06 19:06:56 +02002665 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002666 return 0;
2667
Willy Tarreau56123282010-08-06 19:06:56 +02002668 return acl_fetch_http_req_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002669}
2670
2671/* set test->i to the cumulated number of sessions from the session's tracked BE counters */
2672static int
Willy Tarreau56123282010-08-06 19:06:56 +02002673acl_fetch_sc2_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2674 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002675{
Willy Tarreau56123282010-08-06 19:06:56 +02002676 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002677 return 0;
2678
Willy Tarreau56123282010-08-06 19:06:56 +02002679 return acl_fetch_http_req_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002680}
2681
2682/* set test->i to the cumulated number of session from the session's source
2683 * address in the table pointed to by expr.
2684 */
2685static int
2686acl_fetch_src_http_req_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002687 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002688{
2689 struct stktable_key *key;
2690
2691 key = tcpv4_src_to_stktable_key(l4);
2692 if (!key)
2693 return 0; /* only TCPv4 is supported right now */
2694
2695 if (expr->arg_len)
2696 px = find_stktable(expr->arg.str);
2697
2698 if (!px)
2699 return 0; /* table not found */
2700
2701 return acl_fetch_http_req_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2702}
2703
2704/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2705static int
2706acl_fetch_http_req_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2707{
2708 test->flags = ACL_TEST_F_VOL_TEST;
2709 test->i = 0;
2710 if (ts != NULL) {
2711 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_RATE);
2712 if (!ptr)
2713 return 0; /* parameter not stored */
2714 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2715 table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
2716 }
2717 return 1;
2718}
2719
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002720/* set test->i to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02002721 * the configured period.
2722 */
2723static int
Willy Tarreau56123282010-08-06 19:06:56 +02002724acl_fetch_sc1_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2725 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002726{
Willy Tarreau56123282010-08-06 19:06:56 +02002727 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002728 return 0;
2729
Willy Tarreau56123282010-08-06 19:06:56 +02002730 return acl_fetch_http_req_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002731}
2732
2733/* set test->i to the session rate from the session's tracked BE counters over
2734 * the configured period.
2735 */
2736static int
Willy Tarreau56123282010-08-06 19:06:56 +02002737acl_fetch_sc2_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2738 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002739{
Willy Tarreau56123282010-08-06 19:06:56 +02002740 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002741 return 0;
2742
Willy Tarreau56123282010-08-06 19:06:56 +02002743 return acl_fetch_http_req_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002744}
2745
2746/* set test->i to the session rate from the session's source address in the
2747 * table pointed to by expr, over the configured period.
2748 */
2749static int
2750acl_fetch_src_http_req_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002751 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002752{
2753 struct stktable_key *key;
2754
2755 key = tcpv4_src_to_stktable_key(l4);
2756 if (!key)
2757 return 0; /* only TCPv4 is supported right now */
2758
2759 if (expr->arg_len)
2760 px = find_stktable(expr->arg.str);
2761
2762 if (!px)
2763 return 0; /* table not found */
2764
2765 return acl_fetch_http_req_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2766}
2767
2768/* set test->i to the cumulated number of sessions in the stksess entry <ts> */
2769static int
2770acl_fetch_http_err_cnt(struct stktable *table, struct acl_test *test, struct stksess *ts)
2771{
2772 test->flags = ACL_TEST_F_VOL_TEST;
2773 test->i = 0;
2774 if (ts != NULL) {
2775 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_CNT);
2776 if (!ptr)
2777 return 0; /* parameter not stored */
2778 test->i = stktable_data_cast(ptr, http_err_cnt);
2779 }
2780 return 1;
2781}
2782
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002783/* set test->i to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002784static int
Willy Tarreau56123282010-08-06 19:06:56 +02002785acl_fetch_sc1_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2786 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002787{
Willy Tarreau56123282010-08-06 19:06:56 +02002788 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002789 return 0;
2790
Willy Tarreau56123282010-08-06 19:06:56 +02002791 return acl_fetch_http_err_cnt(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002792}
2793
2794/* set test->i to the cumulated number of sessions from the session's tracked BE counters */
2795static int
Willy Tarreau56123282010-08-06 19:06:56 +02002796acl_fetch_sc2_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
2797 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002798{
Willy Tarreau56123282010-08-06 19:06:56 +02002799 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002800 return 0;
2801
Willy Tarreau56123282010-08-06 19:06:56 +02002802 return acl_fetch_http_err_cnt(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002803}
2804
2805/* set test->i to the cumulated number of session from the session's source
2806 * address in the table pointed to by expr.
2807 */
2808static int
2809acl_fetch_src_http_err_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02002810 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002811{
2812 struct stktable_key *key;
2813
2814 key = tcpv4_src_to_stktable_key(l4);
2815 if (!key)
2816 return 0; /* only TCPv4 is supported right now */
2817
2818 if (expr->arg_len)
2819 px = find_stktable(expr->arg.str);
2820
2821 if (!px)
2822 return 0; /* table not found */
2823
2824 return acl_fetch_http_err_cnt(&px->table, test, stktable_lookup_key(&px->table, key));
2825}
2826
2827/* set test->i to the session rate in the stksess entry <ts> over the configured period */
2828static int
2829acl_fetch_http_err_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2830{
2831 test->flags = ACL_TEST_F_VOL_TEST;
2832 test->i = 0;
2833 if (ts != NULL) {
2834 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_RATE);
2835 if (!ptr)
2836 return 0; /* parameter not stored */
2837 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2838 table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
2839 }
2840 return 1;
2841}
2842
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002843/* set test->i to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02002844 * the configured period.
2845 */
2846static int
Willy Tarreau56123282010-08-06 19:06:56 +02002847acl_fetch_sc1_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2848 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002849{
Willy Tarreau56123282010-08-06 19:06:56 +02002850 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002851 return 0;
2852
Willy Tarreau56123282010-08-06 19:06:56 +02002853 return acl_fetch_http_err_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002854}
2855
2856/* set test->i to the session rate from the session's tracked BE counters over
2857 * the configured period.
2858 */
2859static int
Willy Tarreau56123282010-08-06 19:06:56 +02002860acl_fetch_sc2_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2861 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002862{
Willy Tarreau56123282010-08-06 19:06:56 +02002863 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002864 return 0;
2865
Willy Tarreau56123282010-08-06 19:06:56 +02002866 return acl_fetch_http_err_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002867}
2868
2869/* set test->i to the session rate from the session's source address in the
2870 * table pointed to by expr, over the configured period.
2871 */
2872static int
2873acl_fetch_src_http_err_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2874 struct acl_expr *expr, struct acl_test *test)
2875{
2876 struct stktable_key *key;
2877
2878 key = tcpv4_src_to_stktable_key(l4);
2879 if (!key)
2880 return 0; /* only TCPv4 is supported right now */
2881
2882 if (expr->arg_len)
2883 px = find_stktable(expr->arg.str);
2884
2885 if (!px)
2886 return 0; /* table not found */
2887
2888 return acl_fetch_http_err_rate(&px->table, test, stktable_lookup_key(&px->table, key));
2889}
2890
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002891/* set test->i to the number of kbytes received from clients matching the stksess entry <ts> */
2892static int
2893acl_fetch_kbytes_in(struct stktable *table, struct acl_test *test, struct stksess *ts)
2894{
2895 test->flags = ACL_TEST_F_VOL_TEST;
2896 test->i = 0;
2897
2898 if (ts != NULL) {
2899 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_CNT);
2900 if (!ptr)
2901 return 0; /* parameter not stored */
2902 test->i = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
2903 }
2904 return 1;
2905}
2906
2907/* set test->i to the number of kbytes received from clients according to the
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002908 * session's tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002909 */
2910static int
Willy Tarreau56123282010-08-06 19:06:56 +02002911acl_fetch_sc1_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2912 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002913{
Willy Tarreau56123282010-08-06 19:06:56 +02002914 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002915 return 0;
2916
Willy Tarreau56123282010-08-06 19:06:56 +02002917 return acl_fetch_kbytes_in(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002918}
2919
2920/* set test->i to the number of kbytes received from clients according to the
2921 * session's tracked BE counters.
2922 */
2923static int
Willy Tarreau56123282010-08-06 19:06:56 +02002924acl_fetch_sc2_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2925 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002926{
Willy Tarreau56123282010-08-06 19:06:56 +02002927 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002928 return 0;
2929
Willy Tarreau56123282010-08-06 19:06:56 +02002930 return acl_fetch_kbytes_in(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002931}
2932
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002933/* set test->i to the number of kbytes received from the session's source
2934 * address in the table pointed to by expr.
2935 */
2936static int
2937acl_fetch_src_kbytes_in(struct proxy *px, struct session *l4, void *l7, int dir,
2938 struct acl_expr *expr, struct acl_test *test)
2939{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02002940 struct stktable_key *key;
2941
2942 key = tcpv4_src_to_stktable_key(l4);
2943 if (!key)
2944 return 0; /* only TCPv4 is supported right now */
2945
2946 if (expr->arg_len)
2947 px = find_stktable(expr->arg.str);
2948
2949 if (!px)
2950 return 0; /* table not found */
2951
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002952 return acl_fetch_kbytes_in(&px->table, test, stktable_lookup_key(&px->table, key));
2953}
2954
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002955/* set test->i to the bytes rate from clients in the stksess entry <ts> over the
2956 * configured period.
2957 */
2958static int
2959acl_fetch_bytes_in_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
2960{
2961 test->flags = ACL_TEST_F_VOL_TEST;
2962 test->i = 0;
2963 if (ts != NULL) {
2964 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_RATE);
2965 if (!ptr)
2966 return 0; /* parameter not stored */
2967 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
2968 table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
2969 }
2970 return 1;
2971}
2972
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002973/* set test->i to the bytes rate from clients from the session's tracked FE
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002974 * counters over the configured period.
2975 */
2976static int
Willy Tarreau56123282010-08-06 19:06:56 +02002977acl_fetch_sc1_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2978 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002979{
Willy Tarreau56123282010-08-06 19:06:56 +02002980 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002981 return 0;
2982
Willy Tarreau56123282010-08-06 19:06:56 +02002983 return acl_fetch_bytes_in_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002984}
2985
2986/* set test->i to the bytes rate from clients from the session's tracked BE
2987 * counters over the configured period.
2988 */
2989static int
Willy Tarreau56123282010-08-06 19:06:56 +02002990acl_fetch_sc2_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
2991 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002992{
Willy Tarreau56123282010-08-06 19:06:56 +02002993 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002994 return 0;
2995
Willy Tarreau56123282010-08-06 19:06:56 +02002996 return acl_fetch_bytes_in_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02002997}
2998
2999/* set test->i to the bytes rate from clients from the session's source address
3000 * in the table pointed to by expr, over the configured period.
3001 */
3002static int
3003acl_fetch_src_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02003004 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003005{
3006 struct stktable_key *key;
3007
3008 key = tcpv4_src_to_stktable_key(l4);
3009 if (!key)
3010 return 0; /* only TCPv4 is supported right now */
3011
3012 if (expr->arg_len)
3013 px = find_stktable(expr->arg.str);
3014
3015 if (!px)
3016 return 0; /* table not found */
3017
3018 return acl_fetch_bytes_in_rate(&px->table, test, stktable_lookup_key(&px->table, key));
3019}
3020
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003021/* set test->i to the number of kbytes sent to clients matching the stksess entry <ts> */
3022static int
3023acl_fetch_kbytes_out(struct stktable *table, struct acl_test *test, struct stksess *ts)
3024{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003025 test->flags = ACL_TEST_F_VOL_TEST;
3026 test->i = 0;
3027
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003028 if (ts != NULL) {
3029 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003030 if (!ptr)
3031 return 0; /* parameter not stored */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003032 test->i = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003033 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003034 return 1;
3035}
3036
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003037/* set test->i to the number of kbytes sent to clients according to the session's
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003038 * tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003039 */
3040static int
Willy Tarreau56123282010-08-06 19:06:56 +02003041acl_fetch_sc1_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3042 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003043{
Willy Tarreau56123282010-08-06 19:06:56 +02003044 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003045 return 0;
3046
Willy Tarreau56123282010-08-06 19:06:56 +02003047 return acl_fetch_kbytes_out(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003048}
3049
3050/* set test->i to the number of kbytes sent to clients according to the session's
3051 * tracked BE counters.
3052 */
3053static int
Willy Tarreau56123282010-08-06 19:06:56 +02003054acl_fetch_sc2_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3055 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003056{
Willy Tarreau56123282010-08-06 19:06:56 +02003057 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003058 return 0;
3059
Willy Tarreau56123282010-08-06 19:06:56 +02003060 return acl_fetch_kbytes_out(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003061}
3062
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003063/* set test->i to the number of kbytes sent to the session's source address in
3064 * the table pointed to by expr.
3065 */
3066static int
3067acl_fetch_src_kbytes_out(struct proxy *px, struct session *l4, void *l7, int dir,
3068 struct acl_expr *expr, struct acl_test *test)
3069{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003070 struct stktable_key *key;
3071
3072 key = tcpv4_src_to_stktable_key(l4);
3073 if (!key)
3074 return 0; /* only TCPv4 is supported right now */
3075
3076 if (expr->arg_len)
3077 px = find_stktable(expr->arg.str);
3078
3079 if (!px)
3080 return 0; /* table not found */
3081
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003082 return acl_fetch_kbytes_out(&px->table, test, stktable_lookup_key(&px->table, key));
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003083}
3084
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003085/* set test->i to the bytes rate to clients in the stksess entry <ts> over the
3086 * configured period.
3087 */
3088static int
3089acl_fetch_bytes_out_rate(struct stktable *table, struct acl_test *test, struct stksess *ts)
3090{
3091 test->flags = ACL_TEST_F_VOL_TEST;
3092 test->i = 0;
3093 if (ts != NULL) {
3094 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_RATE);
3095 if (!ptr)
3096 return 0; /* parameter not stored */
3097 test->i = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
3098 table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
3099 }
3100 return 1;
3101}
3102
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003103/* set test->i to the bytes rate to clients from the session's tracked FE counters
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003104 * over the configured period.
3105 */
3106static int
Willy Tarreau56123282010-08-06 19:06:56 +02003107acl_fetch_sc1_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3108 struct acl_expr *expr, struct acl_test *test)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003109{
Willy Tarreau56123282010-08-06 19:06:56 +02003110 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003111 return 0;
3112
Willy Tarreau56123282010-08-06 19:06:56 +02003113 return acl_fetch_bytes_out_rate(l4->stkctr1_table, test, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003114}
3115
3116/* set test->i to the bytes rate to clients from the session's tracked BE counters
3117 * over the configured period.
3118 */
3119static int
Willy Tarreau56123282010-08-06 19:06:56 +02003120acl_fetch_sc2_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
3121 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003122{
Willy Tarreau56123282010-08-06 19:06:56 +02003123 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003124 return 0;
3125
Willy Tarreau56123282010-08-06 19:06:56 +02003126 return acl_fetch_bytes_out_rate(l4->stkctr2_table, test, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003127}
3128
3129/* set test->i to the bytes rate to client from the session's source address in
3130 * the table pointed to by expr, over the configured period.
3131 */
3132static int
3133acl_fetch_src_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau56123282010-08-06 19:06:56 +02003134 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003135{
3136 struct stktable_key *key;
3137
3138 key = tcpv4_src_to_stktable_key(l4);
3139 if (!key)
3140 return 0; /* only TCPv4 is supported right now */
3141
3142 if (expr->arg_len)
3143 px = find_stktable(expr->arg.str);
3144
3145 if (!px)
3146 return 0; /* table not found */
3147
3148 return acl_fetch_bytes_out_rate(&px->table, test, stktable_lookup_key(&px->table, key));
3149}
3150
Willy Tarreau8b22a712010-06-18 17:46:06 +02003151
3152/* Note: must not be declared <const> as its list will be overwritten */
3153static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau56123282010-08-06 19:06:56 +02003154 { "sc1_get_gpc0", acl_parse_int, acl_fetch_sc1_get_gpc0, acl_match_int, ACL_USE_NOTHING },
3155 { "sc2_get_gpc0", acl_parse_int, acl_fetch_sc2_get_gpc0, acl_match_int, ACL_USE_NOTHING },
3156 { "src_get_gpc0", acl_parse_int, acl_fetch_src_get_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE },
3157 { "sc1_inc_gpc0", acl_parse_int, acl_fetch_sc1_inc_gpc0, acl_match_int, ACL_USE_NOTHING },
3158 { "sc2_inc_gpc0", acl_parse_int, acl_fetch_sc2_inc_gpc0, acl_match_int, ACL_USE_NOTHING },
3159 { "src_inc_gpc0", acl_parse_int, acl_fetch_src_inc_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE },
3160 { "sc1_conn_cnt", acl_parse_int, acl_fetch_sc1_conn_cnt, acl_match_int, ACL_USE_NOTHING },
3161 { "sc2_conn_cnt", acl_parse_int, acl_fetch_sc2_conn_cnt, acl_match_int, ACL_USE_NOTHING },
3162 { "src_conn_cnt", acl_parse_int, acl_fetch_src_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3163 { "sc1_conn_rate", acl_parse_int, acl_fetch_sc1_conn_rate, acl_match_int, ACL_USE_NOTHING },
3164 { "sc2_conn_rate", acl_parse_int, acl_fetch_sc2_conn_rate, acl_match_int, ACL_USE_NOTHING },
3165 { "src_conn_rate", acl_parse_int, acl_fetch_src_conn_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3166 { "src_updt_conn_cnt", acl_parse_int, acl_fetch_src_updt_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3167 { "sc1_conn_cur", acl_parse_int, acl_fetch_sc1_conn_cur, acl_match_int, ACL_USE_NOTHING },
3168 { "sc2_conn_cur", acl_parse_int, acl_fetch_sc2_conn_cur, acl_match_int, ACL_USE_NOTHING },
3169 { "src_conn_cur", acl_parse_int, acl_fetch_src_conn_cur, acl_match_int, ACL_USE_TCP4_VOLATILE },
3170 { "sc1_sess_cnt", acl_parse_int, acl_fetch_sc1_sess_cnt, acl_match_int, ACL_USE_NOTHING },
3171 { "sc2_sess_cnt", acl_parse_int, acl_fetch_sc2_sess_cnt, acl_match_int, ACL_USE_NOTHING },
3172 { "src_sess_cnt", acl_parse_int, acl_fetch_src_sess_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3173 { "sc1_sess_rate", acl_parse_int, acl_fetch_sc1_sess_rate, acl_match_int, ACL_USE_NOTHING },
3174 { "sc2_sess_rate", acl_parse_int, acl_fetch_sc2_sess_rate, acl_match_int, ACL_USE_NOTHING },
3175 { "src_sess_rate", acl_parse_int, acl_fetch_src_sess_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3176 { "sc1_http_req_cnt", acl_parse_int, acl_fetch_sc1_http_req_cnt, acl_match_int, ACL_USE_NOTHING },
3177 { "sc2_http_req_cnt", acl_parse_int, acl_fetch_sc2_http_req_cnt, acl_match_int, ACL_USE_NOTHING },
3178 { "src_http_req_cnt", acl_parse_int, acl_fetch_src_http_req_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3179 { "sc1_http_req_rate", acl_parse_int, acl_fetch_sc1_http_req_rate, acl_match_int, ACL_USE_NOTHING },
3180 { "sc2_http_req_rate", acl_parse_int, acl_fetch_sc2_http_req_rate, acl_match_int, ACL_USE_NOTHING },
3181 { "src_http_req_rate", acl_parse_int, acl_fetch_src_http_req_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3182 { "sc1_http_err_cnt", acl_parse_int, acl_fetch_sc1_http_err_cnt, acl_match_int, ACL_USE_NOTHING },
3183 { "sc2_http_err_cnt", acl_parse_int, acl_fetch_sc2_http_err_cnt, acl_match_int, ACL_USE_NOTHING },
3184 { "src_http_err_cnt", acl_parse_int, acl_fetch_src_http_err_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
3185 { "sc1_http_err_rate", acl_parse_int, acl_fetch_sc1_http_err_rate, acl_match_int, ACL_USE_NOTHING },
3186 { "sc2_http_err_rate", acl_parse_int, acl_fetch_sc2_http_err_rate, acl_match_int, ACL_USE_NOTHING },
3187 { "src_http_err_rate", acl_parse_int, acl_fetch_src_http_err_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3188 { "sc1_kbytes_in", acl_parse_int, acl_fetch_sc1_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3189 { "sc2_kbytes_in", acl_parse_int, acl_fetch_sc2_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3190 { "src_kbytes_in", acl_parse_int, acl_fetch_src_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE },
3191 { "sc1_bytes_in_rate", acl_parse_int, acl_fetch_sc1_bytes_in_rate, acl_match_int, ACL_USE_NOTHING },
3192 { "sc2_bytes_in_rate", acl_parse_int, acl_fetch_sc2_bytes_in_rate, acl_match_int, ACL_USE_NOTHING },
3193 { "src_bytes_in_rate", acl_parse_int, acl_fetch_src_bytes_in_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
3194 { "sc1_kbytes_out", acl_parse_int, acl_fetch_sc1_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3195 { "sc2_kbytes_out", acl_parse_int, acl_fetch_sc2_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3196 { "src_kbytes_out", acl_parse_int, acl_fetch_src_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE },
3197 { "sc1_bytes_out_rate", acl_parse_int, acl_fetch_sc1_bytes_out_rate, acl_match_int, ACL_USE_NOTHING },
3198 { "sc2_bytes_out_rate", acl_parse_int, acl_fetch_sc2_bytes_out_rate, acl_match_int, ACL_USE_NOTHING },
3199 { "src_bytes_out_rate", acl_parse_int, acl_fetch_src_bytes_out_rate, acl_match_int, ACL_USE_TCP4_VOLATILE },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003200 { NULL, NULL, NULL, NULL },
3201}};
3202
3203
Willy Tarreau56123282010-08-06 19:06:56 +02003204/* Parse a "track-sc[12]" line starting with "track-sc[12]" in args[arg-1].
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003205 * Returns the number of warnings emitted, or -1 in case of fatal errors. The
3206 * <prm> struct is fed with the table name if any. If unspecified, the caller
3207 * will assume that the current proxy's table is used.
3208 */
3209int parse_track_counters(char **args, int *arg,
3210 int section_type, struct proxy *curpx,
3211 struct track_ctr_prm *prm,
3212 struct proxy *defpx, char *err, int errlen)
3213{
3214 int pattern_type = 0;
Willy Tarreau56123282010-08-06 19:06:56 +02003215 char *kw = args[*arg - 1];
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003216
Willy Tarreau56123282010-08-06 19:06:56 +02003217 /* parse the arguments of "track-sc[12]" before the condition in the
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003218 * following form :
Willy Tarreau56123282010-08-06 19:06:56 +02003219 * track-sc[12] src [ table xxx ] [ if|unless ... ]
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003220 */
3221 while (args[*arg]) {
3222 if (strcmp(args[*arg], "src") == 0) {
3223 prm->type = STKTABLE_TYPE_IP;
3224 pattern_type = 1;
3225 }
3226 else if (strcmp(args[*arg], "table") == 0) {
3227 if (!args[*arg + 1]) {
3228 snprintf(err, errlen,
Willy Tarreau56123282010-08-06 19:06:56 +02003229 "missing table for %s in %s '%s'.",
3230 kw, proxy_type_str(curpx), curpx->id);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003231 return -1;
3232 }
3233 /* we copy the table name for now, it will be resolved later */
3234 prm->table.n = strdup(args[*arg + 1]);
3235 (*arg)++;
3236 }
3237 else {
3238 /* unhandled keywords are handled by the caller */
3239 break;
3240 }
3241 (*arg)++;
3242 }
3243
3244 if (!pattern_type) {
3245 snprintf(err, errlen,
Willy Tarreau56123282010-08-06 19:06:56 +02003246 "%s key not specified in %s '%s' (found %s, only 'src' is supported).",
3247 kw, proxy_type_str(curpx), curpx->id, quote_arg(args[*arg]));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003248 return -1;
3249 }
3250
3251 return 0;
3252}
3253
Willy Tarreau8b22a712010-06-18 17:46:06 +02003254__attribute__((constructor))
3255static void __session_init(void)
3256{
3257 acl_register_keywords(&acl_kws);
3258}
3259
Willy Tarreaubaaee002006-06-26 02:48:02 +02003260/*
3261 * Local variables:
3262 * c-indent-level: 8
3263 * c-basic-offset: 8
3264 * End:
3265 */