blob: 6e098e2603e90fdb8fea1d09246713ff71d7813a [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 Tarreaud28c3532012-04-19 19:28:33 +02004 * Copyright 2000-2012 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 Tarreau9b28e032012-10-12 23:49:43 +020018#include <common/buffer.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020019#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020020#include <common/memory.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
Willy Tarreaubaaee002006-06-26 02:48:02 +020022#include <types/capture.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010023#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020024
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020025#include <proto/acl.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020026#include <proto/arg.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010027#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020028#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010029#include <proto/checks.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020030#include <proto/connection.h>
Willy Tarreau5ca791d2009-08-16 19:06:42 +020031#include <proto/dumpstats.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020032#include <proto/fd.h>
Willy Tarreau91c43d72010-06-20 11:19:22 +020033#include <proto/freq_ctr.h>
Willy Tarreau3041b9f2010-10-15 23:25:20 +020034#include <proto/frontend.h>
Willy Tarreau8d5d7f22007-01-21 19:16:41 +010035#include <proto/hdr_idx.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020036#include <proto/listener.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020037#include <proto/log.h>
Willy Tarreaucbaaec42012-09-06 11:32:07 +020038#include <proto/raw_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039#include <proto/session.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010040#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010041#include <proto/proto_http.h>
42#include <proto/proto_tcp.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020043#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010045#include <proto/server.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020046#include <proto/sample.h>
Emeric Brun1d33b292010-01-04 15:47:17 +010047#include <proto/stick_table.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010048#include <proto/stream_interface.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010049#include <proto/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020050
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020051struct pool_head *pool2_session;
Willy Tarreauf54f8bd2008-11-23 19:53:55 +010052struct list sessions;
Willy Tarreaubaaee002006-06-26 02:48:02 +020053
Willy Tarreau071e1372012-10-03 01:39:48 +020054static int conn_session_complete(struct connection *conn);
Willy Tarreau9683e9a2012-10-03 21:17:23 +020055static int conn_session_update(struct connection *conn);
Willy Tarreau2542b532012-08-31 16:01:23 +020056static struct task *expire_mini_session(struct task *t);
57int session_complete(struct session *s);
58
Willy Tarreau5e75e272012-10-02 21:21:20 +020059/* data layer callbacks for an embryonic session */
60struct data_cb sess_conn_cb = {
61 .recv = NULL,
62 .send = NULL,
Willy Tarreau9683e9a2012-10-03 21:17:23 +020063 .wake = conn_session_update,
Willy Tarreau071e1372012-10-03 01:39:48 +020064 .init = conn_session_complete,
Willy Tarreau5e75e272012-10-02 21:21:20 +020065};
66
Willy Tarreau2542b532012-08-31 16:01:23 +020067/* This function is called from the protocol layer accept() in order to
68 * instanciate a new embryonic session on behalf of a given listener and
69 * frontend. It returns a positive value upon success, 0 if the connection
70 * can be ignored, or a negative value upon critical failure. The accepted
71 * file descriptor is closed if we return <= 0.
Willy Tarreau81f9aa32010-06-01 17:45:26 +020072 */
73int session_accept(struct listener *l, int cfd, struct sockaddr_storage *addr)
74{
75 struct proxy *p = l->frontend;
76 struct session *s;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020077 struct task *t;
Willy Tarreauabe8ea52010-11-11 10:56:04 +010078 int ret;
79
80
81 ret = -1; /* assume unrecoverable error by default */
Willy Tarreau81f9aa32010-06-01 17:45:26 +020082
Willy Tarreaufffe1322010-11-11 09:48:16 +010083 if (unlikely((s = pool_alloc2(pool2_session)) == NULL))
Willy Tarreau81f9aa32010-06-01 17:45:26 +020084 goto out_close;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020085
Willy Tarreauf2943dc2012-10-26 20:10:28 +020086 if (unlikely((s->si[0].conn = pool_alloc2(pool2_connection)) == NULL))
87 goto out_fail_conn0;
88
89 if (unlikely((s->si[1].conn = pool_alloc2(pool2_connection)) == NULL))
90 goto out_fail_conn1;
91
Willy Tarreau2542b532012-08-31 16:01:23 +020092 /* minimum session initialization required for an embryonic session is
93 * fairly low. We need very little to execute L4 ACLs, then we need a
94 * task to make the client-side connection live on its own.
95 * - flags
96 * - stick-entry tracking
97 */
Willy Tarreau81f9aa32010-06-01 17:45:26 +020098 s->flags = 0;
99 s->logs.logwait = p->to_log;
Willy Tarreau56123282010-08-06 19:06:56 +0200100 s->stkctr1_entry = NULL;
101 s->stkctr2_entry = NULL;
102 s->stkctr1_table = NULL;
103 s->stkctr2_table = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200104
Willy Tarreau2542b532012-08-31 16:01:23 +0200105 s->listener = l;
106 s->fe = p;
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100107
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200108 /* OK, we're keeping the session, so let's properly initialize the session */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200109 s->si[0].conn->t.sock.fd = cfd;
110 s->si[0].conn->ctrl = l->proto;
111 s->si[0].conn->flags = CO_FL_NONE;
Willy Tarreau14cba4b2012-11-30 17:33:05 +0100112 s->si[0].conn->err_code = CO_ER_NONE;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200113 s->si[0].conn->addr.from = *addr;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100114 s->si[0].conn->target = &l->obj_type;
Willy Tarreau2542b532012-08-31 16:01:23 +0200115
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200116 s->logs.accept_date = date; /* user-visible date for logging */
117 s->logs.tv_accept = now; /* corrected date for internal use */
118 s->uniq_id = totalconn;
Willy Tarreau2542b532012-08-31 16:01:23 +0200119 p->feconn++;
120 /* This session was accepted, count it now */
121 if (p->feconn > p->fe_counters.conn_max)
122 p->fe_counters.conn_max = p->feconn;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200123
Willy Tarreau2542b532012-08-31 16:01:23 +0200124 proxy_inc_fe_conn_ctr(l, p);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200125
126 /* now evaluate the tcp-request layer4 rules. Since we expect to be able
127 * to abort right here as soon as possible, we check the rules before
128 * even initializing the stream interfaces.
129 */
130 if ((l->options & LI_O_TCP_RULES) && !tcp_exec_req_rules(s)) {
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200131 /* let's do a no-linger now to close with a single RST. */
132 setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100133 ret = 0; /* successful termination */
Willy Tarreau2542b532012-08-31 16:01:23 +0200134 goto out_free_session;
135 }
136
Willy Tarreau1bc4aab2012-10-08 20:11:03 +0200137#ifndef USE_ACCEPT4
138 /* Adjust some socket options if the connection was accepted by a plain
139 * accept() syscall.
140 */
Willy Tarreau82569f92012-09-27 23:48:56 +0200141 if (unlikely(fcntl(cfd, F_SETFL, O_NONBLOCK) == -1))
142 goto out_free_session;
Willy Tarreau1bc4aab2012-10-08 20:11:03 +0200143#endif
Willy Tarreau82569f92012-09-27 23:48:56 +0200144
145 /* monitor-net and health mode are processed immediately after TCP
146 * connection rules. This way it's possible to block them, but they
147 * never use the lower data layers, they send directly over the socket,
148 * as they were designed for. We first flush the socket receive buffer
149 * in order to avoid emission of an RST by the system. We ignore any
150 * error.
151 */
152 if (unlikely((p->mode == PR_MODE_HEALTH) ||
153 ((l->options & LI_O_CHK_MONNET) &&
154 addr->ss_family == AF_INET &&
155 (((struct sockaddr_in *)addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr))) {
156 /* we have 4 possibilities here :
157 * - HTTP mode, from monitoring address => send "HTTP/1.0 200 OK"
158 * - HEALTH mode with HTTP check => send "HTTP/1.0 200 OK"
159 * - HEALTH mode without HTTP check => just send "OK"
160 * - TCP mode from monitoring address => just close
161 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100162 recv(cfd, trash.str, trash.size, MSG_DONTWAIT);
Willy Tarreau82569f92012-09-27 23:48:56 +0200163 if (p->mode == PR_MODE_HTTP ||
164 (p->mode == PR_MODE_HEALTH && (p->options2 & PR_O2_CHK_ANY) == PR_O2_HTTP_CHK))
165 send(cfd, "HTTP/1.0 200 OK\r\n\r\n", 19, MSG_DONTWAIT|MSG_NOSIGNAL|MSG_MORE);
166 else if (p->mode == PR_MODE_HEALTH)
167 send(cfd, "OK\n", 3, MSG_DONTWAIT|MSG_NOSIGNAL|MSG_MORE);
168 ret = 0;
169 goto out_free_session;
170 }
171
172
Willy Tarreau22cda212012-08-31 17:43:29 +0200173 /* wait for a PROXY protocol header */
174 if (l->options & LI_O_ACC_PROXY) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200175 s->si[0].conn->flags |= CO_FL_ACCEPT_PROXY;
176 conn_sock_want_recv(s->si[0].conn);
Willy Tarreau22cda212012-08-31 17:43:29 +0200177 }
178
Willy Tarreau2542b532012-08-31 16:01:23 +0200179 if (unlikely((t = task_new()) == NULL))
180 goto out_free_session;
181
182 t->context = s;
183 t->nice = l->nice;
184 s->task = t;
185
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200186 /* Add the various callbacks. Right now the transport layer is present
187 * but not initialized. Also note we need to be careful as the stream
188 * int is not initialized yet.
Willy Tarreau2542b532012-08-31 16:01:23 +0200189 */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200190 conn_prepare(s->si[0].conn, &sess_conn_cb, l->proto, l->xprt, s);
Willy Tarreau2542b532012-08-31 16:01:23 +0200191
192 /* finish initialization of the accepted file descriptor */
193 fd_insert(cfd);
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200194 fdtab[cfd].owner = s->si[0].conn;
Willy Tarreau2542b532012-08-31 16:01:23 +0200195 fdtab[cfd].iocb = conn_fd_handler;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200196 conn_data_want_recv(s->si[0].conn);
197 if (conn_xprt_init(s->si[0].conn) < 0)
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100198 goto out_free_task;
Willy Tarreau2542b532012-08-31 16:01:23 +0200199
200 /* OK, now either we have a pending handshake to execute with and
201 * then we must return to the I/O layer, or we can proceed with the
202 * end of the session initialization. In case of handshake, we also
203 * set the I/O timeout to the frontend's client timeout.
204 */
205
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200206 if (s->si[0].conn->flags & CO_FL_HANDSHAKE) {
Willy Tarreau2542b532012-08-31 16:01:23 +0200207 t->process = expire_mini_session;
208 t->expire = tick_add_ifset(now_ms, p->timeout.client);
209 task_queue(t);
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200210 s->si[0].conn->flags |= CO_FL_INIT_DATA | CO_FL_WAKE_DATA;
Willy Tarreau2542b532012-08-31 16:01:23 +0200211 return 1;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200212 }
213
Willy Tarreau815f5ec2012-11-06 00:14:25 +0100214 /* OK let's complete session initialization since there is no handshake */
215 s->si[0].conn->flags |= CO_FL_CONNECTED;
Willy Tarreau2542b532012-08-31 16:01:23 +0200216 ret = session_complete(s);
217 if (ret > 0)
218 return ret;
219
220 /* Error unrolling */
221 out_free_task:
222 task_free(t);
223 out_free_session:
224 p->feconn--;
225 if (s->stkctr1_entry || s->stkctr2_entry)
226 session_store_counters(s);
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200227 pool_free2(pool2_connection, s->si[1].conn);
228 out_fail_conn1:
229 pool_free2(pool2_connection, s->si[0].conn);
230 out_fail_conn0:
Willy Tarreau2542b532012-08-31 16:01:23 +0200231 pool_free2(pool2_session, s);
232 out_close:
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200233 if (ret < 0 && l->xprt == &raw_sock && p->mode == PR_MODE_HTTP) {
Willy Tarreau2542b532012-08-31 16:01:23 +0200234 /* critical error, no more memory, try to emit a 500 response */
Willy Tarreau783f2582012-09-04 12:19:04 +0200235 struct chunk *err_msg = http_error_message(s, HTTP_ERR_500);
Willy Tarreau2542b532012-08-31 16:01:23 +0200236 send(cfd, err_msg->str, err_msg->len, MSG_DONTWAIT|MSG_NOSIGNAL);
237 }
238
239 if (fdtab[cfd].owner)
240 fd_delete(cfd);
241 else
242 close(cfd);
243 return ret;
244}
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100245
Willy Tarreau0af29122012-12-03 15:35:00 +0100246
247/* prepare the trash with a log prefix for session <s> */
248static void prepare_mini_sess_log_prefix(struct session *s)
249{
250 struct tm tm;
251 char pn[INET6_ADDRSTRLEN];
252 int ret;
253 char *end;
254
255 ret = addr_to_str(&s->si[0].conn->addr.from, pn, sizeof(pn));
256 if (ret <= 0)
257 chunk_printf(&trash, "unknown [");
258 else if (ret == AF_UNIX)
259 chunk_printf(&trash, "%s:%d [", pn, s->listener->luid);
260 else
261 chunk_printf(&trash, "%s:%d [", pn, get_host_port(&s->si[0].conn->addr.from));
262
263 get_localtime(s->logs.accept_date.tv_sec, &tm);
264 end = date2str_log(trash.str + trash.len, &tm, &(s->logs.accept_date), trash.size - trash.len);
265 trash.len = end - trash.str;
266 if (s->listener->name)
267 chunk_appendf(&trash, "] %s/%s", s->fe->id, s->listener->name);
268 else
269 chunk_appendf(&trash, "] %s/%d", s->fe->id, s->listener->luid);
270}
271
Willy Tarreau2542b532012-08-31 16:01:23 +0200272/* This function kills an existing embryonic session. It stops the connection's
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200273 * transport layer, releases assigned resources, resumes the listener if it was
Willy Tarreau2542b532012-08-31 16:01:23 +0200274 * disabled and finally kills the file descriptor.
275 */
276static void kill_mini_session(struct session *s)
277{
Willy Tarreau0af29122012-12-03 15:35:00 +0100278 int level = LOG_INFO;
279 struct connection *conn = s->si[0].conn;
280 unsigned int log = s->logs.logwait;
281 const char *err_msg;
282
283 if (s->fe->options2 & PR_O2_LOGERRORS)
284 level = LOG_ERR;
285
286 if (log && (s->fe->options & PR_O_NULLNOLOG)) {
287 /* with "option dontlognull", we don't log connections with no transfer */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100288 if (!conn->err_code ||
Willy Tarreau20879a02012-12-03 16:32:10 +0100289 conn->err_code == CO_ER_PRX_EMPTY || conn->err_code == CO_ER_PRX_ABORT ||
290 conn->err_code == CO_ER_SSL_EMPTY || conn->err_code == CO_ER_SSL_ABORT)
Willy Tarreau0af29122012-12-03 15:35:00 +0100291 log = 0;
292 }
293
294 if (log) {
295 if (!conn->err_code && (s->task->state & TASK_WOKEN_TIMER)) {
296 if (conn->flags & CO_FL_ACCEPT_PROXY)
297 conn->err_code = CO_ER_PRX_TIMEOUT;
298 else if (conn->flags & CO_FL_SSL_WAIT_HS)
299 conn->err_code = CO_ER_SSL_TIMEOUT;
300 }
301
302 prepare_mini_sess_log_prefix(s);
303 err_msg = conn_err_code_str(conn);
304 if (err_msg)
305 send_log(s->fe, level, "%s: %s\n", trash.str, err_msg);
306 else
307 send_log(s->fe, level, "%s: unknown connection error (code=%d flags=%08x)\n",
308 trash.str, conn->err_code, conn->flags);
309 }
310
Willy Tarreau2542b532012-08-31 16:01:23 +0200311 /* kill the connection now */
Willy Tarreau2b199c92012-11-23 17:32:21 +0100312 conn_full_close(s->si[0].conn);
Willy Tarreau2542b532012-08-31 16:01:23 +0200313
314 s->fe->feconn--;
315 if (s->stkctr1_entry || s->stkctr2_entry)
316 session_store_counters(s);
317
318 if (!(s->listener->options & LI_O_UNLIMITED))
319 actconn--;
320 jobs--;
321 s->listener->nbconn--;
322 if (s->listener->state == LI_FULL)
323 resume_listener(s->listener);
324
325 /* Dequeues all of the listeners waiting for a resource */
326 if (!LIST_ISEMPTY(&global_listener_queue))
327 dequeue_all_listeners(&global_listener_queue);
328
329 if (!LIST_ISEMPTY(&s->fe->listener_queue) &&
330 (!s->fe->fe_sps_lim || freq_ctr_remain(&s->fe->fe_sess_per_sec, s->fe->fe_sps_lim, 0) > 0))
331 dequeue_all_listeners(&s->fe->listener_queue);
332
333 task_delete(s->task);
334 task_free(s->task);
335
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200336 pool_free2(pool2_connection, s->si[1].conn);
337 pool_free2(pool2_connection, s->si[0].conn);
Willy Tarreau2542b532012-08-31 16:01:23 +0200338 pool_free2(pool2_session, s);
339}
340
Willy Tarreau22cda212012-08-31 17:43:29 +0200341/* Finish initializing a session from a connection, or kills it if the
342 * connection shows and error. Returns <0 if the connection was killed.
Willy Tarreau2542b532012-08-31 16:01:23 +0200343 */
Willy Tarreau071e1372012-10-03 01:39:48 +0200344static int conn_session_complete(struct connection *conn)
Willy Tarreau2542b532012-08-31 16:01:23 +0200345{
Willy Tarreau5e75e272012-10-02 21:21:20 +0200346 struct session *s = conn->owner;
Willy Tarreau2542b532012-08-31 16:01:23 +0200347
Willy Tarreau22cda212012-08-31 17:43:29 +0200348 if (!(conn->flags & CO_FL_ERROR) && (session_complete(s) > 0)) {
Willy Tarreau071e1372012-10-03 01:39:48 +0200349 conn->flags &= ~CO_FL_INIT_DATA;
Willy Tarreau2542b532012-08-31 16:01:23 +0200350 return 0;
351 }
352
353 /* kill the connection now */
354 kill_mini_session(s);
355 return -1;
356}
357
Willy Tarreau9683e9a2012-10-03 21:17:23 +0200358/* Update an embryonic session status. The connection is killed in case of
359 * error, and <0 will be returned. Otherwise it does nothing.
360 */
361static int conn_session_update(struct connection *conn)
362{
363 if (conn->flags & CO_FL_ERROR) {
364 kill_mini_session(conn->owner);
365 return -1;
366 }
367 return 0;
368}
369
Willy Tarreau2542b532012-08-31 16:01:23 +0200370/* Manages embryonic sessions timeout. It is only called when the timeout
371 * strikes and performs the required cleanup.
372 */
373static struct task *expire_mini_session(struct task *t)
374{
375 struct session *s = t->context;
376
377 if (!(t->state & TASK_WOKEN_TIMER))
378 return t;
379
380 kill_mini_session(s);
381 return NULL;
382}
383
384/* This function is called from the I/O handler which detects the end of
385 * handshake, in order to complete initialization of a valid session. It must
386 * be called with an embryonic session. It returns a positive value upon
387 * success, 0 if the connection can be ignored, or a negative value upon
388 * critical failure. The accepted file descriptor is closed if we return <= 0.
389 */
390int session_complete(struct session *s)
391{
392 struct listener *l = s->listener;
393 struct proxy *p = s->fe;
394 struct http_txn *txn;
395 struct task *t = s->task;
396 int ret;
397
398 ret = -1; /* assume unrecoverable error by default */
399
400 /* OK, we're keeping the session, so let's properly initialize the session */
401 LIST_ADDQ(&sessions, &s->list);
402 LIST_INIT(&s->back_refs);
Willy Tarreau5e75e272012-10-02 21:21:20 +0200403 si_takeover_conn(&s->si[0], l->proto, l->xprt);
Willy Tarreau2542b532012-08-31 16:01:23 +0200404 s->flags |= SN_INITIALIZED;
405
406 s->unique_id = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200407
408 t->process = l->handler;
409 t->context = s;
410 t->expire = TICK_ETERNITY;
411
412 /* Note: initially, the session's backend points to the frontend.
413 * This changes later when switching rules are executed or
414 * when the default backend is assigned.
415 */
416 s->be = s->fe;
417 s->req = s->rep = NULL; /* will be allocated later */
William Lallemand82fe75c2012-10-23 10:25:10 +0200418 s->comp_algo = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200419
420 /* Let's count a session now */
Willy Tarreaub36b4242010-06-04 20:59:39 +0200421 proxy_inc_fe_sess_ctr(l, p);
Willy Tarreau56123282010-08-06 19:06:56 +0200422 if (s->stkctr1_entry) {
Willy Tarreau91c43d72010-06-20 11:19:22 +0200423 void *ptr;
424
Willy Tarreau56123282010-08-06 19:06:56 +0200425 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200426 if (ptr)
427 stktable_data_cast(ptr, sess_cnt)++;
Willy Tarreau91c43d72010-06-20 11:19:22 +0200428
Willy Tarreau56123282010-08-06 19:06:56 +0200429 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +0200430 if (ptr)
431 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200432 s->stkctr1_table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200433 }
Willy Tarreaub36b4242010-06-04 20:59:39 +0200434
Willy Tarreau56123282010-08-06 19:06:56 +0200435 if (s->stkctr2_entry) {
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200436 void *ptr;
437
Willy Tarreau56123282010-08-06 19:06:56 +0200438 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_SESS_CNT);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200439 if (ptr)
440 stktable_data_cast(ptr, sess_cnt)++;
441
Willy Tarreau56123282010-08-06 19:06:56 +0200442 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_SESS_RATE);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200443 if (ptr)
444 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200445 s->stkctr2_table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200446 }
447
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200448 /* this part should be common with other protocols */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200449 s->si[0].owner = t;
450 s->si[0].state = s->si[0].prev_state = SI_ST_EST;
451 s->si[0].err_type = SI_ET_NONE;
452 s->si[0].err_loc = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200453 s->si[0].release = NULL;
Willy Tarreau63e7fe32012-05-08 15:20:43 +0200454 s->si[0].send_proxy_ofs = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200455 s->si[0].exp = TICK_ETERNITY;
456 s->si[0].flags = SI_FL_NONE;
457
458 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
459 s->si[0].flags |= SI_FL_INDEP_STR;
460
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200461 /* pre-initialize the other side's stream interface to an INIT state. The
462 * callbacks will be initialized before attempting to connect.
463 */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200464 s->si[1].conn->t.sock.fd = -1; /* just to help with debugging */
465 s->si[1].conn->flags = CO_FL_NONE;
Willy Tarreau14cba4b2012-11-30 17:33:05 +0100466 s->si[1].conn->err_code = CO_ER_NONE;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200467 s->si[1].owner = t;
468 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
469 s->si[1].err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +0200470 s->si[1].conn_retries = 0; /* used for logging too */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200471 s->si[1].err_loc = NULL;
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200472 s->si[1].release = NULL;
Willy Tarreau63e7fe32012-05-08 15:20:43 +0200473 s->si[1].send_proxy_ofs = 0;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100474 s->si[1].conn->target = NULL;
Willy Tarreauc5788912012-08-24 18:12:41 +0200475 si_prepare_embedded(&s->si[1]);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200476 s->si[1].exp = TICK_ETERNITY;
477 s->si[1].flags = SI_FL_NONE;
478
479 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
480 s->si[1].flags |= SI_FL_INDEP_STR;
481
Willy Tarreau9bd0d742011-07-20 00:17:39 +0200482 session_init_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100483 s->target = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200484 s->pend_pos = NULL;
485
486 /* init store persistence */
487 s->store_count = 0;
488
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200489 if (unlikely((s->req = pool_alloc2(pool2_channel)) == NULL))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200490 goto out_free_task; /* no memory */
491
Willy Tarreau9b28e032012-10-12 23:49:43 +0200492 if (unlikely((s->req->buf = pool_alloc2(pool2_buffer)) == NULL))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200493 goto out_free_req; /* no memory */
494
Willy Tarreau9b28e032012-10-12 23:49:43 +0200495 if (unlikely((s->rep = pool_alloc2(pool2_channel)) == NULL))
496 goto out_free_req_buf; /* no memory */
497
498 if (unlikely((s->rep->buf = pool_alloc2(pool2_buffer)) == NULL))
499 goto out_free_rep; /* no memory */
500
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200501 /* initialize the request buffer */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200502 s->req->buf->size = global.tune.bufsize;
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200503 channel_init(s->req);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200504 s->req->prod = &s->si[0];
505 s->req->cons = &s->si[1];
506 s->si[0].ib = s->si[1].ob = s->req;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200507 s->req->flags |= CF_READ_ATTACHED; /* the producer is already connected */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200508
509 /* activate default analysers enabled for this listener */
510 s->req->analysers = l->analysers;
511
512 s->req->wto = TICK_ETERNITY;
513 s->req->rto = TICK_ETERNITY;
514 s->req->rex = TICK_ETERNITY;
515 s->req->wex = TICK_ETERNITY;
516 s->req->analyse_exp = TICK_ETERNITY;
517
518 /* initialize response buffer */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200519 s->rep->buf->size = global.tune.bufsize;
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200520 channel_init(s->rep);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200521 s->rep->prod = &s->si[1];
522 s->rep->cons = &s->si[0];
523 s->si[0].ob = s->si[1].ib = s->rep;
524 s->rep->analysers = 0;
525
Willy Tarreau96e31212011-05-30 18:10:30 +0200526 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200527 s->req->flags |= CF_NEVER_WAIT;
528 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +0200529 }
530
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200531 s->rep->rto = TICK_ETERNITY;
532 s->rep->wto = TICK_ETERNITY;
533 s->rep->rex = TICK_ETERNITY;
534 s->rep->wex = TICK_ETERNITY;
535 s->rep->analyse_exp = TICK_ETERNITY;
536
Willy Tarreau62f791e2012-03-09 11:32:30 +0100537 txn = &s->txn;
538 /* Those variables will be checked and freed if non-NULL in
539 * session.c:session_free(). It is important that they are
540 * properly initialized.
541 */
542 txn->sessid = NULL;
543 txn->srv_cookie = NULL;
544 txn->cli_cookie = NULL;
545 txn->uri = NULL;
546 txn->req.cap = NULL;
547 txn->rsp.cap = NULL;
548 txn->hdr_idx.v = NULL;
549 txn->hdr_idx.size = txn->hdr_idx.used = 0;
550 txn->req.flags = 0;
551 txn->rsp.flags = 0;
552 /* the HTTP messages need to know what buffer they're associated with */
Willy Tarreau394db372012-10-12 22:40:39 +0200553 txn->req.chn = s->req;
554 txn->rsp.chn = s->rep;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100555
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200556 /* finish initialization of the accepted file descriptor */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200557 conn_data_want_recv(s->si[0].conn);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200558
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100559 if (p->accept && (ret = p->accept(s)) <= 0) {
560 /* Either we had an unrecoverable error (<0) or work is
561 * finished (=0, eg: monitoring), in both situations,
562 * we can release everything and close.
563 */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200564 goto out_free_rep_buf;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200565 }
566
Willy Tarreau93dbc2b2012-10-12 18:01:49 +0200567 /* if logs require transport layer information, note it on the connection */
568 if (s->logs.logwait & LW_XPRT)
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200569 s->si[0].conn->flags |= CO_FL_XPRT_TRACKED;
Willy Tarreau93dbc2b2012-10-12 18:01:49 +0200570
Willy Tarreau2542b532012-08-31 16:01:23 +0200571 /* we want the connection handler to notify the stream interface about updates. */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200572 s->si[0].conn->flags |= CO_FL_WAKE_DATA;
Willy Tarreau2542b532012-08-31 16:01:23 +0200573
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200574 /* it is important not to call the wakeup function directly but to
575 * pass through task_wakeup(), because this one knows how to apply
576 * priorities to tasks.
577 */
578 task_wakeup(t, TASK_WOKEN_INIT);
579 return 1;
580
581 /* Error unrolling */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200582 out_free_rep_buf:
583 pool_free2(pool2_buffer, s->rep->buf);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200584 out_free_rep:
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200585 pool_free2(pool2_channel, s->rep);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200586 out_free_req_buf:
587 pool_free2(pool2_buffer, s->req->buf);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200588 out_free_req:
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200589 pool_free2(pool2_channel, s->req);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200590 out_free_task:
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100591 return ret;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200592}
593
Willy Tarreaubaaee002006-06-26 02:48:02 +0200594/*
595 * frees the context associated to a session. It must have been removed first.
596 */
Simon Hormandec5be42011-06-08 09:19:07 +0900597static void session_free(struct session *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200598{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +0100599 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +0200600 struct proxy *fe = s->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100601 struct bref *bref, *back;
Willy Tarreaua4cda672010-06-06 18:28:49 +0200602 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100603
Willy Tarreaubaaee002006-06-26 02:48:02 +0200604 if (s->pend_pos)
605 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100606
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100607 if (objt_server(s->target)) { /* there may be requests left pending in queue */
Willy Tarreau1e62de62008-11-11 20:20:02 +0100608 if (s->flags & SN_CURR_SESS) {
609 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100610 objt_server(s->target)->cur_sess--;
Willy Tarreau1e62de62008-11-11 20:20:02 +0100611 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100612 if (may_dequeue_tasks(objt_server(s->target), s->be))
613 process_srv_queue(objt_server(s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100614 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100615
Willy Tarreau7c669d72008-06-20 15:04:11 +0200616 if (unlikely(s->srv_conn)) {
617 /* the session still has a reserved slot on a server, but
618 * it should normally be only the same as the one above,
619 * so this should not happen in fact.
620 */
621 sess_change_server(s, NULL);
622 }
623
Willy Tarreau543db622012-11-15 16:41:22 +0100624 if (s->flags & SN_COMP_READY)
William Lallemand1c2d6222012-10-30 15:52:53 +0100625 s->comp_algo->end(&s->comp_ctx);
Willy Tarreau543db622012-11-15 16:41:22 +0100626 s->comp_algo = NULL;
627 s->flags &= ~SN_COMP_READY;
William Lallemand82fe75c2012-10-23 10:25:10 +0200628
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100629 if (s->req->pipe)
630 put_pipe(s->req->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100631
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100632 if (s->rep->pipe)
633 put_pipe(s->rep->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100634
Willy Tarreau9b28e032012-10-12 23:49:43 +0200635 pool_free2(pool2_buffer, s->req->buf);
636 pool_free2(pool2_buffer, s->rep->buf);
637
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200638 pool_free2(pool2_channel, s->req);
639 pool_free2(pool2_channel, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200640
Willy Tarreau46023632010-01-07 22:51:47 +0100641 http_end_txn(s);
642
Willy Tarreau1e954912012-10-12 17:50:05 +0200643 /* ensure the client-side transport layer is destroyed */
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200644 s->si[0].conn->flags &= ~CO_FL_XPRT_TRACKED;
Willy Tarreau0ede5a32012-12-08 08:44:02 +0100645 conn_full_close(s->si[0].conn);
Willy Tarreau1e954912012-10-12 17:50:05 +0200646
Willy Tarreaua4cda672010-06-06 18:28:49 +0200647 for (i = 0; i < s->store_count; i++) {
648 if (!s->store[i].ts)
649 continue;
650 stksess_free(s->store[i].table, s->store[i].ts);
651 s->store[i].ts = NULL;
652 }
653
Willy Tarreau34eb6712011-10-24 18:15:04 +0200654 pool_free2(pool2_hdr_idx, txn->hdr_idx.v);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200655 if (fe) {
Willy Tarreau46023632010-01-07 22:51:47 +0100656 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
657 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200658 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100659
Willy Tarreau56123282010-08-06 19:06:56 +0200660 if (s->stkctr1_entry || s->stkctr2_entry)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200661 session_store_counters(s);
662
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100663 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100664 /* we have to unlink all watchers. We must not relink them if
665 * this session was the last one in the list.
666 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100667 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100668 LIST_INIT(&bref->users);
669 if (s->list.n != &sessions)
670 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100671 bref->ref = s->list.n;
672 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100673 LIST_DEL(&s->list);
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200674 pool_free2(pool2_connection, s->si[1].conn);
675 pool_free2(pool2_connection, s->si[0].conn);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200676 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200677
678 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200679 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +0200680 pool_flush2(pool2_buffer);
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200681 pool_flush2(pool2_channel);
Willy Tarreau34eb6712011-10-24 18:15:04 +0200682 pool_flush2(pool2_hdr_idx);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200683 pool_flush2(pool2_requri);
684 pool_flush2(pool2_capture);
685 pool_flush2(pool2_session);
686 pool_flush2(fe->req_cap_pool);
687 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200688 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200689}
690
691
692/* perform minimal intializations, report 0 in case of error, 1 if OK. */
693int init_session()
694{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100695 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200696 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
697 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200698}
699
Willy Tarreau30e71012007-11-26 20:15:35 +0100700void session_process_counters(struct session *s)
701{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100702 unsigned long long bytes;
703
Willy Tarreau30e71012007-11-26 20:15:35 +0100704 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100705 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100706 s->logs.bytes_in = s->req->total;
707 if (bytes) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100708 s->fe->fe_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100709
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100710 s->be->be_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100711
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100712 if (objt_server(s->target))
713 objt_server(s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200714
715 if (s->listener->counters)
716 s->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200717
Willy Tarreau56123282010-08-06 19:06:56 +0200718 if (s->stkctr2_entry) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200719 void *ptr;
720
Willy Tarreau56123282010-08-06 19:06:56 +0200721 ptr = stktable_data_ptr(s->stkctr2_table,
722 s->stkctr2_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200723 STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200724 if (ptr)
725 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200726
Willy Tarreau56123282010-08-06 19:06:56 +0200727 ptr = stktable_data_ptr(s->stkctr2_table,
728 s->stkctr2_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200729 STKTABLE_DT_BYTES_IN_RATE);
730 if (ptr)
731 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200732 s->stkctr2_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200733 }
734
Willy Tarreau56123282010-08-06 19:06:56 +0200735 if (s->stkctr1_entry) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200736 void *ptr;
737
Willy Tarreau56123282010-08-06 19:06:56 +0200738 ptr = stktable_data_ptr(s->stkctr1_table,
739 s->stkctr1_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200740 STKTABLE_DT_BYTES_IN_CNT);
741 if (ptr)
742 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
743
Willy Tarreau56123282010-08-06 19:06:56 +0200744 ptr = stktable_data_ptr(s->stkctr1_table,
745 s->stkctr1_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200746 STKTABLE_DT_BYTES_IN_RATE);
747 if (ptr)
748 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200749 s->stkctr1_table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200750 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100751 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100752 }
753
Willy Tarreau30e71012007-11-26 20:15:35 +0100754 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100755 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100756 s->logs.bytes_out = s->rep->total;
757 if (bytes) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100758 s->fe->fe_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100759
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100760 s->be->be_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100761
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100762 if (objt_server(s->target))
763 objt_server(s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200764
765 if (s->listener->counters)
766 s->listener->counters->bytes_out += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200767
Willy Tarreau56123282010-08-06 19:06:56 +0200768 if (s->stkctr2_entry) {
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200769 void *ptr;
770
Willy Tarreau56123282010-08-06 19:06:56 +0200771 ptr = stktable_data_ptr(s->stkctr2_table,
772 s->stkctr2_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200773 STKTABLE_DT_BYTES_OUT_CNT);
774 if (ptr)
775 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
776
Willy Tarreau56123282010-08-06 19:06:56 +0200777 ptr = stktable_data_ptr(s->stkctr2_table,
778 s->stkctr2_entry,
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200779 STKTABLE_DT_BYTES_OUT_RATE);
780 if (ptr)
781 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200782 s->stkctr2_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200783 }
784
Willy Tarreau56123282010-08-06 19:06:56 +0200785 if (s->stkctr1_entry) {
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200786 void *ptr;
787
Willy Tarreau56123282010-08-06 19:06:56 +0200788 ptr = stktable_data_ptr(s->stkctr1_table,
789 s->stkctr1_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200790 STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200791 if (ptr)
792 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200793
Willy Tarreau56123282010-08-06 19:06:56 +0200794 ptr = stktable_data_ptr(s->stkctr1_table,
795 s->stkctr1_entry,
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200796 STKTABLE_DT_BYTES_OUT_RATE);
797 if (ptr)
798 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200799 s->stkctr1_table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200800 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100801 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100802 }
803}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200804
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100805/* This function is called with (si->state == SI_ST_CON) meaning that a
806 * connection was attempted and that the file descriptor is already allocated.
807 * We must check for establishment, error and abort. Possible output states
808 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
809 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
810 * otherwise 1.
811 */
Simon Hormandec5be42011-06-08 09:19:07 +0900812static int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100813{
Willy Tarreau7421efb2012-07-02 15:11:27 +0200814 struct channel *req = si->ob;
815 struct channel *rep = si->ib;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100816
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100817 /* If we got an error, or if nothing happened and the connection timed
818 * out, we must give up. The CER state handler will take care of retry
819 * attempts and error reports.
820 */
821 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreaue3224e82012-10-29 22:41:31 +0100822 if (unlikely(si->ob->flags & CF_WRITE_PARTIAL)) {
823 /* Some data were sent past the connection establishment,
824 * so we need to pretend we're established to log correctly
825 * and let later states handle the failure.
826 */
827 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
828 si->exp = TICK_ETERNITY;
829 si->state = SI_ST_EST;
830 si->err_type = SI_ET_DATA_ERR;
831 si->ib->flags |= CF_READ_ERROR | CF_WRITE_ERROR;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100832 si->err_loc = objt_server(s->target);
Willy Tarreaue3224e82012-10-29 22:41:31 +0100833 return 1;
834 }
Willy Tarreau127334e2009-03-28 10:47:26 +0100835 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100836 si->state = SI_ST_CER;
Willy Tarreau0ede5a32012-12-08 08:44:02 +0100837
838 si->conn->flags &= ~CO_FL_XPRT_TRACKED;
839 conn_full_close(si->conn);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100840
Willy Tarreau0bd05ea2010-07-02 11:18:03 +0200841 if (si->release)
842 si->release(si);
843
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100844 if (si->err_type)
845 return 0;
846
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100847 si->err_loc = objt_server(s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100848 if (si->flags & SI_FL_ERR)
849 si->err_type = SI_ET_CONN_ERR;
850 else
851 si->err_type = SI_ET_CONN_TO;
852 return 0;
853 }
854
855 /* OK, maybe we want to abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200856 if (unlikely((rep->flags & CF_SHUTW) ||
857 ((req->flags & CF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
858 ((!(req->flags & CF_WRITE_ACTIVITY) && channel_is_empty(req)) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100859 s->be->options & PR_O_ABRT_CLOSE)))) {
860 /* give up */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200861 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100862 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100863 si->err_loc = objt_server(s->target);
Willy Tarreau84455332009-03-15 22:34:05 +0100864 if (s->srv_error)
865 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100866 return 1;
867 }
868
869 /* we need to wait a bit more if there was no activity either */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200870 if (!(req->flags & CF_WRITE_ACTIVITY))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100871 return 1;
872
873 /* OK, this means that a connection succeeded. The caller will be
874 * responsible for handling the transition from CON to EST.
875 */
876 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau127334e2009-03-28 10:47:26 +0100877 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100878 si->state = SI_ST_EST;
879 si->err_type = SI_ET_NONE;
880 si->err_loc = NULL;
881 return 1;
882}
883
884/* This function is called with (si->state == SI_ST_CER) meaning that a
885 * previous connection attempt has failed and that the file descriptor
886 * has already been released. Possible causes include asynchronous error
887 * notification and time out. Possible output states are SI_ST_CLO when
888 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
889 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
890 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
891 * marked as in error state. It returns 0.
892 */
Simon Hormandec5be42011-06-08 09:19:07 +0900893static int sess_update_st_cer(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100894{
895 /* we probably have to release last session from the server */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100896 if (objt_server(s->target)) {
897 health_adjust(objt_server(s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100898
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100899 if (s->flags & SN_CURR_SESS) {
900 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100901 objt_server(s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100902 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100903 }
904
905 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200906 si->conn_retries--;
907 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100908 if (!si->err_type) {
909 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100910 si->err_loc = objt_server(s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100911 }
912
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100913 if (objt_server(s->target))
914 objt_server(s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100915 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100916 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100917 if (may_dequeue_tasks(objt_server(s->target), s->be))
918 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100919
920 /* shutw is enough so stop a connecting socket */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200921 si_shutw(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200922 si->ob->flags |= CF_WRITE_ERROR;
923 si->ib->flags |= CF_READ_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100924
925 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100926 if (s->srv_error)
927 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100928 return 0;
929 }
930
931 /* If the "redispatch" option is set on the backend, we are allowed to
932 * retry on another server for the last retry. In order to achieve this,
933 * we must mark the session unassigned, and eventually clear the DIRECT
934 * bit to ignore any persistence cookie. We won't count a retry nor a
935 * redispatch yet, because this will depend on what server is selected.
936 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100937 if (objt_server(s->target) && si->conn_retries == 0 &&
Willy Tarreau4de91492010-01-22 19:10:05 +0100938 s->be->options & PR_O_REDISP && !(s->flags & SN_FORCE_PRST)) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100939 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100940 if (may_dequeue_tasks(objt_server(s->target), s->be))
941 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100942
943 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100944 si->state = SI_ST_REQ;
945 } else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100946 if (objt_server(s->target))
947 objt_server(s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100948 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100949 si->state = SI_ST_ASS;
950 }
951
952 if (si->flags & SI_FL_ERR) {
953 /* The error was an asynchronous connection error, and we will
954 * likely have to retry connecting to the same server, most
955 * likely leading to the same result. To avoid this, we wait
956 * one second before retrying.
957 */
958
959 if (!si->err_type)
960 si->err_type = SI_ET_CONN_ERR;
961
962 si->state = SI_ST_TAR;
963 si->exp = tick_add(now_ms, MS_TO_TICKS(1000));
964 return 0;
965 }
966 return 0;
967}
968
969/*
970 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200971 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
Willy Tarreau26d8c592012-05-07 18:12:14 +0200972 * SI_ST_INI) to SI_ST_EST, but only when a ->proto is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100973 */
Simon Hormandec5be42011-06-08 09:19:07 +0900974static void sess_establish(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100975{
Willy Tarreau7421efb2012-07-02 15:11:27 +0200976 struct channel *req = si->ob;
977 struct channel *rep = si->ib;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100978
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100979 if (objt_server(s->target))
980 health_adjust(objt_server(s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100981
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100982 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100983 /* if the user wants to log as soon as possible, without counting
984 * bytes from the server, then this is the right moment. */
985 if (s->fe->to_log && !(s->logs.logwait & LW_BYTES)) {
986 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100987 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100988 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100989 }
990 else {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100991 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
992 /* reset hdr_idx which was already initialized by the request.
993 * right now, the http parser does it.
994 * hdr_idx_init(&s->txn.hdr_idx);
995 */
996 }
997
Willy Tarreau4e5b8282009-08-16 22:57:50 +0200998 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200999 rep->flags |= CF_READ_ATTACHED; /* producer is now attached */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001000 if (si_ctrl(si)) {
Willy Tarreaud04e8582010-05-31 12:31:35 +02001001 /* real connections have timeouts */
1002 req->wto = s->be->timeout.server;
1003 rep->rto = s->be->timeout.server;
1004 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001005 req->wex = TICK_ETERNITY;
1006}
1007
1008/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
1009 * Other input states are simply ignored.
1010 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON.
1011 * Flags must have previously been updated for timeouts and other conditions.
1012 */
Simon Hormandec5be42011-06-08 09:19:07 +09001013static void sess_update_stream_int(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001014{
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001015 struct server *srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001016
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001017 DPRINTF(stderr,"[%u] %s: sess=%p rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rqh=%d rqt=%d rph=%d rpt=%d cs=%d ss=%d\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001018 now_ms, __FUNCTION__,
1019 s,
1020 s->req, s->rep,
1021 s->req->rex, s->rep->wex,
1022 s->req->flags, s->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001023 s->req->buf->i, s->req->buf->o, s->rep->buf->i, s->rep->buf->o, s->rep->cons->state, s->req->cons->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001024
1025 if (si->state == SI_ST_ASS) {
1026 /* Server assigned to connection request, we have to try to connect now */
1027 int conn_err;
1028
1029 conn_err = connect_server(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001030 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001031
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001032 if (conn_err == SN_ERR_NONE) {
1033 /* state = SI_ST_CON now */
Willy Tarreau827aee92011-03-10 16:55:02 +01001034 if (srv)
1035 srv_inc_sess_ctr(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001036 return;
1037 }
1038
1039 /* We have received a synchronous error. We might have to
1040 * abort, retry immediately or redispatch.
1041 */
1042 if (conn_err == SN_ERR_INTERNAL) {
1043 if (!si->err_type) {
1044 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau827aee92011-03-10 16:55:02 +01001045 si->err_loc = srv;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001046 }
1047
Willy Tarreau827aee92011-03-10 16:55:02 +01001048 if (srv)
1049 srv_inc_sess_ctr(srv);
1050 if (srv)
1051 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001052 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001053
1054 /* release other sessions waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +01001055 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001056 if (may_dequeue_tasks(srv, s->be))
1057 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001058
1059 /* Failed and not retryable. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001060 si_shutr(si);
1061 si_shutw(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001062 si->ob->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001063
1064 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1065
1066 /* no session was ever accounted for this server */
1067 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001068 if (s->srv_error)
1069 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001070 return;
1071 }
1072
1073 /* We are facing a retryable error, but we don't want to run a
1074 * turn-around now, as the problem is likely a source port
1075 * allocation problem, so we want to retry now.
1076 */
1077 si->state = SI_ST_CER;
1078 si->flags &= ~SI_FL_ERR;
1079 sess_update_st_cer(s, si);
1080 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
1081 return;
1082 }
1083 else if (si->state == SI_ST_QUE) {
1084 /* connection request was queued, check for any update */
1085 if (!s->pend_pos) {
1086 /* The connection is not in the queue anymore. Either
1087 * we have a server connection slot available and we
1088 * go directly to the assigned state, or we need to
1089 * load-balance first and go to the INI state.
1090 */
1091 si->exp = TICK_ETERNITY;
1092 if (unlikely(!(s->flags & SN_ASSIGNED)))
1093 si->state = SI_ST_REQ;
1094 else {
1095 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1096 si->state = SI_ST_ASS;
1097 }
1098 return;
1099 }
1100
1101 /* Connection request still in queue... */
1102 if (si->flags & SI_FL_EXP) {
1103 /* ... and timeout expired */
1104 si->exp = TICK_ETERNITY;
1105 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +01001106 if (srv)
1107 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001108 s->be->be_counters.failed_conns++;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001109 si_shutr(si);
1110 si_shutw(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001111 si->ob->flags |= CF_WRITE_TIMEOUT;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001112 if (!si->err_type)
1113 si->err_type = SI_ET_QUEUE_TO;
1114 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001115 if (s->srv_error)
1116 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001117 return;
1118 }
1119
1120 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001121 if ((si->ob->flags & (CF_READ_ERROR)) ||
1122 ((si->ob->flags & CF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreau8e21bb92012-08-24 22:40:29 +02001123 (channel_is_empty(si->ob) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001124 /* give up */
1125 si->exp = TICK_ETERNITY;
1126 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau73b013b2012-05-21 16:31:45 +02001127 si_shutr(si);
1128 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001129 si->err_type |= SI_ET_QUEUE_ABRT;
1130 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001131 if (s->srv_error)
1132 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001133 return;
1134 }
1135
1136 /* Nothing changed */
1137 return;
1138 }
1139 else if (si->state == SI_ST_TAR) {
1140 /* Connection request might be aborted */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001141 if ((si->ob->flags & (CF_READ_ERROR)) ||
1142 ((si->ob->flags & CF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreau8e21bb92012-08-24 22:40:29 +02001143 (channel_is_empty(si->ob) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001144 /* give up */
1145 si->exp = TICK_ETERNITY;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001146 si_shutr(si);
1147 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001148 si->err_type |= SI_ET_CONN_ABRT;
1149 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001150 if (s->srv_error)
1151 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001152 return;
1153 }
1154
1155 if (!(si->flags & SI_FL_EXP))
1156 return; /* still in turn-around */
1157
1158 si->exp = TICK_ETERNITY;
1159
1160 /* we keep trying on the same server as long as the session is
1161 * marked "assigned".
1162 * FIXME: Should we force a redispatch attempt when the server is down ?
1163 */
1164 if (s->flags & SN_ASSIGNED)
1165 si->state = SI_ST_ASS;
1166 else
1167 si->state = SI_ST_REQ;
1168 return;
1169 }
1170}
1171
Simon Hormandec5be42011-06-08 09:19:07 +09001172/* Set correct session termination flags in case no analyser has done it. It
1173 * also counts a failed request if the server state has not reached the request
1174 * stage.
1175 */
1176static void sess_set_term_flags(struct session *s)
1177{
1178 if (!(s->flags & SN_FINST_MASK)) {
1179 if (s->si[1].state < SI_ST_REQ) {
1180
1181 s->fe->fe_counters.failed_req++;
1182 if (s->listener->counters)
1183 s->listener->counters->failed_req++;
1184
1185 s->flags |= SN_FINST_R;
1186 }
1187 else if (s->si[1].state == SI_ST_QUE)
1188 s->flags |= SN_FINST_Q;
1189 else if (s->si[1].state < SI_ST_EST)
1190 s->flags |= SN_FINST_C;
1191 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
1192 s->flags |= SN_FINST_D;
1193 else
1194 s->flags |= SN_FINST_L;
1195 }
1196}
1197
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001198/* This function initiates a server connection request on a stream interface
1199 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS,
1200 * indicating that a server has been assigned. It may also return SI_ST_QUE,
1201 * or SI_ST_CLO upon error.
1202 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001203static void sess_prepare_conn_req(struct session *s, struct stream_interface *si)
1204{
1205 DPRINTF(stderr,"[%u] %s: sess=%p rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rqh=%d rqt=%d rph=%d rpt=%d cs=%d ss=%d\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001206 now_ms, __FUNCTION__,
1207 s,
1208 s->req, s->rep,
1209 s->req->rex, s->rep->wex,
1210 s->req->flags, s->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001211 s->req->buf->i, s->req->buf->o, s->rep->buf->i, s->rep->buf->o, s->rep->cons->state, s->req->cons->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001212
1213 if (si->state != SI_ST_REQ)
1214 return;
1215
1216 /* Try to assign a server */
1217 if (srv_redispatch_connect(s) != 0) {
1218 /* We did not get a server. Either we queued the
1219 * connection request, or we encountered an error.
1220 */
1221 if (si->state == SI_ST_QUE)
1222 return;
1223
1224 /* we did not get any server, let's check the cause */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001225 si_shutr(si);
1226 si_shutw(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001227 si->ob->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001228 if (!si->err_type)
1229 si->err_type = SI_ET_CONN_OTHER;
1230 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001231 if (s->srv_error)
1232 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001233 return;
1234 }
1235
1236 /* The server is assigned */
1237 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1238 si->state = SI_ST_ASS;
1239}
1240
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001241/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +01001242 * if appropriate. The default_backend rule is also considered, then the
1243 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001244 * It returns 1 if the processing can continue on next analysers, or zero if it
1245 * either needs more data or wants to immediately abort the request.
1246 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001247static int process_switching_rules(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001248{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001249 struct persist_rule *prst_rule;
Willy Tarreau4de91492010-01-22 19:10:05 +01001250
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001251 req->analysers &= ~an_bit;
1252 req->analyse_exp = TICK_ETERNITY;
1253
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001254 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001255 now_ms, __FUNCTION__,
1256 s,
1257 req,
1258 req->rex, req->wex,
1259 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001260 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001261 req->analysers);
1262
1263 /* now check whether we have some switching rules for this request */
1264 if (!(s->flags & SN_BE_ASSIGNED)) {
1265 struct switching_rule *rule;
1266
1267 list_for_each_entry(rule, &s->fe->switching_rules, list) {
1268 int ret;
1269
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001270 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001271 ret = acl_pass(ret);
1272 if (rule->cond->pol == ACL_COND_UNLESS)
1273 ret = !ret;
1274
1275 if (ret) {
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001276 if (!session_set_backend(s, rule->be.backend))
1277 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001278 break;
1279 }
1280 }
1281
1282 /* To ensure correct connection accounting on the backend, we
1283 * have to assign one if it was not set (eg: a listen). This
1284 * measure also takes care of correctly setting the default
1285 * backend if any.
1286 */
1287 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001288 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
1289 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001290 }
1291
Willy Tarreaufb356202010-08-03 14:02:05 +02001292 /* we don't want to run the TCP or HTTP filters again if the backend has not changed */
1293 if (s->fe == s->be) {
1294 s->req->analysers &= ~AN_REQ_INSPECT_BE;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001295 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001296 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001297
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001298 /* 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 +01001299 * persistence rule, and report that in the session.
1300 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001301 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001302 int ret = 1;
1303
1304 if (prst_rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001305 ret = acl_exec_cond(prst_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau4de91492010-01-22 19:10:05 +01001306 ret = acl_pass(ret);
1307 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1308 ret = !ret;
1309 }
1310
1311 if (ret) {
1312 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001313 if (prst_rule->type == PERSIST_TYPE_FORCE) {
1314 s->flags |= SN_FORCE_PRST;
1315 } else {
1316 s->flags |= SN_IGNORE_PRST;
1317 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001318 break;
1319 }
1320 }
1321
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001322 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001323
1324 sw_failed:
1325 /* immediately abort this request in case of allocation failure */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001326 channel_abort(s->req);
1327 channel_abort(s->rep);
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001328
1329 if (!(s->flags & SN_ERR_MASK))
1330 s->flags |= SN_ERR_RESOURCE;
1331 if (!(s->flags & SN_FINST_MASK))
1332 s->flags |= SN_FINST_R;
1333
1334 s->txn.status = 500;
1335 s->req->analysers = 0;
1336 s->req->analyse_exp = TICK_ETERNITY;
1337 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001338}
1339
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001340/* This stream analyser works on a request. It applies all use-server rules on
1341 * it then returns 1. The data must already be present in the buffer otherwise
1342 * they won't match. It always returns 1.
1343 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001344static int process_server_rules(struct session *s, struct channel *req, int an_bit)
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001345{
1346 struct proxy *px = s->be;
1347 struct server_rule *rule;
1348
1349 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1350 now_ms, __FUNCTION__,
1351 s,
1352 req,
1353 req->rex, req->wex,
1354 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001355 req->buf->i + req->buf->o,
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001356 req->analysers);
1357
1358 if (!(s->flags & SN_ASSIGNED)) {
1359 list_for_each_entry(rule, &px->server_rules, list) {
1360 int ret;
1361
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001362 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001363 ret = acl_pass(ret);
1364 if (rule->cond->pol == ACL_COND_UNLESS)
1365 ret = !ret;
1366
1367 if (ret) {
1368 struct server *srv = rule->srv.ptr;
1369
1370 if ((srv->state & SRV_RUNNING) ||
1371 (px->options & PR_O_PERSIST) ||
1372 (s->flags & SN_FORCE_PRST)) {
1373 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001374 s->target = &srv->obj_type;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001375 break;
1376 }
1377 /* if the server is not UP, let's go on with next rules
1378 * just in case another one is suited.
1379 */
1380 }
1381 }
1382 }
1383
1384 req->analysers &= ~an_bit;
1385 req->analyse_exp = TICK_ETERNITY;
1386 return 1;
1387}
1388
Emeric Brun1d33b292010-01-04 15:47:17 +01001389/* This stream analyser works on a request. It applies all sticking rules on
1390 * it then returns 1. The data must already be present in the buffer otherwise
1391 * they won't match. It always returns 1.
1392 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001393static int process_sticking_rules(struct session *s, struct channel *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001394{
1395 struct proxy *px = s->be;
1396 struct sticking_rule *rule;
1397
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001398 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Emeric Brun1d33b292010-01-04 15:47:17 +01001399 now_ms, __FUNCTION__,
1400 s,
1401 req,
1402 req->rex, req->wex,
1403 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001404 req->buf->i,
Emeric Brun1d33b292010-01-04 15:47:17 +01001405 req->analysers);
1406
1407 list_for_each_entry(rule, &px->sticking_rules, list) {
1408 int ret = 1 ;
1409 int i;
1410
1411 for (i = 0; i < s->store_count; i++) {
1412 if (rule->table.t == s->store[i].table)
1413 break;
1414 }
1415
1416 if (i != s->store_count)
1417 continue;
1418
1419 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001420 ret = acl_exec_cond(rule->cond, px, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001421 ret = acl_pass(ret);
1422 if (rule->cond->pol == ACL_COND_UNLESS)
1423 ret = !ret;
1424 }
1425
1426 if (ret) {
1427 struct stktable_key *key;
1428
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001429 key = stktable_fetch_key(rule->table.t, px, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->expr);
Emeric Brun1d33b292010-01-04 15:47:17 +01001430 if (!key)
1431 continue;
1432
1433 if (rule->flags & STK_IS_MATCH) {
1434 struct stksess *ts;
1435
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001436 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001437 if (!(s->flags & SN_ASSIGNED)) {
1438 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001439 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001440
1441 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001442 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1443 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001444 if (node) {
1445 struct server *srv;
1446
1447 srv = container_of(node, struct server, conf.id);
Willy Tarreau4de91492010-01-22 19:10:05 +01001448 if ((srv->state & SRV_RUNNING) ||
1449 (px->options & PR_O_PERSIST) ||
1450 (s->flags & SN_FORCE_PRST)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001451 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001452 s->target = &srv->obj_type;
Emeric Brun1d33b292010-01-04 15:47:17 +01001453 }
1454 }
1455 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001456 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001457 }
1458 }
1459 if (rule->flags & STK_IS_STORE) {
1460 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1461 struct stksess *ts;
1462
1463 ts = stksess_new(rule->table.t, key);
1464 if (ts) {
1465 s->store[s->store_count].table = rule->table.t;
1466 s->store[s->store_count++].ts = ts;
1467 }
1468 }
1469 }
1470 }
1471 }
1472
1473 req->analysers &= ~an_bit;
1474 req->analyse_exp = TICK_ETERNITY;
1475 return 1;
1476}
1477
1478/* This stream analyser works on a response. It applies all store rules on it
1479 * then returns 1. The data must already be present in the buffer otherwise
1480 * they won't match. It always returns 1.
1481 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001482static int process_store_rules(struct session *s, struct channel *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001483{
1484 struct proxy *px = s->be;
1485 struct sticking_rule *rule;
1486 int i;
1487
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001488 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Emeric Brun1d33b292010-01-04 15:47:17 +01001489 now_ms, __FUNCTION__,
1490 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001491 rep,
1492 rep->rex, rep->wex,
1493 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001494 rep->buf->i,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001495 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001496
1497 list_for_each_entry(rule, &px->storersp_rules, list) {
1498 int ret = 1 ;
1499 int storereqidx = -1;
1500
1501 for (i = 0; i < s->store_count; i++) {
1502 if (rule->table.t == s->store[i].table) {
1503 if (!(s->store[i].flags))
1504 storereqidx = i;
1505 break;
1506 }
1507 }
1508
1509 if ((i != s->store_count) && (storereqidx == -1))
1510 continue;
1511
1512 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001513 ret = acl_exec_cond(rule->cond, px, s, &s->txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001514 ret = acl_pass(ret);
1515 if (rule->cond->pol == ACL_COND_UNLESS)
1516 ret = !ret;
1517 }
1518
1519 if (ret) {
1520 struct stktable_key *key;
1521
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001522 key = stktable_fetch_key(rule->table.t, px, s, &s->txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->expr);
Emeric Brun1d33b292010-01-04 15:47:17 +01001523 if (!key)
1524 continue;
1525
1526 if (storereqidx != -1) {
Willy Tarreau393379c2010-06-06 12:11:37 +02001527 stksess_setkey(s->store[storereqidx].table, s->store[storereqidx].ts, key);
Emeric Brun1d33b292010-01-04 15:47:17 +01001528 s->store[storereqidx].flags = 1;
1529 }
1530 else if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1531 struct stksess *ts;
1532
1533 ts = stksess_new(rule->table.t, key);
1534 if (ts) {
1535 s->store[s->store_count].table = rule->table.t;
1536 s->store[s->store_count].flags = 1;
1537 s->store[s->store_count++].ts = ts;
1538 }
1539 }
1540 }
1541 }
1542
1543 /* process store request and store response */
1544 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001545 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001546 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001547
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001548 if (objt_server(s->target) && objt_server(s->target)->state & SRV_NON_STICK) {
Simon Hormanfa461682011-06-25 09:39:49 +09001549 stksess_free(s->store[i].table, s->store[i].ts);
1550 s->store[i].ts = NULL;
1551 continue;
1552 }
1553
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001554 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1555 if (ts) {
1556 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001557 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001558 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001559 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001560 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001561 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001562
1563 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001564 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001565 stktable_data_cast(ptr, server_id) = objt_server(s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001566 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001567 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001568
1569 rep->analysers &= ~an_bit;
1570 rep->analyse_exp = TICK_ETERNITY;
1571 return 1;
1572}
1573
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001574/* This macro is very specific to the function below. See the comments in
1575 * process_session() below to understand the logic and the tests.
1576 */
1577#define UPDATE_ANALYSERS(real, list, back, flag) { \
1578 list = (((list) & ~(flag)) | ~(back)) & (real); \
1579 back = real; \
1580 if (!(list)) \
1581 break; \
1582 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1583 continue; \
1584}
1585
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001586/* Processes the client, server, request and response jobs of a session task,
1587 * then puts it back to the wait queue in a clean state, or cleans up its
1588 * resources if it must be deleted. Returns in <next> the date the task wants
1589 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1590 * nothing too many times, the request and response buffers flags are monitored
1591 * and each function is called only if at least another function has changed at
1592 * least one flag it is interested in.
1593 */
Willy Tarreau26c25062009-03-08 09:38:41 +01001594struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001595{
Willy Tarreau827aee92011-03-10 16:55:02 +01001596 struct server *srv;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001597 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001598 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001599 unsigned int rq_prod_last, rq_cons_last;
1600 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001601 unsigned int req_ana_back;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001602
1603 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
1604 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
1605
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001606 /* this data may be no longer valid, clear it */
1607 memset(&s->txn.auth, 0, sizeof(s->txn.auth));
1608
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001609 /* This flag must explicitly be set every time */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001610 s->req->flags &= ~CF_READ_NOEXP;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001611
1612 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001613 rqf_last = s->req->flags & ~CF_MASK_ANALYSER;
1614 rpf_last = s->rep->flags & ~CF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001615
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001616 /* we don't want the stream interface functions to recursively wake us up */
1617 if (s->req->prod->owner == t)
1618 s->req->prod->flags |= SI_FL_DONT_WAKE;
1619 if (s->req->cons->owner == t)
1620 s->req->cons->flags |= SI_FL_DONT_WAKE;
1621
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001622 /* 1a: Check for low level timeouts if needed. We just set a flag on
1623 * stream interfaces when their timeouts have expired.
1624 */
1625 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
1626 stream_int_check_timeouts(&s->si[0]);
1627 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001628
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001629 /* check channel timeouts, and close the corresponding stream interfaces
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001630 * for future reads or writes. Note: this will also concern upper layers
1631 * but we do not touch any other flag. We must be careful and correctly
1632 * detect state changes when calling them.
1633 */
1634
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001635 channel_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001636
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001637 if (unlikely((s->req->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
Willy Tarreau14641402009-12-29 14:49:56 +01001638 s->req->cons->flags |= SI_FL_NOLINGER;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001639 si_shutw(s->req->cons);
Willy Tarreau14641402009-12-29 14:49:56 +01001640 }
1641
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001642 if (unlikely((s->req->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001643 if (s->req->prod->flags & SI_FL_NOHALF)
1644 s->req->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001645 si_shutr(s->req->prod);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001646 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001647
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001648 channel_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001649
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001650 if (unlikely((s->rep->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
Willy Tarreau14641402009-12-29 14:49:56 +01001651 s->rep->cons->flags |= SI_FL_NOLINGER;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001652 si_shutw(s->rep->cons);
Willy Tarreau14641402009-12-29 14:49:56 +01001653 }
1654
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001655 if (unlikely((s->rep->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001656 if (s->rep->prod->flags & SI_FL_NOHALF)
1657 s->rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001658 si_shutr(s->rep->prod);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001659 }
Willy Tarreau798f4322012-11-08 14:49:17 +01001660
1661 /* Once in a while we're woken up because the task expires. But
1662 * this does not necessarily mean that a timeout has been reached.
1663 * So let's not run a whole session processing if only an expiration
1664 * timeout needs to be refreshed.
1665 */
1666 if (!((s->req->flags | s->rep->flags) &
1667 (CF_SHUTR|CF_READ_ACTIVITY|CF_READ_TIMEOUT|CF_SHUTW|
1668 CF_WRITE_ACTIVITY|CF_WRITE_TIMEOUT|CF_ANA_TIMEOUT)) &&
1669 !((s->si[0].flags | s->si[1].flags) & (SI_FL_EXP|SI_FL_ERR)) &&
1670 ((t->state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER))
1671 goto update_exp_and_leave;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001672 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001673
1674 /* 1b: check for low-level errors reported at the stream interface.
1675 * First we check if it's a retryable error (in which case we don't
1676 * want to tell the buffer). Otherwise we report the error one level
1677 * upper by setting flags into the buffers. Note that the side towards
1678 * the client cannot have connect (hence retryable) errors. Also, the
1679 * connection setup code must be able to deal with any type of abort.
1680 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001681 srv = objt_server(s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001682 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
1683 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
Willy Tarreau73b013b2012-05-21 16:31:45 +02001684 si_shutr(&s->si[0]);
1685 si_shutw(&s->si[0]);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001686 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001687 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001688 s->be->be_counters.cli_aborts++;
1689 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001690 if (srv)
1691 srv->counters.cli_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001692 if (!(s->flags & SN_ERR_MASK))
1693 s->flags |= SN_ERR_CLICL;
1694 if (!(s->flags & SN_FINST_MASK))
1695 s->flags |= SN_FINST_D;
1696 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001697 }
1698 }
1699
1700 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
1701 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
Willy Tarreau73b013b2012-05-21 16:31:45 +02001702 si_shutr(&s->si[1]);
1703 si_shutw(&s->si[1]);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001704 stream_int_report_error(&s->si[1]);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001705 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001706 if (srv)
1707 srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001708 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001709 s->be->be_counters.srv_aborts++;
1710 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001711 if (srv)
1712 srv->counters.srv_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001713 if (!(s->flags & SN_ERR_MASK))
1714 s->flags |= SN_ERR_SRVCL;
1715 if (!(s->flags & SN_FINST_MASK))
1716 s->flags |= SN_FINST_D;
1717 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001718 }
1719 /* note: maybe we should process connection errors here ? */
1720 }
1721
1722 if (s->si[1].state == SI_ST_CON) {
1723 /* we were trying to establish a connection on the server side,
1724 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1725 */
1726 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
1727 sess_update_st_cer(s, &s->si[1]);
1728 else if (s->si[1].state == SI_ST_EST)
1729 sess_establish(s, &s->si[1]);
1730
1731 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1732 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1733 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1734 */
1735 }
1736
Willy Tarreau815a9b22010-07-27 17:15:12 +02001737 rq_prod_last = s->si[0].state;
1738 rq_cons_last = s->si[1].state;
1739 rp_cons_last = s->si[0].state;
1740 rp_prod_last = s->si[1].state;
1741
1742 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001743 /* Check for connection closure */
1744
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001745 DPRINTF(stderr,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001746 "[%u] %s:%d: task=%p s=%p, sfl=0x%08x, rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rqh=%d rqt=%d rph=%d rpt=%d cs=%d ss=%d, cet=0x%x set=0x%x retr=%d\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001747 now_ms, __FUNCTION__, __LINE__,
1748 t,
1749 s, s->flags,
1750 s->req, s->rep,
1751 s->req->rex, s->rep->wex,
1752 s->req->flags, s->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001753 s->req->buf->i, s->req->buf->o, s->rep->buf->i, s->rep->buf->o, s->rep->cons->state, s->req->cons->state,
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001754 s->rep->cons->err_type, s->req->cons->err_type,
Willy Tarreauee28de02010-06-01 09:51:00 +02001755 s->req->cons->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001756
1757 /* nothing special to be done on client side */
1758 if (unlikely(s->req->prod->state == SI_ST_DIS))
1759 s->req->prod->state = SI_ST_CLO;
1760
1761 /* When a server-side connection is released, we have to count it and
1762 * check for pending connections on this server.
1763 */
1764 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
1765 s->req->cons->state = SI_ST_CLO;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001766 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001767 if (srv) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001768 if (s->flags & SN_CURR_SESS) {
1769 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001770 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001771 }
1772 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001773 if (may_dequeue_tasks(srv, s->be))
1774 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001775 }
1776 }
1777
1778 /*
1779 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1780 * at this point.
1781 */
1782
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001783 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001784 /* Analyse request */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001785 if (((s->req->flags & ~rqf_last) & CF_MASK_ANALYSER) ||
1786 ((s->req->flags ^ rqf_last) & CF_MASK_STATIC) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001787 s->si[0].state != rq_prod_last ||
1788 s->si[1].state != rq_cons_last) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001789 unsigned int flags = s->req->flags;
1790
1791 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001792 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001793 unsigned int ana_list;
1794 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001795
Willy Tarreau90deb182010-01-07 00:20:41 +01001796 /* it's up to the analysers to stop new connections,
1797 * disable reading or closing. Note: if an analyser
1798 * disables any of these bits, it is responsible for
1799 * enabling them again when it disables itself, so
1800 * that other analysers are called in similar conditions.
1801 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001802 channel_auto_read(s->req);
1803 channel_auto_connect(s->req);
1804 channel_auto_close(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001805
1806 /* We will call all analysers for which a bit is set in
1807 * s->req->analysers, following the bit order from LSB
1808 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001809 * the list when not needed. Any analyser may return 0
1810 * to break out of the loop, either because of missing
1811 * data to take a decision, or because it decides to
1812 * kill the session. We loop at least once through each
1813 * analyser, and we may loop again if other analysers
1814 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001815 *
1816 * We build a list of analysers to run. We evaluate all
1817 * of these analysers in the order of the lower bit to
1818 * the higher bit. This ordering is very important.
1819 * An analyser will often add/remove other analysers,
1820 * including itself. Any changes to itself have no effect
1821 * on the loop. If it removes any other analysers, we
1822 * want those analysers not to be called anymore during
1823 * this loop. If it adds an analyser that is located
1824 * after itself, we want it to be scheduled for being
1825 * processed during the loop. If it adds an analyser
1826 * which is located before it, we want it to switch to
1827 * it immediately, even if it has already been called
1828 * once but removed since.
1829 *
1830 * In order to achieve this, we compare the analyser
1831 * list after the call with a copy of it before the
1832 * call. The work list is fed with analyser bits that
1833 * appeared during the call. Then we compare previous
1834 * work list with the new one, and check the bits that
1835 * appeared. If the lowest of these bits is lower than
1836 * the current bit, it means we have enabled a previous
1837 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001838 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001839
1840 ana_list = ana_back = s->req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001841 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001842 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001843
Willy Tarreaufb356202010-08-03 14:02:05 +02001844 if (ana_list & AN_REQ_INSPECT_FE) {
1845 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001846 break;
Willy Tarreaufb356202010-08-03 14:02:05 +02001847 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001848 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001849
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001850 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001851 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001852 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001853 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001854 }
1855
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001856 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001857 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
1858 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001859 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001860 }
1861
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001862 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001863 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
1864 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001865 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001866 }
1867
Willy Tarreaufb356202010-08-03 14:02:05 +02001868 if (ana_list & AN_REQ_INSPECT_BE) {
1869 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_BE))
1870 break;
1871 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
1872 }
1873
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001874 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001875 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
1876 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001877 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001878 }
1879
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001880 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001881 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001882 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001883 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001884 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001885
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001886 if (ana_list & AN_REQ_SRV_RULES) {
1887 if (!process_server_rules(s, s->req, AN_REQ_SRV_RULES))
1888 break;
1889 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SRV_RULES);
1890 }
1891
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001892 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreauc465fd72009-08-31 00:17:18 +02001893 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
1894 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001895 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001896 }
1897
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001898 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001899 if (!http_process_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001900 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001901 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001902 }
Emeric Brun647caf12009-06-30 17:57:00 +02001903
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001904 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Emeric Brun647caf12009-06-30 17:57:00 +02001905 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
1906 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001907 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001908 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001909
Emeric Brun1d33b292010-01-04 15:47:17 +01001910 if (ana_list & AN_REQ_STICKING_RULES) {
1911 if (!process_sticking_rules(s, s->req, AN_REQ_STICKING_RULES))
1912 break;
1913 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
1914 }
1915
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001916 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01001917 if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY))
1918 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001919 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001920 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001921 break;
1922 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001923 }
Willy Tarreau84455332009-03-15 22:34:05 +01001924
Willy Tarreau815a9b22010-07-27 17:15:12 +02001925 rq_prod_last = s->si[0].state;
1926 rq_cons_last = s->si[1].state;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001927 s->req->flags &= ~CF_WAKE_ONCE;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001928 rqf_last = s->req->flags;
1929
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001930 if ((s->req->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001931 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001932 }
1933
Willy Tarreau576507f2010-01-07 00:09:04 +01001934 /* we'll monitor the request analysers while parsing the response,
1935 * because some response analysers may indirectly enable new request
1936 * analysers (eg: HTTP keep-alive).
1937 */
1938 req_ana_back = s->req->analysers;
1939
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001940 resync_response:
1941 /* Analyse response */
1942
Willy Tarreaub31c9712012-11-11 23:05:39 +01001943 if (((s->rep->flags & ~rpf_last) & CF_MASK_ANALYSER) ||
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001944 (s->rep->flags ^ rpf_last) & CF_MASK_STATIC ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001945 s->si[0].state != rp_cons_last ||
1946 s->si[1].state != rp_prod_last) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001947 unsigned int flags = s->rep->flags;
1948
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001949 if ((s->rep->flags & CF_MASK_ANALYSER) &&
Willy Tarreau0499e352010-12-17 07:13:42 +01001950 (s->rep->analysers & AN_REQ_WAIT_HTTP)) {
1951 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1952 * for some free space in the response buffer, so we might need to call
1953 * it when something changes in the response buffer, but still we pass
1954 * it the request buffer. Note that the SI state might very well still
1955 * be zero due to us returning a flow of redirects!
1956 */
1957 s->rep->analysers &= ~AN_REQ_WAIT_HTTP;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001958 s->req->flags |= CF_WAKE_ONCE;
Willy Tarreau0499e352010-12-17 07:13:42 +01001959 }
1960
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001961 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001962 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001963 unsigned int ana_list;
1964 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001965
Willy Tarreau90deb182010-01-07 00:20:41 +01001966 /* it's up to the analysers to stop disable reading or
1967 * closing. Note: if an analyser disables any of these
1968 * bits, it is responsible for enabling them again when
1969 * it disables itself, so that other analysers are called
1970 * in similar conditions.
1971 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001972 channel_auto_read(s->rep);
1973 channel_auto_close(s->rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001974
1975 /* We will call all analysers for which a bit is set in
1976 * s->rep->analysers, following the bit order from LSB
1977 * to MSB. The analysers must remove themselves from
1978 * the list when not needed. Any analyser may return 0
1979 * to break out of the loop, either because of missing
1980 * data to take a decision, or because it decides to
1981 * kill the session. We loop at least once through each
1982 * analyser, and we may loop again if other analysers
1983 * are added in the middle.
1984 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001985
1986 ana_list = ana_back = s->rep->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001987 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001988 /* Warning! ensure that analysers are always placed in ascending order! */
1989
Emeric Brun97679e72010-09-23 17:56:44 +02001990 if (ana_list & AN_RES_INSPECT) {
1991 if (!tcp_inspect_response(s, s->rep, AN_RES_INSPECT))
1992 break;
1993 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_INSPECT);
1994 }
1995
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001996 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001997 if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP))
1998 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001999 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002000 }
2001
Emeric Brun1d33b292010-01-04 15:47:17 +01002002 if (ana_list & AN_RES_STORE_RULES) {
2003 if (!process_store_rules(s, s->rep, AN_RES_STORE_RULES))
2004 break;
2005 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
2006 }
2007
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002008 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002009 if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be))
2010 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002011 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002012 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002013
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002014 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01002015 if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY))
2016 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002017 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002018 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01002019 break;
2020 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002021 }
2022
Willy Tarreau815a9b22010-07-27 17:15:12 +02002023 rp_cons_last = s->si[0].state;
2024 rp_prod_last = s->si[1].state;
2025 rpf_last = s->rep->flags;
2026
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002027 if ((s->rep->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002028 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002029 }
2030
Willy Tarreau576507f2010-01-07 00:09:04 +01002031 /* maybe someone has added some request analysers, so we must check and loop */
2032 if (s->req->analysers & ~req_ana_back)
2033 goto resync_request;
2034
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002035 if ((s->req->flags & ~rqf_last) & CF_MASK_ANALYSER)
Willy Tarreau0499e352010-12-17 07:13:42 +01002036 goto resync_request;
2037
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002038 /* FIXME: here we should call protocol handlers which rely on
2039 * both buffers.
2040 */
2041
2042
2043 /*
Willy Tarreauae526782010-03-04 20:34:23 +01002044 * Now we propagate unhandled errors to the session. Normally
2045 * we're just in a data phase here since it means we have not
2046 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002047 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002048 srv = objt_server(s->target);
Willy Tarreau2f976e12010-11-11 14:28:47 +01002049 if (unlikely(!(s->flags & SN_ERR_MASK))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002050 if (s->req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002051 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +01002052 s->req->analysers = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002053 if (s->req->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002054 s->be->be_counters.cli_aborts++;
2055 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002056 if (srv)
2057 srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01002058 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01002059 }
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002060 else if (s->req->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002061 s->be->be_counters.cli_aborts++;
2062 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002063 if (srv)
2064 srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01002065 s->flags |= SN_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01002066 }
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002067 else if (s->req->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002068 s->be->be_counters.srv_aborts++;
2069 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002070 if (srv)
2071 srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01002072 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01002073 }
2074 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002075 s->be->be_counters.srv_aborts++;
2076 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002077 if (srv)
2078 srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01002079 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01002080 }
Willy Tarreau84455332009-03-15 22:34:05 +01002081 sess_set_term_flags(s);
2082 }
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002083 else if (s->rep->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002084 /* Report it if the server got an error or a read timeout expired */
2085 s->rep->analysers = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002086 if (s->rep->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002087 s->be->be_counters.srv_aborts++;
2088 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002089 if (srv)
2090 srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002091 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01002092 }
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002093 else if (s->rep->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002094 s->be->be_counters.srv_aborts++;
2095 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002096 if (srv)
2097 srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002098 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01002099 }
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002100 else if (s->rep->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002101 s->be->be_counters.cli_aborts++;
2102 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002103 if (srv)
2104 srv->counters.cli_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002105 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01002106 }
2107 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002108 s->be->be_counters.cli_aborts++;
2109 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002110 if (srv)
2111 srv->counters.cli_aborts++;
Willy Tarreauae526782010-03-04 20:34:23 +01002112 s->flags |= SN_ERR_CLITO;
2113 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002114 sess_set_term_flags(s);
2115 }
Willy Tarreau84455332009-03-15 22:34:05 +01002116 }
2117
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002118 /*
2119 * Here we take care of forwarding unhandled data. This also includes
2120 * connection establishments and shutdown requests.
2121 */
2122
2123
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002124 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002125 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002126 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002127 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002128 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002129 if (!s->req->analysers &&
Willy Tarreaub31c9712012-11-11 23:05:39 +01002130 !(s->req->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02002131 (s->req->prod->state >= SI_ST_EST) &&
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002132 (s->req->to_forward != CHN_INFINITE_FORWARD)) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002133 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002134 * attached to it. If any data are left in, we'll permit them to
2135 * move.
2136 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002137 channel_auto_read(s->req);
2138 channel_auto_connect(s->req);
2139 channel_auto_close(s->req);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002140 buffer_flush(s->req->buf);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01002141
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002142 /* We'll let data flow between the producer (if still connected)
2143 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002144 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002145 if (!(s->req->flags & (CF_SHUTR|CF_SHUTW_NOW)))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002146 channel_forward(s->req, CHN_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002147 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002148
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002149 /* check if it is wise to enable kernel splicing to forward request data */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002150 if (!(s->req->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002151 s->req->to_forward &&
2152 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002153 (s->si[0].conn->xprt && s->si[0].conn->xprt->rcv_pipe && s->si[0].conn->xprt->snd_pipe) &&
2154 (s->si[1].conn->xprt && s->si[1].conn->xprt->rcv_pipe && s->si[1].conn->xprt->snd_pipe) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002155 (pipes_used < global.maxpipes) &&
2156 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
2157 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002158 (s->req->flags & CF_STREAMER_FAST)))) {
2159 s->req->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002160 }
2161
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002162 /* reflect what the L7 analysers have seen last */
2163 rqf_last = s->req->flags;
2164
2165 /*
2166 * Now forward all shutdown requests between both sides of the buffer
2167 */
2168
Willy Tarreau520d95e2009-09-19 21:04:57 +02002169 /* first, let's check if the request buffer needs to shutdown(write), which may
2170 * happen either because the input is closed or because we want to force a close
Willy Tarreaue4599762010-03-21 23:25:09 +01002171 * once the server has begun to respond.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002172 */
Willy Tarreaub31c9712012-11-11 23:05:39 +01002173 if (unlikely((s->req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002174 (CF_AUTO_CLOSE|CF_SHUTR)))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002175 channel_shutw_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002176
2177 /* shutdown(write) pending */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002178 if (unlikely((s->req->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
Willy Tarreau8e21bb92012-08-24 22:40:29 +02002179 channel_is_empty(s->req)))
Willy Tarreau73b013b2012-05-21 16:31:45 +02002180 si_shutw(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002181
2182 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002183 if (unlikely((s->req->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW &&
Willy Tarreau3dbc6942008-12-07 13:05:04 +01002184 !s->req->analysers))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002185 channel_shutr_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002186
2187 /* shutdown(read) pending */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002188 if (unlikely((s->req->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002189 if (s->req->prod->flags & SI_FL_NOHALF)
2190 s->req->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau73b013b2012-05-21 16:31:45 +02002191 si_shutr(s->req->prod);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002192 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002193
Willy Tarreau520d95e2009-09-19 21:04:57 +02002194 /* it's possible that an upper layer has requested a connection setup or abort.
2195 * There are 2 situations where we decide to establish a new connection :
2196 * - there are data scheduled for emission in the buffer
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002197 * - the CF_AUTO_CONNECT flag is set (active connection)
Willy Tarreau520d95e2009-09-19 21:04:57 +02002198 */
2199 if (s->req->cons->state == SI_ST_INI) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002200 if (!(s->req->flags & CF_SHUTW)) {
2201 if ((s->req->flags & CF_AUTO_CONNECT) || !channel_is_empty(s->req)) {
Willy Tarreaub24281b2011-02-13 13:16:36 +01002202 /* If we have an applet without a connect method, we immediately
Willy Tarreau85e7d002010-05-31 11:57:51 +02002203 * switch to the connected state, otherwise we perform a connection
2204 * request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002205 */
Willy Tarreau85e7d002010-05-31 11:57:51 +02002206 s->req->cons->state = SI_ST_REQ; /* new connection requested */
Willy Tarreau070ceb62010-06-01 10:36:43 +02002207 s->req->cons->conn_retries = s->be->conn_retries;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002208 if (unlikely(obj_type(s->req->cons->conn->target) == OBJ_TYPE_APPLET &&
Willy Tarreau73b013b2012-05-21 16:31:45 +02002209 !(si_ctrl(s->req->cons) && si_ctrl(s->req->cons)->connect))) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02002210 s->req->cons->state = SI_ST_EST; /* connection established */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002211 s->rep->flags |= CF_READ_ATTACHED; /* producer is now attached */
Willy Tarreau85e7d002010-05-31 11:57:51 +02002212 s->req->wex = TICK_ETERNITY;
2213 }
Willy Tarreau520d95e2009-09-19 21:04:57 +02002214 }
Willy Tarreau73201222009-08-16 18:27:24 +02002215 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002216 else {
Willy Tarreau92795622009-03-06 12:51:23 +01002217 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002218 channel_shutw_now(s->req); /* fix buffer flags upon abort */
2219 channel_shutr_now(s->rep);
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002220 }
Willy Tarreau92795622009-03-06 12:51:23 +01002221 }
2222
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002223
2224 /* we may have a pending connection request, or a connection waiting
2225 * for completion.
2226 */
2227 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
2228 do {
2229 /* nb: step 1 might switch from QUE to ASS, but we first want
2230 * to give a chance to step 2 to perform a redirect if needed.
2231 */
2232 if (s->si[1].state != SI_ST_REQ)
2233 sess_update_stream_int(s, &s->si[1]);
2234 if (s->si[1].state == SI_ST_REQ)
2235 sess_prepare_conn_req(s, &s->si[1]);
2236
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002237 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01002238 if (s->si[1].state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SN_REDIRECTABLE))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002239 perform_http_redirect(s, &s->si[1]);
2240 } while (s->si[1].state == SI_ST_ASS);
Mark Lamourinec2247f02012-01-04 13:02:01 -05002241
2242 /* Now we can add the server name to a header (if requested) */
2243 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
Stathis Voukelatos09a030a2012-01-09 14:27:13 +01002244 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau45c0d982012-03-09 12:11:57 +01002245 (s->be->mode == PR_MODE_HTTP) &&
2246 (s->be->server_id_hdr_name != NULL)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002247 http_send_name_header(&s->txn, s->be, objt_server(s->target)->id);
Mark Lamourinec2247f02012-01-04 13:02:01 -05002248 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002249 }
2250
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002251 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002252 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002253 goto resync_stream_interface;
2254
Willy Tarreau815a9b22010-07-27 17:15:12 +02002255 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002256 if ((s->req->flags ^ rqf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002257 goto resync_request;
2258
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002259 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01002260
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002261 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002262 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002263 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002264 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002265 */
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002266 if (!s->rep->analysers &&
Willy Tarreaub31c9712012-11-11 23:05:39 +01002267 !(s->rep->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02002268 (s->rep->prod->state >= SI_ST_EST) &&
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002269 (s->rep->to_forward != CHN_INFINITE_FORWARD)) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002270 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002271 * attached to it. If any data are left in, we'll permit them to
2272 * move.
2273 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002274 channel_auto_read(s->rep);
2275 channel_auto_close(s->rep);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002276 buffer_flush(s->rep->buf);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002277
2278 /* We'll let data flow between the producer (if still connected)
2279 * to the consumer.
2280 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002281 if (!(s->rep->flags & (CF_SHUTR|CF_SHUTW_NOW)))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002282 channel_forward(s->rep, CHN_INFINITE_FORWARD);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002283
2284 /* if we have no analyser anymore in any direction and have a
2285 * tunnel timeout set, use it now.
2286 */
2287 if (!s->req->analysers && s->be->timeout.tunnel) {
2288 s->req->rto = s->req->wto = s->rep->rto = s->rep->wto =
2289 s->be->timeout.tunnel;
2290 s->req->rex = s->req->wex = s->rep->rex = s->rep->wex =
2291 tick_add(now_ms, s->be->timeout.tunnel);
2292 }
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002293 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002294
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002295 /* check if it is wise to enable kernel splicing to forward response data */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002296 if (!(s->rep->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002297 s->rep->to_forward &&
2298 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002299 (s->si[0].conn->xprt && s->si[0].conn->xprt->rcv_pipe && s->si[0].conn->xprt->snd_pipe) &&
2300 (s->si[1].conn->xprt && s->si[1].conn->xprt->rcv_pipe && s->si[1].conn->xprt->snd_pipe) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002301 (pipes_used < global.maxpipes) &&
2302 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2303 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002304 (s->rep->flags & CF_STREAMER_FAST)))) {
2305 s->rep->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002306 }
2307
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002308 /* reflect what the L7 analysers have seen last */
2309 rpf_last = s->rep->flags;
2310
2311 /*
2312 * Now forward all shutdown requests between both sides of the buffer
2313 */
2314
2315 /*
2316 * FIXME: this is probably where we should produce error responses.
2317 */
2318
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002319 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreaub31c9712012-11-11 23:05:39 +01002320 if (unlikely((s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002321 (CF_AUTO_CLOSE|CF_SHUTR)))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002322 channel_shutw_now(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002323
2324 /* shutdown(write) pending */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002325 if (unlikely((s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
Willy Tarreau8e21bb92012-08-24 22:40:29 +02002326 channel_is_empty(s->rep)))
Willy Tarreau73b013b2012-05-21 16:31:45 +02002327 si_shutw(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002328
2329 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002330 if (unlikely((s->rep->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW) &&
Willy Tarreau3dbc6942008-12-07 13:05:04 +01002331 !s->rep->analysers)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002332 channel_shutr_now(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002333
2334 /* shutdown(read) pending */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002335 if (unlikely((s->rep->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002336 if (s->rep->prod->flags & SI_FL_NOHALF)
2337 s->rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau73b013b2012-05-21 16:31:45 +02002338 si_shutr(s->rep->prod);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002339 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002340
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002341 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002342 goto resync_stream_interface;
2343
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002344 if (s->req->flags != rqf_last)
2345 goto resync_request;
2346
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002347 if ((s->rep->flags ^ rpf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002348 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002349
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002350 /* we're interested in getting wakeups again */
2351 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
2352 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
2353
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002354 /* This is needed only when debugging is enabled, to indicate
2355 * client-side or server-side close. Please note that in the unlikely
2356 * event where both sides would close at once, the sequence is reported
2357 * on the server side first.
2358 */
2359 if (unlikely((global.mode & MODE_DEBUG) &&
2360 (!(global.mode & MODE_QUIET) ||
2361 (global.mode & MODE_VERBOSE)))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002362 if (s->si[1].state == SI_ST_CLO &&
2363 s->si[1].prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002364 chunk_printf(&trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002365 s->uniq_id, s->be->id,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01002366 (unsigned short)s->si[0].conn->t.sock.fd,
2367 (unsigned short)s->si[1].conn->t.sock.fd);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002368 if (write(1, trash.str, trash.len) < 0) /* shut gcc warning */;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002369 }
2370
2371 if (s->si[0].state == SI_ST_CLO &&
2372 s->si[0].prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002373 chunk_printf(&trash, "%08x:%s.clicls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002374 s->uniq_id, s->be->id,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01002375 (unsigned short)s->si[0].conn->t.sock.fd,
2376 (unsigned short)s->si[1].conn->t.sock.fd);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002377 if (write(1, trash.str, trash.len) < 0) /* shut gcc warning */;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002378 }
2379 }
2380
2381 if (likely((s->rep->cons->state != SI_ST_CLO) ||
2382 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
2383
2384 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
2385 session_process_counters(s);
2386
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002387 if (s->rep->cons->state == SI_ST_EST && obj_type(s->rep->cons->conn->target) != OBJ_TYPE_APPLET)
Willy Tarreau73b013b2012-05-21 16:31:45 +02002388 si_update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002389
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002390 if (s->req->cons->state == SI_ST_EST && obj_type(s->req->cons->conn->target) != OBJ_TYPE_APPLET)
Willy Tarreau73b013b2012-05-21 16:31:45 +02002391 si_update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002392
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002393 s->req->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2394 s->rep->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002395 s->si[0].prev_state = s->si[0].state;
2396 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01002397 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
2398 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002399
2400 /* Trick: if a request is being waiting for the server to respond,
2401 * and if we know the server can timeout, we don't want the timeout
2402 * to expire on the client side first, but we're still interested
2403 * in passing data from the client to the server (eg: POST). Thus,
2404 * we can cancel the client's request timeout if the server's
2405 * request timeout is set and the server has not yet sent a response.
2406 */
2407
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002408 if ((s->rep->flags & (CF_AUTO_CLOSE|CF_SHUTR)) == 0 &&
Willy Tarreau86491c32008-12-14 09:04:47 +01002409 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002410 s->req->flags |= CF_READ_NOEXP;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002411 s->req->rex = TICK_ETERNITY;
Willy Tarreau86491c32008-12-14 09:04:47 +01002412 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002413
Willy Tarreau7a20aa62010-07-13 16:30:45 +02002414 /* Call the stream interfaces' I/O handlers when embedded.
Willy Tarreau1accfc02009-09-05 20:57:35 +02002415 * Note that this one may wake the task up again.
2416 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002417 if (obj_type(s->req->cons->conn->target) == OBJ_TYPE_APPLET ||
2418 obj_type(s->rep->cons->conn->target) == OBJ_TYPE_APPLET) {
2419 if (objt_applet(s->req->cons->conn->target))
2420 objt_applet(s->req->cons->conn->target)->fct(s->req->cons);
2421 if (objt_applet(s->rep->cons->conn->target))
2422 objt_applet(s->rep->cons->conn->target)->fct(s->rep->cons);
Willy Tarreau1accfc02009-09-05 20:57:35 +02002423 if (task_in_rq(t)) {
2424 /* If we woke up, we don't want to requeue the
2425 * task to the wait queue, but rather requeue
2426 * it into the runqueue ASAP.
2427 */
2428 t->expire = TICK_ETERNITY;
2429 return t;
2430 }
2431 }
2432
Willy Tarreau798f4322012-11-08 14:49:17 +01002433 update_exp_and_leave:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002434 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
2435 tick_first(s->rep->rex, s->rep->wex));
2436 if (s->req->analysers)
2437 t->expire = tick_first(t->expire, s->req->analyse_exp);
2438
2439 if (s->si[0].exp)
2440 t->expire = tick_first(t->expire, s->si[0].exp);
2441
2442 if (s->si[1].exp)
2443 t->expire = tick_first(t->expire, s->si[1].exp);
2444
2445#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002446 fprintf(stderr,
2447 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2448 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
2449 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
2450 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 +01002451#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002452
2453#ifdef DEBUG_DEV
2454 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002455 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002456 ABORT_NOW();
2457#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01002458 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002459 }
2460
2461 s->fe->feconn--;
2462 if (s->flags & SN_BE_ASSIGNED)
2463 s->be->beconn--;
Willy Tarreau3c63fd82011-09-07 18:00:47 +02002464 if (!(s->listener->options & LI_O_UNLIMITED))
2465 actconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002466 jobs--;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +02002467 s->listener->nbconn--;
Willy Tarreau62793712011-07-24 19:23:38 +02002468 if (s->listener->state == LI_FULL)
2469 resume_listener(s->listener);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002470
Willy Tarreau08ceb102011-07-24 22:58:00 +02002471 /* Dequeues all of the listeners waiting for a resource */
2472 if (!LIST_ISEMPTY(&global_listener_queue))
2473 dequeue_all_listeners(&global_listener_queue);
2474
Willy Tarreaub32907b2011-07-25 08:37:44 +02002475 if (!LIST_ISEMPTY(&s->fe->listener_queue) &&
2476 (!s->fe->fe_sps_lim || freq_ctr_remain(&s->fe->fe_sess_per_sec, s->fe->fe_sps_lim, 0) > 0))
Willy Tarreau07687c12011-07-24 23:55:06 +02002477 dequeue_all_listeners(&s->fe->listener_queue);
2478
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002479 if (unlikely((global.mode & MODE_DEBUG) &&
2480 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002481 chunk_printf(&trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002482 s->uniq_id, s->be->id,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01002483 (unsigned short)s->req->prod->conn->t.sock.fd,
2484 (unsigned short)s->req->cons->conn->t.sock.fd);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002485 if (write(1, trash.str, trash.len) < 0) /* shut gcc warning */;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002486 }
2487
2488 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
2489 session_process_counters(s);
2490
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002491 if (s->txn.status) {
2492 int n;
2493
2494 n = s->txn.status / 100;
2495 if (n < 1 || n > 5)
2496 n = 0;
2497
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002498 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002499 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01002500 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002501 s->fe->fe_counters.p.http.comp_rsp++;
2502 }
Willy Tarreau24657792010-02-26 10:30:28 +01002503 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002504 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002505 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002506 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01002507 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002508 s->be->be_counters.p.http.comp_rsp++;
2509 }
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002510 }
2511
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002512 /* let's do a final log if we need it */
2513 if (s->logs.logwait &&
2514 !(s->flags & SN_MONITOR) &&
2515 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002516 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002517 }
2518
2519 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002520 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002521 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002522 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002523 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002524}
2525
Willy Tarreau7c669d72008-06-20 15:04:11 +02002526/*
2527 * This function adjusts sess->srv_conn and maintains the previous and new
2528 * server's served session counts. Setting newsrv to NULL is enough to release
2529 * current connection slot. This function also notifies any LB algo which might
2530 * expect to be informed about any change in the number of active sessions on a
2531 * server.
2532 */
2533void sess_change_server(struct session *sess, struct server *newsrv)
2534{
2535 if (sess->srv_conn == newsrv)
2536 return;
2537
2538 if (sess->srv_conn) {
2539 sess->srv_conn->served--;
2540 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2541 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Simon Hormanaf514952011-06-21 14:34:57 +09002542 session_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002543 }
2544
2545 if (newsrv) {
2546 newsrv->served++;
2547 if (newsrv->proxy->lbprm.server_take_conn)
2548 newsrv->proxy->lbprm.server_take_conn(newsrv);
Simon Hormanaf514952011-06-21 14:34:57 +09002549 session_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002550 }
2551}
2552
Willy Tarreau84455332009-03-15 22:34:05 +01002553/* Handle server-side errors for default protocols. It is called whenever a a
2554 * connection setup is aborted or a request is aborted in queue. It sets the
2555 * session termination flags so that the caller does not have to worry about
2556 * them. It's installed as ->srv_error for the server-side stream_interface.
2557 */
2558void default_srv_error(struct session *s, struct stream_interface *si)
2559{
2560 int err_type = si->err_type;
2561 int err = 0, fin = 0;
2562
2563 if (err_type & SI_ET_QUEUE_ABRT) {
2564 err = SN_ERR_CLICL;
2565 fin = SN_FINST_Q;
2566 }
2567 else if (err_type & SI_ET_CONN_ABRT) {
2568 err = SN_ERR_CLICL;
2569 fin = SN_FINST_C;
2570 }
2571 else if (err_type & SI_ET_QUEUE_TO) {
2572 err = SN_ERR_SRVTO;
2573 fin = SN_FINST_Q;
2574 }
2575 else if (err_type & SI_ET_QUEUE_ERR) {
2576 err = SN_ERR_SRVCL;
2577 fin = SN_FINST_Q;
2578 }
2579 else if (err_type & SI_ET_CONN_TO) {
2580 err = SN_ERR_SRVTO;
2581 fin = SN_FINST_C;
2582 }
2583 else if (err_type & SI_ET_CONN_ERR) {
2584 err = SN_ERR_SRVCL;
2585 fin = SN_FINST_C;
2586 }
2587 else /* SI_ET_CONN_OTHER and others */ {
2588 err = SN_ERR_INTERNAL;
2589 fin = SN_FINST_C;
2590 }
2591
2592 if (!(s->flags & SN_ERR_MASK))
2593 s->flags |= err;
2594 if (!(s->flags & SN_FINST_MASK))
2595 s->flags |= fin;
2596}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002597
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002598/* kill a session and set the termination flags to <why> (one of SN_ERR_*) */
2599void session_shutdown(struct session *session, int why)
2600{
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002601 if (session->req->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002602 return;
2603
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002604 channel_shutw_now(session->req);
2605 channel_shutr_now(session->rep);
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002606 session->task->nice = 1024;
2607 if (!(session->flags & SN_ERR_MASK))
2608 session->flags |= why;
2609 task_wakeup(session->task, TASK_WOKEN_OTHER);
2610}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002611
Willy Tarreau8b22a712010-06-18 17:46:06 +02002612/************************************************************************/
2613/* All supported ACL keywords must be declared here. */
2614/************************************************************************/
2615
Willy Tarreaua5e37562011-12-16 17:06:15 +01002616/* set temp integer to the General Purpose Counter 0 value in the stksess entry <ts> */
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002617static int
Willy Tarreau37406352012-04-23 16:16:37 +02002618acl_fetch_get_gpc0(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002619{
Willy Tarreau37406352012-04-23 16:16:37 +02002620 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002621 smp->type = SMP_T_UINT;
2622 smp->data.uint = 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002623 if (ts != NULL) {
2624 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2625 if (!ptr)
2626 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002627 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002628 }
2629 return 1;
2630}
2631
Willy Tarreaua5e37562011-12-16 17:06:15 +01002632/* set temp integer to the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002633 * frontend counters.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002634 */
2635static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002636acl_fetch_sc1_get_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002637 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002638{
Willy Tarreau56123282010-08-06 19:06:56 +02002639 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002640 return 0;
Willy Tarreau37406352012-04-23 16:16:37 +02002641 return acl_fetch_get_gpc0(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002642}
2643
Willy Tarreaua5e37562011-12-16 17:06:15 +01002644/* set temp integer to the General Purpose Counter 0 value from the session's tracked
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002645 * backend counters.
2646 */
2647static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002648acl_fetch_sc2_get_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002649 const struct arg *args, struct sample *smp)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002650{
Willy Tarreau56123282010-08-06 19:06:56 +02002651 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002652 return 0;
Willy Tarreau37406352012-04-23 16:16:37 +02002653 return acl_fetch_get_gpc0(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002654}
2655
Willy Tarreaua5e37562011-12-16 17:06:15 +01002656/* set temp integer to the General Purpose Counter 0 value from the session's source
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002657 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002658 * Accepts exactly 1 argument of type table.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002659 */
2660static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002661acl_fetch_src_get_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002662 const struct arg *args, struct sample *smp)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002663{
2664 struct stktable_key *key;
2665
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002666 key = addr_to_stktable_key(&l4->si[0].conn->addr.from);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002667 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002668 return 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002669
Willy Tarreau24e32d82012-04-23 23:55:44 +02002670 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002671 return acl_fetch_get_gpc0(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002672}
2673
2674/* Increment the General Purpose Counter 0 value in the stksess entry <ts> and
Willy Tarreaua5e37562011-12-16 17:06:15 +01002675 * return it into temp integer.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002676 */
2677static int
Willy Tarreau37406352012-04-23 16:16:37 +02002678acl_fetch_inc_gpc0(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002679{
Willy Tarreau37406352012-04-23 16:16:37 +02002680 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002681 smp->type = SMP_T_UINT;
2682 smp->data.uint = 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002683 if (ts != NULL) {
2684 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2685 if (!ptr)
2686 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002687 smp->data.uint = ++stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002688 }
2689 return 1;
2690}
2691
2692/* Increment the General Purpose Counter 0 value from the session's tracked
Willy Tarreaua5e37562011-12-16 17:06:15 +01002693 * frontend counters and return it into temp integer.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002694 */
2695static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002696acl_fetch_sc1_inc_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002697 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002698{
Willy Tarreau56123282010-08-06 19:06:56 +02002699 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002700 return 0;
Willy Tarreau37406352012-04-23 16:16:37 +02002701 return acl_fetch_inc_gpc0(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002702}
2703
2704/* Increment the General Purpose Counter 0 value from the session's tracked
Willy Tarreaua5e37562011-12-16 17:06:15 +01002705 * backend counters and return it into temp integer.
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002706 */
2707static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002708acl_fetch_sc2_inc_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002709 const struct arg *args, struct sample *smp)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002710{
Willy Tarreau56123282010-08-06 19:06:56 +02002711 if (!l4->stkctr2_entry)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002712 return 0;
Willy Tarreau37406352012-04-23 16:16:37 +02002713 return acl_fetch_inc_gpc0(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002714}
2715
2716/* Increment the General Purpose Counter 0 value from the session's source
Willy Tarreaua5e37562011-12-16 17:06:15 +01002717 * address in the table pointed to by expr, and return it into temp integer.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002718 * Accepts exactly 1 argument of type table.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002719 */
2720static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002721acl_fetch_src_inc_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002722 const struct arg *args, struct sample *smp)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002723{
2724 struct stktable_key *key;
2725
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002726 key = addr_to_stktable_key(&l4->si[0].conn->addr.from);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002727 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002728 return 0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002729
Willy Tarreau24e32d82012-04-23 23:55:44 +02002730 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002731 return acl_fetch_inc_gpc0(&px->table, smp, stktable_update_key(&px->table, key));
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002732}
2733
Willy Tarreauf73cd112011-08-13 01:45:16 +02002734/* Clear the General Purpose Counter 0 value in the stksess entry <ts> and
Willy Tarreaua5e37562011-12-16 17:06:15 +01002735 * return its previous value into temp integer.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002736 */
2737static int
Willy Tarreau37406352012-04-23 16:16:37 +02002738acl_fetch_clr_gpc0(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002739{
Willy Tarreau37406352012-04-23 16:16:37 +02002740 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002741 smp->type = SMP_T_UINT;
2742 smp->data.uint = 0;
Willy Tarreauf73cd112011-08-13 01:45:16 +02002743 if (ts != NULL) {
2744 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_GPC0);
2745 if (!ptr)
2746 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002747 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002748 stktable_data_cast(ptr, gpc0) = 0;
2749 }
2750 return 1;
2751}
2752
2753/* Clear the General Purpose Counter 0 value from the session's tracked
Willy Tarreaua5e37562011-12-16 17:06:15 +01002754 * frontend counters and return its previous value into temp integer.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002755 */
2756static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002757acl_fetch_sc1_clr_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002758 const struct arg *args, struct sample *smp)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002759{
2760 if (!l4->stkctr1_entry)
2761 return 0;
Willy Tarreau37406352012-04-23 16:16:37 +02002762 return acl_fetch_clr_gpc0(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002763}
2764
2765/* Clear the General Purpose Counter 0 value from the session's tracked
Willy Tarreaua5e37562011-12-16 17:06:15 +01002766 * backend counters and return its previous value into temp integer.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002767 */
2768static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002769acl_fetch_sc2_clr_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002770 const struct arg *args, struct sample *smp)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002771{
2772 if (!l4->stkctr2_entry)
2773 return 0;
Willy Tarreau37406352012-04-23 16:16:37 +02002774 return acl_fetch_clr_gpc0(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002775}
2776
2777/* Clear the General Purpose Counter 0 value from the session's source address
Willy Tarreaua5e37562011-12-16 17:06:15 +01002778 * in the table pointed to by expr, and return its previous value into temp integer.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002779 * Accepts exactly 1 argument of type table.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002780 */
2781static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002782acl_fetch_src_clr_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002783 const struct arg *args, struct sample *smp)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002784{
2785 struct stktable_key *key;
2786
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002787 key = addr_to_stktable_key(&l4->si[0].conn->addr.from);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002788 if (!key)
2789 return 0;
2790
Willy Tarreau24e32d82012-04-23 23:55:44 +02002791 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002792 return acl_fetch_clr_gpc0(&px->table, smp, stktable_update_key(&px->table, key));
Willy Tarreauf73cd112011-08-13 01:45:16 +02002793}
2794
Willy Tarreaua5e37562011-12-16 17:06:15 +01002795/* set temp integer to the cumulated number of connections in the stksess entry <ts> */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002796static int
Willy Tarreau37406352012-04-23 16:16:37 +02002797acl_fetch_conn_cnt(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002798{
Willy Tarreau37406352012-04-23 16:16:37 +02002799 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002800 smp->type = SMP_T_UINT;
2801 smp->data.uint = 0;
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002802 if (ts != NULL) {
2803 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CNT);
2804 if (!ptr)
2805 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002806 smp->data.uint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002807 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002808 return 1;
2809}
2810
Willy Tarreaua5e37562011-12-16 17:06:15 +01002811/* set temp integer to the cumulated number of connections from the session's tracked FE counters */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002812static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002813acl_fetch_sc1_conn_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002814 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002815{
Willy Tarreau56123282010-08-06 19:06:56 +02002816 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002817 return 0;
2818
Willy Tarreau37406352012-04-23 16:16:37 +02002819 return acl_fetch_conn_cnt(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002820}
2821
Willy Tarreaua5e37562011-12-16 17:06:15 +01002822/* set temp integer to the cumulated number of connections from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002823static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002824acl_fetch_sc2_conn_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002825 const struct arg *args, struct sample *smp)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002826{
Willy Tarreau56123282010-08-06 19:06:56 +02002827 if (!l4->stkctr2_entry)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002828 return 0;
2829
Willy Tarreau37406352012-04-23 16:16:37 +02002830 return acl_fetch_conn_cnt(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002831}
2832
Willy Tarreaua5e37562011-12-16 17:06:15 +01002833/* set temp integer to the cumulated number of connections from the session's source
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002834 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002835 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002836 */
2837static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002838acl_fetch_src_conn_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002839 const struct arg *args, struct sample *smp)
Willy Tarreau8b22a712010-06-18 17:46:06 +02002840{
Willy Tarreau8b22a712010-06-18 17:46:06 +02002841 struct stktable_key *key;
2842
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002843 key = addr_to_stktable_key(&l4->si[0].conn->addr.from);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002844 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002845 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002846
Willy Tarreau24e32d82012-04-23 23:55:44 +02002847 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002848 return acl_fetch_conn_cnt(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau8b22a712010-06-18 17:46:06 +02002849}
2850
Willy Tarreaua5e37562011-12-16 17:06:15 +01002851/* set temp integer to the connection rate in the stksess entry <ts> over the configured period */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002852static int
Willy Tarreau37406352012-04-23 16:16:37 +02002853acl_fetch_conn_rate(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002854{
Willy Tarreau37406352012-04-23 16:16:37 +02002855 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002856 smp->type = SMP_T_UINT;
2857 smp->data.uint = 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002858 if (ts != NULL) {
2859 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_RATE);
2860 if (!ptr)
2861 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002862 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreau91c43d72010-06-20 11:19:22 +02002863 table->data_arg[STKTABLE_DT_CONN_RATE].u);
2864 }
2865 return 1;
2866}
2867
Willy Tarreaua5e37562011-12-16 17:06:15 +01002868/* set temp integer to the connection rate from the session's tracked FE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02002869 * the configured period.
2870 */
2871static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002872acl_fetch_sc1_conn_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002873 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002874{
Willy Tarreau56123282010-08-06 19:06:56 +02002875 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002876 return 0;
2877
Willy Tarreau37406352012-04-23 16:16:37 +02002878 return acl_fetch_conn_rate(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002879}
2880
Willy Tarreaua5e37562011-12-16 17:06:15 +01002881/* set temp integer to the connection rate from the session's tracked BE counters over
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002882 * the configured period.
2883 */
2884static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002885acl_fetch_sc2_conn_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002886 const struct arg *args, struct sample *smp)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002887{
Willy Tarreau56123282010-08-06 19:06:56 +02002888 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002889 return 0;
2890
Willy Tarreau37406352012-04-23 16:16:37 +02002891 return acl_fetch_conn_rate(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002892}
2893
Willy Tarreaua5e37562011-12-16 17:06:15 +01002894/* set temp integer to the connection rate from the session's source address in the
Willy Tarreau91c43d72010-06-20 11:19:22 +02002895 * table pointed to by expr, over the configured period.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002896 * Accepts exactly 1 argument of type table.
Willy Tarreau91c43d72010-06-20 11:19:22 +02002897 */
2898static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002899acl_fetch_src_conn_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002900 const struct arg *args, struct sample *smp)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002901{
2902 struct stktable_key *key;
2903
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002904 key = addr_to_stktable_key(&l4->si[0].conn->addr.from);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002905 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002906 return 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02002907
Willy Tarreau24e32d82012-04-23 23:55:44 +02002908 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002909 return acl_fetch_conn_rate(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau91c43d72010-06-20 11:19:22 +02002910}
2911
Willy Tarreaua5e37562011-12-16 17:06:15 +01002912/* set temp integer to the number of connections from the session's source address
Willy Tarreau8b22a712010-06-18 17:46:06 +02002913 * in the table pointed to by expr, after updating it.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002914 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002915 */
2916static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002917acl_fetch_src_updt_conn_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002918 const struct arg *args, struct sample *smp)
Willy Tarreau8b22a712010-06-18 17:46:06 +02002919{
2920 struct stksess *ts;
2921 struct stktable_key *key;
2922 void *ptr;
2923
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002924 key = addr_to_stktable_key(&l4->si[0].conn->addr.from);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002925 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002926 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002927
Willy Tarreau24e32d82012-04-23 23:55:44 +02002928 px = args->data.prx;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002929
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002930 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2931 /* entry does not exist and could not be created */
2932 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002933
2934 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2935 if (!ptr)
2936 return 0; /* parameter not stored in this table */
2937
Willy Tarreauf853c462012-04-23 18:53:56 +02002938 smp->type = SMP_T_UINT;
2939 smp->data.uint = ++stktable_data_cast(ptr, conn_cnt);
Willy Tarreau37406352012-04-23 16:16:37 +02002940 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002941 return 1;
2942}
2943
Willy Tarreaua5e37562011-12-16 17:06:15 +01002944/* set temp integer to the number of concurrent connections in the stksess entry <ts> */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002945static int
Willy Tarreau37406352012-04-23 16:16:37 +02002946acl_fetch_conn_cur(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002947{
Willy Tarreau37406352012-04-23 16:16:37 +02002948 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002949 smp->type = SMP_T_UINT;
2950 smp->data.uint = 0;
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002951
2952 if (ts != NULL) {
2953 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_CONN_CUR);
2954 if (!ptr)
2955 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002956 smp->data.uint = stktable_data_cast(ptr, conn_cur);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002957 }
2958 return 1;
2959}
2960
Willy Tarreaua5e37562011-12-16 17:06:15 +01002961/* set temp integer to the number of concurrent connections from the session's tracked FE counters */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002962static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002963acl_fetch_sc1_conn_cur(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002964 const struct arg *args, struct sample *smp)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002965{
Willy Tarreau56123282010-08-06 19:06:56 +02002966 if (!l4->stkctr1_entry)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002967 return 0;
2968
Willy Tarreau37406352012-04-23 16:16:37 +02002969 return acl_fetch_conn_cur(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002970}
2971
Willy Tarreaua5e37562011-12-16 17:06:15 +01002972/* set temp integer to the number of concurrent connections from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002973static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002974acl_fetch_sc2_conn_cur(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002975 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002976{
Willy Tarreau56123282010-08-06 19:06:56 +02002977 if (!l4->stkctr2_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002978 return 0;
2979
Willy Tarreau37406352012-04-23 16:16:37 +02002980 return acl_fetch_conn_cur(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02002981}
2982
Willy Tarreaua5e37562011-12-16 17:06:15 +01002983/* set temp integer to the number of concurrent connections from the session's source
Willy Tarreau38285c12010-06-18 16:35:43 +02002984 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002985 * Accepts exactly 1 argument of type table.
Willy Tarreau38285c12010-06-18 16:35:43 +02002986 */
2987static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002988acl_fetch_src_conn_cur(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02002989 const struct arg *args, struct sample *smp)
Willy Tarreau38285c12010-06-18 16:35:43 +02002990{
Willy Tarreau38285c12010-06-18 16:35:43 +02002991 struct stktable_key *key;
2992
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002993 key = addr_to_stktable_key(&l4->si[0].conn->addr.from);
Willy Tarreau38285c12010-06-18 16:35:43 +02002994 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002995 return 0;
Willy Tarreau38285c12010-06-18 16:35:43 +02002996
Willy Tarreau24e32d82012-04-23 23:55:44 +02002997 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02002998 return acl_fetch_conn_cur(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau38285c12010-06-18 16:35:43 +02002999}
3000
Willy Tarreaua5e37562011-12-16 17:06:15 +01003001/* set temp integer to the cumulated number of sessions in the stksess entry <ts> */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003002static int
Willy Tarreau37406352012-04-23 16:16:37 +02003003acl_fetch_sess_cnt(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003004{
Willy Tarreau37406352012-04-23 16:16:37 +02003005 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003006 smp->type = SMP_T_UINT;
3007 smp->data.uint = 0;
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003008 if (ts != NULL) {
3009 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_CNT);
3010 if (!ptr)
3011 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003012 smp->data.uint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003013 }
3014 return 1;
3015}
3016
Willy Tarreaua5e37562011-12-16 17:06:15 +01003017/* set temp integer to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003018static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003019acl_fetch_sc1_sess_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003020 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003021{
Willy Tarreau56123282010-08-06 19:06:56 +02003022 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003023 return 0;
3024
Willy Tarreau37406352012-04-23 16:16:37 +02003025 return acl_fetch_sess_cnt(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003026}
3027
Willy Tarreaua5e37562011-12-16 17:06:15 +01003028/* set temp integer to the cumulated number of sessions from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003029static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003030acl_fetch_sc2_sess_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003031 const struct arg *args, struct sample *smp)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003032{
Willy Tarreau56123282010-08-06 19:06:56 +02003033 if (!l4->stkctr2_entry)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003034 return 0;
3035
Willy Tarreau37406352012-04-23 16:16:37 +02003036 return acl_fetch_sess_cnt(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003037}
3038
Willy Tarreaua5e37562011-12-16 17:06:15 +01003039/* set temp integer to the cumulated number of session from the session's source
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003040 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003041 * Accepts exactly 1 argument of type table.
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003042 */
3043static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003044acl_fetch_src_sess_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003045 const struct arg *args, struct sample *smp)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003046{
3047 struct stktable_key *key;
3048
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003049 key = addr_to_stktable_key(&l4->si[0].conn->addr.from);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003050 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003051 return 0;
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003052
Willy Tarreau24e32d82012-04-23 23:55:44 +02003053 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003054 return acl_fetch_sess_cnt(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003055}
3056
Willy Tarreaua5e37562011-12-16 17:06:15 +01003057/* set temp integer to the session rate in the stksess entry <ts> over the configured period */
Willy Tarreau91c43d72010-06-20 11:19:22 +02003058static int
Willy Tarreau37406352012-04-23 16:16:37 +02003059acl_fetch_sess_rate(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau91c43d72010-06-20 11:19:22 +02003060{
Willy Tarreau37406352012-04-23 16:16:37 +02003061 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003062 smp->type = SMP_T_UINT;
3063 smp->data.uint = 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02003064 if (ts != NULL) {
3065 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_SESS_RATE);
3066 if (!ptr)
3067 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003068 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau91c43d72010-06-20 11:19:22 +02003069 table->data_arg[STKTABLE_DT_SESS_RATE].u);
3070 }
3071 return 1;
3072}
3073
Willy Tarreaua5e37562011-12-16 17:06:15 +01003074/* set temp integer to the session rate from the session's tracked FE counters over
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003075 * the configured period.
3076 */
3077static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003078acl_fetch_sc1_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003079 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003080{
Willy Tarreau56123282010-08-06 19:06:56 +02003081 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003082 return 0;
3083
Willy Tarreau37406352012-04-23 16:16:37 +02003084 return acl_fetch_sess_rate(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003085}
3086
Willy Tarreaua5e37562011-12-16 17:06:15 +01003087/* set temp integer to the session rate from the session's tracked BE counters over
Willy Tarreau91c43d72010-06-20 11:19:22 +02003088 * the configured period.
3089 */
3090static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003091acl_fetch_sc2_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003092 const struct arg *args, struct sample *smp)
Willy Tarreau91c43d72010-06-20 11:19:22 +02003093{
Willy Tarreau56123282010-08-06 19:06:56 +02003094 if (!l4->stkctr2_entry)
Willy Tarreau91c43d72010-06-20 11:19:22 +02003095 return 0;
3096
Willy Tarreau37406352012-04-23 16:16:37 +02003097 return acl_fetch_sess_rate(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003098}
3099
Willy Tarreaua5e37562011-12-16 17:06:15 +01003100/* set temp integer to the session rate from the session's source address in the
Willy Tarreau91c43d72010-06-20 11:19:22 +02003101 * table pointed to by expr, over the configured period.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003102 * Accepts exactly 1 argument of type table.
Willy Tarreau91c43d72010-06-20 11:19:22 +02003103 */
3104static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003105acl_fetch_src_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003106 const struct arg *args, struct sample *smp)
Willy Tarreau91c43d72010-06-20 11:19:22 +02003107{
3108 struct stktable_key *key;
3109
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003110 key = addr_to_stktable_key(&l4->si[0].conn->addr.from);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003111 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003112 return 0;
Willy Tarreau91c43d72010-06-20 11:19:22 +02003113
Willy Tarreau24e32d82012-04-23 23:55:44 +02003114 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003115 return acl_fetch_sess_rate(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau91c43d72010-06-20 11:19:22 +02003116}
3117
Willy Tarreaua5e37562011-12-16 17:06:15 +01003118/* set temp integer to the cumulated number of sessions in the stksess entry <ts> */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003119static int
Willy Tarreau37406352012-04-23 16:16:37 +02003120acl_fetch_http_req_cnt(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003121{
Willy Tarreau37406352012-04-23 16:16:37 +02003122 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003123 smp->type = SMP_T_UINT;
3124 smp->data.uint = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003125 if (ts != NULL) {
3126 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_CNT);
3127 if (!ptr)
3128 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003129 smp->data.uint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003130 }
3131 return 1;
3132}
3133
Willy Tarreaua5e37562011-12-16 17:06:15 +01003134/* set temp integer to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003135static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003136acl_fetch_sc1_http_req_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003137 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003138{
Willy Tarreau56123282010-08-06 19:06:56 +02003139 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003140 return 0;
3141
Willy Tarreau37406352012-04-23 16:16:37 +02003142 return acl_fetch_http_req_cnt(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003143}
3144
Willy Tarreaua5e37562011-12-16 17:06:15 +01003145/* set temp integer to the cumulated number of sessions from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003146static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003147acl_fetch_sc2_http_req_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003148 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003149{
Willy Tarreau56123282010-08-06 19:06:56 +02003150 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003151 return 0;
3152
Willy Tarreau37406352012-04-23 16:16:37 +02003153 return acl_fetch_http_req_cnt(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003154}
3155
Willy Tarreaua5e37562011-12-16 17:06:15 +01003156/* set temp integer to the cumulated number of session from the session's source
Willy Tarreauda7ff642010-06-23 11:44:09 +02003157 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003158 * Accepts exactly 1 argument of type table.
Willy Tarreauda7ff642010-06-23 11:44:09 +02003159 */
3160static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003161acl_fetch_src_http_req_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003162 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003163{
3164 struct stktable_key *key;
3165
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003166 key = addr_to_stktable_key(&l4->si[0].conn->addr.from);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003167 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003168 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003169
Willy Tarreau24e32d82012-04-23 23:55:44 +02003170 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003171 return acl_fetch_http_req_cnt(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003172}
3173
Willy Tarreaua5e37562011-12-16 17:06:15 +01003174/* set temp integer to the session rate in the stksess entry <ts> over the configured period */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003175static int
Willy Tarreau37406352012-04-23 16:16:37 +02003176acl_fetch_http_req_rate(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003177{
Willy Tarreau37406352012-04-23 16:16:37 +02003178 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003179 smp->type = SMP_T_UINT;
3180 smp->data.uint = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003181 if (ts != NULL) {
3182 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_REQ_RATE);
3183 if (!ptr)
3184 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003185 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreauda7ff642010-06-23 11:44:09 +02003186 table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
3187 }
3188 return 1;
3189}
3190
Willy Tarreaua5e37562011-12-16 17:06:15 +01003191/* set temp integer to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02003192 * the configured period.
3193 */
3194static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003195acl_fetch_sc1_http_req_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003196 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003197{
Willy Tarreau56123282010-08-06 19:06:56 +02003198 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003199 return 0;
3200
Willy Tarreau37406352012-04-23 16:16:37 +02003201 return acl_fetch_http_req_rate(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003202}
3203
Willy Tarreaua5e37562011-12-16 17:06:15 +01003204/* set temp integer to the session rate from the session's tracked BE counters over
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003205 * the configured period.
3206 */
3207static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003208acl_fetch_sc2_http_req_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003209 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003210{
Willy Tarreau56123282010-08-06 19:06:56 +02003211 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003212 return 0;
3213
Willy Tarreau37406352012-04-23 16:16:37 +02003214 return acl_fetch_http_req_rate(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003215}
3216
Willy Tarreaua5e37562011-12-16 17:06:15 +01003217/* set temp integer to the session rate from the session's source address in the
Willy Tarreauda7ff642010-06-23 11:44:09 +02003218 * table pointed to by expr, over the configured period.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003219 * Accepts exactly 1 argument of type table.
Willy Tarreauda7ff642010-06-23 11:44:09 +02003220 */
3221static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003222acl_fetch_src_http_req_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003223 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003224{
3225 struct stktable_key *key;
3226
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003227 key = addr_to_stktable_key(&l4->si[0].conn->addr.from);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003228 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003229 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003230
Willy Tarreau24e32d82012-04-23 23:55:44 +02003231 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003232 return acl_fetch_http_req_rate(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003233}
3234
Willy Tarreaua5e37562011-12-16 17:06:15 +01003235/* set temp integer to the cumulated number of sessions in the stksess entry <ts> */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003236static int
Willy Tarreau37406352012-04-23 16:16:37 +02003237acl_fetch_http_err_cnt(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003238{
Willy Tarreau37406352012-04-23 16:16:37 +02003239 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003240 smp->type = SMP_T_UINT;
3241 smp->data.uint = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003242 if (ts != NULL) {
3243 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_CNT);
3244 if (!ptr)
3245 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003246 smp->data.uint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003247 }
3248 return 1;
3249}
3250
Willy Tarreaua5e37562011-12-16 17:06:15 +01003251/* set temp integer to the cumulated number of sessions from the session's tracked FE counters */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003252static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003253acl_fetch_sc1_http_err_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003254 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003255{
Willy Tarreau56123282010-08-06 19:06:56 +02003256 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003257 return 0;
3258
Willy Tarreau37406352012-04-23 16:16:37 +02003259 return acl_fetch_http_err_cnt(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003260}
3261
Willy Tarreaua5e37562011-12-16 17:06:15 +01003262/* set temp integer to the cumulated number of sessions from the session's tracked BE counters */
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003263static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003264acl_fetch_sc2_http_err_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003265 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003266{
Willy Tarreau56123282010-08-06 19:06:56 +02003267 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003268 return 0;
3269
Willy Tarreau37406352012-04-23 16:16:37 +02003270 return acl_fetch_http_err_cnt(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003271}
3272
Willy Tarreaua5e37562011-12-16 17:06:15 +01003273/* set temp integer to the cumulated number of session from the session's source
Willy Tarreauda7ff642010-06-23 11:44:09 +02003274 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003275 * Accepts exactly 1 argument of type table.
Willy Tarreauda7ff642010-06-23 11:44:09 +02003276 */
3277static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003278acl_fetch_src_http_err_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003279 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003280{
3281 struct stktable_key *key;
3282
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003283 key = addr_to_stktable_key(&l4->si[0].conn->addr.from);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003284 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003285 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003286
Willy Tarreau24e32d82012-04-23 23:55:44 +02003287 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003288 return acl_fetch_http_err_cnt(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003289}
3290
Willy Tarreaua5e37562011-12-16 17:06:15 +01003291/* set temp integer to the session rate in the stksess entry <ts> over the configured period */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003292static int
Willy Tarreau37406352012-04-23 16:16:37 +02003293acl_fetch_http_err_rate(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003294{
Willy Tarreau37406352012-04-23 16:16:37 +02003295 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003296 smp->type = SMP_T_UINT;
3297 smp->data.uint = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003298 if (ts != NULL) {
3299 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_HTTP_ERR_RATE);
3300 if (!ptr)
3301 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003302 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreauda7ff642010-06-23 11:44:09 +02003303 table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
3304 }
3305 return 1;
3306}
3307
Willy Tarreaua5e37562011-12-16 17:06:15 +01003308/* set temp integer to the session rate from the session's tracked FE counters over
Willy Tarreauda7ff642010-06-23 11:44:09 +02003309 * the configured period.
3310 */
3311static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003312acl_fetch_sc1_http_err_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003313 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003314{
Willy Tarreau56123282010-08-06 19:06:56 +02003315 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003316 return 0;
3317
Willy Tarreau37406352012-04-23 16:16:37 +02003318 return acl_fetch_http_err_rate(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003319}
3320
Willy Tarreaua5e37562011-12-16 17:06:15 +01003321/* set temp integer to the session rate from the session's tracked BE counters over
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003322 * the configured period.
3323 */
3324static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003325acl_fetch_sc2_http_err_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003326 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003327{
Willy Tarreau56123282010-08-06 19:06:56 +02003328 if (!l4->stkctr2_entry)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003329 return 0;
3330
Willy Tarreau37406352012-04-23 16:16:37 +02003331 return acl_fetch_http_err_rate(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003332}
3333
Willy Tarreaua5e37562011-12-16 17:06:15 +01003334/* set temp integer to the session rate from the session's source address in the
Willy Tarreauda7ff642010-06-23 11:44:09 +02003335 * table pointed to by expr, over the configured period.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003336 * Accepts exactly 1 argument of type table.
Willy Tarreauda7ff642010-06-23 11:44:09 +02003337 */
3338static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003339acl_fetch_src_http_err_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003340 const struct arg *args, struct sample *smp)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003341{
3342 struct stktable_key *key;
3343
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003344 key = addr_to_stktable_key(&l4->si[0].conn->addr.from);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003345 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003346 return 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003347
Willy Tarreau24e32d82012-04-23 23:55:44 +02003348 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003349 return acl_fetch_http_err_rate(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003350}
3351
Willy Tarreaua5e37562011-12-16 17:06:15 +01003352/* set temp integer to the number of kbytes received from clients matching the stksess entry <ts> */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003353static int
Willy Tarreau37406352012-04-23 16:16:37 +02003354acl_fetch_kbytes_in(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003355{
Willy Tarreau37406352012-04-23 16:16:37 +02003356 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003357 smp->type = SMP_T_UINT;
3358 smp->data.uint = 0;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003359
3360 if (ts != NULL) {
3361 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_CNT);
3362 if (!ptr)
3363 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003364 smp->data.uint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003365 }
3366 return 1;
3367}
3368
Willy Tarreaua5e37562011-12-16 17:06:15 +01003369/* set temp integer to the number of kbytes received from clients according to the
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003370 * session's tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003371 */
3372static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003373acl_fetch_sc1_kbytes_in(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003374 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003375{
Willy Tarreau56123282010-08-06 19:06:56 +02003376 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003377 return 0;
3378
Willy Tarreau37406352012-04-23 16:16:37 +02003379 return acl_fetch_kbytes_in(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003380}
3381
Willy Tarreaua5e37562011-12-16 17:06:15 +01003382/* set temp integer to the number of kbytes received from clients according to the
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003383 * session's tracked BE counters.
3384 */
3385static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003386acl_fetch_sc2_kbytes_in(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003387 const struct arg *args, struct sample *smp)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003388{
Willy Tarreau56123282010-08-06 19:06:56 +02003389 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003390 return 0;
3391
Willy Tarreau37406352012-04-23 16:16:37 +02003392 return acl_fetch_kbytes_in(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003393}
3394
Willy Tarreaua5e37562011-12-16 17:06:15 +01003395/* set temp integer to the number of kbytes received from the session's source
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003396 * address in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003397 * Accepts exactly 1 argument of type table.
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003398 */
3399static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003400acl_fetch_src_kbytes_in(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003401 const struct arg *args, struct sample *smp)
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003402{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003403 struct stktable_key *key;
3404
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003405 key = addr_to_stktable_key(&l4->si[0].conn->addr.from);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003406 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003407 return 0;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003408
Willy Tarreau24e32d82012-04-23 23:55:44 +02003409 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003410 return acl_fetch_kbytes_in(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003411}
3412
Willy Tarreaua5e37562011-12-16 17:06:15 +01003413/* set temp integer to the bytes rate from clients in the stksess entry <ts> over the
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003414 * configured period.
3415 */
3416static int
Willy Tarreau37406352012-04-23 16:16:37 +02003417acl_fetch_bytes_in_rate(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003418{
Willy Tarreau37406352012-04-23 16:16:37 +02003419 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003420 smp->type = SMP_T_UINT;
3421 smp->data.uint = 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003422 if (ts != NULL) {
3423 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_IN_RATE);
3424 if (!ptr)
3425 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003426 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003427 table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
3428 }
3429 return 1;
3430}
3431
Willy Tarreaua5e37562011-12-16 17:06:15 +01003432/* set temp integer to the bytes rate from clients from the session's tracked FE
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003433 * counters over the configured period.
3434 */
3435static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003436acl_fetch_sc1_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003437 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003438{
Willy Tarreau56123282010-08-06 19:06:56 +02003439 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003440 return 0;
3441
Willy Tarreau37406352012-04-23 16:16:37 +02003442 return acl_fetch_bytes_in_rate(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003443}
3444
Willy Tarreaua5e37562011-12-16 17:06:15 +01003445/* set temp integer to the bytes rate from clients from the session's tracked BE
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003446 * counters over the configured period.
3447 */
3448static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003449acl_fetch_sc2_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003450 const struct arg *args, struct sample *smp)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003451{
Willy Tarreau56123282010-08-06 19:06:56 +02003452 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003453 return 0;
3454
Willy Tarreau37406352012-04-23 16:16:37 +02003455 return acl_fetch_bytes_in_rate(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003456}
3457
Willy Tarreaua5e37562011-12-16 17:06:15 +01003458/* set temp integer to the bytes rate from clients from the session's source address
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003459 * in the table pointed to by expr, over the configured period.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003460 * Accepts exactly 1 argument of type table.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003461 */
3462static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003463acl_fetch_src_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003464 const struct arg *args, struct sample *smp)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003465{
3466 struct stktable_key *key;
3467
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003468 key = addr_to_stktable_key(&l4->si[0].conn->addr.from);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003469 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003470 return 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003471
Willy Tarreau24e32d82012-04-23 23:55:44 +02003472 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003473 return acl_fetch_bytes_in_rate(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003474}
3475
Willy Tarreaua5e37562011-12-16 17:06:15 +01003476/* set temp integer to the number of kbytes sent to clients matching the stksess entry <ts> */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003477static int
Willy Tarreau37406352012-04-23 16:16:37 +02003478acl_fetch_kbytes_out(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003479{
Willy Tarreau37406352012-04-23 16:16:37 +02003480 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003481 smp->type = SMP_T_UINT;
3482 smp->data.uint = 0;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003483
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003484 if (ts != NULL) {
3485 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003486 if (!ptr)
3487 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003488 smp->data.uint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003489 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003490 return 1;
3491}
3492
Willy Tarreaua5e37562011-12-16 17:06:15 +01003493/* set temp integer to the number of kbytes sent to clients according to the session's
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003494 * tracked FE counters.
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003495 */
3496static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003497acl_fetch_sc1_kbytes_out(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003498 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003499{
Willy Tarreau56123282010-08-06 19:06:56 +02003500 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003501 return 0;
3502
Willy Tarreau37406352012-04-23 16:16:37 +02003503 return acl_fetch_kbytes_out(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003504}
3505
Willy Tarreaua5e37562011-12-16 17:06:15 +01003506/* set temp integer to the number of kbytes sent to clients according to the session's
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003507 * tracked BE counters.
3508 */
3509static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003510acl_fetch_sc2_kbytes_out(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003511 const struct arg *args, struct sample *smp)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003512{
Willy Tarreau56123282010-08-06 19:06:56 +02003513 if (!l4->stkctr2_entry)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003514 return 0;
3515
Willy Tarreau37406352012-04-23 16:16:37 +02003516 return acl_fetch_kbytes_out(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003517}
3518
Willy Tarreaua5e37562011-12-16 17:06:15 +01003519/* set temp integer to the number of kbytes sent to the session's source address in
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003520 * the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003521 * Accepts exactly 1 argument of type table.
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003522 */
3523static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003524acl_fetch_src_kbytes_out(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003525 const struct arg *args, struct sample *smp)
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003526{
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003527 struct stktable_key *key;
3528
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003529 key = addr_to_stktable_key(&l4->si[0].conn->addr.from);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003530 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003531 return 0;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003532
Willy Tarreau24e32d82012-04-23 23:55:44 +02003533 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003534 return acl_fetch_kbytes_out(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003535}
3536
Willy Tarreaua5e37562011-12-16 17:06:15 +01003537/* set temp integer to the bytes rate to clients in the stksess entry <ts> over the
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003538 * configured period.
3539 */
3540static int
Willy Tarreau37406352012-04-23 16:16:37 +02003541acl_fetch_bytes_out_rate(struct stktable *table, struct sample *smp, struct stksess *ts)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003542{
Willy Tarreau37406352012-04-23 16:16:37 +02003543 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003544 smp->type = SMP_T_UINT;
3545 smp->data.uint = 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003546 if (ts != NULL) {
3547 void *ptr = stktable_data_ptr(table, ts, STKTABLE_DT_BYTES_OUT_RATE);
3548 if (!ptr)
3549 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003550 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003551 table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
3552 }
3553 return 1;
3554}
3555
Willy Tarreaua5e37562011-12-16 17:06:15 +01003556/* set temp integer to the bytes rate to clients from the session's tracked FE counters
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003557 * over the configured period.
3558 */
3559static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003560acl_fetch_sc1_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003561 const struct arg *args, struct sample *smp)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003562{
Willy Tarreau56123282010-08-06 19:06:56 +02003563 if (!l4->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003564 return 0;
3565
Willy Tarreau37406352012-04-23 16:16:37 +02003566 return acl_fetch_bytes_out_rate(l4->stkctr1_table, smp, l4->stkctr1_entry);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003567}
3568
Willy Tarreaua5e37562011-12-16 17:06:15 +01003569/* set temp integer to the bytes rate to clients from the session's tracked BE counters
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003570 * over the configured period.
3571 */
3572static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003573acl_fetch_sc2_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003574 const struct arg *args, struct sample *smp)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003575{
Willy Tarreau56123282010-08-06 19:06:56 +02003576 if (!l4->stkctr2_entry)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003577 return 0;
3578
Willy Tarreau37406352012-04-23 16:16:37 +02003579 return acl_fetch_bytes_out_rate(l4->stkctr2_table, smp, l4->stkctr2_entry);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003580}
3581
Willy Tarreaua5e37562011-12-16 17:06:15 +01003582/* set temp integer to the bytes rate to client from the session's source address in
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003583 * the table pointed to by expr, over the configured period.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003584 * Accepts exactly 1 argument of type table.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003585 */
3586static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003587acl_fetch_src_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003588 const struct arg *args, struct sample *smp)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003589{
3590 struct stktable_key *key;
3591
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003592 key = addr_to_stktable_key(&l4->si[0].conn->addr.from);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003593 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003594 return 0;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003595
Willy Tarreau24e32d82012-04-23 23:55:44 +02003596 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003597 return acl_fetch_bytes_out_rate(&px->table, smp, stktable_lookup_key(&px->table, key));
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003598}
3599
Willy Tarreau34db1082012-04-19 17:16:54 +02003600/* set temp integer to the number of used entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003601 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003602 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003603static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003604acl_fetch_table_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003605 const struct arg *args, struct sample *smp)
Willy Tarreauc735a072011-03-29 00:57:02 +02003606{
Willy Tarreau37406352012-04-23 16:16:37 +02003607 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003608 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02003609 smp->data.uint = args->data.prx->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003610 return 1;
3611}
3612
Willy Tarreau34db1082012-04-19 17:16:54 +02003613/* set temp integer to the number of free entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003614 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003615 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003616static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003617acl_fetch_table_avl(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02003618 const struct arg *args, struct sample *smp)
Willy Tarreauc735a072011-03-29 00:57:02 +02003619{
Willy Tarreau24e32d82012-04-23 23:55:44 +02003620 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003621 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003622 smp->type = SMP_T_UINT;
3623 smp->data.uint = px->table.size - px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003624 return 1;
3625}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003626
Willy Tarreau61612d42012-04-19 18:42:05 +02003627/* Note: must not be declared <const> as its list will be overwritten.
3628 * Please take care of keeping this list alphabetically sorted.
3629 */
Willy Tarreau8b22a712010-06-18 17:46:06 +02003630static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau61612d42012-04-19 18:42:05 +02003631 { "sc1_bytes_in_rate", acl_parse_int, acl_fetch_sc1_bytes_in_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3632 { "sc1_bytes_out_rate", acl_parse_int, acl_fetch_sc1_bytes_out_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3633 { "sc1_clr_gpc0", acl_parse_int, acl_fetch_sc1_clr_gpc0, acl_match_int, ACL_USE_NOTHING, 0 },
3634 { "sc1_conn_cnt", acl_parse_int, acl_fetch_sc1_conn_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3635 { "sc1_conn_cur", acl_parse_int, acl_fetch_sc1_conn_cur, acl_match_int, ACL_USE_NOTHING, 0 },
3636 { "sc1_conn_rate", acl_parse_int, acl_fetch_sc1_conn_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3637 { "sc1_get_gpc0", acl_parse_int, acl_fetch_sc1_get_gpc0, acl_match_int, ACL_USE_NOTHING, 0 },
3638 { "sc1_http_err_cnt", acl_parse_int, acl_fetch_sc1_http_err_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3639 { "sc1_http_err_rate", acl_parse_int, acl_fetch_sc1_http_err_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3640 { "sc1_http_req_cnt", acl_parse_int, acl_fetch_sc1_http_req_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3641 { "sc1_http_req_rate", acl_parse_int, acl_fetch_sc1_http_req_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3642 { "sc1_inc_gpc0", acl_parse_int, acl_fetch_sc1_inc_gpc0, acl_match_int, ACL_USE_NOTHING, 0 },
3643 { "sc1_kbytes_in", acl_parse_int, acl_fetch_sc1_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE, 0 },
3644 { "sc1_kbytes_out", acl_parse_int, acl_fetch_sc1_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE, 0 },
3645 { "sc1_sess_cnt", acl_parse_int, acl_fetch_sc1_sess_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3646 { "sc1_sess_rate", acl_parse_int, acl_fetch_sc1_sess_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3647 { "sc2_bytes_in_rate", acl_parse_int, acl_fetch_sc2_bytes_in_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3648 { "sc2_bytes_out_rate", acl_parse_int, acl_fetch_sc2_bytes_out_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3649 { "sc2_clr_gpc0", acl_parse_int, acl_fetch_sc2_clr_gpc0, acl_match_int, ACL_USE_NOTHING, 0 },
3650 { "sc2_conn_cnt", acl_parse_int, acl_fetch_sc2_conn_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3651 { "sc2_conn_cur", acl_parse_int, acl_fetch_sc2_conn_cur, acl_match_int, ACL_USE_NOTHING, 0 },
3652 { "sc2_conn_rate", acl_parse_int, acl_fetch_sc2_conn_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3653 { "sc2_get_gpc0", acl_parse_int, acl_fetch_sc2_get_gpc0, acl_match_int, ACL_USE_NOTHING, 0 },
3654 { "sc2_http_err_cnt", acl_parse_int, acl_fetch_sc2_http_err_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3655 { "sc2_http_err_rate", acl_parse_int, acl_fetch_sc2_http_err_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3656 { "sc2_http_req_cnt", acl_parse_int, acl_fetch_sc2_http_req_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3657 { "sc2_http_req_rate", acl_parse_int, acl_fetch_sc2_http_req_rate, acl_match_int, ACL_USE_NOTHING, 0 },
3658 { "sc2_inc_gpc0", acl_parse_int, acl_fetch_sc2_inc_gpc0, acl_match_int, ACL_USE_NOTHING, 0 },
3659 { "sc2_kbytes_in", acl_parse_int, acl_fetch_sc2_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE, 0 },
3660 { "sc2_kbytes_out", acl_parse_int, acl_fetch_sc2_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE, 0 },
3661 { "sc2_sess_cnt", acl_parse_int, acl_fetch_sc2_sess_cnt, acl_match_int, ACL_USE_NOTHING, 0 },
3662 { "sc2_sess_rate", acl_parse_int, acl_fetch_sc2_sess_rate, acl_match_int, ACL_USE_NOTHING, 0 },
Willy Tarreaufc2c1fd2012-04-19 23:35:54 +02003663 { "src_bytes_in_rate", acl_parse_int, acl_fetch_src_bytes_in_rate, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3664 { "src_bytes_out_rate", acl_parse_int, acl_fetch_src_bytes_out_rate, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3665 { "src_clr_gpc0", acl_parse_int, acl_fetch_src_clr_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3666 { "src_conn_cnt", acl_parse_int, acl_fetch_src_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3667 { "src_conn_cur", acl_parse_int, acl_fetch_src_conn_cur, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3668 { "src_conn_rate", acl_parse_int, acl_fetch_src_conn_rate, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3669 { "src_get_gpc0", acl_parse_int, acl_fetch_src_get_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3670 { "src_http_err_cnt", acl_parse_int, acl_fetch_src_http_err_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3671 { "src_http_err_rate", acl_parse_int, acl_fetch_src_http_err_rate, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3672 { "src_http_req_cnt", acl_parse_int, acl_fetch_src_http_req_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3673 { "src_http_req_rate", acl_parse_int, acl_fetch_src_http_req_rate, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3674 { "src_inc_gpc0", acl_parse_int, acl_fetch_src_inc_gpc0, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3675 { "src_kbytes_in", acl_parse_int, acl_fetch_src_kbytes_in, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3676 { "src_kbytes_out", acl_parse_int, acl_fetch_src_kbytes_out, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3677 { "src_sess_cnt", acl_parse_int, acl_fetch_src_sess_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3678 { "src_sess_rate", acl_parse_int, acl_fetch_src_sess_rate, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3679 { "src_updt_conn_cnt", acl_parse_int, acl_fetch_src_updt_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE, ARG1(1,TAB) },
3680 { "table_avl", acl_parse_int, acl_fetch_table_avl, acl_match_int, ACL_USE_NOTHING, ARG1(1,TAB) },
3681 { "table_cnt", acl_parse_int, acl_fetch_table_cnt, acl_match_int, ACL_USE_NOTHING, ARG1(1,TAB) },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003682 { NULL, NULL, NULL, NULL },
3683}};
3684
3685
Willy Tarreau56123282010-08-06 19:06:56 +02003686/* Parse a "track-sc[12]" line starting with "track-sc[12]" in args[arg-1].
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003687 * Returns the number of warnings emitted, or -1 in case of fatal errors. The
3688 * <prm> struct is fed with the table name if any. If unspecified, the caller
3689 * will assume that the current proxy's table is used.
3690 */
3691int parse_track_counters(char **args, int *arg,
3692 int section_type, struct proxy *curpx,
3693 struct track_ctr_prm *prm,
Willy Tarreau0a3dd742012-05-08 19:47:01 +02003694 struct proxy *defpx, char **err)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003695{
Willy Tarreau12785782012-04-27 21:37:17 +02003696 int sample_type = 0;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003697
Willy Tarreau56123282010-08-06 19:06:56 +02003698 /* parse the arguments of "track-sc[12]" before the condition in the
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003699 * following form :
Willy Tarreau56123282010-08-06 19:06:56 +02003700 * track-sc[12] src [ table xxx ] [ if|unless ... ]
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003701 */
3702 while (args[*arg]) {
3703 if (strcmp(args[*arg], "src") == 0) {
3704 prm->type = STKTABLE_TYPE_IP;
Willy Tarreau12785782012-04-27 21:37:17 +02003705 sample_type = 1;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003706 }
3707 else if (strcmp(args[*arg], "table") == 0) {
3708 if (!args[*arg + 1]) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02003709 memprintf(err, "missing table name");
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003710 return -1;
3711 }
3712 /* we copy the table name for now, it will be resolved later */
3713 prm->table.n = strdup(args[*arg + 1]);
3714 (*arg)++;
3715 }
3716 else {
3717 /* unhandled keywords are handled by the caller */
3718 break;
3719 }
3720 (*arg)++;
3721 }
3722
Willy Tarreau12785782012-04-27 21:37:17 +02003723 if (!sample_type) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +02003724 memprintf(err,
3725 "tracking key not specified (found %s, only 'src' is supported)",
3726 quote_arg(args[*arg]));
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02003727 return -1;
3728 }
3729
3730 return 0;
3731}
3732
Willy Tarreau8b22a712010-06-18 17:46:06 +02003733__attribute__((constructor))
3734static void __session_init(void)
3735{
3736 acl_register_keywords(&acl_kws);
3737}
3738
Willy Tarreaubaaee002006-06-26 02:48:02 +02003739/*
3740 * Local variables:
3741 * c-indent-level: 8
3742 * c-basic-offset: 8
3743 * End:
3744 */