blob: 9d906edf0d85dd62be88795e0b0435e3575b52e0 [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 Tarreaufeb76402015-04-03 14:10:06 +020040#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020041#include <proto/stream.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010042#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010043#include <proto/proto_http.h>
44#include <proto/proto_tcp.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020045#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020046#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010047#include <proto/server.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020048#include <proto/sample.h>
Emeric Brun1d33b292010-01-04 15:47:17 +010049#include <proto/stick_table.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010050#include <proto/stream_interface.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010051#include <proto/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020052
Willy Tarreau87b09662015-04-03 00:22:06 +020053struct pool_head *pool2_stream;
54struct list streams;
Willy Tarreaubaaee002006-06-26 02:48:02 +020055
Willy Tarreau87b09662015-04-03 00:22:06 +020056/* list of streams waiting for at least one buffer */
Willy Tarreaubf883e02014-11-25 21:10:35 +010057struct list buffer_wq = LIST_HEAD_INIT(buffer_wq);
58
Willy Tarreau87b09662015-04-03 00:22:06 +020059static int conn_stream_complete(struct connection *conn);
60static int conn_stream_update(struct connection *conn);
Willy Tarreau2542b532012-08-31 16:01:23 +020061static struct task *expire_mini_session(struct task *t);
Willy Tarreau87b09662015-04-03 00:22:06 +020062int stream_complete(struct stream *s);
Willy Tarreau2542b532012-08-31 16:01:23 +020063
Willy Tarreau87b09662015-04-03 00:22:06 +020064/* data layer callbacks for an embryonic stream */
Willy Tarreau5e75e272012-10-02 21:21:20 +020065struct data_cb sess_conn_cb = {
66 .recv = NULL,
67 .send = NULL,
Willy Tarreau87b09662015-04-03 00:22:06 +020068 .wake = conn_stream_update,
69 .init = conn_stream_complete,
Willy Tarreau5e75e272012-10-02 21:21:20 +020070};
71
Willy Tarreau2542b532012-08-31 16:01:23 +020072/* This function is called from the protocol layer accept() in order to
Willy Tarreau87b09662015-04-03 00:22:06 +020073 * instanciate a new embryonic stream on behalf of a given listener and
Willy Tarreau2542b532012-08-31 16:01:23 +020074 * frontend. It returns a positive value upon success, 0 if the connection
75 * can be ignored, or a negative value upon critical failure. The accepted
76 * file descriptor is closed if we return <= 0.
Willy Tarreau81f9aa32010-06-01 17:45:26 +020077 */
Willy Tarreau87b09662015-04-03 00:22:06 +020078int stream_accept(struct listener *l, int cfd, struct sockaddr_storage *addr)
Willy Tarreau81f9aa32010-06-01 17:45:26 +020079{
Willy Tarreaub363a1f2013-10-01 10:45:07 +020080 struct connection *cli_conn;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020081 struct proxy *p = l->frontend;
Willy Tarreau87b09662015-04-03 00:22:06 +020082 struct stream *s;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020083 struct task *t;
Willy Tarreauabe8ea52010-11-11 10:56:04 +010084 int ret;
85
86
87 ret = -1; /* assume unrecoverable error by default */
Willy Tarreau81f9aa32010-06-01 17:45:26 +020088
Willy Tarreau32e3c6a2013-10-11 19:34:20 +020089 if (unlikely((cli_conn = conn_new()) == NULL))
Willy Tarreau81f9aa32010-06-01 17:45:26 +020090 goto out_close;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020091
Willy Tarreau32e3c6a2013-10-11 19:34:20 +020092 conn_prepare(cli_conn, l->proto, l->xprt);
Willy Tarreauf2943dc2012-10-26 20:10:28 +020093
Willy Tarreau32e3c6a2013-10-11 19:34:20 +020094 cli_conn->t.sock.fd = cfd;
95 cli_conn->addr.from = *addr;
96 cli_conn->flags |= CO_FL_ADDR_FROM_SET;
97 cli_conn->target = &l->obj_type;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010098 cli_conn->proxy_netns = l->netns;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +020099
Willy Tarreau87b09662015-04-03 00:22:06 +0200100 if (unlikely((s = pool_alloc2(pool2_stream)) == NULL))
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200101 goto out_free_conn;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200102
Willy Tarreau87b09662015-04-03 00:22:06 +0200103 /* minimum stream initialization required for an embryonic stream is
Willy Tarreau2542b532012-08-31 16:01:23 +0200104 * fairly low. We need very little to execute L4 ACLs, then we need a
105 * task to make the client-side connection live on its own.
106 * - flags
107 * - stick-entry tracking
108 */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200109 s->flags = 0;
110 s->logs.logwait = p->to_log;
Willy Tarreau9a355ec2013-06-11 17:45:46 +0200111 s->logs.level = 0;
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +0200112
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +0100113 /* Initialise the current rule list pointer to NULL. We are sure that
114 * any rulelist match the NULL pointer.
115 */
116 s->current_rule_list = NULL;
117
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +0200118 memset(s->stkctr, 0, sizeof(s->stkctr));
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200119
Willy Tarreaufeb76402015-04-03 14:10:06 +0200120 s->sess = pool_alloc2(pool2_session);
121 if (!s->sess)
122 goto out_free_stream;
123
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200124 s->sess->listener = l;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200125 s->sess->fe = p;
Willy Tarreau40606ab2015-04-03 18:08:29 +0200126 s->sess->origin = &cli_conn->obj_type;
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100127
Willy Tarreaua5f5d8d2014-11-28 11:26:07 +0100128 s->si[0].flags = SI_FL_NONE;
129 s->si[1].flags = SI_FL_ISBACK;
130
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200131 s->logs.accept_date = date; /* user-visible date for logging */
132 s->logs.tv_accept = now; /* corrected date for internal use */
Willy Tarreau1f0da242014-01-25 11:01:50 +0100133 s->uniq_id = global.req_count++;
Willy Tarreau2542b532012-08-31 16:01:23 +0200134 p->feconn++;
Willy Tarreau87b09662015-04-03 00:22:06 +0200135 /* This stream was accepted, count it now */
Willy Tarreau2542b532012-08-31 16:01:23 +0200136 if (p->feconn > p->fe_counters.conn_max)
137 p->fe_counters.conn_max = p->feconn;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200138
Willy Tarreau2542b532012-08-31 16:01:23 +0200139 proxy_inc_fe_conn_ctr(l, p);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200140
Willy Tarreau59e3ff42013-12-16 02:16:50 +0100141 /* Add the minimum callbacks to prepare the connection's control layer.
142 * We need this so that we can safely execute the ACLs used by the
143 * "tcp-request connection" ruleset. We also carefully attach the
144 * connection to the stream interface without initializing the rest,
145 * so that ACLs can use si[0]->end.
146 */
147 si_attach_conn(&s->si[0], cli_conn);
148 conn_attach(cli_conn, s, &sess_conn_cb);
149 conn_ctrl_init(cli_conn);
150
Willy Tarreau2cff2f72013-12-16 10:12:54 +0100151 /* now evaluate the tcp-request layer4 rules. Since we expect to be able
152 * to abort right here as soon as possible, we check the rules before
153 * even initializing the stream interfaces.
154 */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200155 if ((l->options & LI_O_TCP_RULES) && !tcp_exec_req_rules(s)) {
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200156 /* let's do a no-linger now to close with a single RST. */
157 setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100158 ret = 0; /* successful termination */
Willy Tarreau2542b532012-08-31 16:01:23 +0200159 goto out_free_session;
160 }
161
Willy Tarreau82569f92012-09-27 23:48:56 +0200162 /* monitor-net and health mode are processed immediately after TCP
163 * connection rules. This way it's possible to block them, but they
164 * never use the lower data layers, they send directly over the socket,
165 * as they were designed for. We first flush the socket receive buffer
166 * in order to avoid emission of an RST by the system. We ignore any
167 * error.
168 */
169 if (unlikely((p->mode == PR_MODE_HEALTH) ||
170 ((l->options & LI_O_CHK_MONNET) &&
171 addr->ss_family == AF_INET &&
172 (((struct sockaddr_in *)addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr))) {
173 /* we have 4 possibilities here :
174 * - HTTP mode, from monitoring address => send "HTTP/1.0 200 OK"
175 * - HEALTH mode with HTTP check => send "HTTP/1.0 200 OK"
176 * - HEALTH mode without HTTP check => just send "OK"
177 * - TCP mode from monitoring address => just close
178 */
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200179 if (l->proto->drain)
180 l->proto->drain(cfd);
Willy Tarreau82569f92012-09-27 23:48:56 +0200181 if (p->mode == PR_MODE_HTTP ||
182 (p->mode == PR_MODE_HEALTH && (p->options2 & PR_O2_CHK_ANY) == PR_O2_HTTP_CHK))
183 send(cfd, "HTTP/1.0 200 OK\r\n\r\n", 19, MSG_DONTWAIT|MSG_NOSIGNAL|MSG_MORE);
184 else if (p->mode == PR_MODE_HEALTH)
185 send(cfd, "OK\n", 3, MSG_DONTWAIT|MSG_NOSIGNAL|MSG_MORE);
186 ret = 0;
187 goto out_free_session;
188 }
189
Willy Tarreau22cda212012-08-31 17:43:29 +0200190 /* wait for a PROXY protocol header */
191 if (l->options & LI_O_ACC_PROXY) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200192 cli_conn->flags |= CO_FL_ACCEPT_PROXY;
193 conn_sock_want_recv(cli_conn);
Willy Tarreau22cda212012-08-31 17:43:29 +0200194 }
195
Willy Tarreau2542b532012-08-31 16:01:23 +0200196 if (unlikely((t = task_new()) == NULL))
197 goto out_free_session;
198
199 t->context = s;
200 t->nice = l->nice;
201 s->task = t;
202
Willy Tarreau59e3ff42013-12-16 02:16:50 +0100203 /* Finish setting the callbacks. Right now the transport layer is present
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200204 * but not initialized. Also note we need to be careful as the stream
205 * int is not initialized yet.
Willy Tarreau2542b532012-08-31 16:01:23 +0200206 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200207 conn_data_want_recv(cli_conn);
208 if (conn_xprt_init(cli_conn) < 0)
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100209 goto out_free_task;
Willy Tarreau2542b532012-08-31 16:01:23 +0200210
211 /* OK, now either we have a pending handshake to execute with and
212 * then we must return to the I/O layer, or we can proceed with the
Willy Tarreau87b09662015-04-03 00:22:06 +0200213 * end of the stream initialization. In case of handshake, we also
Willy Tarreau2542b532012-08-31 16:01:23 +0200214 * set the I/O timeout to the frontend's client timeout.
215 */
216
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200217 if (cli_conn->flags & CO_FL_HANDSHAKE) {
Willy Tarreau2542b532012-08-31 16:01:23 +0200218 t->process = expire_mini_session;
219 t->expire = tick_add_ifset(now_ms, p->timeout.client);
220 task_queue(t);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200221 cli_conn->flags |= CO_FL_INIT_DATA | CO_FL_WAKE_DATA;
Willy Tarreau2542b532012-08-31 16:01:23 +0200222 return 1;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200223 }
224
Willy Tarreau87b09662015-04-03 00:22:06 +0200225 /* OK let's complete stream initialization since there is no handshake */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200226 cli_conn->flags |= CO_FL_CONNECTED;
Willy Tarreau87b09662015-04-03 00:22:06 +0200227 ret = stream_complete(s);
Willy Tarreau2542b532012-08-31 16:01:23 +0200228 if (ret > 0)
229 return ret;
230
231 /* Error unrolling */
232 out_free_task:
233 task_free(t);
234 out_free_session:
Willy Tarreaufeb76402015-04-03 14:10:06 +0200235 pool_free2(pool2_session, s->sess);
236 out_free_stream:
Willy Tarreau2542b532012-08-31 16:01:23 +0200237 p->feconn--;
Willy Tarreau87b09662015-04-03 00:22:06 +0200238 stream_store_counters(s);
239 pool_free2(pool2_stream, s);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200240 out_free_conn:
241 cli_conn->flags &= ~CO_FL_XPRT_TRACKED;
242 conn_xprt_close(cli_conn);
243 conn_free(cli_conn);
Willy Tarreau2542b532012-08-31 16:01:23 +0200244 out_close:
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200245 if (ret < 0 && l->xprt == &raw_sock && p->mode == PR_MODE_HTTP) {
Willy Tarreau2542b532012-08-31 16:01:23 +0200246 /* critical error, no more memory, try to emit a 500 response */
Willy Tarreau05bf5e12013-10-20 23:10:28 +0200247 struct chunk *err_msg = &p->errmsg[HTTP_ERR_500];
248 if (!err_msg->str)
249 err_msg = &http_err_chunks[HTTP_ERR_500];
Willy Tarreau2542b532012-08-31 16:01:23 +0200250 send(cfd, err_msg->str, err_msg->len, MSG_DONTWAIT|MSG_NOSIGNAL);
251 }
252
253 if (fdtab[cfd].owner)
254 fd_delete(cfd);
255 else
256 close(cfd);
257 return ret;
258}
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100259
Willy Tarreau0af29122012-12-03 15:35:00 +0100260
Willy Tarreau87b09662015-04-03 00:22:06 +0200261/* prepare the trash with a log prefix for stream <s>. It only works with
262 * embryonic streams based on a real connection. This function requires that
Willy Tarreau1f52bb22015-04-04 14:28:46 +0200263 * at sess->origin points to the incoming connection.
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200264 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200265static void prepare_mini_sess_log_prefix(struct stream *s)
Willy Tarreau0af29122012-12-03 15:35:00 +0100266{
267 struct tm tm;
268 char pn[INET6_ADDRSTRLEN];
269 int ret;
270 char *end;
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200271 struct session *sess = s->sess;
Willy Tarreau1f52bb22015-04-04 14:28:46 +0200272 struct connection *cli_conn = __objt_conn(sess->origin);
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)
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200278 chunk_printf(&trash, "%s:%d [", pn, sess->listener->luid);
Willy Tarreau0af29122012-12-03 15:35:00 +0100279 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;
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200285 if (sess->listener->name)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200286 chunk_appendf(&trash, "] %s/%s", sess->fe->id, sess->listener->name);
Willy Tarreau0af29122012-12-03 15:35:00 +0100287 else
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200288 chunk_appendf(&trash, "] %s/%d", sess->fe->id, sess->listener->luid);
Willy Tarreau0af29122012-12-03 15:35:00 +0100289}
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
Willy Tarreau1f52bb22015-04-04 14:28:46 +0200294 * at sess->origin 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 Tarreaufb0afa72015-04-03 14:46:27 +0200299 struct session *sess = s->sess;
Willy Tarreau1f52bb22015-04-04 14:28:46 +0200300 struct connection *conn = __objt_conn(sess->origin);
Willy Tarreau0af29122012-12-03 15:35:00 +0100301 unsigned int log = s->logs.logwait;
302 const char *err_msg;
303
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200304 if (sess->fe->options2 & PR_O2_LOGERRORS)
Willy Tarreau0af29122012-12-03 15:35:00 +0100305 level = LOG_ERR;
306
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200307 if (log && (sess->fe->options & PR_O_NULLNOLOG)) {
Willy Tarreau0af29122012-12-03 15:35:00 +0100308 /* with "option dontlognull", we don't log connections with no transfer */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100309 if (!conn->err_code ||
Willy Tarreau20879a02012-12-03 16:32:10 +0100310 conn->err_code == CO_ER_PRX_EMPTY || conn->err_code == CO_ER_PRX_ABORT ||
311 conn->err_code == CO_ER_SSL_EMPTY || conn->err_code == CO_ER_SSL_ABORT)
Willy Tarreau0af29122012-12-03 15:35:00 +0100312 log = 0;
313 }
314
315 if (log) {
316 if (!conn->err_code && (s->task->state & TASK_WOKEN_TIMER)) {
317 if (conn->flags & CO_FL_ACCEPT_PROXY)
318 conn->err_code = CO_ER_PRX_TIMEOUT;
319 else if (conn->flags & CO_FL_SSL_WAIT_HS)
320 conn->err_code = CO_ER_SSL_TIMEOUT;
321 }
322
323 prepare_mini_sess_log_prefix(s);
324 err_msg = conn_err_code_str(conn);
325 if (err_msg)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200326 send_log(sess->fe, level, "%s: %s\n", trash.str, err_msg);
Willy Tarreau0af29122012-12-03 15:35:00 +0100327 else
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200328 send_log(sess->fe, level, "%s: unknown connection error (code=%d flags=%08x)\n",
Willy Tarreau0af29122012-12-03 15:35:00 +0100329 trash.str, conn->err_code, conn->flags);
330 }
331
Willy Tarreau2542b532012-08-31 16:01:23 +0200332 /* kill the connection now */
Willy Tarreauf79c8172013-10-21 16:30:56 +0200333 conn_force_close(conn);
Willy Tarreaua23ee3a2014-02-05 00:18:47 +0100334 conn_free(conn);
Willy Tarreau2542b532012-08-31 16:01:23 +0200335
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200336 sess->fe->feconn--;
Willy Tarreau87b09662015-04-03 00:22:06 +0200337 stream_store_counters(s);
Willy Tarreau2542b532012-08-31 16:01:23 +0200338
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200339 if (!(sess->listener->options & LI_O_UNLIMITED))
Willy Tarreau2542b532012-08-31 16:01:23 +0200340 actconn--;
341 jobs--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200342 sess->listener->nbconn--;
343 if (sess->listener->state == LI_FULL)
344 resume_listener(sess->listener);
Willy Tarreau2542b532012-08-31 16:01:23 +0200345
346 /* Dequeues all of the listeners waiting for a resource */
347 if (!LIST_ISEMPTY(&global_listener_queue))
348 dequeue_all_listeners(&global_listener_queue);
349
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200350 if (!LIST_ISEMPTY(&sess->fe->listener_queue) &&
351 (!sess->fe->fe_sps_lim || freq_ctr_remain(&sess->fe->fe_sess_per_sec, sess->fe->fe_sps_lim, 0) > 0))
352 dequeue_all_listeners(&sess->fe->listener_queue);
Willy Tarreau2542b532012-08-31 16:01:23 +0200353
354 task_delete(s->task);
355 task_free(s->task);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200356 /* FIXME: for now we have a 1:1 relation between stream and session so
357 * the stream must free the session.
358 */
359 pool_free2(pool2_session, s->sess);
Willy Tarreau87b09662015-04-03 00:22:06 +0200360 pool_free2(pool2_stream, s);
Willy Tarreau2542b532012-08-31 16:01:23 +0200361}
362
Willy Tarreau87b09662015-04-03 00:22:06 +0200363/* Finish initializing a stream from a connection, or kills it if the
Willy Tarreau22cda212012-08-31 17:43:29 +0200364 * connection shows and error. Returns <0 if the connection was killed.
Willy Tarreau2542b532012-08-31 16:01:23 +0200365 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200366static int conn_stream_complete(struct connection *conn)
Willy Tarreau2542b532012-08-31 16:01:23 +0200367{
Willy Tarreau87b09662015-04-03 00:22:06 +0200368 struct stream *s = conn->owner;
Willy Tarreau2542b532012-08-31 16:01:23 +0200369
Willy Tarreau87b09662015-04-03 00:22:06 +0200370 if (!(conn->flags & CO_FL_ERROR) && (stream_complete(s) > 0)) {
Willy Tarreau071e1372012-10-03 01:39:48 +0200371 conn->flags &= ~CO_FL_INIT_DATA;
Willy Tarreau2542b532012-08-31 16:01:23 +0200372 return 0;
373 }
374
375 /* kill the connection now */
376 kill_mini_session(s);
377 return -1;
378}
379
Willy Tarreau87b09662015-04-03 00:22:06 +0200380/* Update an embryonic stream status. The connection is killed in case of
Willy Tarreau9683e9a2012-10-03 21:17:23 +0200381 * error, and <0 will be returned. Otherwise it does nothing.
382 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200383static int conn_stream_update(struct connection *conn)
Willy Tarreau9683e9a2012-10-03 21:17:23 +0200384{
385 if (conn->flags & CO_FL_ERROR) {
386 kill_mini_session(conn->owner);
387 return -1;
388 }
389 return 0;
390}
391
Willy Tarreau87b09662015-04-03 00:22:06 +0200392/* Manages embryonic streams timeout. It is only called when the timeout
Willy Tarreau2542b532012-08-31 16:01:23 +0200393 * strikes and performs the required cleanup.
394 */
395static struct task *expire_mini_session(struct task *t)
396{
Willy Tarreau87b09662015-04-03 00:22:06 +0200397 struct stream *s = t->context;
Willy Tarreau2542b532012-08-31 16:01:23 +0200398
399 if (!(t->state & TASK_WOKEN_TIMER))
400 return t;
401
402 kill_mini_session(s);
403 return NULL;
404}
405
406/* This function is called from the I/O handler which detects the end of
Willy Tarreau87b09662015-04-03 00:22:06 +0200407 * handshake, in order to complete initialization of a valid stream. It must
408 * be called with an embryonic stream. It returns a positive value upon
Willy Tarreau2542b532012-08-31 16:01:23 +0200409 * success, 0 if the connection can be ignored, or a negative value upon
410 * critical failure. The accepted file descriptor is closed if we return <= 0.
Willy Tarreaub4f98092014-05-08 21:06:11 +0200411 * The client-side end point is assumed to be a connection, whose pointer is
Willy Tarreau1f52bb22015-04-04 14:28:46 +0200412 * taken from sess->origin which is assumed to be valid.
Willy Tarreau2542b532012-08-31 16:01:23 +0200413 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200414int stream_complete(struct stream *s)
Willy Tarreau2542b532012-08-31 16:01:23 +0200415{
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200416 struct session *sess = s->sess;
417 struct listener *l = sess->listener;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200418 struct proxy *p = sess->fe;
Willy Tarreau2542b532012-08-31 16:01:23 +0200419 struct task *t = s->task;
Willy Tarreau1f52bb22015-04-04 14:28:46 +0200420 struct connection *conn = __objt_conn(sess->origin);
Willy Tarreau2542b532012-08-31 16:01:23 +0200421 int ret;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100422 int i;
Willy Tarreau2542b532012-08-31 16:01:23 +0200423
424 ret = -1; /* assume unrecoverable error by default */
425
Willy Tarreau87b09662015-04-03 00:22:06 +0200426 /* OK, we're keeping the stream, so let's properly initialize the stream */
427 LIST_ADDQ(&streams, &s->list);
Willy Tarreau2542b532012-08-31 16:01:23 +0200428 LIST_INIT(&s->back_refs);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100429 LIST_INIT(&s->buffer_wait);
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200430
Willy Tarreaue7dff022015-04-03 01:14:29 +0200431 s->flags |= SF_INITIALIZED;
Willy Tarreau2542b532012-08-31 16:01:23 +0200432 s->unique_id = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200433
434 t->process = l->handler;
435 t->context = s;
436 t->expire = TICK_ETERNITY;
437
Willy Tarreau87b09662015-04-03 00:22:06 +0200438 /* Note: initially, the stream's backend points to the frontend.
Willy Tarreau2542b532012-08-31 16:01:23 +0200439 * This changes later when switching rules are executed or
440 * when the default backend is assigned.
441 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200442 s->be = sess->fe;
William Lallemand82fe75c2012-10-23 10:25:10 +0200443 s->comp_algo = NULL;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100444 s->req.buf = s->res.buf = NULL;
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200445 s->req_cap = NULL;
446 s->res_cap = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200447
Willy Tarreau87b09662015-04-03 00:22:06 +0200448 /* Let's count a stream now */
Willy Tarreaub36b4242010-06-04 20:59:39 +0200449 proxy_inc_fe_sess_ctr(l, p);
Willy Tarreau91c43d72010-06-20 11:19:22 +0200450
Willy Tarreaub4c84932013-07-23 19:15:30 +0200451 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200452 void *ptr;
453
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100454 if (!stkctr_entry(&s->stkctr[i]))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100455 continue;
456
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100457 ptr = stktable_data_ptr(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]), STKTABLE_DT_SESS_CNT);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200458 if (ptr)
459 stktable_data_cast(ptr, sess_cnt)++;
460
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100461 ptr = stktable_data_ptr(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]), STKTABLE_DT_SESS_RATE);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200462 if (ptr)
463 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau20d46a52012-12-09 15:55:40 +0100464 s->stkctr[i].table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200465 }
466
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200467 /* this part should be common with other protocols */
Willy Tarreau819d3322014-11-28 12:12:34 +0100468 si_reset(&s->si[0]);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200469 si_set_state(&s->si[0], SI_ST_EST);
470
Willy Tarreau1f52bb22015-04-04 14:28:46 +0200471 /* attach the incoming connection to the stream interface now. */
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200472 si_attach_conn(&s->si[0], conn);
473
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200474 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200475 s->si[0].flags |= SI_FL_INDEP_STR;
476
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200477 /* pre-initialize the other side's stream interface to an INIT state. The
478 * callbacks will be initialized before attempting to connect.
479 */
Willy Tarreau819d3322014-11-28 12:12:34 +0100480 si_reset(&s->si[1]);
Willy Tarreau2a6e8802013-10-24 15:50:53 +0200481 si_detach(&s->si[1]);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200482
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200483 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200484 s->si[1].flags |= SI_FL_INDEP_STR;
485
Willy Tarreau87b09662015-04-03 00:22:06 +0200486 stream_init_srv_conn(s);
Willy Tarreau10b688f2015-03-13 16:43:12 +0100487 s->target = l->default_target; /* used by peers and CLI */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200488 s->pend_pos = NULL;
489
490 /* init store persistence */
491 s->store_count = 0;
492
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100493 channel_init(&s->req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100494 s->req.flags |= CF_READ_ATTACHED; /* the producer is already connected */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200495
496 /* activate default analysers enabled for this listener */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100497 s->req.analysers = l->analysers;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200498
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100499 s->req.wto = TICK_ETERNITY;
500 s->req.rto = TICK_ETERNITY;
501 s->req.rex = TICK_ETERNITY;
502 s->req.wex = TICK_ETERNITY;
503 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200504
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100505 channel_init(&s->res);
Willy Tarreauef573c02014-11-28 14:17:09 +0100506 s->res.flags |= CF_ISRESP;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100507 s->res.analysers = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200508
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200509 if (sess->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100510 s->req.flags |= CF_NEVER_WAIT;
511 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +0200512 }
513
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100514 s->res.rto = TICK_ETERNITY;
515 s->res.wto = TICK_ETERNITY;
516 s->res.rex = TICK_ETERNITY;
517 s->res.wex = TICK_ETERNITY;
518 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200519
Willy Tarreaueee5b512015-04-03 23:46:31 +0200520 s->txn = NULL;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100521
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100522 HLUA_INIT(&s->hlua);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100523
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200524 /* finish initialization of the accepted file descriptor */
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200525 conn_data_want_recv(conn);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200526
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100527 if (p->accept && (ret = p->accept(s)) <= 0) {
528 /* Either we had an unrecoverable error (<0) or work is
529 * finished (=0, eg: monitoring), in both situations,
530 * we can release everything and close.
531 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100532 goto out_free_strm;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200533 }
534
Willy Tarreau93dbc2b2012-10-12 18:01:49 +0200535 /* if logs require transport layer information, note it on the connection */
536 if (s->logs.logwait & LW_XPRT)
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200537 conn->flags |= CO_FL_XPRT_TRACKED;
Willy Tarreau93dbc2b2012-10-12 18:01:49 +0200538
Willy Tarreau2542b532012-08-31 16:01:23 +0200539 /* we want the connection handler to notify the stream interface about updates. */
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200540 conn->flags |= CO_FL_WAKE_DATA;
Willy Tarreau2542b532012-08-31 16:01:23 +0200541
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200542 /* it is important not to call the wakeup function directly but to
543 * pass through task_wakeup(), because this one knows how to apply
544 * priorities to tasks.
545 */
546 task_wakeup(t, TASK_WOKEN_INIT);
547 return 1;
548
549 /* Error unrolling */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100550 out_free_strm:
Willy Tarreaub4f98092014-05-08 21:06:11 +0200551 /* and restore the connection pointer in case we destroyed it,
552 * because kill_mini_session() will need it.
553 */
Willy Tarreau3b246412014-11-25 17:10:33 +0100554 LIST_DEL(&s->list);
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100555 return ret;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200556}
557
Willy Tarreaubaaee002006-06-26 02:48:02 +0200558/*
Willy Tarreau87b09662015-04-03 00:22:06 +0200559 * frees the context associated to a stream. It must have been removed first.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200560 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200561static void stream_free(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200562{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200563 struct session *sess = strm_sess(s);
564 struct proxy *fe = sess->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100565 struct bref *bref, *back;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200566 struct connection *cli_conn = objt_conn(sess->origin);
Willy Tarreaua4cda672010-06-06 18:28:49 +0200567 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100568
Willy Tarreaubaaee002006-06-26 02:48:02 +0200569 if (s->pend_pos)
570 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100571
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100572 if (objt_server(s->target)) { /* there may be requests left pending in queue */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200573 if (s->flags & SF_CURR_SESS) {
574 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100575 objt_server(s->target)->cur_sess--;
Willy Tarreau1e62de62008-11-11 20:20:02 +0100576 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100577 if (may_dequeue_tasks(objt_server(s->target), s->be))
578 process_srv_queue(objt_server(s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100579 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100580
Willy Tarreau7c669d72008-06-20 15:04:11 +0200581 if (unlikely(s->srv_conn)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200582 /* the stream still has a reserved slot on a server, but
Willy Tarreau7c669d72008-06-20 15:04:11 +0200583 * it should normally be only the same as the one above,
584 * so this should not happen in fact.
585 */
586 sess_change_server(s, NULL);
587 }
588
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100589 if (s->req.pipe)
590 put_pipe(s->req.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100591
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100592 if (s->res.pipe)
593 put_pipe(s->res.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100594
Willy Tarreaubf883e02014-11-25 21:10:35 +0100595 /* We may still be present in the buffer wait queue */
596 if (!LIST_ISEMPTY(&s->buffer_wait)) {
597 LIST_DEL(&s->buffer_wait);
598 LIST_INIT(&s->buffer_wait);
599 }
600
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100601 b_drop(&s->req.buf);
602 b_drop(&s->res.buf);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100603 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200604 stream_offer_buffers();
Willy Tarreau9b28e032012-10-12 23:49:43 +0200605
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100606 hlua_ctx_destroy(&s->hlua);
Willy Tarreaueee5b512015-04-03 23:46:31 +0200607 if (s->txn)
608 http_end_txn(s);
Willy Tarreau46023632010-01-07 22:51:47 +0100609
Willy Tarreau1e954912012-10-12 17:50:05 +0200610 /* ensure the client-side transport layer is destroyed */
Willy Tarreauf79c8172013-10-21 16:30:56 +0200611 if (cli_conn)
612 conn_force_close(cli_conn);
Willy Tarreau1e954912012-10-12 17:50:05 +0200613
Willy Tarreaua4cda672010-06-06 18:28:49 +0200614 for (i = 0; i < s->store_count; i++) {
615 if (!s->store[i].ts)
616 continue;
617 stksess_free(s->store[i].table, s->store[i].ts);
618 s->store[i].ts = NULL;
619 }
620
Willy Tarreaueee5b512015-04-03 23:46:31 +0200621 if (s->txn) {
622 pool_free2(pool2_hdr_idx, s->txn->hdr_idx.v);
623 pool_free2(pool2_http_txn, s->txn);
624 s->txn = NULL;
625 }
626
Willy Tarreau92fb9832007-10-16 17:34:28 +0200627 if (fe) {
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200628 pool_free2(fe->rsp_cap_pool, s->res_cap);
629 pool_free2(fe->req_cap_pool, s->req_cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200630 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100631
Willy Tarreau87b09662015-04-03 00:22:06 +0200632 stream_store_counters(s);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200633
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100634 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100635 /* we have to unlink all watchers. We must not relink them if
Willy Tarreau87b09662015-04-03 00:22:06 +0200636 * this stream was the last one in the list.
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100637 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100638 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100639 LIST_INIT(&bref->users);
Willy Tarreau87b09662015-04-03 00:22:06 +0200640 if (s->list.n != &streams)
641 LIST_ADDQ(&LIST_ELEM(s->list.n, struct stream *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100642 bref->ref = s->list.n;
643 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100644 LIST_DEL(&s->list);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200645 si_release_endpoint(&s->si[1]);
646 si_release_endpoint(&s->si[0]);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200647
648 /* FIXME: for now we have a 1:1 relation between stream and session so
649 * the stream must free the session.
650 */
651 pool_free2(pool2_session, s->sess);
Willy Tarreau87b09662015-04-03 00:22:06 +0200652 pool_free2(pool2_stream, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200653
654 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200655 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +0200656 pool_flush2(pool2_buffer);
Willy Tarreau63986c72015-04-03 22:55:33 +0200657 pool_flush2(pool2_http_txn);
Willy Tarreau34eb6712011-10-24 18:15:04 +0200658 pool_flush2(pool2_hdr_idx);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200659 pool_flush2(pool2_requri);
660 pool_flush2(pool2_capture);
Willy Tarreau87b09662015-04-03 00:22:06 +0200661 pool_flush2(pool2_stream);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200662 pool_flush2(pool2_session);
Willy Tarreau3a5e0602014-11-13 16:46:28 +0100663 pool_flush2(pool2_connection);
664 pool_flush2(pool2_pendconn);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200665 pool_flush2(fe->req_cap_pool);
666 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200667 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200668}
669
Willy Tarreau78955f42014-12-28 13:09:02 +0100670/* Allocates a receive buffer for channel <chn>, but only if it's guaranteed
671 * that it's not the last available buffer or it's the response buffer. Unless
672 * the buffer is the response buffer, an extra control is made so that we always
673 * keep <tune.buffers.reserved> buffers available after this allocation. To be
674 * called at the beginning of recv() callbacks to ensure that the required
675 * buffers are properly allocated. Returns 0 in case of failure, non-zero
676 * otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100677 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200678int stream_alloc_recv_buffer(struct channel *chn)
Willy Tarreau656859d2014-11-25 19:46:36 +0100679{
Willy Tarreau87b09662015-04-03 00:22:06 +0200680 struct stream *s;
Willy Tarreau656859d2014-11-25 19:46:36 +0100681 struct buffer *b;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100682 int margin = 0;
Willy Tarreau656859d2014-11-25 19:46:36 +0100683
Willy Tarreau78955f42014-12-28 13:09:02 +0100684 if (!(chn->flags & CF_ISRESP))
Willy Tarreaua24adf02014-11-27 01:11:56 +0100685 margin = global.tune.reserved_bufs;
686
Willy Tarreau78955f42014-12-28 13:09:02 +0100687 s = chn_sess(chn);
688
689 b = b_alloc_margin(&chn->buf, margin);
Willy Tarreau656859d2014-11-25 19:46:36 +0100690 if (b)
691 return 1;
692
Willy Tarreaubf883e02014-11-25 21:10:35 +0100693 if (LIST_ISEMPTY(&s->buffer_wait))
694 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100695 return 0;
696}
697
Willy Tarreau87b09662015-04-03 00:22:06 +0200698/* Allocates a work buffer for stream <s>. It is meant to be called inside
699 * process_stream(). It will only allocate the side needed for the function
Willy Tarreaua24adf02014-11-27 01:11:56 +0100700 * to work fine. For a regular connection, only the response is needed so that
701 * an error message may be built and returned. In case where the initiator is
702 * an applet (eg: peers), then we need to allocate the request buffer for the
703 * applet to be able to send its data (in this case a response is not needed).
704 * Request buffers are never picked from the reserved pool, but response
705 * buffers may be allocated from the reserve. This is critical to ensure that
706 * a response may always flow and will never block a server from releasing a
707 * connection. Returns 0 in case of failure, non-zero otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100708 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200709int stream_alloc_work_buffer(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100710{
Willy Tarreaua24adf02014-11-27 01:11:56 +0100711 int margin;
712 struct buffer **buf;
713
714 if (objt_appctx(s->si[0].end)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100715 buf = &s->req.buf;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100716 margin = global.tune.reserved_bufs;
717 }
718 else {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100719 buf = &s->res.buf;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100720 margin = 0;
721 }
722
Willy Tarreaubf883e02014-11-25 21:10:35 +0100723 if (!LIST_ISEMPTY(&s->buffer_wait)) {
724 LIST_DEL(&s->buffer_wait);
725 LIST_INIT(&s->buffer_wait);
726 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100727
Willy Tarreaua24adf02014-11-27 01:11:56 +0100728 if (b_alloc_margin(buf, margin))
Willy Tarreau656859d2014-11-25 19:46:36 +0100729 return 1;
730
Willy Tarreaubf883e02014-11-25 21:10:35 +0100731 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100732 return 0;
733}
734
735/* releases unused buffers after processing. Typically used at the end of the
Willy Tarreaubf883e02014-11-25 21:10:35 +0100736 * update() functions. It will try to wake up as many tasks as the number of
Willy Tarreau87b09662015-04-03 00:22:06 +0200737 * buffers that it releases. In practice, most often streams are blocked on
Willy Tarreaubf883e02014-11-25 21:10:35 +0100738 * a single buffer, so it makes sense to try to wake two up when two buffers
739 * are released at once.
Willy Tarreau656859d2014-11-25 19:46:36 +0100740 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200741void stream_release_buffers(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100742{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100743 if (s->req.buf->size && buffer_empty(s->req.buf))
744 b_free(&s->req.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100745
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100746 if (s->res.buf->size && buffer_empty(s->res.buf))
747 b_free(&s->res.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100748
Willy Tarreaubf883e02014-11-25 21:10:35 +0100749 /* if we're certain to have at least 1 buffer available, and there is
750 * someone waiting, we can wake up a waiter and offer them.
751 */
Willy Tarreaua24adf02014-11-27 01:11:56 +0100752 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200753 stream_offer_buffers();
Willy Tarreaubf883e02014-11-25 21:10:35 +0100754}
755
Willy Tarreau87b09662015-04-03 00:22:06 +0200756/* Runs across the list of pending streams waiting for a buffer and wakes one
Willy Tarreaua24adf02014-11-27 01:11:56 +0100757 * up if buffers are available. Will stop when the run queue reaches <rqlimit>.
Willy Tarreau87b09662015-04-03 00:22:06 +0200758 * Should not be called directly, use stream_offer_buffers() instead.
Willy Tarreaubf883e02014-11-25 21:10:35 +0100759 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200760void __stream_offer_buffers(int rqlimit)
Willy Tarreaubf883e02014-11-25 21:10:35 +0100761{
Willy Tarreau87b09662015-04-03 00:22:06 +0200762 struct stream *sess, *bak;
Willy Tarreaubf883e02014-11-25 21:10:35 +0100763
764 list_for_each_entry_safe(sess, bak, &buffer_wq, buffer_wait) {
Willy Tarreaua24adf02014-11-27 01:11:56 +0100765 if (rqlimit <= run_queue)
766 break;
767
Willy Tarreaubf883e02014-11-25 21:10:35 +0100768 if (sess->task->state & TASK_RUNNING)
769 continue;
770
771 LIST_DEL(&sess->buffer_wait);
772 LIST_INIT(&sess->buffer_wait);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100773 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100774 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100775}
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200776
777/* perform minimal intializations, report 0 in case of error, 1 if OK. */
Willy Tarreau87b09662015-04-03 00:22:06 +0200778int init_stream()
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200779{
Willy Tarreau87b09662015-04-03 00:22:06 +0200780 LIST_INIT(&streams);
781 pool2_stream = create_pool("stream", sizeof(struct stream), MEM_F_SHARED);
782 return pool2_stream != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200783}
784
Willy Tarreau87b09662015-04-03 00:22:06 +0200785void stream_process_counters(struct stream *s)
Willy Tarreau30e71012007-11-26 20:15:35 +0100786{
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200787 struct session *sess = s->sess;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100788 unsigned long long bytes;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100789 void *ptr;
790 int i;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100791
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100792 bytes = s->req.total - s->logs.bytes_in;
793 s->logs.bytes_in = s->req.total;
794 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200795 sess->fe->fe_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100796
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100797 s->be->be_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100798
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100799 if (objt_server(s->target))
800 objt_server(s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200801
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200802 if (sess->listener && sess->listener->counters)
803 sess->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200804
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100805 for (i = 0; i < MAX_SESS_STKCTR; i++) {
806 if (!stkctr_entry(&s->stkctr[i]))
807 continue;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200808
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100809 ptr = stktable_data_ptr(s->stkctr[i].table,
810 stkctr_entry(&s->stkctr[i]),
811 STKTABLE_DT_BYTES_IN_CNT);
812 if (ptr)
813 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200814
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100815 ptr = stktable_data_ptr(s->stkctr[i].table,
816 stkctr_entry(&s->stkctr[i]),
817 STKTABLE_DT_BYTES_IN_RATE);
818 if (ptr)
819 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
820 s->stkctr[i].table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreau30e71012007-11-26 20:15:35 +0100821 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100822 }
823
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100824 bytes = s->res.total - s->logs.bytes_out;
825 s->logs.bytes_out = s->res.total;
826 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200827 sess->fe->fe_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100828
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100829 s->be->be_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100830
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100831 if (objt_server(s->target))
832 objt_server(s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200833
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200834 if (sess->listener && sess->listener->counters)
835 sess->listener->counters->bytes_out += bytes;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200836
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100837 for (i = 0; i < MAX_SESS_STKCTR; i++) {
838 if (!stkctr_entry(&s->stkctr[i]))
839 continue;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200840
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100841 ptr = stktable_data_ptr(s->stkctr[i].table,
842 stkctr_entry(&s->stkctr[i]),
843 STKTABLE_DT_BYTES_OUT_CNT);
844 if (ptr)
845 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200846
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100847 ptr = stktable_data_ptr(s->stkctr[i].table,
848 stkctr_entry(&s->stkctr[i]),
849 STKTABLE_DT_BYTES_OUT_RATE);
850 if (ptr)
851 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
852 s->stkctr[i].table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreau30e71012007-11-26 20:15:35 +0100853 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100854 }
855}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200856
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100857/* This function is called with (si->state == SI_ST_CON) meaning that a
858 * connection was attempted and that the file descriptor is already allocated.
859 * We must check for establishment, error and abort. Possible output states
860 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
861 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
Willy Tarreau87b09662015-04-03 00:22:06 +0200862 * otherwise 1. This only works with connection-based streams.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100863 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200864static int sess_update_st_con_tcp(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100865{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100866 struct stream_interface *si = &s->si[1];
867 struct channel *req = &s->req;
868 struct channel *rep = &s->res;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200869 struct connection *srv_conn = __objt_conn(si->end);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100870
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100871 /* If we got an error, or if nothing happened and the connection timed
872 * out, we must give up. The CER state handler will take care of retry
873 * attempts and error reports.
874 */
875 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau103197d2014-11-28 15:26:12 +0100876 if (unlikely(req->flags & CF_WRITE_PARTIAL)) {
Willy Tarreaue3224e82012-10-29 22:41:31 +0100877 /* Some data were sent past the connection establishment,
878 * so we need to pretend we're established to log correctly
879 * and let later states handle the failure.
880 */
Willy Tarreaue3224e82012-10-29 22:41:31 +0100881 si->state = SI_ST_EST;
882 si->err_type = SI_ET_DATA_ERR;
Willy Tarreau103197d2014-11-28 15:26:12 +0100883 rep->flags |= CF_READ_ERROR | CF_WRITE_ERROR;
Willy Tarreaue3224e82012-10-29 22:41:31 +0100884 return 1;
885 }
Willy Tarreau127334e2009-03-28 10:47:26 +0100886 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100887 si->state = SI_ST_CER;
Willy Tarreau0ede5a32012-12-08 08:44:02 +0100888
Willy Tarreauf79c8172013-10-21 16:30:56 +0200889 conn_force_close(srv_conn);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100890
891 if (si->err_type)
892 return 0;
893
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100894 if (si->flags & SI_FL_ERR)
895 si->err_type = SI_ET_CONN_ERR;
896 else
897 si->err_type = SI_ET_CONN_TO;
898 return 0;
899 }
900
901 /* OK, maybe we want to abort */
Willy Tarreaua7a7ebc2012-12-30 00:50:35 +0100902 if (!(req->flags & CF_WRITE_PARTIAL) &&
903 unlikely((rep->flags & CF_SHUTW) ||
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200904 ((req->flags & CF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
905 ((!(req->flags & CF_WRITE_ACTIVITY) && channel_is_empty(req)) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100906 s->be->options & PR_O_ABRT_CLOSE)))) {
907 /* give up */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200908 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100909 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau84455332009-03-15 22:34:05 +0100910 if (s->srv_error)
911 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100912 return 1;
913 }
914
915 /* we need to wait a bit more if there was no activity either */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200916 if (!(req->flags & CF_WRITE_ACTIVITY))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100917 return 1;
918
919 /* OK, this means that a connection succeeded. The caller will be
920 * responsible for handling the transition from CON to EST.
921 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100922 si->state = SI_ST_EST;
923 si->err_type = SI_ET_NONE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100924 return 1;
925}
926
927/* This function is called with (si->state == SI_ST_CER) meaning that a
928 * previous connection attempt has failed and that the file descriptor
929 * has already been released. Possible causes include asynchronous error
930 * notification and time out. Possible output states are SI_ST_CLO when
931 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
932 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
933 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
934 * marked as in error state. It returns 0.
935 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200936static int sess_update_st_cer(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100937{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100938 struct stream_interface *si = &s->si[1];
939
Willy Tarreau87b09662015-04-03 00:22:06 +0200940 /* we probably have to release last stream from the server */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100941 if (objt_server(s->target)) {
942 health_adjust(objt_server(s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100943
Willy Tarreaue7dff022015-04-03 01:14:29 +0200944 if (s->flags & SF_CURR_SESS) {
945 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100946 objt_server(s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100947 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100948 }
949
950 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200951 si->conn_retries--;
952 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100953 if (!si->err_type) {
954 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100955 }
956
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100957 if (objt_server(s->target))
958 objt_server(s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100959 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100960 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100961 if (may_dequeue_tasks(objt_server(s->target), s->be))
962 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100963
964 /* shutw is enough so stop a connecting socket */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200965 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100966 s->req.flags |= CF_WRITE_ERROR;
967 s->res.flags |= CF_READ_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100968
969 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100970 if (s->srv_error)
971 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100972 return 0;
973 }
974
975 /* If the "redispatch" option is set on the backend, we are allowed to
976 * retry on another server for the last retry. In order to achieve this,
Willy Tarreau87b09662015-04-03 00:22:06 +0200977 * we must mark the stream unassigned, and eventually clear the DIRECT
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100978 * bit to ignore any persistence cookie. We won't count a retry nor a
979 * redispatch yet, because this will depend on what server is selected.
Willy Tarreau33a14e52014-06-13 17:49:40 +0200980 * If the connection is not persistent, the balancing algorithm is not
981 * determinist (round robin) and there is more than one active server,
982 * we accept to perform an immediate redispatch without waiting since
983 * we don't care about this particular server.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100984 */
Willy Tarreau33a14e52014-06-13 17:49:40 +0200985 if (objt_server(s->target) &&
986 (si->conn_retries == 0 ||
Willy Tarreaue7dff022015-04-03 01:14:29 +0200987 (!(s->flags & SF_DIRECT) && s->be->srv_act > 1 &&
Willy Tarreau33a14e52014-06-13 17:49:40 +0200988 ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR))) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +0200989 s->be->options & PR_O_REDISP && !(s->flags & SF_FORCE_PRST)) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100990 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100991 if (may_dequeue_tasks(objt_server(s->target), s->be))
992 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100993
Willy Tarreaue7dff022015-04-03 01:14:29 +0200994 s->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100995 si->state = SI_ST_REQ;
996 } else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100997 if (objt_server(s->target))
998 objt_server(s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100999 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001000 si->state = SI_ST_ASS;
1001 }
1002
1003 if (si->flags & SI_FL_ERR) {
1004 /* The error was an asynchronous connection error, and we will
1005 * likely have to retry connecting to the same server, most
1006 * likely leading to the same result. To avoid this, we wait
Willy Tarreaub0290662014-06-13 17:04:44 +02001007 * MIN(one second, connect timeout) before retrying.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001008 */
1009
Willy Tarreaub0290662014-06-13 17:04:44 +02001010 int delay = 1000;
1011
1012 if (s->be->timeout.connect && s->be->timeout.connect < delay)
1013 delay = s->be->timeout.connect;
1014
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001015 if (!si->err_type)
1016 si->err_type = SI_ET_CONN_ERR;
1017
Willy Tarreaudb6d0122014-06-13 17:40:15 +02001018 /* only wait when we're retrying on the same server */
1019 if (si->state == SI_ST_ASS ||
1020 (s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_RR ||
1021 (s->be->srv_act <= 1)) {
1022 si->state = SI_ST_TAR;
1023 si->exp = tick_add(now_ms, MS_TO_TICKS(delay));
1024 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001025 return 0;
1026 }
1027 return 0;
1028}
1029
1030/*
1031 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +02001032 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
Willy Tarreau26d8c592012-05-07 18:12:14 +02001033 * SI_ST_INI) to SI_ST_EST, but only when a ->proto is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001034 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001035static void sess_establish(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001036{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001037 struct stream_interface *si = &s->si[1];
1038 struct channel *req = &s->req;
1039 struct channel *rep = &s->res;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001040
Willy Tarreau0e37f1c2013-12-31 23:06:46 +01001041 /* First, centralize the timers information */
1042 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
1043 si->exp = TICK_ETERNITY;
1044
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001045 if (objt_server(s->target))
1046 health_adjust(objt_server(s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001047
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001048 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001049 /* if the user wants to log as soon as possible, without counting
1050 * bytes from the server, then this is the right moment. */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001051 if (!LIST_ISEMPTY(&strm_fe(s)->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001052 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001053 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001054 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001055 }
1056 else {
Willy Tarreaud81ca042013-12-31 22:33:13 +01001057 rep->flags |= CF_READ_DONTWAIT; /* a single read is enough to get response headers */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001058 }
1059
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001060 rep->analysers |= strm_fe(s)->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001061 rep->flags |= CF_READ_ATTACHED; /* producer is now attached */
Willy Tarreau644c1012014-04-30 18:11:11 +02001062 if (req->flags & CF_WAKE_CONNECT) {
1063 req->flags |= CF_WAKE_ONCE;
1064 req->flags &= ~CF_WAKE_CONNECT;
1065 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001066 if (objt_conn(si->end)) {
Willy Tarreaud04e8582010-05-31 12:31:35 +02001067 /* real connections have timeouts */
1068 req->wto = s->be->timeout.server;
1069 rep->rto = s->be->timeout.server;
1070 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001071 req->wex = TICK_ETERNITY;
1072}
1073
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001074/* Update back stream interface status for input states SI_ST_ASS, SI_ST_QUE,
1075 * SI_ST_TAR. Other input states are simply ignored.
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01001076 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON
1077 * and SI_ST_EST. Flags must have previously been updated for timeouts and other
1078 * conditions.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001079 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001080static void sess_update_stream_int(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001081{
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001082 struct server *srv = objt_server(s->target);
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001083 struct stream_interface *si = &s->si[1];
Willy Tarreau103197d2014-11-28 15:26:12 +01001084 struct channel *req = &s->req;
Willy Tarreau827aee92011-03-10 16:55:02 +01001085
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001086 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 +01001087 now_ms, __FUNCTION__,
1088 s,
Willy Tarreau103197d2014-11-28 15:26:12 +01001089 req, s->rep,
1090 req->rex, s->res.wex,
1091 req->flags, s->res.flags,
1092 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 +01001093
1094 if (si->state == SI_ST_ASS) {
1095 /* Server assigned to connection request, we have to try to connect now */
1096 int conn_err;
1097
1098 conn_err = connect_server(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001099 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001100
Willy Tarreaue7dff022015-04-03 01:14:29 +02001101 if (conn_err == SF_ERR_NONE) {
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01001102 /* state = SI_ST_CON or SI_ST_EST now */
Willy Tarreau827aee92011-03-10 16:55:02 +01001103 if (srv)
1104 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001105 if (srv)
1106 srv_set_sess_last(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001107 return;
1108 }
1109
1110 /* We have received a synchronous error. We might have to
1111 * abort, retry immediately or redispatch.
1112 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001113 if (conn_err == SF_ERR_INTERNAL) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001114 if (!si->err_type) {
1115 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001116 }
1117
Willy Tarreau827aee92011-03-10 16:55:02 +01001118 if (srv)
1119 srv_inc_sess_ctr(srv);
1120 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001121 srv_set_sess_last(srv);
1122 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +01001123 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001124 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001125
Willy Tarreau87b09662015-04-03 00:22:06 +02001126 /* release other streams waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +01001127 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001128 if (may_dequeue_tasks(srv, s->be))
1129 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001130
1131 /* Failed and not retryable. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001132 si_shutr(si);
1133 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001134 req->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001135
1136 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1137
Willy Tarreau87b09662015-04-03 00:22:06 +02001138 /* no stream was ever accounted for this server */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001139 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001140 if (s->srv_error)
1141 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001142 return;
1143 }
1144
1145 /* We are facing a retryable error, but we don't want to run a
1146 * turn-around now, as the problem is likely a source port
1147 * allocation problem, so we want to retry now.
1148 */
1149 si->state = SI_ST_CER;
1150 si->flags &= ~SI_FL_ERR;
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001151 sess_update_st_cer(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001152 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
1153 return;
1154 }
1155 else if (si->state == SI_ST_QUE) {
1156 /* connection request was queued, check for any update */
1157 if (!s->pend_pos) {
1158 /* The connection is not in the queue anymore. Either
1159 * we have a server connection slot available and we
1160 * go directly to the assigned state, or we need to
1161 * load-balance first and go to the INI state.
1162 */
1163 si->exp = TICK_ETERNITY;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001164 if (unlikely(!(s->flags & SF_ASSIGNED)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001165 si->state = SI_ST_REQ;
1166 else {
1167 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1168 si->state = SI_ST_ASS;
1169 }
1170 return;
1171 }
1172
1173 /* Connection request still in queue... */
1174 if (si->flags & SI_FL_EXP) {
1175 /* ... and timeout expired */
1176 si->exp = TICK_ETERNITY;
1177 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +01001178 if (srv)
1179 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001180 s->be->be_counters.failed_conns++;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001181 si_shutr(si);
1182 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001183 req->flags |= CF_WRITE_TIMEOUT;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001184 if (!si->err_type)
1185 si->err_type = SI_ET_QUEUE_TO;
1186 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001187 if (s->srv_error)
1188 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001189 return;
1190 }
1191
1192 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau103197d2014-11-28 15:26:12 +01001193 if ((req->flags & (CF_READ_ERROR)) ||
1194 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
1195 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001196 /* give up */
1197 si->exp = TICK_ETERNITY;
1198 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau73b013b2012-05-21 16:31:45 +02001199 si_shutr(si);
1200 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001201 si->err_type |= SI_ET_QUEUE_ABRT;
1202 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001203 if (s->srv_error)
1204 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001205 return;
1206 }
1207
1208 /* Nothing changed */
1209 return;
1210 }
1211 else if (si->state == SI_ST_TAR) {
1212 /* Connection request might be aborted */
Willy Tarreau103197d2014-11-28 15:26:12 +01001213 if ((req->flags & (CF_READ_ERROR)) ||
1214 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
1215 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001216 /* give up */
1217 si->exp = TICK_ETERNITY;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001218 si_shutr(si);
1219 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001220 si->err_type |= SI_ET_CONN_ABRT;
1221 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001222 if (s->srv_error)
1223 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001224 return;
1225 }
1226
1227 if (!(si->flags & SI_FL_EXP))
1228 return; /* still in turn-around */
1229
1230 si->exp = TICK_ETERNITY;
1231
Willy Tarreau87b09662015-04-03 00:22:06 +02001232 /* we keep trying on the same server as long as the stream is
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001233 * marked "assigned".
1234 * FIXME: Should we force a redispatch attempt when the server is down ?
1235 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001236 if (s->flags & SF_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001237 si->state = SI_ST_ASS;
1238 else
1239 si->state = SI_ST_REQ;
1240 return;
1241 }
1242}
1243
Willy Tarreau87b09662015-04-03 00:22:06 +02001244/* Set correct stream termination flags in case no analyser has done it. It
Simon Hormandec5be42011-06-08 09:19:07 +09001245 * also counts a failed request if the server state has not reached the request
1246 * stage.
1247 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001248static void sess_set_term_flags(struct stream *s)
Simon Hormandec5be42011-06-08 09:19:07 +09001249{
Willy Tarreaue7dff022015-04-03 01:14:29 +02001250 if (!(s->flags & SF_FINST_MASK)) {
Simon Hormandec5be42011-06-08 09:19:07 +09001251 if (s->si[1].state < SI_ST_REQ) {
1252
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001253 strm_fe(s)->fe_counters.failed_req++;
1254 if (strm_li(s)->counters)
1255 strm_li(s)->counters->failed_req++;
Simon Hormandec5be42011-06-08 09:19:07 +09001256
Willy Tarreaue7dff022015-04-03 01:14:29 +02001257 s->flags |= SF_FINST_R;
Simon Hormandec5be42011-06-08 09:19:07 +09001258 }
1259 else if (s->si[1].state == SI_ST_QUE)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001260 s->flags |= SF_FINST_Q;
Simon Hormandec5be42011-06-08 09:19:07 +09001261 else if (s->si[1].state < SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001262 s->flags |= SF_FINST_C;
Simon Hormandec5be42011-06-08 09:19:07 +09001263 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001264 s->flags |= SF_FINST_D;
Simon Hormandec5be42011-06-08 09:19:07 +09001265 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02001266 s->flags |= SF_FINST_L;
Simon Hormandec5be42011-06-08 09:19:07 +09001267 }
1268}
1269
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001270/* This function initiates a server connection request on a stream interface
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001271 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS for
1272 * a real connection to a server, indicating that a server has been assigned,
1273 * or SI_ST_EST for a successful connection to an applet. It may also return
1274 * SI_ST_QUE, or SI_ST_CLO upon error.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001275 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001276static void sess_prepare_conn_req(struct stream *s)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001277{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001278 struct stream_interface *si = &s->si[1];
1279
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001280 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 +01001281 now_ms, __FUNCTION__,
1282 s,
1283 s->req, s->rep,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001284 s->req.rex, s->res.wex,
1285 s->req.flags, s->res.flags,
Willy Tarreau350f4872014-11-28 14:42:25 +01001286 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 +01001287
1288 if (si->state != SI_ST_REQ)
1289 return;
1290
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001291 if (unlikely(obj_type(s->target) == OBJ_TYPE_APPLET)) {
1292 /* the applet directly goes to the EST state */
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001293 struct appctx *appctx = objt_appctx(si->end);
1294
1295 if (!appctx || appctx->applet != __objt_applet(s->target))
1296 appctx = stream_int_register_handler(si, objt_applet(s->target));
1297
1298 if (!appctx) {
1299 /* No more memory, let's immediately abort. Force the
1300 * error code to ignore the ERR_LOCAL which is not a
1301 * real error.
1302 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001303 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001304
1305 si_shutr(si);
1306 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001307 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau6bbb2f62013-12-09 17:14:23 +01001308 si->err_type = SI_ET_CONN_RES;
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001309 si->state = SI_ST_CLO;
1310 if (s->srv_error)
1311 s->srv_error(s, si);
1312 return;
1313 }
1314
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001315 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001316 si->state = SI_ST_EST;
1317 si->err_type = SI_ET_NONE;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001318 be_set_sess_last(s->be);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01001319 /* let sess_establish() finish the job */
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001320 return;
1321 }
1322
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001323 /* Try to assign a server */
1324 if (srv_redispatch_connect(s) != 0) {
1325 /* We did not get a server. Either we queued the
1326 * connection request, or we encountered an error.
1327 */
1328 if (si->state == SI_ST_QUE)
1329 return;
1330
1331 /* we did not get any server, let's check the cause */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001332 si_shutr(si);
1333 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001334 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001335 if (!si->err_type)
1336 si->err_type = SI_ET_CONN_OTHER;
1337 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001338 if (s->srv_error)
1339 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001340 return;
1341 }
1342
1343 /* The server is assigned */
1344 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1345 si->state = SI_ST_ASS;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001346 be_set_sess_last(s->be);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001347}
1348
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001349/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +01001350 * if appropriate. The default_backend rule is also considered, then the
1351 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001352 * It returns 1 if the processing can continue on next analysers, or zero if it
1353 * either needs more data or wants to immediately abort the request.
1354 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001355static int process_switching_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001356{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001357 struct persist_rule *prst_rule;
Willy Tarreau192252e2015-04-04 01:47:55 +02001358 struct session *sess = s->sess;
1359 struct proxy *fe = sess->fe;
Willy Tarreau4de91492010-01-22 19:10:05 +01001360
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001361 req->analysers &= ~an_bit;
1362 req->analyse_exp = TICK_ETERNITY;
1363
Willy Tarreau87b09662015-04-03 00:22:06 +02001364 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 +02001365 now_ms, __FUNCTION__,
1366 s,
1367 req,
1368 req->rex, req->wex,
1369 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001370 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001371 req->analysers);
1372
1373 /* now check whether we have some switching rules for this request */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001374 if (!(s->flags & SF_BE_ASSIGNED)) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001375 struct switching_rule *rule;
1376
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001377 list_for_each_entry(rule, &fe->switching_rules, list) {
Willy Tarreauf51658d2014-04-23 01:21:56 +02001378 int ret = 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001379
Willy Tarreauf51658d2014-04-23 01:21:56 +02001380 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001381 ret = acl_exec_cond(rule->cond, fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf51658d2014-04-23 01:21:56 +02001382 ret = acl_pass(ret);
1383 if (rule->cond->pol == ACL_COND_UNLESS)
1384 ret = !ret;
1385 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001386
1387 if (ret) {
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001388 /* If the backend name is dynamic, try to resolve the name.
1389 * If we can't resolve the name, or if any error occurs, break
1390 * the loop and fallback to the default backend.
1391 */
1392 struct proxy *backend;
1393
1394 if (rule->dynamic) {
1395 struct chunk *tmp = get_trash_chunk();
1396 if (!build_logline(s, tmp->str, tmp->size, &rule->be.expr))
1397 break;
1398 backend = findproxy(tmp->str, PR_CAP_BE);
1399 if (!backend)
1400 break;
1401 }
1402 else
1403 backend = rule->be.backend;
1404
Willy Tarreau87b09662015-04-03 00:22:06 +02001405 if (!stream_set_backend(s, backend))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001406 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001407 break;
1408 }
1409 }
1410
1411 /* To ensure correct connection accounting on the backend, we
1412 * have to assign one if it was not set (eg: a listen). This
1413 * measure also takes care of correctly setting the default
1414 * backend if any.
1415 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001416 if (!(s->flags & SF_BE_ASSIGNED))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001417 if (!stream_set_backend(s, fe->defbe.be ? fe->defbe.be : s->be))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001418 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001419 }
1420
Willy Tarreaufb356202010-08-03 14:02:05 +02001421 /* we don't want to run the TCP or HTTP filters again if the backend has not changed */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001422 if (fe == s->be) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001423 s->req.analysers &= ~AN_REQ_INSPECT_BE;
1424 s->req.analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001425 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001426
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001427 /* 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 +02001428 * persistence rule, and report that in the stream.
Willy Tarreau4de91492010-01-22 19:10:05 +01001429 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001430 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001431 int ret = 1;
1432
1433 if (prst_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001434 ret = acl_exec_cond(prst_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau4de91492010-01-22 19:10:05 +01001435 ret = acl_pass(ret);
1436 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1437 ret = !ret;
1438 }
1439
1440 if (ret) {
1441 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001442 if (prst_rule->type == PERSIST_TYPE_FORCE) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001443 s->flags |= SF_FORCE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001444 } else {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001445 s->flags |= SF_IGNORE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001446 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001447 break;
1448 }
1449 }
1450
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001451 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001452
1453 sw_failed:
1454 /* immediately abort this request in case of allocation failure */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001455 channel_abort(&s->req);
1456 channel_abort(&s->res);
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001457
Willy Tarreaue7dff022015-04-03 01:14:29 +02001458 if (!(s->flags & SF_ERR_MASK))
1459 s->flags |= SF_ERR_RESOURCE;
1460 if (!(s->flags & SF_FINST_MASK))
1461 s->flags |= SF_FINST_R;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001462
Willy Tarreaueee5b512015-04-03 23:46:31 +02001463 if (s->txn)
1464 s->txn->status = 500;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001465 s->req.analysers = 0;
1466 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001467 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001468}
1469
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001470/* This stream analyser works on a request. It applies all use-server rules on
1471 * it then returns 1. The data must already be present in the buffer otherwise
1472 * they won't match. It always returns 1.
1473 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001474static int process_server_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001475{
1476 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001477 struct session *sess = s->sess;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001478 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 Tarreau192252e2015-04-04 01:47:55 +02001493 ret = acl_exec_cond(rule->cond, s->be, sess, s, 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;
Willy Tarreau192252e2015-04-04 01:47:55 +02001527 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001528 struct sticking_rule *rule;
1529
Willy Tarreau87b09662015-04-03 00:22:06 +02001530 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 +01001531 now_ms, __FUNCTION__,
1532 s,
1533 req,
1534 req->rex, req->wex,
1535 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001536 req->buf->i,
Emeric Brun1d33b292010-01-04 15:47:17 +01001537 req->analysers);
1538
1539 list_for_each_entry(rule, &px->sticking_rules, list) {
1540 int ret = 1 ;
1541 int i;
1542
Willy Tarreau9667a802013-12-09 12:52:13 +01001543 /* Only the first stick store-request of each table is applied
1544 * and other ones are ignored. The purpose is to allow complex
1545 * configurations which look for multiple entries by decreasing
1546 * order of precision and to stop at the first which matches.
1547 * An example could be a store of the IP address from an HTTP
1548 * header first, then from the source if not found.
1549 */
Emeric Brun1d33b292010-01-04 15:47:17 +01001550 for (i = 0; i < s->store_count; i++) {
1551 if (rule->table.t == s->store[i].table)
1552 break;
1553 }
1554
1555 if (i != s->store_count)
1556 continue;
1557
1558 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001559 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001560 ret = acl_pass(ret);
1561 if (rule->cond->pol == ACL_COND_UNLESS)
1562 ret = !ret;
1563 }
1564
1565 if (ret) {
1566 struct stktable_key *key;
1567
Willy Tarreau192252e2015-04-04 01:47:55 +02001568 key = stktable_fetch_key(rule->table.t, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->expr, NULL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001569 if (!key)
1570 continue;
1571
1572 if (rule->flags & STK_IS_MATCH) {
1573 struct stksess *ts;
1574
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001575 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001576 if (!(s->flags & SF_ASSIGNED)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001577 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001578 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001579
1580 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001581 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1582 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001583 if (node) {
1584 struct server *srv;
1585
1586 srv = container_of(node, struct server, conf.id);
Willy Tarreau892337c2014-05-13 23:41:20 +02001587 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4de91492010-01-22 19:10:05 +01001588 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001589 (s->flags & SF_FORCE_PRST)) {
1590 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001591 s->target = &srv->obj_type;
Emeric Brun1d33b292010-01-04 15:47:17 +01001592 }
1593 }
1594 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001595 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001596 }
1597 }
1598 if (rule->flags & STK_IS_STORE) {
1599 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1600 struct stksess *ts;
1601
1602 ts = stksess_new(rule->table.t, key);
1603 if (ts) {
1604 s->store[s->store_count].table = rule->table.t;
1605 s->store[s->store_count++].ts = ts;
1606 }
1607 }
1608 }
1609 }
1610 }
1611
1612 req->analysers &= ~an_bit;
1613 req->analyse_exp = TICK_ETERNITY;
1614 return 1;
1615}
1616
1617/* This stream analyser works on a response. It applies all store rules on it
1618 * then returns 1. The data must already be present in the buffer otherwise
1619 * they won't match. It always returns 1.
1620 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001621static int process_store_rules(struct stream *s, struct channel *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001622{
1623 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001624 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001625 struct sticking_rule *rule;
1626 int i;
Willy Tarreau9667a802013-12-09 12:52:13 +01001627 int nbreq = s->store_count;
Emeric Brun1d33b292010-01-04 15:47:17 +01001628
Willy Tarreau87b09662015-04-03 00:22:06 +02001629 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 +01001630 now_ms, __FUNCTION__,
1631 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001632 rep,
1633 rep->rex, rep->wex,
1634 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001635 rep->buf->i,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001636 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001637
1638 list_for_each_entry(rule, &px->storersp_rules, list) {
1639 int ret = 1 ;
Emeric Brun1d33b292010-01-04 15:47:17 +01001640
Willy Tarreau9667a802013-12-09 12:52:13 +01001641 /* Only the first stick store-response of each table is applied
1642 * and other ones are ignored. The purpose is to allow complex
1643 * configurations which look for multiple entries by decreasing
1644 * order of precision and to stop at the first which matches.
1645 * An example could be a store of a set-cookie value, with a
1646 * fallback to a parameter found in a 302 redirect.
1647 *
1648 * The store-response rules are not allowed to override the
1649 * store-request rules for the same table, but they may coexist.
1650 * Thus we can have up to one store-request entry and one store-
1651 * response entry for the same table at any time.
1652 */
1653 for (i = nbreq; i < s->store_count; i++) {
1654 if (rule->table.t == s->store[i].table)
1655 break;
1656 }
1657
1658 /* skip existing entries for this table */
1659 if (i < s->store_count)
1660 continue;
1661
Emeric Brun1d33b292010-01-04 15:47:17 +01001662 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001663 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001664 ret = acl_pass(ret);
1665 if (rule->cond->pol == ACL_COND_UNLESS)
1666 ret = !ret;
1667 }
1668
1669 if (ret) {
1670 struct stktable_key *key;
1671
Willy Tarreau192252e2015-04-04 01:47:55 +02001672 key = stktable_fetch_key(rule->table.t, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->expr, NULL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001673 if (!key)
1674 continue;
1675
Willy Tarreau37e340c2013-12-06 23:05:21 +01001676 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001677 struct stksess *ts;
1678
1679 ts = stksess_new(rule->table.t, key);
1680 if (ts) {
1681 s->store[s->store_count].table = rule->table.t;
Emeric Brun1d33b292010-01-04 15:47:17 +01001682 s->store[s->store_count++].ts = ts;
1683 }
1684 }
1685 }
1686 }
1687
1688 /* process store request and store response */
1689 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001690 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001691 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001692
Willy Tarreauc93cd162014-05-13 15:54:22 +02001693 if (objt_server(s->target) && objt_server(s->target)->flags & SRV_F_NON_STICK) {
Simon Hormanfa461682011-06-25 09:39:49 +09001694 stksess_free(s->store[i].table, s->store[i].ts);
1695 s->store[i].ts = NULL;
1696 continue;
1697 }
1698
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001699 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1700 if (ts) {
1701 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001702 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001703 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001704 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001705 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001706 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001707
1708 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001709 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001710 stktable_data_cast(ptr, server_id) = objt_server(s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001711 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001712 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001713
1714 rep->analysers &= ~an_bit;
1715 rep->analyse_exp = TICK_ETERNITY;
1716 return 1;
1717}
1718
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001719/* This macro is very specific to the function below. See the comments in
Willy Tarreau87b09662015-04-03 00:22:06 +02001720 * process_stream() below to understand the logic and the tests.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001721 */
1722#define UPDATE_ANALYSERS(real, list, back, flag) { \
1723 list = (((list) & ~(flag)) | ~(back)) & (real); \
1724 back = real; \
1725 if (!(list)) \
1726 break; \
1727 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1728 continue; \
1729}
1730
Willy Tarreau87b09662015-04-03 00:22:06 +02001731/* Processes the client, server, request and response jobs of a stream task,
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001732 * then puts it back to the wait queue in a clean state, or cleans up its
1733 * resources if it must be deleted. Returns in <next> the date the task wants
1734 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1735 * nothing too many times, the request and response buffers flags are monitored
1736 * and each function is called only if at least another function has changed at
1737 * least one flag it is interested in.
1738 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001739struct task *process_stream(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001740{
Willy Tarreau827aee92011-03-10 16:55:02 +01001741 struct server *srv;
Willy Tarreau87b09662015-04-03 00:22:06 +02001742 struct stream *s = t->context;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001743 struct session *sess = s->sess;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001744 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001745 unsigned int rq_prod_last, rq_cons_last;
1746 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001747 unsigned int req_ana_back;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001748 struct channel *req, *res;
1749 struct stream_interface *si_f, *si_b;
1750
1751 req = &s->req;
1752 res = &s->res;
1753
1754 si_f = &s->si[0];
1755 si_b = &s->si[1];
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001756
1757 //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 +01001758 // si_f->state, si_b->state, si_b->err_type, req->flags, res->flags);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001759
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001760 /* this data may be no longer valid, clear it */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001761 if (s->txn)
1762 memset(&s->txn->auth, 0, sizeof(s->txn->auth));
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001763
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02001764 /* This flag must explicitly be set every time */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001765 req->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
1766 res->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001767
1768 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001769 rqf_last = req->flags & ~CF_MASK_ANALYSER;
1770 rpf_last = res->flags & ~CF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001771
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001772 /* we don't want the stream interface functions to recursively wake us up */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001773 si_f->flags |= SI_FL_DONT_WAKE;
1774 si_b->flags |= SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001775
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001776 /* 1a: Check for low level timeouts if needed. We just set a flag on
1777 * stream interfaces when their timeouts have expired.
1778 */
1779 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001780 stream_int_check_timeouts(si_f);
1781 stream_int_check_timeouts(si_b);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001782
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001783 /* check channel timeouts, and close the corresponding stream interfaces
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001784 * for future reads or writes. Note: this will also concern upper layers
1785 * but we do not touch any other flag. We must be careful and correctly
1786 * detect state changes when calling them.
1787 */
1788
Willy Tarreau8f128b42014-11-28 15:07:47 +01001789 channel_check_timeouts(req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001790
Willy Tarreau8f128b42014-11-28 15:07:47 +01001791 if (unlikely((req->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1792 si_b->flags |= SI_FL_NOLINGER;
1793 si_shutw(si_b);
Willy Tarreau14641402009-12-29 14:49:56 +01001794 }
1795
Willy Tarreau8f128b42014-11-28 15:07:47 +01001796 if (unlikely((req->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1797 if (si_f->flags & SI_FL_NOHALF)
1798 si_f->flags |= SI_FL_NOLINGER;
1799 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001800 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001801
Willy Tarreau8f128b42014-11-28 15:07:47 +01001802 channel_check_timeouts(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001803
Willy Tarreau8f128b42014-11-28 15:07:47 +01001804 if (unlikely((res->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1805 si_f->flags |= SI_FL_NOLINGER;
1806 si_shutw(si_f);
Willy Tarreau14641402009-12-29 14:49:56 +01001807 }
1808
Willy Tarreau8f128b42014-11-28 15:07:47 +01001809 if (unlikely((res->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1810 if (si_b->flags & SI_FL_NOHALF)
1811 si_b->flags |= SI_FL_NOLINGER;
1812 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001813 }
Willy Tarreau798f4322012-11-08 14:49:17 +01001814
1815 /* Once in a while we're woken up because the task expires. But
1816 * this does not necessarily mean that a timeout has been reached.
Willy Tarreau87b09662015-04-03 00:22:06 +02001817 * So let's not run a whole stream processing if only an expiration
Willy Tarreau798f4322012-11-08 14:49:17 +01001818 * timeout needs to be refreshed.
1819 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001820 if (!((req->flags | res->flags) &
Willy Tarreau798f4322012-11-08 14:49:17 +01001821 (CF_SHUTR|CF_READ_ACTIVITY|CF_READ_TIMEOUT|CF_SHUTW|
1822 CF_WRITE_ACTIVITY|CF_WRITE_TIMEOUT|CF_ANA_TIMEOUT)) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01001823 !((si_f->flags | si_b->flags) & (SI_FL_EXP|SI_FL_ERR)) &&
Willy Tarreau798f4322012-11-08 14:49:17 +01001824 ((t->state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER))
1825 goto update_exp_and_leave;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001826 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001827
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001828 /* below we may emit error messages so we have to ensure that we have
1829 * our buffers properly allocated.
1830 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001831 if (!stream_alloc_work_buffer(s)) {
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001832 /* No buffer available, we've been subscribed to the list of
1833 * buffer waiters, let's wait for our turn.
1834 */
1835 goto update_exp_and_leave;
1836 }
1837
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001838 /* 1b: check for low-level errors reported at the stream interface.
1839 * First we check if it's a retryable error (in which case we don't
1840 * want to tell the buffer). Otherwise we report the error one level
1841 * upper by setting flags into the buffers. Note that the side towards
1842 * the client cannot have connect (hence retryable) errors. Also, the
1843 * connection setup code must be able to deal with any type of abort.
1844 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001845 srv = objt_server(s->target);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001846 if (unlikely(si_f->flags & SI_FL_ERR)) {
1847 if (si_f->state == SI_ST_EST || si_f->state == SI_ST_DIS) {
1848 si_shutr(si_f);
1849 si_shutw(si_f);
1850 stream_int_report_error(si_f);
1851 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001852 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001853 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001854 if (srv)
1855 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001856 if (!(s->flags & SF_ERR_MASK))
1857 s->flags |= SF_ERR_CLICL;
1858 if (!(s->flags & SF_FINST_MASK))
1859 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001860 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001861 }
1862 }
1863
Willy Tarreau8f128b42014-11-28 15:07:47 +01001864 if (unlikely(si_b->flags & SI_FL_ERR)) {
1865 if (si_b->state == SI_ST_EST || si_b->state == SI_ST_DIS) {
1866 si_shutr(si_b);
1867 si_shutw(si_b);
1868 stream_int_report_error(si_b);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001869 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001870 if (srv)
1871 srv->counters.failed_resp++;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001872 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001873 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001874 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001875 if (srv)
1876 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001877 if (!(s->flags & SF_ERR_MASK))
1878 s->flags |= SF_ERR_SRVCL;
1879 if (!(s->flags & SF_FINST_MASK))
1880 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001881 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001882 }
1883 /* note: maybe we should process connection errors here ? */
1884 }
1885
Willy Tarreau8f128b42014-11-28 15:07:47 +01001886 if (si_b->state == SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001887 /* we were trying to establish a connection on the server side,
1888 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1889 */
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001890 if (unlikely(!sess_update_st_con_tcp(s)))
1891 sess_update_st_cer(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001892 else if (si_b->state == SI_ST_EST)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001893 sess_establish(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001894
1895 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1896 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1897 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1898 */
1899 }
1900
Willy Tarreau8f128b42014-11-28 15:07:47 +01001901 rq_prod_last = si_f->state;
1902 rq_cons_last = si_b->state;
1903 rp_cons_last = si_f->state;
1904 rp_prod_last = si_b->state;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001905
1906 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001907 /* Check for connection closure */
1908
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001909 DPRINTF(stderr,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001910 "[%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 +01001911 now_ms, __FUNCTION__, __LINE__,
1912 t,
1913 s, s->flags,
Willy Tarreau8f128b42014-11-28 15:07:47 +01001914 req, res,
1915 req->rex, res->wex,
1916 req->flags, res->flags,
1917 req->buf->i, req->buf->o, res->buf->i, res->buf->o, si_f->state, si_b->state,
1918 si_f->err_type, si_b->err_type,
1919 si_b->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001920
1921 /* nothing special to be done on client side */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001922 if (unlikely(si_f->state == SI_ST_DIS))
1923 si_f->state = SI_ST_CLO;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001924
1925 /* When a server-side connection is released, we have to count it and
1926 * check for pending connections on this server.
1927 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001928 if (unlikely(si_b->state == SI_ST_DIS)) {
1929 si_b->state = SI_ST_CLO;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001930 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001931 if (srv) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001932 if (s->flags & SF_CURR_SESS) {
1933 s->flags &= ~SF_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001934 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001935 }
1936 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001937 if (may_dequeue_tasks(srv, s->be))
1938 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001939 }
1940 }
1941
1942 /*
1943 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1944 * at this point.
1945 */
1946
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001947 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001948 /* Analyse request */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001949 if (((req->flags & ~rqf_last) & CF_MASK_ANALYSER) ||
1950 ((req->flags ^ rqf_last) & CF_MASK_STATIC) ||
1951 si_f->state != rq_prod_last ||
1952 si_b->state != rq_cons_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001953 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001954 unsigned int flags = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001955
Willy Tarreau8f128b42014-11-28 15:07:47 +01001956 if (si_f->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001957 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001958 unsigned int ana_list;
1959 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001960
Willy Tarreau90deb182010-01-07 00:20:41 +01001961 /* it's up to the analysers to stop new connections,
1962 * disable reading or closing. Note: if an analyser
1963 * disables any of these bits, it is responsible for
1964 * enabling them again when it disables itself, so
1965 * that other analysers are called in similar conditions.
1966 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001967 channel_auto_read(req);
1968 channel_auto_connect(req);
1969 channel_auto_close(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001970
1971 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001972 * req->analysers, following the bit order from LSB
Willy Tarreauedcf6682008-11-30 23:15:34 +01001973 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001974 * the list when not needed. Any analyser may return 0
1975 * to break out of the loop, either because of missing
1976 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001977 * kill the stream. We loop at least once through each
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001978 * analyser, and we may loop again if other analysers
1979 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001980 *
1981 * We build a list of analysers to run. We evaluate all
1982 * of these analysers in the order of the lower bit to
1983 * the higher bit. This ordering is very important.
1984 * An analyser will often add/remove other analysers,
1985 * including itself. Any changes to itself have no effect
1986 * on the loop. If it removes any other analysers, we
1987 * want those analysers not to be called anymore during
1988 * this loop. If it adds an analyser that is located
1989 * after itself, we want it to be scheduled for being
1990 * processed during the loop. If it adds an analyser
1991 * which is located before it, we want it to switch to
1992 * it immediately, even if it has already been called
1993 * once but removed since.
1994 *
1995 * In order to achieve this, we compare the analyser
1996 * list after the call with a copy of it before the
1997 * call. The work list is fed with analyser bits that
1998 * appeared during the call. Then we compare previous
1999 * work list with the new one, and check the bits that
2000 * appeared. If the lowest of these bits is lower than
2001 * the current bit, it means we have enabled a previous
2002 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01002003 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002004
Willy Tarreau8f128b42014-11-28 15:07:47 +01002005 ana_list = ana_back = req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01002006 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002007 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02002008
Willy Tarreaufb356202010-08-03 14:02:05 +02002009 if (ana_list & AN_REQ_INSPECT_FE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002010 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01002011 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002012 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02002013 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01002014
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002015 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002016 if (!http_wait_for_request(s, req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02002017 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002018 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02002019 }
2020
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002021 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002022 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_FE, sess->fe))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002023 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002024 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002025 }
2026
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002027 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002028 if (!process_switching_rules(s, req, AN_REQ_SWITCHING_RULES))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002029 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002030 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002031 }
2032
Willy Tarreaufb356202010-08-03 14:02:05 +02002033 if (ana_list & AN_REQ_INSPECT_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002034 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_BE))
Willy Tarreaufb356202010-08-03 14:02:05 +02002035 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002036 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
Willy Tarreaufb356202010-08-03 14:02:05 +02002037 }
2038
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002039 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002040 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_BE, s->be))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002041 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002042 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002043 }
2044
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002045 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002046 if (!http_process_tarpit(s, req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01002047 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002048 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02002049 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01002050
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002051 if (ana_list & AN_REQ_SRV_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002052 if (!process_server_rules(s, req, AN_REQ_SRV_RULES))
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002053 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002054 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SRV_RULES);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02002055 }
2056
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002057 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002058 if (!http_process_request(s, req, AN_REQ_HTTP_INNER))
Willy Tarreauc465fd72009-08-31 00:17:18 +02002059 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002060 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002061 }
2062
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002063 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002064 if (!http_wait_for_request_body(s, req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01002065 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002066 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02002067 }
Emeric Brun647caf12009-06-30 17:57:00 +02002068
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002069 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002070 if (!tcp_persist_rdp_cookie(s, req, AN_REQ_PRST_RDP_COOKIE))
Emeric Brun647caf12009-06-30 17:57:00 +02002071 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002072 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02002073 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002074
Emeric Brun1d33b292010-01-04 15:47:17 +01002075 if (ana_list & AN_REQ_STICKING_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002076 if (!process_sticking_rules(s, req, AN_REQ_STICKING_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01002077 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002078 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01002079 }
2080
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002081 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002082 if (!http_request_forward_body(s, req, AN_REQ_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01002083 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002084 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002085 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01002086 break;
2087 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002088 }
Willy Tarreau84455332009-03-15 22:34:05 +01002089
Willy Tarreau8f128b42014-11-28 15:07:47 +01002090 rq_prod_last = si_f->state;
2091 rq_cons_last = si_b->state;
2092 req->flags &= ~CF_WAKE_ONCE;
2093 rqf_last = req->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02002094
Willy Tarreau8f128b42014-11-28 15:07:47 +01002095 if ((req->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002096 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002097 }
2098
Willy Tarreau576507f2010-01-07 00:09:04 +01002099 /* we'll monitor the request analysers while parsing the response,
2100 * because some response analysers may indirectly enable new request
2101 * analysers (eg: HTTP keep-alive).
2102 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002103 req_ana_back = req->analysers;
Willy Tarreau576507f2010-01-07 00:09:04 +01002104
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002105 resync_response:
2106 /* Analyse response */
2107
Willy Tarreau8f128b42014-11-28 15:07:47 +01002108 if (((res->flags & ~rpf_last) & CF_MASK_ANALYSER) ||
2109 (res->flags ^ rpf_last) & CF_MASK_STATIC ||
2110 si_f->state != rp_cons_last ||
2111 si_b->state != rp_prod_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01002112 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002113 unsigned int flags = res->flags;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002114
Willy Tarreau8f128b42014-11-28 15:07:47 +01002115 if ((res->flags & CF_MASK_ANALYSER) &&
2116 (res->analysers & AN_REQ_ALL)) {
Willy Tarreau0499e352010-12-17 07:13:42 +01002117 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
2118 * for some free space in the response buffer, so we might need to call
2119 * it when something changes in the response buffer, but still we pass
2120 * it the request buffer. Note that the SI state might very well still
2121 * be zero due to us returning a flow of redirects!
2122 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002123 res->analysers &= ~AN_REQ_ALL;
2124 req->flags |= CF_WAKE_ONCE;
Willy Tarreau0499e352010-12-17 07:13:42 +01002125 }
2126
Willy Tarreau8f128b42014-11-28 15:07:47 +01002127 if (si_b->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01002128 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002129 unsigned int ana_list;
2130 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002131
Willy Tarreau90deb182010-01-07 00:20:41 +01002132 /* it's up to the analysers to stop disable reading or
2133 * closing. Note: if an analyser disables any of these
2134 * bits, it is responsible for enabling them again when
2135 * it disables itself, so that other analysers are called
2136 * in similar conditions.
2137 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002138 channel_auto_read(res);
2139 channel_auto_close(res);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002140
2141 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01002142 * res->analysers, following the bit order from LSB
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002143 * to MSB. The analysers must remove themselves from
2144 * the list when not needed. Any analyser may return 0
2145 * to break out of the loop, either because of missing
2146 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02002147 * kill the stream. We loop at least once through each
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002148 * analyser, and we may loop again if other analysers
2149 * are added in the middle.
2150 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002151
Willy Tarreau8f128b42014-11-28 15:07:47 +01002152 ana_list = ana_back = res->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01002153 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002154 /* Warning! ensure that analysers are always placed in ascending order! */
2155
Emeric Brun97679e72010-09-23 17:56:44 +02002156 if (ana_list & AN_RES_INSPECT) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002157 if (!tcp_inspect_response(s, res, AN_RES_INSPECT))
Emeric Brun97679e72010-09-23 17:56:44 +02002158 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002159 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_INSPECT);
Emeric Brun97679e72010-09-23 17:56:44 +02002160 }
2161
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002162 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002163 if (!http_wait_for_response(s, res, AN_RES_WAIT_HTTP))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002164 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002165 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002166 }
2167
Emeric Brun1d33b292010-01-04 15:47:17 +01002168 if (ana_list & AN_RES_STORE_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002169 if (!process_store_rules(s, res, AN_RES_STORE_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01002170 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002171 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01002172 }
2173
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002174 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002175 if (!http_process_res_common(s, res, AN_RES_HTTP_PROCESS_BE, s->be))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002176 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002177 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002178 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002179
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002180 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002181 if (!http_response_forward_body(s, res, AN_RES_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01002182 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002183 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002184 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01002185 break;
2186 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002187 }
2188
Willy Tarreau8f128b42014-11-28 15:07:47 +01002189 rp_cons_last = si_f->state;
2190 rp_prod_last = si_b->state;
2191 rpf_last = res->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02002192
Willy Tarreau8f128b42014-11-28 15:07:47 +01002193 if ((res->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002194 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002195 }
2196
Willy Tarreau576507f2010-01-07 00:09:04 +01002197 /* maybe someone has added some request analysers, so we must check and loop */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002198 if (req->analysers & ~req_ana_back)
Willy Tarreau576507f2010-01-07 00:09:04 +01002199 goto resync_request;
2200
Willy Tarreau8f128b42014-11-28 15:07:47 +01002201 if ((req->flags & ~rqf_last) & CF_MASK_ANALYSER)
Willy Tarreau0499e352010-12-17 07:13:42 +01002202 goto resync_request;
2203
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002204 /* FIXME: here we should call protocol handlers which rely on
2205 * both buffers.
2206 */
2207
2208
2209 /*
Willy Tarreau87b09662015-04-03 00:22:06 +02002210 * Now we propagate unhandled errors to the stream. Normally
Willy Tarreauae526782010-03-04 20:34:23 +01002211 * we're just in a data phase here since it means we have not
2212 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002213 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002214 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002215 if (unlikely(!(s->flags & SF_ERR_MASK))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002216 if (req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002217 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002218 req->analysers = 0;
2219 if (req->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002220 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002221 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002222 if (srv)
2223 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002224 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01002225 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002226 else if (req->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002227 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002228 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002229 if (srv)
2230 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002231 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01002232 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002233 else if (req->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002234 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002235 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002236 if (srv)
2237 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002238 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01002239 }
2240 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002241 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002242 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002243 if (srv)
2244 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002245 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01002246 }
Willy Tarreau84455332009-03-15 22:34:05 +01002247 sess_set_term_flags(s);
2248 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002249 else if (res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002250 /* Report it if the server got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002251 res->analysers = 0;
2252 if (res->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002253 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002254 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002255 if (srv)
2256 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002257 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01002258 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002259 else if (res->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002260 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002261 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002262 if (srv)
2263 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002264 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01002265 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002266 else if (res->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002267 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002268 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002269 if (srv)
2270 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002271 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01002272 }
2273 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002274 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002275 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002276 if (srv)
2277 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002278 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01002279 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002280 sess_set_term_flags(s);
2281 }
Willy Tarreau84455332009-03-15 22:34:05 +01002282 }
2283
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002284 /*
2285 * Here we take care of forwarding unhandled data. This also includes
2286 * connection establishments and shutdown requests.
2287 */
2288
2289
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002290 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002291 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002292 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002293 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002294 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002295 if (unlikely(!req->analysers &&
2296 !(req->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2297 (si_f->state >= SI_ST_EST) &&
2298 (req->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002299 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002300 * attached to it. If any data are left in, we'll permit them to
2301 * move.
2302 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002303 channel_auto_read(req);
2304 channel_auto_connect(req);
2305 channel_auto_close(req);
2306 buffer_flush(req->buf);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01002307
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002308 /* We'll let data flow between the producer (if still connected)
2309 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002310 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002311 if (!(req->flags & (CF_SHUTR|CF_SHUTW_NOW)))
2312 channel_forward(req, CHN_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002313 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002314
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002315 /* check if it is wise to enable kernel splicing to forward request data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002316 if (!(req->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2317 req->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002318 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002319 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->rcv_pipe) &&
2320 (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 +01002321 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002322 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
2323 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002324 (req->flags & CF_STREAMER_FAST)))) {
2325 req->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002326 }
2327
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002328 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002329 rqf_last = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002330
2331 /*
2332 * Now forward all shutdown requests between both sides of the buffer
2333 */
2334
Willy Tarreau520d95e2009-09-19 21:04:57 +02002335 /* first, let's check if the request buffer needs to shutdown(write), which may
2336 * happen either because the input is closed or because we want to force a close
Willy Tarreau05cdd962014-05-10 14:30:07 +02002337 * once the server has begun to respond. If a half-closed timeout is set, we adjust
2338 * the other side's timeout as well.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002339 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002340 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002341 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002342 channel_shutw_now(req);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002343 if (tick_isset(sess->fe->timeout.clientfin)) {
2344 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002345 res->wex = tick_add(now_ms, res->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002346 }
2347 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002348
2349 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002350 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2351 channel_is_empty(req))) {
2352 if (req->flags & CF_READ_ERROR)
2353 si_b->flags |= SI_FL_NOLINGER;
2354 si_shutw(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002355 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002356 res->rto = s->be->timeout.serverfin;
2357 res->rex = tick_add(now_ms, res->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002358 }
Willy Tarreau8615c2a2013-06-21 08:20:19 +02002359 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002360
2361 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002362 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW &&
2363 !req->analysers))
2364 channel_shutr_now(req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002365
2366 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002367 if (unlikely((req->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2368 if (si_f->flags & SI_FL_NOHALF)
2369 si_f->flags |= SI_FL_NOLINGER;
2370 si_shutr(si_f);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002371 if (tick_isset(sess->fe->timeout.clientfin)) {
2372 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002373 res->wex = tick_add(now_ms, res->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002374 }
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002375 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002376
Willy Tarreau520d95e2009-09-19 21:04:57 +02002377 /* it's possible that an upper layer has requested a connection setup or abort.
2378 * There are 2 situations where we decide to establish a new connection :
2379 * - there are data scheduled for emission in the buffer
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002380 * - the CF_AUTO_CONNECT flag is set (active connection)
Willy Tarreau520d95e2009-09-19 21:04:57 +02002381 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002382 if (si_b->state == SI_ST_INI) {
2383 if (!(req->flags & CF_SHUTW)) {
2384 if ((req->flags & CF_AUTO_CONNECT) || !channel_is_empty(req)) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002385 /* If we have an appctx, there is no connect method, so we
2386 * immediately switch to the connected state, otherwise we
2387 * perform a connection request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002388 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002389 si_b->state = SI_ST_REQ; /* new connection requested */
2390 si_b->conn_retries = s->be->conn_retries;
Willy Tarreau520d95e2009-09-19 21:04:57 +02002391 }
Willy Tarreau73201222009-08-16 18:27:24 +02002392 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002393 else {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002394 si_b->state = SI_ST_CLO; /* shutw+ini = abort */
2395 channel_shutw_now(req); /* fix buffer flags upon abort */
2396 channel_shutr_now(res);
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002397 }
Willy Tarreau92795622009-03-06 12:51:23 +01002398 }
2399
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002400
2401 /* we may have a pending connection request, or a connection waiting
2402 * for completion.
2403 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002404 if (si_b->state >= SI_ST_REQ && si_b->state < SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002405 do {
2406 /* nb: step 1 might switch from QUE to ASS, but we first want
2407 * to give a chance to step 2 to perform a redirect if needed.
2408 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002409 if (si_b->state != SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002410 sess_update_stream_int(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01002411 if (si_b->state == SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002412 sess_prepare_conn_req(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002413
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01002414 /* applets directly go to the ESTABLISHED state. Similarly,
2415 * servers experience the same fate when their connection
2416 * is reused.
2417 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002418 if (unlikely(si_b->state == SI_ST_EST))
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002419 sess_establish(s);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01002420
Willy Tarreaub44c8732013-12-31 23:16:50 +01002421 /* Now we can add the server name to a header (if requested) */
2422 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002423 if ((si_b->state >= SI_ST_CON) &&
Willy Tarreaub44c8732013-12-31 23:16:50 +01002424 (s->be->server_id_hdr_name != NULL) &&
2425 (s->be->mode == PR_MODE_HTTP) &&
2426 objt_server(s->target)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02002427 http_send_name_header(s->txn, s->be, objt_server(s->target)->id);
Willy Tarreau1e5dfda2013-04-07 18:19:16 +02002428 }
2429
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002430 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002431 if (si_b->state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SF_REDIRECTABLE))
Willy Tarreau8f128b42014-11-28 15:07:47 +01002432 http_perform_server_redirect(s, si_b);
2433 } while (si_b->state == SI_ST_ASS);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002434 }
2435
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002436 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002437 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002438 goto resync_stream_interface;
2439
Willy Tarreau815a9b22010-07-27 17:15:12 +02002440 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002441 if ((req->flags ^ rqf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002442 goto resync_request;
2443
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002444 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01002445
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002446 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002447 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002448 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002449 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002450 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002451 if (unlikely(!res->analysers &&
2452 !(res->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2453 (si_b->state >= SI_ST_EST) &&
2454 (res->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002455 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002456 * attached to it. If any data are left in, we'll permit them to
2457 * move.
2458 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002459 channel_auto_read(res);
2460 channel_auto_close(res);
2461 buffer_flush(res->buf);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002462
2463 /* We'll let data flow between the producer (if still connected)
2464 * to the consumer.
2465 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002466 if (!(res->flags & (CF_SHUTR|CF_SHUTW_NOW)))
2467 channel_forward(res, CHN_INFINITE_FORWARD);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002468
2469 /* if we have no analyser anymore in any direction and have a
Willy Tarreau05cdd962014-05-10 14:30:07 +02002470 * tunnel timeout set, use it now. Note that we must respect
2471 * the half-closed timeouts as well.
Willy Tarreauce887fd2012-05-12 12:50:00 +02002472 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002473 if (!req->analysers && s->be->timeout.tunnel) {
2474 req->rto = req->wto = res->rto = res->wto =
Willy Tarreauce887fd2012-05-12 12:50:00 +02002475 s->be->timeout.tunnel;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002476
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002477 if ((req->flags & CF_SHUTR) && tick_isset(sess->fe->timeout.clientfin))
2478 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002479 if ((req->flags & CF_SHUTW) && tick_isset(s->be->timeout.serverfin))
2480 res->rto = s->be->timeout.serverfin;
2481 if ((res->flags & CF_SHUTR) && tick_isset(s->be->timeout.serverfin))
2482 req->wto = s->be->timeout.serverfin;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002483 if ((res->flags & CF_SHUTW) && tick_isset(sess->fe->timeout.clientfin))
2484 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002485
Willy Tarreau8f128b42014-11-28 15:07:47 +01002486 req->rex = tick_add(now_ms, req->rto);
2487 req->wex = tick_add(now_ms, req->wto);
2488 res->rex = tick_add(now_ms, res->rto);
2489 res->wex = tick_add(now_ms, res->wto);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002490 }
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002491 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002492
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002493 /* check if it is wise to enable kernel splicing to forward response data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002494 if (!(res->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2495 res->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002496 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002497 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->snd_pipe) &&
2498 (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 +01002499 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002500 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2501 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002502 (res->flags & CF_STREAMER_FAST)))) {
2503 res->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002504 }
2505
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002506 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002507 rpf_last = res->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002508
2509 /*
2510 * Now forward all shutdown requests between both sides of the buffer
2511 */
2512
2513 /*
2514 * FIXME: this is probably where we should produce error responses.
2515 */
2516
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002517 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002518 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002519 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002520 channel_shutw_now(res);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002521 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002522 req->wto = s->be->timeout.serverfin;
2523 req->wex = tick_add(now_ms, req->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002524 }
2525 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002526
2527 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002528 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2529 channel_is_empty(res))) {
2530 si_shutw(si_f);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002531 if (tick_isset(sess->fe->timeout.clientfin)) {
2532 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002533 req->rex = tick_add(now_ms, req->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002534 }
2535 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002536
2537 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002538 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW) &&
2539 !res->analysers)
2540 channel_shutr_now(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002541
2542 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002543 if (unlikely((res->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2544 if (si_b->flags & SI_FL_NOHALF)
2545 si_b->flags |= SI_FL_NOLINGER;
2546 si_shutr(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002547 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002548 req->wto = s->be->timeout.serverfin;
2549 req->wex = tick_add(now_ms, req->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002550 }
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002551 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002552
Willy Tarreau8f128b42014-11-28 15:07:47 +01002553 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002554 goto resync_stream_interface;
2555
Willy Tarreau8f128b42014-11-28 15:07:47 +01002556 if (req->flags != rqf_last)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002557 goto resync_request;
2558
Willy Tarreau8f128b42014-11-28 15:07:47 +01002559 if ((res->flags ^ rpf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002560 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002561
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002562 /* we're interested in getting wakeups again */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002563 si_f->flags &= ~SI_FL_DONT_WAKE;
2564 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002565
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002566 /* This is needed only when debugging is enabled, to indicate
2567 * client-side or server-side close. Please note that in the unlikely
2568 * event where both sides would close at once, the sequence is reported
2569 * on the server side first.
2570 */
2571 if (unlikely((global.mode & MODE_DEBUG) &&
2572 (!(global.mode & MODE_QUIET) ||
2573 (global.mode & MODE_VERBOSE)))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002574 if (si_b->state == SI_ST_CLO &&
2575 si_b->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002576 chunk_printf(&trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002577 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002578 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2579 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002580 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002581 }
2582
Willy Tarreau8f128b42014-11-28 15:07:47 +01002583 if (si_f->state == SI_ST_CLO &&
2584 si_f->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002585 chunk_printf(&trash, "%08x:%s.clicls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002586 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002587 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2588 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002589 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002590 }
2591 }
2592
Willy Tarreau8f128b42014-11-28 15:07:47 +01002593 if (likely((si_f->state != SI_ST_CLO) ||
2594 (si_b->state > SI_ST_INI && si_b->state < SI_ST_CLO))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002595
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002596 if ((sess->fe->options & PR_O_CONTSTATS) && (s->flags & SF_BE_ASSIGNED))
Willy Tarreau87b09662015-04-03 00:22:06 +02002597 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002598
Willy Tarreau8f128b42014-11-28 15:07:47 +01002599 if (si_f->state == SI_ST_EST && obj_type(si_f->end) != OBJ_TYPE_APPCTX)
2600 si_update(si_f);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002601
Willy Tarreau8f128b42014-11-28 15:07:47 +01002602 if (si_b->state == SI_ST_EST && obj_type(si_b->end) != OBJ_TYPE_APPCTX)
2603 si_update(si_b);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002604
Willy Tarreau8f128b42014-11-28 15:07:47 +01002605 req->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2606 res->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2607 si_f->prev_state = si_f->state;
2608 si_b->prev_state = si_b->state;
2609 si_f->flags &= ~(SI_FL_ERR|SI_FL_EXP);
2610 si_b->flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002611
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002612 /* Trick: if a request is being waiting for the server to respond,
2613 * and if we know the server can timeout, we don't want the timeout
2614 * to expire on the client side first, but we're still interested
2615 * in passing data from the client to the server (eg: POST). Thus,
2616 * we can cancel the client's request timeout if the server's
2617 * request timeout is set and the server has not yet sent a response.
2618 */
2619
Willy Tarreau8f128b42014-11-28 15:07:47 +01002620 if ((res->flags & (CF_AUTO_CLOSE|CF_SHUTR)) == 0 &&
2621 (tick_isset(req->wex) || tick_isset(res->rex))) {
2622 req->flags |= CF_READ_NOEXP;
2623 req->rex = TICK_ETERNITY;
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002624 }
2625
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002626 /* When any of the stream interfaces is attached to an applet,
2627 * we have to call it here. Note that this one may wake the
2628 * task up again. If at least one applet was called, the current
2629 * task might have been woken up, in which case we don't want it
2630 * to be requeued to the wait queue but rather to the run queue
2631 * to run ASAP. The bitwise "or" in the condition ensures that
2632 * both functions are always called and that we wake up if at
2633 * least one did something.
Willy Tarreau1accfc02009-09-05 20:57:35 +02002634 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002635 if ((si_applet_call(si_b) | si_applet_call(si_f)) != 0) {
Willy Tarreau1accfc02009-09-05 20:57:35 +02002636 if (task_in_rq(t)) {
Willy Tarreau1accfc02009-09-05 20:57:35 +02002637 t->expire = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02002638 stream_release_buffers(s);
Willy Tarreau1accfc02009-09-05 20:57:35 +02002639 return t;
2640 }
2641 }
2642
Willy Tarreau798f4322012-11-08 14:49:17 +01002643 update_exp_and_leave:
Willy Tarreau8f128b42014-11-28 15:07:47 +01002644 t->expire = tick_first(tick_first(req->rex, req->wex),
2645 tick_first(res->rex, res->wex));
2646 if (req->analysers)
2647 t->expire = tick_first(t->expire, req->analyse_exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002648
Willy Tarreau8f128b42014-11-28 15:07:47 +01002649 if (si_f->exp)
2650 t->expire = tick_first(t->expire, si_f->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002651
Willy Tarreau8f128b42014-11-28 15:07:47 +01002652 if (si_b->exp)
2653 t->expire = tick_first(t->expire, si_b->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002654
2655#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002656 fprintf(stderr,
2657 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2658 " 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 +01002659 now_ms, t->expire, req->rex, req->wex, req->analyse_exp,
2660 res->rex, res->wex, si_f->exp, si_b->exp, si_f->state, si_b->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002661#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002662
2663#ifdef DEBUG_DEV
2664 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002665 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002666 ABORT_NOW();
2667#endif
Willy Tarreau87b09662015-04-03 00:22:06 +02002668 stream_release_buffers(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002669 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002670 }
2671
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002672 sess->fe->feconn--;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002673 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002674 s->be->beconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002675 jobs--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002676 if (sess->listener) {
2677 if (!(sess->listener->options & LI_O_UNLIMITED))
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002678 actconn--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002679 sess->listener->nbconn--;
2680 if (sess->listener->state == LI_FULL)
2681 resume_listener(sess->listener);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002682
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002683 /* Dequeues all of the listeners waiting for a resource */
2684 if (!LIST_ISEMPTY(&global_listener_queue))
2685 dequeue_all_listeners(&global_listener_queue);
Willy Tarreau08ceb102011-07-24 22:58:00 +02002686
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002687 if (!LIST_ISEMPTY(&sess->fe->listener_queue) &&
2688 (!sess->fe->fe_sps_lim || freq_ctr_remain(&sess->fe->fe_sess_per_sec, sess->fe->fe_sps_lim, 0) > 0))
2689 dequeue_all_listeners(&sess->fe->listener_queue);
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002690 }
Willy Tarreau07687c12011-07-24 23:55:06 +02002691
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002692 if (unlikely((global.mode & MODE_DEBUG) &&
2693 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002694 chunk_printf(&trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002695 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002696 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2697 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002698 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002699 }
2700
2701 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02002702 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002703
Willy Tarreaueee5b512015-04-03 23:46:31 +02002704 if (s->txn && s->txn->status) {
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002705 int n;
2706
Willy Tarreaueee5b512015-04-03 23:46:31 +02002707 n = s->txn->status / 100;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002708 if (n < 1 || n > 5)
2709 n = 0;
2710
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002711 if (sess->fe->mode == PR_MODE_HTTP) {
2712 sess->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002713 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002714 sess->fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002715 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02002716 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002717 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002718 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002719 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002720 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002721 s->be->be_counters.p.http.comp_rsp++;
2722 }
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002723 }
2724
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002725 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002726 if (!LIST_ISEMPTY(&sess->fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02002727 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002728 (!(sess->fe->options & PR_O_NULLNOLOG) || req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002729 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002730 }
2731
Willy Tarreau87b09662015-04-03 00:22:06 +02002732 /* update time stats for this stream */
2733 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002734
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002735 /* the task MUST not be in the run queue anymore */
Willy Tarreau87b09662015-04-03 00:22:06 +02002736 stream_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002737 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002738 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002739 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002740}
2741
Willy Tarreau87b09662015-04-03 00:22:06 +02002742/* Update the stream's backend and server time stats */
2743void stream_update_time_stats(struct stream *s)
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002744{
2745 int t_request;
2746 int t_queue;
2747 int t_connect;
2748 int t_data;
2749 int t_close;
2750 struct server *srv;
2751
2752 t_request = 0;
2753 t_queue = s->logs.t_queue;
2754 t_connect = s->logs.t_connect;
2755 t_close = s->logs.t_close;
2756 t_data = s->logs.t_data;
2757
2758 if (s->be->mode != PR_MODE_HTTP)
2759 t_data = t_connect;
2760
2761 if (t_connect < 0 || t_data < 0)
2762 return;
2763
2764 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
2765 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
2766
2767 t_data -= t_connect;
2768 t_connect -= t_queue;
2769 t_queue -= t_request;
2770
2771 srv = objt_server(s->target);
2772 if (srv) {
2773 swrate_add(&srv->counters.q_time, TIME_STATS_SAMPLES, t_queue);
2774 swrate_add(&srv->counters.c_time, TIME_STATS_SAMPLES, t_connect);
2775 swrate_add(&srv->counters.d_time, TIME_STATS_SAMPLES, t_data);
2776 swrate_add(&srv->counters.t_time, TIME_STATS_SAMPLES, t_close);
2777 }
2778 swrate_add(&s->be->be_counters.q_time, TIME_STATS_SAMPLES, t_queue);
2779 swrate_add(&s->be->be_counters.c_time, TIME_STATS_SAMPLES, t_connect);
2780 swrate_add(&s->be->be_counters.d_time, TIME_STATS_SAMPLES, t_data);
2781 swrate_add(&s->be->be_counters.t_time, TIME_STATS_SAMPLES, t_close);
2782}
2783
Willy Tarreau7c669d72008-06-20 15:04:11 +02002784/*
2785 * This function adjusts sess->srv_conn and maintains the previous and new
Willy Tarreau87b09662015-04-03 00:22:06 +02002786 * server's served stream counts. Setting newsrv to NULL is enough to release
Willy Tarreau7c669d72008-06-20 15:04:11 +02002787 * current connection slot. This function also notifies any LB algo which might
Willy Tarreau87b09662015-04-03 00:22:06 +02002788 * expect to be informed about any change in the number of active streams on a
Willy Tarreau7c669d72008-06-20 15:04:11 +02002789 * server.
2790 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002791void sess_change_server(struct stream *sess, struct server *newsrv)
Willy Tarreau7c669d72008-06-20 15:04:11 +02002792{
2793 if (sess->srv_conn == newsrv)
2794 return;
2795
2796 if (sess->srv_conn) {
2797 sess->srv_conn->served--;
2798 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2799 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Willy Tarreau87b09662015-04-03 00:22:06 +02002800 stream_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002801 }
2802
2803 if (newsrv) {
2804 newsrv->served++;
2805 if (newsrv->proxy->lbprm.server_take_conn)
2806 newsrv->proxy->lbprm.server_take_conn(newsrv);
Willy Tarreau87b09662015-04-03 00:22:06 +02002807 stream_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002808 }
2809}
2810
Willy Tarreau84455332009-03-15 22:34:05 +01002811/* Handle server-side errors for default protocols. It is called whenever a a
2812 * connection setup is aborted or a request is aborted in queue. It sets the
Willy Tarreau87b09662015-04-03 00:22:06 +02002813 * stream termination flags so that the caller does not have to worry about
Willy Tarreau84455332009-03-15 22:34:05 +01002814 * them. It's installed as ->srv_error for the server-side stream_interface.
2815 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002816void default_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreau84455332009-03-15 22:34:05 +01002817{
2818 int err_type = si->err_type;
2819 int err = 0, fin = 0;
2820
2821 if (err_type & SI_ET_QUEUE_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002822 err = SF_ERR_CLICL;
2823 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002824 }
2825 else if (err_type & SI_ET_CONN_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002826 err = SF_ERR_CLICL;
2827 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002828 }
2829 else if (err_type & SI_ET_QUEUE_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002830 err = SF_ERR_SRVTO;
2831 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002832 }
2833 else if (err_type & SI_ET_QUEUE_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002834 err = SF_ERR_SRVCL;
2835 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002836 }
2837 else if (err_type & SI_ET_CONN_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002838 err = SF_ERR_SRVTO;
2839 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002840 }
2841 else if (err_type & SI_ET_CONN_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002842 err = SF_ERR_SRVCL;
2843 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002844 }
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002845 else if (err_type & SI_ET_CONN_RES) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002846 err = SF_ERR_RESOURCE;
2847 fin = SF_FINST_C;
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002848 }
Willy Tarreau84455332009-03-15 22:34:05 +01002849 else /* SI_ET_CONN_OTHER and others */ {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002850 err = SF_ERR_INTERNAL;
2851 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002852 }
2853
Willy Tarreaue7dff022015-04-03 01:14:29 +02002854 if (!(s->flags & SF_ERR_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002855 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002856 if (!(s->flags & SF_FINST_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002857 s->flags |= fin;
2858}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002859
Willy Tarreaue7dff022015-04-03 01:14:29 +02002860/* kill a stream and set the termination flags to <why> (one of SF_ERR_*) */
Willy Tarreau87b09662015-04-03 00:22:06 +02002861void stream_shutdown(struct stream *stream, int why)
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002862{
Willy Tarreau87b09662015-04-03 00:22:06 +02002863 if (stream->req.flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002864 return;
2865
Willy Tarreau87b09662015-04-03 00:22:06 +02002866 channel_shutw_now(&stream->req);
2867 channel_shutr_now(&stream->res);
2868 stream->task->nice = 1024;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002869 if (!(stream->flags & SF_ERR_MASK))
Willy Tarreau87b09662015-04-03 00:22:06 +02002870 stream->flags |= why;
2871 task_wakeup(stream->task, TASK_WOKEN_OTHER);
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002872}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002873
Willy Tarreau8b22a712010-06-18 17:46:06 +02002874/************************************************************************/
2875/* All supported ACL keywords must be declared here. */
2876/************************************************************************/
2877
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002878/* Returns a pointer to a stkctr depending on the fetch keyword name.
2879 * It is designed to be called as sc[0-9]_* sc_* or src_* exclusively.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002880 * sc[0-9]_* will return a pointer to the respective field in the
Willy Tarreau87b09662015-04-03 00:22:06 +02002881 * stream <l4>. sc_* requires an UINT argument specifying the stick
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002882 * counter number. src_* will fill a locally allocated structure with
Willy Tarreaua65536c2013-07-22 22:40:11 +02002883 * the table and entry corresponding to what is specified with src_*.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002884 * NULL may be returned if the designated stkctr is not tracked. For
2885 * the sc_* and sc[0-9]_* forms, an optional table argument may be
2886 * passed. When present, the currently tracked key is then looked up
2887 * in the specified table instead of the current table. The purpose is
2888 * to be able to convery multiple values per key (eg: have gpc0 from
2889 * multiple tables).
Willy Tarreaua65536c2013-07-22 22:40:11 +02002890 */
Willy Tarreaue12704b2014-07-15 19:06:18 +02002891struct stkctr *
Willy Tarreau192252e2015-04-04 01:47:55 +02002892smp_fetch_sc_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
Willy Tarreaua65536c2013-07-22 22:40:11 +02002893{
2894 static struct stkctr stkctr;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002895 struct stksess *stksess;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002896 unsigned int num = kw[2] - '0';
Willy Tarreau0f791d42013-07-23 19:56:43 +02002897 int arg = 0;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002898
Willy Tarreau0f791d42013-07-23 19:56:43 +02002899 if (num == '_' - '0') {
2900 /* sc_* variant, args[0] = ctr# (mandatory) */
2901 num = args[arg++].data.uint;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002902 if (num >= MAX_SESS_STKCTR)
2903 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002904 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002905 else if (num > 9) { /* src_* variant, args[0] = table */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002906 struct stktable_key *key;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002907 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002908
2909 if (!conn)
2910 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002911
Thierry FOURNIER74c219d2014-04-14 14:35:40 +02002912 key = addr_to_stktable_key(&conn->addr.from, args->data.prx->table.type);
Willy Tarreaua65536c2013-07-22 22:40:11 +02002913 if (!key)
2914 return NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002915
Willy Tarreaua65536c2013-07-22 22:40:11 +02002916 stkctr.table = &args->data.prx->table;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002917 stkctr_set_entry(&stkctr, stktable_lookup_key(stkctr.table, key));
Willy Tarreaua65536c2013-07-22 22:40:11 +02002918 return &stkctr;
2919 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002920
2921 /* Here, <num> contains the counter number from 0 to 9 for
2922 * the sc[0-9]_ form, or even higher using sc_(num) if needed.
2923 * args[arg] is the first optional argument.
2924 */
Willy Tarreau192252e2015-04-04 01:47:55 +02002925 stksess = stkctr_entry(&strm->stkctr[num]);
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002926 if (!stksess)
2927 return NULL;
2928
Willy Tarreau0f791d42013-07-23 19:56:43 +02002929 if (unlikely(args[arg].type == ARGT_TAB)) {
2930 /* an alternate table was specified, let's look up the same key there */
2931 stkctr.table = &args[arg].data.prx->table;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002932 stkctr_set_entry(&stkctr, stktable_lookup(stkctr.table, stksess));
Willy Tarreau0f791d42013-07-23 19:56:43 +02002933 return &stkctr;
2934 }
Willy Tarreau192252e2015-04-04 01:47:55 +02002935 return &strm->stkctr[num];
Willy Tarreaua65536c2013-07-22 22:40:11 +02002936}
2937
Willy Tarreau87b09662015-04-03 00:22:06 +02002938/* set return a boolean indicating if the requested stream counter is
Willy Tarreau88821242013-07-22 18:29:29 +02002939 * currently being tracked or not.
2940 * Supports being called as "sc[0-9]_tracked" only.
2941 */
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002942static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002943smp_fetch_sc_tracked(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +02002944 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002945{
2946 smp->flags = SMP_F_VOL_TEST;
2947 smp->type = SMP_T_BOOL;
Willy Tarreau192252e2015-04-04 01:47:55 +02002948 smp->data.uint = !!smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002949 return 1;
2950}
2951
Willy Tarreau87b09662015-04-03 00:22:06 +02002952/* set <smp> to the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau30b20462013-07-22 19:46:52 +02002953 * frontend counters or from the src.
2954 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpc0" only. Value
2955 * zero is returned if the key is new.
2956 */
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002957static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002958smp_fetch_sc_get_gpc0(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002959 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002960{
Willy Tarreau192252e2015-04-04 01:47:55 +02002961 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau30b20462013-07-22 19:46:52 +02002962
2963 if (!stkctr)
2964 return 0;
2965
Willy Tarreau37406352012-04-23 16:16:37 +02002966 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002967 smp->type = SMP_T_UINT;
2968 smp->data.uint = 0;
Willy Tarreau30b20462013-07-22 19:46:52 +02002969
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002970 if (stkctr_entry(stkctr) != NULL) {
2971 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002972 if (!ptr)
2973 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002974 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002975 }
2976 return 1;
2977}
2978
Willy Tarreau87b09662015-04-03 00:22:06 +02002979/* set <smp> to the General Purpose Counter 0's event rate from the stream's
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002980 * tracked frontend counters or from the src.
2981 * Supports being called as "sc[0-9]_gpc0_rate" or "src_gpc0_rate" only.
2982 * Value zero is returned if the key is new.
2983 */
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002984static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002985smp_fetch_sc_gpc0_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002986 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002987{
Willy Tarreau192252e2015-04-04 01:47:55 +02002988 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002989
2990 if (!stkctr)
2991 return 0;
2992
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002993 smp->flags = SMP_F_VOL_TEST;
2994 smp->type = SMP_T_UINT;
2995 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002996 if (stkctr_entry(stkctr) != NULL) {
2997 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002998 if (!ptr)
2999 return 0; /* parameter not stored */
3000 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Willy Tarreaub5e0af02013-07-22 23:47:07 +02003001 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02003002 }
3003 return 1;
3004}
3005
Willy Tarreau87b09662015-04-03 00:22:06 +02003006/* Increment the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau710d38c2013-07-23 00:07:04 +02003007 * frontend counters and return it into temp integer.
3008 * Supports being called as "sc[0-9]_inc_gpc0" or "src_inc_gpc0" only.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02003009 */
3010static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003011smp_fetch_sc_inc_gpc0(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003012 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02003013{
Willy Tarreau192252e2015-04-04 01:47:55 +02003014 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau710d38c2013-07-23 00:07:04 +02003015
3016 if (!stkctr)
3017 return 0;
3018
Willy Tarreau37406352012-04-23 16:16:37 +02003019 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003020 smp->type = SMP_T_UINT;
3021 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003022 if (stkctr_entry(stkctr) != NULL) {
Willy Tarreauba2ffd12013-05-29 15:54:14 +02003023 void *ptr;
3024
3025 /* First, update gpc0_rate if it's tracked. Second, update its
3026 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
3027 */
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003028 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02003029 if (ptr) {
3030 update_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Willy Tarreau710d38c2013-07-23 00:07:04 +02003031 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02003032 smp->data.uint = (&stktable_data_cast(ptr, gpc0_rate))->curr_ctr;
3033 }
3034
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003035 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02003036 if (ptr)
3037 smp->data.uint = ++stktable_data_cast(ptr, gpc0);
3038
Willy Tarreauc3bd9722010-06-20 12:47:25 +02003039 }
3040 return 1;
3041}
3042
Willy Tarreau87b09662015-04-03 00:22:06 +02003043/* Clear the General Purpose Counter 0 value from the stream's tracked
Willy Tarreaub9f441d2013-07-23 00:10:35 +02003044 * frontend counters and return its previous value into temp integer.
3045 * Supports being called as "sc[0-9]_clr_gpc0" or "src_clr_gpc0" only.
Willy Tarreauf73cd112011-08-13 01:45:16 +02003046 */
3047static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003048smp_fetch_sc_clr_gpc0(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003049 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf73cd112011-08-13 01:45:16 +02003050{
Willy Tarreau192252e2015-04-04 01:47:55 +02003051 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaub9f441d2013-07-23 00:10:35 +02003052
3053 if (!stkctr)
3054 return 0;
3055
Willy Tarreau37406352012-04-23 16:16:37 +02003056 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003057 smp->type = SMP_T_UINT;
3058 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003059 if (stkctr_entry(stkctr) != NULL) {
3060 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02003061 if (!ptr)
3062 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003063 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02003064 stktable_data_cast(ptr, gpc0) = 0;
3065 }
3066 return 1;
3067}
3068
Willy Tarreau87b09662015-04-03 00:22:06 +02003069/* set <smp> to the cumulated number of connections from the stream's tracked
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02003070 * frontend counters. Supports being called as "sc[0-9]_conn_cnt" or
3071 * "src_conn_cnt" only.
3072 */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02003073static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003074smp_fetch_sc_conn_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003075 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02003076{
Willy Tarreau192252e2015-04-04 01:47:55 +02003077 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02003078
3079 if (!stkctr)
3080 return 0;
3081
Willy Tarreau37406352012-04-23 16:16:37 +02003082 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003083 smp->type = SMP_T_UINT;
3084 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003085 if (stkctr_entry(stkctr) != NULL) {
3086 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CNT);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02003087 if (!ptr)
3088 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003089 smp->data.uint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02003090 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02003091 return 1;
3092}
3093
Willy Tarreau87b09662015-04-03 00:22:06 +02003094/* set <smp> to the connection rate from the stream's tracked frontend
Willy Tarreauc8c65702013-07-23 15:09:35 +02003095 * counters. Supports being called as "sc[0-9]_conn_rate" or "src_conn_rate"
3096 * only.
3097 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02003098static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003099smp_fetch_sc_conn_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003100 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02003101{
Willy Tarreau192252e2015-04-04 01:47:55 +02003102 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreauc8c65702013-07-23 15:09:35 +02003103
3104 if (!stkctr)
3105 return 0;
3106
Willy Tarreau37406352012-04-23 16:16:37 +02003107 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003108 smp->type = SMP_T_UINT;
3109 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003110 if (stkctr_entry(stkctr) != NULL) {
3111 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003112 if (!ptr)
3113 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003114 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreauc8c65702013-07-23 15:09:35 +02003115 stkctr->table->data_arg[STKTABLE_DT_CONN_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003116 }
3117 return 1;
3118}
3119
Willy Tarreau87b09662015-04-03 00:22:06 +02003120/* set temp integer to the number of connections from the stream's source address
Willy Tarreau8b22a712010-06-18 17:46:06 +02003121 * in the table pointed to by expr, after updating it.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003122 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02003123 */
3124static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003125smp_fetch_src_updt_conn_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003126 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8b22a712010-06-18 17:46:06 +02003127{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003128 struct connection *conn = objt_conn(sess->origin);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003129 struct stksess *ts;
3130 struct stktable_key *key;
3131 void *ptr;
3132
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003133 if (!conn)
3134 return 0;
3135
Thierry FOURNIER74c219d2014-04-14 14:35:40 +02003136 key = addr_to_stktable_key(&conn->addr.from, px->table.type);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003137 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003138 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02003139
Willy Tarreau24e32d82012-04-23 23:55:44 +02003140 px = args->data.prx;
Willy Tarreau8b22a712010-06-18 17:46:06 +02003141
Willy Tarreau1f7e9252010-06-20 12:27:21 +02003142 if ((ts = stktable_update_key(&px->table, key)) == NULL)
3143 /* entry does not exist and could not be created */
3144 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02003145
3146 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
3147 if (!ptr)
3148 return 0; /* parameter not stored in this table */
3149
Willy Tarreauf853c462012-04-23 18:53:56 +02003150 smp->type = SMP_T_UINT;
3151 smp->data.uint = ++stktable_data_cast(ptr, conn_cnt);
Willy Tarreau37406352012-04-23 16:16:37 +02003152 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau8b22a712010-06-18 17:46:06 +02003153 return 1;
3154}
3155
Willy Tarreau87b09662015-04-03 00:22:06 +02003156/* set <smp> to the number of concurrent connections from the stream's tracked
Willy Tarreauf44a5532013-07-23 15:17:53 +02003157 * frontend counters. Supports being called as "sc[0-9]_conn_cur" or
3158 * "src_conn_cur" only.
3159 */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003160static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003161smp_fetch_sc_conn_cur(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003162 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003163{
Willy Tarreau192252e2015-04-04 01:47:55 +02003164 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreauf44a5532013-07-23 15:17:53 +02003165
3166 if (!stkctr)
3167 return 0;
3168
Willy Tarreau37406352012-04-23 16:16:37 +02003169 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003170 smp->type = SMP_T_UINT;
3171 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003172 if (stkctr_entry(stkctr) != NULL) {
3173 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CUR);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003174 if (!ptr)
3175 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003176 smp->data.uint = stktable_data_cast(ptr, conn_cur);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003177 }
3178 return 1;
3179}
3180
Willy Tarreau87b09662015-04-03 00:22:06 +02003181/* set <smp> to the cumulated number of streams from the stream's tracked
Willy Tarreau20843082013-07-23 15:35:33 +02003182 * frontend counters. Supports being called as "sc[0-9]_sess_cnt" or
3183 * "src_sess_cnt" only.
3184 */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003185static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003186smp_fetch_sc_sess_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003187 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003188{
Willy Tarreau192252e2015-04-04 01:47:55 +02003189 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau20843082013-07-23 15:35:33 +02003190
3191 if (!stkctr)
3192 return 0;
3193
Willy Tarreau37406352012-04-23 16:16:37 +02003194 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003195 smp->type = SMP_T_UINT;
3196 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003197 if (stkctr_entry(stkctr) != NULL) {
3198 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003199 if (!ptr)
3200 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003201 smp->data.uint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003202 }
3203 return 1;
3204}
3205
Willy Tarreau87b09662015-04-03 00:22:06 +02003206/* set <smp> to the stream rate from the stream's tracked frontend counters.
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003207 * Supports being called as "sc[0-9]_sess_rate" or "src_sess_rate" only.
3208 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02003209static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003210smp_fetch_sc_sess_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003211 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02003212{
Willy Tarreau192252e2015-04-04 01:47:55 +02003213 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003214
3215 if (!stkctr)
3216 return 0;
3217
Willy Tarreau37406352012-04-23 16:16:37 +02003218 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003219 smp->type = SMP_T_UINT;
3220 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003221 if (stkctr_entry(stkctr) != NULL) {
3222 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003223 if (!ptr)
3224 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003225 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003226 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003227 }
3228 return 1;
3229}
3230
Willy Tarreau87b09662015-04-03 00:22:06 +02003231/* set <smp> to the cumulated number of HTTP requests from the stream's tracked
Willy Tarreau91200da2013-07-23 15:55:19 +02003232 * frontend counters. Supports being called as "sc[0-9]_http_req_cnt" or
3233 * "src_http_req_cnt" only.
3234 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003235static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003236smp_fetch_sc_http_req_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003237 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003238{
Willy Tarreau192252e2015-04-04 01:47:55 +02003239 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau91200da2013-07-23 15:55:19 +02003240
3241 if (!stkctr)
3242 return 0;
3243
Willy Tarreau37406352012-04-23 16:16:37 +02003244 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003245 smp->type = SMP_T_UINT;
3246 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003247 if (stkctr_entry(stkctr) != NULL) {
3248 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003249 if (!ptr)
3250 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003251 smp->data.uint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003252 }
3253 return 1;
3254}
3255
Willy Tarreau87b09662015-04-03 00:22:06 +02003256/* set <smp> to the HTTP request rate from the stream's tracked frontend
Willy Tarreaucf477632013-07-23 16:04:37 +02003257 * counters. Supports being called as "sc[0-9]_http_req_rate" or
3258 * "src_http_req_rate" only.
3259 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003260static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003261smp_fetch_sc_http_req_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003262 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003263{
Willy Tarreau192252e2015-04-04 01:47:55 +02003264 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaucf477632013-07-23 16:04:37 +02003265
3266 if (!stkctr)
3267 return 0;
3268
Willy Tarreau37406352012-04-23 16:16:37 +02003269 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003270 smp->type = SMP_T_UINT;
3271 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003272 if (stkctr_entry(stkctr) != NULL) {
3273 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003274 if (!ptr)
3275 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003276 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreaucf477632013-07-23 16:04:37 +02003277 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003278 }
3279 return 1;
3280}
3281
Willy Tarreau87b09662015-04-03 00:22:06 +02003282/* set <smp> to the cumulated number of HTTP requests errors from the stream's
Willy Tarreau30d07c32013-07-23 16:45:38 +02003283 * tracked frontend counters. Supports being called as "sc[0-9]_http_err_cnt" or
3284 * "src_http_err_cnt" only.
3285 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003286static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003287smp_fetch_sc_http_err_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003288 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003289{
Willy Tarreau192252e2015-04-04 01:47:55 +02003290 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau30d07c32013-07-23 16:45:38 +02003291
3292 if (!stkctr)
3293 return 0;
3294
Willy Tarreau37406352012-04-23 16:16:37 +02003295 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003296 smp->type = SMP_T_UINT;
3297 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003298 if (stkctr_entry(stkctr) != NULL) {
3299 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003300 if (!ptr)
3301 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003302 smp->data.uint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003303 }
3304 return 1;
3305}
3306
Willy Tarreau87b09662015-04-03 00:22:06 +02003307/* set <smp> to the HTTP request error rate from the stream's tracked frontend
Willy Tarreau9daf2622013-07-23 16:48:54 +02003308 * counters. Supports being called as "sc[0-9]_http_err_rate" or
3309 * "src_http_err_rate" only.
3310 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003311static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003312smp_fetch_sc_http_err_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003313 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003314{
Willy Tarreau192252e2015-04-04 01:47:55 +02003315 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau9daf2622013-07-23 16:48:54 +02003316
3317 if (!stkctr)
3318 return 0;
3319
Willy Tarreau37406352012-04-23 16:16:37 +02003320 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003321 smp->type = SMP_T_UINT;
3322 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003323 if (stkctr_entry(stkctr) != NULL) {
3324 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003325 if (!ptr)
3326 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003327 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau9daf2622013-07-23 16:48:54 +02003328 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003329 }
3330 return 1;
3331}
3332
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003333/* set <smp> to the number of kbytes received from clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003334 * stream's tracked frontend counters. Supports being called as
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003335 * "sc[0-9]_kbytes_in" or "src_kbytes_in" only.
3336 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003337static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003338smp_fetch_sc_kbytes_in(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003339 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003340{
Willy Tarreau192252e2015-04-04 01:47:55 +02003341 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003342
3343 if (!stkctr)
3344 return 0;
3345
Willy Tarreau37406352012-04-23 16:16:37 +02003346 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003347 smp->type = SMP_T_UINT;
3348 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003349 if (stkctr_entry(stkctr) != NULL) {
3350 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003351 if (!ptr)
3352 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003353 smp->data.uint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003354 }
3355 return 1;
3356}
3357
Willy Tarreau613fe992013-07-23 17:39:19 +02003358/* set <smp> to the data rate received from clients in bytes/s, as found
Willy Tarreau87b09662015-04-03 00:22:06 +02003359 * in the stream's tracked frontend counters. Supports being called as
Willy Tarreau613fe992013-07-23 17:39:19 +02003360 * "sc[0-9]_bytes_in_rate" or "src_bytes_in_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003361 */
3362static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003363smp_fetch_sc_bytes_in_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003364 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003365{
Willy Tarreau192252e2015-04-04 01:47:55 +02003366 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau613fe992013-07-23 17:39:19 +02003367
3368 if (!stkctr)
3369 return 0;
3370
Willy Tarreau37406352012-04-23 16:16:37 +02003371 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003372 smp->type = SMP_T_UINT;
3373 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003374 if (stkctr_entry(stkctr) != NULL) {
3375 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003376 if (!ptr)
3377 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003378 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau613fe992013-07-23 17:39:19 +02003379 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003380 }
3381 return 1;
3382}
3383
Willy Tarreau53aea102013-07-23 17:39:02 +02003384/* set <smp> to the number of kbytes sent to clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003385 * stream's tracked frontend counters. Supports being called as
Willy Tarreau53aea102013-07-23 17:39:02 +02003386 * "sc[0-9]_kbytes_out" or "src_kbytes_out" only.
3387 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003388static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003389smp_fetch_sc_kbytes_out(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +02003390 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003391{
Willy Tarreau192252e2015-04-04 01:47:55 +02003392 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau53aea102013-07-23 17:39:02 +02003393
3394 if (!stkctr)
3395 return 0;
3396
Willy Tarreau37406352012-04-23 16:16:37 +02003397 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003398 smp->type = SMP_T_UINT;
3399 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003400 if (stkctr_entry(stkctr) != NULL) {
3401 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003402 if (!ptr)
3403 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003404 smp->data.uint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003405 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003406 return 1;
3407}
3408
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003409/* set <smp> to the data rate sent to clients in bytes/s, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003410 * stream's tracked frontend counters. Supports being called as
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003411 * "sc[0-9]_bytes_out_rate" or "src_bytes_out_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003412 */
3413static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003414smp_fetch_sc_bytes_out_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003415 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003416{
Willy Tarreau192252e2015-04-04 01:47:55 +02003417 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003418
3419 if (!stkctr)
3420 return 0;
3421
Willy Tarreau37406352012-04-23 16:16:37 +02003422 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003423 smp->type = SMP_T_UINT;
3424 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003425 if (stkctr_entry(stkctr) != NULL) {
3426 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003427 if (!ptr)
3428 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003429 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003430 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003431 }
3432 return 1;
3433}
3434
Willy Tarreau87b09662015-04-03 00:22:06 +02003435/* set <smp> to the number of active trackers on the SC entry in the stream's
Willy Tarreau563eef42013-07-23 18:32:02 +02003436 * tracked frontend counters. Supports being called as "sc[0-9]_trackers" only.
3437 */
Willy Tarreau2406db42012-12-09 12:16:43 +01003438static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003439smp_fetch_sc_trackers(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003440 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2406db42012-12-09 12:16:43 +01003441{
Willy Tarreau192252e2015-04-04 01:47:55 +02003442 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau2406db42012-12-09 12:16:43 +01003443
Willy Tarreau563eef42013-07-23 18:32:02 +02003444 if (!stkctr)
Willy Tarreau2406db42012-12-09 12:16:43 +01003445 return 0;
3446
Willy Tarreau563eef42013-07-23 18:32:02 +02003447 smp->flags = SMP_F_VOL_TEST;
3448 smp->type = SMP_T_UINT;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003449 smp->data.uint = stkctr_entry(stkctr)->ref_cnt;
Willy Tarreau563eef42013-07-23 18:32:02 +02003450 return 1;
Willy Tarreaue25c9172013-05-28 18:32:20 +02003451}
3452
Willy Tarreau34db1082012-04-19 17:16:54 +02003453/* set temp integer to the number of used entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003454 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003455 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003456static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003457smp_fetch_table_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003458 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003459{
Willy Tarreau37406352012-04-23 16:16:37 +02003460 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003461 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02003462 smp->data.uint = args->data.prx->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003463 return 1;
3464}
3465
Willy Tarreau34db1082012-04-19 17:16:54 +02003466/* set temp integer to the number of free entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003467 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003468 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003469static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003470smp_fetch_table_avl(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003471 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003472{
Willy Tarreau24e32d82012-04-23 23:55:44 +02003473 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003474 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003475 smp->type = SMP_T_UINT;
3476 smp->data.uint = px->table.size - px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003477 return 1;
3478}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003479
Willy Tarreau61612d42012-04-19 18:42:05 +02003480/* Note: must not be declared <const> as its list will be overwritten.
3481 * Please take care of keeping this list alphabetically sorted.
3482 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003483static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau281c7992013-01-08 01:23:27 +01003484 { /* END */ },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003485}};
3486
Willy Tarreau281c7992013-01-08 01:23:27 +01003487/* Note: must not be declared <const> as its list will be overwritten.
3488 * Please take care of keeping this list alphabetically sorted.
3489 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003490static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
Willy Tarreau0f791d42013-07-23 19:56:43 +02003491 { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3492 { "sc_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3493 { "sc_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3494 { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3495 { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3496 { "sc_conn_rate", smp_fetch_sc_conn_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3497 { "sc_get_gpc0", smp_fetch_sc_get_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3498 { "sc_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3499 { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3500 { "sc_http_err_rate", smp_fetch_sc_http_err_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3501 { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3502 { "sc_http_req_rate", smp_fetch_sc_http_req_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3503 { "sc_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3504 { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3505 { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3506 { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3507 { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3508 { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,UINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3509 { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3510 { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3511 { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3512 { "sc0_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3513 { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3514 { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3515 { "sc0_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3516 { "sc0_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3517 { "sc0_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3518 { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3519 { "sc0_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3520 { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3521 { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3522 { "sc0_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3523 { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3524 { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3525 { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3526 { "sc0_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3527 { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3528 { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3529 { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3530 { "sc1_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3531 { "sc1_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3532 { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3533 { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3534 { "sc1_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3535 { "sc1_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3536 { "sc1_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3537 { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3538 { "sc1_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3539 { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3540 { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3541 { "sc1_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3542 { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3543 { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3544 { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3545 { "sc1_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3546 { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3547 { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3548 { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3549 { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3550 { "sc2_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3551 { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3552 { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3553 { "sc2_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3554 { "sc2_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3555 { "sc2_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3556 { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3557 { "sc2_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3558 { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3559 { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3560 { "sc2_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3561 { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3562 { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3563 { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3564 { "sc2_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3565 { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3566 { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3567 { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3568 { "src_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3569 { "src_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3570 { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3571 { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3572 { "src_conn_rate", smp_fetch_sc_conn_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3573 { "src_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3574 { "src_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3575 { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3576 { "src_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3577 { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3578 { "src_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3579 { "src_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3580 { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3581 { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3582 { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3583 { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3584 { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3585 { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3586 { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
Willy Tarreau281c7992013-01-08 01:23:27 +01003587 { /* END */ },
3588}};
3589
Willy Tarreau8b22a712010-06-18 17:46:06 +02003590__attribute__((constructor))
Willy Tarreau87b09662015-04-03 00:22:06 +02003591static void __stream_init(void)
Willy Tarreau8b22a712010-06-18 17:46:06 +02003592{
Willy Tarreau281c7992013-01-08 01:23:27 +01003593 sample_register_fetches(&smp_fetch_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003594 acl_register_keywords(&acl_kws);
3595}
3596
Willy Tarreaubaaee002006-06-26 02:48:02 +02003597/*
3598 * Local variables:
3599 * c-indent-level: 8
3600 * c-basic-offset: 8
3601 * End:
3602 */