blob: 5280e372f2a9c5b14472005acd8fd8e7270b2efa [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 Tarreau8a8d83b2015-04-13 13:24:54 +020022#include <types/applet.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020023#include <types/capture.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010024#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020025
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020026#include <proto/acl.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020027#include <proto/arg.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010028#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020029#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010030#include <proto/checks.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020031#include <proto/connection.h>
Willy Tarreau5ca791d2009-08-16 19:06:42 +020032#include <proto/dumpstats.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020033#include <proto/fd.h>
Willy Tarreau91c43d72010-06-20 11:19:22 +020034#include <proto/freq_ctr.h>
Willy Tarreau3041b9f2010-10-15 23:25:20 +020035#include <proto/frontend.h>
Willy Tarreau8d5d7f22007-01-21 19:16:41 +010036#include <proto/hdr_idx.h>
Thierry FOURNIER65f34c62015-02-16 20:11:43 +010037#include <proto/hlua.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020038#include <proto/listener.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020039#include <proto/log.h>
Willy Tarreaucbaaec42012-09-06 11:32:07 +020040#include <proto/raw_sock.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020041#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020042#include <proto/stream.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010043#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010044#include <proto/proto_http.h>
45#include <proto/proto_tcp.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020046#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010048#include <proto/server.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020049#include <proto/sample.h>
Emeric Brun1d33b292010-01-04 15:47:17 +010050#include <proto/stick_table.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010051#include <proto/stream_interface.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010052#include <proto/task.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020053#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020054
Willy Tarreau87b09662015-04-03 00:22:06 +020055struct pool_head *pool2_stream;
56struct list streams;
Willy Tarreaubaaee002006-06-26 02:48:02 +020057
Willy Tarreau87b09662015-04-03 00:22:06 +020058/* list of streams waiting for at least one buffer */
Willy Tarreaubf883e02014-11-25 21:10:35 +010059struct list buffer_wq = LIST_HEAD_INIT(buffer_wq);
60
Willy Tarreau9903f0e2015-04-04 18:50:31 +020061/* This function is called from the session handler which detects the end of
Willy Tarreau73b65ac2015-04-08 18:26:29 +020062 * handshake, in order to complete initialization of a valid stream. It must be
63 * called with a session (which may be embryonic). It returns the pointer to
64 * the newly created stream, or NULL in case of fatal error. The client-facing
65 * end point is assigned to <origin>, which must be valid. The task's context
66 * is set to the new stream, and its function is set to process_stream().
67 * Target and analysers are null.
Willy Tarreau2542b532012-08-31 16:01:23 +020068 */
Willy Tarreau73b65ac2015-04-08 18:26:29 +020069struct stream *stream_new(struct session *sess, struct task *t, enum obj_type *origin)
Willy Tarreau2542b532012-08-31 16:01:23 +020070{
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020071 struct stream *s;
Willy Tarreau73b65ac2015-04-08 18:26:29 +020072 struct connection *conn = objt_conn(origin);
73 struct appctx *appctx = objt_appctx(origin);
Thierry FOURNIER827752e2015-08-18 11:34:18 +020074 int i;
Willy Tarreau2542b532012-08-31 16:01:23 +020075
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020076 if (unlikely((s = pool_alloc2(pool2_stream)) == NULL))
Willy Tarreau02d86382015-04-05 12:00:52 +020077 return s;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020078
79 /* minimum stream initialization required for an embryonic stream is
80 * fairly low. We need very little to execute L4 ACLs, then we need a
81 * task to make the client-side connection live on its own.
82 * - flags
83 * - stick-entry tracking
84 */
85 s->flags = 0;
Willy Tarreaufb9f5842015-04-05 18:19:23 +020086 s->logs.logwait = sess->fe->to_log;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020087 s->logs.level = 0;
Willy Tarreau99eb0f12015-04-05 12:03:54 +020088 s->logs.accept_date = sess->accept_date; /* user-visible date for logging */
89 s->logs.tv_accept = sess->tv_accept; /* corrected date for internal use */
90 tv_zero(&s->logs.tv_request);
91 s->logs.t_queue = -1;
92 s->logs.t_connect = -1;
93 s->logs.t_data = -1;
94 s->logs.t_close = 0;
95 s->logs.bytes_in = s->logs.bytes_out = 0;
96 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
97 s->logs.srv_queue_size = 0; /* we will get this number soon */
98
99 /* default logging function */
100 s->do_log = strm_log;
101
102 /* default error reporting function, may be changed by analysers */
103 s->srv_error = default_srv_error;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200104
105 /* Initialise the current rule list pointer to NULL. We are sure that
106 * any rulelist match the NULL pointer.
107 */
108 s->current_rule_list = NULL;
Remi Gacogne7fb9de22015-07-22 17:10:58 +0200109 s->current_rule = NULL;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200110
Thierry FOURNIER827752e2015-08-18 11:34:18 +0200111 /* Copy SC counters for the stream. Each SC counter will be used by
112 * the stream, so we need to increment the refcount.
113 */
Thierry FOURNIERc8fdb982015-08-16 12:03:39 +0200114 memcpy(s->stkctr, sess->stkctr, sizeof(s->stkctr));
Thierry FOURNIER827752e2015-08-18 11:34:18 +0200115 for (i = 0; i < MAX_SESS_STKCTR; i++)
116 if (stkctr_entry(&s->stkctr[i]))
117 stkctr_entry(&s->stkctr[i])->ref_cnt++;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200118
119 s->sess = sess;
120 s->si[0].flags = SI_FL_NONE;
121 s->si[1].flags = SI_FL_ISBACK;
122
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200123 s->uniq_id = global.req_count++;
124
Willy Tarreau87b09662015-04-03 00:22:06 +0200125 /* OK, we're keeping the stream, so let's properly initialize the stream */
126 LIST_ADDQ(&streams, &s->list);
Willy Tarreau2542b532012-08-31 16:01:23 +0200127 LIST_INIT(&s->back_refs);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100128 LIST_INIT(&s->buffer_wait);
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200129
Willy Tarreaue7dff022015-04-03 01:14:29 +0200130 s->flags |= SF_INITIALIZED;
Willy Tarreau2542b532012-08-31 16:01:23 +0200131 s->unique_id = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200132
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200133 s->task = t;
Willy Tarreaud1769b82015-04-06 00:25:48 +0200134 t->process = process_stream;
Willy Tarreau2542b532012-08-31 16:01:23 +0200135 t->context = s;
136 t->expire = TICK_ETERNITY;
137
Willy Tarreau87b09662015-04-03 00:22:06 +0200138 /* Note: initially, the stream's backend points to the frontend.
Willy Tarreau2542b532012-08-31 16:01:23 +0200139 * This changes later when switching rules are executed or
140 * when the default backend is assigned.
141 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200142 s->be = sess->fe;
William Lallemand82fe75c2012-10-23 10:25:10 +0200143 s->comp_algo = NULL;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100144 s->req.buf = s->res.buf = NULL;
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200145 s->req_cap = NULL;
146 s->res_cap = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200147
Willy Tarreauebcd4842015-06-19 11:59:02 +0200148 /* Initialise all the variables contexts even if not used.
149 * This permits to prune these contexts without errors.
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200150 */
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200151 vars_init(&s->vars_txn, SCOPE_TXN);
152 vars_init(&s->vars_reqres, SCOPE_REQ);
153
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200154 /* this part should be common with other protocols */
Willy Tarreau819d3322014-11-28 12:12:34 +0100155 si_reset(&s->si[0]);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200156 si_set_state(&s->si[0], SI_ST_EST);
157
Willy Tarreau1f52bb22015-04-04 14:28:46 +0200158 /* attach the incoming connection to the stream interface now. */
Willy Tarreauf2b98742015-04-05 01:30:42 +0200159 if (conn)
160 si_attach_conn(&s->si[0], conn);
Willy Tarreau470280e2015-04-05 01:33:13 +0200161 else if (appctx)
162 si_attach_appctx(&s->si[0], appctx);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200163
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200164 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200165 s->si[0].flags |= SI_FL_INDEP_STR;
166
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200167 /* pre-initialize the other side's stream interface to an INIT state. The
168 * callbacks will be initialized before attempting to connect.
169 */
Willy Tarreau819d3322014-11-28 12:12:34 +0100170 si_reset(&s->si[1]);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200171
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200172 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200173 s->si[1].flags |= SI_FL_INDEP_STR;
174
Willy Tarreau87b09662015-04-03 00:22:06 +0200175 stream_init_srv_conn(s);
Willy Tarreaud1769b82015-04-06 00:25:48 +0200176 s->target = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200177 s->pend_pos = NULL;
178
179 /* init store persistence */
180 s->store_count = 0;
181
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100182 channel_init(&s->req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100183 s->req.flags |= CF_READ_ATTACHED; /* the producer is already connected */
Willy Tarreaud1769b82015-04-06 00:25:48 +0200184 s->req.analysers = 0;
185 channel_auto_connect(&s->req); /* don't wait to establish connection */
186 channel_auto_close(&s->req); /* let the producer forward close requests */
Willy Tarreauc8815ef2015-04-05 18:15:59 +0200187
188 s->req.rto = sess->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100189 s->req.wto = TICK_ETERNITY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100190 s->req.rex = TICK_ETERNITY;
191 s->req.wex = TICK_ETERNITY;
192 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200193
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100194 channel_init(&s->res);
Willy Tarreauef573c02014-11-28 14:17:09 +0100195 s->res.flags |= CF_ISRESP;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100196 s->res.analysers = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200197
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200198 if (sess->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100199 s->req.flags |= CF_NEVER_WAIT;
200 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +0200201 }
202
Willy Tarreauc8815ef2015-04-05 18:15:59 +0200203 s->res.wto = sess->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100204 s->res.rto = TICK_ETERNITY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100205 s->res.rex = TICK_ETERNITY;
206 s->res.wex = TICK_ETERNITY;
207 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200208
Willy Tarreaueee5b512015-04-03 23:46:31 +0200209 s->txn = NULL;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100210
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100211 HLUA_INIT(&s->hlua);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100212
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200213 /* finish initialization of the accepted file descriptor */
Willy Tarreauf2b98742015-04-05 01:30:42 +0200214 if (conn)
215 conn_data_want_recv(conn);
Willy Tarreau470280e2015-04-05 01:33:13 +0200216 else if (appctx)
Willy Tarreaufe127932015-04-21 19:23:39 +0200217 si_applet_want_get(&s->si[0]);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200218
Willy Tarreaufb9f5842015-04-05 18:19:23 +0200219 if (sess->fe->accept && sess->fe->accept(s) < 0)
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200220 goto out_fail_accept;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200221
222 /* it is important not to call the wakeup function directly but to
223 * pass through task_wakeup(), because this one knows how to apply
224 * priorities to tasks.
225 */
226 task_wakeup(t, TASK_WOKEN_INIT);
Willy Tarreau02d86382015-04-05 12:00:52 +0200227 return s;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200228
229 /* Error unrolling */
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200230 out_fail_accept:
Willy Tarreau3b246412014-11-25 17:10:33 +0100231 LIST_DEL(&s->list);
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200232 pool_free2(pool2_stream, s);
Willy Tarreau02d86382015-04-05 12:00:52 +0200233 return NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200234}
235
Willy Tarreaubaaee002006-06-26 02:48:02 +0200236/*
Willy Tarreau87b09662015-04-03 00:22:06 +0200237 * frees the context associated to a stream. It must have been removed first.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200238 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200239static void stream_free(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200240{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200241 struct session *sess = strm_sess(s);
242 struct proxy *fe = sess->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100243 struct bref *bref, *back;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200244 struct connection *cli_conn = objt_conn(sess->origin);
Willy Tarreaua4cda672010-06-06 18:28:49 +0200245 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100246
Willy Tarreaubaaee002006-06-26 02:48:02 +0200247 if (s->pend_pos)
248 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100249
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100250 if (objt_server(s->target)) { /* there may be requests left pending in queue */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200251 if (s->flags & SF_CURR_SESS) {
252 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100253 objt_server(s->target)->cur_sess--;
Willy Tarreau1e62de62008-11-11 20:20:02 +0100254 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100255 if (may_dequeue_tasks(objt_server(s->target), s->be))
256 process_srv_queue(objt_server(s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100257 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100258
Willy Tarreau7c669d72008-06-20 15:04:11 +0200259 if (unlikely(s->srv_conn)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200260 /* the stream still has a reserved slot on a server, but
Willy Tarreau7c669d72008-06-20 15:04:11 +0200261 * it should normally be only the same as the one above,
262 * so this should not happen in fact.
263 */
264 sess_change_server(s, NULL);
265 }
266
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100267 if (s->req.pipe)
268 put_pipe(s->req.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100269
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100270 if (s->res.pipe)
271 put_pipe(s->res.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100272
Willy Tarreaubf883e02014-11-25 21:10:35 +0100273 /* We may still be present in the buffer wait queue */
274 if (!LIST_ISEMPTY(&s->buffer_wait)) {
275 LIST_DEL(&s->buffer_wait);
276 LIST_INIT(&s->buffer_wait);
277 }
278
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100279 b_drop(&s->req.buf);
280 b_drop(&s->res.buf);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100281 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200282 stream_offer_buffers();
Willy Tarreau9b28e032012-10-12 23:49:43 +0200283
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100284 hlua_ctx_destroy(&s->hlua);
Willy Tarreaueee5b512015-04-03 23:46:31 +0200285 if (s->txn)
286 http_end_txn(s);
Willy Tarreau46023632010-01-07 22:51:47 +0100287
Willy Tarreau1e954912012-10-12 17:50:05 +0200288 /* ensure the client-side transport layer is destroyed */
Willy Tarreauf79c8172013-10-21 16:30:56 +0200289 if (cli_conn)
290 conn_force_close(cli_conn);
Willy Tarreau1e954912012-10-12 17:50:05 +0200291
Willy Tarreaua4cda672010-06-06 18:28:49 +0200292 for (i = 0; i < s->store_count; i++) {
293 if (!s->store[i].ts)
294 continue;
295 stksess_free(s->store[i].table, s->store[i].ts);
296 s->store[i].ts = NULL;
297 }
298
Willy Tarreaueee5b512015-04-03 23:46:31 +0200299 if (s->txn) {
300 pool_free2(pool2_hdr_idx, s->txn->hdr_idx.v);
301 pool_free2(pool2_http_txn, s->txn);
302 s->txn = NULL;
303 }
304
Willy Tarreau92fb9832007-10-16 17:34:28 +0200305 if (fe) {
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200306 pool_free2(fe->rsp_cap_pool, s->res_cap);
307 pool_free2(fe->req_cap_pool, s->req_cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200308 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100309
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200310 /* Cleanup all variable contexts. */
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200311 vars_prune(&s->vars_txn, s);
312 vars_prune(&s->vars_reqres, s);
313
Willy Tarreau87b09662015-04-03 00:22:06 +0200314 stream_store_counters(s);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200315
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100316 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100317 /* we have to unlink all watchers. We must not relink them if
Willy Tarreau87b09662015-04-03 00:22:06 +0200318 * this stream was the last one in the list.
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100319 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100320 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100321 LIST_INIT(&bref->users);
Willy Tarreau87b09662015-04-03 00:22:06 +0200322 if (s->list.n != &streams)
323 LIST_ADDQ(&LIST_ELEM(s->list.n, struct stream *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100324 bref->ref = s->list.n;
325 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100326 LIST_DEL(&s->list);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200327 si_release_endpoint(&s->si[1]);
328 si_release_endpoint(&s->si[0]);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200329
330 /* FIXME: for now we have a 1:1 relation between stream and session so
331 * the stream must free the session.
332 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200333 pool_free2(pool2_stream, s);
Willy Tarreau11c36242015-04-04 15:54:03 +0200334 session_free(sess);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200335
336 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200337 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +0200338 pool_flush2(pool2_buffer);
Willy Tarreau63986c72015-04-03 22:55:33 +0200339 pool_flush2(pool2_http_txn);
Willy Tarreau34eb6712011-10-24 18:15:04 +0200340 pool_flush2(pool2_hdr_idx);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200341 pool_flush2(pool2_requri);
342 pool_flush2(pool2_capture);
Willy Tarreau87b09662015-04-03 00:22:06 +0200343 pool_flush2(pool2_stream);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200344 pool_flush2(pool2_session);
Willy Tarreau3a5e0602014-11-13 16:46:28 +0100345 pool_flush2(pool2_connection);
346 pool_flush2(pool2_pendconn);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200347 pool_flush2(fe->req_cap_pool);
348 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200349 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200350}
351
Willy Tarreau78955f42014-12-28 13:09:02 +0100352/* Allocates a receive buffer for channel <chn>, but only if it's guaranteed
353 * that it's not the last available buffer or it's the response buffer. Unless
354 * the buffer is the response buffer, an extra control is made so that we always
355 * keep <tune.buffers.reserved> buffers available after this allocation. To be
356 * called at the beginning of recv() callbacks to ensure that the required
357 * buffers are properly allocated. Returns 0 in case of failure, non-zero
358 * otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100359 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200360int stream_alloc_recv_buffer(struct channel *chn)
Willy Tarreau656859d2014-11-25 19:46:36 +0100361{
Willy Tarreau87b09662015-04-03 00:22:06 +0200362 struct stream *s;
Willy Tarreau656859d2014-11-25 19:46:36 +0100363 struct buffer *b;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100364 int margin = 0;
Willy Tarreau656859d2014-11-25 19:46:36 +0100365
Willy Tarreau78955f42014-12-28 13:09:02 +0100366 if (!(chn->flags & CF_ISRESP))
Willy Tarreaua24adf02014-11-27 01:11:56 +0100367 margin = global.tune.reserved_bufs;
368
Willy Tarreau78955f42014-12-28 13:09:02 +0100369 s = chn_sess(chn);
370
371 b = b_alloc_margin(&chn->buf, margin);
Willy Tarreau656859d2014-11-25 19:46:36 +0100372 if (b)
373 return 1;
374
Willy Tarreaubf883e02014-11-25 21:10:35 +0100375 if (LIST_ISEMPTY(&s->buffer_wait))
376 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100377 return 0;
378}
379
Willy Tarreau87b09662015-04-03 00:22:06 +0200380/* Allocates a work buffer for stream <s>. It is meant to be called inside
381 * process_stream(). It will only allocate the side needed for the function
Willy Tarreaubc39a5d2015-04-20 15:52:18 +0200382 * to work fine, which is the response buffer so that an error message may be
383 * built and returned. Response buffers may be allocated from the reserve, this
384 * is critical to ensure that a response may always flow and will never block a
385 * server from releasing a connection. Returns 0 in case of failure, non-zero
386 * otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100387 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200388int stream_alloc_work_buffer(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100389{
Willy Tarreaubf883e02014-11-25 21:10:35 +0100390 if (!LIST_ISEMPTY(&s->buffer_wait)) {
391 LIST_DEL(&s->buffer_wait);
392 LIST_INIT(&s->buffer_wait);
393 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100394
Willy Tarreaubc39a5d2015-04-20 15:52:18 +0200395 if (b_alloc_margin(&s->res.buf, 0))
Willy Tarreau656859d2014-11-25 19:46:36 +0100396 return 1;
397
Willy Tarreaubf883e02014-11-25 21:10:35 +0100398 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100399 return 0;
400}
401
402/* releases unused buffers after processing. Typically used at the end of the
Willy Tarreaubf883e02014-11-25 21:10:35 +0100403 * update() functions. It will try to wake up as many tasks as the number of
Willy Tarreau87b09662015-04-03 00:22:06 +0200404 * buffers that it releases. In practice, most often streams are blocked on
Willy Tarreaubf883e02014-11-25 21:10:35 +0100405 * a single buffer, so it makes sense to try to wake two up when two buffers
406 * are released at once.
Willy Tarreau656859d2014-11-25 19:46:36 +0100407 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200408void stream_release_buffers(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100409{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100410 if (s->req.buf->size && buffer_empty(s->req.buf))
411 b_free(&s->req.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100412
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100413 if (s->res.buf->size && buffer_empty(s->res.buf))
414 b_free(&s->res.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100415
Willy Tarreaubf883e02014-11-25 21:10:35 +0100416 /* if we're certain to have at least 1 buffer available, and there is
417 * someone waiting, we can wake up a waiter and offer them.
418 */
Willy Tarreaua24adf02014-11-27 01:11:56 +0100419 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200420 stream_offer_buffers();
Willy Tarreaubf883e02014-11-25 21:10:35 +0100421}
422
Willy Tarreau87b09662015-04-03 00:22:06 +0200423/* Runs across the list of pending streams waiting for a buffer and wakes one
Willy Tarreaua24adf02014-11-27 01:11:56 +0100424 * up if buffers are available. Will stop when the run queue reaches <rqlimit>.
Willy Tarreau87b09662015-04-03 00:22:06 +0200425 * Should not be called directly, use stream_offer_buffers() instead.
Willy Tarreaubf883e02014-11-25 21:10:35 +0100426 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200427void __stream_offer_buffers(int rqlimit)
Willy Tarreaubf883e02014-11-25 21:10:35 +0100428{
Willy Tarreau87b09662015-04-03 00:22:06 +0200429 struct stream *sess, *bak;
Willy Tarreaubf883e02014-11-25 21:10:35 +0100430
431 list_for_each_entry_safe(sess, bak, &buffer_wq, buffer_wait) {
Willy Tarreaua24adf02014-11-27 01:11:56 +0100432 if (rqlimit <= run_queue)
433 break;
434
Willy Tarreaubf883e02014-11-25 21:10:35 +0100435 if (sess->task->state & TASK_RUNNING)
436 continue;
437
438 LIST_DEL(&sess->buffer_wait);
439 LIST_INIT(&sess->buffer_wait);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100440 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100441 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100442}
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200443
444/* perform minimal intializations, report 0 in case of error, 1 if OK. */
Willy Tarreau87b09662015-04-03 00:22:06 +0200445int init_stream()
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200446{
Willy Tarreau87b09662015-04-03 00:22:06 +0200447 LIST_INIT(&streams);
448 pool2_stream = create_pool("stream", sizeof(struct stream), MEM_F_SHARED);
449 return pool2_stream != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200450}
451
Willy Tarreau87b09662015-04-03 00:22:06 +0200452void stream_process_counters(struct stream *s)
Willy Tarreau30e71012007-11-26 20:15:35 +0100453{
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200454 struct session *sess = s->sess;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100455 unsigned long long bytes;
Emeric Brun57056f02015-06-15 18:29:57 +0200456 void *ptr1,*ptr2;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100457 int i;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100458
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100459 bytes = s->req.total - s->logs.bytes_in;
460 s->logs.bytes_in = s->req.total;
461 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200462 sess->fe->fe_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100463
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100464 s->be->be_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100465
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100466 if (objt_server(s->target))
467 objt_server(s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200468
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200469 if (sess->listener && sess->listener->counters)
470 sess->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200471
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100472 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200473 struct stkctr *stkctr = &s->stkctr[i];
474
475 if (!stkctr_entry(stkctr)) {
476 stkctr = &sess->stkctr[i];
477 if (!stkctr_entry(stkctr))
478 continue;
479 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200480
Emeric Brun57056f02015-06-15 18:29:57 +0200481 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
482 if (ptr1)
483 stktable_data_cast(ptr1, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200484
Emeric Brun57056f02015-06-15 18:29:57 +0200485 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
486 if (ptr2)
487 update_freq_ctr_period(&stktable_data_cast(ptr2, bytes_in_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200488 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Emeric Brun57056f02015-06-15 18:29:57 +0200489
490 /* If data was modified, we need to touch to re-schedule sync */
491 if (ptr1 || ptr2)
492 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreau30e71012007-11-26 20:15:35 +0100493 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100494 }
495
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100496 bytes = s->res.total - s->logs.bytes_out;
497 s->logs.bytes_out = s->res.total;
498 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200499 sess->fe->fe_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100500
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100501 s->be->be_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100502
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100503 if (objt_server(s->target))
504 objt_server(s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200505
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200506 if (sess->listener && sess->listener->counters)
507 sess->listener->counters->bytes_out += bytes;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200508
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100509 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200510 struct stkctr *stkctr = &s->stkctr[i];
511
512 if (!stkctr_entry(stkctr)) {
513 stkctr = &sess->stkctr[i];
514 if (!stkctr_entry(stkctr))
515 continue;
516 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200517
Emeric Brun57056f02015-06-15 18:29:57 +0200518 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
519 if (ptr1)
520 stktable_data_cast(ptr1, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200521
Emeric Brun57056f02015-06-15 18:29:57 +0200522 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
523 if (ptr2)
524 update_freq_ctr_period(&stktable_data_cast(ptr2, bytes_out_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200525 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Emeric Brun57056f02015-06-15 18:29:57 +0200526
527 /* If data was modified, we need to touch to re-schedule sync */
528 if (ptr1 || ptr2)
529 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreau30e71012007-11-26 20:15:35 +0100530 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100531 }
532}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200533
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100534/* This function is called with (si->state == SI_ST_CON) meaning that a
535 * connection was attempted and that the file descriptor is already allocated.
536 * We must check for establishment, error and abort. Possible output states
537 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
538 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
Willy Tarreau87b09662015-04-03 00:22:06 +0200539 * otherwise 1. This only works with connection-based streams.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100540 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200541static int sess_update_st_con_tcp(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100542{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100543 struct stream_interface *si = &s->si[1];
544 struct channel *req = &s->req;
545 struct channel *rep = &s->res;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200546 struct connection *srv_conn = __objt_conn(si->end);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100547
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100548 /* If we got an error, or if nothing happened and the connection timed
549 * out, we must give up. The CER state handler will take care of retry
550 * attempts and error reports.
551 */
552 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau103197d2014-11-28 15:26:12 +0100553 if (unlikely(req->flags & CF_WRITE_PARTIAL)) {
Willy Tarreaue3224e82012-10-29 22:41:31 +0100554 /* Some data were sent past the connection establishment,
555 * so we need to pretend we're established to log correctly
556 * and let later states handle the failure.
557 */
Willy Tarreaue3224e82012-10-29 22:41:31 +0100558 si->state = SI_ST_EST;
559 si->err_type = SI_ET_DATA_ERR;
Willy Tarreau103197d2014-11-28 15:26:12 +0100560 rep->flags |= CF_READ_ERROR | CF_WRITE_ERROR;
Willy Tarreaue3224e82012-10-29 22:41:31 +0100561 return 1;
562 }
Willy Tarreau127334e2009-03-28 10:47:26 +0100563 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100564 si->state = SI_ST_CER;
Willy Tarreau0ede5a32012-12-08 08:44:02 +0100565
Willy Tarreauf79c8172013-10-21 16:30:56 +0200566 conn_force_close(srv_conn);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100567
568 if (si->err_type)
569 return 0;
570
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100571 if (si->flags & SI_FL_ERR)
572 si->err_type = SI_ET_CONN_ERR;
573 else
574 si->err_type = SI_ET_CONN_TO;
575 return 0;
576 }
577
578 /* OK, maybe we want to abort */
Willy Tarreaua7a7ebc2012-12-30 00:50:35 +0100579 if (!(req->flags & CF_WRITE_PARTIAL) &&
580 unlikely((rep->flags & CF_SHUTW) ||
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200581 ((req->flags & CF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
582 ((!(req->flags & CF_WRITE_ACTIVITY) && channel_is_empty(req)) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100583 s->be->options & PR_O_ABRT_CLOSE)))) {
584 /* give up */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200585 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100586 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau84455332009-03-15 22:34:05 +0100587 if (s->srv_error)
588 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100589 return 1;
590 }
591
592 /* we need to wait a bit more if there was no activity either */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200593 if (!(req->flags & CF_WRITE_ACTIVITY))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100594 return 1;
595
596 /* OK, this means that a connection succeeded. The caller will be
597 * responsible for handling the transition from CON to EST.
598 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100599 si->state = SI_ST_EST;
600 si->err_type = SI_ET_NONE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100601 return 1;
602}
603
604/* This function is called with (si->state == SI_ST_CER) meaning that a
605 * previous connection attempt has failed and that the file descriptor
606 * has already been released. Possible causes include asynchronous error
607 * notification and time out. Possible output states are SI_ST_CLO when
608 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
609 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
610 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
611 * marked as in error state. It returns 0.
612 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200613static int sess_update_st_cer(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100614{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100615 struct stream_interface *si = &s->si[1];
616
Willy Tarreau87b09662015-04-03 00:22:06 +0200617 /* we probably have to release last stream from the server */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100618 if (objt_server(s->target)) {
619 health_adjust(objt_server(s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100620
Willy Tarreaue7dff022015-04-03 01:14:29 +0200621 if (s->flags & SF_CURR_SESS) {
622 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100623 objt_server(s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100624 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100625 }
626
627 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200628 si->conn_retries--;
629 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100630 if (!si->err_type) {
631 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100632 }
633
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100634 if (objt_server(s->target))
635 objt_server(s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100636 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100637 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100638 if (may_dequeue_tasks(objt_server(s->target), s->be))
639 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100640
641 /* shutw is enough so stop a connecting socket */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200642 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100643 s->req.flags |= CF_WRITE_ERROR;
644 s->res.flags |= CF_READ_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100645
646 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100647 if (s->srv_error)
648 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100649 return 0;
650 }
651
652 /* If the "redispatch" option is set on the backend, we are allowed to
Joseph Lynch726ab712015-05-11 23:25:34 -0700653 * retry on another server. By default this redispatch occurs on the
654 * last retry, but if configured we allow redispatches to occur on
655 * configurable intervals, e.g. on every retry. In order to achieve this,
Willy Tarreau87b09662015-04-03 00:22:06 +0200656 * we must mark the stream unassigned, and eventually clear the DIRECT
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100657 * bit to ignore any persistence cookie. We won't count a retry nor a
658 * redispatch yet, because this will depend on what server is selected.
Willy Tarreau33a14e52014-06-13 17:49:40 +0200659 * If the connection is not persistent, the balancing algorithm is not
660 * determinist (round robin) and there is more than one active server,
661 * we accept to perform an immediate redispatch without waiting since
662 * we don't care about this particular server.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100663 */
Willy Tarreau33a14e52014-06-13 17:49:40 +0200664 if (objt_server(s->target) &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700665 (s->be->options & PR_O_REDISP) && !(s->flags & SF_FORCE_PRST) &&
666 ((((s->be->redispatch_after > 0) &&
667 ((s->be->conn_retries - si->conn_retries) %
668 s->be->redispatch_after == 0)) ||
669 ((s->be->redispatch_after < 0) &&
670 ((s->be->conn_retries - si->conn_retries) %
671 (s->be->conn_retries + 1 + s->be->redispatch_after) == 0))) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +0200672 (!(s->flags & SF_DIRECT) && s->be->srv_act > 1 &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700673 ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR)))) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100674 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100675 if (may_dequeue_tasks(objt_server(s->target), s->be))
676 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100677
Willy Tarreaue7dff022015-04-03 01:14:29 +0200678 s->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100679 si->state = SI_ST_REQ;
680 } else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100681 if (objt_server(s->target))
682 objt_server(s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100683 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100684 si->state = SI_ST_ASS;
685 }
686
687 if (si->flags & SI_FL_ERR) {
688 /* The error was an asynchronous connection error, and we will
689 * likely have to retry connecting to the same server, most
690 * likely leading to the same result. To avoid this, we wait
Willy Tarreaub0290662014-06-13 17:04:44 +0200691 * MIN(one second, connect timeout) before retrying.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100692 */
693
Willy Tarreaub0290662014-06-13 17:04:44 +0200694 int delay = 1000;
695
696 if (s->be->timeout.connect && s->be->timeout.connect < delay)
697 delay = s->be->timeout.connect;
698
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100699 if (!si->err_type)
700 si->err_type = SI_ET_CONN_ERR;
701
Willy Tarreaudb6d0122014-06-13 17:40:15 +0200702 /* only wait when we're retrying on the same server */
703 if (si->state == SI_ST_ASS ||
704 (s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_RR ||
705 (s->be->srv_act <= 1)) {
706 si->state = SI_ST_TAR;
707 si->exp = tick_add(now_ms, MS_TO_TICKS(delay));
708 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100709 return 0;
710 }
711 return 0;
712}
713
714/*
715 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200716 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
Willy Tarreau26d8c592012-05-07 18:12:14 +0200717 * SI_ST_INI) to SI_ST_EST, but only when a ->proto is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100718 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200719static void sess_establish(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100720{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100721 struct stream_interface *si = &s->si[1];
722 struct channel *req = &s->req;
723 struct channel *rep = &s->res;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100724
Willy Tarreau0e37f1c2013-12-31 23:06:46 +0100725 /* First, centralize the timers information */
726 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
727 si->exp = TICK_ETERNITY;
728
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100729 if (objt_server(s->target))
730 health_adjust(objt_server(s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100731
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100732 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100733 /* if the user wants to log as soon as possible, without counting
734 * bytes from the server, then this is the right moment. */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200735 if (!LIST_ISEMPTY(&strm_fe(s)->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100736 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100737 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100738 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100739 }
740 else {
Willy Tarreaud81ca042013-12-31 22:33:13 +0100741 rep->flags |= CF_READ_DONTWAIT; /* a single read is enough to get response headers */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100742 }
743
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200744 rep->analysers |= strm_fe(s)->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200745 rep->flags |= CF_READ_ATTACHED; /* producer is now attached */
Willy Tarreau644c1012014-04-30 18:11:11 +0200746 if (req->flags & CF_WAKE_CONNECT) {
747 req->flags |= CF_WAKE_ONCE;
748 req->flags &= ~CF_WAKE_CONNECT;
749 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200750 if (objt_conn(si->end)) {
Willy Tarreaud04e8582010-05-31 12:31:35 +0200751 /* real connections have timeouts */
752 req->wto = s->be->timeout.server;
753 rep->rto = s->be->timeout.server;
754 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100755 req->wex = TICK_ETERNITY;
756}
757
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100758/* Update back stream interface status for input states SI_ST_ASS, SI_ST_QUE,
759 * SI_ST_TAR. Other input states are simply ignored.
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100760 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON
761 * and SI_ST_EST. Flags must have previously been updated for timeouts and other
762 * conditions.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100763 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200764static void sess_update_stream_int(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100765{
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100766 struct server *srv = objt_server(s->target);
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100767 struct stream_interface *si = &s->si[1];
Willy Tarreau103197d2014-11-28 15:26:12 +0100768 struct channel *req = &s->req;
Willy Tarreau827aee92011-03-10 16:55:02 +0100769
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100770 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 +0100771 now_ms, __FUNCTION__,
772 s,
CJ Ess6eac32e2015-05-02 16:35:24 -0400773 req, &s->res,
Willy Tarreau103197d2014-11-28 15:26:12 +0100774 req->rex, s->res.wex,
775 req->flags, s->res.flags,
CJ Ess6eac32e2015-05-02 16:35:24 -0400776 req->buf->i, req->buf->o, s->res.buf->i, s->res.buf->o, s->si[0].state, s->si[1].state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100777
778 if (si->state == SI_ST_ASS) {
779 /* Server assigned to connection request, we have to try to connect now */
780 int conn_err;
781
782 conn_err = connect_server(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100783 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100784
Willy Tarreaue7dff022015-04-03 01:14:29 +0200785 if (conn_err == SF_ERR_NONE) {
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100786 /* state = SI_ST_CON or SI_ST_EST now */
Willy Tarreau827aee92011-03-10 16:55:02 +0100787 if (srv)
788 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500789 if (srv)
790 srv_set_sess_last(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100791 return;
792 }
793
794 /* We have received a synchronous error. We might have to
795 * abort, retry immediately or redispatch.
796 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200797 if (conn_err == SF_ERR_INTERNAL) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100798 if (!si->err_type) {
799 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100800 }
801
Willy Tarreau827aee92011-03-10 16:55:02 +0100802 if (srv)
803 srv_inc_sess_ctr(srv);
804 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500805 srv_set_sess_last(srv);
806 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +0100807 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100808 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100809
Willy Tarreau87b09662015-04-03 00:22:06 +0200810 /* release other streams waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100811 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100812 if (may_dequeue_tasks(srv, s->be))
813 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100814
815 /* Failed and not retryable. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200816 si_shutr(si);
817 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100818 req->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100819
820 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
821
Willy Tarreau87b09662015-04-03 00:22:06 +0200822 /* no stream was ever accounted for this server */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100823 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100824 if (s->srv_error)
825 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100826 return;
827 }
828
829 /* We are facing a retryable error, but we don't want to run a
830 * turn-around now, as the problem is likely a source port
831 * allocation problem, so we want to retry now.
832 */
833 si->state = SI_ST_CER;
834 si->flags &= ~SI_FL_ERR;
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100835 sess_update_st_cer(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100836 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
837 return;
838 }
839 else if (si->state == SI_ST_QUE) {
840 /* connection request was queued, check for any update */
841 if (!s->pend_pos) {
842 /* The connection is not in the queue anymore. Either
843 * we have a server connection slot available and we
844 * go directly to the assigned state, or we need to
845 * load-balance first and go to the INI state.
846 */
847 si->exp = TICK_ETERNITY;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200848 if (unlikely(!(s->flags & SF_ASSIGNED)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100849 si->state = SI_ST_REQ;
850 else {
851 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
852 si->state = SI_ST_ASS;
853 }
854 return;
855 }
856
857 /* Connection request still in queue... */
858 if (si->flags & SI_FL_EXP) {
859 /* ... and timeout expired */
860 si->exp = TICK_ETERNITY;
861 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +0100862 if (srv)
863 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100864 s->be->be_counters.failed_conns++;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200865 si_shutr(si);
866 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100867 req->flags |= CF_WRITE_TIMEOUT;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100868 if (!si->err_type)
869 si->err_type = SI_ET_QUEUE_TO;
870 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100871 if (s->srv_error)
872 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100873 return;
874 }
875
876 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau103197d2014-11-28 15:26:12 +0100877 if ((req->flags & (CF_READ_ERROR)) ||
878 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
879 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100880 /* give up */
881 si->exp = TICK_ETERNITY;
882 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200883 si_shutr(si);
884 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100885 si->err_type |= SI_ET_QUEUE_ABRT;
886 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100887 if (s->srv_error)
888 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100889 return;
890 }
891
892 /* Nothing changed */
893 return;
894 }
895 else if (si->state == SI_ST_TAR) {
896 /* Connection request might be aborted */
Willy Tarreau103197d2014-11-28 15:26:12 +0100897 if ((req->flags & (CF_READ_ERROR)) ||
898 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
899 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100900 /* give up */
901 si->exp = TICK_ETERNITY;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200902 si_shutr(si);
903 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100904 si->err_type |= SI_ET_CONN_ABRT;
905 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100906 if (s->srv_error)
907 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100908 return;
909 }
910
911 if (!(si->flags & SI_FL_EXP))
912 return; /* still in turn-around */
913
914 si->exp = TICK_ETERNITY;
915
Willy Tarreau87b09662015-04-03 00:22:06 +0200916 /* we keep trying on the same server as long as the stream is
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100917 * marked "assigned".
918 * FIXME: Should we force a redispatch attempt when the server is down ?
919 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200920 if (s->flags & SF_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100921 si->state = SI_ST_ASS;
922 else
923 si->state = SI_ST_REQ;
924 return;
925 }
926}
927
Willy Tarreau87b09662015-04-03 00:22:06 +0200928/* Set correct stream termination flags in case no analyser has done it. It
Simon Hormandec5be42011-06-08 09:19:07 +0900929 * also counts a failed request if the server state has not reached the request
930 * stage.
931 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200932static void sess_set_term_flags(struct stream *s)
Simon Hormandec5be42011-06-08 09:19:07 +0900933{
Willy Tarreaue7dff022015-04-03 01:14:29 +0200934 if (!(s->flags & SF_FINST_MASK)) {
Simon Hormandec5be42011-06-08 09:19:07 +0900935 if (s->si[1].state < SI_ST_REQ) {
936
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200937 strm_fe(s)->fe_counters.failed_req++;
938 if (strm_li(s)->counters)
939 strm_li(s)->counters->failed_req++;
Simon Hormandec5be42011-06-08 09:19:07 +0900940
Willy Tarreaue7dff022015-04-03 01:14:29 +0200941 s->flags |= SF_FINST_R;
Simon Hormandec5be42011-06-08 09:19:07 +0900942 }
943 else if (s->si[1].state == SI_ST_QUE)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200944 s->flags |= SF_FINST_Q;
Simon Hormandec5be42011-06-08 09:19:07 +0900945 else if (s->si[1].state < SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200946 s->flags |= SF_FINST_C;
Simon Hormandec5be42011-06-08 09:19:07 +0900947 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200948 s->flags |= SF_FINST_D;
Simon Hormandec5be42011-06-08 09:19:07 +0900949 else
Willy Tarreaue7dff022015-04-03 01:14:29 +0200950 s->flags |= SF_FINST_L;
Simon Hormandec5be42011-06-08 09:19:07 +0900951 }
952}
953
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100954/* This function initiates a server connection request on a stream interface
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100955 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS for
956 * a real connection to a server, indicating that a server has been assigned,
957 * or SI_ST_EST for a successful connection to an applet. It may also return
958 * SI_ST_QUE, or SI_ST_CLO upon error.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100959 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200960static void sess_prepare_conn_req(struct stream *s)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100961{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100962 struct stream_interface *si = &s->si[1];
963
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100964 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 +0100965 now_ms, __FUNCTION__,
966 s,
CJ Ess6eac32e2015-05-02 16:35:24 -0400967 &s->req, &s->res,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100968 s->req.rex, s->res.wex,
969 s->req.flags, s->res.flags,
CJ Ess6eac32e2015-05-02 16:35:24 -0400970 s->req.buf->i, s->req.buf->o, s->res.buf->i, s->res.buf->o, s->si[0].state, s->si[1].state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100971
972 if (si->state != SI_ST_REQ)
973 return;
974
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100975 if (unlikely(obj_type(s->target) == OBJ_TYPE_APPLET)) {
976 /* the applet directly goes to the EST state */
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100977 struct appctx *appctx = objt_appctx(si->end);
978
979 if (!appctx || appctx->applet != __objt_applet(s->target))
980 appctx = stream_int_register_handler(si, objt_applet(s->target));
981
982 if (!appctx) {
983 /* No more memory, let's immediately abort. Force the
984 * error code to ignore the ERR_LOCAL which is not a
985 * real error.
986 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200987 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100988
989 si_shutr(si);
990 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100991 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau6bbb2f62013-12-09 17:14:23 +0100992 si->err_type = SI_ET_CONN_RES;
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100993 si->state = SI_ST_CLO;
994 if (s->srv_error)
995 s->srv_error(s, si);
996 return;
997 }
998
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100999 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001000 si->state = SI_ST_EST;
1001 si->err_type = SI_ET_NONE;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001002 be_set_sess_last(s->be);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01001003 /* let sess_establish() finish the job */
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001004 return;
1005 }
1006
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001007 /* Try to assign a server */
1008 if (srv_redispatch_connect(s) != 0) {
1009 /* We did not get a server. Either we queued the
1010 * connection request, or we encountered an error.
1011 */
1012 if (si->state == SI_ST_QUE)
1013 return;
1014
1015 /* we did not get any server, let's check the cause */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001016 si_shutr(si);
1017 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001018 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001019 if (!si->err_type)
1020 si->err_type = SI_ET_CONN_OTHER;
1021 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001022 if (s->srv_error)
1023 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001024 return;
1025 }
1026
1027 /* The server is assigned */
1028 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1029 si->state = SI_ST_ASS;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001030 be_set_sess_last(s->be);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001031}
1032
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001033/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +01001034 * if appropriate. The default_backend rule is also considered, then the
1035 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001036 * It returns 1 if the processing can continue on next analysers, or zero if it
1037 * either needs more data or wants to immediately abort the request.
1038 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001039static int process_switching_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001040{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001041 struct persist_rule *prst_rule;
Willy Tarreau192252e2015-04-04 01:47:55 +02001042 struct session *sess = s->sess;
1043 struct proxy *fe = sess->fe;
Willy Tarreau4de91492010-01-22 19:10:05 +01001044
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001045 req->analysers &= ~an_bit;
1046 req->analyse_exp = TICK_ETERNITY;
1047
Willy Tarreau87b09662015-04-03 00:22:06 +02001048 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 +02001049 now_ms, __FUNCTION__,
1050 s,
1051 req,
1052 req->rex, req->wex,
1053 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001054 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001055 req->analysers);
1056
1057 /* now check whether we have some switching rules for this request */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001058 if (!(s->flags & SF_BE_ASSIGNED)) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001059 struct switching_rule *rule;
1060
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001061 list_for_each_entry(rule, &fe->switching_rules, list) {
Willy Tarreauf51658d2014-04-23 01:21:56 +02001062 int ret = 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001063
Willy Tarreauf51658d2014-04-23 01:21:56 +02001064 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001065 ret = acl_exec_cond(rule->cond, fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf51658d2014-04-23 01:21:56 +02001066 ret = acl_pass(ret);
1067 if (rule->cond->pol == ACL_COND_UNLESS)
1068 ret = !ret;
1069 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001070
1071 if (ret) {
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001072 /* If the backend name is dynamic, try to resolve the name.
1073 * If we can't resolve the name, or if any error occurs, break
1074 * the loop and fallback to the default backend.
1075 */
1076 struct proxy *backend;
1077
1078 if (rule->dynamic) {
1079 struct chunk *tmp = get_trash_chunk();
1080 if (!build_logline(s, tmp->str, tmp->size, &rule->be.expr))
1081 break;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001082 backend = proxy_be_by_name(tmp->str);
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001083 if (!backend)
1084 break;
1085 }
1086 else
1087 backend = rule->be.backend;
1088
Willy Tarreau87b09662015-04-03 00:22:06 +02001089 if (!stream_set_backend(s, backend))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001090 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001091 break;
1092 }
1093 }
1094
1095 /* To ensure correct connection accounting on the backend, we
1096 * have to assign one if it was not set (eg: a listen). This
1097 * measure also takes care of correctly setting the default
1098 * backend if any.
1099 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001100 if (!(s->flags & SF_BE_ASSIGNED))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001101 if (!stream_set_backend(s, fe->defbe.be ? fe->defbe.be : s->be))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001102 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001103 }
1104
Willy Tarreaufb356202010-08-03 14:02:05 +02001105 /* 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 +02001106 if (fe == s->be) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001107 s->req.analysers &= ~AN_REQ_INSPECT_BE;
1108 s->req.analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001109 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001110
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001111 /* 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 +02001112 * persistence rule, and report that in the stream.
Willy Tarreau4de91492010-01-22 19:10:05 +01001113 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001114 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001115 int ret = 1;
1116
1117 if (prst_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001118 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 +01001119 ret = acl_pass(ret);
1120 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1121 ret = !ret;
1122 }
1123
1124 if (ret) {
1125 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001126 if (prst_rule->type == PERSIST_TYPE_FORCE) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001127 s->flags |= SF_FORCE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001128 } else {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001129 s->flags |= SF_IGNORE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001130 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001131 break;
1132 }
1133 }
1134
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001135 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001136
1137 sw_failed:
1138 /* immediately abort this request in case of allocation failure */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001139 channel_abort(&s->req);
1140 channel_abort(&s->res);
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001141
Willy Tarreaue7dff022015-04-03 01:14:29 +02001142 if (!(s->flags & SF_ERR_MASK))
1143 s->flags |= SF_ERR_RESOURCE;
1144 if (!(s->flags & SF_FINST_MASK))
1145 s->flags |= SF_FINST_R;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001146
Willy Tarreaueee5b512015-04-03 23:46:31 +02001147 if (s->txn)
1148 s->txn->status = 500;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001149 s->req.analysers = 0;
1150 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001151 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001152}
1153
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001154/* This stream analyser works on a request. It applies all use-server rules on
1155 * it then returns 1. The data must already be present in the buffer otherwise
1156 * they won't match. It always returns 1.
1157 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001158static int process_server_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001159{
1160 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001161 struct session *sess = s->sess;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001162 struct server_rule *rule;
1163
Willy Tarreau87b09662015-04-03 00:22:06 +02001164 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 +02001165 now_ms, __FUNCTION__,
1166 s,
1167 req,
1168 req->rex, req->wex,
1169 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001170 req->buf->i + req->buf->o,
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001171 req->analysers);
1172
Willy Tarreaue7dff022015-04-03 01:14:29 +02001173 if (!(s->flags & SF_ASSIGNED)) {
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001174 list_for_each_entry(rule, &px->server_rules, list) {
1175 int ret;
1176
Willy Tarreau192252e2015-04-04 01:47:55 +02001177 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001178 ret = acl_pass(ret);
1179 if (rule->cond->pol == ACL_COND_UNLESS)
1180 ret = !ret;
1181
1182 if (ret) {
1183 struct server *srv = rule->srv.ptr;
1184
Willy Tarreau892337c2014-05-13 23:41:20 +02001185 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001186 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001187 (s->flags & SF_FORCE_PRST)) {
1188 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001189 s->target = &srv->obj_type;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001190 break;
1191 }
1192 /* if the server is not UP, let's go on with next rules
1193 * just in case another one is suited.
1194 */
1195 }
1196 }
1197 }
1198
1199 req->analysers &= ~an_bit;
1200 req->analyse_exp = TICK_ETERNITY;
1201 return 1;
1202}
1203
Emeric Brun1d33b292010-01-04 15:47:17 +01001204/* This stream analyser works on a request. It applies all sticking rules on
1205 * it then returns 1. The data must already be present in the buffer otherwise
1206 * they won't match. It always returns 1.
1207 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001208static int process_sticking_rules(struct stream *s, struct channel *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001209{
1210 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001211 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001212 struct sticking_rule *rule;
1213
Willy Tarreau87b09662015-04-03 00:22:06 +02001214 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 +01001215 now_ms, __FUNCTION__,
1216 s,
1217 req,
1218 req->rex, req->wex,
1219 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001220 req->buf->i,
Emeric Brun1d33b292010-01-04 15:47:17 +01001221 req->analysers);
1222
1223 list_for_each_entry(rule, &px->sticking_rules, list) {
1224 int ret = 1 ;
1225 int i;
1226
Willy Tarreau9667a802013-12-09 12:52:13 +01001227 /* Only the first stick store-request of each table is applied
1228 * and other ones are ignored. The purpose is to allow complex
1229 * configurations which look for multiple entries by decreasing
1230 * order of precision and to stop at the first which matches.
1231 * An example could be a store of the IP address from an HTTP
1232 * header first, then from the source if not found.
1233 */
Emeric Brun1d33b292010-01-04 15:47:17 +01001234 for (i = 0; i < s->store_count; i++) {
1235 if (rule->table.t == s->store[i].table)
1236 break;
1237 }
1238
1239 if (i != s->store_count)
1240 continue;
1241
1242 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001243 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001244 ret = acl_pass(ret);
1245 if (rule->cond->pol == ACL_COND_UNLESS)
1246 ret = !ret;
1247 }
1248
1249 if (ret) {
1250 struct stktable_key *key;
1251
Willy Tarreau192252e2015-04-04 01:47:55 +02001252 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 +01001253 if (!key)
1254 continue;
1255
1256 if (rule->flags & STK_IS_MATCH) {
1257 struct stksess *ts;
1258
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001259 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001260 if (!(s->flags & SF_ASSIGNED)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001261 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001262 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001263
1264 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001265 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1266 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001267 if (node) {
1268 struct server *srv;
1269
1270 srv = container_of(node, struct server, conf.id);
Willy Tarreau892337c2014-05-13 23:41:20 +02001271 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4de91492010-01-22 19:10:05 +01001272 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001273 (s->flags & SF_FORCE_PRST)) {
1274 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001275 s->target = &srv->obj_type;
Emeric Brun1d33b292010-01-04 15:47:17 +01001276 }
1277 }
1278 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001279 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001280 }
1281 }
1282 if (rule->flags & STK_IS_STORE) {
1283 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1284 struct stksess *ts;
1285
1286 ts = stksess_new(rule->table.t, key);
1287 if (ts) {
1288 s->store[s->store_count].table = rule->table.t;
1289 s->store[s->store_count++].ts = ts;
1290 }
1291 }
1292 }
1293 }
1294 }
1295
1296 req->analysers &= ~an_bit;
1297 req->analyse_exp = TICK_ETERNITY;
1298 return 1;
1299}
1300
1301/* This stream analyser works on a response. It applies all store rules on it
1302 * then returns 1. The data must already be present in the buffer otherwise
1303 * they won't match. It always returns 1.
1304 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001305static int process_store_rules(struct stream *s, struct channel *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001306{
1307 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001308 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001309 struct sticking_rule *rule;
1310 int i;
Willy Tarreau9667a802013-12-09 12:52:13 +01001311 int nbreq = s->store_count;
Emeric Brun1d33b292010-01-04 15:47:17 +01001312
Willy Tarreau87b09662015-04-03 00:22:06 +02001313 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 +01001314 now_ms, __FUNCTION__,
1315 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001316 rep,
1317 rep->rex, rep->wex,
1318 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001319 rep->buf->i,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001320 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001321
1322 list_for_each_entry(rule, &px->storersp_rules, list) {
1323 int ret = 1 ;
Emeric Brun1d33b292010-01-04 15:47:17 +01001324
Willy Tarreau9667a802013-12-09 12:52:13 +01001325 /* Only the first stick store-response of each table is applied
1326 * and other ones are ignored. The purpose is to allow complex
1327 * configurations which look for multiple entries by decreasing
1328 * order of precision and to stop at the first which matches.
1329 * An example could be a store of a set-cookie value, with a
1330 * fallback to a parameter found in a 302 redirect.
1331 *
1332 * The store-response rules are not allowed to override the
1333 * store-request rules for the same table, but they may coexist.
1334 * Thus we can have up to one store-request entry and one store-
1335 * response entry for the same table at any time.
1336 */
1337 for (i = nbreq; i < s->store_count; i++) {
1338 if (rule->table.t == s->store[i].table)
1339 break;
1340 }
1341
1342 /* skip existing entries for this table */
1343 if (i < s->store_count)
1344 continue;
1345
Emeric Brun1d33b292010-01-04 15:47:17 +01001346 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001347 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001348 ret = acl_pass(ret);
1349 if (rule->cond->pol == ACL_COND_UNLESS)
1350 ret = !ret;
1351 }
1352
1353 if (ret) {
1354 struct stktable_key *key;
1355
Willy Tarreau192252e2015-04-04 01:47:55 +02001356 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 +01001357 if (!key)
1358 continue;
1359
Willy Tarreau37e340c2013-12-06 23:05:21 +01001360 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001361 struct stksess *ts;
1362
1363 ts = stksess_new(rule->table.t, key);
1364 if (ts) {
1365 s->store[s->store_count].table = rule->table.t;
Emeric Brun1d33b292010-01-04 15:47:17 +01001366 s->store[s->store_count++].ts = ts;
1367 }
1368 }
1369 }
1370 }
1371
1372 /* process store request and store response */
1373 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001374 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001375 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001376
Willy Tarreauc93cd162014-05-13 15:54:22 +02001377 if (objt_server(s->target) && objt_server(s->target)->flags & SRV_F_NON_STICK) {
Simon Hormanfa461682011-06-25 09:39:49 +09001378 stksess_free(s->store[i].table, s->store[i].ts);
1379 s->store[i].ts = NULL;
1380 continue;
1381 }
1382
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001383 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1384 if (ts) {
1385 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001386 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001387 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001388 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001389 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001390 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001391
1392 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001393 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001394 stktable_data_cast(ptr, server_id) = objt_server(s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001395 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001396 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001397
1398 rep->analysers &= ~an_bit;
1399 rep->analyse_exp = TICK_ETERNITY;
1400 return 1;
1401}
1402
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001403/* This macro is very specific to the function below. See the comments in
Willy Tarreau87b09662015-04-03 00:22:06 +02001404 * process_stream() below to understand the logic and the tests.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001405 */
1406#define UPDATE_ANALYSERS(real, list, back, flag) { \
1407 list = (((list) & ~(flag)) | ~(back)) & (real); \
1408 back = real; \
1409 if (!(list)) \
1410 break; \
1411 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1412 continue; \
1413}
1414
Willy Tarreau87b09662015-04-03 00:22:06 +02001415/* Processes the client, server, request and response jobs of a stream task,
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001416 * then puts it back to the wait queue in a clean state, or cleans up its
1417 * resources if it must be deleted. Returns in <next> the date the task wants
1418 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1419 * nothing too many times, the request and response buffers flags are monitored
1420 * and each function is called only if at least another function has changed at
1421 * least one flag it is interested in.
1422 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001423struct task *process_stream(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001424{
Willy Tarreau827aee92011-03-10 16:55:02 +01001425 struct server *srv;
Willy Tarreau87b09662015-04-03 00:22:06 +02001426 struct stream *s = t->context;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001427 struct session *sess = s->sess;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001428 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001429 unsigned int rq_prod_last, rq_cons_last;
1430 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001431 unsigned int req_ana_back;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001432 struct channel *req, *res;
1433 struct stream_interface *si_f, *si_b;
1434
1435 req = &s->req;
1436 res = &s->res;
1437
1438 si_f = &s->si[0];
1439 si_b = &s->si[1];
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001440
1441 //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 +01001442 // si_f->state, si_b->state, si_b->err_type, req->flags, res->flags);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001443
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001444 /* this data may be no longer valid, clear it */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001445 if (s->txn)
1446 memset(&s->txn->auth, 0, sizeof(s->txn->auth));
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001447
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02001448 /* This flag must explicitly be set every time */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001449 req->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
1450 res->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001451
1452 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001453 rqf_last = req->flags & ~CF_MASK_ANALYSER;
1454 rpf_last = res->flags & ~CF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001455
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001456 /* we don't want the stream interface functions to recursively wake us up */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001457 si_f->flags |= SI_FL_DONT_WAKE;
1458 si_b->flags |= SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001459
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001460 /* 1a: Check for low level timeouts if needed. We just set a flag on
1461 * stream interfaces when their timeouts have expired.
1462 */
1463 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001464 stream_int_check_timeouts(si_f);
1465 stream_int_check_timeouts(si_b);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001466
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001467 /* check channel timeouts, and close the corresponding stream interfaces
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001468 * for future reads or writes. Note: this will also concern upper layers
1469 * but we do not touch any other flag. We must be careful and correctly
1470 * detect state changes when calling them.
1471 */
1472
Willy Tarreau8f128b42014-11-28 15:07:47 +01001473 channel_check_timeouts(req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001474
Willy Tarreau8f128b42014-11-28 15:07:47 +01001475 if (unlikely((req->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1476 si_b->flags |= SI_FL_NOLINGER;
1477 si_shutw(si_b);
Willy Tarreau14641402009-12-29 14:49:56 +01001478 }
1479
Willy Tarreau8f128b42014-11-28 15:07:47 +01001480 if (unlikely((req->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1481 if (si_f->flags & SI_FL_NOHALF)
1482 si_f->flags |= SI_FL_NOLINGER;
1483 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001484 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001485
Willy Tarreau8f128b42014-11-28 15:07:47 +01001486 channel_check_timeouts(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001487
Willy Tarreau8f128b42014-11-28 15:07:47 +01001488 if (unlikely((res->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1489 si_f->flags |= SI_FL_NOLINGER;
1490 si_shutw(si_f);
Willy Tarreau14641402009-12-29 14:49:56 +01001491 }
1492
Willy Tarreau8f128b42014-11-28 15:07:47 +01001493 if (unlikely((res->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1494 if (si_b->flags & SI_FL_NOHALF)
1495 si_b->flags |= SI_FL_NOLINGER;
1496 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001497 }
Willy Tarreau798f4322012-11-08 14:49:17 +01001498
1499 /* Once in a while we're woken up because the task expires. But
1500 * this does not necessarily mean that a timeout has been reached.
Willy Tarreau87b09662015-04-03 00:22:06 +02001501 * So let's not run a whole stream processing if only an expiration
Willy Tarreau798f4322012-11-08 14:49:17 +01001502 * timeout needs to be refreshed.
1503 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001504 if (!((req->flags | res->flags) &
Willy Tarreau798f4322012-11-08 14:49:17 +01001505 (CF_SHUTR|CF_READ_ACTIVITY|CF_READ_TIMEOUT|CF_SHUTW|
1506 CF_WRITE_ACTIVITY|CF_WRITE_TIMEOUT|CF_ANA_TIMEOUT)) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01001507 !((si_f->flags | si_b->flags) & (SI_FL_EXP|SI_FL_ERR)) &&
Willy Tarreau798f4322012-11-08 14:49:17 +01001508 ((t->state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER))
1509 goto update_exp_and_leave;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001510 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001511
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001512 /* below we may emit error messages so we have to ensure that we have
1513 * our buffers properly allocated.
1514 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001515 if (!stream_alloc_work_buffer(s)) {
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001516 /* No buffer available, we've been subscribed to the list of
1517 * buffer waiters, let's wait for our turn.
1518 */
1519 goto update_exp_and_leave;
1520 }
1521
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001522 /* 1b: check for low-level errors reported at the stream interface.
1523 * First we check if it's a retryable error (in which case we don't
1524 * want to tell the buffer). Otherwise we report the error one level
1525 * upper by setting flags into the buffers. Note that the side towards
1526 * the client cannot have connect (hence retryable) errors. Also, the
1527 * connection setup code must be able to deal with any type of abort.
1528 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001529 srv = objt_server(s->target);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001530 if (unlikely(si_f->flags & SI_FL_ERR)) {
1531 if (si_f->state == SI_ST_EST || si_f->state == SI_ST_DIS) {
1532 si_shutr(si_f);
1533 si_shutw(si_f);
1534 stream_int_report_error(si_f);
1535 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001536 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001537 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001538 if (srv)
1539 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001540 if (!(s->flags & SF_ERR_MASK))
1541 s->flags |= SF_ERR_CLICL;
1542 if (!(s->flags & SF_FINST_MASK))
1543 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001544 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001545 }
1546 }
1547
Willy Tarreau8f128b42014-11-28 15:07:47 +01001548 if (unlikely(si_b->flags & SI_FL_ERR)) {
1549 if (si_b->state == SI_ST_EST || si_b->state == SI_ST_DIS) {
1550 si_shutr(si_b);
1551 si_shutw(si_b);
1552 stream_int_report_error(si_b);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001553 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001554 if (srv)
1555 srv->counters.failed_resp++;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001556 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001557 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001558 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001559 if (srv)
1560 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001561 if (!(s->flags & SF_ERR_MASK))
1562 s->flags |= SF_ERR_SRVCL;
1563 if (!(s->flags & SF_FINST_MASK))
1564 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001565 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001566 }
1567 /* note: maybe we should process connection errors here ? */
1568 }
1569
Willy Tarreau8f128b42014-11-28 15:07:47 +01001570 if (si_b->state == SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001571 /* we were trying to establish a connection on the server side,
1572 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1573 */
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001574 if (unlikely(!sess_update_st_con_tcp(s)))
1575 sess_update_st_cer(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001576 else if (si_b->state == SI_ST_EST)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001577 sess_establish(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001578
1579 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1580 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1581 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1582 */
1583 }
1584
Willy Tarreau8f128b42014-11-28 15:07:47 +01001585 rq_prod_last = si_f->state;
1586 rq_cons_last = si_b->state;
1587 rp_cons_last = si_f->state;
1588 rp_prod_last = si_b->state;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001589
1590 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001591 /* Check for connection closure */
1592
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001593 DPRINTF(stderr,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001594 "[%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 +01001595 now_ms, __FUNCTION__, __LINE__,
1596 t,
1597 s, s->flags,
Willy Tarreau8f128b42014-11-28 15:07:47 +01001598 req, res,
1599 req->rex, res->wex,
1600 req->flags, res->flags,
1601 req->buf->i, req->buf->o, res->buf->i, res->buf->o, si_f->state, si_b->state,
1602 si_f->err_type, si_b->err_type,
1603 si_b->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001604
1605 /* nothing special to be done on client side */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001606 if (unlikely(si_f->state == SI_ST_DIS))
1607 si_f->state = SI_ST_CLO;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001608
1609 /* When a server-side connection is released, we have to count it and
1610 * check for pending connections on this server.
1611 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001612 if (unlikely(si_b->state == SI_ST_DIS)) {
1613 si_b->state = SI_ST_CLO;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001614 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001615 if (srv) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001616 if (s->flags & SF_CURR_SESS) {
1617 s->flags &= ~SF_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001618 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001619 }
1620 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001621 if (may_dequeue_tasks(srv, s->be))
1622 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001623 }
1624 }
1625
1626 /*
1627 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1628 * at this point.
1629 */
1630
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001631 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001632 /* Analyse request */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001633 if (((req->flags & ~rqf_last) & CF_MASK_ANALYSER) ||
1634 ((req->flags ^ rqf_last) & CF_MASK_STATIC) ||
1635 si_f->state != rq_prod_last ||
1636 si_b->state != rq_cons_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001637 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001638 unsigned int flags = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001639
Willy Tarreau8f128b42014-11-28 15:07:47 +01001640 if (si_f->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001641 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001642 unsigned int ana_list;
1643 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001644
Willy Tarreau90deb182010-01-07 00:20:41 +01001645 /* it's up to the analysers to stop new connections,
1646 * disable reading or closing. Note: if an analyser
1647 * disables any of these bits, it is responsible for
1648 * enabling them again when it disables itself, so
1649 * that other analysers are called in similar conditions.
1650 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001651 channel_auto_read(req);
1652 channel_auto_connect(req);
1653 channel_auto_close(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001654
1655 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001656 * req->analysers, following the bit order from LSB
Willy Tarreauedcf6682008-11-30 23:15:34 +01001657 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001658 * the list when not needed. Any analyser may return 0
1659 * to break out of the loop, either because of missing
1660 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001661 * kill the stream. We loop at least once through each
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001662 * analyser, and we may loop again if other analysers
1663 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001664 *
1665 * We build a list of analysers to run. We evaluate all
1666 * of these analysers in the order of the lower bit to
1667 * the higher bit. This ordering is very important.
1668 * An analyser will often add/remove other analysers,
1669 * including itself. Any changes to itself have no effect
1670 * on the loop. If it removes any other analysers, we
1671 * want those analysers not to be called anymore during
1672 * this loop. If it adds an analyser that is located
1673 * after itself, we want it to be scheduled for being
1674 * processed during the loop. If it adds an analyser
1675 * which is located before it, we want it to switch to
1676 * it immediately, even if it has already been called
1677 * once but removed since.
1678 *
1679 * In order to achieve this, we compare the analyser
1680 * list after the call with a copy of it before the
1681 * call. The work list is fed with analyser bits that
1682 * appeared during the call. Then we compare previous
1683 * work list with the new one, and check the bits that
1684 * appeared. If the lowest of these bits is lower than
1685 * the current bit, it means we have enabled a previous
1686 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001687 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001688
Willy Tarreau8f128b42014-11-28 15:07:47 +01001689 ana_list = ana_back = req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001690 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001691 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001692
Willy Tarreaufb356202010-08-03 14:02:05 +02001693 if (ana_list & AN_REQ_INSPECT_FE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001694 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001695 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001696 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001697 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001698
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001699 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001700 if (!http_wait_for_request(s, req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001701 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001702 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001703 }
1704
Willy Tarreau748179e2015-05-01 21:52:31 +02001705 if (ana_list & AN_REQ_HTTP_BODY) {
1706 if (!http_wait_for_request_body(s, req, AN_REQ_HTTP_BODY))
1707 break;
1708 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
1709 }
1710
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001711 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001712 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_FE, sess->fe))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001713 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001714 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001715 }
1716
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001717 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001718 if (!process_switching_rules(s, req, AN_REQ_SWITCHING_RULES))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001719 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001720 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001721 }
1722
Willy Tarreaufb356202010-08-03 14:02:05 +02001723 if (ana_list & AN_REQ_INSPECT_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001724 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_BE))
Willy Tarreaufb356202010-08-03 14:02:05 +02001725 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001726 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
Willy Tarreaufb356202010-08-03 14:02:05 +02001727 }
1728
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001729 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001730 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_BE, s->be))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001731 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001732 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001733 }
1734
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001735 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001736 if (!http_process_tarpit(s, req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001737 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001738 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001739 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001740
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001741 if (ana_list & AN_REQ_SRV_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001742 if (!process_server_rules(s, req, AN_REQ_SRV_RULES))
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001743 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001744 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SRV_RULES);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001745 }
1746
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001747 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001748 if (!http_process_request(s, req, AN_REQ_HTTP_INNER))
Willy Tarreauc465fd72009-08-31 00:17:18 +02001749 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001750 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001751 }
1752
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001753 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001754 if (!tcp_persist_rdp_cookie(s, req, AN_REQ_PRST_RDP_COOKIE))
Emeric Brun647caf12009-06-30 17:57:00 +02001755 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001756 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001757 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001758
Emeric Brun1d33b292010-01-04 15:47:17 +01001759 if (ana_list & AN_REQ_STICKING_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001760 if (!process_sticking_rules(s, req, AN_REQ_STICKING_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01001761 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001762 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01001763 }
1764
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001765 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001766 if (!http_request_forward_body(s, req, AN_REQ_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01001767 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001768 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001769 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001770 break;
1771 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001772 }
Willy Tarreau84455332009-03-15 22:34:05 +01001773
Willy Tarreau8f128b42014-11-28 15:07:47 +01001774 rq_prod_last = si_f->state;
1775 rq_cons_last = si_b->state;
1776 req->flags &= ~CF_WAKE_ONCE;
1777 rqf_last = req->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001778
Willy Tarreau8f128b42014-11-28 15:07:47 +01001779 if ((req->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001780 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001781 }
1782
Willy Tarreau576507f2010-01-07 00:09:04 +01001783 /* we'll monitor the request analysers while parsing the response,
1784 * because some response analysers may indirectly enable new request
1785 * analysers (eg: HTTP keep-alive).
1786 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001787 req_ana_back = req->analysers;
Willy Tarreau576507f2010-01-07 00:09:04 +01001788
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001789 resync_response:
1790 /* Analyse response */
1791
Willy Tarreau8f128b42014-11-28 15:07:47 +01001792 if (((res->flags & ~rpf_last) & CF_MASK_ANALYSER) ||
1793 (res->flags ^ rpf_last) & CF_MASK_STATIC ||
1794 si_f->state != rp_cons_last ||
1795 si_b->state != rp_prod_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001796 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001797 unsigned int flags = res->flags;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001798
Willy Tarreau8f128b42014-11-28 15:07:47 +01001799 if ((res->flags & CF_MASK_ANALYSER) &&
1800 (res->analysers & AN_REQ_ALL)) {
Willy Tarreau0499e352010-12-17 07:13:42 +01001801 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1802 * for some free space in the response buffer, so we might need to call
1803 * it when something changes in the response buffer, but still we pass
1804 * it the request buffer. Note that the SI state might very well still
1805 * be zero due to us returning a flow of redirects!
1806 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001807 res->analysers &= ~AN_REQ_ALL;
1808 req->flags |= CF_WAKE_ONCE;
Willy Tarreau0499e352010-12-17 07:13:42 +01001809 }
1810
Willy Tarreau8f128b42014-11-28 15:07:47 +01001811 if (si_b->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001812 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001813 unsigned int ana_list;
1814 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001815
Willy Tarreau90deb182010-01-07 00:20:41 +01001816 /* it's up to the analysers to stop disable reading or
1817 * closing. Note: if an analyser disables any of these
1818 * bits, it is responsible for enabling them again when
1819 * it disables itself, so that other analysers are called
1820 * in similar conditions.
1821 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001822 channel_auto_read(res);
1823 channel_auto_close(res);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001824
1825 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001826 * res->analysers, following the bit order from LSB
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001827 * to MSB. The analysers must remove themselves from
1828 * the list when not needed. Any analyser may return 0
1829 * to break out of the loop, either because of missing
1830 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001831 * kill the stream. We loop at least once through each
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001832 * analyser, and we may loop again if other analysers
1833 * are added in the middle.
1834 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001835
Willy Tarreau8f128b42014-11-28 15:07:47 +01001836 ana_list = ana_back = res->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001837 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001838 /* Warning! ensure that analysers are always placed in ascending order! */
1839
Emeric Brun97679e72010-09-23 17:56:44 +02001840 if (ana_list & AN_RES_INSPECT) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001841 if (!tcp_inspect_response(s, res, AN_RES_INSPECT))
Emeric Brun97679e72010-09-23 17:56:44 +02001842 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001843 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_INSPECT);
Emeric Brun97679e72010-09-23 17:56:44 +02001844 }
1845
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001846 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001847 if (!http_wait_for_response(s, res, AN_RES_WAIT_HTTP))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001848 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001849 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001850 }
1851
Emeric Brun1d33b292010-01-04 15:47:17 +01001852 if (ana_list & AN_RES_STORE_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001853 if (!process_store_rules(s, res, AN_RES_STORE_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01001854 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001855 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01001856 }
1857
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001858 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001859 if (!http_process_res_common(s, res, AN_RES_HTTP_PROCESS_BE, s->be))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001860 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001861 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001862 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001863
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001864 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001865 if (!http_response_forward_body(s, res, AN_RES_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01001866 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001867 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001868 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001869 break;
1870 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001871 }
1872
Willy Tarreau8f128b42014-11-28 15:07:47 +01001873 rp_cons_last = si_f->state;
1874 rp_prod_last = si_b->state;
1875 rpf_last = res->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001876
Willy Tarreau8f128b42014-11-28 15:07:47 +01001877 if ((res->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001878 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001879 }
1880
Willy Tarreau576507f2010-01-07 00:09:04 +01001881 /* maybe someone has added some request analysers, so we must check and loop */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001882 if (req->analysers & ~req_ana_back)
Willy Tarreau576507f2010-01-07 00:09:04 +01001883 goto resync_request;
1884
Willy Tarreau8f128b42014-11-28 15:07:47 +01001885 if ((req->flags & ~rqf_last) & CF_MASK_ANALYSER)
Willy Tarreau0499e352010-12-17 07:13:42 +01001886 goto resync_request;
1887
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001888 /* FIXME: here we should call protocol handlers which rely on
1889 * both buffers.
1890 */
1891
1892
1893 /*
Willy Tarreau87b09662015-04-03 00:22:06 +02001894 * Now we propagate unhandled errors to the stream. Normally
Willy Tarreauae526782010-03-04 20:34:23 +01001895 * we're just in a data phase here since it means we have not
1896 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001897 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001898 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001899 if (unlikely(!(s->flags & SF_ERR_MASK))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001900 if (req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001901 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001902 req->analysers = 0;
1903 if (req->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001904 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001905 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001906 if (srv)
1907 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001908 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001909 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001910 else if (req->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001911 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001912 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001913 if (srv)
1914 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001915 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001916 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001917 else if (req->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001918 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001919 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001920 if (srv)
1921 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001922 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001923 }
1924 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001925 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001926 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001927 if (srv)
1928 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001929 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001930 }
Willy Tarreau84455332009-03-15 22:34:05 +01001931 sess_set_term_flags(s);
1932 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001933 else if (res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001934 /* Report it if the server got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001935 res->analysers = 0;
1936 if (res->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001937 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001938 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001939 if (srv)
1940 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001941 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001942 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001943 else if (res->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001944 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001945 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001946 if (srv)
1947 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001948 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001949 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001950 else if (res->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001951 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001952 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001953 if (srv)
1954 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001955 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001956 }
1957 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001958 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001959 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001960 if (srv)
1961 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001962 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001963 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001964 sess_set_term_flags(s);
1965 }
Willy Tarreau84455332009-03-15 22:34:05 +01001966 }
1967
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001968 /*
1969 * Here we take care of forwarding unhandled data. This also includes
1970 * connection establishments and shutdown requests.
1971 */
1972
1973
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001974 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001975 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001976 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001977 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001978 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001979 if (unlikely(!req->analysers &&
1980 !(req->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
1981 (si_f->state >= SI_ST_EST) &&
1982 (req->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01001983 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001984 * attached to it. If any data are left in, we'll permit them to
1985 * move.
1986 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001987 channel_auto_read(req);
1988 channel_auto_connect(req);
1989 channel_auto_close(req);
1990 buffer_flush(req->buf);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001991
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001992 /* We'll let data flow between the producer (if still connected)
1993 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001994 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001995 if (!(req->flags & (CF_SHUTR|CF_SHUTW_NOW)))
1996 channel_forward(req, CHN_INFINITE_FORWARD);
Willy Tarreau42529c32015-07-09 18:38:57 +02001997
1998 /* Just in order to support fetching HTTP contents after start
1999 * of forwarding when the HTTP forwarding analyser is not used,
2000 * we simply reset msg->sov so that HTTP rewinding points to the
2001 * headers.
2002 */
2003 if (s->txn)
2004 s->txn->req.sov = s->txn->req.eoh + s->txn->req.eol - req->buf->o;
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002005 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002006
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002007 /* check if it is wise to enable kernel splicing to forward request data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002008 if (!(req->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2009 req->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002010 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002011 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->rcv_pipe) &&
2012 (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 +01002013 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002014 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
2015 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002016 (req->flags & CF_STREAMER_FAST)))) {
2017 req->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002018 }
2019
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002020 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002021 rqf_last = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002022
2023 /*
2024 * Now forward all shutdown requests between both sides of the buffer
2025 */
2026
Willy Tarreau520d95e2009-09-19 21:04:57 +02002027 /* first, let's check if the request buffer needs to shutdown(write), which may
2028 * happen either because the input is closed or because we want to force a close
Willy Tarreau05cdd962014-05-10 14:30:07 +02002029 * once the server has begun to respond. If a half-closed timeout is set, we adjust
2030 * the other side's timeout as well.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002031 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002032 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002033 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002034 channel_shutw_now(req);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002035 if (tick_isset(sess->fe->timeout.clientfin)) {
2036 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002037 res->wex = tick_add(now_ms, res->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002038 }
2039 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002040
2041 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002042 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2043 channel_is_empty(req))) {
2044 if (req->flags & CF_READ_ERROR)
2045 si_b->flags |= SI_FL_NOLINGER;
2046 si_shutw(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002047 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002048 res->rto = s->be->timeout.serverfin;
2049 res->rex = tick_add(now_ms, res->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002050 }
Willy Tarreau8615c2a2013-06-21 08:20:19 +02002051 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002052
2053 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002054 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW &&
2055 !req->analysers))
2056 channel_shutr_now(req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002057
2058 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002059 if (unlikely((req->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2060 if (si_f->flags & SI_FL_NOHALF)
2061 si_f->flags |= SI_FL_NOLINGER;
2062 si_shutr(si_f);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002063 if (tick_isset(sess->fe->timeout.clientfin)) {
2064 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002065 res->wex = tick_add(now_ms, res->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002066 }
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002067 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002068
Willy Tarreau520d95e2009-09-19 21:04:57 +02002069 /* it's possible that an upper layer has requested a connection setup or abort.
2070 * There are 2 situations where we decide to establish a new connection :
2071 * - there are data scheduled for emission in the buffer
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002072 * - the CF_AUTO_CONNECT flag is set (active connection)
Willy Tarreau520d95e2009-09-19 21:04:57 +02002073 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002074 if (si_b->state == SI_ST_INI) {
2075 if (!(req->flags & CF_SHUTW)) {
2076 if ((req->flags & CF_AUTO_CONNECT) || !channel_is_empty(req)) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002077 /* If we have an appctx, there is no connect method, so we
2078 * immediately switch to the connected state, otherwise we
2079 * perform a connection request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002080 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002081 si_b->state = SI_ST_REQ; /* new connection requested */
2082 si_b->conn_retries = s->be->conn_retries;
Willy Tarreau520d95e2009-09-19 21:04:57 +02002083 }
Willy Tarreau73201222009-08-16 18:27:24 +02002084 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002085 else {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002086 si_b->state = SI_ST_CLO; /* shutw+ini = abort */
2087 channel_shutw_now(req); /* fix buffer flags upon abort */
2088 channel_shutr_now(res);
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002089 }
Willy Tarreau92795622009-03-06 12:51:23 +01002090 }
2091
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002092
2093 /* we may have a pending connection request, or a connection waiting
2094 * for completion.
2095 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002096 if (si_b->state >= SI_ST_REQ && si_b->state < SI_ST_CON) {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02002097
2098 /* prune the request variables and swap to the response variables. */
2099 if (s->vars_reqres.scope != SCOPE_RES) {
2100 vars_prune(&s->vars_reqres, s);
2101 vars_init(&s->vars_reqres, SCOPE_RES);
2102 }
2103
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002104 do {
2105 /* nb: step 1 might switch from QUE to ASS, but we first want
2106 * to give a chance to step 2 to perform a redirect if needed.
2107 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002108 if (si_b->state != SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002109 sess_update_stream_int(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01002110 if (si_b->state == SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002111 sess_prepare_conn_req(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002112
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01002113 /* applets directly go to the ESTABLISHED state. Similarly,
2114 * servers experience the same fate when their connection
2115 * is reused.
2116 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002117 if (unlikely(si_b->state == SI_ST_EST))
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002118 sess_establish(s);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01002119
Willy Tarreaub44c8732013-12-31 23:16:50 +01002120 /* Now we can add the server name to a header (if requested) */
2121 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002122 if ((si_b->state >= SI_ST_CON) &&
Willy Tarreaub44c8732013-12-31 23:16:50 +01002123 (s->be->server_id_hdr_name != NULL) &&
2124 (s->be->mode == PR_MODE_HTTP) &&
2125 objt_server(s->target)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02002126 http_send_name_header(s->txn, s->be, objt_server(s->target)->id);
Willy Tarreau1e5dfda2013-04-07 18:19:16 +02002127 }
2128
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002129 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002130 if (si_b->state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SF_REDIRECTABLE))
Willy Tarreau8f128b42014-11-28 15:07:47 +01002131 http_perform_server_redirect(s, si_b);
2132 } while (si_b->state == SI_ST_ASS);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002133 }
2134
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002135 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002136 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002137 goto resync_stream_interface;
2138
Willy Tarreau815a9b22010-07-27 17:15:12 +02002139 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002140 if ((req->flags ^ rqf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002141 goto resync_request;
2142
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002143 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01002144
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002145 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002146 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002147 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002148 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002149 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002150 if (unlikely(!res->analysers &&
2151 !(res->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2152 (si_b->state >= SI_ST_EST) &&
2153 (res->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002154 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002155 * attached to it. If any data are left in, we'll permit them to
2156 * move.
2157 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002158 channel_auto_read(res);
2159 channel_auto_close(res);
2160 buffer_flush(res->buf);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002161
2162 /* We'll let data flow between the producer (if still connected)
2163 * to the consumer.
2164 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002165 if (!(res->flags & (CF_SHUTR|CF_SHUTW_NOW)))
2166 channel_forward(res, CHN_INFINITE_FORWARD);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002167
Willy Tarreau42529c32015-07-09 18:38:57 +02002168 /* Just in order to support fetching HTTP contents after start
2169 * of forwarding when the HTTP forwarding analyser is not used,
2170 * we simply reset msg->sov so that HTTP rewinding points to the
2171 * headers.
2172 */
2173 if (s->txn)
2174 s->txn->rsp.sov = s->txn->rsp.eoh + s->txn->rsp.eol - res->buf->o;
2175
Willy Tarreauce887fd2012-05-12 12:50:00 +02002176 /* if we have no analyser anymore in any direction and have a
Willy Tarreau05cdd962014-05-10 14:30:07 +02002177 * tunnel timeout set, use it now. Note that we must respect
2178 * the half-closed timeouts as well.
Willy Tarreauce887fd2012-05-12 12:50:00 +02002179 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002180 if (!req->analysers && s->be->timeout.tunnel) {
2181 req->rto = req->wto = res->rto = res->wto =
Willy Tarreauce887fd2012-05-12 12:50:00 +02002182 s->be->timeout.tunnel;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002183
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002184 if ((req->flags & CF_SHUTR) && tick_isset(sess->fe->timeout.clientfin))
2185 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002186 if ((req->flags & CF_SHUTW) && tick_isset(s->be->timeout.serverfin))
2187 res->rto = s->be->timeout.serverfin;
2188 if ((res->flags & CF_SHUTR) && tick_isset(s->be->timeout.serverfin))
2189 req->wto = s->be->timeout.serverfin;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002190 if ((res->flags & CF_SHUTW) && tick_isset(sess->fe->timeout.clientfin))
2191 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002192
Willy Tarreau8f128b42014-11-28 15:07:47 +01002193 req->rex = tick_add(now_ms, req->rto);
2194 req->wex = tick_add(now_ms, req->wto);
2195 res->rex = tick_add(now_ms, res->rto);
2196 res->wex = tick_add(now_ms, res->wto);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002197 }
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002198 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002199
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002200 /* check if it is wise to enable kernel splicing to forward response data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002201 if (!(res->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2202 res->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002203 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002204 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->snd_pipe) &&
2205 (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 +01002206 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002207 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2208 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002209 (res->flags & CF_STREAMER_FAST)))) {
2210 res->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002211 }
2212
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002213 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002214 rpf_last = res->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002215
2216 /*
2217 * Now forward all shutdown requests between both sides of the buffer
2218 */
2219
2220 /*
2221 * FIXME: this is probably where we should produce error responses.
2222 */
2223
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002224 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002225 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002226 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002227 channel_shutw_now(res);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002228 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002229 req->wto = s->be->timeout.serverfin;
2230 req->wex = tick_add(now_ms, req->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002231 }
2232 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002233
2234 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002235 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2236 channel_is_empty(res))) {
2237 si_shutw(si_f);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002238 if (tick_isset(sess->fe->timeout.clientfin)) {
2239 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002240 req->rex = tick_add(now_ms, req->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002241 }
2242 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002243
2244 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002245 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW) &&
2246 !res->analysers)
2247 channel_shutr_now(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002248
2249 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002250 if (unlikely((res->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2251 if (si_b->flags & SI_FL_NOHALF)
2252 si_b->flags |= SI_FL_NOLINGER;
2253 si_shutr(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002254 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002255 req->wto = s->be->timeout.serverfin;
2256 req->wex = tick_add(now_ms, req->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002257 }
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002258 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002259
Willy Tarreau8f128b42014-11-28 15:07:47 +01002260 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002261 goto resync_stream_interface;
2262
Willy Tarreau8f128b42014-11-28 15:07:47 +01002263 if (req->flags != rqf_last)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002264 goto resync_request;
2265
Willy Tarreau8f128b42014-11-28 15:07:47 +01002266 if ((res->flags ^ rpf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002267 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002268
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002269 /* we're interested in getting wakeups again */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002270 si_f->flags &= ~SI_FL_DONT_WAKE;
2271 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002272
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002273 /* This is needed only when debugging is enabled, to indicate
2274 * client-side or server-side close. Please note that in the unlikely
2275 * event where both sides would close at once, the sequence is reported
2276 * on the server side first.
2277 */
2278 if (unlikely((global.mode & MODE_DEBUG) &&
2279 (!(global.mode & MODE_QUIET) ||
2280 (global.mode & MODE_VERBOSE)))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002281 if (si_b->state == SI_ST_CLO &&
2282 si_b->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002283 chunk_printf(&trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002284 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002285 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2286 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002287 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002288 }
2289
Willy Tarreau8f128b42014-11-28 15:07:47 +01002290 if (si_f->state == SI_ST_CLO &&
2291 si_f->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002292 chunk_printf(&trash, "%08x:%s.clicls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002293 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002294 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2295 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002296 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002297 }
2298 }
2299
Willy Tarreau8f128b42014-11-28 15:07:47 +01002300 if (likely((si_f->state != SI_ST_CLO) ||
2301 (si_b->state > SI_ST_INI && si_b->state < SI_ST_CLO))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002302
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002303 if ((sess->fe->options & PR_O_CONTSTATS) && (s->flags & SF_BE_ASSIGNED))
Willy Tarreau87b09662015-04-03 00:22:06 +02002304 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002305
Willy Tarreau563cc372015-04-19 18:13:56 +02002306 if (si_f->state == SI_ST_EST)
Willy Tarreau8f128b42014-11-28 15:07:47 +01002307 si_update(si_f);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002308
Willy Tarreau563cc372015-04-19 18:13:56 +02002309 if (si_b->state == SI_ST_EST)
Willy Tarreau8f128b42014-11-28 15:07:47 +01002310 si_update(si_b);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002311
Willy Tarreau8f128b42014-11-28 15:07:47 +01002312 req->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2313 res->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2314 si_f->prev_state = si_f->state;
2315 si_b->prev_state = si_b->state;
2316 si_f->flags &= ~(SI_FL_ERR|SI_FL_EXP);
2317 si_b->flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002318
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002319 /* Trick: if a request is being waiting for the server to respond,
2320 * and if we know the server can timeout, we don't want the timeout
2321 * to expire on the client side first, but we're still interested
2322 * in passing data from the client to the server (eg: POST). Thus,
2323 * we can cancel the client's request timeout if the server's
2324 * request timeout is set and the server has not yet sent a response.
2325 */
2326
Willy Tarreau8f128b42014-11-28 15:07:47 +01002327 if ((res->flags & (CF_AUTO_CLOSE|CF_SHUTR)) == 0 &&
2328 (tick_isset(req->wex) || tick_isset(res->rex))) {
2329 req->flags |= CF_READ_NOEXP;
2330 req->rex = TICK_ETERNITY;
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002331 }
2332
Willy Tarreau798f4322012-11-08 14:49:17 +01002333 update_exp_and_leave:
Willy Tarreau8f128b42014-11-28 15:07:47 +01002334 t->expire = tick_first(tick_first(req->rex, req->wex),
2335 tick_first(res->rex, res->wex));
2336 if (req->analysers)
2337 t->expire = tick_first(t->expire, req->analyse_exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002338
Willy Tarreau8f128b42014-11-28 15:07:47 +01002339 if (si_f->exp)
2340 t->expire = tick_first(t->expire, si_f->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002341
Willy Tarreau8f128b42014-11-28 15:07:47 +01002342 if (si_b->exp)
2343 t->expire = tick_first(t->expire, si_b->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002344
2345#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002346 fprintf(stderr,
2347 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2348 " 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 +01002349 now_ms, t->expire, req->rex, req->wex, req->analyse_exp,
2350 res->rex, res->wex, si_f->exp, si_b->exp, si_f->state, si_b->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002351#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002352
2353#ifdef DEBUG_DEV
2354 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002355 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002356 ABORT_NOW();
2357#endif
Willy Tarreau87b09662015-04-03 00:22:06 +02002358 stream_release_buffers(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002359 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002360 }
2361
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002362 sess->fe->feconn--;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002363 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002364 s->be->beconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002365 jobs--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002366 if (sess->listener) {
2367 if (!(sess->listener->options & LI_O_UNLIMITED))
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002368 actconn--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002369 sess->listener->nbconn--;
2370 if (sess->listener->state == LI_FULL)
2371 resume_listener(sess->listener);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002372
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002373 /* Dequeues all of the listeners waiting for a resource */
2374 if (!LIST_ISEMPTY(&global_listener_queue))
2375 dequeue_all_listeners(&global_listener_queue);
Willy Tarreau08ceb102011-07-24 22:58:00 +02002376
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002377 if (!LIST_ISEMPTY(&sess->fe->listener_queue) &&
2378 (!sess->fe->fe_sps_lim || freq_ctr_remain(&sess->fe->fe_sess_per_sec, sess->fe->fe_sps_lim, 0) > 0))
2379 dequeue_all_listeners(&sess->fe->listener_queue);
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002380 }
Willy Tarreau07687c12011-07-24 23:55:06 +02002381
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002382 if (unlikely((global.mode & MODE_DEBUG) &&
2383 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002384 chunk_printf(&trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002385 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002386 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2387 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002388 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002389 }
2390
2391 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02002392 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002393
Willy Tarreaueee5b512015-04-03 23:46:31 +02002394 if (s->txn && s->txn->status) {
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002395 int n;
2396
Willy Tarreaueee5b512015-04-03 23:46:31 +02002397 n = s->txn->status / 100;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002398 if (n < 1 || n > 5)
2399 n = 0;
2400
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002401 if (sess->fe->mode == PR_MODE_HTTP) {
2402 sess->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002403 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002404 sess->fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002405 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02002406 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002407 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002408 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002409 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002410 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002411 s->be->be_counters.p.http.comp_rsp++;
2412 }
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002413 }
2414
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002415 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002416 if (!LIST_ISEMPTY(&sess->fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02002417 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002418 (!(sess->fe->options & PR_O_NULLNOLOG) || req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002419 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002420 }
2421
Willy Tarreau87b09662015-04-03 00:22:06 +02002422 /* update time stats for this stream */
2423 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002424
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002425 /* the task MUST not be in the run queue anymore */
Willy Tarreau87b09662015-04-03 00:22:06 +02002426 stream_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002427 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002428 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002429 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002430}
2431
Willy Tarreau87b09662015-04-03 00:22:06 +02002432/* Update the stream's backend and server time stats */
2433void stream_update_time_stats(struct stream *s)
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002434{
2435 int t_request;
2436 int t_queue;
2437 int t_connect;
2438 int t_data;
2439 int t_close;
2440 struct server *srv;
2441
2442 t_request = 0;
2443 t_queue = s->logs.t_queue;
2444 t_connect = s->logs.t_connect;
2445 t_close = s->logs.t_close;
2446 t_data = s->logs.t_data;
2447
2448 if (s->be->mode != PR_MODE_HTTP)
2449 t_data = t_connect;
2450
2451 if (t_connect < 0 || t_data < 0)
2452 return;
2453
2454 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
2455 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
2456
2457 t_data -= t_connect;
2458 t_connect -= t_queue;
2459 t_queue -= t_request;
2460
2461 srv = objt_server(s->target);
2462 if (srv) {
2463 swrate_add(&srv->counters.q_time, TIME_STATS_SAMPLES, t_queue);
2464 swrate_add(&srv->counters.c_time, TIME_STATS_SAMPLES, t_connect);
2465 swrate_add(&srv->counters.d_time, TIME_STATS_SAMPLES, t_data);
2466 swrate_add(&srv->counters.t_time, TIME_STATS_SAMPLES, t_close);
2467 }
2468 swrate_add(&s->be->be_counters.q_time, TIME_STATS_SAMPLES, t_queue);
2469 swrate_add(&s->be->be_counters.c_time, TIME_STATS_SAMPLES, t_connect);
2470 swrate_add(&s->be->be_counters.d_time, TIME_STATS_SAMPLES, t_data);
2471 swrate_add(&s->be->be_counters.t_time, TIME_STATS_SAMPLES, t_close);
2472}
2473
Willy Tarreau7c669d72008-06-20 15:04:11 +02002474/*
2475 * This function adjusts sess->srv_conn and maintains the previous and new
Willy Tarreau87b09662015-04-03 00:22:06 +02002476 * server's served stream counts. Setting newsrv to NULL is enough to release
Willy Tarreau7c669d72008-06-20 15:04:11 +02002477 * current connection slot. This function also notifies any LB algo which might
Willy Tarreau87b09662015-04-03 00:22:06 +02002478 * expect to be informed about any change in the number of active streams on a
Willy Tarreau7c669d72008-06-20 15:04:11 +02002479 * server.
2480 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002481void sess_change_server(struct stream *sess, struct server *newsrv)
Willy Tarreau7c669d72008-06-20 15:04:11 +02002482{
2483 if (sess->srv_conn == newsrv)
2484 return;
2485
2486 if (sess->srv_conn) {
2487 sess->srv_conn->served--;
2488 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2489 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Willy Tarreau87b09662015-04-03 00:22:06 +02002490 stream_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002491 }
2492
2493 if (newsrv) {
2494 newsrv->served++;
2495 if (newsrv->proxy->lbprm.server_take_conn)
2496 newsrv->proxy->lbprm.server_take_conn(newsrv);
Willy Tarreau87b09662015-04-03 00:22:06 +02002497 stream_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002498 }
2499}
2500
Willy Tarreau84455332009-03-15 22:34:05 +01002501/* Handle server-side errors for default protocols. It is called whenever a a
2502 * connection setup is aborted or a request is aborted in queue. It sets the
Willy Tarreau87b09662015-04-03 00:22:06 +02002503 * stream termination flags so that the caller does not have to worry about
Willy Tarreau84455332009-03-15 22:34:05 +01002504 * them. It's installed as ->srv_error for the server-side stream_interface.
2505 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002506void default_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreau84455332009-03-15 22:34:05 +01002507{
2508 int err_type = si->err_type;
2509 int err = 0, fin = 0;
2510
2511 if (err_type & SI_ET_QUEUE_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002512 err = SF_ERR_CLICL;
2513 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002514 }
2515 else if (err_type & SI_ET_CONN_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002516 err = SF_ERR_CLICL;
2517 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002518 }
2519 else if (err_type & SI_ET_QUEUE_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002520 err = SF_ERR_SRVTO;
2521 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002522 }
2523 else if (err_type & SI_ET_QUEUE_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002524 err = SF_ERR_SRVCL;
2525 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002526 }
2527 else if (err_type & SI_ET_CONN_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002528 err = SF_ERR_SRVTO;
2529 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002530 }
2531 else if (err_type & SI_ET_CONN_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002532 err = SF_ERR_SRVCL;
2533 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002534 }
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002535 else if (err_type & SI_ET_CONN_RES) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002536 err = SF_ERR_RESOURCE;
2537 fin = SF_FINST_C;
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002538 }
Willy Tarreau84455332009-03-15 22:34:05 +01002539 else /* SI_ET_CONN_OTHER and others */ {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002540 err = SF_ERR_INTERNAL;
2541 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002542 }
2543
Willy Tarreaue7dff022015-04-03 01:14:29 +02002544 if (!(s->flags & SF_ERR_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002545 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002546 if (!(s->flags & SF_FINST_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002547 s->flags |= fin;
2548}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002549
Willy Tarreaue7dff022015-04-03 01:14:29 +02002550/* kill a stream and set the termination flags to <why> (one of SF_ERR_*) */
Willy Tarreau87b09662015-04-03 00:22:06 +02002551void stream_shutdown(struct stream *stream, int why)
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002552{
Willy Tarreau87b09662015-04-03 00:22:06 +02002553 if (stream->req.flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002554 return;
2555
Willy Tarreau87b09662015-04-03 00:22:06 +02002556 channel_shutw_now(&stream->req);
2557 channel_shutr_now(&stream->res);
2558 stream->task->nice = 1024;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002559 if (!(stream->flags & SF_ERR_MASK))
Willy Tarreau87b09662015-04-03 00:22:06 +02002560 stream->flags |= why;
2561 task_wakeup(stream->task, TASK_WOKEN_OTHER);
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002562}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002563
Willy Tarreau8b22a712010-06-18 17:46:06 +02002564/************************************************************************/
2565/* All supported ACL keywords must be declared here. */
2566/************************************************************************/
2567
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002568/* Returns a pointer to a stkctr depending on the fetch keyword name.
2569 * It is designed to be called as sc[0-9]_* sc_* or src_* exclusively.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002570 * sc[0-9]_* will return a pointer to the respective field in the
Willy Tarreau87b09662015-04-03 00:22:06 +02002571 * stream <l4>. sc_* requires an UINT argument specifying the stick
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002572 * counter number. src_* will fill a locally allocated structure with
Willy Tarreaua65536c2013-07-22 22:40:11 +02002573 * the table and entry corresponding to what is specified with src_*.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002574 * NULL may be returned if the designated stkctr is not tracked. For
2575 * the sc_* and sc[0-9]_* forms, an optional table argument may be
2576 * passed. When present, the currently tracked key is then looked up
2577 * in the specified table instead of the current table. The purpose is
2578 * to be able to convery multiple values per key (eg: have gpc0 from
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002579 * multiple tables). <strm> is allowed to be NULL, in which case only
2580 * the session will be consulted.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002581 */
Willy Tarreaue12704b2014-07-15 19:06:18 +02002582struct stkctr *
Willy Tarreau192252e2015-04-04 01:47:55 +02002583smp_fetch_sc_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
Willy Tarreaua65536c2013-07-22 22:40:11 +02002584{
2585 static struct stkctr stkctr;
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002586 struct stkctr *stkptr;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002587 struct stksess *stksess;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002588 unsigned int num = kw[2] - '0';
Willy Tarreau0f791d42013-07-23 19:56:43 +02002589 int arg = 0;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002590
Willy Tarreau0f791d42013-07-23 19:56:43 +02002591 if (num == '_' - '0') {
2592 /* sc_* variant, args[0] = ctr# (mandatory) */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002593 num = args[arg++].data.sint;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002594 if (num >= MAX_SESS_STKCTR)
2595 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002596 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002597 else if (num > 9) { /* src_* variant, args[0] = table */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002598 struct stktable_key *key;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002599 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002600
2601 if (!conn)
2602 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002603
Thierry FOURNIER74c219d2014-04-14 14:35:40 +02002604 key = addr_to_stktable_key(&conn->addr.from, args->data.prx->table.type);
Willy Tarreaua65536c2013-07-22 22:40:11 +02002605 if (!key)
2606 return NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002607
Willy Tarreaua65536c2013-07-22 22:40:11 +02002608 stkctr.table = &args->data.prx->table;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002609 stkctr_set_entry(&stkctr, stktable_lookup_key(stkctr.table, key));
Willy Tarreaua65536c2013-07-22 22:40:11 +02002610 return &stkctr;
2611 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002612
2613 /* Here, <num> contains the counter number from 0 to 9 for
2614 * the sc[0-9]_ form, or even higher using sc_(num) if needed.
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002615 * args[arg] is the first optional argument. We first lookup the
2616 * ctr form the stream, then from the session if it was not there.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002617 */
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002618
2619 stkptr = &strm->stkctr[num];
2620 if (!strm || !stkctr_entry(stkptr)) {
2621 stkptr = &sess->stkctr[num];
2622 if (!stkctr_entry(stkptr))
2623 return NULL;
2624 }
2625
2626 stksess = stkctr_entry(stkptr);
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002627 if (!stksess)
2628 return NULL;
2629
Willy Tarreau0f791d42013-07-23 19:56:43 +02002630 if (unlikely(args[arg].type == ARGT_TAB)) {
2631 /* an alternate table was specified, let's look up the same key there */
2632 stkctr.table = &args[arg].data.prx->table;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002633 stkctr_set_entry(&stkctr, stktable_lookup(stkctr.table, stksess));
Willy Tarreau0f791d42013-07-23 19:56:43 +02002634 return &stkctr;
2635 }
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002636 return stkptr;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002637}
2638
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002639/* same as smp_fetch_sc_stkctr() but dedicated to src_* and can create
2640 * the entry if it doesn't exist yet. This is needed for a few fetch
2641 * functions which need to create an entry, such as src_inc_gpc* and
2642 * src_clr_gpc*.
2643 */
2644struct stkctr *
2645smp_create_src_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
2646{
2647 static struct stkctr stkctr;
2648 struct stktable_key *key;
2649 struct connection *conn = objt_conn(sess->origin);
2650
2651 if (strncmp(kw, "src_", 4) != 0)
2652 return NULL;
2653
2654 if (!conn)
2655 return NULL;
2656
2657 key = addr_to_stktable_key(&conn->addr.from, args->data.prx->table.type);
2658 if (!key)
2659 return NULL;
2660
2661 stkctr.table = &args->data.prx->table;
2662 stkctr_set_entry(&stkctr, stktable_update_key(stkctr.table, key));
2663 return &stkctr;
2664}
2665
Willy Tarreau87b09662015-04-03 00:22:06 +02002666/* set return a boolean indicating if the requested stream counter is
Willy Tarreau88821242013-07-22 18:29:29 +02002667 * currently being tracked or not.
2668 * Supports being called as "sc[0-9]_tracked" only.
2669 */
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002670static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002671smp_fetch_sc_tracked(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002672{
2673 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002674 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002675 smp->data.u.sint = !!smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002676 return 1;
2677}
2678
Willy Tarreau87b09662015-04-03 00:22:06 +02002679/* set <smp> to the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau30b20462013-07-22 19:46:52 +02002680 * frontend counters or from the src.
2681 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpc0" only. Value
2682 * zero is returned if the key is new.
2683 */
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002684static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002685smp_fetch_sc_get_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002686{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002687 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau30b20462013-07-22 19:46:52 +02002688
2689 if (!stkctr)
2690 return 0;
2691
Willy Tarreau37406352012-04-23 16:16:37 +02002692 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002693 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002694 smp->data.u.sint = 0;
Willy Tarreau30b20462013-07-22 19:46:52 +02002695
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002696 if (stkctr_entry(stkctr) != NULL) {
2697 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002698 if (!ptr)
2699 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002700 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002701 }
2702 return 1;
2703}
2704
Willy Tarreau87b09662015-04-03 00:22:06 +02002705/* set <smp> to the General Purpose Counter 0's event rate from the stream's
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002706 * tracked frontend counters or from the src.
2707 * Supports being called as "sc[0-9]_gpc0_rate" or "src_gpc0_rate" only.
2708 * Value zero is returned if the key is new.
2709 */
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002710static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002711smp_fetch_sc_gpc0_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002712{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002713 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002714
2715 if (!stkctr)
2716 return 0;
2717
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002718 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002719 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002720 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002721 if (stkctr_entry(stkctr) != NULL) {
2722 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002723 if (!ptr)
2724 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002725 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002726 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002727 }
2728 return 1;
2729}
2730
Willy Tarreau87b09662015-04-03 00:22:06 +02002731/* Increment the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau710d38c2013-07-23 00:07:04 +02002732 * frontend counters and return it into temp integer.
2733 * Supports being called as "sc[0-9]_inc_gpc0" or "src_inc_gpc0" only.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002734 */
2735static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002736smp_fetch_sc_inc_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002737{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002738 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau710d38c2013-07-23 00:07:04 +02002739
2740 if (!stkctr)
2741 return 0;
2742
Willy Tarreau37406352012-04-23 16:16:37 +02002743 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002744 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002745 smp->data.u.sint = 0;
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002746
2747 if (stkctr_entry(stkctr) == NULL)
2748 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw);
2749
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002750 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun57056f02015-06-15 18:29:57 +02002751 void *ptr1,*ptr2;
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002752
2753 /* First, update gpc0_rate if it's tracked. Second, update its
2754 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
2755 */
Emeric Brun57056f02015-06-15 18:29:57 +02002756 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
2757 if (ptr1) {
2758 update_freq_ctr_period(&stktable_data_cast(ptr1, gpc0_rate),
Willy Tarreau710d38c2013-07-23 00:07:04 +02002759 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002760 smp->data.u.sint = (&stktable_data_cast(ptr1, gpc0_rate))->curr_ctr;
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002761 }
2762
Emeric Brun57056f02015-06-15 18:29:57 +02002763 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
2764 if (ptr2)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002765 smp->data.u.sint = ++stktable_data_cast(ptr2, gpc0);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002766
Emeric Brun57056f02015-06-15 18:29:57 +02002767 /* If data was modified, we need to touch to re-schedule sync */
2768 if (ptr1 || ptr2)
2769 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002770 }
2771 return 1;
2772}
2773
Willy Tarreau87b09662015-04-03 00:22:06 +02002774/* Clear the General Purpose Counter 0 value from the stream's tracked
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002775 * frontend counters and return its previous value into temp integer.
2776 * Supports being called as "sc[0-9]_clr_gpc0" or "src_clr_gpc0" only.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002777 */
2778static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002779smp_fetch_sc_clr_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002780{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002781 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002782
2783 if (!stkctr)
2784 return 0;
2785
Willy Tarreau37406352012-04-23 16:16:37 +02002786 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002787 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002788 smp->data.u.sint = 0;
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002789
2790 if (stkctr_entry(stkctr) == NULL)
2791 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw);
2792
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002793 if (stkctr_entry(stkctr) != NULL) {
2794 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002795 if (!ptr)
2796 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002797 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002798 stktable_data_cast(ptr, gpc0) = 0;
Emeric Brun57056f02015-06-15 18:29:57 +02002799 /* If data was modified, we need to touch to re-schedule sync */
2800 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002801 }
2802 return 1;
2803}
2804
Willy Tarreau87b09662015-04-03 00:22:06 +02002805/* set <smp> to the cumulated number of connections from the stream's tracked
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002806 * frontend counters. Supports being called as "sc[0-9]_conn_cnt" or
2807 * "src_conn_cnt" only.
2808 */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002809static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002810smp_fetch_sc_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002811{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002812 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002813
2814 if (!stkctr)
2815 return 0;
2816
Willy Tarreau37406352012-04-23 16:16:37 +02002817 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002818 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002819 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002820 if (stkctr_entry(stkctr) != NULL) {
2821 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CNT);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002822 if (!ptr)
2823 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002824 smp->data.u.sint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002825 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002826 return 1;
2827}
2828
Willy Tarreau87b09662015-04-03 00:22:06 +02002829/* set <smp> to the connection rate from the stream's tracked frontend
Willy Tarreauc8c65702013-07-23 15:09:35 +02002830 * counters. Supports being called as "sc[0-9]_conn_rate" or "src_conn_rate"
2831 * only.
2832 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002833static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002834smp_fetch_sc_conn_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002835{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002836 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreauc8c65702013-07-23 15:09:35 +02002837
2838 if (!stkctr)
2839 return 0;
2840
Willy Tarreau37406352012-04-23 16:16:37 +02002841 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002842 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002843 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002844 if (stkctr_entry(stkctr) != NULL) {
2845 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002846 if (!ptr)
2847 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002848 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreauc8c65702013-07-23 15:09:35 +02002849 stkctr->table->data_arg[STKTABLE_DT_CONN_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002850 }
2851 return 1;
2852}
2853
Willy Tarreau87b09662015-04-03 00:22:06 +02002854/* set temp integer to the number of connections from the stream's source address
Willy Tarreau8b22a712010-06-18 17:46:06 +02002855 * in the table pointed to by expr, after updating it.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002856 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002857 */
2858static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002859smp_fetch_src_updt_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8b22a712010-06-18 17:46:06 +02002860{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002861 struct connection *conn = objt_conn(smp->sess->origin);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002862 struct stksess *ts;
2863 struct stktable_key *key;
2864 void *ptr;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002865 struct proxy *px;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002866
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002867 if (!conn)
2868 return 0;
2869
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002870 key = addr_to_stktable_key(&conn->addr.from, smp->px->table.type);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002871 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002872 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002873
Willy Tarreau24e32d82012-04-23 23:55:44 +02002874 px = args->data.prx;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002875
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002876 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2877 /* entry does not exist and could not be created */
2878 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002879
2880 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2881 if (!ptr)
2882 return 0; /* parameter not stored in this table */
2883
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002884 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002885 smp->data.u.sint = ++stktable_data_cast(ptr, conn_cnt);
Emeric Brun57056f02015-06-15 18:29:57 +02002886 /* Touch was previously performed by stktable_update_key */
Willy Tarreau37406352012-04-23 16:16:37 +02002887 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002888 return 1;
2889}
2890
Willy Tarreau87b09662015-04-03 00:22:06 +02002891/* set <smp> to the number of concurrent connections from the stream's tracked
Willy Tarreauf44a5532013-07-23 15:17:53 +02002892 * frontend counters. Supports being called as "sc[0-9]_conn_cur" or
2893 * "src_conn_cur" only.
2894 */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002895static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002896smp_fetch_sc_conn_cur(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002897{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002898 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreauf44a5532013-07-23 15:17:53 +02002899
2900 if (!stkctr)
2901 return 0;
2902
Willy Tarreau37406352012-04-23 16:16:37 +02002903 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002904 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002905 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002906 if (stkctr_entry(stkctr) != NULL) {
2907 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CUR);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002908 if (!ptr)
2909 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002910 smp->data.u.sint = stktable_data_cast(ptr, conn_cur);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002911 }
2912 return 1;
2913}
2914
Willy Tarreau87b09662015-04-03 00:22:06 +02002915/* set <smp> to the cumulated number of streams from the stream's tracked
Willy Tarreau20843082013-07-23 15:35:33 +02002916 * frontend counters. Supports being called as "sc[0-9]_sess_cnt" or
2917 * "src_sess_cnt" only.
2918 */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002919static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002920smp_fetch_sc_sess_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002921{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002922 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau20843082013-07-23 15:35:33 +02002923
2924 if (!stkctr)
2925 return 0;
2926
Willy Tarreau37406352012-04-23 16:16:37 +02002927 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002928 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002929 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002930 if (stkctr_entry(stkctr) != NULL) {
2931 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002932 if (!ptr)
2933 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002934 smp->data.u.sint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002935 }
2936 return 1;
2937}
2938
Willy Tarreau87b09662015-04-03 00:22:06 +02002939/* set <smp> to the stream rate from the stream's tracked frontend counters.
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02002940 * Supports being called as "sc[0-9]_sess_rate" or "src_sess_rate" only.
2941 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002942static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002943smp_fetch_sc_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002944{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002945 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02002946
2947 if (!stkctr)
2948 return 0;
2949
Willy Tarreau37406352012-04-23 16:16:37 +02002950 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002951 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002952 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002953 if (stkctr_entry(stkctr) != NULL) {
2954 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002955 if (!ptr)
2956 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002957 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02002958 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002959 }
2960 return 1;
2961}
2962
Willy Tarreau87b09662015-04-03 00:22:06 +02002963/* set <smp> to the cumulated number of HTTP requests from the stream's tracked
Willy Tarreau91200da2013-07-23 15:55:19 +02002964 * frontend counters. Supports being called as "sc[0-9]_http_req_cnt" or
2965 * "src_http_req_cnt" only.
2966 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002967static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002968smp_fetch_sc_http_req_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002969{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002970 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau91200da2013-07-23 15:55:19 +02002971
2972 if (!stkctr)
2973 return 0;
2974
Willy Tarreau37406352012-04-23 16:16:37 +02002975 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002976 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002977 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002978 if (stkctr_entry(stkctr) != NULL) {
2979 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002980 if (!ptr)
2981 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002982 smp->data.u.sint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002983 }
2984 return 1;
2985}
2986
Willy Tarreau87b09662015-04-03 00:22:06 +02002987/* set <smp> to the HTTP request rate from the stream's tracked frontend
Willy Tarreaucf477632013-07-23 16:04:37 +02002988 * counters. Supports being called as "sc[0-9]_http_req_rate" or
2989 * "src_http_req_rate" only.
2990 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002991static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002992smp_fetch_sc_http_req_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002993{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002994 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaucf477632013-07-23 16:04:37 +02002995
2996 if (!stkctr)
2997 return 0;
2998
Willy Tarreau37406352012-04-23 16:16:37 +02002999 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003000 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003001 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003002 if (stkctr_entry(stkctr) != NULL) {
3003 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003004 if (!ptr)
3005 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003006 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreaucf477632013-07-23 16:04:37 +02003007 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003008 }
3009 return 1;
3010}
3011
Willy Tarreau87b09662015-04-03 00:22:06 +02003012/* set <smp> to the cumulated number of HTTP requests errors from the stream's
Willy Tarreau30d07c32013-07-23 16:45:38 +02003013 * tracked frontend counters. Supports being called as "sc[0-9]_http_err_cnt" or
3014 * "src_http_err_cnt" only.
3015 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003016static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003017smp_fetch_sc_http_err_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003018{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003019 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau30d07c32013-07-23 16:45:38 +02003020
3021 if (!stkctr)
3022 return 0;
3023
Willy Tarreau37406352012-04-23 16:16:37 +02003024 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003025 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003026 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003027 if (stkctr_entry(stkctr) != NULL) {
3028 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003029 if (!ptr)
3030 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003031 smp->data.u.sint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003032 }
3033 return 1;
3034}
3035
Willy Tarreau87b09662015-04-03 00:22:06 +02003036/* set <smp> to the HTTP request error rate from the stream's tracked frontend
Willy Tarreau9daf2622013-07-23 16:48:54 +02003037 * counters. Supports being called as "sc[0-9]_http_err_rate" or
3038 * "src_http_err_rate" only.
3039 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003040static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003041smp_fetch_sc_http_err_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003042{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003043 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau9daf2622013-07-23 16:48:54 +02003044
3045 if (!stkctr)
3046 return 0;
3047
Willy Tarreau37406352012-04-23 16:16:37 +02003048 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003049 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003050 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003051 if (stkctr_entry(stkctr) != NULL) {
3052 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003053 if (!ptr)
3054 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003055 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau9daf2622013-07-23 16:48:54 +02003056 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003057 }
3058 return 1;
3059}
3060
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003061/* set <smp> to the number of kbytes received from clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003062 * stream's tracked frontend counters. Supports being called as
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003063 * "sc[0-9]_kbytes_in" or "src_kbytes_in" only.
3064 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003065static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003066smp_fetch_sc_kbytes_in(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003067{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003068 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003069
3070 if (!stkctr)
3071 return 0;
3072
Willy Tarreau37406352012-04-23 16:16:37 +02003073 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003074 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003075 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003076 if (stkctr_entry(stkctr) != NULL) {
3077 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003078 if (!ptr)
3079 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003080 smp->data.u.sint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003081 }
3082 return 1;
3083}
3084
Willy Tarreau613fe992013-07-23 17:39:19 +02003085/* set <smp> to the data rate received from clients in bytes/s, as found
Willy Tarreau87b09662015-04-03 00:22:06 +02003086 * in the stream's tracked frontend counters. Supports being called as
Willy Tarreau613fe992013-07-23 17:39:19 +02003087 * "sc[0-9]_bytes_in_rate" or "src_bytes_in_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003088 */
3089static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003090smp_fetch_sc_bytes_in_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003091{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003092 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau613fe992013-07-23 17:39:19 +02003093
3094 if (!stkctr)
3095 return 0;
3096
Willy Tarreau37406352012-04-23 16:16:37 +02003097 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003098 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003099 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003100 if (stkctr_entry(stkctr) != NULL) {
3101 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003102 if (!ptr)
3103 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003104 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau613fe992013-07-23 17:39:19 +02003105 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003106 }
3107 return 1;
3108}
3109
Willy Tarreau53aea102013-07-23 17:39:02 +02003110/* set <smp> to the number of kbytes sent to clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003111 * stream's tracked frontend counters. Supports being called as
Willy Tarreau53aea102013-07-23 17:39:02 +02003112 * "sc[0-9]_kbytes_out" or "src_kbytes_out" only.
3113 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003114static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003115smp_fetch_sc_kbytes_out(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003116{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003117 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau53aea102013-07-23 17:39:02 +02003118
3119 if (!stkctr)
3120 return 0;
3121
Willy Tarreau37406352012-04-23 16:16:37 +02003122 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003123 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003124 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003125 if (stkctr_entry(stkctr) != NULL) {
3126 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003127 if (!ptr)
3128 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003129 smp->data.u.sint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003130 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003131 return 1;
3132}
3133
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003134/* set <smp> to the data rate sent to clients in bytes/s, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003135 * stream's tracked frontend counters. Supports being called as
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003136 * "sc[0-9]_bytes_out_rate" or "src_bytes_out_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003137 */
3138static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003139smp_fetch_sc_bytes_out_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003140{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003141 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003142
3143 if (!stkctr)
3144 return 0;
3145
Willy Tarreau37406352012-04-23 16:16:37 +02003146 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003147 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003148 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003149 if (stkctr_entry(stkctr) != NULL) {
3150 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003151 if (!ptr)
3152 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003153 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003154 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003155 }
3156 return 1;
3157}
3158
Willy Tarreau87b09662015-04-03 00:22:06 +02003159/* set <smp> to the number of active trackers on the SC entry in the stream's
Willy Tarreau563eef42013-07-23 18:32:02 +02003160 * tracked frontend counters. Supports being called as "sc[0-9]_trackers" only.
3161 */
Willy Tarreau2406db42012-12-09 12:16:43 +01003162static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003163smp_fetch_sc_trackers(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2406db42012-12-09 12:16:43 +01003164{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003165 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau2406db42012-12-09 12:16:43 +01003166
Willy Tarreau563eef42013-07-23 18:32:02 +02003167 if (!stkctr)
Willy Tarreau2406db42012-12-09 12:16:43 +01003168 return 0;
3169
Willy Tarreau563eef42013-07-23 18:32:02 +02003170 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003171 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003172 smp->data.u.sint = stkctr_entry(stkctr)->ref_cnt;
Willy Tarreau563eef42013-07-23 18:32:02 +02003173 return 1;
Willy Tarreaue25c9172013-05-28 18:32:20 +02003174}
3175
Willy Tarreau34db1082012-04-19 17:16:54 +02003176/* set temp integer to the number of used entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003177 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003178 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003179static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003180smp_fetch_table_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003181{
Willy Tarreau37406352012-04-23 16:16:37 +02003182 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003183 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003184 smp->data.u.sint = args->data.prx->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003185 return 1;
3186}
3187
Willy Tarreau34db1082012-04-19 17:16:54 +02003188/* set temp integer to the number of free entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003189 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003190 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003191static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003192smp_fetch_table_avl(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003193{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003194 struct proxy *px;
3195
Willy Tarreau24e32d82012-04-23 23:55:44 +02003196 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003197 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003198 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003199 smp->data.u.sint = px->table.size - px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003200 return 1;
3201}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003202
Willy Tarreau61612d42012-04-19 18:42:05 +02003203/* Note: must not be declared <const> as its list will be overwritten.
3204 * Please take care of keeping this list alphabetically sorted.
3205 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003206static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau281c7992013-01-08 01:23:27 +01003207 { /* END */ },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003208}};
3209
Willy Tarreau281c7992013-01-08 01:23:27 +01003210/* Note: must not be declared <const> as its list will be overwritten.
3211 * Please take care of keeping this list alphabetically sorted.
3212 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003213static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003214 { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3215 { "sc_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3216 { "sc_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3217 { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3218 { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3219 { "sc_conn_rate", smp_fetch_sc_conn_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3220 { "sc_get_gpc0", smp_fetch_sc_get_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3221 { "sc_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3222 { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3223 { "sc_http_err_rate", smp_fetch_sc_http_err_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3224 { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3225 { "sc_http_req_rate", smp_fetch_sc_http_req_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3226 { "sc_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3227 { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3228 { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3229 { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3230 { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3231 { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,SINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3232 { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003233 { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3234 { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3235 { "sc0_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3236 { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3237 { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3238 { "sc0_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3239 { "sc0_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3240 { "sc0_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3241 { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3242 { "sc0_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3243 { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3244 { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3245 { "sc0_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3246 { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3247 { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3248 { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3249 { "sc0_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau0f791d42013-07-23 19:56:43 +02003250 { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003251 { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3252 { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3253 { "sc1_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3254 { "sc1_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3255 { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3256 { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3257 { "sc1_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3258 { "sc1_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3259 { "sc1_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3260 { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3261 { "sc1_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3262 { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3263 { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3264 { "sc1_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3265 { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3266 { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3267 { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3268 { "sc1_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau0f791d42013-07-23 19:56:43 +02003269 { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003270 { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3271 { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3272 { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3273 { "sc2_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3274 { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3275 { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3276 { "sc2_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3277 { "sc2_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3278 { "sc2_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3279 { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3280 { "sc2_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3281 { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3282 { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3283 { "sc2_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3284 { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3285 { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3286 { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3287 { "sc2_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau0f791d42013-07-23 19:56:43 +02003288 { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003289 { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3290 { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3291 { "src_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3292 { "src_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3293 { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3294 { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3295 { "src_conn_rate", smp_fetch_sc_conn_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3296 { "src_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3297 { "src_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3298 { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3299 { "src_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3300 { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3301 { "src_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3302 { "src_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3303 { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3304 { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3305 { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3306 { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3307 { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3308 { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3309 { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau281c7992013-01-08 01:23:27 +01003310 { /* END */ },
3311}};
3312
Willy Tarreau8b22a712010-06-18 17:46:06 +02003313__attribute__((constructor))
Willy Tarreau87b09662015-04-03 00:22:06 +02003314static void __stream_init(void)
Willy Tarreau8b22a712010-06-18 17:46:06 +02003315{
Willy Tarreau281c7992013-01-08 01:23:27 +01003316 sample_register_fetches(&smp_fetch_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003317 acl_register_keywords(&acl_kws);
3318}
3319
Willy Tarreaubaaee002006-06-26 02:48:02 +02003320/*
3321 * Local variables:
3322 * c-indent-level: 8
3323 * c-basic-offset: 8
3324 * End:
3325 */