blob: aaea9c08c8159d8b0b7336e5ec50de936289f2cc [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau87b09662015-04-03 00:22:06 +02002 * Stream 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>
Thierry FOURNIER65f34c62015-02-16 20:11:43 +010036#include <proto/hlua.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020037#include <proto/listener.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020038#include <proto/log.h>
Willy Tarreaucbaaec42012-09-06 11:32:07 +020039#include <proto/raw_sock.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020040#include <proto/stream.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010041#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010042#include <proto/proto_http.h>
43#include <proto/proto_tcp.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020044#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020045#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010046#include <proto/server.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020047#include <proto/sample.h>
Emeric Brun1d33b292010-01-04 15:47:17 +010048#include <proto/stick_table.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010049#include <proto/stream_interface.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010050#include <proto/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020051
Willy Tarreau87b09662015-04-03 00:22:06 +020052struct pool_head *pool2_stream;
53struct list streams;
Willy Tarreaubaaee002006-06-26 02:48:02 +020054
Willy Tarreau87b09662015-04-03 00:22:06 +020055/* list of streams waiting for at least one buffer */
Willy Tarreaubf883e02014-11-25 21:10:35 +010056struct list buffer_wq = LIST_HEAD_INIT(buffer_wq);
57
Willy Tarreau87b09662015-04-03 00:22:06 +020058static int conn_stream_complete(struct connection *conn);
59static int conn_stream_update(struct connection *conn);
Willy Tarreau2542b532012-08-31 16:01:23 +020060static struct task *expire_mini_session(struct task *t);
Willy Tarreau87b09662015-04-03 00:22:06 +020061int stream_complete(struct stream *s);
Willy Tarreau2542b532012-08-31 16:01:23 +020062
Willy Tarreau87b09662015-04-03 00:22:06 +020063/* data layer callbacks for an embryonic stream */
Willy Tarreau5e75e272012-10-02 21:21:20 +020064struct data_cb sess_conn_cb = {
65 .recv = NULL,
66 .send = NULL,
Willy Tarreau87b09662015-04-03 00:22:06 +020067 .wake = conn_stream_update,
68 .init = conn_stream_complete,
Willy Tarreau5e75e272012-10-02 21:21:20 +020069};
70
Willy Tarreau2542b532012-08-31 16:01:23 +020071/* This function is called from the protocol layer accept() in order to
Willy Tarreau87b09662015-04-03 00:22:06 +020072 * instanciate a new embryonic stream on behalf of a given listener and
Willy Tarreau2542b532012-08-31 16:01:23 +020073 * frontend. It returns a positive value upon success, 0 if the connection
74 * can be ignored, or a negative value upon critical failure. The accepted
75 * file descriptor is closed if we return <= 0.
Willy Tarreau81f9aa32010-06-01 17:45:26 +020076 */
Willy Tarreau87b09662015-04-03 00:22:06 +020077int stream_accept(struct listener *l, int cfd, struct sockaddr_storage *addr)
Willy Tarreau81f9aa32010-06-01 17:45:26 +020078{
Willy Tarreaub363a1f2013-10-01 10:45:07 +020079 struct connection *cli_conn;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020080 struct proxy *p = l->frontend;
Willy Tarreau87b09662015-04-03 00:22:06 +020081 struct stream *s;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020082 struct task *t;
Willy Tarreauabe8ea52010-11-11 10:56:04 +010083 int ret;
84
85
86 ret = -1; /* assume unrecoverable error by default */
Willy Tarreau81f9aa32010-06-01 17:45:26 +020087
Willy Tarreau32e3c6a2013-10-11 19:34:20 +020088 if (unlikely((cli_conn = conn_new()) == NULL))
Willy Tarreau81f9aa32010-06-01 17:45:26 +020089 goto out_close;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020090
Willy Tarreau32e3c6a2013-10-11 19:34:20 +020091 conn_prepare(cli_conn, l->proto, l->xprt);
Willy Tarreauf2943dc2012-10-26 20:10:28 +020092
Willy Tarreau32e3c6a2013-10-11 19:34:20 +020093 cli_conn->t.sock.fd = cfd;
94 cli_conn->addr.from = *addr;
95 cli_conn->flags |= CO_FL_ADDR_FROM_SET;
96 cli_conn->target = &l->obj_type;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010097 cli_conn->proxy_netns = l->netns;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +020098
Willy Tarreau87b09662015-04-03 00:22:06 +020099 if (unlikely((s = pool_alloc2(pool2_stream)) == NULL))
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200100 goto out_free_conn;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200101
Willy Tarreau87b09662015-04-03 00:22:06 +0200102 /* minimum stream initialization required for an embryonic stream is
Willy Tarreau2542b532012-08-31 16:01:23 +0200103 * fairly low. We need very little to execute L4 ACLs, then we need a
104 * task to make the client-side connection live on its own.
105 * - flags
106 * - stick-entry tracking
107 */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200108 s->flags = 0;
109 s->logs.logwait = p->to_log;
Willy Tarreau9a355ec2013-06-11 17:45:46 +0200110 s->logs.level = 0;
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +0200111
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +0100112 /* Initialise the current rule list pointer to NULL. We are sure that
113 * any rulelist match the NULL pointer.
114 */
115 s->current_rule_list = NULL;
116
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +0200117 memset(s->stkctr, 0, sizeof(s->stkctr));
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200118
Willy Tarreaub1ec8c42015-04-03 13:53:24 +0200119 s->sess = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200120 s->listener = l;
121 s->fe = p;
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100122
Willy Tarreaua5f5d8d2014-11-28 11:26:07 +0100123 s->si[0].flags = SI_FL_NONE;
124 s->si[1].flags = SI_FL_ISBACK;
125
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200126 /* On a mini-session, the connection is directly attached to the
Willy Tarreau87b09662015-04-03 00:22:06 +0200127 * stream's target so that we don't need to initialize the stream
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200128 * interfaces. Another benefit is that it's easy to detect a mini-
Willy Tarreau87b09662015-04-03 00:22:06 +0200129 * stream in dumps using this : it's the only one which has a
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200130 * connection in s->target.
131 */
132 s->target = &cli_conn->obj_type;
133
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200134 s->logs.accept_date = date; /* user-visible date for logging */
135 s->logs.tv_accept = now; /* corrected date for internal use */
Willy Tarreau1f0da242014-01-25 11:01:50 +0100136 s->uniq_id = global.req_count++;
Willy Tarreau2542b532012-08-31 16:01:23 +0200137 p->feconn++;
Willy Tarreau87b09662015-04-03 00:22:06 +0200138 /* This stream was accepted, count it now */
Willy Tarreau2542b532012-08-31 16:01:23 +0200139 if (p->feconn > p->fe_counters.conn_max)
140 p->fe_counters.conn_max = p->feconn;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200141
Willy Tarreau2542b532012-08-31 16:01:23 +0200142 proxy_inc_fe_conn_ctr(l, p);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200143
Willy Tarreau59e3ff42013-12-16 02:16:50 +0100144 /* Add the minimum callbacks to prepare the connection's control layer.
145 * We need this so that we can safely execute the ACLs used by the
146 * "tcp-request connection" ruleset. We also carefully attach the
147 * connection to the stream interface without initializing the rest,
148 * so that ACLs can use si[0]->end.
149 */
150 si_attach_conn(&s->si[0], cli_conn);
151 conn_attach(cli_conn, s, &sess_conn_cb);
152 conn_ctrl_init(cli_conn);
153
Willy Tarreau2cff2f72013-12-16 10:12:54 +0100154 /* now evaluate the tcp-request layer4 rules. Since we expect to be able
155 * to abort right here as soon as possible, we check the rules before
156 * even initializing the stream interfaces.
157 */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200158 if ((l->options & LI_O_TCP_RULES) && !tcp_exec_req_rules(s)) {
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200159 /* let's do a no-linger now to close with a single RST. */
160 setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100161 ret = 0; /* successful termination */
Willy Tarreau2542b532012-08-31 16:01:23 +0200162 goto out_free_session;
163 }
164
Willy Tarreau82569f92012-09-27 23:48:56 +0200165 /* monitor-net and health mode are processed immediately after TCP
166 * connection rules. This way it's possible to block them, but they
167 * never use the lower data layers, they send directly over the socket,
168 * as they were designed for. We first flush the socket receive buffer
169 * in order to avoid emission of an RST by the system. We ignore any
170 * error.
171 */
172 if (unlikely((p->mode == PR_MODE_HEALTH) ||
173 ((l->options & LI_O_CHK_MONNET) &&
174 addr->ss_family == AF_INET &&
175 (((struct sockaddr_in *)addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr))) {
176 /* we have 4 possibilities here :
177 * - HTTP mode, from monitoring address => send "HTTP/1.0 200 OK"
178 * - HEALTH mode with HTTP check => send "HTTP/1.0 200 OK"
179 * - HEALTH mode without HTTP check => just send "OK"
180 * - TCP mode from monitoring address => just close
181 */
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200182 if (l->proto->drain)
183 l->proto->drain(cfd);
Willy Tarreau82569f92012-09-27 23:48:56 +0200184 if (p->mode == PR_MODE_HTTP ||
185 (p->mode == PR_MODE_HEALTH && (p->options2 & PR_O2_CHK_ANY) == PR_O2_HTTP_CHK))
186 send(cfd, "HTTP/1.0 200 OK\r\n\r\n", 19, MSG_DONTWAIT|MSG_NOSIGNAL|MSG_MORE);
187 else if (p->mode == PR_MODE_HEALTH)
188 send(cfd, "OK\n", 3, MSG_DONTWAIT|MSG_NOSIGNAL|MSG_MORE);
189 ret = 0;
190 goto out_free_session;
191 }
192
Willy Tarreau22cda212012-08-31 17:43:29 +0200193 /* wait for a PROXY protocol header */
194 if (l->options & LI_O_ACC_PROXY) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200195 cli_conn->flags |= CO_FL_ACCEPT_PROXY;
196 conn_sock_want_recv(cli_conn);
Willy Tarreau22cda212012-08-31 17:43:29 +0200197 }
198
Willy Tarreau2542b532012-08-31 16:01:23 +0200199 if (unlikely((t = task_new()) == NULL))
200 goto out_free_session;
201
202 t->context = s;
203 t->nice = l->nice;
204 s->task = t;
205
Willy Tarreau59e3ff42013-12-16 02:16:50 +0100206 /* Finish setting the callbacks. Right now the transport layer is present
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200207 * but not initialized. Also note we need to be careful as the stream
208 * int is not initialized yet.
Willy Tarreau2542b532012-08-31 16:01:23 +0200209 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200210 conn_data_want_recv(cli_conn);
211 if (conn_xprt_init(cli_conn) < 0)
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100212 goto out_free_task;
Willy Tarreau2542b532012-08-31 16:01:23 +0200213
214 /* OK, now either we have a pending handshake to execute with and
215 * then we must return to the I/O layer, or we can proceed with the
Willy Tarreau87b09662015-04-03 00:22:06 +0200216 * end of the stream initialization. In case of handshake, we also
Willy Tarreau2542b532012-08-31 16:01:23 +0200217 * set the I/O timeout to the frontend's client timeout.
218 */
219
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200220 if (cli_conn->flags & CO_FL_HANDSHAKE) {
Willy Tarreau2542b532012-08-31 16:01:23 +0200221 t->process = expire_mini_session;
222 t->expire = tick_add_ifset(now_ms, p->timeout.client);
223 task_queue(t);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200224 cli_conn->flags |= CO_FL_INIT_DATA | CO_FL_WAKE_DATA;
Willy Tarreau2542b532012-08-31 16:01:23 +0200225 return 1;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200226 }
227
Willy Tarreau87b09662015-04-03 00:22:06 +0200228 /* OK let's complete stream initialization since there is no handshake */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200229 cli_conn->flags |= CO_FL_CONNECTED;
Willy Tarreau87b09662015-04-03 00:22:06 +0200230 ret = stream_complete(s);
Willy Tarreau2542b532012-08-31 16:01:23 +0200231 if (ret > 0)
232 return ret;
233
234 /* Error unrolling */
235 out_free_task:
236 task_free(t);
237 out_free_session:
238 p->feconn--;
Willy Tarreau87b09662015-04-03 00:22:06 +0200239 stream_store_counters(s);
240 pool_free2(pool2_stream, s);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200241 out_free_conn:
242 cli_conn->flags &= ~CO_FL_XPRT_TRACKED;
243 conn_xprt_close(cli_conn);
244 conn_free(cli_conn);
Willy Tarreau2542b532012-08-31 16:01:23 +0200245 out_close:
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200246 if (ret < 0 && l->xprt == &raw_sock && p->mode == PR_MODE_HTTP) {
Willy Tarreau2542b532012-08-31 16:01:23 +0200247 /* critical error, no more memory, try to emit a 500 response */
Willy Tarreau05bf5e12013-10-20 23:10:28 +0200248 struct chunk *err_msg = &p->errmsg[HTTP_ERR_500];
249 if (!err_msg->str)
250 err_msg = &http_err_chunks[HTTP_ERR_500];
Willy Tarreau2542b532012-08-31 16:01:23 +0200251 send(cfd, err_msg->str, err_msg->len, MSG_DONTWAIT|MSG_NOSIGNAL);
252 }
253
254 if (fdtab[cfd].owner)
255 fd_delete(cfd);
256 else
257 close(cfd);
258 return ret;
259}
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100260
Willy Tarreau0af29122012-12-03 15:35:00 +0100261
Willy Tarreau87b09662015-04-03 00:22:06 +0200262/* prepare the trash with a log prefix for stream <s>. It only works with
263 * embryonic streams based on a real connection. This function requires that
Willy Tarreaub4f98092014-05-08 21:06:11 +0200264 * at s->target still points to the incoming connection.
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200265 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200266static void prepare_mini_sess_log_prefix(struct stream *s)
Willy Tarreau0af29122012-12-03 15:35:00 +0100267{
268 struct tm tm;
269 char pn[INET6_ADDRSTRLEN];
270 int ret;
271 char *end;
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200272 struct connection *cli_conn = __objt_conn(s->target);
Willy Tarreau0af29122012-12-03 15:35:00 +0100273
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200274 ret = addr_to_str(&cli_conn->addr.from, pn, sizeof(pn));
Willy Tarreau0af29122012-12-03 15:35:00 +0100275 if (ret <= 0)
276 chunk_printf(&trash, "unknown [");
277 else if (ret == AF_UNIX)
278 chunk_printf(&trash, "%s:%d [", pn, s->listener->luid);
279 else
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200280 chunk_printf(&trash, "%s:%d [", pn, get_host_port(&cli_conn->addr.from));
Willy Tarreau0af29122012-12-03 15:35:00 +0100281
282 get_localtime(s->logs.accept_date.tv_sec, &tm);
283 end = date2str_log(trash.str + trash.len, &tm, &(s->logs.accept_date), trash.size - trash.len);
284 trash.len = end - trash.str;
285 if (s->listener->name)
286 chunk_appendf(&trash, "] %s/%s", s->fe->id, s->listener->name);
287 else
288 chunk_appendf(&trash, "] %s/%d", s->fe->id, s->listener->luid);
289}
290
Willy Tarreau87b09662015-04-03 00:22:06 +0200291/* This function kills an existing embryonic stream. It stops the connection's
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200292 * transport layer, releases assigned resources, resumes the listener if it was
Willy Tarreaub4f98092014-05-08 21:06:11 +0200293 * disabled and finally kills the file descriptor. This function requires that
294 * at s->target still points to the incoming connection.
Willy Tarreau2542b532012-08-31 16:01:23 +0200295 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200296static void kill_mini_session(struct stream *s)
Willy Tarreau2542b532012-08-31 16:01:23 +0200297{
Willy Tarreau0af29122012-12-03 15:35:00 +0100298 int level = LOG_INFO;
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200299 struct connection *conn = __objt_conn(s->target);
Willy Tarreau0af29122012-12-03 15:35:00 +0100300 unsigned int log = s->logs.logwait;
301 const char *err_msg;
302
303 if (s->fe->options2 & PR_O2_LOGERRORS)
304 level = LOG_ERR;
305
306 if (log && (s->fe->options & PR_O_NULLNOLOG)) {
307 /* with "option dontlognull", we don't log connections with no transfer */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100308 if (!conn->err_code ||
Willy Tarreau20879a02012-12-03 16:32:10 +0100309 conn->err_code == CO_ER_PRX_EMPTY || conn->err_code == CO_ER_PRX_ABORT ||
310 conn->err_code == CO_ER_SSL_EMPTY || conn->err_code == CO_ER_SSL_ABORT)
Willy Tarreau0af29122012-12-03 15:35:00 +0100311 log = 0;
312 }
313
314 if (log) {
315 if (!conn->err_code && (s->task->state & TASK_WOKEN_TIMER)) {
316 if (conn->flags & CO_FL_ACCEPT_PROXY)
317 conn->err_code = CO_ER_PRX_TIMEOUT;
318 else if (conn->flags & CO_FL_SSL_WAIT_HS)
319 conn->err_code = CO_ER_SSL_TIMEOUT;
320 }
321
322 prepare_mini_sess_log_prefix(s);
323 err_msg = conn_err_code_str(conn);
324 if (err_msg)
325 send_log(s->fe, level, "%s: %s\n", trash.str, err_msg);
326 else
327 send_log(s->fe, level, "%s: unknown connection error (code=%d flags=%08x)\n",
328 trash.str, conn->err_code, conn->flags);
329 }
330
Willy Tarreau2542b532012-08-31 16:01:23 +0200331 /* kill the connection now */
Willy Tarreauf79c8172013-10-21 16:30:56 +0200332 conn_force_close(conn);
Willy Tarreaua23ee3a2014-02-05 00:18:47 +0100333 conn_free(conn);
Willy Tarreau2542b532012-08-31 16:01:23 +0200334
335 s->fe->feconn--;
Willy Tarreau87b09662015-04-03 00:22:06 +0200336 stream_store_counters(s);
Willy Tarreau2542b532012-08-31 16:01:23 +0200337
338 if (!(s->listener->options & LI_O_UNLIMITED))
339 actconn--;
340 jobs--;
341 s->listener->nbconn--;
342 if (s->listener->state == LI_FULL)
343 resume_listener(s->listener);
344
345 /* Dequeues all of the listeners waiting for a resource */
346 if (!LIST_ISEMPTY(&global_listener_queue))
347 dequeue_all_listeners(&global_listener_queue);
348
349 if (!LIST_ISEMPTY(&s->fe->listener_queue) &&
350 (!s->fe->fe_sps_lim || freq_ctr_remain(&s->fe->fe_sess_per_sec, s->fe->fe_sps_lim, 0) > 0))
351 dequeue_all_listeners(&s->fe->listener_queue);
352
353 task_delete(s->task);
354 task_free(s->task);
Willy Tarreau87b09662015-04-03 00:22:06 +0200355 pool_free2(pool2_stream, s);
Willy Tarreau2542b532012-08-31 16:01:23 +0200356}
357
Willy Tarreau87b09662015-04-03 00:22:06 +0200358/* Finish initializing a stream from a connection, or kills it if the
Willy Tarreau22cda212012-08-31 17:43:29 +0200359 * connection shows and error. Returns <0 if the connection was killed.
Willy Tarreau2542b532012-08-31 16:01:23 +0200360 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200361static int conn_stream_complete(struct connection *conn)
Willy Tarreau2542b532012-08-31 16:01:23 +0200362{
Willy Tarreau87b09662015-04-03 00:22:06 +0200363 struct stream *s = conn->owner;
Willy Tarreau2542b532012-08-31 16:01:23 +0200364
Willy Tarreau87b09662015-04-03 00:22:06 +0200365 if (!(conn->flags & CO_FL_ERROR) && (stream_complete(s) > 0)) {
Willy Tarreau071e1372012-10-03 01:39:48 +0200366 conn->flags &= ~CO_FL_INIT_DATA;
Willy Tarreau2542b532012-08-31 16:01:23 +0200367 return 0;
368 }
369
370 /* kill the connection now */
371 kill_mini_session(s);
372 return -1;
373}
374
Willy Tarreau87b09662015-04-03 00:22:06 +0200375/* Update an embryonic stream status. The connection is killed in case of
Willy Tarreau9683e9a2012-10-03 21:17:23 +0200376 * error, and <0 will be returned. Otherwise it does nothing.
377 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200378static int conn_stream_update(struct connection *conn)
Willy Tarreau9683e9a2012-10-03 21:17:23 +0200379{
380 if (conn->flags & CO_FL_ERROR) {
381 kill_mini_session(conn->owner);
382 return -1;
383 }
384 return 0;
385}
386
Willy Tarreau87b09662015-04-03 00:22:06 +0200387/* Manages embryonic streams timeout. It is only called when the timeout
Willy Tarreau2542b532012-08-31 16:01:23 +0200388 * strikes and performs the required cleanup.
389 */
390static struct task *expire_mini_session(struct task *t)
391{
Willy Tarreau87b09662015-04-03 00:22:06 +0200392 struct stream *s = t->context;
Willy Tarreau2542b532012-08-31 16:01:23 +0200393
394 if (!(t->state & TASK_WOKEN_TIMER))
395 return t;
396
397 kill_mini_session(s);
398 return NULL;
399}
400
401/* This function is called from the I/O handler which detects the end of
Willy Tarreau87b09662015-04-03 00:22:06 +0200402 * handshake, in order to complete initialization of a valid stream. It must
403 * be called with an embryonic stream. It returns a positive value upon
Willy Tarreau2542b532012-08-31 16:01:23 +0200404 * success, 0 if the connection can be ignored, or a negative value upon
405 * critical failure. The accepted file descriptor is closed if we return <= 0.
Willy Tarreaub4f98092014-05-08 21:06:11 +0200406 * The client-side end point is assumed to be a connection, whose pointer is
407 * taken from s->target which is assumed to be valid. If the function fails,
408 * it restores s->target.
Willy Tarreau2542b532012-08-31 16:01:23 +0200409 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200410int stream_complete(struct stream *s)
Willy Tarreau2542b532012-08-31 16:01:23 +0200411{
412 struct listener *l = s->listener;
413 struct proxy *p = s->fe;
414 struct http_txn *txn;
415 struct task *t = s->task;
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200416 struct connection *conn = __objt_conn(s->target);
Willy Tarreau2542b532012-08-31 16:01:23 +0200417 int ret;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100418 int i;
Willy Tarreau2542b532012-08-31 16:01:23 +0200419
420 ret = -1; /* assume unrecoverable error by default */
421
Willy Tarreau87b09662015-04-03 00:22:06 +0200422 /* OK, we're keeping the stream, so let's properly initialize the stream */
423 LIST_ADDQ(&streams, &s->list);
Willy Tarreau2542b532012-08-31 16:01:23 +0200424 LIST_INIT(&s->back_refs);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100425 LIST_INIT(&s->buffer_wait);
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200426
Willy Tarreaue7dff022015-04-03 01:14:29 +0200427 s->flags |= SF_INITIALIZED;
Willy Tarreau2542b532012-08-31 16:01:23 +0200428 s->unique_id = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200429
430 t->process = l->handler;
431 t->context = s;
432 t->expire = TICK_ETERNITY;
433
Willy Tarreau87b09662015-04-03 00:22:06 +0200434 /* Note: initially, the stream's backend points to the frontend.
Willy Tarreau2542b532012-08-31 16:01:23 +0200435 * This changes later when switching rules are executed or
436 * when the default backend is assigned.
437 */
438 s->be = s->fe;
William Lallemand82fe75c2012-10-23 10:25:10 +0200439 s->comp_algo = NULL;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100440 s->req.buf = s->res.buf = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200441
Willy Tarreau87b09662015-04-03 00:22:06 +0200442 /* Let's count a stream now */
Willy Tarreaub36b4242010-06-04 20:59:39 +0200443 proxy_inc_fe_sess_ctr(l, p);
Willy Tarreau91c43d72010-06-20 11:19:22 +0200444
Willy Tarreaub4c84932013-07-23 19:15:30 +0200445 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200446 void *ptr;
447
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100448 if (!stkctr_entry(&s->stkctr[i]))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100449 continue;
450
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100451 ptr = stktable_data_ptr(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]), STKTABLE_DT_SESS_CNT);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200452 if (ptr)
453 stktable_data_cast(ptr, sess_cnt)++;
454
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100455 ptr = stktable_data_ptr(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]), STKTABLE_DT_SESS_RATE);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200456 if (ptr)
457 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau20d46a52012-12-09 15:55:40 +0100458 s->stkctr[i].table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200459 }
460
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200461 /* this part should be common with other protocols */
Willy Tarreau819d3322014-11-28 12:12:34 +0100462 si_reset(&s->si[0]);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200463 si_set_state(&s->si[0], SI_ST_EST);
464
Willy Tarreaub4f98092014-05-08 21:06:11 +0200465 /* attach the incoming connection to the stream interface now.
466 * We must do that *before* clearing ->target because we need
467 * to keep a pointer to the connection in case we have to call
468 * kill_mini_session().
469 */
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200470 si_attach_conn(&s->si[0], conn);
471
472 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
473 s->si[0].flags |= SI_FL_INDEP_STR;
474
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200475 /* pre-initialize the other side's stream interface to an INIT state. The
476 * callbacks will be initialized before attempting to connect.
477 */
Willy Tarreau819d3322014-11-28 12:12:34 +0100478 si_reset(&s->si[1]);
Willy Tarreau2a6e8802013-10-24 15:50:53 +0200479 si_detach(&s->si[1]);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200480
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200481 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
482 s->si[1].flags |= SI_FL_INDEP_STR;
483
Willy Tarreau87b09662015-04-03 00:22:06 +0200484 stream_init_srv_conn(s);
Willy Tarreau10b688f2015-03-13 16:43:12 +0100485 s->target = l->default_target; /* used by peers and CLI */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200486 s->pend_pos = NULL;
487
488 /* init store persistence */
489 s->store_count = 0;
490
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100491 channel_init(&s->req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100492 s->req.flags |= CF_READ_ATTACHED; /* the producer is already connected */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200493
494 /* activate default analysers enabled for this listener */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100495 s->req.analysers = l->analysers;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200496
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100497 s->req.wto = TICK_ETERNITY;
498 s->req.rto = TICK_ETERNITY;
499 s->req.rex = TICK_ETERNITY;
500 s->req.wex = TICK_ETERNITY;
501 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200502
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100503 channel_init(&s->res);
Willy Tarreauef573c02014-11-28 14:17:09 +0100504 s->res.flags |= CF_ISRESP;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100505 s->res.analysers = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200506
Willy Tarreau96e31212011-05-30 18:10:30 +0200507 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100508 s->req.flags |= CF_NEVER_WAIT;
509 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +0200510 }
511
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100512 s->res.rto = TICK_ETERNITY;
513 s->res.wto = TICK_ETERNITY;
514 s->res.rex = TICK_ETERNITY;
515 s->res.wex = TICK_ETERNITY;
516 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200517
Willy Tarreau62f791e2012-03-09 11:32:30 +0100518 txn = &s->txn;
519 /* Those variables will be checked and freed if non-NULL in
Willy Tarreau87b09662015-04-03 00:22:06 +0200520 * stream.c:stream_free(). It is important that they are
Willy Tarreau62f791e2012-03-09 11:32:30 +0100521 * properly initialized.
522 */
523 txn->sessid = NULL;
524 txn->srv_cookie = NULL;
525 txn->cli_cookie = NULL;
526 txn->uri = NULL;
527 txn->req.cap = NULL;
528 txn->rsp.cap = NULL;
529 txn->hdr_idx.v = NULL;
530 txn->hdr_idx.size = txn->hdr_idx.used = 0;
Willy Tarreau068621e2013-12-23 15:11:25 +0100531 txn->flags = 0;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100532 txn->req.flags = 0;
533 txn->rsp.flags = 0;
534 /* the HTTP messages need to know what buffer they're associated with */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100535 txn->req.chn = &s->req;
536 txn->rsp.chn = &s->res;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100537
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100538 HLUA_INIT(&s->hlua);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100539
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200540 /* finish initialization of the accepted file descriptor */
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200541 conn_data_want_recv(conn);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200542
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100543 if (p->accept && (ret = p->accept(s)) <= 0) {
544 /* Either we had an unrecoverable error (<0) or work is
545 * finished (=0, eg: monitoring), in both situations,
546 * we can release everything and close.
547 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100548 goto out_free_strm;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200549 }
550
Willy Tarreau93dbc2b2012-10-12 18:01:49 +0200551 /* if logs require transport layer information, note it on the connection */
552 if (s->logs.logwait & LW_XPRT)
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200553 conn->flags |= CO_FL_XPRT_TRACKED;
Willy Tarreau93dbc2b2012-10-12 18:01:49 +0200554
Willy Tarreau2542b532012-08-31 16:01:23 +0200555 /* we want the connection handler to notify the stream interface about updates. */
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200556 conn->flags |= CO_FL_WAKE_DATA;
Willy Tarreau2542b532012-08-31 16:01:23 +0200557
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200558 /* it is important not to call the wakeup function directly but to
559 * pass through task_wakeup(), because this one knows how to apply
560 * priorities to tasks.
561 */
562 task_wakeup(t, TASK_WOKEN_INIT);
563 return 1;
564
565 /* Error unrolling */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100566 out_free_strm:
Willy Tarreaub4f98092014-05-08 21:06:11 +0200567 /* and restore the connection pointer in case we destroyed it,
568 * because kill_mini_session() will need it.
569 */
Willy Tarreau3b246412014-11-25 17:10:33 +0100570 LIST_DEL(&s->list);
Willy Tarreaub4f98092014-05-08 21:06:11 +0200571 s->target = &conn->obj_type;
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100572 return ret;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200573}
574
Willy Tarreaubaaee002006-06-26 02:48:02 +0200575/*
Willy Tarreau87b09662015-04-03 00:22:06 +0200576 * frees the context associated to a stream. It must have been removed first.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200577 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200578static void stream_free(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200579{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +0100580 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +0200581 struct proxy *fe = s->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100582 struct bref *bref, *back;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200583 struct connection *cli_conn = objt_conn(s->si[0].end);
Willy Tarreaua4cda672010-06-06 18:28:49 +0200584 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100585
Willy Tarreaubaaee002006-06-26 02:48:02 +0200586 if (s->pend_pos)
587 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100588
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100589 if (objt_server(s->target)) { /* there may be requests left pending in queue */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200590 if (s->flags & SF_CURR_SESS) {
591 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100592 objt_server(s->target)->cur_sess--;
Willy Tarreau1e62de62008-11-11 20:20:02 +0100593 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100594 if (may_dequeue_tasks(objt_server(s->target), s->be))
595 process_srv_queue(objt_server(s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100596 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100597
Willy Tarreau7c669d72008-06-20 15:04:11 +0200598 if (unlikely(s->srv_conn)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200599 /* the stream still has a reserved slot on a server, but
Willy Tarreau7c669d72008-06-20 15:04:11 +0200600 * it should normally be only the same as the one above,
601 * so this should not happen in fact.
602 */
603 sess_change_server(s, NULL);
604 }
605
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100606 if (s->req.pipe)
607 put_pipe(s->req.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100608
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100609 if (s->res.pipe)
610 put_pipe(s->res.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100611
Willy Tarreaubf883e02014-11-25 21:10:35 +0100612 /* We may still be present in the buffer wait queue */
613 if (!LIST_ISEMPTY(&s->buffer_wait)) {
614 LIST_DEL(&s->buffer_wait);
615 LIST_INIT(&s->buffer_wait);
616 }
617
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100618 b_drop(&s->req.buf);
619 b_drop(&s->res.buf);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100620 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200621 stream_offer_buffers();
Willy Tarreau9b28e032012-10-12 23:49:43 +0200622
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100623 hlua_ctx_destroy(&s->hlua);
Willy Tarreau46023632010-01-07 22:51:47 +0100624 http_end_txn(s);
625
Willy Tarreau1e954912012-10-12 17:50:05 +0200626 /* ensure the client-side transport layer is destroyed */
Willy Tarreauf79c8172013-10-21 16:30:56 +0200627 if (cli_conn)
628 conn_force_close(cli_conn);
Willy Tarreau1e954912012-10-12 17:50:05 +0200629
Willy Tarreaua4cda672010-06-06 18:28:49 +0200630 for (i = 0; i < s->store_count; i++) {
631 if (!s->store[i].ts)
632 continue;
633 stksess_free(s->store[i].table, s->store[i].ts);
634 s->store[i].ts = NULL;
635 }
636
Willy Tarreau34eb6712011-10-24 18:15:04 +0200637 pool_free2(pool2_hdr_idx, txn->hdr_idx.v);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200638 if (fe) {
Willy Tarreau46023632010-01-07 22:51:47 +0100639 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
640 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200641 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100642
Willy Tarreau87b09662015-04-03 00:22:06 +0200643 stream_store_counters(s);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200644
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100645 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100646 /* we have to unlink all watchers. We must not relink them if
Willy Tarreau87b09662015-04-03 00:22:06 +0200647 * this stream was the last one in the list.
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100648 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100649 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100650 LIST_INIT(&bref->users);
Willy Tarreau87b09662015-04-03 00:22:06 +0200651 if (s->list.n != &streams)
652 LIST_ADDQ(&LIST_ELEM(s->list.n, struct stream *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100653 bref->ref = s->list.n;
654 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100655 LIST_DEL(&s->list);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200656 si_release_endpoint(&s->si[1]);
657 si_release_endpoint(&s->si[0]);
Willy Tarreau87b09662015-04-03 00:22:06 +0200658 pool_free2(pool2_stream, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200659
660 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200661 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +0200662 pool_flush2(pool2_buffer);
Willy Tarreau34eb6712011-10-24 18:15:04 +0200663 pool_flush2(pool2_hdr_idx);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200664 pool_flush2(pool2_requri);
665 pool_flush2(pool2_capture);
Willy Tarreau87b09662015-04-03 00:22:06 +0200666 pool_flush2(pool2_stream);
Willy Tarreau3a5e0602014-11-13 16:46:28 +0100667 pool_flush2(pool2_connection);
668 pool_flush2(pool2_pendconn);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200669 pool_flush2(fe->req_cap_pool);
670 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200671 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200672}
673
Willy Tarreau78955f42014-12-28 13:09:02 +0100674/* Allocates a receive buffer for channel <chn>, but only if it's guaranteed
675 * that it's not the last available buffer or it's the response buffer. Unless
676 * the buffer is the response buffer, an extra control is made so that we always
677 * keep <tune.buffers.reserved> buffers available after this allocation. To be
678 * called at the beginning of recv() callbacks to ensure that the required
679 * buffers are properly allocated. Returns 0 in case of failure, non-zero
680 * otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100681 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200682int stream_alloc_recv_buffer(struct channel *chn)
Willy Tarreau656859d2014-11-25 19:46:36 +0100683{
Willy Tarreau87b09662015-04-03 00:22:06 +0200684 struct stream *s;
Willy Tarreau656859d2014-11-25 19:46:36 +0100685 struct buffer *b;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100686 int margin = 0;
Willy Tarreau656859d2014-11-25 19:46:36 +0100687
Willy Tarreau78955f42014-12-28 13:09:02 +0100688 if (!(chn->flags & CF_ISRESP))
Willy Tarreaua24adf02014-11-27 01:11:56 +0100689 margin = global.tune.reserved_bufs;
690
Willy Tarreau78955f42014-12-28 13:09:02 +0100691 s = chn_sess(chn);
692
693 b = b_alloc_margin(&chn->buf, margin);
Willy Tarreau656859d2014-11-25 19:46:36 +0100694 if (b)
695 return 1;
696
Willy Tarreaubf883e02014-11-25 21:10:35 +0100697 if (LIST_ISEMPTY(&s->buffer_wait))
698 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100699 return 0;
700}
701
Willy Tarreau87b09662015-04-03 00:22:06 +0200702/* Allocates a work buffer for stream <s>. It is meant to be called inside
703 * process_stream(). It will only allocate the side needed for the function
Willy Tarreaua24adf02014-11-27 01:11:56 +0100704 * to work fine. For a regular connection, only the response is needed so that
705 * an error message may be built and returned. In case where the initiator is
706 * an applet (eg: peers), then we need to allocate the request buffer for the
707 * applet to be able to send its data (in this case a response is not needed).
708 * Request buffers are never picked from the reserved pool, but response
709 * buffers may be allocated from the reserve. This is critical to ensure that
710 * a response may always flow and will never block a server from releasing a
711 * connection. Returns 0 in case of failure, non-zero otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100712 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200713int stream_alloc_work_buffer(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100714{
Willy Tarreaua24adf02014-11-27 01:11:56 +0100715 int margin;
716 struct buffer **buf;
717
718 if (objt_appctx(s->si[0].end)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100719 buf = &s->req.buf;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100720 margin = global.tune.reserved_bufs;
721 }
722 else {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100723 buf = &s->res.buf;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100724 margin = 0;
725 }
726
Willy Tarreaubf883e02014-11-25 21:10:35 +0100727 if (!LIST_ISEMPTY(&s->buffer_wait)) {
728 LIST_DEL(&s->buffer_wait);
729 LIST_INIT(&s->buffer_wait);
730 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100731
Willy Tarreaua24adf02014-11-27 01:11:56 +0100732 if (b_alloc_margin(buf, margin))
Willy Tarreau656859d2014-11-25 19:46:36 +0100733 return 1;
734
Willy Tarreaubf883e02014-11-25 21:10:35 +0100735 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100736 return 0;
737}
738
739/* releases unused buffers after processing. Typically used at the end of the
Willy Tarreaubf883e02014-11-25 21:10:35 +0100740 * update() functions. It will try to wake up as many tasks as the number of
Willy Tarreau87b09662015-04-03 00:22:06 +0200741 * buffers that it releases. In practice, most often streams are blocked on
Willy Tarreaubf883e02014-11-25 21:10:35 +0100742 * a single buffer, so it makes sense to try to wake two up when two buffers
743 * are released at once.
Willy Tarreau656859d2014-11-25 19:46:36 +0100744 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200745void stream_release_buffers(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100746{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100747 if (s->req.buf->size && buffer_empty(s->req.buf))
748 b_free(&s->req.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100749
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100750 if (s->res.buf->size && buffer_empty(s->res.buf))
751 b_free(&s->res.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100752
Willy Tarreaubf883e02014-11-25 21:10:35 +0100753 /* if we're certain to have at least 1 buffer available, and there is
754 * someone waiting, we can wake up a waiter and offer them.
755 */
Willy Tarreaua24adf02014-11-27 01:11:56 +0100756 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200757 stream_offer_buffers();
Willy Tarreaubf883e02014-11-25 21:10:35 +0100758}
759
Willy Tarreau87b09662015-04-03 00:22:06 +0200760/* Runs across the list of pending streams waiting for a buffer and wakes one
Willy Tarreaua24adf02014-11-27 01:11:56 +0100761 * up if buffers are available. Will stop when the run queue reaches <rqlimit>.
Willy Tarreau87b09662015-04-03 00:22:06 +0200762 * Should not be called directly, use stream_offer_buffers() instead.
Willy Tarreaubf883e02014-11-25 21:10:35 +0100763 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200764void __stream_offer_buffers(int rqlimit)
Willy Tarreaubf883e02014-11-25 21:10:35 +0100765{
Willy Tarreau87b09662015-04-03 00:22:06 +0200766 struct stream *sess, *bak;
Willy Tarreaubf883e02014-11-25 21:10:35 +0100767
768 list_for_each_entry_safe(sess, bak, &buffer_wq, buffer_wait) {
Willy Tarreaua24adf02014-11-27 01:11:56 +0100769 if (rqlimit <= run_queue)
770 break;
771
Willy Tarreaubf883e02014-11-25 21:10:35 +0100772 if (sess->task->state & TASK_RUNNING)
773 continue;
774
775 LIST_DEL(&sess->buffer_wait);
776 LIST_INIT(&sess->buffer_wait);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100777 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100778 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100779}
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200780
781/* perform minimal intializations, report 0 in case of error, 1 if OK. */
Willy Tarreau87b09662015-04-03 00:22:06 +0200782int init_stream()
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200783{
Willy Tarreau87b09662015-04-03 00:22:06 +0200784 LIST_INIT(&streams);
785 pool2_stream = create_pool("stream", sizeof(struct stream), MEM_F_SHARED);
786 return pool2_stream != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200787}
788
Willy Tarreau87b09662015-04-03 00:22:06 +0200789void stream_process_counters(struct stream *s)
Willy Tarreau30e71012007-11-26 20:15:35 +0100790{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100791 unsigned long long bytes;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100792 void *ptr;
793 int i;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100794
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100795 bytes = s->req.total - s->logs.bytes_in;
796 s->logs.bytes_in = s->req.total;
797 if (bytes) {
798 s->fe->fe_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100799
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100800 s->be->be_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100801
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100802 if (objt_server(s->target))
803 objt_server(s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200804
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100805 if (s->listener && s->listener->counters)
806 s->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200807
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100808 for (i = 0; i < MAX_SESS_STKCTR; i++) {
809 if (!stkctr_entry(&s->stkctr[i]))
810 continue;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200811
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100812 ptr = stktable_data_ptr(s->stkctr[i].table,
813 stkctr_entry(&s->stkctr[i]),
814 STKTABLE_DT_BYTES_IN_CNT);
815 if (ptr)
816 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200817
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100818 ptr = stktable_data_ptr(s->stkctr[i].table,
819 stkctr_entry(&s->stkctr[i]),
820 STKTABLE_DT_BYTES_IN_RATE);
821 if (ptr)
822 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
823 s->stkctr[i].table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreau30e71012007-11-26 20:15:35 +0100824 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100825 }
826
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100827 bytes = s->res.total - s->logs.bytes_out;
828 s->logs.bytes_out = s->res.total;
829 if (bytes) {
830 s->fe->fe_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100831
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100832 s->be->be_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100833
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100834 if (objt_server(s->target))
835 objt_server(s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200836
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100837 if (s->listener && s->listener->counters)
838 s->listener->counters->bytes_out += bytes;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200839
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100840 for (i = 0; i < MAX_SESS_STKCTR; i++) {
841 if (!stkctr_entry(&s->stkctr[i]))
842 continue;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200843
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100844 ptr = stktable_data_ptr(s->stkctr[i].table,
845 stkctr_entry(&s->stkctr[i]),
846 STKTABLE_DT_BYTES_OUT_CNT);
847 if (ptr)
848 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200849
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100850 ptr = stktable_data_ptr(s->stkctr[i].table,
851 stkctr_entry(&s->stkctr[i]),
852 STKTABLE_DT_BYTES_OUT_RATE);
853 if (ptr)
854 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
855 s->stkctr[i].table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreau30e71012007-11-26 20:15:35 +0100856 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100857 }
858}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200859
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100860/* This function is called with (si->state == SI_ST_CON) meaning that a
861 * connection was attempted and that the file descriptor is already allocated.
862 * We must check for establishment, error and abort. Possible output states
863 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
864 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
Willy Tarreau87b09662015-04-03 00:22:06 +0200865 * otherwise 1. This only works with connection-based streams.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100866 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200867static int sess_update_st_con_tcp(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100868{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100869 struct stream_interface *si = &s->si[1];
870 struct channel *req = &s->req;
871 struct channel *rep = &s->res;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200872 struct connection *srv_conn = __objt_conn(si->end);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100873
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100874 /* If we got an error, or if nothing happened and the connection timed
875 * out, we must give up. The CER state handler will take care of retry
876 * attempts and error reports.
877 */
878 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau103197d2014-11-28 15:26:12 +0100879 if (unlikely(req->flags & CF_WRITE_PARTIAL)) {
Willy Tarreaue3224e82012-10-29 22:41:31 +0100880 /* Some data were sent past the connection establishment,
881 * so we need to pretend we're established to log correctly
882 * and let later states handle the failure.
883 */
Willy Tarreaue3224e82012-10-29 22:41:31 +0100884 si->state = SI_ST_EST;
885 si->err_type = SI_ET_DATA_ERR;
Willy Tarreau103197d2014-11-28 15:26:12 +0100886 rep->flags |= CF_READ_ERROR | CF_WRITE_ERROR;
Willy Tarreaue3224e82012-10-29 22:41:31 +0100887 return 1;
888 }
Willy Tarreau127334e2009-03-28 10:47:26 +0100889 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100890 si->state = SI_ST_CER;
Willy Tarreau0ede5a32012-12-08 08:44:02 +0100891
Willy Tarreauf79c8172013-10-21 16:30:56 +0200892 conn_force_close(srv_conn);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100893
894 if (si->err_type)
895 return 0;
896
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100897 if (si->flags & SI_FL_ERR)
898 si->err_type = SI_ET_CONN_ERR;
899 else
900 si->err_type = SI_ET_CONN_TO;
901 return 0;
902 }
903
904 /* OK, maybe we want to abort */
Willy Tarreaua7a7ebc2012-12-30 00:50:35 +0100905 if (!(req->flags & CF_WRITE_PARTIAL) &&
906 unlikely((rep->flags & CF_SHUTW) ||
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200907 ((req->flags & CF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
908 ((!(req->flags & CF_WRITE_ACTIVITY) && channel_is_empty(req)) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100909 s->be->options & PR_O_ABRT_CLOSE)))) {
910 /* give up */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200911 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100912 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau84455332009-03-15 22:34:05 +0100913 if (s->srv_error)
914 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100915 return 1;
916 }
917
918 /* we need to wait a bit more if there was no activity either */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200919 if (!(req->flags & CF_WRITE_ACTIVITY))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100920 return 1;
921
922 /* OK, this means that a connection succeeded. The caller will be
923 * responsible for handling the transition from CON to EST.
924 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100925 si->state = SI_ST_EST;
926 si->err_type = SI_ET_NONE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100927 return 1;
928}
929
930/* This function is called with (si->state == SI_ST_CER) meaning that a
931 * previous connection attempt has failed and that the file descriptor
932 * has already been released. Possible causes include asynchronous error
933 * notification and time out. Possible output states are SI_ST_CLO when
934 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
935 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
936 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
937 * marked as in error state. It returns 0.
938 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200939static int sess_update_st_cer(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100940{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100941 struct stream_interface *si = &s->si[1];
942
Willy Tarreau87b09662015-04-03 00:22:06 +0200943 /* we probably have to release last stream from the server */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100944 if (objt_server(s->target)) {
945 health_adjust(objt_server(s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100946
Willy Tarreaue7dff022015-04-03 01:14:29 +0200947 if (s->flags & SF_CURR_SESS) {
948 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100949 objt_server(s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100950 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100951 }
952
953 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200954 si->conn_retries--;
955 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100956 if (!si->err_type) {
957 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100958 }
959
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100960 if (objt_server(s->target))
961 objt_server(s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100962 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100963 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100964 if (may_dequeue_tasks(objt_server(s->target), s->be))
965 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100966
967 /* shutw is enough so stop a connecting socket */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200968 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100969 s->req.flags |= CF_WRITE_ERROR;
970 s->res.flags |= CF_READ_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100971
972 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100973 if (s->srv_error)
974 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100975 return 0;
976 }
977
978 /* If the "redispatch" option is set on the backend, we are allowed to
979 * retry on another server for the last retry. In order to achieve this,
Willy Tarreau87b09662015-04-03 00:22:06 +0200980 * we must mark the stream unassigned, and eventually clear the DIRECT
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100981 * bit to ignore any persistence cookie. We won't count a retry nor a
982 * redispatch yet, because this will depend on what server is selected.
Willy Tarreau33a14e52014-06-13 17:49:40 +0200983 * If the connection is not persistent, the balancing algorithm is not
984 * determinist (round robin) and there is more than one active server,
985 * we accept to perform an immediate redispatch without waiting since
986 * we don't care about this particular server.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100987 */
Willy Tarreau33a14e52014-06-13 17:49:40 +0200988 if (objt_server(s->target) &&
989 (si->conn_retries == 0 ||
Willy Tarreaue7dff022015-04-03 01:14:29 +0200990 (!(s->flags & SF_DIRECT) && s->be->srv_act > 1 &&
Willy Tarreau33a14e52014-06-13 17:49:40 +0200991 ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR))) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +0200992 s->be->options & PR_O_REDISP && !(s->flags & SF_FORCE_PRST)) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100993 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100994 if (may_dequeue_tasks(objt_server(s->target), s->be))
995 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100996
Willy Tarreaue7dff022015-04-03 01:14:29 +0200997 s->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100998 si->state = SI_ST_REQ;
999 } else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001000 if (objt_server(s->target))
1001 objt_server(s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001002 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001003 si->state = SI_ST_ASS;
1004 }
1005
1006 if (si->flags & SI_FL_ERR) {
1007 /* The error was an asynchronous connection error, and we will
1008 * likely have to retry connecting to the same server, most
1009 * likely leading to the same result. To avoid this, we wait
Willy Tarreaub0290662014-06-13 17:04:44 +02001010 * MIN(one second, connect timeout) before retrying.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001011 */
1012
Willy Tarreaub0290662014-06-13 17:04:44 +02001013 int delay = 1000;
1014
1015 if (s->be->timeout.connect && s->be->timeout.connect < delay)
1016 delay = s->be->timeout.connect;
1017
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001018 if (!si->err_type)
1019 si->err_type = SI_ET_CONN_ERR;
1020
Willy Tarreaudb6d0122014-06-13 17:40:15 +02001021 /* only wait when we're retrying on the same server */
1022 if (si->state == SI_ST_ASS ||
1023 (s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_RR ||
1024 (s->be->srv_act <= 1)) {
1025 si->state = SI_ST_TAR;
1026 si->exp = tick_add(now_ms, MS_TO_TICKS(delay));
1027 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001028 return 0;
1029 }
1030 return 0;
1031}
1032
1033/*
1034 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +02001035 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
Willy Tarreau26d8c592012-05-07 18:12:14 +02001036 * SI_ST_INI) to SI_ST_EST, but only when a ->proto is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001037 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001038static void sess_establish(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001039{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001040 struct stream_interface *si = &s->si[1];
1041 struct channel *req = &s->req;
1042 struct channel *rep = &s->res;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001043
Willy Tarreau0e37f1c2013-12-31 23:06:46 +01001044 /* First, centralize the timers information */
1045 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
1046 si->exp = TICK_ETERNITY;
1047
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001048 if (objt_server(s->target))
1049 health_adjust(objt_server(s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001050
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001051 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001052 /* if the user wants to log as soon as possible, without counting
1053 * bytes from the server, then this is the right moment. */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001054 if (!LIST_ISEMPTY(&s->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001055 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001056 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001057 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001058 }
1059 else {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001060 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
Willy Tarreaud81ca042013-12-31 22:33:13 +01001061 rep->flags |= CF_READ_DONTWAIT; /* a single read is enough to get response headers */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001062 }
1063
Willy Tarreau4e5b8282009-08-16 22:57:50 +02001064 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001065 rep->flags |= CF_READ_ATTACHED; /* producer is now attached */
Willy Tarreau644c1012014-04-30 18:11:11 +02001066 if (req->flags & CF_WAKE_CONNECT) {
1067 req->flags |= CF_WAKE_ONCE;
1068 req->flags &= ~CF_WAKE_CONNECT;
1069 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001070 if (objt_conn(si->end)) {
Willy Tarreaud04e8582010-05-31 12:31:35 +02001071 /* real connections have timeouts */
1072 req->wto = s->be->timeout.server;
1073 rep->rto = s->be->timeout.server;
1074 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001075 req->wex = TICK_ETERNITY;
1076}
1077
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001078/* Update back stream interface status for input states SI_ST_ASS, SI_ST_QUE,
1079 * SI_ST_TAR. Other input states are simply ignored.
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01001080 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON
1081 * and SI_ST_EST. Flags must have previously been updated for timeouts and other
1082 * conditions.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001083 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001084static void sess_update_stream_int(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001085{
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001086 struct server *srv = objt_server(s->target);
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001087 struct stream_interface *si = &s->si[1];
Willy Tarreau103197d2014-11-28 15:26:12 +01001088 struct channel *req = &s->req;
Willy Tarreau827aee92011-03-10 16:55:02 +01001089
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001090 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 +01001091 now_ms, __FUNCTION__,
1092 s,
Willy Tarreau103197d2014-11-28 15:26:12 +01001093 req, s->rep,
1094 req->rex, s->res.wex,
1095 req->flags, s->res.flags,
1096 req->buf->i, req->buf->o, s->res.buf->i, s->res.buf->o, s->si[0].state, req->cons->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001097
1098 if (si->state == SI_ST_ASS) {
1099 /* Server assigned to connection request, we have to try to connect now */
1100 int conn_err;
1101
1102 conn_err = connect_server(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001103 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001104
Willy Tarreaue7dff022015-04-03 01:14:29 +02001105 if (conn_err == SF_ERR_NONE) {
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01001106 /* state = SI_ST_CON or SI_ST_EST now */
Willy Tarreau827aee92011-03-10 16:55:02 +01001107 if (srv)
1108 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001109 if (srv)
1110 srv_set_sess_last(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001111 return;
1112 }
1113
1114 /* We have received a synchronous error. We might have to
1115 * abort, retry immediately or redispatch.
1116 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001117 if (conn_err == SF_ERR_INTERNAL) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001118 if (!si->err_type) {
1119 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001120 }
1121
Willy Tarreau827aee92011-03-10 16:55:02 +01001122 if (srv)
1123 srv_inc_sess_ctr(srv);
1124 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001125 srv_set_sess_last(srv);
1126 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +01001127 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001128 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001129
Willy Tarreau87b09662015-04-03 00:22:06 +02001130 /* release other streams waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +01001131 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001132 if (may_dequeue_tasks(srv, s->be))
1133 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001134
1135 /* Failed and not retryable. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001136 si_shutr(si);
1137 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001138 req->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001139
1140 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1141
Willy Tarreau87b09662015-04-03 00:22:06 +02001142 /* no stream was ever accounted for this server */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001143 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001144 if (s->srv_error)
1145 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001146 return;
1147 }
1148
1149 /* We are facing a retryable error, but we don't want to run a
1150 * turn-around now, as the problem is likely a source port
1151 * allocation problem, so we want to retry now.
1152 */
1153 si->state = SI_ST_CER;
1154 si->flags &= ~SI_FL_ERR;
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001155 sess_update_st_cer(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001156 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
1157 return;
1158 }
1159 else if (si->state == SI_ST_QUE) {
1160 /* connection request was queued, check for any update */
1161 if (!s->pend_pos) {
1162 /* The connection is not in the queue anymore. Either
1163 * we have a server connection slot available and we
1164 * go directly to the assigned state, or we need to
1165 * load-balance first and go to the INI state.
1166 */
1167 si->exp = TICK_ETERNITY;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001168 if (unlikely(!(s->flags & SF_ASSIGNED)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001169 si->state = SI_ST_REQ;
1170 else {
1171 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1172 si->state = SI_ST_ASS;
1173 }
1174 return;
1175 }
1176
1177 /* Connection request still in queue... */
1178 if (si->flags & SI_FL_EXP) {
1179 /* ... and timeout expired */
1180 si->exp = TICK_ETERNITY;
1181 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +01001182 if (srv)
1183 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001184 s->be->be_counters.failed_conns++;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001185 si_shutr(si);
1186 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001187 req->flags |= CF_WRITE_TIMEOUT;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001188 if (!si->err_type)
1189 si->err_type = SI_ET_QUEUE_TO;
1190 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001191 if (s->srv_error)
1192 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001193 return;
1194 }
1195
1196 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau103197d2014-11-28 15:26:12 +01001197 if ((req->flags & (CF_READ_ERROR)) ||
1198 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
1199 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001200 /* give up */
1201 si->exp = TICK_ETERNITY;
1202 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau73b013b2012-05-21 16:31:45 +02001203 si_shutr(si);
1204 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001205 si->err_type |= SI_ET_QUEUE_ABRT;
1206 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001207 if (s->srv_error)
1208 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001209 return;
1210 }
1211
1212 /* Nothing changed */
1213 return;
1214 }
1215 else if (si->state == SI_ST_TAR) {
1216 /* Connection request might be aborted */
Willy Tarreau103197d2014-11-28 15:26:12 +01001217 if ((req->flags & (CF_READ_ERROR)) ||
1218 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
1219 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001220 /* give up */
1221 si->exp = TICK_ETERNITY;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001222 si_shutr(si);
1223 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001224 si->err_type |= SI_ET_CONN_ABRT;
1225 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001226 if (s->srv_error)
1227 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001228 return;
1229 }
1230
1231 if (!(si->flags & SI_FL_EXP))
1232 return; /* still in turn-around */
1233
1234 si->exp = TICK_ETERNITY;
1235
Willy Tarreau87b09662015-04-03 00:22:06 +02001236 /* we keep trying on the same server as long as the stream is
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001237 * marked "assigned".
1238 * FIXME: Should we force a redispatch attempt when the server is down ?
1239 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001240 if (s->flags & SF_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001241 si->state = SI_ST_ASS;
1242 else
1243 si->state = SI_ST_REQ;
1244 return;
1245 }
1246}
1247
Willy Tarreau87b09662015-04-03 00:22:06 +02001248/* Set correct stream termination flags in case no analyser has done it. It
Simon Hormandec5be42011-06-08 09:19:07 +09001249 * also counts a failed request if the server state has not reached the request
1250 * stage.
1251 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001252static void sess_set_term_flags(struct stream *s)
Simon Hormandec5be42011-06-08 09:19:07 +09001253{
Willy Tarreaue7dff022015-04-03 01:14:29 +02001254 if (!(s->flags & SF_FINST_MASK)) {
Simon Hormandec5be42011-06-08 09:19:07 +09001255 if (s->si[1].state < SI_ST_REQ) {
1256
1257 s->fe->fe_counters.failed_req++;
1258 if (s->listener->counters)
1259 s->listener->counters->failed_req++;
1260
Willy Tarreaue7dff022015-04-03 01:14:29 +02001261 s->flags |= SF_FINST_R;
Simon Hormandec5be42011-06-08 09:19:07 +09001262 }
1263 else if (s->si[1].state == SI_ST_QUE)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001264 s->flags |= SF_FINST_Q;
Simon Hormandec5be42011-06-08 09:19:07 +09001265 else if (s->si[1].state < SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001266 s->flags |= SF_FINST_C;
Simon Hormandec5be42011-06-08 09:19:07 +09001267 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001268 s->flags |= SF_FINST_D;
Simon Hormandec5be42011-06-08 09:19:07 +09001269 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02001270 s->flags |= SF_FINST_L;
Simon Hormandec5be42011-06-08 09:19:07 +09001271 }
1272}
1273
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001274/* This function initiates a server connection request on a stream interface
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001275 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS for
1276 * a real connection to a server, indicating that a server has been assigned,
1277 * or SI_ST_EST for a successful connection to an applet. It may also return
1278 * SI_ST_QUE, or SI_ST_CLO upon error.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001279 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001280static void sess_prepare_conn_req(struct stream *s)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001281{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001282 struct stream_interface *si = &s->si[1];
1283
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001284 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 +01001285 now_ms, __FUNCTION__,
1286 s,
1287 s->req, s->rep,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001288 s->req.rex, s->res.wex,
1289 s->req.flags, s->res.flags,
Willy Tarreau350f4872014-11-28 14:42:25 +01001290 s->req.buf->i, s->req.buf->o, s->res.buf->i, s->res.buf->o, s->si[0].state, s->req.cons->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001291
1292 if (si->state != SI_ST_REQ)
1293 return;
1294
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001295 if (unlikely(obj_type(s->target) == OBJ_TYPE_APPLET)) {
1296 /* the applet directly goes to the EST state */
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001297 struct appctx *appctx = objt_appctx(si->end);
1298
1299 if (!appctx || appctx->applet != __objt_applet(s->target))
1300 appctx = stream_int_register_handler(si, objt_applet(s->target));
1301
1302 if (!appctx) {
1303 /* No more memory, let's immediately abort. Force the
1304 * error code to ignore the ERR_LOCAL which is not a
1305 * real error.
1306 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001307 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001308
1309 si_shutr(si);
1310 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001311 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau6bbb2f62013-12-09 17:14:23 +01001312 si->err_type = SI_ET_CONN_RES;
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001313 si->state = SI_ST_CLO;
1314 if (s->srv_error)
1315 s->srv_error(s, si);
1316 return;
1317 }
1318
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001319 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001320 si->state = SI_ST_EST;
1321 si->err_type = SI_ET_NONE;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001322 be_set_sess_last(s->be);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01001323 /* let sess_establish() finish the job */
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001324 return;
1325 }
1326
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001327 /* Try to assign a server */
1328 if (srv_redispatch_connect(s) != 0) {
1329 /* We did not get a server. Either we queued the
1330 * connection request, or we encountered an error.
1331 */
1332 if (si->state == SI_ST_QUE)
1333 return;
1334
1335 /* we did not get any server, let's check the cause */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001336 si_shutr(si);
1337 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001338 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001339 if (!si->err_type)
1340 si->err_type = SI_ET_CONN_OTHER;
1341 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001342 if (s->srv_error)
1343 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001344 return;
1345 }
1346
1347 /* The server is assigned */
1348 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1349 si->state = SI_ST_ASS;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001350 be_set_sess_last(s->be);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001351}
1352
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001353/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +01001354 * if appropriate. The default_backend rule is also considered, then the
1355 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001356 * It returns 1 if the processing can continue on next analysers, or zero if it
1357 * either needs more data or wants to immediately abort the request.
1358 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001359static int process_switching_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001360{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001361 struct persist_rule *prst_rule;
Willy Tarreau4de91492010-01-22 19:10:05 +01001362
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001363 req->analysers &= ~an_bit;
1364 req->analyse_exp = TICK_ETERNITY;
1365
Willy Tarreau87b09662015-04-03 00:22:06 +02001366 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001367 now_ms, __FUNCTION__,
1368 s,
1369 req,
1370 req->rex, req->wex,
1371 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001372 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001373 req->analysers);
1374
1375 /* now check whether we have some switching rules for this request */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001376 if (!(s->flags & SF_BE_ASSIGNED)) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001377 struct switching_rule *rule;
1378
1379 list_for_each_entry(rule, &s->fe->switching_rules, list) {
Willy Tarreauf51658d2014-04-23 01:21:56 +02001380 int ret = 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001381
Willy Tarreauf51658d2014-04-23 01:21:56 +02001382 if (rule->cond) {
1383 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
1384 ret = acl_pass(ret);
1385 if (rule->cond->pol == ACL_COND_UNLESS)
1386 ret = !ret;
1387 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001388
1389 if (ret) {
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001390 /* If the backend name is dynamic, try to resolve the name.
1391 * If we can't resolve the name, or if any error occurs, break
1392 * the loop and fallback to the default backend.
1393 */
1394 struct proxy *backend;
1395
1396 if (rule->dynamic) {
1397 struct chunk *tmp = get_trash_chunk();
1398 if (!build_logline(s, tmp->str, tmp->size, &rule->be.expr))
1399 break;
1400 backend = findproxy(tmp->str, PR_CAP_BE);
1401 if (!backend)
1402 break;
1403 }
1404 else
1405 backend = rule->be.backend;
1406
Willy Tarreau87b09662015-04-03 00:22:06 +02001407 if (!stream_set_backend(s, backend))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001408 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001409 break;
1410 }
1411 }
1412
1413 /* To ensure correct connection accounting on the backend, we
1414 * have to assign one if it was not set (eg: a listen). This
1415 * measure also takes care of correctly setting the default
1416 * backend if any.
1417 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001418 if (!(s->flags & SF_BE_ASSIGNED))
Willy Tarreau87b09662015-04-03 00:22:06 +02001419 if (!stream_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001420 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001421 }
1422
Willy Tarreaufb356202010-08-03 14:02:05 +02001423 /* we don't want to run the TCP or HTTP filters again if the backend has not changed */
1424 if (s->fe == s->be) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001425 s->req.analysers &= ~AN_REQ_INSPECT_BE;
1426 s->req.analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001427 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001428
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001429 /* as soon as we know the backend, we must check if we have a matching forced or ignored
Willy Tarreau87b09662015-04-03 00:22:06 +02001430 * persistence rule, and report that in the stream.
Willy Tarreau4de91492010-01-22 19:10:05 +01001431 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001432 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001433 int ret = 1;
1434
1435 if (prst_rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001436 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 +01001437 ret = acl_pass(ret);
1438 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1439 ret = !ret;
1440 }
1441
1442 if (ret) {
1443 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001444 if (prst_rule->type == PERSIST_TYPE_FORCE) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001445 s->flags |= SF_FORCE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001446 } else {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001447 s->flags |= SF_IGNORE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001448 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001449 break;
1450 }
1451 }
1452
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001453 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001454
1455 sw_failed:
1456 /* immediately abort this request in case of allocation failure */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001457 channel_abort(&s->req);
1458 channel_abort(&s->res);
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001459
Willy Tarreaue7dff022015-04-03 01:14:29 +02001460 if (!(s->flags & SF_ERR_MASK))
1461 s->flags |= SF_ERR_RESOURCE;
1462 if (!(s->flags & SF_FINST_MASK))
1463 s->flags |= SF_FINST_R;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001464
1465 s->txn.status = 500;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001466 s->req.analysers = 0;
1467 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001468 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001469}
1470
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001471/* This stream analyser works on a request. It applies all use-server rules on
1472 * it then returns 1. The data must already be present in the buffer otherwise
1473 * they won't match. It always returns 1.
1474 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001475static int process_server_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001476{
1477 struct proxy *px = s->be;
1478 struct server_rule *rule;
1479
Willy Tarreau87b09662015-04-03 00:22:06 +02001480 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001481 now_ms, __FUNCTION__,
1482 s,
1483 req,
1484 req->rex, req->wex,
1485 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001486 req->buf->i + req->buf->o,
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001487 req->analysers);
1488
Willy Tarreaue7dff022015-04-03 01:14:29 +02001489 if (!(s->flags & SF_ASSIGNED)) {
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001490 list_for_each_entry(rule, &px->server_rules, list) {
1491 int ret;
1492
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001493 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 +02001494 ret = acl_pass(ret);
1495 if (rule->cond->pol == ACL_COND_UNLESS)
1496 ret = !ret;
1497
1498 if (ret) {
1499 struct server *srv = rule->srv.ptr;
1500
Willy Tarreau892337c2014-05-13 23:41:20 +02001501 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001502 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001503 (s->flags & SF_FORCE_PRST)) {
1504 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001505 s->target = &srv->obj_type;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001506 break;
1507 }
1508 /* if the server is not UP, let's go on with next rules
1509 * just in case another one is suited.
1510 */
1511 }
1512 }
1513 }
1514
1515 req->analysers &= ~an_bit;
1516 req->analyse_exp = TICK_ETERNITY;
1517 return 1;
1518}
1519
Emeric Brun1d33b292010-01-04 15:47:17 +01001520/* This stream analyser works on a request. It applies all sticking rules on
1521 * it then returns 1. The data must already be present in the buffer otherwise
1522 * they won't match. It always returns 1.
1523 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001524static int process_sticking_rules(struct stream *s, struct channel *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001525{
1526 struct proxy *px = s->be;
1527 struct sticking_rule *rule;
1528
Willy Tarreau87b09662015-04-03 00:22:06 +02001529 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Emeric Brun1d33b292010-01-04 15:47:17 +01001530 now_ms, __FUNCTION__,
1531 s,
1532 req,
1533 req->rex, req->wex,
1534 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001535 req->buf->i,
Emeric Brun1d33b292010-01-04 15:47:17 +01001536 req->analysers);
1537
1538 list_for_each_entry(rule, &px->sticking_rules, list) {
1539 int ret = 1 ;
1540 int i;
1541
Willy Tarreau9667a802013-12-09 12:52:13 +01001542 /* Only the first stick store-request of each table is applied
1543 * and other ones are ignored. The purpose is to allow complex
1544 * configurations which look for multiple entries by decreasing
1545 * order of precision and to stop at the first which matches.
1546 * An example could be a store of the IP address from an HTTP
1547 * header first, then from the source if not found.
1548 */
Emeric Brun1d33b292010-01-04 15:47:17 +01001549 for (i = 0; i < s->store_count; i++) {
1550 if (rule->table.t == s->store[i].table)
1551 break;
1552 }
1553
1554 if (i != s->store_count)
1555 continue;
1556
1557 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001558 ret = acl_exec_cond(rule->cond, px, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001559 ret = acl_pass(ret);
1560 if (rule->cond->pol == ACL_COND_UNLESS)
1561 ret = !ret;
1562 }
1563
1564 if (ret) {
1565 struct stktable_key *key;
1566
Willy Tarreaub5975de2014-06-25 16:20:53 +02001567 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 +01001568 if (!key)
1569 continue;
1570
1571 if (rule->flags & STK_IS_MATCH) {
1572 struct stksess *ts;
1573
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001574 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001575 if (!(s->flags & SF_ASSIGNED)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001576 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001577 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001578
1579 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001580 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1581 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001582 if (node) {
1583 struct server *srv;
1584
1585 srv = container_of(node, struct server, conf.id);
Willy Tarreau892337c2014-05-13 23:41:20 +02001586 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4de91492010-01-22 19:10:05 +01001587 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001588 (s->flags & SF_FORCE_PRST)) {
1589 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001590 s->target = &srv->obj_type;
Emeric Brun1d33b292010-01-04 15:47:17 +01001591 }
1592 }
1593 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001594 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001595 }
1596 }
1597 if (rule->flags & STK_IS_STORE) {
1598 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1599 struct stksess *ts;
1600
1601 ts = stksess_new(rule->table.t, key);
1602 if (ts) {
1603 s->store[s->store_count].table = rule->table.t;
1604 s->store[s->store_count++].ts = ts;
1605 }
1606 }
1607 }
1608 }
1609 }
1610
1611 req->analysers &= ~an_bit;
1612 req->analyse_exp = TICK_ETERNITY;
1613 return 1;
1614}
1615
1616/* This stream analyser works on a response. It applies all store rules on it
1617 * then returns 1. The data must already be present in the buffer otherwise
1618 * they won't match. It always returns 1.
1619 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001620static int process_store_rules(struct stream *s, struct channel *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001621{
1622 struct proxy *px = s->be;
1623 struct sticking_rule *rule;
1624 int i;
Willy Tarreau9667a802013-12-09 12:52:13 +01001625 int nbreq = s->store_count;
Emeric Brun1d33b292010-01-04 15:47:17 +01001626
Willy Tarreau87b09662015-04-03 00:22:06 +02001627 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Emeric Brun1d33b292010-01-04 15:47:17 +01001628 now_ms, __FUNCTION__,
1629 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001630 rep,
1631 rep->rex, rep->wex,
1632 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001633 rep->buf->i,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001634 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001635
1636 list_for_each_entry(rule, &px->storersp_rules, list) {
1637 int ret = 1 ;
Emeric Brun1d33b292010-01-04 15:47:17 +01001638
Willy Tarreau9667a802013-12-09 12:52:13 +01001639 /* Only the first stick store-response of each table is applied
1640 * and other ones are ignored. The purpose is to allow complex
1641 * configurations which look for multiple entries by decreasing
1642 * order of precision and to stop at the first which matches.
1643 * An example could be a store of a set-cookie value, with a
1644 * fallback to a parameter found in a 302 redirect.
1645 *
1646 * The store-response rules are not allowed to override the
1647 * store-request rules for the same table, but they may coexist.
1648 * Thus we can have up to one store-request entry and one store-
1649 * response entry for the same table at any time.
1650 */
1651 for (i = nbreq; i < s->store_count; i++) {
1652 if (rule->table.t == s->store[i].table)
1653 break;
1654 }
1655
1656 /* skip existing entries for this table */
1657 if (i < s->store_count)
1658 continue;
1659
Emeric Brun1d33b292010-01-04 15:47:17 +01001660 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001661 ret = acl_exec_cond(rule->cond, px, s, &s->txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001662 ret = acl_pass(ret);
1663 if (rule->cond->pol == ACL_COND_UNLESS)
1664 ret = !ret;
1665 }
1666
1667 if (ret) {
1668 struct stktable_key *key;
1669
Willy Tarreaub5975de2014-06-25 16:20:53 +02001670 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 +01001671 if (!key)
1672 continue;
1673
Willy Tarreau37e340c2013-12-06 23:05:21 +01001674 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001675 struct stksess *ts;
1676
1677 ts = stksess_new(rule->table.t, key);
1678 if (ts) {
1679 s->store[s->store_count].table = rule->table.t;
Emeric Brun1d33b292010-01-04 15:47:17 +01001680 s->store[s->store_count++].ts = ts;
1681 }
1682 }
1683 }
1684 }
1685
1686 /* process store request and store response */
1687 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001688 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001689 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001690
Willy Tarreauc93cd162014-05-13 15:54:22 +02001691 if (objt_server(s->target) && objt_server(s->target)->flags & SRV_F_NON_STICK) {
Simon Hormanfa461682011-06-25 09:39:49 +09001692 stksess_free(s->store[i].table, s->store[i].ts);
1693 s->store[i].ts = NULL;
1694 continue;
1695 }
1696
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001697 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1698 if (ts) {
1699 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001700 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001701 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001702 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001703 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001704 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001705
1706 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001707 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001708 stktable_data_cast(ptr, server_id) = objt_server(s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001709 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001710 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001711
1712 rep->analysers &= ~an_bit;
1713 rep->analyse_exp = TICK_ETERNITY;
1714 return 1;
1715}
1716
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001717/* This macro is very specific to the function below. See the comments in
Willy Tarreau87b09662015-04-03 00:22:06 +02001718 * process_stream() below to understand the logic and the tests.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001719 */
1720#define UPDATE_ANALYSERS(real, list, back, flag) { \
1721 list = (((list) & ~(flag)) | ~(back)) & (real); \
1722 back = real; \
1723 if (!(list)) \
1724 break; \
1725 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1726 continue; \
1727}
1728
Willy Tarreau87b09662015-04-03 00:22:06 +02001729/* Processes the client, server, request and response jobs of a stream task,
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001730 * then puts it back to the wait queue in a clean state, or cleans up its
1731 * resources if it must be deleted. Returns in <next> the date the task wants
1732 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1733 * nothing too many times, the request and response buffers flags are monitored
1734 * and each function is called only if at least another function has changed at
1735 * least one flag it is interested in.
1736 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001737struct task *process_stream(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001738{
Willy Tarreau827aee92011-03-10 16:55:02 +01001739 struct server *srv;
Willy Tarreau87b09662015-04-03 00:22:06 +02001740 struct stream *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001741 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001742 unsigned int rq_prod_last, rq_cons_last;
1743 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001744 unsigned int req_ana_back;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001745 struct channel *req, *res;
1746 struct stream_interface *si_f, *si_b;
1747
1748 req = &s->req;
1749 res = &s->res;
1750
1751 si_f = &s->si[0];
1752 si_b = &s->si[1];
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001753
1754 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
Willy Tarreau8f128b42014-11-28 15:07:47 +01001755 // si_f->state, si_b->state, si_b->err_type, req->flags, res->flags);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001756
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001757 /* this data may be no longer valid, clear it */
1758 memset(&s->txn.auth, 0, sizeof(s->txn.auth));
1759
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02001760 /* This flag must explicitly be set every time */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001761 req->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
1762 res->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001763
1764 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001765 rqf_last = req->flags & ~CF_MASK_ANALYSER;
1766 rpf_last = res->flags & ~CF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001767
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001768 /* we don't want the stream interface functions to recursively wake us up */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001769 si_f->flags |= SI_FL_DONT_WAKE;
1770 si_b->flags |= SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001771
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001772 /* 1a: Check for low level timeouts if needed. We just set a flag on
1773 * stream interfaces when their timeouts have expired.
1774 */
1775 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001776 stream_int_check_timeouts(si_f);
1777 stream_int_check_timeouts(si_b);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001778
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001779 /* check channel timeouts, and close the corresponding stream interfaces
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001780 * for future reads or writes. Note: this will also concern upper layers
1781 * but we do not touch any other flag. We must be careful and correctly
1782 * detect state changes when calling them.
1783 */
1784
Willy Tarreau8f128b42014-11-28 15:07:47 +01001785 channel_check_timeouts(req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001786
Willy Tarreau8f128b42014-11-28 15:07:47 +01001787 if (unlikely((req->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1788 si_b->flags |= SI_FL_NOLINGER;
1789 si_shutw(si_b);
Willy Tarreau14641402009-12-29 14:49:56 +01001790 }
1791
Willy Tarreau8f128b42014-11-28 15:07:47 +01001792 if (unlikely((req->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1793 if (si_f->flags & SI_FL_NOHALF)
1794 si_f->flags |= SI_FL_NOLINGER;
1795 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001796 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001797
Willy Tarreau8f128b42014-11-28 15:07:47 +01001798 channel_check_timeouts(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001799
Willy Tarreau8f128b42014-11-28 15:07:47 +01001800 if (unlikely((res->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1801 si_f->flags |= SI_FL_NOLINGER;
1802 si_shutw(si_f);
Willy Tarreau14641402009-12-29 14:49:56 +01001803 }
1804
Willy Tarreau8f128b42014-11-28 15:07:47 +01001805 if (unlikely((res->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1806 if (si_b->flags & SI_FL_NOHALF)
1807 si_b->flags |= SI_FL_NOLINGER;
1808 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001809 }
Willy Tarreau798f4322012-11-08 14:49:17 +01001810
1811 /* Once in a while we're woken up because the task expires. But
1812 * this does not necessarily mean that a timeout has been reached.
Willy Tarreau87b09662015-04-03 00:22:06 +02001813 * So let's not run a whole stream processing if only an expiration
Willy Tarreau798f4322012-11-08 14:49:17 +01001814 * timeout needs to be refreshed.
1815 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001816 if (!((req->flags | res->flags) &
Willy Tarreau798f4322012-11-08 14:49:17 +01001817 (CF_SHUTR|CF_READ_ACTIVITY|CF_READ_TIMEOUT|CF_SHUTW|
1818 CF_WRITE_ACTIVITY|CF_WRITE_TIMEOUT|CF_ANA_TIMEOUT)) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01001819 !((si_f->flags | si_b->flags) & (SI_FL_EXP|SI_FL_ERR)) &&
Willy Tarreau798f4322012-11-08 14:49:17 +01001820 ((t->state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER))
1821 goto update_exp_and_leave;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001822 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001823
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001824 /* below we may emit error messages so we have to ensure that we have
1825 * our buffers properly allocated.
1826 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001827 if (!stream_alloc_work_buffer(s)) {
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001828 /* No buffer available, we've been subscribed to the list of
1829 * buffer waiters, let's wait for our turn.
1830 */
1831 goto update_exp_and_leave;
1832 }
1833
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001834 /* 1b: check for low-level errors reported at the stream interface.
1835 * First we check if it's a retryable error (in which case we don't
1836 * want to tell the buffer). Otherwise we report the error one level
1837 * upper by setting flags into the buffers. Note that the side towards
1838 * the client cannot have connect (hence retryable) errors. Also, the
1839 * connection setup code must be able to deal with any type of abort.
1840 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001841 srv = objt_server(s->target);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001842 if (unlikely(si_f->flags & SI_FL_ERR)) {
1843 if (si_f->state == SI_ST_EST || si_f->state == SI_ST_DIS) {
1844 si_shutr(si_f);
1845 si_shutw(si_f);
1846 stream_int_report_error(si_f);
1847 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001848 s->be->be_counters.cli_aborts++;
1849 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001850 if (srv)
1851 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001852 if (!(s->flags & SF_ERR_MASK))
1853 s->flags |= SF_ERR_CLICL;
1854 if (!(s->flags & SF_FINST_MASK))
1855 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001856 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001857 }
1858 }
1859
Willy Tarreau8f128b42014-11-28 15:07:47 +01001860 if (unlikely(si_b->flags & SI_FL_ERR)) {
1861 if (si_b->state == SI_ST_EST || si_b->state == SI_ST_DIS) {
1862 si_shutr(si_b);
1863 si_shutw(si_b);
1864 stream_int_report_error(si_b);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001865 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001866 if (srv)
1867 srv->counters.failed_resp++;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001868 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001869 s->be->be_counters.srv_aborts++;
1870 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001871 if (srv)
1872 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001873 if (!(s->flags & SF_ERR_MASK))
1874 s->flags |= SF_ERR_SRVCL;
1875 if (!(s->flags & SF_FINST_MASK))
1876 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001877 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001878 }
1879 /* note: maybe we should process connection errors here ? */
1880 }
1881
Willy Tarreau8f128b42014-11-28 15:07:47 +01001882 if (si_b->state == SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001883 /* we were trying to establish a connection on the server side,
1884 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1885 */
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001886 if (unlikely(!sess_update_st_con_tcp(s)))
1887 sess_update_st_cer(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001888 else if (si_b->state == SI_ST_EST)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001889 sess_establish(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001890
1891 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1892 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1893 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1894 */
1895 }
1896
Willy Tarreau8f128b42014-11-28 15:07:47 +01001897 rq_prod_last = si_f->state;
1898 rq_cons_last = si_b->state;
1899 rp_cons_last = si_f->state;
1900 rp_prod_last = si_b->state;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001901
1902 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001903 /* Check for connection closure */
1904
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001905 DPRINTF(stderr,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001906 "[%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 +01001907 now_ms, __FUNCTION__, __LINE__,
1908 t,
1909 s, s->flags,
Willy Tarreau8f128b42014-11-28 15:07:47 +01001910 req, res,
1911 req->rex, res->wex,
1912 req->flags, res->flags,
1913 req->buf->i, req->buf->o, res->buf->i, res->buf->o, si_f->state, si_b->state,
1914 si_f->err_type, si_b->err_type,
1915 si_b->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001916
1917 /* nothing special to be done on client side */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001918 if (unlikely(si_f->state == SI_ST_DIS))
1919 si_f->state = SI_ST_CLO;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001920
1921 /* When a server-side connection is released, we have to count it and
1922 * check for pending connections on this server.
1923 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001924 if (unlikely(si_b->state == SI_ST_DIS)) {
1925 si_b->state = SI_ST_CLO;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001926 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001927 if (srv) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001928 if (s->flags & SF_CURR_SESS) {
1929 s->flags &= ~SF_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001930 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001931 }
1932 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001933 if (may_dequeue_tasks(srv, s->be))
1934 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001935 }
1936 }
1937
1938 /*
1939 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1940 * at this point.
1941 */
1942
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001943 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001944 /* Analyse request */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001945 if (((req->flags & ~rqf_last) & CF_MASK_ANALYSER) ||
1946 ((req->flags ^ rqf_last) & CF_MASK_STATIC) ||
1947 si_f->state != rq_prod_last ||
1948 si_b->state != rq_cons_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001949 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001950 unsigned int flags = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001951
Willy Tarreau8f128b42014-11-28 15:07:47 +01001952 if (si_f->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001953 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001954 unsigned int ana_list;
1955 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001956
Willy Tarreau90deb182010-01-07 00:20:41 +01001957 /* it's up to the analysers to stop new connections,
1958 * disable reading or closing. Note: if an analyser
1959 * disables any of these bits, it is responsible for
1960 * enabling them again when it disables itself, so
1961 * that other analysers are called in similar conditions.
1962 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001963 channel_auto_read(req);
1964 channel_auto_connect(req);
1965 channel_auto_close(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001966
1967 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001968 * req->analysers, following the bit order from LSB
Willy Tarreauedcf6682008-11-30 23:15:34 +01001969 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001970 * the list when not needed. Any analyser may return 0
1971 * to break out of the loop, either because of missing
1972 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001973 * kill the stream. We loop at least once through each
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001974 * analyser, and we may loop again if other analysers
1975 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001976 *
1977 * We build a list of analysers to run. We evaluate all
1978 * of these analysers in the order of the lower bit to
1979 * the higher bit. This ordering is very important.
1980 * An analyser will often add/remove other analysers,
1981 * including itself. Any changes to itself have no effect
1982 * on the loop. If it removes any other analysers, we
1983 * want those analysers not to be called anymore during
1984 * this loop. If it adds an analyser that is located
1985 * after itself, we want it to be scheduled for being
1986 * processed during the loop. If it adds an analyser
1987 * which is located before it, we want it to switch to
1988 * it immediately, even if it has already been called
1989 * once but removed since.
1990 *
1991 * In order to achieve this, we compare the analyser
1992 * list after the call with a copy of it before the
1993 * call. The work list is fed with analyser bits that
1994 * appeared during the call. Then we compare previous
1995 * work list with the new one, and check the bits that
1996 * appeared. If the lowest of these bits is lower than
1997 * the current bit, it means we have enabled a previous
1998 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001999 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002000
Willy Tarreau8f128b42014-11-28 15:07:47 +01002001 ana_list = ana_back = req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01002002 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002003 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02002004
Willy Tarreaufb356202010-08-03 14:02:05 +02002005 if (ana_list & AN_REQ_INSPECT_FE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002006 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01002007 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002008 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02002009 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01002010
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002011 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002012 if (!http_wait_for_request(s, req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02002013 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002014 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02002015 }
2016
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002017 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002018 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_FE, s->fe))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002019 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002020 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002021 }
2022
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002023 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002024 if (!process_switching_rules(s, req, AN_REQ_SWITCHING_RULES))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002025 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002026 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002027 }
2028
Willy Tarreaufb356202010-08-03 14:02:05 +02002029 if (ana_list & AN_REQ_INSPECT_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002030 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_BE))
Willy Tarreaufb356202010-08-03 14:02:05 +02002031 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002032 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
Willy Tarreaufb356202010-08-03 14:02:05 +02002033 }
2034
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002035 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002036 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_BE, s->be))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002037 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002038 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002039 }
2040
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002041 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002042 if (!http_process_tarpit(s, req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01002043 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002044 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02002045 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01002046
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002047 if (ana_list & AN_REQ_SRV_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002048 if (!process_server_rules(s, req, AN_REQ_SRV_RULES))
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002049 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002050 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SRV_RULES);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002051 }
2052
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002053 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002054 if (!http_process_request(s, req, AN_REQ_HTTP_INNER))
Willy Tarreauc465fd72009-08-31 00:17:18 +02002055 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002056 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002057 }
2058
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002059 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002060 if (!http_wait_for_request_body(s, req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01002061 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002062 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02002063 }
Emeric Brun647caf12009-06-30 17:57:00 +02002064
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002065 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002066 if (!tcp_persist_rdp_cookie(s, req, AN_REQ_PRST_RDP_COOKIE))
Emeric Brun647caf12009-06-30 17:57:00 +02002067 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002068 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02002069 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002070
Emeric Brun1d33b292010-01-04 15:47:17 +01002071 if (ana_list & AN_REQ_STICKING_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002072 if (!process_sticking_rules(s, req, AN_REQ_STICKING_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01002073 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002074 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01002075 }
2076
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002077 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002078 if (!http_request_forward_body(s, req, AN_REQ_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01002079 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002080 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002081 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01002082 break;
2083 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002084 }
Willy Tarreau84455332009-03-15 22:34:05 +01002085
Willy Tarreau8f128b42014-11-28 15:07:47 +01002086 rq_prod_last = si_f->state;
2087 rq_cons_last = si_b->state;
2088 req->flags &= ~CF_WAKE_ONCE;
2089 rqf_last = req->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02002090
Willy Tarreau8f128b42014-11-28 15:07:47 +01002091 if ((req->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002092 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002093 }
2094
Willy Tarreau576507f2010-01-07 00:09:04 +01002095 /* we'll monitor the request analysers while parsing the response,
2096 * because some response analysers may indirectly enable new request
2097 * analysers (eg: HTTP keep-alive).
2098 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002099 req_ana_back = req->analysers;
Willy Tarreau576507f2010-01-07 00:09:04 +01002100
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002101 resync_response:
2102 /* Analyse response */
2103
Willy Tarreau8f128b42014-11-28 15:07:47 +01002104 if (((res->flags & ~rpf_last) & CF_MASK_ANALYSER) ||
2105 (res->flags ^ rpf_last) & CF_MASK_STATIC ||
2106 si_f->state != rp_cons_last ||
2107 si_b->state != rp_prod_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01002108 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002109 unsigned int flags = res->flags;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002110
Willy Tarreau8f128b42014-11-28 15:07:47 +01002111 if ((res->flags & CF_MASK_ANALYSER) &&
2112 (res->analysers & AN_REQ_ALL)) {
Willy Tarreau0499e352010-12-17 07:13:42 +01002113 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
2114 * for some free space in the response buffer, so we might need to call
2115 * it when something changes in the response buffer, but still we pass
2116 * it the request buffer. Note that the SI state might very well still
2117 * be zero due to us returning a flow of redirects!
2118 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002119 res->analysers &= ~AN_REQ_ALL;
2120 req->flags |= CF_WAKE_ONCE;
Willy Tarreau0499e352010-12-17 07:13:42 +01002121 }
2122
Willy Tarreau8f128b42014-11-28 15:07:47 +01002123 if (si_b->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01002124 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002125 unsigned int ana_list;
2126 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002127
Willy Tarreau90deb182010-01-07 00:20:41 +01002128 /* it's up to the analysers to stop disable reading or
2129 * closing. Note: if an analyser disables any of these
2130 * bits, it is responsible for enabling them again when
2131 * it disables itself, so that other analysers are called
2132 * in similar conditions.
2133 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002134 channel_auto_read(res);
2135 channel_auto_close(res);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002136
2137 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01002138 * res->analysers, following the bit order from LSB
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002139 * to MSB. The analysers must remove themselves from
2140 * the list when not needed. Any analyser may return 0
2141 * to break out of the loop, either because of missing
2142 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02002143 * kill the stream. We loop at least once through each
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002144 * analyser, and we may loop again if other analysers
2145 * are added in the middle.
2146 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002147
Willy Tarreau8f128b42014-11-28 15:07:47 +01002148 ana_list = ana_back = res->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01002149 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002150 /* Warning! ensure that analysers are always placed in ascending order! */
2151
Emeric Brun97679e72010-09-23 17:56:44 +02002152 if (ana_list & AN_RES_INSPECT) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002153 if (!tcp_inspect_response(s, res, AN_RES_INSPECT))
Emeric Brun97679e72010-09-23 17:56:44 +02002154 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002155 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_INSPECT);
Emeric Brun97679e72010-09-23 17:56:44 +02002156 }
2157
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002158 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002159 if (!http_wait_for_response(s, res, AN_RES_WAIT_HTTP))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002160 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002161 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002162 }
2163
Emeric Brun1d33b292010-01-04 15:47:17 +01002164 if (ana_list & AN_RES_STORE_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002165 if (!process_store_rules(s, res, AN_RES_STORE_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01002166 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002167 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01002168 }
2169
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002170 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002171 if (!http_process_res_common(s, res, AN_RES_HTTP_PROCESS_BE, s->be))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002172 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002173 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002174 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002175
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002176 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002177 if (!http_response_forward_body(s, res, AN_RES_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01002178 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002179 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002180 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01002181 break;
2182 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002183 }
2184
Willy Tarreau8f128b42014-11-28 15:07:47 +01002185 rp_cons_last = si_f->state;
2186 rp_prod_last = si_b->state;
2187 rpf_last = res->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02002188
Willy Tarreau8f128b42014-11-28 15:07:47 +01002189 if ((res->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002190 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002191 }
2192
Willy Tarreau576507f2010-01-07 00:09:04 +01002193 /* maybe someone has added some request analysers, so we must check and loop */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002194 if (req->analysers & ~req_ana_back)
Willy Tarreau576507f2010-01-07 00:09:04 +01002195 goto resync_request;
2196
Willy Tarreau8f128b42014-11-28 15:07:47 +01002197 if ((req->flags & ~rqf_last) & CF_MASK_ANALYSER)
Willy Tarreau0499e352010-12-17 07:13:42 +01002198 goto resync_request;
2199
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002200 /* FIXME: here we should call protocol handlers which rely on
2201 * both buffers.
2202 */
2203
2204
2205 /*
Willy Tarreau87b09662015-04-03 00:22:06 +02002206 * Now we propagate unhandled errors to the stream. Normally
Willy Tarreauae526782010-03-04 20:34:23 +01002207 * we're just in a data phase here since it means we have not
2208 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002209 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002210 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002211 if (unlikely(!(s->flags & SF_ERR_MASK))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002212 if (req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002213 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002214 req->analysers = 0;
2215 if (req->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002216 s->be->be_counters.cli_aborts++;
2217 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002218 if (srv)
2219 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002220 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01002221 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002222 else if (req->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002223 s->be->be_counters.cli_aborts++;
2224 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002225 if (srv)
2226 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002227 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01002228 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002229 else if (req->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002230 s->be->be_counters.srv_aborts++;
2231 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002232 if (srv)
2233 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002234 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01002235 }
2236 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002237 s->be->be_counters.srv_aborts++;
2238 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002239 if (srv)
2240 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002241 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01002242 }
Willy Tarreau84455332009-03-15 22:34:05 +01002243 sess_set_term_flags(s);
2244 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002245 else if (res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002246 /* Report it if the server got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002247 res->analysers = 0;
2248 if (res->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002249 s->be->be_counters.srv_aborts++;
2250 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002251 if (srv)
2252 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002253 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01002254 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002255 else if (res->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002256 s->be->be_counters.srv_aborts++;
2257 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002258 if (srv)
2259 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002260 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01002261 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002262 else if (res->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002263 s->be->be_counters.cli_aborts++;
2264 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002265 if (srv)
2266 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002267 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01002268 }
2269 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002270 s->be->be_counters.cli_aborts++;
2271 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002272 if (srv)
2273 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002274 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01002275 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002276 sess_set_term_flags(s);
2277 }
Willy Tarreau84455332009-03-15 22:34:05 +01002278 }
2279
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002280 /*
2281 * Here we take care of forwarding unhandled data. This also includes
2282 * connection establishments and shutdown requests.
2283 */
2284
2285
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002286 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002287 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002288 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002289 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002290 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002291 if (unlikely(!req->analysers &&
2292 !(req->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2293 (si_f->state >= SI_ST_EST) &&
2294 (req->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002295 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002296 * attached to it. If any data are left in, we'll permit them to
2297 * move.
2298 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002299 channel_auto_read(req);
2300 channel_auto_connect(req);
2301 channel_auto_close(req);
2302 buffer_flush(req->buf);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01002303
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002304 /* We'll let data flow between the producer (if still connected)
2305 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002306 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002307 if (!(req->flags & (CF_SHUTR|CF_SHUTW_NOW)))
2308 channel_forward(req, CHN_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002309 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002310
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002311 /* check if it is wise to enable kernel splicing to forward request data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002312 if (!(req->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2313 req->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002314 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002315 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->rcv_pipe) &&
2316 (objt_conn(si_b->end) && __objt_conn(si_b->end)->xprt && __objt_conn(si_b->end)->xprt->snd_pipe) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002317 (pipes_used < global.maxpipes) &&
2318 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
2319 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002320 (req->flags & CF_STREAMER_FAST)))) {
2321 req->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002322 }
2323
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002324 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002325 rqf_last = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002326
2327 /*
2328 * Now forward all shutdown requests between both sides of the buffer
2329 */
2330
Willy Tarreau520d95e2009-09-19 21:04:57 +02002331 /* first, let's check if the request buffer needs to shutdown(write), which may
2332 * happen either because the input is closed or because we want to force a close
Willy Tarreau05cdd962014-05-10 14:30:07 +02002333 * once the server has begun to respond. If a half-closed timeout is set, we adjust
2334 * the other side's timeout as well.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002335 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002336 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002337 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002338 channel_shutw_now(req);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002339 if (tick_isset(s->fe->timeout.clientfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002340 res->wto = s->fe->timeout.clientfin;
2341 res->wex = tick_add(now_ms, res->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002342 }
2343 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002344
2345 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002346 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2347 channel_is_empty(req))) {
2348 if (req->flags & CF_READ_ERROR)
2349 si_b->flags |= SI_FL_NOLINGER;
2350 si_shutw(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002351 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002352 res->rto = s->be->timeout.serverfin;
2353 res->rex = tick_add(now_ms, res->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002354 }
Willy Tarreau8615c2a2013-06-21 08:20:19 +02002355 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002356
2357 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002358 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW &&
2359 !req->analysers))
2360 channel_shutr_now(req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002361
2362 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002363 if (unlikely((req->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2364 if (si_f->flags & SI_FL_NOHALF)
2365 si_f->flags |= SI_FL_NOLINGER;
2366 si_shutr(si_f);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002367 if (tick_isset(s->fe->timeout.clientfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002368 res->wto = s->fe->timeout.clientfin;
2369 res->wex = tick_add(now_ms, res->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002370 }
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002371 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002372
Willy Tarreau520d95e2009-09-19 21:04:57 +02002373 /* it's possible that an upper layer has requested a connection setup or abort.
2374 * There are 2 situations where we decide to establish a new connection :
2375 * - there are data scheduled for emission in the buffer
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002376 * - the CF_AUTO_CONNECT flag is set (active connection)
Willy Tarreau520d95e2009-09-19 21:04:57 +02002377 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002378 if (si_b->state == SI_ST_INI) {
2379 if (!(req->flags & CF_SHUTW)) {
2380 if ((req->flags & CF_AUTO_CONNECT) || !channel_is_empty(req)) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002381 /* If we have an appctx, there is no connect method, so we
2382 * immediately switch to the connected state, otherwise we
2383 * perform a connection request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002384 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002385 si_b->state = SI_ST_REQ; /* new connection requested */
2386 si_b->conn_retries = s->be->conn_retries;
Willy Tarreau520d95e2009-09-19 21:04:57 +02002387 }
Willy Tarreau73201222009-08-16 18:27:24 +02002388 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002389 else {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002390 si_b->state = SI_ST_CLO; /* shutw+ini = abort */
2391 channel_shutw_now(req); /* fix buffer flags upon abort */
2392 channel_shutr_now(res);
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002393 }
Willy Tarreau92795622009-03-06 12:51:23 +01002394 }
2395
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002396
2397 /* we may have a pending connection request, or a connection waiting
2398 * for completion.
2399 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002400 if (si_b->state >= SI_ST_REQ && si_b->state < SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002401 do {
2402 /* nb: step 1 might switch from QUE to ASS, but we first want
2403 * to give a chance to step 2 to perform a redirect if needed.
2404 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002405 if (si_b->state != SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002406 sess_update_stream_int(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01002407 if (si_b->state == SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002408 sess_prepare_conn_req(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002409
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01002410 /* applets directly go to the ESTABLISHED state. Similarly,
2411 * servers experience the same fate when their connection
2412 * is reused.
2413 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002414 if (unlikely(si_b->state == SI_ST_EST))
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002415 sess_establish(s);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01002416
Willy Tarreaub44c8732013-12-31 23:16:50 +01002417 /* Now we can add the server name to a header (if requested) */
2418 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002419 if ((si_b->state >= SI_ST_CON) &&
Willy Tarreaub44c8732013-12-31 23:16:50 +01002420 (s->be->server_id_hdr_name != NULL) &&
2421 (s->be->mode == PR_MODE_HTTP) &&
2422 objt_server(s->target)) {
2423 http_send_name_header(&s->txn, s->be, objt_server(s->target)->id);
Willy Tarreau1e5dfda2013-04-07 18:19:16 +02002424 }
2425
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002426 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002427 if (si_b->state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SF_REDIRECTABLE))
Willy Tarreau8f128b42014-11-28 15:07:47 +01002428 http_perform_server_redirect(s, si_b);
2429 } while (si_b->state == SI_ST_ASS);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002430 }
2431
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002432 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002433 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002434 goto resync_stream_interface;
2435
Willy Tarreau815a9b22010-07-27 17:15:12 +02002436 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002437 if ((req->flags ^ rqf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002438 goto resync_request;
2439
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002440 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01002441
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002442 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002443 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002444 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002445 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002446 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002447 if (unlikely(!res->analysers &&
2448 !(res->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2449 (si_b->state >= SI_ST_EST) &&
2450 (res->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002451 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002452 * attached to it. If any data are left in, we'll permit them to
2453 * move.
2454 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002455 channel_auto_read(res);
2456 channel_auto_close(res);
2457 buffer_flush(res->buf);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002458
2459 /* We'll let data flow between the producer (if still connected)
2460 * to the consumer.
2461 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002462 if (!(res->flags & (CF_SHUTR|CF_SHUTW_NOW)))
2463 channel_forward(res, CHN_INFINITE_FORWARD);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002464
2465 /* if we have no analyser anymore in any direction and have a
Willy Tarreau05cdd962014-05-10 14:30:07 +02002466 * tunnel timeout set, use it now. Note that we must respect
2467 * the half-closed timeouts as well.
Willy Tarreauce887fd2012-05-12 12:50:00 +02002468 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002469 if (!req->analysers && s->be->timeout.tunnel) {
2470 req->rto = req->wto = res->rto = res->wto =
Willy Tarreauce887fd2012-05-12 12:50:00 +02002471 s->be->timeout.tunnel;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002472
Willy Tarreau8f128b42014-11-28 15:07:47 +01002473 if ((req->flags & CF_SHUTR) && tick_isset(s->fe->timeout.clientfin))
2474 res->wto = s->fe->timeout.clientfin;
2475 if ((req->flags & CF_SHUTW) && tick_isset(s->be->timeout.serverfin))
2476 res->rto = s->be->timeout.serverfin;
2477 if ((res->flags & CF_SHUTR) && tick_isset(s->be->timeout.serverfin))
2478 req->wto = s->be->timeout.serverfin;
2479 if ((res->flags & CF_SHUTW) && tick_isset(s->fe->timeout.clientfin))
2480 req->rto = s->fe->timeout.clientfin;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002481
Willy Tarreau8f128b42014-11-28 15:07:47 +01002482 req->rex = tick_add(now_ms, req->rto);
2483 req->wex = tick_add(now_ms, req->wto);
2484 res->rex = tick_add(now_ms, res->rto);
2485 res->wex = tick_add(now_ms, res->wto);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002486 }
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002487 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002488
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002489 /* check if it is wise to enable kernel splicing to forward response data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002490 if (!(res->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2491 res->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002492 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002493 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->snd_pipe) &&
2494 (objt_conn(si_b->end) && __objt_conn(si_b->end)->xprt && __objt_conn(si_b->end)->xprt->rcv_pipe) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002495 (pipes_used < global.maxpipes) &&
2496 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2497 (((s->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002498 (res->flags & CF_STREAMER_FAST)))) {
2499 res->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002500 }
2501
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002502 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002503 rpf_last = res->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002504
2505 /*
2506 * Now forward all shutdown requests between both sides of the buffer
2507 */
2508
2509 /*
2510 * FIXME: this is probably where we should produce error responses.
2511 */
2512
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002513 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002514 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002515 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002516 channel_shutw_now(res);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002517 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002518 req->wto = s->be->timeout.serverfin;
2519 req->wex = tick_add(now_ms, req->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002520 }
2521 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002522
2523 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002524 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2525 channel_is_empty(res))) {
2526 si_shutw(si_f);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002527 if (tick_isset(s->fe->timeout.clientfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002528 req->rto = s->fe->timeout.clientfin;
2529 req->rex = tick_add(now_ms, req->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002530 }
2531 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002532
2533 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002534 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW) &&
2535 !res->analysers)
2536 channel_shutr_now(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002537
2538 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002539 if (unlikely((res->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2540 if (si_b->flags & SI_FL_NOHALF)
2541 si_b->flags |= SI_FL_NOLINGER;
2542 si_shutr(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002543 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002544 req->wto = s->be->timeout.serverfin;
2545 req->wex = tick_add(now_ms, req->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002546 }
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002547 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002548
Willy Tarreau8f128b42014-11-28 15:07:47 +01002549 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002550 goto resync_stream_interface;
2551
Willy Tarreau8f128b42014-11-28 15:07:47 +01002552 if (req->flags != rqf_last)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002553 goto resync_request;
2554
Willy Tarreau8f128b42014-11-28 15:07:47 +01002555 if ((res->flags ^ rpf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002556 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002557
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002558 /* we're interested in getting wakeups again */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002559 si_f->flags &= ~SI_FL_DONT_WAKE;
2560 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002561
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002562 /* This is needed only when debugging is enabled, to indicate
2563 * client-side or server-side close. Please note that in the unlikely
2564 * event where both sides would close at once, the sequence is reported
2565 * on the server side first.
2566 */
2567 if (unlikely((global.mode & MODE_DEBUG) &&
2568 (!(global.mode & MODE_QUIET) ||
2569 (global.mode & MODE_VERBOSE)))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002570 if (si_b->state == SI_ST_CLO &&
2571 si_b->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002572 chunk_printf(&trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002573 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002574 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2575 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002576 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002577 }
2578
Willy Tarreau8f128b42014-11-28 15:07:47 +01002579 if (si_f->state == SI_ST_CLO &&
2580 si_f->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002581 chunk_printf(&trash, "%08x:%s.clicls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002582 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002583 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2584 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002585 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002586 }
2587 }
2588
Willy Tarreau8f128b42014-11-28 15:07:47 +01002589 if (likely((si_f->state != SI_ST_CLO) ||
2590 (si_b->state > SI_ST_INI && si_b->state < SI_ST_CLO))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002591
Willy Tarreaue7dff022015-04-03 01:14:29 +02002592 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SF_BE_ASSIGNED))
Willy Tarreau87b09662015-04-03 00:22:06 +02002593 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002594
Willy Tarreau8f128b42014-11-28 15:07:47 +01002595 if (si_f->state == SI_ST_EST && obj_type(si_f->end) != OBJ_TYPE_APPCTX)
2596 si_update(si_f);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002597
Willy Tarreau8f128b42014-11-28 15:07:47 +01002598 if (si_b->state == SI_ST_EST && obj_type(si_b->end) != OBJ_TYPE_APPCTX)
2599 si_update(si_b);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002600
Willy Tarreau8f128b42014-11-28 15:07:47 +01002601 req->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2602 res->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2603 si_f->prev_state = si_f->state;
2604 si_b->prev_state = si_b->state;
2605 si_f->flags &= ~(SI_FL_ERR|SI_FL_EXP);
2606 si_b->flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002607
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002608 /* Trick: if a request is being waiting for the server to respond,
2609 * and if we know the server can timeout, we don't want the timeout
2610 * to expire on the client side first, but we're still interested
2611 * in passing data from the client to the server (eg: POST). Thus,
2612 * we can cancel the client's request timeout if the server's
2613 * request timeout is set and the server has not yet sent a response.
2614 */
2615
Willy Tarreau8f128b42014-11-28 15:07:47 +01002616 if ((res->flags & (CF_AUTO_CLOSE|CF_SHUTR)) == 0 &&
2617 (tick_isset(req->wex) || tick_isset(res->rex))) {
2618 req->flags |= CF_READ_NOEXP;
2619 req->rex = TICK_ETERNITY;
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002620 }
2621
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002622 /* When any of the stream interfaces is attached to an applet,
2623 * we have to call it here. Note that this one may wake the
2624 * task up again. If at least one applet was called, the current
2625 * task might have been woken up, in which case we don't want it
2626 * to be requeued to the wait queue but rather to the run queue
2627 * to run ASAP. The bitwise "or" in the condition ensures that
2628 * both functions are always called and that we wake up if at
2629 * least one did something.
Willy Tarreau1accfc02009-09-05 20:57:35 +02002630 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002631 if ((si_applet_call(si_b) | si_applet_call(si_f)) != 0) {
Willy Tarreau1accfc02009-09-05 20:57:35 +02002632 if (task_in_rq(t)) {
Willy Tarreau1accfc02009-09-05 20:57:35 +02002633 t->expire = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02002634 stream_release_buffers(s);
Willy Tarreau1accfc02009-09-05 20:57:35 +02002635 return t;
2636 }
2637 }
2638
Willy Tarreau798f4322012-11-08 14:49:17 +01002639 update_exp_and_leave:
Willy Tarreau8f128b42014-11-28 15:07:47 +01002640 t->expire = tick_first(tick_first(req->rex, req->wex),
2641 tick_first(res->rex, res->wex));
2642 if (req->analysers)
2643 t->expire = tick_first(t->expire, req->analyse_exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002644
Willy Tarreau8f128b42014-11-28 15:07:47 +01002645 if (si_f->exp)
2646 t->expire = tick_first(t->expire, si_f->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002647
Willy Tarreau8f128b42014-11-28 15:07:47 +01002648 if (si_b->exp)
2649 t->expire = tick_first(t->expire, si_b->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002650
2651#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002652 fprintf(stderr,
2653 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2654 " rep->rex=%u rep->wex=%u, si[0].exp=%u, si[1].exp=%u, cs=%d, ss=%d\n",
Willy Tarreau8f128b42014-11-28 15:07:47 +01002655 now_ms, t->expire, req->rex, req->wex, req->analyse_exp,
2656 res->rex, res->wex, si_f->exp, si_b->exp, si_f->state, si_b->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002657#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002658
2659#ifdef DEBUG_DEV
2660 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002661 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002662 ABORT_NOW();
2663#endif
Willy Tarreau87b09662015-04-03 00:22:06 +02002664 stream_release_buffers(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002665 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002666 }
2667
2668 s->fe->feconn--;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002669 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002670 s->be->beconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002671 jobs--;
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002672 if (s->listener) {
2673 if (!(s->listener->options & LI_O_UNLIMITED))
2674 actconn--;
2675 s->listener->nbconn--;
2676 if (s->listener->state == LI_FULL)
2677 resume_listener(s->listener);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002678
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002679 /* Dequeues all of the listeners waiting for a resource */
2680 if (!LIST_ISEMPTY(&global_listener_queue))
2681 dequeue_all_listeners(&global_listener_queue);
Willy Tarreau08ceb102011-07-24 22:58:00 +02002682
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002683 if (!LIST_ISEMPTY(&s->fe->listener_queue) &&
2684 (!s->fe->fe_sps_lim || freq_ctr_remain(&s->fe->fe_sess_per_sec, s->fe->fe_sps_lim, 0) > 0))
2685 dequeue_all_listeners(&s->fe->listener_queue);
2686 }
Willy Tarreau07687c12011-07-24 23:55:06 +02002687
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002688 if (unlikely((global.mode & MODE_DEBUG) &&
2689 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002690 chunk_printf(&trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002691 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002692 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2693 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002694 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002695 }
2696
2697 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02002698 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002699
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002700 if (s->txn.status) {
2701 int n;
2702
2703 n = s->txn.status / 100;
2704 if (n < 1 || n > 5)
2705 n = 0;
2706
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002707 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002708 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002709 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002710 s->fe->fe_counters.p.http.comp_rsp++;
2711 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02002712 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002713 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002714 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002715 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002716 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002717 s->be->be_counters.p.http.comp_rsp++;
2718 }
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002719 }
2720
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002721 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002722 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02002723 !(s->flags & SF_MONITOR) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002724 (!(s->fe->options & PR_O_NULLNOLOG) || req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002725 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002726 }
2727
Willy Tarreau87b09662015-04-03 00:22:06 +02002728 /* update time stats for this stream */
2729 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002730
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002731 /* the task MUST not be in the run queue anymore */
Willy Tarreau87b09662015-04-03 00:22:06 +02002732 stream_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002733 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002734 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002735 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002736}
2737
Willy Tarreau87b09662015-04-03 00:22:06 +02002738/* Update the stream's backend and server time stats */
2739void stream_update_time_stats(struct stream *s)
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002740{
2741 int t_request;
2742 int t_queue;
2743 int t_connect;
2744 int t_data;
2745 int t_close;
2746 struct server *srv;
2747
2748 t_request = 0;
2749 t_queue = s->logs.t_queue;
2750 t_connect = s->logs.t_connect;
2751 t_close = s->logs.t_close;
2752 t_data = s->logs.t_data;
2753
2754 if (s->be->mode != PR_MODE_HTTP)
2755 t_data = t_connect;
2756
2757 if (t_connect < 0 || t_data < 0)
2758 return;
2759
2760 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
2761 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
2762
2763 t_data -= t_connect;
2764 t_connect -= t_queue;
2765 t_queue -= t_request;
2766
2767 srv = objt_server(s->target);
2768 if (srv) {
2769 swrate_add(&srv->counters.q_time, TIME_STATS_SAMPLES, t_queue);
2770 swrate_add(&srv->counters.c_time, TIME_STATS_SAMPLES, t_connect);
2771 swrate_add(&srv->counters.d_time, TIME_STATS_SAMPLES, t_data);
2772 swrate_add(&srv->counters.t_time, TIME_STATS_SAMPLES, t_close);
2773 }
2774 swrate_add(&s->be->be_counters.q_time, TIME_STATS_SAMPLES, t_queue);
2775 swrate_add(&s->be->be_counters.c_time, TIME_STATS_SAMPLES, t_connect);
2776 swrate_add(&s->be->be_counters.d_time, TIME_STATS_SAMPLES, t_data);
2777 swrate_add(&s->be->be_counters.t_time, TIME_STATS_SAMPLES, t_close);
2778}
2779
Willy Tarreau7c669d72008-06-20 15:04:11 +02002780/*
2781 * This function adjusts sess->srv_conn and maintains the previous and new
Willy Tarreau87b09662015-04-03 00:22:06 +02002782 * server's served stream counts. Setting newsrv to NULL is enough to release
Willy Tarreau7c669d72008-06-20 15:04:11 +02002783 * current connection slot. This function also notifies any LB algo which might
Willy Tarreau87b09662015-04-03 00:22:06 +02002784 * expect to be informed about any change in the number of active streams on a
Willy Tarreau7c669d72008-06-20 15:04:11 +02002785 * server.
2786 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002787void sess_change_server(struct stream *sess, struct server *newsrv)
Willy Tarreau7c669d72008-06-20 15:04:11 +02002788{
2789 if (sess->srv_conn == newsrv)
2790 return;
2791
2792 if (sess->srv_conn) {
2793 sess->srv_conn->served--;
2794 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2795 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Willy Tarreau87b09662015-04-03 00:22:06 +02002796 stream_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002797 }
2798
2799 if (newsrv) {
2800 newsrv->served++;
2801 if (newsrv->proxy->lbprm.server_take_conn)
2802 newsrv->proxy->lbprm.server_take_conn(newsrv);
Willy Tarreau87b09662015-04-03 00:22:06 +02002803 stream_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002804 }
2805}
2806
Willy Tarreau84455332009-03-15 22:34:05 +01002807/* Handle server-side errors for default protocols. It is called whenever a a
2808 * connection setup is aborted or a request is aborted in queue. It sets the
Willy Tarreau87b09662015-04-03 00:22:06 +02002809 * stream termination flags so that the caller does not have to worry about
Willy Tarreau84455332009-03-15 22:34:05 +01002810 * them. It's installed as ->srv_error for the server-side stream_interface.
2811 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002812void default_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreau84455332009-03-15 22:34:05 +01002813{
2814 int err_type = si->err_type;
2815 int err = 0, fin = 0;
2816
2817 if (err_type & SI_ET_QUEUE_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002818 err = SF_ERR_CLICL;
2819 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002820 }
2821 else if (err_type & SI_ET_CONN_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002822 err = SF_ERR_CLICL;
2823 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002824 }
2825 else if (err_type & SI_ET_QUEUE_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002826 err = SF_ERR_SRVTO;
2827 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002828 }
2829 else if (err_type & SI_ET_QUEUE_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002830 err = SF_ERR_SRVCL;
2831 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002832 }
2833 else if (err_type & SI_ET_CONN_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002834 err = SF_ERR_SRVTO;
2835 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002836 }
2837 else if (err_type & SI_ET_CONN_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002838 err = SF_ERR_SRVCL;
2839 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002840 }
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002841 else if (err_type & SI_ET_CONN_RES) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002842 err = SF_ERR_RESOURCE;
2843 fin = SF_FINST_C;
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002844 }
Willy Tarreau84455332009-03-15 22:34:05 +01002845 else /* SI_ET_CONN_OTHER and others */ {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002846 err = SF_ERR_INTERNAL;
2847 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002848 }
2849
Willy Tarreaue7dff022015-04-03 01:14:29 +02002850 if (!(s->flags & SF_ERR_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002851 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002852 if (!(s->flags & SF_FINST_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002853 s->flags |= fin;
2854}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002855
Willy Tarreaue7dff022015-04-03 01:14:29 +02002856/* kill a stream and set the termination flags to <why> (one of SF_ERR_*) */
Willy Tarreau87b09662015-04-03 00:22:06 +02002857void stream_shutdown(struct stream *stream, int why)
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002858{
Willy Tarreau87b09662015-04-03 00:22:06 +02002859 if (stream->req.flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002860 return;
2861
Willy Tarreau87b09662015-04-03 00:22:06 +02002862 channel_shutw_now(&stream->req);
2863 channel_shutr_now(&stream->res);
2864 stream->task->nice = 1024;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002865 if (!(stream->flags & SF_ERR_MASK))
Willy Tarreau87b09662015-04-03 00:22:06 +02002866 stream->flags |= why;
2867 task_wakeup(stream->task, TASK_WOKEN_OTHER);
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002868}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002869
Willy Tarreau8b22a712010-06-18 17:46:06 +02002870/************************************************************************/
2871/* All supported ACL keywords must be declared here. */
2872/************************************************************************/
2873
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002874/* Returns a pointer to a stkctr depending on the fetch keyword name.
2875 * It is designed to be called as sc[0-9]_* sc_* or src_* exclusively.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002876 * sc[0-9]_* will return a pointer to the respective field in the
Willy Tarreau87b09662015-04-03 00:22:06 +02002877 * stream <l4>. sc_* requires an UINT argument specifying the stick
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002878 * counter number. src_* will fill a locally allocated structure with
Willy Tarreaua65536c2013-07-22 22:40:11 +02002879 * the table and entry corresponding to what is specified with src_*.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002880 * NULL may be returned if the designated stkctr is not tracked. For
2881 * the sc_* and sc[0-9]_* forms, an optional table argument may be
2882 * passed. When present, the currently tracked key is then looked up
2883 * in the specified table instead of the current table. The purpose is
2884 * to be able to convery multiple values per key (eg: have gpc0 from
2885 * multiple tables).
Willy Tarreaua65536c2013-07-22 22:40:11 +02002886 */
Willy Tarreaue12704b2014-07-15 19:06:18 +02002887struct stkctr *
Willy Tarreau87b09662015-04-03 00:22:06 +02002888smp_fetch_sc_stkctr(struct stream *l4, const struct arg *args, const char *kw)
Willy Tarreaua65536c2013-07-22 22:40:11 +02002889{
2890 static struct stkctr stkctr;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002891 struct stksess *stksess;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002892 unsigned int num = kw[2] - '0';
Willy Tarreau0f791d42013-07-23 19:56:43 +02002893 int arg = 0;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002894
Willy Tarreau0f791d42013-07-23 19:56:43 +02002895 if (num == '_' - '0') {
2896 /* sc_* variant, args[0] = ctr# (mandatory) */
2897 num = args[arg++].data.uint;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002898 if (num >= MAX_SESS_STKCTR)
2899 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002900 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002901 else if (num > 9) { /* src_* variant, args[0] = table */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002902 struct stktable_key *key;
2903 struct connection *conn = objt_conn(l4->si[0].end);
2904
2905 if (!conn)
2906 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002907
Thierry FOURNIER74c219d2014-04-14 14:35:40 +02002908 key = addr_to_stktable_key(&conn->addr.from, args->data.prx->table.type);
Willy Tarreaua65536c2013-07-22 22:40:11 +02002909 if (!key)
2910 return NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002911
Willy Tarreaua65536c2013-07-22 22:40:11 +02002912 stkctr.table = &args->data.prx->table;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002913 stkctr_set_entry(&stkctr, stktable_lookup_key(stkctr.table, key));
Willy Tarreaua65536c2013-07-22 22:40:11 +02002914 return &stkctr;
2915 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002916
2917 /* Here, <num> contains the counter number from 0 to 9 for
2918 * the sc[0-9]_ form, or even higher using sc_(num) if needed.
2919 * args[arg] is the first optional argument.
2920 */
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002921 stksess = stkctr_entry(&l4->stkctr[num]);
2922 if (!stksess)
2923 return NULL;
2924
Willy Tarreau0f791d42013-07-23 19:56:43 +02002925 if (unlikely(args[arg].type == ARGT_TAB)) {
2926 /* an alternate table was specified, let's look up the same key there */
2927 stkctr.table = &args[arg].data.prx->table;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002928 stkctr_set_entry(&stkctr, stktable_lookup(stkctr.table, stksess));
Willy Tarreau0f791d42013-07-23 19:56:43 +02002929 return &stkctr;
2930 }
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002931 return &l4->stkctr[num];
Willy Tarreaua65536c2013-07-22 22:40:11 +02002932}
2933
Willy Tarreau87b09662015-04-03 00:22:06 +02002934/* set return a boolean indicating if the requested stream counter is
Willy Tarreau88821242013-07-22 18:29:29 +02002935 * currently being tracked or not.
2936 * Supports being called as "sc[0-9]_tracked" only.
2937 */
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002938static int
Willy Tarreau87b09662015-04-03 00:22:06 +02002939smp_fetch_sc_tracked(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002940 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002941{
2942 smp->flags = SMP_F_VOL_TEST;
2943 smp->type = SMP_T_BOOL;
Thierry FOURNIERd988f212014-04-15 01:15:52 +02002944 smp->data.uint = !!smp_fetch_sc_stkctr(l4, args, kw);
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002945 return 1;
2946}
2947
Willy Tarreau87b09662015-04-03 00:22:06 +02002948/* set <smp> to the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau30b20462013-07-22 19:46:52 +02002949 * frontend counters or from the src.
2950 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpc0" only. Value
2951 * zero is returned if the key is new.
2952 */
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002953static int
Willy Tarreau87b09662015-04-03 00:22:06 +02002954smp_fetch_sc_get_gpc0(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002955 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002956{
Willy Tarreau30b20462013-07-22 19:46:52 +02002957 struct stkctr *stkctr = smp_fetch_sc_stkctr(l4, args, kw);
2958
2959 if (!stkctr)
2960 return 0;
2961
Willy Tarreau37406352012-04-23 16:16:37 +02002962 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002963 smp->type = SMP_T_UINT;
2964 smp->data.uint = 0;
Willy Tarreau30b20462013-07-22 19:46:52 +02002965
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002966 if (stkctr_entry(stkctr) != NULL) {
2967 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002968 if (!ptr)
2969 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002970 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002971 }
2972 return 1;
2973}
2974
Willy Tarreau87b09662015-04-03 00:22:06 +02002975/* set <smp> to the General Purpose Counter 0's event rate from the stream's
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002976 * tracked frontend counters or from the src.
2977 * Supports being called as "sc[0-9]_gpc0_rate" or "src_gpc0_rate" only.
2978 * Value zero is returned if the key is new.
2979 */
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002980static int
Willy Tarreau87b09662015-04-03 00:22:06 +02002981smp_fetch_sc_gpc0_rate(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002982 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002983{
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002984 struct stkctr *stkctr = smp_fetch_sc_stkctr(l4, args, kw);
2985
2986 if (!stkctr)
2987 return 0;
2988
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002989 smp->flags = SMP_F_VOL_TEST;
2990 smp->type = SMP_T_UINT;
2991 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002992 if (stkctr_entry(stkctr) != NULL) {
2993 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002994 if (!ptr)
2995 return 0; /* parameter not stored */
2996 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002997 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002998 }
2999 return 1;
3000}
3001
Willy Tarreau87b09662015-04-03 00:22:06 +02003002/* Increment the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau710d38c2013-07-23 00:07:04 +02003003 * frontend counters and return it into temp integer.
3004 * Supports being called as "sc[0-9]_inc_gpc0" or "src_inc_gpc0" only.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02003005 */
3006static int
Willy Tarreau87b09662015-04-03 00:22:06 +02003007smp_fetch_sc_inc_gpc0(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003008 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02003009{
Willy Tarreau710d38c2013-07-23 00:07:04 +02003010 struct stkctr *stkctr = smp_fetch_sc_stkctr(l4, args, kw);
3011
3012 if (!stkctr)
3013 return 0;
3014
Willy Tarreau37406352012-04-23 16:16:37 +02003015 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003016 smp->type = SMP_T_UINT;
3017 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003018 if (stkctr_entry(stkctr) != NULL) {
Willy Tarreauba2ffd12013-05-29 15:54:14 +02003019 void *ptr;
3020
3021 /* First, update gpc0_rate if it's tracked. Second, update its
3022 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
3023 */
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003024 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02003025 if (ptr) {
3026 update_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Willy Tarreau710d38c2013-07-23 00:07:04 +02003027 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02003028 smp->data.uint = (&stktable_data_cast(ptr, gpc0_rate))->curr_ctr;
3029 }
3030
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003031 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02003032 if (ptr)
3033 smp->data.uint = ++stktable_data_cast(ptr, gpc0);
3034
Willy Tarreauc3bd9722010-06-20 12:47:25 +02003035 }
3036 return 1;
3037}
3038
Willy Tarreau87b09662015-04-03 00:22:06 +02003039/* Clear the General Purpose Counter 0 value from the stream's tracked
Willy Tarreaub9f441d2013-07-23 00:10:35 +02003040 * frontend counters and return its previous value into temp integer.
3041 * Supports being called as "sc[0-9]_clr_gpc0" or "src_clr_gpc0" only.
Willy Tarreauf73cd112011-08-13 01:45:16 +02003042 */
3043static int
Willy Tarreau87b09662015-04-03 00:22:06 +02003044smp_fetch_sc_clr_gpc0(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003045 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf73cd112011-08-13 01:45:16 +02003046{
Willy Tarreaub9f441d2013-07-23 00:10:35 +02003047 struct stkctr *stkctr = smp_fetch_sc_stkctr(l4, args, kw);
3048
3049 if (!stkctr)
3050 return 0;
3051
Willy Tarreau37406352012-04-23 16:16:37 +02003052 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003053 smp->type = SMP_T_UINT;
3054 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003055 if (stkctr_entry(stkctr) != NULL) {
3056 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02003057 if (!ptr)
3058 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003059 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02003060 stktable_data_cast(ptr, gpc0) = 0;
3061 }
3062 return 1;
3063}
3064
Willy Tarreau87b09662015-04-03 00:22:06 +02003065/* set <smp> to the cumulated number of connections from the stream's tracked
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02003066 * frontend counters. Supports being called as "sc[0-9]_conn_cnt" or
3067 * "src_conn_cnt" only.
3068 */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02003069static int
Willy Tarreau87b09662015-04-03 00:22:06 +02003070smp_fetch_sc_conn_cnt(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003071 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02003072{
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02003073 struct stkctr *stkctr = smp_fetch_sc_stkctr(l4, args, kw);
3074
3075 if (!stkctr)
3076 return 0;
3077
Willy Tarreau37406352012-04-23 16:16:37 +02003078 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003079 smp->type = SMP_T_UINT;
3080 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003081 if (stkctr_entry(stkctr) != NULL) {
3082 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CNT);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02003083 if (!ptr)
3084 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003085 smp->data.uint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02003086 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02003087 return 1;
3088}
3089
Willy Tarreau87b09662015-04-03 00:22:06 +02003090/* set <smp> to the connection rate from the stream's tracked frontend
Willy Tarreauc8c65702013-07-23 15:09:35 +02003091 * counters. Supports being called as "sc[0-9]_conn_rate" or "src_conn_rate"
3092 * only.
3093 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02003094static int
Willy Tarreau87b09662015-04-03 00:22:06 +02003095smp_fetch_sc_conn_rate(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003096 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02003097{
Willy Tarreauc8c65702013-07-23 15:09:35 +02003098 struct stkctr *stkctr = smp_fetch_sc_stkctr(l4, args, kw);
3099
3100 if (!stkctr)
3101 return 0;
3102
Willy Tarreau37406352012-04-23 16:16:37 +02003103 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003104 smp->type = SMP_T_UINT;
3105 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003106 if (stkctr_entry(stkctr) != NULL) {
3107 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003108 if (!ptr)
3109 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003110 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreauc8c65702013-07-23 15:09:35 +02003111 stkctr->table->data_arg[STKTABLE_DT_CONN_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003112 }
3113 return 1;
3114}
3115
Willy Tarreau87b09662015-04-03 00:22:06 +02003116/* set temp integer to the number of connections from the stream's source address
Willy Tarreau8b22a712010-06-18 17:46:06 +02003117 * in the table pointed to by expr, after updating it.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003118 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02003119 */
3120static int
Willy Tarreau87b09662015-04-03 00:22:06 +02003121smp_fetch_src_updt_conn_cnt(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003122 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8b22a712010-06-18 17:46:06 +02003123{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003124 struct connection *conn = objt_conn(l4->si[0].end);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003125 struct stksess *ts;
3126 struct stktable_key *key;
3127 void *ptr;
3128
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003129 if (!conn)
3130 return 0;
3131
Thierry FOURNIER74c219d2014-04-14 14:35:40 +02003132 key = addr_to_stktable_key(&conn->addr.from, px->table.type);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003133 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003134 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02003135
Willy Tarreau24e32d82012-04-23 23:55:44 +02003136 px = args->data.prx;
Willy Tarreau8b22a712010-06-18 17:46:06 +02003137
Willy Tarreau1f7e9252010-06-20 12:27:21 +02003138 if ((ts = stktable_update_key(&px->table, key)) == NULL)
3139 /* entry does not exist and could not be created */
3140 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02003141
3142 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
3143 if (!ptr)
3144 return 0; /* parameter not stored in this table */
3145
Willy Tarreauf853c462012-04-23 18:53:56 +02003146 smp->type = SMP_T_UINT;
3147 smp->data.uint = ++stktable_data_cast(ptr, conn_cnt);
Willy Tarreau37406352012-04-23 16:16:37 +02003148 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau8b22a712010-06-18 17:46:06 +02003149 return 1;
3150}
3151
Willy Tarreau87b09662015-04-03 00:22:06 +02003152/* set <smp> to the number of concurrent connections from the stream's tracked
Willy Tarreauf44a5532013-07-23 15:17:53 +02003153 * frontend counters. Supports being called as "sc[0-9]_conn_cur" or
3154 * "src_conn_cur" only.
3155 */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003156static int
Willy Tarreau87b09662015-04-03 00:22:06 +02003157smp_fetch_sc_conn_cur(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003158 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003159{
Willy Tarreauf44a5532013-07-23 15:17:53 +02003160 struct stkctr *stkctr = smp_fetch_sc_stkctr(l4, args, kw);
3161
3162 if (!stkctr)
3163 return 0;
3164
Willy Tarreau37406352012-04-23 16:16:37 +02003165 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003166 smp->type = SMP_T_UINT;
3167 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003168 if (stkctr_entry(stkctr) != NULL) {
3169 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CUR);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003170 if (!ptr)
3171 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003172 smp->data.uint = stktable_data_cast(ptr, conn_cur);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003173 }
3174 return 1;
3175}
3176
Willy Tarreau87b09662015-04-03 00:22:06 +02003177/* set <smp> to the cumulated number of streams from the stream's tracked
Willy Tarreau20843082013-07-23 15:35:33 +02003178 * frontend counters. Supports being called as "sc[0-9]_sess_cnt" or
3179 * "src_sess_cnt" only.
3180 */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003181static int
Willy Tarreau87b09662015-04-03 00:22:06 +02003182smp_fetch_sc_sess_cnt(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003183 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003184{
Willy Tarreau20843082013-07-23 15:35:33 +02003185 struct stkctr *stkctr = smp_fetch_sc_stkctr(l4, args, kw);
3186
3187 if (!stkctr)
3188 return 0;
3189
Willy Tarreau37406352012-04-23 16:16:37 +02003190 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003191 smp->type = SMP_T_UINT;
3192 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003193 if (stkctr_entry(stkctr) != NULL) {
3194 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003195 if (!ptr)
3196 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003197 smp->data.uint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003198 }
3199 return 1;
3200}
3201
Willy Tarreau87b09662015-04-03 00:22:06 +02003202/* set <smp> to the stream rate from the stream's tracked frontend counters.
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003203 * Supports being called as "sc[0-9]_sess_rate" or "src_sess_rate" only.
3204 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02003205static int
Willy Tarreau87b09662015-04-03 00:22:06 +02003206smp_fetch_sc_sess_rate(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003207 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02003208{
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003209 struct stkctr *stkctr = smp_fetch_sc_stkctr(l4, args, kw);
3210
3211 if (!stkctr)
3212 return 0;
3213
Willy Tarreau37406352012-04-23 16:16:37 +02003214 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003215 smp->type = SMP_T_UINT;
3216 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003217 if (stkctr_entry(stkctr) != NULL) {
3218 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003219 if (!ptr)
3220 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003221 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003222 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003223 }
3224 return 1;
3225}
3226
Willy Tarreau87b09662015-04-03 00:22:06 +02003227/* set <smp> to the cumulated number of HTTP requests from the stream's tracked
Willy Tarreau91200da2013-07-23 15:55:19 +02003228 * frontend counters. Supports being called as "sc[0-9]_http_req_cnt" or
3229 * "src_http_req_cnt" only.
3230 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003231static int
Willy Tarreau87b09662015-04-03 00:22:06 +02003232smp_fetch_sc_http_req_cnt(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003233 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003234{
Willy Tarreau91200da2013-07-23 15:55:19 +02003235 struct stkctr *stkctr = smp_fetch_sc_stkctr(l4, args, kw);
3236
3237 if (!stkctr)
3238 return 0;
3239
Willy Tarreau37406352012-04-23 16:16:37 +02003240 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003241 smp->type = SMP_T_UINT;
3242 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003243 if (stkctr_entry(stkctr) != NULL) {
3244 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003245 if (!ptr)
3246 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003247 smp->data.uint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003248 }
3249 return 1;
3250}
3251
Willy Tarreau87b09662015-04-03 00:22:06 +02003252/* set <smp> to the HTTP request rate from the stream's tracked frontend
Willy Tarreaucf477632013-07-23 16:04:37 +02003253 * counters. Supports being called as "sc[0-9]_http_req_rate" or
3254 * "src_http_req_rate" only.
3255 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003256static int
Willy Tarreau87b09662015-04-03 00:22:06 +02003257smp_fetch_sc_http_req_rate(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003258 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003259{
Willy Tarreaucf477632013-07-23 16:04:37 +02003260 struct stkctr *stkctr = smp_fetch_sc_stkctr(l4, args, kw);
3261
3262 if (!stkctr)
3263 return 0;
3264
Willy Tarreau37406352012-04-23 16:16:37 +02003265 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003266 smp->type = SMP_T_UINT;
3267 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003268 if (stkctr_entry(stkctr) != NULL) {
3269 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003270 if (!ptr)
3271 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003272 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreaucf477632013-07-23 16:04:37 +02003273 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003274 }
3275 return 1;
3276}
3277
Willy Tarreau87b09662015-04-03 00:22:06 +02003278/* set <smp> to the cumulated number of HTTP requests errors from the stream's
Willy Tarreau30d07c32013-07-23 16:45:38 +02003279 * tracked frontend counters. Supports being called as "sc[0-9]_http_err_cnt" or
3280 * "src_http_err_cnt" only.
3281 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003282static int
Willy Tarreau87b09662015-04-03 00:22:06 +02003283smp_fetch_sc_http_err_cnt(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003284 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003285{
Willy Tarreau30d07c32013-07-23 16:45:38 +02003286 struct stkctr *stkctr = smp_fetch_sc_stkctr(l4, args, kw);
3287
3288 if (!stkctr)
3289 return 0;
3290
Willy Tarreau37406352012-04-23 16:16:37 +02003291 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003292 smp->type = SMP_T_UINT;
3293 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003294 if (stkctr_entry(stkctr) != NULL) {
3295 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003296 if (!ptr)
3297 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003298 smp->data.uint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003299 }
3300 return 1;
3301}
3302
Willy Tarreau87b09662015-04-03 00:22:06 +02003303/* set <smp> to the HTTP request error rate from the stream's tracked frontend
Willy Tarreau9daf2622013-07-23 16:48:54 +02003304 * counters. Supports being called as "sc[0-9]_http_err_rate" or
3305 * "src_http_err_rate" only.
3306 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003307static int
Willy Tarreau87b09662015-04-03 00:22:06 +02003308smp_fetch_sc_http_err_rate(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003309 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003310{
Willy Tarreau9daf2622013-07-23 16:48:54 +02003311 struct stkctr *stkctr = smp_fetch_sc_stkctr(l4, args, kw);
3312
3313 if (!stkctr)
3314 return 0;
3315
Willy Tarreau37406352012-04-23 16:16:37 +02003316 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003317 smp->type = SMP_T_UINT;
3318 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003319 if (stkctr_entry(stkctr) != NULL) {
3320 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003321 if (!ptr)
3322 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003323 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau9daf2622013-07-23 16:48:54 +02003324 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003325 }
3326 return 1;
3327}
3328
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003329/* set <smp> to the number of kbytes received from clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003330 * stream's tracked frontend counters. Supports being called as
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003331 * "sc[0-9]_kbytes_in" or "src_kbytes_in" only.
3332 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003333static int
Willy Tarreau87b09662015-04-03 00:22:06 +02003334smp_fetch_sc_kbytes_in(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003335 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003336{
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003337 struct stkctr *stkctr = smp_fetch_sc_stkctr(l4, args, kw);
3338
3339 if (!stkctr)
3340 return 0;
3341
Willy Tarreau37406352012-04-23 16:16:37 +02003342 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003343 smp->type = SMP_T_UINT;
3344 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003345 if (stkctr_entry(stkctr) != NULL) {
3346 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003347 if (!ptr)
3348 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003349 smp->data.uint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003350 }
3351 return 1;
3352}
3353
Willy Tarreau613fe992013-07-23 17:39:19 +02003354/* set <smp> to the data rate received from clients in bytes/s, as found
Willy Tarreau87b09662015-04-03 00:22:06 +02003355 * in the stream's tracked frontend counters. Supports being called as
Willy Tarreau613fe992013-07-23 17:39:19 +02003356 * "sc[0-9]_bytes_in_rate" or "src_bytes_in_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003357 */
3358static int
Willy Tarreau87b09662015-04-03 00:22:06 +02003359smp_fetch_sc_bytes_in_rate(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003360 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003361{
Willy Tarreau613fe992013-07-23 17:39:19 +02003362 struct stkctr *stkctr = smp_fetch_sc_stkctr(l4, args, kw);
3363
3364 if (!stkctr)
3365 return 0;
3366
Willy Tarreau37406352012-04-23 16:16:37 +02003367 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003368 smp->type = SMP_T_UINT;
3369 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003370 if (stkctr_entry(stkctr) != NULL) {
3371 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003372 if (!ptr)
3373 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003374 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau613fe992013-07-23 17:39:19 +02003375 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003376 }
3377 return 1;
3378}
3379
Willy Tarreau53aea102013-07-23 17:39:02 +02003380/* set <smp> to the number of kbytes sent to clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003381 * stream's tracked frontend counters. Supports being called as
Willy Tarreau53aea102013-07-23 17:39:02 +02003382 * "sc[0-9]_kbytes_out" or "src_kbytes_out" only.
3383 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003384static int
Willy Tarreau87b09662015-04-03 00:22:06 +02003385smp_fetch_sc_kbytes_out(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003386 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003387{
Willy Tarreau53aea102013-07-23 17:39:02 +02003388 struct stkctr *stkctr = smp_fetch_sc_stkctr(l4, args, kw);
3389
3390 if (!stkctr)
3391 return 0;
3392
Willy Tarreau37406352012-04-23 16:16:37 +02003393 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003394 smp->type = SMP_T_UINT;
3395 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003396 if (stkctr_entry(stkctr) != NULL) {
3397 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003398 if (!ptr)
3399 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003400 smp->data.uint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003401 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003402 return 1;
3403}
3404
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003405/* set <smp> to the data rate sent to clients in bytes/s, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003406 * stream's tracked frontend counters. Supports being called as
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003407 * "sc[0-9]_bytes_out_rate" or "src_bytes_out_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003408 */
3409static int
Willy Tarreau87b09662015-04-03 00:22:06 +02003410smp_fetch_sc_bytes_out_rate(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003411 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003412{
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003413 struct stkctr *stkctr = smp_fetch_sc_stkctr(l4, args, kw);
3414
3415 if (!stkctr)
3416 return 0;
3417
Willy Tarreau37406352012-04-23 16:16:37 +02003418 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003419 smp->type = SMP_T_UINT;
3420 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003421 if (stkctr_entry(stkctr) != NULL) {
3422 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003423 if (!ptr)
3424 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003425 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003426 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003427 }
3428 return 1;
3429}
3430
Willy Tarreau87b09662015-04-03 00:22:06 +02003431/* set <smp> to the number of active trackers on the SC entry in the stream's
Willy Tarreau563eef42013-07-23 18:32:02 +02003432 * tracked frontend counters. Supports being called as "sc[0-9]_trackers" only.
3433 */
Willy Tarreau2406db42012-12-09 12:16:43 +01003434static int
Willy Tarreau87b09662015-04-03 00:22:06 +02003435smp_fetch_sc_trackers(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003436 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2406db42012-12-09 12:16:43 +01003437{
Willy Tarreau563eef42013-07-23 18:32:02 +02003438 struct stkctr *stkctr = smp_fetch_sc_stkctr(l4, args, kw);
Willy Tarreau2406db42012-12-09 12:16:43 +01003439
Willy Tarreau563eef42013-07-23 18:32:02 +02003440 if (!stkctr)
Willy Tarreau2406db42012-12-09 12:16:43 +01003441 return 0;
3442
Willy Tarreau563eef42013-07-23 18:32:02 +02003443 smp->flags = SMP_F_VOL_TEST;
3444 smp->type = SMP_T_UINT;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003445 smp->data.uint = stkctr_entry(stkctr)->ref_cnt;
Willy Tarreau563eef42013-07-23 18:32:02 +02003446 return 1;
Willy Tarreaue25c9172013-05-28 18:32:20 +02003447}
3448
Willy Tarreau34db1082012-04-19 17:16:54 +02003449/* set temp integer to the number of used entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003450 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003451 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003452static int
Willy Tarreau87b09662015-04-03 00:22:06 +02003453smp_fetch_table_cnt(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003454 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003455{
Willy Tarreau37406352012-04-23 16:16:37 +02003456 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003457 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02003458 smp->data.uint = args->data.prx->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003459 return 1;
3460}
3461
Willy Tarreau34db1082012-04-19 17:16:54 +02003462/* set temp integer to the number of free entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003463 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003464 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003465static int
Willy Tarreau87b09662015-04-03 00:22:06 +02003466smp_fetch_table_avl(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003467 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003468{
Willy Tarreau24e32d82012-04-23 23:55:44 +02003469 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003470 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003471 smp->type = SMP_T_UINT;
3472 smp->data.uint = px->table.size - px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003473 return 1;
3474}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003475
Willy Tarreau61612d42012-04-19 18:42:05 +02003476/* Note: must not be declared <const> as its list will be overwritten.
3477 * Please take care of keeping this list alphabetically sorted.
3478 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003479static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau281c7992013-01-08 01:23:27 +01003480 { /* END */ },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003481}};
3482
Willy Tarreau281c7992013-01-08 01:23:27 +01003483/* Note: must not be declared <const> as its list will be overwritten.
3484 * Please take care of keeping this list alphabetically sorted.
3485 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003486static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
Willy Tarreau0f791d42013-07-23 19:56:43 +02003487 { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3488 { "sc_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3489 { "sc_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3490 { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3491 { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3492 { "sc_conn_rate", smp_fetch_sc_conn_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3493 { "sc_get_gpc0", smp_fetch_sc_get_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3494 { "sc_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3495 { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3496 { "sc_http_err_rate", smp_fetch_sc_http_err_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3497 { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3498 { "sc_http_req_rate", smp_fetch_sc_http_req_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3499 { "sc_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3500 { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3501 { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3502 { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3503 { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3504 { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,UINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3505 { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3506 { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3507 { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3508 { "sc0_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3509 { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3510 { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3511 { "sc0_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3512 { "sc0_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3513 { "sc0_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3514 { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3515 { "sc0_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3516 { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3517 { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3518 { "sc0_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3519 { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3520 { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3521 { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3522 { "sc0_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3523 { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3524 { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3525 { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3526 { "sc1_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3527 { "sc1_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3528 { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3529 { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3530 { "sc1_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3531 { "sc1_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3532 { "sc1_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3533 { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3534 { "sc1_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3535 { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3536 { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3537 { "sc1_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3538 { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3539 { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3540 { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3541 { "sc1_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3542 { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3543 { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3544 { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3545 { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3546 { "sc2_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3547 { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3548 { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3549 { "sc2_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3550 { "sc2_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3551 { "sc2_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3552 { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3553 { "sc2_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3554 { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3555 { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3556 { "sc2_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3557 { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3558 { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3559 { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3560 { "sc2_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3561 { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3562 { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3563 { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3564 { "src_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3565 { "src_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3566 { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3567 { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3568 { "src_conn_rate", smp_fetch_sc_conn_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3569 { "src_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3570 { "src_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3571 { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3572 { "src_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3573 { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3574 { "src_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3575 { "src_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3576 { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3577 { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3578 { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3579 { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3580 { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3581 { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3582 { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
Willy Tarreau281c7992013-01-08 01:23:27 +01003583 { /* END */ },
3584}};
3585
Willy Tarreau8b22a712010-06-18 17:46:06 +02003586__attribute__((constructor))
Willy Tarreau87b09662015-04-03 00:22:06 +02003587static void __stream_init(void)
Willy Tarreau8b22a712010-06-18 17:46:06 +02003588{
Willy Tarreau281c7992013-01-08 01:23:27 +01003589 sample_register_fetches(&smp_fetch_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003590 acl_register_keywords(&acl_kws);
3591}
3592
Willy Tarreaubaaee002006-06-26 02:48:02 +02003593/*
3594 * Local variables:
3595 * c-indent-level: 8
3596 * c-basic-offset: 8
3597 * End:
3598 */