blob: fdd99ba956493f0863f2920e4c1c08e49e2f4906 [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{
Willy Tarreaub363a1f2013-10-01 10:45:07 +020075 struct connection *cli_conn;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020076 struct proxy *p = l->frontend;
77 struct session *s;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020078 struct task *t;
Willy Tarreauabe8ea52010-11-11 10:56:04 +010079 int ret;
80
81
82 ret = -1; /* assume unrecoverable error by default */
Willy Tarreau81f9aa32010-06-01 17:45:26 +020083
Willy Tarreau32e3c6a2013-10-11 19:34:20 +020084 if (unlikely((cli_conn = conn_new()) == NULL))
Willy Tarreau81f9aa32010-06-01 17:45:26 +020085 goto out_close;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020086
Willy Tarreau32e3c6a2013-10-11 19:34:20 +020087 conn_prepare(cli_conn, l->proto, l->xprt);
Willy Tarreauf2943dc2012-10-26 20:10:28 +020088
Willy Tarreau32e3c6a2013-10-11 19:34:20 +020089 cli_conn->t.sock.fd = cfd;
90 cli_conn->addr.from = *addr;
91 cli_conn->flags |= CO_FL_ADDR_FROM_SET;
92 cli_conn->target = &l->obj_type;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010093 cli_conn->proxy_netns = l->netns;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +020094
95 if (unlikely((s = pool_alloc2(pool2_session)) == NULL))
96 goto out_free_conn;
Willy Tarreauf2943dc2012-10-26 20:10:28 +020097
Willy Tarreau2542b532012-08-31 16:01:23 +020098 /* minimum session initialization required for an embryonic session is
99 * fairly low. We need very little to execute L4 ACLs, then we need a
100 * task to make the client-side connection live on its own.
101 * - flags
102 * - stick-entry tracking
103 */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200104 s->flags = 0;
105 s->logs.logwait = p->to_log;
Willy Tarreau9a355ec2013-06-11 17:45:46 +0200106 s->logs.level = 0;
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +0200107
108 memset(s->stkctr, 0, sizeof(s->stkctr));
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200109
Willy Tarreau2542b532012-08-31 16:01:23 +0200110 s->listener = l;
111 s->fe = p;
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100112
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200113 /* On a mini-session, the connection is directly attached to the
114 * session's target so that we don't need to initialize the stream
115 * interfaces. Another benefit is that it's easy to detect a mini-
116 * session in dumps using this : it's the only one which has a
117 * connection in s->target.
118 */
119 s->target = &cli_conn->obj_type;
120
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200121 s->logs.accept_date = date; /* user-visible date for logging */
122 s->logs.tv_accept = now; /* corrected date for internal use */
Willy Tarreau1f0da242014-01-25 11:01:50 +0100123 s->uniq_id = global.req_count++;
Willy Tarreau2542b532012-08-31 16:01:23 +0200124 p->feconn++;
125 /* This session was accepted, count it now */
126 if (p->feconn > p->fe_counters.conn_max)
127 p->fe_counters.conn_max = p->feconn;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200128
Willy Tarreau2542b532012-08-31 16:01:23 +0200129 proxy_inc_fe_conn_ctr(l, p);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200130
Willy Tarreau59e3ff42013-12-16 02:16:50 +0100131 /* Add the minimum callbacks to prepare the connection's control layer.
132 * We need this so that we can safely execute the ACLs used by the
133 * "tcp-request connection" ruleset. We also carefully attach the
134 * connection to the stream interface without initializing the rest,
135 * so that ACLs can use si[0]->end.
136 */
137 si_attach_conn(&s->si[0], cli_conn);
138 conn_attach(cli_conn, s, &sess_conn_cb);
139 conn_ctrl_init(cli_conn);
140
Willy Tarreau2cff2f72013-12-16 10:12:54 +0100141 /* now evaluate the tcp-request layer4 rules. Since we expect to be able
142 * to abort right here as soon as possible, we check the rules before
143 * even initializing the stream interfaces.
144 */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200145 if ((l->options & LI_O_TCP_RULES) && !tcp_exec_req_rules(s)) {
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200146 /* let's do a no-linger now to close with a single RST. */
147 setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100148 ret = 0; /* successful termination */
Willy Tarreau2542b532012-08-31 16:01:23 +0200149 goto out_free_session;
150 }
151
Willy Tarreau82569f92012-09-27 23:48:56 +0200152 /* monitor-net and health mode are processed immediately after TCP
153 * connection rules. This way it's possible to block them, but they
154 * never use the lower data layers, they send directly over the socket,
155 * as they were designed for. We first flush the socket receive buffer
156 * in order to avoid emission of an RST by the system. We ignore any
157 * error.
158 */
159 if (unlikely((p->mode == PR_MODE_HEALTH) ||
160 ((l->options & LI_O_CHK_MONNET) &&
161 addr->ss_family == AF_INET &&
162 (((struct sockaddr_in *)addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr))) {
163 /* we have 4 possibilities here :
164 * - HTTP mode, from monitoring address => send "HTTP/1.0 200 OK"
165 * - HEALTH mode with HTTP check => send "HTTP/1.0 200 OK"
166 * - HEALTH mode without HTTP check => just send "OK"
167 * - TCP mode from monitoring address => just close
168 */
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200169 if (l->proto->drain)
170 l->proto->drain(cfd);
Willy Tarreau82569f92012-09-27 23:48:56 +0200171 if (p->mode == PR_MODE_HTTP ||
172 (p->mode == PR_MODE_HEALTH && (p->options2 & PR_O2_CHK_ANY) == PR_O2_HTTP_CHK))
173 send(cfd, "HTTP/1.0 200 OK\r\n\r\n", 19, MSG_DONTWAIT|MSG_NOSIGNAL|MSG_MORE);
174 else if (p->mode == PR_MODE_HEALTH)
175 send(cfd, "OK\n", 3, MSG_DONTWAIT|MSG_NOSIGNAL|MSG_MORE);
176 ret = 0;
177 goto out_free_session;
178 }
179
Willy Tarreau22cda212012-08-31 17:43:29 +0200180 /* wait for a PROXY protocol header */
181 if (l->options & LI_O_ACC_PROXY) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200182 cli_conn->flags |= CO_FL_ACCEPT_PROXY;
183 conn_sock_want_recv(cli_conn);
Willy Tarreau22cda212012-08-31 17:43:29 +0200184 }
185
Willy Tarreau2542b532012-08-31 16:01:23 +0200186 if (unlikely((t = task_new()) == NULL))
187 goto out_free_session;
188
189 t->context = s;
190 t->nice = l->nice;
191 s->task = t;
192
Willy Tarreau59e3ff42013-12-16 02:16:50 +0100193 /* Finish setting the callbacks. Right now the transport layer is present
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200194 * but not initialized. Also note we need to be careful as the stream
195 * int is not initialized yet.
Willy Tarreau2542b532012-08-31 16:01:23 +0200196 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200197 conn_data_want_recv(cli_conn);
198 if (conn_xprt_init(cli_conn) < 0)
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100199 goto out_free_task;
Willy Tarreau2542b532012-08-31 16:01:23 +0200200
201 /* OK, now either we have a pending handshake to execute with and
202 * then we must return to the I/O layer, or we can proceed with the
203 * end of the session initialization. In case of handshake, we also
204 * set the I/O timeout to the frontend's client timeout.
205 */
206
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200207 if (cli_conn->flags & CO_FL_HANDSHAKE) {
Willy Tarreau2542b532012-08-31 16:01:23 +0200208 t->process = expire_mini_session;
209 t->expire = tick_add_ifset(now_ms, p->timeout.client);
210 task_queue(t);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200211 cli_conn->flags |= CO_FL_INIT_DATA | CO_FL_WAKE_DATA;
Willy Tarreau2542b532012-08-31 16:01:23 +0200212 return 1;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200213 }
214
Willy Tarreau815f5ec2012-11-06 00:14:25 +0100215 /* OK let's complete session initialization since there is no handshake */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200216 cli_conn->flags |= CO_FL_CONNECTED;
Willy Tarreau2542b532012-08-31 16:01:23 +0200217 ret = session_complete(s);
218 if (ret > 0)
219 return ret;
220
221 /* Error unrolling */
222 out_free_task:
223 task_free(t);
224 out_free_session:
225 p->feconn--;
Willy Tarreaue9101692014-01-28 22:48:24 +0100226 session_store_counters(s);
Willy Tarreau2542b532012-08-31 16:01:23 +0200227 pool_free2(pool2_session, s);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200228 out_free_conn:
229 cli_conn->flags &= ~CO_FL_XPRT_TRACKED;
230 conn_xprt_close(cli_conn);
231 conn_free(cli_conn);
Willy Tarreau2542b532012-08-31 16:01:23 +0200232 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 Tarreau05bf5e12013-10-20 23:10:28 +0200235 struct chunk *err_msg = &p->errmsg[HTTP_ERR_500];
236 if (!err_msg->str)
237 err_msg = &http_err_chunks[HTTP_ERR_500];
Willy Tarreau2542b532012-08-31 16:01:23 +0200238 send(cfd, err_msg->str, err_msg->len, MSG_DONTWAIT|MSG_NOSIGNAL);
239 }
240
241 if (fdtab[cfd].owner)
242 fd_delete(cfd);
243 else
244 close(cfd);
245 return ret;
246}
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100247
Willy Tarreau0af29122012-12-03 15:35:00 +0100248
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200249/* prepare the trash with a log prefix for session <s>. It only works with
Willy Tarreaub4f98092014-05-08 21:06:11 +0200250 * embryonic sessions based on a real connection. This function requires that
251 * at s->target still points to the incoming connection.
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200252 */
Willy Tarreau0af29122012-12-03 15:35:00 +0100253static void prepare_mini_sess_log_prefix(struct session *s)
254{
255 struct tm tm;
256 char pn[INET6_ADDRSTRLEN];
257 int ret;
258 char *end;
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200259 struct connection *cli_conn = __objt_conn(s->target);
Willy Tarreau0af29122012-12-03 15:35:00 +0100260
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200261 ret = addr_to_str(&cli_conn->addr.from, pn, sizeof(pn));
Willy Tarreau0af29122012-12-03 15:35:00 +0100262 if (ret <= 0)
263 chunk_printf(&trash, "unknown [");
264 else if (ret == AF_UNIX)
265 chunk_printf(&trash, "%s:%d [", pn, s->listener->luid);
266 else
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200267 chunk_printf(&trash, "%s:%d [", pn, get_host_port(&cli_conn->addr.from));
Willy Tarreau0af29122012-12-03 15:35:00 +0100268
269 get_localtime(s->logs.accept_date.tv_sec, &tm);
270 end = date2str_log(trash.str + trash.len, &tm, &(s->logs.accept_date), trash.size - trash.len);
271 trash.len = end - trash.str;
272 if (s->listener->name)
273 chunk_appendf(&trash, "] %s/%s", s->fe->id, s->listener->name);
274 else
275 chunk_appendf(&trash, "] %s/%d", s->fe->id, s->listener->luid);
276}
277
Willy Tarreau2542b532012-08-31 16:01:23 +0200278/* This function kills an existing embryonic session. It stops the connection's
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200279 * transport layer, releases assigned resources, resumes the listener if it was
Willy Tarreaub4f98092014-05-08 21:06:11 +0200280 * disabled and finally kills the file descriptor. This function requires that
281 * at s->target still points to the incoming connection.
Willy Tarreau2542b532012-08-31 16:01:23 +0200282 */
283static void kill_mini_session(struct session *s)
284{
Willy Tarreau0af29122012-12-03 15:35:00 +0100285 int level = LOG_INFO;
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200286 struct connection *conn = __objt_conn(s->target);
Willy Tarreau0af29122012-12-03 15:35:00 +0100287 unsigned int log = s->logs.logwait;
288 const char *err_msg;
289
290 if (s->fe->options2 & PR_O2_LOGERRORS)
291 level = LOG_ERR;
292
293 if (log && (s->fe->options & PR_O_NULLNOLOG)) {
294 /* with "option dontlognull", we don't log connections with no transfer */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100295 if (!conn->err_code ||
Willy Tarreau20879a02012-12-03 16:32:10 +0100296 conn->err_code == CO_ER_PRX_EMPTY || conn->err_code == CO_ER_PRX_ABORT ||
297 conn->err_code == CO_ER_SSL_EMPTY || conn->err_code == CO_ER_SSL_ABORT)
Willy Tarreau0af29122012-12-03 15:35:00 +0100298 log = 0;
299 }
300
301 if (log) {
302 if (!conn->err_code && (s->task->state & TASK_WOKEN_TIMER)) {
303 if (conn->flags & CO_FL_ACCEPT_PROXY)
304 conn->err_code = CO_ER_PRX_TIMEOUT;
305 else if (conn->flags & CO_FL_SSL_WAIT_HS)
306 conn->err_code = CO_ER_SSL_TIMEOUT;
307 }
308
309 prepare_mini_sess_log_prefix(s);
310 err_msg = conn_err_code_str(conn);
311 if (err_msg)
312 send_log(s->fe, level, "%s: %s\n", trash.str, err_msg);
313 else
314 send_log(s->fe, level, "%s: unknown connection error (code=%d flags=%08x)\n",
315 trash.str, conn->err_code, conn->flags);
316 }
317
Willy Tarreau2542b532012-08-31 16:01:23 +0200318 /* kill the connection now */
Willy Tarreauf79c8172013-10-21 16:30:56 +0200319 conn_force_close(conn);
Willy Tarreaua23ee3a2014-02-05 00:18:47 +0100320 conn_free(conn);
Willy Tarreau2542b532012-08-31 16:01:23 +0200321
322 s->fe->feconn--;
Willy Tarreau7af7d592013-07-01 18:07:03 +0200323 session_store_counters(s);
Willy Tarreau2542b532012-08-31 16:01:23 +0200324
325 if (!(s->listener->options & LI_O_UNLIMITED))
326 actconn--;
327 jobs--;
328 s->listener->nbconn--;
329 if (s->listener->state == LI_FULL)
330 resume_listener(s->listener);
331
332 /* Dequeues all of the listeners waiting for a resource */
333 if (!LIST_ISEMPTY(&global_listener_queue))
334 dequeue_all_listeners(&global_listener_queue);
335
336 if (!LIST_ISEMPTY(&s->fe->listener_queue) &&
337 (!s->fe->fe_sps_lim || freq_ctr_remain(&s->fe->fe_sess_per_sec, s->fe->fe_sps_lim, 0) > 0))
338 dequeue_all_listeners(&s->fe->listener_queue);
339
340 task_delete(s->task);
341 task_free(s->task);
Willy Tarreau2542b532012-08-31 16:01:23 +0200342 pool_free2(pool2_session, s);
343}
344
Willy Tarreau22cda212012-08-31 17:43:29 +0200345/* Finish initializing a session from a connection, or kills it if the
346 * connection shows and error. Returns <0 if the connection was killed.
Willy Tarreau2542b532012-08-31 16:01:23 +0200347 */
Willy Tarreau071e1372012-10-03 01:39:48 +0200348static int conn_session_complete(struct connection *conn)
Willy Tarreau2542b532012-08-31 16:01:23 +0200349{
Willy Tarreau5e75e272012-10-02 21:21:20 +0200350 struct session *s = conn->owner;
Willy Tarreau2542b532012-08-31 16:01:23 +0200351
Willy Tarreau22cda212012-08-31 17:43:29 +0200352 if (!(conn->flags & CO_FL_ERROR) && (session_complete(s) > 0)) {
Willy Tarreau071e1372012-10-03 01:39:48 +0200353 conn->flags &= ~CO_FL_INIT_DATA;
Willy Tarreau2542b532012-08-31 16:01:23 +0200354 return 0;
355 }
356
357 /* kill the connection now */
358 kill_mini_session(s);
359 return -1;
360}
361
Willy Tarreau9683e9a2012-10-03 21:17:23 +0200362/* Update an embryonic session status. The connection is killed in case of
363 * error, and <0 will be returned. Otherwise it does nothing.
364 */
365static int conn_session_update(struct connection *conn)
366{
367 if (conn->flags & CO_FL_ERROR) {
368 kill_mini_session(conn->owner);
369 return -1;
370 }
371 return 0;
372}
373
Willy Tarreau2542b532012-08-31 16:01:23 +0200374/* Manages embryonic sessions timeout. It is only called when the timeout
375 * strikes and performs the required cleanup.
376 */
377static struct task *expire_mini_session(struct task *t)
378{
379 struct session *s = t->context;
380
381 if (!(t->state & TASK_WOKEN_TIMER))
382 return t;
383
384 kill_mini_session(s);
385 return NULL;
386}
387
388/* This function is called from the I/O handler which detects the end of
389 * handshake, in order to complete initialization of a valid session. It must
390 * be called with an embryonic session. It returns a positive value upon
391 * success, 0 if the connection can be ignored, or a negative value upon
392 * critical failure. The accepted file descriptor is closed if we return <= 0.
Willy Tarreaub4f98092014-05-08 21:06:11 +0200393 * The client-side end point is assumed to be a connection, whose pointer is
394 * taken from s->target which is assumed to be valid. If the function fails,
395 * it restores s->target.
Willy Tarreau2542b532012-08-31 16:01:23 +0200396 */
397int session_complete(struct session *s)
398{
399 struct listener *l = s->listener;
400 struct proxy *p = s->fe;
401 struct http_txn *txn;
402 struct task *t = s->task;
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200403 struct connection *conn = __objt_conn(s->target);
Willy Tarreau2542b532012-08-31 16:01:23 +0200404 int ret;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100405 int i;
Willy Tarreau2542b532012-08-31 16:01:23 +0200406
407 ret = -1; /* assume unrecoverable error by default */
408
409 /* OK, we're keeping the session, so let's properly initialize the session */
410 LIST_ADDQ(&sessions, &s->list);
411 LIST_INIT(&s->back_refs);
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200412
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200413 s->flags |= SN_INITIALIZED;
Willy Tarreau2542b532012-08-31 16:01:23 +0200414 s->unique_id = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200415
416 t->process = l->handler;
417 t->context = s;
418 t->expire = TICK_ETERNITY;
419
420 /* Note: initially, the session's backend points to the frontend.
421 * This changes later when switching rules are executed or
422 * when the default backend is assigned.
423 */
424 s->be = s->fe;
425 s->req = s->rep = NULL; /* will be allocated later */
William Lallemand82fe75c2012-10-23 10:25:10 +0200426 s->comp_algo = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200427
428 /* Let's count a session now */
Willy Tarreaub36b4242010-06-04 20:59:39 +0200429 proxy_inc_fe_sess_ctr(l, p);
Willy Tarreau91c43d72010-06-20 11:19:22 +0200430
Willy Tarreaub4c84932013-07-23 19:15:30 +0200431 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200432 void *ptr;
433
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100434 if (!stkctr_entry(&s->stkctr[i]))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100435 continue;
436
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100437 ptr = stktable_data_ptr(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]), STKTABLE_DT_SESS_CNT);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200438 if (ptr)
439 stktable_data_cast(ptr, sess_cnt)++;
440
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100441 ptr = stktable_data_ptr(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]), STKTABLE_DT_SESS_RATE);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200442 if (ptr)
443 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau20d46a52012-12-09 15:55:40 +0100444 s->stkctr[i].table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200445 }
446
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200447 /* this part should be common with other protocols */
448 si_reset(&s->si[0], t);
449 si_set_state(&s->si[0], SI_ST_EST);
450
Willy Tarreaub4f98092014-05-08 21:06:11 +0200451 /* attach the incoming connection to the stream interface now.
452 * We must do that *before* clearing ->target because we need
453 * to keep a pointer to the connection in case we have to call
454 * kill_mini_session().
455 */
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200456 si_attach_conn(&s->si[0], conn);
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 Tarreau3ed35ef2013-10-24 11:51:38 +0200464 si_reset(&s->si[1], t);
Willy Tarreau2a6e8802013-10-24 15:50:53 +0200465 si_detach(&s->si[1]);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200466
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200467 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
468 s->si[1].flags |= SI_FL_INDEP_STR;
469
Willy Tarreau9bd0d742011-07-20 00:17:39 +0200470 session_init_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100471 s->target = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200472 s->pend_pos = NULL;
473
474 /* init store persistence */
475 s->store_count = 0;
476
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200477 if (unlikely((s->req = pool_alloc2(pool2_channel)) == NULL))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200478 goto out_free_task; /* no memory */
479
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200480 channel_init(s->req);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200481 s->req->prod = &s->si[0];
482 s->req->cons = &s->si[1];
483 s->si[0].ib = s->si[1].ob = s->req;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200484 s->req->flags |= CF_READ_ATTACHED; /* the producer is already connected */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200485
486 /* activate default analysers enabled for this listener */
487 s->req->analysers = l->analysers;
488
489 s->req->wto = TICK_ETERNITY;
490 s->req->rto = TICK_ETERNITY;
491 s->req->rex = TICK_ETERNITY;
492 s->req->wex = TICK_ETERNITY;
493 s->req->analyse_exp = TICK_ETERNITY;
494
Willy Tarreau696a2912014-11-24 11:36:57 +0100495 if (unlikely((s->rep = pool_alloc2(pool2_channel)) == NULL))
Willy Tarreau909e2672014-11-25 19:54:11 +0100496 goto out_free_req; /* no memory */
Willy Tarreau696a2912014-11-24 11:36:57 +0100497
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200498 channel_init(s->rep);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200499 s->rep->prod = &s->si[1];
500 s->rep->cons = &s->si[0];
501 s->si[0].ob = s->si[1].ib = s->rep;
502 s->rep->analysers = 0;
503
Willy Tarreau96e31212011-05-30 18:10:30 +0200504 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200505 s->req->flags |= CF_NEVER_WAIT;
506 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +0200507 }
508
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200509 s->rep->rto = TICK_ETERNITY;
510 s->rep->wto = TICK_ETERNITY;
511 s->rep->rex = TICK_ETERNITY;
512 s->rep->wex = TICK_ETERNITY;
513 s->rep->analyse_exp = TICK_ETERNITY;
514
Willy Tarreau909e2672014-11-25 19:54:11 +0100515 if (unlikely(b_alloc(&s->req->buf) == NULL))
Willy Tarreau696a2912014-11-24 11:36:57 +0100516 goto out_free_rep; /* no memory */
517
Willy Tarreau909e2672014-11-25 19:54:11 +0100518 if (unlikely(b_alloc(&s->rep->buf) == NULL))
519 goto out_free_req_buf; /* no memory */
520
Willy Tarreau62f791e2012-03-09 11:32:30 +0100521 txn = &s->txn;
522 /* Those variables will be checked and freed if non-NULL in
523 * session.c:session_free(). It is important that they are
524 * properly initialized.
525 */
526 txn->sessid = NULL;
527 txn->srv_cookie = NULL;
528 txn->cli_cookie = NULL;
529 txn->uri = NULL;
530 txn->req.cap = NULL;
531 txn->rsp.cap = NULL;
532 txn->hdr_idx.v = NULL;
533 txn->hdr_idx.size = txn->hdr_idx.used = 0;
Willy Tarreau068621e2013-12-23 15:11:25 +0100534 txn->flags = 0;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100535 txn->req.flags = 0;
536 txn->rsp.flags = 0;
537 /* the HTTP messages need to know what buffer they're associated with */
Willy Tarreau394db372012-10-12 22:40:39 +0200538 txn->req.chn = s->req;
539 txn->rsp.chn = s->rep;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100540
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200541 /* finish initialization of the accepted file descriptor */
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200542 conn_data_want_recv(conn);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200543
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100544 if (p->accept && (ret = p->accept(s)) <= 0) {
545 /* Either we had an unrecoverable error (<0) or work is
546 * finished (=0, eg: monitoring), in both situations,
547 * we can release everything and close.
548 */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200549 goto out_free_rep_buf;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200550 }
551
Willy Tarreau93dbc2b2012-10-12 18:01:49 +0200552 /* if logs require transport layer information, note it on the connection */
553 if (s->logs.logwait & LW_XPRT)
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200554 conn->flags |= CO_FL_XPRT_TRACKED;
Willy Tarreau93dbc2b2012-10-12 18:01:49 +0200555
Willy Tarreau2542b532012-08-31 16:01:23 +0200556 /* we want the connection handler to notify the stream interface about updates. */
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200557 conn->flags |= CO_FL_WAKE_DATA;
Willy Tarreau2542b532012-08-31 16:01:23 +0200558
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200559 /* it is important not to call the wakeup function directly but to
560 * pass through task_wakeup(), because this one knows how to apply
561 * priorities to tasks.
562 */
563 task_wakeup(t, TASK_WOKEN_INIT);
564 return 1;
565
566 /* Error unrolling */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200567 out_free_rep_buf:
Willy Tarreau7dfca9d2014-11-25 19:45:11 +0100568 b_free(&s->rep->buf);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200569 out_free_req_buf:
Willy Tarreau7dfca9d2014-11-25 19:45:11 +0100570 b_free(&s->req->buf);
Willy Tarreau909e2672014-11-25 19:54:11 +0100571 out_free_rep:
572 pool_free2(pool2_channel, s->rep);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200573 out_free_req:
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200574 pool_free2(pool2_channel, s->req);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200575 out_free_task:
Willy Tarreaub4f98092014-05-08 21:06:11 +0200576 /* and restore the connection pointer in case we destroyed it,
577 * because kill_mini_session() will need it.
578 */
Willy Tarreau3b246412014-11-25 17:10:33 +0100579 LIST_DEL(&s->list);
Willy Tarreaub4f98092014-05-08 21:06:11 +0200580 s->target = &conn->obj_type;
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100581 return ret;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200582}
583
Willy Tarreaubaaee002006-06-26 02:48:02 +0200584/*
585 * frees the context associated to a session. It must have been removed first.
586 */
Simon Hormandec5be42011-06-08 09:19:07 +0900587static void session_free(struct session *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200588{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +0100589 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +0200590 struct proxy *fe = s->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100591 struct bref *bref, *back;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200592 struct connection *cli_conn = objt_conn(s->si[0].end);
Willy Tarreaua4cda672010-06-06 18:28:49 +0200593 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100594
Willy Tarreaubaaee002006-06-26 02:48:02 +0200595 if (s->pend_pos)
596 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100597
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100598 if (objt_server(s->target)) { /* there may be requests left pending in queue */
Willy Tarreau1e62de62008-11-11 20:20:02 +0100599 if (s->flags & SN_CURR_SESS) {
600 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100601 objt_server(s->target)->cur_sess--;
Willy Tarreau1e62de62008-11-11 20:20:02 +0100602 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100603 if (may_dequeue_tasks(objt_server(s->target), s->be))
604 process_srv_queue(objt_server(s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100605 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100606
Willy Tarreau7c669d72008-06-20 15:04:11 +0200607 if (unlikely(s->srv_conn)) {
608 /* the session still has a reserved slot on a server, but
609 * it should normally be only the same as the one above,
610 * so this should not happen in fact.
611 */
612 sess_change_server(s, NULL);
613 }
614
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100615 if (s->req->pipe)
616 put_pipe(s->req->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100617
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100618 if (s->rep->pipe)
619 put_pipe(s->rep->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100620
Willy Tarreau7dfca9d2014-11-25 19:45:11 +0100621 b_free(&s->req->buf);
622 b_free(&s->rep->buf);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200623
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200624 pool_free2(pool2_channel, s->req);
625 pool_free2(pool2_channel, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200626
Willy Tarreau46023632010-01-07 22:51:47 +0100627 http_end_txn(s);
628
Willy Tarreau1e954912012-10-12 17:50:05 +0200629 /* ensure the client-side transport layer is destroyed */
Willy Tarreauf79c8172013-10-21 16:30:56 +0200630 if (cli_conn)
631 conn_force_close(cli_conn);
Willy Tarreau1e954912012-10-12 17:50:05 +0200632
Willy Tarreaua4cda672010-06-06 18:28:49 +0200633 for (i = 0; i < s->store_count; i++) {
634 if (!s->store[i].ts)
635 continue;
636 stksess_free(s->store[i].table, s->store[i].ts);
637 s->store[i].ts = NULL;
638 }
639
Willy Tarreau34eb6712011-10-24 18:15:04 +0200640 pool_free2(pool2_hdr_idx, txn->hdr_idx.v);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200641 if (fe) {
Willy Tarreau46023632010-01-07 22:51:47 +0100642 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
643 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200644 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100645
Willy Tarreau7af7d592013-07-01 18:07:03 +0200646 session_store_counters(s);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200647
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100648 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100649 /* we have to unlink all watchers. We must not relink them if
650 * this session was the last one in the list.
651 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100652 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100653 LIST_INIT(&bref->users);
654 if (s->list.n != &sessions)
655 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100656 bref->ref = s->list.n;
657 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100658 LIST_DEL(&s->list);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200659 si_release_endpoint(&s->si[1]);
660 si_release_endpoint(&s->si[0]);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200661 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200662
663 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200664 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +0200665 pool_flush2(pool2_buffer);
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200666 pool_flush2(pool2_channel);
Willy Tarreau34eb6712011-10-24 18:15:04 +0200667 pool_flush2(pool2_hdr_idx);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200668 pool_flush2(pool2_requri);
669 pool_flush2(pool2_capture);
670 pool_flush2(pool2_session);
Willy Tarreau3a5e0602014-11-13 16:46:28 +0100671 pool_flush2(pool2_connection);
672 pool_flush2(pool2_pendconn);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200673 pool_flush2(fe->req_cap_pool);
674 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200675 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200676}
677
Willy Tarreau656859d2014-11-25 19:46:36 +0100678/* Allocates a single buffer for session <s>, but only if it's guaranteed that
679 * it's not the last available buffer. To be called at the beginning of recv()
680 * callbacks to ensure that the required buffers are properly allocated.
681 * Returns 0 in case of failure, non-zero otherwise.
682 */
683int session_alloc_recv_buffer(struct session *s, struct buffer **buf)
684{
685 struct buffer *b;
686
687 b = b_alloc_margin(buf, 2);
688 if (b)
689 return 1;
690
691 /* FIXME: normally we're supposed to subscribe to a list of waiters
692 * for buffers. We release what we failed to allocate.
693 */
694 return 0;
695}
696
697/* Allocates up to two buffers for session <s>. Only succeeds if both buffers
698 * are properly allocated. It is meant to be called inside process_session() so
699 * that both request and response buffers are allocated. Returns 0 incase of
700 * failure, non-zero otherwise.
701 */
702int session_alloc_buffers(struct session *s)
703{
704 if (!s->req->buf->size && !b_alloc(&s->req->buf))
705 return 0;
706
707 if (s->rep->buf->size || b_alloc(&s->rep->buf))
708 return 1;
709
710 if (buffer_empty(s->req->buf)) {
711 __b_drop(&s->req->buf);
712 s->req->buf = &buf_wanted;
713 }
714
715 /* FIXME: normally we're supposed to subscribe to a list of waiters
716 * for buffers. We release what we failed to allocate.
717 */
718 return 0;
719}
720
721/* releases unused buffers after processing. Typically used at the end of the
722 * update() functions.
723 */
724void session_release_buffers(struct session *s)
725{
726 if (s->req->buf->size && buffer_empty(s->req->buf))
727 b_free(&s->req->buf);
728
729 if (s->rep->buf->size && buffer_empty(s->rep->buf))
730 b_free(&s->rep->buf);
731
732 /* FIXME: normally we want to wake up pending tasks */
733}
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200734
735/* perform minimal intializations, report 0 in case of error, 1 if OK. */
736int init_session()
737{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100738 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200739 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
740 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200741}
742
Willy Tarreau30e71012007-11-26 20:15:35 +0100743void session_process_counters(struct session *s)
744{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100745 unsigned long long bytes;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100746 void *ptr;
747 int i;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100748
Willy Tarreau30e71012007-11-26 20:15:35 +0100749 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100750 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100751 s->logs.bytes_in = s->req->total;
752 if (bytes) {
Willy Tarreau20d46a52012-12-09 15:55:40 +0100753 s->fe->fe_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100754
Willy Tarreau20d46a52012-12-09 15:55:40 +0100755 s->be->be_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100756
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100757 if (objt_server(s->target))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100758 objt_server(s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200759
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +0100760 if (s->listener && s->listener->counters)
Willy Tarreau20d46a52012-12-09 15:55:40 +0100761 s->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200762
Willy Tarreaub4c84932013-07-23 19:15:30 +0200763 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100764 if (!stkctr_entry(&s->stkctr[i]))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100765 continue;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200766
Willy Tarreau20d46a52012-12-09 15:55:40 +0100767 ptr = stktable_data_ptr(s->stkctr[i].table,
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100768 stkctr_entry(&s->stkctr[i]),
Willy Tarreau20d46a52012-12-09 15:55:40 +0100769 STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200770 if (ptr)
771 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200772
Willy Tarreau20d46a52012-12-09 15:55:40 +0100773 ptr = stktable_data_ptr(s->stkctr[i].table,
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100774 stkctr_entry(&s->stkctr[i]),
Willy Tarreau20d46a52012-12-09 15:55:40 +0100775 STKTABLE_DT_BYTES_IN_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200776 if (ptr)
777 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau20d46a52012-12-09 15:55:40 +0100778 s->stkctr[i].table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200779 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100780 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100781 }
782
Willy Tarreau30e71012007-11-26 20:15:35 +0100783 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100784 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100785 s->logs.bytes_out = s->rep->total;
786 if (bytes) {
Willy Tarreau20d46a52012-12-09 15:55:40 +0100787 s->fe->fe_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100788
Willy Tarreau20d46a52012-12-09 15:55:40 +0100789 s->be->be_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100790
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100791 if (objt_server(s->target))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100792 objt_server(s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200793
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +0100794 if (s->listener && s->listener->counters)
Willy Tarreau20d46a52012-12-09 15:55:40 +0100795 s->listener->counters->bytes_out += bytes;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200796
Willy Tarreaub4c84932013-07-23 19:15:30 +0200797 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100798 if (!stkctr_entry(&s->stkctr[i]))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100799 continue;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200800
Willy Tarreau20d46a52012-12-09 15:55:40 +0100801 ptr = stktable_data_ptr(s->stkctr[i].table,
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100802 stkctr_entry(&s->stkctr[i]),
Willy Tarreau20d46a52012-12-09 15:55:40 +0100803 STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200804 if (ptr)
805 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200806
Willy Tarreau20d46a52012-12-09 15:55:40 +0100807 ptr = stktable_data_ptr(s->stkctr[i].table,
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100808 stkctr_entry(&s->stkctr[i]),
Willy Tarreau20d46a52012-12-09 15:55:40 +0100809 STKTABLE_DT_BYTES_OUT_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200810 if (ptr)
811 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau20d46a52012-12-09 15:55:40 +0100812 s->stkctr[i].table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200813 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100814 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100815 }
816}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200817
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100818/* This function is called with (si->state == SI_ST_CON) meaning that a
819 * connection was attempted and that the file descriptor is already allocated.
820 * We must check for establishment, error and abort. Possible output states
821 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
822 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200823 * otherwise 1. This only works with connection-based sessions.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100824 */
Simon Hormandec5be42011-06-08 09:19:07 +0900825static int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100826{
Willy Tarreau7421efb2012-07-02 15:11:27 +0200827 struct channel *req = si->ob;
828 struct channel *rep = si->ib;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200829 struct connection *srv_conn = __objt_conn(si->end);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100830
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100831 /* If we got an error, or if nothing happened and the connection timed
832 * out, we must give up. The CER state handler will take care of retry
833 * attempts and error reports.
834 */
835 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreaue3224e82012-10-29 22:41:31 +0100836 if (unlikely(si->ob->flags & CF_WRITE_PARTIAL)) {
837 /* Some data were sent past the connection establishment,
838 * so we need to pretend we're established to log correctly
839 * and let later states handle the failure.
840 */
Willy Tarreaue3224e82012-10-29 22:41:31 +0100841 si->state = SI_ST_EST;
842 si->err_type = SI_ET_DATA_ERR;
843 si->ib->flags |= CF_READ_ERROR | CF_WRITE_ERROR;
Willy Tarreaue3224e82012-10-29 22:41:31 +0100844 return 1;
845 }
Willy Tarreau127334e2009-03-28 10:47:26 +0100846 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100847 si->state = SI_ST_CER;
Willy Tarreau0ede5a32012-12-08 08:44:02 +0100848
Willy Tarreauf79c8172013-10-21 16:30:56 +0200849 conn_force_close(srv_conn);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100850
851 if (si->err_type)
852 return 0;
853
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100854 if (si->flags & SI_FL_ERR)
855 si->err_type = SI_ET_CONN_ERR;
856 else
857 si->err_type = SI_ET_CONN_TO;
858 return 0;
859 }
860
861 /* OK, maybe we want to abort */
Willy Tarreaua7a7ebc2012-12-30 00:50:35 +0100862 if (!(req->flags & CF_WRITE_PARTIAL) &&
863 unlikely((rep->flags & CF_SHUTW) ||
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200864 ((req->flags & CF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
865 ((!(req->flags & CF_WRITE_ACTIVITY) && channel_is_empty(req)) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100866 s->be->options & PR_O_ABRT_CLOSE)))) {
867 /* give up */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200868 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100869 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau84455332009-03-15 22:34:05 +0100870 if (s->srv_error)
871 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100872 return 1;
873 }
874
875 /* we need to wait a bit more if there was no activity either */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200876 if (!(req->flags & CF_WRITE_ACTIVITY))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100877 return 1;
878
879 /* OK, this means that a connection succeeded. The caller will be
880 * responsible for handling the transition from CON to EST.
881 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100882 si->state = SI_ST_EST;
883 si->err_type = SI_ET_NONE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100884 return 1;
885}
886
887/* This function is called with (si->state == SI_ST_CER) meaning that a
888 * previous connection attempt has failed and that the file descriptor
889 * has already been released. Possible causes include asynchronous error
890 * notification and time out. Possible output states are SI_ST_CLO when
891 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
892 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
893 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
894 * marked as in error state. It returns 0.
895 */
Simon Hormandec5be42011-06-08 09:19:07 +0900896static int sess_update_st_cer(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100897{
898 /* we probably have to release last session from the server */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100899 if (objt_server(s->target)) {
900 health_adjust(objt_server(s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100901
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100902 if (s->flags & SN_CURR_SESS) {
903 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100904 objt_server(s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100905 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100906 }
907
908 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200909 si->conn_retries--;
910 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100911 if (!si->err_type) {
912 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100913 }
914
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100915 if (objt_server(s->target))
916 objt_server(s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100917 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100918 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100919 if (may_dequeue_tasks(objt_server(s->target), s->be))
920 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100921
922 /* shutw is enough so stop a connecting socket */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200923 si_shutw(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200924 si->ob->flags |= CF_WRITE_ERROR;
925 si->ib->flags |= CF_READ_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100926
927 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100928 if (s->srv_error)
929 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100930 return 0;
931 }
932
933 /* If the "redispatch" option is set on the backend, we are allowed to
934 * retry on another server for the last retry. In order to achieve this,
935 * we must mark the session unassigned, and eventually clear the DIRECT
936 * bit to ignore any persistence cookie. We won't count a retry nor a
937 * redispatch yet, because this will depend on what server is selected.
Willy Tarreau33a14e52014-06-13 17:49:40 +0200938 * If the connection is not persistent, the balancing algorithm is not
939 * determinist (round robin) and there is more than one active server,
940 * we accept to perform an immediate redispatch without waiting since
941 * we don't care about this particular server.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100942 */
Willy Tarreau33a14e52014-06-13 17:49:40 +0200943 if (objt_server(s->target) &&
944 (si->conn_retries == 0 ||
945 (!(s->flags & SN_DIRECT) && s->be->srv_act > 1 &&
946 ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR))) &&
Willy Tarreau4de91492010-01-22 19:10:05 +0100947 s->be->options & PR_O_REDISP && !(s->flags & SN_FORCE_PRST)) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100948 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100949 if (may_dequeue_tasks(objt_server(s->target), s->be))
950 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100951
952 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100953 si->state = SI_ST_REQ;
954 } else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100955 if (objt_server(s->target))
956 objt_server(s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100957 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100958 si->state = SI_ST_ASS;
959 }
960
961 if (si->flags & SI_FL_ERR) {
962 /* The error was an asynchronous connection error, and we will
963 * likely have to retry connecting to the same server, most
964 * likely leading to the same result. To avoid this, we wait
Willy Tarreaub0290662014-06-13 17:04:44 +0200965 * MIN(one second, connect timeout) before retrying.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100966 */
967
Willy Tarreaub0290662014-06-13 17:04:44 +0200968 int delay = 1000;
969
970 if (s->be->timeout.connect && s->be->timeout.connect < delay)
971 delay = s->be->timeout.connect;
972
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100973 if (!si->err_type)
974 si->err_type = SI_ET_CONN_ERR;
975
Willy Tarreaudb6d0122014-06-13 17:40:15 +0200976 /* only wait when we're retrying on the same server */
977 if (si->state == SI_ST_ASS ||
978 (s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_RR ||
979 (s->be->srv_act <= 1)) {
980 si->state = SI_ST_TAR;
981 si->exp = tick_add(now_ms, MS_TO_TICKS(delay));
982 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100983 return 0;
984 }
985 return 0;
986}
987
988/*
989 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200990 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
Willy Tarreau26d8c592012-05-07 18:12:14 +0200991 * SI_ST_INI) to SI_ST_EST, but only when a ->proto is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100992 */
Simon Hormandec5be42011-06-08 09:19:07 +0900993static void sess_establish(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100994{
Willy Tarreau7421efb2012-07-02 15:11:27 +0200995 struct channel *req = si->ob;
996 struct channel *rep = si->ib;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100997
Willy Tarreau0e37f1c2013-12-31 23:06:46 +0100998 /* First, centralize the timers information */
999 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
1000 si->exp = TICK_ETERNITY;
1001
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001002 if (objt_server(s->target))
1003 health_adjust(objt_server(s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001004
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001005 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001006 /* if the user wants to log as soon as possible, without counting
1007 * bytes from the server, then this is the right moment. */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001008 if (!LIST_ISEMPTY(&s->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001009 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001010 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001011 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001012 }
1013 else {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001014 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
Willy Tarreaud81ca042013-12-31 22:33:13 +01001015 rep->flags |= CF_READ_DONTWAIT; /* a single read is enough to get response headers */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001016 }
1017
Willy Tarreau4e5b8282009-08-16 22:57:50 +02001018 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001019 rep->flags |= CF_READ_ATTACHED; /* producer is now attached */
Willy Tarreau644c1012014-04-30 18:11:11 +02001020 if (req->flags & CF_WAKE_CONNECT) {
1021 req->flags |= CF_WAKE_ONCE;
1022 req->flags &= ~CF_WAKE_CONNECT;
1023 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001024 if (objt_conn(si->end)) {
Willy Tarreaud04e8582010-05-31 12:31:35 +02001025 /* real connections have timeouts */
1026 req->wto = s->be->timeout.server;
1027 rep->rto = s->be->timeout.server;
1028 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001029 req->wex = TICK_ETERNITY;
1030}
1031
1032/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
1033 * Other input states are simply ignored.
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01001034 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON
1035 * and SI_ST_EST. Flags must have previously been updated for timeouts and other
1036 * conditions.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001037 */
Simon Hormandec5be42011-06-08 09:19:07 +09001038static void sess_update_stream_int(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001039{
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001040 struct server *srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001041
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001042 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 +01001043 now_ms, __FUNCTION__,
1044 s,
1045 s->req, s->rep,
1046 s->req->rex, s->rep->wex,
1047 s->req->flags, s->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001048 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 +01001049
1050 if (si->state == SI_ST_ASS) {
1051 /* Server assigned to connection request, we have to try to connect now */
1052 int conn_err;
1053
1054 conn_err = connect_server(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001055 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001056
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001057 if (conn_err == SN_ERR_NONE) {
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01001058 /* state = SI_ST_CON or SI_ST_EST now */
Willy Tarreau827aee92011-03-10 16:55:02 +01001059 if (srv)
1060 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001061 if (srv)
1062 srv_set_sess_last(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001063 return;
1064 }
1065
1066 /* We have received a synchronous error. We might have to
1067 * abort, retry immediately or redispatch.
1068 */
1069 if (conn_err == SN_ERR_INTERNAL) {
1070 if (!si->err_type) {
1071 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001072 }
1073
Willy Tarreau827aee92011-03-10 16:55:02 +01001074 if (srv)
1075 srv_inc_sess_ctr(srv);
1076 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001077 srv_set_sess_last(srv);
1078 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +01001079 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001080 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001081
1082 /* release other sessions waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +01001083 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001084 if (may_dequeue_tasks(srv, s->be))
1085 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001086
1087 /* Failed and not retryable. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001088 si_shutr(si);
1089 si_shutw(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001090 si->ob->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001091
1092 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1093
1094 /* no session was ever accounted for this server */
1095 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001096 if (s->srv_error)
1097 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001098 return;
1099 }
1100
1101 /* We are facing a retryable error, but we don't want to run a
1102 * turn-around now, as the problem is likely a source port
1103 * allocation problem, so we want to retry now.
1104 */
1105 si->state = SI_ST_CER;
1106 si->flags &= ~SI_FL_ERR;
1107 sess_update_st_cer(s, si);
1108 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
1109 return;
1110 }
1111 else if (si->state == SI_ST_QUE) {
1112 /* connection request was queued, check for any update */
1113 if (!s->pend_pos) {
1114 /* The connection is not in the queue anymore. Either
1115 * we have a server connection slot available and we
1116 * go directly to the assigned state, or we need to
1117 * load-balance first and go to the INI state.
1118 */
1119 si->exp = TICK_ETERNITY;
1120 if (unlikely(!(s->flags & SN_ASSIGNED)))
1121 si->state = SI_ST_REQ;
1122 else {
1123 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1124 si->state = SI_ST_ASS;
1125 }
1126 return;
1127 }
1128
1129 /* Connection request still in queue... */
1130 if (si->flags & SI_FL_EXP) {
1131 /* ... and timeout expired */
1132 si->exp = TICK_ETERNITY;
1133 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +01001134 if (srv)
1135 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001136 s->be->be_counters.failed_conns++;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001137 si_shutr(si);
1138 si_shutw(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001139 si->ob->flags |= CF_WRITE_TIMEOUT;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001140 if (!si->err_type)
1141 si->err_type = SI_ET_QUEUE_TO;
1142 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001143 if (s->srv_error)
1144 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001145 return;
1146 }
1147
1148 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001149 if ((si->ob->flags & (CF_READ_ERROR)) ||
1150 ((si->ob->flags & CF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreau8e21bb92012-08-24 22:40:29 +02001151 (channel_is_empty(si->ob) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001152 /* give up */
1153 si->exp = TICK_ETERNITY;
1154 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau73b013b2012-05-21 16:31:45 +02001155 si_shutr(si);
1156 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001157 si->err_type |= SI_ET_QUEUE_ABRT;
1158 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001159 if (s->srv_error)
1160 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001161 return;
1162 }
1163
1164 /* Nothing changed */
1165 return;
1166 }
1167 else if (si->state == SI_ST_TAR) {
1168 /* Connection request might be aborted */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001169 if ((si->ob->flags & (CF_READ_ERROR)) ||
1170 ((si->ob->flags & CF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreau8e21bb92012-08-24 22:40:29 +02001171 (channel_is_empty(si->ob) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001172 /* give up */
1173 si->exp = TICK_ETERNITY;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001174 si_shutr(si);
1175 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001176 si->err_type |= SI_ET_CONN_ABRT;
1177 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001178 if (s->srv_error)
1179 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001180 return;
1181 }
1182
1183 if (!(si->flags & SI_FL_EXP))
1184 return; /* still in turn-around */
1185
1186 si->exp = TICK_ETERNITY;
1187
1188 /* we keep trying on the same server as long as the session is
1189 * marked "assigned".
1190 * FIXME: Should we force a redispatch attempt when the server is down ?
1191 */
1192 if (s->flags & SN_ASSIGNED)
1193 si->state = SI_ST_ASS;
1194 else
1195 si->state = SI_ST_REQ;
1196 return;
1197 }
1198}
1199
Simon Hormandec5be42011-06-08 09:19:07 +09001200/* Set correct session termination flags in case no analyser has done it. It
1201 * also counts a failed request if the server state has not reached the request
1202 * stage.
1203 */
1204static void sess_set_term_flags(struct session *s)
1205{
1206 if (!(s->flags & SN_FINST_MASK)) {
1207 if (s->si[1].state < SI_ST_REQ) {
1208
1209 s->fe->fe_counters.failed_req++;
1210 if (s->listener->counters)
1211 s->listener->counters->failed_req++;
1212
1213 s->flags |= SN_FINST_R;
1214 }
1215 else if (s->si[1].state == SI_ST_QUE)
1216 s->flags |= SN_FINST_Q;
1217 else if (s->si[1].state < SI_ST_EST)
1218 s->flags |= SN_FINST_C;
1219 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
1220 s->flags |= SN_FINST_D;
1221 else
1222 s->flags |= SN_FINST_L;
1223 }
1224}
1225
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001226/* This function initiates a server connection request on a stream interface
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001227 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS for
1228 * a real connection to a server, indicating that a server has been assigned,
1229 * or SI_ST_EST for a successful connection to an applet. It may also return
1230 * SI_ST_QUE, or SI_ST_CLO upon error.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001231 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001232static void sess_prepare_conn_req(struct session *s, struct stream_interface *si)
1233{
1234 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 +01001235 now_ms, __FUNCTION__,
1236 s,
1237 s->req, s->rep,
1238 s->req->rex, s->rep->wex,
1239 s->req->flags, s->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001240 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 +01001241
1242 if (si->state != SI_ST_REQ)
1243 return;
1244
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001245 if (unlikely(obj_type(s->target) == OBJ_TYPE_APPLET)) {
1246 /* the applet directly goes to the EST state */
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001247 struct appctx *appctx = objt_appctx(si->end);
1248
1249 if (!appctx || appctx->applet != __objt_applet(s->target))
1250 appctx = stream_int_register_handler(si, objt_applet(s->target));
1251
1252 if (!appctx) {
1253 /* No more memory, let's immediately abort. Force the
1254 * error code to ignore the ERR_LOCAL which is not a
1255 * real error.
1256 */
Willy Tarreau6bbb2f62013-12-09 17:14:23 +01001257 s->flags &= ~(SN_ERR_MASK | SN_FINST_MASK);
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001258
1259 si_shutr(si);
1260 si_shutw(si);
1261 si->ob->flags |= CF_WRITE_ERROR;
Willy Tarreau6bbb2f62013-12-09 17:14:23 +01001262 si->err_type = SI_ET_CONN_RES;
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001263 si->state = SI_ST_CLO;
1264 if (s->srv_error)
1265 s->srv_error(s, si);
1266 return;
1267 }
1268
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001269 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001270 si->state = SI_ST_EST;
1271 si->err_type = SI_ET_NONE;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001272 be_set_sess_last(s->be);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01001273 /* let sess_establish() finish the job */
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001274 return;
1275 }
1276
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001277 /* Try to assign a server */
1278 if (srv_redispatch_connect(s) != 0) {
1279 /* We did not get a server. Either we queued the
1280 * connection request, or we encountered an error.
1281 */
1282 if (si->state == SI_ST_QUE)
1283 return;
1284
1285 /* we did not get any server, let's check the cause */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001286 si_shutr(si);
1287 si_shutw(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001288 si->ob->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001289 if (!si->err_type)
1290 si->err_type = SI_ET_CONN_OTHER;
1291 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001292 if (s->srv_error)
1293 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001294 return;
1295 }
1296
1297 /* The server is assigned */
1298 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1299 si->state = SI_ST_ASS;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001300 be_set_sess_last(s->be);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001301}
1302
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001303/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +01001304 * if appropriate. The default_backend rule is also considered, then the
1305 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001306 * It returns 1 if the processing can continue on next analysers, or zero if it
1307 * either needs more data or wants to immediately abort the request.
1308 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001309static int process_switching_rules(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001310{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001311 struct persist_rule *prst_rule;
Willy Tarreau4de91492010-01-22 19:10:05 +01001312
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001313 req->analysers &= ~an_bit;
1314 req->analyse_exp = TICK_ETERNITY;
1315
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001316 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 +02001317 now_ms, __FUNCTION__,
1318 s,
1319 req,
1320 req->rex, req->wex,
1321 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001322 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001323 req->analysers);
1324
1325 /* now check whether we have some switching rules for this request */
1326 if (!(s->flags & SN_BE_ASSIGNED)) {
1327 struct switching_rule *rule;
1328
1329 list_for_each_entry(rule, &s->fe->switching_rules, list) {
Willy Tarreauf51658d2014-04-23 01:21:56 +02001330 int ret = 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001331
Willy Tarreauf51658d2014-04-23 01:21:56 +02001332 if (rule->cond) {
1333 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
1334 ret = acl_pass(ret);
1335 if (rule->cond->pol == ACL_COND_UNLESS)
1336 ret = !ret;
1337 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001338
1339 if (ret) {
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001340 /* If the backend name is dynamic, try to resolve the name.
1341 * If we can't resolve the name, or if any error occurs, break
1342 * the loop and fallback to the default backend.
1343 */
1344 struct proxy *backend;
1345
1346 if (rule->dynamic) {
1347 struct chunk *tmp = get_trash_chunk();
1348 if (!build_logline(s, tmp->str, tmp->size, &rule->be.expr))
1349 break;
1350 backend = findproxy(tmp->str, PR_CAP_BE);
1351 if (!backend)
1352 break;
1353 }
1354 else
1355 backend = rule->be.backend;
1356
1357 if (!session_set_backend(s, backend))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001358 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001359 break;
1360 }
1361 }
1362
1363 /* To ensure correct connection accounting on the backend, we
1364 * have to assign one if it was not set (eg: a listen). This
1365 * measure also takes care of correctly setting the default
1366 * backend if any.
1367 */
1368 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001369 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
1370 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001371 }
1372
Willy Tarreaufb356202010-08-03 14:02:05 +02001373 /* we don't want to run the TCP or HTTP filters again if the backend has not changed */
1374 if (s->fe == s->be) {
1375 s->req->analysers &= ~AN_REQ_INSPECT_BE;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001376 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001377 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001378
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001379 /* 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 +01001380 * persistence rule, and report that in the session.
1381 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001382 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001383 int ret = 1;
1384
1385 if (prst_rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001386 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 +01001387 ret = acl_pass(ret);
1388 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1389 ret = !ret;
1390 }
1391
1392 if (ret) {
1393 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001394 if (prst_rule->type == PERSIST_TYPE_FORCE) {
1395 s->flags |= SN_FORCE_PRST;
1396 } else {
1397 s->flags |= SN_IGNORE_PRST;
1398 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001399 break;
1400 }
1401 }
1402
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001403 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001404
1405 sw_failed:
1406 /* immediately abort this request in case of allocation failure */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001407 channel_abort(s->req);
1408 channel_abort(s->rep);
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001409
1410 if (!(s->flags & SN_ERR_MASK))
1411 s->flags |= SN_ERR_RESOURCE;
1412 if (!(s->flags & SN_FINST_MASK))
1413 s->flags |= SN_FINST_R;
1414
1415 s->txn.status = 500;
1416 s->req->analysers = 0;
1417 s->req->analyse_exp = TICK_ETERNITY;
1418 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001419}
1420
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001421/* This stream analyser works on a request. It applies all use-server rules on
1422 * it then returns 1. The data must already be present in the buffer otherwise
1423 * they won't match. It always returns 1.
1424 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001425static int process_server_rules(struct session *s, struct channel *req, int an_bit)
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001426{
1427 struct proxy *px = s->be;
1428 struct server_rule *rule;
1429
1430 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1431 now_ms, __FUNCTION__,
1432 s,
1433 req,
1434 req->rex, req->wex,
1435 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001436 req->buf->i + req->buf->o,
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001437 req->analysers);
1438
1439 if (!(s->flags & SN_ASSIGNED)) {
1440 list_for_each_entry(rule, &px->server_rules, list) {
1441 int ret;
1442
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001443 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 +02001444 ret = acl_pass(ret);
1445 if (rule->cond->pol == ACL_COND_UNLESS)
1446 ret = !ret;
1447
1448 if (ret) {
1449 struct server *srv = rule->srv.ptr;
1450
Willy Tarreau892337c2014-05-13 23:41:20 +02001451 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001452 (px->options & PR_O_PERSIST) ||
1453 (s->flags & SN_FORCE_PRST)) {
1454 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001455 s->target = &srv->obj_type;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001456 break;
1457 }
1458 /* if the server is not UP, let's go on with next rules
1459 * just in case another one is suited.
1460 */
1461 }
1462 }
1463 }
1464
1465 req->analysers &= ~an_bit;
1466 req->analyse_exp = TICK_ETERNITY;
1467 return 1;
1468}
1469
Emeric Brun1d33b292010-01-04 15:47:17 +01001470/* This stream analyser works on a request. It applies all sticking rules on
1471 * it then returns 1. The data must already be present in the buffer otherwise
1472 * they won't match. It always returns 1.
1473 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001474static int process_sticking_rules(struct session *s, struct channel *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001475{
1476 struct proxy *px = s->be;
1477 struct sticking_rule *rule;
1478
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001479 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 +01001480 now_ms, __FUNCTION__,
1481 s,
1482 req,
1483 req->rex, req->wex,
1484 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001485 req->buf->i,
Emeric Brun1d33b292010-01-04 15:47:17 +01001486 req->analysers);
1487
1488 list_for_each_entry(rule, &px->sticking_rules, list) {
1489 int ret = 1 ;
1490 int i;
1491
Willy Tarreau9667a802013-12-09 12:52:13 +01001492 /* Only the first stick store-request of each table is applied
1493 * and other ones are ignored. The purpose is to allow complex
1494 * configurations which look for multiple entries by decreasing
1495 * order of precision and to stop at the first which matches.
1496 * An example could be a store of the IP address from an HTTP
1497 * header first, then from the source if not found.
1498 */
Emeric Brun1d33b292010-01-04 15:47:17 +01001499 for (i = 0; i < s->store_count; i++) {
1500 if (rule->table.t == s->store[i].table)
1501 break;
1502 }
1503
1504 if (i != s->store_count)
1505 continue;
1506
1507 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001508 ret = acl_exec_cond(rule->cond, px, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001509 ret = acl_pass(ret);
1510 if (rule->cond->pol == ACL_COND_UNLESS)
1511 ret = !ret;
1512 }
1513
1514 if (ret) {
1515 struct stktable_key *key;
1516
Willy Tarreaub5975de2014-06-25 16:20:53 +02001517 key = stktable_fetch_key(rule->table.t, px, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->expr, NULL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001518 if (!key)
1519 continue;
1520
1521 if (rule->flags & STK_IS_MATCH) {
1522 struct stksess *ts;
1523
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001524 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001525 if (!(s->flags & SN_ASSIGNED)) {
1526 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001527 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001528
1529 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001530 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1531 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001532 if (node) {
1533 struct server *srv;
1534
1535 srv = container_of(node, struct server, conf.id);
Willy Tarreau892337c2014-05-13 23:41:20 +02001536 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4de91492010-01-22 19:10:05 +01001537 (px->options & PR_O_PERSIST) ||
1538 (s->flags & SN_FORCE_PRST)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001539 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001540 s->target = &srv->obj_type;
Emeric Brun1d33b292010-01-04 15:47:17 +01001541 }
1542 }
1543 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001544 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001545 }
1546 }
1547 if (rule->flags & STK_IS_STORE) {
1548 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1549 struct stksess *ts;
1550
1551 ts = stksess_new(rule->table.t, key);
1552 if (ts) {
1553 s->store[s->store_count].table = rule->table.t;
1554 s->store[s->store_count++].ts = ts;
1555 }
1556 }
1557 }
1558 }
1559 }
1560
1561 req->analysers &= ~an_bit;
1562 req->analyse_exp = TICK_ETERNITY;
1563 return 1;
1564}
1565
1566/* This stream analyser works on a response. It applies all store rules on it
1567 * then returns 1. The data must already be present in the buffer otherwise
1568 * they won't match. It always returns 1.
1569 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001570static int process_store_rules(struct session *s, struct channel *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001571{
1572 struct proxy *px = s->be;
1573 struct sticking_rule *rule;
1574 int i;
Willy Tarreau9667a802013-12-09 12:52:13 +01001575 int nbreq = s->store_count;
Emeric Brun1d33b292010-01-04 15:47:17 +01001576
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001577 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 +01001578 now_ms, __FUNCTION__,
1579 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001580 rep,
1581 rep->rex, rep->wex,
1582 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001583 rep->buf->i,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001584 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001585
1586 list_for_each_entry(rule, &px->storersp_rules, list) {
1587 int ret = 1 ;
Emeric Brun1d33b292010-01-04 15:47:17 +01001588
Willy Tarreau9667a802013-12-09 12:52:13 +01001589 /* Only the first stick store-response of each table is applied
1590 * and other ones are ignored. The purpose is to allow complex
1591 * configurations which look for multiple entries by decreasing
1592 * order of precision and to stop at the first which matches.
1593 * An example could be a store of a set-cookie value, with a
1594 * fallback to a parameter found in a 302 redirect.
1595 *
1596 * The store-response rules are not allowed to override the
1597 * store-request rules for the same table, but they may coexist.
1598 * Thus we can have up to one store-request entry and one store-
1599 * response entry for the same table at any time.
1600 */
1601 for (i = nbreq; i < s->store_count; i++) {
1602 if (rule->table.t == s->store[i].table)
1603 break;
1604 }
1605
1606 /* skip existing entries for this table */
1607 if (i < s->store_count)
1608 continue;
1609
Emeric Brun1d33b292010-01-04 15:47:17 +01001610 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001611 ret = acl_exec_cond(rule->cond, px, s, &s->txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001612 ret = acl_pass(ret);
1613 if (rule->cond->pol == ACL_COND_UNLESS)
1614 ret = !ret;
1615 }
1616
1617 if (ret) {
1618 struct stktable_key *key;
1619
Willy Tarreaub5975de2014-06-25 16:20:53 +02001620 key = stktable_fetch_key(rule->table.t, px, s, &s->txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->expr, NULL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001621 if (!key)
1622 continue;
1623
Willy Tarreau37e340c2013-12-06 23:05:21 +01001624 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001625 struct stksess *ts;
1626
1627 ts = stksess_new(rule->table.t, key);
1628 if (ts) {
1629 s->store[s->store_count].table = rule->table.t;
Emeric Brun1d33b292010-01-04 15:47:17 +01001630 s->store[s->store_count++].ts = ts;
1631 }
1632 }
1633 }
1634 }
1635
1636 /* process store request and store response */
1637 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001638 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001639 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001640
Willy Tarreauc93cd162014-05-13 15:54:22 +02001641 if (objt_server(s->target) && objt_server(s->target)->flags & SRV_F_NON_STICK) {
Simon Hormanfa461682011-06-25 09:39:49 +09001642 stksess_free(s->store[i].table, s->store[i].ts);
1643 s->store[i].ts = NULL;
1644 continue;
1645 }
1646
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001647 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1648 if (ts) {
1649 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001650 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001651 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001652 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001653 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001654 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001655
1656 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001657 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001658 stktable_data_cast(ptr, server_id) = objt_server(s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001659 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001660 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001661
1662 rep->analysers &= ~an_bit;
1663 rep->analyse_exp = TICK_ETERNITY;
1664 return 1;
1665}
1666
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001667/* This macro is very specific to the function below. See the comments in
1668 * process_session() below to understand the logic and the tests.
1669 */
1670#define UPDATE_ANALYSERS(real, list, back, flag) { \
1671 list = (((list) & ~(flag)) | ~(back)) & (real); \
1672 back = real; \
1673 if (!(list)) \
1674 break; \
1675 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1676 continue; \
1677}
1678
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001679/* Processes the client, server, request and response jobs of a session task,
1680 * then puts it back to the wait queue in a clean state, or cleans up its
1681 * resources if it must be deleted. Returns in <next> the date the task wants
1682 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1683 * nothing too many times, the request and response buffers flags are monitored
1684 * and each function is called only if at least another function has changed at
1685 * least one flag it is interested in.
1686 */
Willy Tarreau26c25062009-03-08 09:38:41 +01001687struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001688{
Willy Tarreau827aee92011-03-10 16:55:02 +01001689 struct server *srv;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001690 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001691 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001692 unsigned int rq_prod_last, rq_cons_last;
1693 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001694 unsigned int req_ana_back;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001695
1696 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
1697 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
1698
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001699 /* this data may be no longer valid, clear it */
1700 memset(&s->txn.auth, 0, sizeof(s->txn.auth));
1701
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02001702 /* This flag must explicitly be set every time */
1703 s->req->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
1704 s->rep->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001705
1706 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001707 rqf_last = s->req->flags & ~CF_MASK_ANALYSER;
1708 rpf_last = s->rep->flags & ~CF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001709
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001710 /* we don't want the stream interface functions to recursively wake us up */
1711 if (s->req->prod->owner == t)
1712 s->req->prod->flags |= SI_FL_DONT_WAKE;
1713 if (s->req->cons->owner == t)
1714 s->req->cons->flags |= SI_FL_DONT_WAKE;
1715
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001716 /* 1a: Check for low level timeouts if needed. We just set a flag on
1717 * stream interfaces when their timeouts have expired.
1718 */
1719 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
1720 stream_int_check_timeouts(&s->si[0]);
1721 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001722
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001723 /* check channel timeouts, and close the corresponding stream interfaces
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001724 * for future reads or writes. Note: this will also concern upper layers
1725 * but we do not touch any other flag. We must be careful and correctly
1726 * detect state changes when calling them.
1727 */
1728
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001729 channel_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001730
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001731 if (unlikely((s->req->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
Willy Tarreau14641402009-12-29 14:49:56 +01001732 s->req->cons->flags |= SI_FL_NOLINGER;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001733 si_shutw(s->req->cons);
Willy Tarreau14641402009-12-29 14:49:56 +01001734 }
1735
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001736 if (unlikely((s->req->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001737 if (s->req->prod->flags & SI_FL_NOHALF)
1738 s->req->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001739 si_shutr(s->req->prod);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001740 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001741
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001742 channel_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001743
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001744 if (unlikely((s->rep->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
Willy Tarreau14641402009-12-29 14:49:56 +01001745 s->rep->cons->flags |= SI_FL_NOLINGER;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001746 si_shutw(s->rep->cons);
Willy Tarreau14641402009-12-29 14:49:56 +01001747 }
1748
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001749 if (unlikely((s->rep->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001750 if (s->rep->prod->flags & SI_FL_NOHALF)
1751 s->rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001752 si_shutr(s->rep->prod);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001753 }
Willy Tarreau798f4322012-11-08 14:49:17 +01001754
1755 /* Once in a while we're woken up because the task expires. But
1756 * this does not necessarily mean that a timeout has been reached.
1757 * So let's not run a whole session processing if only an expiration
1758 * timeout needs to be refreshed.
1759 */
1760 if (!((s->req->flags | s->rep->flags) &
1761 (CF_SHUTR|CF_READ_ACTIVITY|CF_READ_TIMEOUT|CF_SHUTW|
1762 CF_WRITE_ACTIVITY|CF_WRITE_TIMEOUT|CF_ANA_TIMEOUT)) &&
1763 !((s->si[0].flags | s->si[1].flags) & (SI_FL_EXP|SI_FL_ERR)) &&
1764 ((t->state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER))
1765 goto update_exp_and_leave;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001766 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001767
1768 /* 1b: check for low-level errors reported at the stream interface.
1769 * First we check if it's a retryable error (in which case we don't
1770 * want to tell the buffer). Otherwise we report the error one level
1771 * upper by setting flags into the buffers. Note that the side towards
1772 * the client cannot have connect (hence retryable) errors. Also, the
1773 * connection setup code must be able to deal with any type of abort.
1774 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001775 srv = objt_server(s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001776 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
1777 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
Willy Tarreau73b013b2012-05-21 16:31:45 +02001778 si_shutr(&s->si[0]);
1779 si_shutw(&s->si[0]);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001780 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001781 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001782 s->be->be_counters.cli_aborts++;
1783 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001784 if (srv)
1785 srv->counters.cli_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001786 if (!(s->flags & SN_ERR_MASK))
1787 s->flags |= SN_ERR_CLICL;
1788 if (!(s->flags & SN_FINST_MASK))
1789 s->flags |= SN_FINST_D;
1790 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001791 }
1792 }
1793
1794 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
1795 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
Willy Tarreau73b013b2012-05-21 16:31:45 +02001796 si_shutr(&s->si[1]);
1797 si_shutw(&s->si[1]);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001798 stream_int_report_error(&s->si[1]);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001799 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001800 if (srv)
1801 srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001802 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001803 s->be->be_counters.srv_aborts++;
1804 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001805 if (srv)
1806 srv->counters.srv_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001807 if (!(s->flags & SN_ERR_MASK))
1808 s->flags |= SN_ERR_SRVCL;
1809 if (!(s->flags & SN_FINST_MASK))
1810 s->flags |= SN_FINST_D;
1811 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001812 }
1813 /* note: maybe we should process connection errors here ? */
1814 }
1815
1816 if (s->si[1].state == SI_ST_CON) {
1817 /* we were trying to establish a connection on the server side,
1818 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1819 */
1820 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
1821 sess_update_st_cer(s, &s->si[1]);
1822 else if (s->si[1].state == SI_ST_EST)
1823 sess_establish(s, &s->si[1]);
1824
1825 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1826 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1827 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1828 */
1829 }
1830
Willy Tarreau815a9b22010-07-27 17:15:12 +02001831 rq_prod_last = s->si[0].state;
1832 rq_cons_last = s->si[1].state;
1833 rp_cons_last = s->si[0].state;
1834 rp_prod_last = s->si[1].state;
1835
1836 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001837 /* Check for connection closure */
1838
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001839 DPRINTF(stderr,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001840 "[%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 +01001841 now_ms, __FUNCTION__, __LINE__,
1842 t,
1843 s, s->flags,
1844 s->req, s->rep,
1845 s->req->rex, s->rep->wex,
1846 s->req->flags, s->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001847 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 +01001848 s->rep->cons->err_type, s->req->cons->err_type,
Willy Tarreauee28de02010-06-01 09:51:00 +02001849 s->req->cons->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001850
1851 /* nothing special to be done on client side */
1852 if (unlikely(s->req->prod->state == SI_ST_DIS))
1853 s->req->prod->state = SI_ST_CLO;
1854
1855 /* When a server-side connection is released, we have to count it and
1856 * check for pending connections on this server.
1857 */
1858 if (unlikely(s->req->cons->state == SI_ST_DIS)) {
1859 s->req->cons->state = SI_ST_CLO;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001860 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001861 if (srv) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001862 if (s->flags & SN_CURR_SESS) {
1863 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001864 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001865 }
1866 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001867 if (may_dequeue_tasks(srv, s->be))
1868 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001869 }
1870 }
1871
1872 /*
1873 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1874 * at this point.
1875 */
1876
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001877 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001878 /* Analyse request */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001879 if (((s->req->flags & ~rqf_last) & CF_MASK_ANALYSER) ||
1880 ((s->req->flags ^ rqf_last) & CF_MASK_STATIC) ||
Willy Tarreau815a9b22010-07-27 17:15:12 +02001881 s->si[0].state != rq_prod_last ||
1882 s->si[1].state != rq_cons_last) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001883 unsigned int flags = s->req->flags;
1884
1885 if (s->req->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001886 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001887 unsigned int ana_list;
1888 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001889
Willy Tarreau90deb182010-01-07 00:20:41 +01001890 /* it's up to the analysers to stop new connections,
1891 * disable reading or closing. Note: if an analyser
1892 * disables any of these bits, it is responsible for
1893 * enabling them again when it disables itself, so
1894 * that other analysers are called in similar conditions.
1895 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001896 channel_auto_read(s->req);
1897 channel_auto_connect(s->req);
1898 channel_auto_close(s->req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001899
1900 /* We will call all analysers for which a bit is set in
1901 * s->req->analysers, following the bit order from LSB
1902 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001903 * the list when not needed. Any analyser may return 0
1904 * to break out of the loop, either because of missing
1905 * data to take a decision, or because it decides to
1906 * kill the session. We loop at least once through each
1907 * analyser, and we may loop again if other analysers
1908 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001909 *
1910 * We build a list of analysers to run. We evaluate all
1911 * of these analysers in the order of the lower bit to
1912 * the higher bit. This ordering is very important.
1913 * An analyser will often add/remove other analysers,
1914 * including itself. Any changes to itself have no effect
1915 * on the loop. If it removes any other analysers, we
1916 * want those analysers not to be called anymore during
1917 * this loop. If it adds an analyser that is located
1918 * after itself, we want it to be scheduled for being
1919 * processed during the loop. If it adds an analyser
1920 * which is located before it, we want it to switch to
1921 * it immediately, even if it has already been called
1922 * once but removed since.
1923 *
1924 * In order to achieve this, we compare the analyser
1925 * list after the call with a copy of it before the
1926 * call. The work list is fed with analyser bits that
1927 * appeared during the call. Then we compare previous
1928 * work list with the new one, and check the bits that
1929 * appeared. If the lowest of these bits is lower than
1930 * the current bit, it means we have enabled a previous
1931 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001932 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001933
1934 ana_list = ana_back = s->req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001935 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001936 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001937
Willy Tarreaufb356202010-08-03 14:02:05 +02001938 if (ana_list & AN_REQ_INSPECT_FE) {
1939 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001940 break;
Willy Tarreaufb356202010-08-03 14:02:05 +02001941 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001942 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001943
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001944 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001945 if (!http_wait_for_request(s, s->req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001946 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001947 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001948 }
1949
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001950 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001951 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_FE, s->fe))
1952 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001953 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001954 }
1955
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001956 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001957 if (!process_switching_rules(s, s->req, AN_REQ_SWITCHING_RULES))
1958 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001959 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001960 }
1961
Willy Tarreaufb356202010-08-03 14:02:05 +02001962 if (ana_list & AN_REQ_INSPECT_BE) {
1963 if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT_BE))
1964 break;
1965 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
1966 }
1967
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001968 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001969 if (!http_process_req_common(s, s->req, AN_REQ_HTTP_PROCESS_BE, s->be))
1970 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001971 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001972 }
1973
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001974 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau3a816292009-07-07 10:55:49 +02001975 if (!http_process_tarpit(s, s->req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001976 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001977 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001978 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001979
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001980 if (ana_list & AN_REQ_SRV_RULES) {
1981 if (!process_server_rules(s, s->req, AN_REQ_SRV_RULES))
1982 break;
1983 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_SRV_RULES);
1984 }
1985
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001986 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreauc465fd72009-08-31 00:17:18 +02001987 if (!http_process_request(s, s->req, AN_REQ_HTTP_INNER))
1988 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001989 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001990 }
1991
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001992 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau5a8f9472014-04-10 11:16:06 +02001993 if (!http_wait_for_request_body(s, s->req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001994 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001995 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001996 }
Emeric Brun647caf12009-06-30 17:57:00 +02001997
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001998 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Emeric Brun647caf12009-06-30 17:57:00 +02001999 if (!tcp_persist_rdp_cookie(s, s->req, AN_REQ_PRST_RDP_COOKIE))
2000 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002001 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02002002 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002003
Emeric Brun1d33b292010-01-04 15:47:17 +01002004 if (ana_list & AN_REQ_STICKING_RULES) {
2005 if (!process_sticking_rules(s, s->req, AN_REQ_STICKING_RULES))
2006 break;
2007 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
2008 }
2009
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002010 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01002011 if (!http_request_forward_body(s, s->req, AN_REQ_HTTP_XFER_BODY))
2012 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002013 UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002014 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01002015 break;
2016 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002017 }
Willy Tarreau84455332009-03-15 22:34:05 +01002018
Willy Tarreau815a9b22010-07-27 17:15:12 +02002019 rq_prod_last = s->si[0].state;
2020 rq_cons_last = s->si[1].state;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002021 s->req->flags &= ~CF_WAKE_ONCE;
Willy Tarreau815a9b22010-07-27 17:15:12 +02002022 rqf_last = s->req->flags;
2023
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002024 if ((s->req->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002025 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002026 }
2027
Willy Tarreau576507f2010-01-07 00:09:04 +01002028 /* we'll monitor the request analysers while parsing the response,
2029 * because some response analysers may indirectly enable new request
2030 * analysers (eg: HTTP keep-alive).
2031 */
2032 req_ana_back = s->req->analysers;
2033
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002034 resync_response:
2035 /* Analyse response */
2036
Willy Tarreaub31c9712012-11-11 23:05:39 +01002037 if (((s->rep->flags & ~rpf_last) & CF_MASK_ANALYSER) ||
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002038 (s->rep->flags ^ rpf_last) & CF_MASK_STATIC ||
2039 s->si[0].state != rp_cons_last ||
2040 s->si[1].state != rp_prod_last) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002041 unsigned int flags = s->rep->flags;
2042
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002043 if ((s->rep->flags & CF_MASK_ANALYSER) &&
Willy Tarreau0499e352010-12-17 07:13:42 +01002044 (s->rep->analysers & AN_REQ_WAIT_HTTP)) {
2045 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
2046 * for some free space in the response buffer, so we might need to call
2047 * it when something changes in the response buffer, but still we pass
2048 * it the request buffer. Note that the SI state might very well still
2049 * be zero due to us returning a flow of redirects!
2050 */
2051 s->rep->analysers &= ~AN_REQ_WAIT_HTTP;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002052 s->req->flags |= CF_WAKE_ONCE;
Willy Tarreau0499e352010-12-17 07:13:42 +01002053 }
2054
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002055 if (s->rep->prod->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01002056 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002057 unsigned int ana_list;
2058 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002059
Willy Tarreau90deb182010-01-07 00:20:41 +01002060 /* it's up to the analysers to stop disable reading or
2061 * closing. Note: if an analyser disables any of these
2062 * bits, it is responsible for enabling them again when
2063 * it disables itself, so that other analysers are called
2064 * in similar conditions.
2065 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002066 channel_auto_read(s->rep);
2067 channel_auto_close(s->rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002068
2069 /* We will call all analysers for which a bit is set in
2070 * s->rep->analysers, following the bit order from LSB
2071 * to MSB. The analysers must remove themselves from
2072 * the list when not needed. Any analyser may return 0
2073 * to break out of the loop, either because of missing
2074 * data to take a decision, or because it decides to
2075 * kill the session. We loop at least once through each
2076 * analyser, and we may loop again if other analysers
2077 * are added in the middle.
2078 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002079
2080 ana_list = ana_back = s->rep->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01002081 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002082 /* Warning! ensure that analysers are always placed in ascending order! */
2083
Emeric Brun97679e72010-09-23 17:56:44 +02002084 if (ana_list & AN_RES_INSPECT) {
2085 if (!tcp_inspect_response(s, s->rep, AN_RES_INSPECT))
2086 break;
2087 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_INSPECT);
2088 }
2089
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002090 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002091 if (!http_wait_for_response(s, s->rep, AN_RES_WAIT_HTTP))
2092 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002093 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002094 }
2095
Emeric Brun1d33b292010-01-04 15:47:17 +01002096 if (ana_list & AN_RES_STORE_RULES) {
2097 if (!process_store_rules(s, s->rep, AN_RES_STORE_RULES))
2098 break;
2099 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
2100 }
2101
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002102 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002103 if (!http_process_res_common(s, s->rep, AN_RES_HTTP_PROCESS_BE, s->be))
2104 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002105 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002106 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002107
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002108 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01002109 if (!http_response_forward_body(s, s->rep, AN_RES_HTTP_XFER_BODY))
2110 break;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002111 UPDATE_ANALYSERS(s->rep->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002112 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01002113 break;
2114 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002115 }
2116
Willy Tarreau815a9b22010-07-27 17:15:12 +02002117 rp_cons_last = s->si[0].state;
2118 rp_prod_last = s->si[1].state;
2119 rpf_last = s->rep->flags;
2120
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002121 if ((s->rep->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002122 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002123 }
2124
Willy Tarreau576507f2010-01-07 00:09:04 +01002125 /* maybe someone has added some request analysers, so we must check and loop */
2126 if (s->req->analysers & ~req_ana_back)
2127 goto resync_request;
2128
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002129 if ((s->req->flags & ~rqf_last) & CF_MASK_ANALYSER)
Willy Tarreau0499e352010-12-17 07:13:42 +01002130 goto resync_request;
2131
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002132 /* FIXME: here we should call protocol handlers which rely on
2133 * both buffers.
2134 */
2135
2136
2137 /*
Willy Tarreauae526782010-03-04 20:34:23 +01002138 * Now we propagate unhandled errors to the session. Normally
2139 * we're just in a data phase here since it means we have not
2140 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002141 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002142 srv = objt_server(s->target);
Willy Tarreau2f976e12010-11-11 14:28:47 +01002143 if (unlikely(!(s->flags & SN_ERR_MASK))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002144 if (s->req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002145 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau84455332009-03-15 22:34:05 +01002146 s->req->analysers = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002147 if (s->req->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002148 s->be->be_counters.cli_aborts++;
2149 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002150 if (srv)
2151 srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01002152 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01002153 }
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002154 else if (s->req->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002155 s->be->be_counters.cli_aborts++;
2156 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002157 if (srv)
2158 srv->counters.cli_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01002159 s->flags |= SN_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01002160 }
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002161 else if (s->req->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002162 s->be->be_counters.srv_aborts++;
2163 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002164 if (srv)
2165 srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01002166 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01002167 }
2168 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002169 s->be->be_counters.srv_aborts++;
2170 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002171 if (srv)
2172 srv->counters.srv_aborts++;
Willy Tarreau84455332009-03-15 22:34:05 +01002173 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01002174 }
Willy Tarreau84455332009-03-15 22:34:05 +01002175 sess_set_term_flags(s);
2176 }
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002177 else if (s->rep->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002178 /* Report it if the server got an error or a read timeout expired */
2179 s->rep->analysers = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002180 if (s->rep->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002181 s->be->be_counters.srv_aborts++;
2182 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002183 if (srv)
2184 srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002185 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01002186 }
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002187 else if (s->rep->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002188 s->be->be_counters.srv_aborts++;
2189 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002190 if (srv)
2191 srv->counters.srv_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002192 s->flags |= SN_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01002193 }
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002194 else if (s->rep->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002195 s->be->be_counters.cli_aborts++;
2196 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002197 if (srv)
2198 srv->counters.cli_aborts++;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002199 s->flags |= SN_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01002200 }
2201 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002202 s->be->be_counters.cli_aborts++;
2203 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002204 if (srv)
2205 srv->counters.cli_aborts++;
Willy Tarreauae526782010-03-04 20:34:23 +01002206 s->flags |= SN_ERR_CLITO;
2207 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002208 sess_set_term_flags(s);
2209 }
Willy Tarreau84455332009-03-15 22:34:05 +01002210 }
2211
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002212 /*
2213 * Here we take care of forwarding unhandled data. This also includes
2214 * connection establishments and shutdown requests.
2215 */
2216
2217
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002218 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002219 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002220 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002221 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002222 */
Willy Tarreau2b028dd2013-12-31 23:56:46 +01002223 if (unlikely(!s->req->analysers &&
Willy Tarreaub31c9712012-11-11 23:05:39 +01002224 !(s->req->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02002225 (s->req->prod->state >= SI_ST_EST) &&
Willy Tarreau2b028dd2013-12-31 23:56:46 +01002226 (s->req->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002227 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002228 * attached to it. If any data are left in, we'll permit them to
2229 * move.
2230 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002231 channel_auto_read(s->req);
2232 channel_auto_connect(s->req);
2233 channel_auto_close(s->req);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002234 buffer_flush(s->req->buf);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01002235
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002236 /* We'll let data flow between the producer (if still connected)
2237 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002238 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002239 if (!(s->req->flags & (CF_SHUTR|CF_SHUTW_NOW)))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002240 channel_forward(s->req, CHN_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002241 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002242
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002243 /* check if it is wise to enable kernel splicing to forward request data */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002244 if (!(s->req->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002245 s->req->to_forward &&
2246 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002247 (objt_conn(s->si[0].end) && __objt_conn(s->si[0].end)->xprt && __objt_conn(s->si[0].end)->xprt->rcv_pipe) &&
2248 (objt_conn(s->si[1].end) && __objt_conn(s->si[1].end)->xprt && __objt_conn(s->si[1].end)->xprt->snd_pipe) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002249 (pipes_used < global.maxpipes) &&
2250 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
2251 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002252 (s->req->flags & CF_STREAMER_FAST)))) {
2253 s->req->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002254 }
2255
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002256 /* reflect what the L7 analysers have seen last */
2257 rqf_last = s->req->flags;
2258
2259 /*
2260 * Now forward all shutdown requests between both sides of the buffer
2261 */
2262
Willy Tarreau520d95e2009-09-19 21:04:57 +02002263 /* first, let's check if the request buffer needs to shutdown(write), which may
2264 * happen either because the input is closed or because we want to force a close
Willy Tarreau05cdd962014-05-10 14:30:07 +02002265 * once the server has begun to respond. If a half-closed timeout is set, we adjust
2266 * the other side's timeout as well.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002267 */
Willy Tarreaub31c9712012-11-11 23:05:39 +01002268 if (unlikely((s->req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002269 (CF_AUTO_CLOSE|CF_SHUTR))) {
2270 channel_shutw_now(s->req);
2271 if (tick_isset(s->fe->timeout.clientfin)) {
2272 s->rep->wto = s->fe->timeout.clientfin;
2273 s->rep->wex = tick_add(now_ms, s->rep->wto);
2274 }
2275 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002276
2277 /* shutdown(write) pending */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002278 if (unlikely((s->req->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
Willy Tarreau8615c2a2013-06-21 08:20:19 +02002279 channel_is_empty(s->req))) {
2280 if (s->req->flags & CF_READ_ERROR)
2281 s->req->cons->flags |= SI_FL_NOLINGER;
Willy Tarreau73b013b2012-05-21 16:31:45 +02002282 si_shutw(s->req->cons);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002283 if (tick_isset(s->be->timeout.serverfin)) {
2284 s->rep->rto = s->be->timeout.serverfin;
2285 s->rep->rex = tick_add(now_ms, s->rep->rto);
2286 }
Willy Tarreau8615c2a2013-06-21 08:20:19 +02002287 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002288
2289 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002290 if (unlikely((s->req->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW &&
Willy Tarreau3dbc6942008-12-07 13:05:04 +01002291 !s->req->analysers))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002292 channel_shutr_now(s->req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002293
2294 /* shutdown(read) pending */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002295 if (unlikely((s->req->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002296 if (s->req->prod->flags & SI_FL_NOHALF)
2297 s->req->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau73b013b2012-05-21 16:31:45 +02002298 si_shutr(s->req->prod);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002299 if (tick_isset(s->fe->timeout.clientfin)) {
2300 s->rep->wto = s->fe->timeout.clientfin;
2301 s->rep->wex = tick_add(now_ms, s->rep->wto);
2302 }
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002303 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002304
Willy Tarreau520d95e2009-09-19 21:04:57 +02002305 /* it's possible that an upper layer has requested a connection setup or abort.
2306 * There are 2 situations where we decide to establish a new connection :
2307 * - there are data scheduled for emission in the buffer
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002308 * - the CF_AUTO_CONNECT flag is set (active connection)
Willy Tarreau520d95e2009-09-19 21:04:57 +02002309 */
2310 if (s->req->cons->state == SI_ST_INI) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002311 if (!(s->req->flags & CF_SHUTW)) {
2312 if ((s->req->flags & CF_AUTO_CONNECT) || !channel_is_empty(s->req)) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002313 /* If we have an appctx, there is no connect method, so we
2314 * immediately switch to the connected state, otherwise we
2315 * perform a connection request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002316 */
Willy Tarreau85e7d002010-05-31 11:57:51 +02002317 s->req->cons->state = SI_ST_REQ; /* new connection requested */
Willy Tarreau070ceb62010-06-01 10:36:43 +02002318 s->req->cons->conn_retries = s->be->conn_retries;
Willy Tarreau520d95e2009-09-19 21:04:57 +02002319 }
Willy Tarreau73201222009-08-16 18:27:24 +02002320 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002321 else {
Willy Tarreau92795622009-03-06 12:51:23 +01002322 s->req->cons->state = SI_ST_CLO; /* shutw+ini = abort */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002323 channel_shutw_now(s->req); /* fix buffer flags upon abort */
2324 channel_shutr_now(s->rep);
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002325 }
Willy Tarreau92795622009-03-06 12:51:23 +01002326 }
2327
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002328
2329 /* we may have a pending connection request, or a connection waiting
2330 * for completion.
2331 */
2332 if (s->si[1].state >= SI_ST_REQ && s->si[1].state < SI_ST_CON) {
2333 do {
2334 /* nb: step 1 might switch from QUE to ASS, but we first want
2335 * to give a chance to step 2 to perform a redirect if needed.
2336 */
2337 if (s->si[1].state != SI_ST_REQ)
2338 sess_update_stream_int(s, &s->si[1]);
Willy Tarreaub44c8732013-12-31 23:16:50 +01002339 if (s->si[1].state == SI_ST_REQ)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002340 sess_prepare_conn_req(s, &s->si[1]);
2341
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01002342 /* applets directly go to the ESTABLISHED state. Similarly,
2343 * servers experience the same fate when their connection
2344 * is reused.
2345 */
Willy Tarreaub44c8732013-12-31 23:16:50 +01002346 if (unlikely(s->si[1].state == SI_ST_EST))
2347 sess_establish(s, &s->si[1]);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01002348
Willy Tarreaub44c8732013-12-31 23:16:50 +01002349 /* Now we can add the server name to a header (if requested) */
2350 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
2351 if ((s->si[1].state >= SI_ST_CON) &&
2352 (s->be->server_id_hdr_name != NULL) &&
2353 (s->be->mode == PR_MODE_HTTP) &&
2354 objt_server(s->target)) {
2355 http_send_name_header(&s->txn, s->be, objt_server(s->target)->id);
Willy Tarreau1e5dfda2013-04-07 18:19:16 +02002356 }
2357
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002358 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01002359 if (s->si[1].state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SN_REDIRECTABLE))
Willy Tarreau71241ab2012-12-27 11:30:54 +01002360 http_perform_server_redirect(s, &s->si[1]);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002361 } while (s->si[1].state == SI_ST_ASS);
2362 }
2363
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002364 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002365 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002366 goto resync_stream_interface;
2367
Willy Tarreau815a9b22010-07-27 17:15:12 +02002368 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002369 if ((s->req->flags ^ rqf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002370 goto resync_request;
2371
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002372 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01002373
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002374 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002375 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002376 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002377 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002378 */
Willy Tarreau2b028dd2013-12-31 23:56:46 +01002379 if (unlikely(!s->rep->analysers &&
Willy Tarreaub31c9712012-11-11 23:05:39 +01002380 !(s->rep->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
Willy Tarreau31971e52009-09-20 12:07:52 +02002381 (s->rep->prod->state >= SI_ST_EST) &&
Willy Tarreau2b028dd2013-12-31 23:56:46 +01002382 (s->rep->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002383 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002384 * attached to it. If any data are left in, we'll permit them to
2385 * move.
2386 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002387 channel_auto_read(s->rep);
2388 channel_auto_close(s->rep);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002389 buffer_flush(s->rep->buf);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002390
2391 /* We'll let data flow between the producer (if still connected)
2392 * to the consumer.
2393 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002394 if (!(s->rep->flags & (CF_SHUTR|CF_SHUTW_NOW)))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002395 channel_forward(s->rep, CHN_INFINITE_FORWARD);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002396
2397 /* if we have no analyser anymore in any direction and have a
Willy Tarreau05cdd962014-05-10 14:30:07 +02002398 * tunnel timeout set, use it now. Note that we must respect
2399 * the half-closed timeouts as well.
Willy Tarreauce887fd2012-05-12 12:50:00 +02002400 */
2401 if (!s->req->analysers && s->be->timeout.tunnel) {
2402 s->req->rto = s->req->wto = s->rep->rto = s->rep->wto =
2403 s->be->timeout.tunnel;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002404
2405 if ((s->req->flags & CF_SHUTR) && tick_isset(s->fe->timeout.clientfin))
2406 s->rep->wto = s->fe->timeout.clientfin;
2407 if ((s->req->flags & CF_SHUTW) && tick_isset(s->be->timeout.serverfin))
2408 s->rep->rto = s->be->timeout.serverfin;
2409 if ((s->rep->flags & CF_SHUTR) && tick_isset(s->be->timeout.serverfin))
2410 s->req->wto = s->be->timeout.serverfin;
2411 if ((s->rep->flags & CF_SHUTW) && tick_isset(s->fe->timeout.clientfin))
2412 s->req->rto = s->fe->timeout.clientfin;
2413
2414 s->req->rex = tick_add(now_ms, s->req->rto);
2415 s->req->wex = tick_add(now_ms, s->req->wto);
2416 s->rep->rex = tick_add(now_ms, s->rep->rto);
2417 s->rep->wex = tick_add(now_ms, s->rep->wto);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002418 }
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002419 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002420
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002421 /* check if it is wise to enable kernel splicing to forward response data */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002422 if (!(s->rep->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002423 s->rep->to_forward &&
2424 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002425 (objt_conn(s->si[0].end) && __objt_conn(s->si[0].end)->xprt && __objt_conn(s->si[0].end)->xprt->snd_pipe) &&
2426 (objt_conn(s->si[1].end) && __objt_conn(s->si[1].end)->xprt && __objt_conn(s->si[1].end)->xprt->rcv_pipe) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002427 (pipes_used < global.maxpipes) &&
2428 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2429 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002430 (s->rep->flags & CF_STREAMER_FAST)))) {
2431 s->rep->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002432 }
2433
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002434 /* reflect what the L7 analysers have seen last */
2435 rpf_last = s->rep->flags;
2436
2437 /*
2438 * Now forward all shutdown requests between both sides of the buffer
2439 */
2440
2441 /*
2442 * FIXME: this is probably where we should produce error responses.
2443 */
2444
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002445 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreaub31c9712012-11-11 23:05:39 +01002446 if (unlikely((s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002447 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002448 channel_shutw_now(s->rep);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002449 if (tick_isset(s->be->timeout.serverfin)) {
2450 s->req->wto = s->be->timeout.serverfin;
2451 s->req->wex = tick_add(now_ms, s->req->wto);
2452 }
2453 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002454
2455 /* shutdown(write) pending */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002456 if (unlikely((s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
Willy Tarreau05cdd962014-05-10 14:30:07 +02002457 channel_is_empty(s->rep))) {
Willy Tarreau73b013b2012-05-21 16:31:45 +02002458 si_shutw(s->rep->cons);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002459 if (tick_isset(s->fe->timeout.clientfin)) {
2460 s->req->rto = s->fe->timeout.clientfin;
2461 s->req->rex = tick_add(now_ms, s->req->rto);
2462 }
2463 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002464
2465 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002466 if (unlikely((s->rep->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW) &&
Willy Tarreau3dbc6942008-12-07 13:05:04 +01002467 !s->rep->analysers)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002468 channel_shutr_now(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002469
2470 /* shutdown(read) pending */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002471 if (unlikely((s->rep->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002472 if (s->rep->prod->flags & SI_FL_NOHALF)
2473 s->rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau73b013b2012-05-21 16:31:45 +02002474 si_shutr(s->rep->prod);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002475 if (tick_isset(s->be->timeout.serverfin)) {
2476 s->req->wto = s->be->timeout.serverfin;
2477 s->req->wex = tick_add(now_ms, s->req->wto);
2478 }
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002479 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002480
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002481 if (s->req->prod->state == SI_ST_DIS || s->req->cons->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002482 goto resync_stream_interface;
2483
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002484 if (s->req->flags != rqf_last)
2485 goto resync_request;
2486
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002487 if ((s->rep->flags ^ rpf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002488 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002489
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002490 /* we're interested in getting wakeups again */
2491 s->req->prod->flags &= ~SI_FL_DONT_WAKE;
2492 s->req->cons->flags &= ~SI_FL_DONT_WAKE;
2493
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002494 /* This is needed only when debugging is enabled, to indicate
2495 * client-side or server-side close. Please note that in the unlikely
2496 * event where both sides would close at once, the sequence is reported
2497 * on the server side first.
2498 */
2499 if (unlikely((global.mode & MODE_DEBUG) &&
2500 (!(global.mode & MODE_QUIET) ||
2501 (global.mode & MODE_VERBOSE)))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002502 if (s->si[1].state == SI_ST_CLO &&
2503 s->si[1].prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002504 chunk_printf(&trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002505 s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002506 objt_conn(s->si[0].end) ? (unsigned short)objt_conn(s->si[0].end)->t.sock.fd : -1,
2507 objt_conn(s->si[1].end) ? (unsigned short)objt_conn(s->si[1].end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002508 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002509 }
2510
2511 if (s->si[0].state == SI_ST_CLO &&
2512 s->si[0].prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002513 chunk_printf(&trash, "%08x:%s.clicls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002514 s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002515 objt_conn(s->si[0].end) ? (unsigned short)objt_conn(s->si[0].end)->t.sock.fd : -1,
2516 objt_conn(s->si[1].end) ? (unsigned short)objt_conn(s->si[1].end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002517 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002518 }
2519 }
2520
2521 if (likely((s->rep->cons->state != SI_ST_CLO) ||
2522 (s->req->cons->state > SI_ST_INI && s->req->cons->state < SI_ST_CLO))) {
2523
2524 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
2525 session_process_counters(s);
2526
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002527 if (s->rep->cons->state == SI_ST_EST && obj_type(s->rep->cons->end) != OBJ_TYPE_APPCTX)
Willy Tarreau73b013b2012-05-21 16:31:45 +02002528 si_update(s->rep->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002529
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002530 if (s->req->cons->state == SI_ST_EST && obj_type(s->req->cons->end) != OBJ_TYPE_APPCTX)
Willy Tarreau73b013b2012-05-21 16:31:45 +02002531 si_update(s->req->cons);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002532
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002533 s->req->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2534 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 +01002535 s->si[0].prev_state = s->si[0].state;
2536 s->si[1].prev_state = s->si[1].state;
Willy Tarreaub0ef7352008-12-14 13:26:20 +01002537 s->si[0].flags &= ~(SI_FL_ERR|SI_FL_EXP);
2538 s->si[1].flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002539
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002540 /* Trick: if a request is being waiting for the server to respond,
2541 * and if we know the server can timeout, we don't want the timeout
2542 * to expire on the client side first, but we're still interested
2543 * in passing data from the client to the server (eg: POST). Thus,
2544 * we can cancel the client's request timeout if the server's
2545 * request timeout is set and the server has not yet sent a response.
2546 */
2547
2548 if ((s->rep->flags & (CF_AUTO_CLOSE|CF_SHUTR)) == 0 &&
2549 (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) {
2550 s->req->flags |= CF_READ_NOEXP;
2551 s->req->rex = TICK_ETERNITY;
2552 }
2553
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002554 /* When any of the stream interfaces is attached to an applet,
2555 * we have to call it here. Note that this one may wake the
2556 * task up again. If at least one applet was called, the current
2557 * task might have been woken up, in which case we don't want it
2558 * to be requeued to the wait queue but rather to the run queue
2559 * to run ASAP. The bitwise "or" in the condition ensures that
2560 * both functions are always called and that we wake up if at
2561 * least one did something.
Willy Tarreau1accfc02009-09-05 20:57:35 +02002562 */
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002563 if ((si_applet_call(s->req->cons) | si_applet_call(s->rep->cons)) != 0) {
Willy Tarreau1accfc02009-09-05 20:57:35 +02002564 if (task_in_rq(t)) {
Willy Tarreau1accfc02009-09-05 20:57:35 +02002565 t->expire = TICK_ETERNITY;
2566 return t;
2567 }
2568 }
2569
Willy Tarreau798f4322012-11-08 14:49:17 +01002570 update_exp_and_leave:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002571 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
2572 tick_first(s->rep->rex, s->rep->wex));
2573 if (s->req->analysers)
2574 t->expire = tick_first(t->expire, s->req->analyse_exp);
2575
2576 if (s->si[0].exp)
2577 t->expire = tick_first(t->expire, s->si[0].exp);
2578
2579 if (s->si[1].exp)
2580 t->expire = tick_first(t->expire, s->si[1].exp);
2581
2582#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002583 fprintf(stderr,
2584 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2585 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
2586 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp,
2587 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 +01002588#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002589
2590#ifdef DEBUG_DEV
2591 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002592 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002593 ABORT_NOW();
2594#endif
Willy Tarreau26c25062009-03-08 09:38:41 +01002595 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002596 }
2597
2598 s->fe->feconn--;
2599 if (s->flags & SN_BE_ASSIGNED)
2600 s->be->beconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002601 jobs--;
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002602 if (s->listener) {
2603 if (!(s->listener->options & LI_O_UNLIMITED))
2604 actconn--;
2605 s->listener->nbconn--;
2606 if (s->listener->state == LI_FULL)
2607 resume_listener(s->listener);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002608
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002609 /* Dequeues all of the listeners waiting for a resource */
2610 if (!LIST_ISEMPTY(&global_listener_queue))
2611 dequeue_all_listeners(&global_listener_queue);
Willy Tarreau08ceb102011-07-24 22:58:00 +02002612
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002613 if (!LIST_ISEMPTY(&s->fe->listener_queue) &&
2614 (!s->fe->fe_sps_lim || freq_ctr_remain(&s->fe->fe_sess_per_sec, s->fe->fe_sps_lim, 0) > 0))
2615 dequeue_all_listeners(&s->fe->listener_queue);
2616 }
Willy Tarreau07687c12011-07-24 23:55:06 +02002617
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002618 if (unlikely((global.mode & MODE_DEBUG) &&
2619 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002620 chunk_printf(&trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002621 s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002622 objt_conn(s->si[0].end) ? (unsigned short)objt_conn(s->si[0].end)->t.sock.fd : -1,
2623 objt_conn(s->si[1].end) ? (unsigned short)objt_conn(s->si[1].end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002624 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002625 }
2626
2627 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
2628 session_process_counters(s);
2629
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002630 if (s->txn.status) {
2631 int n;
2632
2633 n = s->txn.status / 100;
2634 if (n < 1 || n > 5)
2635 n = 0;
2636
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002637 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002638 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01002639 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002640 s->fe->fe_counters.p.http.comp_rsp++;
2641 }
Willy Tarreau24657792010-02-26 10:30:28 +01002642 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002643 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002644 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002645 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01002646 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002647 s->be->be_counters.p.http.comp_rsp++;
2648 }
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002649 }
2650
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002651 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002652 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002653 !(s->flags & SN_MONITOR) &&
2654 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002655 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002656 }
2657
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002658 /* update time stats for this session */
2659 session_update_time_stats(s);
2660
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002661 /* the task MUST not be in the run queue anymore */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002662 session_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002663 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002664 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002665 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002666}
2667
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002668/* Update the session's backend and server time stats */
2669void session_update_time_stats(struct session *s)
2670{
2671 int t_request;
2672 int t_queue;
2673 int t_connect;
2674 int t_data;
2675 int t_close;
2676 struct server *srv;
2677
2678 t_request = 0;
2679 t_queue = s->logs.t_queue;
2680 t_connect = s->logs.t_connect;
2681 t_close = s->logs.t_close;
2682 t_data = s->logs.t_data;
2683
2684 if (s->be->mode != PR_MODE_HTTP)
2685 t_data = t_connect;
2686
2687 if (t_connect < 0 || t_data < 0)
2688 return;
2689
2690 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
2691 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
2692
2693 t_data -= t_connect;
2694 t_connect -= t_queue;
2695 t_queue -= t_request;
2696
2697 srv = objt_server(s->target);
2698 if (srv) {
2699 swrate_add(&srv->counters.q_time, TIME_STATS_SAMPLES, t_queue);
2700 swrate_add(&srv->counters.c_time, TIME_STATS_SAMPLES, t_connect);
2701 swrate_add(&srv->counters.d_time, TIME_STATS_SAMPLES, t_data);
2702 swrate_add(&srv->counters.t_time, TIME_STATS_SAMPLES, t_close);
2703 }
2704 swrate_add(&s->be->be_counters.q_time, TIME_STATS_SAMPLES, t_queue);
2705 swrate_add(&s->be->be_counters.c_time, TIME_STATS_SAMPLES, t_connect);
2706 swrate_add(&s->be->be_counters.d_time, TIME_STATS_SAMPLES, t_data);
2707 swrate_add(&s->be->be_counters.t_time, TIME_STATS_SAMPLES, t_close);
2708}
2709
Willy Tarreau7c669d72008-06-20 15:04:11 +02002710/*
2711 * This function adjusts sess->srv_conn and maintains the previous and new
2712 * server's served session counts. Setting newsrv to NULL is enough to release
2713 * current connection slot. This function also notifies any LB algo which might
2714 * expect to be informed about any change in the number of active sessions on a
2715 * server.
2716 */
2717void sess_change_server(struct session *sess, struct server *newsrv)
2718{
2719 if (sess->srv_conn == newsrv)
2720 return;
2721
2722 if (sess->srv_conn) {
2723 sess->srv_conn->served--;
2724 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2725 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Simon Hormanaf514952011-06-21 14:34:57 +09002726 session_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002727 }
2728
2729 if (newsrv) {
2730 newsrv->served++;
2731 if (newsrv->proxy->lbprm.server_take_conn)
2732 newsrv->proxy->lbprm.server_take_conn(newsrv);
Simon Hormanaf514952011-06-21 14:34:57 +09002733 session_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002734 }
2735}
2736
Willy Tarreau84455332009-03-15 22:34:05 +01002737/* Handle server-side errors for default protocols. It is called whenever a a
2738 * connection setup is aborted or a request is aborted in queue. It sets the
2739 * session termination flags so that the caller does not have to worry about
2740 * them. It's installed as ->srv_error for the server-side stream_interface.
2741 */
2742void default_srv_error(struct session *s, struct stream_interface *si)
2743{
2744 int err_type = si->err_type;
2745 int err = 0, fin = 0;
2746
2747 if (err_type & SI_ET_QUEUE_ABRT) {
2748 err = SN_ERR_CLICL;
2749 fin = SN_FINST_Q;
2750 }
2751 else if (err_type & SI_ET_CONN_ABRT) {
2752 err = SN_ERR_CLICL;
2753 fin = SN_FINST_C;
2754 }
2755 else if (err_type & SI_ET_QUEUE_TO) {
2756 err = SN_ERR_SRVTO;
2757 fin = SN_FINST_Q;
2758 }
2759 else if (err_type & SI_ET_QUEUE_ERR) {
2760 err = SN_ERR_SRVCL;
2761 fin = SN_FINST_Q;
2762 }
2763 else if (err_type & SI_ET_CONN_TO) {
2764 err = SN_ERR_SRVTO;
2765 fin = SN_FINST_C;
2766 }
2767 else if (err_type & SI_ET_CONN_ERR) {
2768 err = SN_ERR_SRVCL;
2769 fin = SN_FINST_C;
2770 }
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002771 else if (err_type & SI_ET_CONN_RES) {
2772 err = SN_ERR_RESOURCE;
2773 fin = SN_FINST_C;
2774 }
Willy Tarreau84455332009-03-15 22:34:05 +01002775 else /* SI_ET_CONN_OTHER and others */ {
2776 err = SN_ERR_INTERNAL;
2777 fin = SN_FINST_C;
2778 }
2779
2780 if (!(s->flags & SN_ERR_MASK))
2781 s->flags |= err;
2782 if (!(s->flags & SN_FINST_MASK))
2783 s->flags |= fin;
2784}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002785
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002786/* kill a session and set the termination flags to <why> (one of SN_ERR_*) */
2787void session_shutdown(struct session *session, int why)
2788{
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002789 if (session->req->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002790 return;
2791
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002792 channel_shutw_now(session->req);
2793 channel_shutr_now(session->rep);
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002794 session->task->nice = 1024;
2795 if (!(session->flags & SN_ERR_MASK))
2796 session->flags |= why;
2797 task_wakeup(session->task, TASK_WOKEN_OTHER);
2798}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002799
Willy Tarreau8b22a712010-06-18 17:46:06 +02002800/************************************************************************/
2801/* All supported ACL keywords must be declared here. */
2802/************************************************************************/
2803
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002804/* Returns a pointer to a stkctr depending on the fetch keyword name.
2805 * It is designed to be called as sc[0-9]_* sc_* or src_* exclusively.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002806 * sc[0-9]_* will return a pointer to the respective field in the
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002807 * session <l4>. sc_* requires an UINT argument specifying the stick
2808 * counter number. src_* will fill a locally allocated structure with
Willy Tarreaua65536c2013-07-22 22:40:11 +02002809 * the table and entry corresponding to what is specified with src_*.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002810 * NULL may be returned if the designated stkctr is not tracked. For
2811 * the sc_* and sc[0-9]_* forms, an optional table argument may be
2812 * passed. When present, the currently tracked key is then looked up
2813 * in the specified table instead of the current table. The purpose is
2814 * to be able to convery multiple values per key (eg: have gpc0 from
2815 * multiple tables).
Willy Tarreaua65536c2013-07-22 22:40:11 +02002816 */
Willy Tarreaue12704b2014-07-15 19:06:18 +02002817struct stkctr *
Willy Tarreaua65536c2013-07-22 22:40:11 +02002818smp_fetch_sc_stkctr(struct session *l4, const struct arg *args, const char *kw)
2819{
2820 static struct stkctr stkctr;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002821 struct stksess *stksess;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002822 unsigned int num = kw[2] - '0';
Willy Tarreau0f791d42013-07-23 19:56:43 +02002823 int arg = 0;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002824
Willy Tarreau0f791d42013-07-23 19:56:43 +02002825 if (num == '_' - '0') {
2826 /* sc_* variant, args[0] = ctr# (mandatory) */
2827 num = args[arg++].data.uint;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002828 if (num >= MAX_SESS_STKCTR)
2829 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002830 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002831 else if (num > 9) { /* src_* variant, args[0] = table */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002832 struct stktable_key *key;
2833 struct connection *conn = objt_conn(l4->si[0].end);
2834
2835 if (!conn)
2836 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002837
Thierry FOURNIER74c219d2014-04-14 14:35:40 +02002838 key = addr_to_stktable_key(&conn->addr.from, args->data.prx->table.type);
Willy Tarreaua65536c2013-07-22 22:40:11 +02002839 if (!key)
2840 return NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002841
Willy Tarreaua65536c2013-07-22 22:40:11 +02002842 stkctr.table = &args->data.prx->table;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002843 stkctr_set_entry(&stkctr, stktable_lookup_key(stkctr.table, key));
Willy Tarreaua65536c2013-07-22 22:40:11 +02002844 return &stkctr;
2845 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002846
2847 /* Here, <num> contains the counter number from 0 to 9 for
2848 * the sc[0-9]_ form, or even higher using sc_(num) if needed.
2849 * args[arg] is the first optional argument.
2850 */
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002851 stksess = stkctr_entry(&l4->stkctr[num]);
2852 if (!stksess)
2853 return NULL;
2854
Willy Tarreau0f791d42013-07-23 19:56:43 +02002855 if (unlikely(args[arg].type == ARGT_TAB)) {
2856 /* an alternate table was specified, let's look up the same key there */
2857 stkctr.table = &args[arg].data.prx->table;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002858 stkctr_set_entry(&stkctr, stktable_lookup(stkctr.table, stksess));
Willy Tarreau0f791d42013-07-23 19:56:43 +02002859 return &stkctr;
2860 }
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002861 return &l4->stkctr[num];
Willy Tarreaua65536c2013-07-22 22:40:11 +02002862}
2863
Willy Tarreau88821242013-07-22 18:29:29 +02002864/* set return a boolean indicating if the requested session counter is
2865 * currently being tracked or not.
2866 * Supports being called as "sc[0-9]_tracked" only.
2867 */
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002868static int
Willy Tarreau88821242013-07-22 18:29:29 +02002869smp_fetch_sc_tracked(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02002870 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002871{
2872 smp->flags = SMP_F_VOL_TEST;
2873 smp->type = SMP_T_BOOL;
Thierry FOURNIERd988f212014-04-15 01:15:52 +02002874 smp->data.uint = !!smp_fetch_sc_stkctr(l4, args, kw);
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002875 return 1;
2876}
2877
Willy Tarreau30b20462013-07-22 19:46:52 +02002878/* set <smp> to the General Purpose Counter 0 value from the session's tracked
2879 * frontend counters or from the src.
2880 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpc0" only. Value
2881 * zero is returned if the key is new.
2882 */
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002883static int
Willy Tarreau30b20462013-07-22 19:46:52 +02002884smp_fetch_sc_get_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
2885 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002886{
Willy Tarreau30b20462013-07-22 19:46:52 +02002887 struct stkctr *stkctr = smp_fetch_sc_stkctr(l4, args, kw);
2888
2889 if (!stkctr)
2890 return 0;
2891
Willy Tarreau37406352012-04-23 16:16:37 +02002892 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002893 smp->type = SMP_T_UINT;
2894 smp->data.uint = 0;
Willy Tarreau30b20462013-07-22 19:46:52 +02002895
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002896 if (stkctr_entry(stkctr) != NULL) {
2897 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002898 if (!ptr)
2899 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002900 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002901 }
2902 return 1;
2903}
2904
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002905/* set <smp> to the General Purpose Counter 0's event rate from the session's
2906 * tracked frontend counters or from the src.
2907 * Supports being called as "sc[0-9]_gpc0_rate" or "src_gpc0_rate" only.
2908 * Value zero is returned if the key is new.
2909 */
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002910static int
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002911smp_fetch_sc_gpc0_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
2912 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002913{
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002914 struct stkctr *stkctr = smp_fetch_sc_stkctr(l4, args, kw);
2915
2916 if (!stkctr)
2917 return 0;
2918
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002919 smp->flags = SMP_F_VOL_TEST;
2920 smp->type = SMP_T_UINT;
2921 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002922 if (stkctr_entry(stkctr) != NULL) {
2923 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002924 if (!ptr)
2925 return 0; /* parameter not stored */
2926 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002927 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002928 }
2929 return 1;
2930}
2931
Willy Tarreau710d38c2013-07-23 00:07:04 +02002932/* Increment the General Purpose Counter 0 value from the session's tracked
2933 * frontend counters and return it into temp integer.
2934 * Supports being called as "sc[0-9]_inc_gpc0" or "src_inc_gpc0" only.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002935 */
2936static int
Willy Tarreau710d38c2013-07-23 00:07:04 +02002937smp_fetch_sc_inc_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
2938 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002939{
Willy Tarreau710d38c2013-07-23 00:07:04 +02002940 struct stkctr *stkctr = smp_fetch_sc_stkctr(l4, args, kw);
2941
2942 if (!stkctr)
2943 return 0;
2944
Willy Tarreau37406352012-04-23 16:16:37 +02002945 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002946 smp->type = SMP_T_UINT;
2947 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002948 if (stkctr_entry(stkctr) != NULL) {
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002949 void *ptr;
2950
2951 /* First, update gpc0_rate if it's tracked. Second, update its
2952 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
2953 */
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002954 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002955 if (ptr) {
2956 update_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Willy Tarreau710d38c2013-07-23 00:07:04 +02002957 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002958 smp->data.uint = (&stktable_data_cast(ptr, gpc0_rate))->curr_ctr;
2959 }
2960
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002961 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002962 if (ptr)
2963 smp->data.uint = ++stktable_data_cast(ptr, gpc0);
2964
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002965 }
2966 return 1;
2967}
2968
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002969/* Clear the General Purpose Counter 0 value from the session's tracked
2970 * frontend counters and return its previous value into temp integer.
2971 * Supports being called as "sc[0-9]_clr_gpc0" or "src_clr_gpc0" only.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002972 */
2973static int
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002974smp_fetch_sc_clr_gpc0(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
2975 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002976{
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002977 struct stkctr *stkctr = smp_fetch_sc_stkctr(l4, args, kw);
2978
2979 if (!stkctr)
2980 return 0;
2981
Willy Tarreau37406352012-04-23 16:16:37 +02002982 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002983 smp->type = SMP_T_UINT;
2984 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002985 if (stkctr_entry(stkctr) != NULL) {
2986 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002987 if (!ptr)
2988 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002989 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002990 stktable_data_cast(ptr, gpc0) = 0;
2991 }
2992 return 1;
2993}
2994
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002995/* set <smp> to the cumulated number of connections from the session's tracked
2996 * frontend counters. Supports being called as "sc[0-9]_conn_cnt" or
2997 * "src_conn_cnt" only.
2998 */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002999static int
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02003000smp_fetch_sc_conn_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
3001 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02003002{
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02003003 struct stkctr *stkctr = smp_fetch_sc_stkctr(l4, args, kw);
3004
3005 if (!stkctr)
3006 return 0;
3007
Willy Tarreau37406352012-04-23 16:16:37 +02003008 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003009 smp->type = SMP_T_UINT;
3010 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003011 if (stkctr_entry(stkctr) != NULL) {
3012 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CNT);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02003013 if (!ptr)
3014 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003015 smp->data.uint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02003016 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02003017 return 1;
3018}
3019
Willy Tarreauc8c65702013-07-23 15:09:35 +02003020/* set <smp> to the connection rate from the session's tracked frontend
3021 * counters. Supports being called as "sc[0-9]_conn_rate" or "src_conn_rate"
3022 * only.
3023 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02003024static int
Willy Tarreauc8c65702013-07-23 15:09:35 +02003025smp_fetch_sc_conn_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
3026 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau91c43d72010-06-20 11:19:22 +02003027{
Willy Tarreauc8c65702013-07-23 15:09:35 +02003028 struct stkctr *stkctr = smp_fetch_sc_stkctr(l4, args, kw);
3029
3030 if (!stkctr)
3031 return 0;
3032
Willy Tarreau37406352012-04-23 16:16:37 +02003033 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003034 smp->type = SMP_T_UINT;
3035 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003036 if (stkctr_entry(stkctr) != NULL) {
3037 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003038 if (!ptr)
3039 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003040 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreauc8c65702013-07-23 15:09:35 +02003041 stkctr->table->data_arg[STKTABLE_DT_CONN_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003042 }
3043 return 1;
3044}
3045
Willy Tarreaua5e37562011-12-16 17:06:15 +01003046/* set temp integer to the number of connections from the session's source address
Willy Tarreau8b22a712010-06-18 17:46:06 +02003047 * in the table pointed to by expr, after updating it.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003048 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02003049 */
3050static int
Willy Tarreau281c7992013-01-08 01:23:27 +01003051smp_fetch_src_updt_conn_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02003052 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8b22a712010-06-18 17:46:06 +02003053{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003054 struct connection *conn = objt_conn(l4->si[0].end);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003055 struct stksess *ts;
3056 struct stktable_key *key;
3057 void *ptr;
3058
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003059 if (!conn)
3060 return 0;
3061
Thierry FOURNIER74c219d2014-04-14 14:35:40 +02003062 key = addr_to_stktable_key(&conn->addr.from, px->table.type);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003063 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003064 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02003065
Willy Tarreau24e32d82012-04-23 23:55:44 +02003066 px = args->data.prx;
Willy Tarreau8b22a712010-06-18 17:46:06 +02003067
Willy Tarreau1f7e9252010-06-20 12:27:21 +02003068 if ((ts = stktable_update_key(&px->table, key)) == NULL)
3069 /* entry does not exist and could not be created */
3070 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02003071
3072 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
3073 if (!ptr)
3074 return 0; /* parameter not stored in this table */
3075
Willy Tarreauf853c462012-04-23 18:53:56 +02003076 smp->type = SMP_T_UINT;
3077 smp->data.uint = ++stktable_data_cast(ptr, conn_cnt);
Willy Tarreau37406352012-04-23 16:16:37 +02003078 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau8b22a712010-06-18 17:46:06 +02003079 return 1;
3080}
3081
Willy Tarreauf44a5532013-07-23 15:17:53 +02003082/* set <smp> to the number of concurrent connections from the session's tracked
3083 * frontend counters. Supports being called as "sc[0-9]_conn_cur" or
3084 * "src_conn_cur" only.
3085 */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003086static int
Willy Tarreauf44a5532013-07-23 15:17:53 +02003087smp_fetch_sc_conn_cur(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
3088 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003089{
Willy Tarreauf44a5532013-07-23 15:17:53 +02003090 struct stkctr *stkctr = smp_fetch_sc_stkctr(l4, args, kw);
3091
3092 if (!stkctr)
3093 return 0;
3094
Willy Tarreau37406352012-04-23 16:16:37 +02003095 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003096 smp->type = SMP_T_UINT;
3097 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003098 if (stkctr_entry(stkctr) != NULL) {
3099 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CUR);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003100 if (!ptr)
3101 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003102 smp->data.uint = stktable_data_cast(ptr, conn_cur);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003103 }
3104 return 1;
3105}
3106
Willy Tarreau20843082013-07-23 15:35:33 +02003107/* set <smp> to the cumulated number of sessions from the session's tracked
3108 * frontend counters. Supports being called as "sc[0-9]_sess_cnt" or
3109 * "src_sess_cnt" only.
3110 */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003111static int
Willy Tarreau20843082013-07-23 15:35:33 +02003112smp_fetch_sc_sess_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
3113 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003114{
Willy Tarreau20843082013-07-23 15:35:33 +02003115 struct stkctr *stkctr = smp_fetch_sc_stkctr(l4, args, kw);
3116
3117 if (!stkctr)
3118 return 0;
3119
Willy Tarreau37406352012-04-23 16:16:37 +02003120 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003121 smp->type = SMP_T_UINT;
3122 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003123 if (stkctr_entry(stkctr) != NULL) {
3124 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003125 if (!ptr)
3126 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003127 smp->data.uint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003128 }
3129 return 1;
3130}
3131
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003132/* set <smp> to the session rate from the session's tracked frontend counters.
3133 * Supports being called as "sc[0-9]_sess_rate" or "src_sess_rate" only.
3134 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02003135static int
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003136smp_fetch_sc_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
3137 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau91c43d72010-06-20 11:19:22 +02003138{
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003139 struct stkctr *stkctr = smp_fetch_sc_stkctr(l4, args, kw);
3140
3141 if (!stkctr)
3142 return 0;
3143
Willy Tarreau37406352012-04-23 16:16:37 +02003144 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003145 smp->type = SMP_T_UINT;
3146 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003147 if (stkctr_entry(stkctr) != NULL) {
3148 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003149 if (!ptr)
3150 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003151 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003152 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003153 }
3154 return 1;
3155}
3156
Willy Tarreau91200da2013-07-23 15:55:19 +02003157/* set <smp> to the cumulated number of HTTP requests from the session's tracked
3158 * frontend counters. Supports being called as "sc[0-9]_http_req_cnt" or
3159 * "src_http_req_cnt" only.
3160 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003161static int
Willy Tarreau91200da2013-07-23 15:55:19 +02003162smp_fetch_sc_http_req_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
3163 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003164{
Willy Tarreau91200da2013-07-23 15:55:19 +02003165 struct stkctr *stkctr = smp_fetch_sc_stkctr(l4, args, kw);
3166
3167 if (!stkctr)
3168 return 0;
3169
Willy Tarreau37406352012-04-23 16:16:37 +02003170 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003171 smp->type = SMP_T_UINT;
3172 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003173 if (stkctr_entry(stkctr) != NULL) {
3174 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003175 if (!ptr)
3176 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003177 smp->data.uint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003178 }
3179 return 1;
3180}
3181
Willy Tarreaucf477632013-07-23 16:04:37 +02003182/* set <smp> to the HTTP request rate from the session's tracked frontend
3183 * counters. Supports being called as "sc[0-9]_http_req_rate" or
3184 * "src_http_req_rate" only.
3185 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003186static int
Willy Tarreaucf477632013-07-23 16:04:37 +02003187smp_fetch_sc_http_req_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
3188 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003189{
Willy Tarreaucf477632013-07-23 16:04:37 +02003190 struct stkctr *stkctr = smp_fetch_sc_stkctr(l4, args, kw);
3191
3192 if (!stkctr)
3193 return 0;
3194
Willy Tarreau37406352012-04-23 16:16:37 +02003195 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003196 smp->type = SMP_T_UINT;
3197 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003198 if (stkctr_entry(stkctr) != NULL) {
3199 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003200 if (!ptr)
3201 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003202 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreaucf477632013-07-23 16:04:37 +02003203 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003204 }
3205 return 1;
3206}
3207
Willy Tarreau30d07c32013-07-23 16:45:38 +02003208/* set <smp> to the cumulated number of HTTP requests errors from the session's
3209 * tracked frontend counters. Supports being called as "sc[0-9]_http_err_cnt" or
3210 * "src_http_err_cnt" only.
3211 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003212static int
Willy Tarreau30d07c32013-07-23 16:45:38 +02003213smp_fetch_sc_http_err_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
3214 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003215{
Willy Tarreau30d07c32013-07-23 16:45:38 +02003216 struct stkctr *stkctr = smp_fetch_sc_stkctr(l4, args, kw);
3217
3218 if (!stkctr)
3219 return 0;
3220
Willy Tarreau37406352012-04-23 16:16:37 +02003221 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003222 smp->type = SMP_T_UINT;
3223 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003224 if (stkctr_entry(stkctr) != NULL) {
3225 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003226 if (!ptr)
3227 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003228 smp->data.uint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003229 }
3230 return 1;
3231}
3232
Willy Tarreau9daf2622013-07-23 16:48:54 +02003233/* set <smp> to the HTTP request error rate from the session's tracked frontend
3234 * counters. Supports being called as "sc[0-9]_http_err_rate" or
3235 * "src_http_err_rate" only.
3236 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003237static int
Willy Tarreau9daf2622013-07-23 16:48:54 +02003238smp_fetch_sc_http_err_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
3239 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003240{
Willy Tarreau9daf2622013-07-23 16:48:54 +02003241 struct stkctr *stkctr = smp_fetch_sc_stkctr(l4, args, kw);
3242
3243 if (!stkctr)
3244 return 0;
3245
Willy Tarreau37406352012-04-23 16:16:37 +02003246 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003247 smp->type = SMP_T_UINT;
3248 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003249 if (stkctr_entry(stkctr) != NULL) {
3250 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003251 if (!ptr)
3252 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003253 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau9daf2622013-07-23 16:48:54 +02003254 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003255 }
3256 return 1;
3257}
3258
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003259/* set <smp> to the number of kbytes received from clients, as found in the
3260 * session's tracked frontend counters. Supports being called as
3261 * "sc[0-9]_kbytes_in" or "src_kbytes_in" only.
3262 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003263static int
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003264smp_fetch_sc_kbytes_in(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
3265 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003266{
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003267 struct stkctr *stkctr = smp_fetch_sc_stkctr(l4, args, kw);
3268
3269 if (!stkctr)
3270 return 0;
3271
Willy Tarreau37406352012-04-23 16:16:37 +02003272 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003273 smp->type = SMP_T_UINT;
3274 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003275 if (stkctr_entry(stkctr) != NULL) {
3276 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003277 if (!ptr)
3278 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003279 smp->data.uint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003280 }
3281 return 1;
3282}
3283
Willy Tarreau613fe992013-07-23 17:39:19 +02003284/* set <smp> to the data rate received from clients in bytes/s, as found
3285 * in the session's tracked frontend counters. Supports being called as
3286 * "sc[0-9]_bytes_in_rate" or "src_bytes_in_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003287 */
3288static int
Willy Tarreau613fe992013-07-23 17:39:19 +02003289smp_fetch_sc_bytes_in_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
3290 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003291{
Willy Tarreau613fe992013-07-23 17:39:19 +02003292 struct stkctr *stkctr = smp_fetch_sc_stkctr(l4, args, kw);
3293
3294 if (!stkctr)
3295 return 0;
3296
Willy Tarreau37406352012-04-23 16:16:37 +02003297 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003298 smp->type = SMP_T_UINT;
3299 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003300 if (stkctr_entry(stkctr) != NULL) {
3301 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003302 if (!ptr)
3303 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003304 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau613fe992013-07-23 17:39:19 +02003305 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003306 }
3307 return 1;
3308}
3309
Willy Tarreau53aea102013-07-23 17:39:02 +02003310/* set <smp> to the number of kbytes sent to clients, as found in the
3311 * session's tracked frontend counters. Supports being called as
3312 * "sc[0-9]_kbytes_out" or "src_kbytes_out" only.
3313 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003314static int
Willy Tarreau53aea102013-07-23 17:39:02 +02003315smp_fetch_sc_kbytes_out(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
3316 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003317{
Willy Tarreau53aea102013-07-23 17:39:02 +02003318 struct stkctr *stkctr = smp_fetch_sc_stkctr(l4, args, kw);
3319
3320 if (!stkctr)
3321 return 0;
3322
Willy Tarreau37406352012-04-23 16:16:37 +02003323 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003324 smp->type = SMP_T_UINT;
3325 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003326 if (stkctr_entry(stkctr) != NULL) {
3327 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003328 if (!ptr)
3329 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003330 smp->data.uint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003331 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003332 return 1;
3333}
3334
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003335/* set <smp> to the data rate sent to clients in bytes/s, as found in the
3336 * session's tracked frontend counters. Supports being called as
3337 * "sc[0-9]_bytes_out_rate" or "src_bytes_out_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003338 */
3339static int
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003340smp_fetch_sc_bytes_out_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
3341 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003342{
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003343 struct stkctr *stkctr = smp_fetch_sc_stkctr(l4, args, kw);
3344
3345 if (!stkctr)
3346 return 0;
3347
Willy Tarreau37406352012-04-23 16:16:37 +02003348 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003349 smp->type = SMP_T_UINT;
3350 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003351 if (stkctr_entry(stkctr) != NULL) {
3352 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003353 if (!ptr)
3354 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003355 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003356 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003357 }
3358 return 1;
3359}
3360
Willy Tarreau563eef42013-07-23 18:32:02 +02003361/* set <smp> to the number of active trackers on the SC entry in the session's
3362 * tracked frontend counters. Supports being called as "sc[0-9]_trackers" only.
3363 */
Willy Tarreau2406db42012-12-09 12:16:43 +01003364static int
Willy Tarreau563eef42013-07-23 18:32:02 +02003365smp_fetch_sc_trackers(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02003366 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2406db42012-12-09 12:16:43 +01003367{
Willy Tarreau563eef42013-07-23 18:32:02 +02003368 struct stkctr *stkctr = smp_fetch_sc_stkctr(l4, args, kw);
Willy Tarreau2406db42012-12-09 12:16:43 +01003369
Willy Tarreau563eef42013-07-23 18:32:02 +02003370 if (!stkctr)
Willy Tarreau2406db42012-12-09 12:16:43 +01003371 return 0;
3372
Willy Tarreau563eef42013-07-23 18:32:02 +02003373 smp->flags = SMP_F_VOL_TEST;
3374 smp->type = SMP_T_UINT;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003375 smp->data.uint = stkctr_entry(stkctr)->ref_cnt;
Willy Tarreau563eef42013-07-23 18:32:02 +02003376 return 1;
Willy Tarreaue25c9172013-05-28 18:32:20 +02003377}
3378
Willy Tarreau34db1082012-04-19 17:16:54 +02003379/* set temp integer to the number of used entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003380 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003381 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003382static int
Willy Tarreau281c7992013-01-08 01:23:27 +01003383smp_fetch_table_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02003384 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauc735a072011-03-29 00:57:02 +02003385{
Willy Tarreau37406352012-04-23 16:16:37 +02003386 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003387 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02003388 smp->data.uint = args->data.prx->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003389 return 1;
3390}
3391
Willy Tarreau34db1082012-04-19 17:16:54 +02003392/* set temp integer to the number of free entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003393 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003394 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003395static int
Willy Tarreau281c7992013-01-08 01:23:27 +01003396smp_fetch_table_avl(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02003397 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauc735a072011-03-29 00:57:02 +02003398{
Willy Tarreau24e32d82012-04-23 23:55:44 +02003399 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003400 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003401 smp->type = SMP_T_UINT;
3402 smp->data.uint = px->table.size - px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003403 return 1;
3404}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003405
Willy Tarreau61612d42012-04-19 18:42:05 +02003406/* Note: must not be declared <const> as its list will be overwritten.
3407 * Please take care of keeping this list alphabetically sorted.
3408 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003409static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau281c7992013-01-08 01:23:27 +01003410 { /* END */ },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003411}};
3412
Willy Tarreau281c7992013-01-08 01:23:27 +01003413/* Note: must not be declared <const> as its list will be overwritten.
3414 * Please take care of keeping this list alphabetically sorted.
3415 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003416static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
Willy Tarreau0f791d42013-07-23 19:56:43 +02003417 { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3418 { "sc_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3419 { "sc_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3420 { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3421 { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3422 { "sc_conn_rate", smp_fetch_sc_conn_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3423 { "sc_get_gpc0", smp_fetch_sc_get_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3424 { "sc_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3425 { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3426 { "sc_http_err_rate", smp_fetch_sc_http_err_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3427 { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3428 { "sc_http_req_rate", smp_fetch_sc_http_req_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3429 { "sc_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3430 { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3431 { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3432 { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3433 { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3434 { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,UINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3435 { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3436 { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3437 { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3438 { "sc0_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3439 { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3440 { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3441 { "sc0_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3442 { "sc0_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3443 { "sc0_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3444 { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3445 { "sc0_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3446 { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3447 { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3448 { "sc0_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3449 { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3450 { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3451 { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3452 { "sc0_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3453 { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3454 { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3455 { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3456 { "sc1_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3457 { "sc1_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3458 { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3459 { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3460 { "sc1_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3461 { "sc1_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3462 { "sc1_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3463 { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3464 { "sc1_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3465 { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3466 { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3467 { "sc1_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3468 { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3469 { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3470 { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3471 { "sc1_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3472 { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3473 { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3474 { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3475 { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3476 { "sc2_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3477 { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3478 { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3479 { "sc2_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3480 { "sc2_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3481 { "sc2_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3482 { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3483 { "sc2_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3484 { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3485 { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3486 { "sc2_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3487 { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3488 { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3489 { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3490 { "sc2_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3491 { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3492 { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3493 { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3494 { "src_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3495 { "src_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3496 { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3497 { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3498 { "src_conn_rate", smp_fetch_sc_conn_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3499 { "src_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3500 { "src_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3501 { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3502 { "src_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3503 { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3504 { "src_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3505 { "src_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3506 { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3507 { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3508 { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3509 { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3510 { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3511 { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3512 { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
Willy Tarreau281c7992013-01-08 01:23:27 +01003513 { /* END */ },
3514}};
3515
Willy Tarreau8b22a712010-06-18 17:46:06 +02003516__attribute__((constructor))
3517static void __session_init(void)
3518{
Willy Tarreau281c7992013-01-08 01:23:27 +01003519 sample_register_fetches(&smp_fetch_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003520 acl_register_keywords(&acl_kws);
3521}
3522
Willy Tarreaubaaee002006-06-26 02:48:02 +02003523/*
3524 * Local variables:
3525 * c-indent-level: 8
3526 * c-basic-offset: 8
3527 * End:
3528 */